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