]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/staging/comedi/drivers/jr3_pci.c
Merge branch 'linus' into x86/mm
[mirror_ubuntu-hirsute-kernel.git] / drivers / staging / comedi / drivers / jr3_pci.c
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 /*
24 Driver: jr3_pci
25 Description: JR3/PCI force sensor board
26 Author: Anders Blomdell <anders.blomdell@control.lth.se>
27 Status: works
28 Devices: [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
53 static void comedi_fw_release(struct device *dev)
54 {
55 printk(KERN_DEBUG "firmware_sample_driver: ghost_release\n");
56 }
57
58 static struct device comedi_fw_device = {
59 .init_name = "comedi",
60 .release = comedi_fw_release
61 };
62
63 typedef int comedi_firmware_callback(struct comedi_device * dev,
64 const u8 * data, size_t size);
65
66 static 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
106 static int jr3_pci_attach(struct comedi_device * dev, struct comedi_devconfig * it);
107 static int jr3_pci_detach(struct comedi_device * dev);
108
109 static 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
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},
125 {0}
126 };
127
128 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
129
130 struct 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
140 struct poll_delay_t {
141
142 int min;
143 int max;
144 };
145
146
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,
156 state_jr3_done
157 } state;
158 int channel_no;
159 int serial_no;
160 int model_no;
161 struct {
162 int length;
163 struct comedi_krange range;
164 } range[9];
165 const struct comedi_lrange *range_table_list[8 * 7 + 2];
166 unsigned int maxdata_list[8 * 7 + 2];
167 u16 errors;
168 int retries;
169 };
170
171 static struct poll_delay_t poll_delay_min_max(int min, int max)
172 {
173 struct poll_delay_t result;
174
175 result.min = min;
176 result.max = max;
177 return result;
178 }
179
180 static int is_complete(volatile struct jr3_channel *channel)
181 {
182 return get_s16(&channel->command_word0) == 0;
183 }
184
185 struct transform_t {
186 struct {
187 u16 link_type;
188 s16 link_amount;
189 } link[8];
190 };
191
192 static void set_transforms(volatile struct jr3_channel *channel,
193 struct transform_t transf, short num)
194 {
195 int i;
196
197 num &= 0x000f; // Make sure that 0 <= num <= 15
198 for (i = 0; i < 8; i++) {
199
200 set_u16(&channel->transforms[num].link[i].link_type,
201 transf.link[i].link_type);
202 comedi_udelay(1);
203 set_s16(&channel->transforms[num].link[i].link_amount,
204 transf.link[i].link_amount);
205 comedi_udelay(1);
206 if (transf.link[i].link_type == end_x_form) {
207 break;
208 }
209 }
210 }
211
212 static void use_transform(volatile struct jr3_channel *channel, short transf_num)
213 {
214 set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f));
215 }
216
217 static void use_offset(volatile struct jr3_channel *channel, short offset_num)
218 {
219 set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f));
220 }
221
222 static void set_offset(volatile struct jr3_channel *channel)
223 {
224 set_s16(&channel->command_word0, 0x0700);
225 }
226
227 struct six_axis_t {
228 s16 fx;
229 s16 fy;
230 s16 fz;
231 s16 mx;
232 s16 my;
233 s16 mz;
234 };
235
236 static void set_full_scales(volatile struct jr3_channel *channel,
237 struct six_axis_t full_scale)
238 {
239 printk("%d %d %d %d %d %d\n",
240 full_scale.fx,
241 full_scale.fy,
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);
250 }
251
252 static struct six_axis_t get_min_full_scales(volatile struct jr3_channel *channel)
253 {
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);
261 return result;
262 }
263
264 static struct six_axis_t get_max_full_scales(volatile struct jr3_channel *channel)
265 {
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);
273 return result;
274 }
275
276 static int jr3_pci_ai_insn_read(struct comedi_device * dev, struct comedi_subdevice * s,
277 struct comedi_insn * insn, unsigned int * data)
278 {
279 int result;
280 struct jr3_pci_subdev_private *p;
281 int channel;
282
283 p = s->private;
284 channel = CR_CHAN(insn->chanspec);
285 if (p == NULL || channel > 57) {
286 result = -EINVAL;
287 } else {
288 int i;
289
290 result = insn->n;
291 if (p->state != state_jr3_done ||
292 (get_u16(&p->channel->
293 errors) & (watch_dog | watch_dog2 |
294 sensor_change))) {
295 /* No sensor or sensor changed */
296 if (p->state == state_jr3_done) {
297 /* Restart polling */
298 p->state = state_jr3_poll;
299 }
300 result = -EAGAIN;
301 }
302 for (i = 0; i < insn->n; i++) {
303 if (channel < 56) {
304 int axis, filter;
305
306 axis = channel % 8;
307 filter = channel / 8;
308 if (p->state != state_jr3_done) {
309 data[i] = 0;
310 } else {
311 int F = 0;
312 switch (axis) {
313 case 0:{
314 F = get_s16(&p->
315 channel->
316 filter[filter].
317 fx);
318 }
319 break;
320 case 1:{
321 F = get_s16(&p->
322 channel->
323 filter[filter].
324 fy);
325 }
326 break;
327 case 2:{
328 F = get_s16(&p->
329 channel->
330 filter[filter].
331 fz);
332 }
333 break;
334 case 3:{
335 F = get_s16(&p->
336 channel->
337 filter[filter].
338 mx);
339 }
340 break;
341 case 4:{
342 F = get_s16(&p->
343 channel->
344 filter[filter].
345 my);
346 }
347 break;
348 case 5:{
349 F = get_s16(&p->
350 channel->
351 filter[filter].
352 mz);
353 }
354 break;
355 case 6:{
356 F = get_s16(&p->
357 channel->
358 filter[filter].
359 v1);
360 }
361 break;
362 case 7:{
363 F = get_s16(&p->
364 channel->
365 filter[filter].
366 v2);
367 }
368 break;
369 }
370 data[i] = F + 0x4000;
371 }
372 } else if (channel == 56) {
373 if (p->state != state_jr3_done) {
374 data[i] = 0;
375 } else {
376 data[i] =
377 get_u16(&p->channel->model_no);
378 }
379 } else if (channel == 57) {
380 if (p->state != state_jr3_done) {
381 data[i] = 0;
382 } else {
383 data[i] =
384 get_u16(&p->channel->serial_no);
385 }
386 }
387 }
388 }
389 return result;
390 }
391
392 static void jr3_pci_open(struct comedi_device * dev)
393 {
394 int i;
395 struct jr3_pci_dev_private *devpriv = dev->private;
396
397 printk("jr3_pci_open\n");
398 for (i = 0; i < devpriv->n_channels; i++) {
399 struct jr3_pci_subdev_private *p;
400
401 p = dev->subdevices[i].private;
402 if (p) {
403 printk("serial: %p %d (%d)\n", p, p->serial_no,
404 p->channel_no);
405 }
406 }
407 }
408
409 int read_idm_word(const u8 * data, size_t size, int *pos, unsigned int *val)
410 {
411 int result = 0;
412 if (pos != 0 && val != 0) {
413 // Skip over non hex
414 for (; *pos < size && !isxdigit(data[*pos]); (*pos)++) {
415 }
416 // Collect value
417 *val = 0;
418 for (; *pos < size && isxdigit(data[*pos]); (*pos)++) {
419 char ch = tolower(data[*pos]);
420 result = 1;
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);
425 }
426 }
427 }
428 return result;
429 }
430
431 static int jr3_download_firmware(struct comedi_device * dev, const u8 * data,
432 size_t size)
433 {
434 /*
435 * IDM file format is:
436 * { count, address, data <count> } *
437 * ffff
438 */
439 int result, more, pos, OK;
440
441 result = 0;
442 more = 1;
443 pos = 0;
444 OK = 0;
445 while (more) {
446 unsigned int count, addr;
447
448 more = more && read_idm_word(data, size, &pos, &count);
449 if (more && count == 0xffff) {
450 OK = 1;
451 break;
452 }
453 more = more && read_idm_word(data, size, &pos, &addr);
454 while (more && count > 0) {
455 unsigned int dummy;
456 more = more && read_idm_word(data, size, &pos, &dummy);
457 count--;
458 }
459 }
460
461 if (!OK) {
462 result = -ENODATA;
463 } else {
464 int i;
465 struct jr3_pci_dev_private *p = dev->private;
466
467 for (i = 0; i < p->n_channels; i++) {
468 struct jr3_pci_subdev_private *sp;
469
470 sp = dev->subdevices[i].private;
471 more = 1;
472 pos = 0;
473 while (more) {
474 unsigned int count, addr;
475 more = more
476 && read_idm_word(data, size, &pos,
477 &count);
478 if (more && count == 0xffff) {
479 break;
480 }
481 more = more
482 && read_idm_word(data, size, &pos,
483 &addr);
484 printk("Loading#%d %4.4x bytes at %4.4x\n", i,
485 count, addr);
486 while (more && count > 0) {
487 if (addr & 0x4000) {
488 // 16 bit data, never seen in real life!!
489 unsigned int data1;
490
491 more = more
492 && read_idm_word(data,
493 size, &pos, &data1);
494 count--;
495 // printk("jr3_data, not tested\n");
496 // jr3[addr + 0x20000 * pnum] = data1;
497 } else {
498 // Download 24 bit program
499 unsigned int data1, data2;
500
501 more = more
502 && read_idm_word(data,
503 size, &pos, &data1);
504 more = more
505 && read_idm_word(data,
506 size, &pos, &data2);
507 count -= 2;
508 if (more) {
509 set_u16(&p->iobase->
510 channel[i].
511 program_low
512 [addr], data1);
513 comedi_udelay(1);
514 set_u16(&p->iobase->
515 channel[i].
516 program_high
517 [addr], data2);
518 comedi_udelay(1);
519
520 }
521 }
522 addr++;
523 }
524 }
525 }
526 }
527 return result;
528 }
529
530 static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice * s)
531 {
532 struct poll_delay_t result = poll_delay_min_max(1000, 2000);
533 struct jr3_pci_subdev_private *p = s->private;
534
535 if (p) {
536 volatile struct jr3_channel *channel = p->channel;
537 int errors = get_u16(&channel->errors);
538
539 if (errors != p->errors) {
540 printk("Errors: %x -> %x\n", p->errors, errors);
541 p->errors = errors;
542 }
543 if (errors & (watch_dog | watch_dog2 | sensor_change)) {
544 // Sensor communication lost, force poll mode
545 p->state = state_jr3_poll;
546
547 }
548 switch (p->state) {
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);
558 } else {
559 p->retries = 0;
560 p->state =
561 state_jr3_init_wait_for_offset;
562 result = poll_delay_min_max(1000, 2000);
563 }
564 }
565 break;
566 case state_jr3_init_wait_for_offset:{
567 p->retries++;
568 if (p->retries < 10) {
569 // Wait for offeset to stabilize (< 10 s according to manual)
570 result = poll_delay_min_max(1000, 2000);
571 } else {
572 struct transform_t transf;
573
574 p->model_no =
575 get_u16(&channel->model_no);
576 p->serial_no =
577 get_u16(&channel->serial_no);
578
579 printk("Setting transform for channel %d\n", p->channel_no);
580 printk("Sensor Model = %i\n",
581 p->model_no);
582 printk("Sensor Serial = %i\n",
583 p->serial_no);
584
585 // Transformation all zeros
586 transf.link[0].link_type =
587 (enum link_types)0;
588 transf.link[0].link_amount = 0;
589 transf.link[1].link_type =
590 (enum link_types)0;
591 transf.link[1].link_amount = 0;
592 transf.link[2].link_type =
593 (enum link_types)0;
594 transf.link[2].link_amount = 0;
595 transf.link[3].link_type =
596 (enum link_types)0;
597 transf.link[3].link_amount = 0;
598
599 set_transforms(channel, transf, 0);
600 use_transform(channel, 0);
601 p->state =
602 state_jr3_init_transform_complete;
603 result = poll_delay_min_max(20, 100); // Allow 20 ms for completion
604 }
605 } break;
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);
610 } else {
611 // Set full scale
612 struct six_axis_t min_full_scale;
613 struct six_axis_t max_full_scale;
614
615 min_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);
624 printk("\n");
625
626 max_full_scale =
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);
635 printk("\n");
636
637 set_full_scales(channel,
638 max_full_scale);
639
640 p->state =
641 state_jr3_init_set_full_scale_complete;
642 result = poll_delay_min_max(20, 100); // Allow 20 ms for completion
643 }
644 }
645 break;
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);
650 } else {
651 volatile struct force_array *full_scale;
652
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) *
657 1000;
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) *
662 1000;
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) *
667 1000;
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;
688
689 {
690 int i;
691 for (i = 0; i < 9; i++) {
692 printk("%d %d - %d\n",
693 i,
694 p->range[i].
695 range.min,
696 p->range[i].
697 range.max);
698 }
699 }
700
701 use_offset(channel, 0);
702 p->state =
703 state_jr3_init_use_offset_complete;
704 result = poll_delay_min_max(40, 100); // Allow 40 ms for completion
705 }
706 }
707 break;
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);
712 } else {
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));
714
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);
721
722 set_offset(channel);
723
724 p->state = state_jr3_done;
725 }
726 }
727 break;
728 case state_jr3_done:{
729 poll_delay_min_max(10000, 20000);
730 }
731 break;
732 default:{
733 poll_delay_min_max(1000, 2000);
734 }
735 break;
736 }
737 }
738 return result;
739 }
740
741 static void jr3_pci_poll_dev(unsigned long data)
742 {
743 unsigned long flags;
744 struct comedi_device *dev = (struct comedi_device *) data;
745 struct jr3_pci_dev_private *devpriv = dev->private;
746 unsigned long now;
747 int delay;
748 int i;
749
750 comedi_spin_lock_irqsave(&dev->spinlock, flags);
751 delay = 1000;
752 now = jiffies;
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;
758
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
766 // possible at once
767 delay = sub_delay.max;
768 }
769 }
770 }
771 comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
772
773 devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
774 add_timer(&devpriv->timer);
775 }
776
777 static int jr3_pci_attach(struct comedi_device * dev, struct comedi_devconfig * it)
778 {
779 int result = 0;
780 struct pci_dev *card = NULL;
781 int opt_bus, opt_slot, i;
782 struct jr3_pci_dev_private *devpriv;
783
784 printk("comedi%d: jr3_pci\n", dev->minor);
785
786 opt_bus = it->options[0];
787 opt_slot = it->options[1];
788
789 if (sizeof(struct jr3_channel) != 0xc00) {
790 printk("sizeof(struct jr3_channel) = %x [expected %x]\n",
791 (unsigned)sizeof(struct jr3_channel), 0xc00);
792 return -EINVAL;
793 }
794
795 result = alloc_private(dev, sizeof(struct jr3_pci_dev_private));
796 if (result < 0) {
797 return -ENOMEM;
798 }
799 card = NULL;
800 devpriv = dev->private;
801 init_timer(&devpriv->timer);
802 while (1) {
803 card = pci_get_device(PCI_VENDOR_ID_JR3, PCI_ANY_ID, card);
804 if (card == NULL) {
805 /* No card found */
806 break;
807 } else {
808 switch (card->device) {
809 case PCI_DEVICE_ID_JR3_1_CHANNEL:{
810 devpriv->n_channels = 1;
811 }
812 break;
813 case PCI_DEVICE_ID_JR3_2_CHANNEL:{
814 devpriv->n_channels = 2;
815 }
816 break;
817 case PCI_DEVICE_ID_JR3_3_CHANNEL:{
818 devpriv->n_channels = 3;
819 }
820 break;
821 case PCI_DEVICE_ID_JR3_4_CHANNEL:{
822 devpriv->n_channels = 4;
823 }
824 break;
825 default:{
826 devpriv->n_channels = 0;
827 }
828 }
829 if (devpriv->n_channels >= 1) {
830 if (opt_bus == 0 && opt_slot == 0) {
831 /* Take first available card */
832 break;
833 } else if (opt_bus == card->bus->number &&
834 opt_slot == PCI_SLOT(card->devfn)) {
835 /* Take requested card */
836 break;
837 }
838 }
839 }
840 }
841 if (!card) {
842 printk(" no jr3_pci found\n");
843 return -EIO;
844 } else {
845 devpriv->pci_dev = card;
846 dev->board_name = "jr3_pci";
847 }
848 if ((result = comedi_pci_enable(card, "jr3_pci")) < 0) {
849 return -EIO;
850 }
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);
854 if (result < 0)
855 goto out;
856
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;
867 int j;
868
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)));
875 p->channel_no = i;
876 for (j = 0; j < 8; j++) {
877 int k;
878
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;
886 }
887 }
888 p->range[8].length = 1;
889 p->range[8].range.min = 0;
890 p->range[8].range.max = 65536;
891
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 =
901 p->range_table_list;
902 dev->subdevices[i].maxdata = 0;
903 dev->subdevices[i].maxdata_list = p->maxdata_list;
904 }
905 }
906
907 // Reset DSP card
908 devpriv->iobase->channel[0].reset = 0;
909
910 result = comedi_load_firmware(dev, "jr3pci.idm", jr3_download_firmware);
911 printk("Firmare load %d\n", result);
912
913 if (result < 0) {
914 goto out;
915 }
916 // TODO: use firmware to load preferred offset tables. Suggested format:
917 // model serial Fx Fy Fz Mx My Mz\n
918 //
919 // comedi_load_firmware(dev, "jr3_offsets_table", jr3_download_firmware);
920
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++) {
925 printk("%c",
926 get_u16(&devpriv->iobase->channel[0].data.
927 copyright[i]) >> 8);
928 }
929
930 // Start card timer
931 for (i = 0; i < devpriv->n_channels; i++) {
932 struct jr3_pci_subdev_private *p = dev->subdevices[i].private;
933
934 p->next_time_min = jiffies + msecs_to_jiffies(500);
935 p->next_time_max = jiffies + msecs_to_jiffies(2000);
936 }
937
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);
942
943 out:
944 return result;
945 }
946
947 static int jr3_pci_detach(struct comedi_device * dev)
948 {
949 int i;
950 struct jr3_pci_dev_private *devpriv = dev->private;
951
952 printk("comedi%d: jr3_pci: remove\n", dev->minor);
953 if (devpriv) {
954 del_timer_sync(&devpriv->timer);
955
956 if (dev->subdevices) {
957 for (i = 0; i < devpriv->n_channels; i++) {
958 kfree(dev->subdevices[i].private);
959 }
960 }
961
962 if (devpriv->iobase) {
963 iounmap((void *)devpriv->iobase);
964 }
965 if (devpriv->pci_enabled) {
966 comedi_pci_disable(devpriv->pci_dev);
967 }
968
969 if (devpriv->pci_dev) {
970 pci_dev_put(devpriv->pci_dev);
971 }
972 }
973 return 0;
974 }
975
976 COMEDI_PCI_INITCLEANUP(driver_jr3_pci, jr3_pci_pci_table);