greybus: uart: Add credits based tracking for transmit path
[linux-2.6/btrfs-unstable.git] / drivers / staging / greybus / uart.c
blob14b3e9d06e9c15d328e10596272177cc77edbfbb
1 /*
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>
28 #include <linux/fs.h>
29 #include <linux/kdev_t.h>
30 #include <linux/kfifo.h>
31 #include <linux/workqueue.h>
33 #include "greybus.h"
34 #include "gbphy.h"
36 #define GB_NUM_MINORS 16 /* 16 is is more than enough */
37 #define GB_NAME "ttyGB"
39 #define GB_UART_WRITE_FIFO_SIZE PAGE_SIZE
40 #define GB_UART_WRITE_ROOM_MARGIN 1 /* leave some space in fifo */
41 #define GB_UART_FIRMWARE_CREDITS 4096
43 struct gb_tty_line_coding {
44 __le32 rate;
45 __u8 format;
46 __u8 parity;
47 __u8 data_bits;
48 __u8 flow_control;
51 struct gb_tty {
52 struct gbphy_device *gbphy_dev;
53 struct tty_port port;
54 void *buffer;
55 size_t buffer_payload_max;
56 struct gb_connection *connection;
57 u16 cport_id;
58 unsigned int minor;
59 unsigned char clocal;
60 bool disconnected;
61 spinlock_t read_lock;
62 spinlock_t write_lock;
63 struct async_icount iocount;
64 struct async_icount oldcount;
65 wait_queue_head_t wioctl;
66 struct mutex mutex;
67 u8 ctrlin; /* input control lines */
68 u8 ctrlout; /* output control lines */
69 struct gb_tty_line_coding line_coding;
70 struct work_struct tx_work;
71 struct kfifo write_fifo;
72 bool close_pending;
73 unsigned int credits;
76 static struct tty_driver *gb_tty_driver;
77 static DEFINE_IDR(tty_minors);
78 static DEFINE_MUTEX(table_lock);
80 static int gb_uart_receive_data_handler(struct gb_operation *op)
82 struct gb_connection *connection = op->connection;
83 struct gb_tty *gb_tty = gb_connection_get_data(connection);
84 struct tty_port *port = &gb_tty->port;
85 struct gb_message *request = op->request;
86 struct gb_uart_recv_data_request *receive_data;
87 u16 recv_data_size;
88 int count;
89 unsigned long tty_flags = TTY_NORMAL;
91 if (request->payload_size < sizeof(*receive_data)) {
92 dev_err(&gb_tty->gbphy_dev->dev,
93 "short receive-data request received (%zu < %zu)\n",
94 request->payload_size, sizeof(*receive_data));
95 return -EINVAL;
98 receive_data = op->request->payload;
99 recv_data_size = le16_to_cpu(receive_data->size);
101 if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
102 dev_err(&gb_tty->gbphy_dev->dev,
103 "malformed receive-data request received (%u != %zu)\n",
104 recv_data_size,
105 request->payload_size - sizeof(*receive_data));
106 return -EINVAL;
109 if (!recv_data_size)
110 return -EINVAL;
112 if (receive_data->flags) {
113 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
114 tty_flags = TTY_BREAK;
115 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
116 tty_flags = TTY_PARITY;
117 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
118 tty_flags = TTY_FRAME;
120 /* overrun is special, not associated with a char */
121 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
122 tty_insert_flip_char(port, 0, TTY_OVERRUN);
124 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
125 tty_flags, recv_data_size);
126 if (count != recv_data_size) {
127 dev_err(&gb_tty->gbphy_dev->dev,
128 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
129 recv_data_size, count);
131 if (count)
132 tty_flip_buffer_push(port);
133 return 0;
136 static int gb_uart_serial_state_handler(struct gb_operation *op)
138 struct gb_connection *connection = op->connection;
139 struct gb_tty *gb_tty = gb_connection_get_data(connection);
140 struct gb_message *request = op->request;
141 struct gb_uart_serial_state_request *serial_state;
143 if (request->payload_size < sizeof(*serial_state)) {
144 dev_err(&gb_tty->gbphy_dev->dev,
145 "short serial-state event received (%zu < %zu)\n",
146 request->payload_size, sizeof(*serial_state));
147 return -EINVAL;
150 serial_state = request->payload;
151 gb_tty->ctrlin = serial_state->control;
153 return 0;
156 static int gb_uart_receive_credits_handler(struct gb_operation *op)
158 struct gb_connection *connection = op->connection;
159 struct gb_tty *gb_tty = gb_connection_get_data(connection);
160 struct gb_message *request = op->request;
161 struct gb_uart_receive_credits_request *credit_request;
162 unsigned long flags;
163 unsigned int incoming_credits;
164 int ret = 0;
166 if (request->payload_size < sizeof(*credit_request)) {
167 dev_err(&gb_tty->gbphy_dev->dev,
168 "short receive_credits event received (%zu < %zu)\n",
169 request->payload_size,
170 sizeof(*credit_request));
171 return -EINVAL;
174 credit_request = request->payload;
175 incoming_credits = le16_to_cpu(credit_request->count);
177 spin_lock_irqsave(&gb_tty->write_lock, flags);
178 gb_tty->credits += incoming_credits;
179 if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
180 gb_tty->credits -= incoming_credits;
181 ret = -EINVAL;
183 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
185 if (ret) {
186 dev_err(&gb_tty->gbphy_dev->dev,
187 "invalid number of incoming credits: %d\n",
188 incoming_credits);
189 return ret;
192 if (!gb_tty->close_pending)
193 schedule_work(&gb_tty->tx_work);
196 * the port the tty layer may be waiting for credits
198 tty_port_tty_wakeup(&gb_tty->port);
200 return ret;
203 static int gb_uart_request_handler(struct gb_operation *op)
205 struct gb_connection *connection = op->connection;
206 struct gb_tty *gb_tty = gb_connection_get_data(connection);
207 int type = op->type;
208 int ret;
210 switch (type) {
211 case GB_UART_TYPE_RECEIVE_DATA:
212 ret = gb_uart_receive_data_handler(op);
213 break;
214 case GB_UART_TYPE_SERIAL_STATE:
215 ret = gb_uart_serial_state_handler(op);
216 break;
217 case GB_UART_TYPE_RECEIVE_CREDITS:
218 ret = gb_uart_receive_credits_handler(op);
219 break;
220 default:
221 dev_err(&gb_tty->gbphy_dev->dev,
222 "unsupported unsolicited request: 0x%02x\n", type);
223 ret = -EINVAL;
226 return ret;
229 static void gb_uart_tx_write_work(struct work_struct *work)
231 struct gb_uart_send_data_request *request;
232 struct gb_tty *gb_tty;
233 unsigned long flags;
234 unsigned int send_size;
235 int ret;
237 gb_tty = container_of(work, struct gb_tty, tx_work);
238 request = gb_tty->buffer;
240 while (1) {
241 if (gb_tty->close_pending)
242 break;
244 spin_lock_irqsave(&gb_tty->write_lock, flags);
245 send_size = gb_tty->buffer_payload_max;
246 if (send_size > gb_tty->credits)
247 send_size = gb_tty->credits;
249 send_size = kfifo_out_peek(&gb_tty->write_fifo,
250 &request->data[0],
251 send_size);
252 if (!send_size) {
253 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
254 break;
257 gb_tty->credits -= send_size;
258 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
260 request->size = cpu_to_le16(send_size);
261 ret = gb_operation_sync(gb_tty->connection,
262 GB_UART_TYPE_SEND_DATA,
263 request, sizeof(*request) + send_size,
264 NULL, 0);
265 if (ret) {
266 dev_err(&gb_tty->gbphy_dev->dev,
267 "send data error: %d\n", ret);
268 spin_lock_irqsave(&gb_tty->write_lock, flags);
269 gb_tty->credits += send_size;
270 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
271 if (!gb_tty->close_pending)
272 schedule_work(work);
273 return;
276 spin_lock_irqsave(&gb_tty->write_lock, flags);
277 ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
278 send_size);
279 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
281 tty_port_tty_wakeup(&gb_tty->port);
285 static int send_line_coding(struct gb_tty *tty)
287 struct gb_uart_set_line_coding_request request;
289 memcpy(&request, &tty->line_coding,
290 sizeof(tty->line_coding));
291 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
292 &request, sizeof(request), NULL, 0);
295 static int send_control(struct gb_tty *gb_tty, u8 control)
297 struct gb_uart_set_control_line_state_request request;
299 request.control = control;
300 return gb_operation_sync(gb_tty->connection,
301 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
302 &request, sizeof(request), NULL, 0);
305 static int send_break(struct gb_tty *gb_tty, u8 state)
307 struct gb_uart_set_break_request request;
309 if ((state != 0) && (state != 1)) {
310 dev_err(&gb_tty->gbphy_dev->dev,
311 "invalid break state of %d\n", state);
312 return -EINVAL;
315 request.state = state;
316 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
317 &request, sizeof(request), NULL, 0);
321 static struct gb_tty *get_gb_by_minor(unsigned minor)
323 struct gb_tty *gb_tty;
325 mutex_lock(&table_lock);
326 gb_tty = idr_find(&tty_minors, minor);
327 if (gb_tty) {
328 mutex_lock(&gb_tty->mutex);
329 if (gb_tty->disconnected) {
330 mutex_unlock(&gb_tty->mutex);
331 gb_tty = NULL;
332 } else {
333 tty_port_get(&gb_tty->port);
334 mutex_unlock(&gb_tty->mutex);
337 mutex_unlock(&table_lock);
338 return gb_tty;
341 static int alloc_minor(struct gb_tty *gb_tty)
343 int minor;
345 mutex_lock(&table_lock);
346 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
347 mutex_unlock(&table_lock);
348 if (minor >= 0)
349 gb_tty->minor = minor;
350 return minor;
353 static void release_minor(struct gb_tty *gb_tty)
355 int minor = gb_tty->minor;
357 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
358 mutex_lock(&table_lock);
359 idr_remove(&tty_minors, minor);
360 mutex_unlock(&table_lock);
363 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
365 struct gb_tty *gb_tty;
366 int retval;
368 gb_tty = get_gb_by_minor(tty->index);
369 if (!gb_tty)
370 return -ENODEV;
372 retval = tty_standard_install(driver, tty);
373 if (retval)
374 goto error;
376 tty->driver_data = gb_tty;
377 return 0;
378 error:
379 tty_port_put(&gb_tty->port);
380 return retval;
383 static int gb_tty_open(struct tty_struct *tty, struct file *file)
385 struct gb_tty *gb_tty = tty->driver_data;
387 return tty_port_open(&gb_tty->port, tty, file);
390 static void gb_tty_close(struct tty_struct *tty, struct file *file)
392 struct gb_tty *gb_tty = tty->driver_data;
394 tty_port_close(&gb_tty->port, tty, file);
397 static void gb_tty_cleanup(struct tty_struct *tty)
399 struct gb_tty *gb_tty = tty->driver_data;
401 tty_port_put(&gb_tty->port);
404 static void gb_tty_hangup(struct tty_struct *tty)
406 struct gb_tty *gb_tty = tty->driver_data;
408 tty_port_hangup(&gb_tty->port);
411 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
412 int count)
414 struct gb_tty *gb_tty = tty->driver_data;
416 count = kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
417 &gb_tty->write_lock);
418 if (count && !gb_tty->close_pending)
419 schedule_work(&gb_tty->tx_work);
421 return count;
424 static int gb_tty_write_room(struct tty_struct *tty)
426 struct gb_tty *gb_tty = tty->driver_data;
427 unsigned long flags;
428 int room;
430 spin_lock_irqsave(&gb_tty->write_lock, flags);
431 room = kfifo_avail(&gb_tty->write_fifo);
432 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
434 room -= GB_UART_WRITE_ROOM_MARGIN;
435 if (room < 0)
436 return 0;
438 return room;
441 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
443 struct gb_tty *gb_tty = tty->driver_data;
444 unsigned long flags;
445 int chars;
447 spin_lock_irqsave(&gb_tty->write_lock, flags);
448 chars = kfifo_len(&gb_tty->write_fifo);
449 if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
450 chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
451 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
453 return chars;
456 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
458 struct gb_tty *gb_tty = tty->driver_data;
460 return send_break(gb_tty, state ? 1 : 0);
463 static void gb_tty_set_termios(struct tty_struct *tty,
464 struct ktermios *termios_old)
466 struct gb_tty *gb_tty = tty->driver_data;
467 struct ktermios *termios = &tty->termios;
468 struct gb_tty_line_coding newline;
469 u8 newctrl = gb_tty->ctrlout;
471 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
472 newline.format = termios->c_cflag & CSTOPB ?
473 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
474 newline.parity = termios->c_cflag & PARENB ?
475 (termios->c_cflag & PARODD ? 1 : 2) +
476 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
478 switch (termios->c_cflag & CSIZE) {
479 case CS5:
480 newline.data_bits = 5;
481 break;
482 case CS6:
483 newline.data_bits = 6;
484 break;
485 case CS7:
486 newline.data_bits = 7;
487 break;
488 case CS8:
489 default:
490 newline.data_bits = 8;
491 break;
494 /* FIXME: needs to clear unsupported bits in the termios */
495 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
497 if (C_BAUD(tty) == B0) {
498 newline.rate = gb_tty->line_coding.rate;
499 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
500 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
501 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
504 if (newctrl != gb_tty->ctrlout) {
505 gb_tty->ctrlout = newctrl;
506 send_control(gb_tty, newctrl);
509 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
510 newline.flow_control |= GB_SERIAL_AUTO_RTSCTS_EN;
511 else
512 newline.flow_control &= ~GB_SERIAL_AUTO_RTSCTS_EN;
514 if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
515 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
516 send_line_coding(gb_tty);
520 static int gb_tty_tiocmget(struct tty_struct *tty)
522 struct gb_tty *gb_tty = tty->driver_data;
524 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
525 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
526 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
527 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
528 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
529 TIOCM_CTS;
532 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
533 unsigned int clear)
535 struct gb_tty *gb_tty = tty->driver_data;
536 u8 newctrl = gb_tty->ctrlout;
538 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
539 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
540 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
541 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
543 newctrl = (newctrl & ~clear) | set;
544 if (gb_tty->ctrlout == newctrl)
545 return 0;
547 gb_tty->ctrlout = newctrl;
548 return send_control(gb_tty, newctrl);
551 static void gb_tty_throttle(struct tty_struct *tty)
553 struct gb_tty *gb_tty = tty->driver_data;
554 unsigned char stop_char;
555 int retval;
557 if (I_IXOFF(tty)) {
558 stop_char = STOP_CHAR(tty);
559 retval = gb_tty_write(tty, &stop_char, 1);
560 if (retval <= 0)
561 return;
564 if (tty->termios.c_cflag & CRTSCTS) {
565 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
566 retval = send_control(gb_tty, gb_tty->ctrlout);
571 static void gb_tty_unthrottle(struct tty_struct *tty)
573 struct gb_tty *gb_tty = tty->driver_data;
574 unsigned char start_char;
575 int retval;
577 if (I_IXOFF(tty)) {
578 start_char = START_CHAR(tty);
579 retval = gb_tty_write(tty, &start_char, 1);
580 if (retval <= 0)
581 return;
584 if (tty->termios.c_cflag & CRTSCTS) {
585 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
586 retval = send_control(gb_tty, gb_tty->ctrlout);
590 static int get_serial_info(struct gb_tty *gb_tty,
591 struct serial_struct __user *info)
593 struct serial_struct tmp;
595 if (!info)
596 return -EINVAL;
598 memset(&tmp, 0, sizeof(tmp));
599 tmp.flags = ASYNC_LOW_LATENCY | ASYNC_SKIP_TEST;
600 tmp.type = PORT_16550A;
601 tmp.line = gb_tty->minor;
602 tmp.xmit_fifo_size = 16;
603 tmp.baud_base = 9600;
604 tmp.close_delay = gb_tty->port.close_delay / 10;
605 tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
606 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
608 if (copy_to_user(info, &tmp, sizeof(tmp)))
609 return -EFAULT;
610 return 0;
613 static int set_serial_info(struct gb_tty *gb_tty,
614 struct serial_struct __user *newinfo)
616 struct serial_struct new_serial;
617 unsigned int closing_wait;
618 unsigned int close_delay;
619 int retval = 0;
621 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
622 return -EFAULT;
624 close_delay = new_serial.close_delay * 10;
625 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
626 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
628 mutex_lock(&gb_tty->port.mutex);
629 if (!capable(CAP_SYS_ADMIN)) {
630 if ((close_delay != gb_tty->port.close_delay) ||
631 (closing_wait != gb_tty->port.closing_wait))
632 retval = -EPERM;
633 else
634 retval = -EOPNOTSUPP;
635 } else {
636 gb_tty->port.close_delay = close_delay;
637 gb_tty->port.closing_wait = closing_wait;
639 mutex_unlock(&gb_tty->port.mutex);
640 return retval;
643 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
645 int retval = 0;
646 DECLARE_WAITQUEUE(wait, current);
647 struct async_icount old;
648 struct async_icount new;
650 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
651 return -EINVAL;
653 do {
654 spin_lock_irq(&gb_tty->read_lock);
655 old = gb_tty->oldcount;
656 new = gb_tty->iocount;
657 gb_tty->oldcount = new;
658 spin_unlock_irq(&gb_tty->read_lock);
660 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
661 break;
662 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
663 break;
664 if ((arg & TIOCM_RI) && (old.rng != new.rng))
665 break;
667 add_wait_queue(&gb_tty->wioctl, &wait);
668 set_current_state(TASK_INTERRUPTIBLE);
669 schedule();
670 remove_wait_queue(&gb_tty->wioctl, &wait);
671 if (gb_tty->disconnected) {
672 if (arg & TIOCM_CD)
673 break;
674 retval = -ENODEV;
675 } else if (signal_pending(current)) {
676 retval = -ERESTARTSYS;
678 } while (!retval);
680 return retval;
684 static int get_serial_usage(struct gb_tty *gb_tty,
685 struct serial_icounter_struct __user *count)
687 struct serial_icounter_struct icount;
688 int retval = 0;
690 memset(&icount, 0, sizeof(icount));
691 icount.dsr = gb_tty->iocount.dsr;
692 icount.rng = gb_tty->iocount.rng;
693 icount.dcd = gb_tty->iocount.dcd;
694 icount.frame = gb_tty->iocount.frame;
695 icount.overrun = gb_tty->iocount.overrun;
696 icount.parity = gb_tty->iocount.parity;
697 icount.brk = gb_tty->iocount.brk;
699 if (copy_to_user(count, &icount, sizeof(icount)) > 0)
700 retval = -EFAULT;
702 return retval;
705 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
706 unsigned long arg)
708 struct gb_tty *gb_tty = tty->driver_data;
710 switch (cmd) {
711 case TIOCGSERIAL:
712 return get_serial_info(gb_tty,
713 (struct serial_struct __user *)arg);
714 case TIOCSSERIAL:
715 return set_serial_info(gb_tty,
716 (struct serial_struct __user *)arg);
717 case TIOCMIWAIT:
718 return wait_serial_change(gb_tty, arg);
719 case TIOCGICOUNT:
720 return get_serial_usage(gb_tty,
721 (struct serial_icounter_struct __user *)arg);
724 return -ENOIOCTLCMD;
727 static void gb_tty_dtr_rts(struct tty_port *port, int on)
729 struct gb_tty *gb_tty;
730 u8 newctrl;
732 gb_tty = container_of(port, struct gb_tty, port);
733 newctrl = gb_tty->ctrlout;
735 if (on)
736 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
737 else
738 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
740 gb_tty->ctrlout = newctrl;
741 send_control(gb_tty, newctrl);
744 static void gb_tty_port_shutdown(struct tty_port *port)
746 struct gb_tty *gb_tty;
747 unsigned long flags;
749 gb_tty = container_of(port, struct gb_tty, port);
751 gb_tty->close_pending = true;
753 cancel_work_sync(&gb_tty->tx_work);
755 spin_lock_irqsave(&gb_tty->write_lock, flags);
756 kfifo_reset_out(&gb_tty->write_fifo);
757 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
759 gb_tty->close_pending = false;
762 static const struct tty_operations gb_ops = {
763 .install = gb_tty_install,
764 .open = gb_tty_open,
765 .close = gb_tty_close,
766 .cleanup = gb_tty_cleanup,
767 .hangup = gb_tty_hangup,
768 .write = gb_tty_write,
769 .write_room = gb_tty_write_room,
770 .ioctl = gb_tty_ioctl,
771 .throttle = gb_tty_throttle,
772 .unthrottle = gb_tty_unthrottle,
773 .chars_in_buffer = gb_tty_chars_in_buffer,
774 .break_ctl = gb_tty_break_ctl,
775 .set_termios = gb_tty_set_termios,
776 .tiocmget = gb_tty_tiocmget,
777 .tiocmset = gb_tty_tiocmset,
780 static struct tty_port_operations gb_port_ops = {
781 .dtr_rts = gb_tty_dtr_rts,
782 .shutdown = gb_tty_port_shutdown,
785 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
786 const struct gbphy_device_id *id)
788 struct gb_connection *connection;
789 size_t max_payload;
790 struct gb_tty *gb_tty;
791 struct device *tty_dev;
792 int retval;
793 int minor;
795 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
796 if (!gb_tty)
797 return -ENOMEM;
799 connection = gb_connection_create(gbphy_dev->bundle,
800 le16_to_cpu(gbphy_dev->cport_desc->id),
801 gb_uart_request_handler);
802 if (IS_ERR(connection)) {
803 retval = PTR_ERR(connection);
804 goto exit_tty_free;
807 max_payload = gb_operation_get_payload_size_max(connection);
808 if (max_payload < sizeof(struct gb_uart_send_data_request)) {
809 retval = -EINVAL;
810 goto exit_connection_destroy;
813 gb_tty->buffer_payload_max = max_payload -
814 sizeof(struct gb_uart_send_data_request);
816 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
817 if (!gb_tty->buffer) {
818 retval = -ENOMEM;
819 goto exit_connection_destroy;
822 INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
824 retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
825 GFP_KERNEL);
826 if (retval)
827 goto exit_buf_free;
829 gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
831 minor = alloc_minor(gb_tty);
832 if (minor < 0) {
833 if (minor == -ENOSPC) {
834 dev_err(&connection->bundle->dev,
835 "no more free minor numbers\n");
836 retval = -ENODEV;
837 } else {
838 retval = minor;
840 goto exit_kfifo_free;
843 gb_tty->minor = minor;
844 spin_lock_init(&gb_tty->write_lock);
845 spin_lock_init(&gb_tty->read_lock);
846 init_waitqueue_head(&gb_tty->wioctl);
847 mutex_init(&gb_tty->mutex);
849 tty_port_init(&gb_tty->port);
850 gb_tty->port.ops = &gb_port_ops;
852 gb_tty->connection = connection;
853 gb_tty->gbphy_dev = gbphy_dev;
854 gb_connection_set_data(connection, gb_tty);
855 gb_gbphy_set_data(gbphy_dev, gb_tty);
857 retval = gb_connection_enable_tx(connection);
858 if (retval)
859 goto exit_release_minor;
861 retval = gb_gbphy_get_version(connection);
862 if (retval)
863 goto exit_connection_disable;
865 send_control(gb_tty, gb_tty->ctrlout);
867 /* initialize the uart to be 9600n81 */
868 gb_tty->line_coding.rate = cpu_to_le32(9600);
869 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
870 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
871 gb_tty->line_coding.data_bits = 8;
872 send_line_coding(gb_tty);
874 retval = gb_connection_enable(connection);
875 if (retval)
876 goto exit_connection_disable;
878 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
879 &gbphy_dev->dev);
880 if (IS_ERR(tty_dev)) {
881 retval = PTR_ERR(tty_dev);
882 goto exit_connection_disable;
886 return 0;
888 exit_connection_disable:
889 gb_connection_disable(connection);
890 exit_release_minor:
891 release_minor(gb_tty);
892 exit_kfifo_free:
893 kfifo_free(&gb_tty->write_fifo);
894 exit_buf_free:
895 kfree(gb_tty->buffer);
896 exit_connection_destroy:
897 gb_connection_destroy(connection);
898 exit_tty_free:
899 kfree(gb_tty);
901 return retval;
904 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
906 struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
907 struct gb_connection *connection = gb_tty->connection;
908 struct tty_struct *tty;
910 mutex_lock(&gb_tty->mutex);
911 gb_tty->disconnected = true;
913 wake_up_all(&gb_tty->wioctl);
914 mutex_unlock(&gb_tty->mutex);
916 tty = tty_port_tty_get(&gb_tty->port);
917 if (tty) {
918 tty_vhangup(tty);
919 tty_kref_put(tty);
922 gb_connection_disable_rx(connection);
923 tty_unregister_device(gb_tty_driver, gb_tty->minor);
925 /* FIXME - free transmit / receive buffers */
927 gb_connection_disable(connection);
928 tty_port_destroy(&gb_tty->port);
929 gb_connection_destroy(connection);
930 kfifo_free(&gb_tty->write_fifo);
931 kfree(gb_tty->buffer);
932 kfree(gb_tty);
935 static int gb_tty_init(void)
937 int retval = 0;
939 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
940 if (IS_ERR(gb_tty_driver)) {
941 pr_err("Can not allocate tty driver\n");
942 retval = -ENOMEM;
943 goto fail_unregister_dev;
946 gb_tty_driver->driver_name = "gb";
947 gb_tty_driver->name = GB_NAME;
948 gb_tty_driver->major = 0;
949 gb_tty_driver->minor_start = 0;
950 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
951 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
952 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
953 gb_tty_driver->init_termios = tty_std_termios;
954 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
955 tty_set_operations(gb_tty_driver, &gb_ops);
957 retval = tty_register_driver(gb_tty_driver);
958 if (retval) {
959 pr_err("Can not register tty driver: %d\n", retval);
960 goto fail_put_gb_tty;
963 return 0;
965 fail_put_gb_tty:
966 put_tty_driver(gb_tty_driver);
967 fail_unregister_dev:
968 return retval;
971 static void gb_tty_exit(void)
973 tty_unregister_driver(gb_tty_driver);
974 put_tty_driver(gb_tty_driver);
975 idr_destroy(&tty_minors);
978 static const struct gbphy_device_id gb_uart_id_table[] = {
979 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
980 { },
982 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
984 static struct gbphy_driver uart_driver = {
985 .name = "uart",
986 .probe = gb_uart_probe,
987 .remove = gb_uart_remove,
988 .id_table = gb_uart_id_table,
991 static int gb_uart_driver_init(void)
993 int ret;
995 ret = gb_tty_init();
996 if (ret)
997 return ret;
999 ret = gb_gbphy_register(&uart_driver);
1000 if (ret) {
1001 gb_tty_exit();
1002 return ret;
1005 return 0;
1007 module_init(gb_uart_driver_init);
1009 static void gb_uart_driver_exit(void)
1011 gb_gbphy_deregister(&uart_driver);
1012 gb_tty_exit();
1015 module_exit(gb_uart_driver_exit);
1016 MODULE_LICENSE("GPL v2");