Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / net / irda / irport.c
blobd9c2edbc0cb092368afbb110816a590e3249f721
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
13 * Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved.
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.
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.
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
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.
37 ********************************************************************/
39 #include <linux/module.h>
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>
53 #include <asm/system.h>
54 #include <asm/bitops.h>
55 #include <asm/io.h>
57 #include <net/irda/irda.h>
58 #include <net/irda/irmod.h>
59 #include <net/irda/wrapper.h>
60 #include <net/irda/irport.h>
62 #define IO_EXTENT 8
64 /*
65 * Currently you'll need to set these values using insmod like this:
66 * insmod irport io=0x3e8 irq=11
68 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
69 static unsigned int irq[] = { 0, 0, 0, 0 };
71 static unsigned int qos_mtt_bits = 0x03;
73 static struct irport_cb *dev_self[] = { NULL, NULL, NULL, NULL};
74 static char *driver_name = "irport";
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);
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);
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);
101 int __init irport_init(void)
103 int i;
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;
113 * Maybe something failed, but we can still be usable for FIR drivers
115 return 0;
119 * Function irport_cleanup ()
121 * Close all configured ports
124 #ifdef MODULE
125 static void irport_cleanup(void)
127 int i;
129 IRDA_DEBUG( 4, __FUNCTION__ "()\n");
131 for (i=0; i < 4; i++) {
132 if (dev_self[i])
133 irport_close(dev_self[i]);
136 #endif /* MODULE */
138 struct irport_cb *
139 irport_open(int i, unsigned int iobase, unsigned int irq)
141 struct net_device *dev;
142 struct irport_cb *self;
143 int ret;
144 int err;
146 IRDA_DEBUG(0, __FUNCTION__ "()\n");
149 * Allocate new instance of the driver
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;
157 memset(self, 0, sizeof(struct irport_cb));
158 spin_lock_init(&self->lock);
160 /* Need to store self somewhere */
161 dev_self[i] = self;
162 self->priv = self;
163 self->index = i;
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;
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;
178 request_region(self->io.sir_base, self->io.sir_ext, driver_name);
180 /* Initialize QoS for this device */
181 irda_init_max_qos_capabilies(&self->qos);
183 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
184 IR_115200;
186 self->qos.min_turn_time.bits = qos_mtt_bits;
187 irda_qos_bits_to_value(&self->qos);
189 self->flags = IFF_SIR|IFF_PIO;
191 /* Specify how much memory we want */
192 self->rx_buff.truesize = 4000;
193 self->tx_buff.truesize = 4000;
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);
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;
210 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
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;
218 if (!(dev = dev_alloc("irda%d", &err))) {
219 ERROR(__FUNCTION__ "(), dev_alloc() failed!\n");
220 return NULL;
222 self->netdev = dev;
224 /* May be overridden by piggyback drivers */
225 dev->priv = (void *) self;
226 self->interrupt = irport_interrupt;
227 self->change_speed = irport_change_speed;
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;
239 /* Make ifconfig display some details */
240 dev->base_addr = iobase;
241 dev->irq = irq;
243 rtnl_lock();
244 err = register_netdevice(dev);
245 rtnl_unlock();
246 if (err) {
247 ERROR(__FUNCTION__ "(), register_netdev() failed!\n");
248 return NULL;
250 MESSAGE("IrDA: Registered device %s\n", dev->name);
252 return self;
255 int irport_close(struct irport_cb *self)
257 ASSERT(self != NULL, return -1;);
259 /* We are not using any dongle anymore! */
260 if (self->dongle)
261 irda_device_dongle_cleanup(self->dongle);
262 self->dongle = NULL;
264 /* Remove netdevice */
265 if (self->netdev) {
266 rtnl_lock();
267 unregister_netdevice(self->netdev);
268 rtnl_unlock();
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);
276 if (self->tx_buff.head)
277 kfree(self->tx_buff.head);
279 if (self->rx_buff.head)
280 kfree(self->rx_buff.head);
282 /* Remove ourselves */
283 dev_self[self->index] = NULL;
284 kfree(self);
286 return 0;
289 void irport_start(struct irport_cb *self)
291 unsigned long flags;
292 int iobase;
294 iobase = self->io.sir_base;
296 spin_lock_irqsave(&self->lock, flags);
298 irport_stop(self);
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);
304 /* Turn on interrups */
305 outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
307 spin_unlock_irqrestore(&self->lock, flags);
310 void irport_stop(struct irport_cb *self)
312 unsigned long flags;
313 int iobase;
315 iobase = self->io.sir_base;
317 spin_lock_irqsave(&self->lock, flags);
319 /* Reset UART */
320 outb(0, iobase+UART_MCR);
322 /* Turn off interrupts */
323 outb(0, iobase+UART_IER);
325 spin_unlock_irqrestore(&self->lock, flags);
329 * Function irport_probe (void)
331 * Start IO port
334 int irport_probe(int iobase)
336 IRDA_DEBUG(4, __FUNCTION__ "(), iobase=%#x\n", iobase);
338 return 0;
342 * Function irport_change_speed (self, speed)
344 * Set speed of IrDA port to specified baudrate
347 void irport_change_speed(void *priv, __u32 speed)
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;
356 IRDA_DEBUG(2, __FUNCTION__ "(), Setting speed to: %d\n", speed);
358 ASSERT(self != NULL, return;);
360 iobase = self->io.sir_base;
362 /* Update accounting for new speed */
363 self->io.speed = speed;
365 spin_lock_irqsave(&self->lock, flags);
367 /* Turn off interrupts */
368 outb(0, iobase+UART_IER);
370 divisor = SPEED_MAX/speed;
372 fcr = UART_FCR_ENABLE_FIFO;
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.
379 if (self->io.speed < 38400)
380 fcr |= UART_FCR_TRIGGER_1;
381 else
382 fcr |= UART_FCR_TRIGGER_14;
384 /* IrDA ports use 8N1 */
385 lcr = UART_LCR_WLEN8;
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 */
393 /* Turn on interrups */
394 outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
396 spin_unlock_irqrestore(&self->lock, flags);
400 * Function __irport_change_speed (instance, state, param)
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
405 int __irport_change_speed(struct irda_task *task)
407 struct irport_cb *self;
408 __u32 speed = (__u32) task->param;
409 int ret = 0;
411 IRDA_DEBUG(2, __FUNCTION__ "(), <%ld>\n", jiffies);
413 self = (struct irport_cb *) task->instance;
415 ASSERT(self != NULL, return -1;);
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;
424 /* Try again later */
425 ret = MSECS_TO_JIFFIES(20);
426 break;
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);
438 /* Change speed of dongle */
439 if (irda_task_execute(self->dongle,
440 self->dongle->issue->change_speed,
441 NULL, task, (void *) speed))
443 /* Dongle need more time to change its speed */
444 irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
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);
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;
469 return ret;
473 * Function irport_write_wakeup (tty)
475 * Called by the driver when there's room for more data. If we have
476 * more packets to send, we send them here.
479 static void irport_write_wakeup(struct irport_cb *self)
481 int actual = 0;
482 int iobase;
483 int fcr;
485 ASSERT(self != NULL, return;);
487 IRDA_DEBUG(4, __FUNCTION__ "()\n");
489 iobase = self->io.sir_base;
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 {
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
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);
514 self->stats.tx_packets++;
517 * Reset Rx FIFO to make sure that all reflected transmit data
518 * is discarded. This is needed for half duplex operation
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;
526 outb(fcr, iobase+UART_FCR);
528 /* Turn on receive interrupts */
529 outb(UART_IER_RDI, iobase+UART_IER);
534 * Function irport_write (driver)
536 * Fill Tx FIFO with transmit data
539 static int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
541 int actual = 0;
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;
549 /* Fill FIFO with current frame */
550 while ((fifo_size-- > 0) && (actual < len)) {
551 /* Transmit next byte */
552 outb(buf[actual], iobase+UART_TX);
554 actual++;
557 return actual;
561 * Function irport_change_speed_complete (task)
563 * Called when the change speed operation completes
566 static int irport_change_speed_complete(struct irda_task *task)
568 struct irport_cb *self;
570 IRDA_DEBUG(0, __FUNCTION__ "()\n");
572 self = (struct irport_cb *) task->instance;
574 ASSERT(self != NULL, return -1;);
575 ASSERT(self->netdev != NULL, return -1;);
577 /* Finished changing speed, so we are not busy any longer */
578 /* Signal network layer so it can try to send the frame */
580 netif_wake_queue(self->netdev);
582 return 0;
586 * Function irport_timeout (struct net_device *dev)
588 * The networking layer thinks we timed out.
592 static void irport_timeout(struct net_device *dev)
594 struct irport_cb *self;
595 int iobase;
597 self = (struct irport_cb *) dev->priv;
598 iobase = self->io.sir_base;
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);
608 * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
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.
614 int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
616 struct irport_cb *self;
617 unsigned long flags;
618 int iobase;
619 __u32 speed;
621 ASSERT(dev != NULL, return 0;);
623 self = (struct irport_cb *) dev->priv;
624 ASSERT(self != NULL, return 0;);
626 iobase = self->io.sir_base;
628 netif_stop_queue(dev);
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;
642 spin_lock_irqsave(&self->lock, flags);
644 /* Init tx buffer */
645 self->tx_buff.data = self->tx_buff.head;
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);
651 self->stats.tx_bytes += self->tx_buff.len;
653 /* Turn on transmit finished interrupt. Will fire immediately! */
654 outb(UART_IER_THRI, iobase+UART_IER);
656 spin_unlock_irqrestore(&self->lock, flags);
658 dev_kfree_skb(skb);
660 return 0;
664 * Function irport_receive (self)
666 * Receive one frame from the infrared port
669 static void irport_receive(struct irport_cb *self)
671 int boguscount = 0;
672 int iobase;
674 ASSERT(self != NULL, return;);
676 iobase = self->io.sir_base;
679 * Receive all characters in Rx FIFO, unwrap and unstuff them.
680 * async_unwrap_char will deliver all found frames
682 do {
683 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
684 inb(iobase+UART_RX));
686 /* Make sure we don't stay here to long */
687 if (boguscount++ > 32) {
688 IRDA_DEBUG(2,__FUNCTION__ "(), breaking!\n");
689 break;
691 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
695 * Function irport_interrupt (irq, dev_id, regs)
697 * Interrupt handler
699 void irport_interrupt(int irq, void *dev_id, struct pt_regs *regs)
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;
707 if (!dev) {
708 WARNING(__FUNCTION__ "() irq %d for unknown device.\n", irq);
709 return;
711 self = (struct irport_cb *) dev->priv;
713 spin_lock(&self->lock);
715 iobase = self->io.sir_base;
717 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
718 while (iir) {
719 /* Clear interrupt */
720 lsr = inb(iobase+UART_LSR);
722 IRDA_DEBUG(4, __FUNCTION__
723 "(), iir=%02x, lsr=%02x, iobase=%#x\n",
724 iir, lsr, iobase);
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;
744 /* Make sure we don't stay here to long */
745 if (boguscount++ > 100)
746 break;
748 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
750 spin_unlock(&self->lock);
753 static int irport_net_init(struct net_device *dev)
755 /* Set up to be a normal IrDA network device driver */
756 irda_device_setup(dev);
758 /* Insert overrides below this line! */
760 return 0;
764 * Function irport_net_open (dev)
766 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
769 int irport_net_open(struct net_device *dev)
771 struct irport_cb *self;
772 int iobase;
774 ASSERT(dev != NULL, return -1;);
775 self = (struct irport_cb *) dev->priv;
777 iobase = self->io.sir_base;
779 if (request_irq(self->io.irq, self->interrupt, 0, dev->name,
780 (void *) dev))
781 return -EAGAIN;
783 irport_start(self);
787 * Open new IrLAP layer instance, now that everything should be
788 * initialized properly
790 self->irlap = irlap_open(dev, &self->qos);
792 /* FIXME: change speed of dongle */
793 /* Ready to play! */
795 netif_start_queue(dev);
797 MOD_INC_USE_COUNT;
799 return 0;
803 * Function irport_net_close (self)
805 * Network device is taken down. Usually this is done by
806 * "ifconfig irda0 down"
808 int irport_net_close(struct net_device *dev)
810 struct irport_cb *self;
811 int iobase;
813 IRDA_DEBUG(4, __FUNCTION__ "()\n");
815 ASSERT(dev != NULL, return -1;);
816 self = (struct irport_cb *) dev->priv;
818 ASSERT(self != NULL, return -1;);
820 iobase = self->io.sir_base;
822 /* Stop device */
823 netif_stop_queue(dev);
825 /* Stop and remove instance of IrLAP */
826 if (self->irlap)
827 irlap_close(self->irlap);
828 self->irlap = NULL;
830 irport_stop(self);
832 free_irq(self->io.irq, dev);
834 MOD_DEC_USE_COUNT;
836 return 0;
840 * Function irport_wait_until_sent (self)
842 * Delay exectution until finished transmitting
845 #if 0
846 void irport_wait_until_sent(struct irport_cb *self)
848 int iobase;
850 iobase = self->io.sir_base;
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));
859 #endif
862 * Function irport_is_receiving (self)
864 * Returns true is we are currently receiving data
867 static int irport_is_receiving(struct irport_cb *self)
869 return (self->rx_buff.state != OUTSIDE_FRAME);
873 * Function irport_set_dtr_rts (tty, dtr, rts)
875 * This function can be used by dongles etc. to set or reset the status
876 * of the dtr and rts lines
878 static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
880 struct irport_cb *self = dev->priv;
881 int iobase;
883 ASSERT(self != NULL, return -1;);
885 iobase = self->io.sir_base;
887 if (dtr)
888 dtr = UART_MCR_DTR;
889 if (rts)
890 rts = UART_MCR_RTS;
892 outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
894 return 0;
897 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
899 struct irport_cb *self = (struct irport_cb *) dev->priv;
900 int actual = 0;
901 int iobase;
903 ASSERT(self != NULL, return -1;);
905 iobase = self->io.sir_base;
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;
913 /* Fill FIFO with current frame */
914 while (actual < len) {
915 /* Transmit next byte */
916 outb(buf[actual], iobase+UART_TX);
917 actual++;
920 return actual;
924 * Function irport_net_ioctl (dev, rq, cmd)
926 * Process IOCTL commands for this device
929 static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
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;
937 ASSERT(dev != NULL, return -1;);
939 self = dev->priv;
941 ASSERT(self != NULL, return -1;);
943 IRDA_DEBUG(2, __FUNCTION__ "(), %s, (cmd=0x%X)\n", dev->name, cmd);
945 /* Disable interrupts & save flags */
946 save_flags(flags);
947 cli();
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;
964 dongle->set_mode = NULL;
965 dongle->read = NULL;
966 dongle->write = irport_raw_write;
967 dongle->set_dtr_rts = irport_set_dtr_rts;
969 self->dongle = dongle;
971 /* Now initialize the dongle! */
972 dongle->issue->open(dongle, &self->qos);
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;
995 restore_flags(flags);
997 return ret;
1000 static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
1002 struct irport_cb *self = (struct irport_cb *) dev->priv;
1004 return &self->stats;
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");
1013 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1014 MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
1016 void cleanup_module(void)
1018 irport_cleanup();
1021 int init_module(void)
1023 return irport_init();
1025 #endif /* MODULE */