kernel.org linux-2.6.10
[linux-2.6.git] / drivers / ide / ide.c
1 /*
2  *  linux/drivers/ide/ide.c             Version 7.00beta2       Mar 05 2003
3  *
4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5  */
6
7 /*
8  *  Mostly written by Mark Lord  <mlord@pobox.com>
9  *                and Gadi Oxman <gadio@netvision.net.il>
10  *                and Andre Hedrick <andre@linux-ide.org>
11  *
12  *  See linux/MAINTAINERS for address of current maintainer.
13  *
14  * This is the multiple IDE interface driver, as evolved from hd.c.
15  * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
16  *   (usually 14 & 15).
17  * There can be up to two drives per interface, as per the ATA-2 spec.
18  *
19  * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
20  * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
21  * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
22  * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
23  * ...
24  *
25  *  From hd.c:
26  *  |
27  *  | It traverses the request-list, using interrupts to jump between functions.
28  *  | As nearly all functions can be called within interrupts, we may not sleep.
29  *  | Special care is recommended.  Have Fun!
30  *  |
31  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
32  *  |
33  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
34  *  | in the early extended-partition checks and added DM partitions.
35  *  |
36  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
37  *  |
38  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
39  *  | and general streamlining by Mark Lord (mlord@pobox.com).
40  *
41  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
42  *
43  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
44  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
45  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
46  *
47  *  This was a rewrite of just about everything from hd.c, though some original
48  *  code is still sprinkled about.  Think of it as a major evolution, with
49  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
50  *
51  *  Version 1.0 ALPHA   initial code, primary i/f working okay
52  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
53  *  Version 1.4 BETA    added auto probing for irq(s)
54  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
55  *  ...
56  * Version 5.50         allow values as small as 20 for idebus=
57  * Version 5.51         force non io_32bit in drive_cmd_intr()
58  *                      change delay_10ms() to delay_50ms() to fix problems
59  * Version 5.52         fix incorrect invalidation of removable devices
60  *                      add "hdx=slow" command line option
61  * Version 5.60         start to modularize the driver; the disk and ATAPI
62  *                       drivers can be compiled as loadable modules.
63  *                      move IDE probe code to ide-probe.c
64  *                      move IDE disk code to ide-disk.c
65  *                      add support for generic IDE device subdrivers
66  *                      add m68k code from Geert Uytterhoeven
67  *                      probe all interfaces by default
68  *                      add ioctl to (re)probe an interface
69  * Version 6.00         use per device request queues
70  *                      attempt to optimize shared hwgroup performance
71  *                      add ioctl to manually adjust bandwidth algorithms
72  *                      add kerneld support for the probe module
73  *                      fix bug in ide_error()
74  *                      fix bug in the first ide_get_lock() call for Atari
75  *                      don't flush leftover data for ATAPI devices
76  * Version 6.01         clear hwgroup->active while the hwgroup sleeps
77  *                      support HDIO_GETGEO for floppies
78  * Version 6.02         fix ide_ack_intr() call
79  *                      check partition table on floppies
80  * Version 6.03         handle bad status bit sequencing in ide_wait_stat()
81  * Version 6.10         deleted old entries from this list of updates
82  *                      replaced triton.c with ide-dma.c generic PCI DMA
83  *                      added support for BIOS-enabled UltraDMA
84  *                      rename all "promise" things to "pdc4030"
85  *                      fix EZ-DRIVE handling on small disks
86  * Version 6.11         fix probe error in ide_scan_devices()
87  *                      fix ancient "jiffies" polling bugs
88  *                      mask all hwgroup interrupts on each irq entry
89  * Version 6.12         integrate ioctl and proc interfaces
90  *                      fix parsing of "idex=" command line parameter
91  * Version 6.13         add support for ide4/ide5 courtesy rjones@orchestream.com
92  * Version 6.14         fixed IRQ sharing among PCI devices
93  * Version 6.15         added SMP awareness to IDE drivers
94  * Version 6.16         fixed various bugs; even more SMP friendly
95  * Version 6.17         fix for newest EZ-Drive problem
96  * Version 6.18         default unpartitioned-disk translation now "BIOS LBA"
97  * Version 6.19         Re-design for a UNIFORM driver for all platforms,
98  *                        model based on suggestions from Russell King and
99  *                        Geert Uytterhoeven
100  *                      Promise DC4030VL now supported.
101  *                      add support for ide6/ide7
102  *                      delay_50ms() changed to ide_delay_50ms() and exported.
103  * Version 6.20         Added/Fixed Generic ATA-66 support and hwif detection.
104  *                      Added hdx=flash to allow for second flash disk
105  *                        detection w/o the hang loop.
106  *                      Added support for ide8/ide9
107  *                      Added idex=ata66 for the quirky chipsets that are
108  *                        ATA-66 compliant, but have yet to determine a method
109  *                        of verification of the 80c cable presence.
110  *                        Specifically Promise's PDC20262 chipset.
111  * Version 6.21         Fixing/Fixed SMP spinlock issue with insight from an old
112  *                        hat that clarified original low level driver design.
113  * Version 6.30         Added SMP support; fixed multmode issues.  -ml
114  * Version 6.31         Debug Share INTR's and request queue streaming
115  *                      Native ATA-100 support
116  *                      Prep for Cascades Project
117  * Version 7.00alpha    First named revision of ide rearrange
118  *
119  *  Some additional driver compile-time options are in ./include/linux/ide.h
120  *
121  *  To do, in likely order of completion:
122  *      - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
123  *
124  */
125
126 #define REVISION        "Revision: 7.00alpha2"
127 #define VERSION         "Id: ide.c 7.00a2 20020906"
128
129 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
130
131 #define _IDE_C                  /* Tell ide.h it's really us */
132
133 #include <linux/config.h>
134 #include <linux/module.h>
135 #include <linux/types.h>
136 #include <linux/string.h>
137 #include <linux/kernel.h>
138 #include <linux/timer.h>
139 #include <linux/mm.h>
140 #include <linux/interrupt.h>
141 #include <linux/major.h>
142 #include <linux/errno.h>
143 #include <linux/genhd.h>
144 #include <linux/blkpg.h>
145 #include <linux/slab.h>
146 #include <linux/init.h>
147 #include <linux/pci.h>
148 #include <linux/delay.h>
149 #include <linux/ide.h>
150 #include <linux/devfs_fs_kernel.h>
151 #include <linux/completion.h>
152 #include <linux/reboot.h>
153 #include <linux/cdrom.h>
154 #include <linux/seq_file.h>
155 #include <linux/device.h>
156 #include <linux/bitops.h>
157
158 #include <asm/byteorder.h>
159 #include <asm/irq.h>
160 #include <asm/uaccess.h>
161 #include <asm/io.h>
162
163
164 /* default maximum number of failures */
165 #define IDE_DEFAULT_MAX_FAILURES        1
166
167 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
168                                         IDE2_MAJOR, IDE3_MAJOR,
169                                         IDE4_MAJOR, IDE5_MAJOR,
170                                         IDE6_MAJOR, IDE7_MAJOR,
171                                         IDE8_MAJOR, IDE9_MAJOR };
172
173 static int idebus_parameter;    /* holds the "idebus=" parameter */
174 static int system_bus_speed;    /* holds what we think is VESA/PCI bus speed */
175 static int initializing;        /* set while initializing built-in drivers */
176
177 DECLARE_MUTEX(ide_cfg_sem);
178 spinlock_t ide_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
179
180 #ifdef CONFIG_BLK_DEV_IDEPCI
181 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
182 #endif
183
184 #ifdef CONFIG_IDEDMA_AUTO
185 int noautodma = 0;
186 #else
187 int noautodma = 1;
188 #endif
189
190 EXPORT_SYMBOL(noautodma);
191
192 /*
193  * This is declared extern in ide.h, for access by other IDE modules:
194  */
195 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
196
197 EXPORT_SYMBOL(ide_hwifs);
198
199 extern ide_driver_t idedefault_driver;
200 static void setup_driver_defaults(ide_driver_t *driver);
201
202 /*
203  * Do not even *think* about calling this!
204  */
205 static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
206 {
207         unsigned int unit;
208
209         /* bulk initialize hwif & drive info with zeros */
210         memset(hwif, 0, sizeof(ide_hwif_t));
211
212         /* fill in any non-zero initial values */
213         hwif->index     = index;
214         hwif->major     = ide_hwif_to_major[index];
215
216         hwif->name[0]   = 'i';
217         hwif->name[1]   = 'd';
218         hwif->name[2]   = 'e';
219         hwif->name[3]   = '0' + index;
220
221         hwif->bus_state = BUSSTATE_ON;
222
223         hwif->atapi_dma = 0;            /* disable all atapi dma */ 
224         hwif->ultra_mask = 0x80;        /* disable all ultra */
225         hwif->mwdma_mask = 0x80;        /* disable all mwdma */
226         hwif->swdma_mask = 0x80;        /* disable all swdma */
227
228         sema_init(&hwif->gendev_rel_sem, 0);
229
230         default_hwif_iops(hwif);
231         default_hwif_transport(hwif);
232         for (unit = 0; unit < MAX_DRIVES; ++unit) {
233                 ide_drive_t *drive = &hwif->drives[unit];
234
235                 drive->media                    = ide_disk;
236                 drive->select.all               = (unit<<4)|0xa0;
237                 drive->hwif                     = hwif;
238                 drive->ctl                      = 0x08;
239                 drive->ready_stat               = READY_STAT;
240                 drive->bad_wstat                = BAD_W_STAT;
241                 drive->special.b.recalibrate    = 1;
242                 drive->special.b.set_geometry   = 1;
243                 drive->name[0]                  = 'h';
244                 drive->name[1]                  = 'd';
245                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
246                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
247                 drive->using_dma                = 0;
248                 drive->is_flash                 = 0;
249                 drive->driver                   = &idedefault_driver;
250                 drive->vdma                     = 0;
251                 INIT_LIST_HEAD(&drive->list);
252                 sema_init(&drive->gendev_rel_sem, 0);
253         }
254 }
255
256 static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
257 {
258         hw_regs_t hw;
259
260         memset(&hw, 0, sizeof(hw_regs_t));
261
262         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
263
264         memcpy(&hwif->hw, &hw, sizeof(hw));
265         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
266
267         hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
268 #ifdef CONFIG_BLK_DEV_HD
269         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
270                 hwif->noprobe = 1;      /* may be overridden by ide_setup() */
271 #endif
272 }
273
274 extern void ide_arm_init(void);
275
276 /*
277  * init_ide_data() sets reasonable default values into all fields
278  * of all instances of the hwifs and drives, but only on the first call.
279  * Subsequent calls have no effect (they don't wipe out anything).
280  *
281  * This routine is normally called at driver initialization time,
282  * but may also be called MUCH earlier during kernel "command-line"
283  * parameter processing.  As such, we cannot depend on any other parts
284  * of the kernel (such as memory allocation) to be functioning yet.
285  *
286  * This is too bad, as otherwise we could dynamically allocate the
287  * ide_drive_t structs as needed, rather than always consuming memory
288  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
289  *
290  * FIXME: We should stuff the setup data into __init and copy the
291  * relevant hwifs/allocate them properly during boot.
292  */
293 #define MAGIC_COOKIE 0x12345678
294 static void __init init_ide_data (void)
295 {
296         ide_hwif_t *hwif;
297         unsigned int index;
298         static unsigned long magic_cookie = MAGIC_COOKIE;
299
300         if (magic_cookie != MAGIC_COOKIE)
301                 return;         /* already initialized */
302         magic_cookie = 0;
303
304         setup_driver_defaults(&idedefault_driver);
305
306         /* Initialise all interface structures */
307         for (index = 0; index < MAX_HWIFS; ++index) {
308                 hwif = &ide_hwifs[index];
309                 init_hwif_data(hwif, index);
310                 init_hwif_default(hwif, index);
311 #if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
312                 hwif->irq = hwif->hw.irq =
313                         ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
314 #endif
315         }
316 #ifdef CONFIG_IDE_ARM
317         initializing = 1;
318         ide_arm_init();
319         initializing = 0;
320 #endif
321 }
322
323 /**
324  *      ide_system_bus_speed    -       guess bus speed
325  *
326  *      ide_system_bus_speed() returns what we think is the system VESA/PCI
327  *      bus speed (in MHz). This is used for calculating interface PIO timings.
328  *      The default is 40 for known PCI systems, 50 otherwise.
329  *      The "idebus=xx" parameter can be used to override this value.
330  *      The actual value to be used is computed/displayed the first time
331  *      through. Drivers should only use this as a last resort.
332  *
333  *      Returns a guessed speed in MHz.
334  */
335
336 int ide_system_bus_speed (void)
337 {
338         static struct pci_device_id pci_default[] = {
339                 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
340                 { }
341         };
342
343         if (!system_bus_speed) {
344                 if (idebus_parameter) {
345                         /* user supplied value */
346                         system_bus_speed = idebus_parameter;
347                 } else if (pci_dev_present(pci_default)) {
348                         /* safe default value for PCI */
349                         system_bus_speed = 33;
350                 } else {
351                         /* safe default value for VESA and PCI */
352                         system_bus_speed = 50;
353                 }
354                 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
355                         "for PIO modes%s\n", system_bus_speed,
356                         idebus_parameter ? "" : "; override with idebus=xx");
357         }
358         return system_bus_speed;
359 }
360
361 /**
362  *      current_capacity        -       drive capacity
363  *      @drive: drive to query
364  *
365  *      Return the current capacity (in sectors) of a drive according to
366  *      its current geometry/LBA settings. Empty removables are reported
367  *      as size zero.
368  */
369
370 sector_t current_capacity (ide_drive_t *drive)
371 {
372         if (!drive->present)
373                 return 0;
374         return DRIVER(drive)->capacity(drive);
375 }
376
377 EXPORT_SYMBOL(current_capacity);
378
379 /**
380  *      ide_dump_status         -       translate ATA error
381  *      @drive: drive the error occured on
382  *      @msg: information string
383  *      @stat: status byte
384  *
385  *      Error reporting, in human readable form (luxurious, but a memory hog).
386  *      Combines the drive name, message and status byte to provide a
387  *      user understandable explanation of the device error.
388  */
389
390 u8 ide_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
391 {
392         ide_hwif_t *hwif = HWIF(drive);
393         unsigned long flags;
394         u8 err = 0;
395
396         local_irq_set(flags);
397         printk(KERN_WARNING "%s: %s: status=0x%02x", drive->name, msg, stat);
398         printk(" { ");
399         if (stat & BUSY_STAT) {
400                 printk("Busy ");
401         } else {
402                 if (stat & READY_STAT)  printk("DriveReady ");
403                 if (stat & WRERR_STAT)  printk("DeviceFault ");
404                 if (stat & SEEK_STAT)   printk("SeekComplete ");
405                 if (stat & DRQ_STAT)    printk("DataRequest ");
406                 if (stat & ECC_STAT)    printk("CorrectedError ");
407                 if (stat & INDEX_STAT)  printk("Index ");
408                 if (stat & ERR_STAT)    printk("Error ");
409         }
410         printk("}");
411         printk("\n");
412         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
413                 err = hwif->INB(IDE_ERROR_REG);
414                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
415                 if (drive->media == ide_disk) {
416                         printk(" { ");
417                         if (err & ABRT_ERR)     printk("DriveStatusError ");
418                         if (err & ICRC_ERR)     printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
419                         if (err & ECC_ERR)      printk("UncorrectableError ");
420                         if (err & ID_ERR)       printk("SectorIdNotFound ");
421                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
422                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
423                         printk("}");
424                         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
425                                 if ((drive->id->command_set_2 & 0x0400) &&
426                                     (drive->id->cfs_enable_2 & 0x0400) &&
427                                     (drive->addressing == 1)) {
428                                         u64 sectors = 0;
429                                         u32 high = 0;
430                                         u32 low = ide_read_24(drive);
431                                         hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
432                                         high = ide_read_24(drive);
433
434                                         sectors = ((u64)high << 24) | low;
435                                         printk(", LBAsect=%llu, high=%d, low=%d",
436                                                (long long) sectors,
437                                                high, low);
438                                 } else {
439                                         u8 cur = hwif->INB(IDE_SELECT_REG);
440                                         if (cur & 0x40) {       /* using LBA? */
441                                                 printk(", LBAsect=%ld", (unsigned long)
442                                                  ((cur&0xf)<<24)
443                                                  |(hwif->INB(IDE_HCYL_REG)<<16)
444                                                  |(hwif->INB(IDE_LCYL_REG)<<8)
445                                                  | hwif->INB(IDE_SECTOR_REG));
446                                         } else {
447                                                 printk(", CHS=%d/%d/%d",
448                                                  (hwif->INB(IDE_HCYL_REG)<<8) +
449                                                   hwif->INB(IDE_LCYL_REG),
450                                                   cur & 0xf,
451                                                   hwif->INB(IDE_SECTOR_REG));
452                                         }
453                                 }
454                                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
455                                         printk(", sector=%llu", (unsigned long long)HWGROUP(drive)->rq->sector);
456                         }
457                 }
458                 printk("\n");
459         }
460         {
461                 struct request *rq;
462                 int opcode = 0x100;
463
464                 spin_lock(&ide_lock);
465                 rq = NULL;
466                 if (HWGROUP(drive))
467                         rq = HWGROUP(drive)->rq;
468                 spin_unlock(&ide_lock);
469                 if (!rq)
470                         goto out;
471                 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
472                         char *args = rq->buffer;
473                         if (args)
474                                 opcode = args[0];
475                 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
476                         ide_task_t *args = rq->special;
477                         if (args) {
478                                 task_struct_t *tf = (task_struct_t *) args->tfRegister;
479                                 opcode = tf->command;
480                         }
481                 }
482
483                 printk("ide: failed opcode was %x\n", opcode);
484         }
485 out:
486         local_irq_restore(flags);
487         return err;
488 }
489
490 EXPORT_SYMBOL(ide_dump_status);
491
492 static int ide_open (struct inode * inode, struct file * filp)
493 {
494         return -ENXIO;
495 }
496
497 /*
498  *      drives_lock protects the list of drives, drivers_lock the
499  *      list of drivers.  Currently nobody takes both at once.
500  */
501
502 static spinlock_t drives_lock = SPIN_LOCK_UNLOCKED;
503 static spinlock_t drivers_lock = SPIN_LOCK_UNLOCKED;
504 static LIST_HEAD(drivers);
505
506 /* Iterator for the driver list. */
507
508 static void *m_start(struct seq_file *m, loff_t *pos)
509 {
510         struct list_head *p;
511         loff_t l = *pos;
512         spin_lock(&drivers_lock);
513         list_for_each(p, &drivers)
514                 if (!l--)
515                         return list_entry(p, ide_driver_t, drivers);
516         return NULL;
517 }
518
519 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
520 {
521         struct list_head *p = ((ide_driver_t *)v)->drivers.next;
522         (*pos)++;
523         return p==&drivers ? NULL : list_entry(p, ide_driver_t, drivers);
524 }
525
526 static void m_stop(struct seq_file *m, void *v)
527 {
528         spin_unlock(&drivers_lock);
529 }
530
531 static int show_driver(struct seq_file *m, void *v)
532 {
533         ide_driver_t *driver = v;
534         seq_printf(m, "%s version %s\n", driver->name, driver->version);
535         return 0;
536 }
537
538 struct seq_operations ide_drivers_op = {
539         .start  = m_start,
540         .next   = m_next,
541         .stop   = m_stop,
542         .show   = show_driver
543 };
544
545 #ifdef CONFIG_PROC_FS
546 struct proc_dir_entry *proc_ide_root;
547
548 ide_proc_entry_t generic_subdriver_entries[] = {
549         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
550         { NULL, 0, NULL, NULL }
551 };
552 #endif
553
554 static struct resource* hwif_request_region(ide_hwif_t *hwif,
555                                             unsigned long addr, int num)
556 {
557         struct resource *res = request_region(addr, num, hwif->name);
558
559         if (!res)
560                 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
561                                 hwif->name, addr, addr+num-1);
562         return res;
563 }
564
565 /**
566  *      ide_hwif_request_regions - request resources for IDE
567  *      @hwif: interface to use
568  *
569  *      Requests all the needed resources for an interface.
570  *      Right now core IDE code does this work which is deeply wrong.
571  *      MMIO leaves it to the controller driver,
572  *      PIO will migrate this way over time.
573  */
574
575 int ide_hwif_request_regions(ide_hwif_t *hwif)
576 {
577         unsigned long addr;
578         unsigned int i;
579
580         if (hwif->mmio == 2)
581                 return 0;
582         BUG_ON(hwif->mmio == 1);
583         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
584         if (addr && !hwif_request_region(hwif, addr, 1))
585                 goto control_region_busy;
586         hwif->straight8 = 0;
587         addr = hwif->io_ports[IDE_DATA_OFFSET];
588         if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
589                 if (!hwif_request_region(hwif, addr, 8))
590                         goto data_region_busy;
591                 hwif->straight8 = 1;
592                 return 0;
593         }
594         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
595                 addr = hwif->io_ports[i];
596                 if (!hwif_request_region(hwif, addr, 1)) {
597                         while (--i)
598                                 release_region(addr, 1);
599                         goto data_region_busy;
600                 }
601         }
602         return 0;
603
604 data_region_busy:
605         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
606         if (addr)
607                 release_region(addr, 1);
608 control_region_busy:
609         /* If any errors are return, we drop the hwif interface. */
610         return -EBUSY;
611 }
612
613 /**
614  *      ide_hwif_release_regions - free IDE resources
615  *
616  *      Note that we only release the standard ports,
617  *      and do not even try to handle any extra ports
618  *      allocated for weird IDE interface chipsets.
619  *
620  *      Note also that we don't yet handle mmio resources here. More
621  *      importantly our caller should be doing this so we need to 
622  *      restructure this as a helper function for drivers.
623  */
624
625 void ide_hwif_release_regions(ide_hwif_t *hwif)
626 {
627         u32 i = 0;
628
629         if (hwif->mmio == 2)
630                 return;
631         if (hwif->io_ports[IDE_CONTROL_OFFSET])
632                 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
633         if (hwif->straight8) {
634                 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
635                 return;
636         }
637         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
638                 if (hwif->io_ports[i])
639                         release_region(hwif->io_ports[i], 1);
640 }
641
642 /**
643  *      ide_hwif_restore        -       restore hwif to template
644  *      @hwif: hwif to update
645  *      @tmp_hwif: template
646  *
647  *      Restore hwif to a previous state by copying most settngs
648  *      from the template.
649  */
650
651 static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
652 {
653         hwif->hwgroup                   = tmp_hwif->hwgroup;
654
655         hwif->gendev.parent             = tmp_hwif->gendev.parent;
656
657         hwif->proc                      = tmp_hwif->proc;
658
659         hwif->major                     = tmp_hwif->major;
660         hwif->straight8                 = tmp_hwif->straight8;
661         hwif->bus_state                 = tmp_hwif->bus_state;
662
663         hwif->atapi_dma                 = tmp_hwif->atapi_dma;
664         hwif->ultra_mask                = tmp_hwif->ultra_mask;
665         hwif->mwdma_mask                = tmp_hwif->mwdma_mask;
666         hwif->swdma_mask                = tmp_hwif->swdma_mask;
667
668         hwif->chipset                   = tmp_hwif->chipset;
669         hwif->hold                      = tmp_hwif->hold;
670
671 #ifdef CONFIG_BLK_DEV_IDEPCI
672         hwif->pci_dev                   = tmp_hwif->pci_dev;
673         hwif->cds                       = tmp_hwif->cds;
674 #endif
675
676         hwif->tuneproc                  = tmp_hwif->tuneproc;
677         hwif->speedproc                 = tmp_hwif->speedproc;
678         hwif->selectproc                = tmp_hwif->selectproc;
679         hwif->reset_poll                = tmp_hwif->reset_poll;
680         hwif->pre_reset                 = tmp_hwif->pre_reset;
681         hwif->resetproc                 = tmp_hwif->resetproc;
682         hwif->intrproc                  = tmp_hwif->intrproc;
683         hwif->maskproc                  = tmp_hwif->maskproc;
684         hwif->quirkproc                 = tmp_hwif->quirkproc;
685         hwif->busproc                   = tmp_hwif->busproc;
686
687         hwif->ata_input_data            = tmp_hwif->ata_input_data;
688         hwif->ata_output_data           = tmp_hwif->ata_output_data;
689         hwif->atapi_input_bytes         = tmp_hwif->atapi_input_bytes;
690         hwif->atapi_output_bytes        = tmp_hwif->atapi_output_bytes;
691
692         hwif->dma_setup                 = tmp_hwif->dma_setup;
693         hwif->dma_exec_cmd              = tmp_hwif->dma_exec_cmd;
694         hwif->dma_start                 = tmp_hwif->dma_start;
695         hwif->ide_dma_end               = tmp_hwif->ide_dma_end;
696         hwif->ide_dma_check             = tmp_hwif->ide_dma_check;
697         hwif->ide_dma_on                = tmp_hwif->ide_dma_on;
698         hwif->ide_dma_off_quietly       = tmp_hwif->ide_dma_off_quietly;
699         hwif->ide_dma_test_irq          = tmp_hwif->ide_dma_test_irq;
700         hwif->ide_dma_host_on           = tmp_hwif->ide_dma_host_on;
701         hwif->ide_dma_host_off          = tmp_hwif->ide_dma_host_off;
702         hwif->ide_dma_lostirq           = tmp_hwif->ide_dma_lostirq;
703         hwif->ide_dma_timeout           = tmp_hwif->ide_dma_timeout;
704
705         hwif->OUTB                      = tmp_hwif->OUTB;
706         hwif->OUTBSYNC                  = tmp_hwif->OUTBSYNC;
707         hwif->OUTW                      = tmp_hwif->OUTW;
708         hwif->OUTL                      = tmp_hwif->OUTL;
709         hwif->OUTSW                     = tmp_hwif->OUTSW;
710         hwif->OUTSL                     = tmp_hwif->OUTSL;
711
712         hwif->INB                       = tmp_hwif->INB;
713         hwif->INW                       = tmp_hwif->INW;
714         hwif->INL                       = tmp_hwif->INL;
715         hwif->INSW                      = tmp_hwif->INSW;
716         hwif->INSL                      = tmp_hwif->INSL;
717
718         hwif->sg_max_nents              = tmp_hwif->sg_max_nents;
719
720         hwif->mmio                      = tmp_hwif->mmio;
721         hwif->rqsize                    = tmp_hwif->rqsize;
722         hwif->no_lba48                  = tmp_hwif->no_lba48;
723
724 #ifndef CONFIG_BLK_DEV_IDECS
725         hwif->irq                       = tmp_hwif->irq;
726 #endif
727
728         hwif->dma_base                  = tmp_hwif->dma_base;
729         hwif->dma_master                = tmp_hwif->dma_master;
730         hwif->dma_command               = tmp_hwif->dma_command;
731         hwif->dma_vendor1               = tmp_hwif->dma_vendor1;
732         hwif->dma_status                = tmp_hwif->dma_status;
733         hwif->dma_vendor3               = tmp_hwif->dma_vendor3;
734         hwif->dma_prdtable              = tmp_hwif->dma_prdtable;
735
736         hwif->dma_extra                 = tmp_hwif->dma_extra;
737         hwif->config_data               = tmp_hwif->config_data;
738         hwif->select_data               = tmp_hwif->select_data;
739         hwif->autodma                   = tmp_hwif->autodma;
740         hwif->udma_four                 = tmp_hwif->udma_four;
741         hwif->no_dsc                    = tmp_hwif->no_dsc;
742
743         hwif->hwif_data                 = tmp_hwif->hwif_data;
744 }
745
746 /**
747  *      ide_unregister          -       free an ide interface
748  *      @index: index of interface (will change soon to a pointer)
749  *
750  *      Perform the final unregister of an IDE interface. At the moment
751  *      we don't refcount interfaces so this will also get split up.
752  *
753  *      Locking:
754  *      The caller must not hold the IDE locks
755  *      The drive present/vanishing is not yet properly locked
756  *      Take care with the callbacks. These have been split to avoid
757  *      deadlocking the IDE layer. The shutdown callback is called
758  *      before we take the lock and free resources. It is up to the
759  *      caller to be sure there is no pending I/O here, and that
760  *      the interfce will not be reopened (present/vanishing locking
761  *      isnt yet done btw). After we commit to the final kill we
762  *      call the cleanup callback with the ide locks held.
763  *
764  *      Unregister restores the hwif structures to the default state.
765  *      This is raving bonkers.
766  */
767
768 void ide_unregister(unsigned int index)
769 {
770         ide_drive_t *drive;
771         ide_hwif_t *hwif, *g;
772         static ide_hwif_t tmp_hwif; /* protected by ide_cfg_sem */
773         ide_hwgroup_t *hwgroup;
774         int irq_count = 0, unit, i;
775
776         BUG_ON(index >= MAX_HWIFS);
777
778         BUG_ON(in_interrupt());
779         BUG_ON(irqs_disabled());
780         down(&ide_cfg_sem);
781         spin_lock_irq(&ide_lock);
782         hwif = &ide_hwifs[index];
783         if (!hwif->present)
784                 goto abort;
785         for (unit = 0; unit < MAX_DRIVES; ++unit) {
786                 drive = &hwif->drives[unit];
787                 if (!drive->present)
788                         continue;
789                 if (drive->usage || DRIVER(drive)->busy)
790                         goto abort;
791                 drive->dead = 1;
792         }
793         hwif->present = 0;
794
795         spin_unlock_irq(&ide_lock);
796
797         for (unit = 0; unit < MAX_DRIVES; ++unit) {
798                 drive = &hwif->drives[unit];
799                 if (!drive->present)
800                         continue;
801                 DRIVER(drive)->cleanup(drive);
802         }
803
804         destroy_proc_ide_interface(hwif);
805
806         hwgroup = hwif->hwgroup;
807         /*
808          * free the irq if we were the only hwif using it
809          */
810         g = hwgroup->hwif;
811         do {
812                 if (g->irq == hwif->irq)
813                         ++irq_count;
814                 g = g->next;
815         } while (g != hwgroup->hwif);
816         if (irq_count == 1)
817                 free_irq(hwif->irq, hwgroup);
818
819         spin_lock_irq(&ide_lock);
820         /*
821          * Note that we only release the standard ports,
822          * and do not even try to handle any extra ports
823          * allocated for weird IDE interface chipsets.
824          */
825         ide_hwif_release_regions(hwif);
826
827         /*
828          * Remove us from the hwgroup, and free
829          * the hwgroup if we were the only member
830          */
831         for (i = 0; i < MAX_DRIVES; ++i) {
832                 drive = &hwif->drives[i];
833                 if (drive->devfs_name[0] != '\0') {
834                         devfs_remove(drive->devfs_name);
835                         drive->devfs_name[0] = '\0';
836                 }
837                 if (!drive->present)
838                         continue;
839                 if (drive == drive->next) {
840                         /* special case: last drive from hwgroup. */
841                         BUG_ON(hwgroup->drive != drive);
842                         hwgroup->drive = NULL;
843                 } else {
844                         ide_drive_t *walk;
845
846                         walk = hwgroup->drive;
847                         while (walk->next != drive)
848                                 walk = walk->next;
849                         walk->next = drive->next;
850                         if (hwgroup->drive == drive) {
851                                 hwgroup->drive = drive->next;
852                                 hwgroup->hwif = HWIF(hwgroup->drive);
853                         }
854                 }
855                 BUG_ON(hwgroup->drive == drive);
856                 if (drive->id != NULL) {
857                         kfree(drive->id);
858                         drive->id = NULL;
859                 }
860                 drive->present = 0;
861                 /* Messed up locking ... */
862                 spin_unlock_irq(&ide_lock);
863                 blk_cleanup_queue(drive->queue);
864                 device_unregister(&drive->gendev);
865                 down(&drive->gendev_rel_sem);
866                 spin_lock_irq(&ide_lock);
867                 drive->queue = NULL;
868         }
869         if (hwif->next == hwif) {
870                 BUG_ON(hwgroup->hwif != hwif);
871                 kfree(hwgroup);
872         } else {
873                 /* There is another interface in hwgroup.
874                  * Unlink us, and set hwgroup->drive and ->hwif to
875                  * something sane.
876                  */
877                 g = hwgroup->hwif;
878                 while (g->next != hwif)
879                         g = g->next;
880                 g->next = hwif->next;
881                 if (hwgroup->hwif == hwif) {
882                         /* Chose a random hwif for hwgroup->hwif.
883                          * It's guaranteed that there are no drives
884                          * left in the hwgroup.
885                          */
886                         BUG_ON(hwgroup->drive != NULL);
887                         hwgroup->hwif = g;
888                 }
889                 BUG_ON(hwgroup->hwif == hwif);
890         }
891
892         /* More messed up locking ... */
893         spin_unlock_irq(&ide_lock);
894         device_unregister(&hwif->gendev);
895         down(&hwif->gendev_rel_sem);
896
897         /*
898          * Remove us from the kernel's knowledge
899          */
900         blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
901         for (i = 0; i < MAX_DRIVES; i++) {
902                 struct gendisk *disk = hwif->drives[i].disk;
903                 hwif->drives[i].disk = NULL;
904                 put_disk(disk);
905         }
906         kfree(hwif->sg_table);
907         unregister_blkdev(hwif->major, hwif->name);
908         spin_lock_irq(&ide_lock);
909
910         if (hwif->dma_base) {
911                 (void) ide_release_dma(hwif);
912
913                 hwif->dma_base = 0;
914                 hwif->dma_master = 0;
915                 hwif->dma_command = 0;
916                 hwif->dma_vendor1 = 0;
917                 hwif->dma_status = 0;
918                 hwif->dma_vendor3 = 0;
919                 hwif->dma_prdtable = 0;
920         }
921
922         /* copy original settings */
923         tmp_hwif = *hwif;
924
925         /* restore hwif data to pristine status */
926         init_hwif_data(hwif, index);
927         init_hwif_default(hwif, index);
928
929         ide_hwif_restore(hwif, &tmp_hwif);
930
931 abort:
932         spin_unlock_irq(&ide_lock);
933         up(&ide_cfg_sem);
934 }
935
936 EXPORT_SYMBOL(ide_unregister);
937
938
939 /**
940  *      ide_setup_ports         -       set up IDE interface ports
941  *      @hw: register descriptions
942  *      @base: base register
943  *      @offsets: table of register offsets
944  *      @ctrl: control register
945  *      @ack_irq: IRQ ack
946  *      @irq: interrupt lie
947  *
948  *      Setup hw_regs_t structure described by parameters.  You
949  *      may set up the hw structure yourself OR use this routine to
950  *      do it for you. This is basically a helper
951  *
952  */
953  
954 void ide_setup_ports (  hw_regs_t *hw,
955                         unsigned long base, int *offsets,
956                         unsigned long ctrl, unsigned long intr,
957                         ide_ack_intr_t *ack_intr,
958 /*
959  *                      ide_io_ops_t *iops,
960  */
961                         int irq)
962 {
963         int i;
964
965         for (i = 0; i < IDE_NR_PORTS; i++) {
966                 if (offsets[i] == -1) {
967                         switch(i) {
968                                 case IDE_CONTROL_OFFSET:
969                                         hw->io_ports[i] = ctrl;
970                                         break;
971 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
972                                 case IDE_IRQ_OFFSET:
973                                         hw->io_ports[i] = intr;
974                                         break;
975 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
976                                 default:
977                                         hw->io_ports[i] = 0;
978                                         break;
979                         }
980                 } else {
981                         hw->io_ports[i] = base + offsets[i];
982                 }
983         }
984         hw->irq = irq;
985         hw->dma = NO_DMA;
986         hw->ack_intr = ack_intr;
987 /*
988  *      hw->iops = iops;
989  */
990 }
991
992 /**
993  *      ide_register_hw_with_fixup      -       register IDE interface
994  *      @hw: hardware registers
995  *      @hwifp: pointer to returned hwif
996  *      @fixup: fixup function
997  *
998  *      Register an IDE interface, specifying exactly the registers etc.
999  *      Set init=1 iff calling before probes have taken place.
1000  *
1001  *      Returns -1 on error.
1002  */
1003
1004 int ide_register_hw_with_fixup(hw_regs_t *hw, ide_hwif_t **hwifp, void(*fixup)(ide_hwif_t *hwif))
1005 {
1006         int index, retry = 1;
1007         ide_hwif_t *hwif;
1008
1009         do {
1010                 for (index = 0; index < MAX_HWIFS; ++index) {
1011                         hwif = &ide_hwifs[index];
1012                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
1013                                 goto found;
1014                 }
1015                 for (index = 0; index < MAX_HWIFS; ++index) {
1016                         hwif = &ide_hwifs[index];
1017                         if (hwif->hold)
1018                                 continue;
1019                         if ((!hwif->present && !hwif->mate && !initializing) ||
1020                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
1021                                 goto found;
1022                 }
1023                 for (index = 0; index < MAX_HWIFS; index++)
1024                         ide_unregister(index);
1025         } while (retry--);
1026         return -1;
1027 found:
1028         if (hwif->present)
1029                 ide_unregister(index);
1030         else if (!hwif->hold) {
1031                 init_hwif_data(hwif, index);
1032                 init_hwif_default(hwif, index);
1033         }
1034         if (hwif->present)
1035                 return -1;
1036         memcpy(&hwif->hw, hw, sizeof(*hw));
1037         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
1038         hwif->irq = hw->irq;
1039         hwif->noprobe = 0;
1040         hwif->chipset = hw->chipset;
1041
1042         if (!initializing) {
1043                 probe_hwif_init_with_fixup(hwif, fixup);
1044                 create_proc_ide_interfaces();
1045         }
1046
1047         if (hwifp)
1048                 *hwifp = hwif;
1049
1050         return (initializing || hwif->present) ? index : -1;
1051 }
1052
1053 EXPORT_SYMBOL(ide_register_hw_with_fixup);
1054
1055 int ide_register_hw(hw_regs_t *hw, ide_hwif_t **hwifp)
1056 {
1057         return ide_register_hw_with_fixup(hw, hwifp, NULL);
1058 }
1059
1060 EXPORT_SYMBOL(ide_register_hw);
1061
1062 /*
1063  *      Locks for IDE setting functionality
1064  */
1065
1066 DECLARE_MUTEX(ide_setting_sem);
1067
1068 /**
1069  *      ide_add_setting -       add an ide setting option
1070  *      @drive: drive to use
1071  *      @name: setting name
1072  *      @rw: true if the function is read write
1073  *      @read_ioctl: function to call on read
1074  *      @write_ioctl: function to call on write
1075  *      @data_type: type of data
1076  *      @min: range minimum
1077  *      @max: range maximum
1078  *      @mul_factor: multiplication scale
1079  *      @div_factor: divison scale
1080  *      @data: private data field
1081  *      @set: setting
1082  *
1083  *      Removes the setting named from the device if it is present.
1084  *      The function takes the settings_lock to protect against 
1085  *      parallel changes. This function must not be called from IRQ
1086  *      context. Returns 0 on success or -1 on failure.
1087  *
1088  *      BUGS: This code is seriously over-engineered. There is also
1089  *      magic about how the driver specific features are setup. If
1090  *      a driver is attached we assume the driver settings are auto
1091  *      remove.
1092  */
1093  
1094 int ide_add_setting (ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
1095 {
1096         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1097
1098         down(&ide_setting_sem);
1099         while ((*p) && strcmp((*p)->name, name) < 0)
1100                 p = &((*p)->next);
1101         if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1102                 goto abort;
1103         memset(setting, 0, sizeof(*setting));
1104         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1105                 goto abort;
1106         strcpy(setting->name, name);
1107         setting->rw = rw;
1108         setting->read_ioctl = read_ioctl;
1109         setting->write_ioctl = write_ioctl;
1110         setting->data_type = data_type;
1111         setting->min = min;
1112         setting->max = max;
1113         setting->mul_factor = mul_factor;
1114         setting->div_factor = div_factor;
1115         setting->data = data;
1116         setting->set = set;
1117         
1118         setting->next = *p;
1119         if (drive->driver != &idedefault_driver)
1120                 setting->auto_remove = 1;
1121         *p = setting;
1122         up(&ide_setting_sem);
1123         return 0;
1124 abort:
1125         up(&ide_setting_sem);
1126         if (setting)
1127                 kfree(setting);
1128         return -1;
1129 }
1130
1131 EXPORT_SYMBOL(ide_add_setting);
1132
1133 /**
1134  *      __ide_remove_setting    -       remove an ide setting option
1135  *      @drive: drive to use
1136  *      @name: setting name
1137  *
1138  *      Removes the setting named from the device if it is present.
1139  *      The caller must hold the setting semaphore.
1140  */
1141  
1142 static void __ide_remove_setting (ide_drive_t *drive, char *name)
1143 {
1144         ide_settings_t **p, *setting;
1145
1146         p = (ide_settings_t **) &drive->settings;
1147
1148         while ((*p) && strcmp((*p)->name, name))
1149                 p = &((*p)->next);
1150         if ((setting = (*p)) == NULL)
1151                 return;
1152
1153         (*p) = setting->next;
1154         
1155         kfree(setting->name);
1156         kfree(setting);
1157 }
1158
1159 /**
1160  *      ide_find_setting_by_ioctl       -       find a drive specific ioctl
1161  *      @drive: drive to scan
1162  *      @cmd: ioctl command to handle
1163  *
1164  *      Scan's the device setting table for a matching entry and returns
1165  *      this or NULL if no entry is found. The caller must hold the
1166  *      setting semaphore
1167  */
1168  
1169 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1170 {
1171         ide_settings_t *setting = drive->settings;
1172
1173         while (setting) {
1174                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1175                         break;
1176                 setting = setting->next;
1177         }
1178         
1179         return setting;
1180 }
1181
1182 /**
1183  *      ide_find_setting_by_name        -       find a drive specific setting
1184  *      @drive: drive to scan
1185  *      @name: setting name
1186  *
1187  *      Scan's the device setting table for a matching entry and returns
1188  *      this or NULL if no entry is found. The caller must hold the
1189  *      setting semaphore
1190  */
1191  
1192 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1193 {
1194         ide_settings_t *setting = drive->settings;
1195
1196         while (setting) {
1197                 if (strcmp(setting->name, name) == 0)
1198                         break;
1199                 setting = setting->next;
1200         }
1201         return setting;
1202 }
1203
1204 /**
1205  *      auto_remove_settings    -       remove driver specific settings
1206  *      @drive: drive
1207  *
1208  *      Automatically remove all the driver specific settings for this
1209  *      drive. This function may sleep and must not be called from IRQ
1210  *      context. The caller must hold ide_setting_sem.
1211  */
1212  
1213 static void auto_remove_settings (ide_drive_t *drive)
1214 {
1215         ide_settings_t *setting;
1216 repeat:
1217         setting = drive->settings;
1218         while (setting) {
1219                 if (setting->auto_remove) {
1220                         __ide_remove_setting(drive, setting->name);
1221                         goto repeat;
1222                 }
1223                 setting = setting->next;
1224         }
1225 }
1226
1227 /**
1228  *      ide_read_setting        -       read an IDE setting
1229  *      @drive: drive to read from
1230  *      @setting: drive setting
1231  *
1232  *      Read a drive setting and return the value. The caller
1233  *      must hold the ide_setting_sem when making this call.
1234  *
1235  *      BUGS: the data return and error are the same return value
1236  *      so an error -EINVAL and true return of the same value cannot
1237  *      be told apart
1238  */
1239  
1240 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1241 {
1242         int             val = -EINVAL;
1243         unsigned long   flags;
1244
1245         if ((setting->rw & SETTING_READ)) {
1246                 spin_lock_irqsave(&ide_lock, flags);
1247                 switch(setting->data_type) {
1248                         case TYPE_BYTE:
1249                                 val = *((u8 *) setting->data);
1250                                 break;
1251                         case TYPE_SHORT:
1252                                 val = *((u16 *) setting->data);
1253                                 break;
1254                         case TYPE_INT:
1255                         case TYPE_INTA:
1256                                 val = *((u32 *) setting->data);
1257                                 break;
1258                 }
1259                 spin_unlock_irqrestore(&ide_lock, flags);
1260         }
1261         return val;
1262 }
1263
1264 /**
1265  *      ide_spin_wait_hwgroup   -       wait for group
1266  *      @drive: drive in the group
1267  *
1268  *      Wait for an IDE device group to go non busy and then return
1269  *      holding the ide_lock which guards the hwgroup->busy status
1270  *      and right to use it.
1271  */
1272
1273 int ide_spin_wait_hwgroup (ide_drive_t *drive)
1274 {
1275         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1276         unsigned long timeout = jiffies + (3 * HZ);
1277
1278         spin_lock_irq(&ide_lock);
1279
1280         while (hwgroup->busy) {
1281                 unsigned long lflags;
1282                 spin_unlock_irq(&ide_lock);
1283                 local_irq_set(lflags);
1284                 if (time_after(jiffies, timeout)) {
1285                         local_irq_restore(lflags);
1286                         printk(KERN_ERR "%s: channel busy\n", drive->name);
1287                         return -EBUSY;
1288                 }
1289                 local_irq_restore(lflags);
1290                 spin_lock_irq(&ide_lock);
1291         }
1292         return 0;
1293 }
1294
1295 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1296
1297 /**
1298  *      ide_write_setting       -       read an IDE setting
1299  *      @drive: drive to read from
1300  *      @setting: drive setting
1301  *      @val: value
1302  *
1303  *      Write a drive setting if it is possible. The caller
1304  *      must hold the ide_setting_sem when making this call.
1305  *
1306  *      BUGS: the data return and error are the same return value
1307  *      so an error -EINVAL and true return of the same value cannot
1308  *      be told apart
1309  *
1310  *      FIXME:  This should be changed to enqueue a special request
1311  *      to the driver to change settings, and then wait on a sema for completion.
1312  *      The current scheme of polling is kludgy, though safe enough.
1313  */
1314
1315 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1316 {
1317         int i;
1318         u32 *p;
1319
1320         if (!capable(CAP_SYS_ADMIN))
1321                 return -EACCES;
1322         if (!(setting->rw & SETTING_WRITE))
1323                 return -EPERM;
1324         if (val < setting->min || val > setting->max)
1325                 return -EINVAL;
1326         if (setting->set)
1327                 return setting->set(drive, val);
1328         if (ide_spin_wait_hwgroup(drive))
1329                 return -EBUSY;
1330         switch (setting->data_type) {
1331                 case TYPE_BYTE:
1332                         *((u8 *) setting->data) = val;
1333                         break;
1334                 case TYPE_SHORT:
1335                         *((u16 *) setting->data) = val;
1336                         break;
1337                 case TYPE_INT:
1338                         *((u32 *) setting->data) = val;
1339                         break;
1340                 case TYPE_INTA:
1341                         p = (u32 *) setting->data;
1342                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1343                                 *p = val;
1344                         break;
1345         }
1346         spin_unlock_irq(&ide_lock);
1347         return 0;
1348 }
1349
1350 static int set_io_32bit(ide_drive_t *drive, int arg)
1351 {
1352         drive->io_32bit = arg;
1353 #ifdef CONFIG_BLK_DEV_DTC2278
1354         if (HWIF(drive)->chipset == ide_dtc2278)
1355                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1356 #endif /* CONFIG_BLK_DEV_DTC2278 */
1357         return 0;
1358 }
1359
1360 static int set_using_dma (ide_drive_t *drive, int arg)
1361 {
1362 #ifdef CONFIG_BLK_DEV_IDEDMA
1363         if (!drive->id || !(drive->id->capability & 1))
1364                 return -EPERM;
1365         if (HWIF(drive)->ide_dma_check == NULL)
1366                 return -EPERM;
1367         if (arg) {
1368                 if (HWIF(drive)->ide_dma_check(drive)) return -EIO;
1369                 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1370         } else {
1371                 if (__ide_dma_off(drive))
1372                         return -EIO;
1373         }
1374         return 0;
1375 #else
1376         return -EPERM;
1377 #endif
1378 }
1379
1380 static int set_pio_mode (ide_drive_t *drive, int arg)
1381 {
1382         struct request rq;
1383
1384         if (!HWIF(drive)->tuneproc)
1385                 return -ENOSYS;
1386         if (drive->special.b.set_tune)
1387                 return -EBUSY;
1388         ide_init_drive_cmd(&rq);
1389         drive->tune_req = (u8) arg;
1390         drive->special.b.set_tune = 1;
1391         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1392         return 0;
1393 }
1394
1395 static int set_xfer_rate (ide_drive_t *drive, int arg)
1396 {
1397         int err = ide_wait_cmd(drive,
1398                         WIN_SETFEATURES, (u8) arg,
1399                         SETFEATURES_XFER, 0, NULL);
1400
1401         if (!err && arg) {
1402                 ide_set_xfer_rate(drive, (u8) arg);
1403                 ide_driveid_update(drive);
1404         }
1405         return err;
1406 }
1407
1408 /**
1409  *      ide_add_generic_settings        -       generic ide settings
1410  *      @drive: drive being configured
1411  *
1412  *      Add the generic parts of the system settings to the /proc files and
1413  *      ioctls for this IDE device. The caller must not be holding the
1414  *      ide_setting_sem.
1415  */
1416
1417 void ide_add_generic_settings (ide_drive_t *drive)
1418 {
1419 /*
1420  *                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function
1421  */
1422         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);
1423         ide_add_setting(drive,  "keepsettings",         SETTING_RW,                                     HDIO_GET_KEEPSETTINGS,  HDIO_SET_KEEPSETTINGS,  TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL);
1424         ide_add_setting(drive,  "nice1",                SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL);
1425         ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
1426         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);
1427         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);
1428         ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->init_speed,             NULL);
1429         ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->current_speed,          set_xfer_rate);
1430         ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
1431 }
1432
1433 /**
1434  *      system_bus_clock        -       clock guess
1435  *
1436  *      External version of the bus clock guess used by very old IDE drivers
1437  *      for things like VLB timings. Should not be used.
1438  */
1439
1440 int system_bus_clock (void)
1441 {
1442         return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1443 }
1444
1445 EXPORT_SYMBOL(system_bus_clock);
1446
1447 /*
1448  *      Locking is badly broken here - since way back.  That sucker is
1449  * root-only, but that's not an excuse...  The real question is what
1450  * exclusion rules do we want here.
1451  */
1452 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1453 {
1454         if (!drive->present || drive->usage || drive->dead)
1455                 goto abort;
1456         if (DRIVER(drive)->cleanup(drive))
1457                 goto abort;
1458         strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
1459         if (ata_attach(drive)) {
1460                 spin_lock(&drives_lock);
1461                 list_del_init(&drive->list);
1462                 spin_unlock(&drives_lock);
1463                 drive->driver_req[0] = 0;
1464                 ata_attach(drive);
1465         } else {
1466                 drive->driver_req[0] = 0;
1467         }
1468         if (DRIVER(drive)!= &idedefault_driver && !strcmp(DRIVER(drive)->name, driver))
1469                 return 0;
1470 abort:
1471         return 1;
1472 }
1473
1474 /**
1475  *      ata_attach              -       attach an ATA/ATAPI device
1476  *      @drive: drive to attach
1477  *
1478  *      Takes a drive that is as yet not assigned to any midlayer IDE
1479  *      driver (or is assigned to the default driver) and figures out
1480  *      which driver would like to own it. If nobody claims the drive
1481  *      then it is automatically attached to the default driver used for
1482  *      unclaimed objects.
1483  *
1484  *      A return of zero indicates attachment to a driver, of one
1485  *      attachment to the default driver.
1486  *
1487  *      Takes drivers_lock.
1488  */
1489
1490 int ata_attach(ide_drive_t *drive)
1491 {
1492         struct list_head *p;
1493         spin_lock(&drivers_lock);
1494         list_for_each(p, &drivers) {
1495                 ide_driver_t *driver = list_entry(p, ide_driver_t, drivers);
1496                 if (!try_module_get(driver->owner))
1497                         continue;
1498                 spin_unlock(&drivers_lock);
1499                 if (driver->attach(drive) == 0) {
1500                         module_put(driver->owner);
1501                         drive->gendev.driver = &driver->gen_driver;
1502                         return 0;
1503                 }
1504                 spin_lock(&drivers_lock);
1505                 module_put(driver->owner);
1506         }
1507         drive->gendev.driver = &idedefault_driver.gen_driver;
1508         spin_unlock(&drivers_lock);
1509         if(idedefault_driver.attach(drive) != 0)
1510                 panic("ide: default attach failed");
1511         return 1;
1512 }
1513
1514 static int generic_ide_suspend(struct device *dev, u32 state)
1515 {
1516         ide_drive_t *drive = dev->driver_data;
1517         struct request rq;
1518         struct request_pm_state rqpm;
1519         ide_task_t args;
1520
1521         memset(&rq, 0, sizeof(rq));
1522         memset(&rqpm, 0, sizeof(rqpm));
1523         memset(&args, 0, sizeof(args));
1524         rq.flags = REQ_PM_SUSPEND;
1525         rq.special = &args;
1526         rq.pm = &rqpm;
1527         rqpm.pm_step = ide_pm_state_start_suspend;
1528         rqpm.pm_state = state;
1529
1530         return ide_do_drive_cmd(drive, &rq, ide_wait);
1531 }
1532
1533 static int generic_ide_resume(struct device *dev)
1534 {
1535         ide_drive_t *drive = dev->driver_data;
1536         struct request rq;
1537         struct request_pm_state rqpm;
1538         ide_task_t args;
1539
1540         memset(&rq, 0, sizeof(rq));
1541         memset(&rqpm, 0, sizeof(rqpm));
1542         memset(&args, 0, sizeof(args));
1543         rq.flags = REQ_PM_RESUME;
1544         rq.special = &args;
1545         rq.pm = &rqpm;
1546         rqpm.pm_step = ide_pm_state_start_resume;
1547         rqpm.pm_state = 0;
1548
1549         return ide_do_drive_cmd(drive, &rq, ide_head_wait);
1550 }
1551
1552 int generic_ide_ioctl(struct file *file, struct block_device *bdev,
1553                         unsigned int cmd, unsigned long arg)
1554 {
1555         ide_drive_t *drive = bdev->bd_disk->private_data;
1556         ide_settings_t *setting;
1557         int err = 0;
1558         void __user *p = (void __user *)arg;
1559
1560         down(&ide_setting_sem);
1561         if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1562                 if (cmd == setting->read_ioctl) {
1563                         err = ide_read_setting(drive, setting);
1564                         up(&ide_setting_sem);
1565                         return err >= 0 ? put_user(err, (long __user *)arg) : err;
1566                 } else {
1567                         if (bdev != bdev->bd_contains)
1568                                 err = -EINVAL;
1569                         else
1570                                 err = ide_write_setting(drive, setting, arg);
1571                         up(&ide_setting_sem);
1572                         return err;
1573                 }
1574         }
1575         up(&ide_setting_sem);
1576
1577         switch (cmd) {
1578                 case HDIO_GETGEO:
1579                 {
1580                         struct hd_geometry geom;
1581                         if (!p || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1582                         geom.heads = drive->bios_head;
1583                         geom.sectors = drive->bios_sect;
1584                         geom.cylinders = (u16)drive->bios_cyl; /* truncate */
1585                         geom.start = get_start_sect(bdev);
1586                         if (copy_to_user(p, &geom, sizeof(struct hd_geometry)))
1587                                 return -EFAULT;
1588                         return 0;
1589                 }
1590
1591                 case HDIO_OBSOLETE_IDENTITY:
1592                 case HDIO_GET_IDENTITY:
1593                         if (bdev != bdev->bd_contains)
1594                                 return -EINVAL;
1595                         if (drive->id_read == 0)
1596                                 return -ENOMSG;
1597                         if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1598                                 return -EFAULT;
1599                         return 0;
1600
1601                 case HDIO_GET_NICE:
1602                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
1603                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
1604                                         drive->nice0            <<      IDE_NICE_0              |
1605                                         drive->nice1            <<      IDE_NICE_1              |
1606                                         drive->nice2            <<      IDE_NICE_2,
1607                                         (long __user *) arg);
1608
1609 #ifdef CONFIG_IDE_TASK_IOCTL
1610                 case HDIO_DRIVE_TASKFILE:
1611                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1612                                 return -EACCES;
1613                         switch(drive->media) {
1614                                 case ide_disk:
1615                                         return ide_taskfile_ioctl(drive, cmd, arg);
1616                                 default:
1617                                         return -ENOMSG;
1618                         }
1619 #endif /* CONFIG_IDE_TASK_IOCTL */
1620
1621                 case HDIO_DRIVE_CMD:
1622                         if (!capable(CAP_SYS_RAWIO))
1623                                 return -EACCES;
1624                         return ide_cmd_ioctl(drive, cmd, arg);
1625
1626                 case HDIO_DRIVE_TASK:
1627                         if (!capable(CAP_SYS_RAWIO))
1628                                 return -EACCES;
1629                         return ide_task_ioctl(drive, cmd, arg);
1630
1631                 case HDIO_SCAN_HWIF:
1632                 {
1633                         hw_regs_t hw;
1634                         int args[3];
1635                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1636                         if (copy_from_user(args, p, 3 * sizeof(int)))
1637                                 return -EFAULT;
1638                         memset(&hw, 0, sizeof(hw));
1639                         ide_init_hwif_ports(&hw, (unsigned long) args[0],
1640                                             (unsigned long) args[1], NULL);
1641                         hw.irq = args[2];
1642                         if (ide_register_hw(&hw, NULL) == -1)
1643                                 return -EIO;
1644                         return 0;
1645                 }
1646                 case HDIO_UNREGISTER_HWIF:
1647                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1648                         /* (arg > MAX_HWIFS) checked in function */
1649                         ide_unregister(arg);
1650                         return 0;
1651                 case HDIO_SET_NICE:
1652                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1653                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1654                                 return -EPERM;
1655                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1656                         if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1657                                 drive->dsc_overlap = 0;
1658                                 return -EPERM;
1659                         }
1660                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
1661                         return 0;
1662                 case HDIO_DRIVE_RESET:
1663                 {
1664                         unsigned long flags;
1665                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1666                         
1667                         /*
1668                          *      Abort the current command on the
1669                          *      group if there is one, taking
1670                          *      care not to allow anything else
1671                          *      to be queued and to die on the
1672                          *      spot if we miss one somehow
1673                          */
1674
1675                         spin_lock_irqsave(&ide_lock, flags);
1676                         
1677                         DRIVER(drive)->abort(drive, "drive reset");
1678                         if(HWGROUP(drive)->handler)
1679                                 BUG();
1680                                 
1681                         /* Ensure nothing gets queued after we
1682                            drop the lock. Reset will clear the busy */
1683                    
1684                         HWGROUP(drive)->busy = 1;
1685                         spin_unlock_irqrestore(&ide_lock, flags);
1686                         (void) ide_do_reset(drive);
1687                         if (drive->suspend_reset) {
1688 /*
1689  *                              APM WAKE UP todo !!
1690  *                              int nogoodpower = 1;
1691  *                              while(nogoodpower) {
1692  *                                      check_power1() or check_power2()
1693  *                                      nogoodpower = 0;
1694  *                              } 
1695  *                              HWIF(drive)->multiproc(drive);
1696  */
1697                                 return ioctl_by_bdev(bdev, BLKRRPART, 0);
1698                         }
1699                         return 0;
1700                 }
1701
1702                 case CDROMEJECT:
1703                 case CDROMCLOSETRAY:
1704                         return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
1705
1706                 case HDIO_GET_BUSSTATE:
1707                         if (!capable(CAP_SYS_ADMIN))
1708                                 return -EACCES;
1709                         if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1710                                 return -EFAULT;
1711                         return 0;
1712
1713                 case HDIO_SET_BUSSTATE:
1714                         if (!capable(CAP_SYS_ADMIN))
1715                                 return -EACCES;
1716                         if (HWIF(drive)->busproc)
1717                                 return HWIF(drive)->busproc(drive, (int)arg);
1718                         return -EOPNOTSUPP;
1719                 default:
1720                         return -EINVAL;
1721         }
1722 }
1723
1724 EXPORT_SYMBOL(generic_ide_ioctl);
1725
1726 /*
1727  * stridx() returns the offset of c within s,
1728  * or -1 if c is '\0' or not found within s.
1729  */
1730 static int __init stridx (const char *s, char c)
1731 {
1732         char *i = strchr(s, c);
1733         return (i && c) ? i - s : -1;
1734 }
1735
1736 /*
1737  * match_parm() does parsing for ide_setup():
1738  *
1739  * 1. the first char of s must be '='.
1740  * 2. if the remainder matches one of the supplied keywords,
1741  *     the index (1 based) of the keyword is negated and returned.
1742  * 3. if the remainder is a series of no more than max_vals numbers
1743  *     separated by commas, the numbers are saved in vals[] and a
1744  *     count of how many were saved is returned.  Base10 is assumed,
1745  *     and base16 is allowed when prefixed with "0x".
1746  * 4. otherwise, zero is returned.
1747  */
1748 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1749 {
1750         static const char *decimal = "0123456789";
1751         static const char *hex = "0123456789abcdef";
1752         int i, n;
1753
1754         if (*s++ == '=') {
1755                 /*
1756                  * Try matching against the supplied keywords,
1757                  * and return -(index+1) if we match one
1758                  */
1759                 if (keywords != NULL) {
1760                         for (i = 0; *keywords != NULL; ++i) {
1761                                 if (!strcmp(s, *keywords++))
1762                                         return -(i+1);
1763                         }
1764                 }
1765                 /*
1766                  * Look for a series of no more than "max_vals"
1767                  * numeric values separated by commas, in base10,
1768                  * or base16 when prefixed with "0x".
1769                  * Return a count of how many were found.
1770                  */
1771                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1772                         vals[n] = i;
1773                         while ((i = stridx(decimal, *++s)) >= 0)
1774                                 vals[n] = (vals[n] * 10) + i;
1775                         if (*s == 'x' && !vals[n]) {
1776                                 while ((i = stridx(hex, *++s)) >= 0)
1777                                         vals[n] = (vals[n] * 0x10) + i;
1778                         }
1779                         if (++n == max_vals)
1780                                 break;
1781                         if (*s == ',' || *s == ';')
1782                                 ++s;
1783                 }
1784                 if (!*s)
1785                         return n;
1786         }
1787         return 0;       /* zero = nothing matched */
1788 }
1789
1790 #ifdef CONFIG_BLK_DEV_ALI14XX
1791 static int __initdata probe_ali14xx;
1792 extern int ali14xx_init(void);
1793 #endif
1794 #ifdef CONFIG_BLK_DEV_UMC8672
1795 static int __initdata probe_umc8672;
1796 extern int umc8672_init(void);
1797 #endif
1798 #ifdef CONFIG_BLK_DEV_DTC2278
1799 static int __initdata probe_dtc2278;
1800 extern int dtc2278_init(void);
1801 #endif
1802 #ifdef CONFIG_BLK_DEV_HT6560B
1803 static int __initdata probe_ht6560b;
1804 extern int ht6560b_init(void);
1805 #endif
1806 #ifdef CONFIG_BLK_DEV_QD65XX
1807 static int __initdata probe_qd65xx;
1808 extern int qd65xx_init(void);
1809 #endif
1810
1811 static int __initdata is_chipset_set[MAX_HWIFS];
1812
1813 /*
1814  * ide_setup() gets called VERY EARLY during initialization,
1815  * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1816  *
1817  * Remember to update Documentation/ide.txt if you change something here.
1818  */
1819 int __init ide_setup (char *s)
1820 {
1821         int i, vals[3];
1822         ide_hwif_t *hwif;
1823         ide_drive_t *drive;
1824         unsigned int hw, unit;
1825         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1826         const char max_hwif  = '0' + (MAX_HWIFS - 1);
1827
1828         
1829         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
1830                 return 0;                               /* driver and not us */
1831
1832         if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1833                 return 0;
1834
1835         printk(KERN_INFO "ide_setup: %s", s);
1836         init_ide_data ();
1837
1838 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1839         if (!strcmp(s, "ide=doubler")) {
1840                 extern int ide_doubler;
1841
1842                 printk(" : Enabled support for IDE doublers\n");
1843                 ide_doubler = 1;
1844                 return 1;
1845         }
1846 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1847
1848         if (!strcmp(s, "ide=nodma")) {
1849                 printk(" : Prevented DMA\n");
1850                 noautodma = 1;
1851                 return 1;
1852         }
1853
1854 #ifdef CONFIG_BLK_DEV_IDEPCI
1855         if (!strcmp(s, "ide=reverse")) {
1856                 ide_scan_direction = 1;
1857                 printk(" : Enabled support for IDE inverse scan order.\n");
1858                 return 1;
1859         }
1860 #endif /* CONFIG_BLK_DEV_IDEPCI */
1861
1862         /*
1863          * Look for drive options:  "hdx="
1864          */
1865         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1866                 const char *hd_words[] = {
1867                         "none", "noprobe", "nowerr", "cdrom", "serialize",
1868                         "autotune", "noautotune", "minus8", "swapdata", "bswap",
1869                         "minus11", "remap", "remap63", "scsi", NULL };
1870                 unit = s[2] - 'a';
1871                 hw   = unit / MAX_DRIVES;
1872                 unit = unit % MAX_DRIVES;
1873                 hwif = &ide_hwifs[hw];
1874                 drive = &hwif->drives[unit];
1875                 if (strncmp(s + 4, "ide-", 4) == 0) {
1876                         strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1877                         goto done;
1878                 }
1879                 switch (match_parm(&s[3], hd_words, vals, 3)) {
1880                         case -1: /* "none" */
1881                         case -2: /* "noprobe" */
1882                                 drive->noprobe = 1;
1883                                 goto done;
1884                         case -3: /* "nowerr" */
1885                                 drive->bad_wstat = BAD_R_STAT;
1886                                 hwif->noprobe = 0;
1887                                 goto done;
1888                         case -4: /* "cdrom" */
1889                                 drive->present = 1;
1890                                 drive->media = ide_cdrom;
1891                                 hwif->noprobe = 0;
1892                                 goto done;
1893                         case -5: /* "serialize" */
1894                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
1895                                 goto do_serialize;
1896                         case -6: /* "autotune" */
1897                                 drive->autotune = IDE_TUNE_AUTO;
1898                                 goto obsolete_option;
1899                         case -7: /* "noautotune" */
1900                                 drive->autotune = IDE_TUNE_NOAUTO;
1901                                 goto obsolete_option;
1902                         case -9: /* "swapdata" */
1903                         case -10: /* "bswap" */
1904                                 drive->bswap = 1;
1905                                 goto done;
1906                         case -12: /* "remap" */
1907                                 drive->remap_0_to_1 = 1;
1908                                 goto done;
1909                         case -13: /* "remap63" */
1910                                 drive->sect0 = 63;
1911                                 goto done;
1912                         case -14: /* "scsi" */
1913                                 drive->scsi = 1;
1914                                 goto done;
1915                         case 3: /* cyl,head,sect */
1916                                 drive->media    = ide_disk;
1917                                 drive->cyl      = drive->bios_cyl  = vals[0];
1918                                 drive->head     = drive->bios_head = vals[1];
1919                                 drive->sect     = drive->bios_sect = vals[2];
1920                                 drive->present  = 1;
1921                                 drive->forced_geom = 1;
1922                                 hwif->noprobe = 0;
1923                                 goto done;
1924                         default:
1925                                 goto bad_option;
1926                 }
1927         }
1928
1929         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1930                 goto bad_option;
1931         /*
1932          * Look for bus speed option:  "idebus="
1933          */
1934         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1935                 if (match_parm(&s[6], NULL, vals, 1) != 1)
1936                         goto bad_option;
1937                 if (vals[0] >= 20 && vals[0] <= 66) {
1938                         idebus_parameter = vals[0];
1939                 } else
1940                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1941                 goto done;
1942         }
1943         /*
1944          * Look for interface options:  "idex="
1945          */
1946         if (s[3] >= '0' && s[3] <= max_hwif) {
1947                 /*
1948                  * Be VERY CAREFUL changing this: note hardcoded indexes below
1949                  * (-8, -9, -10) are reserved to ease the hardcoding.
1950                  */
1951                 static const char *ide_words[] = {
1952                         "noprobe", "serialize", "autotune", "noautotune", 
1953                         "reset", "dma", "ata66", "minus8", "minus9",
1954                         "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1955                         "dtc2278", "umc8672", "ali14xx", NULL };
1956                 hw = s[3] - '0';
1957                 hwif = &ide_hwifs[hw];
1958                 i = match_parm(&s[4], ide_words, vals, 3);
1959
1960                 /*
1961                  * Cryptic check to ensure chipset not already set for hwif.
1962                  * Note: we can't depend on hwif->chipset here.
1963                  */
1964                 if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1965                         /* chipset already specified */
1966                         if (is_chipset_set[hw])
1967                                 goto bad_option;
1968                         if (i > -18 && i <= -11) {
1969                                 /* these drivers are for "ide0=" only */
1970                                 if (hw != 0)
1971                                         goto bad_hwif;
1972                                 /* chipset already specified for 2nd port */
1973                                 if (is_chipset_set[hw+1])
1974                                         goto bad_option;
1975                         }
1976                         is_chipset_set[hw] = 1;
1977                         printk("\n");
1978                 }
1979
1980                 switch (i) {
1981 #ifdef CONFIG_BLK_DEV_ALI14XX
1982                         case -17: /* "ali14xx" */
1983                                 probe_ali14xx = 1;
1984                                 goto done;
1985 #endif
1986 #ifdef CONFIG_BLK_DEV_UMC8672
1987                         case -16: /* "umc8672" */
1988                                 probe_umc8672 = 1;
1989                                 goto done;
1990 #endif
1991 #ifdef CONFIG_BLK_DEV_DTC2278
1992                         case -15: /* "dtc2278" */
1993                                 probe_dtc2278 = 1;
1994                                 goto done;
1995 #endif
1996 #ifdef CONFIG_BLK_DEV_CMD640
1997                         case -14: /* "cmd640_vlb" */
1998                         {
1999                                 extern int cmd640_vlb; /* flag for cmd640.c */
2000                                 cmd640_vlb = 1;
2001                                 goto done;
2002                         }
2003 #endif
2004 #ifdef CONFIG_BLK_DEV_HT6560B
2005                         case -13: /* "ht6560b" */
2006                                 probe_ht6560b = 1;
2007                                 goto done;
2008 #endif
2009 #ifdef CONFIG_BLK_DEV_QD65XX
2010                         case -12: /* "qd65xx" */
2011                                 probe_qd65xx = 1;
2012                                 goto done;
2013 #endif
2014 #ifdef CONFIG_BLK_DEV_4DRIVES
2015                         case -11: /* "four" drives on one set of ports */
2016                         {
2017                                 ide_hwif_t *mate = &ide_hwifs[hw^1];
2018                                 mate->drives[0].select.all ^= 0x20;
2019                                 mate->drives[1].select.all ^= 0x20;
2020                                 hwif->chipset = mate->chipset = ide_4drives;
2021                                 mate->irq = hwif->irq;
2022                                 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2023                                 goto do_serialize;
2024                         }
2025 #endif /* CONFIG_BLK_DEV_4DRIVES */
2026                         case -10: /* minus10 */
2027                         case -9: /* minus9 */
2028                         case -8: /* minus8 */
2029                                 goto bad_option;
2030                         case -7: /* ata66 */
2031 #ifdef CONFIG_BLK_DEV_IDEPCI
2032                                 hwif->udma_four = 1;
2033                                 goto obsolete_option;
2034 #else
2035                                 goto bad_hwif;
2036 #endif
2037                         case -6: /* dma */
2038                                 hwif->autodma = 1;
2039                                 goto obsolete_option;
2040                         case -5: /* "reset" */
2041                                 hwif->reset = 1;
2042                                 goto obsolete_option;
2043                         case -4: /* "noautotune" */
2044                                 hwif->drives[0].autotune = IDE_TUNE_NOAUTO;
2045                                 hwif->drives[1].autotune = IDE_TUNE_NOAUTO;
2046                                 goto obsolete_option;
2047                         case -3: /* "autotune" */
2048                                 hwif->drives[0].autotune = IDE_TUNE_AUTO;
2049                                 hwif->drives[1].autotune = IDE_TUNE_AUTO;
2050                                 goto obsolete_option;
2051                         case -2: /* "serialize" */
2052                         do_serialize:
2053                                 hwif->mate = &ide_hwifs[hw^1];
2054                                 hwif->mate->mate = hwif;
2055                                 hwif->serialized = hwif->mate->serialized = 1;
2056                                 goto obsolete_option;
2057
2058                         case -1: /* "noprobe" */
2059                                 hwif->noprobe = 1;
2060                                 goto done;
2061
2062                         case 1: /* base */
2063                                 vals[1] = vals[0] + 0x206; /* default ctl */
2064                         case 2: /* base,ctl */
2065                                 vals[2] = 0;    /* default irq = probe for it */
2066                         case 3: /* base,ctl,irq */
2067                                 hwif->hw.irq = vals[2];
2068                                 ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
2069                                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2070                                 hwif->irq      = vals[2];
2071                                 hwif->noprobe  = 0;
2072                                 hwif->chipset  = ide_forced;
2073                                 goto obsolete_option;
2074
2075                         case 0: goto bad_option;
2076                         default:
2077                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2078                                 return 1;
2079                 }
2080         }
2081 bad_option:
2082         printk(" -- BAD OPTION\n");
2083         return 1;
2084 obsolete_option:
2085         printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
2086         return 1;
2087 bad_hwif:
2088         printk("-- NOT SUPPORTED ON ide%d", hw);
2089 done:
2090         printk("\n");
2091         return 1;
2092 }
2093
2094 extern void pnpide_init(void);
2095 extern void h8300_ide_init(void);
2096
2097 /*
2098  * probe_for_hwifs() finds/initializes "known" IDE interfaces
2099  */
2100 static void __init probe_for_hwifs (void)
2101 {
2102 #ifdef CONFIG_BLK_DEV_IDEPCI
2103         ide_scan_pcibus(ide_scan_direction);
2104 #endif /* CONFIG_BLK_DEV_IDEPCI */
2105
2106 #ifdef CONFIG_ETRAX_IDE
2107         {
2108                 extern void init_e100_ide(void);
2109                 init_e100_ide();
2110         }
2111 #endif /* CONFIG_ETRAX_IDE */
2112 #ifdef CONFIG_BLK_DEV_CMD640
2113         {
2114                 extern void ide_probe_for_cmd640x(void);
2115                 ide_probe_for_cmd640x();
2116         }
2117 #endif /* CONFIG_BLK_DEV_CMD640 */
2118 #ifdef CONFIG_BLK_DEV_IDE_PMAC
2119         {
2120                 extern void pmac_ide_probe(void);
2121                 pmac_ide_probe();
2122         }
2123 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
2124 #ifdef CONFIG_BLK_DEV_GAYLE
2125         {
2126                 extern void gayle_init(void);
2127                 gayle_init();
2128         }
2129 #endif /* CONFIG_BLK_DEV_GAYLE */
2130 #ifdef CONFIG_BLK_DEV_FALCON_IDE
2131         {
2132                 extern void falconide_init(void);
2133                 falconide_init();
2134         }
2135 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
2136 #ifdef CONFIG_BLK_DEV_MAC_IDE
2137         {
2138                 extern void macide_init(void);
2139                 macide_init();
2140         }
2141 #endif /* CONFIG_BLK_DEV_MAC_IDE */
2142 #ifdef CONFIG_BLK_DEV_Q40IDE
2143         {
2144                 extern void q40ide_init(void);
2145                 q40ide_init();
2146         }
2147 #endif /* CONFIG_BLK_DEV_Q40IDE */
2148 #ifdef CONFIG_BLK_DEV_BUDDHA
2149         {
2150                 extern void buddha_init(void);
2151                 buddha_init();
2152         }
2153 #endif /* CONFIG_BLK_DEV_BUDDHA */
2154 #ifdef CONFIG_BLK_DEV_IDEPNP
2155         pnpide_init();
2156 #endif
2157 #ifdef CONFIG_H8300
2158         h8300_ide_init();
2159 #endif
2160 }
2161
2162 /*
2163  *      Actually unregister the subdriver. Called with the
2164  *      request lock dropped.
2165  */
2166  
2167 static int default_cleanup (ide_drive_t *drive)
2168 {
2169         return ide_unregister_subdriver(drive);
2170 }
2171
2172 static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
2173 {
2174         ide_end_request(drive, 0, 0);
2175         return ide_stopped;
2176 }
2177
2178 static int default_end_request (ide_drive_t *drive, int uptodate, int nr_sects)
2179 {
2180         return ide_end_request(drive, uptodate, nr_sects);
2181 }
2182
2183 static u8 default_sense (ide_drive_t *drive, const char *msg, u8 stat)
2184 {
2185         return ide_dump_status(drive, msg, stat);
2186 }
2187
2188 static ide_startstop_t default_error (ide_drive_t *drive, const char *msg, u8 stat)
2189 {
2190         return ide_error(drive, msg, stat);
2191 }
2192
2193 static void default_pre_reset (ide_drive_t *drive)
2194 {
2195 }
2196
2197 static sector_t default_capacity (ide_drive_t *drive)
2198 {
2199         return 0x7fffffff;
2200 }
2201
2202 static ide_startstop_t default_special (ide_drive_t *drive)
2203 {
2204         special_t *s = &drive->special;
2205
2206         s->all = 0;
2207         drive->mult_req = 0;
2208         return ide_stopped;
2209 }
2210
2211 static int default_attach (ide_drive_t *drive)
2212 {
2213         printk(KERN_ERR "%s: does not support hotswap of device class !\n",
2214                 drive->name);
2215
2216         return 0;
2217 }
2218
2219 static ide_startstop_t default_abort (ide_drive_t *drive, const char *msg)
2220 {
2221         return ide_abort(drive, msg);
2222 }
2223
2224 static ide_startstop_t default_start_power_step(ide_drive_t *drive,
2225                                                 struct request *rq)
2226 {
2227         rq->pm->pm_step = ide_pm_state_completed;
2228         return ide_stopped;
2229 }
2230
2231 static void setup_driver_defaults (ide_driver_t *d)
2232 {
2233         if (d->cleanup == NULL)         d->cleanup = default_cleanup;
2234         if (d->do_request == NULL)      d->do_request = default_do_request;
2235         if (d->end_request == NULL)     d->end_request = default_end_request;
2236         if (d->sense == NULL)           d->sense = default_sense;
2237         if (d->error == NULL)           d->error = default_error;
2238         if (d->abort == NULL)           d->abort = default_abort;
2239         if (d->pre_reset == NULL)       d->pre_reset = default_pre_reset;
2240         if (d->capacity == NULL)        d->capacity = default_capacity;
2241         if (d->special == NULL)         d->special = default_special;
2242         if (d->attach == NULL)          d->attach = default_attach;
2243         if (d->start_power_step == NULL)
2244                 d->start_power_step = default_start_power_step;
2245 }
2246
2247 int ide_register_subdriver(ide_drive_t *drive, ide_driver_t *driver)
2248 {
2249         unsigned long flags;
2250
2251         BUG_ON(!drive->driver);
2252
2253         spin_lock_irqsave(&ide_lock, flags);
2254         if (!drive->present || drive->driver != &idedefault_driver ||
2255             drive->usage || drive->dead) {
2256                 spin_unlock_irqrestore(&ide_lock, flags);
2257                 return 1;
2258         }
2259         drive->driver = driver;
2260         spin_unlock_irqrestore(&ide_lock, flags);
2261         spin_lock(&drives_lock);
2262         list_add_tail(&drive->list, &driver->drives);
2263         spin_unlock(&drives_lock);
2264 //      printk(KERN_INFO "%s: attached %s driver.\n", drive->name, driver->name);
2265         if ((drive->autotune == IDE_TUNE_DEFAULT) ||
2266                 (drive->autotune == IDE_TUNE_AUTO)) {
2267                 /* DMA timings and setup moved to ide-probe.c */
2268                 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2269                 drive->nice1 = 1;
2270         }
2271         drive->suspend_reset = 0;
2272 #ifdef CONFIG_PROC_FS
2273         if (drive->driver != &idedefault_driver) {
2274                 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2275                 ide_add_proc_entries(drive->proc, driver->proc, drive);
2276         }
2277 #endif
2278         return 0;
2279 }
2280
2281 EXPORT_SYMBOL(ide_register_subdriver);
2282
2283 /**
2284  *      ide_unregister_subdriver        -       disconnect drive from driver
2285  *      @drive: drive to unplug
2286  *
2287  *      Disconnect a drive from the driver it was attached to and then
2288  *      clean up the various proc files and other objects attached to it.
2289  *
2290  *      Takes ide_setting_sem, ide_lock and drives_lock.
2291  *      Caller must hold none of the locks.
2292  *
2293  *      No locking versus subdriver unload because we are moving to the
2294  *      default driver anyway. Wants double checking.
2295  */
2296
2297 int ide_unregister_subdriver (ide_drive_t *drive)
2298 {
2299         unsigned long flags;
2300         
2301         down(&ide_setting_sem);
2302         spin_lock_irqsave(&ide_lock, flags);
2303         if (drive->usage || drive->driver == &idedefault_driver || DRIVER(drive)->busy) {
2304                 spin_unlock_irqrestore(&ide_lock, flags);
2305                 up(&ide_setting_sem);
2306                 return 1;
2307         }
2308 #ifdef CONFIG_PROC_FS
2309         ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2310         ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2311 #endif
2312         auto_remove_settings(drive);
2313         drive->driver = &idedefault_driver;
2314         spin_unlock_irqrestore(&ide_lock, flags);
2315         up(&ide_setting_sem);
2316         spin_lock(&drives_lock);
2317         list_del_init(&drive->list);
2318         spin_unlock(&drives_lock);
2319         /* drive will be added to &idedefault_driver->drives in ata_attach() */
2320         return 0;
2321 }
2322
2323 EXPORT_SYMBOL(ide_unregister_subdriver);
2324
2325 static int ide_drive_remove(struct device * dev)
2326 {
2327         ide_drive_t * drive = container_of(dev,ide_drive_t,gendev);
2328         DRIVER(drive)->cleanup(drive);
2329         return 0;
2330 }
2331
2332 /**
2333  *      ide_register_driver     -       register IDE device driver
2334  *      @driver: the IDE device driver
2335  *
2336  *      Register a new device driver and then scan the devices
2337  *      on the IDE bus in case any should be attached to the
2338  *      driver we have just registered.  If so attach them.
2339  *
2340  *      Takes drivers_lock and drives_lock.
2341  */
2342
2343 int ide_register_driver(ide_driver_t *driver)
2344 {
2345         struct list_head list;
2346         struct list_head *list_loop;
2347         struct list_head *tmp_storage;
2348
2349         setup_driver_defaults(driver);
2350
2351         spin_lock(&drivers_lock);
2352         list_add(&driver->drivers, &drivers);
2353         spin_unlock(&drivers_lock);
2354
2355         INIT_LIST_HEAD(&list);
2356         spin_lock(&drives_lock);
2357         list_splice_init(&idedefault_driver.drives, &list);
2358         spin_unlock(&drives_lock);
2359
2360         list_for_each_safe(list_loop, tmp_storage, &list) {
2361                 ide_drive_t *drive = container_of(list_loop, ide_drive_t, list);
2362                 list_del_init(&drive->list);
2363                 if (drive->present)
2364                         ata_attach(drive);
2365         }
2366         driver->gen_driver.name = (char *) driver->name;
2367         driver->gen_driver.bus = &ide_bus_type;
2368         driver->gen_driver.remove = ide_drive_remove;
2369         return driver_register(&driver->gen_driver);
2370 }
2371
2372 EXPORT_SYMBOL(ide_register_driver);
2373
2374 /**
2375  *      ide_unregister_driver   -       unregister IDE device driver
2376  *      @driver: the IDE device driver
2377  *
2378  *      Called when a driver module is being unloaded. We reattach any
2379  *      devices to whatever driver claims them next (typically the default
2380  *      driver).
2381  *
2382  *      Takes drivers_lock and called functions will take ide_setting_sem.
2383  */
2384
2385 void ide_unregister_driver(ide_driver_t *driver)
2386 {
2387         ide_drive_t *drive;
2388
2389         spin_lock(&drivers_lock);
2390         list_del(&driver->drivers);
2391         spin_unlock(&drivers_lock);
2392
2393         driver_unregister(&driver->gen_driver);
2394
2395         while(!list_empty(&driver->drives)) {
2396                 drive = list_entry(driver->drives.next, ide_drive_t, list);
2397                 if (driver->cleanup(drive)) {
2398                         printk(KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
2399                         BUG();
2400                 }
2401                 ata_attach(drive);
2402         }
2403 }
2404
2405 EXPORT_SYMBOL(ide_unregister_driver);
2406
2407 struct block_device_operations ide_fops[] = {{
2408         .owner          = THIS_MODULE,
2409         .open           = ide_open,
2410 }};
2411
2412 EXPORT_SYMBOL(ide_fops);
2413
2414 /*
2415  * Probe module
2416  */
2417
2418 EXPORT_SYMBOL(ide_lock);
2419
2420 struct bus_type ide_bus_type = {
2421         .name           = "ide",
2422         .suspend        = generic_ide_suspend,
2423         .resume         = generic_ide_resume,
2424 };
2425
2426 /*
2427  * This is gets invoked once during initialization, to set *everything* up
2428  */
2429 int __init ide_init (void)
2430 {
2431         printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
2432         devfs_mk_dir("ide");
2433         system_bus_speed = ide_system_bus_speed();
2434
2435         bus_register(&ide_bus_type);
2436
2437         init_ide_data();
2438
2439 #ifdef CONFIG_PROC_FS
2440         proc_ide_root = proc_mkdir("ide", NULL);
2441 #endif
2442
2443 #ifdef CONFIG_BLK_DEV_ALI14XX
2444         if (probe_ali14xx)
2445                 (void)ali14xx_init();
2446 #endif
2447 #ifdef CONFIG_BLK_DEV_UMC8672
2448         if (probe_umc8672)
2449                 (void)umc8672_init();
2450 #endif
2451 #ifdef CONFIG_BLK_DEV_DTC2278
2452         if (probe_dtc2278)
2453                 (void)dtc2278_init();
2454 #endif
2455 #ifdef CONFIG_BLK_DEV_HT6560B
2456         if (probe_ht6560b)
2457                 (void)ht6560b_init();
2458 #endif
2459 #ifdef CONFIG_BLK_DEV_QD65XX
2460         if (probe_qd65xx)
2461                 (void)qd65xx_init();
2462 #endif
2463
2464         initializing = 1;
2465         /* Probe for special PCI and other "known" interface chipsets. */
2466         probe_for_hwifs();
2467         initializing = 0;
2468
2469 #ifdef CONFIG_PROC_FS
2470         proc_ide_create();
2471 #endif
2472         return 0;
2473 }
2474
2475 #ifdef MODULE
2476 char *options = NULL;
2477 module_param(options, charp, 0);
2478 MODULE_LICENSE("GPL");
2479
2480 static void __init parse_options (char *line)
2481 {
2482         char *next = line;
2483
2484         if (line == NULL || !*line)
2485                 return;
2486         while ((line = next) != NULL) {
2487                 if ((next = strchr(line,' ')) != NULL)
2488                         *next++ = 0;
2489                 if (!ide_setup(line))
2490                         printk (KERN_INFO "Unknown option '%s'\n", line);
2491         }
2492 }
2493
2494 int init_module (void)
2495 {
2496         parse_options(options);
2497         return ide_init();
2498 }
2499
2500 void cleanup_module (void)
2501 {
2502         int index;
2503
2504         for (index = 0; index < MAX_HWIFS; ++index) {
2505                 ide_unregister(index);
2506                 if (ide_hwifs[index].dma_base)
2507                         (void) ide_release_dma(&ide_hwifs[index]);
2508         }
2509
2510 #ifdef CONFIG_PROC_FS
2511         proc_ide_destroy();
2512 #endif
2513         devfs_remove("ide");
2514
2515         bus_unregister(&ide_bus_type);
2516 }
2517
2518 #else /* !MODULE */
2519
2520 __setup("", ide_setup);
2521
2522 module_init(ide_init);
2523
2524 #endif /* MODULE */