]> Pileus Git - ~andy/linux/blob - drivers/ide/ide.c
92ab39d5bc25759dc42096fc5de1d46a0acd08c7
[~andy/linux] / 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/module.h>
134 #include <linux/types.h>
135 #include <linux/string.h>
136 #include <linux/kernel.h>
137 #include <linux/timer.h>
138 #include <linux/mm.h>
139 #include <linux/interrupt.h>
140 #include <linux/major.h>
141 #include <linux/errno.h>
142 #include <linux/genhd.h>
143 #include <linux/blkpg.h>
144 #include <linux/slab.h>
145 #include <linux/init.h>
146 #include <linux/pci.h>
147 #include <linux/delay.h>
148 #include <linux/ide.h>
149 #include <linux/completion.h>
150 #include <linux/reboot.h>
151 #include <linux/cdrom.h>
152 #include <linux/seq_file.h>
153 #include <linux/device.h>
154 #include <linux/bitops.h>
155
156 #include <asm/byteorder.h>
157 #include <asm/irq.h>
158 #include <asm/uaccess.h>
159 #include <asm/io.h>
160
161
162 /* default maximum number of failures */
163 #define IDE_DEFAULT_MAX_FAILURES        1
164
165 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
166                                         IDE2_MAJOR, IDE3_MAJOR,
167                                         IDE4_MAJOR, IDE5_MAJOR,
168                                         IDE6_MAJOR, IDE7_MAJOR,
169                                         IDE8_MAJOR, IDE9_MAJOR };
170
171 static int idebus_parameter;    /* holds the "idebus=" parameter */
172 static int system_bus_speed;    /* holds what we think is VESA/PCI bus speed */
173 static int initializing;        /* set while initializing built-in drivers */
174
175 DECLARE_MUTEX(ide_cfg_sem);
176  __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
177
178 #ifdef CONFIG_BLK_DEV_IDEPCI
179 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
180 #endif
181
182 #ifdef CONFIG_IDEDMA_AUTO
183 int noautodma = 0;
184 #else
185 int noautodma = 1;
186 #endif
187
188 EXPORT_SYMBOL(noautodma);
189
190 #ifdef CONFIG_BLK_DEV_IDEACPI
191 int ide_noacpi = 0;
192 int ide_noacpitfs = 1;
193 int ide_noacpionboot = 1;
194 #endif
195
196 /*
197  * This is declared extern in ide.h, for access by other IDE modules:
198  */
199 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
200
201 EXPORT_SYMBOL(ide_hwifs);
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
210         /* bulk initialize hwif & drive info with zeros */
211         memset(hwif, 0, sizeof(ide_hwif_t));
212
213         /* fill in any non-zero initial values */
214         hwif->index     = index;
215         hwif->major     = ide_hwif_to_major[index];
216
217         hwif->name[0]   = 'i';
218         hwif->name[1]   = 'd';
219         hwif->name[2]   = 'e';
220         hwif->name[3]   = '0' + index;
221
222         hwif->bus_state = BUSSTATE_ON;
223
224         hwif->atapi_dma = 0;            /* disable all atapi dma */ 
225         hwif->ultra_mask = 0x80;        /* disable all ultra */
226         hwif->mwdma_mask = 0x80;        /* disable all mwdma */
227         hwif->swdma_mask = 0x80;        /* disable all swdma */
228
229         init_completion(&hwif->gendev_rel_comp);
230
231         default_hwif_iops(hwif);
232         default_hwif_transport(hwif);
233         for (unit = 0; unit < MAX_DRIVES; ++unit) {
234                 ide_drive_t *drive = &hwif->drives[unit];
235
236                 drive->media                    = ide_disk;
237                 drive->select.all               = (unit<<4)|0xa0;
238                 drive->hwif                     = hwif;
239                 drive->ctl                      = 0x08;
240                 drive->ready_stat               = READY_STAT;
241                 drive->bad_wstat                = BAD_W_STAT;
242                 drive->special.b.recalibrate    = 1;
243                 drive->special.b.set_geometry   = 1;
244                 drive->name[0]                  = 'h';
245                 drive->name[1]                  = 'd';
246                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
247                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
248                 drive->using_dma                = 0;
249                 drive->vdma                     = 0;
250                 INIT_LIST_HEAD(&drive->list);
251                 init_completion(&drive->gendev_rel_comp);
252         }
253 }
254
255 static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
256 {
257         hw_regs_t hw;
258
259         memset(&hw, 0, sizeof(hw_regs_t));
260
261         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
262
263         memcpy(&hwif->hw, &hw, sizeof(hw));
264         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
265
266         hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
267 #ifdef CONFIG_BLK_DEV_HD
268         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
269                 hwif->noprobe = 1;      /* may be overridden by ide_setup() */
270 #endif
271 }
272
273 extern void ide_arm_init(void);
274
275 /*
276  * init_ide_data() sets reasonable default values into all fields
277  * of all instances of the hwifs and drives, but only on the first call.
278  * Subsequent calls have no effect (they don't wipe out anything).
279  *
280  * This routine is normally called at driver initialization time,
281  * but may also be called MUCH earlier during kernel "command-line"
282  * parameter processing.  As such, we cannot depend on any other parts
283  * of the kernel (such as memory allocation) to be functioning yet.
284  *
285  * This is too bad, as otherwise we could dynamically allocate the
286  * ide_drive_t structs as needed, rather than always consuming memory
287  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
288  *
289  * FIXME: We should stuff the setup data into __init and copy the
290  * relevant hwifs/allocate them properly during boot.
291  */
292 #define MAGIC_COOKIE 0x12345678
293 static void __init init_ide_data (void)
294 {
295         ide_hwif_t *hwif;
296         unsigned int index;
297         static unsigned long magic_cookie = MAGIC_COOKIE;
298
299         if (magic_cookie != MAGIC_COOKIE)
300                 return;         /* already initialized */
301         magic_cookie = 0;
302
303         /* Initialise all interface structures */
304         for (index = 0; index < MAX_HWIFS; ++index) {
305                 hwif = &ide_hwifs[index];
306                 init_hwif_data(hwif, index);
307                 init_hwif_default(hwif, index);
308 #if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
309                 hwif->irq = hwif->hw.irq =
310                         ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
311 #endif
312         }
313 #ifdef CONFIG_IDE_ARM
314         initializing = 1;
315         ide_arm_init();
316         initializing = 0;
317 #endif
318 }
319
320 /**
321  *      ide_system_bus_speed    -       guess bus speed
322  *
323  *      ide_system_bus_speed() returns what we think is the system VESA/PCI
324  *      bus speed (in MHz). This is used for calculating interface PIO timings.
325  *      The default is 40 for known PCI systems, 50 otherwise.
326  *      The "idebus=xx" parameter can be used to override this value.
327  *      The actual value to be used is computed/displayed the first time
328  *      through. Drivers should only use this as a last resort.
329  *
330  *      Returns a guessed speed in MHz.
331  */
332
333 static int ide_system_bus_speed(void)
334 {
335 #ifdef CONFIG_PCI
336         static struct pci_device_id pci_default[] = {
337                 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
338                 { }
339         };
340 #else
341 #define pci_default 0
342 #endif /* CONFIG_PCI */
343
344         if (!system_bus_speed) {
345                 if (idebus_parameter) {
346                         /* user supplied value */
347                         system_bus_speed = idebus_parameter;
348                 } else if (pci_dev_present(pci_default)) {
349                         /* safe default value for PCI */
350                         system_bus_speed = 33;
351                 } else {
352                         /* safe default value for VESA and PCI */
353                         system_bus_speed = 50;
354                 }
355                 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
356                         "for PIO modes%s\n", system_bus_speed,
357                         idebus_parameter ? "" : "; override with idebus=xx");
358         }
359         return system_bus_speed;
360 }
361
362 #ifdef CONFIG_PROC_FS
363 struct proc_dir_entry *proc_ide_root;
364 #endif
365
366 static struct resource* hwif_request_region(ide_hwif_t *hwif,
367                                             unsigned long addr, int num)
368 {
369         struct resource *res = request_region(addr, num, hwif->name);
370
371         if (!res)
372                 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
373                                 hwif->name, addr, addr+num-1);
374         return res;
375 }
376
377 /**
378  *      ide_hwif_request_regions - request resources for IDE
379  *      @hwif: interface to use
380  *
381  *      Requests all the needed resources for an interface.
382  *      Right now core IDE code does this work which is deeply wrong.
383  *      MMIO leaves it to the controller driver,
384  *      PIO will migrate this way over time.
385  */
386
387 int ide_hwif_request_regions(ide_hwif_t *hwif)
388 {
389         unsigned long addr;
390         unsigned int i;
391
392         if (hwif->mmio == 2)
393                 return 0;
394         BUG_ON(hwif->mmio == 1);
395         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
396         if (addr && !hwif_request_region(hwif, addr, 1))
397                 goto control_region_busy;
398         hwif->straight8 = 0;
399         addr = hwif->io_ports[IDE_DATA_OFFSET];
400         if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
401                 if (!hwif_request_region(hwif, addr, 8))
402                         goto data_region_busy;
403                 hwif->straight8 = 1;
404                 return 0;
405         }
406         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
407                 addr = hwif->io_ports[i];
408                 if (!hwif_request_region(hwif, addr, 1)) {
409                         while (--i)
410                                 release_region(addr, 1);
411                         goto data_region_busy;
412                 }
413         }
414         return 0;
415
416 data_region_busy:
417         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
418         if (addr)
419                 release_region(addr, 1);
420 control_region_busy:
421         /* If any errors are return, we drop the hwif interface. */
422         return -EBUSY;
423 }
424
425 /**
426  *      ide_hwif_release_regions - free IDE resources
427  *
428  *      Note that we only release the standard ports,
429  *      and do not even try to handle any extra ports
430  *      allocated for weird IDE interface chipsets.
431  *
432  *      Note also that we don't yet handle mmio resources here. More
433  *      importantly our caller should be doing this so we need to 
434  *      restructure this as a helper function for drivers.
435  */
436
437 void ide_hwif_release_regions(ide_hwif_t *hwif)
438 {
439         u32 i = 0;
440
441         if (hwif->mmio == 2)
442                 return;
443         if (hwif->io_ports[IDE_CONTROL_OFFSET])
444                 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
445         if (hwif->straight8) {
446                 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
447                 return;
448         }
449         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
450                 if (hwif->io_ports[i])
451                         release_region(hwif->io_ports[i], 1);
452 }
453
454 /**
455  *      ide_hwif_restore        -       restore hwif to template
456  *      @hwif: hwif to update
457  *      @tmp_hwif: template
458  *
459  *      Restore hwif to a previous state by copying most settings
460  *      from the template.
461  */
462
463 static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
464 {
465         hwif->hwgroup                   = tmp_hwif->hwgroup;
466
467         hwif->gendev.parent             = tmp_hwif->gendev.parent;
468
469         hwif->proc                      = tmp_hwif->proc;
470
471         hwif->major                     = tmp_hwif->major;
472         hwif->straight8                 = tmp_hwif->straight8;
473         hwif->bus_state                 = tmp_hwif->bus_state;
474
475         hwif->atapi_dma                 = tmp_hwif->atapi_dma;
476         hwif->ultra_mask                = tmp_hwif->ultra_mask;
477         hwif->mwdma_mask                = tmp_hwif->mwdma_mask;
478         hwif->swdma_mask                = tmp_hwif->swdma_mask;
479
480         hwif->chipset                   = tmp_hwif->chipset;
481         hwif->hold                      = tmp_hwif->hold;
482
483 #ifdef CONFIG_BLK_DEV_IDEPCI
484         hwif->pci_dev                   = tmp_hwif->pci_dev;
485         hwif->cds                       = tmp_hwif->cds;
486 #endif
487
488         hwif->tuneproc                  = tmp_hwif->tuneproc;
489         hwif->speedproc                 = tmp_hwif->speedproc;
490         hwif->selectproc                = tmp_hwif->selectproc;
491         hwif->reset_poll                = tmp_hwif->reset_poll;
492         hwif->pre_reset                 = tmp_hwif->pre_reset;
493         hwif->resetproc                 = tmp_hwif->resetproc;
494         hwif->intrproc                  = tmp_hwif->intrproc;
495         hwif->maskproc                  = tmp_hwif->maskproc;
496         hwif->quirkproc                 = tmp_hwif->quirkproc;
497         hwif->busproc                   = tmp_hwif->busproc;
498
499         hwif->ata_input_data            = tmp_hwif->ata_input_data;
500         hwif->ata_output_data           = tmp_hwif->ata_output_data;
501         hwif->atapi_input_bytes         = tmp_hwif->atapi_input_bytes;
502         hwif->atapi_output_bytes        = tmp_hwif->atapi_output_bytes;
503
504         hwif->dma_setup                 = tmp_hwif->dma_setup;
505         hwif->dma_exec_cmd              = tmp_hwif->dma_exec_cmd;
506         hwif->dma_start                 = tmp_hwif->dma_start;
507         hwif->ide_dma_end               = tmp_hwif->ide_dma_end;
508         hwif->ide_dma_check             = tmp_hwif->ide_dma_check;
509         hwif->ide_dma_on                = tmp_hwif->ide_dma_on;
510         hwif->ide_dma_off_quietly       = tmp_hwif->ide_dma_off_quietly;
511         hwif->ide_dma_test_irq          = tmp_hwif->ide_dma_test_irq;
512         hwif->ide_dma_clear_irq         = tmp_hwif->ide_dma_clear_irq;
513         hwif->ide_dma_host_on           = tmp_hwif->ide_dma_host_on;
514         hwif->ide_dma_host_off          = tmp_hwif->ide_dma_host_off;
515         hwif->ide_dma_lostirq           = tmp_hwif->ide_dma_lostirq;
516         hwif->ide_dma_timeout           = tmp_hwif->ide_dma_timeout;
517
518         hwif->OUTB                      = tmp_hwif->OUTB;
519         hwif->OUTBSYNC                  = tmp_hwif->OUTBSYNC;
520         hwif->OUTW                      = tmp_hwif->OUTW;
521         hwif->OUTSW                     = tmp_hwif->OUTSW;
522         hwif->OUTSL                     = tmp_hwif->OUTSL;
523
524         hwif->INB                       = tmp_hwif->INB;
525         hwif->INW                       = tmp_hwif->INW;
526         hwif->INSW                      = tmp_hwif->INSW;
527         hwif->INSL                      = tmp_hwif->INSL;
528
529         hwif->sg_max_nents              = tmp_hwif->sg_max_nents;
530
531         hwif->mmio                      = tmp_hwif->mmio;
532         hwif->rqsize                    = tmp_hwif->rqsize;
533         hwif->no_lba48                  = tmp_hwif->no_lba48;
534
535 #ifndef CONFIG_BLK_DEV_IDECS
536         hwif->irq                       = tmp_hwif->irq;
537 #endif
538
539         hwif->dma_base                  = tmp_hwif->dma_base;
540         hwif->dma_master                = tmp_hwif->dma_master;
541         hwif->dma_command               = tmp_hwif->dma_command;
542         hwif->dma_vendor1               = tmp_hwif->dma_vendor1;
543         hwif->dma_status                = tmp_hwif->dma_status;
544         hwif->dma_vendor3               = tmp_hwif->dma_vendor3;
545         hwif->dma_prdtable              = tmp_hwif->dma_prdtable;
546
547         hwif->config_data               = tmp_hwif->config_data;
548         hwif->select_data               = tmp_hwif->select_data;
549         hwif->extra_base                = tmp_hwif->extra_base;
550         hwif->extra_ports               = tmp_hwif->extra_ports;
551         hwif->autodma                   = tmp_hwif->autodma;
552         hwif->udma_four                 = tmp_hwif->udma_four;
553
554         hwif->hwif_data                 = tmp_hwif->hwif_data;
555 }
556
557 /**
558  *      ide_unregister          -       free an IDE interface
559  *      @index: index of interface (will change soon to a pointer)
560  *
561  *      Perform the final unregister of an IDE interface. At the moment
562  *      we don't refcount interfaces so this will also get split up.
563  *
564  *      Locking:
565  *      The caller must not hold the IDE locks
566  *      The drive present/vanishing is not yet properly locked
567  *      Take care with the callbacks. These have been split to avoid
568  *      deadlocking the IDE layer. The shutdown callback is called
569  *      before we take the lock and free resources. It is up to the
570  *      caller to be sure there is no pending I/O here, and that
571  *      the interface will not be reopened (present/vanishing locking
572  *      isn't yet done BTW). After we commit to the final kill we
573  *      call the cleanup callback with the ide locks held.
574  *
575  *      Unregister restores the hwif structures to the default state.
576  *      This is raving bonkers.
577  */
578
579 void ide_unregister(unsigned int index)
580 {
581         ide_drive_t *drive;
582         ide_hwif_t *hwif, *g;
583         static ide_hwif_t tmp_hwif; /* protected by ide_cfg_sem */
584         ide_hwgroup_t *hwgroup;
585         int irq_count = 0, unit;
586
587         BUG_ON(index >= MAX_HWIFS);
588
589         BUG_ON(in_interrupt());
590         BUG_ON(irqs_disabled());
591         down(&ide_cfg_sem);
592         spin_lock_irq(&ide_lock);
593         hwif = &ide_hwifs[index];
594         if (!hwif->present)
595                 goto abort;
596         for (unit = 0; unit < MAX_DRIVES; ++unit) {
597                 drive = &hwif->drives[unit];
598                 if (!drive->present)
599                         continue;
600                 spin_unlock_irq(&ide_lock);
601                 device_unregister(&drive->gendev);
602                 wait_for_completion(&drive->gendev_rel_comp);
603                 spin_lock_irq(&ide_lock);
604         }
605         hwif->present = 0;
606
607         spin_unlock_irq(&ide_lock);
608
609         destroy_proc_ide_interface(hwif);
610
611         hwgroup = hwif->hwgroup;
612         /*
613          * free the irq if we were the only hwif using it
614          */
615         g = hwgroup->hwif;
616         do {
617                 if (g->irq == hwif->irq)
618                         ++irq_count;
619                 g = g->next;
620         } while (g != hwgroup->hwif);
621         if (irq_count == 1)
622                 free_irq(hwif->irq, hwgroup);
623
624         spin_lock_irq(&ide_lock);
625         /*
626          * Note that we only release the standard ports,
627          * and do not even try to handle any extra ports
628          * allocated for weird IDE interface chipsets.
629          */
630         ide_hwif_release_regions(hwif);
631
632         /*
633          * Remove us from the hwgroup, and free
634          * the hwgroup if we were the only member
635          */
636         if (hwif->next == hwif) {
637                 BUG_ON(hwgroup->hwif != hwif);
638                 kfree(hwgroup);
639         } else {
640                 /* There is another interface in hwgroup.
641                  * Unlink us, and set hwgroup->drive and ->hwif to
642                  * something sane.
643                  */
644                 g = hwgroup->hwif;
645                 while (g->next != hwif)
646                         g = g->next;
647                 g->next = hwif->next;
648                 if (hwgroup->hwif == hwif) {
649                         /* Chose a random hwif for hwgroup->hwif.
650                          * It's guaranteed that there are no drives
651                          * left in the hwgroup.
652                          */
653                         BUG_ON(hwgroup->drive != NULL);
654                         hwgroup->hwif = g;
655                 }
656                 BUG_ON(hwgroup->hwif == hwif);
657         }
658
659         /* More messed up locking ... */
660         spin_unlock_irq(&ide_lock);
661         device_unregister(&hwif->gendev);
662         wait_for_completion(&hwif->gendev_rel_comp);
663
664         /*
665          * Remove us from the kernel's knowledge
666          */
667         blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
668         kfree(hwif->sg_table);
669         unregister_blkdev(hwif->major, hwif->name);
670         spin_lock_irq(&ide_lock);
671
672         if (hwif->dma_base) {
673                 (void) ide_release_dma(hwif);
674
675                 hwif->dma_base = 0;
676                 hwif->dma_master = 0;
677                 hwif->dma_command = 0;
678                 hwif->dma_vendor1 = 0;
679                 hwif->dma_status = 0;
680                 hwif->dma_vendor3 = 0;
681                 hwif->dma_prdtable = 0;
682
683                 hwif->extra_base  = 0;
684                 hwif->extra_ports = 0;
685         }
686
687         /* copy original settings */
688         tmp_hwif = *hwif;
689
690         /* restore hwif data to pristine status */
691         init_hwif_data(hwif, index);
692         init_hwif_default(hwif, index);
693
694         ide_hwif_restore(hwif, &tmp_hwif);
695
696 abort:
697         spin_unlock_irq(&ide_lock);
698         up(&ide_cfg_sem);
699 }
700
701 EXPORT_SYMBOL(ide_unregister);
702
703
704 /**
705  *      ide_setup_ports         -       set up IDE interface ports
706  *      @hw: register descriptions
707  *      @base: base register
708  *      @offsets: table of register offsets
709  *      @ctrl: control register
710  *      @ack_irq: IRQ ack
711  *      @irq: interrupt lie
712  *
713  *      Setup hw_regs_t structure described by parameters.  You
714  *      may set up the hw structure yourself OR use this routine to
715  *      do it for you. This is basically a helper
716  *
717  */
718  
719 void ide_setup_ports (  hw_regs_t *hw,
720                         unsigned long base, int *offsets,
721                         unsigned long ctrl, unsigned long intr,
722                         ide_ack_intr_t *ack_intr,
723 /*
724  *                      ide_io_ops_t *iops,
725  */
726                         int irq)
727 {
728         int i;
729
730         memset(hw, 0, sizeof(hw_regs_t));
731         for (i = 0; i < IDE_NR_PORTS; i++) {
732                 if (offsets[i] == -1) {
733                         switch(i) {
734                                 case IDE_CONTROL_OFFSET:
735                                         hw->io_ports[i] = ctrl;
736                                         break;
737 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
738                                 case IDE_IRQ_OFFSET:
739                                         hw->io_ports[i] = intr;
740                                         break;
741 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
742                                 default:
743                                         hw->io_ports[i] = 0;
744                                         break;
745                         }
746                 } else {
747                         hw->io_ports[i] = base + offsets[i];
748                 }
749         }
750         hw->irq = irq;
751         hw->dma = NO_DMA;
752         hw->ack_intr = ack_intr;
753 /*
754  *      hw->iops = iops;
755  */
756 }
757
758 /**
759  *      ide_register_hw_with_fixup      -       register IDE interface
760  *      @hw: hardware registers
761  *      @hwifp: pointer to returned hwif
762  *      @fixup: fixup function
763  *
764  *      Register an IDE interface, specifying exactly the registers etc.
765  *      Set init=1 iff calling before probes have taken place.
766  *
767  *      Returns -1 on error.
768  */
769
770 int ide_register_hw_with_fixup(hw_regs_t *hw, ide_hwif_t **hwifp, void(*fixup)(ide_hwif_t *hwif))
771 {
772         int index, retry = 1;
773         ide_hwif_t *hwif;
774
775         do {
776                 for (index = 0; index < MAX_HWIFS; ++index) {
777                         hwif = &ide_hwifs[index];
778                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
779                                 goto found;
780                 }
781                 for (index = 0; index < MAX_HWIFS; ++index) {
782                         hwif = &ide_hwifs[index];
783                         if (hwif->hold)
784                                 continue;
785                         if ((!hwif->present && !hwif->mate && !initializing) ||
786                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
787                                 goto found;
788                 }
789                 for (index = 0; index < MAX_HWIFS; index++)
790                         ide_unregister(index);
791         } while (retry--);
792         return -1;
793 found:
794         if (hwif->present)
795                 ide_unregister(index);
796         else if (!hwif->hold) {
797                 init_hwif_data(hwif, index);
798                 init_hwif_default(hwif, index);
799         }
800         if (hwif->present)
801                 return -1;
802         memcpy(&hwif->hw, hw, sizeof(*hw));
803         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
804         hwif->irq = hw->irq;
805         hwif->noprobe = 0;
806         hwif->chipset = hw->chipset;
807         hwif->gendev.parent = hw->dev;
808
809         if (!initializing) {
810                 probe_hwif_init_with_fixup(hwif, fixup);
811                 create_proc_ide_interfaces();
812         }
813
814         if (hwifp)
815                 *hwifp = hwif;
816
817         return (initializing || hwif->present) ? index : -1;
818 }
819
820 EXPORT_SYMBOL(ide_register_hw_with_fixup);
821
822 int ide_register_hw(hw_regs_t *hw, ide_hwif_t **hwifp)
823 {
824         return ide_register_hw_with_fixup(hw, hwifp, NULL);
825 }
826
827 EXPORT_SYMBOL(ide_register_hw);
828
829 /*
830  *      Locks for IDE setting functionality
831  */
832
833 DECLARE_MUTEX(ide_setting_sem);
834
835 /**
836  *      __ide_add_setting       -       add an ide setting option
837  *      @drive: drive to use
838  *      @name: setting name
839  *      @rw: true if the function is read write
840  *      @read_ioctl: function to call on read
841  *      @write_ioctl: function to call on write
842  *      @data_type: type of data
843  *      @min: range minimum
844  *      @max: range maximum
845  *      @mul_factor: multiplication scale
846  *      @div_factor: divison scale
847  *      @data: private data field
848  *      @set: setting
849  *      @auto_remove: setting auto removal flag
850  *
851  *      Removes the setting named from the device if it is present.
852  *      The function takes the settings_lock to protect against 
853  *      parallel changes. This function must not be called from IRQ
854  *      context. Returns 0 on success or -1 on failure.
855  *
856  *      BUGS: This code is seriously over-engineered. There is also
857  *      magic about how the driver specific features are setup. If
858  *      a driver is attached we assume the driver settings are auto
859  *      remove.
860  */
861
862 static 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, int auto_remove)
863 {
864         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
865
866         down(&ide_setting_sem);
867         while ((*p) && strcmp((*p)->name, name) < 0)
868                 p = &((*p)->next);
869         if ((setting = kzalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
870                 goto abort;
871         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
872                 goto abort;
873         strcpy(setting->name, name);
874         setting->rw = rw;
875         setting->read_ioctl = read_ioctl;
876         setting->write_ioctl = write_ioctl;
877         setting->data_type = data_type;
878         setting->min = min;
879         setting->max = max;
880         setting->mul_factor = mul_factor;
881         setting->div_factor = div_factor;
882         setting->data = data;
883         setting->set = set;
884         
885         setting->next = *p;
886         if (auto_remove)
887                 setting->auto_remove = 1;
888         *p = setting;
889         up(&ide_setting_sem);
890         return 0;
891 abort:
892         up(&ide_setting_sem);
893         kfree(setting);
894         return -1;
895 }
896
897 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)
898 {
899         return __ide_add_setting(drive, name, rw, read_ioctl, write_ioctl, data_type, min, max, mul_factor, div_factor, data, set, 1);
900 }
901
902 EXPORT_SYMBOL(ide_add_setting);
903
904 /**
905  *      __ide_remove_setting    -       remove an ide setting option
906  *      @drive: drive to use
907  *      @name: setting name
908  *
909  *      Removes the setting named from the device if it is present.
910  *      The caller must hold the setting semaphore.
911  */
912  
913 static void __ide_remove_setting (ide_drive_t *drive, char *name)
914 {
915         ide_settings_t **p, *setting;
916
917         p = (ide_settings_t **) &drive->settings;
918
919         while ((*p) && strcmp((*p)->name, name))
920                 p = &((*p)->next);
921         if ((setting = (*p)) == NULL)
922                 return;
923
924         (*p) = setting->next;
925         
926         kfree(setting->name);
927         kfree(setting);
928 }
929
930 /**
931  *      ide_find_setting_by_ioctl       -       find a drive specific ioctl
932  *      @drive: drive to scan
933  *      @cmd: ioctl command to handle
934  *
935  *      Scan's the device setting table for a matching entry and returns
936  *      this or NULL if no entry is found. The caller must hold the
937  *      setting semaphore
938  */
939  
940 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
941 {
942         ide_settings_t *setting = drive->settings;
943
944         while (setting) {
945                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
946                         break;
947                 setting = setting->next;
948         }
949         
950         return setting;
951 }
952
953 /**
954  *      ide_find_setting_by_name        -       find a drive specific setting
955  *      @drive: drive to scan
956  *      @name: setting name
957  *
958  *      Scan's the device setting table for a matching entry and returns
959  *      this or NULL if no entry is found. The caller must hold the
960  *      setting semaphore
961  */
962  
963 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
964 {
965         ide_settings_t *setting = drive->settings;
966
967         while (setting) {
968                 if (strcmp(setting->name, name) == 0)
969                         break;
970                 setting = setting->next;
971         }
972         return setting;
973 }
974
975 /**
976  *      auto_remove_settings    -       remove driver specific settings
977  *      @drive: drive
978  *
979  *      Automatically remove all the driver specific settings for this
980  *      drive. This function may not be called from IRQ context. The
981  *      caller must hold ide_setting_sem.
982  */
983  
984 static void auto_remove_settings (ide_drive_t *drive)
985 {
986         ide_settings_t *setting;
987 repeat:
988         setting = drive->settings;
989         while (setting) {
990                 if (setting->auto_remove) {
991                         __ide_remove_setting(drive, setting->name);
992                         goto repeat;
993                 }
994                 setting = setting->next;
995         }
996 }
997
998 /**
999  *      ide_read_setting        -       read an IDE setting
1000  *      @drive: drive to read from
1001  *      @setting: drive setting
1002  *
1003  *      Read a drive setting and return the value. The caller
1004  *      must hold the ide_setting_sem when making this call.
1005  *
1006  *      BUGS: the data return and error are the same return value
1007  *      so an error -EINVAL and true return of the same value cannot
1008  *      be told apart
1009  */
1010  
1011 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1012 {
1013         int             val = -EINVAL;
1014         unsigned long   flags;
1015
1016         if ((setting->rw & SETTING_READ)) {
1017                 spin_lock_irqsave(&ide_lock, flags);
1018                 switch(setting->data_type) {
1019                         case TYPE_BYTE:
1020                                 val = *((u8 *) setting->data);
1021                                 break;
1022                         case TYPE_SHORT:
1023                                 val = *((u16 *) setting->data);
1024                                 break;
1025                         case TYPE_INT:
1026                         case TYPE_INTA:
1027                                 val = *((u32 *) setting->data);
1028                                 break;
1029                 }
1030                 spin_unlock_irqrestore(&ide_lock, flags);
1031         }
1032         return val;
1033 }
1034
1035 /**
1036  *      ide_spin_wait_hwgroup   -       wait for group
1037  *      @drive: drive in the group
1038  *
1039  *      Wait for an IDE device group to go non busy and then return
1040  *      holding the ide_lock which guards the hwgroup->busy status
1041  *      and right to use it.
1042  */
1043
1044 int ide_spin_wait_hwgroup (ide_drive_t *drive)
1045 {
1046         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1047         unsigned long timeout = jiffies + (3 * HZ);
1048
1049         spin_lock_irq(&ide_lock);
1050
1051         while (hwgroup->busy) {
1052                 unsigned long lflags;
1053                 spin_unlock_irq(&ide_lock);
1054                 local_irq_set(lflags);
1055                 if (time_after(jiffies, timeout)) {
1056                         local_irq_restore(lflags);
1057                         printk(KERN_ERR "%s: channel busy\n", drive->name);
1058                         return -EBUSY;
1059                 }
1060                 local_irq_restore(lflags);
1061                 spin_lock_irq(&ide_lock);
1062         }
1063         return 0;
1064 }
1065
1066 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1067
1068 /**
1069  *      ide_write_setting       -       read an IDE setting
1070  *      @drive: drive to read from
1071  *      @setting: drive setting
1072  *      @val: value
1073  *
1074  *      Write a drive setting if it is possible. The caller
1075  *      must hold the ide_setting_sem when making this call.
1076  *
1077  *      BUGS: the data return and error are the same return value
1078  *      so an error -EINVAL and true return of the same value cannot
1079  *      be told apart
1080  *
1081  *      FIXME:  This should be changed to enqueue a special request
1082  *      to the driver to change settings, and then wait on a sema for completion.
1083  *      The current scheme of polling is kludgy, though safe enough.
1084  */
1085
1086 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1087 {
1088         int i;
1089         u32 *p;
1090
1091         if (!capable(CAP_SYS_ADMIN))
1092                 return -EACCES;
1093         if (!(setting->rw & SETTING_WRITE))
1094                 return -EPERM;
1095         if (val < setting->min || val > setting->max)
1096                 return -EINVAL;
1097         if (setting->set)
1098                 return setting->set(drive, val);
1099         if (ide_spin_wait_hwgroup(drive))
1100                 return -EBUSY;
1101         switch (setting->data_type) {
1102                 case TYPE_BYTE:
1103                         *((u8 *) setting->data) = val;
1104                         break;
1105                 case TYPE_SHORT:
1106                         *((u16 *) setting->data) = val;
1107                         break;
1108                 case TYPE_INT:
1109                         *((u32 *) setting->data) = val;
1110                         break;
1111                 case TYPE_INTA:
1112                         p = (u32 *) setting->data;
1113                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1114                                 *p = val;
1115                         break;
1116         }
1117         spin_unlock_irq(&ide_lock);
1118         return 0;
1119 }
1120
1121 static int set_io_32bit(ide_drive_t *drive, int arg)
1122 {
1123         drive->io_32bit = arg;
1124 #ifdef CONFIG_BLK_DEV_DTC2278
1125         if (HWIF(drive)->chipset == ide_dtc2278)
1126                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1127 #endif /* CONFIG_BLK_DEV_DTC2278 */
1128         return 0;
1129 }
1130
1131 static int set_using_dma (ide_drive_t *drive, int arg)
1132 {
1133 #ifdef CONFIG_BLK_DEV_IDEDMA
1134         if (!drive->id || !(drive->id->capability & 1))
1135                 return -EPERM;
1136         if (HWIF(drive)->ide_dma_check == NULL)
1137                 return -EPERM;
1138         if (arg) {
1139                 if (HWIF(drive)->ide_dma_check(drive)) return -EIO;
1140                 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1141         } else {
1142                 if (__ide_dma_off(drive))
1143                         return -EIO;
1144         }
1145         return 0;
1146 #else
1147         return -EPERM;
1148 #endif
1149 }
1150
1151 static int set_pio_mode (ide_drive_t *drive, int arg)
1152 {
1153         struct request rq;
1154
1155         if (!HWIF(drive)->tuneproc)
1156                 return -ENOSYS;
1157         if (drive->special.b.set_tune)
1158                 return -EBUSY;
1159         ide_init_drive_cmd(&rq);
1160         drive->tune_req = (u8) arg;
1161         drive->special.b.set_tune = 1;
1162         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1163         return 0;
1164 }
1165
1166 static int set_xfer_rate (ide_drive_t *drive, int arg)
1167 {
1168         int err = ide_wait_cmd(drive,
1169                         WIN_SETFEATURES, (u8) arg,
1170                         SETFEATURES_XFER, 0, NULL);
1171
1172         if (!err && arg) {
1173                 ide_set_xfer_rate(drive, (u8) arg);
1174                 ide_driveid_update(drive);
1175         }
1176         return err;
1177 }
1178
1179 /**
1180  *      ide_add_generic_settings        -       generic ide settings
1181  *      @drive: drive being configured
1182  *
1183  *      Add the generic parts of the system settings to the /proc files and
1184  *      ioctls for this IDE device. The caller must not be holding the
1185  *      ide_setting_sem.
1186  */
1187
1188 void ide_add_generic_settings (ide_drive_t *drive)
1189 {
1190 /*
1191  *                        drive         setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function
1192  */
1193         __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,   0);
1194         __ide_add_setting(drive,        "keepsettings",         SETTING_RW,                                     HDIO_GET_KEEPSETTINGS,  HDIO_SET_KEEPSETTINGS,  TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL,           0);
1195         __ide_add_setting(drive,        "nice1",                SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL,           0);
1196         __ide_add_setting(drive,        "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode,   0);
1197         __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,           0);
1198         __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,  0);
1199         __ide_add_setting(drive,        "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->init_speed,             NULL,           0);
1200         __ide_add_setting(drive,        "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->current_speed,          set_xfer_rate,  0);
1201         __ide_add_setting(drive,        "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL,           0);
1202 }
1203
1204 /**
1205  *      system_bus_clock        -       clock guess
1206  *
1207  *      External version of the bus clock guess used by very old IDE drivers
1208  *      for things like VLB timings. Should not be used.
1209  */
1210
1211 int system_bus_clock (void)
1212 {
1213         return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1214 }
1215
1216 EXPORT_SYMBOL(system_bus_clock);
1217
1218 static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
1219 {
1220         ide_drive_t *drive = dev->driver_data;
1221         ide_hwif_t *hwif = HWIF(drive);
1222         struct request rq;
1223         struct request_pm_state rqpm;
1224         ide_task_t args;
1225
1226         /* Call ACPI _GTM only once */
1227         if (!(drive->dn % 2))
1228                 ide_acpi_get_timing(hwif);
1229
1230         memset(&rq, 0, sizeof(rq));
1231         memset(&rqpm, 0, sizeof(rqpm));
1232         memset(&args, 0, sizeof(args));
1233         rq.cmd_type = REQ_TYPE_PM_SUSPEND;
1234         rq.special = &args;
1235         rq.data = &rqpm;
1236         rqpm.pm_step = ide_pm_state_start_suspend;
1237         if (mesg.event == PM_EVENT_PRETHAW)
1238                 mesg.event = PM_EVENT_FREEZE;
1239         rqpm.pm_state = mesg.event;
1240
1241         return ide_do_drive_cmd(drive, &rq, ide_wait);
1242 }
1243
1244 static int generic_ide_resume(struct device *dev)
1245 {
1246         ide_drive_t *drive = dev->driver_data;
1247         ide_hwif_t *hwif = HWIF(drive);
1248         struct request rq;
1249         struct request_pm_state rqpm;
1250         ide_task_t args;
1251
1252         /* Call ACPI _STM only once */
1253         if (!(drive->dn % 2))
1254                 ide_acpi_push_timing(hwif);
1255
1256         ide_acpi_exec_tfs(drive);
1257
1258         memset(&rq, 0, sizeof(rq));
1259         memset(&rqpm, 0, sizeof(rqpm));
1260         memset(&args, 0, sizeof(args));
1261         rq.cmd_type = REQ_TYPE_PM_RESUME;
1262         rq.special = &args;
1263         rq.data = &rqpm;
1264         rqpm.pm_step = ide_pm_state_start_resume;
1265         rqpm.pm_state = PM_EVENT_ON;
1266
1267         return ide_do_drive_cmd(drive, &rq, ide_head_wait);
1268 }
1269
1270 int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
1271                         unsigned int cmd, unsigned long arg)
1272 {
1273         ide_settings_t *setting;
1274         ide_driver_t *drv;
1275         int err = 0;
1276         void __user *p = (void __user *)arg;
1277
1278         down(&ide_setting_sem);
1279         if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1280                 if (cmd == setting->read_ioctl) {
1281                         err = ide_read_setting(drive, setting);
1282                         up(&ide_setting_sem);
1283                         return err >= 0 ? put_user(err, (long __user *)arg) : err;
1284                 } else {
1285                         if (bdev != bdev->bd_contains)
1286                                 err = -EINVAL;
1287                         else
1288                                 err = ide_write_setting(drive, setting, arg);
1289                         up(&ide_setting_sem);
1290                         return err;
1291                 }
1292         }
1293         up(&ide_setting_sem);
1294
1295         switch (cmd) {
1296                 case HDIO_OBSOLETE_IDENTITY:
1297                 case HDIO_GET_IDENTITY:
1298                         if (bdev != bdev->bd_contains)
1299                                 return -EINVAL;
1300                         if (drive->id_read == 0)
1301                                 return -ENOMSG;
1302                         if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1303                                 return -EFAULT;
1304                         return 0;
1305
1306                 case HDIO_GET_NICE:
1307                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
1308                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
1309                                         drive->nice0            <<      IDE_NICE_0              |
1310                                         drive->nice1            <<      IDE_NICE_1              |
1311                                         drive->nice2            <<      IDE_NICE_2,
1312                                         (long __user *) arg);
1313
1314 #ifdef CONFIG_IDE_TASK_IOCTL
1315                 case HDIO_DRIVE_TASKFILE:
1316                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1317                                 return -EACCES;
1318                         switch(drive->media) {
1319                                 case ide_disk:
1320                                         return ide_taskfile_ioctl(drive, cmd, arg);
1321                                 default:
1322                                         return -ENOMSG;
1323                         }
1324 #endif /* CONFIG_IDE_TASK_IOCTL */
1325
1326                 case HDIO_DRIVE_CMD:
1327                         if (!capable(CAP_SYS_RAWIO))
1328                                 return -EACCES;
1329                         return ide_cmd_ioctl(drive, cmd, arg);
1330
1331                 case HDIO_DRIVE_TASK:
1332                         if (!capable(CAP_SYS_RAWIO))
1333                                 return -EACCES;
1334                         return ide_task_ioctl(drive, cmd, arg);
1335
1336                 case HDIO_SCAN_HWIF:
1337                 {
1338                         hw_regs_t hw;
1339                         int args[3];
1340                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1341                         if (copy_from_user(args, p, 3 * sizeof(int)))
1342                                 return -EFAULT;
1343                         memset(&hw, 0, sizeof(hw));
1344                         ide_init_hwif_ports(&hw, (unsigned long) args[0],
1345                                             (unsigned long) args[1], NULL);
1346                         hw.irq = args[2];
1347                         if (ide_register_hw(&hw, NULL) == -1)
1348                                 return -EIO;
1349                         return 0;
1350                 }
1351                 case HDIO_UNREGISTER_HWIF:
1352                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1353                         /* (arg > MAX_HWIFS) checked in function */
1354                         ide_unregister(arg);
1355                         return 0;
1356                 case HDIO_SET_NICE:
1357                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1358                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1359                                 return -EPERM;
1360                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1361                         drv = *(ide_driver_t **)bdev->bd_disk->private_data;
1362                         if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
1363                                 drive->dsc_overlap = 0;
1364                                 return -EPERM;
1365                         }
1366                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
1367                         return 0;
1368                 case HDIO_DRIVE_RESET:
1369                 {
1370                         unsigned long flags;
1371                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1372                         
1373                         /*
1374                          *      Abort the current command on the
1375                          *      group if there is one, taking
1376                          *      care not to allow anything else
1377                          *      to be queued and to die on the
1378                          *      spot if we miss one somehow
1379                          */
1380
1381                         spin_lock_irqsave(&ide_lock, flags);
1382
1383                         if (HWGROUP(drive)->resetting) {
1384                                 spin_unlock_irqrestore(&ide_lock, flags);
1385                                 return -EBUSY;
1386                         }
1387
1388                         ide_abort(drive, "drive reset");
1389
1390                         BUG_ON(HWGROUP(drive)->handler);
1391                                 
1392                         /* Ensure nothing gets queued after we
1393                            drop the lock. Reset will clear the busy */
1394                    
1395                         HWGROUP(drive)->busy = 1;
1396                         spin_unlock_irqrestore(&ide_lock, flags);
1397                         (void) ide_do_reset(drive);
1398
1399                         return 0;
1400                 }
1401
1402                 case CDROMEJECT:
1403                 case CDROMCLOSETRAY:
1404                         return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
1405
1406                 case HDIO_GET_BUSSTATE:
1407                         if (!capable(CAP_SYS_ADMIN))
1408                                 return -EACCES;
1409                         if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1410                                 return -EFAULT;
1411                         return 0;
1412
1413                 case HDIO_SET_BUSSTATE:
1414                         if (!capable(CAP_SYS_ADMIN))
1415                                 return -EACCES;
1416                         if (HWIF(drive)->busproc)
1417                                 return HWIF(drive)->busproc(drive, (int)arg);
1418                         return -EOPNOTSUPP;
1419                 default:
1420                         return -EINVAL;
1421         }
1422 }
1423
1424 EXPORT_SYMBOL(generic_ide_ioctl);
1425
1426 /*
1427  * stridx() returns the offset of c within s,
1428  * or -1 if c is '\0' or not found within s.
1429  */
1430 static int __init stridx (const char *s, char c)
1431 {
1432         char *i = strchr(s, c);
1433         return (i && c) ? i - s : -1;
1434 }
1435
1436 /*
1437  * match_parm() does parsing for ide_setup():
1438  *
1439  * 1. the first char of s must be '='.
1440  * 2. if the remainder matches one of the supplied keywords,
1441  *     the index (1 based) of the keyword is negated and returned.
1442  * 3. if the remainder is a series of no more than max_vals numbers
1443  *     separated by commas, the numbers are saved in vals[] and a
1444  *     count of how many were saved is returned.  Base10 is assumed,
1445  *     and base16 is allowed when prefixed with "0x".
1446  * 4. otherwise, zero is returned.
1447  */
1448 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1449 {
1450         static const char *decimal = "0123456789";
1451         static const char *hex = "0123456789abcdef";
1452         int i, n;
1453
1454         if (*s++ == '=') {
1455                 /*
1456                  * Try matching against the supplied keywords,
1457                  * and return -(index+1) if we match one
1458                  */
1459                 if (keywords != NULL) {
1460                         for (i = 0; *keywords != NULL; ++i) {
1461                                 if (!strcmp(s, *keywords++))
1462                                         return -(i+1);
1463                         }
1464                 }
1465                 /*
1466                  * Look for a series of no more than "max_vals"
1467                  * numeric values separated by commas, in base10,
1468                  * or base16 when prefixed with "0x".
1469                  * Return a count of how many were found.
1470                  */
1471                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1472                         vals[n] = i;
1473                         while ((i = stridx(decimal, *++s)) >= 0)
1474                                 vals[n] = (vals[n] * 10) + i;
1475                         if (*s == 'x' && !vals[n]) {
1476                                 while ((i = stridx(hex, *++s)) >= 0)
1477                                         vals[n] = (vals[n] * 0x10) + i;
1478                         }
1479                         if (++n == max_vals)
1480                                 break;
1481                         if (*s == ',' || *s == ';')
1482                                 ++s;
1483                 }
1484                 if (!*s)
1485                         return n;
1486         }
1487         return 0;       /* zero = nothing matched */
1488 }
1489
1490 #ifdef CONFIG_BLK_DEV_ALI14XX
1491 static int __initdata probe_ali14xx;
1492 extern int ali14xx_init(void);
1493 #endif
1494 #ifdef CONFIG_BLK_DEV_UMC8672
1495 static int __initdata probe_umc8672;
1496 extern int umc8672_init(void);
1497 #endif
1498 #ifdef CONFIG_BLK_DEV_DTC2278
1499 static int __initdata probe_dtc2278;
1500 extern int dtc2278_init(void);
1501 #endif
1502 #ifdef CONFIG_BLK_DEV_HT6560B
1503 static int __initdata probe_ht6560b;
1504 extern int ht6560b_init(void);
1505 #endif
1506 #ifdef CONFIG_BLK_DEV_QD65XX
1507 static int __initdata probe_qd65xx;
1508 extern int qd65xx_init(void);
1509 #endif
1510
1511 static int __initdata is_chipset_set[MAX_HWIFS];
1512
1513 /*
1514  * ide_setup() gets called VERY EARLY during initialization,
1515  * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1516  *
1517  * Remember to update Documentation/ide.txt if you change something here.
1518  */
1519 static int __init ide_setup(char *s)
1520 {
1521         int i, vals[3];
1522         ide_hwif_t *hwif;
1523         ide_drive_t *drive;
1524         unsigned int hw, unit;
1525         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1526         const char max_hwif  = '0' + (MAX_HWIFS - 1);
1527
1528         
1529         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
1530                 return 0;                               /* driver and not us */
1531
1532         if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1533                 return 0;
1534
1535         printk(KERN_INFO "ide_setup: %s", s);
1536         init_ide_data ();
1537
1538 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1539         if (!strcmp(s, "ide=doubler")) {
1540                 extern int ide_doubler;
1541
1542                 printk(" : Enabled support for IDE doublers\n");
1543                 ide_doubler = 1;
1544                 return 1;
1545         }
1546 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1547
1548         if (!strcmp(s, "ide=nodma")) {
1549                 printk(" : Prevented DMA\n");
1550                 noautodma = 1;
1551                 return 1;
1552         }
1553
1554 #ifdef CONFIG_BLK_DEV_IDEPCI
1555         if (!strcmp(s, "ide=reverse")) {
1556                 ide_scan_direction = 1;
1557                 printk(" : Enabled support for IDE inverse scan order.\n");
1558                 return 1;
1559         }
1560 #endif /* CONFIG_BLK_DEV_IDEPCI */
1561
1562 #ifdef CONFIG_BLK_DEV_IDEACPI
1563         if (!strcmp(s, "ide=noacpi")) {
1564                 //printk(" : Disable IDE ACPI support.\n");
1565                 ide_noacpi = 1;
1566                 return 1;
1567         }
1568         if (!strcmp(s, "ide=acpigtf")) {
1569                 //printk(" : Enable IDE ACPI _GTF support.\n");
1570                 ide_noacpitfs = 0;
1571                 return 1;
1572         }
1573         if (!strcmp(s, "ide=acpionboot")) {
1574                 //printk(" : Call IDE ACPI methods on boot.\n");
1575                 ide_noacpionboot = 0;
1576                 return 1;
1577         }
1578 #endif /* CONFIG_BLK_DEV_IDEACPI */
1579
1580         /*
1581          * Look for drive options:  "hdx="
1582          */
1583         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1584                 const char *hd_words[] = {
1585                         "none", "noprobe", "nowerr", "cdrom", "serialize",
1586                         "autotune", "noautotune", "minus8", "swapdata", "bswap",
1587                         "noflush", "remap", "remap63", "scsi", NULL };
1588                 unit = s[2] - 'a';
1589                 hw   = unit / MAX_DRIVES;
1590                 unit = unit % MAX_DRIVES;
1591                 hwif = &ide_hwifs[hw];
1592                 drive = &hwif->drives[unit];
1593                 if (strncmp(s + 4, "ide-", 4) == 0) {
1594                         strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1595                         goto done;
1596                 }
1597                 switch (match_parm(&s[3], hd_words, vals, 3)) {
1598                         case -1: /* "none" */
1599                         case -2: /* "noprobe" */
1600                                 drive->noprobe = 1;
1601                                 goto done;
1602                         case -3: /* "nowerr" */
1603                                 drive->bad_wstat = BAD_R_STAT;
1604                                 hwif->noprobe = 0;
1605                                 goto done;
1606                         case -4: /* "cdrom" */
1607                                 drive->present = 1;
1608                                 drive->media = ide_cdrom;
1609                                 /* an ATAPI device ignores DRDY */
1610                                 drive->ready_stat = 0;
1611                                 hwif->noprobe = 0;
1612                                 goto done;
1613                         case -5: /* "serialize" */
1614                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
1615                                 goto do_serialize;
1616                         case -6: /* "autotune" */
1617                                 drive->autotune = IDE_TUNE_AUTO;
1618                                 goto obsolete_option;
1619                         case -7: /* "noautotune" */
1620                                 drive->autotune = IDE_TUNE_NOAUTO;
1621                                 goto obsolete_option;
1622                         case -9: /* "swapdata" */
1623                         case -10: /* "bswap" */
1624                                 drive->bswap = 1;
1625                                 goto done;
1626                         case -11: /* noflush */
1627                                 drive->noflush = 1;
1628                                 goto done;
1629                         case -12: /* "remap" */
1630                                 drive->remap_0_to_1 = 1;
1631                                 goto done;
1632                         case -13: /* "remap63" */
1633                                 drive->sect0 = 63;
1634                                 goto done;
1635                         case -14: /* "scsi" */
1636                                 drive->scsi = 1;
1637                                 goto done;
1638                         case 3: /* cyl,head,sect */
1639                                 drive->media    = ide_disk;
1640                                 drive->ready_stat = READY_STAT;
1641                                 drive->cyl      = drive->bios_cyl  = vals[0];
1642                                 drive->head     = drive->bios_head = vals[1];
1643                                 drive->sect     = drive->bios_sect = vals[2];
1644                                 drive->present  = 1;
1645                                 drive->forced_geom = 1;
1646                                 hwif->noprobe = 0;
1647                                 goto done;
1648                         default:
1649                                 goto bad_option;
1650                 }
1651         }
1652
1653         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1654                 goto bad_option;
1655         /*
1656          * Look for bus speed option:  "idebus="
1657          */
1658         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1659                 if (match_parm(&s[6], NULL, vals, 1) != 1)
1660                         goto bad_option;
1661                 if (vals[0] >= 20 && vals[0] <= 66) {
1662                         idebus_parameter = vals[0];
1663                 } else
1664                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1665                 goto done;
1666         }
1667         /*
1668          * Look for interface options:  "idex="
1669          */
1670         if (s[3] >= '0' && s[3] <= max_hwif) {
1671                 /*
1672                  * Be VERY CAREFUL changing this: note hardcoded indexes below
1673                  * (-8, -9, -10) are reserved to ease the hardcoding.
1674                  */
1675                 static const char *ide_words[] = {
1676                         "noprobe", "serialize", "autotune", "noautotune", 
1677                         "reset", "dma", "ata66", "minus8", "minus9",
1678                         "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1679                         "dtc2278", "umc8672", "ali14xx", NULL };
1680                 hw = s[3] - '0';
1681                 hwif = &ide_hwifs[hw];
1682                 i = match_parm(&s[4], ide_words, vals, 3);
1683
1684                 /*
1685                  * Cryptic check to ensure chipset not already set for hwif.
1686                  * Note: we can't depend on hwif->chipset here.
1687                  */
1688                 if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1689                         /* chipset already specified */
1690                         if (is_chipset_set[hw])
1691                                 goto bad_option;
1692                         if (i > -18 && i <= -11) {
1693                                 /* these drivers are for "ide0=" only */
1694                                 if (hw != 0)
1695                                         goto bad_hwif;
1696                                 /* chipset already specified for 2nd port */
1697                                 if (is_chipset_set[hw+1])
1698                                         goto bad_option;
1699                         }
1700                         is_chipset_set[hw] = 1;
1701                         printk("\n");
1702                 }
1703
1704                 switch (i) {
1705 #ifdef CONFIG_BLK_DEV_ALI14XX
1706                         case -17: /* "ali14xx" */
1707                                 probe_ali14xx = 1;
1708                                 goto done;
1709 #endif
1710 #ifdef CONFIG_BLK_DEV_UMC8672
1711                         case -16: /* "umc8672" */
1712                                 probe_umc8672 = 1;
1713                                 goto done;
1714 #endif
1715 #ifdef CONFIG_BLK_DEV_DTC2278
1716                         case -15: /* "dtc2278" */
1717                                 probe_dtc2278 = 1;
1718                                 goto done;
1719 #endif
1720 #ifdef CONFIG_BLK_DEV_CMD640
1721                         case -14: /* "cmd640_vlb" */
1722                         {
1723                                 extern int cmd640_vlb; /* flag for cmd640.c */
1724                                 cmd640_vlb = 1;
1725                                 goto done;
1726                         }
1727 #endif
1728 #ifdef CONFIG_BLK_DEV_HT6560B
1729                         case -13: /* "ht6560b" */
1730                                 probe_ht6560b = 1;
1731                                 goto done;
1732 #endif
1733 #ifdef CONFIG_BLK_DEV_QD65XX
1734                         case -12: /* "qd65xx" */
1735                                 probe_qd65xx = 1;
1736                                 goto done;
1737 #endif
1738 #ifdef CONFIG_BLK_DEV_4DRIVES
1739                         case -11: /* "four" drives on one set of ports */
1740                         {
1741                                 ide_hwif_t *mate = &ide_hwifs[hw^1];
1742                                 mate->drives[0].select.all ^= 0x20;
1743                                 mate->drives[1].select.all ^= 0x20;
1744                                 hwif->chipset = mate->chipset = ide_4drives;
1745                                 mate->irq = hwif->irq;
1746                                 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
1747                                 goto do_serialize;
1748                         }
1749 #endif /* CONFIG_BLK_DEV_4DRIVES */
1750                         case -10: /* minus10 */
1751                         case -9: /* minus9 */
1752                         case -8: /* minus8 */
1753                                 goto bad_option;
1754                         case -7: /* ata66 */
1755 #ifdef CONFIG_BLK_DEV_IDEPCI
1756                                 hwif->udma_four = 1;
1757                                 goto obsolete_option;
1758 #else
1759                                 goto bad_hwif;
1760 #endif
1761                         case -6: /* dma */
1762                                 hwif->autodma = 1;
1763                                 goto obsolete_option;
1764                         case -5: /* "reset" */
1765                                 hwif->reset = 1;
1766                                 goto obsolete_option;
1767                         case -4: /* "noautotune" */
1768                                 hwif->drives[0].autotune = IDE_TUNE_NOAUTO;
1769                                 hwif->drives[1].autotune = IDE_TUNE_NOAUTO;
1770                                 goto obsolete_option;
1771                         case -3: /* "autotune" */
1772                                 hwif->drives[0].autotune = IDE_TUNE_AUTO;
1773                                 hwif->drives[1].autotune = IDE_TUNE_AUTO;
1774                                 goto obsolete_option;
1775                         case -2: /* "serialize" */
1776                         do_serialize:
1777                                 hwif->mate = &ide_hwifs[hw^1];
1778                                 hwif->mate->mate = hwif;
1779                                 hwif->serialized = hwif->mate->serialized = 1;
1780                                 goto obsolete_option;
1781
1782                         case -1: /* "noprobe" */
1783                                 hwif->noprobe = 1;
1784                                 goto done;
1785
1786                         case 1: /* base */
1787                                 vals[1] = vals[0] + 0x206; /* default ctl */
1788                         case 2: /* base,ctl */
1789                                 vals[2] = 0;    /* default irq = probe for it */
1790                         case 3: /* base,ctl,irq */
1791                                 hwif->hw.irq = vals[2];
1792                                 ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
1793                                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
1794                                 hwif->irq      = vals[2];
1795                                 hwif->noprobe  = 0;
1796                                 hwif->chipset  = ide_forced;
1797                                 goto obsolete_option;
1798
1799                         case 0: goto bad_option;
1800                         default:
1801                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1802                                 return 1;
1803                 }
1804         }
1805 bad_option:
1806         printk(" -- BAD OPTION\n");
1807         return 1;
1808 obsolete_option:
1809         printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1810         return 1;
1811 bad_hwif:
1812         printk("-- NOT SUPPORTED ON ide%d", hw);
1813 done:
1814         printk("\n");
1815         return 1;
1816 }
1817
1818 extern void __init pnpide_init(void);
1819 extern void __exit pnpide_exit(void);
1820 extern void __init h8300_ide_init(void);
1821
1822 /*
1823  * probe_for_hwifs() finds/initializes "known" IDE interfaces
1824  */
1825 static void __init probe_for_hwifs (void)
1826 {
1827 #ifdef CONFIG_BLK_DEV_IDEPCI
1828         ide_scan_pcibus(ide_scan_direction);
1829 #endif /* CONFIG_BLK_DEV_IDEPCI */
1830
1831 #ifdef CONFIG_ETRAX_IDE
1832         {
1833                 extern void init_e100_ide(void);
1834                 init_e100_ide();
1835         }
1836 #endif /* CONFIG_ETRAX_IDE */
1837 #ifdef CONFIG_BLK_DEV_CMD640
1838         {
1839                 extern void ide_probe_for_cmd640x(void);
1840                 ide_probe_for_cmd640x();
1841         }
1842 #endif /* CONFIG_BLK_DEV_CMD640 */
1843 #ifdef CONFIG_BLK_DEV_IDE_PMAC
1844         {
1845                 extern void pmac_ide_probe(void);
1846                 pmac_ide_probe();
1847         }
1848 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
1849 #ifdef CONFIG_BLK_DEV_GAYLE
1850         {
1851                 extern void gayle_init(void);
1852                 gayle_init();
1853         }
1854 #endif /* CONFIG_BLK_DEV_GAYLE */
1855 #ifdef CONFIG_BLK_DEV_FALCON_IDE
1856         {
1857                 extern void falconide_init(void);
1858                 falconide_init();
1859         }
1860 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
1861 #ifdef CONFIG_BLK_DEV_MAC_IDE
1862         {
1863                 extern void macide_init(void);
1864                 macide_init();
1865         }
1866 #endif /* CONFIG_BLK_DEV_MAC_IDE */
1867 #ifdef CONFIG_BLK_DEV_Q40IDE
1868         {
1869                 extern void q40ide_init(void);
1870                 q40ide_init();
1871         }
1872 #endif /* CONFIG_BLK_DEV_Q40IDE */
1873 #ifdef CONFIG_BLK_DEV_BUDDHA
1874         {
1875                 extern void buddha_init(void);
1876                 buddha_init();
1877         }
1878 #endif /* CONFIG_BLK_DEV_BUDDHA */
1879 #ifdef CONFIG_BLK_DEV_IDEPNP
1880         pnpide_init();
1881 #endif
1882 #ifdef CONFIG_H8300
1883         h8300_ide_init();
1884 #endif
1885 }
1886
1887 void ide_register_subdriver(ide_drive_t *drive, ide_driver_t *driver)
1888 {
1889 #ifdef CONFIG_PROC_FS
1890         ide_add_proc_entries(drive->proc, driver->proc, drive);
1891 #endif
1892 }
1893
1894 EXPORT_SYMBOL(ide_register_subdriver);
1895
1896 /**
1897  *      ide_unregister_subdriver        -       disconnect drive from driver
1898  *      @drive: drive to unplug
1899  *      @driver: driver
1900  *
1901  *      Disconnect a drive from the driver it was attached to and then
1902  *      clean up the various proc files and other objects attached to it.
1903  *
1904  *      Takes ide_setting_sem and ide_lock.
1905  *      Caller must hold none of the locks.
1906  */
1907
1908 void ide_unregister_subdriver(ide_drive_t *drive, ide_driver_t *driver)
1909 {
1910         unsigned long flags;
1911         
1912 #ifdef CONFIG_PROC_FS
1913         ide_remove_proc_entries(drive->proc, driver->proc);
1914 #endif
1915         down(&ide_setting_sem);
1916         spin_lock_irqsave(&ide_lock, flags);
1917         /*
1918          * ide_setting_sem protects the settings list
1919          * ide_lock protects the use of settings
1920          *
1921          * so we need to hold both, ide_settings_sem because we want to
1922          * modify the settings list, and ide_lock because we cannot take
1923          * a setting out that is being used.
1924          *
1925          * OTOH both ide_{read,write}_setting are only ever used under
1926          * ide_setting_sem.
1927          */
1928         auto_remove_settings(drive);
1929         spin_unlock_irqrestore(&ide_lock, flags);
1930         up(&ide_setting_sem);
1931 }
1932
1933 EXPORT_SYMBOL(ide_unregister_subdriver);
1934
1935 /*
1936  * Probe module
1937  */
1938
1939 EXPORT_SYMBOL(ide_lock);
1940
1941 static int ide_bus_match(struct device *dev, struct device_driver *drv)
1942 {
1943         return 1;
1944 }
1945
1946 static char *media_string(ide_drive_t *drive)
1947 {
1948         switch (drive->media) {
1949         case ide_disk:
1950                 return "disk";
1951         case ide_cdrom:
1952                 return "cdrom";
1953         case ide_tape:
1954                 return "tape";
1955         case ide_floppy:
1956                 return "floppy";
1957         default:
1958                 return "UNKNOWN";
1959         }
1960 }
1961
1962 static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1963 {
1964         ide_drive_t *drive = to_ide_device(dev);
1965         return sprintf(buf, "%s\n", media_string(drive));
1966 }
1967
1968 static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1969 {
1970         ide_drive_t *drive = to_ide_device(dev);
1971         return sprintf(buf, "%s\n", drive->name);
1972 }
1973
1974 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1975 {
1976         ide_drive_t *drive = to_ide_device(dev);
1977         return sprintf(buf, "ide:m-%s\n", media_string(drive));
1978 }
1979
1980 static struct device_attribute ide_dev_attrs[] = {
1981         __ATTR_RO(media),
1982         __ATTR_RO(drivename),
1983         __ATTR_RO(modalias),
1984         __ATTR_NULL
1985 };
1986
1987 static int ide_uevent(struct device *dev, char **envp, int num_envp,
1988                       char *buffer, int buffer_size)
1989 {
1990         ide_drive_t *drive = to_ide_device(dev);
1991         int i = 0;
1992         int length = 0;
1993
1994         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1995                        "MEDIA=%s", media_string(drive));
1996         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1997                        "DRIVENAME=%s", drive->name);
1998         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1999                        "MODALIAS=ide:m-%s", media_string(drive));
2000         envp[i] = NULL;
2001         return 0;
2002 }
2003
2004 static int generic_ide_probe(struct device *dev)
2005 {
2006         ide_drive_t *drive = to_ide_device(dev);
2007         ide_driver_t *drv = to_ide_driver(dev->driver);
2008
2009         return drv->probe ? drv->probe(drive) : -ENODEV;
2010 }
2011
2012 static int generic_ide_remove(struct device *dev)
2013 {
2014         ide_drive_t *drive = to_ide_device(dev);
2015         ide_driver_t *drv = to_ide_driver(dev->driver);
2016
2017         if (drv->remove)
2018                 drv->remove(drive);
2019
2020         return 0;
2021 }
2022
2023 static void generic_ide_shutdown(struct device *dev)
2024 {
2025         ide_drive_t *drive = to_ide_device(dev);
2026         ide_driver_t *drv = to_ide_driver(dev->driver);
2027
2028         if (dev->driver && drv->shutdown)
2029                 drv->shutdown(drive);
2030 }
2031
2032 struct bus_type ide_bus_type = {
2033         .name           = "ide",
2034         .match          = ide_bus_match,
2035         .uevent         = ide_uevent,
2036         .probe          = generic_ide_probe,
2037         .remove         = generic_ide_remove,
2038         .shutdown       = generic_ide_shutdown,
2039         .dev_attrs      = ide_dev_attrs,
2040         .suspend        = generic_ide_suspend,
2041         .resume         = generic_ide_resume,
2042 };
2043
2044 EXPORT_SYMBOL_GPL(ide_bus_type);
2045
2046 /*
2047  * This is gets invoked once during initialization, to set *everything* up
2048  */
2049 static int __init ide_init(void)
2050 {
2051         int ret;
2052
2053         printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
2054         system_bus_speed = ide_system_bus_speed();
2055
2056         ret = bus_register(&ide_bus_type);
2057         if (ret < 0) {
2058                 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
2059                 return ret;
2060         }
2061
2062         init_ide_data();
2063
2064 #ifdef CONFIG_PROC_FS
2065         proc_ide_root = proc_mkdir("ide", NULL);
2066 #endif
2067
2068 #ifdef CONFIG_BLK_DEV_ALI14XX
2069         if (probe_ali14xx)
2070                 (void)ali14xx_init();
2071 #endif
2072 #ifdef CONFIG_BLK_DEV_UMC8672
2073         if (probe_umc8672)
2074                 (void)umc8672_init();
2075 #endif
2076 #ifdef CONFIG_BLK_DEV_DTC2278
2077         if (probe_dtc2278)
2078                 (void)dtc2278_init();
2079 #endif
2080 #ifdef CONFIG_BLK_DEV_HT6560B
2081         if (probe_ht6560b)
2082                 (void)ht6560b_init();
2083 #endif
2084 #ifdef CONFIG_BLK_DEV_QD65XX
2085         if (probe_qd65xx)
2086                 (void)qd65xx_init();
2087 #endif
2088
2089         initializing = 1;
2090         /* Probe for special PCI and other "known" interface chipsets. */
2091         probe_for_hwifs();
2092         initializing = 0;
2093
2094 #ifdef CONFIG_PROC_FS
2095         proc_ide_create();
2096 #endif
2097         return 0;
2098 }
2099
2100 #ifdef MODULE
2101 static char *options = NULL;
2102 module_param(options, charp, 0);
2103 MODULE_LICENSE("GPL");
2104
2105 static void __init parse_options (char *line)
2106 {
2107         char *next = line;
2108
2109         if (line == NULL || !*line)
2110                 return;
2111         while ((line = next) != NULL) {
2112                 if ((next = strchr(line,' ')) != NULL)
2113                         *next++ = 0;
2114                 if (!ide_setup(line))
2115                         printk (KERN_INFO "Unknown option '%s'\n", line);
2116         }
2117 }
2118
2119 int __init init_module (void)
2120 {
2121         parse_options(options);
2122         return ide_init();
2123 }
2124
2125 void __exit cleanup_module (void)
2126 {
2127         int index;
2128
2129         for (index = 0; index < MAX_HWIFS; ++index)
2130                 ide_unregister(index);
2131
2132 #ifdef CONFIG_BLK_DEV_IDEPNP
2133         pnpide_exit();
2134 #endif
2135
2136 #ifdef CONFIG_PROC_FS
2137         proc_ide_destroy();
2138 #endif
2139
2140         bus_unregister(&ide_bus_type);
2141 }
2142
2143 #else /* !MODULE */
2144
2145 __setup("", ide_setup);
2146
2147 module_init(ide_init);
2148
2149 #endif /* MODULE */