avm_cs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. /*
  49. The attach() and detach() entry points are used to create and destroy
  50. "instances" of the driver, where each instance represents everything
  51. needed to manage one actual PCMCIA card.
  52. */
  53. static void avmcs_detach(struct pcmcia_device *p_dev);
  54. /*
  55. A linked list of "instances" of the skeleton device. Each actual
  56. PCMCIA card corresponds to one device instance, and is described
  57. by one dev_link_t structure (defined in ds.h).
  58. You may not want to use a linked list for this -- for example, the
  59. memory card driver uses an array of dev_link_t pointers, where minor
  60. device numbers are used to derive the corresponding array index.
  61. */
  62. /*
  63. A driver needs to provide a dev_node_t structure for each device
  64. on a card. In some cases, there is only one device per card (for
  65. example, ethernet cards, modems). In other cases, there may be
  66. many actual or logical devices (SCSI adapters, memory cards with
  67. multiple partitions). The dev_node_t structures need to be kept
  68. in a linked list starting at the 'dev' field of a dev_link_t
  69. structure. We allocate them in the card's private data structure,
  70. because they generally can't be allocated dynamically.
  71. */
  72. typedef struct local_info_t {
  73. dev_node_t node;
  74. } local_info_t;
  75. /*======================================================================
  76. avmcs_attach() creates an "instance" of the driver, allocating
  77. local data structures for one device. The device is registered
  78. with Card Services.
  79. The dev_link structure is initialized, but we don't actually
  80. configure the card at this point -- we wait until we receive a
  81. card insertion event.
  82. ======================================================================*/
  83. static int avmcs_attach(struct pcmcia_device *p_dev)
  84. {
  85. dev_link_t *link;
  86. local_info_t *local;
  87. /* Initialize the dev_link_t structure */
  88. link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
  89. if (!link)
  90. goto err;
  91. memset(link, 0, sizeof(struct dev_link_t));
  92. /* The io structure describes IO port mapping */
  93. link->io.NumPorts1 = 16;
  94. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  95. link->io.NumPorts2 = 0;
  96. /* Interrupt setup */
  97. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  98. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  99. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  100. /* General socket configuration */
  101. link->conf.Attributes = CONF_ENABLE_IRQ;
  102. link->conf.Vcc = 50;
  103. link->conf.IntType = INT_MEMORY_AND_IO;
  104. link->conf.ConfigIndex = 1;
  105. link->conf.Present = PRESENT_OPTION;
  106. /* Allocate space for private device-specific data */
  107. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  108. if (!local)
  109. goto err_kfree;
  110. memset(local, 0, sizeof(local_info_t));
  111. link->priv = local;
  112. link->handle = p_dev;
  113. p_dev->instance = link;
  114. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  115. avmcs_config(link);
  116. return 0;
  117. err_kfree:
  118. kfree(link);
  119. err:
  120. return -EINVAL;
  121. } /* avmcs_attach */
  122. /*======================================================================
  123. This deletes a driver "instance". The device is de-registered
  124. with Card Services. If it has been released, all local data
  125. structures are freed. Otherwise, the structures will be freed
  126. when the device is released.
  127. ======================================================================*/
  128. static void avmcs_detach(struct pcmcia_device *p_dev)
  129. {
  130. dev_link_t *link = dev_to_instance(p_dev);
  131. if (link->state & DEV_CONFIG)
  132. avmcs_release(link);
  133. kfree(link->priv);
  134. kfree(link);
  135. } /* avmcs_detach */
  136. /*======================================================================
  137. avmcs_config() is scheduled to run after a CARD_INSERTION event
  138. is received, to configure the PCMCIA socket, and to make the
  139. ethernet device available to the system.
  140. ======================================================================*/
  141. static int get_tuple(client_handle_t handle, tuple_t *tuple,
  142. cisparse_t *parse)
  143. {
  144. int i = pcmcia_get_tuple_data(handle, tuple);
  145. if (i != CS_SUCCESS) return i;
  146. return pcmcia_parse_tuple(handle, tuple, parse);
  147. }
  148. static int first_tuple(client_handle_t handle, tuple_t *tuple,
  149. cisparse_t *parse)
  150. {
  151. int i = pcmcia_get_first_tuple(handle, tuple);
  152. if (i != CS_SUCCESS) return i;
  153. return get_tuple(handle, tuple, parse);
  154. }
  155. static int next_tuple(client_handle_t handle, tuple_t *tuple,
  156. cisparse_t *parse)
  157. {
  158. int i = pcmcia_get_next_tuple(handle, tuple);
  159. if (i != CS_SUCCESS) return i;
  160. return get_tuple(handle, tuple, parse);
  161. }
  162. static void avmcs_config(dev_link_t *link)
  163. {
  164. client_handle_t handle;
  165. tuple_t tuple;
  166. cisparse_t parse;
  167. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  168. local_info_t *dev;
  169. int i;
  170. u_char buf[64];
  171. char devname[128];
  172. int cardtype;
  173. int (*addcard)(unsigned int port, unsigned irq);
  174. handle = link->handle;
  175. dev = link->priv;
  176. /*
  177. This reads the card's CONFIG tuple to find its configuration
  178. registers.
  179. */
  180. do {
  181. tuple.DesiredTuple = CISTPL_CONFIG;
  182. i = pcmcia_get_first_tuple(handle, &tuple);
  183. if (i != CS_SUCCESS) break;
  184. tuple.TupleData = buf;
  185. tuple.TupleDataMax = 64;
  186. tuple.TupleOffset = 0;
  187. i = pcmcia_get_tuple_data(handle, &tuple);
  188. if (i != CS_SUCCESS) break;
  189. i = pcmcia_parse_tuple(handle, &tuple, &parse);
  190. if (i != CS_SUCCESS) break;
  191. link->conf.ConfigBase = parse.config.base;
  192. } while (0);
  193. if (i != CS_SUCCESS) {
  194. cs_error(link->handle, ParseTuple, i);
  195. link->state &= ~DEV_CONFIG_PENDING;
  196. return;
  197. }
  198. /* Configure card */
  199. link->state |= DEV_CONFIG;
  200. do {
  201. tuple.Attributes = 0;
  202. tuple.TupleData = buf;
  203. tuple.TupleDataMax = 254;
  204. tuple.TupleOffset = 0;
  205. tuple.DesiredTuple = CISTPL_VERS_1;
  206. devname[0] = 0;
  207. if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
  208. strlcpy(devname,parse.version_1.str + parse.version_1.ofs[1],
  209. sizeof(devname));
  210. }
  211. /*
  212. * find IO port
  213. */
  214. tuple.TupleData = (cisdata_t *)buf;
  215. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  216. tuple.Attributes = 0;
  217. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  218. i = first_tuple(handle, &tuple, &parse);
  219. while (i == CS_SUCCESS) {
  220. if (cf->io.nwin > 0) {
  221. link->conf.ConfigIndex = cf->index;
  222. link->io.BasePort1 = cf->io.win[0].base;
  223. link->io.NumPorts1 = cf->io.win[0].len;
  224. link->io.NumPorts2 = 0;
  225. printk(KERN_INFO "avm_cs: testing i/o %#x-%#x\n",
  226. link->io.BasePort1,
  227. link->io.BasePort1+link->io.NumPorts1-1);
  228. i = pcmcia_request_io(link->handle, &link->io);
  229. if (i == CS_SUCCESS) goto found_port;
  230. }
  231. i = next_tuple(handle, &tuple, &parse);
  232. }
  233. found_port:
  234. if (i != CS_SUCCESS) {
  235. cs_error(link->handle, RequestIO, i);
  236. break;
  237. }
  238. /*
  239. * allocate an interrupt line
  240. */
  241. i = pcmcia_request_irq(link->handle, &link->irq);
  242. if (i != CS_SUCCESS) {
  243. cs_error(link->handle, RequestIRQ, i);
  244. pcmcia_release_io(link->handle, &link->io);
  245. break;
  246. }
  247. /*
  248. * configure the PCMCIA socket
  249. */
  250. i = pcmcia_request_configuration(link->handle, &link->conf);
  251. if (i != CS_SUCCESS) {
  252. cs_error(link->handle, RequestConfiguration, i);
  253. pcmcia_release_io(link->handle, &link->io);
  254. pcmcia_release_irq(link->handle, &link->irq);
  255. break;
  256. }
  257. } while (0);
  258. /* At this point, the dev_node_t structure(s) should be
  259. initialized and arranged in a linked list at link->dev. */
  260. if (devname[0]) {
  261. char *s = strrchr(devname, ' ');
  262. if (!s)
  263. s = devname;
  264. else s++;
  265. strcpy(dev->node.dev_name, s);
  266. if (strcmp("M1", s) == 0) {
  267. cardtype = AVM_CARDTYPE_M1;
  268. } else if (strcmp("M2", s) == 0) {
  269. cardtype = AVM_CARDTYPE_M2;
  270. } else {
  271. cardtype = AVM_CARDTYPE_B1;
  272. }
  273. } else {
  274. strcpy(dev->node.dev_name, "b1");
  275. cardtype = AVM_CARDTYPE_B1;
  276. }
  277. dev->node.major = 64;
  278. dev->node.minor = 0;
  279. link->dev = &dev->node;
  280. link->state &= ~DEV_CONFIG_PENDING;
  281. /* If any step failed, release any partially configured state */
  282. if (i != 0) {
  283. avmcs_release(link);
  284. return;
  285. }
  286. switch (cardtype) {
  287. case AVM_CARDTYPE_M1: addcard = b1pcmcia_addcard_m1; break;
  288. case AVM_CARDTYPE_M2: addcard = b1pcmcia_addcard_m2; break;
  289. default:
  290. case AVM_CARDTYPE_B1: addcard = b1pcmcia_addcard_b1; break;
  291. }
  292. if ((i = (*addcard)(link->io.BasePort1, link->irq.AssignedIRQ)) < 0) {
  293. printk(KERN_ERR "avm_cs: failed to add AVM-%s-Controller at i/o %#x, irq %d\n",
  294. dev->node.dev_name, link->io.BasePort1, link->irq.AssignedIRQ);
  295. avmcs_release(link);
  296. return;
  297. }
  298. dev->node.minor = i;
  299. } /* avmcs_config */
  300. /*======================================================================
  301. After a card is removed, avmcs_release() will unregister the net
  302. device, and release the PCMCIA configuration. If the device is
  303. still open, this will be postponed until it is closed.
  304. ======================================================================*/
  305. static void avmcs_release(dev_link_t *link)
  306. {
  307. b1pcmcia_delcard(link->io.BasePort1, link->irq.AssignedIRQ);
  308. /* Unlink the device chain */
  309. link->dev = NULL;
  310. /* Don't bother checking to see if these succeed or not */
  311. pcmcia_release_configuration(link->handle);
  312. pcmcia_release_io(link->handle, &link->io);
  313. pcmcia_release_irq(link->handle, &link->irq);
  314. link->state &= ~DEV_CONFIG;
  315. } /* avmcs_release */
  316. static int avmcs_suspend(struct pcmcia_device *dev)
  317. {
  318. dev_link_t *link = dev_to_instance(dev);
  319. link->state |= DEV_SUSPEND;
  320. if (link->state & DEV_CONFIG)
  321. pcmcia_release_configuration(link->handle);
  322. return 0;
  323. }
  324. static int avmcs_resume(struct pcmcia_device *dev)
  325. {
  326. dev_link_t *link = dev_to_instance(dev);
  327. link->state &= ~DEV_SUSPEND;
  328. if (link->state & DEV_CONFIG)
  329. pcmcia_request_configuration(link->handle, &link->conf);
  330. return 0;
  331. }
  332. /*======================================================================
  333. The card status event handler. Mostly, this schedules other
  334. stuff to run after an event is received. A CARD_REMOVAL event
  335. also sets some flags to discourage the net drivers from trying
  336. to talk to the card any more.
  337. When a CARD_REMOVAL event is received, we immediately set a flag
  338. to block future accesses to this device. All the functions that
  339. actually access the device should check this flag to make sure
  340. the card is still present.
  341. ======================================================================*/
  342. static struct pcmcia_device_id avmcs_ids[] = {
  343. PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN-Controller B1", 0x95d42008, 0x845dc335),
  344. PCMCIA_DEVICE_PROD_ID12("AVM", "Mobile ISDN-Controller M1", 0x95d42008, 0x81e10430),
  345. PCMCIA_DEVICE_PROD_ID12("AVM", "Mobile ISDN-Controller M2", 0x95d42008, 0x18e8558a),
  346. PCMCIA_DEVICE_NULL
  347. };
  348. MODULE_DEVICE_TABLE(pcmcia, avmcs_ids);
  349. static struct pcmcia_driver avmcs_driver = {
  350. .owner = THIS_MODULE,
  351. .drv = {
  352. .name = "avm_cs",
  353. },
  354. .probe = avmcs_attach,
  355. .remove = avmcs_detach,
  356. .id_table = avmcs_ids,
  357. .suspend= avmcs_suspend,
  358. .resume = avmcs_resume,
  359. };
  360. static int __init avmcs_init(void)
  361. {
  362. return pcmcia_register_driver(&avmcs_driver);
  363. }
  364. static void __exit avmcs_exit(void)
  365. {
  366. pcmcia_unregister_driver(&avmcs_driver);
  367. }
  368. module_init(avmcs_init);
  369. module_exit(avmcs_exit);