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 {
47 size_t buffer_payload_max;
48 struct gb_connection *connection;
54 spinlock_t write_lock;
55 struct async_icount iocount;
56 struct async_icount oldcount;
57 wait_queue_head_t wioctl;
59 u8 ctrlin; /* input control lines */
60 u8 ctrlout; /* output control lines */
61 struct gb_tty_line_coding line_coding;
64 static struct tty_driver *gb_tty_driver;
65 static DEFINE_IDR(tty_minors);
66 static DEFINE_MUTEX(table_lock);
67 static atomic_t reference_count = ATOMIC_INIT(0);
69 static int gb_uart_receive_data_handler(struct gb_operation *op)
71 struct gb_connection *connection = op->connection;
72 struct gb_tty *gb_tty = gb_connection_get_data(connection);
73 struct tty_port *port = &gb_tty->port;
74 struct gb_message *request = op->request;
75 struct gb_uart_recv_data_request *receive_data;
78 unsigned long tty_flags = TTY_NORMAL;
80 if (request->payload_size < sizeof(*receive_data)) {
81 dev_err(&connection->bundle->dev,
82 "short receive-data request received (%zu < %zu)\n",
83 request->payload_size, sizeof(*receive_data));
87 receive_data = op->request->payload;
88 recv_data_size = le16_to_cpu(receive_data->size);
90 if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
91 dev_err(&connection->bundle->dev,
92 "malformed receive-data request received (%u != %zu)\n",
94 request->payload_size - sizeof(*receive_data));
101 if (receive_data->flags) {
102 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
103 tty_flags = TTY_BREAK;
104 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
105 tty_flags = TTY_PARITY;
106 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
107 tty_flags = TTY_FRAME;
109 /* overrun is special, not associated with a char */
110 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
111 tty_insert_flip_char(port, 0, TTY_OVERRUN);
113 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
114 tty_flags, recv_data_size);
115 if (count != recv_data_size) {
116 dev_err(&connection->bundle->dev,
117 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
118 recv_data_size, count);
121 tty_flip_buffer_push(port);
125 static int gb_uart_serial_state_handler(struct gb_operation *op)
127 struct gb_connection *connection = op->connection;
128 struct gb_tty *gb_tty = gb_connection_get_data(connection);
129 struct gb_message *request = op->request;
130 struct gb_uart_serial_state_request *serial_state;
132 if (request->payload_size < sizeof(*serial_state)) {
133 dev_err(&connection->bundle->dev,
134 "short serial-state event received (%zu < %zu)\n",
135 request->payload_size, sizeof(*serial_state));
139 serial_state = request->payload;
140 gb_tty->ctrlin = serial_state->control;
145 static int gb_uart_request_recv(u8 type, struct gb_operation *op)
147 struct gb_connection *connection = op->connection;
151 case GB_UART_TYPE_RECEIVE_DATA:
152 ret = gb_uart_receive_data_handler(op);
154 case GB_UART_TYPE_SERIAL_STATE:
155 ret = gb_uart_serial_state_handler(op);
158 dev_err(&connection->bundle->dev,
159 "unsupported unsolicited request: 0x%02x\n", type);
166 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
168 struct gb_uart_send_data_request *request;
174 if (size > tty->buffer_payload_max)
175 size = tty->buffer_payload_max;
176 request = tty->buffer;
177 request->size = cpu_to_le16(size);
178 memcpy(&request->data[0], data, size);
179 ret = gb_operation_sync(tty->connection, GB_UART_TYPE_SEND_DATA,
180 request, sizeof(*request) + size, NULL, 0);
187 static int send_line_coding(struct gb_tty *tty)
189 struct gb_uart_set_line_coding_request request;
191 memcpy(&request, &tty->line_coding,
192 sizeof(tty->line_coding));
193 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
194 &request, sizeof(request), NULL, 0);
197 static int send_control(struct gb_tty *gb_tty, u8 control)
199 struct gb_uart_set_control_line_state_request request;
201 request.control = control;
202 return gb_operation_sync(gb_tty->connection,
203 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
204 &request, sizeof(request), NULL, 0);
207 static int send_break(struct gb_tty *gb_tty, u8 state)
209 struct gb_uart_set_break_request request;
211 if ((state != 0) && (state != 1)) {
212 dev_err(&gb_tty->connection->bundle->dev,
213 "invalid break state of %d\n", state);
217 request.state = state;
218 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
219 &request, sizeof(request), NULL, 0);
223 static struct gb_tty *get_gb_by_minor(unsigned minor)
225 struct gb_tty *gb_tty;
227 mutex_lock(&table_lock);
228 gb_tty = idr_find(&tty_minors, minor);
230 mutex_lock(&gb_tty->mutex);
231 if (gb_tty->disconnected) {
232 mutex_unlock(&gb_tty->mutex);
235 tty_port_get(&gb_tty->port);
236 mutex_unlock(&gb_tty->mutex);
239 mutex_unlock(&table_lock);
243 static int alloc_minor(struct gb_tty *gb_tty)
247 mutex_lock(&table_lock);
248 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
249 mutex_unlock(&table_lock);
251 gb_tty->minor = minor;
255 static void release_minor(struct gb_tty *gb_tty)
257 int minor = gb_tty->minor;
259 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
260 mutex_lock(&table_lock);
261 idr_remove(&tty_minors, minor);
262 mutex_unlock(&table_lock);
265 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
267 struct gb_tty *gb_tty;
270 gb_tty = get_gb_by_minor(tty->index);
274 retval = tty_standard_install(driver, tty);
278 tty->driver_data = gb_tty;
281 tty_port_put(&gb_tty->port);
285 static int gb_tty_open(struct tty_struct *tty, struct file *file)
287 struct gb_tty *gb_tty = tty->driver_data;
289 return tty_port_open(&gb_tty->port, tty, file);
292 static void gb_tty_close(struct tty_struct *tty, struct file *file)
294 struct gb_tty *gb_tty = tty->driver_data;
296 tty_port_close(&gb_tty->port, tty, file);
299 static void gb_tty_cleanup(struct tty_struct *tty)
301 struct gb_tty *gb_tty = tty->driver_data;
303 tty_port_put(&gb_tty->port);
306 static void gb_tty_hangup(struct tty_struct *tty)
308 struct gb_tty *gb_tty = tty->driver_data;
310 tty_port_hangup(&gb_tty->port);
313 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
316 struct gb_tty *gb_tty = tty->driver_data;
318 return send_data(gb_tty, count, buf);
321 static int gb_tty_write_room(struct tty_struct *tty)
323 struct gb_tty *gb_tty = tty->driver_data;
325 return gb_tty->buffer_payload_max;
328 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
333 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
335 struct gb_tty *gb_tty = tty->driver_data;
337 return send_break(gb_tty, state ? 1 : 0);
340 static void gb_tty_set_termios(struct tty_struct *tty,
341 struct ktermios *termios_old)
343 struct gb_tty *gb_tty = tty->driver_data;
344 struct ktermios *termios = &tty->termios;
345 struct gb_tty_line_coding newline;
346 u8 newctrl = gb_tty->ctrlout;
348 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
349 newline.format = termios->c_cflag & CSTOPB ?
350 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
351 newline.parity = termios->c_cflag & PARENB ?
352 (termios->c_cflag & PARODD ? 1 : 2) +
353 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
355 switch (termios->c_cflag & CSIZE) {
357 newline.data_bits = 5;
360 newline.data_bits = 6;
363 newline.data_bits = 7;
367 newline.data_bits = 8;
371 /* FIXME: needs to clear unsupported bits in the termios */
372 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
374 if (C_BAUD(tty) == B0) {
375 newline.rate = gb_tty->line_coding.rate;
376 newctrl &= GB_UART_CTRL_DTR;
377 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
378 newctrl |= GB_UART_CTRL_DTR;
381 if (newctrl != gb_tty->ctrlout) {
382 gb_tty->ctrlout = newctrl;
383 send_control(gb_tty, newctrl);
386 if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
387 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
388 send_line_coding(gb_tty);
392 static int gb_tty_tiocmget(struct tty_struct *tty)
394 struct gb_tty *gb_tty = tty->driver_data;
396 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
397 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
398 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
399 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
400 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
404 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
407 struct gb_tty *gb_tty = tty->driver_data;
408 u8 newctrl = gb_tty->ctrlout;
410 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
411 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
412 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
413 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
415 newctrl = (newctrl & ~clear) | set;
416 if (gb_tty->ctrlout == newctrl)
419 gb_tty->ctrlout = newctrl;
420 return send_control(gb_tty, newctrl);
423 static void gb_tty_throttle(struct tty_struct *tty)
425 struct gb_tty *gb_tty = tty->driver_data;
426 unsigned char stop_char;
430 stop_char = STOP_CHAR(tty);
431 retval = gb_tty_write(tty, &stop_char, 1);
436 if (tty->termios.c_cflag & CRTSCTS) {
437 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
438 retval = send_control(gb_tty, gb_tty->ctrlout);
443 static void gb_tty_unthrottle(struct tty_struct *tty)
445 struct gb_tty *gb_tty = tty->driver_data;
446 unsigned char start_char;
450 start_char = START_CHAR(tty);
451 retval = gb_tty_write(tty, &start_char, 1);
456 if (tty->termios.c_cflag & CRTSCTS) {
457 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
458 retval = send_control(gb_tty, gb_tty->ctrlout);
462 static int get_serial_info(struct gb_tty *gb_tty,
463 struct serial_struct __user *info)
465 struct serial_struct tmp;
470 memset(&tmp, 0, sizeof(tmp));
471 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
472 tmp.type = PORT_16550A;
473 tmp.line = gb_tty->minor;
474 tmp.xmit_fifo_size = 16;
475 tmp.baud_base = 9600;
476 tmp.close_delay = gb_tty->port.close_delay / 10;
477 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
478 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
480 if (copy_to_user(info, &tmp, sizeof(tmp)))
485 static int set_serial_info(struct gb_tty *gb_tty,
486 struct serial_struct __user *newinfo)
488 struct serial_struct new_serial;
489 unsigned int closing_wait;
490 unsigned int close_delay;
493 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
496 close_delay = new_serial.close_delay * 10;
497 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
498 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
500 mutex_lock(&gb_tty->port.mutex);
501 if (!capable(CAP_SYS_ADMIN)) {
502 if ((close_delay != gb_tty->port.close_delay) ||
503 (closing_wait != gb_tty->port.closing_wait))
506 retval = -EOPNOTSUPP;
508 gb_tty->port.close_delay = close_delay;
509 gb_tty->port.closing_wait = closing_wait;
511 mutex_unlock(&gb_tty->port.mutex);
515 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
518 DECLARE_WAITQUEUE(wait, current);
519 struct async_icount old;
520 struct async_icount new;
522 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
526 spin_lock_irq(&gb_tty->read_lock);
527 old = gb_tty->oldcount;
528 new = gb_tty->iocount;
529 gb_tty->oldcount = new;
530 spin_unlock_irq(&gb_tty->read_lock);
532 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
534 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
536 if ((arg & TIOCM_RI) && (old.rng != new.rng))
539 add_wait_queue(&gb_tty->wioctl, &wait);
540 set_current_state(TASK_INTERRUPTIBLE);
542 remove_wait_queue(&gb_tty->wioctl, &wait);
543 if (gb_tty->disconnected) {
547 } else if (signal_pending(current)) {
548 retval = -ERESTARTSYS;
555 static int get_serial_usage(struct gb_tty *gb_tty,
556 struct serial_icounter_struct __user *count)
558 struct serial_icounter_struct icount;
561 memset(&icount, 0, sizeof(icount));
562 icount.dsr = gb_tty->iocount.dsr;
563 icount.rng = gb_tty->iocount.rng;
564 icount.dcd = gb_tty->iocount.dcd;
565 icount.frame = gb_tty->iocount.frame;
566 icount.overrun = gb_tty->iocount.overrun;
567 icount.parity = gb_tty->iocount.parity;
568 icount.brk = gb_tty->iocount.brk;
570 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
576 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
579 struct gb_tty *gb_tty = tty->driver_data;
583 return get_serial_info(gb_tty,
584 (struct serial_struct __user *)arg);
586 return set_serial_info(gb_tty,
587 (struct serial_struct __user *)arg);
589 return wait_serial_change(gb_tty, arg);
591 return get_serial_usage(gb_tty,
592 (struct serial_icounter_struct __user *)arg);
599 static const struct tty_operations gb_ops = {
600 .install = gb_tty_install,
602 .close = gb_tty_close,
603 .cleanup = gb_tty_cleanup,
604 .hangup = gb_tty_hangup,
605 .write = gb_tty_write,
606 .write_room = gb_tty_write_room,
607 .ioctl = gb_tty_ioctl,
608 .throttle = gb_tty_throttle,
609 .unthrottle = gb_tty_unthrottle,
610 .chars_in_buffer = gb_tty_chars_in_buffer,
611 .break_ctl = gb_tty_break_ctl,
612 .set_termios = gb_tty_set_termios,
613 .tiocmget = gb_tty_tiocmget,
614 .tiocmset = gb_tty_tiocmset,
617 static struct tty_port_operations null_ops = { };
619 static int gb_tty_init(void);
620 static void gb_tty_exit(void);
622 static int gb_uart_connection_init(struct gb_connection *connection)
625 struct gb_tty *gb_tty;
626 struct device *tty_dev;
630 /* First time here, initialize the tty structures */
631 if (atomic_inc_return(&reference_count) == 1) {
632 retval = gb_tty_init();
634 atomic_dec(&reference_count);
639 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
645 max_payload = gb_operation_get_payload_size_max(connection);
646 if (max_payload < sizeof(struct gb_uart_send_data_request)) {
651 gb_tty->buffer_payload_max = max_payload -
652 sizeof(struct gb_uart_send_data_request);
654 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
655 if (!gb_tty->buffer) {
660 gb_tty->connection = connection;
661 gb_connection_set_data(connection, gb_tty);
663 minor = alloc_minor(gb_tty);
665 if (minor == -ENOSPC) {
666 dev_err(&connection->bundle->dev,
667 "no more free minor numbers\n");
675 gb_tty->minor = minor;
676 spin_lock_init(&gb_tty->write_lock);
677 spin_lock_init(&gb_tty->read_lock);
678 init_waitqueue_head(&gb_tty->wioctl);
679 mutex_init(&gb_tty->mutex);
681 tty_port_init(&gb_tty->port);
682 gb_tty->port.ops = &null_ops;
684 send_control(gb_tty, gb_tty->ctrlout);
686 /* initialize the uart to be 9600n81 */
687 gb_tty->line_coding.rate = cpu_to_le32(9600);
688 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
689 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
690 gb_tty->line_coding.data_bits = 8;
691 send_line_coding(gb_tty);
693 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
694 &connection->bundle->dev);
695 if (IS_ERR(tty_dev)) {
696 retval = PTR_ERR(tty_dev);
702 tty_port_destroy(&gb_tty->port);
703 release_minor(gb_tty);
705 gb_connection_set_data(connection, NULL);
706 kfree(gb_tty->buffer);
710 if (atomic_dec_return(&reference_count) == 0)
715 static void gb_uart_connection_exit(struct gb_connection *connection)
717 struct gb_tty *gb_tty = gb_connection_get_data(connection);
718 struct tty_struct *tty;
723 mutex_lock(&gb_tty->mutex);
724 gb_tty->disconnected = true;
726 wake_up_all(&gb_tty->wioctl);
727 gb_connection_set_data(connection, NULL);
728 mutex_unlock(&gb_tty->mutex);
730 tty = tty_port_tty_get(&gb_tty->port);
735 /* FIXME - stop all traffic */
737 tty_unregister_device(gb_tty_driver, gb_tty->minor);
739 /* FIXME - free transmit / receive buffers */
741 tty_port_destroy(&gb_tty->port);
742 kfree(gb_tty->buffer);
745 /* If last device is gone, tear down the tty structures */
746 if (atomic_dec_return(&reference_count) == 0)
750 static int gb_tty_init(void)
754 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
755 if (IS_ERR(gb_tty_driver)) {
756 pr_err("Can not allocate tty driver\n");
758 goto fail_unregister_dev;
761 gb_tty_driver->driver_name = "gb";
762 gb_tty_driver->name = GB_NAME;
763 gb_tty_driver->major = 0;
764 gb_tty_driver->minor_start = 0;
765 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
766 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
767 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
768 gb_tty_driver->init_termios = tty_std_termios;
769 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
770 tty_set_operations(gb_tty_driver, &gb_ops);
772 retval = tty_register_driver(gb_tty_driver);
774 pr_err("Can not register tty driver: %d\n", retval);
775 goto fail_put_gb_tty;
781 put_tty_driver(gb_tty_driver);
786 static void gb_tty_exit(void)
788 tty_unregister_driver(gb_tty_driver);
789 put_tty_driver(gb_tty_driver);
790 idr_destroy(&tty_minors);
793 static struct gb_protocol uart_protocol = {
795 .id = GREYBUS_PROTOCOL_UART,
796 .major = GB_UART_VERSION_MAJOR,
797 .minor = GB_UART_VERSION_MINOR,
798 .connection_init = gb_uart_connection_init,
799 .connection_exit = gb_uart_connection_exit,
800 .request_recv = gb_uart_request_recv,
803 gb_builtin_protocol_driver(uart_protocol);