more changes on original files
[linux-2.4.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.58 2001/10/13 00:08:51
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
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.
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/blk.h>
47
48 #include <../drivers/scsi/scsi.h>
49 #include <../drivers/scsi/hosts.h>
50 #include <scsi/scsi_ioctl.h>
51 #include <../drivers/scsi/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 /*====================================================================*/
60
61 /* Module parameters */
62
63 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
64 MODULE_DESCRIPTION("Adaptec AHA152x-compatible PCMCIA SCSI driver");
65 MODULE_LICENSE("Dual MPL/GPL");
66
67 static int irq_list[4] = { -1 };
68 MODULE_PARM(irq_list, "1-4i");
69
70 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
71
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);
79
80 #ifdef AHA152X_DEBUG
81 INT_MODULE_PARM(debug,          0);
82 #endif
83
84 #ifdef PCMCIA_DEBUG
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)";
89 #else
90 #define DEBUG(n, args...)
91 #endif
92
93 /*====================================================================*/
94
95 typedef struct scsi_info_t {
96     dev_link_t          link;
97     struct Scsi_Host    *host;
98     int                 ndev;
99     dev_node_t          node[8];
100 } scsi_info_t;
101
102 extern void aha152x_setup(char *str, int *ints);
103
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);
107
108 static dev_link_t *aha152x_attach(void);
109 static void aha152x_detach(dev_link_t *);
110
111 static Scsi_Host_Template driver_template = AHA152X;
112
113 static dev_link_t *dev_list = NULL;
114
115 static dev_info_t dev_info = "aha152x_cs";
116
117 /*====================================================================*/
118
119 static void cs_error(client_handle_t handle, int func, int ret)
120 {
121     error_info_t err = { func, ret };
122     CardServices(ReportError, handle, &err);
123 }
124
125 /*====================================================================*/
126
127 static dev_link_t *aha152x_attach(void)
128 {
129     scsi_info_t *info;
130     client_reg_t client_reg;
131     dev_link_t *link;
132     int i, ret;
133     
134     DEBUG(0, "aha152x_attach()\n");
135
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;
141
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;
149     else
150         for (i = 0; i < 4; i++)
151             link->irq.IRQInfo2 |= 1 << irq_list[i];
152     link->conf.Attributes = CONF_ENABLE_IRQ;
153     link->conf.Vcc = 50;
154     link->conf.IntType = INT_MEMORY_AND_IO;
155     link->conf.Present = PRESENT_OPTION;
156
157     /* Register with Card Services */
158     link->next = dev_list;
159     dev_list = link;
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);
170     if (ret != 0) {
171         cs_error(link->handle, RegisterClient, ret);
172         aha152x_detach(link);
173         return NULL;
174     }
175     
176     return link;
177 } /* aha152x_attach */
178
179 /*====================================================================*/
180
181 static void aha152x_detach(dev_link_t *link)
182 {
183     dev_link_t **linkp;
184
185     DEBUG(0, "aha152x_detach(0x%p)\n", link);
186     
187     /* Locate device structure */
188     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
189         if (*linkp == link) break;
190     if (*linkp == NULL)
191         return;
192
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;
197             return;
198         }
199     }
200
201     if (link->handle)
202         CardServices(DeregisterClient, link->handle);
203     
204     /* Unlink device structure, free bits */
205     *linkp = link->next;
206     kfree(link->priv);
207     
208 } /* aha152x_detach */
209
210 /*====================================================================*/
211
212 #define CS_CHECK(fn, args...) \
213 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
214
215 #define CFG_CHECK(fn, args...) \
216 if (CardServices(fn, args) != 0) goto next_entry
217
218 static void aha152x_config_cs(dev_link_t *link)
219 {
220     client_handle_t handle = link->handle;
221     scsi_info_t *info = link->priv;
222     tuple_t tuple;
223     cisparse_t parse;
224     int i, last_ret, last_fn, ints[8];
225     u_char tuple_data[64];
226     Scsi_Device *dev;
227     dev_node_t *node, **tail;
228     struct Scsi_Host *host;
229     
230     DEBUG(0, "aha152x_config(0x%p)\n", link);
231
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;
240
241     /* Configure card */
242     driver_template.module = &__this_module;
243     link->state |= DEV_CONFIG;
244
245     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
246     CS_CHECK(GetFirstTuple, handle, &tuple);
247     while (1) {
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;
261         }
262     next_entry:
263         CS_CHECK(GetNextTuple, handle, &tuple);
264     }
265     
266     CS_CHECK(RequestIRQ, handle, &link->irq);
267     CS_CHECK(RequestConfiguration, handle, &link->conf);
268     
269     /* A bad hack... */
270     release_region(link->io.BasePort1, link->io.NumPorts1);
271     
272     /* Set configuration options for the aha152x driver */
273     ints[1] = link->io.BasePort1;
274     ints[2] = link->irq.AssignedIRQ;
275     ints[3] = host_id;
276     ints[4] = reconnect;
277     ints[5] = parity;
278     ints[6] = synchronous;
279     ints[7] = reset_delay;
280     ints[8] = ext_trans;
281 #ifdef AHA152X_DEBUG
282     ints[9] = debug;
283     ints[0] = 9;
284 #else
285     ints[0] = 8;
286 #endif
287     aha152x_setup("PCMCIA setup", ints);
288     
289     scsi_register_module(MODULE_SCSI_HA, &driver_template);
290
291     tail = &link->dev;
292     info->ndev = 0;
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) {
296             u_long arg[2], id;
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];
301             node->minor = 0;
302             switch (dev->type) {
303             case TYPE_TAPE:
304                 node->major = SCSI_TAPE_MAJOR;
305                 sprintf(node->dev_name, "st#%04lx", id);
306                 break;
307             case TYPE_DISK:
308             case TYPE_MOD:
309                 node->major = SCSI_DISK0_MAJOR;
310                 sprintf(node->dev_name, "sd#%04lx", id);
311                 break;
312             case TYPE_ROM:
313             case TYPE_WORM:
314                 node->major = SCSI_CDROM_MAJOR;
315                 sprintf(node->dev_name, "sr#%04lx", id);
316                 break;
317             default:
318                 node->major = SCSI_GENERIC_MAJOR;
319                 sprintf(node->dev_name, "sg#%04lx", id);
320                 break;
321             }
322             *tail = node; tail = &node->next;
323             info->ndev++;
324             info->host = dev->host;
325         }
326     *tail = NULL;
327     if (info->ndev == 0)
328         printk(KERN_INFO "aha152x_cs: no SCSI devices found\n");
329     
330     link->state &= ~DEV_CONFIG_PENDING;
331     return;
332     
333 cs_failed:
334     cs_error(link->handle, last_fn, last_ret);
335     aha152x_release_cs((u_long)link);
336     return;
337     
338 } /* aha152x_config_cs */
339
340 /*====================================================================*/
341
342 static void aha152x_release_cs(u_long arg)
343 {
344     dev_link_t *link = (dev_link_t *)arg;
345
346     DEBUG(0, "aha152x_release_cs(0x%p)\n", link);
347
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;
352         return;
353     }
354
355     scsi_unregister_module(MODULE_SCSI_HA, &driver_template);
356     link->dev = NULL;
357     
358     CardServices(ReleaseConfiguration, link->handle);
359     CardServices(ReleaseIO, link->handle, &link->io);
360     CardServices(ReleaseIRQ, link->handle, &link->irq);
361     
362     link->state &= ~DEV_CONFIG;
363     if (link->state & DEV_STALE_LINK)
364         aha152x_detach(link);
365     
366 } /* aha152x_release_cs */
367
368 /*====================================================================*/
369
370 static int aha152x_event(event_t event, int priority,
371                          event_callback_args_t *args)
372 {
373     dev_link_t *link = args->client_data;
374     scsi_info_t *info = link->priv;
375     
376     DEBUG(0, "aha152x_event(0x%06x)\n", event);
377     
378     switch (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);
383         break;
384     case CS_EVENT_CARD_INSERTION:
385         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
386         aha152x_config_cs(link);
387         break;
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);
394         break;
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) {
400             Scsi_Cmnd tmp;
401             CardServices(RequestConfiguration, link->handle, &link->conf);
402             tmp.host = info->host;
403             aha152x_host_reset(&tmp);
404         }
405         break;
406     }
407     return 0;
408 } /* aha152x_event */
409
410 /*====================================================================*/
411
412 static int __init init_aha152x_cs(void) {
413     servinfo_t serv;
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");
419         return -1;
420     }
421     register_pccard_driver(&dev_info, &aha152x_attach, &aha152x_detach);
422     return 0;
423 }
424
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);
430 }
431
432 module_init(init_aha152x_cs);
433 module_exit(exit_aha152x_cs);
434