ide.c 35 KB

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