]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/jr3_pci.c
b52d58e5de276b204978d5ea4d12e9c030d8cd97
[karo-tx-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 /*
19  * Driver: jr3_pci
20  * Description: JR3/PCI force sensor board
21  * Author: Anders Blomdell <anders.blomdell@control.lth.se>
22  * Updated: Thu, 01 Nov 2012 17:34:55 +0000
23  * Status: works
24  * Devices: [JR3] PCI force sensor board (jr3_pci)
25  *
26  * Configuration options:
27  *   None
28  *
29  * Manual configuration of comedi devices is not supported by this
30  * driver; supported PCI devices are configured as comedi devices
31  * automatically.
32  *
33  * The DSP on the board requires initialization code, which can be
34  * loaded by placing it in /lib/firmware/comedi.  The initialization
35  * code should be somewhere on the media you got with your card.  One
36  * version is available from http://www.comedi.org in the
37  * comedi_nonfree_firmware tarball.  The file is called "jr3pci.idm".
38  */
39
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/delay.h>
44 #include <linux/ctype.h>
45 #include <linux/jiffies.h>
46 #include <linux/slab.h>
47 #include <linux/timer.h>
48
49 #include "../comedidev.h"
50
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_1_CHANNEL_NEW 0x1111
56 #define PCI_DEVICE_ID_JR3_2_CHANNEL 0x3112
57 #define PCI_DEVICE_ID_JR3_3_CHANNEL 0x3113
58 #define PCI_DEVICE_ID_JR3_4_CHANNEL 0x3114
59
60 struct jr3_pci_dev_private {
61         struct jr3_t __iomem *iobase;
62         int n_channels;
63         struct timer_list timer;
64 };
65
66 struct poll_delay_t {
67         int min;
68         int max;
69 };
70
71 struct jr3_pci_subdev_private {
72         struct jr3_channel __iomem *channel;
73         unsigned long next_time_min;
74         unsigned long next_time_max;
75         enum { state_jr3_poll,
76                 state_jr3_init_wait_for_offset,
77                 state_jr3_init_transform_complete,
78                 state_jr3_init_set_full_scale_complete,
79                 state_jr3_init_use_offset_complete,
80                 state_jr3_done
81         } state;
82         int channel_no;
83         int serial_no;
84         int model_no;
85         struct {
86                 int length;
87                 struct comedi_krange range;
88         } range[9];
89         const struct comedi_lrange *range_table_list[8 * 7 + 2];
90         unsigned int maxdata_list[8 * 7 + 2];
91         u16 errors;
92         int retries;
93 };
94
95 static struct poll_delay_t poll_delay_min_max(int min, int max)
96 {
97         struct poll_delay_t result;
98
99         result.min = min;
100         result.max = max;
101         return result;
102 }
103
104 static int is_complete(struct jr3_channel __iomem *channel)
105 {
106         return get_s16(&channel->command_word0) == 0;
107 }
108
109 struct transform_t {
110         struct {
111                 u16 link_type;
112                 s16 link_amount;
113         } link[8];
114 };
115
116 static void set_transforms(struct jr3_channel __iomem *channel,
117                            struct transform_t transf, short num)
118 {
119         int i;
120
121         num &= 0x000f;          /*  Make sure that 0 <= num <= 15 */
122         for (i = 0; i < 8; i++) {
123                 set_u16(&channel->transforms[num].link[i].link_type,
124                         transf.link[i].link_type);
125                 udelay(1);
126                 set_s16(&channel->transforms[num].link[i].link_amount,
127                         transf.link[i].link_amount);
128                 udelay(1);
129                 if (transf.link[i].link_type == end_x_form)
130                         break;
131         }
132 }
133
134 static void use_transform(struct jr3_channel __iomem *channel,
135                           short transf_num)
136 {
137         set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f));
138 }
139
140 static void use_offset(struct jr3_channel __iomem *channel, short offset_num)
141 {
142         set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f));
143 }
144
145 static void set_offset(struct jr3_channel __iomem *channel)
146 {
147         set_s16(&channel->command_word0, 0x0700);
148 }
149
150 struct six_axis_t {
151         s16 fx;
152         s16 fy;
153         s16 fz;
154         s16 mx;
155         s16 my;
156         s16 mz;
157 };
158
159 static void set_full_scales(struct jr3_channel __iomem *channel,
160                             struct six_axis_t full_scale)
161 {
162         set_s16(&channel->full_scale.fx, full_scale.fx);
163         set_s16(&channel->full_scale.fy, full_scale.fy);
164         set_s16(&channel->full_scale.fz, full_scale.fz);
165         set_s16(&channel->full_scale.mx, full_scale.mx);
166         set_s16(&channel->full_scale.my, full_scale.my);
167         set_s16(&channel->full_scale.mz, full_scale.mz);
168         set_s16(&channel->command_word0, 0x0a00);
169 }
170
171 static struct six_axis_t get_min_full_scales(struct jr3_channel __iomem
172                                              *channel)
173 {
174         struct six_axis_t result;
175         result.fx = get_s16(&channel->min_full_scale.fx);
176         result.fy = get_s16(&channel->min_full_scale.fy);
177         result.fz = get_s16(&channel->min_full_scale.fz);
178         result.mx = get_s16(&channel->min_full_scale.mx);
179         result.my = get_s16(&channel->min_full_scale.my);
180         result.mz = get_s16(&channel->min_full_scale.mz);
181         return result;
182 }
183
184 static struct six_axis_t get_max_full_scales(struct jr3_channel __iomem
185                                              *channel)
186 {
187         struct six_axis_t result;
188         result.fx = get_s16(&channel->max_full_scale.fx);
189         result.fy = get_s16(&channel->max_full_scale.fy);
190         result.fz = get_s16(&channel->max_full_scale.fz);
191         result.mx = get_s16(&channel->max_full_scale.mx);
192         result.my = get_s16(&channel->max_full_scale.my);
193         result.mz = get_s16(&channel->max_full_scale.mz);
194         return result;
195 }
196
197 static int jr3_pci_ai_insn_read(struct comedi_device *dev,
198                                 struct comedi_subdevice *s,
199                                 struct comedi_insn *insn, unsigned int *data)
200 {
201         int result;
202         struct jr3_pci_subdev_private *p;
203         int channel;
204
205         p = s->private;
206         channel = CR_CHAN(insn->chanspec);
207         if (p == NULL || channel > 57) {
208                 result = -EINVAL;
209         } else {
210                 int i;
211
212                 result = insn->n;
213                 if (p->state != state_jr3_done ||
214                     (get_u16(&p->channel->errors) & (watch_dog | watch_dog2 |
215                                                      sensor_change))) {
216                         /* No sensor or sensor changed */
217                         if (p->state == state_jr3_done) {
218                                 /* Restart polling */
219                                 p->state = state_jr3_poll;
220                         }
221                         result = -EAGAIN;
222                 }
223                 for (i = 0; i < insn->n; i++) {
224                         if (channel < 56) {
225                                 int axis, filter;
226
227                                 axis = channel % 8;
228                                 filter = channel / 8;
229                                 if (p->state != state_jr3_done) {
230                                         data[i] = 0;
231                                 } else {
232                                         int F = 0;
233                                         switch (axis) {
234                                         case 0:
235                                                 F = get_s16(&p->channel->
236                                                             filter[filter].fx);
237                                                 break;
238                                         case 1:
239                                                 F = get_s16(&p->channel->
240                                                             filter[filter].fy);
241                                                 break;
242                                         case 2:
243                                                 F = get_s16(&p->channel->
244                                                             filter[filter].fz);
245                                                 break;
246                                         case 3:
247                                                 F = get_s16(&p->channel->
248                                                             filter[filter].mx);
249                                                 break;
250                                         case 4:
251                                                 F = get_s16(&p->channel->
252                                                             filter[filter].my);
253                                                 break;
254                                         case 5:
255                                                 F = get_s16(&p->channel->
256                                                             filter[filter].mz);
257                                                 break;
258                                         case 6:
259                                                 F = get_s16(&p->channel->
260                                                             filter[filter].v1);
261                                                 break;
262                                         case 7:
263                                                 F = get_s16(&p->channel->
264                                                             filter[filter].v2);
265                                                 break;
266                                         }
267                                         data[i] = F + 0x4000;
268                                 }
269                         } else if (channel == 56) {
270                                 if (p->state != state_jr3_done)
271                                         data[i] = 0;
272                                 else
273                                         data[i] =
274                                         get_u16(&p->channel->model_no);
275                         } else if (channel == 57) {
276                                 if (p->state != state_jr3_done)
277                                         data[i] = 0;
278                                 else
279                                         data[i] =
280                                         get_u16(&p->channel->serial_no);
281                         }
282                 }
283         }
284         return result;
285 }
286
287 static int jr3_pci_open(struct comedi_device *dev)
288 {
289         int i;
290         struct jr3_pci_dev_private *devpriv = dev->private;
291
292         dev_dbg(dev->class_dev, "jr3_pci_open\n");
293         for (i = 0; i < devpriv->n_channels; i++) {
294                 struct jr3_pci_subdev_private *p;
295
296                 p = dev->subdevices[i].private;
297                 if (p) {
298                         dev_dbg(dev->class_dev, "serial: %p %d (%d)\n", p,
299                                 p->serial_no, p->channel_no);
300                 }
301         }
302         return 0;
303 }
304
305 static int read_idm_word(const u8 *data, size_t size, int *pos,
306                          unsigned int *val)
307 {
308         int result = 0;
309         if (pos && val) {
310                 /*  Skip over non hex */
311                 for (; *pos < size && !isxdigit(data[*pos]); (*pos)++)
312                         ;
313                 /*  Collect value */
314                 *val = 0;
315                 for (; *pos < size; (*pos)++) {
316                         int value;
317                         value = hex_to_bin(data[*pos]);
318                         if (value >= 0) {
319                                 result = 1;
320                                 *val = (*val << 4) + value;
321                         } else {
322                                 break;
323                         }
324                 }
325         }
326         return result;
327 }
328
329 static int jr3_download_firmware(struct comedi_device *dev,
330                                  const u8 *data, size_t size,
331                                  unsigned long context)
332 {
333         /*
334          * IDM file format is:
335          *   { count, address, data <count> } *
336          *   ffff
337          */
338         int result, more, pos, OK;
339
340         result = 0;
341         more = 1;
342         pos = 0;
343         OK = 0;
344         while (more) {
345                 unsigned int count, addr;
346
347                 more = more && read_idm_word(data, size, &pos, &count);
348                 if (more && count == 0xffff) {
349                         OK = 1;
350                         break;
351                 }
352                 more = more && read_idm_word(data, size, &pos, &addr);
353                 while (more && count > 0) {
354                         unsigned int dummy;
355                         more = more && read_idm_word(data, size, &pos, &dummy);
356                         count--;
357                 }
358         }
359
360         if (!OK) {
361                 result = -ENODATA;
362         } else {
363                 int i;
364                 struct jr3_pci_dev_private *p = dev->private;
365
366                 for (i = 0; i < p->n_channels; i++) {
367                         struct jr3_pci_subdev_private *sp;
368
369                         sp = dev->subdevices[i].private;
370                         more = 1;
371                         pos = 0;
372                         while (more) {
373                                 unsigned int count, addr;
374                                 more = more &&
375                                        read_idm_word(data, size, &pos, &count);
376                                 if (more && count == 0xffff)
377                                         break;
378                                 more = more &&
379                                        read_idm_word(data, size, &pos, &addr);
380                                 dev_dbg(dev->class_dev,
381                                         "Loading#%d %4.4x bytes at %4.4x\n",
382                                         i, count, addr);
383                                 while (more && count > 0) {
384                                         if (addr & 0x4000) {
385                                                 /*  16 bit data, never seen
386                                                  *  in real life!! */
387                                                 unsigned int data1;
388
389                                                 more = more &&
390                                                        read_idm_word(data,
391                                                                      size, &pos,
392                                                                      &data1);
393                                                 count--;
394                                                 /* jr3[addr + 0x20000 * pnum] =
395                                                    data1; */
396                                         } else {
397                                                 /*   Download 24 bit program */
398                                                 unsigned int data1, data2;
399
400                                                 more = more &&
401                                                        read_idm_word(data,
402                                                                      size, &pos,
403                                                                      &data1);
404                                                 more = more &&
405                                                        read_idm_word(data, size,
406                                                                      &pos,
407                                                                      &data2);
408                                                 count -= 2;
409                                                 if (more) {
410                                                         set_u16(&p->
411                                                                 iobase->channel
412                                                                 [i].program_low
413                                                                 [addr], data1);
414                                                         udelay(1);
415                                                         set_u16(&p->
416                                                                 iobase->channel
417                                                                 [i].program_high
418                                                                 [addr], data2);
419                                                         udelay(1);
420                                                 }
421                                         }
422                                         addr++;
423                                 }
424                         }
425                 }
426         }
427         return result;
428 }
429
430 static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice *s)
431 {
432         struct poll_delay_t result = poll_delay_min_max(1000, 2000);
433         struct jr3_pci_subdev_private *p = s->private;
434         int i;
435
436         if (p) {
437                 struct jr3_channel __iomem *channel = p->channel;
438                 int errors = get_u16(&channel->errors);
439
440                 if (errors != p->errors)
441                         p->errors = errors;
442
443                 if (errors & (watch_dog | watch_dog2 | sensor_change))
444                         /*  Sensor communication lost, force poll mode */
445                         p->state = state_jr3_poll;
446
447                 switch (p->state) {
448                 case state_jr3_poll: {
449                                 u16 model_no = get_u16(&channel->model_no);
450                                 u16 serial_no = get_u16(&channel->serial_no);
451                                 if ((errors & (watch_dog | watch_dog2)) ||
452                                     model_no == 0 || serial_no == 0) {
453                                         /*
454                                          * Still no sensor, keep on polling.
455                                          * Since it takes up to 10 seconds
456                                          * for offsets to stabilize, polling
457                                          * each second should suffice.
458                                          */
459                                         result = poll_delay_min_max(1000, 2000);
460                                 } else {
461                                         p->retries = 0;
462                                         p->state =
463                                                 state_jr3_init_wait_for_offset;
464                                         result = poll_delay_min_max(1000, 2000);
465                                 }
466                         }
467                         break;
468                 case state_jr3_init_wait_for_offset:
469                         p->retries++;
470                         if (p->retries < 10) {
471                                 /*  Wait for offeset to stabilize
472                                  *  (< 10 s according to manual) */
473                                 result = poll_delay_min_max(1000, 2000);
474                         } else {
475                                 struct transform_t transf;
476
477                                 p->model_no = get_u16(&channel->model_no);
478                                 p->serial_no = get_u16(&channel->serial_no);
479
480                                 /*  Transformation all zeros */
481                                 for (i = 0; i < ARRAY_SIZE(transf.link); i++) {
482                                         transf.link[i].link_type =
483                                                 (enum link_types)0;
484                                         transf.link[i].link_amount = 0;
485                                 }
486
487                                 set_transforms(channel, transf, 0);
488                                 use_transform(channel, 0);
489                                 p->state = state_jr3_init_transform_complete;
490                                 /*  Allow 20 ms for completion */
491                                 result = poll_delay_min_max(20, 100);
492                         }
493                         break;
494                 case state_jr3_init_transform_complete:
495                         if (!is_complete(channel)) {
496                                 result = poll_delay_min_max(20, 100);
497                         } else {
498                                 /*  Set full scale */
499                                 struct six_axis_t min_full_scale;
500                                 struct six_axis_t max_full_scale;
501
502                                 min_full_scale = get_min_full_scales(channel);
503                                 max_full_scale = get_max_full_scales(channel);
504                                 set_full_scales(channel, max_full_scale);
505
506                                 p->state =
507                                         state_jr3_init_set_full_scale_complete;
508                                 /*  Allow 20 ms for completion */
509                                 result = poll_delay_min_max(20, 100);
510                         }
511                         break;
512                 case state_jr3_init_set_full_scale_complete:
513                         if (!is_complete(channel)) {
514                                 result = poll_delay_min_max(20, 100);
515                         } else {
516                                 struct force_array __iomem *full_scale;
517
518                                 /*  Use ranges in kN or we will
519                                  *  overflow around 2000N! */
520                                 full_scale = &channel->full_scale;
521                                 p->range[0].range.min =
522                                         -get_s16(&full_scale->fx) * 1000;
523                                 p->range[0].range.max =
524                                         get_s16(&full_scale->fx) * 1000;
525                                 p->range[1].range.min =
526                                         -get_s16(&full_scale->fy) * 1000;
527                                 p->range[1].range.max =
528                                         get_s16(&full_scale->fy) * 1000;
529                                 p->range[2].range.min =
530                                         -get_s16(&full_scale->fz) * 1000;
531                                 p->range[2].range.max =
532                                         get_s16(&full_scale->fz) * 1000;
533                                 p->range[3].range.min =
534                                         -get_s16(&full_scale->mx) * 100;
535                                 p->range[3].range.max =
536                                         get_s16(&full_scale->mx) * 100;
537                                 p->range[4].range.min =
538                                         -get_s16(&full_scale->my) * 100;
539                                 p->range[4].range.max =
540                                         get_s16(&full_scale->my) * 100;
541                                 p->range[5].range.min =
542                                         -get_s16(&full_scale->mz) * 100;
543                                 p->range[5].range.max =
544                                         get_s16(&full_scale->mz) * 100; /* ?? */
545                                 p->range[6].range.min =
546                                         -get_s16(&full_scale->v1) * 100;/* ?? */
547                                 p->range[6].range.max =
548                                         get_s16(&full_scale->v1) * 100; /* ?? */
549                                 p->range[7].range.min =
550                                         -get_s16(&full_scale->v2) * 100;/* ?? */
551                                 p->range[7].range.max =
552                                         get_s16(&full_scale->v2) * 100; /* ?? */
553                                 p->range[8].range.min = 0;
554                                 p->range[8].range.max = 65535;
555
556                                 use_offset(channel, 0);
557                                 p->state = state_jr3_init_use_offset_complete;
558                                 /*  Allow 40 ms for completion */
559                                 result = poll_delay_min_max(40, 100);
560                         }
561                         break;
562                 case state_jr3_init_use_offset_complete:
563                         if (!is_complete(channel)) {
564                                 result = poll_delay_min_max(20, 100);
565                         } else {
566                                 set_s16(&channel->offsets.fx, 0);
567                                 set_s16(&channel->offsets.fy, 0);
568                                 set_s16(&channel->offsets.fz, 0);
569                                 set_s16(&channel->offsets.mx, 0);
570                                 set_s16(&channel->offsets.my, 0);
571                                 set_s16(&channel->offsets.mz, 0);
572
573                                 set_offset(channel);
574
575                                 p->state = state_jr3_done;
576                         }
577                         break;
578                 case state_jr3_done:
579                         poll_delay_min_max(10000, 20000);
580                         break;
581                 default:
582                         poll_delay_min_max(1000, 2000);
583                         break;
584                 }
585         }
586         return result;
587 }
588
589 static void jr3_pci_poll_dev(unsigned long data)
590 {
591         unsigned long flags;
592         struct comedi_device *dev = (struct comedi_device *)data;
593         struct jr3_pci_dev_private *devpriv = dev->private;
594         unsigned long now;
595         int delay;
596         int i;
597
598         spin_lock_irqsave(&dev->spinlock, flags);
599         delay = 1000;
600         now = jiffies;
601         /*  Poll all channels that are ready to be polled */
602         for (i = 0; i < devpriv->n_channels; i++) {
603                 struct jr3_pci_subdev_private *subdevpriv =
604                         dev->subdevices[i].private;
605                 if (now > subdevpriv->next_time_min) {
606                         struct poll_delay_t sub_delay;
607
608                         sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]);
609                         subdevpriv->next_time_min =
610                                 jiffies + msecs_to_jiffies(sub_delay.min);
611                         subdevpriv->next_time_max =
612                                 jiffies + msecs_to_jiffies(sub_delay.max);
613                         if (sub_delay.max && sub_delay.max < delay)
614                                 /*
615                                  * Wake up as late as possible ->
616                                  * poll as many channels as possible at once.
617                                  */
618                                 delay = sub_delay.max;
619                 }
620         }
621         spin_unlock_irqrestore(&dev->spinlock, flags);
622
623         devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
624         add_timer(&devpriv->timer);
625 }
626
627 static int jr3_pci_auto_attach(struct comedi_device *dev,
628                                          unsigned long context_unused)
629 {
630         int result;
631         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
632         int i;
633         struct jr3_pci_dev_private *devpriv;
634
635         if (sizeof(struct jr3_channel) != 0xc00) {
636                 dev_err(dev->class_dev,
637                         "sizeof(struct jr3_channel) = %x [expected %x]\n",
638                         (unsigned)sizeof(struct jr3_channel), 0xc00);
639                 return -EINVAL;
640         }
641
642         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
643         if (!devpriv)
644                 return -ENOMEM;
645
646         init_timer(&devpriv->timer);
647         switch (pcidev->device) {
648         case PCI_DEVICE_ID_JR3_1_CHANNEL:
649         case PCI_DEVICE_ID_JR3_1_CHANNEL_NEW:
650                 devpriv->n_channels = 1;
651                 break;
652         case PCI_DEVICE_ID_JR3_2_CHANNEL:
653                 devpriv->n_channels = 2;
654                 break;
655         case PCI_DEVICE_ID_JR3_3_CHANNEL:
656                 devpriv->n_channels = 3;
657                 break;
658         case PCI_DEVICE_ID_JR3_4_CHANNEL:
659                 devpriv->n_channels = 4;
660                 break;
661         default:
662                 dev_err(dev->class_dev, "jr3_pci: pci %s not supported\n",
663                         pci_name(pcidev));
664                 return -EINVAL;
665                 break;
666         }
667
668         result = comedi_pci_enable(dev);
669         if (result)
670                 return result;
671
672         devpriv->iobase = pci_ioremap_bar(pcidev, 0);
673         if (!devpriv->iobase)
674                 return -ENOMEM;
675
676         result = comedi_alloc_subdevices(dev, devpriv->n_channels);
677         if (result)
678                 return result;
679
680         dev->open = jr3_pci_open;
681         for (i = 0; i < devpriv->n_channels; i++) {
682                 dev->subdevices[i].type = COMEDI_SUBD_AI;
683                 dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND;
684                 dev->subdevices[i].n_chan = 8 * 7 + 2;
685                 dev->subdevices[i].insn_read = jr3_pci_ai_insn_read;
686                 dev->subdevices[i].private =
687                         kzalloc(sizeof(struct jr3_pci_subdev_private),
688                                 GFP_KERNEL);
689                 if (dev->subdevices[i].private) {
690                         struct jr3_pci_subdev_private *p;
691                         int j;
692
693                         p = dev->subdevices[i].private;
694                         p->channel = &devpriv->iobase->channel[i].data;
695                         dev_dbg(dev->class_dev, "p->channel %p %p (%tx)\n",
696                                 p->channel, devpriv->iobase,
697                                 ((char __iomem *)p->channel -
698                                  (char __iomem *)devpriv->iobase));
699                         p->channel_no = i;
700                         for (j = 0; j < 8; j++) {
701                                 int k;
702
703                                 p->range[j].length = 1;
704                                 p->range[j].range.min = -1000000;
705                                 p->range[j].range.max = 1000000;
706                                 for (k = 0; k < 7; k++) {
707                                         p->range_table_list[j + k * 8] =
708                                             (struct comedi_lrange *)&p->
709                                             range[j];
710                                         p->maxdata_list[j + k * 8] = 0x7fff;
711                                 }
712                         }
713                         p->range[8].length = 1;
714                         p->range[8].range.min = 0;
715                         p->range[8].range.max = 65536;
716
717                         p->range_table_list[56] =
718                                 (struct comedi_lrange *)&p->range[8];
719                         p->range_table_list[57] =
720                                 (struct comedi_lrange *)&p->range[8];
721                         p->maxdata_list[56] = 0xffff;
722                         p->maxdata_list[57] = 0xffff;
723                         /*  Channel specific range and maxdata */
724                         dev->subdevices[i].range_table = NULL;
725                         dev->subdevices[i].range_table_list =
726                                 p->range_table_list;
727                         dev->subdevices[i].maxdata = 0;
728                         dev->subdevices[i].maxdata_list = p->maxdata_list;
729                 }
730         }
731
732         /*  Reset DSP card */
733         writel(0, &devpriv->iobase->channel[0].reset);
734
735         result = comedi_load_firmware(dev, &comedi_to_pci_dev(dev)->dev,
736                                       "comedi/jr3pci.idm",
737                                       jr3_download_firmware, 0);
738         dev_dbg(dev->class_dev, "Firmare load %d\n", result);
739
740         if (result < 0)
741                 return result;
742         /*
743          * TODO: use firmware to load preferred offset tables. Suggested
744          * format:
745          *     model serial Fx Fy Fz Mx My Mz\n
746          *
747          *     comedi_load_firmware(dev, &comedi_to_pci_dev(dev)->dev,
748          *                          "comedi/jr3_offsets_table",
749          *                          jr3_download_firmware, 1);
750          */
751
752         /*
753          * It takes a few milliseconds for software to settle as much as we
754          * can read firmware version
755          */
756         msleep_interruptible(25);
757         for (i = 0; i < 0x18; i++) {
758                 dev_dbg(dev->class_dev, "%c\n",
759                         get_u16(&devpriv->iobase->channel[0].
760                                 data.copyright[i]) >> 8);
761         }
762
763         /*  Start card timer */
764         for (i = 0; i < devpriv->n_channels; i++) {
765                 struct jr3_pci_subdev_private *p = dev->subdevices[i].private;
766
767                 p->next_time_min = jiffies + msecs_to_jiffies(500);
768                 p->next_time_max = jiffies + msecs_to_jiffies(2000);
769         }
770
771         devpriv->timer.data = (unsigned long)dev;
772         devpriv->timer.function = jr3_pci_poll_dev;
773         devpriv->timer.expires = jiffies + msecs_to_jiffies(1000);
774         add_timer(&devpriv->timer);
775
776         return result;
777 }
778
779 static void jr3_pci_detach(struct comedi_device *dev)
780 {
781         int i;
782         struct jr3_pci_dev_private *devpriv = dev->private;
783
784         if (devpriv) {
785                 del_timer_sync(&devpriv->timer);
786
787                 if (dev->subdevices) {
788                         for (i = 0; i < devpriv->n_channels; i++)
789                                 kfree(dev->subdevices[i].private);
790                 }
791                 if (devpriv->iobase)
792                         iounmap(devpriv->iobase);
793         }
794         comedi_pci_disable(dev);
795 }
796
797 static struct comedi_driver jr3_pci_driver = {
798         .driver_name    = "jr3_pci",
799         .module         = THIS_MODULE,
800         .auto_attach    = jr3_pci_auto_attach,
801         .detach         = jr3_pci_detach,
802 };
803
804 static int jr3_pci_pci_probe(struct pci_dev *dev,
805                              const struct pci_device_id *id)
806 {
807         return comedi_pci_auto_config(dev, &jr3_pci_driver, id->driver_data);
808 }
809
810 static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_table) = {
811         { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL) },
812         { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL_NEW) },
813         { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_2_CHANNEL) },
814         { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_3_CHANNEL) },
815         { PCI_DEVICE(PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_4_CHANNEL) },
816         { 0 }
817 };
818 MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);
819
820 static struct pci_driver jr3_pci_pci_driver = {
821         .name           = "jr3_pci",
822         .id_table       = jr3_pci_pci_table,
823         .probe          = jr3_pci_pci_probe,
824         .remove         = comedi_pci_auto_unconfig,
825 };
826 module_comedi_pci_driver(jr3_pci_driver, jr3_pci_pci_driver);
827
828 MODULE_AUTHOR("Comedi http://www.comedi.org");
829 MODULE_DESCRIPTION("Comedi low-level driver");
830 MODULE_LICENSE("GPL");
831 MODULE_FIRMWARE("comedi/jr3pci.idm");