]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/irda/vlsi_ir.c
irda: Remove IRDA_<TYPE> logging macros
[mirror_ubuntu-bionic-kernel.git] / drivers / net / irda / vlsi_ir.c
CommitLineData
1da177e4
LT
1/*********************************************************************
2 *
3 * vlsi_ir.c: VLSI82C147 PCI IrDA controller driver for Linux
4 *
5 * Copyright (c) 2001-2003 Martin Diehl
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
e8478de3 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1da177e4
LT
19 *
20 ********************************************************************/
21
1da177e4
LT
22#include <linux/module.h>
23
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>"
28
29MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
30MODULE_AUTHOR(DRIVER_AUTHOR);
31MODULE_LICENSE("GPL");
32
33/********************************************************/
34
35#include <linux/kernel.h>
36#include <linux/init.h>
a6b7a407 37#include <linux/interrupt.h>
1da177e4
LT
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>
aa429110 46#include <linux/mutex.h>
1da177e4
LT
47#include <asm/uaccess.h>
48#include <asm/byteorder.h>
49
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>
54
55#include "vlsi_ir.h"
56
57/********************************************************/
58
59static /* const */ char drivername[] = DRIVER_NAME;
60
9baa3c34 61static const struct pci_device_id vlsi_irda_table[] = {
1da177e4
LT
62 {
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,
69 },
70 { /* all zeroes */ }
71};
72
73MODULE_DEVICE_TABLE(pci, vlsi_irda_table);
74
75/********************************************************/
76
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)
82 */
83
84static int clksrc = 0; /* default is 0(auto) */
85module_param(clksrc, int, 0);
86MODULE_PARM_DESC(clksrc, "clock input source selection");
87
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
94 */
95
96static int ringsize[] = {8,8}; /* default is tx=8 / rx=8 */
97module_param_array(ringsize, int, NULL, 0);
98MODULE_PARM_DESC(ringsize, "TX, RX ring descriptor size");
99
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.
107 */
108
109static int sirpulse = 1; /* default is 3/16 bittime */
110module_param(sirpulse, int, 0);
111MODULE_PARM_DESC(sirpulse, "SIR pulse width tuning");
112
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.
118 */
119
120static int qos_mtt_bits = 0x07; /* default is 1 ms or more */
121module_param(qos_mtt_bits, int, 0);
122MODULE_PARM_DESC(qos_mtt_bits, "IrLAP bitfield representing min-turn-time");
123
124/********************************************************/
125
126static void vlsi_reg_debug(unsigned iobase, const char *s)
127{
128 int i;
129
130 printk(KERN_DEBUG "%s: ", s);
131 for (i = 0; i < 0x20; i++)
132 printk("%02x", (unsigned)inb((iobase+i)));
133 printk("\n");
134}
135
136static void vlsi_ring_debug(struct vlsi_ring *r)
137{
138 struct ring_descr *rd;
139 unsigned i;
140
141 printk(KERN_DEBUG "%s - ring %p / size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
a97a6f10
HH
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__,
1da177e4
LT
144 atomic_read(&r->head) & r->mask, atomic_read(&r->tail) & r->mask);
145 for (i = 0; i < r->size; i++) {
146 rd = &r->rd[i];
a97a6f10 147 printk(KERN_DEBUG "%s - ring descr %u: ", __func__, i);
1da177e4
LT
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",
a97a6f10 150 __func__, (unsigned) rd_get_status(rd),
1da177e4
LT
151 (unsigned) rd_get_count(rd), (unsigned) rd_get_addr(rd));
152 }
153}
154
155/********************************************************/
156
157/* needed regardless of CONFIG_PROC_FS */
158static struct proc_dir_entry *vlsi_proc_root = NULL;
159
160#ifdef CONFIG_PROC_FS
161
162static void vlsi_proc_pdev(struct seq_file *seq, struct pci_dev *pdev)
163{
164 unsigned iobase = pci_resource_start(pdev, 0);
165 unsigned i;
166
978e9aec 167 seq_printf(seq, "\n%s (vid/did: [%04x:%04x])\n",
778a43fd 168 pci_name(pdev), (int)pdev->vendor, (int)pdev->device);
1da177e4
LT
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");
176}
177
178static void vlsi_proc_ndev(struct seq_file *seq, struct net_device *ndev)
179{
4cf1653a 180 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
181 u8 byte;
182 u16 word;
183 unsigned delta1, delta2;
184 struct timeval now;
185 unsigned iobase = ndev->base_addr;
186
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");
192
193 if (!netif_running(ndev))
194 return;
195
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"));
207 }
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);
217
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));
236
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);
276
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;
283 delta1 = 0;
284 }
285 else {
286 delta2 = 1000000 + now.tv_usec - idev->last_rx.tv_usec;
287 delta1 = 1;
288 }
289 seq_printf(seq, "last rx: %lu.%06u sec\n",
290 now.tv_sec - idev->last_rx.tv_sec - delta1, delta2);
291
292 seq_printf(seq, "RX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu",
af049081
SH
293 ndev->stats.rx_packets, ndev->stats.rx_bytes, ndev->stats.rx_errors,
294 ndev->stats.rx_dropped);
1da177e4 295 seq_printf(seq, " / overrun=%lu / length=%lu / frame=%lu / crc=%lu\n",
af049081
SH
296 ndev->stats.rx_over_errors, ndev->stats.rx_length_errors,
297 ndev->stats.rx_frame_errors, ndev->stats.rx_crc_errors);
1da177e4 298 seq_printf(seq, "TX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu / fifo=%lu\n",
af049081
SH
299 ndev->stats.tx_packets, ndev->stats.tx_bytes, ndev->stats.tx_errors,
300 ndev->stats.tx_dropped, ndev->stats.tx_fifo_errors);
1da177e4
LT
301
302}
303
304static void vlsi_proc_ring(struct seq_file *seq, struct vlsi_ring *r)
305{
306 struct ring_descr *rd;
307 unsigned i, j;
308 int h, t;
309
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);
315 if (h == t)
316 seq_printf(seq, "(empty)\n");
317 else {
318 if (((t+1)&r->mask) == h)
319 seq_printf(seq, "(full)\n");
320 else
321 seq_printf(seq, "(level = %d)\n", ((unsigned)(t-h) & r->mask));
322 rd = &r->rd[h];
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);
326 if (j > 0) {
be07b79d
AS
327 seq_printf(seq, " data: %*ph\n",
328 min_t(unsigned, j, 20), rd->buf);
1da177e4
LT
329 }
330 }
331 for (i = 0; i < r->size; i++) {
332 rd = &r->rd[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));
338 }
339}
340
341static int vlsi_seq_show(struct seq_file *seq, void *v)
342{
343 struct net_device *ndev = seq->private;
4cf1653a 344 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
345 unsigned long flags;
346
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);
355
356 spin_lock_irqsave(&idev->lock, flags);
357 if (idev->pdev != NULL) {
358 vlsi_proc_pdev(seq, idev->pdev);
359
360 if (idev->pdev->current_state == 0)
361 vlsi_proc_ndev(seq, ndev);
362 else
363 seq_printf(seq, "\nPCI controller down - resume_ok = %d\n",
364 idev->resume_ok);
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);
370 }
371 }
372 seq_printf(seq, "\n");
373 spin_unlock_irqrestore(&idev->lock, flags);
374
375 return 0;
376}
377
378static int vlsi_seq_open(struct inode *inode, struct file *file)
379{
d9dda78b 380 return single_open(file, vlsi_seq_show, PDE_DATA(inode));
1da177e4
LT
381}
382
d54b1fdb 383static const struct file_operations vlsi_proc_fops = {
1da177e4
LT
384 .owner = THIS_MODULE,
385 .open = vlsi_seq_open,
386 .read = seq_read,
387 .llseek = seq_lseek,
388 .release = single_release,
389};
390
391#define VLSI_PROC_FOPS (&vlsi_proc_fops)
392
393#else /* CONFIG_PROC_FS */
394#define VLSI_PROC_FOPS NULL
395#endif
396
397/********************************************************/
398
399static struct vlsi_ring *vlsi_alloc_ring(struct pci_dev *pdev, struct ring_descr_hw *hwmap,
400 unsigned size, unsigned len, int dir)
401{
402 struct vlsi_ring *r;
403 struct ring_descr *rd;
404 unsigned i, j;
405 dma_addr_t busaddr;
406
407 if (!size || ((size-1)&size)!=0) /* must be >0 and power of 2 */
408 return NULL;
409
410 r = kmalloc(sizeof(*r) + size * sizeof(struct ring_descr), GFP_KERNEL);
411 if (!r)
412 return NULL;
413 memset(r, 0, sizeof(*r));
414
415 r->pdev = pdev;
416 r->dir = dir;
417 r->len = len;
418 r->rd = (struct ring_descr *)(r+1);
419 r->mask = size - 1;
420 r->size = size;
421 atomic_set(&r->head, 0);
422 atomic_set(&r->tail, 0);
423
424 for (i = 0; i < size; i++) {
425 rd = r->rd + i;
426 memset(rd, 0, sizeof(*rd));
427 rd->hw = hwmap + i;
428 rd->buf = kmalloc(len, GFP_KERNEL|GFP_DMA);
8e95a202
JP
429 if (rd->buf == NULL ||
430 !(busaddr = pci_map_single(pdev, rd->buf, len, dir))) {
1da177e4 431 if (rd->buf) {
6c91023d
JP
432 net_err_ratelimited("%s: failed to create PCI-MAP for %p\n",
433 __func__, rd->buf);
1da177e4
LT
434 kfree(rd->buf);
435 rd->buf = NULL;
436 }
437 for (j = 0; j < i; j++) {
438 rd = r->rd + j;
439 busaddr = rd_get_addr(rd);
440 rd_set_addr_status(rd, 0, 0);
441 if (busaddr)
442 pci_unmap_single(pdev, busaddr, len, dir);
443 kfree(rd->buf);
444 rd->buf = NULL;
445 }
446 kfree(r);
447 return NULL;
448 }
449 rd_set_addr_status(rd, busaddr, 0);
450 /* initially, the dma buffer is owned by the CPU */
451 rd->skb = NULL;
452 }
453 return r;
454}
455
456static int vlsi_free_ring(struct vlsi_ring *r)
457{
458 struct ring_descr *rd;
459 unsigned i;
460 dma_addr_t busaddr;
461
462 for (i = 0; i < r->size; i++) {
463 rd = r->rd + i;
464 if (rd->skb)
465 dev_kfree_skb_any(rd->skb);
466 busaddr = rd_get_addr(rd);
467 rd_set_addr_status(rd, 0, 0);
468 if (busaddr)
469 pci_unmap_single(r->pdev, busaddr, r->len, r->dir);
b4558ea9 470 kfree(rd->buf);
1da177e4
LT
471 }
472 kfree(r);
473 return 0;
474}
475
476static int vlsi_create_hwif(vlsi_irda_dev_t *idev)
477{
478 char *ringarea;
479 struct ring_descr_hw *hwmap;
480
481 idev->virtaddr = NULL;
482 idev->busaddr = 0;
483
38537b7f
JP
484 ringarea = pci_zalloc_consistent(idev->pdev, HW_RING_AREA_SIZE,
485 &idev->busaddr);
6c91023d 486 if (!ringarea)
1da177e4 487 goto out;
1da177e4
LT
488
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)
493 goto out_unmap;
494
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)
499 goto out_free_rx;
500
501 idev->virtaddr = ringarea;
502 return 0;
503
504out_free_rx:
505 vlsi_free_ring(idev->rx_ring);
506out_unmap:
507 idev->rx_ring = idev->tx_ring = NULL;
508 pci_free_consistent(idev->pdev, HW_RING_AREA_SIZE, ringarea, idev->busaddr);
509 idev->busaddr = 0;
510out:
511 return -ENOMEM;
512}
513
514static int vlsi_destroy_hwif(vlsi_irda_dev_t *idev)
515{
516 vlsi_free_ring(idev->rx_ring);
517 vlsi_free_ring(idev->tx_ring);
518 idev->rx_ring = idev->tx_ring = NULL;
519
520 if (idev->busaddr)
521 pci_free_consistent(idev->pdev,HW_RING_AREA_SIZE,idev->virtaddr,idev->busaddr);
522
523 idev->virtaddr = NULL;
524 idev->busaddr = 0;
525
526 return 0;
527}
528
529/********************************************************/
530
531static int vlsi_process_rx(struct vlsi_ring *r, struct ring_descr *rd)
532{
533 u16 status;
534 int crclen, len = 0;
535 struct sk_buff *skb;
536 int ret = 0;
1ecfd462 537 struct net_device *ndev = pci_get_drvdata(r->pdev);
4cf1653a 538 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
539
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)
545 ret |= VLSI_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)
551 ret |= VLSI_RX_CRC;
552 goto done;
553 }
554
555 len = rd_get_count(rd);
556 crclen = (idev->mode==IFF_FIR) ? sizeof(u32) : sizeof(u16);
557 len -= crclen; /* remove trailing CRC */
558 if (len <= 0) {
a97a6f10 559 IRDA_DEBUG(0, "%s: strange frame (len=%d)\n", __func__, len);
1da177e4
LT
560 ret |= VLSI_RX_DROP;
561 goto done;
562 }
563
564 if (idev->mode == IFF_SIR) { /* hw checks CRC in MIR, FIR mode */
565
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.
571 */
572 le16_to_cpus(rd->buf+len);
573 if (irda_calc_crc16(INIT_FCS,rd->buf,len+crclen) != GOOD_FCS) {
a97a6f10 574 IRDA_DEBUG(0, "%s: crc error\n", __func__);
1da177e4
LT
575 ret |= VLSI_RX_CRC;
576 goto done;
577 }
578 }
579
580 if (!rd->skb) {
6c91023d 581 net_warn_ratelimited("%s: rx packet lost\n", __func__);
1da177e4
LT
582 ret |= VLSI_RX_DROP;
583 goto done;
584 }
585
586 skb = rd->skb;
587 rd->skb = NULL;
588 skb->dev = ndev;
589 memcpy(skb_put(skb,len), rd->buf, len);
459a98ed 590 skb_reset_mac_header(skb);
1da177e4
LT
591 if (in_interrupt())
592 netif_rx(skb);
593 else
594 netif_rx_ni(skb);
1da177e4
LT
595
596done:
597 rd_set_status(rd, 0);
598 rd_set_count(rd, 0);
599 /* buffer still owned by CPU */
600
601 return (ret) ? -ret : len;
602}
603
604static void vlsi_fill_rx(struct vlsi_ring *r)
605{
606 struct ring_descr *rd;
607
608 for (rd = ring_last(r); rd != NULL; rd = ring_put(r)) {
609 if (rd_is_active(rd)) {
6c91023d
JP
610 net_warn_ratelimited("%s: driver bug: rx descr race with hw\n",
611 __func__);
1da177e4
LT
612 vlsi_ring_debug(r);
613 break;
614 }
615 if (!rd->skb) {
616 rd->skb = dev_alloc_skb(IRLAP_SKB_ALLOCSIZE);
617 if (rd->skb) {
618 skb_reserve(rd->skb,1);
619 rd->skb->protocol = htons(ETH_P_IRDA);
620 }
621 else
622 break; /* probably not worth logging? */
623 }
624 /* give dma buffer back to busmaster */
625 pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
626 rd_activate(rd);
627 }
628}
629
630static void vlsi_rx_interrupt(struct net_device *ndev)
631{
4cf1653a 632 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
633 struct vlsi_ring *r = idev->rx_ring;
634 struct ring_descr *rd;
635 int ret;
636
637 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
638
639 if (rd_is_active(rd))
640 break;
641
642 ret = vlsi_process_rx(r, rd);
643
644 if (ret < 0) {
645 ret = -ret;
af049081 646 ndev->stats.rx_errors++;
1da177e4 647 if (ret & VLSI_RX_DROP)
af049081 648 ndev->stats.rx_dropped++;
1da177e4 649 if (ret & VLSI_RX_OVER)
af049081 650 ndev->stats.rx_over_errors++;
1da177e4 651 if (ret & VLSI_RX_LENGTH)
af049081 652 ndev->stats.rx_length_errors++;
1da177e4 653 if (ret & VLSI_RX_FRAME)
af049081 654 ndev->stats.rx_frame_errors++;
1da177e4 655 if (ret & VLSI_RX_CRC)
af049081 656 ndev->stats.rx_crc_errors++;
1da177e4
LT
657 }
658 else if (ret > 0) {
af049081
SH
659 ndev->stats.rx_packets++;
660 ndev->stats.rx_bytes += ret;
1da177e4
LT
661 }
662 }
663
664 do_gettimeofday(&idev->last_rx); /* remember "now" for later mtt delay */
665
666 vlsi_fill_rx(r);
667
668 if (ring_first(r) == NULL) {
669 /* we are in big trouble, if this should ever happen */
6c91023d 670 net_err_ratelimited("%s: rx ring exhausted!\n", __func__);
1da177e4
LT
671 vlsi_ring_debug(r);
672 }
673 else
674 outw(0, ndev->base_addr+VLSI_PIO_PROMPT);
675}
676
677/* caller must have stopped the controller from busmastering */
678
679static void vlsi_unarm_rx(vlsi_irda_dev_t *idev)
680{
af049081 681 struct net_device *ndev = pci_get_drvdata(idev->pdev);
1da177e4
LT
682 struct vlsi_ring *r = idev->rx_ring;
683 struct ring_descr *rd;
684 int ret;
685
686 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
687
688 ret = 0;
689 if (rd_is_active(rd)) {
690 rd_set_status(rd, 0);
691 if (rd_get_count(rd)) {
a97a6f10 692 IRDA_DEBUG(0, "%s - dropping rx packet\n", __func__);
1da177e4
LT
693 ret = -VLSI_RX_DROP;
694 }
695 rd_set_count(rd, 0);
696 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
697 if (rd->skb) {
698 dev_kfree_skb_any(rd->skb);
699 rd->skb = NULL;
700 }
701 }
702 else
703 ret = vlsi_process_rx(r, rd);
704
705 if (ret < 0) {
706 ret = -ret;
af049081 707 ndev->stats.rx_errors++;
1da177e4 708 if (ret & VLSI_RX_DROP)
af049081 709 ndev->stats.rx_dropped++;
1da177e4 710 if (ret & VLSI_RX_OVER)
af049081 711 ndev->stats.rx_over_errors++;
1da177e4 712 if (ret & VLSI_RX_LENGTH)
af049081 713 ndev->stats.rx_length_errors++;
1da177e4 714 if (ret & VLSI_RX_FRAME)
af049081 715 ndev->stats.rx_frame_errors++;
1da177e4 716 if (ret & VLSI_RX_CRC)
af049081 717 ndev->stats.rx_crc_errors++;
1da177e4
LT
718 }
719 else if (ret > 0) {
af049081
SH
720 ndev->stats.rx_packets++;
721 ndev->stats.rx_bytes += ret;
1da177e4
LT
722 }
723 }
724}
725
726/********************************************************/
727
728static int vlsi_process_tx(struct vlsi_ring *r, struct ring_descr *rd)
729{
730 u16 status;
731 int len;
732 int ret;
733
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)
738 ret = VLSI_TX_FIFO;
739 else
740 ret = 0;
741 rd_set_status(rd, 0);
742
743 if (rd->skb) {
744 len = rd->skb->len;
745 dev_kfree_skb_any(rd->skb);
746 rd->skb = NULL;
747 }
748 else /* tx-skb already freed? - should never happen */
749 len = rd_get_count(rd); /* incorrect for SIR! (due to wrapping) */
750
751 rd_set_count(rd, 0);
752 /* dma buffer still owned by the CPU */
753
754 return (ret) ? -ret : len;
755}
756
757static int vlsi_set_baud(vlsi_irda_dev_t *idev, unsigned iobase)
758{
759 u16 nphyctl;
760 u16 config;
761 unsigned mode;
762 int ret;
763 int baudrate;
764 int fifocnt;
765
766 baudrate = idev->new_baud;
a97a6f10 767 IRDA_DEBUG(2, "%s: %d -> %d\n", __func__, idev->baud, idev->new_baud);
1da177e4
LT
768 if (baudrate == 4000000) {
769 mode = IFF_FIR;
770 config = IRCFG_FIR;
771 nphyctl = PHYCTL_FIR;
772 }
773 else if (baudrate == 1152000) {
774 mode = IFF_MIR;
775 config = IRCFG_MIR | IRCFG_CRC16;
776 nphyctl = PHYCTL_MIR(clksrc==3);
777 }
778 else {
779 mode = IFF_SIR;
780 config = IRCFG_SIR | IRCFG_SIRFILT | IRCFG_RXANY;
781 switch(baudrate) {
782 default:
6c91023d
JP
783 net_warn_ratelimited("%s: undefined baudrate %d - fallback to 9600!\n",
784 __func__, baudrate);
1da177e4
LT
785 baudrate = 9600;
786 /* fallthru */
787 case 2400:
788 case 9600:
789 case 19200:
790 case 38400:
791 case 57600:
792 case 115200:
793 nphyctl = PHYCTL_SIR(baudrate,sirpulse,clksrc==3);
794 break;
795 }
796 }
797 config |= IRCFG_MSTR | IRCFG_ENRX;
798
799 fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
800 if (fifocnt != 0) {
a97a6f10 801 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __func__, fifocnt);
1da177e4
LT
802 }
803
804 outw(0, iobase+VLSI_PIO_IRENABLE);
805 outw(config, iobase+VLSI_PIO_IRCFG);
806 outw(nphyctl, iobase+VLSI_PIO_NPHYCTL);
807 wmb();
808 outw(IRENABLE_PHYANDCLOCK, iobase+VLSI_PIO_IRENABLE);
809 mb();
810
811 udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
812
813 /* read back settings for validation */
814
815 config = inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_MASK;
816
817 if (mode == IFF_FIR)
818 config ^= IRENABLE_FIR_ON;
819 else if (mode == IFF_MIR)
820 config ^= (IRENABLE_MIR_ON|IRENABLE_CRC16_ON);
821 else
822 config ^= IRENABLE_SIR_ON;
823
824 if (config != (IRENABLE_PHYANDCLOCK|IRENABLE_ENRXST)) {
6c91023d
JP
825 net_warn_ratelimited("%s: failed to set %s mode!\n",
826 __func__,
827 mode == IFF_SIR ? "SIR" :
828 mode == IFF_MIR ? "MIR" : "FIR");
1da177e4
LT
829 ret = -1;
830 }
831 else {
832 if (inw(iobase+VLSI_PIO_PHYCTL) != nphyctl) {
6c91023d
JP
833 net_warn_ratelimited("%s: failed to apply baudrate %d\n",
834 __func__, baudrate);
1da177e4
LT
835 ret = -1;
836 }
837 else {
838 idev->mode = mode;
839 idev->baud = baudrate;
840 idev->new_baud = 0;
841 ret = 0;
842 }
843 }
844
845 if (ret)
a97a6f10 846 vlsi_reg_debug(iobase,__func__);
1da177e4
LT
847
848 return ret;
849}
850
6518bbb8
SH
851static netdev_tx_t vlsi_hard_start_xmit(struct sk_buff *skb,
852 struct net_device *ndev)
1da177e4 853{
4cf1653a 854 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
855 struct vlsi_ring *r = idev->tx_ring;
856 struct ring_descr *rd;
857 unsigned long flags;
858 unsigned iobase = ndev->base_addr;
859 u8 status;
860 u16 config;
861 int mtt;
862 int len, speed;
863 struct timeval now, ready;
864 char *msg = NULL;
865
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 */
872 }
873 else
874 status = 0;
875
876 if (skb->len == 0) {
877 /* handle zero packets - should be speed change */
878 if (status == 0) {
879 msg = "bogus zero-length packet";
880 goto drop_unlock;
881 }
882
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.
898 */
899
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);
904 }
905 else
906 ;
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.
910 */
911 spin_unlock_irqrestore(&idev->lock, flags);
912 dev_kfree_skb_any(skb);
6ed10654 913 return NETDEV_TX_OK;
1da177e4
LT
914 }
915
916 /* sanity checks - simply drop the packet */
917
918 rd = ring_last(r);
919 if (!rd) {
920 msg = "ring full, but queue wasn't stopped";
921 goto drop_unlock;
922 }
923
924 if (rd_is_active(rd)) {
925 msg = "entry still owned by hw";
926 goto drop_unlock;
927 }
928
929 if (!rd->buf) {
930 msg = "tx ring entry without pci buffer";
931 goto drop_unlock;
932 }
933
934 if (rd->skb) {
935 msg = "ring entry with old skb still attached";
936 goto drop_unlock;
937 }
938
939 /* no need for serialization or interrupt disable during mtt */
940 spin_unlock_irqrestore(&idev->lock, flags);
941
942 if ((mtt = irda_get_mtt(skb)) > 0) {
943
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 */
949 }
950 for(;;) {
951 do_gettimeofday(&now);
8e95a202
JP
952 if (now.tv_sec > ready.tv_sec ||
953 (now.tv_sec==ready.tv_sec && now.tv_usec>=ready.tv_usec))
1da177e4
LT
954 break;
955 udelay(100);
932ff279 956 /* must not sleep here - called under netif_tx_lock! */
1da177e4
LT
957 }
958 }
959
960 /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
961 * after subsequent tx-completion
962 */
963
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);
967
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
972 * going to transmit.
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.
976 */
977
978 if (len >= r->len-5)
6c91023d
JP
979 net_warn_ratelimited("%s: possible buffer overflow with SIR wrapping!\n",
980 __func__);
1da177e4
LT
981 }
982 else {
983 /* hw deals with MIR/FIR mode wrapping */
984 status |= RD_TX_PULSE; /* send 2 us highspeed indication pulse */
985 len = skb->len;
986 if (len > r->len) {
987 msg = "frame exceeds tx buffer length";
988 goto drop;
989 }
990 else
d626f62b 991 skb_copy_from_linear_data(skb, rd->buf, len);
1da177e4
LT
992 }
993
994 rd->skb = skb; /* remember skb for tx-complete stats */
995
996 rd_set_count(rd, len);
997 rd_set_status(rd, status); /* not yet active! */
998
999 /* give dma buffer back to busmaster-hw (flush caches to make
1000 * CPU-driven changes visible from the pci bus).
1001 */
1002
1003 pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
1004
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.
1014 */
1015
1016 spin_lock_irqsave(&idev->lock,flags);
1017
1018 rd_activate(rd);
1019
1020 if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1021 int fifocnt;
1022
1023 fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1024 if (fifocnt != 0) {
a97a6f10 1025 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __func__, fifocnt);
1da177e4
LT
1026 }
1027
1028 config = inw(iobase+VLSI_PIO_IRCFG);
1029 mb();
1030 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1031 wmb();
1032 outw(0, iobase+VLSI_PIO_PROMPT);
1033 }
1da177e4
LT
1034
1035 if (ring_put(r) == NULL) {
1036 netif_stop_queue(ndev);
a97a6f10 1037 IRDA_DEBUG(3, "%s: tx ring full - queue stopped\n", __func__);
1da177e4
LT
1038 }
1039 spin_unlock_irqrestore(&idev->lock, flags);
1040
6ed10654 1041 return NETDEV_TX_OK;
1da177e4
LT
1042
1043drop_unlock:
1044 spin_unlock_irqrestore(&idev->lock, flags);
1045drop:
6c91023d 1046 net_warn_ratelimited("%s: dropping packet - %s\n", __func__, msg);
1da177e4 1047 dev_kfree_skb_any(skb);
af049081
SH
1048 ndev->stats.tx_errors++;
1049 ndev->stats.tx_dropped++;
1da177e4
LT
1050 /* Don't even think about returning NET_XMIT_DROP (=1) here!
1051 * In fact any retval!=0 causes the packet scheduler to requeue the
1052 * packet for later retry of transmission - which isn't exactly
1053 * what we want after we've just called dev_kfree_skb_any ;-)
1054 */
6ed10654 1055 return NETDEV_TX_OK;
1da177e4
LT
1056}
1057
1058static void vlsi_tx_interrupt(struct net_device *ndev)
1059{
4cf1653a 1060 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1061 struct vlsi_ring *r = idev->tx_ring;
1062 struct ring_descr *rd;
1063 unsigned iobase;
1064 int ret;
1065 u16 config;
1066
1067 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1068
1069 if (rd_is_active(rd))
1070 break;
1071
1072 ret = vlsi_process_tx(r, rd);
1073
1074 if (ret < 0) {
1075 ret = -ret;
af049081 1076 ndev->stats.tx_errors++;
1da177e4 1077 if (ret & VLSI_TX_DROP)
af049081 1078 ndev->stats.tx_dropped++;
1da177e4 1079 if (ret & VLSI_TX_FIFO)
af049081 1080 ndev->stats.tx_fifo_errors++;
1da177e4
LT
1081 }
1082 else if (ret > 0){
af049081
SH
1083 ndev->stats.tx_packets++;
1084 ndev->stats.tx_bytes += ret;
1da177e4
LT
1085 }
1086 }
1087
1088 iobase = ndev->base_addr;
1089
1090 if (idev->new_baud && rd == NULL) /* tx ring empty and speed change pending */
1091 vlsi_set_baud(idev, iobase);
1092
1093 config = inw(iobase+VLSI_PIO_IRCFG);
1094 if (rd == NULL) /* tx ring empty: re-enable rx */
1095 outw((config & ~IRCFG_ENTX) | IRCFG_ENRX, iobase+VLSI_PIO_IRCFG);
1096
1097 else if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1098 int fifocnt;
1099
1100 fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1101 if (fifocnt != 0) {
1102 IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n",
a97a6f10 1103 __func__, fifocnt);
1da177e4
LT
1104 }
1105 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1106 }
1107
1108 outw(0, iobase+VLSI_PIO_PROMPT);
1109
1110 if (netif_queue_stopped(ndev) && !idev->new_baud) {
1111 netif_wake_queue(ndev);
a97a6f10 1112 IRDA_DEBUG(3, "%s: queue awoken\n", __func__);
1da177e4
LT
1113 }
1114}
1115
1116/* caller must have stopped the controller from busmastering */
1117
1118static void vlsi_unarm_tx(vlsi_irda_dev_t *idev)
1119{
af049081 1120 struct net_device *ndev = pci_get_drvdata(idev->pdev);
1da177e4
LT
1121 struct vlsi_ring *r = idev->tx_ring;
1122 struct ring_descr *rd;
1123 int ret;
1124
1125 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1126
1127 ret = 0;
1128 if (rd_is_active(rd)) {
1129 rd_set_status(rd, 0);
1130 rd_set_count(rd, 0);
1131 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
1132 if (rd->skb) {
1133 dev_kfree_skb_any(rd->skb);
1134 rd->skb = NULL;
1135 }
a97a6f10 1136 IRDA_DEBUG(0, "%s - dropping tx packet\n", __func__);
1da177e4
LT
1137 ret = -VLSI_TX_DROP;
1138 }
1139 else
1140 ret = vlsi_process_tx(r, rd);
1141
1142 if (ret < 0) {
1143 ret = -ret;
af049081 1144 ndev->stats.tx_errors++;
1da177e4 1145 if (ret & VLSI_TX_DROP)
af049081 1146 ndev->stats.tx_dropped++;
1da177e4 1147 if (ret & VLSI_TX_FIFO)
af049081 1148 ndev->stats.tx_fifo_errors++;
1da177e4
LT
1149 }
1150 else if (ret > 0){
af049081
SH
1151 ndev->stats.tx_packets++;
1152 ndev->stats.tx_bytes += ret;
1da177e4
LT
1153 }
1154 }
1155
1156}
1157
1158/********************************************************/
1159
1160static int vlsi_start_clock(struct pci_dev *pdev)
1161{
1162 u8 clkctl, lock;
1163 int i, count;
1164
1165 if (clksrc < 2) { /* auto or PLL: try PLL */
1166 clkctl = CLKCTL_PD_INV | CLKCTL_CLKSTP;
1167 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1168
1169 /* procedure to detect PLL lock synchronisation:
1170 * after 0.5 msec initial delay we expect to find 3 PLL lock
1171 * indications within 10 msec for successful PLL detection.
1172 */
1173 udelay(500);
1174 count = 0;
1175 for (i = 500; i <= 10000; i += 50) { /* max 10 msec */
1176 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &lock);
1177 if (lock&CLKCTL_LOCK) {
1178 if (++count >= 3)
1179 break;
1180 }
1181 udelay(50);
1182 }
1183 if (count < 3) {
1184 if (clksrc == 1) { /* explicitly asked for PLL hence bail out */
6c91023d
JP
1185 net_err_ratelimited("%s: no PLL or failed to lock!\n",
1186 __func__);
1da177e4
LT
1187 clkctl = CLKCTL_CLKSTP;
1188 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1189 return -1;
1190 }
1191 else /* was: clksrc=0(auto) */
1192 clksrc = 3; /* fallback to 40MHz XCLK (OB800) */
1193
1194 IRDA_DEBUG(0, "%s: PLL not locked, fallback to clksrc=%d\n",
a97a6f10 1195 __func__, clksrc);
1da177e4
LT
1196 }
1197 else
1198 clksrc = 1; /* got successful PLL lock */
1199 }
1200
1201 if (clksrc != 1) {
1202 /* we get here if either no PLL detected in auto-mode or
1203 an external clock source was explicitly specified */
1204
1205 clkctl = CLKCTL_EXTCLK | CLKCTL_CLKSTP;
1206 if (clksrc == 3)
1207 clkctl |= CLKCTL_XCKSEL;
1208 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1209
1210 /* no way to test for working XCLK */
1211 }
1212 else
1213 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1214
1215 /* ok, now going to connect the chip with the clock source */
1216
1217 clkctl &= ~CLKCTL_CLKSTP;
1218 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1219
1220 return 0;
1221}
1222
1223static void vlsi_stop_clock(struct pci_dev *pdev)
1224{
1225 u8 clkctl;
1226
1227 /* disconnect chip from clock source */
1228 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1229 clkctl |= CLKCTL_CLKSTP;
1230 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1231
1232 /* disable all clock sources */
1233 clkctl &= ~(CLKCTL_EXTCLK | CLKCTL_PD_INV);
1234 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1235}
1236
1237/********************************************************/
1238
1239/* writing all-zero to the VLSI PCI IO register area seems to prevent
1240 * some occasional situations where the hardware fails (symptoms are
1241 * what appears as stalled tx/rx state machines, i.e. everything ok for
1242 * receive or transmit but hw makes no progress or is unable to access
1243 * the bus memory locations).
1244 * Best place to call this is immediately after/before the internal clock
1245 * gets started/stopped.
1246 */
1247
1248static inline void vlsi_clear_regs(unsigned iobase)
1249{
1250 unsigned i;
1251 const unsigned chip_io_extent = 32;
1252
1253 for (i = 0; i < chip_io_extent; i += sizeof(u16))
1254 outw(0, iobase + i);
1255}
1256
1257static int vlsi_init_chip(struct pci_dev *pdev)
1258{
1259 struct net_device *ndev = pci_get_drvdata(pdev);
4cf1653a 1260 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1261 unsigned iobase;
1262 u16 ptr;
1263
1264 /* start the clock and clean the registers */
1265
1266 if (vlsi_start_clock(pdev)) {
6c91023d 1267 net_err_ratelimited("%s: no valid clock source\n", __func__);
1da177e4
LT
1268 return -1;
1269 }
1270 iobase = ndev->base_addr;
1271 vlsi_clear_regs(iobase);
1272
1273 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* w/c pending IRQ, disable all INT */
1274
1275 outw(0, iobase+VLSI_PIO_IRENABLE); /* disable IrPHY-interface */
1276
1277 /* disable everything, particularly IRCFG_MSTR - (also resetting the RING_PTR) */
1278
1279 outw(0, iobase+VLSI_PIO_IRCFG);
1280 wmb();
1281
1282 outw(MAX_PACKET_LENGTH, iobase+VLSI_PIO_MAXPKT); /* max possible value=0x0fff */
1283
1284 outw(BUS_TO_RINGBASE(idev->busaddr), iobase+VLSI_PIO_RINGBASE);
1285
1286 outw(TX_RX_TO_RINGSIZE(idev->tx_ring->size, idev->rx_ring->size),
1287 iobase+VLSI_PIO_RINGSIZE);
1288
1289 ptr = inw(iobase+VLSI_PIO_RINGPTR);
1290 atomic_set(&idev->rx_ring->head, RINGPTR_GET_RX(ptr));
1291 atomic_set(&idev->rx_ring->tail, RINGPTR_GET_RX(ptr));
1292 atomic_set(&idev->tx_ring->head, RINGPTR_GET_TX(ptr));
1293 atomic_set(&idev->tx_ring->tail, RINGPTR_GET_TX(ptr));
1294
1295 vlsi_set_baud(idev, iobase); /* idev->new_baud used as provided by caller */
1296
1297 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* just in case - w/c pending IRQ's */
1298 wmb();
1299
1300 /* DO NOT BLINDLY ENABLE IRINTR_ACTEN!
1301 * basically every received pulse fires an ACTIVITY-INT
1302 * leading to >>1000 INT's per second instead of few 10
1303 */
1304
1305 outb(IRINTR_RPKTEN|IRINTR_TPKTEN, iobase+VLSI_PIO_IRINTR);
1306
1307 return 0;
1308}
1309
1310static int vlsi_start_hw(vlsi_irda_dev_t *idev)
1311{
1312 struct pci_dev *pdev = idev->pdev;
1313 struct net_device *ndev = pci_get_drvdata(pdev);
1314 unsigned iobase = ndev->base_addr;
1315 u8 byte;
1316
1317 /* we don't use the legacy UART, disable its address decoding */
1318
1319 pci_read_config_byte(pdev, VLSI_PCI_IRMISC, &byte);
1320 byte &= ~(IRMISC_UARTEN | IRMISC_UARTTST);
1321 pci_write_config_byte(pdev, VLSI_PCI_IRMISC, byte);
1322
1323 /* enable PCI busmaster access to our 16MB page */
1324
1325 pci_write_config_byte(pdev, VLSI_PCI_MSTRPAGE, MSTRPAGE_VALUE);
1326 pci_set_master(pdev);
1327
1328 if (vlsi_init_chip(pdev) < 0) {
1329 pci_disable_device(pdev);
1330 return -1;
1331 }
1332
1333 vlsi_fill_rx(idev->rx_ring);
1334
1335 do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
1336
1337 outw(0, iobase+VLSI_PIO_PROMPT); /* kick hw state machine */
1338
1339 return 0;
1340}
1341
1342static int vlsi_stop_hw(vlsi_irda_dev_t *idev)
1343{
1344 struct pci_dev *pdev = idev->pdev;
1345 struct net_device *ndev = pci_get_drvdata(pdev);
1346 unsigned iobase = ndev->base_addr;
1347 unsigned long flags;
1348
1349 spin_lock_irqsave(&idev->lock,flags);
1350 outw(0, iobase+VLSI_PIO_IRENABLE);
1351 outw(0, iobase+VLSI_PIO_IRCFG); /* disable everything */
1352
1353 /* disable and w/c irqs */
1354 outb(0, iobase+VLSI_PIO_IRINTR);
1355 wmb();
1356 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR);
1357 spin_unlock_irqrestore(&idev->lock,flags);
1358
1359 vlsi_unarm_tx(idev);
1360 vlsi_unarm_rx(idev);
1361
1362 vlsi_clear_regs(iobase);
1363 vlsi_stop_clock(pdev);
1364
1365 pci_disable_device(pdev);
1366
1367 return 0;
1368}
1369
1370/**************************************************************/
1371
1da177e4
LT
1372static void vlsi_tx_timeout(struct net_device *ndev)
1373{
4cf1653a 1374 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1375
1376
a97a6f10 1377 vlsi_reg_debug(ndev->base_addr, __func__);
1da177e4
LT
1378 vlsi_ring_debug(idev->tx_ring);
1379
1380 if (netif_running(ndev))
1381 netif_stop_queue(ndev);
1382
1383 vlsi_stop_hw(idev);
1384
1385 /* now simply restart the whole thing */
1386
1387 if (!idev->new_baud)
1388 idev->new_baud = idev->baud; /* keep current baudrate */
1389
1390 if (vlsi_start_hw(idev))
6c91023d
JP
1391 net_err_ratelimited("%s: failed to restart hw - %s(%s) unusable!\n",
1392 __func__, pci_name(idev->pdev), ndev->name);
1da177e4
LT
1393 else
1394 netif_start_queue(ndev);
1395}
1396
1397static int vlsi_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1398{
4cf1653a 1399 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1400 struct if_irda_req *irq = (struct if_irda_req *) rq;
1401 unsigned long flags;
1402 u16 fifocnt;
1403 int ret = 0;
1404
1405 switch (cmd) {
1406 case SIOCSBANDWIDTH:
1407 if (!capable(CAP_NET_ADMIN)) {
1408 ret = -EPERM;
1409 break;
1410 }
1411 spin_lock_irqsave(&idev->lock, flags);
1412 idev->new_baud = irq->ifr_baudrate;
1413 /* when called from userland there might be a minor race window here
1414 * if the stack tries to change speed concurrently - which would be
1415 * pretty strange anyway with the userland having full control...
1416 */
1417 vlsi_set_baud(idev, ndev->base_addr);
1418 spin_unlock_irqrestore(&idev->lock, flags);
1419 break;
1420 case SIOCSMEDIABUSY:
1421 if (!capable(CAP_NET_ADMIN)) {
1422 ret = -EPERM;
1423 break;
1424 }
1425 irda_device_set_media_busy(ndev, TRUE);
1426 break;
1427 case SIOCGRECEIVING:
1428 /* the best we can do: check whether there are any bytes in rx fifo.
1429 * The trustable window (in case some data arrives just afterwards)
1430 * may be as short as 1usec or so at 4Mbps.
1431 */
1432 fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1433 irq->ifr_receiving = (fifocnt!=0) ? 1 : 0;
1434 break;
1435 default:
6c91023d
JP
1436 net_warn_ratelimited("%s: notsupp - cmd=%04x\n",
1437 __func__, cmd);
1da177e4
LT
1438 ret = -EOPNOTSUPP;
1439 }
1440
1441 return ret;
1442}
1443
1444/********************************************************/
1445
7d12e780 1446static irqreturn_t vlsi_interrupt(int irq, void *dev_instance)
1da177e4
LT
1447{
1448 struct net_device *ndev = dev_instance;
4cf1653a 1449 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1450 unsigned iobase;
1451 u8 irintr;
1452 int boguscount = 5;
1453 unsigned long flags;
1454 int handled = 0;
1455
1456 iobase = ndev->base_addr;
1457 spin_lock_irqsave(&idev->lock,flags);
1458 do {
1459 irintr = inb(iobase+VLSI_PIO_IRINTR);
1460 mb();
1461 outb(irintr, iobase+VLSI_PIO_IRINTR); /* acknowledge asap */
1462
1463 if (!(irintr&=IRINTR_INT_MASK)) /* not our INT - probably shared */
1464 break;
1465
1466 handled = 1;
1467
1468 if (unlikely(!(irintr & ~IRINTR_ACTIVITY)))
1469 break; /* nothing todo if only activity */
1470
1471 if (irintr&IRINTR_RPKTINT)
1472 vlsi_rx_interrupt(ndev);
1473
1474 if (irintr&IRINTR_TPKTINT)
1475 vlsi_tx_interrupt(ndev);
1476
1477 } while (--boguscount > 0);
1478 spin_unlock_irqrestore(&idev->lock,flags);
1479
1480 if (boguscount <= 0)
6c91023d
JP
1481 net_info_ratelimited("%s: too much work in interrupt!\n",
1482 __func__);
1da177e4
LT
1483 return IRQ_RETVAL(handled);
1484}
1485
1486/********************************************************/
1487
1488static int vlsi_open(struct net_device *ndev)
1489{
4cf1653a 1490 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1491 int err = -EAGAIN;
1492 char hwname[32];
1493
1494 if (pci_request_regions(idev->pdev, drivername)) {
6c91023d 1495 net_warn_ratelimited("%s: io resource busy\n", __func__);
1da177e4
LT
1496 goto errout;
1497 }
1498 ndev->base_addr = pci_resource_start(idev->pdev,0);
1499 ndev->irq = idev->pdev->irq;
1500
1501 /* under some rare occasions the chip apparently comes up with
1502 * IRQ's pending. We better w/c pending IRQ and disable them all
1503 */
1504
1505 outb(IRINTR_INT_MASK, ndev->base_addr+VLSI_PIO_IRINTR);
1506
1fb9df5d 1507 if (request_irq(ndev->irq, vlsi_interrupt, IRQF_SHARED,
1da177e4 1508 drivername, ndev)) {
6c91023d
JP
1509 net_warn_ratelimited("%s: couldn't get IRQ: %d\n",
1510 __func__, ndev->irq);
1da177e4
LT
1511 goto errout_io;
1512 }
1513
1514 if ((err = vlsi_create_hwif(idev)) != 0)
1515 goto errout_irq;
1516
1517 sprintf(hwname, "VLSI-FIR @ 0x%04x", (unsigned)ndev->base_addr);
1518 idev->irlap = irlap_open(ndev,&idev->qos,hwname);
1519 if (!idev->irlap)
1520 goto errout_free_ring;
1521
1522 do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
1523
1524 idev->new_baud = 9600; /* start with IrPHY using 9600(SIR) mode */
1525
1526 if ((err = vlsi_start_hw(idev)) != 0)
1527 goto errout_close_irlap;
1528
1529 netif_start_queue(ndev);
1530
6c91023d
JP
1531 net_info_ratelimited("%s: device %s operational\n",
1532 __func__, ndev->name);
1da177e4
LT
1533
1534 return 0;
1535
1536errout_close_irlap:
1537 irlap_close(idev->irlap);
1538errout_free_ring:
1539 vlsi_destroy_hwif(idev);
1540errout_irq:
1541 free_irq(ndev->irq,ndev);
1542errout_io:
1543 pci_release_regions(idev->pdev);
1544errout:
1545 return err;
1546}
1547
1548static int vlsi_close(struct net_device *ndev)
1549{
4cf1653a 1550 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1551
1552 netif_stop_queue(ndev);
1553
1554 if (idev->irlap)
1555 irlap_close(idev->irlap);
1556 idev->irlap = NULL;
1557
1558 vlsi_stop_hw(idev);
1559
1560 vlsi_destroy_hwif(idev);
1561
1562 free_irq(ndev->irq,ndev);
1563
1564 pci_release_regions(idev->pdev);
1565
6c91023d 1566 net_info_ratelimited("%s: device %s stopped\n", __func__, ndev->name);
1da177e4
LT
1567
1568 return 0;
1569}
1570
30a5d7f7
SH
1571static const struct net_device_ops vlsi_netdev_ops = {
1572 .ndo_open = vlsi_open,
1573 .ndo_stop = vlsi_close,
1574 .ndo_start_xmit = vlsi_hard_start_xmit,
1575 .ndo_do_ioctl = vlsi_ioctl,
1576 .ndo_tx_timeout = vlsi_tx_timeout,
1577};
1578
1da177e4
LT
1579static int vlsi_irda_init(struct net_device *ndev)
1580{
4cf1653a 1581 vlsi_irda_dev_t *idev = netdev_priv(ndev);
1da177e4
LT
1582 struct pci_dev *pdev = idev->pdev;
1583
1da177e4
LT
1584 ndev->irq = pdev->irq;
1585 ndev->base_addr = pci_resource_start(pdev,0);
1586
1587 /* PCI busmastering
1588 * see include file for details why we need these 2 masks, in this order!
1589 */
1590
8e95a202
JP
1591 if (pci_set_dma_mask(pdev,DMA_MASK_USED_BY_HW) ||
1592 pci_set_dma_mask(pdev,DMA_MASK_MSTRPAGE)) {
6c91023d
JP
1593 net_err_ratelimited("%s: aborting due to PCI BM-DMA address limitations\n",
1594 __func__);
1da177e4
LT
1595 return -1;
1596 }
1597
1598 irda_init_max_qos_capabilies(&idev->qos);
1599
1600 /* the VLSI82C147 does not support 576000! */
1601
1602 idev->qos.baud_rate.bits = IR_2400 | IR_9600
1603 | IR_19200 | IR_38400 | IR_57600 | IR_115200
1604 | IR_1152000 | (IR_4000000 << 8);
1605
1606 idev->qos.min_turn_time.bits = qos_mtt_bits;
1607
1608 irda_qos_bits_to_value(&idev->qos);
1609
1610 /* currently no public media definitions for IrDA */
1611
1612 ndev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
1613 ndev->if_port = IF_PORT_UNKNOWN;
1614
30a5d7f7 1615 ndev->netdev_ops = &vlsi_netdev_ops;
1da177e4
LT
1616 ndev->watchdog_timeo = 500*HZ/1000; /* max. allowed turn time for IrLAP */
1617
1618 SET_NETDEV_DEV(ndev, &pdev->dev);
1619
1620 return 0;
1621}
1622
1623/**************************************************************/
1624
45ac936c 1625static int
1da177e4
LT
1626vlsi_irda_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1627{
1628 struct net_device *ndev;
1629 vlsi_irda_dev_t *idev;
1630
1631 if (pci_enable_device(pdev))
1632 goto out;
1633 else
1634 pdev->current_state = 0; /* hw must be running now */
1635
6c91023d
JP
1636 net_info_ratelimited("%s: IrDA PCI controller %s detected\n",
1637 drivername, pci_name(pdev));
1da177e4 1638
8e95a202
JP
1639 if ( !pci_resource_start(pdev,0) ||
1640 !(pci_resource_flags(pdev,0) & IORESOURCE_IO) ) {
6c91023d 1641 net_err_ratelimited("%s: bar 0 invalid", __func__);
1da177e4
LT
1642 goto out_disable;
1643 }
1644
1645 ndev = alloc_irdadev(sizeof(*idev));
1646 if (ndev==NULL) {
6c91023d
JP
1647 net_err_ratelimited("%s: Unable to allocate device memory.\n",
1648 __func__);
1da177e4
LT
1649 goto out_disable;
1650 }
1651
4cf1653a 1652 idev = netdev_priv(ndev);
1da177e4
LT
1653
1654 spin_lock_init(&idev->lock);
aa429110
MK
1655 mutex_init(&idev->mtx);
1656 mutex_lock(&idev->mtx);
1da177e4
LT
1657 idev->pdev = pdev;
1658
1659 if (vlsi_irda_init(ndev) < 0)
1660 goto out_freedev;
1661
1662 if (register_netdev(ndev) < 0) {
6c91023d 1663 net_err_ratelimited("%s: register_netdev failed\n", __func__);
1da177e4
LT
1664 goto out_freedev;
1665 }
1666
1667 if (vlsi_proc_root != NULL) {
1668 struct proc_dir_entry *ent;
1669
a95609cb
DL
1670 ent = proc_create_data(ndev->name, S_IFREG|S_IRUGO,
1671 vlsi_proc_root, VLSI_PROC_FOPS, ndev);
1da177e4 1672 if (!ent) {
6c91023d
JP
1673 net_warn_ratelimited("%s: failed to create proc entry\n",
1674 __func__);
1da177e4 1675 } else {
271a15ea 1676 proc_set_size(ent, 0);
1da177e4
LT
1677 }
1678 idev->proc_entry = ent;
1679 }
6c91023d
JP
1680 net_info_ratelimited("%s: registered device %s\n",
1681 drivername, ndev->name);
1da177e4
LT
1682
1683 pci_set_drvdata(pdev, ndev);
aa429110 1684 mutex_unlock(&idev->mtx);
1da177e4
LT
1685
1686 return 0;
1687
1688out_freedev:
aa429110 1689 mutex_unlock(&idev->mtx);
1da177e4
LT
1690 free_netdev(ndev);
1691out_disable:
1692 pci_disable_device(pdev);
1693out:
1da177e4
LT
1694 return -ENODEV;
1695}
1696
45ac936c 1697static void vlsi_irda_remove(struct pci_dev *pdev)
1da177e4
LT
1698{
1699 struct net_device *ndev = pci_get_drvdata(pdev);
1700 vlsi_irda_dev_t *idev;
1701
1702 if (!ndev) {
6c91023d 1703 net_err_ratelimited("%s: lost netdevice?\n", drivername);
1da177e4
LT
1704 return;
1705 }
1706
1707 unregister_netdev(ndev);
1708
4cf1653a 1709 idev = netdev_priv(ndev);
aa429110 1710 mutex_lock(&idev->mtx);
1da177e4
LT
1711 if (idev->proc_entry) {
1712 remove_proc_entry(ndev->name, vlsi_proc_root);
1713 idev->proc_entry = NULL;
1714 }
aa429110 1715 mutex_unlock(&idev->mtx);
1da177e4
LT
1716
1717 free_netdev(ndev);
1718
6c91023d 1719 net_info_ratelimited("%s: %s removed\n", drivername, pci_name(pdev));
1da177e4
LT
1720}
1721
1722#ifdef CONFIG_PM
1723
1724/* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
1725 * Some of the Linux PCI-PM code however depends on this, for example in
1726 * pci_set_power_state(). So we have to take care to perform the required
1727 * operations on our own (particularly reflecting the pdev->current_state)
1728 * otherwise we might get cheated by pci-pm.
1729 */
1730
1731
05adc3b7 1732static int vlsi_irda_suspend(struct pci_dev *pdev, pm_message_t state)
1da177e4
LT
1733{
1734 struct net_device *ndev = pci_get_drvdata(pdev);
1735 vlsi_irda_dev_t *idev;
1736
1da177e4 1737 if (!ndev) {
6c91023d
JP
1738 net_err_ratelimited("%s - %s: no netdevice\n",
1739 __func__, pci_name(pdev));
1da177e4
LT
1740 return 0;
1741 }
4cf1653a 1742 idev = netdev_priv(ndev);
aa429110 1743 mutex_lock(&idev->mtx);
1da177e4 1744 if (pdev->current_state != 0) { /* already suspended */
ca078bae
PM
1745 if (state.event > pdev->current_state) { /* simply go deeper */
1746 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1747 pdev->current_state = state.event;
1da177e4
LT
1748 }
1749 else
6c91023d
JP
1750 net_err_ratelimited("%s - %s: invalid suspend request %u -> %u\n",
1751 __func__, pci_name(pdev),
1752 pdev->current_state, state.event);
aa429110 1753 mutex_unlock(&idev->mtx);
1da177e4
LT
1754 return 0;
1755 }
1756
1757 if (netif_running(ndev)) {
1758 netif_device_detach(ndev);
1759 vlsi_stop_hw(idev);
1760 pci_save_state(pdev);
1761 if (!idev->new_baud)
1762 /* remember speed settings to restore on resume */
1763 idev->new_baud = idev->baud;
1764 }
1765
829ca9a3 1766 pci_set_power_state(pdev, pci_choose_state(pdev, state));
ca078bae 1767 pdev->current_state = state.event;
1da177e4 1768 idev->resume_ok = 1;
aa429110 1769 mutex_unlock(&idev->mtx);
1da177e4
LT
1770 return 0;
1771}
1772
1773static int vlsi_irda_resume(struct pci_dev *pdev)
1774{
1775 struct net_device *ndev = pci_get_drvdata(pdev);
1776 vlsi_irda_dev_t *idev;
1777
1778 if (!ndev) {
6c91023d
JP
1779 net_err_ratelimited("%s - %s: no netdevice\n",
1780 __func__, pci_name(pdev));
1da177e4
LT
1781 return 0;
1782 }
4cf1653a 1783 idev = netdev_priv(ndev);
aa429110 1784 mutex_lock(&idev->mtx);
1da177e4 1785 if (pdev->current_state == 0) {
aa429110 1786 mutex_unlock(&idev->mtx);
6c91023d
JP
1787 net_warn_ratelimited("%s - %s: already resumed\n",
1788 __func__, pci_name(pdev));
1da177e4
LT
1789 return 0;
1790 }
1791
829ca9a3
PM
1792 pci_set_power_state(pdev, PCI_D0);
1793 pdev->current_state = PM_EVENT_ON;
1da177e4
LT
1794
1795 if (!idev->resume_ok) {
1796 /* should be obsolete now - but used to happen due to:
1797 * - pci layer initially setting pdev->current_state = 4 (unknown)
1798 * - pci layer did not walk the save_state-tree (might be APM problem)
1799 * so we could not refuse to suspend from undefined state
1800 * - vlsi_irda_suspend detected invalid state and refused to save
1801 * configuration for resume - but was too late to stop suspending
1802 * - vlsi_irda_resume got screwed when trying to resume from garbage
1803 *
1804 * now we explicitly set pdev->current_state = 0 after enabling the
1805 * device and independently resume_ok should catch any garbage config.
1806 */
6c91023d 1807 net_warn_ratelimited("%s - hm, nothing to resume?\n", __func__);
aa429110 1808 mutex_unlock(&idev->mtx);
1da177e4
LT
1809 return 0;
1810 }
1811
1812 if (netif_running(ndev)) {
1813 pci_restore_state(pdev);
1814 vlsi_start_hw(idev);
1815 netif_device_attach(ndev);
1816 }
1817 idev->resume_ok = 0;
aa429110 1818 mutex_unlock(&idev->mtx);
1da177e4
LT
1819 return 0;
1820}
1821
1822#endif /* CONFIG_PM */
1823
1824/*********************************************************/
1825
1826static struct pci_driver vlsi_irda_driver = {
1827 .name = drivername,
1828 .id_table = vlsi_irda_table,
1829 .probe = vlsi_irda_probe,
45ac936c 1830 .remove = vlsi_irda_remove,
1da177e4
LT
1831#ifdef CONFIG_PM
1832 .suspend = vlsi_irda_suspend,
1833 .resume = vlsi_irda_resume,
1834#endif
1835};
1836
1837#define PROC_DIR ("driver/" DRIVER_NAME)
1838
1839static int __init vlsi_mod_init(void)
1840{
1841 int i, ret;
1842
1843 if (clksrc < 0 || clksrc > 3) {
6c91023d
JP
1844 net_err_ratelimited("%s: invalid clksrc=%d\n",
1845 drivername, clksrc);
1da177e4
LT
1846 return -1;
1847 }
1848
1849 for (i = 0; i < 2; i++) {
1850 switch(ringsize[i]) {
1851 case 4:
1852 case 8:
1853 case 16:
1854 case 32:
1855 case 64:
1856 break;
1857 default:
6c91023d
JP
1858 net_warn_ratelimited("%s: invalid %s ringsize %d, using default=8\n",
1859 drivername,
1860 i ? "rx" : "tx",
1861 ringsize[i]);
1da177e4
LT
1862 ringsize[i] = 8;
1863 break;
1864 }
1865 }
1866
1867 sirpulse = !!sirpulse;
1868
66600221 1869 /* proc_mkdir returns NULL if !CONFIG_PROC_FS.
1da177e4
LT
1870 * Failure to create the procfs entry is handled like running
1871 * without procfs - it's not required for the driver to work.
1872 */
66600221 1873 vlsi_proc_root = proc_mkdir(PROC_DIR, NULL);
1da177e4 1874
a85d771e 1875 ret = pci_register_driver(&vlsi_irda_driver);
1da177e4
LT
1876
1877 if (ret && vlsi_proc_root)
1878 remove_proc_entry(PROC_DIR, NULL);
1879 return ret;
1880
1881}
1882
1883static void __exit vlsi_mod_exit(void)
1884{
1885 pci_unregister_driver(&vlsi_irda_driver);
1886 if (vlsi_proc_root)
1887 remove_proc_entry(PROC_DIR, NULL);
1888}
1889
1890module_init(vlsi_mod_init);
1891module_exit(vlsi_mod_exit);