summaryrefslogtreecommitdiffstats
path: root/configs/fedora/generic/CONFIG_ARM64_USE_LSE_ATOMICS
Commit message (Collapse)AuthorAgeFilesLines
* Linux v5.8.1Justin M. Forbes2020-08-121-0/+1
Signed-off-by: Justin M. Forbes <jforbes@fedoraproject.org>
id='n55' href='#n55'>55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
/*
  comedi/drivers/jr3_pci.c
  hardware driver for JR3/PCI force sensor board

  COMEDI - Linux Control and Measurement Device Interface
  Copyright (C) 2007 Anders Blomdell <anders.blomdell@control.lth.se>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
/*
Driver: jr3_pci
Description: JR3/PCI force sensor board
Author: Anders Blomdell <anders.blomdell@control.lth.se>
Status: works
Devices: [JR3] PCI force sensor board (jr3_pci)

  The DSP on the board requires initialization code, which can
  be loaded by placing it in /lib/firmware/comedi.
  The initialization code should be somewhere on the media you got
  with your card. One version is available from http://www.comedi.org
  in the comedi_nonfree_firmware tarball.

  Configuration options:
  [0] - PCI bus number - if bus number and slot number are 0,
                         then driver search for first unused card
  [1] - PCI slot number

*/

#include "../comedidev.h"

#include <linux/delay.h>
#include <linux/ctype.h>
#include <linux/firmware.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include "comedi_pci.h"
#include "jr3_pci.h"

#define PCI_VENDOR_ID_JR3 0x1762
#define PCI_DEVICE_ID_JR3_1_CHANNEL 0x3111
#define PCI_DEVICE_ID_JR3_2_CHANNEL 0x3112
#define PCI_DEVICE_ID_JR3_3_CHANNEL 0x3113
#define PCI_DEVICE_ID_JR3_4_CHANNEL 0x3114

static int jr3_pci_attach(struct comedi_device *dev,
			  struct comedi_devconfig *it);
static int jr3_pci_detach(struct comedi_device *dev);

static struct comedi_driver driver_jr3_pci = {
	.driver_name = "jr3_pci",
	.module = THIS_MODULE,
	.attach = jr3_pci_attach,
	.detach = jr3_pci_detach,
};

static DEFINE_PCI_DEVICE_TABLE(jr3_pci_pci_table) = {
	{
	PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_1_CHANNEL,
		    PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
	PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_2_CHANNEL,
		    PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
	PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_3_CHANNEL,
		    PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
	PCI_VENDOR_ID_JR3, PCI_DEVICE_ID_JR3_4_CHANNEL,
		    PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
	0}
};

MODULE_DEVICE_TABLE(pci, jr3_pci_pci_table);

struct jr3_pci_dev_private {

	struct pci_dev *pci_dev;
	int pci_enabled;
	volatile struct jr3_t *iobase;
	int n_channels;
	struct timer_list timer;
};

struct poll_delay_t {

	int min;
	int max;
};

struct jr3_pci_subdev_private {
	volatile struct jr3_channel *channel;
	unsigned long next_time_min;
	unsigned long next_time_max;
	enum { state_jr3_poll,
		state_jr3_init_wait_for_offset,
		state_jr3_init_transform_complete,
		state_jr3_init_set_full_scale_complete,
		state_jr3_init_use_offset_complete,
		state_jr3_done
	} state;
	int channel_no;
	int serial_no;
	int model_no;
	struct {
		int length;
		struct comedi_krange range;
	} range[9];
	const struct comedi_lrange *range_table_list[8 * 7 + 2];
	unsigned int maxdata_list[8 * 7 + 2];
	u16 errors;
	int retries;
};

/* Hotplug firmware loading stuff */

typedef int comedi_firmware_callback(struct comedi_device *dev,
				     const u8 * data, size_t size);

static int comedi_load_firmware(struct comedi_device *dev, char *name,
				comedi_firmware_callback cb)
{
	int result = 0;
	const struct firmware *fw;
	char *firmware_path;
	static const char *prefix = "comedi/";
	struct jr3_pci_dev_private *devpriv = dev->private;

	firmware_path = kmalloc(strlen(prefix) + strlen(name) + 1, GFP_KERNEL);
	if (!firmware_path) {
		result = -ENOMEM;
	} else {
		firmware_path[0] = '\0';
		strcat(firmware_path, prefix);
		strcat(firmware_path, name);
		result = request_firmware(&fw, firmware_path,
					  &devpriv->pci_dev->dev);
		if (result == 0) {
			if (!cb)
				result = -EINVAL;
			else
				result = cb(dev, fw->data, fw->size);
			release_firmware(fw);
		}
		kfree(firmware_path);
	}
	return result;
}

static struct poll_delay_t poll_delay_min_max(int min, int max)
{
	struct poll_delay_t result;

	result.min = min;
	result.max = max;
	return result;
}

static int is_complete(volatile struct jr3_channel *channel)
{
	return get_s16(&channel->command_word0) == 0;
}

struct transform_t {
	struct {
		u16 link_type;
		s16 link_amount;
	} link[8];
};

static void set_transforms(volatile struct jr3_channel *channel,
			   struct transform_t transf, short num)
{
	int i;

	num &= 0x000f;		/*  Make sure that 0 <= num <= 15 */
	for (i = 0; i < 8; i++) {

		set_u16(&channel->transforms[num].link[i].link_type,
			transf.link[i].link_type);
		udelay(1);
		set_s16(&channel->transforms[num].link[i].link_amount,
			transf.link[i].link_amount);
		udelay(1);
		if (transf.link[i].link_type == end_x_form) {
			break;
		}
	}
}

static void use_transform(volatile struct jr3_channel *channel,
			  short transf_num)
{
	set_s16(&channel->command_word0, 0x0500 + (transf_num & 0x000f));
}

static void use_offset(volatile struct jr3_channel *channel, short offset_num)
{
	set_s16(&channel->command_word0, 0x0600 + (offset_num & 0x000f));
}

static void set_offset(volatile struct jr3_channel *channel)
{
	set_s16(&channel->command_word0, 0x0700);
}

struct six_axis_t {
	s16 fx;
	s16 fy;
	s16 fz;
	s16 mx;
	s16 my;
	s16 mz;
};

static void set_full_scales(volatile struct jr3_channel *channel,
			    struct six_axis_t full_scale)
{
	printk("%d %d %d %d %d %d\n",
	       full_scale.fx,
	       full_scale.fy,
	       full_scale.fz, full_scale.mx, full_scale.my, full_scale.mz);
	set_s16(&channel->full_scale.fx, full_scale.fx);
	set_s16(&channel->full_scale.fy, full_scale.fy);
	set_s16(&channel->full_scale.fz, full_scale.fz);
	set_s16(&channel->full_scale.mx, full_scale.mx);
	set_s16(&channel->full_scale.my, full_scale.my);
	set_s16(&channel->full_scale.mz, full_scale.mz);
	set_s16(&channel->command_word0, 0x0a00);
}

static struct six_axis_t get_min_full_scales(volatile struct jr3_channel
					     *channel)
{
	struct six_axis_t result;
	result.fx = get_s16(&channel->min_full_scale.fx);
	result.fy = get_s16(&channel->min_full_scale.fy);
	result.fz = get_s16(&channel->min_full_scale.fz);
	result.mx = get_s16(&channel->min_full_scale.mx);
	result.my = get_s16(&channel->min_full_scale.my);
	result.mz = get_s16(&channel->min_full_scale.mz);
	return result;
}

static struct six_axis_t get_max_full_scales(volatile struct jr3_channel
					     *channel)
{
	struct six_axis_t result;
	result.fx = get_s16(&channel->max_full_scale.fx);
	result.fy = get_s16(&channel->max_full_scale.fy);
	result.fz = get_s16(&channel->max_full_scale.fz);
	result.mx = get_s16(&channel->max_full_scale.mx);
	result.my = get_s16(&channel->max_full_scale.my);
	result.mz = get_s16(&channel->max_full_scale.mz);
	return result;
}

static int jr3_pci_ai_insn_read(struct comedi_device *dev,
				struct comedi_subdevice *s,
				struct comedi_insn *insn, unsigned int *data)
{
	int result;
	struct jr3_pci_subdev_private *p;
	int channel;

	p = s->private;
	channel = CR_CHAN(insn->chanspec);
	if (p == NULL || channel > 57) {
		result = -EINVAL;
	} else {
		int i;

		result = insn->n;
		if (p->state != state_jr3_done ||
		    (get_u16(&p->channel->errors) & (watch_dog | watch_dog2 |
						     sensor_change))) {
			/* No sensor or sensor changed */
			if (p->state == state_jr3_done) {
				/* Restart polling */
				p->state = state_jr3_poll;
			}
			result = -EAGAIN;
		}
		for (i = 0; i < insn->n; i++) {
			if (channel < 56) {
				int axis, filter;

				axis = channel % 8;
				filter = channel / 8;
				if (p->state != state_jr3_done) {
					data[i] = 0;
				} else {
					int F = 0;
					switch (axis) {
					case 0:{
							F = get_s16
							    (&p->channel->filter
							     [filter].fx);
						}
						break;
					case 1:{
							F = get_s16
							    (&p->channel->filter
							     [filter].fy);
						}
						break;
					case 2:{
							F = get_s16
							    (&p->channel->filter
							     [filter].fz);
						}
						break;
					case 3:{
							F = get_s16
							    (&p->channel->filter
							     [filter].mx);
						}
						break;
					case 4:{
							F = get_s16
							    (&p->channel->filter
							     [filter].my);
						}
						break;
					case 5:{
							F = get_s16
							    (&p->channel->filter
							     [filter].mz);
						}
						break;
					case 6:{
							F = get_s16
							    (&p->channel->filter
							     [filter].v1);
						}
						break;
					case 7:{
							F = get_s16
							    (&p->channel->filter
							     [filter].v2);
						}
						break;
					}
					data[i] = F + 0x4000;
				}
			} else if (channel == 56) {
				if (p->state != state_jr3_done) {
					data[i] = 0;
				} else {
					data[i] =
					    get_u16(&p->channel->model_no);
				}
			} else if (channel == 57) {
				if (p->state != state_jr3_done) {
					data[i] = 0;
				} else {
					data[i] =
					    get_u16(&p->channel->serial_no);
				}
			}
		}
	}
	return result;
}

static void jr3_pci_open(struct comedi_device *dev)
{
	int i;
	struct jr3_pci_dev_private *devpriv = dev->private;

	printk("jr3_pci_open\n");
	for (i = 0; i < devpriv->n_channels; i++) {
		struct jr3_pci_subdev_private *p;

		p = dev->subdevices[i].private;
		if (p) {
			printk("serial: %p %d (%d)\n", p, p->serial_no,
			       p->channel_no);
		}
	}
}

int read_idm_word(const u8 * data, size_t size, int *pos, unsigned int *val)
{
	int result = 0;
	if (pos != 0 && val != 0) {
		/*  Skip over non hex */
		for (; *pos < size && !isxdigit(data[*pos]); (*pos)++) {
		}
		/*  Collect value */
		*val = 0;
		for (; *pos < size && isxdigit(data[*pos]); (*pos)++) {
			char ch = tolower(data[*pos]);
			result = 1;
			if ('0' <= ch && ch <= '9') {
				*val = (*val << 4) + (ch - '0');
			} else if ('a' <= ch && ch <= 'f') {
				*val = (*val << 4) + (ch - 'a' + 10);
			}
		}
	}
	return result;
}

static int jr3_download_firmware(struct comedi_device *dev, const u8 * data,
				 size_t size)
{
	/*
	 * IDM file format is:
	 *   { count, address, data <count> } *
	 *   ffff
	 */
	int result, more, pos, OK;

	result = 0;
	more = 1;
	pos = 0;
	OK = 0;
	while (more) {
		unsigned int count, addr;

		more = more && read_idm_word(data, size, &pos, &count);
		if (more && count == 0xffff) {
			OK = 1;
			break;
		}
		more = more && read_idm_word(data, size, &pos, &addr);
		while (more && count > 0) {
			unsigned int dummy;
			more = more && read_idm_word(data, size, &pos, &dummy);
			count--;
		}
	}

	if (!OK) {
		result = -ENODATA;
	} else {
		int i;
		struct jr3_pci_dev_private *p = dev->private;

		for (i = 0; i < p->n_channels; i++) {
			struct jr3_pci_subdev_private *sp;

			sp = dev->subdevices[i].private;
			more = 1;
			pos = 0;
			while (more) {
				unsigned int count, addr;
				more = more
				    && read_idm_word(data, size, &pos, &count);
				if (more && count == 0xffff) {
					break;
				}
				more = more
				    && read_idm_word(data, size, &pos, &addr);
				printk("Loading#%d %4.4x bytes at %4.4x\n", i,
				       count, addr);
				while (more && count > 0) {
					if (addr & 0x4000) {
						/*  16 bit data, never seen in real life!! */
						unsigned int data1;

						more = more
						    && read_idm_word(data,
								     size, &pos,
								     &data1);
						count--;
						/* printk("jr3_data, not tested\n"); */
						/* jr3[addr + 0x20000 * pnum] = data1; */
					} else {
						/*   Download 24 bit program */
						unsigned int data1, data2;

						more = more
						    && read_idm_word(data,
								     size, &pos,
								     &data1);
						more = more
						    && read_idm_word(data, size,
								     &pos,
								     &data2);
						count -= 2;
						if (more) {
							set_u16(&p->
								iobase->channel
								[i].program_low
								[addr], data1);
							udelay(1);
							set_u16(&p->
								iobase->channel
								[i].program_high
								[addr], data2);
							udelay(1);

						}
					}
					addr++;
				}
			}
		}
	}
	return result;
}

static struct poll_delay_t jr3_pci_poll_subdevice(struct comedi_subdevice *s)
{
	struct poll_delay_t result = poll_delay_min_max(1000, 2000);
	struct jr3_pci_subdev_private *p = s->private;
	int i;

	if (p) {
		volatile struct jr3_channel *channel = p->channel;
		int errors = get_u16(&channel->errors);

		if (errors != p->errors) {
			printk("Errors: %x -> %x\n", p->errors, errors);
			p->errors = errors;
		}
		if (errors & (watch_dog | watch_dog2 | sensor_change)) {
			/*  Sensor communication lost, force poll mode */
			p->state = state_jr3_poll;

		}
		switch (p->state) {
		case state_jr3_poll:{
				u16 model_no = get_u16(&channel->model_no);
				u16 serial_no = get_u16(&channel->serial_no);
				if ((errors & (watch_dog | watch_dog2)) ||
				    model_no == 0 || serial_no == 0) {
/*
 * Still no sensor, keep on polling. Since it takes up to 10 seconds
 * for offsets to stabilize, polling each second should suffice.
 */
					result = poll_delay_min_max(1000, 2000);
				} else {
					p->retries = 0;
					p->state =
					    state_jr3_init_wait_for_offset;
					result = poll_delay_min_max(1000, 2000);
				}
			}
			break;
		case state_jr3_init_wait_for_offset:{
				p->retries++;
				if (p->retries < 10) {
					/*  Wait for offeset to stabilize (< 10 s according to manual) */
					result = poll_delay_min_max(1000, 2000);
				} else {
					struct transform_t transf;

					p->model_no =
					    get_u16(&channel->model_no);
					p->serial_no =
					    get_u16(&channel->serial_no);

					printk
					    ("Setting transform for channel %d\n",
					     p->channel_no);
					printk("Sensor Model     = %i\n",
					       p->model_no);
					printk("Sensor Serial    = %i\n",
					       p->serial_no);

					/*  Transformation all zeros */
					for (i = 0; i < ARRAY_SIZE(transf.link); i++) {
						transf.link[i].link_type =
							(enum link_types)0;
						transf.link[i].link_amount = 0;
					}

					set_transforms(channel, transf, 0);
					use_transform(channel, 0);
					p->state =
					    state_jr3_init_transform_complete;
					result = poll_delay_min_max(20, 100);	/*  Allow 20 ms for completion */
				}
			} break;
		case state_jr3_init_transform_complete:{
				if (!is_complete(channel)) {
					printk
					    ("state_jr3_init_transform_complete complete = %d\n",
					     is_complete(channel));
					result = poll_delay_min_max(20, 100);
				} else {
					/*  Set full scale */
					struct six_axis_t min_full_scale;
					struct six_axis_t max_full_scale;

					min_full_scale =
					    get_min_full_scales(channel);
					printk("Obtained Min. Full Scales:\n");
					printk("%i   ", (min_full_scale).fx);
					printk("%i   ", (min_full_scale).fy);
					printk("%i   ", (min_full_scale).fz);
					printk("%i   ", (min_full_scale).mx);
					printk("%i   ", (min_full_scale).my);
					printk("%i   ", (min_full_scale).mz);
					printk("\n");

					max_full_scale =
					    get_max_full_scales(channel);
					printk("Obtained Max. Full Scales:\n");
					printk("%i   ", (max_full_scale).fx);
					printk("%i   ", (max_full_scale).fy);
					printk("%i   ", (max_full_scale).fz);
					printk("%i   ", (max_full_scale).mx);
					printk("%i   ", (max_full_scale).my);
					printk("%i   ", (max_full_scale).mz);
					printk("\n");

					set_full_scales(channel,
							max_full_scale);

					p->state =
					    state_jr3_init_set_full_scale_complete;
					result = poll_delay_min_max(20, 100);	/*  Allow 20 ms for completion */
				}
			}
			break;
		case state_jr3_init_set_full_scale_complete:{
				if (!is_complete(channel)) {
					printk
					    ("state_jr3_init_set_full_scale_complete complete = %d\n",
					     is_complete(channel));
					result = poll_delay_min_max(20, 100);
				} else {
					volatile struct force_array *full_scale;

					/*  Use ranges in kN or we will overflow arount 2000N! */
					full_scale = &channel->full_scale;
					p->range[0].range.min =
					    -get_s16(&full_scale->fx) * 1000;
					p->range[0].range.max =
					    get_s16(&full_scale->fx) * 1000;
					p->range[1].range.min =
					    -get_s16(&full_scale->fy) * 1000;
					p->range[1].range.max =
					    get_s16(&full_scale->fy) * 1000;
					p->range[2].range.min =
					    -get_s16(&full_scale->fz) * 1000;
					p->range[2].range.max =
					    get_s16(&full_scale->fz) * 1000;
					p->range[3].range.min =
					    -get_s16(&full_scale->mx) * 100;
					p->range[3].range.max =
					    get_s16(&full_scale->mx) * 100;
					p->range[4].range.min =
					    -get_s16(&full_scale->my) * 100;
					p->range[4].range.max =
					    get_s16(&full_scale->my) * 100;
					p->range[5].range.min =
					    -get_s16(&full_scale->mz) * 100;
					p->range[5].range.max =
					    get_s16(&full_scale->mz) * 100;
					p->range[6].range.min = -get_s16(&full_scale->v1) * 100;	/*  ?? */
					p->range[6].range.max = get_s16(&full_scale->v1) * 100;	/*  ?? */
					p->range[7].range.min = -get_s16(&full_scale->v2) * 100;	/*  ?? */
					p->range[7].range.max = get_s16(&full_scale->v2) * 100;	/*  ?? */
					p->range[8].range.min = 0;
					p->range[8].range.max = 65535;

					{
						int i;
						for (i = 0; i < 9; i++) {
							printk("%d %d - %d\n",
							       i,
							       p->
							       range[i].range.
							       min,
							       p->
							       range[i].range.
							       max);
						}
					}

					use_offset(channel, 0);
					p->state =
					    state_jr3_init_use_offset_complete;
					result = poll_delay_min_max(40, 100);	/*  Allow 40 ms for completion */
				}
			}
			break;
		case state_jr3_init_use_offset_complete:{
				if (!is_complete(channel)) {
					printk
					    ("state_jr3_init_use_offset_complete complete = %d\n",
					     is_complete(channel));
					result = poll_delay_min_max(20, 100);
				} else {
					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));

					set_s16(&channel->offsets.fx, 0);
					set_s16(&channel->offsets.fy, 0);
					set_s16(&channel->offsets.fz, 0);
					set_s16(&channel->offsets.mx, 0);
					set_s16(&channel->offsets.my, 0);
					set_s16(&channel->offsets.mz, 0);

					set_offset(channel);

					p->state = state_jr3_done;
				}
			}
			break;
		case state_jr3_done:{
				poll_delay_min_max(10000, 20000);
			}
			break;
		default:{
				poll_delay_min_max(1000, 2000);
			}
			break;
		}
	}
	return result;
}

static void jr3_pci_poll_dev(unsigned long data)
{
	unsigned long flags;
	struct comedi_device *dev = (struct comedi_device *)data;
	struct jr3_pci_dev_private *devpriv = dev->private;
	unsigned long now;
	int delay;
	int i;

	spin_lock_irqsave(&dev->spinlock, flags);
	delay = 1000;
	now = jiffies;
	/*  Poll all channels that are ready to be polled */
	for (i = 0; i < devpriv->n_channels; i++) {
		struct jr3_pci_subdev_private *subdevpriv =
		    dev->subdevices[i].private;
		if (now > subdevpriv->next_time_min) {
			struct poll_delay_t sub_delay;

			sub_delay = jr3_pci_poll_subdevice(&dev->subdevices[i]);
			subdevpriv->next_time_min =
			    jiffies + msecs_to_jiffies(sub_delay.min);
			subdevpriv->next_time_max =
			    jiffies + msecs_to_jiffies(sub_delay.max);
			if (sub_delay.max && sub_delay.max < delay) {
/*
* Wake up as late as possible -> poll as many channels as possible
* at once
*/
				delay = sub_delay.max;
			}
		}
	}
	spin_unlock_irqrestore(&dev->spinlock, flags);

	devpriv->timer.expires = jiffies + msecs_to_jiffies(delay);
	add_timer(&devpriv->timer);
}

static int jr3_pci_attach(struct comedi_device *dev,
			  struct comedi_devconfig *it)
{
	int result = 0;
	struct pci_dev *card = NULL;
	int opt_bus, opt_slot, i;
	struct jr3_pci_dev_private *devpriv;

	printk("comedi%d: jr3_pci\n", dev->minor);

	opt_bus = it->options[0];
	opt_slot = it->options[1];

	if (sizeof(struct jr3_channel) != 0xc00) {
		printk("sizeof(struct jr3_channel) = %x [expected %x]\n",
		       (unsigned)sizeof(struct jr3_channel), 0xc00);
		return -EINVAL;
	}

	result = alloc_private(dev, sizeof(struct jr3_pci_dev_private));
	if (result < 0) {
		return -ENOMEM;
	}
	card = NULL;
	devpriv = dev->private;
	init_timer(&devpriv->timer);
	while (1) {
		card = pci_get_device(PCI_VENDOR_ID_JR3, PCI_ANY_ID, card);
		if (card == NULL) {
			/* No card found */
			break;
		} else {
			switch (card->device) {
			case PCI_DEVICE_ID_JR3_1_CHANNEL:{
					devpriv->n_channels = 1;
				}
				break;
			case PCI_DEVICE_ID_JR3_2_CHANNEL:{
					devpriv->n_channels = 2;
				}
				break;
			case PCI_DEVICE_ID_JR3_3_CHANNEL:{
					devpriv->n_channels = 3;
				}
				break;
			case PCI_DEVICE_ID_JR3_4_CHANNEL:{
					devpriv->n_channels = 4;
				}
				break;
			default:{
					devpriv->n_channels = 0;
				}
			}
			if (devpriv->n_channels >= 1) {
				if (opt_bus == 0 && opt_slot == 0) {
					/* Take first available card */
					break;
				} else if (opt_bus == card->bus->number &&
					   opt_slot == PCI_SLOT(card->devfn)) {
					/* Take requested card */
					break;
				}
			}
		}
	}
	if (!card) {
		printk(" no jr3_pci found\n");
		return -EIO;
	} else {
		devpriv->pci_dev = card;
		dev->board_name = "jr3_pci";
	}

	result = comedi_pci_enable(card, "jr3_pci");
	if (result < 0) {
		return -EIO;
	}

	devpriv->pci_enabled = 1;
	devpriv->iobase = ioremap(pci_resource_start(card, 0),
			offsetof(struct jr3_t, channel[devpriv->n_channels]));
	if (!devpriv->iobase)
		return -ENOMEM;

	result = alloc_subdevices(dev, devpriv->n_channels);
	if (result < 0)
		goto out;

	dev->open = jr3_pci_open;
	for (i = 0; i < devpriv->n_channels; i++) {
		dev->subdevices[i].type = COMEDI_SUBD_AI;
		dev->subdevices[i].subdev_flags = SDF_READABLE | SDF_GROUND;
		dev->subdevices[i].n_chan = 8 * 7 + 2;
		dev->subdevices[i].insn_read = jr3_pci_ai_insn_read;
		dev->subdevices[i].private =
		    kzalloc(sizeof(struct jr3_pci_subdev_private), GFP_KERNEL);
		if (dev->subdevices[i].private) {
			struct jr3_pci_subdev_private *p;
			int j;

			p = dev->subdevices[i].private;
			p->channel = &devpriv->iobase->channel[i].data;
			printk("p->channel %p %p (%tx)\n",
			       p->channel, devpriv->iobase,
			       ((char *)(p->channel) -
				(char *)(devpriv->iobase)));
			p->channel_no = i;
			for (j = 0; j < 8; j++) {
				int k;

				p->range[j].length = 1;
				p->range[j].range.min = -1000000;
				p->range[j].range.max = 1000000;
				for (k = 0; k < 7; k++) {
					p->range_table_list[j + k * 8] =
					    (struct comedi_lrange *)&p->
					    range[j];
					p->maxdata_list[j + k * 8] = 0x7fff;
				}
			}
			p->range[8].length = 1;
			p->range[8].range.min = 0;
			p->range[8].range.max = 65536;

			p->range_table_list[56] =
			    (struct comedi_lrange *)&p->range[8];
			p->range_table_list[57] =
			    (struct comedi_lrange *)&p->range[8];
			p->maxdata_list[56] = 0xffff;
			p->maxdata_list[57] = 0xffff;
			/*  Channel specific range and maxdata */
			dev->subdevices[i].range_table = 0;
			dev->subdevices[i].range_table_list =
			    p->range_table_list;
			dev->subdevices[i].maxdata = 0;
			dev->subdevices[i].maxdata_list = p->maxdata_list;
		}
	}

	/*  Reset DSP card */
	devpriv->iobase->channel[0].reset = 0;

	result = comedi_load_firmware(dev, "jr3pci.idm", jr3_download_firmware);
	printk("Firmare load %d\n", result);

	if (result < 0) {
		goto out;
	}
/*
 * TODO: use firmware to load preferred offset tables. Suggested
 * format:
 *     model serial Fx Fy Fz Mx My Mz\n
 *
 *     comedi_load_firmware(dev, "jr3_offsets_table", jr3_download_firmware);
 */

/*
 * It takes a few milliseconds for software to settle as much as we
 * can read firmware version
 */
	msleep_interruptible(25);
	for (i = 0; i < 0x18; i++) {
		printk("%c",
		       get_u16(&devpriv->iobase->channel[0].
			       data.copyright[i]) >> 8);
	}

	/*  Start card timer */
	for (i = 0; i < devpriv->n_channels; i++) {
		struct jr3_pci_subdev_private *p = dev->subdevices[i].private;

		p->next_time_min = jiffies + msecs_to_jiffies(500);
		p->next_time_max = jiffies + msecs_to_jiffies(2000);
	}

	devpriv->timer.data = (unsigned long)dev;
	devpriv->timer.function = jr3_pci_poll_dev;
	devpriv->timer.expires = jiffies + msecs_to_jiffies(1000);
	add_timer(&devpriv->timer);

out:
	return result;
}

MODULE_FIRMWARE("comedi/jr3pci.idm");

static int jr3_pci_detach(struct comedi_device *dev)
{
	int i;
	struct jr3_pci_dev_private *devpriv = dev->private;

	printk("comedi%d: jr3_pci: remove\n", dev->minor);
	if (devpriv) {
		del_timer_sync(&devpriv->timer);

		if (dev->subdevices) {
			for (i = 0; i < devpriv->n_channels; i++) {
				kfree(dev->subdevices[i].private);
			}
		}

		if (devpriv->iobase) {
			iounmap((void *)devpriv->iobase);
		}
		if (devpriv->pci_enabled) {
			comedi_pci_disable(devpriv->pci_dev);
		}

		if (devpriv->pci_dev) {
			pci_dev_put(devpriv->pci_dev);
		}
	}
	return 0;
}

COMEDI_PCI_INITCLEANUP(driver_jr3_pci, jr3_pci_pci_table);