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, see <http://www.gnu.org/licenses/>.
20 ********************************************************************/
22 #include <linux/module.h>
24 #define DRIVER_NAME "vlsi_ir"
25 #define DRIVER_VERSION "v0.5"
26 #define DRIVER_DESCRIPTION "IrDA SIR/MIR/FIR driver for VLSI 82C147"
27 #define DRIVER_AUTHOR "Martin Diehl <info@mdiehl.de>"
29 MODULE_DESCRIPTION(DRIVER_DESCRIPTION
);
30 MODULE_AUTHOR(DRIVER_AUTHOR
);
31 MODULE_LICENSE("GPL");
33 /********************************************************/
35 #include <linux/kernel.h>
36 #include <linux/init.h>
37 #include <linux/interrupt.h>
38 #include <linux/pci.h>
39 #include <linux/slab.h>
40 #include <linux/netdevice.h>
41 #include <linux/skbuff.h>
42 #include <linux/delay.h>
43 #include <linux/time.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/mutex.h>
47 #include <asm/uaccess.h>
48 #include <asm/byteorder.h>
50 #include <net/irda/irda.h>
51 #include <net/irda/irda_device.h>
52 #include <net/irda/wrapper.h>
53 #include <net/irda/crc.h>
57 /********************************************************/
59 static /* const */ char drivername
[] = DRIVER_NAME
;
61 static const struct pci_device_id vlsi_irda_table
[] = {
63 .class = PCI_CLASS_WIRELESS_IRDA
<< 8,
64 .class_mask
= PCI_CLASS_SUBCLASS_MASK
<< 8,
65 .vendor
= PCI_VENDOR_ID_VLSI
,
66 .device
= PCI_DEVICE_ID_VLSI_82C147
,
67 .subvendor
= PCI_ANY_ID
,
68 .subdevice
= PCI_ANY_ID
,
73 MODULE_DEVICE_TABLE(pci
, vlsi_irda_table
);
75 /********************************************************/
77 /* clksrc: which clock source to be used
78 * 0: auto - try PLL, fallback to 40MHz XCLK
79 * 1: on-chip 48MHz PLL
80 * 2: external 48MHz XCLK
81 * 3: external 40MHz XCLK (HP OB-800)
84 static int clksrc
= 0; /* default is 0(auto) */
85 module_param(clksrc
, int, 0);
86 MODULE_PARM_DESC(clksrc
, "clock input source selection");
88 /* ringsize: size of the tx and rx descriptor rings
89 * independent for tx and rx
90 * specify as ringsize=tx[,rx]
91 * allowed values: 4, 8, 16, 32, 64
92 * Due to the IrDA 1.x max. allowed window size=7,
93 * there should be no gain when using rings larger than 8
96 static int ringsize
[] = {8,8}; /* default is tx=8 / rx=8 */
97 module_param_array(ringsize
, int, NULL
, 0);
98 MODULE_PARM_DESC(ringsize
, "TX, RX ring descriptor size");
100 /* sirpulse: tuning of the SIR pulse width within IrPHY 1.3 limits
101 * 0: very short, 1.5us (exception: 6us at 2.4 kbaud)
102 * 1: nominal 3/16 bittime width
103 * note: IrDA compliant peer devices should be happy regardless
104 * which one is used. Primary goal is to save some power
105 * on the sender's side - at 9.6kbaud for example the short
106 * pulse width saves more than 90% of the transmitted IR power.
109 static int sirpulse
= 1; /* default is 3/16 bittime */
110 module_param(sirpulse
, int, 0);
111 MODULE_PARM_DESC(sirpulse
, "SIR pulse width tuning");
113 /* qos_mtt_bits: encoded min-turn-time value we require the peer device
114 * to use before transmitting to us. "Type 1" (per-station)
115 * bitfield according to IrLAP definition (section 6.6.8)
116 * Don't know which transceiver is used by my OB800 - the
117 * pretty common HP HDLS-1100 requires 1 msec - so lets use this.
120 static int qos_mtt_bits
= 0x07; /* default is 1 ms or more */
121 module_param(qos_mtt_bits
, int, 0);
122 MODULE_PARM_DESC(qos_mtt_bits
, "IrLAP bitfield representing min-turn-time");
124 /********************************************************/
126 static void vlsi_reg_debug(unsigned iobase
, const char *s
)
130 printk(KERN_DEBUG
"%s: ", s
);
131 for (i
= 0; i
< 0x20; i
++)
132 printk("%02x", (unsigned)inb((iobase
+i
)));
136 static void vlsi_ring_debug(struct vlsi_ring
*r
)
138 struct ring_descr
*rd
;
141 printk(KERN_DEBUG
"%s - ring %p / size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
142 __func__
, r
, r
->size
, r
->mask
, r
->len
, r
->dir
, r
->rd
[0].hw
);
143 printk(KERN_DEBUG
"%s - head = %d / tail = %d\n", __func__
,
144 atomic_read(&r
->head
) & r
->mask
, atomic_read(&r
->tail
) & r
->mask
);
145 for (i
= 0; i
< r
->size
; i
++) {
147 printk(KERN_DEBUG
"%s - ring descr %u: ", __func__
, i
);
148 printk("skb=%p data=%p hw=%p\n", rd
->skb
, rd
->buf
, rd
->hw
);
149 printk(KERN_DEBUG
"%s - hw: status=%02x count=%u addr=0x%08x\n",
150 __func__
, (unsigned) rd_get_status(rd
),
151 (unsigned) rd_get_count(rd
), (unsigned) rd_get_addr(rd
));
155 /********************************************************/
157 /* needed regardless of CONFIG_PROC_FS */
158 static struct proc_dir_entry
*vlsi_proc_root
= NULL
;
160 #ifdef CONFIG_PROC_FS
162 static void vlsi_proc_pdev(struct seq_file
*seq
, struct pci_dev
*pdev
)
164 unsigned iobase
= pci_resource_start(pdev
, 0);
167 seq_printf(seq
, "\n%s (vid/did: [%04x:%04x])\n",
168 pci_name(pdev
), (int)pdev
->vendor
, (int)pdev
->device
);
169 seq_printf(seq
, "pci-power-state: %u\n", (unsigned) pdev
->current_state
);
170 seq_printf(seq
, "resources: irq=%u / io=0x%04x / dma_mask=0x%016Lx\n",
171 pdev
->irq
, (unsigned)pci_resource_start(pdev
, 0), (unsigned long long)pdev
->dma_mask
);
172 seq_printf(seq
, "hw registers: ");
173 for (i
= 0; i
< 0x20; i
++)
174 seq_printf(seq
, "%02x", (unsigned)inb((iobase
+i
)));
175 seq_printf(seq
, "\n");
178 static void vlsi_proc_ndev(struct seq_file
*seq
, struct net_device
*ndev
)
180 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
183 unsigned delta1
, delta2
;
185 unsigned iobase
= ndev
->base_addr
;
187 seq_printf(seq
, "\n%s link state: %s / %s / %s / %s\n", ndev
->name
,
188 netif_device_present(ndev
) ? "attached" : "detached",
189 netif_running(ndev
) ? "running" : "not running",
190 netif_carrier_ok(ndev
) ? "carrier ok" : "no carrier",
191 netif_queue_stopped(ndev
) ? "queue stopped" : "queue running");
193 if (!netif_running(ndev
))
196 seq_printf(seq
, "\nhw-state:\n");
197 pci_read_config_byte(idev
->pdev
, VLSI_PCI_IRMISC
, &byte
);
198 seq_printf(seq
, "IRMISC:%s%s%s uart%s",
199 (byte
&IRMISC_IRRAIL
) ? " irrail" : "",
200 (byte
&IRMISC_IRPD
) ? " irpd" : "",
201 (byte
&IRMISC_UARTTST
) ? " uarttest" : "",
202 (byte
&IRMISC_UARTEN
) ? "@" : " disabled\n");
203 if (byte
&IRMISC_UARTEN
) {
204 seq_printf(seq
, "0x%s\n",
205 (byte
&2) ? ((byte
&1) ? "3e8" : "2e8")
206 : ((byte
&1) ? "3f8" : "2f8"));
208 pci_read_config_byte(idev
->pdev
, VLSI_PCI_CLKCTL
, &byte
);
209 seq_printf(seq
, "CLKCTL: PLL %s%s%s / clock %s / wakeup %s\n",
210 (byte
&CLKCTL_PD_INV
) ? "powered" : "down",
211 (byte
&CLKCTL_LOCK
) ? " locked" : "",
212 (byte
&CLKCTL_EXTCLK
) ? ((byte
&CLKCTL_XCKSEL
)?" / 40 MHz XCLK":" / 48 MHz XCLK") : "",
213 (byte
&CLKCTL_CLKSTP
) ? "stopped" : "running",
214 (byte
&CLKCTL_WAKE
) ? "enabled" : "disabled");
215 pci_read_config_byte(idev
->pdev
, VLSI_PCI_MSTRPAGE
, &byte
);
216 seq_printf(seq
, "MSTRPAGE: 0x%02x\n", (unsigned)byte
);
218 byte
= inb(iobase
+VLSI_PIO_IRINTR
);
219 seq_printf(seq
, "IRINTR:%s%s%s%s%s%s%s%s\n",
220 (byte
&IRINTR_ACTEN
) ? " ACTEN" : "",
221 (byte
&IRINTR_RPKTEN
) ? " RPKTEN" : "",
222 (byte
&IRINTR_TPKTEN
) ? " TPKTEN" : "",
223 (byte
&IRINTR_OE_EN
) ? " OE_EN" : "",
224 (byte
&IRINTR_ACTIVITY
) ? " ACTIVITY" : "",
225 (byte
&IRINTR_RPKTINT
) ? " RPKTINT" : "",
226 (byte
&IRINTR_TPKTINT
) ? " TPKTINT" : "",
227 (byte
&IRINTR_OE_INT
) ? " OE_INT" : "");
228 word
= inw(iobase
+VLSI_PIO_RINGPTR
);
229 seq_printf(seq
, "RINGPTR: rx=%u / tx=%u\n", RINGPTR_GET_RX(word
), RINGPTR_GET_TX(word
));
230 word
= inw(iobase
+VLSI_PIO_RINGBASE
);
231 seq_printf(seq
, "RINGBASE: busmap=0x%08x\n",
232 ((unsigned)word
<< 10)|(MSTRPAGE_VALUE
<<24));
233 word
= inw(iobase
+VLSI_PIO_RINGSIZE
);
234 seq_printf(seq
, "RINGSIZE: rx=%u / tx=%u\n", RINGSIZE_TO_RXSIZE(word
),
235 RINGSIZE_TO_TXSIZE(word
));
237 word
= inw(iobase
+VLSI_PIO_IRCFG
);
238 seq_printf(seq
, "IRCFG:%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
239 (word
&IRCFG_LOOP
) ? " LOOP" : "",
240 (word
&IRCFG_ENTX
) ? " ENTX" : "",
241 (word
&IRCFG_ENRX
) ? " ENRX" : "",
242 (word
&IRCFG_MSTR
) ? " MSTR" : "",
243 (word
&IRCFG_RXANY
) ? " RXANY" : "",
244 (word
&IRCFG_CRC16
) ? " CRC16" : "",
245 (word
&IRCFG_FIR
) ? " FIR" : "",
246 (word
&IRCFG_MIR
) ? " MIR" : "",
247 (word
&IRCFG_SIR
) ? " SIR" : "",
248 (word
&IRCFG_SIRFILT
) ? " SIRFILT" : "",
249 (word
&IRCFG_SIRTEST
) ? " SIRTEST" : "",
250 (word
&IRCFG_TXPOL
) ? " TXPOL" : "",
251 (word
&IRCFG_RXPOL
) ? " RXPOL" : "");
252 word
= inw(iobase
+VLSI_PIO_IRENABLE
);
253 seq_printf(seq
, "IRENABLE:%s%s%s%s%s%s%s%s\n",
254 (word
&IRENABLE_PHYANDCLOCK
) ? " PHYANDCLOCK" : "",
255 (word
&IRENABLE_CFGER
) ? " CFGERR" : "",
256 (word
&IRENABLE_FIR_ON
) ? " FIR_ON" : "",
257 (word
&IRENABLE_MIR_ON
) ? " MIR_ON" : "",
258 (word
&IRENABLE_SIR_ON
) ? " SIR_ON" : "",
259 (word
&IRENABLE_ENTXST
) ? " ENTXST" : "",
260 (word
&IRENABLE_ENRXST
) ? " ENRXST" : "",
261 (word
&IRENABLE_CRC16_ON
) ? " CRC16_ON" : "");
262 word
= inw(iobase
+VLSI_PIO_PHYCTL
);
263 seq_printf(seq
, "PHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
264 (unsigned)PHYCTL_TO_BAUD(word
),
265 (unsigned)PHYCTL_TO_PLSWID(word
),
266 (unsigned)PHYCTL_TO_PREAMB(word
));
267 word
= inw(iobase
+VLSI_PIO_NPHYCTL
);
268 seq_printf(seq
, "NPHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
269 (unsigned)PHYCTL_TO_BAUD(word
),
270 (unsigned)PHYCTL_TO_PLSWID(word
),
271 (unsigned)PHYCTL_TO_PREAMB(word
));
272 word
= inw(iobase
+VLSI_PIO_MAXPKT
);
273 seq_printf(seq
, "MAXPKT: max. rx packet size = %u\n", word
);
274 word
= inw(iobase
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
275 seq_printf(seq
, "RCVBCNT: rx-fifo filling level = %u\n", word
);
277 seq_printf(seq
, "\nsw-state:\n");
278 seq_printf(seq
, "IrPHY setup: %d baud - %s encoding\n", idev
->baud
,
279 (idev
->mode
==IFF_SIR
)?"SIR":((idev
->mode
==IFF_MIR
)?"MIR":"FIR"));
280 do_gettimeofday(&now
);
281 if (now
.tv_usec
>= idev
->last_rx
.tv_usec
) {
282 delta2
= now
.tv_usec
- idev
->last_rx
.tv_usec
;
286 delta2
= 1000000 + now
.tv_usec
- idev
->last_rx
.tv_usec
;
289 seq_printf(seq
, "last rx: %lu.%06u sec\n",
290 now
.tv_sec
- idev
->last_rx
.tv_sec
- delta1
, delta2
);
292 seq_printf(seq
, "RX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu",
293 ndev
->stats
.rx_packets
, ndev
->stats
.rx_bytes
, ndev
->stats
.rx_errors
,
294 ndev
->stats
.rx_dropped
);
295 seq_printf(seq
, " / overrun=%lu / length=%lu / frame=%lu / crc=%lu\n",
296 ndev
->stats
.rx_over_errors
, ndev
->stats
.rx_length_errors
,
297 ndev
->stats
.rx_frame_errors
, ndev
->stats
.rx_crc_errors
);
298 seq_printf(seq
, "TX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu / fifo=%lu\n",
299 ndev
->stats
.tx_packets
, ndev
->stats
.tx_bytes
, ndev
->stats
.tx_errors
,
300 ndev
->stats
.tx_dropped
, ndev
->stats
.tx_fifo_errors
);
304 static void vlsi_proc_ring(struct seq_file
*seq
, struct vlsi_ring
*r
)
306 struct ring_descr
*rd
;
310 seq_printf(seq
, "size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
311 r
->size
, r
->mask
, r
->len
, r
->dir
, r
->rd
[0].hw
);
312 h
= atomic_read(&r
->head
) & r
->mask
;
313 t
= atomic_read(&r
->tail
) & r
->mask
;
314 seq_printf(seq
, "head = %d / tail = %d ", h
, t
);
316 seq_printf(seq
, "(empty)\n");
318 if (((t
+1)&r
->mask
) == h
)
319 seq_printf(seq
, "(full)\n");
321 seq_printf(seq
, "(level = %d)\n", ((unsigned)(t
-h
) & r
->mask
));
323 j
= (unsigned) rd_get_count(rd
);
324 seq_printf(seq
, "current: rd = %d / status = %02x / len = %u\n",
325 h
, (unsigned)rd_get_status(rd
), j
);
327 seq_printf(seq
, " data: %*ph\n",
328 min_t(unsigned, j
, 20), rd
->buf
);
331 for (i
= 0; i
< r
->size
; i
++) {
333 seq_printf(seq
, "> ring descr %u: ", i
);
334 seq_printf(seq
, "skb=%p data=%p hw=%p\n", rd
->skb
, rd
->buf
, rd
->hw
);
335 seq_printf(seq
, " hw: status=%02x count=%u busaddr=0x%08x\n",
336 (unsigned) rd_get_status(rd
),
337 (unsigned) rd_get_count(rd
), (unsigned) rd_get_addr(rd
));
341 static int vlsi_seq_show(struct seq_file
*seq
, void *v
)
343 struct net_device
*ndev
= seq
->private;
344 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
347 seq_printf(seq
, "\n%s %s\n\n", DRIVER_NAME
, DRIVER_VERSION
);
348 seq_printf(seq
, "clksrc: %s\n",
349 (clksrc
>=2) ? ((clksrc
==3)?"40MHz XCLK":"48MHz XCLK")
350 : ((clksrc
==1)?"48MHz PLL":"autodetect"));
351 seq_printf(seq
, "ringsize: tx=%d / rx=%d\n",
352 ringsize
[0], ringsize
[1]);
353 seq_printf(seq
, "sirpulse: %s\n", (sirpulse
)?"3/16 bittime":"short");
354 seq_printf(seq
, "qos_mtt_bits: 0x%02x\n", (unsigned)qos_mtt_bits
);
356 spin_lock_irqsave(&idev
->lock
, flags
);
357 if (idev
->pdev
!= NULL
) {
358 vlsi_proc_pdev(seq
, idev
->pdev
);
360 if (idev
->pdev
->current_state
== 0)
361 vlsi_proc_ndev(seq
, ndev
);
363 seq_printf(seq
, "\nPCI controller down - resume_ok = %d\n",
365 if (netif_running(ndev
) && idev
->rx_ring
&& idev
->tx_ring
) {
366 seq_printf(seq
, "\n--------- RX ring -----------\n\n");
367 vlsi_proc_ring(seq
, idev
->rx_ring
);
368 seq_printf(seq
, "\n--------- TX ring -----------\n\n");
369 vlsi_proc_ring(seq
, idev
->tx_ring
);
372 seq_printf(seq
, "\n");
373 spin_unlock_irqrestore(&idev
->lock
, flags
);
378 static int vlsi_seq_open(struct inode
*inode
, struct file
*file
)
380 return single_open(file
, vlsi_seq_show
, PDE_DATA(inode
));
383 static const struct file_operations vlsi_proc_fops
= {
384 .owner
= THIS_MODULE
,
385 .open
= vlsi_seq_open
,
388 .release
= single_release
,
391 #define VLSI_PROC_FOPS (&vlsi_proc_fops)
393 #else /* CONFIG_PROC_FS */
394 #define VLSI_PROC_FOPS NULL
397 /********************************************************/
399 static struct vlsi_ring
*vlsi_alloc_ring(struct pci_dev
*pdev
, struct ring_descr_hw
*hwmap
,
400 unsigned size
, unsigned len
, int dir
)
403 struct ring_descr
*rd
;
407 if (!size
|| ((size
-1)&size
)!=0) /* must be >0 and power of 2 */
410 r
= kmalloc(sizeof(*r
) + size
* sizeof(struct ring_descr
), GFP_KERNEL
);
413 memset(r
, 0, sizeof(*r
));
418 r
->rd
= (struct ring_descr
*)(r
+1);
421 atomic_set(&r
->head
, 0);
422 atomic_set(&r
->tail
, 0);
424 for (i
= 0; i
< size
; i
++) {
426 memset(rd
, 0, sizeof(*rd
));
428 rd
->buf
= kmalloc(len
, GFP_KERNEL
|GFP_DMA
);
429 if (rd
->buf
== NULL
||
430 !(busaddr
= pci_map_single(pdev
, rd
->buf
, len
, dir
))) {
432 net_err_ratelimited("%s: failed to create PCI-MAP for %p\n",
437 for (j
= 0; j
< i
; j
++) {
439 busaddr
= rd_get_addr(rd
);
440 rd_set_addr_status(rd
, 0, 0);
442 pci_unmap_single(pdev
, busaddr
, len
, dir
);
449 rd_set_addr_status(rd
, busaddr
, 0);
450 /* initially, the dma buffer is owned by the CPU */
456 static int vlsi_free_ring(struct vlsi_ring
*r
)
458 struct ring_descr
*rd
;
462 for (i
= 0; i
< r
->size
; i
++) {
465 dev_kfree_skb_any(rd
->skb
);
466 busaddr
= rd_get_addr(rd
);
467 rd_set_addr_status(rd
, 0, 0);
469 pci_unmap_single(r
->pdev
, busaddr
, r
->len
, r
->dir
);
476 static int vlsi_create_hwif(vlsi_irda_dev_t
*idev
)
479 struct ring_descr_hw
*hwmap
;
481 idev
->virtaddr
= NULL
;
484 ringarea
= pci_zalloc_consistent(idev
->pdev
, HW_RING_AREA_SIZE
,
489 hwmap
= (struct ring_descr_hw
*)ringarea
;
490 idev
->rx_ring
= vlsi_alloc_ring(idev
->pdev
, hwmap
, ringsize
[1],
491 XFER_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
492 if (idev
->rx_ring
== NULL
)
495 hwmap
+= MAX_RING_DESCR
;
496 idev
->tx_ring
= vlsi_alloc_ring(idev
->pdev
, hwmap
, ringsize
[0],
497 XFER_BUF_SIZE
, PCI_DMA_TODEVICE
);
498 if (idev
->tx_ring
== NULL
)
501 idev
->virtaddr
= ringarea
;
505 vlsi_free_ring(idev
->rx_ring
);
507 idev
->rx_ring
= idev
->tx_ring
= NULL
;
508 pci_free_consistent(idev
->pdev
, HW_RING_AREA_SIZE
, ringarea
, idev
->busaddr
);
514 static int vlsi_destroy_hwif(vlsi_irda_dev_t
*idev
)
516 vlsi_free_ring(idev
->rx_ring
);
517 vlsi_free_ring(idev
->tx_ring
);
518 idev
->rx_ring
= idev
->tx_ring
= NULL
;
521 pci_free_consistent(idev
->pdev
,HW_RING_AREA_SIZE
,idev
->virtaddr
,idev
->busaddr
);
523 idev
->virtaddr
= NULL
;
529 /********************************************************/
531 static int vlsi_process_rx(struct vlsi_ring
*r
, struct ring_descr
*rd
)
537 struct net_device
*ndev
= pci_get_drvdata(r
->pdev
);
538 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
540 pci_dma_sync_single_for_cpu(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
541 /* dma buffer now owned by the CPU */
542 status
= rd_get_status(rd
);
543 if (status
& RD_RX_ERROR
) {
544 if (status
& RD_RX_OVER
)
546 if (status
& RD_RX_LENGTH
)
547 ret
|= VLSI_RX_LENGTH
;
548 if (status
& RD_RX_PHYERR
)
549 ret
|= VLSI_RX_FRAME
;
550 if (status
& RD_RX_CRCERR
)
555 len
= rd_get_count(rd
);
556 crclen
= (idev
->mode
==IFF_FIR
) ? sizeof(u32
) : sizeof(u16
);
557 len
-= crclen
; /* remove trailing CRC */
559 pr_debug("%s: strange frame (len=%d)\n", __func__
, len
);
564 if (idev
->mode
== IFF_SIR
) { /* hw checks CRC in MIR, FIR mode */
566 /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
567 * endian-adjustment there just in place will dirty a cache line
568 * which belongs to the map and thus we must be sure it will
569 * get flushed before giving the buffer back to hardware.
570 * vlsi_fill_rx() will do this anyway - but here we rely on.
572 le16_to_cpus(rd
->buf
+len
);
573 if (irda_calc_crc16(INIT_FCS
,rd
->buf
,len
+crclen
) != GOOD_FCS
) {
574 pr_debug("%s: crc error\n", __func__
);
581 net_warn_ratelimited("%s: rx packet lost\n", __func__
);
589 memcpy(skb_put(skb
,len
), rd
->buf
, len
);
590 skb_reset_mac_header(skb
);
597 rd_set_status(rd
, 0);
599 /* buffer still owned by CPU */
601 return (ret
) ? -ret
: len
;
604 static void vlsi_fill_rx(struct vlsi_ring
*r
)
606 struct ring_descr
*rd
;
608 for (rd
= ring_last(r
); rd
!= NULL
; rd
= ring_put(r
)) {
609 if (rd_is_active(rd
)) {
610 net_warn_ratelimited("%s: driver bug: rx descr race with hw\n",
616 rd
->skb
= dev_alloc_skb(IRLAP_SKB_ALLOCSIZE
);
618 skb_reserve(rd
->skb
,1);
619 rd
->skb
->protocol
= htons(ETH_P_IRDA
);
622 break; /* probably not worth logging? */
624 /* give dma buffer back to busmaster */
625 pci_dma_sync_single_for_device(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
630 static void vlsi_rx_interrupt(struct net_device
*ndev
)
632 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
633 struct vlsi_ring
*r
= idev
->rx_ring
;
634 struct ring_descr
*rd
;
637 for (rd
= ring_first(r
); rd
!= NULL
; rd
= ring_get(r
)) {
639 if (rd_is_active(rd
))
642 ret
= vlsi_process_rx(r
, rd
);
646 ndev
->stats
.rx_errors
++;
647 if (ret
& VLSI_RX_DROP
)
648 ndev
->stats
.rx_dropped
++;
649 if (ret
& VLSI_RX_OVER
)
650 ndev
->stats
.rx_over_errors
++;
651 if (ret
& VLSI_RX_LENGTH
)
652 ndev
->stats
.rx_length_errors
++;
653 if (ret
& VLSI_RX_FRAME
)
654 ndev
->stats
.rx_frame_errors
++;
655 if (ret
& VLSI_RX_CRC
)
656 ndev
->stats
.rx_crc_errors
++;
659 ndev
->stats
.rx_packets
++;
660 ndev
->stats
.rx_bytes
+= ret
;
664 do_gettimeofday(&idev
->last_rx
); /* remember "now" for later mtt delay */
668 if (ring_first(r
) == NULL
) {
669 /* we are in big trouble, if this should ever happen */
670 net_err_ratelimited("%s: rx ring exhausted!\n", __func__
);
674 outw(0, ndev
->base_addr
+VLSI_PIO_PROMPT
);
677 /* caller must have stopped the controller from busmastering */
679 static void vlsi_unarm_rx(vlsi_irda_dev_t
*idev
)
681 struct net_device
*ndev
= pci_get_drvdata(idev
->pdev
);
682 struct vlsi_ring
*r
= idev
->rx_ring
;
683 struct ring_descr
*rd
;
686 for (rd
= ring_first(r
); rd
!= NULL
; rd
= ring_get(r
)) {
689 if (rd_is_active(rd
)) {
690 rd_set_status(rd
, 0);
691 if (rd_get_count(rd
)) {
692 pr_debug("%s - dropping rx packet\n", __func__
);
696 pci_dma_sync_single_for_cpu(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
698 dev_kfree_skb_any(rd
->skb
);
703 ret
= vlsi_process_rx(r
, rd
);
707 ndev
->stats
.rx_errors
++;
708 if (ret
& VLSI_RX_DROP
)
709 ndev
->stats
.rx_dropped
++;
710 if (ret
& VLSI_RX_OVER
)
711 ndev
->stats
.rx_over_errors
++;
712 if (ret
& VLSI_RX_LENGTH
)
713 ndev
->stats
.rx_length_errors
++;
714 if (ret
& VLSI_RX_FRAME
)
715 ndev
->stats
.rx_frame_errors
++;
716 if (ret
& VLSI_RX_CRC
)
717 ndev
->stats
.rx_crc_errors
++;
720 ndev
->stats
.rx_packets
++;
721 ndev
->stats
.rx_bytes
+= ret
;
726 /********************************************************/
728 static int vlsi_process_tx(struct vlsi_ring
*r
, struct ring_descr
*rd
)
734 pci_dma_sync_single_for_cpu(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
735 /* dma buffer now owned by the CPU */
736 status
= rd_get_status(rd
);
737 if (status
& RD_TX_UNDRN
)
741 rd_set_status(rd
, 0);
745 dev_kfree_skb_any(rd
->skb
);
748 else /* tx-skb already freed? - should never happen */
749 len
= rd_get_count(rd
); /* incorrect for SIR! (due to wrapping) */
752 /* dma buffer still owned by the CPU */
754 return (ret
) ? -ret
: len
;
757 static int vlsi_set_baud(vlsi_irda_dev_t
*idev
, unsigned iobase
)
766 baudrate
= idev
->new_baud
;
767 pr_debug("%s: %d -> %d\n", __func__
, idev
->baud
, idev
->new_baud
);
768 if (baudrate
== 4000000) {
771 nphyctl
= PHYCTL_FIR
;
773 else if (baudrate
== 1152000) {
775 config
= IRCFG_MIR
| IRCFG_CRC16
;
776 nphyctl
= PHYCTL_MIR(clksrc
==3);
780 config
= IRCFG_SIR
| IRCFG_SIRFILT
| IRCFG_RXANY
;
783 net_warn_ratelimited("%s: undefined baudrate %d - fallback to 9600!\n",
793 nphyctl
= PHYCTL_SIR(baudrate
,sirpulse
,clksrc
==3);
797 config
|= IRCFG_MSTR
| IRCFG_ENRX
;
799 fifocnt
= inw(iobase
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
801 pr_debug("%s: rx fifo not empty(%d)\n", __func__
, fifocnt
);
804 outw(0, iobase
+VLSI_PIO_IRENABLE
);
805 outw(config
, iobase
+VLSI_PIO_IRCFG
);
806 outw(nphyctl
, iobase
+VLSI_PIO_NPHYCTL
);
808 outw(IRENABLE_PHYANDCLOCK
, iobase
+VLSI_PIO_IRENABLE
);
811 udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
813 /* read back settings for validation */
815 config
= inw(iobase
+VLSI_PIO_IRENABLE
) & IRENABLE_MASK
;
818 config
^= IRENABLE_FIR_ON
;
819 else if (mode
== IFF_MIR
)
820 config
^= (IRENABLE_MIR_ON
|IRENABLE_CRC16_ON
);
822 config
^= IRENABLE_SIR_ON
;
824 if (config
!= (IRENABLE_PHYANDCLOCK
|IRENABLE_ENRXST
)) {
825 net_warn_ratelimited("%s: failed to set %s mode!\n",
827 mode
== IFF_SIR
? "SIR" :
828 mode
== IFF_MIR
? "MIR" : "FIR");
832 if (inw(iobase
+VLSI_PIO_PHYCTL
) != nphyctl
) {
833 net_warn_ratelimited("%s: failed to apply baudrate %d\n",
839 idev
->baud
= baudrate
;
846 vlsi_reg_debug(iobase
,__func__
);
851 static netdev_tx_t
vlsi_hard_start_xmit(struct sk_buff
*skb
,
852 struct net_device
*ndev
)
854 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
855 struct vlsi_ring
*r
= idev
->tx_ring
;
856 struct ring_descr
*rd
;
858 unsigned iobase
= ndev
->base_addr
;
863 struct timeval now
, ready
;
866 speed
= irda_get_next_speed(skb
);
867 spin_lock_irqsave(&idev
->lock
, flags
);
868 if (speed
!= -1 && speed
!= idev
->baud
) {
869 netif_stop_queue(ndev
);
870 idev
->new_baud
= speed
;
871 status
= RD_TX_CLRENTX
; /* stop tx-ring after this frame */
877 /* handle zero packets - should be speed change */
879 msg
= "bogus zero-length packet";
883 /* due to the completely asynch tx operation we might have
884 * IrLAP racing with the hardware here, f.e. if the controller
885 * is just sending the last packet with current speed while
886 * the LAP is already switching the speed using synchronous
887 * len=0 packet. Immediate execution would lead to hw lockup
888 * requiring a powercycle to reset. Good candidate to trigger
889 * this is the final UA:RSP packet after receiving a DISC:CMD
890 * when getting the LAP down.
891 * Note that we are not protected by the queue_stop approach
892 * because the final UA:RSP arrives _without_ request to apply
893 * new-speed-after-this-packet - hence the driver doesn't know
894 * this was the last packet and doesn't stop the queue. So the
895 * forced switch to default speed from LAP gets through as fast
896 * as only some 10 usec later while the UA:RSP is still processed
897 * by the hardware and we would get screwed.
900 if (ring_first(idev
->tx_ring
) == NULL
) {
901 /* no race - tx-ring already empty */
902 vlsi_set_baud(idev
, iobase
);
903 netif_wake_queue(ndev
);
907 /* keep the speed change pending like it would
908 * for any len>0 packet. tx completion interrupt
909 * will apply it when the tx ring becomes empty.
911 spin_unlock_irqrestore(&idev
->lock
, flags
);
912 dev_kfree_skb_any(skb
);
916 /* sanity checks - simply drop the packet */
920 msg
= "ring full, but queue wasn't stopped";
924 if (rd_is_active(rd
)) {
925 msg
= "entry still owned by hw";
930 msg
= "tx ring entry without pci buffer";
935 msg
= "ring entry with old skb still attached";
939 /* no need for serialization or interrupt disable during mtt */
940 spin_unlock_irqrestore(&idev
->lock
, flags
);
942 if ((mtt
= irda_get_mtt(skb
)) > 0) {
944 ready
.tv_usec
= idev
->last_rx
.tv_usec
+ mtt
;
945 ready
.tv_sec
= idev
->last_rx
.tv_sec
;
946 if (ready
.tv_usec
>= 1000000) {
947 ready
.tv_usec
-= 1000000;
948 ready
.tv_sec
++; /* IrLAP 1.1: mtt always < 1 sec */
951 do_gettimeofday(&now
);
952 if (now
.tv_sec
> ready
.tv_sec
||
953 (now
.tv_sec
==ready
.tv_sec
&& now
.tv_usec
>=ready
.tv_usec
))
956 /* must not sleep here - called under netif_tx_lock! */
960 /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
961 * after subsequent tx-completion
964 if (idev
->mode
== IFF_SIR
) {
965 status
|= RD_TX_DISCRC
; /* no hw-crc creation */
966 len
= async_wrap_skb(skb
, rd
->buf
, r
->len
);
968 /* Some rare worst case situation in SIR mode might lead to
969 * potential buffer overflow. The wrapper detects this, returns
970 * with a shortened frame (without FCS/EOF) but doesn't provide
971 * any error indication about the invalid packet which we are
973 * Therefore we log if the buffer got filled to the point, where the
974 * wrapper would abort, i.e. when there are less than 5 bytes left to
975 * allow appending the FCS/EOF.
979 net_warn_ratelimited("%s: possible buffer overflow with SIR wrapping!\n",
983 /* hw deals with MIR/FIR mode wrapping */
984 status
|= RD_TX_PULSE
; /* send 2 us highspeed indication pulse */
987 msg
= "frame exceeds tx buffer length";
991 skb_copy_from_linear_data(skb
, rd
->buf
, len
);
994 rd
->skb
= skb
; /* remember skb for tx-complete stats */
996 rd_set_count(rd
, len
);
997 rd_set_status(rd
, status
); /* not yet active! */
999 /* give dma buffer back to busmaster-hw (flush caches to make
1000 * CPU-driven changes visible from the pci bus).
1003 pci_dma_sync_single_for_device(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
1005 /* Switching to TX mode here races with the controller
1006 * which may stop TX at any time when fetching an inactive descriptor
1007 * or one with CLR_ENTX set. So we switch on TX only, if TX was not running
1008 * _after_ the new descriptor was activated on the ring. This ensures
1009 * we will either find TX already stopped or we can be sure, there
1010 * will be a TX-complete interrupt even if the chip stopped doing
1011 * TX just after we found it still running. The ISR will then find
1012 * the non-empty ring and restart TX processing. The enclosing
1013 * spinlock provides the correct serialization to prevent race with isr.
1016 spin_lock_irqsave(&idev
->lock
,flags
);
1020 if (!(inw(iobase
+VLSI_PIO_IRENABLE
) & IRENABLE_ENTXST
)) {
1023 fifocnt
= inw(ndev
->base_addr
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
1025 pr_debug("%s: rx fifo not empty(%d)\n",
1029 config
= inw(iobase
+VLSI_PIO_IRCFG
);
1031 outw(config
| IRCFG_ENTX
, iobase
+VLSI_PIO_IRCFG
);
1033 outw(0, iobase
+VLSI_PIO_PROMPT
);
1036 if (ring_put(r
) == NULL
) {
1037 netif_stop_queue(ndev
);
1038 pr_debug("%s: tx ring full - queue stopped\n", __func__
);
1040 spin_unlock_irqrestore(&idev
->lock
, flags
);
1042 return NETDEV_TX_OK
;
1045 spin_unlock_irqrestore(&idev
->lock
, flags
);
1047 net_warn_ratelimited("%s: dropping packet - %s\n", __func__
, msg
);
1048 dev_kfree_skb_any(skb
);
1049 ndev
->stats
.tx_errors
++;
1050 ndev
->stats
.tx_dropped
++;
1051 /* Don't even think about returning NET_XMIT_DROP (=1) here!
1052 * In fact any retval!=0 causes the packet scheduler to requeue the
1053 * packet for later retry of transmission - which isn't exactly
1054 * what we want after we've just called dev_kfree_skb_any ;-)
1056 return NETDEV_TX_OK
;
1059 static void vlsi_tx_interrupt(struct net_device
*ndev
)
1061 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1062 struct vlsi_ring
*r
= idev
->tx_ring
;
1063 struct ring_descr
*rd
;
1068 for (rd
= ring_first(r
); rd
!= NULL
; rd
= ring_get(r
)) {
1070 if (rd_is_active(rd
))
1073 ret
= vlsi_process_tx(r
, rd
);
1077 ndev
->stats
.tx_errors
++;
1078 if (ret
& VLSI_TX_DROP
)
1079 ndev
->stats
.tx_dropped
++;
1080 if (ret
& VLSI_TX_FIFO
)
1081 ndev
->stats
.tx_fifo_errors
++;
1084 ndev
->stats
.tx_packets
++;
1085 ndev
->stats
.tx_bytes
+= ret
;
1089 iobase
= ndev
->base_addr
;
1091 if (idev
->new_baud
&& rd
== NULL
) /* tx ring empty and speed change pending */
1092 vlsi_set_baud(idev
, iobase
);
1094 config
= inw(iobase
+VLSI_PIO_IRCFG
);
1095 if (rd
== NULL
) /* tx ring empty: re-enable rx */
1096 outw((config
& ~IRCFG_ENTX
) | IRCFG_ENRX
, iobase
+VLSI_PIO_IRCFG
);
1098 else if (!(inw(iobase
+VLSI_PIO_IRENABLE
) & IRENABLE_ENTXST
)) {
1101 fifocnt
= inw(iobase
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
1103 pr_debug("%s: rx fifo not empty(%d)\n",
1106 outw(config
| IRCFG_ENTX
, iobase
+VLSI_PIO_IRCFG
);
1109 outw(0, iobase
+VLSI_PIO_PROMPT
);
1111 if (netif_queue_stopped(ndev
) && !idev
->new_baud
) {
1112 netif_wake_queue(ndev
);
1113 pr_debug("%s: queue awoken\n", __func__
);
1117 /* caller must have stopped the controller from busmastering */
1119 static void vlsi_unarm_tx(vlsi_irda_dev_t
*idev
)
1121 struct net_device
*ndev
= pci_get_drvdata(idev
->pdev
);
1122 struct vlsi_ring
*r
= idev
->tx_ring
;
1123 struct ring_descr
*rd
;
1126 for (rd
= ring_first(r
); rd
!= NULL
; rd
= ring_get(r
)) {
1129 if (rd_is_active(rd
)) {
1130 rd_set_status(rd
, 0);
1131 rd_set_count(rd
, 0);
1132 pci_dma_sync_single_for_cpu(r
->pdev
, rd_get_addr(rd
), r
->len
, r
->dir
);
1134 dev_kfree_skb_any(rd
->skb
);
1137 pr_debug("%s - dropping tx packet\n", __func__
);
1138 ret
= -VLSI_TX_DROP
;
1141 ret
= vlsi_process_tx(r
, rd
);
1145 ndev
->stats
.tx_errors
++;
1146 if (ret
& VLSI_TX_DROP
)
1147 ndev
->stats
.tx_dropped
++;
1148 if (ret
& VLSI_TX_FIFO
)
1149 ndev
->stats
.tx_fifo_errors
++;
1152 ndev
->stats
.tx_packets
++;
1153 ndev
->stats
.tx_bytes
+= ret
;
1159 /********************************************************/
1161 static int vlsi_start_clock(struct pci_dev
*pdev
)
1166 if (clksrc
< 2) { /* auto or PLL: try PLL */
1167 clkctl
= CLKCTL_PD_INV
| CLKCTL_CLKSTP
;
1168 pci_write_config_byte(pdev
, VLSI_PCI_CLKCTL
, clkctl
);
1170 /* procedure to detect PLL lock synchronisation:
1171 * after 0.5 msec initial delay we expect to find 3 PLL lock
1172 * indications within 10 msec for successful PLL detection.
1176 for (i
= 500; i
<= 10000; i
+= 50) { /* max 10 msec */
1177 pci_read_config_byte(pdev
, VLSI_PCI_CLKCTL
, &lock
);
1178 if (lock
&CLKCTL_LOCK
) {
1185 if (clksrc
== 1) { /* explicitly asked for PLL hence bail out */
1186 net_err_ratelimited("%s: no PLL or failed to lock!\n",
1188 clkctl
= CLKCTL_CLKSTP
;
1189 pci_write_config_byte(pdev
, VLSI_PCI_CLKCTL
, clkctl
);
1192 else /* was: clksrc=0(auto) */
1193 clksrc
= 3; /* fallback to 40MHz XCLK (OB800) */
1195 pr_debug("%s: PLL not locked, fallback to clksrc=%d\n",
1199 clksrc
= 1; /* got successful PLL lock */
1203 /* we get here if either no PLL detected in auto-mode or
1204 an external clock source was explicitly specified */
1206 clkctl
= CLKCTL_EXTCLK
| CLKCTL_CLKSTP
;
1208 clkctl
|= CLKCTL_XCKSEL
;
1209 pci_write_config_byte(pdev
, VLSI_PCI_CLKCTL
, clkctl
);
1211 /* no way to test for working XCLK */
1214 pci_read_config_byte(pdev
, VLSI_PCI_CLKCTL
, &clkctl
);
1216 /* ok, now going to connect the chip with the clock source */
1218 clkctl
&= ~CLKCTL_CLKSTP
;
1219 pci_write_config_byte(pdev
, VLSI_PCI_CLKCTL
, clkctl
);
1224 static void vlsi_stop_clock(struct pci_dev
*pdev
)
1228 /* disconnect chip from clock source */
1229 pci_read_config_byte(pdev
, VLSI_PCI_CLKCTL
, &clkctl
);
1230 clkctl
|= CLKCTL_CLKSTP
;
1231 pci_write_config_byte(pdev
, VLSI_PCI_CLKCTL
, clkctl
);
1233 /* disable all clock sources */
1234 clkctl
&= ~(CLKCTL_EXTCLK
| CLKCTL_PD_INV
);
1235 pci_write_config_byte(pdev
, VLSI_PCI_CLKCTL
, clkctl
);
1238 /********************************************************/
1240 /* writing all-zero to the VLSI PCI IO register area seems to prevent
1241 * some occasional situations where the hardware fails (symptoms are
1242 * what appears as stalled tx/rx state machines, i.e. everything ok for
1243 * receive or transmit but hw makes no progress or is unable to access
1244 * the bus memory locations).
1245 * Best place to call this is immediately after/before the internal clock
1246 * gets started/stopped.
1249 static inline void vlsi_clear_regs(unsigned iobase
)
1252 const unsigned chip_io_extent
= 32;
1254 for (i
= 0; i
< chip_io_extent
; i
+= sizeof(u16
))
1255 outw(0, iobase
+ i
);
1258 static int vlsi_init_chip(struct pci_dev
*pdev
)
1260 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1261 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1265 /* start the clock and clean the registers */
1267 if (vlsi_start_clock(pdev
)) {
1268 net_err_ratelimited("%s: no valid clock source\n", __func__
);
1271 iobase
= ndev
->base_addr
;
1272 vlsi_clear_regs(iobase
);
1274 outb(IRINTR_INT_MASK
, iobase
+VLSI_PIO_IRINTR
); /* w/c pending IRQ, disable all INT */
1276 outw(0, iobase
+VLSI_PIO_IRENABLE
); /* disable IrPHY-interface */
1278 /* disable everything, particularly IRCFG_MSTR - (also resetting the RING_PTR) */
1280 outw(0, iobase
+VLSI_PIO_IRCFG
);
1283 outw(MAX_PACKET_LENGTH
, iobase
+VLSI_PIO_MAXPKT
); /* max possible value=0x0fff */
1285 outw(BUS_TO_RINGBASE(idev
->busaddr
), iobase
+VLSI_PIO_RINGBASE
);
1287 outw(TX_RX_TO_RINGSIZE(idev
->tx_ring
->size
, idev
->rx_ring
->size
),
1288 iobase
+VLSI_PIO_RINGSIZE
);
1290 ptr
= inw(iobase
+VLSI_PIO_RINGPTR
);
1291 atomic_set(&idev
->rx_ring
->head
, RINGPTR_GET_RX(ptr
));
1292 atomic_set(&idev
->rx_ring
->tail
, RINGPTR_GET_RX(ptr
));
1293 atomic_set(&idev
->tx_ring
->head
, RINGPTR_GET_TX(ptr
));
1294 atomic_set(&idev
->tx_ring
->tail
, RINGPTR_GET_TX(ptr
));
1296 vlsi_set_baud(idev
, iobase
); /* idev->new_baud used as provided by caller */
1298 outb(IRINTR_INT_MASK
, iobase
+VLSI_PIO_IRINTR
); /* just in case - w/c pending IRQ's */
1301 /* DO NOT BLINDLY ENABLE IRINTR_ACTEN!
1302 * basically every received pulse fires an ACTIVITY-INT
1303 * leading to >>1000 INT's per second instead of few 10
1306 outb(IRINTR_RPKTEN
|IRINTR_TPKTEN
, iobase
+VLSI_PIO_IRINTR
);
1311 static int vlsi_start_hw(vlsi_irda_dev_t
*idev
)
1313 struct pci_dev
*pdev
= idev
->pdev
;
1314 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1315 unsigned iobase
= ndev
->base_addr
;
1318 /* we don't use the legacy UART, disable its address decoding */
1320 pci_read_config_byte(pdev
, VLSI_PCI_IRMISC
, &byte
);
1321 byte
&= ~(IRMISC_UARTEN
| IRMISC_UARTTST
);
1322 pci_write_config_byte(pdev
, VLSI_PCI_IRMISC
, byte
);
1324 /* enable PCI busmaster access to our 16MB page */
1326 pci_write_config_byte(pdev
, VLSI_PCI_MSTRPAGE
, MSTRPAGE_VALUE
);
1327 pci_set_master(pdev
);
1329 if (vlsi_init_chip(pdev
) < 0) {
1330 pci_disable_device(pdev
);
1334 vlsi_fill_rx(idev
->rx_ring
);
1336 do_gettimeofday(&idev
->last_rx
); /* first mtt may start from now on */
1338 outw(0, iobase
+VLSI_PIO_PROMPT
); /* kick hw state machine */
1343 static int vlsi_stop_hw(vlsi_irda_dev_t
*idev
)
1345 struct pci_dev
*pdev
= idev
->pdev
;
1346 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1347 unsigned iobase
= ndev
->base_addr
;
1348 unsigned long flags
;
1350 spin_lock_irqsave(&idev
->lock
,flags
);
1351 outw(0, iobase
+VLSI_PIO_IRENABLE
);
1352 outw(0, iobase
+VLSI_PIO_IRCFG
); /* disable everything */
1354 /* disable and w/c irqs */
1355 outb(0, iobase
+VLSI_PIO_IRINTR
);
1357 outb(IRINTR_INT_MASK
, iobase
+VLSI_PIO_IRINTR
);
1358 spin_unlock_irqrestore(&idev
->lock
,flags
);
1360 vlsi_unarm_tx(idev
);
1361 vlsi_unarm_rx(idev
);
1363 vlsi_clear_regs(iobase
);
1364 vlsi_stop_clock(pdev
);
1366 pci_disable_device(pdev
);
1371 /**************************************************************/
1373 static void vlsi_tx_timeout(struct net_device
*ndev
)
1375 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1378 vlsi_reg_debug(ndev
->base_addr
, __func__
);
1379 vlsi_ring_debug(idev
->tx_ring
);
1381 if (netif_running(ndev
))
1382 netif_stop_queue(ndev
);
1386 /* now simply restart the whole thing */
1388 if (!idev
->new_baud
)
1389 idev
->new_baud
= idev
->baud
; /* keep current baudrate */
1391 if (vlsi_start_hw(idev
))
1392 net_err_ratelimited("%s: failed to restart hw - %s(%s) unusable!\n",
1393 __func__
, pci_name(idev
->pdev
), ndev
->name
);
1395 netif_start_queue(ndev
);
1398 static int vlsi_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
1400 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1401 struct if_irda_req
*irq
= (struct if_irda_req
*) rq
;
1402 unsigned long flags
;
1407 case SIOCSBANDWIDTH
:
1408 if (!capable(CAP_NET_ADMIN
)) {
1412 spin_lock_irqsave(&idev
->lock
, flags
);
1413 idev
->new_baud
= irq
->ifr_baudrate
;
1414 /* when called from userland there might be a minor race window here
1415 * if the stack tries to change speed concurrently - which would be
1416 * pretty strange anyway with the userland having full control...
1418 vlsi_set_baud(idev
, ndev
->base_addr
);
1419 spin_unlock_irqrestore(&idev
->lock
, flags
);
1421 case SIOCSMEDIABUSY
:
1422 if (!capable(CAP_NET_ADMIN
)) {
1426 irda_device_set_media_busy(ndev
, TRUE
);
1428 case SIOCGRECEIVING
:
1429 /* the best we can do: check whether there are any bytes in rx fifo.
1430 * The trustable window (in case some data arrives just afterwards)
1431 * may be as short as 1usec or so at 4Mbps.
1433 fifocnt
= inw(ndev
->base_addr
+VLSI_PIO_RCVBCNT
) & RCVBCNT_MASK
;
1434 irq
->ifr_receiving
= (fifocnt
!=0) ? 1 : 0;
1437 net_warn_ratelimited("%s: notsupp - cmd=%04x\n",
1445 /********************************************************/
1447 static irqreturn_t
vlsi_interrupt(int irq
, void *dev_instance
)
1449 struct net_device
*ndev
= dev_instance
;
1450 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1454 unsigned long flags
;
1457 iobase
= ndev
->base_addr
;
1458 spin_lock_irqsave(&idev
->lock
,flags
);
1460 irintr
= inb(iobase
+VLSI_PIO_IRINTR
);
1462 outb(irintr
, iobase
+VLSI_PIO_IRINTR
); /* acknowledge asap */
1464 if (!(irintr
&=IRINTR_INT_MASK
)) /* not our INT - probably shared */
1469 if (unlikely(!(irintr
& ~IRINTR_ACTIVITY
)))
1470 break; /* nothing todo if only activity */
1472 if (irintr
&IRINTR_RPKTINT
)
1473 vlsi_rx_interrupt(ndev
);
1475 if (irintr
&IRINTR_TPKTINT
)
1476 vlsi_tx_interrupt(ndev
);
1478 } while (--boguscount
> 0);
1479 spin_unlock_irqrestore(&idev
->lock
,flags
);
1481 if (boguscount
<= 0)
1482 net_info_ratelimited("%s: too much work in interrupt!\n",
1484 return IRQ_RETVAL(handled
);
1487 /********************************************************/
1489 static int vlsi_open(struct net_device
*ndev
)
1491 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1495 if (pci_request_regions(idev
->pdev
, drivername
)) {
1496 net_warn_ratelimited("%s: io resource busy\n", __func__
);
1499 ndev
->base_addr
= pci_resource_start(idev
->pdev
,0);
1500 ndev
->irq
= idev
->pdev
->irq
;
1502 /* under some rare occasions the chip apparently comes up with
1503 * IRQ's pending. We better w/c pending IRQ and disable them all
1506 outb(IRINTR_INT_MASK
, ndev
->base_addr
+VLSI_PIO_IRINTR
);
1508 if (request_irq(ndev
->irq
, vlsi_interrupt
, IRQF_SHARED
,
1509 drivername
, ndev
)) {
1510 net_warn_ratelimited("%s: couldn't get IRQ: %d\n",
1511 __func__
, ndev
->irq
);
1515 if ((err
= vlsi_create_hwif(idev
)) != 0)
1518 sprintf(hwname
, "VLSI-FIR @ 0x%04x", (unsigned)ndev
->base_addr
);
1519 idev
->irlap
= irlap_open(ndev
,&idev
->qos
,hwname
);
1521 goto errout_free_ring
;
1523 do_gettimeofday(&idev
->last_rx
); /* first mtt may start from now on */
1525 idev
->new_baud
= 9600; /* start with IrPHY using 9600(SIR) mode */
1527 if ((err
= vlsi_start_hw(idev
)) != 0)
1528 goto errout_close_irlap
;
1530 netif_start_queue(ndev
);
1532 net_info_ratelimited("%s: device %s operational\n",
1533 __func__
, ndev
->name
);
1538 irlap_close(idev
->irlap
);
1540 vlsi_destroy_hwif(idev
);
1542 free_irq(ndev
->irq
,ndev
);
1544 pci_release_regions(idev
->pdev
);
1549 static int vlsi_close(struct net_device
*ndev
)
1551 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1553 netif_stop_queue(ndev
);
1556 irlap_close(idev
->irlap
);
1561 vlsi_destroy_hwif(idev
);
1563 free_irq(ndev
->irq
,ndev
);
1565 pci_release_regions(idev
->pdev
);
1567 net_info_ratelimited("%s: device %s stopped\n", __func__
, ndev
->name
);
1572 static const struct net_device_ops vlsi_netdev_ops
= {
1573 .ndo_open
= vlsi_open
,
1574 .ndo_stop
= vlsi_close
,
1575 .ndo_start_xmit
= vlsi_hard_start_xmit
,
1576 .ndo_do_ioctl
= vlsi_ioctl
,
1577 .ndo_tx_timeout
= vlsi_tx_timeout
,
1580 static int vlsi_irda_init(struct net_device
*ndev
)
1582 vlsi_irda_dev_t
*idev
= netdev_priv(ndev
);
1583 struct pci_dev
*pdev
= idev
->pdev
;
1585 ndev
->irq
= pdev
->irq
;
1586 ndev
->base_addr
= pci_resource_start(pdev
,0);
1589 * see include file for details why we need these 2 masks, in this order!
1592 if (pci_set_dma_mask(pdev
,DMA_MASK_USED_BY_HW
) ||
1593 pci_set_dma_mask(pdev
,DMA_MASK_MSTRPAGE
)) {
1594 net_err_ratelimited("%s: aborting due to PCI BM-DMA address limitations\n",
1599 irda_init_max_qos_capabilies(&idev
->qos
);
1601 /* the VLSI82C147 does not support 576000! */
1603 idev
->qos
.baud_rate
.bits
= IR_2400
| IR_9600
1604 | IR_19200
| IR_38400
| IR_57600
| IR_115200
1605 | IR_1152000
| (IR_4000000
<< 8);
1607 idev
->qos
.min_turn_time
.bits
= qos_mtt_bits
;
1609 irda_qos_bits_to_value(&idev
->qos
);
1611 /* currently no public media definitions for IrDA */
1613 ndev
->flags
|= IFF_PORTSEL
| IFF_AUTOMEDIA
;
1614 ndev
->if_port
= IF_PORT_UNKNOWN
;
1616 ndev
->netdev_ops
= &vlsi_netdev_ops
;
1617 ndev
->watchdog_timeo
= 500*HZ
/1000; /* max. allowed turn time for IrLAP */
1619 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1624 /**************************************************************/
1627 vlsi_irda_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
1629 struct net_device
*ndev
;
1630 vlsi_irda_dev_t
*idev
;
1632 if (pci_enable_device(pdev
))
1635 pdev
->current_state
= 0; /* hw must be running now */
1637 net_info_ratelimited("%s: IrDA PCI controller %s detected\n",
1638 drivername
, pci_name(pdev
));
1640 if ( !pci_resource_start(pdev
,0) ||
1641 !(pci_resource_flags(pdev
,0) & IORESOURCE_IO
) ) {
1642 net_err_ratelimited("%s: bar 0 invalid", __func__
);
1646 ndev
= alloc_irdadev(sizeof(*idev
));
1648 net_err_ratelimited("%s: Unable to allocate device memory.\n",
1653 idev
= netdev_priv(ndev
);
1655 spin_lock_init(&idev
->lock
);
1656 mutex_init(&idev
->mtx
);
1657 mutex_lock(&idev
->mtx
);
1660 if (vlsi_irda_init(ndev
) < 0)
1663 if (register_netdev(ndev
) < 0) {
1664 net_err_ratelimited("%s: register_netdev failed\n", __func__
);
1668 if (vlsi_proc_root
!= NULL
) {
1669 struct proc_dir_entry
*ent
;
1671 ent
= proc_create_data(ndev
->name
, S_IFREG
|S_IRUGO
,
1672 vlsi_proc_root
, VLSI_PROC_FOPS
, ndev
);
1674 net_warn_ratelimited("%s: failed to create proc entry\n",
1677 proc_set_size(ent
, 0);
1679 idev
->proc_entry
= ent
;
1681 net_info_ratelimited("%s: registered device %s\n",
1682 drivername
, ndev
->name
);
1684 pci_set_drvdata(pdev
, ndev
);
1685 mutex_unlock(&idev
->mtx
);
1690 mutex_unlock(&idev
->mtx
);
1693 pci_disable_device(pdev
);
1698 static void vlsi_irda_remove(struct pci_dev
*pdev
)
1700 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1701 vlsi_irda_dev_t
*idev
;
1704 net_err_ratelimited("%s: lost netdevice?\n", drivername
);
1708 unregister_netdev(ndev
);
1710 idev
= netdev_priv(ndev
);
1711 mutex_lock(&idev
->mtx
);
1712 if (idev
->proc_entry
) {
1713 remove_proc_entry(ndev
->name
, vlsi_proc_root
);
1714 idev
->proc_entry
= NULL
;
1716 mutex_unlock(&idev
->mtx
);
1720 net_info_ratelimited("%s: %s removed\n", drivername
, pci_name(pdev
));
1725 /* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
1726 * Some of the Linux PCI-PM code however depends on this, for example in
1727 * pci_set_power_state(). So we have to take care to perform the required
1728 * operations on our own (particularly reflecting the pdev->current_state)
1729 * otherwise we might get cheated by pci-pm.
1733 static int vlsi_irda_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1735 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1736 vlsi_irda_dev_t
*idev
;
1739 net_err_ratelimited("%s - %s: no netdevice\n",
1740 __func__
, pci_name(pdev
));
1743 idev
= netdev_priv(ndev
);
1744 mutex_lock(&idev
->mtx
);
1745 if (pdev
->current_state
!= 0) { /* already suspended */
1746 if (state
.event
> pdev
->current_state
) { /* simply go deeper */
1747 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1748 pdev
->current_state
= state
.event
;
1751 net_err_ratelimited("%s - %s: invalid suspend request %u -> %u\n",
1752 __func__
, pci_name(pdev
),
1753 pdev
->current_state
, state
.event
);
1754 mutex_unlock(&idev
->mtx
);
1758 if (netif_running(ndev
)) {
1759 netif_device_detach(ndev
);
1761 pci_save_state(pdev
);
1762 if (!idev
->new_baud
)
1763 /* remember speed settings to restore on resume */
1764 idev
->new_baud
= idev
->baud
;
1767 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1768 pdev
->current_state
= state
.event
;
1769 idev
->resume_ok
= 1;
1770 mutex_unlock(&idev
->mtx
);
1774 static int vlsi_irda_resume(struct pci_dev
*pdev
)
1776 struct net_device
*ndev
= pci_get_drvdata(pdev
);
1777 vlsi_irda_dev_t
*idev
;
1780 net_err_ratelimited("%s - %s: no netdevice\n",
1781 __func__
, pci_name(pdev
));
1784 idev
= netdev_priv(ndev
);
1785 mutex_lock(&idev
->mtx
);
1786 if (pdev
->current_state
== 0) {
1787 mutex_unlock(&idev
->mtx
);
1788 net_warn_ratelimited("%s - %s: already resumed\n",
1789 __func__
, pci_name(pdev
));
1793 pci_set_power_state(pdev
, PCI_D0
);
1794 pdev
->current_state
= PM_EVENT_ON
;
1796 if (!idev
->resume_ok
) {
1797 /* should be obsolete now - but used to happen due to:
1798 * - pci layer initially setting pdev->current_state = 4 (unknown)
1799 * - pci layer did not walk the save_state-tree (might be APM problem)
1800 * so we could not refuse to suspend from undefined state
1801 * - vlsi_irda_suspend detected invalid state and refused to save
1802 * configuration for resume - but was too late to stop suspending
1803 * - vlsi_irda_resume got screwed when trying to resume from garbage
1805 * now we explicitly set pdev->current_state = 0 after enabling the
1806 * device and independently resume_ok should catch any garbage config.
1808 net_warn_ratelimited("%s - hm, nothing to resume?\n", __func__
);
1809 mutex_unlock(&idev
->mtx
);
1813 if (netif_running(ndev
)) {
1814 pci_restore_state(pdev
);
1815 vlsi_start_hw(idev
);
1816 netif_device_attach(ndev
);
1818 idev
->resume_ok
= 0;
1819 mutex_unlock(&idev
->mtx
);
1823 #endif /* CONFIG_PM */
1825 /*********************************************************/
1827 static struct pci_driver vlsi_irda_driver
= {
1829 .id_table
= vlsi_irda_table
,
1830 .probe
= vlsi_irda_probe
,
1831 .remove
= vlsi_irda_remove
,
1833 .suspend
= vlsi_irda_suspend
,
1834 .resume
= vlsi_irda_resume
,
1838 #define PROC_DIR ("driver/" DRIVER_NAME)
1840 static int __init
vlsi_mod_init(void)
1844 if (clksrc
< 0 || clksrc
> 3) {
1845 net_err_ratelimited("%s: invalid clksrc=%d\n",
1846 drivername
, clksrc
);
1850 for (i
= 0; i
< 2; i
++) {
1851 switch(ringsize
[i
]) {
1859 net_warn_ratelimited("%s: invalid %s ringsize %d, using default=8\n",
1868 sirpulse
= !!sirpulse
;
1870 /* proc_mkdir returns NULL if !CONFIG_PROC_FS.
1871 * Failure to create the procfs entry is handled like running
1872 * without procfs - it's not required for the driver to work.
1874 vlsi_proc_root
= proc_mkdir(PROC_DIR
, NULL
);
1876 ret
= pci_register_driver(&vlsi_irda_driver
);
1878 if (ret
&& vlsi_proc_root
)
1879 remove_proc_entry(PROC_DIR
, NULL
);
1884 static void __exit
vlsi_mod_exit(void)
1886 pci_unregister_driver(&vlsi_irda_driver
);
1888 remove_proc_entry(PROC_DIR
, NULL
);
1891 module_init(vlsi_mod_init
);
1892 module_exit(vlsi_mod_exit
);