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