ni_daq_dio24.c revision 25602bd7350dc180128376db9772277818f041f2
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
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
55static struct pcmcia_device *pcmcia_cur_dev;
56
57#define DIO24_SIZE 4		/*  size of io region used by board */
58
59enum dio24_bustype { pcmcia_bustype };
60
61struct dio24_board_struct {
62	const char *name;
63	int device_id;		/*  device id for pcmcia board */
64	enum dio24_bustype bustype;	/*  PCMCIA */
65	int have_dio;		/*  have 8255 chip */
66	/*  function pointers so we can use inb/outb or readb/writeb as appropriate */
67	unsigned int (*read_byte) (unsigned int address);
68	void (*write_byte) (unsigned int byte, unsigned int address);
69};
70
71static const struct dio24_board_struct dio24_boards[] = {
72	{
73	 .name = "daqcard-dio24",
74	 .device_id = 0x475c,	/*  0x10b is manufacturer id, 0x475c is device id */
75	 .bustype = pcmcia_bustype,
76	 .have_dio = 1,
77	 },
78	{
79	 .name = "ni_daq_dio24",
80	 .device_id = 0x475c,	/*  0x10b is manufacturer id, 0x475c is device id */
81	 .bustype = pcmcia_bustype,
82	 .have_dio = 1,
83	 },
84};
85
86/*
87 * Useful for shorthand access to the particular board structure
88 */
89#define thisboard ((const struct dio24_board_struct *)dev->board_ptr)
90
91struct dio24_private {
92
93	int data;		/* number of data points left to be taken */
94};
95
96static int dio24_attach(struct comedi_device *dev, struct comedi_devconfig *it)
97{
98	struct dio24_private *devpriv;
99	struct comedi_subdevice *s;
100	unsigned long iobase = 0;
101#ifdef incomplete
102	unsigned int irq = 0;
103#endif
104	struct pcmcia_device *link;
105	int ret;
106
107	devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
108	if (!devpriv)
109		return -ENOMEM;
110	dev->private = devpriv;
111
112	/*  get base address, irq etc. based on bustype */
113	switch (thisboard->bustype) {
114	case pcmcia_bustype:
115		link = pcmcia_cur_dev;	/* XXX hack */
116		if (!link)
117			return -EIO;
118		iobase = link->resource[0]->start;
119#ifdef incomplete
120		irq = link->irq;
121#endif
122		break;
123	default:
124		pr_err("bug! couldn't determine board type\n");
125		return -EINVAL;
126		break;
127	}
128	pr_debug("comedi%d: ni_daq_dio24: %s, io 0x%lx", dev->minor,
129		 thisboard->name, iobase);
130#ifdef incomplete
131	if (irq)
132		pr_debug("irq %u\n", irq);
133#endif
134
135	if (iobase == 0) {
136		pr_err("io base address is zero!\n");
137		return -EINVAL;
138	}
139
140	dev->iobase = iobase;
141
142#ifdef incomplete
143	/* grab our IRQ */
144	dev->irq = irq;
145#endif
146
147	dev->board_name = thisboard->name;
148
149	ret = comedi_alloc_subdevices(dev, 1);
150	if (ret)
151		return ret;
152
153	/* 8255 dio */
154	s = &dev->subdevices[0];
155	subdev_8255_init(dev, s, NULL, dev->iobase);
156
157	return 0;
158};
159
160static void dio24_detach(struct comedi_device *dev)
161{
162	struct comedi_subdevice *s;
163
164	if (dev->subdevices) {
165		s = &dev->subdevices[0];
166		subdev_8255_cleanup(dev, s);
167	}
168	if (thisboard->bustype != pcmcia_bustype && dev->iobase)
169		release_region(dev->iobase, DIO24_SIZE);
170	if (dev->irq)
171		free_irq(dev->irq, dev);
172};
173
174static struct comedi_driver driver_dio24 = {
175	.driver_name	= "ni_daq_dio24",
176	.module		= THIS_MODULE,
177	.attach		= dio24_attach,
178	.detach		= dio24_detach,
179	.num_names	= ARRAY_SIZE(dio24_boards),
180	.board_name	= &dio24_boards[0].name,
181	.offset		= sizeof(struct dio24_board_struct),
182};
183
184static int dio24_pcmcia_config_loop(struct pcmcia_device *p_dev,
185				    void *priv_data)
186{
187	if (p_dev->config_index == 0)
188		return -EINVAL;
189
190	return pcmcia_request_io(p_dev);
191}
192
193static int dio24_cs_attach(struct pcmcia_device *link)
194{
195	int ret;
196
197	link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_AUDIO |
198		CONF_AUTO_SET_IO;
199
200	ret = pcmcia_loop_config(link, dio24_pcmcia_config_loop, NULL);
201	if (ret) {
202		dev_warn(&link->dev, "no configuration found\n");
203		goto failed;
204	}
205
206	if (!link->irq)
207		goto failed;
208
209	ret = pcmcia_enable_device(link);
210	if (ret)
211		goto failed;
212
213	pcmcia_cur_dev = link;
214
215	return 0;
216
217failed:
218	pcmcia_disable_device(link);
219	return ret;
220}
221
222static void dio24_cs_detach(struct pcmcia_device *link)
223{
224	pcmcia_disable_device(link);
225}
226
227static const struct pcmcia_device_id dio24_cs_ids[] = {
228	/* N.B. These IDs should match those in dio24_boards */
229	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x475c),	/* daqcard-dio24 */
230	PCMCIA_DEVICE_NULL
231};
232MODULE_DEVICE_TABLE(pcmcia, dio24_cs_ids);
233
234static struct pcmcia_driver dio24_cs_driver = {
235	.name		= "ni_daq_dio24",
236	.owner		= THIS_MODULE,
237	.id_table	= dio24_cs_ids,
238	.probe		= dio24_cs_attach,
239	.remove		= dio24_cs_detach,
240};
241module_comedi_pcmcia_driver(driver_dio24, dio24_cs_driver);
242
243MODULE_AUTHOR("Daniel Vecino Castel <dvecino@able.es>");
244MODULE_DESCRIPTION(
245	"Comedi driver for National Instruments PCMCIA DAQ-Card DIO-24");
246MODULE_LICENSE("GPL");
247