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