2 * f_acm.c -- USB CDC serial (ACM) function driver
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)
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.
15 /* #define VERBOSE_DEBUG */
19 #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
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>
28 #include "gadget_chips.h"
31 #define ACM_LOG "USB_ACM"
34 * This CDC ACM function support just wraps control functions and
35 * notifications around the generic serial-over-usb code.
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.
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.
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.
63 struct usb_ep
*notify
;
64 struct usb_request
*notify_req
;
66 struct usb_cdc_line_coding port_line_coding
; /* 8-N-1 etc */
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 */
73 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
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)
84 static inline struct f_acm
*func_to_acm(struct usb_function
*f
)
86 return container_of(f
, struct f_acm
, port
.func
);
89 static inline struct f_acm
*port_to_acm(struct gserial
*p
)
91 return container_of(p
, struct f_acm
, port
);
94 /*-------------------------------------------------------------------------*/
96 /* notification endpoint uses smallish and infrequent fixed-size messages */
98 #define GS_NOTIFY_INTERVAL_MS 32
99 #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
101 /* interface and class descriptors: */
103 static struct usb_interface_assoc_descriptor
104 acm_iad_descriptor
= {
105 .bLength
= sizeof acm_iad_descriptor
,
106 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
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 */
117 static struct usb_interface_descriptor acm_control_interface_desc
= {
118 .bLength
= USB_DT_INTERFACE_SIZE
,
119 .bDescriptorType
= USB_DT_INTERFACE
,
120 /* .bInterfaceNumber = DYNAMIC */
122 .bInterfaceClass
= USB_CLASS_COMM
,
123 .bInterfaceSubClass
= USB_CDC_SUBCLASS_ACM
,
124 .bInterfaceProtocol
= USB_CDC_ACM_PROTO_AT_V25TER
,
125 /* .iInterface = DYNAMIC */
128 static struct usb_interface_descriptor acm_data_interface_desc
= {
129 .bLength
= USB_DT_INTERFACE_SIZE
,
130 .bDescriptorType
= USB_DT_INTERFACE
,
131 /* .bInterfaceNumber = DYNAMIC */
133 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
134 .bInterfaceSubClass
= 0,
135 .bInterfaceProtocol
= 0,
136 /* .iInterface = DYNAMIC */
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),
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
,
152 /* .bDataInterface = DYNAMIC */
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
,
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 */
170 /* full speed support: */
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
,
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
,
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
,
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
,
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
),
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),
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),
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
,
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),
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),
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
,
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
,
283 /* string descriptors: */
285 #define ACM_CTRL_IDX 0
286 #define ACM_DATA_IDX 1
287 #define ACM_IAD_IDX 2
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 */
297 static struct usb_gadget_strings acm_string_table
= {
298 .language
= 0x0409, /* en-us */
299 .strings
= acm_string_defs
,
302 static struct usb_gadget_strings
*acm_strings
[] = {
307 /*-------------------------------------------------------------------------*/
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.
315 static void acm_complete_set_line_coding(struct usb_ep
*ep
,
316 struct usb_request
*req
)
318 struct f_acm
*acm
= ep
->driver_data
;
319 struct usb_composite_dev
*cdev
= acm
->port
.func
.config
->cdev
;
321 if (req
->status
!= 0) {
322 DBG(cdev
, "acm ttyGS%d completion, err %d\n",
323 acm
->port_num
, req
->status
);
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
);
333 struct usb_cdc_line_coding
*value
= req
->buf
;
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.
342 acm
->port_line_coding
= *value
;
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
);
350 static int acm_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
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
);
360 /* composite driver infrastructure handles everything except
361 * CDC class messages; interface activation uses set_alt().
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.
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
);
372 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
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
)
382 cdev
->gadget
->ep0
->driver_data
= acm
;
383 req
->complete
= acm_complete_set_line_coding
;
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
)
392 value
= min_t(unsigned, w_length
,
393 sizeof(struct usb_cdc_line_coding
));
394 memcpy(req
->buf
, &acm
->port_line_coding
, value
);
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
);
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
)
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.
414 acm
->port_handshake_bits
= w_value
;
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
);
424 /* respond with data transfer or status phase? */
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
);
431 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
433 ERROR(cdev
, "acm response on ttyGS%d, err %d\n",
434 acm
->port_num
, value
);
437 /* device either stalls (value < 0) or reports success */
441 static int acm_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
443 struct f_acm
*acm
= func_to_acm(f
);
444 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
446 /* we know alt == 0, so this is an activation or a reset */
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
);
454 if (!acm
->notify
->desc
)
455 if (config_ep_by_speed(cdev
->gadget
, f
, acm
->notify
))
458 usb_ep_enable(acm
->notify
);
459 acm
->notify
->driver_data
= acm
;
461 } else if (intf
== acm
->data_id
) {
462 if (acm
->port
.in
->driver_data
) {
463 DBG(cdev
, "reset acm ttyGS%d\n", acm
->port_num
);
464 gserial_disconnect(&acm
->port
);
466 if (!acm
->port
.in
->desc
|| !acm
->port
.out
->desc
) {
467 DBG(cdev
, "activate acm ttyGS%d\n", acm
->port_num
);
468 if (config_ep_by_speed(cdev
->gadget
, f
,
470 config_ep_by_speed(cdev
->gadget
, f
,
472 acm
->port
.in
->desc
= NULL
;
473 acm
->port
.out
->desc
= NULL
;
477 gserial_connect(&acm
->port
, acm
->port_num
);
485 static void acm_disable(struct usb_function
*f
)
487 struct f_acm
*acm
= func_to_acm(f
);
488 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
490 INFO(cdev
, "acm ttyGS%d deactivated\n", acm
->port_num
);
491 gserial_disconnect(&acm
->port
);
492 usb_ep_disable(acm
->notify
);
493 acm
->notify
->driver_data
= NULL
;
496 /*-------------------------------------------------------------------------*/
499 * acm_cdc_notify - issue CDC notification to host
500 * @acm: wraps host to be notified
501 * @type: notification type
502 * @value: Refer to cdc specs, wValue field.
503 * @data: data to be sent
504 * @length: size of data
505 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
507 * Returns zero on success or a negative errno.
509 * See section 6.3.5 of the CDC 1.1 specification for information
510 * about the only notification we issue: SerialState change.
512 static int acm_cdc_notify(struct f_acm
*acm
, u8 type
, u16 value
,
513 void *data
, unsigned length
)
515 struct usb_ep
*ep
= acm
->notify
;
516 struct usb_request
*req
;
517 struct usb_cdc_notification
*notify
;
518 const unsigned len
= sizeof(*notify
) + length
;
522 req
= acm
->notify_req
;
523 acm
->notify_req
= NULL
;
524 acm
->pending
= false;
530 notify
->bmRequestType
= USB_DIR_IN
| USB_TYPE_CLASS
531 | USB_RECIP_INTERFACE
;
532 notify
->bNotificationType
= type
;
533 notify
->wValue
= cpu_to_le16(value
);
534 notify
->wIndex
= cpu_to_le16(acm
->ctrl_id
);
535 notify
->wLength
= cpu_to_le16(length
);
536 memcpy(buf
, data
, length
);
538 /* ep_queue() can complete immediately if it fills the fifo... */
539 spin_unlock(&acm
->lock
);
540 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
541 spin_lock(&acm
->lock
);
544 ERROR(acm
->port
.func
.config
->cdev
,
545 "acm ttyGS%d can't notify serial state, %d\n",
546 acm
->port_num
, status
);
547 acm
->notify_req
= req
;
553 static int acm_notify_serial_state(struct f_acm
*acm
)
555 struct usb_composite_dev
*cdev
= acm
->port
.func
.config
->cdev
;
559 spin_lock(&acm
->lock
);
560 if (acm
->notify_req
) {
561 DBG(cdev
, "acm ttyGS%d serial state %04x\n",
562 acm
->port_num
, acm
->serial_state
);
563 serial_state
= cpu_to_le16(acm
->serial_state
);
564 status
= acm_cdc_notify(acm
, USB_CDC_NOTIFY_SERIAL_STATE
,
565 0, &serial_state
, sizeof(acm
->serial_state
));
570 spin_unlock(&acm
->lock
);
574 static void acm_cdc_notify_complete(struct usb_ep
*ep
, struct usb_request
*req
)
576 struct f_acm
*acm
= req
->context
;
579 /* on this call path we do NOT hold the port spinlock,
580 * which is why ACM needs its own spinlock
582 spin_lock(&acm
->lock
);
583 if (req
->status
!= -ESHUTDOWN
)
585 acm
->notify_req
= req
;
586 spin_unlock(&acm
->lock
);
589 acm_notify_serial_state(acm
);
592 /* connect == the TTY link is open */
594 static void acm_connect(struct gserial
*port
)
596 struct f_acm
*acm
= port_to_acm(port
);
598 acm
->serial_state
|= ACM_CTRL_DSR
| ACM_CTRL_DCD
;
599 acm_notify_serial_state(acm
);
602 static void acm_disconnect(struct gserial
*port
)
604 struct f_acm
*acm
= port_to_acm(port
);
606 acm
->serial_state
&= ~(ACM_CTRL_DSR
| ACM_CTRL_DCD
);
607 acm_notify_serial_state(acm
);
610 static int acm_send_break(struct gserial
*port
, int duration
)
612 struct f_acm
*acm
= port_to_acm(port
);
615 state
= acm
->serial_state
;
616 state
&= ~ACM_CTRL_BRK
;
618 state
|= ACM_CTRL_BRK
;
620 acm
->serial_state
= state
;
621 return acm_notify_serial_state(acm
);
624 /*-------------------------------------------------------------------------*/
626 /* ACM function driver setup/binding */
628 acm_bind(struct usb_configuration
*c
, struct usb_function
*f
)
630 struct usb_composite_dev
*cdev
= c
->cdev
;
631 struct f_acm
*acm
= func_to_acm(f
);
632 struct usb_string
*us
;
636 /* REVISIT might want instance-specific strings to help
637 * distinguish instances ...
640 /* maybe allocate device-global string IDs, and patch descriptors */
641 us
= usb_gstrings_attach(cdev
, acm_strings
,
642 ARRAY_SIZE(acm_string_defs
));
645 acm_control_interface_desc
.iInterface
= us
[ACM_CTRL_IDX
].id
;
646 acm_data_interface_desc
.iInterface
= us
[ACM_DATA_IDX
].id
;
647 acm_iad_descriptor
.iFunction
= us
[ACM_IAD_IDX
].id
;
649 /* allocate instance-specific interface IDs, and patch descriptors */
650 status
= usb_interface_id(c
, f
);
653 acm
->ctrl_id
= status
;
654 acm_iad_descriptor
.bFirstInterface
= status
;
656 acm_control_interface_desc
.bInterfaceNumber
= status
;
657 acm_union_desc
.bMasterInterface0
= status
;
659 status
= usb_interface_id(c
, f
);
662 acm
->data_id
= status
;
664 acm_data_interface_desc
.bInterfaceNumber
= status
;
665 acm_union_desc
.bSlaveInterface0
= status
;
666 acm_call_mgmt_descriptor
.bDataInterface
= status
;
670 /* allocate instance-specific endpoints */
671 ep
= usb_ep_autoconfig(cdev
->gadget
, &acm_fs_in_desc
);
675 ep
->driver_data
= cdev
; /* claim */
677 ep
= usb_ep_autoconfig(cdev
->gadget
, &acm_fs_out_desc
);
681 ep
->driver_data
= cdev
; /* claim */
683 ep
= usb_ep_autoconfig(cdev
->gadget
, &acm_fs_notify_desc
);
687 ep
->driver_data
= cdev
; /* claim */
689 /* allocate notification */
690 acm
->notify_req
= gs_alloc_req(ep
,
691 sizeof(struct usb_cdc_notification
) + 2,
693 if (!acm
->notify_req
)
696 acm
->notify_req
->complete
= acm_cdc_notify_complete
;
697 acm
->notify_req
->context
= acm
;
699 /* support all relevant hardware speeds... we expect that when
700 * hardware is dual speed, all bulk-capable endpoints work at
703 acm_hs_in_desc
.bEndpointAddress
= acm_fs_in_desc
.bEndpointAddress
;
704 acm_hs_out_desc
.bEndpointAddress
= acm_fs_out_desc
.bEndpointAddress
;
705 acm_hs_notify_desc
.bEndpointAddress
=
706 acm_fs_notify_desc
.bEndpointAddress
;
708 acm_ss_in_desc
.bEndpointAddress
= acm_fs_in_desc
.bEndpointAddress
;
709 acm_ss_out_desc
.bEndpointAddress
= acm_fs_out_desc
.bEndpointAddress
;
711 status
= usb_assign_descriptors(f
, acm_fs_function
, acm_hs_function
,
716 pr_debug("[XLOG_INFO][USB_ACM]%s: ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n", \
717 __func__
, acm
->port_num
, \
718 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" : \
719 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full", \
720 acm
->port
.in
->name
, acm
->port
.out
->name
, acm
->notify
->name
);
722 DBG(cdev
, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
724 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
725 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
726 acm
->port
.in
->name
, acm
->port
.out
->name
,
732 gs_free_req(acm
->notify
, acm
->notify_req
);
734 /* we might as well release our claims on endpoints */
736 acm
->notify
->driver_data
= NULL
;
738 acm
->port
.out
->driver_data
= NULL
;
740 acm
->port
.in
->driver_data
= NULL
;
742 ERROR(cdev
, "%s/%p: can't bind, err %d\n", f
->name
, f
, status
);
747 static void acm_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
749 struct f_acm
*acm
= func_to_acm(f
);
751 acm_string_defs
[0].id
= 0;
752 usb_free_all_descriptors(f
);
754 gs_free_req(acm
->notify
, acm
->notify_req
);
757 static void acm_free_func(struct usb_function
*f
)
759 struct f_acm
*acm
= func_to_acm(f
);
764 static struct usb_function
*acm_alloc_func(struct usb_function_instance
*fi
)
766 struct f_serial_opts
*opts
;
769 acm
= kzalloc(sizeof(*acm
), GFP_KERNEL
);
771 return ERR_PTR(-ENOMEM
);
773 spin_lock_init(&acm
->lock
);
775 acm
->port
.connect
= acm_connect
;
776 acm
->port
.disconnect
= acm_disconnect
;
777 acm
->port
.send_break
= acm_send_break
;
779 acm
->port
.func
.name
= "acm";
780 acm
->port
.func
.strings
= acm_strings
;
781 /* descriptors are per-instance copies */
782 acm
->port
.func
.bind
= acm_bind
;
783 acm
->port
.func
.set_alt
= acm_set_alt
;
784 acm
->port
.func
.setup
= acm_setup
;
785 acm
->port
.func
.disable
= acm_disable
;
787 opts
= container_of(fi
, struct f_serial_opts
, func_inst
);
788 acm
->port_num
= opts
->port_num
;
789 acm
->port
.func
.unbind
= acm_unbind
;
790 acm
->port
.func
.free_func
= acm_free_func
;
792 return &acm
->port
.func
;
795 static inline struct f_serial_opts
*to_f_serial_opts(struct config_item
*item
)
797 return container_of(to_config_group(item
), struct f_serial_opts
,
801 CONFIGFS_ATTR_STRUCT(f_serial_opts
);
802 static ssize_t
f_acm_attr_show(struct config_item
*item
,
803 struct configfs_attribute
*attr
,
806 struct f_serial_opts
*opts
= to_f_serial_opts(item
);
807 struct f_serial_opts_attribute
*f_serial_opts_attr
=
808 container_of(attr
, struct f_serial_opts_attribute
, attr
);
811 if (f_serial_opts_attr
->show
)
812 ret
= f_serial_opts_attr
->show(opts
, page
);
816 static void acm_attr_release(struct config_item
*item
)
818 struct f_serial_opts
*opts
= to_f_serial_opts(item
);
820 usb_put_function_instance(&opts
->func_inst
);
823 static struct configfs_item_operations acm_item_ops
= {
824 .release
= acm_attr_release
,
825 .show_attribute
= f_acm_attr_show
,
828 static ssize_t
f_acm_port_num_show(struct f_serial_opts
*opts
, char *page
)
830 return sprintf(page
, "%u\n", opts
->port_num
);
833 static struct f_serial_opts_attribute f_acm_port_num
=
834 __CONFIGFS_ATTR_RO(port_num
, f_acm_port_num_show
);
837 static struct configfs_attribute
*acm_attrs
[] = {
838 &f_acm_port_num
.attr
,
842 static struct config_item_type acm_func_type
= {
843 .ct_item_ops
= &acm_item_ops
,
844 .ct_attrs
= acm_attrs
,
845 .ct_owner
= THIS_MODULE
,
848 static void acm_free_instance(struct usb_function_instance
*fi
)
850 struct f_serial_opts
*opts
;
852 opts
= container_of(fi
, struct f_serial_opts
, func_inst
);
853 gserial_free_line(opts
->port_num
);
857 static struct usb_function_instance
*acm_alloc_instance(void)
859 struct f_serial_opts
*opts
;
862 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
864 return ERR_PTR(-ENOMEM
);
865 opts
->func_inst
.free_func_inst
= acm_free_instance
;
866 ret
= gserial_alloc_line(&opts
->port_num
);
871 config_group_init_type_name(&opts
->func_inst
.group
, "",
873 return &opts
->func_inst
;
875 DECLARE_USB_FUNCTION_INIT(acm
, acm_alloc_instance
, acm_alloc_func
);
876 MODULE_LICENSE("GPL");