Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
jr3_pci.c
Go to the documentation of this file.
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 <[email protected]>
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 <[email protected]>
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 
62 
63  struct pci_dev *pci_dev;
65  volatile struct jr3_t *iobase;
67  struct timer_list timer;
68 };
69 
70 struct poll_delay_t {
71 
72  int min;
73  int max;
74 };
75 
77  volatile struct jr3_channel *channel;
78  unsigned long next_time_min;
79  unsigned long next_time_max;
86  } state;
88  int serial_no;
89  int model_no;
90  struct {
91  int length;
93  } range[9];
94  const struct comedi_lrange *range_table_list[8 * 7 + 2];
95  unsigned int maxdata_list[8 * 7 + 2];
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 {
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 {
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 static int read_idm_word(const u8 *data, size_t size, int *pos,
366  unsigned int *val)
367 {
368  int result = 0;
369  if (pos && val) {
370  /* Skip over non hex */
371  for (; *pos < size && !isxdigit(data[*pos]); (*pos)++) {
372  }
373  /* Collect value */
374  *val = 0;
375  for (; *pos < size; (*pos)++) {
376  int value;
377  value = hex_to_bin(data[*pos]);
378  if (value >= 0) {
379  result = 1;
380  *val = (*val << 4) + value;
381  } else
382  break;
383  }
384  }
385  return result;
386 }
387 
388 static int jr3_download_firmware(struct comedi_device *dev, const u8 * data,
389  size_t size)
390 {
391  /*
392  * IDM file format is:
393  * { count, address, data <count> } *
394  * ffff
395  */
396  int result, more, pos, OK;
397 
398  result = 0;
399  more = 1;
400  pos = 0;
401  OK = 0;
402  while (more) {
403  unsigned int count, addr;
404 
405  more = more && read_idm_word(data, size, &pos, &count);
406  if (more && count == 0xffff) {
407  OK = 1;
408  break;
409  }
410  more = more && read_idm_word(data, size, &pos, &addr);
411  while (more && count > 0) {
412  unsigned int dummy;
413  more = more && read_idm_word(data, size, &pos, &dummy);
414  count--;
415  }
416  }
417 
418  if (!OK) {
419  result = -ENODATA;
420  } else {
421  int i;
422  struct jr3_pci_dev_private *p = dev->private;
423 
424  for (i = 0; i < p->n_channels; i++) {
425  struct jr3_pci_subdev_private *sp;
426 
427  sp = dev->subdevices[i].private;
428  more = 1;
429  pos = 0;
430  while (more) {
431  unsigned int count, addr;
432  more = more
433  && read_idm_word(data, size, &pos, &count);
434  if (more && count == 0xffff)
435  break;
436  more = more
437  && read_idm_word(data, size, &pos, &addr);
438  dev_dbg(dev->class_dev,
439  "Loading#%d %4.4x bytes at %4.4x\n",
440  i, count, addr);
441  while (more && count > 0) {
442  if (addr & 0x4000) {
443  /* 16 bit data, never seen in real life!! */
444  unsigned int data1;
445 
446  more = more
447  && read_idm_word(data,
448  size, &pos,
449  &data1);
450  count--;
451  /* printk("jr3_data, not tested\n"); */
452  /* jr3[addr + 0x20000 * pnum] = data1; */
453  } else {
454  /* Download 24 bit program */
455  unsigned int data1, data2;
456 
457  more = more
458  && read_idm_word(data,
459  size, &pos,
460  &data1);
461  more = more
462  && read_idm_word(data, size,
463  &pos,
464  &data2);
465  count -= 2;
466  if (more) {
467  set_u16(&p->
468  iobase->channel
469  [i].program_low
470  [addr], data1);
471  udelay(1);
472  set_u16(&p->
473  iobase->channel
474  [i].program_high
475  [addr], data2);
476  udelay(1);
477 
478  }
479  }
480  addr++;
481  }
482  }
483  }
484  }
485  return result;
486 }
487 
488 static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice *s)
489 {
490  struct poll_delay_t result = poll_delay_min_max(1000, 2000);
491  struct jr3_pci_subdev_private *p = s->private;
492  int i;
493 
494  if (p) {
495  volatile struct jr3_channel *channel = p->channel;
496  int errors = get_u16(&channel->errors);
497 
498  if (errors != p->errors) {
499  printk("Errors: %x -> %x\n", p->errors, errors);
500  p->errors = errors;
501  }
502  if (errors & (watch_dog | watch_dog2 | sensor_change)) {
503  /* Sensor communication lost, force poll mode */
504  p->state = state_jr3_poll;
505 
506  }
507  switch (p->state) {
508  case state_jr3_poll:{
509  u16 model_no = get_u16(&channel->model_no);
510  u16 serial_no = get_u16(&channel->serial_no);
511  if ((errors & (watch_dog | watch_dog2)) ||
512  model_no == 0 || serial_no == 0) {
513 /*
514  * Still no sensor, keep on polling. Since it takes up to 10 seconds
515  * for offsets to stabilize, polling each second should suffice.
516  */
517  result = poll_delay_min_max(1000, 2000);
518  } else {
519  p->retries = 0;
520  p->state =
521  state_jr3_init_wait_for_offset;
522  result = poll_delay_min_max(1000, 2000);
523  }
524  }
525  break;
526  case state_jr3_init_wait_for_offset:{
527  p->retries++;
528  if (p->retries < 10) {
529  /* Wait for offeset to stabilize (< 10 s according to manual) */
530  result = poll_delay_min_max(1000, 2000);
531  } else {
532  struct transform_t transf;
533 
534  p->model_no =
535  get_u16(&channel->model_no);
536  p->serial_no =
537  get_u16(&channel->serial_no);
538 
539  printk
540  ("Setting transform for channel %d\n",
541  p->channel_no);
542  printk("Sensor Model = %i\n",
543  p->model_no);
544  printk("Sensor Serial = %i\n",
545  p->serial_no);
546 
547  /* Transformation all zeros */
548  for (i = 0; i < ARRAY_SIZE(transf.link); i++) {
549  transf.link[i].link_type =
550  (enum link_types)0;
551  transf.link[i].link_amount = 0;
552  }
553 
554  set_transforms(channel, transf, 0);
555  use_transform(channel, 0);
556  p->state =
557  state_jr3_init_transform_complete;
558  result = poll_delay_min_max(20, 100); /* Allow 20 ms for completion */
559  }
560  } break;
561  case state_jr3_init_transform_complete:{
562  if (!is_complete(channel)) {
563  printk
564  ("state_jr3_init_transform_complete complete = %d\n",
565  is_complete(channel));
566  result = poll_delay_min_max(20, 100);
567  } else {
568  /* Set full scale */
569  struct six_axis_t min_full_scale;
570  struct six_axis_t max_full_scale;
571 
572  min_full_scale =
573  get_min_full_scales(channel);
574  printk("Obtained Min. Full Scales:\n");
575  printk(KERN_DEBUG "%i ", (min_full_scale).fx);
576  printk(KERN_CONT "%i ", (min_full_scale).fy);
577  printk(KERN_CONT "%i ", (min_full_scale).fz);
578  printk(KERN_CONT "%i ", (min_full_scale).mx);
579  printk(KERN_CONT "%i ", (min_full_scale).my);
580  printk(KERN_CONT "%i ", (min_full_scale).mz);
581  printk(KERN_CONT "\n");
582 
583  max_full_scale =
584  get_max_full_scales(channel);
585  printk("Obtained Max. Full Scales:\n");
586  printk(KERN_DEBUG "%i ", (max_full_scale).fx);
587  printk(KERN_CONT "%i ", (max_full_scale).fy);
588  printk(KERN_CONT "%i ", (max_full_scale).fz);
589  printk(KERN_CONT "%i ", (max_full_scale).mx);
590  printk(KERN_CONT "%i ", (max_full_scale).my);
591  printk(KERN_CONT "%i ", (max_full_scale).mz);
592  printk(KERN_CONT "\n");
593 
594  set_full_scales(channel,
595  max_full_scale);
596 
597  p->state =
598  state_jr3_init_set_full_scale_complete;
599  result = poll_delay_min_max(20, 100); /* Allow 20 ms for completion */
600  }
601  }
602  break;
603  case state_jr3_init_set_full_scale_complete:{
604  if (!is_complete(channel)) {
605  printk
606  ("state_jr3_init_set_full_scale_complete complete = %d\n",
607  is_complete(channel));
608  result = poll_delay_min_max(20, 100);
609  } else {
610  volatile struct force_array *full_scale;
611 
612  /* Use ranges in kN or we will overflow arount 2000N! */
613  full_scale = &channel->full_scale;
614  p->range[0].range.min =
615  -get_s16(&full_scale->fx) * 1000;
616  p->range[0].range.max =
617  get_s16(&full_scale->fx) * 1000;
618  p->range[1].range.min =
619  -get_s16(&full_scale->fy) * 1000;
620  p->range[1].range.max =
621  get_s16(&full_scale->fy) * 1000;
622  p->range[2].range.min =
623  -get_s16(&full_scale->fz) * 1000;
624  p->range[2].range.max =
625  get_s16(&full_scale->fz) * 1000;
626  p->range[3].range.min =
627  -get_s16(&full_scale->mx) * 100;
628  p->range[3].range.max =
629  get_s16(&full_scale->mx) * 100;
630  p->range[4].range.min =
631  -get_s16(&full_scale->my) * 100;
632  p->range[4].range.max =
633  get_s16(&full_scale->my) * 100;
634  p->range[5].range.min =
635  -get_s16(&full_scale->mz) * 100;
636  p->range[5].range.max =
637  get_s16(&full_scale->mz) * 100;
638  p->range[6].range.min = -get_s16(&full_scale->v1) * 100; /* ?? */
639  p->range[6].range.max = get_s16(&full_scale->v1) * 100; /* ?? */
640  p->range[7].range.min = -get_s16(&full_scale->v2) * 100; /* ?? */
641  p->range[7].range.max = get_s16(&full_scale->v2) * 100; /* ?? */
642  p->range[8].range.min = 0;
643  p->range[8].range.max = 65535;
644 
645  {
646  int i;
647  for (i = 0; i < 9; i++) {
648  printk("%d %d - %d\n",
649  i,
650  p->
651  range[i].range.
652  min,
653  p->
654  range[i].range.
655  max);
656  }
657  }
658 
659  use_offset(channel, 0);
660  p->state =
661  state_jr3_init_use_offset_complete;
662  result = poll_delay_min_max(40, 100); /* Allow 40 ms for completion */
663  }
664  }
665  break;
666  case state_jr3_init_use_offset_complete:{
667  if (!is_complete(channel)) {
668  printk
669  ("state_jr3_init_use_offset_complete complete = %d\n",
670  is_complete(channel));
671  result = poll_delay_min_max(20, 100);
672  } else {
673  printk
674  ("Default offsets %d %d %d %d %d %d\n",
675  get_s16(&channel->offsets.fx),
676  get_s16(&channel->offsets.fy),
677  get_s16(&channel->offsets.fz),
678  get_s16(&channel->offsets.mx),
679  get_s16(&channel->offsets.my),
680  get_s16(&channel->offsets.mz));
681 
682  set_s16(&channel->offsets.fx, 0);
683  set_s16(&channel->offsets.fy, 0);
684  set_s16(&channel->offsets.fz, 0);
685  set_s16(&channel->offsets.mx, 0);
686  set_s16(&channel->offsets.my, 0);
687  set_s16(&channel->offsets.mz, 0);
688 
689  set_offset(channel);
690 
691  p->state = state_jr3_done;
692  }
693  }
694  break;
695  case state_jr3_done:{
696  poll_delay_min_max(10000, 20000);
697  }
698  break;
699  default:{
700  poll_delay_min_max(1000, 2000);
701  }
702  break;
703  }
704  }
705  return result;
706 }
707 
708 static void jr3_pci_poll_dev(unsigned long data)
709 {
710  unsigned long flags;
711  struct comedi_device *dev = (struct comedi_device *)data;
712  struct jr3_pci_dev_private *devpriv = dev->private;
713  unsigned long now;
714  int delay;
715  int i;
716 
717  spin_lock_irqsave(&dev->spinlock, flags);
718  delay = 1000;
719  now = jiffies;
720  /* Poll all channels that are ready to be polled */
721  for (i = 0; i < devpriv->n_channels; i++) {
722  struct jr3_pci_subdev_private *subdevpriv =
723  dev->subdevices[i].private;
724  if (now > subdevpriv->next_time_min) {
725  struct poll_delay_t sub_delay;
726 
727  sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]);
728  subdevpriv->next_time_min =
729  jiffies + msecs_to_jiffies(sub_delay.min);
730  subdevpriv->next_time_max =
731  jiffies + msecs_to_jiffies(sub_delay.max);
732  if (sub_delay.max && sub_delay.max < delay) {
733 /*
734 * Wake up as late as possible -> poll as many channels as possible
735 * at once
736 */
737  delay = sub_delay.max;
738  }
739  }
740  }
741  spin_unlock_irqrestore(&dev->spinlock, flags);
742 
743  devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
744  add_timer(&devpriv->timer);
745 }
746 
747 static int jr3_pci_attach(struct comedi_device *dev,
748  struct comedi_devconfig *it)
749 {
750  int result = 0;
751  struct pci_dev *card = NULL;
752  int opt_bus, opt_slot, i;
754 
755  opt_bus = it->options[0];
756  opt_slot = it->options[1];
757 
758  if (sizeof(struct jr3_channel) != 0xc00) {
759  dev_err(dev->class_dev,
760  "sizeof(struct jr3_channel) = %x [expected %x]\n",
761  (unsigned)sizeof(struct jr3_channel), 0xc00);
762  return -EINVAL;
763  }
764 
765  result = alloc_private(dev, sizeof(struct jr3_pci_dev_private));
766  if (result < 0)
767  return -ENOMEM;
768  card = NULL;
769  devpriv = dev->private;
770  init_timer(&devpriv->timer);
771  while (1) {
773  if (card == NULL) {
774  /* No card found */
775  break;
776  } else {
777  switch (card->device) {
779  devpriv->n_channels = 1;
780  }
781  break;
783  devpriv->n_channels = 1;
784  }
785  break;
787  devpriv->n_channels = 2;
788  }
789  break;
791  devpriv->n_channels = 3;
792  }
793  break;
795  devpriv->n_channels = 4;
796  }
797  break;
798  default:{
799  devpriv->n_channels = 0;
800  }
801  }
802  if (devpriv->n_channels >= 1) {
803  if (opt_bus == 0 && opt_slot == 0) {
804  /* Take first available card */
805  break;
806  } else if (opt_bus == card->bus->number &&
807  opt_slot == PCI_SLOT(card->devfn)) {
808  /* Take requested card */
809  break;
810  }
811  }
812  }
813  }
814  if (!card) {
815  dev_err(dev->class_dev, "no jr3_pci found\n");
816  return -EIO;
817  } else {
818  devpriv->pci_dev = card;
819  dev->board_name = "jr3_pci";
820  }
821 
822  result = comedi_pci_enable(card, "jr3_pci");
823  if (result < 0)
824  return -EIO;
825 
826  devpriv->pci_enabled = 1;
827  devpriv->iobase = ioremap(pci_resource_start(card, 0),
828  offsetof(struct jr3_t, channel[devpriv->n_channels]));
829  if (!devpriv->iobase)
830  return -ENOMEM;
831 
832  result = comedi_alloc_subdevices(dev, devpriv->n_channels);
833  if (result)
834  return result;
835 
836  dev->open = jr3_pci_open;
837  for (i = 0; i < devpriv->n_channels; i++) {
838  dev->subdevices[i].type = COMEDI_SUBD_AI;
839  dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND;
840  dev->subdevices[i].n_chan = 8 * 7 + 2;
841  dev->subdevices[i].insn_read = jr3_pci_ai_insn_read;
842  dev->subdevices[i].private =
843  kzalloc(sizeof(struct jr3_pci_subdev_private), GFP_KERNEL);
844  if (dev->subdevices[i].private) {
845  struct jr3_pci_subdev_private *p;
846  int j;
847 
848  p = dev->subdevices[i].private;
849  p->channel = &devpriv->iobase->channel[i].data;
850  dev_dbg(dev->class_dev, "p->channel %p %p (%tx)\n",
851  p->channel, devpriv->iobase,
852  ((char *)(p->channel) -
853  (char *)(devpriv->iobase)));
854  p->channel_no = i;
855  for (j = 0; j < 8; j++) {
856  int k;
857 
858  p->range[j].length = 1;
859  p->range[j].range.min = -1000000;
860  p->range[j].range.max = 1000000;
861  for (k = 0; k < 7; k++) {
862  p->range_table_list[j + k * 8] =
863  (struct comedi_lrange *)&p->
864  range[j];
865  p->maxdata_list[j + k * 8] = 0x7fff;
866  }
867  }
868  p->range[8].length = 1;
869  p->range[8].range.min = 0;
870  p->range[8].range.max = 65536;
871 
872  p->range_table_list[56] =
873  (struct comedi_lrange *)&p->range[8];
874  p->range_table_list[57] =
875  (struct comedi_lrange *)&p->range[8];
876  p->maxdata_list[56] = 0xffff;
877  p->maxdata_list[57] = 0xffff;
878  /* Channel specific range and maxdata */
879  dev->subdevices[i].range_table = NULL;
880  dev->subdevices[i].range_table_list =
881  p->range_table_list;
882  dev->subdevices[i].maxdata = 0;
883  dev->subdevices[i].maxdata_list = p->maxdata_list;
884  }
885  }
886 
887  /* Reset DSP card */
888  writel(0, &devpriv->iobase->channel[0].reset);
889 
890  result = comedi_load_firmware(dev, "jr3pci.idm", jr3_download_firmware);
891  dev_dbg(dev->class_dev, "Firmare load %d\n", result);
892 
893  if (result < 0)
894  goto out;
895 /*
896  * TODO: use firmware to load preferred offset tables. Suggested
897  * format:
898  * model serial Fx Fy Fz Mx My Mz\n
899  *
900  * comedi_load_firmware(dev, "jr3_offsets_table", jr3_download_firmware);
901  */
902 
903 /*
904  * It takes a few milliseconds for software to settle as much as we
905  * can read firmware version
906  */
908  for (i = 0; i < 0x18; i++) {
909  dev_dbg(dev->class_dev, "%c\n",
910  get_u16(&devpriv->iobase->channel[0].
911  data.copyright[i]) >> 8);
912  }
913 
914  /* Start card timer */
915  for (i = 0; i < devpriv->n_channels; i++) {
916  struct jr3_pci_subdev_private *p = dev->subdevices[i].private;
917 
920  }
921 
922  devpriv->timer.data = (unsigned long)dev;
923  devpriv->timer.function = jr3_pci_poll_dev;
924  devpriv->timer.expires = jiffies + msecs_to_jiffies(1000);
925  add_timer(&devpriv->timer);
926 
927 out:
928  return result;
929 }
930 
931 static void jr3_pci_detach(struct comedi_device *dev)
932 {
933  int i;
934  struct jr3_pci_dev_private *devpriv = dev->private;
935 
936  if (devpriv) {
937  del_timer_sync(&devpriv->timer);
938 
939  if (dev->subdevices) {
940  for (i = 0; i < devpriv->n_channels; i++)
941  kfree(dev->subdevices[i].private);
942  }
943  if (devpriv->iobase)
944  iounmap((void *)devpriv->iobase);
945  if (devpriv->pci_enabled)
946  comedi_pci_disable(devpriv->pci_dev);
947  if (devpriv->pci_dev)
948  pci_dev_put(devpriv->pci_dev);
949  }
950 }
951 
952 static struct comedi_driver jr3_pci_driver = {
953  .driver_name = "jr3_pci",
954  .module = THIS_MODULE,
955  .attach = jr3_pci_attach,
956  .detach = jr3_pci_detach,
957 };
958 
959 static int __devinit jr3_pci_pci_probe(struct pci_dev *dev,
960  const struct pci_device_id *ent)
961 {
962  return comedi_pci_auto_config(dev, &jr3_pci_driver);
963 }
964 
965 static void __devexit jr3_pci_pci_remove(struct pci_dev *dev)
966 {
968 }
969 
970 static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_table) = {
976  { 0 }
977 };
978 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
979 
980 static struct pci_driver jr3_pci_pci_driver = {
981  .name = "jr3_pci",
982  .id_table = jr3_pci_pci_table,
983  .probe = jr3_pci_pci_probe,
984  .remove = __devexit_p(jr3_pci_pci_remove),
985 };
986 module_comedi_pci_driver(jr3_pci_driver, jr3_pci_pci_driver);
987 
988 MODULE_AUTHOR("Comedi http://www.comedi.org");
989 MODULE_DESCRIPTION("Comedi low-level driver");
990 MODULE_LICENSE("GPL");
991 MODULE_FIRMWARE("comedi/jr3pci.idm");