1/* $Id: t1pci.c,v 1.1.2.2 2004/01/16 21:09:27 keil Exp $
2 *
3 * Module for AVM T1 PCI-card.
4 *
5 * Copyright 1999 by Carsten Paeth <calle@calle.de>
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/delay.h>
16#include <linux/mm.h>
17#include <linux/interrupt.h>
18#include <linux/ioport.h>
19#include <linux/pci.h>
20#include <linux/capi.h>
21#include <linux/init.h>
22#include <asm/io.h>
23#include <linux/isdn/capicmd.h>
24#include <linux/isdn/capiutil.h>
25#include <linux/isdn/capilli.h>
26#include "avmcard.h"
27
28#undef CONFIG_T1PCI_DEBUG
29#undef CONFIG_T1PCI_POLLDEBUG
30
31/* ------------------------------------------------------------- */
32static char *revision = "$Revision: 1.1.2.2 $";
33/* ------------------------------------------------------------- */
34
35static struct pci_device_id t1pci_pci_tbl[] = {
36	{ PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_T1, PCI_ANY_ID, PCI_ANY_ID },
37	{ }				/* Terminating entry */
38};
39
40MODULE_DEVICE_TABLE(pci, t1pci_pci_tbl);
41MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM T1 PCI card");
42MODULE_AUTHOR("Carsten Paeth");
43MODULE_LICENSE("GPL");
44
45/* ------------------------------------------------------------- */
46
47static char *t1pci_procinfo(struct capi_ctr *ctrl);
48
49static int t1pci_add_card(struct capicardparams *p, struct pci_dev *pdev)
50{
51	avmcard *card;
52	avmctrl_info *cinfo;
53	int retval;
54
55	card = b1_alloc_card(1);
56	if (!card) {
57		printk(KERN_WARNING "t1pci: no memory.\n");
58		retval = -ENOMEM;
59		goto err;
60	}
61
62	card->dma = avmcard_dma_alloc("t1pci", pdev, 2048 + 128, 2048 + 128);
63	if (!card->dma) {
64		printk(KERN_WARNING "t1pci: no memory.\n");
65		retval = -ENOMEM;
66		goto err_free;
67	}
68
69	cinfo = card->ctrlinfo;
70	sprintf(card->name, "t1pci-%x", p->port);
71	card->port = p->port;
72	card->irq = p->irq;
73	card->membase = p->membase;
74	card->cardtype = avm_t1pci;
75
76	if (!request_region(card->port, AVMB1_PORTLEN, card->name)) {
77		printk(KERN_WARNING "t1pci: ports 0x%03x-0x%03x in use.\n",
78		       card->port, card->port + AVMB1_PORTLEN);
79		retval = -EBUSY;
80		goto err_free_dma;
81	}
82
83	card->mbase = ioremap(card->membase, 64);
84	if (!card->mbase) {
85		printk(KERN_NOTICE "t1pci: can't remap memory at 0x%lx\n",
86		       card->membase);
87		retval = -EIO;
88		goto err_release_region;
89	}
90
91	b1dma_reset(card);
92
93	retval = t1pci_detect(card);
94	if (retval != 0) {
95		if (retval < 6)
96			printk(KERN_NOTICE "t1pci: NO card at 0x%x (%d)\n",
97			       card->port, retval);
98		else
99			printk(KERN_NOTICE "t1pci: card at 0x%x, but cable not connected or T1 has no power (%d)\n",
100			       card->port, retval);
101		retval = -EIO;
102		goto err_unmap;
103	}
104	b1dma_reset(card);
105
106	retval = request_irq(card->irq, b1dma_interrupt, IRQF_SHARED, card->name, card);
107	if (retval) {
108		printk(KERN_ERR "t1pci: unable to get IRQ %d.\n", card->irq);
109		retval = -EBUSY;
110		goto err_unmap;
111	}
112
113	cinfo->capi_ctrl.owner         = THIS_MODULE;
114	cinfo->capi_ctrl.driver_name   = "t1pci";
115	cinfo->capi_ctrl.driverdata    = cinfo;
116	cinfo->capi_ctrl.register_appl = b1dma_register_appl;
117	cinfo->capi_ctrl.release_appl  = b1dma_release_appl;
118	cinfo->capi_ctrl.send_message  = b1dma_send_message;
119	cinfo->capi_ctrl.load_firmware = b1dma_load_firmware;
120	cinfo->capi_ctrl.reset_ctr     = b1dma_reset_ctr;
121	cinfo->capi_ctrl.procinfo      = t1pci_procinfo;
122	cinfo->capi_ctrl.proc_fops = &b1dmactl_proc_fops;
123	strcpy(cinfo->capi_ctrl.name, card->name);
124
125	retval = attach_capi_ctr(&cinfo->capi_ctrl);
126	if (retval) {
127		printk(KERN_ERR "t1pci: attach controller failed.\n");
128		retval = -EBUSY;
129		goto err_free_irq;
130	}
131	card->cardnr = cinfo->capi_ctrl.cnr;
132
133	printk(KERN_INFO "t1pci: AVM T1 PCI at i/o %#x, irq %d, mem %#lx\n",
134	       card->port, card->irq, card->membase);
135
136	pci_set_drvdata(pdev, card);
137	return 0;
138
139err_free_irq:
140	free_irq(card->irq, card);
141err_unmap:
142	iounmap(card->mbase);
143err_release_region:
144	release_region(card->port, AVMB1_PORTLEN);
145err_free_dma:
146	avmcard_dma_free(card->dma);
147err_free:
148	b1_free_card(card);
149err:
150	return retval;
151}
152
153/* ------------------------------------------------------------- */
154
155static void t1pci_remove(struct pci_dev *pdev)
156{
157	avmcard *card = pci_get_drvdata(pdev);
158	avmctrl_info *cinfo = card->ctrlinfo;
159
160	b1dma_reset(card);
161
162	detach_capi_ctr(&cinfo->capi_ctrl);
163	free_irq(card->irq, card);
164	iounmap(card->mbase);
165	release_region(card->port, AVMB1_PORTLEN);
166	avmcard_dma_free(card->dma);
167	b1_free_card(card);
168}
169
170/* ------------------------------------------------------------- */
171
172static char *t1pci_procinfo(struct capi_ctr *ctrl)
173{
174	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
175
176	if (!cinfo)
177		return "";
178	sprintf(cinfo->infobuf, "%s %s 0x%x %d 0x%lx",
179		cinfo->cardname[0] ? cinfo->cardname : "-",
180		cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
181		cinfo->card ? cinfo->card->port : 0x0,
182		cinfo->card ? cinfo->card->irq : 0,
183		cinfo->card ? cinfo->card->membase : 0
184		);
185	return cinfo->infobuf;
186}
187
188/* ------------------------------------------------------------- */
189
190static int t1pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
191{
192	struct capicardparams param;
193	int retval;
194
195	if (pci_enable_device(dev) < 0) {
196		printk(KERN_ERR	"t1pci: failed to enable AVM-T1-PCI\n");
197		return -ENODEV;
198	}
199	pci_set_master(dev);
200
201	param.port = pci_resource_start(dev, 1);
202	param.irq = dev->irq;
203	param.membase = pci_resource_start(dev, 0);
204
205	printk(KERN_INFO "t1pci: PCI BIOS reports AVM-T1-PCI at i/o %#x, irq %d, mem %#x\n",
206	       param.port, param.irq, param.membase);
207
208	retval = t1pci_add_card(&param, dev);
209	if (retval != 0) {
210		printk(KERN_ERR "t1pci: no AVM-T1-PCI at i/o %#x, irq %d detected, mem %#x\n",
211		       param.port, param.irq, param.membase);
212		pci_disable_device(dev);
213		return -ENODEV;
214	}
215	return 0;
216}
217
218static struct pci_driver t1pci_pci_driver = {
219	.name           = "t1pci",
220	.id_table       = t1pci_pci_tbl,
221	.probe          = t1pci_probe,
222	.remove         = t1pci_remove,
223};
224
225static struct capi_driver capi_driver_t1pci = {
226	.name		= "t1pci",
227	.revision	= "1.0",
228};
229
230static int __init t1pci_init(void)
231{
232	char *p;
233	char rev[32];
234	int err;
235
236	if ((p = strchr(revision, ':')) != NULL && p[1]) {
237		strlcpy(rev, p + 2, 32);
238		if ((p = strchr(rev, '$')) != NULL && p > rev)
239			*(p - 1) = 0;
240	} else
241		strcpy(rev, "1.0");
242
243	err = pci_register_driver(&t1pci_pci_driver);
244	if (!err) {
245		strlcpy(capi_driver_t1pci.revision, rev, 32);
246		register_capi_driver(&capi_driver_t1pci);
247		printk(KERN_INFO "t1pci: revision %s\n", rev);
248	}
249	return err;
250}
251
252static void __exit t1pci_exit(void)
253{
254	unregister_capi_driver(&capi_driver_t1pci);
255	pci_unregister_driver(&t1pci_pci_driver);
256}
257
258module_init(t1pci_init);
259module_exit(t1pci_exit);
260