commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / block / paride / paride.c
1 /* 
2         paride.c  (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                               Under the terms of the GNU General Public License.
4
5         This is the base module for the family of device drivers
6         that support parallel port IDE devices.  
7
8 */
9
10 /* Changes:
11
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
19 */
20
21 #define PI_VERSION      "1.06"
22
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>
32
33 #ifdef CONFIG_PARPORT_MODULE
34 #define CONFIG_PARPORT
35 #endif
36
37 #ifdef CONFIG_PARPORT
38 #include <linux/parport.h>
39 #endif
40
41 #include "paride.h"
42
43 MODULE_LICENSE("GPL");
44
45 #define MAX_PROTOS      32
46
47 static struct pi_protocol *protocols[MAX_PROTOS];
48
49 static spinlock_t pi_spinlock = SPIN_LOCK_UNLOCKED;
50
51 void pi_write_regr(PIA * pi, int cont, int regr, int val)
52 {
53         pi->proto->write_regr(pi, cont, regr, val);
54 }
55
56 EXPORT_SYMBOL(pi_write_regr);
57
58 int pi_read_regr(PIA * pi, int cont, int regr)
59 {
60         return pi->proto->read_regr(pi, cont, regr);
61 }
62
63 EXPORT_SYMBOL(pi_read_regr);
64
65 void pi_write_block(PIA * pi, char *buf, int count)
66 {
67         pi->proto->write_block(pi, buf, count);
68 }
69
70 EXPORT_SYMBOL(pi_write_block);
71
72 void pi_read_block(PIA * pi, char *buf, int count)
73 {
74         pi->proto->read_block(pi, buf, count);
75 }
76
77 EXPORT_SYMBOL(pi_read_block);
78
79 #ifdef CONFIG_PARPORT
80
81 static void pi_wake_up(void *p)
82 {
83         PIA *pi = (PIA *) p;
84         unsigned long flags;
85         void (*cont) (void) = NULL;
86
87         spin_lock_irqsave(&pi_spinlock, flags);
88
89         if (pi->claim_cont && !parport_claim(pi->pardev)) {
90                 cont = pi->claim_cont;
91                 pi->claim_cont = NULL;
92                 pi->claimed = 1;
93         }
94
95         spin_unlock_irqrestore(&pi_spinlock, flags);
96
97         wake_up(&(pi->parq));
98
99         if (cont)
100                 cont();
101 }
102
103 #endif
104
105 void pi_do_claimed(PIA * pi, void (*cont) (void))
106 {
107 #ifdef CONFIG_PARPORT
108         unsigned long flags;
109
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;
115         }
116         pi->claimed = 1;
117         spin_unlock_irqrestore(&pi_spinlock, flags);
118 #endif
119         cont();
120 }
121
122 EXPORT_SYMBOL(pi_do_claimed);
123
124 static void pi_claim(PIA * pi)
125 {
126         if (pi->claimed)
127                 return;
128         pi->claimed = 1;
129 #ifdef CONFIG_PARPORT
130         if (pi->pardev)
131                 wait_event(pi->parq,
132                            !parport_claim((struct pardevice *) pi->pardev));
133 #endif
134 }
135
136 static void pi_unclaim(PIA * pi)
137 {
138         pi->claimed = 0;
139 #ifdef CONFIG_PARPORT
140         if (pi->pardev)
141                 parport_release((struct pardevice *) (pi->pardev));
142 #endif
143 }
144
145 void pi_connect(PIA * pi)
146 {
147         pi_claim(pi);
148         pi->proto->connect(pi);
149 }
150
151 EXPORT_SYMBOL(pi_connect);
152
153 void pi_disconnect(PIA * pi)
154 {
155         pi->proto->disconnect(pi);
156         pi_unclaim(pi);
157 }
158
159 EXPORT_SYMBOL(pi_disconnect);
160
161 static void pi_unregister_parport(PIA * pi)
162 {
163 #ifdef CONFIG_PARPORT
164         if (pi->pardev) {
165                 parport_unregister_device((struct pardevice *) (pi->pardev));
166                 pi->pardev = NULL;
167         }
168 #endif
169 }
170
171 void pi_release(PIA * pi)
172 {
173         pi_unregister_parport(pi);
174 #ifndef CONFIG_PARPORT
175         if (pi->reserved)
176                 release_region(pi->port, pi->reserved);
177 #endif                          /* !CONFIG_PARPORT */
178         if (pi->proto->release_proto)
179                 pi->proto->release_proto(pi);
180         module_put(pi->proto->owner);
181 }
182
183 EXPORT_SYMBOL(pi_release);
184
185 static int default_test_proto(PIA * pi, char *scratch, int verbose)
186 {
187         int j, k;
188         int e[2] = { 0, 0 };
189
190         pi->proto->connect(pi);
191
192         for (j = 0; j < 2; j++) {
193                 pi_write_regr(pi, 0, 6, 0xa0 + j * 0x10);
194                 for (k = 0; k < 256; k++) {
195                         pi_write_regr(pi, 0, 2, k ^ 0xaa);
196                         pi_write_regr(pi, 0, 3, k ^ 0x55);
197                         if (pi_read_regr(pi, 0, 2) != (k ^ 0xaa))
198                                 e[j]++;
199                 }
200         }
201         pi->proto->disconnect(pi);
202
203         if (verbose)
204                 printk("%s: %s: port 0x%x, mode  %d, test=(%d,%d)\n",
205                        pi->device, pi->proto->name, pi->port,
206                        pi->mode, e[0], e[1]);
207
208         return (e[0] && e[1]);  /* not here if both > 0 */
209 }
210
211 static int pi_test_proto(PIA * pi, char *scratch, int verbose)
212 {
213         int res;
214
215         pi_claim(pi);
216         if (pi->proto->test_proto)
217                 res = pi->proto->test_proto(pi, scratch, verbose);
218         else
219                 res = default_test_proto(pi, scratch, verbose);
220         pi_unclaim(pi);
221
222         return res;
223 }
224
225 int pi_register(PIP * pr)
226 {
227         int k;
228
229         for (k = 0; k < MAX_PROTOS; k++)
230                 if (protocols[k] && !strcmp(pr->name, protocols[k]->name)) {
231                         printk("paride: %s protocol already registered\n",
232                                pr->name);
233                         return 0;
234                 }
235         k = 0;
236         while ((k < MAX_PROTOS) && (protocols[k]))
237                 k++;
238         if (k == MAX_PROTOS) {
239                 printk("paride: protocol table full\n");
240                 return 0;
241         }
242         protocols[k] = pr;
243         pr->index = k;
244         printk("paride: %s registered as protocol %d\n", pr->name, k);
245         return 1;
246 }
247
248 EXPORT_SYMBOL(pi_register);
249
250 void pi_unregister(PIP * pr)
251 {
252         if (!pr)
253                 return;
254         if (protocols[pr->index] != pr) {
255                 printk("paride: %s not registered\n", pr->name);
256                 return;
257         }
258         protocols[pr->index] = 0;
259 }
260
261 EXPORT_SYMBOL(pi_unregister);
262
263 static int pi_register_parport(PIA * pi, int verbose)
264 {
265 #ifdef CONFIG_PARPORT
266
267         struct parport *port;
268
269         port = parport_find_base(pi->port);
270         if (!port)
271                 return 0;
272
273         pi->pardev = parport_register_device(port,
274                                              pi->device, NULL,
275                                              pi_wake_up, NULL, 0, (void *) pi);
276         parport_put_port(port);
277         if (!pi->pardev)
278                 return 0;
279
280         init_waitqueue_head(&pi->parq);
281
282         if (verbose)
283                 printk("%s: 0x%x is %s\n", pi->device, pi->port, port->name);
284
285         pi->parname = (char *) port->name;
286 #endif
287
288         return 1;
289 }
290
291 static int pi_probe_mode(PIA * pi, int max, char *scratch, int verbose)
292 {
293         int best, range;
294
295         if (pi->mode != -1) {
296                 if (pi->mode >= max)
297                         return 0;
298                 range = 3;
299                 if (pi->mode >= pi->proto->epp_first)
300                         range = 8;
301                 if ((range == 8) && (pi->port % 8))
302                         return 0;
303                 pi->reserved = range;
304                 return (!pi_test_proto(pi, scratch, verbose));
305         }
306         best = -1;
307         for (pi->mode = 0; pi->mode < max; pi->mode++) {
308                 range = 3;
309                 if (pi->mode >= pi->proto->epp_first)
310                         range = 8;
311                 if ((range == 8) && (pi->port % 8))
312                         break;
313                 pi->reserved = range;
314                 if (!pi_test_proto(pi, scratch, verbose))
315                         best = pi->mode;
316         }
317         pi->mode = best;
318         return (best > -1);
319 }
320
321 static int pi_probe_unit(PIA * pi, int unit, char *scratch, int verbose)
322 {
323         int max, s, e;
324
325         s = unit;
326         e = s + 1;
327
328         if (s == -1) {
329                 s = 0;
330                 e = pi->proto->max_units;
331         }
332
333         if (!pi_register_parport(pi, verbose))
334                 return 0;
335
336         if (pi->proto->test_port) {
337                 pi_claim(pi);
338                 max = pi->proto->test_port(pi);
339                 pi_unclaim(pi);
340         } else
341                 max = pi->proto->max_mode;
342
343         if (pi->proto->probe_unit) {
344                 pi_claim(pi);
345                 for (pi->unit = s; pi->unit < e; pi->unit++)
346                         if (pi->proto->probe_unit(pi)) {
347                                 pi_unclaim(pi);
348                                 if (pi_probe_mode(pi, max, scratch, verbose))
349                                         return 1;
350                                 pi_unregister_parport(pi);
351                                 return 0;
352                         }
353                 pi_unclaim(pi);
354                 pi_unregister_parport(pi);
355                 return 0;
356         }
357
358         if (!pi_probe_mode(pi, max, scratch, verbose)) {
359                 pi_unregister_parport(pi);
360                 return 0;
361         }
362         return 1;
363
364 }
365
366 int pi_init(PIA * pi, int autoprobe, int port, int mode,
367         int unit, int protocol, int delay, char *scratch,
368         int devtype, int verbose, char *device)
369 {
370         int p, k, s, e;
371         int lpts[7] = { 0x3bc, 0x378, 0x278, 0x268, 0x27c, 0x26c, 0 };
372
373         s = protocol;
374         e = s + 1;
375
376         if (!protocols[0])
377                 request_module("paride_protocol");
378
379         if (autoprobe) {
380                 s = 0;
381                 e = MAX_PROTOS;
382         } else if ((s < 0) || (s >= MAX_PROTOS) || (port <= 0) ||
383                    (!protocols[s]) || (unit < 0) ||
384                    (unit >= protocols[s]->max_units)) {
385                 printk("%s: Invalid parameters\n", device);
386                 return 0;
387         }
388
389         for (p = s; p < e; p++) {
390                 struct pi_protocol *proto = protocols[p];
391                 if (!proto)
392                         continue;
393                 /* still racy */
394                 if (!try_module_get(proto->owner))
395                         continue;
396                 pi->proto = proto;
397                 pi->private = 0;
398                 if (proto->init_proto && proto->init_proto(pi) < 0) {
399                         pi->proto = NULL;
400                         module_put(proto->owner);
401                         continue;
402                 }
403                 if (delay == -1)
404                         pi->delay = pi->proto->default_delay;
405                 else
406                         pi->delay = delay;
407                 pi->devtype = devtype;
408                 pi->device = device;
409
410                 pi->parname = NULL;
411                 pi->pardev = NULL;
412                 init_waitqueue_head(&pi->parq);
413                 pi->claimed = 0;
414                 pi->claim_cont = NULL;
415
416                 pi->mode = mode;
417                 if (port != -1) {
418                         pi->port = port;
419                         if (pi_probe_unit(pi, unit, scratch, verbose))
420                                 break;
421                         pi->port = 0;
422                 } else {
423                         k = 0;
424                         while ((pi->port = lpts[k++]))
425                                 if (pi_probe_unit
426                                     (pi, unit, scratch, verbose))
427                                         break;
428                         if (pi->port)
429                                 break;
430                 }
431                 if (pi->proto->release_proto)
432                         pi->proto->release_proto(pi);
433                 module_put(proto->owner);
434         }
435
436         if (!pi->port) {
437                 if (autoprobe)
438                         printk("%s: Autoprobe failed\n", device);
439                 else
440                         printk("%s: Adapter not found\n", device);
441                 return 0;
442         }
443 #ifndef CONFIG_PARPORT
444         if (!request_region(pi->port, pi->reserved, pi->device)) {
445                 printk(KERN_WARNING "paride: Unable to request region 0x%x\n",
446                        pi->port);
447                 return 0;
448         }
449 #endif                          /* !CONFIG_PARPORT */
450
451         if (pi->parname)
452                 printk("%s: Sharing %s at 0x%x\n", pi->device,
453                        pi->parname, pi->port);
454
455         pi->proto->log_adapter(pi, scratch, verbose);
456
457         return 1;
458 }
459
460 EXPORT_SYMBOL(pi_init);