pcmcia: avoid buffer overflow in pcmcia_setup_isa_irq
[pandora-kernel.git] / drivers / staging / comedi / drivers / ni_labpc_cs.c
1 /*
2     comedi/drivers/ni_labpc_cs.c
3     Driver for National Instruments daqcard-1200 boards
4     Copyright (C) 2001, 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net>
5
6     PCMCIA crap 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.
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_labpc_cs
30 Description: National Instruments Lab-PC (& compatibles)
31 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
32 Devices: [National Instruments] DAQCard-1200 (daqcard-1200)
33 Status: works
34
35 Thanks go to Fredrik Lingvall for much testing and perseverance in
36 helping to debug daqcard-1200 support.
37
38 The 1200 series boards have onboard calibration dacs for correcting
39 analog input/output offsets and gains.  The proper settings for these
40 caldacs are stored on the board's eeprom.  To read the caldac values
41 from the eeprom and store them into a file that can be then be used by
42 comedilib, use the comedi_calibrate program.
43
44 Configuration options:
45   none
46
47 The daqcard-1200 has quirky chanlist requirements
48 when scanning multiple channels.  Multiple channel scan
49 sequence must start at highest channel, then decrement down to
50 channel 0.  Chanlists consisting of all one channel
51 are also legal, and allow you to pace conversions in bursts.
52
53 */
54
55 /*
56
57 NI manuals:
58 340988a (daqcard-1200)
59
60 */
61
62 #undef LABPC_DEBUG  /* debugging messages */
63
64 #include "../comedidev.h"
65
66 #include <linux/delay.h>
67 #include <linux/slab.h>
68
69 #include "8253.h"
70 #include "8255.h"
71 #include "comedi_fc.h"
72 #include "ni_labpc.h"
73
74 #include <pcmcia/cs.h>
75 #include <pcmcia/cistpl.h>
76 #include <pcmcia/cisreg.h>
77 #include <pcmcia/ds.h>
78
79 static struct pcmcia_device *pcmcia_cur_dev;
80
81 static int labpc_attach(struct comedi_device *dev, struct comedi_devconfig *it);
82
83 static const struct labpc_board_struct labpc_cs_boards[] = {
84         {
85          .name = "daqcard-1200",
86          .device_id = 0x103,    /* 0x10b is manufacturer id,
87                                    0x103 is device id */
88          .ai_speed = 10000,
89          .bustype = pcmcia_bustype,
90          .register_layout = labpc_1200_layout,
91          .has_ao = 1,
92          .ai_range_table = &range_labpc_1200_ai,
93          .ai_range_code = labpc_1200_ai_gain_bits,
94          .ai_range_is_unipolar = labpc_1200_is_unipolar,
95          .ai_scan_up = 0,
96          .memory_mapped_io = 0,
97          },
98         /* duplicate entry, to support using alternate name */
99         {
100          .name = "ni_labpc_cs",
101          .device_id = 0x103,
102          .ai_speed = 10000,
103          .bustype = pcmcia_bustype,
104          .register_layout = labpc_1200_layout,
105          .has_ao = 1,
106          .ai_range_table = &range_labpc_1200_ai,
107          .ai_range_code = labpc_1200_ai_gain_bits,
108          .ai_range_is_unipolar = labpc_1200_is_unipolar,
109          .ai_scan_up = 0,
110          .memory_mapped_io = 0,
111          },
112 };
113
114 /*
115  * Useful for shorthand access to the particular board structure
116  */
117 #define thisboard ((const struct labpc_board_struct *)dev->board_ptr)
118
119 static struct comedi_driver driver_labpc_cs = {
120         .driver_name = "ni_labpc_cs",
121         .module = THIS_MODULE,
122         .attach = &labpc_attach,
123         .detach = &labpc_common_detach,
124         .num_names = ARRAY_SIZE(labpc_cs_boards),
125         .board_name = &labpc_cs_boards[0].name,
126         .offset = sizeof(struct labpc_board_struct),
127 };
128
129 static int labpc_attach(struct comedi_device *dev, struct comedi_devconfig *it)
130 {
131         unsigned long iobase = 0;
132         unsigned int irq = 0;
133         struct pcmcia_device *link;
134
135         /* allocate and initialize dev->private */
136         if (alloc_private(dev, sizeof(struct labpc_private)) < 0)
137                 return -ENOMEM;
138
139         /*  get base address, irq etc. based on bustype */
140         switch (thisboard->bustype) {
141         case pcmcia_bustype:
142                 link = pcmcia_cur_dev;  /* XXX hack */
143                 if (!link)
144                         return -EIO;
145                 iobase = link->resource[0]->start;
146                 irq = link->irq;
147                 break;
148         default:
149                 printk("bug! couldn't determine board type\n");
150                 return -EINVAL;
151                 break;
152         }
153         return labpc_common_attach(dev, iobase, irq, 0);
154 }
155
156 /*====================================================================*/
157
158 /*
159    The event() function is this driver's Card Services event handler.
160    It will be called by Card Services when an appropriate card status
161    event is received.  The config() and release() entry points are
162    used to configure or release a socket, in response to card
163    insertion and ejection events.  They are invoked from the dummy
164    event handler.
165
166    Kernel version 2.6.16 upwards uses suspend() and resume() functions
167    instead of an event() function.
168 */
169
170 static void labpc_config(struct pcmcia_device *link);
171 static void labpc_release(struct pcmcia_device *link);
172 static int labpc_cs_suspend(struct pcmcia_device *p_dev);
173 static int labpc_cs_resume(struct pcmcia_device *p_dev);
174
175 /*
176    The attach() and detach() entry points are used to create and destroy
177    "instances" of the driver, where each instance represents everything
178    needed to manage one actual PCMCIA card.
179 */
180
181 static int labpc_cs_attach(struct pcmcia_device *);
182 static void labpc_cs_detach(struct pcmcia_device *);
183
184 /*
185    You'll also need to prototype all the functions that will actually
186    be used to talk to your device.  See 'memory_cs' for a good example
187    of a fully self-sufficient driver; the other drivers rely more or
188    less on other parts of the kernel.
189 */
190
191 /*
192    The dev_info variable is the "key" that is used to match up this
193    device driver with appropriate cards, through the card configuration
194    database.
195 */
196
197 static const dev_info_t dev_info = "daqcard-1200";
198
199 struct local_info_t {
200         struct pcmcia_device *link;
201         int stop;
202         struct bus_operations *bus;
203 };
204
205 /*======================================================================
206
207     labpc_cs_attach() creates an "instance" of the driver, allocating
208     local data structures for one device.  The device is registered
209     with Card Services.
210
211     The dev_link structure is initialized, but we don't actually
212     configure the card at this point -- we wait until we receive a
213     card insertion event.
214
215 ======================================================================*/
216
217 static int labpc_cs_attach(struct pcmcia_device *link)
218 {
219         struct local_info_t *local;
220
221         dev_dbg(&link->dev, "labpc_cs_attach()\n");
222
223         /* Allocate space for private device-specific data */
224         local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
225         if (!local)
226                 return -ENOMEM;
227         local->link = link;
228         link->priv = local;
229
230         /*
231            General socket configuration defaults can go here.  In this
232            client, we assume very little, and rely on the CIS for almost
233            everything.  In most clients, many details (i.e., number, sizes,
234            and attributes of IO windows) are fixed by the nature of the
235            device, and can be hard-wired here.
236          */
237         link->conf.Attributes = 0;
238         link->conf.IntType = INT_MEMORY_AND_IO;
239
240         pcmcia_cur_dev = link;
241
242         labpc_config(link);
243
244         return 0;
245 }                               /* labpc_cs_attach */
246
247 /*======================================================================
248
249     This deletes a driver "instance".  The device is de-registered
250     with Card Services.  If it has been released, all local data
251     structures are freed.  Otherwise, the structures will be freed
252     when the device is released.
253
254 ======================================================================*/
255
256 static void labpc_cs_detach(struct pcmcia_device *link)
257 {
258         dev_dbg(&link->dev, "labpc_cs_detach\n");
259
260         /*
261            If the device is currently configured and active, we won't
262            actually delete it yet.  Instead, it is marked so that when
263            the release() function is called, that will trigger a proper
264            detach().
265          */
266         ((struct local_info_t *)link->priv)->stop = 1;
267         labpc_release(link);
268
269         /* This points to the parent local_info_t struct (may be null) */
270         kfree(link->priv);
271
272 }                               /* labpc_cs_detach */
273
274 /*======================================================================
275
276     labpc_config() is scheduled to run after a CARD_INSERTION event
277     is received, to configure the PCMCIA socket, and to make the
278     device available to the system.
279
280 ======================================================================*/
281
282 static int labpc_pcmcia_config_loop(struct pcmcia_device *p_dev,
283                                 cistpl_cftable_entry_t *cfg,
284                                 cistpl_cftable_entry_t *dflt,
285                                 unsigned int vcc,
286                                 void *priv_data)
287 {
288         if (cfg->index == 0)
289                 return -ENODEV;
290
291         /* Does this card need audio output? */
292         if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
293                 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
294                 p_dev->conf.Status = CCSR_AUDIO_ENA;
295         }
296
297         /* Do we need to allocate an interrupt? */
298         p_dev->conf.Attributes |= CONF_ENABLE_IRQ | CONF_ENABLE_PULSE_IRQ;
299
300         /* IO window settings */
301         p_dev->resource[0]->end = p_dev->resource[1]->end = 0;
302         if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
303                 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
304                 p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
305                 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
306                 p_dev->resource[0]->flags |=
307                         pcmcia_io_cfg_data_width(io->flags);
308                 p_dev->resource[0]->start = io->win[0].base;
309                 p_dev->resource[0]->end = io->win[0].len;
310                 if (io->nwin > 1) {
311                         p_dev->resource[1]->flags = p_dev->resource[0]->flags;
312                         p_dev->resource[1]->start = io->win[1].base;
313                         p_dev->resource[1]->end = io->win[1].len;
314                 }
315                 /* This reserves IO space but doesn't actually enable it */
316                 if (pcmcia_request_io(p_dev) != 0)
317                         return -ENODEV;
318         }
319
320         /* If we got this far, we're cool! */
321         return 0;
322 }
323
324
325 static void labpc_config(struct pcmcia_device *link)
326 {
327         int ret;
328
329         dev_dbg(&link->dev, "labpc_config\n");
330
331         ret = pcmcia_loop_config(link, labpc_pcmcia_config_loop, NULL);
332         if (ret) {
333                 dev_warn(&link->dev, "no configuration found\n");
334                 goto failed;
335         }
336
337         if (!link->irq)
338                 goto failed;
339
340         /*
341            This actually configures the PCMCIA socket -- setting up
342            the I/O windows and the interrupt mapping, and putting the
343            card and host interface into "Memory and IO" mode.
344          */
345         ret = pcmcia_request_configuration(link, &link->conf);
346         if (ret)
347                 goto failed;
348
349         /* Finally, report what we've done */
350         dev_info(&link->dev, "index 0x%02x", link->conf.ConfigIndex);
351         if (link->conf.Attributes & CONF_ENABLE_IRQ)
352                 printk(", irq %d", link->irq);
353         if (link->resource[0])
354                 printk(" & %pR", link->resource[0]);
355         if (link->resource[1])
356                 printk(" & %pR", link->resource[1]);
357         printk("\n");
358
359         return;
360
361 failed:
362         labpc_release(link);
363
364 }                               /* labpc_config */
365
366 static void labpc_release(struct pcmcia_device *link)
367 {
368         dev_dbg(&link->dev, "labpc_release\n");
369
370         pcmcia_disable_device(link);
371 }                               /* labpc_release */
372
373 /*======================================================================
374
375     The card status event handler.  Mostly, this schedules other
376     stuff to run after an event is received.
377
378     When a CARD_REMOVAL event is received, we immediately set a
379     private flag to block future accesses to this device.  All the
380     functions that actually access the device should check this flag
381     to make sure the card is still present.
382
383 ======================================================================*/
384
385 static int labpc_cs_suspend(struct pcmcia_device *link)
386 {
387         struct local_info_t *local = link->priv;
388
389         /* Mark the device as stopped, to block IO until later */
390         local->stop = 1;
391         return 0;
392 }                               /* labpc_cs_suspend */
393
394 static int labpc_cs_resume(struct pcmcia_device *link)
395 {
396         struct local_info_t *local = link->priv;
397
398         local->stop = 0;
399         return 0;
400 }                               /* labpc_cs_resume */
401
402 /*====================================================================*/
403
404 static struct pcmcia_device_id labpc_cs_ids[] = {
405         /* N.B. These IDs should match those in labpc_cs_boards (ni_labpc.c) */
406         PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0103),        /* daqcard-1200 */
407         PCMCIA_DEVICE_NULL
408 };
409
410 MODULE_DEVICE_TABLE(pcmcia, labpc_cs_ids);
411 MODULE_AUTHOR("Frank Mori Hess <fmhess@users.sourceforge.net>");
412 MODULE_DESCRIPTION("Comedi driver for National Instruments Lab-PC");
413 MODULE_LICENSE("GPL");
414
415 struct pcmcia_driver labpc_cs_driver = {
416         .probe = labpc_cs_attach,
417         .remove = labpc_cs_detach,
418         .suspend = labpc_cs_suspend,
419         .resume = labpc_cs_resume,
420         .id_table = labpc_cs_ids,
421         .owner = THIS_MODULE,
422         .drv = {
423                 .name = dev_info,
424                 },
425 };
426
427 static int __init init_labpc_cs(void)
428 {
429         pcmcia_register_driver(&labpc_cs_driver);
430         return 0;
431 }
432
433 static void __exit exit_labpc_cs(void)
434 {
435         pcmcia_unregister_driver(&labpc_cs_driver);
436 }
437
438 int __init labpc_init_module(void)
439 {
440         int ret;
441
442         ret = init_labpc_cs();
443         if (ret < 0)
444                 return ret;
445
446         return comedi_driver_register(&driver_labpc_cs);
447 }
448
449 void __exit labpc_exit_module(void)
450 {
451         exit_labpc_cs();
452         comedi_driver_unregister(&driver_labpc_cs);
453 }
454
455 module_init(labpc_init_module);
456 module_exit(labpc_exit_module);