blob: be9df09fde26508c6b658cd0686681fb43e1d4bb [file] [log] [blame]
David Brownell4d5a73d2008-06-19 18:18:40 -07001/*
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
Michal Nazarewiczb97503f2009-10-28 16:57:30 +01007 * Copyright (C) 2009 by Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01008 * Author: Michal Nazarewicz (mina86@mina86.com)
David Brownell4d5a73d2008-06-19 18:18:40 -07009 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070018#include <linux/kernel.h>
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +010019#include <linux/module.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070020#include <linux/device.h>
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +010021#include <linux/err.h>
David Brownell4d5a73d2008-06-19 18:18:40 -070022
23#include "u_serial.h"
David Brownell4d5a73d2008-06-19 18:18:40 -070024
25
26/*
27 * This CDC ACM function support just wraps control functions and
28 * notifications around the generic serial-over-usb code.
29 *
30 * Because CDC ACM is standardized by the USB-IF, many host operating
31 * systems have drivers for it. Accordingly, ACM is the preferred
32 * interop solution for serial-port type connections. The control
33 * models are often not necessary, and in any case don't do much in
34 * this bare-bones implementation.
35 *
36 * Note that even MS-Windows has some support for ACM. However, that
37 * support is somewhat broken because when you use ACM in a composite
38 * device, having multiple interfaces confuses the poor OS. It doesn't
39 * seem to understand CDC Union descriptors. The new "association"
40 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
41 */
42
David Brownell4d5a73d2008-06-19 18:18:40 -070043struct f_acm {
44 struct gserial port;
45 u8 ctrl_id, data_id;
46 u8 port_num;
47
David Brownell1f1ba112008-08-06 18:49:57 -070048 u8 pending;
49
50 /* lock is mostly for pending and notify_req ... they get accessed
51 * by callbacks both from tty (open/close/break) under its spinlock,
52 * and notify_req.complete() which can't use that lock.
53 */
54 spinlock_t lock;
55
David Brownell4d5a73d2008-06-19 18:18:40 -070056 struct usb_ep *notify;
David Brownell1f1ba112008-08-06 18:49:57 -070057 struct usb_request *notify_req;
David Brownell4d5a73d2008-06-19 18:18:40 -070058
59 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
David Brownell1f1ba112008-08-06 18:49:57 -070060
61 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
David Brownell4d5a73d2008-06-19 18:18:40 -070062 u16 port_handshake_bits;
David Brownell1f1ba112008-08-06 18:49:57 -070063#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
64#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
65
66 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
67 u16 serial_state;
68#define ACM_CTRL_OVERRUN (1 << 6)
69#define ACM_CTRL_PARITY (1 << 5)
70#define ACM_CTRL_FRAMING (1 << 4)
71#define ACM_CTRL_RI (1 << 3)
72#define ACM_CTRL_BRK (1 << 2)
73#define ACM_CTRL_DSR (1 << 1)
74#define ACM_CTRL_DCD (1 << 0)
David Brownell4d5a73d2008-06-19 18:18:40 -070075};
76
77static inline struct f_acm *func_to_acm(struct usb_function *f)
78{
79 return container_of(f, struct f_acm, port.func);
80}
81
David Brownell1f1ba112008-08-06 18:49:57 -070082static inline struct f_acm *port_to_acm(struct gserial *p)
83{
84 return container_of(p, struct f_acm, port);
85}
86
David Brownell4d5a73d2008-06-19 18:18:40 -070087/*-------------------------------------------------------------------------*/
88
89/* notification endpoint uses smallish and infrequent fixed-size messages */
90
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +020091#define GS_NOTIFY_INTERVAL_MS 32
David Brownell1f1ba112008-08-06 18:49:57 -070092#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
David Brownell4d5a73d2008-06-19 18:18:40 -070093
94/* interface and class descriptors: */
95
Michal Nazarewiczb97503f2009-10-28 16:57:30 +010096static struct usb_interface_assoc_descriptor
97acm_iad_descriptor = {
98 .bLength = sizeof acm_iad_descriptor,
99 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
100
101 /* .bFirstInterface = DYNAMIC, */
102 .bInterfaceCount = 2, // control + data
103 .bFunctionClass = USB_CLASS_COMM,
104 .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530105 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100106 /* .iFunction = DYNAMIC */
107};
108
109
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200110static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700111 .bLength = USB_DT_INTERFACE_SIZE,
112 .bDescriptorType = USB_DT_INTERFACE,
113 /* .bInterfaceNumber = DYNAMIC */
114 .bNumEndpoints = 1,
115 .bInterfaceClass = USB_CLASS_COMM,
116 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
117 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
118 /* .iInterface = DYNAMIC */
119};
120
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200121static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700122 .bLength = USB_DT_INTERFACE_SIZE,
123 .bDescriptorType = USB_DT_INTERFACE,
124 /* .bInterfaceNumber = DYNAMIC */
125 .bNumEndpoints = 2,
126 .bInterfaceClass = USB_CLASS_CDC_DATA,
127 .bInterfaceSubClass = 0,
128 .bInterfaceProtocol = 0,
129 /* .iInterface = DYNAMIC */
130};
131
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200132static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700133 .bLength = sizeof(acm_header_desc),
134 .bDescriptorType = USB_DT_CS_INTERFACE,
135 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800136 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700137};
138
139static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200140acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700141 .bLength = sizeof(acm_call_mgmt_descriptor),
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
144 .bmCapabilities = 0,
145 /* .bDataInterface = DYNAMIC */
146};
147
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200148static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700149 .bLength = sizeof(acm_descriptor),
150 .bDescriptorType = USB_DT_CS_INTERFACE,
151 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700152 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700153};
154
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200155static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700156 .bLength = sizeof(acm_union_desc),
157 .bDescriptorType = USB_DT_CS_INTERFACE,
158 .bDescriptorSubType = USB_CDC_UNION_TYPE,
159 /* .bMasterInterface0 = DYNAMIC */
160 /* .bSlaveInterface0 = DYNAMIC */
161};
162
163/* full speed support: */
164
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200165static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700166 .bLength = USB_DT_ENDPOINT_SIZE,
167 .bDescriptorType = USB_DT_ENDPOINT,
168 .bEndpointAddress = USB_DIR_IN,
169 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800170 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200171 .bInterval = GS_NOTIFY_INTERVAL_MS,
David Brownell4d5a73d2008-06-19 18:18:40 -0700172};
173
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200174static struct usb_endpoint_descriptor acm_fs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_IN,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179};
180
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200181static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700182 .bLength = USB_DT_ENDPOINT_SIZE,
183 .bDescriptorType = USB_DT_ENDPOINT,
184 .bEndpointAddress = USB_DIR_OUT,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
186};
187
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200188static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100189 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700190 (struct usb_descriptor_header *) &acm_control_interface_desc,
191 (struct usb_descriptor_header *) &acm_header_desc,
192 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
193 (struct usb_descriptor_header *) &acm_descriptor,
194 (struct usb_descriptor_header *) &acm_union_desc,
195 (struct usb_descriptor_header *) &acm_fs_notify_desc,
196 (struct usb_descriptor_header *) &acm_data_interface_desc,
197 (struct usb_descriptor_header *) &acm_fs_in_desc,
198 (struct usb_descriptor_header *) &acm_fs_out_desc,
199 NULL,
200};
201
202/* high speed support: */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200203static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700204 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800208 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200209 .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
David Brownell4d5a73d2008-06-19 18:18:40 -0700210};
211
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200212static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700213 .bLength = USB_DT_ENDPOINT_SIZE,
214 .bDescriptorType = USB_DT_ENDPOINT,
215 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800216 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700217};
218
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200219static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700220 .bLength = USB_DT_ENDPOINT_SIZE,
221 .bDescriptorType = USB_DT_ENDPOINT,
222 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800223 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700224};
225
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200226static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100227 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700228 (struct usb_descriptor_header *) &acm_control_interface_desc,
229 (struct usb_descriptor_header *) &acm_header_desc,
230 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
231 (struct usb_descriptor_header *) &acm_descriptor,
232 (struct usb_descriptor_header *) &acm_union_desc,
233 (struct usb_descriptor_header *) &acm_hs_notify_desc,
234 (struct usb_descriptor_header *) &acm_data_interface_desc,
235 (struct usb_descriptor_header *) &acm_hs_in_desc,
236 (struct usb_descriptor_header *) &acm_hs_out_desc,
237 NULL,
238};
239
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100240static struct usb_endpoint_descriptor acm_ss_in_desc = {
241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
243 .bmAttributes = USB_ENDPOINT_XFER_BULK,
244 .wMaxPacketSize = cpu_to_le16(1024),
245};
246
247static struct usb_endpoint_descriptor acm_ss_out_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
254static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
255 .bLength = sizeof acm_ss_bulk_comp_desc,
256 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
257};
258
259static struct usb_descriptor_header *acm_ss_function[] = {
260 (struct usb_descriptor_header *) &acm_iad_descriptor,
261 (struct usb_descriptor_header *) &acm_control_interface_desc,
262 (struct usb_descriptor_header *) &acm_header_desc,
263 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
264 (struct usb_descriptor_header *) &acm_descriptor,
265 (struct usb_descriptor_header *) &acm_union_desc,
266 (struct usb_descriptor_header *) &acm_hs_notify_desc,
267 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
268 (struct usb_descriptor_header *) &acm_data_interface_desc,
269 (struct usb_descriptor_header *) &acm_ss_in_desc,
270 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
271 (struct usb_descriptor_header *) &acm_ss_out_desc,
272 (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
273 NULL,
274};
275
David Brownell4d5a73d2008-06-19 18:18:40 -0700276/* string descriptors: */
277
278#define ACM_CTRL_IDX 0
279#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100280#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700281
282/* static strings, in UTF-8 */
283static struct usb_string acm_string_defs[] = {
284 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
285 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100286 [ACM_IAD_IDX ].s = "CDC Serial",
Graham Williamsd2572212013-08-28 16:36:14 -0700287 { } /* end of list */
David Brownell4d5a73d2008-06-19 18:18:40 -0700288};
289
290static struct usb_gadget_strings acm_string_table = {
291 .language = 0x0409, /* en-us */
292 .strings = acm_string_defs,
293};
294
295static struct usb_gadget_strings *acm_strings[] = {
296 &acm_string_table,
297 NULL,
298};
299
300/*-------------------------------------------------------------------------*/
301
302/* ACM control ... data handling is delegated to tty library code.
303 * The main task of this function is to activate and deactivate
304 * that code based on device state; track parameters like line
305 * speed, handshake state, and so on; and issue notifications.
306 */
307
308static void acm_complete_set_line_coding(struct usb_ep *ep,
309 struct usb_request *req)
310{
311 struct f_acm *acm = ep->driver_data;
312 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
313
314 if (req->status != 0) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200315 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d completion, err %d\n",
316 acm->port_num, req->status);
David Brownell4d5a73d2008-06-19 18:18:40 -0700317 return;
318 }
319
320 /* normal completion */
321 if (req->actual != sizeof(acm->port_line_coding)) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200322 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d short resp, len %d\n",
323 acm->port_num, req->actual);
David Brownell4d5a73d2008-06-19 18:18:40 -0700324 usb_ep_set_halt(ep);
325 } else {
326 struct usb_cdc_line_coding *value = req->buf;
327
328 /* REVISIT: we currently just remember this data.
329 * If we change that, (a) validate it first, then
330 * (b) update whatever hardware needs updating,
331 * (c) worry about locking. This is information on
332 * the order of 9600-8-N-1 ... most of which means
333 * nothing unless we control a real RS232 line.
334 */
335 acm->port_line_coding = *value;
336 }
337}
338
339static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
340{
341 struct f_acm *acm = func_to_acm(f);
342 struct usb_composite_dev *cdev = f->config->cdev;
343 struct usb_request *req = cdev->req;
344 int value = -EOPNOTSUPP;
345 u16 w_index = le16_to_cpu(ctrl->wIndex);
346 u16 w_value = le16_to_cpu(ctrl->wValue);
347 u16 w_length = le16_to_cpu(ctrl->wLength);
348
349 /* composite driver infrastructure handles everything except
350 * CDC class messages; interface activation uses set_alt().
David Brownell1f1ba112008-08-06 18:49:57 -0700351 *
352 * Note CDC spec table 4 lists the ACM request profile. It requires
353 * encapsulated command support ... we don't handle any, and respond
354 * to them by stalling. Options include get/set/clear comm features
355 * (not that useful) and SEND_BREAK.
David Brownell4d5a73d2008-06-19 18:18:40 -0700356 */
357 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
358
359 /* SET_LINE_CODING ... just read and save what the host sends */
360 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
361 | USB_CDC_REQ_SET_LINE_CODING:
362 if (w_length != sizeof(struct usb_cdc_line_coding)
363 || w_index != acm->ctrl_id)
364 goto invalid;
365
366 value = w_length;
367 cdev->gadget->ep0->driver_data = acm;
368 req->complete = acm_complete_set_line_coding;
369 break;
370
371 /* GET_LINE_CODING ... return what host sent, or initial value */
372 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
373 | USB_CDC_REQ_GET_LINE_CODING:
374 if (w_index != acm->ctrl_id)
375 goto invalid;
376
377 value = min_t(unsigned, w_length,
378 sizeof(struct usb_cdc_line_coding));
379 memcpy(req->buf, &acm->port_line_coding, value);
380 break;
381
382 /* SET_CONTROL_LINE_STATE ... save what the host sent */
383 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
384 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
385 if (w_index != acm->ctrl_id)
386 goto invalid;
387
388 value = 0;
389
390 /* FIXME we should not allow data to flow until the
David Brownell1f1ba112008-08-06 18:49:57 -0700391 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700392 * that bit, we should return to that no-flow state.
393 */
394 acm->port_handshake_bits = w_value;
395 break;
396
397 default:
398invalid:
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200399 dev_vdbg(&cdev->gadget->dev,
400 "invalid control req%02x.%02x v%04x i%04x l%d\n",
401 ctrl->bRequestType, ctrl->bRequest,
402 w_value, w_index, w_length);
David Brownell4d5a73d2008-06-19 18:18:40 -0700403 }
404
405 /* respond with data transfer or status phase? */
406 if (value >= 0) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200407 dev_dbg(&cdev->gadget->dev,
408 "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
David Brownell4d5a73d2008-06-19 18:18:40 -0700409 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
410 w_value, w_index, w_length);
411 req->zero = 0;
412 req->length = value;
413 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
414 if (value < 0)
415 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
416 acm->port_num, value);
417 }
418
419 /* device either stalls (value < 0) or reports success */
420 return value;
421}
422
423static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
424{
425 struct f_acm *acm = func_to_acm(f);
426 struct usb_composite_dev *cdev = f->config->cdev;
427
428 /* we know alt == 0, so this is an activation or a reset */
429
430 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700431 if (acm->notify->driver_data) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200432 dev_vdbg(&cdev->gadget->dev,
433 "reset acm control interface %d\n", intf);
David Brownell4d5a73d2008-06-19 18:18:40 -0700434 usb_ep_disable(acm->notify);
Felipe Balbi52ec49a2014-09-29 13:35:54 -0500435 }
436
437 if (!acm->notify->desc)
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300438 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
439 return -EINVAL;
Felipe Balbi52ec49a2014-09-29 13:35:54 -0500440
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300441 usb_ep_enable(acm->notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700442 acm->notify->driver_data = acm;
443
444 } else if (intf == acm->data_id) {
445 if (acm->port.in->driver_data) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200446 dev_dbg(&cdev->gadget->dev,
447 "reset acm ttyGS%d\n", acm->port_num);
David Brownell4d5a73d2008-06-19 18:18:40 -0700448 gserial_disconnect(&acm->port);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300449 }
450 if (!acm->port.in->desc || !acm->port.out->desc) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200451 dev_dbg(&cdev->gadget->dev,
452 "activate acm ttyGS%d\n", acm->port_num);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300453 if (config_ep_by_speed(cdev->gadget, f,
454 acm->port.in) ||
455 config_ep_by_speed(cdev->gadget, f,
456 acm->port.out)) {
457 acm->port.in->desc = NULL;
458 acm->port.out->desc = NULL;
459 return -EINVAL;
460 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700461 }
462 gserial_connect(&acm->port, acm->port_num);
463
464 } else
465 return -EINVAL;
466
467 return 0;
468}
469
470static void acm_disable(struct usb_function *f)
471{
472 struct f_acm *acm = func_to_acm(f);
473 struct usb_composite_dev *cdev = f->config->cdev;
474
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200475 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d deactivated\n", acm->port_num);
David Brownell4d5a73d2008-06-19 18:18:40 -0700476 gserial_disconnect(&acm->port);
477 usb_ep_disable(acm->notify);
478 acm->notify->driver_data = NULL;
479}
480
481/*-------------------------------------------------------------------------*/
482
David Brownell1f1ba112008-08-06 18:49:57 -0700483/**
484 * acm_cdc_notify - issue CDC notification to host
485 * @acm: wraps host to be notified
486 * @type: notification type
487 * @value: Refer to cdc specs, wValue field.
488 * @data: data to be sent
489 * @length: size of data
490 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
491 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200492 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700493 *
494 * See section 6.3.5 of the CDC 1.1 specification for information
495 * about the only notification we issue: SerialState change.
496 */
497static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
498 void *data, unsigned length)
499{
500 struct usb_ep *ep = acm->notify;
501 struct usb_request *req;
502 struct usb_cdc_notification *notify;
503 const unsigned len = sizeof(*notify) + length;
504 void *buf;
505 int status;
506
507 req = acm->notify_req;
508 acm->notify_req = NULL;
509 acm->pending = false;
510
511 req->length = len;
512 notify = req->buf;
513 buf = notify + 1;
514
515 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
516 | USB_RECIP_INTERFACE;
517 notify->bNotificationType = type;
518 notify->wValue = cpu_to_le16(value);
519 notify->wIndex = cpu_to_le16(acm->ctrl_id);
520 notify->wLength = cpu_to_le16(length);
521 memcpy(buf, data, length);
522
David Brownelle50ae572008-11-12 11:35:13 -0800523 /* ep_queue() can complete immediately if it fills the fifo... */
524 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700525 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800526 spin_lock(&acm->lock);
527
David Brownell1f1ba112008-08-06 18:49:57 -0700528 if (status < 0) {
529 ERROR(acm->port.func.config->cdev,
530 "acm ttyGS%d can't notify serial state, %d\n",
531 acm->port_num, status);
532 acm->notify_req = req;
533 }
534
535 return status;
536}
537
538static int acm_notify_serial_state(struct f_acm *acm)
539{
540 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
541 int status;
542
543 spin_lock(&acm->lock);
544 if (acm->notify_req) {
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200545 dev_dbg(&cdev->gadget->dev, "acm ttyGS%d serial state %04x\n",
546 acm->port_num, acm->serial_state);
David Brownell1f1ba112008-08-06 18:49:57 -0700547 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
548 0, &acm->serial_state, sizeof(acm->serial_state));
549 } else {
550 acm->pending = true;
551 status = 0;
552 }
553 spin_unlock(&acm->lock);
554 return status;
555}
556
557static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
558{
559 struct f_acm *acm = req->context;
560 u8 doit = false;
561
562 /* on this call path we do NOT hold the port spinlock,
563 * which is why ACM needs its own spinlock
564 */
565 spin_lock(&acm->lock);
566 if (req->status != -ESHUTDOWN)
567 doit = acm->pending;
568 acm->notify_req = req;
569 spin_unlock(&acm->lock);
570
571 if (doit)
572 acm_notify_serial_state(acm);
573}
574
575/* connect == the TTY link is open */
576
577static void acm_connect(struct gserial *port)
578{
579 struct f_acm *acm = port_to_acm(port);
580
581 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
582 acm_notify_serial_state(acm);
583}
584
585static void acm_disconnect(struct gserial *port)
586{
587 struct f_acm *acm = port_to_acm(port);
588
589 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
590 acm_notify_serial_state(acm);
591}
592
593static int acm_send_break(struct gserial *port, int duration)
594{
595 struct f_acm *acm = port_to_acm(port);
596 u16 state;
597
598 state = acm->serial_state;
599 state &= ~ACM_CTRL_BRK;
600 if (duration)
601 state |= ACM_CTRL_BRK;
602
603 acm->serial_state = state;
604 return acm_notify_serial_state(acm);
605}
606
607/*-------------------------------------------------------------------------*/
608
David Brownell4d5a73d2008-06-19 18:18:40 -0700609/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200610static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700611acm_bind(struct usb_configuration *c, struct usb_function *f)
612{
613 struct usb_composite_dev *cdev = c->cdev;
614 struct f_acm *acm = func_to_acm(f);
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +0100615 struct usb_string *us;
David Brownell4d5a73d2008-06-19 18:18:40 -0700616 int status;
617 struct usb_ep *ep;
618
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100619 /* REVISIT might want instance-specific strings to help
620 * distinguish instances ...
621 */
622
623 /* maybe allocate device-global string IDs, and patch descriptors */
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +0100624 us = usb_gstrings_attach(cdev, acm_strings,
625 ARRAY_SIZE(acm_string_defs));
626 if (IS_ERR(us))
627 return PTR_ERR(us);
628 acm_control_interface_desc.iInterface = us[ACM_CTRL_IDX].id;
629 acm_data_interface_desc.iInterface = us[ACM_DATA_IDX].id;
630 acm_iad_descriptor.iFunction = us[ACM_IAD_IDX].id;
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100631
David Brownell4d5a73d2008-06-19 18:18:40 -0700632 /* allocate instance-specific interface IDs, and patch descriptors */
633 status = usb_interface_id(c, f);
634 if (status < 0)
635 goto fail;
636 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100637 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700638
639 acm_control_interface_desc.bInterfaceNumber = status;
640 acm_union_desc .bMasterInterface0 = status;
641
642 status = usb_interface_id(c, f);
643 if (status < 0)
644 goto fail;
645 acm->data_id = status;
646
647 acm_data_interface_desc.bInterfaceNumber = status;
648 acm_union_desc.bSlaveInterface0 = status;
649 acm_call_mgmt_descriptor.bDataInterface = status;
650
651 status = -ENODEV;
652
653 /* allocate instance-specific endpoints */
654 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
655 if (!ep)
656 goto fail;
657 acm->port.in = ep;
658 ep->driver_data = cdev; /* claim */
659
660 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
661 if (!ep)
662 goto fail;
663 acm->port.out = ep;
664 ep->driver_data = cdev; /* claim */
665
666 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
667 if (!ep)
668 goto fail;
669 acm->notify = ep;
670 ep->driver_data = cdev; /* claim */
671
David Brownell1f1ba112008-08-06 18:49:57 -0700672 /* allocate notification */
673 acm->notify_req = gs_alloc_req(ep,
674 sizeof(struct usb_cdc_notification) + 2,
675 GFP_KERNEL);
676 if (!acm->notify_req)
677 goto fail;
678
679 acm->notify_req->complete = acm_cdc_notify_complete;
680 acm->notify_req->context = acm;
681
David Brownell4d5a73d2008-06-19 18:18:40 -0700682 /* support all relevant hardware speeds... we expect that when
683 * hardware is dual speed, all bulk-capable endpoints work at
684 * both speeds
685 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200686 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
687 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
688 acm_hs_notify_desc.bEndpointAddress =
689 acm_fs_notify_desc.bEndpointAddress;
David Brownell4d5a73d2008-06-19 18:18:40 -0700690
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200691 acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
692 acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100693
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200694 status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
695 acm_ss_function);
696 if (status)
697 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700698
Richard Leitnerb8b0ea52014-08-21 08:31:39 +0200699 dev_dbg(&cdev->gadget->dev,
700 "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
701 acm->port_num,
702 gadget_is_superspeed(c->cdev->gadget) ? "super" :
703 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
704 acm->port.in->name, acm->port.out->name,
705 acm->notify->name);
David Brownell4d5a73d2008-06-19 18:18:40 -0700706 return 0;
707
708fail:
David Brownell1f1ba112008-08-06 18:49:57 -0700709 if (acm->notify_req)
710 gs_free_req(acm->notify, acm->notify_req);
711
David Brownell4d5a73d2008-06-19 18:18:40 -0700712 /* we might as well release our claims on endpoints */
713 if (acm->notify)
714 acm->notify->driver_data = NULL;
715 if (acm->port.out)
716 acm->port.out->driver_data = NULL;
717 if (acm->port.in)
718 acm->port.in->driver_data = NULL;
719
720 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
721
722 return status;
723}
724
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100725static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
726{
727 struct f_acm *acm = func_to_acm(f);
728
729 acm_string_defs[0].id = 0;
730 usb_free_all_descriptors(f);
731 if (acm->notify_req)
732 gs_free_req(acm->notify, acm->notify_req);
733}
734
735static void acm_free_func(struct usb_function *f)
736{
737 struct f_acm *acm = func_to_acm(f);
738
739 kfree(acm);
740}
741
742static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
743{
744 struct f_serial_opts *opts;
745 struct f_acm *acm;
746
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100747 acm = kzalloc(sizeof(*acm), GFP_KERNEL);
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100748 if (!acm)
749 return ERR_PTR(-ENOMEM);
750
Sebastian Andrzej Siewior15761822013-01-25 14:09:17 +0100751 spin_lock_init(&acm->lock);
752
753 acm->port.connect = acm_connect;
754 acm->port.disconnect = acm_disconnect;
755 acm->port.send_break = acm_send_break;
756
757 acm->port.func.name = "acm";
758 acm->port.func.strings = acm_strings;
759 /* descriptors are per-instance copies */
760 acm->port.func.bind = acm_bind;
761 acm->port.func.set_alt = acm_set_alt;
762 acm->port.func.setup = acm_setup;
763 acm->port.func.disable = acm_disable;
764
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100765 opts = container_of(fi, struct f_serial_opts, func_inst);
766 acm->port_num = opts->port_num;
767 acm->port.func.unbind = acm_unbind;
768 acm->port.func.free_func = acm_free_func;
769
770 return &acm->port.func;
771}
772
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100773static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
774{
775 return container_of(to_config_group(item), struct f_serial_opts,
776 func_inst.group);
777}
778
779CONFIGFS_ATTR_STRUCT(f_serial_opts);
780static ssize_t f_acm_attr_show(struct config_item *item,
781 struct configfs_attribute *attr,
782 char *page)
783{
784 struct f_serial_opts *opts = to_f_serial_opts(item);
785 struct f_serial_opts_attribute *f_serial_opts_attr =
786 container_of(attr, struct f_serial_opts_attribute, attr);
787 ssize_t ret = 0;
788
789 if (f_serial_opts_attr->show)
790 ret = f_serial_opts_attr->show(opts, page);
791 return ret;
792}
793
794static void acm_attr_release(struct config_item *item)
795{
796 struct f_serial_opts *opts = to_f_serial_opts(item);
797
798 usb_put_function_instance(&opts->func_inst);
799}
800
801static struct configfs_item_operations acm_item_ops = {
802 .release = acm_attr_release,
803 .show_attribute = f_acm_attr_show,
804};
805
806static ssize_t f_acm_port_num_show(struct f_serial_opts *opts, char *page)
807{
808 return sprintf(page, "%u\n", opts->port_num);
809}
810
811static struct f_serial_opts_attribute f_acm_port_num =
812 __CONFIGFS_ATTR_RO(port_num, f_acm_port_num_show);
813
814
815static struct configfs_attribute *acm_attrs[] = {
816 &f_acm_port_num.attr,
817 NULL,
818};
819
820static struct config_item_type acm_func_type = {
821 .ct_item_ops = &acm_item_ops,
822 .ct_attrs = acm_attrs,
823 .ct_owner = THIS_MODULE,
824};
825
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100826static void acm_free_instance(struct usb_function_instance *fi)
827{
828 struct f_serial_opts *opts;
829
830 opts = container_of(fi, struct f_serial_opts, func_inst);
Sebastian Andrzej Siewiorc4ed4ac2012-12-23 21:10:18 +0100831 gserial_free_line(opts->port_num);
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100832 kfree(opts);
833}
834
835static struct usb_function_instance *acm_alloc_instance(void)
836{
837 struct f_serial_opts *opts;
Sebastian Andrzej Siewiorc4ed4ac2012-12-23 21:10:18 +0100838 int ret;
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100839
840 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
841 if (!opts)
842 return ERR_PTR(-ENOMEM);
843 opts->func_inst.free_func_inst = acm_free_instance;
Sebastian Andrzej Siewiorc4ed4ac2012-12-23 21:10:18 +0100844 ret = gserial_alloc_line(&opts->port_num);
845 if (ret) {
846 kfree(opts);
847 return ERR_PTR(ret);
848 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +0100849 config_group_init_type_name(&opts->func_inst.group, "",
850 &acm_func_type);
Sebastian Andrzej Siewiorff47f592012-12-23 21:10:07 +0100851 return &opts->func_inst;
852}
853DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
854MODULE_LICENSE("GPL");