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