parport_cs.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/ptrace.h>
  32. #include <linux/slab.h>
  33. #include <linux/string.h>
  34. #include <linux/timer.h>
  35. #include <linux/ioport.h>
  36. #include <linux/major.h>
  37. #include <linux/parport.h>
  38. #include <linux/parport_pc.h>
  39. #include <pcmcia/cs_types.h>
  40. #include <pcmcia/cs.h>
  41. #include <pcmcia/cistpl.h>
  42. #include <pcmcia/ds.h>
  43. #include <pcmcia/cisreg.h>
  44. #include <pcmcia/ciscode.h>
  45. /*====================================================================*/
  46. /* Module parameters */
  47. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  48. MODULE_DESCRIPTION("PCMCIA parallel port card driver");
  49. MODULE_LICENSE("Dual MPL/GPL");
  50. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  51. INT_MODULE_PARM(epp_mode, 1);
  52. #ifdef PCMCIA_DEBUG
  53. INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
  54. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  55. static char *version =
  56. "parport_cs.c 1.29 2002/10/11 06:57:41 (David Hinds)";
  57. #else
  58. #define DEBUG(n, args...)
  59. #endif
  60. /*====================================================================*/
  61. #define FORCE_EPP_MODE 0x08
  62. typedef struct parport_info_t {
  63. struct pcmcia_device *p_dev;
  64. int ndev;
  65. dev_node_t node;
  66. struct parport *port;
  67. } parport_info_t;
  68. static void parport_detach(struct pcmcia_device *p_dev);
  69. static int parport_config(struct pcmcia_device *link);
  70. static void parport_cs_release(struct pcmcia_device *);
  71. /*======================================================================
  72. parport_attach() creates an "instance" of the driver, allocating
  73. local data structures for one device. The device is registered
  74. with Card Services.
  75. ======================================================================*/
  76. static int parport_probe(struct pcmcia_device *link)
  77. {
  78. parport_info_t *info;
  79. DEBUG(0, "parport_attach()\n");
  80. /* Create new parport device */
  81. info = kmalloc(sizeof(*info), GFP_KERNEL);
  82. if (!info) return -ENOMEM;
  83. memset(info, 0, sizeof(*info));
  84. link->priv = info;
  85. info->p_dev = link;
  86. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  87. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  88. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  89. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  90. link->conf.Attributes = CONF_ENABLE_IRQ;
  91. link->conf.IntType = INT_MEMORY_AND_IO;
  92. return parport_config(link);
  93. } /* parport_attach */
  94. /*======================================================================
  95. This deletes a driver "instance". The device is de-registered
  96. with Card Services. If it has been released, all local data
  97. structures are freed. Otherwise, the structures will be freed
  98. when the device is released.
  99. ======================================================================*/
  100. static void parport_detach(struct pcmcia_device *link)
  101. {
  102. DEBUG(0, "parport_detach(0x%p)\n", link);
  103. parport_cs_release(link);
  104. kfree(link->priv);
  105. } /* parport_detach */
  106. /*======================================================================
  107. parport_config() is scheduled to run after a CARD_INSERTION event
  108. is received, to configure the PCMCIA socket, and to make the
  109. parport device available to the system.
  110. ======================================================================*/
  111. #define CS_CHECK(fn, ret) \
  112. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  113. static int parport_config(struct pcmcia_device *link)
  114. {
  115. parport_info_t *info = link->priv;
  116. tuple_t tuple;
  117. u_short buf[128];
  118. cisparse_t parse;
  119. cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
  120. cistpl_cftable_entry_t dflt = { 0 };
  121. struct parport *p;
  122. int last_ret, last_fn;
  123. DEBUG(0, "parport_config(0x%p)\n", link);
  124. tuple.TupleData = (cisdata_t *)buf;
  125. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  126. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  127. tuple.Attributes = 0;
  128. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  129. while (1) {
  130. if (pcmcia_get_tuple_data(link, &tuple) != 0 ||
  131. pcmcia_parse_tuple(link, &tuple, &parse) != 0)
  132. goto next_entry;
  133. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  134. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  135. link->conf.ConfigIndex = cfg->index;
  136. if (epp_mode)
  137. link->conf.ConfigIndex |= FORCE_EPP_MODE;
  138. link->io.BasePort1 = io->win[0].base;
  139. link->io.NumPorts1 = io->win[0].len;
  140. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  141. if (io->nwin == 2) {
  142. link->io.BasePort2 = io->win[1].base;
  143. link->io.NumPorts2 = io->win[1].len;
  144. }
  145. if (pcmcia_request_io(link, &link->io) != 0)
  146. goto next_entry;
  147. /* If we've got this far, we're done */
  148. break;
  149. }
  150. next_entry:
  151. if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
  152. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(link, &tuple));
  153. }
  154. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  155. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  156. p = parport_pc_probe_port(link->io.BasePort1, link->io.BasePort2,
  157. link->irq.AssignedIRQ, PARPORT_DMA_NONE,
  158. NULL);
  159. if (p == NULL) {
  160. printk(KERN_NOTICE "parport_cs: parport_pc_probe_port() at "
  161. "0x%3x, irq %u failed\n", link->io.BasePort1,
  162. link->irq.AssignedIRQ);
  163. goto failed;
  164. }
  165. p->modes |= PARPORT_MODE_PCSPP;
  166. if (epp_mode)
  167. p->modes |= PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP;
  168. info->ndev = 1;
  169. info->node.major = LP_MAJOR;
  170. info->node.minor = p->number;
  171. info->port = p;
  172. strcpy(info->node.dev_name, p->name);
  173. link->dev_node = &info->node;
  174. return 0;
  175. cs_failed:
  176. cs_error(link, last_fn, last_ret);
  177. failed:
  178. parport_cs_release(link);
  179. return -ENODEV;
  180. } /* parport_config */
  181. /*======================================================================
  182. After a card is removed, parport_cs_release() will unregister the
  183. device, and release the PCMCIA configuration. If the device is
  184. still open, this will be postponed until it is closed.
  185. ======================================================================*/
  186. void parport_cs_release(struct pcmcia_device *link)
  187. {
  188. parport_info_t *info = link->priv;
  189. DEBUG(0, "parport_release(0x%p)\n", link);
  190. if (info->ndev) {
  191. struct parport *p = info->port;
  192. parport_pc_unregister_port(p);
  193. }
  194. info->ndev = 0;
  195. pcmcia_disable_device(link);
  196. } /* parport_cs_release */
  197. static struct pcmcia_device_id parport_ids[] = {
  198. PCMCIA_DEVICE_FUNC_ID(3),
  199. PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial+Parallel Port: SP230",0x3beb8cf2,0xdb9e58bc),
  200. PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0003),
  201. PCMCIA_DEVICE_NULL
  202. };
  203. MODULE_DEVICE_TABLE(pcmcia, parport_ids);
  204. static struct pcmcia_driver parport_cs_driver = {
  205. .owner = THIS_MODULE,
  206. .drv = {
  207. .name = "parport_cs",
  208. },
  209. .probe = parport_probe,
  210. .remove = parport_detach,
  211. .id_table = parport_ids,
  212. };
  213. static int __init init_parport_cs(void)
  214. {
  215. return pcmcia_register_driver(&parport_cs_driver);
  216. }
  217. static void __exit exit_parport_cs(void)
  218. {
  219. pcmcia_unregister_driver(&parport_cs_driver);
  220. }
  221. module_init(init_parport_cs);
  222. module_exit(exit_parport_cs);