2 comedi/drivers/jr3_pci.c
3 hardware driver for JR3/PCI force sensor board
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2007 Anders Blomdell <anders.blomdell@control.lth.se>
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.
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.
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.
25 Description: JR3/PCI force sensor board
26 Author: Anders Blomdell <anders.blomdell@control.lth.se>
28 Devices: [JR3] PCI force sensor board (jr3_pci)
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.
36 Configuration options:
37 [0] - PCI bus number - if bus number and slot number are 0,
38 then driver search for first unused card
43 #include "../comedidev.h"
45 #include <linux/delay.h>
46 #include <linux/ctype.h>
47 #include <linux/firmware.h>
48 #include "comedi_pci.h"
51 /* Hotplug firmware loading stuff */
53 static void comedi_fw_release(struct device *dev)
55 printk(KERN_DEBUG "firmware_sample_driver: ghost_release\n");
58 static struct device comedi_fw_device = {
59 .init_name = "comedi",
60 .release = comedi_fw_release
63 typedef int comedi_firmware_callback(struct comedi_device * dev,
64 const u8 * data, size_t size);
66 static int comedi_load_firmware(struct comedi_device * dev,
67 char *name, comedi_firmware_callback cb)
70 const struct firmware *fw;
72 static const char *prefix = "comedi/";
74 firmware_path = kmalloc(strlen(prefix) + strlen(name) + 1, GFP_KERNEL);
78 firmware_path[0] = '\0';
79 strcat(firmware_path, prefix);
80 strcat(firmware_path, name);
81 result = device_register(&comedi_fw_device);
83 result = request_firmware(&fw, firmware_path,
89 result = cb(dev, fw->data, fw->size);
93 device_unregister(&comedi_fw_device);
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
106 static int jr3_pci_attach(struct comedi_device * dev, struct comedi_devconfig * it);
107 static int jr3_pci_detach(struct comedi_device * dev);
109 static struct comedi_driver driver_jr3_pci = {
110 driver_name:"jr3_pci",
112 attach:jr3_pci_attach,
113 detach:jr3_pci_detach,
116 static 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},
128 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
130 struct jr3_pci_dev_private {
132 struct pci_dev *pci_dev;
134 volatile struct jr3_t *iobase;
136 struct timer_list timer;
140 struct poll_delay_t {
147 struct jr3_pci_subdev_private {
148 volatile struct jr3_channel *channel;
149 unsigned long next_time_min;
150 unsigned long next_time_max;
151 enum { state_jr3_poll,
152 state_jr3_init_wait_for_offset,
153 state_jr3_init_transform_complete,
154 state_jr3_init_set_full_scale_complete,
155 state_jr3_init_use_offset_complete,
163 struct comedi_krange range;
165 const struct comedi_lrange *range_table_list[8 * 7 + 2];
166 unsigned int maxdata_list[8 * 7 + 2];
171 static struct poll_delay_t poll_delay_min_max(int min, int max)
173 struct poll_delay_t result;
180 static int is_complete(volatile struct jr3_channel *channel)
182 return get_s16(&channel->command_word0) == 0;
192 static void set_transforms(volatile struct jr3_channel *channel,
193 struct transform_t transf, short num)
197 num &= 0x000f; // Make sure that 0 <= num <= 15
198 for (i = 0; i < 8; i++) {
200 set_u16(&channel->transforms[num].link[i].link_type,
201 transf.link[i].link_type);
203 set_s16(&channel->transforms[num].link[i].link_amount,
204 transf.link[i].link_amount);
206 if (transf.link[i].link_type == end_x_form) {
212 static void use_transform(volatile struct jr3_channel *channel, short transf_num)
214 set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f));
217 static void use_offset(volatile struct jr3_channel *channel, short offset_num)
219 set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f));
222 static void set_offset(volatile struct jr3_channel *channel)
224 set_s16(&channel->command_word0, 0x0700);
236 static void set_full_scales(volatile struct jr3_channel *channel,
237 struct six_axis_t full_scale)
239 printk("%d %d %d %d %d %d\n",
242 full_scale.fz, full_scale.mx, full_scale.my, full_scale.mz);
243 set_s16(&channel->full_scale.fx, full_scale.fx);
244 set_s16(&channel->full_scale.fy, full_scale.fy);
245 set_s16(&channel->full_scale.fz, full_scale.fz);
246 set_s16(&channel->full_scale.mx, full_scale.mx);
247 set_s16(&channel->full_scale.my, full_scale.my);
248 set_s16(&channel->full_scale.mz, full_scale.mz);
249 set_s16(&channel->command_word0, 0x0a00);
252 static struct six_axis_t get_min_full_scales(volatile struct jr3_channel *channel)
254 struct six_axis_t result;
255 result.fx = get_s16(&channel->min_full_scale.fx);
256 result.fy = get_s16(&channel->min_full_scale.fy);
257 result.fz = get_s16(&channel->min_full_scale.fz);
258 result.mx = get_s16(&channel->min_full_scale.mx);
259 result.my = get_s16(&channel->min_full_scale.my);
260 result.mz = get_s16(&channel->min_full_scale.mz);
264 static struct six_axis_t get_max_full_scales(volatile struct jr3_channel *channel)
266 struct six_axis_t result;
267 result.fx = get_s16(&channel->max_full_scale.fx);
268 result.fy = get_s16(&channel->max_full_scale.fy);
269 result.fz = get_s16(&channel->max_full_scale.fz);
270 result.mx = get_s16(&channel->max_full_scale.mx);
271 result.my = get_s16(&channel->max_full_scale.my);
272 result.mz = get_s16(&channel->max_full_scale.mz);
276 static int jr3_pci_ai_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
277 struct comedi_insn * insn, unsigned int * data)
280 struct jr3_pci_subdev_private *p;
284 channel = CR_CHAN(insn->chanspec);
285 if (p == NULL || channel > 57) {
291 if (p->state != state_jr3_done ||
292 (get_u16(&p->channel->
293 errors) & (watch_dog | watch_dog2 |
295 /* No sensor or sensor changed */
296 if (p->state == state_jr3_done) {
297 /* Restart polling */
298 p->state = state_jr3_poll;
302 for (i = 0; i < insn->n; i++) {
307 filter = channel / 8;
308 if (p->state != state_jr3_done) {
370 data[i] = F + 0x4000;
372 } else if (channel == 56) {
373 if (p->state != state_jr3_done) {
377 get_u16(&p->channel->model_no);
379 } else if (channel == 57) {
380 if (p->state != state_jr3_done) {
384 get_u16(&p->channel->serial_no);
392 static void jr3_pci_open(struct comedi_device * dev)
395 struct jr3_pci_dev_private *devpriv = dev->private;
397 printk("jr3_pci_open\n");
398 for (i = 0; i < devpriv->n_channels; i++) {
399 struct jr3_pci_subdev_private *p;
401 p = dev->subdevices[i].private;
403 printk("serial: %p %d (%d)\n", p, p->serial_no,
409 int read_idm_word(const u8 * data, size_t size, int *pos, unsigned int *val)
412 if (pos != 0 && val != 0) {
414 for (; *pos < size && !isxdigit(data[*pos]); (*pos)++) {
418 for (; *pos < size && isxdigit(data[*pos]); (*pos)++) {
419 char ch = tolower(data[*pos]);
421 if ('0' <= ch && ch <= '9') {
422 *val = (*val << 4) + (ch - '0');
423 } else if ('a' <= ch && ch <= 'f') {
424 *val = (*val << 4) + (ch - 'a' + 10);
431 static int jr3_download_firmware(struct comedi_device * dev, const u8 * data,
435 * IDM file format is:
436 * { count, address, data <count> } *
439 int result, more, pos, OK;
446 unsigned int count, addr;
448 more = more && read_idm_word(data, size, &pos, &count);
449 if (more && count == 0xffff) {
453 more = more && read_idm_word(data, size, &pos, &addr);
454 while (more && count > 0) {
456 more = more && read_idm_word(data, size, &pos, &dummy);
465 struct jr3_pci_dev_private *p = dev->private;
467 for (i = 0; i < p->n_channels; i++) {
468 struct jr3_pci_subdev_private *sp;
470 sp = dev->subdevices[i].private;
474 unsigned int count, addr;
476 && read_idm_word(data, size, &pos,
478 if (more && count == 0xffff) {
482 && read_idm_word(data, size, &pos,
484 printk("Loading#%d %4.4x bytes at %4.4x\n", i,
486 while (more && count > 0) {
488 // 16 bit data, never seen in real life!!
492 && read_idm_word(data,
495 // printk("jr3_data, not tested\n");
496 // jr3[addr + 0x20000 * pnum] = data1;
498 // Download 24 bit program
499 unsigned int data1, data2;
502 && read_idm_word(data,
505 && read_idm_word(data,
530 static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice * s)
532 struct poll_delay_t result = poll_delay_min_max(1000, 2000);
533 struct jr3_pci_subdev_private *p = s->private;
536 volatile struct jr3_channel *channel = p->channel;
537 int errors = get_u16(&channel->errors);
539 if (errors != p->errors) {
540 printk("Errors: %x -> %x\n", p->errors, errors);
543 if (errors & (watch_dog | watch_dog2 | sensor_change)) {
544 // Sensor communication lost, force poll mode
545 p->state = state_jr3_poll;
549 case state_jr3_poll:{
550 u16 model_no = get_u16(&channel->model_no);
551 u16 serial_no = get_u16(&channel->serial_no);
552 if ((errors & (watch_dog | watch_dog2)) ||
553 model_no == 0 || serial_no == 0) {
554 // Still no sensor, keep on polling. Since it takes up to
555 // 10 seconds for offsets to stabilize, polling each
556 // second should suffice.
557 result = poll_delay_min_max(1000, 2000);
561 state_jr3_init_wait_for_offset;
562 result = poll_delay_min_max(1000, 2000);
566 case state_jr3_init_wait_for_offset:{
568 if (p->retries < 10) {
569 // Wait for offeset to stabilize (< 10 s according to manual)
570 result = poll_delay_min_max(1000, 2000);
572 struct transform_t transf;
575 get_u16(&channel->model_no);
577 get_u16(&channel->serial_no);
579 printk("Setting transform for channel %d\n", p->channel_no);
580 printk("Sensor Model = %i\n",
582 printk("Sensor Serial = %i\n",
585 // Transformation all zeros
586 transf.link[0].link_type =
588 transf.link[0].link_amount = 0;
589 transf.link[1].link_type =
591 transf.link[1].link_amount = 0;
592 transf.link[2].link_type =
594 transf.link[2].link_amount = 0;
595 transf.link[3].link_type =
597 transf.link[3].link_amount = 0;
599 set_transforms(channel, transf, 0);
600 use_transform(channel, 0);
602 state_jr3_init_transform_complete;
603 result = poll_delay_min_max(20, 100); // Allow 20 ms for completion
606 case state_jr3_init_transform_complete:{
607 if (!is_complete(channel)) {
608 printk("state_jr3_init_transform_complete complete = %d\n", is_complete(channel));
609 result = poll_delay_min_max(20, 100);
612 struct six_axis_t min_full_scale;
613 struct six_axis_t max_full_scale;
616 get_min_full_scales(channel);
617 printk("Obtained Min. Full Scales:\n");
618 printk("%i ", (min_full_scale).fx);
619 printk("%i ", (min_full_scale).fy);
620 printk("%i ", (min_full_scale).fz);
621 printk("%i ", (min_full_scale).mx);
622 printk("%i ", (min_full_scale).my);
623 printk("%i ", (min_full_scale).mz);
627 get_max_full_scales(channel);
628 printk("Obtained Max. Full Scales:\n");
629 printk("%i ", (max_full_scale).fx);
630 printk("%i ", (max_full_scale).fy);
631 printk("%i ", (max_full_scale).fz);
632 printk("%i ", (max_full_scale).mx);
633 printk("%i ", (max_full_scale).my);
634 printk("%i ", (max_full_scale).mz);
637 set_full_scales(channel,
641 state_jr3_init_set_full_scale_complete;
642 result = poll_delay_min_max(20, 100); // Allow 20 ms for completion
646 case state_jr3_init_set_full_scale_complete:{
647 if (!is_complete(channel)) {
648 printk("state_jr3_init_set_full_scale_complete complete = %d\n", is_complete(channel));
649 result = poll_delay_min_max(20, 100);
651 volatile struct force_array *full_scale;
653 // Use ranges in kN or we will overflow arount 2000N!
654 full_scale = &channel->full_scale;
655 p->range[0].range.min =
656 -get_s16(&full_scale->fx) *
658 p->range[0].range.max =
659 get_s16(&full_scale->fx) * 1000;
660 p->range[1].range.min =
661 -get_s16(&full_scale->fy) *
663 p->range[1].range.max =
664 get_s16(&full_scale->fy) * 1000;
665 p->range[2].range.min =
666 -get_s16(&full_scale->fz) *
668 p->range[2].range.max =
669 get_s16(&full_scale->fz) * 1000;
670 p->range[3].range.min =
671 -get_s16(&full_scale->mx) * 100;
672 p->range[3].range.max =
673 get_s16(&full_scale->mx) * 100;
674 p->range[4].range.min =
675 -get_s16(&full_scale->my) * 100;
676 p->range[4].range.max =
677 get_s16(&full_scale->my) * 100;
678 p->range[5].range.min =
679 -get_s16(&full_scale->mz) * 100;
680 p->range[5].range.max =
681 get_s16(&full_scale->mz) * 100;
682 p->range[6].range.min = -get_s16(&full_scale->v1) * 100; // ??
683 p->range[6].range.max = get_s16(&full_scale->v1) * 100; // ??
684 p->range[7].range.min = -get_s16(&full_scale->v2) * 100; // ??
685 p->range[7].range.max = get_s16(&full_scale->v2) * 100; // ??
686 p->range[8].range.min = 0;
687 p->range[8].range.max = 65535;
691 for (i = 0; i < 9; i++) {
692 printk("%d %d - %d\n",
701 use_offset(channel, 0);
703 state_jr3_init_use_offset_complete;
704 result = poll_delay_min_max(40, 100); // Allow 40 ms for completion
708 case state_jr3_init_use_offset_complete:{
709 if (!is_complete(channel)) {
710 printk("state_jr3_init_use_offset_complete complete = %d\n", is_complete(channel));
711 result = poll_delay_min_max(20, 100);
713 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));
715 set_s16(&channel->offsets.fx, 0);
716 set_s16(&channel->offsets.fy, 0);
717 set_s16(&channel->offsets.fz, 0);
718 set_s16(&channel->offsets.mx, 0);
719 set_s16(&channel->offsets.my, 0);
720 set_s16(&channel->offsets.mz, 0);
724 p->state = state_jr3_done;
728 case state_jr3_done:{
729 poll_delay_min_max(10000, 20000);
733 poll_delay_min_max(1000, 2000);
741 static void jr3_pci_poll_dev(unsigned long data)
744 struct comedi_device *dev = (struct comedi_device *) data;
745 struct jr3_pci_dev_private *devpriv = dev->private;
750 comedi_spin_lock_irqsave(&dev->spinlock, flags);
753 // Poll all channels that are ready to be polled
754 for (i = 0; i < devpriv->n_channels; i++) {
755 struct jr3_pci_subdev_private *subdevpriv = dev->subdevices[i].private;
756 if (now > subdevpriv->next_time_min) {
757 struct poll_delay_t sub_delay;
759 sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]);
760 subdevpriv->next_time_min =
761 jiffies + msecs_to_jiffies(sub_delay.min);
762 subdevpriv->next_time_max =
763 jiffies + msecs_to_jiffies(sub_delay.max);
764 if (sub_delay.max && sub_delay.max < delay) {
765 // Wake up as late as possible -> poll as many channels as
767 delay = sub_delay.max;
771 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
773 devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
774 add_timer(&devpriv->timer);
777 static int jr3_pci_attach(struct comedi_device * dev, struct comedi_devconfig * it)
780 struct pci_dev *card = NULL;
781 int opt_bus, opt_slot, i;
782 struct jr3_pci_dev_private *devpriv;
784 printk("comedi%d: jr3_pci\n", dev->minor);
786 opt_bus = it->options[0];
787 opt_slot = it->options[1];
789 if (sizeof(struct jr3_channel) != 0xc00) {
790 printk("sizeof(struct jr3_channel) = %x [expected %x]\n",
791 (unsigned)sizeof(struct jr3_channel), 0xc00);
795 result = alloc_private(dev, sizeof(struct jr3_pci_dev_private));
800 devpriv = dev->private;
801 init_timer(&devpriv->timer);
803 card = pci_get_device(PCI_VENDOR_ID_JR3, PCI_ANY_ID, card);
808 switch (card->device) {
809 case PCI_DEVICE_ID_JR3_1_CHANNEL:{
810 devpriv->n_channels = 1;
813 case PCI_DEVICE_ID_JR3_2_CHANNEL:{
814 devpriv->n_channels = 2;
817 case PCI_DEVICE_ID_JR3_3_CHANNEL:{
818 devpriv->n_channels = 3;
821 case PCI_DEVICE_ID_JR3_4_CHANNEL:{
822 devpriv->n_channels = 4;
826 devpriv->n_channels = 0;
829 if (devpriv->n_channels >= 1) {
830 if (opt_bus == 0 && opt_slot == 0) {
831 /* Take first available card */
833 } else if (opt_bus == card->bus->number &&
834 opt_slot == PCI_SLOT(card->devfn)) {
835 /* Take requested card */
842 printk(" no jr3_pci found\n");
845 devpriv->pci_dev = card;
846 dev->board_name = "jr3_pci";
848 if ((result = comedi_pci_enable(card, "jr3_pci")) < 0) {
851 devpriv->pci_enabled = 1;
852 devpriv->iobase = ioremap(pci_resource_start(card, 0), sizeof(struct jr3_t));
853 result = alloc_subdevices(dev, devpriv->n_channels);
857 dev->open = jr3_pci_open;
858 for (i = 0; i < devpriv->n_channels; i++) {
859 dev->subdevices[i].type = COMEDI_SUBD_AI;
860 dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND;
861 dev->subdevices[i].n_chan = 8 * 7 + 2;
862 dev->subdevices[i].insn_read = jr3_pci_ai_insn_read;
863 dev->subdevices[i].private =
864 kzalloc(sizeof(struct jr3_pci_subdev_private), GFP_KERNEL);
865 if (dev->subdevices[i].private) {
866 struct jr3_pci_subdev_private *p;
869 p = dev->subdevices[i].private;
870 p->channel = &devpriv->iobase->channel[i].data;
871 printk("p->channel %p %p (%tx)\n",
872 p->channel, devpriv->iobase,
873 ((char *)(p->channel) -
874 (char *)(devpriv->iobase)));
876 for (j = 0; j < 8; j++) {
879 p->range[j].length = 1;
880 p->range[j].range.min = -1000000;
881 p->range[j].range.max = 1000000;
882 for (k = 0; k < 7; k++) {
883 p->range_table_list[j + k * 8] =
884 (struct comedi_lrange *) & p->range[j];
885 p->maxdata_list[j + k * 8] = 0x7fff;
888 p->range[8].length = 1;
889 p->range[8].range.min = 0;
890 p->range[8].range.max = 65536;
892 p->range_table_list[56] =
893 (struct comedi_lrange *) & p->range[8];
894 p->range_table_list[57] =
895 (struct comedi_lrange *) & p->range[8];
896 p->maxdata_list[56] = 0xffff;
897 p->maxdata_list[57] = 0xffff;
898 // Channel specific range and maxdata
899 dev->subdevices[i].range_table = 0;
900 dev->subdevices[i].range_table_list =
902 dev->subdevices[i].maxdata = 0;
903 dev->subdevices[i].maxdata_list = p->maxdata_list;
908 devpriv->iobase->channel[0].reset = 0;
910 result = comedi_load_firmware(dev, "jr3pci.idm", jr3_download_firmware);
911 printk("Firmare load %d\n", result);
916 // TODO: use firmware to load preferred offset tables. Suggested format:
917 // model serial Fx Fy Fz Mx My Mz\n
919 // comedi_load_firmware(dev, "jr3_offsets_table", jr3_download_firmware);
921 // It takes a few milliseconds for software to settle
922 // as much as we can read firmware version
923 msleep_interruptible(25);
924 for (i = 0; i < 0x18; i++) {
926 get_u16(&devpriv->iobase->channel[0].data.
931 for (i = 0; i < devpriv->n_channels; i++) {
932 struct jr3_pci_subdev_private *p = dev->subdevices[i].private;
934 p->next_time_min = jiffies + msecs_to_jiffies(500);
935 p->next_time_max = jiffies + msecs_to_jiffies(2000);
938 devpriv->timer.data = (unsigned long)dev;
939 devpriv->timer.function = jr3_pci_poll_dev;
940 devpriv->timer.expires = jiffies + msecs_to_jiffies(1000);
941 add_timer(&devpriv->timer);
947 static int jr3_pci_detach(struct comedi_device * dev)
950 struct jr3_pci_dev_private *devpriv = dev->private;
952 printk("comedi%d: jr3_pci: remove\n", dev->minor);
954 del_timer_sync(&devpriv->timer);
956 if (dev->subdevices) {
957 for (i = 0; i < devpriv->n_channels; i++) {
958 kfree(dev->subdevices[i].private);
962 if (devpriv->iobase) {
963 iounmap((void *)devpriv->iobase);
965 if (devpriv->pci_enabled) {
966 comedi_pci_disable(devpriv->pci_dev);
969 if (devpriv->pci_dev) {
970 pci_dev_put(devpriv->pci_dev);
976 COMEDI_PCI_INITCLEANUP(driver_jr3_pci, jr3_pci_pci_table);