| Linux kernel & device driver programming |
| [ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] |
1 /* 1
2 comedi/drivers/jr3_pci.c
3 hardware driver for JR3/PCI force sensor boa
4
5 COMEDI - Linux Control and Measurement Devic
6 Copyright (C) 2007 Anders Blomdell <anders.b
7
8 This program is free software; you can redis
9 it under the terms of the GNU General Public
10 the Free Software Foundation; either version
11 (at your option) any later version.
12
13 This program is distributed in the hope that
14 but WITHOUT ANY WARRANTY; without even the i
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU G
19 along with this program; if not, write to th
20 Foundation, Inc., 675 Mass Ave, Cambridge, M
21
22 */
23 /*
24 Driver: jr3_pci
25 Description: JR3/PCI force sensor board
26 Author: Anders Blomdell <anders.blomdell@contr
27 Status: works
28 Devices: [JR3] PCI force sensor board (jr3_pci
29
30 The DSP on the board requires initialization
31 be loaded by placing it in /lib/firmware/com
32 The initialization code should be somewhere
33 with your card. One version is available fro
34 in the comedi_nonfree_firmware tarball.
35
36 Configuration options:
37 [0] - PCI bus number - if bus number and slo
38 then driver search fo
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/timer.h>
50 #include "comedi_pci.h"
51 #include "jr3_pci.h"
52
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
58
59 static int jr3_pci_attach(struct comedi_device
60 static int jr3_pci_detach(struct comedi_device
61
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,
67 };
68
69 static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_tab
70 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_
71 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
72 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_
73 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
74 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_
75 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
76 {PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_
77 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
78 {0}
79 };
80
81 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
82
83 struct jr3_pci_dev_private {
84
85 struct pci_dev *pci_dev;
86 int pci_enabled;
87 volatile struct jr3_t *iobase;
88 int n_channels;
89 struct timer_list timer;
90 };
91
92
93 struct poll_delay_t {
94
95 int min;
96 int max;
97 };
98
99
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_compl
107 state_jr3_init_set_full_scale_
108 state_jr3_init_use_offset_comp
109 state_jr3_done
110 } state;
111 int channel_no;
112 int serial_no;
113 int model_no;
114 struct {
115 int length;
116 struct comedi_krange range;
117 } range[9];
118 const struct comedi_lrange *range_tabl
119 unsigned int maxdata_list[8 * 7 + 2];
120 u16 errors;
121 int retries;
122 };
123
124 /* Hotplug firmware loading stuff */
125
126 typedef int comedi_firmware_callback(struct co
127 const u8
128
129 static int comedi_load_firmware(struct comedi_
130 comedi_firmwar
131 {
132 int result = 0;
133 const struct firmware *fw;
134 char *firmware_path;
135 static const char *prefix = "comedi/";
136 struct jr3_pci_dev_private *devpriv =
137
138 firmware_path = kmalloc(strlen(prefix)
139 if (!firmware_path) {
140 result = -ENOMEM;
141 } else {
142 firmware_path[0] = '\0';
143 strcat(firmware_path, prefix);
144 strcat(firmware_path, name);
145 result = request_firmware(&fw,
146 &devpriv->pci_dev->dev
147 if (result == 0) {
148 if (!cb)
149 result = -EINV
150 else
151 result = cb(de
152 release_firmware(fw);
153 }
154 kfree(firmware_path);
155 }
156 return result;
157 }
158
159 static struct poll_delay_t poll_delay_min_max(
160 {
161 struct poll_delay_t result;
162
163 result.min = min;
164 result.max = max;
165 return result;
166 }
167
168 static int is_complete(volatile struct jr3_cha
169 {
170 return get_s16(&channel->command_word0
171 }
172
173 struct transform_t {
174 struct {
175 u16 link_type;
176 s16 link_amount;
177 } link[8];
178 };
179
180 static void set_transforms(volatile struct jr3
181 struct transform_t transf, short num)
182 {
183 int i;
184
185 num &= 0x000f; /* Make sure
186 for (i = 0; i < 8; i++) {
187
188 set_u16(&channel->transforms[n
189 transf.link[i].link_ty
190 udelay(1);
191 set_s16(&channel->transforms[n
192 transf.link[i].link_am
193 udelay(1);
194 if (transf.link[i].link_type =
195 break;
196 }
197 }
198 }
199
200 static void use_transform(volatile struct jr3_
201 {
202 set_s16(&channel->command_word0, 0x050
203 }
204
205 static void use_offset(volatile struct jr3_cha
206 {
207 set_s16(&channel->command_word0, 0x060
208 }
209
210 static void set_offset(volatile struct jr3_cha
211 {
212 set_s16(&channel->command_word0, 0x070
213 }
214
215 struct six_axis_t {
216 s16 fx;
217 s16 fy;
218 s16 fz;
219 s16 mx;
220 s16 my;
221 s16 mz;
222 };
223
224 static void set_full_scales(volatile struct jr
225 struct six_axis_t full_scale)
226 {
227 printk("%d %d %d %d %d %d\n",
228 full_scale.fx,
229 full_scale.fy,
230 full_scale.fz, full_scale.mx,
231 set_s16(&channel->full_scale.fx, full_
232 set_s16(&channel->full_scale.fy, full_
233 set_s16(&channel->full_scale.fz, full_
234 set_s16(&channel->full_scale.mx, full_
235 set_s16(&channel->full_scale.my, full_
236 set_s16(&channel->full_scale.mz, full_
237 set_s16(&channel->command_word0, 0x0a0
238 }
239
240 static struct six_axis_t get_min_full_scales(v
241 {
242 struct six_axis_t result;
243 result.fx = get_s16(&channel->min_full
244 result.fy = get_s16(&channel->min_full
245 result.fz = get_s16(&channel->min_full
246 result.mx = get_s16(&channel->min_full
247 result.my = get_s16(&channel->min_full
248 result.mz = get_s16(&channel->min_full
249 return result;
250 }
251
252 static struct six_axis_t get_max_full_scales(v
253 {
254 struct six_axis_t result;
255 result.fx = get_s16(&channel->max_full
256 result.fy = get_s16(&channel->max_full
257 result.fz = get_s16(&channel->max_full
258 result.mx = get_s16(&channel->max_full
259 result.my = get_s16(&channel->max_full
260 result.mz = get_s16(&channel->max_full
261 return result;
262 }
263
264 static int jr3_pci_ai_insn_read(struct comedi_
265 struct comedi_insn *insn, unsigned int
266 {
267 int result;
268 struct jr3_pci_subdev_private *p;
269 int channel;
270
271 p = s->private;
272 channel = CR_CHAN(insn->chanspec);
273 if (p == NULL || channel > 57) {
274 result = -EINVAL;
275 } else {
276 int i;
277
278 result = insn->n;
279 if (p->state != state_jr3_done
280 (get_u16(&p->channel->
281 errors
282 sensor
283 /* No sensor or sensor
284 if (p->state == state_
285 /* Restart pol
286 p->state = sta
287 }
288 result = -EAGAIN;
289 }
290 for (i = 0; i < insn->n; i++)
291 if (channel < 56) {
292 int axis, filt
293
294 axis = channel
295 filter = chann
296 if (p->state !
297 data[i
298 } else {
299 int F
300 switch
301 case 0
302
303
304
305
306
307
308 case 1
309
310
311
312
313
314
315 case 2
316
317
318
319
320
321
322 case 3
323
324
325
326
327
328
329 case 4
330
331
332
333
334
335
336 case 5
337
338
339
340
341
342
343 case 6
344
345
346
347
348
349
350 case 7
351
352
353
354
355
356
357 }
358 data[i
359 }
360 } else if (channel ==
361 if (p->state !
362 data[i
363 } else {
364 data[i
365
366 }
367 } else if (channel ==
368 if (p->state !
369 data[i
370 } else {
371 data[i
372
373 }
374 }
375 }
376 }
377 return result;
378 }
379
380 static void jr3_pci_open(struct comedi_device
381 {
382 int i;
383 struct jr3_pci_dev_private *devpriv =
384
385 printk("jr3_pci_open\n");
386 for (i = 0; i < devpriv->n_channels; i
387 struct jr3_pci_subdev_private
388
389 p = dev->subdevices[i].private
390 if (p) {
391 printk("serial: %p %d
392 p->channel_no)
393 }
394 }
395 }
396
397 int read_idm_word(const u8 *data, size_t size,
398 {
399 int result = 0;
400 if (pos != 0 && val != 0) {
401 /* Skip over non hex */
402 for (; *pos < size && !isxdigi
403 }
404 /* Collect value */
405 *val = 0;
406 for (; *pos < size && isxdigit
407 char ch = tolower(data
408 result = 1;
409 if ('' <= ch && ch <=
410 *val = (*val <
411 } else if ('a' <= ch &
412 *val = (*val <
413 }
414 }
415 }
416 return result;
417 }
418
419 static int jr3_download_firmware(struct comedi
420 size_t size)
421 {
422 /*
423 * IDM file format is:
424 * { count, address, data <count> }
425 * ffff
426 */
427 int result, more, pos, OK;
428
429 result = 0;
430 more = 1;
431 pos = 0;
432 OK = 0;
433 while (more) {
434 unsigned int count, addr;
435
436 more = more && read_idm_word(d
437 if (more && count == 0xffff) {
438 OK = 1;
439 break;
440 }
441 more = more && read_idm_word(d
442 while (more && count > 0) {
443 unsigned int dummy;
444 more = more && read_id
445 count--;
446 }
447 }
448
449 if (!OK) {
450 result = -ENODATA;
451 } else {
452 int i;
453 struct jr3_pci_dev_private *p
454
455 for (i = 0; i < p->n_channels;
456 struct jr3_pci_subdev_
457
458 sp = dev->subdevices[i
459 more = 1;
460 pos = 0;
461 while (more) {
462 unsigned int c
463 more = more
464 && rea
465 &count
466 if (more && co
467 break;
468 }
469 more = more
470 && rea
471 &addr)
472 printk("Loadin
473 count,
474 while (more &&
475 if (ad
476
477
478
479
480
481
482
483
484
485 } else
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 }
510 addr++
511 }
512 }
513 }
514 }
515 return result;
516 }
517
518 static struct poll_delay_t jr3_pci_poll_subdev
519 {
520 struct poll_delay_t result = poll_dela
521 struct jr3_pci_subdev_private *p = s->
522
523 if (p) {
524 volatile struct jr3_channel *c
525 int errors = get_u16(&channel-
526
527 if (errors != p->errors) {
528 printk("Errors: %x ->
529 p->errors = errors;
530 }
531 if (errors & (watch_dog | watc
532 /* Sensor communicati
533 p->state = state_jr3_p
534
535 }
536 switch (p->state) {
537 case state_jr3_poll:{
538 u16 model_no =
539 u16 serial_no
540 if ((errors &
541 model_
542 /*
543 * Still no sensor, keep on polling. Since it
544 * for offsets to stabilize, polling each seco
545 */
546 result
547 } else {
548 p->ret
549 p->sta
550
551 result
552 }
553 }
554 break;
555 case state_jr3_init_wait_for_o
556 p->retries++;
557 if (p->retries
558 /* Wa
559 result
560 } else {
561 struct
562
563 p->mod
564
565 p->ser
566
567
568 printk
569 printk
570
571 printk
572
573
574 /* Tr
575 transf
576
577 transf
578 transf
579
580 transf
581 transf
582
583 transf
584 transf
585
586 transf
587
588 set_tr
589 use_tr
590 p->sta
591
592 result
593 }
594 } break;
595 case state_jr3_init_transform_
596 if (!is_comple
597 printk
598 result
599 } else {
600 /* Se
601 struct
602 struct
603
604 min_fu
605
606 printk
607 printk
608 printk
609 printk
610 printk
611 printk
612 printk
613 printk
614
615 max_fu
616
617 printk
618 printk
619 printk
620 printk
621 printk
622 printk
623 printk
624 printk
625
626 set_fu
627
628
629 p->sta
630
631 result
632 }
633 }
634 break;
635 case state_jr3_init_set_full_s
636 if (!is_comple
637 printk
638 result
639 } else {
640 volati
641
642 /* Us
643 full_s
644 p->ran
645
646
647 p->ran
648
649 p->ran
650
651
652 p->ran
653
654 p->ran
655
656
657 p->ran
658
659 p->ran
660
661 p->ran
662
663 p->ran
664
665 p->ran
666
667 p->ran
668
669 p->ran
670
671 p->ran
672 p->ran
673 p->ran
674 p->ran
675 p->ran
676 p->ran
677
678 {
679
680
681
682
683
684
685
686
687
688 }
689
690 use_of
691 p->sta
692
693 result
694 }
695 }
696 break;
697 case state_jr3_init_use_offset
698 if (!is_comple
699 printk
700 result
701 } else {
702 printk
703
704 set_s1
705 set_s1
706 set_s1
707 set_s1
708 set_s1
709 set_s1
710
711 set_of
712
713 p->sta
714 }
715 }
716 break;
717 case state_jr3_done:{
718 poll_delay_min
719 }
720 break;
721 default:{
722 poll_delay_min
723 }
724 break;
725 }
726 }
727 return result;
728 }
729
730 static void jr3_pci_poll_dev(unsigned long dat
731 {
732 unsigned long flags;
733 struct comedi_device *dev = (struct co
734 struct jr3_pci_dev_private *devpriv =
735 unsigned long now;
736 int delay;
737 int i;
738
739 spin_lock_irqsave(&dev->spinlock, flag
740 delay = 1000;
741 now = jiffies;
742 /* Poll all channels that are ready t
743 for (i = 0; i < devpriv->n_channels; i
744 struct jr3_pci_subdev_private
745 if (now > subdevpriv->next_tim
746 struct poll_delay_t su
747
748 sub_delay = jr3_pci_po
749 subdevpriv->next_time_
750 jiffies + msec
751 subdevpriv->next_time_
752 jiffies + msec
753 if (sub_delay.max && s
754 /*
755 * Wake up as late as possible -> poll as many
756 * at once
757 */
758 delay = sub_de
759 }
760 }
761 }
762 spin_unlock_irqrestore(&dev->spinlock,
763
764 devpriv->timer.expires = jiffies + mse
765 add_timer(&devpriv->timer);
766 }
767
768 static int jr3_pci_attach(struct comedi_device
769 {
770 int result = 0;
771 struct pci_dev *card = NULL;
772 int opt_bus, opt_slot, i;
773 struct jr3_pci_dev_private *devpriv;
774
775 printk("comedi%d: jr3_pci\n", dev->min
776
777 opt_bus = it->options[0];
778 opt_slot = it->options[1];
779
780 if (sizeof(struct jr3_channel) != 0xc0
781 printk("sizeof(struct jr3_chan
782 (unsigned)sizeof(struc
783 return -EINVAL;
784 }
785
786 result = alloc_private(dev, sizeof(str
787 if (result < 0) {
788 return -ENOMEM;
789 }
790 card = NULL;
791 devpriv = dev->private;
792 init_timer(&devpriv->timer);
793 while (1) {
794 card = pci_get_device(PCI_VEND
795 if (card == NULL) {
796 /* No card found */
797 break;
798 } else {
799 switch (card->device)
800 case PCI_DEVICE_ID_JR3
801 devpri
802 }
803 break;
804 case PCI_DEVICE_ID_JR3
805 devpri
806 }
807 break;
808 case PCI_DEVICE_ID_JR3
809 devpri
810 }
811 break;
812 case PCI_DEVICE_ID_JR3
813 devpri
814 }
815 break;
816 default:{
817 devpri
818 }
819 }
820 if (devpriv->n_channel
821 if (opt_bus ==
822 /* Tak
823 break;
824 } else if (opt
825 opt_sl
826 /* Tak
827 break;
828 }
829 }
830 }
831 }
832 if (!card) {
833 printk(" no jr3_pci found\n");
834 return -EIO;
835 } else {
836 devpriv->pci_dev = card;
837 dev->board_name = "jr3_pci";
838 }
839
840 result = comedi_pci_enable(card, "jr3_
841 if (result < 0) {
842 return -EIO;
843 }
844
845 devpriv->pci_enabled = 1;
846 devpriv->iobase = ioremap(pci_resource
847 result = alloc_subdevices(dev, devpriv
848 if (result < 0)
849 goto out;
850
851 dev->open = jr3_pci_open;
852 for (i = 0; i < devpriv->n_channels; i
853 dev->subdevices[i].type = COME
854 dev->subdevices[i].subdev_flag
855 dev->subdevices[i].n_chan = 8
856 dev->subdevices[i].insn_read =
857 dev->subdevices[i].private =
858 kzalloc(sizeof(struct
859 if (dev->subdevices[i].private
860 struct jr3_pci_subdev_
861 int j;
862
863 p = dev->subdevices[i]
864 p->channel = &devpriv-
865 printk("p->channel %p
866 p->channel, de
867 ((char *)(p->c
868 (char
869 p->channel_no = i;
870 for (j = 0; j < 8; j++
871 int k;
872
873 p->range[j].le
874 p->range[j].ra
875 p->range[j].ra
876 for (k = 0; k
877 p->ran
878
879 p->max
880 }
881 }
882 p->range[8].length = 1
883 p->range[8].range.min
884 p->range[8].range.max
885
886 p->range_table_list[56
887 (struct comedi
888 p->range_table_list[57
889 (struct comedi
890 p->maxdata_list[56] =
891 p->maxdata_list[57] =
892 /* Channel specific r
893 dev->subdevices[i].ran
894 dev->subdevices[i].ran
895 p->range_table
896 dev->subdevices[i].max
897 dev->subdevices[i].max
898 }
899 }
900
901 /* Reset DSP card */
902 devpriv->iobase->channel[0].reset = 0;
903
904 result = comedi_load_firmware(dev, "jr
905 printk("Firmare load %d\n", result);
906
907 if (result < 0) {
908 goto out;
909 }
910 /*
911 * TODO: use firmware to load preferred offset
912 * format:
913 * model serial Fx Fy Fz Mx My Mz\n
914 *
915 * comedi_load_firmware(dev, "jr3_offsets_
916 */
917
918 /*
919 * It takes a few milliseconds for software to
920 * can read firmware version
921 */
922 msleep_interruptible(25);
923 for (i = 0; i < 0x18; i++) {
924 printk("%c",
925 get_u16(&devpriv->ioba
926 copyright[i])
927 }
928
929 /* Start card timer */
930 for (i = 0; i < devpriv->n_channels; i
931 struct jr3_pci_subdev_private
932
933 p->next_time_min = jiffies + m
934 p->next_time_max = jiffies + m
935 }
936
937 devpriv->timer.data = (unsigned long)d
938 devpriv->timer.function = jr3_pci_poll
939 devpriv->timer.expires = jiffies + mse
940 add_timer(&devpriv->timer);
941
942 out:
943 return result;
944 }
945
946 static int jr3_pci_detach(struct comedi_device
947 {
948 int i;
949 struct jr3_pci_dev_private *devpriv =
950
951 printk("comedi%d: jr3_pci: remove\n",
952 if (devpriv) {
953 del_timer_sync(&devpriv->timer
954
955 if (dev->subdevices) {
956 for (i = 0; i < devpri
957 kfree(dev->sub
958 }
959 }
960
961 if (devpriv->iobase) {
962 iounmap((void *)devpri
963 }
964 if (devpriv->pci_enabled) {
965 comedi_pci_disable(dev
966 }
967
968 if (devpriv->pci_dev) {
969 pci_dev_put(devpriv->p
970 }
971 }
972 return 0;
973 }
974
975 COMEDI_PCI_INITCLEANUP(driver_jr3_pci, jr3_pci
976
| This page was automatically generated by the LXR engine. |