adl_pci6208.c revision 949a18d39d75e92f516d562befe5447d368ab67d
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
56/* Board descriptions */
57struct pci6208_board {
58	const char *name;
59	unsigned short dev_id;	/* `lspci` will show you this */
60	int ao_chans;
61	/* int ao_bits; */
62};
63
64static const struct pci6208_board pci6208_boards[] = {
65	/*{
66	   .name = "pci6208v",
67	   .dev_id = 0x6208,      // not sure
68	   .ao_chans = 8
69	   // , .ao_bits = 16
70	   },
71	   {
72	   .name = "pci6216v",
73	   .dev_id = 0x6208,      // not sure
74	   .ao_chans = 16
75	   // , .ao_bits = 16
76	   }, */
77	{
78	 .name = "pci6208a",
79	 .dev_id = 0x6208,
80	 .ao_chans = 8
81	 /* ,    .ao_bits = 16 */
82	 }
83};
84
85struct pci6208_private {
86	int data;
87	struct pci_dev *pci_dev;	/* for a PCI device */
88	unsigned int ao_readback[2];	/* Used for AO readback */
89};
90
91static int pci6208_ao_winsn(struct comedi_device *dev,
92			    struct comedi_subdevice *s,
93			    struct comedi_insn *insn, unsigned int *data)
94{
95	struct pci6208_private *devpriv = dev->private;
96	int i = 0, Data_Read;
97	unsigned short chan = CR_CHAN(insn->chanspec);
98	unsigned long invert = 1 << (16 - 1);
99	unsigned long out_value;
100	/* Writing a list of values to an AO channel is probably not
101	 * very useful, but that's how the interface is defined. */
102	for (i = 0; i < insn->n; i++) {
103		out_value = data[i] ^ invert;
104		/* a typical programming sequence */
105		do {
106			Data_Read = (inw(dev->iobase) & 1);
107		} while (Data_Read);
108		outw(out_value, dev->iobase + (0x02 * chan));
109		devpriv->ao_readback[chan] = out_value;
110	}
111
112	/* return the number of samples read/written */
113	return i;
114}
115
116/* AO subdevices should have a read insn as well as a write insn.
117 * Usually this means copying a value stored in devpriv. */
118static int pci6208_ao_rinsn(struct comedi_device *dev,
119			    struct comedi_subdevice *s,
120			    struct comedi_insn *insn, unsigned int *data)
121{
122	struct pci6208_private *devpriv = dev->private;
123	int i;
124	int chan = CR_CHAN(insn->chanspec);
125
126	for (i = 0; i < insn->n; i++)
127		data[i] = devpriv->ao_readback[chan];
128
129	return i;
130}
131
132/* DIO devices are slightly special.  Although it is possible to
133 * implement the insn_read/insn_write interface, it is much more
134 * useful to applications if you implement the insn_bits interface.
135 * This allows packed reading/writing of the DIO channels.  The
136 * comedi core can convert between insn_bits and insn_read/write */
137/* static int pci6208_dio_insn_bits(struct comedi_device *dev,
138 *					struct comedi_subdevice *s, */
139/* struct comedi_insn *insn,unsigned int *data) */
140/* { */
141	/* The insn data is a mask in data[0] and the new data
142	 * in data[1], each channel cooresponding to a bit. */
143/* if(data[0]){ */
144/* s->state &= ~data[0]; */
145/* s->state |= data[0]&data[1]; */
146		/* Write out the new digital output lines */
147		/* outw(s->state,dev->iobase + SKEL_DIO); */
148/* } */
149
150	/* on return, data[1] contains the value of the digital
151	 * input and output lines. */
152	/* data[1]=inw(dev->iobase + SKEL_DIO); */
153	/* or we could just return the software copy of the output values if
154	 * it was a purely digital output subdevice */
155	/* data[1]=s->state; */
156
157/* return insn->n; */
158/* } */
159
160/* static int pci6208_dio_insn_config(struct comedi_device *dev,
161 *					struct comedi_subdevice *s, */
162/* struct comedi_insn *insn,unsigned int *data) */
163/* { */
164/* int chan=CR_CHAN(insn->chanspec); */
165
166	/* The input or output configuration of each digital line is
167	 * configured by a special insn_config instruction.  chanspec
168	 * contains the channel to be changed, and data[0] contains the
169	 * value COMEDI_INPUT or COMEDI_OUTPUT. */
170
171/* if(data[0]==COMEDI_OUTPUT){ */
172/* s->io_bits |= 1<<chan; */
173/* }else{ */
174/* s->io_bits &= ~(1<<chan); */
175/* } */
176	/* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
177
178/* return 1; */
179/* } */
180
181static int pci6208_find_device(struct comedi_device *dev, int bus, int slot)
182{
183	struct pci6208_private *devpriv = dev->private;
184	struct pci_dev *pci_dev = NULL;
185	int i;
186
187	for_each_pci_dev(pci_dev) {
188		if (pci_dev->vendor == PCI_VENDOR_ID_ADLINK) {
189			for (i = 0; i < ARRAY_SIZE(pci6208_boards); i++) {
190				if (pci6208_boards[i].dev_id ==
191					pci_dev->device) {
192					/*
193					 * was a particular bus/slot requested?
194					*/
195					if ((bus != 0) || (slot != 0)) {
196						/*
197						 * are we on the
198						 * wrong bus/slot?
199						*/
200						if (pci_dev->bus->number
201						    != bus ||
202						    PCI_SLOT(pci_dev->devfn)
203						    != slot) {
204							continue;
205						}
206					}
207					dev->board_ptr = pci6208_boards + i;
208					goto found;
209				}
210			}
211		}
212	}
213
214	printk(KERN_ERR "comedi%d: no supported board found! "
215			"(req. bus/slot : %d/%d)\n",
216			dev->minor, bus, slot);
217	return -EIO;
218
219found:
220	printk("comedi%d: found %s (b:s:f=%d:%d:%d) , irq=%d\n",
221	       dev->minor,
222	       pci6208_boards[i].name,
223	       pci_dev->bus->number,
224	       PCI_SLOT(pci_dev->devfn),
225	       PCI_FUNC(pci_dev->devfn), pci_dev->irq);
226
227	/*  TODO: Warn about non-tested boards. */
228	/* switch(board->device_id) */
229	/* { */
230	/* }; */
231
232	devpriv->pci_dev = pci_dev;
233
234	return 0;
235}
236
237static int
238pci6208_pci_setup(struct pci_dev *pci_dev, unsigned long *io_base_ptr,
239		  int dev_minor)
240{
241	unsigned long io_base, io_range, lcr_io_base, lcr_io_range;
242
243	/*  Enable PCI device and request regions */
244	if (comedi_pci_enable(pci_dev, "adl_pci6208") < 0) {
245		printk(KERN_ERR "comedi%d: Failed to enable PCI device "
246			"and request regions\n",
247			dev_minor);
248		return -EIO;
249	}
250	/* Read local configuration register
251	 * base address [PCI_BASE_ADDRESS #1].
252	 */
253	lcr_io_base = pci_resource_start(pci_dev, 1);
254	lcr_io_range = pci_resource_len(pci_dev, 1);
255
256	printk(KERN_INFO "comedi%d: local config registers at address"
257			" 0x%4lx [0x%4lx]\n",
258			dev_minor, lcr_io_base, lcr_io_range);
259
260	/*  Read PCI6208 register base address [PCI_BASE_ADDRESS #2]. */
261	io_base = pci_resource_start(pci_dev, 2);
262	io_range = pci_resource_end(pci_dev, 2) - io_base + 1;
263
264	printk("comedi%d: 6208 registers at address 0x%4lx [0x%4lx]\n",
265	       dev_minor, io_base, io_range);
266
267	*io_base_ptr = io_base;
268	/* devpriv->io_range = io_range; */
269	/* devpriv->is_valid=0; */
270	/* devpriv->lcr_io_base=lcr_io_base; */
271	/* devpriv->lcr_io_range=lcr_io_range; */
272
273	return 0;
274}
275
276static int pci6208_attach(struct comedi_device *dev,
277			  struct comedi_devconfig *it)
278{
279	const struct pci6208_board *thisboard;
280	struct pci6208_private *devpriv;
281	struct comedi_subdevice *s;
282	int retval;
283	unsigned long io_base;
284
285	printk(KERN_INFO "comedi%d: pci6208: ", dev->minor);
286
287	retval = alloc_private(dev, sizeof(*devpriv));
288	if (retval < 0)
289		return retval;
290	devpriv = dev->private;
291
292	retval = pci6208_find_device(dev, it->options[0], it->options[1]);
293	if (retval < 0)
294		return retval;
295	thisboard = comedi_board(dev);
296
297	retval = pci6208_pci_setup(devpriv->pci_dev, &io_base, dev->minor);
298	if (retval < 0)
299		return retval;
300
301	dev->iobase = io_base;
302	dev->board_name = thisboard->name;
303
304	retval = comedi_alloc_subdevices(dev, 2);
305	if (retval)
306		return retval;
307
308	s = dev->subdevices + 0;
309	/* analog output subdevice */
310	s->type = COMEDI_SUBD_AO;
311	s->subdev_flags = SDF_WRITABLE;	/* anything else to add here?? */
312	s->n_chan = thisboard->ao_chans;
313	s->maxdata = 0xffff;	/* 16-bit DAC */
314	s->range_table = &range_bipolar10;	/* this needs to be checked. */
315	s->insn_write = pci6208_ao_winsn;
316	s->insn_read = pci6208_ao_rinsn;
317
318	/* s=dev->subdevices+1; */
319	/* digital i/o subdevice */
320	/* s->type=COMEDI_SUBD_DIO; */
321	/* s->subdev_flags=SDF_READABLE|SDF_WRITABLE; */
322	/* s->n_chan=16; */
323	/* s->maxdata=1; */
324	/* s->range_table=&range_digital; */
325	/* s->insn_bits = pci6208_dio_insn_bits; */
326	/* s->insn_config = pci6208_dio_insn_config; */
327
328	printk(KERN_INFO "attached\n");
329
330	return 1;
331}
332
333static void pci6208_detach(struct comedi_device *dev)
334{
335	struct pci6208_private *devpriv = dev->private;
336
337	if (devpriv && devpriv->pci_dev) {
338		if (dev->iobase)
339			comedi_pci_disable(devpriv->pci_dev);
340		pci_dev_put(devpriv->pci_dev);
341	}
342}
343
344static struct comedi_driver adl_pci6208_driver = {
345	.driver_name	= "adl_pci6208",
346	.module		= THIS_MODULE,
347	.attach		= pci6208_attach,
348	.detach		= pci6208_detach,
349};
350
351static int __devinit adl_pci6208_pci_probe(struct pci_dev *dev,
352					   const struct pci_device_id *ent)
353{
354	return comedi_pci_auto_config(dev, &adl_pci6208_driver);
355}
356
357static void __devexit adl_pci6208_pci_remove(struct pci_dev *dev)
358{
359	comedi_pci_auto_unconfig(dev);
360}
361
362/* This is used by modprobe to translate PCI IDs to drivers.  Should
363 * only be used for PCI and ISA-PnP devices */
364static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
365	/* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
366	/* { PCI_VENDOR_ID_ADLINK, 0x6208, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, */
367	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x6208) },
368	{ 0 }
369};
370MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
371
372static struct pci_driver adl_pci6208_pci_driver = {
373	.name		= "adl_pci6208",
374	.id_table	= adl_pci6208_pci_table,
375	.probe		= adl_pci6208_pci_probe,
376	.remove		= __devexit_p(adl_pci6208_pci_remove),
377};
378module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
379
380MODULE_AUTHOR("Comedi http://www.comedi.org");
381MODULE_DESCRIPTION("Comedi low-level driver");
382MODULE_LICENSE("GPL");
383