[PATCH] swsusp: switch pm_message_t to struct
[safe/jmp/linux-2.6] / drivers / net / irda / vlsi_ir.c
1 /*********************************************************************
2  *
3  *      vlsi_ir.c:      VLSI82C147 PCI IrDA controller driver for Linux
4  *
5  *      Copyright (c) 2001-2003 Martin Diehl
6  *
7  *      This program is free software; you can redistribute it and/or 
8  *      modify it under the terms of the GNU General Public License as 
9  *      published by the Free Software Foundation; either version 2 of 
10  *      the License, or (at your option) any later version.
11  *
12  *      This program is distributed in the hope that it will be useful,
13  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *      GNU General Public License for more details.
16  *
17  *      You should have received a copy of the GNU General Public License 
18  *      along with this program; if not, write to the Free Software 
19  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
20  *      MA 02111-1307 USA
21  *
22  ********************************************************************/
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26  
27 #define DRIVER_NAME             "vlsi_ir"
28 #define DRIVER_VERSION          "v0.5"
29 #define DRIVER_DESCRIPTION      "IrDA SIR/MIR/FIR driver for VLSI 82C147"
30 #define DRIVER_AUTHOR           "Martin Diehl <info@mdiehl.de>"
31
32 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
33 MODULE_AUTHOR(DRIVER_AUTHOR);
34 MODULE_LICENSE("GPL");
35
36 /********************************************************/
37
38 #include <linux/kernel.h>
39 #include <linux/init.h>
40 #include <linux/pci.h>
41 #include <linux/slab.h>
42 #include <linux/netdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/delay.h>
45 #include <linux/time.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
48 #include <linux/smp_lock.h>
49 #include <asm/uaccess.h>
50 #include <asm/byteorder.h>
51
52 #include <net/irda/irda.h>
53 #include <net/irda/irda_device.h>
54 #include <net/irda/wrapper.h>
55 #include <net/irda/crc.h>
56
57 #include "vlsi_ir.h"
58
59 /********************************************************/
60
61 static /* const */ char drivername[] = DRIVER_NAME;
62
63 static struct pci_device_id vlsi_irda_table [] = {
64         {
65                 .class =        PCI_CLASS_WIRELESS_IRDA << 8,
66                 .class_mask =   PCI_CLASS_SUBCLASS_MASK << 8, 
67                 .vendor =       PCI_VENDOR_ID_VLSI,
68                 .device =       PCI_DEVICE_ID_VLSI_82C147,
69                 .subvendor =    PCI_ANY_ID,
70                 .subdevice =    PCI_ANY_ID,
71         },
72         { /* all zeroes */ }
73 };
74
75 MODULE_DEVICE_TABLE(pci, vlsi_irda_table);
76
77 /********************************************************/
78
79 /*      clksrc: which clock source to be used
80  *              0: auto - try PLL, fallback to 40MHz XCLK
81  *              1: on-chip 48MHz PLL
82  *              2: external 48MHz XCLK
83  *              3: external 40MHz XCLK (HP OB-800)
84  */
85
86 static int clksrc = 0;                  /* default is 0(auto) */
87 module_param(clksrc, int, 0);
88 MODULE_PARM_DESC(clksrc, "clock input source selection");
89
90 /*      ringsize: size of the tx and rx descriptor rings
91  *              independent for tx and rx
92  *              specify as ringsize=tx[,rx]
93  *              allowed values: 4, 8, 16, 32, 64
94  *              Due to the IrDA 1.x max. allowed window size=7,
95  *              there should be no gain when using rings larger than 8
96  */
97
98 static int ringsize[] = {8,8};          /* default is tx=8 / rx=8 */
99 module_param_array(ringsize, int, NULL, 0);
100 MODULE_PARM_DESC(ringsize, "TX, RX ring descriptor size");
101
102 /*      sirpulse: tuning of the SIR pulse width within IrPHY 1.3 limits
103  *              0: very short, 1.5us (exception: 6us at 2.4 kbaud)
104  *              1: nominal 3/16 bittime width
105  *      note: IrDA compliant peer devices should be happy regardless
106  *              which one is used. Primary goal is to save some power
107  *              on the sender's side - at 9.6kbaud for example the short
108  *              pulse width saves more than 90% of the transmitted IR power.
109  */
110
111 static int sirpulse = 1;                /* default is 3/16 bittime */
112 module_param(sirpulse, int, 0);
113 MODULE_PARM_DESC(sirpulse, "SIR pulse width tuning");
114
115 /*      qos_mtt_bits: encoded min-turn-time value we require the peer device
116  *               to use before transmitting to us. "Type 1" (per-station)
117  *               bitfield according to IrLAP definition (section 6.6.8)
118  *               Don't know which transceiver is used by my OB800 - the
119  *               pretty common HP HDLS-1100 requires 1 msec - so lets use this.
120  */
121
122 static int qos_mtt_bits = 0x07;         /* default is 1 ms or more */
123 module_param(qos_mtt_bits, int, 0);
124 MODULE_PARM_DESC(qos_mtt_bits, "IrLAP bitfield representing min-turn-time");
125
126 /********************************************************/
127
128 static void vlsi_reg_debug(unsigned iobase, const char *s)
129 {
130         int     i;
131
132         printk(KERN_DEBUG "%s: ", s);
133         for (i = 0; i < 0x20; i++)
134                 printk("%02x", (unsigned)inb((iobase+i)));
135         printk("\n");
136 }
137
138 static void vlsi_ring_debug(struct vlsi_ring *r)
139 {
140         struct ring_descr *rd;
141         unsigned i;
142
143         printk(KERN_DEBUG "%s - ring %p / size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
144                 __FUNCTION__, r, r->size, r->mask, r->len, r->dir, r->rd[0].hw);
145         printk(KERN_DEBUG "%s - head = %d / tail = %d\n", __FUNCTION__,
146                 atomic_read(&r->head) & r->mask, atomic_read(&r->tail) & r->mask);
147         for (i = 0; i < r->size; i++) {
148                 rd = &r->rd[i];
149                 printk(KERN_DEBUG "%s - ring descr %u: ", __FUNCTION__, i);
150                 printk("skb=%p data=%p hw=%p\n", rd->skb, rd->buf, rd->hw);
151                 printk(KERN_DEBUG "%s - hw: status=%02x count=%u addr=0x%08x\n",
152                         __FUNCTION__, (unsigned) rd_get_status(rd),
153                         (unsigned) rd_get_count(rd), (unsigned) rd_get_addr(rd));
154         }
155 }
156
157 /********************************************************/
158
159 /* needed regardless of CONFIG_PROC_FS */
160 static struct proc_dir_entry *vlsi_proc_root = NULL;
161
162 #ifdef CONFIG_PROC_FS
163
164 static void vlsi_proc_pdev(struct seq_file *seq, struct pci_dev *pdev)
165 {
166         unsigned iobase = pci_resource_start(pdev, 0);
167         unsigned i;
168
169         seq_printf(seq, "\n%s (vid/did: %04x/%04x)\n",
170                    PCIDEV_NAME(pdev), (int)pdev->vendor, (int)pdev->device);
171         seq_printf(seq, "pci-power-state: %u\n", (unsigned) pdev->current_state);
172         seq_printf(seq, "resources: irq=%u / io=0x%04x / dma_mask=0x%016Lx\n",
173                    pdev->irq, (unsigned)pci_resource_start(pdev, 0), (unsigned long long)pdev->dma_mask);
174         seq_printf(seq, "hw registers: ");
175         for (i = 0; i < 0x20; i++)
176                 seq_printf(seq, "%02x", (unsigned)inb((iobase+i)));
177         seq_printf(seq, "\n");
178 }
179                 
180 static void vlsi_proc_ndev(struct seq_file *seq, struct net_device *ndev)
181 {
182         vlsi_irda_dev_t *idev = ndev->priv;
183         u8 byte;
184         u16 word;
185         unsigned delta1, delta2;
186         struct timeval now;
187         unsigned iobase = ndev->base_addr;
188
189         seq_printf(seq, "\n%s link state: %s / %s / %s / %s\n", ndev->name,
190                 netif_device_present(ndev) ? "attached" : "detached", 
191                 netif_running(ndev) ? "running" : "not running",
192                 netif_carrier_ok(ndev) ? "carrier ok" : "no carrier",
193                 netif_queue_stopped(ndev) ? "queue stopped" : "queue running");
194
195         if (!netif_running(ndev))
196                 return;
197
198         seq_printf(seq, "\nhw-state:\n");
199         pci_read_config_byte(idev->pdev, VLSI_PCI_IRMISC, &byte);
200         seq_printf(seq, "IRMISC:%s%s%s uart%s",
201                 (byte&IRMISC_IRRAIL) ? " irrail" : "",
202                 (byte&IRMISC_IRPD) ? " irpd" : "",
203                 (byte&IRMISC_UARTTST) ? " uarttest" : "",
204                 (byte&IRMISC_UARTEN) ? "@" : " disabled\n");
205         if (byte&IRMISC_UARTEN) {
206                 seq_printf(seq, "0x%s\n",
207                         (byte&2) ? ((byte&1) ? "3e8" : "2e8")
208                                  : ((byte&1) ? "3f8" : "2f8"));
209         }
210         pci_read_config_byte(idev->pdev, VLSI_PCI_CLKCTL, &byte);
211         seq_printf(seq, "CLKCTL: PLL %s%s%s / clock %s / wakeup %s\n",
212                 (byte&CLKCTL_PD_INV) ? "powered" : "down",
213                 (byte&CLKCTL_LOCK) ? " locked" : "",
214                 (byte&CLKCTL_EXTCLK) ? ((byte&CLKCTL_XCKSEL)?" / 40 MHz XCLK":" / 48 MHz XCLK") : "",
215                 (byte&CLKCTL_CLKSTP) ? "stopped" : "running",
216                 (byte&CLKCTL_WAKE) ? "enabled" : "disabled");
217         pci_read_config_byte(idev->pdev, VLSI_PCI_MSTRPAGE, &byte);
218         seq_printf(seq, "MSTRPAGE: 0x%02x\n", (unsigned)byte);
219
220         byte = inb(iobase+VLSI_PIO_IRINTR);
221         seq_printf(seq, "IRINTR:%s%s%s%s%s%s%s%s\n",
222                 (byte&IRINTR_ACTEN) ? " ACTEN" : "",
223                 (byte&IRINTR_RPKTEN) ? " RPKTEN" : "",
224                 (byte&IRINTR_TPKTEN) ? " TPKTEN" : "",
225                 (byte&IRINTR_OE_EN) ? " OE_EN" : "",
226                 (byte&IRINTR_ACTIVITY) ? " ACTIVITY" : "",
227                 (byte&IRINTR_RPKTINT) ? " RPKTINT" : "",
228                 (byte&IRINTR_TPKTINT) ? " TPKTINT" : "",
229                 (byte&IRINTR_OE_INT) ? " OE_INT" : "");
230         word = inw(iobase+VLSI_PIO_RINGPTR);
231         seq_printf(seq, "RINGPTR: rx=%u / tx=%u\n", RINGPTR_GET_RX(word), RINGPTR_GET_TX(word));
232         word = inw(iobase+VLSI_PIO_RINGBASE);
233         seq_printf(seq, "RINGBASE: busmap=0x%08x\n",
234                 ((unsigned)word << 10)|(MSTRPAGE_VALUE<<24));
235         word = inw(iobase+VLSI_PIO_RINGSIZE);
236         seq_printf(seq, "RINGSIZE: rx=%u / tx=%u\n", RINGSIZE_TO_RXSIZE(word),
237                 RINGSIZE_TO_TXSIZE(word));
238
239         word = inw(iobase+VLSI_PIO_IRCFG);
240         seq_printf(seq, "IRCFG:%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
241                 (word&IRCFG_LOOP) ? " LOOP" : "",
242                 (word&IRCFG_ENTX) ? " ENTX" : "",
243                 (word&IRCFG_ENRX) ? " ENRX" : "",
244                 (word&IRCFG_MSTR) ? " MSTR" : "",
245                 (word&IRCFG_RXANY) ? " RXANY" : "",
246                 (word&IRCFG_CRC16) ? " CRC16" : "",
247                 (word&IRCFG_FIR) ? " FIR" : "",
248                 (word&IRCFG_MIR) ? " MIR" : "",
249                 (word&IRCFG_SIR) ? " SIR" : "",
250                 (word&IRCFG_SIRFILT) ? " SIRFILT" : "",
251                 (word&IRCFG_SIRTEST) ? " SIRTEST" : "",
252                 (word&IRCFG_TXPOL) ? " TXPOL" : "",
253                 (word&IRCFG_RXPOL) ? " RXPOL" : "");
254         word = inw(iobase+VLSI_PIO_IRENABLE);
255         seq_printf(seq, "IRENABLE:%s%s%s%s%s%s%s%s\n",
256                 (word&IRENABLE_PHYANDCLOCK) ? " PHYANDCLOCK" : "",
257                 (word&IRENABLE_CFGER) ? " CFGERR" : "",
258                 (word&IRENABLE_FIR_ON) ? " FIR_ON" : "",
259                 (word&IRENABLE_MIR_ON) ? " MIR_ON" : "",
260                 (word&IRENABLE_SIR_ON) ? " SIR_ON" : "",
261                 (word&IRENABLE_ENTXST) ? " ENTXST" : "",
262                 (word&IRENABLE_ENRXST) ? " ENRXST" : "",
263                 (word&IRENABLE_CRC16_ON) ? " CRC16_ON" : "");
264         word = inw(iobase+VLSI_PIO_PHYCTL);
265         seq_printf(seq, "PHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
266                 (unsigned)PHYCTL_TO_BAUD(word),
267                 (unsigned)PHYCTL_TO_PLSWID(word),
268                 (unsigned)PHYCTL_TO_PREAMB(word));
269         word = inw(iobase+VLSI_PIO_NPHYCTL);
270         seq_printf(seq, "NPHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
271                 (unsigned)PHYCTL_TO_BAUD(word),
272                 (unsigned)PHYCTL_TO_PLSWID(word),
273                 (unsigned)PHYCTL_TO_PREAMB(word));
274         word = inw(iobase+VLSI_PIO_MAXPKT);
275         seq_printf(seq, "MAXPKT: max. rx packet size = %u\n", word);
276         word = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
277         seq_printf(seq, "RCVBCNT: rx-fifo filling level = %u\n", word);
278
279         seq_printf(seq, "\nsw-state:\n");
280         seq_printf(seq, "IrPHY setup: %d baud - %s encoding\n", idev->baud, 
281                 (idev->mode==IFF_SIR)?"SIR":((idev->mode==IFF_MIR)?"MIR":"FIR"));
282         do_gettimeofday(&now);
283         if (now.tv_usec >= idev->last_rx.tv_usec) {
284                 delta2 = now.tv_usec - idev->last_rx.tv_usec;
285                 delta1 = 0;
286         }
287         else {
288                 delta2 = 1000000 + now.tv_usec - idev->last_rx.tv_usec;
289                 delta1 = 1;
290         }
291         seq_printf(seq, "last rx: %lu.%06u sec\n",
292                 now.tv_sec - idev->last_rx.tv_sec - delta1, delta2);    
293
294         seq_printf(seq, "RX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu",
295                 idev->stats.rx_packets, idev->stats.rx_bytes, idev->stats.rx_errors,
296                 idev->stats.rx_dropped);
297         seq_printf(seq, " / overrun=%lu / length=%lu / frame=%lu / crc=%lu\n",
298                 idev->stats.rx_over_errors, idev->stats.rx_length_errors,
299                 idev->stats.rx_frame_errors, idev->stats.rx_crc_errors);
300         seq_printf(seq, "TX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu / fifo=%lu\n",
301                 idev->stats.tx_packets, idev->stats.tx_bytes, idev->stats.tx_errors,
302                 idev->stats.tx_dropped, idev->stats.tx_fifo_errors);
303
304 }
305                 
306 static void vlsi_proc_ring(struct seq_file *seq, struct vlsi_ring *r)
307 {
308         struct ring_descr *rd;
309         unsigned i, j;
310         int h, t;
311
312         seq_printf(seq, "size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
313                 r->size, r->mask, r->len, r->dir, r->rd[0].hw);
314         h = atomic_read(&r->head) & r->mask;
315         t = atomic_read(&r->tail) & r->mask;
316         seq_printf(seq, "head = %d / tail = %d ", h, t);
317         if (h == t)
318                 seq_printf(seq, "(empty)\n");
319         else {
320                 if (((t+1)&r->mask) == h)
321                         seq_printf(seq, "(full)\n");
322                 else
323                         seq_printf(seq, "(level = %d)\n", ((unsigned)(t-h) & r->mask)); 
324                 rd = &r->rd[h];
325                 j = (unsigned) rd_get_count(rd);
326                 seq_printf(seq, "current: rd = %d / status = %02x / len = %u\n",
327                                 h, (unsigned)rd_get_status(rd), j);
328                 if (j > 0) {
329                         seq_printf(seq, "   data:");
330                         if (j > 20)
331                                 j = 20;
332                         for (i = 0; i < j; i++)
333                                 seq_printf(seq, " %02x", (unsigned)((unsigned char *)rd->buf)[i]);
334                         seq_printf(seq, "\n");
335                 }
336         }
337         for (i = 0; i < r->size; i++) {
338                 rd = &r->rd[i];
339                 seq_printf(seq, "> ring descr %u: ", i);
340                 seq_printf(seq, "skb=%p data=%p hw=%p\n", rd->skb, rd->buf, rd->hw);
341                 seq_printf(seq, "  hw: status=%02x count=%u busaddr=0x%08x\n",
342                         (unsigned) rd_get_status(rd),
343                         (unsigned) rd_get_count(rd), (unsigned) rd_get_addr(rd));
344         }
345 }
346
347 static int vlsi_seq_show(struct seq_file *seq, void *v)
348 {
349         struct net_device *ndev = seq->private;
350         vlsi_irda_dev_t *idev = ndev->priv;
351         unsigned long flags;
352
353         seq_printf(seq, "\n%s %s\n\n", DRIVER_NAME, DRIVER_VERSION);
354         seq_printf(seq, "clksrc: %s\n", 
355                 (clksrc>=2) ? ((clksrc==3)?"40MHz XCLK":"48MHz XCLK")
356                             : ((clksrc==1)?"48MHz PLL":"autodetect"));
357         seq_printf(seq, "ringsize: tx=%d / rx=%d\n",
358                 ringsize[0], ringsize[1]);
359         seq_printf(seq, "sirpulse: %s\n", (sirpulse)?"3/16 bittime":"short");
360         seq_printf(seq, "qos_mtt_bits: 0x%02x\n", (unsigned)qos_mtt_bits);
361
362         spin_lock_irqsave(&idev->lock, flags);
363         if (idev->pdev != NULL) {
364                 vlsi_proc_pdev(seq, idev->pdev);
365
366                 if (idev->pdev->current_state == 0)
367                         vlsi_proc_ndev(seq, ndev);
368                 else
369                         seq_printf(seq, "\nPCI controller down - resume_ok = %d\n",
370                                 idev->resume_ok);
371                 if (netif_running(ndev) && idev->rx_ring && idev->tx_ring) {
372                         seq_printf(seq, "\n--------- RX ring -----------\n\n");
373                         vlsi_proc_ring(seq, idev->rx_ring);
374                         seq_printf(seq, "\n--------- TX ring -----------\n\n");
375                         vlsi_proc_ring(seq, idev->tx_ring);
376                 }
377         }
378         seq_printf(seq, "\n");
379         spin_unlock_irqrestore(&idev->lock, flags);
380
381         return 0;
382 }
383
384 static int vlsi_seq_open(struct inode *inode, struct file *file)
385 {
386         return single_open(file, vlsi_seq_show, PDE(inode)->data);
387 }
388
389 static struct file_operations vlsi_proc_fops = {
390         .owner   = THIS_MODULE,
391         .open    = vlsi_seq_open,
392         .read    = seq_read,
393         .llseek  = seq_lseek,
394         .release = single_release,
395 };
396
397 #define VLSI_PROC_FOPS          (&vlsi_proc_fops)
398
399 #else   /* CONFIG_PROC_FS */
400 #define VLSI_PROC_FOPS          NULL
401 #endif
402
403 /********************************************************/
404
405 static struct vlsi_ring *vlsi_alloc_ring(struct pci_dev *pdev, struct ring_descr_hw *hwmap,
406                                                 unsigned size, unsigned len, int dir)
407 {
408         struct vlsi_ring *r;
409         struct ring_descr *rd;
410         unsigned        i, j;
411         dma_addr_t      busaddr;
412
413         if (!size  ||  ((size-1)&size)!=0)      /* must be >0 and power of 2 */
414                 return NULL;
415
416         r = kmalloc(sizeof(*r) + size * sizeof(struct ring_descr), GFP_KERNEL);
417         if (!r)
418                 return NULL;
419         memset(r, 0, sizeof(*r));
420
421         r->pdev = pdev;
422         r->dir = dir;
423         r->len = len;
424         r->rd = (struct ring_descr *)(r+1);
425         r->mask = size - 1;
426         r->size = size;
427         atomic_set(&r->head, 0);
428         atomic_set(&r->tail, 0);
429
430         for (i = 0; i < size; i++) {
431                 rd = r->rd + i;
432                 memset(rd, 0, sizeof(*rd));
433                 rd->hw = hwmap + i;
434                 rd->buf = kmalloc(len, GFP_KERNEL|GFP_DMA);
435                 if (rd->buf == NULL
436                     ||  !(busaddr = pci_map_single(pdev, rd->buf, len, dir))) {
437                         if (rd->buf) {
438                                 IRDA_ERROR("%s: failed to create PCI-MAP for %p",
439                                            __FUNCTION__, rd->buf);
440                                 kfree(rd->buf);
441                                 rd->buf = NULL;
442                         }
443                         for (j = 0; j < i; j++) {
444                                 rd = r->rd + j;
445                                 busaddr = rd_get_addr(rd);
446                                 rd_set_addr_status(rd, 0, 0);
447                                 if (busaddr)
448                                         pci_unmap_single(pdev, busaddr, len, dir);
449                                 kfree(rd->buf);
450                                 rd->buf = NULL;
451                         }
452                         kfree(r);
453                         return NULL;
454                 }
455                 rd_set_addr_status(rd, busaddr, 0);
456                 /* initially, the dma buffer is owned by the CPU */
457                 rd->skb = NULL;
458         }
459         return r;
460 }
461
462 static int vlsi_free_ring(struct vlsi_ring *r)
463 {
464         struct ring_descr *rd;
465         unsigned        i;
466         dma_addr_t      busaddr;
467
468         for (i = 0; i < r->size; i++) {
469                 rd = r->rd + i;
470                 if (rd->skb)
471                         dev_kfree_skb_any(rd->skb);
472                 busaddr = rd_get_addr(rd);
473                 rd_set_addr_status(rd, 0, 0);
474                 if (busaddr)
475                         pci_unmap_single(r->pdev, busaddr, r->len, r->dir);
476                 if (rd->buf)
477                         kfree(rd->buf);
478         }
479         kfree(r);
480         return 0;
481 }
482
483 static int vlsi_create_hwif(vlsi_irda_dev_t *idev)
484 {
485         char                    *ringarea;
486         struct ring_descr_hw    *hwmap;
487
488         idev->virtaddr = NULL;
489         idev->busaddr = 0;
490
491         ringarea = pci_alloc_consistent(idev->pdev, HW_RING_AREA_SIZE, &idev->busaddr);
492         if (!ringarea) {
493                 IRDA_ERROR("%s: insufficient memory for descriptor rings\n",
494                            __FUNCTION__);
495                 goto out;
496         }
497         memset(ringarea, 0, HW_RING_AREA_SIZE);
498
499         hwmap = (struct ring_descr_hw *)ringarea;
500         idev->rx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[1],
501                                         XFER_BUF_SIZE, PCI_DMA_FROMDEVICE);
502         if (idev->rx_ring == NULL)
503                 goto out_unmap;
504
505         hwmap += MAX_RING_DESCR;
506         idev->tx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[0],
507                                         XFER_BUF_SIZE, PCI_DMA_TODEVICE);
508         if (idev->tx_ring == NULL)
509                 goto out_free_rx;
510
511         idev->virtaddr = ringarea;
512         return 0;
513
514 out_free_rx:
515         vlsi_free_ring(idev->rx_ring);
516 out_unmap:
517         idev->rx_ring = idev->tx_ring = NULL;
518         pci_free_consistent(idev->pdev, HW_RING_AREA_SIZE, ringarea, idev->busaddr);
519         idev->busaddr = 0;
520 out:
521         return -ENOMEM;
522 }
523
524 static int vlsi_destroy_hwif(vlsi_irda_dev_t *idev)
525 {
526         vlsi_free_ring(idev->rx_ring);
527         vlsi_free_ring(idev->tx_ring);
528         idev->rx_ring = idev->tx_ring = NULL;
529
530         if (idev->busaddr)
531                 pci_free_consistent(idev->pdev,HW_RING_AREA_SIZE,idev->virtaddr,idev->busaddr);
532
533         idev->virtaddr = NULL;
534         idev->busaddr = 0;
535
536         return 0;
537 }
538
539 /********************************************************/
540
541 static int vlsi_process_rx(struct vlsi_ring *r, struct ring_descr *rd)
542 {
543         u16             status;
544         int             crclen, len = 0;
545         struct sk_buff  *skb;
546         int             ret = 0;
547         struct net_device *ndev = (struct net_device *)pci_get_drvdata(r->pdev);
548         vlsi_irda_dev_t *idev = ndev->priv;
549
550         pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
551         /* dma buffer now owned by the CPU */
552         status = rd_get_status(rd);
553         if (status & RD_RX_ERROR) {
554                 if (status & RD_RX_OVER)  
555                         ret |= VLSI_RX_OVER;
556                 if (status & RD_RX_LENGTH)  
557                         ret |= VLSI_RX_LENGTH;
558                 if (status & RD_RX_PHYERR)  
559                         ret |= VLSI_RX_FRAME;
560                 if (status & RD_RX_CRCERR)  
561                         ret |= VLSI_RX_CRC;
562                 goto done;
563         }
564
565         len = rd_get_count(rd);
566         crclen = (idev->mode==IFF_FIR) ? sizeof(u32) : sizeof(u16);
567         len -= crclen;          /* remove trailing CRC */
568         if (len <= 0) {
569                 IRDA_DEBUG(0, "%s: strange frame (len=%d)\n", __FUNCTION__, len);
570                 ret |= VLSI_RX_DROP;
571                 goto done;
572         }
573
574         if (idev->mode == IFF_SIR) {    /* hw checks CRC in MIR, FIR mode */
575
576                 /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
577                  * endian-adjustment there just in place will dirty a cache line
578                  * which belongs to the map and thus we must be sure it will
579                  * get flushed before giving the buffer back to hardware.
580                  * vlsi_fill_rx() will do this anyway - but here we rely on.
581                  */
582                 le16_to_cpus(rd->buf+len);
583                 if (irda_calc_crc16(INIT_FCS,rd->buf,len+crclen) != GOOD_FCS) {
584                         IRDA_DEBUG(0, "%s: crc error\n", __FUNCTION__);
585                         ret |= VLSI_RX_CRC;
586                         goto done;
587                 }
588         }
589
590         if (!rd->skb) {
591                 IRDA_WARNING("%s: rx packet lost\n", __FUNCTION__);
592                 ret |= VLSI_RX_DROP;
593                 goto done;
594         }
595
596         skb = rd->skb;
597         rd->skb = NULL;
598         skb->dev = ndev;
599         memcpy(skb_put(skb,len), rd->buf, len);
600         skb->mac.raw = skb->data;
601         if (in_interrupt())
602                 netif_rx(skb);
603         else
604                 netif_rx_ni(skb);
605         ndev->last_rx = jiffies;
606
607 done:
608         rd_set_status(rd, 0);
609         rd_set_count(rd, 0);
610         /* buffer still owned by CPU */
611
612         return (ret) ? -ret : len;
613 }
614
615 static void vlsi_fill_rx(struct vlsi_ring *r)
616 {
617         struct ring_descr *rd;
618
619         for (rd = ring_last(r); rd != NULL; rd = ring_put(r)) {
620                 if (rd_is_active(rd)) {
621                         IRDA_WARNING("%s: driver bug: rx descr race with hw\n",
622                                      __FUNCTION__);
623                         vlsi_ring_debug(r);
624                         break;
625                 }
626                 if (!rd->skb) {
627                         rd->skb = dev_alloc_skb(IRLAP_SKB_ALLOCSIZE);
628                         if (rd->skb) {
629                                 skb_reserve(rd->skb,1);
630                                 rd->skb->protocol = htons(ETH_P_IRDA);
631                         }
632                         else
633                                 break;  /* probably not worth logging? */
634                 }
635                 /* give dma buffer back to busmaster */
636                 pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
637                 rd_activate(rd);
638         }
639 }
640
641 static void vlsi_rx_interrupt(struct net_device *ndev)
642 {
643         vlsi_irda_dev_t *idev = ndev->priv;
644         struct vlsi_ring *r = idev->rx_ring;
645         struct ring_descr *rd;
646         int ret;
647
648         for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
649
650                 if (rd_is_active(rd))
651                         break;
652
653                 ret = vlsi_process_rx(r, rd);
654
655                 if (ret < 0) {
656                         ret = -ret;
657                         idev->stats.rx_errors++;
658                         if (ret & VLSI_RX_DROP)  
659                                 idev->stats.rx_dropped++;
660                         if (ret & VLSI_RX_OVER)  
661                                 idev->stats.rx_over_errors++;
662                         if (ret & VLSI_RX_LENGTH)  
663                                 idev->stats.rx_length_errors++;
664                         if (ret & VLSI_RX_FRAME)  
665                                 idev->stats.rx_frame_errors++;
666                         if (ret & VLSI_RX_CRC)  
667                                 idev->stats.rx_crc_errors++;
668                 }
669                 else if (ret > 0) {
670                         idev->stats.rx_packets++;
671                         idev->stats.rx_bytes += ret;
672                 }
673         }
674
675         do_gettimeofday(&idev->last_rx); /* remember "now" for later mtt delay */
676
677         vlsi_fill_rx(r);
678
679         if (ring_first(r) == NULL) {
680                 /* we are in big trouble, if this should ever happen */
681                 IRDA_ERROR("%s: rx ring exhausted!\n", __FUNCTION__);
682                 vlsi_ring_debug(r);
683         }
684         else
685                 outw(0, ndev->base_addr+VLSI_PIO_PROMPT);
686 }
687
688 /* caller must have stopped the controller from busmastering */
689
690 static void vlsi_unarm_rx(vlsi_irda_dev_t *idev)
691 {
692         struct vlsi_ring *r = idev->rx_ring;
693         struct ring_descr *rd;
694         int ret;
695
696         for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
697
698                 ret = 0;
699                 if (rd_is_active(rd)) {
700                         rd_set_status(rd, 0);
701                         if (rd_get_count(rd)) {
702                                 IRDA_DEBUG(0, "%s - dropping rx packet\n", __FUNCTION__);
703                                 ret = -VLSI_RX_DROP;
704                         }
705                         rd_set_count(rd, 0);
706                         pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
707                         if (rd->skb) {
708                                 dev_kfree_skb_any(rd->skb);
709                                 rd->skb = NULL;
710                         }
711                 }
712                 else
713                         ret = vlsi_process_rx(r, rd);
714
715                 if (ret < 0) {
716                         ret = -ret;
717                         idev->stats.rx_errors++;
718                         if (ret & VLSI_RX_DROP)  
719                                 idev->stats.rx_dropped++;
720                         if (ret & VLSI_RX_OVER)  
721                                 idev->stats.rx_over_errors++;
722                         if (ret & VLSI_RX_LENGTH)  
723                                 idev->stats.rx_length_errors++;
724                         if (ret & VLSI_RX_FRAME)  
725                                 idev->stats.rx_frame_errors++;
726                         if (ret & VLSI_RX_CRC)  
727                                 idev->stats.rx_crc_errors++;
728                 }
729                 else if (ret > 0) {
730                         idev->stats.rx_packets++;
731                         idev->stats.rx_bytes += ret;
732                 }
733         }
734 }
735
736 /********************************************************/
737
738 static int vlsi_process_tx(struct vlsi_ring *r, struct ring_descr *rd)
739 {
740         u16             status;
741         int             len;
742         int             ret;
743
744         pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
745         /* dma buffer now owned by the CPU */
746         status = rd_get_status(rd);
747         if (status & RD_TX_UNDRN)
748                 ret = VLSI_TX_FIFO;
749         else
750                 ret = 0;
751         rd_set_status(rd, 0);
752
753         if (rd->skb) {
754                 len = rd->skb->len;
755                 dev_kfree_skb_any(rd->skb);
756                 rd->skb = NULL;
757         }
758         else    /* tx-skb already freed? - should never happen */
759                 len = rd_get_count(rd);         /* incorrect for SIR! (due to wrapping) */
760
761         rd_set_count(rd, 0);
762         /* dma buffer still owned by the CPU */
763
764         return (ret) ? -ret : len;
765 }
766
767 static int vlsi_set_baud(vlsi_irda_dev_t *idev, unsigned iobase)
768 {
769         u16 nphyctl;
770         u16 config;
771         unsigned mode;
772         int     ret;
773         int     baudrate;
774         int     fifocnt;
775
776         baudrate = idev->new_baud;
777         IRDA_DEBUG(2, "%s: %d -> %d\n", __FUNCTION__, idev->baud, idev->new_baud);
778         if (baudrate == 4000000) {
779                 mode = IFF_FIR;
780                 config = IRCFG_FIR;
781                 nphyctl = PHYCTL_FIR;
782         }
783         else if (baudrate == 1152000) {
784                 mode = IFF_MIR;
785                 config = IRCFG_MIR | IRCFG_CRC16;
786                 nphyctl = PHYCTL_MIR(clksrc==3);
787         }
788         else {
789                 mode = IFF_SIR;
790                 config = IRCFG_SIR | IRCFG_SIRFILT  | IRCFG_RXANY;
791                 switch(baudrate) {
792                         default:
793                                 IRDA_WARNING("%s: undefined baudrate %d - fallback to 9600!\n",
794                                              __FUNCTION__, baudrate);
795                                 baudrate = 9600;
796                                 /* fallthru */
797                         case 2400:
798                         case 9600:
799                         case 19200:
800                         case 38400:
801                         case 57600:
802                         case 115200:
803                                 nphyctl = PHYCTL_SIR(baudrate,sirpulse,clksrc==3);
804                                 break;
805                 }
806         }
807         config |= IRCFG_MSTR | IRCFG_ENRX;
808
809         fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
810         if (fifocnt != 0) {
811                 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
812         }
813
814         outw(0, iobase+VLSI_PIO_IRENABLE);
815         outw(config, iobase+VLSI_PIO_IRCFG);
816         outw(nphyctl, iobase+VLSI_PIO_NPHYCTL);
817         wmb();
818         outw(IRENABLE_PHYANDCLOCK, iobase+VLSI_PIO_IRENABLE);
819         mb();
820
821         udelay(1);      /* chip applies IRCFG on next rising edge of its 8MHz clock */
822
823         /* read back settings for validation */
824
825         config = inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_MASK;
826
827         if (mode == IFF_FIR)
828                 config ^= IRENABLE_FIR_ON;
829         else if (mode == IFF_MIR)
830                 config ^= (IRENABLE_MIR_ON|IRENABLE_CRC16_ON);
831         else
832                 config ^= IRENABLE_SIR_ON;
833
834         if (config != (IRENABLE_PHYANDCLOCK|IRENABLE_ENRXST)) {
835                 IRDA_WARNING("%s: failed to set %s mode!\n", __FUNCTION__,
836                         (mode==IFF_SIR)?"SIR":((mode==IFF_MIR)?"MIR":"FIR"));
837                 ret = -1;
838         }
839         else {
840                 if (inw(iobase+VLSI_PIO_PHYCTL) != nphyctl) {
841                         IRDA_WARNING("%s: failed to apply baudrate %d\n",
842                                      __FUNCTION__, baudrate);
843                         ret = -1;
844                 }
845                 else {
846                         idev->mode = mode;
847                         idev->baud = baudrate;
848                         idev->new_baud = 0;
849                         ret = 0;
850                 }
851         }
852
853         if (ret)
854                 vlsi_reg_debug(iobase,__FUNCTION__);
855
856         return ret;
857 }
858
859 static int vlsi_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
860 {
861         vlsi_irda_dev_t *idev = ndev->priv;
862         struct vlsi_ring        *r = idev->tx_ring;
863         struct ring_descr *rd;
864         unsigned long flags;
865         unsigned iobase = ndev->base_addr;
866         u8 status;
867         u16 config;
868         int mtt;
869         int len, speed;
870         struct timeval  now, ready;
871         char *msg = NULL;
872
873         speed = irda_get_next_speed(skb);
874         spin_lock_irqsave(&idev->lock, flags);
875         if (speed != -1  &&  speed != idev->baud) {
876                 netif_stop_queue(ndev);
877                 idev->new_baud = speed;
878                 status = RD_TX_CLRENTX;  /* stop tx-ring after this frame */
879         }
880         else
881                 status = 0;
882
883         if (skb->len == 0) {
884                 /* handle zero packets - should be speed change */
885                 if (status == 0) {
886                         msg = "bogus zero-length packet";
887                         goto drop_unlock;
888                 }
889
890                 /* due to the completely asynch tx operation we might have
891                  * IrLAP racing with the hardware here, f.e. if the controller
892                  * is just sending the last packet with current speed while
893                  * the LAP is already switching the speed using synchronous
894                  * len=0 packet. Immediate execution would lead to hw lockup
895                  * requiring a powercycle to reset. Good candidate to trigger
896                  * this is the final UA:RSP packet after receiving a DISC:CMD
897                  * when getting the LAP down.
898                  * Note that we are not protected by the queue_stop approach
899                  * because the final UA:RSP arrives _without_ request to apply
900                  * new-speed-after-this-packet - hence the driver doesn't know
901                  * this was the last packet and doesn't stop the queue. So the
902                  * forced switch to default speed from LAP gets through as fast
903                  * as only some 10 usec later while the UA:RSP is still processed
904                  * by the hardware and we would get screwed.
905                  */
906
907                 if (ring_first(idev->tx_ring) == NULL) {
908                         /* no race - tx-ring already empty */
909                         vlsi_set_baud(idev, iobase);
910                         netif_wake_queue(ndev);
911                 }
912                 else
913                         ;
914                         /* keep the speed change pending like it would
915                          * for any len>0 packet. tx completion interrupt
916                          * will apply it when the tx ring becomes empty.
917                          */
918                 spin_unlock_irqrestore(&idev->lock, flags);
919                 dev_kfree_skb_any(skb);
920                 return 0;
921         }
922
923         /* sanity checks - simply drop the packet */
924
925         rd = ring_last(r);
926         if (!rd) {
927                 msg = "ring full, but queue wasn't stopped";
928                 goto drop_unlock;
929         }
930
931         if (rd_is_active(rd)) {
932                 msg = "entry still owned by hw";
933                 goto drop_unlock;
934         }
935
936         if (!rd->buf) {
937                 msg = "tx ring entry without pci buffer";
938                 goto drop_unlock;
939         }
940
941         if (rd->skb) {
942                 msg = "ring entry with old skb still attached";
943                 goto drop_unlock;
944         }
945
946         /* no need for serialization or interrupt disable during mtt */
947         spin_unlock_irqrestore(&idev->lock, flags);
948
949         if ((mtt = irda_get_mtt(skb)) > 0) {
950         
951                 ready.tv_usec = idev->last_rx.tv_usec + mtt;
952                 ready.tv_sec = idev->last_rx.tv_sec;
953                 if (ready.tv_usec >= 1000000) {
954                         ready.tv_usec -= 1000000;
955                         ready.tv_sec++;         /* IrLAP 1.1: mtt always < 1 sec */
956                 }
957                 for(;;) {
958                         do_gettimeofday(&now);
959                         if (now.tv_sec > ready.tv_sec
960                             ||  (now.tv_sec==ready.tv_sec && now.tv_usec>=ready.tv_usec))
961                                 break;
962                         udelay(100);
963                         /* must not sleep here - we are called under xmit_lock! */
964                 }
965         }
966
967         /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
968          * after subsequent tx-completion
969          */
970
971         if (idev->mode == IFF_SIR) {
972                 status |= RD_TX_DISCRC;         /* no hw-crc creation */
973                 len = async_wrap_skb(skb, rd->buf, r->len);
974
975                 /* Some rare worst case situation in SIR mode might lead to
976                  * potential buffer overflow. The wrapper detects this, returns
977                  * with a shortened frame (without FCS/EOF) but doesn't provide
978                  * any error indication about the invalid packet which we are
979                  * going to transmit.
980                  * Therefore we log if the buffer got filled to the point, where the
981                  * wrapper would abort, i.e. when there are less than 5 bytes left to
982                  * allow appending the FCS/EOF.
983                  */
984
985                 if (len >= r->len-5)
986                          IRDA_WARNING("%s: possible buffer overflow with SIR wrapping!\n",
987                                       __FUNCTION__);
988         }
989         else {
990                 /* hw deals with MIR/FIR mode wrapping */
991                 status |= RD_TX_PULSE;          /* send 2 us highspeed indication pulse */
992                 len = skb->len;
993                 if (len > r->len) {
994                         msg = "frame exceeds tx buffer length";
995                         goto drop;
996                 }
997                 else
998                         memcpy(rd->buf, skb->data, len);
999         }
1000
1001         rd->skb = skb;                  /* remember skb for tx-complete stats */
1002
1003         rd_set_count(rd, len);
1004         rd_set_status(rd, status);      /* not yet active! */
1005
1006         /* give dma buffer back to busmaster-hw (flush caches to make
1007          * CPU-driven changes visible from the pci bus).
1008          */
1009
1010         pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
1011
1012 /*      Switching to TX mode here races with the controller
1013  *      which may stop TX at any time when fetching an inactive descriptor
1014  *      or one with CLR_ENTX set. So we switch on TX only, if TX was not running
1015  *      _after_ the new descriptor was activated on the ring. This ensures
1016  *      we will either find TX already stopped or we can be sure, there
1017  *      will be a TX-complete interrupt even if the chip stopped doing
1018  *      TX just after we found it still running. The ISR will then find
1019  *      the non-empty ring and restart TX processing. The enclosing
1020  *      spinlock provides the correct serialization to prevent race with isr.
1021  */
1022
1023         spin_lock_irqsave(&idev->lock,flags);
1024
1025         rd_activate(rd);
1026
1027         if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1028                 int fifocnt;
1029
1030                 fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1031                 if (fifocnt != 0) {
1032                         IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
1033                 }
1034
1035                 config = inw(iobase+VLSI_PIO_IRCFG);
1036                 mb();
1037                 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1038                 wmb();
1039                 outw(0, iobase+VLSI_PIO_PROMPT);
1040         }
1041         ndev->trans_start = jiffies;
1042
1043         if (ring_put(r) == NULL) {
1044                 netif_stop_queue(ndev);
1045                 IRDA_DEBUG(3, "%s: tx ring full - queue stopped\n", __FUNCTION__);
1046         }
1047         spin_unlock_irqrestore(&idev->lock, flags);
1048
1049         return 0;
1050
1051 drop_unlock:
1052         spin_unlock_irqrestore(&idev->lock, flags);
1053 drop:
1054         IRDA_WARNING("%s: dropping packet - %s\n", __FUNCTION__, msg);
1055         dev_kfree_skb_any(skb);
1056         idev->stats.tx_errors++;
1057         idev->stats.tx_dropped++;
1058         /* Don't even think about returning NET_XMIT_DROP (=1) here!
1059          * In fact any retval!=0 causes the packet scheduler to requeue the
1060          * packet for later retry of transmission - which isn't exactly
1061          * what we want after we've just called dev_kfree_skb_any ;-)
1062          */
1063         return 0;
1064 }
1065
1066 static void vlsi_tx_interrupt(struct net_device *ndev)
1067 {
1068         vlsi_irda_dev_t *idev = ndev->priv;
1069         struct vlsi_ring        *r = idev->tx_ring;
1070         struct ring_descr       *rd;
1071         unsigned        iobase;
1072         int     ret;
1073         u16     config;
1074
1075         for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1076
1077                 if (rd_is_active(rd))
1078                         break;
1079
1080                 ret = vlsi_process_tx(r, rd);
1081
1082                 if (ret < 0) {
1083                         ret = -ret;
1084                         idev->stats.tx_errors++;
1085                         if (ret & VLSI_TX_DROP)
1086                                 idev->stats.tx_dropped++;
1087                         if (ret & VLSI_TX_FIFO)
1088                                 idev->stats.tx_fifo_errors++;
1089                 }
1090                 else if (ret > 0){
1091                         idev->stats.tx_packets++;
1092                         idev->stats.tx_bytes += ret;
1093                 }
1094         }
1095
1096         iobase = ndev->base_addr;
1097
1098         if (idev->new_baud  &&  rd == NULL)     /* tx ring empty and speed change pending */
1099                 vlsi_set_baud(idev, iobase);
1100
1101         config = inw(iobase+VLSI_PIO_IRCFG);
1102         if (rd == NULL)                 /* tx ring empty: re-enable rx */
1103                 outw((config & ~IRCFG_ENTX) | IRCFG_ENRX, iobase+VLSI_PIO_IRCFG);
1104
1105         else if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1106                 int fifocnt;
1107
1108                 fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1109                 if (fifocnt != 0) {
1110                         IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n",
1111                                 __FUNCTION__, fifocnt);
1112                 }
1113                 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1114         }
1115
1116         outw(0, iobase+VLSI_PIO_PROMPT);
1117
1118         if (netif_queue_stopped(ndev)  &&  !idev->new_baud) {
1119                 netif_wake_queue(ndev);
1120                 IRDA_DEBUG(3, "%s: queue awoken\n", __FUNCTION__);
1121         }
1122 }
1123
1124 /* caller must have stopped the controller from busmastering */
1125
1126 static void vlsi_unarm_tx(vlsi_irda_dev_t *idev)
1127 {
1128         struct vlsi_ring *r = idev->tx_ring;
1129         struct ring_descr *rd;
1130         int ret;
1131
1132         for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1133
1134                 ret = 0;
1135                 if (rd_is_active(rd)) {
1136                         rd_set_status(rd, 0);
1137                         rd_set_count(rd, 0);
1138                         pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
1139                         if (rd->skb) {
1140                                 dev_kfree_skb_any(rd->skb);
1141                                 rd->skb = NULL;
1142                         }
1143                         IRDA_DEBUG(0, "%s - dropping tx packet\n", __FUNCTION__);
1144                         ret = -VLSI_TX_DROP;
1145                 }
1146                 else
1147                         ret = vlsi_process_tx(r, rd);
1148
1149                 if (ret < 0) {
1150                         ret = -ret;
1151                         idev->stats.tx_errors++;
1152                         if (ret & VLSI_TX_DROP)
1153                                 idev->stats.tx_dropped++;
1154                         if (ret & VLSI_TX_FIFO)
1155                                 idev->stats.tx_fifo_errors++;
1156                 }
1157                 else if (ret > 0){
1158                         idev->stats.tx_packets++;
1159                         idev->stats.tx_bytes += ret;
1160                 }
1161         }
1162
1163 }
1164
1165 /********************************************************/
1166
1167 static int vlsi_start_clock(struct pci_dev *pdev)
1168 {
1169         u8      clkctl, lock;
1170         int     i, count;
1171
1172         if (clksrc < 2) { /* auto or PLL: try PLL */
1173                 clkctl = CLKCTL_PD_INV | CLKCTL_CLKSTP;
1174                 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1175
1176                 /* procedure to detect PLL lock synchronisation:
1177                  * after 0.5 msec initial delay we expect to find 3 PLL lock
1178                  * indications within 10 msec for successful PLL detection.
1179                  */
1180                 udelay(500);
1181                 count = 0;
1182                 for (i = 500; i <= 10000; i += 50) { /* max 10 msec */
1183                         pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &lock);
1184                         if (lock&CLKCTL_LOCK) {
1185                                 if (++count >= 3)
1186                                         break;
1187                         }
1188                         udelay(50);
1189                 }
1190                 if (count < 3) {
1191                         if (clksrc == 1) { /* explicitly asked for PLL hence bail out */
1192                                 IRDA_ERROR("%s: no PLL or failed to lock!\n",
1193                                            __FUNCTION__);
1194                                 clkctl = CLKCTL_CLKSTP;
1195                                 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1196                                 return -1;
1197                         }
1198                         else                    /* was: clksrc=0(auto) */
1199                                 clksrc = 3;     /* fallback to 40MHz XCLK (OB800) */
1200
1201                         IRDA_DEBUG(0, "%s: PLL not locked, fallback to clksrc=%d\n",
1202                                 __FUNCTION__, clksrc);
1203                 }
1204                 else
1205                         clksrc = 1;     /* got successful PLL lock */
1206         }
1207
1208         if (clksrc != 1) {
1209                 /* we get here if either no PLL detected in auto-mode or
1210                    an external clock source was explicitly specified */
1211
1212                 clkctl = CLKCTL_EXTCLK | CLKCTL_CLKSTP;
1213                 if (clksrc == 3)
1214                         clkctl |= CLKCTL_XCKSEL;        
1215                 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1216
1217                 /* no way to test for working XCLK */
1218         }
1219         else
1220                 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1221
1222         /* ok, now going to connect the chip with the clock source */
1223
1224         clkctl &= ~CLKCTL_CLKSTP;
1225         pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1226
1227         return 0;
1228 }
1229
1230 static void vlsi_stop_clock(struct pci_dev *pdev)
1231 {
1232         u8      clkctl;
1233
1234         /* disconnect chip from clock source */
1235         pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1236         clkctl |= CLKCTL_CLKSTP;
1237         pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1238
1239         /* disable all clock sources */
1240         clkctl &= ~(CLKCTL_EXTCLK | CLKCTL_PD_INV);
1241         pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1242 }
1243
1244 /********************************************************/
1245
1246 /* writing all-zero to the VLSI PCI IO register area seems to prevent
1247  * some occasional situations where the hardware fails (symptoms are 
1248  * what appears as stalled tx/rx state machines, i.e. everything ok for
1249  * receive or transmit but hw makes no progress or is unable to access
1250  * the bus memory locations).
1251  * Best place to call this is immediately after/before the internal clock
1252  * gets started/stopped.
1253  */
1254
1255 static inline void vlsi_clear_regs(unsigned iobase)
1256 {
1257         unsigned        i;
1258         const unsigned  chip_io_extent = 32;
1259
1260         for (i = 0; i < chip_io_extent; i += sizeof(u16))
1261                 outw(0, iobase + i);
1262 }
1263
1264 static int vlsi_init_chip(struct pci_dev *pdev)
1265 {
1266         struct net_device *ndev = pci_get_drvdata(pdev);
1267         vlsi_irda_dev_t *idev = ndev->priv;
1268         unsigned        iobase;
1269         u16 ptr;
1270
1271         /* start the clock and clean the registers */
1272
1273         if (vlsi_start_clock(pdev)) {
1274                 IRDA_ERROR("%s: no valid clock source\n", __FUNCTION__);
1275                 return -1;
1276         }
1277         iobase = ndev->base_addr;
1278         vlsi_clear_regs(iobase);
1279
1280         outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* w/c pending IRQ, disable all INT */
1281
1282         outw(0, iobase+VLSI_PIO_IRENABLE);      /* disable IrPHY-interface */
1283
1284         /* disable everything, particularly IRCFG_MSTR - (also resetting the RING_PTR) */
1285
1286         outw(0, iobase+VLSI_PIO_IRCFG);
1287         wmb();
1288
1289         outw(MAX_PACKET_LENGTH, iobase+VLSI_PIO_MAXPKT);  /* max possible value=0x0fff */
1290
1291         outw(BUS_TO_RINGBASE(idev->busaddr), iobase+VLSI_PIO_RINGBASE);
1292
1293         outw(TX_RX_TO_RINGSIZE(idev->tx_ring->size, idev->rx_ring->size),
1294                 iobase+VLSI_PIO_RINGSIZE);      
1295
1296         ptr = inw(iobase+VLSI_PIO_RINGPTR);
1297         atomic_set(&idev->rx_ring->head, RINGPTR_GET_RX(ptr));
1298         atomic_set(&idev->rx_ring->tail, RINGPTR_GET_RX(ptr));
1299         atomic_set(&idev->tx_ring->head, RINGPTR_GET_TX(ptr));
1300         atomic_set(&idev->tx_ring->tail, RINGPTR_GET_TX(ptr));
1301
1302         vlsi_set_baud(idev, iobase);    /* idev->new_baud used as provided by caller */
1303
1304         outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR);  /* just in case - w/c pending IRQ's */
1305         wmb();
1306
1307         /* DO NOT BLINDLY ENABLE IRINTR_ACTEN!
1308          * basically every received pulse fires an ACTIVITY-INT
1309          * leading to >>1000 INT's per second instead of few 10
1310          */
1311
1312         outb(IRINTR_RPKTEN|IRINTR_TPKTEN, iobase+VLSI_PIO_IRINTR);
1313
1314         return 0;
1315 }
1316
1317 static int vlsi_start_hw(vlsi_irda_dev_t *idev)
1318 {
1319         struct pci_dev *pdev = idev->pdev;
1320         struct net_device *ndev = pci_get_drvdata(pdev);
1321         unsigned iobase = ndev->base_addr;
1322         u8 byte;
1323
1324         /* we don't use the legacy UART, disable its address decoding */
1325
1326         pci_read_config_byte(pdev, VLSI_PCI_IRMISC, &byte);
1327         byte &= ~(IRMISC_UARTEN | IRMISC_UARTTST);
1328         pci_write_config_byte(pdev, VLSI_PCI_IRMISC, byte);
1329
1330         /* enable PCI busmaster access to our 16MB page */
1331
1332         pci_write_config_byte(pdev, VLSI_PCI_MSTRPAGE, MSTRPAGE_VALUE);
1333         pci_set_master(pdev);
1334
1335         if (vlsi_init_chip(pdev) < 0) {
1336                 pci_disable_device(pdev);
1337                 return -1;
1338         }
1339
1340         vlsi_fill_rx(idev->rx_ring);
1341
1342         do_gettimeofday(&idev->last_rx);        /* first mtt may start from now on */
1343
1344         outw(0, iobase+VLSI_PIO_PROMPT);        /* kick hw state machine */
1345
1346         return 0;
1347 }
1348
1349 static int vlsi_stop_hw(vlsi_irda_dev_t *idev)
1350 {
1351         struct pci_dev *pdev = idev->pdev;
1352         struct net_device *ndev = pci_get_drvdata(pdev);
1353         unsigned iobase = ndev->base_addr;
1354         unsigned long flags;
1355
1356         spin_lock_irqsave(&idev->lock,flags);
1357         outw(0, iobase+VLSI_PIO_IRENABLE);
1358         outw(0, iobase+VLSI_PIO_IRCFG);                 /* disable everything */
1359
1360         /* disable and w/c irqs */
1361         outb(0, iobase+VLSI_PIO_IRINTR);
1362         wmb();
1363         outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR);
1364         spin_unlock_irqrestore(&idev->lock,flags);
1365
1366         vlsi_unarm_tx(idev);
1367         vlsi_unarm_rx(idev);
1368
1369         vlsi_clear_regs(iobase);
1370         vlsi_stop_clock(pdev);
1371
1372         pci_disable_device(pdev);
1373
1374         return 0;
1375 }
1376
1377 /**************************************************************/
1378
1379 static struct net_device_stats * vlsi_get_stats(struct net_device *ndev)
1380 {
1381         vlsi_irda_dev_t *idev = ndev->priv;
1382
1383         return &idev->stats;
1384 }
1385
1386 static void vlsi_tx_timeout(struct net_device *ndev)
1387 {
1388         vlsi_irda_dev_t *idev = ndev->priv;
1389
1390
1391         vlsi_reg_debug(ndev->base_addr, __FUNCTION__);
1392         vlsi_ring_debug(idev->tx_ring);
1393
1394         if (netif_running(ndev))
1395                 netif_stop_queue(ndev);
1396
1397         vlsi_stop_hw(idev);
1398
1399         /* now simply restart the whole thing */
1400
1401         if (!idev->new_baud)
1402                 idev->new_baud = idev->baud;            /* keep current baudrate */
1403
1404         if (vlsi_start_hw(idev))
1405                 IRDA_ERROR("%s: failed to restart hw - %s(%s) unusable!\n",
1406                            __FUNCTION__, PCIDEV_NAME(idev->pdev), ndev->name);
1407         else
1408                 netif_start_queue(ndev);
1409 }
1410
1411 static int vlsi_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1412 {
1413         vlsi_irda_dev_t *idev = ndev->priv;
1414         struct if_irda_req *irq = (struct if_irda_req *) rq;
1415         unsigned long flags;
1416         u16 fifocnt;
1417         int ret = 0;
1418
1419         switch (cmd) {
1420                 case SIOCSBANDWIDTH:
1421                         if (!capable(CAP_NET_ADMIN)) {
1422                                 ret = -EPERM;
1423                                 break;
1424                         }
1425                         spin_lock_irqsave(&idev->lock, flags);
1426                         idev->new_baud = irq->ifr_baudrate;
1427                         /* when called from userland there might be a minor race window here
1428                          * if the stack tries to change speed concurrently - which would be
1429                          * pretty strange anyway with the userland having full control...
1430                          */
1431                         vlsi_set_baud(idev, ndev->base_addr);
1432                         spin_unlock_irqrestore(&idev->lock, flags);
1433                         break;
1434                 case SIOCSMEDIABUSY:
1435                         if (!capable(CAP_NET_ADMIN)) {
1436                                 ret = -EPERM;
1437                                 break;
1438                         }
1439                         irda_device_set_media_busy(ndev, TRUE);
1440                         break;
1441                 case SIOCGRECEIVING:
1442                         /* the best we can do: check whether there are any bytes in rx fifo.
1443                          * The trustable window (in case some data arrives just afterwards)
1444                          * may be as short as 1usec or so at 4Mbps.
1445                          */
1446                         fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1447                         irq->ifr_receiving = (fifocnt!=0) ? 1 : 0;
1448                         break;
1449                 default:
1450                         IRDA_WARNING("%s: notsupp - cmd=%04x\n",
1451                                      __FUNCTION__, cmd);
1452                         ret = -EOPNOTSUPP;
1453         }       
1454         
1455         return ret;
1456 }
1457
1458 /********************************************************/
1459
1460 static irqreturn_t vlsi_interrupt(int irq, void *dev_instance,
1461                                         struct pt_regs *regs)
1462 {
1463         struct net_device *ndev = dev_instance;
1464         vlsi_irda_dev_t *idev = ndev->priv;
1465         unsigned        iobase;
1466         u8              irintr;
1467         int             boguscount = 5;
1468         unsigned long   flags;
1469         int             handled = 0;
1470
1471         iobase = ndev->base_addr;
1472         spin_lock_irqsave(&idev->lock,flags);
1473         do {
1474                 irintr = inb(iobase+VLSI_PIO_IRINTR);
1475                 mb();
1476                 outb(irintr, iobase+VLSI_PIO_IRINTR);   /* acknowledge asap */
1477
1478                 if (!(irintr&=IRINTR_INT_MASK))         /* not our INT - probably shared */
1479                         break;
1480
1481                 handled = 1;
1482
1483                 if (unlikely(!(irintr & ~IRINTR_ACTIVITY)))
1484                         break;                          /* nothing todo if only activity */
1485
1486                 if (irintr&IRINTR_RPKTINT)
1487                         vlsi_rx_interrupt(ndev);
1488
1489                 if (irintr&IRINTR_TPKTINT)
1490                         vlsi_tx_interrupt(ndev);
1491
1492         } while (--boguscount > 0);
1493         spin_unlock_irqrestore(&idev->lock,flags);
1494
1495         if (boguscount <= 0)
1496                 IRDA_MESSAGE("%s: too much work in interrupt!\n",
1497                              __FUNCTION__);
1498         return IRQ_RETVAL(handled);
1499 }
1500
1501 /********************************************************/
1502
1503 static int vlsi_open(struct net_device *ndev)
1504 {
1505         vlsi_irda_dev_t *idev = ndev->priv;
1506         int     err = -EAGAIN;
1507         char    hwname[32];
1508
1509         if (pci_request_regions(idev->pdev, drivername)) {
1510                 IRDA_WARNING("%s: io resource busy\n", __FUNCTION__);
1511                 goto errout;
1512         }
1513         ndev->base_addr = pci_resource_start(idev->pdev,0);
1514         ndev->irq = idev->pdev->irq;
1515
1516         /* under some rare occasions the chip apparently comes up with
1517          * IRQ's pending. We better w/c pending IRQ and disable them all
1518          */
1519
1520         outb(IRINTR_INT_MASK, ndev->base_addr+VLSI_PIO_IRINTR);
1521
1522         if (request_irq(ndev->irq, vlsi_interrupt, SA_SHIRQ,
1523                         drivername, ndev)) {
1524                 IRDA_WARNING("%s: couldn't get IRQ: %d\n",
1525                              __FUNCTION__, ndev->irq);
1526                 goto errout_io;
1527         }
1528
1529         if ((err = vlsi_create_hwif(idev)) != 0)
1530                 goto errout_irq;
1531
1532         sprintf(hwname, "VLSI-FIR @ 0x%04x", (unsigned)ndev->base_addr);
1533         idev->irlap = irlap_open(ndev,&idev->qos,hwname);
1534         if (!idev->irlap)
1535                 goto errout_free_ring;
1536
1537         do_gettimeofday(&idev->last_rx);  /* first mtt may start from now on */
1538
1539         idev->new_baud = 9600;          /* start with IrPHY using 9600(SIR) mode */
1540
1541         if ((err = vlsi_start_hw(idev)) != 0)
1542                 goto errout_close_irlap;
1543
1544         netif_start_queue(ndev);
1545
1546         IRDA_MESSAGE("%s: device %s operational\n", __FUNCTION__, ndev->name);
1547
1548         return 0;
1549
1550 errout_close_irlap:
1551         irlap_close(idev->irlap);
1552 errout_free_ring:
1553         vlsi_destroy_hwif(idev);
1554 errout_irq:
1555         free_irq(ndev->irq,ndev);
1556 errout_io:
1557         pci_release_regions(idev->pdev);
1558 errout:
1559         return err;
1560 }
1561
1562 static int vlsi_close(struct net_device *ndev)
1563 {
1564         vlsi_irda_dev_t *idev = ndev->priv;
1565
1566         netif_stop_queue(ndev);
1567
1568         if (idev->irlap)
1569                 irlap_close(idev->irlap);
1570         idev->irlap = NULL;
1571
1572         vlsi_stop_hw(idev);
1573
1574         vlsi_destroy_hwif(idev);
1575
1576         free_irq(ndev->irq,ndev);
1577
1578         pci_release_regions(idev->pdev);
1579
1580         IRDA_MESSAGE("%s: device %s stopped\n", __FUNCTION__, ndev->name);
1581
1582         return 0;
1583 }
1584
1585 static int vlsi_irda_init(struct net_device *ndev)
1586 {
1587         vlsi_irda_dev_t *idev = ndev->priv;
1588         struct pci_dev *pdev = idev->pdev;
1589
1590         SET_MODULE_OWNER(ndev);
1591
1592         ndev->irq = pdev->irq;
1593         ndev->base_addr = pci_resource_start(pdev,0);
1594
1595         /* PCI busmastering
1596          * see include file for details why we need these 2 masks, in this order!
1597          */
1598
1599         if (pci_set_dma_mask(pdev,DMA_MASK_USED_BY_HW)
1600             || pci_set_dma_mask(pdev,DMA_MASK_MSTRPAGE)) {
1601                 IRDA_ERROR("%s: aborting due to PCI BM-DMA address limitations\n", __FUNCTION__);
1602                 return -1;
1603         }
1604
1605         irda_init_max_qos_capabilies(&idev->qos);
1606
1607         /* the VLSI82C147 does not support 576000! */
1608
1609         idev->qos.baud_rate.bits = IR_2400 | IR_9600
1610                 | IR_19200 | IR_38400 | IR_57600 | IR_115200
1611                 | IR_1152000 | (IR_4000000 << 8);
1612
1613         idev->qos.min_turn_time.bits = qos_mtt_bits;
1614
1615         irda_qos_bits_to_value(&idev->qos);
1616
1617         /* currently no public media definitions for IrDA */
1618
1619         ndev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
1620         ndev->if_port = IF_PORT_UNKNOWN;
1621  
1622         ndev->open            = vlsi_open;
1623         ndev->stop            = vlsi_close;
1624         ndev->get_stats       = vlsi_get_stats;
1625         ndev->hard_start_xmit = vlsi_hard_start_xmit;
1626         ndev->do_ioctl        = vlsi_ioctl;
1627         ndev->tx_timeout      = vlsi_tx_timeout;
1628         ndev->watchdog_timeo  = 500*HZ/1000;    /* max. allowed turn time for IrLAP */
1629
1630         SET_NETDEV_DEV(ndev, &pdev->dev);
1631
1632         return 0;
1633 }       
1634
1635 /**************************************************************/
1636
1637 static int __devinit
1638 vlsi_irda_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1639 {
1640         struct net_device       *ndev;
1641         vlsi_irda_dev_t         *idev;
1642
1643         if (pci_enable_device(pdev))
1644                 goto out;
1645         else
1646                 pdev->current_state = 0; /* hw must be running now */
1647
1648         IRDA_MESSAGE("%s: IrDA PCI controller %s detected\n",
1649                      drivername, PCIDEV_NAME(pdev));
1650
1651         if ( !pci_resource_start(pdev,0)
1652              || !(pci_resource_flags(pdev,0) & IORESOURCE_IO) ) {
1653                 IRDA_ERROR("%s: bar 0 invalid", __FUNCTION__);
1654                 goto out_disable;
1655         }
1656
1657         ndev = alloc_irdadev(sizeof(*idev));
1658         if (ndev==NULL) {
1659                 IRDA_ERROR("%s: Unable to allocate device memory.\n",
1660                            __FUNCTION__);
1661                 goto out_disable;
1662         }
1663
1664         idev = ndev->priv;
1665
1666         spin_lock_init(&idev->lock);
1667         init_MUTEX(&idev->sem);
1668         down(&idev->sem);
1669         idev->pdev = pdev;
1670
1671         if (vlsi_irda_init(ndev) < 0)
1672                 goto out_freedev;
1673
1674         if (register_netdev(ndev) < 0) {
1675                 IRDA_ERROR("%s: register_netdev failed\n", __FUNCTION__);
1676                 goto out_freedev;
1677         }
1678
1679         if (vlsi_proc_root != NULL) {
1680                 struct proc_dir_entry *ent;
1681
1682                 ent = create_proc_entry(ndev->name, S_IFREG|S_IRUGO, vlsi_proc_root);
1683                 if (!ent) {
1684                         IRDA_WARNING("%s: failed to create proc entry\n",
1685                                      __FUNCTION__);
1686                 } else {
1687                         ent->data = ndev;
1688                         ent->proc_fops = VLSI_PROC_FOPS;
1689                         ent->size = 0;
1690                 }
1691                 idev->proc_entry = ent;
1692         }
1693         IRDA_MESSAGE("%s: registered device %s\n", drivername, ndev->name);
1694
1695         pci_set_drvdata(pdev, ndev);
1696         up(&idev->sem);
1697
1698         return 0;
1699
1700 out_freedev:
1701         up(&idev->sem);
1702         free_netdev(ndev);
1703 out_disable:
1704         pci_disable_device(pdev);
1705 out:
1706         pci_set_drvdata(pdev, NULL);
1707         return -ENODEV;
1708 }
1709
1710 static void __devexit vlsi_irda_remove(struct pci_dev *pdev)
1711 {
1712         struct net_device *ndev = pci_get_drvdata(pdev);
1713         vlsi_irda_dev_t *idev;
1714
1715         if (!ndev) {
1716                 IRDA_ERROR("%s: lost netdevice?\n", drivername);
1717                 return;
1718         }
1719
1720         unregister_netdev(ndev);
1721
1722         idev = ndev->priv;
1723         down(&idev->sem);
1724         if (idev->proc_entry) {
1725                 remove_proc_entry(ndev->name, vlsi_proc_root);
1726                 idev->proc_entry = NULL;
1727         }
1728         up(&idev->sem);
1729
1730         free_netdev(ndev);
1731
1732         pci_set_drvdata(pdev, NULL);
1733
1734         IRDA_MESSAGE("%s: %s removed\n", drivername, PCIDEV_NAME(pdev));
1735 }
1736
1737 #ifdef CONFIG_PM
1738
1739 /* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
1740  * Some of the Linux PCI-PM code however depends on this, for example in
1741  * pci_set_power_state(). So we have to take care to perform the required
1742  * operations on our own (particularly reflecting the pdev->current_state)
1743  * otherwise we might get cheated by pci-pm.
1744  */
1745
1746
1747 static int vlsi_irda_suspend(struct pci_dev *pdev, pm_message_t state)
1748 {
1749         struct net_device *ndev = pci_get_drvdata(pdev);
1750         vlsi_irda_dev_t *idev;
1751
1752         if (!ndev) {
1753                 IRDA_ERROR("%s - %s: no netdevice \n",
1754                            __FUNCTION__, PCIDEV_NAME(pdev));
1755                 return 0;
1756         }
1757         idev = ndev->priv;      
1758         down(&idev->sem);
1759         if (pdev->current_state != 0) {                 /* already suspended */
1760                 if (state.event > pdev->current_state) {        /* simply go deeper */
1761                         pci_set_power_state(pdev, pci_choose_state(pdev, state));
1762                         pdev->current_state = state.event;
1763                 }
1764                 else
1765                         IRDA_ERROR("%s - %s: invalid suspend request %u -> %u\n", __FUNCTION__, PCIDEV_NAME(pdev), pdev->current_state, state.event);
1766                 up(&idev->sem);
1767                 return 0;
1768         }
1769
1770         if (netif_running(ndev)) {
1771                 netif_device_detach(ndev);
1772                 vlsi_stop_hw(idev);
1773                 pci_save_state(pdev);
1774                 if (!idev->new_baud)
1775                         /* remember speed settings to restore on resume */
1776                         idev->new_baud = idev->baud;
1777         }
1778
1779         pci_set_power_state(pdev, pci_choose_state(pdev, state));
1780         pdev->current_state = state.event;
1781         idev->resume_ok = 1;
1782         up(&idev->sem);
1783         return 0;
1784 }
1785
1786 static int vlsi_irda_resume(struct pci_dev *pdev)
1787 {
1788         struct net_device *ndev = pci_get_drvdata(pdev);
1789         vlsi_irda_dev_t *idev;
1790
1791         if (!ndev) {
1792                 IRDA_ERROR("%s - %s: no netdevice \n",
1793                            __FUNCTION__, PCIDEV_NAME(pdev));
1794                 return 0;
1795         }
1796         idev = ndev->priv;      
1797         down(&idev->sem);
1798         if (pdev->current_state == 0) {
1799                 up(&idev->sem);
1800                 IRDA_WARNING("%s - %s: already resumed\n",
1801                              __FUNCTION__, PCIDEV_NAME(pdev));
1802                 return 0;
1803         }
1804         
1805         pci_set_power_state(pdev, PCI_D0);
1806         pdev->current_state = PM_EVENT_ON;
1807
1808         if (!idev->resume_ok) {
1809                 /* should be obsolete now - but used to happen due to:
1810                  * - pci layer initially setting pdev->current_state = 4 (unknown)
1811                  * - pci layer did not walk the save_state-tree (might be APM problem)
1812                  *   so we could not refuse to suspend from undefined state
1813                  * - vlsi_irda_suspend detected invalid state and refused to save
1814                  *   configuration for resume - but was too late to stop suspending
1815                  * - vlsi_irda_resume got screwed when trying to resume from garbage
1816                  *
1817                  * now we explicitly set pdev->current_state = 0 after enabling the
1818                  * device and independently resume_ok should catch any garbage config.
1819                  */
1820                 IRDA_WARNING("%s - hm, nothing to resume?\n", __FUNCTION__);
1821                 up(&idev->sem);
1822                 return 0;
1823         }
1824
1825         if (netif_running(ndev)) {
1826                 pci_restore_state(pdev);
1827                 vlsi_start_hw(idev);
1828                 netif_device_attach(ndev);
1829         }
1830         idev->resume_ok = 0;
1831         up(&idev->sem);
1832         return 0;
1833 }
1834
1835 #endif /* CONFIG_PM */
1836
1837 /*********************************************************/
1838
1839 static struct pci_driver vlsi_irda_driver = {
1840         .name           = drivername,
1841         .id_table       = vlsi_irda_table,
1842         .probe          = vlsi_irda_probe,
1843         .remove         = __devexit_p(vlsi_irda_remove),
1844 #ifdef CONFIG_PM
1845         .suspend        = vlsi_irda_suspend,
1846         .resume         = vlsi_irda_resume,
1847 #endif
1848 };
1849
1850 #define PROC_DIR ("driver/" DRIVER_NAME)
1851
1852 static int __init vlsi_mod_init(void)
1853 {
1854         int     i, ret;
1855
1856         if (clksrc < 0  ||  clksrc > 3) {
1857                 IRDA_ERROR("%s: invalid clksrc=%d\n", drivername, clksrc);
1858                 return -1;
1859         }
1860
1861         for (i = 0; i < 2; i++) {
1862                 switch(ringsize[i]) {
1863                         case 4:
1864                         case 8:
1865                         case 16:
1866                         case 32:
1867                         case 64:
1868                                 break;
1869                         default:
1870                                 IRDA_WARNING("%s: invalid %s ringsize %d, using default=8", drivername, (i)?"rx":"tx", ringsize[i]);
1871                                 ringsize[i] = 8;
1872                                 break;
1873                 }
1874         } 
1875
1876         sirpulse = !!sirpulse;
1877
1878         /* create_proc_entry returns NULL if !CONFIG_PROC_FS.
1879          * Failure to create the procfs entry is handled like running
1880          * without procfs - it's not required for the driver to work.
1881          */
1882         vlsi_proc_root = create_proc_entry(PROC_DIR, S_IFDIR, NULL);
1883         if (vlsi_proc_root) {
1884                 /* protect registered procdir against module removal.
1885                  * Because we are in the module init path there's no race
1886                  * window after create_proc_entry (and no barrier needed).
1887                  */
1888                 vlsi_proc_root->owner = THIS_MODULE;
1889         }
1890
1891         ret = pci_module_init(&vlsi_irda_driver);
1892
1893         if (ret && vlsi_proc_root)
1894                 remove_proc_entry(PROC_DIR, NULL);
1895         return ret;
1896
1897 }
1898
1899 static void __exit vlsi_mod_exit(void)
1900 {
1901         pci_unregister_driver(&vlsi_irda_driver);
1902         if (vlsi_proc_root)
1903                 remove_proc_entry(PROC_DIR, NULL);
1904 }
1905
1906 module_init(vlsi_mod_init);
1907 module_exit(vlsi_mod_exit);