avma1_cs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:16k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * PCMCIA client driver for AVM A1 / Fritz!PCMCIA
  3.  *
  4.  * Author       Carsten Paeth
  5.  * Copyright    1998-2001 by Carsten Paeth <calle@calle.in-berlin.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/sched.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/timer.h>
  18. #include <asm/io.h>
  19. #include <asm/system.h>
  20. #include <pcmcia/version.h>
  21. #include <pcmcia/cs_types.h>
  22. #include <pcmcia/cs.h>
  23. #include <pcmcia/cistpl.h>
  24. #include <pcmcia/ds.h>
  25. MODULE_DESCRIPTION("ISDN4Linux: PCMCIA client driver for AVM A1/Fritz!PCMCIA cards");
  26. MODULE_AUTHOR("Carsten Paeth");
  27. MODULE_LICENSE("GPL");
  28. int avm_a1_init_pcmcia(void *pcm_iob, int pcm_irq, int *busy_flag, int prot);
  29. void HiSax_closecard(int cardnr);
  30. /*
  31.    All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
  32.    you do not define PCMCIA_DEBUG at all, all the debug code will be
  33.    left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
  34.    be present but disabled -- but it can then be enabled for specific
  35.    modules at load time with a 'pc_debug=#' option to insmod.
  36. */
  37. #ifdef PCMCIA_DEBUG
  38. static int pc_debug = PCMCIA_DEBUG;
  39. MODULE_PARM(pc_debug, "i");
  40. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
  41. static char *version =
  42. "avma1_cs.c 1.00 1998/01/23 10:00:00 (Carsten Paeth)";
  43. #else
  44. #define DEBUG(n, args...)
  45. #endif
  46. /*====================================================================*/
  47. /* Parameters that can be set with 'insmod' */
  48. static int default_irq_list[11] = { 15, 13, 12, 11, 10, 9, 7, 5, 4, 3, -1 };
  49. static int irq_list[11] = { -1 };
  50. static int isdnprot = 2;
  51. MODULE_PARM(irq_list, "1-11i");
  52. MODULE_PARM(isdnprot, "1-4i");
  53. /*====================================================================*/
  54. /*
  55.    The event() function is this driver's Card Services event handler.
  56.    It will be called by Card Services when an appropriate card status
  57.    event is received.  The config() and release() entry points are
  58.    used to configure or release a socket, in response to card insertion
  59.    and ejection events.  They are invoked from the skeleton event
  60.    handler.
  61. */
  62. static void avma1cs_config(dev_link_t *link);
  63. static void avma1cs_release(u_long arg);
  64. static int avma1cs_event(event_t event, int priority,
  65.   event_callback_args_t *args);
  66. /*
  67.    The attach() and detach() entry points are used to create and destroy
  68.    "instances" of the driver, where each instance represents everything
  69.    needed to manage one actual PCMCIA card.
  70. */
  71. static dev_link_t *avma1cs_attach(void);
  72. static void avma1cs_detach(dev_link_t *);
  73. /*
  74.    The dev_info variable is the "key" that is used to match up this
  75.    device driver with appropriate cards, through the card configuration
  76.    database.
  77. */
  78. static dev_info_t dev_info = "avma1_cs";
  79. /*
  80.    A linked list of "instances" of the skeleton device.  Each actual
  81.    PCMCIA card corresponds to one device instance, and is described
  82.    by one dev_link_t structure (defined in ds.h).
  83.    You may not want to use a linked list for this -- for example, the
  84.    memory card driver uses an array of dev_link_t pointers, where minor
  85.    device numbers are used to derive the corresponding array index.
  86. */
  87. static dev_link_t *dev_list = NULL;
  88. /*
  89.    A dev_link_t structure has fields for most things that are needed
  90.    to keep track of a socket, but there will usually be some device
  91.    specific information that also needs to be kept track of.  The
  92.    'priv' pointer in a dev_link_t structure can be used to point to
  93.    a device-specific private data structure, like this.
  94.    A driver needs to provide a dev_node_t structure for each device
  95.    on a card.  In some cases, there is only one device per card (for
  96.    example, ethernet cards, modems).  In other cases, there may be
  97.    many actual or logical devices (SCSI adapters, memory cards with
  98.    multiple partitions).  The dev_node_t structures need to be kept
  99.    in a linked list starting at the 'dev' field of a dev_link_t
  100.    structure.  We allocate them in the card's private data structure,
  101.    because they generally can't be allocated dynamically.
  102. */
  103.    
  104. typedef struct local_info_t {
  105.     dev_node_t node;
  106. } local_info_t;
  107. /*====================================================================*/
  108. static void cs_error(client_handle_t handle, int func, int ret)
  109. {
  110.     error_info_t err = { func, ret };
  111.     CardServices(ReportError, handle, &err);
  112. }
  113. /*======================================================================
  114.     avma1cs_attach() creates an "instance" of the driver, allocating
  115.     local data structures for one device.  The device is registered
  116.     with Card Services.
  117.     The dev_link structure is initialized, but we don't actually
  118.     configure the card at this point -- we wait until we receive a
  119.     card insertion event.
  120.     
  121. ======================================================================*/
  122. static dev_link_t *avma1cs_attach(void)
  123. {
  124.     client_reg_t client_reg;
  125.     dev_link_t *link;
  126.     local_info_t *local;
  127.     int ret, i;
  128.     
  129.     DEBUG(0, "avma1cs_attach()n");
  130.     /* Initialize the dev_link_t structure */
  131.     link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
  132.     memset(link, 0, sizeof(struct dev_link_t));
  133.     link->release.function = &avma1cs_release;
  134.     link->release.data = (u_long)link;
  135.     /* The io structure describes IO port mapping */
  136.     link->io.NumPorts1 = 16;
  137.     link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  138.     link->io.NumPorts2 = 16;
  139.     link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
  140.     link->io.IOAddrLines = 5;
  141.     /* Interrupt setup */
  142.     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  143.     link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  144.     link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
  145.     if (irq_list[0] != -1) {
  146.     for (i = 0; i < 10 && irq_list[i] > 0; i++)
  147.        link->irq.IRQInfo2 |= 1 << irq_list[i];
  148.     } else {
  149.     for (i = 0; i < 10 && default_irq_list[i] > 0; i++)
  150.        link->irq.IRQInfo2 |= 1 << default_irq_list[i];
  151.     }
  152.     
  153.     /* General socket configuration */
  154.     link->conf.Attributes = CONF_ENABLE_IRQ;
  155.     link->conf.Vcc = 50;
  156.     link->conf.IntType = INT_MEMORY_AND_IO;
  157.     link->conf.ConfigIndex = 1;
  158.     link->conf.Present = PRESENT_OPTION;
  159.     /* Allocate space for private device-specific data */
  160.     local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
  161.     memset(local, 0, sizeof(local_info_t));
  162.     link->priv = local;
  163.     
  164.     /* Register with Card Services */
  165.     link->next = dev_list;
  166.     dev_list = link;
  167.     client_reg.dev_info = &dev_info;
  168.     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
  169.     client_reg.EventMask =
  170. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  171. CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
  172. CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  173.     client_reg.event_handler = &avma1cs_event;
  174.     client_reg.Version = 0x0210;
  175.     client_reg.event_callback_args.client_data = link;
  176.     ret = CardServices(RegisterClient, &link->handle, &client_reg);
  177.     if (ret != 0) {
  178. cs_error(link->handle, RegisterClient, ret);
  179. avma1cs_detach(link);
  180. return NULL;
  181.     }
  182.     return link;
  183. } /* avma1cs_attach */
  184. /*======================================================================
  185.     This deletes a driver "instance".  The device is de-registered
  186.     with Card Services.  If it has been released, all local data
  187.     structures are freed.  Otherwise, the structures will be freed
  188.     when the device is released.
  189. ======================================================================*/
  190. static void avma1cs_detach(dev_link_t *link)
  191. {
  192.     dev_link_t **linkp;
  193.     DEBUG(0, "avma1cs_detach(0x%p)n", link);
  194.     
  195.     /* Locate device structure */
  196.     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  197. if (*linkp == link) break;
  198.     if (*linkp == NULL)
  199. return;
  200.     /*
  201.        If the device is currently configured and active, we won't
  202.        actually delete it yet.  Instead, it is marked so that when
  203.        the release() function is called, that will trigger a proper
  204.        detach().
  205.     */
  206.     if (link->state & DEV_CONFIG) {
  207. #ifdef PCMCIA_DEBUG
  208. printk(KERN_DEBUG "avma1_cs: detach postponed, '%s' "
  209.        "still lockedn", link->dev->dev_name);
  210. #endif
  211. link->state |= DEV_STALE_LINK;
  212. return;
  213.     }
  214.     /* Break the link with Card Services */
  215.     if (link->handle)
  216. CardServices(DeregisterClient, link->handle);
  217.     
  218.     /* Unlink device structure, free pieces */
  219.     *linkp = link->next;
  220.     if (link->priv) {
  221. kfree(link->priv);
  222.     }
  223.     kfree(link);
  224.     
  225. } /* avma1cs_detach */
  226. /*======================================================================
  227.     avma1cs_config() is scheduled to run after a CARD_INSERTION event
  228.     is received, to configure the PCMCIA socket, and to make the
  229.     ethernet device available to the system.
  230.     
  231. ======================================================================*/
  232. static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple,
  233.      cisparse_t *parse)
  234. {
  235.     int i;
  236.     i = CardServices(fn, handle, tuple);
  237.     if (i != CS_SUCCESS) return i;
  238.     i = CardServices(GetTupleData, handle, tuple);
  239.     if (i != CS_SUCCESS) return i;
  240.     return CardServices(ParseTuple, handle, tuple, parse);
  241. }
  242. #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
  243. #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
  244. static void avma1cs_config(dev_link_t *link)
  245. {
  246.     client_handle_t handle;
  247.     tuple_t tuple;
  248.     cisparse_t parse;
  249.     cistpl_cftable_entry_t *cf = &parse.cftable_entry;
  250.     local_info_t *dev;
  251.     int i;
  252.     u_char buf[64];
  253.     char devname[128];
  254.     int busy = 0;
  255.     
  256.     handle = link->handle;
  257.     dev = link->priv;
  258.     DEBUG(0, "avma1cs_config(0x%p)n", link);
  259.     /*
  260.        This reads the card's CONFIG tuple to find its configuration
  261.        registers.
  262.     */
  263.     do {
  264. tuple.DesiredTuple = CISTPL_CONFIG;
  265. i = CardServices(GetFirstTuple, handle, &tuple);
  266. if (i != CS_SUCCESS) break;
  267. tuple.TupleData = buf;
  268. tuple.TupleDataMax = 64;
  269. tuple.TupleOffset = 0;
  270. i = CardServices(GetTupleData, handle, &tuple);
  271. if (i != CS_SUCCESS) break;
  272. i = CardServices(ParseTuple, handle, &tuple, &parse);
  273. if (i != CS_SUCCESS) break;
  274. link->conf.ConfigBase = parse.config.base;
  275.     } while (0);
  276.     if (i != CS_SUCCESS) {
  277. cs_error(link->handle, ParseTuple, i);
  278. link->state &= ~DEV_CONFIG_PENDING;
  279. return;
  280.     }
  281.     
  282.     /* Configure card */
  283.     link->state |= DEV_CONFIG;
  284.     do {
  285. tuple.Attributes = 0;
  286. tuple.TupleData = buf;
  287. tuple.TupleDataMax = 254;
  288. tuple.TupleOffset = 0;
  289. tuple.DesiredTuple = CISTPL_VERS_1;
  290. devname[0] = 0;
  291. if( !first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 1 ) {
  292.     strncpy(devname,parse.version_1.str + parse.version_1.ofs[1], 
  293. sizeof(devname));
  294. }
  295. /*
  296.          * find IO port
  297.          */
  298. tuple.TupleData = (cisdata_t *)buf;
  299. tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
  300. tuple.Attributes = 0;
  301. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  302. i = first_tuple(handle, &tuple, &parse);
  303. while (i == CS_SUCCESS) {
  304.     if (cf->io.nwin > 0) {
  305. link->conf.ConfigIndex = cf->index;
  306. link->io.BasePort1 = cf->io.win[0].base;
  307. link->io.NumPorts1 = cf->io.win[0].len;
  308. link->io.NumPorts2 = 0;
  309.                 printk(KERN_INFO "avma1_cs: testing i/o %#x-%#xn",
  310. link->io.BasePort1,
  311.         link->io.BasePort1+link->io.NumPorts1 - 1);
  312. i = CardServices(RequestIO, link->handle, &link->io);
  313. if (i == CS_SUCCESS) goto found_port;
  314.     }
  315.     i = next_tuple(handle, &tuple, &parse);
  316. }
  317. found_port:
  318. if (i != CS_SUCCESS) {
  319.     cs_error(link->handle, RequestIO, i);
  320.     break;
  321. }
  322. /*
  323.  * allocate an interrupt line
  324.  */
  325. i = CardServices(RequestIRQ, link->handle, &link->irq);
  326. if (i != CS_SUCCESS) {
  327.     cs_error(link->handle, RequestIRQ, i);
  328.     CardServices(ReleaseIO, link->handle, &link->io);
  329.     break;
  330. }
  331. /*
  332.          * configure the PCMCIA socket
  333.   */
  334. i = CardServices(RequestConfiguration, link->handle, &link->conf);
  335. if (i != CS_SUCCESS) {
  336.     cs_error(link->handle, RequestConfiguration, i);
  337.     CardServices(ReleaseIO, link->handle, &link->io);
  338.     CardServices(ReleaseIRQ, link->handle, &link->irq);
  339.     break;
  340. }
  341.     } while (0);
  342.     /* At this point, the dev_node_t structure(s) should be
  343.        initialized and arranged in a linked list at link->dev. */
  344.     strcpy(dev->node.dev_name, "A1");
  345.     dev->node.major = 45;
  346.     dev->node.minor = 0;
  347.     link->dev = &dev->node;
  348.     
  349.     link->state &= ~DEV_CONFIG_PENDING;
  350.     /* If any step failed, release any partially configured state */
  351.     if (i != 0) {
  352. avma1cs_release((u_long)link);
  353. return;
  354.     }
  355.     printk(KERN_NOTICE "avma1_cs: checking at i/o %#x, irq %dn",
  356. link->io.BasePort1, link->irq.AssignedIRQ);
  357.     if (avm_a1_init_pcmcia((void *)(int)link->io.BasePort1,
  358.                            link->irq.AssignedIRQ,
  359.                            &busy, isdnprot) != 0) {
  360.        printk(KERN_ERR "avma1_cs: failed to initialize AVM A1 PCMCIA %d at i/o %#xn", i, link->io.BasePort1);
  361.        return;
  362.     }
  363.     i = 0; /* no returncode for cardnr :-( */
  364.     dev->node.minor = i;
  365. } /* avma1cs_config */
  366. /*======================================================================
  367.     After a card is removed, avma1cs_release() will unregister the net
  368.     device, and release the PCMCIA configuration.  If the device is
  369.     still open, this will be postponed until it is closed.
  370.     
  371. ======================================================================*/
  372. static void avma1cs_release(u_long arg)
  373. {
  374.     dev_link_t *link = (dev_link_t *)arg;
  375.     local_info_t *local = link->priv;
  376.     DEBUG(0, "avma1cs_release(0x%p)n", link);
  377.     /*
  378.        If the device is currently in use, we won't release until it
  379.        is actually closed.
  380.     */
  381.     if (link->open) {
  382. DEBUG(1, "avma1_cs: release postponed, '%s' still openn",
  383.       link->dev->dev_name);
  384. link->state |= DEV_STALE_CONFIG;
  385. return;
  386.     }
  387.     /* no unregister function with hisax */
  388.     HiSax_closecard(local->node.minor);
  389.     /* Unlink the device chain */
  390.     link->dev = NULL;
  391.     
  392.     /* Don't bother checking to see if these succeed or not */
  393.     CardServices(ReleaseConfiguration, link->handle);
  394.     CardServices(ReleaseIO, link->handle, &link->io);
  395.     CardServices(ReleaseIRQ, link->handle, &link->irq);
  396.     link->state &= ~DEV_CONFIG;
  397.     
  398.     if (link->state & DEV_STALE_LINK)
  399. avma1cs_detach(link);
  400.     
  401. } /* avma1cs_release */
  402. /*======================================================================
  403.     The card status event handler.  Mostly, this schedules other
  404.     stuff to run after an event is received.  A CARD_REMOVAL event
  405.     also sets some flags to discourage the net drivers from trying
  406.     to talk to the card any more.
  407.     When a CARD_REMOVAL event is received, we immediately set a flag
  408.     to block future accesses to this device.  All the functions that
  409.     actually access the device should check this flag to make sure
  410.     the card is still present.
  411.     
  412. ======================================================================*/
  413. static int avma1cs_event(event_t event, int priority,
  414.   event_callback_args_t *args)
  415. {
  416.     dev_link_t *link = args->client_data;
  417.     DEBUG(1, "avma1cs_event(0x%06x)n", event);
  418.     
  419.     switch (event) {
  420.     case CS_EVENT_CARD_REMOVAL:
  421. link->state &= ~DEV_PRESENT;
  422. if (link->state & DEV_CONFIG) {
  423.     link->release.expires =  jiffies + HZ/20;
  424.     add_timer(&link->release);
  425. }
  426. break;
  427.     case CS_EVENT_CARD_INSERTION:
  428. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  429. avma1cs_config(link);
  430. break;
  431.     case CS_EVENT_PM_SUSPEND:
  432. link->state |= DEV_SUSPEND;
  433. /* Fall through... */
  434.     case CS_EVENT_RESET_PHYSICAL:
  435. if (link->state & DEV_CONFIG)
  436.     CardServices(ReleaseConfiguration, link->handle);
  437. break;
  438.     case CS_EVENT_PM_RESUME:
  439. link->state &= ~DEV_SUSPEND;
  440. /* Fall through... */
  441.     case CS_EVENT_CARD_RESET:
  442. if (link->state & DEV_CONFIG)
  443.     CardServices(RequestConfiguration, link->handle, &link->conf);
  444. break;
  445.     }
  446.     return 0;
  447. } /* avma1cs_event */
  448. /*====================================================================*/
  449. static int __init init_avma1_cs(void)
  450. {
  451.     servinfo_t serv;
  452.     DEBUG(0, "%sn", version);
  453.     CardServices(GetCardServicesInfo, &serv);
  454.     if (serv.Revision != CS_RELEASE_CODE) {
  455.         printk(KERN_NOTICE "avma1_cs: Card Services release "
  456.                "does not match!n");
  457.         return -1;
  458.     }
  459.     register_pccard_driver(&dev_info, &avma1cs_attach, &avma1cs_detach);
  460.     return 0;
  461. }
  462. static void __exit exit_avma1_cs(void)
  463. {
  464.     DEBUG(0, "avma1_cs: unloadingn");
  465.     unregister_pccard_driver(&dev_info);
  466.     while (dev_list != NULL)
  467. if (dev_list->state & DEV_CONFIG)
  468.     avma1cs_release((u_long)dev_list);
  469.         avma1cs_detach(dev_list);
  470. }
  471. module_init(init_avma1_cs);
  472. module_exit(exit_avma1_cs);