Import 2.3.18pre1
[davej-history.git] / drivers / net / irda / irport.c
blob615592776696a86c7e37475330faf2f4a9b91f56
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: Wed Aug 11 09:24:46 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: serial.c by Linus Torvalds
13 * Copyright (c) 1997, 1998, 1999 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>
52 #include <asm/system.h>
53 #include <asm/bitops.h>
54 #include <asm/io.h>
56 #include <net/irda/irda.h>
57 #include <net/irda/irmod.h>
58 #include <net/irda/wrapper.h>
59 #include <net/irda/irport.h>
61 #define IO_EXTENT 8
63 /*
64 * Currently you'll need to set these values using insmod like this:
65 * insmod irport io=0x3e8 irq=11
67 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
68 static unsigned int irq[] = { 0, 0, 0, 0 };
70 static unsigned int qos_mtt_bits = 0x03;
72 static struct irda_device *dev_self[] = { NULL, NULL, NULL, NULL};
73 static char *driver_name = "irport";
75 static int irport_open(int i, unsigned int iobase, unsigned int irq);
76 static int irport_close(struct irda_device *idev);
78 static void irport_write_wakeup(struct irda_device *idev);
79 static int irport_write(int iobase, int fifo_size, __u8 *buf, int len);
80 static void irport_receive(struct irda_device *idev);
82 static int irport_net_init(struct net_device *dev);
83 static int irport_net_open(struct net_device *dev);
84 static int irport_net_close(struct net_device *dev);
85 static int irport_is_receiving(struct irda_device *idev);
86 static void irport_set_dtr_rts(struct irda_device *idev, int dtr, int rts);
87 static int irport_raw_write(struct irda_device *idev, __u8 *buf, int len);
89 int __init irport_init(void)
91 int i;
93 for (i=0; (io[i] < 2000) && (i < 4); i++) {
94 int ioaddr = io[i];
95 if (check_region(ioaddr, IO_EXTENT))
96 continue;
97 if (irport_open(i, io[i], irq[i]) == 0)
98 return 0;
101 * Maybe something failed, but we can still be usable for FIR drivers
103 return 0;
107 * Function irport_cleanup ()
109 * Close all configured ports
112 #ifdef MODULE
113 static void irport_cleanup(void)
115 int i;
117 DEBUG( 4, __FUNCTION__ "()\n");
119 for (i=0; i < 4; i++) {
120 if (dev_self[i])
121 irport_close(dev_self[i]);
124 #endif /* MODULE */
126 static int irport_open(int i, unsigned int iobase, unsigned int irq)
128 struct irda_device *idev;
129 int ret;
131 DEBUG( 0, __FUNCTION__ "()\n");
133 /* if (irport_probe(iobase, irq) == -1) */
134 /* return -1; */
137 * Allocate new instance of the driver
139 idev = kmalloc(sizeof(struct irda_device), GFP_KERNEL);
140 if (idev == NULL) {
141 printk( KERN_ERR "IrDA: Can't allocate memory for "
142 "IrDA control block!\n");
143 return -ENOMEM;
145 memset(idev, 0, sizeof(struct irda_device));
147 /* Need to store self somewhere */
148 dev_self[i] = idev;
150 /* Initialize IO */
151 idev->io.iobase2 = iobase;
152 idev->io.irq2 = irq;
153 idev->io.io_ext = IO_EXTENT;
154 idev->io.fifo_size = 16;
156 idev->netdev.base_addr = iobase;
157 idev->netdev.irq = irq;
159 /* Lock the port that we need */
160 ret = check_region(idev->io.iobase2, idev->io.io_ext);
161 if (ret < 0) {
162 DEBUG(0, __FUNCTION__ "(), can't get iobase of 0x%03x\n",
163 idev->io.iobase2);
164 /* irport_cleanup(self->idev); */
165 return -ENODEV;
167 request_region(idev->io.iobase2, idev->io.io_ext, idev->name);
169 /* Initialize QoS for this device */
170 irda_init_max_qos_capabilies(&idev->qos);
172 idev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
173 IR_115200;
175 idev->qos.min_turn_time.bits = qos_mtt_bits;
176 irda_qos_bits_to_value(&idev->qos);
178 idev->flags = IFF_SIR|IFF_PIO;
180 /* Specify which buffer allocation policy we need */
181 idev->rx_buff.flags = GFP_KERNEL;
182 idev->tx_buff.flags = GFP_KERNEL;
184 idev->rx_buff.truesize = 4000;
185 idev->tx_buff.truesize = 4000;
187 /* Initialize callbacks */
188 idev->change_speed = irport_change_speed;
189 idev->wait_until_sent = irport_wait_until_sent;
190 idev->is_receiving = irport_is_receiving;
191 idev->set_dtr_rts = irport_set_dtr_rts;
192 idev->raw_write = irport_raw_write;
194 /* Override the network functions we need to use */
195 idev->netdev.init = irport_net_init;
196 idev->netdev.hard_start_xmit = irport_hard_xmit;
197 idev->netdev.open = irport_net_open;
198 idev->netdev.stop = irport_net_close;
200 /* Open the IrDA device */
201 irda_device_open(idev, driver_name, NULL);
203 return 0;
206 static int irport_close(struct irda_device *idev)
208 ASSERT(idev != NULL, return -1;);
209 ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return -1;);
211 /* Release the IO-port that this driver is using */
212 DEBUG(0 , __FUNCTION__ "(), Releasing Region %03x\n",
213 idev->io.iobase2);
214 release_region(idev->io.iobase2, idev->io.io_ext);
216 irda_device_close(idev);
218 kfree(idev);
220 return 0;
223 void irport_start(struct irda_device *idev, int iobase)
225 unsigned long flags;
227 spin_lock_irqsave(&idev->lock, flags);
229 irport_stop(idev, iobase);
231 /* Initialize UART */
232 outb(UART_LCR_WLEN8, iobase+UART_LCR); /* Reset DLAB */
233 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
235 /* Turn on interrups */
236 outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
238 spin_unlock_irqrestore(&idev->lock, flags);
241 void irport_stop(struct irda_device *idev, int iobase)
243 unsigned long flags;
245 spin_lock_irqsave(&idev->lock, flags);
247 /* Reset UART */
248 outb(0, iobase+UART_MCR);
250 /* Turn off interrupts */
251 outb(0, iobase+UART_IER);
253 spin_unlock_irqrestore(&idev->lock, flags);
257 * Function irport_probe (void)
259 * Start IO port
262 int irport_probe(int iobase)
264 DEBUG(4, __FUNCTION__ "(), iobase=%#x\n", iobase);
266 return 0;
270 * Function irport_change_speed (idev, speed)
272 * Set speed of IrDA port to specified baudrate
275 void irport_change_speed(struct irda_device *idev, __u32 speed)
277 unsigned long flags;
278 int iobase;
279 int fcr; /* FIFO control reg */
280 int lcr; /* Line control reg */
281 int divisor;
283 DEBUG(0, __FUNCTION__ "(), Setting speed to: %d\n", speed);
285 ASSERT(idev != NULL, return;);
286 ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return;);
288 iobase = idev->io.iobase2;
290 /* Update accounting for new speed */
291 idev->io.baudrate = speed;
293 spin_lock_irqsave(&idev->lock, flags);
295 /* Turn off interrupts */
296 outb(0, iobase+UART_IER);
298 divisor = SPEED_MAX/speed;
300 fcr = UART_FCR_ENABLE_FIFO;
303 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
304 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
305 * about this timeout since it will always be fast enough.
307 if (idev->io.baudrate < 38400)
308 fcr |= UART_FCR_TRIGGER_1;
309 else
310 fcr |= UART_FCR_TRIGGER_14;
312 /* IrDA ports use 8N1 */
313 lcr = UART_LCR_WLEN8;
315 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
316 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
317 outb(divisor >> 8, iobase+UART_DLM);
318 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
319 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
321 /* Turn on interrups */
322 outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
324 spin_unlock_irqrestore(&idev->lock, flags);
328 * Function irport_write_wakeup (tty)
330 * Called by the driver when there's room for more data. If we have
331 * more packets to send, we send them here.
334 static void irport_write_wakeup(struct irda_device *idev)
336 int actual = 0;
337 int iobase;
338 int fcr;
340 ASSERT(idev != NULL, return;);
341 ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return;);
343 DEBUG(4, __FUNCTION__ "()\n");
345 /* Finished with frame? */
346 if (idev->tx_buff.len > 0) {
347 /* Write data left in transmit buffer */
348 actual = irport_write(idev->io.iobase2, idev->io.fifo_size,
349 idev->tx_buff.data, idev->tx_buff.len);
350 idev->tx_buff.data += actual;
351 idev->tx_buff.len -= actual;
352 } else {
353 iobase = idev->io.iobase2;
355 * Now serial buffer is almost free & we can start
356 * transmission of another packet
358 idev->netdev.tbusy = 0; /* Unlock */
359 idev->stats.tx_packets++;
361 /* Schedule network layer, so we can get some more frames */
362 mark_bh(NET_BH);
364 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR;
366 if (idev->io.baudrate < 38400)
367 fcr |= UART_FCR_TRIGGER_1;
368 else
369 fcr |= UART_FCR_TRIGGER_14;
372 * Reset Rx FIFO to make sure that all reflected transmit data
373 * will be discarded
375 outb(fcr, iobase+UART_FCR);
377 /* Turn on receive interrupts */
378 outb(/* UART_IER_RLSI| */UART_IER_RDI, iobase+UART_IER);
383 * Function irport_write (driver)
385 * Fill Tx FIFO with transmit data
388 static int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
390 int actual = 0;
392 /* Tx FIFO should be empty! */
393 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
394 DEBUG(0, __FUNCTION__ "(), failed, fifo not empty!\n");
395 return -1;
398 /* Fill FIFO with current frame */
399 while ((fifo_size-- > 0) && (actual < len)) {
400 /* Transmit next byte */
401 outb(buf[actual], iobase+UART_TX);
403 actual++;
406 return actual;
410 * Function irport_xmit (void)
412 * Transmits the current frame until FIFO is full, then
413 * waits until the next transmitt interrupt, and continues until the
414 * frame is transmited.
416 int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
418 struct irda_device *idev;
419 unsigned long flags;
420 int actual = 0;
421 int iobase;
423 ASSERT(dev != NULL, return 0;);
425 idev = (struct irda_device *) dev->priv;
427 ASSERT(idev != NULL, return 0;);
428 ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return 0;);
430 iobase = idev->io.iobase2;
432 /* Lock transmit buffer */
433 if (irda_lock((void *) &dev->tbusy) == FALSE) {
434 int tickssofar = jiffies - dev->trans_start;
435 if ((tickssofar < 5) || !dev->start)
436 return -EBUSY;
438 WARNING("%s: transmit timed out\n", dev->name);
439 irport_start(idev, iobase);
440 irport_change_speed(idev, idev->io.baudrate);
442 dev->trans_start = jiffies;
445 spin_lock_irqsave(&idev->lock, flags);
447 /* Init tx buffer */
448 idev->tx_buff.data = idev->tx_buff.head;
450 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
451 idev->tx_buff.len = async_wrap_skb(skb, idev->tx_buff.data,
452 idev->tx_buff.truesize);
454 idev->tx_buff.data += actual;
455 idev->tx_buff.len -= actual;
457 /* Turn on transmit finished interrupt. Will fire immediately! */
458 outb(UART_IER_THRI, iobase+UART_IER);
460 spin_unlock_irqrestore(&idev->lock, flags);
462 dev_kfree_skb(skb);
464 return 0;
468 * Function irport_receive (idev)
470 * Receive one frame from the infrared port
473 static void irport_receive(struct irda_device *idev)
475 int iobase;
476 int boguscount = 0;
478 ASSERT(idev != NULL, return;);
480 iobase = idev->io.iobase2;
483 * Receive all characters in Rx FIFO, unwrap and unstuff them.
484 * async_unwrap_char will deliver all found frames
486 do {
487 async_unwrap_char(idev, inb(iobase+UART_RX));
489 /* Make sure we don't stay here to long */
490 if (boguscount++ > 32) {
491 DEBUG(0,__FUNCTION__ "(), breaking!\n");
492 break;
494 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
498 * Function irport_interrupt (irq, dev_id, regs)
500 * Interrupt handler
502 void irport_interrupt(int irq, void *dev_id, struct pt_regs *regs)
504 struct irda_device *idev = (struct irda_device *) dev_id;
505 int iobase;
506 int iir, lsr;
507 int boguscount = 0;
509 if (!idev) {
510 WARNING(__FUNCTION__ "() irq %d for unknown device.\n", irq);
511 return;
514 spin_lock(&idev->lock);
516 idev->netdev.interrupt = 1;
518 iobase = idev->io.iobase2;
520 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
521 while (iir) {
522 /* Clear interrupt */
523 lsr = inb(iobase+UART_LSR);
525 DEBUG(4, __FUNCTION__ "(), iir=%02x, lsr=%02x, iobase=%#x\n",
526 iir, lsr, iobase);
528 switch (iir) {
529 case UART_IIR_RLSI:
530 DEBUG(2, __FUNCTION__ "(), RLSI\n");
531 break;
532 case UART_IIR_RDI:
533 //if (lsr & UART_LSR_DR)
534 /* Receive interrupt */
535 irport_receive(idev);
536 break;
537 case UART_IIR_THRI:
538 if (lsr & UART_LSR_THRE)
539 /* Transmitter ready for data */
540 irport_write_wakeup(idev);
541 break;
542 default:
543 DEBUG(0, __FUNCTION__ "(), unhandled IIR=%#x\n", iir);
544 break;
547 /* Make sure we don't stay here to long */
548 if (boguscount++ > 100)
549 break;
551 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
553 idev->netdev.interrupt = 0;
555 spin_unlock(&idev->lock);
558 static int irport_net_init(struct net_device *dev)
560 /* Set up to be a normal IrDA network device driver */
561 irda_device_setup(dev);
563 /* Insert overrides below this line! */
565 return 0;
569 * Function irport_net_open (dev)
574 static int irport_net_open(struct net_device *dev)
576 struct irda_device *idev;
577 int iobase;
579 ASSERT(dev != NULL, return -1;);
580 idev = (struct irda_device *) dev->priv;
582 iobase = idev->io.iobase2;
584 if (request_irq(idev->io.irq2, irport_interrupt, 0, idev->name,
585 (void *) idev))
586 return -EAGAIN;
588 irport_start(idev, iobase);
589 irda_device_net_open(dev);
591 /* Change speed to make sure dongles follow us again */
592 if (idev->change_speed)
593 idev->change_speed(idev, 9600);
595 MOD_INC_USE_COUNT;
597 return 0;
601 * Function irport_net_close (idev)
606 static int irport_net_close(struct net_device *dev)
608 struct irda_device *idev;
609 int iobase;
611 DEBUG(4, __FUNCTION__ "()\n");
613 ASSERT(dev != NULL, return -1;);
614 idev = (struct irda_device *) dev->priv;
616 ASSERT(idev != NULL, return -1;);
617 ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return -1;)
619 iobase = idev->io.iobase2;
621 irda_device_net_close(dev);
622 irport_stop(idev, iobase);
624 free_irq(idev->io.irq2, idev);
626 MOD_DEC_USE_COUNT;
628 return 0;
632 * Function irport_wait_until_sent (idev)
634 * Delay exectution until finished transmitting
637 void irport_wait_until_sent(struct irda_device *idev)
639 int iobase;
641 iobase = idev->io.iobase2;
643 /* Wait until Tx FIFO is empty */
644 while (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
645 DEBUG(2, __FUNCTION__ "(), waiting!\n");
646 current->state = TASK_INTERRUPTIBLE;
647 schedule_timeout(MSECS_TO_JIFFIES(60));
652 * Function irport_is_receiving (idev)
654 * Returns true is we are currently receiving data
657 static int irport_is_receiving(struct irda_device *idev)
659 return (idev->rx_buff.state != OUTSIDE_FRAME);
663 * Function irport_set_dtr_rts (tty, dtr, rts)
665 * This function can be used by dongles etc. to set or reset the status
666 * of the dtr and rts lines
668 static void irport_set_dtr_rts(struct irda_device *idev, int dtr, int rts)
670 int iobase;
672 ASSERT(idev != NULL, return;);
673 ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return;);
675 iobase = idev->io.iobase2;
677 if (dtr)
678 dtr = UART_MCR_DTR;
679 if (rts)
680 rts = UART_MCR_RTS;
682 outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
685 static int irport_raw_write(struct irda_device *idev, __u8 *buf, int len)
687 int iobase;
688 int actual = 0;
690 ASSERT(idev != NULL, return -1;);
691 ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return -1;);
693 iobase = idev->io.iobase2;
695 /* Tx FIFO should be empty! */
696 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
697 DEBUG( 0, __FUNCTION__ "(), failed, fifo not empty!\n");
698 return -1;
701 /* Fill FIFO with current frame */
702 while (actual < len) {
703 /* Transmit next byte */
704 outb(buf[actual], iobase+UART_TX);
705 actual++;
708 return actual;
711 #ifdef MODULE
713 MODULE_PARM(io, "1-4i");
714 MODULE_PARM(irq, "1-4i");
716 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
717 MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
720 * Function cleanup_module (void)
725 void cleanup_module(void)
727 irport_cleanup();
731 * Function init_module (void)
735 int init_module(void)
737 return irport_init();
740 #endif /* MODULE */