]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/uart-gb.c
greybus: Merge branch 'master' into driver_model_rework
[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 struct gb_tty {
37         struct tty_port port;
38         struct gb_connection *connection;
39         u16 cport_id;
40         unsigned int minor;
41         unsigned char clocal;
42         unsigned int throttled:1;
43         unsigned int throttle_req:1;
44         bool disconnected;
45         int writesize;          // FIXME - set this somehow.
46         spinlock_t read_lock;
47         spinlock_t write_lock;
48         struct async_icount iocount;
49         struct async_icount oldcount;
50         wait_queue_head_t wioctl;
51         struct mutex mutex;
52 };
53
54 static const struct greybus_module_id id_table[] = {
55         { GREYBUS_DEVICE(0x45, 0x45) }, /* make shit up */
56         { },    /* terminating NULL entry */
57 };
58
59 static struct tty_driver *gb_tty_driver;
60 static DEFINE_IDR(tty_minors);
61 static DEFINE_MUTEX(table_lock);
62 static atomic_t reference_count = ATOMIC_INIT(0);
63
64 static int gb_tty_init(void);
65 static void gb_tty_exit(void);
66
67 static struct gb_tty *get_gb_by_minor(unsigned minor)
68 {
69         struct gb_tty *gb_tty;
70
71         mutex_lock(&table_lock);
72         gb_tty = idr_find(&tty_minors, minor);
73         if (gb_tty) {
74                 mutex_lock(&gb_tty->mutex);
75                 if (gb_tty->disconnected) {
76                         mutex_unlock(&gb_tty->mutex);
77                         gb_tty = NULL;
78                 } else {
79                         tty_port_get(&gb_tty->port);
80                         mutex_unlock(&gb_tty->mutex);
81                 }
82         }
83         mutex_unlock(&table_lock);
84         return gb_tty;
85 }
86
87 static int alloc_minor(struct gb_tty *gb_tty)
88 {
89         int minor;
90
91         mutex_lock(&table_lock);
92         minor = idr_alloc(&tty_minors, gb_tty, 0, GB_NUM_MINORS, GFP_KERNEL);
93         mutex_unlock(&table_lock);
94         if (minor >= 0)
95                 gb_tty->minor = minor;
96         return minor;
97 }
98
99 static void release_minor(struct gb_tty *gb_tty)
100 {
101         int minor = gb_tty->minor;
102
103         gb_tty->minor = 0;      /* Maybe should use an invalid value instead */
104         mutex_lock(&table_lock);
105         idr_remove(&tty_minors, minor);
106         mutex_unlock(&table_lock);
107 }
108
109 static int gb_tty_install(struct tty_driver *driver, struct tty_struct *tty)
110 {
111         struct gb_tty *gb_tty;
112         int retval;
113
114         gb_tty = get_gb_by_minor(tty->index);
115         if (!gb_tty)
116                 return -ENODEV;
117
118         retval = tty_standard_install(driver, tty);
119         if (retval)
120                 goto error;
121
122         tty->driver_data = gb_tty;
123         return 0;
124 error:
125         tty_port_put(&gb_tty->port);
126         return retval;
127 }
128
129 static int gb_tty_open(struct tty_struct *tty, struct file *file)
130 {
131         struct gb_tty *gb_tty = tty->driver_data;
132
133         return tty_port_open(&gb_tty->port, tty, file);
134 }
135
136 static void gb_tty_close(struct tty_struct *tty, struct file *file)
137 {
138         struct gb_tty *gb_tty = tty->driver_data;
139
140         tty_port_close(&gb_tty->port, tty, file);
141 }
142
143 static void gb_tty_cleanup(struct tty_struct *tty)
144 {
145         struct gb_tty *gb_tty = tty->driver_data;
146
147         tty_port_put(&gb_tty->port);
148 }
149
150 static void gb_tty_hangup(struct tty_struct *tty)
151 {
152         struct gb_tty *gb_tty = tty->driver_data;
153
154         tty_port_hangup(&gb_tty->port);
155 }
156
157 static int gb_tty_write(struct tty_struct *tty, const unsigned char *buf,
158                         int count)
159 {
160 //      struct gb_tty *gb_tty = tty->driver_data;
161
162         // FIXME - actually implement...
163
164         return 0;
165 }
166
167 static int gb_tty_write_room(struct tty_struct *tty)
168 {
169 //      struct gb_tty *gb_tty = tty->driver_data;
170
171         // FIXME - how much do we want to say we have room for?
172         return 0;
173 }
174
175 static int gb_tty_chars_in_buffer(struct tty_struct *tty)
176 {
177 //      struct gb_tty *gb_tty = tty->driver_data;
178
179         // FIXME - how many left to send?
180         return 0;
181 }
182
183 static int gb_tty_break_ctl(struct tty_struct *tty, int state)
184 {
185 //      struct gb_tty *gb_tty = tty->driver_data;
186
187         // FIXME - send a break, if asked to...
188         return 0;
189 }
190
191 static void gb_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
192 {
193         // FIXME - is this it???
194         tty_termios_copy_hw(&tty->termios, old);
195 }
196
197 static int gb_tty_tiocmget(struct tty_struct *tty)
198 {
199 //      struct gb_tty *gb_tty = tty->driver_data;
200
201         // FIXME - get some tiocms!
202         return 0;
203 }
204
205 static int gb_tty_tiocmset(struct tty_struct *tty, unsigned int set,
206                            unsigned int clear)
207 {
208 //      struct gb_tty *gb_tty = tty->driver_data;
209
210         // FIXME - set some tiocms!
211         return 0;
212 }
213
214 static void gb_tty_throttle(struct tty_struct *tty)
215 {
216         struct gb_tty *gb_tty = tty->driver_data;
217
218         spin_lock_irq(&gb_tty->read_lock);
219         gb_tty->throttle_req = 1;
220         spin_unlock_irq(&gb_tty->read_lock);
221 }
222
223 static void gb_tty_unthrottle(struct tty_struct *tty)
224 {
225         struct gb_tty *gb_tty = tty->driver_data;
226         unsigned int was_throttled;
227
228         spin_lock_irq(&gb_tty->read_lock);
229         was_throttled = gb_tty->throttled;
230         gb_tty->throttle_req = 0;
231         gb_tty->throttled = 0;
232         spin_unlock_irq(&gb_tty->read_lock);
233
234         if (was_throttled) {
235                 // FIXME - send more data
236         }
237 }
238
239 static int get_serial_info(struct gb_tty *gb_tty,
240                            struct serial_struct __user *info)
241 {
242         struct serial_struct tmp;
243
244         if (!info)
245                 return -EINVAL;
246
247         memset(&tmp, 0, sizeof(tmp));
248         tmp.flags = ASYNC_LOW_LATENCY;
249         tmp.xmit_fifo_size = gb_tty->writesize;
250         tmp.baud_base = 0;      // FIXME
251         tmp.close_delay = gb_tty->port.close_delay / 10;
252         tmp.closing_wait = gb_tty->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
253                                 ASYNC_CLOSING_WAIT_NONE : gb_tty->port.closing_wait / 10;
254
255         if (copy_to_user(info, &tmp, sizeof(tmp)))
256                 return -EFAULT;
257         return 0;
258 }
259
260 static int set_serial_info(struct gb_tty *gb_tty,
261                            struct serial_struct __user *newinfo)
262 {
263         struct serial_struct new_serial;
264         unsigned int closing_wait;
265         unsigned int close_delay;
266         int retval = 0;
267
268         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
269                 return -EFAULT;
270
271         close_delay = new_serial.close_delay * 10;
272         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
273                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
274
275         mutex_lock(&gb_tty->port.mutex);
276         if (!capable(CAP_SYS_ADMIN)) {
277                 if ((close_delay != gb_tty->port.close_delay) ||
278                     (closing_wait != gb_tty->port.closing_wait))
279                         retval = -EPERM;
280                 else
281                         retval = -EOPNOTSUPP;
282         } else {
283                 gb_tty->port.close_delay = close_delay;
284                 gb_tty->port.closing_wait = closing_wait;
285         }
286         mutex_unlock(&gb_tty->port.mutex);
287         return retval;
288 }
289
290 static int wait_serial_change(struct gb_tty *gb_tty, unsigned long arg)
291 {
292         int retval = 0;
293         DECLARE_WAITQUEUE(wait, current);
294         struct async_icount old;
295         struct async_icount new;
296
297         if (!(arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD)))
298                 return -EINVAL;
299
300         do {
301                 spin_lock_irq(&gb_tty->read_lock);
302                 old = gb_tty->oldcount;
303                 new = gb_tty->iocount;
304                 gb_tty->oldcount = new;
305                 spin_unlock_irq(&gb_tty->read_lock);
306
307                 if ((arg & TIOCM_DSR) && (old.dsr != new.dsr))
308                         break;
309                 if ((arg & TIOCM_CD) && (old.dcd != new.dcd))
310                         break;
311                 if ((arg & TIOCM_RI) && (old.rng != new.rng))
312                         break;
313
314                 add_wait_queue(&gb_tty->wioctl, &wait);
315                 set_current_state(TASK_INTERRUPTIBLE);
316                 schedule();
317                 remove_wait_queue(&gb_tty->wioctl, &wait);
318                 if (gb_tty->disconnected) {
319                         if (arg & TIOCM_CD)
320                                 break;
321                         retval = -ENODEV;
322                 } else if (signal_pending(current)) {
323                         retval = -ERESTARTSYS;
324                 }
325         } while (!retval);
326
327         return retval;
328 }
329
330 static int get_serial_usage(struct gb_tty *gb_tty,
331                             struct serial_icounter_struct __user *count)
332 {
333         struct serial_icounter_struct icount;
334         int retval = 0;
335
336         memset(&icount, 0, sizeof(icount));
337         icount.dsr = gb_tty->iocount.dsr;
338         icount.rng = gb_tty->iocount.rng;
339         icount.dcd = gb_tty->iocount.dcd;
340         icount.frame = gb_tty->iocount.frame;
341         icount.overrun = gb_tty->iocount.overrun;
342         icount.parity = gb_tty->iocount.parity;
343         icount.brk = gb_tty->iocount.brk;
344
345         if (copy_to_user(count, &icount, sizeof(icount)) > 0)
346                 retval = -EFAULT;
347
348         return retval;
349 }
350
351 static int gb_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
352                         unsigned long arg)
353 {
354         struct gb_tty *gb_tty = tty->driver_data;
355
356         switch (cmd) {
357         case TIOCGSERIAL:
358                 return get_serial_info(gb_tty,
359                                        (struct serial_struct __user *)arg);
360         case TIOCSSERIAL:
361                 return set_serial_info(gb_tty,
362                                        (struct serial_struct __user *)arg);
363         case TIOCMIWAIT:
364                 return wait_serial_change(gb_tty, arg);
365         case TIOCGICOUNT:
366                 return get_serial_usage(gb_tty,
367                                         (struct serial_icounter_struct __user *)arg);
368         }
369
370         return -ENOIOCTLCMD;
371 }
372
373
374 static const struct tty_operations gb_ops = {
375         .install =              gb_tty_install,
376         .open =                 gb_tty_open,
377         .close =                gb_tty_close,
378         .cleanup =              gb_tty_cleanup,
379         .hangup =               gb_tty_hangup,
380         .write =                gb_tty_write,
381         .write_room =           gb_tty_write_room,
382         .ioctl =                gb_tty_ioctl,
383         .throttle =             gb_tty_throttle,
384         .unthrottle =           gb_tty_unthrottle,
385         .chars_in_buffer =      gb_tty_chars_in_buffer,
386         .break_ctl =            gb_tty_break_ctl,
387         .set_termios =          gb_tty_set_termios,
388         .tiocmget =             gb_tty_tiocmget,
389         .tiocmset =             gb_tty_tiocmset,
390 };
391
392
393 static int gb_uart_connection_init(struct gb_connection *connection)
394 {
395         struct gb_tty *gb_tty;
396         struct device *tty_dev;
397         int retval;
398         int minor;
399
400         /* First time here, initialize the tty structures */
401         if (atomic_inc_return(&reference_count) == 1) {
402                 retval = gb_tty_init();
403                 if (retval) {
404                         atomic_dec(&reference_count);
405                         return retval;
406                 }
407         }
408
409         gb_tty = kzalloc(sizeof(*gb_tty), GFP_KERNEL);
410         if (!gb_tty)
411                 return -ENOMEM;
412
413         minor = alloc_minor(gb_tty);
414         if (minor < 0) {
415                 if (minor == -ENOSPC) {
416                         dev_err(&connection->dev, "no more free minor numbers\n");
417                         return -ENODEV;
418                 }
419                 return minor;
420         }
421
422         gb_tty->minor = minor;
423         gb_tty->connection = connection;
424         spin_lock_init(&gb_tty->write_lock);
425         spin_lock_init(&gb_tty->read_lock);
426         init_waitqueue_head(&gb_tty->wioctl);
427         mutex_init(&gb_tty->mutex);
428
429         /* FIXME - allocate gb buffers */
430
431         connection->private = gb_tty;
432
433         tty_dev = tty_port_register_device(&gb_tty->port, gb_tty_driver, minor,
434                                            &connection->dev);
435         if (IS_ERR(tty_dev)) {
436                 retval = PTR_ERR(tty_dev);
437                 goto error;
438         }
439
440         return 0;
441 error:
442         connection->private = NULL;
443         release_minor(gb_tty);
444         return retval;
445 }
446
447 static void gb_uart_connection_exit(struct gb_connection *connection)
448 {
449         struct gb_tty *gb_tty = connection->private;
450         struct tty_struct *tty;
451
452         if (!gb_tty)
453                 return;
454
455         mutex_lock(&gb_tty->mutex);
456         gb_tty->disconnected = true;
457
458         wake_up_all(&gb_tty->wioctl);
459         connection->private = NULL;
460         mutex_unlock(&gb_tty->mutex);
461
462         tty = tty_port_tty_get(&gb_tty->port);
463         if (tty) {
464                 tty_vhangup(tty);
465                 tty_kref_put(tty);
466         }
467         /* FIXME - stop all traffic */
468
469         tty_unregister_device(gb_tty_driver, gb_tty->minor);
470
471         /* FIXME - free transmit / recieve buffers */
472
473         tty_port_put(&gb_tty->port);
474
475         kfree(gb_tty);
476
477         /* If last device is gone, tear down the tty structures */
478         if (atomic_dec_return(&reference_count) == 0)
479                 gb_tty_exit();
480 }
481
482 static int gb_tty_init(void)
483 {
484         int retval = 0;
485
486         gb_tty_driver = tty_alloc_driver(GB_NUM_MINORS, 0);
487         if (IS_ERR(gb_tty_driver)) {
488                 pr_err("Can not allocate tty driver\n");
489                 retval = -ENOMEM;
490                 goto fail_unregister_dev;
491         }
492
493         gb_tty_driver->driver_name = "gb";
494         gb_tty_driver->name = GB_NAME;
495         gb_tty_driver->major = 0;
496         gb_tty_driver->minor_start = 0;
497         gb_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
498         gb_tty_driver->subtype = SERIAL_TYPE_NORMAL;
499         gb_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
500         gb_tty_driver->init_termios = tty_std_termios;
501         gb_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
502         tty_set_operations(gb_tty_driver, &gb_ops);
503
504         retval = tty_register_driver(gb_tty_driver);
505         if (retval) {
506                 pr_err("Can not register tty driver: %d\n", retval);
507                 goto fail_put_gb_tty;
508         }
509
510         return 0;
511
512 fail_put_gb_tty:
513         put_tty_driver(gb_tty_driver);
514 fail_unregister_dev:
515         return retval;
516 }
517
518 static void gb_tty_exit(void)
519 {
520         int major = MAJOR(gb_tty_driver->major);
521         int minor = gb_tty_driver->minor_start;
522
523         tty_unregister_driver(gb_tty_driver);
524         put_tty_driver(gb_tty_driver);
525         unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
526 }
527
528 struct gb_connection_handler gb_uart_connection_handler = {
529         .connection_init        = gb_uart_connection_init,
530         .connection_exit        = gb_uart_connection_exit,
531 };