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 <m.nazarewicz@samsung.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/utsname.h>
33 #include <linux/usb/ch9.h>
36 /*-------------------------------------------------------------------------*/
38 #define DRIVER_DESC "Mass Storage Gadget"
39 #define DRIVER_VERSION "2009/09/11"
41 /*-------------------------------------------------------------------------*/
44 * kbuild is not very cooperative with respect to linking separately
45 * compiled library objects into one module. So for now we won't use
46 * separate compilation ... ensuring init/exit sections work to shrink
47 * the runtime footprint, and giving us at least some parts of what
48 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
51 #include "composite.c"
52 #include "usbstring.c"
54 #include "epautoconf.c"
55 #include "f_mass_storage.c"
57 /*-------------------------------------------------------------------------*/
59 static struct usb_device_descriptor msg_device_desc
= {
60 .bLength
= sizeof msg_device_desc
,
61 .bDescriptorType
= USB_DT_DEVICE
,
63 .bcdUSB
= cpu_to_le16(0x0200),
64 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
66 /* Vendor and product id can be overridden by module parameters. */
67 .idVendor
= cpu_to_le16(FSG_VENDOR_ID
),
68 .idProduct
= cpu_to_le16(FSG_PRODUCT_ID
),
69 .bNumConfigurations
= 1,
72 static struct usb_otg_descriptor otg_descriptor
= {
73 .bLength
= sizeof otg_descriptor
,
74 .bDescriptorType
= USB_DT_OTG
,
77 * REVISIT SRP-only hardware is possible, although
78 * it would not be called "OTG" ...
80 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
83 static const struct usb_descriptor_header
*otg_desc
[] = {
84 (struct usb_descriptor_header
*) &otg_descriptor
,
89 /****************************** Configurations ******************************/
91 static struct fsg_module_parameters mod_data
= {
94 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data
);
96 static unsigned long msg_registered
;
97 static void msg_cleanup(void);
99 static int msg_thread_exits(struct fsg_common
*common
)
105 static int __init
msg_do_config(struct usb_configuration
*c
)
107 static const struct fsg_operations ops
= {
108 .thread_exits
= msg_thread_exits
,
110 static struct fsg_common common
;
112 struct fsg_common
*retp
;
113 struct fsg_config config
;
116 if (gadget_is_otg(c
->cdev
->gadget
)) {
117 c
->descriptors
= otg_desc
;
118 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
121 fsg_config_from_params(&config
, &mod_data
);
124 retp
= fsg_common_init(&common
, c
->cdev
, &config
);
126 return PTR_ERR(retp
);
128 ret
= fsg_bind_config(c
->cdev
, c
, &common
);
129 fsg_common_put(&common
);
133 static struct usb_configuration msg_config_driver
= {
134 .label
= "Linux File-Backed Storage",
135 .bConfigurationValue
= 1,
136 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
140 /****************************** Gadget Bind ******************************/
142 static int __init
msg_bind(struct usb_composite_dev
*cdev
)
146 status
= usb_add_config(cdev
, &msg_config_driver
, msg_do_config
);
150 dev_info(&cdev
->gadget
->dev
,
151 DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
152 set_bit(0, &msg_registered
);
157 /****************************** Some noise ******************************/
159 static struct usb_composite_driver msg_driver
= {
160 .name
= "g_mass_storage",
161 .dev
= &msg_device_desc
,
162 .iProduct
= DRIVER_DESC
,
163 .max_speed
= USB_SPEED_HIGH
,
167 MODULE_DESCRIPTION(DRIVER_DESC
);
168 MODULE_AUTHOR("Michal Nazarewicz");
169 MODULE_LICENSE("GPL");
171 static int __init
msg_init(void)
173 return usb_composite_probe(&msg_driver
, msg_bind
);
175 module_init(msg_init
);
177 static void msg_cleanup(void)
179 if (test_and_clear_bit(0, &msg_registered
))
180 usb_composite_unregister(&msg_driver
);
182 module_exit(msg_cleanup
);