adl_pci6208.c revision 90f703d30dd3e0c16ff80f35e34e511385a05ad5
1/*
2    comedi/drivers/adl_pci6208.c
3
4    Hardware driver for ADLink 6208 series cards:
5	card	     | voltage output    | current output
6	-------------+-------------------+---------------
7	PCI-6208V    |  8 channels       | -
8	PCI-6216V    | 16 channels       | -
9	PCI-6208A    |  8 channels       | 8 channels
10
11    COMEDI - Linux Control and Measurement Device Interface
12    Copyright (C) 2000 David A. Schleef <ds@schleef.org>
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 2 of the License, or
17    (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27*/
28/*
29Driver: adl_pci6208
30Description: ADLink PCI-6208A
31Devices: [ADLink] PCI-6208A (adl_pci6208)
32Author: nsyeow <nsyeow@pd.jaring.my>
33Updated: Fri, 30 Jan 2004 14:44:27 +0800
34Status: untested
35
36Configuration Options:
37  none
38
39References:
40	- ni_660x.c
41	- adl_pci9111.c		copied the entire pci setup section
42	- adl_pci9118.c
43*/
44/*
45 * These headers should be followed by a blank line, and any comments
46 * you wish to say about the driver.  The comment area is the place
47 * to put any known bugs, limitations, unsupported features, supported
48 * command triggers, whether or not commands are supported on particular
49 * subdevices, etc.
50 *
51 * Somewhere in the comment should be information about configuration
52 * options that are used with comedi_config.
53 */
54#include "../comedidev.h"
55#include "comedi_pci.h"
56
57#define PCI6208_DRIVER_NAME	"adl_pci6208"
58
59/* Board descriptions */
60struct pci6208_board {
61	const char *name;
62	unsigned short dev_id;	/* `lspci` will show you this */
63	int ao_chans;
64	/* int ao_bits; */
65};
66
67static const struct pci6208_board pci6208_boards[] = {
68	/*{
69	   .name = "pci6208v",
70	   .dev_id = 0x6208,      // not sure
71	   .ao_chans = 8
72	   // , .ao_bits = 16
73	   },
74	   {
75	   .name = "pci6216v",
76	   .dev_id = 0x6208,      // not sure
77	   .ao_chans = 16
78	   // , .ao_bits = 16
79	   }, */
80	{
81	 .name = "pci6208a",
82	 .dev_id = 0x6208,
83	 .ao_chans = 8
84	 /* ,    .ao_bits = 16 */
85	 }
86};
87
88/* This is used by modprobe to translate PCI IDs to drivers.  Should
89 * only be used for PCI and ISA-PnP devices */
90static DEFINE_PCI_DEVICE_TABLE(pci6208_pci_table) = {
91	/* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
92	/* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
93	{
94	PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
95	0}
96};
97
98MODULE_DEVICE_TABLE(pci, pci6208_pci_table);
99
100/* Will be initialized in pci6208_find device(). */
101#define thisboard ((const struct pci6208_board *)dev->board_ptr)
102
103struct pci6208_private {
104	int data;
105	struct pci_dev *pci_dev;	/* for a PCI device */
106	unsigned int ao_readback[2];	/* Used for AO readback */
107};
108
109#define devpriv ((struct pci6208_private *)dev->private)
110
111static int pci6208_attach(struct comedi_device *dev,
112			  struct comedi_devconfig *it);
113static int pci6208_detach(struct comedi_device *dev);
114
115static struct comedi_driver driver_pci6208 = {
116	.driver_name = PCI6208_DRIVER_NAME,
117	.module = THIS_MODULE,
118	.attach = pci6208_attach,
119	.detach = pci6208_detach,
120};
121
122COMEDI_PCI_INITCLEANUP(driver_pci6208, pci6208_pci_table);
123
124static int pci6208_find_device(struct comedi_device *dev, int bus, int slot);
125static int
126pci6208_pci_setup(struct pci_dev *pci_dev, unsigned long *io_base_ptr,
127		  int dev_minor);
128
129/*read/write functions*/
130static int pci6208_ao_winsn(struct comedi_device *dev,
131			    struct comedi_subdevice *s,
132			    struct comedi_insn *insn, unsigned int *data);
133static int pci6208_ao_rinsn(struct comedi_device *dev,
134			    struct comedi_subdevice *s,
135			    struct comedi_insn *insn, unsigned int *data);
136/* static int pci6208_dio_insn_bits (struct comedi_device *dev,
137 *					struct comedi_subdevice *s, */
138/* struct comedi_insn *insn,unsigned int *data); */
139/* static int pci6208_dio_insn_config(struct comedi_device *dev,
140 *					struct comedi_subdevice *s, */
141/* struct comedi_insn *insn,unsigned int *data); */
142
143/*
144 * Attach is called by the Comedi core to configure the driver
145 * for a particular board.  If you specified a board_name array
146 * in the driver structure, dev->board_ptr contains that
147 * address.
148 */
149static int pci6208_attach(struct comedi_device *dev,
150			  struct comedi_devconfig *it)
151{
152	struct comedi_subdevice *s;
153	int retval;
154	unsigned long io_base;
155
156	printk(KERN_INFO "comedi%d: pci6208: ", dev->minor);
157
158	retval = alloc_private(dev, sizeof(struct pci6208_private));
159	if (retval < 0)
160		return retval;
161
162	retval = pci6208_find_device(dev, it->options[0], it->options[1]);
163	if (retval < 0)
164		return retval;
165
166	retval = pci6208_pci_setup(devpriv->pci_dev, &io_base, dev->minor);
167	if (retval < 0)
168		return retval;
169
170	dev->iobase = io_base;
171	dev->board_name = thisboard->name;
172
173/*
174 * Allocate the subdevice structures.  alloc_subdevice() is a
175 * convenient macro defined in comedidev.h.
176 */
177	if (alloc_subdevices(dev, 2) < 0)
178		return -ENOMEM;
179
180	s = dev->subdevices + 0;
181	/* analog output subdevice */
182	s->type = COMEDI_SUBD_AO;
183	s->subdev_flags = SDF_WRITABLE;	/* anything else to add here?? */
184	s->n_chan = thisboard->ao_chans;
185	s->maxdata = 0xffff;	/* 16-bit DAC */
186	s->range_table = &range_bipolar10;	/* this needs to be checked. */
187	s->insn_write = pci6208_ao_winsn;
188	s->insn_read = pci6208_ao_rinsn;
189
190	/* s=dev->subdevices+1; */
191	/* digital i/o subdevice */
192	/* s->type=COMEDI_SUBD_DIO; */
193	/* s->subdev_flags=SDF_READABLE|SDF_WRITABLE; */
194	/* s->n_chan=16; */
195	/* s->maxdata=1; */
196	/* s->range_table=&range_digital; */
197	/* s->insn_bits = pci6208_dio_insn_bits; */
198	/* s->insn_config = pci6208_dio_insn_config; */
199
200	printk(KERN_INFO "attached\n");
201
202	return 1;
203}
204
205/*
206 * _detach is called to deconfigure a device.  It should deallocate
207 * resources.
208 * This function is also called when _attach() fails, so it should be
209 * careful not to release resources that were not necessarily
210 * allocated by _attach().  dev->private and dev->subdevices are
211 * deallocated automatically by the core.
212 */
213static int pci6208_detach(struct comedi_device *dev)
214{
215	printk(KERN_INFO "comedi%d: pci6208: remove\n", dev->minor);
216
217	if (devpriv && devpriv->pci_dev) {
218		if (dev->iobase)
219			comedi_pci_disable(devpriv->pci_dev);
220		pci_dev_put(devpriv->pci_dev);
221	}
222
223	return 0;
224}
225
226static int pci6208_ao_winsn(struct comedi_device *dev,
227			    struct comedi_subdevice *s,
228			    struct comedi_insn *insn, unsigned int *data)
229{
230	int i = 0, Data_Read;
231	unsigned short chan = CR_CHAN(insn->chanspec);
232	unsigned long invert = 1 << (16 - 1);
233	unsigned long out_value;
234	/* Writing a list of values to an AO channel is probably not
235	 * very useful, but that's how the interface is defined. */
236	for (i = 0; i < insn->n; i++) {
237		out_value = data[i] ^ invert;
238		/* a typical programming sequence */
239		do {
240			Data_Read = (inw(dev->iobase) & 1);
241		} while (Data_Read);
242		outw(out_value, dev->iobase + (0x02 * chan));
243		devpriv->ao_readback[chan] = out_value;
244	}
245
246	/* return the number of samples read/written */
247	return i;
248}
249
250/* AO subdevices should have a read insn as well as a write insn.
251 * Usually this means copying a value stored in devpriv. */
252static int pci6208_ao_rinsn(struct comedi_device *dev,
253			    struct comedi_subdevice *s,
254			    struct comedi_insn *insn, unsigned int *data)
255{
256	int i;
257	int chan = CR_CHAN(insn->chanspec);
258
259	for (i = 0; i < insn->n; i++)
260		data[i] = devpriv->ao_readback[chan];
261
262	return i;
263}
264
265/* DIO devices are slightly special.  Although it is possible to
266 * implement the insn_read/insn_write interface, it is much more
267 * useful to applications if you implement the insn_bits interface.
268 * This allows packed reading/writing of the DIO channels.  The
269 * comedi core can convert between insn_bits and insn_read/write */
270/* static int pci6208_dio_insn_bits(struct comedi_device *dev,
271 *					struct comedi_subdevice *s, */
272/* struct comedi_insn *insn,unsigned int *data) */
273/* { */
274/* if(insn->n!=2)return -EINVAL; */
275
276	/* The insn data is a mask in data[0] and the new data
277	 * in data[1], each channel cooresponding to a bit. */
278/* if(data[0]){ */
279/* s->state &= ~data[0]; */
280/* s->state |= data[0]&data[1]; */
281		/* Write out the new digital output lines */
282		/* outw(s->state,dev->iobase + SKEL_DIO); */
283/* } */
284
285	/* on return, data[1] contains the value of the digital
286	 * input and output lines. */
287	/* data[1]=inw(dev->iobase + SKEL_DIO); */
288	/* or we could just return the software copy of the output values if
289	 * it was a purely digital output subdevice */
290	/* data[1]=s->state; */
291
292/* return 2; */
293/* } */
294
295/* static int pci6208_dio_insn_config(struct comedi_device *dev,
296 *					struct comedi_subdevice *s, */
297/* struct comedi_insn *insn,unsigned int *data) */
298/* { */
299/* int chan=CR_CHAN(insn->chanspec); */
300
301	/* The input or output configuration of each digital line is
302	 * configured by a special insn_config instruction.  chanspec
303	 * contains the channel to be changed, and data[0] contains the
304	 * value COMEDI_INPUT or COMEDI_OUTPUT. */
305
306/* if(data[0]==COMEDI_OUTPUT){ */
307/* s->io_bits |= 1<<chan; */
308/* }else{ */
309/* s->io_bits &= ~(1<<chan); */
310/* } */
311	/* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
312
313/* return 1; */
314/* } */
315
316static int pci6208_find_device(struct comedi_device *dev, int bus, int slot)
317{
318	struct pci_dev *pci_dev;
319	int i;
320
321	for (pci_dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
322	     pci_dev != NULL;
323	     pci_dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) {
324		if (pci_dev->vendor == PCI_VENDOR_ID_ADLINK) {
325			for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
326				if (pci6208_boards[i].dev_id ==
327					pci_dev->device) {
328					/*
329					 * was a particular bus/slot requested?
330					*/
331					if ((bus != 0) || (slot != 0)) {
332						/*
333						 * are we on the
334						 * wrong bus/slot?
335						*/
336						if (pci_dev->bus->number
337						    != bus ||
338						    PCI_SLOT(pci_dev->devfn)
339						    != slot) {
340							continue;
341						}
342					}
343					dev->board_ptr = pci6208_boards + i;
344					goto found;
345				}
346			}
347		}
348	}
349
350	printk(KERN_ERR "comedi%d: no supported board found! "
351			"(req. bus/slot : %d/%d)\n",
352			dev->minor, bus, slot);
353	return -EIO;
354
355found:
356	printk("comedi%d: found %s (b:s:f=%d:%d:%d) , irq=%d\n",
357	       dev->minor,
358	       pci6208_boards[i].name,
359	       pci_dev->bus->number,
360	       PCI_SLOT(pci_dev->devfn),
361	       PCI_FUNC(pci_dev->devfn), pci_dev->irq);
362
363	/*  TODO: Warn about non-tested boards. */
364	/* switch(board->device_id) */
365	/* { */
366	/* }; */
367
368	devpriv->pci_dev = pci_dev;
369
370	return 0;
371}
372
373static int
374pci6208_pci_setup(struct pci_dev *pci_dev, unsigned long *io_base_ptr,
375		  int dev_minor)
376{
377	unsigned long io_base, io_range, lcr_io_base, lcr_io_range;
378
379	/*  Enable PCI device and request regions */
380	if (comedi_pci_enable(pci_dev, PCI6208_DRIVER_NAME) < 0) {
381		printk(KERN_ERR "comedi%d: Failed to enable PCI device "
382			"and request regions\n",
383			dev_minor);
384		return -EIO;
385	}
386	/* Read local configuration register
387	 * base address [PCI_BASE_ADDRESS #1].
388	 */
389	lcr_io_base = pci_resource_start(pci_dev, 1);
390	lcr_io_range = pci_resource_len(pci_dev, 1);
391
392	printk(KERN_INFO "comedi%d: local config registers at address"
393			" 0x%4lx [0x%4lx]\n",
394			dev_minor, lcr_io_base, lcr_io_range);
395
396	/*  Read PCI6208 register base address [PCI_BASE_ADDRESS #2]. */
397	io_base = pci_resource_start(pci_dev, 2);
398	io_range = pci_resource_end(pci_dev, 2) - io_base + 1;
399
400	printk("comedi%d: 6208 registers at address 0x%4lx [0x%4lx]\n",
401	       dev_minor, io_base, io_range);
402
403	*io_base_ptr = io_base;
404	/* devpriv->io_range = io_range; */
405	/* devpriv->is_valid=0; */
406	/* devpriv->lcr_io_base=lcr_io_base; */
407	/* devpriv->lcr_io_range=lcr_io_range; */
408
409	return 0;
410}
411
412MODULE_AUTHOR("Comedi http://www.comedi.org");
413MODULE_DESCRIPTION("Comedi low-level driver");
414MODULE_LICENSE("GPL");
415