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