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