patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / irda / irport.c
1 /*********************************************************************
2  * 
3  * Filename:      irport.c
4  * Version:       1.0
5  * Description:   Half duplex serial port SIR driver for IrDA. 
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sun Aug  3 13:49:59 1997
9  * Modified at:   Fri Jan 28 20:22:38 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * Sources:       serial.c by Linus Torvalds 
12  * 
13  *     Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved.
14  *     Copyright (c) 2000-2003 Jean Tourrilhes, 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  *     This program is distributed in the hope that it will be useful,
22  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  *     GNU General Public License for more details.
25  * 
26  *     You should have received a copy of the GNU General Public License 
27  *     along with this program; if not, write to the Free Software 
28  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
29  *     MA 02111-1307 USA
30  *
31  *     This driver is ment to be a small half duplex serial driver to be
32  *     used for IR-chipsets that has a UART (16550) compatibility mode. 
33  *     Eventually it will replace irtty, because of irtty has some 
34  *     problems that is hard to get around when we don't have control
35  *     over the serial driver. This driver may also be used by FIR 
36  *     drivers to handle SIR mode for them.
37  *
38  ********************************************************************/
39
40 #include <linux/module.h>
41
42 #include <linux/kernel.h>
43 #include <linux/types.h>
44 #include <linux/ioport.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/skbuff.h>
48 #include <linux/serial_reg.h>
49 #include <linux/errno.h>
50 #include <linux/init.h>
51 #include <linux/spinlock.h>
52 #include <linux/delay.h>
53 #include <linux/rtnetlink.h>
54
55 #include <asm/system.h>
56 #include <asm/bitops.h>
57 #include <asm/io.h>
58
59 #include <net/irda/irda.h>
60 #include <net/irda/wrapper.h>
61 #include "irport.h"
62
63 #define IO_EXTENT 8
64
65 /* 
66  * Currently you'll need to set these values using insmod like this:
67  * insmod irport io=0x3e8 irq=11
68  */
69 static unsigned int io[]  = { ~0, ~0, ~0, ~0 };
70 static unsigned int irq[] = { 0, 0, 0, 0 };
71
72 static unsigned int qos_mtt_bits = 0x03;
73
74 static struct irport_cb *dev_self[] = { NULL, NULL, NULL, NULL};
75 static char *driver_name = "irport";
76
77 static inline void irport_write_wakeup(struct irport_cb *self);
78 static inline int  irport_write(int iobase, int fifo_size, __u8 *buf, int len);
79 static inline void irport_receive(struct irport_cb *self);
80
81 static int  irport_net_ioctl(struct net_device *dev, struct ifreq *rq, 
82                              int cmd);
83 static inline int  irport_is_receiving(struct irport_cb *self);
84 static int  irport_set_dtr_rts(struct net_device *dev, int dtr, int rts);
85 static int  irport_raw_write(struct net_device *dev, __u8 *buf, int len);
86 static struct net_device_stats *irport_net_get_stats(struct net_device *dev);
87 static int irport_change_speed_complete(struct irda_task *task);
88 static void irport_timeout(struct net_device *dev);
89
90 EXPORT_SYMBOL(irport_open);
91 EXPORT_SYMBOL(irport_close);
92 EXPORT_SYMBOL(irport_start);
93 EXPORT_SYMBOL(irport_stop);
94 EXPORT_SYMBOL(irport_interrupt);
95 EXPORT_SYMBOL(irport_hard_xmit);
96 EXPORT_SYMBOL(irport_timeout);
97 EXPORT_SYMBOL(irport_change_speed);
98 EXPORT_SYMBOL(irport_net_open);
99 EXPORT_SYMBOL(irport_net_close);
100
101 static int __init irport_init(void)
102 {
103         int i;
104
105         for (i=0; (io[i] < 2000) && (i < 4); i++) {
106                 if (irport_open(i, io[i], irq[i]) != NULL)
107                         return 0;
108         }
109         /* 
110          * Maybe something failed, but we can still be usable for FIR drivers 
111          */
112         return 0;
113 }
114
115 /*
116  * Function irport_cleanup ()
117  *
118  *    Close all configured ports
119  *
120  */
121 static void __exit irport_cleanup(void)
122 {
123         int i;
124
125         IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
126
127         for (i=0; i < 4; i++) {
128                 if (dev_self[i])
129                         irport_close(dev_self[i]);
130         }
131 }
132
133 struct irport_cb *
134 irport_open(int i, unsigned int iobase, unsigned int irq)
135 {
136         struct net_device *dev;
137         struct irport_cb *self;
138
139         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
140
141         /* Lock the port that we need */
142         if (!request_region(iobase, IO_EXTENT, driver_name)) {
143                 IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
144                            __FUNCTION__, iobase);
145                 goto err_out1;
146         }
147
148         /*
149          *  Allocate new instance of the driver
150          */
151         dev = alloc_irdadev(sizeof(struct irport_cb));
152         if (!dev) {
153                 ERROR("%s(), can't allocate memory for "
154                       "irda device!\n", __FUNCTION__);
155                 goto err_out2;
156         }
157
158         self = dev->priv;
159         spin_lock_init(&self->lock);
160
161         /* Need to store self somewhere */
162         dev_self[i] = self;
163         self->priv = self;
164         self->index = i;
165
166         /* Initialize IO */
167         self->io.sir_base  = iobase;
168         self->io.sir_ext   = IO_EXTENT;
169         self->io.irq       = irq;
170         self->io.fifo_size = 16;                /* 16550A and compatible */
171
172         /* Initialize QoS for this device */
173         irda_init_max_qos_capabilies(&self->qos);
174         
175         self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
176                 IR_115200;
177
178         self->qos.min_turn_time.bits = qos_mtt_bits;
179         irda_qos_bits_to_value(&self->qos);
180         
181         /* Bootstrap ZeroCopy Rx */
182         self->rx_buff.truesize = IRDA_SKB_MAX_MTU;
183         self->rx_buff.skb = __dev_alloc_skb(self->rx_buff.truesize,
184                                             GFP_KERNEL);
185         if (self->rx_buff.skb == NULL) {
186                 ERROR("%s(), can't allocate memory for "
187                       "receive buffer!\n", __FUNCTION__);
188                 goto err_out3;
189         }
190         skb_reserve(self->rx_buff.skb, 1);
191         self->rx_buff.head = self->rx_buff.skb->data;
192         /* No need to memset the buffer, unless you are really pedantic */
193
194         /* Finish setup the Rx buffer descriptor */
195         self->rx_buff.in_frame = FALSE;
196         self->rx_buff.state = OUTSIDE_FRAME;
197         self->rx_buff.data = self->rx_buff.head;
198
199         /* Specify how much memory we want */
200         self->tx_buff.truesize = 4000;
201         
202         /* Allocate memory if needed */
203         if (self->tx_buff.truesize > 0) {
204                 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize, 
205                                                       GFP_KERNEL);
206                 if (self->tx_buff.head == NULL) {
207                         ERROR("%s(), can't allocate memory for "
208                               "transmit buffer!\n", __FUNCTION__);
209                         goto err_out4;
210                 }
211                 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
212         }       
213         self->tx_buff.data = self->tx_buff.head;
214
215         self->netdev = dev;
216         /* Keep track of module usage */
217         SET_MODULE_OWNER(dev);
218
219         /* May be overridden by piggyback drivers */
220         self->interrupt    = irport_interrupt;
221         self->change_speed = irport_change_speed;
222
223         /* Override the network functions we need to use */
224         dev->hard_start_xmit = irport_hard_xmit;
225         dev->tx_timeout      = irport_timeout;
226         dev->watchdog_timeo  = HZ;  /* Allow time enough for speed change */
227         dev->open            = irport_net_open;
228         dev->stop            = irport_net_close;
229         dev->get_stats       = irport_net_get_stats;
230         dev->do_ioctl        = irport_net_ioctl;
231
232         /* Make ifconfig display some details */
233         dev->base_addr = iobase;
234         dev->irq = irq;
235
236         if (register_netdev(dev)) {
237                 ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
238                 goto err_out5;
239         }
240         MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n",
241                 dev->name, iobase, irq);
242
243         return self;
244  err_out5:
245         kfree(self->tx_buff.head);
246  err_out4:
247         kfree_skb(self->rx_buff.skb);
248  err_out3:
249         free_netdev(dev);
250         dev_self[i] = NULL;
251  err_out2:
252         release_region(iobase, IO_EXTENT);
253  err_out1:
254         return NULL;
255 }
256
257 int irport_close(struct irport_cb *self)
258 {
259         ASSERT(self != NULL, return -1;);
260
261         /* We are not using any dongle anymore! */
262         if (self->dongle)
263                 irda_device_dongle_cleanup(self->dongle);
264         self->dongle = NULL;
265         
266         /* Remove netdevice */
267         unregister_netdev(self->netdev);
268
269         /* Release the IO-port that this driver is using */
270         IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n", 
271                    __FUNCTION__, self->io.sir_base);
272         release_region(self->io.sir_base, self->io.sir_ext);
273
274         if (self->tx_buff.head)
275                 kfree(self->tx_buff.head);
276         
277         if (self->rx_buff.skb)
278                 kfree_skb(self->rx_buff.skb);
279         self->rx_buff.skb = NULL;
280         
281         /* Remove ourselves */
282         dev_self[self->index] = NULL;
283         free_netdev(self->netdev);
284         
285         return 0;
286 }
287
288 void irport_start(struct irport_cb *self)
289 {
290         int iobase;
291
292         iobase = self->io.sir_base;
293
294         irport_stop(self);
295         
296         /* We can't lock, we may be called from a FIR driver - Jean II */
297
298         /* Initialize UART */
299         outb(UART_LCR_WLEN8, iobase+UART_LCR);  /* Reset DLAB */
300         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
301         
302         /* Turn on interrups */
303         outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
304 }
305
306 void irport_stop(struct irport_cb *self)
307 {
308         int iobase;
309
310         iobase = self->io.sir_base;
311
312         /* We can't lock, we may be called from a FIR driver - Jean II */
313
314         /* We are not transmitting any more */
315         self->transmitting = 0;
316
317         /* Reset UART */
318         outb(0, iobase+UART_MCR);
319         
320         /* Turn off interrupts */
321         outb(0, iobase+UART_IER);
322 }
323
324 /*
325  * Function irport_probe (void)
326  *
327  *    Start IO port 
328  *
329  */
330 int irport_probe(int iobase)
331 {
332         IRDA_DEBUG(4, "%s(), iobase=%#x\n", __FUNCTION__, iobase);
333
334         return 0;
335 }
336
337 /*
338  * Function irport_get_fcr (speed)
339  *
340  *    Compute value of fcr
341  *
342  */
343 static inline unsigned int irport_get_fcr(__u32 speed)
344 {
345         unsigned int fcr;    /* FIFO control reg */
346
347         /* Enable fifos */
348         fcr = UART_FCR_ENABLE_FIFO;
349
350         /* 
351          * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
352          * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
353          * about this timeout since it will always be fast enough. 
354          */
355         if (speed < 38400)
356                 fcr |= UART_FCR_TRIGGER_1;
357         else 
358                 //fcr |= UART_FCR_TRIGGER_14;
359                 fcr |= UART_FCR_TRIGGER_8;
360
361         return(fcr);
362 }
363  
364 /*
365  * Function irport_change_speed (self, speed)
366  *
367  *    Set speed of IrDA port to specified baudrate
368  *
369  * This function should be called with irq off and spin-lock.
370  */
371 void irport_change_speed(void *priv, __u32 speed)
372 {
373         struct irport_cb *self = (struct irport_cb *) priv;
374         int iobase; 
375         unsigned int fcr;    /* FIFO control reg */
376         unsigned int lcr;    /* Line control reg */
377         int divisor;
378
379         ASSERT(self != NULL, return;);
380         ASSERT(speed != 0, return;);
381
382         IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n",
383                     __FUNCTION__, speed, self->io.sir_base);
384
385         /* We can't lock, we may be called from a FIR driver - Jean II */
386
387         iobase = self->io.sir_base;
388         
389         /* Update accounting for new speed */
390         self->io.speed = speed;
391
392         /* Turn off interrupts */
393         outb(0, iobase+UART_IER); 
394
395         divisor = SPEED_MAX/speed;
396         
397         /* Get proper fifo configuration */
398         fcr = irport_get_fcr(speed);
399
400         /* IrDA ports use 8N1 */
401         lcr = UART_LCR_WLEN8;
402         
403         outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
404         outb(divisor & 0xff,      iobase+UART_DLL); /* Set speed */
405         outb(divisor >> 8,        iobase+UART_DLM);
406         outb(lcr,                 iobase+UART_LCR); /* Set 8N1  */
407         outb(fcr,                 iobase+UART_FCR); /* Enable FIFO's */
408
409         /* Turn on interrups */
410         /* This will generate a fatal interrupt storm.
411          * People calling us will do that properly - Jean II */
412         //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
413 }
414
415 /*
416  * Function __irport_change_speed (instance, state, param)
417  *
418  *    State machine for changing speed of the device. We do it this way since
419  *    we cannot use schedule_timeout() when we are in interrupt context
420  *
421  */
422 int __irport_change_speed(struct irda_task *task)
423 {
424         struct irport_cb *self;
425         __u32 speed = (__u32) task->param;
426         unsigned long flags = 0;
427         int wasunlocked = 0;
428         int ret = 0;
429
430         IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies); 
431
432         self = (struct irport_cb *) task->instance;
433
434         ASSERT(self != NULL, return -1;);
435
436         /* Locking notes : this function may be called from irq context with
437          * spinlock, via irport_write_wakeup(), or from non-interrupt without
438          * spinlock (from the task timer). Yuck !
439          * This is ugly, and unsafe is the spinlock is not already aquired.
440          * This will be fixed when irda-task get rewritten.
441          * Jean II */
442         if (!spin_is_locked(&self->lock)) {
443                 spin_lock_irqsave(&self->lock, flags);
444                 wasunlocked = 1;
445         }
446
447         switch (task->state) {
448         case IRDA_TASK_INIT:
449         case IRDA_TASK_WAIT:
450                 /* Are we ready to change speed yet? */
451                 if (self->tx_buff.len > 0) {
452                         task->state = IRDA_TASK_WAIT;
453
454                         /* Try again later */
455                         ret = msecs_to_jiffies(20);
456                         break;
457                 }
458
459                 if (self->dongle)
460                         irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
461                 else
462                         irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
463                 break;
464         case IRDA_TASK_CHILD_INIT:
465                 /* Go to default speed */
466                 self->change_speed(self->priv, 9600);
467
468                 /* Change speed of dongle */
469                 if (irda_task_execute(self->dongle,
470                                       self->dongle->issue->change_speed, 
471                                       NULL, task, (void *) speed))
472                 {
473                         /* Dongle need more time to change its speed */
474                         irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
475
476                         /* Give dongle 1 sec to finish */
477                         ret = msecs_to_jiffies(1000);
478                 } else
479                         /* Child finished immediately */
480                         irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
481                 break;
482         case IRDA_TASK_CHILD_WAIT:
483                 WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
484                 ret = -1;               
485                 break;
486         case IRDA_TASK_CHILD_DONE:
487                 /* Finally we are ready to change the speed */
488                 self->change_speed(self->priv, speed);
489                 
490                 irda_task_next_state(task, IRDA_TASK_DONE);
491                 break;
492         default:
493                 ERROR("%s(), unknown state %d\n", __FUNCTION__, task->state);
494                 irda_task_next_state(task, IRDA_TASK_DONE);
495                 ret = -1;
496                 break;
497         }
498         /* Put stuff in the state we found them - Jean II */
499         if(wasunlocked) {
500                 spin_unlock_irqrestore(&self->lock, flags);
501         }
502
503         return ret;
504 }
505
506 /*
507  * Function irport_change_speed_complete (task)
508  *
509  *    Called when the change speed operation completes
510  *
511  */
512 static int irport_change_speed_complete(struct irda_task *task)
513 {
514         struct irport_cb *self;
515
516         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
517
518         self = (struct irport_cb *) task->instance;
519
520         ASSERT(self != NULL, return -1;);
521         ASSERT(self->netdev != NULL, return -1;);
522
523         /* Finished changing speed, so we are not busy any longer */
524         /* Signal network layer so it can try to send the frame */
525
526         netif_wake_queue(self->netdev);
527         
528         return 0;
529 }
530
531 /*
532  * Function irport_timeout (struct net_device *dev)
533  *
534  *    The networking layer thinks we timed out.
535  *
536  */
537
538 static void irport_timeout(struct net_device *dev)
539 {
540         struct irport_cb *self;
541         int iobase;
542         int iir, lsr;
543         unsigned long flags;
544
545         self = (struct irport_cb *) dev->priv;
546         ASSERT(self != NULL, return;);
547         iobase = self->io.sir_base;
548         
549         WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n",
550                 dev->name, jiffies, dev->trans_start);
551         spin_lock_irqsave(&self->lock, flags);
552
553         /* Debug what's happening... */
554
555         /* Get interrupt status */
556         lsr = inb(iobase+UART_LSR);
557         /* Read interrupt register */
558         iir = inb(iobase+UART_IIR);
559         IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", 
560                    __FUNCTION__, iir, lsr, iobase);
561
562         IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%d\n", 
563                    __FUNCTION__, self->transmitting, self->tx_buff.len,
564                    self->tx_buff.data - self->tx_buff.head);
565
566         /* Now, restart the port */
567         irport_start(self);
568         self->change_speed(self->priv, self->io.speed);
569         /* This will re-enable irqs */
570         outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
571         dev->trans_start = jiffies;
572         spin_unlock_irqrestore(&self->lock, flags);
573
574         netif_wake_queue(dev);
575 }
576  
577 /*
578  * Function irport_wait_hw_transmitter_finish ()
579  *
580  *    Wait for the real end of HW transmission
581  *
582  * The UART is a strict FIFO, and we get called only when we have finished
583  * pushing data to the FIFO, so the maximum amount of time we must wait
584  * is only for the FIFO to drain out.
585  *
586  * We use a simple calibrated loop. We may need to adjust the loop
587  * delay (udelay) to balance I/O traffic and latency. And we also need to
588  * adjust the maximum timeout.
589  * It would probably be better to wait for the proper interrupt,
590  * but it doesn't seem to be available.
591  *
592  * We can't use jiffies or kernel timers because :
593  * 1) We are called from the interrupt handler, which disable softirqs,
594  * so jiffies won't be increased
595  * 2) Jiffies granularity is usually very coarse (10ms), and we don't
596  * want to wait that long to detect stuck hardware.
597  * Jean II
598  */
599
600 static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
601 {
602         int iobase;
603         int count = 1000;       /* 1 ms */
604         
605         iobase = self->io.sir_base;
606
607         /* Calibrated busy loop */
608         while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT))
609                 udelay(1);
610
611         if(count == 0)
612                 IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);
613 }
614
615 /*
616  * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
617  *
618  *    Transmits the current frame until FIFO is full, then
619  *    waits until the next transmitt interrupt, and continues until the
620  *    frame is transmitted.
621  */
622 int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
623 {
624         struct irport_cb *self;
625         unsigned long flags;
626         int iobase;
627         s32 speed;
628
629         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
630
631         ASSERT(dev != NULL, return 0;);
632         
633         self = (struct irport_cb *) dev->priv;
634         ASSERT(self != NULL, return 0;);
635
636         iobase = self->io.sir_base;
637
638         netif_stop_queue(dev);
639
640         /* Make sure tests & speed change are atomic */
641         spin_lock_irqsave(&self->lock, flags);
642
643         /* Check if we need to change the speed */
644         speed = irda_get_next_speed(skb);
645         if ((speed != self->io.speed) && (speed != -1)) {
646                 /* Check for empty frame */
647                 if (!skb->len) {
648                         /*
649                          * We send frames one by one in SIR mode (no
650                          * pipelining), so at this point, if we were sending
651                          * a previous frame, we just received the interrupt
652                          * telling us it is finished (UART_IIR_THRI).
653                          * Therefore, waiting for the transmitter to really
654                          * finish draining the fifo won't take too long.
655                          * And the interrupt handler is not expected to run.
656                          * - Jean II */
657                         irport_wait_hw_transmitter_finish(self);
658                         /* Better go there already locked - Jean II */
659                         irda_task_execute(self, __irport_change_speed, 
660                                           irport_change_speed_complete, 
661                                           NULL, (void *) speed);
662                         dev->trans_start = jiffies;
663                         spin_unlock_irqrestore(&self->lock, flags);
664                         dev_kfree_skb(skb);
665                         return 0;
666                 } else
667                         self->new_speed = speed;
668         }
669
670         /* Init tx buffer */
671         self->tx_buff.data = self->tx_buff.head;
672
673         /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
674         self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
675                                            self->tx_buff.truesize);
676         
677         self->stats.tx_bytes += self->tx_buff.len;
678
679         /* We are transmitting */
680         self->transmitting = 1;
681
682         /* Turn on transmit finished interrupt. Will fire immediately!  */
683         outb(UART_IER_THRI, iobase+UART_IER); 
684
685         dev->trans_start = jiffies;
686         spin_unlock_irqrestore(&self->lock, flags);
687
688         dev_kfree_skb(skb);
689         
690         return 0;
691 }
692         
693 /*
694  * Function irport_write (driver)
695  *
696  *    Fill Tx FIFO with transmit data
697  *
698  * Called only from irport_write_wakeup()
699  */
700 static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
701 {
702         int actual = 0;
703
704         /* Fill FIFO with current frame */
705         while ((actual < fifo_size) && (actual < len)) {
706                 /* Transmit next byte */
707                 outb(buf[actual], iobase+UART_TX);
708
709                 actual++;
710         }
711         
712         return actual;
713 }
714
715 /*
716  * Function irport_write_wakeup (tty)
717  *
718  *    Called by the driver when there's room for more data.  If we have
719  *    more packets to send, we send them here.
720  *
721  * Called only from irport_interrupt()
722  * Make sure this function is *not* called while we are receiving,
723  * otherwise we will reset fifo and loose data :-(
724  */
725 static inline void irport_write_wakeup(struct irport_cb *self)
726 {
727         int actual = 0;
728         int iobase;
729         unsigned int fcr;
730
731         ASSERT(self != NULL, return;);
732
733         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
734
735         iobase = self->io.sir_base;
736
737         /* Finished with frame?  */
738         if (self->tx_buff.len > 0)  {
739                 /* Write data left in transmit buffer */
740                 actual = irport_write(iobase, self->io.fifo_size, 
741                                       self->tx_buff.data, self->tx_buff.len);
742                 self->tx_buff.data += actual;
743                 self->tx_buff.len  -= actual;
744         } else {
745                 /* 
746                  *  Now serial buffer is almost free & we can start 
747                  *  transmission of another packet. But first we must check
748                  *  if we need to change the speed of the hardware
749                  */
750                 if (self->new_speed) {
751                         irport_wait_hw_transmitter_finish(self);
752                         irda_task_execute(self, __irport_change_speed, 
753                                           irport_change_speed_complete, 
754                                           NULL, (void *) self->new_speed);
755                         self->new_speed = 0;
756                 } else {
757                         /* Tell network layer that we want more frames */
758                         netif_wake_queue(self->netdev);
759                 }
760                 self->stats.tx_packets++;
761
762                 /* 
763                  * Reset Rx FIFO to make sure that all reflected transmit data
764                  * is discarded. This is needed for half duplex operation
765                  */
766                 fcr = irport_get_fcr(self->io.speed);
767                 fcr |= UART_FCR_CLEAR_RCVR;
768                 outb(fcr, iobase+UART_FCR);
769
770                 /* Finished transmitting */
771                 self->transmitting = 0;
772
773                 /* Turn on receive interrupts */
774                 outb(UART_IER_RDI, iobase+UART_IER);
775
776                 IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__);
777         }
778 }
779
780 /*
781  * Function irport_receive (self)
782  *
783  *    Receive one frame from the infrared port
784  *
785  * Called only from irport_interrupt()
786  */
787 static inline void irport_receive(struct irport_cb *self) 
788 {
789         int boguscount = 0;
790         int iobase;
791
792         ASSERT(self != NULL, return;);
793
794         iobase = self->io.sir_base;
795
796         /*  
797          * Receive all characters in Rx FIFO, unwrap and unstuff them. 
798          * async_unwrap_char will deliver all found frames  
799          */
800         do {
801                 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff, 
802                                   inb(iobase+UART_RX));
803
804                 /* Make sure we don't stay here too long */
805                 if (boguscount++ > 32) {
806                         IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
807                         break;
808                 }
809         } while (inb(iobase+UART_LSR) & UART_LSR_DR);   
810 }
811
812 /*
813  * Function irport_interrupt (irq, dev_id, regs)
814  *
815  *    Interrupt handler
816  */
817 irqreturn_t irport_interrupt(int irq, void *dev_id, struct pt_regs *regs) 
818 {
819         struct net_device *dev = (struct net_device *) dev_id;
820         struct irport_cb *self;
821         int boguscount = 0;
822         int iobase;
823         int iir, lsr;
824         int handled = 0;
825
826         if (!dev) {
827                 WARNING("%s() irq %d for unknown device.\n", __FUNCTION__, irq);
828                 return IRQ_NONE;
829         }
830         self = (struct irport_cb *) dev->priv;
831
832         spin_lock(&self->lock);
833
834         iobase = self->io.sir_base;
835
836         /* Cut'n'paste interrupt routine from serial.c
837          * This version try to minimise latency and I/O operations.
838          * Simplified and modified to enforce half duplex operation.
839          * - Jean II */
840
841         /* Check status even is iir reg is cleared, more robust and
842          * eliminate a read on the I/O bus - Jean II */
843         do {
844                 /* Get interrupt status ; Clear interrupt */
845                 lsr = inb(iobase+UART_LSR);
846                 
847                 /* Are we receiving or transmitting ? */
848                 if(!self->transmitting) {
849                         /* Received something ? */
850                         if (lsr & UART_LSR_DR)
851                                 irport_receive(self);
852                 } else {
853                         /* Room in Tx fifo ? */
854                         if (lsr & (UART_LSR_THRE | UART_LSR_TEMT))
855                                 irport_write_wakeup(self);
856                 }
857
858                 /* A bit hackish, but working as expected... Jean II */
859                 if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR))
860                         handled = 1;
861
862                 /* Make sure we don't stay here to long */
863                 if (boguscount++ > 10) {
864                         WARNING("%s() irq handler looping : lsr=%02x\n",
865                                 __FUNCTION__, lsr);
866                         break;
867                 }
868
869                 /* Read interrupt register */
870                 iir = inb(iobase+UART_IIR);
871
872                 /* Enable this debug only when no other options and at low
873                  * bit rates, otherwise it may cause Rx overruns (lsr=63).
874                  * - Jean II */
875                 IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", 
876                             __FUNCTION__, iir, lsr, iobase);
877
878                 /* As long as interrupt pending... */
879         } while ((iir & UART_IIR_NO_INT) == 0);
880
881         spin_unlock(&self->lock);
882         return IRQ_RETVAL(handled);
883 }
884
885 /*
886  * Function irport_net_open (dev)
887  *
888  *    Network device is taken up. Usually this is done by "ifconfig irda0 up" 
889  *   
890  */
891 int irport_net_open(struct net_device *dev)
892 {
893         struct irport_cb *self;
894         int iobase;
895         char hwname[16];
896         unsigned long flags;
897
898         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
899
900         ASSERT(dev != NULL, return -1;);
901         self = (struct irport_cb *) dev->priv;
902
903         iobase = self->io.sir_base;
904
905         if (request_irq(self->io.irq, self->interrupt, 0, dev->name, 
906                         (void *) dev)) {
907                 IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
908                            __FUNCTION__, self->io.irq);
909                 return -EAGAIN;
910         }
911
912         spin_lock_irqsave(&self->lock, flags);
913         /* Init uart */
914         irport_start(self);
915         /* Set 9600 bauds per default, including at the dongle */
916         irda_task_execute(self, __irport_change_speed, 
917                           irport_change_speed_complete, 
918                           NULL, (void *) 9600);
919         spin_unlock_irqrestore(&self->lock, flags);
920
921
922         /* Give self a hardware name */
923         sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
924
925         /* 
926          * Open new IrLAP layer instance, now that everything should be
927          * initialized properly 
928          */
929         self->irlap = irlap_open(dev, &self->qos, hwname);
930
931         /* Ready to play! */
932
933         netif_start_queue(dev);
934
935         return 0;
936 }
937
938 /*
939  * Function irport_net_close (self)
940  *
941  *    Network device is taken down. Usually this is done by 
942  *    "ifconfig irda0 down" 
943  */
944 int irport_net_close(struct net_device *dev)
945 {
946         struct irport_cb *self;
947         int iobase;
948         unsigned long flags;
949
950         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
951
952         ASSERT(dev != NULL, return -1;);
953         self = (struct irport_cb *) dev->priv;
954
955         ASSERT(self != NULL, return -1;);
956
957         iobase = self->io.sir_base;
958
959         /* Stop device */
960         netif_stop_queue(dev);
961         
962         /* Stop and remove instance of IrLAP */
963         if (self->irlap)
964                 irlap_close(self->irlap);
965         self->irlap = NULL;
966
967         spin_lock_irqsave(&self->lock, flags);
968         irport_stop(self);
969         spin_unlock_irqrestore(&self->lock, flags);
970
971         free_irq(self->io.irq, dev);
972
973         return 0;
974 }
975
976 /*
977  * Function irport_is_receiving (self)
978  *
979  *    Returns true is we are currently receiving data
980  *
981  */
982 static inline int irport_is_receiving(struct irport_cb *self)
983 {
984         return (self->rx_buff.state != OUTSIDE_FRAME);
985 }
986
987 /*
988  * Function irport_set_dtr_rts (tty, dtr, rts)
989  *
990  *    This function can be used by dongles etc. to set or reset the status
991  *    of the dtr and rts lines
992  */
993 static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
994 {
995         struct irport_cb *self = dev->priv;
996         int iobase;
997
998         ASSERT(self != NULL, return -1;);
999
1000         iobase = self->io.sir_base;
1001
1002         if (dtr)
1003                 dtr = UART_MCR_DTR;
1004         if (rts)
1005                 rts = UART_MCR_RTS;
1006
1007         outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
1008
1009         return 0;
1010 }
1011
1012 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
1013 {
1014         struct irport_cb *self = (struct irport_cb *) dev->priv;
1015         int actual = 0;
1016         int iobase;
1017
1018         ASSERT(self != NULL, return -1;);
1019
1020         iobase = self->io.sir_base;
1021
1022         /* Tx FIFO should be empty! */
1023         if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1024                 IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__);
1025                 return -1;
1026         }
1027         
1028         /* Fill FIFO with current frame */
1029         while (actual < len) {
1030                 /* Transmit next byte */
1031                 outb(buf[actual], iobase+UART_TX);
1032                 actual++;
1033         }
1034
1035         return actual;
1036 }
1037
1038 /*
1039  * Function irport_net_ioctl (dev, rq, cmd)
1040  *
1041  *    Process IOCTL commands for this device
1042  *
1043  */
1044 static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1045 {
1046         struct if_irda_req *irq = (struct if_irda_req *) rq;
1047         struct irport_cb *self;
1048         dongle_t *dongle;
1049         unsigned long flags;
1050         int ret = 0;
1051
1052         ASSERT(dev != NULL, return -1;);
1053
1054         self = dev->priv;
1055
1056         ASSERT(self != NULL, return -1;);
1057
1058         IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1059         
1060         switch (cmd) {
1061         case SIOCSBANDWIDTH: /* Set bandwidth */
1062                 if (!capable(CAP_NET_ADMIN))
1063                         ret = -EPERM;
1064                 else
1065                         irda_task_execute(self, __irport_change_speed, NULL, 
1066                                           NULL, (void *) irq->ifr_baudrate);
1067                 break;
1068         case SIOCSDONGLE: /* Set dongle */
1069                 if (!capable(CAP_NET_ADMIN)) {
1070                         ret = -EPERM;
1071                         break;
1072                 }
1073
1074                 /* Locking :
1075                  * irda_device_dongle_init() can't be locked.
1076                  * irda_task_execute() doesn't need to be locked.
1077                  * Jean II
1078                  */
1079
1080                 /* Initialize dongle */
1081                 dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
1082                 if (!dongle)
1083                         break;
1084                 
1085                 dongle->set_mode    = NULL;
1086                 dongle->read        = NULL;
1087                 dongle->write       = irport_raw_write;
1088                 dongle->set_dtr_rts = irport_set_dtr_rts;
1089                 
1090                 /* Now initialize the dongle!  */
1091                 dongle->issue->open(dongle, &self->qos);
1092                 
1093                 /* Reset dongle */
1094                 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
1095                                   NULL);        
1096
1097                 /* Make dongle available to driver only now to avoid
1098                  * race conditions - Jean II */
1099                 self->dongle = dongle;
1100                 break;
1101         case SIOCSMEDIABUSY: /* Set media busy */
1102                 if (!capable(CAP_NET_ADMIN)) {
1103                         ret = -EPERM;
1104                         break;
1105                 }
1106
1107                 irda_device_set_media_busy(self->netdev, TRUE);
1108                 break;
1109         case SIOCGRECEIVING: /* Check if we are receiving right now */
1110                 irq->ifr_receiving = irport_is_receiving(self);
1111                 break;
1112         case SIOCSDTRRTS:
1113                 if (!capable(CAP_NET_ADMIN)) {
1114                         ret = -EPERM;
1115                         break;
1116                 }
1117
1118                 /* No real need to lock... */
1119                 spin_lock_irqsave(&self->lock, flags);
1120                 irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
1121                 spin_unlock_irqrestore(&self->lock, flags);
1122                 break;
1123         default:
1124                 ret = -EOPNOTSUPP;
1125         }
1126         
1127         return ret;
1128 }
1129
1130 static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
1131 {
1132         struct irport_cb *self = (struct irport_cb *) dev->priv;
1133         
1134         return &self->stats;
1135 }
1136
1137 MODULE_PARM(io, "1-4i");
1138 MODULE_PARM_DESC(io, "Base I/O addresses");
1139 MODULE_PARM(irq, "1-4i");
1140 MODULE_PARM_DESC(irq, "IRQ lines");
1141
1142 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1143 MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
1144 MODULE_LICENSE("GPL");
1145
1146 module_init(irport_init);
1147 module_exit(irport_cleanup);
1148