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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*======================================================================
  2.     A driver for Future Domain-compatible PCMCIA SCSI cards
  3.     fdomain_cs.c 1.47 2001/10/13 00:08:52
  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.     Alternatively, the contents of this file may be used under the
  16.     terms of the GNU General Public License version 2 (the "GPL"), in
  17.     which case the provisions of the GPL are applicable instead of the
  18.     above.  If you wish to allow the use of your version of this file
  19.     only under the terms of the GPL and not to allow others to use
  20.     your version of this file under the MPL, indicate your decision
  21.     by deleting the provisions above and replace them with the notice
  22.     and other provisions required by the GPL.  If you do not delete
  23.     the provisions above, a recipient may use your version of this
  24.     file under either the MPL or the GPL.
  25.     
  26. ======================================================================*/
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/kernel.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/string.h>
  33. #include <linux/timer.h>
  34. #include <linux/ioport.h>
  35. #include <scsi/scsi.h>
  36. #include <linux/major.h>
  37. #include <linux/blk.h>
  38. #include <../drivers/scsi/scsi.h>
  39. #include <../drivers/scsi/hosts.h>
  40. #include <scsi/scsi_ioctl.h>
  41. #include <../drivers/scsi/fdomain.h>
  42. #include <pcmcia/version.h>
  43. #include <pcmcia/cs_types.h>
  44. #include <pcmcia/cs.h>
  45. #include <pcmcia/cistpl.h>
  46. #include <pcmcia/ds.h>
  47. /*====================================================================*/
  48. /* Module parameters */
  49. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  50. MODULE_DESCRIPTION("Future Domain PCMCIA SCSI driver");
  51. MODULE_LICENSE("Dual MPL/GPL");
  52. #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
  53. /* Bit map of interrupts to choose from */
  54. INT_MODULE_PARM(irq_mask, 0xdeb8);
  55. static int irq_list[4] = { -1 };
  56. MODULE_PARM(irq_list, "1-4i");
  57. #ifdef PCMCIA_DEBUG
  58. INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
  59. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  60. static char *version =
  61. "fdomain_cs.c 1.47 2001/10/13 00:08:52 (David Hinds)";
  62. #else
  63. #define DEBUG(n, args...)
  64. #endif
  65. /*====================================================================*/
  66. typedef struct scsi_info_t {
  67.     dev_link_t link;
  68.     int ndev;
  69.     dev_node_t node[8];
  70. } scsi_info_t;
  71. extern void fdomain_setup(char *str, int *ints);
  72. static void fdomain_release(u_long arg);
  73. static int fdomain_event(event_t event, int priority,
  74. event_callback_args_t *args);
  75. static dev_link_t *fdomain_attach(void);
  76. static void fdomain_detach(dev_link_t *);
  77. static Scsi_Host_Template driver_template = FDOMAIN_16X0;
  78. static dev_link_t *dev_list = NULL;
  79. static dev_info_t dev_info = "fdomain_cs";
  80. /*====================================================================*/
  81. static void cs_error(client_handle_t handle, int func, int ret)
  82. {
  83.     error_info_t err = { func, ret };
  84.     CardServices(ReportError, handle, &err);
  85. }
  86. /*====================================================================*/
  87. static dev_link_t *fdomain_attach(void)
  88. {
  89.     scsi_info_t *info;
  90.     client_reg_t client_reg;
  91.     dev_link_t *link;
  92.     int i, ret;
  93.     
  94.     DEBUG(0, "fdomain_attach()n");
  95.     /* Create new SCSI device */
  96.     info = kmalloc(sizeof(*info), GFP_KERNEL);
  97.     if (!info) return NULL;
  98.     memset(info, 0, sizeof(*info));
  99.     link = &info->link; link->priv = info;
  100.     link->release.function = &fdomain_release;
  101.     link->release.data = (u_long)link;
  102.     link->io.NumPorts1 = 0x10;
  103.     link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  104.     link->io.IOAddrLines = 10;
  105.     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  106.     link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
  107.     if (irq_list[0] == -1)
  108. link->irq.IRQInfo2 = irq_mask;
  109.     else
  110. for (i = 0; i < 4; i++)
  111.     link->irq.IRQInfo2 |= 1 << irq_list[i];
  112.     link->conf.Attributes = CONF_ENABLE_IRQ;
  113.     link->conf.Vcc = 50;
  114.     link->conf.IntType = INT_MEMORY_AND_IO;
  115.     link->conf.Present = PRESENT_OPTION;
  116.     /* Register with Card Services */
  117.     link->next = dev_list;
  118.     dev_list = link;
  119.     client_reg.dev_info = &dev_info;
  120.     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
  121.     client_reg.event_handler = &fdomain_event;
  122.     client_reg.EventMask =
  123. CS_EVENT_RESET_REQUEST | CS_EVENT_CARD_RESET |
  124. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  125. CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  126.     client_reg.Version = 0x0210;
  127.     client_reg.event_callback_args.client_data = link;
  128.     ret = CardServices(RegisterClient, &link->handle, &client_reg);
  129.     if (ret != 0) {
  130. cs_error(link->handle, RegisterClient, ret);
  131. fdomain_detach(link);
  132. return NULL;
  133.     }
  134.     
  135.     return link;
  136. } /* fdomain_attach */
  137. /*====================================================================*/
  138. static void fdomain_detach(dev_link_t *link)
  139. {
  140.     dev_link_t **linkp;
  141.     DEBUG(0, "fdomain_detach(0x%p)n", link);
  142.     
  143.     /* Locate device structure */
  144.     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  145. if (*linkp == link) break;
  146.     if (*linkp == NULL)
  147. return;
  148.     del_timer(&link->release);
  149.     if (link->state & DEV_CONFIG) {
  150. fdomain_release((u_long)link);
  151. if (link->state & DEV_STALE_CONFIG) {
  152.     link->state |= DEV_STALE_LINK;
  153.     return;
  154. }
  155.     }
  156.     if (link->handle)
  157. CardServices(DeregisterClient, link->handle);
  158.     
  159.     /* Unlink device structure, free bits */
  160.     *linkp = link->next;
  161.     kfree(link->priv);
  162.     
  163. } /* fdomain_detach */
  164. /*====================================================================*/
  165. #define CS_CHECK(fn, args...) 
  166. while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
  167. #define CFG_CHECK(fn, args...) 
  168. if (CardServices(fn, args) != 0) goto next_entry
  169. static void fdomain_config(dev_link_t *link)
  170. {
  171.     client_handle_t handle = link->handle;
  172.     scsi_info_t *info = link->priv;
  173.     tuple_t tuple;
  174.     cisparse_t parse;
  175.     int i, last_ret, last_fn, ints[3];
  176.     u_char tuple_data[64];
  177.     Scsi_Device *dev;
  178.     dev_node_t *node, **tail;
  179.     char str[16];
  180.     struct Scsi_Host *host;
  181.     DEBUG(0, "fdomain_config(0x%p)n", link);
  182.     tuple.DesiredTuple = CISTPL_CONFIG;
  183.     tuple.TupleData = tuple_data;
  184.     tuple.TupleDataMax = 64;
  185.     tuple.TupleOffset = 0;
  186.     CS_CHECK(GetFirstTuple, handle, &tuple);
  187.     CS_CHECK(GetTupleData, handle, &tuple);
  188.     CS_CHECK(ParseTuple, handle, &tuple, &parse);
  189.     link->conf.ConfigBase = parse.config.base;
  190.     /* Configure card */
  191.     driver_template.module = &__this_module;
  192.     link->state |= DEV_CONFIG;
  193.     
  194.     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  195.     CS_CHECK(GetFirstTuple, handle, &tuple);
  196.     while (1) {
  197. CFG_CHECK(GetTupleData, handle, &tuple);
  198. CFG_CHECK(ParseTuple, handle, &tuple, &parse);
  199. link->conf.ConfigIndex = parse.cftable_entry.index;
  200. link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
  201. i = CardServices(RequestIO, handle, &link->io);
  202. if (i == CS_SUCCESS) break;
  203.     next_entry:
  204. CS_CHECK(GetNextTuple, handle, &tuple);
  205.     }
  206.     CS_CHECK(RequestIRQ, handle, &link->irq);
  207.     CS_CHECK(RequestConfiguration, handle, &link->conf);
  208.     
  209.     /* A bad hack... */
  210.     release_region(link->io.BasePort1, link->io.NumPorts1);
  211.     /* Set configuration options for the fdomain driver */
  212.     ints[0] = 2;
  213.     ints[1] = link->io.BasePort1;
  214.     ints[2] = link->irq.AssignedIRQ;
  215.     sprintf(str, "%d,%d", link->io.BasePort1, link->irq.AssignedIRQ);
  216.     fdomain_setup(str, ints);
  217.     
  218.     scsi_register_module(MODULE_SCSI_HA, &driver_template);
  219.     tail = &link->dev;
  220.     info->ndev = 0;
  221.     for (host = scsi_hostlist; host; host = host->next)
  222. if (host->hostt == &driver_template)
  223.     for (dev = host->host_queue; dev; dev = dev->next) {
  224.     u_long arg[2], id;
  225.     kernel_scsi_ioctl(dev, SCSI_IOCTL_GET_IDLUN, arg);
  226.     id = (arg[0]&0x0f) + ((arg[0]>>4)&0xf0) +
  227. ((arg[0]>>8)&0xf00) + ((arg[0]>>12)&0xf000);
  228.     node = &info->node[info->ndev];
  229.     node->minor = 0;
  230.     switch (dev->type) {
  231.     case TYPE_TAPE:
  232. node->major = SCSI_TAPE_MAJOR;
  233. sprintf(node->dev_name, "st#%04lx", id);
  234. break;
  235.     case TYPE_DISK:
  236.     case TYPE_MOD:
  237. node->major = SCSI_DISK0_MAJOR;
  238. sprintf(node->dev_name, "sd#%04lx", id);
  239. break;
  240.     case TYPE_ROM:
  241.     case TYPE_WORM:
  242. node->major = SCSI_CDROM_MAJOR;
  243. sprintf(node->dev_name, "sr#%04lx", id);
  244. break;
  245.     default:
  246. node->major = SCSI_GENERIC_MAJOR;
  247. sprintf(node->dev_name, "sg#%04lx", id);
  248. break;
  249.     }
  250.     *tail = node; tail = &node->next;
  251.     info->ndev++;
  252. }
  253.     *tail = NULL;
  254.     if (info->ndev == 0)
  255. printk(KERN_INFO "fdomain_cs: no SCSI devices foundn");
  256.     
  257.     link->state &= ~DEV_CONFIG_PENDING;
  258.     return;
  259.     
  260. cs_failed:
  261.     cs_error(link->handle, last_fn, last_ret);
  262.     fdomain_release((u_long)link);
  263.     return;
  264.     
  265. } /* fdomain_config */
  266. /*====================================================================*/
  267. static void fdomain_release(u_long arg)
  268. {
  269.     dev_link_t *link = (dev_link_t *)arg;
  270.     DEBUG(0, "fdomain_release(0x%p)n", link);
  271.     if (GET_USE_COUNT(&__this_module) != 0) {
  272. DEBUG(1, "fdomain_cs: release postponed, "
  273.       "device still openn");
  274. link->state |= DEV_STALE_CONFIG;
  275. return;
  276.     }
  277.     scsi_unregister_module(MODULE_SCSI_HA, &driver_template);
  278.     link->dev = NULL;
  279.     
  280.     CardServices(ReleaseConfiguration, link->handle);
  281.     CardServices(ReleaseIO, link->handle, &link->io);
  282.     CardServices(ReleaseIRQ, link->handle, &link->irq);
  283.     
  284.     link->state &= ~DEV_CONFIG;
  285.     if (link->state & DEV_STALE_LINK)
  286. fdomain_detach(link);
  287.     
  288. } /* fdomain_release */
  289. /*====================================================================*/
  290. static int fdomain_event(event_t event, int priority,
  291. event_callback_args_t *args)
  292. {
  293.     dev_link_t *link = args->client_data;
  294.     DEBUG(1, "fdomain_event(0x%06x)n", event);
  295.     
  296.     switch (event) {
  297.     case CS_EVENT_CARD_REMOVAL:
  298. link->state &= ~DEV_PRESENT;
  299. if (link->state & DEV_CONFIG)
  300.     mod_timer(&link->release, jiffies + HZ/20);
  301. break;
  302.     case CS_EVENT_CARD_INSERTION:
  303. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  304. fdomain_config(link);
  305. break;
  306.     case CS_EVENT_PM_SUSPEND:
  307. link->state |= DEV_SUSPEND;
  308. /* Fall through... */
  309.     case CS_EVENT_RESET_PHYSICAL:
  310. if (link->state & DEV_CONFIG)
  311.     CardServices(ReleaseConfiguration, link->handle);
  312. break;
  313.     case CS_EVENT_PM_RESUME:
  314. link->state &= ~DEV_SUSPEND;
  315. /* Fall through... */
  316.     case CS_EVENT_CARD_RESET:
  317. if (link->state & DEV_CONFIG) {
  318.     CardServices(RequestConfiguration, link->handle, &link->conf);
  319.     fdomain_16x0_reset(NULL, 0);
  320. }
  321. break;
  322.     }
  323.     return 0;
  324. } /* fdomain_event */
  325. /*====================================================================*/
  326. static int __init init_fdomain_cs(void) {
  327.     servinfo_t serv;
  328.     DEBUG(0, "%sn", version);
  329.     CardServices(GetCardServicesInfo, &serv);
  330.     if (serv.Revision != CS_RELEASE_CODE) {
  331. printk(KERN_NOTICE "fdomain_cs: Card Services release "
  332.        "does not match!n");
  333. return -1;
  334.     }
  335.     register_pccard_driver(&dev_info, &fdomain_attach, &fdomain_detach);
  336.     return 0;
  337. }
  338. static void __exit exit_fdomain_cs(void) {
  339.     DEBUG(0, "fdomain_cs: unloadingn");
  340.     unregister_pccard_driver(&dev_info);
  341.     while (dev_list != NULL)
  342. fdomain_detach(dev_list);
  343. }
  344. module_init(init_fdomain_cs);
  345. module_exit(exit_fdomain_cs);