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