2 * mass_storage.c -- Mass Storage USB Gadget
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz <mina86@mina86.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
17 * The Mass Storage Gadget acts as a USB Mass Storage device,
18 * appearing to the host as a disk drive or as a CD-ROM drive. In
19 * addition to providing an example of a genuinely useful gadget
20 * driver for a USB device, it also illustrates a technique of
21 * double-buffering for increased throughput. Last but not least, it
22 * gives an easy way to probe the behavior of the Mass Storage drivers
25 * Since this file serves only administrative purposes and all the
26 * business logic is implemented in f_mass_storage.* file. Read
27 * comments in this file for more detailed description.
31 #include <linux/kernel.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/module.h>
35 /*-------------------------------------------------------------------------*/
37 #define DRIVER_DESC "Mass Storage Gadget"
38 #define DRIVER_VERSION "2009/09/11"
41 * Thanks to NetChip Technologies for donating this product ID.
43 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
44 * Instead: allocate your own, using normal USB-IF procedures.
46 #define FSG_VENDOR_ID 0x0525 /* NetChip */
47 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
49 #include "f_mass_storage.h"
51 /*-------------------------------------------------------------------------*/
52 USB_GADGET_COMPOSITE_OPTIONS();
54 static struct usb_device_descriptor msg_device_desc
= {
55 .bLength
= sizeof msg_device_desc
,
56 .bDescriptorType
= USB_DT_DEVICE
,
58 .bcdUSB
= cpu_to_le16(0x0200),
59 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
61 /* Vendor and product id can be overridden by module parameters. */
62 .idVendor
= cpu_to_le16(FSG_VENDOR_ID
),
63 .idProduct
= cpu_to_le16(FSG_PRODUCT_ID
),
64 .bNumConfigurations
= 1,
67 static struct usb_otg_descriptor otg_descriptor
= {
68 .bLength
= sizeof otg_descriptor
,
69 .bDescriptorType
= USB_DT_OTG
,
72 * REVISIT SRP-only hardware is possible, although
73 * it would not be called "OTG" ...
75 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
78 static const struct usb_descriptor_header
*otg_desc
[] = {
79 (struct usb_descriptor_header
*) &otg_descriptor
,
83 static struct usb_string strings_dev
[] = {
84 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
85 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
86 [USB_GADGET_SERIAL_IDX
].s
= "",
90 static struct usb_gadget_strings stringtab_dev
= {
91 .language
= 0x0409, /* en-us */
92 .strings
= strings_dev
,
95 static struct usb_gadget_strings
*dev_strings
[] = {
100 static struct usb_function_instance
*fi_msg
;
101 static struct usb_function
*f_msg
;
103 /****************************** Configurations ******************************/
105 static struct fsg_module_parameters mod_data
= {
108 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
110 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
115 * Number of buffers we will use.
116 * 2 is usually enough for good buffering pipeline
118 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
120 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
122 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data
);
124 static unsigned long msg_registered
;
125 static void msg_cleanup(void);
127 static int msg_thread_exits(struct fsg_common
*common
)
133 static int msg_do_config(struct usb_configuration
*c
)
135 struct fsg_opts
*opts
;
138 if (gadget_is_otg(c
->cdev
->gadget
)) {
139 c
->descriptors
= otg_desc
;
140 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
143 opts
= fsg_opts_from_func_inst(fi_msg
);
145 f_msg
= usb_get_function(fi_msg
);
147 return PTR_ERR(f_msg
);
149 ret
= fsg_common_run_thread(opts
->common
);
153 ret
= usb_add_function(c
, f_msg
);
160 usb_put_function(f_msg
);
164 static struct usb_configuration msg_config_driver
= {
165 .label
= "Linux File-Backed Storage",
166 .bConfigurationValue
= 1,
167 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
171 /****************************** Gadget Bind ******************************/
173 static int msg_bind(struct usb_composite_dev
*cdev
)
175 static const struct fsg_operations ops
= {
176 .thread_exits
= msg_thread_exits
,
178 struct fsg_opts
*opts
;
179 struct fsg_config config
;
182 fi_msg
= usb_get_function_instance("mass_storage");
184 return PTR_ERR(fi_msg
);
186 fsg_config_from_params(&config
, &mod_data
, fsg_num_buffers
);
187 opts
= fsg_opts_from_func_inst(fi_msg
);
189 opts
->no_configfs
= true;
190 status
= fsg_common_set_num_buffers(opts
->common
, fsg_num_buffers
);
194 status
= fsg_common_set_nluns(opts
->common
, config
.nluns
);
198 fsg_common_set_ops(opts
->common
, &ops
);
200 status
= fsg_common_set_cdev(opts
->common
, cdev
, config
.can_stall
);
204 fsg_common_set_sysfs(opts
->common
, true);
205 status
= fsg_common_create_luns(opts
->common
, &config
);
209 fsg_common_set_inquiry_string(opts
->common
, config
.vendor_name
,
210 config
.product_name
);
212 status
= usb_string_ids_tab(cdev
, strings_dev
);
214 goto fail_string_ids
;
215 msg_device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
217 status
= usb_add_config(cdev
, &msg_config_driver
, msg_do_config
);
219 goto fail_string_ids
;
221 usb_composite_overwrite_options(cdev
, &coverwrite
);
222 dev_info(&cdev
->gadget
->dev
,
223 DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
224 set_bit(0, &msg_registered
);
228 fsg_common_remove_luns(opts
->common
);
230 fsg_common_free_luns(opts
->common
);
232 fsg_common_free_buffers(opts
->common
);
234 usb_put_function_instance(fi_msg
);
238 static int msg_unbind(struct usb_composite_dev
*cdev
)
241 usb_put_function(f_msg
);
244 usb_put_function_instance(fi_msg
);
249 /****************************** Some noise ******************************/
251 static struct usb_composite_driver msg_driver
= {
252 .name
= "g_mass_storage",
253 .dev
= &msg_device_desc
,
254 .max_speed
= USB_SPEED_SUPER
,
256 .strings
= dev_strings
,
258 .unbind
= msg_unbind
,
261 MODULE_DESCRIPTION(DRIVER_DESC
);
262 MODULE_AUTHOR("Michal Nazarewicz");
263 MODULE_LICENSE("GPL");
265 static int __init
msg_init(void)
267 return usb_composite_probe(&msg_driver
);
269 module_init(msg_init
);
271 static void msg_cleanup(void)
273 if (test_and_clear_bit(0, &msg_registered
))
274 usb_composite_unregister(&msg_driver
);
276 module_exit(msg_cleanup
);