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