Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * acm_ms.c -- Composite driver, with ACM and mass storage support |
| 3 | * |
| 4 | * Copyright (C) 2008 David Brownell |
| 5 | * Copyright (C) 2008 Nokia Corporation |
| 6 | * Author: David Brownell |
| 7 | * Modified: Klaus Schwarzkopf <schwarzkopf@sensortherm.de> |
| 8 | * |
| 9 | * Heavily based on multi.c and cdc2.c |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 19 | |
| 20 | #include "u_serial.h" |
| 21 | |
| 22 | #define DRIVER_DESC "Composite Gadget (ACM + MS)" |
| 23 | #define DRIVER_VERSION "2011/10/10" |
| 24 | |
| 25 | /*-------------------------------------------------------------------------*/ |
| 26 | |
| 27 | /* |
| 28 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 29 | * Instead: allocate your own, using normal USB-IF procedures. |
| 30 | */ |
| 31 | #define ACM_MS_VENDOR_NUM 0x1d6b /* Linux Foundation */ |
| 32 | #define ACM_MS_PRODUCT_NUM 0x0106 /* Composite Gadget: ACM + MS*/ |
| 33 | |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 34 | #include "f_mass_storage.h" |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 35 | |
| 36 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 37 | USB_GADGET_COMPOSITE_OPTIONS(); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 38 | |
| 39 | static struct usb_device_descriptor device_desc = { |
| 40 | .bLength = sizeof device_desc, |
| 41 | .bDescriptorType = USB_DT_DEVICE, |
| 42 | |
| 43 | .bcdUSB = cpu_to_le16(0x0200), |
| 44 | |
| 45 | .bDeviceClass = USB_CLASS_MISC /* 0xEF */, |
| 46 | .bDeviceSubClass = 2, |
| 47 | .bDeviceProtocol = 1, |
| 48 | |
| 49 | /* .bMaxPacketSize0 = f(hardware) */ |
| 50 | |
| 51 | /* Vendor and product id can be overridden by module parameters. */ |
| 52 | .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM), |
| 53 | .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM), |
| 54 | /* .bcdDevice = f(hardware) */ |
| 55 | /* .iManufacturer = DYNAMIC */ |
| 56 | /* .iProduct = DYNAMIC */ |
| 57 | /* NO SERIAL NUMBER */ |
| 58 | /*.bNumConfigurations = DYNAMIC*/ |
| 59 | }; |
| 60 | |
| 61 | static struct usb_otg_descriptor otg_descriptor = { |
| 62 | .bLength = sizeof otg_descriptor, |
| 63 | .bDescriptorType = USB_DT_OTG, |
| 64 | |
| 65 | /* |
| 66 | * REVISIT SRP-only hardware is possible, although |
| 67 | * it would not be called "OTG" ... |
| 68 | */ |
| 69 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 70 | }; |
| 71 | |
| 72 | static const struct usb_descriptor_header *otg_desc[] = { |
| 73 | (struct usb_descriptor_header *) &otg_descriptor, |
| 74 | NULL, |
| 75 | }; |
| 76 | |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 77 | /* string IDs are assigned dynamically */ |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 78 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 79 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 80 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
| 81 | [USB_GADGET_SERIAL_IDX].s = "", |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 82 | { } /* end of list */ |
| 83 | }; |
| 84 | |
| 85 | static struct usb_gadget_strings stringtab_dev = { |
| 86 | .language = 0x0409, /* en-us */ |
| 87 | .strings = strings_dev, |
| 88 | }; |
| 89 | |
| 90 | static struct usb_gadget_strings *dev_strings[] = { |
| 91 | &stringtab_dev, |
| 92 | NULL, |
| 93 | }; |
| 94 | |
| 95 | /****************************** Configurations ******************************/ |
| 96 | |
| 97 | static struct fsg_module_parameters fsg_mod_data = { .stall = 1 }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 99 | |
| 100 | static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; |
| 101 | |
| 102 | #else |
| 103 | |
| 104 | /* |
| 105 | * Number of buffers we will use. |
| 106 | * 2 is usually enough for good buffering pipeline |
| 107 | */ |
| 108 | #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS |
| 109 | |
Greg Kroah-Hartman | 0c8b199 | 2013-12-19 15:43:10 -0800 | [diff] [blame] | 110 | #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 111 | |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 112 | FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data); |
| 113 | |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 114 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 5f72bbf | 2012-12-23 21:10:08 +0100 | [diff] [blame] | 115 | static struct usb_function *f_acm; |
| 116 | static struct usb_function_instance *f_acm_inst; |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 117 | |
| 118 | static struct usb_function_instance *fi_msg; |
| 119 | static struct usb_function *f_msg; |
| 120 | |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 121 | /* |
| 122 | * We _always_ have both ACM and mass storage functions. |
| 123 | */ |
| 124 | static int __init acm_ms_do_config(struct usb_configuration *c) |
| 125 | { |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 126 | struct fsg_opts *opts; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 127 | int status; |
| 128 | |
| 129 | if (gadget_is_otg(c->cdev->gadget)) { |
| 130 | c->descriptors = otg_desc; |
| 131 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 132 | } |
| 133 | |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 134 | opts = fsg_opts_from_func_inst(fi_msg); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 135 | |
Sebastian Andrzej Siewior | 5f72bbf | 2012-12-23 21:10:08 +0100 | [diff] [blame] | 136 | f_acm = usb_get_function(f_acm_inst); |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 137 | if (IS_ERR(f_acm)) |
| 138 | return PTR_ERR(f_acm); |
| 139 | |
| 140 | f_msg = usb_get_function(fi_msg); |
| 141 | if (IS_ERR(f_msg)) { |
| 142 | status = PTR_ERR(f_msg); |
| 143 | goto put_acm; |
Sebastian Andrzej Siewior | 5f72bbf | 2012-12-23 21:10:08 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | status = usb_add_function(c, f_acm); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 147 | if (status < 0) |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 148 | goto put_msg; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 149 | |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 150 | status = fsg_common_run_thread(opts->common); |
| 151 | if (status) |
| 152 | goto remove_acm; |
| 153 | |
| 154 | status = usb_add_function(c, f_msg); |
| 155 | if (status) |
| 156 | goto remove_acm; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 157 | |
| 158 | return 0; |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 159 | remove_acm: |
Sebastian Andrzej Siewior | 5f72bbf | 2012-12-23 21:10:08 +0100 | [diff] [blame] | 160 | usb_remove_function(c, f_acm); |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 161 | put_msg: |
| 162 | usb_put_function(f_msg); |
| 163 | put_acm: |
Sebastian Andrzej Siewior | 5f72bbf | 2012-12-23 21:10:08 +0100 | [diff] [blame] | 164 | usb_put_function(f_acm); |
Sebastian Andrzej Siewior | 5f72bbf | 2012-12-23 21:10:08 +0100 | [diff] [blame] | 165 | return status; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static struct usb_configuration acm_ms_config_driver = { |
| 169 | .label = DRIVER_DESC, |
| 170 | .bConfigurationValue = 1, |
| 171 | /* .iConfiguration = DYNAMIC */ |
| 172 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 173 | }; |
| 174 | |
| 175 | /*-------------------------------------------------------------------------*/ |
| 176 | |
| 177 | static int __init acm_ms_bind(struct usb_composite_dev *cdev) |
| 178 | { |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 179 | struct usb_gadget *gadget = cdev->gadget; |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 180 | struct fsg_opts *opts; |
| 181 | struct fsg_config config; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 182 | int status; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 183 | |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 184 | f_acm_inst = usb_get_function_instance("acm"); |
| 185 | if (IS_ERR(f_acm_inst)) |
| 186 | return PTR_ERR(f_acm_inst); |
| 187 | |
| 188 | fi_msg = usb_get_function_instance("mass_storage"); |
| 189 | if (IS_ERR(fi_msg)) { |
| 190 | status = PTR_ERR(fi_msg); |
| 191 | goto fail_get_msg; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 194 | /* set up mass storage function */ |
| 195 | fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers); |
| 196 | opts = fsg_opts_from_func_inst(fi_msg); |
| 197 | |
| 198 | opts->no_configfs = true; |
| 199 | status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers); |
| 200 | if (status) |
| 201 | goto fail; |
| 202 | |
| 203 | status = fsg_common_set_nluns(opts->common, config.nluns); |
| 204 | if (status) |
| 205 | goto fail_set_nluns; |
| 206 | |
| 207 | status = fsg_common_set_cdev(opts->common, cdev, config.can_stall); |
| 208 | if (status) |
| 209 | goto fail_set_cdev; |
| 210 | |
| 211 | fsg_common_set_sysfs(opts->common, true); |
| 212 | status = fsg_common_create_luns(opts->common, &config); |
| 213 | if (status) |
| 214 | goto fail_set_cdev; |
| 215 | |
| 216 | fsg_common_set_inquiry_string(opts->common, config.vendor_name, |
| 217 | config.product_name); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 218 | /* |
| 219 | * Allocate string descriptor numbers ... note that string |
| 220 | * contents can be overridden by the composite_dev glue. |
| 221 | */ |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 222 | status = usb_string_ids_tab(cdev, strings_dev); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 223 | if (status < 0) |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 224 | goto fail_string_ids; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 225 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 226 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 227 | |
| 228 | /* register our configuration */ |
| 229 | status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config); |
| 230 | if (status < 0) |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 231 | goto fail_string_ids; |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 232 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 233 | usb_composite_overwrite_options(cdev, &coverwrite); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 234 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
| 235 | DRIVER_DESC); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 236 | return 0; |
| 237 | |
| 238 | /* error recovery */ |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 239 | fail_string_ids: |
| 240 | fsg_common_remove_luns(opts->common); |
| 241 | fail_set_cdev: |
| 242 | fsg_common_free_luns(opts->common); |
| 243 | fail_set_nluns: |
| 244 | fsg_common_free_buffers(opts->common); |
| 245 | fail: |
| 246 | usb_put_function_instance(fi_msg); |
| 247 | fail_get_msg: |
| 248 | usb_put_function_instance(f_acm_inst); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 249 | return status; |
| 250 | } |
| 251 | |
| 252 | static int __exit acm_ms_unbind(struct usb_composite_dev *cdev) |
| 253 | { |
Andrzej Pietrasiewicz | e6c661e | 2013-10-09 10:07:29 +0200 | [diff] [blame] | 254 | usb_put_function(f_msg); |
| 255 | usb_put_function_instance(fi_msg); |
Sebastian Andrzej Siewior | 5f72bbf | 2012-12-23 21:10:08 +0100 | [diff] [blame] | 256 | usb_put_function(f_acm); |
| 257 | usb_put_function_instance(f_acm_inst); |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 261 | static __refdata struct usb_composite_driver acm_ms_driver = { |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 262 | .name = "g_acm_ms", |
| 263 | .dev = &device_desc, |
Steve Bennett | 6f47209 | 2012-06-22 14:16:19 +1000 | [diff] [blame] | 264 | .max_speed = USB_SPEED_SUPER, |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 265 | .strings = dev_strings, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 266 | .bind = acm_ms_bind, |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 267 | .unbind = __exit_p(acm_ms_unbind), |
| 268 | }; |
| 269 | |
Tobias Klauser | 909346a | 2014-07-09 18:09:56 +0200 | [diff] [blame] | 270 | module_usb_composite_driver(acm_ms_driver); |
| 271 | |
Klaus Schwarzkopf | fa3ae0c | 2011-10-10 10:32:23 +0200 | [diff] [blame] | 272 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 273 | MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>"); |
| 274 | MODULE_LICENSE("GPL v2"); |