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)
27 static int dbc_start_tx(struct dbc_port *port)
28 __releases(&port->port_lock)
29 __acquires(&port->port_lock)
32 struct dbc_request *req;
34 bool do_tty_wake = false;
35 struct list_head *pool = &port->write_pool;
37 while (!list_empty(pool)) {
38 req = list_entry(pool->next, struct dbc_request, list_pool);
39 len = kfifo_out(&port->port.xmit_fifo, req->buf, DBC_MAX_PACKET);
45 list_del(&req->list_pool);
47 spin_unlock(&port->port_lock);
48 status = dbc_ep_queue(req);
49 spin_lock(&port->port_lock);
52 list_add(&req->list_pool, pool);
57 if (do_tty_wake && port->port.tty)
58 tty_wakeup(port->port.tty);
63 static void dbc_start_rx(struct dbc_port *port)
64 __releases(&port->port_lock)
65 __acquires(&port->port_lock)
67 struct dbc_request *req;
69 struct list_head *pool = &port->read_pool;
71 while (!list_empty(pool)) {
75 req = list_entry(pool->next, struct dbc_request, list_pool);
76 list_del(&req->list_pool);
77 req->length = DBC_MAX_PACKET;
79 spin_unlock(&port->port_lock);
80 status = dbc_ep_queue(req);
81 spin_lock(&port->port_lock);
84 list_add(&req->list_pool, pool);
91 dbc_read_complete(struct xhci_dbc *dbc, struct dbc_request *req)
94 struct dbc_port *port = dbc_to_port(dbc);
96 spin_lock_irqsave(&port->port_lock, flags);
97 list_add_tail(&req->list_pool, &port->read_queue);
98 tasklet_schedule(&port->push);
99 spin_unlock_irqrestore(&port->port_lock, flags);
102 static void dbc_write_complete(struct xhci_dbc *dbc, struct dbc_request *req)
105 struct dbc_port *port = dbc_to_port(dbc);
107 spin_lock_irqsave(&port->port_lock, flags);
108 list_add(&req->list_pool, &port->write_pool);
109 switch (req->status) {
116 dev_warn(dbc->dev, "unexpected write complete status %d\n",
120 spin_unlock_irqrestore(&port->port_lock, flags);
123 static void xhci_dbc_free_req(struct dbc_request *req)
126 dbc_free_request(req);
130 xhci_dbc_alloc_requests(struct xhci_dbc *dbc, unsigned int direction,
131 struct list_head *head,
132 void (*fn)(struct xhci_dbc *, struct dbc_request *))
135 struct dbc_request *req;
137 for (i = 0; i < DBC_QUEUE_SIZE; i++) {
138 req = dbc_alloc_request(dbc, direction, GFP_KERNEL);
142 req->length = DBC_MAX_PACKET;
143 req->buf = kmalloc(req->length, GFP_KERNEL);
145 dbc_free_request(req);
150 list_add_tail(&req->list_pool, head);
153 return list_empty(head) ? -ENOMEM : 0;
157 xhci_dbc_free_requests(struct list_head *head)
159 struct dbc_request *req;
161 while (!list_empty(head)) {
162 req = list_entry(head->next, struct dbc_request, list_pool);
163 list_del(&req->list_pool);
164 xhci_dbc_free_req(req);
168 static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
170 struct dbc_port *port;
172 mutex_lock(&dbc_tty_minors_lock);
173 port = idr_find(&dbc_tty_minors, tty->index);
174 mutex_unlock(&dbc_tty_minors_lock);
179 tty->driver_data = port;
181 return tty_port_install(&port->port, driver, tty);
184 static int dbc_tty_open(struct tty_struct *tty, struct file *file)
186 struct dbc_port *port = tty->driver_data;
188 return tty_port_open(&port->port, tty, file);
191 static void dbc_tty_close(struct tty_struct *tty, struct file *file)
193 struct dbc_port *port = tty->driver_data;
195 tty_port_close(&port->port, tty, file);
198 static ssize_t dbc_tty_write(struct tty_struct *tty, const u8 *buf,
201 struct dbc_port *port = tty->driver_data;
204 spin_lock_irqsave(&port->port_lock, flags);
206 count = kfifo_in(&port->port.xmit_fifo, buf, count);
208 spin_unlock_irqrestore(&port->port_lock, flags);
213 static int dbc_tty_put_char(struct tty_struct *tty, u8 ch)
215 struct dbc_port *port = tty->driver_data;
219 spin_lock_irqsave(&port->port_lock, flags);
220 status = kfifo_put(&port->port.xmit_fifo, ch);
221 spin_unlock_irqrestore(&port->port_lock, flags);
226 static void dbc_tty_flush_chars(struct tty_struct *tty)
228 struct dbc_port *port = tty->driver_data;
231 spin_lock_irqsave(&port->port_lock, flags);
233 spin_unlock_irqrestore(&port->port_lock, flags);
236 static unsigned int dbc_tty_write_room(struct tty_struct *tty)
238 struct dbc_port *port = tty->driver_data;
242 spin_lock_irqsave(&port->port_lock, flags);
243 room = kfifo_avail(&port->port.xmit_fifo);
244 spin_unlock_irqrestore(&port->port_lock, flags);
249 static unsigned int dbc_tty_chars_in_buffer(struct tty_struct *tty)
251 struct dbc_port *port = tty->driver_data;
255 spin_lock_irqsave(&port->port_lock, flags);
256 chars = kfifo_len(&port->port.xmit_fifo);
257 spin_unlock_irqrestore(&port->port_lock, flags);
262 static void dbc_tty_unthrottle(struct tty_struct *tty)
264 struct dbc_port *port = tty->driver_data;
267 spin_lock_irqsave(&port->port_lock, flags);
268 tasklet_schedule(&port->push);
269 spin_unlock_irqrestore(&port->port_lock, flags);
272 static const struct tty_operations dbc_tty_ops = {
273 .install = dbc_tty_install,
274 .open = dbc_tty_open,
275 .close = dbc_tty_close,
276 .write = dbc_tty_write,
277 .put_char = dbc_tty_put_char,
278 .flush_chars = dbc_tty_flush_chars,
279 .write_room = dbc_tty_write_room,
280 .chars_in_buffer = dbc_tty_chars_in_buffer,
281 .unthrottle = dbc_tty_unthrottle,
284 static void dbc_rx_push(struct tasklet_struct *t)
286 struct dbc_request *req;
287 struct tty_struct *tty;
289 bool do_push = false;
290 bool disconnect = false;
291 struct dbc_port *port = from_tasklet(port, t, push);
292 struct list_head *queue = &port->read_queue;
294 spin_lock_irqsave(&port->port_lock, flags);
295 tty = port->port.tty;
296 while (!list_empty(queue)) {
297 req = list_first_entry(queue, struct dbc_request, list_pool);
299 if (tty && tty_throttled(tty))
302 switch (req->status) {
309 pr_warn("ttyDBC0: unexpected RX status %d\n",
315 char *packet = req->buf;
316 unsigned int n, size = req->actual;
325 count = tty_insert_flip_string(&port->port, packet,
330 port->n_read += count;
336 list_move_tail(&req->list_pool, &port->read_pool);
340 tty_flip_buffer_push(&port->port);
342 if (!list_empty(queue) && tty) {
343 if (!tty_throttled(tty)) {
345 tasklet_schedule(&port->push);
347 pr_warn("ttyDBC0: RX not scheduled?\n");
354 spin_unlock_irqrestore(&port->port_lock, flags);
357 static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty)
360 struct dbc_port *port = container_of(_port, struct dbc_port, port);
362 spin_lock_irqsave(&port->port_lock, flags);
364 spin_unlock_irqrestore(&port->port_lock, flags);
369 static const struct tty_port_operations dbc_port_ops = {
370 .activate = dbc_port_activate,
374 xhci_dbc_tty_init_port(struct xhci_dbc *dbc, struct dbc_port *port)
376 tty_port_init(&port->port);
377 spin_lock_init(&port->port_lock);
378 tasklet_setup(&port->push, dbc_rx_push);
379 INIT_LIST_HEAD(&port->read_pool);
380 INIT_LIST_HEAD(&port->read_queue);
381 INIT_LIST_HEAD(&port->write_pool);
383 port->port.ops = &dbc_port_ops;
388 xhci_dbc_tty_exit_port(struct dbc_port *port)
390 tasklet_kill(&port->push);
391 tty_port_destroy(&port->port);
394 static int xhci_dbc_tty_register_device(struct xhci_dbc *dbc)
397 struct device *tty_dev;
398 struct dbc_port *port = dbc_to_port(dbc);
400 if (port->registered)
403 xhci_dbc_tty_init_port(dbc, port);
405 mutex_lock(&dbc_tty_minors_lock);
406 port->minor = idr_alloc(&dbc_tty_minors, port, 0, 64, GFP_KERNEL);
407 mutex_unlock(&dbc_tty_minors_lock);
409 if (port->minor < 0) {
414 ret = kfifo_alloc(&port->port.xmit_fifo, DBC_WRITE_BUF_SIZE,
419 ret = xhci_dbc_alloc_requests(dbc, BULK_IN, &port->read_pool,
424 ret = xhci_dbc_alloc_requests(dbc, BULK_OUT, &port->write_pool,
427 goto err_free_requests;
429 tty_dev = tty_port_register_device(&port->port,
430 dbc_tty_driver, port->minor, NULL);
431 if (IS_ERR(tty_dev)) {
432 ret = PTR_ERR(tty_dev);
433 goto err_free_requests;
436 port->registered = true;
441 xhci_dbc_free_requests(&port->read_pool);
442 xhci_dbc_free_requests(&port->write_pool);
444 kfifo_free(&port->port.xmit_fifo);
446 idr_remove(&dbc_tty_minors, port->minor);
448 xhci_dbc_tty_exit_port(port);
450 dev_err(dbc->dev, "can't register tty port, err %d\n", ret);
455 static void xhci_dbc_tty_unregister_device(struct xhci_dbc *dbc)
457 struct dbc_port *port = dbc_to_port(dbc);
459 if (!port->registered)
461 tty_unregister_device(dbc_tty_driver, port->minor);
462 xhci_dbc_tty_exit_port(port);
463 port->registered = false;
465 mutex_lock(&dbc_tty_minors_lock);
466 idr_remove(&dbc_tty_minors, port->minor);
467 mutex_unlock(&dbc_tty_minors_lock);
469 kfifo_free(&port->port.xmit_fifo);
470 xhci_dbc_free_requests(&port->read_pool);
471 xhci_dbc_free_requests(&port->read_queue);
472 xhci_dbc_free_requests(&port->write_pool);
475 static const struct dbc_driver dbc_driver = {
476 .configure = xhci_dbc_tty_register_device,
477 .disconnect = xhci_dbc_tty_unregister_device,
480 int xhci_dbc_tty_probe(struct device *dev, void __iomem *base, struct xhci_hcd *xhci)
482 struct xhci_dbc *dbc;
483 struct dbc_port *port;
489 port = kzalloc(sizeof(*port), GFP_KERNEL);
493 dbc = xhci_alloc_dbc(dev, base, &dbc_driver);
502 /* get rid of xhci once this is a real driver binding to a device */
513 * undo what probe did, assume dbc is stopped already.
514 * we also assume tty_unregister_device() is called before this
516 void xhci_dbc_tty_remove(struct xhci_dbc *dbc)
518 struct dbc_port *port = dbc_to_port(dbc);
520 xhci_dbc_remove(dbc);
524 int dbc_tty_init(void)
528 idr_init(&dbc_tty_minors);
530 dbc_tty_driver = tty_alloc_driver(64, TTY_DRIVER_REAL_RAW |
531 TTY_DRIVER_DYNAMIC_DEV);
532 if (IS_ERR(dbc_tty_driver)) {
533 idr_destroy(&dbc_tty_minors);
534 return PTR_ERR(dbc_tty_driver);
537 dbc_tty_driver->driver_name = "dbc_serial";
538 dbc_tty_driver->name = "ttyDBC";
540 dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
541 dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
542 dbc_tty_driver->init_termios = tty_std_termios;
543 dbc_tty_driver->init_termios.c_cflag =
544 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
545 dbc_tty_driver->init_termios.c_ispeed = 9600;
546 dbc_tty_driver->init_termios.c_ospeed = 9600;
548 tty_set_operations(dbc_tty_driver, &dbc_tty_ops);
550 ret = tty_register_driver(dbc_tty_driver);
552 pr_err("Can't register dbc tty driver\n");
553 tty_driver_kref_put(dbc_tty_driver);
554 idr_destroy(&dbc_tty_minors);
560 void dbc_tty_exit(void)
562 if (dbc_tty_driver) {
563 tty_unregister_driver(dbc_tty_driver);
564 tty_driver_kref_put(dbc_tty_driver);
565 dbc_tty_driver = NULL;
568 idr_destroy(&dbc_tty_minors);