pcmcia: do not use io_req_t when calling pcmcia_request_io()
[deliverable/linux.git] / drivers / staging / comedi / drivers / ni_daq_dio24.c
CommitLineData
d1d3cb32
DVC
1/*
2 comedi/drivers/ni_daq_dio24.c
3 Driver for National Instruments PCMCIA DAQ-Card DIO-24
4 Copyright (C) 2002 Daniel Vecino Castel <dvecino@able.es>
5
6 PCMCIA crap at end of file is adapted from dummy_cs.c 1.31 2001/08/24 12:13:13
7 from the pcmcia package.
8 The initial developer of the pcmcia dummy_cs.c code is David A. Hinds
9 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
10 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26************************************************************************
27*/
28/*
29Driver: ni_daq_dio24
30Description: National Instruments PCMCIA DAQ-Card DIO-24
31Author: Daniel Vecino Castel <dvecino@able.es>
32Devices: [National Instruments] PCMCIA DAQ-Card DIO-24 (ni_daq_dio24)
33Status: ?
34Updated: Thu, 07 Nov 2002 21:53:06 -0800
35
36This is just a wrapper around the 8255.o driver to properly handle
37the PCMCIA interface.
38*/
39
0a85b6f0 40 /* #define LABPC_DEBUG *//* enable debugging messages */
d1d3cb32
DVC
41#undef LABPC_DEBUG
42
25436dc9 43#include <linux/interrupt.h>
5a0e3ad6 44#include <linux/slab.h>
d1d3cb32
DVC
45#include "../comedidev.h"
46
47#include <linux/ioport.h>
d1d3cb32
DVC
48
49#include "8255.h"
50
d1d3cb32
DVC
51#include <pcmcia/cs.h>
52#include <pcmcia/cistpl.h>
53#include <pcmcia/cisreg.h>
54#include <pcmcia/ds.h>
55
56static struct pcmcia_device *pcmcia_cur_dev = NULL;
57
2696fb57 58#define DIO24_SIZE 4 /* size of io region used by board */
d1d3cb32 59
da91b269
BP
60static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it);
61static int dio24_detach(struct comedi_device *dev);
d1d3cb32
DVC
62
63enum dio24_bustype { pcmcia_bustype };
64
cf4c8d1b 65struct dio24_board_struct {
d1d3cb32 66 const char *name;
2696fb57
BP
67 int device_id; /* device id for pcmcia board */
68 enum dio24_bustype bustype; /* PCMCIA */
69 int have_dio; /* have 8255 chip */
70 /* function pointers so we can use inb/outb or readb/writeb as appropriate */
d1d3cb32
DVC
71 unsigned int (*read_byte) (unsigned int address);
72 void (*write_byte) (unsigned int byte, unsigned int address);
cf4c8d1b 73};
d1d3cb32 74
cf4c8d1b 75static const struct dio24_board_struct dio24_boards[] = {
d1d3cb32 76 {
0a85b6f0
MT
77 .name = "daqcard-dio24",
78 .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */
79 .bustype = pcmcia_bustype,
80 .have_dio = 1,
81 },
d1d3cb32 82 {
0a85b6f0
MT
83 .name = "ni_daq_dio24",
84 .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */
85 .bustype = pcmcia_bustype,
86 .have_dio = 1,
87 },
d1d3cb32
DVC
88};
89
90/*
91 * Useful for shorthand access to the particular board structure
92 */
cf4c8d1b 93#define thisboard ((const struct dio24_board_struct *)dev->board_ptr)
d1d3cb32 94
344d23e9
BP
95struct dio24_private {
96
d1d3cb32 97 int data; /* number of data points left to be taken */
344d23e9
BP
98};
99
344d23e9 100#define devpriv ((struct dio24_private *)dev->private)
d1d3cb32 101
139dfbdf 102static struct comedi_driver driver_dio24 = {
68c3dbff
BP
103 .driver_name = "ni_daq_dio24",
104 .module = THIS_MODULE,
105 .attach = dio24_attach,
106 .detach = dio24_detach,
8629efa4 107 .num_names = ARRAY_SIZE(dio24_boards),
68c3dbff
BP
108 .board_name = &dio24_boards[0].name,
109 .offset = sizeof(struct dio24_board_struct),
d1d3cb32
DVC
110};
111
da91b269 112static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it)
d1d3cb32 113{
34c43922 114 struct comedi_subdevice *s;
d1d3cb32
DVC
115 unsigned long iobase = 0;
116#ifdef incomplete
117 unsigned int irq = 0;
118#endif
119 struct pcmcia_device *link;
120
121 /* allocate and initialize dev->private */
344d23e9 122 if (alloc_private(dev, sizeof(struct dio24_private)) < 0)
d1d3cb32
DVC
123 return -ENOMEM;
124
2696fb57 125 /* get base address, irq etc. based on bustype */
d1d3cb32
DVC
126 switch (thisboard->bustype) {
127 case pcmcia_bustype:
128 link = pcmcia_cur_dev; /* XXX hack */
129 if (!link)
130 return -EIO;
9a017a91 131 iobase = link->resource[0]->start;
d1d3cb32 132#ifdef incomplete
eb14120f 133 irq = link->irq;
d1d3cb32
DVC
134#endif
135 break;
136 default:
137 printk("bug! couldn't determine board type\n");
138 return -EINVAL;
139 break;
140 }
141 printk("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor,
0a85b6f0 142 thisboard->name, iobase);
d1d3cb32
DVC
143#ifdef incomplete
144 if (irq) {
145 printk(", irq %u", irq);
146 }
147#endif
148
149 printk("\n");
150
151 if (iobase == 0) {
152 printk("io base address is zero!\n");
153 return -EINVAL;
154 }
155
156 dev->iobase = iobase;
157
158#ifdef incomplete
159 /* grab our IRQ */
160 dev->irq = irq;
161#endif
162
163 dev->board_name = thisboard->name;
164
165 if (alloc_subdevices(dev, 1) < 0)
166 return -ENOMEM;
167
168 /* 8255 dio */
169 s = dev->subdevices + 0;
170 subdev_8255_init(dev, s, NULL, dev->iobase);
171
172 return 0;
173};
174
da91b269 175static int dio24_detach(struct comedi_device *dev)
d1d3cb32
DVC
176{
177 printk("comedi%d: ni_daq_dio24: remove\n", dev->minor);
178
179 if (dev->subdevices)
180 subdev_8255_cleanup(dev, dev->subdevices + 0);
181
182 if (thisboard->bustype != pcmcia_bustype && dev->iobase)
183 release_region(dev->iobase, DIO24_SIZE);
184 if (dev->irq)
5f74ea14 185 free_irq(dev->irq, dev);
d1d3cb32
DVC
186
187 return 0;
188};
189
55a19b39 190/* PCMCIA crap -- watch your words! */
d1d3cb32
DVC
191
192static void dio24_config(struct pcmcia_device *link);
193static void dio24_release(struct pcmcia_device *link);
194static int dio24_cs_suspend(struct pcmcia_device *p_dev);
195static int dio24_cs_resume(struct pcmcia_device *p_dev);
196
197/*
198 The attach() and detach() entry points are used to create and destroy
199 "instances" of the driver, where each instance represents everything
200 needed to manage one actual PCMCIA card.
201*/
202
203static int dio24_cs_attach(struct pcmcia_device *);
204static void dio24_cs_detach(struct pcmcia_device *);
205
206/*
207 You'll also need to prototype all the functions that will actually
208 be used to talk to your device. See 'memory_cs' for a good example
209 of a fully self-sufficient driver; the other drivers rely more or
210 less on other parts of the kernel.
211*/
212
213/*
214 The dev_info variable is the "key" that is used to match up this
215 device driver with appropriate cards, through the card configuration
216 database.
217*/
218
219static const dev_info_t dev_info = "ni_daq_dio24";
220
2ec9f875 221struct local_info_t {
d1d3cb32 222 struct pcmcia_device *link;
d1d3cb32
DVC
223 int stop;
224 struct bus_operations *bus;
2ec9f875 225};
d1d3cb32
DVC
226
227/*======================================================================
228
229 dio24_cs_attach() creates an "instance" of the driver, allocating
230 local data structures for one device. The device is registered
231 with Card Services.
232
233 The dev_link structure is initialized, but we don't actually
234 configure the card at this point -- we wait until we receive a
235 card insertion event.
236
237======================================================================*/
238
239static int dio24_cs_attach(struct pcmcia_device *link)
240{
2ec9f875 241 struct local_info_t *local;
d1d3cb32
DVC
242
243 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n");
244
55a19b39 245 dev_dbg(&link->dev, "dio24_cs_attach()\n");
d1d3cb32
DVC
246
247 /* Allocate space for private device-specific data */
2ec9f875 248 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
d1d3cb32
DVC
249 if (!local)
250 return -ENOMEM;
251 local->link = link;
252 link->priv = local;
253
d1d3cb32
DVC
254 /*
255 General socket configuration defaults can go here. In this
256 client, we assume very little, and rely on the CIS for almost
257 everything. In most clients, many details (i.e., number, sizes,
258 and attributes of IO windows) are fixed by the nature of the
259 device, and can be hard-wired here.
260 */
261 link->conf.Attributes = 0;
262 link->conf.IntType = INT_MEMORY_AND_IO;
263
264 pcmcia_cur_dev = link;
265
266 dio24_config(link);
267
268 return 0;
269} /* dio24_cs_attach */
270
271/*======================================================================
272
273 This deletes a driver "instance". The device is de-registered
274 with Card Services. If it has been released, all local data
275 structures are freed. Otherwise, the structures will be freed
276 when the device is released.
277
278======================================================================*/
279
280static void dio24_cs_detach(struct pcmcia_device *link)
281{
282
283 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n");
284
55a19b39 285 dev_dbg(&link->dev, "dio24_cs_detach\n");
d1d3cb32 286
78950591
JMC
287 ((struct local_info_t *)link->priv)->stop = 1;
288 dio24_release(link);
d1d3cb32
DVC
289
290 /* This points to the parent local_info_t struct */
291 if (link->priv)
292 kfree(link->priv);
293
294} /* dio24_cs_detach */
295
296/*======================================================================
297
298 dio24_config() is scheduled to run after a CARD_INSERTION event
299 is received, to configure the PCMCIA socket, and to make the
300 device available to the system.
301
302======================================================================*/
303
55a19b39
DB
304static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev,
305 cistpl_cftable_entry_t *cfg,
306 cistpl_cftable_entry_t *dflt,
307 unsigned int vcc,
308 void *priv_data)
d1d3cb32 309{
55a19b39 310 win_req_t *req = priv_data;
d1d3cb32 311 memreq_t map;
d1d3cb32 312
55a19b39
DB
313 if (cfg->index == 0)
314 return -ENODEV;
c3744138 315
55a19b39
DB
316 /* Does this card need audio output? */
317 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
318 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
319 p_dev->conf.Status = CCSR_AUDIO_ENA;
d1d3cb32 320 }
c3744138 321
55a19b39 322 /* Do we need to allocate an interrupt? */
eb14120f 323 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
55a19b39
DB
324
325 /* IO window settings */
90abdc3b 326 p_dev->resource[0]->end = p_dev->resource[1]->end = 0;
55a19b39
DB
327 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
328 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
90abdc3b
DB
329 p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
330 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
331 p_dev->resource[0]->flags |=
332 pcmcia_io_cfg_data_width(io->flags);
333 p_dev->resource[0]->start = io->win[0].base;
334 p_dev->resource[0]->end = io->win[0].len;
55a19b39 335 if (io->nwin > 1) {
90abdc3b
DB
336 p_dev->resource[1]->flags = p_dev->resource[0]->flags;
337 p_dev->resource[1]->start = io->win[1].base;
338 p_dev->resource[1]->end = io->win[1].len;
55a19b39
DB
339 }
340 /* This reserves IO space but doesn't actually enable it */
90abdc3b 341 if (pcmcia_request_io(p_dev) != 0)
55a19b39 342 return -ENODEV;
d1d3cb32 343 }
c3744138 344
55a19b39
DB
345 if ((cfg->mem.nwin > 0) || (dflt->mem.nwin > 0)) {
346 cistpl_mem_t *mem =
347 (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
348 req->Attributes = WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
349 req->Attributes |= WIN_ENABLE;
350 req->Base = mem->win[0].host_addr;
351 req->Size = mem->win[0].len;
352 if (req->Size < 0x1000)
353 req->Size = 0x1000;
354 req->AccessSpeed = 0;
6838b03f 355 if (pcmcia_request_window(p_dev, req, &p_dev->win))
55a19b39
DB
356 return -ENODEV;
357 map.Page = 0;
358 map.CardOffset = mem->win[0].card_addr;
868575d1 359 if (pcmcia_map_mem_page(p_dev, p_dev->win, &map))
55a19b39 360 return -ENODEV;
d1d3cb32 361 }
55a19b39
DB
362 /* If we got this far, we're cool! */
363 return 0;
364}
d1d3cb32 365
55a19b39
DB
366static void dio24_config(struct pcmcia_device *link)
367{
55a19b39
DB
368 int ret;
369 win_req_t req;
d1d3cb32 370
55a19b39 371 printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n");
d1d3cb32 372
55a19b39 373 dev_dbg(&link->dev, "dio24_config\n");
c3744138 374
55a19b39
DB
375 ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, &req);
376 if (ret) {
377 dev_warn(&link->dev, "no configuration found\n");
378 goto failed;
d1d3cb32
DVC
379 }
380
eb14120f
DB
381 if (!link->irq)
382 goto failed;
d1d3cb32
DVC
383
384 /*
385 This actually configures the PCMCIA socket -- setting up
386 the I/O windows and the interrupt mapping, and putting the
387 card and host interface into "Memory and IO" mode.
388 */
55a19b39
DB
389 ret = pcmcia_request_configuration(link, &link->conf);
390 if (ret)
391 goto failed;
d1d3cb32 392
d1d3cb32 393 /* Finally, report what we've done */
78950591 394 dev_info(&link->dev, "index 0x%02x", link->conf.ConfigIndex);
d1d3cb32 395 if (link->conf.Attributes & CONF_ENABLE_IRQ)
eb14120f 396 printk(", irq %d", link->irq);
9a017a91
DB
397 if (link->resource[0])
398 printk(" & %pR", link->resource[0]);
399 if (link->resource[1])
400 printk(" & %pR", link->resource[1]);
d1d3cb32
DVC
401 if (link->win)
402 printk(", mem 0x%06lx-0x%06lx", req.Base,
0a85b6f0 403 req.Base + req.Size - 1);
d1d3cb32
DVC
404 printk("\n");
405
406 return;
407
55a19b39 408failed:
d1d3cb32
DVC
409 printk(KERN_INFO "Fallo");
410 dio24_release(link);
411
412} /* dio24_config */
413
414static void dio24_release(struct pcmcia_device *link)
415{
55a19b39 416 dev_dbg(&link->dev, "dio24_release\n");
d1d3cb32
DVC
417
418 pcmcia_disable_device(link);
419} /* dio24_release */
420
421/*======================================================================
422
423 The card status event handler. Mostly, this schedules other
424 stuff to run after an event is received.
425
426 When a CARD_REMOVAL event is received, we immediately set a
427 private flag to block future accesses to this device. All the
428 functions that actually access the device should check this flag
429 to make sure the card is still present.
430
431======================================================================*/
432
433static int dio24_cs_suspend(struct pcmcia_device *link)
434{
2ec9f875 435 struct local_info_t *local = link->priv;
d1d3cb32
DVC
436
437 /* Mark the device as stopped, to block IO until later */
438 local->stop = 1;
439 return 0;
440} /* dio24_cs_suspend */
441
442static int dio24_cs_resume(struct pcmcia_device *link)
443{
2ec9f875 444 struct local_info_t *local = link->priv;
d1d3cb32
DVC
445
446 local->stop = 0;
447 return 0;
448} /* dio24_cs_resume */
449
450/*====================================================================*/
451
452static struct pcmcia_device_id dio24_cs_ids[] = {
453 /* N.B. These IDs should match those in dio24_boards */
454 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */
455 PCMCIA_DEVICE_NULL
456};
457
458MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
6c7f8196
AK
459MODULE_AUTHOR("Daniel Vecino Castel <dvecino@able.es>");
460MODULE_DESCRIPTION("Comedi driver for National Instruments "
461 "PCMCIA DAQ-Card DIO-24");
462MODULE_LICENSE("GPL");
d1d3cb32
DVC
463
464struct pcmcia_driver dio24_cs_driver = {
465 .probe = dio24_cs_attach,
466 .remove = dio24_cs_detach,
467 .suspend = dio24_cs_suspend,
468 .resume = dio24_cs_resume,
469 .id_table = dio24_cs_ids,
470 .owner = THIS_MODULE,
471 .drv = {
0a85b6f0 472 .name = dev_info,
d1d3cb32
DVC
473 },
474};
475
476static int __init init_dio24_cs(void)
477{
478 printk("ni_daq_dio24: HOLA SOY YO!\n");
d1d3cb32
DVC
479 pcmcia_register_driver(&dio24_cs_driver);
480 return 0;
481}
482
483static void __exit exit_dio24_cs(void)
484{
d1d3cb32
DVC
485 pcmcia_unregister_driver(&dio24_cs_driver);
486}
487
488int __init init_module(void)
489{
490 int ret;
491
492 ret = init_dio24_cs();
493 if (ret < 0)
494 return ret;
495
496 return comedi_driver_register(&driver_dio24);
497}
498
499void __exit cleanup_module(void)
500{
501 exit_dio24_cs();
502 comedi_driver_unregister(&driver_dio24);
503}
This page took 0.167221 seconds and 5 git commands to generate.