patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / irda / irtty-sir.c
1 /*********************************************************************
2  *                
3  * Filename:      irtty-sir.c
4  * Version:       2.0
5  * Description:   IrDA line discipline implementation
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Tue Dec  9 21:18:38 1997
9  * Modified at:   Sun Oct 27 22:13:30 2002
10  * Modified by:   Martin Diehl <mad@mdiehl.de>
11  * Sources:       slip.c by Laurence Culhane,   <loz@holmes.demon.co.uk>
12  *                          Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
13  * 
14  *     Copyright (c) 1998-2000 Dag Brattli,
15  *     Copyright (c) 2002 Martin Diehl,
16  *     All Rights Reserved.
17  *      
18  *     This program is free software; you can redistribute it and/or 
19  *     modify it under the terms of the GNU General Public License as 
20  *     published by the Free Software Foundation; either version 2 of 
21  *     the License, or (at your option) any later version.
22  *  
23  *     Neither Dag Brattli nor University of Tromsø admit liability nor
24  *     provide warranty for any of this software. This material is 
25  *     provided "AS-IS" and at no charge.
26  *     
27  ********************************************************************/    
28
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/tty.h>
32 #include <linux/init.h>
33 #include <asm/uaccess.h>
34 #include <linux/smp_lock.h>
35
36 #include <net/irda/irda.h>
37 #include <net/irda/irda_device.h>
38
39 #include "sir-dev.h"
40 #include "irtty-sir.h"
41
42 MODULE_PARM(qos_mtt_bits, "i");
43 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
44
45 static int qos_mtt_bits = 0x03;      /* 5 ms or more */
46
47 /* ------------------------------------------------------- */
48
49 /* device configuration callbacks always invoked with irda-thread context */
50
51 /* find out, how many chars we have in buffers below us
52  * this is allowed to lie, i.e. return less chars than we
53  * actually have. The returned value is used to determine
54  * how long the irdathread should wait before doing the
55  * real blocking wait_until_sent()
56  */
57
58 static int irtty_chars_in_buffer(struct sir_dev *dev)
59 {
60         struct sirtty_cb *priv = dev->priv;
61
62         ASSERT(priv != NULL, return -1;);
63         ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
64
65         return priv->tty->driver->chars_in_buffer(priv->tty);
66 }
67
68 /* Wait (sleep) until underlaying hardware finished transmission
69  * i.e. hardware buffers are drained
70  * this must block and not return before all characters are really sent
71  *
72  * If the tty sits on top of a 16550A-like uart, there are typically
73  * up to 16 bytes in the fifo - f.e. 9600 bps 8N1 needs 16.7 msec
74  *
75  * With usbserial the uart-fifo is basically replaced by the converter's
76  * outgoing endpoint buffer, which can usually hold 64 bytes (at least).
77  * With pl2303 it appears we are safe with 60msec here.
78  *
79  * I really wish all serial drivers would provide
80  * correct implementation of wait_until_sent()
81  */
82
83 #define USBSERIAL_TX_DONE_DELAY 60
84
85 static void irtty_wait_until_sent(struct sir_dev *dev)
86 {
87         struct sirtty_cb *priv = dev->priv;
88         struct tty_struct *tty;
89
90         ASSERT(priv != NULL, return;);
91         ASSERT(priv->magic == IRTTY_MAGIC, return;);
92
93         tty = priv->tty;
94         if (tty->driver->wait_until_sent) {
95                 lock_kernel();
96                 tty->driver->wait_until_sent(tty, msecs_to_jiffies(100));
97                 unlock_kernel();
98         }
99         else {
100                 set_task_state(current, TASK_UNINTERRUPTIBLE);
101                 schedule_timeout(msecs_to_jiffies(USBSERIAL_TX_DONE_DELAY));
102         }
103 }
104
105 /* 
106  *  Function irtty_change_speed (dev, speed)
107  *
108  *    Change the speed of the serial port.
109  *
110  * This may sleep in set_termios (usbserial driver f.e.) and must
111  * not be called from interrupt/timer/tasklet therefore.
112  * All such invocations are deferred to kIrDAd now so we can sleep there.
113  */
114
115 static int irtty_change_speed(struct sir_dev *dev, unsigned speed)
116 {
117         struct sirtty_cb *priv = dev->priv;
118         struct tty_struct *tty;
119         struct termios old_termios;
120         int cflag;
121
122         ASSERT(priv != NULL, return -1;);
123         ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
124
125         tty = priv->tty;
126
127         lock_kernel();
128         old_termios = *(tty->termios);
129         cflag = tty->termios->c_cflag;
130
131         cflag &= ~CBAUD;
132
133         IRDA_DEBUG(2, "%s(), Setting speed to %d\n", __FUNCTION__, speed);
134
135         switch (speed) {
136         case 1200:
137                 cflag |= B1200;
138                 break;
139         case 2400:
140                 cflag |= B2400;
141                 break;
142         case 4800:
143                 cflag |= B4800;
144                 break;
145         case 19200:
146                 cflag |= B19200;
147                 break;
148         case 38400:
149                 cflag |= B38400;
150                 break;
151         case 57600:
152                 cflag |= B57600;
153                 break;
154         case 115200:
155                 cflag |= B115200;
156                 break;
157         case 9600:
158         default:
159                 cflag |= B9600;
160                 break;
161         }       
162
163         tty->termios->c_cflag = cflag;
164         if (tty->driver->set_termios)
165                 tty->driver->set_termios(tty, &old_termios);
166         unlock_kernel();
167
168         priv->io.speed = speed;
169
170         return 0;
171 }
172
173 /*
174  * Function irtty_set_dtr_rts (dev, dtr, rts)
175  *
176  *    This function can be used by dongles etc. to set or reset the status
177  *    of the dtr and rts lines
178  */
179
180 static int irtty_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
181 {
182         struct sirtty_cb *priv = dev->priv;
183         int set = 0;
184         int clear = 0;
185
186         ASSERT(priv != NULL, return -1;);
187         ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
188
189         if (rts)
190                 set |= TIOCM_RTS;
191         else
192                 clear |= TIOCM_RTS;
193         if (dtr)
194                 set |= TIOCM_DTR;
195         else
196                 clear |= TIOCM_DTR;
197
198         /*
199          * We can't use ioctl() because it expects a non-null file structure,
200          * and we don't have that here.
201          * This function is not yet defined for all tty driver, so
202          * let's be careful... Jean II
203          */
204         ASSERT(priv->tty->driver->tiocmset != NULL, return -1;);
205         priv->tty->driver->tiocmset(priv->tty, NULL, set, clear);
206
207         return 0;
208 }
209
210 /* ------------------------------------------------------- */
211
212 /* called from sir_dev when there is more data to send
213  * context is either netdev->hard_xmit or some transmit-completion bh
214  * i.e. we are under spinlock here and must not sleep.
215  */
216
217 static int irtty_do_write(struct sir_dev *dev, const unsigned char *ptr, size_t len)
218 {
219         struct sirtty_cb *priv = dev->priv;
220         struct tty_struct *tty;
221         int writelen;
222
223         ASSERT(priv != NULL, return -1;);
224         ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
225
226         tty = priv->tty;
227         if (!tty->driver->write)
228                 return 0;
229         tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
230         if (tty->driver->write_room) {
231                 writelen = tty->driver->write_room(tty);
232                 if (writelen > len)
233                         writelen = len;
234         }
235         else
236                 writelen = len;
237         return tty->driver->write(tty, 0, ptr, writelen);
238 }
239
240 /* ------------------------------------------------------- */
241
242 /* irda line discipline callbacks */
243
244 /* 
245  *  Function irtty_receive_buf( tty, cp, count)
246  *
247  *    Handle the 'receiver data ready' interrupt.  This function is called
248  *    by the 'tty_io' module in the kernel when a block of IrDA data has
249  *    been received, which can now be decapsulated and delivered for
250  *    further processing 
251  *
252  * calling context depends on underlying driver and tty->low_latency!
253  * for example (low_latency: 1 / 0):
254  * serial.c:    uart-interrupt / softint
255  * usbserial:   urb-complete-interrupt / softint
256  */
257
258 static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
259                               char *fp, int count) 
260 {
261         struct sir_dev *dev;
262         struct sirtty_cb *priv = tty->disc_data;
263         int     i;
264
265         ASSERT(priv != NULL, return;);
266         ASSERT(priv->magic == IRTTY_MAGIC, return;);
267
268         if (unlikely(count==0))         /* yes, this happens */
269                 return;
270
271         dev = priv->dev;
272         if (!dev) {
273                 WARNING("%s(), not ready yet!\n", __FUNCTION__);
274                 return;
275         }
276
277         for (i = 0; i < count; i++) {
278                 /* 
279                  *  Characters received with a parity error, etc?
280                  */
281                 if (fp && *fp++) { 
282                         IRDA_DEBUG(0, "Framing or parity error!\n");
283                         sirdev_receive(dev, NULL, 0);   /* notify sir_dev (updating stats) */
284                         return;
285                 }
286         }
287
288         sirdev_receive(dev, cp, count);
289 }
290
291 /*
292  * Function irtty_receive_room (tty)
293  *
294  *    Used by the TTY to find out how much data we can receive at a time
295  * 
296 */
297 static int irtty_receive_room(struct tty_struct *tty) 
298 {
299         struct sirtty_cb *priv = tty->disc_data;
300
301         ASSERT(priv != NULL, return 0;);
302         ASSERT(priv->magic == IRTTY_MAGIC, return 0;);
303
304         return 65536;  /* We can handle an infinite amount of data. :-) */
305 }
306
307 /*
308  * Function irtty_write_wakeup (tty)
309  *
310  *    Called by the driver when there's room for more data.  If we have
311  *    more packets to send, we send them here.
312  *
313  */
314 static void irtty_write_wakeup(struct tty_struct *tty) 
315 {
316         struct sirtty_cb *priv = tty->disc_data;
317
318         ASSERT(priv != NULL, return;);
319         ASSERT(priv->magic == IRTTY_MAGIC, return;);
320
321         tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
322
323         if (priv->dev)
324                 sirdev_write_complete(priv->dev);
325 }
326
327 /* ------------------------------------------------------- */
328
329 /*
330  * Function irtty_stop_receiver (tty, stop)
331  *
332  */
333
334 static inline void irtty_stop_receiver(struct tty_struct *tty, int stop)
335 {
336         struct termios old_termios;
337         int cflag;
338
339         lock_kernel();
340         old_termios = *(tty->termios);
341         cflag = tty->termios->c_cflag;
342         
343         if (stop)
344                 cflag &= ~CREAD;
345         else
346                 cflag |= CREAD;
347
348         tty->termios->c_cflag = cflag;
349         if (tty->driver->set_termios)
350                 tty->driver->set_termios(tty, &old_termios);
351         unlock_kernel();
352 }
353
354 /*****************************************************************/
355
356 /* serialize ldisc open/close with sir_dev */
357 static DECLARE_MUTEX(irtty_sem);
358
359 /* notifier from sir_dev when irda% device gets opened (ifup) */
360
361 static int irtty_start_dev(struct sir_dev *dev)
362 {
363         struct sirtty_cb *priv;
364         struct tty_struct *tty;
365
366         /* serialize with ldisc open/close */
367         down(&irtty_sem);
368
369         priv = dev->priv;
370         if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
371                 up(&irtty_sem);
372                 return -ESTALE;
373         }
374
375         tty = priv->tty;
376
377         if (tty->driver->start)
378                 tty->driver->start(tty);
379         /* Make sure we can receive more data */
380         irtty_stop_receiver(tty, FALSE);
381
382         up(&irtty_sem);
383         return 0;
384 }
385
386 /* notifier from sir_dev when irda% device gets closed (ifdown) */
387
388 static int irtty_stop_dev(struct sir_dev *dev)
389 {
390         struct sirtty_cb *priv;
391         struct tty_struct *tty;
392
393         /* serialize with ldisc open/close */
394         down(&irtty_sem);
395
396         priv = dev->priv;
397         if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
398                 up(&irtty_sem);
399                 return -ESTALE;
400         }
401
402         tty = priv->tty;
403
404         /* Make sure we don't receive more data */
405         irtty_stop_receiver(tty, TRUE);
406         if (tty->driver->stop)
407                 tty->driver->stop(tty);
408
409         up(&irtty_sem);
410
411         return 0;
412 }
413
414 /* ------------------------------------------------------- */
415
416 struct sir_driver sir_tty_drv = {
417         .owner                  = THIS_MODULE,
418         .driver_name            = "sir_tty",
419         .start_dev              = irtty_start_dev,
420         .stop_dev               = irtty_stop_dev,
421         .do_write               = irtty_do_write,
422         .chars_in_buffer        = irtty_chars_in_buffer,
423         .wait_until_sent        = irtty_wait_until_sent,
424         .set_speed              = irtty_change_speed,
425         .set_dtr_rts            = irtty_set_dtr_rts,
426 };
427
428 /* ------------------------------------------------------- */
429
430 /*
431  * Function irtty_ioctl (tty, file, cmd, arg)
432  *
433  *     The Swiss army knife of system calls :-)
434  *
435  */
436 static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
437 {
438         struct irtty_info { char name[6]; } info;
439         struct sir_dev *dev;
440         struct sirtty_cb *priv = tty->disc_data;
441         int size = _IOC_SIZE(cmd);
442         int err = 0;
443
444         ASSERT(priv != NULL, return -ENODEV;);
445         ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;);
446
447         IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __FUNCTION__, cmd);
448
449         dev = priv->dev;
450         ASSERT(dev != NULL, return -1;);
451
452         if (_IOC_DIR(cmd) & _IOC_READ)
453                 err = verify_area(VERIFY_WRITE, (void *) arg, size);
454         else if (_IOC_DIR(cmd) & _IOC_WRITE)
455                 err = verify_area(VERIFY_READ, (void *) arg, size);
456         if (err)
457                 return err;
458         
459         switch (cmd) {
460         case TCGETS:
461         case TCGETA:
462                 err = n_tty_ioctl(tty, file, cmd, arg);
463                 break;
464
465         case IRTTY_IOCTDONGLE:
466                 /* this call blocks for completion */
467                 err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg);
468                 break;
469
470         case IRTTY_IOCGET:
471                 ASSERT(dev->netdev != NULL, return -1;);
472
473                 memset(&info, 0, sizeof(info)); 
474                 strncpy(info.name, dev->netdev->name, sizeof(info.name)-1);
475
476                 if (copy_to_user((void *)arg, &info, sizeof(info)))
477                         err = -EFAULT;
478                 break;
479         default:
480                 err = -ENOIOCTLCMD;
481                 break;
482         }
483         return err;
484 }
485
486
487 /* 
488  *  Function irtty_open(tty)
489  *
490  *    This function is called by the TTY module when the IrDA line
491  *    discipline is called for.  Because we are sure the tty line exists,
492  *    we only have to link it to a free IrDA channel.  
493  */
494 static int irtty_open(struct tty_struct *tty) 
495 {
496         struct sir_dev *dev;
497         struct sirtty_cb *priv;
498         int ret = 0;
499
500         /* Module stuff handled via irda_ldisc.owner - Jean II */
501
502         /* First make sure we're not already connected. */
503         if (tty->disc_data != NULL) {
504                 priv = tty->disc_data;
505                 if (priv && priv->magic == IRTTY_MAGIC) {
506                         ret = -EEXIST;
507                         goto out;
508                 }
509                 tty->disc_data = NULL;          /* ### */
510         }
511
512         /* stop the underlying  driver */
513         irtty_stop_receiver(tty, TRUE);
514         if (tty->driver->stop)
515                 tty->driver->stop(tty);
516
517         if (tty->driver->flush_buffer)
518                 tty->driver->flush_buffer(tty);
519         
520 /* from old irtty - but what is it good for?
521  * we _are_ the ldisc and we _don't_ implement flush_buffer!
522  *
523  *      if (tty->ldisc.flush_buffer)
524  *              tty->ldisc.flush_buffer(tty);
525  */
526
527         /* apply mtt override */
528         sir_tty_drv.qos_mtt_bits = qos_mtt_bits;
529
530         /* get a sir device instance for this driver */
531         dev = sirdev_get_instance(&sir_tty_drv, tty->name);
532         if (!dev) {
533                 ret = -ENODEV;
534                 goto out;
535         }
536
537         /* allocate private device info block */
538         priv = kmalloc(sizeof(*priv), GFP_KERNEL);
539         if (!priv)
540                 goto out_put;
541         memset(priv, 0, sizeof(*priv));
542
543         priv->magic = IRTTY_MAGIC;
544         priv->tty = tty;
545         priv->dev = dev;
546
547         /* serialize with start_dev - in case we were racing with ifup */
548         down(&irtty_sem);
549
550         dev->priv = priv;
551         tty->disc_data = priv;
552
553         up(&irtty_sem);
554
555         IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __FUNCTION__, tty->name);
556
557         return 0;
558
559 out_put:
560         sirdev_put_instance(dev);
561 out:
562         return ret;
563 }
564
565 /* 
566  *  Function irtty_close (tty)
567  *
568  *    Close down a IrDA channel. This means flushing out any pending queues,
569  *    and then restoring the TTY line discipline to what it was before it got
570  *    hooked to IrDA (which usually is TTY again).  
571  */
572 static void irtty_close(struct tty_struct *tty) 
573 {
574         struct sirtty_cb *priv = tty->disc_data;
575
576         ASSERT(priv != NULL, return;);
577         ASSERT(priv->magic == IRTTY_MAGIC, return;);
578
579         /* Hm, with a dongle attached the dongle driver wants
580          * to close the dongle - which requires the use of
581          * some tty write and/or termios or ioctl operations.
582          * Are we allowed to call those when already requested
583          * to shutdown the ldisc?
584          * If not, we should somehow mark the dev being staled.
585          * Question remains, how to close the dongle in this case...
586          * For now let's assume we are granted to issue tty driver calls
587          * until we return here from the ldisc close. I'm just wondering
588          * how this behaves with hotpluggable serial hardware like
589          * rs232-pcmcia card or usb-serial...
590          *
591          * priv->tty = NULL?;
592          */
593
594         /* we are dead now */
595         tty->disc_data = 0;
596
597         sirdev_put_instance(priv->dev);
598
599         /* Stop tty */
600         irtty_stop_receiver(tty, TRUE);
601         tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
602         if (tty->driver->stop)
603                 tty->driver->stop(tty);
604
605         kfree(priv);
606
607         IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __FUNCTION__, tty->name);
608 }
609
610 /* ------------------------------------------------------- */
611
612 static struct tty_ldisc irda_ldisc = {
613         .magic          = TTY_LDISC_MAGIC,
614         .name           = "irda",
615         .flags          = 0,
616         .open           = irtty_open,
617         .close          = irtty_close,
618         .read           = NULL,
619         .write          = NULL,
620         .ioctl          = irtty_ioctl,
621         .poll           = NULL,
622         .receive_buf    = irtty_receive_buf,
623         .receive_room   = irtty_receive_room,
624         .write_wakeup   = irtty_write_wakeup,
625         .owner          = THIS_MODULE,
626 };
627
628 /* ------------------------------------------------------- */
629
630 static int __init irtty_sir_init(void)
631 {
632         int err;
633
634         if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0)
635                 ERROR("IrDA: can't register line discipline (err = %d)\n",
636                         err);
637         return err;
638 }
639
640 static void __exit irtty_sir_cleanup(void) 
641 {
642         int err;
643
644         if ((err = tty_register_ldisc(N_IRDA, NULL))) {
645                 ERROR("%s(), can't unregister line discipline (err = %d)\n",
646                       __FUNCTION__, err);
647         }
648 }
649
650 module_init(irtty_sir_init);
651 module_exit(irtty_sir_cleanup);
652
653 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
654 MODULE_DESCRIPTION("IrDA TTY device driver");
655 MODULE_ALIAS_LDISC(N_IRDA);
656 MODULE_LICENSE("GPL");
657