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