David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * f_serial.c - generic USB serial function driver |
| 3 | * |
| 4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) |
| 5 | * Copyright (C) 2008 by David Brownell |
| 6 | * Copyright (C) 2008 by Nokia Corporation |
| 7 | * |
| 8 | * This software is distributed under the terms of the GNU General |
| 9 | * Public License ("GPL") as published by the Free Software Foundation, |
| 10 | * either version 2 of that License or (at your option) any later version. |
| 11 | */ |
| 12 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/device.h> |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 16 | #include <mach/usb_gadget_xport.h> |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 17 | |
| 18 | #include "u_serial.h" |
| 19 | #include "gadget_chips.h" |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | * This function packages a simple "generic serial" port with no real |
| 24 | * control mechanisms, just raw data transfer over two bulk endpoints. |
| 25 | * |
| 26 | * Because it's not standardized, this isn't as interoperable as the |
| 27 | * CDC ACM driver. However, for many purposes it's just as functional |
| 28 | * if you can arrange appropriate host side drivers. |
| 29 | */ |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 30 | #define GSERIAL_NO_PORTS 3 |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 31 | |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 32 | struct f_gser { |
| 33 | struct gserial port; |
| 34 | u8 data_id; |
| 35 | u8 port_num; |
| 36 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | u8 online; |
| 38 | enum transport_type transport; |
| 39 | |
| 40 | #ifdef CONFIG_MODEM_SUPPORT |
| 41 | u8 pending; |
| 42 | spinlock_t lock; |
| 43 | struct usb_ep *notify; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 44 | struct usb_request *notify_req; |
| 45 | |
| 46 | struct usb_cdc_line_coding port_line_coding; |
| 47 | |
| 48 | /* SetControlLineState request */ |
| 49 | u16 port_handshake_bits; |
| 50 | #define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */ |
| 51 | #define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */ |
| 52 | |
| 53 | /* SerialState notification */ |
| 54 | u16 serial_state; |
| 55 | #define ACM_CTRL_OVERRUN (1 << 6) |
| 56 | #define ACM_CTRL_PARITY (1 << 5) |
| 57 | #define ACM_CTRL_FRAMING (1 << 4) |
| 58 | #define ACM_CTRL_RI (1 << 3) |
| 59 | #define ACM_CTRL_BRK (1 << 2) |
| 60 | #define ACM_CTRL_DSR (1 << 1) |
| 61 | #define ACM_CTRL_DCD (1 << 0) |
| 62 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 65 | static unsigned int no_tty_ports; |
| 66 | static unsigned int no_sdio_ports; |
| 67 | static unsigned int no_smd_ports; |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 68 | static unsigned int no_hsic_sports; |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 69 | static unsigned int no_hsuart_sports; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 70 | static unsigned int nr_ports; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 71 | |
| 72 | static struct port_info { |
| 73 | enum transport_type transport; |
| 74 | unsigned port_num; |
| 75 | unsigned client_port_num; |
| 76 | } gserial_ports[GSERIAL_NO_PORTS]; |
| 77 | |
| 78 | static inline bool is_transport_sdio(enum transport_type t) |
| 79 | { |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 80 | if (t == USB_GADGET_XPORT_SDIO) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | return 1; |
| 82 | return 0; |
| 83 | } |
| 84 | |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 85 | static inline struct f_gser *func_to_gser(struct usb_function *f) |
| 86 | { |
| 87 | return container_of(f, struct f_gser, port.func); |
| 88 | } |
| 89 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 90 | #ifdef CONFIG_MODEM_SUPPORT |
| 91 | static inline struct f_gser *port_to_gser(struct gserial *p) |
| 92 | { |
| 93 | return container_of(p, struct f_gser, port); |
| 94 | } |
| 95 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ |
| 96 | #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */ |
| 97 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 98 | /*-------------------------------------------------------------------------*/ |
| 99 | |
| 100 | /* interface descriptor: */ |
| 101 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 102 | static struct usb_interface_descriptor gser_interface_desc = { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 103 | .bLength = USB_DT_INTERFACE_SIZE, |
| 104 | .bDescriptorType = USB_DT_INTERFACE, |
| 105 | /* .bInterfaceNumber = DYNAMIC */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 106 | #ifdef CONFIG_MODEM_SUPPORT |
| 107 | .bNumEndpoints = 3, |
| 108 | #else |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 109 | .bNumEndpoints = 2, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 110 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 111 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
| 112 | .bInterfaceSubClass = 0, |
| 113 | .bInterfaceProtocol = 0, |
| 114 | /* .iInterface = DYNAMIC */ |
| 115 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 116 | #ifdef CONFIG_MODEM_SUPPORT |
| 117 | static struct usb_cdc_header_desc gser_header_desc = { |
| 118 | .bLength = sizeof(gser_header_desc), |
| 119 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 120 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 121 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
| 122 | }; |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 123 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 124 | static struct usb_cdc_call_mgmt_descriptor |
| 125 | gser_call_mgmt_descriptor = { |
| 126 | .bLength = sizeof(gser_call_mgmt_descriptor), |
| 127 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 128 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 129 | .bmCapabilities = 0, |
| 130 | /* .bDataInterface = DYNAMIC */ |
| 131 | }; |
| 132 | |
| 133 | static struct usb_cdc_acm_descriptor gser_descriptor = { |
| 134 | .bLength = sizeof(gser_descriptor), |
| 135 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 136 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
| 137 | .bmCapabilities = USB_CDC_CAP_LINE, |
| 138 | }; |
| 139 | |
| 140 | static struct usb_cdc_union_desc gser_union_desc = { |
| 141 | .bLength = sizeof(gser_union_desc), |
| 142 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 143 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 144 | /* .bMasterInterface0 = DYNAMIC */ |
| 145 | /* .bSlaveInterface0 = DYNAMIC */ |
| 146 | }; |
| 147 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 148 | /* full speed support: */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 149 | #ifdef CONFIG_MODEM_SUPPORT |
| 150 | static struct usb_endpoint_descriptor gser_fs_notify_desc = { |
| 151 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 152 | .bDescriptorType = USB_DT_ENDPOINT, |
| 153 | .bEndpointAddress = USB_DIR_IN, |
| 154 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 155 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 156 | .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, |
| 157 | }; |
| 158 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 159 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 160 | static struct usb_endpoint_descriptor gser_fs_in_desc = { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 161 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 162 | .bDescriptorType = USB_DT_ENDPOINT, |
| 163 | .bEndpointAddress = USB_DIR_IN, |
| 164 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 165 | }; |
| 166 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 167 | static struct usb_endpoint_descriptor gser_fs_out_desc = { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 168 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 169 | .bDescriptorType = USB_DT_ENDPOINT, |
| 170 | .bEndpointAddress = USB_DIR_OUT, |
| 171 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 172 | }; |
| 173 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 174 | static struct usb_descriptor_header *gser_fs_function[] = { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 175 | (struct usb_descriptor_header *) &gser_interface_desc, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 176 | #ifdef CONFIG_MODEM_SUPPORT |
| 177 | (struct usb_descriptor_header *) &gser_header_desc, |
| 178 | (struct usb_descriptor_header *) &gser_call_mgmt_descriptor, |
| 179 | (struct usb_descriptor_header *) &gser_descriptor, |
| 180 | (struct usb_descriptor_header *) &gser_union_desc, |
| 181 | (struct usb_descriptor_header *) &gser_fs_notify_desc, |
| 182 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 183 | (struct usb_descriptor_header *) &gser_fs_in_desc, |
| 184 | (struct usb_descriptor_header *) &gser_fs_out_desc, |
| 185 | NULL, |
| 186 | }; |
| 187 | |
| 188 | /* high speed support: */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | #ifdef CONFIG_MODEM_SUPPORT |
| 190 | static struct usb_endpoint_descriptor gser_hs_notify_desc = { |
| 191 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 192 | .bDescriptorType = USB_DT_ENDPOINT, |
| 193 | .bEndpointAddress = USB_DIR_IN, |
| 194 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 195 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 196 | .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, |
| 197 | }; |
| 198 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 199 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 200 | static struct usb_endpoint_descriptor gser_hs_in_desc = { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 201 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 202 | .bDescriptorType = USB_DT_ENDPOINT, |
| 203 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 204 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 207 | static struct usb_endpoint_descriptor gser_hs_out_desc = { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 208 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 209 | .bDescriptorType = USB_DT_ENDPOINT, |
| 210 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 211 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 214 | static struct usb_descriptor_header *gser_hs_function[] = { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 215 | (struct usb_descriptor_header *) &gser_interface_desc, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 216 | #ifdef CONFIG_MODEM_SUPPORT |
| 217 | (struct usb_descriptor_header *) &gser_header_desc, |
| 218 | (struct usb_descriptor_header *) &gser_call_mgmt_descriptor, |
| 219 | (struct usb_descriptor_header *) &gser_descriptor, |
| 220 | (struct usb_descriptor_header *) &gser_union_desc, |
| 221 | (struct usb_descriptor_header *) &gser_hs_notify_desc, |
| 222 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 223 | (struct usb_descriptor_header *) &gser_hs_in_desc, |
| 224 | (struct usb_descriptor_header *) &gser_hs_out_desc, |
| 225 | NULL, |
| 226 | }; |
| 227 | |
Sebastian Andrzej Siewior | 6fecfb0 | 2012-02-06 18:46:36 +0100 | [diff] [blame] | 228 | static struct usb_endpoint_descriptor gser_ss_in_desc __initdata = { |
| 229 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 230 | .bDescriptorType = USB_DT_ENDPOINT, |
| 231 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 232 | .wMaxPacketSize = cpu_to_le16(1024), |
| 233 | }; |
| 234 | |
| 235 | static struct usb_endpoint_descriptor gser_ss_out_desc __initdata = { |
| 236 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 237 | .bDescriptorType = USB_DT_ENDPOINT, |
| 238 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 239 | .wMaxPacketSize = cpu_to_le16(1024), |
| 240 | }; |
| 241 | |
| 242 | static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc __initdata = { |
| 243 | .bLength = sizeof gser_ss_bulk_comp_desc, |
| 244 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 245 | }; |
| 246 | |
| 247 | static struct usb_descriptor_header *gser_ss_function[] __initdata = { |
| 248 | (struct usb_descriptor_header *) &gser_interface_desc, |
| 249 | (struct usb_descriptor_header *) &gser_ss_in_desc, |
| 250 | (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc, |
| 251 | (struct usb_descriptor_header *) &gser_ss_out_desc, |
| 252 | (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc, |
| 253 | NULL, |
| 254 | }; |
| 255 | |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 256 | /* string descriptors: */ |
| 257 | |
| 258 | static struct usb_string gser_string_defs[] = { |
| 259 | [0].s = "Generic Serial", |
| 260 | { } /* end of list */ |
| 261 | }; |
| 262 | |
| 263 | static struct usb_gadget_strings gser_string_table = { |
| 264 | .language = 0x0409, /* en-us */ |
| 265 | .strings = gser_string_defs, |
| 266 | }; |
| 267 | |
| 268 | static struct usb_gadget_strings *gser_strings[] = { |
| 269 | &gser_string_table, |
| 270 | NULL, |
| 271 | }; |
| 272 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 273 | static int gport_setup(struct usb_configuration *c) |
| 274 | { |
| 275 | int ret = 0; |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 276 | int port_idx; |
| 277 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 279 | pr_debug("%s: no_tty_ports: %u no_sdio_ports: %u" |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 280 | " no_smd_ports: %u no_hsic_sports: %u no_hsuart_ports: %u nr_ports: %u\n", |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 281 | __func__, no_tty_ports, no_sdio_ports, no_smd_ports, |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 282 | no_hsic_sports, no_hsuart_sports, nr_ports); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 283 | |
| 284 | if (no_tty_ports) |
| 285 | ret = gserial_setup(c->cdev->gadget, no_tty_ports); |
| 286 | if (no_sdio_ports) |
| 287 | ret = gsdio_setup(c->cdev->gadget, no_sdio_ports); |
| 288 | if (no_smd_ports) |
| 289 | ret = gsmd_setup(c->cdev->gadget, no_smd_ports); |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 290 | if (no_hsic_sports) { |
| 291 | port_idx = ghsic_data_setup(no_hsic_sports, USB_GADGET_SERIAL); |
| 292 | if (port_idx < 0) |
| 293 | return port_idx; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 294 | |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 295 | for (i = 0; i < nr_ports; i++) { |
| 296 | if (gserial_ports[i].transport == |
| 297 | USB_GADGET_XPORT_HSIC) { |
| 298 | gserial_ports[i].client_port_num = port_idx; |
| 299 | port_idx++; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /*clinet port num is same for data setup and ctrl setup*/ |
| 304 | ret = ghsic_ctrl_setup(no_hsic_sports, USB_GADGET_SERIAL); |
| 305 | if (ret < 0) |
| 306 | return ret; |
| 307 | return 0; |
| 308 | } |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 309 | if (no_hsuart_sports) { |
| 310 | port_idx = ghsuart_data_setup(no_hsuart_sports, |
| 311 | USB_GADGET_SERIAL); |
| 312 | if (port_idx < 0) |
| 313 | return port_idx; |
| 314 | |
| 315 | for (i = 0; i < nr_ports; i++) { |
| 316 | if (gserial_ports[i].transport == |
| 317 | USB_GADGET_XPORT_HSUART) { |
| 318 | gserial_ports[i].client_port_num = port_idx; |
| 319 | port_idx++; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 325 | return ret; |
| 326 | } |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 327 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 328 | static int gport_connect(struct f_gser *gser) |
| 329 | { |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 330 | unsigned port_num; |
| 331 | int ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 332 | |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 333 | pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n", |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 334 | __func__, xport_to_str(gser->transport), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 335 | gser, &gser->port, gser->port_num); |
| 336 | |
| 337 | port_num = gserial_ports[gser->port_num].client_port_num; |
| 338 | |
| 339 | switch (gser->transport) { |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 340 | case USB_GADGET_XPORT_TTY: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 341 | gserial_connect(&gser->port, port_num); |
| 342 | break; |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 343 | case USB_GADGET_XPORT_SDIO: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 344 | gsdio_connect(&gser->port, port_num); |
| 345 | break; |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 346 | case USB_GADGET_XPORT_SMD: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 347 | gsmd_connect(&gser->port, port_num); |
| 348 | break; |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 349 | case USB_GADGET_XPORT_HSIC: |
| 350 | ret = ghsic_ctrl_connect(&gser->port, port_num); |
| 351 | if (ret) { |
| 352 | pr_err("%s: ghsic_ctrl_connect failed: err:%d\n", |
| 353 | __func__, ret); |
| 354 | return ret; |
| 355 | } |
| 356 | ret = ghsic_data_connect(&gser->port, port_num); |
| 357 | if (ret) { |
| 358 | pr_err("%s: ghsic_data_connect failed: err:%d\n", |
| 359 | __func__, ret); |
| 360 | ghsic_ctrl_disconnect(&gser->port, port_num); |
| 361 | return ret; |
| 362 | } |
| 363 | break; |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 364 | case USB_GADGET_XPORT_HSUART: |
| 365 | ret = ghsuart_data_connect(&gser->port, port_num); |
| 366 | if (ret) { |
| 367 | pr_err("%s: ghsuart_data_connect failed: err:%d\n", |
| 368 | __func__, ret); |
| 369 | return ret; |
| 370 | } |
| 371 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | default: |
| 373 | pr_err("%s: Un-supported transport: %s\n", __func__, |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 374 | xport_to_str(gser->transport)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 375 | return -ENODEV; |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int gport_disconnect(struct f_gser *gser) |
| 382 | { |
| 383 | unsigned port_num; |
| 384 | |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 385 | pr_debug("%s: transport: %s f_gser: %p gserial: %p port_num: %d\n", |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 386 | __func__, xport_to_str(gser->transport), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 387 | gser, &gser->port, gser->port_num); |
| 388 | |
| 389 | port_num = gserial_ports[gser->port_num].client_port_num; |
| 390 | |
| 391 | switch (gser->transport) { |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 392 | case USB_GADGET_XPORT_TTY: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 393 | gserial_disconnect(&gser->port); |
| 394 | break; |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 395 | case USB_GADGET_XPORT_SDIO: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 396 | gsdio_disconnect(&gser->port, port_num); |
| 397 | break; |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 398 | case USB_GADGET_XPORT_SMD: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 399 | gsmd_disconnect(&gser->port, port_num); |
| 400 | break; |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 401 | case USB_GADGET_XPORT_HSIC: |
| 402 | ghsic_ctrl_disconnect(&gser->port, port_num); |
| 403 | ghsic_data_disconnect(&gser->port, port_num); |
| 404 | break; |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 405 | case USB_GADGET_XPORT_HSUART: |
| 406 | ghsuart_data_disconnect(&gser->port, port_num); |
| 407 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | default: |
| 409 | pr_err("%s: Un-supported transport:%s\n", __func__, |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 410 | xport_to_str(gser->transport)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | return -ENODEV; |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | #ifdef CONFIG_MODEM_SUPPORT |
| 418 | static void gser_complete_set_line_coding(struct usb_ep *ep, |
| 419 | struct usb_request *req) |
| 420 | { |
| 421 | struct f_gser *gser = ep->driver_data; |
| 422 | struct usb_composite_dev *cdev = gser->port.func.config->cdev; |
| 423 | |
| 424 | if (req->status != 0) { |
| 425 | DBG(cdev, "gser ttyGS%d completion, err %d\n", |
| 426 | gser->port_num, req->status); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | /* normal completion */ |
| 431 | if (req->actual != sizeof(gser->port_line_coding)) { |
| 432 | DBG(cdev, "gser ttyGS%d short resp, len %d\n", |
| 433 | gser->port_num, req->actual); |
| 434 | usb_ep_set_halt(ep); |
| 435 | } else { |
| 436 | struct usb_cdc_line_coding *value = req->buf; |
| 437 | gser->port_line_coding = *value; |
| 438 | } |
| 439 | } |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 440 | /*-------------------------------------------------------------------------*/ |
| 441 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 442 | static int |
| 443 | gser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 444 | { |
| 445 | struct f_gser *gser = func_to_gser(f); |
| 446 | struct usb_composite_dev *cdev = f->config->cdev; |
| 447 | struct usb_request *req = cdev->req; |
| 448 | int value = -EOPNOTSUPP; |
| 449 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 450 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 451 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 452 | |
| 453 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 454 | |
| 455 | /* SET_LINE_CODING ... just read and save what the host sends */ |
| 456 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 457 | | USB_CDC_REQ_SET_LINE_CODING: |
| 458 | if (w_length != sizeof(struct usb_cdc_line_coding)) |
| 459 | goto invalid; |
| 460 | |
| 461 | value = w_length; |
| 462 | cdev->gadget->ep0->driver_data = gser; |
| 463 | req->complete = gser_complete_set_line_coding; |
| 464 | break; |
| 465 | |
| 466 | /* GET_LINE_CODING ... return what host sent, or initial value */ |
| 467 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 468 | | USB_CDC_REQ_GET_LINE_CODING: |
| 469 | value = min_t(unsigned, w_length, |
| 470 | sizeof(struct usb_cdc_line_coding)); |
| 471 | memcpy(req->buf, &gser->port_line_coding, value); |
| 472 | break; |
| 473 | |
| 474 | /* SET_CONTROL_LINE_STATE ... save what the host sent */ |
| 475 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 476 | | USB_CDC_REQ_SET_CONTROL_LINE_STATE: |
| 477 | |
| 478 | value = 0; |
| 479 | gser->port_handshake_bits = w_value; |
| 480 | if (gser->port.notify_modem) { |
| 481 | unsigned port_num = |
| 482 | gserial_ports[gser->port_num].client_port_num; |
| 483 | |
| 484 | gser->port.notify_modem(&gser->port, |
| 485 | port_num, w_value); |
| 486 | } |
| 487 | break; |
| 488 | |
| 489 | default: |
| 490 | invalid: |
| 491 | DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 492 | ctrl->bRequestType, ctrl->bRequest, |
| 493 | w_value, w_index, w_length); |
| 494 | } |
| 495 | |
| 496 | /* respond with data transfer or status phase? */ |
| 497 | if (value >= 0) { |
| 498 | DBG(cdev, "gser ttyGS%d req%02x.%02x v%04x i%04x l%d\n", |
| 499 | gser->port_num, ctrl->bRequestType, ctrl->bRequest, |
| 500 | w_value, w_index, w_length); |
| 501 | req->zero = 0; |
| 502 | req->length = value; |
| 503 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 504 | if (value < 0) |
| 505 | ERROR(cdev, "gser response on ttyGS%d, err %d\n", |
| 506 | gser->port_num, value); |
| 507 | } |
| 508 | |
| 509 | /* device either stalls (value < 0) or reports success */ |
| 510 | return value; |
| 511 | } |
| 512 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 513 | static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 514 | { |
| 515 | struct f_gser *gser = func_to_gser(f); |
| 516 | struct usb_composite_dev *cdev = f->config->cdev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | int rc = 0; |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 518 | |
| 519 | /* we know alt == 0, so this is an activation or a reset */ |
| 520 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 521 | #ifdef CONFIG_MODEM_SUPPORT |
| 522 | if (gser->notify->driver_data) { |
| 523 | DBG(cdev, "reset generic ctl ttyGS%d\n", gser->port_num); |
| 524 | usb_ep_disable(gser->notify); |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 525 | } |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 526 | |
| 527 | if (!gser->notify->desc) { |
| 528 | if (config_ep_by_speed(cdev->gadget, f, gser->notify)) { |
| 529 | gser->notify->desc = NULL; |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | } |
Tatyana Brokhman | cf709c1 | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 533 | rc = usb_ep_enable(gser->notify); |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 534 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 535 | if (rc) { |
| 536 | ERROR(cdev, "can't enable %s, result %d\n", |
| 537 | gser->notify->name, rc); |
| 538 | return rc; |
| 539 | } |
| 540 | gser->notify->driver_data = gser; |
| 541 | #endif |
| 542 | |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 543 | if (gser->port.in->driver_data) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 544 | DBG(cdev, "reset generic data ttyGS%d\n", gser->port_num); |
| 545 | gport_disconnect(gser); |
Tatyana Brokhman | ea2a1df7 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 546 | } |
| 547 | if (!gser->port.in->desc || !gser->port.out->desc) { |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 548 | DBG(cdev, "activate generic ttyGS%d\n", gser->port_num); |
Robert Jarzmik | fef6964 | 2011-11-18 20:16:27 +0100 | [diff] [blame] | 549 | if (config_ep_by_speed(cdev->gadget, f, gser->port.in) || |
| 550 | config_ep_by_speed(cdev->gadget, f, gser->port.out)) { |
Tatyana Brokhman | ea2a1df7 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 551 | gser->port.in->desc = NULL; |
| 552 | gser->port.out->desc = NULL; |
| 553 | return -EINVAL; |
| 554 | } |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 555 | } |
David Brown | ac5d154 | 2012-02-06 10:37:22 -0800 | [diff] [blame] | 556 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 557 | gport_connect(gser); |
| 558 | |
| 559 | gser->online = 1; |
| 560 | return rc; |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static void gser_disable(struct usb_function *f) |
| 564 | { |
| 565 | struct f_gser *gser = func_to_gser(f); |
| 566 | struct usb_composite_dev *cdev = f->config->cdev; |
| 567 | |
| 568 | DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 569 | |
| 570 | gport_disconnect(gser); |
| 571 | |
| 572 | #ifdef CONFIG_MODEM_SUPPORT |
| 573 | usb_ep_fifo_flush(gser->notify); |
| 574 | usb_ep_disable(gser->notify); |
Chiranjeevi Velempati | 4fd1c16 | 2012-08-01 09:41:22 +0530 | [diff] [blame^] | 575 | gser->notify->driver_data = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | #endif |
| 577 | gser->online = 0; |
| 578 | } |
| 579 | #ifdef CONFIG_MODEM_SUPPORT |
| 580 | static int gser_notify(struct f_gser *gser, u8 type, u16 value, |
| 581 | void *data, unsigned length) |
| 582 | { |
| 583 | struct usb_ep *ep = gser->notify; |
| 584 | struct usb_request *req; |
| 585 | struct usb_cdc_notification *notify; |
| 586 | const unsigned len = sizeof(*notify) + length; |
| 587 | void *buf; |
| 588 | int status; |
| 589 | struct usb_composite_dev *cdev = gser->port.func.config->cdev; |
| 590 | |
| 591 | req = gser->notify_req; |
| 592 | gser->notify_req = NULL; |
| 593 | gser->pending = false; |
| 594 | |
| 595 | req->length = len; |
| 596 | notify = req->buf; |
| 597 | buf = notify + 1; |
| 598 | |
| 599 | notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS |
| 600 | | USB_RECIP_INTERFACE; |
| 601 | notify->bNotificationType = type; |
| 602 | notify->wValue = cpu_to_le16(value); |
| 603 | notify->wIndex = cpu_to_le16(gser->data_id); |
| 604 | notify->wLength = cpu_to_le16(length); |
| 605 | memcpy(buf, data, length); |
| 606 | |
| 607 | status = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 608 | if (status < 0) { |
| 609 | ERROR(cdev, "gser ttyGS%d can't notify serial state, %d\n", |
| 610 | gser->port_num, status); |
| 611 | gser->notify_req = req; |
| 612 | } |
| 613 | |
| 614 | return status; |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 617 | static int gser_notify_serial_state(struct f_gser *gser) |
| 618 | { |
| 619 | int status; |
| 620 | unsigned long flags; |
| 621 | struct usb_composite_dev *cdev = gser->port.func.config->cdev; |
| 622 | |
| 623 | spin_lock_irqsave(&gser->lock, flags); |
| 624 | if (gser->notify_req) { |
| 625 | DBG(cdev, "gser ttyGS%d serial state %04x\n", |
| 626 | gser->port_num, gser->serial_state); |
| 627 | status = gser_notify(gser, USB_CDC_NOTIFY_SERIAL_STATE, |
| 628 | 0, &gser->serial_state, |
| 629 | sizeof(gser->serial_state)); |
| 630 | } else { |
| 631 | gser->pending = true; |
| 632 | status = 0; |
| 633 | } |
| 634 | spin_unlock_irqrestore(&gser->lock, flags); |
| 635 | return status; |
| 636 | } |
| 637 | |
| 638 | static void gser_notify_complete(struct usb_ep *ep, struct usb_request *req) |
| 639 | { |
| 640 | struct f_gser *gser = req->context; |
| 641 | u8 doit = false; |
| 642 | unsigned long flags; |
| 643 | |
| 644 | /* on this call path we do NOT hold the port spinlock, |
| 645 | * which is why ACM needs its own spinlock |
| 646 | */ |
| 647 | spin_lock_irqsave(&gser->lock, flags); |
| 648 | if (req->status != -ESHUTDOWN) |
| 649 | doit = gser->pending; |
| 650 | gser->notify_req = req; |
| 651 | spin_unlock_irqrestore(&gser->lock, flags); |
| 652 | |
| 653 | if (doit && gser->online) |
| 654 | gser_notify_serial_state(gser); |
| 655 | } |
| 656 | static void gser_connect(struct gserial *port) |
| 657 | { |
| 658 | struct f_gser *gser = port_to_gser(port); |
| 659 | |
| 660 | gser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD; |
| 661 | gser_notify_serial_state(gser); |
| 662 | } |
| 663 | |
| 664 | unsigned int gser_get_dtr(struct gserial *port) |
| 665 | { |
| 666 | struct f_gser *gser = port_to_gser(port); |
| 667 | |
| 668 | if (gser->port_handshake_bits & ACM_CTRL_DTR) |
| 669 | return 1; |
| 670 | else |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | unsigned int gser_get_rts(struct gserial *port) |
| 675 | { |
| 676 | struct f_gser *gser = port_to_gser(port); |
| 677 | |
| 678 | if (gser->port_handshake_bits & ACM_CTRL_RTS) |
| 679 | return 1; |
| 680 | else |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | unsigned int gser_send_carrier_detect(struct gserial *port, unsigned int yes) |
| 685 | { |
| 686 | struct f_gser *gser = port_to_gser(port); |
| 687 | u16 state; |
| 688 | |
| 689 | state = gser->serial_state; |
| 690 | state &= ~ACM_CTRL_DCD; |
| 691 | if (yes) |
| 692 | state |= ACM_CTRL_DCD; |
| 693 | |
| 694 | gser->serial_state = state; |
| 695 | return gser_notify_serial_state(gser); |
| 696 | |
| 697 | } |
| 698 | |
| 699 | unsigned int gser_send_ring_indicator(struct gserial *port, unsigned int yes) |
| 700 | { |
| 701 | struct f_gser *gser = port_to_gser(port); |
| 702 | u16 state; |
| 703 | |
| 704 | state = gser->serial_state; |
| 705 | state &= ~ACM_CTRL_RI; |
| 706 | if (yes) |
| 707 | state |= ACM_CTRL_RI; |
| 708 | |
| 709 | gser->serial_state = state; |
| 710 | return gser_notify_serial_state(gser); |
| 711 | |
| 712 | } |
| 713 | static void gser_disconnect(struct gserial *port) |
| 714 | { |
| 715 | struct f_gser *gser = port_to_gser(port); |
| 716 | |
| 717 | gser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD); |
| 718 | gser_notify_serial_state(gser); |
| 719 | } |
| 720 | |
| 721 | static int gser_send_break(struct gserial *port, int duration) |
| 722 | { |
| 723 | struct f_gser *gser = port_to_gser(port); |
| 724 | u16 state; |
| 725 | |
| 726 | state = gser->serial_state; |
| 727 | state &= ~ACM_CTRL_BRK; |
| 728 | if (duration) |
| 729 | state |= ACM_CTRL_BRK; |
| 730 | |
| 731 | gser->serial_state = state; |
| 732 | return gser_notify_serial_state(gser); |
| 733 | } |
| 734 | |
| 735 | static int gser_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits) |
| 736 | { |
| 737 | struct f_gser *gser = port_to_gser(port); |
| 738 | |
| 739 | gser->serial_state = ctrl_bits; |
| 740 | |
| 741 | return gser_notify_serial_state(gser); |
| 742 | } |
| 743 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 744 | /*-------------------------------------------------------------------------*/ |
| 745 | |
| 746 | /* serial function driver setup/binding */ |
| 747 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 748 | static int |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 749 | gser_bind(struct usb_configuration *c, struct usb_function *f) |
| 750 | { |
| 751 | struct usb_composite_dev *cdev = c->cdev; |
| 752 | struct f_gser *gser = func_to_gser(f); |
| 753 | int status; |
| 754 | struct usb_ep *ep; |
| 755 | |
| 756 | /* allocate instance-specific interface IDs */ |
| 757 | status = usb_interface_id(c, f); |
| 758 | if (status < 0) |
| 759 | goto fail; |
| 760 | gser->data_id = status; |
| 761 | gser_interface_desc.bInterfaceNumber = status; |
| 762 | |
| 763 | status = -ENODEV; |
| 764 | |
| 765 | /* allocate instance-specific endpoints */ |
| 766 | ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc); |
| 767 | if (!ep) |
| 768 | goto fail; |
| 769 | gser->port.in = ep; |
| 770 | ep->driver_data = cdev; /* claim */ |
| 771 | |
| 772 | ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc); |
| 773 | if (!ep) |
| 774 | goto fail; |
| 775 | gser->port.out = ep; |
| 776 | ep->driver_data = cdev; /* claim */ |
| 777 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 778 | #ifdef CONFIG_MODEM_SUPPORT |
| 779 | ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_notify_desc); |
| 780 | if (!ep) |
| 781 | goto fail; |
| 782 | gser->notify = ep; |
| 783 | ep->driver_data = cdev; /* claim */ |
| 784 | /* allocate notification */ |
| 785 | gser->notify_req = gs_alloc_req(ep, |
| 786 | sizeof(struct usb_cdc_notification) + 2, |
| 787 | GFP_KERNEL); |
| 788 | if (!gser->notify_req) |
| 789 | goto fail; |
| 790 | |
| 791 | gser->notify_req->complete = gser_notify_complete; |
| 792 | gser->notify_req->context = gser; |
| 793 | #endif |
| 794 | |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 795 | /* copy descriptors, and track endpoint copies */ |
| 796 | f->descriptors = usb_copy_descriptors(gser_fs_function); |
| 797 | |
Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 798 | if (!f->descriptors) |
| 799 | goto fail; |
| 800 | |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 801 | /* support all relevant hardware speeds... we expect that when |
| 802 | * hardware is dual speed, all bulk-capable endpoints work at |
| 803 | * both speeds |
| 804 | */ |
| 805 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 806 | gser_hs_in_desc.bEndpointAddress = |
| 807 | gser_fs_in_desc.bEndpointAddress; |
| 808 | gser_hs_out_desc.bEndpointAddress = |
| 809 | gser_fs_out_desc.bEndpointAddress; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 810 | #ifdef CONFIG_MODEM_SUPPORT |
| 811 | gser_hs_notify_desc.bEndpointAddress = |
| 812 | gser_fs_notify_desc.bEndpointAddress; |
| 813 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 814 | |
| 815 | /* copy descriptors, and track endpoint copies */ |
| 816 | f->hs_descriptors = usb_copy_descriptors(gser_hs_function); |
| 817 | |
Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 818 | if (!f->hs_descriptors) |
| 819 | goto fail; |
| 820 | |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 821 | } |
Sebastian Andrzej Siewior | 6fecfb0 | 2012-02-06 18:46:36 +0100 | [diff] [blame] | 822 | if (gadget_is_superspeed(c->cdev->gadget)) { |
| 823 | gser_ss_in_desc.bEndpointAddress = |
| 824 | gser_fs_in_desc.bEndpointAddress; |
| 825 | gser_ss_out_desc.bEndpointAddress = |
| 826 | gser_fs_out_desc.bEndpointAddress; |
| 827 | |
| 828 | /* copy descriptors, and track endpoint copies */ |
| 829 | f->ss_descriptors = usb_copy_descriptors(gser_ss_function); |
| 830 | if (!f->ss_descriptors) |
| 831 | goto fail; |
| 832 | } |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 833 | |
| 834 | DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n", |
| 835 | gser->port_num, |
Sebastian Andrzej Siewior | 6fecfb0 | 2012-02-06 18:46:36 +0100 | [diff] [blame] | 836 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 837 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 838 | gser->port.in->name, gser->port.out->name); |
| 839 | return 0; |
| 840 | |
| 841 | fail: |
Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 842 | if (f->descriptors) |
| 843 | usb_free_descriptors(f->descriptors); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 844 | #ifdef CONFIG_MODEM_SUPPORT |
| 845 | if (gser->notify_req) |
| 846 | gs_free_req(gser->notify, gser->notify_req); |
| 847 | |
| 848 | /* we might as well release our claims on endpoints */ |
| 849 | if (gser->notify) |
| 850 | gser->notify->driver_data = NULL; |
| 851 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 852 | /* we might as well release our claims on endpoints */ |
| 853 | if (gser->port.out) |
| 854 | gser->port.out->driver_data = NULL; |
| 855 | if (gser->port.in) |
| 856 | gser->port.in->driver_data = NULL; |
| 857 | |
| 858 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); |
| 859 | |
| 860 | return status; |
| 861 | } |
| 862 | |
| 863 | static void |
| 864 | gser_unbind(struct usb_configuration *c, struct usb_function *f) |
| 865 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 866 | #ifdef CONFIG_MODEM_SUPPORT |
| 867 | struct f_gser *gser = func_to_gser(f); |
| 868 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 869 | if (gadget_is_dualspeed(c->cdev->gadget)) |
| 870 | usb_free_descriptors(f->hs_descriptors); |
Sebastian Andrzej Siewior | 6fecfb0 | 2012-02-06 18:46:36 +0100 | [diff] [blame] | 871 | if (gadget_is_superspeed(c->cdev->gadget)) |
| 872 | usb_free_descriptors(f->ss_descriptors); |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 873 | usb_free_descriptors(f->descriptors); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 874 | #ifdef CONFIG_MODEM_SUPPORT |
| 875 | gs_free_req(gser->notify, gser->notify_req); |
| 876 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 877 | kfree(func_to_gser(f)); |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * gser_bind_config - add a generic serial function to a configuration |
| 882 | * @c: the configuration to support the serial instance |
| 883 | * @port_num: /dev/ttyGS* port this interface will use |
| 884 | * Context: single threaded during gadget setup |
| 885 | * |
| 886 | * Returns zero on success, else negative errno. |
| 887 | * |
| 888 | * Caller must have called @gserial_setup() with enough ports to |
| 889 | * handle all the ones it binds. Caller is also responsible |
| 890 | * for calling @gserial_cleanup() before module unload. |
| 891 | */ |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 892 | int gser_bind_config(struct usb_configuration *c, u8 port_num) |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 893 | { |
| 894 | struct f_gser *gser; |
| 895 | int status; |
| 896 | |
| 897 | /* REVISIT might want instance-specific strings to help |
| 898 | * distinguish instances ... |
| 899 | */ |
| 900 | |
| 901 | /* maybe allocate device-global string ID */ |
| 902 | if (gser_string_defs[0].id == 0) { |
| 903 | status = usb_string_id(c->cdev); |
| 904 | if (status < 0) |
| 905 | return status; |
| 906 | gser_string_defs[0].id = status; |
| 907 | } |
| 908 | |
| 909 | /* allocate and initialize one new instance */ |
| 910 | gser = kzalloc(sizeof *gser, GFP_KERNEL); |
| 911 | if (!gser) |
| 912 | return -ENOMEM; |
| 913 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 914 | #ifdef CONFIG_MODEM_SUPPORT |
| 915 | spin_lock_init(&gser->lock); |
| 916 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 917 | gser->port_num = port_num; |
| 918 | |
| 919 | gser->port.func.name = "gser"; |
| 920 | gser->port.func.strings = gser_strings; |
| 921 | gser->port.func.bind = gser_bind; |
| 922 | gser->port.func.unbind = gser_unbind; |
| 923 | gser->port.func.set_alt = gser_set_alt; |
| 924 | gser->port.func.disable = gser_disable; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 925 | gser->transport = gserial_ports[port_num].transport; |
| 926 | #ifdef CONFIG_MODEM_SUPPORT |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 927 | /* We support only three ports for now */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 928 | if (port_num == 0) |
| 929 | gser->port.func.name = "modem"; |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 930 | else if (port_num == 1) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 931 | gser->port.func.name = "nmea"; |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 932 | else |
| 933 | gser->port.func.name = "modem2"; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 934 | gser->port.func.setup = gser_setup; |
| 935 | gser->port.connect = gser_connect; |
| 936 | gser->port.get_dtr = gser_get_dtr; |
| 937 | gser->port.get_rts = gser_get_rts; |
| 938 | gser->port.send_carrier_detect = gser_send_carrier_detect; |
| 939 | gser->port.send_ring_indicator = gser_send_ring_indicator; |
| 940 | gser->port.send_modem_ctrl_bits = gser_send_modem_ctrl_bits; |
| 941 | gser->port.disconnect = gser_disconnect; |
| 942 | gser->port.send_break = gser_send_break; |
| 943 | #endif |
David Brownell | 61d8bae | 2008-06-19 18:18:50 -0700 | [diff] [blame] | 944 | |
| 945 | status = usb_add_function(c, &gser->port.func); |
| 946 | if (status) |
| 947 | kfree(gser); |
| 948 | return status; |
| 949 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 950 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 951 | /** |
| 952 | * gserial_init_port - bind a gserial_port to its transport |
| 953 | */ |
| 954 | static int gserial_init_port(int port_num, const char *name) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 955 | { |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 956 | enum transport_type transport; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 957 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 958 | if (port_num >= GSERIAL_NO_PORTS) |
| 959 | return -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 960 | |
Hemant Kumar | 1b820d5 | 2011-11-03 15:08:28 -0700 | [diff] [blame] | 961 | transport = str_to_xport(name); |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 962 | pr_debug("%s, port:%d, transport:%s\n", __func__, |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 963 | port_num, xport_to_str(transport)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 964 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 965 | gserial_ports[port_num].transport = transport; |
| 966 | gserial_ports[port_num].port_num = port_num; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 967 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 968 | switch (transport) { |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 969 | case USB_GADGET_XPORT_TTY: |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 970 | gserial_ports[port_num].client_port_num = no_tty_ports; |
| 971 | no_tty_ports++; |
| 972 | break; |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 973 | case USB_GADGET_XPORT_SDIO: |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 974 | gserial_ports[port_num].client_port_num = no_sdio_ports; |
| 975 | no_sdio_ports++; |
| 976 | break; |
Hemant Kumar | bffd414 | 2011-11-03 13:20:40 -0700 | [diff] [blame] | 977 | case USB_GADGET_XPORT_SMD: |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 978 | gserial_ports[port_num].client_port_num = no_smd_ports; |
| 979 | no_smd_ports++; |
| 980 | break; |
Jack Pham | 427f692 | 2011-11-23 19:42:00 -0800 | [diff] [blame] | 981 | case USB_GADGET_XPORT_HSIC: |
| 982 | /*client port number will be updated in gport_setup*/ |
| 983 | no_hsic_sports++; |
| 984 | break; |
Vijayavardhan Vennapusa | eb8d239 | 2012-04-03 18:58:49 +0530 | [diff] [blame] | 985 | case USB_GADGET_XPORT_HSUART: |
| 986 | /*client port number will be updated in gport_setup*/ |
| 987 | no_hsuart_sports++; |
| 988 | break; |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 989 | default: |
| 990 | pr_err("%s: Un-supported transport transport: %u\n", |
| 991 | __func__, gserial_ports[port_num].transport); |
| 992 | return -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Manu Gautam | a4d993f | 2011-08-30 18:25:55 +0530 | [diff] [blame] | 995 | nr_ports++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 996 | |
| 997 | return 0; |
| 998 | } |