teles_cs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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/sched.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/timer.h>
  20. #include <linux/ioport.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/cisreg.h>
  27. #include <pcmcia/ds.h>
  28. #include "hisax_cfg.h"
  29. MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for Teles PCMCIA cards");
  30. MODULE_AUTHOR("Christof Petig, christof.petig@wtal.de, Karsten Keil, kkeil@suse.de");
  31. MODULE_LICENSE("GPL");
  32. /*
  33. All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
  34. you do not define PCMCIA_DEBUG at all, all the debug code will be
  35. left out. If you compile with PCMCIA_DEBUG=0, the debug code will
  36. be present but disabled -- but it can then be enabled for specific
  37. modules at load time with a 'pc_debug=#' option to insmod.
  38. */
  39. #ifdef PCMCIA_DEBUG
  40. static int pc_debug = PCMCIA_DEBUG;
  41. module_param(pc_debug, int, 0);
  42. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
  43. static char *version =
  44. "teles_cs.c 2.10 2002/07/30 22:23:34 kkeil";
  45. #else
  46. #define DEBUG(n, args...)
  47. #endif
  48. /*====================================================================*/
  49. /* Parameters that can be set with 'insmod' */
  50. static int protocol = 2; /* EURO-ISDN Default */
  51. module_param(protocol, int, 0);
  52. /*====================================================================*/
  53. /*
  54. The event() function is this driver's Card Services event handler.
  55. It will be called by Card Services when an appropriate card status
  56. event is received. The config() and release() entry points are
  57. used to configure or release a socket, in response to card insertion
  58. and ejection events. They are invoked from the teles_cs event
  59. handler.
  60. */
  61. static void teles_cs_config(dev_link_t *link);
  62. static void teles_cs_release(dev_link_t *link);
  63. static int teles_cs_event(event_t event, int priority,
  64. event_callback_args_t *args);
  65. /*
  66. The attach() and detach() entry points are used to create and destroy
  67. "instances" of the driver, where each instance represents everything
  68. needed to manage one actual PCMCIA card.
  69. */
  70. static dev_link_t *teles_attach(void);
  71. static void teles_detach(struct pcmcia_device *p_dev);
  72. /*
  73. The dev_info variable is the "key" that is used to match up this
  74. device driver with appropriate cards, through the card configuration
  75. database.
  76. */
  77. static dev_info_t dev_info = "teles_cs";
  78. /*
  79. A linked list of "instances" of the teles_cs device. Each actual
  80. PCMCIA card corresponds to one device instance, and is described
  81. by one dev_link_t structure (defined in ds.h).
  82. You may not want to use a linked list for this -- for example, the
  83. memory card driver uses an array of dev_link_t pointers, where minor
  84. device numbers are used to derive the corresponding array index.
  85. */
  86. /*
  87. A driver needs to provide a dev_node_t structure for each device
  88. on a card. In some cases, there is only one device per card (for
  89. example, ethernet cards, modems). In other cases, there may be
  90. many actual or logical devices (SCSI adapters, memory cards with
  91. multiple partitions). The dev_node_t structures need to be kept
  92. in a linked list starting at the 'dev' field of a dev_link_t
  93. structure. We allocate them in the card's private data structure,
  94. because they generally shouldn't be allocated dynamically.
  95. In this case, we also provide a flag to indicate if a device is
  96. "stopped" due to a power management event, or card ejection. The
  97. device IO routines can use a flag like this to throttle IO to a
  98. card that is not ready to accept it.
  99. */
  100. typedef struct local_info_t {
  101. dev_link_t link;
  102. dev_node_t node;
  103. int busy;
  104. int cardnr;
  105. } local_info_t;
  106. /*======================================================================
  107. teles_attach() creates an "instance" of the driver, allocatingx
  108. local data structures for one device. The device is registered
  109. with Card Services.
  110. The dev_link structure is initialized, but we don't actually
  111. configure the card at this point -- we wait until we receive a
  112. card insertion event.
  113. ======================================================================*/
  114. static dev_link_t *teles_attach(void)
  115. {
  116. client_reg_t client_reg;
  117. dev_link_t *link;
  118. local_info_t *local;
  119. int ret;
  120. DEBUG(0, "teles_attach()\n");
  121. /* Allocate space for private device-specific data */
  122. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  123. if (!local) return NULL;
  124. memset(local, 0, sizeof(local_info_t));
  125. local->cardnr = -1;
  126. link = &local->link; link->priv = local;
  127. /* Interrupt setup */
  128. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  129. link->irq.IRQInfo1 = IRQ_LEVEL_ID|IRQ_SHARE_ID;
  130. link->irq.Handler = NULL;
  131. /*
  132. General socket configuration defaults can go here. In this
  133. client, we assume very little, and rely on the CIS for almost
  134. everything. In most clients, many details (i.e., number, sizes,
  135. and attributes of IO windows) are fixed by the nature of the
  136. device, and can be hard-wired here.
  137. */
  138. link->io.NumPorts1 = 96;
  139. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  140. link->io.IOAddrLines = 5;
  141. link->conf.Attributes = CONF_ENABLE_IRQ;
  142. link->conf.Vcc = 50;
  143. link->conf.IntType = INT_MEMORY_AND_IO;
  144. /* Register with Card Services */
  145. link->next = NULL;
  146. client_reg.dev_info = &dev_info;
  147. client_reg.Version = 0x0210;
  148. client_reg.event_callback_args.client_data = link;
  149. ret = pcmcia_register_client(&link->handle, &client_reg);
  150. if (ret != CS_SUCCESS) {
  151. cs_error(link->handle, RegisterClient, ret);
  152. teles_detach(link->handle);
  153. return NULL;
  154. }
  155. return link;
  156. } /* teles_attach */
  157. /*======================================================================
  158. This deletes a driver "instance". The device is de-registered
  159. with Card Services. If it has been released, all local data
  160. structures are freed. Otherwise, the structures will be freed
  161. when the device is released.
  162. ======================================================================*/
  163. static void teles_detach(struct pcmcia_device *p_dev)
  164. {
  165. dev_link_t *link = dev_to_instance(p_dev);
  166. local_info_t *info = link->priv;
  167. DEBUG(0, "teles_detach(0x%p)\n", link);
  168. if (link->state & DEV_CONFIG) {
  169. info->busy = 1;
  170. teles_cs_release(link);
  171. }
  172. kfree(info);
  173. } /* teles_detach */
  174. /*======================================================================
  175. teles_cs_config() is scheduled to run after a CARD_INSERTION event
  176. is received, to configure the PCMCIA socket, and to make the
  177. device available to the system.
  178. ======================================================================*/
  179. static int get_tuple(client_handle_t handle, tuple_t *tuple,
  180. cisparse_t *parse)
  181. {
  182. int i = pcmcia_get_tuple_data(handle, tuple);
  183. if (i != CS_SUCCESS) return i;
  184. return pcmcia_parse_tuple(handle, tuple, parse);
  185. }
  186. static int first_tuple(client_handle_t handle, tuple_t *tuple,
  187. cisparse_t *parse)
  188. {
  189. int i = pcmcia_get_first_tuple(handle, tuple);
  190. if (i != CS_SUCCESS) return i;
  191. return get_tuple(handle, tuple, parse);
  192. }
  193. static int next_tuple(client_handle_t handle, tuple_t *tuple,
  194. cisparse_t *parse)
  195. {
  196. int i = pcmcia_get_next_tuple(handle, tuple);
  197. if (i != CS_SUCCESS) return i;
  198. return get_tuple(handle, tuple, parse);
  199. }
  200. static void teles_cs_config(dev_link_t *link)
  201. {
  202. client_handle_t handle;
  203. tuple_t tuple;
  204. cisparse_t parse;
  205. local_info_t *dev;
  206. int i, j, last_fn;
  207. u_short buf[128];
  208. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  209. IsdnCard_t icard;
  210. DEBUG(0, "teles_config(0x%p)\n", link);
  211. handle = link->handle;
  212. dev = link->priv;
  213. /*
  214. This reads the card's CONFIG tuple to find its configuration
  215. registers.
  216. */
  217. tuple.DesiredTuple = CISTPL_CONFIG;
  218. tuple.TupleData = (cisdata_t *)buf;
  219. tuple.TupleDataMax = 255;
  220. tuple.TupleOffset = 0;
  221. tuple.Attributes = 0;
  222. i = first_tuple(handle, &tuple, &parse);
  223. if (i != CS_SUCCESS) {
  224. last_fn = ParseTuple;
  225. goto cs_failed;
  226. }
  227. link->conf.ConfigBase = parse.config.base;
  228. link->conf.Present = parse.config.rmask[0];
  229. /* Configure card */
  230. link->state |= DEV_CONFIG;
  231. tuple.TupleData = (cisdata_t *)buf;
  232. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  233. tuple.Attributes = 0;
  234. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  235. i = first_tuple(handle, &tuple, &parse);
  236. while (i == CS_SUCCESS) {
  237. if ( (cf->io.nwin > 0) && cf->io.win[0].base) {
  238. printk(KERN_INFO "(teles_cs: looks like the 96 model)\n");
  239. link->conf.ConfigIndex = cf->index;
  240. link->io.BasePort1 = cf->io.win[0].base;
  241. i = pcmcia_request_io(link->handle, &link->io);
  242. if (i == CS_SUCCESS) break;
  243. } else {
  244. printk(KERN_INFO "(teles_cs: looks like the 97 model)\n");
  245. link->conf.ConfigIndex = cf->index;
  246. for (i = 0, j = 0x2f0; j > 0x100; j -= 0x10) {
  247. link->io.BasePort1 = j;
  248. i = pcmcia_request_io(link->handle, &link->io);
  249. if (i == CS_SUCCESS) break;
  250. }
  251. break;
  252. }
  253. i = next_tuple(handle, &tuple, &parse);
  254. }
  255. if (i != CS_SUCCESS) {
  256. last_fn = RequestIO;
  257. goto cs_failed;
  258. }
  259. i = pcmcia_request_irq(link->handle, &link->irq);
  260. if (i != CS_SUCCESS) {
  261. link->irq.AssignedIRQ = 0;
  262. last_fn = RequestIRQ;
  263. goto cs_failed;
  264. }
  265. i = pcmcia_request_configuration(link->handle, &link->conf);
  266. if (i != CS_SUCCESS) {
  267. last_fn = RequestConfiguration;
  268. goto cs_failed;
  269. }
  270. /* At this point, the dev_node_t structure(s) should be
  271. initialized and arranged in a linked list at link->dev. *//* */
  272. sprintf(dev->node.dev_name, "teles");
  273. dev->node.major = dev->node.minor = 0x0;
  274. link->dev = &dev->node;
  275. /* Finally, report what we've done */
  276. printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
  277. dev->node.dev_name, link->conf.ConfigIndex,
  278. link->conf.Vcc/10, link->conf.Vcc%10);
  279. if (link->conf.Vpp1)
  280. printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
  281. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  282. printk(", irq %d", link->irq.AssignedIRQ);
  283. if (link->io.NumPorts1)
  284. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  285. link->io.BasePort1+link->io.NumPorts1-1);
  286. if (link->io.NumPorts2)
  287. printk(" & 0x%04x-0x%04x", link->io.BasePort2,
  288. link->io.BasePort2+link->io.NumPorts2-1);
  289. printk("\n");
  290. link->state &= ~DEV_CONFIG_PENDING;
  291. icard.para[0] = link->irq.AssignedIRQ;
  292. icard.para[1] = link->io.BasePort1;
  293. icard.protocol = protocol;
  294. icard.typ = ISDN_CTYPE_TELESPCMCIA;
  295. i = hisax_init_pcmcia(link, &(((local_info_t*)link->priv)->busy), &icard);
  296. if (i < 0) {
  297. printk(KERN_ERR "teles_cs: failed to initialize Teles PCMCIA %d at i/o %#x\n",
  298. i, link->io.BasePort1);
  299. teles_cs_release(link);
  300. } else
  301. ((local_info_t*)link->priv)->cardnr = i;
  302. return;
  303. cs_failed:
  304. cs_error(link->handle, last_fn, i);
  305. teles_cs_release(link);
  306. } /* teles_cs_config */
  307. /*======================================================================
  308. After a card is removed, teles_cs_release() will unregister the net
  309. device, and release the PCMCIA configuration. If the device is
  310. still open, this will be postponed until it is closed.
  311. ======================================================================*/
  312. static void teles_cs_release(dev_link_t *link)
  313. {
  314. local_info_t *local = link->priv;
  315. DEBUG(0, "teles_cs_release(0x%p)\n", link);
  316. if (local) {
  317. if (local->cardnr >= 0) {
  318. /* no unregister function with hisax */
  319. HiSax_closecard(local->cardnr);
  320. }
  321. }
  322. /* Unlink the device chain */
  323. link->dev = NULL;
  324. /* Don't bother checking to see if these succeed or not */
  325. if (link->win)
  326. pcmcia_release_window(link->win);
  327. pcmcia_release_configuration(link->handle);
  328. pcmcia_release_io(link->handle, &link->io);
  329. pcmcia_release_irq(link->handle, &link->irq);
  330. link->state &= ~DEV_CONFIG;
  331. } /* teles_cs_release */
  332. static int teles_suspend(struct pcmcia_device *p_dev)
  333. {
  334. dev_link_t *link = dev_to_instance(p_dev);
  335. local_info_t *dev = link->priv;
  336. link->state |= DEV_SUSPEND;
  337. dev->busy = 1;
  338. if (link->state & DEV_CONFIG)
  339. pcmcia_release_configuration(link->handle);
  340. return 0;
  341. }
  342. static int teles_resume(struct pcmcia_device *p_dev)
  343. {
  344. dev_link_t *link = dev_to_instance(p_dev);
  345. local_info_t *dev = link->priv;
  346. link->state &= ~DEV_SUSPEND;
  347. if (link->state & DEV_CONFIG)
  348. pcmcia_request_configuration(link->handle, &link->conf);
  349. dev->busy = 0;
  350. return 0;
  351. }
  352. /*======================================================================
  353. The card status event handler. Mostly, this schedules other
  354. stuff to run after an event is received. A CARD_REMOVAL event
  355. also sets some flags to discourage the net drivers from trying
  356. to talk to the card any more.
  357. When a CARD_REMOVAL event is received, we immediately set a flag
  358. to block future accesses to this device. All the functions that
  359. actually access the device should check this flag to make sure
  360. the card is still present.
  361. ======================================================================*/
  362. static int teles_cs_event(event_t event, int priority,
  363. event_callback_args_t *args)
  364. {
  365. dev_link_t *link = args->client_data;
  366. DEBUG(1, "teles_cs_event(%d)\n", event);
  367. switch (event) {
  368. case CS_EVENT_CARD_INSERTION:
  369. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  370. teles_cs_config(link);
  371. break;
  372. }
  373. return 0;
  374. } /* teles_cs_event */
  375. static struct pcmcia_device_id teles_ids[] = {
  376. PCMCIA_DEVICE_PROD_ID12("TELES", "S0/PC", 0x67b50eae, 0xe9e70119),
  377. PCMCIA_DEVICE_NULL,
  378. };
  379. MODULE_DEVICE_TABLE(pcmcia, teles_ids);
  380. static struct pcmcia_driver teles_cs_driver = {
  381. .owner = THIS_MODULE,
  382. .drv = {
  383. .name = "teles_cs",
  384. },
  385. .attach = teles_attach,
  386. .event = teles_cs_event,
  387. .remove = teles_detach,
  388. .id_table = teles_ids,
  389. .suspend = teles_suspend,
  390. .resume = teles_resume,
  391. };
  392. static int __init init_teles_cs(void)
  393. {
  394. return pcmcia_register_driver(&teles_cs_driver);
  395. }
  396. static void __exit exit_teles_cs(void)
  397. {
  398. pcmcia_unregister_driver(&teles_cs_driver);
  399. }
  400. module_init(init_teles_cs);
  401. module_exit(exit_teles_cs);