sl811_cs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * PCMCIA driver for SL811HS (as found in REX-CFU1U)
  3. * Filename: sl811_cs.c
  4. * Author: Yukio Yamamoto
  5. *
  6. * Port to sl811-hcd and 2.6.x by
  7. * Botond Botyanszki <boti@rocketmail.com>
  8. * Simon Pickering
  9. *
  10. * Last update: 2005-05-12
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/sched.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/timer.h>
  20. #include <linux/ioport.h>
  21. #include <linux/platform_device.h>
  22. #include <pcmcia/cs_types.h>
  23. #include <pcmcia/cs.h>
  24. #include <pcmcia/cistpl.h>
  25. #include <pcmcia/cisreg.h>
  26. #include <pcmcia/ds.h>
  27. #include <linux/usb_sl811.h>
  28. MODULE_AUTHOR("Botond Botyanszki");
  29. MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
  30. MODULE_LICENSE("GPL");
  31. /*====================================================================*/
  32. /* MACROS */
  33. /*====================================================================*/
  34. #if defined(DEBUG) || defined(PCMCIA_DEBUG)
  35. static int pc_debug = 0;
  36. module_param(pc_debug, int, 0644);
  37. #define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
  38. #else
  39. #define DBG(n, args...) do{}while(0)
  40. #endif /* no debugging */
  41. #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
  42. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
  43. #define CS_CHECK(fn, ret) \
  44. do { \
  45. last_fn = (fn); \
  46. if ((last_ret = (ret)) != 0) \
  47. goto cs_failed; \
  48. } while (0)
  49. /*====================================================================*/
  50. /* VARIABLES */
  51. /*====================================================================*/
  52. static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
  53. typedef struct local_info_t {
  54. dev_link_t link;
  55. dev_node_t node;
  56. } local_info_t;
  57. static void sl811_cs_release(dev_link_t * link);
  58. /*====================================================================*/
  59. static void release_platform_dev(struct device * dev)
  60. {
  61. DBG(0, "sl811_cs platform_dev release\n");
  62. dev->parent = NULL;
  63. }
  64. static struct sl811_platform_data platform_data = {
  65. .potpg = 100,
  66. .power = 50, /* == 100mA */
  67. // .reset = ... FIXME: invoke CF reset on the card
  68. };
  69. static struct resource resources[] = {
  70. [0] = {
  71. .flags = IORESOURCE_IRQ,
  72. },
  73. [1] = {
  74. // .name = "address",
  75. .flags = IORESOURCE_IO,
  76. },
  77. [2] = {
  78. // .name = "data",
  79. .flags = IORESOURCE_IO,
  80. },
  81. };
  82. extern struct device_driver sl811h_driver;
  83. static struct platform_device platform_dev = {
  84. .id = -1,
  85. .dev = {
  86. .platform_data = &platform_data,
  87. .release = release_platform_dev,
  88. },
  89. .resource = resources,
  90. .num_resources = ARRAY_SIZE(resources),
  91. };
  92. static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
  93. {
  94. if (platform_dev.dev.parent)
  95. return -EBUSY;
  96. platform_dev.dev.parent = parent;
  97. /* finish seting up the platform device */
  98. resources[0].start = irq;
  99. resources[1].start = base_addr;
  100. resources[1].end = base_addr;
  101. resources[2].start = base_addr + 1;
  102. resources[2].end = base_addr + 1;
  103. /* The driver core will probe for us. We know sl811-hcd has been
  104. * initialized already because of the link order dependency created
  105. * by referencing "sl811h_driver".
  106. */
  107. platform_dev.name = sl811h_driver.name;
  108. return platform_device_register(&platform_dev);
  109. }
  110. /*====================================================================*/
  111. static void sl811_cs_detach(struct pcmcia_device *p_dev)
  112. {
  113. dev_link_t *link = dev_to_instance(p_dev);
  114. DBG(0, "sl811_cs_detach(0x%p)\n", link);
  115. link->state &= ~DEV_PRESENT;
  116. if (link->state & DEV_CONFIG)
  117. sl811_cs_release(link);
  118. /* This points to the parent local_info_t struct */
  119. kfree(link->priv);
  120. }
  121. static void sl811_cs_release(dev_link_t * link)
  122. {
  123. DBG(0, "sl811_cs_release(0x%p)\n", link);
  124. /* Unlink the device chain */
  125. link->dev = NULL;
  126. platform_device_unregister(&platform_dev);
  127. pcmcia_release_configuration(link->handle);
  128. if (link->io.NumPorts1)
  129. pcmcia_release_io(link->handle, &link->io);
  130. if (link->irq.AssignedIRQ)
  131. pcmcia_release_irq(link->handle, &link->irq);
  132. link->state &= ~DEV_CONFIG;
  133. }
  134. static void sl811_cs_config(dev_link_t *link)
  135. {
  136. client_handle_t handle = link->handle;
  137. struct device *parent = &handle_to_dev(handle);
  138. local_info_t *dev = link->priv;
  139. tuple_t tuple;
  140. cisparse_t parse;
  141. int last_fn, last_ret;
  142. u_char buf[64];
  143. config_info_t conf;
  144. cistpl_cftable_entry_t dflt = { 0 };
  145. DBG(0, "sl811_cs_config(0x%p)\n", link);
  146. tuple.DesiredTuple = CISTPL_CONFIG;
  147. tuple.Attributes = 0;
  148. tuple.TupleData = buf;
  149. tuple.TupleDataMax = sizeof(buf);
  150. tuple.TupleOffset = 0;
  151. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  152. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  153. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  154. link->conf.ConfigBase = parse.config.base;
  155. link->conf.Present = parse.config.rmask[0];
  156. /* Configure card */
  157. link->state |= DEV_CONFIG;
  158. /* Look up the current Vcc */
  159. CS_CHECK(GetConfigurationInfo,
  160. pcmcia_get_configuration_info(handle, &conf));
  161. link->conf.Vcc = conf.Vcc;
  162. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  163. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  164. while (1) {
  165. cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
  166. if (pcmcia_get_tuple_data(handle, &tuple) != 0
  167. || pcmcia_parse_tuple(handle, &tuple, &parse)
  168. != 0)
  169. goto next_entry;
  170. if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
  171. dflt = *cfg;
  172. }
  173. if (cfg->index == 0)
  174. goto next_entry;
  175. link->conf.ConfigIndex = cfg->index;
  176. /* Use power settings for Vcc and Vpp if present */
  177. /* Note that the CIS values need to be rescaled */
  178. if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
  179. if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
  180. != conf.Vcc)
  181. goto next_entry;
  182. } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
  183. if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
  184. != conf.Vcc)
  185. goto next_entry;
  186. }
  187. if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
  188. link->conf.Vpp1 = link->conf.Vpp2 =
  189. cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
  190. else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
  191. link->conf.Vpp1 = link->conf.Vpp2 =
  192. dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
  193. /* we need an interrupt */
  194. if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
  195. link->conf.Attributes |= CONF_ENABLE_IRQ;
  196. /* IO window settings */
  197. link->io.NumPorts1 = link->io.NumPorts2 = 0;
  198. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  199. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  200. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  201. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  202. link->io.BasePort1 = io->win[0].base;
  203. link->io.NumPorts1 = io->win[0].len;
  204. if (pcmcia_request_io(link->handle, &link->io) != 0)
  205. goto next_entry;
  206. }
  207. break;
  208. next_entry:
  209. if (link->io.NumPorts1)
  210. pcmcia_release_io(link->handle, &link->io);
  211. last_ret = pcmcia_get_next_tuple(handle, &tuple);
  212. }
  213. /* require an IRQ and two registers */
  214. if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
  215. goto cs_failed;
  216. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  217. CS_CHECK(RequestIRQ,
  218. pcmcia_request_irq(link->handle, &link->irq));
  219. else
  220. goto cs_failed;
  221. CS_CHECK(RequestConfiguration,
  222. pcmcia_request_configuration(link->handle, &link->conf));
  223. sprintf(dev->node.dev_name, driver_name);
  224. dev->node.major = dev->node.minor = 0;
  225. link->dev = &dev->node;
  226. printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
  227. dev->node.dev_name, link->conf.ConfigIndex,
  228. link->conf.Vcc/10, link->conf.Vcc%10);
  229. if (link->conf.Vpp1)
  230. printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
  231. printk(", irq %d", link->irq.AssignedIRQ);
  232. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  233. link->io.BasePort1+link->io.NumPorts1-1);
  234. printk("\n");
  235. link->state &= ~DEV_CONFIG_PENDING;
  236. if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
  237. < 0) {
  238. cs_failed:
  239. printk("sl811_cs_config failed\n");
  240. cs_error(link->handle, last_fn, last_ret);
  241. sl811_cs_release(link);
  242. link->state &= ~DEV_CONFIG_PENDING;
  243. }
  244. }
  245. static int sl811_suspend(struct pcmcia_device *dev)
  246. {
  247. dev_link_t *link = dev_to_instance(dev);
  248. link->state |= DEV_SUSPEND;
  249. if (link->state & DEV_CONFIG)
  250. pcmcia_release_configuration(link->handle);
  251. return 0;
  252. }
  253. static int sl811_resume(struct pcmcia_device *dev)
  254. {
  255. dev_link_t *link = dev_to_instance(dev);
  256. link->state &= ~DEV_SUSPEND;
  257. if (link->state & DEV_CONFIG)
  258. pcmcia_request_configuration(link->handle, &link->conf);
  259. return 0;
  260. }
  261. static int sl811_cs_attach(struct pcmcia_device *p_dev)
  262. {
  263. local_info_t *local;
  264. dev_link_t *link;
  265. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  266. if (!local)
  267. return -ENOMEM;
  268. memset(local, 0, sizeof(local_info_t));
  269. link = &local->link;
  270. link->priv = local;
  271. /* Initialize */
  272. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  273. link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
  274. link->irq.Handler = NULL;
  275. link->conf.Attributes = 0;
  276. link->conf.Vcc = 33;
  277. link->conf.IntType = INT_MEMORY_AND_IO;
  278. link->handle = p_dev;
  279. p_dev->instance = link;
  280. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  281. sl811_cs_config(link);
  282. return 0;
  283. }
  284. static struct pcmcia_device_id sl811_ids[] = {
  285. PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
  286. PCMCIA_DEVICE_NULL,
  287. };
  288. MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
  289. static struct pcmcia_driver sl811_cs_driver = {
  290. .owner = THIS_MODULE,
  291. .drv = {
  292. .name = (char *)driver_name,
  293. },
  294. .probe = sl811_cs_attach,
  295. .remove = sl811_cs_detach,
  296. .id_table = sl811_ids,
  297. .suspend = sl811_suspend,
  298. .resume = sl811_resume,
  299. };
  300. /*====================================================================*/
  301. static int __init init_sl811_cs(void)
  302. {
  303. return pcmcia_register_driver(&sl811_cs_driver);
  304. }
  305. module_init(init_sl811_cs);
  306. static void __exit exit_sl811_cs(void)
  307. {
  308. pcmcia_unregister_driver(&sl811_cs_driver);
  309. }
  310. module_exit(exit_sl811_cs);