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/module.h>
26 #define DRIVER_NAME "vlsi_ir"
27 #define DRIVER_VERSION "v0.5"
28 #define DRIVER_DESCRIPTION "IrDA SIR/MIR/FIR driver for VLSI 82C147"
29 #define DRIVER_AUTHOR "Martin Diehl <info@mdiehl.de>"
31 MODULE_DESCRIPTION(DRIVER_DESCRIPTION
);
32 MODULE_AUTHOR(DRIVER_AUTHOR
);
33 MODULE_LICENSE("GPL");
35 /********************************************************/
37 #include <linux/kernel.h>
38 #include <linux/init.h>
39 #include <linux/pci.h>
40 #include <linux/slab.h>
41 #include <linux/netdevice.h>
42 #include <linux/skbuff.h>
43 #include <linux/delay.h>
44 #include <linux/time.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/mutex.h>
48 #include <asm/uaccess.h>
49 #include <asm/byteorder.h>
51 #include <net/irda/irda.h>
52 #include <net/irda/irda_device.h>
53 #include <net/irda/wrapper.h>
54 #include <net/irda/crc.h>
58 /********************************************************/
60 static /* const */ char drivername
[] = DRIVER_NAME
;
62 static struct pci_device_id vlsi_irda_table
[] = {
64 .class = PCI_CLASS_WIRELESS_IRDA
<< 8,
65 .class_mask
= PCI_CLASS_SUBCLASS_MASK
<< 8,
66 .vendor
= PCI_VENDOR_ID_VLSI
,
67 .device
= PCI_DEVICE_ID_VLSI_82C147
,
68 .subvendor
= PCI_ANY_ID
,
69 .subdevice
= PCI_ANY_ID
,
74 MODULE_DEVICE_TABLE(pci
, vlsi_irda_table
);
76 /********************************************************/
78 /* clksrc: which clock source to be used
79 * 0: auto - try PLL, fallback to 40MHz XCLK
80 * 1: on-chip 48MHz PLL
81 * 2: external 48MHz XCLK
82 * 3: external 40MHz XCLK (HP OB-800)
85 static int clksrc
= 0; /* default is 0(auto) */
86 module_param(clksrc
, int, 0);
87 MODULE_PARM_DESC(clksrc
, "clock input source selection");
89 /* ringsize: size of the tx and rx descriptor rings
90 * independent for tx and rx
91 * specify as ringsize=tx[,rx]
92 * allowed values: 4, 8, 16, 32, 64
93 * Due to the IrDA 1.x max. allowed window size=7,
94 * there should be no gain when using rings larger than 8
97 static int ringsize
[] = {8,8}; /* default is tx=8 / rx=8 */
98 module_param_array(ringsize
, int, NULL
, 0);
99 MODULE_PARM_DESC(ringsize
, "TX, RX ring descriptor size");
101 /* sirpulse: tuning of the SIR pulse width within IrPHY 1.3 limits
102 * 0: very short, 1.5us (exception: 6us at 2.4 kbaud)
103 * 1: nominal 3/16 bittime width
104 * note: IrDA compliant peer devices should be happy regardless
105 * which one is used. Primary goal is to save some power
106 * on the sender's side - at 9.6kbaud for example the short
107 * pulse width saves more than 90% of the transmitted IR power.
110 static int sirpulse
= 1; /* default is 3/16 bittime */
111 module_param(sirpulse
, int, 0);
112 MODULE_PARM_DESC(sirpulse
, "SIR pulse width tuning");
114 /* qos_mtt_bits: encoded min-turn-time value we require the peer device
115 * to use before transmitting to us. "Type 1" (per-station)
116 * bitfield according to IrLAP definition (section 6.6.8)
117 * Don't know which transceiver is used by my OB800 - the
118 * pretty common HP HDLS-1100 requires 1 msec - so lets use this.
121 static int qos_mtt_bits
= 0x07; /* default is 1 ms or more */
122 module_param(qos_mtt_bits
, int, 0);
123 MODULE_PARM_DESC(qos_mtt_bits
, "IrLAP bitfield representing min-turn-time");
125 /********************************************************/
127 static void vlsi_reg_debug(unsigned iobase
, const char *s
)
131 printk(KERN_DEBUG
"%s: ", s
);
132 for (i
= 0; i
< 0x20; i
++)
133 printk("%02x", (unsigned)inb((iobase
+i
)));
137 static void vlsi_ring_debug(struct vlsi_ring
*r
)
139 struct ring_descr
*rd
;
142 printk(KERN_DEBUG
"%s - ring %p / size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
143 __func__
, r
, r
->size
, r
->mask
, r
->len
, r
->dir
, r
->rd
[0].hw
);
144 printk(KERN_DEBUG
"%s - head = %d / tail = %d\n", __func__
,
145 atomic_read(&r
->head
) & r
->mask
, atomic_read(&r
->tail
) & r
->mask
);
146 for (i
= 0; i
< r
->size
; i
++) {
148 printk(KERN_DEBUG
"%s - ring descr %u: ", __func__
, i
);
149 printk("skb=%p data=%p hw=%p\n", rd
->skb
, rd
->buf
, rd
->hw
);
150 printk(KERN_DEBUG
"%s - hw: status=%02x count=%u addr=0x%08x\n",
151 __func__
, (unsigned) rd_get_status(rd
),
152 (unsigned) rd_get_count(rd
), (unsigned) rd_get_addr(rd
));
156 /********************************************************/
158 /* needed regardless of CONFIG_PROC_FS */
159 static struct proc_dir_entry
*vlsi_proc_root
= NULL
;
161 #ifdef CONFIG_PROC_FS
163 static void vlsi_proc_pdev(struct seq_file
*seq
, struct pci_dev
*pdev
)
165 unsigned iobase
= pci_resource_start(pdev
, 0);
168 seq_printf(seq
, "\n%s (vid/did: [%04x:%04x])\n",
169 pci_name(pdev
), (int)pdev
->vendor
, (int)pdev
->device
);
170 seq_printf(seq
, "pci-power-state: %u\n", (unsigned) pdev
->current_state
);
171 seq_printf(seq
, "resources: irq=%u / io=0x%04x / dma_mask=0x%016Lx\n",
172 pdev
->irq
, (unsigned)pci_resource_start(pdev
, 0), (unsigned long long)pdev
->dma_mask
);
173 seq_printf(seq
, "hw registers: ");
174 for (i
= 0; i
< 0x20; i
++)
175 seq_printf(seq
, "%02x", (unsigned)inb((iobase
+i
)));
176 seq_printf(seq
, "\n");
179 static void vlsi_proc_ndev(struct seq_file
*seq
, struct net_device
*ndev
)
181 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
184 unsigned delta1
, delta2
;
186 unsigned iobase
= ndev
->base_addr
;
188 seq_printf(seq
, "\n%s link state: %s / %s / %s / %s\n", ndev
->name
,
189 netif_device_present(ndev
) ? "attached" : "detached",
190 netif_running(ndev
) ? "running" : "not running",
191 netif_carrier_ok(ndev
) ? "carrier ok" : "no carrier",
192 netif_queue_stopped(ndev
) ? "queue stopped" : "queue running");
194 if (!netif_running(ndev
))
197 seq_printf(seq
, "\nhw-state:\n");
198 pci_read_config_byte(idev
->pdev
, VLSI_PCI_IRMISC
, &byte
);
199 seq_printf(seq
, "IRMISC:%s%s%s uart%s",
200 (byte
&IRMISC_IRRAIL
) ? " irrail" : "",
201 (byte
&IRMISC_IRPD
) ? " irpd" : "",
202 (byte
&IRMISC_UARTTST
) ? " uarttest" : "",
203 (byte
&IRMISC_UARTEN
) ? "@" : " disabled\n");
204 if (byte
&IRMISC_UARTEN
) {
205 seq_printf(seq
, "0x%s\n",
206 (byte
&2) ? ((byte
&1) ? "3e8" : "2e8")
207 : ((byte
&1) ? "3f8" : "2f8"));
209 pci_read_config_byte(idev
->pdev
, VLSI_PCI_CLKCTL
, &byte
);
210 seq_printf(seq
, "CLKCTL: PLL %s%s%s / clock %s / wakeup %s\n",
211 (byte
&CLKCTL_PD_INV
) ? "powered" : "down",
212 (byte
&CLKCTL_LOCK
) ? " locked" : "",
213 (byte
&CLKCTL_EXTCLK
) ? ((byte
&CLKCTL_XCKSEL
)?" / 40 MHz XCLK":" / 48 MHz XCLK") : "",
214 (byte
&CLKCTL_CLKSTP
) ? "stopped" : "running",
215 (byte
&CLKCTL_WAKE
) ? "enabled" : "disabled");
216 pci_read_config_byte(idev
->pdev
, VLSI_PCI_MSTRPAGE
, &byte
);
217 seq_printf(seq
, "MSTRPAGE: 0x%02x\n", (unsigned)byte
);
219 byte
= inb(iobase
+VLSI_PIO_IRINTR
);
220 seq_printf(seq
, "IRINTR:%s%s%s%s%s%s%s%s\n",
221 (byte
&IRINTR_ACTEN
) ? " ACTEN" : "",
222 (byte
&IRINTR_RPKTEN
) ? " RPKTEN" : "",
223 (byte
&IRINTR_TPKTEN
) ? " TPKTEN" : "",
224 (byte
&IRINTR_OE_EN
) ? " OE_EN" : "",
225 (byte
&IRINTR_ACTIVITY
) ? " ACTIVITY" : "",
226 (byte
&IRINTR_RPKTINT
) ? " RPKTINT" : "",
227 (byte
&IRINTR_TPKTINT
) ? " TPKTINT" : "",
228 (byte
&IRINTR_OE_INT
) ? " OE_INT" : "");
229 word
= inw(iobase
+VLSI_PIO_RINGPTR
);
230 seq_printf(seq
, "RINGPTR: rx=%u / tx=%u\n", RINGPTR_GET_RX(word
), RINGPTR_GET_TX(word
));
231 word
= inw(iobase
+VLSI_PIO_RINGBASE
);
232 seq_printf(seq
, "RINGBASE: busmap=0x%08x\n",
233 ((unsigned)word
<< 10)|(MSTRPAGE_VALUE
<<24));
234 word
= inw(iobase
+VLSI_PIO_RINGSIZE
);
235 seq_printf(seq
, "RINGSIZE: rx=%u / tx=%u\n", RINGSIZE_TO_RXSIZE(word
),
236 RINGSIZE_TO_TXSIZE(word
));
238 word
= inw(iobase
+VLSI_PIO_IRCFG
);
239 seq_printf(seq
, "IRCFG:%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
240 (word
&IRCFG_LOOP
) ? " LOOP" : "",
241 (word
&IRCFG_ENTX
) ? " ENTX" : "",
242 (word
&IRCFG_ENRX
) ? " ENRX" : "",
243 (word
&IRCFG_MSTR
) ? " MSTR" : "",
244 (word
&IRCFG_RXANY
) ? " RXANY" : "",
245 (word
&IRCFG_CRC16
) ? " CRC16" : "",
246 (word
&IRCFG_FIR
) ? " FIR" : "",
247 (word
&IRCFG_MIR
) ? " MIR" : "",
248 (word
&IRCFG_SIR
) ? " SIR" : "",
249 (word
&IRCFG_SIRFILT
) ? " SIRFILT" : "",
250 (word
&IRCFG_SIRTEST
) ? " SIRTEST" : "",
251 (word
&IRCFG_TXPOL
) ? " TXPOL" : "",
252 (word
&IRCFG_RXPOL
) ? " RXPOL" : "");
253 word
= inw(iobase
+VLSI_PIO_IRENABLE
);
254 seq_printf(seq
, "IRENABLE:%s%s%s%s%s%s%s%s\n",
255 (word
&IRENABLE_PHYANDCLOCK
) ? " PHYANDCLOCK" : "",
256 (word
&IRENABLE_CFGER
) ? " CFGERR" : "",
257 (word
&IRENABLE_FIR_ON
) ? " FIR_ON" : "",
258 (word
&IRENABLE_MIR_ON
) ? " MIR_ON" : "",
259 (word
&IRENABLE_SIR_ON
) ? " SIR_ON" : "",
260 (word
&IRENABLE_ENTXST
) ? " ENTXST" : "",
261 (word
&IRENABLE_ENRXST
) ? " ENRXST" : "",
262 (word
&IRENABLE_CRC16_ON
) ? " CRC16_ON" : "");
263 word
= inw(iobase
+VLSI_PIO_PHYCTL
);
264 seq_printf(seq
, "PHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
265 (unsigned)PHYCTL_TO_BAUD(word
),
266 (unsigned)PHYCTL_TO_PLSWID(word
),
267 (unsigned)PHYCTL_TO_PREAMB(word
));
268 word
= inw(iobase
+VLSI_PIO_NPHYCTL
);
269 seq_printf(seq
, "NPHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
270 (unsigned)PHYCTL_TO_BAUD(word
),
271 (unsigned)PHYCTL_TO_PLSWID(word
),
272 (unsigned)PHYCTL_TO_PREAMB(word
));
273 word
= inw(iobase
+VLSI_PIO_MAXPKT
);
274 seq_printf(seq
, "MAXPKT: max. rx packet size = %u\n", word
);
275 word
= inw(iobase
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
276 seq_printf(seq
, "RCVBCNT: rx-fifo filling level = %u\n", word
);
278 seq_printf(seq
, "\nsw-state:\n");
279 seq_printf(seq
, "IrPHY setup: %d baud - %s encoding\n", idev
->baud
,
280 (idev
->mode
==IFF_SIR
)?"SIR":((idev
->mode
==IFF_MIR
)?"MIR":"FIR"));
281 do_gettimeofday(&now
);
282 if (now
.tv_usec
>= idev
->last_rx
.tv_usec
) {
283 delta2
= now
.tv_usec
- idev
->last_rx
.tv_usec
;
287 delta2
= 1000000 + now
.tv_usec
- idev
->last_rx
.tv_usec
;
290 seq_printf(seq
, "last rx: %lu.%06u sec\n",
291 now
.tv_sec
- idev
->last_rx
.tv_sec
- delta1
, delta2
);
293 seq_printf(seq
, "RX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu",
294 ndev
->stats
.rx_packets
, ndev
->stats
.rx_bytes
, ndev
->stats
.rx_errors
,
295 ndev
->stats
.rx_dropped
);
296 seq_printf(seq
, " / overrun=%lu / length=%lu / frame=%lu / crc=%lu\n",
297 ndev
->stats
.rx_over_errors
, ndev
->stats
.rx_length_errors
,
298 ndev
->stats
.rx_frame_errors
, ndev
->stats
.rx_crc_errors
);
299 seq_printf(seq
, "TX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu / fifo=%lu\n",
300 ndev
->stats
.tx_packets
, ndev
->stats
.tx_bytes
, ndev
->stats
.tx_errors
,
301 ndev
->stats
.tx_dropped
, ndev
->stats
.tx_fifo_errors
);
305 static void vlsi_proc_ring(struct seq_file
*seq
, struct vlsi_ring
*r
)
307 struct ring_descr
*rd
;
311 seq_printf(seq
, "size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
312 r
->size
, r
->mask
, r
->len
, r
->dir
, r
->rd
[0].hw
);
313 h
= atomic_read(&r
->head
) & r
->mask
;
314 t
= atomic_read(&r
->tail
) & r
->mask
;
315 seq_printf(seq
, "head = %d / tail = %d ", h
, t
);
317 seq_printf(seq
, "(empty)\n");
319 if (((t
+1)&r
->mask
) == h
)
320 seq_printf(seq
, "(full)\n");
322 seq_printf(seq
, "(level = %d)\n", ((unsigned)(t
-h
) & r
->mask
));
324 j
= (unsigned) rd_get_count(rd
);
325 seq_printf(seq
, "current: rd = %d / status = %02x / len = %u\n",
326 h
, (unsigned)rd_get_status(rd
), j
);
328 seq_printf(seq
, " data:");
331 for (i
= 0; i
< j
; i
++)
332 seq_printf(seq
, " %02x", (unsigned)((unsigned char *)rd
->buf
)[i
]);
333 seq_printf(seq
, "\n");
336 for (i
= 0; i
< r
->size
; i
++) {
338 seq_printf(seq
, "> ring descr %u: ", i
);
339 seq_printf(seq
, "skb=%p data=%p hw=%p\n", rd
->skb
, rd
->buf
, rd
->hw
);
340 seq_printf(seq
, " hw: status=%02x count=%u busaddr=0x%08x\n",
341 (unsigned) rd_get_status(rd
),
342 (unsigned) rd_get_count(rd
), (unsigned) rd_get_addr(rd
));
346 static int vlsi_seq_show(struct seq_file
*seq
, void *v
)
348 struct net_device
*ndev
= seq
->private;
349 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
352 seq_printf(seq
, "\n%s %s\n\n", DRIVER_NAME
, DRIVER_VERSION
);
353 seq_printf(seq
, "clksrc: %s\n",
354 (clksrc
>=2) ? ((clksrc
==3)?"40MHz XCLK":"48MHz XCLK")
355 : ((clksrc
==1)?"48MHz PLL":"autodetect"));
356 seq_printf(seq
, "ringsize: tx=%d / rx=%d\n",
357 ringsize
[0], ringsize
[1]);
358 seq_printf(seq
, "sirpulse: %s\n", (sirpulse
)?"3/16 bittime":"short");
359 seq_printf(seq
, "qos_mtt_bits: 0x%02x\n", (unsigned)qos_mtt_bits
);
361 spin_lock_irqsave(&idev
->lock
, flags
);
362 if (idev
->pdev
!= NULL
) {
363 vlsi_proc_pdev(seq
, idev
->pdev
);
365 if (idev
->pdev
->current_state
== 0)
366 vlsi_proc_ndev(seq
, ndev
);
368 seq_printf(seq
, "\nPCI controller down - resume_ok = %d\n",
370 if (netif_running(ndev
) && idev
->rx_ring
&& idev
->tx_ring
) {
371 seq_printf(seq
, "\n--------- RX ring -----------\n\n");
372 vlsi_proc_ring(seq
, idev
->rx_ring
);
373 seq_printf(seq
, "\n--------- TX ring -----------\n\n");
374 vlsi_proc_ring(seq
, idev
->tx_ring
);
377 seq_printf(seq
, "\n");
378 spin_unlock_irqrestore(&idev
->lock
, flags
);
383 static int vlsi_seq_open(struct inode
*inode
, struct file
*file
)
385 return single_open(file
, vlsi_seq_show
, PDE(inode
)->data
);
388 static const struct file_operations vlsi_proc_fops
= {
389 .owner
= THIS_MODULE
,
390 .open
= vlsi_seq_open
,
393 .release
= single_release
,
396 #define VLSI_PROC_FOPS (&vlsi_proc_fops)
398 #else /* CONFIG_PROC_FS */
399 #define VLSI_PROC_FOPS NULL
402 /********************************************************/
404 static struct vlsi_ring
*vlsi_alloc_ring(struct pci_dev
*pdev
, struct ring_descr_hw
*hwmap
,
405 unsigned size
, unsigned len
, int dir
)
408 struct ring_descr
*rd
;
412 if (!size
|| ((size
-1)&size
)!=0) /* must be >0 and power of 2 */
415 r
= kmalloc(sizeof(*r
) + size
* sizeof(struct ring_descr
), GFP_KERNEL
);
418 memset(r
, 0, sizeof(*r
));
423 r
->rd
= (struct ring_descr
*)(r
+1);
426 atomic_set(&r
->head
, 0);
427 atomic_set(&r
->tail
, 0);
429 for (i
= 0; i
< size
; i
++) {
431 memset(rd
, 0, sizeof(*rd
));
433 rd
->buf
= kmalloc(len
, GFP_KERNEL
|GFP_DMA
);
435 || !(busaddr
= pci_map_single(pdev
, rd
->buf
, len
, dir
))) {
437 IRDA_ERROR("%s: failed to create PCI-MAP for %p",
442 for (j
= 0; j
< i
; j
++) {
444 busaddr
= rd_get_addr(rd
);
445 rd_set_addr_status(rd
, 0, 0);
447 pci_unmap_single(pdev
, busaddr
, len
, dir
);
454 rd_set_addr_status(rd
, busaddr
, 0);
455 /* initially, the dma buffer is owned by the CPU */
461 static int vlsi_free_ring(struct vlsi_ring
*r
)
463 struct ring_descr
*rd
;
467 for (i
= 0; i
< r
->size
; i
++) {
470 dev_kfree_skb_any(rd
->skb
);
471 busaddr
= rd_get_addr(rd
);
472 rd_set_addr_status(rd
, 0, 0);
474 pci_unmap_single(r
->pdev
, busaddr
, r
->len
, r
->dir
);
481 static int vlsi_create_hwif(vlsi_irda_dev_t
*idev
)
484 struct ring_descr_hw
*hwmap
;
486 idev
->virtaddr
= NULL
;
489 ringarea
= pci_alloc_consistent(idev
->pdev
, HW_RING_AREA_SIZE
, &idev
->busaddr
);
491 IRDA_ERROR("%s: insufficient memory for descriptor rings\n",
495 memset(ringarea
, 0, HW_RING_AREA_SIZE
);
497 hwmap
= (struct ring_descr_hw
*)ringarea
;
498 idev
->rx_ring
= vlsi_alloc_ring(idev
->pdev
, hwmap
, ringsize
[1],
499 XFER_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
500 if (idev
->rx_ring
== NULL
)
503 hwmap
+= MAX_RING_DESCR
;
504 idev
->tx_ring
= vlsi_alloc_ring(idev
->pdev
, hwmap
, ringsize
[0],
505 XFER_BUF_SIZE
, PCI_DMA_TODEVICE
);
506 if (idev
->tx_ring
== NULL
)
509 idev
->virtaddr
= ringarea
;
513 vlsi_free_ring(idev
->rx_ring
);
515 idev
->rx_ring
= idev
->tx_ring
= NULL
;
516 pci_free_consistent(idev
->pdev
, HW_RING_AREA_SIZE
, ringarea
, idev
->busaddr
);
522 static int vlsi_destroy_hwif(vlsi_irda_dev_t
*idev
)
524 vlsi_free_ring(idev
->rx_ring
);
525 vlsi_free_ring(idev
->tx_ring
);
526 idev
->rx_ring
= idev
->tx_ring
= NULL
;
529 pci_free_consistent(idev
->pdev
,HW_RING_AREA_SIZE
,idev
->virtaddr
,idev
->busaddr
);
531 idev
->virtaddr
= NULL
;
537 /********************************************************/
539 static int vlsi_process_rx(struct vlsi_ring
*r
, struct ring_descr
*rd
)
545 struct net_device
*ndev
= (struct net_device
*)pci_get_drvdata(r
->pdev
);
546 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
548 pci_dma_sync_single_for_cpu(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
549 /* dma buffer now owned by the CPU */
550 status
= rd_get_status(rd
);
551 if (status
& RD_RX_ERROR
) {
552 if (status
& RD_RX_OVER
)
554 if (status
& RD_RX_LENGTH
)
555 ret
|= VLSI_RX_LENGTH
;
556 if (status
& RD_RX_PHYERR
)
557 ret
|= VLSI_RX_FRAME
;
558 if (status
& RD_RX_CRCERR
)
563 len
= rd_get_count(rd
);
564 crclen
= (idev
->mode
==IFF_FIR
) ? sizeof(u32
) : sizeof(u16
);
565 len
-= crclen
; /* remove trailing CRC */
567 IRDA_DEBUG(0, "%s: strange frame (len=%d)\n", __func__
, len
);
572 if (idev
->mode
== IFF_SIR
) { /* hw checks CRC in MIR, FIR mode */
574 /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
575 * endian-adjustment there just in place will dirty a cache line
576 * which belongs to the map and thus we must be sure it will
577 * get flushed before giving the buffer back to hardware.
578 * vlsi_fill_rx() will do this anyway - but here we rely on.
580 le16_to_cpus(rd
->buf
+len
);
581 if (irda_calc_crc16(INIT_FCS
,rd
->buf
,len
+crclen
) != GOOD_FCS
) {
582 IRDA_DEBUG(0, "%s: crc error\n", __func__
);
589 IRDA_WARNING("%s: rx packet lost\n", __func__
);
597 memcpy(skb_put(skb
,len
), rd
->buf
, len
);
598 skb_reset_mac_header(skb
);
605 rd_set_status(rd
, 0);
607 /* buffer still owned by CPU */
609 return (ret
) ? -ret
: len
;
612 static void vlsi_fill_rx(struct vlsi_ring
*r
)
614 struct ring_descr
*rd
;
616 for (rd
= ring_last(r
); rd
!= NULL
; rd
= ring_put(r
)) {
617 if (rd_is_active(rd
)) {
618 IRDA_WARNING("%s: driver bug: rx descr race with hw\n",
624 rd
->skb
= dev_alloc_skb(IRLAP_SKB_ALLOCSIZE
);
626 skb_reserve(rd
->skb
,1);
627 rd
->skb
->protocol
= htons(ETH_P_IRDA
);
630 break; /* probably not worth logging? */
632 /* give dma buffer back to busmaster */
633 pci_dma_sync_single_for_device(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
638 static void vlsi_rx_interrupt(struct net_device
*ndev
)
640 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
641 struct vlsi_ring
*r
= idev
->rx_ring
;
642 struct ring_descr
*rd
;
645 for (rd
= ring_first(r
); rd
!= NULL
; rd
= ring_get(r
)) {
647 if (rd_is_active(rd
))
650 ret
= vlsi_process_rx(r
, rd
);
654 ndev
->stats
.rx_errors
++;
655 if (ret
& VLSI_RX_DROP
)
656 ndev
->stats
.rx_dropped
++;
657 if (ret
& VLSI_RX_OVER
)
658 ndev
->stats
.rx_over_errors
++;
659 if (ret
& VLSI_RX_LENGTH
)
660 ndev
->stats
.rx_length_errors
++;
661 if (ret
& VLSI_RX_FRAME
)
662 ndev
->stats
.rx_frame_errors
++;
663 if (ret
& VLSI_RX_CRC
)
664 ndev
->stats
.rx_crc_errors
++;
667 ndev
->stats
.rx_packets
++;
668 ndev
->stats
.rx_bytes
+= ret
;
672 do_gettimeofday(&idev
->last_rx
); /* remember "now" for later mtt delay */
676 if (ring_first(r
) == NULL
) {
677 /* we are in big trouble, if this should ever happen */
678 IRDA_ERROR("%s: rx ring exhausted!\n", __func__
);
682 outw(0, ndev
->base_addr
+VLSI_PIO_PROMPT
);
685 /* caller must have stopped the controller from busmastering */
687 static void vlsi_unarm_rx(vlsi_irda_dev_t
*idev
)
689 struct net_device
*ndev
= pci_get_drvdata(idev
->pdev
);
690 struct vlsi_ring
*r
= idev
->rx_ring
;
691 struct ring_descr
*rd
;
694 for (rd
= ring_first(r
); rd
!= NULL
; rd
= ring_get(r
)) {
697 if (rd_is_active(rd
)) {
698 rd_set_status(rd
, 0);
699 if (rd_get_count(rd
)) {
700 IRDA_DEBUG(0, "%s - dropping rx packet\n", __func__
);
704 pci_dma_sync_single_for_cpu(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
706 dev_kfree_skb_any(rd
->skb
);
711 ret
= vlsi_process_rx(r
, rd
);
715 ndev
->stats
.rx_errors
++;
716 if (ret
& VLSI_RX_DROP
)
717 ndev
->stats
.rx_dropped
++;
718 if (ret
& VLSI_RX_OVER
)
719 ndev
->stats
.rx_over_errors
++;
720 if (ret
& VLSI_RX_LENGTH
)
721 ndev
->stats
.rx_length_errors
++;
722 if (ret
& VLSI_RX_FRAME
)
723 ndev
->stats
.rx_frame_errors
++;
724 if (ret
& VLSI_RX_CRC
)
725 ndev
->stats
.rx_crc_errors
++;
728 ndev
->stats
.rx_packets
++;
729 ndev
->stats
.rx_bytes
+= ret
;
734 /********************************************************/
736 static int vlsi_process_tx(struct vlsi_ring
*r
, struct ring_descr
*rd
)
742 pci_dma_sync_single_for_cpu(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
743 /* dma buffer now owned by the CPU */
744 status
= rd_get_status(rd
);
745 if (status
& RD_TX_UNDRN
)
749 rd_set_status(rd
, 0);
753 dev_kfree_skb_any(rd
->skb
);
756 else /* tx-skb already freed? - should never happen */
757 len
= rd_get_count(rd
); /* incorrect for SIR! (due to wrapping) */
760 /* dma buffer still owned by the CPU */
762 return (ret
) ? -ret
: len
;
765 static int vlsi_set_baud(vlsi_irda_dev_t
*idev
, unsigned iobase
)
774 baudrate
= idev
->new_baud
;
775 IRDA_DEBUG(2, "%s: %d -> %d\n", __func__
, idev
->baud
, idev
->new_baud
);
776 if (baudrate
== 4000000) {
779 nphyctl
= PHYCTL_FIR
;
781 else if (baudrate
== 1152000) {
783 config
= IRCFG_MIR
| IRCFG_CRC16
;
784 nphyctl
= PHYCTL_MIR(clksrc
==3);
788 config
= IRCFG_SIR
| IRCFG_SIRFILT
| IRCFG_RXANY
;
791 IRDA_WARNING("%s: undefined baudrate %d - fallback to 9600!\n",
801 nphyctl
= PHYCTL_SIR(baudrate
,sirpulse
,clksrc
==3);
805 config
|= IRCFG_MSTR
| IRCFG_ENRX
;
807 fifocnt
= inw(iobase
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
809 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __func__
, fifocnt
);
812 outw(0, iobase
+VLSI_PIO_IRENABLE
);
813 outw(config
, iobase
+VLSI_PIO_IRCFG
);
814 outw(nphyctl
, iobase
+VLSI_PIO_NPHYCTL
);
816 outw(IRENABLE_PHYANDCLOCK
, iobase
+VLSI_PIO_IRENABLE
);
819 udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
821 /* read back settings for validation */
823 config
= inw(iobase
+VLSI_PIO_IRENABLE
) & IRENABLE_MASK
;
826 config
^= IRENABLE_FIR_ON
;
827 else if (mode
== IFF_MIR
)
828 config
^= (IRENABLE_MIR_ON
|IRENABLE_CRC16_ON
);
830 config
^= IRENABLE_SIR_ON
;
832 if (config
!= (IRENABLE_PHYANDCLOCK
|IRENABLE_ENRXST
)) {
833 IRDA_WARNING("%s: failed to set %s mode!\n", __func__
,
834 (mode
==IFF_SIR
)?"SIR":((mode
==IFF_MIR
)?"MIR":"FIR"));
838 if (inw(iobase
+VLSI_PIO_PHYCTL
) != nphyctl
) {
839 IRDA_WARNING("%s: failed to apply baudrate %d\n",
845 idev
->baud
= baudrate
;
852 vlsi_reg_debug(iobase
,__func__
);
857 static int vlsi_hard_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
859 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
860 struct vlsi_ring
*r
= idev
->tx_ring
;
861 struct ring_descr
*rd
;
863 unsigned iobase
= ndev
->base_addr
;
868 struct timeval now
, ready
;
871 speed
= irda_get_next_speed(skb
);
872 spin_lock_irqsave(&idev
->lock
, flags
);
873 if (speed
!= -1 && speed
!= idev
->baud
) {
874 netif_stop_queue(ndev
);
875 idev
->new_baud
= speed
;
876 status
= RD_TX_CLRENTX
; /* stop tx-ring after this frame */
882 /* handle zero packets - should be speed change */
884 msg
= "bogus zero-length packet";
888 /* due to the completely asynch tx operation we might have
889 * IrLAP racing with the hardware here, f.e. if the controller
890 * is just sending the last packet with current speed while
891 * the LAP is already switching the speed using synchronous
892 * len=0 packet. Immediate execution would lead to hw lockup
893 * requiring a powercycle to reset. Good candidate to trigger
894 * this is the final UA:RSP packet after receiving a DISC:CMD
895 * when getting the LAP down.
896 * Note that we are not protected by the queue_stop approach
897 * because the final UA:RSP arrives _without_ request to apply
898 * new-speed-after-this-packet - hence the driver doesn't know
899 * this was the last packet and doesn't stop the queue. So the
900 * forced switch to default speed from LAP gets through as fast
901 * as only some 10 usec later while the UA:RSP is still processed
902 * by the hardware and we would get screwed.
905 if (ring_first(idev
->tx_ring
) == NULL
) {
906 /* no race - tx-ring already empty */
907 vlsi_set_baud(idev
, iobase
);
908 netif_wake_queue(ndev
);
912 /* keep the speed change pending like it would
913 * for any len>0 packet. tx completion interrupt
914 * will apply it when the tx ring becomes empty.
916 spin_unlock_irqrestore(&idev
->lock
, flags
);
917 dev_kfree_skb_any(skb
);
921 /* sanity checks - simply drop the packet */
925 msg
= "ring full, but queue wasn't stopped";
929 if (rd_is_active(rd
)) {
930 msg
= "entry still owned by hw";
935 msg
= "tx ring entry without pci buffer";
940 msg
= "ring entry with old skb still attached";
944 /* no need for serialization or interrupt disable during mtt */
945 spin_unlock_irqrestore(&idev
->lock
, flags
);
947 if ((mtt
= irda_get_mtt(skb
)) > 0) {
949 ready
.tv_usec
= idev
->last_rx
.tv_usec
+ mtt
;
950 ready
.tv_sec
= idev
->last_rx
.tv_sec
;
951 if (ready
.tv_usec
>= 1000000) {
952 ready
.tv_usec
-= 1000000;
953 ready
.tv_sec
++; /* IrLAP 1.1: mtt always < 1 sec */
956 do_gettimeofday(&now
);
957 if (now
.tv_sec
> ready
.tv_sec
958 || (now
.tv_sec
==ready
.tv_sec
&& now
.tv_usec
>=ready
.tv_usec
))
961 /* must not sleep here - called under netif_tx_lock! */
965 /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
966 * after subsequent tx-completion
969 if (idev
->mode
== IFF_SIR
) {
970 status
|= RD_TX_DISCRC
; /* no hw-crc creation */
971 len
= async_wrap_skb(skb
, rd
->buf
, r
->len
);
973 /* Some rare worst case situation in SIR mode might lead to
974 * potential buffer overflow. The wrapper detects this, returns
975 * with a shortened frame (without FCS/EOF) but doesn't provide
976 * any error indication about the invalid packet which we are
978 * Therefore we log if the buffer got filled to the point, where the
979 * wrapper would abort, i.e. when there are less than 5 bytes left to
980 * allow appending the FCS/EOF.
984 IRDA_WARNING("%s: possible buffer overflow with SIR wrapping!\n",
988 /* hw deals with MIR/FIR mode wrapping */
989 status
|= RD_TX_PULSE
; /* send 2 us highspeed indication pulse */
992 msg
= "frame exceeds tx buffer length";
996 skb_copy_from_linear_data(skb
, rd
->buf
, len
);
999 rd
->skb
= skb
; /* remember skb for tx-complete stats */
1001 rd_set_count(rd
, len
);
1002 rd_set_status(rd
, status
); /* not yet active! */
1004 /* give dma buffer back to busmaster-hw (flush caches to make
1005 * CPU-driven changes visible from the pci bus).
1008 pci_dma_sync_single_for_device(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
1010 /* Switching to TX mode here races with the controller
1011 * which may stop TX at any time when fetching an inactive descriptor
1012 * or one with CLR_ENTX set. So we switch on TX only, if TX was not running
1013 * _after_ the new descriptor was activated on the ring. This ensures
1014 * we will either find TX already stopped or we can be sure, there
1015 * will be a TX-complete interrupt even if the chip stopped doing
1016 * TX just after we found it still running. The ISR will then find
1017 * the non-empty ring and restart TX processing. The enclosing
1018 * spinlock provides the correct serialization to prevent race with isr.
1021 spin_lock_irqsave(&idev
->lock
,flags
);
1025 if (!(inw(iobase
+VLSI_PIO_IRENABLE
) & IRENABLE_ENTXST
)) {
1028 fifocnt
= inw(ndev
->base_addr
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
1030 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __func__
, fifocnt
);
1033 config
= inw(iobase
+VLSI_PIO_IRCFG
);
1035 outw(config
| IRCFG_ENTX
, iobase
+VLSI_PIO_IRCFG
);
1037 outw(0, iobase
+VLSI_PIO_PROMPT
);
1039 ndev
->trans_start
= jiffies
;
1041 if (ring_put(r
) == NULL
) {
1042 netif_stop_queue(ndev
);
1043 IRDA_DEBUG(3, "%s: tx ring full - queue stopped\n", __func__
);
1045 spin_unlock_irqrestore(&idev
->lock
, flags
);
1050 spin_unlock_irqrestore(&idev
->lock
, flags
);
1052 IRDA_WARNING("%s: dropping packet - %s\n", __func__
, msg
);
1053 dev_kfree_skb_any(skb
);
1054 ndev
->stats
.tx_errors
++;
1055 ndev
->stats
.tx_dropped
++;
1056 /* Don't even think about returning NET_XMIT_DROP (=1) here!
1057 * In fact any retval!=0 causes the packet scheduler to requeue the
1058 * packet for later retry of transmission - which isn't exactly
1059 * what we want after we've just called dev_kfree_skb_any ;-)
1064 static void vlsi_tx_interrupt(struct net_device
*ndev
)
1066 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1067 struct vlsi_ring
*r
= idev
->tx_ring
;
1068 struct ring_descr
*rd
;
1073 for (rd
= ring_first(r
); rd
!= NULL
; rd
= ring_get(r
)) {
1075 if (rd_is_active(rd
))
1078 ret
= vlsi_process_tx(r
, rd
);
1082 ndev
->stats
.tx_errors
++;
1083 if (ret
& VLSI_TX_DROP
)
1084 ndev
->stats
.tx_dropped
++;
1085 if (ret
& VLSI_TX_FIFO
)
1086 ndev
->stats
.tx_fifo_errors
++;
1089 ndev
->stats
.tx_packets
++;
1090 ndev
->stats
.tx_bytes
+= ret
;
1094 iobase
= ndev
->base_addr
;
1096 if (idev
->new_baud
&& rd
== NULL
) /* tx ring empty and speed change pending */
1097 vlsi_set_baud(idev
, iobase
);
1099 config
= inw(iobase
+VLSI_PIO_IRCFG
);
1100 if (rd
== NULL
) /* tx ring empty: re-enable rx */
1101 outw((config
& ~IRCFG_ENTX
) | IRCFG_ENRX
, iobase
+VLSI_PIO_IRCFG
);
1103 else if (!(inw(iobase
+VLSI_PIO_IRENABLE
) & IRENABLE_ENTXST
)) {
1106 fifocnt
= inw(iobase
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
1108 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n",
1111 outw(config
| IRCFG_ENTX
, iobase
+VLSI_PIO_IRCFG
);
1114 outw(0, iobase
+VLSI_PIO_PROMPT
);
1116 if (netif_queue_stopped(ndev
) && !idev
->new_baud
) {
1117 netif_wake_queue(ndev
);
1118 IRDA_DEBUG(3, "%s: queue awoken\n", __func__
);
1122 /* caller must have stopped the controller from busmastering */
1124 static void vlsi_unarm_tx(vlsi_irda_dev_t
*idev
)
1126 struct net_device
*ndev
= pci_get_drvdata(idev
->pdev
);
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", __func__
);
1143 ret
= -VLSI_TX_DROP
;
1146 ret
= vlsi_process_tx(r
, rd
);
1150 ndev
->stats
.tx_errors
++;
1151 if (ret
& VLSI_TX_DROP
)
1152 ndev
->stats
.tx_dropped
++;
1153 if (ret
& VLSI_TX_FIFO
)
1154 ndev
->stats
.tx_fifo_errors
++;
1157 ndev
->stats
.tx_packets
++;
1158 ndev
->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",
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
= netdev_priv(ndev
);
1270 /* start the clock and clean the registers */
1272 if (vlsi_start_clock(pdev
)) {
1273 IRDA_ERROR("%s: no valid clock source\n", __func__
);
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 void vlsi_tx_timeout(struct net_device
*ndev
)
1380 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1383 vlsi_reg_debug(ndev
->base_addr
, __func__
);
1384 vlsi_ring_debug(idev
->tx_ring
);
1386 if (netif_running(ndev
))
1387 netif_stop_queue(ndev
);
1391 /* now simply restart the whole thing */
1393 if (!idev
->new_baud
)
1394 idev
->new_baud
= idev
->baud
; /* keep current baudrate */
1396 if (vlsi_start_hw(idev
))
1397 IRDA_ERROR("%s: failed to restart hw - %s(%s) unusable!\n",
1398 __func__
, pci_name(idev
->pdev
), ndev
->name
);
1400 netif_start_queue(ndev
);
1403 static int vlsi_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
1405 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1406 struct if_irda_req
*irq
= (struct if_irda_req
*) rq
;
1407 unsigned long flags
;
1412 case SIOCSBANDWIDTH
:
1413 if (!capable(CAP_NET_ADMIN
)) {
1417 spin_lock_irqsave(&idev
->lock
, flags
);
1418 idev
->new_baud
= irq
->ifr_baudrate
;
1419 /* when called from userland there might be a minor race window here
1420 * if the stack tries to change speed concurrently - which would be
1421 * pretty strange anyway with the userland having full control...
1423 vlsi_set_baud(idev
, ndev
->base_addr
);
1424 spin_unlock_irqrestore(&idev
->lock
, flags
);
1426 case SIOCSMEDIABUSY
:
1427 if (!capable(CAP_NET_ADMIN
)) {
1431 irda_device_set_media_busy(ndev
, TRUE
);
1433 case SIOCGRECEIVING
:
1434 /* the best we can do: check whether there are any bytes in rx fifo.
1435 * The trustable window (in case some data arrives just afterwards)
1436 * may be as short as 1usec or so at 4Mbps.
1438 fifocnt
= inw(ndev
->base_addr
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
1439 irq
->ifr_receiving
= (fifocnt
!=0) ? 1 : 0;
1442 IRDA_WARNING("%s: notsupp - cmd=%04x\n",
1450 /********************************************************/
1452 static irqreturn_t
vlsi_interrupt(int irq
, void *dev_instance
)
1454 struct net_device
*ndev
= dev_instance
;
1455 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1459 unsigned long flags
;
1462 iobase
= ndev
->base_addr
;
1463 spin_lock_irqsave(&idev
->lock
,flags
);
1465 irintr
= inb(iobase
+VLSI_PIO_IRINTR
);
1467 outb(irintr
, iobase
+VLSI_PIO_IRINTR
); /* acknowledge asap */
1469 if (!(irintr
&=IRINTR_INT_MASK
)) /* not our INT - probably shared */
1474 if (unlikely(!(irintr
& ~IRINTR_ACTIVITY
)))
1475 break; /* nothing todo if only activity */
1477 if (irintr
&IRINTR_RPKTINT
)
1478 vlsi_rx_interrupt(ndev
);
1480 if (irintr
&IRINTR_TPKTINT
)
1481 vlsi_tx_interrupt(ndev
);
1483 } while (--boguscount
> 0);
1484 spin_unlock_irqrestore(&idev
->lock
,flags
);
1486 if (boguscount
<= 0)
1487 IRDA_MESSAGE("%s: too much work in interrupt!\n",
1489 return IRQ_RETVAL(handled
);
1492 /********************************************************/
1494 static int vlsi_open(struct net_device
*ndev
)
1496 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1500 if (pci_request_regions(idev
->pdev
, drivername
)) {
1501 IRDA_WARNING("%s: io resource busy\n", __func__
);
1504 ndev
->base_addr
= pci_resource_start(idev
->pdev
,0);
1505 ndev
->irq
= idev
->pdev
->irq
;
1507 /* under some rare occasions the chip apparently comes up with
1508 * IRQ's pending. We better w/c pending IRQ and disable them all
1511 outb(IRINTR_INT_MASK
, ndev
->base_addr
+VLSI_PIO_IRINTR
);
1513 if (request_irq(ndev
->irq
, vlsi_interrupt
, IRQF_SHARED
,
1514 drivername
, ndev
)) {
1515 IRDA_WARNING("%s: couldn't get IRQ: %d\n",
1516 __func__
, ndev
->irq
);
1520 if ((err
= vlsi_create_hwif(idev
)) != 0)
1523 sprintf(hwname
, "VLSI-FIR @ 0x%04x", (unsigned)ndev
->base_addr
);
1524 idev
->irlap
= irlap_open(ndev
,&idev
->qos
,hwname
);
1526 goto errout_free_ring
;
1528 do_gettimeofday(&idev
->last_rx
); /* first mtt may start from now on */
1530 idev
->new_baud
= 9600; /* start with IrPHY using 9600(SIR) mode */
1532 if ((err
= vlsi_start_hw(idev
)) != 0)
1533 goto errout_close_irlap
;
1535 netif_start_queue(ndev
);
1537 IRDA_MESSAGE("%s: device %s operational\n", __func__
, ndev
->name
);
1542 irlap_close(idev
->irlap
);
1544 vlsi_destroy_hwif(idev
);
1546 free_irq(ndev
->irq
,ndev
);
1548 pci_release_regions(idev
->pdev
);
1553 static int vlsi_close(struct net_device
*ndev
)
1555 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1557 netif_stop_queue(ndev
);
1560 irlap_close(idev
->irlap
);
1565 vlsi_destroy_hwif(idev
);
1567 free_irq(ndev
->irq
,ndev
);
1569 pci_release_regions(idev
->pdev
);
1571 IRDA_MESSAGE("%s: device %s stopped\n", __func__
, ndev
->name
);
1576 static const struct net_device_ops vlsi_netdev_ops
= {
1577 .ndo_open
= vlsi_open
,
1578 .ndo_stop
= vlsi_close
,
1579 .ndo_start_xmit
= vlsi_hard_start_xmit
,
1580 .ndo_do_ioctl
= vlsi_ioctl
,
1581 .ndo_tx_timeout
= vlsi_tx_timeout
,
1584 static int vlsi_irda_init(struct net_device
*ndev
)
1586 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1587 struct pci_dev
*pdev
= idev
->pdev
;
1589 ndev
->irq
= pdev
->irq
;
1590 ndev
->base_addr
= pci_resource_start(pdev
,0);
1593 * see include file for details why we need these 2 masks, in this order!
1596 if (pci_set_dma_mask(pdev
,DMA_MASK_USED_BY_HW
)
1597 || pci_set_dma_mask(pdev
,DMA_MASK_MSTRPAGE
)) {
1598 IRDA_ERROR("%s: aborting due to PCI BM-DMA address limitations\n", __func__
);
1602 irda_init_max_qos_capabilies(&idev
->qos
);
1604 /* the VLSI82C147 does not support 576000! */
1606 idev
->qos
.baud_rate
.bits
= IR_2400
| IR_9600
1607 | IR_19200
| IR_38400
| IR_57600
| IR_115200
1608 | IR_1152000
| (IR_4000000
<< 8);
1610 idev
->qos
.min_turn_time
.bits
= qos_mtt_bits
;
1612 irda_qos_bits_to_value(&idev
->qos
);
1614 /* currently no public media definitions for IrDA */
1616 ndev
->flags
|= IFF_PORTSEL
| IFF_AUTOMEDIA
;
1617 ndev
->if_port
= IF_PORT_UNKNOWN
;
1619 ndev
->netdev_ops
= &vlsi_netdev_ops
;
1620 ndev
->watchdog_timeo
= 500*HZ
/1000; /* max. allowed turn time for IrLAP */
1622 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1627 /**************************************************************/
1629 static int __devinit
1630 vlsi_irda_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1632 struct net_device
*ndev
;
1633 vlsi_irda_dev_t
*idev
;
1635 if (pci_enable_device(pdev
))
1638 pdev
->current_state
= 0; /* hw must be running now */
1640 IRDA_MESSAGE("%s: IrDA PCI controller %s detected\n",
1641 drivername
, pci_name(pdev
));
1643 if ( !pci_resource_start(pdev
,0)
1644 || !(pci_resource_flags(pdev
,0) & IORESOURCE_IO
) ) {
1645 IRDA_ERROR("%s: bar 0 invalid", __func__
);
1649 ndev
= alloc_irdadev(sizeof(*idev
));
1651 IRDA_ERROR("%s: Unable to allocate device memory.\n",
1656 idev
= netdev_priv(ndev
);
1658 spin_lock_init(&idev
->lock
);
1659 mutex_init(&idev
->mtx
);
1660 mutex_lock(&idev
->mtx
);
1663 if (vlsi_irda_init(ndev
) < 0)
1666 if (register_netdev(ndev
) < 0) {
1667 IRDA_ERROR("%s: register_netdev failed\n", __func__
);
1671 if (vlsi_proc_root
!= NULL
) {
1672 struct proc_dir_entry
*ent
;
1674 ent
= proc_create_data(ndev
->name
, S_IFREG
|S_IRUGO
,
1675 vlsi_proc_root
, VLSI_PROC_FOPS
, ndev
);
1677 IRDA_WARNING("%s: failed to create proc entry\n",
1682 idev
->proc_entry
= ent
;
1684 IRDA_MESSAGE("%s: registered device %s\n", drivername
, ndev
->name
);
1686 pci_set_drvdata(pdev
, ndev
);
1687 mutex_unlock(&idev
->mtx
);
1692 mutex_unlock(&idev
->mtx
);
1695 pci_disable_device(pdev
);
1697 pci_set_drvdata(pdev
, NULL
);
1701 static void __devexit
vlsi_irda_remove(struct pci_dev
*pdev
)
1703 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1704 vlsi_irda_dev_t
*idev
;
1707 IRDA_ERROR("%s: lost netdevice?\n", drivername
);
1711 unregister_netdev(ndev
);
1713 idev
= netdev_priv(ndev
);
1714 mutex_lock(&idev
->mtx
);
1715 if (idev
->proc_entry
) {
1716 remove_proc_entry(ndev
->name
, vlsi_proc_root
);
1717 idev
->proc_entry
= NULL
;
1719 mutex_unlock(&idev
->mtx
);
1723 pci_set_drvdata(pdev
, NULL
);
1725 IRDA_MESSAGE("%s: %s removed\n", drivername
, pci_name(pdev
));
1730 /* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
1731 * Some of the Linux PCI-PM code however depends on this, for example in
1732 * pci_set_power_state(). So we have to take care to perform the required
1733 * operations on our own (particularly reflecting the pdev->current_state)
1734 * otherwise we might get cheated by pci-pm.
1738 static int vlsi_irda_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1740 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1741 vlsi_irda_dev_t
*idev
;
1744 IRDA_ERROR("%s - %s: no netdevice \n",
1745 __func__
, pci_name(pdev
));
1748 idev
= netdev_priv(ndev
);
1749 mutex_lock(&idev
->mtx
);
1750 if (pdev
->current_state
!= 0) { /* already suspended */
1751 if (state
.event
> pdev
->current_state
) { /* simply go deeper */
1752 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1753 pdev
->current_state
= state
.event
;
1756 IRDA_ERROR("%s - %s: invalid suspend request %u -> %u\n", __func__
, pci_name(pdev
), pdev
->current_state
, state
.event
);
1757 mutex_unlock(&idev
->mtx
);
1761 if (netif_running(ndev
)) {
1762 netif_device_detach(ndev
);
1764 pci_save_state(pdev
);
1765 if (!idev
->new_baud
)
1766 /* remember speed settings to restore on resume */
1767 idev
->new_baud
= idev
->baud
;
1770 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1771 pdev
->current_state
= state
.event
;
1772 idev
->resume_ok
= 1;
1773 mutex_unlock(&idev
->mtx
);
1777 static int vlsi_irda_resume(struct pci_dev
*pdev
)
1779 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1780 vlsi_irda_dev_t
*idev
;
1783 IRDA_ERROR("%s - %s: no netdevice \n",
1784 __func__
, pci_name(pdev
));
1787 idev
= netdev_priv(ndev
);
1788 mutex_lock(&idev
->mtx
);
1789 if (pdev
->current_state
== 0) {
1790 mutex_unlock(&idev
->mtx
);
1791 IRDA_WARNING("%s - %s: already resumed\n",
1792 __func__
, pci_name(pdev
));
1796 pci_set_power_state(pdev
, PCI_D0
);
1797 pdev
->current_state
= PM_EVENT_ON
;
1799 if (!idev
->resume_ok
) {
1800 /* should be obsolete now - but used to happen due to:
1801 * - pci layer initially setting pdev->current_state = 4 (unknown)
1802 * - pci layer did not walk the save_state-tree (might be APM problem)
1803 * so we could not refuse to suspend from undefined state
1804 * - vlsi_irda_suspend detected invalid state and refused to save
1805 * configuration for resume - but was too late to stop suspending
1806 * - vlsi_irda_resume got screwed when trying to resume from garbage
1808 * now we explicitly set pdev->current_state = 0 after enabling the
1809 * device and independently resume_ok should catch any garbage config.
1811 IRDA_WARNING("%s - hm, nothing to resume?\n", __func__
);
1812 mutex_unlock(&idev
->mtx
);
1816 if (netif_running(ndev
)) {
1817 pci_restore_state(pdev
);
1818 vlsi_start_hw(idev
);
1819 netif_device_attach(ndev
);
1821 idev
->resume_ok
= 0;
1822 mutex_unlock(&idev
->mtx
);
1826 #endif /* CONFIG_PM */
1828 /*********************************************************/
1830 static struct pci_driver vlsi_irda_driver
= {
1832 .id_table
= vlsi_irda_table
,
1833 .probe
= vlsi_irda_probe
,
1834 .remove
= __devexit_p(vlsi_irda_remove
),
1836 .suspend
= vlsi_irda_suspend
,
1837 .resume
= vlsi_irda_resume
,
1841 #define PROC_DIR ("driver/" DRIVER_NAME)
1843 static int __init
vlsi_mod_init(void)
1847 if (clksrc
< 0 || clksrc
> 3) {
1848 IRDA_ERROR("%s: invalid clksrc=%d\n", drivername
, clksrc
);
1852 for (i
= 0; i
< 2; i
++) {
1853 switch(ringsize
[i
]) {
1861 IRDA_WARNING("%s: invalid %s ringsize %d, using default=8", drivername
, (i
)?"rx":"tx", ringsize
[i
]);
1867 sirpulse
= !!sirpulse
;
1869 /* proc_mkdir returns NULL if !CONFIG_PROC_FS.
1870 * Failure to create the procfs entry is handled like running
1871 * without procfs - it's not required for the driver to work.
1873 vlsi_proc_root
= proc_mkdir(PROC_DIR
, NULL
);
1875 ret
= pci_register_driver(&vlsi_irda_driver
);
1877 if (ret
&& vlsi_proc_root
)
1878 remove_proc_entry(PROC_DIR
, NULL
);
1883 static void __exit
vlsi_mod_exit(void)
1885 pci_unregister_driver(&vlsi_irda_driver
);
1887 remove_proc_entry(PROC_DIR
, NULL
);
1890 module_init(vlsi_mod_init
);
1891 module_exit(vlsi_mod_exit
);