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