Import 2.1.118
[davej-history.git] / drivers / block / paride / paride.c
blob068deffd6edf3fa979d33cf0d4eb3abc9cda49bc
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
18 #define PI_VERSION "1.03"
20 #include <linux/module.h>
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/ioport.h>
25 #include <linux/string.h>
26 #include <asm/spinlock.h>
28 #ifdef CONFIG_PARPORT_MODULE
29 #define CONFIG_PARPORT
30 #endif
32 #ifdef CONFIG_PARPORT
33 #include <linux/parport.h>
34 #endif
36 #include "paride.h"
38 #define MAX_PROTOS 32
40 static struct pi_protocol *protocols[MAX_PROTOS];
42 spinlock_t pi_spinlock = SPIN_LOCK_UNLOCKED;
44 void pi_write_regr( PIA *pi, int cont, int regr, int val)
46 { pi->proto->write_regr(pi,cont,regr,val);
49 int pi_read_regr( PIA *pi, int cont, int regr)
51 { return pi->proto->read_regr(pi,cont,regr);
54 void pi_write_block( PIA *pi, char * buf, int count)
56 { pi->proto->write_block(pi,buf,count);
59 void pi_read_block( PIA *pi, char * buf, int count)
61 { pi->proto->read_block(pi,buf,count);
64 #ifdef CONFIG_PARPORT
66 static void pi_wake_up( void *p)
68 { PIA *pi = (PIA *) p;
69 long flags;
70 void (*cont)(void) = NULL;
72 spin_lock_irqsave(&pi_spinlock,flags);
74 if (pi->claim_cont && !parport_claim(pi->pardev)) {
75 cont = pi->claim_cont;
76 pi->claim_cont = NULL;
77 pi->claimed = 1;
80 spin_unlock_irqrestore(&pi_spinlock,flags);
82 wake_up(&(pi->parq));
84 if (cont) cont();
87 #endif
89 void pi_do_claimed( PIA *pi, void(*cont)(void))
91 #ifdef CONFIG_PARPORT
93 { long flags;
95 spin_lock_irqsave(&pi_spinlock,flags);
97 if (!pi->pardev || !parport_claim(pi->pardev)) {
98 pi->claimed = 1;
99 spin_unlock_irqrestore(&pi_spinlock,flags);
100 cont();
101 } else {
102 pi->claim_cont = cont;
103 spin_unlock_irqrestore(&pi_spinlock,flags);
107 #else
109 { cont();
112 #endif
114 static void pi_claim( PIA *pi)
116 { if (pi->claimed) return;
117 pi->claimed = 1;
118 #ifdef CONFIG_PARPORT
119 if (pi->pardev)
120 while (parport_claim((struct pardevice *)(pi->pardev)))
121 sleep_on(&(pi->parq));
122 #endif
125 static void pi_unclaim( PIA *pi)
127 { pi->claimed = 0;
128 #ifdef CONFIG_PARPORT
129 if (pi->pardev) parport_release((struct pardevice *)(pi->pardev));
130 #endif
133 void pi_connect( PIA *pi)
135 { pi_claim(pi);
136 pi->proto->connect(pi);
139 void pi_disconnect( PIA *pi)
141 { pi->proto->disconnect(pi);
142 pi_unclaim(pi);
145 static void pi_unregister_parport( PIA *pi)
148 #ifdef CONFIG_PARPORT
149 if (pi->pardev) {
150 parport_unregister_device((struct pardevice *)(pi->pardev));
151 pi->pardev = NULL;
153 #endif
156 void pi_release( PIA *pi)
158 { pi_unregister_parport(pi);
159 if ((!pi->pardev)&&(pi->reserved))
160 release_region(pi->port,pi->reserved);
161 pi->proto->release_proto(pi);
164 #define WR(r,v) pi_write_regr(pi,0,r,v)
165 #define RR(r) (pi_read_regr(pi,0,r))
167 static int pi_test_proto( PIA *pi, char * scratch, int verbose )
169 { int j, k;
170 int e[2] = {0,0};
172 if (pi->proto->test_proto) {
173 pi_claim(pi);
174 j = pi->proto->test_proto(pi,scratch,verbose);
175 pi_unclaim(pi);
176 return j;
179 pi_connect(pi);
181 for (j=0;j<2;j++) {
182 WR(6,0xa0+j*0x10);
183 for (k=0;k<256;k++) {
184 WR(2,k^0xaa);
185 WR(3,k^0x55);
186 if (RR(2) != (k^0xaa)) e[j]++;
190 pi_disconnect(pi);
192 if (verbose)
193 printk("%s: %s: port 0x%x, mode %d, test=(%d,%d)\n",
194 pi->device,pi->proto->name,pi->port,
195 pi->mode,e[0],e[1]);
197 return (e[0] && e[1]); /* not here if both > 0 */
200 int pi_register( PIP *pr)
202 { int k;
204 for (k=0;k<MAX_PROTOS;k++)
205 if (protocols[k] && !strcmp(pr->name,protocols[k]->name)) {
206 printk("paride: %s protocol already registered\n",pr->name);
207 return 0;
209 k = 0;
210 while((k<MAX_PROTOS) && (protocols[k])) k++;
211 if (k == MAX_PROTOS) {
212 printk("paride: protocol table full\n");
213 return 0;
215 MOD_INC_USE_COUNT;
216 protocols[k] = pr;
217 pr->index = k;
218 printk("paride: %s registered as protocol %d\n",pr->name,k);
219 return 1;
222 void pi_unregister( PIP *pr)
224 { if (!pr) return;
225 if (protocols[pr->index] != pr) {
226 printk("paride: %s not registered\n",pr->name);
227 return;
229 protocols[pr->index] = 0;
230 MOD_DEC_USE_COUNT;
233 static void pi_register_parport( PIA *pi, int verbose)
236 #ifdef CONFIG_PARPORT
238 struct parport *pp;
240 pp = parport_enumerate();
242 while((pp)&&(pp->base != pi->port)) pp = pp->next;
244 if (!pp) return;
246 pi->pardev = (void *) parport_register_device(
247 pp,pi->device,NULL,pi_wake_up,NULL,0,(void *)pi);
249 pi->parq = NULL;
251 if (verbose) printk("%s: 0x%x is %s\n",pi->device,pi->port,pp->name);
253 pi->parname = (char *)pp->name;
255 #endif
258 static int pi_probe_mode( PIA *pi, int max, char * scratch, int verbose)
260 { int best, range;
262 if (pi->mode != -1) {
263 if (pi->mode >= max) return 0;
264 range = 3;
265 if (pi->mode >= pi->proto->epp_first) range = 8;
266 if ((range == 8) && (pi->port % 8)) return 0;
267 if ((!pi->pardev) && check_region(pi->port,range)) return 0;
268 pi->reserved = range;
269 return (!pi_test_proto(pi,scratch,verbose));
271 best = -1;
272 for(pi->mode=0;pi->mode<max;pi->mode++) {
273 range = 3;
274 if (pi->mode >= pi->proto->epp_first) range = 8;
275 if ((range == 8) && (pi->port % 8)) break;
276 if ((!pi->pardev) && check_region(pi->port,range)) break;
277 pi->reserved = range;
278 if (!pi_test_proto(pi,scratch,verbose)) best = pi->mode;
280 pi->mode = best;
281 return (best > -1);
284 static int pi_probe_unit( PIA *pi, int unit, char * scratch, int verbose)
286 { int max,s,e;
288 s = unit; e = s+1;
290 if (s == -1) {
291 s = 0;
292 e = pi->proto->max_units;
295 pi_register_parport(pi,verbose);
297 if ((!pi->pardev) && check_region(pi->port,3)) return 0;
299 if (pi->proto->test_port) {
300 pi_claim(pi);
301 max = pi->proto->test_port(pi);
302 pi_unclaim(pi);
304 else max = pi->proto->max_mode;
306 if (pi->proto->probe_unit) {
307 pi_claim(pi);
308 for (pi->unit=s;pi->unit<e;pi->unit++)
309 if (pi->proto->probe_unit(pi)) {
310 pi_unclaim(pi);
311 if (pi_probe_mode(pi,max,scratch,verbose)) return 1;
312 pi_unregister_parport(pi);
313 return 0;
315 pi_unclaim(pi);
316 pi_unregister_parport(pi);
317 return 0;
320 if (!pi_probe_mode(pi,max,scratch,verbose)) {
321 pi_unregister_parport(pi);
322 return 0;
324 return 1;
328 int pi_init(PIA *pi, int autoprobe, int port, int mode,
329 int unit, int protocol, int delay, char * scratch,
330 int devtype, int verbose, char *device )
332 { int p,k,s,e;
333 int lpts[7] = {0x3bc,0x378,0x278,0x268,0x27c,0x26c,0};
335 s = protocol; e = s+1;
337 if (autoprobe) {
338 s = 0;
339 e = MAX_PROTOS;
340 } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
341 (!protocols[s]) || (unit < 0) ||
342 (unit >= protocols[s]->max_units)) {
343 printk("%s: Invalid parameters\n",device);
344 return 0;
347 for (p=s;p<e;p++) {
348 if (protocols[p]) {
349 pi->proto = protocols[p];
350 pi->private = 0;
351 pi->proto->init_proto(pi);
352 if (delay == -1) pi->delay = pi->proto->default_delay;
353 else pi->delay = delay;
354 pi->devtype = devtype;
355 pi->device = device;
357 pi->parname = NULL;
358 pi->pardev = NULL;
359 pi->parq = NULL;
360 pi->claimed = 0;
361 pi->claim_cont = NULL;
363 pi->mode = mode;
364 if (port != -1) {
365 pi->port = port;
366 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
367 pi->port = 0;
368 } else {
369 k = 0;
370 while ((pi->port = lpts[k++]))
371 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
372 if (pi->port) break;
374 pi->proto->release_proto(pi);
378 if (!pi->port) {
379 if (autoprobe) printk("%s: Autoprobe failed\n",device);
380 else printk("%s: Adapter not found\n",device);
381 return 0;
384 if (!pi->pardev)
385 request_region(pi->port,pi->reserved,pi->device);
387 if (pi->parname)
388 printk("%s: Sharing %s at 0x%x\n",pi->device,
389 pi->parname,pi->port);
391 pi->proto->log_adapter(pi,scratch,verbose);
393 return 1;
396 #ifdef MODULE
398 int init_module(void)
400 { int k;
402 for (k=0;k<MAX_PROTOS;k++) protocols[k] = 0;
403 printk("paride: version %s installed\n",PI_VERSION);
404 return 0;
407 void cleanup_module(void)
412 #else
414 void paride_init( void )
418 #ifdef CONFIG_PARIDE_ATEN
419 { extern struct pi_protocol aten;
420 pi_register(&aten);
422 #endif
423 #ifdef CONFIG_PARIDE_BPCK
424 { extern struct pi_protocol bpck;
425 pi_register(&bpck);
427 #endif
428 #ifdef CONFIG_PARIDE_COMM
429 { extern struct pi_protocol comm;
430 pi_register(&comm);
432 #endif
433 #ifdef CONFIG_PARIDE_DSTR
434 { extern struct pi_protocol dstr;
435 pi_register(&dstr);
437 #endif
438 #ifdef CONFIG_PARIDE_EPAT
439 { extern struct pi_protocol epat;
440 pi_register(&epat);
442 #endif
443 #ifdef CONFIG_PARIDE_EPIA
444 { extern struct pi_protocol epia;
445 pi_register(&epia);
447 #endif
448 #ifdef CONFIG_PARIDE_FRPW
449 { extern struct pi_protocol frpw;
450 pi_register(&frpw);
452 #endif
453 #ifdef CONFIG_PARIDE_FIT2
454 { extern struct pi_protocol fit2;
455 pi_register(&fit2);
457 #endif
458 #ifdef CONFIG_PARIDE_FIT3
459 { extern struct pi_protocol fit3;
460 pi_register(&fit3);
462 #endif
463 #ifdef CONFIG_PARIDE_KBIC
464 { extern struct pi_protocol k951;
465 extern struct pi_protocol k971;
466 pi_register(&k951);
467 pi_register(&k971);
469 #endif
470 #ifdef CONFIG_PARIDE_KTTI
471 { extern struct pi_protocol ktti;
472 pi_register(&ktti);
474 #endif
475 #ifdef CONFIG_PARIDE_ON20
476 { extern struct pi_protocol on20;
477 pi_register(&on20);
479 #endif
480 #ifdef CONFIG_PARIDE_ON26
481 { extern struct pi_protocol on26;
482 pi_register(&on26);
484 #endif
486 #ifdef CONFIG_PARIDE_PD
487 { extern int pd_init(void);
488 pd_init();
490 #endif
491 #ifdef CONFIG_PARIDE_PCD
492 { extern int pcd_init(void);
493 pcd_init();
495 #endif
496 #ifdef CONFIG_PARIDE_PF
497 { extern int pf_init(void);
498 pf_init();
500 #endif
501 #ifdef CONFIG_PARIDE_PT
502 { extern int pt_init(void);
503 pt_init();
505 #endif
506 #ifdef CONFIG_PARIDE_PG
507 { extern int pg_init(void);
508 pg_init();
510 #endif
513 #endif
515 /* end of paride.c */