avm_cs.c 15 KB

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