cb_pcimdda.c revision 71d4c80db1e042baae6c5a2ca1bbfc16c89bd2b5
1/* 2 comedi/drivers/cb_pcimdda.c 3 Computer Boards PCIM-DDA06-16 Comedi driver 4 Author: Calin Culianu <calin@ajvar.org> 5 6 COMEDI - Linux Control and Measurement Device Interface 7 Copyright (C) 2000 David A. Schleef <ds@schleef.org> 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 23*/ 24/* 25Driver: cb_pcimdda 26Description: Measurement Computing PCIM-DDA06-16 27Devices: [Measurement Computing] PCIM-DDA06-16 (cb_pcimdda) 28Author: Calin Culianu <calin@ajvar.org> 29Updated: Mon, 14 Apr 2008 15:15:51 +0100 30Status: works 31 32All features of the PCIM-DDA06-16 board are supported. This board 33has 6 16-bit AO channels, and the usual 8255 DIO setup. (24 channels, 34configurable in banks of 8 and 4, etc.). This board does not support commands. 35 36The board has a peculiar way of specifying AO gain/range settings -- You have 371 jumper bank on the card, which either makes all 6 AO channels either 385 Volt unipolar, 5V bipolar, 10 Volt unipolar or 10V bipolar. 39 40Since there is absolutely _no_ way to tell in software how this jumper is set 41(well, at least according to the rather thin spec. from Measurement Computing 42 that comes with the board), the driver assumes the jumper is at its factory 43default setting of +/-5V. 44 45Also of note is the fact that this board features another jumper, whose 46state is also completely invisible to software. It toggles two possible AO 47output modes on the board: 48 49 - Update Mode: Writing to an AO channel instantaneously updates the actual 50 signal output by the DAC on the board (this is the factory default). 51 - Simultaneous XFER Mode: Writing to an AO channel has no effect until 52 you read from any one of the AO channels. This is useful for loading 53 all 6 AO values, and then reading from any one of the AO channels on the 54 device to instantly update all 6 AO values in unison. Useful for some 55 control apps, I would assume? If your jumper is in this setting, then you 56 need to issue your comedi_data_write()s to load all the values you want, 57 then issue one comedi_data_read() on any channel on the AO subdevice 58 to initiate the simultaneous XFER. 59 60Configuration Options: 61 [0] PCI bus (optional) 62 [1] PCI slot (optional) 63 [2] analog output range jumper setting 64 0 == +/- 5 V 65 1 == +/- 10 V 66*/ 67 68/* 69 This is a driver for the Computer Boards PCIM-DDA06-16 Analog Output 70 card. This board has a unique register layout and as such probably 71 deserves its own driver file. 72 73 It is theoretically possible to integrate this board into the cb_pcidda 74 file, but since that isn't my code, I didn't want to significantly 75 modify that file to support this board (I thought it impolite to do so). 76 77 At any rate, if you feel ambitious, please feel free to take 78 the code out of this file and combine it with a more unified driver 79 file. 80 81 I would like to thank Timothy Curry <Timothy.Curry@rdec.redstone.army.mil> 82 for lending me a board so that I could write this driver. 83 84 -Calin Culianu <calin@ajvar.org> 85 */ 86 87#include "../comedidev.h" 88 89#include "comedi_pci.h" 90 91#include "8255.h" 92 93/* device ids of the cards we support -- currently only 1 card supported */ 94#define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307 95#define PCI_ID_PCIM_DDA06_16 0x0053 96 97/* 98 * This is straight from skel.c -- I did this in case this source file 99 * will someday support more than 1 board... 100 */ 101struct board_struct { 102 const char *name; 103 unsigned short device_id; 104 int ao_chans; 105 int ao_bits; 106 int dio_chans; 107 int dio_method; 108 int dio_offset; /* how many bytes into the BADR are the DIO ports */ 109 int regs_badrindex; /* IO Region for the control, analog output, 110 and DIO registers */ 111 int reg_sz; /* number of bytes of registers in io region */ 112}; 113 114enum DIO_METHODS { 115 DIO_NONE = 0, 116 DIO_8255, 117 DIO_INTERNAL /* unimplemented */ 118}; 119 120static const struct board_struct boards[] = { 121 { 122 .name = "cb_pcimdda06-16", 123 .device_id = PCI_ID_PCIM_DDA06_16, 124 .ao_chans = 6, 125 .ao_bits = 16, 126 .dio_chans = 24, 127 .dio_method = DIO_8255, 128 .dio_offset = 12, 129 .regs_badrindex = 3, 130 .reg_sz = 16, 131 } 132}; 133 134/* 135 * Useful for shorthand access to the particular board structure 136 */ 137#define thisboard ((const struct board_struct *)dev->board_ptr) 138 139#define REG_SZ (thisboard->reg_sz) 140#define REGS_BADRINDEX (thisboard->regs_badrindex) 141 142/* This is used by modprobe to translate PCI IDs to drivers. Should 143 * only be used for PCI and ISA-PnP devices */ 144/* Please add your PCI vendor ID to comedidev.h, and it will be forwarded 145 * upstream. */ 146static DEFINE_PCI_DEVICE_TABLE(pci_table) = { 147 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTERBOARDS, PCI_ID_PCIM_DDA06_16) }, 148 {0} 149}; 150 151MODULE_DEVICE_TABLE(pci, pci_table); 152 153/* this structure is for data unique to this hardware driver. If 154 several hardware drivers keep similar information in this structure, 155 feel free to suggest moving the variable to the struct comedi_device struct. */ 156struct board_private_struct { 157 unsigned long registers; /* set by probe */ 158 unsigned long dio_registers; 159 char attached_to_8255; /* boolean */ 160 char attached_successfully; /* boolean */ 161 /* would be useful for a PCI device */ 162 struct pci_dev *pci_dev; 163 164#define MAX_AO_READBACK_CHANNELS 6 165 /* Used for AO readback */ 166 unsigned int ao_readback[MAX_AO_READBACK_CHANNELS]; 167 168}; 169 170/* 171 * most drivers define the following macro to make it easy to 172 * access the private structure. 173 */ 174#define devpriv ((struct board_private_struct *)dev->private) 175 176/* 177 * The struct comedi_driver structure tells the Comedi core module 178 * which functions to call to configure/deconfigure (attach/detach) 179 * the board, and also about the kernel module that contains 180 * the device code. 181 */ 182static int attach(struct comedi_device *dev, struct comedi_devconfig *it); 183static int detach(struct comedi_device *dev); 184static struct comedi_driver cb_pcimdda_driver = { 185 .driver_name = "cb_pcimdda", 186 .module = THIS_MODULE, 187 .attach = attach, 188 .detach = detach, 189}; 190 191MODULE_AUTHOR("Calin A. Culianu <calin@rtlab.org>"); 192MODULE_DESCRIPTION("Comedi low-level driver for the Computerboards PCIM-DDA " 193 "series. Currently only supports PCIM-DDA06-16 (which " 194 "also happens to be the only board in this series. :) ) "); 195MODULE_LICENSE("GPL"); 196static int __devinit cb_pcimdda_driver_pci_probe(struct pci_dev *dev, 197 const struct pci_device_id 198 *ent) 199{ 200 return comedi_pci_auto_config(dev, cb_pcimdda_driver.driver_name); 201} 202 203static void __devexit cb_pcimdda_driver_pci_remove(struct pci_dev *dev) 204{ 205 comedi_pci_auto_unconfig(dev); 206} 207 208static struct pci_driver cb_pcimdda_driver_pci_driver = { 209 .id_table = pci_table, 210 .probe = &cb_pcimdda_driver_pci_probe, 211 .remove = __devexit_p(&cb_pcimdda_driver_pci_remove) 212}; 213 214static int __init cb_pcimdda_driver_init_module(void) 215{ 216 int retval; 217 218 retval = comedi_driver_register(&cb_pcimdda_driver); 219 if (retval < 0) 220 return retval; 221 222 cb_pcimdda_driver_pci_driver.name = 223 (char *)cb_pcimdda_driver.driver_name; 224 return pci_register_driver(&cb_pcimdda_driver_pci_driver); 225} 226 227static void __exit cb_pcimdda_driver_cleanup_module(void) 228{ 229 pci_unregister_driver(&cb_pcimdda_driver_pci_driver); 230 comedi_driver_unregister(&cb_pcimdda_driver); 231} 232 233module_init(cb_pcimdda_driver_init_module); 234module_exit(cb_pcimdda_driver_cleanup_module); 235 236static int ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s, 237 struct comedi_insn *insn, unsigned int *data); 238static int ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s, 239 struct comedi_insn *insn, unsigned int *data); 240 241/*--------------------------------------------------------------------------- 242 HELPER FUNCTION DECLARATIONS 243-----------------------------------------------------------------------------*/ 244 245/* returns a maxdata value for a given n_bits */ 246static inline unsigned int figure_out_maxdata(int bits) 247{ 248 return ((unsigned int)1 << bits) - 1; 249} 250 251/* 252 * Probes for a supported device. 253 * 254 * Prerequisite: private be allocated already inside dev 255 * 256 * If the device is found, it returns 0 and has the following side effects: 257 * 258 * o assigns a struct pci_dev * to dev->private->pci_dev 259 * o assigns a struct board * to dev->board_ptr 260 * o sets dev->private->registers 261 * o sets dev->private->dio_registers 262 * 263 * Otherwise, returns a -errno on error 264 */ 265static int probe(struct comedi_device *dev, const struct comedi_devconfig *it); 266 267/*--------------------------------------------------------------------------- 268 FUNCTION DEFINITIONS 269-----------------------------------------------------------------------------*/ 270 271/* 272 * Attach is called by the Comedi core to configure the driver 273 * for a particular board. If you specified a board_name array 274 * in the driver structure, dev->board_ptr contains that 275 * address. 276 */ 277static int attach(struct comedi_device *dev, struct comedi_devconfig *it) 278{ 279 struct comedi_subdevice *s; 280 int err; 281 282/* 283 * Allocate the private structure area. alloc_private() is a 284 * convenient macro defined in comedidev.h. 285 * if this function fails (returns negative) then the private area is 286 * kfree'd by comedi 287 */ 288 if (alloc_private(dev, sizeof(struct board_private_struct)) < 0) 289 return -ENOMEM; 290 291/* 292 * If you can probe the device to determine what device in a series 293 * it is, this is the place to do it. Otherwise, dev->board_ptr 294 * should already be initialized. 295 */ 296 err = probe(dev, it); 297 if (err) 298 return err; 299 300/* Output some info */ 301 printk("comedi%d: %s: ", dev->minor, thisboard->name); 302 303/* 304 * Initialize dev->board_name. Note that we can use the "thisboard" 305 * macro now, since we just initialized it in the last line. 306 */ 307 dev->board_name = thisboard->name; 308 309/* 310 * Allocate the subdevice structures. alloc_subdevice() is a 311 * convenient macro defined in comedidev.h. 312 */ 313 if (alloc_subdevices(dev, 2) < 0) 314 return -ENOMEM; 315 316 s = dev->subdevices + 0; 317 318 /* analog output subdevice */ 319 s->type = COMEDI_SUBD_AO; 320 s->subdev_flags = SDF_WRITABLE | SDF_READABLE; 321 s->n_chan = thisboard->ao_chans; 322 s->maxdata = figure_out_maxdata(thisboard->ao_bits); 323 /* this is hard-coded here */ 324 if (it->options[2]) 325 s->range_table = &range_bipolar10; 326 else 327 s->range_table = &range_bipolar5; 328 s->insn_write = &ao_winsn; 329 s->insn_read = &ao_rinsn; 330 331 s = dev->subdevices + 1; 332 /* digital i/o subdevice */ 333 if (thisboard->dio_chans) { 334 switch (thisboard->dio_method) { 335 case DIO_8255: 336 /* this is a straight 8255, so register us with the 8255 driver */ 337 subdev_8255_init(dev, s, NULL, devpriv->dio_registers); 338 devpriv->attached_to_8255 = 1; 339 break; 340 case DIO_INTERNAL: 341 default: 342 printk("DIO_INTERNAL not implemented yet!\n"); 343 return -ENXIO; 344 break; 345 } 346 } else { 347 s->type = COMEDI_SUBD_UNUSED; 348 } 349 350 devpriv->attached_successfully = 1; 351 352 printk("attached\n"); 353 354 return 1; 355} 356 357/* 358 * _detach is called to deconfigure a device. It should deallocate 359 * resources. 360 * This function is also called when _attach() fails, so it should be 361 * careful not to release resources that were not necessarily 362 * allocated by _attach(). dev->private and dev->subdevices are 363 * deallocated automatically by the core. 364 */ 365static int detach(struct comedi_device *dev) 366{ 367 if (devpriv) { 368 369 if (dev->subdevices && devpriv->attached_to_8255) { 370 /* de-register us from the 8255 driver */ 371 subdev_8255_cleanup(dev, dev->subdevices + 2); 372 devpriv->attached_to_8255 = 0; 373 } 374 375 if (devpriv->pci_dev) { 376 if (devpriv->registers) 377 comedi_pci_disable(devpriv->pci_dev); 378 pci_dev_put(devpriv->pci_dev); 379 } 380 381 if (devpriv->attached_successfully && thisboard) 382 printk("comedi%d: %s: detached\n", dev->minor, 383 thisboard->name); 384 385 } 386 387 return 0; 388} 389 390static int ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s, 391 struct comedi_insn *insn, unsigned int *data) 392{ 393 int i; 394 int chan = CR_CHAN(insn->chanspec); 395 unsigned long offset = devpriv->registers + chan * 2; 396 397 /* Writing a list of values to an AO channel is probably not 398 * very useful, but that's how the interface is defined. */ 399 for (i = 0; i < insn->n; i++) { 400 /* first, load the low byte */ 401 outb((char)(data[i] & 0x00ff), offset); 402 /* next, write the high byte -- only after this is written is 403 the channel voltage updated in the DAC, unless 404 we're in simultaneous xfer mode (jumper on card) 405 then a rinsn is necessary to actually update the DAC -- 406 see ao_rinsn() below... */ 407 outb((char)(data[i] >> 8 & 0x00ff), offset + 1); 408 409 /* for testing only.. the actual rinsn SHOULD do an inw! 410 (see the stuff about simultaneous XFER mode on this board) */ 411 devpriv->ao_readback[chan] = data[i]; 412 } 413 414 /* return the number of samples read/written */ 415 return i; 416} 417 418/* AO subdevices should have a read insn as well as a write insn. 419 420 Usually this means copying a value stored in devpriv->ao_readback. 421 However, since this board has this jumper setting called "Simultaneous 422 Xfer mode" (off by default), we will support it. Simultaneaous xfer 423 mode is accomplished by loading ALL the values you want for AO in all the 424 channels, then READing off one of the AO registers to initiate the 425 instantaneous simultaneous update of all DAC outputs, which makes 426 all AO channels update simultaneously. This is useful for some control 427 applications, I would imagine. 428*/ 429static int ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s, 430 struct comedi_insn *insn, unsigned int *data) 431{ 432 int i; 433 int chan = CR_CHAN(insn->chanspec); 434 435 for (i = 0; i < insn->n; i++) { 436 inw(devpriv->registers + chan * 2); 437 /* should I set data[i] to the result of the actual read on the register 438 or the cached unsigned int in devpriv->ao_readback[]? */ 439 data[i] = devpriv->ao_readback[chan]; 440 } 441 442 return i; 443} 444 445/*--------------------------------------------------------------------------- 446 HELPER FUNCTION DEFINITIONS 447-----------------------------------------------------------------------------*/ 448 449/* 450 * Probes for a supported device. 451 * 452 * Prerequisite: private be allocated already inside dev 453 * 454 * If the device is found, it returns 0 and has the following side effects: 455 * 456 * o assigns a struct pci_dev * to dev->private->pci_dev 457 * o assigns a struct board * to dev->board_ptr 458 * o sets dev->private->registers 459 * o sets dev->private->dio_registers 460 * 461 * Otherwise, returns a -errno on error 462 */ 463static int probe(struct comedi_device *dev, const struct comedi_devconfig *it) 464{ 465 struct pci_dev *pcidev = NULL; 466 int index; 467 unsigned long registers; 468 469 for_each_pci_dev(pcidev) { 470 /* is it not a computer boards card? */ 471 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS) 472 continue; 473 /* loop through cards supported by this driver */ 474 for (index = 0; index < ARRAY_SIZE(boards); index++) { 475 if (boards[index].device_id != pcidev->device) 476 continue; 477 /* was a particular bus/slot requested? */ 478 if (it->options[0] || it->options[1]) { 479 /* are we on the wrong bus/slot? */ 480 if (pcidev->bus->number != it->options[0] || 481 PCI_SLOT(pcidev->devfn) != it->options[1]) { 482 continue; 483 } 484 } 485 /* found ! */ 486 487 devpriv->pci_dev = pcidev; 488 dev->board_ptr = boards + index; 489 if (comedi_pci_enable(pcidev, thisboard->name)) { 490 printk 491 ("cb_pcimdda: Failed to enable PCI device and request regions\n"); 492 return -EIO; 493 } 494 registers = 495 pci_resource_start(devpriv->pci_dev, 496 REGS_BADRINDEX); 497 devpriv->registers = registers; 498 devpriv->dio_registers 499 = devpriv->registers + thisboard->dio_offset; 500 return 0; 501 } 502 } 503 504 printk("cb_pcimdda: No supported ComputerBoards/MeasurementComputing " 505 "card found at the requested position\n"); 506 return -ENODEV; 507} 508