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