* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / block / paride / paride.c
blobf05196389aa9b7c54165469fdf124e08d4c7fcab
1 /*
2 paride.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU public license.
5 This is the base module for the family of device drivers
6 that support parallel port IDE devices.
8 */
10 /* Changes:
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
19 #define PI_VERSION "1.04"
21 #include <linux/module.h>
22 #include <linux/config.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/ioport.h>
26 #include <linux/string.h>
27 #include <linux/spinlock.h>
28 #include <linux/wait.h>
30 #ifdef CONFIG_PARPORT_MODULE
31 #define CONFIG_PARPORT
32 #endif
34 #ifdef CONFIG_PARPORT
35 #include <linux/parport.h>
36 #endif
38 #include "paride.h"
40 #define MAX_PROTOS 32
42 static struct pi_protocol *protocols[MAX_PROTOS];
44 spinlock_t pi_spinlock = SPIN_LOCK_UNLOCKED;
46 void pi_write_regr( PIA *pi, int cont, int regr, int val)
48 { pi->proto->write_regr(pi,cont,regr,val);
51 int pi_read_regr( PIA *pi, int cont, int regr)
53 { return pi->proto->read_regr(pi,cont,regr);
56 void pi_write_block( PIA *pi, char * buf, int count)
58 { pi->proto->write_block(pi,buf,count);
61 void pi_read_block( PIA *pi, char * buf, int count)
63 { pi->proto->read_block(pi,buf,count);
66 #ifdef CONFIG_PARPORT
68 static void pi_wake_up( void *p)
70 { PIA *pi = (PIA *) p;
71 long flags;
72 void (*cont)(void) = NULL;
74 spin_lock_irqsave(&pi_spinlock,flags);
76 if (pi->claim_cont && !parport_claim(pi->pardev)) {
77 cont = pi->claim_cont;
78 pi->claim_cont = NULL;
79 pi->claimed = 1;
82 spin_unlock_irqrestore(&pi_spinlock,flags);
84 wake_up(&(pi->parq));
86 if (cont) cont();
89 #endif
91 void pi_do_claimed( PIA *pi, void(*cont)(void))
93 #ifdef CONFIG_PARPORT
95 { long flags;
97 spin_lock_irqsave(&pi_spinlock,flags);
99 if (!pi->pardev || !parport_claim(pi->pardev)) {
100 pi->claimed = 1;
101 spin_unlock_irqrestore(&pi_spinlock,flags);
102 cont();
103 } else {
104 pi->claim_cont = cont;
105 spin_unlock_irqrestore(&pi_spinlock,flags);
109 #else
111 { cont();
114 #endif
116 static void pi_claim( PIA *pi)
118 { if (pi->claimed) return;
119 pi->claimed = 1;
120 #ifdef CONFIG_PARPORT
121 if (pi->pardev)
122 while (parport_claim((struct pardevice *)(pi->pardev)))
123 sleep_on(&(pi->parq));
124 #endif
127 static void pi_unclaim( PIA *pi)
129 { pi->claimed = 0;
130 #ifdef CONFIG_PARPORT
131 if (pi->pardev) parport_release((struct pardevice *)(pi->pardev));
132 #endif
135 void pi_connect( PIA *pi)
137 { pi_claim(pi);
138 pi->proto->connect(pi);
141 void pi_disconnect( PIA *pi)
143 { pi->proto->disconnect(pi);
144 pi_unclaim(pi);
147 static void pi_unregister_parport( PIA *pi)
150 #ifdef CONFIG_PARPORT
151 if (pi->pardev) {
152 parport_unregister_device((struct pardevice *)(pi->pardev));
153 pi->pardev = NULL;
155 #endif
158 void pi_release( PIA *pi)
160 { pi_unregister_parport(pi);
161 if ((!pi->pardev)&&(pi->reserved))
162 release_region(pi->port,pi->reserved);
163 pi->proto->release_proto(pi);
166 #define WR(r,v) pi_write_regr(pi,0,r,v)
167 #define RR(r) (pi_read_regr(pi,0,r))
169 static int pi_test_proto( PIA *pi, char * scratch, int verbose )
171 { int j, k;
172 int e[2] = {0,0};
174 if (pi->proto->test_proto) {
175 pi_claim(pi);
176 j = pi->proto->test_proto(pi,scratch,verbose);
177 pi_unclaim(pi);
178 return j;
181 pi_connect(pi);
183 for (j=0;j<2;j++) {
184 WR(6,0xa0+j*0x10);
185 for (k=0;k<256;k++) {
186 WR(2,k^0xaa);
187 WR(3,k^0x55);
188 if (RR(2) != (k^0xaa)) e[j]++;
192 pi_disconnect(pi);
194 if (verbose)
195 printk("%s: %s: port 0x%x, mode %d, test=(%d,%d)\n",
196 pi->device,pi->proto->name,pi->port,
197 pi->mode,e[0],e[1]);
199 return (e[0] && e[1]); /* not here if both > 0 */
202 int pi_register( PIP *pr)
204 { int k;
206 for (k=0;k<MAX_PROTOS;k++)
207 if (protocols[k] && !strcmp(pr->name,protocols[k]->name)) {
208 printk("paride: %s protocol already registered\n",pr->name);
209 return 0;
211 k = 0;
212 while((k<MAX_PROTOS) && (protocols[k])) k++;
213 if (k == MAX_PROTOS) {
214 printk("paride: protocol table full\n");
215 return 0;
217 MOD_INC_USE_COUNT;
218 protocols[k] = pr;
219 pr->index = k;
220 printk("paride: %s registered as protocol %d\n",pr->name,k);
221 return 1;
224 void pi_unregister( PIP *pr)
226 { if (!pr) return;
227 if (protocols[pr->index] != pr) {
228 printk("paride: %s not registered\n",pr->name);
229 return;
231 protocols[pr->index] = 0;
232 MOD_DEC_USE_COUNT;
235 static void pi_register_parport( PIA *pi, int verbose)
238 #ifdef CONFIG_PARPORT
240 struct parport *pp;
242 pp = parport_enumerate();
244 while((pp)&&(pp->base != pi->port)) pp = pp->next;
246 if (!pp) return;
248 pi->pardev = (void *) parport_register_device(
249 pp,pi->device,NULL,pi_wake_up,NULL,0,(void *)pi);
251 init_waitqueue_head(&pi->parq);
253 if (verbose) printk("%s: 0x%x is %s\n",pi->device,pi->port,pp->name);
255 pi->parname = (char *)pp->name;
257 #endif
260 static int pi_probe_mode( PIA *pi, int max, char * scratch, int verbose)
262 { int best, range;
264 if (pi->mode != -1) {
265 if (pi->mode >= max) return 0;
266 range = 3;
267 if (pi->mode >= pi->proto->epp_first) range = 8;
268 if ((range == 8) && (pi->port % 8)) return 0;
269 if ((!pi->pardev) && check_region(pi->port,range)) return 0;
270 pi->reserved = range;
271 return (!pi_test_proto(pi,scratch,verbose));
273 best = -1;
274 for(pi->mode=0;pi->mode<max;pi->mode++) {
275 range = 3;
276 if (pi->mode >= pi->proto->epp_first) range = 8;
277 if ((range == 8) && (pi->port % 8)) break;
278 if ((!pi->pardev) && check_region(pi->port,range)) break;
279 pi->reserved = range;
280 if (!pi_test_proto(pi,scratch,verbose)) best = pi->mode;
282 pi->mode = best;
283 return (best > -1);
286 static int pi_probe_unit( PIA *pi, int unit, char * scratch, int verbose)
288 { int max,s,e;
290 s = unit; e = s+1;
292 if (s == -1) {
293 s = 0;
294 e = pi->proto->max_units;
297 pi_register_parport(pi,verbose);
299 if ((!pi->pardev) && check_region(pi->port,3)) return 0;
301 if (pi->proto->test_port) {
302 pi_claim(pi);
303 max = pi->proto->test_port(pi);
304 pi_unclaim(pi);
306 else max = pi->proto->max_mode;
308 if (pi->proto->probe_unit) {
309 pi_claim(pi);
310 for (pi->unit=s;pi->unit<e;pi->unit++)
311 if (pi->proto->probe_unit(pi)) {
312 pi_unclaim(pi);
313 if (pi_probe_mode(pi,max,scratch,verbose)) return 1;
314 pi_unregister_parport(pi);
315 return 0;
317 pi_unclaim(pi);
318 pi_unregister_parport(pi);
319 return 0;
322 if (!pi_probe_mode(pi,max,scratch,verbose)) {
323 pi_unregister_parport(pi);
324 return 0;
326 return 1;
330 int pi_init(PIA *pi, int autoprobe, int port, int mode,
331 int unit, int protocol, int delay, char * scratch,
332 int devtype, int verbose, char *device )
334 { int p,k,s,e;
335 int lpts[7] = {0x3bc,0x378,0x278,0x268,0x27c,0x26c,0};
337 s = protocol; e = s+1;
339 if (autoprobe) {
340 s = 0;
341 e = MAX_PROTOS;
342 } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
343 (!protocols[s]) || (unit < 0) ||
344 (unit >= protocols[s]->max_units)) {
345 printk("%s: Invalid parameters\n",device);
346 return 0;
349 for (p=s;p<e;p++) {
350 if (protocols[p]) {
351 pi->proto = protocols[p];
352 pi->private = 0;
353 pi->proto->init_proto(pi);
354 if (delay == -1) pi->delay = pi->proto->default_delay;
355 else pi->delay = delay;
356 pi->devtype = devtype;
357 pi->device = device;
359 pi->parname = NULL;
360 pi->pardev = NULL;
361 init_waitqueue_head(&pi->parq);
362 pi->claimed = 0;
363 pi->claim_cont = NULL;
365 pi->mode = mode;
366 if (port != -1) {
367 pi->port = port;
368 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
369 pi->port = 0;
370 } else {
371 k = 0;
372 while ((pi->port = lpts[k++]))
373 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
374 if (pi->port) break;
376 pi->proto->release_proto(pi);
380 if (!pi->port) {
381 if (autoprobe) printk("%s: Autoprobe failed\n",device);
382 else printk("%s: Adapter not found\n",device);
383 return 0;
386 if (!pi->pardev)
387 request_region(pi->port,pi->reserved,pi->device);
389 if (pi->parname)
390 printk("%s: Sharing %s at 0x%x\n",pi->device,
391 pi->parname,pi->port);
393 pi->proto->log_adapter(pi,scratch,verbose);
395 return 1;
398 #ifdef MODULE
400 int init_module(void)
402 { int k;
404 for (k=0;k<MAX_PROTOS;k++) protocols[k] = 0;
405 printk("paride: version %s installed\n",PI_VERSION);
406 return 0;
409 void cleanup_module(void)
414 #else
416 void paride_init( void )
420 #ifdef CONFIG_PARIDE_ATEN
421 { extern struct pi_protocol aten;
422 pi_register(&aten);
424 #endif
425 #ifdef CONFIG_PARIDE_BPCK
426 { extern struct pi_protocol bpck;
427 pi_register(&bpck);
429 #endif
430 #ifdef CONFIG_PARIDE_COMM
431 { extern struct pi_protocol comm;
432 pi_register(&comm);
434 #endif
435 #ifdef CONFIG_PARIDE_DSTR
436 { extern struct pi_protocol dstr;
437 pi_register(&dstr);
439 #endif
440 #ifdef CONFIG_PARIDE_EPAT
441 { extern struct pi_protocol epat;
442 pi_register(&epat);
444 #endif
445 #ifdef CONFIG_PARIDE_EPIA
446 { extern struct pi_protocol epia;
447 pi_register(&epia);
449 #endif
450 #ifdef CONFIG_PARIDE_FRPW
451 { extern struct pi_protocol frpw;
452 pi_register(&frpw);
454 #endif
455 #ifdef CONFIG_PARIDE_FRIQ
456 { extern struct pi_protocol friq;
457 pi_register(&friq);
459 #endif
460 #ifdef CONFIG_PARIDE_FIT2
461 { extern struct pi_protocol fit2;
462 pi_register(&fit2);
464 #endif
465 #ifdef CONFIG_PARIDE_FIT3
466 { extern struct pi_protocol fit3;
467 pi_register(&fit3);
469 #endif
470 #ifdef CONFIG_PARIDE_KBIC
471 { extern struct pi_protocol k951;
472 extern struct pi_protocol k971;
473 pi_register(&k951);
474 pi_register(&k971);
476 #endif
477 #ifdef CONFIG_PARIDE_KTTI
478 { extern struct pi_protocol ktti;
479 pi_register(&ktti);
481 #endif
482 #ifdef CONFIG_PARIDE_ON20
483 { extern struct pi_protocol on20;
484 pi_register(&on20);
486 #endif
487 #ifdef CONFIG_PARIDE_ON26
488 { extern struct pi_protocol on26;
489 pi_register(&on26);
491 #endif
493 #ifdef CONFIG_PARIDE_PD
494 { extern int pd_init(void);
495 pd_init();
497 #endif
498 #ifdef CONFIG_PARIDE_PCD
499 { extern int pcd_init(void);
500 pcd_init();
502 #endif
503 #ifdef CONFIG_PARIDE_PF
504 { extern int pf_init(void);
505 pf_init();
507 #endif
508 #ifdef CONFIG_PARIDE_PT
509 { extern int pt_init(void);
510 pt_init();
512 #endif
513 #ifdef CONFIG_PARIDE_PG
514 { extern int pg_init(void);
515 pg_init();
517 #endif
520 #endif
522 /* end of paride.c */