usb: gadget: create a utility module for mass_storage
[deliverable/linux.git] / drivers / usb / gadget / mass_storage.c
1 /*
2 * mass_storage.c -- Mass Storage USB Gadget
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz <mina86@mina86.com>
7 * All rights reserved.
8 *
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.
13 */
14
15
16 /*
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
23 * in a USB host.
24 *
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.
28 */
29
30
31 #include <linux/kernel.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/module.h>
34
35 /*-------------------------------------------------------------------------*/
36
37 #define DRIVER_DESC "Mass Storage Gadget"
38 #define DRIVER_VERSION "2009/09/11"
39
40 /*
41 * Thanks to NetChip Technologies for donating this product ID.
42 *
43 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
44 * Instead: allocate your own, using normal USB-IF procedures.
45 */
46 #define FSG_VENDOR_ID 0x0525 /* NetChip */
47 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
48
49 /*-------------------------------------------------------------------------*/
50
51 /*
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.
57 */
58 #include "f_mass_storage.c"
59
60 /*-------------------------------------------------------------------------*/
61 USB_GADGET_COMPOSITE_OPTIONS();
62
63 static struct usb_device_descriptor msg_device_desc = {
64 .bLength = sizeof msg_device_desc,
65 .bDescriptorType = USB_DT_DEVICE,
66
67 .bcdUSB = cpu_to_le16(0x0200),
68 .bDeviceClass = USB_CLASS_PER_INTERFACE,
69
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,
74 };
75
76 static struct usb_otg_descriptor otg_descriptor = {
77 .bLength = sizeof otg_descriptor,
78 .bDescriptorType = USB_DT_OTG,
79
80 /*
81 * REVISIT SRP-only hardware is possible, although
82 * it would not be called "OTG" ...
83 */
84 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
85 };
86
87 static const struct usb_descriptor_header *otg_desc[] = {
88 (struct usb_descriptor_header *) &otg_descriptor,
89 NULL,
90 };
91
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 = "",
96 { } /* end of list */
97 };
98
99 static struct usb_gadget_strings stringtab_dev = {
100 .language = 0x0409, /* en-us */
101 .strings = strings_dev,
102 };
103
104 static struct usb_gadget_strings *dev_strings[] = {
105 &stringtab_dev,
106 NULL,
107 };
108
109 /****************************** Configurations ******************************/
110
111 static struct fsg_module_parameters mod_data = {
112 .stall = 1
113 };
114 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
115
116 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
117
118 #else
119
120 /*
121 * Number of buffers we will use.
122 * 2 is usually enough for good buffering pipeline
123 */
124 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
125
126 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
127
128 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
129
130 static unsigned long msg_registered;
131 static void msg_cleanup(void);
132
133 static int msg_thread_exits(struct fsg_common *common)
134 {
135 msg_cleanup();
136 return 0;
137 }
138
139 static int __init msg_do_config(struct usb_configuration *c)
140 {
141 static const struct fsg_operations ops = {
142 .thread_exits = msg_thread_exits,
143 };
144 static struct fsg_common common;
145
146 struct fsg_common *retp;
147 struct fsg_config config;
148 int ret;
149
150 if (gadget_is_otg(c->cdev->gadget)) {
151 c->descriptors = otg_desc;
152 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
153 }
154
155 fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
156 config.ops = &ops;
157
158 retp = fsg_common_init(&common, c->cdev, &config);
159 if (IS_ERR(retp))
160 return PTR_ERR(retp);
161
162 ret = fsg_bind_config(c->cdev, c, &common);
163 fsg_common_put(&common);
164 return ret;
165 }
166
167 static struct usb_configuration msg_config_driver = {
168 .label = "Linux File-Backed Storage",
169 .bConfigurationValue = 1,
170 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
171 };
172
173
174 /****************************** Gadget Bind ******************************/
175
176 static int __init msg_bind(struct usb_composite_dev *cdev)
177 {
178 int status;
179
180 status = usb_string_ids_tab(cdev, strings_dev);
181 if (status < 0)
182 return status;
183 msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
184
185 status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
186 if (status < 0)
187 return status;
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);
192 return 0;
193 }
194
195
196 /****************************** Some noise ******************************/
197
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,
202 .needs_serial = 1,
203 .strings = dev_strings,
204 .bind = msg_bind,
205 };
206
207 MODULE_DESCRIPTION(DRIVER_DESC);
208 MODULE_AUTHOR("Michal Nazarewicz");
209 MODULE_LICENSE("GPL");
210
211 static int __init msg_init(void)
212 {
213 return usb_composite_probe(&msg_driver);
214 }
215 module_init(msg_init);
216
217 static void msg_cleanup(void)
218 {
219 if (test_and_clear_bit(0, &msg_registered))
220 usb_composite_unregister(&msg_driver);
221 }
222 module_exit(msg_cleanup);
This page took 0.036754 seconds and 6 git commands to generate.