1 /* $Id: b1pcmcia.c,v 1.1.4.1 2001/11/20 14:19:34 kai Exp $
2  *
3  * Module for AVM B1/M1/M2 PCMCIA-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/init.h>
20 #include <asm/io.h>
21 #include <linux/capi.h>
22 #include <linux/b1pcmcia.h>
23 #include "capicmd.h"
24 #include "capiutil.h"
25 #include "capilli.h"
26 #include "avmcard.h"
27 
28 static char *revision = "$Revision: 1.1.4.1 $";
29 
30 /* ------------------------------------------------------------- */
31 
32 MODULE_DESCRIPTION("CAPI4Linux: Driver for AVM PCMCIA cards");
33 MODULE_AUTHOR("Carsten Paeth");
34 MODULE_LICENSE("GPL");
35 
36 /* ------------------------------------------------------------- */
37 
38 static struct capi_driver_interface *di;
39 
40 /* ------------------------------------------------------------- */
41 
b1pcmcia_interrupt(int interrupt,void * devptr,struct pt_regs * regs)42 static void b1pcmcia_interrupt(int interrupt, void *devptr, struct pt_regs *regs)
43 {
44 	avmcard *card;
45 
46 	card = (avmcard *) devptr;
47 
48 	if (!card) {
49 		printk(KERN_WARNING "b1pcmcia: interrupt: wrong device\n");
50 		return;
51 	}
52 	if (card->interrupt) {
53 		printk(KERN_ERR "%s: reentering interrupt hander.\n",
54 			card->name);
55 		return;
56 	}
57 
58 	card->interrupt = 1;
59 
60 	b1_handle_interrupt(card);
61 
62 	card->interrupt = 0;
63 }
64 /* ------------------------------------------------------------- */
65 
b1pcmcia_remove_ctr(struct capi_ctr * ctrl)66 static void b1pcmcia_remove_ctr(struct capi_ctr *ctrl)
67 {
68 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
69 	avmcard *card = cinfo->card;
70 	unsigned int port = card->port;
71 
72 	b1_reset(port);
73 	b1_reset(port);
74 
75 	di->detach_ctr(ctrl);
76 	free_irq(card->irq, card);
77 	/* io addrsses managent by CardServices
78 	 * release_region(card->port, AVMB1_PORTLEN);
79 	 */
80 	kfree(card);
81 
82 	MOD_DEC_USE_COUNT;
83 }
84 
85 /* ------------------------------------------------------------- */
86 
b1pcmcia_add_card(struct capi_driver * driver,unsigned int port,unsigned irq,enum avmcardtype cardtype)87 static int b1pcmcia_add_card(struct capi_driver *driver,
88 				unsigned int port,
89 				unsigned irq,
90 				enum avmcardtype cardtype)
91 {
92 	avmctrl_info *cinfo;
93 	avmcard *card;
94 	char *cardname;
95 	int retval;
96 
97 	MOD_INC_USE_COUNT;
98 
99 	card = (avmcard *) kmalloc(sizeof(avmcard), GFP_ATOMIC);
100 
101 	if (!card) {
102 		printk(KERN_WARNING "%s: no memory.\n", driver->name);
103 	        MOD_DEC_USE_COUNT;
104 		return -ENOMEM;
105 	}
106 	memset(card, 0, sizeof(avmcard));
107         cinfo = (avmctrl_info *) kmalloc(sizeof(avmctrl_info), GFP_ATOMIC);
108 	if (!cinfo) {
109 		printk(KERN_WARNING "%s: no memory.\n", driver->name);
110 		kfree(card);
111 	        MOD_DEC_USE_COUNT;
112 		return -ENOMEM;
113 	}
114 	memset(cinfo, 0, sizeof(avmctrl_info));
115 	card->ctrlinfo = cinfo;
116 	cinfo->card = card;
117 	switch (cardtype) {
118 		case avm_m1: sprintf(card->name, "m1-%x", port); break;
119 		case avm_m2: sprintf(card->name, "m2-%x", port); break;
120 		default: sprintf(card->name, "b1pcmcia-%x", port); break;
121 	}
122 	card->port = port;
123 	card->irq = irq;
124 	card->cardtype = cardtype;
125 
126 	b1_reset(card->port);
127 	if ((retval = b1_detect(card->port, card->cardtype)) != 0) {
128 		printk(KERN_NOTICE "%s: NO card at 0x%x (%d)\n",
129 					driver->name, card->port, retval);
130 	        kfree(card->ctrlinfo);
131 		kfree(card);
132 	        MOD_DEC_USE_COUNT;
133 		return -EIO;
134 	}
135 	b1_reset(card->port);
136 	b1_getrevision(card);
137 
138 	retval = request_irq(card->irq, b1pcmcia_interrupt, 0, card->name, card);
139 	if (retval) {
140 		printk(KERN_ERR "%s: unable to get IRQ %d.\n",
141 				driver->name, card->irq);
142 	        kfree(card->ctrlinfo);
143 		kfree(card);
144 	        MOD_DEC_USE_COUNT;
145 		return -EBUSY;
146 	}
147 
148 	cinfo->capi_ctrl = di->attach_ctr(driver, card->name, cinfo);
149 	if (!cinfo->capi_ctrl) {
150 		printk(KERN_ERR "%s: attach controller failed.\n",
151 				driver->name);
152 		free_irq(card->irq, card);
153 	        kfree(card->ctrlinfo);
154 		kfree(card);
155 	        MOD_DEC_USE_COUNT;
156 		return -EBUSY;
157 	}
158 	switch (cardtype) {
159 		case avm_m1: cardname = "M1"; break;
160 		case avm_m2: cardname = "M2"; break;
161 		default    : cardname = "B1 PCMCIA"; break;
162 	}
163 
164 	printk(KERN_INFO
165 		"%s: AVM %s at i/o %#x, irq %d, revision %d\n",
166 		driver->name, cardname, card->port, card->irq, card->revision);
167 
168 	return cinfo->capi_ctrl->cnr;
169 }
170 
171 /* ------------------------------------------------------------- */
172 
b1pcmcia_procinfo(struct capi_ctr * ctrl)173 static char *b1pcmcia_procinfo(struct capi_ctr *ctrl)
174 {
175 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
176 
177 	if (!cinfo)
178 		return "";
179 	sprintf(cinfo->infobuf, "%s %s 0x%x %d r%d",
180 		cinfo->cardname[0] ? cinfo->cardname : "-",
181 		cinfo->version[VER_DRIVER] ? cinfo->version[VER_DRIVER] : "-",
182 		cinfo->card ? cinfo->card->port : 0x0,
183 		cinfo->card ? cinfo->card->irq : 0,
184 		cinfo->card ? cinfo->card->revision : 0
185 		);
186 	return cinfo->infobuf;
187 }
188 
189 /* ------------------------------------------------------------- */
190 
191 static struct capi_driver b1pcmcia_driver = {
192     name: "b1pcmcia",
193     revision: "0.0",
194     load_firmware: b1_load_firmware,
195     reset_ctr: b1_reset_ctr,
196     remove_ctr: b1pcmcia_remove_ctr,
197     register_appl: b1_register_appl,
198     release_appl: b1_release_appl,
199     send_message: b1_send_message,
200 
201     procinfo: b1pcmcia_procinfo,
202     ctr_read_proc: b1ctl_read_proc,
203     driver_read_proc: 0,	/* use standard driver_read_proc */
204 
205     add_card: 0,
206 };
207 
208 /* ------------------------------------------------------------- */
209 
b1pcmcia_addcard_b1(unsigned int port,unsigned irq)210 int b1pcmcia_addcard_b1(unsigned int port, unsigned irq)
211 {
212 	return b1pcmcia_add_card(&b1pcmcia_driver, port, irq, avm_b1pcmcia);
213 }
214 
b1pcmcia_addcard_m1(unsigned int port,unsigned irq)215 int b1pcmcia_addcard_m1(unsigned int port, unsigned irq)
216 {
217 	return b1pcmcia_add_card(&b1pcmcia_driver, port, irq, avm_m1);
218 }
219 
b1pcmcia_addcard_m2(unsigned int port,unsigned irq)220 int b1pcmcia_addcard_m2(unsigned int port, unsigned irq)
221 {
222 	return b1pcmcia_add_card(&b1pcmcia_driver, port, irq, avm_m2);
223 }
224 
b1pcmcia_delcard(unsigned int port,unsigned irq)225 int b1pcmcia_delcard(unsigned int port, unsigned irq)
226 {
227 	struct capi_ctr *ctrl;
228 	avmcard *card;
229 
230 	for (ctrl = b1pcmcia_driver.controller; ctrl; ctrl = ctrl->next) {
231 		card = ((avmctrl_info *)(ctrl->driverdata))->card;
232 		if (card->port == port && card->irq == irq) {
233 			b1pcmcia_remove_ctr(ctrl);
234 			return 0;
235 		}
236 	}
237 	return -ESRCH;
238 }
239 
240 EXPORT_SYMBOL(b1pcmcia_addcard_b1);
241 EXPORT_SYMBOL(b1pcmcia_addcard_m1);
242 EXPORT_SYMBOL(b1pcmcia_addcard_m2);
243 EXPORT_SYMBOL(b1pcmcia_delcard);
244 
245 /* ------------------------------------------------------------- */
246 
b1pcmcia_init(void)247 static int __init b1pcmcia_init(void)
248 {
249 	struct capi_driver *driver = &b1pcmcia_driver;
250 	char *p;
251 	int retval = 0;
252 
253 	MOD_INC_USE_COUNT;
254 
255 	if ((p = strchr(revision, ':')) != 0 && p[1]) {
256 		strncpy(driver->revision, p + 2, sizeof(driver->revision));
257 		driver->revision[sizeof(driver->revision)-1] = 0;
258 		if ((p = strchr(driver->revision, '$')) != 0 && p > driver->revision)
259 			*(p-1) = 0;
260 	}
261 
262 	printk(KERN_INFO "%s: revision %s\n", driver->name, driver->revision);
263 
264         di = attach_capi_driver(driver);
265 
266 	if (!di) {
267 		printk(KERN_ERR "%s: failed to attach capi_driver\n",
268 				driver->name);
269 		retval = -EIO;
270 	}
271 	MOD_DEC_USE_COUNT;
272 	return retval;
273 }
274 
b1pcmcia_exit(void)275 static void __exit b1pcmcia_exit(void)
276 {
277     detach_capi_driver(&b1pcmcia_driver);
278 }
279 
280 module_init(b1pcmcia_init);
281 module_exit(b1pcmcia_exit);
282