~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/net/irda/irport.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.