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