Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * g_ffs.c -- user mode file system API for USB composite function controllers |
| 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 5 | * Author: Michal Nazarewicz <mina86@mina86.com> |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 13 | #define pr_fmt(fmt) "g_ffs: " fmt |
| 14 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 15 | #include <linux/module.h> |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 16 | /* |
| 17 | * kbuild is not very cooperative with respect to linking separately |
| 18 | * compiled library objects into one module. So for now we won't use |
| 19 | * separate compilation ... ensuring init/exit sections work to shrink |
| 20 | * the runtime footprint, and giving us at least some parts of what |
| 21 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 22 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 23 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS |
| 24 | # if defined USB_ETH_RNDIS |
| 25 | # undef USB_ETH_RNDIS |
| 26 | # endif |
| 27 | # ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 28 | # define USB_ETH_RNDIS y |
| 29 | # endif |
| 30 | |
| 31 | # include "f_ecm.c" |
| 32 | # include "f_subset.c" |
| 33 | # ifdef USB_ETH_RNDIS |
| 34 | # include "f_rndis.c" |
Andrzej Pietrasiewicz | cbbd14a | 2013-05-24 10:23:02 +0200 | [diff] [blame] | 35 | # include "rndis.h" |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 36 | # endif |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 37 | # include "u_ether.h" |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 38 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 39 | static u8 gfs_host_mac[ETH_ALEN]; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 40 | static struct eth_dev *the_dev; |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 41 | # ifdef CONFIG_USB_FUNCTIONFS_ETH |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 42 | static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 43 | struct eth_dev *dev); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 44 | # endif |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 45 | #else |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 46 | # define the_dev NULL |
| 47 | # define gether_cleanup(dev) do { } while (0) |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 48 | # define gfs_host_mac NULL |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 49 | struct eth_dev; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | #include "f_fs.c" |
| 53 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 54 | #define DRIVER_NAME "g_ffs" |
| 55 | #define DRIVER_DESC "USB Function Filesystem" |
| 56 | #define DRIVER_VERSION "24 Aug 2004" |
| 57 | |
| 58 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 59 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 62 | #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */ |
| 63 | #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 64 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 65 | #define GFS_MAX_DEVS 10 |
| 66 | |
| 67 | struct gfs_ffs_obj { |
| 68 | const char *name; |
| 69 | bool mounted; |
| 70 | bool desc_ready; |
| 71 | struct ffs_data *ffs_data; |
| 72 | }; |
| 73 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 74 | USB_GADGET_COMPOSITE_OPTIONS(); |
| 75 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 76 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 77 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 78 | static struct usb_device_descriptor gfs_dev_desc = { |
| 79 | .bLength = sizeof gfs_dev_desc, |
| 80 | .bDescriptorType = USB_DT_DEVICE, |
| 81 | |
| 82 | .bcdUSB = cpu_to_le16(0x0200), |
| 83 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 84 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 85 | .idVendor = cpu_to_le16(GFS_VENDOR_ID), |
| 86 | .idProduct = cpu_to_le16(GFS_PRODUCT_ID), |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 89 | static char *func_names[GFS_MAX_DEVS]; |
| 90 | static unsigned int func_num; |
| 91 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 92 | module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644); |
| 93 | MODULE_PARM_DESC(bDeviceClass, "USB Device class"); |
| 94 | module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644); |
| 95 | MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass"); |
| 96 | module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644); |
| 97 | MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol"); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 98 | module_param_array_named(functions, func_names, charp, &func_num, 0); |
| 99 | MODULE_PARM_DESC(functions, "USB Functions list"); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 100 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 101 | static const struct usb_descriptor_header *gfs_otg_desc[] = { |
| 102 | (const struct usb_descriptor_header *) |
| 103 | &(const struct usb_otg_descriptor) { |
| 104 | .bLength = sizeof(struct usb_otg_descriptor), |
| 105 | .bDescriptorType = USB_DT_OTG, |
| 106 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 107 | /* |
| 108 | * REVISIT SRP-only hardware is possible, although |
| 109 | * it would not be called "OTG" ... |
| 110 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 111 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 112 | }, |
| 113 | |
| 114 | NULL |
| 115 | }; |
| 116 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 117 | /* String IDs are assigned dynamically */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 118 | static struct usb_string gfs_strings[] = { |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 119 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 120 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 121 | [USB_GADGET_SERIAL_IDX].s = "", |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 122 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 123 | { .s = "FunctionFS + RNDIS" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 124 | #endif |
| 125 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 126 | { .s = "FunctionFS + ECM" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 127 | #endif |
| 128 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 129 | { .s = "FunctionFS" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 130 | #endif |
| 131 | { } /* end of list */ |
| 132 | }; |
| 133 | |
| 134 | static struct usb_gadget_strings *gfs_dev_strings[] = { |
| 135 | &(struct usb_gadget_strings) { |
| 136 | .language = 0x0409, /* en-us */ |
| 137 | .strings = gfs_strings, |
| 138 | }, |
| 139 | NULL, |
| 140 | }; |
| 141 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 142 | struct gfs_configuration { |
| 143 | struct usb_configuration c; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 144 | int (*eth)(struct usb_configuration *c, u8 *ethaddr, |
| 145 | struct eth_dev *dev); |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 146 | } gfs_configurations[] = { |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 147 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 148 | { |
| 149 | .eth = rndis_bind_config, |
| 150 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 151 | #endif |
| 152 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 153 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 154 | { |
| 155 | .eth = eth_bind_config, |
| 156 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 157 | #endif |
| 158 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 159 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 160 | { |
| 161 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 162 | #endif |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 163 | }; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 164 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 165 | static int gfs_bind(struct usb_composite_dev *cdev); |
| 166 | static int gfs_unbind(struct usb_composite_dev *cdev); |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 167 | static int gfs_do_config(struct usb_configuration *c); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 168 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 169 | static __refdata struct usb_composite_driver gfs_driver = { |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 170 | .name = DRIVER_NAME, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 171 | .dev = &gfs_dev_desc, |
| 172 | .strings = gfs_dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 173 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 174 | .bind = gfs_bind, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 175 | .unbind = gfs_unbind, |
| 176 | }; |
| 177 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 178 | static DEFINE_MUTEX(gfs_lock); |
| 179 | static unsigned int missing_funcs; |
| 180 | static bool gfs_ether_setup; |
| 181 | static bool gfs_registered; |
| 182 | static bool gfs_single_func; |
| 183 | static struct gfs_ffs_obj *ffs_tab; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 184 | |
Andrzej Pietrasiewicz | 8545e60 | 2012-03-12 12:55:42 +0100 | [diff] [blame] | 185 | static int __init gfs_init(void) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 186 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 187 | int i; |
| 188 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 189 | ENTER(); |
| 190 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 191 | if (!func_num) { |
| 192 | gfs_single_func = true; |
| 193 | func_num = 1; |
| 194 | } |
| 195 | |
| 196 | ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL); |
| 197 | if (!ffs_tab) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | if (!gfs_single_func) |
| 201 | for (i = 0; i < func_num; i++) |
| 202 | ffs_tab[i].name = func_names[i]; |
| 203 | |
| 204 | missing_funcs = func_num; |
| 205 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 206 | return functionfs_init(); |
| 207 | } |
| 208 | module_init(gfs_init); |
| 209 | |
Andrzej Pietrasiewicz | 8545e60 | 2012-03-12 12:55:42 +0100 | [diff] [blame] | 210 | static void __exit gfs_exit(void) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 211 | { |
| 212 | ENTER(); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 213 | mutex_lock(&gfs_lock); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 214 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 215 | if (gfs_registered) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 216 | usb_composite_unregister(&gfs_driver); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 217 | gfs_registered = false; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 218 | |
| 219 | functionfs_cleanup(); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 220 | |
| 221 | mutex_unlock(&gfs_lock); |
| 222 | kfree(ffs_tab); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 223 | } |
| 224 | module_exit(gfs_exit); |
| 225 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 226 | static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 227 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 228 | int i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 229 | |
| 230 | ENTER(); |
| 231 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 232 | if (gfs_single_func) |
| 233 | return &ffs_tab[0]; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 234 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 235 | for (i = 0; i < func_num; i++) |
| 236 | if (strcmp(ffs_tab[i].name, dev_name) == 0) |
| 237 | return &ffs_tab[i]; |
| 238 | |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | static int functionfs_ready_callback(struct ffs_data *ffs) |
| 243 | { |
| 244 | struct gfs_ffs_obj *ffs_obj; |
| 245 | int ret; |
| 246 | |
| 247 | ENTER(); |
| 248 | mutex_lock(&gfs_lock); |
| 249 | |
| 250 | ffs_obj = ffs->private_data; |
| 251 | if (!ffs_obj) { |
| 252 | ret = -EINVAL; |
| 253 | goto done; |
| 254 | } |
| 255 | |
| 256 | if (WARN_ON(ffs_obj->desc_ready)) { |
| 257 | ret = -EBUSY; |
| 258 | goto done; |
| 259 | } |
| 260 | ffs_obj->desc_ready = true; |
| 261 | ffs_obj->ffs_data = ffs; |
| 262 | |
| 263 | if (--missing_funcs) { |
| 264 | ret = 0; |
| 265 | goto done; |
| 266 | } |
| 267 | |
| 268 | if (gfs_registered) { |
| 269 | ret = -EBUSY; |
| 270 | goto done; |
| 271 | } |
| 272 | gfs_registered = true; |
| 273 | |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 274 | ret = usb_composite_probe(&gfs_driver); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 275 | if (unlikely(ret < 0)) |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 276 | gfs_registered = false; |
| 277 | |
| 278 | done: |
| 279 | mutex_unlock(&gfs_lock); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static void functionfs_closed_callback(struct ffs_data *ffs) |
| 284 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 285 | struct gfs_ffs_obj *ffs_obj; |
| 286 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 287 | ENTER(); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 288 | mutex_lock(&gfs_lock); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 289 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 290 | ffs_obj = ffs->private_data; |
| 291 | if (!ffs_obj) |
| 292 | goto done; |
| 293 | |
| 294 | ffs_obj->desc_ready = false; |
| 295 | missing_funcs++; |
| 296 | |
| 297 | if (gfs_registered) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 298 | usb_composite_unregister(&gfs_driver); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 299 | gfs_registered = false; |
| 300 | |
| 301 | done: |
| 302 | mutex_unlock(&gfs_lock); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 305 | static void *functionfs_acquire_dev_callback(const char *dev_name) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 306 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 307 | struct gfs_ffs_obj *ffs_dev; |
| 308 | |
| 309 | ENTER(); |
| 310 | mutex_lock(&gfs_lock); |
| 311 | |
| 312 | ffs_dev = gfs_find_dev(dev_name); |
| 313 | if (!ffs_dev) { |
| 314 | ffs_dev = ERR_PTR(-ENODEV); |
| 315 | goto done; |
| 316 | } |
| 317 | |
| 318 | if (ffs_dev->mounted) { |
| 319 | ffs_dev = ERR_PTR(-EBUSY); |
| 320 | goto done; |
| 321 | } |
| 322 | ffs_dev->mounted = true; |
| 323 | |
| 324 | done: |
| 325 | mutex_unlock(&gfs_lock); |
| 326 | return ffs_dev; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 329 | static void functionfs_release_dev_callback(struct ffs_data *ffs_data) |
| 330 | { |
| 331 | struct gfs_ffs_obj *ffs_dev; |
| 332 | |
| 333 | ENTER(); |
| 334 | mutex_lock(&gfs_lock); |
| 335 | |
| 336 | ffs_dev = ffs_data->private_data; |
| 337 | if (ffs_dev) |
| 338 | ffs_dev->mounted = false; |
| 339 | |
| 340 | mutex_unlock(&gfs_lock); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * It is assumed that gfs_bind is called from a context where gfs_lock is held |
| 345 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 346 | static int gfs_bind(struct usb_composite_dev *cdev) |
| 347 | { |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 348 | int ret, i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 349 | |
| 350 | ENTER(); |
| 351 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 352 | if (missing_funcs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 353 | return -ENODEV; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 354 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 355 | the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, gfs_host_mac, |
| 356 | qmult); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 357 | #endif |
| 358 | if (IS_ERR(the_dev)) { |
| 359 | ret = PTR_ERR(the_dev); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 360 | goto error_quick; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 361 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 362 | gfs_ether_setup = true; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 363 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 364 | ret = usb_string_ids_tab(cdev, gfs_strings); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 365 | if (unlikely(ret < 0)) |
| 366 | goto error; |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 367 | gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 368 | |
Andrzej Pietrasiewicz | 3416905 | 2013-03-11 16:32:14 +0100 | [diff] [blame] | 369 | for (i = func_num; i--; ) { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 370 | ret = functionfs_bind(ffs_tab[i].ffs_data, cdev); |
| 371 | if (unlikely(ret < 0)) { |
| 372 | while (++i < func_num) |
| 373 | functionfs_unbind(ffs_tab[i].ffs_data); |
| 374 | goto error; |
| 375 | } |
| 376 | } |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 377 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 378 | for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) { |
| 379 | struct gfs_configuration *c = gfs_configurations + i; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 380 | int sid = USB_GADGET_FIRST_AVAIL_IDX + i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 381 | |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 382 | c->c.label = gfs_strings[sid].s; |
| 383 | c->c.iConfiguration = gfs_strings[sid].id; |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 384 | c->c.bConfigurationValue = 1 + i; |
| 385 | c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 386 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 387 | ret = usb_add_config(cdev, &c->c, gfs_do_config); |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 388 | if (unlikely(ret < 0)) |
| 389 | goto error_unbind; |
| 390 | } |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 391 | usb_composite_overwrite_options(cdev, &coverwrite); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 392 | return 0; |
| 393 | |
| 394 | error_unbind: |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 395 | for (i = 0; i < func_num; i++) |
| 396 | functionfs_unbind(ffs_tab[i].ffs_data); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 397 | error: |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 398 | gether_cleanup(the_dev); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 399 | error_quick: |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 400 | gfs_ether_setup = false; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 404 | /* |
| 405 | * It is assumed that gfs_unbind is called from a context where gfs_lock is held |
| 406 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 407 | static int gfs_unbind(struct usb_composite_dev *cdev) |
| 408 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 409 | int i; |
| 410 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 411 | ENTER(); |
| 412 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 413 | /* |
| 414 | * We may have been called in an error recovery from |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 415 | * composite_bind() after gfs_unbind() failure so we need to |
| 416 | * check if gfs_ffs_data is not NULL since gfs_bind() handles |
| 417 | * all error recovery itself. I'd rather we werent called |
| 418 | * from composite on orror recovery, but what you're gonna |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 419 | * do...? |
| 420 | */ |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 421 | if (gfs_ether_setup) |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 422 | gether_cleanup(the_dev); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 423 | gfs_ether_setup = false; |
| 424 | |
Andrzej Pietrasiewicz | 3416905 | 2013-03-11 16:32:14 +0100 | [diff] [blame] | 425 | for (i = func_num; i--; ) |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 426 | if (ffs_tab[i].ffs_data) |
| 427 | functionfs_unbind(ffs_tab[i].ffs_data); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 432 | /* |
| 433 | * It is assumed that gfs_do_config is called from a context where |
| 434 | * gfs_lock is held |
| 435 | */ |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 436 | static int gfs_do_config(struct usb_configuration *c) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 437 | { |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 438 | struct gfs_configuration *gc = |
| 439 | container_of(c, struct gfs_configuration, c); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 440 | int i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 441 | int ret; |
| 442 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 443 | if (missing_funcs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 444 | return -ENODEV; |
| 445 | |
| 446 | if (gadget_is_otg(c->cdev->gadget)) { |
| 447 | c->descriptors = gfs_otg_desc; |
| 448 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 449 | } |
| 450 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 451 | if (gc->eth) { |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 452 | ret = gc->eth(c, gfs_host_mac, the_dev); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 453 | if (unlikely(ret < 0)) |
| 454 | return ret; |
| 455 | } |
| 456 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 457 | for (i = 0; i < func_num; i++) { |
| 458 | ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data); |
| 459 | if (unlikely(ret < 0)) |
| 460 | return ret; |
| 461 | } |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 462 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 463 | /* |
| 464 | * After previous do_configs there may be some invalid |
Michal Nazarewicz | f588c0d | 2010-06-14 10:43:34 +0200 | [diff] [blame] | 465 | * pointers in c->interface array. This happens every time |
| 466 | * a user space function with fewer interfaces than a user |
| 467 | * space function that was run before the new one is run. The |
| 468 | * compasit's set_config() assumes that if there is no more |
| 469 | * then MAX_CONFIG_INTERFACES interfaces in a configuration |
| 470 | * then there is a NULL pointer after the last interface in |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 471 | * c->interface array. We need to make sure this is true. |
| 472 | */ |
Michal Nazarewicz | f588c0d | 2010-06-14 10:43:34 +0200 | [diff] [blame] | 473 | if (c->next_interface_id < ARRAY_SIZE(c->interface)) |
| 474 | c->interface[c->next_interface_id] = NULL; |
| 475 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 479 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 480 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 481 | static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 482 | struct eth_dev *dev) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 483 | { |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 484 | return can_support_ecm(c->cdev->gadget) |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 485 | ? ecm_bind_config(c, ethaddr, dev) |
| 486 | : geth_bind_config(c, ethaddr, dev); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 487 | } |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 488 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 489 | #endif |