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 #include "f_mass_storage.c"
60 /*-------------------------------------------------------------------------*/
61 USB_GADGET_COMPOSITE_OPTIONS();
63 static struct usb_device_descriptor msg_device_desc
= {
64 .bLength
= sizeof msg_device_desc
,
65 .bDescriptorType
= USB_DT_DEVICE
,
67 .bcdUSB
= cpu_to_le16(0x0200),
68 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
70 /* Vendor and product id can be overridden by module parameters. */
71 .idVendor
= cpu_to_le16(FSG_VENDOR_ID
),
72 .idProduct
= cpu_to_le16(FSG_PRODUCT_ID
),
73 .bNumConfigurations
= 1,
76 static struct usb_otg_descriptor otg_descriptor
= {
77 .bLength
= sizeof otg_descriptor
,
78 .bDescriptorType
= USB_DT_OTG
,
81 * REVISIT SRP-only hardware is possible, although
82 * it would not be called "OTG" ...
84 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
87 static const struct usb_descriptor_header
*otg_desc
[] = {
88 (struct usb_descriptor_header
*) &otg_descriptor
,
92 static struct usb_string strings_dev
[] = {
93 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
94 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
95 [USB_GADGET_SERIAL_IDX
].s
= "",
99 static struct usb_gadget_strings stringtab_dev
= {
100 .language
= 0x0409, /* en-us */
101 .strings
= strings_dev
,
104 static struct usb_gadget_strings
*dev_strings
[] = {
109 /****************************** Configurations ******************************/
111 static struct fsg_module_parameters mod_data
= {
114 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
116 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
121 * Number of buffers we will use.
122 * 2 is usually enough for good buffering pipeline
124 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
126 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
128 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data
);
130 static unsigned long msg_registered
;
131 static void msg_cleanup(void);
133 static int msg_thread_exits(struct fsg_common
*common
)
139 static int __init
msg_do_config(struct usb_configuration
*c
)
141 static const struct fsg_operations ops
= {
142 .thread_exits
= msg_thread_exits
,
144 static struct fsg_common common
;
146 struct fsg_common
*retp
;
147 struct fsg_config config
;
150 if (gadget_is_otg(c
->cdev
->gadget
)) {
151 c
->descriptors
= otg_desc
;
152 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
155 fsg_config_from_params(&config
, &mod_data
, fsg_num_buffers
);
158 retp
= fsg_common_init(&common
, c
->cdev
, &config
);
160 return PTR_ERR(retp
);
162 ret
= fsg_bind_config(c
->cdev
, c
, &common
);
163 fsg_common_put(&common
);
167 static struct usb_configuration msg_config_driver
= {
168 .label
= "Linux File-Backed Storage",
169 .bConfigurationValue
= 1,
170 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
174 /****************************** Gadget Bind ******************************/
176 static int __init
msg_bind(struct usb_composite_dev
*cdev
)
180 status
= usb_string_ids_tab(cdev
, strings_dev
);
183 msg_device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
185 status
= usb_add_config(cdev
, &msg_config_driver
, msg_do_config
);
188 usb_composite_overwrite_options(cdev
, &coverwrite
);
189 dev_info(&cdev
->gadget
->dev
,
190 DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
191 set_bit(0, &msg_registered
);
196 /****************************** Some noise ******************************/
198 static __refdata
struct usb_composite_driver msg_driver
= {
199 .name
= "g_mass_storage",
200 .dev
= &msg_device_desc
,
201 .max_speed
= USB_SPEED_SUPER
,
203 .strings
= dev_strings
,
207 MODULE_DESCRIPTION(DRIVER_DESC
);
208 MODULE_AUTHOR("Michal Nazarewicz");
209 MODULE_LICENSE("GPL");
211 static int __init
msg_init(void)
213 return usb_composite_probe(&msg_driver
);
215 module_init(msg_init
);
217 static void msg_cleanup(void)
219 if (test_and_clear_bit(0, &msg_registered
))
220 usb_composite_unregister(&msg_driver
);
222 module_exit(msg_cleanup
);