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