| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * u_serial.c - utilities for USB gadget "serial port"/TTY support | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) | 
|  | 5 | * Copyright (C) 2008 David Brownell | 
|  | 6 | * Copyright (C) 2008 by Nokia Corporation | 
|  | 7 | * | 
|  | 8 | * This code also borrows from usbserial.c, which is | 
|  | 9 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) | 
|  | 10 | * Copyright (C) 2000 Peter Berger (pberger@brimson.com) | 
|  | 11 | * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com) | 
|  | 12 | * | 
|  | 13 | * This software is distributed under the terms of the GNU General | 
|  | 14 | * Public License ("GPL") as published by the Free Software Foundation, | 
|  | 15 | * either version 2 of that License or (at your option) any later version. | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 | /* #define VERBOSE_DEBUG */ | 
|  | 19 |  | 
|  | 20 | #include <linux/kernel.h> | 
| stephane duverger | 1e41394 | 2010-06-29 16:57:25 +0200 | [diff] [blame] | 21 | #include <linux/sched.h> | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 22 | #include <linux/interrupt.h> | 
|  | 23 | #include <linux/device.h> | 
|  | 24 | #include <linux/delay.h> | 
|  | 25 | #include <linux/tty.h> | 
|  | 26 | #include <linux/tty_flip.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> | 
| Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 28 | #include <linux/export.h> | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 29 | #include <linux/debugfs.h> | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | #include "u_serial.h" | 
|  | 32 |  | 
|  | 33 |  | 
|  | 34 | /* | 
|  | 35 | * This component encapsulates the TTY layer glue needed to provide basic | 
|  | 36 | * "serial port" functionality through the USB gadget stack.  Each such | 
|  | 37 | * port is exposed through a /dev/ttyGS* node. | 
|  | 38 | * | 
|  | 39 | * After initialization (gserial_setup), these TTY port devices stay | 
|  | 40 | * available until they are removed (gserial_cleanup).  Each one may be | 
|  | 41 | * connected to a USB function (gserial_connect), or disconnected (with | 
|  | 42 | * gserial_disconnect) when the USB host issues a config change event. | 
|  | 43 | * Data can only flow when the port is connected to the host. | 
|  | 44 | * | 
|  | 45 | * A given TTY port can be made available in multiple configurations. | 
|  | 46 | * For example, each one might expose a ttyGS0 node which provides a | 
|  | 47 | * login application.  In one case that might use CDC ACM interface 0, | 
|  | 48 | * while another configuration might use interface 3 for that.  The | 
|  | 49 | * work to handle that (including descriptor management) is not part | 
|  | 50 | * of this component. | 
|  | 51 | * | 
|  | 52 | * Configurations may expose more than one TTY port.  For example, if | 
|  | 53 | * ttyGS0 provides login service, then ttyGS1 might provide dialer access | 
|  | 54 | * for a telephone or fax link.  And ttyGS2 might be something that just | 
|  | 55 | * needs a simple byte stream interface for some messaging protocol that | 
|  | 56 | * is managed in userspace ... OBEX, PTP, and MTP have been mentioned. | 
|  | 57 | */ | 
|  | 58 |  | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 59 | #define PREFIX	"ttyGS" | 
|  | 60 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 61 | /* | 
|  | 62 | * gserial is the lifecycle interface, used by USB functions | 
|  | 63 | * gs_port is the I/O nexus, used by the tty driver | 
|  | 64 | * tty_struct links to the tty/filesystem framework | 
|  | 65 | * | 
|  | 66 | * gserial <---> gs_port ... links will be null when the USB link is | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 67 | * inactive; managed by gserial_{connect,disconnect}().  each gserial | 
|  | 68 | * instance can wrap its own USB control protocol. | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 69 | *	gserial->ioport == usb_ep->driver_data ... gs_port | 
|  | 70 | *	gs_port->port_usb ... gserial | 
|  | 71 | * | 
|  | 72 | * gs_port <---> tty_struct ... links will be null when the TTY file | 
|  | 73 | * isn't opened; managed by gs_open()/gs_close() | 
|  | 74 | *	gserial->port_tty ... tty_struct | 
|  | 75 | *	tty_struct->driver_data ... gserial | 
|  | 76 | */ | 
|  | 77 |  | 
|  | 78 | /* RX and TX queues can buffer QUEUE_SIZE packets before they hit the | 
|  | 79 | * next layer of buffering.  For TX that's a circular buffer; for RX | 
|  | 80 | * consider it a NOP.  A third layer is provided by the TTY code. | 
|  | 81 | */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 82 | #define TX_QUEUE_SIZE		8 | 
|  | 83 | #define TX_BUF_SIZE		4096 | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 84 | #define WRITE_BUF_SIZE		8192		/* TX only */ | 
|  | 85 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 86 | #define RX_QUEUE_SIZE		8 | 
|  | 87 | #define RX_BUF_SIZE		4096 | 
|  | 88 |  | 
|  | 89 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 90 | /* circular buffer */ | 
|  | 91 | struct gs_buf { | 
|  | 92 | unsigned		buf_size; | 
|  | 93 | char			*buf_buf; | 
|  | 94 | char			*buf_get; | 
|  | 95 | char			*buf_put; | 
|  | 96 | }; | 
|  | 97 |  | 
|  | 98 | /* | 
|  | 99 | * The port structure holds info for each port, one for each minor number | 
|  | 100 | * (and thus for each /dev/ node). | 
|  | 101 | */ | 
|  | 102 | struct gs_port { | 
|  | 103 | spinlock_t		port_lock;	/* guard port_* access */ | 
|  | 104 |  | 
|  | 105 | struct gserial		*port_usb; | 
|  | 106 | struct tty_struct	*port_tty; | 
|  | 107 |  | 
|  | 108 | unsigned		open_count; | 
|  | 109 | bool			openclose;	/* open/close in progress */ | 
|  | 110 | u8			port_num; | 
|  | 111 |  | 
|  | 112 | wait_queue_head_t	close_wait;	/* wait for last close */ | 
|  | 113 |  | 
|  | 114 | struct list_head	read_pool; | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 115 | int read_started; | 
|  | 116 | int read_allocated; | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 117 | struct list_head	read_queue; | 
|  | 118 | unsigned		n_read; | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 119 | struct work_struct	push; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | struct list_head	write_pool; | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 122 | int write_started; | 
|  | 123 | int write_allocated; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 124 | struct gs_buf		port_write_buf; | 
|  | 125 | wait_queue_head_t	drain_wait;	/* wait while writes drain */ | 
|  | 126 |  | 
|  | 127 | /* REVISIT this state ... */ | 
|  | 128 | struct usb_cdc_line_coding port_line_coding;	/* 8-N-1 etc */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | unsigned long           nbytes_from_host; | 
|  | 130 | unsigned long           nbytes_to_tty; | 
|  | 131 | unsigned long           nbytes_from_tty; | 
|  | 132 | unsigned long           nbytes_to_host; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
|  | 135 | /* increase N_PORTS if you need more */ | 
| John Michelau | 677ba87 | 2010-11-08 18:05:37 -0600 | [diff] [blame] | 136 | #define N_PORTS		8 | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 137 | static struct portmaster { | 
|  | 138 | struct mutex	lock;			/* protect open/close */ | 
|  | 139 | struct gs_port	*port; | 
|  | 140 | } ports[N_PORTS]; | 
|  | 141 | static unsigned	n_ports; | 
|  | 142 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 143 | static struct workqueue_struct *gserial_wq; | 
|  | 144 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 145 | #define GS_CLOSE_TIMEOUT		15		/* seconds */ | 
|  | 146 |  | 
|  | 147 |  | 
|  | 148 |  | 
|  | 149 | #ifdef VERBOSE_DEBUG | 
|  | 150 | #define pr_vdebug(fmt, arg...) \ | 
|  | 151 | pr_debug(fmt, ##arg) | 
|  | 152 | #else | 
|  | 153 | #define pr_vdebug(fmt, arg...) \ | 
|  | 154 | ({ if (0) pr_debug(fmt, ##arg); }) | 
|  | 155 | #endif | 
|  | 156 |  | 
|  | 157 | /*-------------------------------------------------------------------------*/ | 
|  | 158 |  | 
|  | 159 | /* Circular Buffer */ | 
|  | 160 |  | 
|  | 161 | /* | 
|  | 162 | * gs_buf_alloc | 
|  | 163 | * | 
|  | 164 | * Allocate a circular buffer and all associated memory. | 
|  | 165 | */ | 
|  | 166 | static int gs_buf_alloc(struct gs_buf *gb, unsigned size) | 
|  | 167 | { | 
|  | 168 | gb->buf_buf = kmalloc(size, GFP_KERNEL); | 
|  | 169 | if (gb->buf_buf == NULL) | 
|  | 170 | return -ENOMEM; | 
|  | 171 |  | 
|  | 172 | gb->buf_size = size; | 
|  | 173 | gb->buf_put = gb->buf_buf; | 
|  | 174 | gb->buf_get = gb->buf_buf; | 
|  | 175 |  | 
|  | 176 | return 0; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | /* | 
|  | 180 | * gs_buf_free | 
|  | 181 | * | 
|  | 182 | * Free the buffer and all associated memory. | 
|  | 183 | */ | 
|  | 184 | static void gs_buf_free(struct gs_buf *gb) | 
|  | 185 | { | 
|  | 186 | kfree(gb->buf_buf); | 
|  | 187 | gb->buf_buf = NULL; | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | /* | 
|  | 191 | * gs_buf_clear | 
|  | 192 | * | 
|  | 193 | * Clear out all data in the circular buffer. | 
|  | 194 | */ | 
|  | 195 | static void gs_buf_clear(struct gs_buf *gb) | 
|  | 196 | { | 
|  | 197 | gb->buf_get = gb->buf_put; | 
|  | 198 | /* equivalent to a get of all data available */ | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | /* | 
|  | 202 | * gs_buf_data_avail | 
|  | 203 | * | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 204 | * Return the number of bytes of data written into the circular | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 205 | * buffer. | 
|  | 206 | */ | 
|  | 207 | static unsigned gs_buf_data_avail(struct gs_buf *gb) | 
|  | 208 | { | 
|  | 209 | return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | /* | 
|  | 213 | * gs_buf_space_avail | 
|  | 214 | * | 
|  | 215 | * Return the number of bytes of space available in the circular | 
|  | 216 | * buffer. | 
|  | 217 | */ | 
|  | 218 | static unsigned gs_buf_space_avail(struct gs_buf *gb) | 
|  | 219 | { | 
|  | 220 | return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | /* | 
|  | 224 | * gs_buf_put | 
|  | 225 | * | 
|  | 226 | * Copy data data from a user buffer and put it into the circular buffer. | 
|  | 227 | * Restrict to the amount of space available. | 
|  | 228 | * | 
|  | 229 | * Return the number of bytes copied. | 
|  | 230 | */ | 
|  | 231 | static unsigned | 
|  | 232 | gs_buf_put(struct gs_buf *gb, const char *buf, unsigned count) | 
|  | 233 | { | 
|  | 234 | unsigned len; | 
|  | 235 |  | 
|  | 236 | len  = gs_buf_space_avail(gb); | 
|  | 237 | if (count > len) | 
|  | 238 | count = len; | 
|  | 239 |  | 
|  | 240 | if (count == 0) | 
|  | 241 | return 0; | 
|  | 242 |  | 
|  | 243 | len = gb->buf_buf + gb->buf_size - gb->buf_put; | 
|  | 244 | if (count > len) { | 
|  | 245 | memcpy(gb->buf_put, buf, len); | 
|  | 246 | memcpy(gb->buf_buf, buf+len, count - len); | 
|  | 247 | gb->buf_put = gb->buf_buf + count - len; | 
|  | 248 | } else { | 
|  | 249 | memcpy(gb->buf_put, buf, count); | 
|  | 250 | if (count < len) | 
|  | 251 | gb->buf_put += count; | 
|  | 252 | else /* count == len */ | 
|  | 253 | gb->buf_put = gb->buf_buf; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | return count; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | /* | 
|  | 260 | * gs_buf_get | 
|  | 261 | * | 
|  | 262 | * Get data from the circular buffer and copy to the given buffer. | 
|  | 263 | * Restrict to the amount of data available. | 
|  | 264 | * | 
|  | 265 | * Return the number of bytes copied. | 
|  | 266 | */ | 
|  | 267 | static unsigned | 
|  | 268 | gs_buf_get(struct gs_buf *gb, char *buf, unsigned count) | 
|  | 269 | { | 
|  | 270 | unsigned len; | 
|  | 271 |  | 
|  | 272 | len = gs_buf_data_avail(gb); | 
|  | 273 | if (count > len) | 
|  | 274 | count = len; | 
|  | 275 |  | 
|  | 276 | if (count == 0) | 
|  | 277 | return 0; | 
|  | 278 |  | 
|  | 279 | len = gb->buf_buf + gb->buf_size - gb->buf_get; | 
|  | 280 | if (count > len) { | 
|  | 281 | memcpy(buf, gb->buf_get, len); | 
|  | 282 | memcpy(buf+len, gb->buf_buf, count - len); | 
|  | 283 | gb->buf_get = gb->buf_buf + count - len; | 
|  | 284 | } else { | 
|  | 285 | memcpy(buf, gb->buf_get, count); | 
|  | 286 | if (count < len) | 
|  | 287 | gb->buf_get += count; | 
|  | 288 | else /* count == len */ | 
|  | 289 | gb->buf_get = gb->buf_buf; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | return count; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | /*-------------------------------------------------------------------------*/ | 
|  | 296 |  | 
|  | 297 | /* I/O glue between TTY (upper) and USB function (lower) driver layers */ | 
|  | 298 |  | 
|  | 299 | /* | 
|  | 300 | * gs_alloc_req | 
|  | 301 | * | 
|  | 302 | * Allocate a usb_request and its buffer.  Returns a pointer to the | 
|  | 303 | * usb_request or NULL if there is an error. | 
|  | 304 | */ | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 305 | struct usb_request * | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 306 | gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags) | 
|  | 307 | { | 
|  | 308 | struct usb_request *req; | 
|  | 309 |  | 
|  | 310 | req = usb_ep_alloc_request(ep, kmalloc_flags); | 
|  | 311 |  | 
|  | 312 | if (req != NULL) { | 
|  | 313 | req->length = len; | 
|  | 314 | req->buf = kmalloc(len, kmalloc_flags); | 
|  | 315 | if (req->buf == NULL) { | 
|  | 316 | usb_ep_free_request(ep, req); | 
|  | 317 | return NULL; | 
|  | 318 | } | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | return req; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | /* | 
|  | 325 | * gs_free_req | 
|  | 326 | * | 
|  | 327 | * Free a usb_request and its buffer. | 
|  | 328 | */ | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 329 | void gs_free_req(struct usb_ep *ep, struct usb_request *req) | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 330 | { | 
|  | 331 | kfree(req->buf); | 
|  | 332 | usb_ep_free_request(ep, req); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | /* | 
|  | 336 | * gs_send_packet | 
|  | 337 | * | 
|  | 338 | * If there is data to send, a packet is built in the given | 
|  | 339 | * buffer and the size is returned.  If there is no data to | 
|  | 340 | * send, 0 is returned. | 
|  | 341 | * | 
|  | 342 | * Called with port_lock held. | 
|  | 343 | */ | 
|  | 344 | static unsigned | 
|  | 345 | gs_send_packet(struct gs_port *port, char *packet, unsigned size) | 
|  | 346 | { | 
|  | 347 | unsigned len; | 
|  | 348 |  | 
|  | 349 | len = gs_buf_data_avail(&port->port_write_buf); | 
|  | 350 | if (len < size) | 
|  | 351 | size = len; | 
|  | 352 | if (size != 0) | 
|  | 353 | size = gs_buf_get(&port->port_write_buf, packet, size); | 
|  | 354 | return size; | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | /* | 
|  | 358 | * gs_start_tx | 
|  | 359 | * | 
|  | 360 | * This function finds available write requests, calls | 
|  | 361 | * gs_send_packet to fill these packets with data, and | 
|  | 362 | * continues until either there are no more write requests | 
|  | 363 | * available or no more data to send.  This function is | 
|  | 364 | * run whenever data arrives or write requests are available. | 
|  | 365 | * | 
|  | 366 | * Context: caller owns port_lock; port_usb is non-null. | 
|  | 367 | */ | 
|  | 368 | static int gs_start_tx(struct gs_port *port) | 
|  | 369 | /* | 
|  | 370 | __releases(&port->port_lock) | 
|  | 371 | __acquires(&port->port_lock) | 
|  | 372 | */ | 
|  | 373 | { | 
|  | 374 | struct list_head	*pool = &port->write_pool; | 
|  | 375 | struct usb_ep		*in = port->port_usb->in; | 
|  | 376 | int			status = 0; | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 377 | static long 		prev_len; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 378 | bool			do_tty_wake = false; | 
|  | 379 |  | 
|  | 380 | while (!list_empty(pool)) { | 
|  | 381 | struct usb_request	*req; | 
|  | 382 | int			len; | 
|  | 383 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 384 | if (port->write_started >= TX_QUEUE_SIZE) | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 385 | break; | 
|  | 386 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 387 | req = list_entry(pool->next, struct usb_request, list); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 388 | len = gs_send_packet(port, req->buf, TX_BUF_SIZE); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 389 | if (len == 0) { | 
| Rajkumar Raghupathy | 4098529 | 2012-04-12 15:19:53 +0530 | [diff] [blame] | 390 | /* Queue zero length packet explicitly to make it | 
|  | 391 | * work with UDCs which don't support req->zero flag | 
|  | 392 | */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 393 | if (prev_len && (prev_len % in->maxpacket == 0)) { | 
|  | 394 | req->length = 0; | 
|  | 395 | list_del(&req->list); | 
|  | 396 | spin_unlock(&port->port_lock); | 
|  | 397 | status = usb_ep_queue(in, req, GFP_ATOMIC); | 
|  | 398 | spin_lock(&port->port_lock); | 
|  | 399 | if (!port->port_usb) { | 
|  | 400 | gs_free_req(in, req); | 
|  | 401 | break; | 
|  | 402 | } | 
|  | 403 | if (status) { | 
|  | 404 | printk(KERN_ERR "%s: %s err %d\n", | 
|  | 405 | __func__, "queue", status); | 
|  | 406 | list_add(&req->list, pool); | 
|  | 407 | } | 
|  | 408 | prev_len = 0; | 
|  | 409 | } | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 410 | wake_up_interruptible(&port->drain_wait); | 
|  | 411 | break; | 
|  | 412 | } | 
|  | 413 | do_tty_wake = true; | 
|  | 414 |  | 
|  | 415 | req->length = len; | 
|  | 416 | list_del(&req->list); | 
|  | 417 |  | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 418 | pr_vdebug(PREFIX "%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n", | 
|  | 419 | port->port_num, len, *((u8 *)req->buf), | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 420 | *((u8 *)req->buf+1), *((u8 *)req->buf+2)); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 421 |  | 
|  | 422 | /* Drop lock while we call out of driver; completions | 
|  | 423 | * could be issued while we do so.  Disconnection may | 
|  | 424 | * happen too; maybe immediately before we queue this! | 
|  | 425 | * | 
|  | 426 | * NOTE that we may keep sending data for a while after | 
|  | 427 | * the TTY closed (dev->ioport->port_tty is NULL). | 
|  | 428 | */ | 
|  | 429 | spin_unlock(&port->port_lock); | 
|  | 430 | status = usb_ep_queue(in, req, GFP_ATOMIC); | 
|  | 431 | spin_lock(&port->port_lock); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 432 | /* | 
|  | 433 | * If port_usb is NULL, gserial disconnect is called | 
|  | 434 | * while the spinlock is dropped and all requests are | 
|  | 435 | * freed. Free the current request here. | 
|  | 436 | */ | 
|  | 437 | if (!port->port_usb) { | 
|  | 438 | do_tty_wake = false; | 
|  | 439 | gs_free_req(in, req); | 
|  | 440 | break; | 
|  | 441 | } | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 442 | if (status) { | 
|  | 443 | pr_debug("%s: %s %s err %d\n", | 
|  | 444 | __func__, "queue", in->name, status); | 
|  | 445 | list_add(&req->list, pool); | 
|  | 446 | break; | 
|  | 447 | } | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 448 | prev_len = req->length; | 
|  | 449 | port->nbytes_from_tty += req->length; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 450 |  | 
| Stephen Boyd | 442fa65 | 2013-01-24 14:47:23 -0800 | [diff] [blame] | 451 | port->write_started++; | 
|  | 452 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 453 | } | 
|  | 454 |  | 
|  | 455 | if (do_tty_wake && port->port_tty) | 
|  | 456 | tty_wakeup(port->port_tty); | 
|  | 457 | return status; | 
|  | 458 | } | 
|  | 459 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 460 | /* | 
|  | 461 | * Context: caller owns port_lock, and port_usb is set | 
|  | 462 | */ | 
|  | 463 | static unsigned gs_start_rx(struct gs_port *port) | 
|  | 464 | /* | 
|  | 465 | __releases(&port->port_lock) | 
|  | 466 | __acquires(&port->port_lock) | 
|  | 467 | */ | 
|  | 468 | { | 
|  | 469 | struct list_head	*pool = &port->read_pool; | 
|  | 470 | struct usb_ep		*out = port->port_usb->out; | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 471 | unsigned		started = 0; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 472 |  | 
|  | 473 | while (!list_empty(pool)) { | 
|  | 474 | struct usb_request	*req; | 
|  | 475 | int			status; | 
|  | 476 | struct tty_struct	*tty; | 
|  | 477 |  | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 478 | /* no more rx if closed */ | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 479 | tty = port->port_tty; | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 480 | if (!tty) | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 481 | break; | 
|  | 482 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 483 | if (port->read_started >= RX_QUEUE_SIZE) | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 484 | break; | 
|  | 485 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 486 | req = list_entry(pool->next, struct usb_request, list); | 
|  | 487 | list_del(&req->list); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 488 | req->length = RX_BUF_SIZE; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 489 |  | 
|  | 490 | /* drop lock while we call out; the controller driver | 
|  | 491 | * may need to call us back (e.g. for disconnect) | 
|  | 492 | */ | 
|  | 493 | spin_unlock(&port->port_lock); | 
|  | 494 | status = usb_ep_queue(out, req, GFP_ATOMIC); | 
|  | 495 | spin_lock(&port->port_lock); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 496 | /* | 
|  | 497 | * If port_usb is NULL, gserial disconnect is called | 
|  | 498 | * while the spinlock is dropped and all requests are | 
|  | 499 | * freed. Free the current request here. | 
|  | 500 | */ | 
|  | 501 | if (!port->port_usb) { | 
|  | 502 | started = 0; | 
|  | 503 | gs_free_req(out, req); | 
|  | 504 | break; | 
|  | 505 | } | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 506 | if (status) { | 
|  | 507 | pr_debug("%s: %s %s err %d\n", | 
|  | 508 | __func__, "queue", out->name, status); | 
|  | 509 | list_add(&req->list, pool); | 
|  | 510 | break; | 
|  | 511 | } | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 512 | port->read_started++; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 513 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 514 | } | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 515 | return port->read_started; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 516 | } | 
|  | 517 |  | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 518 | /* | 
|  | 519 | * RX tasklet takes data out of the RX queue and hands it up to the TTY | 
|  | 520 | * layer until it refuses to take any more data (or is throttled back). | 
|  | 521 | * Then it issues reads for any further data. | 
|  | 522 | * | 
|  | 523 | * If the RX queue becomes full enough that no usb_request is queued, | 
|  | 524 | * the OUT endpoint may begin NAKing as soon as its FIFO fills up. | 
|  | 525 | * So QUEUE_SIZE packets plus however many the FIFO holds (usually two) | 
|  | 526 | * can be buffered before the TTY layer's buffers (currently 64 KB). | 
|  | 527 | */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 528 | static void gs_rx_push(struct work_struct *w) | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 529 | { | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 530 | struct gs_port		*port = container_of(w, struct gs_port, push); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 531 | struct tty_struct	*tty; | 
|  | 532 | struct list_head	*queue = &port->read_queue; | 
|  | 533 | bool			disconnect = false; | 
|  | 534 | bool			do_push = false; | 
|  | 535 |  | 
|  | 536 | /* hand any queued data to the tty */ | 
|  | 537 | spin_lock_irq(&port->port_lock); | 
|  | 538 | tty = port->port_tty; | 
|  | 539 | while (!list_empty(queue)) { | 
|  | 540 | struct usb_request	*req; | 
|  | 541 |  | 
|  | 542 | req = list_first_entry(queue, struct usb_request, list); | 
|  | 543 |  | 
|  | 544 | /* discard data if tty was closed */ | 
|  | 545 | if (!tty) | 
|  | 546 | goto recycle; | 
|  | 547 |  | 
|  | 548 | /* leave data queued if tty was rx throttled */ | 
|  | 549 | if (test_bit(TTY_THROTTLED, &tty->flags)) | 
|  | 550 | break; | 
|  | 551 |  | 
|  | 552 | switch (req->status) { | 
|  | 553 | case -ESHUTDOWN: | 
|  | 554 | disconnect = true; | 
|  | 555 | pr_vdebug(PREFIX "%d: shutdown\n", port->port_num); | 
|  | 556 | break; | 
|  | 557 |  | 
|  | 558 | default: | 
|  | 559 | /* presumably a transient fault */ | 
|  | 560 | pr_warning(PREFIX "%d: unexpected RX status %d\n", | 
|  | 561 | port->port_num, req->status); | 
|  | 562 | /* FALLTHROUGH */ | 
|  | 563 | case 0: | 
|  | 564 | /* normal completion */ | 
|  | 565 | break; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | /* push data to (open) tty */ | 
|  | 569 | if (req->actual) { | 
|  | 570 | char		*packet = req->buf; | 
|  | 571 | unsigned	size = req->actual; | 
|  | 572 | unsigned	n; | 
|  | 573 | int		count; | 
|  | 574 |  | 
|  | 575 | /* we may have pushed part of this packet already... */ | 
|  | 576 | n = port->n_read; | 
|  | 577 | if (n) { | 
|  | 578 | packet += n; | 
|  | 579 | size -= n; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | count = tty_insert_flip_string(tty, packet, size); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 583 | port->nbytes_to_tty += count; | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 584 | if (count) | 
|  | 585 | do_push = true; | 
|  | 586 | if (count != size) { | 
|  | 587 | /* stop pushing; TTY layer can't handle more */ | 
|  | 588 | port->n_read += count; | 
|  | 589 | pr_vdebug(PREFIX "%d: rx block %d/%d\n", | 
|  | 590 | port->port_num, | 
|  | 591 | count, req->actual); | 
|  | 592 | break; | 
|  | 593 | } | 
|  | 594 | port->n_read = 0; | 
|  | 595 | } | 
|  | 596 | recycle: | 
|  | 597 | list_move(&req->list, &port->read_pool); | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 598 | port->read_started--; | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 599 | } | 
|  | 600 |  | 
| Jon Povey | 44a0c01 | 2010-06-14 19:41:04 +0900 | [diff] [blame] | 601 | /* Push from tty to ldisc; without low_latency set this is handled by | 
|  | 602 | * a workqueue, so we won't get callbacks and can hold port_lock | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 603 | */ | 
| Shaun Silk | 5023829 | 2011-09-26 11:26:43 +1000 | [diff] [blame] | 604 | if (tty && do_push) | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 605 | tty_flip_buffer_push(tty); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 606 |  | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 607 | /* We want our data queue to become empty ASAP, keeping data | 
|  | 608 | * in the tty and ldisc (not here).  If we couldn't push any | 
|  | 609 | * this time around, there may be trouble unless there's an | 
|  | 610 | * implicit tty_unthrottle() call on its way... | 
|  | 611 | * | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 612 | * REVISIT we should probably add a timer to keep the work queue | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 613 | * from starving ... but it's not clear that case ever happens. | 
|  | 614 | */ | 
|  | 615 | if (!list_empty(queue) && tty) { | 
|  | 616 | if (!test_bit(TTY_THROTTLED, &tty->flags)) { | 
|  | 617 | if (do_push) | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 618 | queue_work(gserial_wq, &port->push); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 619 | else | 
|  | 620 | pr_warning(PREFIX "%d: RX not scheduled?\n", | 
|  | 621 | port->port_num); | 
|  | 622 | } | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | /* If we're still connected, refill the USB RX queue. */ | 
|  | 626 | if (!disconnect && port->port_usb) | 
|  | 627 | gs_start_rx(port); | 
|  | 628 |  | 
|  | 629 | spin_unlock_irq(&port->port_lock); | 
|  | 630 | } | 
|  | 631 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 632 | static void gs_read_complete(struct usb_ep *ep, struct usb_request *req) | 
|  | 633 | { | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 634 | struct gs_port	*port = ep->driver_data; | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 635 | unsigned long flags; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 636 |  | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 637 | /* Queue all received data until the tty layer is ready for it. */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 638 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 639 | port->nbytes_from_host += req->actual; | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 640 | list_add_tail(&req->list, &port->read_queue); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 641 | queue_work(gserial_wq, &port->push); | 
|  | 642 | spin_unlock_irqrestore(&port->port_lock, flags); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 643 | } | 
|  | 644 |  | 
|  | 645 | static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) | 
|  | 646 | { | 
|  | 647 | struct gs_port	*port = ep->driver_data; | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 648 | unsigned long flags; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 649 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 650 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 651 | port->nbytes_to_host += req->actual; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 652 | list_add(&req->list, &port->write_pool); | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 653 | port->write_started--; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 654 |  | 
|  | 655 | switch (req->status) { | 
|  | 656 | default: | 
|  | 657 | /* presumably a transient fault */ | 
|  | 658 | pr_warning("%s: unexpected %s status %d\n", | 
|  | 659 | __func__, ep->name, req->status); | 
|  | 660 | /* FALL THROUGH */ | 
|  | 661 | case 0: | 
|  | 662 | /* normal completion */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 663 | if (port->port_usb) | 
|  | 664 | gs_start_tx(port); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 665 | break; | 
|  | 666 |  | 
|  | 667 | case -ESHUTDOWN: | 
|  | 668 | /* disconnect */ | 
|  | 669 | pr_vdebug("%s: %s shutdown\n", __func__, ep->name); | 
|  | 670 | break; | 
|  | 671 | } | 
|  | 672 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 673 | spin_unlock_irqrestore(&port->port_lock, flags); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 674 | } | 
|  | 675 |  | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 676 | static void gs_free_requests(struct usb_ep *ep, struct list_head *head, | 
|  | 677 | int *allocated) | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 678 | { | 
|  | 679 | struct usb_request	*req; | 
|  | 680 |  | 
|  | 681 | while (!list_empty(head)) { | 
|  | 682 | req = list_entry(head->next, struct usb_request, list); | 
|  | 683 | list_del(&req->list); | 
|  | 684 | gs_free_req(ep, req); | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 685 | if (allocated) | 
|  | 686 | (*allocated)--; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 687 | } | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head, | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 691 | int num, int size, void (*fn)(struct usb_ep *, struct usb_request *), | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 692 | int *allocated) | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 693 | { | 
|  | 694 | int			i; | 
|  | 695 | struct usb_request	*req; | 
|  | 696 |  | 
|  | 697 | /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't | 
|  | 698 | * do quite that many this time, don't fail ... we just won't | 
|  | 699 | * be as speedy as we might otherwise be. | 
|  | 700 | */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 701 | for (i = 0; i < num; i++) { | 
|  | 702 | req = gs_alloc_req(ep, size, GFP_ATOMIC); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 703 | if (!req) | 
|  | 704 | return list_empty(head) ? -ENOMEM : 0; | 
|  | 705 | req->complete = fn; | 
|  | 706 | list_add_tail(&req->list, head); | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 707 | if (allocated) | 
|  | 708 | (*allocated)++; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 709 | } | 
|  | 710 | return 0; | 
|  | 711 | } | 
|  | 712 |  | 
|  | 713 | /** | 
|  | 714 | * gs_start_io - start USB I/O streams | 
|  | 715 | * @dev: encapsulates endpoints to use | 
|  | 716 | * Context: holding port_lock; port_tty and port_usb are non-null | 
|  | 717 | * | 
|  | 718 | * We only start I/O when something is connected to both sides of | 
|  | 719 | * this port.  If nothing is listening on the host side, we may | 
|  | 720 | * be pointlessly filling up our TX buffers and FIFO. | 
|  | 721 | */ | 
|  | 722 | static int gs_start_io(struct gs_port *port) | 
|  | 723 | { | 
|  | 724 | struct list_head	*head = &port->read_pool; | 
|  | 725 | struct usb_ep		*ep = port->port_usb->out; | 
|  | 726 | int			status; | 
|  | 727 | unsigned		started; | 
|  | 728 |  | 
|  | 729 | /* Allocate RX and TX I/O buffers.  We can't easily do this much | 
|  | 730 | * earlier (with GFP_KERNEL) because the requests are coupled to | 
|  | 731 | * endpoints, as are the packet sizes we'll be using.  Different | 
|  | 732 | * configurations may use different endpoints with a given port; | 
|  | 733 | * and high speed vs full speed changes packet sizes too. | 
|  | 734 | */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 735 | status = gs_alloc_requests(ep, head, RX_QUEUE_SIZE, RX_BUF_SIZE, | 
|  | 736 | gs_read_complete, &port->read_allocated); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 737 | if (status) | 
|  | 738 | return status; | 
|  | 739 |  | 
|  | 740 | status = gs_alloc_requests(port->port_usb->in, &port->write_pool, | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 741 | TX_QUEUE_SIZE, TX_BUF_SIZE, gs_write_complete, &port->write_allocated); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 742 | if (status) { | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 743 | gs_free_requests(ep, head, &port->read_allocated); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 744 | return status; | 
|  | 745 | } | 
|  | 746 |  | 
|  | 747 | /* queue read requests */ | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 748 | port->n_read = 0; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 749 | started = gs_start_rx(port); | 
|  | 750 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 751 | if (!port->port_usb) | 
|  | 752 | return -EIO; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 753 | /* unblock any pending writes into our circular buffer */ | 
|  | 754 | if (started) { | 
|  | 755 | tty_wakeup(port->port_tty); | 
|  | 756 | } else { | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 757 | gs_free_requests(ep, head, &port->read_allocated); | 
|  | 758 | gs_free_requests(port->port_usb->in, &port->write_pool, | 
|  | 759 | &port->write_allocated); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 760 | status = -EIO; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 761 | } | 
|  | 762 |  | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 763 | return status; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 764 | } | 
|  | 765 |  | 
|  | 766 | /*-------------------------------------------------------------------------*/ | 
|  | 767 |  | 
|  | 768 | /* TTY Driver */ | 
|  | 769 |  | 
|  | 770 | /* | 
|  | 771 | * gs_open sets up the link between a gs_port and its associated TTY. | 
|  | 772 | * That link is broken *only* by TTY close(), and all driver methods | 
|  | 773 | * know that. | 
|  | 774 | */ | 
|  | 775 | static int gs_open(struct tty_struct *tty, struct file *file) | 
|  | 776 | { | 
|  | 777 | int		port_num = tty->index; | 
|  | 778 | struct gs_port	*port; | 
|  | 779 | int		status; | 
|  | 780 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 781 | do { | 
|  | 782 | mutex_lock(&ports[port_num].lock); | 
|  | 783 | port = ports[port_num].port; | 
|  | 784 | if (!port) | 
|  | 785 | status = -ENODEV; | 
|  | 786 | else { | 
|  | 787 | spin_lock_irq(&port->port_lock); | 
|  | 788 |  | 
|  | 789 | /* already open?  Great. */ | 
|  | 790 | if (port->open_count) { | 
|  | 791 | status = 0; | 
|  | 792 | port->open_count++; | 
|  | 793 |  | 
|  | 794 | /* currently opening/closing? wait ... */ | 
|  | 795 | } else if (port->openclose) { | 
|  | 796 | status = -EBUSY; | 
|  | 797 |  | 
|  | 798 | /* ... else we do the work */ | 
|  | 799 | } else { | 
|  | 800 | status = -EAGAIN; | 
|  | 801 | port->openclose = true; | 
|  | 802 | } | 
|  | 803 | spin_unlock_irq(&port->port_lock); | 
|  | 804 | } | 
|  | 805 | mutex_unlock(&ports[port_num].lock); | 
|  | 806 |  | 
|  | 807 | switch (status) { | 
|  | 808 | default: | 
|  | 809 | /* fully handled */ | 
|  | 810 | return status; | 
|  | 811 | case -EAGAIN: | 
|  | 812 | /* must do the work */ | 
|  | 813 | break; | 
|  | 814 | case -EBUSY: | 
|  | 815 | /* wait for EAGAIN task to finish */ | 
|  | 816 | msleep(1); | 
|  | 817 | /* REVISIT could have a waitchannel here, if | 
|  | 818 | * concurrent open performance is important | 
|  | 819 | */ | 
|  | 820 | break; | 
|  | 821 | } | 
|  | 822 | } while (status != -EAGAIN); | 
|  | 823 |  | 
|  | 824 | /* Do the "real open" */ | 
|  | 825 | spin_lock_irq(&port->port_lock); | 
|  | 826 |  | 
|  | 827 | /* allocate circular buffer on first open */ | 
|  | 828 | if (port->port_write_buf.buf_buf == NULL) { | 
|  | 829 |  | 
|  | 830 | spin_unlock_irq(&port->port_lock); | 
|  | 831 | status = gs_buf_alloc(&port->port_write_buf, WRITE_BUF_SIZE); | 
|  | 832 | spin_lock_irq(&port->port_lock); | 
|  | 833 |  | 
|  | 834 | if (status) { | 
|  | 835 | pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n", | 
|  | 836 | port->port_num, tty, file); | 
|  | 837 | port->openclose = false; | 
|  | 838 | goto exit_unlock_port; | 
|  | 839 | } | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | /* REVISIT if REMOVED (ports[].port NULL), abort the open | 
|  | 843 | * to let rmmod work faster (but this way isn't wrong). | 
|  | 844 | */ | 
|  | 845 |  | 
|  | 846 | /* REVISIT maybe wait for "carrier detect" */ | 
|  | 847 |  | 
|  | 848 | tty->driver_data = port; | 
|  | 849 | port->port_tty = tty; | 
|  | 850 |  | 
|  | 851 | port->open_count = 1; | 
|  | 852 | port->openclose = false; | 
|  | 853 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 854 | /* if connected, start the I/O stream */ | 
|  | 855 | if (port->port_usb) { | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 856 | struct gserial	*gser = port->port_usb; | 
|  | 857 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 858 | pr_debug("gs_open: start ttyGS%d\n", port->port_num); | 
|  | 859 | gs_start_io(port); | 
|  | 860 |  | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 861 | if (gser->connect) | 
|  | 862 | gser->connect(gser); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 863 | } | 
|  | 864 |  | 
|  | 865 | pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); | 
|  | 866 |  | 
|  | 867 | status = 0; | 
|  | 868 |  | 
|  | 869 | exit_unlock_port: | 
|  | 870 | spin_unlock_irq(&port->port_lock); | 
|  | 871 | return status; | 
|  | 872 | } | 
|  | 873 |  | 
|  | 874 | static int gs_writes_finished(struct gs_port *p) | 
|  | 875 | { | 
|  | 876 | int cond; | 
|  | 877 |  | 
|  | 878 | /* return true on disconnect or empty buffer */ | 
|  | 879 | spin_lock_irq(&p->port_lock); | 
|  | 880 | cond = (p->port_usb == NULL) || !gs_buf_data_avail(&p->port_write_buf); | 
|  | 881 | spin_unlock_irq(&p->port_lock); | 
|  | 882 |  | 
|  | 883 | return cond; | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | static void gs_close(struct tty_struct *tty, struct file *file) | 
|  | 887 | { | 
|  | 888 | struct gs_port *port = tty->driver_data; | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 889 | struct gserial	*gser; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 890 |  | 
|  | 891 | spin_lock_irq(&port->port_lock); | 
|  | 892 |  | 
|  | 893 | if (port->open_count != 1) { | 
|  | 894 | if (port->open_count == 0) | 
|  | 895 | WARN_ON(1); | 
|  | 896 | else | 
|  | 897 | --port->open_count; | 
|  | 898 | goto exit; | 
|  | 899 | } | 
|  | 900 |  | 
|  | 901 | pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); | 
|  | 902 |  | 
|  | 903 | /* mark port as closing but in use; we can drop port lock | 
|  | 904 | * and sleep if necessary | 
|  | 905 | */ | 
|  | 906 | port->openclose = true; | 
|  | 907 | port->open_count = 0; | 
|  | 908 |  | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 909 | gser = port->port_usb; | 
|  | 910 | if (gser && gser->disconnect) | 
|  | 911 | gser->disconnect(gser); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 912 |  | 
|  | 913 | /* wait for circular write buffer to drain, disconnect, or at | 
|  | 914 | * most GS_CLOSE_TIMEOUT seconds; then discard the rest | 
|  | 915 | */ | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 916 | if (gs_buf_data_avail(&port->port_write_buf) > 0 && gser) { | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 917 | spin_unlock_irq(&port->port_lock); | 
|  | 918 | wait_event_interruptible_timeout(port->drain_wait, | 
|  | 919 | gs_writes_finished(port), | 
|  | 920 | GS_CLOSE_TIMEOUT * HZ); | 
|  | 921 | spin_lock_irq(&port->port_lock); | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 922 | gser = port->port_usb; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 923 | } | 
|  | 924 |  | 
|  | 925 | /* Iff we're disconnected, there can be no I/O in flight so it's | 
|  | 926 | * ok to free the circular buffer; else just scrub it.  And don't | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 927 | * let the push work queue fire again until we're re-opened. | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 928 | */ | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 929 | if (gser == NULL) | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 930 | gs_buf_free(&port->port_write_buf); | 
|  | 931 | else | 
|  | 932 | gs_buf_clear(&port->port_write_buf); | 
|  | 933 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 934 | tty->driver_data = NULL; | 
|  | 935 | port->port_tty = NULL; | 
|  | 936 |  | 
|  | 937 | port->openclose = false; | 
|  | 938 |  | 
|  | 939 | pr_debug("gs_close: ttyGS%d (%p,%p) done!\n", | 
|  | 940 | port->port_num, tty, file); | 
|  | 941 |  | 
|  | 942 | wake_up_interruptible(&port->close_wait); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 943 |  | 
|  | 944 | /* | 
|  | 945 | * Freeing the previously queued requests as they are | 
|  | 946 | * allocated again as a part of gs_open() | 
|  | 947 | */ | 
|  | 948 | if (port->port_usb) { | 
|  | 949 | spin_unlock_irq(&port->port_lock); | 
|  | 950 | usb_ep_fifo_flush(gser->out); | 
|  | 951 | usb_ep_fifo_flush(gser->in); | 
|  | 952 | spin_lock_irq(&port->port_lock); | 
|  | 953 | gs_free_requests(gser->out, &port->read_queue, NULL); | 
|  | 954 | gs_free_requests(gser->out, &port->read_pool, NULL); | 
|  | 955 | gs_free_requests(gser->in, &port->write_pool, NULL); | 
|  | 956 | } | 
|  | 957 | port->read_allocated = port->read_started = | 
|  | 958 | port->write_allocated = port->write_started = 0; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 959 | exit: | 
|  | 960 | spin_unlock_irq(&port->port_lock); | 
|  | 961 | } | 
|  | 962 |  | 
|  | 963 | static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) | 
|  | 964 | { | 
|  | 965 | struct gs_port	*port = tty->driver_data; | 
|  | 966 | unsigned long	flags; | 
|  | 967 | int		status; | 
|  | 968 |  | 
|  | 969 | pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n", | 
|  | 970 | port->port_num, tty, count); | 
|  | 971 |  | 
|  | 972 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 973 | if (count) | 
|  | 974 | count = gs_buf_put(&port->port_write_buf, buf, count); | 
|  | 975 | /* treat count == 0 as flush_chars() */ | 
|  | 976 | if (port->port_usb) | 
|  | 977 | status = gs_start_tx(port); | 
|  | 978 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 979 |  | 
|  | 980 | return count; | 
|  | 981 | } | 
|  | 982 |  | 
|  | 983 | static int gs_put_char(struct tty_struct *tty, unsigned char ch) | 
|  | 984 | { | 
|  | 985 | struct gs_port	*port = tty->driver_data; | 
|  | 986 | unsigned long	flags; | 
|  | 987 | int		status; | 
|  | 988 |  | 
|  | 989 | pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %p\n", | 
|  | 990 | port->port_num, tty, ch, __builtin_return_address(0)); | 
|  | 991 |  | 
|  | 992 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 993 | status = gs_buf_put(&port->port_write_buf, &ch, 1); | 
|  | 994 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 995 |  | 
|  | 996 | return status; | 
|  | 997 | } | 
|  | 998 |  | 
|  | 999 | static void gs_flush_chars(struct tty_struct *tty) | 
|  | 1000 | { | 
|  | 1001 | struct gs_port	*port = tty->driver_data; | 
|  | 1002 | unsigned long	flags; | 
|  | 1003 |  | 
|  | 1004 | pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); | 
|  | 1005 |  | 
|  | 1006 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 1007 | if (port->port_usb) | 
|  | 1008 | gs_start_tx(port); | 
|  | 1009 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 1010 | } | 
|  | 1011 |  | 
|  | 1012 | static int gs_write_room(struct tty_struct *tty) | 
|  | 1013 | { | 
|  | 1014 | struct gs_port	*port = tty->driver_data; | 
|  | 1015 | unsigned long	flags; | 
|  | 1016 | int		room = 0; | 
|  | 1017 |  | 
|  | 1018 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 1019 | if (port->port_usb) | 
|  | 1020 | room = gs_buf_space_avail(&port->port_write_buf); | 
|  | 1021 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 1022 |  | 
|  | 1023 | pr_vdebug("gs_write_room: (%d,%p) room=%d\n", | 
|  | 1024 | port->port_num, tty, room); | 
|  | 1025 |  | 
|  | 1026 | return room; | 
|  | 1027 | } | 
|  | 1028 |  | 
|  | 1029 | static int gs_chars_in_buffer(struct tty_struct *tty) | 
|  | 1030 | { | 
|  | 1031 | struct gs_port	*port = tty->driver_data; | 
|  | 1032 | unsigned long	flags; | 
|  | 1033 | int		chars = 0; | 
|  | 1034 |  | 
|  | 1035 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 1036 | chars = gs_buf_data_avail(&port->port_write_buf); | 
|  | 1037 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 1038 |  | 
|  | 1039 | pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%d\n", | 
|  | 1040 | port->port_num, tty, chars); | 
|  | 1041 |  | 
|  | 1042 | return chars; | 
|  | 1043 | } | 
|  | 1044 |  | 
|  | 1045 | /* undo side effects of setting TTY_THROTTLED */ | 
|  | 1046 | static void gs_unthrottle(struct tty_struct *tty) | 
|  | 1047 | { | 
|  | 1048 | struct gs_port		*port = tty->driver_data; | 
|  | 1049 | unsigned long		flags; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1050 |  | 
| Pavankumar Kondeti | f576fa6 | 2013-03-14 13:54:32 +0530 | [diff] [blame] | 1051 | /* | 
|  | 1052 | * tty's driver data is set to NULL during port close.  Nothing | 
|  | 1053 | * to do here. | 
|  | 1054 | */ | 
|  | 1055 | if (!port) | 
|  | 1056 | return; | 
|  | 1057 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1058 | spin_lock_irqsave(&port->port_lock, flags); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 1059 | if (port->port_usb) { | 
|  | 1060 | /* Kickstart read queue processing.  We don't do xon/xoff, | 
|  | 1061 | * rts/cts, or other handshaking with the host, but if the | 
|  | 1062 | * read queue backs up enough we'll be NAKing OUT packets. | 
|  | 1063 | */ | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1064 | queue_work(gserial_wq, &port->push); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 1065 | pr_vdebug(PREFIX "%d: unthrottle\n", port->port_num); | 
|  | 1066 | } | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1067 | spin_unlock_irqrestore(&port->port_lock, flags); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1068 | } | 
|  | 1069 |  | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 1070 | static int gs_break_ctl(struct tty_struct *tty, int duration) | 
|  | 1071 | { | 
|  | 1072 | struct gs_port	*port = tty->driver_data; | 
|  | 1073 | int		status = 0; | 
|  | 1074 | struct gserial	*gser; | 
|  | 1075 |  | 
|  | 1076 | pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n", | 
|  | 1077 | port->port_num, duration); | 
|  | 1078 |  | 
|  | 1079 | spin_lock_irq(&port->port_lock); | 
|  | 1080 | gser = port->port_usb; | 
|  | 1081 | if (gser && gser->send_break) | 
|  | 1082 | status = gser->send_break(gser, duration); | 
|  | 1083 | spin_unlock_irq(&port->port_lock); | 
|  | 1084 |  | 
|  | 1085 | return status; | 
|  | 1086 | } | 
|  | 1087 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1088 | static int gs_tiocmget(struct tty_struct *tty) | 
|  | 1089 | { | 
|  | 1090 | struct gs_port	*port = tty->driver_data; | 
|  | 1091 | struct gserial	*gser; | 
|  | 1092 | unsigned int result = 0; | 
|  | 1093 |  | 
|  | 1094 | spin_lock_irq(&port->port_lock); | 
|  | 1095 | gser = port->port_usb; | 
|  | 1096 | if (!gser) { | 
|  | 1097 | result = -ENODEV; | 
|  | 1098 | goto fail; | 
|  | 1099 | } | 
|  | 1100 |  | 
|  | 1101 | if (gser->get_dtr) | 
|  | 1102 | result |= (gser->get_dtr(gser) ? TIOCM_DTR : 0); | 
|  | 1103 |  | 
|  | 1104 | if (gser->get_rts) | 
|  | 1105 | result |= (gser->get_rts(gser) ? TIOCM_RTS : 0); | 
|  | 1106 |  | 
|  | 1107 | if (gser->serial_state & TIOCM_CD) | 
|  | 1108 | result |= TIOCM_CD; | 
|  | 1109 |  | 
|  | 1110 | if (gser->serial_state & TIOCM_RI) | 
|  | 1111 | result |= TIOCM_RI; | 
|  | 1112 | fail: | 
|  | 1113 | spin_unlock_irq(&port->port_lock); | 
|  | 1114 | return result; | 
|  | 1115 | } | 
|  | 1116 |  | 
|  | 1117 | static int gs_tiocmset(struct tty_struct *tty, | 
|  | 1118 | unsigned int set, unsigned int clear) | 
|  | 1119 | { | 
|  | 1120 | struct gs_port	*port = tty->driver_data; | 
|  | 1121 | struct gserial *gser; | 
|  | 1122 | int	status = 0; | 
|  | 1123 |  | 
|  | 1124 | spin_lock_irq(&port->port_lock); | 
|  | 1125 | gser = port->port_usb; | 
|  | 1126 | if (!gser) { | 
|  | 1127 | status = -ENODEV; | 
|  | 1128 | goto fail; | 
|  | 1129 | } | 
|  | 1130 |  | 
|  | 1131 | if (set & TIOCM_RI) { | 
|  | 1132 | if (gser->send_ring_indicator) { | 
|  | 1133 | gser->serial_state |= TIOCM_RI; | 
|  | 1134 | status = gser->send_ring_indicator(gser, 1); | 
|  | 1135 | } | 
|  | 1136 | } | 
|  | 1137 | if (clear & TIOCM_RI) { | 
|  | 1138 | if (gser->send_ring_indicator) { | 
|  | 1139 | gser->serial_state &= ~TIOCM_RI; | 
|  | 1140 | status = gser->send_ring_indicator(gser, 0); | 
|  | 1141 | } | 
|  | 1142 | } | 
|  | 1143 | if (set & TIOCM_CD) { | 
|  | 1144 | if (gser->send_carrier_detect) { | 
|  | 1145 | gser->serial_state |= TIOCM_CD; | 
|  | 1146 | status = gser->send_carrier_detect(gser, 1); | 
|  | 1147 | } | 
|  | 1148 | } | 
|  | 1149 | if (clear & TIOCM_CD) { | 
|  | 1150 | if (gser->send_carrier_detect) { | 
|  | 1151 | gser->serial_state &= ~TIOCM_CD; | 
|  | 1152 | status = gser->send_carrier_detect(gser, 0); | 
|  | 1153 | } | 
|  | 1154 | } | 
|  | 1155 | fail: | 
|  | 1156 | spin_unlock_irq(&port->port_lock); | 
|  | 1157 | return status; | 
|  | 1158 | } | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1159 | static const struct tty_operations gs_tty_ops = { | 
|  | 1160 | .open =			gs_open, | 
|  | 1161 | .close =		gs_close, | 
|  | 1162 | .write =		gs_write, | 
|  | 1163 | .put_char =		gs_put_char, | 
|  | 1164 | .flush_chars =		gs_flush_chars, | 
|  | 1165 | .write_room =		gs_write_room, | 
|  | 1166 | .chars_in_buffer =	gs_chars_in_buffer, | 
|  | 1167 | .unthrottle =		gs_unthrottle, | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 1168 | .break_ctl =		gs_break_ctl, | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1169 | .tiocmget  =		gs_tiocmget, | 
|  | 1170 | .tiocmset  =		gs_tiocmset, | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1171 | }; | 
|  | 1172 |  | 
|  | 1173 | /*-------------------------------------------------------------------------*/ | 
|  | 1174 |  | 
|  | 1175 | static struct tty_driver *gs_tty_driver; | 
|  | 1176 |  | 
| Benoit Goby | a035fb5 | 2011-12-14 18:04:07 -0800 | [diff] [blame] | 1177 | static int | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1178 | gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding) | 
|  | 1179 | { | 
|  | 1180 | struct gs_port	*port; | 
|  | 1181 |  | 
|  | 1182 | port = kzalloc(sizeof(struct gs_port), GFP_KERNEL); | 
|  | 1183 | if (port == NULL) | 
|  | 1184 | return -ENOMEM; | 
|  | 1185 |  | 
|  | 1186 | spin_lock_init(&port->port_lock); | 
|  | 1187 | init_waitqueue_head(&port->close_wait); | 
|  | 1188 | init_waitqueue_head(&port->drain_wait); | 
|  | 1189 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1190 | INIT_WORK(&port->push, gs_rx_push); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1191 |  | 
|  | 1192 | INIT_LIST_HEAD(&port->read_pool); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 1193 | INIT_LIST_HEAD(&port->read_queue); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1194 | INIT_LIST_HEAD(&port->write_pool); | 
|  | 1195 |  | 
|  | 1196 | port->port_num = port_num; | 
|  | 1197 | port->port_line_coding = *coding; | 
|  | 1198 |  | 
|  | 1199 | ports[port_num].port = port; | 
|  | 1200 |  | 
|  | 1201 | return 0; | 
|  | 1202 | } | 
|  | 1203 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1204 |  | 
|  | 1205 | #if defined(CONFIG_DEBUG_FS) | 
|  | 1206 |  | 
|  | 1207 | #define BUF_SIZE	512 | 
|  | 1208 |  | 
|  | 1209 | static ssize_t debug_read_status(struct file *file, char __user *ubuf, | 
|  | 1210 | size_t count, loff_t *ppos) | 
|  | 1211 | { | 
|  | 1212 | struct gs_port *ui_dev = file->private_data; | 
|  | 1213 | struct tty_struct       *tty; | 
|  | 1214 | struct gserial		*gser; | 
|  | 1215 | char *buf; | 
|  | 1216 | unsigned long flags; | 
|  | 1217 | int i = 0; | 
|  | 1218 | int ret; | 
|  | 1219 | int result = 0; | 
|  | 1220 |  | 
|  | 1221 | tty = ui_dev->port_tty; | 
|  | 1222 | gser = ui_dev->port_usb; | 
|  | 1223 |  | 
|  | 1224 | buf = kzalloc(sizeof(char) * BUF_SIZE, GFP_KERNEL); | 
|  | 1225 | if (!buf) | 
|  | 1226 | return -ENOMEM; | 
|  | 1227 |  | 
|  | 1228 | spin_lock_irqsave(&ui_dev->port_lock, flags); | 
|  | 1229 |  | 
|  | 1230 | i += scnprintf(buf + i, BUF_SIZE - i, | 
|  | 1231 | "nbytes_from_host: %lu\n", ui_dev->nbytes_from_host); | 
|  | 1232 |  | 
|  | 1233 | i += scnprintf(buf + i, BUF_SIZE - i, | 
|  | 1234 | "nbytes_to_tty: %lu\n", ui_dev->nbytes_to_tty); | 
|  | 1235 |  | 
|  | 1236 | i += scnprintf(buf + i, BUF_SIZE - i, "nbytes_with_usb_OUT_txr: %lu\n", | 
|  | 1237 | (ui_dev->nbytes_from_host - ui_dev->nbytes_to_tty)); | 
|  | 1238 |  | 
|  | 1239 | i += scnprintf(buf + i, BUF_SIZE - i, | 
|  | 1240 | "nbytes_from_tty: %lu\n", ui_dev->nbytes_from_tty); | 
|  | 1241 |  | 
|  | 1242 | i += scnprintf(buf + i, BUF_SIZE - i, | 
|  | 1243 | "nbytes_to_host: %lu\n", ui_dev->nbytes_to_host); | 
|  | 1244 |  | 
|  | 1245 | i += scnprintf(buf + i, BUF_SIZE - i, "nbytes_with_usb_IN_txr: %lu\n", | 
|  | 1246 | (ui_dev->nbytes_from_tty - ui_dev->nbytes_to_host)); | 
|  | 1247 |  | 
|  | 1248 | if (tty) | 
|  | 1249 | i += scnprintf(buf + i, BUF_SIZE - i, | 
|  | 1250 | "tty_flags: %lu\n", tty->flags); | 
|  | 1251 |  | 
|  | 1252 | if (gser->get_dtr) { | 
|  | 1253 | result |= (gser->get_dtr(gser) ? TIOCM_DTR : 0); | 
|  | 1254 | i += scnprintf(buf + i, BUF_SIZE - i, | 
|  | 1255 | "DTR_status: %d\n", result); | 
|  | 1256 | } | 
|  | 1257 |  | 
|  | 1258 | spin_unlock_irqrestore(&ui_dev->port_lock, flags); | 
|  | 1259 |  | 
|  | 1260 | ret = simple_read_from_buffer(ubuf, count, ppos, buf, i); | 
|  | 1261 |  | 
|  | 1262 | kfree(buf); | 
|  | 1263 |  | 
|  | 1264 | return ret; | 
|  | 1265 | } | 
|  | 1266 |  | 
|  | 1267 | static ssize_t debug_write_reset(struct file *file, const char __user *buf, | 
|  | 1268 | size_t count, loff_t *ppos) | 
|  | 1269 | { | 
|  | 1270 | struct gs_port *ui_dev = file->private_data; | 
|  | 1271 | unsigned long flags; | 
|  | 1272 |  | 
|  | 1273 | spin_lock_irqsave(&ui_dev->port_lock, flags); | 
|  | 1274 | ui_dev->nbytes_from_host = ui_dev->nbytes_to_tty = | 
|  | 1275 | ui_dev->nbytes_from_tty = ui_dev->nbytes_to_host = 0; | 
|  | 1276 | spin_unlock_irqrestore(&ui_dev->port_lock, flags); | 
|  | 1277 |  | 
|  | 1278 | return count; | 
|  | 1279 | } | 
|  | 1280 |  | 
|  | 1281 | static int serial_debug_open(struct inode *inode, struct file *file) | 
|  | 1282 | { | 
|  | 1283 | file->private_data = inode->i_private; | 
|  | 1284 | return 0; | 
|  | 1285 | } | 
|  | 1286 |  | 
|  | 1287 | const struct file_operations debug_rst_ops = { | 
|  | 1288 | .open = serial_debug_open, | 
|  | 1289 | .write = debug_write_reset, | 
|  | 1290 | }; | 
|  | 1291 |  | 
|  | 1292 | const struct file_operations debug_adb_ops = { | 
|  | 1293 | .open = serial_debug_open, | 
|  | 1294 | .read = debug_read_status, | 
|  | 1295 | }; | 
|  | 1296 |  | 
|  | 1297 | static void usb_debugfs_init(struct gs_port *ui_dev, int port_num) | 
|  | 1298 | { | 
|  | 1299 | struct dentry *dent; | 
|  | 1300 | char buf[48]; | 
|  | 1301 |  | 
|  | 1302 | snprintf(buf, 48, "usb_serial%d", port_num); | 
|  | 1303 | dent = debugfs_create_dir(buf, 0); | 
|  | 1304 | if (IS_ERR(dent)) | 
|  | 1305 | return; | 
|  | 1306 |  | 
|  | 1307 | debugfs_create_file("readstatus", 0444, dent, ui_dev, &debug_adb_ops); | 
| Vamsi Krishna | 118afa1 | 2013-03-11 13:17:09 -0700 | [diff] [blame] | 1308 | debugfs_create_file("reset", S_IRUGO | S_IWUSR, | 
|  | 1309 | dent, ui_dev, &debug_rst_ops); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1310 | } | 
|  | 1311 | #else | 
|  | 1312 | static void usb_debugfs_init(struct gs_port *ui_dev) {} | 
|  | 1313 | #endif | 
|  | 1314 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1315 | /** | 
|  | 1316 | * gserial_setup - initialize TTY driver for one or more ports | 
|  | 1317 | * @g: gadget to associate with these ports | 
|  | 1318 | * @count: how many ports to support | 
|  | 1319 | * Context: may sleep | 
|  | 1320 | * | 
|  | 1321 | * The TTY stack needs to know in advance how many devices it should | 
|  | 1322 | * plan to manage.  Use this call to set up the ports you will be | 
|  | 1323 | * exporting through USB.  Later, connect them to functions based | 
|  | 1324 | * on what configuration is activated by the USB host; and disconnect | 
|  | 1325 | * them as appropriate. | 
|  | 1326 | * | 
|  | 1327 | * An example would be a two-configuration device in which both | 
|  | 1328 | * configurations expose port 0, but through different functions. | 
|  | 1329 | * One configuration could even expose port 1 while the other | 
|  | 1330 | * one doesn't. | 
|  | 1331 | * | 
|  | 1332 | * Returns negative errno or zero. | 
|  | 1333 | */ | 
| Benoit Goby | a035fb5 | 2011-12-14 18:04:07 -0800 | [diff] [blame] | 1334 | int gserial_setup(struct usb_gadget *g, unsigned count) | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1335 | { | 
|  | 1336 | unsigned			i; | 
|  | 1337 | struct usb_cdc_line_coding	coding; | 
|  | 1338 | int				status; | 
|  | 1339 |  | 
|  | 1340 | if (count == 0 || count > N_PORTS) | 
|  | 1341 | return -EINVAL; | 
|  | 1342 |  | 
|  | 1343 | gs_tty_driver = alloc_tty_driver(count); | 
|  | 1344 | if (!gs_tty_driver) | 
|  | 1345 | return -ENOMEM; | 
|  | 1346 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1347 | gs_tty_driver->driver_name = "g_serial"; | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 1348 | gs_tty_driver->name = PREFIX; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1349 | /* uses dynamically assigned dev_t values */ | 
|  | 1350 |  | 
|  | 1351 | gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; | 
|  | 1352 | gs_tty_driver->subtype = SERIAL_TYPE_NORMAL; | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1353 | gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | 
|  | 1354 | | TTY_DRIVER_RESET_TERMIOS; | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1355 | gs_tty_driver->init_termios = tty_std_termios; | 
|  | 1356 |  | 
|  | 1357 | /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on | 
|  | 1358 | * MS-Windows.  Otherwise, most of these flags shouldn't affect | 
|  | 1359 | * anything unless we were to actually hook up to a serial line. | 
|  | 1360 | */ | 
|  | 1361 | gs_tty_driver->init_termios.c_cflag = | 
|  | 1362 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; | 
|  | 1363 | gs_tty_driver->init_termios.c_ispeed = 9600; | 
|  | 1364 | gs_tty_driver->init_termios.c_ospeed = 9600; | 
|  | 1365 |  | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1366 | coding.dwDTERate = cpu_to_le32(9600); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1367 | coding.bCharFormat = 8; | 
|  | 1368 | coding.bParityType = USB_CDC_NO_PARITY; | 
|  | 1369 | coding.bDataBits = USB_CDC_1_STOP_BITS; | 
|  | 1370 |  | 
|  | 1371 | tty_set_operations(gs_tty_driver, &gs_tty_ops); | 
|  | 1372 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1373 | gserial_wq = create_singlethread_workqueue("k_gserial"); | 
|  | 1374 | if (!gserial_wq) { | 
|  | 1375 | status = -ENOMEM; | 
|  | 1376 | goto fail; | 
|  | 1377 | } | 
|  | 1378 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1379 | /* make devices be openable */ | 
|  | 1380 | for (i = 0; i < count; i++) { | 
|  | 1381 | mutex_init(&ports[i].lock); | 
|  | 1382 | status = gs_port_alloc(i, &coding); | 
|  | 1383 | if (status) { | 
|  | 1384 | count = i; | 
|  | 1385 | goto fail; | 
|  | 1386 | } | 
|  | 1387 | } | 
|  | 1388 | n_ports = count; | 
|  | 1389 |  | 
|  | 1390 | /* export the driver ... */ | 
|  | 1391 | status = tty_register_driver(gs_tty_driver); | 
|  | 1392 | if (status) { | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1393 | pr_err("%s: cannot register, err %d\n", | 
|  | 1394 | __func__, status); | 
|  | 1395 | goto fail; | 
|  | 1396 | } | 
|  | 1397 |  | 
|  | 1398 | /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */ | 
|  | 1399 | for (i = 0; i < count; i++) { | 
|  | 1400 | struct device	*tty_dev; | 
|  | 1401 |  | 
|  | 1402 | tty_dev = tty_register_device(gs_tty_driver, i, &g->dev); | 
|  | 1403 | if (IS_ERR(tty_dev)) | 
|  | 1404 | pr_warning("%s: no classdev for port %d, err %ld\n", | 
|  | 1405 | __func__, i, PTR_ERR(tty_dev)); | 
|  | 1406 | } | 
|  | 1407 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1408 | for (i = 0; i < count; i++) | 
|  | 1409 | usb_debugfs_init(ports[i].port, i); | 
|  | 1410 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1411 | pr_debug("%s: registered %d ttyGS* device%s\n", __func__, | 
|  | 1412 | count, (count == 1) ? "" : "s"); | 
|  | 1413 |  | 
|  | 1414 | return status; | 
|  | 1415 | fail: | 
|  | 1416 | while (count--) | 
|  | 1417 | kfree(ports[count].port); | 
| Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 1418 | if (gserial_wq) | 
|  | 1419 | destroy_workqueue(gserial_wq); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1420 | put_tty_driver(gs_tty_driver); | 
|  | 1421 | gs_tty_driver = NULL; | 
|  | 1422 | return status; | 
|  | 1423 | } | 
|  | 1424 |  | 
|  | 1425 | static int gs_closed(struct gs_port *port) | 
|  | 1426 | { | 
|  | 1427 | int cond; | 
|  | 1428 |  | 
|  | 1429 | spin_lock_irq(&port->port_lock); | 
|  | 1430 | cond = (port->open_count == 0) && !port->openclose; | 
|  | 1431 | spin_unlock_irq(&port->port_lock); | 
|  | 1432 | return cond; | 
|  | 1433 | } | 
|  | 1434 |  | 
|  | 1435 | /** | 
|  | 1436 | * gserial_cleanup - remove TTY-over-USB driver and devices | 
|  | 1437 | * Context: may sleep | 
|  | 1438 | * | 
|  | 1439 | * This is called to free all resources allocated by @gserial_setup(). | 
|  | 1440 | * Accordingly, it may need to wait until some open /dev/ files have | 
|  | 1441 | * closed. | 
|  | 1442 | * | 
|  | 1443 | * The caller must have issued @gserial_disconnect() for any ports | 
|  | 1444 | * that had previously been connected, so that there is never any | 
|  | 1445 | * I/O pending when it's called. | 
|  | 1446 | */ | 
|  | 1447 | void gserial_cleanup(void) | 
|  | 1448 | { | 
|  | 1449 | unsigned	i; | 
|  | 1450 | struct gs_port	*port; | 
|  | 1451 |  | 
| David Brownell | ac90e36 | 2008-07-01 13:18:20 -0700 | [diff] [blame] | 1452 | if (!gs_tty_driver) | 
|  | 1453 | return; | 
|  | 1454 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1455 | /* start sysfs and /dev/ttyGS* node removal */ | 
|  | 1456 | for (i = 0; i < n_ports; i++) | 
|  | 1457 | tty_unregister_device(gs_tty_driver, i); | 
|  | 1458 |  | 
|  | 1459 | for (i = 0; i < n_ports; i++) { | 
|  | 1460 | /* prevent new opens */ | 
|  | 1461 | mutex_lock(&ports[i].lock); | 
|  | 1462 | port = ports[i].port; | 
|  | 1463 | ports[i].port = NULL; | 
|  | 1464 | mutex_unlock(&ports[i].lock); | 
|  | 1465 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1466 | cancel_work_sync(&port->push); | 
| David Brownell | 937ef73 | 2008-07-07 12:16:08 -0700 | [diff] [blame] | 1467 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1468 | /* wait for old opens to finish */ | 
|  | 1469 | wait_event(port->close_wait, gs_closed(port)); | 
|  | 1470 |  | 
|  | 1471 | WARN_ON(port->port_usb != NULL); | 
|  | 1472 |  | 
|  | 1473 | kfree(port); | 
|  | 1474 | } | 
|  | 1475 | n_ports = 0; | 
|  | 1476 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1477 | destroy_workqueue(gserial_wq); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1478 | tty_unregister_driver(gs_tty_driver); | 
| Jon Povey | b23097b | 2010-06-14 19:42:10 +0900 | [diff] [blame] | 1479 | put_tty_driver(gs_tty_driver); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1480 | gs_tty_driver = NULL; | 
|  | 1481 |  | 
|  | 1482 | pr_debug("%s: cleaned up ttyGS* support\n", __func__); | 
|  | 1483 | } | 
|  | 1484 |  | 
|  | 1485 | /** | 
|  | 1486 | * gserial_connect - notify TTY I/O glue that USB link is active | 
|  | 1487 | * @gser: the function, set up with endpoints and descriptors | 
|  | 1488 | * @port_num: which port is active | 
|  | 1489 | * Context: any (usually from irq) | 
|  | 1490 | * | 
|  | 1491 | * This is called activate endpoints and let the TTY layer know that | 
|  | 1492 | * the connection is active ... not unlike "carrier detect".  It won't | 
|  | 1493 | * necessarily start I/O queues; unless the TTY is held open by any | 
|  | 1494 | * task, there would be no point.  However, the endpoints will be | 
|  | 1495 | * activated so the USB host can perform I/O, subject to basic USB | 
|  | 1496 | * hardware flow control. | 
|  | 1497 | * | 
|  | 1498 | * Caller needs to have set up the endpoints and USB function in @dev | 
|  | 1499 | * before calling this, as well as the appropriate (speed-specific) | 
|  | 1500 | * endpoint descriptors, and also have set up the TTY driver by calling | 
|  | 1501 | * @gserial_setup(). | 
|  | 1502 | * | 
|  | 1503 | * Returns negative errno or zero. | 
|  | 1504 | * On success, ep->driver_data will be overwritten. | 
|  | 1505 | */ | 
|  | 1506 | int gserial_connect(struct gserial *gser, u8 port_num) | 
|  | 1507 | { | 
|  | 1508 | struct gs_port	*port; | 
|  | 1509 | unsigned long	flags; | 
|  | 1510 | int		status; | 
|  | 1511 |  | 
|  | 1512 | if (!gs_tty_driver || port_num >= n_ports) | 
|  | 1513 | return -ENXIO; | 
|  | 1514 |  | 
|  | 1515 | /* we "know" gserial_cleanup() hasn't been called */ | 
|  | 1516 | port = ports[port_num].port; | 
|  | 1517 |  | 
|  | 1518 | /* activate the endpoints */ | 
| Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1519 | status = usb_ep_enable(gser->in); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1520 | if (status < 0) | 
|  | 1521 | return status; | 
|  | 1522 | gser->in->driver_data = port; | 
|  | 1523 |  | 
| Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1524 | status = usb_ep_enable(gser->out); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1525 | if (status < 0) | 
|  | 1526 | goto fail_out; | 
|  | 1527 | gser->out->driver_data = port; | 
|  | 1528 |  | 
|  | 1529 | /* then tell the tty glue that I/O can work */ | 
|  | 1530 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 1531 | gser->ioport = port; | 
|  | 1532 | port->port_usb = gser; | 
|  | 1533 |  | 
|  | 1534 | /* REVISIT unclear how best to handle this state... | 
|  | 1535 | * we don't really couple it with the Linux TTY. | 
|  | 1536 | */ | 
|  | 1537 | gser->port_line_coding = port->port_line_coding; | 
|  | 1538 |  | 
|  | 1539 | /* REVISIT if waiting on "carrier detect", signal. */ | 
|  | 1540 |  | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 1541 | /* if it's already open, start I/O ... and notify the serial | 
|  | 1542 | * protocol about open/close status (connect/disconnect). | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1543 | */ | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1544 | if (port->open_count) { | 
|  | 1545 | pr_debug("gserial_connect: start ttyGS%d\n", port->port_num); | 
|  | 1546 | gs_start_io(port); | 
| David Brownell | 1f1ba11 | 2008-08-06 18:49:57 -0700 | [diff] [blame] | 1547 | if (gser->connect) | 
|  | 1548 | gser->connect(gser); | 
|  | 1549 | } else { | 
|  | 1550 | if (gser->disconnect) | 
|  | 1551 | gser->disconnect(gser); | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1552 | } | 
|  | 1553 |  | 
|  | 1554 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 1555 |  | 
|  | 1556 | return status; | 
|  | 1557 |  | 
|  | 1558 | fail_out: | 
|  | 1559 | usb_ep_disable(gser->in); | 
|  | 1560 | gser->in->driver_data = NULL; | 
|  | 1561 | return status; | 
|  | 1562 | } | 
|  | 1563 |  | 
|  | 1564 | /** | 
|  | 1565 | * gserial_disconnect - notify TTY I/O glue that USB link is inactive | 
|  | 1566 | * @gser: the function, on which gserial_connect() was called | 
|  | 1567 | * Context: any (usually from irq) | 
|  | 1568 | * | 
|  | 1569 | * This is called to deactivate endpoints and let the TTY layer know | 
|  | 1570 | * that the connection went inactive ... not unlike "hangup". | 
|  | 1571 | * | 
|  | 1572 | * On return, the state is as if gserial_connect() had never been called; | 
|  | 1573 | * there is no active USB I/O on these endpoints. | 
|  | 1574 | */ | 
|  | 1575 | void gserial_disconnect(struct gserial *gser) | 
|  | 1576 | { | 
|  | 1577 | struct gs_port	*port = gser->ioport; | 
|  | 1578 | unsigned long	flags; | 
|  | 1579 |  | 
|  | 1580 | if (!port) | 
|  | 1581 | return; | 
|  | 1582 |  | 
|  | 1583 | /* tell the TTY glue not to do I/O here any more */ | 
|  | 1584 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 1585 |  | 
|  | 1586 | /* REVISIT as above: how best to track this? */ | 
|  | 1587 | port->port_line_coding = gser->port_line_coding; | 
|  | 1588 |  | 
|  | 1589 | port->port_usb = NULL; | 
|  | 1590 | gser->ioport = NULL; | 
|  | 1591 | if (port->open_count > 0 || port->openclose) { | 
|  | 1592 | wake_up_interruptible(&port->drain_wait); | 
|  | 1593 | if (port->port_tty) | 
|  | 1594 | tty_hangup(port->port_tty); | 
|  | 1595 | } | 
|  | 1596 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 1597 |  | 
|  | 1598 | /* disable endpoints, aborting down any active I/O */ | 
|  | 1599 | usb_ep_disable(gser->out); | 
|  | 1600 | gser->out->driver_data = NULL; | 
|  | 1601 |  | 
|  | 1602 | usb_ep_disable(gser->in); | 
|  | 1603 | gser->in->driver_data = NULL; | 
|  | 1604 |  | 
|  | 1605 | /* finally, free any unused/unusable I/O buffers */ | 
|  | 1606 | spin_lock_irqsave(&port->port_lock, flags); | 
|  | 1607 | if (port->open_count == 0 && !port->openclose) | 
|  | 1608 | gs_buf_free(&port->port_write_buf); | 
| Jim Sung | 28609d4 | 2010-11-04 18:47:51 -0700 | [diff] [blame] | 1609 | gs_free_requests(gser->out, &port->read_pool, NULL); | 
|  | 1610 | gs_free_requests(gser->out, &port->read_queue, NULL); | 
|  | 1611 | gs_free_requests(gser->in, &port->write_pool, NULL); | 
|  | 1612 |  | 
|  | 1613 | port->read_allocated = port->read_started = | 
|  | 1614 | port->write_allocated = port->write_started = 0; | 
|  | 1615 |  | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1616 | port->nbytes_from_host = port->nbytes_to_tty = | 
|  | 1617 | port->nbytes_from_tty = port->nbytes_to_host = 0; | 
|  | 1618 |  | 
| David Brownell | c1dca56 | 2008-06-19 17:51:44 -0700 | [diff] [blame] | 1619 | spin_unlock_irqrestore(&port->port_lock, flags); | 
|  | 1620 | } |