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