1/* 2 * f_acm.c -- USB CDC serial (ACM) function driver 3 * 4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) 5 * Copyright (C) 2008 by David Brownell 6 * Copyright (C) 2008 by Nokia Corporation 7 * Copyright (C) 2009 by Samsung Electronics 8 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com) 9 * 10 * This software is distributed under the terms of the GNU General 11 * Public License ("GPL") as published by the Free Software Foundation, 12 * either version 2 of that License or (at your option) any later version. 13 */ 14 15/* #define VERBOSE_DEBUG */ 16 17#include <linux/slab.h> 18#include <linux/kernel.h> 19#include <linux/device.h> 20 21#include "u_serial.h" 22#include "gadget_chips.h" 23 24 25/* 26 * This CDC ACM function support just wraps control functions and 27 * notifications around the generic serial-over-usb code. 28 * 29 * Because CDC ACM is standardized by the USB-IF, many host operating 30 * systems have drivers for it. Accordingly, ACM is the preferred 31 * interop solution for serial-port type connections. The control 32 * models are often not necessary, and in any case don't do much in 33 * this bare-bones implementation. 34 * 35 * Note that even MS-Windows has some support for ACM. However, that 36 * support is somewhat broken because when you use ACM in a composite 37 * device, having multiple interfaces confuses the poor OS. It doesn't 38 * seem to understand CDC Union descriptors. The new "association" 39 * descriptors (roughly equivalent to CDC Unions) may sometimes help. 40 */ 41 42struct acm_ep_descs { 43 struct usb_endpoint_descriptor *in; 44 struct usb_endpoint_descriptor *out; 45 struct usb_endpoint_descriptor *notify; 46}; 47 48struct f_acm { 49 struct gserial port; 50 u8 ctrl_id, data_id; 51 u8 port_num; 52 53 u8 pending; 54 55 /* lock is mostly for pending and notify_req ... they get accessed 56 * by callbacks both from tty (open/close/break) under its spinlock, 57 * and notify_req.complete() which can't use that lock. 58 */ 59 spinlock_t lock; 60 61 struct acm_ep_descs fs; 62 struct acm_ep_descs hs; 63 64 struct usb_ep *notify; 65 struct usb_endpoint_descriptor *notify_desc; 66 struct usb_request *notify_req; 67 68 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ 69 70 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */ 71 u16 port_handshake_bits; 72#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */ 73#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */ 74 75 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */ 76 u16 serial_state; 77#define ACM_CTRL_OVERRUN (1 << 6) 78#define ACM_CTRL_PARITY (1 << 5) 79#define ACM_CTRL_FRAMING (1 << 4) 80#define ACM_CTRL_RI (1 << 3) 81#define ACM_CTRL_BRK (1 << 2) 82#define ACM_CTRL_DSR (1 << 1) 83#define ACM_CTRL_DCD (1 << 0) 84}; 85 86static inline struct f_acm *func_to_acm(struct usb_function *f) 87{ 88 return container_of(f, struct f_acm, port.func); 89} 90 91static inline struct f_acm *port_to_acm(struct gserial *p) 92{ 93 return container_of(p, struct f_acm, port); 94} 95 96/*-------------------------------------------------------------------------*/ 97 98/* notification endpoint uses smallish and infrequent fixed-size messages */ 99 100#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ 101#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */ 102 103/* interface and class descriptors: */ 104 105static struct usb_interface_assoc_descriptor 106acm_iad_descriptor = { 107 .bLength = sizeof acm_iad_descriptor, 108 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 109 110 /* .bFirstInterface = DYNAMIC, */ 111 .bInterfaceCount = 2, // control + data 112 .bFunctionClass = USB_CLASS_COMM, 113 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM, 114 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER, 115 /* .iFunction = DYNAMIC */ 116}; 117 118 119static struct usb_interface_descriptor acm_control_interface_desc = { 120 .bLength = USB_DT_INTERFACE_SIZE, 121 .bDescriptorType = USB_DT_INTERFACE, 122 /* .bInterfaceNumber = DYNAMIC */ 123 .bNumEndpoints = 1, 124 .bInterfaceClass = USB_CLASS_COMM, 125 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, 126 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, 127 /* .iInterface = DYNAMIC */ 128}; 129 130static struct usb_interface_descriptor acm_data_interface_desc = { 131 .bLength = USB_DT_INTERFACE_SIZE, 132 .bDescriptorType = USB_DT_INTERFACE, 133 /* .bInterfaceNumber = DYNAMIC */ 134 .bNumEndpoints = 2, 135 .bInterfaceClass = USB_CLASS_CDC_DATA, 136 .bInterfaceSubClass = 0, 137 .bInterfaceProtocol = 0, 138 /* .iInterface = DYNAMIC */ 139}; 140 141static struct usb_cdc_header_desc acm_header_desc = { 142 .bLength = sizeof(acm_header_desc), 143 .bDescriptorType = USB_DT_CS_INTERFACE, 144 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 145 .bcdCDC = cpu_to_le16(0x0110), 146}; 147 148static struct usb_cdc_call_mgmt_descriptor 149acm_call_mgmt_descriptor = { 150 .bLength = sizeof(acm_call_mgmt_descriptor), 151 .bDescriptorType = USB_DT_CS_INTERFACE, 152 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, 153 .bmCapabilities = 0, 154 /* .bDataInterface = DYNAMIC */ 155}; 156 157static struct usb_cdc_acm_descriptor acm_descriptor = { 158 .bLength = sizeof(acm_descriptor), 159 .bDescriptorType = USB_DT_CS_INTERFACE, 160 .bDescriptorSubType = USB_CDC_ACM_TYPE, 161 .bmCapabilities = USB_CDC_CAP_LINE, 162}; 163 164static struct usb_cdc_union_desc acm_union_desc = { 165 .bLength = sizeof(acm_union_desc), 166 .bDescriptorType = USB_DT_CS_INTERFACE, 167 .bDescriptorSubType = USB_CDC_UNION_TYPE, 168 /* .bMasterInterface0 = DYNAMIC */ 169 /* .bSlaveInterface0 = DYNAMIC */ 170}; 171 172/* full speed support: */ 173 174static struct usb_endpoint_descriptor acm_fs_notify_desc = { 175 .bLength = USB_DT_ENDPOINT_SIZE, 176 .bDescriptorType = USB_DT_ENDPOINT, 177 .bEndpointAddress = USB_DIR_IN, 178 .bmAttributes = USB_ENDPOINT_XFER_INT, 179 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET), 180 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, 181}; 182 183static struct usb_endpoint_descriptor acm_fs_in_desc = { 184 .bLength = USB_DT_ENDPOINT_SIZE, 185 .bDescriptorType = USB_DT_ENDPOINT, 186 .bEndpointAddress = USB_DIR_IN, 187 .bmAttributes = USB_ENDPOINT_XFER_BULK, 188}; 189 190static struct usb_endpoint_descriptor acm_fs_out_desc = { 191 .bLength = USB_DT_ENDPOINT_SIZE, 192 .bDescriptorType = USB_DT_ENDPOINT, 193 .bEndpointAddress = USB_DIR_OUT, 194 .bmAttributes = USB_ENDPOINT_XFER_BULK, 195}; 196 197static struct usb_descriptor_header *acm_fs_function[] = { 198 (struct usb_descriptor_header *) &acm_iad_descriptor, 199 (struct usb_descriptor_header *) &acm_control_interface_desc, 200 (struct usb_descriptor_header *) &acm_header_desc, 201 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, 202 (struct usb_descriptor_header *) &acm_descriptor, 203 (struct usb_descriptor_header *) &acm_union_desc, 204 (struct usb_descriptor_header *) &acm_fs_notify_desc, 205 (struct usb_descriptor_header *) &acm_data_interface_desc, 206 (struct usb_descriptor_header *) &acm_fs_in_desc, 207 (struct usb_descriptor_header *) &acm_fs_out_desc, 208 NULL, 209}; 210 211/* high speed support: */ 212 213static struct usb_endpoint_descriptor acm_hs_notify_desc = { 214 .bLength = USB_DT_ENDPOINT_SIZE, 215 .bDescriptorType = USB_DT_ENDPOINT, 216 .bEndpointAddress = USB_DIR_IN, 217 .bmAttributes = USB_ENDPOINT_XFER_INT, 218 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET), 219 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, 220}; 221 222static struct usb_endpoint_descriptor acm_hs_in_desc = { 223 .bLength = USB_DT_ENDPOINT_SIZE, 224 .bDescriptorType = USB_DT_ENDPOINT, 225 .bmAttributes = USB_ENDPOINT_XFER_BULK, 226 .wMaxPacketSize = cpu_to_le16(512), 227}; 228 229static struct usb_endpoint_descriptor acm_hs_out_desc = { 230 .bLength = USB_DT_ENDPOINT_SIZE, 231 .bDescriptorType = USB_DT_ENDPOINT, 232 .bmAttributes = USB_ENDPOINT_XFER_BULK, 233 .wMaxPacketSize = cpu_to_le16(512), 234}; 235 236static struct usb_descriptor_header *acm_hs_function[] = { 237 (struct usb_descriptor_header *) &acm_iad_descriptor, 238 (struct usb_descriptor_header *) &acm_control_interface_desc, 239 (struct usb_descriptor_header *) &acm_header_desc, 240 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, 241 (struct usb_descriptor_header *) &acm_descriptor, 242 (struct usb_descriptor_header *) &acm_union_desc, 243 (struct usb_descriptor_header *) &acm_hs_notify_desc, 244 (struct usb_descriptor_header *) &acm_data_interface_desc, 245 (struct usb_descriptor_header *) &acm_hs_in_desc, 246 (struct usb_descriptor_header *) &acm_hs_out_desc, 247 NULL, 248}; 249 250/* string descriptors: */ 251 252#define ACM_CTRL_IDX 0 253#define ACM_DATA_IDX 1 254#define ACM_IAD_IDX 2 255 256/* static strings, in UTF-8 */ 257static struct usb_string acm_string_defs[] = { 258 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)", 259 [ACM_DATA_IDX].s = "CDC ACM Data", 260 [ACM_IAD_IDX ].s = "CDC Serial", 261 { /* ZEROES END LIST */ }, 262}; 263 264static struct usb_gadget_strings acm_string_table = { 265 .language = 0x0409, /* en-us */ 266 .strings = acm_string_defs, 267}; 268 269static struct usb_gadget_strings *acm_strings[] = { 270 &acm_string_table, 271 NULL, 272}; 273 274/*-------------------------------------------------------------------------*/ 275 276/* ACM control ... data handling is delegated to tty library code. 277 * The main task of this function is to activate and deactivate 278 * that code based on device state; track parameters like line 279 * speed, handshake state, and so on; and issue notifications. 280 */ 281 282static void acm_complete_set_line_coding(struct usb_ep *ep, 283 struct usb_request *req) 284{ 285 struct f_acm *acm = ep->driver_data; 286 struct usb_composite_dev *cdev = acm->port.func.config->cdev; 287 288 if (req->status != 0) { 289 DBG(cdev, "acm ttyGS%d completion, err %d\n", 290 acm->port_num, req->status); 291 return; 292 } 293 294 /* normal completion */ 295 if (req->actual != sizeof(acm->port_line_coding)) { 296 DBG(cdev, "acm ttyGS%d short resp, len %d\n", 297 acm->port_num, req->actual); 298 usb_ep_set_halt(ep); 299 } else { 300 struct usb_cdc_line_coding *value = req->buf; 301 302 /* REVISIT: we currently just remember this data. 303 * If we change that, (a) validate it first, then 304 * (b) update whatever hardware needs updating, 305 * (c) worry about locking. This is information on 306 * the order of 9600-8-N-1 ... most of which means 307 * nothing unless we control a real RS232 line. 308 */ 309 acm->port_line_coding = *value; 310 } 311} 312 313static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 314{ 315 struct f_acm *acm = func_to_acm(f); 316 struct usb_composite_dev *cdev = f->config->cdev; 317 struct usb_request *req = cdev->req; 318 int value = -EOPNOTSUPP; 319 u16 w_index = le16_to_cpu(ctrl->wIndex); 320 u16 w_value = le16_to_cpu(ctrl->wValue); 321 u16 w_length = le16_to_cpu(ctrl->wLength); 322 323 /* composite driver infrastructure handles everything except 324 * CDC class messages; interface activation uses set_alt(). 325 * 326 * Note CDC spec table 4 lists the ACM request profile. It requires 327 * encapsulated command support ... we don't handle any, and respond 328 * to them by stalling. Options include get/set/clear comm features 329 * (not that useful) and SEND_BREAK. 330 */ 331 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 332 333 /* SET_LINE_CODING ... just read and save what the host sends */ 334 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 335 | USB_CDC_REQ_SET_LINE_CODING: 336 if (w_length != sizeof(struct usb_cdc_line_coding) 337 || w_index != acm->ctrl_id) 338 goto invalid; 339 340 value = w_length; 341 cdev->gadget->ep0->driver_data = acm; 342 req->complete = acm_complete_set_line_coding; 343 break; 344 345 /* GET_LINE_CODING ... return what host sent, or initial value */ 346 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 347 | USB_CDC_REQ_GET_LINE_CODING: 348 if (w_index != acm->ctrl_id) 349 goto invalid; 350 351 value = min_t(unsigned, w_length, 352 sizeof(struct usb_cdc_line_coding)); 353 memcpy(req->buf, &acm->port_line_coding, value); 354 break; 355 356 /* SET_CONTROL_LINE_STATE ... save what the host sent */ 357 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 358 | USB_CDC_REQ_SET_CONTROL_LINE_STATE: 359 if (w_index != acm->ctrl_id) 360 goto invalid; 361 362 value = 0; 363 364 acm->port_handshake_bits = w_value; 365 break; 366 367 default: 368invalid: 369 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 370 ctrl->bRequestType, ctrl->bRequest, 371 w_value, w_index, w_length); 372 } 373 374 /* respond with data transfer or status phase? */ 375 if (value >= 0) { 376 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n", 377 acm->port_num, ctrl->bRequestType, ctrl->bRequest, 378 w_value, w_index, w_length); 379 req->zero = 0; 380 req->length = value; 381 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 382 if (value < 0) 383 ERROR(cdev, "acm response on ttyGS%d, err %d\n", 384 acm->port_num, value); 385 } 386 387 /* device either stalls (value < 0) or reports success */ 388 return value; 389} 390 391static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 392{ 393 struct f_acm *acm = func_to_acm(f); 394 struct usb_composite_dev *cdev = f->config->cdev; 395 396 /* we know alt == 0, so this is an activation or a reset */ 397 398 if (intf == acm->ctrl_id) { 399 if (acm->notify->driver_data) { 400 VDBG(cdev, "reset acm control interface %d\n", intf); 401 usb_ep_disable(acm->notify); 402 } else { 403 VDBG(cdev, "init acm ctrl interface %d\n", intf); 404 acm->notify_desc = ep_choose(cdev->gadget, 405 acm->hs.notify, 406 acm->fs.notify); 407 } 408 usb_ep_enable(acm->notify, acm->notify_desc); 409 acm->notify->driver_data = acm; 410 411 } else if (intf == acm->data_id) { 412 if (acm->port.in->driver_data) { 413 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num); 414 gserial_disconnect(&acm->port); 415 } else { 416 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num); 417 acm->port.in_desc = ep_choose(cdev->gadget, 418 acm->hs.in, acm->fs.in); 419 acm->port.out_desc = ep_choose(cdev->gadget, 420 acm->hs.out, acm->fs.out); 421 } 422 gserial_connect(&acm->port, acm->port_num); 423 424 } else 425 return -EINVAL; 426 427 return 0; 428} 429 430static void acm_disable(struct usb_function *f) 431{ 432 struct f_acm *acm = func_to_acm(f); 433 struct usb_composite_dev *cdev = f->config->cdev; 434 435 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num); 436 gserial_disconnect(&acm->port); 437 usb_ep_disable(acm->notify); 438 acm->notify->driver_data = NULL; 439} 440 441/*-------------------------------------------------------------------------*/ 442 443/** 444 * acm_cdc_notify - issue CDC notification to host 445 * @acm: wraps host to be notified 446 * @type: notification type 447 * @value: Refer to cdc specs, wValue field. 448 * @data: data to be sent 449 * @length: size of data 450 * Context: irqs blocked, acm->lock held, acm_notify_req non-null 451 * 452 * Returns zero on success or a negative errno. 453 * 454 * See section 6.3.5 of the CDC 1.1 specification for information 455 * about the only notification we issue: SerialState change. 456 */ 457static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value, 458 void *data, unsigned length) 459{ 460 struct usb_ep *ep = acm->notify; 461 struct usb_request *req; 462 struct usb_cdc_notification *notify; 463 const unsigned len = sizeof(*notify) + length; 464 void *buf; 465 int status; 466 467 req = acm->notify_req; 468 acm->notify_req = NULL; 469 acm->pending = false; 470 471 req->length = len; 472 notify = req->buf; 473 buf = notify + 1; 474 475 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS 476 | USB_RECIP_INTERFACE; 477 notify->bNotificationType = type; 478 notify->wValue = cpu_to_le16(value); 479 notify->wIndex = cpu_to_le16(acm->ctrl_id); 480 notify->wLength = cpu_to_le16(length); 481 memcpy(buf, data, length); 482 483 /* ep_queue() can complete immediately if it fills the fifo... */ 484 spin_unlock(&acm->lock); 485 status = usb_ep_queue(ep, req, GFP_ATOMIC); 486 spin_lock(&acm->lock); 487 488 if (status < 0) { 489 ERROR(acm->port.func.config->cdev, 490 "acm ttyGS%d can't notify serial state, %d\n", 491 acm->port_num, status); 492 acm->notify_req = req; 493 } 494 495 return status; 496} 497 498static int acm_notify_serial_state(struct f_acm *acm) 499{ 500 struct usb_composite_dev *cdev = acm->port.func.config->cdev; 501 int status; 502 503 spin_lock(&acm->lock); 504 if (acm->notify_req) { 505 DBG(cdev, "acm ttyGS%d serial state %04x\n", 506 acm->port_num, acm->serial_state); 507 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE, 508 0, &acm->serial_state, sizeof(acm->serial_state)); 509 } else { 510 acm->pending = true; 511 status = 0; 512 } 513 spin_unlock(&acm->lock); 514 return status; 515} 516 517static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req) 518{ 519 struct f_acm *acm = req->context; 520 u8 doit = false; 521 522 /* on this call path we do NOT hold the port spinlock, 523 * which is why ACM needs its own spinlock 524 */ 525 spin_lock(&acm->lock); 526 if (req->status != -ESHUTDOWN) 527 doit = acm->pending; 528 acm->notify_req = req; 529 spin_unlock(&acm->lock); 530 531 if (doit) 532 acm_notify_serial_state(acm); 533} 534 535/* connect == the TTY link is open */ 536 537static void acm_connect(struct gserial *port) 538{ 539 struct f_acm *acm = port_to_acm(port); 540 541 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD; 542 acm_notify_serial_state(acm); 543} 544 545static void acm_disconnect(struct gserial *port) 546{ 547 struct f_acm *acm = port_to_acm(port); 548 549 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD); 550 acm_notify_serial_state(acm); 551} 552 553static int acm_send_break(struct gserial *port, int duration) 554{ 555 struct f_acm *acm = port_to_acm(port); 556 u16 state; 557 558 state = acm->serial_state; 559 state &= ~ACM_CTRL_BRK; 560 if (duration) 561 state |= ACM_CTRL_BRK; 562 563 acm->serial_state = state; 564 return acm_notify_serial_state(acm); 565} 566 567/*-------------------------------------------------------------------------*/ 568 569/* ACM function driver setup/binding */ 570static int 571acm_bind(struct usb_configuration *c, struct usb_function *f) 572{ 573 struct usb_composite_dev *cdev = c->cdev; 574 struct f_acm *acm = func_to_acm(f); 575 int status; 576 struct usb_ep *ep; 577 578 /* allocate instance-specific interface IDs, and patch descriptors */ 579 status = usb_interface_id(c, f); 580 if (status < 0) 581 goto fail; 582 acm->ctrl_id = status; 583 acm_iad_descriptor.bFirstInterface = status; 584 585 acm_control_interface_desc.bInterfaceNumber = status; 586 acm_union_desc .bMasterInterface0 = status; 587 588 status = usb_interface_id(c, f); 589 if (status < 0) 590 goto fail; 591 acm->data_id = status; 592 593 acm_data_interface_desc.bInterfaceNumber = status; 594 acm_union_desc.bSlaveInterface0 = status; 595 acm_call_mgmt_descriptor.bDataInterface = status; 596 597 status = -ENODEV; 598 599 /* allocate instance-specific endpoints */ 600 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc); 601 if (!ep) 602 goto fail; 603 acm->port.in = ep; 604 ep->driver_data = cdev; /* claim */ 605 606 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc); 607 if (!ep) 608 goto fail; 609 acm->port.out = ep; 610 ep->driver_data = cdev; /* claim */ 611 612 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc); 613 if (!ep) 614 goto fail; 615 acm->notify = ep; 616 ep->driver_data = cdev; /* claim */ 617 618 /* allocate notification */ 619 acm->notify_req = gs_alloc_req(ep, 620 sizeof(struct usb_cdc_notification) + 2, 621 GFP_KERNEL); 622 if (!acm->notify_req) 623 goto fail; 624 625 acm->notify_req->complete = acm_cdc_notify_complete; 626 acm->notify_req->context = acm; 627 628 /* copy descriptors, and track endpoint copies */ 629 f->descriptors = usb_copy_descriptors(acm_fs_function); 630 if (!f->descriptors) 631 goto fail; 632 633 acm->fs.in = usb_find_endpoint(acm_fs_function, 634 f->descriptors, &acm_fs_in_desc); 635 acm->fs.out = usb_find_endpoint(acm_fs_function, 636 f->descriptors, &acm_fs_out_desc); 637 acm->fs.notify = usb_find_endpoint(acm_fs_function, 638 f->descriptors, &acm_fs_notify_desc); 639 640 /* support all relevant hardware speeds... we expect that when 641 * hardware is dual speed, all bulk-capable endpoints work at 642 * both speeds 643 */ 644 if (gadget_is_dualspeed(c->cdev->gadget)) { 645 acm_hs_in_desc.bEndpointAddress = 646 acm_fs_in_desc.bEndpointAddress; 647 acm_hs_out_desc.bEndpointAddress = 648 acm_fs_out_desc.bEndpointAddress; 649 acm_hs_notify_desc.bEndpointAddress = 650 acm_fs_notify_desc.bEndpointAddress; 651 652 /* copy descriptors, and track endpoint copies */ 653 f->hs_descriptors = usb_copy_descriptors(acm_hs_function); 654 655 acm->hs.in = usb_find_endpoint(acm_hs_function, 656 f->hs_descriptors, &acm_hs_in_desc); 657 acm->hs.out = usb_find_endpoint(acm_hs_function, 658 f->hs_descriptors, &acm_hs_out_desc); 659 acm->hs.notify = usb_find_endpoint(acm_hs_function, 660 f->hs_descriptors, &acm_hs_notify_desc); 661 } 662 663 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n", 664 acm->port_num, 665 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", 666 acm->port.in->name, acm->port.out->name, 667 acm->notify->name); 668 return 0; 669 670fail: 671 if (acm->notify_req) 672 gs_free_req(acm->notify, acm->notify_req); 673 674 /* we might as well release our claims on endpoints */ 675 if (acm->notify) 676 acm->notify->driver_data = NULL; 677 if (acm->port.out) 678 acm->port.out->driver_data = NULL; 679 if (acm->port.in) 680 acm->port.in->driver_data = NULL; 681 682 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status); 683 684 return status; 685} 686 687static void 688acm_unbind(struct usb_configuration *c, struct usb_function *f) 689{ 690 struct f_acm *acm = func_to_acm(f); 691 692 if (gadget_is_dualspeed(c->cdev->gadget)) 693 usb_free_descriptors(f->hs_descriptors); 694 usb_free_descriptors(f->descriptors); 695 gs_free_req(acm->notify, acm->notify_req); 696 kfree(acm); 697} 698 699/* Some controllers can't support CDC ACM ... */ 700static inline bool can_support_cdc(struct usb_configuration *c) 701{ 702 /* everything else is *probably* fine ... */ 703 return true; 704} 705 706/** 707 * acm_bind_config - add a CDC ACM function to a configuration 708 * @c: the configuration to support the CDC ACM instance 709 * @port_num: /dev/ttyGS* port this interface will use 710 * Context: single threaded during gadget setup 711 * 712 * Returns zero on success, else negative errno. 713 * 714 * Caller must have called @gserial_setup() with enough ports to 715 * handle all the ones it binds. Caller is also responsible 716 * for calling @gserial_cleanup() before module unload. 717 */ 718int acm_bind_config(struct usb_configuration *c, u8 port_num) 719{ 720 struct f_acm *acm; 721 int status; 722 723 if (!can_support_cdc(c)) 724 return -EINVAL; 725 726 /* REVISIT might want instance-specific strings to help 727 * distinguish instances ... 728 */ 729 730 /* maybe allocate device-global string IDs, and patch descriptors */ 731 if (acm_string_defs[ACM_CTRL_IDX].id == 0) { 732 status = usb_string_id(c->cdev); 733 if (status < 0) 734 return status; 735 acm_string_defs[ACM_CTRL_IDX].id = status; 736 737 acm_control_interface_desc.iInterface = status; 738 739 status = usb_string_id(c->cdev); 740 if (status < 0) 741 return status; 742 acm_string_defs[ACM_DATA_IDX].id = status; 743 744 acm_data_interface_desc.iInterface = status; 745 746 status = usb_string_id(c->cdev); 747 if (status < 0) 748 return status; 749 acm_string_defs[ACM_IAD_IDX].id = status; 750 751 acm_iad_descriptor.iFunction = status; 752 } 753 754 /* allocate and initialize one new instance */ 755 acm = kzalloc(sizeof *acm, GFP_KERNEL); 756 if (!acm) 757 return -ENOMEM; 758 759 spin_lock_init(&acm->lock); 760 761 acm->port_num = port_num; 762 763 acm->port.connect = acm_connect; 764 acm->port.disconnect = acm_disconnect; 765 acm->port.send_break = acm_send_break; 766 767 acm->port.func.name = "acm"; 768 acm->port.func.strings = acm_strings; 769 /* descriptors are per-instance copies */ 770 acm->port.func.bind = acm_bind; 771 acm->port.func.unbind = acm_unbind; 772 acm->port.func.set_alt = acm_set_alt; 773 acm->port.func.setup = acm_setup; 774 acm->port.func.disable = acm_disable; 775 776 status = usb_add_function(c, &acm->port.func); 777 if (status) 778 kfree(acm); 779 return status; 780} 781