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 {
57 struct gb_uart_send_data_request {
62 struct gb_serial_line_coding {
65 #define GB_SERIAL_1_STOP_BITS 0
66 #define GB_SERIAL_1_5_STOP_BITS 1
67 #define GB_SERIAL_2_STOP_BITS 2
70 #define GB_SERIAL_NO_PARITY 0
71 #define GB_SERIAL_ODD_PARITY 1
72 #define GB_SERIAL_EVEN_PARITY 2
73 #define GB_SERIAL_MARK_PARITY 3
74 #define GB_SERIAL_SPACE_PARITY 4
77 } __attribute__ ((packed));
79 struct gb_uart_set_line_coding_request {
80 struct gb_serial_line_coding line_coding;
83 /* output control lines */
84 #define GB_UART_CTRL_DTR 0x01
85 #define GB_UART_CTRL_RTS 0x02
87 struct gb_uart_set_control_line_state_request {
91 struct gb_uart_set_break_request {
95 /* input control lines and line errors */
96 #define GB_UART_CTRL_DCD 0x01
97 #define GB_UART_CTRL_DSR 0x02
98 #define GB_UART_CTRL_BRK 0x04
99 #define GB_UART_CTRL_RI 0x08
101 #define GB_UART_CTRL_FRAMING 0x10
102 #define GB_UART_CTRL_PARITY 0x20
103 #define GB_UART_CTRL_OVERRUN 0x40
105 struct gb_uart_serial_state_request {
109 struct gb_uart_simple_response {
114 struct tty_port port;
115 struct gb_connection *connection;
118 unsigned char clocal;
120 spinlock_t read_lock;
121 spinlock_t write_lock;
122 struct async_icount iocount;
123 struct async_icount oldcount;
124 wait_queue_head_t wioctl;
128 unsigned int ctrlin; /* input control lines */
129 unsigned int ctrlout; /* output control lines */
130 struct gb_serial_line_coding line_coding;
134 static struct tty_driver *gb_tty_driver;
135 static DEFINE_IDR(tty_minors);
136 static DEFINE_MUTEX(table_lock);
137 static atomic_t reference_count = ATOMIC_INIT(0);
140 static int request_operation(struct gb_connection *connection, int type,
141 void *response, int response_size)
143 struct gb_operation *operation;
144 struct gb_uart_simple_response *fake_response;
147 operation = gb_operation_create(connection, type, 0, response_size);
151 /* Synchronous operation--no callback */
152 ret = gb_operation_request_send(operation, NULL);
154 pr_err("version operation failed (%d)\n", ret);
159 * We only want to look at the status, and all requests have the same
160 * layout for where the status is, so cast this to a random request so
161 * we can see the status easier.
163 fake_response = operation->response.payload;
164 if (fake_response->status) {
165 gb_connection_err(connection, "response %hhu",
166 fake_response->status);
169 /* Good request, so copy to the caller's buffer */
170 if (response_size && response)
171 memcpy(response, fake_response, response_size);
174 gb_operation_destroy(operation);
180 * This request only uses the connection field, and if successful,
181 * fills in the major and minor protocol version of the target.
183 static int get_version(struct gb_tty *tty)
185 struct gb_uart_proto_version_response version_response;
188 retval = request_operation(tty->connection,
189 GB_UART_REQ_PROTOCOL_VERSION,
190 &version_response, sizeof(version_response));
194 if (version_response.major > GB_UART_VERSION_MAJOR) {
195 pr_err("unsupported major version (%hhu > %hhu)\n",
196 version_response.major, GB_UART_VERSION_MAJOR);
200 tty->version_major = version_response.major;
201 tty->version_minor = version_response.minor;
205 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
207 struct gb_connection *connection = tty->connection;
208 struct gb_operation *operation;
209 struct gb_uart_send_data_request *request;
210 struct gb_uart_simple_response *response;
216 operation = gb_operation_create(connection, GB_UART_REQ_SEND_DATA,
217 sizeof(*request) + size,
221 request = operation->request.payload;
222 request->size = cpu_to_le16(size);
223 memcpy(&request->data[0], data, size);
225 /* Synchronous operation--no callback */
226 retval = gb_operation_request_send(operation, NULL);
228 dev_err(&connection->dev,
229 "send data operation failed (%d)\n", retval);
233 response = operation->response.payload;
234 if (response->status) {
235 gb_connection_err(connection, "send data response %hhu",
240 gb_operation_destroy(operation);
245 static int send_line_coding(struct gb_tty *tty,
246 struct gb_serial_line_coding *line_coding)
248 struct gb_connection *connection = tty->connection;
249 struct gb_operation *operation;
250 struct gb_uart_set_line_coding_request *request;
251 struct gb_uart_simple_response *response;
254 operation = gb_operation_create(connection, GB_UART_REQ_SET_LINE_CODING,
259 request = operation->request.payload;
260 memcpy(&request->line_coding, line_coding, sizeof(*line_coding));
262 /* Synchronous operation--no callback */
263 retval = gb_operation_request_send(operation, NULL);
265 dev_err(&connection->dev,
266 "send line coding operation failed (%d)\n", retval);
270 response = operation->response.payload;
271 if (response->status) {
272 gb_connection_err(connection, "send line coding response %hhu",
277 gb_operation_destroy(operation);
282 static int send_control(struct gb_tty *tty, u16 control)
284 struct gb_connection *connection = tty->connection;
285 struct gb_operation *operation;
286 struct gb_uart_set_control_line_state_request *request;
287 struct gb_uart_simple_response *response;
290 operation = gb_operation_create(connection,
291 GB_UART_REQ_SET_CONTROL_LINE_STATE,
296 request = operation->request.payload;
297 request->control = cpu_to_le16(control);
299 /* Synchronous operation--no callback */
300 retval = gb_operation_request_send(operation, NULL);
302 dev_err(&connection->dev,
303 "send control operation failed (%d)\n", retval);
307 response = operation->response.payload;
308 if (response->status) {
309 gb_connection_err(connection, "send control response %hhu",
314 gb_operation_destroy(operation);
319 static int send_break(struct gb_tty *tty, u8 state)
321 struct gb_connection *connection = tty->connection;
322 struct gb_operation *operation;
323 struct gb_uart_set_break_request *request;
324 struct gb_uart_simple_response *response;
327 if ((state != 0) && (state != 1)) {
328 dev_err(&connection->dev, "invalid break state of %d\n", state);
332 operation = gb_operation_create(connection, GB_UART_REQ_SET_BREAK,
337 request = operation->request.payload;
338 request->state = state;
340 /* Synchronous operation--no callback */
341 retval = gb_operation_request_send(operation, NULL);
343 dev_err(&connection->dev,
344 "send break operation failed (%d)\n", retval);
348 response = operation->response.payload;
349 if (response->status) {
350 gb_connection_err(connection, "send break response %hhu",
355 gb_operation_destroy(operation);
361 static struct gb_tty *get_gb_by_minor(unsigned minor)
363 struct gb_tty *gb_tty;
365 mutex_lock(&table_lock);
366 gb_tty = idr_find(&tty_minors, minor);
368 mutex_lock(&gb_tty->mutex);
369 if (gb_tty->disconnected) {
370 mutex_unlock(&gb_tty->mutex);
373 tty_port_get(&gb_tty->port);
374 mutex_unlock(&gb_tty->mutex);
377 mutex_unlock(&table_lock);
381 static int alloc_minor(struct gb_tty *gb_tty)
385 mutex_lock(&table_lock);
386 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
387 mutex_unlock(&table_lock);
389 gb_tty->minor = minor;
393 static void release_minor(struct gb_tty *gb_tty)
395 int minor = gb_tty->minor;
397 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
398 mutex_lock(&table_lock);
399 idr_remove(&tty_minors, minor);
400 mutex_unlock(&table_lock);
403 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
405 struct gb_tty *gb_tty;
408 gb_tty = get_gb_by_minor(tty->index);
412 retval = tty_standard_install(driver, tty);
416 tty->driver_data = gb_tty;
419 tty_port_put(&gb_tty->port);
423 static int gb_tty_open(struct tty_struct *tty, struct file *file)
425 struct gb_tty *gb_tty = tty->driver_data;
427 return tty_port_open(&gb_tty->port, tty, file);
430 static void gb_tty_close(struct tty_struct *tty, struct file *file)
432 struct gb_tty *gb_tty = tty->driver_data;
434 tty_port_close(&gb_tty->port, tty, file);
437 static void gb_tty_cleanup(struct tty_struct *tty)
439 struct gb_tty *gb_tty = tty->driver_data;
441 tty_port_put(&gb_tty->port);
444 static void gb_tty_hangup(struct tty_struct *tty)
446 struct gb_tty *gb_tty = tty->driver_data;
448 tty_port_hangup(&gb_tty->port);
451 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
454 struct gb_tty *gb_tty = tty->driver_data;
456 return send_data(gb_tty, count, buf);
459 static int gb_tty_write_room(struct tty_struct *tty)
461 // struct gb_tty *gb_tty = tty->driver_data;
463 // FIXME - how much do we want to say we have room for?
467 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
469 // struct gb_tty *gb_tty = tty->driver_data;
471 // FIXME - how many left to send?
475 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
477 struct gb_tty *gb_tty = tty->driver_data;
479 return send_break(gb_tty, state ? 1 : 0);
482 static void gb_tty_set_termios(struct tty_struct *tty,
483 struct ktermios *termios_old)
485 struct gb_tty *gb_tty = tty->driver_data;
486 struct ktermios *termios = &tty->termios;
487 struct gb_serial_line_coding newline;
488 int newctrl = gb_tty->ctrlout;
490 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
491 newline.format = termios->c_cflag & CSTOPB ? 2 : 0;
492 newline.parity = termios->c_cflag & PARENB ?
493 (termios->c_cflag & PARODD ? 1 : 2) +
494 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
496 switch (termios->c_cflag & CSIZE) {
512 /* FIXME: needs to clear unsupported bits in the termios */
513 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
515 if (C_BAUD(tty) == B0) {
516 newline.rate = gb_tty->line_coding.rate;
517 newctrl &= GB_UART_CTRL_DTR;
518 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
519 newctrl |= GB_UART_CTRL_DTR;
522 if (newctrl != gb_tty->ctrlout) {
523 gb_tty->ctrlout = newctrl;
524 send_control(gb_tty, newctrl);
527 if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
528 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
529 send_line_coding(gb_tty, &gb_tty->line_coding);
533 static int gb_tty_tiocmget(struct tty_struct *tty)
535 struct gb_tty *gb_tty = tty->driver_data;
537 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
538 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
539 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
540 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
541 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
545 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
548 struct gb_tty *gb_tty = tty->driver_data;
549 unsigned int newctrl = gb_tty->ctrlout;
551 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
552 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
553 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
554 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
556 newctrl = (newctrl & ~clear) | set;
557 if (gb_tty->ctrlout == newctrl)
560 gb_tty->ctrlout = newctrl;
561 return send_control(gb_tty, newctrl);
564 static void gb_tty_throttle(struct tty_struct *tty)
566 struct gb_tty *gb_tty = tty->driver_data;
567 unsigned char stop_char;
571 stop_char = STOP_CHAR(tty);
572 retval = gb_tty_write(tty, &stop_char, 1);
577 if (tty->termios.c_cflag & CRTSCTS) {
578 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
579 retval = send_control(gb_tty, gb_tty->ctrlout);
584 static void gb_tty_unthrottle(struct tty_struct *tty)
586 struct gb_tty *gb_tty = tty->driver_data;
587 unsigned char start_char;
591 start_char = START_CHAR(tty);
592 retval = gb_tty_write(tty, &start_char, 1);
597 if (tty->termios.c_cflag & CRTSCTS) {
598 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
599 retval = send_control(gb_tty, gb_tty->ctrlout);
603 static int get_serial_info(struct gb_tty *gb_tty,
604 struct serial_struct __user *info)
606 struct serial_struct tmp;
611 memset(&tmp, 0, sizeof(tmp));
612 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
613 tmp.type = PORT_16550A;
614 tmp.line = gb_tty->minor;
615 tmp.xmit_fifo_size = 16;
616 tmp.baud_base = 9600;
617 tmp.close_delay = gb_tty->port.close_delay / 10;
618 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
619 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
621 if (copy_to_user(info, &tmp, sizeof(tmp)))
626 static int set_serial_info(struct gb_tty *gb_tty,
627 struct serial_struct __user *newinfo)
629 struct serial_struct new_serial;
630 unsigned int closing_wait;
631 unsigned int close_delay;
634 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
637 close_delay = new_serial.close_delay * 10;
638 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
639 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
641 mutex_lock(&gb_tty->port.mutex);
642 if (!capable(CAP_SYS_ADMIN)) {
643 if ((close_delay != gb_tty->port.close_delay) ||
644 (closing_wait != gb_tty->port.closing_wait))
647 retval = -EOPNOTSUPP;
649 gb_tty->port.close_delay = close_delay;
650 gb_tty->port.closing_wait = closing_wait;
652 mutex_unlock(&gb_tty->port.mutex);
656 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
659 DECLARE_WAITQUEUE(wait, current);
660 struct async_icount old;
661 struct async_icount new;
663 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
667 spin_lock_irq(&gb_tty->read_lock);
668 old = gb_tty->oldcount;
669 new = gb_tty->iocount;
670 gb_tty->oldcount = new;
671 spin_unlock_irq(&gb_tty->read_lock);
673 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
675 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
677 if ((arg & TIOCM_RI) && (old.rng != new.rng))
680 add_wait_queue(&gb_tty->wioctl, &wait);
681 set_current_state(TASK_INTERRUPTIBLE);
683 remove_wait_queue(&gb_tty->wioctl, &wait);
684 if (gb_tty->disconnected) {
688 } else if (signal_pending(current)) {
689 retval = -ERESTARTSYS;
696 static int get_serial_usage(struct gb_tty *gb_tty,
697 struct serial_icounter_struct __user *count)
699 struct serial_icounter_struct icount;
702 memset(&icount, 0, sizeof(icount));
703 icount.dsr = gb_tty->iocount.dsr;
704 icount.rng = gb_tty->iocount.rng;
705 icount.dcd = gb_tty->iocount.dcd;
706 icount.frame = gb_tty->iocount.frame;
707 icount.overrun = gb_tty->iocount.overrun;
708 icount.parity = gb_tty->iocount.parity;
709 icount.brk = gb_tty->iocount.brk;
711 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
717 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
720 struct gb_tty *gb_tty = tty->driver_data;
724 return get_serial_info(gb_tty,
725 (struct serial_struct __user *)arg);
727 return set_serial_info(gb_tty,
728 (struct serial_struct __user *)arg);
730 return wait_serial_change(gb_tty, arg);
732 return get_serial_usage(gb_tty,
733 (struct serial_icounter_struct __user *)arg);
740 static const struct tty_operations gb_ops = {
741 .install = gb_tty_install,
743 .close = gb_tty_close,
744 .cleanup = gb_tty_cleanup,
745 .hangup = gb_tty_hangup,
746 .write = gb_tty_write,
747 .write_room = gb_tty_write_room,
748 .ioctl = gb_tty_ioctl,
749 .throttle = gb_tty_throttle,
750 .unthrottle = gb_tty_unthrottle,
751 .chars_in_buffer = gb_tty_chars_in_buffer,
752 .break_ctl = gb_tty_break_ctl,
753 .set_termios = gb_tty_set_termios,
754 .tiocmget = gb_tty_tiocmget,
755 .tiocmset = gb_tty_tiocmset,
759 static int gb_tty_init(void);
760 static void gb_tty_exit(void);
762 static int gb_uart_connection_init(struct gb_connection *connection)
764 struct gb_tty *gb_tty;
765 struct device *tty_dev;
769 /* First time here, initialize the tty structures */
770 if (atomic_inc_return(&reference_count) == 1) {
771 retval = gb_tty_init();
773 atomic_dec(&reference_count);
778 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
781 gb_tty->connection = connection;
783 /* Check for compatible protocol version */
784 retval = get_version(gb_tty);
788 minor = alloc_minor(gb_tty);
790 if (minor == -ENOSPC) {
791 dev_err(&connection->dev,
792 "no more free minor numbers\n");
798 gb_tty->minor = minor;
799 spin_lock_init(&gb_tty->write_lock);
800 spin_lock_init(&gb_tty->read_lock);
801 init_waitqueue_head(&gb_tty->wioctl);
802 mutex_init(&gb_tty->mutex);
804 connection->private = gb_tty;
806 send_control(gb_tty, gb_tty->ctrlout);
808 /* initialize the uart to be 9600n81 */
809 gb_tty->line_coding.rate = cpu_to_le32(9600);
810 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
811 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
812 gb_tty->line_coding.data = 8;
813 send_line_coding(gb_tty, &gb_tty->line_coding);
815 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
817 if (IS_ERR(tty_dev)) {
818 retval = PTR_ERR(tty_dev);
824 release_minor(gb_tty);
826 connection->private = NULL;
831 static void gb_uart_connection_exit(struct gb_connection *connection)
833 struct gb_tty *gb_tty = connection->private;
834 struct tty_struct *tty;
839 mutex_lock(&gb_tty->mutex);
840 gb_tty->disconnected = true;
842 wake_up_all(&gb_tty->wioctl);
843 connection->private = NULL;
844 mutex_unlock(&gb_tty->mutex);
846 tty = tty_port_tty_get(&gb_tty->port);
851 /* FIXME - stop all traffic */
853 tty_unregister_device(gb_tty_driver, gb_tty->minor);
855 /* FIXME - free transmit / recieve buffers */
857 tty_port_put(&gb_tty->port);
861 /* If last device is gone, tear down the tty structures */
862 if (atomic_dec_return(&reference_count) == 0)
866 static int gb_tty_init(void)
870 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
871 if (IS_ERR(gb_tty_driver)) {
872 pr_err("Can not allocate tty driver\n");
874 goto fail_unregister_dev;
877 gb_tty_driver->driver_name = "gb";
878 gb_tty_driver->name = GB_NAME;
879 gb_tty_driver->major = 0;
880 gb_tty_driver->minor_start = 0;
881 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
882 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
883 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
884 gb_tty_driver->init_termios = tty_std_termios;
885 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
886 tty_set_operations(gb_tty_driver, &gb_ops);
888 retval = tty_register_driver(gb_tty_driver);
890 pr_err("Can not register tty driver: %d\n", retval);
891 goto fail_put_gb_tty;
897 put_tty_driver(gb_tty_driver);
902 static void gb_tty_exit(void)
904 int major = MAJOR(gb_tty_driver->major);
905 int minor = gb_tty_driver->minor_start;
907 tty_unregister_driver(gb_tty_driver);
908 put_tty_driver(gb_tty_driver);
909 unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
912 static struct gb_protocol uart_protocol = {
913 .id = GREYBUS_PROTOCOL_UART,
916 .connection_init = gb_uart_connection_init,
917 .connection_exit = gb_uart_connection_exit,
918 .request_recv = NULL, /* FIXME we have 2 types of requests!!! */
921 bool gb_uart_protocol_init(void)
923 return gb_protocol_register(&uart_protocol);
926 void gb_uart_protocol_exit(void)
928 gb_protocol_deregister(&uart_protocol);