irda update 5/7:
[opensuse:kernel.git] / drivers / net / irda / irtty.c
1 /*********************************************************************
2  *                
3  * Filename:      irtty.c
4  * Version:       1.1
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:   Sat Mar 11 07:43:30 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
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, All Rights Reserved.
15  *      
16  *     This program is free software; you can redistribute it and/or 
17  *     modify it under the terms of the GNU General Public License as 
18  *     published by the Free Software Foundation; either version 2 of 
19  *     the License, or (at your option) any later version.
20  *  
21  *     Neither Dag Brattli nor University of Tromsø admit liability nor
22  *     provide warranty for any of this software. This material is 
23  *     provided "AS-IS" and at no charge.
24  *     
25  ********************************************************************/    
26
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/tty.h>
30 #include <linux/init.h>
31 #include <linux/skbuff.h>
32 #include <linux/if_arp.h>
33 #include <linux/rtnetlink.h>
34
35 #include <asm/uaccess.h>
36
37 #include <net/irda/irda.h>
38 #include <net/irda/irtty.h>
39 #include <net/irda/wrapper.h>
40 #include <net/irda/timer.h>
41 #include <net/irda/irda_device.h>
42
43 static hashbin_t *irtty = NULL;
44 static struct tty_ldisc irda_ldisc;
45
46 static int qos_mtt_bits = 0x03;      /* 5 ms or more */
47
48 /* Network device fuction prototypes */
49 static int  irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev);
50 static int  irtty_net_init(struct net_device *dev);
51 static int  irtty_net_open(struct net_device *dev);
52 static int  irtty_net_close(struct net_device *dev);
53 static int  irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
54 static struct net_device_stats *irtty_net_get_stats(struct net_device *dev);
55
56 /* Line discipline function prototypes */
57 static int  irtty_open(struct tty_struct *tty);
58 static void irtty_close(struct tty_struct *tty);
59 static int  irtty_ioctl(struct tty_struct *, void *, int, void *);
60 static int  irtty_receive_room(struct tty_struct *tty);
61 static void irtty_write_wakeup(struct tty_struct *tty);
62 static void irtty_receive_buf(struct tty_struct *, const unsigned char *, 
63                               char *, int);
64
65 /* IrDA specific function protoctypes */
66 static int  irtty_is_receiving(struct irtty_cb *self);
67 static int  irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts);
68 static int  irtty_raw_write(struct net_device *dev, __u8 *buf, int len);
69 static int  irtty_raw_read(struct net_device *dev, __u8 *buf, int len);
70 static int  irtty_set_mode(struct net_device *dev, int mode);
71 static int  irtty_change_speed(struct irda_task *task);
72
73 char *driver_name = "irtty";
74
75 int __init irtty_init(void)
76 {
77         int status;
78         
79         irtty = hashbin_new( HB_LOCAL);
80         if ( irtty == NULL) {
81                 printk( KERN_WARNING "IrDA: Can't allocate irtty hashbin!\n");
82                 return -ENOMEM;
83         }
84
85         /* Fill in our line protocol discipline, and register it */
86         memset(&irda_ldisc, 0, sizeof( irda_ldisc));
87
88         irda_ldisc.magic = TTY_LDISC_MAGIC;
89         irda_ldisc.name  = "irda";
90         irda_ldisc.flags = 0;
91         irda_ldisc.open  = irtty_open;
92         irda_ldisc.close = irtty_close;
93         irda_ldisc.read  = NULL;
94         irda_ldisc.write = NULL;
95         irda_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
96                                     unsigned int, unsigned long)) irtty_ioctl;
97         irda_ldisc.poll  = NULL;
98         irda_ldisc.receive_buf  = irtty_receive_buf;
99         irda_ldisc.receive_room = irtty_receive_room;
100         irda_ldisc.write_wakeup = irtty_write_wakeup;
101         
102         if ((status = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0) {
103                 ERROR("IrDA: can't register line discipline (err = %d)\n", 
104                       status);
105         }
106         
107         return status;
108 }
109
110 /* 
111  *  Function irtty_cleanup ( )
112  *
113  *    Called when the irda module is removed. Here we remove all instances
114  *    of the driver, and the master array.
115  */
116 #ifdef MODULE
117 static void irtty_cleanup(void) 
118 {
119         int ret;
120         
121         /* Unregister tty line-discipline */
122         if ((ret = tty_register_ldisc(N_IRDA, NULL))) {
123                 ERROR(__FUNCTION__ 
124                       "(), can't unregister line discipline (err = %d)\n",
125                       ret);
126         }
127
128         /*
129          *  The TTY should care of deallocating the instances by using the
130          *  callback to irtty_close(), therefore we do give any deallocation
131          *  function to hashbin_destroy().
132          */
133         hashbin_delete(irtty, NULL);
134 }
135 #endif /* MODULE */
136
137 /* 
138  *  Function irtty_open(tty)
139  *
140  *    This function is called by the TTY module when the IrDA line
141  *    discipline is called for.  Because we are sure the tty line exists,
142  *    we only have to link it to a free IrDA channel.  
143  */
144 static int irtty_open(struct tty_struct *tty) 
145 {
146         struct net_device *dev;
147         struct irtty_cb *self;
148         char name[16];
149         int err;
150         
151         ASSERT(tty != NULL, return -EEXIST;);
152
153         /* First make sure we're not already connected. */
154         self = (struct irtty_cb *) tty->disc_data;
155
156         if (self != NULL && self->magic == IRTTY_MAGIC)
157                 return -EEXIST;
158         
159         /*
160          *  Allocate new instance of the driver
161          */
162         self = kmalloc(sizeof(struct irtty_cb), GFP_KERNEL);
163         if (self == NULL) {
164                 printk(KERN_ERR "IrDA: Can't allocate memory for "
165                        "IrDA control block!\n");
166                 return -ENOMEM;
167         }
168         memset(self, 0, sizeof(struct irtty_cb));
169         
170         self->tty = tty;
171         tty->disc_data = self;
172
173         /* Give self a name */
174         sprintf(name, "%s%d", tty->driver.name,
175                 minor(tty->device) - tty->driver.minor_start +
176                 tty->driver.name_base);
177
178         hashbin_insert(irtty, (irda_queue_t *) self, (int) self, NULL);
179
180         if (tty->driver.flush_buffer)
181                 tty->driver.flush_buffer(tty);
182         
183         if (tty->ldisc.flush_buffer)
184                 tty->ldisc.flush_buffer(tty);
185         
186         self->magic = IRTTY_MAGIC;
187         self->mode = IRDA_IRLAP;
188
189         /* 
190          *  Initialize QoS capabilities, we fill in all the stuff that
191          *  we support. Be careful not to place any restrictions on values
192          *  that are not device dependent (such as link disconnect time) so
193          *  this parameter can be set by IrLAP (or the user) instead. DB
194          */
195         irda_init_max_qos_capabilies(&self->qos);
196
197         /* The only value we must override it the baudrate */
198         self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
199                 IR_115200;
200         self->qos.min_turn_time.bits = qos_mtt_bits;
201         self->flags = IFF_SIR | IFF_PIO;
202         irda_qos_bits_to_value(&self->qos);
203
204         /* Specify how much memory we want */
205         self->rx_buff.truesize = 4000; 
206         self->tx_buff.truesize = 4000;
207
208         /* Allocate memory if needed */
209         if (self->rx_buff.truesize > 0) {
210                 self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
211                                                       GFP_KERNEL);
212                 if (self->rx_buff.head == NULL)
213                         return -ENOMEM;
214                 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
215         }
216         if (self->tx_buff.truesize > 0) {
217                 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize, 
218                                                       GFP_KERNEL);
219                 if (self->tx_buff.head == NULL) {
220                         kfree(self->rx_buff.head);
221                         return -ENOMEM;
222                 }
223                 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
224         }
225         
226         self->rx_buff.in_frame = FALSE;
227         self->rx_buff.state = OUTSIDE_FRAME;
228         self->tx_buff.data = self->tx_buff.head;
229         self->rx_buff.data = self->rx_buff.head;
230         
231         if (!(dev = dev_alloc("irda%d", &err))) {
232                 ERROR(__FUNCTION__ "(), dev_alloc() failed!\n");
233                 return -ENOMEM;
234         }
235
236         dev->priv = (void *) self;
237         self->netdev = dev;
238
239         /* Override the network functions we need to use */
240         dev->init            = irtty_net_init;
241         dev->hard_start_xmit = irtty_hard_xmit;
242         dev->open            = irtty_net_open;
243         dev->stop            = irtty_net_close;
244         dev->get_stats       = irtty_net_get_stats;
245         dev->do_ioctl        = irtty_net_ioctl;
246
247         rtnl_lock();
248         err = register_netdevice(dev);
249         rtnl_unlock();
250         if (err) {
251                 ERROR(__FUNCTION__ "(), register_netdev() failed!\n");
252                 return -1;
253         }
254
255         MESSAGE("IrDA: Registered device %s\n", dev->name);
256
257         MOD_INC_USE_COUNT;
258
259         return 0;
260 }
261
262 /* 
263  *  Function irtty_close (tty)
264  *
265  *    Close down a IrDA channel. This means flushing out any pending queues,
266  *    and then restoring the TTY line discipline to what it was before it got
267  *    hooked to IrDA (which usually is TTY again).  
268  */
269 static void irtty_close(struct tty_struct *tty) 
270 {
271         struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
272         
273         /* First make sure we're connected. */
274         ASSERT(self != NULL, return;);
275         ASSERT(self->magic == IRTTY_MAGIC, return;);
276         
277         /* Stop tty */
278         tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
279         tty->disc_data = 0;
280         
281         /* We are not using any dongle anymore! */
282         if (self->dongle)
283                 irda_device_dongle_cleanup(self->dongle);
284         self->dongle = NULL;
285
286         /* Remove netdevice */
287         if (self->netdev) {
288                 rtnl_lock();
289                 unregister_netdevice(self->netdev);
290                 rtnl_unlock();
291         }
292         
293         /* Remove speed changing task if any */
294         if (self->task)
295                 irda_task_delete(self->task);
296
297         self->tty = NULL;
298         self->magic = 0;
299         
300         self = hashbin_remove(irtty, (int) self, NULL);
301
302         if (self->tx_buff.head)
303                 kfree(self->tx_buff.head);
304         
305         if (self->rx_buff.head)
306                 kfree(self->rx_buff.head);
307         
308         kfree(self);
309         
310         MOD_DEC_USE_COUNT;
311 }
312
313 /*
314  * Function irtty_stop_receiver (self, stop)
315  *
316  *    
317  *
318  */
319 static void irtty_stop_receiver(struct irtty_cb *self, int stop)
320 {
321         struct termios old_termios;
322         int cflag;
323
324         old_termios = *(self->tty->termios);
325         cflag = self->tty->termios->c_cflag;
326         
327         if (stop)
328                 cflag &= ~CREAD;
329         else
330                 cflag |= CREAD;
331
332         self->tty->termios->c_cflag = cflag;
333         self->tty->driver.set_termios(self->tty, &old_termios);
334 }
335
336 /* 
337  *  Function irtty_do_change_speed (self, speed)
338  *
339  *    Change the speed of the serial port.
340  */
341 static void __irtty_change_speed(struct irtty_cb *self, __u32 speed)
342 {
343         struct termios old_termios;
344         int cflag;
345
346         ASSERT(self != NULL, return;);
347         ASSERT(self->magic == IRTTY_MAGIC, return;);
348
349         old_termios = *(self->tty->termios);
350         cflag = self->tty->termios->c_cflag;
351
352         cflag &= ~CBAUD;
353
354         IRDA_DEBUG(2, __FUNCTION__ "(), Setting speed to %d\n", speed);
355
356         switch (speed) {
357         case 1200:
358                 cflag |= B1200;
359                 break;
360         case 2400:
361                 cflag |= B2400;
362                 break;
363         case 4800:
364                 cflag |= B4800;
365                 break;
366         case 19200:
367                 cflag |= B19200;
368                 break;
369         case 38400:
370                 cflag |= B38400;
371                 break;
372         case 57600:
373                 cflag |= B57600;
374                 break;
375         case 115200:
376                 cflag |= B115200;
377                 break;
378         case 9600:
379         default:
380                 cflag |= B9600;
381                 break;
382         }       
383
384         self->tty->termios->c_cflag = cflag;
385         self->tty->driver.set_termios(self->tty, &old_termios);
386
387         self->io.speed = speed;
388 }
389
390 /*
391  * Function irtty_change_speed (instance, state, param)
392  *
393  *    State machine for changing speed of the device. We do it this way since
394  *    we cannot use schedule_timeout() when we are in interrupt context
395  */
396 static int irtty_change_speed(struct irda_task *task)
397 {
398         struct irtty_cb *self;
399         __u32 speed = (__u32) task->param;
400         int ret = 0;
401
402         IRDA_DEBUG(2, __FUNCTION__ "(), <%ld>\n", jiffies); 
403
404         self = (struct irtty_cb *) task->instance;
405         ASSERT(self != NULL, return -1;);
406
407         /* Check if busy */
408         if (self->task && self->task != task) {
409                 IRDA_DEBUG(0, __FUNCTION__ "(), busy!\n");
410                 return MSECS_TO_JIFFIES(10);
411         } else
412                 self->task = task;
413
414         switch (task->state) {
415         case IRDA_TASK_INIT:
416                 /* 
417                  * Make sure all data is sent before changing the speed of the
418                  * serial port.
419                  */
420                 if (self->tty->driver.chars_in_buffer(self->tty)) {
421                         /* Keep state, and try again later */
422                         ret = MSECS_TO_JIFFIES(10);
423                         break;
424                 } else {
425                         /* Transmit buffer is now empty, but it may still
426                          * take over 13 ms for the FIFO to become empty, so
427                          * wait some more to be sure all data is sent
428                          */
429                         irda_task_next_state(task, IRDA_TASK_WAIT);
430                         ret = MSECS_TO_JIFFIES(13);
431                 }
432         case IRDA_TASK_WAIT:
433                 if (self->dongle)
434                         irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
435                 else
436                         irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
437                 break;
438         case IRDA_TASK_CHILD_INIT:
439                 /* Go to default speed */
440                 __irtty_change_speed(self, 9600);
441
442                 /* Change speed of dongle */
443                 if (irda_task_execute(self->dongle,
444                                       self->dongle->issue->change_speed, 
445                                       NULL, task, (void *) speed))
446                 {
447                         /* Dongle need more time to change its speed */
448                         irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
449
450                         /* Give dongle 1 sec to finish */
451                         ret = MSECS_TO_JIFFIES(1000);
452                 } else
453                         /* Child finished immediately */
454                         irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
455                 break;
456         case IRDA_TASK_CHILD_WAIT:
457                 WARNING(__FUNCTION__ 
458                         "(), changing speed of dongle timed out!\n");
459                 ret = -1;               
460                 break;
461         case IRDA_TASK_CHILD_DONE:
462                 /* Finally we are ready to change the speed */
463                 __irtty_change_speed(self, speed);
464                 
465                 irda_task_next_state(task, IRDA_TASK_DONE);
466                 self->task = NULL;
467                 break;
468         default:
469                 ERROR(__FUNCTION__ "(), unknown state %d\n", task->state);
470                 irda_task_next_state(task, IRDA_TASK_DONE);
471                 self->task = NULL;
472                 ret = -1;
473                 break;
474         }       
475         return ret;
476 }
477
478 /*
479  * Function irtty_ioctl (tty, file, cmd, arg)
480  *
481  *     The Swiss army knife of system calls :-)
482  *
483  */
484 static int irtty_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
485 {
486         dongle_t *dongle;
487         struct irtty_info info;
488         struct irtty_cb *self;
489         int size = _IOC_SIZE(cmd);
490         int err = 0;
491
492         self = (struct irtty_cb *) tty->disc_data;
493
494         ASSERT(self != NULL, return -ENODEV;);
495         ASSERT(self->magic == IRTTY_MAGIC, return -EBADR;);
496
497         if (_IOC_DIR(cmd) & _IOC_READ)
498                 err = verify_area(VERIFY_WRITE, (void *) arg, size);
499         else if (_IOC_DIR(cmd) & _IOC_WRITE)
500                 err = verify_area(VERIFY_READ, (void *) arg, size);
501         if (err)
502                 return err;
503         
504         switch (cmd) {
505         case TCGETS:
506         case TCGETA:
507                 return n_tty_ioctl(tty, (struct file *) file, cmd, 
508                                    (unsigned long) arg);
509                 break;
510         case IRTTY_IOCTDONGLE:
511                 /* Initialize dongle */
512                 dongle = irda_device_dongle_init(self->netdev, (int) arg);
513                 if (!dongle)
514                         break;
515                 
516                 /* Initialize callbacks */
517                 dongle->set_mode    = irtty_set_mode;
518                 dongle->read        = irtty_raw_read;
519                 dongle->write       = irtty_raw_write;
520                 dongle->set_dtr_rts = irtty_set_dtr_rts;
521                 
522                 /* Bind dongle */
523                 self->dongle = dongle;
524                 
525                 /* Now initialize the dongle!  */
526                 dongle->issue->open(dongle, &self->qos);
527                 
528                 /* Reset dongle */
529                 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
530                                   NULL);                
531                 break;
532         case IRTTY_IOCGET:
533                 ASSERT(self->netdev != NULL, return -1;);
534
535                 memset(&info, 0, sizeof(struct irtty_info)); 
536                 strncpy(info.name, self->netdev->name, 5);
537
538                 if (copy_to_user(arg, &info, sizeof(struct irtty_info)))
539                         return -EFAULT;
540                 break;
541         default:
542                 return -ENOIOCTLCMD;
543         }
544         return 0;
545 }
546
547 /* 
548  *  Function irtty_receive_buf( tty, cp, count)
549  *
550  *    Handle the 'receiver data ready' interrupt.  This function is called
551  *    by the 'tty_io' module in the kernel when a block of IrDA data has
552  *    been received, which can now be decapsulated and delivered for
553  *    further processing 
554  */
555 static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
556                               char *fp, int count) 
557 {
558         struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
559
560         if (!self || !self->netdev) {
561                 IRDA_DEBUG(0, __FUNCTION__ "(), not ready yet!\n");
562                 return;
563         }
564
565         /* Read the characters out of the buffer */
566         while (count--) {
567                 /* 
568                  *  Characters received with a parity error, etc?
569                  */
570                 if (fp && *fp++) { 
571                         IRDA_DEBUG(0, "Framing or parity error!\n");
572                         irda_device_set_media_busy(self->netdev, TRUE);
573                         
574                         cp++;
575                         continue;
576                 }
577                 
578                 switch (self->mode) {
579                 case IRDA_IRLAP:
580                         /* Unwrap and destuff one byte */
581                         async_unwrap_char(self->netdev, &self->stats, 
582                                           &self->rx_buff, *cp++);
583                         break;
584                 case IRDA_RAW:
585                         /* What should we do when the buffer is full? */
586                         if (self->rx_buff.len == self->rx_buff.truesize)
587                                 self->rx_buff.len = 0;
588                         
589                         self->rx_buff.data[self->rx_buff.len++] = *cp++;
590                         break;
591                 default:
592                         break;
593                 }
594         }
595 }
596
597 /*
598  * Function irtty_change_speed_complete (task)
599  *
600  *    Called when the change speed operation completes
601  *
602  */
603 static int irtty_change_speed_complete(struct irda_task *task)
604 {
605         struct irtty_cb *self;
606
607         IRDA_DEBUG(2, __FUNCTION__ "()\n");
608
609         self = (struct irtty_cb *) task->instance;
610
611         ASSERT(self != NULL, return -1;);
612         ASSERT(self->netdev != NULL, return -1;);
613
614         /* Finished changing speed, so we are not busy any longer */
615         /* Signal network layer so it can try to send the frame */
616         netif_wake_queue(self->netdev);
617         
618         return 0;
619 }
620
621 /*
622  * Function irtty_hard_xmit (skb, dev)
623  *
624  *    Transmit frame
625  *
626  */
627 static int irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev)
628 {
629         struct irtty_cb *self;
630         int actual = 0;
631         __s32 speed;
632
633         self = (struct irtty_cb *) dev->priv;
634         ASSERT(self != NULL, return 0;);
635
636         /* Lock transmit buffer */
637         netif_stop_queue(dev);
638         
639         /* Check if we need to change the speed */
640         speed = irda_get_next_speed(skb);
641         if ((speed != self->io.speed) && (speed != -1)) {
642                 /* Check for empty frame */
643                 if (!skb->len) {
644                         irda_task_execute(self, irtty_change_speed, 
645                                           irtty_change_speed_complete, 
646                                           NULL, (void *) speed);
647                         dev_kfree_skb(skb);
648                         return 0;
649                 } else
650                         self->new_speed = speed;
651         }
652
653         /* Init tx buffer*/
654         self->tx_buff.data = self->tx_buff.head;
655         
656         /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
657         self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
658                                            self->tx_buff.truesize); 
659
660         self->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
661
662         dev->trans_start = jiffies;
663         self->stats.tx_bytes += self->tx_buff.len;
664
665         if (self->tty->driver.write)
666                 actual = self->tty->driver.write(self->tty, 0, 
667                                                  self->tx_buff.data, 
668                                                  self->tx_buff.len);
669         /* Hide the part we just transmitted */
670         self->tx_buff.data += actual;
671         self->tx_buff.len -= actual;
672
673         dev_kfree_skb(skb);
674
675         return 0;
676 }
677
678 /*
679  * Function irtty_receive_room (tty)
680  *
681  *    Used by the TTY to find out how much data we can receive at a time
682  * 
683 */
684 static int irtty_receive_room(struct tty_struct *tty) 
685 {
686         IRDA_DEBUG(0, __FUNCTION__ "()\n");
687         return 65536;  /* We can handle an infinite amount of data. :-) */
688 }
689
690 /*
691  * Function irtty_write_wakeup (tty)
692  *
693  *    Called by the driver when there's room for more data.  If we have
694  *    more packets to send, we send them here.
695  *
696  */
697 static void irtty_write_wakeup(struct tty_struct *tty) 
698 {
699         struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
700         int actual = 0;
701         
702         /* 
703          *  First make sure we're connected. 
704          */
705         ASSERT(self != NULL, return;);
706         ASSERT(self->magic == IRTTY_MAGIC, return;);
707
708         /* Finished with frame?  */
709         if (self->tx_buff.len > 0)  {
710                 /* Write data left in transmit buffer */
711                 actual = tty->driver.write(tty, 0, self->tx_buff.data, 
712                                            self->tx_buff.len);
713
714                 self->tx_buff.data += actual;
715                 self->tx_buff.len  -= actual;
716         } else {                
717                 /* 
718                  *  Now serial buffer is almost free & we can start 
719                  *  transmission of another packet 
720                  */
721                 IRDA_DEBUG(5, __FUNCTION__ "(), finished with frame!\n");
722                 
723                 self->stats.tx_packets++;                     
724
725                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
726
727                 if (self->new_speed) {
728                         IRDA_DEBUG(5, __FUNCTION__ "(), Changing speed!\n");
729                         irda_task_execute(self, irtty_change_speed, 
730                                           irtty_change_speed_complete, 
731                                           NULL, (void *) self->new_speed);
732                         self->new_speed = 0;
733                 } else {
734                         /* Tell network layer that we want more frames */
735                         netif_wake_queue(self->netdev);
736                 }
737         }
738 }
739
740 /*
741  * Function irtty_is_receiving (self)
742  *
743  *    Return TRUE is we are currently receiving a frame
744  *
745  */
746 static int irtty_is_receiving(struct irtty_cb *self)
747 {
748         return (self->rx_buff.state != OUTSIDE_FRAME);
749 }
750
751 /*
752  * Function irtty_set_dtr_rts (tty, dtr, rts)
753  *
754  *    This function can be used by dongles etc. to set or reset the status
755  *    of the dtr and rts lines
756  */
757 static int irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts)
758 {
759         struct irtty_cb *self;
760         struct tty_struct *tty;
761         mm_segment_t fs;
762         int arg = 0;
763
764         self = (struct irtty_cb *) dev->priv;
765         tty = self->tty;
766
767 #ifdef TIOCM_OUT2 /* Not defined for ARM */
768         arg = TIOCM_OUT2;
769 #endif
770         if (rts)
771                 arg |= TIOCM_RTS;
772         if (dtr)
773                 arg |= TIOCM_DTR;
774
775         /*
776          *  The ioctl() function, or actually set_modem_info() in serial.c
777          *  expects a pointer to the argument in user space. To hack us
778          *  around this, we use the set_fs() function to fool the routines 
779          *  that check if they are called from user space. We also need 
780          *  to send a pointer to the argument so get_user() gets happy. DB.
781          */
782
783         fs = get_fs();
784         set_fs(get_ds());
785         
786         if (tty->driver.ioctl(tty, NULL, TIOCMSET, (unsigned long) &arg)) { 
787                 IRDA_DEBUG(2, __FUNCTION__ "(), error doing ioctl!\n");
788         }
789         set_fs(fs);
790
791         return 0;
792 }
793
794 /*
795  * Function irtty_set_mode (self, status)
796  *
797  *    For the airport dongle, we need support for reading raw characters
798  *    from the IrDA device. This function switches between those modes. 
799  *    FALSE is the default mode, and will then treat incoming data as IrDA 
800  *    packets.
801  */
802 int irtty_set_mode(struct net_device *dev, int mode)
803 {
804         struct irtty_cb *self;
805
806         self = (struct irtty_cb *) dev->priv;
807
808         ASSERT(self != NULL, return -1;);
809
810         IRDA_DEBUG(2, __FUNCTION__ "(), mode=%s\n", infrared_mode[mode]);
811         
812         /* save status for driver */
813         self->mode = mode;
814         
815         /* reset the buffer state */
816         self->rx_buff.data = self->rx_buff.head;
817         self->rx_buff.len = 0;
818         self->rx_buff.state = OUTSIDE_FRAME;
819
820         return 0;
821 }
822
823 /*
824  * Function irtty_raw_read (self, buf, len)
825  *
826  *    Receive incoming data. This function sleeps, so it must only be
827  *    called with a process context. Timeout is currently defined to be
828  *    a multiple of 10 ms.
829  */
830 static int irtty_raw_read(struct net_device *dev, __u8 *buf, int len)
831 {
832         struct irtty_cb *self;
833         int count;
834
835         self = (struct irtty_cb *) dev->priv;
836
837         ASSERT(self != NULL, return 0;);
838         ASSERT(self->magic == IRTTY_MAGIC, return 0;);
839
840         return 0;
841 #if 0
842         buf = self->rx_buff.data;
843
844         /* Wait for the requested amount of data to arrive */
845         while (len < self->rx_buff.len) {
846                 current->state = TASK_INTERRUPTIBLE;
847                 schedule_timeout(MSECS_TO_JIFFIES(10));
848
849                 if (!timeout--)
850                         break;
851         }
852         
853         count = self->rx_buff.len < len ? self->rx_buff.len : len;
854
855         /* 
856          * Reset the state, this mean that a raw read is sort of a 
857          * datagram read, and _not_ a stream style read. Be aware of the
858          * difference. Implementing it the other way will just be painful ;-)
859          */
860         self->rx_buff.data = self->rx_buff.head;
861         self->rx_buff.len = 0;
862         self->rx_buff.state = OUTSIDE_FRAME;
863 #endif
864         /* Return the amount we were able to get */
865         return count;
866 }
867
868 static int irtty_raw_write(struct net_device *dev, __u8 *buf, int len)
869 {
870         struct irtty_cb *self;
871         int actual = 0;
872
873         self = (struct irtty_cb *) dev->priv;
874
875         ASSERT(self != NULL, return 0;);
876         ASSERT(self->magic == IRTTY_MAGIC, return 0;);
877
878         if (self->tty->driver.write)
879                 actual = self->tty->driver.write(self->tty, 0, buf, len);
880
881         return actual;
882 }
883
884 static int irtty_net_init(struct net_device *dev)
885 {
886         /* Set up to be a normal IrDA network device driver */
887         irda_device_setup(dev);
888
889         /* Insert overrides below this line! */
890
891         return 0;
892 }
893
894 static int irtty_net_open(struct net_device *dev)
895 {
896         struct irtty_cb *self = (struct irtty_cb *) dev->priv;
897         struct tty_struct *tty = self->tty;
898         char hwname[16];
899
900         ASSERT(self != NULL, return -1;);
901         ASSERT(self->magic == IRTTY_MAGIC, return -1;);
902
903         IRDA_DEBUG(0, __FUNCTION__ "()\n");
904         
905         /* Ready to play! */
906         netif_start_queue(dev);
907         
908         /* Make sure we can receive more data */
909         irtty_stop_receiver(self, FALSE);
910
911         /* Give self a hardware name */
912         sprintf(hwname, "%s%d", tty->driver.name,
913                 minor(tty->device) - tty->driver.minor_start +
914                 tty->driver.name_base);
915
916         /* 
917          * Open new IrLAP layer instance, now that everything should be
918          * initialized properly 
919          */
920         self->irlap = irlap_open(dev, &self->qos, hwname);
921
922         MOD_INC_USE_COUNT;
923
924         return 0;
925 }
926
927 static int irtty_net_close(struct net_device *dev)
928 {
929         struct irtty_cb *self = (struct irtty_cb *) dev->priv;
930
931         ASSERT(self != NULL, return -1;);
932         ASSERT(self->magic == IRTTY_MAGIC, return -1;);
933
934         /* Make sure we don't receive more data */
935         irtty_stop_receiver(self, TRUE);
936
937         /* Stop device */
938         netif_stop_queue(dev);
939         
940         /* Stop and remove instance of IrLAP */
941         if (self->irlap)
942                 irlap_close(self->irlap);
943         self->irlap = NULL;
944
945         MOD_DEC_USE_COUNT;
946
947         return 0;
948 }
949
950 /*
951  * Function irtty_net_ioctl (dev, rq, cmd)
952  *
953  *    Process IOCTL commands for this device
954  *
955  */
956 static int irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
957 {
958         struct if_irda_req *irq = (struct if_irda_req *) rq;
959         struct irtty_cb *self;
960         dongle_t *dongle;
961         unsigned long flags;
962         int ret = 0;
963
964         ASSERT(dev != NULL, return -1;);
965
966         self = dev->priv;
967
968         ASSERT(self != NULL, return -1;);
969         ASSERT(self->magic == IRTTY_MAGIC, return -1;);
970
971         IRDA_DEBUG(3, __FUNCTION__ "(), %s, (cmd=0x%X)\n", dev->name, cmd);
972         
973         /* Locking :
974          * irda_device_dongle_init() can't be locked.
975          * irda_task_execute() doesn't need to be locked (but
976          * irtty_change_speed() should protect itself).
977          * As this driver doesn't have spinlock protection, keep
978          * old fashion locking :-(
979          * Jean II
980          */
981         
982         switch (cmd) {
983         case SIOCSBANDWIDTH: /* Set bandwidth */
984                 if (!capable(CAP_NET_ADMIN))
985                         ret = -EPERM;
986                 else
987                         irda_task_execute(self, irtty_change_speed, NULL, NULL, 
988                                           (void *) irq->ifr_baudrate);
989                 break;
990         case SIOCSDONGLE: /* Set dongle */
991                 if (!capable(CAP_NET_ADMIN)) {
992                         ret = -EPERM;
993                         break;
994                 }
995
996                 /* Initialize dongle */
997                 dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
998                 if (!dongle)
999                         break;
1000                 
1001                 dongle->set_mode    = irtty_set_mode;
1002                 dongle->read        = irtty_raw_read;
1003                 dongle->write       = irtty_raw_write;
1004                 dongle->set_dtr_rts = irtty_set_dtr_rts;
1005                 
1006                 /* Now initialize the dongle!
1007                  * Safe to do unlocked : self->dongle is still NULL. */ 
1008                 dongle->issue->open(dongle, &self->qos);
1009                 
1010                 /* Reset dongle */
1011                 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
1012                                   NULL);        
1013
1014                 /* Make dongle available to driver only now to avoid
1015                  * race conditions - Jean II */
1016                 self->dongle = dongle;
1017                 break;
1018         case SIOCSMEDIABUSY: /* Set media busy */
1019                 if (!capable(CAP_NET_ADMIN))
1020                         ret = -EPERM;
1021                 else
1022                         irda_device_set_media_busy(self->netdev, TRUE);
1023                 break;
1024         case SIOCGRECEIVING: /* Check if we are receiving right now */
1025                 irq->ifr_receiving = irtty_is_receiving(self);
1026                 break;
1027         case SIOCSDTRRTS:
1028                 if (!capable(CAP_NET_ADMIN))
1029                         ret = -EPERM;
1030                 else {
1031                         save_flags(flags);
1032                         cli();
1033                         irtty_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
1034                         restore_flags(flags);
1035                 }
1036                 break;
1037         case SIOCSMODE:
1038                 if (!capable(CAP_NET_ADMIN))
1039                         ret = -EPERM;
1040                 else {
1041                         save_flags(flags);
1042                         cli();
1043                         irtty_set_mode(dev, irq->ifr_mode);
1044                         restore_flags(flags);
1045                 }
1046                 break;
1047         default:
1048                 ret = -EOPNOTSUPP;
1049         }
1050         
1051         return ret;
1052 }
1053
1054 static struct net_device_stats *irtty_net_get_stats(struct net_device *dev)
1055 {
1056         struct irtty_cb *self = (struct irtty_cb *) dev->priv;
1057
1058         return &self->stats;
1059 }
1060
1061 #ifdef MODULE
1062
1063 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1064 MODULE_DESCRIPTION("IrDA TTY device driver");
1065 MODULE_LICENSE("GPL");
1066
1067
1068 MODULE_PARM(qos_mtt_bits, "i");
1069 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1070
1071 /*
1072  * Function init_module (void)
1073  *
1074  *    Initialize IrTTY module
1075  *
1076  */
1077 int init_module(void)
1078 {
1079         return irtty_init();
1080 }
1081
1082 /*
1083  * Function cleanup_module (void)
1084  *
1085  *    Cleanup IrTTY module
1086  *
1087  */
1088 void cleanup_module(void)
1089 {
1090         irtty_cleanup();
1091 }
1092
1093 #endif /* MODULE */