make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / ide / ide.c
1 /*
2  *  linux/drivers/ide/ide.c             Version 7.00beta1       Nov 29 2002
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.00beta-2.4"
125 #define VERSION         "Id: ide.c 7.00b1 20021129"
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 #if defined(__mc68000__) || defined(CONFIG_APUS)
178 /*
179  * ide_lock is used by the Atari code to obtain access to the IDE interrupt,
180  * which is shared between several drivers.
181  */
182 static int ide_intr_lock;
183 #endif /* __mc68000__ || CONFIG_APUS */
184
185 #ifdef CONFIG_IDEDMA_AUTO
186 int noautodma = 0;
187 #else
188 int noautodma = 1;
189 #endif
190
191 EXPORT_SYMBOL(noautodma);
192
193
194 /*
195  * ide_modules keeps track of the available IDE chipset/probe/driver modules.
196  */
197 ide_module_t *ide_chipsets;
198 ide_module_t *ide_modules;
199 ide_module_t *ide_probe;
200
201 /*
202  * This is declared extern in ide.h, for access by other IDE modules:
203  */
204 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
205
206 EXPORT_SYMBOL(ide_hwifs);
207
208 ide_devices_t *idedisk;
209 ide_devices_t *idecd;
210 ide_devices_t *idefloppy;
211 ide_devices_t *idetape;
212 ide_devices_t *idescsi;
213
214 EXPORT_SYMBOL(idedisk);
215 EXPORT_SYMBOL(idecd);
216 EXPORT_SYMBOL(idefloppy);
217 EXPORT_SYMBOL(idetape);
218 EXPORT_SYMBOL(idescsi);
219
220 /*
221  * Do not even *think* about calling this!
222  */
223 static void init_hwif_data (unsigned int index)
224 {
225         unsigned int unit;
226         hw_regs_t hw;
227         ide_hwif_t *hwif = &ide_hwifs[index];
228
229         /* bulk initialize hwif & drive info with zeros */
230         memset(hwif, 0, sizeof(ide_hwif_t));
231         memset(&hw, 0, sizeof(hw_regs_t));
232
233         /* fill in any non-zero initial values */
234         hwif->index     = index;
235         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
236         memcpy(&hwif->hw, &hw, sizeof(hw));
237         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
238         hwif->noprobe   = !hwif->io_ports[IDE_DATA_OFFSET];
239 #ifdef CONFIG_BLK_DEV_HD
240         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
241                 hwif->noprobe = 1; /* may be overridden by ide_setup() */
242 #endif /* CONFIG_BLK_DEV_HD */
243         hwif->major     = ide_hwif_to_major[index];
244         hwif->name[0]   = 'i';
245         hwif->name[1]   = 'd';
246         hwif->name[2]   = 'e';
247         hwif->name[3]   = '0' + index;
248         hwif->bus_state = BUSSTATE_ON;
249         hwif->reset_poll= NULL;
250         hwif->pre_reset = NULL;
251
252         hwif->atapi_dma = 0;            /* disable all atapi dma */ 
253         hwif->ultra_mask = 0x80;        /* disable all ultra */
254         hwif->mwdma_mask = 0x80;        /* disable all mwdma */
255         hwif->swdma_mask = 0x80;        /* disable all swdma */
256
257         default_hwif_iops(hwif);
258         default_hwif_transport(hwif);
259         for (unit = 0; unit < MAX_DRIVES; ++unit) {
260                 ide_drive_t *drive = &hwif->drives[unit];
261
262                 drive->media                    = ide_disk;
263                 drive->select.all               = (unit<<4)|0xa0;
264                 drive->hwif                     = hwif;
265                 drive->ctl                      = 0x08;
266                 drive->ready_stat               = READY_STAT;
267                 drive->bad_wstat                = BAD_W_STAT;
268                 drive->special.b.recalibrate    = 1;
269                 drive->special.b.set_geometry   = 1;
270                 drive->name[0]                  = 'h';
271                 drive->name[1]                  = 'd';
272                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
273                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
274                 drive->using_dma                = 0;
275                 drive->is_flash                 = 0;
276                 init_waitqueue_head(&drive->wqueue);
277         }
278 }
279
280 /*
281  * init_ide_data() sets reasonable default values into all fields
282  * of all instances of the hwifs and drives, but only on the first call.
283  * Subsequent calls have no effect (they don't wipe out anything).
284  *
285  * This routine is normally called at driver initialization time,
286  * but may also be called MUCH earlier during kernel "command-line"
287  * parameter processing.  As such, we cannot depend on any other parts
288  * of the kernel (such as memory allocation) to be functioning yet.
289  *
290  * This is too bad, as otherwise we could dynamically allocate the
291  * ide_drive_t structs as needed, rather than always consuming memory
292  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
293  *
294  * FIXME: We should stuff the setup data into __init and copy the
295  * relevant hwifs/allocate them properly during boot.
296  */
297 #define MAGIC_COOKIE 0x12345678
298 static void __init init_ide_data (void)
299 {
300         unsigned int index;
301         static unsigned long magic_cookie = MAGIC_COOKIE;
302
303         if (magic_cookie != MAGIC_COOKIE)
304                 return;         /* already initialized */
305         magic_cookie = 0;
306
307         /* Initialise all interface structures */
308         for (index = 0; index < MAX_HWIFS; ++index)
309                 init_hwif_data(index);
310
311         /* Add default hw interfaces */
312         ide_init_default_hwifs();
313
314         idebus_parameter = 0;
315         system_bus_speed = 0;
316 }
317
318 /*
319  * ide_system_bus_speed() returns what we think is the system VESA/PCI
320  * bus speed (in MHz).  This is used for calculating interface PIO timings.
321  * The default is 40 for known PCI systems, 50 otherwise.
322  * The "idebus=xx" parameter can be used to override this value.
323  * The actual value to be used is computed/displayed the first time through.
324  */
325 int ide_system_bus_speed (void)
326 {
327         if (!system_bus_speed) {
328                 if (idebus_parameter) {
329                         /* user supplied value */
330                         system_bus_speed = idebus_parameter;
331                 } else if (pci_present()) {
332                         /* safe default value for PCI */
333                         system_bus_speed = 33;
334                 } else {
335                         /* safe default value for VESA and PCI */
336                         system_bus_speed = 50;
337                 }
338                 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
339                         "for PIO modes%s\n", system_bus_speed,
340                         idebus_parameter ? "" : "; override with idebus=xx");
341         }
342         return system_bus_speed;
343 }
344
345 /*
346  * current_capacity() returns the capacity (in sectors) of a drive
347  * according to its current geometry/LBA settings.
348  */
349 unsigned long current_capacity (ide_drive_t *drive)
350 {
351         if (!drive->present)
352                 return 0;
353         if (drive->driver != NULL)
354                 return DRIVER(drive)->capacity(drive);
355         return 0;
356 }
357
358 EXPORT_SYMBOL(current_capacity);
359
360 static inline u32 read_24 (ide_drive_t *drive)
361 {
362         return  (HWIF(drive)->INB(IDE_HCYL_REG)<<16) |
363                 (HWIF(drive)->INB(IDE_LCYL_REG)<<8) |
364                  HWIF(drive)->INB(IDE_SECTOR_REG);
365 }
366
367 /*
368  * Error reporting, in human readable form (luxurious, but a memory hog).
369  */
370 u8 ide_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
371 {
372         ide_hwif_t *hwif = HWIF(drive);
373         unsigned long flags;
374         u8 err = 0;
375
376         local_irq_set(flags);
377         printk(KERN_WARNING "%s: %s: status=0x%02x", drive->name, msg, stat);
378 #if FANCY_STATUS_DUMPS
379         printk(" { ");
380         if (stat & BUSY_STAT) {
381                 printk("Busy ");
382         } else {
383                 if (stat & READY_STAT)  printk("DriveReady ");
384                 if (stat & WRERR_STAT)  printk("DeviceFault ");
385                 if (stat & SEEK_STAT)   printk("SeekComplete ");
386                 if (stat & DRQ_STAT)    printk("DataRequest ");
387                 if (stat & ECC_STAT)    printk("CorrectedError ");
388                 if (stat & INDEX_STAT)  printk("Index ");
389                 if (stat & ERR_STAT)    printk("Error ");
390         }
391         printk("}");
392 #endif  /* FANCY_STATUS_DUMPS */
393         printk("\n");
394         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
395                 err = hwif->INB(IDE_ERROR_REG);
396                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
397 #if FANCY_STATUS_DUMPS
398                 if (drive->media == ide_disk) {
399                         printk(" { ");
400                         if (err & ABRT_ERR)     printk("DriveStatusError ");
401                         if (err & ICRC_ERR)     printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
402                         if (err & ECC_ERR)      printk("UncorrectableError ");
403                         if (err & ID_ERR)       printk("SectorIdNotFound ");
404                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
405                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
406                         printk("}");
407                         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
408                                 if ((drive->id->command_set_2 & 0x0400) &&
409                                     (drive->id->cfs_enable_2 & 0x0400) &&
410                                     (drive->addressing == 1)) {
411                                         u64 sectors = 0;
412                                         u32 high = 0;
413                                         u32 low = read_24(drive);
414                                         hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
415                                         high = read_24(drive);
416
417                                         sectors = ((u64)high << 24) | low;
418                                         printk(", LBAsect=%llu, high=%d, low=%d",
419                                                (u64) sectors,
420                                                high, low);
421                                 } else {
422                                         u8 cur = hwif->INB(IDE_SELECT_REG);
423                                         if (cur & 0x40) {       /* using LBA? */
424                                                 printk(", LBAsect=%ld", (unsigned long)
425                                                  ((cur&0xf)<<24)
426                                                  |(hwif->INB(IDE_HCYL_REG)<<16)
427                                                  |(hwif->INB(IDE_LCYL_REG)<<8)
428                                                  | hwif->INB(IDE_SECTOR_REG));
429                                         } else {
430                                                 printk(", CHS=%d/%d/%d",
431                                                  (hwif->INB(IDE_HCYL_REG)<<8) +
432                                                   hwif->INB(IDE_LCYL_REG),
433                                                   cur & 0xf,
434                                                   hwif->INB(IDE_SECTOR_REG));
435                                         }
436                                 }
437                                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
438                                         printk(", sector=%ld", HWGROUP(drive)->rq->sector);
439                         }
440                 }
441 #endif  /* FANCY_STATUS_DUMPS */
442                 printk("\n");
443         }
444         local_irq_restore(flags);
445         return err;
446 }
447
448 EXPORT_SYMBOL(ide_dump_status);
449
450
451 /*
452  * This routine is called to flush all partitions and partition tables
453  * for a changed disk, and then re-read the new partition table.
454  * If we are revalidating a disk because of a media change, then we
455  * enter with usage == 0.  If we are using an ioctl, we automatically have
456  * usage == 1 (we need an open channel to use an ioctl :-), so this
457  * is our limit.
458  */
459 int ide_revalidate_disk (kdev_t i_rdev)
460 {
461         ide_drive_t *drive;
462         ide_hwgroup_t *hwgroup;
463         unsigned int p, major, minor;
464         unsigned long flags;
465
466         if ((drive = get_info_ptr(i_rdev)) == NULL)
467                 return -ENODEV;
468         major = MAJOR(i_rdev);
469         minor = drive->select.b.unit << PARTN_BITS;
470         hwgroup = HWGROUP(drive);
471         spin_lock_irqsave(&io_request_lock, flags);
472         if (drive->busy || (drive->usage > 1)) {
473                 spin_unlock_irqrestore(&io_request_lock, flags);
474                 return -EBUSY;
475         };
476         drive->busy = 1;
477         MOD_INC_USE_COUNT;
478         spin_unlock_irqrestore(&io_request_lock, flags);
479
480         for (p = 0; p < (1<<PARTN_BITS); ++p) {
481                 if (drive->part[p].nr_sects > 0) {
482                         kdev_t devp = MKDEV(major, minor+p);
483                         invalidate_device(devp, 1);
484                 }
485                 drive->part[p].start_sect = 0;
486                 drive->part[p].nr_sects   = 0;
487         };
488
489         if (DRIVER(drive) && DRIVER(drive)->revalidate)
490                 DRIVER(drive)->revalidate(drive);
491
492         drive->busy = 0;
493         wake_up(&drive->wqueue);
494         MOD_DEC_USE_COUNT;
495         return 0;
496 }
497
498 EXPORT_SYMBOL(ide_revalidate_disk);
499
500 static void revalidate_drives (int revaldiate)
501 {
502         ide_hwif_t *hwif;
503         ide_drive_t *drive;
504         int index, unit;
505
506         for (index = 0; index < MAX_HWIFS; ++index) {
507                 hwif = &ide_hwifs[index];
508                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
509                         drive = &ide_hwifs[index].drives[unit];
510                         if (drive->revalidate) {
511                                 drive->revalidate = 0;
512                                 if ((!initializing) && (revaldiate))
513                                         (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
514                         }
515                 }
516         }
517 }
518
519 void ide_probe_module (int revaldiate)
520 {
521         if (!ide_probe) {
522 #if  defined(CONFIG_BLK_DEV_IDE_MODULE)
523                 (void) request_module("ide-probe-mod");
524 #endif
525         } else {
526                 (void) ide_probe->init();
527         }
528         revalidate_drives(revaldiate);
529 }
530
531 EXPORT_SYMBOL(ide_probe_module);
532
533 void ide_driver_module (int revaldiate)
534 {
535         int index;
536         ide_module_t *module = ide_modules;
537
538         for (index = 0; index < MAX_HWIFS; ++index)
539                 if (ide_hwifs[index].present)
540                         goto search;
541         ide_probe_module(revaldiate);
542 search:
543         while (module) {
544                 (void) module->init();
545                 module = module->next;
546         }
547         revalidate_drives(revaldiate);
548 }
549
550 EXPORT_SYMBOL(ide_driver_module);
551
552 static int ide_open (struct inode * inode, struct file * filp)
553 {
554         ide_drive_t *drive;
555
556         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
557                 return -ENXIO;
558         if (drive->driver == NULL)
559                 ide_driver_module(1);
560         if (drive->driver == NULL) {
561                 if (drive->media == ide_disk)
562                         (void) request_module("ide-disk");
563                 if (drive->scsi)
564                         (void) request_module("ide-scsi");
565                 if (drive->media == ide_cdrom)
566                         (void) request_module("ide-cd");
567                 if (drive->media == ide_tape)
568                         (void) request_module("ide-tape");
569                 if (drive->media == ide_floppy)
570                         (void) request_module("ide-floppy");
571         }
572         
573         /* The locking here isnt enough, but this is hard to fix
574            in the 2.4 cases */
575         while (drive->busy)
576                 sleep_on(&drive->wqueue);
577         drive->usage++;
578         if (drive->driver != NULL && !drive->dead)
579                 return DRIVER(drive)->open(inode, filp, drive);
580         printk(KERN_WARNING "%s: driver not present\n", drive->name);
581         drive->usage--;
582         return -ENXIO;
583 }
584
585 /*
586  * Releasing a block device means we sync() it, so that it can safely
587  * be forgotten about...
588  */
589 static int ide_release (struct inode * inode, struct file * file)
590 {
591         ide_drive_t *drive;
592
593         if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
594                 drive->usage--;
595                 if (drive->driver != NULL)
596                         DRIVER(drive)->release(inode, file, drive);
597         }
598         return 0;
599 }
600
601 #ifdef CONFIG_PROC_FS
602 ide_proc_entry_t generic_subdriver_entries[] = {
603         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
604         { NULL, 0, NULL, NULL }
605 };
606 #endif
607
608
609 #define hwif_release_region(addr, num) \
610         ((hwif->mmio) ? release_mem_region((addr),(num)) : release_region((addr),(num)))
611
612 /*
613  * Note that we only release the standard ports,
614  * and do not even try to handle any extra ports
615  * allocated for weird IDE interface chipsets.
616  */
617 void hwif_unregister (ide_hwif_t *hwif)
618 {
619         u32 i = 0;
620
621         if (hwif->mmio == 2)
622                 return;
623         if (hwif->io_ports[IDE_CONTROL_OFFSET])
624                 hwif_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
625 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
626         if (hwif->io_ports[IDE_IRQ_OFFSET])
627                 hwif_release_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
628 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
629
630         if (hwif->straight8) {
631                 hwif_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
632                 return;
633         }
634         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
635                 if (hwif->io_ports[i]) {
636                         hwif_release_region(hwif->io_ports[i], 1);
637                 }
638         }
639 }
640
641 EXPORT_SYMBOL(hwif_unregister);
642
643 extern void init_hwif_data(unsigned int index);
644
645 int ide_unregister (unsigned int index)
646 {
647         struct gendisk *gd;
648         ide_drive_t *drive, *d;
649         ide_hwif_t *hwif, *g;
650         ide_hwgroup_t *hwgroup;
651         int irq_count = 0, unit, i;
652         unsigned long flags;
653         unsigned int p, minor;
654         ide_hwif_t old_hwif;
655
656         if (index >= MAX_HWIFS)
657                 BUG();
658                 
659         spin_lock_irqsave(&io_request_lock, flags);
660         hwif = &ide_hwifs[index];
661         if (!hwif->present)
662                 goto abort;
663         for (unit = 0; unit < MAX_DRIVES; ++unit) {
664                 drive = &hwif->drives[unit];
665                 if (!drive->present)
666                         continue;
667                 if (drive->busy || drive->usage)
668                         goto abort;
669                 if (drive->driver != NULL && DRIVER(drive)->shutdown(drive))
670                         goto abort;
671         }
672         hwif->present = 0;
673         
674         /*
675          * All clear?  Then blow away the buffer cache
676          */
677         spin_unlock_irqrestore(&io_request_lock, flags);
678         for (unit = 0; unit < MAX_DRIVES; ++unit) {
679                 drive = &hwif->drives[unit];
680                 if (!drive->present)
681                         continue;
682                 if (drive->driver != NULL)
683                         DRIVER(drive)->cleanup(drive);
684                 minor = drive->select.b.unit << PARTN_BITS;
685                 for (p = 0; p < (1<<PARTN_BITS); ++p) {
686                         if (drive->part[p].nr_sects > 0) {
687                                 kdev_t devp = MKDEV(hwif->major, minor+p);
688                                 invalidate_device(devp, 0);
689                         }
690                 }
691 #ifdef CONFIG_PROC_FS
692                 destroy_proc_ide_drives(hwif);
693 #endif
694         }
695
696         spin_lock_irqsave(&io_request_lock, flags);
697         hwgroup = hwif->hwgroup;
698
699         /*
700          * free the irq if we were the only hwif using it
701          */
702         g = hwgroup->hwif;
703         do {
704                 if (g->irq == hwif->irq)
705                         ++irq_count;
706                 g = g->next;
707         } while (g != hwgroup->hwif);
708         if (irq_count == 1)
709                 free_irq(hwif->irq, hwgroup);
710
711         /*
712          * Note that we only release the standard ports,
713          * and do not even try to handle any extra ports
714          * allocated for weird IDE interface chipsets.
715          */
716         hwif_unregister(hwif);
717
718         /*
719          * Remove us from the hwgroup, and free
720          * the hwgroup if we were the only member
721          */
722         d = hwgroup->drive;
723         for (i = 0; i < MAX_DRIVES; ++i) {
724                 drive = &hwif->drives[i];
725                 if (drive->de) {
726                         devfs_unregister(drive->de);
727                         drive->de = NULL;
728                 }
729                 if (!drive->present)
730                         continue;
731                 while (hwgroup->drive->next != drive)
732                         hwgroup->drive = hwgroup->drive->next;
733                 hwgroup->drive->next = drive->next;
734                 if (hwgroup->drive == drive)
735                         hwgroup->drive = NULL;
736                 if (drive->id != NULL) {
737                         kfree(drive->id);
738                         drive->id = NULL;
739                 }
740                 drive->present = 0;
741                 blk_cleanup_queue(&drive->queue);
742         }
743         if (d->present)
744                 hwgroup->drive = d;
745         while (hwgroup->hwif->next != hwif)
746                 hwgroup->hwif = hwgroup->hwif->next;
747         hwgroup->hwif->next = hwif->next;
748         if (hwgroup->hwif == hwif)
749                 kfree(hwgroup);
750         else
751                 hwgroup->hwif = HWIF(hwgroup->drive);
752
753 #if !defined(CONFIG_DMA_NONPCI)
754         if (hwif->dma_base) {
755                 (void) ide_release_dma(hwif);
756
757                 hwif->dma_base = 0;
758                 hwif->dma_master = 0;
759                 hwif->dma_command = 0;
760                 hwif->dma_vendor1 = 0;
761                 hwif->dma_status = 0;
762                 hwif->dma_vendor3 = 0;
763                 hwif->dma_prdtable = 0;
764         }
765 #endif /* !(CONFIG_DMA_NONPCI) */
766
767         /*
768          * Remove us from the kernel's knowledge
769          */
770         unregister_blkdev(hwif->major, hwif->name);
771         kfree(blksize_size[hwif->major]);
772         kfree(max_sectors[hwif->major]);
773         kfree(max_readahead[hwif->major]);
774         blk_dev[hwif->major].data = NULL;
775         blk_dev[hwif->major].queue = NULL;
776         blksize_size[hwif->major] = NULL;
777         gd = hwif->gd;
778         if (gd) {
779                 del_gendisk(gd);
780                 kfree(gd->sizes);
781                 kfree(gd->part);
782                 if (gd->de_arr)
783                         kfree(gd->de_arr);
784                 if (gd->flags)
785                         kfree(gd->flags);
786                 kfree(gd);
787                 hwif->gd = NULL;
788         }
789
790         old_hwif                        = *hwif;
791         init_hwif_data(index);  /* restore hwif data to pristine status */
792         hwif->hwgroup                   = old_hwif.hwgroup;
793
794         hwif->proc                      = old_hwif.proc;
795
796         hwif->major                     = old_hwif.major;
797 //      hwif->index                     = old_hwif.index;
798 //      hwif->channel                   = old_hwif.channel;
799         hwif->straight8                 = old_hwif.straight8;
800         hwif->bus_state                 = old_hwif.bus_state;
801
802         hwif->atapi_dma                 = old_hwif.atapi_dma;
803         hwif->ultra_mask                = old_hwif.ultra_mask;
804         hwif->mwdma_mask                = old_hwif.mwdma_mask;
805         hwif->swdma_mask                = old_hwif.swdma_mask;
806
807         hwif->chipset                   = old_hwif.chipset;
808
809 #ifdef CONFIG_BLK_DEV_IDEPCI
810         hwif->pci_dev                   = old_hwif.pci_dev;
811         hwif->cds                       = old_hwif.cds;
812 #endif /* CONFIG_BLK_DEV_IDEPCI */
813
814 #if 0
815         hwif->hwifops                   = old_hwif.hwifops;
816 #else
817         hwif->identify                  = old_hwif.identify;
818         hwif->tuneproc                  = old_hwif.tuneproc;
819         hwif->speedproc                 = old_hwif.speedproc;
820         hwif->selectproc                = old_hwif.selectproc;
821         hwif->reset_poll                = old_hwif.reset_poll;
822         hwif->pre_reset                 = old_hwif.pre_reset;
823         hwif->resetproc                 = old_hwif.resetproc;
824         hwif->intrproc                  = old_hwif.intrproc;
825         hwif->maskproc                  = old_hwif.maskproc;
826         hwif->quirkproc                 = old_hwif.quirkproc;
827         hwif->busproc                   = old_hwif.busproc;
828 #endif
829
830 #if 0
831         hwif->pioops                    = old_hwif.pioops;
832 #else
833         hwif->ata_input_data            = old_hwif.ata_input_data;
834         hwif->ata_output_data           = old_hwif.ata_output_data;
835         hwif->atapi_input_bytes         = old_hwif.atapi_input_bytes;
836         hwif->atapi_output_bytes        = old_hwif.atapi_output_bytes;
837 #endif
838
839 #if 0
840         hwif->dmaops                    = old_hwif.dmaops;
841 #else
842         hwif->ide_dma_read              = old_hwif.ide_dma_read;
843         hwif->ide_dma_write             = old_hwif.ide_dma_write;
844         hwif->ide_dma_begin             = old_hwif.ide_dma_begin;
845         hwif->ide_dma_end               = old_hwif.ide_dma_end;
846         hwif->ide_dma_check             = old_hwif.ide_dma_check;
847         hwif->ide_dma_on                = old_hwif.ide_dma_on;
848         hwif->ide_dma_off               = old_hwif.ide_dma_off;
849         hwif->ide_dma_off_quietly       = old_hwif.ide_dma_off_quietly;
850         hwif->ide_dma_test_irq          = old_hwif.ide_dma_test_irq;
851         hwif->ide_dma_host_on           = old_hwif.ide_dma_host_on;
852         hwif->ide_dma_host_off          = old_hwif.ide_dma_host_off;
853         hwif->ide_dma_bad_drive         = old_hwif.ide_dma_bad_drive;
854         hwif->ide_dma_good_drive        = old_hwif.ide_dma_good_drive;
855         hwif->ide_dma_count             = old_hwif.ide_dma_count;
856         hwif->ide_dma_verbose           = old_hwif.ide_dma_verbose;
857         hwif->ide_dma_retune            = old_hwif.ide_dma_retune;
858         hwif->ide_dma_lostirq           = old_hwif.ide_dma_lostirq;
859         hwif->ide_dma_timeout           = old_hwif.ide_dma_timeout;
860 #endif
861
862 #if 0
863         hwif->iops                      = old_hwif.iops;
864 #else
865         hwif->OUTB              = old_hwif.OUTB;
866         hwif->OUTW              = old_hwif.OUTW;
867         hwif->OUTL              = old_hwif.OUTL;
868         hwif->OUTSW             = old_hwif.OUTSW;
869         hwif->OUTSL             = old_hwif.OUTSL;
870
871         hwif->INB               = old_hwif.INB;
872         hwif->INW               = old_hwif.INW;
873         hwif->INL               = old_hwif.INL;
874         hwif->INSW              = old_hwif.INSW;
875         hwif->INSL              = old_hwif.INSL;
876 #endif
877
878         hwif->mmio                      = old_hwif.mmio;
879         hwif->rqsize                    = old_hwif.rqsize;
880         hwif->addressing                = old_hwif.addressing;
881 #ifndef CONFIG_BLK_DEV_IDECS
882         hwif->irq                       = old_hwif.irq;
883 #endif /* CONFIG_BLK_DEV_IDECS */
884         hwif->initializing              = old_hwif.initializing;
885
886         hwif->dma_base                  = old_hwif.dma_base;
887         hwif->dma_master                = old_hwif.dma_master;
888         hwif->dma_command               = old_hwif.dma_command;
889         hwif->dma_vendor1               = old_hwif.dma_vendor1;
890         hwif->dma_status                = old_hwif.dma_status;
891         hwif->dma_vendor3               = old_hwif.dma_vendor3;
892         hwif->dma_prdtable              = old_hwif.dma_prdtable;
893
894         hwif->dma_extra                 = old_hwif.dma_extra;
895         hwif->config_data               = old_hwif.config_data;
896         hwif->select_data               = old_hwif.select_data;
897         hwif->autodma                   = old_hwif.autodma;
898         hwif->udma_four                 = old_hwif.udma_four;
899         hwif->no_dsc                    = old_hwif.no_dsc;
900
901         hwif->hwif_data                 = old_hwif.hwif_data;
902         spin_unlock_irqrestore(&io_request_lock, flags);
903         return 0;
904
905 abort:
906         spin_unlock_irqrestore(&io_request_lock, flags);
907         return 1;
908         
909 }
910
911 EXPORT_SYMBOL(ide_unregister);
912
913 /*
914  * Setup hw_regs_t structure described by parameters.  You
915  * may set up the hw structure yourself OR use this routine to
916  * do it for you.
917  */
918 void ide_setup_ports (  hw_regs_t *hw,
919                         ide_ioreg_t base, int *offsets,
920                         ide_ioreg_t ctrl, ide_ioreg_t intr,
921                         ide_ack_intr_t *ack_intr,
922 /*
923  *                      ide_io_ops_t *iops,
924  */
925                         int irq)
926 {
927         int i;
928
929         for (i = 0; i < IDE_NR_PORTS; i++) {
930                 if (offsets[i] == -1) {
931                         switch(i) {
932                                 case IDE_CONTROL_OFFSET:
933                                         hw->io_ports[i] = ctrl;
934                                         break;
935 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
936                                 case IDE_IRQ_OFFSET:
937                                         hw->io_ports[i] = intr;
938                                         break;
939 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
940                                 default:
941                                         hw->io_ports[i] = 0;
942                                         break;
943                         }
944                 } else {
945                         hw->io_ports[i] = base + offsets[i];
946                 }
947         }
948         hw->irq = irq;
949         hw->dma = NO_DMA;
950         hw->ack_intr = ack_intr;
951         hw->chipset = ide_generic;
952 /*
953  *      hw->iops = iops;
954  */
955 }
956
957 EXPORT_SYMBOL(ide_setup_ports);
958
959 /*
960  * Register an IDE interface, specifing exactly the registers etc
961  * Set init=1 iff calling before probes have taken place.
962  */
963 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
964 {
965         int index, retry = 1;
966         ide_hwif_t *hwif;
967
968         do {
969                 for (index = 0; index < MAX_HWIFS; ++index) {
970                         hwif = &ide_hwifs[index];
971                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
972                                 goto found;
973                 }
974                 for (index = 0; index < MAX_HWIFS; ++index) {
975                         hwif = &ide_hwifs[index];
976                         if ((!hwif->present && !hwif->mate && !initializing) ||
977                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
978                                 goto found;
979                 }
980                 for (index = 0; index < MAX_HWIFS; index++)
981                         ide_unregister(index);
982         } while (retry--);
983         return -1;
984 found:
985         if (hwif->present)
986                 ide_unregister(index);
987         if (hwif->present)
988                 return -1;
989         memcpy(&hwif->hw, hw, sizeof(*hw));
990         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
991         hwif->irq = hw->irq;
992         hwif->noprobe = 0;
993         hwif->chipset = hw->chipset;
994
995         if (!initializing) {
996                 ide_probe_module(1);
997 #ifdef CONFIG_PROC_FS
998                 create_proc_ide_interfaces();
999 #endif
1000                 ide_driver_module(1);
1001         }
1002
1003         if (hwifp)
1004                 *hwifp = hwif;
1005
1006         return (initializing || hwif->present) ? index : -1;
1007 }
1008
1009 EXPORT_SYMBOL(ide_register_hw);
1010
1011 /*
1012  * Compatability function with existing drivers.  If you want
1013  * something different, use the function above.
1014  */
1015 int ide_register (int arg1, int arg2, int irq)
1016 {
1017         hw_regs_t hw;
1018         ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
1019         hw.irq = irq;
1020         hw.chipset = ide_unknown;
1021         return ide_register_hw(&hw, NULL);
1022 }
1023
1024 EXPORT_SYMBOL(ide_register);
1025
1026 void 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)
1027 {
1028         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1029
1030         while ((*p) && strcmp((*p)->name, name) < 0)
1031                 p = &((*p)->next);
1032         if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1033                 goto abort;
1034         memset(setting, 0, sizeof(*setting));
1035         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1036                 goto abort;
1037         strcpy(setting->name, name);
1038         setting->rw = rw;
1039         setting->read_ioctl = read_ioctl;
1040         setting->write_ioctl = write_ioctl;
1041         setting->data_type = data_type;
1042         setting->min = min;
1043         setting->max = max;
1044         setting->mul_factor = mul_factor;
1045         setting->div_factor = div_factor;
1046         setting->data = data;
1047         setting->set = set;
1048         setting->next = *p;
1049         if (drive->driver)
1050                 setting->auto_remove = 1;
1051         *p = setting;
1052         return;
1053 abort:
1054         if (setting)
1055                 kfree(setting);
1056 }
1057
1058 EXPORT_SYMBOL(ide_add_setting);
1059
1060 void ide_remove_setting (ide_drive_t *drive, char *name)
1061 {
1062         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
1063
1064         while ((*p) && strcmp((*p)->name, name))
1065                 p = &((*p)->next);
1066         if ((setting = (*p)) == NULL)
1067                 return;
1068         (*p) = setting->next;
1069         kfree(setting->name);
1070         kfree(setting);
1071 }
1072
1073 EXPORT_SYMBOL(ide_remove_setting);
1074
1075 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1076 {
1077         ide_settings_t *setting = drive->settings;
1078
1079         while (setting) {
1080                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1081                         break;
1082                 setting = setting->next;
1083         }
1084         return setting;
1085 }
1086
1087 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1088 {
1089         ide_settings_t *setting = drive->settings;
1090
1091         while (setting) {
1092                 if (strcmp(setting->name, name) == 0)
1093                         break;
1094                 setting = setting->next;
1095         }
1096         return setting;
1097 }
1098
1099 static void auto_remove_settings (ide_drive_t *drive)
1100 {
1101         ide_settings_t *setting;
1102 repeat:
1103         setting = drive->settings;
1104         while (setting) {
1105                 if (setting->auto_remove) {
1106                         ide_remove_setting(drive, setting->name);
1107                         goto repeat;
1108                 }
1109                 setting = setting->next;
1110         }
1111 }
1112
1113 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1114 {
1115         int             val = -EINVAL;
1116         unsigned long   flags;
1117
1118         if ((setting->rw & SETTING_READ)) {
1119                 spin_lock_irqsave(&io_request_lock, flags);
1120                 switch(setting->data_type) {
1121                         case TYPE_BYTE:
1122                                 val = *((u8 *) setting->data);
1123                                 break;
1124                         case TYPE_SHORT:
1125                                 val = *((u16 *) setting->data);
1126                                 break;
1127                         case TYPE_INT:
1128                         case TYPE_INTA:
1129                                 val = *((u32 *) setting->data);
1130                                 break;
1131                 }
1132                 spin_unlock_irqrestore(&io_request_lock, flags);
1133         }
1134         return val;
1135 }
1136
1137 int ide_spin_wait_hwgroup (ide_drive_t *drive)
1138 {
1139         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1140         unsigned long timeout = jiffies + (3 * HZ);
1141
1142         spin_lock_irq(&io_request_lock);
1143
1144         while (hwgroup->busy) {
1145                 unsigned long lflags;
1146                 spin_unlock_irq(&io_request_lock);
1147                 local_irq_set(lflags);
1148                 if (time_after(jiffies, timeout)) {
1149                         local_irq_restore(lflags);
1150                         printk(KERN_ERR "%s: channel busy\n", drive->name);
1151                         return -EBUSY;
1152                 }
1153                 local_irq_restore(lflags);
1154                 spin_lock_irq(&io_request_lock);
1155         }
1156         return 0;
1157 }
1158
1159 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1160
1161 /*
1162  * FIXME:  This should be changed to enqueue a special request
1163  * to the driver to change settings, and then wait on a sema for completion.
1164  * The current scheme of polling is kludgey, though safe enough.
1165  */
1166 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1167 {
1168         int i;
1169         u32 *p;
1170
1171         if (!capable(CAP_SYS_ADMIN))
1172                 return -EACCES;
1173         if (!(setting->rw & SETTING_WRITE))
1174                 return -EPERM;
1175         if (val < setting->min || val > setting->max)
1176                 return -EINVAL;
1177         if (setting->set)
1178                 return setting->set(drive, val);
1179         if (ide_spin_wait_hwgroup(drive))
1180                 return -EBUSY;
1181         switch (setting->data_type) {
1182                 case TYPE_BYTE:
1183                         *((u8 *) setting->data) = val;
1184                         break;
1185                 case TYPE_SHORT:
1186                         *((u16 *) setting->data) = val;
1187                         break;
1188                 case TYPE_INT:
1189                         *((u32 *) setting->data) = val;
1190                         break;
1191                 case TYPE_INTA:
1192                         p = (u32 *) setting->data;
1193                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1194                                 *p = val;
1195                         break;
1196         }
1197         spin_unlock_irq(&io_request_lock);
1198         return 0;
1199 }
1200
1201 EXPORT_SYMBOL(ide_write_setting);
1202
1203 static int set_io_32bit(ide_drive_t *drive, int arg)
1204 {
1205         drive->io_32bit = arg;
1206 #ifdef CONFIG_BLK_DEV_DTC2278
1207         if (HWIF(drive)->chipset == ide_dtc2278)
1208                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1209 #endif /* CONFIG_BLK_DEV_DTC2278 */
1210         return 0;
1211 }
1212
1213 static int set_using_dma (ide_drive_t *drive, int arg)
1214 {
1215         if (!drive->driver || !DRIVER(drive)->supports_dma)
1216                 return -EPERM;
1217         if (!drive->id || !(drive->id->capability & 1))
1218                 return -EPERM;
1219         if (HWIF(drive)->ide_dma_check == NULL)
1220                 return -EPERM;
1221         if (arg) {
1222                 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1223         } else {
1224                 if (HWIF(drive)->ide_dma_off(drive)) return -EIO;
1225         }
1226         return 0;
1227 }
1228
1229 static int set_pio_mode (ide_drive_t *drive, int arg)
1230 {
1231         struct request rq;
1232
1233         if (!HWIF(drive)->tuneproc)
1234                 return -ENOSYS;
1235         if (drive->special.b.set_tune)
1236                 return -EBUSY;
1237         ide_init_drive_cmd(&rq);
1238         drive->tune_req = (u8) arg;
1239         drive->special.b.set_tune = 1;
1240         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1241         return 0;
1242 }
1243
1244 static int set_xfer_rate (ide_drive_t *drive, int arg)
1245 {
1246         int err = ide_wait_cmd(drive,
1247                         WIN_SETFEATURES, (u8) arg,
1248                         SETFEATURES_XFER, 0, NULL);
1249
1250         if (!err && arg) {
1251                 ide_set_xfer_rate(drive, (u8) arg);
1252                 ide_driveid_update(drive);
1253         }
1254         return err;
1255 }
1256
1257 int ide_atapi_to_scsi (ide_drive_t *drive, int arg)
1258 {
1259         if (drive->media == ide_disk) {
1260                 drive->scsi = 0;
1261                 return 0;
1262         }
1263         if (drive->driver != NULL) {
1264                 if (DRIVER(drive)->cleanup(drive)) {
1265                         drive->scsi = 0;
1266                         return 0;
1267                 }
1268         }
1269         drive->scsi = (u8) arg;
1270         ide_attach_drive(drive);
1271         return 0;
1272 }
1273
1274 void ide_add_generic_settings (ide_drive_t *drive)
1275 {
1276 /*
1277  *                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function
1278  */
1279         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);
1280         ide_add_setting(drive,  "keepsettings",         SETTING_RW,                                     HDIO_GET_KEEPSETTINGS,  HDIO_SET_KEEPSETTINGS,  TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL);
1281         ide_add_setting(drive,  "nice1",                SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL);
1282         ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
1283         ide_add_setting(drive,  "slow",                 SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->slow,                   NULL);
1284         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);
1285         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);
1286         ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->init_speed,             NULL);
1287         ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->current_speed,          set_xfer_rate);
1288         ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
1289         if (drive->media != ide_disk)
1290                 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);
1291 }
1292
1293 /*
1294  * Delay for *at least* 50ms.  As we don't know how much time is left
1295  * until the next tick occurs, we wait an extra tick to be safe.
1296  * This is used only during the probing/polling for drives at boot time.
1297  *
1298  * However, its usefullness may be needed in other places, thus we export it now.
1299  * The future may change this to a millisecond setable delay.
1300  */
1301 void ide_delay_50ms (void)
1302 {
1303 #ifndef CONFIG_BLK_DEV_IDECS
1304         mdelay(50);
1305 #else
1306         __set_current_state(TASK_UNINTERRUPTIBLE);
1307         schedule_timeout(HZ/20);
1308 #endif /* CONFIG_BLK_DEV_IDECS */
1309 }
1310
1311 EXPORT_SYMBOL(ide_delay_50ms);
1312
1313 int system_bus_clock (void)
1314 {
1315         return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1316 }
1317
1318 EXPORT_SYMBOL(system_bus_clock);
1319
1320 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1321 {
1322         if (!drive->present || drive->busy || drive->usage || drive->dead)
1323                 goto abort;
1324         if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1325                 goto abort;
1326         strncpy(drive->driver_req, driver, 9);
1327         ide_driver_module(0);
1328         drive->driver_req[0] = 0;
1329         ide_driver_module(0);
1330         if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
1331                 return 0;
1332 abort:
1333         return 1;
1334 }
1335
1336 EXPORT_SYMBOL(ide_replace_subdriver);
1337
1338 int ide_attach_drive (ide_drive_t *drive)
1339 {
1340         /* Someone unplugged the device on us */
1341         if(drive->dead)
1342                 return 1;
1343                 
1344 #ifdef CONFIG_BLK_DEV_IDESCSI
1345         if (drive->scsi) {
1346                 extern int idescsi_attach(ide_drive_t *drive);
1347                 if (idescsi_attach(drive))
1348                         return 0;
1349         }
1350 #endif /* CONFIG_BLK_DEV_IDESCSI */
1351
1352         switch (drive->media) {
1353 #ifdef CONFIG_BLK_DEV_IDECD
1354                 case ide_cdrom:
1355                 {
1356                         extern int ide_cdrom_attach(ide_drive_t *drive);
1357                         if (ide_cdrom_attach(drive))
1358                                 return 1;
1359                         break;
1360                 }
1361 #endif /* CONFIG_BLK_DEV_IDECD */
1362 #ifdef CONFIG_BLK_DEV_IDEDISK
1363                 case ide_disk:
1364                 {
1365                         extern int idedisk_attach(ide_drive_t *drive);
1366                         if (idedisk_attach(drive))
1367                                 return 1;
1368                         break;
1369                 }
1370 #endif /* CONFIG_BLK_DEV_IDEDISK */
1371 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
1372                 case ide_floppy:
1373                 {
1374                         extern int idefloppy_attach(ide_drive_t *drive);
1375                         if (idefloppy_attach(drive))
1376                                 return 1;
1377                         break;
1378                 }
1379 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
1380 #ifdef CONFIG_BLK_DEV_IDETAPE
1381                 case ide_tape:
1382                 {
1383                         extern int idetape_attach(ide_drive_t *drive);
1384                         if (idetape_attach(drive))
1385                                 return 1;
1386                         break;
1387                 }
1388 #endif /* CONFIG_BLK_DEV_IDETAPE */
1389                 default:
1390                         return 1;
1391         }
1392         return 0;
1393 }
1394
1395 EXPORT_SYMBOL(ide_attach_drive);
1396
1397 static int ide_ioctl (struct inode *inode, struct file *file,
1398                         unsigned int cmd, unsigned long arg)
1399 {
1400         int err = 0, major, minor;
1401         ide_drive_t *drive;
1402         struct request rq;
1403         kdev_t dev;
1404         ide_settings_t *setting;
1405
1406         if (!inode || !(dev = inode->i_rdev))
1407                 return -EINVAL;
1408         major = MAJOR(dev); minor = MINOR(dev);
1409         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1410                 return -ENODEV;
1411
1412         if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1413                 if (cmd == setting->read_ioctl) {
1414                         err = ide_read_setting(drive, setting);
1415                         return err >= 0 ? put_user(err, (long *) arg) : err;
1416                 } else {
1417                         if ((MINOR(inode->i_rdev) & PARTN_MASK))
1418                                 return -EINVAL;
1419                         return ide_write_setting(drive, setting, arg);
1420                 }
1421         }
1422
1423         ide_init_drive_cmd (&rq);
1424         switch (cmd) {
1425                 case HDIO_GETGEO:
1426                 {
1427                         struct hd_geometry *loc = (struct hd_geometry *) arg;
1428                         u16 bios_cyl = drive->bios_cyl; /* truncate */
1429                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1430                         if (put_user(drive->bios_head, (u8 *) &loc->heads)) return -EFAULT;
1431                         if (put_user(drive->bios_sect, (u8 *) &loc->sectors)) return -EFAULT;
1432                         if (put_user(bios_cyl, (u16 *) &loc->cylinders)) return -EFAULT;
1433                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1434                                 (unsigned long *) &loc->start)) return -EFAULT;
1435                         return 0;
1436                 }
1437
1438                 case HDIO_GETGEO_BIG:
1439                 {
1440                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
1441                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1442                         if (put_user(drive->bios_head, (u8 *) &loc->heads)) return -EFAULT;
1443                         if (put_user(drive->bios_sect, (u8 *) &loc->sectors)) return -EFAULT;
1444                         if (put_user(drive->bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
1445                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1446                                 (unsigned long *) &loc->start)) return -EFAULT;
1447                         return 0;
1448                 }
1449
1450                 case HDIO_GETGEO_BIG_RAW:
1451                 {
1452                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
1453                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1454                         if (put_user(drive->head, (u8 *) &loc->heads)) return -EFAULT;
1455                         if (put_user(drive->sect, (u8 *) &loc->sectors)) return -EFAULT;
1456                         if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
1457                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1458                                 (unsigned long *) &loc->start)) return -EFAULT;
1459                         return 0;
1460                 }
1461
1462                 case BLKGETSIZE:   /* Return device size */
1463                         return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (unsigned long *) arg);
1464                 case BLKGETSIZE64:
1465                         return put_user((u64)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects << 9, (u64 *) arg);
1466
1467                 case BLKRRPART: /* Re-read partition tables */
1468                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1469                         return ide_revalidate_disk(inode->i_rdev);
1470
1471                 case HDIO_OBSOLETE_IDENTITY:
1472                 case HDIO_GET_IDENTITY:
1473                         if (MINOR(inode->i_rdev) & PARTN_MASK)
1474                                 return -EINVAL;
1475                         if (drive->id == NULL)
1476                                 return -ENOMSG;
1477                         if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1478                                 return -EFAULT;
1479                         return 0;
1480
1481                 case HDIO_GET_NICE:
1482                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
1483                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
1484                                         drive->nice0            <<      IDE_NICE_0              |
1485                                         drive->nice1            <<      IDE_NICE_1              |
1486                                         drive->nice2            <<      IDE_NICE_2,
1487                                         (long *) arg);
1488
1489 #ifdef CONFIG_IDE_TASK_IOCTL
1490                 case HDIO_DRIVE_TASKFILE:
1491                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1492                                 return -EACCES;
1493                         switch(drive->media) {
1494                                 case ide_disk:
1495                                         return ide_taskfile_ioctl(drive, inode, file, cmd, arg);
1496 #ifdef CONFIG_PKT_TASK_IOCTL
1497                                 case ide_cdrom:
1498                                 case ide_tape:
1499                                 case ide_floppy:
1500                                         return pkt_taskfile_ioctl(drive, inode, file, cmd, arg);
1501 #endif /* CONFIG_PKT_TASK_IOCTL */
1502                                 default:
1503                                         return -ENOMSG;
1504                         }
1505 #endif /* CONFIG_IDE_TASK_IOCTL */
1506
1507                 case HDIO_DRIVE_CMD:
1508                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1509                                 return -EACCES;
1510                         return ide_cmd_ioctl(drive, inode, file, cmd, arg);
1511
1512                 case HDIO_DRIVE_TASK:
1513                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1514                                 return -EACCES;
1515                         return ide_task_ioctl(drive, inode, file, cmd, arg);
1516
1517                 case HDIO_SCAN_HWIF:
1518                 {
1519                         int args[3];
1520                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1521                         if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
1522                                 return -EFAULT;
1523                         if (ide_register(args[0], args[1], args[2]) == -1)
1524                                 return -EIO;
1525                         return 0;
1526                 }
1527                 case HDIO_UNREGISTER_HWIF:
1528                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1529                         /* (arg > MAX_HWIFS) checked in function */
1530                         ide_unregister(arg);
1531                         return 0;
1532                 case HDIO_SET_NICE:
1533                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1534                         if (drive->driver == NULL)
1535                                 return -EPERM;
1536                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1537                                 return -EPERM;
1538                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1539                         if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1540                                 drive->dsc_overlap = 0;
1541                                 return -EPERM;
1542                         }
1543                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
1544                         return 0;
1545                 case HDIO_DRIVE_RESET:
1546                 {
1547                         unsigned long flags;
1548                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1549 #if 1
1550                         spin_lock_irqsave(&io_request_lock, flags);
1551                         if ( HWGROUP(drive)->handler != NULL) {
1552                                 printk(KERN_ERR "%s: ide_set_handler: handler not null; %p\n", drive->name, HWGROUP(drive)->handler);
1553                                 (void) HWGROUP(drive)->handler(drive);
1554 //                              HWGROUP(drive)->handler = NULL;
1555                                 HWGROUP(drive)->expiry  = NULL;
1556                                 del_timer(&HWGROUP(drive)->timer);
1557                         }
1558                         spin_unlock_irqrestore(&io_request_lock, flags);
1559
1560 #endif
1561                         (void) ide_do_reset(drive);
1562                         if (drive->suspend_reset) {
1563 /*
1564  *                              APM WAKE UP todo !!
1565  *                              int nogoodpower = 1;
1566  *                              while(nogoodpower) {
1567  *                                      check_power1() or check_power2()
1568  *                                      nogoodpower = 0;
1569  *                              } 
1570  *                              HWIF(drive)->multiproc(drive);
1571  */
1572                                 return ide_revalidate_disk(inode->i_rdev);
1573                         }
1574                         return 0;
1575                 }
1576                 case BLKROSET:
1577                 case BLKROGET:
1578                 case BLKFLSBUF:
1579                 case BLKSSZGET:
1580                 case BLKPG:
1581                 case BLKELVGET:
1582                 case BLKELVSET:
1583                 case BLKBSZGET:
1584                 case BLKBSZSET:
1585                         return blk_ioctl(inode->i_rdev, cmd, arg);
1586
1587                 case HDIO_GET_BUSSTATE:
1588                         if (!capable(CAP_SYS_ADMIN))
1589                                 return -EACCES;
1590                         if (put_user(HWIF(drive)->bus_state, (long *)arg))
1591                                 return -EFAULT;
1592                         return 0;
1593
1594                 case HDIO_SET_BUSSTATE:
1595                         if (!capable(CAP_SYS_ADMIN))
1596                                 return -EACCES;
1597                         if (HWIF(drive)->busproc)
1598                                 HWIF(drive)->busproc(drive, (int)arg);
1599                         return 0;
1600
1601                 default:
1602                         if (drive->driver != NULL)
1603                                 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
1604                         return -EPERM;
1605         }
1606 }
1607
1608 static int ide_check_media_change (kdev_t i_rdev)
1609 {
1610         ide_drive_t *drive;
1611
1612         if ((drive = get_info_ptr(i_rdev)) == NULL)
1613                 return -ENODEV;
1614         if (drive->driver != NULL)
1615                 return DRIVER(drive)->media_change(drive);
1616         return 0;
1617 }
1618
1619 /*
1620  * stridx() returns the offset of c within s,
1621  * or -1 if c is '\0' or not found within s.
1622  */
1623 static int __init stridx (const char *s, char c)
1624 {
1625         char *i = strchr(s, c);
1626         return (i && c) ? i - s : -1;
1627 }
1628
1629 /*
1630  * match_parm() does parsing for ide_setup():
1631  *
1632  * 1. the first char of s must be '='.
1633  * 2. if the remainder matches one of the supplied keywords,
1634  *     the index (1 based) of the keyword is negated and returned.
1635  * 3. if the remainder is a series of no more than max_vals numbers
1636  *     separated by commas, the numbers are saved in vals[] and a
1637  *     count of how many were saved is returned.  Base10 is assumed,
1638  *     and base16 is allowed when prefixed with "0x".
1639  * 4. otherwise, zero is returned.
1640  */
1641 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1642 {
1643         static const char *decimal = "0123456789";
1644         static const char *hex = "0123456789abcdef";
1645         int i, n;
1646
1647         if (*s++ == '=') {
1648                 /*
1649                  * Try matching against the supplied keywords,
1650                  * and return -(index+1) if we match one
1651                  */
1652                 if (keywords != NULL) {
1653                         for (i = 0; *keywords != NULL; ++i) {
1654                                 if (!strcmp(s, *keywords++))
1655                                         return -(i+1);
1656                         }
1657                 }
1658                 /*
1659                  * Look for a series of no more than "max_vals"
1660                  * numeric values separated by commas, in base10,
1661                  * or base16 when prefixed with "0x".
1662                  * Return a count of how many were found.
1663                  */
1664                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1665                         vals[n] = i;
1666                         while ((i = stridx(decimal, *++s)) >= 0)
1667                                 vals[n] = (vals[n] * 10) + i;
1668                         if (*s == 'x' && !vals[n]) {
1669                                 while ((i = stridx(hex, *++s)) >= 0)
1670                                         vals[n] = (vals[n] * 0x10) + i;
1671                         }
1672                         if (++n == max_vals)
1673                                 break;
1674                         if (*s == ',' || *s == ';')
1675                                 ++s;
1676                 }
1677                 if (!*s)
1678                         return n;
1679         }
1680         return 0;       /* zero = nothing matched */
1681 }
1682
1683 /*
1684  * ide_setup() gets called VERY EARLY during initialization,
1685  * to handle kernel "command line" strings beginning with "hdx="
1686  * or "ide".  Here is the complete set currently supported:
1687  *
1688  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
1689  * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
1690  *
1691  * "hdx=noprobe"        : drive may be present, but do not probe for it
1692  * "hdx=none"           : drive is NOT present, ignore cmos and do not probe
1693  * "hdx=nowerr"         : ignore the WRERR_STAT bit on this drive
1694  * "hdx=cdrom"          : drive is present, and is a cdrom drive
1695  * "hdx=cyl,head,sect"  : disk drive is present, with specified geometry
1696  * "hdx=noremap"        : do not remap 0->1 even though EZD was detected
1697  * "hdx=autotune"       : driver will attempt to tune interface speed
1698  *                              to the fastest PIO mode supported,
1699  *                              if possible for this drive only.
1700  *                              Not fully supported by all chipset types,
1701  *                              and quite likely to cause trouble with
1702  *                              older/odd IDE drives.
1703  *
1704  * "hdx=slow"           : insert a huge pause after each access to the data
1705  *                              port. Should be used only as a last resort.
1706  *
1707  * "hdx=swapdata"       : when the drive is a disk, byte swap all data
1708  * "hdx=bswap"          : same as above..........
1709  * "hdxlun=xx"          : set the drive last logical unit.
1710  * "hdx=flash"          : allows for more than one ata_flash disk to be
1711  *                              registered. In most cases, only one device
1712  *                              will be present.
1713  * "hdx=scsi"           : the return of the ide-scsi flag, this is useful for
1714  *                              allowwing ide-floppy, ide-tape, and ide-cdrom|writers
1715  *                              to use ide-scsi emulation on a device specific option.
1716  * "idebus=xx"          : inform IDE driver of VESA/PCI bus speed in MHz,
1717  *                              where "xx" is between 20 and 66 inclusive,
1718  *                              used when tuning chipset PIO modes.
1719  *                              For PCI bus, 25 is correct for a P75 system,
1720  *                              30 is correct for P90,P120,P180 systems,
1721  *                              and 33 is used for P100,P133,P166 systems.
1722  *                              If in doubt, use idebus=33 for PCI.
1723  *                              As for VLB, it is safest to not specify it.
1724  *
1725  * "idex=noprobe"       : do not attempt to access/use this interface
1726  * "idex=base"          : probe for an interface at the addr specified,
1727  *                              where "base" is usually 0x1f0 or 0x170
1728  *                              and "ctl" is assumed to be "base"+0x206
1729  * "idex=base,ctl"      : specify both base and ctl
1730  * "idex=base,ctl,irq"  : specify base, ctl, and irq number
1731  * "idex=autotune"      : driver will attempt to tune interface speed
1732  *                              to the fastest PIO mode supported,
1733  *                              for all drives on this interface.
1734  *                              Not fully supported by all chipset types,
1735  *                              and quite likely to cause trouble with
1736  *                              older/odd IDE drives.
1737  * "idex=noautotune"    : driver will NOT attempt to tune interface speed
1738  *                              This is the default for most chipsets,
1739  *                              except the cmd640.
1740  * "idex=serialize"     : do not overlap operations on idex and ide(x^1)
1741  * "idex=four"          : four drives on idex and ide(x^1) share same ports
1742  * "idex=reset"         : reset interface before first use
1743  * "idex=dma"           : enable DMA by default on both drives if possible
1744  * "idex=ata66"         : informs the interface that it has an 80c cable
1745  *                              for chipsets that are ATA-66 capable, but
1746  *                              the ablity to bit test for detection is
1747  *                              currently unknown.
1748  * "ide=reverse"        : Formerly called to pci sub-system, but now local.
1749  *
1750  * The following are valid ONLY on ide0, (except dc4030)
1751  * and the defaults for the base,ctl ports must not be altered.
1752  *
1753  * "ide0=dtc2278"       : probe/support DTC2278 interface
1754  * "ide0=ht6560b"       : probe/support HT6560B interface
1755  * "ide0=cmd640_vlb"    : *REQUIRED* for VLB cards with the CMD640 chip
1756  *                        (not for PCI -- automatically detected)
1757  * "ide0=qd65xx"        : probe/support qd65xx interface
1758  * "ide0=ali14xx"       : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
1759  * "ide0=umc8672"       : probe/support umc8672 chipsets
1760  * "idex=dc4030"        : probe/support Promise DC4030VL interface
1761  * "ide=doubler"        : probe/support IDE doublers on Amiga
1762  */
1763 int __init ide_setup (char *s)
1764 {
1765         int i, vals[3];
1766         ide_hwif_t *hwif;
1767         ide_drive_t *drive;
1768         unsigned int hw, unit;
1769         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1770         const char max_hwif  = '0' + (MAX_HWIFS - 1);
1771
1772         
1773         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
1774                 return 0;                               /* driver and not us */
1775
1776         if (strncmp(s,"ide",3) &&
1777             strncmp(s,"idebus",6) &&
1778             strncmp(s,"hd",2))          /* hdx= & hdxlun= */
1779                 return 0;
1780
1781         printk(KERN_INFO "ide_setup: %s", s);
1782         init_ide_data ();
1783
1784 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1785         if (!strcmp(s, "ide=doubler")) {
1786                 extern int ide_doubler;
1787
1788                 printk(" : Enabled support for IDE doublers\n");
1789                 ide_doubler = 1;
1790                 return 1;
1791         }
1792 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1793
1794         if (!strcmp(s, "ide=nodma")) {
1795                 printk(" : Prevented DMA\n");
1796                 noautodma = 1;
1797                 return 1;
1798         }
1799
1800 #ifdef CONFIG_BLK_DEV_IDEPCI
1801         if (!strcmp(s, "ide=reverse")) {
1802                 ide_scan_direction = 1;
1803                 printk(" : Enabled support for IDE inverse scan order.\n");
1804                 return 1;
1805         }
1806 #endif /* CONFIG_BLK_DEV_IDEPCI */
1807
1808         /*
1809          * Look for drive options:  "hdx="
1810          */
1811         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1812                 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
1813                                 "serialize", "autotune", "noautotune",
1814                                 "slow", "swapdata", "bswap", "flash",
1815                                 "remap", "noremap", "scsi", NULL};
1816                 unit = s[2] - 'a';
1817                 hw   = unit / MAX_DRIVES;
1818                 unit = unit % MAX_DRIVES;
1819                 hwif = &ide_hwifs[hw];
1820                 drive = &hwif->drives[unit];
1821                 if (strncmp(s + 4, "ide-", 4) == 0) {
1822                         strncpy(drive->driver_req, s + 4, 9);
1823                         goto done;
1824                 }
1825                 /*
1826                  * Look for last lun option:  "hdxlun="
1827                  */
1828                 if (s[3] == 'l' && s[4] == 'u' && s[5] == 'n') {
1829                         if (match_parm(&s[6], NULL, vals, 1) != 1)
1830                                 goto bad_option;
1831                         if (vals[0] >= 0 && vals[0] <= 7) {
1832                                 drive->last_lun = vals[0];
1833                                 drive->forced_lun = 1;
1834                         } else
1835                                 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
1836                         goto done;
1837                 }
1838                 switch (match_parm(&s[3], hd_words, vals, 3)) {
1839                         case -1: /* "none" */
1840                                 drive->nobios = 1;  /* drop into "noprobe" */
1841                         case -2: /* "noprobe" */
1842                                 drive->noprobe = 1;
1843                                 goto done;
1844                         case -3: /* "nowerr" */
1845                                 drive->bad_wstat = BAD_R_STAT;
1846                                 hwif->noprobe = 0;
1847                                 goto done;
1848                         case -4: /* "cdrom" */
1849                                 drive->present = 1;
1850                                 drive->media = ide_cdrom;
1851                                 hwif->noprobe = 0;
1852                                 goto done;
1853                         case -5: /* "serialize" */
1854                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
1855                                 goto do_serialize;
1856                         case -6: /* "autotune" */
1857                                 drive->autotune = 1;
1858                                 goto done;
1859                         case -7: /* "noautotune" */
1860                                 drive->autotune = 2;
1861                                 goto done;
1862                         case -8: /* "slow" */
1863                                 drive->slow = 1;
1864                                 goto done;
1865                         case -9: /* "swapdata" or "bswap" */
1866                         case -10:
1867                                 drive->bswap = 1;
1868                                 goto done;
1869                         case -11: /* "flash" */
1870                                 drive->ata_flash = 1;
1871                                 goto done;
1872                         case -12: /* "remap" */
1873                                 drive->remap_0_to_1 = 1;
1874                                 goto done;
1875                         case -13: /* "noremap" */
1876                                 drive->remap_0_to_1 = 2;
1877                                 goto done;
1878                         case -14: /* "scsi" */
1879 #if defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI)
1880                                 drive->scsi = 1;
1881                                 goto done;
1882 #else
1883                                 drive->scsi = 0;
1884                                 goto bad_option;
1885 #endif /* defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI) */
1886                         case 3: /* cyl,head,sect */
1887                                 drive->media    = ide_disk;
1888                                 drive->cyl      = drive->bios_cyl  = vals[0];
1889                                 drive->head     = drive->bios_head = vals[1];
1890                                 drive->sect     = drive->bios_sect = vals[2];
1891                                 drive->present  = 1;
1892                                 drive->forced_geom = 1;
1893                                 hwif->noprobe = 0;
1894                                 goto done;
1895                         default:
1896                                 goto bad_option;
1897                 }
1898         }
1899
1900         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1901                 goto bad_option;
1902         /*
1903          * Look for bus speed option:  "idebus="
1904          */
1905         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1906                 if (match_parm(&s[6], NULL, vals, 1) != 1)
1907                         goto bad_option;
1908                 if (vals[0] >= 20 && vals[0] <= 66) {
1909                         idebus_parameter = vals[0];
1910                 } else
1911                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1912                 goto done;
1913         }
1914         /*
1915          * Look for interface options:  "idex="
1916          */
1917         if (s[3] >= '0' && s[3] <= max_hwif) {
1918                 /*
1919                  * Be VERY CAREFUL changing this: note hardcoded indexes below
1920                  * -8,-9,-10 : are reserved for future idex calls to ease the hardcoding.
1921                  */
1922                 const char *ide_words[] = {
1923                         "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66",
1924                         "minus8", "minus9", "minus10",
1925                         "four", "qd65xx", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
1926                 hw = s[3] - '0';
1927                 hwif = &ide_hwifs[hw];
1928                 i = match_parm(&s[4], ide_words, vals, 3);
1929
1930                 /*
1931                  * Cryptic check to ensure chipset not already set for hwif:
1932                  */
1933                 if (i > 0 || i <= -11) {                        /* is parameter a chipset name? */
1934                         if (hwif->chipset != ide_unknown)
1935                                 goto bad_option;        /* chipset already specified */
1936                         if (i <= -11 && i != -18 && hw != 0)
1937                                 goto bad_hwif;          /* chipset drivers are for "ide0=" only */
1938                         if (i <= -11 && i != -18 && ide_hwifs[hw+1].chipset != ide_unknown)
1939                                 goto bad_option;        /* chipset for 2nd port already specified */
1940                         printk("\n");
1941                 }
1942
1943                 switch (i) {
1944 #ifdef CONFIG_BLK_DEV_PDC4030
1945                         case -18: /* "dc4030" */
1946                         {
1947                                 extern void init_pdc4030(void);
1948                                 init_pdc4030();
1949                                 goto done;
1950                         }
1951 #endif /* CONFIG_BLK_DEV_PDC4030 */
1952 #ifdef CONFIG_BLK_DEV_ALI14XX
1953                         case -17: /* "ali14xx" */
1954                         {
1955                                 extern void init_ali14xx (void);
1956                                 init_ali14xx();
1957                                 goto done;
1958                         }
1959 #endif /* CONFIG_BLK_DEV_ALI14XX */
1960 #ifdef CONFIG_BLK_DEV_UMC8672
1961                         case -16: /* "umc8672" */
1962                         {
1963                                 extern void init_umc8672 (void);
1964                                 init_umc8672();
1965                                 goto done;
1966                         }
1967 #endif /* CONFIG_BLK_DEV_UMC8672 */
1968 #ifdef CONFIG_BLK_DEV_DTC2278
1969                         case -15: /* "dtc2278" */
1970                         {
1971                                 extern void init_dtc2278 (void);
1972                                 init_dtc2278();
1973                                 goto done;
1974                         }
1975 #endif /* CONFIG_BLK_DEV_DTC2278 */
1976 #ifdef CONFIG_BLK_DEV_CMD640
1977                         case -14: /* "cmd640_vlb" */
1978                         {
1979                                 extern int cmd640_vlb; /* flag for cmd640.c */
1980                                 cmd640_vlb = 1;
1981                                 goto done;
1982                         }
1983 #endif /* CONFIG_BLK_DEV_CMD640 */
1984 #ifdef CONFIG_BLK_DEV_HT6560B
1985                         case -13: /* "ht6560b" */
1986                         {
1987                                 extern void init_ht6560b (void);
1988                                 init_ht6560b();
1989                                 goto done;
1990                         }
1991 #endif /* CONFIG_BLK_DEV_HT6560B */
1992 #if CONFIG_BLK_DEV_QD65XX
1993                         case -12: /* "qd65xx" */
1994                         {
1995                                 extern void init_qd65xx (void);
1996                                 init_qd65xx();
1997                                 goto done;
1998                         }
1999 #endif /* CONFIG_BLK_DEV_QD65XX */
2000 #ifdef CONFIG_BLK_DEV_4DRIVES
2001                         case -11: /* "four" drives on one set of ports */
2002                         {
2003                                 ide_hwif_t *mate = &ide_hwifs[hw^1];
2004                                 mate->drives[0].select.all ^= 0x20;
2005                                 mate->drives[1].select.all ^= 0x20;
2006                                 hwif->chipset = mate->chipset = ide_4drives;
2007                                 mate->irq = hwif->irq;
2008                                 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2009                                 goto do_serialize;
2010                         }
2011 #endif /* CONFIG_BLK_DEV_4DRIVES */
2012                         case -10: /* minus10 */
2013                         case -9: /* minus9 */
2014                         case -8: /* minus8 */
2015                                 goto bad_option;
2016                         case -7: /* ata66 */
2017 #ifdef CONFIG_BLK_DEV_IDEPCI
2018                                 hwif->udma_four = 1;
2019                                 goto done;
2020 #else /* !CONFIG_BLK_DEV_IDEPCI */
2021                                 hwif->udma_four = 0;
2022                                 goto bad_hwif;
2023 #endif /* CONFIG_BLK_DEV_IDEPCI */
2024                         case -6: /* dma */
2025                                 hwif->autodma = 1;
2026                                 goto done;
2027                         case -5: /* "reset" */
2028                                 hwif->reset = 1;
2029                                 goto done;
2030                         case -4: /* "noautotune" */
2031                                 hwif->drives[0].autotune = 2;
2032                                 hwif->drives[1].autotune = 2;
2033                                 goto done;
2034                         case -3: /* "autotune" */
2035                                 hwif->drives[0].autotune = 1;
2036                                 hwif->drives[1].autotune = 1;
2037                                 goto done;
2038                         case -2: /* "serialize" */
2039                         do_serialize:
2040                                 hwif->mate = &ide_hwifs[hw^1];
2041                                 hwif->mate->mate = hwif;
2042                                 hwif->serialized = hwif->mate->serialized = 1;
2043                                 goto done;
2044
2045                         case -1: /* "noprobe" */
2046                                 hwif->noprobe = 1;
2047                                 goto done;
2048
2049                         case 1: /* base */
2050                                 vals[1] = vals[0] + 0x206; /* default ctl */
2051                         case 2: /* base,ctl */
2052                                 vals[2] = 0;    /* default irq = probe for it */
2053                         case 3: /* base,ctl,irq */
2054                                 hwif->hw.irq = vals[2];
2055                                 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
2056                                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2057                                 hwif->irq      = vals[2];
2058                                 hwif->noprobe  = 0;
2059                                 hwif->chipset  = ide_generic;
2060                                 goto done;
2061
2062                         case 0: goto bad_option;
2063                         default:
2064                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2065                                 return 1;
2066                 }
2067         }
2068 bad_option:
2069         printk(" -- BAD OPTION\n");
2070         return 1;
2071 bad_hwif:
2072         printk("-- NOT SUPPORTED ON ide%d", hw);
2073 done:
2074         printk("\n");
2075         return 1;
2076 }
2077
2078 /*
2079  * probe_for_hwifs() finds/initializes "known" IDE interfaces
2080  */
2081 static void __init probe_for_hwifs (void)
2082 {
2083 #ifdef CONFIG_BLK_DEV_IDEPCI
2084         if (pci_present())
2085         {
2086                 ide_scan_pcibus(ide_scan_direction);
2087         }
2088 #endif /* CONFIG_BLK_DEV_IDEPCI */
2089
2090 #ifdef CONFIG_ETRAX_IDE
2091         {
2092                 extern void init_e100_ide(void);
2093                 init_e100_ide();
2094         }
2095 #endif /* CONFIG_ETRAX_IDE */
2096 #ifdef CONFIG_BLK_DEV_CMD640
2097         {
2098                 extern void ide_probe_for_cmd640x(void);
2099                 ide_probe_for_cmd640x();
2100         }
2101 #endif /* CONFIG_BLK_DEV_CMD640 */
2102 #ifdef CONFIG_BLK_DEV_PDC4030
2103         {
2104                 extern int ide_probe_for_pdc4030(void);
2105                 (void) ide_probe_for_pdc4030();
2106         }
2107 #endif /* CONFIG_BLK_DEV_PDC4030 */
2108 #ifdef CONFIG_BLK_DEV_IDE_PMAC
2109         {
2110                 extern void pmac_ide_probe(void);
2111                 pmac_ide_probe();
2112         }
2113 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
2114 #ifdef CONFIG_BLK_DEV_IDE_SIBYTE
2115         {
2116                 extern void sibyte_ide_probe(void);
2117                 sibyte_ide_probe();
2118         }
2119 #endif /* CONFIG_BLK_DEV_IDE_SIBYTE */
2120 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
2121         {
2122                 extern void icside_init(void);
2123                 icside_init();
2124         }
2125 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
2126 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
2127         {
2128                 extern void rapide_init(void);
2129                 rapide_init();
2130         }
2131 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
2132 #ifdef CONFIG_BLK_DEV_GAYLE
2133         {
2134                 extern void gayle_init(void);
2135                 gayle_init();
2136         }
2137 #endif /* CONFIG_BLK_DEV_GAYLE */
2138 #ifdef CONFIG_BLK_DEV_FALCON_IDE
2139         {
2140                 extern void falconide_init(void);
2141                 falconide_init();
2142         }
2143 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
2144 #ifdef CONFIG_BLK_DEV_MAC_IDE
2145         {
2146                 extern void macide_init(void);
2147                 macide_init();
2148         }
2149 #endif /* CONFIG_BLK_DEV_MAC_IDE */
2150 #ifdef CONFIG_BLK_DEV_CPCI405_IDE
2151         {
2152                 extern void cpci405ide_init(void);
2153                 cpci405ide_init();
2154         }
2155 #endif /* CONFIG_BLK_DEV_CPCI405_IDE */
2156 #ifdef CONFIG_BLK_DEV_Q40IDE
2157         {
2158                 extern void q40ide_init(void);
2159                 q40ide_init();
2160         }
2161 #endif /* CONFIG_BLK_DEV_Q40IDE */
2162 #ifdef CONFIG_BLK_DEV_BUDDHA
2163         {
2164                 extern void buddha_init(void);
2165                 buddha_init();
2166         }
2167 #endif /* CONFIG_BLK_DEV_BUDDHA */
2168 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP)
2169         {
2170                 extern void pnpide_init(int enable);
2171                 pnpide_init(1);
2172         }
2173 #endif /* CONFIG_BLK_DEV_ISAPNP */
2174 }
2175
2176 void __init ide_init_builtin_subdrivers (void)
2177 {
2178 #ifdef CONFIG_BLK_DEV_IDEDISK
2179         (void) idedisk_init();
2180 #endif /* CONFIG_BLK_DEV_IDEDISK */
2181 #ifdef CONFIG_BLK_DEV_IDECD
2182         (void) ide_cdrom_init();
2183 #endif /* CONFIG_BLK_DEV_IDECD */
2184 #ifdef CONFIG_BLK_DEV_IDETAPE
2185         (void) idetape_init();
2186 #endif /* CONFIG_BLK_DEV_IDETAPE */
2187 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2188         (void) idefloppy_init();
2189 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2190 #ifdef CONFIG_BLK_DEV_IDESCSI
2191  #ifdef CONFIG_SCSI
2192         (void) idescsi_init();
2193  #else
2194     #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
2195  #endif
2196 #endif /* CONFIG_BLK_DEV_IDESCSI */
2197 }
2198
2199 #ifndef CLASSIC_BUILTINS_METHOD
2200 #  ifndef DIRECT_HWIF_PROBE_ATTACH_METHOD
2201 #    ifdef FAKE_CLASSIC_ATTACH_METHOD
2202 /*
2203  * Attempt to match drivers for the available drives
2204  */
2205 void ide_attach_devices (void)
2206 {
2207         ide_hwif_t *hwif;
2208         ide_drive_t *drive;
2209         int i, unit;
2210
2211         for (i = 0; i < MAX_HWIFS; i++) {
2212                 hwif = &ide_hwifs[i];
2213                 if (!hwif->present)
2214                         continue;
2215                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2216                         drive = &hwif->drives[unit];
2217                         if (drive->present && !drive->dead)
2218                                 ide_attach_drive(drive);
2219                 }
2220         }
2221 }
2222 #    endif /* FAKE_CLASSIC_ATTACH_METHOD */
2223 #  endif /* DIRECT_HWIF_PROBE_ATTACH_METHOD */
2224 #endif /* CLASSIC_BUILTINS_METHOD */
2225
2226 void __init ide_init_builtin_drivers (void)
2227 {
2228         /*
2229          * Probe for special PCI and other "known" interface chipsets
2230          */
2231         probe_for_hwifs ();
2232
2233 #ifdef CONFIG_BLK_DEV_IDE
2234         if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2235                 ide_get_lock(&ide_intr_lock, NULL, NULL); /* for atari only */
2236
2237         (void) ideprobe_init();
2238
2239         if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2240                 ide_release_lock(&ide_intr_lock);       /* for atari only */
2241 #endif /* CONFIG_BLK_DEV_IDE */
2242
2243 #ifdef CONFIG_PROC_FS
2244         proc_ide_create();
2245 #endif
2246
2247 #ifdef CLASSIC_BUILTINS_METHOD
2248         ide_init_builtin_subdrivers();
2249 #else /* ! CLASSIC_BUILTINS_METHOD */
2250 #  ifndef DIRECT_HWIF_PROBE_ATTACH_METHOD
2251 #    ifdef FAKE_CLASSIC_ATTACH_METHOD
2252         ide_attach_devices();
2253 #    endif /* FAKE_CLASSIC_ATTACH_METHOD */
2254 #  endif /* DIRECT_HWIF_PROBE_ATTACH_METHOD */
2255 #endif /* CLASSIC_BUILTINS_METHOD */
2256 }
2257
2258 /*
2259  *      Actually unregister the subdriver. Called with the
2260  *      request lock dropped.
2261  */
2262  
2263 static int default_cleanup (ide_drive_t *drive)
2264 {
2265         return ide_unregister_subdriver(drive);
2266 }
2267
2268 /*
2269  *      Check if we can unregister the subdriver. Called with the
2270  *      request lock held.
2271  */
2272  
2273 static int default_shutdown(ide_drive_t *drive)
2274 {
2275         if (drive->usage || drive->busy ||
2276             drive->driver == NULL || DRIVER(drive)->busy) {
2277                 return 1;
2278         }
2279         drive->dead = 1;
2280         return 0;
2281 }
2282
2283 static int default_standby (ide_drive_t *drive)
2284 {
2285         return 0;
2286 }
2287
2288 static int default_suspend (ide_drive_t *drive)
2289 {
2290         return 0;
2291 }
2292
2293 static int default_resume (ide_drive_t *drive)
2294 {
2295         return 0;
2296 }
2297
2298 static int default_flushcache (ide_drive_t *drive)
2299 {
2300         return 0;
2301 }
2302
2303 static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, unsigned long block)
2304 {
2305         ide_end_request(drive, 0);
2306         return ide_stopped;
2307 }
2308
2309 static int default_end_request (ide_drive_t *drive, int uptodate)
2310 {
2311         return ide_end_request(drive, uptodate);
2312 }
2313
2314 static u8 default_sense (ide_drive_t *drive, const char *msg, u8 stat)
2315 {
2316         return ide_dump_status(drive, msg, stat);
2317 }
2318
2319 static ide_startstop_t default_error (ide_drive_t *drive, const char *msg, u8 stat)
2320 {
2321         return ide_error(drive, msg, stat);
2322 }
2323
2324 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
2325                           unsigned int cmd, unsigned long arg)
2326 {
2327         return -EIO;
2328 }
2329
2330 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
2331 {
2332         drive->usage--;
2333         return -EIO;
2334 }
2335
2336 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
2337 {
2338 }
2339
2340 static int default_check_media_change (ide_drive_t *drive)
2341 {
2342         return 1;
2343 }
2344
2345 static void default_pre_reset (ide_drive_t *drive)
2346 {
2347 }
2348
2349 static unsigned long default_capacity (ide_drive_t *drive)
2350 {
2351         return 0x7fffffff;
2352 }
2353
2354 static ide_startstop_t default_special (ide_drive_t *drive)
2355 {
2356         special_t *s = &drive->special;
2357
2358         s->all = 0;
2359         drive->mult_req = 0;
2360         return ide_stopped;
2361 }
2362
2363 static int default_init (void)
2364 {
2365         return 0;
2366 }
2367
2368 static int default_attach (ide_drive_t *drive)
2369 {
2370         printk(KERN_ERR "%s: does not support hotswap of device class !\n",
2371                 drive->name);
2372
2373         return 0;
2374 }
2375
2376 static void setup_driver_defaults (ide_drive_t *drive)
2377 {
2378         ide_driver_t *d = drive->driver;
2379
2380         if (d->cleanup == NULL)         d->cleanup = default_cleanup;
2381         if (d->shutdown == NULL)        d->shutdown = default_shutdown;
2382         if (d->standby == NULL)         d->standby = default_standby;
2383         if (d->suspend == NULL)         d->suspend = default_suspend;
2384         if (d->resume == NULL)          d->resume = default_resume;
2385         if (d->flushcache == NULL)      d->flushcache = default_flushcache;
2386         if (d->do_request == NULL)      d->do_request = default_do_request;
2387         if (d->end_request == NULL)     d->end_request = default_end_request;
2388         if (d->sense == NULL)           d->sense = default_sense;
2389         if (d->error == NULL)           d->error = default_error;
2390         if (d->ioctl == NULL)           d->ioctl = default_ioctl;
2391         if (d->open == NULL)            d->open = default_open;
2392         if (d->release == NULL)         d->release = default_release;
2393         if (d->media_change == NULL)    d->media_change = default_check_media_change;
2394         if (d->pre_reset == NULL)       d->pre_reset = default_pre_reset;
2395         if (d->capacity == NULL)        d->capacity = default_capacity;
2396         if (d->special == NULL)         d->special = default_special;
2397         if (d->init == NULL)            d->init = default_init;
2398         if (d->attach == NULL)          d->attach = default_attach;
2399 }
2400
2401 ide_drive_t *ide_scan_devices (u8 media, const char *name, ide_driver_t *driver, int n)
2402 {
2403         unsigned int unit, index, i;
2404
2405         for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
2406                 ide_hwif_t *hwif = &ide_hwifs[index];
2407                 if (!hwif->present)
2408                         continue;
2409                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2410                         ide_drive_t *drive = &hwif->drives[unit];
2411                         char *req = drive->driver_req;
2412                         if (*req && !strstr(name, req))
2413                                 continue;
2414                         if (drive->present && drive->media == media &&
2415                             drive->driver == driver && ++i > n)
2416                                 return drive;
2417                 }
2418         }
2419         return NULL;
2420 }
2421
2422 EXPORT_SYMBOL(ide_scan_devices);
2423
2424 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
2425 {
2426         unsigned long flags;
2427         
2428         spin_lock_irqsave(&io_request_lock, flags);
2429         if (version != IDE_SUBDRIVER_VERSION || !drive->present ||
2430             drive->driver != NULL || drive->busy || drive->usage) {
2431                 spin_unlock_irqrestore(&io_request_lock, flags);
2432                 return 1;
2433         }
2434         drive->driver = driver;
2435         setup_driver_defaults(drive);
2436         spin_unlock_irqrestore(&io_request_lock, flags);
2437         if (drive->autotune != 2) {
2438                 /* DMA timings and setup moved to ide-probe.c */
2439                 if (!driver->supports_dma && HWIF(drive)->ide_dma_off_quietly)
2440 //                      HWIF(drive)->ide_dma_off_quietly(drive);
2441                         HWIF(drive)->ide_dma_off(drive);
2442                 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2443                 drive->nice1 = 1;
2444         }
2445         drive->revalidate = 1;
2446         drive->suspend_reset = 0;
2447 #ifdef CONFIG_PROC_FS
2448         ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2449         ide_add_proc_entries(drive->proc, driver->proc, drive);
2450 #endif
2451         return 0;
2452 }
2453
2454 EXPORT_SYMBOL(ide_register_subdriver);
2455
2456 int ide_unregister_subdriver (ide_drive_t *drive)
2457 {
2458         unsigned long flags;
2459         
2460         spin_lock_irqsave(&io_request_lock, flags);
2461         if (drive->usage || drive->busy ||
2462             drive->driver == NULL || DRIVER(drive)->busy) {
2463                 spin_unlock_irqrestore(&io_request_lock, flags);
2464                 return 1;
2465         }
2466 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP) && defined(MODULE)
2467         pnpide_init(0);
2468 #endif /* CONFIG_BLK_DEV_ISAPNP */
2469 #ifdef CONFIG_PROC_FS
2470         ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2471         ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2472 #endif
2473         auto_remove_settings(drive);
2474         drive->driver = NULL;
2475         spin_unlock_irqrestore(&io_request_lock, flags);
2476         return 0;
2477 }
2478
2479 EXPORT_SYMBOL(ide_unregister_subdriver);
2480
2481 int ide_register_module (ide_module_t *module)
2482 {
2483         ide_module_t *p = ide_modules;
2484
2485         while (p) {
2486                 if (p == module)
2487                         return 1;
2488                 p = p->next;
2489         }
2490         module->next = ide_modules;
2491         ide_modules = module;
2492         revalidate_drives(1);
2493         return 0;
2494 }
2495
2496 EXPORT_SYMBOL(ide_register_module);
2497
2498 void ide_unregister_module (ide_module_t *module)
2499 {
2500         ide_module_t **p;
2501
2502         for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
2503         if (*p)
2504                 *p = (*p)->next;
2505 }
2506
2507 EXPORT_SYMBOL(ide_unregister_module);
2508
2509 struct block_device_operations ide_fops[] = {{
2510         owner:                  THIS_MODULE,
2511         open:                   ide_open,
2512         release:                ide_release,
2513         ioctl:                  ide_ioctl,
2514         check_media_change:     ide_check_media_change,
2515         revalidate:             ide_revalidate_disk
2516 }};
2517
2518 EXPORT_SYMBOL(ide_fops);
2519
2520 /*
2521  * ide_geninit() is called exactly *once* for each interface.
2522  */
2523 void ide_geninit (ide_hwif_t *hwif)
2524 {
2525         unsigned int unit;
2526         struct gendisk *gd = hwif->gd;
2527
2528         for (unit = 0; unit < MAX_DRIVES; ++unit) {
2529                 ide_drive_t *drive = &hwif->drives[unit];
2530
2531                 if (!drive->present)
2532                         continue;
2533                 if (drive->media!=ide_disk && drive->media!=ide_floppy)
2534                         continue;
2535                 register_disk(gd,MKDEV(hwif->major,unit<<PARTN_BITS),
2536 #ifdef CONFIG_BLK_DEV_ISAPNP
2537                         (drive->forced_geom && drive->noprobe) ? 1 :
2538 #endif /* CONFIG_BLK_DEV_ISAPNP */
2539                         1<<PARTN_BITS, ide_fops,
2540                         current_capacity(drive));
2541         }
2542 }
2543
2544 EXPORT_SYMBOL(ide_geninit);
2545
2546
2547 /*
2548  * Probe module
2549  */
2550 devfs_handle_t ide_devfs_handle;
2551
2552 EXPORT_SYMBOL(ide_probe);
2553 EXPORT_SYMBOL(ide_devfs_handle);
2554
2555 static int ide_notify_reboot (struct notifier_block *this, unsigned long event, void *x)
2556 {
2557         ide_hwif_t *hwif;
2558         ide_drive_t *drive;
2559         int i, unit;
2560
2561         switch (event) {
2562                 case SYS_HALT:
2563                 case SYS_POWER_OFF:
2564                 case SYS_RESTART:
2565                         break;
2566                 default:
2567                         return NOTIFY_DONE;
2568         }
2569
2570         printk(KERN_INFO "flushing ide devices: ");
2571
2572         for (i = 0; i < MAX_HWIFS; i++) {
2573                 hwif = &ide_hwifs[i];
2574                 if (!hwif->present)
2575                         continue;
2576                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2577                         drive = &hwif->drives[unit];
2578                         if (!drive->present)
2579                                 continue;
2580
2581                         /* set the drive to standby */
2582                         printk("%s ", drive->name);
2583                         if (event != SYS_RESTART)
2584                                 if (drive->driver != NULL && DRIVER(drive)->standby(drive))
2585                                 continue;
2586
2587                         if (drive->driver != NULL)
2588                         {
2589                                 DRIVER(drive)->cleanup(drive);
2590                                 continue;
2591                         }
2592                 }
2593         }
2594         printk("\n");
2595         return NOTIFY_DONE;
2596 }
2597
2598 static struct notifier_block ide_notifier = {
2599         ide_notify_reboot,
2600         NULL,
2601         5
2602 };
2603
2604 /*
2605  * This is gets invoked once during initialization, to set *everything* up
2606  */
2607 int __init ide_init (void)
2608 {
2609         static char banner_printed;
2610         int i;
2611
2612         if (!banner_printed) {
2613                 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
2614                 ide_devfs_handle = devfs_mk_dir(NULL, "ide", NULL);
2615                 system_bus_speed = ide_system_bus_speed();
2616                 banner_printed = 1;
2617         }
2618
2619         init_ide_data();
2620
2621 #ifndef CLASSIC_BUILTINS_METHOD
2622         ide_init_builtin_subdrivers();
2623 #endif /* CLASSIC_BUILTINS_METHOD */
2624
2625         initializing = 1;
2626         ide_init_builtin_drivers();
2627         initializing = 0;
2628
2629         for (i = 0; i < MAX_HWIFS; ++i) {
2630                 ide_hwif_t  *hwif = &ide_hwifs[i];
2631                 if (hwif->present)
2632                         ide_geninit(hwif);
2633         }
2634
2635         register_reboot_notifier(&ide_notifier);
2636         return 0;
2637 }
2638
2639 #ifdef MODULE
2640 char *options = NULL;
2641 MODULE_PARM(options,"s");
2642 MODULE_LICENSE("GPL");
2643
2644 static void __init parse_options (char *line)
2645 {
2646         char *next = line;
2647
2648         if (line == NULL || !*line)
2649                 return;
2650         while ((line = next) != NULL) {
2651                 if ((next = strchr(line,' ')) != NULL)
2652                         *next++ = 0;
2653                 if (!ide_setup(line))
2654                         printk ("Unknown option '%s'\n", line);
2655         }
2656 }
2657
2658 int init_module (void)
2659 {
2660         parse_options(options);
2661         return ide_init();
2662 }
2663
2664 void cleanup_module (void)
2665 {
2666         int index;
2667
2668         unregister_reboot_notifier(&ide_notifier);
2669         for (index = 0; index < MAX_HWIFS; ++index) {
2670                 ide_unregister(index);
2671 #if !defined(CONFIG_DMA_NONPCI)
2672                 if (ide_hwifs[index].dma_base)
2673                         (void) ide_release_dma(&ide_hwifs[index]);
2674 #endif /* !(CONFIG_DMA_NONPCI) */
2675         }
2676
2677 #ifdef CONFIG_PROC_FS
2678         proc_ide_destroy();
2679 #endif
2680         devfs_unregister (ide_devfs_handle);
2681 }
2682
2683 #else /* !MODULE */
2684
2685 __setup("", ide_setup);
2686 module_init(ide_init);
2687
2688 #endif /* MODULE */