ide.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. unsigned int i;
  217. if (hwif->mmio)
  218. return 0;
  219. addr = hwif->io_ports[IDE_CONTROL_OFFSET];
  220. if (addr && !hwif_request_region(hwif, addr, 1))
  221. goto control_region_busy;
  222. hwif->straight8 = 0;
  223. addr = hwif->io_ports[IDE_DATA_OFFSET];
  224. if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
  225. if (!hwif_request_region(hwif, addr, 8))
  226. goto data_region_busy;
  227. hwif->straight8 = 1;
  228. return 0;
  229. }
  230. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
  231. addr = hwif->io_ports[i];
  232. if (!hwif_request_region(hwif, addr, 1)) {
  233. while (--i)
  234. release_region(addr, 1);
  235. goto data_region_busy;
  236. }
  237. }
  238. return 0;
  239. data_region_busy:
  240. addr = hwif->io_ports[IDE_CONTROL_OFFSET];
  241. if (addr)
  242. release_region(addr, 1);
  243. control_region_busy:
  244. /* If any errors are return, we drop the hwif interface. */
  245. return -EBUSY;
  246. }
  247. /**
  248. * ide_hwif_release_regions - free IDE resources
  249. *
  250. * Note that we only release the standard ports,
  251. * and do not even try to handle any extra ports
  252. * allocated for weird IDE interface chipsets.
  253. *
  254. * Note also that we don't yet handle mmio resources here. More
  255. * importantly our caller should be doing this so we need to
  256. * restructure this as a helper function for drivers.
  257. */
  258. void ide_hwif_release_regions(ide_hwif_t *hwif)
  259. {
  260. u32 i = 0;
  261. if (hwif->mmio)
  262. return;
  263. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  264. release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
  265. if (hwif->straight8) {
  266. release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
  267. return;
  268. }
  269. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
  270. if (hwif->io_ports[i])
  271. release_region(hwif->io_ports[i], 1);
  272. }
  273. void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
  274. {
  275. ide_hwgroup_t *hwgroup = hwif->hwgroup;
  276. spin_lock_irq(&ide_lock);
  277. /*
  278. * Remove us from the hwgroup, and free
  279. * the hwgroup if we were the only member
  280. */
  281. if (hwif->next == hwif) {
  282. BUG_ON(hwgroup->hwif != hwif);
  283. kfree(hwgroup);
  284. } else {
  285. /* There is another interface in hwgroup.
  286. * Unlink us, and set hwgroup->drive and ->hwif to
  287. * something sane.
  288. */
  289. ide_hwif_t *g = hwgroup->hwif;
  290. while (g->next != hwif)
  291. g = g->next;
  292. g->next = hwif->next;
  293. if (hwgroup->hwif == hwif) {
  294. /* Chose a random hwif for hwgroup->hwif.
  295. * It's guaranteed that there are no drives
  296. * left in the hwgroup.
  297. */
  298. BUG_ON(hwgroup->drive != NULL);
  299. hwgroup->hwif = g;
  300. }
  301. BUG_ON(hwgroup->hwif == hwif);
  302. }
  303. spin_unlock_irq(&ide_lock);
  304. }
  305. /* Called with ide_lock held. */
  306. static void __ide_port_unregister_devices(ide_hwif_t *hwif)
  307. {
  308. int i;
  309. for (i = 0; i < MAX_DRIVES; i++) {
  310. ide_drive_t *drive = &hwif->drives[i];
  311. if (drive->present) {
  312. spin_unlock_irq(&ide_lock);
  313. device_unregister(&drive->gendev);
  314. wait_for_completion(&drive->gendev_rel_comp);
  315. spin_lock_irq(&ide_lock);
  316. }
  317. }
  318. }
  319. void ide_port_unregister_devices(ide_hwif_t *hwif)
  320. {
  321. mutex_lock(&ide_cfg_mtx);
  322. spin_lock_irq(&ide_lock);
  323. __ide_port_unregister_devices(hwif);
  324. hwif->present = 0;
  325. ide_port_init_devices_data(hwif);
  326. spin_unlock_irq(&ide_lock);
  327. mutex_unlock(&ide_cfg_mtx);
  328. }
  329. EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
  330. /**
  331. * ide_unregister - free an IDE interface
  332. * @index: index of interface (will change soon to a pointer)
  333. *
  334. * Perform the final unregister of an IDE interface. At the moment
  335. * we don't refcount interfaces so this will also get split up.
  336. *
  337. * Locking:
  338. * The caller must not hold the IDE locks
  339. * The drive present/vanishing is not yet properly locked
  340. * Take care with the callbacks. These have been split to avoid
  341. * deadlocking the IDE layer. The shutdown callback is called
  342. * before we take the lock and free resources. It is up to the
  343. * caller to be sure there is no pending I/O here, and that
  344. * the interface will not be reopened (present/vanishing locking
  345. * isn't yet done BTW). After we commit to the final kill we
  346. * call the cleanup callback with the ide locks held.
  347. *
  348. * Unregister restores the hwif structures to the default state.
  349. * This is raving bonkers.
  350. */
  351. void ide_unregister(unsigned int index)
  352. {
  353. ide_hwif_t *hwif, *g;
  354. ide_hwgroup_t *hwgroup;
  355. int irq_count = 0;
  356. BUG_ON(index >= MAX_HWIFS);
  357. BUG_ON(in_interrupt());
  358. BUG_ON(irqs_disabled());
  359. mutex_lock(&ide_cfg_mtx);
  360. spin_lock_irq(&ide_lock);
  361. hwif = &ide_hwifs[index];
  362. if (!hwif->present)
  363. goto abort;
  364. __ide_port_unregister_devices(hwif);
  365. hwif->present = 0;
  366. spin_unlock_irq(&ide_lock);
  367. ide_proc_unregister_port(hwif);
  368. hwgroup = hwif->hwgroup;
  369. /*
  370. * free the irq if we were the only hwif using it
  371. */
  372. g = hwgroup->hwif;
  373. do {
  374. if (g->irq == hwif->irq)
  375. ++irq_count;
  376. g = g->next;
  377. } while (g != hwgroup->hwif);
  378. if (irq_count == 1)
  379. free_irq(hwif->irq, hwgroup);
  380. ide_remove_port_from_hwgroup(hwif);
  381. device_unregister(hwif->portdev);
  382. device_unregister(&hwif->gendev);
  383. wait_for_completion(&hwif->gendev_rel_comp);
  384. /*
  385. * Remove us from the kernel's knowledge
  386. */
  387. blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
  388. kfree(hwif->sg_table);
  389. unregister_blkdev(hwif->major, hwif->name);
  390. spin_lock_irq(&ide_lock);
  391. if (hwif->dma_base)
  392. (void)ide_release_dma(hwif);
  393. ide_hwif_release_regions(hwif);
  394. /* restore hwif data to pristine status */
  395. ide_init_port_data(hwif, index);
  396. abort:
  397. spin_unlock_irq(&ide_lock);
  398. mutex_unlock(&ide_cfg_mtx);
  399. }
  400. EXPORT_SYMBOL(ide_unregister);
  401. void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
  402. {
  403. memcpy(hwif->io_ports, hw->io_ports, sizeof(hwif->io_ports));
  404. hwif->irq = hw->irq;
  405. hwif->noprobe = 0;
  406. hwif->chipset = hw->chipset;
  407. hwif->gendev.parent = hw->dev;
  408. hwif->ack_intr = hw->ack_intr;
  409. }
  410. EXPORT_SYMBOL_GPL(ide_init_port_hw);
  411. /*
  412. * Locks for IDE setting functionality
  413. */
  414. DEFINE_MUTEX(ide_setting_mtx);
  415. EXPORT_SYMBOL_GPL(ide_setting_mtx);
  416. /**
  417. * ide_spin_wait_hwgroup - wait for group
  418. * @drive: drive in the group
  419. *
  420. * Wait for an IDE device group to go non busy and then return
  421. * holding the ide_lock which guards the hwgroup->busy status
  422. * and right to use it.
  423. */
  424. int ide_spin_wait_hwgroup (ide_drive_t *drive)
  425. {
  426. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  427. unsigned long timeout = jiffies + (3 * HZ);
  428. spin_lock_irq(&ide_lock);
  429. while (hwgroup->busy) {
  430. unsigned long lflags;
  431. spin_unlock_irq(&ide_lock);
  432. local_irq_set(lflags);
  433. if (time_after(jiffies, timeout)) {
  434. local_irq_restore(lflags);
  435. printk(KERN_ERR "%s: channel busy\n", drive->name);
  436. return -EBUSY;
  437. }
  438. local_irq_restore(lflags);
  439. spin_lock_irq(&ide_lock);
  440. }
  441. return 0;
  442. }
  443. EXPORT_SYMBOL(ide_spin_wait_hwgroup);
  444. int set_io_32bit(ide_drive_t *drive, int arg)
  445. {
  446. if (drive->no_io_32bit)
  447. return -EPERM;
  448. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  449. return -EINVAL;
  450. if (ide_spin_wait_hwgroup(drive))
  451. return -EBUSY;
  452. drive->io_32bit = arg;
  453. spin_unlock_irq(&ide_lock);
  454. return 0;
  455. }
  456. static int set_ksettings(ide_drive_t *drive, int arg)
  457. {
  458. if (arg < 0 || arg > 1)
  459. return -EINVAL;
  460. if (ide_spin_wait_hwgroup(drive))
  461. return -EBUSY;
  462. drive->keep_settings = arg;
  463. spin_unlock_irq(&ide_lock);
  464. return 0;
  465. }
  466. int set_using_dma(ide_drive_t *drive, int arg)
  467. {
  468. #ifdef CONFIG_BLK_DEV_IDEDMA
  469. ide_hwif_t *hwif = drive->hwif;
  470. int err = -EPERM;
  471. if (arg < 0 || arg > 1)
  472. return -EINVAL;
  473. if (!drive->id || !(drive->id->capability & 1))
  474. goto out;
  475. if (hwif->dma_host_set == NULL)
  476. goto out;
  477. err = -EBUSY;
  478. if (ide_spin_wait_hwgroup(drive))
  479. goto out;
  480. /*
  481. * set ->busy flag, unlock and let it ride
  482. */
  483. hwif->hwgroup->busy = 1;
  484. spin_unlock_irq(&ide_lock);
  485. err = 0;
  486. if (arg) {
  487. if (ide_set_dma(drive))
  488. err = -EIO;
  489. } else
  490. ide_dma_off(drive);
  491. /*
  492. * lock, clear ->busy flag and unlock before leaving
  493. */
  494. spin_lock_irq(&ide_lock);
  495. hwif->hwgroup->busy = 0;
  496. spin_unlock_irq(&ide_lock);
  497. out:
  498. return err;
  499. #else
  500. if (arg < 0 || arg > 1)
  501. return -EINVAL;
  502. return -EPERM;
  503. #endif
  504. }
  505. int set_pio_mode(ide_drive_t *drive, int arg)
  506. {
  507. struct request rq;
  508. if (arg < 0 || arg > 255)
  509. return -EINVAL;
  510. if (drive->hwif->set_pio_mode == NULL)
  511. return -ENOSYS;
  512. if (drive->special.b.set_tune)
  513. return -EBUSY;
  514. ide_init_drive_cmd(&rq);
  515. rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
  516. drive->tune_req = (u8) arg;
  517. drive->special.b.set_tune = 1;
  518. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  519. return 0;
  520. }
  521. static int set_unmaskirq(ide_drive_t *drive, int arg)
  522. {
  523. if (drive->no_unmask)
  524. return -EPERM;
  525. if (arg < 0 || arg > 1)
  526. return -EINVAL;
  527. if (ide_spin_wait_hwgroup(drive))
  528. return -EBUSY;
  529. drive->unmask = arg;
  530. spin_unlock_irq(&ide_lock);
  531. return 0;
  532. }
  533. /**
  534. * system_bus_clock - clock guess
  535. *
  536. * External version of the bus clock guess used by very old IDE drivers
  537. * for things like VLB timings. Should not be used.
  538. */
  539. int system_bus_clock (void)
  540. {
  541. return system_bus_speed;
  542. }
  543. EXPORT_SYMBOL(system_bus_clock);
  544. static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
  545. {
  546. ide_drive_t *drive = dev->driver_data;
  547. ide_hwif_t *hwif = HWIF(drive);
  548. struct request rq;
  549. struct request_pm_state rqpm;
  550. ide_task_t args;
  551. int ret;
  552. /* Call ACPI _GTM only once */
  553. if (!(drive->dn % 2))
  554. ide_acpi_get_timing(hwif);
  555. memset(&rq, 0, sizeof(rq));
  556. memset(&rqpm, 0, sizeof(rqpm));
  557. memset(&args, 0, sizeof(args));
  558. rq.cmd_type = REQ_TYPE_PM_SUSPEND;
  559. rq.special = &args;
  560. rq.data = &rqpm;
  561. rqpm.pm_step = ide_pm_state_start_suspend;
  562. if (mesg.event == PM_EVENT_PRETHAW)
  563. mesg.event = PM_EVENT_FREEZE;
  564. rqpm.pm_state = mesg.event;
  565. ret = ide_do_drive_cmd(drive, &rq, ide_wait);
  566. /* only call ACPI _PS3 after both drivers are suspended */
  567. if (!ret && (((drive->dn % 2) && hwif->drives[0].present
  568. && hwif->drives[1].present)
  569. || !hwif->drives[0].present
  570. || !hwif->drives[1].present))
  571. ide_acpi_set_state(hwif, 0);
  572. return ret;
  573. }
  574. static int generic_ide_resume(struct device *dev)
  575. {
  576. ide_drive_t *drive = dev->driver_data;
  577. ide_hwif_t *hwif = HWIF(drive);
  578. struct request rq;
  579. struct request_pm_state rqpm;
  580. ide_task_t args;
  581. int err;
  582. /* Call ACPI _STM only once */
  583. if (!(drive->dn % 2)) {
  584. ide_acpi_set_state(hwif, 1);
  585. ide_acpi_push_timing(hwif);
  586. }
  587. ide_acpi_exec_tfs(drive);
  588. memset(&rq, 0, sizeof(rq));
  589. memset(&rqpm, 0, sizeof(rqpm));
  590. memset(&args, 0, sizeof(args));
  591. rq.cmd_type = REQ_TYPE_PM_RESUME;
  592. rq.special = &args;
  593. rq.data = &rqpm;
  594. rqpm.pm_step = ide_pm_state_start_resume;
  595. rqpm.pm_state = PM_EVENT_ON;
  596. err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
  597. if (err == 0 && dev->driver) {
  598. ide_driver_t *drv = to_ide_driver(dev->driver);
  599. if (drv->resume)
  600. drv->resume(drive);
  601. }
  602. return err;
  603. }
  604. int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
  605. unsigned int cmd, unsigned long arg)
  606. {
  607. unsigned long flags;
  608. ide_driver_t *drv;
  609. void __user *p = (void __user *)arg;
  610. int err = 0, (*setfunc)(ide_drive_t *, int);
  611. u8 *val;
  612. switch (cmd) {
  613. case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val;
  614. case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
  615. case HDIO_GET_UNMASKINTR: val = &drive->unmask; goto read_val;
  616. case HDIO_GET_DMA: val = &drive->using_dma; goto read_val;
  617. case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
  618. case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
  619. case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
  620. case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
  621. case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
  622. }
  623. switch (cmd) {
  624. case HDIO_OBSOLETE_IDENTITY:
  625. case HDIO_GET_IDENTITY:
  626. if (bdev != bdev->bd_contains)
  627. return -EINVAL;
  628. if (drive->id_read == 0)
  629. return -ENOMSG;
  630. if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
  631. return -EFAULT;
  632. return 0;
  633. case HDIO_GET_NICE:
  634. return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
  635. drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
  636. drive->nice1 << IDE_NICE_1,
  637. (long __user *) arg);
  638. #ifdef CONFIG_IDE_TASK_IOCTL
  639. case HDIO_DRIVE_TASKFILE:
  640. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  641. return -EACCES;
  642. switch(drive->media) {
  643. case ide_disk:
  644. return ide_taskfile_ioctl(drive, cmd, arg);
  645. default:
  646. return -ENOMSG;
  647. }
  648. #endif /* CONFIG_IDE_TASK_IOCTL */
  649. case HDIO_DRIVE_CMD:
  650. if (!capable(CAP_SYS_RAWIO))
  651. return -EACCES;
  652. return ide_cmd_ioctl(drive, cmd, arg);
  653. case HDIO_DRIVE_TASK:
  654. if (!capable(CAP_SYS_RAWIO))
  655. return -EACCES;
  656. return ide_task_ioctl(drive, cmd, arg);
  657. case HDIO_SET_NICE:
  658. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  659. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  660. return -EPERM;
  661. drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
  662. drv = *(ide_driver_t **)bdev->bd_disk->private_data;
  663. if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
  664. drive->dsc_overlap = 0;
  665. return -EPERM;
  666. }
  667. drive->nice1 = (arg >> IDE_NICE_1) & 1;
  668. return 0;
  669. case HDIO_DRIVE_RESET:
  670. if (!capable(CAP_SYS_ADMIN))
  671. return -EACCES;
  672. /*
  673. * Abort the current command on the
  674. * group if there is one, taking
  675. * care not to allow anything else
  676. * to be queued and to die on the
  677. * spot if we miss one somehow
  678. */
  679. spin_lock_irqsave(&ide_lock, flags);
  680. if (HWGROUP(drive)->resetting) {
  681. spin_unlock_irqrestore(&ide_lock, flags);
  682. return -EBUSY;
  683. }
  684. ide_abort(drive, "drive reset");
  685. BUG_ON(HWGROUP(drive)->handler);
  686. /* Ensure nothing gets queued after we
  687. drop the lock. Reset will clear the busy */
  688. HWGROUP(drive)->busy = 1;
  689. spin_unlock_irqrestore(&ide_lock, flags);
  690. (void) ide_do_reset(drive);
  691. return 0;
  692. case HDIO_GET_BUSSTATE:
  693. if (!capable(CAP_SYS_ADMIN))
  694. return -EACCES;
  695. if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
  696. return -EFAULT;
  697. return 0;
  698. case HDIO_SET_BUSSTATE:
  699. if (!capable(CAP_SYS_ADMIN))
  700. return -EACCES;
  701. return -EOPNOTSUPP;
  702. default:
  703. return -EINVAL;
  704. }
  705. read_val:
  706. mutex_lock(&ide_setting_mtx);
  707. spin_lock_irqsave(&ide_lock, flags);
  708. err = *val;
  709. spin_unlock_irqrestore(&ide_lock, flags);
  710. mutex_unlock(&ide_setting_mtx);
  711. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  712. set_val:
  713. if (bdev != bdev->bd_contains)
  714. err = -EINVAL;
  715. else {
  716. if (!capable(CAP_SYS_ADMIN))
  717. err = -EACCES;
  718. else {
  719. mutex_lock(&ide_setting_mtx);
  720. err = setfunc(drive, arg);
  721. mutex_unlock(&ide_setting_mtx);
  722. }
  723. }
  724. return err;
  725. }
  726. EXPORT_SYMBOL(generic_ide_ioctl);
  727. /*
  728. * stridx() returns the offset of c within s,
  729. * or -1 if c is '\0' or not found within s.
  730. */
  731. static int __init stridx (const char *s, char c)
  732. {
  733. char *i = strchr(s, c);
  734. return (i && c) ? i - s : -1;
  735. }
  736. /*
  737. * match_parm() does parsing for ide_setup():
  738. *
  739. * 1. the first char of s must be '='.
  740. * 2. if the remainder matches one of the supplied keywords,
  741. * the index (1 based) of the keyword is negated and returned.
  742. * 3. if the remainder is a series of no more than max_vals numbers
  743. * separated by commas, the numbers are saved in vals[] and a
  744. * count of how many were saved is returned. Base10 is assumed,
  745. * and base16 is allowed when prefixed with "0x".
  746. * 4. otherwise, zero is returned.
  747. */
  748. static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
  749. {
  750. static const char *decimal = "0123456789";
  751. static const char *hex = "0123456789abcdef";
  752. int i, n;
  753. if (*s++ == '=') {
  754. /*
  755. * Try matching against the supplied keywords,
  756. * and return -(index+1) if we match one
  757. */
  758. if (keywords != NULL) {
  759. for (i = 0; *keywords != NULL; ++i) {
  760. if (!strcmp(s, *keywords++))
  761. return -(i+1);
  762. }
  763. }
  764. /*
  765. * Look for a series of no more than "max_vals"
  766. * numeric values separated by commas, in base10,
  767. * or base16 when prefixed with "0x".
  768. * Return a count of how many were found.
  769. */
  770. for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
  771. vals[n] = i;
  772. while ((i = stridx(decimal, *++s)) >= 0)
  773. vals[n] = (vals[n] * 10) + i;
  774. if (*s == 'x' && !vals[n]) {
  775. while ((i = stridx(hex, *++s)) >= 0)
  776. vals[n] = (vals[n] * 0x10) + i;
  777. }
  778. if (++n == max_vals)
  779. break;
  780. if (*s == ',' || *s == ';')
  781. ++s;
  782. }
  783. if (!*s)
  784. return n;
  785. }
  786. return 0; /* zero = nothing matched */
  787. }
  788. extern int probe_ali14xx;
  789. extern int probe_umc8672;
  790. extern int probe_dtc2278;
  791. extern int probe_ht6560b;
  792. extern int probe_qd65xx;
  793. extern int cmd640_vlb;
  794. extern int probe_4drives;
  795. static int __initdata is_chipset_set;
  796. /*
  797. * ide_setup() gets called VERY EARLY during initialization,
  798. * to handle kernel "command line" strings beginning with "hdx=" or "ide".
  799. *
  800. * Remember to update Documentation/ide/ide.txt if you change something here.
  801. */
  802. static int __init ide_setup(char *s)
  803. {
  804. int i, vals[3];
  805. ide_hwif_t *hwif;
  806. ide_drive_t *drive;
  807. unsigned int hw, unit;
  808. const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
  809. const char max_hwif = '0' + (MAX_HWIFS - 1);
  810. if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
  811. return 0; /* driver and not us */
  812. if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
  813. return 0;
  814. printk(KERN_INFO "ide_setup: %s", s);
  815. init_ide_data ();
  816. #ifdef CONFIG_BLK_DEV_IDEDOUBLER
  817. if (!strcmp(s, "ide=doubler")) {
  818. extern int ide_doubler;
  819. printk(" : Enabled support for IDE doublers\n");
  820. ide_doubler = 1;
  821. return 1;
  822. }
  823. #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
  824. if (!strcmp(s, "ide=nodma")) {
  825. printk(" : Prevented DMA\n");
  826. noautodma = 1;
  827. goto obsolete_option;
  828. }
  829. #ifdef CONFIG_BLK_DEV_IDEACPI
  830. if (!strcmp(s, "ide=noacpi")) {
  831. //printk(" : Disable IDE ACPI support.\n");
  832. ide_noacpi = 1;
  833. return 1;
  834. }
  835. if (!strcmp(s, "ide=acpigtf")) {
  836. //printk(" : Enable IDE ACPI _GTF support.\n");
  837. ide_noacpitfs = 0;
  838. return 1;
  839. }
  840. if (!strcmp(s, "ide=acpionboot")) {
  841. //printk(" : Call IDE ACPI methods on boot.\n");
  842. ide_noacpionboot = 0;
  843. return 1;
  844. }
  845. #endif /* CONFIG_BLK_DEV_IDEACPI */
  846. /*
  847. * Look for drive options: "hdx="
  848. */
  849. if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
  850. const char *hd_words[] = {
  851. "none", "noprobe", "nowerr", "cdrom", "nodma",
  852. "autotune", "noautotune", "-8", "-9", "-10",
  853. "noflush", "remap", "remap63", "scsi", NULL };
  854. unit = s[2] - 'a';
  855. hw = unit / MAX_DRIVES;
  856. unit = unit % MAX_DRIVES;
  857. hwif = &ide_hwifs[hw];
  858. drive = &hwif->drives[unit];
  859. if (strncmp(s + 4, "ide-", 4) == 0) {
  860. strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
  861. goto obsolete_option;
  862. }
  863. switch (match_parm(&s[3], hd_words, vals, 3)) {
  864. case -1: /* "none" */
  865. case -2: /* "noprobe" */
  866. drive->noprobe = 1;
  867. goto done;
  868. case -3: /* "nowerr" */
  869. drive->bad_wstat = BAD_R_STAT;
  870. hwif->noprobe = 0;
  871. goto done;
  872. case -4: /* "cdrom" */
  873. drive->present = 1;
  874. drive->media = ide_cdrom;
  875. /* an ATAPI device ignores DRDY */
  876. drive->ready_stat = 0;
  877. hwif->noprobe = 0;
  878. goto done;
  879. case -5: /* nodma */
  880. drive->nodma = 1;
  881. goto done;
  882. case -6: /* "autotune" */
  883. drive->autotune = IDE_TUNE_AUTO;
  884. goto obsolete_option;
  885. case -7: /* "noautotune" */
  886. drive->autotune = IDE_TUNE_NOAUTO;
  887. goto obsolete_option;
  888. case -11: /* noflush */
  889. drive->noflush = 1;
  890. goto done;
  891. case -12: /* "remap" */
  892. drive->remap_0_to_1 = 1;
  893. goto obsolete_option;
  894. case -13: /* "remap63" */
  895. drive->sect0 = 63;
  896. goto obsolete_option;
  897. case -14: /* "scsi" */
  898. drive->scsi = 1;
  899. goto obsolete_option;
  900. case 3: /* cyl,head,sect */
  901. drive->media = ide_disk;
  902. drive->ready_stat = READY_STAT;
  903. drive->cyl = drive->bios_cyl = vals[0];
  904. drive->head = drive->bios_head = vals[1];
  905. drive->sect = drive->bios_sect = vals[2];
  906. drive->present = 1;
  907. drive->forced_geom = 1;
  908. hwif->noprobe = 0;
  909. goto done;
  910. default:
  911. goto bad_option;
  912. }
  913. }
  914. if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
  915. goto bad_option;
  916. /*
  917. * Look for bus speed option: "idebus="
  918. */
  919. if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
  920. if (match_parm(&s[6], NULL, vals, 1) != 1)
  921. goto bad_option;
  922. if (vals[0] >= 20 && vals[0] <= 66) {
  923. idebus_parameter = vals[0];
  924. } else
  925. printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
  926. goto done;
  927. }
  928. /*
  929. * Look for interface options: "idex="
  930. */
  931. if (s[3] >= '0' && s[3] <= max_hwif) {
  932. /*
  933. * Be VERY CAREFUL changing this: note hardcoded indexes below
  934. * (-8, -9, -10) are reserved to ease the hardcoding.
  935. */
  936. static const char *ide_words[] = {
  937. "minus1", "serialize", "minus3", "minus4",
  938. "reset", "minus6", "ata66", "minus8", "minus9",
  939. "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
  940. "dtc2278", "umc8672", "ali14xx", NULL };
  941. hw = s[3] - '0';
  942. hwif = &ide_hwifs[hw];
  943. i = match_parm(&s[4], ide_words, vals, 3);
  944. /*
  945. * Cryptic check to ensure chipset not already set for hwif.
  946. * Note: we can't depend on hwif->chipset here.
  947. */
  948. if (i >= -18 && i <= -11) {
  949. /* chipset already specified */
  950. if (is_chipset_set)
  951. goto bad_option;
  952. /* these drivers are for "ide0=" only */
  953. if (hw != 0)
  954. goto bad_hwif;
  955. is_chipset_set = 1;
  956. printk("\n");
  957. }
  958. switch (i) {
  959. #ifdef CONFIG_BLK_DEV_ALI14XX
  960. case -17: /* "ali14xx" */
  961. probe_ali14xx = 1;
  962. goto obsolete_option;
  963. #endif
  964. #ifdef CONFIG_BLK_DEV_UMC8672
  965. case -16: /* "umc8672" */
  966. probe_umc8672 = 1;
  967. goto obsolete_option;
  968. #endif
  969. #ifdef CONFIG_BLK_DEV_DTC2278
  970. case -15: /* "dtc2278" */
  971. probe_dtc2278 = 1;
  972. goto obsolete_option;
  973. #endif
  974. #ifdef CONFIG_BLK_DEV_CMD640
  975. case -14: /* "cmd640_vlb" */
  976. cmd640_vlb = 1;
  977. goto obsolete_option;
  978. #endif
  979. #ifdef CONFIG_BLK_DEV_HT6560B
  980. case -13: /* "ht6560b" */
  981. probe_ht6560b = 1;
  982. goto obsolete_option;
  983. #endif
  984. #ifdef CONFIG_BLK_DEV_QD65XX
  985. case -12: /* "qd65xx" */
  986. probe_qd65xx = 1;
  987. goto obsolete_option;
  988. #endif
  989. #ifdef CONFIG_BLK_DEV_4DRIVES
  990. case -11: /* "four" drives on one set of ports */
  991. probe_4drives = 1;
  992. goto obsolete_option;
  993. #endif
  994. case -10: /* minus10 */
  995. case -9: /* minus9 */
  996. case -8: /* minus8 */
  997. case -6:
  998. case -4:
  999. case -3:
  1000. goto bad_option;
  1001. case -7: /* ata66 */
  1002. #ifdef CONFIG_BLK_DEV_IDEPCI
  1003. /*
  1004. * Use ATA_CBL_PATA40_SHORT so drive side
  1005. * cable detection is also overriden.
  1006. */
  1007. hwif->cbl = ATA_CBL_PATA40_SHORT;
  1008. goto obsolete_option;
  1009. #else
  1010. goto bad_hwif;
  1011. #endif
  1012. case -5: /* "reset" */
  1013. hwif->reset = 1;
  1014. goto obsolete_option;
  1015. case -2: /* "serialize" */
  1016. hwif->mate = &ide_hwifs[hw^1];
  1017. hwif->mate->mate = hwif;
  1018. hwif->serialized = hwif->mate->serialized = 1;
  1019. goto obsolete_option;
  1020. case -1:
  1021. case 0:
  1022. case 1:
  1023. case 2:
  1024. case 3:
  1025. goto bad_option;
  1026. default:
  1027. printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
  1028. return 1;
  1029. }
  1030. }
  1031. bad_option:
  1032. printk(" -- BAD OPTION\n");
  1033. return 1;
  1034. obsolete_option:
  1035. printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
  1036. return 1;
  1037. bad_hwif:
  1038. printk("-- NOT SUPPORTED ON ide%d", hw);
  1039. done:
  1040. printk("\n");
  1041. return 1;
  1042. }
  1043. EXPORT_SYMBOL(ide_lock);
  1044. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  1045. {
  1046. return 1;
  1047. }
  1048. static char *media_string(ide_drive_t *drive)
  1049. {
  1050. switch (drive->media) {
  1051. case ide_disk:
  1052. return "disk";
  1053. case ide_cdrom:
  1054. return "cdrom";
  1055. case ide_tape:
  1056. return "tape";
  1057. case ide_floppy:
  1058. return "floppy";
  1059. case ide_optical:
  1060. return "optical";
  1061. default:
  1062. return "UNKNOWN";
  1063. }
  1064. }
  1065. static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
  1066. {
  1067. ide_drive_t *drive = to_ide_device(dev);
  1068. return sprintf(buf, "%s\n", media_string(drive));
  1069. }
  1070. static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
  1071. {
  1072. ide_drive_t *drive = to_ide_device(dev);
  1073. return sprintf(buf, "%s\n", drive->name);
  1074. }
  1075. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  1076. {
  1077. ide_drive_t *drive = to_ide_device(dev);
  1078. return sprintf(buf, "ide:m-%s\n", media_string(drive));
  1079. }
  1080. static ssize_t model_show(struct device *dev, struct device_attribute *attr,
  1081. char *buf)
  1082. {
  1083. ide_drive_t *drive = to_ide_device(dev);
  1084. return sprintf(buf, "%s\n", drive->id->model);
  1085. }
  1086. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  1087. char *buf)
  1088. {
  1089. ide_drive_t *drive = to_ide_device(dev);
  1090. return sprintf(buf, "%s\n", drive->id->fw_rev);
  1091. }
  1092. static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
  1093. char *buf)
  1094. {
  1095. ide_drive_t *drive = to_ide_device(dev);
  1096. return sprintf(buf, "%s\n", drive->id->serial_no);
  1097. }
  1098. static struct device_attribute ide_dev_attrs[] = {
  1099. __ATTR_RO(media),
  1100. __ATTR_RO(drivename),
  1101. __ATTR_RO(modalias),
  1102. __ATTR_RO(model),
  1103. __ATTR_RO(firmware),
  1104. __ATTR(serial, 0400, serial_show, NULL),
  1105. __ATTR_NULL
  1106. };
  1107. static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
  1108. {
  1109. ide_drive_t *drive = to_ide_device(dev);
  1110. add_uevent_var(env, "MEDIA=%s", media_string(drive));
  1111. add_uevent_var(env, "DRIVENAME=%s", drive->name);
  1112. add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
  1113. return 0;
  1114. }
  1115. static int generic_ide_probe(struct device *dev)
  1116. {
  1117. ide_drive_t *drive = to_ide_device(dev);
  1118. ide_driver_t *drv = to_ide_driver(dev->driver);
  1119. return drv->probe ? drv->probe(drive) : -ENODEV;
  1120. }
  1121. static int generic_ide_remove(struct device *dev)
  1122. {
  1123. ide_drive_t *drive = to_ide_device(dev);
  1124. ide_driver_t *drv = to_ide_driver(dev->driver);
  1125. if (drv->remove)
  1126. drv->remove(drive);
  1127. return 0;
  1128. }
  1129. static void generic_ide_shutdown(struct device *dev)
  1130. {
  1131. ide_drive_t *drive = to_ide_device(dev);
  1132. ide_driver_t *drv = to_ide_driver(dev->driver);
  1133. if (dev->driver && drv->shutdown)
  1134. drv->shutdown(drive);
  1135. }
  1136. struct bus_type ide_bus_type = {
  1137. .name = "ide",
  1138. .match = ide_bus_match,
  1139. .uevent = ide_uevent,
  1140. .probe = generic_ide_probe,
  1141. .remove = generic_ide_remove,
  1142. .shutdown = generic_ide_shutdown,
  1143. .dev_attrs = ide_dev_attrs,
  1144. .suspend = generic_ide_suspend,
  1145. .resume = generic_ide_resume,
  1146. };
  1147. EXPORT_SYMBOL_GPL(ide_bus_type);
  1148. static void ide_port_class_release(struct device *portdev)
  1149. {
  1150. ide_hwif_t *hwif = dev_get_drvdata(portdev);
  1151. put_device(&hwif->gendev);
  1152. }
  1153. /*
  1154. * This is gets invoked once during initialization, to set *everything* up
  1155. */
  1156. static int __init ide_init(void)
  1157. {
  1158. int ret;
  1159. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
  1160. system_bus_speed = ide_system_bus_speed();
  1161. printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
  1162. "for PIO modes%s\n", system_bus_speed,
  1163. idebus_parameter ? "" : "; override with idebus=xx");
  1164. ret = bus_register(&ide_bus_type);
  1165. if (ret < 0) {
  1166. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  1167. return ret;
  1168. }
  1169. ide_port_class = class_create(THIS_MODULE, "ide_port");
  1170. if (IS_ERR(ide_port_class)) {
  1171. ret = PTR_ERR(ide_port_class);
  1172. goto out_port_class;
  1173. }
  1174. ide_port_class->dev_release = ide_port_class_release;
  1175. init_ide_data();
  1176. proc_ide_create();
  1177. return 0;
  1178. out_port_class:
  1179. bus_unregister(&ide_bus_type);
  1180. return ret;
  1181. }
  1182. #ifdef MODULE
  1183. static char *options = NULL;
  1184. module_param(options, charp, 0);
  1185. MODULE_LICENSE("GPL");
  1186. static void __init parse_options (char *line)
  1187. {
  1188. char *next = line;
  1189. if (line == NULL || !*line)
  1190. return;
  1191. while ((line = next) != NULL) {
  1192. if ((next = strchr(line,' ')) != NULL)
  1193. *next++ = 0;
  1194. if (!ide_setup(line))
  1195. printk (KERN_INFO "Unknown option '%s'\n", line);
  1196. }
  1197. }
  1198. int __init init_module (void)
  1199. {
  1200. parse_options(options);
  1201. return ide_init();
  1202. }
  1203. void __exit cleanup_module (void)
  1204. {
  1205. int index;
  1206. for (index = 0; index < MAX_HWIFS; ++index)
  1207. ide_unregister(index);
  1208. proc_ide_destroy();
  1209. class_destroy(ide_port_class);
  1210. bus_unregister(&ide_bus_type);
  1211. }
  1212. #else /* !MODULE */
  1213. __setup("", ide_setup);
  1214. module_init(ide_init);
  1215. #endif /* MODULE */