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;
119 unsigned int throttled:1;
120 unsigned int throttle_req:1;
122 int writesize; // FIXME - set this somehow.
123 spinlock_t read_lock;
124 spinlock_t write_lock;
125 struct async_icount iocount;
126 struct async_icount oldcount;
127 wait_queue_head_t wioctl;
131 unsigned int ctrlin; /* input control lines */
132 unsigned int ctrlout; /* output control lines */
133 struct gb_serial_line_coding line_coding;
137 static struct tty_driver *gb_tty_driver;
138 static DEFINE_IDR(tty_minors);
139 static DEFINE_MUTEX(table_lock);
140 static atomic_t reference_count = ATOMIC_INIT(0);
143 static int request_operation(struct gb_connection *connection, int type,
144 void *response, int response_size)
146 struct gb_operation *operation;
147 struct gb_uart_simple_response *fake_request;
151 local_response = kmalloc(response_size, GFP_KERNEL);
155 operation = gb_operation_create(connection, type, 0, response_size);
157 kfree(local_response);
161 /* Synchronous operation--no callback */
162 ret = gb_operation_request_send(operation, NULL);
164 pr_err("version operation failed (%d)\n", ret);
169 * We only want to look at the status, and all requests have the same
170 * layout for where the status is, so cast this to a random request so
171 * we can see the status easier.
173 fake_request = (struct gb_uart_simple_response *)local_response;
174 if (fake_request->status) {
175 gb_connection_err(connection, "response %hhu",
176 fake_request->status);
179 /* Good request, so copy to the caller's buffer */
180 memcpy(response, local_response, response_size);
183 gb_operation_destroy(operation);
184 kfree(local_response);
190 * This request only uses the connection field, and if successful,
191 * fills in the major and minor protocol version of the target.
193 static int get_version(struct gb_tty *tty)
195 struct gb_uart_proto_version_response version_request;
198 retval = request_operation(tty->connection,
199 GB_UART_REQ_PROTOCOL_VERSION,
200 &version_request, sizeof(version_request));
204 if (version_request.major > GB_UART_VERSION_MAJOR) {
205 pr_err("unsupported major version (%hhu > %hhu)\n",
206 version_request.major, GB_UART_VERSION_MAJOR);
210 tty->version_major = version_request.major;
211 tty->version_minor = version_request.minor;
215 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
217 struct gb_connection *connection = tty->connection;
218 struct gb_operation *operation;
219 struct gb_uart_send_data_request *request;
220 struct gb_uart_simple_response *response;
226 operation = gb_operation_create(connection, GB_UART_REQ_SEND_DATA,
227 sizeof(*request) + size,
231 request = operation->request_payload;
232 request->size = cpu_to_le16(size);
233 memcpy(&request->data[0], data, size);
235 /* Synchronous operation--no callback */
236 retval = gb_operation_request_send(operation, NULL);
238 dev_err(&connection->dev,
239 "send data operation failed (%d)\n", retval);
243 response = operation->response_payload;
244 if (response->status) {
245 gb_connection_err(connection, "send data response %hhu",
250 gb_operation_destroy(operation);
255 static int send_line_coding(struct gb_tty *tty,
256 struct gb_serial_line_coding *line_coding)
258 struct gb_connection *connection = tty->connection;
259 struct gb_operation *operation;
260 struct gb_uart_set_line_coding_request *request;
261 struct gb_uart_simple_response *response;
264 operation = gb_operation_create(connection, GB_UART_REQ_SET_LINE_CODING,
269 request = operation->request_payload;
270 memcpy(&request->line_coding, line_coding, sizeof(*line_coding));
272 /* Synchronous operation--no callback */
273 retval = gb_operation_request_send(operation, NULL);
275 dev_err(&connection->dev,
276 "send line coding operation failed (%d)\n", retval);
280 response = operation->response_payload;
281 if (response->status) {
282 gb_connection_err(connection, "send line coding response %hhu",
287 gb_operation_destroy(operation);
292 static int send_control(struct gb_tty *tty, u16 control)
294 struct gb_connection *connection = tty->connection;
295 struct gb_operation *operation;
296 struct gb_uart_set_control_line_state_request *request;
297 struct gb_uart_simple_response *response;
300 operation = gb_operation_create(connection,
301 GB_UART_REQ_SET_CONTROL_LINE_STATE,
306 request = operation->request_payload;
307 request->control = cpu_to_le16(control);
309 /* Synchronous operation--no callback */
310 retval = gb_operation_request_send(operation, NULL);
312 dev_err(&connection->dev,
313 "send control operation failed (%d)\n", retval);
317 response = operation->response_payload;
318 if (response->status) {
319 gb_connection_err(connection, "send control response %hhu",
324 gb_operation_destroy(operation);
329 static int send_break(struct gb_tty *tty, u8 state)
331 struct gb_connection *connection = tty->connection;
332 struct gb_operation *operation;
333 struct gb_uart_set_break_request *request;
334 struct gb_uart_simple_response *response;
337 if ((state != 0) && (state != 1)) {
338 dev_err(&connection->dev, "invalid break state of %d\n", state);
342 operation = gb_operation_create(connection, GB_UART_REQ_SET_BREAK,
347 request = operation->request_payload;
348 request->state = state;
350 /* Synchronous operation--no callback */
351 retval = gb_operation_request_send(operation, NULL);
353 dev_err(&connection->dev,
354 "send break operation failed (%d)\n", retval);
358 response = operation->response_payload;
359 if (response->status) {
360 gb_connection_err(connection, "send break response %hhu",
365 gb_operation_destroy(operation);
371 static struct gb_tty *get_gb_by_minor(unsigned minor)
373 struct gb_tty *gb_tty;
375 mutex_lock(&table_lock);
376 gb_tty = idr_find(&tty_minors, minor);
378 mutex_lock(&gb_tty->mutex);
379 if (gb_tty->disconnected) {
380 mutex_unlock(&gb_tty->mutex);
383 tty_port_get(&gb_tty->port);
384 mutex_unlock(&gb_tty->mutex);
387 mutex_unlock(&table_lock);
391 static int alloc_minor(struct gb_tty *gb_tty)
395 mutex_lock(&table_lock);
396 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
397 mutex_unlock(&table_lock);
399 gb_tty->minor = minor;
403 static void release_minor(struct gb_tty *gb_tty)
405 int minor = gb_tty->minor;
407 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
408 mutex_lock(&table_lock);
409 idr_remove(&tty_minors, minor);
410 mutex_unlock(&table_lock);
413 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
415 struct gb_tty *gb_tty;
418 gb_tty = get_gb_by_minor(tty->index);
422 retval = tty_standard_install(driver, tty);
426 tty->driver_data = gb_tty;
429 tty_port_put(&gb_tty->port);
433 static int gb_tty_open(struct tty_struct *tty, struct file *file)
435 struct gb_tty *gb_tty = tty->driver_data;
437 return tty_port_open(&gb_tty->port, tty, file);
440 static void gb_tty_close(struct tty_struct *tty, struct file *file)
442 struct gb_tty *gb_tty = tty->driver_data;
444 tty_port_close(&gb_tty->port, tty, file);
447 static void gb_tty_cleanup(struct tty_struct *tty)
449 struct gb_tty *gb_tty = tty->driver_data;
451 tty_port_put(&gb_tty->port);
454 static void gb_tty_hangup(struct tty_struct *tty)
456 struct gb_tty *gb_tty = tty->driver_data;
458 tty_port_hangup(&gb_tty->port);
461 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
464 struct gb_tty *gb_tty = tty->driver_data;
466 return send_data(gb_tty, count, buf);
469 static int gb_tty_write_room(struct tty_struct *tty)
471 // struct gb_tty *gb_tty = tty->driver_data;
473 // FIXME - how much do we want to say we have room for?
477 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
479 // struct gb_tty *gb_tty = tty->driver_data;
481 // FIXME - how many left to send?
485 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
487 struct gb_tty *gb_tty = tty->driver_data;
489 return send_break(gb_tty, state ? 1 : 0);
492 static void gb_tty_set_termios(struct tty_struct *tty,
493 struct ktermios *termios_old)
495 struct gb_tty *gb_tty = tty->driver_data;
496 struct ktermios *termios = &tty->termios;
497 struct gb_serial_line_coding newline;
498 int newctrl = gb_tty->ctrlout;
500 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
501 newline.format = termios->c_cflag & CSTOPB ? 2 : 0;
502 newline.parity = termios->c_cflag & PARENB ?
503 (termios->c_cflag & PARODD ? 1 : 2) +
504 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
506 switch (termios->c_cflag & CSIZE) {
522 /* FIXME: needs to clear unsupported bits in the termios */
523 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
525 if (C_BAUD(tty) == B0) {
526 newline.rate = gb_tty->line_coding.rate;
527 newctrl &= GB_UART_CTRL_DTR;
528 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
529 newctrl |= GB_UART_CTRL_DTR;
532 if (newctrl != gb_tty->ctrlout) {
533 gb_tty->ctrlout = newctrl;
534 send_control(gb_tty, newctrl);
537 if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
538 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
539 send_line_coding(gb_tty, &gb_tty->line_coding);
543 static int gb_tty_tiocmget(struct tty_struct *tty)
545 struct gb_tty *gb_tty = tty->driver_data;
547 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
548 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
549 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
550 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
551 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
555 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
558 struct gb_tty *gb_tty = tty->driver_data;
559 unsigned int newctrl = gb_tty->ctrlout;
561 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
562 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
563 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
564 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
566 newctrl = (newctrl & ~clear) | set;
567 if (gb_tty->ctrlout == newctrl)
570 gb_tty->ctrlout = newctrl;
571 return send_control(gb_tty, newctrl);
574 static void gb_tty_throttle(struct tty_struct *tty)
576 struct gb_tty *gb_tty = tty->driver_data;
578 spin_lock_irq(&gb_tty->read_lock);
579 gb_tty->throttle_req = 1;
580 spin_unlock_irq(&gb_tty->read_lock);
583 static void gb_tty_unthrottle(struct tty_struct *tty)
585 struct gb_tty *gb_tty = tty->driver_data;
586 unsigned int was_throttled;
588 spin_lock_irq(&gb_tty->read_lock);
589 was_throttled = gb_tty->throttled;
590 gb_tty->throttle_req = 0;
591 gb_tty->throttled = 0;
592 spin_unlock_irq(&gb_tty->read_lock);
595 // FIXME - send more data
599 static int get_serial_info(struct gb_tty *gb_tty,
600 struct serial_struct __user *info)
602 struct serial_struct tmp;
607 memset(&tmp, 0, sizeof(tmp));
608 tmp.flags = ASYNC_LOW_LATENCY;
609 tmp.xmit_fifo_size = gb_tty->writesize;
610 tmp.baud_base = 0; // FIXME
611 tmp.close_delay = gb_tty->port.close_delay / 10;
612 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
613 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
615 if (copy_to_user(info, &tmp, sizeof(tmp)))
620 static int set_serial_info(struct gb_tty *gb_tty,
621 struct serial_struct __user *newinfo)
623 struct serial_struct new_serial;
624 unsigned int closing_wait;
625 unsigned int close_delay;
628 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
631 close_delay = new_serial.close_delay * 10;
632 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
633 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
635 mutex_lock(&gb_tty->port.mutex);
636 if (!capable(CAP_SYS_ADMIN)) {
637 if ((close_delay != gb_tty->port.close_delay) ||
638 (closing_wait != gb_tty->port.closing_wait))
641 retval = -EOPNOTSUPP;
643 gb_tty->port.close_delay = close_delay;
644 gb_tty->port.closing_wait = closing_wait;
646 mutex_unlock(&gb_tty->port.mutex);
650 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
653 DECLARE_WAITQUEUE(wait, current);
654 struct async_icount old;
655 struct async_icount new;
657 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
661 spin_lock_irq(&gb_tty->read_lock);
662 old = gb_tty->oldcount;
663 new = gb_tty->iocount;
664 gb_tty->oldcount = new;
665 spin_unlock_irq(&gb_tty->read_lock);
667 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
669 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
671 if ((arg & TIOCM_RI) && (old.rng != new.rng))
674 add_wait_queue(&gb_tty->wioctl, &wait);
675 set_current_state(TASK_INTERRUPTIBLE);
677 remove_wait_queue(&gb_tty->wioctl, &wait);
678 if (gb_tty->disconnected) {
682 } else if (signal_pending(current)) {
683 retval = -ERESTARTSYS;
690 static int get_serial_usage(struct gb_tty *gb_tty,
691 struct serial_icounter_struct __user *count)
693 struct serial_icounter_struct icount;
696 memset(&icount, 0, sizeof(icount));
697 icount.dsr = gb_tty->iocount.dsr;
698 icount.rng = gb_tty->iocount.rng;
699 icount.dcd = gb_tty->iocount.dcd;
700 icount.frame = gb_tty->iocount.frame;
701 icount.overrun = gb_tty->iocount.overrun;
702 icount.parity = gb_tty->iocount.parity;
703 icount.brk = gb_tty->iocount.brk;
705 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
711 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
714 struct gb_tty *gb_tty = tty->driver_data;
718 return get_serial_info(gb_tty,
719 (struct serial_struct __user *)arg);
721 return set_serial_info(gb_tty,
722 (struct serial_struct __user *)arg);
724 return wait_serial_change(gb_tty, arg);
726 return get_serial_usage(gb_tty,
727 (struct serial_icounter_struct __user *)arg);
734 static const struct tty_operations gb_ops = {
735 .install = gb_tty_install,
737 .close = gb_tty_close,
738 .cleanup = gb_tty_cleanup,
739 .hangup = gb_tty_hangup,
740 .write = gb_tty_write,
741 .write_room = gb_tty_write_room,
742 .ioctl = gb_tty_ioctl,
743 .throttle = gb_tty_throttle,
744 .unthrottle = gb_tty_unthrottle,
745 .chars_in_buffer = gb_tty_chars_in_buffer,
746 .break_ctl = gb_tty_break_ctl,
747 .set_termios = gb_tty_set_termios,
748 .tiocmget = gb_tty_tiocmget,
749 .tiocmset = gb_tty_tiocmset,
753 static int gb_tty_init(void);
754 static void gb_tty_exit(void);
756 static int gb_uart_connection_init(struct gb_connection *connection)
758 struct gb_tty *gb_tty;
759 struct device *tty_dev;
763 /* First time here, initialize the tty structures */
764 if (atomic_inc_return(&reference_count) == 1) {
765 retval = gb_tty_init();
767 atomic_dec(&reference_count);
772 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
775 gb_tty->connection = connection;
777 /* Check for compatible protocol version */
778 retval = get_version(gb_tty);
782 minor = alloc_minor(gb_tty);
784 if (minor == -ENOSPC) {
785 dev_err(&connection->dev,
786 "no more free minor numbers\n");
792 gb_tty->minor = minor;
793 spin_lock_init(&gb_tty->write_lock);
794 spin_lock_init(&gb_tty->read_lock);
795 init_waitqueue_head(&gb_tty->wioctl);
796 mutex_init(&gb_tty->mutex);
798 connection->private = gb_tty;
800 send_control(gb_tty, gb_tty->ctrlout);
802 /* initialize the uart to be 9600n81 */
803 gb_tty->line_coding.rate = cpu_to_le32(9600);
804 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
805 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
806 gb_tty->line_coding.data = 8;
807 send_line_coding(gb_tty, &gb_tty->line_coding);
809 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
811 if (IS_ERR(tty_dev)) {
812 retval = PTR_ERR(tty_dev);
818 release_minor(gb_tty);
820 connection->private = NULL;
825 static void gb_uart_connection_exit(struct gb_connection *connection)
827 struct gb_tty *gb_tty = connection->private;
828 struct tty_struct *tty;
833 mutex_lock(&gb_tty->mutex);
834 gb_tty->disconnected = true;
836 wake_up_all(&gb_tty->wioctl);
837 connection->private = NULL;
838 mutex_unlock(&gb_tty->mutex);
840 tty = tty_port_tty_get(&gb_tty->port);
845 /* FIXME - stop all traffic */
847 tty_unregister_device(gb_tty_driver, gb_tty->minor);
849 /* FIXME - free transmit / recieve buffers */
851 tty_port_put(&gb_tty->port);
855 /* If last device is gone, tear down the tty structures */
856 if (atomic_dec_return(&reference_count) == 0)
860 static int gb_tty_init(void)
864 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
865 if (IS_ERR(gb_tty_driver)) {
866 pr_err("Can not allocate tty driver\n");
868 goto fail_unregister_dev;
871 gb_tty_driver->driver_name = "gb";
872 gb_tty_driver->name = GB_NAME;
873 gb_tty_driver->major = 0;
874 gb_tty_driver->minor_start = 0;
875 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
876 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
877 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
878 gb_tty_driver->init_termios = tty_std_termios;
879 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
880 tty_set_operations(gb_tty_driver, &gb_ops);
882 retval = tty_register_driver(gb_tty_driver);
884 pr_err("Can not register tty driver: %d\n", retval);
885 goto fail_put_gb_tty;
891 put_tty_driver(gb_tty_driver);
896 static void gb_tty_exit(void)
898 int major = MAJOR(gb_tty_driver->major);
899 int minor = gb_tty_driver->minor_start;
901 tty_unregister_driver(gb_tty_driver);
902 put_tty_driver(gb_tty_driver);
903 unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
906 static struct gb_protocol uart_protocol = {
907 .id = GREYBUS_PROTOCOL_UART,
910 .connection_init = gb_uart_connection_init,
911 .connection_exit = gb_uart_connection_exit,
912 .request_recv = NULL, /* FIXME we have 2 types of requests!!! */
915 bool gb_uart_protocol_init(void)
917 return gb_protocol_register(&uart_protocol);
920 void gb_uart_protocol_exit(void)
922 gb_protocol_deregister(&uart_protocol);