1 /*********************************************************************
3 * vlsi_ir.c: VLSI82C147 PCI IrDA controller driver for Linux
5 * Copyright (c) 2001-2003 Martin Diehl
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.
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.
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,
22 ********************************************************************/
24 #include <linux/config.h>
25 #include <linux/module.h>
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>"
32 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
33 MODULE_AUTHOR(DRIVER_AUTHOR);
34 MODULE_LICENSE("GPL");
36 /********************************************************/
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>
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>
59 /********************************************************/
61 static /* const */ char drivername[] = DRIVER_NAME;
63 static struct pci_device_id vlsi_irda_table [] = {
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,
75 MODULE_DEVICE_TABLE(pci, vlsi_irda_table);
77 /********************************************************/
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)
86 static int clksrc = 0; /* default is 0(auto) */
87 module_param(clksrc, int, 0);
88 MODULE_PARM_DESC(clksrc, "clock input source selection");
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
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");
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.
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");
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.
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");
126 /********************************************************/
128 static void vlsi_reg_debug(unsigned iobase, const char *s)
132 printk(KERN_DEBUG "%s: ", s);
133 for (i = 0; i < 0x20; i++)
134 printk("%02x", (unsigned)inb((iobase+i)));
138 static void vlsi_ring_debug(struct vlsi_ring *r)
140 struct ring_descr *rd;
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++) {
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));
157 /********************************************************/
159 /* needed regardless of CONFIG_PROC_FS */
160 static struct proc_dir_entry *vlsi_proc_root = NULL;
162 #ifdef CONFIG_PROC_FS
164 static void vlsi_proc_pdev(struct seq_file *seq, struct pci_dev *pdev)
166 unsigned iobase = pci_resource_start(pdev, 0);
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");
180 static void vlsi_proc_ndev(struct seq_file *seq, struct net_device *ndev)
182 vlsi_irda_dev_t *idev = ndev->priv;
185 unsigned delta1, delta2;
187 unsigned iobase = ndev->base_addr;
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");
195 if (!netif_running(ndev))
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"));
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);
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));
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);
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;
288 delta2 = 1000000 + now.tv_usec - idev->last_rx.tv_usec;
291 seq_printf(seq, "last rx: %lu.%06u sec\n",
292 now.tv_sec - idev->last_rx.tv_sec - delta1, delta2);
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);
306 static void vlsi_proc_ring(struct seq_file *seq, struct vlsi_ring *r)
308 struct ring_descr *rd;
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);
318 seq_printf(seq, "(empty)\n");
320 if (((t+1)&r->mask) == h)
321 seq_printf(seq, "(full)\n");
323 seq_printf(seq, "(level = %d)\n", ((unsigned)(t-h) & r->mask));
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);
329 seq_printf(seq, " data:");
332 for (i = 0; i < j; i++)
333 seq_printf(seq, " %02x", (unsigned)((unsigned char *)rd->buf)[i]);
334 seq_printf(seq, "\n");
337 for (i = 0; i < r->size; 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));
347 static int vlsi_seq_show(struct seq_file *seq, void *v)
349 struct net_device *ndev = seq->private;
350 vlsi_irda_dev_t *idev = ndev->priv;
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);
362 spin_lock_irqsave(&idev->lock, flags);
363 if (idev->pdev != NULL) {
364 vlsi_proc_pdev(seq, idev->pdev);
366 if (idev->pdev->current_state == 0)
367 vlsi_proc_ndev(seq, ndev);
369 seq_printf(seq, "\nPCI controller down - resume_ok = %d\n",
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);
378 seq_printf(seq, "\n");
379 spin_unlock_irqrestore(&idev->lock, flags);
384 static int vlsi_seq_open(struct inode *inode, struct file *file)
386 return single_open(file, vlsi_seq_show, PDE(inode)->data);
389 static struct file_operations vlsi_proc_fops = {
390 .owner = THIS_MODULE,
391 .open = vlsi_seq_open,
394 .release = single_release,
397 #define VLSI_PROC_FOPS (&vlsi_proc_fops)
399 #else /* CONFIG_PROC_FS */
400 #define VLSI_PROC_FOPS NULL
403 /********************************************************/
405 static struct vlsi_ring *vlsi_alloc_ring(struct pci_dev *pdev, struct ring_descr_hw *hwmap,
406 unsigned size, unsigned len, int dir)
409 struct ring_descr *rd;
413 if (!size || ((size-1)&size)!=0) /* must be >0 and power of 2 */
416 r = kmalloc(sizeof(*r) + size * sizeof(struct ring_descr), GFP_KERNEL);
419 memset(r, 0, sizeof(*r));
424 r->rd = (struct ring_descr *)(r+1);
427 atomic_set(&r->head, 0);
428 atomic_set(&r->tail, 0);
430 for (i = 0; i < size; i++) {
432 memset(rd, 0, sizeof(*rd));
434 rd->buf = kmalloc(len, GFP_KERNEL|GFP_DMA);
436 || !(busaddr = pci_map_single(pdev, rd->buf, len, dir))) {
438 IRDA_ERROR("%s: failed to create PCI-MAP for %p",
439 __FUNCTION__, rd->buf);
443 for (j = 0; j < i; j++) {
445 busaddr = rd_get_addr(rd);
446 rd_set_addr_status(rd, 0, 0);
448 pci_unmap_single(pdev, busaddr, len, dir);
455 rd_set_addr_status(rd, busaddr, 0);
456 /* initially, the dma buffer is owned by the CPU */
462 static int vlsi_free_ring(struct vlsi_ring *r)
464 struct ring_descr *rd;
468 for (i = 0; i < r->size; i++) {
471 dev_kfree_skb_any(rd->skb);
472 busaddr = rd_get_addr(rd);
473 rd_set_addr_status(rd, 0, 0);
475 pci_unmap_single(r->pdev, busaddr, r->len, r->dir);
482 static int vlsi_create_hwif(vlsi_irda_dev_t *idev)
485 struct ring_descr_hw *hwmap;
487 idev->virtaddr = NULL;
490 ringarea = pci_alloc_consistent(idev->pdev, HW_RING_AREA_SIZE, &idev->busaddr);
492 IRDA_ERROR("%s: insufficient memory for descriptor rings\n",
496 memset(ringarea, 0, HW_RING_AREA_SIZE);
498 hwmap = (struct ring_descr_hw *)ringarea;
499 idev->rx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[1],
500 XFER_BUF_SIZE, PCI_DMA_FROMDEVICE);
501 if (idev->rx_ring == NULL)
504 hwmap += MAX_RING_DESCR;
505 idev->tx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[0],
506 XFER_BUF_SIZE, PCI_DMA_TODEVICE);
507 if (idev->tx_ring == NULL)
510 idev->virtaddr = ringarea;
514 vlsi_free_ring(idev->rx_ring);
516 idev->rx_ring = idev->tx_ring = NULL;
517 pci_free_consistent(idev->pdev, HW_RING_AREA_SIZE, ringarea, idev->busaddr);
523 static int vlsi_destroy_hwif(vlsi_irda_dev_t *idev)
525 vlsi_free_ring(idev->rx_ring);
526 vlsi_free_ring(idev->tx_ring);
527 idev->rx_ring = idev->tx_ring = NULL;
530 pci_free_consistent(idev->pdev,HW_RING_AREA_SIZE,idev->virtaddr,idev->busaddr);
532 idev->virtaddr = NULL;
538 /********************************************************/
540 static int vlsi_process_rx(struct vlsi_ring *r, struct ring_descr *rd)
546 struct net_device *ndev = (struct net_device *)pci_get_drvdata(r->pdev);
547 vlsi_irda_dev_t *idev = ndev->priv;
549 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
550 /* dma buffer now owned by the CPU */
551 status = rd_get_status(rd);
552 if (status & RD_RX_ERROR) {
553 if (status & RD_RX_OVER)
555 if (status & RD_RX_LENGTH)
556 ret |= VLSI_RX_LENGTH;
557 if (status & RD_RX_PHYERR)
558 ret |= VLSI_RX_FRAME;
559 if (status & RD_RX_CRCERR)
564 len = rd_get_count(rd);
565 crclen = (idev->mode==IFF_FIR) ? sizeof(u32) : sizeof(u16);
566 len -= crclen; /* remove trailing CRC */
568 IRDA_DEBUG(0, "%s: strange frame (len=%d)\n", __FUNCTION__, len);
573 if (idev->mode == IFF_SIR) { /* hw checks CRC in MIR, FIR mode */
575 /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
576 * endian-adjustment there just in place will dirty a cache line
577 * which belongs to the map and thus we must be sure it will
578 * get flushed before giving the buffer back to hardware.
579 * vlsi_fill_rx() will do this anyway - but here we rely on.
581 le16_to_cpus(rd->buf+len);
582 if (irda_calc_crc16(INIT_FCS,rd->buf,len+crclen) != GOOD_FCS) {
583 IRDA_DEBUG(0, "%s: crc error\n", __FUNCTION__);
590 IRDA_WARNING("%s: rx packet lost\n", __FUNCTION__);
598 memcpy(skb_put(skb,len), rd->buf, len);
599 skb->mac.raw = skb->data;
604 ndev->last_rx = jiffies;
607 rd_set_status(rd, 0);
609 /* buffer still owned by CPU */
611 return (ret) ? -ret : len;
614 static void vlsi_fill_rx(struct vlsi_ring *r)
616 struct ring_descr *rd;
618 for (rd = ring_last(r); rd != NULL; rd = ring_put(r)) {
619 if (rd_is_active(rd)) {
620 IRDA_WARNING("%s: driver bug: rx descr race with hw\n",
626 rd->skb = dev_alloc_skb(IRLAP_SKB_ALLOCSIZE);
628 skb_reserve(rd->skb,1);
629 rd->skb->protocol = htons(ETH_P_IRDA);
632 break; /* probably not worth logging? */
634 /* give dma buffer back to busmaster */
635 pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
640 static void vlsi_rx_interrupt(struct net_device *ndev)
642 vlsi_irda_dev_t *idev = ndev->priv;
643 struct vlsi_ring *r = idev->rx_ring;
644 struct ring_descr *rd;
647 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
649 if (rd_is_active(rd))
652 ret = vlsi_process_rx(r, rd);
656 idev->stats.rx_errors++;
657 if (ret & VLSI_RX_DROP)
658 idev->stats.rx_dropped++;
659 if (ret & VLSI_RX_OVER)
660 idev->stats.rx_over_errors++;
661 if (ret & VLSI_RX_LENGTH)
662 idev->stats.rx_length_errors++;
663 if (ret & VLSI_RX_FRAME)
664 idev->stats.rx_frame_errors++;
665 if (ret & VLSI_RX_CRC)
666 idev->stats.rx_crc_errors++;
669 idev->stats.rx_packets++;
670 idev->stats.rx_bytes += ret;
674 do_gettimeofday(&idev->last_rx); /* remember "now" for later mtt delay */
678 if (ring_first(r) == NULL) {
679 /* we are in big trouble, if this should ever happen */
680 IRDA_ERROR("%s: rx ring exhausted!\n", __FUNCTION__);
684 outw(0, ndev->base_addr+VLSI_PIO_PROMPT);
687 /* caller must have stopped the controller from busmastering */
689 static void vlsi_unarm_rx(vlsi_irda_dev_t *idev)
691 struct vlsi_ring *r = idev->rx_ring;
692 struct ring_descr *rd;
695 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
698 if (rd_is_active(rd)) {
699 rd_set_status(rd, 0);
700 if (rd_get_count(rd)) {
701 IRDA_DEBUG(0, "%s - dropping rx packet\n", __FUNCTION__);
705 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
707 dev_kfree_skb_any(rd->skb);
712 ret = vlsi_process_rx(r, rd);
716 idev->stats.rx_errors++;
717 if (ret & VLSI_RX_DROP)
718 idev->stats.rx_dropped++;
719 if (ret & VLSI_RX_OVER)
720 idev->stats.rx_over_errors++;
721 if (ret & VLSI_RX_LENGTH)
722 idev->stats.rx_length_errors++;
723 if (ret & VLSI_RX_FRAME)
724 idev->stats.rx_frame_errors++;
725 if (ret & VLSI_RX_CRC)
726 idev->stats.rx_crc_errors++;
729 idev->stats.rx_packets++;
730 idev->stats.rx_bytes += ret;
735 /********************************************************/
737 static int vlsi_process_tx(struct vlsi_ring *r, struct ring_descr *rd)
743 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
744 /* dma buffer now owned by the CPU */
745 status = rd_get_status(rd);
746 if (status & RD_TX_UNDRN)
750 rd_set_status(rd, 0);
754 dev_kfree_skb_any(rd->skb);
757 else /* tx-skb already freed? - should never happen */
758 len = rd_get_count(rd); /* incorrect for SIR! (due to wrapping) */
761 /* dma buffer still owned by the CPU */
763 return (ret) ? -ret : len;
766 static int vlsi_set_baud(vlsi_irda_dev_t *idev, unsigned iobase)
775 baudrate = idev->new_baud;
776 IRDA_DEBUG(2, "%s: %d -> %d\n", __FUNCTION__, idev->baud, idev->new_baud);
777 if (baudrate == 4000000) {
780 nphyctl = PHYCTL_FIR;
782 else if (baudrate == 1152000) {
784 config = IRCFG_MIR | IRCFG_CRC16;
785 nphyctl = PHYCTL_MIR(clksrc==3);
789 config = IRCFG_SIR | IRCFG_SIRFILT | IRCFG_RXANY;
792 IRDA_WARNING("%s: undefined baudrate %d - fallback to 9600!\n",
793 __FUNCTION__, baudrate);
802 nphyctl = PHYCTL_SIR(baudrate,sirpulse,clksrc==3);
806 config |= IRCFG_MSTR | IRCFG_ENRX;
808 fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
810 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
813 outw(0, iobase+VLSI_PIO_IRENABLE);
814 outw(config, iobase+VLSI_PIO_IRCFG);
815 outw(nphyctl, iobase+VLSI_PIO_NPHYCTL);
817 outw(IRENABLE_PHYANDCLOCK, iobase+VLSI_PIO_IRENABLE);
820 udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
822 /* read back settings for validation */
824 config = inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_MASK;
827 config ^= IRENABLE_FIR_ON;
828 else if (mode == IFF_MIR)
829 config ^= (IRENABLE_MIR_ON|IRENABLE_CRC16_ON);
831 config ^= IRENABLE_SIR_ON;
833 if (config != (IRENABLE_PHYANDCLOCK|IRENABLE_ENRXST)) {
834 IRDA_WARNING("%s: failed to set %s mode!\n", __FUNCTION__,
835 (mode==IFF_SIR)?"SIR":((mode==IFF_MIR)?"MIR":"FIR"));
839 if (inw(iobase+VLSI_PIO_PHYCTL) != nphyctl) {
840 IRDA_WARNING("%s: failed to apply baudrate %d\n",
841 __FUNCTION__, baudrate);
846 idev->baud = baudrate;
853 vlsi_reg_debug(iobase,__FUNCTION__);
858 static int vlsi_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
860 vlsi_irda_dev_t *idev = ndev->priv;
861 struct vlsi_ring *r = idev->tx_ring;
862 struct ring_descr *rd;
864 unsigned iobase = ndev->base_addr;
869 struct timeval now, ready;
872 speed = irda_get_next_speed(skb);
873 spin_lock_irqsave(&idev->lock, flags);
874 if (speed != -1 && speed != idev->baud) {
875 netif_stop_queue(ndev);
876 idev->new_baud = speed;
877 status = RD_TX_CLRENTX; /* stop tx-ring after this frame */
883 /* handle zero packets - should be speed change */
885 msg = "bogus zero-length packet";
889 /* due to the completely asynch tx operation we might have
890 * IrLAP racing with the hardware here, f.e. if the controller
891 * is just sending the last packet with current speed while
892 * the LAP is already switching the speed using synchronous
893 * len=0 packet. Immediate execution would lead to hw lockup
894 * requiring a powercycle to reset. Good candidate to trigger
895 * this is the final UA:RSP packet after receiving a DISC:CMD
896 * when getting the LAP down.
897 * Note that we are not protected by the queue_stop approach
898 * because the final UA:RSP arrives _without_ request to apply
899 * new-speed-after-this-packet - hence the driver doesn't know
900 * this was the last packet and doesn't stop the queue. So the
901 * forced switch to default speed from LAP gets through as fast
902 * as only some 10 usec later while the UA:RSP is still processed
903 * by the hardware and we would get screwed.
906 if (ring_first(idev->tx_ring) == NULL) {
907 /* no race - tx-ring already empty */
908 vlsi_set_baud(idev, iobase);
909 netif_wake_queue(ndev);
913 /* keep the speed change pending like it would
914 * for any len>0 packet. tx completion interrupt
915 * will apply it when the tx ring becomes empty.
917 spin_unlock_irqrestore(&idev->lock, flags);
918 dev_kfree_skb_any(skb);
922 /* sanity checks - simply drop the packet */
926 msg = "ring full, but queue wasn't stopped";
930 if (rd_is_active(rd)) {
931 msg = "entry still owned by hw";
936 msg = "tx ring entry without pci buffer";
941 msg = "ring entry with old skb still attached";
945 /* no need for serialization or interrupt disable during mtt */
946 spin_unlock_irqrestore(&idev->lock, flags);
948 if ((mtt = irda_get_mtt(skb)) > 0) {
950 ready.tv_usec = idev->last_rx.tv_usec + mtt;
951 ready.tv_sec = idev->last_rx.tv_sec;
952 if (ready.tv_usec >= 1000000) {
953 ready.tv_usec -= 1000000;
954 ready.tv_sec++; /* IrLAP 1.1: mtt always < 1 sec */
957 do_gettimeofday(&now);
958 if (now.tv_sec > ready.tv_sec
959 || (now.tv_sec==ready.tv_sec && now.tv_usec>=ready.tv_usec))
962 /* must not sleep here - we are called under xmit_lock! */
966 /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
967 * after subsequent tx-completion
970 if (idev->mode == IFF_SIR) {
971 status |= RD_TX_DISCRC; /* no hw-crc creation */
972 len = async_wrap_skb(skb, rd->buf, r->len);
974 /* Some rare worst case situation in SIR mode might lead to
975 * potential buffer overflow. The wrapper detects this, returns
976 * with a shortened frame (without FCS/EOF) but doesn't provide
977 * any error indication about the invalid packet which we are
979 * Therefore we log if the buffer got filled to the point, where the
980 * wrapper would abort, i.e. when there are less than 5 bytes left to
981 * allow appending the FCS/EOF.
985 IRDA_WARNING("%s: possible buffer overflow with SIR wrapping!\n",
989 /* hw deals with MIR/FIR mode wrapping */
990 status |= RD_TX_PULSE; /* send 2 us highspeed indication pulse */
993 msg = "frame exceeds tx buffer length";
997 memcpy(rd->buf, skb->data, len);
1000 rd->skb = skb; /* remember skb for tx-complete stats */
1002 rd_set_count(rd, len);
1003 rd_set_status(rd, status); /* not yet active! */
1005 /* give dma buffer back to busmaster-hw (flush caches to make
1006 * CPU-driven changes visible from the pci bus).
1009 pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
1011 /* Switching to TX mode here races with the controller
1012 * which may stop TX at any time when fetching an inactive descriptor
1013 * or one with CLR_ENTX set. So we switch on TX only, if TX was not running
1014 * _after_ the new descriptor was activated on the ring. This ensures
1015 * we will either find TX already stopped or we can be sure, there
1016 * will be a TX-complete interrupt even if the chip stopped doing
1017 * TX just after we found it still running. The ISR will then find
1018 * the non-empty ring and restart TX processing. The enclosing
1019 * spinlock provides the correct serialization to prevent race with isr.
1022 spin_lock_irqsave(&idev->lock,flags);
1026 if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1029 fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1031 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
1034 config = inw(iobase+VLSI_PIO_IRCFG);
1036 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1038 outw(0, iobase+VLSI_PIO_PROMPT);
1040 ndev->trans_start = jiffies;
1042 if (ring_put(r) == NULL) {
1043 netif_stop_queue(ndev);
1044 IRDA_DEBUG(3, "%s: tx ring full - queue stopped\n", __FUNCTION__);
1046 spin_unlock_irqrestore(&idev->lock, flags);
1051 spin_unlock_irqrestore(&idev->lock, flags);
1053 IRDA_WARNING("%s: dropping packet - %s\n", __FUNCTION__, msg);
1054 dev_kfree_skb_any(skb);
1055 idev->stats.tx_errors++;
1056 idev->stats.tx_dropped++;
1057 /* Don't even think about returning NET_XMIT_DROP (=1) here!
1058 * In fact any retval!=0 causes the packet scheduler to requeue the
1059 * packet for later retry of transmission - which isn't exactly
1060 * what we want after we've just called dev_kfree_skb_any ;-)
1065 static void vlsi_tx_interrupt(struct net_device *ndev)
1067 vlsi_irda_dev_t *idev = ndev->priv;
1068 struct vlsi_ring *r = idev->tx_ring;
1069 struct ring_descr *rd;
1074 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1076 if (rd_is_active(rd))
1079 ret = vlsi_process_tx(r, rd);
1083 idev->stats.tx_errors++;
1084 if (ret & VLSI_TX_DROP)
1085 idev->stats.tx_dropped++;
1086 if (ret & VLSI_TX_FIFO)
1087 idev->stats.tx_fifo_errors++;
1090 idev->stats.tx_packets++;
1091 idev->stats.tx_bytes += ret;
1095 iobase = ndev->base_addr;
1097 if (idev->new_baud && rd == NULL) /* tx ring empty and speed change pending */
1098 vlsi_set_baud(idev, iobase);
1100 config = inw(iobase+VLSI_PIO_IRCFG);
1101 if (rd == NULL) /* tx ring empty: re-enable rx */
1102 outw((config & ~IRCFG_ENTX) | IRCFG_ENRX, iobase+VLSI_PIO_IRCFG);
1104 else if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1107 fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1109 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n",
1110 __FUNCTION__, fifocnt);
1112 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1115 outw(0, iobase+VLSI_PIO_PROMPT);
1117 if (netif_queue_stopped(ndev) && !idev->new_baud) {
1118 netif_wake_queue(ndev);
1119 IRDA_DEBUG(3, "%s: queue awoken\n", __FUNCTION__);
1123 /* caller must have stopped the controller from busmastering */
1125 static void vlsi_unarm_tx(vlsi_irda_dev_t *idev)
1127 struct vlsi_ring *r = idev->tx_ring;
1128 struct ring_descr *rd;
1131 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1134 if (rd_is_active(rd)) {
1135 rd_set_status(rd, 0);
1136 rd_set_count(rd, 0);
1137 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
1139 dev_kfree_skb_any(rd->skb);
1142 IRDA_DEBUG(0, "%s - dropping tx packet\n", __FUNCTION__);
1143 ret = -VLSI_TX_DROP;
1146 ret = vlsi_process_tx(r, rd);
1150 idev->stats.tx_errors++;
1151 if (ret & VLSI_TX_DROP)
1152 idev->stats.tx_dropped++;
1153 if (ret & VLSI_TX_FIFO)
1154 idev->stats.tx_fifo_errors++;
1157 idev->stats.tx_packets++;
1158 idev->stats.tx_bytes += ret;
1164 /********************************************************/
1166 static int vlsi_start_clock(struct pci_dev *pdev)
1171 if (clksrc < 2) { /* auto or PLL: try PLL */
1172 clkctl = CLKCTL_PD_INV | CLKCTL_CLKSTP;
1173 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1175 /* procedure to detect PLL lock synchronisation:
1176 * after 0.5 msec initial delay we expect to find 3 PLL lock
1177 * indications within 10 msec for successful PLL detection.
1181 for (i = 500; i <= 10000; i += 50) { /* max 10 msec */
1182 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &lock);
1183 if (lock&CLKCTL_LOCK) {
1190 if (clksrc == 1) { /* explicitly asked for PLL hence bail out */
1191 IRDA_ERROR("%s: no PLL or failed to lock!\n",
1193 clkctl = CLKCTL_CLKSTP;
1194 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1197 else /* was: clksrc=0(auto) */
1198 clksrc = 3; /* fallback to 40MHz XCLK (OB800) */
1200 IRDA_DEBUG(0, "%s: PLL not locked, fallback to clksrc=%d\n",
1201 __FUNCTION__, clksrc);
1204 clksrc = 1; /* got successful PLL lock */
1208 /* we get here if either no PLL detected in auto-mode or
1209 an external clock source was explicitly specified */
1211 clkctl = CLKCTL_EXTCLK | CLKCTL_CLKSTP;
1213 clkctl |= CLKCTL_XCKSEL;
1214 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1216 /* no way to test for working XCLK */
1219 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1221 /* ok, now going to connect the chip with the clock source */
1223 clkctl &= ~CLKCTL_CLKSTP;
1224 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1229 static void vlsi_stop_clock(struct pci_dev *pdev)
1233 /* disconnect chip from clock source */
1234 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1235 clkctl |= CLKCTL_CLKSTP;
1236 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1238 /* disable all clock sources */
1239 clkctl &= ~(CLKCTL_EXTCLK | CLKCTL_PD_INV);
1240 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1243 /********************************************************/
1245 /* writing all-zero to the VLSI PCI IO register area seems to prevent
1246 * some occasional situations where the hardware fails (symptoms are
1247 * what appears as stalled tx/rx state machines, i.e. everything ok for
1248 * receive or transmit but hw makes no progress or is unable to access
1249 * the bus memory locations).
1250 * Best place to call this is immediately after/before the internal clock
1251 * gets started/stopped.
1254 static inline void vlsi_clear_regs(unsigned iobase)
1257 const unsigned chip_io_extent = 32;
1259 for (i = 0; i < chip_io_extent; i += sizeof(u16))
1260 outw(0, iobase + i);
1263 static int vlsi_init_chip(struct pci_dev *pdev)
1265 struct net_device *ndev = pci_get_drvdata(pdev);
1266 vlsi_irda_dev_t *idev = ndev->priv;
1270 /* start the clock and clean the registers */
1272 if (vlsi_start_clock(pdev)) {
1273 IRDA_ERROR("%s: no valid clock source\n", __FUNCTION__);
1276 iobase = ndev->base_addr;
1277 vlsi_clear_regs(iobase);
1279 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* w/c pending IRQ, disable all INT */
1281 outw(0, iobase+VLSI_PIO_IRENABLE); /* disable IrPHY-interface */
1283 /* disable everything, particularly IRCFG_MSTR - (also resetting the RING_PTR) */
1285 outw(0, iobase+VLSI_PIO_IRCFG);
1288 outw(MAX_PACKET_LENGTH, iobase+VLSI_PIO_MAXPKT); /* max possible value=0x0fff */
1290 outw(BUS_TO_RINGBASE(idev->busaddr), iobase+VLSI_PIO_RINGBASE);
1292 outw(TX_RX_TO_RINGSIZE(idev->tx_ring->size, idev->rx_ring->size),
1293 iobase+VLSI_PIO_RINGSIZE);
1295 ptr = inw(iobase+VLSI_PIO_RINGPTR);
1296 atomic_set(&idev->rx_ring->head, RINGPTR_GET_RX(ptr));
1297 atomic_set(&idev->rx_ring->tail, RINGPTR_GET_RX(ptr));
1298 atomic_set(&idev->tx_ring->head, RINGPTR_GET_TX(ptr));
1299 atomic_set(&idev->tx_ring->tail, RINGPTR_GET_TX(ptr));
1301 vlsi_set_baud(idev, iobase); /* idev->new_baud used as provided by caller */
1303 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* just in case - w/c pending IRQ's */
1306 /* DO NOT BLINDLY ENABLE IRINTR_ACTEN!
1307 * basically every received pulse fires an ACTIVITY-INT
1308 * leading to >>1000 INT's per second instead of few 10
1311 outb(IRINTR_RPKTEN|IRINTR_TPKTEN, iobase+VLSI_PIO_IRINTR);
1316 static int vlsi_start_hw(vlsi_irda_dev_t *idev)
1318 struct pci_dev *pdev = idev->pdev;
1319 struct net_device *ndev = pci_get_drvdata(pdev);
1320 unsigned iobase = ndev->base_addr;
1323 /* we don't use the legacy UART, disable its address decoding */
1325 pci_read_config_byte(pdev, VLSI_PCI_IRMISC, &byte);
1326 byte &= ~(IRMISC_UARTEN | IRMISC_UARTTST);
1327 pci_write_config_byte(pdev, VLSI_PCI_IRMISC, byte);
1329 /* enable PCI busmaster access to our 16MB page */
1331 pci_write_config_byte(pdev, VLSI_PCI_MSTRPAGE, MSTRPAGE_VALUE);
1332 pci_set_master(pdev);
1334 if (vlsi_init_chip(pdev) < 0) {
1335 pci_disable_device(pdev);
1339 vlsi_fill_rx(idev->rx_ring);
1341 do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
1343 outw(0, iobase+VLSI_PIO_PROMPT); /* kick hw state machine */
1348 static int vlsi_stop_hw(vlsi_irda_dev_t *idev)
1350 struct pci_dev *pdev = idev->pdev;
1351 struct net_device *ndev = pci_get_drvdata(pdev);
1352 unsigned iobase = ndev->base_addr;
1353 unsigned long flags;
1355 spin_lock_irqsave(&idev->lock,flags);
1356 outw(0, iobase+VLSI_PIO_IRENABLE);
1357 outw(0, iobase+VLSI_PIO_IRCFG); /* disable everything */
1359 /* disable and w/c irqs */
1360 outb(0, iobase+VLSI_PIO_IRINTR);
1362 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR);
1363 spin_unlock_irqrestore(&idev->lock,flags);
1365 vlsi_unarm_tx(idev);
1366 vlsi_unarm_rx(idev);
1368 vlsi_clear_regs(iobase);
1369 vlsi_stop_clock(pdev);
1371 pci_disable_device(pdev);
1376 /**************************************************************/
1378 static struct net_device_stats * vlsi_get_stats(struct net_device *ndev)
1380 vlsi_irda_dev_t *idev = ndev->priv;
1382 return &idev->stats;
1385 static void vlsi_tx_timeout(struct net_device *ndev)
1387 vlsi_irda_dev_t *idev = ndev->priv;
1390 vlsi_reg_debug(ndev->base_addr, __FUNCTION__);
1391 vlsi_ring_debug(idev->tx_ring);
1393 if (netif_running(ndev))
1394 netif_stop_queue(ndev);
1398 /* now simply restart the whole thing */
1400 if (!idev->new_baud)
1401 idev->new_baud = idev->baud; /* keep current baudrate */
1403 if (vlsi_start_hw(idev))
1404 IRDA_ERROR("%s: failed to restart hw - %s(%s) unusable!\n",
1405 __FUNCTION__, PCIDEV_NAME(idev->pdev), ndev->name);
1407 netif_start_queue(ndev);
1410 static int vlsi_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1412 vlsi_irda_dev_t *idev = ndev->priv;
1413 struct if_irda_req *irq = (struct if_irda_req *) rq;
1414 unsigned long flags;
1419 case SIOCSBANDWIDTH:
1420 if (!capable(CAP_NET_ADMIN)) {
1424 spin_lock_irqsave(&idev->lock, flags);
1425 idev->new_baud = irq->ifr_baudrate;
1426 /* when called from userland there might be a minor race window here
1427 * if the stack tries to change speed concurrently - which would be
1428 * pretty strange anyway with the userland having full control...
1430 vlsi_set_baud(idev, ndev->base_addr);
1431 spin_unlock_irqrestore(&idev->lock, flags);
1433 case SIOCSMEDIABUSY:
1434 if (!capable(CAP_NET_ADMIN)) {
1438 irda_device_set_media_busy(ndev, TRUE);
1440 case SIOCGRECEIVING:
1441 /* the best we can do: check whether there are any bytes in rx fifo.
1442 * The trustable window (in case some data arrives just afterwards)
1443 * may be as short as 1usec or so at 4Mbps.
1445 fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1446 irq->ifr_receiving = (fifocnt!=0) ? 1 : 0;
1449 IRDA_WARNING("%s: notsupp - cmd=%04x\n",
1457 /********************************************************/
1459 static irqreturn_t vlsi_interrupt(int irq, void *dev_instance,
1460 struct pt_regs *regs)
1462 struct net_device *ndev = dev_instance;
1463 vlsi_irda_dev_t *idev = ndev->priv;
1467 unsigned long flags;
1470 iobase = ndev->base_addr;
1471 spin_lock_irqsave(&idev->lock,flags);
1473 irintr = inb(iobase+VLSI_PIO_IRINTR);
1475 outb(irintr, iobase+VLSI_PIO_IRINTR); /* acknowledge asap */
1477 if (!(irintr&=IRINTR_INT_MASK)) /* not our INT - probably shared */
1482 if (unlikely(!(irintr & ~IRINTR_ACTIVITY)))
1483 break; /* nothing todo if only activity */
1485 if (irintr&IRINTR_RPKTINT)
1486 vlsi_rx_interrupt(ndev);
1488 if (irintr&IRINTR_TPKTINT)
1489 vlsi_tx_interrupt(ndev);
1491 } while (--boguscount > 0);
1492 spin_unlock_irqrestore(&idev->lock,flags);
1494 if (boguscount <= 0)
1495 IRDA_MESSAGE("%s: too much work in interrupt!\n",
1497 return IRQ_RETVAL(handled);
1500 /********************************************************/
1502 static int vlsi_open(struct net_device *ndev)
1504 vlsi_irda_dev_t *idev = ndev->priv;
1508 if (pci_request_regions(idev->pdev, drivername)) {
1509 IRDA_WARNING("%s: io resource busy\n", __FUNCTION__);
1512 ndev->base_addr = pci_resource_start(idev->pdev,0);
1513 ndev->irq = idev->pdev->irq;
1515 /* under some rare occasions the chip apparently comes up with
1516 * IRQ's pending. We better w/c pending IRQ and disable them all
1519 outb(IRINTR_INT_MASK, ndev->base_addr+VLSI_PIO_IRINTR);
1521 if (request_irq(ndev->irq, vlsi_interrupt, SA_SHIRQ,
1522 drivername, ndev)) {
1523 IRDA_WARNING("%s: couldn't get IRQ: %d\n",
1524 __FUNCTION__, ndev->irq);
1528 if ((err = vlsi_create_hwif(idev)) != 0)
1531 sprintf(hwname, "VLSI-FIR @ 0x%04x", (unsigned)ndev->base_addr);
1532 idev->irlap = irlap_open(ndev,&idev->qos,hwname);
1534 goto errout_free_ring;
1536 do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
1538 idev->new_baud = 9600; /* start with IrPHY using 9600(SIR) mode */
1540 if ((err = vlsi_start_hw(idev)) != 0)
1541 goto errout_close_irlap;
1543 netif_start_queue(ndev);
1545 IRDA_MESSAGE("%s: device %s operational\n", __FUNCTION__, ndev->name);
1550 irlap_close(idev->irlap);
1552 vlsi_destroy_hwif(idev);
1554 free_irq(ndev->irq,ndev);
1556 pci_release_regions(idev->pdev);
1561 static int vlsi_close(struct net_device *ndev)
1563 vlsi_irda_dev_t *idev = ndev->priv;
1565 netif_stop_queue(ndev);
1568 irlap_close(idev->irlap);
1573 vlsi_destroy_hwif(idev);
1575 free_irq(ndev->irq,ndev);
1577 pci_release_regions(idev->pdev);
1579 IRDA_MESSAGE("%s: device %s stopped\n", __FUNCTION__, ndev->name);
1584 static int vlsi_irda_init(struct net_device *ndev)
1586 vlsi_irda_dev_t *idev = ndev->priv;
1587 struct pci_dev *pdev = idev->pdev;
1589 SET_MODULE_OWNER(ndev);
1591 ndev->irq = pdev->irq;
1592 ndev->base_addr = pci_resource_start(pdev,0);
1595 * see include file for details why we need these 2 masks, in this order!
1598 if (pci_set_dma_mask(pdev,DMA_MASK_USED_BY_HW)
1599 || pci_set_dma_mask(pdev,DMA_MASK_MSTRPAGE)) {
1600 IRDA_ERROR("%s: aborting due to PCI BM-DMA address limitations\n", __FUNCTION__);
1604 irda_init_max_qos_capabilies(&idev->qos);
1606 /* the VLSI82C147 does not support 576000! */
1608 idev->qos.baud_rate.bits = IR_2400 | IR_9600
1609 | IR_19200 | IR_38400 | IR_57600 | IR_115200
1610 | IR_1152000 | (IR_4000000 << 8);
1612 idev->qos.min_turn_time.bits = qos_mtt_bits;
1614 irda_qos_bits_to_value(&idev->qos);
1616 /* currently no public media definitions for IrDA */
1618 ndev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
1619 ndev->if_port = IF_PORT_UNKNOWN;
1621 ndev->open = vlsi_open;
1622 ndev->stop = vlsi_close;
1623 ndev->get_stats = vlsi_get_stats;
1624 ndev->hard_start_xmit = vlsi_hard_start_xmit;
1625 ndev->do_ioctl = vlsi_ioctl;
1626 ndev->tx_timeout = vlsi_tx_timeout;
1627 ndev->watchdog_timeo = 500*HZ/1000; /* max. allowed turn time for IrLAP */
1629 SET_NETDEV_DEV(ndev, &pdev->dev);
1634 /**************************************************************/
1636 static int __devinit
1637 vlsi_irda_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1639 struct net_device *ndev;
1640 vlsi_irda_dev_t *idev;
1642 if (pci_enable_device(pdev))
1645 pdev->current_state = 0; /* hw must be running now */
1647 IRDA_MESSAGE("%s: IrDA PCI controller %s detected\n",
1648 drivername, PCIDEV_NAME(pdev));
1650 if ( !pci_resource_start(pdev,0)
1651 || !(pci_resource_flags(pdev,0) & IORESOURCE_IO) ) {
1652 IRDA_ERROR("%s: bar 0 invalid", __FUNCTION__);
1656 ndev = alloc_irdadev(sizeof(*idev));
1658 IRDA_ERROR("%s: Unable to allocate device memory.\n",
1665 spin_lock_init(&idev->lock);
1666 init_MUTEX(&idev->sem);
1670 if (vlsi_irda_init(ndev) < 0)
1673 if (register_netdev(ndev) < 0) {
1674 IRDA_ERROR("%s: register_netdev failed\n", __FUNCTION__);
1678 if (vlsi_proc_root != NULL) {
1679 struct proc_dir_entry *ent;
1681 ent = create_proc_entry(ndev->name, S_IFREG|S_IRUGO, vlsi_proc_root);
1683 IRDA_WARNING("%s: failed to create proc entry\n",
1687 ent->proc_fops = VLSI_PROC_FOPS;
1690 idev->proc_entry = ent;
1692 IRDA_MESSAGE("%s: registered device %s\n", drivername, ndev->name);
1694 pci_set_drvdata(pdev, ndev);
1703 pci_disable_device(pdev);
1705 pci_set_drvdata(pdev, NULL);
1709 static void __devexit vlsi_irda_remove(struct pci_dev *pdev)
1711 struct net_device *ndev = pci_get_drvdata(pdev);
1712 vlsi_irda_dev_t *idev;
1715 IRDA_ERROR("%s: lost netdevice?\n", drivername);
1719 unregister_netdev(ndev);
1723 if (idev->proc_entry) {
1724 remove_proc_entry(ndev->name, vlsi_proc_root);
1725 idev->proc_entry = NULL;
1731 pci_set_drvdata(pdev, NULL);
1733 IRDA_MESSAGE("%s: %s removed\n", drivername, PCIDEV_NAME(pdev));
1738 /* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
1739 * Some of the Linux PCI-PM code however depends on this, for example in
1740 * pci_set_power_state(). So we have to take care to perform the required
1741 * operations on our own (particularly reflecting the pdev->current_state)
1742 * otherwise we might get cheated by pci-pm.
1746 static int vlsi_irda_suspend(struct pci_dev *pdev, pm_message_t state)
1748 struct net_device *ndev = pci_get_drvdata(pdev);
1749 vlsi_irda_dev_t *idev;
1752 IRDA_ERROR("%s - %s: no netdevice \n",
1753 __FUNCTION__, PCIDEV_NAME(pdev));
1758 if (pdev->current_state != 0) { /* already suspended */
1759 if (state.event > pdev->current_state) { /* simply go deeper */
1760 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1761 pdev->current_state = state.event;
1764 IRDA_ERROR("%s - %s: invalid suspend request %u -> %u\n", __FUNCTION__, PCIDEV_NAME(pdev), pdev->current_state, state.event);
1769 if (netif_running(ndev)) {
1770 netif_device_detach(ndev);
1772 pci_save_state(pdev);
1773 if (!idev->new_baud)
1774 /* remember speed settings to restore on resume */
1775 idev->new_baud = idev->baud;
1778 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1779 pdev->current_state = state.event;
1780 idev->resume_ok = 1;
1785 static int vlsi_irda_resume(struct pci_dev *pdev)
1787 struct net_device *ndev = pci_get_drvdata(pdev);
1788 vlsi_irda_dev_t *idev;
1791 IRDA_ERROR("%s - %s: no netdevice \n",
1792 __FUNCTION__, PCIDEV_NAME(pdev));
1797 if (pdev->current_state == 0) {
1799 IRDA_WARNING("%s - %s: already resumed\n",
1800 __FUNCTION__, PCIDEV_NAME(pdev));
1804 pci_set_power_state(pdev, PCI_D0);
1805 pdev->current_state = PM_EVENT_ON;
1807 if (!idev->resume_ok) {
1808 /* should be obsolete now - but used to happen due to:
1809 * - pci layer initially setting pdev->current_state = 4 (unknown)
1810 * - pci layer did not walk the save_state-tree (might be APM problem)
1811 * so we could not refuse to suspend from undefined state
1812 * - vlsi_irda_suspend detected invalid state and refused to save
1813 * configuration for resume - but was too late to stop suspending
1814 * - vlsi_irda_resume got screwed when trying to resume from garbage
1816 * now we explicitly set pdev->current_state = 0 after enabling the
1817 * device and independently resume_ok should catch any garbage config.
1819 IRDA_WARNING("%s - hm, nothing to resume?\n", __FUNCTION__);
1824 if (netif_running(ndev)) {
1825 pci_restore_state(pdev);
1826 vlsi_start_hw(idev);
1827 netif_device_attach(ndev);
1829 idev->resume_ok = 0;
1834 #endif /* CONFIG_PM */
1836 /*********************************************************/
1838 static struct pci_driver vlsi_irda_driver = {
1840 .id_table = vlsi_irda_table,
1841 .probe = vlsi_irda_probe,
1842 .remove = __devexit_p(vlsi_irda_remove),
1844 .suspend = vlsi_irda_suspend,
1845 .resume = vlsi_irda_resume,
1849 #define PROC_DIR ("driver/" DRIVER_NAME)
1851 static int __init vlsi_mod_init(void)
1855 if (clksrc < 0 || clksrc > 3) {
1856 IRDA_ERROR("%s: invalid clksrc=%d\n", drivername, clksrc);
1860 for (i = 0; i < 2; i++) {
1861 switch(ringsize[i]) {
1869 IRDA_WARNING("%s: invalid %s ringsize %d, using default=8", drivername, (i)?"rx":"tx", ringsize[i]);
1875 sirpulse = !!sirpulse;
1877 /* proc_mkdir returns NULL if !CONFIG_PROC_FS.
1878 * Failure to create the procfs entry is handled like running
1879 * without procfs - it's not required for the driver to work.
1881 vlsi_proc_root = proc_mkdir(PROC_DIR, NULL);
1882 if (vlsi_proc_root) {
1883 /* protect registered procdir against module removal.
1884 * Because we are in the module init path there's no race
1885 * window after create_proc_entry (and no barrier needed).
1887 vlsi_proc_root->owner = THIS_MODULE;
1890 ret = pci_module_init(&vlsi_irda_driver);
1892 if (ret && vlsi_proc_root)
1893 remove_proc_entry(PROC_DIR, NULL);
1898 static void __exit vlsi_mod_exit(void)
1900 pci_unregister_driver(&vlsi_irda_driver);
1902 remove_proc_entry(PROC_DIR, NULL);
1905 module_init(vlsi_mod_init);
1906 module_exit(vlsi_mod_exit);