| 1 | /*************************************************************************** |
| 2 | * * |
| 3 | * comedi/drivers/unioxx5.c * |
| 4 | * Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards. * |
| 5 | * * |
| 6 | * Copyright (C) 2006 Kruchinin Daniil (asgard) [asgard@etersoft.ru] * |
| 7 | * * |
| 8 | * COMEDI - Linux Control and Measurement Device Interface * |
| 9 | * Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org> * |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | * This program is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 19 | * GNU General Public License for more details. * |
| 20 | * * |
| 21 | * You should have received a copy of the GNU General Public License * |
| 22 | * along with this program; if not, write to the Free Software * |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 24 | * * |
| 25 | ***************************************************************************/ |
| 26 | /* |
| 27 | |
| 28 | Driver: unioxx5 |
| 29 | Description: Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards. |
| 30 | Author: Kruchinin Daniil (asgard) <asgard@etersoft.ru> |
| 31 | Status: unknown |
| 32 | Updated: 2006-10-09 |
| 33 | Devices: [Fastwel] UNIOxx-5 (unioxx5), |
| 34 | |
| 35 | This card supports digital and analog I/O. It written for g01 |
| 36 | subdevices only. |
| 37 | channels range: 0 .. 23 dio channels |
| 38 | and 0 .. 11 analog modules range |
| 39 | During attaching unioxx5 module displays modules identifiers |
| 40 | (see dmesg after comedi_config) in format: |
| 41 | | [module_number] module_id | |
| 42 | |
| 43 | */ |
| 44 | |
| 45 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 46 | |
| 47 | #include "../comedidev.h" |
| 48 | #include <linux/ioport.h> |
| 49 | #include <linux/slab.h> |
| 50 | |
| 51 | #define DRIVER_NAME "unioxx5" |
| 52 | #define UNIOXX5_SIZE 0x10 |
| 53 | #define UNIOXX5_SUBDEV_BASE 0xA000 /* base addr of first subdev */ |
| 54 | #define UNIOXX5_SUBDEV_ODDS 0x400 |
| 55 | |
| 56 | /* modules types */ |
| 57 | #define MODULE_DIGITAL 0 |
| 58 | #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */ |
| 59 | |
| 60 | /* constants for digital i/o */ |
| 61 | #define UNIOXX5_NUM_OF_CHANS 24 |
| 62 | |
| 63 | /* constants for analog i/o */ |
| 64 | #define TxBE 0x10 /* transmit buffer enable */ |
| 65 | #define RxCA 0x20 /* 1 receive character available */ |
| 66 | #define Rx2CA 0x40 /* 2 receive character available */ |
| 67 | #define Rx4CA 0x80 /* 4 receive character available */ |
| 68 | |
| 69 | /* bytes mask errors */ |
| 70 | #define Rx2CA_ERR_MASK 0x04 /* 2 bytes receiving error */ |
| 71 | #define Rx4CA_ERR_MASK 0x08 /* 4 bytes receiving error */ |
| 72 | |
| 73 | /* channel modes */ |
| 74 | #define ALL_2_INPUT 0 /* config all digital channels to input */ |
| 75 | #define ALL_2_OUTPUT 1 /* config all digital channels to output */ |
| 76 | |
| 77 | /* 'private' structure for each subdevice */ |
| 78 | struct unioxx5_subd_priv { |
| 79 | int usp_iobase; |
| 80 | /* 12 modules. each can be 70L or 73L */ |
| 81 | unsigned char usp_module_type[12]; |
| 82 | /* for saving previous written value for analog modules */ |
| 83 | unsigned char usp_extra_data[12][4]; |
| 84 | unsigned char usp_prev_wr_val[3]; /* previous written value */ |
| 85 | unsigned char usp_prev_cn_val[3]; /* previous channel value */ |
| 86 | }; |
| 87 | |
| 88 | static int __unioxx5_define_chan_offset(int chan_num) |
| 89 | { |
| 90 | |
| 91 | if (chan_num < 0 || chan_num > 23) |
| 92 | return -1; |
| 93 | |
| 94 | return (chan_num >> 3) + 1; |
| 95 | } |
| 96 | |
| 97 | #if 0 /* not used? */ |
| 98 | static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode) |
| 99 | { |
| 100 | int i, mask; |
| 101 | |
| 102 | mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00; |
| 103 | printk("COMEDI: mode = %d\n", mask); |
| 104 | |
| 105 | outb(1, usp->usp_iobase + 0); |
| 106 | |
| 107 | for (i = 0; i < 3; i++) |
| 108 | outb(mask, usp->usp_iobase + i); |
| 109 | |
| 110 | outb(0, usp->usp_iobase + 0); |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | /* configure channels for analog i/o (even to output, odd to input) */ |
| 115 | static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel) |
| 116 | { |
| 117 | int chan_a, chan_b, conf, channel_offset; |
| 118 | |
| 119 | channel_offset = __unioxx5_define_chan_offset(channel); |
| 120 | conf = usp->usp_prev_cn_val[channel_offset - 1]; |
| 121 | chan_a = chan_b = 1; |
| 122 | |
| 123 | /* setting channel A and channel B mask */ |
| 124 | if (channel % 2 == 0) { |
| 125 | chan_a <<= channel & 0x07; |
| 126 | chan_b <<= (channel + 1) & 0x07; |
| 127 | } else { |
| 128 | chan_a <<= (channel - 1) & 0x07; |
| 129 | chan_b <<= channel & 0x07; |
| 130 | } |
| 131 | |
| 132 | conf |= chan_a; /* even channel ot output */ |
| 133 | conf &= ~chan_b; /* odd channel to input */ |
| 134 | |
| 135 | outb(1, usp->usp_iobase + 0); |
| 136 | outb(conf, usp->usp_iobase + channel_offset); |
| 137 | outb(0, usp->usp_iobase + 0); |
| 138 | |
| 139 | usp->usp_prev_cn_val[channel_offset - 1] = conf; |
| 140 | } |
| 141 | |
| 142 | static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp, |
| 143 | unsigned int *data, int channel, int minor) |
| 144 | { |
| 145 | int channel_offset, mask = 1 << (channel & 0x07); |
| 146 | |
| 147 | channel_offset = __unioxx5_define_chan_offset(channel); |
| 148 | if (channel_offset < 0) { |
| 149 | pr_err("comedi%d: undefined channel %d. channel range is 0 .. 23\n", |
| 150 | minor, channel); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | *data = inb(usp->usp_iobase + channel_offset); |
| 155 | *data &= mask; |
| 156 | |
| 157 | /* correct the read value to 0 or 1 */ |
| 158 | if (channel_offset > 1) |
| 159 | channel -= 2 << channel_offset; |
| 160 | *data >>= channel; |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp, |
| 165 | unsigned int *data, int channel, int minor) |
| 166 | { |
| 167 | int module_no, read_ch; |
| 168 | char control; |
| 169 | |
| 170 | module_no = channel / 2; |
| 171 | read_ch = channel % 2; /* depend on type of channel (A or B) */ |
| 172 | |
| 173 | /* defining if given module can work on input */ |
| 174 | if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) { |
| 175 | pr_err("comedi%d: module in position %d with id 0x%02x is for output only", |
| 176 | minor, module_no, usp->usp_module_type[module_no]); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | __unioxx5_analog_config(usp, channel); |
| 181 | /* sends module number to card(1 .. 12) */ |
| 182 | outb(module_no + 1, usp->usp_iobase + 5); |
| 183 | outb('V', usp->usp_iobase + 6); /* sends to module (V)erify command */ |
| 184 | control = inb(usp->usp_iobase); /* get control register byte */ |
| 185 | |
| 186 | /* waits while reading four bytes will be allowed */ |
| 187 | while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA)) |
| 188 | ; |
| 189 | |
| 190 | /* if four bytes readding error occurs - return 0(false) */ |
| 191 | if ((control & Rx4CA_ERR_MASK)) { |
| 192 | printk("COMEDI: 4 bytes error\n"); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | if (read_ch) |
| 197 | *data = inw(usp->usp_iobase + 6); /* channel B */ |
| 198 | else |
| 199 | *data = inw(usp->usp_iobase + 4); /* channel A */ |
| 200 | |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp, |
| 205 | unsigned int *data, int channel, int minor) |
| 206 | { |
| 207 | int channel_offset, val; |
| 208 | int mask = 1 << (channel & 0x07); |
| 209 | |
| 210 | channel_offset = __unioxx5_define_chan_offset(channel); |
| 211 | if (channel_offset < 0) { |
| 212 | pr_err("comedi%d: undefined channel %d. channel range is 0 .. 23\n", |
| 213 | minor, channel); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* getting previous written value */ |
| 218 | val = usp->usp_prev_wr_val[channel_offset - 1]; |
| 219 | |
| 220 | if (*data) |
| 221 | val |= mask; |
| 222 | else |
| 223 | val &= ~mask; |
| 224 | |
| 225 | outb(val, usp->usp_iobase + channel_offset); |
| 226 | /* saving new written value */ |
| 227 | usp->usp_prev_wr_val[channel_offset - 1] = val; |
| 228 | |
| 229 | return 1; |
| 230 | } |
| 231 | |
| 232 | static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp, |
| 233 | unsigned int *data, int channel, int minor) |
| 234 | { |
| 235 | int module, i; |
| 236 | |
| 237 | module = channel / 2; /* definig module number(0 .. 11) */ |
| 238 | i = (channel % 2) << 1; /* depends on type of channel (A or B) */ |
| 239 | |
| 240 | /* defining if given module can work on output */ |
| 241 | if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) { |
| 242 | pr_err("comedi%d: module in position %d with id 0x%0x is for input only!\n", |
| 243 | minor, module, usp->usp_module_type[module]); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | __unioxx5_analog_config(usp, channel); |
| 248 | /* saving minor byte */ |
| 249 | usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF); |
| 250 | /* saving major byte */ |
| 251 | usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8); |
| 252 | |
| 253 | /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */ |
| 254 | /* sending module number to card(1 .. 12) */ |
| 255 | outb(module + 1, usp->usp_iobase + 5); |
| 256 | outb('W', usp->usp_iobase + 6); /* sends (W)rite command to module */ |
| 257 | |
| 258 | /* sending for bytes to module(one byte per cycle iteration) */ |
| 259 | for (i = 0; i < 4; i++) { |
| 260 | while (!((inb(usp->usp_iobase + 0)) & TxBE)) |
| 261 | ; /* waits while writting will be allowed */ |
| 262 | outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6); |
| 263 | } |
| 264 | |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | static int unioxx5_subdev_read(struct comedi_device *dev, |
| 269 | struct comedi_subdevice *subdev, |
| 270 | struct comedi_insn *insn, unsigned int *data) |
| 271 | { |
| 272 | struct unioxx5_subd_priv *usp = subdev->private; |
| 273 | int channel, type; |
| 274 | |
| 275 | channel = CR_CHAN(insn->chanspec); |
| 276 | /* defining module type(analog or digital) */ |
| 277 | type = usp->usp_module_type[channel / 2]; |
| 278 | |
| 279 | if (type == MODULE_DIGITAL) { |
| 280 | if (!__unioxx5_digital_read(usp, data, channel, dev->minor)) |
| 281 | return -1; |
| 282 | } else { |
| 283 | if (!__unioxx5_analog_read(usp, data, channel, dev->minor)) |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | static int unioxx5_subdev_write(struct comedi_device *dev, |
| 291 | struct comedi_subdevice *subdev, |
| 292 | struct comedi_insn *insn, unsigned int *data) |
| 293 | { |
| 294 | struct unioxx5_subd_priv *usp = subdev->private; |
| 295 | int channel, type; |
| 296 | |
| 297 | channel = CR_CHAN(insn->chanspec); |
| 298 | /* defining module type(analog or digital) */ |
| 299 | type = usp->usp_module_type[channel / 2]; |
| 300 | |
| 301 | if (type == MODULE_DIGITAL) { |
| 302 | if (!__unioxx5_digital_write(usp, data, channel, dev->minor)) |
| 303 | return -1; |
| 304 | } else { |
| 305 | if (!__unioxx5_analog_write(usp, data, channel, dev->minor)) |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | /* for digital modules only */ |
| 313 | static int unioxx5_insn_config(struct comedi_device *dev, |
| 314 | struct comedi_subdevice *subdev, |
| 315 | struct comedi_insn *insn, unsigned int *data) |
| 316 | { |
| 317 | int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type; |
| 318 | struct unioxx5_subd_priv *usp = subdev->private; |
| 319 | int mask = 1 << (channel & 0x07); |
| 320 | |
| 321 | type = usp->usp_module_type[channel / 2]; |
| 322 | |
| 323 | if (type != MODULE_DIGITAL) { |
| 324 | dev_err(dev->class_dev, |
| 325 | "comedi%d: channel configuration accessible only for digital modules\n", |
| 326 | dev->minor); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | channel_offset = __unioxx5_define_chan_offset(channel); |
| 331 | if (channel_offset < 0) { |
| 332 | dev_err(dev->class_dev, |
| 333 | "comedi%d: undefined channel %d. channel range is 0 .. 23\n", |
| 334 | dev->minor, channel); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | /* gets previously written value */ |
| 339 | flags = usp->usp_prev_cn_val[channel_offset - 1]; |
| 340 | |
| 341 | switch (*data) { |
| 342 | case COMEDI_INPUT: |
| 343 | flags &= ~mask; |
| 344 | break; |
| 345 | case COMEDI_OUTPUT: |
| 346 | flags |= mask; |
| 347 | break; |
| 348 | default: |
| 349 | dev_err(dev->class_dev, |
| 350 | "comedi%d: unknown flag\n", dev->minor); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | /* *\ |
| 355 | * sets channels buffer to 1(after this we are allowed to * |
| 356 | * change channel type on input or output) * |
| 357 | \* */ |
| 358 | outb(1, usp->usp_iobase + 0); |
| 359 | /* changes type of _one_ channel */ |
| 360 | outb(flags, usp->usp_iobase + channel_offset); |
| 361 | /* sets channels bank to 0(allows directly input/output) */ |
| 362 | outb(0, usp->usp_iobase + 0); |
| 363 | /* saves written value */ |
| 364 | usp->usp_prev_cn_val[channel_offset - 1] = flags; |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* initializing subdevice with given address */ |
| 370 | static int __unioxx5_subdev_init(struct comedi_device *dev, |
| 371 | struct comedi_subdevice *s, |
| 372 | int iobase) |
| 373 | { |
| 374 | struct unioxx5_subd_priv *usp; |
| 375 | int i, to, ndef_flag = 0; |
| 376 | int ret; |
| 377 | |
| 378 | usp = kzalloc(sizeof(*usp), GFP_KERNEL); |
| 379 | if (usp == NULL) |
| 380 | return -ENOMEM; |
| 381 | |
| 382 | ret = __comedi_request_region(dev, iobase, UNIOXX5_SIZE); |
| 383 | if (ret) { |
| 384 | kfree(usp); |
| 385 | return ret; |
| 386 | } |
| 387 | usp->usp_iobase = iobase; |
| 388 | |
| 389 | /* defining modules types */ |
| 390 | for (i = 0; i < 12; i++) { |
| 391 | to = 10000; |
| 392 | |
| 393 | __unioxx5_analog_config(usp, i * 2); |
| 394 | /* sends channel number to card */ |
| 395 | outb(i + 1, iobase + 5); |
| 396 | outb('H', iobase + 6); /* requests EEPROM world */ |
| 397 | while (!(inb(iobase + 0) & TxBE)) |
| 398 | ; /* waits while writting will be allowed */ |
| 399 | outb(0, iobase + 6); |
| 400 | |
| 401 | /* waits while reading of two bytes will be allowed */ |
| 402 | while (!(inb(iobase + 0) & Rx2CA)) { |
| 403 | if (--to <= 0) { |
| 404 | ndef_flag = 1; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (ndef_flag) { |
| 410 | usp->usp_module_type[i] = 0; |
| 411 | ndef_flag = 0; |
| 412 | } else |
| 413 | usp->usp_module_type[i] = inb(iobase + 6); |
| 414 | |
| 415 | udelay(1); |
| 416 | } |
| 417 | |
| 418 | /* initial subdevice for digital or analog i/o */ |
| 419 | s->type = COMEDI_SUBD_DIO; |
| 420 | s->private = usp; |
| 421 | s->subdev_flags = SDF_READABLE | SDF_WRITABLE; |
| 422 | s->n_chan = UNIOXX5_NUM_OF_CHANS; |
| 423 | s->maxdata = 0xFFF; |
| 424 | s->range_table = &range_digital; |
| 425 | s->insn_read = unioxx5_subdev_read; |
| 426 | s->insn_write = unioxx5_subdev_write; |
| 427 | /* for digital modules only!!! */ |
| 428 | s->insn_config = unioxx5_insn_config; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int unioxx5_attach(struct comedi_device *dev, |
| 434 | struct comedi_devconfig *it) |
| 435 | { |
| 436 | struct comedi_subdevice *s; |
| 437 | int iobase, i, n_subd; |
| 438 | int id, num, ba; |
| 439 | int ret; |
| 440 | |
| 441 | iobase = it->options[0]; |
| 442 | |
| 443 | dev->iobase = iobase; |
| 444 | iobase += UNIOXX5_SUBDEV_BASE; |
| 445 | |
| 446 | /* defining number of subdevices and getting they types (it must be 'g01') */ |
| 447 | for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) { |
| 448 | id = inb(ba + 0xE); |
| 449 | num = inb(ba + 0xF); |
| 450 | |
| 451 | if (id != 'g' || num != 1) |
| 452 | continue; |
| 453 | |
| 454 | n_subd++; |
| 455 | } |
| 456 | |
| 457 | /* unioxx5 can has from two to four subdevices */ |
| 458 | if (n_subd < 2) { |
| 459 | dev_err(dev->class_dev, |
| 460 | "your card must has at least 2 'g01' subdevices\n"); |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | ret = comedi_alloc_subdevices(dev, n_subd); |
| 465 | if (ret) |
| 466 | return ret; |
| 467 | |
| 468 | /* initializing each of for same subdevices */ |
| 469 | for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) { |
| 470 | s = &dev->subdevices[i]; |
| 471 | ret = __unioxx5_subdev_init(dev, s, iobase); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static void unioxx5_detach(struct comedi_device *dev) |
| 480 | { |
| 481 | int i; |
| 482 | struct comedi_subdevice *subdev; |
| 483 | struct unioxx5_subd_priv *usp; |
| 484 | |
| 485 | for (i = 0; i < dev->n_subdevices; i++) { |
| 486 | subdev = &dev->subdevices[i]; |
| 487 | usp = subdev->private; |
| 488 | release_region(usp->usp_iobase, UNIOXX5_SIZE); |
| 489 | kfree(subdev->private); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | static struct comedi_driver unioxx5_driver = { |
| 494 | .driver_name = DRIVER_NAME, |
| 495 | .module = THIS_MODULE, |
| 496 | .attach = unioxx5_attach, |
| 497 | .detach = unioxx5_detach, |
| 498 | }; |
| 499 | module_comedi_driver(unioxx5_driver); |
| 500 | |
| 501 | MODULE_AUTHOR("Comedi http://www.comedi.org"); |
| 502 | MODULE_DESCRIPTION("Comedi low-level driver"); |
| 503 | MODULE_LICENSE("GPL"); |