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