Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ni_daq_dio24.c
Go to the documentation of this file.
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 <[email protected]>
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  <[email protected]>. 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 <[email protected]>
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 
40  /* #define LABPC_DEBUG *//* enable debugging messages */
41 #undef LABPC_DEBUG
42 
43 #include <linux/interrupt.h>
44 #include <linux/slab.h>
45 #include "../comedidev.h"
46 
47 #include <linux/ioport.h>
48 
49 #include "8255.h"
50 
51 #include <pcmcia/cistpl.h>
52 #include <pcmcia/cisreg.h>
53 #include <pcmcia/ds.h>
54 
55 static struct pcmcia_device *pcmcia_cur_dev;
56 
57 #define DIO24_SIZE 4 /* size of io region used by board */
58 
59 static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it);
60 static void dio24_detach(struct comedi_device *dev);
61 
63 
65  const char *name;
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 */
70  unsigned int (*read_byte) (unsigned int address);
71  void (*write_byte) (unsigned int byte, unsigned int address);
72 };
73 
74 static const struct dio24_board_struct dio24_boards[] = {
75  {
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  },
81  {
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  },
87 };
88 
89 /*
90  * Useful for shorthand access to the particular board structure
91  */
92 #define thisboard ((const struct dio24_board_struct *)dev->board_ptr)
93 
94 struct dio24_private {
95 
96  int data; /* number of data points left to be taken */
97 };
98 
99 #define devpriv ((struct dio24_private *)dev->private)
100 
101 static struct comedi_driver driver_dio24 = {
102  .driver_name = "ni_daq_dio24",
103  .module = THIS_MODULE,
104  .attach = dio24_attach,
105  .detach = dio24_detach,
106  .num_names = ARRAY_SIZE(dio24_boards),
107  .board_name = &dio24_boards[0].name,
108  .offset = sizeof(struct dio24_board_struct),
109 };
110 
111 static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it)
112 {
113  struct comedi_subdevice *s;
114  unsigned long iobase = 0;
115 #ifdef incomplete
116  unsigned int irq = 0;
117 #endif
118  struct pcmcia_device *link;
119  int ret;
120 
121  /* allocate and initialize dev->private */
122  if (alloc_private(dev, sizeof(struct dio24_private)) < 0)
123  return -ENOMEM;
124 
125  /* get base address, irq etc. based on bustype */
126  switch (thisboard->bustype) {
127  case pcmcia_bustype:
128  link = pcmcia_cur_dev; /* XXX hack */
129  if (!link)
130  return -EIO;
131  iobase = link->resource[0]->start;
132 #ifdef incomplete
133  irq = link->irq;
134 #endif
135  break;
136  default:
137  pr_err("bug! couldn't determine board type\n");
138  return -EINVAL;
139  break;
140  }
141  pr_debug("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor,
142  thisboard->name, iobase);
143 #ifdef incomplete
144  if (irq)
145  pr_debug("irq %u\n", irq);
146 #endif
147 
148  if (iobase == 0) {
149  pr_err("io base address is zero!\n");
150  return -EINVAL;
151  }
152 
153  dev->iobase = iobase;
154 
155 #ifdef incomplete
156  /* grab our IRQ */
157  dev->irq = irq;
158 #endif
159 
160  dev->board_name = thisboard->name;
161 
162  ret = comedi_alloc_subdevices(dev, 1);
163  if (ret)
164  return ret;
165 
166  /* 8255 dio */
167  s = &dev->subdevices[0];
168  subdev_8255_init(dev, s, NULL, dev->iobase);
169 
170  return 0;
171 };
172 
173 static void dio24_detach(struct comedi_device *dev)
174 {
175  struct comedi_subdevice *s;
176 
177  if (dev->subdevices) {
178  s = &dev->subdevices[0];
179  subdev_8255_cleanup(dev, s);
180  }
181  if (thisboard->bustype != pcmcia_bustype && dev->iobase)
183  if (dev->irq)
184  free_irq(dev->irq, dev);
185 };
186 
187 static void dio24_config(struct pcmcia_device *link);
188 static void dio24_release(struct pcmcia_device *link);
189 static int dio24_cs_suspend(struct pcmcia_device *p_dev);
190 static int dio24_cs_resume(struct pcmcia_device *p_dev);
191 
192 static int dio24_cs_attach(struct pcmcia_device *);
193 static void dio24_cs_detach(struct pcmcia_device *);
194 
195 struct local_info_t {
196  struct pcmcia_device *link;
197  int stop;
198  struct bus_operations *bus;
199 };
200 
201 static int dio24_cs_attach(struct pcmcia_device *link)
202 {
203  struct local_info_t *local;
204 
205  printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO - CS-attach!\n");
206 
207  dev_dbg(&link->dev, "dio24_cs_attach()\n");
208 
209  /* Allocate space for private device-specific data */
210  local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
211  if (!local)
212  return -ENOMEM;
213  local->link = link;
214  link->priv = local;
215 
216  pcmcia_cur_dev = link;
217 
218  dio24_config(link);
219 
220  return 0;
221 } /* dio24_cs_attach */
222 
223 static void dio24_cs_detach(struct pcmcia_device *link)
224 {
225  ((struct local_info_t *)link->priv)->stop = 1;
226  dio24_release(link);
227 
228  /* This points to the parent local_info_t struct */
229  kfree(link->priv);
230 }
231 
232 static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev,
233  void *priv_data)
234 {
235  if (p_dev->config_index == 0)
236  return -EINVAL;
237 
238  return pcmcia_request_io(p_dev);
239 }
240 
241 static void dio24_config(struct pcmcia_device *link)
242 {
243  int ret;
244 
245  printk(KERN_INFO "ni_daq_dio24: HOLA SOY YO! - config\n");
246 
247  dev_dbg(&link->dev, "dio24_config\n");
248 
249  link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_AUDIO |
250  CONF_AUTO_SET_IO;
251 
252  ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, NULL);
253  if (ret) {
254  dev_warn(&link->dev, "no configuration found\n");
255  goto failed;
256  }
257 
258  if (!link->irq)
259  goto failed;
260 
261  ret = pcmcia_enable_device(link);
262  if (ret)
263  goto failed;
264 
265  return;
266 
267 failed:
268  printk(KERN_INFO "Fallo");
269  dio24_release(link);
270 
271 } /* dio24_config */
272 
273 static void dio24_release(struct pcmcia_device *link)
274 {
275  dev_dbg(&link->dev, "dio24_release\n");
276 
277  pcmcia_disable_device(link);
278 } /* dio24_release */
279 
280 static int dio24_cs_suspend(struct pcmcia_device *link)
281 {
282  struct local_info_t *local = link->priv;
283 
284  /* Mark the device as stopped, to block IO until later */
285  local->stop = 1;
286  return 0;
287 } /* dio24_cs_suspend */
288 
289 static int dio24_cs_resume(struct pcmcia_device *link)
290 {
291  struct local_info_t *local = link->priv;
292 
293  local->stop = 0;
294  return 0;
295 } /* dio24_cs_resume */
296 
297 /*====================================================================*/
298 
299 static const struct pcmcia_device_id dio24_cs_ids[] = {
300  /* N.B. These IDs should match those in dio24_boards */
301  PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c), /* daqcard-dio24 */
302  PCMCIA_DEVICE_NULL
303 };
304 
305 MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
306 MODULE_AUTHOR("Daniel Vecino Castel <[email protected]>");
307 MODULE_DESCRIPTION("Comedi driver for National Instruments "
308  "PCMCIA DAQ-Card DIO-24");
309 MODULE_LICENSE("GPL");
310 
311 static struct pcmcia_driver dio24_cs_driver = {
312  .probe = dio24_cs_attach,
313  .remove = dio24_cs_detach,
314  .suspend = dio24_cs_suspend,
315  .resume = dio24_cs_resume,
316  .id_table = dio24_cs_ids,
317  .owner = THIS_MODULE,
318  .name = "ni_daq_dio24",
319 };
320 
321 static int __init init_dio24_cs(void)
322 {
323  printk("ni_daq_dio24: HOLA SOY YO!\n");
324  pcmcia_register_driver(&dio24_cs_driver);
325  return 0;
326 }
327 
328 static void __exit exit_dio24_cs(void)
329 {
330  pcmcia_unregister_driver(&dio24_cs_driver);
331 }
332 
334 {
335  int ret;
336 
337  ret = init_dio24_cs();
338  if (ret < 0)
339  return ret;
340 
341  return comedi_driver_register(&driver_dio24);
342 }
343 
345 {
346  exit_dio24_cs();
347  comedi_driver_unregister(&driver_dio24);
348 }