Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * g_serial.c -- USB gadget serial driver |
| 3 | * |
| 4 | * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com) |
| 5 | * |
| 6 | * This code is based in part on the Gadget Zero driver, which |
| 7 | * is Copyright (C) 2003 by David Brownell, all rights reserved. |
| 8 | * |
| 9 | * This code also borrows from usbserial.c, which is |
| 10 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) |
| 11 | * Copyright (C) 2000 Peter Berger (pberger@brimson.com) |
| 12 | * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com) |
| 13 | * |
| 14 | * This software is distributed under the terms of the GNU General |
| 15 | * Public License ("GPL") as published by the Free Software Foundation, |
| 16 | * either version 2 of that License or (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/utsname.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/device.h> |
| 22 | #include <linux/tty.h> |
| 23 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 25 | #include <linux/usb/ch9.h> |
David Brownell | a8c28f2 | 2006-06-13 09:57:47 -0700 | [diff] [blame] | 26 | #include <linux/usb/cdc.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 27 | #include <linux/usb/gadget.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include "gadget_chips.h" |
| 30 | |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* Defines */ |
| 33 | |
Franck Bui-Huu | ca094f1 | 2006-06-14 10:47:18 +0200 | [diff] [blame] | 34 | #define GS_VERSION_STR "v2.2" |
| 35 | #define GS_VERSION_NUM 0x0202 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #define GS_LONG_NAME "Gadget Serial" |
| 38 | #define GS_SHORT_NAME "g_serial" |
| 39 | |
| 40 | #define GS_MAJOR 127 |
| 41 | #define GS_MINOR_START 0 |
| 42 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 43 | /* REVISIT only one port is supported for now; |
| 44 | * see gs_{send,recv}_packet() ... no multiplexing, |
| 45 | * and no support for multiple ACM devices. |
| 46 | */ |
| 47 | #define GS_NUM_PORTS 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #define GS_NUM_CONFIGS 1 |
| 50 | #define GS_NO_CONFIG_ID 0 |
| 51 | #define GS_BULK_CONFIG_ID 1 |
| 52 | #define GS_ACM_CONFIG_ID 2 |
| 53 | |
| 54 | #define GS_MAX_NUM_INTERFACES 2 |
| 55 | #define GS_BULK_INTERFACE_ID 0 |
| 56 | #define GS_CONTROL_INTERFACE_ID 0 |
| 57 | #define GS_DATA_INTERFACE_ID 1 |
| 58 | |
| 59 | #define GS_MAX_DESC_LEN 256 |
| 60 | |
| 61 | #define GS_DEFAULT_READ_Q_SIZE 32 |
| 62 | #define GS_DEFAULT_WRITE_Q_SIZE 32 |
| 63 | |
| 64 | #define GS_DEFAULT_WRITE_BUF_SIZE 8192 |
| 65 | #define GS_TMP_BUF_SIZE 8192 |
| 66 | |
| 67 | #define GS_CLOSE_TIMEOUT 15 |
| 68 | |
| 69 | #define GS_DEFAULT_USE_ACM 0 |
| 70 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 71 | /* 9600-8-N-1 ... matches init_termios.c_cflag and defaults |
| 72 | * expected by "usbser.sys" on MS-Windows. |
| 73 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #define GS_DEFAULT_DTE_RATE 9600 |
| 75 | #define GS_DEFAULT_DATA_BITS 8 |
| 76 | #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY |
| 77 | #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS |
| 78 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 79 | /* maxpacket and other transfer characteristics vary by speed. */ |
| 80 | static inline struct usb_endpoint_descriptor * |
| 81 | choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, |
| 82 | struct usb_endpoint_descriptor *fs) |
| 83 | { |
| 84 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 85 | return hs; |
| 86 | return fs; |
| 87 | } |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* debug settings */ |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 91 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | static int debug = 1; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 93 | #else |
| 94 | #define debug 0 |
| 95 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | #define gs_debug(format, arg...) \ |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 98 | do { if (debug) pr_debug(format, ## arg); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #define gs_debug_level(level, format, arg...) \ |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 100 | do { if (debug >= level) pr_debug(format, ## arg); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* Thanks to NetChip Technologies for donating this product ID. |
| 104 | * |
| 105 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 106 | * Instead: allocate your own, using normal USB-IF procedures. |
| 107 | */ |
| 108 | #define GS_VENDOR_ID 0x0525 /* NetChip */ |
| 109 | #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */ |
| 110 | #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */ |
| 111 | |
| 112 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ |
| 113 | #define GS_NOTIFY_MAXPACKET 8 |
| 114 | |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* circular buffer */ |
| 117 | struct gs_buf { |
| 118 | unsigned int buf_size; |
| 119 | char *buf_buf; |
| 120 | char *buf_get; |
| 121 | char *buf_put; |
| 122 | }; |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /* the port structure holds info for each port, one for each minor number */ |
| 125 | struct gs_port { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 126 | struct gs_dev *port_dev; /* pointer to device struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | struct tty_struct *port_tty; /* pointer to tty struct */ |
| 128 | spinlock_t port_lock; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 129 | int port_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | int port_open_count; |
| 131 | int port_in_use; /* open/close in progress */ |
| 132 | wait_queue_head_t port_write_wait;/* waiting to write */ |
| 133 | struct gs_buf *port_write_buf; |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 134 | struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ |
| 135 | u16 port_handshake_bits; |
| 136 | #define RS232_RTS (1 << 1) |
| 137 | #define RS232_DTE (1 << 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /* the device structure holds info for the USB device */ |
| 141 | struct gs_dev { |
| 142 | struct usb_gadget *dev_gadget; /* gadget device pointer */ |
| 143 | spinlock_t dev_lock; /* lock for set/reset config */ |
| 144 | int dev_config; /* configuration number */ |
| 145 | struct usb_ep *dev_notify_ep; /* address of notify endpoint */ |
| 146 | struct usb_ep *dev_in_ep; /* address of in endpoint */ |
| 147 | struct usb_ep *dev_out_ep; /* address of out endpoint */ |
Steven Cole | 093cf72 | 2005-05-03 19:07:24 -0600 | [diff] [blame] | 148 | struct usb_endpoint_descriptor /* descriptor of notify ep */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | *dev_notify_ep_desc; |
| 150 | struct usb_endpoint_descriptor /* descriptor of in endpoint */ |
| 151 | *dev_in_ep_desc; |
| 152 | struct usb_endpoint_descriptor /* descriptor of out endpoint */ |
| 153 | *dev_out_ep_desc; |
| 154 | struct usb_request *dev_ctrl_req; /* control request */ |
| 155 | struct list_head dev_req_list; /* list of write requests */ |
| 156 | int dev_sched_port; /* round robin port scheduled */ |
| 157 | struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */ |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | /* Functions */ |
| 162 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 163 | /* tty driver internals */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | static int gs_send(struct gs_dev *dev); |
| 165 | static int gs_send_packet(struct gs_dev *dev, char *packet, |
| 166 | unsigned int size); |
| 167 | static int gs_recv_packet(struct gs_dev *dev, char *packet, |
| 168 | unsigned int size); |
| 169 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req); |
| 170 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req); |
| 171 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 172 | /* gadget driver internals */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | static int gs_set_config(struct gs_dev *dev, unsigned config); |
| 174 | static void gs_reset_config(struct gs_dev *dev); |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 175 | static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | u8 type, unsigned int index, int is_otg); |
| 177 | |
| 178 | static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 179 | gfp_t kmalloc_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req); |
| 181 | |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 182 | static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | static void gs_free_ports(struct gs_dev *dev); |
| 184 | |
| 185 | /* circular buffer */ |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 186 | static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | static void gs_buf_free(struct gs_buf *gb); |
| 188 | static void gs_buf_clear(struct gs_buf *gb); |
| 189 | static unsigned int gs_buf_data_avail(struct gs_buf *gb); |
| 190 | static unsigned int gs_buf_space_avail(struct gs_buf *gb); |
| 191 | static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, |
| 192 | unsigned int count); |
| 193 | static unsigned int gs_buf_get(struct gs_buf *gb, char *buf, |
| 194 | unsigned int count); |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | /* Globals */ |
| 198 | |
| 199 | static struct gs_dev *gs_device; |
| 200 | |
| 201 | static const char *EP_IN_NAME; |
| 202 | static const char *EP_OUT_NAME; |
| 203 | static const char *EP_NOTIFY_NAME; |
| 204 | |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 205 | static struct mutex gs_open_close_lock[GS_NUM_PORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 208 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | /* USB descriptors */ |
| 211 | |
| 212 | #define GS_MANUFACTURER_STR_ID 1 |
| 213 | #define GS_PRODUCT_STR_ID 2 |
| 214 | #define GS_SERIAL_STR_ID 3 |
| 215 | #define GS_BULK_CONFIG_STR_ID 4 |
| 216 | #define GS_ACM_CONFIG_STR_ID 5 |
| 217 | #define GS_CONTROL_STR_ID 6 |
| 218 | #define GS_DATA_STR_ID 7 |
| 219 | |
| 220 | /* static strings, in UTF-8 */ |
| 221 | static char manufacturer[50]; |
| 222 | static struct usb_string gs_strings[] = { |
| 223 | { GS_MANUFACTURER_STR_ID, manufacturer }, |
| 224 | { GS_PRODUCT_STR_ID, GS_LONG_NAME }, |
| 225 | { GS_SERIAL_STR_ID, "0" }, |
| 226 | { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" }, |
| 227 | { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" }, |
| 228 | { GS_CONTROL_STR_ID, "Gadget Serial Control" }, |
| 229 | { GS_DATA_STR_ID, "Gadget Serial Data" }, |
| 230 | { } /* end of list */ |
| 231 | }; |
| 232 | |
| 233 | static struct usb_gadget_strings gs_string_table = { |
| 234 | .language = 0x0409, /* en-us */ |
| 235 | .strings = gs_strings, |
| 236 | }; |
| 237 | |
| 238 | static struct usb_device_descriptor gs_device_desc = { |
| 239 | .bLength = USB_DT_DEVICE_SIZE, |
| 240 | .bDescriptorType = USB_DT_DEVICE, |
| 241 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 242 | .bDeviceSubClass = 0, |
| 243 | .bDeviceProtocol = 0, |
| 244 | .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID), |
| 245 | .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID), |
| 246 | .iManufacturer = GS_MANUFACTURER_STR_ID, |
| 247 | .iProduct = GS_PRODUCT_STR_ID, |
| 248 | .iSerialNumber = GS_SERIAL_STR_ID, |
| 249 | .bNumConfigurations = GS_NUM_CONFIGS, |
| 250 | }; |
| 251 | |
| 252 | static struct usb_otg_descriptor gs_otg_descriptor = { |
| 253 | .bLength = sizeof(gs_otg_descriptor), |
| 254 | .bDescriptorType = USB_DT_OTG, |
| 255 | .bmAttributes = USB_OTG_SRP, |
| 256 | }; |
| 257 | |
| 258 | static struct usb_config_descriptor gs_bulk_config_desc = { |
| 259 | .bLength = USB_DT_CONFIG_SIZE, |
| 260 | .bDescriptorType = USB_DT_CONFIG, |
| 261 | /* .wTotalLength computed dynamically */ |
| 262 | .bNumInterfaces = 1, |
| 263 | .bConfigurationValue = GS_BULK_CONFIG_ID, |
| 264 | .iConfiguration = GS_BULK_CONFIG_STR_ID, |
| 265 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 266 | .bMaxPower = 1, |
| 267 | }; |
| 268 | |
| 269 | static struct usb_config_descriptor gs_acm_config_desc = { |
| 270 | .bLength = USB_DT_CONFIG_SIZE, |
| 271 | .bDescriptorType = USB_DT_CONFIG, |
| 272 | /* .wTotalLength computed dynamically */ |
| 273 | .bNumInterfaces = 2, |
| 274 | .bConfigurationValue = GS_ACM_CONFIG_ID, |
| 275 | .iConfiguration = GS_ACM_CONFIG_STR_ID, |
| 276 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 277 | .bMaxPower = 1, |
| 278 | }; |
| 279 | |
| 280 | static const struct usb_interface_descriptor gs_bulk_interface_desc = { |
| 281 | .bLength = USB_DT_INTERFACE_SIZE, |
| 282 | .bDescriptorType = USB_DT_INTERFACE, |
| 283 | .bInterfaceNumber = GS_BULK_INTERFACE_ID, |
| 284 | .bNumEndpoints = 2, |
| 285 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 286 | .bInterfaceSubClass = 0, |
| 287 | .bInterfaceProtocol = 0, |
| 288 | .iInterface = GS_DATA_STR_ID, |
| 289 | }; |
| 290 | |
| 291 | static const struct usb_interface_descriptor gs_control_interface_desc = { |
| 292 | .bLength = USB_DT_INTERFACE_SIZE, |
| 293 | .bDescriptorType = USB_DT_INTERFACE, |
| 294 | .bInterfaceNumber = GS_CONTROL_INTERFACE_ID, |
| 295 | .bNumEndpoints = 1, |
| 296 | .bInterfaceClass = USB_CLASS_COMM, |
| 297 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 298 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, |
| 299 | .iInterface = GS_CONTROL_STR_ID, |
| 300 | }; |
| 301 | |
| 302 | static const struct usb_interface_descriptor gs_data_interface_desc = { |
| 303 | .bLength = USB_DT_INTERFACE_SIZE, |
| 304 | .bDescriptorType = USB_DT_INTERFACE, |
| 305 | .bInterfaceNumber = GS_DATA_INTERFACE_ID, |
| 306 | .bNumEndpoints = 2, |
| 307 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 308 | .bInterfaceSubClass = 0, |
| 309 | .bInterfaceProtocol = 0, |
| 310 | .iInterface = GS_DATA_STR_ID, |
| 311 | }; |
| 312 | |
| 313 | static const struct usb_cdc_header_desc gs_header_desc = { |
| 314 | .bLength = sizeof(gs_header_desc), |
| 315 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 316 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 317 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
| 318 | }; |
| 319 | |
| 320 | static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 321 | .bLength = sizeof(gs_call_mgmt_descriptor), |
| 322 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 323 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 324 | .bmCapabilities = 0, |
| 325 | .bDataInterface = 1, /* index of data interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | static struct usb_cdc_acm_descriptor gs_acm_descriptor = { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 329 | .bLength = sizeof(gs_acm_descriptor), |
| 330 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 331 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 332 | .bmCapabilities = (1 << 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | static const struct usb_cdc_union_desc gs_union_desc = { |
| 336 | .bLength = sizeof(gs_union_desc), |
| 337 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 338 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 339 | .bMasterInterface0 = 0, /* index of control interface */ |
| 340 | .bSlaveInterface0 = 1, /* index of data interface */ |
| 341 | }; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = { |
| 344 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 345 | .bDescriptorType = USB_DT_ENDPOINT, |
| 346 | .bEndpointAddress = USB_DIR_IN, |
| 347 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 348 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 349 | .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, |
| 350 | }; |
| 351 | |
| 352 | static struct usb_endpoint_descriptor gs_fullspeed_in_desc = { |
| 353 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 354 | .bDescriptorType = USB_DT_ENDPOINT, |
| 355 | .bEndpointAddress = USB_DIR_IN, |
| 356 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 357 | }; |
| 358 | |
| 359 | static struct usb_endpoint_descriptor gs_fullspeed_out_desc = { |
| 360 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 361 | .bDescriptorType = USB_DT_ENDPOINT, |
| 362 | .bEndpointAddress = USB_DIR_OUT, |
| 363 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 364 | }; |
| 365 | |
| 366 | static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = { |
| 367 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 368 | (struct usb_descriptor_header *) &gs_bulk_interface_desc, |
| 369 | (struct usb_descriptor_header *) &gs_fullspeed_in_desc, |
| 370 | (struct usb_descriptor_header *) &gs_fullspeed_out_desc, |
| 371 | NULL, |
| 372 | }; |
| 373 | |
| 374 | static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = { |
| 375 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 376 | (struct usb_descriptor_header *) &gs_control_interface_desc, |
| 377 | (struct usb_descriptor_header *) &gs_header_desc, |
| 378 | (struct usb_descriptor_header *) &gs_call_mgmt_descriptor, |
| 379 | (struct usb_descriptor_header *) &gs_acm_descriptor, |
| 380 | (struct usb_descriptor_header *) &gs_union_desc, |
| 381 | (struct usb_descriptor_header *) &gs_fullspeed_notify_desc, |
| 382 | (struct usb_descriptor_header *) &gs_data_interface_desc, |
| 383 | (struct usb_descriptor_header *) &gs_fullspeed_in_desc, |
| 384 | (struct usb_descriptor_header *) &gs_fullspeed_out_desc, |
| 385 | NULL, |
| 386 | }; |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | static struct usb_endpoint_descriptor gs_highspeed_notify_desc = { |
| 389 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 390 | .bDescriptorType = USB_DT_ENDPOINT, |
| 391 | .bEndpointAddress = USB_DIR_IN, |
| 392 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 393 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 394 | .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, |
| 395 | }; |
| 396 | |
| 397 | static struct usb_endpoint_descriptor gs_highspeed_in_desc = { |
| 398 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 399 | .bDescriptorType = USB_DT_ENDPOINT, |
| 400 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 401 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 402 | }; |
| 403 | |
| 404 | static struct usb_endpoint_descriptor gs_highspeed_out_desc = { |
| 405 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 406 | .bDescriptorType = USB_DT_ENDPOINT, |
| 407 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 408 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 409 | }; |
| 410 | |
| 411 | static struct usb_qualifier_descriptor gs_qualifier_desc = { |
| 412 | .bLength = sizeof(struct usb_qualifier_descriptor), |
| 413 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, |
| 414 | .bcdUSB = __constant_cpu_to_le16 (0x0200), |
| 415 | /* assumes ep0 uses the same value for both speeds ... */ |
| 416 | .bNumConfigurations = GS_NUM_CONFIGS, |
| 417 | }; |
| 418 | |
| 419 | static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = { |
| 420 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 421 | (struct usb_descriptor_header *) &gs_bulk_interface_desc, |
| 422 | (struct usb_descriptor_header *) &gs_highspeed_in_desc, |
| 423 | (struct usb_descriptor_header *) &gs_highspeed_out_desc, |
| 424 | NULL, |
| 425 | }; |
| 426 | |
| 427 | static const struct usb_descriptor_header *gs_acm_highspeed_function[] = { |
| 428 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 429 | (struct usb_descriptor_header *) &gs_control_interface_desc, |
| 430 | (struct usb_descriptor_header *) &gs_header_desc, |
| 431 | (struct usb_descriptor_header *) &gs_call_mgmt_descriptor, |
| 432 | (struct usb_descriptor_header *) &gs_acm_descriptor, |
| 433 | (struct usb_descriptor_header *) &gs_union_desc, |
| 434 | (struct usb_descriptor_header *) &gs_highspeed_notify_desc, |
| 435 | (struct usb_descriptor_header *) &gs_data_interface_desc, |
| 436 | (struct usb_descriptor_header *) &gs_highspeed_in_desc, |
| 437 | (struct usb_descriptor_header *) &gs_highspeed_out_desc, |
| 438 | NULL, |
| 439 | }; |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 442 | /*-------------------------------------------------------------------------*/ |
| 443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | /* Module */ |
| 445 | MODULE_DESCRIPTION(GS_LONG_NAME); |
| 446 | MODULE_AUTHOR("Al Borchers"); |
| 447 | MODULE_LICENSE("GPL"); |
| 448 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 449 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | module_param(debug, int, S_IRUGO|S_IWUSR); |
| 451 | MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on"); |
| 452 | #endif |
| 453 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 454 | static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | module_param(read_q_size, uint, S_IRUGO); |
| 456 | MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32"); |
| 457 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 458 | static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | module_param(write_q_size, uint, S_IRUGO); |
| 460 | MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32"); |
| 461 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 462 | static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | module_param(write_buf_size, uint, S_IRUGO); |
| 464 | MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192"); |
| 465 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 466 | static unsigned int use_acm = GS_DEFAULT_USE_ACM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | module_param(use_acm, uint, S_IRUGO); |
| 468 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no"); |
| 469 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 470 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
| 472 | /* TTY Driver */ |
| 473 | |
| 474 | /* |
| 475 | * gs_open |
| 476 | */ |
| 477 | static int gs_open(struct tty_struct *tty, struct file *file) |
| 478 | { |
| 479 | int port_num; |
| 480 | unsigned long flags; |
| 481 | struct gs_port *port; |
| 482 | struct gs_dev *dev; |
| 483 | struct gs_buf *buf; |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 484 | struct mutex *mtx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | int ret; |
| 486 | |
| 487 | port_num = tty->index; |
| 488 | |
| 489 | gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file); |
| 490 | |
| 491 | if (port_num < 0 || port_num >= GS_NUM_PORTS) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 492 | pr_err("gs_open: (%d,%p,%p) invalid port number\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | port_num, tty, file); |
| 494 | return -ENODEV; |
| 495 | } |
| 496 | |
| 497 | dev = gs_device; |
| 498 | |
| 499 | if (dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 500 | pr_err("gs_open: (%d,%p,%p) NULL device pointer\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | port_num, tty, file); |
| 502 | return -ENODEV; |
| 503 | } |
| 504 | |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 505 | mtx = &gs_open_close_lock[port_num]; |
| 506 | if (mutex_lock_interruptible(mtx)) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 507 | pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | port_num, tty, file); |
| 509 | return -ERESTARTSYS; |
| 510 | } |
| 511 | |
| 512 | spin_lock_irqsave(&dev->dev_lock, flags); |
| 513 | |
| 514 | if (dev->dev_config == GS_NO_CONFIG_ID) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 515 | pr_err("gs_open: (%d,%p,%p) device is not connected\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | port_num, tty, file); |
| 517 | ret = -ENODEV; |
| 518 | goto exit_unlock_dev; |
| 519 | } |
| 520 | |
| 521 | port = dev->dev_port[port_num]; |
| 522 | |
| 523 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 524 | pr_err("gs_open: (%d,%p,%p) NULL port pointer\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | port_num, tty, file); |
| 526 | ret = -ENODEV; |
| 527 | goto exit_unlock_dev; |
| 528 | } |
| 529 | |
| 530 | spin_lock(&port->port_lock); |
| 531 | spin_unlock(&dev->dev_lock); |
| 532 | |
| 533 | if (port->port_dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 534 | pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | port_num, tty, file); |
| 536 | ret = -EIO; |
| 537 | goto exit_unlock_port; |
| 538 | } |
| 539 | |
| 540 | if (port->port_open_count > 0) { |
| 541 | ++port->port_open_count; |
| 542 | gs_debug("gs_open: (%d,%p,%p) already open\n", |
| 543 | port_num, tty, file); |
| 544 | ret = 0; |
| 545 | goto exit_unlock_port; |
| 546 | } |
| 547 | |
| 548 | tty->driver_data = NULL; |
| 549 | |
| 550 | /* mark port as in use, we can drop port lock and sleep if necessary */ |
| 551 | port->port_in_use = 1; |
| 552 | |
| 553 | /* allocate write buffer on first open */ |
| 554 | if (port->port_write_buf == NULL) { |
| 555 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 556 | buf = gs_buf_alloc(write_buf_size, GFP_KERNEL); |
| 557 | spin_lock_irqsave(&port->port_lock, flags); |
| 558 | |
| 559 | /* might have been disconnected while asleep, check */ |
| 560 | if (port->port_dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 561 | pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | port_num, tty, file); |
| 563 | port->port_in_use = 0; |
| 564 | ret = -EIO; |
| 565 | goto exit_unlock_port; |
| 566 | } |
| 567 | |
| 568 | if ((port->port_write_buf=buf) == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 569 | pr_err("gs_open: (%d,%p,%p) cannot allocate " |
| 570 | "port write buffer\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | port_num, tty, file); |
| 572 | port->port_in_use = 0; |
| 573 | ret = -ENOMEM; |
| 574 | goto exit_unlock_port; |
| 575 | } |
| 576 | |
| 577 | } |
| 578 | |
| 579 | /* wait for carrier detect (not implemented) */ |
| 580 | |
| 581 | /* might have been disconnected while asleep, check */ |
| 582 | if (port->port_dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 583 | pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | port_num, tty, file); |
| 585 | port->port_in_use = 0; |
| 586 | ret = -EIO; |
| 587 | goto exit_unlock_port; |
| 588 | } |
| 589 | |
| 590 | tty->driver_data = port; |
| 591 | port->port_tty = tty; |
| 592 | port->port_open_count = 1; |
| 593 | port->port_in_use = 0; |
| 594 | |
| 595 | gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file); |
| 596 | |
| 597 | ret = 0; |
| 598 | |
| 599 | exit_unlock_port: |
| 600 | spin_unlock_irqrestore(&port->port_lock, flags); |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 601 | mutex_unlock(mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | return ret; |
| 603 | |
| 604 | exit_unlock_dev: |
| 605 | spin_unlock_irqrestore(&dev->dev_lock, flags); |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 606 | mutex_unlock(mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | return ret; |
| 608 | |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * gs_close |
| 613 | */ |
Franck Bui-Huu | 943e1b4 | 2006-06-14 10:29:21 +0200 | [diff] [blame] | 614 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 615 | static int gs_write_finished_event_safely(struct gs_port *p) |
| 616 | { |
| 617 | int cond; |
| 618 | |
| 619 | spin_lock_irq(&(p)->port_lock); |
| 620 | cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); |
| 621 | spin_unlock_irq(&(p)->port_lock); |
| 622 | return cond; |
| 623 | } |
Franck Bui-Huu | 943e1b4 | 2006-06-14 10:29:21 +0200 | [diff] [blame] | 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | static void gs_close(struct tty_struct *tty, struct file *file) |
| 626 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | struct gs_port *port = tty->driver_data; |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 628 | struct mutex *mtx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 631 | pr_err("gs_close: NULL port pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | return; |
| 633 | } |
| 634 | |
| 635 | gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file); |
| 636 | |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 637 | mtx = &gs_open_close_lock[port->port_num]; |
| 638 | mutex_lock(mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Franck Bui-Huu | ca094f1 | 2006-06-14 10:47:18 +0200 | [diff] [blame] | 640 | spin_lock_irq(&port->port_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
| 642 | if (port->port_open_count == 0) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 643 | pr_err("gs_close: (%d,%p,%p) port is already closed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | port->port_num, tty, file); |
| 645 | goto exit; |
| 646 | } |
| 647 | |
| 648 | if (port->port_open_count > 1) { |
| 649 | --port->port_open_count; |
| 650 | goto exit; |
| 651 | } |
| 652 | |
| 653 | /* free disconnected port on final close */ |
| 654 | if (port->port_dev == NULL) { |
| 655 | kfree(port); |
| 656 | goto exit; |
| 657 | } |
| 658 | |
| 659 | /* mark port as closed but in use, we can drop port lock */ |
| 660 | /* and sleep if necessary */ |
| 661 | port->port_in_use = 1; |
| 662 | port->port_open_count = 0; |
| 663 | |
| 664 | /* wait for write buffer to drain, or */ |
| 665 | /* at most GS_CLOSE_TIMEOUT seconds */ |
| 666 | if (gs_buf_data_avail(port->port_write_buf) > 0) { |
Franck Bui-Huu | ca094f1 | 2006-06-14 10:47:18 +0200 | [diff] [blame] | 667 | spin_unlock_irq(&port->port_lock); |
Franck Bui-Huu | 943e1b4 | 2006-06-14 10:29:21 +0200 | [diff] [blame] | 668 | wait_event_interruptible_timeout(port->port_write_wait, |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 669 | gs_write_finished_event_safely(port), |
Franck Bui-Huu | 943e1b4 | 2006-06-14 10:29:21 +0200 | [diff] [blame] | 670 | GS_CLOSE_TIMEOUT * HZ); |
Franck Bui-Huu | ca094f1 | 2006-06-14 10:47:18 +0200 | [diff] [blame] | 671 | spin_lock_irq(&port->port_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* free disconnected port on final close */ |
| 675 | /* (might have happened during the above sleep) */ |
| 676 | if (port->port_dev == NULL) { |
| 677 | kfree(port); |
| 678 | goto exit; |
| 679 | } |
| 680 | |
| 681 | gs_buf_clear(port->port_write_buf); |
| 682 | |
| 683 | tty->driver_data = NULL; |
| 684 | port->port_tty = NULL; |
| 685 | port->port_in_use = 0; |
| 686 | |
| 687 | gs_debug("gs_close: (%d,%p,%p) completed\n", |
| 688 | port->port_num, tty, file); |
| 689 | |
| 690 | exit: |
Franck Bui-Huu | ca094f1 | 2006-06-14 10:47:18 +0200 | [diff] [blame] | 691 | spin_unlock_irq(&port->port_lock); |
Matthias Kaehlcke | 831c70f | 2007-07-13 21:25:25 +0200 | [diff] [blame] | 692 | mutex_unlock(mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* |
| 696 | * gs_write |
| 697 | */ |
| 698 | static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 699 | { |
| 700 | unsigned long flags; |
| 701 | struct gs_port *port = tty->driver_data; |
| 702 | int ret; |
| 703 | |
| 704 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 705 | pr_err("gs_write: NULL port pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | return -EIO; |
| 707 | } |
| 708 | |
| 709 | gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty, |
| 710 | count); |
| 711 | |
| 712 | if (count == 0) |
| 713 | return 0; |
| 714 | |
| 715 | spin_lock_irqsave(&port->port_lock, flags); |
| 716 | |
| 717 | if (port->port_dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 718 | pr_err("gs_write: (%d,%p) port is not connected\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | port->port_num, tty); |
| 720 | ret = -EIO; |
| 721 | goto exit; |
| 722 | } |
| 723 | |
| 724 | if (port->port_open_count == 0) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 725 | pr_err("gs_write: (%d,%p) port is closed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | port->port_num, tty); |
| 727 | ret = -EBADF; |
| 728 | goto exit; |
| 729 | } |
| 730 | |
| 731 | count = gs_buf_put(port->port_write_buf, buf, count); |
| 732 | |
| 733 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 734 | |
| 735 | gs_send(gs_device); |
| 736 | |
| 737 | gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty, |
| 738 | count); |
| 739 | |
| 740 | return count; |
| 741 | |
| 742 | exit: |
| 743 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * gs_put_char |
| 749 | */ |
Alan Cox | 4cd55ab | 2008-04-30 00:54:01 -0700 | [diff] [blame] | 750 | static int gs_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
| 752 | unsigned long flags; |
| 753 | struct gs_port *port = tty->driver_data; |
Alan Cox | 4cd55ab | 2008-04-30 00:54:01 -0700 | [diff] [blame] | 754 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
| 756 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 757 | pr_err("gs_put_char: NULL port pointer\n"); |
Alan Cox | 4cd55ab | 2008-04-30 00:54:01 -0700 | [diff] [blame] | 758 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 761 | gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n", |
| 762 | port->port_num, tty, ch, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
| 764 | spin_lock_irqsave(&port->port_lock, flags); |
| 765 | |
| 766 | if (port->port_dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 767 | pr_err("gs_put_char: (%d,%p) port is not connected\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | port->port_num, tty); |
| 769 | goto exit; |
| 770 | } |
| 771 | |
| 772 | if (port->port_open_count == 0) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 773 | pr_err("gs_put_char: (%d,%p) port is closed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | port->port_num, tty); |
| 775 | goto exit; |
| 776 | } |
| 777 | |
Alan Cox | 4cd55ab | 2008-04-30 00:54:01 -0700 | [diff] [blame] | 778 | ret = gs_buf_put(port->port_write_buf, &ch, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | exit: |
| 781 | spin_unlock_irqrestore(&port->port_lock, flags); |
Alan Cox | 4cd55ab | 2008-04-30 00:54:01 -0700 | [diff] [blame] | 782 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* |
| 786 | * gs_flush_chars |
| 787 | */ |
| 788 | static void gs_flush_chars(struct tty_struct *tty) |
| 789 | { |
| 790 | unsigned long flags; |
| 791 | struct gs_port *port = tty->driver_data; |
| 792 | |
| 793 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 794 | pr_err("gs_flush_chars: NULL port pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return; |
| 796 | } |
| 797 | |
| 798 | gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); |
| 799 | |
| 800 | spin_lock_irqsave(&port->port_lock, flags); |
| 801 | |
| 802 | if (port->port_dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 803 | pr_err("gs_flush_chars: (%d,%p) port is not connected\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | port->port_num, tty); |
| 805 | goto exit; |
| 806 | } |
| 807 | |
| 808 | if (port->port_open_count == 0) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 809 | pr_err("gs_flush_chars: (%d,%p) port is closed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | port->port_num, tty); |
| 811 | goto exit; |
| 812 | } |
| 813 | |
| 814 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 815 | |
| 816 | gs_send(gs_device); |
| 817 | |
| 818 | return; |
| 819 | |
| 820 | exit: |
| 821 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * gs_write_room |
| 826 | */ |
| 827 | static int gs_write_room(struct tty_struct *tty) |
| 828 | { |
| 829 | |
| 830 | int room = 0; |
| 831 | unsigned long flags; |
| 832 | struct gs_port *port = tty->driver_data; |
| 833 | |
| 834 | |
| 835 | if (port == NULL) |
| 836 | return 0; |
| 837 | |
| 838 | spin_lock_irqsave(&port->port_lock, flags); |
| 839 | |
| 840 | if (port->port_dev != NULL && port->port_open_count > 0 |
| 841 | && port->port_write_buf != NULL) |
| 842 | room = gs_buf_space_avail(port->port_write_buf); |
| 843 | |
| 844 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 845 | |
| 846 | gs_debug("gs_write_room: (%d,%p) room=%d\n", |
| 847 | port->port_num, tty, room); |
| 848 | |
| 849 | return room; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * gs_chars_in_buffer |
| 854 | */ |
| 855 | static int gs_chars_in_buffer(struct tty_struct *tty) |
| 856 | { |
| 857 | int chars = 0; |
| 858 | unsigned long flags; |
| 859 | struct gs_port *port = tty->driver_data; |
| 860 | |
| 861 | if (port == NULL) |
| 862 | return 0; |
| 863 | |
| 864 | spin_lock_irqsave(&port->port_lock, flags); |
| 865 | |
| 866 | if (port->port_dev != NULL && port->port_open_count > 0 |
| 867 | && port->port_write_buf != NULL) |
| 868 | chars = gs_buf_data_avail(port->port_write_buf); |
| 869 | |
| 870 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 871 | |
| 872 | gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n", |
| 873 | port->port_num, tty, chars); |
| 874 | |
| 875 | return chars; |
| 876 | } |
| 877 | |
| 878 | /* |
| 879 | * gs_throttle |
| 880 | */ |
| 881 | static void gs_throttle(struct tty_struct *tty) |
| 882 | { |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * gs_unthrottle |
| 887 | */ |
| 888 | static void gs_unthrottle(struct tty_struct *tty) |
| 889 | { |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * gs_break |
| 894 | */ |
| 895 | static void gs_break(struct tty_struct *tty, int break_state) |
| 896 | { |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * gs_ioctl |
| 901 | */ |
| 902 | static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) |
| 903 | { |
| 904 | struct gs_port *port = tty->driver_data; |
| 905 | |
| 906 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 907 | pr_err("gs_ioctl: NULL port pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return -EIO; |
| 909 | } |
| 910 | |
| 911 | gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n", |
| 912 | port->port_num, tty, file, cmd, arg); |
| 913 | |
| 914 | /* handle ioctls */ |
| 915 | |
| 916 | /* could not handle ioctl */ |
| 917 | return -ENOIOCTLCMD; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * gs_set_termios |
| 922 | */ |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 923 | static void gs_set_termios(struct tty_struct *tty, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | { |
| 925 | } |
| 926 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 927 | static const struct tty_operations gs_tty_ops = { |
| 928 | .open = gs_open, |
| 929 | .close = gs_close, |
| 930 | .write = gs_write, |
| 931 | .put_char = gs_put_char, |
| 932 | .flush_chars = gs_flush_chars, |
| 933 | .write_room = gs_write_room, |
| 934 | .ioctl = gs_ioctl, |
| 935 | .set_termios = gs_set_termios, |
| 936 | .throttle = gs_throttle, |
| 937 | .unthrottle = gs_unthrottle, |
| 938 | .break_ctl = gs_break, |
| 939 | .chars_in_buffer = gs_chars_in_buffer, |
| 940 | }; |
| 941 | |
| 942 | /*-------------------------------------------------------------------------*/ |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | /* |
| 945 | * gs_send |
| 946 | * |
| 947 | * This function finds available write requests, calls |
| 948 | * gs_send_packet to fill these packets with data, and |
| 949 | * continues until either there are no more write requests |
| 950 | * available or no more data to send. This function is |
| 951 | * run whenever data arrives or write requests are available. |
| 952 | */ |
| 953 | static int gs_send(struct gs_dev *dev) |
| 954 | { |
| 955 | int ret,len; |
| 956 | unsigned long flags; |
| 957 | struct usb_ep *ep; |
| 958 | struct usb_request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
| 960 | if (dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 961 | pr_err("gs_send: NULL device pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | return -ENODEV; |
| 963 | } |
| 964 | |
| 965 | spin_lock_irqsave(&dev->dev_lock, flags); |
| 966 | |
| 967 | ep = dev->dev_in_ep; |
| 968 | |
| 969 | while(!list_empty(&dev->dev_req_list)) { |
| 970 | |
David Brownell | 2c2d28a | 2008-05-07 14:24:10 -0700 | [diff] [blame^] | 971 | req = list_entry(dev->dev_req_list.next, |
| 972 | struct usb_request, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
| 974 | len = gs_send_packet(dev, req->buf, ep->maxpacket); |
| 975 | |
| 976 | if (len > 0) { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 977 | gs_debug_level(3, "gs_send: len=%d, 0x%2.2x " |
| 978 | "0x%2.2x 0x%2.2x ...\n", len, |
| 979 | *((unsigned char *)req->buf), |
| 980 | *((unsigned char *)req->buf+1), |
| 981 | *((unsigned char *)req->buf+2)); |
David Brownell | 2c2d28a | 2008-05-07 14:24:10 -0700 | [diff] [blame^] | 982 | list_del(&req->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | req->length = len; |
Eugeny S. Mints | 80f8af0 | 2006-09-02 03:59:19 -0700 | [diff] [blame] | 984 | spin_unlock_irqrestore(&dev->dev_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 986 | pr_err( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | "gs_send: cannot queue read request, ret=%d\n", |
| 988 | ret); |
Eugeny S. Mints | 80f8af0 | 2006-09-02 03:59:19 -0700 | [diff] [blame] | 989 | spin_lock_irqsave(&dev->dev_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | break; |
| 991 | } |
Eugeny S. Mints | 80f8af0 | 2006-09-02 03:59:19 -0700 | [diff] [blame] | 992 | spin_lock_irqsave(&dev->dev_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } else { |
| 994 | break; |
| 995 | } |
| 996 | |
| 997 | } |
| 998 | |
| 999 | spin_unlock_irqrestore(&dev->dev_lock, flags); |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * gs_send_packet |
| 1006 | * |
| 1007 | * If there is data to send, a packet is built in the given |
| 1008 | * buffer and the size is returned. If there is no data to |
| 1009 | * send, 0 is returned. If there is any error a negative |
| 1010 | * error number is returned. |
| 1011 | * |
| 1012 | * Called during USB completion routine, on interrupt time. |
| 1013 | * |
| 1014 | * We assume that disconnect will not happen until all completion |
| 1015 | * routines have completed, so we can assume that the dev_port |
| 1016 | * array does not change during the lifetime of this function. |
| 1017 | */ |
| 1018 | static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size) |
| 1019 | { |
| 1020 | unsigned int len; |
| 1021 | struct gs_port *port; |
| 1022 | |
| 1023 | /* TEMPORARY -- only port 0 is supported right now */ |
| 1024 | port = dev->dev_port[0]; |
| 1025 | |
| 1026 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1027 | pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | return -EIO; |
| 1029 | } |
| 1030 | |
| 1031 | spin_lock(&port->port_lock); |
| 1032 | |
| 1033 | len = gs_buf_data_avail(port->port_write_buf); |
| 1034 | if (len < size) |
| 1035 | size = len; |
| 1036 | |
| 1037 | if (size == 0) |
| 1038 | goto exit; |
| 1039 | |
| 1040 | size = gs_buf_get(port->port_write_buf, packet, size); |
| 1041 | |
| 1042 | if (port->port_tty) |
| 1043 | wake_up_interruptible(&port->port_tty->write_wait); |
| 1044 | |
| 1045 | exit: |
| 1046 | spin_unlock(&port->port_lock); |
| 1047 | return size; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * gs_recv_packet |
| 1052 | * |
| 1053 | * Called for each USB packet received. Reads the packet |
| 1054 | * header and stuffs the data in the appropriate tty buffer. |
| 1055 | * Returns 0 if successful, or a negative error number. |
| 1056 | * |
| 1057 | * Called during USB completion routine, on interrupt time. |
| 1058 | * |
| 1059 | * We assume that disconnect will not happen until all completion |
| 1060 | * routines have completed, so we can assume that the dev_port |
| 1061 | * array does not change during the lifetime of this function. |
| 1062 | */ |
| 1063 | static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size) |
| 1064 | { |
| 1065 | unsigned int len; |
| 1066 | struct gs_port *port; |
| 1067 | int ret; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1068 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | /* TEMPORARY -- only port 0 is supported right now */ |
| 1071 | port = dev->dev_port[0]; |
| 1072 | |
| 1073 | if (port == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1074 | pr_err("gs_recv_packet: port=%d, NULL port pointer\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | port->port_num); |
| 1076 | return -EIO; |
| 1077 | } |
| 1078 | |
| 1079 | spin_lock(&port->port_lock); |
| 1080 | |
| 1081 | if (port->port_open_count == 0) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1082 | pr_err("gs_recv_packet: port=%d, port is closed\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | port->port_num); |
| 1084 | ret = -EIO; |
| 1085 | goto exit; |
| 1086 | } |
| 1087 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1088 | |
| 1089 | tty = port->port_tty; |
| 1090 | |
| 1091 | if (tty == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1092 | pr_err("gs_recv_packet: port=%d, NULL tty pointer\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | port->port_num); |
| 1094 | ret = -EIO; |
| 1095 | goto exit; |
| 1096 | } |
| 1097 | |
| 1098 | if (port->port_tty->magic != TTY_MAGIC) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1099 | pr_err("gs_recv_packet: port=%d, bad tty magic\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | port->port_num); |
| 1101 | ret = -EIO; |
| 1102 | goto exit; |
| 1103 | } |
| 1104 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1105 | len = tty_buffer_request_room(tty, size); |
| 1106 | if (len > 0) { |
| 1107 | tty_insert_flip_string(tty, packet, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | tty_flip_buffer_push(port->port_tty); |
| 1109 | wake_up_interruptible(&port->port_tty->read_wait); |
| 1110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | exit: |
| 1113 | spin_unlock(&port->port_lock); |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * gs_read_complete |
| 1119 | */ |
| 1120 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req) |
| 1121 | { |
| 1122 | int ret; |
| 1123 | struct gs_dev *dev = ep->driver_data; |
| 1124 | |
| 1125 | if (dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1126 | pr_err("gs_read_complete: NULL device pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | switch(req->status) { |
| 1131 | case 0: |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1132 | /* normal completion */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | gs_recv_packet(dev, req->buf, req->actual); |
| 1134 | requeue: |
| 1135 | req->length = ep->maxpacket; |
| 1136 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1137 | pr_err( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | "gs_read_complete: cannot queue read request, ret=%d\n", |
| 1139 | ret); |
| 1140 | } |
| 1141 | break; |
| 1142 | |
| 1143 | case -ESHUTDOWN: |
| 1144 | /* disconnect */ |
| 1145 | gs_debug("gs_read_complete: shutdown\n"); |
| 1146 | gs_free_req(ep, req); |
| 1147 | break; |
| 1148 | |
| 1149 | default: |
| 1150 | /* unexpected */ |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1151 | pr_err( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | "gs_read_complete: unexpected status error, status=%d\n", |
| 1153 | req->status); |
| 1154 | goto requeue; |
| 1155 | break; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * gs_write_complete |
| 1161 | */ |
| 1162 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) |
| 1163 | { |
| 1164 | struct gs_dev *dev = ep->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
| 1166 | if (dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1167 | pr_err("gs_write_complete: NULL device pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | switch(req->status) { |
| 1172 | case 0: |
| 1173 | /* normal completion */ |
| 1174 | requeue: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | spin_lock(&dev->dev_lock); |
David Brownell | 2c2d28a | 2008-05-07 14:24:10 -0700 | [diff] [blame^] | 1176 | list_add(&req->list, &dev->dev_req_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | spin_unlock(&dev->dev_lock); |
| 1178 | |
| 1179 | gs_send(dev); |
| 1180 | |
| 1181 | break; |
| 1182 | |
| 1183 | case -ESHUTDOWN: |
| 1184 | /* disconnect */ |
| 1185 | gs_debug("gs_write_complete: shutdown\n"); |
| 1186 | gs_free_req(ep, req); |
| 1187 | break; |
| 1188 | |
| 1189 | default: |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1190 | pr_err( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | "gs_write_complete: unexpected status error, status=%d\n", |
| 1192 | req->status); |
| 1193 | goto requeue; |
| 1194 | break; |
| 1195 | } |
| 1196 | } |
| 1197 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1198 | /*-------------------------------------------------------------------------*/ |
| 1199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | /* Gadget Driver */ |
| 1201 | |
| 1202 | /* |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1203 | * gs_unbind |
| 1204 | * |
| 1205 | * Called on module unload. Frees the control request and device |
| 1206 | * structure. |
| 1207 | */ |
| 1208 | static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget) |
| 1209 | { |
| 1210 | struct gs_dev *dev = get_gadget_data(gadget); |
| 1211 | |
| 1212 | gs_device = NULL; |
| 1213 | |
| 1214 | /* read/write requests already freed, only control request remains */ |
| 1215 | if (dev != NULL) { |
| 1216 | if (dev->dev_ctrl_req != NULL) { |
| 1217 | gs_free_req(gadget->ep0, dev->dev_ctrl_req); |
| 1218 | dev->dev_ctrl_req = NULL; |
| 1219 | } |
| 1220 | gs_free_ports(dev); |
| 1221 | if (dev->dev_notify_ep) |
| 1222 | usb_ep_disable(dev->dev_notify_ep); |
| 1223 | if (dev->dev_in_ep) |
| 1224 | usb_ep_disable(dev->dev_in_ep); |
| 1225 | if (dev->dev_out_ep) |
| 1226 | usb_ep_disable(dev->dev_out_ep); |
| 1227 | kfree(dev); |
| 1228 | set_gadget_data(gadget, NULL); |
| 1229 | } |
| 1230 | |
| 1231 | pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME, |
| 1232 | GS_VERSION_STR); |
| 1233 | } |
| 1234 | |
| 1235 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | * gs_bind |
| 1237 | * |
| 1238 | * Called on module load. Allocates and initializes the device |
| 1239 | * structure and a control request. |
| 1240 | */ |
David Brownell | 329af28 | 2006-02-18 12:31:05 -0800 | [diff] [blame] | 1241 | static int __init gs_bind(struct usb_gadget *gadget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | { |
| 1243 | int ret; |
| 1244 | struct usb_ep *ep; |
| 1245 | struct gs_dev *dev; |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 1246 | int gcnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 1248 | /* Some controllers can't support CDC ACM: |
| 1249 | * - sh doesn't support multiple interfaces or configs; |
| 1250 | * - sa1100 doesn't have a third interrupt endpoint |
| 1251 | */ |
| 1252 | if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | use_acm = 0; |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 1254 | |
| 1255 | gcnum = usb_gadget_controller_number(gadget); |
| 1256 | if (gcnum >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | gs_device_desc.bcdDevice = |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 1258 | cpu_to_le16(GS_VERSION_NUM | gcnum); |
| 1259 | else { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1260 | pr_warning("gs_bind: controller '%s' not recognized\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | gadget->name); |
| 1262 | /* unrecognized, but safe unless bulk is REALLY quirky */ |
| 1263 | gs_device_desc.bcdDevice = |
| 1264 | __constant_cpu_to_le16(GS_VERSION_NUM|0x0099); |
| 1265 | } |
| 1266 | |
| 1267 | usb_ep_autoconfig_reset(gadget); |
| 1268 | |
| 1269 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc); |
| 1270 | if (!ep) |
| 1271 | goto autoconf_fail; |
| 1272 | EP_IN_NAME = ep->name; |
| 1273 | ep->driver_data = ep; /* claim the endpoint */ |
| 1274 | |
| 1275 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc); |
| 1276 | if (!ep) |
| 1277 | goto autoconf_fail; |
| 1278 | EP_OUT_NAME = ep->name; |
| 1279 | ep->driver_data = ep; /* claim the endpoint */ |
| 1280 | |
| 1281 | if (use_acm) { |
| 1282 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc); |
| 1283 | if (!ep) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1284 | pr_err("gs_bind: cannot run ACM on %s\n", gadget->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | goto autoconf_fail; |
| 1286 | } |
| 1287 | gs_device_desc.idProduct = __constant_cpu_to_le16( |
| 1288 | GS_CDC_PRODUCT_ID), |
| 1289 | EP_NOTIFY_NAME = ep->name; |
| 1290 | ep->driver_data = ep; /* claim the endpoint */ |
| 1291 | } |
| 1292 | |
| 1293 | gs_device_desc.bDeviceClass = use_acm |
| 1294 | ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC; |
| 1295 | gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
| 1296 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1297 | if (gadget_is_dualspeed(gadget)) { |
| 1298 | gs_qualifier_desc.bDeviceClass = use_acm |
| 1299 | ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC; |
| 1300 | /* assume ep0 uses the same packet size for both speeds */ |
| 1301 | gs_qualifier_desc.bMaxPacketSize0 = |
| 1302 | gs_device_desc.bMaxPacketSize0; |
| 1303 | /* assume endpoints are dual-speed */ |
| 1304 | gs_highspeed_notify_desc.bEndpointAddress = |
| 1305 | gs_fullspeed_notify_desc.bEndpointAddress; |
| 1306 | gs_highspeed_in_desc.bEndpointAddress = |
| 1307 | gs_fullspeed_in_desc.bEndpointAddress; |
| 1308 | gs_highspeed_out_desc.bEndpointAddress = |
| 1309 | gs_fullspeed_out_desc.bEndpointAddress; |
| 1310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | usb_gadget_set_selfpowered(gadget); |
| 1313 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1314 | if (gadget_is_otg(gadget)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | gs_otg_descriptor.bmAttributes |= USB_OTG_HNP, |
| 1316 | gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 1317 | gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 1318 | } |
| 1319 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 1320 | gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | if (dev == NULL) |
| 1322 | return -ENOMEM; |
| 1323 | |
| 1324 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 1325 | init_utsname()->sysname, init_utsname()->release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | gadget->name); |
| 1327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | dev->dev_gadget = gadget; |
| 1329 | spin_lock_init(&dev->dev_lock); |
| 1330 | INIT_LIST_HEAD(&dev->dev_req_list); |
| 1331 | set_gadget_data(gadget, dev); |
| 1332 | |
| 1333 | if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1334 | pr_err("gs_bind: cannot allocate ports\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | gs_unbind(gadget); |
| 1336 | return ret; |
| 1337 | } |
| 1338 | |
| 1339 | /* preallocate control response and buffer */ |
| 1340 | dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN, |
| 1341 | GFP_KERNEL); |
| 1342 | if (dev->dev_ctrl_req == NULL) { |
| 1343 | gs_unbind(gadget); |
| 1344 | return -ENOMEM; |
| 1345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | gadget->ep0->driver_data = dev; |
| 1347 | |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1348 | pr_info("gs_bind: %s %s bound\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | GS_LONG_NAME, GS_VERSION_STR); |
| 1350 | |
| 1351 | return 0; |
| 1352 | |
| 1353 | autoconf_fail: |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1354 | pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | return -ENODEV; |
| 1356 | } |
| 1357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | static int gs_setup_standard(struct usb_gadget *gadget, |
| 1359 | const struct usb_ctrlrequest *ctrl) |
| 1360 | { |
| 1361 | int ret = -EOPNOTSUPP; |
| 1362 | struct gs_dev *dev = get_gadget_data(gadget); |
| 1363 | struct usb_request *req = dev->dev_ctrl_req; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 1364 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 1365 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 1366 | u16 wLength = le16_to_cpu(ctrl->wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
| 1368 | switch (ctrl->bRequest) { |
| 1369 | case USB_REQ_GET_DESCRIPTOR: |
| 1370 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1371 | break; |
| 1372 | |
| 1373 | switch (wValue >> 8) { |
| 1374 | case USB_DT_DEVICE: |
| 1375 | ret = min(wLength, |
| 1376 | (u16)sizeof(struct usb_device_descriptor)); |
| 1377 | memcpy(req->buf, &gs_device_desc, ret); |
| 1378 | break; |
| 1379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | case USB_DT_DEVICE_QUALIFIER: |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1381 | if (!gadget_is_dualspeed(gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | ret = min(wLength, |
| 1384 | (u16)sizeof(struct usb_qualifier_descriptor)); |
| 1385 | memcpy(req->buf, &gs_qualifier_desc, ret); |
| 1386 | break; |
| 1387 | |
| 1388 | case USB_DT_OTHER_SPEED_CONFIG: |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1389 | if (!gadget_is_dualspeed(gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | break; |
| 1391 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | case USB_DT_CONFIG: |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1393 | ret = gs_build_config_buf(req->buf, gadget, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | wValue >> 8, wValue & 0xff, |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1395 | gadget_is_otg(gadget)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | if (ret >= 0) |
| 1397 | ret = min(wLength, (u16)ret); |
| 1398 | break; |
| 1399 | |
| 1400 | case USB_DT_STRING: |
| 1401 | /* wIndex == language code. */ |
| 1402 | ret = usb_gadget_get_string(&gs_string_table, |
| 1403 | wValue & 0xff, req->buf); |
| 1404 | if (ret >= 0) |
| 1405 | ret = min(wLength, (u16)ret); |
| 1406 | break; |
| 1407 | } |
| 1408 | break; |
| 1409 | |
| 1410 | case USB_REQ_SET_CONFIGURATION: |
| 1411 | if (ctrl->bRequestType != 0) |
| 1412 | break; |
| 1413 | spin_lock(&dev->dev_lock); |
| 1414 | ret = gs_set_config(dev, wValue); |
| 1415 | spin_unlock(&dev->dev_lock); |
| 1416 | break; |
| 1417 | |
| 1418 | case USB_REQ_GET_CONFIGURATION: |
| 1419 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1420 | break; |
| 1421 | *(u8 *)req->buf = dev->dev_config; |
| 1422 | ret = min(wLength, (u16)1); |
| 1423 | break; |
| 1424 | |
| 1425 | case USB_REQ_SET_INTERFACE: |
| 1426 | if (ctrl->bRequestType != USB_RECIP_INTERFACE |
| 1427 | || !dev->dev_config |
| 1428 | || wIndex >= GS_MAX_NUM_INTERFACES) |
| 1429 | break; |
| 1430 | if (dev->dev_config == GS_BULK_CONFIG_ID |
| 1431 | && wIndex != GS_BULK_INTERFACE_ID) |
| 1432 | break; |
| 1433 | /* no alternate interface settings */ |
| 1434 | if (wValue != 0) |
| 1435 | break; |
| 1436 | spin_lock(&dev->dev_lock); |
| 1437 | /* PXA hardware partially handles SET_INTERFACE; |
| 1438 | * we need to kluge around that interference. */ |
| 1439 | if (gadget_is_pxa(gadget)) { |
| 1440 | ret = gs_set_config(dev, use_acm ? |
| 1441 | GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID); |
| 1442 | goto set_interface_done; |
| 1443 | } |
| 1444 | if (dev->dev_config != GS_BULK_CONFIG_ID |
| 1445 | && wIndex == GS_CONTROL_INTERFACE_ID) { |
| 1446 | if (dev->dev_notify_ep) { |
| 1447 | usb_ep_disable(dev->dev_notify_ep); |
| 1448 | usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc); |
| 1449 | } |
| 1450 | } else { |
| 1451 | usb_ep_disable(dev->dev_in_ep); |
| 1452 | usb_ep_disable(dev->dev_out_ep); |
| 1453 | usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc); |
| 1454 | usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc); |
| 1455 | } |
| 1456 | ret = 0; |
| 1457 | set_interface_done: |
| 1458 | spin_unlock(&dev->dev_lock); |
| 1459 | break; |
| 1460 | |
| 1461 | case USB_REQ_GET_INTERFACE: |
| 1462 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) |
| 1463 | || dev->dev_config == GS_NO_CONFIG_ID) |
| 1464 | break; |
| 1465 | if (wIndex >= GS_MAX_NUM_INTERFACES |
| 1466 | || (dev->dev_config == GS_BULK_CONFIG_ID |
| 1467 | && wIndex != GS_BULK_INTERFACE_ID)) { |
| 1468 | ret = -EDOM; |
| 1469 | break; |
| 1470 | } |
| 1471 | /* no alternate interface settings */ |
| 1472 | *(u8 *)req->buf = 0; |
| 1473 | ret = min(wLength, (u16)1); |
| 1474 | break; |
| 1475 | |
| 1476 | default: |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1477 | pr_err("gs_setup: unknown standard request, type=%02x, " |
| 1478 | "request=%02x, value=%04x, index=%04x, length=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | ctrl->bRequestType, ctrl->bRequest, |
| 1480 | wValue, wIndex, wLength); |
| 1481 | break; |
| 1482 | } |
| 1483 | |
| 1484 | return ret; |
| 1485 | } |
| 1486 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1487 | static void gs_setup_complete_set_line_coding(struct usb_ep *ep, |
| 1488 | struct usb_request *req) |
| 1489 | { |
| 1490 | struct gs_dev *dev = ep->driver_data; |
| 1491 | struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ |
| 1492 | |
| 1493 | switch (req->status) { |
| 1494 | case 0: |
| 1495 | /* normal completion */ |
| 1496 | if (req->actual != sizeof(port->port_line_coding)) |
| 1497 | usb_ep_set_halt(ep); |
| 1498 | else if (port) { |
| 1499 | struct usb_cdc_line_coding *value = req->buf; |
| 1500 | |
| 1501 | /* REVISIT: we currently just remember this data. |
| 1502 | * If we change that, (a) validate it first, then |
| 1503 | * (b) update whatever hardware needs updating. |
| 1504 | */ |
| 1505 | spin_lock(&port->port_lock); |
| 1506 | port->port_line_coding = *value; |
| 1507 | spin_unlock(&port->port_lock); |
| 1508 | } |
| 1509 | break; |
| 1510 | |
| 1511 | case -ESHUTDOWN: |
| 1512 | /* disconnect */ |
| 1513 | gs_free_req(ep, req); |
| 1514 | break; |
| 1515 | |
| 1516 | default: |
| 1517 | /* unexpected */ |
| 1518 | break; |
| 1519 | } |
| 1520 | return; |
| 1521 | } |
| 1522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | static int gs_setup_class(struct usb_gadget *gadget, |
| 1524 | const struct usb_ctrlrequest *ctrl) |
| 1525 | { |
| 1526 | int ret = -EOPNOTSUPP; |
| 1527 | struct gs_dev *dev = get_gadget_data(gadget); |
| 1528 | struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ |
| 1529 | struct usb_request *req = dev->dev_ctrl_req; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 1530 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 1531 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 1532 | u16 wLength = le16_to_cpu(ctrl->wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
| 1534 | switch (ctrl->bRequest) { |
| 1535 | case USB_CDC_REQ_SET_LINE_CODING: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 1536 | if (wLength != sizeof(struct usb_cdc_line_coding)) |
| 1537 | break; |
| 1538 | ret = wLength; |
| 1539 | req->complete = gs_setup_complete_set_line_coding; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | break; |
| 1541 | |
| 1542 | case USB_CDC_REQ_GET_LINE_CODING: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 1543 | ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | if (port) { |
| 1545 | spin_lock(&port->port_lock); |
| 1546 | memcpy(req->buf, &port->port_line_coding, ret); |
| 1547 | spin_unlock(&port->port_lock); |
| 1548 | } |
| 1549 | break; |
| 1550 | |
| 1551 | case USB_CDC_REQ_SET_CONTROL_LINE_STATE: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 1552 | if (wLength != 0) |
| 1553 | break; |
| 1554 | ret = 0; |
| 1555 | if (port) { |
| 1556 | /* REVISIT: we currently just remember this data. |
| 1557 | * If we change that, update whatever hardware needs |
| 1558 | * updating. |
| 1559 | */ |
| 1560 | spin_lock(&port->port_lock); |
| 1561 | port->port_handshake_bits = wValue; |
| 1562 | spin_unlock(&port->port_lock); |
| 1563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | break; |
| 1565 | |
| 1566 | default: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 1567 | /* NOTE: strictly speaking, we should accept AT-commands |
| 1568 | * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE. |
| 1569 | * But our call management descriptor says we don't handle |
| 1570 | * call management, so we should be able to get by without |
| 1571 | * handling those "required" commands (except by stalling). |
| 1572 | */ |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1573 | pr_err("gs_setup: unknown class request, " |
David Brownell | 49b4f90 | 2007-08-26 12:44:24 -0700 | [diff] [blame] | 1574 | "type=%02x, request=%02x, value=%04x, " |
| 1575 | "index=%04x, length=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | ctrl->bRequestType, ctrl->bRequest, |
| 1577 | wValue, wIndex, wLength); |
| 1578 | break; |
| 1579 | } |
| 1580 | |
| 1581 | return ret; |
| 1582 | } |
| 1583 | |
| 1584 | /* |
| 1585 | * gs_setup_complete |
| 1586 | */ |
| 1587 | static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 1588 | { |
| 1589 | if (req->status || req->actual != req->length) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1590 | pr_err("gs_setup_complete: status error, status=%d, " |
| 1591 | "actual=%d, length=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | req->status, req->actual, req->length); |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | /* |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1597 | * gs_setup |
| 1598 | * |
| 1599 | * Implements all the control endpoint functionality that's not |
| 1600 | * handled in hardware or the hardware driver. |
| 1601 | * |
| 1602 | * Returns the size of the data sent to the host, or a negative |
| 1603 | * error number. |
| 1604 | */ |
| 1605 | static int gs_setup(struct usb_gadget *gadget, |
| 1606 | const struct usb_ctrlrequest *ctrl) |
| 1607 | { |
| 1608 | int ret = -EOPNOTSUPP; |
| 1609 | struct gs_dev *dev = get_gadget_data(gadget); |
| 1610 | struct usb_request *req = dev->dev_ctrl_req; |
| 1611 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 1612 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 1613 | u16 wLength = le16_to_cpu(ctrl->wLength); |
| 1614 | |
| 1615 | req->complete = gs_setup_complete; |
| 1616 | |
| 1617 | switch (ctrl->bRequestType & USB_TYPE_MASK) { |
| 1618 | case USB_TYPE_STANDARD: |
| 1619 | ret = gs_setup_standard(gadget, ctrl); |
| 1620 | break; |
| 1621 | |
| 1622 | case USB_TYPE_CLASS: |
| 1623 | ret = gs_setup_class(gadget, ctrl); |
| 1624 | break; |
| 1625 | |
| 1626 | default: |
| 1627 | pr_err("gs_setup: unknown request, type=%02x, request=%02x, " |
| 1628 | "value=%04x, index=%04x, length=%d\n", |
| 1629 | ctrl->bRequestType, ctrl->bRequest, |
| 1630 | wValue, wIndex, wLength); |
| 1631 | break; |
| 1632 | } |
| 1633 | |
| 1634 | /* respond with data transfer before status phase? */ |
| 1635 | if (ret >= 0) { |
| 1636 | req->length = ret; |
| 1637 | req->zero = ret < wLength |
| 1638 | && (ret % gadget->ep0->maxpacket) == 0; |
| 1639 | ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 1640 | if (ret < 0) { |
| 1641 | pr_err("gs_setup: cannot queue response, ret=%d\n", |
| 1642 | ret); |
| 1643 | req->status = 0; |
| 1644 | gs_setup_complete(gadget->ep0, req); |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | /* device either stalls (ret < 0) or reports success */ |
| 1649 | return ret; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | * gs_disconnect |
| 1654 | * |
| 1655 | * Called when the device is disconnected. Frees the closed |
| 1656 | * ports and disconnects open ports. Open ports will be freed |
| 1657 | * on close. Then reallocates the ports for the next connection. |
| 1658 | */ |
| 1659 | static void gs_disconnect(struct usb_gadget *gadget) |
| 1660 | { |
| 1661 | unsigned long flags; |
| 1662 | struct gs_dev *dev = get_gadget_data(gadget); |
| 1663 | |
| 1664 | spin_lock_irqsave(&dev->dev_lock, flags); |
| 1665 | |
| 1666 | gs_reset_config(dev); |
| 1667 | |
| 1668 | /* free closed ports and disconnect open ports */ |
| 1669 | /* (open ports will be freed when closed) */ |
| 1670 | gs_free_ports(dev); |
| 1671 | |
| 1672 | /* re-allocate ports for the next connection */ |
| 1673 | if (gs_alloc_ports(dev, GFP_ATOMIC) != 0) |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1674 | pr_err("gs_disconnect: cannot re-allocate ports\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | |
| 1676 | spin_unlock_irqrestore(&dev->dev_lock, flags); |
| 1677 | |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1678 | pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1681 | static struct usb_gadget_driver gs_gadget_driver = { |
| 1682 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
| 1683 | .speed = USB_SPEED_HIGH, |
| 1684 | #else |
| 1685 | .speed = USB_SPEED_FULL, |
| 1686 | #endif /* CONFIG_USB_GADGET_DUALSPEED */ |
| 1687 | .function = GS_LONG_NAME, |
| 1688 | .bind = gs_bind, |
| 1689 | .unbind = gs_unbind, |
| 1690 | .setup = gs_setup, |
| 1691 | .disconnect = gs_disconnect, |
| 1692 | .driver = { |
| 1693 | .name = GS_SHORT_NAME, |
| 1694 | .owner = THIS_MODULE, |
| 1695 | }, |
| 1696 | }; |
| 1697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | /* |
| 1699 | * gs_set_config |
| 1700 | * |
| 1701 | * Configures the device by enabling device specific |
| 1702 | * optimizations, setting up the endpoints, allocating |
| 1703 | * read and write requests and queuing read requests. |
| 1704 | * |
| 1705 | * The device lock must be held when calling this function. |
| 1706 | */ |
| 1707 | static int gs_set_config(struct gs_dev *dev, unsigned config) |
| 1708 | { |
| 1709 | int i; |
| 1710 | int ret = 0; |
| 1711 | struct usb_gadget *gadget = dev->dev_gadget; |
| 1712 | struct usb_ep *ep; |
| 1713 | struct usb_endpoint_descriptor *ep_desc; |
| 1714 | struct usb_request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
| 1716 | if (dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1717 | pr_err("gs_set_config: NULL device pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | if (config == dev->dev_config) |
| 1722 | return 0; |
| 1723 | |
| 1724 | gs_reset_config(dev); |
| 1725 | |
| 1726 | switch (config) { |
| 1727 | case GS_NO_CONFIG_ID: |
| 1728 | return 0; |
| 1729 | case GS_BULK_CONFIG_ID: |
| 1730 | if (use_acm) |
| 1731 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | break; |
| 1733 | case GS_ACM_CONFIG_ID: |
| 1734 | if (!use_acm) |
| 1735 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | break; |
| 1737 | default: |
| 1738 | return -EINVAL; |
| 1739 | } |
| 1740 | |
| 1741 | dev->dev_config = config; |
| 1742 | |
| 1743 | gadget_for_each_ep(ep, gadget) { |
| 1744 | |
| 1745 | if (EP_NOTIFY_NAME |
| 1746 | && strcmp(ep->name, EP_NOTIFY_NAME) == 0) { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1747 | ep_desc = choose_ep_desc(gadget, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | &gs_highspeed_notify_desc, |
| 1749 | &gs_fullspeed_notify_desc); |
| 1750 | ret = usb_ep_enable(ep,ep_desc); |
| 1751 | if (ret == 0) { |
| 1752 | ep->driver_data = dev; |
| 1753 | dev->dev_notify_ep = ep; |
| 1754 | dev->dev_notify_ep_desc = ep_desc; |
| 1755 | } else { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1756 | pr_err("gs_set_config: cannot enable NOTIFY " |
| 1757 | "endpoint %s, ret=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | ep->name, ret); |
| 1759 | goto exit_reset_config; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | else if (strcmp(ep->name, EP_IN_NAME) == 0) { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1764 | ep_desc = choose_ep_desc(gadget, |
| 1765 | &gs_highspeed_in_desc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | &gs_fullspeed_in_desc); |
| 1767 | ret = usb_ep_enable(ep,ep_desc); |
| 1768 | if (ret == 0) { |
| 1769 | ep->driver_data = dev; |
| 1770 | dev->dev_in_ep = ep; |
| 1771 | dev->dev_in_ep_desc = ep_desc; |
| 1772 | } else { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1773 | pr_err("gs_set_config: cannot enable IN " |
| 1774 | "endpoint %s, ret=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | ep->name, ret); |
| 1776 | goto exit_reset_config; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | else if (strcmp(ep->name, EP_OUT_NAME) == 0) { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1781 | ep_desc = choose_ep_desc(gadget, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | &gs_highspeed_out_desc, |
| 1783 | &gs_fullspeed_out_desc); |
| 1784 | ret = usb_ep_enable(ep,ep_desc); |
| 1785 | if (ret == 0) { |
| 1786 | ep->driver_data = dev; |
| 1787 | dev->dev_out_ep = ep; |
| 1788 | dev->dev_out_ep_desc = ep_desc; |
| 1789 | } else { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1790 | pr_err("gs_set_config: cannot enable OUT " |
| 1791 | "endpoint %s, ret=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | ep->name, ret); |
| 1793 | goto exit_reset_config; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | } |
| 1798 | |
| 1799 | if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL |
| 1800 | || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1801 | pr_err("gs_set_config: cannot find endpoints\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | ret = -ENODEV; |
| 1803 | goto exit_reset_config; |
| 1804 | } |
| 1805 | |
| 1806 | /* allocate and queue read requests */ |
| 1807 | ep = dev->dev_out_ep; |
| 1808 | for (i=0; i<read_q_size && ret == 0; i++) { |
| 1809 | if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) { |
| 1810 | req->complete = gs_read_complete; |
| 1811 | if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1812 | pr_err("gs_set_config: cannot queue read " |
| 1813 | "request, ret=%d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } |
| 1815 | } else { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1816 | pr_err("gs_set_config: cannot allocate " |
| 1817 | "read requests\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | ret = -ENOMEM; |
| 1819 | goto exit_reset_config; |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | /* allocate write requests, and put on free list */ |
| 1824 | ep = dev->dev_in_ep; |
| 1825 | for (i=0; i<write_q_size; i++) { |
David Brownell | 2c2d28a | 2008-05-07 14:24:10 -0700 | [diff] [blame^] | 1826 | req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); |
| 1827 | if (req) { |
| 1828 | req->complete = gs_write_complete; |
| 1829 | list_add(&req->list, &dev->dev_req_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | } else { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1831 | pr_err("gs_set_config: cannot allocate " |
| 1832 | "write requests\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | ret = -ENOMEM; |
| 1834 | goto exit_reset_config; |
| 1835 | } |
| 1836 | } |
| 1837 | |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 1838 | /* REVISIT the ACM mode should be able to actually *issue* some |
| 1839 | * notifications, for at least serial state change events if |
| 1840 | * not also for network connection; say so in bmCapabilities. |
| 1841 | */ |
| 1842 | |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1843 | pr_info("gs_set_config: %s configured, %s speed %s config\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | GS_LONG_NAME, |
| 1845 | gadget->speed == USB_SPEED_HIGH ? "high" : "full", |
| 1846 | config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM"); |
| 1847 | |
| 1848 | return 0; |
| 1849 | |
| 1850 | exit_reset_config: |
| 1851 | gs_reset_config(dev); |
| 1852 | return ret; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | * gs_reset_config |
| 1857 | * |
| 1858 | * Mark the device as not configured, disable all endpoints, |
| 1859 | * which forces completion of pending I/O and frees queued |
| 1860 | * requests, and free the remaining write requests on the |
| 1861 | * free list. |
| 1862 | * |
| 1863 | * The device lock must be held when calling this function. |
| 1864 | */ |
| 1865 | static void gs_reset_config(struct gs_dev *dev) |
| 1866 | { |
David Brownell | 2c2d28a | 2008-05-07 14:24:10 -0700 | [diff] [blame^] | 1867 | struct usb_request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
| 1869 | if (dev == NULL) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1870 | pr_err("gs_reset_config: NULL device pointer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | return; |
| 1872 | } |
| 1873 | |
| 1874 | if (dev->dev_config == GS_NO_CONFIG_ID) |
| 1875 | return; |
| 1876 | |
| 1877 | dev->dev_config = GS_NO_CONFIG_ID; |
| 1878 | |
| 1879 | /* free write requests on the free list */ |
| 1880 | while(!list_empty(&dev->dev_req_list)) { |
David Brownell | 2c2d28a | 2008-05-07 14:24:10 -0700 | [diff] [blame^] | 1881 | req = list_entry(dev->dev_req_list.next, |
| 1882 | struct usb_request, list); |
| 1883 | list_del(&req->list); |
| 1884 | gs_free_req(dev->dev_in_ep, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | /* disable endpoints, forcing completion of pending i/o; */ |
| 1888 | /* completion handlers free their requests in this case */ |
| 1889 | if (dev->dev_notify_ep) { |
| 1890 | usb_ep_disable(dev->dev_notify_ep); |
| 1891 | dev->dev_notify_ep = NULL; |
| 1892 | } |
| 1893 | if (dev->dev_in_ep) { |
| 1894 | usb_ep_disable(dev->dev_in_ep); |
| 1895 | dev->dev_in_ep = NULL; |
| 1896 | } |
| 1897 | if (dev->dev_out_ep) { |
| 1898 | usb_ep_disable(dev->dev_out_ep); |
| 1899 | dev->dev_out_ep = NULL; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * gs_build_config_buf |
| 1905 | * |
| 1906 | * Builds the config descriptors in the given buffer and returns the |
| 1907 | * length, or a negative error number. |
| 1908 | */ |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1909 | static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | u8 type, unsigned int index, int is_otg) |
| 1911 | { |
| 1912 | int len; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1913 | int high_speed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | const struct usb_config_descriptor *config_desc; |
| 1915 | const struct usb_descriptor_header **function; |
| 1916 | |
| 1917 | if (index >= gs_device_desc.bNumConfigurations) |
| 1918 | return -EINVAL; |
| 1919 | |
| 1920 | /* other speed switches high and full speed */ |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1921 | if (gadget_is_dualspeed(g)) { |
| 1922 | high_speed = (g->speed == USB_SPEED_HIGH); |
| 1923 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 1924 | high_speed = !high_speed; |
| 1925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | |
| 1927 | if (use_acm) { |
| 1928 | config_desc = &gs_acm_config_desc; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1929 | function = high_speed |
| 1930 | ? gs_acm_highspeed_function |
| 1931 | : gs_acm_fullspeed_function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | } else { |
| 1933 | config_desc = &gs_bulk_config_desc; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 1934 | function = high_speed |
| 1935 | ? gs_bulk_highspeed_function |
| 1936 | : gs_bulk_fullspeed_function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /* for now, don't advertise srp-only devices */ |
| 1940 | if (!is_otg) |
| 1941 | function++; |
| 1942 | |
| 1943 | len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function); |
| 1944 | if (len < 0) |
| 1945 | return len; |
| 1946 | |
| 1947 | ((struct usb_config_descriptor *)buf)->bDescriptorType = type; |
| 1948 | |
| 1949 | return len; |
| 1950 | } |
| 1951 | |
| 1952 | /* |
| 1953 | * gs_alloc_req |
| 1954 | * |
| 1955 | * Allocate a usb_request and its buffer. Returns a pointer to the |
| 1956 | * usb_request or NULL if there is an error. |
| 1957 | */ |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 1958 | static struct usb_request * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1959 | gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | { |
| 1961 | struct usb_request *req; |
| 1962 | |
| 1963 | if (ep == NULL) |
| 1964 | return NULL; |
| 1965 | |
| 1966 | req = usb_ep_alloc_request(ep, kmalloc_flags); |
| 1967 | |
| 1968 | if (req != NULL) { |
| 1969 | req->length = len; |
| 1970 | req->buf = kmalloc(len, kmalloc_flags); |
| 1971 | if (req->buf == NULL) { |
| 1972 | usb_ep_free_request(ep, req); |
| 1973 | return NULL; |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | return req; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * gs_free_req |
| 1982 | * |
| 1983 | * Free a usb_request and its buffer. |
| 1984 | */ |
| 1985 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req) |
| 1986 | { |
| 1987 | if (ep != NULL && req != NULL) { |
| 1988 | kfree(req->buf); |
| 1989 | usb_ep_free_request(ep, req); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | * gs_alloc_ports |
| 1995 | * |
| 1996 | * Allocate all ports and set the gs_dev struct to point to them. |
| 1997 | * Return 0 if successful, or a negative error number. |
| 1998 | * |
| 1999 | * The device lock is normally held when calling this function. |
| 2000 | */ |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 2001 | static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | { |
| 2003 | int i; |
| 2004 | struct gs_port *port; |
| 2005 | |
| 2006 | if (dev == NULL) |
| 2007 | return -EIO; |
| 2008 | |
| 2009 | for (i=0; i<GS_NUM_PORTS; i++) { |
Eric Sesterhenn | 7039f42 | 2006-02-27 13:34:10 -0800 | [diff] [blame] | 2010 | if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | return -ENOMEM; |
| 2012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | port->port_dev = dev; |
| 2014 | port->port_num = i; |
| 2015 | port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE); |
| 2016 | port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT; |
| 2017 | port->port_line_coding.bParityType = GS_DEFAULT_PARITY; |
| 2018 | port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS; |
| 2019 | spin_lock_init(&port->port_lock); |
| 2020 | init_waitqueue_head(&port->port_write_wait); |
| 2021 | |
| 2022 | dev->dev_port[i] = port; |
| 2023 | } |
| 2024 | |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * gs_free_ports |
| 2030 | * |
| 2031 | * Free all closed ports. Open ports are disconnected by |
| 2032 | * freeing their write buffers, setting their device pointers |
| 2033 | * and the pointers to them in the device to NULL. These |
| 2034 | * ports will be freed when closed. |
| 2035 | * |
| 2036 | * The device lock is normally held when calling this function. |
| 2037 | */ |
| 2038 | static void gs_free_ports(struct gs_dev *dev) |
| 2039 | { |
| 2040 | int i; |
| 2041 | unsigned long flags; |
| 2042 | struct gs_port *port; |
| 2043 | |
| 2044 | if (dev == NULL) |
| 2045 | return; |
| 2046 | |
| 2047 | for (i=0; i<GS_NUM_PORTS; i++) { |
| 2048 | if ((port=dev->dev_port[i]) != NULL) { |
| 2049 | dev->dev_port[i] = NULL; |
| 2050 | |
| 2051 | spin_lock_irqsave(&port->port_lock, flags); |
| 2052 | |
| 2053 | if (port->port_write_buf != NULL) { |
| 2054 | gs_buf_free(port->port_write_buf); |
| 2055 | port->port_write_buf = NULL; |
| 2056 | } |
| 2057 | |
| 2058 | if (port->port_open_count > 0 || port->port_in_use) { |
| 2059 | port->port_dev = NULL; |
| 2060 | wake_up_interruptible(&port->port_write_wait); |
| 2061 | if (port->port_tty) { |
Savin Zlobec | 4208978 | 2008-02-15 13:42:01 +0100 | [diff] [blame] | 2062 | tty_hangup(port->port_tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | } |
| 2064 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 2065 | } else { |
| 2066 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 2067 | kfree(port); |
| 2068 | } |
| 2069 | |
| 2070 | } |
| 2071 | } |
| 2072 | } |
| 2073 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 2074 | /*-------------------------------------------------------------------------*/ |
| 2075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | /* Circular Buffer */ |
| 2077 | |
| 2078 | /* |
| 2079 | * gs_buf_alloc |
| 2080 | * |
| 2081 | * Allocate a circular buffer and all associated memory. |
| 2082 | */ |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 2083 | static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | { |
| 2085 | struct gs_buf *gb; |
| 2086 | |
| 2087 | if (size == 0) |
| 2088 | return NULL; |
| 2089 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 2090 | gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | if (gb == NULL) |
| 2092 | return NULL; |
| 2093 | |
| 2094 | gb->buf_buf = kmalloc(size, kmalloc_flags); |
| 2095 | if (gb->buf_buf == NULL) { |
| 2096 | kfree(gb); |
| 2097 | return NULL; |
| 2098 | } |
| 2099 | |
| 2100 | gb->buf_size = size; |
| 2101 | gb->buf_get = gb->buf_put = gb->buf_buf; |
| 2102 | |
| 2103 | return gb; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * gs_buf_free |
| 2108 | * |
| 2109 | * Free the buffer and all associated memory. |
| 2110 | */ |
David Brownell | b29dbbd | 2007-05-25 20:40:31 -0700 | [diff] [blame] | 2111 | static void gs_buf_free(struct gs_buf *gb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 2113 | if (gb) { |
| 2114 | kfree(gb->buf_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | kfree(gb); |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * gs_buf_clear |
| 2121 | * |
| 2122 | * Clear out all data in the circular buffer. |
| 2123 | */ |
David Brownell | b29dbbd | 2007-05-25 20:40:31 -0700 | [diff] [blame] | 2124 | static void gs_buf_clear(struct gs_buf *gb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | { |
| 2126 | if (gb != NULL) |
| 2127 | gb->buf_get = gb->buf_put; |
| 2128 | /* equivalent to a get of all data available */ |
| 2129 | } |
| 2130 | |
| 2131 | /* |
| 2132 | * gs_buf_data_avail |
| 2133 | * |
| 2134 | * Return the number of bytes of data available in the circular |
| 2135 | * buffer. |
| 2136 | */ |
David Brownell | b29dbbd | 2007-05-25 20:40:31 -0700 | [diff] [blame] | 2137 | static unsigned int gs_buf_data_avail(struct gs_buf *gb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | { |
| 2139 | if (gb != NULL) |
| 2140 | return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size; |
| 2141 | else |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | /* |
| 2146 | * gs_buf_space_avail |
| 2147 | * |
| 2148 | * Return the number of bytes of space available in the circular |
| 2149 | * buffer. |
| 2150 | */ |
David Brownell | b29dbbd | 2007-05-25 20:40:31 -0700 | [diff] [blame] | 2151 | static unsigned int gs_buf_space_avail(struct gs_buf *gb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | { |
| 2153 | if (gb != NULL) |
| 2154 | return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size; |
| 2155 | else |
| 2156 | return 0; |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * gs_buf_put |
| 2161 | * |
| 2162 | * Copy data data from a user buffer and put it into the circular buffer. |
| 2163 | * Restrict to the amount of space available. |
| 2164 | * |
| 2165 | * Return the number of bytes copied. |
| 2166 | */ |
David Brownell | b29dbbd | 2007-05-25 20:40:31 -0700 | [diff] [blame] | 2167 | static unsigned int |
| 2168 | gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | { |
| 2170 | unsigned int len; |
| 2171 | |
| 2172 | if (gb == NULL) |
| 2173 | return 0; |
| 2174 | |
| 2175 | len = gs_buf_space_avail(gb); |
| 2176 | if (count > len) |
| 2177 | count = len; |
| 2178 | |
| 2179 | if (count == 0) |
| 2180 | return 0; |
| 2181 | |
| 2182 | len = gb->buf_buf + gb->buf_size - gb->buf_put; |
| 2183 | if (count > len) { |
| 2184 | memcpy(gb->buf_put, buf, len); |
| 2185 | memcpy(gb->buf_buf, buf+len, count - len); |
| 2186 | gb->buf_put = gb->buf_buf + count - len; |
| 2187 | } else { |
| 2188 | memcpy(gb->buf_put, buf, count); |
| 2189 | if (count < len) |
| 2190 | gb->buf_put += count; |
| 2191 | else /* count == len */ |
| 2192 | gb->buf_put = gb->buf_buf; |
| 2193 | } |
| 2194 | |
| 2195 | return count; |
| 2196 | } |
| 2197 | |
| 2198 | /* |
| 2199 | * gs_buf_get |
| 2200 | * |
| 2201 | * Get data from the circular buffer and copy to the given buffer. |
| 2202 | * Restrict to the amount of data available. |
| 2203 | * |
| 2204 | * Return the number of bytes copied. |
| 2205 | */ |
David Brownell | b29dbbd | 2007-05-25 20:40:31 -0700 | [diff] [blame] | 2206 | static unsigned int |
| 2207 | gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | { |
| 2209 | unsigned int len; |
| 2210 | |
| 2211 | if (gb == NULL) |
| 2212 | return 0; |
| 2213 | |
| 2214 | len = gs_buf_data_avail(gb); |
| 2215 | if (count > len) |
| 2216 | count = len; |
| 2217 | |
| 2218 | if (count == 0) |
| 2219 | return 0; |
| 2220 | |
| 2221 | len = gb->buf_buf + gb->buf_size - gb->buf_get; |
| 2222 | if (count > len) { |
| 2223 | memcpy(buf, gb->buf_get, len); |
| 2224 | memcpy(buf+len, gb->buf_buf, count - len); |
| 2225 | gb->buf_get = gb->buf_buf + count - len; |
| 2226 | } else { |
| 2227 | memcpy(buf, gb->buf_get, count); |
| 2228 | if (count < len) |
| 2229 | gb->buf_get += count; |
| 2230 | else /* count == len */ |
| 2231 | gb->buf_get = gb->buf_buf; |
| 2232 | } |
| 2233 | |
| 2234 | return count; |
| 2235 | } |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 2236 | |
| 2237 | /*-------------------------------------------------------------------------*/ |
| 2238 | |
| 2239 | static struct tty_driver *gs_tty_driver; |
| 2240 | |
| 2241 | /* |
| 2242 | * gs_module_init |
| 2243 | * |
| 2244 | * Register as a USB gadget driver and a tty driver. |
| 2245 | */ |
| 2246 | static int __init gs_module_init(void) |
| 2247 | { |
| 2248 | int i; |
| 2249 | int retval; |
| 2250 | |
| 2251 | retval = usb_gadget_register_driver(&gs_gadget_driver); |
| 2252 | if (retval) { |
| 2253 | pr_err("gs_module_init: cannot register gadget driver, " |
| 2254 | "ret=%d\n", retval); |
| 2255 | return retval; |
| 2256 | } |
| 2257 | |
| 2258 | gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS); |
| 2259 | if (!gs_tty_driver) |
| 2260 | return -ENOMEM; |
| 2261 | gs_tty_driver->owner = THIS_MODULE; |
| 2262 | gs_tty_driver->driver_name = GS_SHORT_NAME; |
| 2263 | gs_tty_driver->name = "ttygs"; |
| 2264 | gs_tty_driver->major = GS_MAJOR; |
| 2265 | gs_tty_driver->minor_start = GS_MINOR_START; |
| 2266 | gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 2267 | gs_tty_driver->subtype = SERIAL_TYPE_NORMAL; |
| 2268 | gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
| 2269 | gs_tty_driver->init_termios = tty_std_termios; |
| 2270 | /* must match GS_DEFAULT_DTE_RATE and friends */ |
| 2271 | gs_tty_driver->init_termios.c_cflag = |
| 2272 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 2273 | gs_tty_driver->init_termios.c_ispeed = GS_DEFAULT_DTE_RATE; |
| 2274 | gs_tty_driver->init_termios.c_ospeed = GS_DEFAULT_DTE_RATE; |
| 2275 | tty_set_operations(gs_tty_driver, &gs_tty_ops); |
| 2276 | |
| 2277 | for (i = 0; i < GS_NUM_PORTS; i++) |
| 2278 | mutex_init(&gs_open_close_lock[i]); |
| 2279 | |
| 2280 | retval = tty_register_driver(gs_tty_driver); |
| 2281 | if (retval) { |
| 2282 | usb_gadget_unregister_driver(&gs_gadget_driver); |
| 2283 | put_tty_driver(gs_tty_driver); |
| 2284 | pr_err("gs_module_init: cannot register tty driver, " |
| 2285 | "ret=%d\n", retval); |
| 2286 | return retval; |
| 2287 | } |
| 2288 | |
| 2289 | pr_info("gs_module_init: %s %s loaded\n", |
| 2290 | GS_LONG_NAME, GS_VERSION_STR); |
| 2291 | return 0; |
| 2292 | } |
| 2293 | module_init(gs_module_init); |
| 2294 | |
| 2295 | /* |
| 2296 | * gs_module_exit |
| 2297 | * |
| 2298 | * Unregister as a tty driver and a USB gadget driver. |
| 2299 | */ |
| 2300 | static void __exit gs_module_exit(void) |
| 2301 | { |
| 2302 | tty_unregister_driver(gs_tty_driver); |
| 2303 | put_tty_driver(gs_tty_driver); |
| 2304 | usb_gadget_unregister_driver(&gs_gadget_driver); |
| 2305 | |
| 2306 | pr_info("gs_module_exit: %s %s unloaded\n", |
| 2307 | GS_LONG_NAME, GS_VERSION_STR); |
| 2308 | } |
| 2309 | module_exit(gs_module_exit); |