2 paride.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
5 This is the base module for the family of device drivers
6 that support parallel port IDE devices.
12 1.01 GRG 1998.05.03 Use spinlocks
13 1.02 GRG 1998.05.05 init_proto, release_proto, ktti
14 1.03 GRG 1998.08.15 eliminate compiler warning
15 1.04 GRG 1998.11.28 added support for FRIQ
16 1.05 TMW 2000.06.06 use parport_find_number instead of
18 1.06 TMW 2001.03.26 more sane parport-or-not resource management
21 #define PI_VERSION "1.06"
23 #include <linux/module.h>
24 #include <linux/config.h>
25 #include <linux/kmod.h>
26 #include <linux/types.h>
27 #include <linux/kernel.h>
28 #include <linux/ioport.h>
29 #include <linux/string.h>
30 #include <linux/spinlock.h>
31 #include <linux/wait.h>
32 #include <linux/sched.h> /* TASK_* */
34 #ifdef CONFIG_PARPORT_MODULE
35 #define CONFIG_PARPORT
39 #include <linux/parport.h>
44 MODULE_LICENSE("GPL");
48 static struct pi_protocol *protocols[MAX_PROTOS];
50 static DEFINE_SPINLOCK(pi_spinlock);
52 void pi_write_regr(PIA * pi, int cont, int regr, int val)
54 pi->proto->write_regr(pi, cont, regr, val);
57 EXPORT_SYMBOL(pi_write_regr);
59 int pi_read_regr(PIA * pi, int cont, int regr)
61 return pi->proto->read_regr(pi, cont, regr);
64 EXPORT_SYMBOL(pi_read_regr);
66 void pi_write_block(PIA * pi, char *buf, int count)
68 pi->proto->write_block(pi, buf, count);
71 EXPORT_SYMBOL(pi_write_block);
73 void pi_read_block(PIA * pi, char *buf, int count)
75 pi->proto->read_block(pi, buf, count);
78 EXPORT_SYMBOL(pi_read_block);
82 static void pi_wake_up(void *p)
86 void (*cont) (void) = NULL;
88 spin_lock_irqsave(&pi_spinlock, flags);
90 if (pi->claim_cont && !parport_claim(pi->pardev)) {
91 cont = pi->claim_cont;
92 pi->claim_cont = NULL;
96 spin_unlock_irqrestore(&pi_spinlock, flags);
106 int pi_schedule_claimed(PIA * pi, void (*cont) (void))
108 #ifdef CONFIG_PARPORT
111 spin_lock_irqsave(&pi_spinlock, flags);
112 if (pi->pardev && parport_claim(pi->pardev)) {
113 pi->claim_cont = cont;
114 spin_unlock_irqrestore(&pi_spinlock, flags);
118 spin_unlock_irqrestore(&pi_spinlock, flags);
122 EXPORT_SYMBOL(pi_schedule_claimed);
124 void pi_do_claimed(PIA * pi, void (*cont) (void))
126 if (pi_schedule_claimed(pi, cont))
130 EXPORT_SYMBOL(pi_do_claimed);
132 static void pi_claim(PIA * pi)
137 #ifdef CONFIG_PARPORT
140 !parport_claim((struct pardevice *) pi->pardev));
144 static void pi_unclaim(PIA * pi)
147 #ifdef CONFIG_PARPORT
149 parport_release((struct pardevice *) (pi->pardev));
153 void pi_connect(PIA * pi)
156 pi->proto->connect(pi);
159 EXPORT_SYMBOL(pi_connect);
161 void pi_disconnect(PIA * pi)
163 pi->proto->disconnect(pi);
167 EXPORT_SYMBOL(pi_disconnect);
169 static void pi_unregister_parport(PIA * pi)
171 #ifdef CONFIG_PARPORT
173 parport_unregister_device((struct pardevice *) (pi->pardev));
179 void pi_release(PIA * pi)
181 pi_unregister_parport(pi);
182 #ifndef CONFIG_PARPORT
184 release_region(pi->port, pi->reserved);
185 #endif /* !CONFIG_PARPORT */
186 if (pi->proto->release_proto)
187 pi->proto->release_proto(pi);
188 module_put(pi->proto->owner);
191 EXPORT_SYMBOL(pi_release);
193 static int default_test_proto(PIA * pi, char *scratch, int verbose)
198 pi->proto->connect(pi);
200 for (j = 0; j < 2; j++) {
201 pi_write_regr(pi, 0, 6, 0xa0 + j * 0x10);
202 for (k = 0; k < 256; k++) {
203 pi_write_regr(pi, 0, 2, k ^ 0xaa);
204 pi_write_regr(pi, 0, 3, k ^ 0x55);
205 if (pi_read_regr(pi, 0, 2) != (k ^ 0xaa))
209 pi->proto->disconnect(pi);
212 printk("%s: %s: port 0x%x, mode %d, test=(%d,%d)\n",
213 pi->device, pi->proto->name, pi->port,
214 pi->mode, e[0], e[1]);
216 return (e[0] && e[1]); /* not here if both > 0 */
219 static int pi_test_proto(PIA * pi, char *scratch, int verbose)
224 if (pi->proto->test_proto)
225 res = pi->proto->test_proto(pi, scratch, verbose);
227 res = default_test_proto(pi, scratch, verbose);
233 int pi_register(PIP * pr)
237 for (k = 0; k < MAX_PROTOS; k++)
238 if (protocols[k] && !strcmp(pr->name, protocols[k]->name)) {
239 printk("paride: %s protocol already registered\n",
244 while ((k < MAX_PROTOS) && (protocols[k]))
246 if (k == MAX_PROTOS) {
247 printk("paride: protocol table full\n");
252 printk("paride: %s registered as protocol %d\n", pr->name, k);
256 EXPORT_SYMBOL(pi_register);
258 void pi_unregister(PIP * pr)
262 if (protocols[pr->index] != pr) {
263 printk("paride: %s not registered\n", pr->name);
266 protocols[pr->index] = NULL;
269 EXPORT_SYMBOL(pi_unregister);
271 static int pi_register_parport(PIA * pi, int verbose)
273 #ifdef CONFIG_PARPORT
275 struct parport *port;
277 port = parport_find_base(pi->port);
281 pi->pardev = parport_register_device(port,
283 pi_wake_up, NULL, 0, (void *) pi);
284 parport_put_port(port);
288 init_waitqueue_head(&pi->parq);
291 printk("%s: 0x%x is %s\n", pi->device, pi->port, port->name);
293 pi->parname = (char *) port->name;
299 static int pi_probe_mode(PIA * pi, int max, char *scratch, int verbose)
303 if (pi->mode != -1) {
307 if (pi->mode >= pi->proto->epp_first)
309 if ((range == 8) && (pi->port % 8))
311 pi->reserved = range;
312 return (!pi_test_proto(pi, scratch, verbose));
315 for (pi->mode = 0; pi->mode < max; pi->mode++) {
317 if (pi->mode >= pi->proto->epp_first)
319 if ((range == 8) && (pi->port % 8))
321 pi->reserved = range;
322 if (!pi_test_proto(pi, scratch, verbose))
329 static int pi_probe_unit(PIA * pi, int unit, char *scratch, int verbose)
338 e = pi->proto->max_units;
341 if (!pi_register_parport(pi, verbose))
344 if (pi->proto->test_port) {
346 max = pi->proto->test_port(pi);
349 max = pi->proto->max_mode;
351 if (pi->proto->probe_unit) {
353 for (pi->unit = s; pi->unit < e; pi->unit++)
354 if (pi->proto->probe_unit(pi)) {
356 if (pi_probe_mode(pi, max, scratch, verbose))
358 pi_unregister_parport(pi);
362 pi_unregister_parport(pi);
366 if (!pi_probe_mode(pi, max, scratch, verbose)) {
367 pi_unregister_parport(pi);
374 int pi_init(PIA * pi, int autoprobe, int port, int mode,
375 int unit, int protocol, int delay, char *scratch,
376 int devtype, int verbose, char *device)
379 int lpts[7] = { 0x3bc, 0x378, 0x278, 0x268, 0x27c, 0x26c, 0 };
385 request_module("paride_protocol");
390 } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
391 (!protocols[s]) || (unit < 0) ||
392 (unit >= protocols[s]->max_units)) {
393 printk("%s: Invalid parameters\n", device);
397 for (p = s; p < e; p++) {
398 struct pi_protocol *proto = protocols[p];
402 if (!try_module_get(proto->owner))
406 if (proto->init_proto && proto->init_proto(pi) < 0) {
408 module_put(proto->owner);
412 pi->delay = pi->proto->default_delay;
415 pi->devtype = devtype;
420 init_waitqueue_head(&pi->parq);
422 pi->claim_cont = NULL;
427 if (pi_probe_unit(pi, unit, scratch, verbose))
432 while ((pi->port = lpts[k++]))
434 (pi, unit, scratch, verbose))
439 if (pi->proto->release_proto)
440 pi->proto->release_proto(pi);
441 module_put(proto->owner);
446 printk("%s: Autoprobe failed\n", device);
448 printk("%s: Adapter not found\n", device);
451 #ifndef CONFIG_PARPORT
452 if (!request_region(pi->port, pi->reserved, pi->device)) {
453 printk(KERN_WARNING "paride: Unable to request region 0x%x\n",
457 #endif /* !CONFIG_PARPORT */
460 printk("%s: Sharing %s at 0x%x\n", pi->device,
461 pi->parname, pi->port);
463 pi->proto->log_adapter(pi, scratch, verbose);
468 EXPORT_SYMBOL(pi_init);