1 // SPDX-License-Identifier: GPL-2.0
3 * xhci-dbgtty.c - tty glue for xHCI debug capability
5 * Copyright (C) 2017 Intel Corporation
10 #include <linux/slab.h>
11 #include <linux/tty.h>
12 #include <linux/tty_flip.h>
13 #include <linux/idr.h>
16 #include "xhci-dbgcap.h"
18 static struct tty_driver *dbc_tty_driver;
19 static struct idr dbc_tty_minors;
20 static DEFINE_MUTEX(dbc_tty_minors_lock);
22 static inline struct dbc_port *dbc_to_port(struct xhci_dbc *dbc)
28 dbc_kfifo_to_req(struct dbc_port *port, char *packet)
32 len = kfifo_len(&port->port.xmit_fifo);
37 len = min(len, DBC_MAX_PACKET);
39 if (port->tx_boundary)
40 len = min(port->tx_boundary, len);
42 len = kfifo_out(&port->port.xmit_fifo, packet, len);
44 if (port->tx_boundary)
45 port->tx_boundary -= len;
50 static int dbc_start_tx(struct dbc_port *port)
51 __releases(&port->port_lock)
52 __acquires(&port->port_lock)
55 struct dbc_request *req;
57 bool do_tty_wake = false;
58 struct list_head *pool = &port->write_pool;
60 while (!list_empty(pool)) {
61 req = list_entry(pool->next, struct dbc_request, list_pool);
62 len = dbc_kfifo_to_req(port, req->buf);
68 list_del(&req->list_pool);
70 spin_unlock(&port->port_lock);
71 status = dbc_ep_queue(req);
72 spin_lock(&port->port_lock);
75 list_add(&req->list_pool, pool);
80 if (do_tty_wake && port->port.tty)
81 tty_wakeup(port->port.tty);
86 static void dbc_start_rx(struct dbc_port *port)
87 __releases(&port->port_lock)
88 __acquires(&port->port_lock)
90 struct dbc_request *req;
92 struct list_head *pool = &port->read_pool;
94 while (!list_empty(pool)) {
98 req = list_entry(pool->next, struct dbc_request, list_pool);
99 list_del(&req->list_pool);
100 req->length = DBC_MAX_PACKET;
102 spin_unlock(&port->port_lock);
103 status = dbc_ep_queue(req);
104 spin_lock(&port->port_lock);
107 list_add(&req->list_pool, pool);
114 * Queue received data to tty buffer and push it.
116 * Returns nr of remaining bytes that didn't fit tty buffer, i.e. 0 if all
117 * bytes sucessfullt moved. In case of error returns negative errno.
118 * Call with lock held
120 static int dbc_rx_push_buffer(struct dbc_port *port, struct dbc_request *req)
122 char *packet = req->buf;
123 unsigned int n, size = req->actual;
129 /* if n_read is set then request was partially moved to tty buffer */
136 count = tty_insert_flip_string(&port->port, packet, size);
138 tty_flip_buffer_push(&port->port);
140 port->n_read += count;
149 dbc_read_complete(struct xhci_dbc *dbc, struct dbc_request *req)
152 struct dbc_port *port = dbc_to_port(dbc);
153 struct tty_struct *tty;
156 tty = port->port.tty;
158 spin_lock_irqsave(&port->port_lock, flags);
161 * Only defer copyig data to tty buffer in case:
162 * - !list_empty(&port->read_queue), there are older pending data
164 * - failed to copy all data to buffer, defer remaining part
167 if (list_empty(&port->read_queue) && tty && !tty_throttled(tty)) {
168 untransferred = dbc_rx_push_buffer(port, req);
169 if (untransferred == 0) {
170 list_add_tail(&req->list_pool, &port->read_pool);
171 if (req->status != -ESHUTDOWN)
177 /* defer moving data from req to tty buffer to a tasklet */
178 list_add_tail(&req->list_pool, &port->read_queue);
179 tasklet_schedule(&port->push);
181 spin_unlock_irqrestore(&port->port_lock, flags);
184 static void dbc_write_complete(struct xhci_dbc *dbc, struct dbc_request *req)
187 struct dbc_port *port = dbc_to_port(dbc);
189 spin_lock_irqsave(&port->port_lock, flags);
190 list_add(&req->list_pool, &port->write_pool);
191 switch (req->status) {
198 dev_warn(dbc->dev, "unexpected write complete status %d\n",
202 spin_unlock_irqrestore(&port->port_lock, flags);
205 static void xhci_dbc_free_req(struct dbc_request *req)
208 dbc_free_request(req);
212 xhci_dbc_alloc_requests(struct xhci_dbc *dbc, unsigned int direction,
213 struct list_head *head,
214 void (*fn)(struct xhci_dbc *, struct dbc_request *))
217 struct dbc_request *req;
219 for (i = 0; i < DBC_QUEUE_SIZE; i++) {
220 req = dbc_alloc_request(dbc, direction, GFP_KERNEL);
224 req->length = DBC_MAX_PACKET;
225 req->buf = kmalloc(req->length, GFP_KERNEL);
227 dbc_free_request(req);
232 list_add_tail(&req->list_pool, head);
235 return list_empty(head) ? -ENOMEM : 0;
239 xhci_dbc_free_requests(struct list_head *head)
241 struct dbc_request *req;
243 while (!list_empty(head)) {
244 req = list_entry(head->next, struct dbc_request, list_pool);
245 list_del(&req->list_pool);
246 xhci_dbc_free_req(req);
250 static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
252 struct dbc_port *port;
254 mutex_lock(&dbc_tty_minors_lock);
255 port = idr_find(&dbc_tty_minors, tty->index);
256 mutex_unlock(&dbc_tty_minors_lock);
261 tty->driver_data = port;
263 return tty_port_install(&port->port, driver, tty);
266 static int dbc_tty_open(struct tty_struct *tty, struct file *file)
268 struct dbc_port *port = tty->driver_data;
270 return tty_port_open(&port->port, tty, file);
273 static void dbc_tty_close(struct tty_struct *tty, struct file *file)
275 struct dbc_port *port = tty->driver_data;
277 tty_port_close(&port->port, tty, file);
280 static ssize_t dbc_tty_write(struct tty_struct *tty, const u8 *buf,
283 struct dbc_port *port = tty->driver_data;
285 unsigned int written = 0;
287 spin_lock_irqsave(&port->port_lock, flags);
290 * Treat tty write as one usb transfer. Make sure the writes are turned
291 * into TRB request having the same size boundaries as the tty writes.
292 * Don't add data to kfifo before previous write is turned into TRBs
294 if (port->tx_boundary) {
295 spin_unlock_irqrestore(&port->port_lock, flags);
300 written = kfifo_in(&port->port.xmit_fifo, buf, count);
302 if (written == count)
303 port->tx_boundary = kfifo_len(&port->port.xmit_fifo);
308 spin_unlock_irqrestore(&port->port_lock, flags);
313 static int dbc_tty_put_char(struct tty_struct *tty, u8 ch)
315 struct dbc_port *port = tty->driver_data;
319 spin_lock_irqsave(&port->port_lock, flags);
320 status = kfifo_put(&port->port.xmit_fifo, ch);
321 spin_unlock_irqrestore(&port->port_lock, flags);
326 static void dbc_tty_flush_chars(struct tty_struct *tty)
328 struct dbc_port *port = tty->driver_data;
331 spin_lock_irqsave(&port->port_lock, flags);
333 spin_unlock_irqrestore(&port->port_lock, flags);
336 static unsigned int dbc_tty_write_room(struct tty_struct *tty)
338 struct dbc_port *port = tty->driver_data;
342 spin_lock_irqsave(&port->port_lock, flags);
343 room = kfifo_avail(&port->port.xmit_fifo);
345 if (port->tx_boundary)
348 spin_unlock_irqrestore(&port->port_lock, flags);
353 static unsigned int dbc_tty_chars_in_buffer(struct tty_struct *tty)
355 struct dbc_port *port = tty->driver_data;
359 spin_lock_irqsave(&port->port_lock, flags);
360 chars = kfifo_len(&port->port.xmit_fifo);
361 spin_unlock_irqrestore(&port->port_lock, flags);
366 static void dbc_tty_unthrottle(struct tty_struct *tty)
368 struct dbc_port *port = tty->driver_data;
371 spin_lock_irqsave(&port->port_lock, flags);
372 tasklet_schedule(&port->push);
373 spin_unlock_irqrestore(&port->port_lock, flags);
376 static const struct tty_operations dbc_tty_ops = {
377 .install = dbc_tty_install,
378 .open = dbc_tty_open,
379 .close = dbc_tty_close,
380 .write = dbc_tty_write,
381 .put_char = dbc_tty_put_char,
382 .flush_chars = dbc_tty_flush_chars,
383 .write_room = dbc_tty_write_room,
384 .chars_in_buffer = dbc_tty_chars_in_buffer,
385 .unthrottle = dbc_tty_unthrottle,
388 static void dbc_rx_push(struct tasklet_struct *t)
390 struct dbc_request *req;
391 struct tty_struct *tty;
393 bool disconnect = false;
394 struct dbc_port *port = from_tasklet(port, t, push);
395 struct list_head *queue = &port->read_queue;
398 spin_lock_irqsave(&port->port_lock, flags);
399 tty = port->port.tty;
400 while (!list_empty(queue)) {
401 req = list_first_entry(queue, struct dbc_request, list_pool);
403 if (tty && tty_throttled(tty))
406 switch (req->status) {
413 pr_warn("ttyDBC0: unexpected RX status %d\n",
418 untransferred = dbc_rx_push_buffer(port, req);
419 if (untransferred > 0)
422 list_move_tail(&req->list_pool, &port->read_pool);
425 if (!list_empty(queue))
426 tasklet_schedule(&port->push);
431 spin_unlock_irqrestore(&port->port_lock, flags);
434 static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty)
437 struct dbc_port *port = container_of(_port, struct dbc_port, port);
439 spin_lock_irqsave(&port->port_lock, flags);
441 spin_unlock_irqrestore(&port->port_lock, flags);
446 static const struct tty_port_operations dbc_port_ops = {
447 .activate = dbc_port_activate,
451 xhci_dbc_tty_init_port(struct xhci_dbc *dbc, struct dbc_port *port)
453 tty_port_init(&port->port);
454 spin_lock_init(&port->port_lock);
455 tasklet_setup(&port->push, dbc_rx_push);
456 INIT_LIST_HEAD(&port->read_pool);
457 INIT_LIST_HEAD(&port->read_queue);
458 INIT_LIST_HEAD(&port->write_pool);
460 port->port.ops = &dbc_port_ops;
465 xhci_dbc_tty_exit_port(struct dbc_port *port)
467 tasklet_kill(&port->push);
468 tty_port_destroy(&port->port);
471 static int xhci_dbc_tty_register_device(struct xhci_dbc *dbc)
474 struct device *tty_dev;
475 struct dbc_port *port = dbc_to_port(dbc);
477 if (port->registered)
480 xhci_dbc_tty_init_port(dbc, port);
482 mutex_lock(&dbc_tty_minors_lock);
483 port->minor = idr_alloc(&dbc_tty_minors, port, 0, 64, GFP_KERNEL);
484 mutex_unlock(&dbc_tty_minors_lock);
486 if (port->minor < 0) {
491 ret = kfifo_alloc(&port->port.xmit_fifo, DBC_WRITE_BUF_SIZE,
496 ret = xhci_dbc_alloc_requests(dbc, BULK_IN, &port->read_pool,
501 ret = xhci_dbc_alloc_requests(dbc, BULK_OUT, &port->write_pool,
504 goto err_free_requests;
506 tty_dev = tty_port_register_device(&port->port,
507 dbc_tty_driver, port->minor, NULL);
508 if (IS_ERR(tty_dev)) {
509 ret = PTR_ERR(tty_dev);
510 goto err_free_requests;
513 port->registered = true;
518 xhci_dbc_free_requests(&port->read_pool);
519 xhci_dbc_free_requests(&port->write_pool);
521 kfifo_free(&port->port.xmit_fifo);
523 idr_remove(&dbc_tty_minors, port->minor);
525 xhci_dbc_tty_exit_port(port);
527 dev_err(dbc->dev, "can't register tty port, err %d\n", ret);
532 static void xhci_dbc_tty_unregister_device(struct xhci_dbc *dbc)
534 struct dbc_port *port = dbc_to_port(dbc);
536 if (!port->registered)
538 tty_unregister_device(dbc_tty_driver, port->minor);
539 xhci_dbc_tty_exit_port(port);
540 port->registered = false;
542 mutex_lock(&dbc_tty_minors_lock);
543 idr_remove(&dbc_tty_minors, port->minor);
544 mutex_unlock(&dbc_tty_minors_lock);
546 kfifo_free(&port->port.xmit_fifo);
547 xhci_dbc_free_requests(&port->read_pool);
548 xhci_dbc_free_requests(&port->read_queue);
549 xhci_dbc_free_requests(&port->write_pool);
552 static const struct dbc_driver dbc_driver = {
553 .configure = xhci_dbc_tty_register_device,
554 .disconnect = xhci_dbc_tty_unregister_device,
557 int xhci_dbc_tty_probe(struct device *dev, void __iomem *base, struct xhci_hcd *xhci)
559 struct xhci_dbc *dbc;
560 struct dbc_port *port;
566 port = kzalloc(sizeof(*port), GFP_KERNEL);
570 dbc = xhci_alloc_dbc(dev, base, &dbc_driver);
579 /* get rid of xhci once this is a real driver binding to a device */
590 * undo what probe did, assume dbc is stopped already.
591 * we also assume tty_unregister_device() is called before this
593 void xhci_dbc_tty_remove(struct xhci_dbc *dbc)
595 struct dbc_port *port = dbc_to_port(dbc);
597 xhci_dbc_remove(dbc);
601 int dbc_tty_init(void)
605 idr_init(&dbc_tty_minors);
607 dbc_tty_driver = tty_alloc_driver(64, TTY_DRIVER_REAL_RAW |
608 TTY_DRIVER_DYNAMIC_DEV);
609 if (IS_ERR(dbc_tty_driver)) {
610 idr_destroy(&dbc_tty_minors);
611 return PTR_ERR(dbc_tty_driver);
614 dbc_tty_driver->driver_name = "dbc_serial";
615 dbc_tty_driver->name = "ttyDBC";
617 dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
618 dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
619 dbc_tty_driver->init_termios = tty_std_termios;
620 dbc_tty_driver->init_termios.c_cflag =
621 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
622 dbc_tty_driver->init_termios.c_ispeed = 9600;
623 dbc_tty_driver->init_termios.c_ospeed = 9600;
625 tty_set_operations(dbc_tty_driver, &dbc_tty_ops);
627 ret = tty_register_driver(dbc_tty_driver);
629 pr_err("Can't register dbc tty driver\n");
630 tty_driver_kref_put(dbc_tty_driver);
631 idr_destroy(&dbc_tty_minors);
637 void dbc_tty_exit(void)
639 if (dbc_tty_driver) {
640 tty_unregister_driver(dbc_tty_driver);
641 tty_driver_kref_put(dbc_tty_driver);
642 dbc_tty_driver = NULL;
645 idr_destroy(&dbc_tty_minors);