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