Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * multi.c -- Multifunction Composite driver |
| 3 | * |
| 4 | * Copyright (C) 2008 David Brownell |
| 5 | * Copyright (C) 2008 Nokia Corporation |
| 6 | * Copyright (C) 2009 Samsung Electronics |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 7 | * Author: Michal Nazarewicz (mina86@mina86.com) |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +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 | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | |
| 16 | #include <linux/kernel.h> |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 18 | #include <linux/netdevice.h> |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 19 | |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 20 | #include "u_serial.h" |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 21 | #if defined USB_ETH_RNDIS |
| 22 | # undef USB_ETH_RNDIS |
| 23 | #endif |
Michal Nazarewicz | dbe4a99 | 2010-01-22 15:18:21 +0100 | [diff] [blame] | 24 | #ifdef CONFIG_USB_G_MULTI_RNDIS |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 25 | # define USB_ETH_RNDIS y |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | |
| 29 | #define DRIVER_DESC "Multifunction Composite Gadget" |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 30 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 31 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 32 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 33 | MODULE_LICENSE("GPL"); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 34 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 35 | |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 36 | #include "f_mass_storage.h" |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 37 | |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 38 | #include "u_ecm.h" |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 39 | #ifdef USB_ETH_RNDIS |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 40 | # include "u_rndis.h" |
Andrzej Pietrasiewicz | cbbd14a | 2013-05-24 10:23:02 +0200 | [diff] [blame] | 41 | # include "rndis.h" |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 42 | #endif |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 43 | #include "u_ether.h" |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 44 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 45 | USB_GADGET_COMPOSITE_OPTIONS(); |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 46 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 47 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 48 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 49 | /***************************** Device Descriptor ****************************/ |
| 50 | |
Michal Nazarewicz | 1c6529e | 2010-08-12 17:43:44 +0200 | [diff] [blame] | 51 | #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */ |
| 52 | #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */ |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 53 | |
| 54 | |
| 55 | enum { |
| 56 | __MULTI_NO_CONFIG, |
| 57 | #ifdef CONFIG_USB_G_MULTI_RNDIS |
| 58 | MULTI_RNDIS_CONFIG_NUM, |
| 59 | #endif |
| 60 | #ifdef CONFIG_USB_G_MULTI_CDC |
| 61 | MULTI_CDC_CONFIG_NUM, |
| 62 | #endif |
| 63 | }; |
| 64 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 65 | |
| 66 | static struct usb_device_descriptor device_desc = { |
| 67 | .bLength = sizeof device_desc, |
| 68 | .bDescriptorType = USB_DT_DEVICE, |
| 69 | |
| 70 | .bcdUSB = cpu_to_le16(0x0200), |
| 71 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 72 | .bDeviceClass = USB_CLASS_MISC /* 0xEF */, |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 73 | .bDeviceSubClass = 2, |
| 74 | .bDeviceProtocol = 1, |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 75 | |
| 76 | /* Vendor and product id can be overridden by module parameters. */ |
| 77 | .idVendor = cpu_to_le16(MULTI_VENDOR_NUM), |
| 78 | .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM), |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Li Jun | 21ddc2a | 2015-07-09 15:18:56 +0800 | [diff] [blame] | 81 | static const struct usb_descriptor_header *otg_desc[2]; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 82 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 83 | enum { |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 84 | MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX, |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 85 | MULTI_STRING_CDC_CONFIG_IDX, |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 86 | }; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 87 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 88 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 89 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 90 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 91 | [USB_GADGET_SERIAL_IDX].s = "", |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 92 | [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS", |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 93 | [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM", |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 94 | { } /* end of list */ |
| 95 | }; |
| 96 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 97 | static struct usb_gadget_strings *dev_strings[] = { |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 98 | &(struct usb_gadget_strings){ |
| 99 | .language = 0x0409, /* en-us */ |
| 100 | .strings = strings_dev, |
| 101 | }, |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 102 | NULL, |
| 103 | }; |
| 104 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 105 | |
| 106 | |
| 107 | |
| 108 | /****************************** Configurations ******************************/ |
| 109 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 110 | static struct fsg_module_parameters fsg_mod_data = { .stall = 1 }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 111 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 112 | |
| 113 | static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; |
| 114 | |
| 115 | #else |
| 116 | |
| 117 | /* |
| 118 | * Number of buffers we will use. |
| 119 | * 2 is usually enough for good buffering pipeline |
| 120 | */ |
| 121 | #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS |
| 122 | |
Greg Kroah-Hartman | 0c8b199 | 2013-12-19 15:43:10 -0800 | [diff] [blame] | 123 | #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 124 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 125 | FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 126 | |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 127 | static struct usb_function_instance *fi_acm; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 128 | static struct usb_function_instance *fi_msg; |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 129 | |
| 130 | /********** RNDIS **********/ |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 131 | |
Michal Nazarewicz | 396cda9 | 2009-11-30 10:55:40 +0100 | [diff] [blame] | 132 | #ifdef USB_ETH_RNDIS |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 133 | static struct usb_function_instance *fi_rndis; |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 134 | static struct usb_function *f_acm_rndis; |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 135 | static struct usb_function *f_rndis; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 136 | static struct usb_function *f_msg_rndis; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 137 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 138 | static int rndis_do_config(struct usb_configuration *c) |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 139 | { |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 140 | struct fsg_opts *fsg_opts; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 141 | int ret; |
| 142 | |
| 143 | if (gadget_is_otg(c->cdev->gadget)) { |
| 144 | c->descriptors = otg_desc; |
| 145 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 146 | } |
| 147 | |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 148 | f_rndis = usb_get_function(fi_rndis); |
| 149 | if (IS_ERR(f_rndis)) |
| 150 | return PTR_ERR(f_rndis); |
| 151 | |
| 152 | ret = usb_add_function(c, f_rndis); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 153 | if (ret < 0) |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 154 | goto err_func_rndis; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 155 | |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 156 | f_acm_rndis = usb_get_function(fi_acm); |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 157 | if (IS_ERR(f_acm_rndis)) { |
| 158 | ret = PTR_ERR(f_acm_rndis); |
| 159 | goto err_func_acm; |
| 160 | } |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 161 | |
| 162 | ret = usb_add_function(c, f_acm_rndis); |
| 163 | if (ret) |
| 164 | goto err_conf; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 165 | |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 166 | f_msg_rndis = usb_get_function(fi_msg); |
| 167 | if (IS_ERR(f_msg_rndis)) { |
| 168 | ret = PTR_ERR(f_msg_rndis); |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 169 | goto err_fsg; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | fsg_opts = fsg_opts_from_func_inst(fi_msg); |
| 173 | ret = fsg_common_run_thread(fsg_opts->common); |
| 174 | if (ret) |
| 175 | goto err_run; |
| 176 | |
| 177 | ret = usb_add_function(c, f_msg_rndis); |
| 178 | if (ret) |
| 179 | goto err_run; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 180 | |
| 181 | return 0; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 182 | err_run: |
| 183 | usb_put_function(f_msg_rndis); |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 184 | err_fsg: |
| 185 | usb_remove_function(c, f_acm_rndis); |
| 186 | err_conf: |
| 187 | usb_put_function(f_acm_rndis); |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 188 | err_func_acm: |
| 189 | usb_remove_function(c, f_rndis); |
| 190 | err_func_rndis: |
| 191 | usb_put_function(f_rndis); |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 192 | return ret; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Chen Gang | 780cc0f | 2013-09-02 18:14:42 +0800 | [diff] [blame] | 195 | static __ref int rndis_config_register(struct usb_composite_dev *cdev) |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 196 | { |
| 197 | static struct usb_configuration config = { |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 198 | .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM, |
| 199 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 200 | }; |
| 201 | |
| 202 | config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s; |
| 203 | config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id; |
| 204 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 205 | return usb_add_config(cdev, &config, rndis_do_config); |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | #else |
| 209 | |
Chen Gang | 780cc0f | 2013-09-02 18:14:42 +0800 | [diff] [blame] | 210 | static __ref int rndis_config_register(struct usb_composite_dev *cdev) |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 211 | { |
| 212 | return 0; |
| 213 | } |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 214 | |
| 215 | #endif |
| 216 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 217 | |
| 218 | /********** CDC ECM **********/ |
| 219 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 220 | #ifdef CONFIG_USB_G_MULTI_CDC |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 221 | static struct usb_function_instance *fi_ecm; |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 222 | static struct usb_function *f_acm_multi; |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 223 | static struct usb_function *f_ecm; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 224 | static struct usb_function *f_msg_multi; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 225 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 226 | static int cdc_do_config(struct usb_configuration *c) |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 227 | { |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 228 | struct fsg_opts *fsg_opts; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 229 | int ret; |
| 230 | |
| 231 | if (gadget_is_otg(c->cdev->gadget)) { |
| 232 | c->descriptors = otg_desc; |
| 233 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 234 | } |
| 235 | |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 236 | f_ecm = usb_get_function(fi_ecm); |
| 237 | if (IS_ERR(f_ecm)) |
| 238 | return PTR_ERR(f_ecm); |
| 239 | |
| 240 | ret = usb_add_function(c, f_ecm); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 241 | if (ret < 0) |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 242 | goto err_func_ecm; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 243 | |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 244 | /* implicit port_num is zero */ |
| 245 | f_acm_multi = usb_get_function(fi_acm); |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 246 | if (IS_ERR(f_acm_multi)) { |
| 247 | ret = PTR_ERR(f_acm_multi); |
| 248 | goto err_func_acm; |
| 249 | } |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 250 | |
| 251 | ret = usb_add_function(c, f_acm_multi); |
| 252 | if (ret) |
| 253 | goto err_conf; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 254 | |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 255 | f_msg_multi = usb_get_function(fi_msg); |
| 256 | if (IS_ERR(f_msg_multi)) { |
| 257 | ret = PTR_ERR(f_msg_multi); |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 258 | goto err_fsg; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | fsg_opts = fsg_opts_from_func_inst(fi_msg); |
| 262 | ret = fsg_common_run_thread(fsg_opts->common); |
| 263 | if (ret) |
| 264 | goto err_run; |
| 265 | |
| 266 | ret = usb_add_function(c, f_msg_multi); |
| 267 | if (ret) |
| 268 | goto err_run; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 269 | |
| 270 | return 0; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 271 | err_run: |
| 272 | usb_put_function(f_msg_multi); |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 273 | err_fsg: |
| 274 | usb_remove_function(c, f_acm_multi); |
| 275 | err_conf: |
| 276 | usb_put_function(f_acm_multi); |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 277 | err_func_acm: |
| 278 | usb_remove_function(c, f_ecm); |
| 279 | err_func_ecm: |
| 280 | usb_put_function(f_ecm); |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 281 | return ret; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Chen Gang | 780cc0f | 2013-09-02 18:14:42 +0800 | [diff] [blame] | 284 | static __ref int cdc_config_register(struct usb_composite_dev *cdev) |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 285 | { |
| 286 | static struct usb_configuration config = { |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 287 | .bConfigurationValue = MULTI_CDC_CONFIG_NUM, |
| 288 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 289 | }; |
| 290 | |
| 291 | config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s; |
| 292 | config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id; |
| 293 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 294 | return usb_add_config(cdev, &config, cdc_do_config); |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | #else |
| 298 | |
Chen Gang | 780cc0f | 2013-09-02 18:14:42 +0800 | [diff] [blame] | 299 | static __ref int cdc_config_register(struct usb_composite_dev *cdev) |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 300 | { |
| 301 | return 0; |
| 302 | } |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 303 | |
| 304 | #endif |
| 305 | |
| 306 | |
| 307 | |
| 308 | /****************************** Gadget Bind ******************************/ |
| 309 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 310 | static int __ref multi_bind(struct usb_composite_dev *cdev) |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 311 | { |
| 312 | struct usb_gadget *gadget = cdev->gadget; |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 313 | #ifdef CONFIG_USB_G_MULTI_CDC |
| 314 | struct f_ecm_opts *ecm_opts; |
| 315 | #endif |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 316 | #ifdef USB_ETH_RNDIS |
| 317 | struct f_rndis_opts *rndis_opts; |
| 318 | #endif |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 319 | struct fsg_opts *fsg_opts; |
| 320 | struct fsg_config config; |
Sebastian Andrzej Siewior | ed9cbda | 2012-09-10 09:16:07 +0200 | [diff] [blame] | 321 | int status; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 322 | |
| 323 | if (!can_support_ecm(cdev->gadget)) { |
| 324 | dev_err(&gadget->dev, "controller '%s' not usable\n", |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 325 | gadget->name); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | } |
| 328 | |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 329 | #ifdef CONFIG_USB_G_MULTI_CDC |
| 330 | fi_ecm = usb_get_function_instance("ecm"); |
| 331 | if (IS_ERR(fi_ecm)) |
| 332 | return PTR_ERR(fi_ecm); |
| 333 | |
| 334 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
| 335 | |
| 336 | gether_set_qmult(ecm_opts->net, qmult); |
| 337 | if (!gether_set_host_addr(ecm_opts->net, host_addr)) |
| 338 | pr_info("using host ethernet address: %s", host_addr); |
| 339 | if (!gether_set_dev_addr(ecm_opts->net, dev_addr)) |
| 340 | pr_info("using self ethernet address: %s", dev_addr); |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 341 | #endif |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 342 | |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 343 | #ifdef USB_ETH_RNDIS |
| 344 | fi_rndis = usb_get_function_instance("rndis"); |
| 345 | if (IS_ERR(fi_rndis)) { |
| 346 | status = PTR_ERR(fi_rndis); |
| 347 | goto fail; |
| 348 | } |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 349 | |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 350 | rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst); |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 351 | |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 352 | gether_set_qmult(rndis_opts->net, qmult); |
| 353 | if (!gether_set_host_addr(rndis_opts->net, host_addr)) |
| 354 | pr_info("using host ethernet address: %s", host_addr); |
| 355 | if (!gether_set_dev_addr(rndis_opts->net, dev_addr)) |
| 356 | pr_info("using self ethernet address: %s", dev_addr); |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 357 | #endif |
| 358 | |
| 359 | #if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS) |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 360 | /* |
| 361 | * If both ecm and rndis are selected then: |
| 362 | * 1) rndis borrows the net interface from ecm |
| 363 | * 2) since the interface is shared it must not be bound |
| 364 | * twice - in ecm's _and_ rndis' binds, so do it here. |
| 365 | */ |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 366 | gether_set_gadget(ecm_opts->net, cdev->gadget); |
| 367 | status = gether_register_netdev(ecm_opts->net); |
| 368 | if (status) |
| 369 | goto fail0; |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 370 | |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 371 | rndis_borrow_net(fi_rndis, ecm_opts->net); |
| 372 | ecm_opts->bound = true; |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 373 | #endif |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 374 | |
| 375 | /* set up serial link layer */ |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 376 | fi_acm = usb_get_function_instance("acm"); |
| 377 | if (IS_ERR(fi_acm)) { |
| 378 | status = PTR_ERR(fi_acm); |
Sebastian Andrzej Siewior | c4ed4ac | 2012-12-23 21:10:18 +0100 | [diff] [blame] | 379 | goto fail0; |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 382 | /* set up mass storage function */ |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 383 | fi_msg = usb_get_function_instance("mass_storage"); |
| 384 | if (IS_ERR(fi_msg)) { |
| 385 | status = PTR_ERR(fi_msg); |
| 386 | goto fail1; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 387 | } |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 388 | fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers); |
| 389 | fsg_opts = fsg_opts_from_func_inst(fi_msg); |
| 390 | |
| 391 | fsg_opts->no_configfs = true; |
| 392 | status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers); |
| 393 | if (status) |
| 394 | goto fail2; |
| 395 | |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 396 | status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall); |
| 397 | if (status) |
| 398 | goto fail_set_cdev; |
| 399 | |
| 400 | fsg_common_set_sysfs(fsg_opts->common, true); |
| 401 | status = fsg_common_create_luns(fsg_opts->common, &config); |
| 402 | if (status) |
| 403 | goto fail_set_cdev; |
| 404 | |
| 405 | fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name, |
| 406 | config.product_name); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 407 | |
Michal Nazarewicz | a99d8a4 | 2010-08-12 17:43:49 +0200 | [diff] [blame] | 408 | /* allocate string IDs */ |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 409 | status = usb_string_ids_tab(cdev, strings_dev); |
| 410 | if (unlikely(status < 0)) |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 411 | goto fail_string_ids; |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 412 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 413 | |
Li Jun | 21ddc2a | 2015-07-09 15:18:56 +0800 | [diff] [blame] | 414 | if (gadget_is_otg(gadget) && !otg_desc[0]) { |
| 415 | struct usb_descriptor_header *usb_desc; |
| 416 | |
| 417 | usb_desc = usb_otg_descriptor_alloc(gadget); |
| 418 | if (!usb_desc) |
| 419 | goto fail_string_ids; |
| 420 | usb_otg_descriptor_init(gadget, usb_desc); |
| 421 | otg_desc[0] = usb_desc; |
| 422 | otg_desc[1] = NULL; |
| 423 | } |
| 424 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 425 | /* register configurations */ |
| 426 | status = rndis_config_register(cdev); |
| 427 | if (unlikely(status < 0)) |
Li Jun | 21ddc2a | 2015-07-09 15:18:56 +0800 | [diff] [blame] | 428 | goto fail_otg_desc; |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 429 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 430 | status = cdc_config_register(cdev); |
| 431 | if (unlikely(status < 0)) |
Li Jun | 21ddc2a | 2015-07-09 15:18:56 +0800 | [diff] [blame] | 432 | goto fail_otg_desc; |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 433 | usb_composite_overwrite_options(cdev, &coverwrite); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 434 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 435 | /* we're done */ |
| 436 | dev_info(&gadget->dev, DRIVER_DESC "\n"); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 437 | return 0; |
| 438 | |
Michal Nazarewicz | 279cc49 | 2010-06-21 13:57:03 +0200 | [diff] [blame] | 439 | |
| 440 | /* error recovery */ |
Li Jun | 21ddc2a | 2015-07-09 15:18:56 +0800 | [diff] [blame] | 441 | fail_otg_desc: |
| 442 | kfree(otg_desc[0]); |
| 443 | otg_desc[0] = NULL; |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 444 | fail_string_ids: |
| 445 | fsg_common_remove_luns(fsg_opts->common); |
| 446 | fail_set_cdev: |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 447 | fsg_common_free_buffers(fsg_opts->common); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 448 | fail2: |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 449 | usb_put_function_instance(fi_msg); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 450 | fail1: |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 451 | usb_put_function_instance(fi_acm); |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 452 | fail0: |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 453 | #ifdef USB_ETH_RNDIS |
| 454 | usb_put_function_instance(fi_rndis); |
| 455 | fail: |
| 456 | #endif |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 457 | #ifdef CONFIG_USB_G_MULTI_CDC |
| 458 | usb_put_function_instance(fi_ecm); |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 459 | #endif |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 460 | return status; |
| 461 | } |
| 462 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 463 | static int multi_unbind(struct usb_composite_dev *cdev) |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 464 | { |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 465 | #ifdef CONFIG_USB_G_MULTI_CDC |
Andrzej Pietrasiewicz | 1bcce93 | 2013-10-09 10:08:27 +0200 | [diff] [blame] | 466 | usb_put_function(f_msg_multi); |
| 467 | #endif |
| 468 | #ifdef USB_ETH_RNDIS |
| 469 | usb_put_function(f_msg_rndis); |
| 470 | #endif |
| 471 | usb_put_function_instance(fi_msg); |
| 472 | #ifdef CONFIG_USB_G_MULTI_CDC |
Sebastian Andrzej Siewior | 59835ad | 2012-12-23 21:10:10 +0100 | [diff] [blame] | 473 | usb_put_function(f_acm_multi); |
| 474 | #endif |
| 475 | #ifdef USB_ETH_RNDIS |
| 476 | usb_put_function(f_acm_rndis); |
| 477 | #endif |
| 478 | usb_put_function_instance(fi_acm); |
Andrzej Pietrasiewicz | 4d1a8f6 | 2013-10-09 10:08:26 +0200 | [diff] [blame] | 479 | #ifdef USB_ETH_RNDIS |
| 480 | usb_put_function(f_rndis); |
| 481 | usb_put_function_instance(fi_rndis); |
| 482 | #endif |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 483 | #ifdef CONFIG_USB_G_MULTI_CDC |
| 484 | usb_put_function(f_ecm); |
| 485 | usb_put_function_instance(fi_ecm); |
Andrzej Pietrasiewicz | 7388901 | 2013-10-09 10:08:25 +0200 | [diff] [blame] | 486 | #endif |
Li Jun | 21ddc2a | 2015-07-09 15:18:56 +0800 | [diff] [blame] | 487 | kfree(otg_desc[0]); |
| 488 | otg_desc[0] = NULL; |
| 489 | |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | /****************************** Some noise ******************************/ |
| 495 | |
| 496 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 497 | static struct usb_composite_driver multi_driver = { |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 498 | .name = "g_multi", |
| 499 | .dev = &device_desc, |
| 500 | .strings = dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 501 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 502 | .bind = multi_bind, |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 503 | .unbind = multi_unbind, |
Michal Nazarewicz | a99d8a4 | 2010-08-12 17:43:49 +0200 | [diff] [blame] | 504 | .needs_serial = 1, |
Michal Nazarewicz | f176a5d | 2009-11-09 14:15:27 +0100 | [diff] [blame] | 505 | }; |
| 506 | |
Tobias Klauser | 909346a | 2014-07-09 18:09:56 +0200 | [diff] [blame] | 507 | module_usb_composite_driver(multi_driver); |