sl811_cs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <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(CONFIG_USB_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. static dev_link_t *dev_list = NULL;
  53. typedef struct local_info_t {
  54. dev_link_t link;
  55. dev_node_t node;
  56. } local_info_t;
  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 device_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.
  104. */
  105. platform_dev.name = sl811h_driver.name;
  106. return platform_device_register(&platform_dev);
  107. }
  108. /*====================================================================*/
  109. static void sl811_cs_detach(dev_link_t *link)
  110. {
  111. dev_link_t **linkp;
  112. DBG(0, "sl811_cs_detach(0x%p)\n", link);
  113. /* Locate device structure */
  114. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) {
  115. if (*linkp == link)
  116. break;
  117. }
  118. if (*linkp == NULL)
  119. return;
  120. /* Break the link with Card Services */
  121. if (link->handle)
  122. pcmcia_deregister_client(link->handle);
  123. /* Unlink device structure, and free it */
  124. *linkp = link->next;
  125. /* This points to the parent local_info_t struct */
  126. kfree(link->priv);
  127. }
  128. static void sl811_cs_release(dev_link_t * link)
  129. {
  130. DBG(0, "sl811_cs_release(0x%p)\n", link);
  131. if (link->open) {
  132. DBG(1, "sl811_cs: release postponed, '%s' still open\n",
  133. link->dev->dev_name);
  134. link->state |= DEV_STALE_CONFIG;
  135. return;
  136. }
  137. /* Unlink the device chain */
  138. link->dev = NULL;
  139. platform_device_unregister(&platform_dev);
  140. pcmcia_release_configuration(link->handle);
  141. if (link->io.NumPorts1)
  142. pcmcia_release_io(link->handle, &link->io);
  143. if (link->irq.AssignedIRQ)
  144. pcmcia_release_irq(link->handle, &link->irq);
  145. link->state &= ~DEV_CONFIG;
  146. if (link->state & DEV_STALE_LINK)
  147. sl811_cs_detach(link);
  148. }
  149. static void sl811_cs_config(dev_link_t *link)
  150. {
  151. client_handle_t handle = link->handle;
  152. struct device *parent = &handle_to_dev(handle);
  153. local_info_t *dev = link->priv;
  154. tuple_t tuple;
  155. cisparse_t parse;
  156. int last_fn, last_ret;
  157. u_char buf[64];
  158. config_info_t conf;
  159. cistpl_cftable_entry_t dflt = { 0 };
  160. DBG(0, "sl811_cs_config(0x%p)\n", link);
  161. tuple.DesiredTuple = CISTPL_CONFIG;
  162. tuple.Attributes = 0;
  163. tuple.TupleData = buf;
  164. tuple.TupleDataMax = sizeof(buf);
  165. tuple.TupleOffset = 0;
  166. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  167. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  168. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  169. link->conf.ConfigBase = parse.config.base;
  170. link->conf.Present = parse.config.rmask[0];
  171. /* Configure card */
  172. link->state |= DEV_CONFIG;
  173. /* Look up the current Vcc */
  174. CS_CHECK(GetConfigurationInfo,
  175. pcmcia_get_configuration_info(handle, &conf));
  176. link->conf.Vcc = conf.Vcc;
  177. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  178. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  179. while (1) {
  180. cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
  181. if (pcmcia_get_tuple_data(handle, &tuple) != 0
  182. || pcmcia_parse_tuple(handle, &tuple, &parse)
  183. != 0)
  184. goto next_entry;
  185. if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
  186. dflt = *cfg;
  187. }
  188. if (cfg->index == 0)
  189. goto next_entry;
  190. link->conf.ConfigIndex = cfg->index;
  191. /* Use power settings for Vcc and Vpp if present */
  192. /* Note that the CIS values need to be rescaled */
  193. if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
  194. if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
  195. != conf.Vcc)
  196. goto next_entry;
  197. } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
  198. if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
  199. != conf.Vcc)
  200. goto next_entry;
  201. }
  202. if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
  203. link->conf.Vpp1 = link->conf.Vpp2 =
  204. cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
  205. else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
  206. link->conf.Vpp1 = link->conf.Vpp2 =
  207. dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
  208. /* we need an interrupt */
  209. if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
  210. link->conf.Attributes |= CONF_ENABLE_IRQ;
  211. /* IO window settings */
  212. link->io.NumPorts1 = link->io.NumPorts2 = 0;
  213. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  214. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  215. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  216. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  217. link->io.BasePort1 = io->win[0].base;
  218. link->io.NumPorts1 = io->win[0].len;
  219. if (pcmcia_request_io(link->handle, &link->io) != 0)
  220. goto next_entry;
  221. }
  222. break;
  223. next_entry:
  224. if (link->io.NumPorts1)
  225. pcmcia_release_io(link->handle, &link->io);
  226. last_ret = pcmcia_get_next_tuple(handle, &tuple);
  227. }
  228. /* require an IRQ and two registers */
  229. if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
  230. goto cs_failed;
  231. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  232. CS_CHECK(RequestIRQ,
  233. pcmcia_request_irq(link->handle, &link->irq));
  234. else
  235. goto cs_failed;
  236. CS_CHECK(RequestConfiguration,
  237. pcmcia_request_configuration(link->handle, &link->conf));
  238. sprintf(dev->node.dev_name, driver_name);
  239. dev->node.major = dev->node.minor = 0;
  240. link->dev = &dev->node;
  241. printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
  242. dev->node.dev_name, link->conf.ConfigIndex,
  243. link->conf.Vcc/10, link->conf.Vcc%10);
  244. if (link->conf.Vpp1)
  245. printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
  246. printk(", irq %d", link->irq.AssignedIRQ);
  247. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  248. link->io.BasePort1+link->io.NumPorts1-1);
  249. printk("\n");
  250. link->state &= ~DEV_CONFIG_PENDING;
  251. if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
  252. < 0) {
  253. cs_failed:
  254. printk("sl811_cs_config failed\n");
  255. cs_error(link->handle, last_fn, last_ret);
  256. sl811_cs_release(link);
  257. link->state &= ~DEV_CONFIG_PENDING;
  258. }
  259. }
  260. static int
  261. sl811_cs_event(event_t event, int priority, event_callback_args_t *args)
  262. {
  263. dev_link_t *link = args->client_data;
  264. DBG(1, "sl811_cs_event(0x%06x)\n", event);
  265. switch (event) {
  266. case CS_EVENT_CARD_REMOVAL:
  267. link->state &= ~DEV_PRESENT;
  268. if (link->state & DEV_CONFIG)
  269. sl811_cs_release(link);
  270. break;
  271. case CS_EVENT_CARD_INSERTION:
  272. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  273. sl811_cs_config(link);
  274. break;
  275. case CS_EVENT_PM_SUSPEND:
  276. link->state |= DEV_SUSPEND;
  277. /* Fall through... */
  278. case CS_EVENT_RESET_PHYSICAL:
  279. if (link->state & DEV_CONFIG)
  280. pcmcia_release_configuration(link->handle);
  281. break;
  282. case CS_EVENT_PM_RESUME:
  283. link->state &= ~DEV_SUSPEND;
  284. /* Fall through... */
  285. case CS_EVENT_CARD_RESET:
  286. if (link->state & DEV_CONFIG)
  287. pcmcia_request_configuration(link->handle, &link->conf);
  288. DBG(0, "reset sl811-hcd here?\n");
  289. break;
  290. }
  291. return 0;
  292. }
  293. static dev_link_t *sl811_cs_attach(void)
  294. {
  295. local_info_t *local;
  296. dev_link_t *link;
  297. client_reg_t client_reg;
  298. int ret;
  299. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  300. if (!local)
  301. return NULL;
  302. memset(local, 0, sizeof(local_info_t));
  303. link = &local->link;
  304. link->priv = local;
  305. /* Initialize */
  306. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  307. link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
  308. link->irq.Handler = NULL;
  309. link->conf.Attributes = 0;
  310. link->conf.Vcc = 33;
  311. link->conf.IntType = INT_MEMORY_AND_IO;
  312. /* Register with Card Services */
  313. link->next = dev_list;
  314. dev_list = link;
  315. client_reg.dev_info = (dev_info_t *) &driver_name;
  316. client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
  317. client_reg.Version = 0x0210;
  318. client_reg.event_callback_args.client_data = link;
  319. ret = pcmcia_register_client(&link->handle, &client_reg);
  320. if (ret != CS_SUCCESS) {
  321. cs_error(link->handle, RegisterClient, ret);
  322. sl811_cs_detach(link);
  323. return NULL;
  324. }
  325. return link;
  326. }
  327. static struct pcmcia_device_id sl811_ids[] = {
  328. PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
  329. PCMCIA_DEVICE_NULL,
  330. };
  331. MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
  332. static struct pcmcia_driver sl811_cs_driver = {
  333. .owner = THIS_MODULE,
  334. .drv = {
  335. .name = (char *)driver_name,
  336. },
  337. .attach = sl811_cs_attach,
  338. .event = sl811_cs_event,
  339. .detach = sl811_cs_detach,
  340. .id_table = sl811_ids,
  341. };
  342. /*====================================================================*/
  343. static int __init init_sl811_cs(void)
  344. {
  345. return pcmcia_register_driver(&sl811_cs_driver);
  346. }
  347. module_init(init_sl811_cs);
  348. static void __exit exit_sl811_cs(void)
  349. {
  350. pcmcia_unregister_driver(&sl811_cs_driver);
  351. }
  352. module_exit(exit_sl811_cs);