2 * UART driver for the Greybus "generic" UART module.
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
9 * Heavily based on drivers/usb/class/cdc-acm.c and
10 * drivers/usb/serial/usb-serial.c.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <linux/mutex.h>
22 #include <linux/tty.h>
23 #include <linux/serial.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/serial.h>
27 #include <linux/idr.h>
29 #include <linux/kdev_t.h>
34 #define GB_NUM_MINORS 16 /* 16 is is more than enough */
35 #define GB_NAME "ttyGB"
37 struct gb_tty_line_coding {
46 struct gbphy_device *gbphy_dev;
49 size_t buffer_payload_max;
50 struct gb_connection *connection;
56 spinlock_t write_lock;
57 struct async_icount iocount;
58 struct async_icount oldcount;
59 wait_queue_head_t wioctl;
61 u8 ctrlin; /* input control lines */
62 u8 ctrlout; /* output control lines */
63 struct gb_tty_line_coding line_coding;
66 static struct tty_driver *gb_tty_driver;
67 static DEFINE_IDR(tty_minors);
68 static DEFINE_MUTEX(table_lock);
70 static int gb_uart_receive_data_handler(struct gb_operation *op)
72 struct gb_connection *connection = op->connection;
73 struct gb_tty *gb_tty = gb_connection_get_data(connection);
74 struct tty_port *port = &gb_tty->port;
75 struct gb_message *request = op->request;
76 struct gb_uart_recv_data_request *receive_data;
79 unsigned long tty_flags = TTY_NORMAL;
81 if (request->payload_size < sizeof(*receive_data)) {
82 dev_err(&gb_tty->gbphy_dev->dev,
83 "short receive-data request received (%zu < %zu)\n",
84 request->payload_size, sizeof(*receive_data));
88 receive_data = op->request->payload;
89 recv_data_size = le16_to_cpu(receive_data->size);
91 if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
92 dev_err(&gb_tty->gbphy_dev->dev,
93 "malformed receive-data request received (%u != %zu)\n",
95 request->payload_size - sizeof(*receive_data));
102 if (receive_data->flags) {
103 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
104 tty_flags = TTY_BREAK;
105 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
106 tty_flags = TTY_PARITY;
107 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
108 tty_flags = TTY_FRAME;
110 /* overrun is special, not associated with a char */
111 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
112 tty_insert_flip_char(port, 0, TTY_OVERRUN);
114 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
115 tty_flags, recv_data_size);
116 if (count != recv_data_size) {
117 dev_err(&gb_tty->gbphy_dev->dev,
118 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
119 recv_data_size, count);
122 tty_flip_buffer_push(port);
126 static int gb_uart_serial_state_handler(struct gb_operation *op)
128 struct gb_connection *connection = op->connection;
129 struct gb_tty *gb_tty = gb_connection_get_data(connection);
130 struct gb_message *request = op->request;
131 struct gb_uart_serial_state_request *serial_state;
133 if (request->payload_size < sizeof(*serial_state)) {
134 dev_err(&gb_tty->gbphy_dev->dev,
135 "short serial-state event received (%zu < %zu)\n",
136 request->payload_size, sizeof(*serial_state));
140 serial_state = request->payload;
141 gb_tty->ctrlin = serial_state->control;
146 static int gb_uart_request_handler(struct gb_operation *op)
148 struct gb_connection *connection = op->connection;
149 struct gb_tty *gb_tty = gb_connection_get_data(connection);
154 case GB_UART_TYPE_RECEIVE_DATA:
155 ret = gb_uart_receive_data_handler(op);
157 case GB_UART_TYPE_SERIAL_STATE:
158 ret = gb_uart_serial_state_handler(op);
161 dev_err(&gb_tty->gbphy_dev->dev,
162 "unsupported unsolicited request: 0x%02x\n", type);
169 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
171 struct gb_uart_send_data_request *request;
177 if (size > tty->buffer_payload_max)
178 size = tty->buffer_payload_max;
179 request = tty->buffer;
180 request->size = cpu_to_le16(size);
181 memcpy(&request->data[0], data, size);
182 ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
183 request, sizeof(*request) + size, NULL, 0);
190 static int send_line_coding(struct gb_tty *tty)
192 struct gb_uart_set_line_coding_request request;
194 memcpy(&request, &tty->line_coding,
195 sizeof(tty->line_coding));
196 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
197 &request, sizeof(request), NULL, 0);
200 static int send_control(struct gb_tty *gb_tty, u8 control)
202 struct gb_uart_set_control_line_state_request request;
204 request.control = control;
205 return gb_operation_sync(gb_tty->connection,
206 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
207 &request, sizeof(request), NULL, 0);
210 static int send_break(struct gb_tty *gb_tty, u8 state)
212 struct gb_uart_set_break_request request;
214 if ((state != 0) && (state != 1)) {
215 dev_err(&gb_tty->gbphy_dev->dev,
216 "invalid break state of %d\n", state);
220 request.state = state;
221 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
222 &request, sizeof(request), NULL, 0);
226 static struct gb_tty *get_gb_by_minor(unsigned minor)
228 struct gb_tty *gb_tty;
230 mutex_lock(&table_lock);
231 gb_tty = idr_find(&tty_minors, minor);
233 mutex_lock(&gb_tty->mutex);
234 if (gb_tty->disconnected) {
235 mutex_unlock(&gb_tty->mutex);
238 tty_port_get(&gb_tty->port);
239 mutex_unlock(&gb_tty->mutex);
242 mutex_unlock(&table_lock);
246 static int alloc_minor(struct gb_tty *gb_tty)
250 mutex_lock(&table_lock);
251 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
252 mutex_unlock(&table_lock);
254 gb_tty->minor = minor;
258 static void release_minor(struct gb_tty *gb_tty)
260 int minor = gb_tty->minor;
262 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
263 mutex_lock(&table_lock);
264 idr_remove(&tty_minors, minor);
265 mutex_unlock(&table_lock);
268 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
270 struct gb_tty *gb_tty;
273 gb_tty = get_gb_by_minor(tty->index);
277 retval = tty_standard_install(driver, tty);
281 tty->driver_data = gb_tty;
284 tty_port_put(&gb_tty->port);
288 static int gb_tty_open(struct tty_struct *tty, struct file *file)
290 struct gb_tty *gb_tty = tty->driver_data;
292 return tty_port_open(&gb_tty->port, tty, file);
295 static void gb_tty_close(struct tty_struct *tty, struct file *file)
297 struct gb_tty *gb_tty = tty->driver_data;
299 tty_port_close(&gb_tty->port, tty, file);
302 static void gb_tty_cleanup(struct tty_struct *tty)
304 struct gb_tty *gb_tty = tty->driver_data;
306 tty_port_put(&gb_tty->port);
309 static void gb_tty_hangup(struct tty_struct *tty)
311 struct gb_tty *gb_tty = tty->driver_data;
313 tty_port_hangup(&gb_tty->port);
316 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
319 struct gb_tty *gb_tty = tty->driver_data;
321 return send_data(gb_tty, count, buf);
324 static int gb_tty_write_room(struct tty_struct *tty)
326 struct gb_tty *gb_tty = tty->driver_data;
328 return gb_tty->buffer_payload_max;
331 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
336 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
338 struct gb_tty *gb_tty = tty->driver_data;
340 return send_break(gb_tty, state ? 1 : 0);
343 static void gb_tty_set_termios(struct tty_struct *tty,
344 struct ktermios *termios_old)
346 struct gb_tty *gb_tty = tty->driver_data;
347 struct ktermios *termios = &tty->termios;
348 struct gb_tty_line_coding newline;
349 u8 newctrl = gb_tty->ctrlout;
351 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
352 newline.format = termios->c_cflag & CSTOPB ?
353 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
354 newline.parity = termios->c_cflag & PARENB ?
355 (termios->c_cflag & PARODD ? 1 : 2) +
356 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
358 switch (termios->c_cflag & CSIZE) {
360 newline.data_bits = 5;
363 newline.data_bits = 6;
366 newline.data_bits = 7;
370 newline.data_bits = 8;
374 /* FIXME: needs to clear unsupported bits in the termios */
375 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
377 if (C_BAUD(tty) == B0) {
378 newline.rate = gb_tty->line_coding.rate;
379 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
380 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
381 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
384 if (newctrl != gb_tty->ctrlout) {
385 gb_tty->ctrlout = newctrl;
386 send_control(gb_tty, newctrl);
389 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
390 newline.flow_control |= GB_SERIAL_AUTO_RTSCTS_EN;
392 newline.flow_control &= ~GB_SERIAL_AUTO_RTSCTS_EN;
394 if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
395 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
396 send_line_coding(gb_tty);
400 static int gb_tty_tiocmget(struct tty_struct *tty)
402 struct gb_tty *gb_tty = tty->driver_data;
404 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
405 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
406 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
407 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
408 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
412 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
415 struct gb_tty *gb_tty = tty->driver_data;
416 u8 newctrl = gb_tty->ctrlout;
418 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
419 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
420 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
421 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
423 newctrl = (newctrl & ~clear) | set;
424 if (gb_tty->ctrlout == newctrl)
427 gb_tty->ctrlout = newctrl;
428 return send_control(gb_tty, newctrl);
431 static void gb_tty_throttle(struct tty_struct *tty)
433 struct gb_tty *gb_tty = tty->driver_data;
434 unsigned char stop_char;
438 stop_char = STOP_CHAR(tty);
439 retval = gb_tty_write(tty, &stop_char, 1);
444 if (tty->termios.c_cflag & CRTSCTS) {
445 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
446 retval = send_control(gb_tty, gb_tty->ctrlout);
451 static void gb_tty_unthrottle(struct tty_struct *tty)
453 struct gb_tty *gb_tty = tty->driver_data;
454 unsigned char start_char;
458 start_char = START_CHAR(tty);
459 retval = gb_tty_write(tty, &start_char, 1);
464 if (tty->termios.c_cflag & CRTSCTS) {
465 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
466 retval = send_control(gb_tty, gb_tty->ctrlout);
470 static int get_serial_info(struct gb_tty *gb_tty,
471 struct serial_struct __user *info)
473 struct serial_struct tmp;
478 memset(&tmp, 0, sizeof(tmp));
479 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
480 tmp.type = PORT_16550A;
481 tmp.line = gb_tty->minor;
482 tmp.xmit_fifo_size = 16;
483 tmp.baud_base = 9600;
484 tmp.close_delay = gb_tty->port.close_delay / 10;
485 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
486 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
488 if (copy_to_user(info, &tmp, sizeof(tmp)))
493 static int set_serial_info(struct gb_tty *gb_tty,
494 struct serial_struct __user *newinfo)
496 struct serial_struct new_serial;
497 unsigned int closing_wait;
498 unsigned int close_delay;
501 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
504 close_delay = new_serial.close_delay * 10;
505 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
506 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
508 mutex_lock(&gb_tty->port.mutex);
509 if (!capable(CAP_SYS_ADMIN)) {
510 if ((close_delay != gb_tty->port.close_delay) ||
511 (closing_wait != gb_tty->port.closing_wait))
514 retval = -EOPNOTSUPP;
516 gb_tty->port.close_delay = close_delay;
517 gb_tty->port.closing_wait = closing_wait;
519 mutex_unlock(&gb_tty->port.mutex);
523 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
526 DECLARE_WAITQUEUE(wait, current);
527 struct async_icount old;
528 struct async_icount new;
530 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
534 spin_lock_irq(&gb_tty->read_lock);
535 old = gb_tty->oldcount;
536 new = gb_tty->iocount;
537 gb_tty->oldcount = new;
538 spin_unlock_irq(&gb_tty->read_lock);
540 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
542 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
544 if ((arg & TIOCM_RI) && (old.rng != new.rng))
547 add_wait_queue(&gb_tty->wioctl, &wait);
548 set_current_state(TASK_INTERRUPTIBLE);
550 remove_wait_queue(&gb_tty->wioctl, &wait);
551 if (gb_tty->disconnected) {
555 } else if (signal_pending(current)) {
556 retval = -ERESTARTSYS;
564 static int get_serial_usage(struct gb_tty *gb_tty,
565 struct serial_icounter_struct __user *count)
567 struct serial_icounter_struct icount;
570 memset(&icount, 0, sizeof(icount));
571 icount.dsr = gb_tty->iocount.dsr;
572 icount.rng = gb_tty->iocount.rng;
573 icount.dcd = gb_tty->iocount.dcd;
574 icount.frame = gb_tty->iocount.frame;
575 icount.overrun = gb_tty->iocount.overrun;
576 icount.parity = gb_tty->iocount.parity;
577 icount.brk = gb_tty->iocount.brk;
579 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
585 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
588 struct gb_tty *gb_tty = tty->driver_data;
592 return get_serial_info(gb_tty,
593 (struct serial_struct __user *)arg);
595 return set_serial_info(gb_tty,
596 (struct serial_struct __user *)arg);
598 return wait_serial_change(gb_tty, arg);
600 return get_serial_usage(gb_tty,
601 (struct serial_icounter_struct __user *)arg);
607 static void gb_tty_dtr_rts(struct tty_port *port, int on)
609 struct gb_tty *gb_tty;
612 gb_tty = container_of(port, struct gb_tty, port);
613 newctrl = gb_tty->ctrlout;
616 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
618 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
620 gb_tty->ctrlout = newctrl;
621 send_control(gb_tty, newctrl);
624 static const struct tty_operations gb_ops = {
625 .install = gb_tty_install,
627 .close = gb_tty_close,
628 .cleanup = gb_tty_cleanup,
629 .hangup = gb_tty_hangup,
630 .write = gb_tty_write,
631 .write_room = gb_tty_write_room,
632 .ioctl = gb_tty_ioctl,
633 .throttle = gb_tty_throttle,
634 .unthrottle = gb_tty_unthrottle,
635 .chars_in_buffer = gb_tty_chars_in_buffer,
636 .break_ctl = gb_tty_break_ctl,
637 .set_termios = gb_tty_set_termios,
638 .tiocmget = gb_tty_tiocmget,
639 .tiocmset = gb_tty_tiocmset,
642 static struct tty_port_operations gb_port_ops = {
643 .dtr_rts = gb_tty_dtr_rts,
646 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
647 const struct gbphy_device_id *id)
649 struct gb_connection *connection;
651 struct gb_tty *gb_tty;
652 struct device *tty_dev;
656 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
660 connection = gb_connection_create(gbphy_dev->bundle,
661 le16_to_cpu(gbphy_dev->cport_desc->id),
662 gb_uart_request_handler);
663 if (IS_ERR(connection)) {
664 retval = PTR_ERR(connection);
668 max_payload = gb_operation_get_payload_size_max(connection);
669 if (max_payload < sizeof(struct gb_uart_send_data_request)) {
671 goto exit_connection_destroy;
674 gb_tty->buffer_payload_max = max_payload -
675 sizeof(struct gb_uart_send_data_request);
677 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
678 if (!gb_tty->buffer) {
680 goto exit_connection_destroy;
683 minor = alloc_minor(gb_tty);
685 if (minor == -ENOSPC) {
686 dev_err(&connection->bundle->dev,
687 "no more free minor numbers\n");
695 gb_tty->minor = minor;
696 spin_lock_init(&gb_tty->write_lock);
697 spin_lock_init(&gb_tty->read_lock);
698 init_waitqueue_head(&gb_tty->wioctl);
699 mutex_init(&gb_tty->mutex);
701 tty_port_init(&gb_tty->port);
702 gb_tty->port.ops = &gb_port_ops;
704 gb_tty->connection = connection;
705 gb_tty->gbphy_dev = gbphy_dev;
706 gb_connection_set_data(connection, gb_tty);
707 gb_gbphy_set_data(gbphy_dev, gb_tty);
709 retval = gb_connection_enable_tx(connection);
711 goto exit_release_minor;
713 retval = gb_gbphy_get_version(connection);
715 goto exit_connection_disable;
717 send_control(gb_tty, gb_tty->ctrlout);
719 /* initialize the uart to be 9600n81 */
720 gb_tty->line_coding.rate = cpu_to_le32(9600);
721 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
722 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
723 gb_tty->line_coding.data_bits = 8;
724 send_line_coding(gb_tty);
726 retval = gb_connection_enable(connection);
728 goto exit_connection_disable;
730 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
732 if (IS_ERR(tty_dev)) {
733 retval = PTR_ERR(tty_dev);
734 goto exit_connection_disable;
740 exit_connection_disable:
741 gb_connection_disable(connection);
743 release_minor(gb_tty);
745 kfree(gb_tty->buffer);
746 exit_connection_destroy:
747 gb_connection_destroy(connection);
754 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
756 struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
757 struct gb_connection *connection = gb_tty->connection;
758 struct tty_struct *tty;
760 mutex_lock(&gb_tty->mutex);
761 gb_tty->disconnected = true;
763 wake_up_all(&gb_tty->wioctl);
764 mutex_unlock(&gb_tty->mutex);
766 tty = tty_port_tty_get(&gb_tty->port);
772 gb_connection_disable_rx(connection);
773 tty_unregister_device(gb_tty_driver, gb_tty->minor);
775 /* FIXME - free transmit / receive buffers */
777 gb_connection_disable(connection);
778 tty_port_destroy(&gb_tty->port);
779 gb_connection_destroy(connection);
780 kfree(gb_tty->buffer);
784 static int gb_tty_init(void)
788 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
789 if (IS_ERR(gb_tty_driver)) {
790 pr_err("Can not allocate tty driver\n");
792 goto fail_unregister_dev;
795 gb_tty_driver->driver_name = "gb";
796 gb_tty_driver->name = GB_NAME;
797 gb_tty_driver->major = 0;
798 gb_tty_driver->minor_start = 0;
799 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
800 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
801 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
802 gb_tty_driver->init_termios = tty_std_termios;
803 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
804 tty_set_operations(gb_tty_driver, &gb_ops);
806 retval = tty_register_driver(gb_tty_driver);
808 pr_err("Can not register tty driver: %d\n", retval);
809 goto fail_put_gb_tty;
815 put_tty_driver(gb_tty_driver);
820 static void gb_tty_exit(void)
822 tty_unregister_driver(gb_tty_driver);
823 put_tty_driver(gb_tty_driver);
824 idr_destroy(&tty_minors);
827 static const struct gbphy_device_id gb_uart_id_table[] = {
828 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
831 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
833 static struct gbphy_driver uart_driver = {
835 .probe = gb_uart_probe,
836 .remove = gb_uart_remove,
837 .id_table = gb_uart_id_table,
840 static int gb_uart_driver_init(void)
848 ret = gb_gbphy_register(&uart_driver);
856 module_init(gb_uart_driver_init);
858 static void gb_uart_driver_exit(void)
860 gb_gbphy_deregister(&uart_driver);
864 module_exit(gb_uart_driver_exit);
865 MODULE_LICENSE("GPL v2");