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