added mtd driver
[linux-2.4.git] / drivers / ide / ide.c
1 /*
2  *  linux/drivers/ide/ide.c             Version 7.00beta3       Apr 22 2003
3  *
4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5  */
6
7 /*
8  *  Mostly written by Mark Lord  <mlord@pobox.com>
9  *                and Gadi Oxman <gadio@netvision.net.il>
10  *                and Andre Hedrick <andre@linux-ide.org>
11  *
12  *  See linux/MAINTAINERS for address of current maintainer.
13  *
14  * This is the multiple IDE interface driver, as evolved from hd.c.
15  * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
16  *   (usually 14 & 15).
17  * There can be up to two drives per interface, as per the ATA-2 spec.
18  *
19  * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
20  * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
21  * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
22  * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
23  * ...
24  *
25  *  From hd.c:
26  *  |
27  *  | It traverses the request-list, using interrupts to jump between functions.
28  *  | As nearly all functions can be called within interrupts, we may not sleep.
29  *  | Special care is recommended.  Have Fun!
30  *  |
31  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
32  *  |
33  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
34  *  | in the early extended-partition checks and added DM partitions.
35  *  |
36  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
37  *  |
38  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
39  *  | and general streamlining by Mark Lord (mlord@pobox.com).
40  *
41  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
42  *
43  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
44  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
45  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
46  *
47  *  This was a rewrite of just about everything from hd.c, though some original
48  *  code is still sprinkled about.  Think of it as a major evolution, with
49  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
50  *
51  *  Version 1.0 ALPHA   initial code, primary i/f working okay
52  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
53  *  Version 1.4 BETA    added auto probing for irq(s)
54  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
55  *  ...
56  * Version 5.50         allow values as small as 20 for idebus=
57  * Version 5.51         force non io_32bit in drive_cmd_intr()
58  *                      change delay_10ms() to delay_50ms() to fix problems
59  * Version 5.52         fix incorrect invalidation of removable devices
60  *                      add "hdx=slow" command line option
61  * Version 5.60         start to modularize the driver; the disk and ATAPI
62  *                       drivers can be compiled as loadable modules.
63  *                      move IDE probe code to ide-probe.c
64  *                      move IDE disk code to ide-disk.c
65  *                      add support for generic IDE device subdrivers
66  *                      add m68k code from Geert Uytterhoeven
67  *                      probe all interfaces by default
68  *                      add ioctl to (re)probe an interface
69  * Version 6.00         use per device request queues
70  *                      attempt to optimize shared hwgroup performance
71  *                      add ioctl to manually adjust bandwidth algorithms
72  *                      add kerneld support for the probe module
73  *                      fix bug in ide_error()
74  *                      fix bug in the first ide_get_lock() call for Atari
75  *                      don't flush leftover data for ATAPI devices
76  * Version 6.01         clear hwgroup->active while the hwgroup sleeps
77  *                      support HDIO_GETGEO for floppies
78  * Version 6.02         fix ide_ack_intr() call
79  *                      check partition table on floppies
80  * Version 6.03         handle bad status bit sequencing in ide_wait_stat()
81  * Version 6.10         deleted old entries from this list of updates
82  *                      replaced triton.c with ide-dma.c generic PCI DMA
83  *                      added support for BIOS-enabled UltraDMA
84  *                      rename all "promise" things to "pdc4030"
85  *                      fix EZ-DRIVE handling on small disks
86  * Version 6.11         fix probe error in ide_scan_devices()
87  *                      fix ancient "jiffies" polling bugs
88  *                      mask all hwgroup interrupts on each irq entry
89  * Version 6.12         integrate ioctl and proc interfaces
90  *                      fix parsing of "idex=" command line parameter
91  * Version 6.13         add support for ide4/ide5 courtesy rjones@orchestream.com
92  * Version 6.14         fixed IRQ sharing among PCI devices
93  * Version 6.15         added SMP awareness to IDE drivers
94  * Version 6.16         fixed various bugs; even more SMP friendly
95  * Version 6.17         fix for newest EZ-Drive problem
96  * Version 6.18         default unpartitioned-disk translation now "BIOS LBA"
97  * Version 6.19         Re-design for a UNIFORM driver for all platforms,
98  *                        model based on suggestions from Russell King and
99  *                        Geert Uytterhoeven
100  *                      Promise DC4030VL now supported.
101  *                      add support for ide6/ide7
102  *                      delay_50ms() changed to ide_delay_50ms() and exported.
103  * Version 6.20         Added/Fixed Generic ATA-66 support and hwif detection.
104  *                      Added hdx=flash to allow for second flash disk
105  *                        detection w/o the hang loop.
106  *                      Added support for ide8/ide9
107  *                      Added idex=ata66 for the quirky chipsets that are
108  *                        ATA-66 compliant, but have yet to determine a method
109  *                        of verification of the 80c cable presence.
110  *                        Specifically Promise's PDC20262 chipset.
111  * Version 6.21         Fixing/Fixed SMP spinlock issue with insight from an old
112  *                        hat that clarified original low level driver design.
113  * Version 6.30         Added SMP support; fixed multmode issues.  -ml
114  * Version 6.31         Debug Share INTR's and request queue streaming
115  *                      Native ATA-100 support
116  *                      Prep for Cascades Project
117  * Version 7.00alpha    First named revision of ide rearrange
118  * Version 7.00beta     (2.4 backport)
119  *
120  *  Some additional driver compile-time options are in ./include/linux/ide.h
121  *
122  */
123
124 #define REVISION        "Revision: 7.00beta4-2.4"
125 #define VERSION         "Id: ide.c 7.00b4 20030520"
126
127 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
128
129 #define _IDE_C                  /* Tell ide.h it's really us */
130
131 #include <linux/config.h>
132 #include <linux/module.h>
133 #include <linux/types.h>
134 #include <linux/string.h>
135 #include <linux/kernel.h>
136 #include <linux/timer.h>
137 #include <linux/mm.h>
138 #include <linux/interrupt.h>
139 #include <linux/major.h>
140 #include <linux/errno.h>
141 #include <linux/genhd.h>
142 #include <linux/blkpg.h>
143 #include <linux/slab.h>
144 #include <linux/init.h>
145 #include <linux/pci.h>
146 #include <linux/delay.h>
147 #include <linux/ide.h>
148 #include <linux/devfs_fs_kernel.h>
149 #include <linux/completion.h>
150 #include <linux/reboot.h>
151
152 #include <asm/byteorder.h>
153 #include <asm/irq.h>
154 #include <asm/uaccess.h>
155 #include <asm/io.h>
156 #include <asm/bitops.h>
157
158 #include "ide_modes.h"
159
160 #include <linux/kmod.h>
161
162 /* default maximum number of failures */
163 #define IDE_DEFAULT_MAX_FAILURES        1
164
165 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
166                                         IDE2_MAJOR, IDE3_MAJOR,
167                                         IDE4_MAJOR, IDE5_MAJOR,
168                                         IDE6_MAJOR, IDE7_MAJOR,
169                                         IDE8_MAJOR, IDE9_MAJOR };
170
171 static int idebus_parameter;    /* holds the "idebus=" parameter */
172 static int system_bus_speed;    /* holds what we think is VESA/PCI bus speed */
173 static int initializing;        /* set while initializing built-in drivers */
174
175 static int ide_scan_direction;  /* THIS was formerly 2.2.x pci=reverse */
176
177 #ifdef CONFIG_IDEDMA_AUTO
178 int noautodma = 0;
179 #else
180 int noautodma = 1;
181 #endif
182
183 EXPORT_SYMBOL(noautodma);
184
185
186 /*
187  * ide_modules keeps track of the available IDE chipset/probe/driver modules.
188  */
189 ide_module_t *ide_chipsets;
190 ide_module_t *ide_modules;
191 ide_module_t *ide_probe;
192
193 /*
194  * This is declared extern in ide.h, for access by other IDE modules:
195  */
196 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
197
198 EXPORT_SYMBOL(ide_hwifs);
199
200 ide_devices_t *idedisk;
201 ide_devices_t *idecd;
202 ide_devices_t *idefloppy;
203 ide_devices_t *idetape;
204 ide_devices_t *idescsi;
205
206 EXPORT_SYMBOL(idedisk);
207 EXPORT_SYMBOL(idecd);
208 EXPORT_SYMBOL(idefloppy);
209 EXPORT_SYMBOL(idetape);
210 EXPORT_SYMBOL(idescsi);
211
212 extern ide_driver_t idedefault_driver;
213 static void setup_driver_defaults (ide_drive_t *drive);
214
215 /*
216  * Do not even *think* about calling this!
217  */
218 static void init_hwif_data (unsigned int index)
219 {
220         unsigned int unit;
221         hw_regs_t hw;
222         ide_hwif_t *hwif = &ide_hwifs[index];
223
224         /* bulk initialize hwif & drive info with zeros */
225         memset(hwif, 0, sizeof(ide_hwif_t));
226         memset(&hw, 0, sizeof(hw_regs_t));
227
228         /* fill in any non-zero initial values */
229         hwif->index     = index;
230         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
231         memcpy(&hwif->hw, &hw, sizeof(hw));
232         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
233         hwif->noprobe   = !hwif->io_ports[IDE_DATA_OFFSET];
234 #ifdef CONFIG_BLK_DEV_HD
235         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
236                 hwif->noprobe = 1; /* may be overridden by ide_setup() */
237 #endif /* CONFIG_BLK_DEV_HD */
238         hwif->major     = ide_hwif_to_major[index];
239         hwif->name[0]   = 'i';
240         hwif->name[1]   = 'd';
241         hwif->name[2]   = 'e';
242         hwif->name[3]   = '0' + index;
243         hwif->bus_state = BUSSTATE_ON;
244         hwif->reset_poll= NULL;
245         hwif->pre_reset = NULL;
246
247         hwif->atapi_dma = 0;            /* disable all atapi dma */ 
248         hwif->ultra_mask = 0x80;        /* disable all ultra */
249         hwif->mwdma_mask = 0x80;        /* disable all mwdma */
250         hwif->swdma_mask = 0x80;        /* disable all swdma */
251         hwif->sata = 0;                 /* assume PATA */
252
253         default_hwif_iops(hwif);
254         default_hwif_transport(hwif);
255         for (unit = 0; unit < MAX_DRIVES; ++unit) {
256                 ide_drive_t *drive = &hwif->drives[unit];
257
258                 drive->media                    = ide_disk;
259                 drive->select.all               = (unit<<4)|0xa0;
260                 drive->hwif                     = hwif;
261                 drive->ctl                      = 0x08;
262                 drive->ready_stat               = READY_STAT;
263                 drive->bad_wstat                = BAD_W_STAT;
264                 drive->special.b.recalibrate    = 1;
265                 drive->special.b.set_geometry   = 1;
266                 drive->name[0]                  = 'h';
267                 drive->name[1]                  = 'd';
268                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
269                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
270                 drive->using_dma                = 0;
271                 drive->is_flash                 = 0;
272                 drive->driver                   = &idedefault_driver;
273                 setup_driver_defaults(drive);
274                 init_waitqueue_head(&drive->wqueue);
275         }
276 }
277
278 /*
279  * init_ide_data() sets reasonable default values into all fields
280  * of all instances of the hwifs and drives, but only on the first call.
281  * Subsequent calls have no effect (they don't wipe out anything).
282  *
283  * This routine is normally called at driver initialization time,
284  * but may also be called MUCH earlier during kernel "command-line"
285  * parameter processing.  As such, we cannot depend on any other parts
286  * of the kernel (such as memory allocation) to be functioning yet.
287  *
288  * This is too bad, as otherwise we could dynamically allocate the
289  * ide_drive_t structs as needed, rather than always consuming memory
290  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
291  *
292  * FIXME: We should stuff the setup data into __init and copy the
293  * relevant hwifs/allocate them properly during boot.
294  */
295 #define MAGIC_COOKIE 0x12345678
296 static void __init init_ide_data (void)
297 {
298         unsigned int index;
299         static unsigned long magic_cookie = MAGIC_COOKIE;
300
301         if (magic_cookie != MAGIC_COOKIE)
302                 return;         /* already initialized */
303         magic_cookie = 0;
304
305         /* Initialise all interface structures */
306         for (index = 0; index < MAX_HWIFS; ++index)
307                 init_hwif_data(index);
308
309         /* Add default hw interfaces */
310         ide_init_default_hwifs();
311
312         idebus_parameter = 0;
313         system_bus_speed = 0;
314 }
315
316 /*
317  * ide_system_bus_speed() returns what we think is the system VESA/PCI
318  * bus speed (in MHz).  This is used for calculating interface PIO timings.
319  * The default is 40 for known PCI systems, 50 otherwise.
320  * The "idebus=xx" parameter can be used to override this value.
321  * The actual value to be used is computed/displayed the first time through.
322  */
323 int ide_system_bus_speed (void)
324 {
325         if (!system_bus_speed) {
326                 if (idebus_parameter) {
327                         /* user supplied value */
328                         system_bus_speed = idebus_parameter;
329                 } else if (pci_present()) {
330                         /* safe default value for PCI */
331                         system_bus_speed = 33;
332                 } else {
333                         /* safe default value for VESA and PCI */
334                         system_bus_speed = 50;
335                 }
336                 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
337                         "for PIO modes%s\n", system_bus_speed,
338                         idebus_parameter ? "" : "; override with idebus=xx");
339         }
340         return system_bus_speed;
341 }
342
343 /*
344  * current_capacity() returns the capacity (in sectors) of a drive
345  * according to its current geometry/LBA settings.
346  */
347 unsigned long current_capacity (ide_drive_t *drive)
348 {
349         if (!drive->present)
350                 return 0;
351         return DRIVER(drive)->capacity(drive);
352 }
353
354 EXPORT_SYMBOL(current_capacity);
355
356 static inline u32 read_24 (ide_drive_t *drive)
357 {
358         return  (HWIF(drive)->INB(IDE_HCYL_REG)<<16) |
359                 (HWIF(drive)->INB(IDE_LCYL_REG)<<8) |
360                  HWIF(drive)->INB(IDE_SECTOR_REG);
361 }
362
363 /*
364  * Error reporting, in human readable form (luxurious, but a memory hog).
365  */
366 u8 ide_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
367 {
368         ide_hwif_t *hwif = HWIF(drive);
369         unsigned long flags;
370         u8 err = 0;
371
372         local_irq_set(flags);
373         printk(KERN_WARNING "%s: %s: status=0x%02x", drive->name, msg, stat);
374 #if FANCY_STATUS_DUMPS
375         printk(" { ");
376         if (stat & BUSY_STAT) {
377                 printk("Busy ");
378         } else {
379                 if (stat & READY_STAT)  printk("DriveReady ");
380                 if (stat & WRERR_STAT)  printk("DeviceFault ");
381                 if (stat & SEEK_STAT)   printk("SeekComplete ");
382                 if (stat & DRQ_STAT)    printk("DataRequest ");
383                 if (stat & ECC_STAT)    printk("CorrectedError ");
384                 if (stat & INDEX_STAT)  printk("Index ");
385                 if (stat & ERR_STAT)    printk("Error ");
386         }
387         printk("}");
388 #endif  /* FANCY_STATUS_DUMPS */
389         printk("\n");
390         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
391                 err = hwif->INB(IDE_ERROR_REG);
392                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
393 #if FANCY_STATUS_DUMPS
394                 if (drive->media == ide_disk) {
395                         printk(" { ");
396                         if (err & ABRT_ERR)     printk("DriveStatusError ");
397                         if (err & ICRC_ERR)     printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
398                         if (err & ECC_ERR)      printk("UncorrectableError ");
399                         if (err & ID_ERR)       printk("SectorIdNotFound ");
400                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
401                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
402                         printk("}");
403                         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
404                                 if ((drive->id->command_set_2 & 0x0400) &&
405                                     (drive->id->cfs_enable_2 & 0x0400) &&
406                                     (drive->addressing == 1)) {
407                                         u64 sectors = 0;
408                                         u32 high = 0;
409                                         u32 low = read_24(drive);
410                                         hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
411                                         high = read_24(drive);
412
413                                         sectors = ((u64)high << 24) | low;
414                                         printk(", LBAsect=%llu, high=%d, low=%d",
415                                                (u64) sectors,
416                                                high, low);
417                                 } else {
418                                         u8 cur = hwif->INB(IDE_SELECT_REG);
419                                         if (cur & 0x40) {       /* using LBA? */
420                                                 printk(", LBAsect=%ld", (unsigned long)
421                                                  ((cur&0xf)<<24)
422                                                  |(hwif->INB(IDE_HCYL_REG)<<16)
423                                                  |(hwif->INB(IDE_LCYL_REG)<<8)
424                                                  | hwif->INB(IDE_SECTOR_REG));
425                                         } else {
426                                                 printk(", CHS=%d/%d/%d",
427                                                  (hwif->INB(IDE_HCYL_REG)<<8) +
428                                                   hwif->INB(IDE_LCYL_REG),
429                                                   cur & 0xf,
430                                                   hwif->INB(IDE_SECTOR_REG));
431                                         }
432                                 }
433                                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
434                                         printk(", sector=%ld", HWGROUP(drive)->rq->sector);
435                         }
436                 }
437 #endif  /* FANCY_STATUS_DUMPS */
438                 printk("\n");
439         }
440         local_irq_restore(flags);
441         return err;
442 }
443
444 EXPORT_SYMBOL(ide_dump_status);
445
446
447 /*
448  * This routine is called to flush all partitions and partition tables
449  * for a changed disk, and then re-read the new partition table.
450  * If we are revalidating a disk because of a media change, then we
451  * enter with usage == 0.  If we are using an ioctl, we automatically have
452  * usage == 1 (we need an open channel to use an ioctl :-), so this
453  * is our limit.
454  */
455 int ide_revalidate_disk (kdev_t i_rdev)
456 {
457         ide_drive_t *drive;
458         ide_hwgroup_t *hwgroup;
459         unsigned int p, major, minor;
460         unsigned long flags;
461
462         if ((drive = ide_info_ptr(i_rdev, 0)) == NULL)
463                 return -ENODEV;
464         major = MAJOR(i_rdev);
465         minor = drive->select.b.unit << PARTN_BITS;
466         hwgroup = HWGROUP(drive);
467         spin_lock_irqsave(&io_request_lock, flags);
468         if (drive->busy || (drive->usage > 1)) {
469                 spin_unlock_irqrestore(&io_request_lock, flags);
470                 return -EBUSY;
471         };
472         drive->busy = 1;
473         MOD_INC_USE_COUNT;
474         spin_unlock_irqrestore(&io_request_lock, flags);
475
476         for (p = 0; p < (1<<PARTN_BITS); ++p) {
477                 if (drive->part[p].nr_sects > 0) {
478                         kdev_t devp = MKDEV(major, minor+p);
479                         invalidate_device(devp, 1);
480                 }
481                 drive->part[p].start_sect = 0;
482                 drive->part[p].nr_sects   = 0;
483         };
484
485         if (DRIVER(drive)->revalidate)
486                 DRIVER(drive)->revalidate(drive);
487
488         drive->busy = 0;
489         wake_up(&drive->wqueue);
490         MOD_DEC_USE_COUNT;
491         return 0;
492 }
493
494 EXPORT_SYMBOL(ide_revalidate_disk);
495
496 static void revalidate_drives (int revaldiate)
497 {
498         ide_hwif_t *hwif;
499         ide_drive_t *drive;
500         int index, unit;
501
502         for (index = 0; index < MAX_HWIFS; ++index) {
503                 hwif = &ide_hwifs[index];
504                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
505                         drive = &ide_hwifs[index].drives[unit];
506                         if (drive->revalidate) {
507                                 drive->revalidate = 0;
508                                 if ((!initializing) && (revaldiate))
509                                         (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
510                         }
511                 }
512         }
513 }
514
515 void ide_probe_module (int revaldiate)
516 {
517         if (!ide_probe) {
518 #if  defined(CONFIG_BLK_DEV_IDE_MODULE)
519                 (void) request_module("ide-probe-mod");
520 #endif
521         } else {
522                 (void) ide_probe->init();
523         }
524         revalidate_drives(revaldiate);
525 }
526
527 EXPORT_SYMBOL(ide_probe_module);
528
529 void ide_driver_module (int revaldiate)
530 {
531         int index;
532         ide_module_t *module = ide_modules;
533
534         for (index = 0; index < MAX_HWIFS; ++index)
535                 if (ide_hwifs[index].present)
536                         goto search;
537         ide_probe_module(revaldiate);
538 search:
539         while (module) {
540                 (void) module->init();
541                 module = module->next;
542         }
543         revalidate_drives(revaldiate);
544 }
545
546 EXPORT_SYMBOL(ide_driver_module);
547
548 static int ide_open (struct inode * inode, struct file * filp)
549 {
550         ide_drive_t *drive;
551         int force = 1/*FIXME 0*/;
552         
553         if(capable(CAP_SYS_ADMIN) && (filp->f_flags & O_NDELAY))
554                 force = 1;
555
556         if ((drive = ide_info_ptr(inode->i_rdev, force)) == NULL)
557                 return -ENXIO;
558         
559         /*
560          *      If the device is present make sure that we attach any
561          *      needed driver
562          */     
563
564         if (drive->present)
565         {
566                 if (drive->driver == &idedefault_driver)
567                         ide_driver_module(1);
568                 if (drive->driver == &idedefault_driver) {
569                         if (drive->media == ide_disk)
570                                 (void) request_module("ide-disk");
571                         if (drive->scsi)
572                                 (void) request_module("ide-scsi");
573                         if (drive->media == ide_cdrom)
574                                 (void) request_module("ide-cd");
575                         if (drive->media == ide_tape)
576                                 (void) request_module("ide-tape");
577                         if (drive->media == ide_floppy)
578                                 (void) request_module("ide-floppy");
579                 }
580         
581                 /* The locking here isnt enough, but this is hard to fix
582                    in the 2.4 cases */
583                 while (drive->busy)
584                         sleep_on(&drive->wqueue);
585         }
586         
587         /*
588          *      Now do the actual open
589          */
590          
591         drive->usage++;
592         if (!drive->dead || force)
593                 return DRIVER(drive)->open(inode, filp, drive);
594         printk(KERN_WARNING "%s: driver not present\n", drive->name);
595         drive->usage--;
596         return -ENXIO;
597 }
598
599 /*
600  * Releasing a block device means we sync() it, so that it can safely
601  * be forgotten about...
602  */
603 static int ide_release (struct inode * inode, struct file * file)
604 {
605         ide_drive_t *drive;
606
607         if ((drive = ide_info_ptr(inode->i_rdev, 1)) != NULL) {
608                 drive->usage--;
609                 DRIVER(drive)->release(inode, file, drive);
610         }
611         return 0;
612 }
613
614 #ifdef CONFIG_PROC_FS
615 ide_proc_entry_t generic_subdriver_entries[] = {
616         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
617         { NULL, 0, NULL, NULL }
618 };
619 #endif
620
621
622 #define hwif_release_region(addr, num) \
623         ((hwif->mmio) ? release_mem_region((addr),(num)) : release_region((addr),(num)))
624
625 /*
626  * Note that we only release the standard ports,
627  * and do not even try to handle any extra ports
628  * allocated for weird IDE interface chipsets.
629  */
630 void hwif_unregister (ide_hwif_t *hwif)
631 {
632         u32 i = 0;
633
634         if (hwif->mmio == 2)
635                 return;
636         if (hwif->io_ports[IDE_CONTROL_OFFSET])
637                 hwif_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
638 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
639         if (hwif->io_ports[IDE_IRQ_OFFSET])
640                 hwif_release_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
641 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
642
643         if (hwif->straight8) {
644                 hwif_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
645                 return;
646         }
647         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
648                 if (hwif->io_ports[i]) {
649                         hwif_release_region(hwif->io_ports[i], 1);
650                 }
651         }
652 }
653
654 EXPORT_SYMBOL(hwif_unregister);
655
656 extern void init_hwif_data(unsigned int index);
657
658 /**
659  *      ide_prepare_tristate    -       prepare interface for warm unplug
660  *      @drive: drive on this hwif we are using
661  *
662  *      Prepares a drive for shutdown after a bus tristate. The
663  *      drives must be quiescent and the only user the calling ioctl
664  */
665  
666 static int ide_prepare_tristate(ide_drive_t *our_drive)
667 {
668         ide_drive_t *drive;
669         int unit;
670         unsigned long flags;
671         int minor;
672         int p;
673         int i;
674         ide_hwif_t *hwif = HWIF(our_drive);
675                 
676         if(our_drive->busy)
677                 printk("HUH? We are busy.\n");
678                 
679         if (!hwif->present)
680                 BUG();
681         spin_lock_irqsave(&io_request_lock, flags);
682         
683         /* Abort if anything is busy */
684         for (unit = 0; unit < MAX_DRIVES; ++unit) {
685                 drive = &hwif->drives[unit];
686                 if (!drive->present)
687                         continue;
688                 if (drive == our_drive && drive->usage != 1)
689                         goto abort;
690                 if (drive != our_drive && drive->usage)
691                         goto abort;
692                 if (drive->busy)
693                         goto abort;
694         }
695         /* Commit to shutdown sequence */
696         for (unit = 0; unit < MAX_DRIVES; ++unit) {
697                 drive = &hwif->drives[unit];
698                 if (!drive->present)
699                         continue;
700                 if (drive != our_drive && DRIVER(drive)->shutdown(drive))
701                         goto abort;
702         }
703         /* We hold the lock here.. which is important as we need to play
704            with usage counts beyond the scenes */
705            
706         our_drive->usage--;
707         i = DRIVER(our_drive)->shutdown(our_drive);
708         if(i)
709                 goto abort_fix;
710         /* Drive shutdown sequence done */
711         /* Prevent new opens ?? */
712         spin_unlock_irqrestore(&io_request_lock, flags);
713         /*
714          * Flush kernel side caches, and dump the /proc files
715          */
716         spin_unlock_irqrestore(&io_request_lock, flags);
717         for (unit = 0; unit < MAX_DRIVES; ++unit) {
718                 drive = &hwif->drives[unit];
719                 if (!drive->present)
720                         continue;
721                 DRIVER(drive)->cleanup(drive);
722                 minor = drive->select.b.unit << PARTN_BITS;
723                 for (p = 0; p < (1<<PARTN_BITS); ++p) {
724                         if (drive->part[p].nr_sects > 0) {
725                                 kdev_t devp = MKDEV(hwif->major, minor+p);
726                                 invalidate_device(devp, 0);
727                         }
728                 }
729 #ifdef CONFIG_PROC_FS
730                 destroy_proc_ide_drives(hwif);
731 #endif
732         }
733         spin_lock_irqsave(&io_request_lock, flags);
734         our_drive->usage++;
735         for (i = 0; i < MAX_DRIVES; ++i) {
736                 drive = &hwif->drives[i];
737                 if (drive->de) {
738                         devfs_unregister(drive->de);
739                         drive->de = NULL;
740                 }
741                 if (!drive->present)
742                         continue;
743                 if (drive->id != NULL) {
744                         kfree(drive->id);
745                         drive->id = NULL;
746                 }
747                 drive->present = 0;
748                 /* Safe to clear now */
749                 drive->dead = 0;
750         }
751         spin_unlock_irqrestore(&io_request_lock, flags);
752         return 0;
753
754 abort_fix:
755         our_drive->usage++;
756 abort:
757         spin_unlock_irqrestore(&io_request_lock, flags);
758         return -EBUSY;
759 }
760
761
762 /**
763  *      ide_resume_hwif         -       return a hwif to active mode
764  *      @hwif: interface to resume
765  *      
766  *      Restore a dead interface from tristate back to normality. At this
767  *      point the hardware driver busproc has reconnected the bus, but
768  *      nothing else has happened
769  */
770  
771 static int ide_resume_hwif(ide_drive_t *our_drive)
772 {
773         ide_hwif_t *hwif = HWIF(our_drive);
774         int err = ide_wait_hwif_ready(hwif);
775         int irqd;
776         int present = 0;
777         int unit;
778                 
779         if(err)
780         {
781                 printk(KERN_ERR "%s: drives not ready.\n", our_drive->name);
782                 return err;
783         }
784                 
785         /* The drives are now taking commands */
786         
787         irqd = hwif->irq;
788         if(irqd)
789                 disable_irq(irqd);
790                 
791         /* Identify and probe the drives */
792         
793         for (unit = 0; unit < MAX_DRIVES; ++unit) {
794                 ide_drive_t *drive = &hwif->drives[unit];
795                 drive->dn = ((hwif->channel ? 2 : 0) + unit);
796                 drive->usage = 0;
797                 drive->busy = 0;
798                 hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
799                 (void) ide_probe_for_drive(drive);
800                 if (drive->present)
801                         present = 1;
802         }
803         ide_probe_reset(hwif);
804         if(irqd)
805                 enable_irq(irqd);
806         
807         if(present)
808                 printk(KERN_INFO "ide: drives found on hot-added interface.\n");
809                         
810         /*
811          *      Set up the drive modes (Even if we didnt swap drives
812          *      we may have lost settings when we disconnected the bus)
813          */
814          
815         ide_tune_drives(hwif);
816         if(present)
817                 hwif->present = 1;
818                 
819         /*
820          *      Reattach the devices to drivers
821          */
822         for (unit = 0; unit < MAX_DRIVES; ++unit) {
823                 ide_drive_t *drive = &hwif->drives[unit];
824                 if(drive->present && !drive->dead)
825                         ide_attach_drive(drive);
826         }
827         our_drive->usage++;
828         return 0;
829 }
830
831 int ide_unregister (unsigned int index)
832 {
833         struct gendisk *gd;
834         ide_drive_t *drive, *d;
835         ide_hwif_t *hwif, *g;
836         ide_hwgroup_t *hwgroup;
837         int irq_count = 0, unit, i;
838         unsigned long flags;
839         unsigned int p, minor;
840         ide_hwif_t old_hwif;
841
842         if (index >= MAX_HWIFS)
843                 BUG();
844                 
845         spin_lock_irqsave(&io_request_lock, flags);
846         hwif = &ide_hwifs[index];
847         if (!hwif->present)
848                 goto abort;
849         for (unit = 0; unit < MAX_DRIVES; ++unit) {
850                 drive = &hwif->drives[unit];
851                 if (!drive->present)
852                         continue;
853                 if (drive->busy || drive->usage)
854                         goto abort;
855                 if (DRIVER(drive)->shutdown(drive))
856                         goto abort;
857         }
858         hwif->present = 0;
859         
860         /*
861          * All clear?  Then blow away the buffer cache
862          */
863         spin_unlock_irqrestore(&io_request_lock, flags);
864         for (unit = 0; unit < MAX_DRIVES; ++unit) {
865                 drive = &hwif->drives[unit];
866                 if (!drive->present)
867                         continue;
868                 DRIVER(drive)->cleanup(drive);
869                 minor = drive->select.b.unit << PARTN_BITS;
870                 for (p = 0; p < (1<<PARTN_BITS); ++p) {
871                         if (drive->part[p].nr_sects > 0) {
872                                 kdev_t devp = MKDEV(hwif->major, minor+p);
873                                 invalidate_device(devp, 0);
874                         }
875                 }
876 #ifdef CONFIG_PROC_FS
877                 destroy_proc_ide_drives(hwif);
878 #endif
879         }
880
881         spin_lock_irqsave(&io_request_lock, flags);
882         hwgroup = hwif->hwgroup;
883
884         /*
885          * free the irq if we were the only hwif using it
886          */
887         g = hwgroup->hwif;
888         do {
889                 if (g->irq == hwif->irq)
890                         ++irq_count;
891                 g = g->next;
892         } while (g != hwgroup->hwif);
893         if (irq_count == 1)
894                 free_irq(hwif->irq, hwgroup);
895
896         /*
897          * Note that we only release the standard ports,
898          * and do not even try to handle any extra ports
899          * allocated for weird IDE interface chipsets.
900          */
901         hwif_unregister(hwif);
902
903         /*
904          * Remove us from the hwgroup, and free
905          * the hwgroup if we were the only member
906          */
907         d = hwgroup->drive;
908         for (i = 0; i < MAX_DRIVES; ++i) {
909                 drive = &hwif->drives[i];
910                 if (drive->de) {
911                         devfs_unregister(drive->de);
912                         drive->de = NULL;
913                 }
914                 if (!drive->present)
915                         continue;
916                 while (hwgroup->drive->next != drive)
917                         hwgroup->drive = hwgroup->drive->next;
918                 hwgroup->drive->next = drive->next;
919                 if (hwgroup->drive == drive)
920                         hwgroup->drive = NULL;
921                 if (drive->id != NULL) {
922                         kfree(drive->id);
923                         drive->id = NULL;
924                 }
925                 drive->present = 0;
926                 blk_cleanup_queue(&drive->queue);
927         }
928         if (d->present)
929                 hwgroup->drive = d;
930         while (hwgroup->hwif->next != hwif)
931                 hwgroup->hwif = hwgroup->hwif->next;
932         hwgroup->hwif->next = hwif->next;
933         if (hwgroup->hwif == hwif)
934                 kfree(hwgroup);
935         else
936                 hwgroup->hwif = HWIF(hwgroup->drive);
937
938 #if !defined(CONFIG_DMA_NONPCI)
939         if (hwif->dma_base) {
940                 (void) ide_release_dma(hwif);
941
942                 hwif->dma_base = 0;
943                 hwif->dma_master = 0;
944                 hwif->dma_command = 0;
945                 hwif->dma_vendor1 = 0;
946                 hwif->dma_status = 0;
947                 hwif->dma_vendor3 = 0;
948                 hwif->dma_prdtable = 0;
949         }
950 #endif /* !(CONFIG_DMA_NONPCI) */
951
952         /*
953          * Remove us from the kernel's knowledge
954          */
955         unregister_blkdev(hwif->major, hwif->name);
956         kfree(blksize_size[hwif->major]);
957         kfree(max_sectors[hwif->major]);
958         kfree(max_readahead[hwif->major]);
959         blk_dev[hwif->major].data = NULL;
960         blk_dev[hwif->major].queue = NULL;
961         blksize_size[hwif->major] = NULL;
962         gd = hwif->gd;
963         if (gd) {
964                 del_gendisk(gd);
965                 kfree(gd->sizes);
966                 kfree(gd->part);
967                 if (gd->de_arr)
968                         kfree(gd->de_arr);
969                 if (gd->flags)
970                         kfree(gd->flags);
971                 kfree(gd);
972                 hwif->gd = NULL;
973         }
974
975         old_hwif                        = *hwif;
976         init_hwif_data(index);  /* restore hwif data to pristine status */
977         hwif->hwgroup                   = old_hwif.hwgroup;
978
979         hwif->proc                      = old_hwif.proc;
980
981         hwif->major                     = old_hwif.major;
982 //      hwif->index                     = old_hwif.index;
983 //      hwif->channel                   = old_hwif.channel;
984         hwif->straight8                 = old_hwif.straight8;
985         hwif->bus_state                 = old_hwif.bus_state;
986
987         hwif->atapi_dma                 = old_hwif.atapi_dma;
988         hwif->ultra_mask                = old_hwif.ultra_mask;
989         hwif->mwdma_mask                = old_hwif.mwdma_mask;
990         hwif->swdma_mask                = old_hwif.swdma_mask;
991
992         hwif->chipset                   = old_hwif.chipset;
993         hwif->hold                      = old_hwif.hold;
994
995 #ifdef CONFIG_BLK_DEV_IDEPCI
996         hwif->pci_dev                   = old_hwif.pci_dev;
997         hwif->cds                       = old_hwif.cds;
998 #endif /* CONFIG_BLK_DEV_IDEPCI */
999
1000 #if 0
1001         hwif->hwifops                   = old_hwif.hwifops;
1002 #else
1003         hwif->identify                  = old_hwif.identify;
1004         hwif->tuneproc                  = old_hwif.tuneproc;
1005         hwif->speedproc                 = old_hwif.speedproc;
1006         hwif->selectproc                = old_hwif.selectproc;
1007         hwif->reset_poll                = old_hwif.reset_poll;
1008         hwif->pre_reset                 = old_hwif.pre_reset;
1009         hwif->resetproc                 = old_hwif.resetproc;
1010         hwif->intrproc                  = old_hwif.intrproc;
1011         hwif->maskproc                  = old_hwif.maskproc;
1012         hwif->quirkproc                 = old_hwif.quirkproc;
1013         hwif->busproc                   = old_hwif.busproc;
1014 #endif
1015
1016 #if 0
1017         hwif->pioops                    = old_hwif.pioops;
1018 #else
1019         hwif->ata_input_data            = old_hwif.ata_input_data;
1020         hwif->ata_output_data           = old_hwif.ata_output_data;
1021         hwif->atapi_input_bytes         = old_hwif.atapi_input_bytes;
1022         hwif->atapi_output_bytes        = old_hwif.atapi_output_bytes;
1023 #endif
1024
1025 #if 0
1026         hwif->dmaops                    = old_hwif.dmaops;
1027 #else
1028         hwif->ide_dma_read              = old_hwif.ide_dma_read;
1029         hwif->ide_dma_write             = old_hwif.ide_dma_write;
1030         hwif->ide_dma_begin             = old_hwif.ide_dma_begin;
1031         hwif->ide_dma_end               = old_hwif.ide_dma_end;
1032         hwif->ide_dma_check             = old_hwif.ide_dma_check;
1033         hwif->ide_dma_on                = old_hwif.ide_dma_on;
1034         hwif->ide_dma_off               = old_hwif.ide_dma_off;
1035         hwif->ide_dma_off_quietly       = old_hwif.ide_dma_off_quietly;
1036         hwif->ide_dma_test_irq          = old_hwif.ide_dma_test_irq;
1037         hwif->ide_dma_host_on           = old_hwif.ide_dma_host_on;
1038         hwif->ide_dma_host_off          = old_hwif.ide_dma_host_off;
1039         hwif->ide_dma_bad_drive         = old_hwif.ide_dma_bad_drive;
1040         hwif->ide_dma_good_drive        = old_hwif.ide_dma_good_drive;
1041         hwif->ide_dma_count             = old_hwif.ide_dma_count;
1042         hwif->ide_dma_verbose           = old_hwif.ide_dma_verbose;
1043         hwif->ide_dma_retune            = old_hwif.ide_dma_retune;
1044         hwif->ide_dma_lostirq           = old_hwif.ide_dma_lostirq;
1045         hwif->ide_dma_timeout           = old_hwif.ide_dma_timeout;
1046 #endif
1047
1048 #if 0
1049         hwif->iops                      = old_hwif.iops;
1050 #else
1051         hwif->OUTB              = old_hwif.OUTB;
1052         hwif->OUTBSYNC          = old_hwif.OUTBSYNC;
1053         hwif->OUTW              = old_hwif.OUTW;
1054         hwif->OUTL              = old_hwif.OUTL;
1055         hwif->OUTSW             = old_hwif.OUTSW;
1056         hwif->OUTSL             = old_hwif.OUTSL;
1057
1058         hwif->INB               = old_hwif.INB;
1059         hwif->INW               = old_hwif.INW;
1060         hwif->INL               = old_hwif.INL;
1061         hwif->INSW              = old_hwif.INSW;
1062         hwif->INSL              = old_hwif.INSL;
1063 #endif
1064
1065         hwif->mmio                      = old_hwif.mmio;
1066         hwif->rqsize                    = old_hwif.rqsize;
1067         hwif->addressing                = old_hwif.addressing;
1068 #ifndef CONFIG_BLK_DEV_IDECS
1069         hwif->irq                       = old_hwif.irq;
1070 #endif /* CONFIG_BLK_DEV_IDECS */
1071         hwif->initializing              = old_hwif.initializing;
1072
1073         hwif->dma_base                  = old_hwif.dma_base;
1074         hwif->dma_master                = old_hwif.dma_master;
1075         hwif->dma_command               = old_hwif.dma_command;
1076         hwif->dma_vendor1               = old_hwif.dma_vendor1;
1077         hwif->dma_status                = old_hwif.dma_status;
1078         hwif->dma_vendor3               = old_hwif.dma_vendor3;
1079         hwif->dma_prdtable              = old_hwif.dma_prdtable;
1080
1081         hwif->dma_extra                 = old_hwif.dma_extra;
1082         hwif->config_data               = old_hwif.config_data;
1083         hwif->select_data               = old_hwif.select_data;
1084         hwif->autodma                   = old_hwif.autodma;
1085         hwif->udma_four                 = old_hwif.udma_four;
1086         hwif->no_dsc                    = old_hwif.no_dsc;
1087
1088         hwif->hwif_data                 = old_hwif.hwif_data;
1089         spin_unlock_irqrestore(&io_request_lock, flags);
1090         return 0;
1091
1092 abort:
1093         spin_unlock_irqrestore(&io_request_lock, flags);
1094         return 1;
1095         
1096 }
1097
1098 EXPORT_SYMBOL(ide_unregister);
1099
1100 /*
1101  * Setup hw_regs_t structure described by parameters.  You
1102  * may set up the hw structure yourself OR use this routine to
1103  * do it for you.
1104  */
1105 void ide_setup_ports (  hw_regs_t *hw,
1106                         ide_ioreg_t base, int *offsets,
1107                         ide_ioreg_t ctrl, ide_ioreg_t intr,
1108                         ide_ack_intr_t *ack_intr,
1109 /*
1110  *                      ide_io_ops_t *iops,
1111  */
1112                         int irq)
1113 {
1114         int i;
1115
1116         for (i = 0; i < IDE_NR_PORTS; i++) {
1117                 if (offsets[i] == -1) {
1118                         switch(i) {
1119                                 case IDE_CONTROL_OFFSET:
1120                                         hw->io_ports[i] = ctrl;
1121                                         break;
1122 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
1123                                 case IDE_IRQ_OFFSET:
1124                                         hw->io_ports[i] = intr;
1125                                         break;
1126 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
1127                                 default:
1128                                         hw->io_ports[i] = 0;
1129                                         break;
1130                         }
1131                 } else {
1132                         hw->io_ports[i] = base + offsets[i];
1133                 }
1134         }
1135         hw->irq = irq;
1136         hw->dma = NO_DMA;
1137         hw->ack_intr = ack_intr;
1138 /*
1139  *      hw->iops = iops;
1140  */
1141 }
1142
1143 EXPORT_SYMBOL(ide_setup_ports);
1144
1145 /*
1146  * Register an IDE interface, specifing exactly the registers etc
1147  * Set init=1 iff calling before probes have taken place.
1148  */
1149 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
1150 {
1151         int index, retry = 1;
1152         ide_hwif_t *hwif;
1153
1154         do {
1155                 for (index = 0; index < MAX_HWIFS; ++index) {
1156                         hwif = &ide_hwifs[index];
1157                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
1158                                 goto found;
1159                 }
1160                 for (index = 0; index < MAX_HWIFS; ++index) {
1161                         hwif = &ide_hwifs[index];
1162                         if (!hwif->hold && ((!hwif->present && !hwif->mate && !initializing) ||
1163                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing)))
1164                                 goto found;
1165                 }
1166                 for (index = 0; index < MAX_HWIFS; index++)
1167                         ide_unregister(index);
1168         } while (retry--);
1169         return -1;
1170 found:
1171         if (hwif->present)
1172                 ide_unregister(index);
1173         else if (!hwif->hold)
1174                 init_hwif_data(index);
1175         if (hwif->present)
1176                 return -1;
1177         memcpy(&hwif->hw, hw, sizeof(*hw));
1178         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
1179         hwif->irq = hw->irq;
1180         hwif->noprobe = 0;
1181         hwif->chipset = hw->chipset;
1182
1183         if (!initializing) {
1184                 ide_probe_module(1);
1185 #ifdef CONFIG_PROC_FS
1186                 create_proc_ide_interfaces();
1187 #endif
1188                 ide_driver_module(1);
1189         }
1190
1191         if (hwifp)
1192                 *hwifp = hwif;
1193
1194         return (initializing || hwif->present) ? index : -1;
1195 }
1196
1197 EXPORT_SYMBOL(ide_register_hw);
1198
1199 /*
1200  * Compatability function with existing drivers.  If you want
1201  * something different, use the function above.
1202  */
1203 int ide_register (int arg1, int arg2, int irq)
1204 {
1205         hw_regs_t hw;
1206         ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
1207         hw.irq = irq;
1208         return ide_register_hw(&hw, NULL);
1209 }
1210
1211 EXPORT_SYMBOL(ide_register);
1212
1213
1214 /*
1215  *      Locks for IDE setting functionality
1216  */
1217
1218 DECLARE_MUTEX(ide_setting_sem);
1219 EXPORT_SYMBOL(ide_setting_sem);
1220
1221 /**
1222  *      ide_add_setting -       add an ide setting option
1223  *      @drive: drive to use
1224  *      @name: setting name
1225  *      @rw: true if the function is read write
1226  *      @read_ioctl: function to call on read
1227  *      @write_ioctl: function to call on write
1228  *      @data_type: type of data
1229  *      @min: range minimum
1230  *      @max: range maximum
1231  *      @mul_factor: multiplication scale
1232  *      @div_factor: divison scale
1233  *      @data: private data field
1234  *      @set: setting
1235  *
1236  *      Removes the setting named from the device if it is present.
1237  *      The function takes the settings_lock to protect against 
1238  *      parallel changes. This function must not be called from IRQ
1239  *      context. Returns 0 on success or -1 on failure.
1240  *
1241  *      BUGS: This code is seriously over-engineered. There is also
1242  *      magic about how the driver specific features are setup. If
1243  *      a driver is attached we assume the driver settings are auto
1244  *      remove.
1245  */
1246  
1247 int ide_add_setting (ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
1248 {
1249         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1250
1251         down(&ide_setting_sem);
1252         while ((*p) && strcmp((*p)->name, name) < 0)
1253                 p = &((*p)->next);
1254         if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1255                 goto abort;
1256         memset(setting, 0, sizeof(*setting));
1257         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1258                 goto abort;
1259         strcpy(setting->name, name);
1260         setting->rw = rw;
1261         setting->read_ioctl = read_ioctl;
1262         setting->write_ioctl = write_ioctl;
1263         setting->data_type = data_type;
1264         setting->min = min;
1265         setting->max = max;
1266         setting->mul_factor = mul_factor;
1267         setting->div_factor = div_factor;
1268         setting->data = data;
1269         setting->set = set;
1270         
1271         setting->next = *p;
1272         if (drive->driver != &idedefault_driver)
1273                 setting->auto_remove = 1;
1274         *p = setting;
1275         up(&ide_setting_sem);
1276         return 0;
1277 abort:
1278         up(&ide_setting_sem);
1279         if (setting)
1280                 kfree(setting);
1281         return -1;
1282 }
1283
1284 EXPORT_SYMBOL(ide_add_setting);
1285
1286 /**
1287  *      __ide_remove_setting    -       remove an ide setting option
1288  *      @drive: drive to use
1289  *      @name: setting name
1290  *
1291  *      Removes the setting named from the device if it is present.
1292  *      The caller must hold the setting semaphore.
1293  */
1294  
1295 static void __ide_remove_setting (ide_drive_t *drive, char *name)
1296 {
1297         ide_settings_t **p, *setting;
1298
1299         p = (ide_settings_t **) &drive->settings;
1300
1301         while ((*p) && strcmp((*p)->name, name))
1302                 p = &((*p)->next);
1303         if ((setting = (*p)) == NULL)
1304                 return;
1305
1306         (*p) = setting->next;
1307         
1308         kfree(setting->name);
1309         kfree(setting);
1310 }
1311
1312 /**
1313  *      ide_remove_setting      -       remove an ide setting option
1314  *      @drive: drive to use
1315  *      @name: setting name
1316  *
1317  *      Removes the setting named from the device if it is present.
1318  *      The function takes the settings_lock to protect against 
1319  *      parallel changes. This function must not be called from IRQ
1320  *      context.
1321  */
1322  
1323 void ide_remove_setting (ide_drive_t *drive, char *name)
1324 {
1325         down(&ide_setting_sem);
1326         __ide_remove_setting(drive, name);
1327         up(&ide_setting_sem);
1328 }
1329
1330 EXPORT_SYMBOL(ide_remove_setting);
1331
1332 /**
1333  *      ide_find_setting_by_ioctl       -       find a drive specific ioctl
1334  *      @drive: drive to scan
1335  *      @cmd: ioctl command to handle
1336  *
1337  *      Scan's the device setting table for a matching entry and returns
1338  *      this or NULL if no entry is found. The caller must hold the
1339  *      setting semaphore
1340  */
1341  
1342 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1343 {
1344         ide_settings_t *setting = drive->settings;
1345
1346         while (setting) {
1347                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1348                         break;
1349                 setting = setting->next;
1350         }
1351         
1352         return setting;
1353 }
1354
1355 /**
1356  *      ide_find_setting_by_name        -       find a drive specific setting
1357  *      @drive: drive to scan
1358  *      @name: setting name
1359  *
1360  *      Scan's the device setting table for a matching entry and returns
1361  *      this or NULL if no entry is found. The caller must hold the
1362  *      setting semaphore
1363  */
1364  
1365 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1366 {
1367         ide_settings_t *setting = drive->settings;
1368
1369         while (setting) {
1370                 if (strcmp(setting->name, name) == 0)
1371                         break;
1372                 setting = setting->next;
1373         }
1374         return setting;
1375 }
1376
1377 /**
1378  *      auto_remove_settings    -       remove driver specific settings
1379  *      @drive: drive
1380  *
1381  *      Automatically remove all the driver specific settings for this
1382  *      drive. This function may sleep and must not be called from IRQ
1383  *      context. Caller must hold the setting lock.
1384  */
1385  
1386 static void auto_remove_settings (ide_drive_t *drive)
1387 {
1388         ide_settings_t *setting;
1389 repeat:
1390         setting = drive->settings;
1391         while (setting) {
1392                 if (setting->auto_remove) {
1393                         __ide_remove_setting(drive, setting->name);
1394                         goto repeat;
1395                 }
1396                 setting = setting->next;
1397         }
1398 }
1399
1400 /**
1401  *      ide_read_setting        -       read an IDE setting
1402  *      @drive: drive to read from
1403  *      @setting: drive setting
1404  *
1405  *      Read a drive setting and return the value. The caller
1406  *      must hold the ide_setting_sem when making this call.
1407  *
1408  *      BUGS: the data return and error are the same return value
1409  *      so an error -EINVAL and true return of the same value cannot
1410  *      be told apart
1411  */
1412  
1413 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1414 {
1415         int             val = -EINVAL;
1416         unsigned long   flags;
1417
1418         if ((setting->rw & SETTING_READ)) {
1419                 spin_lock_irqsave(&io_request_lock, flags);
1420                 switch(setting->data_type) {
1421                         case TYPE_BYTE:
1422                                 val = *((u8 *) setting->data);
1423                                 break;
1424                         case TYPE_SHORT:
1425                                 val = *((u16 *) setting->data);
1426                                 break;
1427                         case TYPE_INT:
1428                         case TYPE_INTA:
1429                                 val = *((u32 *) setting->data);
1430                                 break;
1431                 }
1432                 spin_unlock_irqrestore(&io_request_lock, flags);
1433         }
1434         return val;
1435 }
1436
1437 int ide_spin_wait_hwgroup (ide_drive_t *drive)
1438 {
1439         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1440         unsigned long timeout = jiffies + (3 * HZ);
1441
1442         spin_lock_irq(&io_request_lock);
1443
1444         while (hwgroup->busy) {
1445                 unsigned long lflags;
1446                 spin_unlock_irq(&io_request_lock);
1447                 local_irq_set(lflags);
1448                 if (time_after(jiffies, timeout)) {
1449                         local_irq_restore(lflags);
1450                         printk(KERN_ERR "%s: channel busy\n", drive->name);
1451                         return -EBUSY;
1452                 }
1453                 local_irq_restore(lflags);
1454                 spin_lock_irq(&io_request_lock);
1455         }
1456         return 0;
1457 }
1458
1459 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1460
1461 /**
1462  *      ide_write_setting       -       read an IDE setting
1463  *      @drive: drive to read from
1464  *      @setting: drive setting
1465  *      @val: value
1466  *
1467  *      Write a drive setting if it is possible. The caller
1468  *      must hold the ide_setting_sem when making this call.
1469  *
1470  *      BUGS: the data return and error are the same return value
1471  *      so an error -EINVAL and true return of the same value cannot
1472  *      be told apart
1473  *
1474  *      FIXME:  This should be changed to enqueue a special request
1475  *      to the driver to change settings, and then wait on a sema for completion.
1476  *      The current scheme of polling is kludgy, though safe enough.
1477  */
1478 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1479 {
1480         int i;
1481         u32 *p;
1482
1483         if (!capable(CAP_SYS_ADMIN))
1484                 return -EACCES;
1485         if (!(setting->rw & SETTING_WRITE))
1486                 return -EPERM;
1487         if (val < setting->min || val > setting->max)
1488                 return -EINVAL;
1489         if (setting->set)
1490                 return setting->set(drive, val);
1491         if (ide_spin_wait_hwgroup(drive))
1492                 return -EBUSY;
1493         switch (setting->data_type) {
1494                 case TYPE_BYTE:
1495                         *((u8 *) setting->data) = val;
1496                         break;
1497                 case TYPE_SHORT:
1498                         *((u16 *) setting->data) = val;
1499                         break;
1500                 case TYPE_INT:
1501                         *((u32 *) setting->data) = val;
1502                         break;
1503                 case TYPE_INTA:
1504                         p = (u32 *) setting->data;
1505                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1506                                 *p = val;
1507                         break;
1508         }
1509         spin_unlock_irq(&io_request_lock);
1510         return 0;
1511 }
1512
1513 EXPORT_SYMBOL(ide_write_setting);
1514
1515 static int set_io_32bit(ide_drive_t *drive, int arg)
1516 {
1517         drive->io_32bit = arg;
1518 #ifdef CONFIG_BLK_DEV_DTC2278
1519         if (HWIF(drive)->chipset == ide_dtc2278)
1520                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1521 #endif /* CONFIG_BLK_DEV_DTC2278 */
1522         return 0;
1523 }
1524
1525 static int set_using_dma (ide_drive_t *drive, int arg)
1526 {
1527         if (!DRIVER(drive)->supports_dma)
1528                 return -EPERM;
1529         if (!(drive->id->capability & 1))
1530                 return -EPERM;
1531         if (HWIF(drive)->ide_dma_check == NULL)
1532                 return -EPERM;
1533         if (HWIF(drive)->ide_dma_check(drive) != 0)
1534                 return -EIO;
1535         if (arg) {
1536                 if (HWIF(drive)->ide_dma_check(drive) != 0)
1537                         return -EIO;
1538                 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1539         } else {
1540                 if (HWIF(drive)->ide_dma_off(drive)) return -EIO;
1541         }
1542         return 0;
1543 }
1544
1545 static int set_pio_mode (ide_drive_t *drive, int arg)
1546 {
1547         struct request rq;
1548
1549         if (!HWIF(drive)->tuneproc)
1550                 return -ENOSYS;
1551         if (drive->special.b.set_tune)
1552                 return -EBUSY;
1553         ide_init_drive_cmd(&rq);
1554         drive->tune_req = (u8) arg;
1555         drive->special.b.set_tune = 1;
1556         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1557         return 0;
1558 }
1559
1560 static int set_xfer_rate (ide_drive_t *drive, int arg)
1561 {
1562         int err = ide_wait_cmd(drive,
1563                         WIN_SETFEATURES, (u8) arg,
1564                         SETFEATURES_XFER, 0, NULL);
1565
1566         if (!err && arg) {
1567                 ide_set_xfer_rate(drive, (u8) arg);
1568                 ide_driveid_update(drive);
1569         }
1570         return err;
1571 }
1572
1573 int ide_atapi_to_scsi (ide_drive_t *drive, int arg)
1574 {
1575         if (drive->media == ide_disk) {
1576                 drive->scsi = 0;
1577                 return 0;
1578         }
1579         if (DRIVER(drive)->cleanup(drive)) {
1580                 drive->scsi = 0;
1581                 return 0;
1582         }
1583         drive->scsi = (u8) arg;
1584         ide_attach_drive(drive);
1585         return 0;
1586 }
1587
1588 void ide_add_generic_settings (ide_drive_t *drive)
1589 {
1590 /*
1591  *                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function
1592  */
1593         ide_add_setting(drive,  "io_32bit",             drive->no_io_32bit ? SETTING_READ : SETTING_RW, HDIO_GET_32BIT,         HDIO_SET_32BIT,         TYPE_BYTE,      0,      1 + (SUPPORT_VLB_SYNC << 1),    1,              1,              &drive->io_32bit,               set_io_32bit);
1594         ide_add_setting(drive,  "keepsettings",         SETTING_RW,                                     HDIO_GET_KEEPSETTINGS,  HDIO_SET_KEEPSETTINGS,  TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL);
1595         ide_add_setting(drive,  "nice1",                SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL);
1596         ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
1597         ide_add_setting(drive,  "slow",                 SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->slow,                   NULL);
1598         ide_add_setting(drive,  "unmaskirq",            drive->no_unmask ? SETTING_READ : SETTING_RW,   HDIO_GET_UNMASKINTR,    HDIO_SET_UNMASKINTR,    TYPE_BYTE,      0,      1,                              1,              1,              &drive->unmask,                 NULL);
1599         ide_add_setting(drive,  "using_dma",            SETTING_RW,                                     HDIO_GET_DMA,           HDIO_SET_DMA,           TYPE_BYTE,      0,      1,                              1,              1,              &drive->using_dma,              set_using_dma);
1600         ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->init_speed,             NULL);
1601         ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->current_speed,          set_xfer_rate);
1602         ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
1603 #if 0
1604         /* Experimental, but this needs the setting/register locking rewritten to be used */    
1605         if (drive->media != ide_disk)
1606                 ide_add_setting(drive,  "ide-scsi",             SETTING_RW,                                     -1,             HDIO_SET_IDE_SCSI,              TYPE_BYTE,      0,      1,                              1,              1,              &drive->scsi,                   ide_atapi_to_scsi);
1607 #endif          
1608 }
1609
1610 EXPORT_SYMBOL_GPL(ide_add_generic_settings);
1611
1612 /*
1613  * Delay for *at least* 50ms.  As we don't know how much time is left
1614  * until the next tick occurs, we wait an extra tick to be safe.
1615  * This is used only during the probing/polling for drives at boot time.
1616  *
1617  * However, its usefullness may be needed in other places, thus we export it now.
1618  * The future may change this to a millisecond setable delay.
1619  */
1620 void ide_delay_50ms (void)
1621 {
1622 #ifndef CONFIG_BLK_DEV_IDECS
1623         mdelay(50);
1624 #else
1625         __set_current_state(TASK_UNINTERRUPTIBLE);
1626         schedule_timeout(1+HZ/20);
1627 #endif /* CONFIG_BLK_DEV_IDECS */
1628 }
1629
1630 EXPORT_SYMBOL(ide_delay_50ms);
1631
1632 int system_bus_clock (void)
1633 {
1634         return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1635 }
1636
1637 EXPORT_SYMBOL(system_bus_clock);
1638
1639 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1640 {
1641         if (!drive->present || drive->busy || drive->usage || drive->dead)
1642                 goto abort;
1643         if (DRIVER(drive)->cleanup(drive))
1644                 goto abort;
1645         strncpy(drive->driver_req, driver, 9);
1646         ide_driver_module(0);
1647         drive->driver_req[0] = 0;
1648         ide_driver_module(0);
1649         if (!strcmp(DRIVER(drive)->name, driver))
1650                 return 0;
1651 abort:
1652         return 1;
1653 }
1654
1655 EXPORT_SYMBOL(ide_replace_subdriver);
1656
1657 int ide_attach_drive (ide_drive_t *drive)
1658 {
1659         /* Someone unplugged the device on us */
1660         if(drive->dead)
1661                 return 1;
1662                 
1663 #ifdef CONFIG_BLK_DEV_IDESCSI
1664         if (drive->scsi) {
1665                 extern int idescsi_attach(ide_drive_t *drive);
1666                 if (idescsi_attach(drive))
1667                         return 0;
1668         }
1669 #endif /* CONFIG_BLK_DEV_IDESCSI */
1670
1671         switch (drive->media) {
1672 #ifdef CONFIG_BLK_DEV_IDECD
1673                 case ide_cdrom:
1674                 {
1675                         extern int ide_cdrom_attach(ide_drive_t *drive);
1676                         if (ide_cdrom_attach(drive))
1677                                 return 1;
1678                         break;
1679                 }
1680 #endif /* CONFIG_BLK_DEV_IDECD */
1681 #ifdef CONFIG_BLK_DEV_IDEDISK
1682                 case ide_disk:
1683                 {
1684                         extern int idedisk_attach(ide_drive_t *drive);
1685                         if (idedisk_attach(drive))
1686                                 return 1;
1687                         break;
1688                 }
1689 #endif /* CONFIG_BLK_DEV_IDEDISK */
1690 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
1691                 case ide_floppy:
1692                 {
1693                         extern int idefloppy_attach(ide_drive_t *drive);
1694                         if (idefloppy_attach(drive))
1695                                 return 1;
1696                         break;
1697                 }
1698 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
1699 #ifdef CONFIG_BLK_DEV_IDETAPE
1700                 case ide_tape:
1701                 {
1702                         extern int idetape_attach(ide_drive_t *drive);
1703                         if (idetape_attach(drive))
1704                                 return 1;
1705                         break;
1706                 }
1707 #endif /* CONFIG_BLK_DEV_IDETAPE */
1708                 default:
1709                 {
1710                         extern int idedefault_attach(ide_drive_t *drive);
1711                         if(idedefault_attach(drive))
1712                                 printk(KERN_CRIT "ide: failed to attach default driver.\n");
1713                         return 1;
1714                 }
1715         }
1716         return 0;
1717 }
1718
1719 EXPORT_SYMBOL(ide_attach_drive);
1720
1721 static int ide_ioctl (struct inode *inode, struct file *file,
1722                         unsigned int cmd, unsigned long arg)
1723 {
1724         int err = 0, major, minor;
1725         ide_drive_t *drive;
1726         struct request rq;
1727         kdev_t dev;
1728         ide_settings_t *setting;
1729         int force = 0;
1730         
1731         if (!inode || !(dev = inode->i_rdev))
1732                 return -EINVAL;
1733                 
1734         switch(cmd)
1735         {
1736                 case HDIO_GET_BUSSTATE:
1737                 case HDIO_SET_BUSSTATE:
1738                 case HDIO_SCAN_HWIF:
1739                 case HDIO_UNREGISTER_HWIF:
1740                         force = 1;
1741         }
1742         
1743         major = MAJOR(dev); minor = MINOR(dev);
1744         if ((drive = ide_info_ptr(inode->i_rdev, force)) == NULL)
1745                 return -ENODEV;
1746
1747         down(&ide_setting_sem);
1748         if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1749                 if (cmd == setting->read_ioctl) {
1750                         err = ide_read_setting(drive, setting);
1751                         up(&ide_setting_sem);
1752                         return err >= 0 ? put_user(err, (long *) arg) : err;
1753                 } else {
1754                         if ((MINOR(inode->i_rdev) & PARTN_MASK))
1755                                 err = -EINVAL;
1756                         else 
1757                                 err = ide_write_setting(drive, setting, arg);
1758                         up(&ide_setting_sem);
1759                         return err;
1760                 }
1761         }
1762         up(&ide_setting_sem);
1763
1764         ide_init_drive_cmd (&rq);
1765         switch (cmd) {
1766                 case HDIO_GETGEO:
1767                 {
1768                         struct hd_geometry *loc = (struct hd_geometry *) arg;
1769                         u16 bios_cyl = drive->bios_cyl; /* truncate */
1770                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1771                         if (put_user(drive->bios_head, (u8 *) &loc->heads)) return -EFAULT;
1772                         if (put_user(drive->bios_sect, (u8 *) &loc->sectors)) return -EFAULT;
1773                         if (put_user(bios_cyl, (u16 *) &loc->cylinders)) return -EFAULT;
1774                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1775                                 (unsigned long *) &loc->start)) return -EFAULT;
1776                         return 0;
1777                 }
1778
1779                 case HDIO_GETGEO_BIG:
1780                 {
1781                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
1782                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1783                         if (put_user(drive->bios_head, (u8 *) &loc->heads)) return -EFAULT;
1784                         if (put_user(drive->bios_sect, (u8 *) &loc->sectors)) return -EFAULT;
1785                         if (put_user(drive->bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
1786                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1787                                 (unsigned long *) &loc->start)) return -EFAULT;
1788                         return 0;
1789                 }
1790
1791                 case HDIO_GETGEO_BIG_RAW:
1792                 {
1793                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
1794                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1795                         if (put_user(drive->head, (u8 *) &loc->heads)) return -EFAULT;
1796                         if (put_user(drive->sect, (u8 *) &loc->sectors)) return -EFAULT;
1797                         if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
1798                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1799                                 (unsigned long *) &loc->start)) return -EFAULT;
1800                         return 0;
1801                 }
1802
1803                 case BLKGETSIZE:   /* Return device size */
1804                         return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (unsigned long *) arg);
1805                 case BLKGETSIZE64:
1806                         return put_user((u64)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects << 9, (u64 *) arg);
1807
1808                 case BLKRRPART: /* Re-read partition tables */
1809                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1810                         return ide_revalidate_disk(inode->i_rdev);
1811
1812                 case HDIO_OBSOLETE_IDENTITY:
1813                 case HDIO_GET_IDENTITY:
1814                         if (MINOR(inode->i_rdev) & PARTN_MASK)
1815                                 return -EINVAL;
1816                         if (drive->id_read == 0)
1817                                 return -ENOMSG;
1818                         if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1819                                 return -EFAULT;
1820                         return 0;
1821
1822                 case HDIO_GET_NICE:
1823                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
1824                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
1825                                         drive->nice0            <<      IDE_NICE_0              |
1826                                         drive->nice1            <<      IDE_NICE_1              |
1827                                         drive->nice2            <<      IDE_NICE_2,
1828                                         (long *) arg);
1829
1830 #ifdef CONFIG_IDE_TASK_IOCTL
1831                 case HDIO_DRIVE_TASKFILE:
1832                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1833                                 return -EACCES;
1834                         switch(drive->media) {
1835                                 case ide_disk:
1836                                         return ide_taskfile_ioctl(drive, inode, file, cmd, arg);
1837 #ifdef CONFIG_PKT_TASK_IOCTL
1838                                 case ide_cdrom:
1839                                 case ide_tape:
1840                                 case ide_floppy:
1841                                         return pkt_taskfile_ioctl(drive, inode, file, cmd, arg);
1842 #endif /* CONFIG_PKT_TASK_IOCTL */
1843                                 default:
1844                                         return -ENOMSG;
1845                         }
1846 #endif /* CONFIG_IDE_TASK_IOCTL */
1847
1848                 case HDIO_DRIVE_CMD:
1849                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1850                                 return -EACCES;
1851                         return ide_cmd_ioctl(drive, inode, file, cmd, arg);
1852
1853                 case HDIO_DRIVE_TASK:
1854                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1855                                 return -EACCES;
1856                         return ide_task_ioctl(drive, inode, file, cmd, arg);
1857
1858                 case HDIO_SCAN_HWIF:
1859                 {
1860                         int args[3];
1861                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1862                         if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
1863                                 return -EFAULT;
1864                         if (ide_register(args[0], args[1], args[2]) == -1)
1865                                 return -EIO;
1866                         return 0;
1867                 }
1868                 case HDIO_UNREGISTER_HWIF:
1869                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1870                         /* (arg > MAX_HWIFS) checked in function */
1871                         ide_unregister(arg);
1872                         return 0;
1873                 case HDIO_SET_NICE:
1874                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1875                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1876                                 return -EPERM;
1877                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1878                         if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1879                                 drive->dsc_overlap = 0;
1880                                 return -EPERM;
1881                         }
1882                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
1883                         return 0;
1884                 case HDIO_DRIVE_RESET:
1885                 {
1886                         unsigned long flags;
1887                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1888                         
1889                         /*
1890                          *      Abort the current command on the
1891                          *      group if there is one, taking
1892                          *      care not to allow anything else
1893                          *      to be queued and to die on the
1894                          *      spot if we miss one somehow
1895                          */
1896
1897                         spin_lock_irqsave(&io_request_lock, flags);
1898                         
1899                         DRIVER(drive)->abort(drive, "drive reset");
1900                         if(HWGROUP(drive)->handler)
1901                                 BUG();
1902                                 
1903                         /* Ensure nothing gets queued after we
1904                            drop the lock. Reset will clear the busy */
1905                            
1906                         HWGROUP(drive)->busy = 1;
1907                         spin_unlock_irqrestore(&io_request_lock, flags);
1908
1909                         (void) ide_do_reset(drive);
1910                         if (drive->suspend_reset) {
1911 /*
1912  *                              APM WAKE UP todo !!
1913  *                              int nogoodpower = 1;
1914  *                              while(nogoodpower) {
1915  *                                      check_power1() or check_power2()
1916  *                                      nogoodpower = 0;
1917  *                              } 
1918  *                              HWIF(drive)->multiproc(drive);
1919  */
1920                                 return ide_revalidate_disk(inode->i_rdev);
1921                         }
1922                         return 0;
1923                 }
1924                 case BLKROSET:
1925                 case BLKROGET:
1926                 case BLKFLSBUF:
1927                 case BLKSSZGET:
1928                 case BLKPG:
1929                 case BLKELVGET:
1930                 case BLKELVSET:
1931                 case BLKBSZGET:
1932                 case BLKBSZSET:
1933                         return blk_ioctl(inode->i_rdev, cmd, arg);
1934
1935                 case HDIO_GET_BUSSTATE:
1936                         if (!capable(CAP_SYS_ADMIN))
1937                                 return -EACCES;
1938                         if (put_user(HWIF(drive)->bus_state, (long *)arg))
1939                                 return -EFAULT;
1940                         return 0;
1941
1942                 case HDIO_SET_BUSSTATE:
1943                 {
1944                         ide_hwif_t *hwif =  HWIF(drive);
1945                         
1946                         if (!capable(CAP_SYS_ADMIN))
1947                                 return -EACCES;
1948 #ifdef OLD_STUFF
1949                         if (hwif->busproc)
1950                                 return HWIF(drive)->busproc(drive, (int)arg);
1951                         return -EOPNOTSUPP;
1952 #else
1953                         if(hwif->bus_state == arg)
1954                                 return 0;
1955                                 
1956                         if(hwif->bus_state == BUSSTATE_ON)
1957                         {
1958                                 /* "drive" may vanish beyond here */
1959                                 if((err = ide_prepare_tristate(drive)) != 0)
1960                                         return err;
1961                                 hwif->bus_state = arg;
1962                         }
1963                         if (hwif->busproc)
1964                         {
1965                                 err = hwif->busproc(drive, (int)arg);
1966                                 if(err)
1967                                         return err;
1968                         }
1969                         if(arg != BUSSTATE_OFF)
1970                         {
1971                                 err = ide_resume_hwif(drive);
1972                                 hwif->bus_state = arg;
1973                                 if(err)
1974                                         return err;
1975                         }
1976                         return 0;
1977 #endif                  
1978                 }                               
1979
1980                 default:
1981                         return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
1982                         return -EPERM;
1983         }
1984 }
1985
1986 static int ide_check_media_change (kdev_t i_rdev)
1987 {
1988         ide_drive_t *drive;
1989
1990         if ((drive = ide_info_ptr(i_rdev, 0)) == NULL)
1991                 return -ENODEV;
1992         return DRIVER(drive)->media_change(drive);
1993 }
1994
1995 /*
1996  * stridx() returns the offset of c within s,
1997  * or -1 if c is '\0' or not found within s.
1998  */
1999 static int __init stridx (const char *s, char c)
2000 {
2001         char *i = strchr(s, c);
2002         return (i && c) ? i - s : -1;
2003 }
2004
2005 /*
2006  * match_parm() does parsing for ide_setup():
2007  *
2008  * 1. the first char of s must be '='.
2009  * 2. if the remainder matches one of the supplied keywords,
2010  *     the index (1 based) of the keyword is negated and returned.
2011  * 3. if the remainder is a series of no more than max_vals numbers
2012  *     separated by commas, the numbers are saved in vals[] and a
2013  *     count of how many were saved is returned.  Base10 is assumed,
2014  *     and base16 is allowed when prefixed with "0x".
2015  * 4. otherwise, zero is returned.
2016  */
2017 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2018 {
2019         static const char *decimal = "0123456789";
2020         static const char *hex = "0123456789abcdef";
2021         int i, n;
2022
2023         if (*s++ == '=') {
2024                 /*
2025                  * Try matching against the supplied keywords,
2026                  * and return -(index+1) if we match one
2027                  */
2028                 if (keywords != NULL) {
2029                         for (i = 0; *keywords != NULL; ++i) {
2030                                 if (!strcmp(s, *keywords++))
2031                                         return -(i+1);
2032                         }
2033                 }
2034                 /*
2035                  * Look for a series of no more than "max_vals"
2036                  * numeric values separated by commas, in base10,
2037                  * or base16 when prefixed with "0x".
2038                  * Return a count of how many were found.
2039                  */
2040                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2041                         vals[n] = i;
2042                         while ((i = stridx(decimal, *++s)) >= 0)
2043                                 vals[n] = (vals[n] * 10) + i;
2044                         if (*s == 'x' && !vals[n]) {
2045                                 while ((i = stridx(hex, *++s)) >= 0)
2046                                         vals[n] = (vals[n] * 0x10) + i;
2047                         }
2048                         if (++n == max_vals)
2049                                 break;
2050                         if (*s == ',' || *s == ';')
2051                                 ++s;
2052                 }
2053                 if (!*s)
2054                         return n;
2055         }
2056         return 0;       /* zero = nothing matched */
2057 }
2058
2059 /*
2060  * ide_setup() gets called VERY EARLY during initialization,
2061  * to handle kernel "command line" strings beginning with "hdx="
2062  * or "ide".  Here is the complete set currently supported:
2063  *
2064  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
2065  * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2066  *
2067  * "hdx=noprobe"        : drive may be present, but do not probe for it
2068  * "hdx=none"           : drive is NOT present, ignore cmos and do not probe
2069  * "hdx=nowerr"         : ignore the WRERR_STAT bit on this drive
2070  * "hdx=cdrom"          : drive is present, and is a cdrom drive
2071  * "hdx=cyl,head,sect"  : disk drive is present, with specified geometry
2072  * "hdx=noremap"        : do not remap 0->1 even though EZD was detected
2073  * "hdx=autotune"       : driver will attempt to tune interface speed
2074  *                              to the fastest PIO mode supported,
2075  *                              if possible for this drive only.
2076  *                              Not fully supported by all chipset types,
2077  *                              and quite likely to cause trouble with
2078  *                              older/odd IDE drives.
2079  *
2080  * "hdx=slow"           : insert a huge pause after each access to the data
2081  *                              port. Should be used only as a last resort.
2082  *
2083  * "hdx=swapdata"       : when the drive is a disk, byte swap all data
2084  * "hdx=bswap"          : same as above..........
2085  * "hdxlun=xx"          : set the drive last logical unit.
2086  * "hdx=flash"          : allows for more than one ata_flash disk to be
2087  *                              registered. In most cases, only one device
2088  *                              will be present.
2089  * "hdx=scsi"           : the return of the ide-scsi flag, this is useful for
2090  *                              allowwing ide-floppy, ide-tape, and ide-cdrom|writers
2091  *                              to use ide-scsi emulation on a device specific option.
2092  * "idebus=xx"          : inform IDE driver of VESA/PCI bus speed in MHz,
2093  *                              where "xx" is between 20 and 66 inclusive,
2094  *                              used when tuning chipset PIO modes.
2095  *                              For PCI bus, 25 is correct for a P75 system,
2096  *                              30 is correct for P90,P120,P180 systems,
2097  *                              and 33 is used for P100,P133,P166 systems.
2098  *                              If in doubt, use idebus=33 for PCI.
2099  *                              As for VLB, it is safest to not specify it.
2100  *
2101  * "idex=noprobe"       : do not attempt to access/use this interface
2102  * "idex=base"          : probe for an interface at the addr specified,
2103  *                              where "base" is usually 0x1f0 or 0x170
2104  *                              and "ctl" is assumed to be "base"+0x206
2105  * "idex=base,ctl"      : specify both base and ctl
2106  * "idex=base,ctl,irq"  : specify base, ctl, and irq number
2107  * "idex=autotune"      : driver will attempt to tune interface speed
2108  *                              to the fastest PIO mode supported,
2109  *                              for all drives on this interface.
2110  *                              Not fully supported by all chipset types,
2111  *                              and quite likely to cause trouble with
2112  *                              older/odd IDE drives.
2113  * "idex=noautotune"    : driver will NOT attempt to tune interface speed
2114  *                              This is the default for most chipsets,
2115  *                              except the cmd640.
2116  * "idex=serialize"     : do not overlap operations on idex and ide(x^1)
2117  * "idex=four"          : four drives on idex and ide(x^1) share same ports
2118  * "idex=reset"         : reset interface before first use
2119  * "idex=dma"           : enable DMA by default on both drives if possible
2120  * "idex=ata66"         : informs the interface that it has an 80c cable
2121  *                              for chipsets that are ATA-66 capable, but
2122  *                              the ablity to bit test for detection is
2123  *                              currently unknown.
2124  * "ide=reverse"        : Formerly called to pci sub-system, but now local.
2125  *
2126  * The following are valid ONLY on ide0, (except dc4030)
2127  * and the defaults for the base,ctl ports must not be altered.
2128  *
2129  * "ide0=dtc2278"       : probe/support DTC2278 interface
2130  * "ide0=ht6560b"       : probe/support HT6560B interface
2131  * "ide0=cmd640_vlb"    : *REQUIRED* for VLB cards with the CMD640 chip
2132  *                        (not for PCI -- automatically detected)
2133  * "ide0=qd65xx"        : probe/support qd65xx interface
2134  * "ide0=ali14xx"       : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2135  * "ide0=umc8672"       : probe/support umc8672 chipsets
2136  * "idex=dc4030"        : probe/support Promise DC4030VL interface
2137  * "ide=doubler"        : probe/support IDE doublers on Amiga
2138  */
2139 int __init ide_setup (char *s)
2140 {
2141         int i, vals[3];
2142         ide_hwif_t *hwif;
2143         ide_drive_t *drive;
2144         unsigned int hw, unit;
2145         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2146         const char max_hwif  = '0' + (MAX_HWIFS - 1);
2147
2148         
2149         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
2150                 return 0;                               /* driver and not us */
2151
2152         if (strncmp(s,"ide",3) &&
2153             strncmp(s,"idebus",6) &&
2154             strncmp(s,"hd",2))          /* hdx= & hdxlun= */
2155                 return 0;
2156
2157         printk(KERN_INFO "ide_setup: %s", s);
2158         init_ide_data ();
2159
2160 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2161         if (!strcmp(s, "ide=doubler")) {
2162                 extern int ide_doubler;
2163
2164                 printk(" : Enabled support for IDE doublers\n");
2165                 ide_doubler = 1;
2166                 return 1;
2167         }
2168 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
2169
2170         if (!strcmp(s, "ide=nodma")) {
2171                 printk(" : Prevented DMA\n");
2172                 noautodma = 1;
2173                 return 1;
2174         }
2175
2176 #ifdef CONFIG_BLK_DEV_IDEPCI
2177         if (!strcmp(s, "ide=reverse")) {
2178                 ide_scan_direction = 1;
2179                 printk(" : Enabled support for IDE inverse scan order.\n");
2180                 return 1;
2181         }
2182 #endif /* CONFIG_BLK_DEV_IDEPCI */
2183
2184         /*
2185          * Look for drive options:  "hdx="
2186          */
2187         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2188                 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2189                                 "serialize", "autotune", "noautotune",
2190                                 "slow", "swapdata", "bswap", "flash",
2191                                 "remap", "noremap", "scsi", NULL};
2192                 unit = s[2] - 'a';
2193                 hw   = unit / MAX_DRIVES;
2194                 unit = unit % MAX_DRIVES;
2195                 hwif = &ide_hwifs[hw];
2196                 drive = &hwif->drives[unit];
2197                 if (strncmp(s + 4, "ide-", 4) == 0) {
2198                         strncpy(drive->driver_req, s + 4, 9);
2199                         goto done;
2200                 }
2201                 /*
2202                  * Look for last lun option:  "hdxlun="
2203                  */
2204                 if (s[3] == 'l' && s[4] == 'u' && s[5] == 'n') {
2205                         if (match_parm(&s[6], NULL, vals, 1) != 1)
2206                                 goto bad_option;
2207                         if (vals[0] >= 0 && vals[0] <= 7) {
2208                                 drive->last_lun = vals[0];
2209                                 drive->forced_lun = 1;
2210                         } else
2211                                 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
2212                         goto done;
2213                 }
2214                 switch (match_parm(&s[3], hd_words, vals, 3)) {
2215                         case -1: /* "none" */
2216                                 drive->nobios = 1;  /* drop into "noprobe" */
2217                         case -2: /* "noprobe" */
2218                                 drive->noprobe = 1;
2219                                 goto done;
2220                         case -3: /* "nowerr" */
2221                                 drive->bad_wstat = BAD_R_STAT;
2222                                 hwif->noprobe = 0;
2223                                 goto done;
2224                         case -4: /* "cdrom" */
2225                                 drive->present = 1;
2226                                 drive->media = ide_cdrom;
2227                                 hwif->noprobe = 0;
2228                                 goto done;
2229                         case -5: /* "serialize" */
2230                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2231                                 goto do_serialize;
2232                         case -6: /* "autotune" */
2233                                 drive->autotune = 1;
2234                                 goto done;
2235                         case -7: /* "noautotune" */
2236                                 drive->autotune = 2;
2237                                 goto done;
2238                         case -8: /* "slow" */
2239                                 drive->slow = 1;
2240                                 goto done;
2241                         case -9: /* "swapdata" or "bswap" */
2242                         case -10:
2243                                 drive->bswap = 1;
2244                                 goto done;
2245                         case -11: /* "flash" */
2246                                 drive->ata_flash = 1;
2247                                 goto done;
2248                         case -12: /* "remap" */
2249                                 drive->remap_0_to_1 = 1;
2250                                 goto done;
2251                         case -13: /* "noremap" */
2252                                 drive->remap_0_to_1 = 2;
2253                                 goto done;
2254                         case -14: /* "scsi" */
2255                                 drive->scsi = 1;
2256                                 goto done;
2257                         case 3: /* cyl,head,sect */
2258                                 drive->media    = ide_disk;
2259                                 drive->cyl      = drive->bios_cyl  = vals[0];
2260                                 drive->head     = drive->bios_head = vals[1];
2261                                 drive->sect     = drive->bios_sect = vals[2];
2262                                 drive->present  = 1;
2263                                 drive->forced_geom = 1;
2264                                 hwif->noprobe = 0;
2265                                 goto done;
2266                         default:
2267                                 goto bad_option;
2268                 }
2269         }
2270
2271         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2272                 goto bad_option;
2273         /*
2274          * Look for bus speed option:  "idebus="
2275          */
2276         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
2277                 if (match_parm(&s[6], NULL, vals, 1) != 1)
2278                         goto bad_option;
2279                 if (vals[0] >= 20 && vals[0] <= 66) {
2280                         idebus_parameter = vals[0];
2281                 } else
2282                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2283                 goto done;
2284         }
2285         /*
2286          * Look for interface options:  "idex="
2287          */
2288         if (s[3] >= '0' && s[3] <= max_hwif) {
2289                 /*
2290                  * Be VERY CAREFUL changing this: note hardcoded indexes below
2291                  * -8,-9,-10 : are reserved for future idex calls to ease the hardcoding.
2292                  */
2293                 const char *ide_words[] = {
2294                         "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66",
2295                         "minus8", "minus9", "minus10",
2296                         "four", "qd65xx", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
2297                 hw = s[3] - '0';
2298                 hwif = &ide_hwifs[hw];
2299                 i = match_parm(&s[4], ide_words, vals, 3);
2300
2301                 /*
2302                  * Cryptic check to ensure chipset not already set for hwif:
2303                  */
2304                 if (i > 0 || i <= -11) {                        /* is parameter a chipset name? */
2305                         if (hwif->chipset != ide_unknown)
2306                                 goto bad_option;        /* chipset already specified */
2307                         if (i <= -11 && i != -18 && hw != 0)
2308                                 goto bad_hwif;          /* chipset drivers are for "ide0=" only */
2309                         if (i <= -11 && i != -18 && ide_hwifs[hw+1].chipset != ide_unknown)
2310                                 goto bad_option;        /* chipset for 2nd port already specified */
2311                         printk("\n");
2312                 }
2313
2314                 switch (i) {
2315 #ifdef CONFIG_BLK_DEV_PDC4030
2316                         case -18: /* "dc4030" */
2317                         {
2318                                 extern void init_pdc4030(void);
2319                                 init_pdc4030();
2320                                 goto done;
2321                         }
2322 #endif /* CONFIG_BLK_DEV_PDC4030 */
2323 #ifdef CONFIG_BLK_DEV_ALI14XX
2324                         case -17: /* "ali14xx" */
2325                         {
2326                                 extern void init_ali14xx (void);
2327                                 init_ali14xx();
2328                                 goto done;
2329                         }
2330 #endif /* CONFIG_BLK_DEV_ALI14XX */
2331 #ifdef CONFIG_BLK_DEV_UMC8672
2332                         case -16: /* "umc8672" */
2333                         {
2334                                 extern void init_umc8672 (void);
2335                                 init_umc8672();
2336                                 goto done;
2337                         }
2338 #endif /* CONFIG_BLK_DEV_UMC8672 */
2339 #ifdef CONFIG_BLK_DEV_DTC2278
2340                         case -15: /* "dtc2278" */
2341                         {
2342                                 extern void init_dtc2278 (void);
2343                                 init_dtc2278();
2344                                 goto done;
2345                         }
2346 #endif /* CONFIG_BLK_DEV_DTC2278 */
2347 #ifdef CONFIG_BLK_DEV_CMD640
2348                         case -14: /* "cmd640_vlb" */
2349                         {
2350                                 extern void init_cmd640_vlb (void);
2351                                 init_cmd640_vlb();
2352                                 goto done;
2353                         }
2354 #endif /* CONFIG_BLK_DEV_CMD640 */
2355 #ifdef CONFIG_BLK_DEV_HT6560B
2356                         case -13: /* "ht6560b" */
2357                         {
2358                                 extern void init_ht6560b (void);
2359                                 init_ht6560b();
2360                                 goto done;
2361                         }
2362 #endif /* CONFIG_BLK_DEV_HT6560B */
2363 #if CONFIG_BLK_DEV_QD65XX
2364                         case -12: /* "qd65xx" */
2365                         {
2366                                 extern void init_qd65xx (void);
2367                                 init_qd65xx();
2368                                 goto done;
2369                         }
2370 #endif /* CONFIG_BLK_DEV_QD65XX */
2371 #ifdef CONFIG_BLK_DEV_4DRIVES
2372                         case -11: /* "four" drives on one set of ports */
2373                         {
2374                                 ide_hwif_t *mate = &ide_hwifs[hw^1];
2375                                 mate->drives[0].select.all ^= 0x20;
2376                                 mate->drives[1].select.all ^= 0x20;
2377                                 hwif->chipset = mate->chipset = ide_4drives;
2378                                 mate->irq = hwif->irq;
2379                                 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2380                                 goto do_serialize;
2381                         }
2382 #endif /* CONFIG_BLK_DEV_4DRIVES */
2383                         case -10: /* minus10 */
2384                         case -9: /* minus9 */
2385                         case -8: /* minus8 */
2386                                 goto bad_option;
2387                         case -7: /* ata66 */
2388 #ifdef CONFIG_BLK_DEV_IDEPCI
2389                                 hwif->udma_four = 1;
2390                                 goto done;
2391 #else /* !CONFIG_BLK_DEV_IDEPCI */
2392                                 hwif->udma_four = 0;
2393                                 goto bad_hwif;
2394 #endif /* CONFIG_BLK_DEV_IDEPCI */
2395                         case -6: /* dma */
2396                                 hwif->autodma = 1;
2397                                 goto done;
2398                         case -5: /* "reset" */
2399                                 hwif->reset = 1;
2400                                 goto done;
2401                         case -4: /* "noautotune" */
2402                                 hwif->drives[0].autotune = 2;
2403                                 hwif->drives[1].autotune = 2;
2404                                 goto done;
2405                         case -3: /* "autotune" */
2406                                 hwif->drives[0].autotune = 1;
2407                                 hwif->drives[1].autotune = 1;
2408                                 goto done;
2409                         case -2: /* "serialize" */
2410                         do_serialize:
2411                                 hwif->mate = &ide_hwifs[hw^1];
2412                                 hwif->mate->mate = hwif;
2413                                 hwif->serialized = hwif->mate->serialized = 1;
2414                                 goto done;
2415
2416                         case -1: /* "noprobe" */
2417                                 hwif->noprobe = 1;
2418                                 goto done;
2419
2420                         case 1: /* base */
2421                                 vals[1] = vals[0] + 0x206; /* default ctl */
2422                         case 2: /* base,ctl */
2423                                 vals[2] = 0;    /* default irq = probe for it */
2424                         case 3: /* base,ctl,irq */
2425                                 hwif->hw.irq = vals[2];
2426                                 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
2427                                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2428                                 hwif->irq      = vals[2];
2429                                 hwif->noprobe  = 0;
2430                                 hwif->chipset  = ide_generic;
2431                                 goto done;
2432
2433                         case 0: goto bad_option;
2434                         default:
2435                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2436                                 return 1;
2437                 }
2438         }
2439 bad_option:
2440         printk(" -- BAD OPTION\n");
2441         return 1;
2442 bad_hwif:
2443         printk("-- NOT SUPPORTED ON ide%d", hw);
2444 done:
2445         printk("\n");
2446         return 1;
2447 }
2448
2449 /*
2450  *      Legacy interface registration
2451  */
2452 #define NUM_DRIVER              32
2453
2454 /*
2455  *      Yes this is moronically simple, but why not - it works!
2456  */
2457
2458 static __initdata ide_driver_call ide_scan[NUM_DRIVER];
2459 static int drivers_run = 0;
2460
2461 void __init ide_register_driver(ide_driver_call scan)
2462 {
2463         static int ide_scans = 0;
2464         if(ide_scans == NUM_DRIVER)
2465                 panic("Too many IDE drivers");
2466         ide_scan[ide_scans++]=scan;
2467         if(drivers_run)
2468         {
2469                 printk(KERN_ERR "ide: late registration of driver.\n");
2470                 scan();
2471         }
2472 }
2473
2474 EXPORT_SYMBOL(ide_register_driver);
2475
2476 static void __init ide_scan_drivers(void)
2477 {
2478         int i = 0;
2479         while(ide_scan[i])
2480         {
2481                 (ide_scan[i])();
2482                 i++;
2483         }
2484         drivers_run = 1;
2485 }
2486
2487 /*
2488  * probe_for_hwifs() finds/initializes "known" IDE interfaces
2489  */
2490 static void __init probe_for_hwifs (void)
2491 {
2492 #ifdef CONFIG_BLK_DEV_IDEPCI
2493         if (pci_present())
2494         {
2495                 ide_scan_pcibus(ide_scan_direction);
2496         }
2497 #endif /* CONFIG_BLK_DEV_IDEPCI */
2498         ide_scan_drivers();
2499
2500         /*
2501          *      Unconverted drivers
2502          */     
2503 #ifdef CONFIG_ETRAX_IDE
2504         {
2505                 extern void init_e100_ide(void);
2506                 init_e100_ide();
2507         }
2508 #endif /* CONFIG_ETRAX_IDE */
2509 #ifdef CONFIG_BLK_DEV_IDE_PMAC
2510         {
2511                 extern void pmac_ide_probe(void);
2512                 pmac_ide_probe();
2513         }
2514 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
2515 #ifdef CONFIG_BLK_DEV_IDE_SIBYTE
2516         {
2517                 extern void sibyte_ide_probe(void);
2518                 sibyte_ide_probe();
2519         }
2520 #endif /* CONFIG_BLK_DEV_IDE_SIBYTE */
2521 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
2522         {
2523                 extern void icside_init(void);
2524                 icside_init();
2525         }
2526 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
2527 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
2528         {
2529                 extern void rapide_init(void);
2530                 rapide_init();
2531         }
2532 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
2533 #ifdef CONFIG_BLK_DEV_GAYLE
2534         {
2535                 extern void gayle_init(void);
2536                 gayle_init();
2537         }
2538 #endif /* CONFIG_BLK_DEV_GAYLE */
2539 #ifdef CONFIG_BLK_DEV_FALCON_IDE
2540         {
2541                 extern void falconide_init(void);
2542                 falconide_init();
2543         }
2544 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
2545 #ifdef CONFIG_BLK_DEV_MAC_IDE
2546         {
2547                 extern void macide_init(void);
2548                 macide_init();
2549         }
2550 #endif /* CONFIG_BLK_DEV_MAC_IDE */
2551 #ifdef CONFIG_BLK_DEV_CPCI405_IDE
2552         {
2553                 extern void cpci405ide_init(void);
2554                 cpci405ide_init();
2555         }
2556 #endif /* CONFIG_BLK_DEV_CPCI405_IDE */
2557 #ifdef CONFIG_BLK_DEV_Q40IDE
2558         {
2559                 extern void q40ide_init(void);
2560                 q40ide_init();
2561         }
2562 #endif /* CONFIG_BLK_DEV_Q40IDE */
2563 #ifdef CONFIG_BLK_DEV_BUDDHA
2564         {
2565                 extern void buddha_init(void);
2566                 buddha_init();
2567         }
2568 #endif /* CONFIG_BLK_DEV_BUDDHA */
2569 }
2570
2571 void __init ide_init_builtin_subdrivers (void)
2572 {
2573         idedefault_init();
2574 #ifdef CONFIG_BLK_DEV_IDEDISK
2575         (void) idedisk_init();
2576 #endif /* CONFIG_BLK_DEV_IDEDISK */
2577 #ifdef CONFIG_BLK_DEV_IDECD
2578         (void) ide_cdrom_init();
2579 #endif /* CONFIG_BLK_DEV_IDECD */
2580 #ifdef CONFIG_BLK_DEV_IDETAPE
2581         (void) idetape_init();
2582 #endif /* CONFIG_BLK_DEV_IDETAPE */
2583 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2584         (void) idefloppy_init();
2585 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2586 #ifdef CONFIG_BLK_DEV_IDESCSI
2587  #ifdef CONFIG_SCSI
2588         (void) idescsi_init();
2589  #else
2590     #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
2591  #endif
2592 #endif /* CONFIG_BLK_DEV_IDESCSI */
2593 }
2594
2595 #ifndef CLASSIC_BUILTINS_METHOD
2596 #  ifndef DIRECT_HWIF_PROBE_ATTACH_METHOD
2597 #    ifdef FAKE_CLASSIC_ATTACH_METHOD
2598 /*
2599  * Attempt to match drivers for the available drives
2600  */
2601 void ide_attach_devices (void)
2602 {
2603         ide_hwif_t *hwif;
2604         ide_drive_t *drive;
2605         int i, unit;
2606
2607         for (i = 0; i < MAX_HWIFS; i++) {
2608                 hwif = &ide_hwifs[i];
2609                 if (!hwif->present)
2610                         continue;
2611                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2612                         drive = &hwif->drives[unit];
2613                         if (drive->present && !drive->dead)
2614                                 ide_attach_drive(drive);
2615                 }
2616         }
2617 }
2618 #    endif /* FAKE_CLASSIC_ATTACH_METHOD */
2619 #  endif /* DIRECT_HWIF_PROBE_ATTACH_METHOD */
2620 #endif /* CLASSIC_BUILTINS_METHOD */
2621
2622 void __init ide_init_builtin_drivers (void)
2623 {
2624         /*
2625          * Attach null driver
2626          */
2627         idedefault_init();
2628         /*
2629          * Probe for special PCI and other "known" interface chipsets
2630          */
2631         probe_for_hwifs ();
2632
2633 #ifdef CONFIG_BLK_DEV_IDE
2634         if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2635                 ide_get_lock(NULL, NULL); /* for atari only */
2636
2637         (void) ideprobe_init();
2638
2639         if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2640                 ide_release_lock();     /* for atari only */
2641 #endif /* CONFIG_BLK_DEV_IDE */
2642
2643 #ifdef CONFIG_PROC_FS
2644         proc_ide_create();
2645 #endif
2646
2647 #ifdef CLASSIC_BUILTINS_METHOD
2648         ide_init_builtin_subdrivers();
2649 #else /* ! CLASSIC_BUILTINS_METHOD */
2650 #  ifndef DIRECT_HWIF_PROBE_ATTACH_METHOD
2651 #    ifdef FAKE_CLASSIC_ATTACH_METHOD
2652         ide_attach_devices();
2653 #    endif /* FAKE_CLASSIC_ATTACH_METHOD */
2654 #  endif /* DIRECT_HWIF_PROBE_ATTACH_METHOD */
2655 #endif /* CLASSIC_BUILTINS_METHOD */
2656 }
2657
2658 /*
2659  *      Actually unregister the subdriver. Called with the
2660  *      request lock dropped.
2661  */
2662  
2663 static int default_cleanup (ide_drive_t *drive)
2664 {
2665         return ide_unregister_subdriver(drive);
2666 }
2667
2668 /*
2669  *      Check if we can unregister the subdriver. Called with the
2670  *      request lock held.
2671  */
2672  
2673 static int default_shutdown(ide_drive_t *drive)
2674 {
2675         if (drive->usage || drive->busy || DRIVER(drive)->busy) {
2676                 return 1;
2677         }
2678         drive->dead = 1;
2679         return 0;
2680 }
2681
2682 static int default_standby (ide_drive_t *drive)
2683 {
2684         return 0;
2685 }
2686
2687 static int default_suspend (ide_drive_t *drive)
2688 {
2689         return 0;
2690 }
2691
2692 static int default_resume (ide_drive_t *drive)
2693 {
2694         return 0;
2695 }
2696
2697 static int default_flushcache (ide_drive_t *drive)
2698 {
2699         return 0;
2700 }
2701
2702 static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2703 {
2704         ide_end_request(drive, 0);
2705         return ide_stopped;
2706 }
2707
2708 static int default_end_request (ide_drive_t *drive, int uptodate)
2709 {
2710         return ide_end_request(drive, uptodate);
2711 }
2712
2713 static u8 default_sense (ide_drive_t *drive, const char *msg, u8 stat)
2714 {
2715         return ide_dump_status(drive, msg, stat);
2716 }
2717
2718 static ide_startstop_t default_error (ide_drive_t *drive, const char *msg, u8 stat)
2719 {
2720         return ide_error(drive, msg, stat);
2721 }
2722
2723 static ide_startstop_t default_abort (ide_drive_t *drive, const char *msg)
2724 {
2725         return ide_abort(drive, msg);
2726 }
2727
2728 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
2729                           unsigned int cmd, unsigned long arg)
2730 {
2731         return -EIO;
2732 }
2733
2734 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
2735 {
2736         drive->usage--;
2737         return -EIO;
2738 }
2739
2740 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
2741 {
2742 }
2743
2744 static int default_check_media_change (ide_drive_t *drive)
2745 {
2746         return 1;
2747 }
2748
2749 static void default_pre_reset (ide_drive_t *drive)
2750 {
2751 }
2752
2753 static unsigned long default_capacity (ide_drive_t *drive)
2754 {
2755         return 0x7fffffff;
2756 }
2757
2758 static ide_startstop_t default_special (ide_drive_t *drive)
2759 {
2760         special_t *s = &drive->special;
2761
2762         s->all = 0;
2763         drive->mult_req = 0;
2764         return ide_stopped;
2765 }
2766
2767 static int default_init (void)
2768 {
2769         return 0;
2770 }
2771
2772 static int default_attach (ide_drive_t *drive)
2773 {
2774         printk(KERN_ERR "%s: does not support hotswap of device class !\n",
2775                 drive->name);
2776
2777         return 0;
2778 }
2779
2780 static void setup_driver_defaults (ide_drive_t *drive)
2781 {
2782         ide_driver_t *d = drive->driver;
2783
2784         if (d->cleanup == NULL)         d->cleanup = default_cleanup;
2785         if (d->shutdown == NULL)        d->shutdown = default_shutdown;
2786         if (d->standby == NULL)         d->standby = default_standby;
2787         if (d->suspend == NULL)         d->suspend = default_suspend;
2788         if (d->resume == NULL)          d->resume = default_resume;
2789         if (d->flushcache == NULL)      d->flushcache = default_flushcache;
2790         if (d->do_request == NULL)      d->do_request = default_do_request;
2791         if (d->end_request == NULL)     d->end_request = default_end_request;
2792         if (d->sense == NULL)           d->sense = default_sense;
2793         if (d->error == NULL)           d->error = default_error;
2794         if (d->error == NULL)           d->abort = default_abort;
2795         if (d->ioctl == NULL)           d->ioctl = default_ioctl;
2796         if (d->open == NULL)            d->open = default_open;
2797         if (d->release == NULL)         d->release = default_release;
2798         if (d->media_change == NULL)    d->media_change = default_check_media_change;
2799         if (d->pre_reset == NULL)       d->pre_reset = default_pre_reset;
2800         if (d->capacity == NULL)        d->capacity = default_capacity;
2801         if (d->special == NULL)         d->special = default_special;
2802         if (d->init == NULL)            d->init = default_init;
2803         if (d->attach == NULL)          d->attach = default_attach;
2804 }
2805
2806 ide_drive_t *ide_scan_devices (u8 media, const char *name, ide_driver_t *driver, int n)
2807 {
2808         unsigned int unit, index, i;
2809         
2810         if(driver == NULL)      /* No driver is actually &idedefault_driver */
2811                 driver = &idedefault_driver;
2812
2813         for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
2814                 ide_hwif_t *hwif = &ide_hwifs[index];
2815                 if (!hwif->present)
2816                         continue;
2817                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2818                         ide_drive_t *drive = &hwif->drives[unit];
2819                         char *req = drive->driver_req;
2820                         if (*req && !strstr(name, req))
2821                                 continue;
2822                         if (drive->present && drive->media == media &&
2823                             drive->driver == driver && ++i > n)
2824                                 return drive;
2825                 }
2826         }
2827         return NULL;
2828 }
2829
2830 EXPORT_SYMBOL(ide_scan_devices);
2831
2832 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
2833 {
2834         unsigned long flags;
2835         
2836         BUG_ON(drive->driver == NULL);
2837         
2838         spin_lock_irqsave(&io_request_lock, flags);
2839         if (version != IDE_SUBDRIVER_VERSION || !drive->present ||
2840             drive->driver != &idedefault_driver || drive->busy || drive->usage) {
2841                 spin_unlock_irqrestore(&io_request_lock, flags);
2842                 return 1;
2843         }
2844         drive->driver = driver;
2845         setup_driver_defaults(drive);
2846         printk("%s: attached %s driver.\n", drive->name, driver->name);
2847         spin_unlock_irqrestore(&io_request_lock, flags);
2848         if (drive->autotune != 2) {
2849                 /* DMA timings and setup moved to ide-probe.c */
2850                 if (!driver->supports_dma && HWIF(drive)->ide_dma_off_quietly)
2851 //                      HWIF(drive)->ide_dma_off_quietly(drive);
2852                         HWIF(drive)->ide_dma_off(drive);
2853                 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2854                 drive->nice1 = 1;
2855         }
2856         drive->revalidate = 1;
2857         drive->suspend_reset = 0;
2858 #ifdef CONFIG_PROC_FS
2859         if (drive->driver != &idedefault_driver) {
2860                 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2861                 ide_add_proc_entries(drive->proc, driver->proc, drive);
2862         }
2863 #endif
2864         return 0;
2865 }
2866
2867 EXPORT_SYMBOL(ide_register_subdriver);
2868
2869 int ide_unregister_subdriver (ide_drive_t *drive)
2870 {
2871         unsigned long flags;
2872
2873         down(&ide_setting_sem); 
2874         spin_lock_irqsave(&io_request_lock, flags);
2875         if (drive->usage || drive->busy || DRIVER(drive)->busy) {
2876                 spin_unlock_irqrestore(&io_request_lock, flags);
2877                 up(&ide_setting_sem);
2878                 return 1;
2879         }
2880 #ifdef CONFIG_PROC_FS
2881         ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2882         ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2883 #endif
2884         drive->driver = &idedefault_driver;
2885         setup_driver_defaults(drive);
2886         auto_remove_settings(drive);
2887         spin_unlock_irqrestore(&io_request_lock, flags);
2888         up(&ide_setting_sem);
2889         return 0;
2890 }
2891
2892 EXPORT_SYMBOL(ide_unregister_subdriver);
2893
2894 int ide_register_module (ide_module_t *module)
2895 {
2896         ide_module_t *p = ide_modules;
2897
2898         while (p) {
2899                 if (p == module)
2900                         return 1;
2901                 p = p->next;
2902         }
2903         module->next = ide_modules;
2904         ide_modules = module;
2905         revalidate_drives(1);
2906         return 0;
2907 }
2908
2909 EXPORT_SYMBOL(ide_register_module);
2910
2911 void ide_unregister_module (ide_module_t *module)
2912 {
2913         ide_module_t **p;
2914
2915         for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
2916         if (*p)
2917                 *p = (*p)->next;
2918 }
2919
2920 EXPORT_SYMBOL(ide_unregister_module);
2921
2922 struct block_device_operations ide_fops[] = {{
2923         owner:                  THIS_MODULE,
2924         open:                   ide_open,
2925         release:                ide_release,
2926         ioctl:                  ide_ioctl,
2927         check_media_change:     ide_check_media_change,
2928         revalidate:             ide_revalidate_disk
2929 }};
2930
2931 EXPORT_SYMBOL(ide_fops);
2932
2933 /*
2934  * ide_geninit() is called exactly *once* for each interface.
2935  */
2936 void ide_geninit (ide_hwif_t *hwif)
2937 {
2938         unsigned int unit;
2939         struct gendisk *gd = hwif->gd;
2940
2941         for (unit = 0; unit < MAX_DRIVES; ++unit) {
2942                 ide_drive_t *drive = &hwif->drives[unit];
2943
2944                 if (!drive->present)
2945                         continue;
2946                 if (drive->media!=ide_disk && drive->media!=ide_floppy)
2947                         continue;
2948                 register_disk(gd,MKDEV(hwif->major,unit<<PARTN_BITS),
2949 #ifdef CONFIG_BLK_DEV_ISAPNP
2950                         (drive->forced_geom && drive->noprobe) ? 1 :
2951 #endif /* CONFIG_BLK_DEV_ISAPNP */
2952                         1<<PARTN_BITS, ide_fops,
2953                         current_capacity(drive));
2954         }
2955 }
2956
2957 EXPORT_SYMBOL(ide_geninit);
2958
2959
2960 /*
2961  * Probe module
2962  */
2963 devfs_handle_t ide_devfs_handle;
2964
2965 EXPORT_SYMBOL(ide_probe);
2966 EXPORT_SYMBOL(ide_devfs_handle);
2967
2968 static int ide_notify_reboot (struct notifier_block *this, unsigned long event, void *x)
2969 {
2970         ide_hwif_t *hwif;
2971         ide_drive_t *drive;
2972         int i, unit;
2973
2974         switch (event) {
2975                 case SYS_HALT:
2976                 case SYS_POWER_OFF:
2977                 case SYS_RESTART:
2978                         break;
2979                 default:
2980                         return NOTIFY_DONE;
2981         }
2982
2983         printk(KERN_INFO "flushing ide devices: ");
2984
2985         for (i = 0; i < MAX_HWIFS; i++) {
2986                 hwif = &ide_hwifs[i];
2987                 if (!hwif->present)
2988                         continue;
2989                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2990                         drive = &hwif->drives[unit];
2991                         if (!drive->present)
2992                                 continue;
2993
2994                         /* set the drive to standby */
2995                         printk("%s ", drive->name);
2996 #ifdef  CONFIG_ALPHA
2997                         /* On Alpha, halt(8) doesn't actually turn the machine
2998                            off, it puts you into the sort of firmware monitor.
2999                            Typically, it's used to boot another kernel image,
3000                            so it's not much different from reboot(8).
3001                            Therefore, we don't need to spin down the disk in
3002                            this case, especially since Alpha firmware doesn't
3003                            handle disks in standby mode properly.
3004                            On the other hand, it's reasonably safe to turn
3005                            the power off when the shutdown process reaches
3006                            the firmware prompt, as the firmware initialization
3007                            takes rather long time - at least 10 seconds,
3008                            which should be sufficient for the disk to expire
3009                            its write cache. */
3010                         if (event == SYS_POWER_OFF)
3011 #else
3012                         if (event != SYS_RESTART)
3013 #endif
3014                                 if (DRIVER(drive)->standby(drive))
3015                                         continue;
3016
3017                         DRIVER(drive)->cleanup(drive);
3018                         continue;
3019                 }
3020         }
3021         printk("\n");
3022         return NOTIFY_DONE;
3023 }
3024
3025 static struct notifier_block ide_notifier = {
3026         ide_notify_reboot,
3027         NULL,
3028         5
3029 };
3030
3031 /*
3032  * This is gets invoked once during initialization, to set *everything* up
3033  */
3034 int __init ide_init (void)
3035 {
3036         static char banner_printed;
3037         int i;
3038
3039         if (!banner_printed) {
3040                 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3041                 ide_devfs_handle = devfs_mk_dir(NULL, "ide", NULL);
3042                 system_bus_speed = ide_system_bus_speed();
3043                 banner_printed = 1;
3044         }
3045
3046         init_ide_data();
3047
3048 #ifndef CLASSIC_BUILTINS_METHOD
3049         ide_init_builtin_subdrivers();
3050 #endif /* CLASSIC_BUILTINS_METHOD */
3051
3052         initializing = 1;
3053         ide_init_builtin_drivers();
3054         initializing = 0;
3055
3056         for (i = 0; i < MAX_HWIFS; ++i) {
3057                 ide_hwif_t  *hwif = &ide_hwifs[i];
3058                 if (hwif->present)
3059                         ide_geninit(hwif);
3060         }
3061
3062         register_reboot_notifier(&ide_notifier);
3063         return 0;
3064 }
3065
3066 #ifdef MODULE
3067 char *options = NULL;
3068 MODULE_PARM(options,"s");
3069 MODULE_LICENSE("GPL");
3070
3071 static void __init parse_options (char *line)
3072 {
3073         char *next = line;
3074
3075         if (line == NULL || !*line)
3076                 return;
3077         while ((line = next) != NULL) {
3078                 if ((next = strchr(line,' ')) != NULL)
3079                         *next++ = 0;
3080                 if (!ide_setup(line))
3081                         printk ("Unknown option '%s'\n", line);
3082         }
3083 }
3084
3085 int init_module (void)
3086 {
3087         parse_options(options);
3088         return ide_init();
3089 }
3090
3091 void cleanup_module (void)
3092 {
3093         int index;
3094
3095         unregister_reboot_notifier(&ide_notifier);
3096         for (index = 0; index < MAX_HWIFS; ++index) {
3097                 ide_unregister(index);
3098 #if !defined(CONFIG_DMA_NONPCI)
3099                 if (ide_hwifs[index].dma_base)
3100                         (void) ide_release_dma(&ide_hwifs[index]);
3101 #endif /* !(CONFIG_DMA_NONPCI) */
3102         }
3103
3104 #ifdef CONFIG_PROC_FS
3105         proc_ide_destroy();
3106 #endif
3107         devfs_unregister (ide_devfs_handle);
3108 }
3109
3110 #else /* !MODULE */
3111
3112 __setup("", ide_setup);
3113 module_init(ide_init);
3114
3115 #endif /* MODULE */