x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / net / irda / donauboe.c
blobb337e6d23a88109cc929a558cb1305bea9fa3ab2
1 /*****************************************************************
3 * Filename: donauboe.c
4 * Version: 2.17
5 * Description: Driver for the Toshiba OBOE (or type-O or 701)
6 * FIR Chipset, also supports the DONAUOBOE (type-DO
7 * or d01) FIR chipset which as far as I know is
8 * register compatible.
9 * Documentation: http://libxg.free.fr/irda/lib-irda.html
10 * Status: Experimental.
11 * Author: James McKenzie <james@fishsoup.dhs.org>
12 * Created at: Sat May 8 12:35:27 1999
13 * Modified: Paul Bristow <paul.bristow@technologist.com>
14 * Modified: Mon Nov 11 19:10:05 1999
15 * Modified: James McKenzie <james@fishsoup.dhs.org>
16 * Modified: Thu Mar 16 12:49:00 2000 (Substantial rewrite)
17 * Modified: Sat Apr 29 00:23:03 2000 (Added DONAUOBOE support)
18 * Modified: Wed May 24 23:45:02 2000 (Fixed chipio_t structure)
19 * Modified: 2.13 Christian Gennerat <christian.gennerat@polytechnique.org>
20 * Modified: 2.13 dim jan 07 21:57:39 2001 (tested with kernel 2.4 & irnet/ppp)
21 * Modified: 2.14 Christian Gennerat <christian.gennerat@polytechnique.org>
22 * Modified: 2.14 lun fev 05 17:55:59 2001 (adapted to patch-2.4.1-pre8-irda1)
23 * Modified: 2.15 Martin Lucina <mato@kotelna.sk>
24 * Modified: 2.15 Fri Jun 21 20:40:59 2002 (sync with 2.4.18, substantial fixes)
25 * Modified: 2.16 Martin Lucina <mato@kotelna.sk>
26 * Modified: 2.16 Sat Jun 22 18:54:29 2002 (fix freeregion, default to verbose)
27 * Modified: 2.17 Christian Gennerat <christian.gennerat@polytechnique.org>
28 * Modified: 2.17 jeu sep 12 08:50:20 2002 (save_flags();cli(); replaced by spinlocks)
29 * Modified: 2.18 Christian Gennerat <christian.gennerat@polytechnique.org>
30 * Modified: 2.18 ven jan 10 03:14:16 2003 Change probe default options
32 * Copyright (c) 1999 James McKenzie, All Rights Reserved.
34 * This program is free software; you can redistribute it and/or
35 * modify it under the terms of the GNU General Public License as
36 * published by the Free Software Foundation; either version 2 of
37 * the License, or (at your option) any later version.
39 * Neither James McKenzie nor Cambridge University admit liability nor
40 * provide warranty for any of this software. This material is
41 * provided "AS-IS" and at no charge.
43 * Applicable Models : Libretto 100/110CT and many more.
44 * Toshiba refers to this chip as the type-O IR port,
45 * or the type-DO IR port.
47 ********************************************************************/
49 /* Look at toshoboe.h (currently in include/net/irda) for details of */
50 /* Where to get documentation on the chip */
52 /* See below for a description of the logic in this driver */
54 /* User servicable parts */
55 /* USE_PROBE Create the code which probes the chip and does a few tests */
56 /* do_probe module parameter Enable this code */
57 /* Probe code is very useful for understanding how the hardware works */
58 /* Use it with various combinations of TT_LEN, RX_LEN */
59 /* Strongly recommended, disable if the probe fails on your machine */
60 /* and send me <james@fishsoup.dhs.org> the output of dmesg */
61 #define USE_PROBE 1
62 #undef USE_PROBE
64 /* Trace Transmit ring, interrupts, Receive ring or not ? */
65 #define PROBE_VERBOSE 1
67 /* Debug option, examine sent and received raw data */
68 /* Irdadump is better, but does not see all packets. enable it if you want. */
69 #undef DUMP_PACKETS
71 /* MIR mode has not been tested. Some behaviour is different */
72 /* Seems to work against an Ericsson R520 for me. -Martin */
73 #define USE_MIR
75 /* Schedule back to back hardware transmits wherever possible, otherwise */
76 /* we need an interrupt for every frame, unset if oboe works for a bit and */
77 /* then hangs */
78 #define OPTIMIZE_TX
80 /* Set the number of slots in the rings */
81 /* If you get rx/tx fifo overflows at high bitrates, you can try increasing */
82 /* these */
84 #define RING_SIZE (OBOE_RING_SIZE_RX8 | OBOE_RING_SIZE_TX8)
85 #define TX_SLOTS 8
86 #define RX_SLOTS 8
89 /* Less user servicable parts below here */
91 /* Test, Transmit and receive buffer sizes, adjust at your peril */
92 /* remarks: nfs usually needs 1k blocks */
93 /* remarks: in SIR mode, CRC is received, -> RX_LEN=TX_LEN+2 */
94 /* remarks: test accepts large blocks. Standard is 0x80 */
95 /* When TT_LEN > RX_LEN (SIR mode) data is stored in successive slots. */
96 /* When 3 or more slots are needed for each test packet, */
97 /* data received in the first slots is overwritten, even */
98 /* if OBOE_CTL_RX_HW_OWNS is not set, without any error! */
99 #define TT_LEN 0x80
100 #define TX_LEN 0xc00
101 #define RX_LEN 0xc04
102 /* Real transmitted length (SIR mode) is about 14+(2%*TX_LEN) more */
103 /* long than user-defined length (see async_wrap_skb) and is less then 4K */
104 /* Real received length is (max RX_LEN) differs from user-defined */
105 /* length only b the CRC (2 or 4 bytes) */
106 #define BUF_SAFETY 0x7a
107 #define RX_BUF_SZ (RX_LEN)
108 #define TX_BUF_SZ (TX_LEN+BUF_SAFETY)
111 /* Logic of the netdev part of this driver */
113 /* The RX ring is filled with buffers, when a packet arrives */
114 /* it is DMA'd into the buffer which is marked used and RxDone called */
115 /* RxDone forms an skb (and checks the CRC if in SIR mode) and ships */
116 /* the packet off upstairs */
118 /* The transmitter on the oboe chip can work in one of two modes */
119 /* for each ring->tx[] the transmitter can either */
120 /* a) transmit the packet, leave the trasmitter enabled and proceed to */
121 /* the next ring */
122 /* OR */
123 /* b) transmit the packet, switch off the transmitter and issue TxDone */
125 /* All packets are entered into the ring in mode b), if the ring was */
126 /* empty the transmitter is started. */
128 /* If OPTIMIZE_TX is defined then in TxDone if the ring contains */
129 /* more than one packet, all but the last are set to mode a) [HOWEVER */
130 /* the hardware may not notice this, this is why we start in mode b) ] */
131 /* then restart the transmitter */
133 /* If OPTIMIZE_TX is not defined then we just restart the transmitter */
134 /* if the ring isn't empty */
136 /* Speed changes are delayed until the TxRing is empty */
137 /* mtt is handled by generating packets with bad CRCs, before the data */
139 /* TODO: */
140 /* check the mtt works ok */
141 /* finish the watchdog */
143 /* No user servicable parts below here */
145 #include <linux/module.h>
147 #include <linux/kernel.h>
148 #include <linux/types.h>
149 #include <linux/skbuff.h>
150 #include <linux/netdevice.h>
151 #include <linux/ioport.h>
152 #include <linux/delay.h>
153 #include <linux/slab.h>
154 #include <linux/init.h>
155 #include <linux/interrupt.h>
156 #include <linux/pci.h>
157 #include <linux/rtnetlink.h>
159 #include <asm/io.h>
161 #include <net/irda/wrapper.h>
162 #include <net/irda/irda.h>
163 //#include <net/irda/irmod.h>
164 //#include <net/irda/irlap_frame.h>
165 #include <net/irda/irda_device.h>
166 #include <net/irda/crc.h>
168 #include "donauboe.h"
170 #define INB(port) inb_p(port)
171 #define OUTB(val,port) outb_p(val,port)
172 #define OUTBP(val,port) outb_p(val,port)
174 #define PROMPT OUTB(OBOE_PROMPT_BIT,OBOE_PROMPT);
176 #if PROBE_VERBOSE
177 #define PROBE_DEBUG(args...) (printk (args))
178 #else
179 #define PROBE_DEBUG(args...) ;
180 #endif
182 /* Set the DMA to be byte at a time */
183 #define CONFIG0H_DMA_OFF OBOE_CONFIG0H_RCVANY
184 #define CONFIG0H_DMA_ON_NORX CONFIG0H_DMA_OFF| OBOE_CONFIG0H_ENDMAC
185 #define CONFIG0H_DMA_ON CONFIG0H_DMA_ON_NORX | OBOE_CONFIG0H_ENRX
187 static const struct pci_device_id toshoboe_pci_tbl[] = {
188 { PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_FIR701, PCI_ANY_ID, PCI_ANY_ID, },
189 { PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_FIRD01, PCI_ANY_ID, PCI_ANY_ID, },
190 { } /* Terminating entry */
192 MODULE_DEVICE_TABLE(pci, toshoboe_pci_tbl);
194 #define DRIVER_NAME "toshoboe"
195 static char *driver_name = DRIVER_NAME;
197 static int max_baud = 4000000;
198 #ifdef USE_PROBE
199 static bool do_probe = false;
200 #endif
203 /**********************************************************************/
204 static int
205 toshoboe_checkfcs (unsigned char *buf, int len)
207 int i;
208 union
210 __u16 value;
211 __u8 bytes[2];
213 fcs;
215 fcs.value = INIT_FCS;
217 for (i = 0; i < len; ++i)
218 fcs.value = irda_fcs (fcs.value, *(buf++));
220 return fcs.value == GOOD_FCS;
223 /***********************************************************************/
224 /* Generic chip handling code */
225 #ifdef DUMP_PACKETS
226 static unsigned char dump[50];
227 static void
228 _dumpbufs (unsigned char *data, int len, char tete)
230 int i,j;
231 char head=tete;
232 for (i=0;i<len;i+=16) {
233 for (j=0;j<16 && i+j<len;j++) { sprintf(&dump[3*j],"%02x.",data[i+j]); }
234 dump [3*j]=0;
235 pr_debug("%c%s\n", head, dump);
236 head='+';
239 #endif
241 #ifdef USE_PROBE
242 /* Dump the registers */
243 static void
244 toshoboe_dumpregs (struct toshoboe_cb *self)
246 __u32 ringbase;
248 ringbase = INB (OBOE_RING_BASE0) << 10;
249 ringbase |= INB (OBOE_RING_BASE1) << 18;
250 ringbase |= INB (OBOE_RING_BASE2) << 26;
252 printk (KERN_ERR DRIVER_NAME ": Register dump:\n");
253 printk (KERN_ERR "Interrupts: Tx:%d Rx:%d TxUnder:%d RxOver:%d Sip:%d\n",
254 self->int_tx, self->int_rx, self->int_txunder, self->int_rxover,
255 self->int_sip);
256 printk (KERN_ERR "RX %02x TX %02x RingBase %08x\n",
257 INB (OBOE_RXSLOT), INB (OBOE_TXSLOT), ringbase);
258 printk (KERN_ERR "RING_SIZE %02x IER %02x ISR %02x\n",
259 INB (OBOE_RING_SIZE), INB (OBOE_IER), INB (OBOE_ISR));
260 printk (KERN_ERR "CONFIG1 %02x STATUS %02x\n",
261 INB (OBOE_CONFIG1), INB (OBOE_STATUS));
262 printk (KERN_ERR "CONFIG0 %02x%02x ENABLE %02x%02x\n",
263 INB (OBOE_CONFIG0H), INB (OBOE_CONFIG0L),
264 INB (OBOE_ENABLEH), INB (OBOE_ENABLEL));
265 printk (KERN_ERR "NEW_PCONFIG %02x%02x CURR_PCONFIG %02x%02x\n",
266 INB (OBOE_NEW_PCONFIGH), INB (OBOE_NEW_PCONFIGL),
267 INB (OBOE_CURR_PCONFIGH), INB (OBOE_CURR_PCONFIGL));
268 printk (KERN_ERR "MAXLEN %02x%02x RXCOUNT %02x%02x\n",
269 INB (OBOE_MAXLENH), INB (OBOE_MAXLENL),
270 INB (OBOE_RXCOUNTL), INB (OBOE_RXCOUNTH));
272 if (self->ring)
274 int i;
275 ringbase = virt_to_bus (self->ring);
276 printk (KERN_ERR "Ring at %08x:\n", ringbase);
277 printk (KERN_ERR "RX:");
278 for (i = 0; i < RX_SLOTS; ++i)
279 printk (" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
280 printk ("\n");
281 printk (KERN_ERR "TX:");
282 for (i = 0; i < RX_SLOTS; ++i)
283 printk (" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
284 printk ("\n");
287 #endif
289 /*Don't let the chip look at memory */
290 static void
291 toshoboe_disablebm (struct toshoboe_cb *self)
293 __u8 command;
294 pci_read_config_byte (self->pdev, PCI_COMMAND, &command);
295 command &= ~PCI_COMMAND_MASTER;
296 pci_write_config_byte (self->pdev, PCI_COMMAND, command);
300 /* Shutdown the chip and point the taskfile reg somewhere else */
301 static void
302 toshoboe_stopchip (struct toshoboe_cb *self)
304 /*Disable interrupts */
305 OUTB (0x0, OBOE_IER);
306 /*Disable DMA, Disable Rx, Disable Tx */
307 OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
308 /*Disable SIR MIR FIR, Tx and Rx */
309 OUTB (0x00, OBOE_ENABLEH);
310 /*Point the ring somewhere safe */
311 OUTB (0x3f, OBOE_RING_BASE2);
312 OUTB (0xff, OBOE_RING_BASE1);
313 OUTB (0xff, OBOE_RING_BASE0);
315 OUTB (RX_LEN >> 8, OBOE_MAXLENH);
316 OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
318 /*Acknoledge any pending interrupts */
319 OUTB (0xff, OBOE_ISR);
321 /*Why */
322 OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
324 /*switch it off */
325 OUTB (OBOE_CONFIG1_OFF, OBOE_CONFIG1);
327 toshoboe_disablebm (self);
330 /* Transmitter initialization */
331 static void
332 toshoboe_start_DMA (struct toshoboe_cb *self, int opts)
334 OUTB (0x0, OBOE_ENABLEH);
335 OUTB (CONFIG0H_DMA_ON | opts, OBOE_CONFIG0H);
336 OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
337 PROMPT;
340 /*Set the baud rate */
341 static void
342 toshoboe_setbaud (struct toshoboe_cb *self)
344 __u16 pconfig = 0;
345 __u8 config0l = 0;
347 pr_debug("%s(%d/%d)\n", __func__, self->speed, self->io.speed);
349 switch (self->speed)
351 case 2400:
352 case 4800:
353 case 9600:
354 case 19200:
355 case 38400:
356 case 57600:
357 case 115200:
358 #ifdef USE_MIR
359 case 1152000:
360 #endif
361 case 4000000:
362 break;
363 default:
365 printk (KERN_ERR DRIVER_NAME ": switch to unsupported baudrate %d\n",
366 self->speed);
367 return;
370 switch (self->speed)
372 /* For SIR the preamble is done by adding XBOFs */
373 /* to the packet */
374 /* set to filtered SIR mode, filter looks for BOF and EOF */
375 case 2400:
376 pconfig |= 47 << OBOE_PCONFIG_BAUDSHIFT;
377 pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
378 break;
379 case 4800:
380 pconfig |= 23 << OBOE_PCONFIG_BAUDSHIFT;
381 pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
382 break;
383 case 9600:
384 pconfig |= 11 << OBOE_PCONFIG_BAUDSHIFT;
385 pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
386 break;
387 case 19200:
388 pconfig |= 5 << OBOE_PCONFIG_BAUDSHIFT;
389 pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
390 break;
391 case 38400:
392 pconfig |= 2 << OBOE_PCONFIG_BAUDSHIFT;
393 pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
394 break;
395 case 57600:
396 pconfig |= 1 << OBOE_PCONFIG_BAUDSHIFT;
397 pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
398 break;
399 case 115200:
400 pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
401 pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
402 break;
403 default:
404 /*Set to packet based reception */
405 OUTB (RX_LEN >> 8, OBOE_MAXLENH);
406 OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
407 break;
410 switch (self->speed)
412 case 2400:
413 case 4800:
414 case 9600:
415 case 19200:
416 case 38400:
417 case 57600:
418 case 115200:
419 config0l = OBOE_CONFIG0L_ENSIR;
420 if (self->async)
422 /*Set to character based reception */
423 /*System will lock if MAXLEN=0 */
424 /*so have to be careful */
425 OUTB (0x01, OBOE_MAXLENH);
426 OUTB (0x01, OBOE_MAXLENL);
427 OUTB (0x00, OBOE_MAXLENH);
429 else
431 /*Set to packet based reception */
432 config0l |= OBOE_CONFIG0L_ENSIRF;
433 OUTB (RX_LEN >> 8, OBOE_MAXLENH);
434 OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
436 break;
438 #ifdef USE_MIR
439 /* MIR mode */
440 /* Set for 16 bit CRC and enable MIR */
441 /* Preamble now handled by the chip */
442 case 1152000:
443 pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
444 pconfig |= 8 << OBOE_PCONFIG_WIDTHSHIFT;
445 pconfig |= 1 << OBOE_PCONFIG_PREAMBLESHIFT;
446 config0l = OBOE_CONFIG0L_CRC16 | OBOE_CONFIG0L_ENMIR;
447 break;
448 #endif
449 /* FIR mode */
450 /* Set for 32 bit CRC and enable FIR */
451 /* Preamble handled by the chip */
452 case 4000000:
453 pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
454 /* Documentation says 14, but toshiba use 15 in their drivers */
455 pconfig |= 15 << OBOE_PCONFIG_PREAMBLESHIFT;
456 config0l = OBOE_CONFIG0L_ENFIR;
457 break;
460 /* Copy into new PHY config buffer */
461 OUTBP (pconfig >> 8, OBOE_NEW_PCONFIGH);
462 OUTB (pconfig & 0xff, OBOE_NEW_PCONFIGL);
463 OUTB (config0l, OBOE_CONFIG0L);
465 /* Now make OBOE copy from new PHY to current PHY */
466 OUTB (0x0, OBOE_ENABLEH);
467 OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
468 PROMPT;
470 /* speed change executed */
471 self->new_speed = 0;
472 self->io.speed = self->speed;
475 /*Let the chip look at memory */
476 static void
477 toshoboe_enablebm (struct toshoboe_cb *self)
479 pci_set_master (self->pdev);
482 /*setup the ring */
483 static void
484 toshoboe_initring (struct toshoboe_cb *self)
486 int i;
488 for (i = 0; i < TX_SLOTS; ++i)
490 self->ring->tx[i].len = 0;
491 self->ring->tx[i].control = 0x00;
492 self->ring->tx[i].address = virt_to_bus (self->tx_bufs[i]);
495 for (i = 0; i < RX_SLOTS; ++i)
497 self->ring->rx[i].len = RX_LEN;
498 self->ring->rx[i].len = 0;
499 self->ring->rx[i].address = virt_to_bus (self->rx_bufs[i]);
500 self->ring->rx[i].control = OBOE_CTL_RX_HW_OWNS;
504 static void
505 toshoboe_resetptrs (struct toshoboe_cb *self)
507 /* Can reset pointers by twidling DMA */
508 OUTB (0x0, OBOE_ENABLEH);
509 OUTBP (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
510 OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
512 self->rxs = inb_p (OBOE_RXSLOT) & OBOE_SLOT_MASK;
513 self->txs = inb_p (OBOE_TXSLOT) & OBOE_SLOT_MASK;
516 /* Called in locked state */
517 static void
518 toshoboe_initptrs (struct toshoboe_cb *self)
521 /* spin_lock_irqsave(self->spinlock, flags); */
522 /* save_flags (flags); */
524 /* Can reset pointers by twidling DMA */
525 toshoboe_resetptrs (self);
527 OUTB (0x0, OBOE_ENABLEH);
528 OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);
529 OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
531 self->txpending = 0;
533 /* spin_unlock_irqrestore(self->spinlock, flags); */
534 /* restore_flags (flags); */
537 /* Wake the chip up and get it looking at the rings */
538 /* Called in locked state */
539 static void
540 toshoboe_startchip (struct toshoboe_cb *self)
542 __u32 physaddr;
544 toshoboe_initring (self);
545 toshoboe_enablebm (self);
546 OUTBP (OBOE_CONFIG1_RESET, OBOE_CONFIG1);
547 OUTBP (OBOE_CONFIG1_ON, OBOE_CONFIG1);
549 /* Stop the clocks */
550 OUTB (0, OBOE_ENABLEH);
552 /*Set size of rings */
553 OUTB (RING_SIZE, OBOE_RING_SIZE);
555 /*Acknoledge any pending interrupts */
556 OUTB (0xff, OBOE_ISR);
558 /*Enable ints */
559 OUTB (OBOE_INT_TXDONE | OBOE_INT_RXDONE |
560 OBOE_INT_TXUNDER | OBOE_INT_RXOVER | OBOE_INT_SIP , OBOE_IER);
562 /*Acknoledge any pending interrupts */
563 OUTB (0xff, OBOE_ISR);
565 /*Set the maximum packet length to 0xfff (4095) */
566 OUTB (RX_LEN >> 8, OBOE_MAXLENH);
567 OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
569 /*Shutdown DMA */
570 OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
572 /*Find out where the rings live */
573 physaddr = virt_to_bus (self->ring);
575 IRDA_ASSERT ((physaddr & 0x3ff) == 0,
576 printk (KERN_ERR DRIVER_NAME "ring not correctly aligned\n");
577 return;);
579 OUTB ((physaddr >> 10) & 0xff, OBOE_RING_BASE0);
580 OUTB ((physaddr >> 18) & 0xff, OBOE_RING_BASE1);
581 OUTB ((physaddr >> 26) & 0x3f, OBOE_RING_BASE2);
583 /*Enable DMA controller in byte mode and RX */
584 OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);
586 /* Start up the clocks */
587 OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
589 /*set to sensible speed */
590 self->speed = 9600;
591 toshoboe_setbaud (self);
592 toshoboe_initptrs (self);
595 static void
596 toshoboe_isntstuck (struct toshoboe_cb *self)
600 static void
601 toshoboe_checkstuck (struct toshoboe_cb *self)
603 unsigned long flags;
605 if (0)
607 spin_lock_irqsave(&self->spinlock, flags);
609 /* This will reset the chip completely */
610 printk (KERN_ERR DRIVER_NAME ": Resetting chip\n");
612 toshoboe_stopchip (self);
613 toshoboe_startchip (self);
614 spin_unlock_irqrestore(&self->spinlock, flags);
618 /*Generate packet of about mtt us long */
619 static int
620 toshoboe_makemttpacket (struct toshoboe_cb *self, void *buf, int mtt)
622 int xbofs;
624 xbofs = ((int) (mtt/100)) * (int) (self->speed);
625 xbofs=xbofs/80000; /*Eight bits per byte, and mtt is in us*/
626 xbofs++;
628 pr_debug(DRIVER_NAME ": generated mtt of %d bytes for %d us at %d baud\n",
629 xbofs, mtt, self->speed);
631 if (xbofs > TX_LEN)
633 printk (KERN_ERR DRIVER_NAME ": wanted %d bytes MTT but TX_LEN is %d\n",
634 xbofs, TX_LEN);
635 xbofs = TX_LEN;
638 /*xbofs will do for SIR, MIR and FIR,SIR mode doesn't generate a checksum anyway */
639 memset (buf, XBOF, xbofs);
641 return xbofs;
644 #ifdef USE_PROBE
645 /***********************************************************************/
646 /* Probe code */
648 static void
649 toshoboe_dumptx (struct toshoboe_cb *self)
651 int i;
652 PROBE_DEBUG(KERN_WARNING "TX:");
653 for (i = 0; i < RX_SLOTS; ++i)
654 PROBE_DEBUG(" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
655 PROBE_DEBUG(" [%d]\n",self->speed);
658 static void
659 toshoboe_dumprx (struct toshoboe_cb *self, int score)
661 int i;
662 PROBE_DEBUG(" %d\nRX:",score);
663 for (i = 0; i < RX_SLOTS; ++i)
664 PROBE_DEBUG(" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
665 PROBE_DEBUG("\n");
668 static inline int
669 stuff_byte (__u8 byte, __u8 * buf)
671 switch (byte)
673 case BOF: /* FALLTHROUGH */
674 case EOF: /* FALLTHROUGH */
675 case CE:
676 /* Insert transparently coded */
677 buf[0] = CE; /* Send link escape */
678 buf[1] = byte ^ IRDA_TRANS; /* Complement bit 5 */
679 return 2;
680 /* break; */
681 default:
682 /* Non-special value, no transparency required */
683 buf[0] = byte;
684 return 1;
685 /* break; */
689 static irqreturn_t
690 toshoboe_probeinterrupt (int irq, void *dev_id)
692 struct toshoboe_cb *self = dev_id;
693 __u8 irqstat;
695 irqstat = INB (OBOE_ISR);
697 /* was it us */
698 if (!(irqstat & OBOE_INT_MASK))
699 return IRQ_NONE;
701 /* Ack all the interrupts */
702 OUTB (irqstat, OBOE_ISR);
704 if (irqstat & OBOE_INT_TXDONE)
706 int txp;
708 self->int_tx++;
709 PROBE_DEBUG("T");
711 txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;
712 if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
714 self->int_tx+=100;
715 PROBE_DEBUG("S");
716 toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
720 if (irqstat & OBOE_INT_RXDONE) {
721 self->int_rx++;
722 PROBE_DEBUG("R"); }
723 if (irqstat & OBOE_INT_TXUNDER) {
724 self->int_txunder++;
725 PROBE_DEBUG("U"); }
726 if (irqstat & OBOE_INT_RXOVER) {
727 self->int_rxover++;
728 PROBE_DEBUG("O"); }
729 if (irqstat & OBOE_INT_SIP) {
730 self->int_sip++;
731 PROBE_DEBUG("I"); }
732 return IRQ_HANDLED;
735 static int
736 toshoboe_maketestpacket (unsigned char *buf, int badcrc, int fir)
738 int i;
739 int len = 0;
740 union
742 __u16 value;
743 __u8 bytes[2];
745 fcs;
747 if (fir)
749 memset (buf, 0, TT_LEN);
750 return TT_LEN;
753 fcs.value = INIT_FCS;
755 memset (buf, XBOF, 10);
756 len += 10;
757 buf[len++] = BOF;
759 for (i = 0; i < TT_LEN; ++i)
761 len += stuff_byte (i, buf + len);
762 fcs.value = irda_fcs (fcs.value, i);
765 len += stuff_byte (fcs.bytes[0] ^ badcrc, buf + len);
766 len += stuff_byte (fcs.bytes[1] ^ badcrc, buf + len);
767 buf[len++] = EOF;
768 len++;
769 return len;
772 static int
773 toshoboe_probefail (struct toshoboe_cb *self, char *msg)
775 printk (KERN_ERR DRIVER_NAME "probe(%d) failed %s\n",self-> speed, msg);
776 toshoboe_dumpregs (self);
777 toshoboe_stopchip (self);
778 free_irq (self->io.irq, (void *) self);
779 return 0;
782 static int
783 toshoboe_numvalidrcvs (struct toshoboe_cb *self)
785 int i, ret = 0;
786 for (i = 0; i < RX_SLOTS; ++i)
787 if ((self->ring->rx[i].control & 0xe0) == 0)
788 ret++;
790 return ret;
793 static int
794 toshoboe_numrcvs (struct toshoboe_cb *self)
796 int i, ret = 0;
797 for (i = 0; i < RX_SLOTS; ++i)
798 if (!(self->ring->rx[i].control & OBOE_CTL_RX_HW_OWNS))
799 ret++;
801 return ret;
804 static int
805 toshoboe_probe (struct toshoboe_cb *self)
807 int i, j, n;
808 #ifdef USE_MIR
809 static const int bauds[] = { 9600, 115200, 4000000, 1152000 };
810 #else
811 static const int bauds[] = { 9600, 115200, 4000000 };
812 #endif
813 unsigned long flags;
815 if (request_irq (self->io.irq, toshoboe_probeinterrupt,
816 self->io.irqflags, "toshoboe", (void *) self))
818 printk (KERN_ERR DRIVER_NAME ": probe failed to allocate irq %d\n",
819 self->io.irq);
820 return 0;
823 /* test 1: SIR filter and back to back */
825 for (j = 0; j < ARRAY_SIZE(bauds); ++j)
827 int fir = (j > 1);
828 toshoboe_stopchip (self);
831 spin_lock_irqsave(&self->spinlock, flags);
832 /*Address is already setup */
833 toshoboe_startchip (self);
834 self->int_rx = self->int_tx = 0;
835 self->speed = bauds[j];
836 toshoboe_setbaud (self);
837 toshoboe_initptrs (self);
838 spin_unlock_irqrestore(&self->spinlock, flags);
840 self->ring->tx[self->txs].control =
841 /* (FIR only) OBOE_CTL_TX_SIP needed for switching to next slot */
842 /* MIR: all received data is stored in one slot */
843 (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
844 : OBOE_CTL_TX_HW_OWNS ;
845 self->ring->tx[self->txs].len =
846 toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
847 self->txs++;
848 self->txs %= TX_SLOTS;
850 self->ring->tx[self->txs].control =
851 (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_SIP
852 : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
853 self->ring->tx[self->txs].len =
854 toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
855 self->txs++;
856 self->txs %= TX_SLOTS;
858 self->ring->tx[self->txs].control =
859 (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
860 : OBOE_CTL_TX_HW_OWNS ;
861 self->ring->tx[self->txs].len =
862 toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
863 self->txs++;
864 self->txs %= TX_SLOTS;
866 self->ring->tx[self->txs].control =
867 (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
868 | OBOE_CTL_TX_SIP | OBOE_CTL_TX_BAD_CRC
869 : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
870 self->ring->tx[self->txs].len =
871 toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
872 self->txs++;
873 self->txs %= TX_SLOTS;
875 toshoboe_dumptx (self);
876 /* Turn on TX and RX and loopback */
877 toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
879 i = 0;
880 n = fir ? 1 : 4;
881 while (toshoboe_numvalidrcvs (self) != n)
883 if (i > 4800)
884 return toshoboe_probefail (self, "filter test");
885 udelay ((9600*(TT_LEN+16))/self->speed);
886 i++;
889 n = fir ? 203 : 102;
890 while ((toshoboe_numrcvs(self) != self->int_rx) || (self->int_tx != n))
892 if (i > 4800)
893 return toshoboe_probefail (self, "interrupt test");
894 udelay ((9600*(TT_LEN+16))/self->speed);
895 i++;
897 toshoboe_dumprx (self,i);
901 /* test 2: SIR in char at a time */
903 toshoboe_stopchip (self);
904 self->int_rx = self->int_tx = 0;
906 spin_lock_irqsave(&self->spinlock, flags);
907 toshoboe_startchip (self);
908 spin_unlock_irqrestore(&self->spinlock, flags);
910 self->async = 1;
911 self->speed = 115200;
912 toshoboe_setbaud (self);
913 self->ring->tx[self->txs].control =
914 OBOE_CTL_TX_RTCENTX | OBOE_CTL_TX_HW_OWNS;
915 self->ring->tx[self->txs].len = 4;
917 ((unsigned char *) self->tx_bufs[self->txs])[0] = 'f';
918 ((unsigned char *) self->tx_bufs[self->txs])[1] = 'i';
919 ((unsigned char *) self->tx_bufs[self->txs])[2] = 's';
920 ((unsigned char *) self->tx_bufs[self->txs])[3] = 'h';
921 toshoboe_dumptx (self);
922 toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
924 i = 0;
925 while (toshoboe_numvalidrcvs (self) != 4)
927 if (i > 100)
928 return toshoboe_probefail (self, "Async test");
929 udelay (100);
930 i++;
933 while ((toshoboe_numrcvs (self) != self->int_rx) || (self->int_tx != 1))
935 if (i > 100)
936 return toshoboe_probefail (self, "Async interrupt test");
937 udelay (100);
938 i++;
940 toshoboe_dumprx (self,i);
942 self->async = 0;
943 self->speed = 9600;
944 toshoboe_setbaud (self);
945 toshoboe_stopchip (self);
947 free_irq (self->io.irq, (void *) self);
949 printk (KERN_WARNING DRIVER_NAME ": Self test passed ok\n");
951 return 1;
953 #endif
955 /******************************************************************/
956 /* Netdev style code */
958 /* Transmit something */
959 static netdev_tx_t
960 toshoboe_hard_xmit (struct sk_buff *skb, struct net_device *dev)
962 struct toshoboe_cb *self;
963 __s32 speed;
964 int mtt, len, ctl;
965 unsigned long flags;
966 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
968 self = netdev_priv(dev);
970 IRDA_ASSERT (self != NULL, return NETDEV_TX_OK; );
972 pr_debug("%s.tx:%x(%x)%x\n",
973 __func__, skb->len, self->txpending, INB(OBOE_ENABLEH));
974 if (!cb->magic) {
975 pr_debug("%s.Not IrLAP:%x\n", __func__, cb->magic);
976 #ifdef DUMP_PACKETS
977 _dumpbufs(skb->data,skb->len,'>');
978 #endif
981 /* change speed pending, wait for its execution */
982 if (self->new_speed)
983 return NETDEV_TX_BUSY;
985 /* device stopped (apm) wait for restart */
986 if (self->stopped)
987 return NETDEV_TX_BUSY;
989 toshoboe_checkstuck (self);
991 /* Check if we need to change the speed */
992 /* But not now. Wait after transmission if mtt not required */
993 speed=irda_get_next_speed(skb);
994 if ((speed != self->io.speed) && (speed != -1))
996 spin_lock_irqsave(&self->spinlock, flags);
998 if (self->txpending || skb->len)
1000 self->new_speed = speed;
1001 pr_debug("%s: Queued TxDone scheduled speed change %d\n" ,
1002 __func__, speed);
1003 /* if no data, that's all! */
1004 if (!skb->len)
1006 spin_unlock_irqrestore(&self->spinlock, flags);
1007 dev_kfree_skb (skb);
1008 return NETDEV_TX_OK;
1010 /* True packet, go on, but */
1011 /* do not accept anything before change speed execution */
1012 netif_stop_queue(dev);
1013 /* ready to process TxDone interrupt */
1014 spin_unlock_irqrestore(&self->spinlock, flags);
1016 else
1018 /* idle and no data, change speed now */
1019 self->speed = speed;
1020 toshoboe_setbaud (self);
1021 spin_unlock_irqrestore(&self->spinlock, flags);
1022 dev_kfree_skb (skb);
1023 return NETDEV_TX_OK;
1028 if ((mtt = irda_get_mtt(skb)))
1030 /* This is fair since the queue should be empty anyway */
1031 spin_lock_irqsave(&self->spinlock, flags);
1033 if (self->txpending)
1035 spin_unlock_irqrestore(&self->spinlock, flags);
1036 return NETDEV_TX_BUSY;
1039 /* If in SIR mode we need to generate a string of XBOFs */
1040 /* In MIR and FIR we need to generate a string of data */
1041 /* which we will add a wrong checksum to */
1043 mtt = toshoboe_makemttpacket (self, self->tx_bufs[self->txs], mtt);
1044 pr_debug("%s.mtt:%x(%x)%d\n", __func__, skb->len, mtt, self->txpending);
1045 if (mtt)
1047 self->ring->tx[self->txs].len = mtt & 0xfff;
1049 ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
1050 if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
1052 ctl |= OBOE_CTL_TX_BAD_CRC | OBOE_CTL_TX_SIP ;
1054 #ifdef USE_MIR
1055 else if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_MIRON)
1057 ctl |= OBOE_CTL_TX_BAD_CRC;
1059 #endif
1060 self->ring->tx[self->txs].control = ctl;
1062 OUTB (0x0, OBOE_ENABLEH);
1063 /* It is only a timer. Do not send mtt packet outside! */
1064 toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
1066 self->txpending++;
1068 self->txs++;
1069 self->txs %= TX_SLOTS;
1072 else
1074 printk(KERN_ERR DRIVER_NAME ": problem with mtt packet - ignored\n");
1076 spin_unlock_irqrestore(&self->spinlock, flags);
1079 #ifdef DUMP_PACKETS
1080 dumpbufs(skb->data,skb->len,'>');
1081 #endif
1083 spin_lock_irqsave(&self->spinlock, flags);
1085 if (self->ring->tx[self->txs].control & OBOE_CTL_TX_HW_OWNS)
1087 pr_debug("%s.ful:%x(%x)%x\n",
1088 __func__, skb->len, self->ring->tx[self->txs].control,
1089 self->txpending);
1090 toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
1091 spin_unlock_irqrestore(&self->spinlock, flags);
1092 return NETDEV_TX_BUSY;
1095 if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_SIRON)
1097 len = async_wrap_skb (skb, self->tx_bufs[self->txs], TX_BUF_SZ);
1099 else
1101 len = skb->len;
1102 skb_copy_from_linear_data(skb, self->tx_bufs[self->txs], len);
1104 self->ring->tx[self->txs].len = len & 0x0fff;
1106 /*Sometimes the HW doesn't see us assert RTCENTX in the interrupt code */
1107 /*later this plays safe, we garuntee the last packet to be transmitted */
1108 /*has RTCENTX set */
1110 ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
1111 if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
1113 ctl |= OBOE_CTL_TX_SIP ;
1115 self->ring->tx[self->txs].control = ctl;
1117 /* If transmitter is idle start in one-shot mode */
1119 if (!self->txpending)
1120 toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
1122 self->txpending++;
1124 self->txs++;
1125 self->txs %= TX_SLOTS;
1127 spin_unlock_irqrestore(&self->spinlock, flags);
1128 dev_kfree_skb (skb);
1130 return NETDEV_TX_OK;
1133 /*interrupt handler */
1134 static irqreturn_t
1135 toshoboe_interrupt (int irq, void *dev_id)
1137 struct toshoboe_cb *self = dev_id;
1138 __u8 irqstat;
1139 struct sk_buff *skb = NULL;
1141 irqstat = INB (OBOE_ISR);
1143 /* was it us */
1144 if (!(irqstat & OBOE_INT_MASK))
1145 return IRQ_NONE;
1147 /* Ack all the interrupts */
1148 OUTB (irqstat, OBOE_ISR);
1150 toshoboe_isntstuck (self);
1152 /* Txdone */
1153 if (irqstat & OBOE_INT_TXDONE)
1155 int txp, txpc;
1156 int i;
1158 txp = self->txpending;
1159 self->txpending = 0;
1161 for (i = 0; i < TX_SLOTS; ++i)
1163 if (self->ring->tx[i].control & OBOE_CTL_TX_HW_OWNS)
1164 self->txpending++;
1166 pr_debug("%s.txd(%x)%x/%x\n", __func__, irqstat, txp, self->txpending);
1168 txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;
1170 /* Got anything queued ? start it together */
1171 if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
1173 txpc = txp;
1174 #ifdef OPTIMIZE_TX
1175 while (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
1177 txp = txpc;
1178 txpc++;
1179 txpc %= TX_SLOTS;
1180 self->netdev->stats.tx_packets++;
1181 if (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
1182 self->ring->tx[txp].control &= ~OBOE_CTL_TX_RTCENTX;
1184 self->netdev->stats.tx_packets--;
1185 #else
1186 self->netdev->stats.tx_packets++;
1187 #endif
1188 toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
1191 if ((!self->txpending) && (self->new_speed))
1193 self->speed = self->new_speed;
1194 pr_debug("%s: Executed TxDone scheduled speed change %d\n",
1195 __func__, self->speed);
1196 toshoboe_setbaud (self);
1199 /* Tell network layer that we want more frames */
1200 if (!self->new_speed)
1201 netif_wake_queue(self->netdev);
1204 if (irqstat & OBOE_INT_RXDONE)
1206 while (!(self->ring->rx[self->rxs].control & OBOE_CTL_RX_HW_OWNS))
1208 int len = self->ring->rx[self->rxs].len;
1209 skb = NULL;
1210 pr_debug("%s.rcv:%x(%x)\n", __func__
1211 , len, self->ring->rx[self->rxs].control);
1213 #ifdef DUMP_PACKETS
1214 dumpbufs(self->rx_bufs[self->rxs],len,'<');
1215 #endif
1217 if (self->ring->rx[self->rxs].control == 0)
1219 __u8 enable = INB (OBOE_ENABLEH);
1221 /* In SIR mode we need to check the CRC as this */
1222 /* hasn't been done by the hardware */
1223 if (enable & OBOE_ENABLEH_SIRON)
1225 if (!toshoboe_checkfcs (self->rx_bufs[self->rxs], len))
1226 len = 0;
1227 /*Trim off the CRC */
1228 if (len > 1)
1229 len -= 2;
1230 else
1231 len = 0;
1232 pr_debug("%s.SIR:%x(%x)\n", __func__, len, enable);
1235 #ifdef USE_MIR
1236 else if (enable & OBOE_ENABLEH_MIRON)
1238 if (len > 1)
1239 len -= 2;
1240 else
1241 len = 0;
1242 pr_debug("%s.MIR:%x(%x)\n", __func__, len, enable);
1244 #endif
1245 else if (enable & OBOE_ENABLEH_FIRON)
1247 if (len > 3)
1248 len -= 4; /*FIXME: check this */
1249 else
1250 len = 0;
1251 pr_debug("%s.FIR:%x(%x)\n", __func__, len, enable);
1253 else
1254 pr_debug("%s.?IR:%x(%x)\n", __func__, len, enable);
1256 if (len)
1258 skb = dev_alloc_skb (len + 1);
1259 if (skb)
1261 skb_reserve (skb, 1);
1263 skb_put (skb, len);
1264 skb_copy_to_linear_data(skb, self->rx_bufs[self->rxs],
1265 len);
1266 self->netdev->stats.rx_packets++;
1267 skb->dev = self->netdev;
1268 skb_reset_mac_header(skb);
1269 skb->protocol = htons (ETH_P_IRDA);
1271 else
1273 printk (KERN_INFO
1274 "%s(), memory squeeze, dropping frame.\n",
1275 __func__);
1279 else
1281 /* TODO: =========================================== */
1282 /* if OBOE_CTL_RX_LENGTH, our buffers are too small */
1283 /* (MIR or FIR) data is lost. */
1284 /* (SIR) data is splitted in several slots. */
1285 /* we have to join all the received buffers received */
1286 /*in a large buffer before checking CRC. */
1287 pr_debug("%s.err:%x(%x)\n", __func__
1288 , len, self->ring->rx[self->rxs].control);
1291 self->ring->rx[self->rxs].len = 0x0;
1292 self->ring->rx[self->rxs].control = OBOE_CTL_RX_HW_OWNS;
1294 self->rxs++;
1295 self->rxs %= RX_SLOTS;
1297 if (skb)
1298 netif_rx (skb);
1303 if (irqstat & OBOE_INT_TXUNDER)
1305 printk (KERN_WARNING DRIVER_NAME ": tx fifo underflow\n");
1307 if (irqstat & OBOE_INT_RXOVER)
1309 printk (KERN_WARNING DRIVER_NAME ": rx fifo overflow\n");
1311 /* This must be useful for something... */
1312 if (irqstat & OBOE_INT_SIP)
1314 self->int_sip++;
1315 pr_debug("%s.sip:%x(%x)%x\n",
1316 __func__, self->int_sip, irqstat, self->txpending);
1318 return IRQ_HANDLED;
1322 static int
1323 toshoboe_net_open (struct net_device *dev)
1325 struct toshoboe_cb *self;
1326 unsigned long flags;
1327 int rc;
1329 self = netdev_priv(dev);
1331 if (self->async)
1332 return -EBUSY;
1334 if (self->stopped)
1335 return 0;
1337 rc = request_irq (self->io.irq, toshoboe_interrupt,
1338 IRQF_SHARED, dev->name, self);
1339 if (rc)
1340 return rc;
1342 spin_lock_irqsave(&self->spinlock, flags);
1343 toshoboe_startchip (self);
1344 spin_unlock_irqrestore(&self->spinlock, flags);
1346 /* Ready to play! */
1347 netif_start_queue(dev);
1350 * Open new IrLAP layer instance, now that everything should be
1351 * initialized properly
1353 self->irlap = irlap_open (dev, &self->qos, driver_name);
1355 self->irdad = 1;
1357 return 0;
1360 static int
1361 toshoboe_net_close (struct net_device *dev)
1363 struct toshoboe_cb *self;
1365 IRDA_ASSERT (dev != NULL, return -1; );
1366 self = netdev_priv(dev);
1368 /* Stop device */
1369 netif_stop_queue(dev);
1371 /* Stop and remove instance of IrLAP */
1372 if (self->irlap)
1373 irlap_close (self->irlap);
1374 self->irlap = NULL;
1376 self->irdad = 0;
1378 free_irq (self->io.irq, (void *) self);
1380 if (!self->stopped)
1382 toshoboe_stopchip (self);
1385 return 0;
1389 * Function toshoboe_net_ioctl (dev, rq, cmd)
1391 * Process IOCTL commands for this device
1394 static int
1395 toshoboe_net_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1397 struct if_irda_req *irq = (struct if_irda_req *) rq;
1398 struct toshoboe_cb *self;
1399 unsigned long flags;
1400 int ret = 0;
1402 IRDA_ASSERT (dev != NULL, return -1; );
1404 self = netdev_priv(dev);
1406 IRDA_ASSERT (self != NULL, return -1; );
1408 pr_debug("%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
1410 /* Disable interrupts & save flags */
1411 spin_lock_irqsave(&self->spinlock, flags);
1413 switch (cmd)
1415 case SIOCSBANDWIDTH: /* Set bandwidth */
1416 /* This function will also be used by IrLAP to change the
1417 * speed, so we still must allow for speed change within
1418 * interrupt context.
1420 pr_debug("%s(BANDWIDTH), %s, (%X/%ld\n",
1421 __func__, dev->name, INB(OBOE_STATUS), irq->ifr_baudrate);
1422 if (!in_interrupt () && !capable (CAP_NET_ADMIN)) {
1423 ret = -EPERM;
1424 goto out;
1427 /* self->speed=irq->ifr_baudrate; */
1428 /* toshoboe_setbaud(self); */
1429 /* Just change speed once - inserted by Paul Bristow */
1430 self->new_speed = irq->ifr_baudrate;
1431 break;
1432 case SIOCSMEDIABUSY: /* Set media busy */
1433 pr_debug("%s(MEDIABUSY), %s, (%X/%x)\n",
1434 __func__, dev->name,
1435 INB(OBOE_STATUS), capable(CAP_NET_ADMIN));
1436 if (!capable (CAP_NET_ADMIN)) {
1437 ret = -EPERM;
1438 goto out;
1440 irda_device_set_media_busy (self->netdev, TRUE);
1441 break;
1442 case SIOCGRECEIVING: /* Check if we are receiving right now */
1443 irq->ifr_receiving = (INB (OBOE_STATUS) & OBOE_STATUS_RXBUSY) ? 1 : 0;
1444 pr_debug("%s(RECEIVING), %s, (%X/%x)\n",
1445 __func__, dev->name, INB(OBOE_STATUS), irq->ifr_receiving);
1446 break;
1447 default:
1448 pr_debug("%s(?), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
1449 ret = -EOPNOTSUPP;
1451 out:
1452 spin_unlock_irqrestore(&self->spinlock, flags);
1453 return ret;
1457 MODULE_DESCRIPTION("Toshiba OBOE IrDA Device Driver");
1458 MODULE_AUTHOR("James McKenzie <james@fishsoup.dhs.org>");
1459 MODULE_LICENSE("GPL");
1461 module_param (max_baud, int, 0);
1462 MODULE_PARM_DESC(max_baud, "Maximum baud rate");
1464 #ifdef USE_PROBE
1465 module_param (do_probe, bool, 0);
1466 MODULE_PARM_DESC(do_probe, "Enable/disable chip probing and self-test");
1467 #endif
1469 static void
1470 toshoboe_close (struct pci_dev *pci_dev)
1472 int i;
1473 struct toshoboe_cb *self = pci_get_drvdata(pci_dev);
1475 IRDA_ASSERT (self != NULL, return; );
1477 if (!self->stopped)
1479 toshoboe_stopchip (self);
1482 release_region (self->io.fir_base, self->io.fir_ext);
1484 for (i = 0; i < TX_SLOTS; ++i)
1486 kfree (self->tx_bufs[i]);
1487 self->tx_bufs[i] = NULL;
1490 for (i = 0; i < RX_SLOTS; ++i)
1492 kfree (self->rx_bufs[i]);
1493 self->rx_bufs[i] = NULL;
1496 unregister_netdev(self->netdev);
1498 kfree (self->ringbuf);
1499 self->ringbuf = NULL;
1500 self->ring = NULL;
1502 free_netdev(self->netdev);
1505 static const struct net_device_ops toshoboe_netdev_ops = {
1506 .ndo_open = toshoboe_net_open,
1507 .ndo_stop = toshoboe_net_close,
1508 .ndo_start_xmit = toshoboe_hard_xmit,
1509 .ndo_do_ioctl = toshoboe_net_ioctl,
1512 static int
1513 toshoboe_open (struct pci_dev *pci_dev, const struct pci_device_id *pdid)
1515 struct toshoboe_cb *self;
1516 struct net_device *dev;
1517 int i = 0;
1518 int ok = 0;
1519 int err;
1521 if ((err=pci_enable_device(pci_dev)))
1522 return err;
1524 dev = alloc_irdadev(sizeof (struct toshoboe_cb));
1525 if (dev == NULL)
1527 printk (KERN_ERR DRIVER_NAME ": can't allocate memory for "
1528 "IrDA control block\n");
1529 return -ENOMEM;
1532 self = netdev_priv(dev);
1533 self->netdev = dev;
1534 self->pdev = pci_dev;
1535 self->base = pci_resource_start(pci_dev,0);
1537 self->io.fir_base = self->base;
1538 self->io.fir_ext = OBOE_IO_EXTENT;
1539 self->io.irq = pci_dev->irq;
1540 self->io.irqflags = IRQF_SHARED;
1542 self->speed = self->io.speed = 9600;
1543 self->async = 0;
1545 /* Lock the port that we need */
1546 if (NULL==request_region (self->io.fir_base, self->io.fir_ext, driver_name))
1548 printk (KERN_ERR DRIVER_NAME ": can't get iobase of 0x%03x\n"
1549 ,self->io.fir_base);
1550 err = -EBUSY;
1551 goto freeself;
1554 spin_lock_init(&self->spinlock);
1556 irda_init_max_qos_capabilies (&self->qos);
1557 self->qos.baud_rate.bits = 0;
1559 if (max_baud >= 2400)
1560 self->qos.baud_rate.bits |= IR_2400;
1561 /*if (max_baud>=4800) idev->qos.baud_rate.bits|=IR_4800; */
1562 if (max_baud >= 9600)
1563 self->qos.baud_rate.bits |= IR_9600;
1564 if (max_baud >= 19200)
1565 self->qos.baud_rate.bits |= IR_19200;
1566 if (max_baud >= 115200)
1567 self->qos.baud_rate.bits |= IR_115200;
1568 #ifdef USE_MIR
1569 if (max_baud >= 1152000)
1571 self->qos.baud_rate.bits |= IR_1152000;
1573 #endif
1574 if (max_baud >= 4000000)
1576 self->qos.baud_rate.bits |= (IR_4000000 << 8);
1579 /*FIXME: work this out... */
1580 self->qos.min_turn_time.bits = 0xff;
1582 irda_qos_bits_to_value (&self->qos);
1584 /* Allocate twice the size to guarantee alignment */
1585 self->ringbuf = kmalloc(OBOE_RING_LEN << 1, GFP_KERNEL);
1586 if (!self->ringbuf)
1588 err = -ENOMEM;
1589 goto freeregion;
1592 #if (BITS_PER_LONG == 64)
1593 #error broken on 64-bit: casts pointer to 32-bit, and then back to pointer.
1594 #endif
1596 /*We need to align the taskfile on a taskfile size boundary */
1598 unsigned long addr;
1600 addr = (__u32) self->ringbuf;
1601 addr &= ~(OBOE_RING_LEN - 1);
1602 addr += OBOE_RING_LEN;
1603 self->ring = (struct OboeRing *) addr;
1606 memset (self->ring, 0, OBOE_RING_LEN);
1607 self->io.mem_base = (__u32) self->ring;
1609 ok = 1;
1610 for (i = 0; i < TX_SLOTS; ++i)
1612 self->tx_bufs[i] = kmalloc (TX_BUF_SZ, GFP_KERNEL);
1613 if (!self->tx_bufs[i])
1614 ok = 0;
1617 for (i = 0; i < RX_SLOTS; ++i)
1619 self->rx_bufs[i] = kmalloc (RX_BUF_SZ, GFP_KERNEL);
1620 if (!self->rx_bufs[i])
1621 ok = 0;
1624 if (!ok)
1626 err = -ENOMEM;
1627 goto freebufs;
1631 #ifdef USE_PROBE
1632 if (do_probe)
1633 if (!toshoboe_probe (self))
1635 err = -ENODEV;
1636 goto freebufs;
1638 #endif
1640 SET_NETDEV_DEV(dev, &pci_dev->dev);
1641 dev->netdev_ops = &toshoboe_netdev_ops;
1643 err = register_netdev(dev);
1644 if (err)
1646 printk (KERN_ERR DRIVER_NAME ": register_netdev() failed\n");
1647 err = -ENOMEM;
1648 goto freebufs;
1650 printk (KERN_INFO "IrDA: Registered device %s\n", dev->name);
1652 pci_set_drvdata(pci_dev,self);
1654 printk (KERN_INFO DRIVER_NAME ": Using multiple tasks\n");
1656 return 0;
1658 freebufs:
1659 for (i = 0; i < TX_SLOTS; ++i)
1660 kfree (self->tx_bufs[i]);
1661 for (i = 0; i < RX_SLOTS; ++i)
1662 kfree (self->rx_bufs[i]);
1663 kfree(self->ringbuf);
1665 freeregion:
1666 release_region (self->io.fir_base, self->io.fir_ext);
1668 freeself:
1669 free_netdev(dev);
1671 return err;
1674 static int
1675 toshoboe_gotosleep (struct pci_dev *pci_dev, pm_message_t crap)
1677 struct toshoboe_cb *self = pci_get_drvdata(pci_dev);
1678 unsigned long flags;
1679 int i = 10;
1681 if (!self || self->stopped)
1682 return 0;
1684 if ((!self->irdad) && (!self->async))
1685 return 0;
1687 /* Flush all packets */
1688 while ((i--) && (self->txpending))
1689 msleep(10);
1691 spin_lock_irqsave(&self->spinlock, flags);
1693 toshoboe_stopchip (self);
1694 self->stopped = 1;
1695 self->txpending = 0;
1697 spin_unlock_irqrestore(&self->spinlock, flags);
1698 return 0;
1701 static int
1702 toshoboe_wakeup (struct pci_dev *pci_dev)
1704 struct toshoboe_cb *self = pci_get_drvdata(pci_dev);
1705 unsigned long flags;
1707 if (!self || !self->stopped)
1708 return 0;
1710 if ((!self->irdad) && (!self->async))
1711 return 0;
1713 spin_lock_irqsave(&self->spinlock, flags);
1715 toshoboe_startchip (self);
1716 self->stopped = 0;
1718 netif_wake_queue(self->netdev);
1719 spin_unlock_irqrestore(&self->spinlock, flags);
1720 return 0;
1723 static struct pci_driver donauboe_pci_driver = {
1724 .name = "donauboe",
1725 .id_table = toshoboe_pci_tbl,
1726 .probe = toshoboe_open,
1727 .remove = toshoboe_close,
1728 .suspend = toshoboe_gotosleep,
1729 .resume = toshoboe_wakeup
1732 module_pci_driver(donauboe_pci_driver);