parport_cs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*======================================================================
  2. A driver for PCMCIA parallel port adapters
  3. (specifically, for the Quatech SPP-100 EPP card: other cards will
  4. probably require driver tweaks)
  5. parport_cs.c 1.29 2002/10/11 06:57:41
  6. The contents of this file are subject to the Mozilla Public
  7. License Version 1.1 (the "License"); you may not use this file
  8. except in compliance with the License. You may obtain a copy of
  9. the License at http://www.mozilla.org/MPL/
  10. Software distributed under the License is distributed on an "AS
  11. IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12. implied. See the License for the specific language governing
  13. rights and limitations under the License.
  14. The initial developer of the original code is David A. Hinds
  15. <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  16. are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  17. Alternatively, the contents of this file may be used under the
  18. terms of the GNU General Public License version 2 (the "GPL"), in
  19. which case the provisions of the GPL are applicable instead of the
  20. above. If you wish to allow the use of your version of this file
  21. only under the terms of the GPL and not to allow others to use
  22. your version of this file under the MPL, indicate your decision
  23. by deleting the provisions above and replace them with the notice
  24. and other provisions required by the GPL. If you do not delete
  25. the provisions above, a recipient may use your version of this
  26. file under either the MPL or the GPL.
  27. ======================================================================*/
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/sched.h>
  32. #include <linux/ptrace.h>
  33. #include <linux/slab.h>
  34. #include <linux/string.h>
  35. #include <linux/timer.h>
  36. #include <linux/ioport.h>
  37. #include <linux/major.h>
  38. #include <linux/parport.h>
  39. #include <linux/parport_pc.h>
  40. #include <pcmcia/cs_types.h>
  41. #include <pcmcia/cs.h>
  42. #include <pcmcia/cistpl.h>
  43. #include <pcmcia/ds.h>
  44. #include <pcmcia/cisreg.h>
  45. #include <pcmcia/ciscode.h>
  46. /*====================================================================*/
  47. /* Module parameters */
  48. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  49. MODULE_DESCRIPTION("PCMCIA parallel port card driver");
  50. MODULE_LICENSE("Dual MPL/GPL");
  51. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  52. INT_MODULE_PARM(epp_mode, 1);
  53. #ifdef PCMCIA_DEBUG
  54. INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
  55. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  56. static char *version =
  57. "parport_cs.c 1.29 2002/10/11 06:57:41 (David Hinds)";
  58. #else
  59. #define DEBUG(n, args...)
  60. #endif
  61. /*====================================================================*/
  62. #define FORCE_EPP_MODE 0x08
  63. typedef struct parport_info_t {
  64. dev_link_t link;
  65. int ndev;
  66. dev_node_t node;
  67. struct parport *port;
  68. } parport_info_t;
  69. static dev_link_t *parport_attach(void);
  70. static void parport_detach(dev_link_t *);
  71. static void parport_config(dev_link_t *link);
  72. static void parport_cs_release(dev_link_t *);
  73. static int parport_event(event_t event, int priority,
  74. event_callback_args_t *args);
  75. static dev_info_t dev_info = "parport_cs";
  76. static dev_link_t *dev_list = NULL;
  77. /*======================================================================
  78. parport_attach() creates an "instance" of the driver, allocating
  79. local data structures for one device. The device is registered
  80. with Card Services.
  81. ======================================================================*/
  82. static dev_link_t *parport_attach(void)
  83. {
  84. parport_info_t *info;
  85. dev_link_t *link;
  86. client_reg_t client_reg;
  87. int ret;
  88. DEBUG(0, "parport_attach()\n");
  89. /* Create new parport device */
  90. info = kmalloc(sizeof(*info), GFP_KERNEL);
  91. if (!info) return NULL;
  92. memset(info, 0, sizeof(*info));
  93. link = &info->link; link->priv = info;
  94. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  95. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  96. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  97. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  98. link->conf.Attributes = CONF_ENABLE_IRQ;
  99. link->conf.Vcc = 50;
  100. link->conf.IntType = INT_MEMORY_AND_IO;
  101. /* Register with Card Services */
  102. link->next = dev_list;
  103. dev_list = link;
  104. client_reg.dev_info = &dev_info;
  105. client_reg.Version = 0x0210;
  106. client_reg.event_callback_args.client_data = link;
  107. ret = pcmcia_register_client(&link->handle, &client_reg);
  108. if (ret != CS_SUCCESS) {
  109. cs_error(link->handle, RegisterClient, ret);
  110. parport_detach(link);
  111. return NULL;
  112. }
  113. return link;
  114. } /* parport_attach */
  115. /*======================================================================
  116. This deletes a driver "instance". The device is de-registered
  117. with Card Services. If it has been released, all local data
  118. structures are freed. Otherwise, the structures will be freed
  119. when the device is released.
  120. ======================================================================*/
  121. static void parport_detach(dev_link_t *link)
  122. {
  123. dev_link_t **linkp;
  124. int ret;
  125. DEBUG(0, "parport_detach(0x%p)\n", link);
  126. /* Locate device structure */
  127. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  128. if (*linkp == link) break;
  129. if (*linkp == NULL)
  130. return;
  131. if (link->state & DEV_CONFIG)
  132. parport_cs_release(link);
  133. if (link->handle) {
  134. ret = pcmcia_deregister_client(link->handle);
  135. if (ret != CS_SUCCESS)
  136. cs_error(link->handle, DeregisterClient, ret);
  137. }
  138. /* Unlink, free device structure */
  139. *linkp = link->next;
  140. kfree(link->priv);
  141. } /* parport_detach */
  142. /*======================================================================
  143. parport_config() is scheduled to run after a CARD_INSERTION event
  144. is received, to configure the PCMCIA socket, and to make the
  145. parport device available to the system.
  146. ======================================================================*/
  147. #define CS_CHECK(fn, ret) \
  148. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  149. void parport_config(dev_link_t *link)
  150. {
  151. client_handle_t handle = link->handle;
  152. parport_info_t *info = link->priv;
  153. tuple_t tuple;
  154. u_short buf[128];
  155. cisparse_t parse;
  156. config_info_t conf;
  157. cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
  158. cistpl_cftable_entry_t dflt = { 0 };
  159. struct parport *p;
  160. int last_ret, last_fn;
  161. DEBUG(0, "parport_config(0x%p)\n", link);
  162. tuple.TupleData = (cisdata_t *)buf;
  163. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  164. tuple.Attributes = 0;
  165. tuple.DesiredTuple = CISTPL_CONFIG;
  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. /* Not sure if this is right... look up the current Vcc */
  174. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
  175. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  176. tuple.Attributes = 0;
  177. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  178. while (1) {
  179. if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
  180. pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
  181. goto next_entry;
  182. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  183. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  184. link->conf.ConfigIndex = cfg->index;
  185. if (epp_mode)
  186. link->conf.ConfigIndex |= FORCE_EPP_MODE;
  187. link->io.BasePort1 = io->win[0].base;
  188. link->io.NumPorts1 = io->win[0].len;
  189. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  190. if (io->nwin == 2) {
  191. link->io.BasePort2 = io->win[1].base;
  192. link->io.NumPorts2 = io->win[1].len;
  193. }
  194. if (pcmcia_request_io(link->handle, &link->io) != 0)
  195. goto next_entry;
  196. /* If we've got this far, we're done */
  197. break;
  198. }
  199. next_entry:
  200. if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
  201. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
  202. }
  203. CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
  204. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
  205. release_region(link->io.BasePort1, link->io.NumPorts1);
  206. if (link->io.NumPorts2)
  207. release_region(link->io.BasePort2, link->io.NumPorts2);
  208. p = parport_pc_probe_port(link->io.BasePort1, link->io.BasePort2,
  209. link->irq.AssignedIRQ, PARPORT_DMA_NONE,
  210. NULL);
  211. if (p == NULL) {
  212. printk(KERN_NOTICE "parport_cs: parport_pc_probe_port() at "
  213. "0x%3x, irq %u failed\n", link->io.BasePort1,
  214. link->irq.AssignedIRQ);
  215. goto failed;
  216. }
  217. p->modes |= PARPORT_MODE_PCSPP;
  218. if (epp_mode)
  219. p->modes |= PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP;
  220. info->ndev = 1;
  221. info->node.major = LP_MAJOR;
  222. info->node.minor = p->number;
  223. info->port = p;
  224. strcpy(info->node.dev_name, p->name);
  225. link->dev = &info->node;
  226. link->state &= ~DEV_CONFIG_PENDING;
  227. return;
  228. cs_failed:
  229. cs_error(link->handle, last_fn, last_ret);
  230. failed:
  231. parport_cs_release(link);
  232. link->state &= ~DEV_CONFIG_PENDING;
  233. } /* parport_config */
  234. /*======================================================================
  235. After a card is removed, parport_cs_release() will unregister the
  236. device, and release the PCMCIA configuration. If the device is
  237. still open, this will be postponed until it is closed.
  238. ======================================================================*/
  239. void parport_cs_release(dev_link_t *link)
  240. {
  241. parport_info_t *info = link->priv;
  242. DEBUG(0, "parport_release(0x%p)\n", link);
  243. if (info->ndev) {
  244. struct parport *p = info->port;
  245. parport_pc_unregister_port(p);
  246. request_region(link->io.BasePort1, link->io.NumPorts1,
  247. info->node.dev_name);
  248. if (link->io.NumPorts2)
  249. request_region(link->io.BasePort2, link->io.NumPorts2,
  250. info->node.dev_name);
  251. }
  252. info->ndev = 0;
  253. link->dev = NULL;
  254. pcmcia_release_configuration(link->handle);
  255. pcmcia_release_io(link->handle, &link->io);
  256. pcmcia_release_irq(link->handle, &link->irq);
  257. link->state &= ~DEV_CONFIG;
  258. } /* parport_cs_release */
  259. /*======================================================================
  260. The card status event handler. Mostly, this schedules other
  261. stuff to run after an event is received.
  262. ======================================================================*/
  263. int parport_event(event_t event, int priority,
  264. event_callback_args_t *args)
  265. {
  266. dev_link_t *link = args->client_data;
  267. DEBUG(1, "parport_event(0x%06x)\n", event);
  268. switch (event) {
  269. case CS_EVENT_CARD_REMOVAL:
  270. link->state &= ~DEV_PRESENT;
  271. if (link->state & DEV_CONFIG)
  272. parport_cs_release(link);
  273. break;
  274. case CS_EVENT_CARD_INSERTION:
  275. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  276. parport_config(link);
  277. break;
  278. case CS_EVENT_PM_SUSPEND:
  279. link->state |= DEV_SUSPEND;
  280. /* Fall through... */
  281. case CS_EVENT_RESET_PHYSICAL:
  282. if (link->state & DEV_CONFIG)
  283. pcmcia_release_configuration(link->handle);
  284. break;
  285. case CS_EVENT_PM_RESUME:
  286. link->state &= ~DEV_SUSPEND;
  287. /* Fall through... */
  288. case CS_EVENT_CARD_RESET:
  289. if (DEV_OK(link))
  290. pcmcia_request_configuration(link->handle, &link->conf);
  291. break;
  292. }
  293. return 0;
  294. } /* parport_event */
  295. static struct pcmcia_device_id parport_ids[] = {
  296. PCMCIA_DEVICE_FUNC_ID(3),
  297. PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0003),
  298. PCMCIA_DEVICE_NULL
  299. };
  300. MODULE_DEVICE_TABLE(pcmcia, parport_ids);
  301. static struct pcmcia_driver parport_cs_driver = {
  302. .owner = THIS_MODULE,
  303. .drv = {
  304. .name = "parport_cs",
  305. },
  306. .attach = parport_attach,
  307. .event = parport_event,
  308. .detach = parport_detach,
  309. .id_table = parport_ids,
  310. };
  311. static int __init init_parport_cs(void)
  312. {
  313. return pcmcia_register_driver(&parport_cs_driver);
  314. }
  315. static void __exit exit_parport_cs(void)
  316. {
  317. pcmcia_unregister_driver(&parport_cs_driver);
  318. BUG_ON(dev_list != NULL);
  319. }
  320. module_init(init_parport_cs);
  321. module_exit(exit_parport_cs);