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