1 /*======================================================================
3 A driver for Adaptec AHA152X-compatible PCMCIA SCSI cards.
5 This driver supports the Adaptec AHA-1460, the New Media Bus
6 Toaster, and the New Media Toast & Jam.
8 aha152x_cs.c 1.58 2001/10/13 00:08:51
10 The contents of this file are subject to the Mozilla Public
11 License Version 1.1 (the "License"); you may not use this file
12 except in compliance with the License. You may obtain a copy of
13 the License at http://www.mozilla.org/MPL/
15 Software distributed under the License is distributed on an "AS
16 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 implied. See the License for the specific language governing
18 rights and limitations under the License.
20 The initial developer of the original code is David A. Hinds
21 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
22 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the
25 terms of the GNU General Public License version 2 (the "GPL"), in
26 which case the provisions of the GPL are applicable instead of the
27 above. If you wish to allow the use of your version of this file
28 only under the terms of the GPL and not to allow others to use
29 your version of this file under the MPL, indicate your decision
30 by deleting the provisions above and replace them with the notice
31 and other provisions required by the GPL. If you do not delete
32 the provisions above, a recipient may use your version of this
33 file under either the MPL or the GPL.
35 ======================================================================*/
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/string.h>
43 #include <linux/ioport.h>
44 #include <scsi/scsi.h>
45 #include <linux/major.h>
46 #include <linux/blk.h>
48 #include <../drivers/scsi/scsi.h>
49 #include <../drivers/scsi/hosts.h>
50 #include <scsi/scsi_ioctl.h>
51 #include <../drivers/scsi/aha152x.h>
53 #include <pcmcia/version.h>
54 #include <pcmcia/cs_types.h>
55 #include <pcmcia/cs.h>
56 #include <pcmcia/cistpl.h>
57 #include <pcmcia/ds.h>
59 /*====================================================================*/
61 /* Module parameters */
63 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
64 MODULE_DESCRIPTION("Adaptec AHA152x-compatible PCMCIA SCSI driver");
65 MODULE_LICENSE("Dual MPL/GPL");
67 static int irq_list[4] = { -1 };
68 MODULE_PARM(irq_list, "1-4i");
70 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
72 INT_MODULE_PARM(irq_mask, 0xdeb8);
73 INT_MODULE_PARM(host_id, 7);
74 INT_MODULE_PARM(reconnect, 1);
75 INT_MODULE_PARM(parity, 1);
76 INT_MODULE_PARM(synchronous, 0);
77 INT_MODULE_PARM(reset_delay, 100);
78 INT_MODULE_PARM(ext_trans, 0);
81 INT_MODULE_PARM(debug, 0);
85 INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
86 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
87 static char *version =
88 "aha152x_cs.c 1.58 2001/10/13 00:08:51 (David Hinds)";
90 #define DEBUG(n, args...)
93 /*====================================================================*/
95 typedef struct scsi_info_t {
97 struct Scsi_Host *host;
102 extern void aha152x_setup(char *str, int *ints);
104 static void aha152x_release_cs(u_long arg);
105 static int aha152x_event(event_t event, int priority,
106 event_callback_args_t *args);
108 static dev_link_t *aha152x_attach(void);
109 static void aha152x_detach(dev_link_t *);
111 static Scsi_Host_Template driver_template = AHA152X;
113 static dev_link_t *dev_list = NULL;
115 static dev_info_t dev_info = "aha152x_cs";
117 /*====================================================================*/
119 static void cs_error(client_handle_t handle, int func, int ret)
121 error_info_t err = { func, ret };
122 CardServices(ReportError, handle, &err);
125 /*====================================================================*/
127 static dev_link_t *aha152x_attach(void)
130 client_reg_t client_reg;
134 DEBUG(0, "aha152x_attach()\n");
136 /* Create new SCSI device */
137 info = kmalloc(sizeof(*info), GFP_KERNEL);
138 if (!info) return NULL;
139 memset(info, 0, sizeof(*info));
140 link = &info->link; link->priv = info;
142 link->io.NumPorts1 = 0x20;
143 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
144 link->io.IOAddrLines = 10;
145 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
146 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
147 if (irq_list[0] == -1)
148 link->irq.IRQInfo2 = irq_mask;
150 for (i = 0; i < 4; i++)
151 link->irq.IRQInfo2 |= 1 << irq_list[i];
152 link->conf.Attributes = CONF_ENABLE_IRQ;
154 link->conf.IntType = INT_MEMORY_AND_IO;
155 link->conf.Present = PRESENT_OPTION;
157 /* Register with Card Services */
158 link->next = dev_list;
160 client_reg.dev_info = &dev_info;
161 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
162 client_reg.event_handler = &aha152x_event;
163 client_reg.EventMask =
164 CS_EVENT_RESET_REQUEST | CS_EVENT_CARD_RESET |
165 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
166 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
167 client_reg.Version = 0x0210;
168 client_reg.event_callback_args.client_data = link;
169 ret = CardServices(RegisterClient, &link->handle, &client_reg);
171 cs_error(link->handle, RegisterClient, ret);
172 aha152x_detach(link);
177 } /* aha152x_attach */
179 /*====================================================================*/
181 static void aha152x_detach(dev_link_t *link)
185 DEBUG(0, "aha152x_detach(0x%p)\n", link);
187 /* Locate device structure */
188 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
189 if (*linkp == link) break;
193 if (link->state & DEV_CONFIG) {
194 aha152x_release_cs((u_long)link);
195 if (link->state & DEV_STALE_CONFIG) {
196 link->state |= DEV_STALE_LINK;
202 CardServices(DeregisterClient, link->handle);
204 /* Unlink device structure, free bits */
208 } /* aha152x_detach */
210 /*====================================================================*/
212 #define CS_CHECK(fn, args...) \
213 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
215 #define CFG_CHECK(fn, args...) \
216 if (CardServices(fn, args) != 0) goto next_entry
218 static void aha152x_config_cs(dev_link_t *link)
220 client_handle_t handle = link->handle;
221 scsi_info_t *info = link->priv;
224 int i, last_ret, last_fn, ints[8];
225 u_char tuple_data[64];
227 dev_node_t *node, **tail;
228 struct Scsi_Host *host;
230 DEBUG(0, "aha152x_config(0x%p)\n", link);
232 tuple.DesiredTuple = CISTPL_CONFIG;
233 tuple.TupleData = tuple_data;
234 tuple.TupleDataMax = 64;
235 tuple.TupleOffset = 0;
236 CS_CHECK(GetFirstTuple, handle, &tuple);
237 CS_CHECK(GetTupleData, handle, &tuple);
238 CS_CHECK(ParseTuple, handle, &tuple, &parse);
239 link->conf.ConfigBase = parse.config.base;
242 driver_template.module = &__this_module;
243 link->state |= DEV_CONFIG;
245 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
246 CS_CHECK(GetFirstTuple, handle, &tuple);
248 CFG_CHECK(GetTupleData, handle, &tuple);
249 CFG_CHECK(ParseTuple, handle, &tuple, &parse);
250 /* For New Media T&J, look for a SCSI window */
251 if (parse.cftable_entry.io.win[0].len >= 0x20)
252 link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
253 else if ((parse.cftable_entry.io.nwin > 1) &&
254 (parse.cftable_entry.io.win[1].len >= 0x20))
255 link->io.BasePort1 = parse.cftable_entry.io.win[1].base;
256 if ((parse.cftable_entry.io.nwin > 0) &&
257 (link->io.BasePort1 < 0xffff)) {
258 link->conf.ConfigIndex = parse.cftable_entry.index;
259 i = CardServices(RequestIO, handle, &link->io);
260 if (i == CS_SUCCESS) break;
263 CS_CHECK(GetNextTuple, handle, &tuple);
266 CS_CHECK(RequestIRQ, handle, &link->irq);
267 CS_CHECK(RequestConfiguration, handle, &link->conf);
270 release_region(link->io.BasePort1, link->io.NumPorts1);
272 /* Set configuration options for the aha152x driver */
273 ints[1] = link->io.BasePort1;
274 ints[2] = link->irq.AssignedIRQ;
278 ints[6] = synchronous;
279 ints[7] = reset_delay;
287 aha152x_setup("PCMCIA setup", ints);
289 scsi_register_module(MODULE_SCSI_HA, &driver_template);
293 for (host = scsi_hostlist; host; host = host->next)
294 if (host->hostt == &driver_template)
295 for (dev = host->host_queue; dev; dev = dev->next) {
297 kernel_scsi_ioctl(dev, SCSI_IOCTL_GET_IDLUN, arg);
298 id = (arg[0]&0x0f) + ((arg[0]>>4)&0xf0) +
299 ((arg[0]>>8)&0xf00) + ((arg[0]>>12)&0xf000);
300 node = &info->node[info->ndev];
304 node->major = SCSI_TAPE_MAJOR;
305 sprintf(node->dev_name, "st#%04lx", id);
309 node->major = SCSI_DISK0_MAJOR;
310 sprintf(node->dev_name, "sd#%04lx", id);
314 node->major = SCSI_CDROM_MAJOR;
315 sprintf(node->dev_name, "sr#%04lx", id);
318 node->major = SCSI_GENERIC_MAJOR;
319 sprintf(node->dev_name, "sg#%04lx", id);
322 *tail = node; tail = &node->next;
324 info->host = dev->host;
328 printk(KERN_INFO "aha152x_cs: no SCSI devices found\n");
330 link->state &= ~DEV_CONFIG_PENDING;
334 cs_error(link->handle, last_fn, last_ret);
335 aha152x_release_cs((u_long)link);
338 } /* aha152x_config_cs */
340 /*====================================================================*/
342 static void aha152x_release_cs(u_long arg)
344 dev_link_t *link = (dev_link_t *)arg;
346 DEBUG(0, "aha152x_release_cs(0x%p)\n", link);
348 if (GET_USE_COUNT(driver_template.module) != 0) {
349 DEBUG(1, "aha152x_cs: release postponed, "
350 "device still open\n");
351 link->state |= DEV_STALE_CONFIG;
355 scsi_unregister_module(MODULE_SCSI_HA, &driver_template);
358 CardServices(ReleaseConfiguration, link->handle);
359 CardServices(ReleaseIO, link->handle, &link->io);
360 CardServices(ReleaseIRQ, link->handle, &link->irq);
362 link->state &= ~DEV_CONFIG;
363 if (link->state & DEV_STALE_LINK)
364 aha152x_detach(link);
366 } /* aha152x_release_cs */
368 /*====================================================================*/
370 static int aha152x_event(event_t event, int priority,
371 event_callback_args_t *args)
373 dev_link_t *link = args->client_data;
374 scsi_info_t *info = link->priv;
376 DEBUG(0, "aha152x_event(0x%06x)\n", event);
379 case CS_EVENT_CARD_REMOVAL:
380 link->state &= ~DEV_PRESENT;
381 if (link->state & DEV_CONFIG)
382 aha152x_release_cs((u_long)link);
384 case CS_EVENT_CARD_INSERTION:
385 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
386 aha152x_config_cs(link);
388 case CS_EVENT_PM_SUSPEND:
389 link->state |= DEV_SUSPEND;
390 /* Fall through... */
391 case CS_EVENT_RESET_PHYSICAL:
392 if (link->state & DEV_CONFIG)
393 CardServices(ReleaseConfiguration, link->handle);
395 case CS_EVENT_PM_RESUME:
396 link->state &= ~DEV_SUSPEND;
397 /* Fall through... */
398 case CS_EVENT_CARD_RESET:
399 if (link->state & DEV_CONFIG) {
401 CardServices(RequestConfiguration, link->handle, &link->conf);
402 tmp.host = info->host;
403 aha152x_host_reset(&tmp);
408 } /* aha152x_event */
410 /*====================================================================*/
412 static int __init init_aha152x_cs(void) {
414 DEBUG(0, "%s\n", version);
415 CardServices(GetCardServicesInfo, &serv);
416 if (serv.Revision != CS_RELEASE_CODE) {
417 printk(KERN_NOTICE "aha152x_cs: Card Services release "
418 "does not match!\n");
421 register_pccard_driver(&dev_info, &aha152x_attach, &aha152x_detach);
425 static void __exit exit_aha152x_cs(void) {
426 DEBUG(0, "aha152x_cs: unloading\n");
427 unregister_pccard_driver(&dev_info);
428 while (dev_list != NULL)
429 aha152x_detach(dev_list);
432 module_init(init_aha152x_cs);
433 module_exit(exit_aha152x_cs);