teles_cs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* $Id: teles_cs.c,v 1.1.2.2 2004/01/25 15:07:06 keil Exp $ */
  2. /*======================================================================
  3. A teles S0 PCMCIA client driver
  4. Based on skeleton by David Hinds, dhinds@allegro.stanford.edu
  5. Written by Christof Petig, christof.petig@wtal.de
  6. Also inspired by ELSA PCMCIA driver
  7. by Klaus Lichtenwalder <Lichtenwalder@ACM.org>
  8. Extentions to new hisax_pcmcia by Karsten Keil
  9. minor changes to be compatible with kernel 2.4.x
  10. by Jan.Schubert@GMX.li
  11. ======================================================================*/
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/timer.h>
  19. #include <linux/ioport.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/cisreg.h>
  26. #include <pcmcia/ds.h>
  27. #include "hisax_cfg.h"
  28. MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for Teles PCMCIA cards");
  29. MODULE_AUTHOR("Christof Petig, christof.petig@wtal.de, Karsten Keil, kkeil@suse.de");
  30. MODULE_LICENSE("GPL");
  31. /*
  32. All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
  33. you do not define PCMCIA_DEBUG at all, all the debug code will be
  34. left out. If you compile with PCMCIA_DEBUG=0, the debug code will
  35. be present but disabled -- but it can then be enabled for specific
  36. modules at load time with a 'pc_debug=#' option to insmod.
  37. */
  38. #ifdef PCMCIA_DEBUG
  39. static int pc_debug = PCMCIA_DEBUG;
  40. module_param(pc_debug, int, 0);
  41. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
  42. static char *version =
  43. "teles_cs.c 2.10 2002/07/30 22:23:34 kkeil";
  44. #else
  45. #define DEBUG(n, args...)
  46. #endif
  47. /*====================================================================*/
  48. /* Parameters that can be set with 'insmod' */
  49. static int protocol = 2; /* EURO-ISDN Default */
  50. module_param(protocol, int, 0);
  51. /*====================================================================*/
  52. /*
  53. The event() function is this driver's Card Services event handler.
  54. It will be called by Card Services when an appropriate card status
  55. event is received. The config() and release() entry points are
  56. used to configure or release a socket, in response to card insertion
  57. and ejection events. They are invoked from the teles_cs event
  58. handler.
  59. */
  60. static int teles_cs_config(struct pcmcia_device *link);
  61. static void teles_cs_release(struct pcmcia_device *link);
  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 void teles_detach(struct pcmcia_device *p_dev);
  68. /*
  69. A linked list of "instances" of the teles_cs device. Each actual
  70. PCMCIA card corresponds to one device instance, and is described
  71. by one struct pcmcia_device structure (defined in ds.h).
  72. You may not want to use a linked list for this -- for example, the
  73. memory card driver uses an array of struct pcmcia_device pointers, where minor
  74. device numbers are used to derive the corresponding array index.
  75. */
  76. /*
  77. A driver needs to provide a dev_node_t structure for each device
  78. on a card. In some cases, there is only one device per card (for
  79. example, ethernet cards, modems). In other cases, there may be
  80. many actual or logical devices (SCSI adapters, memory cards with
  81. multiple partitions). The dev_node_t structures need to be kept
  82. in a linked list starting at the 'dev' field of a struct pcmcia_device
  83. structure. We allocate them in the card's private data structure,
  84. because they generally shouldn't be allocated dynamically.
  85. In this case, we also provide a flag to indicate if a device is
  86. "stopped" due to a power management event, or card ejection. The
  87. device IO routines can use a flag like this to throttle IO to a
  88. card that is not ready to accept it.
  89. */
  90. typedef struct local_info_t {
  91. struct pcmcia_device *p_dev;
  92. dev_node_t node;
  93. int busy;
  94. int cardnr;
  95. } local_info_t;
  96. /*======================================================================
  97. teles_attach() creates an "instance" of the driver, allocatingx
  98. local data structures for one device. The device is registered
  99. with Card Services.
  100. The dev_link structure is initialized, but we don't actually
  101. configure the card at this point -- we wait until we receive a
  102. card insertion event.
  103. ======================================================================*/
  104. static int teles_probe(struct pcmcia_device *link)
  105. {
  106. local_info_t *local;
  107. DEBUG(0, "teles_attach()\n");
  108. /* Allocate space for private device-specific data */
  109. local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
  110. if (!local) return -ENOMEM;
  111. local->cardnr = -1;
  112. local->p_dev = link;
  113. link->priv = local;
  114. /* Interrupt setup */
  115. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  116. link->irq.IRQInfo1 = IRQ_LEVEL_ID|IRQ_SHARE_ID;
  117. link->irq.Handler = NULL;
  118. /*
  119. General socket configuration defaults can go here. In this
  120. client, we assume very little, and rely on the CIS for almost
  121. everything. In most clients, many details (i.e., number, sizes,
  122. and attributes of IO windows) are fixed by the nature of the
  123. device, and can be hard-wired here.
  124. */
  125. link->io.NumPorts1 = 96;
  126. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  127. link->io.IOAddrLines = 5;
  128. link->conf.Attributes = CONF_ENABLE_IRQ;
  129. link->conf.IntType = INT_MEMORY_AND_IO;
  130. return teles_cs_config(link);
  131. } /* teles_attach */
  132. /*======================================================================
  133. This deletes a driver "instance". The device is de-registered
  134. with Card Services. If it has been released, all local data
  135. structures are freed. Otherwise, the structures will be freed
  136. when the device is released.
  137. ======================================================================*/
  138. static void teles_detach(struct pcmcia_device *link)
  139. {
  140. local_info_t *info = link->priv;
  141. DEBUG(0, "teles_detach(0x%p)\n", link);
  142. info->busy = 1;
  143. teles_cs_release(link);
  144. kfree(info);
  145. } /* teles_detach */
  146. /*======================================================================
  147. teles_cs_config() is scheduled to run after a CARD_INSERTION event
  148. is received, to configure the PCMCIA socket, and to make the
  149. device available to the system.
  150. ======================================================================*/
  151. static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  152. cisparse_t *parse)
  153. {
  154. int i = pcmcia_get_tuple_data(handle, tuple);
  155. if (i != CS_SUCCESS) return i;
  156. return pcmcia_parse_tuple(handle, tuple, parse);
  157. }
  158. static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  159. cisparse_t *parse)
  160. {
  161. int i = pcmcia_get_first_tuple(handle, tuple);
  162. if (i != CS_SUCCESS) return i;
  163. return get_tuple(handle, tuple, parse);
  164. }
  165. static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple,
  166. cisparse_t *parse)
  167. {
  168. int i = pcmcia_get_next_tuple(handle, tuple);
  169. if (i != CS_SUCCESS) return i;
  170. return get_tuple(handle, tuple, parse);
  171. }
  172. static int teles_cs_config(struct pcmcia_device *link)
  173. {
  174. tuple_t tuple;
  175. cisparse_t parse;
  176. local_info_t *dev;
  177. int i, j, last_fn;
  178. u_short buf[128];
  179. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  180. IsdnCard_t icard;
  181. DEBUG(0, "teles_config(0x%p)\n", link);
  182. dev = link->priv;
  183. tuple.TupleData = (cisdata_t *)buf;
  184. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  185. tuple.Attributes = 0;
  186. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  187. i = first_tuple(link, &tuple, &parse);
  188. while (i == CS_SUCCESS) {
  189. if ( (cf->io.nwin > 0) && cf->io.win[0].base) {
  190. printk(KERN_INFO "(teles_cs: looks like the 96 model)\n");
  191. link->conf.ConfigIndex = cf->index;
  192. link->io.BasePort1 = cf->io.win[0].base;
  193. i = pcmcia_request_io(link, &link->io);
  194. if (i == CS_SUCCESS) break;
  195. } else {
  196. printk(KERN_INFO "(teles_cs: looks like the 97 model)\n");
  197. link->conf.ConfigIndex = cf->index;
  198. for (i = 0, j = 0x2f0; j > 0x100; j -= 0x10) {
  199. link->io.BasePort1 = j;
  200. i = pcmcia_request_io(link, &link->io);
  201. if (i == CS_SUCCESS) break;
  202. }
  203. break;
  204. }
  205. i = next_tuple(link, &tuple, &parse);
  206. }
  207. if (i != CS_SUCCESS) {
  208. last_fn = RequestIO;
  209. goto cs_failed;
  210. }
  211. i = pcmcia_request_irq(link, &link->irq);
  212. if (i != CS_SUCCESS) {
  213. link->irq.AssignedIRQ = 0;
  214. last_fn = RequestIRQ;
  215. goto cs_failed;
  216. }
  217. i = pcmcia_request_configuration(link, &link->conf);
  218. if (i != CS_SUCCESS) {
  219. last_fn = RequestConfiguration;
  220. goto cs_failed;
  221. }
  222. /* At this point, the dev_node_t structure(s) should be
  223. initialized and arranged in a linked list at link->dev. *//* */
  224. sprintf(dev->node.dev_name, "teles");
  225. dev->node.major = dev->node.minor = 0x0;
  226. link->dev_node = &dev->node;
  227. /* Finally, report what we've done */
  228. printk(KERN_INFO "%s: index 0x%02x:",
  229. dev->node.dev_name, link->conf.ConfigIndex);
  230. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  231. printk(", irq %d", link->irq.AssignedIRQ);
  232. if (link->io.NumPorts1)
  233. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  234. link->io.BasePort1+link->io.NumPorts1-1);
  235. if (link->io.NumPorts2)
  236. printk(" & 0x%04x-0x%04x", link->io.BasePort2,
  237. link->io.BasePort2+link->io.NumPorts2-1);
  238. printk("\n");
  239. icard.para[0] = link->irq.AssignedIRQ;
  240. icard.para[1] = link->io.BasePort1;
  241. icard.protocol = protocol;
  242. icard.typ = ISDN_CTYPE_TELESPCMCIA;
  243. i = hisax_init_pcmcia(link, &(((local_info_t*)link->priv)->busy), &icard);
  244. if (i < 0) {
  245. printk(KERN_ERR "teles_cs: failed to initialize Teles PCMCIA %d at i/o %#x\n",
  246. i, link->io.BasePort1);
  247. teles_cs_release(link);
  248. return -ENODEV;
  249. }
  250. ((local_info_t*)link->priv)->cardnr = i;
  251. return 0;
  252. cs_failed:
  253. cs_error(link, last_fn, i);
  254. teles_cs_release(link);
  255. return -ENODEV;
  256. } /* teles_cs_config */
  257. /*======================================================================
  258. After a card is removed, teles_cs_release() will unregister the net
  259. device, and release the PCMCIA configuration. If the device is
  260. still open, this will be postponed until it is closed.
  261. ======================================================================*/
  262. static void teles_cs_release(struct pcmcia_device *link)
  263. {
  264. local_info_t *local = link->priv;
  265. DEBUG(0, "teles_cs_release(0x%p)\n", link);
  266. if (local) {
  267. if (local->cardnr >= 0) {
  268. /* no unregister function with hisax */
  269. HiSax_closecard(local->cardnr);
  270. }
  271. }
  272. pcmcia_disable_device(link);
  273. } /* teles_cs_release */
  274. static int teles_suspend(struct pcmcia_device *link)
  275. {
  276. local_info_t *dev = link->priv;
  277. dev->busy = 1;
  278. return 0;
  279. }
  280. static int teles_resume(struct pcmcia_device *link)
  281. {
  282. local_info_t *dev = link->priv;
  283. dev->busy = 0;
  284. return 0;
  285. }
  286. static struct pcmcia_device_id teles_ids[] = {
  287. PCMCIA_DEVICE_PROD_ID12("TELES", "S0/PC", 0x67b50eae, 0xe9e70119),
  288. PCMCIA_DEVICE_NULL,
  289. };
  290. MODULE_DEVICE_TABLE(pcmcia, teles_ids);
  291. static struct pcmcia_driver teles_cs_driver = {
  292. .owner = THIS_MODULE,
  293. .drv = {
  294. .name = "teles_cs",
  295. },
  296. .probe = teles_probe,
  297. .remove = teles_detach,
  298. .id_table = teles_ids,
  299. .suspend = teles_suspend,
  300. .resume = teles_resume,
  301. };
  302. static int __init init_teles_cs(void)
  303. {
  304. return pcmcia_register_driver(&teles_cs_driver);
  305. }
  306. static void __exit exit_teles_cs(void)
  307. {
  308. pcmcia_unregister_driver(&teles_cs_driver);
  309. }
  310. module_init(init_teles_cs);
  311. module_exit(exit_teles_cs);