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