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.54 2000/06/12 21:27:25
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 which
26 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/blkdev.h>
47 #include <scsi/scsi_ioctl.h>
50 #include <scsi/scsi_host.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>
60 static int pc_debug = PCMCIA_DEBUG;
61 MODULE_PARM(pc_debug, "i");
62 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
63 static char *version =
64 "aha152x_cs.c 1.54 2000/06/12 21:27:25 (David Hinds)";
66 #define DEBUG(n, args...)
69 /*====================================================================*/
71 /* Parameters that can be set with 'insmod' */
73 /* Bit map of interrupts to choose from */
74 static u_int irq_mask = 0xdeb8;
75 static int irq_list[4] = { -1 };
77 /* SCSI bus setup options */
78 static int host_id = 7;
79 static int reconnect = 1;
80 static int parity = 1;
81 static int synchronous = 1;
82 static int reset_delay = 100;
83 static int ext_trans = 0;
85 MODULE_PARM(irq_mask, "i");
86 MODULE_PARM(irq_list, "1-4i");
87 MODULE_PARM(host_id, "i");
88 MODULE_PARM(reconnect, "i");
89 MODULE_PARM(parity, "i");
90 MODULE_PARM(synchronous, "i");
91 MODULE_PARM(reset_delay, "i");
92 MODULE_PARM(ext_trans, "i");
94 MODULE_LICENSE("Dual MPL/GPL");
96 /*====================================================================*/
98 typedef struct scsi_info_t {
101 struct Scsi_Host *host;
104 static void aha152x_release_cs(dev_link_t *link);
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 dev_link_t *dev_list;
112 static dev_info_t dev_info = "aha152x_cs";
114 static dev_link_t *aha152x_attach(void)
117 client_reg_t client_reg;
121 DEBUG(0, "aha152x_attach()\n");
123 /* Create new SCSI device */
124 info = kmalloc(sizeof(*info), GFP_KERNEL);
125 if (!info) return NULL;
126 memset(info, 0, sizeof(*info));
127 link = &info->link; link->priv = info;
129 link->io.NumPorts1 = 0x20;
130 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
131 link->io.IOAddrLines = 10;
132 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
133 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
134 if (irq_list[0] == -1)
135 link->irq.IRQInfo2 = irq_mask;
137 for (i = 0; i < 4; i++)
138 link->irq.IRQInfo2 |= 1 << irq_list[i];
139 link->conf.Attributes = CONF_ENABLE_IRQ;
141 link->conf.IntType = INT_MEMORY_AND_IO;
142 link->conf.Present = PRESENT_OPTION;
144 /* Register with Card Services */
145 link->next = dev_list;
147 client_reg.dev_info = &dev_info;
148 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
149 client_reg.event_handler = &aha152x_event;
150 client_reg.EventMask =
151 CS_EVENT_RESET_REQUEST | CS_EVENT_CARD_RESET |
152 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
153 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
154 client_reg.Version = 0x0210;
155 client_reg.event_callback_args.client_data = link;
156 ret = pcmcia_register_client(&link->handle, &client_reg);
158 cs_error(link->handle, RegisterClient, ret);
159 aha152x_detach(link);
164 } /* aha152x_attach */
166 /*====================================================================*/
168 static void aha152x_detach(dev_link_t *link)
172 DEBUG(0, "aha152x_detach(0x%p)\n", link);
174 /* Locate device structure */
175 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
176 if (*linkp == link) break;
180 if (link->state & DEV_CONFIG)
181 aha152x_release_cs(link);
184 pcmcia_deregister_client(link->handle);
186 /* Unlink device structure, free bits */
190 } /* aha152x_detach */
192 /*====================================================================*/
194 #define CS_CHECK(fn, ret) \
195 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
197 static void aha152x_config_cs(dev_link_t *link)
199 client_handle_t handle = link->handle;
200 scsi_info_t *info = link->priv;
201 struct aha152x_setup s;
204 int i, last_ret, last_fn;
205 u_char tuple_data[64];
206 struct Scsi_Host *host;
208 DEBUG(0, "aha152x_config(0x%p)\n", link);
210 tuple.DesiredTuple = CISTPL_CONFIG;
211 tuple.TupleData = tuple_data;
212 tuple.TupleDataMax = 64;
213 tuple.TupleOffset = 0;
214 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
215 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
216 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
217 link->conf.ConfigBase = parse.config.base;
220 link->state |= DEV_CONFIG;
222 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
223 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
225 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
226 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
228 /* For New Media T&J, look for a SCSI window */
229 if (parse.cftable_entry.io.win[0].len >= 0x20)
230 link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
231 else if ((parse.cftable_entry.io.nwin > 1) &&
232 (parse.cftable_entry.io.win[1].len >= 0x20))
233 link->io.BasePort1 = parse.cftable_entry.io.win[1].base;
234 if ((parse.cftable_entry.io.nwin > 0) &&
235 (link->io.BasePort1 < 0xffff)) {
236 link->conf.ConfigIndex = parse.cftable_entry.index;
237 i = pcmcia_request_io(handle, &link->io);
238 if (i == CS_SUCCESS) break;
241 CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
244 CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
245 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
247 /* Set configuration options for the aha152x driver */
248 memset(&s, 0, sizeof(s));
249 s.conf = "PCMCIA setup";
250 s.io_port = link->io.BasePort1;
251 s.irq = link->irq.AssignedIRQ;
253 s.reconnect = reconnect;
255 s.synchronous = synchronous;
256 s.delay = reset_delay;
258 s.ext_trans = ext_trans;
260 host = aha152x_probe_one(&s);
262 printk(KERN_INFO "aha152x_cs: no SCSI devices found\n");
266 sprintf(info->node.dev_name, "scsi%d", host->host_no);
267 link->dev = &info->node;
270 link->state &= ~DEV_CONFIG_PENDING;
274 cs_error(link->handle, last_fn, last_ret);
275 aha152x_release_cs(link);
279 static void aha152x_release_cs(dev_link_t *link)
281 scsi_info_t *info = link->priv;
283 aha152x_release(info->host);
286 pcmcia_release_configuration(link->handle);
287 pcmcia_release_io(link->handle, &link->io);
288 pcmcia_release_irq(link->handle, &link->irq);
290 link->state &= ~DEV_CONFIG;
293 static int aha152x_event(event_t event, int priority,
294 event_callback_args_t *args)
296 dev_link_t *link = args->client_data;
297 scsi_info_t *info = link->priv;
299 DEBUG(0, "aha152x_event(0x%06x)\n", event);
302 case CS_EVENT_CARD_REMOVAL:
303 link->state &= ~DEV_PRESENT;
304 if (link->state & DEV_CONFIG)
305 aha152x_release_cs(link);
307 case CS_EVENT_CARD_INSERTION:
308 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
309 aha152x_config_cs(link);
311 case CS_EVENT_PM_SUSPEND:
312 link->state |= DEV_SUSPEND;
313 /* Fall through... */
314 case CS_EVENT_RESET_PHYSICAL:
315 if (link->state & DEV_CONFIG)
316 pcmcia_release_configuration(link->handle);
318 case CS_EVENT_PM_RESUME:
319 link->state &= ~DEV_SUSPEND;
320 /* Fall through... */
321 case CS_EVENT_CARD_RESET:
322 if (link->state & DEV_CONFIG) {
324 pcmcia_request_configuration(link->handle, &link->conf);
325 tmp.device->host = info->host;
326 aha152x_host_reset(&tmp);
333 static struct pcmcia_driver aha152x_cs_driver = {
334 .owner = THIS_MODULE,
336 .name = "aha152x_cs",
338 .attach = aha152x_attach,
339 .detach = aha152x_detach,
342 static int __init init_aha152x_cs(void)
344 return pcmcia_register_driver(&aha152x_cs_driver);
347 static void __exit exit_aha152x_cs(void)
349 pcmcia_unregister_driver(&aha152x_cs_driver);
351 /* XXX: this really needs to move into generic code.. */
352 while (dev_list != NULL)
353 aha152x_detach(dev_list);
356 module_init(init_aha152x_cs);
357 module_exit(exit_aha152x_cs);