avm_cs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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/ptrace.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/tty.h>
  18. #include <linux/serial.h>
  19. #include <linux/major.h>
  20. #include <asm/io.h>
  21. #include <asm/system.h>
  22. #include <pcmcia/cs_types.h>
  23. #include <pcmcia/cs.h>
  24. #include <pcmcia/cistpl.h>
  25. #include <pcmcia/ciscode.h>
  26. #include <pcmcia/ds.h>
  27. #include <pcmcia/cisreg.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/capi.h>
  30. #include <linux/b1lli.h>
  31. #include <linux/b1pcmcia.h>
  32. /*====================================================================*/
  33. MODULE_DESCRIPTION("CAPI4Linux: PCMCIA client driver for AVM B1/M1/M2");
  34. MODULE_AUTHOR("Carsten Paeth");
  35. MODULE_LICENSE("GPL");
  36. /*====================================================================*/
  37. /*
  38. The event() function is this driver's Card Services event handler.
  39. It will be called by Card Services when an appropriate card status
  40. event is received. The config() and release() entry points are
  41. used to configure or release a socket, in response to card insertion
  42. and ejection events. They are invoked from the skeleton event
  43. handler.
  44. */
  45. static int avmcs_config(struct pcmcia_device *link);
  46. static void avmcs_release(struct pcmcia_device *link);
  47. /*
  48. The attach() and detach() entry points are used to create and destroy
  49. "instances" of the driver, where each instance represents everything
  50. needed to manage one actual PCMCIA card.
  51. */
  52. static void avmcs_detach(struct pcmcia_device *p_dev);
  53. /*
  54. A linked list of "instances" of the skeleton device. Each actual
  55. PCMCIA card corresponds to one device instance, and is described
  56. by one struct pcmcia_device structure (defined in ds.h).
  57. You may not want to use a linked list for this -- for example, the
  58. memory card driver uses an array of struct pcmcia_device pointers, where minor
  59. device numbers are used to derive the corresponding array index.
  60. */
  61. /*
  62. A driver needs to provide a dev_node_t structure for each device
  63. on a card. In some cases, there is only one device per card (for
  64. example, ethernet cards, modems). In other cases, there may be
  65. many actual or logical devices (SCSI adapters, memory cards with
  66. multiple partitions). The dev_node_t structures need to be kept
  67. in a linked list starting at the 'dev' field of a struct pcmcia_device
  68. structure. We allocate them in the card's private data structure,
  69. because they generally can't be allocated dynamically.
  70. */
  71. typedef struct local_info_t {
  72. dev_node_t node;
  73. } local_info_t;
  74. /*======================================================================
  75. avmcs_attach() creates an "instance" of the driver, allocating
  76. local data structures for one device. The device is registered
  77. with Card Services.
  78. The dev_link structure is initialized, but we don't actually
  79. configure the card at this point -- we wait until we receive a
  80. card insertion event.
  81. ======================================================================*/
  82. static int avmcs_probe(struct pcmcia_device *p_dev)
  83. {
  84. local_info_t *local;
  85. /* The io structure describes IO port mapping */
  86. p_dev->io.NumPorts1 = 16;
  87. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  88. p_dev->io.NumPorts2 = 0;
  89. /* Interrupt setup */
  90. p_dev->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  91. p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  92. p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
  93. /* General socket configuration */
  94. p_dev->conf.Attributes = CONF_ENABLE_IRQ;
  95. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  96. p_dev->conf.ConfigIndex = 1;
  97. p_dev->conf.Present = PRESENT_OPTION;
  98. /* Allocate space for private device-specific data */
  99. local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
  100. if (!local)
  101. goto err;
  102. p_dev->priv = local;
  103. return avmcs_config(p_dev);
  104. err:
  105. return -ENOMEM;
  106. } /* avmcs_attach */
  107. /*======================================================================
  108. This deletes a driver "instance". The device is de-registered
  109. with Card Services. If it has been released, all local data
  110. structures are freed. Otherwise, the structures will be freed
  111. when the device is released.
  112. ======================================================================*/
  113. static void avmcs_detach(struct pcmcia_device *link)
  114. {
  115. avmcs_release(link);
  116. kfree(link->priv);
  117. } /* avmcs_detach */
  118. /*======================================================================
  119. avmcs_config() is scheduled to run after a CARD_INSERTION event
  120. is received, to configure the PCMCIA socket, and to make the
  121. ethernet device available to the system.
  122. ======================================================================*/
  123. static int avmcs_configcheck(struct pcmcia_device *p_dev,
  124. cistpl_cftable_entry_t *cf,
  125. cistpl_cftable_entry_t *dflt,
  126. unsigned int vcc,
  127. void *priv_data)
  128. {
  129. if (cf->io.nwin <= 0)
  130. return -ENODEV;
  131. p_dev->io.BasePort1 = cf->io.win[0].base;
  132. p_dev->io.NumPorts1 = cf->io.win[0].len;
  133. p_dev->io.NumPorts2 = 0;
  134. printk(KERN_INFO "avm_cs: testing i/o %#x-%#x\n",
  135. p_dev->io.BasePort1,
  136. p_dev->io.BasePort1+p_dev->io.NumPorts1-1);
  137. return pcmcia_request_io(p_dev, &p_dev->io);
  138. }
  139. static int avmcs_config(struct pcmcia_device *link)
  140. {
  141. local_info_t *dev;
  142. int i;
  143. char devname[128];
  144. int cardtype;
  145. int (*addcard)(unsigned int port, unsigned irq);
  146. dev = link->priv;
  147. devname[0] = 0;
  148. if (link->prod_id[1])
  149. strlcpy(devname, link->prod_id[1], sizeof(devname));
  150. /*
  151. * find IO port
  152. */
  153. if (pcmcia_loop_config(link, avmcs_configcheck, NULL))
  154. return -ENODEV;
  155. do {
  156. /*
  157. * allocate an interrupt line
  158. */
  159. i = pcmcia_request_irq(link, &link->irq);
  160. if (i != 0) {
  161. cs_error(link, RequestIRQ, i);
  162. /* undo */
  163. pcmcia_disable_device(link);
  164. break;
  165. }
  166. /*
  167. * configure the PCMCIA socket
  168. */
  169. i = pcmcia_request_configuration(link, &link->conf);
  170. if (i != 0) {
  171. cs_error(link, RequestConfiguration, i);
  172. pcmcia_disable_device(link);
  173. break;
  174. }
  175. } while (0);
  176. /* At this point, the dev_node_t structure(s) should be
  177. initialized and arranged in a linked list at link->dev. */
  178. if (devname[0]) {
  179. char *s = strrchr(devname, ' ');
  180. if (!s)
  181. s = devname;
  182. else s++;
  183. strcpy(dev->node.dev_name, s);
  184. if (strcmp("M1", s) == 0) {
  185. cardtype = AVM_CARDTYPE_M1;
  186. } else if (strcmp("M2", s) == 0) {
  187. cardtype = AVM_CARDTYPE_M2;
  188. } else {
  189. cardtype = AVM_CARDTYPE_B1;
  190. }
  191. } else {
  192. strcpy(dev->node.dev_name, "b1");
  193. cardtype = AVM_CARDTYPE_B1;
  194. }
  195. dev->node.major = 64;
  196. dev->node.minor = 0;
  197. link->dev_node = &dev->node;
  198. /* If any step failed, release any partially configured state */
  199. if (i != 0) {
  200. avmcs_release(link);
  201. return -ENODEV;
  202. }
  203. switch (cardtype) {
  204. case AVM_CARDTYPE_M1: addcard = b1pcmcia_addcard_m1; break;
  205. case AVM_CARDTYPE_M2: addcard = b1pcmcia_addcard_m2; break;
  206. default:
  207. case AVM_CARDTYPE_B1: addcard = b1pcmcia_addcard_b1; break;
  208. }
  209. if ((i = (*addcard)(link->io.BasePort1, link->irq.AssignedIRQ)) < 0) {
  210. printk(KERN_ERR "avm_cs: failed to add AVM-%s-Controller at i/o %#x, irq %d\n",
  211. dev->node.dev_name, link->io.BasePort1, link->irq.AssignedIRQ);
  212. avmcs_release(link);
  213. return -ENODEV;
  214. }
  215. dev->node.minor = i;
  216. return 0;
  217. } /* avmcs_config */
  218. /*======================================================================
  219. After a card is removed, avmcs_release() will unregister the net
  220. device, and release the PCMCIA configuration. If the device is
  221. still open, this will be postponed until it is closed.
  222. ======================================================================*/
  223. static void avmcs_release(struct pcmcia_device *link)
  224. {
  225. b1pcmcia_delcard(link->io.BasePort1, link->irq.AssignedIRQ);
  226. pcmcia_disable_device(link);
  227. } /* avmcs_release */
  228. static struct pcmcia_device_id avmcs_ids[] = {
  229. PCMCIA_DEVICE_PROD_ID12("AVM", "ISDN-Controller B1", 0x95d42008, 0x845dc335),
  230. PCMCIA_DEVICE_PROD_ID12("AVM", "Mobile ISDN-Controller M1", 0x95d42008, 0x81e10430),
  231. PCMCIA_DEVICE_PROD_ID12("AVM", "Mobile ISDN-Controller M2", 0x95d42008, 0x18e8558a),
  232. PCMCIA_DEVICE_NULL
  233. };
  234. MODULE_DEVICE_TABLE(pcmcia, avmcs_ids);
  235. static struct pcmcia_driver avmcs_driver = {
  236. .owner = THIS_MODULE,
  237. .drv = {
  238. .name = "avm_cs",
  239. },
  240. .probe = avmcs_probe,
  241. .remove = avmcs_detach,
  242. .id_table = avmcs_ids,
  243. };
  244. static int __init avmcs_init(void)
  245. {
  246. return pcmcia_register_driver(&avmcs_driver);
  247. }
  248. static void __exit avmcs_exit(void)
  249. {
  250. pcmcia_unregister_driver(&avmcs_driver);
  251. }
  252. module_init(avmcs_init);
  253. module_exit(avmcs_exit);