blob: 308242b5d6e0ab4a9c02ea00798f2edada1421a1 [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>
19#include <linux/device.h>
20
21#include "u_serial.h"
22#include "gadget_chips.h"
23
24
25/*
26 * This CDC ACM function support just wraps control functions and
27 * notifications around the generic serial-over-usb code.
28 *
29 * Because CDC ACM is standardized by the USB-IF, many host operating
30 * systems have drivers for it. Accordingly, ACM is the preferred
31 * interop solution for serial-port type connections. The control
32 * models are often not necessary, and in any case don't do much in
33 * this bare-bones implementation.
34 *
35 * Note that even MS-Windows has some support for ACM. However, that
36 * support is somewhat broken because when you use ACM in a composite
37 * device, having multiple interfaces confuses the poor OS. It doesn't
38 * seem to understand CDC Union descriptors. The new "association"
39 * descriptors (roughly equivalent to CDC Unions) may sometimes help.
40 */
41
David Brownell4d5a73d2008-06-19 18:18:40 -070042struct f_acm {
43 struct gserial port;
44 u8 ctrl_id, data_id;
45 u8 port_num;
46
David Brownell1f1ba112008-08-06 18:49:57 -070047 u8 pending;
48
49 /* lock is mostly for pending and notify_req ... they get accessed
50 * by callbacks both from tty (open/close/break) under its spinlock,
51 * and notify_req.complete() which can't use that lock.
52 */
53 spinlock_t lock;
54
David Brownell4d5a73d2008-06-19 18:18:40 -070055 struct usb_ep *notify;
David Brownell1f1ba112008-08-06 18:49:57 -070056 struct usb_request *notify_req;
David Brownell4d5a73d2008-06-19 18:18:40 -070057
58 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
David Brownell1f1ba112008-08-06 18:49:57 -070059
60 /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
David Brownell4d5a73d2008-06-19 18:18:40 -070061 u16 port_handshake_bits;
David Brownell1f1ba112008-08-06 18:49:57 -070062#define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
63#define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
64
65 /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
66 u16 serial_state;
67#define ACM_CTRL_OVERRUN (1 << 6)
68#define ACM_CTRL_PARITY (1 << 5)
69#define ACM_CTRL_FRAMING (1 << 4)
70#define ACM_CTRL_RI (1 << 3)
71#define ACM_CTRL_BRK (1 << 2)
72#define ACM_CTRL_DSR (1 << 1)
73#define ACM_CTRL_DCD (1 << 0)
David Brownell4d5a73d2008-06-19 18:18:40 -070074};
75
76static inline struct f_acm *func_to_acm(struct usb_function *f)
77{
78 return container_of(f, struct f_acm, port.func);
79}
80
David Brownell1f1ba112008-08-06 18:49:57 -070081static inline struct f_acm *port_to_acm(struct gserial *p)
82{
83 return container_of(p, struct f_acm, port);
84}
85
David Brownell4d5a73d2008-06-19 18:18:40 -070086/*-------------------------------------------------------------------------*/
87
88/* notification endpoint uses smallish and infrequent fixed-size messages */
89
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +020090#define GS_NOTIFY_INTERVAL_MS 32
David Brownell1f1ba112008-08-06 18:49:57 -070091#define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
David Brownell4d5a73d2008-06-19 18:18:40 -070092
93/* interface and class descriptors: */
94
Michal Nazarewiczb97503f2009-10-28 16:57:30 +010095static struct usb_interface_assoc_descriptor
96acm_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,
Praveena Nadahally5c8db072010-09-10 23:05:03 +0530104 .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100105 /* .iFunction = DYNAMIC */
106};
107
108
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200109static struct usb_interface_descriptor acm_control_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700110 .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
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200120static struct usb_interface_descriptor acm_data_interface_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700121 .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
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200131static struct usb_cdc_header_desc acm_header_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700132 .bLength = sizeof(acm_header_desc),
133 .bDescriptorType = USB_DT_CS_INTERFACE,
134 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800135 .bcdCDC = cpu_to_le16(0x0110),
David Brownell4d5a73d2008-06-19 18:18:40 -0700136};
137
138static struct usb_cdc_call_mgmt_descriptor
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200139acm_call_mgmt_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700140 .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
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200147static struct usb_cdc_acm_descriptor acm_descriptor = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700148 .bLength = sizeof(acm_descriptor),
149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownell1f1ba112008-08-06 18:49:57 -0700151 .bmCapabilities = USB_CDC_CAP_LINE,
David Brownell4d5a73d2008-06-19 18:18:40 -0700152};
153
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200154static struct usb_cdc_union_desc acm_union_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700155 .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
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200164static struct usb_endpoint_descriptor acm_fs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700165 .bLength = USB_DT_ENDPOINT_SIZE,
166 .bDescriptorType = USB_DT_ENDPOINT,
167 .bEndpointAddress = USB_DIR_IN,
168 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800169 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200170 .bInterval = GS_NOTIFY_INTERVAL_MS,
David Brownell4d5a73d2008-06-19 18:18:40 -0700171};
172
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200173static struct usb_endpoint_descriptor acm_fs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700174 .bLength = USB_DT_ENDPOINT_SIZE,
175 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = USB_DIR_IN,
177 .bmAttributes = USB_ENDPOINT_XFER_BULK,
178};
179
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200180static struct usb_endpoint_descriptor acm_fs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700181 .bLength = USB_DT_ENDPOINT_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = USB_DIR_OUT,
184 .bmAttributes = USB_ENDPOINT_XFER_BULK,
185};
186
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200187static struct usb_descriptor_header *acm_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100188 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700189 (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: */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200202static struct usb_endpoint_descriptor acm_hs_notify_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800207 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200208 .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
David Brownell4d5a73d2008-06-19 18:18:40 -0700209};
210
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200211static struct usb_endpoint_descriptor acm_hs_in_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700212 .bLength = USB_DT_ENDPOINT_SIZE,
213 .bDescriptorType = USB_DT_ENDPOINT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800215 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700216};
217
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200218static struct usb_endpoint_descriptor acm_hs_out_desc = {
David Brownell4d5a73d2008-06-19 18:18:40 -0700219 .bLength = USB_DT_ENDPOINT_SIZE,
220 .bDescriptorType = USB_DT_ENDPOINT,
221 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800222 .wMaxPacketSize = cpu_to_le16(512),
David Brownell4d5a73d2008-06-19 18:18:40 -0700223};
224
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200225static struct usb_descriptor_header *acm_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100226 (struct usb_descriptor_header *) &acm_iad_descriptor,
David Brownell4d5a73d2008-06-19 18:18:40 -0700227 (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
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100239static 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
246static 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
253static 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
258static 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
David Brownell4d5a73d2008-06-19 18:18:40 -0700275/* string descriptors: */
276
277#define ACM_CTRL_IDX 0
278#define ACM_DATA_IDX 1
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100279#define ACM_IAD_IDX 2
David Brownell4d5a73d2008-06-19 18:18:40 -0700280
281/* static strings, in UTF-8 */
282static struct usb_string acm_string_defs[] = {
283 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
284 [ACM_DATA_IDX].s = "CDC ACM Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100285 [ACM_IAD_IDX ].s = "CDC Serial",
David Brownell4d5a73d2008-06-19 18:18:40 -0700286 { /* ZEROES END LIST */ },
287};
288
289static struct usb_gadget_strings acm_string_table = {
290 .language = 0x0409, /* en-us */
291 .strings = acm_string_defs,
292};
293
294static 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
307static 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 DBG(cdev, "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 DBG(cdev, "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
338static 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().
David Brownell1f1ba112008-08-06 18:49:57 -0700350 *
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.
David Brownell4d5a73d2008-06-19 18:18:40 -0700355 */
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
David Brownell1f1ba112008-08-06 18:49:57 -0700390 * host sets the ACM_CTRL_DTR bit; and when it clears
David Brownell4d5a73d2008-06-19 18:18:40 -0700391 * that bit, we should return to that no-flow state.
392 */
393 acm->port_handshake_bits = w_value;
394 break;
395
396 default:
397invalid:
398 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
399 ctrl->bRequestType, ctrl->bRequest,
400 w_value, w_index, w_length);
401 }
402
403 /* respond with data transfer or status phase? */
404 if (value >= 0) {
405 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
406 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
407 w_value, w_index, w_length);
408 req->zero = 0;
409 req->length = value;
410 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
411 if (value < 0)
412 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
413 acm->port_num, value);
414 }
415
416 /* device either stalls (value < 0) or reports success */
417 return value;
418}
419
420static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
421{
422 struct f_acm *acm = func_to_acm(f);
423 struct usb_composite_dev *cdev = f->config->cdev;
424
425 /* we know alt == 0, so this is an activation or a reset */
426
427 if (intf == acm->ctrl_id) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700428 if (acm->notify->driver_data) {
429 VDBG(cdev, "reset acm control interface %d\n", intf);
430 usb_ep_disable(acm->notify);
431 } else {
432 VDBG(cdev, "init acm ctrl interface %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300433 if (config_ep_by_speed(cdev->gadget, f, acm->notify))
434 return -EINVAL;
David Brownell4d5a73d2008-06-19 18:18:40 -0700435 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300436 usb_ep_enable(acm->notify);
David Brownell4d5a73d2008-06-19 18:18:40 -0700437 acm->notify->driver_data = acm;
438
439 } else if (intf == acm->data_id) {
440 if (acm->port.in->driver_data) {
441 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
442 gserial_disconnect(&acm->port);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300443 }
444 if (!acm->port.in->desc || !acm->port.out->desc) {
David Brownell4d5a73d2008-06-19 18:18:40 -0700445 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300446 if (config_ep_by_speed(cdev->gadget, f,
447 acm->port.in) ||
448 config_ep_by_speed(cdev->gadget, f,
449 acm->port.out)) {
450 acm->port.in->desc = NULL;
451 acm->port.out->desc = NULL;
452 return -EINVAL;
453 }
David Brownell4d5a73d2008-06-19 18:18:40 -0700454 }
455 gserial_connect(&acm->port, acm->port_num);
456
457 } else
458 return -EINVAL;
459
460 return 0;
461}
462
463static void acm_disable(struct usb_function *f)
464{
465 struct f_acm *acm = func_to_acm(f);
466 struct usb_composite_dev *cdev = f->config->cdev;
467
468 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
469 gserial_disconnect(&acm->port);
470 usb_ep_disable(acm->notify);
471 acm->notify->driver_data = NULL;
472}
473
474/*-------------------------------------------------------------------------*/
475
David Brownell1f1ba112008-08-06 18:49:57 -0700476/**
477 * acm_cdc_notify - issue CDC notification to host
478 * @acm: wraps host to be notified
479 * @type: notification type
480 * @value: Refer to cdc specs, wValue field.
481 * @data: data to be sent
482 * @length: size of data
483 * Context: irqs blocked, acm->lock held, acm_notify_req non-null
484 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200485 * Returns zero on success or a negative errno.
David Brownell1f1ba112008-08-06 18:49:57 -0700486 *
487 * See section 6.3.5 of the CDC 1.1 specification for information
488 * about the only notification we issue: SerialState change.
489 */
490static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
491 void *data, unsigned length)
492{
493 struct usb_ep *ep = acm->notify;
494 struct usb_request *req;
495 struct usb_cdc_notification *notify;
496 const unsigned len = sizeof(*notify) + length;
497 void *buf;
498 int status;
499
500 req = acm->notify_req;
501 acm->notify_req = NULL;
502 acm->pending = false;
503
504 req->length = len;
505 notify = req->buf;
506 buf = notify + 1;
507
508 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
509 | USB_RECIP_INTERFACE;
510 notify->bNotificationType = type;
511 notify->wValue = cpu_to_le16(value);
512 notify->wIndex = cpu_to_le16(acm->ctrl_id);
513 notify->wLength = cpu_to_le16(length);
514 memcpy(buf, data, length);
515
David Brownelle50ae572008-11-12 11:35:13 -0800516 /* ep_queue() can complete immediately if it fills the fifo... */
517 spin_unlock(&acm->lock);
David Brownell1f1ba112008-08-06 18:49:57 -0700518 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle50ae572008-11-12 11:35:13 -0800519 spin_lock(&acm->lock);
520
David Brownell1f1ba112008-08-06 18:49:57 -0700521 if (status < 0) {
522 ERROR(acm->port.func.config->cdev,
523 "acm ttyGS%d can't notify serial state, %d\n",
524 acm->port_num, status);
525 acm->notify_req = req;
526 }
527
528 return status;
529}
530
531static int acm_notify_serial_state(struct f_acm *acm)
532{
533 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
534 int status;
535
536 spin_lock(&acm->lock);
537 if (acm->notify_req) {
538 DBG(cdev, "acm ttyGS%d serial state %04x\n",
539 acm->port_num, acm->serial_state);
540 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
541 0, &acm->serial_state, sizeof(acm->serial_state));
542 } else {
543 acm->pending = true;
544 status = 0;
545 }
546 spin_unlock(&acm->lock);
547 return status;
548}
549
550static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
551{
552 struct f_acm *acm = req->context;
553 u8 doit = false;
554
555 /* on this call path we do NOT hold the port spinlock,
556 * which is why ACM needs its own spinlock
557 */
558 spin_lock(&acm->lock);
559 if (req->status != -ESHUTDOWN)
560 doit = acm->pending;
561 acm->notify_req = req;
562 spin_unlock(&acm->lock);
563
564 if (doit)
565 acm_notify_serial_state(acm);
566}
567
568/* connect == the TTY link is open */
569
570static void acm_connect(struct gserial *port)
571{
572 struct f_acm *acm = port_to_acm(port);
573
574 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
575 acm_notify_serial_state(acm);
576}
577
578static void acm_disconnect(struct gserial *port)
579{
580 struct f_acm *acm = port_to_acm(port);
581
582 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
583 acm_notify_serial_state(acm);
584}
585
586static int acm_send_break(struct gserial *port, int duration)
587{
588 struct f_acm *acm = port_to_acm(port);
589 u16 state;
590
591 state = acm->serial_state;
592 state &= ~ACM_CTRL_BRK;
593 if (duration)
594 state |= ACM_CTRL_BRK;
595
596 acm->serial_state = state;
597 return acm_notify_serial_state(acm);
598}
599
600/*-------------------------------------------------------------------------*/
601
David Brownell4d5a73d2008-06-19 18:18:40 -0700602/* ACM function driver setup/binding */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200603static int
David Brownell4d5a73d2008-06-19 18:18:40 -0700604acm_bind(struct usb_configuration *c, struct usb_function *f)
605{
606 struct usb_composite_dev *cdev = c->cdev;
607 struct f_acm *acm = func_to_acm(f);
608 int status;
609 struct usb_ep *ep;
610
611 /* allocate instance-specific interface IDs, and patch descriptors */
612 status = usb_interface_id(c, f);
613 if (status < 0)
614 goto fail;
615 acm->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100616 acm_iad_descriptor.bFirstInterface = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700617
618 acm_control_interface_desc.bInterfaceNumber = status;
619 acm_union_desc .bMasterInterface0 = status;
620
621 status = usb_interface_id(c, f);
622 if (status < 0)
623 goto fail;
624 acm->data_id = status;
625
626 acm_data_interface_desc.bInterfaceNumber = status;
627 acm_union_desc.bSlaveInterface0 = status;
628 acm_call_mgmt_descriptor.bDataInterface = status;
629
630 status = -ENODEV;
631
632 /* allocate instance-specific endpoints */
633 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
634 if (!ep)
635 goto fail;
636 acm->port.in = ep;
637 ep->driver_data = cdev; /* claim */
638
639 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
640 if (!ep)
641 goto fail;
642 acm->port.out = ep;
643 ep->driver_data = cdev; /* claim */
644
645 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
646 if (!ep)
647 goto fail;
648 acm->notify = ep;
649 ep->driver_data = cdev; /* claim */
650
David Brownell1f1ba112008-08-06 18:49:57 -0700651 /* allocate notification */
652 acm->notify_req = gs_alloc_req(ep,
653 sizeof(struct usb_cdc_notification) + 2,
654 GFP_KERNEL);
655 if (!acm->notify_req)
656 goto fail;
657
658 acm->notify_req->complete = acm_cdc_notify_complete;
659 acm->notify_req->context = acm;
660
David Brownell4d5a73d2008-06-19 18:18:40 -0700661 /* support all relevant hardware speeds... we expect that when
662 * hardware is dual speed, all bulk-capable endpoints work at
663 * both speeds
664 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200665 acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
666 acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
667 acm_hs_notify_desc.bEndpointAddress =
668 acm_fs_notify_desc.bEndpointAddress;
David Brownell4d5a73d2008-06-19 18:18:40 -0700669
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200670 acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
671 acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100672
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200673 status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
674 acm_ss_function);
675 if (status)
676 goto fail;
David Brownell4d5a73d2008-06-19 18:18:40 -0700677
David Brownell4d5a73d2008-06-19 18:18:40 -0700678 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
679 acm->port_num,
Sebastian Andrzej Siewior6fecfb02012-02-06 18:46:36 +0100680 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell4d5a73d2008-06-19 18:18:40 -0700681 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
682 acm->port.in->name, acm->port.out->name,
683 acm->notify->name);
684 return 0;
685
686fail:
David Brownell1f1ba112008-08-06 18:49:57 -0700687 if (acm->notify_req)
688 gs_free_req(acm->notify, acm->notify_req);
689
David Brownell4d5a73d2008-06-19 18:18:40 -0700690 /* we might as well release our claims on endpoints */
691 if (acm->notify)
692 acm->notify->driver_data = NULL;
693 if (acm->port.out)
694 acm->port.out->driver_data = NULL;
695 if (acm->port.in)
696 acm->port.in->driver_data = NULL;
697
698 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
699
700 return status;
701}
702
703static void
704acm_unbind(struct usb_configuration *c, struct usb_function *f)
705{
David Brownell1f1ba112008-08-06 18:49:57 -0700706 struct f_acm *acm = func_to_acm(f);
707
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200708 usb_free_all_descriptors(f);
David Brownell1f1ba112008-08-06 18:49:57 -0700709 gs_free_req(acm->notify, acm->notify_req);
710 kfree(acm);
David Brownell4d5a73d2008-06-19 18:18:40 -0700711}
712
713/* Some controllers can't support CDC ACM ... */
714static inline bool can_support_cdc(struct usb_configuration *c)
715{
David Brownell4d5a73d2008-06-19 18:18:40 -0700716 /* everything else is *probably* fine ... */
717 return true;
718}
719
720/**
721 * acm_bind_config - add a CDC ACM function to a configuration
722 * @c: the configuration to support the CDC ACM instance
723 * @port_num: /dev/ttyGS* port this interface will use
724 * Context: single threaded during gadget setup
725 *
726 * Returns zero on success, else negative errno.
727 *
728 * Caller must have called @gserial_setup() with enough ports to
729 * handle all the ones it binds. Caller is also responsible
730 * for calling @gserial_cleanup() before module unload.
731 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200732int acm_bind_config(struct usb_configuration *c, u8 port_num)
David Brownell4d5a73d2008-06-19 18:18:40 -0700733{
734 struct f_acm *acm;
735 int status;
736
737 if (!can_support_cdc(c))
738 return -EINVAL;
739
740 /* REVISIT might want instance-specific strings to help
741 * distinguish instances ...
742 */
743
744 /* maybe allocate device-global string IDs, and patch descriptors */
745 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
746 status = usb_string_id(c->cdev);
747 if (status < 0)
748 return status;
749 acm_string_defs[ACM_CTRL_IDX].id = status;
750
751 acm_control_interface_desc.iInterface = status;
752
753 status = usb_string_id(c->cdev);
754 if (status < 0)
755 return status;
756 acm_string_defs[ACM_DATA_IDX].id = status;
757
758 acm_data_interface_desc.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100759
760 status = usb_string_id(c->cdev);
761 if (status < 0)
762 return status;
763 acm_string_defs[ACM_IAD_IDX].id = status;
764
765 acm_iad_descriptor.iFunction = status;
David Brownell4d5a73d2008-06-19 18:18:40 -0700766 }
767
768 /* allocate and initialize one new instance */
769 acm = kzalloc(sizeof *acm, GFP_KERNEL);
770 if (!acm)
771 return -ENOMEM;
772
David Brownell1f1ba112008-08-06 18:49:57 -0700773 spin_lock_init(&acm->lock);
774
David Brownell4d5a73d2008-06-19 18:18:40 -0700775 acm->port_num = port_num;
776
David Brownell1f1ba112008-08-06 18:49:57 -0700777 acm->port.connect = acm_connect;
778 acm->port.disconnect = acm_disconnect;
779 acm->port.send_break = acm_send_break;
780
David Brownell4d5a73d2008-06-19 18:18:40 -0700781 acm->port.func.name = "acm";
782 acm->port.func.strings = acm_strings;
783 /* descriptors are per-instance copies */
784 acm->port.func.bind = acm_bind;
785 acm->port.func.unbind = acm_unbind;
786 acm->port.func.set_alt = acm_set_alt;
787 acm->port.func.setup = acm_setup;
788 acm->port.func.disable = acm_disable;
789
790 status = usb_add_function(c, &acm->port.func);
791 if (status)
792 kfree(acm);
793 return status;
794}