summaryrefslogtreecommitdiff
path: root/drivers/gpib/hp_82341/hp_82341.c
blob: 1a2ad0560e1416190115a968ad8a35612f98a2d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
// SPDX-License-Identifier: GPL-2.0

/***************************************************************************
 *     Driver for hp 82341a/b/c/d boards.                                  *
 * Might be worth merging with Agilent 82350b driver.                      *
 *   copyright            : (C) 2002, 2005 by Frank Mori Hess              *
 ***************************************************************************/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define dev_fmt pr_fmt
#define DRV_NAME KBUILD_MODNAME

#include "hp_82341.h"
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/isapnp.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("GPIB driver for hp 82341a/b/c/d boards");

static unsigned short read_and_clear_event_status(struct gpib_board *board);
static void set_transfer_counter(struct hp_82341_priv *hp_priv, int count);
static int read_transfer_counter(struct hp_82341_priv *hp_priv);
static int hp_82341_write(struct gpib_board *board, u8 *buffer, size_t length, int send_eoi,
			  size_t *bytes_written);
static irqreturn_t hp_82341_interrupt(int irq, void *arg);

static int hp_82341_accel_read(struct gpib_board *board, u8 *buffer, size_t length, int *end,
			       size_t *bytes_read)
{
	struct hp_82341_priv *hp_priv = board->private_data;
	struct tms9914_priv *tms_priv = &hp_priv->tms9914_priv;
	int retval = 0;
	unsigned short event_status;
	int i;
	int num_fifo_bytes;
	// hardware doesn't support checking for end-of-string character when using fifo
	if (tms_priv->eos_flags & REOS)
		return tms9914_read(board, tms_priv, buffer, length, end, bytes_read);

	clear_bit(DEV_CLEAR_BN, &tms_priv->state);

	read_and_clear_event_status(board);
	*end = 0;
	*bytes_read = 0;
	if (length == 0)
		return 0;
	// disable fifo for the moment
	outb(DIRECTION_GPIB_TO_HOST_BIT, hp_priv->iobase[3] + BUFFER_CONTROL_REG);
	/*
	 * Handle corner case of board not in holdoff and one byte has slipped in already.
	 * Also, board sometimes has problems (spurious 1 byte reads) when read fifo is
	 * started up with board in TACS under certain data holdoff conditions.
	 * Doing a 1 byte tms9914-style read avoids these problems.
	 */
	if (/*tms_priv->holdoff_active == 0 && */length > 1) {
		size_t num_bytes;

		retval = tms9914_read(board, tms_priv, buffer, 1, end, &num_bytes);
		*bytes_read += num_bytes;
		if (retval < 0)
			dev_err(board->gpib_dev, "tms9914_read failed retval=%i\n", retval);
		if (retval < 0 || *end)
			return retval;
		++buffer;
		--length;
	}
	tms9914_set_holdoff_mode(tms_priv, TMS9914_HOLDOFF_EOI);
	tms9914_release_holdoff(tms_priv);
	outb(0x00, hp_priv->iobase[3] + BUFFER_FLUSH_REG);
	i = 0;
	num_fifo_bytes = length - 1;
	while (i < num_fifo_bytes && *end == 0)	{
		int block_size;
		int j;
		int count;

		block_size = min(num_fifo_bytes - i, hp_82341_fifo_size);
		set_transfer_counter(hp_priv, block_size);
		outb(ENABLE_TI_BUFFER_BIT | DIRECTION_GPIB_TO_HOST_BIT, hp_priv->iobase[3] +
		     BUFFER_CONTROL_REG);
		if (inb(hp_priv->iobase[0] + STREAM_STATUS_REG) & HALTED_STATUS_BIT)
			outb(RESTART_STREAM_BIT, hp_priv->iobase[0] + STREAM_STATUS_REG);

		clear_bit(READ_READY_BN, &tms_priv->state);

		retval = wait_event_interruptible(board->wait,
						  ((event_status =
						    read_and_clear_event_status(board)) &
						   (TERMINAL_COUNT_EVENT_BIT |
						    BUFFER_END_EVENT_BIT)) ||
						  test_bit(DEV_CLEAR_BN, &tms_priv->state) ||
						  test_bit(TIMO_NUM, &board->status));
		if (retval)  {
			retval = -ERESTARTSYS;
			break;
		}
		// have to disable buffer before we can read from buffer port
		outb(DIRECTION_GPIB_TO_HOST_BIT, hp_priv->iobase[3] + BUFFER_CONTROL_REG);
		count = block_size - read_transfer_counter(hp_priv);
		j = 0;
		while (j < count && i < num_fifo_bytes) {
			unsigned short data_word = inw(hp_priv->iobase[3] + BUFFER_PORT_LOW_REG);

			buffer[i++] = data_word & 0xff;
			++j;
			if (j < count && i < num_fifo_bytes) {
				buffer[i++] = (data_word >> 8) & 0xff;
				++j;
			}
		}
		if (event_status & BUFFER_END_EVENT_BIT) {
			clear_bit(RECEIVED_END_BN, &tms_priv->state);

			*end = 1;
			tms_priv->holdoff_active = 1;
		}
		if (test_bit(TIMO_NUM, &board->status))	{
			retval = -ETIMEDOUT;
			break;
		}
		if (test_bit(DEV_CLEAR_BN, &tms_priv->state)) {
			retval = -EINTR;
			break;
		}
	}
	*bytes_read += i;
	buffer += i;
	length -= i;
	if (retval < 0)
		return retval;
	// read last byte if we havn't received an END yet
	if (*end == 0) {
		size_t num_bytes;
		// try to make sure we holdoff after last byte read
		retval = tms9914_read(board, tms_priv, buffer, length, end, &num_bytes);
		*bytes_read += num_bytes;
		if (retval < 0)
			return retval;
	}
	return 0;
}

static int restart_write_fifo(struct gpib_board *board, struct hp_82341_priv *hp_priv)
{
	struct tms9914_priv *tms_priv = &hp_priv->tms9914_priv;

	if ((inb(hp_priv->iobase[0] + STREAM_STATUS_REG) & HALTED_STATUS_BIT) == 0)
		return 0;
	while (1) {
		int status;

		// restart doesn't work if data holdoff is in effect
		status = tms9914_line_status(board, tms_priv);
		if ((status & BUS_NRFD) == 0) {
			outb(RESTART_STREAM_BIT, hp_priv->iobase[0] + STREAM_STATUS_REG);
			return 0;
		}
		if (test_bit(DEV_CLEAR_BN, &tms_priv->state))
			return -EINTR;
		if (test_bit(TIMO_NUM, &board->status))
			return -ETIMEDOUT;
		if (msleep_interruptible(1))
			return -EINTR;
	}
	return 0;
}

static int hp_82341_accel_write(struct gpib_board *board, u8 *buffer, size_t length,
				int send_eoi, size_t *bytes_written)
{
	struct hp_82341_priv *hp_priv = board->private_data;
	struct tms9914_priv *tms_priv = &hp_priv->tms9914_priv;
	int i, j;
	unsigned short event_status;
	int retval = 0;
	int fifo_xfer_len = length;

	*bytes_written = 0;
	if (send_eoi)
		--fifo_xfer_len;

	clear_bit(DEV_CLEAR_BN, &tms_priv->state);

	read_and_clear_event_status(board);
	outb(0, hp_priv->iobase[3] + BUFFER_CONTROL_REG);
	outb(0x00, hp_priv->iobase[3] + BUFFER_FLUSH_REG);
	for (i = 0; i < fifo_xfer_len;) {
		int block_size;

		block_size = min(fifo_xfer_len - i, hp_82341_fifo_size);
		set_transfer_counter(hp_priv, block_size);
		// load data into board's fifo
		for (j = 0; j < block_size;) {
			unsigned short data_word = buffer[i++];
			++j;
			if (j < block_size) {
				data_word |= buffer[i++] << 8;
				++j;
			}
			outw(data_word, hp_priv->iobase[3] + BUFFER_PORT_LOW_REG);
		}
		clear_bit(WRITE_READY_BN, &tms_priv->state);
		outb(ENABLE_TI_BUFFER_BIT, hp_priv->iobase[3] + BUFFER_CONTROL_REG);
		retval = restart_write_fifo(board, hp_priv);
		if (retval < 0)	{
			dev_err(board->gpib_dev, "failed to restart write stream\n");
			break;
		}
		retval = wait_event_interruptible(board->wait,
						  ((event_status =
						    read_and_clear_event_status(board)) &
						   TERMINAL_COUNT_EVENT_BIT) ||
						  test_bit(DEV_CLEAR_BN, &tms_priv->state) ||
						  test_bit(TIMO_NUM, &board->status));
		outb(0, hp_priv->iobase[3] + BUFFER_CONTROL_REG);
		*bytes_written += block_size - read_transfer_counter(hp_priv);
		if (retval) {
			retval = -ERESTARTSYS;
			break;
		}
		if (test_bit(TIMO_NUM, &board->status))	{
			retval = -ETIMEDOUT;
			break;
		}
		if (test_bit(DEV_CLEAR_BN, &tms_priv->state)) {
			retval = -EINTR;
			break;
		}
	}
	if (retval)
		return retval;
	if (send_eoi) {
		size_t num_bytes;

		retval = hp_82341_write(board, buffer + fifo_xfer_len, 1, 1, &num_bytes);
		*bytes_written += num_bytes;
		if (retval < 0)
			return retval;
	}
	return 0;
}

static int hp_82341_attach(struct gpib_board *board, const struct gpib_board_config *config);

static void hp_82341_detach(struct gpib_board *board);

// wrappers for interface functions
static int hp_82341_read(struct gpib_board *board, u8 *buffer, size_t length, int *end,
			 size_t *bytes_read)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_read(board, &priv->tms9914_priv, buffer, length, end, bytes_read);
}

static int hp_82341_write(struct gpib_board *board, u8 *buffer, size_t length, int send_eoi,
			  size_t *bytes_written)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_write(board, &priv->tms9914_priv, buffer, length, send_eoi, bytes_written);
}

static int hp_82341_command(struct gpib_board *board, u8 *buffer, size_t length,
			    size_t *bytes_written)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_command(board, &priv->tms9914_priv, buffer, length, bytes_written);
}

static int hp_82341_take_control(struct gpib_board *board, int synchronous)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_take_control(board, &priv->tms9914_priv, synchronous);
}

static int hp_82341_go_to_standby(struct gpib_board *board)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_go_to_standby(board, &priv->tms9914_priv);
}

static int hp_82341_request_system_control(struct gpib_board *board, int request_control)
{
	struct hp_82341_priv *priv = board->private_data;

	if (request_control)
		priv->mode_control_bits |= SYSTEM_CONTROLLER_BIT;
	else
		priv->mode_control_bits &= ~SYSTEM_CONTROLLER_BIT;
	outb(priv->mode_control_bits, priv->iobase[0] + MODE_CONTROL_STATUS_REG);
	return tms9914_request_system_control(board, &priv->tms9914_priv, request_control);
}

static void hp_82341_interface_clear(struct gpib_board *board, int assert)
{
	struct hp_82341_priv *priv = board->private_data;

	tms9914_interface_clear(board, &priv->tms9914_priv, assert);
}

static void hp_82341_remote_enable(struct gpib_board *board, int enable)
{
	struct hp_82341_priv *priv = board->private_data;

	tms9914_remote_enable(board, &priv->tms9914_priv, enable);
}

static int hp_82341_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_enable_eos(board, &priv->tms9914_priv, eos_byte, compare_8_bits);
}

static void hp_82341_disable_eos(struct gpib_board *board)
{
	struct hp_82341_priv *priv = board->private_data;

	tms9914_disable_eos(board, &priv->tms9914_priv);
}

static unsigned int hp_82341_update_status(struct gpib_board *board, unsigned int clear_mask)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_update_status(board, &priv->tms9914_priv, clear_mask);
}

static int hp_82341_primary_address(struct gpib_board *board, unsigned int address)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_primary_address(board, &priv->tms9914_priv, address);
}

static int hp_82341_secondary_address(struct gpib_board *board, unsigned int address, int enable)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_secondary_address(board, &priv->tms9914_priv, address, enable);
}

static int hp_82341_parallel_poll(struct gpib_board *board, u8 *result)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_parallel_poll(board, &priv->tms9914_priv, result);
}

static void hp_82341_parallel_poll_configure(struct gpib_board *board, u8 config)
{
	struct hp_82341_priv *priv = board->private_data;

	tms9914_parallel_poll_configure(board, &priv->tms9914_priv, config);
}

static void hp_82341_parallel_poll_response(struct gpib_board *board, int ist)
{
	struct hp_82341_priv *priv = board->private_data;

	tms9914_parallel_poll_response(board, &priv->tms9914_priv, ist);
}

static void hp_82341_serial_poll_response(struct gpib_board *board, u8 status)
{
	struct hp_82341_priv *priv = board->private_data;

	tms9914_serial_poll_response(board, &priv->tms9914_priv, status);
}

static u8 hp_82341_serial_poll_status(struct gpib_board *board)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_serial_poll_status(board, &priv->tms9914_priv);
}

static int hp_82341_line_status(const struct gpib_board *board)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_line_status(board, &priv->tms9914_priv);
}

static int hp_82341_t1_delay(struct gpib_board *board, unsigned int nano_sec)
{
	struct hp_82341_priv *priv = board->private_data;

	return tms9914_t1_delay(board, &priv->tms9914_priv, nano_sec);
}

static void hp_82341_return_to_local(struct gpib_board *board)
{
	struct hp_82341_priv *priv = board->private_data;

	tms9914_return_to_local(board, &priv->tms9914_priv);
}

static struct gpib_interface hp_82341_unaccel_interface = {
	.name = "hp_82341_unaccel",
	.attach = hp_82341_attach,
	.detach = hp_82341_detach,
	.read = hp_82341_read,
	.write = hp_82341_write,
	.command = hp_82341_command,
	.request_system_control = hp_82341_request_system_control,
	.take_control = hp_82341_take_control,
	.go_to_standby = hp_82341_go_to_standby,
	.interface_clear = hp_82341_interface_clear,
	.remote_enable = hp_82341_remote_enable,
	.enable_eos = hp_82341_enable_eos,
	.disable_eos = hp_82341_disable_eos,
	.parallel_poll = hp_82341_parallel_poll,
	.parallel_poll_configure = hp_82341_parallel_poll_configure,
	.parallel_poll_response = hp_82341_parallel_poll_response,
	.local_parallel_poll_mode = NULL, // XXX
	.line_status = hp_82341_line_status,
	.update_status = hp_82341_update_status,
	.primary_address = hp_82341_primary_address,
	.secondary_address = hp_82341_secondary_address,
	.serial_poll_response = hp_82341_serial_poll_response,
	.serial_poll_status = hp_82341_serial_poll_status,
	.t1_delay = hp_82341_t1_delay,
	.return_to_local = hp_82341_return_to_local,
};

static struct gpib_interface hp_82341_interface = {
	.name = "hp_82341",
	.attach = hp_82341_attach,
	.detach = hp_82341_detach,
	.read = hp_82341_accel_read,
	.write = hp_82341_accel_write,
	.command = hp_82341_command,
	.request_system_control = hp_82341_request_system_control,
	.take_control = hp_82341_take_control,
	.go_to_standby = hp_82341_go_to_standby,
	.interface_clear = hp_82341_interface_clear,
	.remote_enable = hp_82341_remote_enable,
	.enable_eos = hp_82341_enable_eos,
	.disable_eos = hp_82341_disable_eos,
	.parallel_poll = hp_82341_parallel_poll,
	.parallel_poll_configure = hp_82341_parallel_poll_configure,
	.parallel_poll_response = hp_82341_parallel_poll_response,
	.local_parallel_poll_mode = NULL, // XXX
	.line_status = hp_82341_line_status,
	.update_status = hp_82341_update_status,
	.primary_address = hp_82341_primary_address,
	.secondary_address = hp_82341_secondary_address,
	.serial_poll_response = hp_82341_serial_poll_response,
	.t1_delay = hp_82341_t1_delay,
	.return_to_local = hp_82341_return_to_local,
};

static int hp_82341_allocate_private(struct gpib_board *board)
{
	board->private_data = kzalloc(sizeof(struct hp_82341_priv), GFP_KERNEL);
	if (!board->private_data)
		return -ENOMEM;
	return 0;
}

static void hp_82341_free_private(struct gpib_board *board)
{
	kfree(board->private_data);
	board->private_data = NULL;
}

static u8 hp_82341_read_byte(struct tms9914_priv *priv, unsigned int register_num)
{
	return inb(priv->iobase + register_num);
}

static void hp_82341_write_byte(struct tms9914_priv *priv, u8 data, unsigned int register_num)
{
	outb(data, priv->iobase + register_num);
}

static int hp_82341_find_isapnp_board(struct pnp_dev **dev)
{
	*dev = pnp_find_dev(NULL, ISAPNP_VENDOR('H', 'W', 'P'),
			    ISAPNP_FUNCTION(0x1411), NULL);
	if (!*dev || !(*dev)->card) {
		pr_err("failed to find isapnp board\n");
		return -ENODEV;
	}
	if (pnp_device_attach(*dev) < 0) {
		pr_err("board already active, skipping\n");
		return -EBUSY;
	}
	if (pnp_activate_dev(*dev) < 0) {
		pnp_device_detach(*dev);
		pr_err("failed to activate(), aborting\n");
		return -EAGAIN;
	}
	if (!pnp_port_valid(*dev, 0) || !pnp_irq_valid(*dev, 0)) {
		pnp_device_detach(*dev);
		pr_err("invalid port or irq, aborting\n");
		return -ENOMEM;
	}
	return 0;
}

static int xilinx_ready(struct hp_82341_priv *hp_priv)
{
	switch (hp_priv->hw_version) {
	case HW_VERSION_82341C:
		if (inb(hp_priv->iobase[0] + CONFIG_CONTROL_STATUS_REG) & XILINX_READY_BIT)
			return 1;
		else
			return 0;
		break;
	case HW_VERSION_82341D:
		if (isapnp_read_byte(PIO_DATA_REG) & HP_82341D_XILINX_READY_BIT)
			return 1;
		else
			return 0;
	default:
		pr_err("bug! unknown hw_version\n");
		break;
	}
	return 0;
}

static int xilinx_done(struct hp_82341_priv *hp_priv)
{
	switch (hp_priv->hw_version) {
	case HW_VERSION_82341C:
		if (inb(hp_priv->iobase[0] + CONFIG_CONTROL_STATUS_REG) & DONE_PGL_BIT)
			return 1;
		else
			return 0;
	case HW_VERSION_82341D:
		if (isapnp_read_byte(PIO_DATA_REG) & HP_82341D_XILINX_DONE_BIT)
			return 1;
		else
			return 0;
	default:
		pr_err("bug! unknown hw_version\n");
		break;
	}
	return 0;
}

static int irq_valid(struct hp_82341_priv *hp_priv, int irq)
{
	switch (hp_priv->hw_version) {
	case HW_VERSION_82341C:
		switch (irq) {
		case 3:
		case 5:
		case 7:
		case 9:
		case 10:
		case 11:
		case 12:
		case 15:
			return 1;
		default:
			pr_err("invalid irq=%i for 82341C, irq must be 3, 5, 7, 9, 10, 11, 12, or 15.\n",
			       irq);
			return 0;
		}
		break;
	case HW_VERSION_82341D:
		return 1;
	default:
		pr_err("bug! unknown hw_version\n");
		break;
	}
	return 0;
}

static int hp_82341_load_firmware_array(struct hp_82341_priv *hp_priv,
					const unsigned char *firmware_data,
					unsigned int firmware_length)
{
	int i, j;
	static const int timeout = 100;

	for (i = 0; i < firmware_length; ++i) {
		for (j = 0; j < timeout; ++j) {
			if (need_resched())
				schedule();
			if (xilinx_ready(hp_priv))
				break;
			usleep_range(10, 15);
		}
		if (j == timeout) {
			pr_err("timed out waiting for Xilinx ready.\n");
			return -ETIMEDOUT;
		}
		outb(firmware_data[i], hp_priv->iobase[0] + XILINX_DATA_REG);
	}
	for (j = 0; j < timeout; ++j) {
		if (xilinx_done(hp_priv))
			break;
		if (need_resched())
			schedule();
		usleep_range(10, 15);
	}
	if (j == timeout) {
		pr_err("timed out waiting for Xilinx done.\n");
		return -ETIMEDOUT;
	}
	return 0;
}

static int hp_82341_load_firmware(struct hp_82341_priv *hp_priv,
				  const struct gpib_board_config *config)
{
	if (config->init_data_length == 0) {
		if (xilinx_done(hp_priv))
			return 0;
		pr_err("board needs be initialized with firmware upload.\n"
		       "\tUse the --init-data option of gpib_config.\n");
		return -EINVAL;
	}
	switch (hp_priv->hw_version) {
	case HW_VERSION_82341C:
		if (config->init_data_length != hp_82341c_firmware_length) {
			pr_err("bad firmware length=%i for 82341c (expected %i).\n",
			       config->init_data_length, hp_82341c_firmware_length);
			return -EINVAL;
		}
		break;
	case HW_VERSION_82341D:
		if (config->init_data_length != hp_82341d_firmware_length) {
			pr_err("bad firmware length=%i for 82341d (expected %i).\n",
			       config->init_data_length, hp_82341d_firmware_length);
			return -EINVAL;
		}
		break;
	default:
		pr_err("bug! unknown hw_version\n");
		break;
	}
	return hp_82341_load_firmware_array(hp_priv, config->init_data, config->init_data_length);
}

static void set_xilinx_not_prog(struct hp_82341_priv *hp_priv, int assert)
{
	switch (hp_priv->hw_version) {
	case HW_VERSION_82341C:
		if (assert)
			hp_priv->config_control_bits |= DONE_PGL_BIT;
		else
			hp_priv->config_control_bits &= ~DONE_PGL_BIT;
		outb(hp_priv->config_control_bits, hp_priv->iobase[0] + CONFIG_CONTROL_STATUS_REG);
		break;
	case HW_VERSION_82341D:
		if (assert)
			isapnp_write_byte(PIO_DATA_REG, HP_82341D_NOT_PROG_BIT);
		else
			isapnp_write_byte(PIO_DATA_REG, 0x0);
		break;
	default:
		break;
	}
}

// clear xilinx firmware
static int clear_xilinx(struct hp_82341_priv *hp_priv)
{
	set_xilinx_not_prog(hp_priv, 1);
	if (msleep_interruptible(1))
		return -EINTR;
	set_xilinx_not_prog(hp_priv, 0);
	if (msleep_interruptible(1))
		return -EINTR;
	set_xilinx_not_prog(hp_priv, 1);
	if (msleep_interruptible(1))
		return -EINTR;
	return 0;
}

static int hp_82341_attach(struct gpib_board *board, const struct gpib_board_config *config)
{
	struct hp_82341_priv *hp_priv;
	struct tms9914_priv *tms_priv;
	u32 start_addr;
	u32 iobase;
	int irq;
	int i;
	int retval;

	board->status = 0;
	if (hp_82341_allocate_private(board))
		return -ENOMEM;
	hp_priv = board->private_data;
	tms_priv = &hp_priv->tms9914_priv;
	tms_priv->read_byte = hp_82341_read_byte;
	tms_priv->write_byte = hp_82341_write_byte;
	tms_priv->offset = 1;

	if (config->ibbase == 0) {
		struct pnp_dev *dev;
		int retval = hp_82341_find_isapnp_board(&dev);

		if (retval < 0)
			return retval;
		hp_priv->pnp_dev = dev;
		iobase = pnp_port_start(dev, 0);
		irq = pnp_irq(dev, 0);
		hp_priv->hw_version = HW_VERSION_82341D;
		hp_priv->io_region_offset = 0x8;
	} else {
		iobase = config->ibbase;
		irq = config->ibirq;
		hp_priv->hw_version = HW_VERSION_82341C;
		hp_priv->io_region_offset = 0x400;
	}
	for (i = 0; i < hp_82341_num_io_regions; ++i) {
		start_addr = iobase + i * hp_priv->io_region_offset;
		if (!request_region(start_addr, hp_82341_region_iosize, DRV_NAME)) {
			dev_err(board->gpib_dev, "failed to allocate io ports 0x%x-0x%x\n",
				start_addr,
				start_addr + hp_82341_region_iosize - 1);
			return -EIO;
		}
		hp_priv->iobase[i] = start_addr;
	}
	tms_priv->iobase = hp_priv->iobase[2];
	if (hp_priv->hw_version == HW_VERSION_82341D) {
		retval = isapnp_cfg_begin(hp_priv->pnp_dev->card->number,
					  hp_priv->pnp_dev->number);
		if (retval < 0)	{
			dev_err(board->gpib_dev, "isapnp_cfg_begin returned error\n");
			return retval;
		}
		isapnp_write_byte(PIO_DIRECTION_REG, HP_82341D_XILINX_READY_BIT |
				  HP_82341D_XILINX_DONE_BIT);
	}
	retval = clear_xilinx(hp_priv);
	if (retval < 0)
		return retval;
	retval = hp_82341_load_firmware(hp_priv, config);
	if (hp_priv->hw_version == HW_VERSION_82341D)
		isapnp_cfg_end();
	if (retval < 0)
		return retval;
	if (irq_valid(hp_priv, irq) == 0)
		return -EINVAL;
	if (request_irq(irq, hp_82341_interrupt, 0, DRV_NAME, board))	{
		dev_err(board->gpib_dev, "failed to allocate IRQ %d\n", irq);
		return -EIO;
	}
	hp_priv->irq = irq;
	hp_priv->config_control_bits &= ~IRQ_SELECT_MASK;
	hp_priv->config_control_bits |= IRQ_SELECT_BITS(irq);
	outb(hp_priv->config_control_bits, hp_priv->iobase[0] + CONFIG_CONTROL_STATUS_REG);
	hp_priv->mode_control_bits |= ENABLE_IRQ_CONFIG_BIT;
	outb(hp_priv->mode_control_bits, hp_priv->iobase[0] + MODE_CONTROL_STATUS_REG);
	tms9914_board_reset(tms_priv);
	outb(ENABLE_BUFFER_END_EVENT_BIT | ENABLE_TERMINAL_COUNT_EVENT_BIT |
	     ENABLE_TI_INTERRUPT_EVENT_BIT, hp_priv->iobase[0] +  EVENT_ENABLE_REG);
	outb(ENABLE_BUFFER_END_INTERRUPT_BIT | ENABLE_TERMINAL_COUNT_INTERRUPT_BIT |
	     ENABLE_TI_INTERRUPT_BIT, hp_priv->iobase[0] + INTERRUPT_ENABLE_REG);
	// write clear event register
	outb((TI_INTERRUPT_EVENT_BIT | POINTERS_EQUAL_EVENT_BIT |
	      BUFFER_END_EVENT_BIT | TERMINAL_COUNT_EVENT_BIT),
	     hp_priv->iobase[0] + EVENT_STATUS_REG);

	tms9914_online(board, tms_priv);

	return 0;
}

static void hp_82341_detach(struct gpib_board *board)
{
	struct hp_82341_priv *hp_priv = board->private_data;
	struct tms9914_priv *tms_priv;
	int i;

	if (hp_priv) {
		tms_priv = &hp_priv->tms9914_priv;
		if (hp_priv->iobase[0])	{
			outb(0, hp_priv->iobase[0] + INTERRUPT_ENABLE_REG);
			if (tms_priv->iobase)
				tms9914_board_reset(tms_priv);
			if (hp_priv->irq)
				free_irq(hp_priv->irq, board);
		}
		for (i = 0; i < hp_82341_num_io_regions; ++i) {
			if (hp_priv->iobase[i])
				release_region(hp_priv->iobase[i], hp_82341_region_iosize);
		}
		if (hp_priv->pnp_dev)
			pnp_device_detach(hp_priv->pnp_dev);
	}
	hp_82341_free_private(board);
}

#if 0
/* unused, will be needed when the driver is turned into a pnp_driver */
static const struct pnp_device_id hp_82341_pnp_table[] = {
	{.id = "HWP1411"},
	{.id = ""}
};
MODULE_DEVICE_TABLE(pnp, hp_82341_pnp_table);
#endif

static int __init hp_82341_init_module(void)
{
	int ret;

	ret = gpib_register_driver(&hp_82341_unaccel_interface, THIS_MODULE);
	if (ret) {
		pr_err("gpib_register_driver failed: error = %d\n", ret);
		return ret;
	}

	ret = gpib_register_driver(&hp_82341_interface, THIS_MODULE);
	if (ret) {
		pr_err("gpib_register_driver failed: error = %d\n", ret);
		gpib_unregister_driver(&hp_82341_unaccel_interface);
		return ret;
	}

	return 0;
}

static void __exit hp_82341_exit_module(void)
{
	gpib_unregister_driver(&hp_82341_interface);
	gpib_unregister_driver(&hp_82341_unaccel_interface);
}

module_init(hp_82341_init_module);
module_exit(hp_82341_exit_module);

/*
 * GPIB interrupt service routines
 */
static unsigned short read_and_clear_event_status(struct gpib_board *board)
{
	struct hp_82341_priv *hp_priv = board->private_data;
	unsigned long flags;
	unsigned short status;

	spin_lock_irqsave(&board->spinlock, flags);
	status = hp_priv->event_status_bits;
	hp_priv->event_status_bits = 0;
	spin_unlock_irqrestore(&board->spinlock, flags);
	return status;
}

static irqreturn_t hp_82341_interrupt(int irq, void *arg)
{
	int status1, status2;
	struct gpib_board *board = arg;
	struct hp_82341_priv *hp_priv = board->private_data;
	struct tms9914_priv *tms_priv = &hp_priv->tms9914_priv;
	unsigned long flags;
	irqreturn_t retval = IRQ_NONE;
	int event_status;

	spin_lock_irqsave(&board->spinlock, flags);
	event_status = inb(hp_priv->iobase[0] + EVENT_STATUS_REG);
	if (event_status & INTERRUPT_PENDING_EVENT_BIT)
		retval = IRQ_HANDLED;
	// write-clear status bits
	if (event_status & (TI_INTERRUPT_EVENT_BIT | POINTERS_EQUAL_EVENT_BIT |
			    BUFFER_END_EVENT_BIT | TERMINAL_COUNT_EVENT_BIT)) {
		outb(event_status & (TI_INTERRUPT_EVENT_BIT | POINTERS_EQUAL_EVENT_BIT |
				     BUFFER_END_EVENT_BIT | TERMINAL_COUNT_EVENT_BIT),
		     hp_priv->iobase[0] + EVENT_STATUS_REG);
		hp_priv->event_status_bits |= event_status;
	}
	if (event_status & TI_INTERRUPT_EVENT_BIT) {
		status1 = read_byte(tms_priv, ISR0);
		status2 = read_byte(tms_priv, ISR1);
		tms9914_interrupt_have_status(board, tms_priv, status1, status2);
	}
	spin_unlock_irqrestore(&board->spinlock, flags);
	return retval;
}

static int read_transfer_counter(struct hp_82341_priv *hp_priv)
{
	int lo, mid, value;

	lo = inb(hp_priv->iobase[1] + TRANSFER_COUNT_LOW_REG);
	mid = inb(hp_priv->iobase[1] + TRANSFER_COUNT_MID_REG);
	value = (lo & 0xff) | ((mid << 8) & 0x7f00);
	value = ~(value - 1) & 0x7fff;
	return value;
}

static void set_transfer_counter(struct hp_82341_priv *hp_priv, int count)
{
	int complement = -count;

	outb(complement & 0xff, hp_priv->iobase[1] + TRANSFER_COUNT_LOW_REG);
	outb((complement >> 8) & 0xff, hp_priv->iobase[1] + TRANSFER_COUNT_MID_REG);
	// I don't think the hi count reg is even used, but oh well
	outb((complement >> 16) & 0xf, hp_priv->iobase[1] + TRANSFER_COUNT_HIGH_REG);
}