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