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 <linux/jiffies.h>
49 #include <linux/timer.h>
50 #include "comedi_pci.h"
53 #define PCI_VENDOR_ID_JR3 0x1762
54 #define PCI_DEVICE_ID_JR3_1_CHANNEL 0x3111
55 #define PCI_DEVICE_ID_JR3_2_CHANNEL 0x3112
56 #define PCI_DEVICE_ID_JR3_3_CHANNEL 0x3113
57 #define PCI_DEVICE_ID_JR3_4_CHANNEL 0x3114
59 static int jr3_pci_attach(struct comedi_device *dev, struct comedi_devconfig *it);
60 static int jr3_pci_detach(struct comedi_device *dev);
62 static struct comedi_driver driver_jr3_pci = {
63 .driver_name = "jr3_pci",
64 .module = THIS_MODULE,
65 .attach = jr3_pci_attach,
66 .detach = jr3_pci_detach,
69 static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_table) = {
70 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL,
71 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
72 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_2_CHANNEL,
73 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
74 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_3_CHANNEL,
75 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
76 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_4_CHANNEL,
77 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
81 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
83 struct jr3_pci_dev_private {
85 struct pci_dev *pci_dev;
87 volatile struct jr3_t *iobase;
89 struct timer_list timer;
100 struct jr3_pci_subdev_private {
101 volatile struct jr3_channel *channel;
102 unsigned long next_time_min;
103 unsigned long next_time_max;
104 enum { state_jr3_poll,
105 state_jr3_init_wait_for_offset,
106 state_jr3_init_transform_complete,
107 state_jr3_init_set_full_scale_complete,
108 state_jr3_init_use_offset_complete,
116 struct comedi_krange range;
118 const struct comedi_lrange *range_table_list[8 * 7 + 2];
119 unsigned int maxdata_list[8 * 7 + 2];
124 /* Hotplug firmware loading stuff */
126 typedef int comedi_firmware_callback(struct comedi_device *dev,
127 const u8 *data, size_t size);
129 static int comedi_load_firmware(struct comedi_device *dev, char *name,
130 comedi_firmware_callback cb)
133 const struct firmware *fw;
135 static const char *prefix = "comedi/";
136 struct jr3_pci_dev_private *devpriv = dev->private;
138 firmware_path = kmalloc(strlen(prefix) + strlen(name) + 1, GFP_KERNEL);
139 if (!firmware_path) {
142 firmware_path[0] = '\0';
143 strcat(firmware_path, prefix);
144 strcat(firmware_path, name);
145 result = request_firmware(&fw, firmware_path,
146 &devpriv->pci_dev->dev);
151 result = cb(dev, fw->data, fw->size);
152 release_firmware(fw);
154 kfree(firmware_path);
159 static struct poll_delay_t poll_delay_min_max(int min, int max)
161 struct poll_delay_t result;
168 static int is_complete(volatile struct jr3_channel *channel)
170 return get_s16(&channel->command_word0) == 0;
180 static void set_transforms(volatile struct jr3_channel *channel,
181 struct transform_t transf, short num)
185 num &= 0x000f; /* Make sure that 0 <= num <= 15 */
186 for (i = 0; i < 8; i++) {
188 set_u16(&channel->transforms[num].link[i].link_type,
189 transf.link[i].link_type);
191 set_s16(&channel->transforms[num].link[i].link_amount,
192 transf.link[i].link_amount);
194 if (transf.link[i].link_type == end_x_form) {
200 static void use_transform(volatile struct jr3_channel *channel, short transf_num)
202 set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f));
205 static void use_offset(volatile struct jr3_channel *channel, short offset_num)
207 set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f));
210 static void set_offset(volatile struct jr3_channel *channel)
212 set_s16(&channel->command_word0, 0x0700);
224 static void set_full_scales(volatile struct jr3_channel *channel,
225 struct six_axis_t full_scale)
227 printk("%d %d %d %d %d %d\n",
230 full_scale.fz, full_scale.mx, full_scale.my, full_scale.mz);
231 set_s16(&channel->full_scale.fx, full_scale.fx);
232 set_s16(&channel->full_scale.fy, full_scale.fy);
233 set_s16(&channel->full_scale.fz, full_scale.fz);
234 set_s16(&channel->full_scale.mx, full_scale.mx);
235 set_s16(&channel->full_scale.my, full_scale.my);
236 set_s16(&channel->full_scale.mz, full_scale.mz);
237 set_s16(&channel->command_word0, 0x0a00);
240 static struct six_axis_t get_min_full_scales(volatile struct jr3_channel *channel)
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);
252 static struct six_axis_t get_max_full_scales(volatile struct jr3_channel *channel)
254 struct six_axis_t result;
255 result.fx = get_s16(&channel->max_full_scale.fx);
256 result.fy = get_s16(&channel->max_full_scale.fy);
257 result.fz = get_s16(&channel->max_full_scale.fz);
258 result.mx = get_s16(&channel->max_full_scale.mx);
259 result.my = get_s16(&channel->max_full_scale.my);
260 result.mz = get_s16(&channel->max_full_scale.mz);
264 static int jr3_pci_ai_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
265 struct comedi_insn *insn, unsigned int *data)
268 struct jr3_pci_subdev_private *p;
272 channel = CR_CHAN(insn->chanspec);
273 if (p == NULL || channel > 57) {
279 if (p->state != state_jr3_done ||
280 (get_u16(&p->channel->
281 errors) & (watch_dog | watch_dog2 |
283 /* No sensor or sensor changed */
284 if (p->state == state_jr3_done) {
285 /* Restart polling */
286 p->state = state_jr3_poll;
290 for (i = 0; i < insn->n; i++) {
295 filter = channel / 8;
296 if (p->state != state_jr3_done) {
358 data[i] = F + 0x4000;
360 } else if (channel == 56) {
361 if (p->state != state_jr3_done) {
365 get_u16(&p->channel->model_no);
367 } else if (channel == 57) {
368 if (p->state != state_jr3_done) {
372 get_u16(&p->channel->serial_no);
380 static void jr3_pci_open(struct comedi_device *dev)
383 struct jr3_pci_dev_private *devpriv = dev->private;
385 printk("jr3_pci_open\n");
386 for (i = 0; i < devpriv->n_channels; i++) {
387 struct jr3_pci_subdev_private *p;
389 p = dev->subdevices[i].private;
391 printk("serial: %p %d (%d)\n", p, p->serial_no,
397 int read_idm_word(const u8 *data, size_t size, int *pos, unsigned int *val)
400 if (pos != 0 && val != 0) {
401 /* Skip over non hex */
402 for (; *pos < size && !isxdigit(data[*pos]); (*pos)++) {
406 for (; *pos < size && isxdigit(data[*pos]); (*pos)++) {
407 char ch = tolower(data[*pos]);
409 if ('0' <= ch && ch <= '9') {
410 *val = (*val << 4) + (ch - '0');
411 } else if ('a' <= ch && ch <= 'f') {
412 *val = (*val << 4) + (ch - 'a' + 10);
419 static int jr3_download_firmware(struct comedi_device *dev, const u8 *data,
423 * IDM file format is:
424 * { count, address, data <count> } *
427 int result, more, pos, OK;
434 unsigned int count, addr;
436 more = more && read_idm_word(data, size, &pos, &count);
437 if (more && count == 0xffff) {
441 more = more && read_idm_word(data, size, &pos, &addr);
442 while (more && count > 0) {
444 more = more && read_idm_word(data, size, &pos, &dummy);
453 struct jr3_pci_dev_private *p = dev->private;
455 for (i = 0; i < p->n_channels; i++) {
456 struct jr3_pci_subdev_private *sp;
458 sp = dev->subdevices[i].private;
462 unsigned int count, addr;
464 && read_idm_word(data, size, &pos,
466 if (more && count == 0xffff) {
470 && read_idm_word(data, size, &pos,
472 printk("Loading#%d %4.4x bytes at %4.4x\n", i,
474 while (more && count > 0) {
476 /* 16 bit data, never seen in real life!! */
480 && read_idm_word(data,
483 /* printk("jr3_data, not tested\n"); */
484 /* jr3[addr + 0x20000 * pnum] = data1; */
486 /* Download 24 bit program */
487 unsigned int data1, data2;
490 && read_idm_word(data,
493 && read_idm_word(data,
518 static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice *s)
520 struct poll_delay_t result = poll_delay_min_max(1000, 2000);
521 struct jr3_pci_subdev_private *p = s->private;
524 volatile struct jr3_channel *channel = p->channel;
525 int errors = get_u16(&channel->errors);
527 if (errors != p->errors) {
528 printk("Errors: %x -> %x\n", p->errors, errors);
531 if (errors & (watch_dog | watch_dog2 | sensor_change)) {
532 /* Sensor communication lost, force poll mode */
533 p->state = state_jr3_poll;
537 case state_jr3_poll:{
538 u16 model_no = get_u16(&channel->model_no);
539 u16 serial_no = get_u16(&channel->serial_no);
540 if ((errors & (watch_dog | watch_dog2)) ||
541 model_no == 0 || serial_no == 0) {
543 * Still no sensor, keep on polling. Since it takes up to 10 seconds
544 * for offsets to stabilize, polling each second should suffice.
546 result = poll_delay_min_max(1000, 2000);
550 state_jr3_init_wait_for_offset;
551 result = poll_delay_min_max(1000, 2000);
555 case state_jr3_init_wait_for_offset:{
557 if (p->retries < 10) {
558 /* Wait for offeset to stabilize (< 10 s according to manual) */
559 result = poll_delay_min_max(1000, 2000);
561 struct transform_t transf;
564 get_u16(&channel->model_no);
566 get_u16(&channel->serial_no);
568 printk("Setting transform for channel %d\n", p->channel_no);
569 printk("Sensor Model = %i\n",
571 printk("Sensor Serial = %i\n",
574 /* Transformation all zeros */
575 transf.link[0].link_type =
577 transf.link[0].link_amount = 0;
578 transf.link[1].link_type =
580 transf.link[1].link_amount = 0;
581 transf.link[2].link_type =
583 transf.link[2].link_amount = 0;
584 transf.link[3].link_type =
586 transf.link[3].link_amount = 0;
588 set_transforms(channel, transf, 0);
589 use_transform(channel, 0);
591 state_jr3_init_transform_complete;
592 result = poll_delay_min_max(20, 100); /* Allow 20 ms for completion */
595 case state_jr3_init_transform_complete:{
596 if (!is_complete(channel)) {
597 printk("state_jr3_init_transform_complete complete = %d\n", is_complete(channel));
598 result = poll_delay_min_max(20, 100);
601 struct six_axis_t min_full_scale;
602 struct six_axis_t max_full_scale;
605 get_min_full_scales(channel);
606 printk("Obtained Min. Full Scales:\n");
607 printk("%i ", (min_full_scale).fx);
608 printk("%i ", (min_full_scale).fy);
609 printk("%i ", (min_full_scale).fz);
610 printk("%i ", (min_full_scale).mx);
611 printk("%i ", (min_full_scale).my);
612 printk("%i ", (min_full_scale).mz);
616 get_max_full_scales(channel);
617 printk("Obtained Max. Full Scales:\n");
618 printk("%i ", (max_full_scale).fx);
619 printk("%i ", (max_full_scale).fy);
620 printk("%i ", (max_full_scale).fz);
621 printk("%i ", (max_full_scale).mx);
622 printk("%i ", (max_full_scale).my);
623 printk("%i ", (max_full_scale).mz);
626 set_full_scales(channel,
630 state_jr3_init_set_full_scale_complete;
631 result = poll_delay_min_max(20, 100); /* Allow 20 ms for completion */
635 case state_jr3_init_set_full_scale_complete:{
636 if (!is_complete(channel)) {
637 printk("state_jr3_init_set_full_scale_complete complete = %d\n", is_complete(channel));
638 result = poll_delay_min_max(20, 100);
640 volatile struct force_array *full_scale;
642 /* Use ranges in kN or we will overflow arount 2000N! */
643 full_scale = &channel->full_scale;
644 p->range[0].range.min =
645 -get_s16(&full_scale->fx) *
647 p->range[0].range.max =
648 get_s16(&full_scale->fx) * 1000;
649 p->range[1].range.min =
650 -get_s16(&full_scale->fy) *
652 p->range[1].range.max =
653 get_s16(&full_scale->fy) * 1000;
654 p->range[2].range.min =
655 -get_s16(&full_scale->fz) *
657 p->range[2].range.max =
658 get_s16(&full_scale->fz) * 1000;
659 p->range[3].range.min =
660 -get_s16(&full_scale->mx) * 100;
661 p->range[3].range.max =
662 get_s16(&full_scale->mx) * 100;
663 p->range[4].range.min =
664 -get_s16(&full_scale->my) * 100;
665 p->range[4].range.max =
666 get_s16(&full_scale->my) * 100;
667 p->range[5].range.min =
668 -get_s16(&full_scale->mz) * 100;
669 p->range[5].range.max =
670 get_s16(&full_scale->mz) * 100;
671 p->range[6].range.min = -get_s16(&full_scale->v1) * 100; /* ?? */
672 p->range[6].range.max = get_s16(&full_scale->v1) * 100; /* ?? */
673 p->range[7].range.min = -get_s16(&full_scale->v2) * 100; /* ?? */
674 p->range[7].range.max = get_s16(&full_scale->v2) * 100; /* ?? */
675 p->range[8].range.min = 0;
676 p->range[8].range.max = 65535;
680 for (i = 0; i < 9; i++) {
681 printk("%d %d - %d\n",
690 use_offset(channel, 0);
692 state_jr3_init_use_offset_complete;
693 result = poll_delay_min_max(40, 100); /* Allow 40 ms for completion */
697 case state_jr3_init_use_offset_complete:{
698 if (!is_complete(channel)) {
699 printk("state_jr3_init_use_offset_complete complete = %d\n", is_complete(channel));
700 result = poll_delay_min_max(20, 100);
702 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));
704 set_s16(&channel->offsets.fx, 0);
705 set_s16(&channel->offsets.fy, 0);
706 set_s16(&channel->offsets.fz, 0);
707 set_s16(&channel->offsets.mx, 0);
708 set_s16(&channel->offsets.my, 0);
709 set_s16(&channel->offsets.mz, 0);
713 p->state = state_jr3_done;
717 case state_jr3_done:{
718 poll_delay_min_max(10000, 20000);
722 poll_delay_min_max(1000, 2000);
730 static void jr3_pci_poll_dev(unsigned long data)
733 struct comedi_device *dev = (struct comedi_device *) data;
734 struct jr3_pci_dev_private *devpriv = dev->private;
739 spin_lock_irqsave(&dev->spinlock, flags);
742 /* Poll all channels that are ready to be polled */
743 for (i = 0; i < devpriv->n_channels; i++) {
744 struct jr3_pci_subdev_private *subdevpriv = dev->subdevices[i].private;
745 if (now > subdevpriv->next_time_min) {
746 struct poll_delay_t sub_delay;
748 sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]);
749 subdevpriv->next_time_min =
750 jiffies + msecs_to_jiffies(sub_delay.min);
751 subdevpriv->next_time_max =
752 jiffies + msecs_to_jiffies(sub_delay.max);
753 if (sub_delay.max && sub_delay.max < delay) {
755 * Wake up as late as possible -> poll as many channels as possible
758 delay = sub_delay.max;
762 spin_unlock_irqrestore(&dev->spinlock, flags);
764 devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
765 add_timer(&devpriv->timer);
768 static int jr3_pci_attach(struct comedi_device *dev, struct comedi_devconfig *it)
771 struct pci_dev *card = NULL;
772 int opt_bus, opt_slot, i;
773 struct jr3_pci_dev_private *devpriv;
775 printk("comedi%d: jr3_pci\n", dev->minor);
777 opt_bus = it->options[0];
778 opt_slot = it->options[1];
780 if (sizeof(struct jr3_channel) != 0xc00) {
781 printk("sizeof(struct jr3_channel) = %x [expected %x]\n",
782 (unsigned)sizeof(struct jr3_channel), 0xc00);
786 result = alloc_private(dev, sizeof(struct jr3_pci_dev_private));
791 devpriv = dev->private;
792 init_timer(&devpriv->timer);
794 card = pci_get_device(PCI_VENDOR_ID_JR3, PCI_ANY_ID, card);
799 switch (card->device) {
800 case PCI_DEVICE_ID_JR3_1_CHANNEL:{
801 devpriv->n_channels = 1;
804 case PCI_DEVICE_ID_JR3_2_CHANNEL:{
805 devpriv->n_channels = 2;
808 case PCI_DEVICE_ID_JR3_3_CHANNEL:{
809 devpriv->n_channels = 3;
812 case PCI_DEVICE_ID_JR3_4_CHANNEL:{
813 devpriv->n_channels = 4;
817 devpriv->n_channels = 0;
820 if (devpriv->n_channels >= 1) {
821 if (opt_bus == 0 && opt_slot == 0) {
822 /* Take first available card */
824 } else if (opt_bus == card->bus->number &&
825 opt_slot == PCI_SLOT(card->devfn)) {
826 /* Take requested card */
833 printk(" no jr3_pci found\n");
836 devpriv->pci_dev = card;
837 dev->board_name = "jr3_pci";
840 result = comedi_pci_enable(card, "jr3_pci");
845 devpriv->pci_enabled = 1;
846 devpriv->iobase = ioremap(pci_resource_start(card, 0), sizeof(struct jr3_t));
847 result = alloc_subdevices(dev, devpriv->n_channels);
851 dev->open = jr3_pci_open;
852 for (i = 0; i < devpriv->n_channels; i++) {
853 dev->subdevices[i].type = COMEDI_SUBD_AI;
854 dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND;
855 dev->subdevices[i].n_chan = 8 * 7 + 2;
856 dev->subdevices[i].insn_read = jr3_pci_ai_insn_read;
857 dev->subdevices[i].private =
858 kzalloc(sizeof(struct jr3_pci_subdev_private), GFP_KERNEL);
859 if (dev->subdevices[i].private) {
860 struct jr3_pci_subdev_private *p;
863 p = dev->subdevices[i].private;
864 p->channel = &devpriv->iobase->channel[i].data;
865 printk("p->channel %p %p (%tx)\n",
866 p->channel, devpriv->iobase,
867 ((char *)(p->channel) -
868 (char *)(devpriv->iobase)));
870 for (j = 0; j < 8; j++) {
873 p->range[j].length = 1;
874 p->range[j].range.min = -1000000;
875 p->range[j].range.max = 1000000;
876 for (k = 0; k < 7; k++) {
877 p->range_table_list[j + k * 8] =
878 (struct comedi_lrange *) &p->range[j];
879 p->maxdata_list[j + k * 8] = 0x7fff;
882 p->range[8].length = 1;
883 p->range[8].range.min = 0;
884 p->range[8].range.max = 65536;
886 p->range_table_list[56] =
887 (struct comedi_lrange *) &p->range[8];
888 p->range_table_list[57] =
889 (struct comedi_lrange *) &p->range[8];
890 p->maxdata_list[56] = 0xffff;
891 p->maxdata_list[57] = 0xffff;
892 /* Channel specific range and maxdata */
893 dev->subdevices[i].range_table = 0;
894 dev->subdevices[i].range_table_list =
896 dev->subdevices[i].maxdata = 0;
897 dev->subdevices[i].maxdata_list = p->maxdata_list;
902 devpriv->iobase->channel[0].reset = 0;
904 result = comedi_load_firmware(dev, "jr3pci.idm", jr3_download_firmware);
905 printk("Firmare load %d\n", result);
911 * TODO: use firmware to load preferred offset tables. Suggested
913 * model serial Fx Fy Fz Mx My Mz\n
915 * comedi_load_firmware(dev, "jr3_offsets_table", jr3_download_firmware);
919 * It takes a few milliseconds for software to settle as much as we
920 * can read firmware version
922 msleep_interruptible(25);
923 for (i = 0; i < 0x18; i++) {
925 get_u16(&devpriv->iobase->channel[0].data.
929 /* Start card timer */
930 for (i = 0; i < devpriv->n_channels; i++) {
931 struct jr3_pci_subdev_private *p = dev->subdevices[i].private;
933 p->next_time_min = jiffies + msecs_to_jiffies(500);
934 p->next_time_max = jiffies + msecs_to_jiffies(2000);
937 devpriv->timer.data = (unsigned long)dev;
938 devpriv->timer.function = jr3_pci_poll_dev;
939 devpriv->timer.expires = jiffies + msecs_to_jiffies(1000);
940 add_timer(&devpriv->timer);
946 static int jr3_pci_detach(struct comedi_device *dev)
949 struct jr3_pci_dev_private *devpriv = dev->private;
951 printk("comedi%d: jr3_pci: remove\n", dev->minor);
953 del_timer_sync(&devpriv->timer);
955 if (dev->subdevices) {
956 for (i = 0; i < devpriv->n_channels; i++) {
957 kfree(dev->subdevices[i].private);
961 if (devpriv->iobase) {
962 iounmap((void *)devpriv->iobase);
964 if (devpriv->pci_enabled) {
965 comedi_pci_disable(devpriv->pci_dev);
968 if (devpriv->pci_dev) {
969 pci_dev_put(devpriv->pci_dev);
975 COMEDI_PCI_INITCLEANUP(driver_jr3_pci, jr3_pci_pci_table);