David Brownell | 4d5a73d | 2008-06-19 18:18:40 -0700 | [diff] [blame] | 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 | * |
| 8 | * This software is distributed under the terms of the GNU General |
| 9 | * Public License ("GPL") as published by the Free Software Foundation, |
| 10 | * either version 2 of that License or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | /* #define VERBOSE_DEBUG */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/device.h> |
| 17 | |
| 18 | #include "u_serial.h" |
| 19 | #include "gadget_chips.h" |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | * This CDC ACM function support just wraps control functions and |
| 24 | * notifications around the generic serial-over-usb code. |
| 25 | * |
| 26 | * Because CDC ACM is standardized by the USB-IF, many host operating |
| 27 | * systems have drivers for it. Accordingly, ACM is the preferred |
| 28 | * interop solution for serial-port type connections. The control |
| 29 | * models are often not necessary, and in any case don't do much in |
| 30 | * this bare-bones implementation. |
| 31 | * |
| 32 | * Note that even MS-Windows has some support for ACM. However, that |
| 33 | * support is somewhat broken because when you use ACM in a composite |
| 34 | * device, having multiple interfaces confuses the poor OS. It doesn't |
| 35 | * seem to understand CDC Union descriptors. The new "association" |
| 36 | * descriptors (roughly equivalent to CDC Unions) may sometimes help. |
| 37 | */ |
| 38 | |
| 39 | struct acm_ep_descs { |
| 40 | struct usb_endpoint_descriptor *in; |
| 41 | struct usb_endpoint_descriptor *out; |
| 42 | struct usb_endpoint_descriptor *notify; |
| 43 | }; |
| 44 | |
| 45 | struct f_acm { |
| 46 | struct gserial port; |
| 47 | u8 ctrl_id, data_id; |
| 48 | u8 port_num; |
| 49 | |
| 50 | struct usb_descriptor_header **fs_function; |
| 51 | struct acm_ep_descs fs; |
| 52 | struct usb_descriptor_header **hs_function; |
| 53 | struct acm_ep_descs hs; |
| 54 | |
| 55 | struct usb_ep *notify; |
| 56 | struct usb_endpoint_descriptor *notify_desc; |
| 57 | |
| 58 | struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ |
| 59 | u16 port_handshake_bits; |
| 60 | #define RS232_RTS (1 << 1) /* unused with full duplex */ |
| 61 | #define RS232_DTR (1 << 0) /* host is ready for data r/w */ |
| 62 | }; |
| 63 | |
| 64 | static inline struct f_acm *func_to_acm(struct usb_function *f) |
| 65 | { |
| 66 | return container_of(f, struct f_acm, port.func); |
| 67 | } |
| 68 | |
| 69 | /*-------------------------------------------------------------------------*/ |
| 70 | |
| 71 | /* notification endpoint uses smallish and infrequent fixed-size messages */ |
| 72 | |
| 73 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ |
| 74 | #define GS_NOTIFY_MAXPACKET 8 |
| 75 | |
| 76 | /* interface and class descriptors: */ |
| 77 | |
| 78 | static struct usb_interface_descriptor acm_control_interface_desc __initdata = { |
| 79 | .bLength = USB_DT_INTERFACE_SIZE, |
| 80 | .bDescriptorType = USB_DT_INTERFACE, |
| 81 | /* .bInterfaceNumber = DYNAMIC */ |
| 82 | .bNumEndpoints = 1, |
| 83 | .bInterfaceClass = USB_CLASS_COMM, |
| 84 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 85 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, |
| 86 | /* .iInterface = DYNAMIC */ |
| 87 | }; |
| 88 | |
| 89 | static struct usb_interface_descriptor acm_data_interface_desc __initdata = { |
| 90 | .bLength = USB_DT_INTERFACE_SIZE, |
| 91 | .bDescriptorType = USB_DT_INTERFACE, |
| 92 | /* .bInterfaceNumber = DYNAMIC */ |
| 93 | .bNumEndpoints = 2, |
| 94 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 95 | .bInterfaceSubClass = 0, |
| 96 | .bInterfaceProtocol = 0, |
| 97 | /* .iInterface = DYNAMIC */ |
| 98 | }; |
| 99 | |
| 100 | static struct usb_cdc_header_desc acm_header_desc __initdata = { |
| 101 | .bLength = sizeof(acm_header_desc), |
| 102 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 103 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 104 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
| 105 | }; |
| 106 | |
| 107 | static struct usb_cdc_call_mgmt_descriptor |
| 108 | acm_call_mgmt_descriptor __initdata = { |
| 109 | .bLength = sizeof(acm_call_mgmt_descriptor), |
| 110 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 111 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 112 | .bmCapabilities = 0, |
| 113 | /* .bDataInterface = DYNAMIC */ |
| 114 | }; |
| 115 | |
| 116 | static struct usb_cdc_acm_descriptor acm_descriptor __initdata = { |
| 117 | .bLength = sizeof(acm_descriptor), |
| 118 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 119 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
| 120 | .bmCapabilities = (1 << 1), |
| 121 | }; |
| 122 | |
| 123 | static struct usb_cdc_union_desc acm_union_desc __initdata = { |
| 124 | .bLength = sizeof(acm_union_desc), |
| 125 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 126 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 127 | /* .bMasterInterface0 = DYNAMIC */ |
| 128 | /* .bSlaveInterface0 = DYNAMIC */ |
| 129 | }; |
| 130 | |
| 131 | /* full speed support: */ |
| 132 | |
| 133 | static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = { |
| 134 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 135 | .bDescriptorType = USB_DT_ENDPOINT, |
| 136 | .bEndpointAddress = USB_DIR_IN, |
| 137 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 138 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 139 | .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, |
| 140 | }; |
| 141 | |
| 142 | static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = { |
| 143 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 144 | .bDescriptorType = USB_DT_ENDPOINT, |
| 145 | .bEndpointAddress = USB_DIR_IN, |
| 146 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 147 | }; |
| 148 | |
| 149 | static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = { |
| 150 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 151 | .bDescriptorType = USB_DT_ENDPOINT, |
| 152 | .bEndpointAddress = USB_DIR_OUT, |
| 153 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 154 | }; |
| 155 | |
| 156 | static struct usb_descriptor_header *acm_fs_function[] __initdata = { |
| 157 | (struct usb_descriptor_header *) &acm_control_interface_desc, |
| 158 | (struct usb_descriptor_header *) &acm_header_desc, |
| 159 | (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, |
| 160 | (struct usb_descriptor_header *) &acm_descriptor, |
| 161 | (struct usb_descriptor_header *) &acm_union_desc, |
| 162 | (struct usb_descriptor_header *) &acm_fs_notify_desc, |
| 163 | (struct usb_descriptor_header *) &acm_data_interface_desc, |
| 164 | (struct usb_descriptor_header *) &acm_fs_in_desc, |
| 165 | (struct usb_descriptor_header *) &acm_fs_out_desc, |
| 166 | NULL, |
| 167 | }; |
| 168 | |
| 169 | /* high speed support: */ |
| 170 | |
| 171 | static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = { |
| 172 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 173 | .bDescriptorType = USB_DT_ENDPOINT, |
| 174 | .bEndpointAddress = USB_DIR_IN, |
| 175 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 176 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 177 | .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, |
| 178 | }; |
| 179 | |
| 180 | static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = { |
| 181 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 182 | .bDescriptorType = USB_DT_ENDPOINT, |
| 183 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 184 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 185 | }; |
| 186 | |
| 187 | static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = { |
| 188 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 189 | .bDescriptorType = USB_DT_ENDPOINT, |
| 190 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 191 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 192 | }; |
| 193 | |
| 194 | static struct usb_descriptor_header *acm_hs_function[] __initdata = { |
| 195 | (struct usb_descriptor_header *) &acm_control_interface_desc, |
| 196 | (struct usb_descriptor_header *) &acm_header_desc, |
| 197 | (struct usb_descriptor_header *) &acm_call_mgmt_descriptor, |
| 198 | (struct usb_descriptor_header *) &acm_descriptor, |
| 199 | (struct usb_descriptor_header *) &acm_union_desc, |
| 200 | (struct usb_descriptor_header *) &acm_hs_notify_desc, |
| 201 | (struct usb_descriptor_header *) &acm_data_interface_desc, |
| 202 | (struct usb_descriptor_header *) &acm_hs_in_desc, |
| 203 | (struct usb_descriptor_header *) &acm_hs_out_desc, |
| 204 | NULL, |
| 205 | }; |
| 206 | |
| 207 | /* string descriptors: */ |
| 208 | |
| 209 | #define ACM_CTRL_IDX 0 |
| 210 | #define ACM_DATA_IDX 1 |
| 211 | |
| 212 | /* static strings, in UTF-8 */ |
| 213 | static struct usb_string acm_string_defs[] = { |
| 214 | [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)", |
| 215 | [ACM_DATA_IDX].s = "CDC ACM Data", |
| 216 | { /* ZEROES END LIST */ }, |
| 217 | }; |
| 218 | |
| 219 | static struct usb_gadget_strings acm_string_table = { |
| 220 | .language = 0x0409, /* en-us */ |
| 221 | .strings = acm_string_defs, |
| 222 | }; |
| 223 | |
| 224 | static struct usb_gadget_strings *acm_strings[] = { |
| 225 | &acm_string_table, |
| 226 | NULL, |
| 227 | }; |
| 228 | |
| 229 | /*-------------------------------------------------------------------------*/ |
| 230 | |
| 231 | /* ACM control ... data handling is delegated to tty library code. |
| 232 | * The main task of this function is to activate and deactivate |
| 233 | * that code based on device state; track parameters like line |
| 234 | * speed, handshake state, and so on; and issue notifications. |
| 235 | */ |
| 236 | |
| 237 | static void acm_complete_set_line_coding(struct usb_ep *ep, |
| 238 | struct usb_request *req) |
| 239 | { |
| 240 | struct f_acm *acm = ep->driver_data; |
| 241 | struct usb_composite_dev *cdev = acm->port.func.config->cdev; |
| 242 | |
| 243 | if (req->status != 0) { |
| 244 | DBG(cdev, "acm ttyGS%d completion, err %d\n", |
| 245 | acm->port_num, req->status); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | /* normal completion */ |
| 250 | if (req->actual != sizeof(acm->port_line_coding)) { |
| 251 | DBG(cdev, "acm ttyGS%d short resp, len %d\n", |
| 252 | acm->port_num, req->actual); |
| 253 | usb_ep_set_halt(ep); |
| 254 | } else { |
| 255 | struct usb_cdc_line_coding *value = req->buf; |
| 256 | |
| 257 | /* REVISIT: we currently just remember this data. |
| 258 | * If we change that, (a) validate it first, then |
| 259 | * (b) update whatever hardware needs updating, |
| 260 | * (c) worry about locking. This is information on |
| 261 | * the order of 9600-8-N-1 ... most of which means |
| 262 | * nothing unless we control a real RS232 line. |
| 263 | */ |
| 264 | acm->port_line_coding = *value; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 269 | { |
| 270 | struct f_acm *acm = func_to_acm(f); |
| 271 | struct usb_composite_dev *cdev = f->config->cdev; |
| 272 | struct usb_request *req = cdev->req; |
| 273 | int value = -EOPNOTSUPP; |
| 274 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 275 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 276 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 277 | |
| 278 | /* composite driver infrastructure handles everything except |
| 279 | * CDC class messages; interface activation uses set_alt(). |
| 280 | */ |
| 281 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 282 | |
| 283 | /* SET_LINE_CODING ... just read and save what the host sends */ |
| 284 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 285 | | USB_CDC_REQ_SET_LINE_CODING: |
| 286 | if (w_length != sizeof(struct usb_cdc_line_coding) |
| 287 | || w_index != acm->ctrl_id) |
| 288 | goto invalid; |
| 289 | |
| 290 | value = w_length; |
| 291 | cdev->gadget->ep0->driver_data = acm; |
| 292 | req->complete = acm_complete_set_line_coding; |
| 293 | break; |
| 294 | |
| 295 | /* GET_LINE_CODING ... return what host sent, or initial value */ |
| 296 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 297 | | USB_CDC_REQ_GET_LINE_CODING: |
| 298 | if (w_index != acm->ctrl_id) |
| 299 | goto invalid; |
| 300 | |
| 301 | value = min_t(unsigned, w_length, |
| 302 | sizeof(struct usb_cdc_line_coding)); |
| 303 | memcpy(req->buf, &acm->port_line_coding, value); |
| 304 | break; |
| 305 | |
| 306 | /* SET_CONTROL_LINE_STATE ... save what the host sent */ |
| 307 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 308 | | USB_CDC_REQ_SET_CONTROL_LINE_STATE: |
| 309 | if (w_index != acm->ctrl_id) |
| 310 | goto invalid; |
| 311 | |
| 312 | value = 0; |
| 313 | |
| 314 | /* FIXME we should not allow data to flow until the |
| 315 | * host sets the RS232_DTR bit; and when it clears |
| 316 | * that bit, we should return to that no-flow state. |
| 317 | */ |
| 318 | acm->port_handshake_bits = w_value; |
| 319 | break; |
| 320 | |
| 321 | default: |
| 322 | invalid: |
| 323 | VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 324 | ctrl->bRequestType, ctrl->bRequest, |
| 325 | w_value, w_index, w_length); |
| 326 | } |
| 327 | |
| 328 | /* respond with data transfer or status phase? */ |
| 329 | if (value >= 0) { |
| 330 | DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n", |
| 331 | acm->port_num, ctrl->bRequestType, ctrl->bRequest, |
| 332 | w_value, w_index, w_length); |
| 333 | req->zero = 0; |
| 334 | req->length = value; |
| 335 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 336 | if (value < 0) |
| 337 | ERROR(cdev, "acm response on ttyGS%d, err %d\n", |
| 338 | acm->port_num, value); |
| 339 | } |
| 340 | |
| 341 | /* device either stalls (value < 0) or reports success */ |
| 342 | return value; |
| 343 | } |
| 344 | |
| 345 | static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 346 | { |
| 347 | struct f_acm *acm = func_to_acm(f); |
| 348 | struct usb_composite_dev *cdev = f->config->cdev; |
| 349 | |
| 350 | /* we know alt == 0, so this is an activation or a reset */ |
| 351 | |
| 352 | if (intf == acm->ctrl_id) { |
| 353 | /* REVISIT this may need more work when we start to |
| 354 | * send notifications ... |
| 355 | */ |
| 356 | if (acm->notify->driver_data) { |
| 357 | VDBG(cdev, "reset acm control interface %d\n", intf); |
| 358 | usb_ep_disable(acm->notify); |
| 359 | } else { |
| 360 | VDBG(cdev, "init acm ctrl interface %d\n", intf); |
| 361 | acm->notify_desc = ep_choose(cdev->gadget, |
| 362 | acm->hs.notify, |
| 363 | acm->fs.notify); |
| 364 | } |
| 365 | usb_ep_enable(acm->notify, acm->notify_desc); |
| 366 | acm->notify->driver_data = acm; |
| 367 | |
| 368 | } else if (intf == acm->data_id) { |
| 369 | if (acm->port.in->driver_data) { |
| 370 | DBG(cdev, "reset acm ttyGS%d\n", acm->port_num); |
| 371 | gserial_disconnect(&acm->port); |
| 372 | } else { |
| 373 | DBG(cdev, "activate acm ttyGS%d\n", acm->port_num); |
| 374 | acm->port.in_desc = ep_choose(cdev->gadget, |
| 375 | acm->hs.in, acm->fs.in); |
| 376 | acm->port.out_desc = ep_choose(cdev->gadget, |
| 377 | acm->hs.out, acm->fs.out); |
| 378 | } |
| 379 | gserial_connect(&acm->port, acm->port_num); |
| 380 | |
| 381 | } else |
| 382 | return -EINVAL; |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static void acm_disable(struct usb_function *f) |
| 388 | { |
| 389 | struct f_acm *acm = func_to_acm(f); |
| 390 | struct usb_composite_dev *cdev = f->config->cdev; |
| 391 | |
| 392 | DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num); |
| 393 | gserial_disconnect(&acm->port); |
| 394 | usb_ep_disable(acm->notify); |
| 395 | acm->notify->driver_data = NULL; |
| 396 | } |
| 397 | |
| 398 | /*-------------------------------------------------------------------------*/ |
| 399 | |
| 400 | /* ACM function driver setup/binding */ |
| 401 | static int __init |
| 402 | acm_bind(struct usb_configuration *c, struct usb_function *f) |
| 403 | { |
| 404 | struct usb_composite_dev *cdev = c->cdev; |
| 405 | struct f_acm *acm = func_to_acm(f); |
| 406 | int status; |
| 407 | struct usb_ep *ep; |
| 408 | |
| 409 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 410 | status = usb_interface_id(c, f); |
| 411 | if (status < 0) |
| 412 | goto fail; |
| 413 | acm->ctrl_id = status; |
| 414 | |
| 415 | acm_control_interface_desc.bInterfaceNumber = status; |
| 416 | acm_union_desc .bMasterInterface0 = status; |
| 417 | |
| 418 | status = usb_interface_id(c, f); |
| 419 | if (status < 0) |
| 420 | goto fail; |
| 421 | acm->data_id = status; |
| 422 | |
| 423 | acm_data_interface_desc.bInterfaceNumber = status; |
| 424 | acm_union_desc.bSlaveInterface0 = status; |
| 425 | acm_call_mgmt_descriptor.bDataInterface = status; |
| 426 | |
| 427 | status = -ENODEV; |
| 428 | |
| 429 | /* allocate instance-specific endpoints */ |
| 430 | ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc); |
| 431 | if (!ep) |
| 432 | goto fail; |
| 433 | acm->port.in = ep; |
| 434 | ep->driver_data = cdev; /* claim */ |
| 435 | |
| 436 | ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc); |
| 437 | if (!ep) |
| 438 | goto fail; |
| 439 | acm->port.out = ep; |
| 440 | ep->driver_data = cdev; /* claim */ |
| 441 | |
| 442 | ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc); |
| 443 | if (!ep) |
| 444 | goto fail; |
| 445 | acm->notify = ep; |
| 446 | ep->driver_data = cdev; /* claim */ |
| 447 | |
| 448 | /* copy descriptors, and track endpoint copies */ |
| 449 | f->descriptors = usb_copy_descriptors(acm_fs_function); |
| 450 | |
| 451 | acm->fs.in = usb_find_endpoint(acm_fs_function, |
| 452 | f->descriptors, &acm_fs_in_desc); |
| 453 | acm->fs.out = usb_find_endpoint(acm_fs_function, |
| 454 | f->descriptors, &acm_fs_out_desc); |
| 455 | acm->fs.notify = usb_find_endpoint(acm_fs_function, |
| 456 | f->descriptors, &acm_fs_notify_desc); |
| 457 | |
| 458 | /* support all relevant hardware speeds... we expect that when |
| 459 | * hardware is dual speed, all bulk-capable endpoints work at |
| 460 | * both speeds |
| 461 | */ |
| 462 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 463 | acm_hs_in_desc.bEndpointAddress = |
| 464 | acm_fs_in_desc.bEndpointAddress; |
| 465 | acm_hs_out_desc.bEndpointAddress = |
| 466 | acm_fs_out_desc.bEndpointAddress; |
| 467 | acm_hs_notify_desc.bEndpointAddress = |
| 468 | acm_fs_notify_desc.bEndpointAddress; |
| 469 | |
| 470 | /* copy descriptors, and track endpoint copies */ |
| 471 | f->hs_descriptors = usb_copy_descriptors(acm_hs_function); |
| 472 | |
| 473 | acm->hs.in = usb_find_endpoint(acm_hs_function, |
| 474 | f->hs_descriptors, &acm_hs_in_desc); |
| 475 | acm->hs.out = usb_find_endpoint(acm_hs_function, |
| 476 | f->hs_descriptors, &acm_hs_out_desc); |
| 477 | acm->hs.notify = usb_find_endpoint(acm_hs_function, |
| 478 | f->hs_descriptors, &acm_hs_notify_desc); |
| 479 | } |
| 480 | |
| 481 | /* FIXME provide a callback for triggering notifications */ |
| 482 | |
| 483 | DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n", |
| 484 | acm->port_num, |
| 485 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 486 | acm->port.in->name, acm->port.out->name, |
| 487 | acm->notify->name); |
| 488 | return 0; |
| 489 | |
| 490 | fail: |
| 491 | /* we might as well release our claims on endpoints */ |
| 492 | if (acm->notify) |
| 493 | acm->notify->driver_data = NULL; |
| 494 | if (acm->port.out) |
| 495 | acm->port.out->driver_data = NULL; |
| 496 | if (acm->port.in) |
| 497 | acm->port.in->driver_data = NULL; |
| 498 | |
| 499 | ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status); |
| 500 | |
| 501 | return status; |
| 502 | } |
| 503 | |
| 504 | static void |
| 505 | acm_unbind(struct usb_configuration *c, struct usb_function *f) |
| 506 | { |
| 507 | if (gadget_is_dualspeed(c->cdev->gadget)) |
| 508 | usb_free_descriptors(f->hs_descriptors); |
| 509 | usb_free_descriptors(f->descriptors); |
| 510 | kfree(func_to_acm(f)); |
| 511 | } |
| 512 | |
| 513 | /* Some controllers can't support CDC ACM ... */ |
| 514 | static inline bool can_support_cdc(struct usb_configuration *c) |
| 515 | { |
| 516 | /* SH3 doesn't support multiple interfaces */ |
| 517 | if (gadget_is_sh(c->cdev->gadget)) |
| 518 | return false; |
| 519 | |
| 520 | /* sa1100 doesn't have a third interrupt endpoint */ |
| 521 | if (gadget_is_sa1100(c->cdev->gadget)) |
| 522 | return false; |
| 523 | |
| 524 | /* everything else is *probably* fine ... */ |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * acm_bind_config - add a CDC ACM function to a configuration |
| 530 | * @c: the configuration to support the CDC ACM instance |
| 531 | * @port_num: /dev/ttyGS* port this interface will use |
| 532 | * Context: single threaded during gadget setup |
| 533 | * |
| 534 | * Returns zero on success, else negative errno. |
| 535 | * |
| 536 | * Caller must have called @gserial_setup() with enough ports to |
| 537 | * handle all the ones it binds. Caller is also responsible |
| 538 | * for calling @gserial_cleanup() before module unload. |
| 539 | */ |
| 540 | int __init acm_bind_config(struct usb_configuration *c, u8 port_num) |
| 541 | { |
| 542 | struct f_acm *acm; |
| 543 | int status; |
| 544 | |
| 545 | if (!can_support_cdc(c)) |
| 546 | return -EINVAL; |
| 547 | |
| 548 | /* REVISIT might want instance-specific strings to help |
| 549 | * distinguish instances ... |
| 550 | */ |
| 551 | |
| 552 | /* maybe allocate device-global string IDs, and patch descriptors */ |
| 553 | if (acm_string_defs[ACM_CTRL_IDX].id == 0) { |
| 554 | status = usb_string_id(c->cdev); |
| 555 | if (status < 0) |
| 556 | return status; |
| 557 | acm_string_defs[ACM_CTRL_IDX].id = status; |
| 558 | |
| 559 | acm_control_interface_desc.iInterface = status; |
| 560 | |
| 561 | status = usb_string_id(c->cdev); |
| 562 | if (status < 0) |
| 563 | return status; |
| 564 | acm_string_defs[ACM_DATA_IDX].id = status; |
| 565 | |
| 566 | acm_data_interface_desc.iInterface = status; |
| 567 | } |
| 568 | |
| 569 | /* allocate and initialize one new instance */ |
| 570 | acm = kzalloc(sizeof *acm, GFP_KERNEL); |
| 571 | if (!acm) |
| 572 | return -ENOMEM; |
| 573 | |
| 574 | acm->port_num = port_num; |
| 575 | |
| 576 | acm->port.func.name = "acm"; |
| 577 | acm->port.func.strings = acm_strings; |
| 578 | /* descriptors are per-instance copies */ |
| 579 | acm->port.func.bind = acm_bind; |
| 580 | acm->port.func.unbind = acm_unbind; |
| 581 | acm->port.func.set_alt = acm_set_alt; |
| 582 | acm->port.func.setup = acm_setup; |
| 583 | acm->port.func.disable = acm_disable; |
| 584 | |
| 585 | status = usb_add_function(c, &acm->port.func); |
| 586 | if (status) |
| 587 | kfree(acm); |
| 588 | return status; |
| 589 | } |