sl811_cs.c 11 KB

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