GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / greybus / uart.c
1 /*
2  * UART driver for the Greybus "generic" UART module.
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  *
9  * Heavily based on drivers/usb/class/cdc-acm.c and
10  * drivers/usb/serial/usb-serial.c.
11  */
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/sched/signal.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/idr.h>
27 #include <linux/fs.h>
28 #include <linux/kdev_t.h>
29 #include <linux/kfifo.h>
30 #include <linux/workqueue.h>
31 #include <linux/completion.h>
32
33 #include "greybus.h"
34 #include "gbphy.h"
35
36 #define GB_NUM_MINORS   16      /* 16 is more than enough */
37 #define GB_NAME         "ttyGB"
38
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
42 #define GB_UART_CREDIT_WAIT_TIMEOUT_MSEC        10000
43
44 struct gb_tty_line_coding {
45         __le32  rate;
46         __u8    format;
47         __u8    parity;
48         __u8    data_bits;
49         __u8    flow_control;
50 };
51
52 struct gb_tty {
53         struct gbphy_device *gbphy_dev;
54         struct tty_port port;
55         void *buffer;
56         size_t buffer_payload_max;
57         struct gb_connection *connection;
58         u16 cport_id;
59         unsigned int minor;
60         unsigned char clocal;
61         bool disconnected;
62         spinlock_t read_lock;
63         spinlock_t write_lock;
64         struct async_icount iocount;
65         struct async_icount oldcount;
66         wait_queue_head_t wioctl;
67         struct mutex mutex;
68         u8 ctrlin;      /* input control lines */
69         u8 ctrlout;     /* output control lines */
70         struct gb_tty_line_coding line_coding;
71         struct work_struct tx_work;
72         struct kfifo write_fifo;
73         bool close_pending;
74         unsigned int credits;
75         struct completion credits_complete;
76 };
77
78 static struct tty_driver *gb_tty_driver;
79 static DEFINE_IDR(tty_minors);
80 static DEFINE_MUTEX(table_lock);
81
82 static int gb_uart_receive_data_handler(struct gb_operation *op)
83 {
84         struct gb_connection *connection = op->connection;
85         struct gb_tty *gb_tty = gb_connection_get_data(connection);
86         struct tty_port *port = &gb_tty->port;
87         struct gb_message *request = op->request;
88         struct gb_uart_recv_data_request *receive_data;
89         u16 recv_data_size;
90         int count;
91         unsigned long tty_flags = TTY_NORMAL;
92
93         if (request->payload_size < sizeof(*receive_data)) {
94                 dev_err(&gb_tty->gbphy_dev->dev,
95                         "short receive-data request received (%zu < %zu)\n",
96                         request->payload_size, sizeof(*receive_data));
97                 return -EINVAL;
98         }
99
100         receive_data = op->request->payload;
101         recv_data_size = le16_to_cpu(receive_data->size);
102
103         if (recv_data_size != request->payload_size - sizeof(*receive_data)) {
104                 dev_err(&gb_tty->gbphy_dev->dev,
105                         "malformed receive-data request received (%u != %zu)\n",
106                         recv_data_size,
107                         request->payload_size - sizeof(*receive_data));
108                 return -EINVAL;
109         }
110
111         if (!recv_data_size)
112                 return -EINVAL;
113
114         if (receive_data->flags) {
115                 if (receive_data->flags & GB_UART_RECV_FLAG_BREAK)
116                         tty_flags = TTY_BREAK;
117                 else if (receive_data->flags & GB_UART_RECV_FLAG_PARITY)
118                         tty_flags = TTY_PARITY;
119                 else if (receive_data->flags & GB_UART_RECV_FLAG_FRAMING)
120                         tty_flags = TTY_FRAME;
121
122                 /* overrun is special, not associated with a char */
123                 if (receive_data->flags & GB_UART_RECV_FLAG_OVERRUN)
124                         tty_insert_flip_char(port, 0, TTY_OVERRUN);
125         }
126         count = tty_insert_flip_string_fixed_flag(port, receive_data->data,
127                                                   tty_flags, recv_data_size);
128         if (count != recv_data_size) {
129                 dev_err(&gb_tty->gbphy_dev->dev,
130                         "UART: RX 0x%08x bytes only wrote 0x%08x\n",
131                         recv_data_size, count);
132         }
133         if (count)
134                 tty_flip_buffer_push(port);
135         return 0;
136 }
137
138 static int gb_uart_serial_state_handler(struct gb_operation *op)
139 {
140         struct gb_connection *connection = op->connection;
141         struct gb_tty *gb_tty = gb_connection_get_data(connection);
142         struct gb_message *request = op->request;
143         struct gb_uart_serial_state_request *serial_state;
144
145         if (request->payload_size < sizeof(*serial_state)) {
146                 dev_err(&gb_tty->gbphy_dev->dev,
147                         "short serial-state event received (%zu < %zu)\n",
148                         request->payload_size, sizeof(*serial_state));
149                 return -EINVAL;
150         }
151
152         serial_state = request->payload;
153         gb_tty->ctrlin = serial_state->control;
154
155         return 0;
156 }
157
158 static int gb_uart_receive_credits_handler(struct gb_operation *op)
159 {
160         struct gb_connection *connection = op->connection;
161         struct gb_tty *gb_tty = gb_connection_get_data(connection);
162         struct gb_message *request = op->request;
163         struct gb_uart_receive_credits_request *credit_request;
164         unsigned long flags;
165         unsigned int incoming_credits;
166         int ret = 0;
167
168         if (request->payload_size < sizeof(*credit_request)) {
169                 dev_err(&gb_tty->gbphy_dev->dev,
170                         "short receive_credits event received (%zu < %zu)\n",
171                         request->payload_size,
172                         sizeof(*credit_request));
173                 return -EINVAL;
174         }
175
176         credit_request = request->payload;
177         incoming_credits = le16_to_cpu(credit_request->count);
178
179         spin_lock_irqsave(&gb_tty->write_lock, flags);
180         gb_tty->credits += incoming_credits;
181         if (gb_tty->credits > GB_UART_FIRMWARE_CREDITS) {
182                 gb_tty->credits -= incoming_credits;
183                 ret = -EINVAL;
184         }
185         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
186
187         if (ret) {
188                 dev_err(&gb_tty->gbphy_dev->dev,
189                         "invalid number of incoming credits: %d\n",
190                         incoming_credits);
191                 return ret;
192         }
193
194         if (!gb_tty->close_pending)
195                 schedule_work(&gb_tty->tx_work);
196
197         /*
198          * the port the tty layer may be waiting for credits
199          */
200         tty_port_tty_wakeup(&gb_tty->port);
201
202         if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
203                 complete(&gb_tty->credits_complete);
204
205         return ret;
206 }
207
208 static int gb_uart_request_handler(struct gb_operation *op)
209 {
210         struct gb_connection *connection = op->connection;
211         struct gb_tty *gb_tty = gb_connection_get_data(connection);
212         int type = op->type;
213         int ret;
214
215         switch (type) {
216         case GB_UART_TYPE_RECEIVE_DATA:
217                 ret = gb_uart_receive_data_handler(op);
218                 break;
219         case GB_UART_TYPE_SERIAL_STATE:
220                 ret = gb_uart_serial_state_handler(op);
221                 break;
222         case GB_UART_TYPE_RECEIVE_CREDITS:
223                 ret = gb_uart_receive_credits_handler(op);
224                 break;
225         default:
226                 dev_err(&gb_tty->gbphy_dev->dev,
227                         "unsupported unsolicited request: 0x%02x\n", type);
228                 ret = -EINVAL;
229         }
230
231         return ret;
232 }
233
234 static void  gb_uart_tx_write_work(struct work_struct *work)
235 {
236         struct gb_uart_send_data_request *request;
237         struct gb_tty *gb_tty;
238         unsigned long flags;
239         unsigned int send_size;
240         int ret;
241
242         gb_tty = container_of(work, struct gb_tty, tx_work);
243         request = gb_tty->buffer;
244
245         while (1) {
246                 if (gb_tty->close_pending)
247                         break;
248
249                 spin_lock_irqsave(&gb_tty->write_lock, flags);
250                 send_size = gb_tty->buffer_payload_max;
251                 if (send_size > gb_tty->credits)
252                         send_size = gb_tty->credits;
253
254                 send_size = kfifo_out_peek(&gb_tty->write_fifo,
255                                            &request->data[0],
256                                            send_size);
257                 if (!send_size) {
258                         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
259                         break;
260                 }
261
262                 gb_tty->credits -= send_size;
263                 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
264
265                 request->size = cpu_to_le16(send_size);
266                 ret = gb_operation_sync(gb_tty->connection,
267                                         GB_UART_TYPE_SEND_DATA,
268                                         request, sizeof(*request) + send_size,
269                                         NULL, 0);
270                 if (ret) {
271                         dev_err(&gb_tty->gbphy_dev->dev,
272                                 "send data error: %d\n", ret);
273                         spin_lock_irqsave(&gb_tty->write_lock, flags);
274                         gb_tty->credits += send_size;
275                         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
276                         if (!gb_tty->close_pending)
277                                 schedule_work(work);
278                         return;
279                 }
280
281                 spin_lock_irqsave(&gb_tty->write_lock, flags);
282                 ret = kfifo_out(&gb_tty->write_fifo, &request->data[0],
283                                 send_size);
284                 spin_unlock_irqrestore(&gb_tty->write_lock, flags);
285
286                 tty_port_tty_wakeup(&gb_tty->port);
287         }
288 }
289
290 static int send_line_coding(struct gb_tty *tty)
291 {
292         struct gb_uart_set_line_coding_request request;
293
294         memcpy(&request, &tty->line_coding,
295                sizeof(tty->line_coding));
296         return gb_operation_sync(tty->connection, GB_UART_TYPE_SET_LINE_CODING,
297                                  &request, sizeof(request), NULL, 0);
298 }
299
300 static int send_control(struct gb_tty *gb_tty, u8 control)
301 {
302         struct gb_uart_set_control_line_state_request request;
303
304         request.control = control;
305         return gb_operation_sync(gb_tty->connection,
306                                  GB_UART_TYPE_SET_CONTROL_LINE_STATE,
307                                  &request, sizeof(request), NULL, 0);
308 }
309
310 static int send_break(struct gb_tty *gb_tty, u8 state)
311 {
312         struct gb_uart_set_break_request request;
313
314         if ((state != 0) && (state != 1)) {
315                 dev_err(&gb_tty->gbphy_dev->dev,
316                         "invalid break state of %d\n", state);
317                 return -EINVAL;
318         }
319
320         request.state = state;
321         return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_SEND_BREAK,
322                                  &request, sizeof(request), NULL, 0);
323 }
324
325 static int gb_uart_wait_for_all_credits(struct gb_tty *gb_tty)
326 {
327         int ret;
328
329         if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
330                 return 0;
331
332         ret = wait_for_completion_timeout(&gb_tty->credits_complete,
333                         msecs_to_jiffies(GB_UART_CREDIT_WAIT_TIMEOUT_MSEC));
334         if (!ret) {
335                 dev_err(&gb_tty->gbphy_dev->dev,
336                         "time out waiting for credits\n");
337                 return -ETIMEDOUT;
338         }
339
340         return 0;
341 }
342
343 static int gb_uart_flush(struct gb_tty *gb_tty, u8 flags)
344 {
345         struct gb_uart_serial_flush_request request;
346
347         request.flags = flags;
348         return gb_operation_sync(gb_tty->connection, GB_UART_TYPE_FLUSH_FIFOS,
349                                  &request, sizeof(request), NULL, 0);
350 }
351
352 static struct gb_tty *get_gb_by_minor(unsigned int minor)
353 {
354         struct gb_tty *gb_tty;
355
356         mutex_lock(&table_lock);
357         gb_tty = idr_find(&tty_minors, minor);
358         if (gb_tty) {
359                 mutex_lock(&gb_tty->mutex);
360                 if (gb_tty->disconnected) {
361                         mutex_unlock(&gb_tty->mutex);
362                         gb_tty = NULL;
363                 } else {
364                         tty_port_get(&gb_tty->port);
365                         mutex_unlock(&gb_tty->mutex);
366                 }
367         }
368         mutex_unlock(&table_lock);
369         return gb_tty;
370 }
371
372 static int alloc_minor(struct gb_tty *gb_tty)
373 {
374         int minor;
375
376         mutex_lock(&table_lock);
377         minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
378         mutex_unlock(&table_lock);
379         if (minor >= 0)
380                 gb_tty->minor = minor;
381         return minor;
382 }
383
384 static void release_minor(struct gb_tty *gb_tty)
385 {
386         int minor = gb_tty->minor;
387
388         gb_tty->minor = 0;      /* Maybe should use an invalid value instead */
389         mutex_lock(&table_lock);
390         idr_remove(&tty_minors, minor);
391         mutex_unlock(&table_lock);
392 }
393
394 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
395 {
396         struct gb_tty *gb_tty;
397         int retval;
398
399         gb_tty = get_gb_by_minor(tty->index);
400         if (!gb_tty)
401                 return -ENODEV;
402
403         retval = tty_standard_install(driver, tty);
404         if (retval)
405                 goto error;
406
407         tty->driver_data = gb_tty;
408         return 0;
409 error:
410         tty_port_put(&gb_tty->port);
411         return retval;
412 }
413
414 static int gb_tty_open(struct tty_struct *tty, struct file *file)
415 {
416         struct gb_tty *gb_tty = tty->driver_data;
417
418         return tty_port_open(&gb_tty->port, tty, file);
419 }
420
421 static void gb_tty_close(struct tty_struct *tty, struct file *file)
422 {
423         struct gb_tty *gb_tty = tty->driver_data;
424
425         tty_port_close(&gb_tty->port, tty, file);
426 }
427
428 static void gb_tty_cleanup(struct tty_struct *tty)
429 {
430         struct gb_tty *gb_tty = tty->driver_data;
431
432         tty_port_put(&gb_tty->port);
433 }
434
435 static void gb_tty_hangup(struct tty_struct *tty)
436 {
437         struct gb_tty *gb_tty = tty->driver_data;
438
439         tty_port_hangup(&gb_tty->port);
440 }
441
442 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
443                         int count)
444 {
445         struct gb_tty *gb_tty = tty->driver_data;
446
447         count =  kfifo_in_spinlocked(&gb_tty->write_fifo, buf, count,
448                                      &gb_tty->write_lock);
449         if (count && !gb_tty->close_pending)
450                 schedule_work(&gb_tty->tx_work);
451
452         return count;
453 }
454
455 static int gb_tty_write_room(struct tty_struct *tty)
456 {
457         struct gb_tty *gb_tty = tty->driver_data;
458         unsigned long flags;
459         int room;
460
461         spin_lock_irqsave(&gb_tty->write_lock, flags);
462         room = kfifo_avail(&gb_tty->write_fifo);
463         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
464
465         room -= GB_UART_WRITE_ROOM_MARGIN;
466         if (room < 0)
467                 return 0;
468
469         return room;
470 }
471
472 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
473 {
474         struct gb_tty *gb_tty = tty->driver_data;
475         unsigned long flags;
476         int chars;
477
478         spin_lock_irqsave(&gb_tty->write_lock, flags);
479         chars = kfifo_len(&gb_tty->write_fifo);
480         if (gb_tty->credits < GB_UART_FIRMWARE_CREDITS)
481                 chars += GB_UART_FIRMWARE_CREDITS - gb_tty->credits;
482         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
483
484         return chars;
485 }
486
487 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
488 {
489         struct gb_tty *gb_tty = tty->driver_data;
490
491         return send_break(gb_tty, state ? 1 : 0);
492 }
493
494 static void gb_tty_set_termios(struct tty_struct *tty,
495                                struct ktermios *termios_old)
496 {
497         struct gb_tty *gb_tty = tty->driver_data;
498         struct ktermios *termios = &tty->termios;
499         struct gb_tty_line_coding newline;
500         u8 newctrl = gb_tty->ctrlout;
501
502         newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
503         newline.format = termios->c_cflag & CSTOPB ?
504                                 GB_SERIAL_2_STOP_BITS : GB_SERIAL_1_STOP_BITS;
505         newline.parity = termios->c_cflag & PARENB ?
506                                 (termios->c_cflag & PARODD ? 1 : 2) +
507                                 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
508
509         switch (termios->c_cflag & CSIZE) {
510         case CS5:
511                 newline.data_bits = 5;
512                 break;
513         case CS6:
514                 newline.data_bits = 6;
515                 break;
516         case CS7:
517                 newline.data_bits = 7;
518                 break;
519         case CS8:
520         default:
521                 newline.data_bits = 8;
522                 break;
523         }
524
525         /* FIXME: needs to clear unsupported bits in the termios */
526         gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
527
528         if (C_BAUD(tty) == B0) {
529                 newline.rate = gb_tty->line_coding.rate;
530                 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
531         } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
532                 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
533         }
534
535         if (newctrl != gb_tty->ctrlout) {
536                 gb_tty->ctrlout = newctrl;
537                 send_control(gb_tty, newctrl);
538         }
539
540         if (C_CRTSCTS(tty) && C_BAUD(tty) != B0)
541                 newline.flow_control = GB_SERIAL_AUTO_RTSCTS_EN;
542         else
543                 newline.flow_control = 0;
544
545         if (memcmp(&gb_tty->line_coding, &newline, sizeof(newline))) {
546                 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
547                 send_line_coding(gb_tty);
548         }
549 }
550
551 static int gb_tty_tiocmget(struct tty_struct *tty)
552 {
553         struct gb_tty *gb_tty = tty->driver_data;
554
555         return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
556                (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
557                (gb_tty->ctrlin  & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
558                (gb_tty->ctrlin  & GB_UART_CTRL_RI  ? TIOCM_RI  : 0) |
559                (gb_tty->ctrlin  & GB_UART_CTRL_DCD ? TIOCM_CD  : 0) |
560                TIOCM_CTS;
561 }
562
563 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
564                            unsigned int clear)
565 {
566         struct gb_tty *gb_tty = tty->driver_data;
567         u8 newctrl = gb_tty->ctrlout;
568
569         set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
570               (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
571         clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
572                 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
573
574         newctrl = (newctrl & ~clear) | set;
575         if (gb_tty->ctrlout == newctrl)
576                 return 0;
577
578         gb_tty->ctrlout = newctrl;
579         return send_control(gb_tty, newctrl);
580 }
581
582 static void gb_tty_throttle(struct tty_struct *tty)
583 {
584         struct gb_tty *gb_tty = tty->driver_data;
585         unsigned char stop_char;
586         int retval;
587
588         if (I_IXOFF(tty)) {
589                 stop_char = STOP_CHAR(tty);
590                 retval = gb_tty_write(tty, &stop_char, 1);
591                 if (retval <= 0)
592                         return;
593         }
594
595         if (tty->termios.c_cflag & CRTSCTS) {
596                 gb_tty->ctrlout &= ~GB_UART_CTRL_RTS;
597                 retval = send_control(gb_tty, gb_tty->ctrlout);
598         }
599 }
600
601 static void gb_tty_unthrottle(struct tty_struct *tty)
602 {
603         struct gb_tty *gb_tty = tty->driver_data;
604         unsigned char start_char;
605         int retval;
606
607         if (I_IXOFF(tty)) {
608                 start_char = START_CHAR(tty);
609                 retval = gb_tty_write(tty, &start_char, 1);
610                 if (retval <= 0)
611                         return;
612         }
613
614         if (tty->termios.c_cflag & CRTSCTS) {
615                 gb_tty->ctrlout |= GB_UART_CTRL_RTS;
616                 retval = send_control(gb_tty, gb_tty->ctrlout);
617         }
618 }
619
620 static int get_serial_info(struct gb_tty *gb_tty,
621                            struct serial_struct __user *info)
622 {
623         struct serial_struct tmp;
624
625         memset(&tmp, 0, sizeof(tmp));
626         tmp.type = PORT_16550A;
627         tmp.line = gb_tty->minor;
628         tmp.xmit_fifo_size = 16;
629         tmp.baud_base = 9600;
630         tmp.close_delay = gb_tty->port.close_delay / 10;
631         tmp.closing_wait =
632                 gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
633                 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
634
635         if (copy_to_user(info, &tmp, sizeof(tmp)))
636                 return -EFAULT;
637         return 0;
638 }
639
640 static int set_serial_info(struct gb_tty *gb_tty,
641                            struct serial_struct __user *newinfo)
642 {
643         struct serial_struct new_serial;
644         unsigned int closing_wait;
645         unsigned int close_delay;
646         int retval = 0;
647
648         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
649                 return -EFAULT;
650
651         close_delay = new_serial.close_delay * 10;
652         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
653                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
654
655         mutex_lock(&gb_tty->port.mutex);
656         if (!capable(CAP_SYS_ADMIN)) {
657                 if ((close_delay != gb_tty->port.close_delay) ||
658                     (closing_wait != gb_tty->port.closing_wait))
659                         retval = -EPERM;
660         } else {
661                 gb_tty->port.close_delay = close_delay;
662                 gb_tty->port.closing_wait = closing_wait;
663         }
664         mutex_unlock(&gb_tty->port.mutex);
665         return retval;
666 }
667
668 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
669 {
670         int retval = 0;
671         DECLARE_WAITQUEUE(wait, current);
672         struct async_icount old;
673         struct async_icount new;
674
675         if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
676                 return -EINVAL;
677
678         do {
679                 spin_lock_irq(&gb_tty->read_lock);
680                 old = gb_tty->oldcount;
681                 new = gb_tty->iocount;
682                 gb_tty->oldcount = new;
683                 spin_unlock_irq(&gb_tty->read_lock);
684
685                 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
686                         break;
687                 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
688                         break;
689                 if ((arg & TIOCM_RI) && (old.rng != new.rng))
690                         break;
691
692                 add_wait_queue(&gb_tty->wioctl, &wait);
693                 set_current_state(TASK_INTERRUPTIBLE);
694                 schedule();
695                 remove_wait_queue(&gb_tty->wioctl, &wait);
696                 if (gb_tty->disconnected) {
697                         if (arg & TIOCM_CD)
698                                 break;
699                         retval = -ENODEV;
700                 } else if (signal_pending(current)) {
701                         retval = -ERESTARTSYS;
702                 }
703         } while (!retval);
704
705         return retval;
706 }
707
708 static int gb_tty_get_icount(struct tty_struct *tty,
709                              struct serial_icounter_struct *icount)
710 {
711         struct gb_tty *gb_tty = tty->driver_data;
712
713         icount->dsr = gb_tty->iocount.dsr;
714         icount->rng = gb_tty->iocount.rng;
715         icount->dcd = gb_tty->iocount.dcd;
716         icount->frame = gb_tty->iocount.frame;
717         icount->overrun = gb_tty->iocount.overrun;
718         icount->parity = gb_tty->iocount.parity;
719         icount->brk = gb_tty->iocount.brk;
720
721         return 0;
722 }
723
724 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
725                         unsigned long arg)
726 {
727         struct gb_tty *gb_tty = tty->driver_data;
728
729         switch (cmd) {
730         case TIOCGSERIAL:
731                 return get_serial_info(gb_tty,
732                                        (struct serial_struct __user *)arg);
733         case TIOCSSERIAL:
734                 return set_serial_info(gb_tty,
735                                        (struct serial_struct __user *)arg);
736         case TIOCMIWAIT:
737                 return wait_serial_change(gb_tty, arg);
738         }
739
740         return -ENOIOCTLCMD;
741 }
742
743 static void gb_tty_dtr_rts(struct tty_port *port, int on)
744 {
745         struct gb_tty *gb_tty;
746         u8 newctrl;
747
748         gb_tty = container_of(port, struct gb_tty, port);
749         newctrl = gb_tty->ctrlout;
750
751         if (on)
752                 newctrl |= (GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
753         else
754                 newctrl &= ~(GB_UART_CTRL_DTR | GB_UART_CTRL_RTS);
755
756         gb_tty->ctrlout = newctrl;
757         send_control(gb_tty, newctrl);
758 }
759
760 static int gb_tty_port_activate(struct tty_port *port,
761                                 struct tty_struct *tty)
762 {
763         struct gb_tty *gb_tty;
764
765         gb_tty = container_of(port, struct gb_tty, port);
766
767         return gbphy_runtime_get_sync(gb_tty->gbphy_dev);
768 }
769
770 static void gb_tty_port_shutdown(struct tty_port *port)
771 {
772         struct gb_tty *gb_tty;
773         unsigned long flags;
774         int ret;
775
776         gb_tty = container_of(port, struct gb_tty, port);
777
778         gb_tty->close_pending = true;
779
780         cancel_work_sync(&gb_tty->tx_work);
781
782         spin_lock_irqsave(&gb_tty->write_lock, flags);
783         kfifo_reset_out(&gb_tty->write_fifo);
784         spin_unlock_irqrestore(&gb_tty->write_lock, flags);
785
786         if (gb_tty->credits == GB_UART_FIRMWARE_CREDITS)
787                 goto out;
788
789         ret = gb_uart_flush(gb_tty, GB_SERIAL_FLAG_FLUSH_TRANSMITTER);
790         if (ret) {
791                 dev_err(&gb_tty->gbphy_dev->dev,
792                         "error flushing transmitter: %d\n", ret);
793         }
794
795         gb_uart_wait_for_all_credits(gb_tty);
796
797 out:
798         gb_tty->close_pending = false;
799
800         gbphy_runtime_put_autosuspend(gb_tty->gbphy_dev);
801 }
802
803 static void gb_tty_port_destruct(struct tty_port *port)
804 {
805         struct gb_tty *gb_tty = container_of(port, struct gb_tty, port);
806
807         if (gb_tty->minor != GB_NUM_MINORS)
808                 release_minor(gb_tty);
809         kfifo_free(&gb_tty->write_fifo);
810         kfree(gb_tty->buffer);
811         kfree(gb_tty);
812 }
813
814 static const struct tty_operations gb_ops = {
815         .install =              gb_tty_install,
816         .open =                 gb_tty_open,
817         .close =                gb_tty_close,
818         .cleanup =              gb_tty_cleanup,
819         .hangup =               gb_tty_hangup,
820         .write =                gb_tty_write,
821         .write_room =           gb_tty_write_room,
822         .ioctl =                gb_tty_ioctl,
823         .throttle =             gb_tty_throttle,
824         .unthrottle =           gb_tty_unthrottle,
825         .chars_in_buffer =      gb_tty_chars_in_buffer,
826         .break_ctl =            gb_tty_break_ctl,
827         .set_termios =          gb_tty_set_termios,
828         .tiocmget =             gb_tty_tiocmget,
829         .tiocmset =             gb_tty_tiocmset,
830         .get_icount =           gb_tty_get_icount,
831 };
832
833 static const struct tty_port_operations gb_port_ops = {
834         .dtr_rts =              gb_tty_dtr_rts,
835         .activate =             gb_tty_port_activate,
836         .shutdown =             gb_tty_port_shutdown,
837         .destruct =             gb_tty_port_destruct,
838 };
839
840 static int gb_uart_probe(struct gbphy_device *gbphy_dev,
841                          const struct gbphy_device_id *id)
842 {
843         struct gb_connection *connection;
844         size_t max_payload;
845         struct gb_tty *gb_tty;
846         struct device *tty_dev;
847         int retval;
848         int minor;
849
850         connection = gb_connection_create(gbphy_dev->bundle,
851                                           le16_to_cpu(gbphy_dev->cport_desc->id),
852                                           gb_uart_request_handler);
853         if (IS_ERR(connection))
854                 return PTR_ERR(connection);
855
856         max_payload = gb_operation_get_payload_size_max(connection);
857         if (max_payload < sizeof(struct gb_uart_send_data_request)) {
858                 retval = -EINVAL;
859                 goto exit_connection_destroy;
860         }
861
862         gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
863         if (!gb_tty) {
864                 retval = -ENOMEM;
865                 goto exit_connection_destroy;
866         }
867
868         tty_port_init(&gb_tty->port);
869         gb_tty->port.ops = &gb_port_ops;
870         gb_tty->minor = GB_NUM_MINORS;
871
872         gb_tty->buffer_payload_max = max_payload -
873                         sizeof(struct gb_uart_send_data_request);
874
875         gb_tty->buffer = kzalloc(gb_tty->buffer_payload_max, GFP_KERNEL);
876         if (!gb_tty->buffer) {
877                 retval = -ENOMEM;
878                 goto exit_put_port;
879         }
880
881         INIT_WORK(&gb_tty->tx_work, gb_uart_tx_write_work);
882
883         retval = kfifo_alloc(&gb_tty->write_fifo, GB_UART_WRITE_FIFO_SIZE,
884                              GFP_KERNEL);
885         if (retval)
886                 goto exit_put_port;
887
888         gb_tty->credits = GB_UART_FIRMWARE_CREDITS;
889         init_completion(&gb_tty->credits_complete);
890
891         minor = alloc_minor(gb_tty);
892         if (minor < 0) {
893                 if (minor == -ENOSPC) {
894                         dev_err(&gbphy_dev->dev,
895                                 "no more free minor numbers\n");
896                         retval = -ENODEV;
897                 } else {
898                         retval = minor;
899                 }
900                 goto exit_put_port;
901         }
902
903         gb_tty->minor = minor;
904         spin_lock_init(&gb_tty->write_lock);
905         spin_lock_init(&gb_tty->read_lock);
906         init_waitqueue_head(&gb_tty->wioctl);
907         mutex_init(&gb_tty->mutex);
908
909         gb_tty->connection = connection;
910         gb_tty->gbphy_dev = gbphy_dev;
911         gb_connection_set_data(connection, gb_tty);
912         gb_gbphy_set_data(gbphy_dev, gb_tty);
913
914         retval = gb_connection_enable_tx(connection);
915         if (retval)
916                 goto exit_put_port;
917
918         send_control(gb_tty, gb_tty->ctrlout);
919
920         /* initialize the uart to be 9600n81 */
921         gb_tty->line_coding.rate = cpu_to_le32(9600);
922         gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
923         gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
924         gb_tty->line_coding.data_bits = 8;
925         send_line_coding(gb_tty);
926
927         retval = gb_connection_enable(connection);
928         if (retval)
929                 goto exit_connection_disable;
930
931         tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
932                                            &gbphy_dev->dev);
933         if (IS_ERR(tty_dev)) {
934                 retval = PTR_ERR(tty_dev);
935                 goto exit_connection_disable;
936         }
937
938         gbphy_runtime_put_autosuspend(gbphy_dev);
939         return 0;
940
941 exit_connection_disable:
942         gb_connection_disable(connection);
943 exit_put_port:
944         tty_port_put(&gb_tty->port);
945 exit_connection_destroy:
946         gb_connection_destroy(connection);
947
948         return retval;
949 }
950
951 static void gb_uart_remove(struct gbphy_device *gbphy_dev)
952 {
953         struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
954         struct gb_connection *connection = gb_tty->connection;
955         struct tty_struct *tty;
956         int ret;
957
958         ret = gbphy_runtime_get_sync(gbphy_dev);
959         if (ret)
960                 gbphy_runtime_get_noresume(gbphy_dev);
961
962         mutex_lock(&gb_tty->mutex);
963         gb_tty->disconnected = true;
964
965         wake_up_all(&gb_tty->wioctl);
966         mutex_unlock(&gb_tty->mutex);
967
968         tty = tty_port_tty_get(&gb_tty->port);
969         if (tty) {
970                 tty_vhangup(tty);
971                 tty_kref_put(tty);
972         }
973
974         gb_connection_disable_rx(connection);
975         tty_unregister_device(gb_tty_driver, gb_tty->minor);
976
977         gb_connection_disable(connection);
978         gb_connection_destroy(connection);
979
980         tty_port_put(&gb_tty->port);
981 }
982
983 static int gb_tty_init(void)
984 {
985         int retval = 0;
986
987         gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
988         if (IS_ERR(gb_tty_driver)) {
989                 pr_err("Can not allocate tty driver\n");
990                 retval = -ENOMEM;
991                 goto fail_unregister_dev;
992         }
993
994         gb_tty_driver->driver_name = "gb";
995         gb_tty_driver->name = GB_NAME;
996         gb_tty_driver->major = 0;
997         gb_tty_driver->minor_start = 0;
998         gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
999         gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1000         gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1001         gb_tty_driver->init_termios = tty_std_termios;
1002         gb_tty_driver->init_termios.c_cflag = B9600 | CS8 |
1003                 CREAD | HUPCL | CLOCAL;
1004         tty_set_operations(gb_tty_driver, &gb_ops);
1005
1006         retval = tty_register_driver(gb_tty_driver);
1007         if (retval) {
1008                 pr_err("Can not register tty driver: %d\n", retval);
1009                 goto fail_put_gb_tty;
1010         }
1011
1012         return 0;
1013
1014 fail_put_gb_tty:
1015         put_tty_driver(gb_tty_driver);
1016 fail_unregister_dev:
1017         return retval;
1018 }
1019
1020 static void gb_tty_exit(void)
1021 {
1022         tty_unregister_driver(gb_tty_driver);
1023         put_tty_driver(gb_tty_driver);
1024         idr_destroy(&tty_minors);
1025 }
1026
1027 static const struct gbphy_device_id gb_uart_id_table[] = {
1028         { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_UART) },
1029         { },
1030 };
1031 MODULE_DEVICE_TABLE(gbphy, gb_uart_id_table);
1032
1033 static struct gbphy_driver uart_driver = {
1034         .name           = "uart",
1035         .probe          = gb_uart_probe,
1036         .remove         = gb_uart_remove,
1037         .id_table       = gb_uart_id_table,
1038 };
1039
1040 static int gb_uart_driver_init(void)
1041 {
1042         int ret;
1043
1044         ret = gb_tty_init();
1045         if (ret)
1046                 return ret;
1047
1048         ret = gb_gbphy_register(&uart_driver);
1049         if (ret) {
1050                 gb_tty_exit();
1051                 return ret;
1052         }
1053
1054         return 0;
1055 }
1056 module_init(gb_uart_driver_init);
1057
1058 static void gb_uart_driver_exit(void)
1059 {
1060         gb_gbphy_deregister(&uart_driver);
1061         gb_tty_exit();
1062 }
1063
1064 module_exit(gb_uart_driver_exit);
1065 MODULE_LICENSE("GPL v2");