ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / pcmcia / aha152x_stub.c
1 /*======================================================================
2
3     A driver for Adaptec AHA152X-compatible PCMCIA SCSI cards.
4
5     This driver supports the Adaptec AHA-1460, the New Media Bus
6     Toaster, and the New Media Toast & Jam.
7     
8     aha152x_cs.c 1.54 2000/06/12 21:27:25
9
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/
14
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.
19
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.
23
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.
34     
35 ======================================================================*/
36
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>
48
49 #include "scsi.h"
50 #include "hosts.h"
51 #include "aha152x.h"
52
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>
58
59 #ifdef PCMCIA_DEBUG
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)";
65 #else
66 #define DEBUG(n, args...)
67 #endif
68
69 /*====================================================================*/
70
71 /* Parameters that can be set with 'insmod' */
72
73 /* Bit map of interrupts to choose from */
74 static u_int irq_mask = 0xdeb8;
75 static int irq_list[4] = { -1 };
76
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;
84
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");
93
94 MODULE_LICENSE("Dual MPL/GPL");
95
96 /*====================================================================*/
97
98 typedef struct scsi_info_t {
99     dev_link_t          link;
100     dev_node_t          node;
101     struct Scsi_Host    *host;
102 } scsi_info_t;
103
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);
107
108 static dev_link_t *aha152x_attach(void);
109 static void aha152x_detach(dev_link_t *);
110
111 static dev_link_t *dev_list;
112 static dev_info_t dev_info = "aha152x_cs";
113
114 static dev_link_t *aha152x_attach(void)
115 {
116     scsi_info_t *info;
117     client_reg_t client_reg;
118     dev_link_t *link;
119     int i, ret;
120     
121     DEBUG(0, "aha152x_attach()\n");
122
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;
128
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;
136     else
137         for (i = 0; i < 4; i++)
138             link->irq.IRQInfo2 |= 1 << irq_list[i];
139     link->conf.Attributes = CONF_ENABLE_IRQ;
140     link->conf.Vcc = 50;
141     link->conf.IntType = INT_MEMORY_AND_IO;
142     link->conf.Present = PRESENT_OPTION;
143
144     /* Register with Card Services */
145     link->next = dev_list;
146     dev_list = link;
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);
157     if (ret != 0) {
158         cs_error(link->handle, RegisterClient, ret);
159         aha152x_detach(link);
160         return NULL;
161     }
162     
163     return link;
164 } /* aha152x_attach */
165
166 /*====================================================================*/
167
168 static void aha152x_detach(dev_link_t *link)
169 {
170     dev_link_t **linkp;
171
172     DEBUG(0, "aha152x_detach(0x%p)\n", link);
173     
174     /* Locate device structure */
175     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
176         if (*linkp == link) break;
177     if (*linkp == NULL)
178         return;
179
180     if (link->state & DEV_CONFIG)
181         aha152x_release_cs(link);
182
183     if (link->handle)
184         pcmcia_deregister_client(link->handle);
185     
186     /* Unlink device structure, free bits */
187     *linkp = link->next;
188     kfree(link->priv);
189     
190 } /* aha152x_detach */
191
192 /*====================================================================*/
193
194 #define CS_CHECK(fn, ret) \
195 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
196
197 static void aha152x_config_cs(dev_link_t *link)
198 {
199     client_handle_t handle = link->handle;
200     scsi_info_t *info = link->priv;
201     struct aha152x_setup s;
202     tuple_t tuple;
203     cisparse_t parse;
204     int i, last_ret, last_fn;
205     u_char tuple_data[64];
206     struct Scsi_Host *host;
207     
208     DEBUG(0, "aha152x_config(0x%p)\n", link);
209
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;
218
219     /* Configure card */
220     link->state |= DEV_CONFIG;
221
222     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
223     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
224     while (1) {
225         if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
226                 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
227             goto next_entry;
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;
239         }
240     next_entry:
241         CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
242     }
243     
244     CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
245     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
246     
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;
252     s.scsiid      = host_id;
253     s.reconnect   = reconnect;
254     s.parity      = parity;
255     s.synchronous = synchronous;
256     s.delay       = reset_delay;
257     if (ext_trans)
258         s.ext_trans = ext_trans;
259
260     host = aha152x_probe_one(&s);
261     if (host == NULL) {
262         printk(KERN_INFO "aha152x_cs: no SCSI devices found\n");
263         goto cs_failed;
264     }
265
266     sprintf(info->node.dev_name, "scsi%d", host->host_no);
267     link->dev = &info->node;
268     info->host = host;
269
270     link->state &= ~DEV_CONFIG_PENDING;
271     return;
272     
273 cs_failed:
274     cs_error(link->handle, last_fn, last_ret);
275     aha152x_release_cs(link);
276     return;
277 }
278
279 static void aha152x_release_cs(dev_link_t *link)
280 {
281         scsi_info_t *info = link->priv;
282
283         aha152x_release(info->host);
284         link->dev = NULL;
285     
286         pcmcia_release_configuration(link->handle);
287         pcmcia_release_io(link->handle, &link->io);
288         pcmcia_release_irq(link->handle, &link->irq);
289     
290         link->state &= ~DEV_CONFIG;
291 }
292
293 static int aha152x_event(event_t event, int priority,
294                          event_callback_args_t *args)
295 {
296     dev_link_t *link = args->client_data;
297     scsi_info_t *info = link->priv;
298     
299     DEBUG(0, "aha152x_event(0x%06x)\n", event);
300     
301     switch (event) {
302     case CS_EVENT_CARD_REMOVAL:
303         link->state &= ~DEV_PRESENT;
304         if (link->state & DEV_CONFIG)
305             aha152x_release_cs(link);
306         break;
307     case CS_EVENT_CARD_INSERTION:
308         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
309         aha152x_config_cs(link);
310         break;
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);
317         break;
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) {
323             Scsi_Cmnd tmp;
324             pcmcia_request_configuration(link->handle, &link->conf);
325             tmp.device->host = info->host;
326             aha152x_host_reset(&tmp);
327         }
328         break;
329     }
330     return 0;
331 }
332
333 static struct pcmcia_driver aha152x_cs_driver = {
334         .owner          = THIS_MODULE,
335         .drv            = {
336                 .name   = "aha152x_cs",
337         },
338         .attach         = aha152x_attach,
339         .detach         = aha152x_detach,
340 };
341
342 static int __init init_aha152x_cs(void)
343 {
344         return pcmcia_register_driver(&aha152x_cs_driver);
345 }
346
347 static void __exit exit_aha152x_cs(void)
348 {
349         pcmcia_unregister_driver(&aha152x_cs_driver);
350
351         /* XXX: this really needs to move into generic code.. */
352         while (dev_list != NULL)
353                 aha152x_detach(dev_list);
354 }
355
356 module_init(init_aha152x_cs);
357 module_exit(exit_aha152x_cs);
358