ide: cleanup ide_find_port()
[safe/jmp/linux-2.6] / drivers / ide / ide.c
1 /*
2  *  Copyright (C) 1994-1998         Linus Torvalds & authors (see below)
3  *  Copyrifht (C) 2003-2005, 2007   Bartlomiej Zolnierkiewicz
4  */
5
6 /*
7  *  Mostly written by Mark Lord  <mlord@pobox.com>
8  *                and Gadi Oxman <gadio@netvision.net.il>
9  *                and Andre Hedrick <andre@linux-ide.org>
10  *
11  *  See linux/MAINTAINERS for address of current maintainer.
12  *
13  * This is the multiple IDE interface driver, as evolved from hd.c.
14  * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
15  *   (usually 14 & 15).
16  * There can be up to two drives per interface, as per the ATA-2 spec.
17  *
18  * ...
19  *
20  *  From hd.c:
21  *  |
22  *  | It traverses the request-list, using interrupts to jump between functions.
23  *  | As nearly all functions can be called within interrupts, we may not sleep.
24  *  | Special care is recommended.  Have Fun!
25  *  |
26  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
27  *  |
28  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
29  *  | in the early extended-partition checks and added DM partitions.
30  *  |
31  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
32  *  |
33  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
34  *  | and general streamlining by Mark Lord (mlord@pobox.com).
35  *
36  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
37  *
38  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
39  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
40  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
41  *
42  *  This was a rewrite of just about everything from hd.c, though some original
43  *  code is still sprinkled about.  Think of it as a major evolution, with
44  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
45  */
46
47 #define _IDE_C                  /* Tell ide.h it's really us */
48
49 #include <linux/module.h>
50 #include <linux/types.h>
51 #include <linux/string.h>
52 #include <linux/kernel.h>
53 #include <linux/timer.h>
54 #include <linux/mm.h>
55 #include <linux/interrupt.h>
56 #include <linux/major.h>
57 #include <linux/errno.h>
58 #include <linux/genhd.h>
59 #include <linux/blkpg.h>
60 #include <linux/slab.h>
61 #include <linux/init.h>
62 #include <linux/pci.h>
63 #include <linux/delay.h>
64 #include <linux/ide.h>
65 #include <linux/completion.h>
66 #include <linux/reboot.h>
67 #include <linux/cdrom.h>
68 #include <linux/seq_file.h>
69 #include <linux/device.h>
70 #include <linux/bitops.h>
71
72 #include <asm/byteorder.h>
73 #include <asm/irq.h>
74 #include <asm/uaccess.h>
75 #include <asm/io.h>
76
77
78 /* default maximum number of failures */
79 #define IDE_DEFAULT_MAX_FAILURES        1
80
81 struct class *ide_port_class;
82
83 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
84                                         IDE2_MAJOR, IDE3_MAJOR,
85                                         IDE4_MAJOR, IDE5_MAJOR,
86                                         IDE6_MAJOR, IDE7_MAJOR,
87                                         IDE8_MAJOR, IDE9_MAJOR };
88
89 static int idebus_parameter;    /* holds the "idebus=" parameter */
90 static int system_bus_speed;    /* holds what we think is VESA/PCI bus speed */
91
92 DEFINE_MUTEX(ide_cfg_mtx);
93  __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
94
95 int noautodma = 0;
96
97 #ifdef CONFIG_BLK_DEV_IDEACPI
98 int ide_noacpi = 0;
99 int ide_noacpitfs = 1;
100 int ide_noacpionboot = 1;
101 #endif
102
103 /*
104  * This is declared extern in ide.h, for access by other IDE modules:
105  */
106 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
107
108 EXPORT_SYMBOL(ide_hwifs);
109
110 static void ide_port_init_devices_data(ide_hwif_t *);
111
112 /*
113  * Do not even *think* about calling this!
114  */
115 void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
116 {
117         /* bulk initialize hwif & drive info with zeros */
118         memset(hwif, 0, sizeof(ide_hwif_t));
119
120         /* fill in any non-zero initial values */
121         hwif->index     = index;
122         hwif->major     = ide_hwif_to_major[index];
123
124         hwif->name[0]   = 'i';
125         hwif->name[1]   = 'd';
126         hwif->name[2]   = 'e';
127         hwif->name[3]   = '0' + index;
128
129         hwif->bus_state = BUSSTATE_ON;
130
131         init_completion(&hwif->gendev_rel_comp);
132
133         default_hwif_iops(hwif);
134         default_hwif_transport(hwif);
135
136         ide_port_init_devices_data(hwif);
137 }
138 EXPORT_SYMBOL_GPL(ide_init_port_data);
139
140 static void ide_port_init_devices_data(ide_hwif_t *hwif)
141 {
142         int unit;
143
144         for (unit = 0; unit < MAX_DRIVES; ++unit) {
145                 ide_drive_t *drive = &hwif->drives[unit];
146                 u8 j = (hwif->index * MAX_DRIVES) + unit;
147
148                 memset(drive, 0, sizeof(*drive));
149
150                 drive->media                    = ide_disk;
151                 drive->select.all               = (unit<<4)|0xa0;
152                 drive->hwif                     = hwif;
153                 drive->ctl                      = 0x08;
154                 drive->ready_stat               = READY_STAT;
155                 drive->bad_wstat                = BAD_W_STAT;
156                 drive->special.b.recalibrate    = 1;
157                 drive->special.b.set_geometry   = 1;
158                 drive->name[0]                  = 'h';
159                 drive->name[1]                  = 'd';
160                 drive->name[2]                  = 'a' + j;
161                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
162
163                 INIT_LIST_HEAD(&drive->list);
164                 init_completion(&drive->gendev_rel_comp);
165         }
166 }
167
168 /*
169  * init_ide_data() sets reasonable default values into all fields
170  * of all instances of the hwifs and drives, but only on the first call.
171  * Subsequent calls have no effect (they don't wipe out anything).
172  *
173  * This routine is normally called at driver initialization time,
174  * but may also be called MUCH earlier during kernel "command-line"
175  * parameter processing.  As such, we cannot depend on any other parts
176  * of the kernel (such as memory allocation) to be functioning yet.
177  *
178  * This is too bad, as otherwise we could dynamically allocate the
179  * ide_drive_t structs as needed, rather than always consuming memory
180  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
181  *
182  * FIXME: We should stuff the setup data into __init and copy the
183  * relevant hwifs/allocate them properly during boot.
184  */
185 #define MAGIC_COOKIE 0x12345678
186 static void __init init_ide_data (void)
187 {
188         unsigned int index;
189         static unsigned long magic_cookie = MAGIC_COOKIE;
190
191         if (magic_cookie != MAGIC_COOKIE)
192                 return;         /* already initialized */
193         magic_cookie = 0;
194
195         /* Initialise all interface structures */
196         for (index = 0; index < MAX_HWIFS; ++index) {
197                 ide_hwif_t *hwif = &ide_hwifs[index];
198
199                 ide_init_port_data(hwif, index);
200         }
201 }
202
203 /**
204  *      ide_system_bus_speed    -       guess bus speed
205  *
206  *      ide_system_bus_speed() returns what we think is the system VESA/PCI
207  *      bus speed (in MHz). This is used for calculating interface PIO timings.
208  *      The default is 40 for known PCI systems, 50 otherwise.
209  *      The "idebus=xx" parameter can be used to override this value.
210  *      The actual value to be used is computed/displayed the first time
211  *      through. Drivers should only use this as a last resort.
212  *
213  *      Returns a guessed speed in MHz.
214  */
215
216 static int ide_system_bus_speed(void)
217 {
218 #ifdef CONFIG_PCI
219         static struct pci_device_id pci_default[] = {
220                 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
221                 { }
222         };
223 #else
224 #define pci_default 0
225 #endif /* CONFIG_PCI */
226
227         /* user supplied value */
228         if (idebus_parameter)
229                 return idebus_parameter;
230
231         /* safe default value for PCI or VESA and PCI*/
232         return pci_dev_present(pci_default) ? 33 : 50;
233 }
234
235 ide_hwif_t *ide_find_port(void)
236 {
237         ide_hwif_t *hwif;
238         int i;
239
240         for (i = 0; i < MAX_HWIFS; i++) {
241                 hwif = &ide_hwifs[i];
242                 if (hwif->chipset == ide_unknown)
243                         return hwif;
244         }
245
246         return NULL;
247 }
248 EXPORT_SYMBOL_GPL(ide_find_port);
249
250 static struct resource* hwif_request_region(ide_hwif_t *hwif,
251                                             unsigned long addr, int num)
252 {
253         struct resource *res = request_region(addr, num, hwif->name);
254
255         if (!res)
256                 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
257                                 hwif->name, addr, addr+num-1);
258         return res;
259 }
260
261 /**
262  *      ide_hwif_request_regions - request resources for IDE
263  *      @hwif: interface to use
264  *
265  *      Requests all the needed resources for an interface.
266  *      Right now core IDE code does this work which is deeply wrong.
267  *      MMIO leaves it to the controller driver,
268  *      PIO will migrate this way over time.
269  */
270
271 int ide_hwif_request_regions(ide_hwif_t *hwif)
272 {
273         unsigned long addr;
274         unsigned int i;
275
276         if (hwif->mmio)
277                 return 0;
278         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
279         if (addr && !hwif_request_region(hwif, addr, 1))
280                 goto control_region_busy;
281         hwif->straight8 = 0;
282         addr = hwif->io_ports[IDE_DATA_OFFSET];
283         if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
284                 if (!hwif_request_region(hwif, addr, 8))
285                         goto data_region_busy;
286                 hwif->straight8 = 1;
287                 return 0;
288         }
289         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
290                 addr = hwif->io_ports[i];
291                 if (!hwif_request_region(hwif, addr, 1)) {
292                         while (--i)
293                                 release_region(addr, 1);
294                         goto data_region_busy;
295                 }
296         }
297         return 0;
298
299 data_region_busy:
300         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
301         if (addr)
302                 release_region(addr, 1);
303 control_region_busy:
304         /* If any errors are return, we drop the hwif interface. */
305         return -EBUSY;
306 }
307
308 /**
309  *      ide_hwif_release_regions - free IDE resources
310  *
311  *      Note that we only release the standard ports,
312  *      and do not even try to handle any extra ports
313  *      allocated for weird IDE interface chipsets.
314  *
315  *      Note also that we don't yet handle mmio resources here. More
316  *      importantly our caller should be doing this so we need to 
317  *      restructure this as a helper function for drivers.
318  */
319
320 void ide_hwif_release_regions(ide_hwif_t *hwif)
321 {
322         u32 i = 0;
323
324         if (hwif->mmio)
325                 return;
326         if (hwif->io_ports[IDE_CONTROL_OFFSET])
327                 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
328         if (hwif->straight8) {
329                 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
330                 return;
331         }
332         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
333                 if (hwif->io_ports[i])
334                         release_region(hwif->io_ports[i], 1);
335 }
336
337 void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
338 {
339         ide_hwgroup_t *hwgroup = hwif->hwgroup;
340
341         spin_lock_irq(&ide_lock);
342         /*
343          * Remove us from the hwgroup, and free
344          * the hwgroup if we were the only member
345          */
346         if (hwif->next == hwif) {
347                 BUG_ON(hwgroup->hwif != hwif);
348                 kfree(hwgroup);
349         } else {
350                 /* There is another interface in hwgroup.
351                  * Unlink us, and set hwgroup->drive and ->hwif to
352                  * something sane.
353                  */
354                 ide_hwif_t *g = hwgroup->hwif;
355
356                 while (g->next != hwif)
357                         g = g->next;
358                 g->next = hwif->next;
359                 if (hwgroup->hwif == hwif) {
360                         /* Chose a random hwif for hwgroup->hwif.
361                          * It's guaranteed that there are no drives
362                          * left in the hwgroup.
363                          */
364                         BUG_ON(hwgroup->drive != NULL);
365                         hwgroup->hwif = g;
366                 }
367                 BUG_ON(hwgroup->hwif == hwif);
368         }
369         spin_unlock_irq(&ide_lock);
370 }
371
372 /* Called with ide_lock held. */
373 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
374 {
375         int i;
376
377         for (i = 0; i < MAX_DRIVES; i++) {
378                 ide_drive_t *drive = &hwif->drives[i];
379
380                 if (drive->present) {
381                         spin_unlock_irq(&ide_lock);
382                         device_unregister(&drive->gendev);
383                         wait_for_completion(&drive->gendev_rel_comp);
384                         spin_lock_irq(&ide_lock);
385                 }
386         }
387 }
388
389 void ide_port_unregister_devices(ide_hwif_t *hwif)
390 {
391         mutex_lock(&ide_cfg_mtx);
392         spin_lock_irq(&ide_lock);
393         __ide_port_unregister_devices(hwif);
394         hwif->present = 0;
395         ide_port_init_devices_data(hwif);
396         spin_unlock_irq(&ide_lock);
397         mutex_unlock(&ide_cfg_mtx);
398 }
399 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
400
401 /**
402  *      ide_unregister          -       free an IDE interface
403  *      @index: index of interface (will change soon to a pointer)
404  *
405  *      Perform the final unregister of an IDE interface. At the moment
406  *      we don't refcount interfaces so this will also get split up.
407  *
408  *      Locking:
409  *      The caller must not hold the IDE locks
410  *      The drive present/vanishing is not yet properly locked
411  *      Take care with the callbacks. These have been split to avoid
412  *      deadlocking the IDE layer. The shutdown callback is called
413  *      before we take the lock and free resources. It is up to the
414  *      caller to be sure there is no pending I/O here, and that
415  *      the interface will not be reopened (present/vanishing locking
416  *      isn't yet done BTW). After we commit to the final kill we
417  *      call the cleanup callback with the ide locks held.
418  *
419  *      Unregister restores the hwif structures to the default state.
420  *      This is raving bonkers.
421  */
422
423 void ide_unregister(unsigned int index)
424 {
425         ide_hwif_t *hwif, *g;
426         ide_hwgroup_t *hwgroup;
427         int irq_count = 0;
428
429         BUG_ON(index >= MAX_HWIFS);
430
431         BUG_ON(in_interrupt());
432         BUG_ON(irqs_disabled());
433         mutex_lock(&ide_cfg_mtx);
434         spin_lock_irq(&ide_lock);
435         hwif = &ide_hwifs[index];
436         if (!hwif->present)
437                 goto abort;
438         __ide_port_unregister_devices(hwif);
439         hwif->present = 0;
440
441         spin_unlock_irq(&ide_lock);
442
443         ide_proc_unregister_port(hwif);
444
445         hwgroup = hwif->hwgroup;
446         /*
447          * free the irq if we were the only hwif using it
448          */
449         g = hwgroup->hwif;
450         do {
451                 if (g->irq == hwif->irq)
452                         ++irq_count;
453                 g = g->next;
454         } while (g != hwgroup->hwif);
455         if (irq_count == 1)
456                 free_irq(hwif->irq, hwgroup);
457
458         ide_remove_port_from_hwgroup(hwif);
459
460         device_unregister(hwif->portdev);
461         device_unregister(&hwif->gendev);
462         wait_for_completion(&hwif->gendev_rel_comp);
463
464         /*
465          * Remove us from the kernel's knowledge
466          */
467         blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
468         kfree(hwif->sg_table);
469         unregister_blkdev(hwif->major, hwif->name);
470         spin_lock_irq(&ide_lock);
471
472         if (hwif->dma_base)
473                 (void)ide_release_dma(hwif);
474
475         ide_hwif_release_regions(hwif);
476
477         /* restore hwif data to pristine status */
478         ide_init_port_data(hwif, index);
479
480 abort:
481         spin_unlock_irq(&ide_lock);
482         mutex_unlock(&ide_cfg_mtx);
483 }
484
485 EXPORT_SYMBOL(ide_unregister);
486
487 void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
488 {
489         memcpy(hwif->io_ports, hw->io_ports, sizeof(hwif->io_ports));
490         hwif->irq = hw->irq;
491         hwif->noprobe = 0;
492         hwif->chipset = hw->chipset;
493         hwif->gendev.parent = hw->dev;
494         hwif->ack_intr = hw->ack_intr;
495 }
496 EXPORT_SYMBOL_GPL(ide_init_port_hw);
497
498 /*
499  *      Locks for IDE setting functionality
500  */
501
502 DEFINE_MUTEX(ide_setting_mtx);
503
504 EXPORT_SYMBOL_GPL(ide_setting_mtx);
505
506 /**
507  *      ide_spin_wait_hwgroup   -       wait for group
508  *      @drive: drive in the group
509  *
510  *      Wait for an IDE device group to go non busy and then return
511  *      holding the ide_lock which guards the hwgroup->busy status
512  *      and right to use it.
513  */
514
515 int ide_spin_wait_hwgroup (ide_drive_t *drive)
516 {
517         ide_hwgroup_t *hwgroup = HWGROUP(drive);
518         unsigned long timeout = jiffies + (3 * HZ);
519
520         spin_lock_irq(&ide_lock);
521
522         while (hwgroup->busy) {
523                 unsigned long lflags;
524                 spin_unlock_irq(&ide_lock);
525                 local_irq_set(lflags);
526                 if (time_after(jiffies, timeout)) {
527                         local_irq_restore(lflags);
528                         printk(KERN_ERR "%s: channel busy\n", drive->name);
529                         return -EBUSY;
530                 }
531                 local_irq_restore(lflags);
532                 spin_lock_irq(&ide_lock);
533         }
534         return 0;
535 }
536
537 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
538
539 int set_io_32bit(ide_drive_t *drive, int arg)
540 {
541         if (drive->no_io_32bit)
542                 return -EPERM;
543
544         if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
545                 return -EINVAL;
546
547         if (ide_spin_wait_hwgroup(drive))
548                 return -EBUSY;
549
550         drive->io_32bit = arg;
551
552         spin_unlock_irq(&ide_lock);
553
554         return 0;
555 }
556
557 static int set_ksettings(ide_drive_t *drive, int arg)
558 {
559         if (arg < 0 || arg > 1)
560                 return -EINVAL;
561
562         if (ide_spin_wait_hwgroup(drive))
563                 return -EBUSY;
564         drive->keep_settings = arg;
565         spin_unlock_irq(&ide_lock);
566
567         return 0;
568 }
569
570 int set_using_dma(ide_drive_t *drive, int arg)
571 {
572 #ifdef CONFIG_BLK_DEV_IDEDMA
573         ide_hwif_t *hwif = drive->hwif;
574         int err = -EPERM;
575
576         if (arg < 0 || arg > 1)
577                 return -EINVAL;
578
579         if (!drive->id || !(drive->id->capability & 1))
580                 goto out;
581
582         if (hwif->dma_host_set == NULL)
583                 goto out;
584
585         err = -EBUSY;
586         if (ide_spin_wait_hwgroup(drive))
587                 goto out;
588         /*
589          * set ->busy flag, unlock and let it ride
590          */
591         hwif->hwgroup->busy = 1;
592         spin_unlock_irq(&ide_lock);
593
594         err = 0;
595
596         if (arg) {
597                 if (ide_set_dma(drive))
598                         err = -EIO;
599         } else
600                 ide_dma_off(drive);
601
602         /*
603          * lock, clear ->busy flag and unlock before leaving
604          */
605         spin_lock_irq(&ide_lock);
606         hwif->hwgroup->busy = 0;
607         spin_unlock_irq(&ide_lock);
608 out:
609         return err;
610 #else
611         if (arg < 0 || arg > 1)
612                 return -EINVAL;
613
614         return -EPERM;
615 #endif
616 }
617
618 int set_pio_mode(ide_drive_t *drive, int arg)
619 {
620         struct request rq;
621
622         if (arg < 0 || arg > 255)
623                 return -EINVAL;
624
625         if (drive->hwif->set_pio_mode == NULL)
626                 return -ENOSYS;
627
628         if (drive->special.b.set_tune)
629                 return -EBUSY;
630
631         ide_init_drive_cmd(&rq);
632         rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
633
634         drive->tune_req = (u8) arg;
635         drive->special.b.set_tune = 1;
636         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
637         return 0;
638 }
639
640 static int set_unmaskirq(ide_drive_t *drive, int arg)
641 {
642         if (drive->no_unmask)
643                 return -EPERM;
644
645         if (arg < 0 || arg > 1)
646                 return -EINVAL;
647
648         if (ide_spin_wait_hwgroup(drive))
649                 return -EBUSY;
650         drive->unmask = arg;
651         spin_unlock_irq(&ide_lock);
652
653         return 0;
654 }
655
656 /**
657  *      system_bus_clock        -       clock guess
658  *
659  *      External version of the bus clock guess used by very old IDE drivers
660  *      for things like VLB timings. Should not be used.
661  */
662
663 int system_bus_clock (void)
664 {
665         return system_bus_speed;
666 }
667
668 EXPORT_SYMBOL(system_bus_clock);
669
670 static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
671 {
672         ide_drive_t *drive = dev->driver_data;
673         ide_hwif_t *hwif = HWIF(drive);
674         struct request rq;
675         struct request_pm_state rqpm;
676         ide_task_t args;
677         int ret;
678
679         /* Call ACPI _GTM only once */
680         if (!(drive->dn % 2))
681                 ide_acpi_get_timing(hwif);
682
683         memset(&rq, 0, sizeof(rq));
684         memset(&rqpm, 0, sizeof(rqpm));
685         memset(&args, 0, sizeof(args));
686         rq.cmd_type = REQ_TYPE_PM_SUSPEND;
687         rq.special = &args;
688         rq.data = &rqpm;
689         rqpm.pm_step = ide_pm_state_start_suspend;
690         if (mesg.event == PM_EVENT_PRETHAW)
691                 mesg.event = PM_EVENT_FREEZE;
692         rqpm.pm_state = mesg.event;
693
694         ret = ide_do_drive_cmd(drive, &rq, ide_wait);
695         /* only call ACPI _PS3 after both drivers are suspended */
696         if (!ret && (((drive->dn % 2) && hwif->drives[0].present
697                  && hwif->drives[1].present)
698                  || !hwif->drives[0].present
699                  || !hwif->drives[1].present))
700                 ide_acpi_set_state(hwif, 0);
701         return ret;
702 }
703
704 static int generic_ide_resume(struct device *dev)
705 {
706         ide_drive_t *drive = dev->driver_data;
707         ide_hwif_t *hwif = HWIF(drive);
708         struct request rq;
709         struct request_pm_state rqpm;
710         ide_task_t args;
711         int err;
712
713         /* Call ACPI _STM only once */
714         if (!(drive->dn % 2)) {
715                 ide_acpi_set_state(hwif, 1);
716                 ide_acpi_push_timing(hwif);
717         }
718
719         ide_acpi_exec_tfs(drive);
720
721         memset(&rq, 0, sizeof(rq));
722         memset(&rqpm, 0, sizeof(rqpm));
723         memset(&args, 0, sizeof(args));
724         rq.cmd_type = REQ_TYPE_PM_RESUME;
725         rq.special = &args;
726         rq.data = &rqpm;
727         rqpm.pm_step = ide_pm_state_start_resume;
728         rqpm.pm_state = PM_EVENT_ON;
729
730         err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
731
732         if (err == 0 && dev->driver) {
733                 ide_driver_t *drv = to_ide_driver(dev->driver);
734
735                 if (drv->resume)
736                         drv->resume(drive);
737         }
738
739         return err;
740 }
741
742 int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
743                         unsigned int cmd, unsigned long arg)
744 {
745         unsigned long flags;
746         ide_driver_t *drv;
747         void __user *p = (void __user *)arg;
748         int err = 0, (*setfunc)(ide_drive_t *, int);
749         u8 *val;
750
751         switch (cmd) {
752         case HDIO_GET_32BIT:        val = &drive->io_32bit;      goto read_val;
753         case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
754         case HDIO_GET_UNMASKINTR:   val = &drive->unmask;        goto read_val;
755         case HDIO_GET_DMA:          val = &drive->using_dma;     goto read_val;
756         case HDIO_SET_32BIT:        setfunc = set_io_32bit;      goto set_val;
757         case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings;     goto set_val;
758         case HDIO_SET_PIO_MODE:     setfunc = set_pio_mode;      goto set_val;
759         case HDIO_SET_UNMASKINTR:   setfunc = set_unmaskirq;     goto set_val;
760         case HDIO_SET_DMA:          setfunc = set_using_dma;     goto set_val;
761         }
762
763         switch (cmd) {
764                 case HDIO_OBSOLETE_IDENTITY:
765                 case HDIO_GET_IDENTITY:
766                         if (bdev != bdev->bd_contains)
767                                 return -EINVAL;
768                         if (drive->id_read == 0)
769                                 return -ENOMSG;
770                         if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
771                                 return -EFAULT;
772                         return 0;
773
774                 case HDIO_GET_NICE:
775                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
776                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
777                                         drive->nice1 << IDE_NICE_1,
778                                         (long __user *) arg);
779 #ifdef CONFIG_IDE_TASK_IOCTL
780                 case HDIO_DRIVE_TASKFILE:
781                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
782                                 return -EACCES;
783                         switch(drive->media) {
784                                 case ide_disk:
785                                         return ide_taskfile_ioctl(drive, cmd, arg);
786                                 default:
787                                         return -ENOMSG;
788                         }
789 #endif /* CONFIG_IDE_TASK_IOCTL */
790
791                 case HDIO_DRIVE_CMD:
792                         if (!capable(CAP_SYS_RAWIO))
793                                 return -EACCES;
794                         return ide_cmd_ioctl(drive, cmd, arg);
795
796                 case HDIO_DRIVE_TASK:
797                         if (!capable(CAP_SYS_RAWIO))
798                                 return -EACCES;
799                         return ide_task_ioctl(drive, cmd, arg);
800                 case HDIO_SET_NICE:
801                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
802                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
803                                 return -EPERM;
804                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
805                         drv = *(ide_driver_t **)bdev->bd_disk->private_data;
806                         if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
807                                 drive->dsc_overlap = 0;
808                                 return -EPERM;
809                         }
810                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
811                         return 0;
812                 case HDIO_DRIVE_RESET:
813                         if (!capable(CAP_SYS_ADMIN))
814                                 return -EACCES;
815
816                         /*
817                          *      Abort the current command on the
818                          *      group if there is one, taking
819                          *      care not to allow anything else
820                          *      to be queued and to die on the
821                          *      spot if we miss one somehow
822                          */
823
824                         spin_lock_irqsave(&ide_lock, flags);
825
826                         if (HWGROUP(drive)->resetting) {
827                                 spin_unlock_irqrestore(&ide_lock, flags);
828                                 return -EBUSY;
829                         }
830
831                         ide_abort(drive, "drive reset");
832
833                         BUG_ON(HWGROUP(drive)->handler);
834
835                         /* Ensure nothing gets queued after we
836                            drop the lock. Reset will clear the busy */
837
838                         HWGROUP(drive)->busy = 1;
839                         spin_unlock_irqrestore(&ide_lock, flags);
840                         (void) ide_do_reset(drive);
841
842                         return 0;
843                 case HDIO_GET_BUSSTATE:
844                         if (!capable(CAP_SYS_ADMIN))
845                                 return -EACCES;
846                         if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
847                                 return -EFAULT;
848                         return 0;
849
850                 case HDIO_SET_BUSSTATE:
851                         if (!capable(CAP_SYS_ADMIN))
852                                 return -EACCES;
853                         return -EOPNOTSUPP;
854                 default:
855                         return -EINVAL;
856         }
857
858 read_val:
859         mutex_lock(&ide_setting_mtx);
860         spin_lock_irqsave(&ide_lock, flags);
861         err = *val;
862         spin_unlock_irqrestore(&ide_lock, flags);
863         mutex_unlock(&ide_setting_mtx);
864         return err >= 0 ? put_user(err, (long __user *)arg) : err;
865
866 set_val:
867         if (bdev != bdev->bd_contains)
868                 err = -EINVAL;
869         else {
870                 if (!capable(CAP_SYS_ADMIN))
871                         err = -EACCES;
872                 else {
873                         mutex_lock(&ide_setting_mtx);
874                         err = setfunc(drive, arg);
875                         mutex_unlock(&ide_setting_mtx);
876                 }
877         }
878         return err;
879 }
880
881 EXPORT_SYMBOL(generic_ide_ioctl);
882
883 /*
884  * stridx() returns the offset of c within s,
885  * or -1 if c is '\0' or not found within s.
886  */
887 static int __init stridx (const char *s, char c)
888 {
889         char *i = strchr(s, c);
890         return (i && c) ? i - s : -1;
891 }
892
893 /*
894  * match_parm() does parsing for ide_setup():
895  *
896  * 1. the first char of s must be '='.
897  * 2. if the remainder matches one of the supplied keywords,
898  *     the index (1 based) of the keyword is negated and returned.
899  * 3. if the remainder is a series of no more than max_vals numbers
900  *     separated by commas, the numbers are saved in vals[] and a
901  *     count of how many were saved is returned.  Base10 is assumed,
902  *     and base16 is allowed when prefixed with "0x".
903  * 4. otherwise, zero is returned.
904  */
905 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
906 {
907         static const char *decimal = "0123456789";
908         static const char *hex = "0123456789abcdef";
909         int i, n;
910
911         if (*s++ == '=') {
912                 /*
913                  * Try matching against the supplied keywords,
914                  * and return -(index+1) if we match one
915                  */
916                 if (keywords != NULL) {
917                         for (i = 0; *keywords != NULL; ++i) {
918                                 if (!strcmp(s, *keywords++))
919                                         return -(i+1);
920                         }
921                 }
922                 /*
923                  * Look for a series of no more than "max_vals"
924                  * numeric values separated by commas, in base10,
925                  * or base16 when prefixed with "0x".
926                  * Return a count of how many were found.
927                  */
928                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
929                         vals[n] = i;
930                         while ((i = stridx(decimal, *++s)) >= 0)
931                                 vals[n] = (vals[n] * 10) + i;
932                         if (*s == 'x' && !vals[n]) {
933                                 while ((i = stridx(hex, *++s)) >= 0)
934                                         vals[n] = (vals[n] * 0x10) + i;
935                         }
936                         if (++n == max_vals)
937                                 break;
938                         if (*s == ',' || *s == ';')
939                                 ++s;
940                 }
941                 if (!*s)
942                         return n;
943         }
944         return 0;       /* zero = nothing matched */
945 }
946
947 extern int probe_ali14xx;
948 extern int probe_umc8672;
949 extern int probe_dtc2278;
950 extern int probe_ht6560b;
951 extern int probe_qd65xx;
952 extern int cmd640_vlb;
953 extern int probe_4drives;
954
955 static int __initdata is_chipset_set;
956
957 /*
958  * ide_setup() gets called VERY EARLY during initialization,
959  * to handle kernel "command line" strings beginning with "hdx=" or "ide".
960  *
961  * Remember to update Documentation/ide/ide.txt if you change something here.
962  */
963 static int __init ide_setup(char *s)
964 {
965         int i, vals[3];
966         ide_hwif_t *hwif;
967         ide_drive_t *drive;
968         unsigned int hw, unit;
969         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
970         const char max_hwif  = '0' + (MAX_HWIFS - 1);
971
972         
973         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
974                 return 0;                               /* driver and not us */
975
976         if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
977                 return 0;
978
979         printk(KERN_INFO "ide_setup: %s", s);
980         init_ide_data ();
981
982 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
983         if (!strcmp(s, "ide=doubler")) {
984                 extern int ide_doubler;
985
986                 printk(" : Enabled support for IDE doublers\n");
987                 ide_doubler = 1;
988                 return 1;
989         }
990 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
991
992         if (!strcmp(s, "ide=nodma")) {
993                 printk(" : Prevented DMA\n");
994                 noautodma = 1;
995                 goto obsolete_option;
996         }
997
998 #ifdef CONFIG_BLK_DEV_IDEACPI
999         if (!strcmp(s, "ide=noacpi")) {
1000                 //printk(" : Disable IDE ACPI support.\n");
1001                 ide_noacpi = 1;
1002                 return 1;
1003         }
1004         if (!strcmp(s, "ide=acpigtf")) {
1005                 //printk(" : Enable IDE ACPI _GTF support.\n");
1006                 ide_noacpitfs = 0;
1007                 return 1;
1008         }
1009         if (!strcmp(s, "ide=acpionboot")) {
1010                 //printk(" : Call IDE ACPI methods on boot.\n");
1011                 ide_noacpionboot = 0;
1012                 return 1;
1013         }
1014 #endif /* CONFIG_BLK_DEV_IDEACPI */
1015
1016         /*
1017          * Look for drive options:  "hdx="
1018          */
1019         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1020                 const char *hd_words[] = {
1021                         "none", "noprobe", "nowerr", "cdrom", "nodma",
1022                         "autotune", "noautotune", "-8", "-9", "-10",
1023                         "noflush", "remap", "remap63", "scsi", NULL };
1024                 unit = s[2] - 'a';
1025                 hw   = unit / MAX_DRIVES;
1026                 unit = unit % MAX_DRIVES;
1027                 hwif = &ide_hwifs[hw];
1028                 drive = &hwif->drives[unit];
1029                 if (strncmp(s + 4, "ide-", 4) == 0) {
1030                         strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1031                         goto obsolete_option;
1032                 }
1033                 switch (match_parm(&s[3], hd_words, vals, 3)) {
1034                         case -1: /* "none" */
1035                         case -2: /* "noprobe" */
1036                                 drive->noprobe = 1;
1037                                 goto done;
1038                         case -3: /* "nowerr" */
1039                                 drive->bad_wstat = BAD_R_STAT;
1040                                 hwif->noprobe = 0;
1041                                 goto done;
1042                         case -4: /* "cdrom" */
1043                                 drive->present = 1;
1044                                 drive->media = ide_cdrom;
1045                                 /* an ATAPI device ignores DRDY */
1046                                 drive->ready_stat = 0;
1047                                 hwif->noprobe = 0;
1048                                 goto done;
1049                         case -5: /* nodma */
1050                                 drive->nodma = 1;
1051                                 goto done;
1052                         case -6: /* "autotune" */
1053                                 drive->autotune = IDE_TUNE_AUTO;
1054                                 goto obsolete_option;
1055                         case -7: /* "noautotune" */
1056                                 drive->autotune = IDE_TUNE_NOAUTO;
1057                                 goto obsolete_option;
1058                         case -11: /* noflush */
1059                                 drive->noflush = 1;
1060                                 goto done;
1061                         case -12: /* "remap" */
1062                                 drive->remap_0_to_1 = 1;
1063                                 goto obsolete_option;
1064                         case -13: /* "remap63" */
1065                                 drive->sect0 = 63;
1066                                 goto obsolete_option;
1067                         case -14: /* "scsi" */
1068                                 drive->scsi = 1;
1069                                 goto obsolete_option;
1070                         case 3: /* cyl,head,sect */
1071                                 drive->media    = ide_disk;
1072                                 drive->ready_stat = READY_STAT;
1073                                 drive->cyl      = drive->bios_cyl  = vals[0];
1074                                 drive->head     = drive->bios_head = vals[1];
1075                                 drive->sect     = drive->bios_sect = vals[2];
1076                                 drive->present  = 1;
1077                                 drive->forced_geom = 1;
1078                                 hwif->noprobe = 0;
1079                                 goto done;
1080                         default:
1081                                 goto bad_option;
1082                 }
1083         }
1084
1085         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1086                 goto bad_option;
1087         /*
1088          * Look for bus speed option:  "idebus="
1089          */
1090         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1091                 if (match_parm(&s[6], NULL, vals, 1) != 1)
1092                         goto bad_option;
1093                 if (vals[0] >= 20 && vals[0] <= 66) {
1094                         idebus_parameter = vals[0];
1095                 } else
1096                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1097                 goto done;
1098         }
1099         /*
1100          * Look for interface options:  "idex="
1101          */
1102         if (s[3] >= '0' && s[3] <= max_hwif) {
1103                 /*
1104                  * Be VERY CAREFUL changing this: note hardcoded indexes below
1105                  * (-8, -9, -10) are reserved to ease the hardcoding.
1106                  */
1107                 static const char *ide_words[] = {
1108                         "minus1", "serialize", "minus3", "minus4",
1109                         "reset", "minus6", "ata66", "minus8", "minus9",
1110                         "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1111                         "dtc2278", "umc8672", "ali14xx", NULL };
1112
1113                 hw = s[3] - '0';
1114                 hwif = &ide_hwifs[hw];
1115                 i = match_parm(&s[4], ide_words, vals, 3);
1116
1117                 /*
1118                  * Cryptic check to ensure chipset not already set for hwif.
1119                  * Note: we can't depend on hwif->chipset here.
1120                  */
1121                 if (i >= -18 && i <= -11) {
1122                         /* chipset already specified */
1123                         if (is_chipset_set)
1124                                 goto bad_option;
1125                         /* these drivers are for "ide0=" only */
1126                         if (hw != 0)
1127                                 goto bad_hwif;
1128                         is_chipset_set = 1;
1129                         printk("\n");
1130                 }
1131
1132                 switch (i) {
1133 #ifdef CONFIG_BLK_DEV_ALI14XX
1134                         case -17: /* "ali14xx" */
1135                                 probe_ali14xx = 1;
1136                                 goto obsolete_option;
1137 #endif
1138 #ifdef CONFIG_BLK_DEV_UMC8672
1139                         case -16: /* "umc8672" */
1140                                 probe_umc8672 = 1;
1141                                 goto obsolete_option;
1142 #endif
1143 #ifdef CONFIG_BLK_DEV_DTC2278
1144                         case -15: /* "dtc2278" */
1145                                 probe_dtc2278 = 1;
1146                                 goto obsolete_option;
1147 #endif
1148 #ifdef CONFIG_BLK_DEV_CMD640
1149                         case -14: /* "cmd640_vlb" */
1150                                 cmd640_vlb = 1;
1151                                 goto obsolete_option;
1152 #endif
1153 #ifdef CONFIG_BLK_DEV_HT6560B
1154                         case -13: /* "ht6560b" */
1155                                 probe_ht6560b = 1;
1156                                 goto obsolete_option;
1157 #endif
1158 #ifdef CONFIG_BLK_DEV_QD65XX
1159                         case -12: /* "qd65xx" */
1160                                 probe_qd65xx = 1;
1161                                 goto obsolete_option;
1162 #endif
1163 #ifdef CONFIG_BLK_DEV_4DRIVES
1164                         case -11: /* "four" drives on one set of ports */
1165                                 probe_4drives = 1;
1166                                 goto obsolete_option;
1167 #endif
1168                         case -10: /* minus10 */
1169                         case -9: /* minus9 */
1170                         case -8: /* minus8 */
1171                         case -6:
1172                         case -4:
1173                         case -3:
1174                                 goto bad_option;
1175                         case -7: /* ata66 */
1176 #ifdef CONFIG_BLK_DEV_IDEPCI
1177                                 /*
1178                                  * Use ATA_CBL_PATA40_SHORT so drive side
1179                                  * cable detection is also overriden.
1180                                  */
1181                                 hwif->cbl = ATA_CBL_PATA40_SHORT;
1182                                 goto obsolete_option;
1183 #else
1184                                 goto bad_hwif;
1185 #endif
1186                         case -5: /* "reset" */
1187                                 hwif->reset = 1;
1188                                 goto obsolete_option;
1189                         case -2: /* "serialize" */
1190                                 hwif->mate = &ide_hwifs[hw^1];
1191                                 hwif->mate->mate = hwif;
1192                                 hwif->serialized = hwif->mate->serialized = 1;
1193                                 goto obsolete_option;
1194
1195                         case -1:
1196                         case 0:
1197                         case 1:
1198                         case 2:
1199                         case 3:
1200                                 goto bad_option;
1201                         default:
1202                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1203                                 return 1;
1204                 }
1205         }
1206 bad_option:
1207         printk(" -- BAD OPTION\n");
1208         return 1;
1209 obsolete_option:
1210         printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1211         return 1;
1212 bad_hwif:
1213         printk("-- NOT SUPPORTED ON ide%d", hw);
1214 done:
1215         printk("\n");
1216         return 1;
1217 }
1218
1219 EXPORT_SYMBOL(ide_lock);
1220
1221 static int ide_bus_match(struct device *dev, struct device_driver *drv)
1222 {
1223         return 1;
1224 }
1225
1226 static char *media_string(ide_drive_t *drive)
1227 {
1228         switch (drive->media) {
1229         case ide_disk:
1230                 return "disk";
1231         case ide_cdrom:
1232                 return "cdrom";
1233         case ide_tape:
1234                 return "tape";
1235         case ide_floppy:
1236                 return "floppy";
1237         case ide_optical:
1238                 return "optical";
1239         default:
1240                 return "UNKNOWN";
1241         }
1242 }
1243
1244 static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1245 {
1246         ide_drive_t *drive = to_ide_device(dev);
1247         return sprintf(buf, "%s\n", media_string(drive));
1248 }
1249
1250 static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1251 {
1252         ide_drive_t *drive = to_ide_device(dev);
1253         return sprintf(buf, "%s\n", drive->name);
1254 }
1255
1256 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1257 {
1258         ide_drive_t *drive = to_ide_device(dev);
1259         return sprintf(buf, "ide:m-%s\n", media_string(drive));
1260 }
1261
1262 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
1263                           char *buf)
1264 {
1265         ide_drive_t *drive = to_ide_device(dev);
1266         return sprintf(buf, "%s\n", drive->id->model);
1267 }
1268
1269 static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
1270                              char *buf)
1271 {
1272         ide_drive_t *drive = to_ide_device(dev);
1273         return sprintf(buf, "%s\n", drive->id->fw_rev);
1274 }
1275
1276 static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
1277                            char *buf)
1278 {
1279         ide_drive_t *drive = to_ide_device(dev);
1280         return sprintf(buf, "%s\n", drive->id->serial_no);
1281 }
1282
1283 static struct device_attribute ide_dev_attrs[] = {
1284         __ATTR_RO(media),
1285         __ATTR_RO(drivename),
1286         __ATTR_RO(modalias),
1287         __ATTR_RO(model),
1288         __ATTR_RO(firmware),
1289         __ATTR(serial, 0400, serial_show, NULL),
1290         __ATTR_NULL
1291 };
1292
1293 static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
1294 {
1295         ide_drive_t *drive = to_ide_device(dev);
1296
1297         add_uevent_var(env, "MEDIA=%s", media_string(drive));
1298         add_uevent_var(env, "DRIVENAME=%s", drive->name);
1299         add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
1300         return 0;
1301 }
1302
1303 static int generic_ide_probe(struct device *dev)
1304 {
1305         ide_drive_t *drive = to_ide_device(dev);
1306         ide_driver_t *drv = to_ide_driver(dev->driver);
1307
1308         return drv->probe ? drv->probe(drive) : -ENODEV;
1309 }
1310
1311 static int generic_ide_remove(struct device *dev)
1312 {
1313         ide_drive_t *drive = to_ide_device(dev);
1314         ide_driver_t *drv = to_ide_driver(dev->driver);
1315
1316         if (drv->remove)
1317                 drv->remove(drive);
1318
1319         return 0;
1320 }
1321
1322 static void generic_ide_shutdown(struct device *dev)
1323 {
1324         ide_drive_t *drive = to_ide_device(dev);
1325         ide_driver_t *drv = to_ide_driver(dev->driver);
1326
1327         if (dev->driver && drv->shutdown)
1328                 drv->shutdown(drive);
1329 }
1330
1331 struct bus_type ide_bus_type = {
1332         .name           = "ide",
1333         .match          = ide_bus_match,
1334         .uevent         = ide_uevent,
1335         .probe          = generic_ide_probe,
1336         .remove         = generic_ide_remove,
1337         .shutdown       = generic_ide_shutdown,
1338         .dev_attrs      = ide_dev_attrs,
1339         .suspend        = generic_ide_suspend,
1340         .resume         = generic_ide_resume,
1341 };
1342
1343 EXPORT_SYMBOL_GPL(ide_bus_type);
1344
1345 static void ide_port_class_release(struct device *portdev)
1346 {
1347         ide_hwif_t *hwif = dev_get_drvdata(portdev);
1348
1349         put_device(&hwif->gendev);
1350 }
1351
1352 /*
1353  * This is gets invoked once during initialization, to set *everything* up
1354  */
1355 static int __init ide_init(void)
1356 {
1357         int ret;
1358
1359         printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
1360         system_bus_speed = ide_system_bus_speed();
1361
1362         printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
1363                          "for PIO modes%s\n", system_bus_speed,
1364                         idebus_parameter ? "" : "; override with idebus=xx");
1365
1366         ret = bus_register(&ide_bus_type);
1367         if (ret < 0) {
1368                 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1369                 return ret;
1370         }
1371
1372         ide_port_class = class_create(THIS_MODULE, "ide_port");
1373         if (IS_ERR(ide_port_class)) {
1374                 ret = PTR_ERR(ide_port_class);
1375                 goto out_port_class;
1376         }
1377         ide_port_class->dev_release = ide_port_class_release;
1378
1379         init_ide_data();
1380
1381         proc_ide_create();
1382
1383         return 0;
1384
1385 out_port_class:
1386         bus_unregister(&ide_bus_type);
1387
1388         return ret;
1389 }
1390
1391 #ifdef MODULE
1392 static char *options = NULL;
1393 module_param(options, charp, 0);
1394 MODULE_LICENSE("GPL");
1395
1396 static void __init parse_options (char *line)
1397 {
1398         char *next = line;
1399
1400         if (line == NULL || !*line)
1401                 return;
1402         while ((line = next) != NULL) {
1403                 if ((next = strchr(line,' ')) != NULL)
1404                         *next++ = 0;
1405                 if (!ide_setup(line))
1406                         printk (KERN_INFO "Unknown option '%s'\n", line);
1407         }
1408 }
1409
1410 int __init init_module (void)
1411 {
1412         parse_options(options);
1413         return ide_init();
1414 }
1415
1416 void __exit cleanup_module (void)
1417 {
1418         int index;
1419
1420         for (index = 0; index < MAX_HWIFS; ++index)
1421                 ide_unregister(index);
1422
1423         proc_ide_destroy();
1424
1425         class_destroy(ide_port_class);
1426
1427         bus_unregister(&ide_bus_type);
1428 }
1429
1430 #else /* !MODULE */
1431
1432 __setup("", ide_setup);
1433
1434 module_init(ide_init);
1435
1436 #endif /* MODULE */