1 // SPDX-License-Identifier: GPL-2.0
3 * xhci-dbgtty.c - tty glue for xHCI debug capability
5 * Copyright (C) 2017 Intel Corporation
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
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_send_packet(struct dbc_port *port, char *packet, unsigned int size)
32 len = kfifo_len(&port->write_fifo);
36 size = kfifo_out(&port->write_fifo, packet, size);
40 static int dbc_start_tx(struct dbc_port *port)
41 __releases(&port->port_lock)
42 __acquires(&port->port_lock)
45 struct dbc_request *req;
47 bool do_tty_wake = false;
48 struct list_head *pool = &port->write_pool;
50 while (!list_empty(pool)) {
51 req = list_entry(pool->next, struct dbc_request, list_pool);
52 len = dbc_send_packet(port, req->buf, DBC_MAX_PACKET);
58 list_del(&req->list_pool);
60 spin_unlock(&port->port_lock);
61 status = dbc_ep_queue(req);
62 spin_lock(&port->port_lock);
65 list_add(&req->list_pool, pool);
70 if (do_tty_wake && port->port.tty)
71 tty_wakeup(port->port.tty);
76 static void dbc_start_rx(struct dbc_port *port)
77 __releases(&port->port_lock)
78 __acquires(&port->port_lock)
80 struct dbc_request *req;
82 struct list_head *pool = &port->read_pool;
84 while (!list_empty(pool)) {
88 req = list_entry(pool->next, struct dbc_request, list_pool);
89 list_del(&req->list_pool);
90 req->length = DBC_MAX_PACKET;
92 spin_unlock(&port->port_lock);
93 status = dbc_ep_queue(req);
94 spin_lock(&port->port_lock);
97 list_add(&req->list_pool, pool);
104 dbc_read_complete(struct xhci_dbc *dbc, struct dbc_request *req)
107 struct dbc_port *port = dbc_to_port(dbc);
109 spin_lock_irqsave(&port->port_lock, flags);
110 list_add_tail(&req->list_pool, &port->read_queue);
111 tasklet_schedule(&port->push);
112 spin_unlock_irqrestore(&port->port_lock, flags);
115 static void dbc_write_complete(struct xhci_dbc *dbc, struct dbc_request *req)
118 struct dbc_port *port = dbc_to_port(dbc);
120 spin_lock_irqsave(&port->port_lock, flags);
121 list_add(&req->list_pool, &port->write_pool);
122 switch (req->status) {
129 dev_warn(dbc->dev, "unexpected write complete status %d\n",
133 spin_unlock_irqrestore(&port->port_lock, flags);
136 static void xhci_dbc_free_req(struct dbc_request *req)
139 dbc_free_request(req);
143 xhci_dbc_alloc_requests(struct xhci_dbc *dbc, unsigned int direction,
144 struct list_head *head,
145 void (*fn)(struct xhci_dbc *, struct dbc_request *))
148 struct dbc_request *req;
150 for (i = 0; i < DBC_QUEUE_SIZE; i++) {
151 req = dbc_alloc_request(dbc, direction, GFP_KERNEL);
155 req->length = DBC_MAX_PACKET;
156 req->buf = kmalloc(req->length, GFP_KERNEL);
158 dbc_free_request(req);
163 list_add_tail(&req->list_pool, head);
166 return list_empty(head) ? -ENOMEM : 0;
170 xhci_dbc_free_requests(struct list_head *head)
172 struct dbc_request *req;
174 while (!list_empty(head)) {
175 req = list_entry(head->next, struct dbc_request, list_pool);
176 list_del(&req->list_pool);
177 xhci_dbc_free_req(req);
181 static int dbc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
183 struct dbc_port *port;
185 mutex_lock(&dbc_tty_minors_lock);
186 port = idr_find(&dbc_tty_minors, tty->index);
187 mutex_unlock(&dbc_tty_minors_lock);
192 tty->driver_data = port;
194 return tty_port_install(&port->port, driver, tty);
197 static int dbc_tty_open(struct tty_struct *tty, struct file *file)
199 struct dbc_port *port = tty->driver_data;
201 return tty_port_open(&port->port, tty, file);
204 static void dbc_tty_close(struct tty_struct *tty, struct file *file)
206 struct dbc_port *port = tty->driver_data;
208 tty_port_close(&port->port, tty, file);
211 static int dbc_tty_write(struct tty_struct *tty,
212 const unsigned char *buf,
215 struct dbc_port *port = tty->driver_data;
218 spin_lock_irqsave(&port->port_lock, flags);
220 count = kfifo_in(&port->write_fifo, buf, count);
222 spin_unlock_irqrestore(&port->port_lock, flags);
227 static int dbc_tty_put_char(struct tty_struct *tty, unsigned char ch)
229 struct dbc_port *port = tty->driver_data;
233 spin_lock_irqsave(&port->port_lock, flags);
234 status = kfifo_put(&port->write_fifo, ch);
235 spin_unlock_irqrestore(&port->port_lock, flags);
240 static void dbc_tty_flush_chars(struct tty_struct *tty)
242 struct dbc_port *port = tty->driver_data;
245 spin_lock_irqsave(&port->port_lock, flags);
247 spin_unlock_irqrestore(&port->port_lock, flags);
250 static unsigned int dbc_tty_write_room(struct tty_struct *tty)
252 struct dbc_port *port = tty->driver_data;
256 spin_lock_irqsave(&port->port_lock, flags);
257 room = kfifo_avail(&port->write_fifo);
258 spin_unlock_irqrestore(&port->port_lock, flags);
263 static unsigned int dbc_tty_chars_in_buffer(struct tty_struct *tty)
265 struct dbc_port *port = tty->driver_data;
269 spin_lock_irqsave(&port->port_lock, flags);
270 chars = kfifo_len(&port->write_fifo);
271 spin_unlock_irqrestore(&port->port_lock, flags);
276 static void dbc_tty_unthrottle(struct tty_struct *tty)
278 struct dbc_port *port = tty->driver_data;
281 spin_lock_irqsave(&port->port_lock, flags);
282 tasklet_schedule(&port->push);
283 spin_unlock_irqrestore(&port->port_lock, flags);
286 static const struct tty_operations dbc_tty_ops = {
287 .install = dbc_tty_install,
288 .open = dbc_tty_open,
289 .close = dbc_tty_close,
290 .write = dbc_tty_write,
291 .put_char = dbc_tty_put_char,
292 .flush_chars = dbc_tty_flush_chars,
293 .write_room = dbc_tty_write_room,
294 .chars_in_buffer = dbc_tty_chars_in_buffer,
295 .unthrottle = dbc_tty_unthrottle,
298 static void dbc_rx_push(struct tasklet_struct *t)
300 struct dbc_request *req;
301 struct tty_struct *tty;
303 bool do_push = false;
304 bool disconnect = false;
305 struct dbc_port *port = from_tasklet(port, t, push);
306 struct list_head *queue = &port->read_queue;
308 spin_lock_irqsave(&port->port_lock, flags);
309 tty = port->port.tty;
310 while (!list_empty(queue)) {
311 req = list_first_entry(queue, struct dbc_request, list_pool);
313 if (tty && tty_throttled(tty))
316 switch (req->status) {
323 pr_warn("ttyDBC0: unexpected RX status %d\n",
329 char *packet = req->buf;
330 unsigned int n, size = req->actual;
339 count = tty_insert_flip_string(&port->port, packet,
344 port->n_read += count;
350 list_move(&req->list_pool, &port->read_pool);
354 tty_flip_buffer_push(&port->port);
356 if (!list_empty(queue) && tty) {
357 if (!tty_throttled(tty)) {
359 tasklet_schedule(&port->push);
361 pr_warn("ttyDBC0: RX not scheduled?\n");
368 spin_unlock_irqrestore(&port->port_lock, flags);
371 static int dbc_port_activate(struct tty_port *_port, struct tty_struct *tty)
374 struct dbc_port *port = container_of(_port, struct dbc_port, port);
376 spin_lock_irqsave(&port->port_lock, flags);
378 spin_unlock_irqrestore(&port->port_lock, flags);
383 static const struct tty_port_operations dbc_port_ops = {
384 .activate = dbc_port_activate,
388 xhci_dbc_tty_init_port(struct xhci_dbc *dbc, struct dbc_port *port)
390 tty_port_init(&port->port);
391 spin_lock_init(&port->port_lock);
392 tasklet_setup(&port->push, dbc_rx_push);
393 INIT_LIST_HEAD(&port->read_pool);
394 INIT_LIST_HEAD(&port->read_queue);
395 INIT_LIST_HEAD(&port->write_pool);
397 port->port.ops = &dbc_port_ops;
402 xhci_dbc_tty_exit_port(struct dbc_port *port)
404 tasklet_kill(&port->push);
405 tty_port_destroy(&port->port);
408 static int xhci_dbc_tty_register_device(struct xhci_dbc *dbc)
411 struct device *tty_dev;
412 struct dbc_port *port = dbc_to_port(dbc);
414 if (port->registered)
417 xhci_dbc_tty_init_port(dbc, port);
419 mutex_lock(&dbc_tty_minors_lock);
420 port->minor = idr_alloc(&dbc_tty_minors, port, 0, 64, GFP_KERNEL);
421 mutex_unlock(&dbc_tty_minors_lock);
423 if (port->minor < 0) {
428 ret = kfifo_alloc(&port->write_fifo, DBC_WRITE_BUF_SIZE, GFP_KERNEL);
432 ret = xhci_dbc_alloc_requests(dbc, BULK_IN, &port->read_pool,
437 ret = xhci_dbc_alloc_requests(dbc, BULK_OUT, &port->write_pool,
440 goto err_free_requests;
442 tty_dev = tty_port_register_device(&port->port,
443 dbc_tty_driver, port->minor, NULL);
444 if (IS_ERR(tty_dev)) {
445 ret = PTR_ERR(tty_dev);
446 goto err_free_requests;
449 port->registered = true;
454 xhci_dbc_free_requests(&port->read_pool);
455 xhci_dbc_free_requests(&port->write_pool);
457 kfifo_free(&port->write_fifo);
459 idr_remove(&dbc_tty_minors, port->minor);
461 xhci_dbc_tty_exit_port(port);
463 dev_err(dbc->dev, "can't register tty port, err %d\n", ret);
468 static void xhci_dbc_tty_unregister_device(struct xhci_dbc *dbc)
470 struct dbc_port *port = dbc_to_port(dbc);
472 if (!port->registered)
474 tty_unregister_device(dbc_tty_driver, port->minor);
475 xhci_dbc_tty_exit_port(port);
476 port->registered = false;
478 mutex_lock(&dbc_tty_minors_lock);
479 idr_remove(&dbc_tty_minors, port->minor);
480 mutex_unlock(&dbc_tty_minors_lock);
482 kfifo_free(&port->write_fifo);
483 xhci_dbc_free_requests(&port->read_pool);
484 xhci_dbc_free_requests(&port->read_queue);
485 xhci_dbc_free_requests(&port->write_pool);
488 static const struct dbc_driver dbc_driver = {
489 .configure = xhci_dbc_tty_register_device,
490 .disconnect = xhci_dbc_tty_unregister_device,
493 int xhci_dbc_tty_probe(struct device *dev, void __iomem *base, struct xhci_hcd *xhci)
495 struct xhci_dbc *dbc;
496 struct dbc_port *port;
502 port = kzalloc(sizeof(*port), GFP_KERNEL);
506 dbc = xhci_alloc_dbc(dev, base, &dbc_driver);
515 /* get rid of xhci once this is a real driver binding to a device */
526 * undo what probe did, assume dbc is stopped already.
527 * we also assume tty_unregister_device() is called before this
529 void xhci_dbc_tty_remove(struct xhci_dbc *dbc)
531 struct dbc_port *port = dbc_to_port(dbc);
533 xhci_dbc_remove(dbc);
537 int dbc_tty_init(void)
541 idr_init(&dbc_tty_minors);
543 dbc_tty_driver = tty_alloc_driver(64, TTY_DRIVER_REAL_RAW |
544 TTY_DRIVER_DYNAMIC_DEV);
545 if (IS_ERR(dbc_tty_driver)) {
546 idr_destroy(&dbc_tty_minors);
547 return PTR_ERR(dbc_tty_driver);
550 dbc_tty_driver->driver_name = "dbc_serial";
551 dbc_tty_driver->name = "ttyDBC";
553 dbc_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
554 dbc_tty_driver->subtype = SERIAL_TYPE_NORMAL;
555 dbc_tty_driver->init_termios = tty_std_termios;
556 dbc_tty_driver->init_termios.c_cflag =
557 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
558 dbc_tty_driver->init_termios.c_ispeed = 9600;
559 dbc_tty_driver->init_termios.c_ospeed = 9600;
561 tty_set_operations(dbc_tty_driver, &dbc_tty_ops);
563 ret = tty_register_driver(dbc_tty_driver);
565 pr_err("Can't register dbc tty driver\n");
566 tty_driver_kref_put(dbc_tty_driver);
567 idr_destroy(&dbc_tty_minors);
573 void dbc_tty_exit(void)
575 if (dbc_tty_driver) {
576 tty_unregister_driver(dbc_tty_driver);
577 tty_driver_kref_put(dbc_tty_driver);
578 dbc_tty_driver = NULL;
581 idr_destroy(&dbc_tty_minors);