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