LT4000 driver source code

mail mail towergeebulk at gmail.com
Wed Sep 7 11:37:55 EDT 2005


Hi, 


Here is my source code.

==================================BEGIN==============================

 /*======================================================================

   A Infineer PCMCIA client driver

   LT4000_cs.c 1.28 2000/10/04 00:31:08

   Derived from dummy_cs.c 1.27 2000/06/12 21:27:25 which had the
   following notice:

   The contents of this file are subject to the Mozilla Public
   License Version 1.1 (the "License"); you may not use this file
   except in compliance with the License. You may obtain a copy of
   the License at 

   Software distributed under the License is distributed on an "AS
   IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
   implied. See the License for the specific language governing
   rights and limitations under the License. 

   The initial developer of the original code is David A. Hinds
   <dahinds at users.sourceforge.net>.  Portions created by David A. Hinds
   are Copyright (C) 1999 David A. Hinds.  All Rights Reserved. 

   Alternatively, the contents of this file may be used under the
   terms of the GNU Public License version 2 (the "GPL"), in which
   case the provisions of the GPL are applicable instead of the
   above.  If you wish to allow the use of your version of this file 
   only under the terms of the GPL and not to allow others to use
   your version of this file under the MPL, indicate your decision
   by deleting the provisions above and replace them with the notice
   and other provisions required by the GPL.  If you do not delete 
   the provisions above, a recipient may use your version of this
   file under either the MPL or the GPL.

======================================================================*/
#define PCMCIA_DEBUG 2
#define INFINEER_BUFSZ          265     /* Max buffer size      */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/string.h> 
#include <linux/timer.h>
#include <linux/ioport.h>
#include <linux/fs.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <asm/uaccess.h> 

#include <pcmcia/version.h>
#include <pcmcia/cs_types.h>
#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/cisreg.h>
#include <pcmcia/ds.h>
#include <linux/kref.h> 
//#include <pcmcia/bus_ops.h>

#include "LT4000_cs.h"

/*
  All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
  you do not define PCMCIA_DEBUG at all, all the debug code will be
  left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
  be present but disabled -- but it can then be enabled for specific
  modules at load time with a 'pc_debug=#' option to insmod.
*/

static int debug = 0;

#ifdef PCMCIA_DEBUG
//static int pc_debug = PCMCIA_DEBUG;
//MODULE_PARM(pc_debug, "i");
#define DEBUG(n, args...) printk(KERN_DEBUG args)
//#define PRINTK printk
#define PRINTK(format, arg...) do { if (debug) printk(KERN_DEBUG \ 
__FILE__ ": " format "\n" , ## arg); } while (0)
static char *version =
"LT4000_cs.c 1.02 2005/8/30 01:04:01 (TowerGee) 1.01 2004/05/20
00:31:08 (Alexandre Davi Silva)";
#else
#define DEBUG(n, args...)
#endif

/*====================================================================*/
 /* Parameters that can be set with 'insmod' */
/* hharan - to be removed */
/* Release IO ports after configuration? */
static int free_ports = 0; 

/* hharan - to be removed */
/* The old way: bit map of interrupts to choose from */
/* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
static u_int irq_mask = 0xdeb8;
/* Newer, simpler way of listing specific interrupts */ 
static int irq_list[4] = { -1 };

MODULE_PARM(free_ports, "i");
MODULE_PARM(irq_mask, "i");
MODULE_PARM(irq_list, "1-4i");

/*====================================================================*/

/*
  The event() function is this driver's Card Services event handler.
  It will be called by Card Services when an appropriate card status
  event is received.  The config() and release() entry points are
  used to configure or release a socket, in response to card
  insertion and ejection events.  They are invoked from the LT4000
  event handler.
*/

static void LT4000_config(dev_link_t *link);
static void LT4000_release(u_long arg);
static int  LT4000_event(event_t event, int priority,
                      event_callback_args_t *args);

/*
  The attach() and detach() entry points are used to create and destroy
  "instances" of the driver, where each instance represents everything
  needed to manage one actual PCMCIA card.
*/

static dev_link_t *LT4000_attach(void);
static void LT4000_detach(dev_link_t *);

/*
  To Do: Need to prototype all the functions that will actually
  be used to talk to the LT4000
*/
static ssize_t  LT4000_read(struct file * , char *, size_t,loff_t *);
static ssize_t  LT4000_write(struct file * , const char * , size_t , loff_t *); 
static int LT4000_open(struct inode *, struct file *) ;
static int LT4000_close(struct inode *, struct file *) ;
static int LT4000_ioctl(struct inode *, struct file *,u_int , u_long );

static struct file_operations LT4000_fops = {
open: LT4000_open,
release: LT4000_close,
read: LT4000_read,
write: LT4000_write,
ioctl: LT4000_ioctl,
};

/*
  The dev_info variable is the "key" that is used to match up this
  device driver with appropriate cards, through the card configuration
  database.
*/

static dev_info_t dev_info = "LT4000_cs";

/*
  A linked list of "instances" of the LT4000 device.  Each actual
  PCMCIA card corresponds to one device instance, and is described
  by one dev_link_t structure (defined in ds.h).

  You may not want to use a linked list for this -- for example, the
  memory card driver uses an array of dev_link_t pointers, where minor
  device numbers are used to derive the corresponding array index.
*/ 

static dev_link_t *dev_list = NULL;

/*
  A dev_link_t structure has fields for most things that are needed
  to keep track of a socket, but there will usually be some device
  specific information that also needs to be kept track of.  The
  'priv' pointer in a dev_link_t structure can be used to point to 
  a device-specific private data structure, like this.

  To simplify the data structure handling, we actually include the
  dev_link_t structure in the device's private data structure.

  A driver needs to provide a dev_node_t structure for each device
  on a card.  In some cases, there is only one device per card (for
  example, ethernet cards, modems).  In other cases, there may be
  many actual or logical devices (SCSI adapters, memory cards with 
  multiple partitions).  The dev_node_t structures need to be kept
  in a linked list starting at the 'dev' field of a dev_link_t
  structure.  We allocate them in the card's private data structure,
  because they generally shouldn't be allocated dynamically. 

  In this case, we also provide a flag to indicate if a device is
  "stopped" due to a power management event, or card ejection.  The
  device IO routines can use a flag like this to throttle IO to a
  card that is not ready to accept it.

  The bus_operations pointer is used on platforms for which we need
  to use special socket-specific versions of normal IO primitives
  (inb, outb, readb, writeb, etc) for card IO.
*/
 /*
typedef struct local_info_t { 
   dev_link_t          link;
   dev_node_t          node;
   int                 stop;
   struct bus_operations *bus;
} local_info_t;
 sample devnode_t structure */

 /*LT4000 Dev_t Structure*/
typedef struct LT4000_local_t {
       dev_link_t link;
       dev_node_t node;
       int stop;
       //struct bus_operations *bus;
       caddr_t am_base;        /* Base of mapped attribute memory      */ 
       u_long io_base;         /* Base of I/O port range               */
       struct kref kref;
} LT4000_local_t;

// Added by TowerGee
static struct pcmcia_driver LT4000_driver = {
       .owner  = THIS_MODULE,
       .drv    = {
               .name = "LT4000_cs",
       },
       .attach = LT4000_attach, 
       .detach = LT4000_detach,
};

#define to_skel_dev(d) container_of(d, struct LT4000_local_t, kref)

static int writeToDevice(u_char * ,u_char * ,LT4000_local_t * );


/*====================================================================*/
#if 0
void cs_error(client_handle_t handle, int func, int ret)
{
   error_info_t err = { func, ret };
   //CardServices(ReportError, handle, &err); 
   pcmcia_report_error(handle, &err);
}
#endif
/*======================================================================

   LT4000_attach() creates an "instance" of the driver, allocating
   local data structures for one device.  The device is registered
   with Card Services.

   The dev_link structure is initialized, but we don't actually
   configure the card at this point -- we wait until we receive a
   card insertion event.

======================================================================*/

static dev_link_t *LT4000_attach(void)
{
   LT4000_local_t *local;
   dev_link_t *link;
   client_reg_t client_reg;
   int ret, i;

   //DEBUG(0, "LT4000_attach()\n");
   printk("LT4000_attach() v1.3\n");

   /* Allocate space for private device-specific data */
   local = kmalloc(sizeof(LT4000_local_t), GFP_KERNEL);
   if (!local) return NULL;
   memset(local, 0, sizeof(LT4000_local_t));
   link = &local->link; link->priv = local; 

   /* Initialize the dev_link_t structure */
   //link->release.function = &LT4000_release;
   //link->release.data = (u_long)link;

   /* Interrupt setup */
   link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
   link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
   if (irq_list[0] == -1)
       link->irq.IRQInfo2 = irq_mask;
   else 
       for (i = 0; i < 4; i++)
           link->irq.IRQInfo2 |= 1 << irq_list[i];
   link->irq.Handler = NULL;

   /*
     General socket configuration defaults can go here.  In this
     client, we assume very little, and rely on the CIS for almost
     everything.  In most clients, many details (i.e., number, sizes,
     and attributes of IO windows) are fixed by the nature of the
     device, and can be hard-wired here.
   */
   link->conf.Attributes = 0;
   link->conf.Vcc = 50;
   link->conf.IntType = INT_MEMORY_AND_IO; 

   /* Register with Card Services */
   link->next = dev_list;
   dev_list = link;
   client_reg.dev_info = &dev_info;
   client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
   client_reg.EventMask = 
       CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
       CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
       CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
   client_reg.event_handler = &LT4000_event; 
   client_reg.Version = 0x0210;
   client_reg.event_callback_args.client_data = link;
   //ret = CardServices(RegisterClient, &link->handle, &client_reg);
   ret = pcmcia_register_client(&link->handle, &client_reg); 
   if (ret != CS_SUCCESS) {
       cs_error(link->handle, RegisterClient, ret);
       LT4000_detach(link);
       return NULL;
   }

   // Added by TowerGee
   // kref->get(&local->kref);
   //kref_init(&local->kref);

   return link;
} /* LT4000_attach */

/*======================================================================

   This deletes a driver "instance".  The device is de-registered
   with Card Services.  If it has been released, all local data
   structures are freed.  Otherwise, the structures will be freed
   when the device is released. 

======================================================================*/

static void LT4000_detach(dev_link_t *link)
{
   dev_link_t **linkp;

   //EBUG(0, "LT4000_detach(0x%p)\n", link);
   printk("LT4000_detach(0x%p)\n", link);

   /* Locate device structure */
   for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
       if (*linkp == link) break;
   if (*linkp == NULL)
       return;

   /*
      If the device is currently configured and active, we won't
      actually delete it yet.  Instead, it is marked so that when
      the release() function is called, that will trigger a proper
      detach(). 
   */
   if (link->state & DEV_CONFIG) {
#ifdef PCMCIA_DEBUG
       printk(KERN_DEBUG "LT4000_cs: detach postponed, '%s' "
              "still locked\n", link->dev->dev_name); 
#endif
       link->state |= DEV_STALE_LINK;
       return;
   }

   /* Break the link with Card Services */
   if (link->handle)
       //CardServices(DeregisterClient, link->handle);
       pcmcia_deregister_client(link->handle);

   /* Unlink device structure, and free it */
   *linkp = link->next;
   /* This points to the parent local_info_t struct */
   kfree(link->priv);

   // Added by Towergee
   //kref->put(&link.priv.kref, LT4000_delete);

} /* LT4000_detach */

/*======================================================================

   LT4000_config() is scheduled to run after a CARD_INSERTION event
   is received, to configure the PCMCIA socket, and to make the
   device available to the system.

======================================================================*/
/*
#define CS_CHECK(fn, args...) \
while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed

#define CS_CHECK(fn, ret) \
  do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)

#define CS_CHECK(fn_number, fn, args...) \
do { last_fn = fn_number; \
 if ((last_ret = fn(args)) != 0) goto cs_failed; \
} while(0)

#define CFG_CHECK(fn, args...) \
if (CardServices(fn, args) != 0) goto next_entry

#define CFG_CHECK(fn_number, fn, args...) \
{ last_fn = fn_number \
if ((last_ret=fn(args))!=0) {  \
*/
#define CS_CHECK(fn, ret) \
do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0) 
                                                                                
#define CFG_CHECK(fn, ret) \
 if ((last_ret = (ret)) != 0) goto next_entry;

 

static void LT4000_config(dev_link_t *link)
{
   client_handle_t handle = link->handle;
   LT4000_local_t *dev = link->priv;
   tuple_t tuple;
   cisparse_t parse;
   int last_fn, last_ret;
   u_char buf[64];
   config_info_t conf;
   win_req_t req;
   memreq_t map;
   cisinfo_t cisinfo;
   int ret=0;

   //DEBUG(1, "LT4000_config(0x%p)\n", link);
   printk("LT4000_config(0x%p)\n", link);

   /*
      This reads the card's CONFIG tuple to find its configuration
      registers.
   */
   //ret = CardServices(ValidateCIS,handle,&cisinfo);
   ret = pcmcia_validate_cis(handle, &cisinfo); 
       printk(KERN_INFO "ValidateCIS returns %d\n",cisinfo.Chains);
   tuple.DesiredTuple = CISTPL_CONFIG;
   tuple.Attributes = 0;
   tuple.TupleData = buf;
   tuple.TupleDataMax = sizeof(buf);
   tuple.TupleOffset = 0;
   //CS_CHECK(GetFirstTuple, handle, &tuple);
   CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
   //CS_CHECK(GetTupleData, handle, &tuple);
   CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple)); 
   //CS_CHECK(ParseTuple, handle, &tuple, &parse);
   CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
   link->conf.ConfigBase = parse.config.base;
   link->conf.Present = parse.config.rmask[0];

   /* Configure card */
   link->state |= DEV_CONFIG;

   /* Look up the current Vcc */
   //CS_CHECK(GetConfigurationInfo, handle, &conf);
   CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
   link->conf.Vcc = conf.Vcc;

   /*
     In this loop, we scan the CIS for configuration table entries,
     each of which describes a valid card configuration, including
     voltage, IO window, memory window, and interrupt settings.

     We make no assumptions about the card to be configured: we use
     just the information available in the CIS.  In an ideal world,
     this would work for any PCMCIA card, but it requires a complete
     and accurate CIS.  In practice, a driver usually "knows" most of 
     these things without consulting the CIS, and most client drivers
     will only use the CIS to fill in implementation-defined details.
   */
   tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
   //CS_CHECK(GetFirstTuple, handle, &tuple); 
   CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));

   while (1) {
       cistpl_cftable_entry_t dflt = { 0 };
       cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
       //CFG_CHECK(GetTupleData, handle, &tuple);
       CFG_CHECK(GetTupleDate, pcmcia_get_tuple_data(handle, &tuple)); 
       //CFG_CHECK(ParseTuple, handle, &tuple, &parse);
       CFG_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));

       if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
       if (cfg->index == 0) goto next_entry;
       link->conf.ConfigIndex = cfg->index;

       /* Does this card need audio output? */
       if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
           link->conf.Attributes |= CONF_ENABLE_SPKR;
           link->conf.Status = CCSR_AUDIO_ENA; 
       }

       /* Use power settings for Vcc and Vpp if present */
       /*  Note that the CIS values need to be rescaled */
       if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
           if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000)
               goto next_entry;
       } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
           if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM]/10000) 
               goto next_entry;
       }

       if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
           link->conf.Vpp1 = link->conf.Vpp2 =
               cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
       else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
           link->conf.Vpp1 = link->conf.Vpp2 =
               dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;

       /* Do we need to allocate an interrupt? */
       if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
           link->conf.Attributes |= CONF_ENABLE_IRQ;

       /* IO window settings */
       link->io.NumPorts1 = link->io.NumPorts2 = 0;
       if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
           cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io; 
           link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
           if (!(io->flags & CISTPL_IO_8BIT))
               link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
           if (!(io->flags & CISTPL_IO_16BIT)) 
               link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
           link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
           link->io.BasePort1 = io->win[0].base;
           link-> io.NumPorts1 = io->win[0].len;
           if (io->nwin > 1) {
               link->io.Attributes2 = link->io.Attributes1;
               link->io.BasePort2 = io->win[1].base;
               link-> io.NumPorts2 = io->win[1].len;
           }
           /* This reserves IO space but doesn't actually enable it */
           //CFG_CHECK(RequestIO, link->handle, &link->io);
           CFG_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io)); 
       }

       /*
         Now set up a common memory window, if needed.  There is room
         in the dev_link_t structure for one memory window handle,
         but if the base addresses need to be saved, or if multiple 
         windows are needed, the info should go in the private data
         structure for this device.

         Note that the memory window base is a physical address, and
         needs to be mapped to virtual space with ioremap() before it
         is used.
       */
       if ((cfg->mem.nwin > 0) || ( dflt.mem.nwin > 0)) {
           cistpl_mem_t *mem =
               (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
           req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM;
           req.Attributes |= WIN_ENABLE;
           req.Base = mem->win[0].host_addr;
           req.Size = mem->win[0].len;
           if (req.Size < 0x1000)
               req.Size = 0x1000;
           req.AccessSpeed = 0; 
           link->win = (window_handle_t)link->handle;
           //CFG_CHECK(RequestWindow, &link->win, &req);
           CFG_CHECK(RequestWindow,
pcmcia_request_window(&link->handle, &req, &link->win));
           map.Page = 0; map.CardOffset = mem->win[0].card_addr;
           //CFG_CHECK(MapMemPage, link->win, &map);
           CFG_CHECK(MapMemPage, pcmcia_map_mem_page(link->win, &map));
       } 
       /* If we got this far, we're cool! */
       break;

   next_entry:
       if (link->io.NumPorts1)
           //CardServices(ReleaseIO, link->handle, &link->io);
           pcmcia_release_io(link->handle, &link->io);
       //CS_CHECK(GetNextTuple, handle, &tuple); 
       CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
   }

   /*
      Allocate an interrupt line.  Note that this does not assign a
      handler to the interrupt, unless the 'Handler' member of the
      irq structure is initialized.
   */
   if (link->conf.Attributes & CONF_ENABLE_IRQ)
       //CS_CHECK(RequestIRQ, link->handle, &link->irq);
       CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));

   /*
      This actually configures the PCMCIA socket -- setting up
      the I/O windows and the interrupt mapping, and putting the
      card and host interface into "Memory and IO" mode.
   */ 
   //CS_CHECK(RequestConfiguration, link->handle, &link->conf);
   CS_CHECK(RequestConfiguration,
pcmcia_request_configuration(link->handle, &link->conf));

   /*
     We can release the IO port allocations here, if some other
     driver for the card is going to loaded, and will expect the
     ports to be available.
   */
   if (free_ports) {
       printk(KERN_INFO "in free ports \n"); 
       if (link->io.BasePort1)
           release_region(link->io.BasePort1, link->io.NumPorts1);
       if (link->io.BasePort2)
           release_region(link->io.BasePort2, link->io.NumPorts2 );
   }

   /*
     At this point, the dev_node_t structure(s) need to be
     initialized and arranged in a linked list at link->dev.
 */
   sprintf(dev->node.dev_name, "LT4000");
   dev->node.major = 123;
   dev->node.minor = 0;

 /* Maps Physical Memmory To Local Data Structure For IO */
 /* Using physical Base requires (caddr_t) cast */
   dev->am_base = ioremap(req.Base, req.Size);
       dev->io_base = link->io.BasePort1; 

   /* Make Device Link*/
   link->dev = &dev->node;

   /* Finally, report what we've done */
   printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
          dev->node.dev_name, link->conf.ConfigIndex,
          link->conf.Vcc/10, link->conf.Vcc%10 );
   if (link->conf.Vpp1)
       printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
   if (link->conf.Attributes & CONF_ENABLE_IRQ)
       printk(", irq %d", link-> irq.AssignedIRQ);
   if (link->io.NumPorts1)
       printk(", io 0x%04x-0x%04x", link->io.BasePort1,
              link->io.BasePort1+link->io.NumPorts1-1);
   if (link->io.NumPorts2)
       printk(" & 0x%04x-0x%04x", link->io.BasePort2,
              link->io.BasePort2+link->io.NumPorts2-1);
   if (link->win)
       printk(", mem 0x%06lx-0x%06lx", req.Base, 
              req.Base+req.Size-1);
   printk("\n");

   link->state &= ~DEV_CONFIG_PENDING;
   return;

cs_failed:
   cs_error(link->handle, last_fn, last_ret);
   LT4000_release((u_long)link);

} /* LT4000_config */

/*======================================================================

   After a card is removed, LT4000_release() will unregister the
   device, and release the PCMCIA configuration.  If the device is
   still open, this will be postponed until it is closed.

======================================================================*/

static void LT4000_release(u_long arg)
{
   dev_link_t *link = (dev_link_t *)arg;

   //DEBUG(0, "LT4000_release(0x%p)\n", link);
   printk("LT4000_release(0x%p)\n", link);

   /*
      If the device is currently in use, we won't release until it
      is actually closed, because until then, we can't be sure that
      no one will try to access the device or its data structures.
   */
   if (link->open) {
       //DEBUG(1, "LT4000_cs: release postponed, '%s' still open\n",
       printk("LT4000_cs: release postponed, '%s' still open\n",
             link->dev->dev_name); 
       link->state |= DEV_STALE_CONFIG;
       return;
   }

   /* Unlink the device chain */
   link->dev = NULL;

   /*
     In a normal driver, additional code may be needed to release
     other kernel data structures associated with this device.
   */

   /* Don't bother checking to see if these succeed or not */
   if (link->win)
      // CardServices(ReleaseWindow, link->win);
   //CardServices(ReleaseConfiguration, link->handle);
      pcmcia_release_window(link->win); 
      pcmcia_release_configuration(link->handle);
   if (link->io.NumPorts1)
       //CardServices(ReleaseIO, link->handle, &link->io);
       pcmcia_release_io(link->handle, &link->io); 
   if (link->irq.AssignedIRQ)
       //CardServices(ReleaseIRQ, link->handle, &link->irq);
       pcmcia_release_irq(link->handle, &link->irq);
   link->state &= ~DEV_CONFIG;

   if (link->state & DEV_STALE_LINK)
       LT4000_detach(link);

} /* LT4000_release */

/*======================================================================

   The card status event handler.  Mostly, this schedules other
   stuff to run after an event is received.

   When a CARD_REMOVAL event is received, we immediately set a
   private flag to block future accesses to this device.  All the
   functions that actually access the device should check this flag
   to make sure the card is still present. 

======================================================================*/

static int LT4000_event(event_t event, int priority,
                      event_callback_args_t *args)
{
   dev_link_t *link = args->client_data;
   LT4000_local_t *dev = link->priv;

   //DEBUG(1, "LT4000_event(0x%06x)\n", event);
   printk( "LT4000_event(0x%06x)\n", event);

   switch (event) {
   case CS_EVENT_CARD_REMOVAL:
       link->state &= ~DEV_PRESENT;
       if (link->state & DEV_CONFIG) {
           ((LT4000_local_t *)link->priv)->stop = 1;
           //mod_timer(&link->release, jiffies + HZ/20); 
       }
       break;
   case CS_EVENT_CARD_INSERTION:
       link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
//      dev->bus = args->bus;
       LT4000_config(link);
       break;
   case CS_EVENT_PM_SUSPEND: 
       link->state |= DEV_SUSPEND;
       /* Fall through... */
   case CS_EVENT_RESET_PHYSICAL:
       /* Mark the device as stopped, to block IO until later */
       dev->stop = 1;
       if (link->state & DEV_CONFIG) 
           //CardServices(ReleaseConfiguration, link->handle);
           pcmcia_release_configuration(link->handle);
       break;
   case CS_EVENT_PM_RESUME:
       link->state &= ~DEV_SUSPEND; 
       /* Fall through... */
   case CS_EVENT_CARD_RESET:
       if (link->state & DEV_CONFIG)
           //CardServices(RequestConfiguration, link->handle, &link->conf);
           pcmcia_release_configuration(link->handle); 
       dev->stop = 0;
       /*
         In a normal driver, additional code may go here to restore
         the device state and restart IO.
       */
       break;
   }
   return 0;
} /* LT4000_event */ 

/*====================================================================*/
/*
static struct file_operations LT4000_fops = {
owner: THIS_MODULE,
open: LT4000_open,
release: LT4000_close,
read: LT4000_read, 
write: LT4000_write,
ioctl: LT4000_ioctl,
} ;
*/

int majorn=123;

static int __init init_LT4000_cs(void)
{
   // Commented by TowerGee
   servinfo_t serv;
   // Added by TowerGee
   int error;

   //DEBUG(0, "%s\n", version);
   printk("%s\n", version);

   // Commented by TowerGee
   //CardServices(GetCardServicesInfo, &serv);
   pcmcia_get_card_services_info(&serv);
   if (serv.Revision != CS_RELEASE_CODE) {
       printk(KERN_NOTICE "LT4000_cs: Card Services release " 
              "does not match!\n");
       return -1;
   }
   //register_pccard_driver(&dev_info, &LT4000_attach, &LT4000_detach);

   // Added by TowerGee
   error = pcmcia_register_driver(&LT4000_driver);
   if (error)
       return error;

       majorn = register_chrdev(123,"LT4000",&LT4000_fops);


   return 0;
}

static void __exit exit_LT4000_cs(void)
{
   //DEBUG(0, "LT4000_cs: unloading\n");
   printk("LT4000_cs: unloading\n");
   // Commented by TowerGee
   /*
   unregister_pccard_driver(&dev_info); 
       unregister_chrdev(123,"LT4000");
   while (dev_list != NULL) {
       //del_timer(&dev_list->release);
       if (dev_list->state & DEV_CONFIG)
           LT4000_release((u_long)dev_list); 
       LT4000_detach(dev_list);
   }
   */
   pcmcia_unregister_driver(&LT4000_driver);
   while (dev_list != NULL)
       LT4000_detach(dev_list);
}

module_init(init_LT4000_cs);
module_exit(exit_LT4000_cs);


/**********************************************************************************/
/* Smart Port Specific Routines*/
/**********************************************************************************/
static ssize_t  LT4000_read(struct file *file,char *buf,size_t count
,loff_t *loc)
{

       return -EINVAL;
}

static ssize_t LT4000_write(struct file *file,const char *buf,size_t
count , loff_t *loc)
{
       return -EINVAL;
}

static int LT4000_open(struct inode *inode , struct file *file )
{
 int minor = MINOR(inode->i_rdev);
 dev_link_t *link;
/*  LT4000_local_t *dev;  */

 printk(KERN_INFO "LT4000_open(%d)\n", minor);

 for (link = dev_list; link; link = link->next) {
     if (link->dev && link->dev->minor == minor)
         break;
 }
 if(!DEV_OK(link))
       return -EINVAL;
 link->open++;
 //MOD_INC_USE_COUNT;
 //kref_get(&link.priv->kref);
 return 0;
}

static int LT4000_close(struct inode *inode , struct file *file )
{
 int minor = MINOR(inode->i_rdev);
 dev_link_t *link;
 printk(KERN_INFO "LT4000_close(%d)\n", minor);

 for (link = dev_list; link; link = link->next) {
     if (link->dev && link->dev->minor == minor)
         break;
 }
 link->open--;
 //MOD_DEC_USE_COUNT;
 return 0;
}

#if 0
static u_char calcChksum( u_char data[] ,int length)
{
       int i;
       u_char chksum=0;
   for(i=0;i<length;i++) {
               chksum ^= data[i];
       }
       return chksum; 
}
#endif

static int writeToDevice(u_char * cmd ,u_char result[] ,LT4000_local_t *dev )
{
int i;
caddr_t am_base;   /* To store the base address */
u_char len;
u_char rlen;
u_char SPCmdPacket[265];
u_char ctl_byte; 
u_char chksum,sts_byte;
u_char status;
am_base = dev->am_base;

if(get_user(len,cmd+1)<0) return -EFAULT;
if(get_user(ctl_byte,cmd+0) < 0 ) return -EFAULT;
if ( ctl_byte & CONTROL_LENGTHBIT ) len+=256;
if(copy_from_user(SPCmdPacket,cmd,len+3)) return -EFAULT;

PRINTK("WSPCmdPacket = ");
for(i=0;i<len+3;i++) {
       PRINTK("%0x-",SPCmdPacket[i]);
       writeb(SPCmdPacket[i],am_base+i*2);
}
PRINTK("\n");

/* Tell the card that there is a new Command */
writeb(NEW_COMMAND,am_base+STATUS_OFFSET);

/* Wait for status to change */
while(1) {
   status = readb(am_base+STATUS_OFFSET);
   //printk(KERN_INFO " status = %0x \n ", status );
 if( status != STATUS_READY ) {
           schedule_timeout(1000);//100 
       }
   else
       break;
   }
sts_byte = readb(am_base);
PRINTK(KERN_INFO "sts_byte = %0x \n",sts_byte);
/*if( !validStatus(sts_byte) ) return  -1; */
rlen = readb(am_base+LENGTH_OFFSET) ; 
if( sts_byte & STATUS_LENGTHBIT)  rlen+=256;
PRINTK(KERN_INFO "rlen = %d \n",rlen);
chksum = readb(am_base+CHKSUM_OFFSET);
result[0]=sts_byte;
result[1]=rlen;
result[2]=chksum;
PRINTK(KERN_INFO " response = "); 
for(i=3;i<rlen+3;i++) {
   result[i]=readb(am_base+i*2);
   PRINTK("%0x-",result[i]);
}
PRINTK("\nSaiu Write to device\n");


return rlen+3;
}

static int LT4000_ioctl(struct inode *inode, struct file *file, u_int
cmd, u_long arg)
{
       int minor = MINOR(inode->i_rdev);
       dev_link_t *link;
       LT4000_local_t *dev;
       int length = 0;                                                
                                /* return value                 */
//      u_int size;                                                   
                                         /* size for data transfers   
  */
       u_char buf[LT4000_BUFSZ];

//#ifdef PCMCIA_DEBUG
//      if (pc_debug)
               printk(KERN_DEBUG "LT4000_ioctl(%d, 0x%0x)\n", minor, cmd);
//#endif

/*Determine Recipient of IOCTL*/
       for (link = dev_list; link; link = link->next) {
               if (link->dev && link->dev->minor == minor)
                       break;
       } 

       if (!DEV_OK(link))
               return -ENODEV;

       dev = (LT4000_local_t *)link->priv;
/*
       size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
       if (cmd & IOC_IN)
               if ((ret = verify_area(VERIFY_READ, (char *)arg, size))) 
                       return ret;
       if (cmd & IOC_OUT)
               if ((ret = verify_area(VERIFY_WRITE, (char *)arg, size)))
                       return ret;
*/
/*
       switch (cmd) { 
       case LT4000_SEL_CHAN:
        CmdAPDU[0]=0;
                CmdAPDU[1] = 0x00;
                CmdAPDU[2] = 0;
                CmdAPDU[3] = 0;
                CmdAPDU[4] = 0;
                length=5; 
                control=0xC0;
                break;
       case LT4000_STATUS:
                CmdAPDU[0]=0;
                CmdAPDU[1] = 0x16;
                CmdAPDU[2] = 0;
                CmdAPDU[3] = 0; 
                CmdAPDU[4] = 0;
                length =5;
                control = 0xC0;
                break;
       break;
   case LT4000_CONTROL:
               CmdAPDU[0]=0x00;
               CmdAPDU[1]=0x16; 
               CmdAPDU[2]=0x00;
               get_user(CmdAPDU[3],(u_char *)arg);
               CmdAPDU[4]=0x00;
               length=5;
               control=0x80;
               break;
   case LT4000_RESET: 
               CmdAPDU[0]=0x00;
               CmdAPDU[1]=0x02;
               CmdAPDU[2]=0x00;
               get_user(temp,(u_char *) arg);
               if( temp == 1 )
                       CmdAPDU[3]=0x01; 
               else
                       CmdAPDU[3]=0x00;
               CmdAPDU[4]=0x20;
               length = 5;
               control = 0xC0;
               break;

       case LT4000_SLEEP:
               CmdAPDU[0]=0x00;
               CmdAPDU[1]=0x0E;
               get_user(CmdAPDU[2],(u_char *)arg);
               get_user(CmdAPDU[3],(u_char *)(arg+1));
               CmdAPDU[4]=0x00; 
               length = 0x05;
               control = 0x80;
               break;

       case LT4000_POWER:
               CmdAPDU[0]=0x00;
               CmdAPDU[1]=0x14;
               CmdAPDU[2]=0x00;
               get_user(CmdAPDU[3],(u_char *)arg);
               CmdAPDU[4]=0x20; 
               length = 0x05;
               control = 0xC0;
               break;
       case LT4000_PASS_THRU:
               get_user(length,(u_char *)arg);
               get_user(temp,(u_char *)(arg+1)); 
               if( temp ==1 )
                       length+=256;
               for(i=0;i<length;i++) {
                       get_user(CmdAPDU[i],(u_char *)( arg+2+i));
                       }
               printk(KERN_INFO "length in transmit = %0x \n",length ); 
               control =0x00;
               break;
       default:
               ret = -EINVAL;
               break;
       }
       if( cmd == LT4000_DEBUG) {
               length = writeSPCmdPacket_debug(CmdAPDU,buf,length,dev); 
       }
       else
               length = writeSPCmdPacket(CmdAPDU ,buf ,length, control , dev);
       if( buf == NULL ) return -EINVAL;
       if ( copy_to_user((u_char *) arg,buf,length) ) return -EFAULT; 
       return length-1;

*/
       switch(cmd) {

       case LT4000_COMMAND :
                       length = writeToDevice((u_char *)arg,buf,dev);
                       if( length < 0) return length;
                       if ( copy_to_user((u_char *) arg,buf,length) )
return -EFAULT;
                       break;
       default:
                       break;
       }
       return 0;
}

MODULE_LICENSE("GPL");


===================================END===============================



More information about the linux-pcmcia mailing list