1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_obex.c -- USB CDC OBEX function driver 4 * 5 * Copyright (C) 2008 Nokia Corporation 6 * Contact: Felipe Balbi <felipe.balbi@nokia.com> 7 * 8 * Based on f_acm.c by Al Borchers and David Brownell. 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 */ 15 16 /* #define VERBOSE_DEBUG */ 17 18 #include <linux/slab.h> 19 #include <linux/kernel.h> 20 #include <linux/device.h> 21 #include <linux/module.h> 22 23 #include "u_serial.h" 24 25 26 /* 27 * This CDC OBEX function support just packages a TTY-ish byte stream. 28 * A user mode server will put it into "raw" mode and handle all the 29 * relevant protocol details ... this is just a kernel passthrough. 30 * When possible, we prevent gadget enumeration until that server is 31 * ready to handle the commands. 32 */ 33 34 struct f_obex { 35 struct gserial port; 36 u8 ctrl_id; 37 u8 data_id; 38 u8 cur_alt; 39 u8 port_num; 40 }; 41 42 static inline struct f_obex *func_to_obex(struct usb_function *f) 43 { 44 return container_of(f, struct f_obex, port.func); 45 } 46 47 static inline struct f_obex *port_to_obex(struct gserial *p) 48 { 49 return container_of(p, struct f_obex, port); 50 } 51 52 /*-------------------------------------------------------------------------*/ 53 54 #define OBEX_CTRL_IDX 0 55 #define OBEX_DATA_IDX 1 56 57 static struct usb_string obex_string_defs[] = { 58 [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)", 59 [OBEX_DATA_IDX].s = "CDC OBEX Data", 60 { }, /* end of list */ 61 }; 62 63 static struct usb_gadget_strings obex_string_table = { 64 .language = 0x0409, /* en-US */ 65 .strings = obex_string_defs, 66 }; 67 68 static struct usb_gadget_strings *obex_strings[] = { 69 &obex_string_table, 70 NULL, 71 }; 72 73 /*-------------------------------------------------------------------------*/ 74 75 static struct usb_interface_descriptor obex_control_intf = { 76 .bLength = sizeof(obex_control_intf), 77 .bDescriptorType = USB_DT_INTERFACE, 78 .bInterfaceNumber = 0, 79 80 .bAlternateSetting = 0, 81 .bNumEndpoints = 0, 82 .bInterfaceClass = USB_CLASS_COMM, 83 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX, 84 }; 85 86 static struct usb_interface_descriptor obex_data_nop_intf = { 87 .bLength = sizeof(obex_data_nop_intf), 88 .bDescriptorType = USB_DT_INTERFACE, 89 .bInterfaceNumber = 1, 90 91 .bAlternateSetting = 0, 92 .bNumEndpoints = 0, 93 .bInterfaceClass = USB_CLASS_CDC_DATA, 94 }; 95 96 static struct usb_interface_descriptor obex_data_intf = { 97 .bLength = sizeof(obex_data_intf), 98 .bDescriptorType = USB_DT_INTERFACE, 99 .bInterfaceNumber = 2, 100 101 .bAlternateSetting = 1, 102 .bNumEndpoints = 2, 103 .bInterfaceClass = USB_CLASS_CDC_DATA, 104 }; 105 106 static struct usb_cdc_header_desc obex_cdc_header_desc = { 107 .bLength = sizeof(obex_cdc_header_desc), 108 .bDescriptorType = USB_DT_CS_INTERFACE, 109 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 110 .bcdCDC = cpu_to_le16(0x0120), 111 }; 112 113 static struct usb_cdc_union_desc obex_cdc_union_desc = { 114 .bLength = sizeof(obex_cdc_union_desc), 115 .bDescriptorType = USB_DT_CS_INTERFACE, 116 .bDescriptorSubType = USB_CDC_UNION_TYPE, 117 .bMasterInterface0 = 1, 118 .bSlaveInterface0 = 2, 119 }; 120 121 static struct usb_cdc_obex_desc obex_desc = { 122 .bLength = sizeof(obex_desc), 123 .bDescriptorType = USB_DT_CS_INTERFACE, 124 .bDescriptorSubType = USB_CDC_OBEX_TYPE, 125 .bcdVersion = cpu_to_le16(0x0100), 126 }; 127 128 /* High-Speed Support */ 129 130 static struct usb_endpoint_descriptor obex_hs_ep_out_desc = { 131 .bLength = USB_DT_ENDPOINT_SIZE, 132 .bDescriptorType = USB_DT_ENDPOINT, 133 134 .bEndpointAddress = USB_DIR_OUT, 135 .bmAttributes = USB_ENDPOINT_XFER_BULK, 136 .wMaxPacketSize = cpu_to_le16(512), 137 }; 138 139 static struct usb_endpoint_descriptor obex_hs_ep_in_desc = { 140 .bLength = USB_DT_ENDPOINT_SIZE, 141 .bDescriptorType = USB_DT_ENDPOINT, 142 143 .bEndpointAddress = USB_DIR_IN, 144 .bmAttributes = USB_ENDPOINT_XFER_BULK, 145 .wMaxPacketSize = cpu_to_le16(512), 146 }; 147 148 static struct usb_descriptor_header *hs_function[] = { 149 (struct usb_descriptor_header *) &obex_control_intf, 150 (struct usb_descriptor_header *) &obex_cdc_header_desc, 151 (struct usb_descriptor_header *) &obex_desc, 152 (struct usb_descriptor_header *) &obex_cdc_union_desc, 153 154 (struct usb_descriptor_header *) &obex_data_nop_intf, 155 (struct usb_descriptor_header *) &obex_data_intf, 156 (struct usb_descriptor_header *) &obex_hs_ep_in_desc, 157 (struct usb_descriptor_header *) &obex_hs_ep_out_desc, 158 NULL, 159 }; 160 161 /* Full-Speed Support */ 162 163 static struct usb_endpoint_descriptor obex_fs_ep_in_desc = { 164 .bLength = USB_DT_ENDPOINT_SIZE, 165 .bDescriptorType = USB_DT_ENDPOINT, 166 167 .bEndpointAddress = USB_DIR_IN, 168 .bmAttributes = USB_ENDPOINT_XFER_BULK, 169 }; 170 171 static struct usb_endpoint_descriptor obex_fs_ep_out_desc = { 172 .bLength = USB_DT_ENDPOINT_SIZE, 173 .bDescriptorType = USB_DT_ENDPOINT, 174 175 .bEndpointAddress = USB_DIR_OUT, 176 .bmAttributes = USB_ENDPOINT_XFER_BULK, 177 }; 178 179 static struct usb_descriptor_header *fs_function[] = { 180 (struct usb_descriptor_header *) &obex_control_intf, 181 (struct usb_descriptor_header *) &obex_cdc_header_desc, 182 (struct usb_descriptor_header *) &obex_desc, 183 (struct usb_descriptor_header *) &obex_cdc_union_desc, 184 185 (struct usb_descriptor_header *) &obex_data_nop_intf, 186 (struct usb_descriptor_header *) &obex_data_intf, 187 (struct usb_descriptor_header *) &obex_fs_ep_in_desc, 188 (struct usb_descriptor_header *) &obex_fs_ep_out_desc, 189 NULL, 190 }; 191 192 /*-------------------------------------------------------------------------*/ 193 194 static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 195 { 196 struct f_obex *obex = func_to_obex(f); 197 struct usb_composite_dev *cdev = f->config->cdev; 198 199 if (intf == obex->ctrl_id) { 200 if (alt != 0) 201 goto fail; 202 /* NOP */ 203 dev_dbg(&cdev->gadget->dev, 204 "reset obex ttyGS%d control\n", obex->port_num); 205 206 } else if (intf == obex->data_id) { 207 if (alt > 1) 208 goto fail; 209 210 if (obex->port.in->enabled) { 211 dev_dbg(&cdev->gadget->dev, 212 "reset obex ttyGS%d\n", obex->port_num); 213 gserial_disconnect(&obex->port); 214 } 215 216 if (!obex->port.in->desc || !obex->port.out->desc) { 217 dev_dbg(&cdev->gadget->dev, 218 "init obex ttyGS%d\n", obex->port_num); 219 if (config_ep_by_speed(cdev->gadget, f, 220 obex->port.in) || 221 config_ep_by_speed(cdev->gadget, f, 222 obex->port.out)) { 223 obex->port.out->desc = NULL; 224 obex->port.in->desc = NULL; 225 goto fail; 226 } 227 } 228 229 if (alt == 1) { 230 dev_dbg(&cdev->gadget->dev, 231 "activate obex ttyGS%d\n", obex->port_num); 232 gserial_connect(&obex->port, obex->port_num); 233 } 234 235 } else 236 goto fail; 237 238 obex->cur_alt = alt; 239 240 return 0; 241 242 fail: 243 return -EINVAL; 244 } 245 246 static int obex_get_alt(struct usb_function *f, unsigned intf) 247 { 248 struct f_obex *obex = func_to_obex(f); 249 250 return obex->cur_alt; 251 } 252 253 static void obex_disable(struct usb_function *f) 254 { 255 struct f_obex *obex = func_to_obex(f); 256 struct usb_composite_dev *cdev = f->config->cdev; 257 258 dev_dbg(&cdev->gadget->dev, "obex ttyGS%d disable\n", obex->port_num); 259 gserial_disconnect(&obex->port); 260 } 261 262 /*-------------------------------------------------------------------------*/ 263 264 static void obex_connect(struct gserial *g) 265 { 266 struct f_obex *obex = port_to_obex(g); 267 struct usb_composite_dev *cdev = g->func.config->cdev; 268 int status; 269 270 status = usb_function_activate(&g->func); 271 if (status) 272 dev_dbg(&cdev->gadget->dev, 273 "obex ttyGS%d function activate --> %d\n", 274 obex->port_num, status); 275 } 276 277 static void obex_disconnect(struct gserial *g) 278 { 279 struct f_obex *obex = port_to_obex(g); 280 struct usb_composite_dev *cdev = g->func.config->cdev; 281 int status; 282 283 status = usb_function_deactivate(&g->func); 284 if (status) 285 dev_dbg(&cdev->gadget->dev, 286 "obex ttyGS%d function deactivate --> %d\n", 287 obex->port_num, status); 288 } 289 290 /*-------------------------------------------------------------------------*/ 291 292 /* Some controllers can't support CDC OBEX ... */ 293 static inline bool can_support_obex(struct usb_configuration *c) 294 { 295 /* Since the first interface is a NOP, we can ignore the 296 * issue of multi-interface support on most controllers. 297 * 298 * Altsettings are mandatory, however... 299 */ 300 if (!gadget_is_altset_supported(c->cdev->gadget)) 301 return false; 302 303 /* everything else is *probably* fine ... */ 304 return true; 305 } 306 307 static int obex_bind(struct usb_configuration *c, struct usb_function *f) 308 { 309 struct usb_composite_dev *cdev = c->cdev; 310 struct f_obex *obex = func_to_obex(f); 311 struct usb_string *us; 312 int status; 313 struct usb_ep *ep; 314 315 if (!can_support_obex(c)) 316 return -EINVAL; 317 318 us = usb_gstrings_attach(cdev, obex_strings, 319 ARRAY_SIZE(obex_string_defs)); 320 if (IS_ERR(us)) 321 return PTR_ERR(us); 322 obex_control_intf.iInterface = us[OBEX_CTRL_IDX].id; 323 obex_data_nop_intf.iInterface = us[OBEX_DATA_IDX].id; 324 obex_data_intf.iInterface = us[OBEX_DATA_IDX].id; 325 326 /* allocate instance-specific interface IDs, and patch descriptors */ 327 328 status = usb_interface_id(c, f); 329 if (status < 0) 330 goto fail; 331 obex->ctrl_id = status; 332 333 obex_control_intf.bInterfaceNumber = status; 334 obex_cdc_union_desc.bMasterInterface0 = status; 335 336 status = usb_interface_id(c, f); 337 if (status < 0) 338 goto fail; 339 obex->data_id = status; 340 341 obex_data_nop_intf.bInterfaceNumber = status; 342 obex_data_intf.bInterfaceNumber = status; 343 obex_cdc_union_desc.bSlaveInterface0 = status; 344 345 /* allocate instance-specific endpoints */ 346 347 status = -ENODEV; 348 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc); 349 if (!ep) 350 goto fail; 351 obex->port.in = ep; 352 353 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc); 354 if (!ep) 355 goto fail; 356 obex->port.out = ep; 357 358 /* support all relevant hardware speeds... we expect that when 359 * hardware is dual speed, all bulk-capable endpoints work at 360 * both speeds 361 */ 362 363 obex_hs_ep_in_desc.bEndpointAddress = 364 obex_fs_ep_in_desc.bEndpointAddress; 365 obex_hs_ep_out_desc.bEndpointAddress = 366 obex_fs_ep_out_desc.bEndpointAddress; 367 368 status = usb_assign_descriptors(f, fs_function, hs_function, NULL, 369 NULL); 370 if (status) 371 goto fail; 372 373 dev_dbg(&cdev->gadget->dev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n", 374 obex->port_num, 375 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", 376 obex->port.in->name, obex->port.out->name); 377 378 return 0; 379 380 fail: 381 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status); 382 383 return status; 384 } 385 386 static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item) 387 { 388 return container_of(to_config_group(item), struct f_serial_opts, 389 func_inst.group); 390 } 391 392 static void obex_attr_release(struct config_item *item) 393 { 394 struct f_serial_opts *opts = to_f_serial_opts(item); 395 396 usb_put_function_instance(&opts->func_inst); 397 } 398 399 static struct configfs_item_operations obex_item_ops = { 400 .release = obex_attr_release, 401 }; 402 403 static ssize_t f_obex_port_num_show(struct config_item *item, char *page) 404 { 405 return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num); 406 } 407 408 CONFIGFS_ATTR_RO(f_obex_, port_num); 409 410 static struct configfs_attribute *acm_attrs[] = { 411 &f_obex_attr_port_num, 412 NULL, 413 }; 414 415 static struct config_item_type obex_func_type = { 416 .ct_item_ops = &obex_item_ops, 417 .ct_attrs = acm_attrs, 418 .ct_owner = THIS_MODULE, 419 }; 420 421 static void obex_free_inst(struct usb_function_instance *f) 422 { 423 struct f_serial_opts *opts; 424 425 opts = container_of(f, struct f_serial_opts, func_inst); 426 gserial_free_line(opts->port_num); 427 kfree(opts); 428 } 429 430 static struct usb_function_instance *obex_alloc_inst(void) 431 { 432 struct f_serial_opts *opts; 433 int ret; 434 435 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 436 if (!opts) 437 return ERR_PTR(-ENOMEM); 438 439 opts->func_inst.free_func_inst = obex_free_inst; 440 ret = gserial_alloc_line(&opts->port_num); 441 if (ret) { 442 kfree(opts); 443 return ERR_PTR(ret); 444 } 445 config_group_init_type_name(&opts->func_inst.group, "", 446 &obex_func_type); 447 448 return &opts->func_inst; 449 } 450 451 static void obex_free(struct usb_function *f) 452 { 453 struct f_obex *obex; 454 455 obex = func_to_obex(f); 456 kfree(obex); 457 } 458 459 static void obex_unbind(struct usb_configuration *c, struct usb_function *f) 460 { 461 usb_free_all_descriptors(f); 462 } 463 464 static struct usb_function *obex_alloc(struct usb_function_instance *fi) 465 { 466 struct f_obex *obex; 467 struct f_serial_opts *opts; 468 469 /* allocate and initialize one new instance */ 470 obex = kzalloc(sizeof(*obex), GFP_KERNEL); 471 if (!obex) 472 return ERR_PTR(-ENOMEM); 473 474 opts = container_of(fi, struct f_serial_opts, func_inst); 475 476 obex->port_num = opts->port_num; 477 478 obex->port.connect = obex_connect; 479 obex->port.disconnect = obex_disconnect; 480 481 obex->port.func.name = "obex"; 482 /* descriptors are per-instance copies */ 483 obex->port.func.bind = obex_bind; 484 obex->port.func.unbind = obex_unbind; 485 obex->port.func.set_alt = obex_set_alt; 486 obex->port.func.get_alt = obex_get_alt; 487 obex->port.func.disable = obex_disable; 488 obex->port.func.free_func = obex_free; 489 obex->port.func.bind_deactivated = true; 490 491 return &obex->port.func; 492 } 493 494 DECLARE_USB_FUNCTION_INIT(obex, obex_alloc_inst, obex_alloc); 495 MODULE_AUTHOR("Felipe Balbi"); 496 MODULE_LICENSE("GPL"); 497