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