usb: gadget: f_mass_storage: convert to new function interface with backward compatib...
[firefly-linux-kernel-4.4.55.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 #define USB_FMS_INCLUDED
59 #include "f_mass_storage.c"
60
61 /*-------------------------------------------------------------------------*/
62 USB_GADGET_COMPOSITE_OPTIONS();
63
64 static struct usb_device_descriptor msg_device_desc = {
65         .bLength =              sizeof msg_device_desc,
66         .bDescriptorType =      USB_DT_DEVICE,
67
68         .bcdUSB =               cpu_to_le16(0x0200),
69         .bDeviceClass =         USB_CLASS_PER_INTERFACE,
70
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,
75 };
76
77 static struct usb_otg_descriptor otg_descriptor = {
78         .bLength =              sizeof otg_descriptor,
79         .bDescriptorType =      USB_DT_OTG,
80
81         /*
82          * REVISIT SRP-only hardware is possible, although
83          * it would not be called "OTG" ...
84          */
85         .bmAttributes =         USB_OTG_SRP | USB_OTG_HNP,
86 };
87
88 static const struct usb_descriptor_header *otg_desc[] = {
89         (struct usb_descriptor_header *) &otg_descriptor,
90         NULL,
91 };
92
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 = "",
97         {  } /* end of list */
98 };
99
100 static struct usb_gadget_strings stringtab_dev = {
101         .language       = 0x0409,       /* en-us */
102         .strings        = strings_dev,
103 };
104
105 static struct usb_gadget_strings *dev_strings[] = {
106         &stringtab_dev,
107         NULL,
108 };
109
110 /****************************** Configurations ******************************/
111
112 static struct fsg_module_parameters mod_data = {
113         .stall = 1
114 };
115 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
116
117 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
118
119 #else
120
121 /*
122  * Number of buffers we will use.
123  * 2 is usually enough for good buffering pipeline
124  */
125 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
126
127 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
128
129 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
130
131 static unsigned long msg_registered;
132 static void msg_cleanup(void);
133
134 static int msg_thread_exits(struct fsg_common *common)
135 {
136         msg_cleanup();
137         return 0;
138 }
139
140 static int __init msg_do_config(struct usb_configuration *c)
141 {
142         static const struct fsg_operations ops = {
143                 .thread_exits = msg_thread_exits,
144         };
145         static struct fsg_common common;
146
147         struct fsg_common *retp;
148         struct fsg_config config;
149         int ret;
150
151         if (gadget_is_otg(c->cdev->gadget)) {
152                 c->descriptors = otg_desc;
153                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
154         }
155
156         fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
157         config.ops = &ops;
158
159         retp = fsg_common_init(&common, c->cdev, &config);
160         if (IS_ERR(retp))
161                 return PTR_ERR(retp);
162
163         ret = fsg_bind_config(c->cdev, c, &common);
164         fsg_common_put(&common);
165         return ret;
166 }
167
168 static struct usb_configuration msg_config_driver = {
169         .label                  = "Linux File-Backed Storage",
170         .bConfigurationValue    = 1,
171         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
172 };
173
174
175 /****************************** Gadget Bind ******************************/
176
177 static int __init msg_bind(struct usb_composite_dev *cdev)
178 {
179         int status;
180
181         status = usb_string_ids_tab(cdev, strings_dev);
182         if (status < 0)
183                 return status;
184         msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
185
186         status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
187         if (status < 0)
188                 return status;
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);
193         return 0;
194 }
195
196
197 /****************************** Some noise ******************************/
198
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,
203         .needs_serial   = 1,
204         .strings        = dev_strings,
205         .bind           = msg_bind,
206 };
207
208 MODULE_DESCRIPTION(DRIVER_DESC);
209 MODULE_AUTHOR("Michal Nazarewicz");
210 MODULE_LICENSE("GPL");
211
212 static int __init msg_init(void)
213 {
214         return usb_composite_probe(&msg_driver);
215 }
216 module_init(msg_init);
217
218 static void msg_cleanup(void)
219 {
220         if (test_and_clear_bit(0, &msg_registered))
221                 usb_composite_unregister(&msg_driver);
222 }
223 module_exit(msg_cleanup);