v2.5.0.7 -> v2.5.0.8
[opensuse:kernel.git] / drivers / block / xd.c
1 /*
2  * This file contains the driver for an XT hard disk controller
3  * (at least the DTC 5150X) for Linux.
4  *
5  * Author: Pat Mackinlay, pat@it.com.au
6  * Date: 29/09/92
7  * 
8  * Revised: 01/01/93, ...
9  *
10  * Ref: DTC 5150X Controller Specification (thanks to Kevin Fowler,
11  *   kevinf@agora.rain.com)
12  * Also thanks to: Salvador Abreu, Dave Thaler, Risto Kankkunen and
13  *   Wim Van Dorst.
14  *
15  * Revised: 04/04/94 by Risto Kankkunen
16  *   Moved the detection code from xd_init() to xd_geninit() as it needed
17  *   interrupts enabled and Linus didn't want to enable them in that first
18  *   phase. xd_geninit() is the place to do these kinds of things anyway,
19  *   he says.
20  *
21  * Modularized: 04/10/96 by Todd Fries, tfries@umr.edu
22  *
23  * Revised: 13/12/97 by Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl
24  *   Fixed some problems with disk initialization and module initiation.
25  *   Added support for manual geometry setting (except Seagate controllers)
26  *   in form:
27  *      xd_geo=<cyl_xda>,<head_xda>,<sec_xda>[,<cyl_xdb>,<head_xdb>,<sec_xdb>]
28  *   Recovered DMA access. Abridged messages. Added support for DTC5051CX,
29  *   WD1002-27X & XEBEC controllers. Driver uses now some jumper settings.
30  *   Extended ioctl() support.
31  *
32  * Bugfix: 15/02/01, Paul G. - inform queue layer of tiny xd_maxsect.
33  *
34  */
35
36 #include <linux/module.h>
37 #include <linux/errno.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/fs.h>
41 #include <linux/kernel.h>
42 #include <linux/timer.h>
43 #include <linux/genhd.h>
44 #include <linux/hdreg.h>
45 #include <linux/ioport.h>
46 #include <linux/init.h>
47 #include <linux/devfs_fs_kernel.h>
48
49 #include <asm/system.h>
50 #include <asm/io.h>
51 #include <asm/uaccess.h>
52 #include <asm/dma.h>
53
54 #define MAJOR_NR XT_DISK_MAJOR
55 #include <linux/blk.h>
56 #include <linux/blkpg.h>
57
58 #include "xd.h"
59
60 #define XD_DONT_USE_DMA         0  /* Initial value. may be overriden using
61                                       "nodma" module option */
62 #define XD_INIT_DISK_DELAY      (30*HZ/1000)  /* 30 ms delay during disk initialization */
63
64 /* Above may need to be increased if a problem with the 2nd drive detection
65    (ST11M controller) or resetting a controller (WD) appears */
66
67 XD_INFO xd_info[XD_MAXDRIVES];
68
69 /* If you try this driver and find that your card is not detected by the driver at bootup, you need to add your BIOS
70    signature and details to the following list of signatures. A BIOS signature is a string embedded into the first
71    few bytes of your controller's on-board ROM BIOS. To find out what yours is, use something like MS-DOS's DEBUG
72    command. Run DEBUG, and then you can examine your BIOS signature with:
73
74         d xxxx:0000
75
76    where xxxx is the segment of your controller (like C800 or D000 or something). On the ASCII dump at the right, you should
77    be able to see a string mentioning the manufacturer's copyright etc. Add this string into the table below. The parameters
78    in the table are, in order:
79
80         offset                  ; this is the offset (in bytes) from the start of your ROM where the signature starts
81         signature               ; this is the actual text of the signature
82         xd_?_init_controller    ; this is the controller init routine used by your controller
83         xd_?_init_drive         ; this is the drive init routine used by your controller
84
85    The controllers directly supported at the moment are: DTC 5150x, WD 1004A27X, ST11M/R and override. If your controller is
86    made by the same manufacturer as one of these, try using the same init routines as they do. If that doesn't work, your
87    best bet is to use the "override" routines. These routines use a "portable" method of getting the disk's geometry, and
88    may work with your card. If none of these seem to work, try sending me some email and I'll see what I can do <grin>.
89
90    NOTE: You can now specify your XT controller's parameters from the command line in the form xd=TYPE,IRQ,IO,DMA. The driver
91    should be able to detect your drive's geometry from this info. (eg: xd=0,5,0x320,3 is the "standard"). */
92
93 #include <asm/page.h>
94 #define xd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
95 #define xd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
96 static char *xd_dma_buffer = 0;
97
98 static XD_SIGNATURE xd_sigs[] __initdata = {
99         { 0x0000,"Override geometry handler",NULL,xd_override_init_drive,"n unknown" }, /* Pat Mackinlay, pat@it.com.au */
100         { 0x0008,"[BXD06 (C) DTC 17-MAY-1985]",xd_dtc_init_controller,xd_dtc5150cx_init_drive," DTC 5150CX" }, /* Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl */
101         { 0x000B,"CRD18A   Not an IBM rom. (C) Copyright Data Technology Corp. 05/31/88",xd_dtc_init_controller,xd_dtc_init_drive," DTC 5150X" }, /* Todd Fries, tfries@umr.edu */
102         { 0x000B,"CXD23A Not an IBM ROM (C)Copyright Data Technology Corp 12/03/88",xd_dtc_init_controller,xd_dtc_init_drive," DTC 5150X" }, /* Pat Mackinlay, pat@it.com.au */
103         { 0x0008,"07/15/86(C) Copyright 1986 Western Digital Corp.",xd_wd_init_controller,xd_wd_init_drive," Western Dig. 1002-27X" }, /* Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl */
104         { 0x0008,"06/24/88(C) Copyright 1988 Western Digital Corp.",xd_wd_init_controller,xd_wd_init_drive," Western Dig. WDXT-GEN2" }, /* Dan Newcombe, newcombe@aa.csc.peachnet.edu */
105         { 0x0015,"SEAGATE ST11 BIOS REVISION",xd_seagate_init_controller,xd_seagate_init_drive," Seagate ST11M/R" }, /* Salvador Abreu, spa@fct.unl.pt */
106         { 0x0010,"ST11R BIOS",xd_seagate_init_controller,xd_seagate_init_drive," Seagate ST11M/R" }, /* Risto Kankkunen, risto.kankkunen@cs.helsinki.fi */
107         { 0x0010,"ST11 BIOS v1.7",xd_seagate_init_controller,xd_seagate_init_drive," Seagate ST11R" }, /* Alan Hourihane, alanh@fairlite.demon.co.uk */
108         { 0x1000,"(c)Copyright 1987 SMS",xd_omti_init_controller,xd_omti_init_drive,"n OMTI 5520" }, /* Dirk Melchers, dirk@merlin.nbg.sub.org */
109         { 0x0006,"COPYRIGHT XEBEC (C) 1984",xd_xebec_init_controller,xd_xebec_init_drive," XEBEC" }, /* Andrzej Krzysztofowicz, ankry@mif.pg.gda.pl */
110 };
111
112 static unsigned int xd_bases[] __initdata =
113 {
114         0xC8000, 0xCA000, 0xCC000,
115         0xCE000, 0xD0000, 0xD2000,
116         0xD4000, 0xD6000, 0xD8000,
117         0xDA000, 0xDC000, 0xDE000,
118         0xE0000
119 };
120
121 static struct hd_struct xd_struct[XD_MAXDRIVES << 6];
122 static int xd_sizes[XD_MAXDRIVES << 6], xd_access[XD_MAXDRIVES];
123 static int xd_blocksizes[XD_MAXDRIVES << 6];
124
125 extern struct block_device_operations xd_fops;
126
127 static struct gendisk xd_gendisk = {
128         major:          MAJOR_NR,
129         major_name:     "xd",
130         minor_shift:    6,
131         max_p:          1 << 6,
132         part:           xd_struct,
133         sizes:          xd_sizes,
134         real_devices:   (void *)xd_info,
135         fops:           &xd_fops,
136 };
137
138 static struct block_device_operations xd_fops = {
139         owner:          THIS_MODULE,
140         open:           xd_open,
141         release:        xd_release,
142         ioctl:          xd_ioctl,
143 };
144 static DECLARE_WAIT_QUEUE_HEAD(xd_wait_int);
145 static DECLARE_WAIT_QUEUE_HEAD(xd_wait_open);
146 static u_char xd_valid[XD_MAXDRIVES] = { 0,0 };
147 static u_char xd_drives, xd_irq = 5, xd_dma = 3, xd_maxsectors;
148 static u_char xd_override __initdata = 0, xd_type __initdata = 0;
149 static u_short xd_iobase = 0x320;
150 static int xd_geo[XD_MAXDRIVES*3] __initdata = { 0, };
151
152 static volatile int xdc_busy;
153 static DECLARE_WAIT_QUEUE_HEAD(xdc_wait);
154
155 static struct timer_list xd_timer, xd_watchdog_int;
156
157 static volatile u_char xd_error;
158 static int nodma = XD_DONT_USE_DMA;
159
160 static devfs_handle_t devfs_handle = NULL;
161
162 /* xd_init: register the block device number and set up pointer tables */
163 int __init xd_init (void)
164 {
165         init_timer (&xd_timer); xd_timer.function = xd_wakeup;
166         init_timer (&xd_watchdog_int); xd_watchdog_int.function = xd_watchdog;
167
168         if (devfs_register_blkdev(MAJOR_NR,"xd",&xd_fops)) {
169                 printk("xd: Unable to get major number %d\n",MAJOR_NR);
170                 return -1;
171         }
172         devfs_handle = devfs_mk_dir (NULL, xd_gendisk.major_name, NULL);
173         blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
174         read_ahead[MAJOR_NR] = 8;       /* 8 sector (4kB) read ahead */
175         add_gendisk(&xd_gendisk);
176         xd_geninit();
177
178         return 0;
179 }
180
181 /* xd_detect: scan the possible BIOS ROM locations for the signature strings */
182 static u_char __init xd_detect (u_char *controller, unsigned int *address)
183 {
184         u_char i,j,found = 0;
185
186         if (xd_override)
187         {
188                 *controller = xd_type;
189                 *address = 0;
190                 return(1);
191         }
192
193         for (i = 0; i < (sizeof(xd_bases) / sizeof(xd_bases[0])) && !found; i++)
194                 for (j = 1; j < (sizeof(xd_sigs) / sizeof(xd_sigs[0])) && !found; j++)
195                         if (isa_check_signature(xd_bases[i] + xd_sigs[j].offset,xd_sigs[j].string,strlen(xd_sigs[j].string))) {
196                                 *controller = j;
197                                 xd_type = j;
198                                 *address = xd_bases[i];
199                                 found++;
200                         }
201         return (found);
202 }
203
204 /* xd_geninit: grab the IRQ and DMA channel, initialise the drives */
205 /* and set up the "raw" device entries in the table */
206 static void __init xd_geninit (void)
207 {
208         u_char i,controller;
209         unsigned int address;
210
211         for(i=0;i<(XD_MAXDRIVES << 6);i++) xd_blocksizes[i] = 1024;
212         blksize_size[MAJOR_NR] = xd_blocksizes;
213
214         if (xd_detect(&controller,&address)) {
215
216                 printk("Detected a%s controller (type %d) at address %06x\n",
217                         xd_sigs[controller].name,controller,address);
218                 if (check_region(xd_iobase,4)) {
219                         printk("xd: Ports at 0x%x are not available\n",
220                                 xd_iobase);
221                         return;
222                 }
223                 request_region(xd_iobase,4,"xd");
224                 if (controller)
225                         xd_sigs[controller].init_controller(address);
226                 xd_drives = xd_initdrives(xd_sigs[controller].init_drive);
227                 
228                 printk("Detected %d hard drive%s (using IRQ%d & DMA%d)\n",
229                         xd_drives,xd_drives == 1 ? "" : "s",xd_irq,xd_dma);
230                 for (i = 0; i < xd_drives; i++)
231                         printk(" xd%c: CHS=%d/%d/%d\n",'a'+i,
232                                 xd_info[i].cylinders,xd_info[i].heads,
233                                 xd_info[i].sectors);
234
235         }
236         if (xd_drives) {
237                 if (!request_irq(xd_irq,xd_interrupt_handler, 0, "XT hard disk", NULL)) {
238                         if (request_dma(xd_dma,"xd")) {
239                                 printk("xd: unable to get DMA%d\n",xd_dma);
240                                 free_irq(xd_irq, NULL);
241                         }
242                 }
243                 else
244                         printk("xd: unable to get IRQ%d\n",xd_irq);
245         }
246
247         /* xd_maxsectors depends on controller - so set after detection */
248         blk_queue_max_sectors(BLK_DEFAULT_QUEUE(MAJOR_NR), xd_maxsectors);
249
250         for (i = 0; i < xd_drives; i++) {
251                 xd_valid[i] = 1;
252                 register_disk(&xd_gendisk, MKDEV(MAJOR_NR,i<<6), 1<<6, &xd_fops,
253                                 xd_info[i].heads * xd_info[i].cylinders *
254                                 xd_info[i].sectors);
255         }
256
257         xd_gendisk.nr_real = xd_drives;
258 }
259
260 /* xd_open: open a device */
261 static int xd_open (struct inode *inode,struct file *file)
262 {
263         int dev = DEVICE_NR(inode->i_rdev);
264
265         if (dev < xd_drives) {
266                 while (!xd_valid[dev])
267                         sleep_on(&xd_wait_open);
268
269                 xd_access[dev]++;
270
271                 return (0);
272         }
273
274         return -ENXIO;
275 }
276
277 /* do_xd_request: handle an incoming request */
278 static void do_xd_request (request_queue_t * q)
279 {
280         u_int block,count,retry;
281         int code;
282
283         sti();
284         if (xdc_busy)
285                 return;
286         while (code = 0, !QUEUE_EMPTY) {
287                 INIT_REQUEST;   /* do some checking on the request structure */
288
289                 if (CURRENT_DEV < xd_drives
290                     && CURRENT->sector + CURRENT->nr_sectors
291                          <= xd_struct[MINOR(CURRENT->rq_dev)].nr_sects) {
292                         block = CURRENT->sector;
293                         count = CURRENT->nr_sectors;
294
295                         switch (rq_data_dir(CURRENT)) {
296                                 case READ:
297                                 case WRITE:
298                                         for (retry = 0; (retry < XD_RETRIES) && !code; retry++)
299                                                 code = xd_readwrite(rq_data_dir(CURRENT),CURRENT_DEV,CURRENT->buffer,block,count);
300                                         break;
301                                 default:
302                                         printk("do_xd_request: unknown request\n");
303                                         break;
304                         }
305                 }
306                 end_request(code);      /* wrap up, 0 = fail, 1 = success */
307         }
308 }
309
310 /* xd_ioctl: handle device ioctl's */
311 static int xd_ioctl (struct inode *inode,struct file *file,u_int cmd,u_long arg)
312 {
313         int dev;
314
315         if ((!inode) || !(inode->i_rdev))
316                 return -EINVAL;
317         dev = DEVICE_NR(inode->i_rdev);
318
319         if (dev >= xd_drives) return -EINVAL;
320         switch (cmd) {
321                 case HDIO_GETGEO:
322                 {
323                         struct hd_geometry g;
324                         struct hd_geometry *geometry = (struct hd_geometry *) arg;
325                         if (!geometry) return -EINVAL;
326                         g.heads = xd_info[dev].heads;
327                         g.sectors = xd_info[dev].sectors;
328                         g.cylinders = xd_info[dev].cylinders;
329                         g.start = get_start_sect(inode->i_rdev);
330                         return copy_to_user(geometry, &g, sizeof g) ? -EFAULT : 0;
331                 }
332                 case HDIO_SET_DMA:
333                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
334                         if (xdc_busy) return -EBUSY;
335                         nodma = !arg;
336                         if (nodma && xd_dma_buffer) {
337                                 xd_dma_mem_free((unsigned long)xd_dma_buffer,
338                                                 xd_maxsectors * 0x200);
339                                 xd_dma_buffer = 0;
340                         }
341                         return 0;
342                 case HDIO_GET_DMA:
343                         return put_user(!nodma, (long *) arg);
344                 case HDIO_GET_MULTCOUNT:
345                         return put_user(xd_maxsectors, (long *) arg);
346                 case BLKRRPART:
347                         if (!capable(CAP_SYS_ADMIN)) 
348                                 return -EACCES;
349                         return xd_reread_partitions(inode->i_rdev);
350
351                 case BLKGETSIZE:
352                 case BLKGETSIZE64:
353                 case BLKFLSBUF:
354                 case BLKROSET:
355                 case BLKROGET:
356                 case BLKRASET:
357                 case BLKRAGET:
358                 case BLKPG:
359                         return blk_ioctl(inode->i_rdev, cmd, arg);
360
361                 default:
362                         return -EINVAL;
363         }
364 }
365
366 /* xd_release: release the device */
367 static int xd_release (struct inode *inode, struct file *file)
368 {
369         int target = DEVICE_NR(inode->i_rdev);
370         if (target < xd_drives)
371                 xd_access[target]--;
372         return 0;
373 }
374
375 /* xd_reread_partitions: rereads the partition table from a drive */
376 static int xd_reread_partitions(kdev_t dev)
377 {
378         int target;
379         int res;
380         
381         target = DEVICE_NR(dev);
382
383         cli();
384         xd_valid[target] = (xd_access[target] != 1);
385         sti();
386         if (xd_valid[target])
387                 return -EBUSY;
388
389         res = wipe_partitions(dev);
390         if (!res)
391                 grok_partitions(dev, xd_info[target].heads
392                                 * xd_info[target].cylinders
393                                 * xd_info[target].sectors);
394
395         xd_valid[target] = 1;
396         wake_up(&xd_wait_open);
397
398         return res;
399 }
400
401 /* xd_readwrite: handle a read/write request */
402 static int xd_readwrite (u_char operation,u_char drive,char *buffer,u_int block,u_int count)
403 {
404         u_char cmdblk[6],sense[4];
405         u_short track,cylinder;
406         u_char head,sector,control,mode = PIO_MODE,temp;
407         char **real_buffer;
408         register int i;
409         
410 #ifdef DEBUG_READWRITE
411         printk("xd_readwrite: operation = %s, drive = %d, buffer = 0x%X, block = %d, count = %d\n",operation == READ ? "read" : "write",drive,buffer,block,count);
412 #endif /* DEBUG_READWRITE */
413
414         control = xd_info[drive].control;
415         if (!xd_dma_buffer)
416                 xd_dma_buffer = (char *)xd_dma_mem_alloc(xd_maxsectors * 0x200);
417         while (count) {
418                 temp = count < xd_maxsectors ? count : xd_maxsectors;
419
420                 track = block / xd_info[drive].sectors;
421                 head = track % xd_info[drive].heads;
422                 cylinder = track / xd_info[drive].heads;
423                 sector = block % xd_info[drive].sectors;
424
425 #ifdef DEBUG_READWRITE
426                 printk("xd_readwrite: drive = %d, head = %d, cylinder = %d, sector = %d, count = %d\n",drive,head,cylinder,sector,temp);
427 #endif /* DEBUG_READWRITE */
428
429                 if (xd_dma_buffer) {
430                         mode = xd_setup_dma(operation == READ ? DMA_MODE_READ : DMA_MODE_WRITE,(u_char *)(xd_dma_buffer),temp * 0x200);
431                         real_buffer = &xd_dma_buffer;
432                         for (i=0; i < (temp * 0x200); i++)
433                                 xd_dma_buffer[i] = buffer[i];
434                 }
435                 else
436                         real_buffer = &buffer;
437
438                 xd_build(cmdblk,operation == READ ? CMD_READ : CMD_WRITE,drive,head,cylinder,sector,temp & 0xFF,control);
439
440                 switch (xd_command(cmdblk,mode,(u_char *)(*real_buffer),(u_char *)(*real_buffer),sense,XD_TIMEOUT)) {
441                         case 1:
442                                 printk("xd%c: %s timeout, recalibrating drive\n",'a'+drive,(operation == READ ? "read" : "write"));
443                                 xd_recalibrate(drive);
444                                 return (0);
445                         case 2:
446                                 if (sense[0] & 0x30) {
447                                         printk("xd%c: %s - ",'a'+drive,(operation == READ ? "reading" : "writing"));
448                                         switch ((sense[0] & 0x30) >> 4) {
449                                         case 0: printk("drive error, code = 0x%X",sense[0] & 0x0F);
450                                                 break;
451                                         case 1: printk("controller error, code = 0x%X",sense[0] & 0x0F);
452                                                 break;
453                                         case 2: printk("command error, code = 0x%X",sense[0] & 0x0F);
454                                                 break;
455                                         case 3: printk("miscellaneous error, code = 0x%X",sense[0] & 0x0F);
456                                                 break;
457                                         }
458                                 }
459                                 if (sense[0] & 0x80)
460                                         printk(" - CHS = %d/%d/%d\n",((sense[2] & 0xC0) << 2) | sense[3],sense[1] & 0x1F,sense[2] & 0x3F);
461                                 /*      reported drive number = (sense[1] & 0xE0) >> 5 */
462                                 else
463                                         printk(" - no valid disk address\n");
464                                 return (0);
465                 }
466                 if (xd_dma_buffer)
467                         for (i=0; i < (temp * 0x200); i++)
468                                 buffer[i] = xd_dma_buffer[i];
469
470                 count -= temp, buffer += temp * 0x200, block += temp;
471         }
472         return (1);
473 }
474
475 /* xd_recalibrate: recalibrate a given drive and reset controller if necessary */
476 static void xd_recalibrate (u_char drive)
477 {
478         u_char cmdblk[6];
479         
480         xd_build(cmdblk,CMD_RECALIBRATE,drive,0,0,0,0,0);
481         if (xd_command(cmdblk,PIO_MODE,0,0,0,XD_TIMEOUT * 8))
482                 printk("xd%c: warning! error recalibrating, controller may be unstable\n", 'a'+drive);
483 }
484
485 /* xd_interrupt_handler: interrupt service routine */
486 static void xd_interrupt_handler(int irq, void *dev_id, struct pt_regs * regs)
487 {
488         if (inb(XD_STATUS) & STAT_INTERRUPT) {                                                  /* check if it was our device */
489 #ifdef DEBUG_OTHER
490                 printk("xd_interrupt_handler: interrupt detected\n");
491 #endif /* DEBUG_OTHER */
492                 outb(0,XD_CONTROL);                                                             /* acknowledge interrupt */
493                 wake_up(&xd_wait_int);                                                          /* and wake up sleeping processes */
494         }
495         else
496                 printk("xd: unexpected interrupt\n");
497 }
498
499 /* xd_setup_dma: set up the DMA controller for a data transfer */
500 static u_char xd_setup_dma (u_char mode,u_char *buffer,u_int count)
501 {
502         unsigned long f;
503         
504         if (nodma)
505                 return (PIO_MODE);
506         if (((unsigned long) buffer & 0xFFFF0000) != (((unsigned long) buffer + count) & 0xFFFF0000)) {
507 #ifdef DEBUG_OTHER
508                 printk("xd_setup_dma: using PIO, transfer overlaps 64k boundary\n");
509 #endif /* DEBUG_OTHER */
510                 return (PIO_MODE);
511         }
512         
513         f=claim_dma_lock();
514         disable_dma(xd_dma);
515         clear_dma_ff(xd_dma);
516         set_dma_mode(xd_dma,mode);
517         set_dma_addr(xd_dma, (unsigned long) buffer);
518         set_dma_count(xd_dma,count);
519         
520         release_dma_lock(f);
521
522         return (DMA_MODE);                      /* use DMA and INT */
523 }
524
525 /* xd_build: put stuff into an array in a format suitable for the controller */
526 static u_char *xd_build (u_char *cmdblk,u_char command,u_char drive,u_char head,u_short cylinder,u_char sector,u_char count,u_char control)
527 {
528         cmdblk[0] = command;
529         cmdblk[1] = ((drive & 0x07) << 5) | (head & 0x1F);
530         cmdblk[2] = ((cylinder & 0x300) >> 2) | (sector & 0x3F);
531         cmdblk[3] = cylinder & 0xFF;
532         cmdblk[4] = count;
533         cmdblk[5] = control;
534         
535         return (cmdblk);
536 }
537
538 /* xd_wakeup is called from timer interrupt */
539 static void xd_wakeup (unsigned long unused)
540 {
541         wake_up(&xdc_wait);
542 }
543
544 /* xd_wakeup is called from timer interrupt */
545 static void xd_watchdog (unsigned long unused)
546 {
547         xd_error = 1;
548         wake_up(&xd_wait_int);
549 }
550
551 /* xd_waitport: waits until port & mask == flags or a timeout occurs. return 1 for a timeout */
552 static inline u_char xd_waitport (u_short port,u_char flags,u_char mask,u_long timeout)
553 {
554         u_long expiry = jiffies + timeout;
555         int success;
556
557         xdc_busy = 1;
558         while ((success = ((inb(port) & mask) != flags)) && time_before(jiffies, expiry)) {
559                 xd_timer.expires = jiffies;
560                 cli();
561                 add_timer(&xd_timer);
562                 sleep_on(&xdc_wait);
563                 del_timer(&xd_timer);
564                 sti();
565         }
566         xdc_busy = 0;
567         return (success);
568 }
569
570 static inline u_int xd_wait_for_IRQ (void)
571 {
572         unsigned long flags;
573         xd_watchdog_int.expires = jiffies + 8 * HZ;
574         add_timer(&xd_watchdog_int);
575         
576         flags=claim_dma_lock();
577         enable_dma(xd_dma);
578         release_dma_lock(flags);
579         
580         sleep_on(&xd_wait_int);
581         del_timer(&xd_watchdog_int);
582         xdc_busy = 0;
583         
584         flags=claim_dma_lock();
585         disable_dma(xd_dma);
586         release_dma_lock(flags);
587         
588         if (xd_error) {
589                 printk("xd: missed IRQ - command aborted\n");
590                 xd_error = 0;
591                 return (1);
592         }
593         return (0);
594 }
595
596 /* xd_command: handle all data transfers necessary for a single command */
597 static u_int xd_command (u_char *command,u_char mode,u_char *indata,u_char *outdata,u_char *sense,u_long timeout)
598 {
599         u_char cmdblk[6],csb,complete = 0;
600
601 #ifdef DEBUG_COMMAND
602         printk("xd_command: command = 0x%X, mode = 0x%X, indata = 0x%X, outdata = 0x%X, sense = 0x%X\n",command,mode,indata,outdata,sense);
603 #endif /* DEBUG_COMMAND */
604
605         outb(0,XD_SELECT);
606         outb(mode,XD_CONTROL);
607
608         if (xd_waitport(XD_STATUS,STAT_SELECT,STAT_SELECT,timeout))
609                 return (1);
610
611         while (!complete) {
612                 if (xd_waitport(XD_STATUS,STAT_READY,STAT_READY,timeout))
613                         return (1);
614
615                 switch (inb(XD_STATUS) & (STAT_COMMAND | STAT_INPUT)) {
616                         case 0:
617                                 if (mode == DMA_MODE) {
618                                         if (xd_wait_for_IRQ())
619                                                 return (1);
620                                 } else
621                                         outb(outdata ? *outdata++ : 0,XD_DATA);
622                                 break;
623                         case STAT_INPUT:
624                                 if (mode == DMA_MODE) {
625                                         if (xd_wait_for_IRQ())
626                                                 return (1);
627                                 } else
628                                         if (indata)
629                                                 *indata++ = inb(XD_DATA);
630                                         else
631                                                 inb(XD_DATA);
632                                 break;
633                         case STAT_COMMAND:
634                                 outb(command ? *command++ : 0,XD_DATA);
635                                 break;
636                         case STAT_COMMAND | STAT_INPUT:
637                                 complete = 1;
638                                 break;
639                 }
640         }
641         csb = inb(XD_DATA);
642
643         if (xd_waitport(XD_STATUS,0,STAT_SELECT,timeout))                                       /* wait until deselected */
644                 return (1);
645
646         if (csb & CSB_ERROR) {                                                                  /* read sense data if error */
647                 xd_build(cmdblk,CMD_SENSE,(csb & CSB_LUN) >> 5,0,0,0,0,0);
648                 if (xd_command(cmdblk,0,sense,0,0,XD_TIMEOUT))
649                         printk("xd: warning! sense command failed!\n");
650         }
651
652 #ifdef DEBUG_COMMAND
653         printk("xd_command: completed with csb = 0x%X\n",csb);
654 #endif /* DEBUG_COMMAND */
655
656         return (csb & CSB_ERROR);
657 }
658
659 static u_char __init xd_initdrives (void (*init_drive)(u_char drive))
660 {
661         u_char cmdblk[6],i,count = 0;
662
663         for (i = 0; i < XD_MAXDRIVES; i++) {
664                 xd_build(cmdblk,CMD_TESTREADY,i,0,0,0,0,0);
665                 if (!xd_command(cmdblk,PIO_MODE,0,0,0,XD_TIMEOUT * 8)) {
666                         xd_timer.expires = jiffies + XD_INIT_DISK_DELAY;
667                         add_timer(&xd_timer);
668                         sleep_on(&xdc_wait);
669
670                         init_drive(count);
671                         count++;
672
673                         xd_timer.expires = jiffies + XD_INIT_DISK_DELAY;
674                         add_timer(&xd_timer);
675                         sleep_on(&xdc_wait);
676                 }
677         }
678         return (count);
679 }
680
681 static void __init xd_manual_geo_set (u_char drive)
682 {
683         xd_info[drive].heads = (u_char)(xd_geo[3 * drive + 1]);
684         xd_info[drive].cylinders = (u_short)(xd_geo[3 * drive]);
685         xd_info[drive].sectors = (u_char)(xd_geo[3 * drive + 2]);
686 }
687
688 static void __init xd_dtc_init_controller (unsigned int address)
689 {
690         switch (address) {
691                 case 0x00000:
692                 case 0xC8000:   break;                  /*initial: 0x320 */
693                 case 0xCA000:   xd_iobase = 0x324; 
694                 case 0xD0000:                           /*5150CX*/
695                 case 0xD8000:   break;                  /*5150CX & 5150XL*/
696                 default:        printk("xd_dtc_init_controller: unsupported BIOS address %06x\n",address);
697                                 break;
698         }
699         xd_maxsectors = 0x01;           /* my card seems to have trouble doing multi-block transfers? */
700
701         outb(0,XD_RESET);               /* reset the controller */
702 }
703
704
705 static void __init xd_dtc5150cx_init_drive (u_char drive)
706 {
707         /* values from controller's BIOS - BIOS chip may be removed */
708         static u_short geometry_table[][4] = {
709                 {0x200,8,0x200,0x100},
710                 {0x267,2,0x267,0x267},
711                 {0x264,4,0x264,0x80},
712                 {0x132,4,0x132,0x0},
713                 {0x132,2,0x80, 0x132},
714                 {0x177,8,0x177,0x0},
715                 {0x132,8,0x84, 0x0},
716                 {},  /* not used */
717                 {0x132,6,0x80, 0x100},
718                 {0x200,6,0x100,0x100},
719                 {0x264,2,0x264,0x80},
720                 {0x280,4,0x280,0x100},
721                 {0x2B9,3,0x2B9,0x2B9},
722                 {0x2B9,5,0x2B9,0x2B9},
723                 {0x280,6,0x280,0x100},
724                 {0x132,4,0x132,0x0}};
725         u_char n;
726
727         n = inb(XD_JUMPER);
728         n = (drive ? n : (n >> 2)) & 0x33;
729         n = (n | (n >> 2)) & 0x0F;
730         if (xd_geo[3*drive])
731                 xd_manual_geo_set(drive);
732         else
733                 if (n != 7) {   
734                         xd_info[drive].heads = (u_char)(geometry_table[n][1]);                  /* heads */
735                         xd_info[drive].cylinders = geometry_table[n][0];        /* cylinders */
736                         xd_info[drive].sectors = 17;                            /* sectors */
737 #if 0
738                         xd_info[drive].rwrite = geometry_table[n][2];   /* reduced write */
739                         xd_info[drive].precomp = geometry_table[n][3]           /* write precomp */
740                         xd_info[drive].ecc = 0x0B;                              /* ecc length */
741 #endif /* 0 */
742                 }
743                 else {
744                         printk("xd%c: undetermined drive geometry\n",'a'+drive);
745                         return;
746                 }
747         xd_info[drive].control = 5;                             /* control byte */
748         xd_setparam(CMD_DTCSETPARAM,drive,xd_info[drive].heads,xd_info[drive].cylinders,geometry_table[n][2],geometry_table[n][3],0x0B);
749         xd_recalibrate(drive);
750 }
751
752 static void __init xd_dtc_init_drive (u_char drive)
753 {
754         u_char cmdblk[6],buf[64];
755
756         xd_build(cmdblk,CMD_DTCGETGEOM,drive,0,0,0,0,0);
757         if (!xd_command(cmdblk,PIO_MODE,buf,0,0,XD_TIMEOUT * 2)) {
758                 xd_info[drive].heads = buf[0x0A];                       /* heads */
759                 xd_info[drive].cylinders = ((u_short *) (buf))[0x04];   /* cylinders */
760                 xd_info[drive].sectors = 17;                            /* sectors */
761                 if (xd_geo[3*drive])
762                         xd_manual_geo_set(drive);
763 #if 0
764                 xd_info[drive].rwrite = ((u_short *) (buf + 1))[0x05];  /* reduced write */
765                 xd_info[drive].precomp = ((u_short *) (buf + 1))[0x06]; /* write precomp */
766                 xd_info[drive].ecc = buf[0x0F];                         /* ecc length */
767 #endif /* 0 */
768                 xd_info[drive].control = 0;                             /* control byte */
769
770                 xd_setparam(CMD_DTCSETPARAM,drive,xd_info[drive].heads,xd_info[drive].cylinders,((u_short *) (buf + 1))[0x05],((u_short *) (buf + 1))[0x06],buf[0x0F]);
771                 xd_build(cmdblk,CMD_DTCSETSTEP,drive,0,0,0,0,7);
772                 if (xd_command(cmdblk,PIO_MODE,0,0,0,XD_TIMEOUT * 2))
773                         printk("xd_dtc_init_drive: error setting step rate for xd%c\n", 'a'+drive);
774         }
775         else
776                 printk("xd_dtc_init_drive: error reading geometry for xd%c\n", 'a'+drive);
777 }
778
779 static void __init xd_wd_init_controller (unsigned int address)
780 {
781         switch (address) {
782                 case 0x00000:
783                 case 0xC8000:   break;                  /*initial: 0x320 */
784                 case 0xCA000:   xd_iobase = 0x324; break;
785                 case 0xCC000:   xd_iobase = 0x328; break;
786                 case 0xCE000:   xd_iobase = 0x32C; break;
787                 case 0xD0000:   xd_iobase = 0x328; break; /* ? */
788                 case 0xD8000:   xd_iobase = 0x32C; break; /* ? */
789                 default:        printk("xd_wd_init_controller: unsupported BIOS address %06x\n",address);
790                                 break;
791         }
792         xd_maxsectors = 0x01;           /* this one doesn't wrap properly either... */
793
794         outb(0,XD_RESET);               /* reset the controller */
795
796         xd_timer.expires = jiffies + XD_INIT_DISK_DELAY;
797         add_timer(&xd_timer);
798         sleep_on(&xdc_wait);
799 }
800
801 static void __init xd_wd_init_drive (u_char drive)
802 {
803         /* values from controller's BIOS - BIOS may be disabled */
804         static u_short geometry_table[][4] = {
805                 {0x264,4,0x1C2,0x1C2},   /* common part */
806                 {0x132,4,0x099,0x0},
807                 {0x267,2,0x1C2,0x1C2},
808                 {0x267,4,0x1C2,0x1C2},
809
810                 {0x334,6,0x335,0x335},   /* 1004 series RLL */
811                 {0x30E,4,0x30F,0x3DC},
812                 {0x30E,2,0x30F,0x30F},
813                 {0x267,4,0x268,0x268},
814
815                 {0x3D5,5,0x3D6,0x3D6},   /* 1002 series RLL */
816                 {0x3DB,7,0x3DC,0x3DC},
817                 {0x264,4,0x265,0x265},
818                 {0x267,4,0x268,0x268}};
819
820         u_char cmdblk[6],buf[0x200];
821         u_char n = 0,rll,jumper_state,use_jumper_geo;
822         u_char wd_1002 = (xd_sigs[xd_type].string[7] == '6');
823         
824         jumper_state = ~(inb(0x322));
825         if (jumper_state & 0x40)
826                 xd_irq = 9;
827         rll = (jumper_state & 0x30) ? (0x04 << wd_1002) : 0;
828         xd_build(cmdblk,CMD_READ,drive,0,0,0,1,0);
829         if (!xd_command(cmdblk,PIO_MODE,buf,0,0,XD_TIMEOUT * 2)) {
830                 xd_info[drive].heads = buf[0x1AF];                              /* heads */
831                 xd_info[drive].cylinders = ((u_short *) (buf + 1))[0xD6];       /* cylinders */
832                 xd_info[drive].sectors = 17;                                    /* sectors */
833                 if (xd_geo[3*drive])
834                         xd_manual_geo_set(drive);
835 #if 0
836                 xd_info[drive].rwrite = ((u_short *) (buf))[0xD8];              /* reduced write */
837                 xd_info[drive].wprecomp = ((u_short *) (buf))[0xDA];            /* write precomp */
838                 xd_info[drive].ecc = buf[0x1B4];                                /* ecc length */
839 #endif /* 0 */
840                 xd_info[drive].control = buf[0x1B5];                            /* control byte */
841                 use_jumper_geo = !(xd_info[drive].heads) || !(xd_info[drive].cylinders);
842                 if (xd_geo[3*drive]) {
843                         xd_manual_geo_set(drive);
844                         xd_info[drive].control = rll ? 7 : 5;
845                 }
846                 else if (use_jumper_geo) {
847                         n = (((jumper_state & 0x0F) >> (drive << 1)) & 0x03) | rll;
848                         xd_info[drive].cylinders = geometry_table[n][0];
849                         xd_info[drive].heads = (u_char)(geometry_table[n][1]);
850                         xd_info[drive].control = rll ? 7 : 5;
851 #if 0
852                         xd_info[drive].rwrite = geometry_table[n][2];
853                         xd_info[drive].wprecomp = geometry_table[n][3];
854                         xd_info[drive].ecc = 0x0B;
855 #endif /* 0 */
856                 }
857                 if (!wd_1002) {
858                         if (use_jumper_geo)
859                                 xd_setparam(CMD_WDSETPARAM,drive,xd_info[drive].heads,xd_info[drive].cylinders,
860                                         geometry_table[n][2],geometry_table[n][3],0x0B);
861                         else
862                                 xd_setparam(CMD_WDSETPARAM,drive,xd_info[drive].heads,xd_info[drive].cylinders,
863                                         ((u_short *) (buf))[0xD8],((u_short *) (buf))[0xDA],buf[0x1B4]);
864                 }
865         /* 1002 based RLL controller requests converted addressing, but reports physical 
866            (physical 26 sec., logical 17 sec.) 
867            1004 based ???? */
868                 if (rll & wd_1002) {
869                         if ((xd_info[drive].cylinders *= 26,
870                              xd_info[drive].cylinders /= 17) > 1023)
871                                 xd_info[drive].cylinders = 1023;  /* 1024 ? */
872 #if 0
873                         xd_info[drive].rwrite *= 26; 
874                         xd_info[drive].rwrite /= 17;
875                         xd_info[drive].wprecomp *= 26
876                         xd_info[drive].wprecomp /= 17;
877 #endif /* 0 */
878                 }
879         }
880         else
881                 printk("xd_wd_init_drive: error reading geometry for xd%c\n",'a'+drive);        
882
883 }
884
885 static void __init xd_seagate_init_controller (unsigned int address)
886 {
887         switch (address) {
888                 case 0x00000:
889                 case 0xC8000:   break;                  /*initial: 0x320 */
890                 case 0xD0000:   xd_iobase = 0x324; break;
891                 case 0xD8000:   xd_iobase = 0x328; break;
892                 case 0xE0000:   xd_iobase = 0x32C; break;
893                 default:        printk("xd_seagate_init_controller: unsupported BIOS address %06x\n",address);
894                                 break;
895         }
896         xd_maxsectors = 0x40;
897
898         outb(0,XD_RESET);               /* reset the controller */
899 }
900
901 static void __init xd_seagate_init_drive (u_char drive)
902 {
903         u_char cmdblk[6],buf[0x200];
904
905         xd_build(cmdblk,CMD_ST11GETGEOM,drive,0,0,0,1,0);
906         if (!xd_command(cmdblk,PIO_MODE,buf,0,0,XD_TIMEOUT * 2)) {
907                 xd_info[drive].heads = buf[0x04];                               /* heads */
908                 xd_info[drive].cylinders = (buf[0x02] << 8) | buf[0x03];        /* cylinders */
909                 xd_info[drive].sectors = buf[0x05];                             /* sectors */
910                 xd_info[drive].control = 0;                                     /* control byte */
911         }
912         else
913                 printk("xd_seagate_init_drive: error reading geometry from xd%c\n", 'a'+drive);
914 }
915
916 /* Omti support courtesy Dirk Melchers */
917 static void __init xd_omti_init_controller (unsigned int address)
918 {
919         switch (address) {
920                 case 0x00000:
921                 case 0xC8000:   break;                  /*initial: 0x320 */
922                 case 0xD0000:   xd_iobase = 0x324; break;
923                 case 0xD8000:   xd_iobase = 0x328; break;
924                 case 0xE0000:   xd_iobase = 0x32C; break;
925                 default:        printk("xd_omti_init_controller: unsupported BIOS address %06x\n",address);
926                                 break;
927         }
928         
929         xd_maxsectors = 0x40;
930
931         outb(0,XD_RESET);               /* reset the controller */
932 }
933
934 static void __init xd_omti_init_drive (u_char drive)
935 {
936         /* gets infos from drive */
937         xd_override_init_drive(drive);
938
939         /* set other parameters, Hardcoded, not that nice :-) */
940         xd_info[drive].control = 2;
941 }
942
943 /* Xebec support (AK) */
944 static void __init xd_xebec_init_controller (unsigned int address)
945 {
946 /* iobase may be set manually in range 0x300 - 0x33C
947       irq may be set manually to 2(9),3,4,5,6,7
948       dma may be set manually to 1,2,3
949         (How to detect them ???)
950 BIOS address may be set manually in range 0x0 - 0xF8000
951 If you need non-standard settings use the xd=... command */
952
953         switch (address) {
954                 case 0x00000:
955                 case 0xC8000:   /* initially: xd_iobase==0x320 */
956                 case 0xD0000:
957                 case 0xD2000:
958                 case 0xD4000:
959                 case 0xD6000:
960                 case 0xD8000:
961                 case 0xDA000:
962                 case 0xDC000:
963                 case 0xDE000:
964                 case 0xE0000:   break;
965                 default:        printk("xd_xebec_init_controller: unsupported BIOS address %06x\n",address);
966                                 break;
967                 }
968
969         xd_maxsectors = 0x01;
970         outb(0,XD_RESET);               /* reset the controller */
971
972         xd_timer.expires = jiffies + XD_INIT_DISK_DELAY;
973         add_timer(&xd_timer);
974         sleep_on(&xdc_wait);
975 }
976
977 static void __init xd_xebec_init_drive (u_char drive)
978 {
979         /* values from controller's BIOS - BIOS chip may be removed */
980         static u_short geometry_table[][5] = {
981                 {0x132,4,0x080,0x080,0x7},
982                 {0x132,4,0x080,0x080,0x17},
983                 {0x264,2,0x100,0x100,0x7},
984                 {0x264,2,0x100,0x100,0x17},
985                 {0x132,8,0x080,0x080,0x7},
986                 {0x132,8,0x080,0x080,0x17},
987                 {0x264,4,0x100,0x100,0x6},
988                 {0x264,4,0x100,0x100,0x17},
989                 {0x2BC,5,0x2BC,0x12C,0x6},
990                 {0x3A5,4,0x3A5,0x3A5,0x7},
991                 {0x26C,6,0x26C,0x26C,0x7},
992                 {0x200,8,0x200,0x100,0x17},
993                 {0x400,5,0x400,0x400,0x7},
994                 {0x400,6,0x400,0x400,0x7},
995                 {0x264,8,0x264,0x200,0x17},
996                 {0x33E,7,0x33E,0x200,0x7}};
997         u_char n;
998
999         n = inb(XD_JUMPER) & 0x0F; /* BIOS's drive number: same geometry 
1000                                         is assumed for BOTH drives */
1001         if (xd_geo[3*drive])
1002                 xd_manual_geo_set(drive);
1003         else {
1004                 xd_info[drive].heads = (u_char)(geometry_table[n][1]);                  /* heads */
1005                 xd_info[drive].cylinders = geometry_table[n][0];        /* cylinders */
1006                 xd_info[drive].sectors = 17;                            /* sectors */
1007 #if 0
1008                 xd_info[drive].rwrite = geometry_table[n][2];   /* reduced write */
1009                 xd_info[drive].precomp = geometry_table[n][3]           /* write precomp */
1010                 xd_info[drive].ecc = 0x0B;                              /* ecc length */
1011 #endif /* 0 */
1012         }
1013         xd_info[drive].control = geometry_table[n][4];                  /* control byte */
1014         xd_setparam(CMD_XBSETPARAM,drive,xd_info[drive].heads,xd_info[drive].cylinders,geometry_table[n][2],geometry_table[n][3],0x0B);
1015         xd_recalibrate(drive);
1016 }
1017
1018 /* xd_override_init_drive: this finds disk geometry in a "binary search" style, narrowing in on the "correct" number of heads
1019    etc. by trying values until it gets the highest successful value. Idea courtesy Salvador Abreu (spa@fct.unl.pt). */
1020 static void __init xd_override_init_drive (u_char drive)
1021 {
1022         u_short min[] = { 0,0,0 },max[] = { 16,1024,64 },test[] = { 0,0,0 };
1023         u_char cmdblk[6],i;
1024
1025         if (xd_geo[3*drive])
1026                 xd_manual_geo_set(drive);
1027         else {
1028                 for (i = 0; i < 3; i++) {
1029                         while (min[i] != max[i] - 1) {
1030                                 test[i] = (min[i] + max[i]) / 2;
1031                                 xd_build(cmdblk,CMD_SEEK,drive,(u_char) test[0],(u_short) test[1],(u_char) test[2],0,0);
1032                                 if (!xd_command(cmdblk,PIO_MODE,0,0,0,XD_TIMEOUT * 2))
1033                                         min[i] = test[i];
1034                                 else
1035                                         max[i] = test[i];
1036                         }
1037                         test[i] = min[i];
1038                 }
1039                 xd_info[drive].heads = (u_char) min[0] + 1;
1040                 xd_info[drive].cylinders = (u_short) min[1] + 1;
1041                 xd_info[drive].sectors = (u_char) min[2] + 1;
1042         }
1043         xd_info[drive].control = 0;
1044 }
1045
1046 /* xd_setup: initialise controller from command line parameters */
1047 void __init do_xd_setup (int *integers)
1048 {
1049         switch (integers[0]) {
1050                 case 4: if (integers[4] < 0)
1051                                 nodma = 1;
1052                         else if (integers[4] < 8)
1053                                 xd_dma = integers[4];
1054                 case 3: if ((integers[3] > 0) && (integers[3] <= 0x3FC))
1055                                 xd_iobase = integers[3];
1056                 case 2: if ((integers[2] > 0) && (integers[2] < 16))
1057                                 xd_irq = integers[2];
1058                 case 1: xd_override = 1;
1059                         if ((integers[1] >= 0) && (integers[1] < (sizeof(xd_sigs) / sizeof(xd_sigs[0]))))
1060                                 xd_type = integers[1];
1061                 case 0: break;
1062                 default:printk("xd: too many parameters for xd\n");
1063         }
1064         xd_maxsectors = 0x01;
1065 }
1066
1067 /* xd_setparam: set the drive characteristics */
1068 static void __init xd_setparam (u_char command,u_char drive,u_char heads,u_short cylinders,u_short rwrite,u_short wprecomp,u_char ecc)
1069 {
1070         u_char cmdblk[14];
1071
1072         xd_build(cmdblk,command,drive,0,0,0,0,0);
1073         cmdblk[6] = (u_char) (cylinders >> 8) & 0x03;
1074         cmdblk[7] = (u_char) (cylinders & 0xFF);
1075         cmdblk[8] = heads & 0x1F;
1076         cmdblk[9] = (u_char) (rwrite >> 8) & 0x03;
1077         cmdblk[10] = (u_char) (rwrite & 0xFF);
1078         cmdblk[11] = (u_char) (wprecomp >> 8) & 0x03;
1079         cmdblk[12] = (u_char) (wprecomp & 0xFF);
1080         cmdblk[13] = ecc;
1081
1082         /* Some controllers require geometry info as data, not command */
1083
1084         if (xd_command(cmdblk,PIO_MODE,0,&cmdblk[6],0,XD_TIMEOUT * 2))
1085                 printk("xd: error setting characteristics for xd%c\n", 'a'+drive);
1086 }
1087
1088
1089 #ifdef MODULE
1090 static int xd[5] = { -1,-1,-1,-1, };
1091
1092 MODULE_PARM(xd, "1-4i");
1093 MODULE_PARM(xd_geo, "3-6i");
1094 MODULE_PARM(nodma, "i");
1095
1096 MODULE_LICENSE("GPL");
1097
1098 static void xd_done (void)
1099 {
1100         blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1101         del_gendisk(&xd_gendisk);
1102         blk_clear(MAJOR_NR);
1103         release_region(xd_iobase,4);
1104 }
1105
1106 int init_module(void)
1107 {
1108         int i,count = 0;
1109         int error;
1110
1111         for (i = 4; i > 0; i--)
1112                 if(((xd[i] = xd[i-1]) >= 0) && !count)
1113                         count = i;
1114         if((xd[0] = count))
1115                 do_xd_setup(xd);
1116
1117         error = xd_init();
1118         if (error) return error;
1119
1120         printk(KERN_INFO "XD: Loaded as a module.\n");
1121         if (!xd_drives) {
1122                 /* no drives detected - unload module */
1123                 devfs_unregister_blkdev(MAJOR_NR, "xd");
1124                 xd_done();
1125                 return (-1);
1126         }
1127         
1128         return 0;
1129 }
1130
1131 void cleanup_module(void)
1132 {
1133         devfs_unregister_blkdev(MAJOR_NR, "xd");
1134         xd_done();
1135         devfs_unregister (devfs_handle);
1136         if (xd_drives) {
1137                 free_irq(xd_irq, NULL);
1138                 free_dma(xd_dma);
1139                 if (xd_dma_buffer)
1140                         xd_dma_mem_free((unsigned long)xd_dma_buffer, xd_maxsectors * 0x200);
1141         }
1142 }
1143 #else
1144
1145 static int __init xd_setup (char *str)
1146 {
1147         int ints[5];
1148         get_options (str, ARRAY_SIZE (ints), ints);
1149         do_xd_setup (ints);
1150         return 1;
1151 }
1152
1153 /* xd_manual_geo_init: initialise drive geometry from command line parameters
1154    (used only for WD drives) */
1155 static int __init xd_manual_geo_init (char *str)
1156 {
1157         int i, integers[1 + 3*XD_MAXDRIVES];
1158
1159         get_options (str, ARRAY_SIZE (integers), integers);
1160         if (integers[0]%3 != 0) {
1161                 printk("xd: incorrect number of parameters for xd_geo\n");
1162                 return 1;
1163         }
1164         for (i = 0; (i < integers[0]) && (i < 3*XD_MAXDRIVES); i++)
1165                 xd_geo[i] = integers[i+1];
1166         return 1;
1167 }
1168
1169 __setup ("xd=", xd_setup);
1170 __setup ("xd_geo=", xd_manual_geo_init);
1171
1172 #endif /* MODULE */
1173