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