avma1_cs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 void avma1cs_config(dev_link_t *link);
  58. static void avma1cs_release(dev_link_t *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 dev_link_t 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 dev_link_t 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 dev_link_t
  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_attach(struct pcmcia_device *p_dev)
  95. {
  96. dev_link_t *link;
  97. local_info_t *local;
  98. DEBUG(0, "avma1cs_attach()\n");
  99. /* Initialize the dev_link_t structure */
  100. link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
  101. if (!link)
  102. return -ENOMEM;
  103. memset(link, 0, sizeof(struct dev_link_t));
  104. /* Allocate space for private device-specific data */
  105. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  106. if (!local) {
  107. kfree(link);
  108. return -ENOMEM;
  109. }
  110. memset(local, 0, sizeof(local_info_t));
  111. link->priv = local;
  112. /* The io structure describes IO port mapping */
  113. link->io.NumPorts1 = 16;
  114. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  115. link->io.NumPorts2 = 16;
  116. link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
  117. link->io.IOAddrLines = 5;
  118. /* Interrupt setup */
  119. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  120. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  121. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  122. /* General socket configuration */
  123. link->conf.Attributes = CONF_ENABLE_IRQ;
  124. link->conf.Vcc = 50;
  125. link->conf.IntType = INT_MEMORY_AND_IO;
  126. link->conf.ConfigIndex = 1;
  127. link->conf.Present = PRESENT_OPTION;
  128. link->handle = p_dev;
  129. p_dev->instance = link;
  130. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  131. avma1cs_config(link);
  132. return 0;
  133. } /* avma1cs_attach */
  134. /*======================================================================
  135. This deletes a driver "instance". The device is de-registered
  136. with Card Services. If it has been released, all local data
  137. structures are freed. Otherwise, the structures will be freed
  138. when the device is released.
  139. ======================================================================*/
  140. static void avma1cs_detach(struct pcmcia_device *p_dev)
  141. {
  142. dev_link_t *link = dev_to_instance(p_dev);
  143. DEBUG(0, "avma1cs_detach(0x%p)\n", link);
  144. if (link->state & DEV_CONFIG)
  145. avma1cs_release(link);
  146. kfree(link->priv);
  147. kfree(link);
  148. } /* avma1cs_detach */
  149. /*======================================================================
  150. avma1cs_config() is scheduled to run after a CARD_INSERTION event
  151. is received, to configure the PCMCIA socket, and to make the
  152. ethernet device available to the system.
  153. ======================================================================*/
  154. static int get_tuple(client_handle_t handle, tuple_t *tuple,
  155. cisparse_t *parse)
  156. {
  157. int i = pcmcia_get_tuple_data(handle, tuple);
  158. if (i != CS_SUCCESS) return i;
  159. return pcmcia_parse_tuple(handle, tuple, parse);
  160. }
  161. static int first_tuple(client_handle_t handle, tuple_t *tuple,
  162. cisparse_t *parse)
  163. {
  164. int i = pcmcia_get_first_tuple(handle, tuple);
  165. if (i != CS_SUCCESS) return i;
  166. return get_tuple(handle, tuple, parse);
  167. }
  168. static int next_tuple(client_handle_t handle, tuple_t *tuple,
  169. cisparse_t *parse)
  170. {
  171. int i = pcmcia_get_next_tuple(handle, tuple);
  172. if (i != CS_SUCCESS) return i;
  173. return get_tuple(handle, tuple, parse);
  174. }
  175. static void avma1cs_config(dev_link_t *link)
  176. {
  177. client_handle_t handle;
  178. tuple_t tuple;
  179. cisparse_t parse;
  180. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  181. local_info_t *dev;
  182. int i;
  183. u_char buf[64];
  184. char devname[128];
  185. IsdnCard_t icard;
  186. int busy = 0;
  187. handle = link->handle;
  188. dev = link->priv;
  189. DEBUG(0, "avma1cs_config(0x%p)\n", link);
  190. /*
  191. This reads the card's CONFIG tuple to find its configuration
  192. registers.
  193. */
  194. do {
  195. tuple.DesiredTuple = CISTPL_CONFIG;
  196. i = pcmcia_get_first_tuple(handle, &tuple);
  197. if (i != CS_SUCCESS) break;
  198. tuple.TupleData = buf;
  199. tuple.TupleDataMax = 64;
  200. tuple.TupleOffset = 0;
  201. i = pcmcia_get_tuple_data(handle, &tuple);
  202. if (i != CS_SUCCESS) break;
  203. i = pcmcia_parse_tuple(handle, &tuple, &parse);
  204. if (i != CS_SUCCESS) break;
  205. link->conf.ConfigBase = parse.config.base;
  206. } while (0);
  207. if (i != CS_SUCCESS) {
  208. cs_error(link->handle, ParseTuple, i);
  209. link->state &= ~DEV_CONFIG_PENDING;
  210. return;
  211. }
  212. /* Configure card */
  213. link->state |= DEV_CONFIG;
  214. do {
  215. tuple.Attributes = 0;
  216. tuple.TupleData = buf;
  217. tuple.TupleDataMax = 254;
  218. tuple.TupleOffset = 0;
  219. tuple.DesiredTuple = CISTPL_VERS_1;
  220. devname[0] = 0;
  221. if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
  222. strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
  223. sizeof(devname));
  224. }
  225. /*
  226. * find IO port
  227. */
  228. tuple.TupleData = (cisdata_t *)buf;
  229. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  230. tuple.Attributes = 0;
  231. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  232. i = first_tuple(handle, &tuple, &parse);
  233. while (i == CS_SUCCESS) {
  234. if (cf->io.nwin > 0) {
  235. link->conf.ConfigIndex = cf->index;
  236. link->io.BasePort1 = cf->io.win[0].base;
  237. link->io.NumPorts1 = cf->io.win[0].len;
  238. link->io.NumPorts2 = 0;
  239. printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
  240. link->io.BasePort1,
  241. link->io.BasePort1+link->io.NumPorts1 - 1);
  242. i = pcmcia_request_io(link->handle, &link->io);
  243. if (i == CS_SUCCESS) goto found_port;
  244. }
  245. i = next_tuple(handle, &tuple, &parse);
  246. }
  247. found_port:
  248. if (i != CS_SUCCESS) {
  249. cs_error(link->handle, RequestIO, i);
  250. break;
  251. }
  252. /*
  253. * allocate an interrupt line
  254. */
  255. i = pcmcia_request_irq(link->handle, &link->irq);
  256. if (i != CS_SUCCESS) {
  257. cs_error(link->handle, RequestIRQ, i);
  258. pcmcia_release_io(link->handle, &link->io);
  259. break;
  260. }
  261. /*
  262. * configure the PCMCIA socket
  263. */
  264. i = pcmcia_request_configuration(link->handle, &link->conf);
  265. if (i != CS_SUCCESS) {
  266. cs_error(link->handle, RequestConfiguration, i);
  267. pcmcia_release_io(link->handle, &link->io);
  268. pcmcia_release_irq(link->handle, &link->irq);
  269. break;
  270. }
  271. } while (0);
  272. /* At this point, the dev_node_t structure(s) should be
  273. initialized and arranged in a linked list at link->dev. */
  274. strcpy(dev->node.dev_name, "A1");
  275. dev->node.major = 45;
  276. dev->node.minor = 0;
  277. link->dev = &dev->node;
  278. link->state &= ~DEV_CONFIG_PENDING;
  279. /* If any step failed, release any partially configured state */
  280. if (i != 0) {
  281. avma1cs_release(link);
  282. return;
  283. }
  284. printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
  285. link->io.BasePort1, link->irq.AssignedIRQ);
  286. icard.para[0] = link->irq.AssignedIRQ;
  287. icard.para[1] = link->io.BasePort1;
  288. icard.protocol = isdnprot;
  289. icard.typ = ISDN_CTYPE_A1_PCMCIA;
  290. i = hisax_init_pcmcia(link, &busy, &icard);
  291. if (i < 0) {
  292. printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
  293. avma1cs_release(link);
  294. return;
  295. }
  296. dev->node.minor = i;
  297. } /* avma1cs_config */
  298. /*======================================================================
  299. After a card is removed, avma1cs_release() will unregister the net
  300. device, and release the PCMCIA configuration. If the device is
  301. still open, this will be postponed until it is closed.
  302. ======================================================================*/
  303. static void avma1cs_release(dev_link_t *link)
  304. {
  305. local_info_t *local = link->priv;
  306. DEBUG(0, "avma1cs_release(0x%p)\n", link);
  307. /* no unregister function with hisax */
  308. HiSax_closecard(local->node.minor);
  309. /* Unlink the device chain */
  310. link->dev = NULL;
  311. /* Don't bother checking to see if these succeed or not */
  312. pcmcia_release_configuration(link->handle);
  313. pcmcia_release_io(link->handle, &link->io);
  314. pcmcia_release_irq(link->handle, &link->irq);
  315. link->state &= ~DEV_CONFIG;
  316. } /* avma1cs_release */
  317. static int avma1cs_suspend(struct pcmcia_device *dev)
  318. {
  319. dev_link_t *link = dev_to_instance(dev);
  320. link->state |= DEV_SUSPEND;
  321. if (link->state & DEV_CONFIG)
  322. pcmcia_release_configuration(link->handle);
  323. return 0;
  324. }
  325. static int avma1cs_resume(struct pcmcia_device *dev)
  326. {
  327. dev_link_t *link = dev_to_instance(dev);
  328. link->state &= ~DEV_SUSPEND;
  329. if (link->state & DEV_CONFIG)
  330. pcmcia_request_configuration(link->handle, &link->conf);
  331. return 0;
  332. }
  333. static struct pcmcia_device_id avma1cs_ids[] = {
  334. PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
  335. PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
  336. PCMCIA_DEVICE_NULL
  337. };
  338. MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
  339. static struct pcmcia_driver avma1cs_driver = {
  340. .owner = THIS_MODULE,
  341. .drv = {
  342. .name = "avma1_cs",
  343. },
  344. .probe = avma1cs_attach,
  345. .remove = avma1cs_detach,
  346. .id_table = avma1cs_ids,
  347. .suspend = avma1cs_suspend,
  348. .resume = avma1cs_resume,
  349. };
  350. /*====================================================================*/
  351. static int __init init_avma1_cs(void)
  352. {
  353. return(pcmcia_register_driver(&avma1cs_driver));
  354. }
  355. static void __exit exit_avma1_cs(void)
  356. {
  357. pcmcia_unregister_driver(&avma1cs_driver);
  358. }
  359. module_init(init_avma1_cs);
  360. module_exit(exit_avma1_cs);