Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 1 | /* |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 2 | * mass_storage.c -- Mass Storage USB Gadget |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003-2008 Alan Stern |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 5 | * Copyright (C) 2009 Samsung Electronics |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 6 | * Author: Michal Nazarewicz <mina86@mina86.com> |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 7 | * All rights reserved. |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 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. |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 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> |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 32 | #include <linux/usb/ch9.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 33 | #include <linux/module.h> |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 34 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 35 | /*-------------------------------------------------------------------------*/ |
| 36 | |
| 37 | #define DRIVER_DESC "Mass Storage Gadget" |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 38 | #define DRIVER_VERSION "2009/09/11" |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 39 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 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 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 49 | #include "f_mass_storage.h" |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 50 | |
| 51 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 52 | USB_GADGET_COMPOSITE_OPTIONS(); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 53 | |
| 54 | static struct usb_device_descriptor msg_device_desc = { |
| 55 | .bLength = sizeof msg_device_desc, |
| 56 | .bDescriptorType = USB_DT_DEVICE, |
| 57 | |
Igor Kotrasinski | 0aecfc1 | 2015-10-20 18:33:13 +0200 | [diff] [blame] | 58 | /* .bcdUSB = DYNAMIC */ |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 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), |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 64 | .bNumConfigurations = 1, |
| 65 | }; |
| 66 | |
Li Jun | d867889 | 2015-07-09 15:18:55 +0800 | [diff] [blame] | 67 | static const struct usb_descriptor_header *otg_desc[2]; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 68 | |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 69 | static struct usb_string strings_dev[] = { |
| 70 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 71 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 72 | [USB_GADGET_SERIAL_IDX].s = "", |
| 73 | { } /* end of list */ |
| 74 | }; |
| 75 | |
| 76 | static struct usb_gadget_strings stringtab_dev = { |
| 77 | .language = 0x0409, /* en-us */ |
| 78 | .strings = strings_dev, |
| 79 | }; |
| 80 | |
| 81 | static struct usb_gadget_strings *dev_strings[] = { |
| 82 | &stringtab_dev, |
| 83 | NULL, |
| 84 | }; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 85 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 86 | static struct usb_function_instance *fi_msg; |
| 87 | static struct usb_function *f_msg; |
| 88 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 89 | /****************************** Configurations ******************************/ |
| 90 | |
Michal Nazarewicz | 481e492 | 2009-11-09 14:15:21 +0100 | [diff] [blame] | 91 | static struct fsg_module_parameters mod_data = { |
| 92 | .stall = 1 |
| 93 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 94 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 95 | |
| 96 | static 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 Nazarewicz | 481e492 | 2009-11-09 14:15:21 +0100 | [diff] [blame] | 108 | FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); |
| 109 | |
Michal Nazarewicz | b73af61 | 2010-10-28 17:31:23 +0200 | [diff] [blame] | 110 | static unsigned long msg_registered; |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 111 | static void msg_cleanup(void); |
| 112 | |
Michal Nazarewicz | 7f1ee82 | 2010-01-28 13:05:26 +0100 | [diff] [blame] | 113 | static int msg_thread_exits(struct fsg_common *common) |
| 114 | { |
| 115 | msg_cleanup(); |
| 116 | return 0; |
| 117 | } |
| 118 | |
Arnd Bergmann | c94e289f | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 119 | static int msg_do_config(struct usb_configuration *c) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 120 | { |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 121 | struct fsg_opts *opts; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 122 | 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 Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 129 | opts = fsg_opts_from_func_inst(fi_msg); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 130 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 131 | f_msg = usb_get_function(fi_msg); |
| 132 | if (IS_ERR(f_msg)) |
| 133 | return PTR_ERR(f_msg); |
Michal Nazarewicz | 26eca10 | 2010-06-16 12:07:56 +0200 | [diff] [blame] | 134 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 135 | ret = usb_add_function(c, f_msg); |
| 136 | if (ret) |
| 137 | goto put_func; |
| 138 | |
| 139 | return 0; |
| 140 | |
| 141 | put_func: |
| 142 | usb_put_function(f_msg); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static struct usb_configuration msg_config_driver = { |
| 147 | .label = "Linux File-Backed Storage", |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 148 | .bConfigurationValue = 1, |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 149 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 150 | }; |
| 151 | |
| 152 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 153 | /****************************** Gadget Bind ******************************/ |
| 154 | |
Arnd Bergmann | c94e289f | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 155 | static int msg_bind(struct usb_composite_dev *cdev) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 156 | { |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 157 | static const struct fsg_operations ops = { |
| 158 | .thread_exits = msg_thread_exits, |
| 159 | }; |
| 160 | struct fsg_opts *opts; |
| 161 | struct fsg_config config; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 162 | int status; |
| 163 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 164 | fi_msg = usb_get_function_instance("mass_storage"); |
| 165 | if (IS_ERR(fi_msg)) |
| 166 | return PTR_ERR(fi_msg); |
| 167 | |
| 168 | fsg_config_from_params(&config, &mod_data, fsg_num_buffers); |
| 169 | opts = fsg_opts_from_func_inst(fi_msg); |
| 170 | |
| 171 | opts->no_configfs = true; |
| 172 | status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers); |
| 173 | if (status) |
| 174 | goto fail; |
| 175 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 176 | fsg_common_set_ops(opts->common, &ops); |
| 177 | |
| 178 | status = fsg_common_set_cdev(opts->common, cdev, config.can_stall); |
| 179 | if (status) |
| 180 | goto fail_set_cdev; |
| 181 | |
| 182 | fsg_common_set_sysfs(opts->common, true); |
| 183 | status = fsg_common_create_luns(opts->common, &config); |
| 184 | if (status) |
| 185 | goto fail_set_cdev; |
| 186 | |
| 187 | fsg_common_set_inquiry_string(opts->common, config.vendor_name, |
| 188 | config.product_name); |
| 189 | |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 190 | status = usb_string_ids_tab(cdev, strings_dev); |
| 191 | if (status < 0) |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 192 | goto fail_string_ids; |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 193 | msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 194 | |
Li Jun | d867889 | 2015-07-09 15:18:55 +0800 | [diff] [blame] | 195 | if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) { |
| 196 | struct usb_descriptor_header *usb_desc; |
| 197 | |
| 198 | usb_desc = usb_otg_descriptor_alloc(cdev->gadget); |
| 199 | if (!usb_desc) |
| 200 | goto fail_string_ids; |
| 201 | usb_otg_descriptor_init(cdev->gadget, usb_desc); |
| 202 | otg_desc[0] = usb_desc; |
| 203 | otg_desc[1] = NULL; |
| 204 | } |
| 205 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 206 | status = usb_add_config(cdev, &msg_config_driver, msg_do_config); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 207 | if (status < 0) |
Li Jun | d867889 | 2015-07-09 15:18:55 +0800 | [diff] [blame] | 208 | goto fail_otg_desc; |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 209 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 210 | usb_composite_overwrite_options(cdev, &coverwrite); |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 211 | dev_info(&cdev->gadget->dev, |
| 212 | DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 213 | set_bit(0, &msg_registered); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 214 | return 0; |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 215 | |
Li Jun | d867889 | 2015-07-09 15:18:55 +0800 | [diff] [blame] | 216 | fail_otg_desc: |
| 217 | kfree(otg_desc[0]); |
| 218 | otg_desc[0] = NULL; |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 219 | fail_string_ids: |
| 220 | fsg_common_remove_luns(opts->common); |
| 221 | fail_set_cdev: |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 222 | fsg_common_free_buffers(opts->common); |
| 223 | fail: |
| 224 | usb_put_function_instance(fi_msg); |
| 225 | return status; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 228 | static int msg_unbind(struct usb_composite_dev *cdev) |
| 229 | { |
| 230 | if (!IS_ERR(f_msg)) |
| 231 | usb_put_function(f_msg); |
| 232 | |
| 233 | if (!IS_ERR(fi_msg)) |
| 234 | usb_put_function_instance(fi_msg); |
| 235 | |
Li Jun | d867889 | 2015-07-09 15:18:55 +0800 | [diff] [blame] | 236 | kfree(otg_desc[0]); |
| 237 | otg_desc[0] = NULL; |
| 238 | |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 239 | return 0; |
| 240 | } |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 241 | |
| 242 | /****************************** Some noise ******************************/ |
| 243 | |
Arnd Bergmann | c94e289f | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 244 | static struct usb_composite_driver msg_driver = { |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 245 | .name = "g_mass_storage", |
| 246 | .dev = &msg_device_desc, |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 247 | .max_speed = USB_SPEED_SUPER, |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 248 | .needs_serial = 1, |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 249 | .strings = dev_strings, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 250 | .bind = msg_bind, |
Andrzej Pietrasiewicz | 2412fbf | 2013-10-09 10:06:02 +0200 | [diff] [blame] | 251 | .unbind = msg_unbind, |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 255 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 256 | MODULE_LICENSE("GPL"); |
| 257 | |
| 258 | static int __init msg_init(void) |
| 259 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 260 | return usb_composite_probe(&msg_driver); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 261 | } |
| 262 | module_init(msg_init); |
| 263 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 264 | static void msg_cleanup(void) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 265 | { |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 266 | if (test_and_clear_bit(0, &msg_registered)) |
| 267 | usb_composite_unregister(&msg_driver); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 268 | } |
| 269 | module_exit(msg_cleanup); |