adl_pci6208.c revision e27b81d5bbe275ed8edfc2f61c6dbf50711abbfb
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	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x6208) },
94	{ 0 }
95};
96
97MODULE_DEVICE_TABLE(pci, pci6208_pci_table);
98
99/* Will be initialized in pci6208_find device(). */
100#define thisboard ((const struct pci6208_board *)dev->board_ptr)
101
102struct pci6208_private {
103	int data;
104	struct pci_dev *pci_dev;	/* for a PCI device */
105	unsigned int ao_readback[2];	/* Used for AO readback */
106};
107
108#define devpriv ((struct pci6208_private *)dev->private)
109
110static int pci6208_attach(struct comedi_device *dev,
111			  struct comedi_devconfig *it);
112static int pci6208_detach(struct comedi_device *dev);
113
114static struct comedi_driver driver_pci6208 = {
115	.driver_name = PCI6208_DRIVER_NAME,
116	.module = THIS_MODULE,
117	.attach = pci6208_attach,
118	.detach = pci6208_detach,
119};
120
121static int __devinit driver_pci6208_pci_probe(struct pci_dev *dev,
122					      const struct pci_device_id *ent)
123{
124	return comedi_pci_auto_config(dev, driver_pci6208.driver_name);
125}
126
127static void __devexit driver_pci6208_pci_remove(struct pci_dev *dev)
128{
129	comedi_pci_auto_unconfig(dev);
130}
131
132static struct pci_driver driver_pci6208_pci_driver = {
133	.id_table = pci6208_pci_table,
134	.probe = &driver_pci6208_pci_probe,
135	.remove = __devexit_p(&driver_pci6208_pci_remove)
136};
137
138static int __init driver_pci6208_init_module(void)
139{
140	int retval;
141
142	retval = comedi_driver_register(&driver_pci6208);
143	if (retval < 0)
144		return retval;
145
146	driver_pci6208_pci_driver.name = (char *)driver_pci6208.driver_name;
147	return pci_register_driver(&driver_pci6208_pci_driver);
148}
149
150static void __exit driver_pci6208_cleanup_module(void)
151{
152	pci_unregister_driver(&driver_pci6208_pci_driver);
153	comedi_driver_unregister(&driver_pci6208);
154}
155
156module_init(driver_pci6208_init_module);
157module_exit(driver_pci6208_cleanup_module);
158
159static int pci6208_find_device(struct comedi_device *dev, int bus, int slot);
160static int
161pci6208_pci_setup(struct pci_dev *pci_dev, unsigned long *io_base_ptr,
162		  int dev_minor);
163
164/*read/write functions*/
165static int pci6208_ao_winsn(struct comedi_device *dev,
166			    struct comedi_subdevice *s,
167			    struct comedi_insn *insn, unsigned int *data);
168static int pci6208_ao_rinsn(struct comedi_device *dev,
169			    struct comedi_subdevice *s,
170			    struct comedi_insn *insn, unsigned int *data);
171/* static int pci6208_dio_insn_bits (struct comedi_device *dev,
172 *					struct comedi_subdevice *s, */
173/* struct comedi_insn *insn,unsigned int *data); */
174/* static int pci6208_dio_insn_config(struct comedi_device *dev,
175 *					struct comedi_subdevice *s, */
176/* struct comedi_insn *insn,unsigned int *data); */
177
178/*
179 * Attach is called by the Comedi core to configure the driver
180 * for a particular board.  If you specified a board_name array
181 * in the driver structure, dev->board_ptr contains that
182 * address.
183 */
184static int pci6208_attach(struct comedi_device *dev,
185			  struct comedi_devconfig *it)
186{
187	struct comedi_subdevice *s;
188	int retval;
189	unsigned long io_base;
190
191	printk(KERN_INFO "comedi%d: pci6208: ", dev->minor);
192
193	retval = alloc_private(dev, sizeof(struct pci6208_private));
194	if (retval < 0)
195		return retval;
196
197	retval = pci6208_find_device(dev, it->options[0], it->options[1]);
198	if (retval < 0)
199		return retval;
200
201	retval = pci6208_pci_setup(devpriv->pci_dev, &io_base, dev->minor);
202	if (retval < 0)
203		return retval;
204
205	dev->iobase = io_base;
206	dev->board_name = thisboard->name;
207
208/*
209 * Allocate the subdevice structures.  alloc_subdevice() is a
210 * convenient macro defined in comedidev.h.
211 */
212	if (alloc_subdevices(dev, 2) < 0)
213		return -ENOMEM;
214
215	s = dev->subdevices + 0;
216	/* analog output subdevice */
217	s->type = COMEDI_SUBD_AO;
218	s->subdev_flags = SDF_WRITABLE;	/* anything else to add here?? */
219	s->n_chan = thisboard->ao_chans;
220	s->maxdata = 0xffff;	/* 16-bit DAC */
221	s->range_table = &range_bipolar10;	/* this needs to be checked. */
222	s->insn_write = pci6208_ao_winsn;
223	s->insn_read = pci6208_ao_rinsn;
224
225	/* s=dev->subdevices+1; */
226	/* digital i/o subdevice */
227	/* s->type=COMEDI_SUBD_DIO; */
228	/* s->subdev_flags=SDF_READABLE|SDF_WRITABLE; */
229	/* s->n_chan=16; */
230	/* s->maxdata=1; */
231	/* s->range_table=&range_digital; */
232	/* s->insn_bits = pci6208_dio_insn_bits; */
233	/* s->insn_config = pci6208_dio_insn_config; */
234
235	printk(KERN_INFO "attached\n");
236
237	return 1;
238}
239
240/*
241 * _detach is called to deconfigure a device.  It should deallocate
242 * resources.
243 * This function is also called when _attach() fails, so it should be
244 * careful not to release resources that were not necessarily
245 * allocated by _attach().  dev->private and dev->subdevices are
246 * deallocated automatically by the core.
247 */
248static int pci6208_detach(struct comedi_device *dev)
249{
250	printk(KERN_INFO "comedi%d: pci6208: remove\n", dev->minor);
251
252	if (devpriv && devpriv->pci_dev) {
253		if (dev->iobase)
254			comedi_pci_disable(devpriv->pci_dev);
255		pci_dev_put(devpriv->pci_dev);
256	}
257
258	return 0;
259}
260
261static int pci6208_ao_winsn(struct comedi_device *dev,
262			    struct comedi_subdevice *s,
263			    struct comedi_insn *insn, unsigned int *data)
264{
265	int i = 0, Data_Read;
266	unsigned short chan = CR_CHAN(insn->chanspec);
267	unsigned long invert = 1 << (16 - 1);
268	unsigned long out_value;
269	/* Writing a list of values to an AO channel is probably not
270	 * very useful, but that's how the interface is defined. */
271	for (i = 0; i < insn->n; i++) {
272		out_value = data[i] ^ invert;
273		/* a typical programming sequence */
274		do {
275			Data_Read = (inw(dev->iobase) & 1);
276		} while (Data_Read);
277		outw(out_value, dev->iobase + (0x02 * chan));
278		devpriv->ao_readback[chan] = out_value;
279	}
280
281	/* return the number of samples read/written */
282	return i;
283}
284
285/* AO subdevices should have a read insn as well as a write insn.
286 * Usually this means copying a value stored in devpriv. */
287static int pci6208_ao_rinsn(struct comedi_device *dev,
288			    struct comedi_subdevice *s,
289			    struct comedi_insn *insn, unsigned int *data)
290{
291	int i;
292	int chan = CR_CHAN(insn->chanspec);
293
294	for (i = 0; i < insn->n; i++)
295		data[i] = devpriv->ao_readback[chan];
296
297	return i;
298}
299
300/* DIO devices are slightly special.  Although it is possible to
301 * implement the insn_read/insn_write interface, it is much more
302 * useful to applications if you implement the insn_bits interface.
303 * This allows packed reading/writing of the DIO channels.  The
304 * comedi core can convert between insn_bits and insn_read/write */
305/* static int pci6208_dio_insn_bits(struct comedi_device *dev,
306 *					struct comedi_subdevice *s, */
307/* struct comedi_insn *insn,unsigned int *data) */
308/* { */
309/* if(insn->n!=2)return -EINVAL; */
310
311	/* The insn data is a mask in data[0] and the new data
312	 * in data[1], each channel cooresponding to a bit. */
313/* if(data[0]){ */
314/* s->state &= ~data[0]; */
315/* s->state |= data[0]&data[1]; */
316		/* Write out the new digital output lines */
317		/* outw(s->state,dev->iobase + SKEL_DIO); */
318/* } */
319
320	/* on return, data[1] contains the value of the digital
321	 * input and output lines. */
322	/* data[1]=inw(dev->iobase + SKEL_DIO); */
323	/* or we could just return the software copy of the output values if
324	 * it was a purely digital output subdevice */
325	/* data[1]=s->state; */
326
327/* return 2; */
328/* } */
329
330/* static int pci6208_dio_insn_config(struct comedi_device *dev,
331 *					struct comedi_subdevice *s, */
332/* struct comedi_insn *insn,unsigned int *data) */
333/* { */
334/* int chan=CR_CHAN(insn->chanspec); */
335
336	/* The input or output configuration of each digital line is
337	 * configured by a special insn_config instruction.  chanspec
338	 * contains the channel to be changed, and data[0] contains the
339	 * value COMEDI_INPUT or COMEDI_OUTPUT. */
340
341/* if(data[0]==COMEDI_OUTPUT){ */
342/* s->io_bits |= 1<<chan; */
343/* }else{ */
344/* s->io_bits &= ~(1<<chan); */
345/* } */
346	/* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
347
348/* return 1; */
349/* } */
350
351static int pci6208_find_device(struct comedi_device *dev, int bus, int slot)
352{
353	struct pci_dev *pci_dev = NULL;
354	int i;
355
356	for_each_pci_dev(pci_dev) {
357		if (pci_dev->vendor == PCI_VENDOR_ID_ADLINK) {
358			for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
359				if (pci6208_boards[i].dev_id ==
360					pci_dev->device) {
361					/*
362					 * was a particular bus/slot requested?
363					*/
364					if ((bus != 0) || (slot != 0)) {
365						/*
366						 * are we on the
367						 * wrong bus/slot?
368						*/
369						if (pci_dev->bus->number
370						    != bus ||
371						    PCI_SLOT(pci_dev->devfn)
372						    != slot) {
373							continue;
374						}
375					}
376					dev->board_ptr = pci6208_boards + i;
377					goto found;
378				}
379			}
380		}
381	}
382
383	printk(KERN_ERR "comedi%d: no supported board found! "
384			"(req. bus/slot : %d/%d)\n",
385			dev->minor, bus, slot);
386	return -EIO;
387
388found:
389	printk("comedi%d: found %s (b:s:f=%d:%d:%d) , irq=%d\n",
390	       dev->minor,
391	       pci6208_boards[i].name,
392	       pci_dev->bus->number,
393	       PCI_SLOT(pci_dev->devfn),
394	       PCI_FUNC(pci_dev->devfn), pci_dev->irq);
395
396	/*  TODO: Warn about non-tested boards. */
397	/* switch(board->device_id) */
398	/* { */
399	/* }; */
400
401	devpriv->pci_dev = pci_dev;
402
403	return 0;
404}
405
406static int
407pci6208_pci_setup(struct pci_dev *pci_dev, unsigned long *io_base_ptr,
408		  int dev_minor)
409{
410	unsigned long io_base, io_range, lcr_io_base, lcr_io_range;
411
412	/*  Enable PCI device and request regions */
413	if (comedi_pci_enable(pci_dev, PCI6208_DRIVER_NAME) < 0) {
414		printk(KERN_ERR "comedi%d: Failed to enable PCI device "
415			"and request regions\n",
416			dev_minor);
417		return -EIO;
418	}
419	/* Read local configuration register
420	 * base address [PCI_BASE_ADDRESS #1].
421	 */
422	lcr_io_base = pci_resource_start(pci_dev, 1);
423	lcr_io_range = pci_resource_len(pci_dev, 1);
424
425	printk(KERN_INFO "comedi%d: local config registers at address"
426			" 0x%4lx [0x%4lx]\n",
427			dev_minor, lcr_io_base, lcr_io_range);
428
429	/*  Read PCI6208 register base address [PCI_BASE_ADDRESS #2]. */
430	io_base = pci_resource_start(pci_dev, 2);
431	io_range = pci_resource_end(pci_dev, 2) - io_base + 1;
432
433	printk("comedi%d: 6208 registers at address 0x%4lx [0x%4lx]\n",
434	       dev_minor, io_base, io_range);
435
436	*io_base_ptr = io_base;
437	/* devpriv->io_range = io_range; */
438	/* devpriv->is_valid=0; */
439	/* devpriv->lcr_io_base=lcr_io_base; */
440	/* devpriv->lcr_io_range=lcr_io_range; */
441
442	return 0;
443}
444
445MODULE_AUTHOR("Comedi http://www.comedi.org");
446MODULE_DESCRIPTION("Comedi low-level driver");
447MODULE_LICENSE("GPL");
448