[PATCH] m68knommu: remove FP conditionals in ucontext struct
[pv_ops_mirror.git] / drivers / block / paride / paride.c
blob4b258f7836f32f0b99b2f0607e3986a7bdebcbd1
1 /*
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.
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
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/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>
31 #include <linux/sched.h> /* TASK_* */
33 #ifdef CONFIG_PARPORT_MODULE
34 #define CONFIG_PARPORT
35 #endif
37 #ifdef CONFIG_PARPORT
38 #include <linux/parport.h>
39 #endif
41 #include "paride.h"
43 MODULE_LICENSE("GPL");
45 #define MAX_PROTOS 32
47 static struct pi_protocol *protocols[MAX_PROTOS];
49 static DEFINE_SPINLOCK(pi_spinlock);
51 void pi_write_regr(PIA * pi, int cont, int regr, int val)
53 pi->proto->write_regr(pi, cont, regr, val);
56 EXPORT_SYMBOL(pi_write_regr);
58 int pi_read_regr(PIA * pi, int cont, int regr)
60 return pi->proto->read_regr(pi, cont, regr);
63 EXPORT_SYMBOL(pi_read_regr);
65 void pi_write_block(PIA * pi, char *buf, int count)
67 pi->proto->write_block(pi, buf, count);
70 EXPORT_SYMBOL(pi_write_block);
72 void pi_read_block(PIA * pi, char *buf, int count)
74 pi->proto->read_block(pi, buf, count);
77 EXPORT_SYMBOL(pi_read_block);
79 #ifdef CONFIG_PARPORT
81 static void pi_wake_up(void *p)
83 PIA *pi = (PIA *) p;
84 unsigned long flags;
85 void (*cont) (void) = NULL;
87 spin_lock_irqsave(&pi_spinlock, flags);
89 if (pi->claim_cont && !parport_claim(pi->pardev)) {
90 cont = pi->claim_cont;
91 pi->claim_cont = NULL;
92 pi->claimed = 1;
95 spin_unlock_irqrestore(&pi_spinlock, flags);
97 wake_up(&(pi->parq));
99 if (cont)
100 cont();
103 #endif
105 int pi_schedule_claimed(PIA * pi, void (*cont) (void))
107 #ifdef CONFIG_PARPORT
108 unsigned long flags;
110 spin_lock_irqsave(&pi_spinlock, flags);
111 if (pi->pardev && parport_claim(pi->pardev)) {
112 pi->claim_cont = cont;
113 spin_unlock_irqrestore(&pi_spinlock, flags);
114 return 0;
116 pi->claimed = 1;
117 spin_unlock_irqrestore(&pi_spinlock, flags);
118 #endif
119 return 1;
121 EXPORT_SYMBOL(pi_schedule_claimed);
123 void pi_do_claimed(PIA * pi, void (*cont) (void))
125 if (pi_schedule_claimed(pi, cont))
126 cont();
129 EXPORT_SYMBOL(pi_do_claimed);
131 static void pi_claim(PIA * pi)
133 if (pi->claimed)
134 return;
135 pi->claimed = 1;
136 #ifdef CONFIG_PARPORT
137 if (pi->pardev)
138 wait_event(pi->parq,
139 !parport_claim((struct pardevice *) pi->pardev));
140 #endif
143 static void pi_unclaim(PIA * pi)
145 pi->claimed = 0;
146 #ifdef CONFIG_PARPORT
147 if (pi->pardev)
148 parport_release((struct pardevice *) (pi->pardev));
149 #endif
152 void pi_connect(PIA * pi)
154 pi_claim(pi);
155 pi->proto->connect(pi);
158 EXPORT_SYMBOL(pi_connect);
160 void pi_disconnect(PIA * pi)
162 pi->proto->disconnect(pi);
163 pi_unclaim(pi);
166 EXPORT_SYMBOL(pi_disconnect);
168 static void pi_unregister_parport(PIA * pi)
170 #ifdef CONFIG_PARPORT
171 if (pi->pardev) {
172 parport_unregister_device((struct pardevice *) (pi->pardev));
173 pi->pardev = NULL;
175 #endif
178 void pi_release(PIA * pi)
180 pi_unregister_parport(pi);
181 #ifndef CONFIG_PARPORT
182 if (pi->reserved)
183 release_region(pi->port, pi->reserved);
184 #endif /* !CONFIG_PARPORT */
185 if (pi->proto->release_proto)
186 pi->proto->release_proto(pi);
187 module_put(pi->proto->owner);
190 EXPORT_SYMBOL(pi_release);
192 static int default_test_proto(PIA * pi, char *scratch, int verbose)
194 int j, k;
195 int e[2] = { 0, 0 };
197 pi->proto->connect(pi);
199 for (j = 0; j < 2; j++) {
200 pi_write_regr(pi, 0, 6, 0xa0 + j * 0x10);
201 for (k = 0; k < 256; k++) {
202 pi_write_regr(pi, 0, 2, k ^ 0xaa);
203 pi_write_regr(pi, 0, 3, k ^ 0x55);
204 if (pi_read_regr(pi, 0, 2) != (k ^ 0xaa))
205 e[j]++;
208 pi->proto->disconnect(pi);
210 if (verbose)
211 printk("%s: %s: port 0x%x, mode %d, test=(%d,%d)\n",
212 pi->device, pi->proto->name, pi->port,
213 pi->mode, e[0], e[1]);
215 return (e[0] && e[1]); /* not here if both > 0 */
218 static int pi_test_proto(PIA * pi, char *scratch, int verbose)
220 int res;
222 pi_claim(pi);
223 if (pi->proto->test_proto)
224 res = pi->proto->test_proto(pi, scratch, verbose);
225 else
226 res = default_test_proto(pi, scratch, verbose);
227 pi_unclaim(pi);
229 return res;
232 int pi_register(PIP * pr)
234 int k;
236 for (k = 0; k < MAX_PROTOS; k++)
237 if (protocols[k] && !strcmp(pr->name, protocols[k]->name)) {
238 printk("paride: %s protocol already registered\n",
239 pr->name);
240 return 0;
242 k = 0;
243 while ((k < MAX_PROTOS) && (protocols[k]))
244 k++;
245 if (k == MAX_PROTOS) {
246 printk("paride: protocol table full\n");
247 return 0;
249 protocols[k] = pr;
250 pr->index = k;
251 printk("paride: %s registered as protocol %d\n", pr->name, k);
252 return 1;
255 EXPORT_SYMBOL(pi_register);
257 void pi_unregister(PIP * pr)
259 if (!pr)
260 return;
261 if (protocols[pr->index] != pr) {
262 printk("paride: %s not registered\n", pr->name);
263 return;
265 protocols[pr->index] = NULL;
268 EXPORT_SYMBOL(pi_unregister);
270 static int pi_register_parport(PIA * pi, int verbose)
272 #ifdef CONFIG_PARPORT
274 struct parport *port;
276 port = parport_find_base(pi->port);
277 if (!port)
278 return 0;
280 pi->pardev = parport_register_device(port,
281 pi->device, NULL,
282 pi_wake_up, NULL, 0, (void *) pi);
283 parport_put_port(port);
284 if (!pi->pardev)
285 return 0;
287 init_waitqueue_head(&pi->parq);
289 if (verbose)
290 printk("%s: 0x%x is %s\n", pi->device, pi->port, port->name);
292 pi->parname = (char *) port->name;
293 #endif
295 return 1;
298 static int pi_probe_mode(PIA * pi, int max, char *scratch, int verbose)
300 int best, range;
302 if (pi->mode != -1) {
303 if (pi->mode >= max)
304 return 0;
305 range = 3;
306 if (pi->mode >= pi->proto->epp_first)
307 range = 8;
308 if ((range == 8) && (pi->port % 8))
309 return 0;
310 pi->reserved = range;
311 return (!pi_test_proto(pi, scratch, verbose));
313 best = -1;
314 for (pi->mode = 0; pi->mode < max; pi->mode++) {
315 range = 3;
316 if (pi->mode >= pi->proto->epp_first)
317 range = 8;
318 if ((range == 8) && (pi->port % 8))
319 break;
320 pi->reserved = range;
321 if (!pi_test_proto(pi, scratch, verbose))
322 best = pi->mode;
324 pi->mode = best;
325 return (best > -1);
328 static int pi_probe_unit(PIA * pi, int unit, char *scratch, int verbose)
330 int max, s, e;
332 s = unit;
333 e = s + 1;
335 if (s == -1) {
336 s = 0;
337 e = pi->proto->max_units;
340 if (!pi_register_parport(pi, verbose))
341 return 0;
343 if (pi->proto->test_port) {
344 pi_claim(pi);
345 max = pi->proto->test_port(pi);
346 pi_unclaim(pi);
347 } else
348 max = pi->proto->max_mode;
350 if (pi->proto->probe_unit) {
351 pi_claim(pi);
352 for (pi->unit = s; pi->unit < e; pi->unit++)
353 if (pi->proto->probe_unit(pi)) {
354 pi_unclaim(pi);
355 if (pi_probe_mode(pi, max, scratch, verbose))
356 return 1;
357 pi_unregister_parport(pi);
358 return 0;
360 pi_unclaim(pi);
361 pi_unregister_parport(pi);
362 return 0;
365 if (!pi_probe_mode(pi, max, scratch, verbose)) {
366 pi_unregister_parport(pi);
367 return 0;
369 return 1;
373 int pi_init(PIA * pi, int autoprobe, int port, int mode,
374 int unit, int protocol, int delay, char *scratch,
375 int devtype, int verbose, char *device)
377 int p, k, s, e;
378 int lpts[7] = { 0x3bc, 0x378, 0x278, 0x268, 0x27c, 0x26c, 0 };
380 s = protocol;
381 e = s + 1;
383 if (!protocols[0])
384 request_module("paride_protocol");
386 if (autoprobe) {
387 s = 0;
388 e = MAX_PROTOS;
389 } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
390 (!protocols[s]) || (unit < 0) ||
391 (unit >= protocols[s]->max_units)) {
392 printk("%s: Invalid parameters\n", device);
393 return 0;
396 for (p = s; p < e; p++) {
397 struct pi_protocol *proto = protocols[p];
398 if (!proto)
399 continue;
400 /* still racy */
401 if (!try_module_get(proto->owner))
402 continue;
403 pi->proto = proto;
404 pi->private = 0;
405 if (proto->init_proto && proto->init_proto(pi) < 0) {
406 pi->proto = NULL;
407 module_put(proto->owner);
408 continue;
410 if (delay == -1)
411 pi->delay = pi->proto->default_delay;
412 else
413 pi->delay = delay;
414 pi->devtype = devtype;
415 pi->device = device;
417 pi->parname = NULL;
418 pi->pardev = NULL;
419 init_waitqueue_head(&pi->parq);
420 pi->claimed = 0;
421 pi->claim_cont = NULL;
423 pi->mode = mode;
424 if (port != -1) {
425 pi->port = port;
426 if (pi_probe_unit(pi, unit, scratch, verbose))
427 break;
428 pi->port = 0;
429 } else {
430 k = 0;
431 while ((pi->port = lpts[k++]))
432 if (pi_probe_unit
433 (pi, unit, scratch, verbose))
434 break;
435 if (pi->port)
436 break;
438 if (pi->proto->release_proto)
439 pi->proto->release_proto(pi);
440 module_put(proto->owner);
443 if (!pi->port) {
444 if (autoprobe)
445 printk("%s: Autoprobe failed\n", device);
446 else
447 printk("%s: Adapter not found\n", device);
448 return 0;
450 #ifndef CONFIG_PARPORT
451 if (!request_region(pi->port, pi->reserved, pi->device)) {
452 printk(KERN_WARNING "paride: Unable to request region 0x%x\n",
453 pi->port);
454 return 0;
456 #endif /* !CONFIG_PARPORT */
458 if (pi->parname)
459 printk("%s: Sharing %s at 0x%x\n", pi->device,
460 pi->parname, pi->port);
462 pi->proto->log_adapter(pi, scratch, verbose);
464 return 1;
467 EXPORT_SYMBOL(pi_init);