]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/uart-gb.c
greybus: Greybus UART connection driver
[karo-tx-linux.git] / drivers / staging / greybus / uart-gb.c
1 /*
2  * UART driver for the Greybus "generic" UART module.
3  *
4  * Copyright 2014 Google Inc.
5  *
6  * Released under the GPLv2 only.
7  *
8  * Heavily based on drivers/usb/class/cdc-acm.c and
9  * drivers/usb/serial/usb-serial.c.
10  */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/wait.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 #include <linux/mutex.h>
21 #include <linux/tty.h>
22 #include <linux/serial.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/idr.h>
27 #include <linux/fs.h>
28 #include <linux/kdev_t.h>
29
30 #include "greybus.h"
31 #include "module.h"
32
33 #define GB_NUM_MINORS   255     /* 255 is enough for anyone... */
34 #define GB_NAME         "ttyGB"
35
36 /* Version of the Greybus PWM protocol we support */
37 #define GB_UART_VERSION_MAJOR           0x00
38 #define GB_UART_VERSION_MINOR           0x01
39
40 /* Greybus UART request types */
41 #define GB_UART_REQ_INVALID                     0x00
42 #define GB_UART_REQ_PROTOCOL_VERSION            0x01
43 #define GB_UART_REQ_SEND_DATA                   0x02
44 #define GB_UART_REQ_RECEIVE_DATA                0x03    /* Unsolicited data */
45 #define GB_UART_REQ_SET_LINE_CODING             0x04
46 #define GB_UART_REQ_SET_CONTROL_LINE_STATE      0x05
47 #define GB_UART_REQ_SET_BREAK                   0x06
48 #define GB_UART_REQ_SERIAL_STATE                0x07    /* Unsolicited data */
49 #define GB_UART_TYPE_RESPONSE                   0x80    /* OR'd with rest */
50
51 struct gb_uart_proto_version_response {
52         __u8    status;
53         __u8    major;
54         __u8    minor;
55 };
56
57 struct gb_uart_send_data_request {
58         __le16  size;
59         __u8    data[0];
60 };
61
62 struct gb_serial_line_coding {
63         __le32  rate;
64         __u8    format;
65 #define GB_SERIAL_1_STOP_BITS           0
66 #define GB_SERIAL_1_5_STOP_BITS         1
67 #define GB_SERIAL_2_STOP_BITS           2
68
69         __u8    parity;
70 #define GB_SERIAL_NO_PARITY             0
71 #define GB_SERIAL_ODD_PARITY            1
72 #define GB_SERIAL_EVEN_PARITY           2
73 #define GB_SERIAL_MARK_PARITY           3
74 #define GB_SERIAL_SPACE_PARITY          4
75
76         __u8    data;
77 } __attribute__ ((packed));
78
79 struct gb_uart_set_line_coding_request {
80         struct gb_serial_line_coding    line_coding;
81 };
82
83 /* output control lines */
84 #define GB_UART_CTRL_DTR                0x01
85 #define GB_UART_CTRL_RTS                0x02
86
87 struct gb_uart_set_control_line_state_request {
88         __le16  control;
89 };
90
91 struct gb_uart_set_break_request {
92         __u8    state;
93 };
94
95 /* input control lines and line errors */
96 #define GB_UART_CTRL_DCD                0x01
97 #define GB_UART_CTRL_DSR                0x02
98 #define GB_UART_CTRL_BRK                0x04
99 #define GB_UART_CTRL_RI                 0x08
100
101 #define GB_UART_CTRL_FRAMING            0x10
102 #define GB_UART_CTRL_PARITY             0x20
103 #define GB_UART_CTRL_OVERRUN            0x40
104
105 struct gb_uart_serial_state_request {
106         __u16   control;
107 };
108
109 struct gb_uart_simple_response {
110         __u8    status;
111 };
112
113 struct gb_tty {
114         struct tty_port port;
115         struct gb_connection *connection;
116         u16 cport_id;
117         unsigned int minor;
118         unsigned char clocal;
119         unsigned int throttled:1;
120         unsigned int throttle_req:1;
121         bool disconnected;
122         int writesize;          // FIXME - set this somehow.
123         spinlock_t read_lock;
124         spinlock_t write_lock;
125         struct async_icount iocount;
126         struct async_icount oldcount;
127         wait_queue_head_t wioctl;
128         struct mutex mutex;
129         u8 version_major;
130         u8 version_minor;
131         unsigned int ctrlin;    /* input control lines */
132         unsigned int ctrlout;   /* output control lines */
133         struct gb_serial_line_coding line_coding;
134 };
135
136
137 static struct tty_driver *gb_tty_driver;
138 static DEFINE_IDR(tty_minors);
139 static DEFINE_MUTEX(table_lock);
140 static atomic_t reference_count = ATOMIC_INIT(0);
141
142
143 static int request_operation(struct gb_connection *connection, int type,
144                              void *response, int response_size)
145 {
146         struct gb_operation *operation;
147         struct gb_uart_simple_response *fake_request;
148         u8 *local_response;
149         int ret;
150
151         local_response = kmalloc(response_size, GFP_KERNEL);
152         if (!local_response)
153                 return -ENOMEM;
154
155         operation = gb_operation_create(connection, type, 0, response_size);
156         if (!operation) {
157                 kfree(local_response);
158                 return -ENOMEM;
159         }
160
161         /* Synchronous operation--no callback */
162         ret = gb_operation_request_send(operation, NULL);
163         if (ret) {
164                 pr_err("version operation failed (%d)\n", ret);
165                 goto out;
166         }
167
168         /*
169          * We only want to look at the status, and all requests have the same
170          * layout for where the status is, so cast this to a random request so
171          * we can see the status easier.
172          */
173         fake_request = (struct gb_uart_simple_response *)local_response;
174         if (fake_request->status) {
175                 gb_connection_err(connection, "response %hhu",
176                         fake_request->status);
177                 ret = -EIO;
178         } else {
179                 /* Good request, so copy to the caller's buffer */
180                 memcpy(response, local_response, response_size);
181         }
182 out:
183         gb_operation_destroy(operation);
184         kfree(local_response);
185
186         return ret;
187 }
188
189 /*
190  * This request only uses the connection field, and if successful,
191  * fills in the major and minor protocol version of the target.
192  */
193 static int get_version(struct gb_tty *tty)
194 {
195         struct gb_uart_proto_version_response version_request;
196         int retval;
197
198         retval = request_operation(tty->connection,
199                                    GB_UART_REQ_PROTOCOL_VERSION,
200                                    &version_request, sizeof(version_request));
201         if (retval)
202                 return retval;
203
204         if (version_request.major > GB_UART_VERSION_MAJOR) {
205                 pr_err("unsupported major version (%hhu > %hhu)\n",
206                         version_request.major, GB_UART_VERSION_MAJOR);
207                 return -ENOTSUPP;
208         }
209
210         tty->version_major = version_request.major;
211         tty->version_minor = version_request.minor;
212         return 0;
213 }
214
215 static int send_data(struct gb_tty *tty, u16 size, const u8 *data)
216 {
217         struct gb_connection *connection = tty->connection;
218         struct gb_operation *operation;
219         struct gb_uart_send_data_request *request;
220         struct gb_uart_simple_response *response;
221         int retval;
222
223         if (!data || !size)
224                 return 0;
225
226         operation = gb_operation_create(connection, GB_UART_REQ_SEND_DATA,
227                                         sizeof(*request) + size,
228                                         sizeof(*response));
229         if (!operation)
230                 return -ENOMEM;
231         request = operation->request_payload;
232         request->size = cpu_to_le16(size);
233         memcpy(&request->data[0], data, size);
234
235         /* Synchronous operation--no callback */
236         retval = gb_operation_request_send(operation, NULL);
237         if (retval) {
238                 dev_err(&connection->dev,
239                         "send data operation failed (%d)\n", retval);
240                 goto out;
241         }
242
243         response = operation->response_payload;
244         if (response->status) {
245                 gb_connection_err(connection, "send data response %hhu",
246                                   response->status);
247                 retval = -EIO;
248         }
249 out:
250         gb_operation_destroy(operation);
251
252         return retval;
253 }
254
255 static int send_line_coding(struct gb_tty *tty,
256                             struct gb_serial_line_coding *line_coding)
257 {
258         struct gb_connection *connection = tty->connection;
259         struct gb_operation *operation;
260         struct gb_uart_set_line_coding_request *request;
261         struct gb_uart_simple_response *response;
262         int retval;
263
264         operation = gb_operation_create(connection, GB_UART_REQ_SET_LINE_CODING,
265                                         sizeof(*request),
266                                         sizeof(*response));
267         if (!operation)
268                 return -ENOMEM;
269         request = operation->request_payload;
270         memcpy(&request->line_coding, line_coding, sizeof(*line_coding));
271
272         /* Synchronous operation--no callback */
273         retval = gb_operation_request_send(operation, NULL);
274         if (retval) {
275                 dev_err(&connection->dev,
276                         "send line coding operation failed (%d)\n", retval);
277                 goto out;
278         }
279
280         response = operation->response_payload;
281         if (response->status) {
282                 gb_connection_err(connection, "send line coding response %hhu",
283                                   response->status);
284                 retval = -EIO;
285         }
286 out:
287         gb_operation_destroy(operation);
288
289         return retval;
290 }
291
292 static int send_control(struct gb_tty *tty, u16 control)
293 {
294         struct gb_connection *connection = tty->connection;
295         struct gb_operation *operation;
296         struct gb_uart_set_control_line_state_request *request;
297         struct gb_uart_simple_response *response;
298         int retval;
299
300         operation = gb_operation_create(connection,
301                                         GB_UART_REQ_SET_CONTROL_LINE_STATE,
302                                         sizeof(*request),
303                                         sizeof(*response));
304         if (!operation)
305                 return -ENOMEM;
306         request = operation->request_payload;
307         request->control = cpu_to_le16(control);
308
309         /* Synchronous operation--no callback */
310         retval = gb_operation_request_send(operation, NULL);
311         if (retval) {
312                 dev_err(&connection->dev,
313                         "send control operation failed (%d)\n", retval);
314                 goto out;
315         }
316
317         response = operation->response_payload;
318         if (response->status) {
319                 gb_connection_err(connection, "send control response %hhu",
320                                   response->status);
321                 retval = -EIO;
322         }
323 out:
324         gb_operation_destroy(operation);
325
326         return retval;
327 }
328
329 static int send_break(struct gb_tty *tty, u8 state)
330 {
331         struct gb_connection *connection = tty->connection;
332         struct gb_operation *operation;
333         struct gb_uart_set_break_request *request;
334         struct gb_uart_simple_response *response;
335         int retval;
336
337         if ((state != 0) && (state != 1)) {
338                 dev_err(&connection->dev, "invalid break state of %d\n", state);
339                 return -EINVAL;
340         }
341
342         operation = gb_operation_create(connection, GB_UART_REQ_SET_BREAK,
343                                         sizeof(*request),
344                                         sizeof(*response));
345         if (!operation)
346                 return -ENOMEM;
347         request = operation->request_payload;
348         request->state = state;
349
350         /* Synchronous operation--no callback */
351         retval = gb_operation_request_send(operation, NULL);
352         if (retval) {
353                 dev_err(&connection->dev,
354                         "send break operation failed (%d)\n", retval);
355                 goto out;
356         }
357
358         response = operation->response_payload;
359         if (response->status) {
360                 gb_connection_err(connection, "send break response %hhu",
361                                   response->status);
362                 retval = -EIO;
363         }
364 out:
365         gb_operation_destroy(operation);
366
367         return retval;
368 }
369
370
371 static struct gb_tty *get_gb_by_minor(unsigned minor)
372 {
373         struct gb_tty *gb_tty;
374
375         mutex_lock(&table_lock);
376         gb_tty = idr_find(&tty_minors, minor);
377         if (gb_tty) {
378                 mutex_lock(&gb_tty->mutex);
379                 if (gb_tty->disconnected) {
380                         mutex_unlock(&gb_tty->mutex);
381                         gb_tty = NULL;
382                 } else {
383                         tty_port_get(&gb_tty->port);
384                         mutex_unlock(&gb_tty->mutex);
385                 }
386         }
387         mutex_unlock(&table_lock);
388         return gb_tty;
389 }
390
391 static int alloc_minor(struct gb_tty *gb_tty)
392 {
393         int minor;
394
395         mutex_lock(&table_lock);
396         minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
397         mutex_unlock(&table_lock);
398         if (minor >= 0)
399                 gb_tty->minor = minor;
400         return minor;
401 }
402
403 static void release_minor(struct gb_tty *gb_tty)
404 {
405         int minor = gb_tty->minor;
406
407         gb_tty->minor = 0;      /* Maybe should use an invalid value instead */
408         mutex_lock(&table_lock);
409         idr_remove(&tty_minors, minor);
410         mutex_unlock(&table_lock);
411 }
412
413 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
414 {
415         struct gb_tty *gb_tty;
416         int retval;
417
418         gb_tty = get_gb_by_minor(tty->index);
419         if (!gb_tty)
420                 return -ENODEV;
421
422         retval = tty_standard_install(driver, tty);
423         if (retval)
424                 goto error;
425
426         tty->driver_data = gb_tty;
427         return 0;
428 error:
429         tty_port_put(&gb_tty->port);
430         return retval;
431 }
432
433 static int gb_tty_open(struct tty_struct *tty, struct file *file)
434 {
435         struct gb_tty *gb_tty = tty->driver_data;
436
437         return tty_port_open(&gb_tty->port, tty, file);
438 }
439
440 static void gb_tty_close(struct tty_struct *tty, struct file *file)
441 {
442         struct gb_tty *gb_tty = tty->driver_data;
443
444         tty_port_close(&gb_tty->port, tty, file);
445 }
446
447 static void gb_tty_cleanup(struct tty_struct *tty)
448 {
449         struct gb_tty *gb_tty = tty->driver_data;
450
451         tty_port_put(&gb_tty->port);
452 }
453
454 static void gb_tty_hangup(struct tty_struct *tty)
455 {
456         struct gb_tty *gb_tty = tty->driver_data;
457
458         tty_port_hangup(&gb_tty->port);
459 }
460
461 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
462                         int count)
463 {
464         struct gb_tty *gb_tty = tty->driver_data;
465
466         return send_data(gb_tty, count, buf);
467 }
468
469 static int gb_tty_write_room(struct tty_struct *tty)
470 {
471 //      struct gb_tty *gb_tty = tty->driver_data;
472
473         // FIXME - how much do we want to say we have room for?
474         return 0;
475 }
476
477 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
478 {
479 //      struct gb_tty *gb_tty = tty->driver_data;
480
481         // FIXME - how many left to send?
482         return 0;
483 }
484
485 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
486 {
487         struct gb_tty *gb_tty = tty->driver_data;
488
489         return send_break(gb_tty, state ? 1 : 0);
490 }
491
492 static void gb_tty_set_termios(struct tty_struct *tty,
493                                struct ktermios *termios_old)
494 {
495         struct gb_tty *gb_tty = tty->driver_data;
496         struct ktermios *termios = &tty->termios;
497         struct gb_serial_line_coding newline;
498         int newctrl = gb_tty->ctrlout;
499
500         newline.rate = cpu_to_le32(tty_get_baud_rate(tty));
501         newline.format = termios->c_cflag & CSTOPB ? 2 : 0;
502         newline.parity = termios->c_cflag & PARENB ?
503                                 (termios->c_cflag & PARODD ? 1 : 2) +
504                                 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
505
506         switch (termios->c_cflag & CSIZE) {
507         case CS5:
508                 newline.data = 5;
509                 break;
510         case CS6:
511                 newline.data = 6;
512                 break;
513         case CS7:
514                 newline.data = 7;
515                 break;
516         case CS8:
517         default:
518                 newline.data = 8;
519                 break;
520         }
521
522         /* FIXME: needs to clear unsupported bits in the termios */
523         gb_tty->clocal = ((termios->c_cflag & CLOCAL) != 0);
524
525         if (C_BAUD(tty) == B0) {
526                 newline.rate = gb_tty->line_coding.rate;
527                 newctrl &= GB_UART_CTRL_DTR;
528         } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
529                 newctrl |= GB_UART_CTRL_DTR;
530         }
531
532         if (newctrl != gb_tty->ctrlout) {
533                 gb_tty->ctrlout = newctrl;
534                 send_control(gb_tty, newctrl);
535         }
536
537         if (memcpy(&gb_tty->line_coding, &newline, sizeof(newline))) {
538                 memcpy(&gb_tty->line_coding, &newline, sizeof(newline));
539                 send_line_coding(gb_tty, &gb_tty->line_coding);
540         }
541 }
542
543 static int gb_tty_tiocmget(struct tty_struct *tty)
544 {
545         struct gb_tty *gb_tty = tty->driver_data;
546
547         return (gb_tty->ctrlout & GB_UART_CTRL_DTR ? TIOCM_DTR : 0) |
548                (gb_tty->ctrlout & GB_UART_CTRL_RTS ? TIOCM_RTS : 0) |
549                (gb_tty->ctrlin  & GB_UART_CTRL_DSR ? TIOCM_DSR : 0) |
550                (gb_tty->ctrlin  & GB_UART_CTRL_RI  ? TIOCM_RI  : 0) |
551                (gb_tty->ctrlin  & GB_UART_CTRL_DCD ? TIOCM_CD  : 0) |
552                TIOCM_CTS;
553 }
554
555 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
556                            unsigned int clear)
557 {
558         struct gb_tty *gb_tty = tty->driver_data;
559         unsigned int newctrl = gb_tty->ctrlout;
560
561         set = (set & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
562               (set & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
563         clear = (clear & TIOCM_DTR ? GB_UART_CTRL_DTR : 0) |
564                 (clear & TIOCM_RTS ? GB_UART_CTRL_RTS : 0);
565
566         newctrl = (newctrl & ~clear) | set;
567         if (gb_tty->ctrlout == newctrl)
568                 return 0;
569
570         gb_tty->ctrlout = newctrl;
571         return send_control(gb_tty, newctrl);
572 }
573
574 static void gb_tty_throttle(struct tty_struct *tty)
575 {
576         struct gb_tty *gb_tty = tty->driver_data;
577
578         spin_lock_irq(&gb_tty->read_lock);
579         gb_tty->throttle_req = 1;
580         spin_unlock_irq(&gb_tty->read_lock);
581 }
582
583 static void gb_tty_unthrottle(struct tty_struct *tty)
584 {
585         struct gb_tty *gb_tty = tty->driver_data;
586         unsigned int was_throttled;
587
588         spin_lock_irq(&gb_tty->read_lock);
589         was_throttled = gb_tty->throttled;
590         gb_tty->throttle_req = 0;
591         gb_tty->throttled = 0;
592         spin_unlock_irq(&gb_tty->read_lock);
593
594         if (was_throttled) {
595                 // FIXME - send more data
596         }
597 }
598
599 static int get_serial_info(struct gb_tty *gb_tty,
600                            struct serial_struct __user *info)
601 {
602         struct serial_struct tmp;
603
604         if (!info)
605                 return -EINVAL;
606
607         memset(&tmp, 0, sizeof(tmp));
608         tmp.flags = ASYNC_LOW_LATENCY;
609         tmp.xmit_fifo_size = gb_tty->writesize;
610         tmp.baud_base = 0;      // FIXME
611         tmp.close_delay = gb_tty->port.close_delay / 10;
612         tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
613                                 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
614
615         if (copy_to_user(info, &tmp, sizeof(tmp)))
616                 return -EFAULT;
617         return 0;
618 }
619
620 static int set_serial_info(struct gb_tty *gb_tty,
621                            struct serial_struct __user *newinfo)
622 {
623         struct serial_struct new_serial;
624         unsigned int closing_wait;
625         unsigned int close_delay;
626         int retval = 0;
627
628         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
629                 return -EFAULT;
630
631         close_delay = new_serial.close_delay * 10;
632         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
633                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
634
635         mutex_lock(&gb_tty->port.mutex);
636         if (!capable(CAP_SYS_ADMIN)) {
637                 if ((close_delay != gb_tty->port.close_delay) ||
638                     (closing_wait != gb_tty->port.closing_wait))
639                         retval = -EPERM;
640                 else
641                         retval = -EOPNOTSUPP;
642         } else {
643                 gb_tty->port.close_delay = close_delay;
644                 gb_tty->port.closing_wait = closing_wait;
645         }
646         mutex_unlock(&gb_tty->port.mutex);
647         return retval;
648 }
649
650 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
651 {
652         int retval = 0;
653         DECLARE_WAITQUEUE(wait, current);
654         struct async_icount old;
655         struct async_icount new;
656
657         if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
658                 return -EINVAL;
659
660         do {
661                 spin_lock_irq(&gb_tty->read_lock);
662                 old = gb_tty->oldcount;
663                 new = gb_tty->iocount;
664                 gb_tty->oldcount = new;
665                 spin_unlock_irq(&gb_tty->read_lock);
666
667                 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
668                         break;
669                 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
670                         break;
671                 if ((arg & TIOCM_RI) && (old.rng != new.rng))
672                         break;
673
674                 add_wait_queue(&gb_tty->wioctl, &wait);
675                 set_current_state(TASK_INTERRUPTIBLE);
676                 schedule();
677                 remove_wait_queue(&gb_tty->wioctl, &wait);
678                 if (gb_tty->disconnected) {
679                         if (arg & TIOCM_CD)
680                                 break;
681                         retval = -ENODEV;
682                 } else if (signal_pending(current)) {
683                         retval = -ERESTARTSYS;
684                 }
685         } while (!retval);
686
687         return retval;
688 }
689
690 static int get_serial_usage(struct gb_tty *gb_tty,
691                             struct serial_icounter_struct __user *count)
692 {
693         struct serial_icounter_struct icount;
694         int retval = 0;
695
696         memset(&icount, 0, sizeof(icount));
697         icount.dsr = gb_tty->iocount.dsr;
698         icount.rng = gb_tty->iocount.rng;
699         icount.dcd = gb_tty->iocount.dcd;
700         icount.frame = gb_tty->iocount.frame;
701         icount.overrun = gb_tty->iocount.overrun;
702         icount.parity = gb_tty->iocount.parity;
703         icount.brk = gb_tty->iocount.brk;
704
705         if (copy_to_user(count, &icount, sizeof(icount)) > 0)
706                 retval = -EFAULT;
707
708         return retval;
709 }
710
711 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
712                         unsigned long arg)
713 {
714         struct gb_tty *gb_tty = tty->driver_data;
715
716         switch (cmd) {
717         case TIOCGSERIAL:
718                 return get_serial_info(gb_tty,
719                                        (struct serial_struct __user *)arg);
720         case TIOCSSERIAL:
721                 return set_serial_info(gb_tty,
722                                        (struct serial_struct __user *)arg);
723         case TIOCMIWAIT:
724                 return wait_serial_change(gb_tty, arg);
725         case TIOCGICOUNT:
726                 return get_serial_usage(gb_tty,
727                                         (struct serial_icounter_struct __user *)arg);
728         }
729
730         return -ENOIOCTLCMD;
731 }
732
733
734 static const struct tty_operations gb_ops = {
735         .install =              gb_tty_install,
736         .open =                 gb_tty_open,
737         .close =                gb_tty_close,
738         .cleanup =              gb_tty_cleanup,
739         .hangup =               gb_tty_hangup,
740         .write =                gb_tty_write,
741         .write_room =           gb_tty_write_room,
742         .ioctl =                gb_tty_ioctl,
743         .throttle =             gb_tty_throttle,
744         .unthrottle =           gb_tty_unthrottle,
745         .chars_in_buffer =      gb_tty_chars_in_buffer,
746         .break_ctl =            gb_tty_break_ctl,
747         .set_termios =          gb_tty_set_termios,
748         .tiocmget =             gb_tty_tiocmget,
749         .tiocmset =             gb_tty_tiocmset,
750 };
751
752
753 static int gb_tty_init(void);
754 static void gb_tty_exit(void);
755
756 static int gb_uart_connection_init(struct gb_connection *connection)
757 {
758         struct gb_tty *gb_tty;
759         struct device *tty_dev;
760         int retval;
761         int minor;
762
763         /* First time here, initialize the tty structures */
764         if (atomic_inc_return(&reference_count) == 1) {
765                 retval = gb_tty_init();
766                 if (retval) {
767                         atomic_dec(&reference_count);
768                         return retval;
769                 }
770         }
771
772         gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
773         if (!gb_tty)
774                 return -ENOMEM;
775         gb_tty->connection = connection;
776
777         /* Check for compatible protocol version */
778         retval = get_version(gb_tty);
779         if (retval)
780                 goto error_version;
781
782         minor = alloc_minor(gb_tty);
783         if (minor < 0) {
784                 if (minor == -ENOSPC) {
785                         dev_err(&connection->dev,
786                                 "no more free minor numbers\n");
787                         return -ENODEV;
788                 }
789                 return minor;
790         }
791
792         gb_tty->minor = minor;
793         spin_lock_init(&gb_tty->write_lock);
794         spin_lock_init(&gb_tty->read_lock);
795         init_waitqueue_head(&gb_tty->wioctl);
796         mutex_init(&gb_tty->mutex);
797
798         connection->private = gb_tty;
799
800         send_control(gb_tty, gb_tty->ctrlout);
801
802         /* initialize the uart to be 9600n81 */
803         gb_tty->line_coding.rate = cpu_to_le32(9600);
804         gb_tty->line_coding.format = GB_SERIAL_1_STOP_BITS;
805         gb_tty->line_coding.parity = GB_SERIAL_NO_PARITY;
806         gb_tty->line_coding.data = 8;
807         send_line_coding(gb_tty, &gb_tty->line_coding);
808
809         tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
810                                            &connection->dev);
811         if (IS_ERR(tty_dev)) {
812                 retval = PTR_ERR(tty_dev);
813                 goto error;
814         }
815
816         return 0;
817 error:
818         release_minor(gb_tty);
819 error_version:
820         connection->private = NULL;
821         kfree(gb_tty);
822         return retval;
823 }
824
825 static void gb_uart_connection_exit(struct gb_connection *connection)
826 {
827         struct gb_tty *gb_tty = connection->private;
828         struct tty_struct *tty;
829
830         if (!gb_tty)
831                 return;
832
833         mutex_lock(&gb_tty->mutex);
834         gb_tty->disconnected = true;
835
836         wake_up_all(&gb_tty->wioctl);
837         connection->private = NULL;
838         mutex_unlock(&gb_tty->mutex);
839
840         tty = tty_port_tty_get(&gb_tty->port);
841         if (tty) {
842                 tty_vhangup(tty);
843                 tty_kref_put(tty);
844         }
845         /* FIXME - stop all traffic */
846
847         tty_unregister_device(gb_tty_driver, gb_tty->minor);
848
849         /* FIXME - free transmit / recieve buffers */
850
851         tty_port_put(&gb_tty->port);
852
853         kfree(gb_tty);
854
855         /* If last device is gone, tear down the tty structures */
856         if (atomic_dec_return(&reference_count) == 0)
857                 gb_tty_exit();
858 }
859
860 static int gb_tty_init(void)
861 {
862         int retval = 0;
863
864         gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
865         if (IS_ERR(gb_tty_driver)) {
866                 pr_err("Can not allocate tty driver\n");
867                 retval = -ENOMEM;
868                 goto fail_unregister_dev;
869         }
870
871         gb_tty_driver->driver_name = "gb";
872         gb_tty_driver->name = GB_NAME;
873         gb_tty_driver->major = 0;
874         gb_tty_driver->minor_start = 0;
875         gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
876         gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
877         gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
878         gb_tty_driver->init_termios = tty_std_termios;
879         gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
880         tty_set_operations(gb_tty_driver, &gb_ops);
881
882         retval = tty_register_driver(gb_tty_driver);
883         if (retval) {
884                 pr_err("Can not register tty driver: %d\n", retval);
885                 goto fail_put_gb_tty;
886         }
887
888         return 0;
889
890 fail_put_gb_tty:
891         put_tty_driver(gb_tty_driver);
892 fail_unregister_dev:
893         return retval;
894 }
895
896 static void gb_tty_exit(void)
897 {
898         int major = MAJOR(gb_tty_driver->major);
899         int minor = gb_tty_driver->minor_start;
900
901         tty_unregister_driver(gb_tty_driver);
902         put_tty_driver(gb_tty_driver);
903         unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
904 }
905
906 static struct gb_protocol uart_protocol = {
907         .id                     = GREYBUS_PROTOCOL_UART,
908         .major                  = 0,
909         .minor                  = 1,
910         .connection_init        = gb_uart_connection_init,
911         .connection_exit        = gb_uart_connection_exit,
912         .request_recv           = NULL, /* FIXME we have 2 types of requests!!! */
913 };
914
915 bool gb_uart_protocol_init(void)
916 {
917         return gb_protocol_register(&uart_protocol);
918 }
919
920 void gb_uart_protocol_exit(void)
921 {
922         gb_protocol_deregister(&uart_protocol);
923 }