ide-cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*======================================================================
  2. A driver for PCMCIA IDE/ATA disk cards
  3. ide-cs.c 1.3 2002/10/26 05:45:31
  4. The contents of this file are subject to the Mozilla Public
  5. License Version 1.1 (the "License"); you may not use this file
  6. except in compliance with the License. You may obtain a copy of
  7. the License at http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS
  9. IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. implied. See the License for the specific language governing
  11. rights and limitations under the License.
  12. The initial developer of the original code is David A. Hinds
  13. <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  14. are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  15. Alternatively, the contents of this file may be used under the
  16. terms of the GNU General Public License version 2 (the "GPL"), in
  17. which case the provisions of the GPL are applicable instead of the
  18. above. If you wish to allow the use of your version of this file
  19. only under the terms of the GPL and not to allow others to use
  20. your version of this file under the MPL, indicate your decision
  21. by deleting the provisions above and replace them with the notice
  22. and other provisions required by the GPL. If you do not delete
  23. the provisions above, a recipient may use your version of this
  24. file under either the MPL or the GPL.
  25. ======================================================================*/
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/sched.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/slab.h>
  32. #include <linux/string.h>
  33. #include <linux/timer.h>
  34. #include <linux/ioport.h>
  35. #include <linux/ide.h>
  36. #include <linux/hdreg.h>
  37. #include <linux/major.h>
  38. #include <linux/delay.h>
  39. #include <asm/io.h>
  40. #include <asm/system.h>
  41. #include <pcmcia/cs_types.h>
  42. #include <pcmcia/cs.h>
  43. #include <pcmcia/cistpl.h>
  44. #include <pcmcia/ds.h>
  45. #include <pcmcia/cisreg.h>
  46. #include <pcmcia/ciscode.h>
  47. /*====================================================================*/
  48. /* Module parameters */
  49. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  50. MODULE_DESCRIPTION("PCMCIA ATA/IDE card driver");
  51. MODULE_LICENSE("Dual MPL/GPL");
  52. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  53. #ifdef PCMCIA_DEBUG
  54. INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
  55. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  56. static char *version =
  57. "ide-cs.c 1.3 2002/10/26 05:45:31 (David Hinds)";
  58. #else
  59. #define DEBUG(n, args...)
  60. #endif
  61. /*====================================================================*/
  62. static const char ide_major[] = {
  63. IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR,
  64. IDE4_MAJOR, IDE5_MAJOR
  65. };
  66. typedef struct ide_info_t {
  67. struct pcmcia_device *p_dev;
  68. int ndev;
  69. dev_node_t node;
  70. int hd;
  71. } ide_info_t;
  72. static void ide_release(struct pcmcia_device *);
  73. static void ide_config(struct pcmcia_device *);
  74. static void ide_detach(struct pcmcia_device *p_dev);
  75. /*======================================================================
  76. ide_attach() creates an "instance" of the driver, allocating
  77. local data structures for one device. The device is registered
  78. with Card Services.
  79. ======================================================================*/
  80. static int ide_attach(struct pcmcia_device *link)
  81. {
  82. ide_info_t *info;
  83. DEBUG(0, "ide_attach()\n");
  84. /* Create new ide device */
  85. info = kzalloc(sizeof(*info), GFP_KERNEL);
  86. if (!info)
  87. return -ENOMEM;
  88. info->p_dev = link;
  89. link->priv = info;
  90. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  91. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  92. link->io.IOAddrLines = 3;
  93. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  94. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  95. link->conf.Attributes = CONF_ENABLE_IRQ;
  96. link->conf.IntType = INT_MEMORY_AND_IO;
  97. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  98. ide_config(link);
  99. return 0;
  100. } /* ide_attach */
  101. /*======================================================================
  102. This deletes a driver "instance". The device is de-registered
  103. with Card Services. If it has been released, all local data
  104. structures are freed. Otherwise, the structures will be freed
  105. when the device is released.
  106. ======================================================================*/
  107. static void ide_detach(struct pcmcia_device *link)
  108. {
  109. DEBUG(0, "ide_detach(0x%p)\n", link);
  110. if (link->state & DEV_CONFIG)
  111. ide_release(link);
  112. kfree(link->priv);
  113. } /* ide_detach */
  114. static int idecs_register(unsigned long io, unsigned long ctl, unsigned long irq, struct pcmcia_device *handle)
  115. {
  116. hw_regs_t hw;
  117. memset(&hw, 0, sizeof(hw));
  118. ide_init_hwif_ports(&hw, io, ctl, NULL);
  119. hw.irq = irq;
  120. hw.chipset = ide_pci;
  121. hw.dev = &handle->dev;
  122. return ide_register_hw_with_fixup(&hw, NULL, ide_undecoded_slave);
  123. }
  124. /*======================================================================
  125. ide_config() is scheduled to run after a CARD_INSERTION event
  126. is received, to configure the PCMCIA socket, and to make the
  127. ide device available to the system.
  128. ======================================================================*/
  129. #define CS_CHECK(fn, ret) \
  130. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  131. static void ide_config(struct pcmcia_device *link)
  132. {
  133. ide_info_t *info = link->priv;
  134. tuple_t tuple;
  135. struct {
  136. u_short buf[128];
  137. cisparse_t parse;
  138. config_info_t conf;
  139. cistpl_cftable_entry_t dflt;
  140. } *stk = NULL;
  141. cistpl_cftable_entry_t *cfg;
  142. int i, pass, last_ret = 0, last_fn = 0, hd, is_kme = 0;
  143. unsigned long io_base, ctl_base;
  144. DEBUG(0, "ide_config(0x%p)\n", link);
  145. stk = kzalloc(sizeof(*stk), GFP_KERNEL);
  146. if (!stk) goto err_mem;
  147. cfg = &stk->parse.cftable_entry;
  148. tuple.TupleData = (cisdata_t *)&stk->buf;
  149. tuple.TupleOffset = 0;
  150. tuple.TupleDataMax = 255;
  151. tuple.Attributes = 0;
  152. tuple.DesiredTuple = CISTPL_CONFIG;
  153. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  154. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
  155. CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &stk->parse));
  156. link->conf.ConfigBase = stk->parse.config.base;
  157. link->conf.Present = stk->parse.config.rmask[0];
  158. tuple.DesiredTuple = CISTPL_MANFID;
  159. if (!pcmcia_get_first_tuple(link, &tuple) &&
  160. !pcmcia_get_tuple_data(link, &tuple) &&
  161. !pcmcia_parse_tuple(link, &tuple, &stk->parse))
  162. is_kme = ((stk->parse.manfid.manf == MANFID_KME) &&
  163. ((stk->parse.manfid.card == PRODID_KME_KXLC005_A) ||
  164. (stk->parse.manfid.card == PRODID_KME_KXLC005_B)));
  165. /* Configure card */
  166. link->state |= DEV_CONFIG;
  167. /* Not sure if this is right... look up the current Vcc */
  168. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(link, &stk->conf));
  169. pass = io_base = ctl_base = 0;
  170. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  171. tuple.Attributes = 0;
  172. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  173. while (1) {
  174. if (pcmcia_get_tuple_data(link, &tuple) != 0) goto next_entry;
  175. if (pcmcia_parse_tuple(link, &tuple, &stk->parse) != 0) goto next_entry;
  176. /* Check for matching Vcc, unless we're desperate */
  177. if (!pass) {
  178. if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  179. if (stk->conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000)
  180. goto next_entry;
  181. } else if (stk->dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
  182. if (stk->conf.Vcc != stk->dflt.vcc.param[CISTPL_POWER_VNOM] / 10000)
  183. goto next_entry;
  184. }
  185. }
  186. if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
  187. link->conf.Vpp =
  188. cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  189. else if (stk->dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
  190. link->conf.Vpp =
  191. stk->dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
  192. if ((cfg->io.nwin > 0) || (stk->dflt.io.nwin > 0)) {
  193. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &stk->dflt.io;
  194. link->conf.ConfigIndex = cfg->index;
  195. link->io.BasePort1 = io->win[0].base;
  196. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  197. if (!(io->flags & CISTPL_IO_16BIT))
  198. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  199. if (io->nwin == 2) {
  200. link->io.NumPorts1 = 8;
  201. link->io.BasePort2 = io->win[1].base;
  202. link->io.NumPorts2 = (is_kme) ? 2 : 1;
  203. if (pcmcia_request_io(link, &link->io) != 0)
  204. goto next_entry;
  205. io_base = link->io.BasePort1;
  206. ctl_base = link->io.BasePort2;
  207. } else if ((io->nwin == 1) && (io->win[0].len >= 16)) {
  208. link->io.NumPorts1 = io->win[0].len;
  209. link->io.NumPorts2 = 0;
  210. if (pcmcia_request_io(link, &link->io) != 0)
  211. goto next_entry;
  212. io_base = link->io.BasePort1;
  213. ctl_base = link->io.BasePort1 + 0x0e;
  214. } else goto next_entry;
  215. /* If we've got this far, we're done */
  216. break;
  217. }
  218. next_entry:
  219. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  220. memcpy(&stk->dflt, cfg, sizeof(stk->dflt));
  221. if (pass) {
  222. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(link, &tuple));
  223. } else if (pcmcia_get_next_tuple(link, &tuple) != 0) {
  224. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  225. memset(&stk->dflt, 0, sizeof(stk->dflt));
  226. pass++;
  227. }
  228. }
  229. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  230. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  231. /* disable drive interrupts during IDE probe */
  232. outb(0x02, ctl_base);
  233. /* special setup for KXLC005 card */
  234. if (is_kme)
  235. outb(0x81, ctl_base+1);
  236. /* retry registration in case device is still spinning up */
  237. for (hd = -1, i = 0; i < 10; i++) {
  238. hd = idecs_register(io_base, ctl_base, link->irq.AssignedIRQ, link);
  239. if (hd >= 0) break;
  240. if (link->io.NumPorts1 == 0x20) {
  241. outb(0x02, ctl_base + 0x10);
  242. hd = idecs_register(io_base + 0x10, ctl_base + 0x10,
  243. link->irq.AssignedIRQ, link);
  244. if (hd >= 0) {
  245. io_base += 0x10;
  246. ctl_base += 0x10;
  247. break;
  248. }
  249. }
  250. msleep(100);
  251. }
  252. if (hd < 0) {
  253. printk(KERN_NOTICE "ide-cs: ide_register() at 0x%3lx & 0x%3lx"
  254. ", irq %u failed\n", io_base, ctl_base,
  255. link->irq.AssignedIRQ);
  256. goto failed;
  257. }
  258. info->ndev = 1;
  259. sprintf(info->node.dev_name, "hd%c", 'a' + (hd * 2));
  260. info->node.major = ide_major[hd];
  261. info->node.minor = 0;
  262. info->hd = hd;
  263. link->dev_node = &info->node;
  264. printk(KERN_INFO "ide-cs: %s: Vpp = %d.%d\n",
  265. info->node.dev_name, link->conf.Vpp / 10, link->conf.Vpp % 10);
  266. link->state &= ~DEV_CONFIG_PENDING;
  267. kfree(stk);
  268. return;
  269. err_mem:
  270. printk(KERN_NOTICE "ide-cs: ide_config failed memory allocation\n");
  271. goto failed;
  272. cs_failed:
  273. cs_error(link, last_fn, last_ret);
  274. failed:
  275. kfree(stk);
  276. ide_release(link);
  277. link->state &= ~DEV_CONFIG_PENDING;
  278. } /* ide_config */
  279. /*======================================================================
  280. After a card is removed, ide_release() will unregister the net
  281. device, and release the PCMCIA configuration. If the device is
  282. still open, this will be postponed until it is closed.
  283. ======================================================================*/
  284. void ide_release(struct pcmcia_device *link)
  285. {
  286. ide_info_t *info = link->priv;
  287. DEBUG(0, "ide_release(0x%p)\n", link);
  288. if (info->ndev) {
  289. /* FIXME: if this fails we need to queue the cleanup somehow
  290. -- need to investigate the required PCMCIA magic */
  291. ide_unregister(info->hd);
  292. }
  293. info->ndev = 0;
  294. pcmcia_disable_device(link);
  295. } /* ide_release */
  296. /*======================================================================
  297. The card status event handler. Mostly, this schedules other
  298. stuff to run after an event is received. A CARD_REMOVAL event
  299. also sets some flags to discourage the ide drivers from
  300. talking to the ports.
  301. ======================================================================*/
  302. static struct pcmcia_device_id ide_ids[] = {
  303. PCMCIA_DEVICE_FUNC_ID(4),
  304. PCMCIA_DEVICE_MANF_CARD(0x0007, 0x0000), /* Hitachi */
  305. PCMCIA_DEVICE_MANF_CARD(0x0032, 0x0704),
  306. PCMCIA_DEVICE_MANF_CARD(0x0045, 0x0401),
  307. PCMCIA_DEVICE_MANF_CARD(0x0098, 0x0000), /* Toshiba */
  308. PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x002d),
  309. PCMCIA_DEVICE_MANF_CARD(0x00ce, 0x0000), /* Samsung */
  310. PCMCIA_DEVICE_MANF_CARD(0x0319, 0x0000), /* Hitachi */
  311. PCMCIA_DEVICE_MANF_CARD(0x2080, 0x0001),
  312. PCMCIA_DEVICE_MANF_CARD(0x4e01, 0x0200), /* Lexar */
  313. PCMCIA_DEVICE_PROD_ID123("Caravelle", "PSC-IDE ", "PSC000", 0x8c36137c, 0xd0693ab8, 0x2768a9f0),
  314. PCMCIA_DEVICE_PROD_ID123("CDROM", "IDE", "MCD-601p", 0x1b9179ca, 0xede88951, 0x0d902f74),
  315. PCMCIA_DEVICE_PROD_ID123("PCMCIA", "IDE CARD", "F1", 0x281f1c5d, 0x1907960c, 0xf7fde8b9),
  316. PCMCIA_DEVICE_PROD_ID12("ARGOSY", "CD-ROM", 0x78f308dc, 0x66536591),
  317. PCMCIA_DEVICE_PROD_ID12("ARGOSY", "PnPIDE", 0x78f308dc, 0x0c694728),
  318. PCMCIA_DEVICE_PROD_ID12("CNF CD-M", "CD-ROM", 0x7d93b852, 0x66536591),
  319. PCMCIA_DEVICE_PROD_ID12("Creative Technology Ltd.", "PCMCIA CD-ROM Interface Card", 0xff8c8a45, 0xfe8020c4),
  320. PCMCIA_DEVICE_PROD_ID12("Digital Equipment Corporation.", "Digital Mobile Media CD-ROM", 0x17692a66, 0xef1dcbde),
  321. PCMCIA_DEVICE_PROD_ID12("EXP", "CD+GAME", 0x6f58c983, 0x63c13aaf),
  322. PCMCIA_DEVICE_PROD_ID12("EXP ", "CD-ROM", 0x0a5c52fd, 0x66536591),
  323. PCMCIA_DEVICE_PROD_ID12("EXP ", "PnPIDE", 0x0a5c52fd, 0x0c694728),
  324. PCMCIA_DEVICE_PROD_ID12("FREECOM", "PCCARD-IDE", 0x5714cbf7, 0x48e0ab8e),
  325. PCMCIA_DEVICE_PROD_ID12("HITACHI", "FLASH", 0xf4f43949, 0x9eb86aae),
  326. PCMCIA_DEVICE_PROD_ID12("HITACHI", "microdrive", 0xf4f43949, 0xa6d76178),
  327. PCMCIA_DEVICE_PROD_ID12("IBM", "IBM17JSSFP20", 0xb569a6e5, 0xf2508753),
  328. PCMCIA_DEVICE_PROD_ID12("IO DATA", "CBIDE2 ", 0x547e66dc, 0x8671043b),
  329. PCMCIA_DEVICE_PROD_ID12("IO DATA", "PCIDE", 0x547e66dc, 0x5c5ab149),
  330. PCMCIA_DEVICE_PROD_ID12("IO DATA", "PCIDEII", 0x547e66dc, 0xb3662674),
  331. PCMCIA_DEVICE_PROD_ID12("LOOKMEET", "CBIDE2 ", 0xe37be2b5, 0x8671043b),
  332. PCMCIA_DEVICE_PROD_ID2("NinjaATA-", 0xebe0bd79),
  333. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "CD-ROM", 0x281f1c5d, 0x66536591),
  334. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "PnPIDE", 0x281f1c5d, 0x0c694728),
  335. PCMCIA_DEVICE_PROD_ID12("SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", 0x4a3f0ba0, 0x322560e1),
  336. PCMCIA_DEVICE_PROD_ID12("TOSHIBA", "MK2001MPL", 0xb4585a1a, 0x3489e003),
  337. PCMCIA_DEVICE_PROD_ID1("TRANSCEND 512M ", 0xd0909443),
  338. PCMCIA_DEVICE_PROD_ID12("WIT", "IDE16", 0x244e5994, 0x3e232852),
  339. PCMCIA_DEVICE_PROD_ID1("STI Flash", 0xe4a13209),
  340. PCMCIA_DEVICE_PROD_ID12("STI", "Flash 5.0", 0xbf2df18d, 0x8cb57a0e),
  341. PCMCIA_MFC_DEVICE_PROD_ID12(1, "SanDisk", "ConnectPlus", 0x7a954bd9, 0x74be00c6),
  342. PCMCIA_DEVICE_NULL,
  343. };
  344. MODULE_DEVICE_TABLE(pcmcia, ide_ids);
  345. static struct pcmcia_driver ide_cs_driver = {
  346. .owner = THIS_MODULE,
  347. .drv = {
  348. .name = "ide-cs",
  349. },
  350. .probe = ide_attach,
  351. .remove = ide_detach,
  352. .id_table = ide_ids,
  353. };
  354. static int __init init_ide_cs(void)
  355. {
  356. return pcmcia_register_driver(&ide_cs_driver);
  357. }
  358. static void __exit exit_ide_cs(void)
  359. {
  360. pcmcia_unregister_driver(&ide_cs_driver);
  361. }
  362. late_initcall(init_ide_cs);
  363. module_exit(exit_ide_cs);