avma1_cs.c 15 KB

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