ide.c 34 KB

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