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 /*-------------------------------------------------------------------------*/
52 * kbuild is not very cooperative with respect to linking separately
53 * compiled library objects into one module. So for now we won't use
54 * separate compilation ... ensuring init/exit sections work to shrink
55 * the runtime footprint, and giving us at least some parts of what
56 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
58 #define USB_FMS_INCLUDED
59 #include "f_mass_storage.c"
61 /*-------------------------------------------------------------------------*/
62 USB_GADGET_COMPOSITE_OPTIONS();
64 static struct usb_device_descriptor msg_device_desc
= {
65 .bLength
= sizeof msg_device_desc
,
66 .bDescriptorType
= USB_DT_DEVICE
,
68 .bcdUSB
= cpu_to_le16(0x0200),
69 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
71 /* Vendor and product id can be overridden by module parameters. */
72 .idVendor
= cpu_to_le16(FSG_VENDOR_ID
),
73 .idProduct
= cpu_to_le16(FSG_PRODUCT_ID
),
74 .bNumConfigurations
= 1,
77 static struct usb_otg_descriptor otg_descriptor
= {
78 .bLength
= sizeof otg_descriptor
,
79 .bDescriptorType
= USB_DT_OTG
,
82 * REVISIT SRP-only hardware is possible, although
83 * it would not be called "OTG" ...
85 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
88 static const struct usb_descriptor_header
*otg_desc
[] = {
89 (struct usb_descriptor_header
*) &otg_descriptor
,
93 static struct usb_string strings_dev
[] = {
94 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
95 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
96 [USB_GADGET_SERIAL_IDX
].s
= "",
100 static struct usb_gadget_strings stringtab_dev
= {
101 .language
= 0x0409, /* en-us */
102 .strings
= strings_dev
,
105 static struct usb_gadget_strings
*dev_strings
[] = {
110 /****************************** Configurations ******************************/
112 static struct fsg_module_parameters mod_data
= {
115 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
117 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
122 * Number of buffers we will use.
123 * 2 is usually enough for good buffering pipeline
125 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
127 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
129 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data
);
131 static unsigned long msg_registered
;
132 static void msg_cleanup(void);
134 static int msg_thread_exits(struct fsg_common
*common
)
140 static int __init
msg_do_config(struct usb_configuration
*c
)
142 static const struct fsg_operations ops
= {
143 .thread_exits
= msg_thread_exits
,
145 static struct fsg_common common
;
147 struct fsg_common
*retp
;
148 struct fsg_config config
;
151 if (gadget_is_otg(c
->cdev
->gadget
)) {
152 c
->descriptors
= otg_desc
;
153 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
156 fsg_config_from_params(&config
, &mod_data
, fsg_num_buffers
);
159 retp
= fsg_common_init(&common
, c
->cdev
, &config
);
161 return PTR_ERR(retp
);
163 ret
= fsg_bind_config(c
->cdev
, c
, &common
);
164 fsg_common_put(&common
);
168 static struct usb_configuration msg_config_driver
= {
169 .label
= "Linux File-Backed Storage",
170 .bConfigurationValue
= 1,
171 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
175 /****************************** Gadget Bind ******************************/
177 static int __init
msg_bind(struct usb_composite_dev
*cdev
)
181 status
= usb_string_ids_tab(cdev
, strings_dev
);
184 msg_device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
186 status
= usb_add_config(cdev
, &msg_config_driver
, msg_do_config
);
189 usb_composite_overwrite_options(cdev
, &coverwrite
);
190 dev_info(&cdev
->gadget
->dev
,
191 DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
192 set_bit(0, &msg_registered
);
197 /****************************** Some noise ******************************/
199 static __refdata
struct usb_composite_driver msg_driver
= {
200 .name
= "g_mass_storage",
201 .dev
= &msg_device_desc
,
202 .max_speed
= USB_SPEED_SUPER
,
204 .strings
= dev_strings
,
208 MODULE_DESCRIPTION(DRIVER_DESC
);
209 MODULE_AUTHOR("Michal Nazarewicz");
210 MODULE_LICENSE("GPL");
212 static int __init
msg_init(void)
214 return usb_composite_probe(&msg_driver
);
216 module_init(msg_init
);
218 static void msg_cleanup(void)
220 if (test_and_clear_bit(0, &msg_registered
))
221 usb_composite_unregister(&msg_driver
);
223 module_exit(msg_cleanup
);