jr3_pci.c revision 56b8421ceef7f2dae95b882034ebf6958bad58f6
1/* 2 comedi/drivers/jr3_pci.c 3 hardware driver for JR3/PCI force sensor board 4 5 COMEDI - Linux Control and Measurement Device Interface 6 Copyright (C) 2007 Anders Blomdell <anders.blomdell@control.lth.se> 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 22*/ 23/* 24Driver: jr3_pci 25Description: JR3/PCI force sensor board 26Author: Anders Blomdell <anders.blomdell@control.lth.se> 27Status: works 28Devices: [JR3] PCI force sensor board (jr3_pci) 29 30 The DSP on the board requires initialization code, which can 31 be loaded by placing it in /lib/firmware/comedi. 32 The initialization code should be somewhere on the media you got 33 with your card. One version is available from http://www.comedi.org 34 in the comedi_nonfree_firmware tarball. 35 36 Configuration options: 37 [0] - PCI bus number - if bus number and slot number are 0, 38 then driver search for first unused card 39 [1] - PCI slot number 40 41*/ 42 43#include "../comedidev.h" 44 45#include <linux/delay.h> 46#include <linux/ctype.h> 47#include <linux/firmware.h> 48#include <linux/jiffies.h> 49#include <linux/slab.h> 50#include <linux/timer.h> 51#include "comedi_pci.h" 52#include "jr3_pci.h" 53 54#define PCI_VENDOR_ID_JR3 0x1762 55#define PCI_DEVICE_ID_JR3_1_CHANNEL 0x3111 56#define PCI_DEVICE_ID_JR3_2_CHANNEL 0x3112 57#define PCI_DEVICE_ID_JR3_3_CHANNEL 0x3113 58#define PCI_DEVICE_ID_JR3_4_CHANNEL 0x3114 59 60static int jr3_pci_attach(struct comedi_device *dev, 61 struct comedi_devconfig *it); 62static int jr3_pci_detach(struct comedi_device *dev); 63 64static struct comedi_driver driver_jr3_pci = { 65 .driver_name = "jr3_pci", 66 .module = THIS_MODULE, 67 .attach = jr3_pci_attach, 68 .detach = jr3_pci_detach, 69}; 70 71static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_table) = { 72 { 73 PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL, 74 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 75 PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_2_CHANNEL, 76 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 77 PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_3_CHANNEL, 78 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 79 PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_4_CHANNEL, 80 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, { 81 0} 82}; 83 84MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table); 85 86struct jr3_pci_dev_private { 87 88 struct pci_dev *pci_dev; 89 int pci_enabled; 90 volatile struct jr3_t *iobase; 91 int n_channels; 92 struct timer_list timer; 93}; 94 95struct poll_delay_t { 96 97 int min; 98 int max; 99}; 100 101struct jr3_pci_subdev_private { 102 volatile struct jr3_channel *channel; 103 unsigned long next_time_min; 104 unsigned long next_time_max; 105 enum { state_jr3_poll, 106 state_jr3_init_wait_for_offset, 107 state_jr3_init_transform_complete, 108 state_jr3_init_set_full_scale_complete, 109 state_jr3_init_use_offset_complete, 110 state_jr3_done 111 } state; 112 int channel_no; 113 int serial_no; 114 int model_no; 115 struct { 116 int length; 117 struct comedi_krange range; 118 } range[9]; 119 const struct comedi_lrange *range_table_list[8 * 7 + 2]; 120 unsigned int maxdata_list[8 * 7 + 2]; 121 u16 errors; 122 int retries; 123}; 124 125/* Hotplug firmware loading stuff */ 126static int comedi_load_firmware(struct comedi_device *dev, char *name, 127 int (*cb)(struct comedi_device *dev, 128 const u8 *data, size_t size)) 129{ 130 int result = 0; 131 const struct firmware *fw; 132 char *firmware_path; 133 static const char *prefix = "comedi/"; 134 struct jr3_pci_dev_private *devpriv = dev->private; 135 136 firmware_path = kmalloc(strlen(prefix) + strlen(name) + 1, GFP_KERNEL); 137 if (!firmware_path) { 138 result = -ENOMEM; 139 } else { 140 firmware_path[0] = '\0'; 141 strcat(firmware_path, prefix); 142 strcat(firmware_path, name); 143 result = request_firmware(&fw, firmware_path, 144 &devpriv->pci_dev->dev); 145 if (result == 0) { 146 if (!cb) 147 result = -EINVAL; 148 else 149 result = cb(dev, fw->data, fw->size); 150 release_firmware(fw); 151 } 152 kfree(firmware_path); 153 } 154 return result; 155} 156 157static struct poll_delay_t poll_delay_min_max(int min, int max) 158{ 159 struct poll_delay_t result; 160 161 result.min = min; 162 result.max = max; 163 return result; 164} 165 166static int is_complete(volatile struct jr3_channel *channel) 167{ 168 return get_s16(&channel->command_word0) == 0; 169} 170 171struct transform_t { 172 struct { 173 u16 link_type; 174 s16 link_amount; 175 } link[8]; 176}; 177 178static void set_transforms(volatile struct jr3_channel *channel, 179 struct transform_t transf, short num) 180{ 181 int i; 182 183 num &= 0x000f; /* Make sure that 0 <= num <= 15 */ 184 for (i = 0; i < 8; i++) { 185 186 set_u16(&channel->transforms[num].link[i].link_type, 187 transf.link[i].link_type); 188 udelay(1); 189 set_s16(&channel->transforms[num].link[i].link_amount, 190 transf.link[i].link_amount); 191 udelay(1); 192 if (transf.link[i].link_type == end_x_form) { 193 break; 194 } 195 } 196} 197 198static void use_transform(volatile struct jr3_channel *channel, 199 short transf_num) 200{ 201 set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f)); 202} 203 204static void use_offset(volatile struct jr3_channel *channel, short offset_num) 205{ 206 set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f)); 207} 208 209static void set_offset(volatile struct jr3_channel *channel) 210{ 211 set_s16(&channel->command_word0, 0x0700); 212} 213 214struct six_axis_t { 215 s16 fx; 216 s16 fy; 217 s16 fz; 218 s16 mx; 219 s16 my; 220 s16 mz; 221}; 222 223static void set_full_scales(volatile struct jr3_channel *channel, 224 struct six_axis_t full_scale) 225{ 226 printk("%d %d %d %d %d %d\n", 227 full_scale.fx, 228 full_scale.fy, 229 full_scale.fz, full_scale.mx, full_scale.my, full_scale.mz); 230 set_s16(&channel->full_scale.fx, full_scale.fx); 231 set_s16(&channel->full_scale.fy, full_scale.fy); 232 set_s16(&channel->full_scale.fz, full_scale.fz); 233 set_s16(&channel->full_scale.mx, full_scale.mx); 234 set_s16(&channel->full_scale.my, full_scale.my); 235 set_s16(&channel->full_scale.mz, full_scale.mz); 236 set_s16(&channel->command_word0, 0x0a00); 237} 238 239static struct six_axis_t get_min_full_scales(volatile struct jr3_channel 240 *channel) 241{ 242 struct six_axis_t result; 243 result.fx = get_s16(&channel->min_full_scale.fx); 244 result.fy = get_s16(&channel->min_full_scale.fy); 245 result.fz = get_s16(&channel->min_full_scale.fz); 246 result.mx = get_s16(&channel->min_full_scale.mx); 247 result.my = get_s16(&channel->min_full_scale.my); 248 result.mz = get_s16(&channel->min_full_scale.mz); 249 return result; 250} 251 252static struct six_axis_t get_max_full_scales(volatile struct jr3_channel 253 *channel) 254{ 255 struct six_axis_t result; 256 result.fx = get_s16(&channel->max_full_scale.fx); 257 result.fy = get_s16(&channel->max_full_scale.fy); 258 result.fz = get_s16(&channel->max_full_scale.fz); 259 result.mx = get_s16(&channel->max_full_scale.mx); 260 result.my = get_s16(&channel->max_full_scale.my); 261 result.mz = get_s16(&channel->max_full_scale.mz); 262 return result; 263} 264 265static int jr3_pci_ai_insn_read(struct comedi_device *dev, 266 struct comedi_subdevice *s, 267 struct comedi_insn *insn, unsigned int *data) 268{ 269 int result; 270 struct jr3_pci_subdev_private *p; 271 int channel; 272 273 p = s->private; 274 channel = CR_CHAN(insn->chanspec); 275 if (p == NULL || channel > 57) { 276 result = -EINVAL; 277 } else { 278 int i; 279 280 result = insn->n; 281 if (p->state != state_jr3_done || 282 (get_u16(&p->channel->errors) & (watch_dog | watch_dog2 | 283 sensor_change))) { 284 /* No sensor or sensor changed */ 285 if (p->state == state_jr3_done) { 286 /* Restart polling */ 287 p->state = state_jr3_poll; 288 } 289 result = -EAGAIN; 290 } 291 for (i = 0; i < insn->n; i++) { 292 if (channel < 56) { 293 int axis, filter; 294 295 axis = channel % 8; 296 filter = channel / 8; 297 if (p->state != state_jr3_done) { 298 data[i] = 0; 299 } else { 300 int F = 0; 301 switch (axis) { 302 case 0:{ 303 F = get_s16 304 (&p->channel->filter 305 [filter].fx); 306 } 307 break; 308 case 1:{ 309 F = get_s16 310 (&p->channel->filter 311 [filter].fy); 312 } 313 break; 314 case 2:{ 315 F = get_s16 316 (&p->channel->filter 317 [filter].fz); 318 } 319 break; 320 case 3:{ 321 F = get_s16 322 (&p->channel->filter 323 [filter].mx); 324 } 325 break; 326 case 4:{ 327 F = get_s16 328 (&p->channel->filter 329 [filter].my); 330 } 331 break; 332 case 5:{ 333 F = get_s16 334 (&p->channel->filter 335 [filter].mz); 336 } 337 break; 338 case 6:{ 339 F = get_s16 340 (&p->channel->filter 341 [filter].v1); 342 } 343 break; 344 case 7:{ 345 F = get_s16 346 (&p->channel->filter 347 [filter].v2); 348 } 349 break; 350 } 351 data[i] = F + 0x4000; 352 } 353 } else if (channel == 56) { 354 if (p->state != state_jr3_done) { 355 data[i] = 0; 356 } else { 357 data[i] = 358 get_u16(&p->channel->model_no); 359 } 360 } else if (channel == 57) { 361 if (p->state != state_jr3_done) { 362 data[i] = 0; 363 } else { 364 data[i] = 365 get_u16(&p->channel->serial_no); 366 } 367 } 368 } 369 } 370 return result; 371} 372 373static int jr3_pci_open(struct comedi_device *dev) 374{ 375 int i; 376 struct jr3_pci_dev_private *devpriv = dev->private; 377 378 printk("jr3_pci_open\n"); 379 for (i = 0; i < devpriv->n_channels; i++) { 380 struct jr3_pci_subdev_private *p; 381 382 p = dev->subdevices[i].private; 383 if (p) { 384 printk("serial: %p %d (%d)\n", p, p->serial_no, 385 p->channel_no); 386 } 387 } 388 return 0; 389} 390 391int read_idm_word(const u8 * data, size_t size, int *pos, unsigned int *val) 392{ 393 int result = 0; 394 if (pos != 0 && val != 0) { 395 /* Skip over non hex */ 396 for (; *pos < size && !isxdigit(data[*pos]); (*pos)++) { 397 } 398 /* Collect value */ 399 *val = 0; 400 for (; *pos < size && isxdigit(data[*pos]); (*pos)++) { 401 char ch = tolower(data[*pos]); 402 result = 1; 403 if ('0' <= ch && ch <= '9') { 404 *val = (*val << 4) + (ch - '0'); 405 } else if ('a' <= ch && ch <= 'f') { 406 *val = (*val << 4) + (ch - 'a' + 10); 407 } 408 } 409 } 410 return result; 411} 412 413static int jr3_download_firmware(struct comedi_device *dev, const u8 * data, 414 size_t size) 415{ 416 /* 417 * IDM file format is: 418 * { count, address, data <count> } * 419 * ffff 420 */ 421 int result, more, pos, OK; 422 423 result = 0; 424 more = 1; 425 pos = 0; 426 OK = 0; 427 while (more) { 428 unsigned int count, addr; 429 430 more = more && read_idm_word(data, size, &pos, &count); 431 if (more && count == 0xffff) { 432 OK = 1; 433 break; 434 } 435 more = more && read_idm_word(data, size, &pos, &addr); 436 while (more && count > 0) { 437 unsigned int dummy; 438 more = more && read_idm_word(data, size, &pos, &dummy); 439 count--; 440 } 441 } 442 443 if (!OK) { 444 result = -ENODATA; 445 } else { 446 int i; 447 struct jr3_pci_dev_private *p = dev->private; 448 449 for (i = 0; i < p->n_channels; i++) { 450 struct jr3_pci_subdev_private *sp; 451 452 sp = dev->subdevices[i].private; 453 more = 1; 454 pos = 0; 455 while (more) { 456 unsigned int count, addr; 457 more = more 458 && read_idm_word(data, size, &pos, &count); 459 if (more && count == 0xffff) { 460 break; 461 } 462 more = more 463 && read_idm_word(data, size, &pos, &addr); 464 printk("Loading#%d %4.4x bytes at %4.4x\n", i, 465 count, addr); 466 while (more && count > 0) { 467 if (addr & 0x4000) { 468 /* 16 bit data, never seen in real life!! */ 469 unsigned int data1; 470 471 more = more 472 && read_idm_word(data, 473 size, &pos, 474 &data1); 475 count--; 476 /* printk("jr3_data, not tested\n"); */ 477 /* jr3[addr + 0x20000 * pnum] = data1; */ 478 } else { 479 /* Download 24 bit program */ 480 unsigned int data1, data2; 481 482 more = more 483 && read_idm_word(data, 484 size, &pos, 485 &data1); 486 more = more 487 && read_idm_word(data, size, 488 &pos, 489 &data2); 490 count -= 2; 491 if (more) { 492 set_u16(&p-> 493 iobase->channel 494 [i].program_low 495 [addr], data1); 496 udelay(1); 497 set_u16(&p-> 498 iobase->channel 499 [i].program_high 500 [addr], data2); 501 udelay(1); 502 503 } 504 } 505 addr++; 506 } 507 } 508 } 509 } 510 return result; 511} 512 513static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice *s) 514{ 515 struct poll_delay_t result = poll_delay_min_max(1000, 2000); 516 struct jr3_pci_subdev_private *p = s->private; 517 int i; 518 519 if (p) { 520 volatile struct jr3_channel *channel = p->channel; 521 int errors = get_u16(&channel->errors); 522 523 if (errors != p->errors) { 524 printk("Errors: %x -> %x\n", p->errors, errors); 525 p->errors = errors; 526 } 527 if (errors & (watch_dog | watch_dog2 | sensor_change)) { 528 /* Sensor communication lost, force poll mode */ 529 p->state = state_jr3_poll; 530 531 } 532 switch (p->state) { 533 case state_jr3_poll:{ 534 u16 model_no = get_u16(&channel->model_no); 535 u16 serial_no = get_u16(&channel->serial_no); 536 if ((errors & (watch_dog | watch_dog2)) || 537 model_no == 0 || serial_no == 0) { 538/* 539 * Still no sensor, keep on polling. Since it takes up to 10 seconds 540 * for offsets to stabilize, polling each second should suffice. 541 */ 542 result = poll_delay_min_max(1000, 2000); 543 } else { 544 p->retries = 0; 545 p->state = 546 state_jr3_init_wait_for_offset; 547 result = poll_delay_min_max(1000, 2000); 548 } 549 } 550 break; 551 case state_jr3_init_wait_for_offset:{ 552 p->retries++; 553 if (p->retries < 10) { 554 /* Wait for offeset to stabilize (< 10 s according to manual) */ 555 result = poll_delay_min_max(1000, 2000); 556 } else { 557 struct transform_t transf; 558 559 p->model_no = 560 get_u16(&channel->model_no); 561 p->serial_no = 562 get_u16(&channel->serial_no); 563 564 printk 565 ("Setting transform for channel %d\n", 566 p->channel_no); 567 printk("Sensor Model = %i\n", 568 p->model_no); 569 printk("Sensor Serial = %i\n", 570 p->serial_no); 571 572 /* Transformation all zeros */ 573 for (i = 0; i < ARRAY_SIZE(transf.link); i++) { 574 transf.link[i].link_type = 575 (enum link_types)0; 576 transf.link[i].link_amount = 0; 577 } 578 579 set_transforms(channel, transf, 0); 580 use_transform(channel, 0); 581 p->state = 582 state_jr3_init_transform_complete; 583 result = poll_delay_min_max(20, 100); /* Allow 20 ms for completion */ 584 } 585 } break; 586 case state_jr3_init_transform_complete:{ 587 if (!is_complete(channel)) { 588 printk 589 ("state_jr3_init_transform_complete complete = %d\n", 590 is_complete(channel)); 591 result = poll_delay_min_max(20, 100); 592 } else { 593 /* Set full scale */ 594 struct six_axis_t min_full_scale; 595 struct six_axis_t max_full_scale; 596 597 min_full_scale = 598 get_min_full_scales(channel); 599 printk("Obtained Min. Full Scales:\n"); 600 printk("%i ", (min_full_scale).fx); 601 printk("%i ", (min_full_scale).fy); 602 printk("%i ", (min_full_scale).fz); 603 printk("%i ", (min_full_scale).mx); 604 printk("%i ", (min_full_scale).my); 605 printk("%i ", (min_full_scale).mz); 606 printk("\n"); 607 608 max_full_scale = 609 get_max_full_scales(channel); 610 printk("Obtained Max. Full Scales:\n"); 611 printk("%i ", (max_full_scale).fx); 612 printk("%i ", (max_full_scale).fy); 613 printk("%i ", (max_full_scale).fz); 614 printk("%i ", (max_full_scale).mx); 615 printk("%i ", (max_full_scale).my); 616 printk("%i ", (max_full_scale).mz); 617 printk("\n"); 618 619 set_full_scales(channel, 620 max_full_scale); 621 622 p->state = 623 state_jr3_init_set_full_scale_complete; 624 result = poll_delay_min_max(20, 100); /* Allow 20 ms for completion */ 625 } 626 } 627 break; 628 case state_jr3_init_set_full_scale_complete:{ 629 if (!is_complete(channel)) { 630 printk 631 ("state_jr3_init_set_full_scale_complete complete = %d\n", 632 is_complete(channel)); 633 result = poll_delay_min_max(20, 100); 634 } else { 635 volatile struct force_array *full_scale; 636 637 /* Use ranges in kN or we will overflow arount 2000N! */ 638 full_scale = &channel->full_scale; 639 p->range[0].range.min = 640 -get_s16(&full_scale->fx) * 1000; 641 p->range[0].range.max = 642 get_s16(&full_scale->fx) * 1000; 643 p->range[1].range.min = 644 -get_s16(&full_scale->fy) * 1000; 645 p->range[1].range.max = 646 get_s16(&full_scale->fy) * 1000; 647 p->range[2].range.min = 648 -get_s16(&full_scale->fz) * 1000; 649 p->range[2].range.max = 650 get_s16(&full_scale->fz) * 1000; 651 p->range[3].range.min = 652 -get_s16(&full_scale->mx) * 100; 653 p->range[3].range.max = 654 get_s16(&full_scale->mx) * 100; 655 p->range[4].range.min = 656 -get_s16(&full_scale->my) * 100; 657 p->range[4].range.max = 658 get_s16(&full_scale->my) * 100; 659 p->range[5].range.min = 660 -get_s16(&full_scale->mz) * 100; 661 p->range[5].range.max = 662 get_s16(&full_scale->mz) * 100; 663 p->range[6].range.min = -get_s16(&full_scale->v1) * 100; /* ?? */ 664 p->range[6].range.max = get_s16(&full_scale->v1) * 100; /* ?? */ 665 p->range[7].range.min = -get_s16(&full_scale->v2) * 100; /* ?? */ 666 p->range[7].range.max = get_s16(&full_scale->v2) * 100; /* ?? */ 667 p->range[8].range.min = 0; 668 p->range[8].range.max = 65535; 669 670 { 671 int i; 672 for (i = 0; i < 9; i++) { 673 printk("%d %d - %d\n", 674 i, 675 p-> 676 range[i].range. 677 min, 678 p-> 679 range[i].range. 680 max); 681 } 682 } 683 684 use_offset(channel, 0); 685 p->state = 686 state_jr3_init_use_offset_complete; 687 result = poll_delay_min_max(40, 100); /* Allow 40 ms for completion */ 688 } 689 } 690 break; 691 case state_jr3_init_use_offset_complete:{ 692 if (!is_complete(channel)) { 693 printk 694 ("state_jr3_init_use_offset_complete complete = %d\n", 695 is_complete(channel)); 696 result = poll_delay_min_max(20, 100); 697 } else { 698 printk 699 ("Default offsets %d %d %d %d %d %d\n", 700 get_s16(&channel->offsets.fx), 701 get_s16(&channel->offsets.fy), 702 get_s16(&channel->offsets.fz), 703 get_s16(&channel->offsets.mx), 704 get_s16(&channel->offsets.my), 705 get_s16(&channel->offsets.mz)); 706 707 set_s16(&channel->offsets.fx, 0); 708 set_s16(&channel->offsets.fy, 0); 709 set_s16(&channel->offsets.fz, 0); 710 set_s16(&channel->offsets.mx, 0); 711 set_s16(&channel->offsets.my, 0); 712 set_s16(&channel->offsets.mz, 0); 713 714 set_offset(channel); 715 716 p->state = state_jr3_done; 717 } 718 } 719 break; 720 case state_jr3_done:{ 721 poll_delay_min_max(10000, 20000); 722 } 723 break; 724 default:{ 725 poll_delay_min_max(1000, 2000); 726 } 727 break; 728 } 729 } 730 return result; 731} 732 733static void jr3_pci_poll_dev(unsigned long data) 734{ 735 unsigned long flags; 736 struct comedi_device *dev = (struct comedi_device *)data; 737 struct jr3_pci_dev_private *devpriv = dev->private; 738 unsigned long now; 739 int delay; 740 int i; 741 742 spin_lock_irqsave(&dev->spinlock, flags); 743 delay = 1000; 744 now = jiffies; 745 /* Poll all channels that are ready to be polled */ 746 for (i = 0; i < devpriv->n_channels; i++) { 747 struct jr3_pci_subdev_private *subdevpriv = 748 dev->subdevices[i].private; 749 if (now > subdevpriv->next_time_min) { 750 struct poll_delay_t sub_delay; 751 752 sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]); 753 subdevpriv->next_time_min = 754 jiffies + msecs_to_jiffies(sub_delay.min); 755 subdevpriv->next_time_max = 756 jiffies + msecs_to_jiffies(sub_delay.max); 757 if (sub_delay.max && sub_delay.max < delay) { 758/* 759* Wake up as late as possible -> poll as many channels as possible 760* at once 761*/ 762 delay = sub_delay.max; 763 } 764 } 765 } 766 spin_unlock_irqrestore(&dev->spinlock, flags); 767 768 devpriv->timer.expires = jiffies + msecs_to_jiffies(delay); 769 add_timer(&devpriv->timer); 770} 771 772static int jr3_pci_attach(struct comedi_device *dev, 773 struct comedi_devconfig *it) 774{ 775 int result = 0; 776 struct pci_dev *card = NULL; 777 int opt_bus, opt_slot, i; 778 struct jr3_pci_dev_private *devpriv; 779 780 printk("comedi%d: jr3_pci\n", dev->minor); 781 782 opt_bus = it->options[0]; 783 opt_slot = it->options[1]; 784 785 if (sizeof(struct jr3_channel) != 0xc00) { 786 printk("sizeof(struct jr3_channel) = %x [expected %x]\n", 787 (unsigned)sizeof(struct jr3_channel), 0xc00); 788 return -EINVAL; 789 } 790 791 result = alloc_private(dev, sizeof(struct jr3_pci_dev_private)); 792 if (result < 0) { 793 return -ENOMEM; 794 } 795 card = NULL; 796 devpriv = dev->private; 797 init_timer(&devpriv->timer); 798 while (1) { 799 card = pci_get_device(PCI_VENDOR_ID_JR3, PCI_ANY_ID, card); 800 if (card == NULL) { 801 /* No card found */ 802 break; 803 } else { 804 switch (card->device) { 805 case PCI_DEVICE_ID_JR3_1_CHANNEL:{ 806 devpriv->n_channels = 1; 807 } 808 break; 809 case PCI_DEVICE_ID_JR3_2_CHANNEL:{ 810 devpriv->n_channels = 2; 811 } 812 break; 813 case PCI_DEVICE_ID_JR3_3_CHANNEL:{ 814 devpriv->n_channels = 3; 815 } 816 break; 817 case PCI_DEVICE_ID_JR3_4_CHANNEL:{ 818 devpriv->n_channels = 4; 819 } 820 break; 821 default:{ 822 devpriv->n_channels = 0; 823 } 824 } 825 if (devpriv->n_channels >= 1) { 826 if (opt_bus == 0 && opt_slot == 0) { 827 /* Take first available card */ 828 break; 829 } else if (opt_bus == card->bus->number && 830 opt_slot == PCI_SLOT(card->devfn)) { 831 /* Take requested card */ 832 break; 833 } 834 } 835 } 836 } 837 if (!card) { 838 printk(" no jr3_pci found\n"); 839 return -EIO; 840 } else { 841 devpriv->pci_dev = card; 842 dev->board_name = "jr3_pci"; 843 } 844 845 result = comedi_pci_enable(card, "jr3_pci"); 846 if (result < 0) { 847 return -EIO; 848 } 849 850 devpriv->pci_enabled = 1; 851 devpriv->iobase = ioremap(pci_resource_start(card, 0), 852 offsetof(struct jr3_t, channel[devpriv->n_channels])); 853 if (!devpriv->iobase) 854 return -ENOMEM; 855 856 result = alloc_subdevices(dev, devpriv->n_channels); 857 if (result < 0) 858 goto out; 859 860 dev->open = jr3_pci_open; 861 for (i = 0; i < devpriv->n_channels; i++) { 862 dev->subdevices[i].type = COMEDI_SUBD_AI; 863 dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND; 864 dev->subdevices[i].n_chan = 8 * 7 + 2; 865 dev->subdevices[i].insn_read = jr3_pci_ai_insn_read; 866 dev->subdevices[i].private = 867 kzalloc(sizeof(struct jr3_pci_subdev_private), GFP_KERNEL); 868 if (dev->subdevices[i].private) { 869 struct jr3_pci_subdev_private *p; 870 int j; 871 872 p = dev->subdevices[i].private; 873 p->channel = &devpriv->iobase->channel[i].data; 874 printk("p->channel %p %p (%tx)\n", 875 p->channel, devpriv->iobase, 876 ((char *)(p->channel) - 877 (char *)(devpriv->iobase))); 878 p->channel_no = i; 879 for (j = 0; j < 8; j++) { 880 int k; 881 882 p->range[j].length = 1; 883 p->range[j].range.min = -1000000; 884 p->range[j].range.max = 1000000; 885 for (k = 0; k < 7; k++) { 886 p->range_table_list[j + k * 8] = 887 (struct comedi_lrange *)&p-> 888 range[j]; 889 p->maxdata_list[j + k * 8] = 0x7fff; 890 } 891 } 892 p->range[8].length = 1; 893 p->range[8].range.min = 0; 894 p->range[8].range.max = 65536; 895 896 p->range_table_list[56] = 897 (struct comedi_lrange *)&p->range[8]; 898 p->range_table_list[57] = 899 (struct comedi_lrange *)&p->range[8]; 900 p->maxdata_list[56] = 0xffff; 901 p->maxdata_list[57] = 0xffff; 902 /* Channel specific range and maxdata */ 903 dev->subdevices[i].range_table = 0; 904 dev->subdevices[i].range_table_list = 905 p->range_table_list; 906 dev->subdevices[i].maxdata = 0; 907 dev->subdevices[i].maxdata_list = p->maxdata_list; 908 } 909 } 910 911 /* Reset DSP card */ 912 devpriv->iobase->channel[0].reset = 0; 913 914 result = comedi_load_firmware(dev, "jr3pci.idm", jr3_download_firmware); 915 printk("Firmare load %d\n", result); 916 917 if (result < 0) { 918 goto out; 919 } 920/* 921 * TODO: use firmware to load preferred offset tables. Suggested 922 * format: 923 * model serial Fx Fy Fz Mx My Mz\n 924 * 925 * comedi_load_firmware(dev, "jr3_offsets_table", jr3_download_firmware); 926 */ 927 928/* 929 * It takes a few milliseconds for software to settle as much as we 930 * can read firmware version 931 */ 932 msleep_interruptible(25); 933 for (i = 0; i < 0x18; i++) { 934 printk("%c", 935 get_u16(&devpriv->iobase->channel[0]. 936 data.copyright[i]) >> 8); 937 } 938 939 /* Start card timer */ 940 for (i = 0; i < devpriv->n_channels; i++) { 941 struct jr3_pci_subdev_private *p = dev->subdevices[i].private; 942 943 p->next_time_min = jiffies + msecs_to_jiffies(500); 944 p->next_time_max = jiffies + msecs_to_jiffies(2000); 945 } 946 947 devpriv->timer.data = (unsigned long)dev; 948 devpriv->timer.function = jr3_pci_poll_dev; 949 devpriv->timer.expires = jiffies + msecs_to_jiffies(1000); 950 add_timer(&devpriv->timer); 951 952out: 953 return result; 954} 955 956MODULE_FIRMWARE("comedi/jr3pci.idm"); 957 958static int jr3_pci_detach(struct comedi_device *dev) 959{ 960 int i; 961 struct jr3_pci_dev_private *devpriv = dev->private; 962 963 printk("comedi%d: jr3_pci: remove\n", dev->minor); 964 if (devpriv) { 965 del_timer_sync(&devpriv->timer); 966 967 if (dev->subdevices) { 968 for (i = 0; i < devpriv->n_channels; i++) { 969 kfree(dev->subdevices[i].private); 970 } 971 } 972 973 if (devpriv->iobase) { 974 iounmap((void *)devpriv->iobase); 975 } 976 if (devpriv->pci_enabled) { 977 comedi_pci_disable(devpriv->pci_dev); 978 } 979 980 if (devpriv->pci_dev) { 981 pci_dev_put(devpriv->pci_dev); 982 } 983 } 984 return 0; 985} 986 987static int __devinit driver_jr3_pci_pci_probe(struct pci_dev *dev, 988 const struct pci_device_id *ent) 989{ 990 return comedi_pci_auto_config(dev, driver_jr3_pci.driver_name); 991} 992 993static void __devexit driver_jr3_pci_pci_remove(struct pci_dev *dev) 994{ 995 comedi_pci_auto_unconfig(dev); 996} 997 998static struct pci_driver driver_jr3_pci_pci_driver = { 999 .id_table = jr3_pci_pci_table, 1000 .probe = &driver_jr3_pci_pci_probe, 1001 .remove = __devexit_p(&driver_jr3_pci_pci_remove) 1002}; 1003 1004static int __init driver_jr3_pci_init_module(void) 1005{ 1006 int retval; 1007 1008 retval = comedi_driver_register(&driver_jr3_pci); 1009 if (retval < 0) 1010 return retval; 1011 1012 driver_jr3_pci_pci_driver.name = (char *)driver_jr3_pci.driver_name; 1013 return pci_register_driver(&driver_jr3_pci_pci_driver); 1014} 1015 1016static void __exit driver_jr3_pci_cleanup_module(void) 1017{ 1018 pci_unregister_driver(&driver_jr3_pci_pci_driver); 1019 comedi_driver_unregister(&driver_jr3_pci); 1020} 1021 1022module_init(driver_jr3_pci_init_module); 1023module_exit(driver_jr3_pci_cleanup_module); 1024 1025MODULE_AUTHOR("Comedi http://www.comedi.org"); 1026MODULE_DESCRIPTION("Comedi low-level driver"); 1027MODULE_LICENSE("GPL"); 1028