]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/mass_storage.c
MLK-9617-2 usb: gadget: set bcdOTG of OTG descriptor for gadget drivers
[karo-tx-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 #include "f_mass_storage.h"
50
51 /*-------------------------------------------------------------------------*/
52 USB_GADGET_COMPOSITE_OPTIONS();
53
54 static struct usb_device_descriptor msg_device_desc = {
55         .bLength =              sizeof msg_device_desc,
56         .bDescriptorType =      USB_DT_DEVICE,
57
58         .bcdUSB =               cpu_to_le16(0x0200),
59         .bDeviceClass =         USB_CLASS_PER_INTERFACE,
60
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,
65 };
66
67 static struct usb_otg_descriptor otg_descriptor = {
68         .bLength =              sizeof otg_descriptor,
69         .bDescriptorType =      USB_DT_OTG,
70
71         /*
72          * REVISIT SRP-only hardware is possible, although
73          * it would not be called "OTG" ...
74          */
75         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
76         .bcdOTG =               cpu_to_le16(0x0200),
77 };
78
79 static const struct usb_descriptor_header *otg_desc[] = {
80         (struct usb_descriptor_header *) &otg_descriptor,
81         NULL,
82 };
83
84 static struct usb_string strings_dev[] = {
85         [USB_GADGET_MANUFACTURER_IDX].s = "",
86         [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
87         [USB_GADGET_SERIAL_IDX].s = "",
88         {  } /* end of list */
89 };
90
91 static struct usb_gadget_strings stringtab_dev = {
92         .language       = 0x0409,       /* en-us */
93         .strings        = strings_dev,
94 };
95
96 static struct usb_gadget_strings *dev_strings[] = {
97         &stringtab_dev,
98         NULL,
99 };
100
101 static struct usb_function_instance *fi_msg;
102 static struct usb_function *f_msg;
103
104 /****************************** Configurations ******************************/
105
106 static struct fsg_module_parameters mod_data = {
107         .stall = 1
108 };
109 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
110
111 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
112
113 #else
114
115 /*
116  * Number of buffers we will use.
117  * 2 is usually enough for good buffering pipeline
118  */
119 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
120
121 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
122
123 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
124
125 static unsigned long msg_registered;
126 static void msg_cleanup(void);
127
128 static int msg_thread_exits(struct fsg_common *common)
129 {
130         msg_cleanup();
131         return 0;
132 }
133
134 static int __init msg_do_config(struct usb_configuration *c)
135 {
136         struct fsg_opts *opts;
137         int ret;
138
139         if (gadget_is_otg(c->cdev->gadget)) {
140                 c->descriptors = otg_desc;
141                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
142         }
143
144         opts = fsg_opts_from_func_inst(fi_msg);
145
146         f_msg = usb_get_function(fi_msg);
147         if (IS_ERR(f_msg))
148                 return PTR_ERR(f_msg);
149
150         ret = fsg_common_run_thread(opts->common);
151         if (ret)
152                 goto put_func;
153
154         ret = usb_add_function(c, f_msg);
155         if (ret)
156                 goto put_func;
157
158         return 0;
159
160 put_func:
161         usb_put_function(f_msg);
162         return ret;
163 }
164
165 static struct usb_configuration msg_config_driver = {
166         .label                  = "Linux File-Backed Storage",
167         .bConfigurationValue    = 1,
168         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
169 };
170
171
172 /****************************** Gadget Bind ******************************/
173
174 static int __init msg_bind(struct usb_composite_dev *cdev)
175 {
176         static const struct fsg_operations ops = {
177                 .thread_exits = msg_thread_exits,
178         };
179         struct fsg_opts *opts;
180         struct fsg_config config;
181         int status;
182
183         fi_msg = usb_get_function_instance("mass_storage");
184         if (IS_ERR(fi_msg))
185                 return PTR_ERR(fi_msg);
186
187         fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
188         opts = fsg_opts_from_func_inst(fi_msg);
189
190         opts->no_configfs = true;
191         status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
192         if (status)
193                 goto fail;
194
195         status = fsg_common_set_nluns(opts->common, config.nluns);
196         if (status)
197                 goto fail_set_nluns;
198
199         fsg_common_set_ops(opts->common, &ops);
200
201         status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
202         if (status)
203                 goto fail_set_cdev;
204
205         fsg_common_set_sysfs(opts->common, true);
206         status = fsg_common_create_luns(opts->common, &config);
207         if (status)
208                 goto fail_set_cdev;
209
210         fsg_common_set_inquiry_string(opts->common, config.vendor_name,
211                                       config.product_name);
212
213         status = usb_string_ids_tab(cdev, strings_dev);
214         if (status < 0)
215                 goto fail_string_ids;
216         msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
217
218         status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
219         if (status < 0)
220                 goto fail_string_ids;
221
222         usb_composite_overwrite_options(cdev, &coverwrite);
223         dev_info(&cdev->gadget->dev,
224                  DRIVER_DESC ", version: " DRIVER_VERSION "\n");
225         set_bit(0, &msg_registered);
226         return 0;
227
228 fail_string_ids:
229         fsg_common_remove_luns(opts->common);
230 fail_set_cdev:
231         fsg_common_free_luns(opts->common);
232 fail_set_nluns:
233         fsg_common_free_buffers(opts->common);
234 fail:
235         usb_put_function_instance(fi_msg);
236         return status;
237 }
238
239 static int msg_unbind(struct usb_composite_dev *cdev)
240 {
241         if (!IS_ERR(f_msg))
242                 usb_put_function(f_msg);
243
244         if (!IS_ERR(fi_msg))
245                 usb_put_function_instance(fi_msg);
246
247         return 0;
248 }
249
250 /****************************** Some noise ******************************/
251
252 static __refdata struct usb_composite_driver msg_driver = {
253         .name           = "g_mass_storage",
254         .dev            = &msg_device_desc,
255         .max_speed      = USB_SPEED_SUPER,
256         .needs_serial   = 1,
257         .strings        = dev_strings,
258         .bind           = msg_bind,
259         .unbind         = msg_unbind,
260 };
261
262 MODULE_DESCRIPTION(DRIVER_DESC);
263 MODULE_AUTHOR("Michal Nazarewicz");
264 MODULE_LICENSE("GPL");
265
266 static int __init msg_init(void)
267 {
268         return usb_composite_probe(&msg_driver);
269 }
270 late_initcall(msg_init);
271
272 static void msg_cleanup(void)
273 {
274         if (test_and_clear_bit(0, &msg_registered))
275                 usb_composite_unregister(&msg_driver);
276 }
277 module_exit(msg_cleanup);