xref: /linux/drivers/isdn/hardware/mISDN/speedfax.c (revision 84abd88a70090cf00f9e45c3a81680874f17626e)
1 /*
2  * speedfax.c	low level stuff for Sedlbauer Speedfax+ cards
3  *		based on the ISAR DSP
4  *		Thanks to Sedlbauer AG for informations and HW
5  *
6  * Author       Karsten Keil <keil@isdn4linux.de>
7  *
8  * Copyright 2009  by Karsten Keil <keil@isdn4linux.de>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24 
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/delay.h>
28 #include <linux/mISDNhw.h>
29 #include <linux/firmware.h>
30 #include "ipac.h"
31 #include "isar.h"
32 
33 #define SPEEDFAX_REV	"2.0"
34 
35 #define PCI_SUBVENDOR_SPEEDFAX_PYRAMID	0x51
36 #define PCI_SUBVENDOR_SPEEDFAX_PCI	0x54
37 #define PCI_SUB_ID_SEDLBAUER		0x01
38 
39 #define SFAX_PCI_ADDR		0xc8
40 #define SFAX_PCI_ISAC		0xd0
41 #define SFAX_PCI_ISAR		0xe0
42 
43 /* TIGER 100 Registers */
44 
45 #define TIGER_RESET_ADDR	0x00
46 #define TIGER_EXTERN_RESET_ON	0x01
47 #define TIGER_EXTERN_RESET_OFF	0x00
48 #define TIGER_AUX_CTRL		0x02
49 #define TIGER_AUX_DATA		0x03
50 #define TIGER_AUX_IRQMASK	0x05
51 #define TIGER_AUX_STATUS	0x07
52 
53 /* Tiger AUX BITs */
54 #define SFAX_AUX_IOMASK		0xdd	/* 1 and 5 are inputs */
55 #define SFAX_ISAR_RESET_BIT_OFF 0x00
56 #define SFAX_ISAR_RESET_BIT_ON	0x01
57 #define SFAX_TIGER_IRQ_BIT	0x02
58 #define SFAX_LED1_BIT		0x08
59 #define SFAX_LED2_BIT		0x10
60 
61 #define SFAX_PCI_RESET_ON	(SFAX_ISAR_RESET_BIT_ON)
62 #define SFAX_PCI_RESET_OFF	(SFAX_LED1_BIT | SFAX_LED2_BIT)
63 
64 static int sfax_cnt;
65 static u32 debug;
66 static u32 irqloops = 4;
67 
68 struct sfax_hw {
69 	struct list_head	list;
70 	struct pci_dev		*pdev;
71 	char			name[MISDN_MAX_IDLEN];
72 	u32			irq;
73 	u32			irqcnt;
74 	u32			cfg;
75 	struct _ioport		p_isac;
76 	struct _ioport		p_isar;
77 	u8			aux_data;
78 	spinlock_t		lock;	/* HW access lock */
79 	struct isac_hw		isac;
80 	struct isar_hw		isar;
81 };
82 
83 static LIST_HEAD(Cards);
84 static DEFINE_RWLOCK(card_lock); /* protect Cards */
85 
86 static void
87 _set_debug(struct sfax_hw *card)
88 {
89 	card->isac.dch.debug = debug;
90 	card->isar.ch[0].bch.debug = debug;
91 	card->isar.ch[1].bch.debug = debug;
92 }
93 
94 static int
95 set_debug(const char *val, struct kernel_param *kp)
96 {
97 	int ret;
98 	struct sfax_hw *card;
99 
100 	ret = param_set_uint(val, kp);
101 	if (!ret) {
102 		read_lock(&card_lock);
103 		list_for_each_entry(card, &Cards, list)
104 			_set_debug(card);
105 		read_unlock(&card_lock);
106 	}
107 	return ret;
108 }
109 
110 MODULE_AUTHOR("Karsten Keil");
111 MODULE_LICENSE("GPL v2");
112 MODULE_VERSION(SPEEDFAX_REV);
113 MODULE_FIRMWARE("isdn/ISAR.BIN");
114 module_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR);
115 MODULE_PARM_DESC(debug, "Speedfax debug mask");
116 module_param(irqloops, uint, S_IRUGO | S_IWUSR);
117 MODULE_PARM_DESC(irqloops, "Speedfax maximal irqloops (default 4)");
118 
119 IOFUNC_IND(ISAC, sfax_hw, p_isac)
120 IOFUNC_IND(ISAR, sfax_hw, p_isar)
121 
122 static irqreturn_t
123 speedfax_irq(int intno, void *dev_id)
124 {
125 	struct sfax_hw	*sf = dev_id;
126 	u8 val;
127 	int cnt = irqloops;
128 
129 	spin_lock(&sf->lock);
130 	val = inb(sf->cfg + TIGER_AUX_STATUS);
131 	if (val & SFAX_TIGER_IRQ_BIT) { /* for us or shared ? */
132 		spin_unlock(&sf->lock);
133 		return IRQ_NONE; /* shared */
134 	}
135 	sf->irqcnt++;
136 	val = ReadISAR_IND(sf, ISAR_IRQBIT);
137 Start_ISAR:
138 	if (val & ISAR_IRQSTA)
139 		mISDNisar_irq(&sf->isar);
140 	val = ReadISAC_IND(sf, ISAC_ISTA);
141 	if (val)
142 		mISDNisac_irq(&sf->isac, val);
143 	val = ReadISAR_IND(sf, ISAR_IRQBIT);
144 	if ((val & ISAR_IRQSTA) && cnt--)
145 		goto Start_ISAR;
146 	if (cnt < irqloops)
147 		pr_debug("%s: %d irqloops cpu%d\n", sf->name,
148 			irqloops - cnt, smp_processor_id());
149 	if (irqloops && !cnt)
150 		pr_notice("%s: %d IRQ LOOP cpu%d\n", sf->name,
151 			irqloops, smp_processor_id());
152 	spin_unlock(&sf->lock);
153 	return IRQ_HANDLED;
154 }
155 
156 static void
157 enable_hwirq(struct sfax_hw *sf)
158 {
159 	WriteISAC_IND(sf, ISAC_MASK, 0);
160 	WriteISAR_IND(sf, ISAR_IRQBIT, ISAR_IRQMSK);
161 	outb(SFAX_TIGER_IRQ_BIT, sf->cfg + TIGER_AUX_IRQMASK);
162 }
163 
164 static void
165 disable_hwirq(struct sfax_hw *sf)
166 {
167 	WriteISAC_IND(sf, ISAC_MASK, 0xFF);
168 	WriteISAR_IND(sf, ISAR_IRQBIT, 0);
169 	outb(0, sf->cfg + TIGER_AUX_IRQMASK);
170 }
171 
172 static void
173 reset_speedfax(struct sfax_hw *sf)
174 {
175 
176 	pr_debug("%s: resetting card\n", sf->name);
177 	outb(TIGER_EXTERN_RESET_ON, sf->cfg + TIGER_RESET_ADDR);
178 	outb(SFAX_PCI_RESET_ON, sf->cfg + TIGER_AUX_DATA);
179 	mdelay(1);
180 	outb(TIGER_EXTERN_RESET_OFF, sf->cfg + TIGER_RESET_ADDR);
181 	sf->aux_data = SFAX_PCI_RESET_OFF;
182 	outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
183 	mdelay(1);
184 }
185 
186 static int
187 sfax_ctrl(struct sfax_hw  *sf, u32 cmd, u_long arg)
188 {
189 	int ret = 0;
190 
191 	switch (cmd) {
192 	case HW_RESET_REQ:
193 		reset_speedfax(sf);
194 		break;
195 	case HW_ACTIVATE_IND:
196 		if (arg & 1)
197 			sf->aux_data &= ~SFAX_LED1_BIT;
198 		if (arg & 2)
199 			sf->aux_data &= ~SFAX_LED2_BIT;
200 		outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
201 		break;
202 	case HW_DEACT_IND:
203 		if (arg & 1)
204 			sf->aux_data |= SFAX_LED1_BIT;
205 		if (arg & 2)
206 			sf->aux_data |= SFAX_LED2_BIT;
207 		outb(sf->aux_data, sf->cfg + TIGER_AUX_DATA);
208 		break;
209 	default:
210 		pr_info("%s: %s unknown command %x %lx\n",
211 			sf->name, __func__, cmd, arg);
212 		ret = -EINVAL;
213 		break;
214 	}
215 	return ret;
216 }
217 
218 static int
219 channel_ctrl(struct sfax_hw  *sf, struct mISDN_ctrl_req *cq)
220 {
221 	int	ret = 0;
222 
223 	switch (cq->op) {
224 	case MISDN_CTRL_GETOP:
225 		cq->op = MISDN_CTRL_LOOP;
226 		break;
227 	case MISDN_CTRL_LOOP:
228 		/* cq->channel: 0 disable, 1 B1 loop 2 B2 loop, 3 both */
229 		if (cq->channel < 0 || cq->channel > 3) {
230 			ret = -EINVAL;
231 			break;
232 		}
233 		ret = sf->isac.ctrl(&sf->isac, HW_TESTLOOP, cq->channel);
234 		break;
235 	default:
236 		pr_info("%s: unknown Op %x\n", sf->name, cq->op);
237 		ret = -EINVAL;
238 		break;
239 	}
240 	return ret;
241 }
242 
243 static int
244 sfax_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
245 {
246 	struct mISDNdevice	*dev = container_of(ch, struct mISDNdevice, D);
247 	struct dchannel		*dch = container_of(dev, struct dchannel, dev);
248 	struct sfax_hw		*sf = dch->hw;
249 	struct channel_req	*rq;
250 	int			err = 0;
251 
252 	pr_debug("%s: cmd:%x %p\n", sf->name, cmd, arg);
253 	switch (cmd) {
254 	case OPEN_CHANNEL:
255 		rq = arg;
256 		if (rq->protocol == ISDN_P_TE_S0)
257 			err = sf->isac.open(&sf->isac, rq);
258 		else
259 			err = sf->isar.open(&sf->isar, rq);
260 		if (err)
261 			break;
262 		if (!try_module_get(THIS_MODULE))
263 			pr_info("%s: cannot get module\n", sf->name);
264 		break;
265 	case CLOSE_CHANNEL:
266 		pr_debug("%s: dev(%d) close from %p\n", sf->name,
267 			dch->dev.id, __builtin_return_address(0));
268 		module_put(THIS_MODULE);
269 		break;
270 	case CONTROL_CHANNEL:
271 		err = channel_ctrl(sf, arg);
272 		break;
273 	default:
274 		pr_debug("%s: unknown command %x\n", sf->name, cmd);
275 		return -EINVAL;
276 	}
277 	return err;
278 }
279 
280 static int __devinit
281 init_card(struct sfax_hw *sf)
282 {
283 	int	ret, cnt = 3;
284 	u_long	flags;
285 
286 	ret = request_irq(sf->irq, speedfax_irq, IRQF_SHARED, sf->name, sf);
287 	if (ret) {
288 		pr_info("%s: couldn't get interrupt %d\n", sf->name, sf->irq);
289 		return ret;
290 	}
291 	while (cnt--) {
292 		spin_lock_irqsave(&sf->lock, flags);
293 		ret = sf->isac.init(&sf->isac);
294 		if (ret) {
295 			spin_unlock_irqrestore(&sf->lock, flags);
296 			pr_info("%s: ISAC init failed with %d\n",
297 				sf->name, ret);
298 			break;
299 		}
300 		enable_hwirq(sf);
301 		/* RESET Receiver and Transmitter */
302 		WriteISAC_IND(sf, ISAC_CMDR, 0x41);
303 		spin_unlock_irqrestore(&sf->lock, flags);
304 		msleep_interruptible(10);
305 		if (debug & DEBUG_HW)
306 			pr_notice("%s: IRQ %d count %d\n", sf->name,
307 				sf->irq, sf->irqcnt);
308 		if (!sf->irqcnt) {
309 			pr_info("%s: IRQ(%d) got no requests during init %d\n",
310 			       sf->name, sf->irq, 3 - cnt);
311 		} else
312 			return 0;
313 	}
314 	free_irq(sf->irq, sf);
315 	return -EIO;
316 }
317 
318 
319 static int __devinit
320 setup_speedfax(struct sfax_hw *sf)
321 {
322 	u_long flags;
323 
324 	if (!request_region(sf->cfg, 256, sf->name)) {
325 		pr_info("mISDN: %s config port %x-%x already in use\n",
326 		       sf->name, sf->cfg, sf->cfg + 255);
327 		return -EIO;
328 	}
329 	outb(0xff, sf->cfg);
330 	outb(0, sf->cfg);
331 	outb(0xdd, sf->cfg + TIGER_AUX_CTRL);
332 	outb(0, sf->cfg + TIGER_AUX_IRQMASK);
333 
334 	sf->isac.type = IPAC_TYPE_ISAC;
335 	sf->p_isac.ale = sf->cfg + SFAX_PCI_ADDR;
336 	sf->p_isac.port = sf->cfg + SFAX_PCI_ISAC;
337 	sf->p_isar.ale = sf->cfg + SFAX_PCI_ADDR;
338 	sf->p_isar.port = sf->cfg + SFAX_PCI_ISAR;
339 	ASSIGN_FUNC(IND, ISAC, sf->isac);
340 	ASSIGN_FUNC(IND, ISAR, sf->isar);
341 	spin_lock_irqsave(&sf->lock, flags);
342 	reset_speedfax(sf);
343 	disable_hwirq(sf);
344 	spin_unlock_irqrestore(&sf->lock, flags);
345 	return 0;
346 }
347 
348 static void
349 release_card(struct sfax_hw *card) {
350 	u_long	flags;
351 
352 	spin_lock_irqsave(&card->lock, flags);
353 	disable_hwirq(card);
354 	spin_unlock_irqrestore(&card->lock, flags);
355 	card->isac.release(&card->isac);
356 	free_irq(card->irq, card);
357 	card->isar.release(&card->isar);
358 	mISDN_unregister_device(&card->isac.dch.dev);
359 	release_region(card->cfg, 256);
360 	pci_disable_device(card->pdev);
361 	pci_set_drvdata(card->pdev, NULL);
362 	write_lock_irqsave(&card_lock, flags);
363 	list_del(&card->list);
364 	write_unlock_irqrestore(&card_lock, flags);
365 	kfree(card);
366 	sfax_cnt--;
367 }
368 
369 static int __devinit
370 setup_instance(struct sfax_hw *card)
371 {
372 	const struct firmware *firmware;
373 	int i, err;
374 	u_long flags;
375 
376 	snprintf(card->name, MISDN_MAX_IDLEN - 1, "Speedfax.%d", sfax_cnt + 1);
377 	write_lock_irqsave(&card_lock, flags);
378 	list_add_tail(&card->list, &Cards);
379 	write_unlock_irqrestore(&card_lock, flags);
380 	_set_debug(card);
381 	spin_lock_init(&card->lock);
382 	card->isac.hwlock = &card->lock;
383 	card->isar.hwlock = &card->lock;
384 	card->isar.ctrl = (void *)&sfax_ctrl;
385 	card->isac.name = card->name;
386 	card->isar.name = card->name;
387 	card->isar.owner = THIS_MODULE;
388 
389 	err = request_firmware(&firmware, "isdn/ISAR.BIN", &card->pdev->dev);
390 	if (err < 0) {
391 		pr_info("%s: firmware request failed %d\n",
392 			card->name, err);
393 		goto error_fw;
394 	}
395 	if (debug & DEBUG_HW)
396 		pr_notice("%s: got firmware %zu bytes\n",
397 			card->name, firmware->size);
398 
399 	mISDNisac_init(&card->isac, card);
400 
401 	card->isac.dch.dev.D.ctrl = sfax_dctrl;
402 	card->isac.dch.dev.Bprotocols =
403 		mISDNisar_init(&card->isar, card);
404 	for (i = 0; i < 2; i++) {
405 		set_channelmap(i + 1, card->isac.dch.dev.channelmap);
406 		list_add(&card->isar.ch[i].bch.ch.list,
407 			&card->isac.dch.dev.bchannels);
408 	}
409 
410 	err = setup_speedfax(card);
411 	if (err)
412 		goto error_setup;
413 	err = card->isar.init(&card->isar);
414 	if (err)
415 		goto error;
416 	err = mISDN_register_device(&card->isac.dch.dev,
417 		&card->pdev->dev, card->name);
418 	if (err)
419 		goto error;
420 	err = init_card(card);
421 	if (err)
422 		goto error_init;
423 	err = card->isar.firmware(&card->isar, firmware->data, firmware->size);
424 	if (!err)  {
425 		release_firmware(firmware);
426 		sfax_cnt++;
427 		pr_notice("SpeedFax %d cards installed\n", sfax_cnt);
428 		return 0;
429 	}
430 	disable_hwirq(card);
431 	free_irq(card->irq, card);
432 error_init:
433 	mISDN_unregister_device(&card->isac.dch.dev);
434 error:
435 	release_region(card->cfg, 256);
436 error_setup:
437 	card->isac.release(&card->isac);
438 	card->isar.release(&card->isar);
439 	release_firmware(firmware);
440 error_fw:
441 	pci_disable_device(card->pdev);
442 	write_lock_irqsave(&card_lock, flags);
443 	list_del(&card->list);
444 	write_unlock_irqrestore(&card_lock, flags);
445 	kfree(card);
446 	return err;
447 }
448 
449 static int __devinit
450 sfaxpci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
451 {
452 	int err = -ENOMEM;
453 	struct sfax_hw *card = kzalloc(sizeof(struct sfax_hw), GFP_KERNEL);
454 
455 	if (!card) {
456 		pr_info("No memory for Speedfax+ PCI\n");
457 		return err;
458 	}
459 	card->pdev = pdev;
460 	err = pci_enable_device(pdev);
461 	if (err) {
462 		kfree(card);
463 		return err;
464 	}
465 
466 	pr_notice("mISDN: Speedfax found adapter %s at %s\n",
467 		(char *)ent->driver_data, pci_name(pdev));
468 
469 	card->cfg = pci_resource_start(pdev, 0);
470 	card->irq = pdev->irq;
471 	pci_set_drvdata(pdev, card);
472 	err = setup_instance(card);
473 	if (err)
474 		pci_set_drvdata(pdev, NULL);
475 	return err;
476 }
477 
478 static void __devexit
479 sfax_remove_pci(struct pci_dev *pdev)
480 {
481 	struct sfax_hw	*card = pci_get_drvdata(pdev);
482 
483 	if (card)
484 		release_card(card);
485 	else
486 		pr_debug("%s: drvdata allready removed\n", __func__);
487 }
488 
489 static struct pci_device_id sfaxpci_ids[] __devinitdata = {
490 	{ PCI_VENDOR_ID_TIGERJET, PCI_DEVICE_ID_TIGERJET_100,
491 	  PCI_SUBVENDOR_SPEEDFAX_PYRAMID, PCI_SUB_ID_SEDLBAUER,
492 	  0, 0, (unsigned long) "Pyramid Speedfax + PCI"
493 	},
494 	{ PCI_VENDOR_ID_TIGERJET, PCI_DEVICE_ID_TIGERJET_100,
495 	  PCI_SUBVENDOR_SPEEDFAX_PCI, PCI_SUB_ID_SEDLBAUER,
496 	  0, 0, (unsigned long) "Sedlbauer Speedfax + PCI"
497 	},
498 	{ }
499 };
500 MODULE_DEVICE_TABLE(pci, sfaxpci_ids);
501 
502 static struct pci_driver sfaxpci_driver = {
503 	.name = "speedfax+ pci",
504 	.probe = sfaxpci_probe,
505 	.remove = __devexit_p(sfax_remove_pci),
506 	.id_table = sfaxpci_ids,
507 };
508 
509 static int __init
510 Speedfax_init(void)
511 {
512 	int err;
513 
514 	pr_notice("Sedlbauer Speedfax+ Driver Rev. %s\n",
515 		SPEEDFAX_REV);
516 	err = pci_register_driver(&sfaxpci_driver);
517 	return err;
518 }
519 
520 static void __exit
521 Speedfax_cleanup(void)
522 {
523 	pci_unregister_driver(&sfaxpci_driver);
524 }
525 
526 module_init(Speedfax_init);
527 module_exit(Speedfax_cleanup);
528