Merge branch 'topic/kmemtrace' of git://git.kernel.org/pub/scm/linux/kernel/git/penbe...
[pandora-kernel.git] / drivers / isdn / gigaset / interface.c
1 /*
2  * interface to user space for the gigaset driver
3  *
4  * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de>
5  *
6  * =====================================================================
7  *    This program is free software; you can redistribute it and/or
8  *    modify it under the terms of the GNU General Public License as
9  *    published by the Free Software Foundation; either version 2 of
10  *    the License, or (at your option) any later version.
11  * =====================================================================
12  */
13
14 #include "gigaset.h"
15 #include <linux/gigaset_dev.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18
19 /*** our ioctls ***/
20
21 static int if_lock(struct cardstate *cs, int *arg)
22 {
23         int cmd = *arg;
24
25         gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
26
27         if (cmd > 1)
28                 return -EINVAL;
29
30         if (cmd < 0) {
31                 *arg = cs->mstate == MS_LOCKED;
32                 return 0;
33         }
34
35         if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
36                 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
37                 cs->ops->baud_rate(cs, B115200);
38                 cs->ops->set_line_ctrl(cs, CS8);
39                 cs->control_state = TIOCM_DTR|TIOCM_RTS;
40         }
41
42         cs->waiting = 1;
43         if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
44                                NULL, cmd, NULL)) {
45                 cs->waiting = 0;
46                 return -ENOMEM;
47         }
48
49         gig_dbg(DEBUG_CMD, "scheduling IF_LOCK");
50         gigaset_schedule_event(cs);
51
52         wait_event(cs->waitqueue, !cs->waiting);
53
54         if (cs->cmd_result >= 0) {
55                 *arg = cs->cmd_result;
56                 return 0;
57         }
58
59         return cs->cmd_result;
60 }
61
62 static int if_version(struct cardstate *cs, unsigned arg[4])
63 {
64         static const unsigned version[4] = GIG_VERSION;
65         static const unsigned compat[4] = GIG_COMPAT;
66         unsigned cmd = arg[0];
67
68         gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
69
70         switch (cmd) {
71         case GIGVER_DRIVER:
72                 memcpy(arg, version, sizeof version);
73                 return 0;
74         case GIGVER_COMPAT:
75                 memcpy(arg, compat, sizeof compat);
76                 return 0;
77         case GIGVER_FWBASE:
78                 cs->waiting = 1;
79                 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
80                                        NULL, 0, arg)) {
81                         cs->waiting = 0;
82                         return -ENOMEM;
83                 }
84
85                 gig_dbg(DEBUG_CMD, "scheduling IF_VER");
86                 gigaset_schedule_event(cs);
87
88                 wait_event(cs->waitqueue, !cs->waiting);
89
90                 if (cs->cmd_result >= 0)
91                         return 0;
92
93                 return cs->cmd_result;
94         default:
95                 return -EINVAL;
96         }
97 }
98
99 static int if_config(struct cardstate *cs, int *arg)
100 {
101         gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
102
103         if (*arg != 1)
104                 return -EINVAL;
105
106         if (cs->mstate != MS_LOCKED)
107                 return -EBUSY;
108
109         if (!cs->connected) {
110                 pr_err("%s: not connected\n", __func__);
111                 return -ENODEV;
112         }
113
114         *arg = 0;
115         return gigaset_enterconfigmode(cs);
116 }
117
118 /*** the terminal driver ***/
119 /* stolen from usbserial and some other tty drivers */
120
121 static int  if_open(struct tty_struct *tty, struct file *filp);
122 static void if_close(struct tty_struct *tty, struct file *filp);
123 static int  if_ioctl(struct tty_struct *tty, struct file *file,
124                      unsigned int cmd, unsigned long arg);
125 static int  if_write_room(struct tty_struct *tty);
126 static int  if_chars_in_buffer(struct tty_struct *tty);
127 static void if_throttle(struct tty_struct *tty);
128 static void if_unthrottle(struct tty_struct *tty);
129 static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
130 static int  if_tiocmget(struct tty_struct *tty, struct file *file);
131 static int  if_tiocmset(struct tty_struct *tty, struct file *file,
132                         unsigned int set, unsigned int clear);
133 static int  if_write(struct tty_struct *tty,
134                      const unsigned char *buf, int count);
135
136 static const struct tty_operations if_ops = {
137         .open =                 if_open,
138         .close =                if_close,
139         .ioctl =                if_ioctl,
140         .write =                if_write,
141         .write_room =           if_write_room,
142         .chars_in_buffer =      if_chars_in_buffer,
143         .set_termios =          if_set_termios,
144         .throttle =             if_throttle,
145         .unthrottle =           if_unthrottle,
146         .tiocmget =             if_tiocmget,
147         .tiocmset =             if_tiocmset,
148 };
149
150 static int if_open(struct tty_struct *tty, struct file *filp)
151 {
152         struct cardstate *cs;
153         unsigned long flags;
154
155         gig_dbg(DEBUG_IF, "%d+%d: %s()",
156                 tty->driver->minor_start, tty->index, __func__);
157
158         tty->driver_data = NULL;
159
160         cs = gigaset_get_cs_by_tty(tty);
161         if (!cs || !try_module_get(cs->driver->owner))
162                 return -ENODEV;
163
164         if (mutex_lock_interruptible(&cs->mutex))
165                 return -ERESTARTSYS; // FIXME -EINTR?
166         tty->driver_data = cs;
167
168         ++cs->open_count;
169
170         if (cs->open_count == 1) {
171                 spin_lock_irqsave(&cs->lock, flags);
172                 cs->tty = tty;
173                 spin_unlock_irqrestore(&cs->lock, flags);
174                 tty->low_latency = 1; //FIXME test
175         }
176
177         mutex_unlock(&cs->mutex);
178         return 0;
179 }
180
181 static void if_close(struct tty_struct *tty, struct file *filp)
182 {
183         struct cardstate *cs;
184         unsigned long flags;
185
186         cs = (struct cardstate *) tty->driver_data;
187         if (!cs) {
188                 pr_err("%s: no cardstate\n", __func__);
189                 return;
190         }
191
192         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
193
194         mutex_lock(&cs->mutex);
195
196         if (!cs->open_count)
197                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
198         else {
199                 if (!--cs->open_count) {
200                         spin_lock_irqsave(&cs->lock, flags);
201                         cs->tty = NULL;
202                         spin_unlock_irqrestore(&cs->lock, flags);
203                 }
204         }
205
206         mutex_unlock(&cs->mutex);
207
208         module_put(cs->driver->owner);
209 }
210
211 static int if_ioctl(struct tty_struct *tty, struct file *file,
212                     unsigned int cmd, unsigned long arg)
213 {
214         struct cardstate *cs;
215         int retval = -ENODEV;
216         int int_arg;
217         unsigned char buf[6];
218         unsigned version[4];
219
220         cs = (struct cardstate *) tty->driver_data;
221         if (!cs) {
222                 pr_err("%s: no cardstate\n", __func__);
223                 return -ENODEV;
224         }
225
226         gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
227
228         if (mutex_lock_interruptible(&cs->mutex))
229                 return -ERESTARTSYS; // FIXME -EINTR?
230
231         if (!cs->open_count)
232                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
233         else {
234                 retval = 0;
235                 switch (cmd) {
236                 case GIGASET_REDIR:
237                         retval = get_user(int_arg, (int __user *) arg);
238                         if (retval >= 0)
239                                 retval = if_lock(cs, &int_arg);
240                         if (retval >= 0)
241                                 retval = put_user(int_arg, (int __user *) arg);
242                         break;
243                 case GIGASET_CONFIG:
244                         retval = get_user(int_arg, (int __user *) arg);
245                         if (retval >= 0)
246                                 retval = if_config(cs, &int_arg);
247                         if (retval >= 0)
248                                 retval = put_user(int_arg, (int __user *) arg);
249                         break;
250                 case GIGASET_BRKCHARS:
251                         //FIXME test if MS_LOCKED
252                         if (!cs->connected) {
253                                 gig_dbg(DEBUG_ANY,
254                                     "can't communicate with unplugged device");
255                                 retval = -ENODEV;
256                                 break;
257                         }
258                         retval = copy_from_user(&buf,
259                                         (const unsigned char __user *) arg, 6)
260                                 ? -EFAULT : 0;
261                         if (retval >= 0) {
262                                 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
263                                                 6, (const unsigned char *) arg);
264                                 retval = cs->ops->brkchars(cs, buf);
265                         }
266                         break;
267                 case GIGASET_VERSION:
268                         retval = copy_from_user(version,
269                                         (unsigned __user *) arg, sizeof version)
270                                 ? -EFAULT : 0;
271                         if (retval >= 0)
272                                 retval = if_version(cs, version);
273                         if (retval >= 0)
274                                 retval = copy_to_user((unsigned __user *) arg,
275                                                       version, sizeof version)
276                                         ? -EFAULT : 0;
277                         break;
278                 default:
279                         gig_dbg(DEBUG_ANY, "%s: arg not supported - 0x%04x",
280                                 __func__, cmd);
281                         retval = -ENOIOCTLCMD;
282                 }
283         }
284
285         mutex_unlock(&cs->mutex);
286
287         return retval;
288 }
289
290 static int if_tiocmget(struct tty_struct *tty, struct file *file)
291 {
292         struct cardstate *cs;
293         int retval;
294
295         cs = (struct cardstate *) tty->driver_data;
296         if (!cs) {
297                 pr_err("%s: no cardstate\n", __func__);
298                 return -ENODEV;
299         }
300
301         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
302
303         if (mutex_lock_interruptible(&cs->mutex))
304                 return -ERESTARTSYS; // FIXME -EINTR?
305
306         // FIXME read from device?
307         retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
308
309         mutex_unlock(&cs->mutex);
310
311         return retval;
312 }
313
314 static int if_tiocmset(struct tty_struct *tty, struct file *file,
315                        unsigned int set, unsigned int clear)
316 {
317         struct cardstate *cs;
318         int retval;
319         unsigned mc;
320
321         cs = (struct cardstate *) tty->driver_data;
322         if (!cs) {
323                 pr_err("%s: no cardstate\n", __func__);
324                 return -ENODEV;
325         }
326
327         gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
328                 cs->minor_index, __func__, set, clear);
329
330         if (mutex_lock_interruptible(&cs->mutex))
331                 return -ERESTARTSYS; // FIXME -EINTR?
332
333         if (!cs->connected) {
334                 gig_dbg(DEBUG_ANY, "can't communicate with unplugged device");
335                 retval = -ENODEV;
336         } else {
337                 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
338                 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
339                 cs->control_state = mc;
340         }
341
342         mutex_unlock(&cs->mutex);
343
344         return retval;
345 }
346
347 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
348 {
349         struct cardstate *cs;
350         int retval = -ENODEV;
351
352         cs = (struct cardstate *) tty->driver_data;
353         if (!cs) {
354                 pr_err("%s: no cardstate\n", __func__);
355                 return -ENODEV;
356         }
357
358         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
359
360         if (mutex_lock_interruptible(&cs->mutex))
361                 return -ERESTARTSYS; // FIXME -EINTR?
362
363         if (!cs->open_count)
364                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
365         else if (cs->mstate != MS_LOCKED) {
366                 dev_warn(cs->dev, "can't write to unlocked device\n");
367                 retval = -EBUSY;
368         } else if (!cs->connected) {
369                 gig_dbg(DEBUG_ANY, "can't write to unplugged device");
370                 retval = -EBUSY; //FIXME
371         } else {
372                 retval = cs->ops->write_cmd(cs, buf, count,
373                                             &cs->if_wake_tasklet);
374         }
375
376         mutex_unlock(&cs->mutex);
377
378         return retval;
379 }
380
381 static int if_write_room(struct tty_struct *tty)
382 {
383         struct cardstate *cs;
384         int retval = -ENODEV;
385
386         cs = (struct cardstate *) tty->driver_data;
387         if (!cs) {
388                 pr_err("%s: no cardstate\n", __func__);
389                 return -ENODEV;
390         }
391
392         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
393
394         if (mutex_lock_interruptible(&cs->mutex))
395                 return -ERESTARTSYS; // FIXME -EINTR?
396
397         if (!cs->open_count)
398                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
399         else if (cs->mstate != MS_LOCKED) {
400                 dev_warn(cs->dev, "can't write to unlocked device\n");
401                 retval = -EBUSY;
402         } else if (!cs->connected) {
403                 gig_dbg(DEBUG_ANY, "can't write to unplugged device");
404                 retval = -EBUSY; //FIXME
405         } else
406                 retval = cs->ops->write_room(cs);
407
408         mutex_unlock(&cs->mutex);
409
410         return retval;
411 }
412
413 static int if_chars_in_buffer(struct tty_struct *tty)
414 {
415         struct cardstate *cs;
416         int retval = -ENODEV;
417
418         cs = (struct cardstate *) tty->driver_data;
419         if (!cs) {
420                 pr_err("%s: no cardstate\n", __func__);
421                 return -ENODEV;
422         }
423
424         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
425
426         if (mutex_lock_interruptible(&cs->mutex))
427                 return -ERESTARTSYS; // FIXME -EINTR?
428
429         if (!cs->open_count)
430                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
431         else if (cs->mstate != MS_LOCKED) {
432                 dev_warn(cs->dev, "can't write to unlocked device\n");
433                 retval = -EBUSY;
434         } else if (!cs->connected) {
435                 gig_dbg(DEBUG_ANY, "can't write to unplugged device");
436                 retval = -EBUSY; //FIXME
437         } else
438                 retval = cs->ops->chars_in_buffer(cs);
439
440         mutex_unlock(&cs->mutex);
441
442         return retval;
443 }
444
445 static void if_throttle(struct tty_struct *tty)
446 {
447         struct cardstate *cs;
448
449         cs = (struct cardstate *) tty->driver_data;
450         if (!cs) {
451                 pr_err("%s: no cardstate\n", __func__);
452                 return;
453         }
454
455         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
456
457         mutex_lock(&cs->mutex);
458
459         if (!cs->open_count)
460                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
461         else {
462                 //FIXME
463         }
464
465         mutex_unlock(&cs->mutex);
466 }
467
468 static void if_unthrottle(struct tty_struct *tty)
469 {
470         struct cardstate *cs;
471
472         cs = (struct cardstate *) tty->driver_data;
473         if (!cs) {
474                 pr_err("%s: no cardstate\n", __func__);
475                 return;
476         }
477
478         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
479
480         mutex_lock(&cs->mutex);
481
482         if (!cs->open_count)
483                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
484         else {
485                 //FIXME
486         }
487
488         mutex_unlock(&cs->mutex);
489 }
490
491 static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
492 {
493         struct cardstate *cs;
494         unsigned int iflag;
495         unsigned int cflag;
496         unsigned int old_cflag;
497         unsigned int control_state, new_state;
498
499         cs = (struct cardstate *) tty->driver_data;
500         if (!cs) {
501                 pr_err("%s: no cardstate\n", __func__);
502                 return;
503         }
504
505         gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
506
507         mutex_lock(&cs->mutex);
508
509         if (!cs->open_count) {
510                 dev_warn(cs->dev, "%s: device not opened\n", __func__);
511                 goto out;
512         }
513
514         if (!cs->connected) {
515                 gig_dbg(DEBUG_ANY, "can't communicate with unplugged device");
516                 goto out;
517         }
518
519         // stolen from mct_u232.c
520         iflag = tty->termios->c_iflag;
521         cflag = tty->termios->c_cflag;
522         old_cflag = old ? old->c_cflag : cflag; //FIXME?
523         gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
524                 cs->minor_index, iflag, cflag, old_cflag);
525
526         /* get a local copy of the current port settings */
527         control_state = cs->control_state;
528
529         /*
530          * Update baud rate.
531          * Do not attempt to cache old rates and skip settings,
532          * disconnects screw such tricks up completely.
533          * Premature optimization is the root of all evil.
534          */
535
536         /* reassert DTR and (maybe) RTS on transition from B0 */
537         if ((old_cflag & CBAUD) == B0) {
538                 new_state = control_state | TIOCM_DTR;
539                 /* don't set RTS if using hardware flow control */
540                 if (!(old_cflag & CRTSCTS))
541                         new_state |= TIOCM_RTS;
542                 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
543                         cs->minor_index,
544                         (new_state & TIOCM_RTS) ? " only" : "/RTS");
545                 cs->ops->set_modem_ctrl(cs, control_state, new_state);
546                 control_state = new_state;
547         }
548
549         cs->ops->baud_rate(cs, cflag & CBAUD);
550
551         if ((cflag & CBAUD) == B0) {
552                 /* Drop RTS and DTR */
553                 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
554                 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
555                 cs->ops->set_modem_ctrl(cs, control_state, new_state);
556                 control_state = new_state;
557         }
558
559         /*
560          * Update line control register (LCR)
561          */
562
563         cs->ops->set_line_ctrl(cs, cflag);
564
565         /* save off the modified port settings */
566         cs->control_state = control_state;
567
568 out:
569         mutex_unlock(&cs->mutex);
570 }
571
572
573 /* wakeup tasklet for the write operation */
574 static void if_wake(unsigned long data)
575 {
576         struct cardstate *cs = (struct cardstate *) data;
577
578         if (cs->tty)
579                 tty_wakeup(cs->tty);
580 }
581
582 /*** interface to common ***/
583
584 void gigaset_if_init(struct cardstate *cs)
585 {
586         struct gigaset_driver *drv;
587
588         drv = cs->driver;
589         if (!drv->have_tty)
590                 return;
591
592         tasklet_init(&cs->if_wake_tasklet, &if_wake, (unsigned long) cs);
593
594         mutex_lock(&cs->mutex);
595         cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
596
597         if (!IS_ERR(cs->tty_dev))
598                 dev_set_drvdata(cs->tty_dev, cs);
599         else {
600                 dev_warn(cs->dev,
601                          "could not register device to the tty subsystem\n");
602                 cs->tty_dev = NULL;
603         }
604         mutex_unlock(&cs->mutex);
605 }
606
607 void gigaset_if_free(struct cardstate *cs)
608 {
609         struct gigaset_driver *drv;
610
611         drv = cs->driver;
612         if (!drv->have_tty)
613                 return;
614
615         tasklet_disable(&cs->if_wake_tasklet);
616         tasklet_kill(&cs->if_wake_tasklet);
617         cs->tty_dev = NULL;
618         tty_unregister_device(drv->tty, cs->minor_index);
619 }
620
621 void gigaset_if_receive(struct cardstate *cs,
622                         unsigned char *buffer, size_t len)
623 {
624         unsigned long flags;
625         struct tty_struct *tty;
626
627         spin_lock_irqsave(&cs->lock, flags);
628         if ((tty = cs->tty) == NULL)
629                 gig_dbg(DEBUG_ANY, "receive on closed device");
630         else {
631                 tty_buffer_request_room(tty, len);
632                 tty_insert_flip_string(tty, buffer, len);
633                 tty_flip_buffer_push(tty);
634         }
635         spin_unlock_irqrestore(&cs->lock, flags);
636 }
637 EXPORT_SYMBOL_GPL(gigaset_if_receive);
638
639 /* gigaset_if_initdriver
640  * Initialize tty interface.
641  * parameters:
642  *      drv             Driver
643  *      procname        Name of the driver (e.g. for /proc/tty/drivers)
644  *      devname         Name of the device files (prefix without minor number)
645  */
646 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
647                            const char *devname)
648 {
649         unsigned minors = drv->minors;
650         int ret;
651         struct tty_driver *tty;
652
653         drv->have_tty = 0;
654
655         if ((drv->tty = alloc_tty_driver(minors)) == NULL)
656                 goto enomem;
657         tty = drv->tty;
658
659         tty->magic =            TTY_DRIVER_MAGIC,
660         tty->major =            GIG_MAJOR,
661         tty->type =             TTY_DRIVER_TYPE_SERIAL,
662         tty->subtype =          SERIAL_TYPE_NORMAL,
663         tty->flags =            TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
664
665         tty->driver_name =      procname;
666         tty->name =             devname;
667         tty->minor_start =      drv->minor;
668         tty->num =              drv->minors;
669
670         tty->owner =            THIS_MODULE;
671
672         tty->init_termios          = tty_std_termios; //FIXME
673         tty->init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL; //FIXME
674         tty_set_operations(tty, &if_ops);
675
676         ret = tty_register_driver(tty);
677         if (ret < 0) {
678                 pr_err("error %d registering tty driver\n", ret);
679                 goto error;
680         }
681         gig_dbg(DEBUG_IF, "tty driver initialized");
682         drv->have_tty = 1;
683         return;
684
685 enomem:
686         pr_err("out of memory\n");
687 error:
688         if (drv->tty)
689                 put_tty_driver(drv->tty);
690 }
691
692 void gigaset_if_freedriver(struct gigaset_driver *drv)
693 {
694         if (!drv->have_tty)
695                 return;
696
697         drv->have_tty = 0;
698         tty_unregister_driver(drv->tty);
699         put_tty_driver(drv->tty);
700 }