sl811_cs.c 11 KB

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