Commit | Line | Data |
---|---|---|
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 | /* | |
29 | Driver: ni_daq_dio24 | |
30 | Description: National Instruments PCMCIA DAQ-Card DIO-24 | |
31 | Author: Daniel Vecino Castel <dvecino@able.es> | |
32 | Devices: [National Instruments] PCMCIA DAQ-Card DIO-24 (ni_daq_dio24) | |
33 | Status: ? | |
34 | Updated: Thu, 07 Nov 2002 21:53:06 -0800 | |
35 | ||
36 | This is just a wrapper around the 8255.o driver to properly handle | |
37 | the 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 | ||
56 | static 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 |
60 | static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it); |
61 | static int dio24_detach(struct comedi_device *dev); | |
d1d3cb32 DVC |
62 | |
63 | enum dio24_bustype { pcmcia_bustype }; | |
64 | ||
cf4c8d1b | 65 | struct 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 | 75 | static 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 |
95 | struct 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 | 102 | static 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 | 112 | static 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 | 175 | static 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 | |
192 | static void dio24_config(struct pcmcia_device *link); | |
193 | static void dio24_release(struct pcmcia_device *link); | |
194 | static int dio24_cs_suspend(struct pcmcia_device *p_dev); | |
195 | static 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 | ||
203 | static int dio24_cs_attach(struct pcmcia_device *); | |
204 | static 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 | ||
2ec9f875 | 213 | struct local_info_t { |
d1d3cb32 | 214 | struct pcmcia_device *link; |
d1d3cb32 DVC |
215 | int stop; |
216 | struct bus_operations *bus; | |
2ec9f875 | 217 | }; |
d1d3cb32 DVC |
218 | |
219 | /*====================================================================== | |
220 | ||
221 | dio24_cs_attach() creates an "instance" of the driver, allocating | |
222 | local data structures for one device. The device is registered | |
223 | with Card Services. | |
224 | ||
225 | The dev_link structure is initialized, but we don't actually | |
226 | configure the card at this point -- we wait until we receive a | |
227 | card insertion event. | |
228 | ||
229 | ======================================================================*/ | |
230 | ||
231 | static int dio24_cs_attach(struct pcmcia_device *link) | |
232 | { | |
2ec9f875 | 233 | struct local_info_t *local; |
d1d3cb32 DVC |
234 | |
235 | printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n"); | |
236 | ||
55a19b39 | 237 | dev_dbg(&link->dev, "dio24_cs_attach()\n"); |
d1d3cb32 DVC |
238 | |
239 | /* Allocate space for private device-specific data */ | |
2ec9f875 | 240 | local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL); |
d1d3cb32 DVC |
241 | if (!local) |
242 | return -ENOMEM; | |
243 | local->link = link; | |
244 | link->priv = local; | |
245 | ||
d1d3cb32 DVC |
246 | /* |
247 | General socket configuration defaults can go here. In this | |
248 | client, we assume very little, and rely on the CIS for almost | |
249 | everything. In most clients, many details (i.e., number, sizes, | |
250 | and attributes of IO windows) are fixed by the nature of the | |
251 | device, and can be hard-wired here. | |
252 | */ | |
253 | link->conf.Attributes = 0; | |
254 | link->conf.IntType = INT_MEMORY_AND_IO; | |
255 | ||
256 | pcmcia_cur_dev = link; | |
257 | ||
258 | dio24_config(link); | |
259 | ||
260 | return 0; | |
261 | } /* dio24_cs_attach */ | |
262 | ||
263 | /*====================================================================== | |
264 | ||
265 | This deletes a driver "instance". The device is de-registered | |
266 | with Card Services. If it has been released, all local data | |
267 | structures are freed. Otherwise, the structures will be freed | |
268 | when the device is released. | |
269 | ||
270 | ======================================================================*/ | |
271 | ||
272 | static void dio24_cs_detach(struct pcmcia_device *link) | |
273 | { | |
274 | ||
275 | printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n"); | |
276 | ||
55a19b39 | 277 | dev_dbg(&link->dev, "dio24_cs_detach\n"); |
d1d3cb32 | 278 | |
78950591 JMC |
279 | ((struct local_info_t *)link->priv)->stop = 1; |
280 | dio24_release(link); | |
d1d3cb32 DVC |
281 | |
282 | /* This points to the parent local_info_t struct */ | |
f25bd6bf | 283 | kfree(link->priv); |
d1d3cb32 DVC |
284 | |
285 | } /* dio24_cs_detach */ | |
286 | ||
287 | /*====================================================================== | |
288 | ||
289 | dio24_config() is scheduled to run after a CARD_INSERTION event | |
290 | is received, to configure the PCMCIA socket, and to make the | |
291 | device available to the system. | |
292 | ||
293 | ======================================================================*/ | |
294 | ||
55a19b39 DB |
295 | static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev, |
296 | cistpl_cftable_entry_t *cfg, | |
297 | cistpl_cftable_entry_t *dflt, | |
298 | unsigned int vcc, | |
299 | void *priv_data) | |
d1d3cb32 | 300 | { |
55a19b39 DB |
301 | if (cfg->index == 0) |
302 | return -ENODEV; | |
c3744138 | 303 | |
55a19b39 DB |
304 | /* Does this card need audio output? */ |
305 | if (cfg->flags & CISTPL_CFTABLE_AUDIO) { | |
306 | p_dev->conf.Attributes |= CONF_ENABLE_SPKR; | |
307 | p_dev->conf.Status = CCSR_AUDIO_ENA; | |
d1d3cb32 | 308 | } |
c3744138 | 309 | |
55a19b39 | 310 | /* Do we need to allocate an interrupt? */ |
eb14120f | 311 | p_dev->conf.Attributes |= CONF_ENABLE_IRQ; |
55a19b39 DB |
312 | |
313 | /* IO window settings */ | |
90abdc3b | 314 | p_dev->resource[0]->end = p_dev->resource[1]->end = 0; |
55a19b39 DB |
315 | if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) { |
316 | cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io; | |
90abdc3b DB |
317 | p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK; |
318 | p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH; | |
319 | p_dev->resource[0]->flags |= | |
320 | pcmcia_io_cfg_data_width(io->flags); | |
321 | p_dev->resource[0]->start = io->win[0].base; | |
322 | p_dev->resource[0]->end = io->win[0].len; | |
55a19b39 | 323 | if (io->nwin > 1) { |
90abdc3b DB |
324 | p_dev->resource[1]->flags = p_dev->resource[0]->flags; |
325 | p_dev->resource[1]->start = io->win[1].base; | |
326 | p_dev->resource[1]->end = io->win[1].len; | |
55a19b39 DB |
327 | } |
328 | /* This reserves IO space but doesn't actually enable it */ | |
90abdc3b | 329 | if (pcmcia_request_io(p_dev) != 0) |
55a19b39 | 330 | return -ENODEV; |
d1d3cb32 | 331 | } |
c3744138 | 332 | |
55a19b39 DB |
333 | /* If we got this far, we're cool! */ |
334 | return 0; | |
335 | } | |
d1d3cb32 | 336 | |
55a19b39 DB |
337 | static void dio24_config(struct pcmcia_device *link) |
338 | { | |
55a19b39 | 339 | int ret; |
d1d3cb32 | 340 | |
55a19b39 | 341 | printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n"); |
d1d3cb32 | 342 | |
55a19b39 | 343 | dev_dbg(&link->dev, "dio24_config\n"); |
c3744138 | 344 | |
0f52e86d | 345 | ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, NULL); |
55a19b39 DB |
346 | if (ret) { |
347 | dev_warn(&link->dev, "no configuration found\n"); | |
348 | goto failed; | |
d1d3cb32 DVC |
349 | } |
350 | ||
eb14120f DB |
351 | if (!link->irq) |
352 | goto failed; | |
d1d3cb32 DVC |
353 | |
354 | /* | |
355 | This actually configures the PCMCIA socket -- setting up | |
356 | the I/O windows and the interrupt mapping, and putting the | |
357 | card and host interface into "Memory and IO" mode. | |
358 | */ | |
55a19b39 DB |
359 | ret = pcmcia_request_configuration(link, &link->conf); |
360 | if (ret) | |
361 | goto failed; | |
d1d3cb32 | 362 | |
d1d3cb32 | 363 | /* Finally, report what we've done */ |
78950591 | 364 | dev_info(&link->dev, "index 0x%02x", link->conf.ConfigIndex); |
d1d3cb32 | 365 | if (link->conf.Attributes & CONF_ENABLE_IRQ) |
eb14120f | 366 | printk(", irq %d", link->irq); |
9a017a91 DB |
367 | if (link->resource[0]) |
368 | printk(" & %pR", link->resource[0]); | |
369 | if (link->resource[1]) | |
370 | printk(" & %pR", link->resource[1]); | |
d1d3cb32 DVC |
371 | printk("\n"); |
372 | ||
373 | return; | |
374 | ||
55a19b39 | 375 | failed: |
d1d3cb32 DVC |
376 | printk(KERN_INFO "Fallo"); |
377 | dio24_release(link); | |
378 | ||
379 | } /* dio24_config */ | |
380 | ||
381 | static void dio24_release(struct pcmcia_device *link) | |
382 | { | |
55a19b39 | 383 | dev_dbg(&link->dev, "dio24_release\n"); |
d1d3cb32 DVC |
384 | |
385 | pcmcia_disable_device(link); | |
386 | } /* dio24_release */ | |
387 | ||
388 | /*====================================================================== | |
389 | ||
390 | The card status event handler. Mostly, this schedules other | |
391 | stuff to run after an event is received. | |
392 | ||
393 | When a CARD_REMOVAL event is received, we immediately set a | |
394 | private flag to block future accesses to this device. All the | |
395 | functions that actually access the device should check this flag | |
396 | to make sure the card is still present. | |
397 | ||
398 | ======================================================================*/ | |
399 | ||
400 | static int dio24_cs_suspend(struct pcmcia_device *link) | |
401 | { | |
2ec9f875 | 402 | struct local_info_t *local = link->priv; |
d1d3cb32 DVC |
403 | |
404 | /* Mark the device as stopped, to block IO until later */ | |
405 | local->stop = 1; | |
406 | return 0; | |
407 | } /* dio24_cs_suspend */ | |
408 | ||
409 | static int dio24_cs_resume(struct pcmcia_device *link) | |
410 | { | |
2ec9f875 | 411 | struct local_info_t *local = link->priv; |
d1d3cb32 DVC |
412 | |
413 | local->stop = 0; | |
414 | return 0; | |
415 | } /* dio24_cs_resume */ | |
416 | ||
417 | /*====================================================================*/ | |
418 | ||
419 | static struct pcmcia_device_id dio24_cs_ids[] = { | |
420 | /* N.B. These IDs should match those in dio24_boards */ | |
421 | PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */ | |
422 | PCMCIA_DEVICE_NULL | |
423 | }; | |
424 | ||
425 | MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids); | |
6c7f8196 AK |
426 | MODULE_AUTHOR("Daniel Vecino Castel <dvecino@able.es>"); |
427 | MODULE_DESCRIPTION("Comedi driver for National Instruments " | |
428 | "PCMCIA DAQ-Card DIO-24"); | |
429 | MODULE_LICENSE("GPL"); | |
d1d3cb32 DVC |
430 | |
431 | struct pcmcia_driver dio24_cs_driver = { | |
432 | .probe = dio24_cs_attach, | |
433 | .remove = dio24_cs_detach, | |
434 | .suspend = dio24_cs_suspend, | |
435 | .resume = dio24_cs_resume, | |
436 | .id_table = dio24_cs_ids, | |
437 | .owner = THIS_MODULE, | |
438 | .drv = { | |
1685e633 | 439 | .name = "ni_daq_dio24", |
d1d3cb32 DVC |
440 | }, |
441 | }; | |
442 | ||
443 | static int __init init_dio24_cs(void) | |
444 | { | |
445 | printk("ni_daq_dio24: HOLA SOY YO!\n"); | |
d1d3cb32 DVC |
446 | pcmcia_register_driver(&dio24_cs_driver); |
447 | return 0; | |
448 | } | |
449 | ||
450 | static void __exit exit_dio24_cs(void) | |
451 | { | |
d1d3cb32 DVC |
452 | pcmcia_unregister_driver(&dio24_cs_driver); |
453 | } | |
454 | ||
455 | int __init init_module(void) | |
456 | { | |
457 | int ret; | |
458 | ||
459 | ret = init_dio24_cs(); | |
460 | if (ret < 0) | |
461 | return ret; | |
462 | ||
463 | return comedi_driver_register(&driver_dio24); | |
464 | } | |
465 | ||
466 | void __exit cleanup_module(void) | |
467 | { | |
468 | exit_dio24_cs(); | |
469 | comedi_driver_unregister(&driver_dio24); | |
470 | } |