teles_cs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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(dev_link_t *);
  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. static dev_link_t *dev_list = NULL;
  87. /*
  88. A dev_link_t structure has fields for most things that are needed
  89. to keep track of a socket, but there will usually be some device
  90. specific information that also needs to be kept track of. The
  91. 'priv' pointer in a dev_link_t structure can be used to point to
  92. a device-specific private data structure, like this.
  93. To simplify the data structure handling, we actually include the
  94. dev_link_t structure in the device's private data structure.
  95. A driver needs to provide a dev_node_t structure for each device
  96. on a card. In some cases, there is only one device per card (for
  97. example, ethernet cards, modems). In other cases, there may be
  98. many actual or logical devices (SCSI adapters, memory cards with
  99. multiple partitions). The dev_node_t structures need to be kept
  100. in a linked list starting at the 'dev' field of a dev_link_t
  101. structure. We allocate them in the card's private data structure,
  102. because they generally shouldn't be allocated dynamically.
  103. In this case, we also provide a flag to indicate if a device is
  104. "stopped" due to a power management event, or card ejection. The
  105. device IO routines can use a flag like this to throttle IO to a
  106. card that is not ready to accept it.
  107. */
  108. typedef struct local_info_t {
  109. dev_link_t link;
  110. dev_node_t node;
  111. int busy;
  112. int cardnr;
  113. } local_info_t;
  114. /*======================================================================
  115. teles_attach() creates an "instance" of the driver, allocatingx
  116. local data structures for one device. The device is registered
  117. with Card Services.
  118. The dev_link structure is initialized, but we don't actually
  119. configure the card at this point -- we wait until we receive a
  120. card insertion event.
  121. ======================================================================*/
  122. static dev_link_t *teles_attach(void)
  123. {
  124. client_reg_t client_reg;
  125. dev_link_t *link;
  126. local_info_t *local;
  127. int ret;
  128. DEBUG(0, "teles_attach()\n");
  129. /* Allocate space for private device-specific data */
  130. local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  131. if (!local) return NULL;
  132. memset(local, 0, sizeof(local_info_t));
  133. local->cardnr = -1;
  134. link = &local->link; link->priv = local;
  135. /* Interrupt setup */
  136. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  137. link->irq.IRQInfo1 = IRQ_LEVEL_ID|IRQ_SHARE_ID;
  138. link->irq.Handler = NULL;
  139. /*
  140. General socket configuration defaults can go here. In this
  141. client, we assume very little, and rely on the CIS for almost
  142. everything. In most clients, many details (i.e., number, sizes,
  143. and attributes of IO windows) are fixed by the nature of the
  144. device, and can be hard-wired here.
  145. */
  146. link->io.NumPorts1 = 96;
  147. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  148. link->io.IOAddrLines = 5;
  149. link->conf.Attributes = CONF_ENABLE_IRQ;
  150. link->conf.Vcc = 50;
  151. link->conf.IntType = INT_MEMORY_AND_IO;
  152. /* Register with Card Services */
  153. link->next = dev_list;
  154. dev_list = link;
  155. client_reg.dev_info = &dev_info;
  156. client_reg.Version = 0x0210;
  157. client_reg.event_callback_args.client_data = link;
  158. ret = pcmcia_register_client(&link->handle, &client_reg);
  159. if (ret != CS_SUCCESS) {
  160. cs_error(link->handle, RegisterClient, ret);
  161. teles_detach(link);
  162. return NULL;
  163. }
  164. return link;
  165. } /* teles_attach */
  166. /*======================================================================
  167. This deletes a driver "instance". The device is de-registered
  168. with Card Services. If it has been released, all local data
  169. structures are freed. Otherwise, the structures will be freed
  170. when the device is released.
  171. ======================================================================*/
  172. static void teles_detach(dev_link_t *link)
  173. {
  174. dev_link_t **linkp;
  175. local_info_t *info = link->priv;
  176. int ret;
  177. DEBUG(0, "teles_detach(0x%p)\n", link);
  178. /* Locate device structure */
  179. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  180. if (*linkp == link) break;
  181. if (*linkp == NULL)
  182. return;
  183. if (link->state & DEV_CONFIG)
  184. teles_cs_release(link);
  185. /* Break the link with Card Services */
  186. if (link->handle) {
  187. ret = pcmcia_deregister_client(link->handle);
  188. if (ret != CS_SUCCESS)
  189. cs_error(link->handle, DeregisterClient, ret);
  190. }
  191. /* Unlink device structure and free it */
  192. *linkp = link->next;
  193. kfree(info);
  194. } /* teles_detach */
  195. /*======================================================================
  196. teles_cs_config() is scheduled to run after a CARD_INSERTION event
  197. is received, to configure the PCMCIA socket, and to make the
  198. device available to the system.
  199. ======================================================================*/
  200. static int get_tuple(client_handle_t handle, tuple_t *tuple,
  201. cisparse_t *parse)
  202. {
  203. int i = pcmcia_get_tuple_data(handle, tuple);
  204. if (i != CS_SUCCESS) return i;
  205. return pcmcia_parse_tuple(handle, tuple, parse);
  206. }
  207. static int first_tuple(client_handle_t handle, tuple_t *tuple,
  208. cisparse_t *parse)
  209. {
  210. int i = pcmcia_get_first_tuple(handle, tuple);
  211. if (i != CS_SUCCESS) return i;
  212. return get_tuple(handle, tuple, parse);
  213. }
  214. static int next_tuple(client_handle_t handle, tuple_t *tuple,
  215. cisparse_t *parse)
  216. {
  217. int i = pcmcia_get_next_tuple(handle, tuple);
  218. if (i != CS_SUCCESS) return i;
  219. return get_tuple(handle, tuple, parse);
  220. }
  221. static void teles_cs_config(dev_link_t *link)
  222. {
  223. client_handle_t handle;
  224. tuple_t tuple;
  225. cisparse_t parse;
  226. local_info_t *dev;
  227. int i, j, last_fn;
  228. u_short buf[128];
  229. cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  230. IsdnCard_t icard;
  231. DEBUG(0, "teles_config(0x%p)\n", link);
  232. handle = link->handle;
  233. dev = link->priv;
  234. /*
  235. This reads the card's CONFIG tuple to find its configuration
  236. registers.
  237. */
  238. tuple.DesiredTuple = CISTPL_CONFIG;
  239. tuple.TupleData = (cisdata_t *)buf;
  240. tuple.TupleDataMax = 255;
  241. tuple.TupleOffset = 0;
  242. tuple.Attributes = 0;
  243. i = first_tuple(handle, &tuple, &parse);
  244. if (i != CS_SUCCESS) {
  245. last_fn = ParseTuple;
  246. goto cs_failed;
  247. }
  248. link->conf.ConfigBase = parse.config.base;
  249. link->conf.Present = parse.config.rmask[0];
  250. /* Configure card */
  251. link->state |= DEV_CONFIG;
  252. tuple.TupleData = (cisdata_t *)buf;
  253. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  254. tuple.Attributes = 0;
  255. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  256. i = first_tuple(handle, &tuple, &parse);
  257. while (i == CS_SUCCESS) {
  258. if ( (cf->io.nwin > 0) && cf->io.win[0].base) {
  259. printk(KERN_INFO "(teles_cs: looks like the 96 model)\n");
  260. link->conf.ConfigIndex = cf->index;
  261. link->io.BasePort1 = cf->io.win[0].base;
  262. i = pcmcia_request_io(link->handle, &link->io);
  263. if (i == CS_SUCCESS) break;
  264. } else {
  265. printk(KERN_INFO "(teles_cs: looks like the 97 model)\n");
  266. link->conf.ConfigIndex = cf->index;
  267. for (i = 0, j = 0x2f0; j > 0x100; j -= 0x10) {
  268. link->io.BasePort1 = j;
  269. i = pcmcia_request_io(link->handle, &link->io);
  270. if (i == CS_SUCCESS) break;
  271. }
  272. break;
  273. }
  274. i = next_tuple(handle, &tuple, &parse);
  275. }
  276. if (i != CS_SUCCESS) {
  277. last_fn = RequestIO;
  278. goto cs_failed;
  279. }
  280. i = pcmcia_request_irq(link->handle, &link->irq);
  281. if (i != CS_SUCCESS) {
  282. link->irq.AssignedIRQ = 0;
  283. last_fn = RequestIRQ;
  284. goto cs_failed;
  285. }
  286. i = pcmcia_request_configuration(link->handle, &link->conf);
  287. if (i != CS_SUCCESS) {
  288. last_fn = RequestConfiguration;
  289. goto cs_failed;
  290. }
  291. /* At this point, the dev_node_t structure(s) should be
  292. initialized and arranged in a linked list at link->dev. *//* */
  293. sprintf(dev->node.dev_name, "teles");
  294. dev->node.major = dev->node.minor = 0x0;
  295. link->dev = &dev->node;
  296. /* Finally, report what we've done */
  297. printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
  298. dev->node.dev_name, link->conf.ConfigIndex,
  299. link->conf.Vcc/10, link->conf.Vcc%10);
  300. if (link->conf.Vpp1)
  301. printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
  302. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  303. printk(", irq %d", link->irq.AssignedIRQ);
  304. if (link->io.NumPorts1)
  305. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  306. link->io.BasePort1+link->io.NumPorts1-1);
  307. if (link->io.NumPorts2)
  308. printk(" & 0x%04x-0x%04x", link->io.BasePort2,
  309. link->io.BasePort2+link->io.NumPorts2-1);
  310. printk("\n");
  311. link->state &= ~DEV_CONFIG_PENDING;
  312. icard.para[0] = link->irq.AssignedIRQ;
  313. icard.para[1] = link->io.BasePort1;
  314. icard.protocol = protocol;
  315. icard.typ = ISDN_CTYPE_TELESPCMCIA;
  316. i = hisax_init_pcmcia(link, &(((local_info_t*)link->priv)->busy), &icard);
  317. if (i < 0) {
  318. printk(KERN_ERR "teles_cs: failed to initialize Teles PCMCIA %d at i/o %#x\n",
  319. i, link->io.BasePort1);
  320. teles_cs_release(link);
  321. } else
  322. ((local_info_t*)link->priv)->cardnr = i;
  323. return;
  324. cs_failed:
  325. cs_error(link->handle, last_fn, i);
  326. teles_cs_release(link);
  327. } /* teles_cs_config */
  328. /*======================================================================
  329. After a card is removed, teles_cs_release() will unregister the net
  330. device, and release the PCMCIA configuration. If the device is
  331. still open, this will be postponed until it is closed.
  332. ======================================================================*/
  333. static void teles_cs_release(dev_link_t *link)
  334. {
  335. local_info_t *local = link->priv;
  336. DEBUG(0, "teles_cs_release(0x%p)\n", link);
  337. if (local) {
  338. if (local->cardnr >= 0) {
  339. /* no unregister function with hisax */
  340. HiSax_closecard(local->cardnr);
  341. }
  342. }
  343. /* Unlink the device chain */
  344. link->dev = NULL;
  345. /* Don't bother checking to see if these succeed or not */
  346. if (link->win)
  347. pcmcia_release_window(link->win);
  348. pcmcia_release_configuration(link->handle);
  349. pcmcia_release_io(link->handle, &link->io);
  350. pcmcia_release_irq(link->handle, &link->irq);
  351. link->state &= ~DEV_CONFIG;
  352. } /* teles_cs_release */
  353. /*======================================================================
  354. The card status event handler. Mostly, this schedules other
  355. stuff to run after an event is received. A CARD_REMOVAL event
  356. also sets some flags to discourage the net drivers from trying
  357. to talk to the card any more.
  358. When a CARD_REMOVAL event is received, we immediately set a flag
  359. to block future accesses to this device. All the functions that
  360. actually access the device should check this flag to make sure
  361. the card is still present.
  362. ======================================================================*/
  363. static int teles_cs_event(event_t event, int priority,
  364. event_callback_args_t *args)
  365. {
  366. dev_link_t *link = args->client_data;
  367. local_info_t *dev = link->priv;
  368. DEBUG(1, "teles_cs_event(%d)\n", event);
  369. switch (event) {
  370. case CS_EVENT_CARD_REMOVAL:
  371. link->state &= ~DEV_PRESENT;
  372. if (link->state & DEV_CONFIG) {
  373. ((local_info_t*)link->priv)->busy = 1;
  374. teles_cs_release(link);
  375. }
  376. break;
  377. case CS_EVENT_CARD_INSERTION:
  378. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  379. teles_cs_config(link);
  380. break;
  381. case CS_EVENT_PM_SUSPEND:
  382. link->state |= DEV_SUSPEND;
  383. /* Fall through... */
  384. case CS_EVENT_RESET_PHYSICAL:
  385. /* Mark the device as stopped, to block IO until later */
  386. dev->busy = 1;
  387. if (link->state & DEV_CONFIG)
  388. pcmcia_release_configuration(link->handle);
  389. break;
  390. case CS_EVENT_PM_RESUME:
  391. link->state &= ~DEV_SUSPEND;
  392. /* Fall through... */
  393. case CS_EVENT_CARD_RESET:
  394. if (link->state & DEV_CONFIG)
  395. pcmcia_request_configuration(link->handle, &link->conf);
  396. dev->busy = 0;
  397. break;
  398. }
  399. return 0;
  400. } /* teles_cs_event */
  401. static struct pcmcia_device_id teles_ids[] = {
  402. PCMCIA_DEVICE_PROD_ID12("TELES", "S0/PC", 0x67b50eae, 0xe9e70119),
  403. PCMCIA_DEVICE_NULL,
  404. };
  405. MODULE_DEVICE_TABLE(pcmcia, teles_ids);
  406. static struct pcmcia_driver teles_cs_driver = {
  407. .owner = THIS_MODULE,
  408. .drv = {
  409. .name = "teles_cs",
  410. },
  411. .attach = teles_attach,
  412. .event = teles_cs_event,
  413. .detach = teles_detach,
  414. .id_table = teles_ids,
  415. };
  416. static int __init init_teles_cs(void)
  417. {
  418. return pcmcia_register_driver(&teles_cs_driver);
  419. }
  420. static void __exit exit_teles_cs(void)
  421. {
  422. pcmcia_unregister_driver(&teles_cs_driver);
  423. BUG_ON(dev_list != NULL);
  424. }
  425. module_init(init_teles_cs);
  426. module_exit(exit_teles_cs);