Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [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/usb/composite.h> |
| 6 | #include <linux/usb/gadget_configfs.h> |
Rashika Kheria | 0009e99 | 2013-12-19 15:37:37 +0530 | [diff] [blame] | 7 | #include "configfs.h" |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 8 | |
| 9 | int check_user_usb_string(const char *name, |
| 10 | struct usb_gadget_strings *stringtab_dev) |
| 11 | { |
| 12 | unsigned primary_lang; |
| 13 | unsigned sub_lang; |
| 14 | u16 num; |
| 15 | int ret; |
| 16 | |
| 17 | ret = kstrtou16(name, 0, &num); |
| 18 | if (ret) |
| 19 | return ret; |
| 20 | |
| 21 | primary_lang = num & 0x3ff; |
| 22 | sub_lang = num >> 10; |
| 23 | |
| 24 | /* simple sanity check for valid langid */ |
| 25 | switch (primary_lang) { |
| 26 | case 0: |
| 27 | case 0x62 ... 0xfe: |
| 28 | case 0x100 ... 0x3ff: |
| 29 | return -EINVAL; |
| 30 | } |
| 31 | if (!sub_lang) |
| 32 | return -EINVAL; |
| 33 | |
| 34 | stringtab_dev->language = num; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | #define MAX_NAME_LEN 40 |
| 39 | #define MAX_USB_STRING_LANGS 2 |
| 40 | |
| 41 | struct gadget_info { |
| 42 | struct config_group group; |
| 43 | struct config_group functions_group; |
| 44 | struct config_group configs_group; |
| 45 | struct config_group strings_group; |
| 46 | struct config_group *default_groups[4]; |
| 47 | |
| 48 | struct mutex lock; |
| 49 | struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; |
| 50 | struct list_head string_list; |
| 51 | struct list_head available_func; |
| 52 | |
| 53 | const char *udc_name; |
| 54 | #ifdef CONFIG_USB_OTG |
| 55 | struct usb_otg_descriptor otg; |
| 56 | #endif |
| 57 | struct usb_composite_driver composite; |
| 58 | struct usb_composite_dev cdev; |
| 59 | }; |
| 60 | |
| 61 | struct config_usb_cfg { |
| 62 | struct config_group group; |
| 63 | struct config_group strings_group; |
| 64 | struct config_group *default_groups[2]; |
| 65 | struct list_head string_list; |
| 66 | struct usb_configuration c; |
| 67 | struct list_head func_list; |
| 68 | struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; |
| 69 | }; |
| 70 | |
| 71 | struct gadget_strings { |
| 72 | struct usb_gadget_strings stringtab_dev; |
| 73 | struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX]; |
| 74 | char *manufacturer; |
| 75 | char *product; |
| 76 | char *serialnumber; |
| 77 | |
| 78 | struct config_group group; |
| 79 | struct list_head list; |
| 80 | }; |
| 81 | |
| 82 | struct gadget_config_name { |
| 83 | struct usb_gadget_strings stringtab_dev; |
| 84 | struct usb_string strings; |
| 85 | char *configuration; |
| 86 | |
| 87 | struct config_group group; |
| 88 | struct list_head list; |
| 89 | }; |
| 90 | |
| 91 | static int usb_string_copy(const char *s, char **s_copy) |
| 92 | { |
| 93 | int ret; |
| 94 | char *str; |
| 95 | char *copy = *s_copy; |
| 96 | ret = strlen(s); |
| 97 | if (ret > 126) |
| 98 | return -EOVERFLOW; |
| 99 | |
| 100 | str = kstrdup(s, GFP_KERNEL); |
| 101 | if (!str) |
| 102 | return -ENOMEM; |
| 103 | if (str[ret - 1] == '\n') |
| 104 | str[ret - 1] = '\0'; |
| 105 | kfree(copy); |
| 106 | *s_copy = str; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | CONFIGFS_ATTR_STRUCT(gadget_info); |
| 111 | CONFIGFS_ATTR_STRUCT(config_usb_cfg); |
| 112 | |
| 113 | #define GI_DEVICE_DESC_ITEM_ATTR(name) \ |
| 114 | static struct gadget_info_attribute gadget_cdev_desc_##name = \ |
| 115 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ |
| 116 | gadget_dev_desc_##name##_show, \ |
| 117 | gadget_dev_desc_##name##_store) |
| 118 | |
| 119 | #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \ |
| 120 | static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \ |
| 121 | char *page) \ |
| 122 | { \ |
| 123 | return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \ |
| 124 | } |
| 125 | |
| 126 | #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \ |
| 127 | static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \ |
| 128 | char *page) \ |
| 129 | { \ |
| 130 | return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \ |
| 131 | } |
| 132 | |
| 133 | |
| 134 | #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \ |
| 135 | static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \ |
| 136 | const char *page, size_t len) \ |
| 137 | { \ |
| 138 | u8 val; \ |
| 139 | int ret; \ |
| 140 | ret = kstrtou8(page, 0, &val); \ |
| 141 | if (ret) \ |
| 142 | return ret; \ |
| 143 | gi->cdev.desc._name = val; \ |
| 144 | return len; \ |
| 145 | } |
| 146 | |
| 147 | #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \ |
| 148 | static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \ |
| 149 | const char *page, size_t len) \ |
| 150 | { \ |
| 151 | u16 val; \ |
| 152 | int ret; \ |
| 153 | ret = kstrtou16(page, 0, &val); \ |
| 154 | if (ret) \ |
| 155 | return ret; \ |
| 156 | gi->cdev.desc._name = cpu_to_le16p(&val); \ |
| 157 | return len; \ |
| 158 | } |
| 159 | |
| 160 | #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \ |
| 161 | GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \ |
| 162 | GI_DEVICE_DESC_SIMPLE_W_##_type(_name) |
| 163 | |
| 164 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB); |
| 165 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8); |
| 166 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8); |
| 167 | GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8); |
| 168 | GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8); |
| 169 | GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16); |
| 170 | GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16); |
| 171 | GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice); |
| 172 | |
| 173 | static ssize_t is_valid_bcd(u16 bcd_val) |
| 174 | { |
| 175 | if ((bcd_val & 0xf) > 9) |
| 176 | return -EINVAL; |
| 177 | if (((bcd_val >> 4) & 0xf) > 9) |
| 178 | return -EINVAL; |
| 179 | if (((bcd_val >> 8) & 0xf) > 9) |
| 180 | return -EINVAL; |
| 181 | if (((bcd_val >> 12) & 0xf) > 9) |
| 182 | return -EINVAL; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi, |
| 187 | const char *page, size_t len) |
| 188 | { |
| 189 | u16 bcdDevice; |
| 190 | int ret; |
| 191 | |
| 192 | ret = kstrtou16(page, 0, &bcdDevice); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | ret = is_valid_bcd(bcdDevice); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 200 | return len; |
| 201 | } |
| 202 | |
| 203 | static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi, |
| 204 | const char *page, size_t len) |
| 205 | { |
| 206 | u16 bcdUSB; |
| 207 | int ret; |
| 208 | |
| 209 | ret = kstrtou16(page, 0, &bcdUSB); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | ret = is_valid_bcd(bcdUSB); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | |
| 216 | gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB); |
| 217 | return len; |
| 218 | } |
| 219 | |
| 220 | static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page) |
| 221 | { |
| 222 | return sprintf(page, "%s\n", gi->udc_name ?: ""); |
| 223 | } |
| 224 | |
| 225 | static int unregister_gadget(struct gadget_info *gi) |
| 226 | { |
| 227 | int ret; |
| 228 | |
| 229 | if (!gi->udc_name) |
| 230 | return -ENODEV; |
| 231 | |
| 232 | ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver); |
| 233 | if (ret) |
| 234 | return ret; |
| 235 | kfree(gi->udc_name); |
| 236 | gi->udc_name = NULL; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi, |
| 241 | const char *page, size_t len) |
| 242 | { |
| 243 | char *name; |
| 244 | int ret; |
| 245 | |
| 246 | name = kstrdup(page, GFP_KERNEL); |
| 247 | if (!name) |
| 248 | return -ENOMEM; |
| 249 | if (name[len - 1] == '\n') |
| 250 | name[len - 1] = '\0'; |
| 251 | |
| 252 | mutex_lock(&gi->lock); |
| 253 | |
| 254 | if (!strlen(name)) { |
| 255 | ret = unregister_gadget(gi); |
| 256 | if (ret) |
| 257 | goto err; |
| 258 | } else { |
| 259 | if (gi->udc_name) { |
| 260 | ret = -EBUSY; |
| 261 | goto err; |
| 262 | } |
| 263 | ret = udc_attach_driver(name, &gi->composite.gadget_driver); |
| 264 | if (ret) |
| 265 | goto err; |
| 266 | gi->udc_name = name; |
| 267 | } |
| 268 | mutex_unlock(&gi->lock); |
| 269 | return len; |
| 270 | err: |
| 271 | kfree(name); |
| 272 | mutex_unlock(&gi->lock); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass); |
| 277 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass); |
| 278 | GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol); |
| 279 | GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0); |
| 280 | GI_DEVICE_DESC_ITEM_ATTR(idVendor); |
| 281 | GI_DEVICE_DESC_ITEM_ATTR(idProduct); |
| 282 | GI_DEVICE_DESC_ITEM_ATTR(bcdDevice); |
| 283 | GI_DEVICE_DESC_ITEM_ATTR(bcdUSB); |
| 284 | GI_DEVICE_DESC_ITEM_ATTR(UDC); |
| 285 | |
| 286 | static struct configfs_attribute *gadget_root_attrs[] = { |
| 287 | &gadget_cdev_desc_bDeviceClass.attr, |
| 288 | &gadget_cdev_desc_bDeviceSubClass.attr, |
| 289 | &gadget_cdev_desc_bDeviceProtocol.attr, |
| 290 | &gadget_cdev_desc_bMaxPacketSize0.attr, |
| 291 | &gadget_cdev_desc_idVendor.attr, |
| 292 | &gadget_cdev_desc_idProduct.attr, |
| 293 | &gadget_cdev_desc_bcdDevice.attr, |
| 294 | &gadget_cdev_desc_bcdUSB.attr, |
| 295 | &gadget_cdev_desc_UDC.attr, |
| 296 | NULL, |
| 297 | }; |
| 298 | |
| 299 | static inline struct gadget_info *to_gadget_info(struct config_item *item) |
| 300 | { |
| 301 | return container_of(to_config_group(item), struct gadget_info, group); |
| 302 | } |
| 303 | |
| 304 | static inline struct gadget_strings *to_gadget_strings(struct config_item *item) |
| 305 | { |
| 306 | return container_of(to_config_group(item), struct gadget_strings, |
| 307 | group); |
| 308 | } |
| 309 | |
| 310 | static inline struct gadget_config_name *to_gadget_config_name( |
| 311 | struct config_item *item) |
| 312 | { |
| 313 | return container_of(to_config_group(item), struct gadget_config_name, |
| 314 | group); |
| 315 | } |
| 316 | |
| 317 | static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) |
| 318 | { |
| 319 | return container_of(to_config_group(item), struct config_usb_cfg, |
| 320 | group); |
| 321 | } |
| 322 | |
| 323 | static inline struct usb_function_instance *to_usb_function_instance( |
| 324 | struct config_item *item) |
| 325 | { |
| 326 | return container_of(to_config_group(item), |
| 327 | struct usb_function_instance, group); |
| 328 | } |
| 329 | |
| 330 | static void gadget_info_attr_release(struct config_item *item) |
| 331 | { |
| 332 | struct gadget_info *gi = to_gadget_info(item); |
| 333 | |
| 334 | WARN_ON(!list_empty(&gi->cdev.configs)); |
| 335 | WARN_ON(!list_empty(&gi->string_list)); |
| 336 | WARN_ON(!list_empty(&gi->available_func)); |
| 337 | kfree(gi->composite.gadget_driver.function); |
| 338 | kfree(gi); |
| 339 | } |
| 340 | |
| 341 | CONFIGFS_ATTR_OPS(gadget_info); |
| 342 | |
| 343 | static struct configfs_item_operations gadget_root_item_ops = { |
| 344 | .release = gadget_info_attr_release, |
| 345 | .show_attribute = gadget_info_attr_show, |
| 346 | .store_attribute = gadget_info_attr_store, |
| 347 | }; |
| 348 | |
| 349 | static void gadget_config_attr_release(struct config_item *item) |
| 350 | { |
| 351 | struct config_usb_cfg *cfg = to_config_usb_cfg(item); |
| 352 | |
| 353 | WARN_ON(!list_empty(&cfg->c.functions)); |
| 354 | list_del(&cfg->c.list); |
| 355 | kfree(cfg->c.label); |
| 356 | kfree(cfg); |
| 357 | } |
| 358 | |
| 359 | static int config_usb_cfg_link( |
| 360 | struct config_item *usb_cfg_ci, |
| 361 | struct config_item *usb_func_ci) |
| 362 | { |
| 363 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); |
| 364 | struct usb_composite_dev *cdev = cfg->c.cdev; |
| 365 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); |
| 366 | |
| 367 | struct config_group *group = to_config_group(usb_func_ci); |
| 368 | struct usb_function_instance *fi = container_of(group, |
| 369 | struct usb_function_instance, group); |
| 370 | struct usb_function_instance *a_fi; |
| 371 | struct usb_function *f; |
| 372 | int ret; |
| 373 | |
| 374 | mutex_lock(&gi->lock); |
| 375 | /* |
| 376 | * Make sure this function is from within our _this_ gadget and not |
| 377 | * from another gadget or a random directory. |
| 378 | * Also a function instance can only be linked once. |
| 379 | */ |
| 380 | list_for_each_entry(a_fi, &gi->available_func, cfs_list) { |
| 381 | if (a_fi == fi) |
| 382 | break; |
| 383 | } |
| 384 | if (a_fi != fi) { |
| 385 | ret = -EINVAL; |
| 386 | goto out; |
| 387 | } |
| 388 | |
| 389 | list_for_each_entry(f, &cfg->func_list, list) { |
| 390 | if (f->fi == fi) { |
| 391 | ret = -EEXIST; |
| 392 | goto out; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | f = usb_get_function(fi); |
| 397 | if (IS_ERR(f)) { |
| 398 | ret = PTR_ERR(f); |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | /* stash the function until we bind it to the gadget */ |
| 403 | list_add_tail(&f->list, &cfg->func_list); |
| 404 | ret = 0; |
| 405 | out: |
| 406 | mutex_unlock(&gi->lock); |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | static int config_usb_cfg_unlink( |
| 411 | struct config_item *usb_cfg_ci, |
| 412 | struct config_item *usb_func_ci) |
| 413 | { |
| 414 | struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); |
| 415 | struct usb_composite_dev *cdev = cfg->c.cdev; |
| 416 | struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); |
| 417 | |
| 418 | struct config_group *group = to_config_group(usb_func_ci); |
| 419 | struct usb_function_instance *fi = container_of(group, |
| 420 | struct usb_function_instance, group); |
| 421 | struct usb_function *f; |
| 422 | |
| 423 | /* |
| 424 | * ideally I would like to forbid to unlink functions while a gadget is |
| 425 | * bound to an UDC. Since this isn't possible at the moment, we simply |
| 426 | * force an unbind, the function is available here and then we can |
| 427 | * remove the function. |
| 428 | */ |
| 429 | mutex_lock(&gi->lock); |
| 430 | if (gi->udc_name) |
| 431 | unregister_gadget(gi); |
| 432 | WARN_ON(gi->udc_name); |
| 433 | |
| 434 | list_for_each_entry(f, &cfg->func_list, list) { |
| 435 | if (f->fi == fi) { |
| 436 | list_del(&f->list); |
| 437 | usb_put_function(f); |
| 438 | mutex_unlock(&gi->lock); |
| 439 | return 0; |
| 440 | } |
| 441 | } |
| 442 | mutex_unlock(&gi->lock); |
David Rientjes | 75bfe23 | 2013-04-07 14:11:47 -0700 | [diff] [blame] | 443 | WARN(1, "Unable to locate function to unbind\n"); |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | CONFIGFS_ATTR_OPS(config_usb_cfg); |
| 448 | |
| 449 | static struct configfs_item_operations gadget_config_item_ops = { |
| 450 | .release = gadget_config_attr_release, |
| 451 | .show_attribute = config_usb_cfg_attr_show, |
| 452 | .store_attribute = config_usb_cfg_attr_store, |
| 453 | .allow_link = config_usb_cfg_link, |
| 454 | .drop_link = config_usb_cfg_unlink, |
| 455 | }; |
| 456 | |
| 457 | |
| 458 | static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg, |
| 459 | char *page) |
| 460 | { |
| 461 | return sprintf(page, "%u\n", cfg->c.MaxPower); |
| 462 | } |
| 463 | |
| 464 | static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg, |
| 465 | const char *page, size_t len) |
| 466 | { |
| 467 | u16 val; |
| 468 | int ret; |
| 469 | ret = kstrtou16(page, 0, &val); |
| 470 | if (ret) |
| 471 | return ret; |
| 472 | if (DIV_ROUND_UP(val, 8) > 0xff) |
| 473 | return -ERANGE; |
| 474 | cfg->c.MaxPower = val; |
| 475 | return len; |
| 476 | } |
| 477 | |
| 478 | static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg, |
| 479 | char *page) |
| 480 | { |
| 481 | return sprintf(page, "0x%02x\n", cfg->c.bmAttributes); |
| 482 | } |
| 483 | |
| 484 | static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg, |
| 485 | const char *page, size_t len) |
| 486 | { |
| 487 | u8 val; |
| 488 | int ret; |
| 489 | ret = kstrtou8(page, 0, &val); |
| 490 | if (ret) |
| 491 | return ret; |
| 492 | if (!(val & USB_CONFIG_ATT_ONE)) |
| 493 | return -EINVAL; |
| 494 | if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | |
| 495 | USB_CONFIG_ATT_WAKEUP)) |
| 496 | return -EINVAL; |
| 497 | cfg->c.bmAttributes = val; |
| 498 | return len; |
| 499 | } |
| 500 | |
| 501 | #define CFG_CONFIG_DESC_ITEM_ATTR(name) \ |
| 502 | static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \ |
| 503 | __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \ |
| 504 | gadget_config_desc_##name##_show, \ |
| 505 | gadget_config_desc_##name##_store) |
| 506 | |
| 507 | CFG_CONFIG_DESC_ITEM_ATTR(MaxPower); |
| 508 | CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes); |
| 509 | |
| 510 | static struct configfs_attribute *gadget_config_attrs[] = { |
| 511 | &gadget_usb_cfg_MaxPower.attr, |
| 512 | &gadget_usb_cfg_bmAttributes.attr, |
| 513 | NULL, |
| 514 | }; |
| 515 | |
| 516 | static struct config_item_type gadget_config_type = { |
| 517 | .ct_item_ops = &gadget_config_item_ops, |
| 518 | .ct_attrs = gadget_config_attrs, |
| 519 | .ct_owner = THIS_MODULE, |
| 520 | }; |
| 521 | |
| 522 | static struct config_item_type gadget_root_type = { |
| 523 | .ct_item_ops = &gadget_root_item_ops, |
| 524 | .ct_attrs = gadget_root_attrs, |
| 525 | .ct_owner = THIS_MODULE, |
| 526 | }; |
| 527 | |
| 528 | static void composite_init_dev(struct usb_composite_dev *cdev) |
| 529 | { |
| 530 | spin_lock_init(&cdev->lock); |
| 531 | INIT_LIST_HEAD(&cdev->configs); |
| 532 | INIT_LIST_HEAD(&cdev->gstrings); |
| 533 | } |
| 534 | |
| 535 | static struct config_group *function_make( |
| 536 | struct config_group *group, |
| 537 | const char *name) |
| 538 | { |
| 539 | struct gadget_info *gi; |
| 540 | struct usb_function_instance *fi; |
| 541 | char buf[MAX_NAME_LEN]; |
| 542 | char *func_name; |
| 543 | char *instance_name; |
| 544 | int ret; |
| 545 | |
| 546 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); |
| 547 | if (ret >= MAX_NAME_LEN) |
| 548 | return ERR_PTR(-ENAMETOOLONG); |
| 549 | |
| 550 | func_name = buf; |
| 551 | instance_name = strchr(func_name, '.'); |
| 552 | if (!instance_name) { |
| 553 | pr_err("Unable to locate . in FUNC.INSTANCE\n"); |
| 554 | return ERR_PTR(-EINVAL); |
| 555 | } |
| 556 | *instance_name = '\0'; |
| 557 | instance_name++; |
| 558 | |
| 559 | fi = usb_get_function_instance(func_name); |
| 560 | if (IS_ERR(fi)) |
Duan Jiong | a346941 | 2013-09-26 15:55:25 +0800 | [diff] [blame] | 561 | return ERR_CAST(fi); |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 562 | |
| 563 | ret = config_item_set_name(&fi->group.cg_item, name); |
| 564 | if (ret) { |
| 565 | usb_put_function_instance(fi); |
| 566 | return ERR_PTR(ret); |
| 567 | } |
Andrzej Pietrasiewicz | 1933861 | 2013-12-03 15:15:21 +0100 | [diff] [blame] | 568 | if (fi->set_inst_name) { |
| 569 | ret = fi->set_inst_name(fi, instance_name); |
| 570 | if (ret) { |
| 571 | usb_put_function_instance(fi); |
| 572 | return ERR_PTR(ret); |
| 573 | } |
| 574 | } |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 575 | |
| 576 | gi = container_of(group, struct gadget_info, functions_group); |
| 577 | |
| 578 | mutex_lock(&gi->lock); |
| 579 | list_add_tail(&fi->cfs_list, &gi->available_func); |
| 580 | mutex_unlock(&gi->lock); |
| 581 | return &fi->group; |
| 582 | } |
| 583 | |
| 584 | static void function_drop( |
| 585 | struct config_group *group, |
| 586 | struct config_item *item) |
| 587 | { |
| 588 | struct usb_function_instance *fi = to_usb_function_instance(item); |
| 589 | struct gadget_info *gi; |
| 590 | |
| 591 | gi = container_of(group, struct gadget_info, functions_group); |
| 592 | |
| 593 | mutex_lock(&gi->lock); |
| 594 | list_del(&fi->cfs_list); |
| 595 | mutex_unlock(&gi->lock); |
| 596 | config_item_put(item); |
| 597 | } |
| 598 | |
| 599 | static struct configfs_group_operations functions_ops = { |
| 600 | .make_group = &function_make, |
| 601 | .drop_item = &function_drop, |
| 602 | }; |
| 603 | |
| 604 | static struct config_item_type functions_type = { |
| 605 | .ct_group_ops = &functions_ops, |
| 606 | .ct_owner = THIS_MODULE, |
| 607 | }; |
| 608 | |
| 609 | CONFIGFS_ATTR_STRUCT(gadget_config_name); |
| 610 | GS_STRINGS_RW(gadget_config_name, configuration); |
| 611 | |
| 612 | static struct configfs_attribute *gadget_config_name_langid_attrs[] = { |
| 613 | &gadget_config_name_configuration.attr, |
| 614 | NULL, |
| 615 | }; |
| 616 | |
| 617 | static void gadget_config_name_attr_release(struct config_item *item) |
| 618 | { |
| 619 | struct gadget_config_name *cn = to_gadget_config_name(item); |
| 620 | |
| 621 | kfree(cn->configuration); |
| 622 | |
| 623 | list_del(&cn->list); |
| 624 | kfree(cn); |
| 625 | } |
| 626 | |
| 627 | USB_CONFIG_STRING_RW_OPS(gadget_config_name); |
| 628 | USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg); |
| 629 | |
| 630 | static struct config_group *config_desc_make( |
| 631 | struct config_group *group, |
| 632 | const char *name) |
| 633 | { |
| 634 | struct gadget_info *gi; |
| 635 | struct config_usb_cfg *cfg; |
| 636 | char buf[MAX_NAME_LEN]; |
| 637 | char *num_str; |
| 638 | u8 num; |
| 639 | int ret; |
| 640 | |
| 641 | gi = container_of(group, struct gadget_info, configs_group); |
| 642 | ret = snprintf(buf, MAX_NAME_LEN, "%s", name); |
| 643 | if (ret >= MAX_NAME_LEN) |
| 644 | return ERR_PTR(-ENAMETOOLONG); |
| 645 | |
| 646 | num_str = strchr(buf, '.'); |
| 647 | if (!num_str) { |
| 648 | pr_err("Unable to locate . in name.bConfigurationValue\n"); |
| 649 | return ERR_PTR(-EINVAL); |
| 650 | } |
| 651 | |
| 652 | *num_str = '\0'; |
| 653 | num_str++; |
| 654 | |
| 655 | if (!strlen(buf)) |
| 656 | return ERR_PTR(-EINVAL); |
| 657 | |
| 658 | ret = kstrtou8(num_str, 0, &num); |
| 659 | if (ret) |
| 660 | return ERR_PTR(ret); |
| 661 | |
| 662 | cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); |
| 663 | if (!cfg) |
| 664 | return ERR_PTR(-ENOMEM); |
| 665 | cfg->c.label = kstrdup(buf, GFP_KERNEL); |
| 666 | if (!cfg->c.label) { |
| 667 | ret = -ENOMEM; |
| 668 | goto err; |
| 669 | } |
| 670 | cfg->c.bConfigurationValue = num; |
| 671 | cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW; |
| 672 | cfg->c.bmAttributes = USB_CONFIG_ATT_ONE; |
| 673 | INIT_LIST_HEAD(&cfg->string_list); |
| 674 | INIT_LIST_HEAD(&cfg->func_list); |
| 675 | |
| 676 | cfg->group.default_groups = cfg->default_groups; |
| 677 | cfg->default_groups[0] = &cfg->strings_group; |
| 678 | |
| 679 | config_group_init_type_name(&cfg->group, name, |
| 680 | &gadget_config_type); |
| 681 | config_group_init_type_name(&cfg->strings_group, "strings", |
| 682 | &gadget_config_name_strings_type); |
| 683 | |
| 684 | ret = usb_add_config_only(&gi->cdev, &cfg->c); |
| 685 | if (ret) |
| 686 | goto err; |
| 687 | |
| 688 | return &cfg->group; |
| 689 | err: |
| 690 | kfree(cfg->c.label); |
| 691 | kfree(cfg); |
| 692 | return ERR_PTR(ret); |
| 693 | } |
| 694 | |
| 695 | static void config_desc_drop( |
| 696 | struct config_group *group, |
| 697 | struct config_item *item) |
| 698 | { |
| 699 | config_item_put(item); |
| 700 | } |
| 701 | |
| 702 | static struct configfs_group_operations config_desc_ops = { |
| 703 | .make_group = &config_desc_make, |
| 704 | .drop_item = &config_desc_drop, |
| 705 | }; |
| 706 | |
| 707 | static struct config_item_type config_desc_type = { |
| 708 | .ct_group_ops = &config_desc_ops, |
| 709 | .ct_owner = THIS_MODULE, |
| 710 | }; |
| 711 | |
| 712 | CONFIGFS_ATTR_STRUCT(gadget_strings); |
| 713 | GS_STRINGS_RW(gadget_strings, manufacturer); |
| 714 | GS_STRINGS_RW(gadget_strings, product); |
| 715 | GS_STRINGS_RW(gadget_strings, serialnumber); |
| 716 | |
| 717 | static struct configfs_attribute *gadget_strings_langid_attrs[] = { |
| 718 | &gadget_strings_manufacturer.attr, |
| 719 | &gadget_strings_product.attr, |
| 720 | &gadget_strings_serialnumber.attr, |
| 721 | NULL, |
| 722 | }; |
| 723 | |
| 724 | static void gadget_strings_attr_release(struct config_item *item) |
| 725 | { |
| 726 | struct gadget_strings *gs = to_gadget_strings(item); |
| 727 | |
| 728 | kfree(gs->manufacturer); |
| 729 | kfree(gs->product); |
| 730 | kfree(gs->serialnumber); |
| 731 | |
| 732 | list_del(&gs->list); |
| 733 | kfree(gs); |
| 734 | } |
| 735 | |
| 736 | USB_CONFIG_STRING_RW_OPS(gadget_strings); |
| 737 | USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info); |
| 738 | |
| 739 | static int configfs_do_nothing(struct usb_composite_dev *cdev) |
| 740 | { |
David Rientjes | 75bfe23 | 2013-04-07 14:11:47 -0700 | [diff] [blame] | 741 | WARN_ON(1); |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 742 | return -EINVAL; |
| 743 | } |
| 744 | |
| 745 | int composite_dev_prepare(struct usb_composite_driver *composite, |
| 746 | struct usb_composite_dev *dev); |
| 747 | |
| 748 | static void purge_configs_funcs(struct gadget_info *gi) |
| 749 | { |
| 750 | struct usb_configuration *c; |
| 751 | |
| 752 | list_for_each_entry(c, &gi->cdev.configs, list) { |
| 753 | struct usb_function *f, *tmp; |
| 754 | struct config_usb_cfg *cfg; |
| 755 | |
| 756 | cfg = container_of(c, struct config_usb_cfg, c); |
| 757 | |
| 758 | list_for_each_entry_safe(f, tmp, &c->functions, list) { |
| 759 | |
| 760 | list_move_tail(&f->list, &cfg->func_list); |
| 761 | if (f->unbind) { |
| 762 | dev_err(&gi->cdev.gadget->dev, "unbind function" |
| 763 | " '%s'/%p\n", f->name, f); |
| 764 | f->unbind(c, f); |
| 765 | } |
| 766 | } |
| 767 | c->next_interface_id = 0; |
| 768 | c->superspeed = 0; |
| 769 | c->highspeed = 0; |
| 770 | c->fullspeed = 0; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | static int configfs_composite_bind(struct usb_gadget *gadget, |
| 775 | struct usb_gadget_driver *gdriver) |
| 776 | { |
| 777 | struct usb_composite_driver *composite = to_cdriver(gdriver); |
| 778 | struct gadget_info *gi = container_of(composite, |
| 779 | struct gadget_info, composite); |
| 780 | struct usb_composite_dev *cdev = &gi->cdev; |
| 781 | struct usb_configuration *c; |
| 782 | struct usb_string *s; |
| 783 | unsigned i; |
| 784 | int ret; |
| 785 | |
| 786 | /* the gi->lock is hold by the caller */ |
| 787 | cdev->gadget = gadget; |
| 788 | set_gadget_data(gadget, cdev); |
| 789 | ret = composite_dev_prepare(composite, cdev); |
| 790 | if (ret) |
| 791 | return ret; |
| 792 | /* and now the gadget bind */ |
| 793 | ret = -EINVAL; |
| 794 | |
| 795 | if (list_empty(&gi->cdev.configs)) { |
| 796 | pr_err("Need atleast one configuration in %s.\n", |
| 797 | gi->composite.name); |
| 798 | goto err_comp_cleanup; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | list_for_each_entry(c, &gi->cdev.configs, list) { |
| 803 | struct config_usb_cfg *cfg; |
| 804 | |
| 805 | cfg = container_of(c, struct config_usb_cfg, c); |
| 806 | if (list_empty(&cfg->func_list)) { |
| 807 | pr_err("Config %s/%d of %s needs atleast one function.\n", |
| 808 | c->label, c->bConfigurationValue, |
| 809 | gi->composite.name); |
| 810 | goto err_comp_cleanup; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* init all strings */ |
| 815 | if (!list_empty(&gi->string_list)) { |
| 816 | struct gadget_strings *gs; |
| 817 | |
| 818 | i = 0; |
| 819 | list_for_each_entry(gs, &gi->string_list, list) { |
| 820 | |
| 821 | gi->gstrings[i] = &gs->stringtab_dev; |
| 822 | gs->stringtab_dev.strings = gs->strings; |
| 823 | gs->strings[USB_GADGET_MANUFACTURER_IDX].s = |
| 824 | gs->manufacturer; |
| 825 | gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product; |
| 826 | gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber; |
| 827 | i++; |
| 828 | } |
| 829 | gi->gstrings[i] = NULL; |
| 830 | s = usb_gstrings_attach(&gi->cdev, gi->gstrings, |
| 831 | USB_GADGET_FIRST_AVAIL_IDX); |
Wei Yongjun | fea7707 | 2013-05-07 19:50:31 +0800 | [diff] [blame] | 832 | if (IS_ERR(s)) { |
| 833 | ret = PTR_ERR(s); |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 834 | goto err_comp_cleanup; |
Wei Yongjun | fea7707 | 2013-05-07 19:50:31 +0800 | [diff] [blame] | 835 | } |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 836 | |
| 837 | gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id; |
| 838 | gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id; |
| 839 | gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id; |
| 840 | } |
| 841 | |
| 842 | /* Go through all configs, attach all functions */ |
| 843 | list_for_each_entry(c, &gi->cdev.configs, list) { |
| 844 | struct config_usb_cfg *cfg; |
| 845 | struct usb_function *f; |
| 846 | struct usb_function *tmp; |
| 847 | struct gadget_config_name *cn; |
| 848 | |
| 849 | cfg = container_of(c, struct config_usb_cfg, c); |
| 850 | if (!list_empty(&cfg->string_list)) { |
| 851 | i = 0; |
| 852 | list_for_each_entry(cn, &cfg->string_list, list) { |
| 853 | cfg->gstrings[i] = &cn->stringtab_dev; |
| 854 | cn->stringtab_dev.strings = &cn->strings; |
| 855 | cn->strings.s = cn->configuration; |
| 856 | i++; |
| 857 | } |
| 858 | cfg->gstrings[i] = NULL; |
| 859 | s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1); |
Wei Yongjun | fea7707 | 2013-05-07 19:50:31 +0800 | [diff] [blame] | 860 | if (IS_ERR(s)) { |
| 861 | ret = PTR_ERR(s); |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 862 | goto err_comp_cleanup; |
Wei Yongjun | fea7707 | 2013-05-07 19:50:31 +0800 | [diff] [blame] | 863 | } |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 864 | c->iConfiguration = s[0].id; |
| 865 | } |
| 866 | |
| 867 | list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { |
| 868 | list_del(&f->list); |
| 869 | ret = usb_add_function(c, f); |
Andrzej Pietrasiewicz | 5a68e9b | 2013-08-08 09:43:28 +0200 | [diff] [blame] | 870 | if (ret) { |
| 871 | list_add(&f->list, &cfg->func_list); |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 872 | goto err_purge_funcs; |
Andrzej Pietrasiewicz | 5a68e9b | 2013-08-08 09:43:28 +0200 | [diff] [blame] | 873 | } |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 874 | } |
| 875 | usb_ep_autoconfig_reset(cdev->gadget); |
| 876 | } |
| 877 | usb_ep_autoconfig_reset(cdev->gadget); |
| 878 | return 0; |
| 879 | |
| 880 | err_purge_funcs: |
| 881 | purge_configs_funcs(gi); |
| 882 | err_comp_cleanup: |
| 883 | composite_dev_cleanup(cdev); |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | static void configfs_composite_unbind(struct usb_gadget *gadget) |
| 888 | { |
| 889 | struct usb_composite_dev *cdev; |
| 890 | struct gadget_info *gi; |
| 891 | |
| 892 | /* the gi->lock is hold by the caller */ |
| 893 | |
| 894 | cdev = get_gadget_data(gadget); |
| 895 | gi = container_of(cdev, struct gadget_info, cdev); |
| 896 | |
| 897 | purge_configs_funcs(gi); |
| 898 | composite_dev_cleanup(cdev); |
| 899 | usb_ep_autoconfig_reset(cdev->gadget); |
| 900 | cdev->gadget = NULL; |
| 901 | set_gadget_data(gadget, NULL); |
| 902 | } |
| 903 | |
| 904 | static const struct usb_gadget_driver configfs_driver_template = { |
| 905 | .bind = configfs_composite_bind, |
| 906 | .unbind = configfs_composite_unbind, |
| 907 | |
| 908 | .setup = composite_setup, |
| 909 | .disconnect = composite_disconnect, |
| 910 | |
| 911 | .max_speed = USB_SPEED_SUPER, |
| 912 | .driver = { |
| 913 | .owner = THIS_MODULE, |
| 914 | .name = "configfs-gadget", |
| 915 | }, |
| 916 | }; |
| 917 | |
| 918 | static struct config_group *gadgets_make( |
| 919 | struct config_group *group, |
| 920 | const char *name) |
| 921 | { |
| 922 | struct gadget_info *gi; |
| 923 | |
| 924 | gi = kzalloc(sizeof(*gi), GFP_KERNEL); |
| 925 | if (!gi) |
| 926 | return ERR_PTR(-ENOMEM); |
| 927 | |
| 928 | gi->group.default_groups = gi->default_groups; |
| 929 | gi->group.default_groups[0] = &gi->functions_group; |
| 930 | gi->group.default_groups[1] = &gi->configs_group; |
| 931 | gi->group.default_groups[2] = &gi->strings_group; |
| 932 | |
| 933 | config_group_init_type_name(&gi->functions_group, "functions", |
| 934 | &functions_type); |
| 935 | config_group_init_type_name(&gi->configs_group, "configs", |
| 936 | &config_desc_type); |
| 937 | config_group_init_type_name(&gi->strings_group, "strings", |
| 938 | &gadget_strings_strings_type); |
| 939 | |
| 940 | gi->composite.bind = configfs_do_nothing; |
| 941 | gi->composite.unbind = configfs_do_nothing; |
| 942 | gi->composite.suspend = NULL; |
| 943 | gi->composite.resume = NULL; |
| 944 | gi->composite.max_speed = USB_SPEED_SUPER; |
| 945 | |
| 946 | mutex_init(&gi->lock); |
| 947 | INIT_LIST_HEAD(&gi->string_list); |
| 948 | INIT_LIST_HEAD(&gi->available_func); |
| 949 | |
| 950 | composite_init_dev(&gi->cdev); |
| 951 | gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE; |
| 952 | gi->cdev.desc.bDescriptorType = USB_DT_DEVICE; |
| 953 | gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice()); |
| 954 | |
| 955 | gi->composite.gadget_driver = configfs_driver_template; |
| 956 | |
| 957 | gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL); |
| 958 | gi->composite.name = gi->composite.gadget_driver.function; |
| 959 | |
| 960 | if (!gi->composite.gadget_driver.function) |
| 961 | goto err; |
| 962 | |
| 963 | #ifdef CONFIG_USB_OTG |
| 964 | gi->otg.bLength = sizeof(struct usb_otg_descriptor); |
| 965 | gi->otg.bDescriptorType = USB_DT_OTG; |
| 966 | gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP; |
| 967 | #endif |
| 968 | |
| 969 | config_group_init_type_name(&gi->group, name, |
| 970 | &gadget_root_type); |
| 971 | return &gi->group; |
| 972 | err: |
| 973 | kfree(gi); |
| 974 | return ERR_PTR(-ENOMEM); |
| 975 | } |
| 976 | |
| 977 | static void gadgets_drop(struct config_group *group, struct config_item *item) |
| 978 | { |
| 979 | config_item_put(item); |
| 980 | } |
| 981 | |
| 982 | static struct configfs_group_operations gadgets_ops = { |
| 983 | .make_group = &gadgets_make, |
| 984 | .drop_item = &gadgets_drop, |
| 985 | }; |
| 986 | |
| 987 | static struct config_item_type gadgets_type = { |
| 988 | .ct_group_ops = &gadgets_ops, |
| 989 | .ct_owner = THIS_MODULE, |
| 990 | }; |
| 991 | |
| 992 | static struct configfs_subsystem gadget_subsys = { |
| 993 | .su_group = { |
| 994 | .cg_item = { |
| 995 | .ci_namebuf = "usb_gadget", |
| 996 | .ci_type = &gadgets_type, |
| 997 | }, |
| 998 | }, |
| 999 | .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex), |
| 1000 | }; |
| 1001 | |
Andrzej Pietrasiewicz | 092a4bd | 2013-09-26 14:38:15 +0200 | [diff] [blame] | 1002 | void unregister_gadget_item(struct config_item *item) |
| 1003 | { |
| 1004 | struct gadget_info *gi = to_gadget_info(item); |
| 1005 | |
| 1006 | unregister_gadget(gi); |
| 1007 | } |
| 1008 | EXPORT_SYMBOL(unregister_gadget_item); |
| 1009 | |
Sebastian Andrzej Siewior | 88af8bb | 2012-12-23 21:10:24 +0100 | [diff] [blame] | 1010 | static int __init gadget_cfs_init(void) |
| 1011 | { |
| 1012 | int ret; |
| 1013 | |
| 1014 | config_group_init(&gadget_subsys.su_group); |
| 1015 | |
| 1016 | ret = configfs_register_subsystem(&gadget_subsys); |
| 1017 | return ret; |
| 1018 | } |
| 1019 | module_init(gadget_cfs_init); |
| 1020 | |
| 1021 | static void __exit gadget_cfs_exit(void) |
| 1022 | { |
| 1023 | configfs_unregister_subsystem(&gadget_subsys); |
| 1024 | } |
| 1025 | module_exit(gadget_cfs_exit); |