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 <scsi/scsi_host.h>
51 #include "aha152x.h"
52 
53 #include <pcmcia/cs_types.h>
54 #include <pcmcia/cs.h>
55 #include <pcmcia/cistpl.h>
56 #include <pcmcia/ds.h>
57 
58 #ifdef PCMCIA_DEBUG
59 static int pc_debug = PCMCIA_DEBUG;
60 module_param(pc_debug, int, 0644);
61 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
62 static char *version =
63 "aha152x_cs.c 1.54 2000/06/12 21:27:25 (David Hinds)";
64 #else
65 #define DEBUG(n, args...)
66 #endif
67 
68 /*====================================================================*/
69 
70 /* Parameters that can be set with 'insmod' */
71 
72 /* SCSI bus setup options */
73 static int host_id = 7;
74 static int reconnect = 1;
75 static int parity = 1;
76 static int synchronous = 1;
77 static int reset_delay = 100;
78 static int ext_trans = 0;
79 
80 module_param(host_id, int, 0);
81 module_param(reconnect, int, 0);
82 module_param(parity, int, 0);
83 module_param(synchronous, int, 0);
84 module_param(reset_delay, int, 0);
85 module_param(ext_trans, int, 0);
86 
87 MODULE_LICENSE("Dual MPL/GPL");
88 
89 /*====================================================================*/
90 
91 typedef struct scsi_info_t {
92     dev_link_t		link;
93     dev_node_t		node;
94     struct Scsi_Host	*host;
95 } scsi_info_t;
96 
97 static void aha152x_release_cs(dev_link_t *link);
98 static void aha152x_detach(struct pcmcia_device *p_dev);
99 static void aha152x_config_cs(dev_link_t *link);
100 
101 static dev_link_t *dev_list;
102 
103 static int aha152x_attach(struct pcmcia_device *p_dev)
104 {
105     scsi_info_t *info;
106     dev_link_t *link;
107 
108     DEBUG(0, "aha152x_attach()\n");
109 
110     /* Create new SCSI device */
111     info = kmalloc(sizeof(*info), GFP_KERNEL);
112     if (!info) return -ENOMEM;
113     memset(info, 0, sizeof(*info));
114     link = &info->link; link->priv = info;
115 
116     link->io.NumPorts1 = 0x20;
117     link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
118     link->io.IOAddrLines = 10;
119     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
120     link->irq.IRQInfo1 = IRQ_LEVEL_ID;
121     link->conf.Attributes = CONF_ENABLE_IRQ;
122     link->conf.Vcc = 50;
123     link->conf.IntType = INT_MEMORY_AND_IO;
124     link->conf.Present = PRESENT_OPTION;
125 
126     link->handle = p_dev;
127     p_dev->instance = link;
128 
129     link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
130     aha152x_config_cs(link);
131 
132     return 0;
133 } /* aha152x_attach */
134 
135 /*====================================================================*/
136 
137 static void aha152x_detach(struct pcmcia_device *p_dev)
138 {
139     dev_link_t *link = dev_to_instance(p_dev);
140     dev_link_t **linkp;
141 
142     DEBUG(0, "aha152x_detach(0x%p)\n", link);
143 
144     /* Locate device structure */
145     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
146 	if (*linkp == link) break;
147     if (*linkp == NULL)
148 	return;
149 
150     if (link->state & DEV_CONFIG)
151 	aha152x_release_cs(link);
152 
153     /* Unlink device structure, free bits */
154     *linkp = link->next;
155     kfree(link->priv);
156 
157 } /* aha152x_detach */
158 
159 /*====================================================================*/
160 
161 #define CS_CHECK(fn, ret) \
162 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
163 
164 static void aha152x_config_cs(dev_link_t *link)
165 {
166     client_handle_t handle = link->handle;
167     scsi_info_t *info = link->priv;
168     struct aha152x_setup s;
169     tuple_t tuple;
170     cisparse_t parse;
171     int i, last_ret, last_fn;
172     u_char tuple_data[64];
173     struct Scsi_Host *host;
174 
175     DEBUG(0, "aha152x_config(0x%p)\n", link);
176 
177     tuple.DesiredTuple = CISTPL_CONFIG;
178     tuple.TupleData = tuple_data;
179     tuple.TupleDataMax = 64;
180     tuple.TupleOffset = 0;
181     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
182     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
183     CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
184     link->conf.ConfigBase = parse.config.base;
185 
186     /* Configure card */
187     link->state |= DEV_CONFIG;
188 
189     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
190     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
191     while (1) {
192 	if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
193 		pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
194 	    goto next_entry;
195 	/* For New Media T&J, look for a SCSI window */
196 	if (parse.cftable_entry.io.win[0].len >= 0x20)
197 	    link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
198 	else if ((parse.cftable_entry.io.nwin > 1) &&
199 		 (parse.cftable_entry.io.win[1].len >= 0x20))
200 	    link->io.BasePort1 = parse.cftable_entry.io.win[1].base;
201 	if ((parse.cftable_entry.io.nwin > 0) &&
202 	    (link->io.BasePort1 < 0xffff)) {
203 	    link->conf.ConfigIndex = parse.cftable_entry.index;
204 	    i = pcmcia_request_io(handle, &link->io);
205 	    if (i == CS_SUCCESS) break;
206 	}
207     next_entry:
208 	CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
209     }
210 
211     CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
212     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
213 
214     /* Set configuration options for the aha152x driver */
215     memset(&s, 0, sizeof(s));
216     s.conf        = "PCMCIA setup";
217     s.io_port     = link->io.BasePort1;
218     s.irq         = link->irq.AssignedIRQ;
219     s.scsiid      = host_id;
220     s.reconnect   = reconnect;
221     s.parity      = parity;
222     s.synchronous = synchronous;
223     s.delay       = reset_delay;
224     if (ext_trans)
225         s.ext_trans = ext_trans;
226 
227     host = aha152x_probe_one(&s);
228     if (host == NULL) {
229 	printk(KERN_INFO "aha152x_cs: no SCSI devices found\n");
230 	goto cs_failed;
231     }
232 
233     sprintf(info->node.dev_name, "scsi%d", host->host_no);
234     link->dev = &info->node;
235     info->host = host;
236 
237     link->state &= ~DEV_CONFIG_PENDING;
238     return;
239 
240 cs_failed:
241     cs_error(link->handle, last_fn, last_ret);
242     aha152x_release_cs(link);
243     return;
244 }
245 
246 static void aha152x_release_cs(dev_link_t *link)
247 {
248 	scsi_info_t *info = link->priv;
249 
250 	aha152x_release(info->host);
251 	link->dev = NULL;
252 
253 	pcmcia_release_configuration(link->handle);
254 	pcmcia_release_io(link->handle, &link->io);
255 	pcmcia_release_irq(link->handle, &link->irq);
256 
257 	link->state &= ~DEV_CONFIG;
258 }
259 
260 static int aha152x_suspend(struct pcmcia_device *dev)
261 {
262 	dev_link_t *link = dev_to_instance(dev);
263 
264 	link->state |= DEV_SUSPEND;
265 	if (link->state & DEV_CONFIG)
266 		pcmcia_release_configuration(link->handle);
267 
268 	return 0;
269 }
270 
271 static int aha152x_resume(struct pcmcia_device *dev)
272 {
273 	dev_link_t *link = dev_to_instance(dev);
274 	scsi_info_t *info = link->priv;
275 
276 	link->state &= ~DEV_SUSPEND;
277 	if (link->state & DEV_CONFIG) {
278 		Scsi_Cmnd tmp;
279 		pcmcia_request_configuration(link->handle, &link->conf);
280 		tmp.device->host = info->host;
281 		aha152x_host_reset(&tmp);
282 	}
283 
284 	return 0;
285 }
286 
287 static struct pcmcia_device_id aha152x_ids[] = {
288 	PCMCIA_DEVICE_PROD_ID123("New Media", "SCSI", "Bus Toaster", 0xcdf7e4cc, 0x35f26476, 0xa8851d6e),
289 	PCMCIA_DEVICE_PROD_ID123("NOTEWORTHY", "SCSI", "Bus Toaster", 0xad89c6e8, 0x35f26476, 0xa8851d6e),
290 	PCMCIA_DEVICE_PROD_ID12("Adaptec, Inc.", "APA-1460 SCSI Host Adapter", 0x24ba9738, 0x3a3c3d20),
291 	PCMCIA_DEVICE_PROD_ID12("New Media Corporation", "Multimedia Sound/SCSI", 0x085a850b, 0x80a6535c),
292 	PCMCIA_DEVICE_PROD_ID12("NOTEWORTHY", "NWCOMB02 SCSI/AUDIO COMBO CARD", 0xad89c6e8, 0x5f9a615b),
293 	PCMCIA_DEVICE_NULL,
294 };
295 MODULE_DEVICE_TABLE(pcmcia, aha152x_ids);
296 
297 static struct pcmcia_driver aha152x_cs_driver = {
298 	.owner		= THIS_MODULE,
299 	.drv		= {
300 		.name	= "aha152x_cs",
301 	},
302 	.probe		= aha152x_attach,
303 	.remove		= aha152x_detach,
304 	.id_table       = aha152x_ids,
305 	.suspend	= aha152x_suspend,
306 	.resume		= aha152x_resume,
307 };
308 
309 static int __init init_aha152x_cs(void)
310 {
311 	return pcmcia_register_driver(&aha152x_cs_driver);
312 }
313 
314 static void __exit exit_aha152x_cs(void)
315 {
316 	pcmcia_unregister_driver(&aha152x_cs_driver);
317 	BUG_ON(dev_list != NULL);
318 }
319 
320 module_init(init_aha152x_cs);
321 module_exit(exit_aha152x_cs);
322 
323