treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / staging / greybus / uart.c
blob55c51143bb0940998b19396a56b39cba3bbd7447
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * UART driver for the Greybus "generic" UART module.
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
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/signal.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/idr.h>
26 #include <linux/fs.h>
27 #include <linux/kdev_t.h>
28 #include <linux/kfifo.h>
29 #include <linux/workqueue.h>
30 #include <linux/completion.h>
31 #include <linux/greybus.h>
33 #include "gbphy.h"
35 #define GB_NUM_MINORS 16 /* 16 is more than enough */
36 #define GB_NAME "ttyGB"
38 #define GB_UART_WRITE_FIFO_SIZE PAGE_SIZE
39 #define GB_UART_WRITE_ROOM_MARGIN 1 /* leave some space in fifo */
40 #define GB_UART_FIRMWARE_CREDITS 4096
41 #define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC 10000
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;
74 struct completion credits_complete;
77 static struct tty_driver *gb_tty_driver;
78 static DEFINE_IDR(tty_minors);
79 static DEFINE_MUTEX(table_lock);
81 static int gb_uart_receive_data_handler(struct gb_operation *op)
83 struct gb_connection *connection = op->connection;
84 struct gb_tty *gb_tty = gb_connection_get_data(connection);
85 struct tty_port *port = &gb_tty->port;
86 struct gb_message *request = op->request;
87 struct gb_uart_recv_data_request *receive_data;
88 u16 recv_data_size;
89 int count;
90 unsigned long tty_flags = TTY_NORMAL;
92 if (request->payload_size < sizeof(*receive_data)) {
93 dev_err(&gb_tty->gbphy_dev->dev,
94 "short receive-data request received (%zu < %zu)\n",
95 request->payload_size, sizeof(*receive_data));
96 return -EINVAL;
99 receive_data = op->request->payload;
100 recv_data_size = le16_to_cpu(receive_data->size);
102 if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
103 dev_err(&gb_tty->gbphy_dev->dev,
104 "malformed receive-data request received (%u != %zu)\n",
105 recv_data_size,
106 request->payload_size - sizeof(*receive_data));
107 return -EINVAL;
110 if (!recv_data_size)
111 return -EINVAL;
113 if (receive_data->flags) {
114 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
115 tty_flags = TTY_BREAK;
116 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
117 tty_flags = TTY_PARITY;
118 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
119 tty_flags = TTY_FRAME;
121 /* overrun is special, not associated with a char */
122 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
123 tty_insert_flip_char(port, 0, TTY_OVERRUN);
125 count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
126 tty_flags, recv_data_size);
127 if (count != recv_data_size) {
128 dev_err(&gb_tty->gbphy_dev->dev,
129 "UART: RX 0x%08x bytes only wrote 0x%08x\n",
130 recv_data_size, count);
132 if (count)
133 tty_flip_buffer_push(port);
134 return 0;
137 static int gb_uart_serial_state_handler(struct gb_operation *op)
139 struct gb_connection *connection = op->connection;
140 struct gb_tty *gb_tty = gb_connection_get_data(connection);
141 struct gb_message *request = op->request;
142 struct gb_uart_serial_state_request *serial_state;
144 if (request->payload_size < sizeof(*serial_state)) {
145 dev_err(&gb_tty->gbphy_dev->dev,
146 "short serial-state event received (%zu < %zu)\n",
147 request->payload_size, sizeof(*serial_state));
148 return -EINVAL;
151 serial_state = request->payload;
152 gb_tty->ctrlin = serial_state->control;
154 return 0;
157 static int gb_uart_receive_credits_handler(struct gb_operation *op)
159 struct gb_connection *connection = op->connection;
160 struct gb_tty *gb_tty = gb_connection_get_data(connection);
161 struct gb_message *request = op->request;
162 struct gb_uart_receive_credits_request *credit_request;
163 unsigned long flags;
164 unsigned int incoming_credits;
165 int ret = 0;
167 if (request->payload_size < sizeof(*credit_request)) {
168 dev_err(&gb_tty->gbphy_dev->dev,
169 "short receive_credits event received (%zu < %zu)\n",
170 request->payload_size,
171 sizeof(*credit_request));
172 return -EINVAL;
175 credit_request = request->payload;
176 incoming_credits = le16_to_cpu(credit_request->count);
178 spin_lock_irqsave(&gb_tty->write_lock, flags);
179 gb_tty->credits += incoming_credits;
180 if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
181 gb_tty->credits -= incoming_credits;
182 ret = -EINVAL;
184 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
186 if (ret) {
187 dev_err(&gb_tty->gbphy_dev->dev,
188 "invalid number of incoming credits: %d\n",
189 incoming_credits);
190 return ret;
193 if (!gb_tty->close_pending)
194 schedule_work(&gb_tty->tx_work);
197 * the port the tty layer may be waiting for credits
199 tty_port_tty_wakeup(&gb_tty->port);
201 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
202 complete(&gb_tty->credits_complete);
204 return ret;
207 static int gb_uart_request_handler(struct gb_operation *op)
209 struct gb_connection *connection = op->connection;
210 struct gb_tty *gb_tty = gb_connection_get_data(connection);
211 int type = op->type;
212 int ret;
214 switch (type) {
215 case GB_UART_TYPE_RECEIVE_DATA:
216 ret = gb_uart_receive_data_handler(op);
217 break;
218 case GB_UART_TYPE_SERIAL_STATE:
219 ret = gb_uart_serial_state_handler(op);
220 break;
221 case GB_UART_TYPE_RECEIVE_CREDITS:
222 ret = gb_uart_receive_credits_handler(op);
223 break;
224 default:
225 dev_err(&gb_tty->gbphy_dev->dev,
226 "unsupported unsolicited request: 0x%02x\n", type);
227 ret = -EINVAL;
230 return ret;
233 static void gb_uart_tx_write_work(struct work_struct *work)
235 struct gb_uart_send_data_request *request;
236 struct gb_tty *gb_tty;
237 unsigned long flags;
238 unsigned int send_size;
239 int ret;
241 gb_tty = container_of(work, struct gb_tty, tx_work);
242 request = gb_tty->buffer;
244 while (1) {
245 if (gb_tty->close_pending)
246 break;
248 spin_lock_irqsave(&gb_tty->write_lock, flags);
249 send_size = gb_tty->buffer_payload_max;
250 if (send_size > gb_tty->credits)
251 send_size = gb_tty->credits;
253 send_size = kfifo_out_peek(&gb_tty->write_fifo,
254 &request->data[0],
255 send_size);
256 if (!send_size) {
257 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
258 break;
261 gb_tty->credits -= send_size;
262 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
264 request->size = cpu_to_le16(send_size);
265 ret = gb_operation_sync(gb_tty->connection,
266 GB_UART_TYPE_SEND_DATA,
267 request, sizeof(*request) + send_size,
268 NULL, 0);
269 if (ret) {
270 dev_err(&gb_tty->gbphy_dev->dev,
271 "send data error: %d\n", ret);
272 spin_lock_irqsave(&gb_tty->write_lock, flags);
273 gb_tty->credits += send_size;
274 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
275 if (!gb_tty->close_pending)
276 schedule_work(work);
277 return;
280 spin_lock_irqsave(&gb_tty->write_lock, flags);
281 ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
282 send_size);
283 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
285 tty_port_tty_wakeup(&gb_tty->port);
289 static int send_line_coding(struct gb_tty *tty)
291 struct gb_uart_set_line_coding_request request;
293 memcpy(&request, &tty->line_coding,
294 sizeof(tty->line_coding));
295 return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
296 &request, sizeof(request), NULL, 0);
299 static int send_control(struct gb_tty *gb_tty, u8 control)
301 struct gb_uart_set_control_line_state_request request;
303 request.control = control;
304 return gb_operation_sync(gb_tty->connection,
305 GB_UART_TYPE_SET_CONTROL_LINE_STATE,
306 &request, sizeof(request), NULL, 0);
309 static int send_break(struct gb_tty *gb_tty, u8 state)
311 struct gb_uart_set_break_request request;
313 if ((state != 0) && (state != 1)) {
314 dev_err(&gb_tty->gbphy_dev->dev,
315 "invalid break state of %d\n", state);
316 return -EINVAL;
319 request.state = state;
320 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
321 &request, sizeof(request), NULL, 0);
324 static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
326 int ret;
328 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
329 return 0;
331 ret = wait_for_completion_timeout(&gb_tty->credits_complete,
332 msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
333 if (!ret) {
334 dev_err(&gb_tty->gbphy_dev->dev,
335 "time out waiting for credits\n");
336 return -ETIMEDOUT;
339 return 0;
342 static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
344 struct gb_uart_serial_flush_request request;
346 request.flags = flags;
347 return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
348 &request, sizeof(request), NULL, 0);
351 static struct gb_tty *get_gb_by_minor(unsigned int minor)
353 struct gb_tty *gb_tty;
355 mutex_lock(&table_lock);
356 gb_tty = idr_find(&tty_minors, minor);
357 if (gb_tty) {
358 mutex_lock(&gb_tty->mutex);
359 if (gb_tty->disconnected) {
360 mutex_unlock(&gb_tty->mutex);
361 gb_tty = NULL;
362 } else {
363 tty_port_get(&gb_tty->port);
364 mutex_unlock(&gb_tty->mutex);
367 mutex_unlock(&table_lock);
368 return gb_tty;
371 static int alloc_minor(struct gb_tty *gb_tty)
373 int minor;
375 mutex_lock(&table_lock);
376 minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
377 mutex_unlock(&table_lock);
378 if (minor >= 0)
379 gb_tty->minor = minor;
380 return minor;
383 static void release_minor(struct gb_tty *gb_tty)
385 int minor = gb_tty->minor;
387 gb_tty->minor = 0; /* Maybe should use an invalid value instead */
388 mutex_lock(&table_lock);
389 idr_remove(&tty_minors, minor);
390 mutex_unlock(&table_lock);
393 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
395 struct gb_tty *gb_tty;
396 int retval;
398 gb_tty = get_gb_by_minor(tty->index);
399 if (!gb_tty)
400 return -ENODEV;
402 retval = tty_standard_install(driver, tty);
403 if (retval)
404 goto error;
406 tty->driver_data = gb_tty;
407 return 0;
408 error:
409 tty_port_put(&gb_tty->port);
410 return retval;
413 static int gb_tty_open(struct tty_struct *tty, struct file *file)
415 struct gb_tty *gb_tty = tty->driver_data;
417 return tty_port_open(&gb_tty->port, tty, file);
420 static void gb_tty_close(struct tty_struct *tty, struct file *file)
422 struct gb_tty *gb_tty = tty->driver_data;
424 tty_port_close(&gb_tty->port, tty, file);
427 static void gb_tty_cleanup(struct tty_struct *tty)
429 struct gb_tty *gb_tty = tty->driver_data;
431 tty_port_put(&gb_tty->port);
434 static void gb_tty_hangup(struct tty_struct *tty)
436 struct gb_tty *gb_tty = tty->driver_data;
438 tty_port_hangup(&gb_tty->port);
441 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
442 int count)
444 struct gb_tty *gb_tty = tty->driver_data;
446 count = kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
447 &gb_tty->write_lock);
448 if (count && !gb_tty->close_pending)
449 schedule_work(&gb_tty->tx_work);
451 return count;
454 static int gb_tty_write_room(struct tty_struct *tty)
456 struct gb_tty *gb_tty = tty->driver_data;
457 unsigned long flags;
458 int room;
460 spin_lock_irqsave(&gb_tty->write_lock, flags);
461 room = kfifo_avail(&gb_tty->write_fifo);
462 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
464 room -= GB_UART_WRITE_ROOM_MARGIN;
465 if (room < 0)
466 return 0;
468 return room;
471 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
473 struct gb_tty *gb_tty = tty->driver_data;
474 unsigned long flags;
475 int chars;
477 spin_lock_irqsave(&gb_tty->write_lock, flags);
478 chars = kfifo_len(&gb_tty->write_fifo);
479 if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
480 chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
481 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
483 return chars;
486 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
488 struct gb_tty *gb_tty = tty->driver_data;
490 return send_break(gb_tty, state ? 1 : 0);
493 static void gb_tty_set_termios(struct tty_struct *tty,
494 struct ktermios *termios_old)
496 struct gb_tty *gb_tty = tty->driver_data;
497 struct ktermios *termios = &tty->termios;
498 struct gb_tty_line_coding newline;
499 u8 newctrl = gb_tty->ctrlout;
501 newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
502 newline.format = termios->c_cflag & CSTOPB ?
503 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
504 newline.parity = termios->c_cflag & PARENB ?
505 (termios->c_cflag & PARODD ? 1 : 2) +
506 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
508 switch (termios->c_cflag & CSIZE) {
509 case CS5:
510 newline.data_bits = 5;
511 break;
512 case CS6:
513 newline.data_bits = 6;
514 break;
515 case CS7:
516 newline.data_bits = 7;
517 break;
518 case CS8:
519 default:
520 newline.data_bits = 8;
521 break;
524 /* FIXME: needs to clear unsupported bits in the termios */
525 gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
527 if (C_BAUD(tty) == B0) {
528 newline.rate = gb_tty->line_coding.rate;
529 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
530 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
531 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
534 if (newctrl != gb_tty->ctrlout) {
535 gb_tty->ctrlout = newctrl;
536 send_control(gb_tty, newctrl);
539 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
540 newline.flow_control |= GB_SERIAL_AUTO_RTSCTS_EN;
541 else
542 newline.flow_control &= ~GB_SERIAL_AUTO_RTSCTS_EN;
544 if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
545 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
546 send_line_coding(gb_tty);
550 static int gb_tty_tiocmget(struct tty_struct *tty)
552 struct gb_tty *gb_tty = tty->driver_data;
554 return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
555 (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
556 (gb_tty->ctrlin & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
557 (gb_tty->ctrlin & GB_UART_CTRL_RI ? TIOCM_RI : 0) |
558 (gb_tty->ctrlin & GB_UART_CTRL_DCD ? TIOCM_CD : 0) |
559 TIOCM_CTS;
562 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
563 unsigned int clear)
565 struct gb_tty *gb_tty = tty->driver_data;
566 u8 newctrl = gb_tty->ctrlout;
568 set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
569 (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
570 clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
571 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
573 newctrl = (newctrl & ~clear) | set;
574 if (gb_tty->ctrlout == newctrl)
575 return 0;
577 gb_tty->ctrlout = newctrl;
578 return send_control(gb_tty, newctrl);
581 static void gb_tty_throttle(struct tty_struct *tty)
583 struct gb_tty *gb_tty = tty->driver_data;
584 unsigned char stop_char;
585 int retval;
587 if (I_IXOFF(tty)) {
588 stop_char = STOP_CHAR(tty);
589 retval = gb_tty_write(tty, &stop_char, 1);
590 if (retval <= 0)
591 return;
594 if (tty->termios.c_cflag & CRTSCTS) {
595 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
596 retval = send_control(gb_tty, gb_tty->ctrlout);
600 static void gb_tty_unthrottle(struct tty_struct *tty)
602 struct gb_tty *gb_tty = tty->driver_data;
603 unsigned char start_char;
604 int retval;
606 if (I_IXOFF(tty)) {
607 start_char = START_CHAR(tty);
608 retval = gb_tty_write(tty, &start_char, 1);
609 if (retval <= 0)
610 return;
613 if (tty->termios.c_cflag & CRTSCTS) {
614 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
615 retval = send_control(gb_tty, gb_tty->ctrlout);
619 static int get_serial_info(struct tty_struct *tty,
620 struct serial_struct *ss)
622 struct gb_tty *gb_tty = tty->driver_data;
624 ss->type = PORT_16550A;
625 ss->line = gb_tty->minor;
626 ss->xmit_fifo_size = 16;
627 ss->baud_base = 9600;
628 ss->close_delay = gb_tty->port.close_delay / 10;
629 ss->closing_wait =
630 gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
631 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
632 return 0;
635 static int set_serial_info(struct tty_struct *tty,
636 struct serial_struct *ss)
638 struct gb_tty *gb_tty = tty->driver_data;
639 unsigned int closing_wait;
640 unsigned int close_delay;
641 int retval = 0;
643 close_delay = ss->close_delay * 10;
644 closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
645 ASYNC_CLOSING_WAIT_NONE : ss->closing_wait * 10;
647 mutex_lock(&gb_tty->port.mutex);
648 if (!capable(CAP_SYS_ADMIN)) {
649 if ((close_delay != gb_tty->port.close_delay) ||
650 (closing_wait != gb_tty->port.closing_wait))
651 retval = -EPERM;
652 else
653 retval = -EOPNOTSUPP;
654 } else {
655 gb_tty->port.close_delay = close_delay;
656 gb_tty->port.closing_wait = closing_wait;
658 mutex_unlock(&gb_tty->port.mutex);
659 return retval;
662 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
664 int retval = 0;
665 DECLARE_WAITQUEUE(wait, current);
666 struct async_icount old;
667 struct async_icount new;
669 if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
670 return -EINVAL;
672 do {
673 spin_lock_irq(&gb_tty->read_lock);
674 old = gb_tty->oldcount;
675 new = gb_tty->iocount;
676 gb_tty->oldcount = new;
677 spin_unlock_irq(&gb_tty->read_lock);
679 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
680 break;
681 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
682 break;
683 if ((arg & TIOCM_RI) && (old.rng != new.rng))
684 break;
686 add_wait_queue(&gb_tty->wioctl, &wait);
687 set_current_state(TASK_INTERRUPTIBLE);
688 schedule();
689 remove_wait_queue(&gb_tty->wioctl, &wait);
690 if (gb_tty->disconnected) {
691 if (arg & TIOCM_CD)
692 break;
693 retval = -ENODEV;
694 } else if (signal_pending(current)) {
695 retval = -ERESTARTSYS;
697 } while (!retval);
699 return retval;
702 static int gb_tty_get_icount(struct tty_struct *tty,
703 struct serial_icounter_struct *icount)
705 struct gb_tty *gb_tty = tty->driver_data;
707 icount->dsr = gb_tty->iocount.dsr;
708 icount->rng = gb_tty->iocount.rng;
709 icount->dcd = gb_tty->iocount.dcd;
710 icount->frame = gb_tty->iocount.frame;
711 icount->overrun = gb_tty->iocount.overrun;
712 icount->parity = gb_tty->iocount.parity;
713 icount->brk = gb_tty->iocount.brk;
715 return 0;
718 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
719 unsigned long arg)
721 struct gb_tty *gb_tty = tty->driver_data;
723 switch (cmd) {
724 case TIOCMIWAIT:
725 return wait_serial_change(gb_tty, arg);
728 return -ENOIOCTLCMD;
731 static void gb_tty_dtr_rts(struct tty_port *port, int on)
733 struct gb_tty *gb_tty;
734 u8 newctrl;
736 gb_tty = container_of(port, struct gb_tty, port);
737 newctrl = gb_tty->ctrlout;
739 if (on)
740 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
741 else
742 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
744 gb_tty->ctrlout = newctrl;
745 send_control(gb_tty, newctrl);
748 static int gb_tty_port_activate(struct tty_port *port,
749 struct tty_struct *tty)
751 struct gb_tty *gb_tty;
753 gb_tty = container_of(port, struct gb_tty, port);
755 return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
758 static void gb_tty_port_shutdown(struct tty_port *port)
760 struct gb_tty *gb_tty;
761 unsigned long flags;
762 int ret;
764 gb_tty = container_of(port, struct gb_tty, port);
766 gb_tty->close_pending = true;
768 cancel_work_sync(&gb_tty->tx_work);
770 spin_lock_irqsave(&gb_tty->write_lock, flags);
771 kfifo_reset_out(&gb_tty->write_fifo);
772 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
774 if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
775 goto out;
777 ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
778 if (ret) {
779 dev_err(&gb_tty->gbphy_dev->dev,
780 "error flushing transmitter: %d\n", ret);
783 gb_uart_wait_for_all_credits(gb_tty);
785 out:
786 gb_tty->close_pending = false;
788 gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
791 static const struct tty_operations gb_ops = {
792 .install = gb_tty_install,
793 .open = gb_tty_open,
794 .close = gb_tty_close,
795 .cleanup = gb_tty_cleanup,
796 .hangup = gb_tty_hangup,
797 .write = gb_tty_write,
798 .write_room = gb_tty_write_room,
799 .ioctl = gb_tty_ioctl,
800 .throttle = gb_tty_throttle,
801 .unthrottle = gb_tty_unthrottle,
802 .chars_in_buffer = gb_tty_chars_in_buffer,
803 .break_ctl = gb_tty_break_ctl,
804 .set_termios = gb_tty_set_termios,
805 .tiocmget = gb_tty_tiocmget,
806 .tiocmset = gb_tty_tiocmset,
807 .get_icount = gb_tty_get_icount,
808 .set_serial = set_serial_info,
809 .get_serial = get_serial_info,
812 static const struct tty_port_operations gb_port_ops = {
813 .dtr_rts = gb_tty_dtr_rts,
814 .activate = gb_tty_port_activate,
815 .shutdown = gb_tty_port_shutdown,
818 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
819 const struct gbphy_device_id *id)
821 struct gb_connection *connection;
822 size_t max_payload;
823 struct gb_tty *gb_tty;
824 struct device *tty_dev;
825 int retval;
826 int minor;
828 gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
829 if (!gb_tty)
830 return -ENOMEM;
832 connection = gb_connection_create(gbphy_dev->bundle,
833 le16_to_cpu(gbphy_dev->cport_desc->id),
834 gb_uart_request_handler);
835 if (IS_ERR(connection)) {
836 retval = PTR_ERR(connection);
837 goto exit_tty_free;
840 max_payload = gb_operation_get_payload_size_max(connection);
841 if (max_payload < sizeof(struct gb_uart_send_data_request)) {
842 retval = -EINVAL;
843 goto exit_connection_destroy;
846 gb_tty->buffer_payload_max = max_payload -
847 sizeof(struct gb_uart_send_data_request);
849 gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
850 if (!gb_tty->buffer) {
851 retval = -ENOMEM;
852 goto exit_connection_destroy;
855 INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
857 retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
858 GFP_KERNEL);
859 if (retval)
860 goto exit_buf_free;
862 gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
863 init_completion(&gb_tty->credits_complete);
865 minor = alloc_minor(gb_tty);
866 if (minor < 0) {
867 if (minor == -ENOSPC) {
868 dev_err(&gbphy_dev->dev,
869 "no more free minor numbers\n");
870 retval = -ENODEV;
871 } else {
872 retval = minor;
874 goto exit_kfifo_free;
877 gb_tty->minor = minor;
878 spin_lock_init(&gb_tty->write_lock);
879 spin_lock_init(&gb_tty->read_lock);
880 init_waitqueue_head(&gb_tty->wioctl);
881 mutex_init(&gb_tty->mutex);
883 tty_port_init(&gb_tty->port);
884 gb_tty->port.ops = &gb_port_ops;
886 gb_tty->connection = connection;
887 gb_tty->gbphy_dev = gbphy_dev;
888 gb_connection_set_data(connection, gb_tty);
889 gb_gbphy_set_data(gbphy_dev, gb_tty);
891 retval = gb_connection_enable_tx(connection);
892 if (retval)
893 goto exit_release_minor;
895 send_control(gb_tty, gb_tty->ctrlout);
897 /* initialize the uart to be 9600n81 */
898 gb_tty->line_coding.rate = cpu_to_le32(9600);
899 gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
900 gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
901 gb_tty->line_coding.data_bits = 8;
902 send_line_coding(gb_tty);
904 retval = gb_connection_enable(connection);
905 if (retval)
906 goto exit_connection_disable;
908 tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
909 &gbphy_dev->dev);
910 if (IS_ERR(tty_dev)) {
911 retval = PTR_ERR(tty_dev);
912 goto exit_connection_disable;
915 gbphy_runtime_put_autosuspend(gbphy_dev);
916 return 0;
918 exit_connection_disable:
919 gb_connection_disable(connection);
920 exit_release_minor:
921 release_minor(gb_tty);
922 exit_kfifo_free:
923 kfifo_free(&gb_tty->write_fifo);
924 exit_buf_free:
925 kfree(gb_tty->buffer);
926 exit_connection_destroy:
927 gb_connection_destroy(connection);
928 exit_tty_free:
929 kfree(gb_tty);
931 return retval;
934 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
936 struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
937 struct gb_connection *connection = gb_tty->connection;
938 struct tty_struct *tty;
939 int ret;
941 ret = gbphy_runtime_get_sync(gbphy_dev);
942 if (ret)
943 gbphy_runtime_get_noresume(gbphy_dev);
945 mutex_lock(&gb_tty->mutex);
946 gb_tty->disconnected = true;
948 wake_up_all(&gb_tty->wioctl);
949 mutex_unlock(&gb_tty->mutex);
951 tty = tty_port_tty_get(&gb_tty->port);
952 if (tty) {
953 tty_vhangup(tty);
954 tty_kref_put(tty);
957 gb_connection_disable_rx(connection);
958 tty_unregister_device(gb_tty_driver, gb_tty->minor);
960 /* FIXME - free transmit / receive buffers */
962 gb_connection_disable(connection);
963 tty_port_destroy(&gb_tty->port);
964 gb_connection_destroy(connection);
965 release_minor(gb_tty);
966 kfifo_free(&gb_tty->write_fifo);
967 kfree(gb_tty->buffer);
968 kfree(gb_tty);
971 static int gb_tty_init(void)
973 int retval = 0;
975 gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
976 if (IS_ERR(gb_tty_driver)) {
977 pr_err("Can not allocate tty driver\n");
978 retval = -ENOMEM;
979 goto fail_unregister_dev;
982 gb_tty_driver->driver_name = "gb";
983 gb_tty_driver->name = GB_NAME;
984 gb_tty_driver->major = 0;
985 gb_tty_driver->minor_start = 0;
986 gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
987 gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
988 gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
989 gb_tty_driver->init_termios = tty_std_termios;
990 gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
991 CREAD | HUPCL | CLOCAL;
992 tty_set_operations(gb_tty_driver, &gb_ops);
994 retval = tty_register_driver(gb_tty_driver);
995 if (retval) {
996 pr_err("Can not register tty driver: %d\n", retval);
997 goto fail_put_gb_tty;
1000 return 0;
1002 fail_put_gb_tty:
1003 put_tty_driver(gb_tty_driver);
1004 fail_unregister_dev:
1005 return retval;
1008 static void gb_tty_exit(void)
1010 tty_unregister_driver(gb_tty_driver);
1011 put_tty_driver(gb_tty_driver);
1012 idr_destroy(&tty_minors);
1015 static const struct gbphy_device_id gb_uart_id_table[] = {
1016 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
1017 { },
1019 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
1021 static struct gbphy_driver uart_driver = {
1022 .name = "uart",
1023 .probe = gb_uart_probe,
1024 .remove = gb_uart_remove,
1025 .id_table = gb_uart_id_table,
1028 static int gb_uart_driver_init(void)
1030 int ret;
1032 ret = gb_tty_init();
1033 if (ret)
1034 return ret;
1036 ret = gb_gbphy_register(&uart_driver);
1037 if (ret) {
1038 gb_tty_exit();
1039 return ret;
1042 return 0;
1044 module_init(gb_uart_driver_init);
1046 static void gb_uart_driver_exit(void)
1048 gb_gbphy_deregister(&uart_driver);
1049 gb_tty_exit();
1052 module_exit(gb_uart_driver_exit);
1053 MODULE_LICENSE("GPL v2");