blob: a0849f49bbec7401c3c6a366e0ca5c8d98fb651f [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
Jeff Kirshere8478de2013-12-06 06:28:44 -080018 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 ********************************************************************/
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Chunyan Zhang497ec1f2015-01-08 12:01:32 +080036#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000038#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/pci.h>
40#include <linux/slab.h>
41#include <linux/netdevice.h>
42#include <linux/skbuff.h>
43#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
Chunyan Zhang497ec1f2015-01-08 12:01:32 +080046#include <linux/math64.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
Benoit Taine9baa3c32014-08-08 15:56:03 +020062static const struct pci_device_id vlsi_irda_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 {
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",
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700143 __func__, r, r->size, r->mask, r->len, r->dir, r->rd[0].hw);
144 printk(KERN_DEBUG "%s - head = %d / tail = %d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 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];
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700148 printk(KERN_DEBUG "%s - ring descr %u: ", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 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",
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700151 __func__, (unsigned) rd_get_status(rd),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 (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
Bjorn Helgaas978e9ae2008-09-22 19:20:06 -0700168 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{
Wang Chen4cf16532008-11-12 23:38:14 -0800181 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 u8 byte;
183 u16 word;
Chunyan Zhang497ec1f2015-01-08 12:01:32 +0800184 s32 sec, usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 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"));
Chunyan Zhang497ec1f2015-01-08 12:01:32 +0800280 sec = div_s64_rem(ktime_us_delta(ktime_get(), idev->last_rx),
281 USEC_PER_SEC, &usec);
282 seq_printf(seq, "last rx: %ul.%06u sec\n", sec, usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 seq_printf(seq, "RX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu",
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800285 ndev->stats.rx_packets, ndev->stats.rx_bytes, ndev->stats.rx_errors,
286 ndev->stats.rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 seq_printf(seq, " / overrun=%lu / length=%lu / frame=%lu / crc=%lu\n",
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800288 ndev->stats.rx_over_errors, ndev->stats.rx_length_errors,
289 ndev->stats.rx_frame_errors, ndev->stats.rx_crc_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 seq_printf(seq, "TX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu / fifo=%lu\n",
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800291 ndev->stats.tx_packets, ndev->stats.tx_bytes, ndev->stats.tx_errors,
292 ndev->stats.tx_dropped, ndev->stats.tx_fifo_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294}
295
296static void vlsi_proc_ring(struct seq_file *seq, struct vlsi_ring *r)
297{
298 struct ring_descr *rd;
299 unsigned i, j;
300 int h, t;
301
302 seq_printf(seq, "size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
303 r->size, r->mask, r->len, r->dir, r->rd[0].hw);
304 h = atomic_read(&r->head) & r->mask;
305 t = atomic_read(&r->tail) & r->mask;
306 seq_printf(seq, "head = %d / tail = %d ", h, t);
307 if (h == t)
308 seq_printf(seq, "(empty)\n");
309 else {
310 if (((t+1)&r->mask) == h)
311 seq_printf(seq, "(full)\n");
312 else
313 seq_printf(seq, "(level = %d)\n", ((unsigned)(t-h) & r->mask));
314 rd = &r->rd[h];
315 j = (unsigned) rd_get_count(rd);
316 seq_printf(seq, "current: rd = %d / status = %02x / len = %u\n",
317 h, (unsigned)rd_get_status(rd), j);
318 if (j > 0) {
Andy Shevchenkobe07b792014-09-09 11:48:29 +0300319 seq_printf(seq, " data: %*ph\n",
320 min_t(unsigned, j, 20), rd->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 }
323 for (i = 0; i < r->size; i++) {
324 rd = &r->rd[i];
325 seq_printf(seq, "> ring descr %u: ", i);
326 seq_printf(seq, "skb=%p data=%p hw=%p\n", rd->skb, rd->buf, rd->hw);
327 seq_printf(seq, " hw: status=%02x count=%u busaddr=0x%08x\n",
328 (unsigned) rd_get_status(rd),
329 (unsigned) rd_get_count(rd), (unsigned) rd_get_addr(rd));
330 }
331}
332
333static int vlsi_seq_show(struct seq_file *seq, void *v)
334{
335 struct net_device *ndev = seq->private;
Wang Chen4cf16532008-11-12 23:38:14 -0800336 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned long flags;
338
339 seq_printf(seq, "\n%s %s\n\n", DRIVER_NAME, DRIVER_VERSION);
340 seq_printf(seq, "clksrc: %s\n",
341 (clksrc>=2) ? ((clksrc==3)?"40MHz XCLK":"48MHz XCLK")
342 : ((clksrc==1)?"48MHz PLL":"autodetect"));
343 seq_printf(seq, "ringsize: tx=%d / rx=%d\n",
344 ringsize[0], ringsize[1]);
345 seq_printf(seq, "sirpulse: %s\n", (sirpulse)?"3/16 bittime":"short");
346 seq_printf(seq, "qos_mtt_bits: 0x%02x\n", (unsigned)qos_mtt_bits);
347
348 spin_lock_irqsave(&idev->lock, flags);
349 if (idev->pdev != NULL) {
350 vlsi_proc_pdev(seq, idev->pdev);
351
352 if (idev->pdev->current_state == 0)
353 vlsi_proc_ndev(seq, ndev);
354 else
355 seq_printf(seq, "\nPCI controller down - resume_ok = %d\n",
356 idev->resume_ok);
357 if (netif_running(ndev) && idev->rx_ring && idev->tx_ring) {
358 seq_printf(seq, "\n--------- RX ring -----------\n\n");
359 vlsi_proc_ring(seq, idev->rx_ring);
360 seq_printf(seq, "\n--------- TX ring -----------\n\n");
361 vlsi_proc_ring(seq, idev->tx_ring);
362 }
363 }
364 seq_printf(seq, "\n");
365 spin_unlock_irqrestore(&idev->lock, flags);
366
367 return 0;
368}
369
370static int vlsi_seq_open(struct inode *inode, struct file *file)
371{
Al Virod9dda782013-03-31 18:16:14 -0400372 return single_open(file, vlsi_seq_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800375static const struct file_operations vlsi_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 .owner = THIS_MODULE,
377 .open = vlsi_seq_open,
378 .read = seq_read,
379 .llseek = seq_lseek,
380 .release = single_release,
381};
382
383#define VLSI_PROC_FOPS (&vlsi_proc_fops)
384
385#else /* CONFIG_PROC_FS */
386#define VLSI_PROC_FOPS NULL
387#endif
388
389/********************************************************/
390
391static struct vlsi_ring *vlsi_alloc_ring(struct pci_dev *pdev, struct ring_descr_hw *hwmap,
392 unsigned size, unsigned len, int dir)
393{
394 struct vlsi_ring *r;
395 struct ring_descr *rd;
396 unsigned i, j;
397 dma_addr_t busaddr;
398
399 if (!size || ((size-1)&size)!=0) /* must be >0 and power of 2 */
400 return NULL;
401
402 r = kmalloc(sizeof(*r) + size * sizeof(struct ring_descr), GFP_KERNEL);
403 if (!r)
404 return NULL;
405 memset(r, 0, sizeof(*r));
406
407 r->pdev = pdev;
408 r->dir = dir;
409 r->len = len;
410 r->rd = (struct ring_descr *)(r+1);
411 r->mask = size - 1;
412 r->size = size;
413 atomic_set(&r->head, 0);
414 atomic_set(&r->tail, 0);
415
416 for (i = 0; i < size; i++) {
417 rd = r->rd + i;
418 memset(rd, 0, sizeof(*rd));
419 rd->hw = hwmap + i;
420 rd->buf = kmalloc(len, GFP_KERNEL|GFP_DMA);
Joe Perches8e95a202009-12-03 07:58:21 +0000421 if (rd->buf == NULL ||
422 !(busaddr = pci_map_single(pdev, rd->buf, len, dir))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (rd->buf) {
Joe Perches6c910232014-11-11 13:37:30 -0800424 net_err_ratelimited("%s: failed to create PCI-MAP for %p\n",
425 __func__, rd->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 kfree(rd->buf);
427 rd->buf = NULL;
428 }
429 for (j = 0; j < i; j++) {
430 rd = r->rd + j;
431 busaddr = rd_get_addr(rd);
432 rd_set_addr_status(rd, 0, 0);
433 if (busaddr)
434 pci_unmap_single(pdev, busaddr, len, dir);
435 kfree(rd->buf);
436 rd->buf = NULL;
437 }
438 kfree(r);
439 return NULL;
440 }
441 rd_set_addr_status(rd, busaddr, 0);
442 /* initially, the dma buffer is owned by the CPU */
443 rd->skb = NULL;
444 }
445 return r;
446}
447
448static int vlsi_free_ring(struct vlsi_ring *r)
449{
450 struct ring_descr *rd;
451 unsigned i;
452 dma_addr_t busaddr;
453
454 for (i = 0; i < r->size; i++) {
455 rd = r->rd + i;
456 if (rd->skb)
457 dev_kfree_skb_any(rd->skb);
458 busaddr = rd_get_addr(rd);
459 rd_set_addr_status(rd, 0, 0);
460 if (busaddr)
461 pci_unmap_single(r->pdev, busaddr, r->len, r->dir);
Jesper Juhlb4558ea2005-10-28 16:53:13 -0400462 kfree(rd->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 kfree(r);
465 return 0;
466}
467
468static int vlsi_create_hwif(vlsi_irda_dev_t *idev)
469{
470 char *ringarea;
471 struct ring_descr_hw *hwmap;
472
473 idev->virtaddr = NULL;
474 idev->busaddr = 0;
475
Joe Perches38537b72014-08-08 14:24:36 -0700476 ringarea = pci_zalloc_consistent(idev->pdev, HW_RING_AREA_SIZE,
477 &idev->busaddr);
Joe Perches6c910232014-11-11 13:37:30 -0800478 if (!ringarea)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 hwmap = (struct ring_descr_hw *)ringarea;
482 idev->rx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[1],
483 XFER_BUF_SIZE, PCI_DMA_FROMDEVICE);
484 if (idev->rx_ring == NULL)
485 goto out_unmap;
486
487 hwmap += MAX_RING_DESCR;
488 idev->tx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[0],
489 XFER_BUF_SIZE, PCI_DMA_TODEVICE);
490 if (idev->tx_ring == NULL)
491 goto out_free_rx;
492
493 idev->virtaddr = ringarea;
494 return 0;
495
496out_free_rx:
497 vlsi_free_ring(idev->rx_ring);
498out_unmap:
499 idev->rx_ring = idev->tx_ring = NULL;
500 pci_free_consistent(idev->pdev, HW_RING_AREA_SIZE, ringarea, idev->busaddr);
501 idev->busaddr = 0;
502out:
503 return -ENOMEM;
504}
505
506static int vlsi_destroy_hwif(vlsi_irda_dev_t *idev)
507{
508 vlsi_free_ring(idev->rx_ring);
509 vlsi_free_ring(idev->tx_ring);
510 idev->rx_ring = idev->tx_ring = NULL;
511
512 if (idev->busaddr)
513 pci_free_consistent(idev->pdev,HW_RING_AREA_SIZE,idev->virtaddr,idev->busaddr);
514
515 idev->virtaddr = NULL;
516 idev->busaddr = 0;
517
518 return 0;
519}
520
521/********************************************************/
522
523static int vlsi_process_rx(struct vlsi_ring *r, struct ring_descr *rd)
524{
525 u16 status;
526 int crclen, len = 0;
527 struct sk_buff *skb;
528 int ret = 0;
Jingoo Han1ecfd462013-09-09 14:24:24 +0900529 struct net_device *ndev = pci_get_drvdata(r->pdev);
Wang Chen4cf16532008-11-12 23:38:14 -0800530 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
533 /* dma buffer now owned by the CPU */
534 status = rd_get_status(rd);
535 if (status & RD_RX_ERROR) {
536 if (status & RD_RX_OVER)
537 ret |= VLSI_RX_OVER;
538 if (status & RD_RX_LENGTH)
539 ret |= VLSI_RX_LENGTH;
540 if (status & RD_RX_PHYERR)
541 ret |= VLSI_RX_FRAME;
542 if (status & RD_RX_CRCERR)
543 ret |= VLSI_RX_CRC;
544 goto done;
545 }
546
547 len = rd_get_count(rd);
548 crclen = (idev->mode==IFF_FIR) ? sizeof(u32) : sizeof(u16);
549 len -= crclen; /* remove trailing CRC */
550 if (len <= 0) {
Joe Perches955a9d202014-11-11 14:44:57 -0800551 pr_debug("%s: strange frame (len=%d)\n", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ret |= VLSI_RX_DROP;
553 goto done;
554 }
555
556 if (idev->mode == IFF_SIR) { /* hw checks CRC in MIR, FIR mode */
557
558 /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
559 * endian-adjustment there just in place will dirty a cache line
560 * which belongs to the map and thus we must be sure it will
561 * get flushed before giving the buffer back to hardware.
562 * vlsi_fill_rx() will do this anyway - but here we rely on.
563 */
564 le16_to_cpus(rd->buf+len);
565 if (irda_calc_crc16(INIT_FCS,rd->buf,len+crclen) != GOOD_FCS) {
Joe Perches955a9d202014-11-11 14:44:57 -0800566 pr_debug("%s: crc error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ret |= VLSI_RX_CRC;
568 goto done;
569 }
570 }
571
572 if (!rd->skb) {
Joe Perches6c910232014-11-11 13:37:30 -0800573 net_warn_ratelimited("%s: rx packet lost\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ret |= VLSI_RX_DROP;
575 goto done;
576 }
577
578 skb = rd->skb;
579 rd->skb = NULL;
580 skb->dev = ndev;
581 memcpy(skb_put(skb,len), rd->buf, len);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700582 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (in_interrupt())
584 netif_rx(skb);
585 else
586 netif_rx_ni(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588done:
589 rd_set_status(rd, 0);
590 rd_set_count(rd, 0);
591 /* buffer still owned by CPU */
592
593 return (ret) ? -ret : len;
594}
595
596static void vlsi_fill_rx(struct vlsi_ring *r)
597{
598 struct ring_descr *rd;
599
600 for (rd = ring_last(r); rd != NULL; rd = ring_put(r)) {
601 if (rd_is_active(rd)) {
Joe Perches6c910232014-11-11 13:37:30 -0800602 net_warn_ratelimited("%s: driver bug: rx descr race with hw\n",
603 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 vlsi_ring_debug(r);
605 break;
606 }
607 if (!rd->skb) {
608 rd->skb = dev_alloc_skb(IRLAP_SKB_ALLOCSIZE);
609 if (rd->skb) {
610 skb_reserve(rd->skb,1);
611 rd->skb->protocol = htons(ETH_P_IRDA);
612 }
613 else
614 break; /* probably not worth logging? */
615 }
616 /* give dma buffer back to busmaster */
617 pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
618 rd_activate(rd);
619 }
620}
621
622static void vlsi_rx_interrupt(struct net_device *ndev)
623{
Wang Chen4cf16532008-11-12 23:38:14 -0800624 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct vlsi_ring *r = idev->rx_ring;
626 struct ring_descr *rd;
627 int ret;
628
629 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
630
631 if (rd_is_active(rd))
632 break;
633
634 ret = vlsi_process_rx(r, rd);
635
636 if (ret < 0) {
637 ret = -ret;
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800638 ndev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (ret & VLSI_RX_DROP)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800640 ndev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (ret & VLSI_RX_OVER)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800642 ndev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (ret & VLSI_RX_LENGTH)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800644 ndev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (ret & VLSI_RX_FRAME)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800646 ndev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (ret & VLSI_RX_CRC)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800648 ndev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 else if (ret > 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800651 ndev->stats.rx_packets++;
652 ndev->stats.rx_bytes += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 }
655
Chunyan Zhang497ec1f2015-01-08 12:01:32 +0800656 idev->last_rx = ktime_get(); /* remember "now" for later mtt delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 vlsi_fill_rx(r);
659
660 if (ring_first(r) == NULL) {
661 /* we are in big trouble, if this should ever happen */
Joe Perches6c910232014-11-11 13:37:30 -0800662 net_err_ratelimited("%s: rx ring exhausted!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 vlsi_ring_debug(r);
664 }
665 else
666 outw(0, ndev->base_addr+VLSI_PIO_PROMPT);
667}
668
669/* caller must have stopped the controller from busmastering */
670
671static void vlsi_unarm_rx(vlsi_irda_dev_t *idev)
672{
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800673 struct net_device *ndev = pci_get_drvdata(idev->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct vlsi_ring *r = idev->rx_ring;
675 struct ring_descr *rd;
676 int ret;
677
678 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
679
680 ret = 0;
681 if (rd_is_active(rd)) {
682 rd_set_status(rd, 0);
683 if (rd_get_count(rd)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800684 pr_debug("%s - dropping rx packet\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ret = -VLSI_RX_DROP;
686 }
687 rd_set_count(rd, 0);
688 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
689 if (rd->skb) {
690 dev_kfree_skb_any(rd->skb);
691 rd->skb = NULL;
692 }
693 }
694 else
695 ret = vlsi_process_rx(r, rd);
696
697 if (ret < 0) {
698 ret = -ret;
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800699 ndev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (ret & VLSI_RX_DROP)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800701 ndev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (ret & VLSI_RX_OVER)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800703 ndev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (ret & VLSI_RX_LENGTH)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800705 ndev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (ret & VLSI_RX_FRAME)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800707 ndev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (ret & VLSI_RX_CRC)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800709 ndev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 else if (ret > 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800712 ndev->stats.rx_packets++;
713 ndev->stats.rx_bytes += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 }
716}
717
718/********************************************************/
719
720static int vlsi_process_tx(struct vlsi_ring *r, struct ring_descr *rd)
721{
722 u16 status;
723 int len;
724 int ret;
725
726 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
727 /* dma buffer now owned by the CPU */
728 status = rd_get_status(rd);
729 if (status & RD_TX_UNDRN)
730 ret = VLSI_TX_FIFO;
731 else
732 ret = 0;
733 rd_set_status(rd, 0);
734
735 if (rd->skb) {
736 len = rd->skb->len;
737 dev_kfree_skb_any(rd->skb);
738 rd->skb = NULL;
739 }
740 else /* tx-skb already freed? - should never happen */
741 len = rd_get_count(rd); /* incorrect for SIR! (due to wrapping) */
742
743 rd_set_count(rd, 0);
744 /* dma buffer still owned by the CPU */
745
746 return (ret) ? -ret : len;
747}
748
749static int vlsi_set_baud(vlsi_irda_dev_t *idev, unsigned iobase)
750{
751 u16 nphyctl;
752 u16 config;
753 unsigned mode;
754 int ret;
755 int baudrate;
756 int fifocnt;
757
758 baudrate = idev->new_baud;
Joe Perches955a9d202014-11-11 14:44:57 -0800759 pr_debug("%s: %d -> %d\n", __func__, idev->baud, idev->new_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (baudrate == 4000000) {
761 mode = IFF_FIR;
762 config = IRCFG_FIR;
763 nphyctl = PHYCTL_FIR;
764 }
765 else if (baudrate == 1152000) {
766 mode = IFF_MIR;
767 config = IRCFG_MIR | IRCFG_CRC16;
768 nphyctl = PHYCTL_MIR(clksrc==3);
769 }
770 else {
771 mode = IFF_SIR;
772 config = IRCFG_SIR | IRCFG_SIRFILT | IRCFG_RXANY;
773 switch(baudrate) {
774 default:
Joe Perches6c910232014-11-11 13:37:30 -0800775 net_warn_ratelimited("%s: undefined baudrate %d - fallback to 9600!\n",
776 __func__, baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 baudrate = 9600;
778 /* fallthru */
779 case 2400:
780 case 9600:
781 case 19200:
782 case 38400:
783 case 57600:
784 case 115200:
785 nphyctl = PHYCTL_SIR(baudrate,sirpulse,clksrc==3);
786 break;
787 }
788 }
789 config |= IRCFG_MSTR | IRCFG_ENRX;
790
791 fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
792 if (fifocnt != 0) {
Joe Perches955a9d202014-11-11 14:44:57 -0800793 pr_debug("%s: rx fifo not empty(%d)\n", __func__, fifocnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 outw(0, iobase+VLSI_PIO_IRENABLE);
797 outw(config, iobase+VLSI_PIO_IRCFG);
798 outw(nphyctl, iobase+VLSI_PIO_NPHYCTL);
799 wmb();
800 outw(IRENABLE_PHYANDCLOCK, iobase+VLSI_PIO_IRENABLE);
801 mb();
802
803 udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
804
805 /* read back settings for validation */
806
807 config = inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_MASK;
808
809 if (mode == IFF_FIR)
810 config ^= IRENABLE_FIR_ON;
811 else if (mode == IFF_MIR)
812 config ^= (IRENABLE_MIR_ON|IRENABLE_CRC16_ON);
813 else
814 config ^= IRENABLE_SIR_ON;
815
816 if (config != (IRENABLE_PHYANDCLOCK|IRENABLE_ENRXST)) {
Joe Perches6c910232014-11-11 13:37:30 -0800817 net_warn_ratelimited("%s: failed to set %s mode!\n",
818 __func__,
819 mode == IFF_SIR ? "SIR" :
820 mode == IFF_MIR ? "MIR" : "FIR");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ret = -1;
822 }
823 else {
824 if (inw(iobase+VLSI_PIO_PHYCTL) != nphyctl) {
Joe Perches6c910232014-11-11 13:37:30 -0800825 net_warn_ratelimited("%s: failed to apply baudrate %d\n",
826 __func__, baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ret = -1;
828 }
829 else {
830 idev->mode = mode;
831 idev->baud = baudrate;
832 idev->new_baud = 0;
833 ret = 0;
834 }
835 }
836
837 if (ret)
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700838 vlsi_reg_debug(iobase,__func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 return ret;
841}
842
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000843static netdev_tx_t vlsi_hard_start_xmit(struct sk_buff *skb,
844 struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Wang Chen4cf16532008-11-12 23:38:14 -0800846 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct vlsi_ring *r = idev->tx_ring;
848 struct ring_descr *rd;
849 unsigned long flags;
850 unsigned iobase = ndev->base_addr;
851 u8 status;
852 u16 config;
Chunyan Zhang497ec1f2015-01-08 12:01:32 +0800853 int mtt, diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int len, speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 char *msg = NULL;
856
857 speed = irda_get_next_speed(skb);
858 spin_lock_irqsave(&idev->lock, flags);
859 if (speed != -1 && speed != idev->baud) {
860 netif_stop_queue(ndev);
861 idev->new_baud = speed;
862 status = RD_TX_CLRENTX; /* stop tx-ring after this frame */
863 }
864 else
865 status = 0;
866
867 if (skb->len == 0) {
868 /* handle zero packets - should be speed change */
869 if (status == 0) {
870 msg = "bogus zero-length packet";
871 goto drop_unlock;
872 }
873
874 /* due to the completely asynch tx operation we might have
875 * IrLAP racing with the hardware here, f.e. if the controller
876 * is just sending the last packet with current speed while
877 * the LAP is already switching the speed using synchronous
878 * len=0 packet. Immediate execution would lead to hw lockup
879 * requiring a powercycle to reset. Good candidate to trigger
880 * this is the final UA:RSP packet after receiving a DISC:CMD
881 * when getting the LAP down.
882 * Note that we are not protected by the queue_stop approach
883 * because the final UA:RSP arrives _without_ request to apply
884 * new-speed-after-this-packet - hence the driver doesn't know
885 * this was the last packet and doesn't stop the queue. So the
886 * forced switch to default speed from LAP gets through as fast
887 * as only some 10 usec later while the UA:RSP is still processed
888 * by the hardware and we would get screwed.
889 */
890
891 if (ring_first(idev->tx_ring) == NULL) {
892 /* no race - tx-ring already empty */
893 vlsi_set_baud(idev, iobase);
894 netif_wake_queue(ndev);
895 }
896 else
897 ;
898 /* keep the speed change pending like it would
899 * for any len>0 packet. tx completion interrupt
900 * will apply it when the tx ring becomes empty.
901 */
902 spin_unlock_irqrestore(&idev->lock, flags);
903 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000904 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
907 /* sanity checks - simply drop the packet */
908
909 rd = ring_last(r);
910 if (!rd) {
911 msg = "ring full, but queue wasn't stopped";
912 goto drop_unlock;
913 }
914
915 if (rd_is_active(rd)) {
916 msg = "entry still owned by hw";
917 goto drop_unlock;
918 }
919
920 if (!rd->buf) {
921 msg = "tx ring entry without pci buffer";
922 goto drop_unlock;
923 }
924
925 if (rd->skb) {
926 msg = "ring entry with old skb still attached";
927 goto drop_unlock;
928 }
929
930 /* no need for serialization or interrupt disable during mtt */
931 spin_unlock_irqrestore(&idev->lock, flags);
932
933 if ((mtt = irda_get_mtt(skb)) > 0) {
Chunyan Zhang497ec1f2015-01-08 12:01:32 +0800934 diff = ktime_us_delta(ktime_get(), idev->last_rx);
935 if (mtt > diff)
936 udelay(mtt - diff);
Herbert Xu932ff272006-06-09 12:20:56 -0700937 /* must not sleep here - called under netif_tx_lock! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
941 * after subsequent tx-completion
942 */
943
944 if (idev->mode == IFF_SIR) {
945 status |= RD_TX_DISCRC; /* no hw-crc creation */
946 len = async_wrap_skb(skb, rd->buf, r->len);
947
948 /* Some rare worst case situation in SIR mode might lead to
949 * potential buffer overflow. The wrapper detects this, returns
950 * with a shortened frame (without FCS/EOF) but doesn't provide
951 * any error indication about the invalid packet which we are
952 * going to transmit.
953 * Therefore we log if the buffer got filled to the point, where the
954 * wrapper would abort, i.e. when there are less than 5 bytes left to
955 * allow appending the FCS/EOF.
956 */
957
958 if (len >= r->len-5)
Joe Perches6c910232014-11-11 13:37:30 -0800959 net_warn_ratelimited("%s: possible buffer overflow with SIR wrapping!\n",
960 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 else {
963 /* hw deals with MIR/FIR mode wrapping */
964 status |= RD_TX_PULSE; /* send 2 us highspeed indication pulse */
965 len = skb->len;
966 if (len > r->len) {
967 msg = "frame exceeds tx buffer length";
968 goto drop;
969 }
970 else
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300971 skb_copy_from_linear_data(skb, rd->buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
974 rd->skb = skb; /* remember skb for tx-complete stats */
975
976 rd_set_count(rd, len);
977 rd_set_status(rd, status); /* not yet active! */
978
979 /* give dma buffer back to busmaster-hw (flush caches to make
980 * CPU-driven changes visible from the pci bus).
981 */
982
983 pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
984
985/* Switching to TX mode here races with the controller
986 * which may stop TX at any time when fetching an inactive descriptor
987 * or one with CLR_ENTX set. So we switch on TX only, if TX was not running
988 * _after_ the new descriptor was activated on the ring. This ensures
989 * we will either find TX already stopped or we can be sure, there
990 * will be a TX-complete interrupt even if the chip stopped doing
991 * TX just after we found it still running. The ISR will then find
992 * the non-empty ring and restart TX processing. The enclosing
993 * spinlock provides the correct serialization to prevent race with isr.
994 */
995
996 spin_lock_irqsave(&idev->lock,flags);
997
998 rd_activate(rd);
999
1000 if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1001 int fifocnt;
1002
1003 fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1004 if (fifocnt != 0) {
Joe Perches955a9d202014-11-11 14:44:57 -08001005 pr_debug("%s: rx fifo not empty(%d)\n",
1006 __func__, fifocnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
1009 config = inw(iobase+VLSI_PIO_IRCFG);
1010 mb();
1011 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1012 wmb();
1013 outw(0, iobase+VLSI_PIO_PROMPT);
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (ring_put(r) == NULL) {
1017 netif_stop_queue(ndev);
Joe Perches955a9d202014-11-11 14:44:57 -08001018 pr_debug("%s: tx ring full - queue stopped\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 spin_unlock_irqrestore(&idev->lock, flags);
1021
Patrick McHardy6ed10652009-06-23 06:03:08 +00001022 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024drop_unlock:
1025 spin_unlock_irqrestore(&idev->lock, flags);
1026drop:
Joe Perches6c910232014-11-11 13:37:30 -08001027 net_warn_ratelimited("%s: dropping packet - %s\n", __func__, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 dev_kfree_skb_any(skb);
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001029 ndev->stats.tx_errors++;
1030 ndev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* Don't even think about returning NET_XMIT_DROP (=1) here!
1032 * In fact any retval!=0 causes the packet scheduler to requeue the
1033 * packet for later retry of transmission - which isn't exactly
1034 * what we want after we've just called dev_kfree_skb_any ;-)
1035 */
Patrick McHardy6ed10652009-06-23 06:03:08 +00001036 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039static void vlsi_tx_interrupt(struct net_device *ndev)
1040{
Wang Chen4cf16532008-11-12 23:38:14 -08001041 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct vlsi_ring *r = idev->tx_ring;
1043 struct ring_descr *rd;
1044 unsigned iobase;
1045 int ret;
1046 u16 config;
1047
1048 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1049
1050 if (rd_is_active(rd))
1051 break;
1052
1053 ret = vlsi_process_tx(r, rd);
1054
1055 if (ret < 0) {
1056 ret = -ret;
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001057 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (ret & VLSI_TX_DROP)
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001059 ndev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (ret & VLSI_TX_FIFO)
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001061 ndev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 else if (ret > 0){
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001064 ndev->stats.tx_packets++;
1065 ndev->stats.tx_bytes += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 }
1068
1069 iobase = ndev->base_addr;
1070
1071 if (idev->new_baud && rd == NULL) /* tx ring empty and speed change pending */
1072 vlsi_set_baud(idev, iobase);
1073
1074 config = inw(iobase+VLSI_PIO_IRCFG);
1075 if (rd == NULL) /* tx ring empty: re-enable rx */
1076 outw((config & ~IRCFG_ENTX) | IRCFG_ENRX, iobase+VLSI_PIO_IRCFG);
1077
1078 else if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
1079 int fifocnt;
1080
1081 fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1082 if (fifocnt != 0) {
Joe Perches955a9d202014-11-11 14:44:57 -08001083 pr_debug("%s: rx fifo not empty(%d)\n",
1084 __func__, fifocnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
1087 }
1088
1089 outw(0, iobase+VLSI_PIO_PROMPT);
1090
1091 if (netif_queue_stopped(ndev) && !idev->new_baud) {
1092 netif_wake_queue(ndev);
Joe Perches955a9d202014-11-11 14:44:57 -08001093 pr_debug("%s: queue awoken\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095}
1096
1097/* caller must have stopped the controller from busmastering */
1098
1099static void vlsi_unarm_tx(vlsi_irda_dev_t *idev)
1100{
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001101 struct net_device *ndev = pci_get_drvdata(idev->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct vlsi_ring *r = idev->tx_ring;
1103 struct ring_descr *rd;
1104 int ret;
1105
1106 for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
1107
1108 ret = 0;
1109 if (rd_is_active(rd)) {
1110 rd_set_status(rd, 0);
1111 rd_set_count(rd, 0);
1112 pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
1113 if (rd->skb) {
1114 dev_kfree_skb_any(rd->skb);
1115 rd->skb = NULL;
1116 }
Joe Perches955a9d202014-11-11 14:44:57 -08001117 pr_debug("%s - dropping tx packet\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ret = -VLSI_TX_DROP;
1119 }
1120 else
1121 ret = vlsi_process_tx(r, rd);
1122
1123 if (ret < 0) {
1124 ret = -ret;
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001125 ndev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (ret & VLSI_TX_DROP)
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001127 ndev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (ret & VLSI_TX_FIFO)
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001129 ndev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 else if (ret > 0){
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001132 ndev->stats.tx_packets++;
1133 ndev->stats.tx_bytes += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 }
1136
1137}
1138
1139/********************************************************/
1140
1141static int vlsi_start_clock(struct pci_dev *pdev)
1142{
1143 u8 clkctl, lock;
1144 int i, count;
1145
1146 if (clksrc < 2) { /* auto or PLL: try PLL */
1147 clkctl = CLKCTL_PD_INV | CLKCTL_CLKSTP;
1148 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1149
1150 /* procedure to detect PLL lock synchronisation:
1151 * after 0.5 msec initial delay we expect to find 3 PLL lock
1152 * indications within 10 msec for successful PLL detection.
1153 */
1154 udelay(500);
1155 count = 0;
1156 for (i = 500; i <= 10000; i += 50) { /* max 10 msec */
1157 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &lock);
1158 if (lock&CLKCTL_LOCK) {
1159 if (++count >= 3)
1160 break;
1161 }
1162 udelay(50);
1163 }
1164 if (count < 3) {
1165 if (clksrc == 1) { /* explicitly asked for PLL hence bail out */
Joe Perches6c910232014-11-11 13:37:30 -08001166 net_err_ratelimited("%s: no PLL or failed to lock!\n",
1167 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 clkctl = CLKCTL_CLKSTP;
1169 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1170 return -1;
1171 }
1172 else /* was: clksrc=0(auto) */
1173 clksrc = 3; /* fallback to 40MHz XCLK (OB800) */
1174
Joe Perches955a9d202014-11-11 14:44:57 -08001175 pr_debug("%s: PLL not locked, fallback to clksrc=%d\n",
1176 __func__, clksrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178 else
1179 clksrc = 1; /* got successful PLL lock */
1180 }
1181
1182 if (clksrc != 1) {
1183 /* we get here if either no PLL detected in auto-mode or
1184 an external clock source was explicitly specified */
1185
1186 clkctl = CLKCTL_EXTCLK | CLKCTL_CLKSTP;
1187 if (clksrc == 3)
1188 clkctl |= CLKCTL_XCKSEL;
1189 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1190
1191 /* no way to test for working XCLK */
1192 }
1193 else
1194 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1195
1196 /* ok, now going to connect the chip with the clock source */
1197
1198 clkctl &= ~CLKCTL_CLKSTP;
1199 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1200
1201 return 0;
1202}
1203
1204static void vlsi_stop_clock(struct pci_dev *pdev)
1205{
1206 u8 clkctl;
1207
1208 /* disconnect chip from clock source */
1209 pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
1210 clkctl |= CLKCTL_CLKSTP;
1211 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1212
1213 /* disable all clock sources */
1214 clkctl &= ~(CLKCTL_EXTCLK | CLKCTL_PD_INV);
1215 pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
1216}
1217
1218/********************************************************/
1219
1220/* writing all-zero to the VLSI PCI IO register area seems to prevent
1221 * some occasional situations where the hardware fails (symptoms are
1222 * what appears as stalled tx/rx state machines, i.e. everything ok for
1223 * receive or transmit but hw makes no progress or is unable to access
1224 * the bus memory locations).
1225 * Best place to call this is immediately after/before the internal clock
1226 * gets started/stopped.
1227 */
1228
1229static inline void vlsi_clear_regs(unsigned iobase)
1230{
1231 unsigned i;
1232 const unsigned chip_io_extent = 32;
1233
1234 for (i = 0; i < chip_io_extent; i += sizeof(u16))
1235 outw(0, iobase + i);
1236}
1237
1238static int vlsi_init_chip(struct pci_dev *pdev)
1239{
1240 struct net_device *ndev = pci_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08001241 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 unsigned iobase;
1243 u16 ptr;
1244
1245 /* start the clock and clean the registers */
1246
1247 if (vlsi_start_clock(pdev)) {
Joe Perches6c910232014-11-11 13:37:30 -08001248 net_err_ratelimited("%s: no valid clock source\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -1;
1250 }
1251 iobase = ndev->base_addr;
1252 vlsi_clear_regs(iobase);
1253
1254 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* w/c pending IRQ, disable all INT */
1255
1256 outw(0, iobase+VLSI_PIO_IRENABLE); /* disable IrPHY-interface */
1257
1258 /* disable everything, particularly IRCFG_MSTR - (also resetting the RING_PTR) */
1259
1260 outw(0, iobase+VLSI_PIO_IRCFG);
1261 wmb();
1262
1263 outw(MAX_PACKET_LENGTH, iobase+VLSI_PIO_MAXPKT); /* max possible value=0x0fff */
1264
1265 outw(BUS_TO_RINGBASE(idev->busaddr), iobase+VLSI_PIO_RINGBASE);
1266
1267 outw(TX_RX_TO_RINGSIZE(idev->tx_ring->size, idev->rx_ring->size),
1268 iobase+VLSI_PIO_RINGSIZE);
1269
1270 ptr = inw(iobase+VLSI_PIO_RINGPTR);
1271 atomic_set(&idev->rx_ring->head, RINGPTR_GET_RX(ptr));
1272 atomic_set(&idev->rx_ring->tail, RINGPTR_GET_RX(ptr));
1273 atomic_set(&idev->tx_ring->head, RINGPTR_GET_TX(ptr));
1274 atomic_set(&idev->tx_ring->tail, RINGPTR_GET_TX(ptr));
1275
1276 vlsi_set_baud(idev, iobase); /* idev->new_baud used as provided by caller */
1277
1278 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* just in case - w/c pending IRQ's */
1279 wmb();
1280
1281 /* DO NOT BLINDLY ENABLE IRINTR_ACTEN!
1282 * basically every received pulse fires an ACTIVITY-INT
1283 * leading to >>1000 INT's per second instead of few 10
1284 */
1285
1286 outb(IRINTR_RPKTEN|IRINTR_TPKTEN, iobase+VLSI_PIO_IRINTR);
1287
1288 return 0;
1289}
1290
1291static int vlsi_start_hw(vlsi_irda_dev_t *idev)
1292{
1293 struct pci_dev *pdev = idev->pdev;
1294 struct net_device *ndev = pci_get_drvdata(pdev);
1295 unsigned iobase = ndev->base_addr;
1296 u8 byte;
1297
1298 /* we don't use the legacy UART, disable its address decoding */
1299
1300 pci_read_config_byte(pdev, VLSI_PCI_IRMISC, &byte);
1301 byte &= ~(IRMISC_UARTEN | IRMISC_UARTTST);
1302 pci_write_config_byte(pdev, VLSI_PCI_IRMISC, byte);
1303
1304 /* enable PCI busmaster access to our 16MB page */
1305
1306 pci_write_config_byte(pdev, VLSI_PCI_MSTRPAGE, MSTRPAGE_VALUE);
1307 pci_set_master(pdev);
1308
1309 if (vlsi_init_chip(pdev) < 0) {
1310 pci_disable_device(pdev);
1311 return -1;
1312 }
1313
1314 vlsi_fill_rx(idev->rx_ring);
1315
Chunyan Zhang497ec1f2015-01-08 12:01:32 +08001316 idev->last_rx = ktime_get(); /* first mtt may start from now on */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 outw(0, iobase+VLSI_PIO_PROMPT); /* kick hw state machine */
1319
1320 return 0;
1321}
1322
1323static int vlsi_stop_hw(vlsi_irda_dev_t *idev)
1324{
1325 struct pci_dev *pdev = idev->pdev;
1326 struct net_device *ndev = pci_get_drvdata(pdev);
1327 unsigned iobase = ndev->base_addr;
1328 unsigned long flags;
1329
1330 spin_lock_irqsave(&idev->lock,flags);
1331 outw(0, iobase+VLSI_PIO_IRENABLE);
1332 outw(0, iobase+VLSI_PIO_IRCFG); /* disable everything */
1333
1334 /* disable and w/c irqs */
1335 outb(0, iobase+VLSI_PIO_IRINTR);
1336 wmb();
1337 outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR);
1338 spin_unlock_irqrestore(&idev->lock,flags);
1339
1340 vlsi_unarm_tx(idev);
1341 vlsi_unarm_rx(idev);
1342
1343 vlsi_clear_regs(iobase);
1344 vlsi_stop_clock(pdev);
1345
1346 pci_disable_device(pdev);
1347
1348 return 0;
1349}
1350
1351/**************************************************************/
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353static void vlsi_tx_timeout(struct net_device *ndev)
1354{
Wang Chen4cf16532008-11-12 23:38:14 -08001355 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001358 vlsi_reg_debug(ndev->base_addr, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 vlsi_ring_debug(idev->tx_ring);
1360
1361 if (netif_running(ndev))
1362 netif_stop_queue(ndev);
1363
1364 vlsi_stop_hw(idev);
1365
1366 /* now simply restart the whole thing */
1367
1368 if (!idev->new_baud)
1369 idev->new_baud = idev->baud; /* keep current baudrate */
1370
1371 if (vlsi_start_hw(idev))
Joe Perches6c910232014-11-11 13:37:30 -08001372 net_err_ratelimited("%s: failed to restart hw - %s(%s) unusable!\n",
1373 __func__, pci_name(idev->pdev), ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 else
1375 netif_start_queue(ndev);
1376}
1377
1378static int vlsi_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
1379{
Wang Chen4cf16532008-11-12 23:38:14 -08001380 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 struct if_irda_req *irq = (struct if_irda_req *) rq;
1382 unsigned long flags;
1383 u16 fifocnt;
1384 int ret = 0;
1385
1386 switch (cmd) {
1387 case SIOCSBANDWIDTH:
1388 if (!capable(CAP_NET_ADMIN)) {
1389 ret = -EPERM;
1390 break;
1391 }
1392 spin_lock_irqsave(&idev->lock, flags);
1393 idev->new_baud = irq->ifr_baudrate;
1394 /* when called from userland there might be a minor race window here
1395 * if the stack tries to change speed concurrently - which would be
1396 * pretty strange anyway with the userland having full control...
1397 */
1398 vlsi_set_baud(idev, ndev->base_addr);
1399 spin_unlock_irqrestore(&idev->lock, flags);
1400 break;
1401 case SIOCSMEDIABUSY:
1402 if (!capable(CAP_NET_ADMIN)) {
1403 ret = -EPERM;
1404 break;
1405 }
1406 irda_device_set_media_busy(ndev, TRUE);
1407 break;
1408 case SIOCGRECEIVING:
1409 /* the best we can do: check whether there are any bytes in rx fifo.
1410 * The trustable window (in case some data arrives just afterwards)
1411 * may be as short as 1usec or so at 4Mbps.
1412 */
1413 fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
1414 irq->ifr_receiving = (fifocnt!=0) ? 1 : 0;
1415 break;
1416 default:
Joe Perches6c910232014-11-11 13:37:30 -08001417 net_warn_ratelimited("%s: notsupp - cmd=%04x\n",
1418 __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ret = -EOPNOTSUPP;
1420 }
1421
1422 return ret;
1423}
1424
1425/********************************************************/
1426
David Howells7d12e782006-10-05 14:55:46 +01001427static irqreturn_t vlsi_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct net_device *ndev = dev_instance;
Wang Chen4cf16532008-11-12 23:38:14 -08001430 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 unsigned iobase;
1432 u8 irintr;
1433 int boguscount = 5;
1434 unsigned long flags;
1435 int handled = 0;
1436
1437 iobase = ndev->base_addr;
1438 spin_lock_irqsave(&idev->lock,flags);
1439 do {
1440 irintr = inb(iobase+VLSI_PIO_IRINTR);
1441 mb();
1442 outb(irintr, iobase+VLSI_PIO_IRINTR); /* acknowledge asap */
1443
1444 if (!(irintr&=IRINTR_INT_MASK)) /* not our INT - probably shared */
1445 break;
1446
1447 handled = 1;
1448
1449 if (unlikely(!(irintr & ~IRINTR_ACTIVITY)))
1450 break; /* nothing todo if only activity */
1451
1452 if (irintr&IRINTR_RPKTINT)
1453 vlsi_rx_interrupt(ndev);
1454
1455 if (irintr&IRINTR_TPKTINT)
1456 vlsi_tx_interrupt(ndev);
1457
1458 } while (--boguscount > 0);
1459 spin_unlock_irqrestore(&idev->lock,flags);
1460
1461 if (boguscount <= 0)
Joe Perches6c910232014-11-11 13:37:30 -08001462 net_info_ratelimited("%s: too much work in interrupt!\n",
1463 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return IRQ_RETVAL(handled);
1465}
1466
1467/********************************************************/
1468
1469static int vlsi_open(struct net_device *ndev)
1470{
Wang Chen4cf16532008-11-12 23:38:14 -08001471 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int err = -EAGAIN;
1473 char hwname[32];
1474
1475 if (pci_request_regions(idev->pdev, drivername)) {
Joe Perches6c910232014-11-11 13:37:30 -08001476 net_warn_ratelimited("%s: io resource busy\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto errout;
1478 }
1479 ndev->base_addr = pci_resource_start(idev->pdev,0);
1480 ndev->irq = idev->pdev->irq;
1481
1482 /* under some rare occasions the chip apparently comes up with
1483 * IRQ's pending. We better w/c pending IRQ and disable them all
1484 */
1485
1486 outb(IRINTR_INT_MASK, ndev->base_addr+VLSI_PIO_IRINTR);
1487
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001488 if (request_irq(ndev->irq, vlsi_interrupt, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 drivername, ndev)) {
Joe Perches6c910232014-11-11 13:37:30 -08001490 net_warn_ratelimited("%s: couldn't get IRQ: %d\n",
1491 __func__, ndev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 goto errout_io;
1493 }
1494
1495 if ((err = vlsi_create_hwif(idev)) != 0)
1496 goto errout_irq;
1497
1498 sprintf(hwname, "VLSI-FIR @ 0x%04x", (unsigned)ndev->base_addr);
1499 idev->irlap = irlap_open(ndev,&idev->qos,hwname);
1500 if (!idev->irlap)
1501 goto errout_free_ring;
1502
Chunyan Zhang497ec1f2015-01-08 12:01:32 +08001503 idev->last_rx = ktime_get(); /* first mtt may start from now on */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 idev->new_baud = 9600; /* start with IrPHY using 9600(SIR) mode */
1506
1507 if ((err = vlsi_start_hw(idev)) != 0)
1508 goto errout_close_irlap;
1509
1510 netif_start_queue(ndev);
1511
Joe Perches6c910232014-11-11 13:37:30 -08001512 net_info_ratelimited("%s: device %s operational\n",
1513 __func__, ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 return 0;
1516
1517errout_close_irlap:
1518 irlap_close(idev->irlap);
1519errout_free_ring:
1520 vlsi_destroy_hwif(idev);
1521errout_irq:
1522 free_irq(ndev->irq,ndev);
1523errout_io:
1524 pci_release_regions(idev->pdev);
1525errout:
1526 return err;
1527}
1528
1529static int vlsi_close(struct net_device *ndev)
1530{
Wang Chen4cf16532008-11-12 23:38:14 -08001531 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 netif_stop_queue(ndev);
1534
1535 if (idev->irlap)
1536 irlap_close(idev->irlap);
1537 idev->irlap = NULL;
1538
1539 vlsi_stop_hw(idev);
1540
1541 vlsi_destroy_hwif(idev);
1542
1543 free_irq(ndev->irq,ndev);
1544
1545 pci_release_regions(idev->pdev);
1546
Joe Perches6c910232014-11-11 13:37:30 -08001547 net_info_ratelimited("%s: device %s stopped\n", __func__, ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 return 0;
1550}
1551
Stephen Hemminger30a5d7f2009-03-20 19:35:41 +00001552static const struct net_device_ops vlsi_netdev_ops = {
1553 .ndo_open = vlsi_open,
1554 .ndo_stop = vlsi_close,
1555 .ndo_start_xmit = vlsi_hard_start_xmit,
1556 .ndo_do_ioctl = vlsi_ioctl,
1557 .ndo_tx_timeout = vlsi_tx_timeout,
1558};
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560static int vlsi_irda_init(struct net_device *ndev)
1561{
Wang Chen4cf16532008-11-12 23:38:14 -08001562 vlsi_irda_dev_t *idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct pci_dev *pdev = idev->pdev;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 ndev->irq = pdev->irq;
1566 ndev->base_addr = pci_resource_start(pdev,0);
1567
1568 /* PCI busmastering
1569 * see include file for details why we need these 2 masks, in this order!
1570 */
1571
Joe Perches8e95a202009-12-03 07:58:21 +00001572 if (pci_set_dma_mask(pdev,DMA_MASK_USED_BY_HW) ||
1573 pci_set_dma_mask(pdev,DMA_MASK_MSTRPAGE)) {
Joe Perches6c910232014-11-11 13:37:30 -08001574 net_err_ratelimited("%s: aborting due to PCI BM-DMA address limitations\n",
1575 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return -1;
1577 }
1578
1579 irda_init_max_qos_capabilies(&idev->qos);
1580
1581 /* the VLSI82C147 does not support 576000! */
1582
1583 idev->qos.baud_rate.bits = IR_2400 | IR_9600
1584 | IR_19200 | IR_38400 | IR_57600 | IR_115200
1585 | IR_1152000 | (IR_4000000 << 8);
1586
1587 idev->qos.min_turn_time.bits = qos_mtt_bits;
1588
1589 irda_qos_bits_to_value(&idev->qos);
1590
1591 /* currently no public media definitions for IrDA */
1592
1593 ndev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
1594 ndev->if_port = IF_PORT_UNKNOWN;
1595
Stephen Hemminger30a5d7f2009-03-20 19:35:41 +00001596 ndev->netdev_ops = &vlsi_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 ndev->watchdog_timeo = 500*HZ/1000; /* max. allowed turn time for IrLAP */
1598
1599 SET_NETDEV_DEV(ndev, &pdev->dev);
1600
1601 return 0;
1602}
1603
1604/**************************************************************/
1605
Bill Pemberton45ac9362012-12-03 09:24:13 -05001606static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607vlsi_irda_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1608{
1609 struct net_device *ndev;
1610 vlsi_irda_dev_t *idev;
1611
1612 if (pci_enable_device(pdev))
1613 goto out;
1614 else
1615 pdev->current_state = 0; /* hw must be running now */
1616
Joe Perches6c910232014-11-11 13:37:30 -08001617 net_info_ratelimited("%s: IrDA PCI controller %s detected\n",
1618 drivername, pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Joe Perches8e95a202009-12-03 07:58:21 +00001620 if ( !pci_resource_start(pdev,0) ||
1621 !(pci_resource_flags(pdev,0) & IORESOURCE_IO) ) {
Joe Perches6c910232014-11-11 13:37:30 -08001622 net_err_ratelimited("%s: bar 0 invalid", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto out_disable;
1624 }
1625
1626 ndev = alloc_irdadev(sizeof(*idev));
1627 if (ndev==NULL) {
Joe Perches6c910232014-11-11 13:37:30 -08001628 net_err_ratelimited("%s: Unable to allocate device memory.\n",
1629 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto out_disable;
1631 }
1632
Wang Chen4cf16532008-11-12 23:38:14 -08001633 idev = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 spin_lock_init(&idev->lock);
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001636 mutex_init(&idev->mtx);
1637 mutex_lock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 idev->pdev = pdev;
1639
1640 if (vlsi_irda_init(ndev) < 0)
1641 goto out_freedev;
1642
1643 if (register_netdev(ndev) < 0) {
Joe Perches6c910232014-11-11 13:37:30 -08001644 net_err_ratelimited("%s: register_netdev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto out_freedev;
1646 }
1647
1648 if (vlsi_proc_root != NULL) {
1649 struct proc_dir_entry *ent;
1650
Denis V. Luneva95609c2008-04-29 01:02:29 -07001651 ent = proc_create_data(ndev->name, S_IFREG|S_IRUGO,
1652 vlsi_proc_root, VLSI_PROC_FOPS, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (!ent) {
Joe Perches6c910232014-11-11 13:37:30 -08001654 net_warn_ratelimited("%s: failed to create proc entry\n",
1655 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 } else {
David Howells271a15e2013-04-12 00:38:51 +01001657 proc_set_size(ent, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 idev->proc_entry = ent;
1660 }
Joe Perches6c910232014-11-11 13:37:30 -08001661 net_info_ratelimited("%s: registered device %s\n",
1662 drivername, ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 pci_set_drvdata(pdev, ndev);
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001665 mutex_unlock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 return 0;
1668
1669out_freedev:
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001670 mutex_unlock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 free_netdev(ndev);
1672out_disable:
1673 pci_disable_device(pdev);
1674out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return -ENODEV;
1676}
1677
Bill Pemberton45ac9362012-12-03 09:24:13 -05001678static void vlsi_irda_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 struct net_device *ndev = pci_get_drvdata(pdev);
1681 vlsi_irda_dev_t *idev;
1682
1683 if (!ndev) {
Joe Perches6c910232014-11-11 13:37:30 -08001684 net_err_ratelimited("%s: lost netdevice?\n", drivername);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return;
1686 }
1687
1688 unregister_netdev(ndev);
1689
Wang Chen4cf16532008-11-12 23:38:14 -08001690 idev = netdev_priv(ndev);
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001691 mutex_lock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (idev->proc_entry) {
1693 remove_proc_entry(ndev->name, vlsi_proc_root);
1694 idev->proc_entry = NULL;
1695 }
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001696 mutex_unlock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 free_netdev(ndev);
1699
Joe Perches6c910232014-11-11 13:37:30 -08001700 net_info_ratelimited("%s: %s removed\n", drivername, pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703#ifdef CONFIG_PM
1704
1705/* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
1706 * Some of the Linux PCI-PM code however depends on this, for example in
1707 * pci_set_power_state(). So we have to take care to perform the required
1708 * operations on our own (particularly reflecting the pdev->current_state)
1709 * otherwise we might get cheated by pci-pm.
1710 */
1711
1712
Pavel Machek05adc3b2005-04-16 15:25:25 -07001713static int vlsi_irda_suspend(struct pci_dev *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct net_device *ndev = pci_get_drvdata(pdev);
1716 vlsi_irda_dev_t *idev;
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (!ndev) {
Joe Perches6c910232014-11-11 13:37:30 -08001719 net_err_ratelimited("%s - %s: no netdevice\n",
1720 __func__, pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 0;
1722 }
Wang Chen4cf16532008-11-12 23:38:14 -08001723 idev = netdev_priv(ndev);
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001724 mutex_lock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (pdev->current_state != 0) { /* already suspended */
Pavel Machekca078ba2005-09-03 15:56:57 -07001726 if (state.event > pdev->current_state) { /* simply go deeper */
1727 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1728 pdev->current_state = state.event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730 else
Joe Perches6c910232014-11-11 13:37:30 -08001731 net_err_ratelimited("%s - %s: invalid suspend request %u -> %u\n",
1732 __func__, pci_name(pdev),
1733 pdev->current_state, state.event);
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001734 mutex_unlock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return 0;
1736 }
1737
1738 if (netif_running(ndev)) {
1739 netif_device_detach(ndev);
1740 vlsi_stop_hw(idev);
1741 pci_save_state(pdev);
1742 if (!idev->new_baud)
1743 /* remember speed settings to restore on resume */
1744 idev->new_baud = idev->baud;
1745 }
1746
Pavel Machek829ca9a2005-09-03 15:56:56 -07001747 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Pavel Machekca078ba2005-09-03 15:56:57 -07001748 pdev->current_state = state.event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 idev->resume_ok = 1;
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001750 mutex_unlock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752}
1753
1754static int vlsi_irda_resume(struct pci_dev *pdev)
1755{
1756 struct net_device *ndev = pci_get_drvdata(pdev);
1757 vlsi_irda_dev_t *idev;
1758
1759 if (!ndev) {
Joe Perches6c910232014-11-11 13:37:30 -08001760 net_err_ratelimited("%s - %s: no netdevice\n",
1761 __func__, pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763 }
Wang Chen4cf16532008-11-12 23:38:14 -08001764 idev = netdev_priv(ndev);
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001765 mutex_lock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (pdev->current_state == 0) {
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001767 mutex_unlock(&idev->mtx);
Joe Perches6c910232014-11-11 13:37:30 -08001768 net_warn_ratelimited("%s - %s: already resumed\n",
1769 __func__, pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return 0;
1771 }
1772
Pavel Machek829ca9a2005-09-03 15:56:56 -07001773 pci_set_power_state(pdev, PCI_D0);
1774 pdev->current_state = PM_EVENT_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 if (!idev->resume_ok) {
1777 /* should be obsolete now - but used to happen due to:
1778 * - pci layer initially setting pdev->current_state = 4 (unknown)
1779 * - pci layer did not walk the save_state-tree (might be APM problem)
1780 * so we could not refuse to suspend from undefined state
1781 * - vlsi_irda_suspend detected invalid state and refused to save
1782 * configuration for resume - but was too late to stop suspending
1783 * - vlsi_irda_resume got screwed when trying to resume from garbage
1784 *
1785 * now we explicitly set pdev->current_state = 0 after enabling the
1786 * device and independently resume_ok should catch any garbage config.
1787 */
Joe Perches6c910232014-11-11 13:37:30 -08001788 net_warn_ratelimited("%s - hm, nothing to resume?\n", __func__);
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001789 mutex_unlock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791 }
1792
1793 if (netif_running(ndev)) {
1794 pci_restore_state(pdev);
1795 vlsi_start_hw(idev);
1796 netif_device_attach(ndev);
1797 }
1798 idev->resume_ok = 0;
Matthias Kaehlckeaa429112007-07-09 13:18:12 -07001799 mutex_unlock(&idev->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return 0;
1801}
1802
1803#endif /* CONFIG_PM */
1804
1805/*********************************************************/
1806
1807static struct pci_driver vlsi_irda_driver = {
1808 .name = drivername,
1809 .id_table = vlsi_irda_table,
1810 .probe = vlsi_irda_probe,
Bill Pemberton45ac9362012-12-03 09:24:13 -05001811 .remove = vlsi_irda_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812#ifdef CONFIG_PM
1813 .suspend = vlsi_irda_suspend,
1814 .resume = vlsi_irda_resume,
1815#endif
1816};
1817
1818#define PROC_DIR ("driver/" DRIVER_NAME)
1819
1820static int __init vlsi_mod_init(void)
1821{
1822 int i, ret;
1823
1824 if (clksrc < 0 || clksrc > 3) {
Joe Perches6c910232014-11-11 13:37:30 -08001825 net_err_ratelimited("%s: invalid clksrc=%d\n",
1826 drivername, clksrc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return -1;
1828 }
1829
1830 for (i = 0; i < 2; i++) {
1831 switch(ringsize[i]) {
1832 case 4:
1833 case 8:
1834 case 16:
1835 case 32:
1836 case 64:
1837 break;
1838 default:
Joe Perches6c910232014-11-11 13:37:30 -08001839 net_warn_ratelimited("%s: invalid %s ringsize %d, using default=8\n",
1840 drivername,
1841 i ? "rx" : "tx",
1842 ringsize[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 ringsize[i] = 8;
1844 break;
1845 }
1846 }
1847
1848 sirpulse = !!sirpulse;
1849
Al Viro66600222005-09-28 22:32:57 +01001850 /* proc_mkdir returns NULL if !CONFIG_PROC_FS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 * Failure to create the procfs entry is handled like running
1852 * without procfs - it's not required for the driver to work.
1853 */
Al Viro66600222005-09-28 22:32:57 +01001854 vlsi_proc_root = proc_mkdir(PROC_DIR, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Christophe Lucasa85d7712006-03-20 19:00:27 -08001856 ret = pci_register_driver(&vlsi_irda_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 if (ret && vlsi_proc_root)
1859 remove_proc_entry(PROC_DIR, NULL);
1860 return ret;
1861
1862}
1863
1864static void __exit vlsi_mod_exit(void)
1865{
1866 pci_unregister_driver(&vlsi_irda_driver);
1867 if (vlsi_proc_root)
1868 remove_proc_entry(PROC_DIR, NULL);
1869}
1870
1871module_init(vlsi_mod_init);
1872module_exit(vlsi_mod_exit);