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