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>
33 #ifdef CONFIG_PARPORT_MODULE
34 #define CONFIG_PARPORT
38 #include <linux/parport.h>
45 static struct pi_protocol *protocols[MAX_PROTOS];
47 static spinlock_t pi_spinlock = SPIN_LOCK_UNLOCKED;
49 void pi_write_regr( PIA *pi, int cont, int regr, int val)
51 { pi->proto->write_regr(pi,cont,regr,val);
54 int pi_read_regr( PIA *pi, int cont, int regr)
56 { return pi->proto->read_regr(pi,cont,regr);
59 void pi_write_block( PIA *pi, char * buf, int count)
61 { pi->proto->write_block(pi,buf,count);
64 void pi_read_block( PIA *pi, char * buf, int count)
66 { pi->proto->read_block(pi,buf,count);
71 static void pi_wake_up( void *p)
73 { PIA *pi = (PIA *) p;
75 void (*cont)(void) = NULL;
77 spin_lock_irqsave(&pi_spinlock,flags);
79 if (pi->claim_cont && !parport_claim(pi->pardev)) {
80 cont = pi->claim_cont;
81 pi->claim_cont = NULL;
85 spin_unlock_irqrestore(&pi_spinlock,flags);
94 void pi_do_claimed( PIA *pi, void(*cont)(void))
98 { unsigned long flags;
100 spin_lock_irqsave(&pi_spinlock,flags);
102 if (!pi->pardev || !parport_claim(pi->pardev)) {
104 spin_unlock_irqrestore(&pi_spinlock,flags);
107 pi->claim_cont = cont;
108 spin_unlock_irqrestore(&pi_spinlock,flags);
119 static void pi_claim( PIA *pi)
121 { if (pi->claimed) return;
123 #ifdef CONFIG_PARPORT
125 wait_event (pi->parq,
126 !parport_claim ((struct pardevice *)pi->pardev));
130 static void pi_unclaim( PIA *pi)
133 #ifdef CONFIG_PARPORT
134 if (pi->pardev) parport_release((struct pardevice *)(pi->pardev));
138 void pi_connect( PIA *pi)
141 pi->proto->connect(pi);
144 void pi_disconnect( PIA *pi)
146 { pi->proto->disconnect(pi);
150 static void pi_unregister_parport( PIA *pi)
153 #ifdef CONFIG_PARPORT
155 parport_unregister_device((struct pardevice *)(pi->pardev));
161 void pi_release( PIA *pi)
163 { pi_unregister_parport(pi);
164 #ifndef CONFIG_PARPORT
166 release_region(pi->port,pi->reserved);
167 #endif /* !CONFIG_PARPORT */
168 pi->proto->release_proto(pi);
171 #define WR(r,v) pi_write_regr(pi,0,r,v)
172 #define RR(r) (pi_read_regr(pi,0,r))
174 static int pi_test_proto( PIA *pi, char * scratch, int verbose )
179 if (pi->proto->test_proto) {
181 j = pi->proto->test_proto(pi,scratch,verbose);
190 for (k=0;k<256;k++) {
193 if (RR(2) != (k^0xaa)) e[j]++;
200 printk("%s: %s: port 0x%x, mode %d, test=(%d,%d)\n",
201 pi->device,pi->proto->name,pi->port,
204 return (e[0] && e[1]); /* not here if both > 0 */
207 int pi_register( PIP *pr)
211 for (k=0;k<MAX_PROTOS;k++)
212 if (protocols[k] && !strcmp(pr->name,protocols[k]->name)) {
213 printk("paride: %s protocol already registered\n",pr->name);
217 while((k<MAX_PROTOS) && (protocols[k])) k++;
218 if (k == MAX_PROTOS) {
219 printk("paride: protocol table full\n");
225 printk("paride: %s registered as protocol %d\n",pr->name,k);
229 void pi_unregister( PIP *pr)
232 if (protocols[pr->index] != pr) {
233 printk("paride: %s not registered\n",pr->name);
236 protocols[pr->index] = 0;
240 static int pi_register_parport( PIA *pi, int verbose)
243 #ifdef CONFIG_PARPORT
245 struct parport *port;
247 port = parport_find_base (pi->port);
251 pi->pardev = parport_register_device(port,
255 parport_put_port (port);
259 init_waitqueue_head(&pi->parq);
261 if (verbose) printk("%s: 0x%x is %s\n",pi->device,pi->port,
264 pi->parname = (char *)port->name;
270 static int pi_probe_mode( PIA *pi, int max, char * scratch, int verbose)
274 if (pi->mode != -1) {
275 if (pi->mode >= max) return 0;
277 if (pi->mode >= pi->proto->epp_first) range = 8;
278 if ((range == 8) && (pi->port % 8)) return 0;
279 pi->reserved = range;
280 return (!pi_test_proto(pi,scratch,verbose));
283 for(pi->mode=0;pi->mode<max;pi->mode++) {
285 if (pi->mode >= pi->proto->epp_first) range = 8;
286 if ((range == 8) && (pi->port % 8)) break;
287 pi->reserved = range;
288 if (!pi_test_proto(pi,scratch,verbose)) best = pi->mode;
294 static int pi_probe_unit( PIA *pi, int unit, char * scratch, int verbose)
302 e = pi->proto->max_units;
305 if (!pi_register_parport(pi,verbose))
308 if (pi->proto->test_port) {
310 max = pi->proto->test_port(pi);
313 else max = pi->proto->max_mode;
315 if (pi->proto->probe_unit) {
317 for (pi->unit=s;pi->unit<e;pi->unit++)
318 if (pi->proto->probe_unit(pi)) {
320 if (pi_probe_mode(pi,max,scratch,verbose)) return 1;
321 pi_unregister_parport(pi);
325 pi_unregister_parport(pi);
329 if (!pi_probe_mode(pi,max,scratch,verbose)) {
330 pi_unregister_parport(pi);
337 int pi_init(PIA *pi, int autoprobe, int port, int mode,
338 int unit, int protocol, int delay, char * scratch,
339 int devtype, int verbose, char *device )
342 int lpts[7] = {0x3bc,0x378,0x278,0x268,0x27c,0x26c,0};
344 s = protocol; e = s+1;
347 request_module ("paride_protocol");
352 } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
353 (!protocols[s]) || (unit < 0) ||
354 (unit >= protocols[s]->max_units)) {
355 printk("%s: Invalid parameters\n",device);
361 pi->proto = protocols[p];
363 pi->proto->init_proto(pi);
364 if (delay == -1) pi->delay = pi->proto->default_delay;
365 else pi->delay = delay;
366 pi->devtype = devtype;
371 init_waitqueue_head(&pi->parq);
373 pi->claim_cont = NULL;
378 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
382 while ((pi->port = lpts[k++]))
383 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
386 pi->proto->release_proto(pi);
391 if (autoprobe) printk("%s: Autoprobe failed\n",device);
392 else printk("%s: Adapter not found\n",device);
396 #ifndef CONFIG_PARPORT
397 if (!request_region(pi->port,pi->reserved,pi->device))
399 printk(KERN_WARNING"paride: Unable to request region 0x%x\n", pi->port);
402 #endif /* !CONFIG_PARPORT */
405 printk("%s: Sharing %s at 0x%x\n",pi->device,
406 pi->parname,pi->port);
408 pi->proto->log_adapter(pi,scratch,verbose);
415 int init_module(void)
419 const char *indicate_pp = "";
420 #ifdef CONFIG_PARPORT
421 indicate_pp = " (parport)";
424 for (k=0;k<MAX_PROTOS;k++) protocols[k] = 0;
426 printk("paride: version %s installed%s\n",PI_VERSION,indicate_pp);
430 void cleanup_module(void)
437 void paride_init( void )
441 #ifdef CONFIG_PARIDE_ATEN
442 { extern struct pi_protocol aten;
446 #ifdef CONFIG_PARIDE_BPCK
447 { extern struct pi_protocol bpck;
451 #ifdef CONFIG_PARIDE_COMM
452 { extern struct pi_protocol comm;
456 #ifdef CONFIG_PARIDE_DSTR
457 { extern struct pi_protocol dstr;
461 #ifdef CONFIG_PARIDE_EPAT
462 { extern struct pi_protocol epat;
466 #ifdef CONFIG_PARIDE_EPIA
467 { extern struct pi_protocol epia;
471 #ifdef CONFIG_PARIDE_FRPW
472 { extern struct pi_protocol frpw;
476 #ifdef CONFIG_PARIDE_FRIQ
477 { extern struct pi_protocol friq;
481 #ifdef CONFIG_PARIDE_FIT2
482 { extern struct pi_protocol fit2;
486 #ifdef CONFIG_PARIDE_FIT3
487 { extern struct pi_protocol fit3;
491 #ifdef CONFIG_PARIDE_KBIC
492 { extern struct pi_protocol k951;
493 extern struct pi_protocol k971;
498 #ifdef CONFIG_PARIDE_KTTI
499 { extern struct pi_protocol ktti;
503 #ifdef CONFIG_PARIDE_ON20
504 { extern struct pi_protocol on20;
508 #ifdef CONFIG_PARIDE_ON26
509 { extern struct pi_protocol on26;
514 #ifdef CONFIG_PARIDE_PD
515 { extern int pd_init(void);
519 #ifdef CONFIG_PARIDE_PCD
520 { extern int pcd_init(void);
524 #ifdef CONFIG_PARIDE_PF
525 { extern int pf_init(void);
529 #ifdef CONFIG_PARIDE_PT
530 { extern int pt_init(void);
534 #ifdef CONFIG_PARIDE_PG
535 { extern int pg_init(void);
543 /* end of paride.c */
544 MODULE_LICENSE("GPL");