avma1_cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. static int avma1cs_event(event_t event, int priority,
  60. event_callback_args_t *args);
  61. /*
  62. The attach() and detach() entry points are used to create and destroy
  63. "instances" of the driver, where each instance represents everything
  64. needed to manage one actual PCMCIA card.
  65. */
  66. static dev_link_t *avma1cs_attach(void);
  67. static void avma1cs_detach(dev_link_t *);
  68. /*
  69. The dev_info variable is the "key" that is used to match up this
  70. device driver with appropriate cards, through the card configuration
  71. database.
  72. */
  73. static dev_info_t dev_info = "avma1_cs";
  74. /*
  75. A linked list of "instances" of the skeleton device. Each actual
  76. PCMCIA card corresponds to one device instance, and is described
  77. by one dev_link_t structure (defined in ds.h).
  78. You may not want to use a linked list for this -- for example, the
  79. memory card driver uses an array of dev_link_t pointers, where minor
  80. device numbers are used to derive the corresponding array index.
  81. */
  82. static dev_link_t *dev_list = NULL;
  83. /*
  84. A dev_link_t structure has fields for most things that are needed
  85. to keep track of a socket, but there will usually be some device
  86. specific information that also needs to be kept track of. The
  87. 'priv' pointer in a dev_link_t structure can be used to point to
  88. a device-specific private data structure, like this.
  89. A driver needs to provide a dev_node_t structure for each device
  90. on a card. In some cases, there is only one device per card (for
  91. example, ethernet cards, modems). In other cases, there may be
  92. many actual or logical devices (SCSI adapters, memory cards with
  93. multiple partitions). The dev_node_t structures need to be kept
  94. in a linked list starting at the 'dev' field of a dev_link_t
  95. structure. We allocate them in the card's private data structure,
  96. because they generally can't be allocated dynamically.
  97. */
  98. typedef struct local_info_t {
  99. dev_node_t node;
  100. } local_info_t;
  101. /*======================================================================
  102. avma1cs_attach() creates an "instance" of the driver, allocating
  103. local data structures for one device. The device is registered
  104. with Card Services.
  105. The dev_link structure is initialized, but we don't actually
  106. configure the card at this point -- we wait until we receive a
  107. card insertion event.
  108. ======================================================================*/
  109. static dev_link_t *avma1cs_attach(void)
  110. {
  111. client_reg_t client_reg;
  112. dev_link_t *link;
  113. local_info_t *local;
  114. int ret;
  115. DEBUG(0, "avma1cs_attach()\n");
  116. /* Initialize the dev_link_t structure */
  117. link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
  118. if (!link)
  119. return NULL;
  120. memset(link, 0, sizeof(struct dev_link_t));
  121. /* Allocate space for private device-specific data */
  122. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  123. if (!local) {
  124. kfree(link);
  125. return NULL;
  126. }
  127. memset(local, 0, sizeof(local_info_t));
  128. link->priv = local;
  129. /* The io structure describes IO port mapping */
  130. link->io.NumPorts1 = 16;
  131. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  132. link->io.NumPorts2 = 16;
  133. link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
  134. link->io.IOAddrLines = 5;
  135. /* Interrupt setup */
  136. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  137. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  138. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  139. /* General socket configuration */
  140. link->conf.Attributes = CONF_ENABLE_IRQ;
  141. link->conf.Vcc = 50;
  142. link->conf.IntType = INT_MEMORY_AND_IO;
  143. link->conf.ConfigIndex = 1;
  144. link->conf.Present = PRESENT_OPTION;
  145. /* Register with Card Services */
  146. link->next = dev_list;
  147. dev_list = link;
  148. client_reg.dev_info = &dev_info;
  149. client_reg.Version = 0x0210;
  150. client_reg.event_callback_args.client_data = link;
  151. ret = pcmcia_register_client(&link->handle, &client_reg);
  152. if (ret != 0) {
  153. cs_error(link->handle, RegisterClient, ret);
  154. avma1cs_detach(link);
  155. return NULL;
  156. }
  157. return link;
  158. } /* avma1cs_attach */
  159. /*======================================================================
  160. This deletes a driver "instance". The device is de-registered
  161. with Card Services. If it has been released, all local data
  162. structures are freed. Otherwise, the structures will be freed
  163. when the device is released.
  164. ======================================================================*/
  165. static void avma1cs_detach(dev_link_t *link)
  166. {
  167. dev_link_t **linkp;
  168. DEBUG(0, "avma1cs_detach(0x%p)\n", link);
  169. /* Locate device structure */
  170. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  171. if (*linkp == link) break;
  172. if (*linkp == NULL)
  173. return;
  174. /*
  175. If the device is currently configured and active, we won't
  176. actually delete it yet. Instead, it is marked so that when
  177. the release() function is called, that will trigger a proper
  178. detach().
  179. */
  180. if (link->state & DEV_CONFIG) {
  181. #ifdef PCMCIA_DEBUG
  182. printk(KERN_DEBUG "avma1_cs: detach postponed, '%s' "
  183. "still locked\n", link->dev->dev_name);
  184. #endif
  185. link->state |= DEV_STALE_LINK;
  186. return;
  187. }
  188. /* Break the link with Card Services */
  189. if (link->handle)
  190. pcmcia_deregister_client(link->handle);
  191. /* Unlink device structure, free pieces */
  192. *linkp = link->next;
  193. if (link->priv) {
  194. kfree(link->priv);
  195. }
  196. kfree(link);
  197. } /* avma1cs_detach */
  198. /*======================================================================
  199. avma1cs_config() is scheduled to run after a CARD_INSERTION event
  200. is received, to configure the PCMCIA socket, and to make the
  201. ethernet device available to the system.
  202. ======================================================================*/
  203. static int get_tuple(client_handle_t handle, tuple_t *tuple,
  204. cisparse_t *parse)
  205. {
  206. int i = pcmcia_get_tuple_data(handle, tuple);
  207. if (i != CS_SUCCESS) return i;
  208. return pcmcia_parse_tuple(handle, tuple, parse);
  209. }
  210. static int first_tuple(client_handle_t handle, tuple_t *tuple,
  211. cisparse_t *parse)
  212. {
  213. int i = pcmcia_get_first_tuple(handle, tuple);
  214. if (i != CS_SUCCESS) return i;
  215. return get_tuple(handle, tuple, parse);
  216. }
  217. static int next_tuple(client_handle_t handle, tuple_t *tuple,
  218. cisparse_t *parse)
  219. {
  220. int i = pcmcia_get_next_tuple(handle, tuple);
  221. if (i != CS_SUCCESS) return i;
  222. return get_tuple(handle, tuple, parse);
  223. }
  224. static void avma1cs_config(dev_link_t *link)
  225. {
  226. client_handle_t handle;
  227. tuple_t tuple;
  228. cisparse_t parse;
  229. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  230. local_info_t *dev;
  231. int i;
  232. u_char buf[64];
  233. char devname[128];
  234. IsdnCard_t icard;
  235. int busy = 0;
  236. handle = link->handle;
  237. dev = link->priv;
  238. DEBUG(0, "avma1cs_config(0x%p)\n", link);
  239. /*
  240. This reads the card's CONFIG tuple to find its configuration
  241. registers.
  242. */
  243. do {
  244. tuple.DesiredTuple = CISTPL_CONFIG;
  245. i = pcmcia_get_first_tuple(handle, &tuple);
  246. if (i != CS_SUCCESS) break;
  247. tuple.TupleData = buf;
  248. tuple.TupleDataMax = 64;
  249. tuple.TupleOffset = 0;
  250. i = pcmcia_get_tuple_data(handle, &tuple);
  251. if (i != CS_SUCCESS) break;
  252. i = pcmcia_parse_tuple(handle, &tuple, &parse);
  253. if (i != CS_SUCCESS) break;
  254. link->conf.ConfigBase = parse.config.base;
  255. } while (0);
  256. if (i != CS_SUCCESS) {
  257. cs_error(link->handle, ParseTuple, i);
  258. link->state &= ~DEV_CONFIG_PENDING;
  259. return;
  260. }
  261. /* Configure card */
  262. link->state |= DEV_CONFIG;
  263. do {
  264. tuple.Attributes = 0;
  265. tuple.TupleData = buf;
  266. tuple.TupleDataMax = 254;
  267. tuple.TupleOffset = 0;
  268. tuple.DesiredTuple = CISTPL_VERS_1;
  269. devname[0] = 0;
  270. if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
  271. strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
  272. sizeof(devname));
  273. }
  274. /*
  275. * find IO port
  276. */
  277. tuple.TupleData = (cisdata_t *)buf;
  278. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  279. tuple.Attributes = 0;
  280. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  281. i = first_tuple(handle, &tuple, &parse);
  282. while (i == CS_SUCCESS) {
  283. if (cf->io.nwin > 0) {
  284. link->conf.ConfigIndex = cf->index;
  285. link->io.BasePort1 = cf->io.win[0].base;
  286. link->io.NumPorts1 = cf->io.win[0].len;
  287. link->io.NumPorts2 = 0;
  288. printk(KERN_INFO "avma1_cs: testing i/o %#x-%#x\n",
  289. link->io.BasePort1,
  290. link->io.BasePort1+link->io.NumPorts1 - 1);
  291. i = pcmcia_request_io(link->handle, &link->io);
  292. if (i == CS_SUCCESS) goto found_port;
  293. }
  294. i = next_tuple(handle, &tuple, &parse);
  295. }
  296. found_port:
  297. if (i != CS_SUCCESS) {
  298. cs_error(link->handle, RequestIO, i);
  299. break;
  300. }
  301. /*
  302. * allocate an interrupt line
  303. */
  304. i = pcmcia_request_irq(link->handle, &link->irq);
  305. if (i != CS_SUCCESS) {
  306. cs_error(link->handle, RequestIRQ, i);
  307. pcmcia_release_io(link->handle, &link->io);
  308. break;
  309. }
  310. /*
  311. * configure the PCMCIA socket
  312. */
  313. i = pcmcia_request_configuration(link->handle, &link->conf);
  314. if (i != CS_SUCCESS) {
  315. cs_error(link->handle, RequestConfiguration, i);
  316. pcmcia_release_io(link->handle, &link->io);
  317. pcmcia_release_irq(link->handle, &link->irq);
  318. break;
  319. }
  320. } while (0);
  321. /* At this point, the dev_node_t structure(s) should be
  322. initialized and arranged in a linked list at link->dev. */
  323. strcpy(dev->node.dev_name, "A1");
  324. dev->node.major = 45;
  325. dev->node.minor = 0;
  326. link->dev = &dev->node;
  327. link->state &= ~DEV_CONFIG_PENDING;
  328. /* If any step failed, release any partially configured state */
  329. if (i != 0) {
  330. avma1cs_release(link);
  331. return;
  332. }
  333. printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %d\n",
  334. link->io.BasePort1, link->irq.AssignedIRQ);
  335. icard.para[0] = link->irq.AssignedIRQ;
  336. icard.para[1] = link->io.BasePort1;
  337. icard.protocol = isdnprot;
  338. icard.typ = ISDN_CTYPE_A1_PCMCIA;
  339. i = hisax_init_pcmcia(link, &busy, &icard);
  340. if (i < 0) {
  341. printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#x\n", i, link->io.BasePort1);
  342. avma1cs_release(link);
  343. return;
  344. }
  345. dev->node.minor = i;
  346. } /* avma1cs_config */
  347. /*======================================================================
  348. After a card is removed, avma1cs_release() will unregister the net
  349. device, and release the PCMCIA configuration. If the device is
  350. still open, this will be postponed until it is closed.
  351. ======================================================================*/
  352. static void avma1cs_release(dev_link_t *link)
  353. {
  354. local_info_t *local = link->priv;
  355. DEBUG(0, "avma1cs_release(0x%p)\n", link);
  356. /* no unregister function with hisax */
  357. HiSax_closecard(local->node.minor);
  358. /* Unlink the device chain */
  359. link->dev = NULL;
  360. /* Don't bother checking to see if these succeed or not */
  361. pcmcia_release_configuration(link->handle);
  362. pcmcia_release_io(link->handle, &link->io);
  363. pcmcia_release_irq(link->handle, &link->irq);
  364. link->state &= ~DEV_CONFIG;
  365. if (link->state & DEV_STALE_LINK)
  366. avma1cs_detach(link);
  367. } /* avma1cs_release */
  368. /*======================================================================
  369. The card status event handler. Mostly, this schedules other
  370. stuff to run after an event is received. A CARD_REMOVAL event
  371. also sets some flags to discourage the net drivers from trying
  372. to talk to the card any more.
  373. When a CARD_REMOVAL event is received, we immediately set a flag
  374. to block future accesses to this device. All the functions that
  375. actually access the device should check this flag to make sure
  376. the card is still present.
  377. ======================================================================*/
  378. static int avma1cs_event(event_t event, int priority,
  379. event_callback_args_t *args)
  380. {
  381. dev_link_t *link = args->client_data;
  382. DEBUG(1, "avma1cs_event(0x%06x)\n", event);
  383. switch (event) {
  384. case CS_EVENT_CARD_REMOVAL:
  385. if (link->state & DEV_CONFIG)
  386. avma1cs_release(link);
  387. break;
  388. case CS_EVENT_CARD_INSERTION:
  389. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  390. avma1cs_config(link);
  391. break;
  392. case CS_EVENT_PM_SUSPEND:
  393. link->state |= DEV_SUSPEND;
  394. /* Fall through... */
  395. case CS_EVENT_RESET_PHYSICAL:
  396. if (link->state & DEV_CONFIG)
  397. pcmcia_release_configuration(link->handle);
  398. break;
  399. case CS_EVENT_PM_RESUME:
  400. link->state &= ~DEV_SUSPEND;
  401. /* Fall through... */
  402. case CS_EVENT_CARD_RESET:
  403. if (link->state & DEV_CONFIG)
  404. pcmcia_request_configuration(link->handle, &link->conf);
  405. break;
  406. }
  407. return 0;
  408. } /* avma1cs_event */
  409. static struct pcmcia_device_id avma1cs_ids[] = {
  410. PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN A", 0x95d42008, 0xadc9d4bb),
  411. PCMCIA_DEVICE_PROD_ID12("ISDN", "CARD", 0x8d9761c8, 0x01c5aa7b),
  412. PCMCIA_DEVICE_NULL
  413. };
  414. MODULE_DEVICE_TABLE(pcmcia, avma1cs_ids);
  415. static struct pcmcia_driver avma1cs_driver = {
  416. .owner = THIS_MODULE,
  417. .drv = {
  418. .name = "avma1_cs",
  419. },
  420. .attach = avma1cs_attach,
  421. .event = avma1cs_event,
  422. .detach = avma1cs_detach,
  423. .id_table = avma1cs_ids,
  424. };
  425. /*====================================================================*/
  426. static int __init init_avma1_cs(void)
  427. {
  428. return(pcmcia_register_driver(&avma1cs_driver));
  429. }
  430. static void __exit exit_avma1_cs(void)
  431. {
  432. pcmcia_unregister_driver(&avma1cs_driver);
  433. BUG_ON(dev_list != NULL);
  434. }
  435. module_init(init_avma1_cs);
  436. module_exit(exit_avma1_cs);