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/cistpl.h> |
52 | #include <pcmcia/cisreg.h> | |
53 | #include <pcmcia/ds.h> | |
54 | ||
55 | static struct pcmcia_device *pcmcia_cur_dev = NULL; | |
56 | ||
2696fb57 | 57 | #define DIO24_SIZE 4 /* size of io region used by board */ |
d1d3cb32 | 58 | |
da91b269 BP |
59 | static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it); |
60 | static int dio24_detach(struct comedi_device *dev); | |
d1d3cb32 DVC |
61 | |
62 | enum dio24_bustype { pcmcia_bustype }; | |
63 | ||
cf4c8d1b | 64 | struct dio24_board_struct { |
d1d3cb32 | 65 | const char *name; |
2696fb57 BP |
66 | int device_id; /* device id for pcmcia board */ |
67 | enum dio24_bustype bustype; /* PCMCIA */ | |
68 | int have_dio; /* have 8255 chip */ | |
69 | /* function pointers so we can use inb/outb or readb/writeb as appropriate */ | |
d1d3cb32 DVC |
70 | unsigned int (*read_byte) (unsigned int address); |
71 | void (*write_byte) (unsigned int byte, unsigned int address); | |
cf4c8d1b | 72 | }; |
d1d3cb32 | 73 | |
cf4c8d1b | 74 | static const struct dio24_board_struct dio24_boards[] = { |
d1d3cb32 | 75 | { |
0a85b6f0 MT |
76 | .name = "daqcard-dio24", |
77 | .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */ | |
78 | .bustype = pcmcia_bustype, | |
79 | .have_dio = 1, | |
80 | }, | |
d1d3cb32 | 81 | { |
0a85b6f0 MT |
82 | .name = "ni_daq_dio24", |
83 | .device_id = 0x475c, /* 0x10b is manufacturer id, 0x475c is device id */ | |
84 | .bustype = pcmcia_bustype, | |
85 | .have_dio = 1, | |
86 | }, | |
d1d3cb32 DVC |
87 | }; |
88 | ||
89 | /* | |
90 | * Useful for shorthand access to the particular board structure | |
91 | */ | |
cf4c8d1b | 92 | #define thisboard ((const struct dio24_board_struct *)dev->board_ptr) |
d1d3cb32 | 93 | |
344d23e9 BP |
94 | struct dio24_private { |
95 | ||
d1d3cb32 | 96 | int data; /* number of data points left to be taken */ |
344d23e9 BP |
97 | }; |
98 | ||
344d23e9 | 99 | #define devpriv ((struct dio24_private *)dev->private) |
d1d3cb32 | 100 | |
139dfbdf | 101 | static struct comedi_driver driver_dio24 = { |
68c3dbff BP |
102 | .driver_name = "ni_daq_dio24", |
103 | .module = THIS_MODULE, | |
104 | .attach = dio24_attach, | |
105 | .detach = dio24_detach, | |
8629efa4 | 106 | .num_names = ARRAY_SIZE(dio24_boards), |
68c3dbff BP |
107 | .board_name = &dio24_boards[0].name, |
108 | .offset = sizeof(struct dio24_board_struct), | |
d1d3cb32 DVC |
109 | }; |
110 | ||
da91b269 | 111 | static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
d1d3cb32 | 112 | { |
34c43922 | 113 | struct comedi_subdevice *s; |
d1d3cb32 DVC |
114 | unsigned long iobase = 0; |
115 | #ifdef incomplete | |
116 | unsigned int irq = 0; | |
117 | #endif | |
118 | struct pcmcia_device *link; | |
119 | ||
120 | /* allocate and initialize dev->private */ | |
344d23e9 | 121 | if (alloc_private(dev, sizeof(struct dio24_private)) < 0) |
d1d3cb32 DVC |
122 | return -ENOMEM; |
123 | ||
2696fb57 | 124 | /* get base address, irq etc. based on bustype */ |
d1d3cb32 DVC |
125 | switch (thisboard->bustype) { |
126 | case pcmcia_bustype: | |
127 | link = pcmcia_cur_dev; /* XXX hack */ | |
128 | if (!link) | |
129 | return -EIO; | |
9a017a91 | 130 | iobase = link->resource[0]->start; |
d1d3cb32 | 131 | #ifdef incomplete |
eb14120f | 132 | irq = link->irq; |
d1d3cb32 DVC |
133 | #endif |
134 | break; | |
135 | default: | |
136 | printk("bug! couldn't determine board type\n"); | |
137 | return -EINVAL; | |
138 | break; | |
139 | } | |
140 | printk("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor, | |
0a85b6f0 | 141 | thisboard->name, iobase); |
d1d3cb32 DVC |
142 | #ifdef incomplete |
143 | if (irq) { | |
144 | printk(", irq %u", irq); | |
145 | } | |
146 | #endif | |
147 | ||
148 | printk("\n"); | |
149 | ||
150 | if (iobase == 0) { | |
151 | printk("io base address is zero!\n"); | |
152 | return -EINVAL; | |
153 | } | |
154 | ||
155 | dev->iobase = iobase; | |
156 | ||
157 | #ifdef incomplete | |
158 | /* grab our IRQ */ | |
159 | dev->irq = irq; | |
160 | #endif | |
161 | ||
162 | dev->board_name = thisboard->name; | |
163 | ||
164 | if (alloc_subdevices(dev, 1) < 0) | |
165 | return -ENOMEM; | |
166 | ||
167 | /* 8255 dio */ | |
168 | s = dev->subdevices + 0; | |
169 | subdev_8255_init(dev, s, NULL, dev->iobase); | |
170 | ||
171 | return 0; | |
172 | }; | |
173 | ||
da91b269 | 174 | static int dio24_detach(struct comedi_device *dev) |
d1d3cb32 DVC |
175 | { |
176 | printk("comedi%d: ni_daq_dio24: remove\n", dev->minor); | |
177 | ||
178 | if (dev->subdevices) | |
179 | subdev_8255_cleanup(dev, dev->subdevices + 0); | |
180 | ||
181 | if (thisboard->bustype != pcmcia_bustype && dev->iobase) | |
182 | release_region(dev->iobase, DIO24_SIZE); | |
183 | if (dev->irq) | |
5f74ea14 | 184 | free_irq(dev->irq, dev); |
d1d3cb32 DVC |
185 | |
186 | return 0; | |
187 | }; | |
188 | ||
55a19b39 | 189 | /* PCMCIA crap -- watch your words! */ |
d1d3cb32 DVC |
190 | |
191 | static void dio24_config(struct pcmcia_device *link); | |
192 | static void dio24_release(struct pcmcia_device *link); | |
193 | static int dio24_cs_suspend(struct pcmcia_device *p_dev); | |
194 | static int dio24_cs_resume(struct pcmcia_device *p_dev); | |
195 | ||
196 | /* | |
197 | The attach() and detach() entry points are used to create and destroy | |
198 | "instances" of the driver, where each instance represents everything | |
199 | needed to manage one actual PCMCIA card. | |
200 | */ | |
201 | ||
202 | static int dio24_cs_attach(struct pcmcia_device *); | |
203 | static void dio24_cs_detach(struct pcmcia_device *); | |
204 | ||
205 | /* | |
206 | You'll also need to prototype all the functions that will actually | |
207 | be used to talk to your device. See 'memory_cs' for a good example | |
208 | of a fully self-sufficient driver; the other drivers rely more or | |
209 | less on other parts of the kernel. | |
210 | */ | |
211 | ||
2ec9f875 | 212 | struct local_info_t { |
d1d3cb32 | 213 | struct pcmcia_device *link; |
d1d3cb32 DVC |
214 | int stop; |
215 | struct bus_operations *bus; | |
2ec9f875 | 216 | }; |
d1d3cb32 DVC |
217 | |
218 | /*====================================================================== | |
219 | ||
220 | dio24_cs_attach() creates an "instance" of the driver, allocating | |
221 | local data structures for one device. The device is registered | |
222 | with Card Services. | |
223 | ||
224 | The dev_link structure is initialized, but we don't actually | |
225 | configure the card at this point -- we wait until we receive a | |
226 | card insertion event. | |
227 | ||
228 | ======================================================================*/ | |
229 | ||
230 | static int dio24_cs_attach(struct pcmcia_device *link) | |
231 | { | |
2ec9f875 | 232 | struct local_info_t *local; |
d1d3cb32 DVC |
233 | |
234 | printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n"); | |
235 | ||
55a19b39 | 236 | dev_dbg(&link->dev, "dio24_cs_attach()\n"); |
d1d3cb32 DVC |
237 | |
238 | /* Allocate space for private device-specific data */ | |
2ec9f875 | 239 | local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL); |
d1d3cb32 DVC |
240 | if (!local) |
241 | return -ENOMEM; | |
242 | local->link = link; | |
243 | link->priv = local; | |
244 | ||
d1d3cb32 DVC |
245 | pcmcia_cur_dev = link; |
246 | ||
247 | dio24_config(link); | |
248 | ||
249 | return 0; | |
250 | } /* dio24_cs_attach */ | |
251 | ||
252 | /*====================================================================== | |
253 | ||
254 | This deletes a driver "instance". The device is de-registered | |
255 | with Card Services. If it has been released, all local data | |
256 | structures are freed. Otherwise, the structures will be freed | |
257 | when the device is released. | |
258 | ||
259 | ======================================================================*/ | |
260 | ||
261 | static void dio24_cs_detach(struct pcmcia_device *link) | |
262 | { | |
263 | ||
264 | printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - cs-detach!\n"); | |
265 | ||
55a19b39 | 266 | dev_dbg(&link->dev, "dio24_cs_detach\n"); |
d1d3cb32 | 267 | |
78950591 JMC |
268 | ((struct local_info_t *)link->priv)->stop = 1; |
269 | dio24_release(link); | |
d1d3cb32 DVC |
270 | |
271 | /* This points to the parent local_info_t struct */ | |
f25bd6bf | 272 | kfree(link->priv); |
d1d3cb32 DVC |
273 | |
274 | } /* dio24_cs_detach */ | |
275 | ||
276 | /*====================================================================== | |
277 | ||
278 | dio24_config() is scheduled to run after a CARD_INSERTION event | |
279 | is received, to configure the PCMCIA socket, and to make the | |
280 | device available to the system. | |
281 | ||
282 | ======================================================================*/ | |
283 | ||
55a19b39 | 284 | static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev, |
55a19b39 | 285 | void *priv_data) |
d1d3cb32 | 286 | { |
00990e7c DB |
287 | if (p_dev->config_index == 0) |
288 | return -EINVAL; | |
c3744138 | 289 | |
00990e7c | 290 | return pcmcia_request_io(p_dev); |
55a19b39 | 291 | } |
d1d3cb32 | 292 | |
55a19b39 DB |
293 | static void dio24_config(struct pcmcia_device *link) |
294 | { | |
55a19b39 | 295 | int ret; |
d1d3cb32 | 296 | |
55a19b39 | 297 | printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n"); |
d1d3cb32 | 298 | |
55a19b39 | 299 | dev_dbg(&link->dev, "dio24_config\n"); |
c3744138 | 300 | |
00990e7c DB |
301 | link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_AUDIO | |
302 | CONF_AUTO_SET_IO; | |
440eed43 | 303 | |
0f52e86d | 304 | ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, NULL); |
55a19b39 DB |
305 | if (ret) { |
306 | dev_warn(&link->dev, "no configuration found\n"); | |
307 | goto failed; | |
d1d3cb32 DVC |
308 | } |
309 | ||
eb14120f DB |
310 | if (!link->irq) |
311 | goto failed; | |
d1d3cb32 DVC |
312 | |
313 | /* | |
314 | This actually configures the PCMCIA socket -- setting up | |
315 | the I/O windows and the interrupt mapping, and putting the | |
316 | card and host interface into "Memory and IO" mode. | |
317 | */ | |
1ac71e5a | 318 | ret = pcmcia_enable_device(link); |
55a19b39 DB |
319 | if (ret) |
320 | goto failed; | |
d1d3cb32 | 321 | |
d1d3cb32 DVC |
322 | return; |
323 | ||
55a19b39 | 324 | failed: |
d1d3cb32 DVC |
325 | printk(KERN_INFO "Fallo"); |
326 | dio24_release(link); | |
327 | ||
328 | } /* dio24_config */ | |
329 | ||
330 | static void dio24_release(struct pcmcia_device *link) | |
331 | { | |
55a19b39 | 332 | dev_dbg(&link->dev, "dio24_release\n"); |
d1d3cb32 DVC |
333 | |
334 | pcmcia_disable_device(link); | |
335 | } /* dio24_release */ | |
336 | ||
337 | /*====================================================================== | |
338 | ||
339 | The card status event handler. Mostly, this schedules other | |
340 | stuff to run after an event is received. | |
341 | ||
342 | When a CARD_REMOVAL event is received, we immediately set a | |
343 | private flag to block future accesses to this device. All the | |
344 | functions that actually access the device should check this flag | |
345 | to make sure the card is still present. | |
346 | ||
347 | ======================================================================*/ | |
348 | ||
349 | static int dio24_cs_suspend(struct pcmcia_device *link) | |
350 | { | |
2ec9f875 | 351 | struct local_info_t *local = link->priv; |
d1d3cb32 DVC |
352 | |
353 | /* Mark the device as stopped, to block IO until later */ | |
354 | local->stop = 1; | |
355 | return 0; | |
356 | } /* dio24_cs_suspend */ | |
357 | ||
358 | static int dio24_cs_resume(struct pcmcia_device *link) | |
359 | { | |
2ec9f875 | 360 | struct local_info_t *local = link->priv; |
d1d3cb32 DVC |
361 | |
362 | local->stop = 0; | |
363 | return 0; | |
364 | } /* dio24_cs_resume */ | |
365 | ||
366 | /*====================================================================*/ | |
367 | ||
368 | static struct pcmcia_device_id dio24_cs_ids[] = { | |
369 | /* N.B. These IDs should match those in dio24_boards */ | |
370 | PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */ | |
371 | PCMCIA_DEVICE_NULL | |
372 | }; | |
373 | ||
374 | MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids); | |
6c7f8196 AK |
375 | MODULE_AUTHOR("Daniel Vecino Castel <dvecino@able.es>"); |
376 | MODULE_DESCRIPTION("Comedi driver for National Instruments " | |
377 | "PCMCIA DAQ-Card DIO-24"); | |
378 | MODULE_LICENSE("GPL"); | |
d1d3cb32 DVC |
379 | |
380 | struct pcmcia_driver dio24_cs_driver = { | |
381 | .probe = dio24_cs_attach, | |
382 | .remove = dio24_cs_detach, | |
383 | .suspend = dio24_cs_suspend, | |
384 | .resume = dio24_cs_resume, | |
385 | .id_table = dio24_cs_ids, | |
386 | .owner = THIS_MODULE, | |
2e9b981a | 387 | .name = "ni_daq_dio24", |
d1d3cb32 DVC |
388 | }; |
389 | ||
390 | static int __init init_dio24_cs(void) | |
391 | { | |
392 | printk("ni_daq_dio24: HOLA SOY YO!\n"); | |
d1d3cb32 DVC |
393 | pcmcia_register_driver(&dio24_cs_driver); |
394 | return 0; | |
395 | } | |
396 | ||
397 | static void __exit exit_dio24_cs(void) | |
398 | { | |
d1d3cb32 DVC |
399 | pcmcia_unregister_driver(&dio24_cs_driver); |
400 | } | |
401 | ||
402 | int __init init_module(void) | |
403 | { | |
404 | int ret; | |
405 | ||
406 | ret = init_dio24_cs(); | |
407 | if (ret < 0) | |
408 | return ret; | |
409 | ||
410 | return comedi_driver_register(&driver_dio24); | |
411 | } | |
412 | ||
413 | void __exit cleanup_module(void) | |
414 | { | |
415 | exit_dio24_cs(); | |
416 | comedi_driver_unregister(&driver_dio24); | |
417 | } |