sl811_cs.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/ptrace.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/timer.h>
  19. #include <linux/ioport.h>
  20. #include <linux/platform_device.h>
  21. #include <pcmcia/cs_types.h>
  22. #include <pcmcia/cs.h>
  23. #include <pcmcia/cistpl.h>
  24. #include <pcmcia/cisreg.h>
  25. #include <pcmcia/ds.h>
  26. #include <linux/usb/sl811.h>
  27. MODULE_AUTHOR("Botond Botyanszki");
  28. MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
  29. MODULE_LICENSE("GPL");
  30. /*====================================================================*/
  31. /* MACROS */
  32. /*====================================================================*/
  33. #if defined(DEBUG) || defined(PCMCIA_DEBUG)
  34. static int pc_debug = 0;
  35. module_param(pc_debug, int, 0644);
  36. #define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
  37. #else
  38. #define DBG(n, args...) do{}while(0)
  39. #endif /* no debugging */
  40. #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
  41. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
  42. #define CS_CHECK(fn, ret) \
  43. do { \
  44. last_fn = (fn); \
  45. if ((last_ret = (ret)) != 0) \
  46. goto cs_failed; \
  47. } while (0)
  48. /*====================================================================*/
  49. /* VARIABLES */
  50. /*====================================================================*/
  51. static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
  52. typedef struct local_info_t {
  53. struct pcmcia_device *p_dev;
  54. dev_node_t node;
  55. } local_info_t;
  56. static void sl811_cs_release(struct pcmcia_device * link);
  57. /*====================================================================*/
  58. static void release_platform_dev(struct device * dev)
  59. {
  60. DBG(0, "sl811_cs platform_dev release\n");
  61. dev->parent = NULL;
  62. }
  63. static struct sl811_platform_data platform_data = {
  64. .potpg = 100,
  65. .power = 50, /* == 100mA */
  66. // .reset = ... FIXME: invoke CF reset on the card
  67. };
  68. static struct resource resources[] = {
  69. [0] = {
  70. .flags = IORESOURCE_IRQ,
  71. },
  72. [1] = {
  73. // .name = "address",
  74. .flags = IORESOURCE_IO,
  75. },
  76. [2] = {
  77. // .name = "data",
  78. .flags = IORESOURCE_IO,
  79. },
  80. };
  81. extern struct platform_driver sl811h_driver;
  82. static struct platform_device platform_dev = {
  83. .id = -1,
  84. .dev = {
  85. .platform_data = &platform_data,
  86. .release = release_platform_dev,
  87. },
  88. .resource = resources,
  89. .num_resources = ARRAY_SIZE(resources),
  90. };
  91. static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
  92. {
  93. if (platform_dev.dev.parent)
  94. return -EBUSY;
  95. platform_dev.dev.parent = parent;
  96. /* finish seting up the platform device */
  97. resources[0].start = irq;
  98. resources[1].start = base_addr;
  99. resources[1].end = base_addr;
  100. resources[2].start = base_addr + 1;
  101. resources[2].end = base_addr + 1;
  102. /* The driver core will probe for us. We know sl811-hcd has been
  103. * initialized already because of the link order dependency created
  104. * by referencing "sl811h_driver".
  105. */
  106. platform_dev.name = sl811h_driver.driver.name;
  107. return platform_device_register(&platform_dev);
  108. }
  109. /*====================================================================*/
  110. static void sl811_cs_detach(struct pcmcia_device *link)
  111. {
  112. DBG(0, "sl811_cs_detach(0x%p)\n", link);
  113. sl811_cs_release(link);
  114. /* This points to the parent local_info_t struct */
  115. kfree(link->priv);
  116. }
  117. static void sl811_cs_release(struct pcmcia_device * link)
  118. {
  119. DBG(0, "sl811_cs_release(0x%p)\n", link);
  120. pcmcia_disable_device(link);
  121. platform_device_unregister(&platform_dev);
  122. }
  123. struct sl811_css_cfg {
  124. cistpl_cftable_entry_t dflt;
  125. config_info_t conf;
  126. };
  127. static int sl811_cs_config_check(struct pcmcia_device *p_dev,
  128. cistpl_cftable_entry_t *cfg,
  129. void *priv_data)
  130. {
  131. struct sl811_css_cfg *cfg_mem = priv_data;
  132. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  133. memcpy(&cfg_mem->dflt, cfg, sizeof(cistpl_cftable_entry_t));
  134. if (cfg->index == 0)
  135. return -ENODEV;
  136. /* Use power settings for Vcc and Vpp if present */
  137. /* Note that the CIS values need to be rescaled */
  138. if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
  139. if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000 !=
  140. cfg_mem->conf.Vcc)
  141. return -ENODEV;
  142. } else if (cfg_mem->dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
  143. if (cfg_mem->dflt.vcc.param[CISTPL_POWER_VNOM]/10000
  144. != cfg_mem->conf.Vcc)
  145. return -ENODEV;
  146. }
  147. if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
  148. p_dev->conf.Vpp =
  149. cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
  150. else if (cfg_mem->dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
  151. p_dev->conf.Vpp =
  152. cfg_mem->dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
  153. /* we need an interrupt */
  154. if (cfg->irq.IRQInfo1 || cfg_mem->dflt.irq.IRQInfo1)
  155. p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
  156. /* IO window settings */
  157. p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
  158. if ((cfg->io.nwin > 0) || (cfg_mem->dflt.io.nwin > 0)) {
  159. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &cfg_mem->dflt.io;
  160. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  161. p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  162. p_dev->io.BasePort1 = io->win[0].base;
  163. p_dev->io.NumPorts1 = io->win[0].len;
  164. return pcmcia_request_io(p_dev, &p_dev->io);
  165. }
  166. pcmcia_disable_device(p_dev);
  167. return -ENODEV;
  168. }
  169. static int sl811_cs_config(struct pcmcia_device *link)
  170. {
  171. struct device *parent = &handle_to_dev(link);
  172. local_info_t *dev = link->priv;
  173. int last_fn, last_ret;
  174. struct sl811_css_cfg *cfg_mem;
  175. DBG(0, "sl811_cs_config(0x%p)\n", link);
  176. cfg_mem = kzalloc(sizeof(struct sl811_css_cfg), GFP_KERNEL);
  177. if (!cfg_mem)
  178. return -ENOMEM;
  179. /* Look up the current Vcc */
  180. CS_CHECK(GetConfigurationInfo,
  181. pcmcia_get_configuration_info(link, &cfg_mem->conf));
  182. if (pcmcia_loop_config(link, sl811_cs_config_check, cfg_mem))
  183. return -ENODEV;
  184. /* require an IRQ and two registers */
  185. if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
  186. goto cs_failed;
  187. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  188. CS_CHECK(RequestIRQ,
  189. pcmcia_request_irq(link, &link->irq));
  190. else
  191. goto cs_failed;
  192. CS_CHECK(RequestConfiguration,
  193. pcmcia_request_configuration(link, &link->conf));
  194. sprintf(dev->node.dev_name, driver_name);
  195. dev->node.major = dev->node.minor = 0;
  196. link->dev_node = &dev->node;
  197. printk(KERN_INFO "%s: index 0x%02x: ",
  198. dev->node.dev_name, link->conf.ConfigIndex);
  199. if (link->conf.Vpp)
  200. printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
  201. printk(", irq %d", link->irq.AssignedIRQ);
  202. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  203. link->io.BasePort1+link->io.NumPorts1-1);
  204. printk("\n");
  205. if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
  206. < 0) {
  207. cs_failed:
  208. printk("sl811_cs_config failed\n");
  209. cs_error(link, last_fn, last_ret);
  210. sl811_cs_release(link);
  211. kfree(cfg_mem);
  212. return -ENODEV;
  213. }
  214. kfree(cfg_mem);
  215. return 0;
  216. }
  217. static int sl811_cs_probe(struct pcmcia_device *link)
  218. {
  219. local_info_t *local;
  220. local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
  221. if (!local)
  222. return -ENOMEM;
  223. local->p_dev = link;
  224. link->priv = local;
  225. /* Initialize */
  226. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  227. link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
  228. link->irq.Handler = NULL;
  229. link->conf.Attributes = 0;
  230. link->conf.IntType = INT_MEMORY_AND_IO;
  231. return sl811_cs_config(link);
  232. }
  233. static struct pcmcia_device_id sl811_ids[] = {
  234. PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
  235. PCMCIA_DEVICE_NULL,
  236. };
  237. MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
  238. static struct pcmcia_driver sl811_cs_driver = {
  239. .owner = THIS_MODULE,
  240. .drv = {
  241. .name = (char *)driver_name,
  242. },
  243. .probe = sl811_cs_probe,
  244. .remove = sl811_cs_detach,
  245. .id_table = sl811_ids,
  246. };
  247. /*====================================================================*/
  248. static int __init init_sl811_cs(void)
  249. {
  250. return pcmcia_register_driver(&sl811_cs_driver);
  251. }
  252. module_init(init_sl811_cs);
  253. static void __exit exit_sl811_cs(void)
  254. {
  255. pcmcia_unregister_driver(&sl811_cs_driver);
  256. }
  257. module_exit(exit_sl811_cs);