rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <linux/configfs.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/slab.h> |
| 4 | #include <linux/device.h> |
| 5 | #include <linux/nls.h> |
| 6 | #include <linux/usb/composite.h> |
| 7 | #include <linux/usb/gadget_configfs.h> |
| 8 | #include "configfs.h" |
| 9 | #include "u_f.h" |
| 10 | #include "u_os_desc.h" |
| 11 | |
| 12 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/kdev_t.h> |
| 15 | #include <linux/usb/ch9.h> |
| 16 | |
| 17 | #ifdef CONFIG_USB_CONFIGFS_F_ACC |
| 18 | extern int acc_ctrlrequest(struct usb_composite_dev *cdev, |
| 19 | const struct usb_ctrlrequest *ctrl); |
| 20 | void acc_disconnect(void); |
| 21 | #endif |
| 22 | static struct class *android_class; |
| 23 | static struct device *android_device; |
| 24 | static int index; |
| 25 | |
| 26 | struct device *create_function_device(char *name) |
| 27 | { |
| 28 | if (android_device && !IS_ERR(android_device)) |
| 29 | return device_create(android_class, android_device, |
| 30 | MKDEV(0, index++), NULL, name); |
| 31 | else |
| 32 | return ERR_PTR(-EINVAL); |
| 33 | } |
| 34 | EXPORT_SYMBOL_GPL(create_function_device); |
| 35 | #endif |
| 36 | |
| 37 | int check_user_usb_string(const char *name, |
| 38 | struct usb_gadget_strings *stringtab_dev) |
| 39 | { |
| 40 | unsigned primary_lang; |
| 41 | unsigned sub_lang; |
| 42 | u16 num; |
| 43 | int ret; |
| 44 | |
| 45 | ret = kstrtou16(name, 0, &num); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | primary_lang = num & 0x3ff; |
| 50 | sub_lang = num >> 10; |
| 51 | |
| 52 | /* simple sanity check for valid langid */ |
| 53 | switch (primary_lang) { |
| 54 | case 0: |
| 55 | case 0x62 ... 0xfe: |
| 56 | case 0x100 ... 0x3ff: |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | if (!sub_lang) |
| 60 | return -EINVAL; |
| 61 | |
| 62 | stringtab_dev->language = num; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | #define MAX_NAME_LEN 40 |
| 67 | #define MAX_USB_STRING_LANGS 2 |
| 68 | |
| 69 | static const struct usb_descriptor_header *otg_desc[2]; |
| 70 | |
| 71 | struct gadget_info { |
| 72 | struct config_group group; |
| 73 | struct config_group functions_group; |
| 74 | struct config_group configs_group; |
| 75 | struct config_group strings_group; |
| 76 | struct config_group os_desc_group; |
| 77 | |
| 78 | struct mutex lock; |
| 79 | struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; |
| 80 | struct list_head string_list; |
| 81 | struct list_head available_func; |
| 82 | |
| 83 | struct usb_composite_driver composite; |
| 84 | struct usb_composite_dev cdev; |
| 85 | bool use_os_desc; |
| 86 | char b_vendor_code; |
| 87 | char qw_sign[OS_STRING_QW_SIGN_LEN]; |
| 88 | spinlock_t spinlock; |
| 89 | bool unbind; |
| 90 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 91 | bool connected; |
| 92 | bool sw_connected; |
| 93 | struct work_struct work; |
| 94 | struct device *dev; |
| 95 | #endif |
| 96 | }; |
| 97 | |
| 98 | static inline struct gadget_info *to_gadget_info(struct config_item *item) |
| 99 | { |
| 100 | return container_of(to_config_group(item), struct gadget_info, group); |
| 101 | } |
| 102 | |
| 103 | struct config_usb_cfg { |
| 104 | struct config_group group; |
| 105 | struct config_group strings_group; |
| 106 | struct list_head string_list; |
| 107 | struct usb_configuration c; |
| 108 | struct list_head func_list; |
| 109 | struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; |
| 110 | }; |
| 111 | |
| 112 | static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) |
| 113 | { |
| 114 | return container_of(to_config_group(item), struct config_usb_cfg, |
| 115 | group); |
| 116 | } |
| 117 | |
| 118 | struct gadget_strings { |
| 119 | struct usb_gadget_strings stringtab_dev; |
| 120 | struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX]; |
| 121 | char *manufacturer; |
| 122 | char *product; |
| 123 | char *serialnumber; |
| 124 | |
| 125 | struct config_group group; |
| 126 | struct list_head list; |
| 127 | }; |
| 128 | |
| 129 | struct os_desc { |
| 130 | struct config_group group; |
| 131 | }; |
| 132 | |
| 133 | struct gadget_config_name { |
| 134 | struct usb_gadget_strings stringtab_dev; |
| 135 | struct usb_string strings; |
| 136 | char *configuration; |
| 137 | |
| 138 | struct config_group group; |
| 139 | struct list_head list; |
| 140 | }; |
| 141 | |
| 142 | static int usb_string_copy(const char *s, char **s_copy) |
| 143 | { |
| 144 | int ret; |
| 145 | char *str; |
| 146 | char *copy = *s_copy; |
| 147 | ret = strlen(s); |
| 148 | if (ret > 126) |
| 149 | return -EOVERFLOW; |
| 150 | |
| 151 | str = kstrdup(s, GFP_KERNEL); |
| 152 | if (!str) |
| 153 | return -ENOMEM; |
| 154 | if (str[ret - 1] == '\n') |
| 155 | str[ret - 1] = '\0'; |
| 156 | kfree(copy); |
| 157 | *s_copy = str; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \ |
| 162 | static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ |
| 163 | char *page) \ |
| 164 | { \ |
| 165 | return sprintf(page, "0x%02x\n", \ |
| 166 | to_gadget_info(item)->cdev.desc.__name); \ |
| 167 | } |
| 168 | |
| 169 | #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \ |
| 170 | static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ |
| 171 | char *page) \ |
| 172 | { \ |
| 173 | return sprintf(page, "0x%04x\n", \ |
| 174 | le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \ |
| 175 | } |
| 176 | |
| 177 | |
| 178 | #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \ |
| 179 | static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ |
| 180 | const char *page, size_t len) \ |
| 181 | { \ |
| 182 | u8 val; \ |
| 183 | int ret; \ |
| 184 | ret = kstrtou8(page, 0, &val); \ |
| 185 | if (ret) \ |
| 186 | return ret; \ |
| 187 | to_gadget_info(item)->cdev.desc._name = val; \ |
| 188 | return len; \ |
| 189 | } |
| 190 | |
| 191 | #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \ |
| 192 | static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ |
| 193 | const char *page, size_t len) \ |
| 194 | { \ |
| 195 | u16 val; \ |
| 196 | int ret; \ |
| 197 | ret = kstrtou16(page, 0, &val); \ |
| 198 | if (ret) \ |
| 199 | return ret; \ |
| 200 | to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \ |
| 201 | return len; \ |
| 202 | } |
| 203 | |
| 204 | #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \ |
| 205 | GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \ |
| 206 | GI_DEVICE_DESC_SIMPLE_W_##_type(_name) |
| 207 | |
| 208 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB); |
| 209 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8); |
| 210 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8); |
| 211 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8); |
| 212 | GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8); |
| 213 | GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16); |
| 214 | GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16); |
| 215 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice); |
| 216 | |
| 217 | static ssize_t is_valid_bcd(u16 bcd_val) |
| 218 | { |
| 219 | if ((bcd_val & 0xf) > 9) |
| 220 | return -EINVAL; |
| 221 | if (((bcd_val >> 4) & 0xf) > 9) |
| 222 | return -EINVAL; |
| 223 | if (((bcd_val >> 8) & 0xf) > 9) |
| 224 | return -EINVAL; |
| 225 | if (((bcd_val >> 12) & 0xf) > 9) |
| 226 | return -EINVAL; |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item, |
| 231 | const char *page, size_t len) |
| 232 | { |
| 233 | u16 bcdDevice; |
| 234 | int ret; |
| 235 | |
| 236 | ret = kstrtou16(page, 0, &bcdDevice); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | ret = is_valid_bcd(bcdDevice); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | |
| 243 | to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 244 | return len; |
| 245 | } |
| 246 | |
| 247 | static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item, |
| 248 | const char *page, size_t len) |
| 249 | { |
| 250 | u16 bcdUSB; |
| 251 | int ret; |
| 252 | |
| 253 | ret = kstrtou16(page, 0, &bcdUSB); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | ret = is_valid_bcd(bcdUSB); |
| 257 | if (ret) |
| 258 | return ret; |
| 259 | |
| 260 | to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB); |
| 261 | return len; |
| 262 | } |
| 263 | |
| 264 | static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page) |
| 265 | { |
| 266 | char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name; |
| 267 | |
| 268 | return sprintf(page, "%s\n", udc_name ?: ""); |
| 269 | } |
| 270 | |
| 271 | static int unregister_gadget(struct gadget_info *gi) |
| 272 | { |
| 273 | int ret; |
| 274 | |
| 275 | if (!gi->composite.gadget_driver.udc_name) |
| 276 | return -ENODEV; |
| 277 | |
| 278 | ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver); |
| 279 | if (ret) |
| 280 | return ret; |
| 281 | kfree(gi->composite.gadget_driver.udc_name); |
| 282 | gi->composite.gadget_driver.udc_name = NULL; |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static ssize_t gadget_dev_desc_UDC_store(struct config_item *item, |
| 287 | const char *page, size_t len) |
| 288 | { |
| 289 | struct gadget_info *gi = to_gadget_info(item); |
| 290 | char *name; |
| 291 | int ret; |
| 292 | |
| 293 | if (strlen(page) < len) |
| 294 | return -EOVERFLOW; |
| 295 | |
| 296 | name = kstrdup(page, GFP_KERNEL); |
| 297 | if (!name) |
| 298 | return -ENOMEM; |
| 299 | if (name[len - 1] == '\n') |
| 300 | name[len - 1] = '\0'; |
| 301 | |
| 302 | mutex_lock(&gi->lock); |
| 303 | |
| 304 | if (!strlen(name) || strcmp(name, "none") == 0) { |
| 305 | ret = unregister_gadget(gi); |
| 306 | if (ret) |
| 307 | goto err; |
| 308 | kfree(name); |
| 309 | } else { |
| 310 | if (gi->composite.gadget_driver.udc_name) { |
| 311 | ret = -EBUSY; |
| 312 | goto err; |
| 313 | } |
| 314 | gi->composite.gadget_driver.udc_name = name; |
| 315 | ret = usb_gadget_probe_driver(&gi->composite.gadget_driver); |
| 316 | if (ret) { |
| 317 | gi->composite.gadget_driver.udc_name = NULL; |
| 318 | goto err; |
| 319 | } |
| 320 | } |
| 321 | mutex_unlock(&gi->lock); |
| 322 | return len; |
| 323 | err: |
| 324 | kfree(name); |
| 325 | mutex_unlock(&gi->lock); |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass); |
| 330 | CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass); |
| 331 | CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol); |
| 332 | CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0); |
| 333 | CONFIGFS_ATTR(gadget_dev_desc_, idVendor); |
| 334 | CONFIGFS_ATTR(gadget_dev_desc_, idProduct); |
| 335 | CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice); |
| 336 | CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB); |
| 337 | CONFIGFS_ATTR(gadget_dev_desc_, UDC); |
| 338 | |
| 339 | static struct configfs_attribute *gadget_root_attrs[] = { |
| 340 | &gadget_dev_desc_attr_bDeviceClass, |
| 341 | &gadget_dev_desc_attr_bDeviceSubClass, |
| 342 | &gadget_dev_desc_attr_bDeviceProtocol, |
| 343 | &gadget_dev_desc_attr_bMaxPacketSize0, |
| 344 | &gadget_dev_desc_attr_idVendor, |
| 345 | &gadget_dev_desc_attr_idProduct, |
| 346 | &gadget_dev_desc_attr_bcdDevice, |
| 347 | &gadget_dev_desc_attr_bcdUSB, |
| 348 | &gadget_dev_desc_attr_UDC, |
| 349 | NULL, |
| 350 | }; |
| 351 | |
| 352 | static inline struct gadget_strings *to_gadget_strings(struct config_item *item) |
| 353 | { |
| 354 | return container_of(to_config_group(item), struct gadget_strings, |
| 355 | group); |
| 356 | } |
| 357 | |
| 358 | static inline struct gadget_config_name *to_gadget_config_name( |
| 359 | struct config_item *item) |
| 360 | { |
| 361 | return container_of(to_config_group(item), struct gadget_config_name, |
| 362 | group); |
| 363 | } |
| 364 | |
| 365 | static inline struct usb_function_instance *to_usb_function_instance( |
| 366 | struct config_item *item) |
| 367 | { |
| 368 | return container_of(to_config_group(item), |
| 369 | struct usb_function_instance, group); |
| 370 | } |
| 371 | |
| 372 | static void gadget_info_attr_release(struct config_item *item) |
| 373 | { |
| 374 | struct gadget_info *gi = to_gadget_info(item); |
| 375 | |
| 376 | WARN_ON(!list_empty(&gi->cdev.configs)); |
| 377 | WARN_ON(!list_empty(&gi->string_list)); |
| 378 | WARN_ON(!list_empty(&gi->available_func)); |
| 379 | kfree(gi->composite.gadget_driver.function); |
| 380 | kfree(gi); |
| 381 | } |
| 382 | |
| 383 | static struct configfs_item_operations gadget_root_item_ops = { |
| 384 | .release = gadget_info_attr_release, |
| 385 | }; |
| 386 | |
| 387 | static void gadget_config_attr_release(struct config_item *item) |
| 388 | { |
| 389 | struct config_usb_cfg *cfg = to_config_usb_cfg(item); |
| 390 | |
| 391 | WARN_ON(!list_empty(&cfg->c.functions)); |
| 392 | list_del(&cfg->c.list); |
| 393 | kfree(cfg->c.label); |
| 394 | kfree(cfg); |
| 395 | } |
| 396 | |
| 397 | static int config_usb_cfg_link( |
| 398 | struct config_item *usb_cfg_ci, |
| 399 | struct config_item *usb_func_ci) |
| 400 | { |
| 401 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); |
| 402 | struct usb_composite_dev *cdev = cfg->c.cdev; |
| 403 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); |
| 404 | |
| 405 | struct config_group *group = to_config_group(usb_func_ci); |
| 406 | struct usb_function_instance *fi = container_of(group, |
| 407 | struct usb_function_instance, group); |
| 408 | struct usb_function_instance *a_fi; |
| 409 | struct usb_function *f; |
| 410 | int ret; |
| 411 | |
| 412 | mutex_lock(&gi->lock); |
| 413 | /* |
| 414 | * Make sure this function is from within our _this_ gadget and not |
| 415 | * from another gadget or a random directory. |
| 416 | * Also a function instance can only be linked once. |
| 417 | */ |
| 418 | list_for_each_entry(a_fi, &gi->available_func, cfs_list) { |
| 419 | if (a_fi == fi) |
| 420 | break; |
| 421 | } |
| 422 | if (a_fi != fi) { |
| 423 | ret = -EINVAL; |
| 424 | goto out; |
| 425 | } |
| 426 | |
| 427 | list_for_each_entry(f, &cfg->func_list, list) { |
| 428 | if (f->fi == fi) { |
| 429 | ret = -EEXIST; |
| 430 | goto out; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | f = usb_get_function(fi); |
| 435 | if (IS_ERR(f)) { |
| 436 | ret = PTR_ERR(f); |
| 437 | goto out; |
| 438 | } |
| 439 | |
| 440 | /* stash the function until we bind it to the gadget */ |
| 441 | list_add_tail(&f->list, &cfg->func_list); |
| 442 | ret = 0; |
| 443 | out: |
| 444 | mutex_unlock(&gi->lock); |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | static void config_usb_cfg_unlink( |
| 449 | struct config_item *usb_cfg_ci, |
| 450 | struct config_item *usb_func_ci) |
| 451 | { |
| 452 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); |
| 453 | struct usb_composite_dev *cdev = cfg->c.cdev; |
| 454 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); |
| 455 | |
| 456 | struct config_group *group = to_config_group(usb_func_ci); |
| 457 | struct usb_function_instance *fi = container_of(group, |
| 458 | struct usb_function_instance, group); |
| 459 | struct usb_function *f; |
| 460 | |
| 461 | /* |
| 462 | * ideally I would like to forbid to unlink functions while a gadget is |
| 463 | * bound to an UDC. Since this isn't possible at the moment, we simply |
| 464 | * force an unbind, the function is available here and then we can |
| 465 | * remove the function. |
| 466 | */ |
| 467 | mutex_lock(&gi->lock); |
| 468 | if (gi->composite.gadget_driver.udc_name) |
| 469 | unregister_gadget(gi); |
| 470 | WARN_ON(gi->composite.gadget_driver.udc_name); |
| 471 | |
| 472 | list_for_each_entry(f, &cfg->func_list, list) { |
| 473 | if (f->fi == fi) { |
| 474 | list_del(&f->list); |
| 475 | usb_put_function(f); |
| 476 | mutex_unlock(&gi->lock); |
| 477 | return; |
| 478 | } |
| 479 | } |
| 480 | mutex_unlock(&gi->lock); |
| 481 | WARN(1, "Unable to locate function to unbind\n"); |
| 482 | } |
| 483 | |
| 484 | static struct configfs_item_operations gadget_config_item_ops = { |
| 485 | .release = gadget_config_attr_release, |
| 486 | .allow_link = config_usb_cfg_link, |
| 487 | .drop_link = config_usb_cfg_unlink, |
| 488 | }; |
| 489 | |
| 490 | |
| 491 | static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item, |
| 492 | char *page) |
| 493 | { |
| 494 | return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower); |
| 495 | } |
| 496 | |
| 497 | static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item, |
| 498 | const char *page, size_t len) |
| 499 | { |
| 500 | u16 val; |
| 501 | int ret; |
| 502 | ret = kstrtou16(page, 0, &val); |
| 503 | if (ret) |
| 504 | return ret; |
| 505 | if (DIV_ROUND_UP(val, 8) > 0xff) |
| 506 | return -ERANGE; |
| 507 | to_config_usb_cfg(item)->c.MaxPower = val; |
| 508 | return len; |
| 509 | } |
| 510 | |
| 511 | static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item, |
| 512 | char *page) |
| 513 | { |
| 514 | return sprintf(page, "0x%02x\n", |
| 515 | to_config_usb_cfg(item)->c.bmAttributes); |
| 516 | } |
| 517 | |
| 518 | static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item, |
| 519 | const char *page, size_t len) |
| 520 | { |
| 521 | u8 val; |
| 522 | int ret; |
| 523 | ret = kstrtou8(page, 0, &val); |
| 524 | if (ret) |
| 525 | return ret; |
| 526 | if (!(val & USB_CONFIG_ATT_ONE)) |
| 527 | return -EINVAL; |
| 528 | if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | |
| 529 | USB_CONFIG_ATT_WAKEUP)) |
| 530 | return -EINVAL; |
| 531 | to_config_usb_cfg(item)->c.bmAttributes = val; |
| 532 | return len; |
| 533 | } |
| 534 | |
| 535 | CONFIGFS_ATTR(gadget_config_desc_, MaxPower); |
| 536 | CONFIGFS_ATTR(gadget_config_desc_, bmAttributes); |
| 537 | |
| 538 | static struct configfs_attribute *gadget_config_attrs[] = { |
| 539 | &gadget_config_desc_attr_MaxPower, |
| 540 | &gadget_config_desc_attr_bmAttributes, |
| 541 | NULL, |
| 542 | }; |
| 543 | |
| 544 | static struct config_item_type gadget_config_type = { |
| 545 | .ct_item_ops = &gadget_config_item_ops, |
| 546 | .ct_attrs = gadget_config_attrs, |
| 547 | .ct_owner = THIS_MODULE, |
| 548 | }; |
| 549 | |
| 550 | static struct config_item_type gadget_root_type = { |
| 551 | .ct_item_ops = &gadget_root_item_ops, |
| 552 | .ct_attrs = gadget_root_attrs, |
| 553 | .ct_owner = THIS_MODULE, |
| 554 | }; |
| 555 | |
| 556 | static void composite_init_dev(struct usb_composite_dev *cdev) |
| 557 | { |
| 558 | spin_lock_init(&cdev->lock); |
| 559 | INIT_LIST_HEAD(&cdev->configs); |
| 560 | INIT_LIST_HEAD(&cdev->gstrings); |
| 561 | } |
| 562 | |
| 563 | static struct config_group *function_make( |
| 564 | struct config_group *group, |
| 565 | const char *name) |
| 566 | { |
| 567 | struct gadget_info *gi; |
| 568 | struct usb_function_instance *fi; |
| 569 | char buf[MAX_NAME_LEN]; |
| 570 | char *func_name; |
| 571 | char *instance_name; |
| 572 | int ret; |
| 573 | |
| 574 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); |
| 575 | if (ret >= MAX_NAME_LEN) |
| 576 | return ERR_PTR(-ENAMETOOLONG); |
| 577 | |
| 578 | func_name = buf; |
| 579 | instance_name = strchr(func_name, '.'); |
| 580 | if (!instance_name) { |
| 581 | pr_err("Unable to locate . in FUNC.INSTANCE\n"); |
| 582 | return ERR_PTR(-EINVAL); |
| 583 | } |
| 584 | *instance_name = '\0'; |
| 585 | instance_name++; |
| 586 | |
| 587 | fi = usb_get_function_instance(func_name); |
| 588 | if (IS_ERR(fi)) |
| 589 | return ERR_CAST(fi); |
| 590 | |
| 591 | ret = config_item_set_name(&fi->group.cg_item, "%s", name); |
| 592 | if (ret) { |
| 593 | usb_put_function_instance(fi); |
| 594 | return ERR_PTR(ret); |
| 595 | } |
| 596 | if (fi->set_inst_name) { |
| 597 | ret = fi->set_inst_name(fi, instance_name); |
| 598 | if (ret) { |
| 599 | usb_put_function_instance(fi); |
| 600 | return ERR_PTR(ret); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | gi = container_of(group, struct gadget_info, functions_group); |
| 605 | |
| 606 | mutex_lock(&gi->lock); |
| 607 | list_add_tail(&fi->cfs_list, &gi->available_func); |
| 608 | mutex_unlock(&gi->lock); |
| 609 | return &fi->group; |
| 610 | } |
| 611 | |
| 612 | static void function_drop( |
| 613 | struct config_group *group, |
| 614 | struct config_item *item) |
| 615 | { |
| 616 | struct usb_function_instance *fi = to_usb_function_instance(item); |
| 617 | struct gadget_info *gi; |
| 618 | |
| 619 | gi = container_of(group, struct gadget_info, functions_group); |
| 620 | |
| 621 | mutex_lock(&gi->lock); |
| 622 | list_del(&fi->cfs_list); |
| 623 | mutex_unlock(&gi->lock); |
| 624 | config_item_put(item); |
| 625 | } |
| 626 | |
| 627 | static struct configfs_group_operations functions_ops = { |
| 628 | .make_group = &function_make, |
| 629 | .drop_item = &function_drop, |
| 630 | }; |
| 631 | |
| 632 | static struct config_item_type functions_type = { |
| 633 | .ct_group_ops = &functions_ops, |
| 634 | .ct_owner = THIS_MODULE, |
| 635 | }; |
| 636 | |
| 637 | GS_STRINGS_RW(gadget_config_name, configuration); |
| 638 | |
| 639 | static struct configfs_attribute *gadget_config_name_langid_attrs[] = { |
| 640 | &gadget_config_name_attr_configuration, |
| 641 | NULL, |
| 642 | }; |
| 643 | |
| 644 | static void gadget_config_name_attr_release(struct config_item *item) |
| 645 | { |
| 646 | struct gadget_config_name *cn = to_gadget_config_name(item); |
| 647 | |
| 648 | kfree(cn->configuration); |
| 649 | |
| 650 | list_del(&cn->list); |
| 651 | kfree(cn); |
| 652 | } |
| 653 | |
| 654 | USB_CONFIG_STRING_RW_OPS(gadget_config_name); |
| 655 | USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg); |
| 656 | |
| 657 | static struct config_group *config_desc_make( |
| 658 | struct config_group *group, |
| 659 | const char *name) |
| 660 | { |
| 661 | struct gadget_info *gi; |
| 662 | struct config_usb_cfg *cfg; |
| 663 | char buf[MAX_NAME_LEN]; |
| 664 | char *num_str; |
| 665 | u8 num; |
| 666 | int ret; |
| 667 | |
| 668 | gi = container_of(group, struct gadget_info, configs_group); |
| 669 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); |
| 670 | if (ret >= MAX_NAME_LEN) |
| 671 | return ERR_PTR(-ENAMETOOLONG); |
| 672 | |
| 673 | num_str = strchr(buf, '.'); |
| 674 | if (!num_str) { |
| 675 | pr_err("Unable to locate . in name.bConfigurationValue\n"); |
| 676 | return ERR_PTR(-EINVAL); |
| 677 | } |
| 678 | |
| 679 | *num_str = '\0'; |
| 680 | num_str++; |
| 681 | |
| 682 | if (!strlen(buf)) |
| 683 | return ERR_PTR(-EINVAL); |
| 684 | |
| 685 | ret = kstrtou8(num_str, 0, &num); |
| 686 | if (ret) |
| 687 | return ERR_PTR(ret); |
| 688 | |
| 689 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); |
| 690 | if (!cfg) |
| 691 | return ERR_PTR(-ENOMEM); |
| 692 | cfg->c.label = kstrdup(buf, GFP_KERNEL); |
| 693 | if (!cfg->c.label) { |
| 694 | ret = -ENOMEM; |
| 695 | goto err; |
| 696 | } |
| 697 | cfg->c.bConfigurationValue = num; |
| 698 | cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW; |
| 699 | cfg->c.bmAttributes = USB_CONFIG_ATT_ONE; |
| 700 | INIT_LIST_HEAD(&cfg->string_list); |
| 701 | INIT_LIST_HEAD(&cfg->func_list); |
| 702 | |
| 703 | config_group_init_type_name(&cfg->group, name, |
| 704 | &gadget_config_type); |
| 705 | |
| 706 | config_group_init_type_name(&cfg->strings_group, "strings", |
| 707 | &gadget_config_name_strings_type); |
| 708 | configfs_add_default_group(&cfg->strings_group, &cfg->group); |
| 709 | |
| 710 | ret = usb_add_config_only(&gi->cdev, &cfg->c); |
| 711 | if (ret) |
| 712 | goto err; |
| 713 | |
| 714 | return &cfg->group; |
| 715 | err: |
| 716 | kfree(cfg->c.label); |
| 717 | kfree(cfg); |
| 718 | return ERR_PTR(ret); |
| 719 | } |
| 720 | |
| 721 | static void config_desc_drop( |
| 722 | struct config_group *group, |
| 723 | struct config_item *item) |
| 724 | { |
| 725 | config_item_put(item); |
| 726 | } |
| 727 | |
| 728 | static struct configfs_group_operations config_desc_ops = { |
| 729 | .make_group = &config_desc_make, |
| 730 | .drop_item = &config_desc_drop, |
| 731 | }; |
| 732 | |
| 733 | static struct config_item_type config_desc_type = { |
| 734 | .ct_group_ops = &config_desc_ops, |
| 735 | .ct_owner = THIS_MODULE, |
| 736 | }; |
| 737 | |
| 738 | GS_STRINGS_RW(gadget_strings, manufacturer); |
| 739 | GS_STRINGS_RW(gadget_strings, product); |
| 740 | GS_STRINGS_RW(gadget_strings, serialnumber); |
| 741 | |
| 742 | static struct configfs_attribute *gadget_strings_langid_attrs[] = { |
| 743 | &gadget_strings_attr_manufacturer, |
| 744 | &gadget_strings_attr_product, |
| 745 | &gadget_strings_attr_serialnumber, |
| 746 | NULL, |
| 747 | }; |
| 748 | |
| 749 | static void gadget_strings_attr_release(struct config_item *item) |
| 750 | { |
| 751 | struct gadget_strings *gs = to_gadget_strings(item); |
| 752 | |
| 753 | kfree(gs->manufacturer); |
| 754 | kfree(gs->product); |
| 755 | kfree(gs->serialnumber); |
| 756 | |
| 757 | list_del(&gs->list); |
| 758 | kfree(gs); |
| 759 | } |
| 760 | |
| 761 | USB_CONFIG_STRING_RW_OPS(gadget_strings); |
| 762 | USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info); |
| 763 | |
| 764 | static inline struct os_desc *to_os_desc(struct config_item *item) |
| 765 | { |
| 766 | return container_of(to_config_group(item), struct os_desc, group); |
| 767 | } |
| 768 | |
| 769 | static inline struct gadget_info *os_desc_item_to_gadget_info( |
| 770 | struct config_item *item) |
| 771 | { |
| 772 | return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent); |
| 773 | } |
| 774 | |
| 775 | static ssize_t os_desc_use_show(struct config_item *item, char *page) |
| 776 | { |
| 777 | return sprintf(page, "%d\n", |
| 778 | os_desc_item_to_gadget_info(item)->use_os_desc); |
| 779 | } |
| 780 | |
| 781 | static ssize_t os_desc_use_store(struct config_item *item, const char *page, |
| 782 | size_t len) |
| 783 | { |
| 784 | struct gadget_info *gi = os_desc_item_to_gadget_info(item); |
| 785 | int ret; |
| 786 | bool use; |
| 787 | |
| 788 | mutex_lock(&gi->lock); |
| 789 | ret = strtobool(page, &use); |
| 790 | if (!ret) { |
| 791 | gi->use_os_desc = use; |
| 792 | ret = len; |
| 793 | } |
| 794 | mutex_unlock(&gi->lock); |
| 795 | |
| 796 | return ret; |
| 797 | } |
| 798 | |
| 799 | static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page) |
| 800 | { |
| 801 | return sprintf(page, "0x%02x\n", |
| 802 | os_desc_item_to_gadget_info(item)->b_vendor_code); |
| 803 | } |
| 804 | |
| 805 | static ssize_t os_desc_b_vendor_code_store(struct config_item *item, |
| 806 | const char *page, size_t len) |
| 807 | { |
| 808 | struct gadget_info *gi = os_desc_item_to_gadget_info(item); |
| 809 | int ret; |
| 810 | u8 b_vendor_code; |
| 811 | |
| 812 | mutex_lock(&gi->lock); |
| 813 | ret = kstrtou8(page, 0, &b_vendor_code); |
| 814 | if (!ret) { |
| 815 | gi->b_vendor_code = b_vendor_code; |
| 816 | ret = len; |
| 817 | } |
| 818 | mutex_unlock(&gi->lock); |
| 819 | |
| 820 | return ret; |
| 821 | } |
| 822 | |
| 823 | static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page) |
| 824 | { |
| 825 | struct gadget_info *gi = os_desc_item_to_gadget_info(item); |
| 826 | int res; |
| 827 | |
| 828 | res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN, |
| 829 | UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1); |
| 830 | page[res++] = '\n'; |
| 831 | |
| 832 | return res; |
| 833 | } |
| 834 | |
| 835 | static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page, |
| 836 | size_t len) |
| 837 | { |
| 838 | struct gadget_info *gi = os_desc_item_to_gadget_info(item); |
| 839 | int res, l; |
| 840 | |
| 841 | l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1); |
| 842 | if (page[l - 1] == '\n') |
| 843 | --l; |
| 844 | |
| 845 | mutex_lock(&gi->lock); |
| 846 | res = utf8s_to_utf16s(page, l, |
| 847 | UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign, |
| 848 | OS_STRING_QW_SIGN_LEN); |
| 849 | if (res > 0) |
| 850 | res = len; |
| 851 | mutex_unlock(&gi->lock); |
| 852 | |
| 853 | return res; |
| 854 | } |
| 855 | |
| 856 | CONFIGFS_ATTR(os_desc_, use); |
| 857 | CONFIGFS_ATTR(os_desc_, b_vendor_code); |
| 858 | CONFIGFS_ATTR(os_desc_, qw_sign); |
| 859 | |
| 860 | static struct configfs_attribute *os_desc_attrs[] = { |
| 861 | &os_desc_attr_use, |
| 862 | &os_desc_attr_b_vendor_code, |
| 863 | &os_desc_attr_qw_sign, |
| 864 | NULL, |
| 865 | }; |
| 866 | |
| 867 | static void os_desc_attr_release(struct config_item *item) |
| 868 | { |
| 869 | struct os_desc *os_desc = to_os_desc(item); |
| 870 | kfree(os_desc); |
| 871 | } |
| 872 | |
| 873 | static int os_desc_link(struct config_item *os_desc_ci, |
| 874 | struct config_item *usb_cfg_ci) |
| 875 | { |
| 876 | struct gadget_info *gi = container_of(to_config_group(os_desc_ci), |
| 877 | struct gadget_info, os_desc_group); |
| 878 | struct usb_composite_dev *cdev = &gi->cdev; |
| 879 | struct config_usb_cfg *c_target = |
| 880 | container_of(to_config_group(usb_cfg_ci), |
| 881 | struct config_usb_cfg, group); |
| 882 | struct usb_configuration *c; |
| 883 | int ret; |
| 884 | |
| 885 | mutex_lock(&gi->lock); |
| 886 | list_for_each_entry(c, &cdev->configs, list) { |
| 887 | if (c == &c_target->c) |
| 888 | break; |
| 889 | } |
| 890 | if (c != &c_target->c) { |
| 891 | ret = -EINVAL; |
| 892 | goto out; |
| 893 | } |
| 894 | |
| 895 | if (cdev->os_desc_config) { |
| 896 | ret = -EBUSY; |
| 897 | goto out; |
| 898 | } |
| 899 | |
| 900 | cdev->os_desc_config = &c_target->c; |
| 901 | ret = 0; |
| 902 | |
| 903 | out: |
| 904 | mutex_unlock(&gi->lock); |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | static void os_desc_unlink(struct config_item *os_desc_ci, |
| 909 | struct config_item *usb_cfg_ci) |
| 910 | { |
| 911 | struct gadget_info *gi = container_of(to_config_group(os_desc_ci), |
| 912 | struct gadget_info, os_desc_group); |
| 913 | struct usb_composite_dev *cdev = &gi->cdev; |
| 914 | |
| 915 | mutex_lock(&gi->lock); |
| 916 | if (gi->composite.gadget_driver.udc_name) |
| 917 | unregister_gadget(gi); |
| 918 | cdev->os_desc_config = NULL; |
| 919 | WARN_ON(gi->composite.gadget_driver.udc_name); |
| 920 | mutex_unlock(&gi->lock); |
| 921 | } |
| 922 | |
| 923 | static struct configfs_item_operations os_desc_ops = { |
| 924 | .release = os_desc_attr_release, |
| 925 | .allow_link = os_desc_link, |
| 926 | .drop_link = os_desc_unlink, |
| 927 | }; |
| 928 | |
| 929 | static struct config_item_type os_desc_type = { |
| 930 | .ct_item_ops = &os_desc_ops, |
| 931 | .ct_attrs = os_desc_attrs, |
| 932 | .ct_owner = THIS_MODULE, |
| 933 | }; |
| 934 | |
| 935 | static inline struct usb_os_desc_ext_prop |
| 936 | *to_usb_os_desc_ext_prop(struct config_item *item) |
| 937 | { |
| 938 | return container_of(item, struct usb_os_desc_ext_prop, item); |
| 939 | } |
| 940 | |
| 941 | static ssize_t ext_prop_type_show(struct config_item *item, char *page) |
| 942 | { |
| 943 | return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type); |
| 944 | } |
| 945 | |
| 946 | static ssize_t ext_prop_type_store(struct config_item *item, |
| 947 | const char *page, size_t len) |
| 948 | { |
| 949 | struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); |
| 950 | struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); |
| 951 | u8 type; |
| 952 | int ret; |
| 953 | |
| 954 | if (desc->opts_mutex) |
| 955 | mutex_lock(desc->opts_mutex); |
| 956 | ret = kstrtou8(page, 0, &type); |
| 957 | if (ret) |
| 958 | goto end; |
| 959 | if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) { |
| 960 | ret = -EINVAL; |
| 961 | goto end; |
| 962 | } |
| 963 | |
| 964 | if ((ext_prop->type == USB_EXT_PROP_BINARY || |
| 965 | ext_prop->type == USB_EXT_PROP_LE32 || |
| 966 | ext_prop->type == USB_EXT_PROP_BE32) && |
| 967 | (type == USB_EXT_PROP_UNICODE || |
| 968 | type == USB_EXT_PROP_UNICODE_ENV || |
| 969 | type == USB_EXT_PROP_UNICODE_LINK)) |
| 970 | ext_prop->data_len <<= 1; |
| 971 | else if ((ext_prop->type == USB_EXT_PROP_UNICODE || |
| 972 | ext_prop->type == USB_EXT_PROP_UNICODE_ENV || |
| 973 | ext_prop->type == USB_EXT_PROP_UNICODE_LINK) && |
| 974 | (type == USB_EXT_PROP_BINARY || |
| 975 | type == USB_EXT_PROP_LE32 || |
| 976 | type == USB_EXT_PROP_BE32)) |
| 977 | ext_prop->data_len >>= 1; |
| 978 | ext_prop->type = type; |
| 979 | ret = len; |
| 980 | |
| 981 | end: |
| 982 | if (desc->opts_mutex) |
| 983 | mutex_unlock(desc->opts_mutex); |
| 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | static ssize_t ext_prop_data_show(struct config_item *item, char *page) |
| 988 | { |
| 989 | struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); |
| 990 | int len = ext_prop->data_len; |
| 991 | |
| 992 | if (ext_prop->type == USB_EXT_PROP_UNICODE || |
| 993 | ext_prop->type == USB_EXT_PROP_UNICODE_ENV || |
| 994 | ext_prop->type == USB_EXT_PROP_UNICODE_LINK) |
| 995 | len >>= 1; |
| 996 | memcpy(page, ext_prop->data, len); |
| 997 | |
| 998 | return len; |
| 999 | } |
| 1000 | |
| 1001 | static ssize_t ext_prop_data_store(struct config_item *item, |
| 1002 | const char *page, size_t len) |
| 1003 | { |
| 1004 | struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); |
| 1005 | struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); |
| 1006 | char *new_data; |
| 1007 | size_t ret_len = len; |
| 1008 | |
| 1009 | if (page[len - 1] == '\n' || page[len - 1] == '\0') |
| 1010 | --len; |
| 1011 | new_data = kmemdup(page, len, GFP_KERNEL); |
| 1012 | if (!new_data) |
| 1013 | return -ENOMEM; |
| 1014 | |
| 1015 | if (desc->opts_mutex) |
| 1016 | mutex_lock(desc->opts_mutex); |
| 1017 | kfree(ext_prop->data); |
| 1018 | ext_prop->data = new_data; |
| 1019 | desc->ext_prop_len -= ext_prop->data_len; |
| 1020 | ext_prop->data_len = len; |
| 1021 | desc->ext_prop_len += ext_prop->data_len; |
| 1022 | if (ext_prop->type == USB_EXT_PROP_UNICODE || |
| 1023 | ext_prop->type == USB_EXT_PROP_UNICODE_ENV || |
| 1024 | ext_prop->type == USB_EXT_PROP_UNICODE_LINK) { |
| 1025 | desc->ext_prop_len -= ext_prop->data_len; |
| 1026 | ext_prop->data_len <<= 1; |
| 1027 | ext_prop->data_len += 2; |
| 1028 | desc->ext_prop_len += ext_prop->data_len; |
| 1029 | } |
| 1030 | if (desc->opts_mutex) |
| 1031 | mutex_unlock(desc->opts_mutex); |
| 1032 | return ret_len; |
| 1033 | } |
| 1034 | |
| 1035 | CONFIGFS_ATTR(ext_prop_, type); |
| 1036 | CONFIGFS_ATTR(ext_prop_, data); |
| 1037 | |
| 1038 | static struct configfs_attribute *ext_prop_attrs[] = { |
| 1039 | &ext_prop_attr_type, |
| 1040 | &ext_prop_attr_data, |
| 1041 | NULL, |
| 1042 | }; |
| 1043 | |
| 1044 | static void usb_os_desc_ext_prop_release(struct config_item *item) |
| 1045 | { |
| 1046 | struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); |
| 1047 | |
| 1048 | kfree(ext_prop); /* frees a whole chunk */ |
| 1049 | } |
| 1050 | |
| 1051 | static struct configfs_item_operations ext_prop_ops = { |
| 1052 | .release = usb_os_desc_ext_prop_release, |
| 1053 | }; |
| 1054 | |
| 1055 | static struct config_item *ext_prop_make( |
| 1056 | struct config_group *group, |
| 1057 | const char *name) |
| 1058 | { |
| 1059 | struct usb_os_desc_ext_prop *ext_prop; |
| 1060 | struct config_item_type *ext_prop_type; |
| 1061 | struct usb_os_desc *desc; |
| 1062 | char *vlabuf; |
| 1063 | |
| 1064 | vla_group(data_chunk); |
| 1065 | vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1); |
| 1066 | vla_item(data_chunk, struct config_item_type, ext_prop_type, 1); |
| 1067 | |
| 1068 | vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); |
| 1069 | if (!vlabuf) |
| 1070 | return ERR_PTR(-ENOMEM); |
| 1071 | |
| 1072 | ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop); |
| 1073 | ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type); |
| 1074 | |
| 1075 | desc = container_of(group, struct usb_os_desc, group); |
| 1076 | ext_prop_type->ct_item_ops = &ext_prop_ops; |
| 1077 | ext_prop_type->ct_attrs = ext_prop_attrs; |
| 1078 | ext_prop_type->ct_owner = desc->owner; |
| 1079 | |
| 1080 | config_item_init_type_name(&ext_prop->item, name, ext_prop_type); |
| 1081 | |
| 1082 | ext_prop->name = kstrdup(name, GFP_KERNEL); |
| 1083 | if (!ext_prop->name) { |
| 1084 | kfree(vlabuf); |
| 1085 | return ERR_PTR(-ENOMEM); |
| 1086 | } |
| 1087 | desc->ext_prop_len += 14; |
| 1088 | ext_prop->name_len = 2 * strlen(ext_prop->name) + 2; |
| 1089 | if (desc->opts_mutex) |
| 1090 | mutex_lock(desc->opts_mutex); |
| 1091 | desc->ext_prop_len += ext_prop->name_len; |
| 1092 | list_add_tail(&ext_prop->entry, &desc->ext_prop); |
| 1093 | ++desc->ext_prop_count; |
| 1094 | if (desc->opts_mutex) |
| 1095 | mutex_unlock(desc->opts_mutex); |
| 1096 | |
| 1097 | return &ext_prop->item; |
| 1098 | } |
| 1099 | |
| 1100 | static void ext_prop_drop(struct config_group *group, struct config_item *item) |
| 1101 | { |
| 1102 | struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); |
| 1103 | struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item); |
| 1104 | |
| 1105 | if (desc->opts_mutex) |
| 1106 | mutex_lock(desc->opts_mutex); |
| 1107 | list_del(&ext_prop->entry); |
| 1108 | --desc->ext_prop_count; |
| 1109 | kfree(ext_prop->name); |
| 1110 | desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14); |
| 1111 | if (desc->opts_mutex) |
| 1112 | mutex_unlock(desc->opts_mutex); |
| 1113 | config_item_put(item); |
| 1114 | } |
| 1115 | |
| 1116 | static struct configfs_group_operations interf_grp_ops = { |
| 1117 | .make_item = &ext_prop_make, |
| 1118 | .drop_item = &ext_prop_drop, |
| 1119 | }; |
| 1120 | |
| 1121 | static ssize_t interf_grp_compatible_id_show(struct config_item *item, |
| 1122 | char *page) |
| 1123 | { |
| 1124 | memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8); |
| 1125 | return 8; |
| 1126 | } |
| 1127 | |
| 1128 | static ssize_t interf_grp_compatible_id_store(struct config_item *item, |
| 1129 | const char *page, size_t len) |
| 1130 | { |
| 1131 | struct usb_os_desc *desc = to_usb_os_desc(item); |
| 1132 | int l; |
| 1133 | |
| 1134 | l = min_t(int, 8, len); |
| 1135 | if (page[l - 1] == '\n') |
| 1136 | --l; |
| 1137 | if (desc->opts_mutex) |
| 1138 | mutex_lock(desc->opts_mutex); |
| 1139 | memcpy(desc->ext_compat_id, page, l); |
| 1140 | |
| 1141 | if (desc->opts_mutex) |
| 1142 | mutex_unlock(desc->opts_mutex); |
| 1143 | |
| 1144 | return len; |
| 1145 | } |
| 1146 | |
| 1147 | static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item, |
| 1148 | char *page) |
| 1149 | { |
| 1150 | memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8); |
| 1151 | return 8; |
| 1152 | } |
| 1153 | |
| 1154 | static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item, |
| 1155 | const char *page, size_t len) |
| 1156 | { |
| 1157 | struct usb_os_desc *desc = to_usb_os_desc(item); |
| 1158 | int l; |
| 1159 | |
| 1160 | l = min_t(int, 8, len); |
| 1161 | if (page[l - 1] == '\n') |
| 1162 | --l; |
| 1163 | if (desc->opts_mutex) |
| 1164 | mutex_lock(desc->opts_mutex); |
| 1165 | memcpy(desc->ext_compat_id + 8, page, l); |
| 1166 | |
| 1167 | if (desc->opts_mutex) |
| 1168 | mutex_unlock(desc->opts_mutex); |
| 1169 | |
| 1170 | return len; |
| 1171 | } |
| 1172 | |
| 1173 | CONFIGFS_ATTR(interf_grp_, compatible_id); |
| 1174 | CONFIGFS_ATTR(interf_grp_, sub_compatible_id); |
| 1175 | |
| 1176 | static struct configfs_attribute *interf_grp_attrs[] = { |
| 1177 | &interf_grp_attr_compatible_id, |
| 1178 | &interf_grp_attr_sub_compatible_id, |
| 1179 | NULL |
| 1180 | }; |
| 1181 | |
| 1182 | struct config_group *usb_os_desc_prepare_interf_dir( |
| 1183 | struct config_group *parent, |
| 1184 | int n_interf, |
| 1185 | struct usb_os_desc **desc, |
| 1186 | char **names, |
| 1187 | struct module *owner) |
| 1188 | { |
| 1189 | struct config_group *os_desc_group; |
| 1190 | struct config_item_type *os_desc_type, *interface_type; |
| 1191 | |
| 1192 | vla_group(data_chunk); |
| 1193 | vla_item(data_chunk, struct config_group, os_desc_group, 1); |
| 1194 | vla_item(data_chunk, struct config_item_type, os_desc_type, 1); |
| 1195 | vla_item(data_chunk, struct config_item_type, interface_type, 1); |
| 1196 | |
| 1197 | char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); |
| 1198 | if (!vlabuf) |
| 1199 | return ERR_PTR(-ENOMEM); |
| 1200 | |
| 1201 | os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group); |
| 1202 | os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type); |
| 1203 | interface_type = vla_ptr(vlabuf, data_chunk, interface_type); |
| 1204 | |
| 1205 | os_desc_type->ct_owner = owner; |
| 1206 | config_group_init_type_name(os_desc_group, "os_desc", os_desc_type); |
| 1207 | configfs_add_default_group(os_desc_group, parent); |
| 1208 | |
| 1209 | interface_type->ct_group_ops = &interf_grp_ops; |
| 1210 | interface_type->ct_attrs = interf_grp_attrs; |
| 1211 | interface_type->ct_owner = owner; |
| 1212 | |
| 1213 | while (n_interf--) { |
| 1214 | struct usb_os_desc *d; |
| 1215 | |
| 1216 | d = desc[n_interf]; |
| 1217 | d->owner = owner; |
| 1218 | config_group_init_type_name(&d->group, "", interface_type); |
| 1219 | config_item_set_name(&d->group.cg_item, "interface.%s", |
| 1220 | names[n_interf]); |
| 1221 | configfs_add_default_group(&d->group, os_desc_group); |
| 1222 | } |
| 1223 | |
| 1224 | return os_desc_group; |
| 1225 | } |
| 1226 | EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir); |
| 1227 | |
| 1228 | static int configfs_do_nothing(struct usb_composite_dev *cdev) |
| 1229 | { |
| 1230 | WARN_ON(1); |
| 1231 | return -EINVAL; |
| 1232 | } |
| 1233 | |
| 1234 | int composite_dev_prepare(struct usb_composite_driver *composite, |
| 1235 | struct usb_composite_dev *dev); |
| 1236 | |
| 1237 | int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, |
| 1238 | struct usb_ep *ep0); |
| 1239 | |
| 1240 | static void purge_configs_funcs(struct gadget_info *gi) |
| 1241 | { |
| 1242 | struct usb_configuration *c; |
| 1243 | |
| 1244 | list_for_each_entry(c, &gi->cdev.configs, list) { |
| 1245 | struct usb_function *f, *tmp; |
| 1246 | struct config_usb_cfg *cfg; |
| 1247 | |
| 1248 | cfg = container_of(c, struct config_usb_cfg, c); |
| 1249 | |
| 1250 | list_for_each_entry_safe(f, tmp, &c->functions, list) { |
| 1251 | |
| 1252 | list_move_tail(&f->list, &cfg->func_list); |
| 1253 | if (f->unbind) { |
| 1254 | dev_dbg(&gi->cdev.gadget->dev, |
| 1255 | "unbind function '%s'/%p\n", |
| 1256 | f->name, f); |
| 1257 | f->unbind(c, f); |
| 1258 | } |
| 1259 | } |
| 1260 | c->next_interface_id = 0; |
| 1261 | memset(c->interface, 0, sizeof(c->interface)); |
| 1262 | c->superspeed_plus = 0; |
| 1263 | c->superspeed = 0; |
| 1264 | c->highspeed = 0; |
| 1265 | c->fullspeed = 0; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | static int configfs_composite_bind(struct usb_gadget *gadget, |
| 1270 | struct usb_gadget_driver *gdriver) |
| 1271 | { |
| 1272 | struct usb_composite_driver *composite = to_cdriver(gdriver); |
| 1273 | struct gadget_info *gi = container_of(composite, |
| 1274 | struct gadget_info, composite); |
| 1275 | struct usb_composite_dev *cdev = &gi->cdev; |
| 1276 | struct usb_configuration *c; |
| 1277 | struct usb_string *s; |
| 1278 | unsigned i; |
| 1279 | int ret; |
| 1280 | |
| 1281 | /* the gi->lock is hold by the caller */ |
| 1282 | gi->unbind = 0; |
| 1283 | cdev->gadget = gadget; |
| 1284 | set_gadget_data(gadget, cdev); |
| 1285 | ret = composite_dev_prepare(composite, cdev); |
| 1286 | if (ret) |
| 1287 | return ret; |
| 1288 | /* and now the gadget bind */ |
| 1289 | ret = -EINVAL; |
| 1290 | |
| 1291 | if (list_empty(&gi->cdev.configs)) { |
| 1292 | pr_err("Need at least one configuration in %s.\n", |
| 1293 | gi->composite.name); |
| 1294 | goto err_comp_cleanup; |
| 1295 | } |
| 1296 | |
| 1297 | |
| 1298 | list_for_each_entry(c, &gi->cdev.configs, list) { |
| 1299 | struct config_usb_cfg *cfg; |
| 1300 | |
| 1301 | cfg = container_of(c, struct config_usb_cfg, c); |
| 1302 | if (list_empty(&cfg->func_list)) { |
| 1303 | pr_err("Config %s/%d of %s needs at least one function.\n", |
| 1304 | c->label, c->bConfigurationValue, |
| 1305 | gi->composite.name); |
| 1306 | goto err_comp_cleanup; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /* init all strings */ |
| 1311 | if (!list_empty(&gi->string_list)) { |
| 1312 | struct gadget_strings *gs; |
| 1313 | |
| 1314 | i = 0; |
| 1315 | list_for_each_entry(gs, &gi->string_list, list) { |
| 1316 | |
| 1317 | gi->gstrings[i] = &gs->stringtab_dev; |
| 1318 | gs->stringtab_dev.strings = gs->strings; |
| 1319 | gs->strings[USB_GADGET_MANUFACTURER_IDX].s = |
| 1320 | gs->manufacturer; |
| 1321 | gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product; |
| 1322 | gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber; |
| 1323 | i++; |
| 1324 | } |
| 1325 | gi->gstrings[i] = NULL; |
| 1326 | s = usb_gstrings_attach(&gi->cdev, gi->gstrings, |
| 1327 | USB_GADGET_FIRST_AVAIL_IDX); |
| 1328 | if (IS_ERR(s)) { |
| 1329 | ret = PTR_ERR(s); |
| 1330 | goto err_comp_cleanup; |
| 1331 | } |
| 1332 | |
| 1333 | gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id; |
| 1334 | gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id; |
| 1335 | gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id; |
| 1336 | } |
| 1337 | |
| 1338 | if (gi->use_os_desc) { |
| 1339 | cdev->use_os_string = true; |
| 1340 | cdev->b_vendor_code = gi->b_vendor_code; |
| 1341 | memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN); |
| 1342 | } |
| 1343 | |
| 1344 | if (gadget_is_otg(gadget) && !otg_desc[0]) { |
| 1345 | struct usb_descriptor_header *usb_desc; |
| 1346 | |
| 1347 | usb_desc = usb_otg_descriptor_alloc(gadget); |
| 1348 | if (!usb_desc) { |
| 1349 | ret = -ENOMEM; |
| 1350 | goto err_comp_cleanup; |
| 1351 | } |
| 1352 | usb_otg_descriptor_init(gadget, usb_desc); |
| 1353 | otg_desc[0] = usb_desc; |
| 1354 | otg_desc[1] = NULL; |
| 1355 | } |
| 1356 | |
| 1357 | /* Go through all configs, attach all functions */ |
| 1358 | list_for_each_entry(c, &gi->cdev.configs, list) { |
| 1359 | struct config_usb_cfg *cfg; |
| 1360 | struct usb_function *f; |
| 1361 | struct usb_function *tmp; |
| 1362 | struct gadget_config_name *cn; |
| 1363 | |
| 1364 | if (gadget_is_otg(gadget)) |
| 1365 | c->descriptors = otg_desc; |
| 1366 | |
| 1367 | cfg = container_of(c, struct config_usb_cfg, c); |
| 1368 | if (!list_empty(&cfg->string_list)) { |
| 1369 | i = 0; |
| 1370 | list_for_each_entry(cn, &cfg->string_list, list) { |
| 1371 | cfg->gstrings[i] = &cn->stringtab_dev; |
| 1372 | cn->stringtab_dev.strings = &cn->strings; |
| 1373 | cn->strings.s = cn->configuration; |
| 1374 | i++; |
| 1375 | } |
| 1376 | cfg->gstrings[i] = NULL; |
| 1377 | s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1); |
| 1378 | if (IS_ERR(s)) { |
| 1379 | ret = PTR_ERR(s); |
| 1380 | goto err_comp_cleanup; |
| 1381 | } |
| 1382 | c->iConfiguration = s[0].id; |
| 1383 | } |
| 1384 | |
| 1385 | list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { |
| 1386 | list_del(&f->list); |
| 1387 | ret = usb_add_function(c, f); |
| 1388 | if (ret) { |
| 1389 | list_add(&f->list, &cfg->func_list); |
| 1390 | goto err_purge_funcs; |
| 1391 | } |
| 1392 | } |
| 1393 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1394 | } |
| 1395 | if (cdev->use_os_string) { |
| 1396 | ret = composite_os_desc_req_prepare(cdev, gadget->ep0); |
| 1397 | if (ret) |
| 1398 | goto err_purge_funcs; |
| 1399 | } |
| 1400 | |
| 1401 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1402 | return 0; |
| 1403 | |
| 1404 | err_purge_funcs: |
| 1405 | purge_configs_funcs(gi); |
| 1406 | err_comp_cleanup: |
| 1407 | composite_dev_cleanup(cdev); |
| 1408 | return ret; |
| 1409 | } |
| 1410 | |
| 1411 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1412 | static void android_work(struct work_struct *data) |
| 1413 | { |
| 1414 | struct gadget_info *gi = container_of(data, struct gadget_info, work); |
| 1415 | struct usb_composite_dev *cdev = &gi->cdev; |
| 1416 | char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL }; |
| 1417 | char *connected[2] = { "USB_STATE=CONNECTED", NULL }; |
| 1418 | char *configured[2] = { "USB_STATE=CONFIGURED", NULL }; |
| 1419 | /* 0-connected 1-configured 2-disconnected*/ |
| 1420 | bool status[3] = { false, false, false }; |
| 1421 | unsigned long flags; |
| 1422 | bool uevent_sent = false; |
| 1423 | |
| 1424 | spin_lock_irqsave(&cdev->lock, flags); |
| 1425 | if (cdev->config) |
| 1426 | status[1] = true; |
| 1427 | |
| 1428 | if (gi->connected != gi->sw_connected) { |
| 1429 | if (gi->connected) |
| 1430 | status[0] = true; |
| 1431 | else |
| 1432 | status[2] = true; |
| 1433 | gi->sw_connected = gi->connected; |
| 1434 | } |
| 1435 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1436 | |
| 1437 | if (status[0]) { |
| 1438 | kobject_uevent_env(&android_device->kobj, |
| 1439 | KOBJ_CHANGE, connected); |
| 1440 | pr_info("%s: sent uevent %s\n", __func__, connected[0]); |
| 1441 | uevent_sent = true; |
| 1442 | } |
| 1443 | |
| 1444 | if (status[1]) { |
| 1445 | kobject_uevent_env(&android_device->kobj, |
| 1446 | KOBJ_CHANGE, configured); |
| 1447 | pr_info("%s: sent uevent %s\n", __func__, configured[0]); |
| 1448 | uevent_sent = true; |
| 1449 | } |
| 1450 | |
| 1451 | if (status[2]) { |
| 1452 | kobject_uevent_env(&android_device->kobj, |
| 1453 | KOBJ_CHANGE, disconnected); |
| 1454 | pr_info("%s: sent uevent %s\n", __func__, disconnected[0]); |
| 1455 | uevent_sent = true; |
| 1456 | } |
| 1457 | |
| 1458 | if (!uevent_sent) { |
| 1459 | pr_info("%s: did not send uevent (%d %d %p)\n", __func__, |
| 1460 | gi->connected, gi->sw_connected, cdev->config); |
| 1461 | } |
| 1462 | } |
| 1463 | #endif |
| 1464 | |
| 1465 | static void configfs_composite_unbind(struct usb_gadget *gadget) |
| 1466 | { |
| 1467 | struct usb_composite_dev *cdev; |
| 1468 | struct gadget_info *gi; |
| 1469 | unsigned long flags; |
| 1470 | |
| 1471 | /* the gi->lock is hold by the caller */ |
| 1472 | |
| 1473 | cdev = get_gadget_data(gadget); |
| 1474 | gi = container_of(cdev, struct gadget_info, cdev); |
| 1475 | spin_lock_irqsave(&gi->spinlock, flags); |
| 1476 | gi->unbind = 1; |
| 1477 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1478 | |
| 1479 | kfree(otg_desc[0]); |
| 1480 | otg_desc[0] = NULL; |
| 1481 | purge_configs_funcs(gi); |
| 1482 | composite_dev_cleanup(cdev); |
| 1483 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1484 | spin_lock_irqsave(&gi->spinlock, flags); |
| 1485 | cdev->gadget = NULL; |
| 1486 | set_gadget_data(gadget, NULL); |
| 1487 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1488 | } |
| 1489 | |
| 1490 | static int configfs_composite_setup(struct usb_gadget *gadget, |
| 1491 | const struct usb_ctrlrequest *ctrl) |
| 1492 | { |
| 1493 | struct usb_composite_dev *cdev; |
| 1494 | struct gadget_info *gi; |
| 1495 | unsigned long flags; |
| 1496 | int ret; |
| 1497 | |
| 1498 | cdev = get_gadget_data(gadget); |
| 1499 | if (!cdev) |
| 1500 | return 0; |
| 1501 | |
| 1502 | gi = container_of(cdev, struct gadget_info, cdev); |
| 1503 | spin_lock_irqsave(&gi->spinlock, flags); |
| 1504 | cdev = get_gadget_data(gadget); |
| 1505 | if (!cdev || gi->unbind) { |
| 1506 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | ret = composite_setup(gadget, ctrl); |
| 1511 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
| 1515 | static void configfs_composite_disconnect(struct usb_gadget *gadget) |
| 1516 | { |
| 1517 | struct usb_composite_dev *cdev; |
| 1518 | struct gadget_info *gi; |
| 1519 | unsigned long flags; |
| 1520 | |
| 1521 | cdev = get_gadget_data(gadget); |
| 1522 | if (!cdev) |
| 1523 | return; |
| 1524 | |
| 1525 | gi = container_of(cdev, struct gadget_info, cdev); |
| 1526 | spin_lock_irqsave(&gi->spinlock, flags); |
| 1527 | cdev = get_gadget_data(gadget); |
| 1528 | if (!cdev || gi->unbind) { |
| 1529 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1530 | return; |
| 1531 | } |
| 1532 | |
| 1533 | composite_disconnect(gadget); |
| 1534 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1535 | } |
| 1536 | |
| 1537 | static void configfs_composite_suspend(struct usb_gadget *gadget) |
| 1538 | { |
| 1539 | struct usb_composite_dev *cdev; |
| 1540 | struct gadget_info *gi; |
| 1541 | unsigned long flags; |
| 1542 | |
| 1543 | cdev = get_gadget_data(gadget); |
| 1544 | if (!cdev) |
| 1545 | return; |
| 1546 | |
| 1547 | gi = container_of(cdev, struct gadget_info, cdev); |
| 1548 | spin_lock_irqsave(&gi->spinlock, flags); |
| 1549 | cdev = get_gadget_data(gadget); |
| 1550 | if (!cdev || gi->unbind) { |
| 1551 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1552 | return; |
| 1553 | } |
| 1554 | |
| 1555 | composite_suspend(gadget); |
| 1556 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1557 | } |
| 1558 | |
| 1559 | static void configfs_composite_resume(struct usb_gadget *gadget) |
| 1560 | { |
| 1561 | struct usb_composite_dev *cdev; |
| 1562 | struct gadget_info *gi; |
| 1563 | unsigned long flags; |
| 1564 | |
| 1565 | cdev = get_gadget_data(gadget); |
| 1566 | if (!cdev) |
| 1567 | return; |
| 1568 | |
| 1569 | gi = container_of(cdev, struct gadget_info, cdev); |
| 1570 | spin_lock_irqsave(&gi->spinlock, flags); |
| 1571 | cdev = get_gadget_data(gadget); |
| 1572 | if (!cdev || gi->unbind) { |
| 1573 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1574 | return; |
| 1575 | } |
| 1576 | |
| 1577 | composite_resume(gadget); |
| 1578 | spin_unlock_irqrestore(&gi->spinlock, flags); |
| 1579 | } |
| 1580 | |
| 1581 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1582 | static int android_setup(struct usb_gadget *gadget, |
| 1583 | const struct usb_ctrlrequest *c) |
| 1584 | { |
| 1585 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1586 | unsigned long flags; |
| 1587 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); |
| 1588 | int value = -EOPNOTSUPP; |
| 1589 | struct usb_function_instance *fi; |
| 1590 | |
| 1591 | spin_lock_irqsave(&cdev->lock, flags); |
| 1592 | if (!gi->connected) { |
| 1593 | gi->connected = 1; |
| 1594 | schedule_work(&gi->work); |
| 1595 | } |
| 1596 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1597 | list_for_each_entry(fi, &gi->available_func, cfs_list) { |
| 1598 | if (fi != NULL && fi->f != NULL && fi->f->setup != NULL) { |
| 1599 | value = fi->f->setup(fi->f, c); |
| 1600 | if (value >= 0) |
| 1601 | break; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | #ifdef CONFIG_USB_CONFIGFS_F_ACC |
| 1606 | if (value < 0) |
| 1607 | value = acc_ctrlrequest(cdev, c); |
| 1608 | #endif |
| 1609 | |
| 1610 | if (value < 0) |
| 1611 | value = composite_setup(gadget, c); |
| 1612 | |
| 1613 | spin_lock_irqsave(&cdev->lock, flags); |
| 1614 | if (c->bRequest == USB_REQ_SET_CONFIGURATION && |
| 1615 | cdev->config) { |
| 1616 | schedule_work(&gi->work); |
| 1617 | } |
| 1618 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1619 | |
| 1620 | return value; |
| 1621 | } |
| 1622 | |
| 1623 | static void android_disconnect(struct usb_gadget *gadget) |
| 1624 | { |
| 1625 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1626 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); |
| 1627 | |
| 1628 | /* FIXME: There's a race between usb_gadget_udc_stop() which is likely |
| 1629 | * to set the gadget driver to NULL in the udc driver and this drivers |
| 1630 | * gadget disconnect fn which likely checks for the gadget driver to |
| 1631 | * be a null ptr. It happens that unbind (doing set_gadget_data(NULL)) |
| 1632 | * is called before the gadget driver is set to NULL and the udc driver |
| 1633 | * calls disconnect fn which results in cdev being a null ptr. |
| 1634 | */ |
| 1635 | if (cdev == NULL) { |
| 1636 | WARN(1, "%s: gadget driver already disconnected\n", __func__); |
| 1637 | return; |
| 1638 | } |
| 1639 | |
| 1640 | /* accessory HID support can be active while the |
| 1641 | accessory function is not actually enabled, |
| 1642 | so we need to inform it when we are disconnected. |
| 1643 | */ |
| 1644 | |
| 1645 | #ifdef CONFIG_USB_CONFIGFS_F_ACC |
| 1646 | acc_disconnect(); |
| 1647 | #endif |
| 1648 | gi->connected = 0; |
| 1649 | schedule_work(&gi->work); |
| 1650 | composite_disconnect(gadget); |
| 1651 | } |
| 1652 | #endif |
| 1653 | |
| 1654 | static const struct usb_gadget_driver configfs_driver_template = { |
| 1655 | .bind = configfs_composite_bind, |
| 1656 | .unbind = configfs_composite_unbind, |
| 1657 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1658 | .setup = android_setup, |
| 1659 | .reset = android_disconnect, |
| 1660 | .disconnect = android_disconnect, |
| 1661 | #else |
| 1662 | .setup = configfs_composite_setup, |
| 1663 | .reset = configfs_composite_disconnect, |
| 1664 | .disconnect = configfs_composite_disconnect, |
| 1665 | #endif |
| 1666 | .suspend = configfs_composite_suspend, |
| 1667 | .resume = configfs_composite_resume, |
| 1668 | |
| 1669 | .max_speed = USB_SPEED_SUPER, |
| 1670 | .driver = { |
| 1671 | .owner = THIS_MODULE, |
| 1672 | .name = "configfs-gadget", |
| 1673 | }, |
| 1674 | .match_existing_only = 1, |
| 1675 | }; |
| 1676 | |
| 1677 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1678 | static ssize_t state_show(struct device *pdev, struct device_attribute *attr, |
| 1679 | char *buf) |
| 1680 | { |
| 1681 | struct gadget_info *dev = dev_get_drvdata(pdev); |
| 1682 | struct usb_composite_dev *cdev; |
| 1683 | char *state = "DISCONNECTED"; |
| 1684 | unsigned long flags; |
| 1685 | |
| 1686 | if (!dev) |
| 1687 | goto out; |
| 1688 | |
| 1689 | cdev = &dev->cdev; |
| 1690 | |
| 1691 | if (!cdev) |
| 1692 | goto out; |
| 1693 | |
| 1694 | spin_lock_irqsave(&cdev->lock, flags); |
| 1695 | if (cdev->config) |
| 1696 | state = "CONFIGURED"; |
| 1697 | else if (dev->connected) |
| 1698 | state = "CONNECTED"; |
| 1699 | spin_unlock_irqrestore(&cdev->lock, flags); |
| 1700 | out: |
| 1701 | return sprintf(buf, "%s\n", state); |
| 1702 | } |
| 1703 | |
| 1704 | static DEVICE_ATTR(state, S_IRUGO, state_show, NULL); |
| 1705 | |
| 1706 | static struct device_attribute *android_usb_attributes[] = { |
| 1707 | &dev_attr_state, |
| 1708 | NULL |
| 1709 | }; |
| 1710 | |
| 1711 | static int android_device_create(struct gadget_info *gi) |
| 1712 | { |
| 1713 | struct device_attribute **attrs; |
| 1714 | struct device_attribute *attr; |
| 1715 | |
| 1716 | INIT_WORK(&gi->work, android_work); |
| 1717 | android_device = device_create(android_class, NULL, |
| 1718 | MKDEV(0, 0), NULL, "android0"); |
| 1719 | if (IS_ERR(android_device)) |
| 1720 | return PTR_ERR(android_device); |
| 1721 | |
| 1722 | dev_set_drvdata(android_device, gi); |
| 1723 | |
| 1724 | attrs = android_usb_attributes; |
| 1725 | while ((attr = *attrs++)) { |
| 1726 | int err; |
| 1727 | |
| 1728 | err = device_create_file(android_device, attr); |
| 1729 | if (err) { |
| 1730 | device_destroy(android_device->class, |
| 1731 | android_device->devt); |
| 1732 | return err; |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | static void android_device_destroy(void) |
| 1740 | { |
| 1741 | struct device_attribute **attrs; |
| 1742 | struct device_attribute *attr; |
| 1743 | |
| 1744 | attrs = android_usb_attributes; |
| 1745 | while ((attr = *attrs++)) |
| 1746 | device_remove_file(android_device, attr); |
| 1747 | device_destroy(android_device->class, android_device->devt); |
| 1748 | } |
| 1749 | #else |
| 1750 | static inline int android_device_create(struct gadget_info *gi) |
| 1751 | { |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
| 1755 | static inline void android_device_destroy(void) |
| 1756 | { |
| 1757 | } |
| 1758 | #endif |
| 1759 | |
| 1760 | static struct config_group *gadgets_make( |
| 1761 | struct config_group *group, |
| 1762 | const char *name) |
| 1763 | { |
| 1764 | struct gadget_info *gi; |
| 1765 | |
| 1766 | gi = kzalloc(sizeof(*gi), GFP_KERNEL); |
| 1767 | if (!gi) |
| 1768 | return ERR_PTR(-ENOMEM); |
| 1769 | |
| 1770 | config_group_init_type_name(&gi->group, name, &gadget_root_type); |
| 1771 | |
| 1772 | config_group_init_type_name(&gi->functions_group, "functions", |
| 1773 | &functions_type); |
| 1774 | configfs_add_default_group(&gi->functions_group, &gi->group); |
| 1775 | |
| 1776 | config_group_init_type_name(&gi->configs_group, "configs", |
| 1777 | &config_desc_type); |
| 1778 | configfs_add_default_group(&gi->configs_group, &gi->group); |
| 1779 | |
| 1780 | config_group_init_type_name(&gi->strings_group, "strings", |
| 1781 | &gadget_strings_strings_type); |
| 1782 | configfs_add_default_group(&gi->strings_group, &gi->group); |
| 1783 | |
| 1784 | config_group_init_type_name(&gi->os_desc_group, "os_desc", |
| 1785 | &os_desc_type); |
| 1786 | configfs_add_default_group(&gi->os_desc_group, &gi->group); |
| 1787 | |
| 1788 | gi->composite.bind = configfs_do_nothing; |
| 1789 | gi->composite.unbind = configfs_do_nothing; |
| 1790 | gi->composite.suspend = NULL; |
| 1791 | gi->composite.resume = NULL; |
| 1792 | gi->composite.max_speed = USB_SPEED_SUPER; |
| 1793 | |
| 1794 | spin_lock_init(&gi->spinlock); |
| 1795 | mutex_init(&gi->lock); |
| 1796 | INIT_LIST_HEAD(&gi->string_list); |
| 1797 | INIT_LIST_HEAD(&gi->available_func); |
| 1798 | |
| 1799 | composite_init_dev(&gi->cdev); |
| 1800 | gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE; |
| 1801 | gi->cdev.desc.bDescriptorType = USB_DT_DEVICE; |
| 1802 | gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice()); |
| 1803 | |
| 1804 | gi->composite.gadget_driver = configfs_driver_template; |
| 1805 | |
| 1806 | gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL); |
| 1807 | gi->composite.name = gi->composite.gadget_driver.function; |
| 1808 | |
| 1809 | if (!gi->composite.gadget_driver.function) |
| 1810 | goto err; |
| 1811 | |
| 1812 | if (android_device_create(gi) < 0) |
| 1813 | goto err; |
| 1814 | |
| 1815 | return &gi->group; |
| 1816 | |
| 1817 | err: |
| 1818 | kfree(gi); |
| 1819 | return ERR_PTR(-ENOMEM); |
| 1820 | } |
| 1821 | |
| 1822 | static void gadgets_drop(struct config_group *group, struct config_item *item) |
| 1823 | { |
| 1824 | config_item_put(item); |
| 1825 | android_device_destroy(); |
| 1826 | } |
| 1827 | |
| 1828 | static struct configfs_group_operations gadgets_ops = { |
| 1829 | .make_group = &gadgets_make, |
| 1830 | .drop_item = &gadgets_drop, |
| 1831 | }; |
| 1832 | |
| 1833 | static struct config_item_type gadgets_type = { |
| 1834 | .ct_group_ops = &gadgets_ops, |
| 1835 | .ct_owner = THIS_MODULE, |
| 1836 | }; |
| 1837 | |
| 1838 | static struct configfs_subsystem gadget_subsys = { |
| 1839 | .su_group = { |
| 1840 | .cg_item = { |
| 1841 | .ci_namebuf = "usb_gadget", |
| 1842 | .ci_type = &gadgets_type, |
| 1843 | }, |
| 1844 | }, |
| 1845 | .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex), |
| 1846 | }; |
| 1847 | |
| 1848 | void unregister_gadget_item(struct config_item *item) |
| 1849 | { |
| 1850 | struct gadget_info *gi = to_gadget_info(item); |
| 1851 | |
| 1852 | mutex_lock(&gi->lock); |
| 1853 | unregister_gadget(gi); |
| 1854 | mutex_unlock(&gi->lock); |
| 1855 | } |
| 1856 | EXPORT_SYMBOL_GPL(unregister_gadget_item); |
| 1857 | |
| 1858 | static int __init gadget_cfs_init(void) |
| 1859 | { |
| 1860 | int ret; |
| 1861 | |
| 1862 | config_group_init(&gadget_subsys.su_group); |
| 1863 | |
| 1864 | ret = configfs_register_subsystem(&gadget_subsys); |
| 1865 | |
| 1866 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1867 | android_class = class_create(THIS_MODULE, "android_usb"); |
| 1868 | if (IS_ERR(android_class)) |
| 1869 | return PTR_ERR(android_class); |
| 1870 | #endif |
| 1871 | |
| 1872 | return ret; |
| 1873 | } |
| 1874 | module_init(gadget_cfs_init); |
| 1875 | |
| 1876 | static void __exit gadget_cfs_exit(void) |
| 1877 | { |
| 1878 | configfs_unregister_subsystem(&gadget_subsys); |
| 1879 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1880 | if (!IS_ERR(android_class)) |
| 1881 | class_destroy(android_class); |
| 1882 | #endif |
| 1883 | |
| 1884 | } |
| 1885 | module_exit(gadget_cfs_exit); |