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