blob: e61af53c7d2b3d8512b7c6ac588a7f64af81a1ab [file] [log] [blame]
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01001/*
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002 * mass_storage.c -- Mass Storage USB Gadget
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003 *
4 * Copyright (C) 2003-2008 Alan Stern
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01005 * Copyright (C) 2009 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01006 * Author: Michal Nazarewicz <mina86@mina86.com>
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01007 * All rights reserved.
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01008 *
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.
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010013 */
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>
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010032#include <linux/usb/ch9.h>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020033#include <linux/module.h>
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010034
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010035/*-------------------------------------------------------------------------*/
36
37#define DRIVER_DESC "Mass Storage Gadget"
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010038#define DRIVER_VERSION "2009/09/11"
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010039
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020040/*
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
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +020049#include "f_mass_storage.h"
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010050
51/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020052USB_GADGET_COMPOSITE_OPTIONS();
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010053
54static struct usb_device_descriptor msg_device_desc = {
55 .bLength = sizeof msg_device_desc,
56 .bDescriptorType = USB_DT_DEVICE,
57
Igor Kotrasinski0aecfc12015-10-20 18:33:13 +020058 /* .bcdUSB = DYNAMIC */
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010059 .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),
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010064 .bNumConfigurations = 1,
65};
66
Li Jund8678892015-07-09 15:18:55 +080067static const struct usb_descriptor_header *otg_desc[2];
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010068
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +020069static struct usb_string strings_dev[] = {
70 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +020071 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +020072 [USB_GADGET_SERIAL_IDX].s = "",
73 { } /* end of list */
74};
75
76static struct usb_gadget_strings stringtab_dev = {
77 .language = 0x0409, /* en-us */
78 .strings = strings_dev,
79};
80
81static struct usb_gadget_strings *dev_strings[] = {
82 &stringtab_dev,
83 NULL,
84};
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010085
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +020086static struct usb_function_instance *fi_msg;
87static struct usb_function *f_msg;
88
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010089/****************************** Configurations ******************************/
90
Michal Nazarewicz481e4922009-11-09 14:15:21 +010091static struct fsg_module_parameters mod_data = {
92 .stall = 1
93};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020094#ifdef CONFIG_USB_GADGET_DEBUG_FILES
95
96static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
97
98#else
99
100/*
101 * Number of buffers we will use.
102 * 2 is usually enough for good buffering pipeline
103 */
104#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
105
106#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
107
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100108FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
109
Michal Nazarewiczb73af612010-10-28 17:31:23 +0200110static unsigned long msg_registered;
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100111static void msg_cleanup(void);
112
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +0100113static int msg_thread_exits(struct fsg_common *common)
114{
115 msg_cleanup();
116 return 0;
117}
118
Arnd Bergmannc94e289f2015-04-11 00:14:21 +0200119static int msg_do_config(struct usb_configuration *c)
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100120{
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200121 struct fsg_opts *opts;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100122 int ret;
123
124 if (gadget_is_otg(c->cdev->gadget)) {
125 c->descriptors = otg_desc;
126 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
127 }
128
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200129 opts = fsg_opts_from_func_inst(fi_msg);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100130
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200131 f_msg = usb_get_function(fi_msg);
132 if (IS_ERR(f_msg))
133 return PTR_ERR(f_msg);
Michal Nazarewicz26eca102010-06-16 12:07:56 +0200134
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200135 ret = fsg_common_run_thread(opts->common);
136 if (ret)
137 goto put_func;
138
139 ret = usb_add_function(c, f_msg);
140 if (ret)
141 goto put_func;
142
143 return 0;
144
145put_func:
146 usb_put_function(f_msg);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100147 return ret;
148}
149
150static struct usb_configuration msg_config_driver = {
151 .label = "Linux File-Backed Storage",
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100152 .bConfigurationValue = 1,
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100153 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
154};
155
156
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100157/****************************** Gadget Bind ******************************/
158
Arnd Bergmannc94e289f2015-04-11 00:14:21 +0200159static int msg_bind(struct usb_composite_dev *cdev)
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100160{
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200161 static const struct fsg_operations ops = {
162 .thread_exits = msg_thread_exits,
163 };
164 struct fsg_opts *opts;
165 struct fsg_config config;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100166 int status;
167
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200168 fi_msg = usb_get_function_instance("mass_storage");
169 if (IS_ERR(fi_msg))
170 return PTR_ERR(fi_msg);
171
172 fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
173 opts = fsg_opts_from_func_inst(fi_msg);
174
175 opts->no_configfs = true;
176 status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
177 if (status)
178 goto fail;
179
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200180 fsg_common_set_ops(opts->common, &ops);
181
182 status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
183 if (status)
184 goto fail_set_cdev;
185
186 fsg_common_set_sysfs(opts->common, true);
187 status = fsg_common_create_luns(opts->common, &config);
188 if (status)
189 goto fail_set_cdev;
190
191 fsg_common_set_inquiry_string(opts->common, config.vendor_name,
192 config.product_name);
193
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +0200194 status = usb_string_ids_tab(cdev, strings_dev);
195 if (status < 0)
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200196 goto fail_string_ids;
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200197 msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +0200198
Li Jund8678892015-07-09 15:18:55 +0800199 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
200 struct usb_descriptor_header *usb_desc;
201
202 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
203 if (!usb_desc)
204 goto fail_string_ids;
205 usb_otg_descriptor_init(cdev->gadget, usb_desc);
206 otg_desc[0] = usb_desc;
207 otg_desc[1] = NULL;
208 }
209
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200210 status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100211 if (status < 0)
Li Jund8678892015-07-09 15:18:55 +0800212 goto fail_otg_desc;
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200213
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200214 usb_composite_overwrite_options(cdev, &coverwrite);
Michal Nazarewicz7c2b61d2010-08-12 17:43:47 +0200215 dev_info(&cdev->gadget->dev,
216 DRIVER_DESC ", version: " DRIVER_VERSION "\n");
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100217 set_bit(0, &msg_registered);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100218 return 0;
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200219
Li Jund8678892015-07-09 15:18:55 +0800220fail_otg_desc:
221 kfree(otg_desc[0]);
222 otg_desc[0] = NULL;
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200223fail_string_ids:
224 fsg_common_remove_luns(opts->common);
225fail_set_cdev:
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200226 fsg_common_free_buffers(opts->common);
227fail:
228 usb_put_function_instance(fi_msg);
229 return status;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100230}
231
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200232static int msg_unbind(struct usb_composite_dev *cdev)
233{
234 if (!IS_ERR(f_msg))
235 usb_put_function(f_msg);
236
237 if (!IS_ERR(fi_msg))
238 usb_put_function_instance(fi_msg);
239
Li Jund8678892015-07-09 15:18:55 +0800240 kfree(otg_desc[0]);
241 otg_desc[0] = NULL;
242
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200243 return 0;
244}
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100245
246/****************************** Some noise ******************************/
247
Arnd Bergmannc94e289f2015-04-11 00:14:21 +0200248static struct usb_composite_driver msg_driver = {
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100249 .name = "g_mass_storage",
250 .dev = &msg_device_desc,
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300251 .max_speed = USB_SPEED_SUPER,
Michal Nazarewicz7c2b61d2010-08-12 17:43:47 +0200252 .needs_serial = 1,
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +0200253 .strings = dev_strings,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200254 .bind = msg_bind,
Andrzej Pietrasiewicz2412fbf2013-10-09 10:06:02 +0200255 .unbind = msg_unbind,
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100256};
257
258MODULE_DESCRIPTION(DRIVER_DESC);
259MODULE_AUTHOR("Michal Nazarewicz");
260MODULE_LICENSE("GPL");
261
262static int __init msg_init(void)
263{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200264 return usb_composite_probe(&msg_driver);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100265}
266module_init(msg_init);
267
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100268static void msg_cleanup(void)
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100269{
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100270 if (test_and_clear_bit(0, &msg_registered))
271 usb_composite_unregister(&msg_driver);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100272}
273module_exit(msg_cleanup);