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