elsa_cs.c 16 KB

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