avma1_cs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * PCMCIA client driver for AVM A1 / Fritz!PCMCIA
  3. *
  4. * Author Carsten Paeth
  5. * Copyright 1998-2001 by Carsten Paeth <calle@calle.in-berlin.de>
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <asm/io.h>
  19. #include <asm/system.h>
  20. #include <pcmcia/cs_types.h>
  21. #include <pcmcia/cs.h>
  22. #include <pcmcia/cistpl.h>
  23. #include <pcmcia/ds.h>
  24. #include "hisax_cfg.h"
  25. MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards");
  26. MODULE_AUTHOR("Carsten Paeth");
  27. MODULE_LICENSE("GPL");
  28. /*
  29. All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
  30. you do not define PCMCIA_DEBUG at all, all the debug code will be
  31. left out. If you compile with PCMCIA_DEBUG=0, the debug code will
  32. be present but disabled -- but it can then be enabled for specific
  33. modules at load time with a 'pc_debug=#' option to insmod.
  34. */
  35. #ifdef PCMCIA_DEBUG
  36. static int pc_debug = PCMCIA_DEBUG;
  37. module_param(pc_debug, int, 0);
  38. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
  39. static char *version =
  40. "avma1_cs.c 1.00 1998/01/23 10:00:00 (Carsten Paeth)";
  41. #else
  42. #define DEBUG(n, args...)
  43. #endif
  44. /*====================================================================*/
  45. /* Parameters that can be set with 'insmod' */
  46. static int isdnprot = 2;
  47. module_param(isdnprot, int, 0);
  48. /*====================================================================*/
  49. /*
  50. The event() function is this driver's Card Services event handler.
  51. It will be called by Card Services when an appropriate card status
  52. event is received. The config() and release() entry points are
  53. used to configure or release a socket, in response to card insertion
  54. and ejection events. They are invoked from the skeleton event
  55. handler.
  56. */
  57. static int avma1cs_config(struct pcmcia_device *link);
  58. static void avma1cs_release(struct pcmcia_device *link);
  59. /*
  60. The attach() and detach() entry points are used to create and destroy
  61. "instances" of the driver, where each instance represents everything
  62. needed to manage one actual PCMCIA card.
  63. */
  64. static void avma1cs_detach(struct pcmcia_device *p_dev);
  65. /*
  66. A linked list of "instances" of the skeleton device. Each actual
  67. PCMCIA card corresponds to one device instance, and is described
  68. by one struct pcmcia_device structure (defined in ds.h).
  69. You may not want to use a linked list for this -- for example, the
  70. memory card driver uses an array of struct pcmcia_device pointers, where minor
  71. device numbers are used to derive the corresponding array index.
  72. */
  73. /*
  74. A driver needs to provide a dev_node_t structure for each device
  75. on a card. In some cases, there is only one device per card (for
  76. example, ethernet cards, modems). In other cases, there may be
  77. many actual or logical devices (SCSI adapters, memory cards with
  78. multiple partitions). The dev_node_t structures need to be kept
  79. in a linked list starting at the 'dev' field of a struct pcmcia_device
  80. structure. We allocate them in the card's private data structure,
  81. because they generally can't be allocated dynamically.
  82. */
  83. typedef struct local_info_t {
  84. dev_node_t node;
  85. } local_info_t;
  86. /*======================================================================
  87. avma1cs_attach() creates an "instance" of the driver, allocating
  88. local data structures for one device. The device is registered
  89. with Card Services.
  90. The dev_link structure is initialized, but we don't actually
  91. configure the card at this point -- we wait until we receive a
  92. card insertion event.
  93. ======================================================================*/
  94. static int avma1cs_probe(struct pcmcia_device *p_dev)
  95. {
  96. local_info_t *local;
  97. DEBUG(0, "avma1cs_attach()\n");
  98. /* Allocate space for private device-specific data */
  99. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  100. if (!local)
  101. return -ENOMEM;
  102. memset(local, 0, sizeof(local_info_t));
  103. p_dev->priv = local;
  104. /* The io structure describes IO port mapping */
  105. p_dev->io.NumPorts1 = 16;
  106. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  107. p_dev->io.NumPorts2 = 16;
  108. p_dev->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
  109. p_dev->io.IOAddrLines = 5;
  110. /* Interrupt setup */
  111. p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  112. p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  113. p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
  114. /* General socket configuration */
  115. p_dev->conf.Attributes = CONF_ENABLE_IRQ;
  116. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  117. p_dev->conf.ConfigIndex = 1;
  118. p_dev->conf.Present = PRESENT_OPTION;
  119. return avma1cs_config(p_dev);
  120. } /* avma1cs_attach */
  121. /*======================================================================
  122. This deletes a driver "instance". The device is de-registered
  123. with Card Services. If it has been released, all local data
  124. structures are freed. Otherwise, the structures will be freed
  125. when the device is released.
  126. ======================================================================*/
  127. static void avma1cs_detach(struct pcmcia_device *link)
  128. {
  129. DEBUG(0, "avma1cs_detach(0x%p)\n", link);
  130. avma1cs_release(link);
  131. kfree(link->priv);
  132. } /* avma1cs_detach */
  133. /*======================================================================
  134. avma1cs_config() is scheduled to run after a CARD_INSERTION event
  135. is received, to configure the PCMCIA socket, and to make the
  136. ethernet device available to the system.
  137. ======================================================================*/
  138. static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  139. cisparse_t *parse)
  140. {
  141. int i = pcmcia_get_tuple_data(handle, tuple);
  142. if (i != CS_SUCCESS) return i;
  143. return pcmcia_parse_tuple(handle, tuple, parse);
  144. }
  145. static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  146. cisparse_t *parse)
  147. {
  148. int i = pcmcia_get_first_tuple(handle, tuple);
  149. if (i != CS_SUCCESS) return i;
  150. return get_tuple(handle, tuple, parse);
  151. }
  152. static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  153. cisparse_t *parse)
  154. {
  155. int i = pcmcia_get_next_tuple(handle, tuple);
  156. if (i != CS_SUCCESS) return i;
  157. return get_tuple(handle, tuple, parse);
  158. }
  159. static int avma1cs_config(struct pcmcia_device *link)
  160. {
  161. tuple_t tuple;
  162. cisparse_t parse;
  163. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  164. local_info_t *dev;
  165. int i;
  166. u_char buf[64];
  167. char devname[128];
  168. IsdnCard_t icard;
  169. int busy = 0;
  170. dev = link->priv;
  171. DEBUG(0, "avma1cs_config(0x%p)\n", link);
  172. /*
  173. This reads the card's CONFIG tuple to find its configuration
  174. registers.
  175. */
  176. do {
  177. tuple.DesiredTuple = CISTPL_CONFIG;
  178. i = pcmcia_get_first_tuple(link, &tuple);
  179. if (i != CS_SUCCESS) break;
  180. tuple.TupleData = buf;
  181. tuple.TupleDataMax = 64;
  182. tuple.TupleOffset = 0;
  183. i = pcmcia_get_tuple_data(link, &tuple);
  184. if (i != CS_SUCCESS) break;
  185. i = pcmcia_parse_tuple(link, &tuple, &parse);
  186. if (i != CS_SUCCESS) break;
  187. link->conf.ConfigBase = parse.config.base;
  188. } while (0);
  189. if (i != CS_SUCCESS) {
  190. cs_error(link, ParseTuple, i);
  191. return -ENODEV;
  192. }
  193. do {
  194. tuple.Attributes = 0;
  195. tuple.TupleData = buf;
  196. tuple.TupleDataMax = 254;
  197. tuple.TupleOffset = 0;
  198. tuple.DesiredTuple = CISTPL_VERS_1;
  199. devname[0] = 0;
  200. if( !first_tuple(link, &tuple, &parse) && parse.version_1.ns > 1 ) {
  201. strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
  202. sizeof(devname));
  203. }
  204. /*
  205. * find IO port
  206. */
  207. tuple.TupleData = (cisdata_t *)buf;
  208. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  209. tuple.Attributes = 0;
  210. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  211. i = first_tuple(link, &tuple, &parse);
  212. while (i == CS_SUCCESS) {
  213. if (cf->io.nwin > 0) {
  214. link->conf.ConfigIndex = cf->index;
  215. link->io.BasePort1 = cf->io.win[0].base;
  216. link->io.NumPorts1 = cf->io.win[0].len;
  217. link->io.NumPorts2 = 0;
  218. printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
  219. link->io.BasePort1,
  220. link->io.BasePort1+link->io.NumPorts1 - 1);
  221. i = pcmcia_request_io(link, &link->io);
  222. if (i == CS_SUCCESS) goto found_port;
  223. }
  224. i = next_tuple(link, &tuple, &parse);
  225. }
  226. found_port:
  227. if (i != CS_SUCCESS) {
  228. cs_error(link, RequestIO, i);
  229. break;
  230. }
  231. /*
  232. * allocate an interrupt line
  233. */
  234. i = pcmcia_request_irq(link, &link->irq);
  235. if (i != CS_SUCCESS) {
  236. cs_error(link, RequestIRQ, i);
  237. /* undo */
  238. pcmcia_disable_device(link);
  239. break;
  240. }
  241. /*
  242. * configure the PCMCIA socket
  243. */
  244. i = pcmcia_request_configuration(link, &link->conf);
  245. if (i != CS_SUCCESS) {
  246. cs_error(link, RequestConfiguration, i);
  247. pcmcia_disable_device(link);
  248. break;
  249. }
  250. } while (0);
  251. /* At this point, the dev_node_t structure(s) should be
  252. initialized and arranged in a linked list at link->dev. */
  253. strcpy(dev->node.dev_name, "A1");
  254. dev->node.major = 45;
  255. dev->node.minor = 0;
  256. link->dev_node = &dev->node;
  257. /* If any step failed, release any partially configured state */
  258. if (i != 0) {
  259. avma1cs_release(link);
  260. return -ENODEV;
  261. }
  262. printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
  263. link->io.BasePort1, link->irq.AssignedIRQ);
  264. icard.para[0] = link->irq.AssignedIRQ;
  265. icard.para[1] = link->io.BasePort1;
  266. icard.protocol = isdnprot;
  267. icard.typ = ISDN_CTYPE_A1_PCMCIA;
  268. i = hisax_init_pcmcia(link, &busy, &icard);
  269. if (i < 0) {
  270. printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
  271. avma1cs_release(link);
  272. return -ENODEV;
  273. }
  274. dev->node.minor = i;
  275. return 0;
  276. } /* avma1cs_config */
  277. /*======================================================================
  278. After a card is removed, avma1cs_release() will unregister the net
  279. device, and release the PCMCIA configuration. If the device is
  280. still open, this will be postponed until it is closed.
  281. ======================================================================*/
  282. static void avma1cs_release(struct pcmcia_device *link)
  283. {
  284. local_info_t *local = link->priv;
  285. DEBUG(0, "avma1cs_release(0x%p)\n", link);
  286. /* now unregister function with hisax */
  287. HiSax_closecard(local->node.minor);
  288. pcmcia_disable_device(link);
  289. } /* avma1cs_release */
  290. static struct pcmcia_device_id avma1cs_ids[] = {
  291. PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
  292. PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
  293. PCMCIA_DEVICE_NULL
  294. };
  295. MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
  296. static struct pcmcia_driver avma1cs_driver = {
  297. .owner = THIS_MODULE,
  298. .drv = {
  299. .name = "avma1_cs",
  300. },
  301. .probe = avma1cs_probe,
  302. .remove = avma1cs_detach,
  303. .id_table = avma1cs_ids,
  304. };
  305. /*====================================================================*/
  306. static int __init init_avma1_cs(void)
  307. {
  308. return(pcmcia_register_driver(&avma1cs_driver));
  309. }
  310. static void __exit exit_avma1_cs(void)
  311. {
  312. pcmcia_unregister_driver(&avma1cs_driver);
  313. }
  314. module_init(init_avma1_cs);
  315. module_exit(exit_avma1_cs);