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