Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / block / paride / paride.c
blobb36fdab112076c72bf5074be5b8281eac5f0bf5b
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
16 1.05 TMW 2000.06.06 use parport_find_number instead of
17 parport_enumerate
20 #define PI_VERSION "1.05"
22 #include <linux/module.h>
23 #include <linux/config.h>
24 #include <linux/kmod.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/ioport.h>
28 #include <linux/string.h>
29 #include <linux/spinlock.h>
30 #include <linux/wait.h>
32 #ifdef CONFIG_PARPORT_MODULE
33 #define CONFIG_PARPORT
34 #endif
36 #ifdef CONFIG_PARPORT
37 #include <linux/parport.h>
38 #endif
40 #include "paride.h"
42 #define MAX_PROTOS 32
44 static struct pi_protocol *protocols[MAX_PROTOS];
46 spinlock_t pi_spinlock = SPIN_LOCK_UNLOCKED;
48 void pi_write_regr( PIA *pi, int cont, int regr, int val)
50 { pi->proto->write_regr(pi,cont,regr,val);
53 int pi_read_regr( PIA *pi, int cont, int regr)
55 { return pi->proto->read_regr(pi,cont,regr);
58 void pi_write_block( PIA *pi, char * buf, int count)
60 { pi->proto->write_block(pi,buf,count);
63 void pi_read_block( PIA *pi, char * buf, int count)
65 { pi->proto->read_block(pi,buf,count);
68 #ifdef CONFIG_PARPORT
70 static void pi_wake_up( void *p)
72 { PIA *pi = (PIA *) p;
73 long flags;
74 void (*cont)(void) = NULL;
76 spin_lock_irqsave(&pi_spinlock,flags);
78 if (pi->claim_cont && !parport_claim(pi->pardev)) {
79 cont = pi->claim_cont;
80 pi->claim_cont = NULL;
81 pi->claimed = 1;
84 spin_unlock_irqrestore(&pi_spinlock,flags);
86 wake_up(&(pi->parq));
88 if (cont) cont();
91 #endif
93 void pi_do_claimed( PIA *pi, void(*cont)(void))
95 #ifdef CONFIG_PARPORT
97 { long flags;
99 spin_lock_irqsave(&pi_spinlock,flags);
101 if (!pi->pardev || !parport_claim(pi->pardev)) {
102 pi->claimed = 1;
103 spin_unlock_irqrestore(&pi_spinlock,flags);
104 cont();
105 } else {
106 pi->claim_cont = cont;
107 spin_unlock_irqrestore(&pi_spinlock,flags);
111 #else
113 { cont();
116 #endif
118 static void pi_claim( PIA *pi)
120 { if (pi->claimed) return;
121 pi->claimed = 1;
122 #ifdef CONFIG_PARPORT
123 if (pi->pardev)
124 wait_event (pi->parq,
125 !parport_claim ((struct pardevice *)pi->pardev));
126 #endif
129 static void pi_unclaim( PIA *pi)
131 { pi->claimed = 0;
132 #ifdef CONFIG_PARPORT
133 if (pi->pardev) parport_release((struct pardevice *)(pi->pardev));
134 #endif
137 void pi_connect( PIA *pi)
139 { pi_claim(pi);
140 pi->proto->connect(pi);
143 void pi_disconnect( PIA *pi)
145 { pi->proto->disconnect(pi);
146 pi_unclaim(pi);
149 static void pi_unregister_parport( PIA *pi)
152 #ifdef CONFIG_PARPORT
153 if (pi->pardev) {
154 parport_unregister_device((struct pardevice *)(pi->pardev));
155 pi->pardev = NULL;
157 #endif
160 void pi_release( PIA *pi)
162 { pi_unregister_parport(pi);
163 if ((!pi->pardev)&&(pi->reserved))
164 release_region(pi->port,pi->reserved);
165 pi->proto->release_proto(pi);
168 #define WR(r,v) pi_write_regr(pi,0,r,v)
169 #define RR(r) (pi_read_regr(pi,0,r))
171 static int pi_test_proto( PIA *pi, char * scratch, int verbose )
173 { int j, k;
174 int e[2] = {0,0};
176 if (pi->proto->test_proto) {
177 pi_claim(pi);
178 j = pi->proto->test_proto(pi,scratch,verbose);
179 pi_unclaim(pi);
180 return j;
183 pi_connect(pi);
185 for (j=0;j<2;j++) {
186 WR(6,0xa0+j*0x10);
187 for (k=0;k<256;k++) {
188 WR(2,k^0xaa);
189 WR(3,k^0x55);
190 if (RR(2) != (k^0xaa)) e[j]++;
194 pi_disconnect(pi);
196 if (verbose)
197 printk("%s: %s: port 0x%x, mode %d, test=(%d,%d)\n",
198 pi->device,pi->proto->name,pi->port,
199 pi->mode,e[0],e[1]);
201 return (e[0] && e[1]); /* not here if both > 0 */
204 int pi_register( PIP *pr)
206 { int k;
208 for (k=0;k<MAX_PROTOS;k++)
209 if (protocols[k] && !strcmp(pr->name,protocols[k]->name)) {
210 printk("paride: %s protocol already registered\n",pr->name);
211 return 0;
213 k = 0;
214 while((k<MAX_PROTOS) && (protocols[k])) k++;
215 if (k == MAX_PROTOS) {
216 printk("paride: protocol table full\n");
217 return 0;
219 MOD_INC_USE_COUNT;
220 protocols[k] = pr;
221 pr->index = k;
222 printk("paride: %s registered as protocol %d\n",pr->name,k);
223 return 1;
226 void pi_unregister( PIP *pr)
228 { if (!pr) return;
229 if (protocols[pr->index] != pr) {
230 printk("paride: %s not registered\n",pr->name);
231 return;
233 protocols[pr->index] = 0;
234 MOD_DEC_USE_COUNT;
237 static void pi_register_parport( PIA *pi, int verbose)
240 #ifdef CONFIG_PARPORT
242 struct parport *port;
244 port = parport_find_base (pi->port);
245 if (!port) return;
247 pi->pardev = parport_register_device(port,
248 pi->device,NULL,
249 pi_wake_up,NULL,
250 0,(void *)pi);
251 parport_put_port (port);
252 if (!pi->pardev) return;
255 init_waitqueue_head(&pi->parq);
257 if (verbose) printk("%s: 0x%x is %s\n",pi->device,pi->port,
258 port->name);
260 pi->parname = (char *)port->name;
262 #endif
265 static int pi_probe_mode( PIA *pi, int max, char * scratch, int verbose)
267 { int best, range;
269 if (pi->mode != -1) {
270 if (pi->mode >= max) return 0;
271 range = 3;
272 if (pi->mode >= pi->proto->epp_first) range = 8;
273 if ((range == 8) && (pi->port % 8)) return 0;
274 if ((!pi->pardev) && check_region(pi->port,range)) return 0;
275 pi->reserved = range;
276 return (!pi_test_proto(pi,scratch,verbose));
278 best = -1;
279 for(pi->mode=0;pi->mode<max;pi->mode++) {
280 range = 3;
281 if (pi->mode >= pi->proto->epp_first) range = 8;
282 if ((range == 8) && (pi->port % 8)) break;
283 if ((!pi->pardev) && check_region(pi->port,range)) break;
284 pi->reserved = range;
285 if (!pi_test_proto(pi,scratch,verbose)) best = pi->mode;
287 pi->mode = best;
288 return (best > -1);
291 static int pi_probe_unit( PIA *pi, int unit, char * scratch, int verbose)
293 { int max,s,e;
295 s = unit; e = s+1;
297 if (s == -1) {
298 s = 0;
299 e = pi->proto->max_units;
302 pi_register_parport(pi,verbose);
304 if ((!pi->pardev) && check_region(pi->port,3)) return 0;
306 if (pi->proto->test_port) {
307 pi_claim(pi);
308 max = pi->proto->test_port(pi);
309 pi_unclaim(pi);
311 else max = pi->proto->max_mode;
313 if (pi->proto->probe_unit) {
314 pi_claim(pi);
315 for (pi->unit=s;pi->unit<e;pi->unit++)
316 if (pi->proto->probe_unit(pi)) {
317 pi_unclaim(pi);
318 if (pi_probe_mode(pi,max,scratch,verbose)) return 1;
319 pi_unregister_parport(pi);
320 return 0;
322 pi_unclaim(pi);
323 pi_unregister_parport(pi);
324 return 0;
327 if (!pi_probe_mode(pi,max,scratch,verbose)) {
328 pi_unregister_parport(pi);
329 return 0;
331 return 1;
335 int pi_init(PIA *pi, int autoprobe, int port, int mode,
336 int unit, int protocol, int delay, char * scratch,
337 int devtype, int verbose, char *device )
339 { int p,k,s,e;
340 int lpts[7] = {0x3bc,0x378,0x278,0x268,0x27c,0x26c,0};
342 s = protocol; e = s+1;
344 if (!protocols[0])
345 request_module ("paride_protocol");
347 if (autoprobe) {
348 s = 0;
349 e = MAX_PROTOS;
350 } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
351 (!protocols[s]) || (unit < 0) ||
352 (unit >= protocols[s]->max_units)) {
353 printk("%s: Invalid parameters\n",device);
354 return 0;
357 for (p=s;p<e;p++) {
358 if (protocols[p]) {
359 pi->proto = protocols[p];
360 pi->private = 0;
361 pi->proto->init_proto(pi);
362 if (delay == -1) pi->delay = pi->proto->default_delay;
363 else pi->delay = delay;
364 pi->devtype = devtype;
365 pi->device = device;
367 pi->parname = NULL;
368 pi->pardev = NULL;
369 init_waitqueue_head(&pi->parq);
370 pi->claimed = 0;
371 pi->claim_cont = NULL;
373 pi->mode = mode;
374 if (port != -1) {
375 pi->port = port;
376 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
377 pi->port = 0;
378 } else {
379 k = 0;
380 while ((pi->port = lpts[k++]))
381 if (pi_probe_unit(pi,unit,scratch,verbose)) break;
382 if (pi->port) break;
384 pi->proto->release_proto(pi);
388 if (!pi->port) {
389 if (autoprobe) printk("%s: Autoprobe failed\n",device);
390 else printk("%s: Adapter not found\n",device);
391 return 0;
394 if (!pi->pardev)
395 request_region(pi->port,pi->reserved,pi->device);
397 if (pi->parname)
398 printk("%s: Sharing %s at 0x%x\n",pi->device,
399 pi->parname,pi->port);
401 pi->proto->log_adapter(pi,scratch,verbose);
403 return 1;
406 #ifdef MODULE
408 int init_module(void)
410 { int k;
412 for (k=0;k<MAX_PROTOS;k++) protocols[k] = 0;
414 printk("paride: version %s installed\n",PI_VERSION);
415 return 0;
418 void cleanup_module(void)
423 #else
425 void paride_init( void )
429 #ifdef CONFIG_PARIDE_ATEN
430 { extern struct pi_protocol aten;
431 pi_register(&aten);
433 #endif
434 #ifdef CONFIG_PARIDE_BPCK
435 { extern struct pi_protocol bpck;
436 pi_register(&bpck);
438 #endif
439 #ifdef CONFIG_PARIDE_COMM
440 { extern struct pi_protocol comm;
441 pi_register(&comm);
443 #endif
444 #ifdef CONFIG_PARIDE_DSTR
445 { extern struct pi_protocol dstr;
446 pi_register(&dstr);
448 #endif
449 #ifdef CONFIG_PARIDE_EPAT
450 { extern struct pi_protocol epat;
451 pi_register(&epat);
453 #endif
454 #ifdef CONFIG_PARIDE_EPIA
455 { extern struct pi_protocol epia;
456 pi_register(&epia);
458 #endif
459 #ifdef CONFIG_PARIDE_FRPW
460 { extern struct pi_protocol frpw;
461 pi_register(&frpw);
463 #endif
464 #ifdef CONFIG_PARIDE_FRIQ
465 { extern struct pi_protocol friq;
466 pi_register(&friq);
468 #endif
469 #ifdef CONFIG_PARIDE_FIT2
470 { extern struct pi_protocol fit2;
471 pi_register(&fit2);
473 #endif
474 #ifdef CONFIG_PARIDE_FIT3
475 { extern struct pi_protocol fit3;
476 pi_register(&fit3);
478 #endif
479 #ifdef CONFIG_PARIDE_KBIC
480 { extern struct pi_protocol k951;
481 extern struct pi_protocol k971;
482 pi_register(&k951);
483 pi_register(&k971);
485 #endif
486 #ifdef CONFIG_PARIDE_KTTI
487 { extern struct pi_protocol ktti;
488 pi_register(&ktti);
490 #endif
491 #ifdef CONFIG_PARIDE_ON20
492 { extern struct pi_protocol on20;
493 pi_register(&on20);
495 #endif
496 #ifdef CONFIG_PARIDE_ON26
497 { extern struct pi_protocol on26;
498 pi_register(&on26);
500 #endif
502 #ifdef CONFIG_PARIDE_PD
503 { extern int pd_init(void);
504 pd_init();
506 #endif
507 #ifdef CONFIG_PARIDE_PCD
508 { extern int pcd_init(void);
509 pcd_init();
511 #endif
512 #ifdef CONFIG_PARIDE_PF
513 { extern int pf_init(void);
514 pf_init();
516 #endif
517 #ifdef CONFIG_PARIDE_PT
518 { extern int pt_init(void);
519 pt_init();
521 #endif
522 #ifdef CONFIG_PARIDE_PG
523 { extern int pg_init(void);
524 pg_init();
526 #endif
529 #endif
531 /* end of paride.c */