2 * UART driver for the Greybus "generic" UART module.
4 * Copyright 2014 Google Inc.
6 * Released under the GPLv2 only.
8 * Heavily based on drivers/usb/class/cdc-acm.c and
9 * drivers/usb/serial/usb-serial.c.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/wait.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 #include <linux/mutex.h>
21 #include <linux/tty.h>
22 #include <linux/serial.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/idr.h>
28 #include <linux/kdev_t.h>
33 #define GB_NUM_MINORS 255 /* 255 is enough for anyone... */
34 #define GB_NAME "ttyGB"
36 /* Version of the Greybus PWM protocol we support */
37 #define GB_UART_VERSION_MAJOR 0x00
38 #define GB_UART_VERSION_MINOR 0x01
40 /* Greybus UART request types */
41 #define GB_UART_REQ_INVALID 0x00
42 #define GB_UART_REQ_PROTOCOL_VERSION 0x01
43 #define GB_UART_REQ_SEND_DATA 0x02
44 #define GB_UART_REQ_RECEIVE_DATA 0x03 /* Unsolicited data */
45 #define GB_UART_REQ_SET_LINE_CODING 0x04
46 #define GB_UART_REQ_SET_CONTROL_LINE_STATE 0x05
47 #define GB_UART_REQ_SET_BREAK 0x06
48 #define GB_UART_REQ_SERIAL_STATE 0x07 /* Unsolicited data */
49 #define GB_UART_TYPE_RESPONSE 0x80 /* OR'd with rest */
51 struct gb_uart_proto_version_response {
56 struct gb_uart_send_data_request {
61 struct gb_serial_line_coding {
64 #define GB_SERIAL_1_STOP_BITS 0
65 #define GB_SERIAL_1_5_STOP_BITS 1
66 #define GB_SERIAL_2_STOP_BITS 2
69 #define GB_SERIAL_NO_PARITY 0
70 #define GB_SERIAL_ODD_PARITY 1
71 #define GB_SERIAL_EVEN_PARITY 2
72 #define GB_SERIAL_MARK_PARITY 3
73 #define GB_SERIAL_SPACE_PARITY 4
76 } __attribute__ ((packed));
78 struct gb_uart_set_line_coding_request {
79 struct gb_serial_line_coding line_coding;
82 /* output control lines */
83 #define GB_UART_CTRL_DTR 0x01
84 #define GB_UART_CTRL_RTS 0x02
86 struct gb_uart_set_control_line_state_request {
90 struct gb_uart_set_break_request {
94 /* input control lines and line errors */
95 #define GB_UART_CTRL_DCD 0x01
96 #define GB_UART_CTRL_DSR 0x02
97 #define GB_UART_CTRL_BRK 0x04
98 #define GB_UART_CTRL_RI 0x08
100 #define GB_UART_CTRL_FRAMING 0x10
101 #define GB_UART_CTRL_PARITY 0x20
102 #define GB_UART_CTRL_OVERRUN 0x40
104 struct gb_uart_serial_state_request {
109 struct tty_port port;
110 struct gb_connection *connection;
113 unsigned char clocal;
115 spinlock_t read_lock;
116 spinlock_t write_lock;
117 struct async_icount iocount;
118 struct async_icount oldcount;
119 wait_queue_head_t wioctl;
123 unsigned int ctrlin; /* input control lines */
124 unsigned int ctrlout; /* output control lines */
125 struct gb_serial_line_coding line_coding;
129 static struct tty_driver *gb_tty_driver;
130 static DEFINE_IDR(tty_minors);
131 static DEFINE_MUTEX(table_lock);
132 static atomic_t reference_count = ATOMIC_INIT(0);
135 * This request only uses the connection field, and if successful,
136 * fills in the major and minor protocol version of the target.
138 static int get_version(struct gb_tty *tty)
140 struct gb_operation *operation;
141 struct gb_uart_proto_version_response *response;
144 operation = gb_operation_create(tty->connection,
145 GB_UART_REQ_PROTOCOL_VERSION,
146 0, sizeof(*response));
150 /* Synchronous operation--no callback */
151 ret = gb_operation_request_send(operation, NULL);
153 pr_err("version operation failed (%d)\n", ret);
157 if (operation->result) {
158 ret = gb_operation_status_map(operation->result);
159 gb_connection_err(tty->connection, "result %hhu",
162 response = operation->response->payload;
163 if (response->major > GB_UART_VERSION_MAJOR) {
164 pr_err("unsupported major version (%hhu > %hhu)\n",
165 response->major, GB_UART_VERSION_MAJOR);
169 tty->version_major = response->major;
170 tty->version_minor = response->minor;
172 pr_debug("%s: version_major = %u version_minor = %u\n",
173 __func__, tty->version_major, tty->version_minor);
176 gb_operation_destroy(operation);
181 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
183 struct gb_connection *connection = tty->connection;
184 struct gb_operation *operation;
185 struct gb_uart_send_data_request *request;
191 operation = gb_operation_create(connection, GB_UART_REQ_SEND_DATA,
192 sizeof(*request) + size, 0);
195 request = operation->request->payload;
196 request->size = cpu_to_le16(size);
197 memcpy(&request->data[0], data, size);
199 /* Synchronous operation--no callback */
200 retval = gb_operation_request_send(operation, NULL);
202 dev_err(&connection->dev,
203 "send data operation failed (%d)\n", retval);
207 if (operation->result) {
208 retval = gb_operation_status_map(operation->result);
209 gb_connection_err(connection, "send data result %hhu",
213 gb_operation_destroy(operation);
218 static int send_line_coding(struct gb_tty *tty,
219 struct gb_serial_line_coding *line_coding)
221 struct gb_connection *connection = tty->connection;
222 struct gb_operation *operation;
223 struct gb_uart_set_line_coding_request *request;
226 operation = gb_operation_create(connection, GB_UART_REQ_SET_LINE_CODING,
227 sizeof(*request), 0);
230 request = operation->request->payload;
231 memcpy(&request->line_coding, line_coding, sizeof(*line_coding));
233 /* Synchronous operation--no callback */
234 retval = gb_operation_request_send(operation, NULL);
236 dev_err(&connection->dev,
237 "send line coding operation failed (%d)\n", retval);
241 if (operation->result) {
242 retval = gb_operation_status_map(operation->result);
243 gb_connection_err(connection, "send line coding result %hhu",
247 gb_operation_destroy(operation);
252 static int send_control(struct gb_tty *tty, u16 control)
254 struct gb_connection *connection = tty->connection;
255 struct gb_operation *operation;
256 struct gb_uart_set_control_line_state_request *request;
259 operation = gb_operation_create(connection,
260 GB_UART_REQ_SET_CONTROL_LINE_STATE,
261 sizeof(*request), 0);
264 request = operation->request->payload;
265 request->control = cpu_to_le16(control);
267 /* Synchronous operation--no callback */
268 retval = gb_operation_request_send(operation, NULL);
270 dev_err(&connection->dev,
271 "send control operation failed (%d)\n", retval);
275 if (operation->result) {
276 retval = gb_operation_status_map(operation->result);
277 gb_connection_err(connection, "send control result %hhu",
281 gb_operation_destroy(operation);
286 static int send_break(struct gb_tty *tty, u8 state)
288 struct gb_connection *connection = tty->connection;
289 struct gb_operation *operation;
290 struct gb_uart_set_break_request *request;
293 if ((state != 0) && (state != 1)) {
294 dev_err(&connection->dev, "invalid break state of %d\n", state);
298 operation = gb_operation_create(connection, GB_UART_REQ_SET_BREAK,
299 sizeof(*request), 0);
302 request = operation->request->payload;
303 request->state = state;
305 /* Synchronous operation--no callback */
306 retval = gb_operation_request_send(operation, NULL);
308 dev_err(&connection->dev,
309 "send break operation failed (%d)\n", retval);
313 if (operation->result) {
314 retval = gb_operation_status_map(operation->result);
315 gb_connection_err(connection, "send break result %hhu",
319 gb_operation_destroy(operation);
325 static struct gb_tty *get_gb_by_minor(unsigned minor)
327 struct gb_tty *gb_tty;
329 mutex_lock(&table_lock);
330 gb_tty = idr_find(&tty_minors, minor);
332 mutex_lock(&gb_tty->mutex);
333 if (gb_tty->disconnected) {
334 mutex_unlock(&gb_tty->mutex);
337 tty_port_get(&gb_tty->port);
338 mutex_unlock(&gb_tty->mutex);
341 mutex_unlock(&table_lock);
345 static int alloc_minor(struct gb_tty *gb_tty)
349 mutex_lock(&table_lock);
350 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
351 mutex_unlock(&table_lock);
353 gb_tty->minor = minor;
357 static void release_minor(struct gb_tty *gb_tty)
359 int minor = gb_tty->minor;
361 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
362 mutex_lock(&table_lock);
363 idr_remove(&tty_minors, minor);
364 mutex_unlock(&table_lock);
367 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
369 struct gb_tty *gb_tty;
372 gb_tty = get_gb_by_minor(tty->index);
376 retval = tty_standard_install(driver, tty);
380 tty->driver_data = gb_tty;
383 tty_port_put(&gb_tty->port);
387 static int gb_tty_open(struct tty_struct *tty, struct file *file)
389 struct gb_tty *gb_tty = tty->driver_data;
391 return tty_port_open(&gb_tty->port, tty, file);
394 static void gb_tty_close(struct tty_struct *tty, struct file *file)
396 struct gb_tty *gb_tty = tty->driver_data;
398 tty_port_close(&gb_tty->port, tty, file);
401 static void gb_tty_cleanup(struct tty_struct *tty)
403 struct gb_tty *gb_tty = tty->driver_data;
405 tty_port_put(&gb_tty->port);
408 static void gb_tty_hangup(struct tty_struct *tty)
410 struct gb_tty *gb_tty = tty->driver_data;
412 tty_port_hangup(&gb_tty->port);
415 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
418 struct gb_tty *gb_tty = tty->driver_data;
420 return send_data(gb_tty, count, buf);
423 static int gb_tty_write_room(struct tty_struct *tty)
425 // struct gb_tty *gb_tty = tty->driver_data;
427 // FIXME - how much do we want to say we have room for?
431 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
433 // struct gb_tty *gb_tty = tty->driver_data;
435 // FIXME - how many left to send?
439 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
441 struct gb_tty *gb_tty = tty->driver_data;
443 return send_break(gb_tty, state ? 1 : 0);
446 static void gb_tty_set_termios(struct tty_struct *tty,
447 struct ktermios *termios_old)
449 struct gb_tty *gb_tty = tty->driver_data;
450 struct ktermios *termios = &tty->termios;
451 struct gb_serial_line_coding newline;
452 int newctrl = gb_tty->ctrlout;
454 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
455 newline.format = termios->c_cflag & CSTOPB ? 2 : 0;
456 newline.parity = termios->c_cflag & PARENB ?
457 (termios->c_cflag & PARODD ? 1 : 2) +
458 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
460 switch (termios->c_cflag & CSIZE) {
476 /* FIXME: needs to clear unsupported bits in the termios */
477 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
479 if (C_BAUD(tty) == B0) {
480 newline.rate = gb_tty->line_coding.rate;
481 newctrl &= GB_UART_CTRL_DTR;
482 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
483 newctrl |= GB_UART_CTRL_DTR;
486 if (newctrl != gb_tty->ctrlout) {
487 gb_tty->ctrlout = newctrl;
488 send_control(gb_tty, newctrl);
491 if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
492 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
493 send_line_coding(gb_tty, &gb_tty->line_coding);
497 static int gb_tty_tiocmget(struct tty_struct *tty)
499 struct gb_tty *gb_tty = tty->driver_data;
501 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
502 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
503 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
504 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
505 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
509 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
512 struct gb_tty *gb_tty = tty->driver_data;
513 unsigned int newctrl = gb_tty->ctrlout;
515 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
516 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
517 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
518 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
520 newctrl = (newctrl & ~clear) | set;
521 if (gb_tty->ctrlout == newctrl)
524 gb_tty->ctrlout = newctrl;
525 return send_control(gb_tty, newctrl);
528 static void gb_tty_throttle(struct tty_struct *tty)
530 struct gb_tty *gb_tty = tty->driver_data;
531 unsigned char stop_char;
535 stop_char = STOP_CHAR(tty);
536 retval = gb_tty_write(tty, &stop_char, 1);
541 if (tty->termios.c_cflag & CRTSCTS) {
542 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
543 retval = send_control(gb_tty, gb_tty->ctrlout);
548 static void gb_tty_unthrottle(struct tty_struct *tty)
550 struct gb_tty *gb_tty = tty->driver_data;
551 unsigned char start_char;
555 start_char = START_CHAR(tty);
556 retval = gb_tty_write(tty, &start_char, 1);
561 if (tty->termios.c_cflag & CRTSCTS) {
562 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
563 retval = send_control(gb_tty, gb_tty->ctrlout);
567 static int get_serial_info(struct gb_tty *gb_tty,
568 struct serial_struct __user *info)
570 struct serial_struct tmp;
575 memset(&tmp, 0, sizeof(tmp));
576 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
577 tmp.type = PORT_16550A;
578 tmp.line = gb_tty->minor;
579 tmp.xmit_fifo_size = 16;
580 tmp.baud_base = 9600;
581 tmp.close_delay = gb_tty->port.close_delay / 10;
582 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
583 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
585 if (copy_to_user(info, &tmp, sizeof(tmp)))
590 static int set_serial_info(struct gb_tty *gb_tty,
591 struct serial_struct __user *newinfo)
593 struct serial_struct new_serial;
594 unsigned int closing_wait;
595 unsigned int close_delay;
598 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
601 close_delay = new_serial.close_delay * 10;
602 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
603 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
605 mutex_lock(&gb_tty->port.mutex);
606 if (!capable(CAP_SYS_ADMIN)) {
607 if ((close_delay != gb_tty->port.close_delay) ||
608 (closing_wait != gb_tty->port.closing_wait))
611 retval = -EOPNOTSUPP;
613 gb_tty->port.close_delay = close_delay;
614 gb_tty->port.closing_wait = closing_wait;
616 mutex_unlock(&gb_tty->port.mutex);
620 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
623 DECLARE_WAITQUEUE(wait, current);
624 struct async_icount old;
625 struct async_icount new;
627 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
631 spin_lock_irq(&gb_tty->read_lock);
632 old = gb_tty->oldcount;
633 new = gb_tty->iocount;
634 gb_tty->oldcount = new;
635 spin_unlock_irq(&gb_tty->read_lock);
637 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
639 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
641 if ((arg & TIOCM_RI) && (old.rng != new.rng))
644 add_wait_queue(&gb_tty->wioctl, &wait);
645 set_current_state(TASK_INTERRUPTIBLE);
647 remove_wait_queue(&gb_tty->wioctl, &wait);
648 if (gb_tty->disconnected) {
652 } else if (signal_pending(current)) {
653 retval = -ERESTARTSYS;
660 static int get_serial_usage(struct gb_tty *gb_tty,
661 struct serial_icounter_struct __user *count)
663 struct serial_icounter_struct icount;
666 memset(&icount, 0, sizeof(icount));
667 icount.dsr = gb_tty->iocount.dsr;
668 icount.rng = gb_tty->iocount.rng;
669 icount.dcd = gb_tty->iocount.dcd;
670 icount.frame = gb_tty->iocount.frame;
671 icount.overrun = gb_tty->iocount.overrun;
672 icount.parity = gb_tty->iocount.parity;
673 icount.brk = gb_tty->iocount.brk;
675 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
681 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
684 struct gb_tty *gb_tty = tty->driver_data;
688 return get_serial_info(gb_tty,
689 (struct serial_struct __user *)arg);
691 return set_serial_info(gb_tty,
692 (struct serial_struct __user *)arg);
694 return wait_serial_change(gb_tty, arg);
696 return get_serial_usage(gb_tty,
697 (struct serial_icounter_struct __user *)arg);
704 static const struct tty_operations gb_ops = {
705 .install = gb_tty_install,
707 .close = gb_tty_close,
708 .cleanup = gb_tty_cleanup,
709 .hangup = gb_tty_hangup,
710 .write = gb_tty_write,
711 .write_room = gb_tty_write_room,
712 .ioctl = gb_tty_ioctl,
713 .throttle = gb_tty_throttle,
714 .unthrottle = gb_tty_unthrottle,
715 .chars_in_buffer = gb_tty_chars_in_buffer,
716 .break_ctl = gb_tty_break_ctl,
717 .set_termios = gb_tty_set_termios,
718 .tiocmget = gb_tty_tiocmget,
719 .tiocmset = gb_tty_tiocmset,
723 static int gb_tty_init(void);
724 static void gb_tty_exit(void);
726 static int gb_uart_connection_init(struct gb_connection *connection)
728 struct gb_tty *gb_tty;
729 struct device *tty_dev;
733 /* First time here, initialize the tty structures */
734 if (atomic_inc_return(&reference_count) == 1) {
735 retval = gb_tty_init();
737 atomic_dec(&reference_count);
742 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
745 gb_tty->connection = connection;
747 /* Check for compatible protocol version */
748 retval = get_version(gb_tty);
752 minor = alloc_minor(gb_tty);
754 if (minor == -ENOSPC) {
755 dev_err(&connection->dev,
756 "no more free minor numbers\n");
762 gb_tty->minor = minor;
763 spin_lock_init(&gb_tty->write_lock);
764 spin_lock_init(&gb_tty->read_lock);
765 init_waitqueue_head(&gb_tty->wioctl);
766 mutex_init(&gb_tty->mutex);
768 connection->private = gb_tty;
770 send_control(gb_tty, gb_tty->ctrlout);
772 /* initialize the uart to be 9600n81 */
773 gb_tty->line_coding.rate = cpu_to_le32(9600);
774 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
775 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
776 gb_tty->line_coding.data = 8;
777 send_line_coding(gb_tty, &gb_tty->line_coding);
779 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
781 if (IS_ERR(tty_dev)) {
782 retval = PTR_ERR(tty_dev);
788 release_minor(gb_tty);
790 connection->private = NULL;
795 static void gb_uart_connection_exit(struct gb_connection *connection)
797 struct gb_tty *gb_tty = connection->private;
798 struct tty_struct *tty;
803 mutex_lock(&gb_tty->mutex);
804 gb_tty->disconnected = true;
806 wake_up_all(&gb_tty->wioctl);
807 connection->private = NULL;
808 mutex_unlock(&gb_tty->mutex);
810 tty = tty_port_tty_get(&gb_tty->port);
815 /* FIXME - stop all traffic */
817 tty_unregister_device(gb_tty_driver, gb_tty->minor);
819 /* FIXME - free transmit / receive buffers */
821 tty_port_put(&gb_tty->port);
825 /* If last device is gone, tear down the tty structures */
826 if (atomic_dec_return(&reference_count) == 0)
830 static int gb_tty_init(void)
834 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
835 if (IS_ERR(gb_tty_driver)) {
836 pr_err("Can not allocate tty driver\n");
838 goto fail_unregister_dev;
841 gb_tty_driver->driver_name = "gb";
842 gb_tty_driver->name = GB_NAME;
843 gb_tty_driver->major = 0;
844 gb_tty_driver->minor_start = 0;
845 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
846 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
847 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
848 gb_tty_driver->init_termios = tty_std_termios;
849 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
850 tty_set_operations(gb_tty_driver, &gb_ops);
852 retval = tty_register_driver(gb_tty_driver);
854 pr_err("Can not register tty driver: %d\n", retval);
855 goto fail_put_gb_tty;
861 put_tty_driver(gb_tty_driver);
866 static void gb_tty_exit(void)
868 int major = MAJOR(gb_tty_driver->major);
869 int minor = gb_tty_driver->minor_start;
871 tty_unregister_driver(gb_tty_driver);
872 put_tty_driver(gb_tty_driver);
873 unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
876 static struct gb_protocol uart_protocol = {
877 .id = GREYBUS_PROTOCOL_UART,
880 .connection_init = gb_uart_connection_init,
881 .connection_exit = gb_uart_connection_exit,
882 .request_recv = NULL, /* FIXME we have 2 types of requests!!! */
885 bool gb_uart_protocol_init(void)
887 return gb_protocol_register(&uart_protocol);
890 void gb_uart_protocol_exit(void)
892 gb_protocol_deregister(&uart_protocol);