1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  * National Instruments boards using tnt4882 or compatible chips (at-gpib, etc).
5  *    copyright            : (C) 2001, 2002 by Frank Mori Hess
6  ***************************************************************************/
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #define dev_fmt pr_fmt
10 #define DRV_NAME KBUILD_MODNAME
11 
12 #include <linux/ioport.h>
13 #include <linux/sched.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/pci.h>
17 #include <linux/pci_ids.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/isapnp.h>
22 
23 #include "nec7210.h"
24 #include "gpibP.h"
25 #include "mite.h"
26 #include "tnt4882_registers.h"
27 
28 static const int ISAPNP_VENDOR_ID_NI = ISAPNP_VENDOR('N', 'I', 'C');
29 static const int ISAPNP_ID_NI_ATGPIB_TNT = 0xc601;
30 enum {
31 	PCI_DEVICE_ID_NI_GPIB = 0xc801,
32 	PCI_DEVICE_ID_NI_GPIB_PLUS = 0xc811,
33 	PCI_DEVICE_ID_NI_GPIB_PLUS2 = 0x71ad,
34 	PCI_DEVICE_ID_NI_PXIGPIB = 0xc821,
35 	PCI_DEVICE_ID_NI_PMCGPIB = 0xc831,
36 	PCI_DEVICE_ID_NI_PCIEGPIB = 0x70cf,
37 	PCI_DEVICE_ID_NI_PCIE2GPIB = 0x710e,
38 // Measurement Computing PCI-488 same design as PCI-GPIB with TNT5004
39 	PCI_DEVICE_ID_MC_PCI488 = 0x7259,
40 	PCI_DEVICE_ID_CEC_NI_GPIB = 0x7258
41 };
42 
43 // struct which defines private_data for tnt4882 devices
44 struct tnt4882_priv {
45 	struct nec7210_priv nec7210_priv;
46 	struct mite_struct *mite;
47 	struct pnp_dev *pnp_dev;
48 	unsigned int irq;
49 	unsigned short imr0_bits;
50 	unsigned short imr3_bits;
51 	unsigned short auxg_bits;	// bits written to auxiliary register G
52 };
53 
54 static irqreturn_t tnt4882_internal_interrupt(struct gpib_board *board);
55 
56 // register offset for nec7210 compatible registers
57 static const int atgpib_reg_offset = 2;
58 
59 // number of ioports used
60 static const int atgpib_iosize = 32;
61 
62 /* paged io */
tnt_paged_readb(struct tnt4882_priv * priv,unsigned long offset)63 static inline unsigned int tnt_paged_readb(struct tnt4882_priv *priv, unsigned long offset)
64 {
65 	iowrite8(AUX_PAGEIN, priv->nec7210_priv.mmiobase + AUXMR * priv->nec7210_priv.offset);
66 	udelay(1);
67 	return ioread8(priv->nec7210_priv.mmiobase + offset);
68 }
69 
tnt_paged_writeb(struct tnt4882_priv * priv,unsigned int value,unsigned long offset)70 static inline void tnt_paged_writeb(struct tnt4882_priv *priv, unsigned int value,
71 				    unsigned long offset)
72 {
73 	iowrite8(AUX_PAGEIN, priv->nec7210_priv.mmiobase + AUXMR * priv->nec7210_priv.offset);
74 	udelay(1);
75 	iowrite8(value, priv->nec7210_priv.mmiobase + offset);
76 }
77 
78 /* readb/writeb wrappers */
tnt_readb(struct tnt4882_priv * priv,unsigned long offset)79 static inline unsigned short tnt_readb(struct tnt4882_priv *priv, unsigned long offset)
80 {
81 	void __iomem *address = priv->nec7210_priv.mmiobase + offset;
82 	unsigned long flags;
83 	unsigned short retval;
84 	spinlock_t *register_lock = &priv->nec7210_priv.register_page_lock;
85 
86 	spin_lock_irqsave(register_lock, flags);
87 	switch (offset) {
88 	case CSR:
89 	case SASR:
90 	case ISR0:
91 	case BSR:
92 		switch (priv->nec7210_priv.type) {
93 		case TNT4882:
94 		case TNT5004:
95 			retval = ioread8(address);
96 			break;
97 		case NAT4882:
98 			retval = tnt_paged_readb(priv, offset - tnt_pagein_offset);
99 			break;
100 		case NEC7210:
101 			retval = 0;
102 			break;
103 		default:
104 			retval = 0;
105 			break;
106 		}
107 		break;
108 	default:
109 		retval = ioread8(address);
110 		break;
111 	}
112 	spin_unlock_irqrestore(register_lock, flags);
113 	return retval;
114 }
115 
tnt_writeb(struct tnt4882_priv * priv,unsigned short value,unsigned long offset)116 static inline void tnt_writeb(struct tnt4882_priv *priv, unsigned short value, unsigned long offset)
117 {
118 	void __iomem *address = priv->nec7210_priv.mmiobase + offset;
119 	unsigned long flags;
120 	spinlock_t *register_lock = &priv->nec7210_priv.register_page_lock;
121 
122 	spin_lock_irqsave(register_lock, flags);
123 	switch (offset)	{
124 	case KEYREG:
125 	case IMR0:
126 	case BCR:
127 		switch (priv->nec7210_priv.type) {
128 		case TNT4882:
129 		case TNT5004:
130 			iowrite8(value, address);
131 			break;
132 		case NAT4882:
133 			tnt_paged_writeb(priv, value, offset - tnt_pagein_offset);
134 			break;
135 		case NEC7210:
136 			break;
137 		default:
138 			break;
139 		}
140 		break;
141 	default:
142 		iowrite8(value, address);
143 		break;
144 	}
145 	spin_unlock_irqrestore(register_lock, flags);
146 }
147 
148 MODULE_LICENSE("GPL");
149 MODULE_DESCRIPTION("GPIB driver for National Instruments boards using tnt4882 or compatible chips");
150 
tnt4882_line_status(const struct gpib_board * board)151 static int tnt4882_line_status(const struct gpib_board *board)
152 {
153 	int status = VALID_ALL;
154 	int bcsr_bits;
155 	struct tnt4882_priv *tnt_priv;
156 
157 	tnt_priv = board->private_data;
158 
159 	bcsr_bits = tnt_readb(tnt_priv, BSR);
160 
161 	if (bcsr_bits & BCSR_REN_BIT)
162 		status |= BUS_REN;
163 	if (bcsr_bits & BCSR_IFC_BIT)
164 		status |= BUS_IFC;
165 	if (bcsr_bits & BCSR_SRQ_BIT)
166 		status |= BUS_SRQ;
167 	if (bcsr_bits & BCSR_EOI_BIT)
168 		status |= BUS_EOI;
169 	if (bcsr_bits & BCSR_NRFD_BIT)
170 		status |= BUS_NRFD;
171 	if (bcsr_bits & BCSR_NDAC_BIT)
172 		status |= BUS_NDAC;
173 	if (bcsr_bits & BCSR_DAV_BIT)
174 		status |= BUS_DAV;
175 	if (bcsr_bits & BCSR_ATN_BIT)
176 		status |= BUS_ATN;
177 
178 	return status;
179 }
180 
tnt4882_t1_delay(struct gpib_board * board,unsigned int nano_sec)181 static int tnt4882_t1_delay(struct gpib_board *board, unsigned int nano_sec)
182 {
183 	struct tnt4882_priv *tnt_priv = board->private_data;
184 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
185 	unsigned int retval;
186 
187 	retval = nec7210_t1_delay(board, nec_priv, nano_sec);
188 	if (nec_priv->type == NEC7210)
189 		return retval;
190 
191 	if (nano_sec <= 350) {
192 		tnt_writeb(tnt_priv, MSTD, KEYREG);
193 		retval = 350;
194 	} else {
195 		tnt_writeb(tnt_priv, 0, KEYREG);
196 	}
197 	if (nano_sec > 500 && nano_sec <= 1100)	{
198 		write_byte(nec_priv, AUXRI | USTD, AUXMR);
199 		retval = 1100;
200 	} else {
201 		write_byte(nec_priv, AUXRI, AUXMR);
202 	}
203 	return retval;
204 }
205 
fifo_word_available(struct tnt4882_priv * tnt_priv)206 static int fifo_word_available(struct tnt4882_priv *tnt_priv)
207 {
208 	int status2;
209 	int retval;
210 
211 	status2 = tnt_readb(tnt_priv, STS2);
212 	retval = (status2 & AEFN) && (status2 & BEFN);
213 
214 	return retval;
215 }
216 
fifo_byte_available(struct tnt4882_priv * tnt_priv)217 static int fifo_byte_available(struct tnt4882_priv *tnt_priv)
218 {
219 	int status2;
220 	int retval;
221 
222 	status2 = tnt_readb(tnt_priv, STS2);
223 	retval = (status2 & AEFN) || (status2 & BEFN);
224 
225 	return retval;
226 }
227 
fifo_xfer_done(struct tnt4882_priv * tnt_priv)228 static int fifo_xfer_done(struct tnt4882_priv *tnt_priv)
229 {
230 	int status1;
231 	int retval;
232 
233 	status1 = tnt_readb(tnt_priv, STS1);
234 	retval = status1 & (S_DONE | S_HALT);
235 
236 	return retval;
237 }
238 
drain_fifo_words(struct tnt4882_priv * tnt_priv,uint8_t * buffer,int num_bytes)239 static int drain_fifo_words(struct tnt4882_priv *tnt_priv, uint8_t *buffer, int num_bytes)
240 {
241 	int count = 0;
242 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
243 
244 	while (fifo_word_available(tnt_priv) && count + 2 <= num_bytes)	{
245 		short word;
246 
247 		word = ioread16(nec_priv->mmiobase + FIFOB);
248 		buffer[count++] = word & 0xff;
249 		buffer[count++] = (word >> 8) & 0xff;
250 	}
251 	return count;
252 }
253 
tnt4882_release_holdoff(struct gpib_board * board,struct tnt4882_priv * tnt_priv)254 static void tnt4882_release_holdoff(struct gpib_board *board, struct tnt4882_priv *tnt_priv)
255 {
256 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
257 	unsigned short sasr_bits;
258 
259 	sasr_bits = tnt_readb(tnt_priv, SASR);
260 
261 	/*tnt4882 not in one-chip mode won't always release holdoff unless we
262 	 * are in the right mode when release handshake command is given
263 	 */
264 	if (sasr_bits & AEHS_BIT) /* holding off due to holdoff on end mode*/	{
265 		nec7210_set_handshake_mode(board, nec_priv, HR_HLDE);
266 		write_byte(nec_priv, AUX_FH, AUXMR);
267 	} else if (sasr_bits & ANHS1_BIT) { /* held off due to holdoff on all data mode*/
268 		nec7210_set_handshake_mode(board, nec_priv, HR_HLDA);
269 		write_byte(nec_priv, AUX_FH, AUXMR);
270 		nec7210_set_handshake_mode(board, nec_priv, HR_HLDE);
271 	} else { /* held off due to holdoff immediately command */
272 		nec7210_set_handshake_mode(board, nec_priv, HR_HLDE);
273 		write_byte(nec_priv, AUX_FH, AUXMR);
274 	}
275 }
276 
tnt4882_accel_read(struct gpib_board * board,uint8_t * buffer,size_t length,int * end,size_t * bytes_read)277 static int tnt4882_accel_read(struct gpib_board *board, uint8_t *buffer, size_t length, int *end,
278 			      size_t *bytes_read)
279 {
280 	size_t count = 0;
281 	ssize_t retval = 0;
282 	struct tnt4882_priv *tnt_priv = board->private_data;
283 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
284 	unsigned int bits;
285 	s32 hw_count;
286 	unsigned long flags;
287 
288 	*bytes_read = 0;
289 	// FIXME: really, DEV_CLEAR_BN should happen elsewhere to prevent race
290 	clear_bit(DEV_CLEAR_BN, &nec_priv->state);
291 	clear_bit(ADR_CHANGE_BN, &nec_priv->state);
292 
293 	nec7210_set_reg_bits(nec_priv, IMR1, HR_ENDIE, HR_ENDIE);
294 	if (nec_priv->type != TNT4882 && nec_priv->type != TNT5004)
295 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, HR_DMAI);
296 	else
297 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
298 	tnt_writeb(tnt_priv, nec_priv->auxa_bits | HR_HLDA, CCR);
299 	bits = TNT_B_16BIT | TNT_IN | TNT_CCEN;
300 	tnt_writeb(tnt_priv, bits, CFG);
301 	tnt_writeb(tnt_priv, RESET_FIFO, CMDR);
302 	udelay(1);
303 	// load 2's complement of count into hardware counters
304 	hw_count = -length;
305 	tnt_writeb(tnt_priv, hw_count & 0xff, CNT0);
306 	tnt_writeb(tnt_priv, (hw_count >> 8) & 0xff, CNT1);
307 	tnt_writeb(tnt_priv, (hw_count >> 16) & 0xff, CNT2);
308 	tnt_writeb(tnt_priv, (hw_count >> 24) & 0xff, CNT3);
309 
310 	tnt4882_release_holdoff(board, tnt_priv);
311 
312 	tnt_writeb(tnt_priv, GO, CMDR);
313 	udelay(1);
314 
315 	spin_lock_irqsave(&board->spinlock, flags);
316 	tnt_priv->imr3_bits |= HR_DONE | HR_NEF;
317 	tnt_writeb(tnt_priv, tnt_priv->imr3_bits, IMR3);
318 	spin_unlock_irqrestore(&board->spinlock, flags);
319 
320 	while (count + 2 <= length &&
321 	       test_bit(RECEIVED_END_BN, &nec_priv->state) == 0 &&
322 	       fifo_xfer_done(tnt_priv) == 0) {
323 		// wait until a word is ready
324 		if (wait_event_interruptible(board->wait,
325 					     fifo_word_available(tnt_priv) ||
326 					     fifo_xfer_done(tnt_priv) ||
327 					     test_bit(RECEIVED_END_BN, &nec_priv->state) ||
328 					     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
329 					     test_bit(ADR_CHANGE_BN, &nec_priv->state) ||
330 					     test_bit(TIMO_NUM, &board->status))) {
331 			retval = -ERESTARTSYS;
332 			break;
333 		}
334 		if (test_bit(TIMO_NUM, &board->status))	{
335 			retval = -ETIMEDOUT;
336 			break;
337 		}
338 		if (test_bit(DEV_CLEAR_BN, &nec_priv->state)) {
339 			retval = -EINTR;
340 			break;
341 		}
342 		if (test_bit(ADR_CHANGE_BN, &nec_priv->state)) {
343 			retval = -EINTR;
344 			break;
345 		}
346 
347 		spin_lock_irqsave(&board->spinlock, flags);
348 		count += drain_fifo_words(tnt_priv, &buffer[count], length - count);
349 		tnt_priv->imr3_bits |= HR_NEF;
350 		tnt_writeb(tnt_priv, tnt_priv->imr3_bits, IMR3);
351 		spin_unlock_irqrestore(&board->spinlock, flags);
352 
353 		if (need_resched())
354 			schedule();
355 	}
356 	// wait for last byte
357 	if (count < length) {
358 		spin_lock_irqsave(&board->spinlock, flags);
359 		tnt_priv->imr3_bits |= HR_DONE | HR_NEF;
360 		tnt_writeb(tnt_priv, tnt_priv->imr3_bits, IMR3);
361 		spin_unlock_irqrestore(&board->spinlock, flags);
362 
363 		if (wait_event_interruptible(board->wait,
364 					     fifo_xfer_done(tnt_priv) ||
365 					     test_bit(RECEIVED_END_BN, &nec_priv->state) ||
366 					     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
367 					     test_bit(ADR_CHANGE_BN, &nec_priv->state) ||
368 					     test_bit(TIMO_NUM, &board->status))) {
369 			retval = -ERESTARTSYS;
370 		}
371 		if (test_bit(TIMO_NUM, &board->status))
372 			retval = -ETIMEDOUT;
373 		if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
374 			retval = -EINTR;
375 		if (test_bit(ADR_CHANGE_BN, &nec_priv->state))
376 			retval = -EINTR;
377 		count += drain_fifo_words(tnt_priv, &buffer[count], length - count);
378 		if (fifo_byte_available(tnt_priv) && count < length)
379 			buffer[count++] = tnt_readb(tnt_priv, FIFOB);
380 	}
381 	if (count < length)
382 		tnt_writeb(tnt_priv, STOP, CMDR);
383 	udelay(1);
384 
385 	nec7210_set_reg_bits(nec_priv, IMR1, HR_ENDIE, 0);
386 	nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
387 	/* force handling of any pending interrupts (seems to be needed
388 	 * to keep interrupts from getting hosed, plus for syncing
389 	 * with RECEIVED_END below)
390 	 */
391 	tnt4882_internal_interrupt(board);
392 	/* RECEIVED_END should be in sync now */
393 	if (test_and_clear_bit(RECEIVED_END_BN, &nec_priv->state))
394 		*end = 1;
395 	if (retval < 0)	{
396 		// force immediate holdoff
397 		write_byte(nec_priv, AUX_HLDI, AUXMR);
398 
399 		set_bit(RFD_HOLDOFF_BN, &nec_priv->state);
400 	}
401 	*bytes_read = count;
402 
403 	return retval;
404 }
405 
fifo_space_available(struct tnt4882_priv * tnt_priv)406 static int fifo_space_available(struct tnt4882_priv *tnt_priv)
407 {
408 	int status2;
409 	int retval;
410 
411 	status2 = tnt_readb(tnt_priv, STS2);
412 	retval = (status2 & AFFN) && (status2 & BFFN);
413 
414 	return retval;
415 }
416 
tnt_transfer_count(struct tnt4882_priv * tnt_priv)417 static unsigned int tnt_transfer_count(struct tnt4882_priv *tnt_priv)
418 {
419 	unsigned int count = 0;
420 
421 	count |= tnt_readb(tnt_priv, CNT0) & 0xff;
422 	count |= (tnt_readb(tnt_priv, CNT1) << 8) & 0xff00;
423 	count |= (tnt_readb(tnt_priv, CNT2) << 16) & 0xff0000;
424 	count |= (tnt_readb(tnt_priv, CNT3) << 24) & 0xff000000;
425 	// return two's complement
426 	return -count;
427 };
428 
write_wait(struct gpib_board * board,struct tnt4882_priv * tnt_priv,int wait_for_done,int send_commands)429 static int write_wait(struct gpib_board *board, struct tnt4882_priv *tnt_priv,
430 		      int wait_for_done, int send_commands)
431 {
432 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
433 
434 	if (wait_event_interruptible(board->wait,
435 				     (!wait_for_done && fifo_space_available(tnt_priv)) ||
436 				     fifo_xfer_done(tnt_priv) ||
437 				     test_bit(BUS_ERROR_BN, &nec_priv->state) ||
438 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
439 				     test_bit(TIMO_NUM, &board->status)))
440 		return -ERESTARTSYS;
441 
442 	if (test_bit(TIMO_NUM, &board->status))
443 		return -ETIMEDOUT;
444 	if (test_and_clear_bit(BUS_ERROR_BN, &nec_priv->state))
445 		return (send_commands) ? -ENOTCONN : -ECOMM;
446 	if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
447 		return -EINTR;
448 	return 0;
449 }
450 
generic_write(struct gpib_board * board,uint8_t * buffer,size_t length,int send_eoi,int send_commands,size_t * bytes_written)451 static int generic_write(struct gpib_board *board, uint8_t *buffer, size_t length,
452 			 int send_eoi, int send_commands, size_t *bytes_written)
453 {
454 	size_t count = 0;
455 	ssize_t retval = 0;
456 	struct tnt4882_priv *tnt_priv = board->private_data;
457 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
458 	unsigned int bits;
459 	s32 hw_count;
460 	unsigned long flags;
461 
462 	*bytes_written = 0;
463 	// FIXME: really, DEV_CLEAR_BN should happen elsewhere to prevent race
464 	clear_bit(DEV_CLEAR_BN, &nec_priv->state);
465 
466 	nec7210_set_reg_bits(nec_priv, IMR1, HR_ERRIE, HR_ERRIE);
467 
468 	if (nec_priv->type != TNT4882 && nec_priv->type != TNT5004)
469 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, HR_DMAO);
470 	else
471 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, 0);
472 
473 	tnt_writeb(tnt_priv, RESET_FIFO, CMDR);
474 	udelay(1);
475 
476 	bits = TNT_B_16BIT;
477 	if (send_eoi) {
478 		bits |= TNT_CCEN;
479 		if (nec_priv->type != TNT4882 && nec_priv->type != TNT5004)
480 			tnt_writeb(tnt_priv, AUX_SEOI, CCR);
481 	}
482 	if (send_commands)
483 		bits |= TNT_COMMAND;
484 	tnt_writeb(tnt_priv, bits, CFG);
485 
486 	// load 2's complement of count into hardware counters
487 	hw_count = -length;
488 	tnt_writeb(tnt_priv, hw_count & 0xff, CNT0);
489 	tnt_writeb(tnt_priv, (hw_count >> 8) & 0xff, CNT1);
490 	tnt_writeb(tnt_priv, (hw_count >> 16) & 0xff, CNT2);
491 	tnt_writeb(tnt_priv, (hw_count >> 24) & 0xff, CNT3);
492 
493 	tnt_writeb(tnt_priv, GO, CMDR);
494 	udelay(1);
495 
496 	spin_lock_irqsave(&board->spinlock, flags);
497 	tnt_priv->imr3_bits |= HR_DONE;
498 	tnt_writeb(tnt_priv, tnt_priv->imr3_bits, IMR3);
499 	spin_unlock_irqrestore(&board->spinlock, flags);
500 
501 	while (count < length)	{
502 		// wait until byte is ready to be sent
503 		retval = write_wait(board, tnt_priv, 0, send_commands);
504 		if (retval < 0)
505 			break;
506 		if (fifo_xfer_done(tnt_priv))
507 			break;
508 		spin_lock_irqsave(&board->spinlock, flags);
509 		while (fifo_space_available(tnt_priv) && count < length) {
510 			u16 word;
511 
512 			word = buffer[count++] & 0xff;
513 			if (count < length)
514 				word |= (buffer[count++] << 8) & 0xff00;
515 			iowrite16(word, nec_priv->mmiobase + FIFOB);
516 		}
517 //  avoid unnecessary HR_NFF interrupts
518 //		tnt_priv->imr3_bits |= HR_NFF;
519 //		tnt_writeb(tnt_priv, tnt_priv->imr3_bits, IMR3);
520 		spin_unlock_irqrestore(&board->spinlock, flags);
521 
522 		if (need_resched())
523 			schedule();
524 	}
525 	// wait last byte has been sent
526 	if (retval == 0)
527 		retval = write_wait(board, tnt_priv, 1, send_commands);
528 
529 	tnt_writeb(tnt_priv, STOP, CMDR);
530 	udelay(1);
531 
532 	nec7210_set_reg_bits(nec_priv, IMR1, HR_ERR, 0x0);
533 	nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, 0x0);
534 	/* force handling of any interrupts that happened
535 	 * while they were masked (this appears to be needed)
536 	 */
537 	tnt4882_internal_interrupt(board);
538 	*bytes_written = length - tnt_transfer_count(tnt_priv);
539 	return retval;
540 }
541 
tnt4882_accel_write(struct gpib_board * board,uint8_t * buffer,size_t length,int send_eoi,size_t * bytes_written)542 static int tnt4882_accel_write(struct gpib_board *board, uint8_t *buffer, size_t length, int send_eoi,
543 			       size_t *bytes_written)
544 {
545 	return generic_write(board, buffer, length, send_eoi, 0, bytes_written);
546 }
547 
tnt4882_command(struct gpib_board * board,uint8_t * buffer,size_t length,size_t * bytes_written)548 static int tnt4882_command(struct gpib_board *board, uint8_t *buffer, size_t length,
549 			   size_t *bytes_written)
550 {
551 	return generic_write(board, buffer, length, 0, 1, bytes_written);
552 }
553 
tnt4882_internal_interrupt(struct gpib_board * board)554 static irqreturn_t tnt4882_internal_interrupt(struct gpib_board *board)
555 {
556 	struct tnt4882_priv *priv = board->private_data;
557 	int isr0_bits, isr3_bits, imr3_bits;
558 	unsigned long flags;
559 
560 	spin_lock_irqsave(&board->spinlock, flags);
561 
562 	nec7210_interrupt(board, &priv->nec7210_priv);
563 
564 	isr0_bits = tnt_readb(priv, ISR0);
565 	isr3_bits = tnt_readb(priv, ISR3);
566 	imr3_bits = priv->imr3_bits;
567 
568 	if (isr0_bits & TNT_IFCI_BIT)
569 		push_gpib_event(board, EventIFC);
570 	//XXX don't need this wakeup, one below should do?
571 //		wake_up_interruptible(&board->wait);
572 
573 	if (isr3_bits & HR_NFF)
574 		priv->imr3_bits &= ~HR_NFF;
575 	if (isr3_bits & HR_NEF)
576 		priv->imr3_bits &= ~HR_NEF;
577 	if (isr3_bits & HR_DONE)
578 		priv->imr3_bits &= ~HR_DONE;
579 	if (isr3_bits & (HR_INTR | HR_TLCI)) {
580 		dev_dbg(board->gpib_dev, "minor %i isr0 0x%x imr0 0x%x isr3 0x%x imr3 0x%x\n",
581 			board->minor, isr0_bits, priv->imr0_bits, isr3_bits, imr3_bits);
582 		tnt_writeb(priv, priv->imr3_bits, IMR3);
583 		wake_up_interruptible(&board->wait);
584 	}
585 	spin_unlock_irqrestore(&board->spinlock, flags);
586 	return IRQ_HANDLED;
587 }
588 
tnt4882_interrupt(int irq,void * arg)589 static irqreturn_t tnt4882_interrupt(int irq, void *arg)
590 {
591 	return tnt4882_internal_interrupt(arg);
592 }
593 
594 // wrappers for interface functions
tnt4882_read(struct gpib_board * board,uint8_t * buffer,size_t length,int * end,size_t * bytes_read)595 static int tnt4882_read(struct gpib_board *board, uint8_t *buffer, size_t length, int *end,
596 			size_t *bytes_read)
597 {
598 	struct tnt4882_priv *priv = board->private_data;
599 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
600 	int retval;
601 	int dummy;
602 
603 	retval = nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
604 
605 	if (retval < 0)	{	// force immediate holdoff
606 		write_byte(nec_priv, AUX_HLDI, AUXMR);
607 
608 		set_bit(RFD_HOLDOFF_BN, &nec_priv->state);
609 
610 		nec7210_read_data_in(board, nec_priv, &dummy);
611 	}
612 	return retval;
613 }
614 
tnt4882_write(struct gpib_board * board,uint8_t * buffer,size_t length,int send_eoi,size_t * bytes_written)615 static int tnt4882_write(struct gpib_board *board, uint8_t *buffer, size_t length, int send_eoi,
616 			 size_t *bytes_written)
617 {
618 	struct tnt4882_priv *priv = board->private_data;
619 
620 	return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
621 }
622 
tnt4882_command_unaccel(struct gpib_board * board,uint8_t * buffer,size_t length,size_t * bytes_written)623 static int tnt4882_command_unaccel(struct gpib_board *board, uint8_t *buffer,
624 				   size_t length, size_t *bytes_written)
625 {
626 	struct tnt4882_priv *priv = board->private_data;
627 
628 	return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
629 }
630 
tnt4882_take_control(struct gpib_board * board,int synchronous)631 static int tnt4882_take_control(struct gpib_board *board, int synchronous)
632 {
633 	struct tnt4882_priv *priv = board->private_data;
634 
635 	return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
636 }
637 
tnt4882_go_to_standby(struct gpib_board * board)638 static int tnt4882_go_to_standby(struct gpib_board *board)
639 {
640 	struct tnt4882_priv *priv = board->private_data;
641 
642 	return nec7210_go_to_standby(board, &priv->nec7210_priv);
643 }
644 
tnt4882_request_system_control(struct gpib_board * board,int request_control)645 static void tnt4882_request_system_control(struct gpib_board *board, int request_control)
646 {
647 	struct tnt4882_priv *priv = board->private_data;
648 
649 	if (request_control) {
650 		tnt_writeb(priv, SETSC, CMDR);
651 		udelay(1);
652 	}
653 	nec7210_request_system_control(board, &priv->nec7210_priv, request_control);
654 	if (!request_control) {
655 		tnt_writeb(priv, CLRSC, CMDR);
656 		udelay(1);
657 	}
658 }
659 
tnt4882_interface_clear(struct gpib_board * board,int assert)660 static void tnt4882_interface_clear(struct gpib_board *board, int assert)
661 {
662 	struct tnt4882_priv *priv = board->private_data;
663 
664 	nec7210_interface_clear(board, &priv->nec7210_priv, assert);
665 }
666 
tnt4882_remote_enable(struct gpib_board * board,int enable)667 static void tnt4882_remote_enable(struct gpib_board *board, int enable)
668 {
669 	struct tnt4882_priv *priv = board->private_data;
670 
671 	nec7210_remote_enable(board, &priv->nec7210_priv, enable);
672 }
673 
tnt4882_enable_eos(struct gpib_board * board,uint8_t eos_byte,int compare_8_bits)674 static int tnt4882_enable_eos(struct gpib_board *board, uint8_t eos_byte, int compare_8_bits)
675 {
676 	struct tnt4882_priv *priv = board->private_data;
677 
678 	return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
679 }
680 
tnt4882_disable_eos(struct gpib_board * board)681 static void tnt4882_disable_eos(struct gpib_board *board)
682 {
683 	struct tnt4882_priv *priv = board->private_data;
684 
685 	nec7210_disable_eos(board, &priv->nec7210_priv);
686 }
687 
tnt4882_update_status(struct gpib_board * board,unsigned int clear_mask)688 static unsigned int tnt4882_update_status(struct gpib_board *board, unsigned int clear_mask)
689 {
690 	unsigned long flags;
691 	u8 line_status;
692 	struct tnt4882_priv *priv = board->private_data;
693 
694 	spin_lock_irqsave(&board->spinlock, flags);
695 	board->status &= ~clear_mask;
696 	nec7210_update_status_nolock(board, &priv->nec7210_priv);
697 	/* set / clear SRQ state since it is not cleared by interrupt */
698 	line_status = tnt_readb(priv, BSR);
699 	if (line_status & BCSR_SRQ_BIT)
700 		set_bit(SRQI_NUM, &board->status);
701 	else
702 		clear_bit(SRQI_NUM, &board->status);
703 	spin_unlock_irqrestore(&board->spinlock, flags);
704 	return board->status;
705 }
706 
tnt4882_primary_address(struct gpib_board * board,unsigned int address)707 static int tnt4882_primary_address(struct gpib_board *board, unsigned int address)
708 {
709 	struct tnt4882_priv *priv = board->private_data;
710 
711 	return nec7210_primary_address(board, &priv->nec7210_priv, address);
712 }
713 
tnt4882_secondary_address(struct gpib_board * board,unsigned int address,int enable)714 static int tnt4882_secondary_address(struct gpib_board *board, unsigned int address, int enable)
715 {
716 	struct tnt4882_priv *priv = board->private_data;
717 
718 	return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
719 }
720 
tnt4882_parallel_poll(struct gpib_board * board,uint8_t * result)721 static int tnt4882_parallel_poll(struct gpib_board *board, uint8_t *result)
722 {
723 	struct tnt4882_priv *tnt_priv = board->private_data;
724 
725 	if (tnt_priv->nec7210_priv.type != NEC7210) {
726 		tnt_priv->auxg_bits |= RPP2_BIT;
727 		write_byte(&tnt_priv->nec7210_priv, tnt_priv->auxg_bits, AUXMR);
728 		udelay(2);	//FIXME use parallel poll timeout
729 		*result = read_byte(&tnt_priv->nec7210_priv, CPTR);
730 		tnt_priv->auxg_bits &= ~RPP2_BIT;
731 		write_byte(&tnt_priv->nec7210_priv, tnt_priv->auxg_bits, AUXMR);
732 		return 0;
733 	} else {
734 		return nec7210_parallel_poll(board, &tnt_priv->nec7210_priv, result);
735 	}
736 }
737 
tnt4882_parallel_poll_configure(struct gpib_board * board,uint8_t config)738 static void tnt4882_parallel_poll_configure(struct gpib_board *board, uint8_t config)
739 {
740 	struct tnt4882_priv *priv = board->private_data;
741 
742 	if (priv->nec7210_priv.type == TNT5004) {
743 		/* configure locally */
744 		write_byte(&priv->nec7210_priv, AUXRI | 0x4, AUXMR);
745 		if (config)
746 			/* set response + clear sense */
747 			write_byte(&priv->nec7210_priv, PPR | config, AUXMR);
748 		else
749 			/* disable ppoll */
750 			write_byte(&priv->nec7210_priv, PPR | 0x10, AUXMR);
751 	} else {
752 		nec7210_parallel_poll_configure(board, &priv->nec7210_priv, config);
753 	}
754 }
755 
tnt4882_parallel_poll_response(struct gpib_board * board,int ist)756 static void tnt4882_parallel_poll_response(struct gpib_board *board, int ist)
757 {
758 	struct tnt4882_priv *priv = board->private_data;
759 
760 	nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
761 }
762 
763 /* this is just used by the old nec7210 isa interfaces, the newer
764  * boards use tnt4882_serial_poll_response2
765  */
tnt4882_serial_poll_response(struct gpib_board * board,uint8_t status)766 static void tnt4882_serial_poll_response(struct gpib_board *board, uint8_t status)
767 {
768 	struct tnt4882_priv *priv = board->private_data;
769 
770 	nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
771 }
772 
tnt4882_serial_poll_response2(struct gpib_board * board,uint8_t status,int new_reason_for_service)773 static void tnt4882_serial_poll_response2(struct gpib_board *board, uint8_t status,
774 					  int new_reason_for_service)
775 {
776 	struct tnt4882_priv *priv = board->private_data;
777 	unsigned long flags;
778 	const int MSS = status & request_service_bit;
779 	const int reqt = MSS && new_reason_for_service;
780 	const int reqf = MSS == 0;
781 
782 	spin_lock_irqsave(&board->spinlock, flags);
783 	if (reqt) {
784 		priv->nec7210_priv.srq_pending = 1;
785 		clear_bit(SPOLL_NUM, &board->status);
786 	} else {
787 		if (reqf)
788 			priv->nec7210_priv.srq_pending = 0;
789 	}
790 	if (reqt)
791 		/* It may seem like a race to issue reqt before updating
792 		 * the status byte, but it is not.  The chip does not
793 		 * issue the reqt until the SPMR is written to at
794 		 * a later time.
795 		 */
796 		write_byte(&priv->nec7210_priv, AUX_REQT, AUXMR);
797 	else if (reqf)
798 		write_byte(&priv->nec7210_priv, AUX_REQF, AUXMR);
799 	/* We need to always zero bit 6 of the status byte before writing it to
800 	 * the SPMR to insure we are using
801 	 * serial poll mode SP1, and not accidentally triggering mode SP3.
802 	 */
803 	write_byte(&priv->nec7210_priv, status & ~request_service_bit, SPMR);
804 	spin_unlock_irqrestore(&board->spinlock, flags);
805 }
806 
tnt4882_serial_poll_status(struct gpib_board * board)807 static uint8_t tnt4882_serial_poll_status(struct gpib_board *board)
808 {
809 	struct tnt4882_priv *priv = board->private_data;
810 
811 	return nec7210_serial_poll_status(board, &priv->nec7210_priv);
812 }
813 
tnt4882_return_to_local(struct gpib_board * board)814 static void tnt4882_return_to_local(struct gpib_board *board)
815 {
816 	struct tnt4882_priv *priv = board->private_data;
817 
818 	nec7210_return_to_local(board, &priv->nec7210_priv);
819 }
820 
tnt4882_board_reset(struct tnt4882_priv * tnt_priv,struct gpib_board * board)821 static void tnt4882_board_reset(struct tnt4882_priv *tnt_priv, struct gpib_board *board)
822 {
823 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
824 
825 	tnt_priv->imr0_bits = 0;
826 	tnt_writeb(tnt_priv, tnt_priv->imr0_bits, IMR0);
827 	tnt_priv->imr3_bits = 0;
828 	tnt_writeb(tnt_priv, tnt_priv->imr3_bits, IMR3);
829 	tnt_readb(tnt_priv, IMR0);
830 	tnt_readb(tnt_priv, IMR3);
831 	nec7210_board_reset(nec_priv, board);
832 }
833 
tnt4882_allocate_private(struct gpib_board * board)834 static int tnt4882_allocate_private(struct gpib_board *board)
835 {
836 	struct tnt4882_priv *tnt_priv;
837 
838 	board->private_data = kmalloc(sizeof(struct tnt4882_priv), GFP_KERNEL);
839 	if (!board->private_data)
840 		return -1;
841 	tnt_priv = board->private_data;
842 	memset(tnt_priv, 0, sizeof(struct tnt4882_priv));
843 	init_nec7210_private(&tnt_priv->nec7210_priv);
844 	return 0;
845 }
846 
tnt4882_free_private(struct gpib_board * board)847 static void tnt4882_free_private(struct gpib_board *board)
848 {
849 	kfree(board->private_data);
850 	board->private_data = NULL;
851 }
852 
tnt4882_init(struct tnt4882_priv * tnt_priv,const struct gpib_board * board)853 static void tnt4882_init(struct tnt4882_priv *tnt_priv, const struct gpib_board *board)
854 {
855 	struct nec7210_priv *nec_priv = &tnt_priv->nec7210_priv;
856 
857 	/* Turbo488 software reset */
858 	tnt_writeb(tnt_priv, SOFT_RESET, CMDR);
859 	udelay(1);
860 
861 	// turn off one-chip mode
862 	tnt_writeb(tnt_priv, NODMA, HSSEL);
863 	tnt_writeb(tnt_priv, 0, ACCWR);
864 	// make sure we are in 7210 mode
865 	tnt_writeb(tnt_priv, AUX_7210, AUXCR);
866 	udelay(1);
867 	// registers might be swapped, so write it to the swapped address too
868 	tnt_writeb(tnt_priv, AUX_7210, SWAPPED_AUXCR);
869 	udelay(1);
870 	// turn on one-chip mode
871 	if (nec_priv->type == TNT4882 || nec_priv->type == TNT5004)
872 		tnt_writeb(tnt_priv, NODMA | TNT_ONE_CHIP_BIT, HSSEL);
873 	else
874 		tnt_writeb(tnt_priv, NODMA, HSSEL);
875 
876 	nec7210_board_reset(nec_priv, board);
877 	// read-clear isr0
878 	tnt_readb(tnt_priv, ISR0);
879 
880 	// enable passing of nat4882 interrupts
881 	tnt_priv->imr3_bits = HR_TLCI;
882 	tnt_writeb(tnt_priv, tnt_priv->imr3_bits, IMR3);
883 
884 	// enable interrupt
885 	tnt_writeb(tnt_priv, 0x1, INTRT);
886 
887 	// force immediate holdoff
888 	write_byte(&tnt_priv->nec7210_priv, AUX_HLDI, AUXMR);
889 
890 	set_bit(RFD_HOLDOFF_BN, &nec_priv->state);
891 
892 	tnt_priv->auxg_bits = AUXRG | NTNL_BIT;
893 	write_byte(&tnt_priv->nec7210_priv, tnt_priv->auxg_bits, AUXMR);
894 
895 	nec7210_board_online(nec_priv, board);
896 	// enable interface clear interrupt for event queue
897 	tnt_priv->imr0_bits = TNT_IMR0_ALWAYS_BITS | TNT_ATNI_BIT | TNT_IFCIE_BIT;
898 	tnt_writeb(tnt_priv, tnt_priv->imr0_bits, IMR0);
899 }
900 
ni_pci_attach(struct gpib_board * board,const gpib_board_config_t * config)901 static int ni_pci_attach(struct gpib_board *board, const gpib_board_config_t *config)
902 {
903 	struct tnt4882_priv *tnt_priv;
904 	struct nec7210_priv *nec_priv;
905 	int isr_flags = IRQF_SHARED;
906 	int retval;
907 	struct mite_struct *mite;
908 
909 	board->status = 0;
910 
911 	if (tnt4882_allocate_private(board))
912 		return -ENOMEM;
913 	tnt_priv = board->private_data;
914 	nec_priv = &tnt_priv->nec7210_priv;
915 	nec_priv->type = TNT4882;
916 	nec_priv->read_byte = nec7210_locking_iomem_read_byte;
917 	nec_priv->write_byte = nec7210_locking_iomem_write_byte;
918 	nec_priv->offset = atgpib_reg_offset;
919 
920 	if (!mite_devices)
921 		return -ENODEV;
922 
923 	for (mite = mite_devices; mite; mite = mite->next) {
924 		short found_board;
925 
926 		if (mite->used)
927 			continue;
928 		if (config->pci_bus >= 0 && config->pci_bus != mite->pcidev->bus->number)
929 			continue;
930 		if (config->pci_slot >= 0 && config->pci_slot != PCI_SLOT(mite->pcidev->devfn))
931 			continue;
932 		switch (mite_device_id(mite)) {
933 		case PCI_DEVICE_ID_NI_GPIB:
934 		case PCI_DEVICE_ID_NI_GPIB_PLUS:
935 		case PCI_DEVICE_ID_NI_GPIB_PLUS2:
936 		case PCI_DEVICE_ID_NI_PXIGPIB:
937 		case PCI_DEVICE_ID_NI_PMCGPIB:
938 		case PCI_DEVICE_ID_NI_PCIEGPIB:
939 		case PCI_DEVICE_ID_NI_PCIE2GPIB:
940 // support for Measurement Computing PCI-488
941 		case PCI_DEVICE_ID_MC_PCI488:
942 		case PCI_DEVICE_ID_CEC_NI_GPIB:
943 			found_board = 1;
944 			break;
945 		default:
946 			found_board = 0;
947 			break;
948 		}
949 		if (found_board)
950 			break;
951 	}
952 	if (!mite)
953 		return -ENODEV;
954 
955 	tnt_priv->mite = mite;
956 	retval = mite_setup(tnt_priv->mite);
957 	if (retval < 0)
958 		return retval;
959 
960 	nec_priv->mmiobase = tnt_priv->mite->daq_io_addr;
961 
962 	// get irq
963 	retval = request_irq(mite_irq(tnt_priv->mite), tnt4882_interrupt, isr_flags, "ni-pci-gpib",
964 			     board);
965 	if (retval) {
966 		dev_err(board->gpib_dev, "failed to obtain pci irq %d\n", mite_irq(tnt_priv->mite));
967 		return retval;
968 	}
969 	tnt_priv->irq = mite_irq(tnt_priv->mite);
970 
971 	// TNT5004 detection
972 	switch (tnt_readb(tnt_priv, CSR) & 0xf0) {
973 	case 0x30:
974 		nec_priv->type = TNT4882;
975 		break;
976 	case 0x40:
977 		nec_priv->type = TNT5004;
978 		break;
979 	}
980 	tnt4882_init(tnt_priv, board);
981 
982 	return 0;
983 }
984 
ni_pci_detach(struct gpib_board * board)985 static void ni_pci_detach(struct gpib_board *board)
986 {
987 	struct tnt4882_priv *tnt_priv = board->private_data;
988 	struct nec7210_priv *nec_priv;
989 
990 	if (tnt_priv) {
991 		nec_priv = &tnt_priv->nec7210_priv;
992 
993 		if (nec_priv->mmiobase)
994 			tnt4882_board_reset(tnt_priv, board);
995 		if (tnt_priv->irq)
996 			free_irq(tnt_priv->irq, board);
997 		if (tnt_priv->mite)
998 			mite_unsetup(tnt_priv->mite);
999 	}
1000 	tnt4882_free_private(board);
1001 }
1002 
ni_isapnp_find(struct pnp_dev ** dev)1003 static int ni_isapnp_find(struct pnp_dev **dev)
1004 {
1005 	*dev = pnp_find_dev(NULL, ISAPNP_VENDOR_ID_NI,
1006 			    ISAPNP_FUNCTION(ISAPNP_ID_NI_ATGPIB_TNT), NULL);
1007 	if (!*dev || !(*dev)->card)
1008 		return -ENODEV;
1009 	if (pnp_device_attach(*dev) < 0)
1010 		return -EBUSY;
1011 	if (pnp_activate_dev(*dev) < 0)	{
1012 		pnp_device_detach(*dev);
1013 		return -EAGAIN;
1014 	}
1015 	if (!pnp_port_valid(*dev, 0) || !pnp_irq_valid(*dev, 0)) {
1016 		pnp_device_detach(*dev);
1017 		return -EINVAL;
1018 	}
1019 	return 0;
1020 }
1021 
ni_isa_attach_common(struct gpib_board * board,const gpib_board_config_t * config,enum nec7210_chipset chipset)1022 static int ni_isa_attach_common(struct gpib_board *board, const gpib_board_config_t *config,
1023 				enum nec7210_chipset chipset)
1024 {
1025 	struct tnt4882_priv *tnt_priv;
1026 	struct nec7210_priv *nec_priv;
1027 	int isr_flags = 0;
1028 	u32 iobase;
1029 	int irq;
1030 	int retval;
1031 
1032 	board->status = 0;
1033 
1034 	if (tnt4882_allocate_private(board))
1035 		return -ENOMEM;
1036 	tnt_priv = board->private_data;
1037 	nec_priv = &tnt_priv->nec7210_priv;
1038 	nec_priv->type = chipset;
1039 	nec_priv->read_byte = nec7210_locking_ioport_read_byte;
1040 	nec_priv->write_byte = nec7210_locking_ioport_write_byte;
1041 	nec_priv->offset = atgpib_reg_offset;
1042 
1043 	// look for plug-n-play board
1044 	if (config->ibbase == 0) {
1045 		struct pnp_dev *dev;
1046 
1047 		retval = ni_isapnp_find(&dev);
1048 		if (retval < 0)
1049 			return retval;
1050 		tnt_priv->pnp_dev = dev;
1051 		iobase = pnp_port_start(dev, 0);
1052 		irq = pnp_irq(dev, 0);
1053 	} else {
1054 		iobase = config->ibbase;
1055 		irq = config->ibirq;
1056 	}
1057 	// allocate ioports
1058 	if (!request_region(iobase, atgpib_iosize, "atgpib"))
1059 		return -EBUSY;
1060 
1061 	nec_priv->mmiobase = ioport_map(iobase, atgpib_iosize);
1062 	if (!nec_priv->mmiobase)
1063 		return -EBUSY;
1064 
1065 	// get irq
1066 	retval = request_irq(irq, tnt4882_interrupt, isr_flags, "atgpib", board);
1067 	if (retval) {
1068 		dev_err(board->gpib_dev, "failed to request ISA irq %d\n", irq);
1069 		return retval;
1070 	}
1071 	tnt_priv->irq = irq;
1072 
1073 	tnt4882_init(tnt_priv, board);
1074 
1075 	return 0;
1076 }
1077 
ni_tnt_isa_attach(struct gpib_board * board,const gpib_board_config_t * config)1078 static int ni_tnt_isa_attach(struct gpib_board *board, const gpib_board_config_t *config)
1079 {
1080 	return ni_isa_attach_common(board, config, TNT4882);
1081 }
1082 
ni_nat4882_isa_attach(struct gpib_board * board,const gpib_board_config_t * config)1083 static int ni_nat4882_isa_attach(struct gpib_board *board, const gpib_board_config_t *config)
1084 {
1085 	return ni_isa_attach_common(board, config, NAT4882);
1086 }
1087 
ni_nec_isa_attach(struct gpib_board * board,const gpib_board_config_t * config)1088 static int ni_nec_isa_attach(struct gpib_board *board, const gpib_board_config_t *config)
1089 {
1090 	return ni_isa_attach_common(board, config, NEC7210);
1091 }
1092 
ni_isa_detach(struct gpib_board * board)1093 static void ni_isa_detach(struct gpib_board *board)
1094 {
1095 	struct tnt4882_priv *tnt_priv = board->private_data;
1096 	struct nec7210_priv *nec_priv;
1097 
1098 	if (tnt_priv) {
1099 		nec_priv = &tnt_priv->nec7210_priv;
1100 		if (nec_priv->iobase)
1101 			tnt4882_board_reset(tnt_priv, board);
1102 		if (tnt_priv->irq)
1103 			free_irq(tnt_priv->irq, board);
1104 		if (nec_priv->mmiobase)
1105 			ioport_unmap(nec_priv->mmiobase);
1106 		if (nec_priv->iobase)
1107 			release_region(nec_priv->iobase, atgpib_iosize);
1108 		if (tnt_priv->pnp_dev)
1109 			pnp_device_detach(tnt_priv->pnp_dev);
1110 	}
1111 	tnt4882_free_private(board);
1112 }
1113 
tnt4882_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)1114 static int tnt4882_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1115 {
1116 	return 0;
1117 }
1118 
1119 static gpib_interface_t ni_pci_interface = {
1120 	.name = "ni_pci",
1121 	.attach = ni_pci_attach,
1122 	.detach = ni_pci_detach,
1123 	.read = tnt4882_accel_read,
1124 	.write = tnt4882_accel_write,
1125 	.command = tnt4882_command,
1126 	.take_control = tnt4882_take_control,
1127 	.go_to_standby = tnt4882_go_to_standby,
1128 	.request_system_control = tnt4882_request_system_control,
1129 	.interface_clear = tnt4882_interface_clear,
1130 	.remote_enable = tnt4882_remote_enable,
1131 	.enable_eos = tnt4882_enable_eos,
1132 	.disable_eos = tnt4882_disable_eos,
1133 	.parallel_poll = tnt4882_parallel_poll,
1134 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1135 	.parallel_poll_response = tnt4882_parallel_poll_response,
1136 	.local_parallel_poll_mode = NULL, // XXX
1137 	.line_status = tnt4882_line_status,
1138 	.update_status = tnt4882_update_status,
1139 	.primary_address = tnt4882_primary_address,
1140 	.secondary_address = tnt4882_secondary_address,
1141 	.serial_poll_response2 = tnt4882_serial_poll_response2,
1142 	.serial_poll_status = tnt4882_serial_poll_status,
1143 	.t1_delay = tnt4882_t1_delay,
1144 	.return_to_local = tnt4882_return_to_local,
1145 };
1146 
1147 static gpib_interface_t ni_pci_accel_interface = {
1148 	.name = "ni_pci_accel",
1149 	.attach = ni_pci_attach,
1150 	.detach = ni_pci_detach,
1151 	.read = tnt4882_accel_read,
1152 	.write = tnt4882_accel_write,
1153 	.command = tnt4882_command,
1154 	.take_control = tnt4882_take_control,
1155 	.go_to_standby = tnt4882_go_to_standby,
1156 	.request_system_control = tnt4882_request_system_control,
1157 	.interface_clear = tnt4882_interface_clear,
1158 	.remote_enable = tnt4882_remote_enable,
1159 	.enable_eos = tnt4882_enable_eos,
1160 	.disable_eos = tnt4882_disable_eos,
1161 	.parallel_poll = tnt4882_parallel_poll,
1162 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1163 	.parallel_poll_response = tnt4882_parallel_poll_response,
1164 	.local_parallel_poll_mode = NULL, // XXX
1165 	.line_status = tnt4882_line_status,
1166 	.update_status = tnt4882_update_status,
1167 	.primary_address = tnt4882_primary_address,
1168 	.secondary_address = tnt4882_secondary_address,
1169 	.serial_poll_response2 = tnt4882_serial_poll_response2,
1170 	.serial_poll_status = tnt4882_serial_poll_status,
1171 	.t1_delay = tnt4882_t1_delay,
1172 	.return_to_local = tnt4882_return_to_local,
1173 };
1174 
1175 static gpib_interface_t ni_isa_interface = {
1176 	.name = "ni_isa",
1177 	.attach = ni_tnt_isa_attach,
1178 	.detach = ni_isa_detach,
1179 	.read = tnt4882_accel_read,
1180 	.write = tnt4882_accel_write,
1181 	.command = tnt4882_command,
1182 	.take_control = tnt4882_take_control,
1183 	.go_to_standby = tnt4882_go_to_standby,
1184 	.request_system_control = tnt4882_request_system_control,
1185 	.interface_clear = tnt4882_interface_clear,
1186 	.remote_enable = tnt4882_remote_enable,
1187 	.enable_eos = tnt4882_enable_eos,
1188 	.disable_eos = tnt4882_disable_eos,
1189 	.parallel_poll = tnt4882_parallel_poll,
1190 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1191 	.parallel_poll_response = tnt4882_parallel_poll_response,
1192 	.local_parallel_poll_mode = NULL, // XXX
1193 	.line_status = tnt4882_line_status,
1194 	.update_status = tnt4882_update_status,
1195 	.primary_address = tnt4882_primary_address,
1196 	.secondary_address = tnt4882_secondary_address,
1197 	.serial_poll_response2 = tnt4882_serial_poll_response2,
1198 	.serial_poll_status = tnt4882_serial_poll_status,
1199 	.t1_delay = tnt4882_t1_delay,
1200 	.return_to_local = tnt4882_return_to_local,
1201 };
1202 
1203 static gpib_interface_t ni_nat4882_isa_interface = {
1204 	.name = "ni_nat4882_isa",
1205 	.attach = ni_nat4882_isa_attach,
1206 	.detach = ni_isa_detach,
1207 	.read = tnt4882_read,
1208 	.write = tnt4882_write,
1209 	.command = tnt4882_command_unaccel,
1210 	.take_control = tnt4882_take_control,
1211 	.go_to_standby = tnt4882_go_to_standby,
1212 	.request_system_control = tnt4882_request_system_control,
1213 	.interface_clear = tnt4882_interface_clear,
1214 	.remote_enable = tnt4882_remote_enable,
1215 	.enable_eos = tnt4882_enable_eos,
1216 	.disable_eos = tnt4882_disable_eos,
1217 	.parallel_poll = tnt4882_parallel_poll,
1218 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1219 	.parallel_poll_response = tnt4882_parallel_poll_response,
1220 	.local_parallel_poll_mode = NULL, // XXX
1221 	.line_status = tnt4882_line_status,
1222 	.update_status = tnt4882_update_status,
1223 	.primary_address = tnt4882_primary_address,
1224 	.secondary_address = tnt4882_secondary_address,
1225 	.serial_poll_response2 = tnt4882_serial_poll_response2,
1226 	.serial_poll_status = tnt4882_serial_poll_status,
1227 	.t1_delay = tnt4882_t1_delay,
1228 	.return_to_local = tnt4882_return_to_local,
1229 };
1230 
1231 static gpib_interface_t ni_nec_isa_interface = {
1232 	.name = "ni_nec_isa",
1233 	.attach = ni_nec_isa_attach,
1234 	.detach = ni_isa_detach,
1235 	.read = tnt4882_read,
1236 	.write = tnt4882_write,
1237 	.command = tnt4882_command_unaccel,
1238 	.take_control = tnt4882_take_control,
1239 	.go_to_standby = tnt4882_go_to_standby,
1240 	.request_system_control = tnt4882_request_system_control,
1241 	.interface_clear = tnt4882_interface_clear,
1242 	.remote_enable = tnt4882_remote_enable,
1243 	.enable_eos = tnt4882_enable_eos,
1244 	.disable_eos = tnt4882_disable_eos,
1245 	.parallel_poll = tnt4882_parallel_poll,
1246 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1247 	.parallel_poll_response = tnt4882_parallel_poll_response,
1248 	.local_parallel_poll_mode = NULL, // XXX
1249 	.line_status = NULL,
1250 	.update_status = tnt4882_update_status,
1251 	.primary_address = tnt4882_primary_address,
1252 	.secondary_address = tnt4882_secondary_address,
1253 	.serial_poll_response = tnt4882_serial_poll_response,
1254 	.serial_poll_status = tnt4882_serial_poll_status,
1255 	.t1_delay = tnt4882_t1_delay,
1256 	.return_to_local = tnt4882_return_to_local,
1257 };
1258 
1259 static gpib_interface_t ni_isa_accel_interface = {
1260 	.name = "ni_isa_accel",
1261 	.attach = ni_tnt_isa_attach,
1262 	.detach = ni_isa_detach,
1263 	.read = tnt4882_accel_read,
1264 	.write = tnt4882_accel_write,
1265 	.command = tnt4882_command,
1266 	.take_control = tnt4882_take_control,
1267 	.go_to_standby = tnt4882_go_to_standby,
1268 	.request_system_control = tnt4882_request_system_control,
1269 	.interface_clear = tnt4882_interface_clear,
1270 	.remote_enable = tnt4882_remote_enable,
1271 	.enable_eos = tnt4882_enable_eos,
1272 	.disable_eos = tnt4882_disable_eos,
1273 	.parallel_poll = tnt4882_parallel_poll,
1274 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1275 	.parallel_poll_response = tnt4882_parallel_poll_response,
1276 	.local_parallel_poll_mode = NULL, // XXX
1277 	.line_status = tnt4882_line_status,
1278 	.update_status = tnt4882_update_status,
1279 	.primary_address = tnt4882_primary_address,
1280 	.secondary_address = tnt4882_secondary_address,
1281 	.serial_poll_response2 = tnt4882_serial_poll_response2,
1282 	.serial_poll_status = tnt4882_serial_poll_status,
1283 	.t1_delay = tnt4882_t1_delay,
1284 	.return_to_local = tnt4882_return_to_local,
1285 };
1286 
1287 static gpib_interface_t ni_nat4882_isa_accel_interface = {
1288 	.name = "ni_nat4882_isa_accel",
1289 	.attach = ni_nat4882_isa_attach,
1290 	.detach = ni_isa_detach,
1291 	.read = tnt4882_accel_read,
1292 	.write = tnt4882_accel_write,
1293 	.command = tnt4882_command_unaccel,
1294 	.take_control = tnt4882_take_control,
1295 	.go_to_standby = tnt4882_go_to_standby,
1296 	.request_system_control = tnt4882_request_system_control,
1297 	.interface_clear = tnt4882_interface_clear,
1298 	.remote_enable = tnt4882_remote_enable,
1299 	.enable_eos = tnt4882_enable_eos,
1300 	.disable_eos = tnt4882_disable_eos,
1301 	.parallel_poll = tnt4882_parallel_poll,
1302 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1303 	.parallel_poll_response = tnt4882_parallel_poll_response,
1304 	.local_parallel_poll_mode = NULL, // XXX
1305 	.line_status = tnt4882_line_status,
1306 	.update_status = tnt4882_update_status,
1307 	.primary_address = tnt4882_primary_address,
1308 	.secondary_address = tnt4882_secondary_address,
1309 	.serial_poll_response2 = tnt4882_serial_poll_response2,
1310 	.serial_poll_status = tnt4882_serial_poll_status,
1311 	.t1_delay = tnt4882_t1_delay,
1312 	.return_to_local = tnt4882_return_to_local,
1313 };
1314 
1315 static gpib_interface_t ni_nec_isa_accel_interface = {
1316 	.name = "ni_nec_isa_accel",
1317 	.attach = ni_nec_isa_attach,
1318 	.detach = ni_isa_detach,
1319 	.read = tnt4882_accel_read,
1320 	.write = tnt4882_accel_write,
1321 	.command = tnt4882_command_unaccel,
1322 	.take_control = tnt4882_take_control,
1323 	.go_to_standby = tnt4882_go_to_standby,
1324 	.request_system_control = tnt4882_request_system_control,
1325 	.interface_clear = tnt4882_interface_clear,
1326 	.remote_enable = tnt4882_remote_enable,
1327 	.enable_eos = tnt4882_enable_eos,
1328 	.disable_eos = tnt4882_disable_eos,
1329 	.parallel_poll = tnt4882_parallel_poll,
1330 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1331 	.parallel_poll_response = tnt4882_parallel_poll_response,
1332 	.local_parallel_poll_mode = NULL, // XXX
1333 	.line_status = NULL,
1334 	.update_status = tnt4882_update_status,
1335 	.primary_address = tnt4882_primary_address,
1336 	.secondary_address = tnt4882_secondary_address,
1337 	.serial_poll_response = tnt4882_serial_poll_response,
1338 	.serial_poll_status = tnt4882_serial_poll_status,
1339 	.t1_delay = tnt4882_t1_delay,
1340 	.return_to_local = tnt4882_return_to_local,
1341 };
1342 
1343 static const struct pci_device_id tnt4882_pci_table[] = {
1344 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_NI_GPIB)},
1345 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_NI_GPIB_PLUS)},
1346 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_NI_GPIB_PLUS2)},
1347 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_NI_PXIGPIB)},
1348 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_NI_PMCGPIB)},
1349 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_NI_PCIEGPIB)},
1350 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_NI_PCIE2GPIB)},
1351 	// support for Measurement Computing PCI-488
1352 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_MC_PCI488)},
1353 	{PCI_DEVICE(PCI_VENDOR_ID_NATINST, PCI_DEVICE_ID_CEC_NI_GPIB)},
1354 	{ 0 }
1355 };
1356 MODULE_DEVICE_TABLE(pci, tnt4882_pci_table);
1357 
1358 static struct pci_driver tnt4882_pci_driver = {
1359 	.name = DRV_NAME,
1360 	.id_table = tnt4882_pci_table,
1361 	.probe = &tnt4882_pci_probe
1362 };
1363 
1364 #if 0
1365 /* unused, will be needed when the driver is turned into a pnp_driver */
1366 static const struct pnp_device_id tnt4882_pnp_table[] = {
1367 	{.id = "NICC601"},
1368 	{.id = ""}
1369 };
1370 MODULE_DEVICE_TABLE(pnp, tnt4882_pnp_table);
1371 #endif
1372 
1373 #ifdef CONFIG_GPIB_PCMCIA
1374 static gpib_interface_t ni_pcmcia_interface;
1375 static gpib_interface_t ni_pcmcia_accel_interface;
1376 static int __init init_ni_gpib_cs(void);
1377 static void __exit exit_ni_gpib_cs(void);
1378 #endif
1379 
tnt4882_init_module(void)1380 static int __init tnt4882_init_module(void)
1381 {
1382 	int result;
1383 
1384 	result = pci_register_driver(&tnt4882_pci_driver);
1385 	if (result) {
1386 		pr_err("pci_register_driver failed: error = %d\n", result);
1387 		return result;
1388 	}
1389 
1390 	result = gpib_register_driver(&ni_isa_interface, THIS_MODULE);
1391 	if (result) {
1392 		pr_err("gpib_register_driver failed: error = %d\n", result);
1393 		goto err_isa;
1394 	}
1395 
1396 	result = gpib_register_driver(&ni_isa_accel_interface, THIS_MODULE);
1397 	if (result) {
1398 		pr_err("gpib_register_driver failed: error = %d\n", result);
1399 		goto err_isa_accel;
1400 	}
1401 
1402 	result = gpib_register_driver(&ni_nat4882_isa_interface, THIS_MODULE);
1403 	if (result) {
1404 		pr_err("gpib_register_driver failed: error = %d\n", result);
1405 		goto err_nat4882_isa;
1406 	}
1407 
1408 	result = gpib_register_driver(&ni_nat4882_isa_accel_interface, THIS_MODULE);
1409 	if (result) {
1410 		pr_err("gpib_register_driver failed: error = %d\n", result);
1411 		goto err_nat4882_isa_accel;
1412 	}
1413 
1414 	result = gpib_register_driver(&ni_nec_isa_interface, THIS_MODULE);
1415 	if (result) {
1416 		pr_err("gpib_register_driver failed: error = %d\n", result);
1417 		goto err_nec_isa;
1418 	}
1419 
1420 	result = gpib_register_driver(&ni_nec_isa_accel_interface, THIS_MODULE);
1421 	if (result) {
1422 		pr_err("gpib_register_driver failed: error = %d\n", result);
1423 		goto err_nec_isa_accel;
1424 	}
1425 
1426 	result = gpib_register_driver(&ni_pci_interface, THIS_MODULE);
1427 	if (result) {
1428 		pr_err("gpib_register_driver failed: error = %d\n", result);
1429 		goto err_pci;
1430 	}
1431 
1432 	result = gpib_register_driver(&ni_pci_accel_interface, THIS_MODULE);
1433 	if (result) {
1434 		pr_err("gpib_register_driver failed: error = %d\n", result);
1435 		goto err_pci_accel;
1436 	}
1437 
1438 #ifdef CONFIG_GPIB_PCMCIA
1439 	result = gpib_register_driver(&ni_pcmcia_interface, THIS_MODULE);
1440 	if (result) {
1441 		pr_err("gpib_register_driver failed: error = %d\n", result);
1442 		goto err_pcmcia;
1443 	}
1444 
1445 	result = gpib_register_driver(&ni_pcmcia_accel_interface, THIS_MODULE);
1446 	if (result) {
1447 		pr_err("gpib_register_driver failed: error = %d\n", result);
1448 		goto err_pcmcia_accel;
1449 	}
1450 
1451 	result = init_ni_gpib_cs();
1452 	if (result) {
1453 		pr_err("pcmcia_register_driver failed: error = %d\n", result);
1454 		goto err_pcmcia_driver;
1455 	}
1456 #endif
1457 
1458 	mite_init();
1459 
1460 	return 0;
1461 
1462 #ifdef CONFIG_GPIB_PCMCIA
1463 err_pcmcia_driver:
1464 	gpib_unregister_driver(&ni_pcmcia_accel_interface);
1465 err_pcmcia_accel:
1466 	gpib_unregister_driver(&ni_pcmcia_interface);
1467 err_pcmcia:
1468 #endif
1469 	gpib_unregister_driver(&ni_pci_accel_interface);
1470 err_pci_accel:
1471 	gpib_unregister_driver(&ni_pci_interface);
1472 err_pci:
1473 	gpib_unregister_driver(&ni_nec_isa_accel_interface);
1474 err_nec_isa_accel:
1475 	gpib_unregister_driver(&ni_nec_isa_interface);
1476 err_nec_isa:
1477 	gpib_unregister_driver(&ni_nat4882_isa_accel_interface);
1478 err_nat4882_isa_accel:
1479 	gpib_unregister_driver(&ni_nat4882_isa_interface);
1480 err_nat4882_isa:
1481 	gpib_unregister_driver(&ni_isa_accel_interface);
1482 err_isa_accel:
1483 	gpib_unregister_driver(&ni_isa_interface);
1484 err_isa:
1485 	pci_unregister_driver(&tnt4882_pci_driver);
1486 
1487 	return result;
1488 }
1489 
tnt4882_exit_module(void)1490 static void __exit tnt4882_exit_module(void)
1491 {
1492 	gpib_unregister_driver(&ni_isa_interface);
1493 	gpib_unregister_driver(&ni_isa_accel_interface);
1494 	gpib_unregister_driver(&ni_nat4882_isa_interface);
1495 	gpib_unregister_driver(&ni_nat4882_isa_accel_interface);
1496 	gpib_unregister_driver(&ni_nec_isa_interface);
1497 	gpib_unregister_driver(&ni_nec_isa_accel_interface);
1498 	gpib_unregister_driver(&ni_pci_interface);
1499 	gpib_unregister_driver(&ni_pci_accel_interface);
1500 #ifdef CONFIG_GPIB_PCMCIA
1501 	gpib_unregister_driver(&ni_pcmcia_interface);
1502 	gpib_unregister_driver(&ni_pcmcia_accel_interface);
1503 	exit_ni_gpib_cs();
1504 #endif
1505 
1506 	mite_cleanup();
1507 
1508 	pci_unregister_driver(&tnt4882_pci_driver);
1509 }
1510 
1511 #ifdef CONFIG_GPIB_PCMCIA
1512 
1513 #include <linux/kernel.h>
1514 #include <linux/moduleparam.h>
1515 #include <linux/ptrace.h>
1516 #include <linux/timer.h>
1517 #include <linux/ioport.h>
1518 #include <linux/io.h>
1519 
1520 #include <pcmcia/cistpl.h>
1521 #include <pcmcia/cisreg.h>
1522 #include <pcmcia/ds.h>
1523 
1524 static int ni_gpib_config(struct pcmcia_device  *link);
1525 static void ni_gpib_release(struct pcmcia_device *link);
1526 static void ni_pcmcia_detach(struct gpib_board *board);
1527 
1528 /*
1529  * A linked list of "instances" of the dummy device.  Each actual
1530  * PCMCIA card corresponds to one device instance, and is described
1531  * by one dev_link_t structure (defined in ds.h).
1532  *
1533  * You may not want to use a linked list for this -- for example, the
1534  * memory card driver uses an array of dev_link_t pointers, where minor
1535  * device numbers are used to derive the corresponding array index.
1536  *
1537  * I think this dev_list is obsolete but the pointer is needed to keep
1538  * the module instance for the ni_pcmcia_attach function.
1539  */
1540 
1541 static struct pcmcia_device   *curr_dev;
1542 
1543 struct local_info_t {
1544 	struct pcmcia_device	*p_dev;
1545 	struct gpib_board		*dev;
1546 	int			stop;
1547 	struct bus_operations	*bus;
1548 };
1549 
1550 /*
1551  * ni_gpib_probe() creates an "instance" of the driver, allocating
1552  * local data structures for one device.  The device is registered
1553  * with Card Services.
1554  */
1555 
ni_gpib_probe(struct pcmcia_device * link)1556 static int ni_gpib_probe(struct pcmcia_device *link)
1557 {
1558 	struct local_info_t *info;
1559 	//struct struct gpib_board *dev;
1560 
1561 	/* Allocate space for private device-specific data */
1562 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1563 	if (!info)
1564 		return -ENOMEM;
1565 
1566 	info->p_dev = link;
1567 	link->priv = info;
1568 
1569 	/*
1570 	 * General socket configuration defaults can go here.  In this
1571 	 * client, we assume very little, and rely on the CIS for almost
1572 	 * everything.  In most clients, many details (i.e., number, sizes,
1573 	 * and attributes of IO windows) are fixed by the nature of the
1574 	 * device, and can be hard-wired here.
1575 	 */
1576 	link->config_flags = CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
1577 
1578 	/* Register with Card Services */
1579 	curr_dev = link;
1580 	return ni_gpib_config(link);
1581 }
1582 
1583 /*
1584  *	This deletes a driver "instance".  The device is de-registered
1585  *	with Card Services.  If it has been released, all local data
1586  *	structures are freed.  Otherwise, the structures will be freed
1587  *	when the device is released.
1588  */
ni_gpib_remove(struct pcmcia_device * link)1589 static void ni_gpib_remove(struct pcmcia_device *link)
1590 {
1591 	struct local_info_t *info = link->priv;
1592 	//struct struct gpib_board *dev = info->dev;
1593 
1594 	if (info->dev)
1595 		ni_pcmcia_detach(info->dev);
1596 	ni_gpib_release(link);
1597 
1598 	//free_netdev(dev);
1599 	kfree(info);
1600 }
1601 
ni_gpib_config_iteration(struct pcmcia_device * link,void * priv_data)1602 static int ni_gpib_config_iteration(struct pcmcia_device *link,	void *priv_data)
1603 {
1604 	int retval;
1605 
1606 	retval = pcmcia_request_io(link);
1607 	if (retval != 0)
1608 		return retval;
1609 
1610 	return 0;
1611 }
1612 
1613 /*
1614  *	ni_gpib_config() is scheduled to run after a CARD_INSERTION event
1615  *	is received, to configure the PCMCIA socket, and to make the
1616  *	device available to the system.
1617  */
ni_gpib_config(struct pcmcia_device * link)1618 static int ni_gpib_config(struct pcmcia_device *link)
1619 {
1620 	//struct local_info_t *info = link->priv;
1621 	//struct gpib_board *dev = info->dev;
1622 	int last_ret;
1623 
1624 	last_ret = pcmcia_loop_config(link, &ni_gpib_config_iteration, NULL);
1625 	if (last_ret) {
1626 		dev_warn(&link->dev, "no configuration found\n");
1627 		ni_gpib_release(link);
1628 		return last_ret;
1629 	}
1630 
1631 	last_ret = pcmcia_enable_device(link);
1632 	if (last_ret) {
1633 		ni_gpib_release(link);
1634 		return last_ret;
1635 	}
1636 	return 0;
1637 } /* ni_gpib_config */
1638 
1639 /*
1640  * After a card is removed, ni_gpib_release() will unregister the
1641  * device, and release the PCMCIA configuration.  If the device is
1642  * still open, this will be postponed until it is closed.
1643  */
ni_gpib_release(struct pcmcia_device * link)1644 static void ni_gpib_release(struct pcmcia_device *link)
1645 {
1646 	pcmcia_disable_device(link);
1647 } /* ni_gpib_release */
1648 
ni_gpib_suspend(struct pcmcia_device * link)1649 static int ni_gpib_suspend(struct pcmcia_device *link)
1650 {
1651 	//struct local_info_t *info = link->priv;
1652 	//struct struct gpib_board *dev = info->dev;
1653 
1654 	if (link->open)
1655 		dev_warn(&link->dev, "Device still open\n");
1656 	//netif_device_detach(dev);
1657 
1658 	return 0;
1659 }
1660 
ni_gpib_resume(struct pcmcia_device * link)1661 static int ni_gpib_resume(struct pcmcia_device *link)
1662 {
1663 	//struct local_info_t *info = link->priv;
1664 	//struct struct gpib_board *dev = info->dev;
1665 
1666 	/*if (link->open) {
1667 	 *	ni_gpib_probe(dev);	/ really?
1668 	 *	//netif_device_attach(dev);
1669 	 *}
1670 	 */
1671 	return ni_gpib_config(link);
1672 }
1673 
1674 static struct pcmcia_device_id ni_pcmcia_ids[] = {
1675 	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x4882),
1676 	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0c71), // NI PCMCIA-GPIB+
1677 	PCMCIA_DEVICE_NULL
1678 };
1679 
1680 MODULE_DEVICE_TABLE(pcmcia, ni_pcmcia_ids);
1681 
1682 static struct pcmcia_driver ni_gpib_cs_driver = {
1683 	.name           = "ni_gpib_cs",
1684 	.owner		= THIS_MODULE,
1685 	.drv = { .name = "ni_gpib_cs", },
1686 	.id_table	= ni_pcmcia_ids,
1687 	.probe		= ni_gpib_probe,
1688 	.remove		= ni_gpib_remove,
1689 	.suspend	= ni_gpib_suspend,
1690 	.resume		= ni_gpib_resume,
1691 };
1692 
init_ni_gpib_cs(void)1693 static int __init init_ni_gpib_cs(void)
1694 {
1695 	return pcmcia_register_driver(&ni_gpib_cs_driver);
1696 }
1697 
exit_ni_gpib_cs(void)1698 static void __exit exit_ni_gpib_cs(void)
1699 {
1700 	pcmcia_unregister_driver(&ni_gpib_cs_driver);
1701 }
1702 
1703 static const int pcmcia_gpib_iosize = 32;
1704 
ni_pcmcia_attach(struct gpib_board * board,const gpib_board_config_t * config)1705 static int ni_pcmcia_attach(struct gpib_board *board, const gpib_board_config_t *config)
1706 {
1707 	struct local_info_t *info;
1708 	struct tnt4882_priv *tnt_priv;
1709 	struct nec7210_priv *nec_priv;
1710 	int isr_flags = IRQF_SHARED;
1711 	int retval;
1712 
1713 	if (!curr_dev)
1714 		return -ENODEV;
1715 
1716 	info = curr_dev->priv;
1717 	info->dev = board;
1718 
1719 	board->status = 0;
1720 
1721 	if (tnt4882_allocate_private(board))
1722 		return -ENOMEM;
1723 
1724 	tnt_priv = board->private_data;
1725 	nec_priv = &tnt_priv->nec7210_priv;
1726 	nec_priv->type = TNT4882;
1727 	nec_priv->read_byte = nec7210_locking_ioport_read_byte;
1728 	nec_priv->write_byte = nec7210_locking_ioport_write_byte;
1729 	nec_priv->offset = atgpib_reg_offset;
1730 
1731 	if (!request_region(curr_dev->resource[0]->start, resource_size(curr_dev->resource[0]),
1732 			    DRV_NAME))
1733 		return -ENOMEM;
1734 
1735 	nec_priv->mmiobase = ioport_map(curr_dev->resource[0]->start,
1736 					resource_size(curr_dev->resource[0]));
1737 	if (!nec_priv->mmiobase)
1738 		return -ENOMEM;
1739 
1740 	// get irq
1741 	retval = request_irq(curr_dev->irq, tnt4882_interrupt, isr_flags, DRV_NAME, board);
1742 	if (retval) {
1743 		dev_err(board->gpib_dev, "failed to obtain PCMCIA irq %d\n", curr_dev->irq);
1744 		return retval;
1745 	}
1746 	tnt_priv->irq = curr_dev->irq;
1747 
1748 	tnt4882_init(tnt_priv, board);
1749 
1750 	return 0;
1751 }
1752 
ni_pcmcia_detach(struct gpib_board * board)1753 static void ni_pcmcia_detach(struct gpib_board *board)
1754 {
1755 	struct tnt4882_priv *tnt_priv = board->private_data;
1756 	struct nec7210_priv *nec_priv;
1757 
1758 	if (tnt_priv) {
1759 		nec_priv = &tnt_priv->nec7210_priv;
1760 		if (tnt_priv->irq)
1761 			free_irq(tnt_priv->irq, board);
1762 		if (nec_priv->mmiobase)
1763 			ioport_unmap(nec_priv->mmiobase);
1764 		if (nec_priv->iobase) {
1765 			tnt4882_board_reset(tnt_priv, board);
1766 			release_region(nec_priv->iobase, pcmcia_gpib_iosize);
1767 		}
1768 	}
1769 	tnt4882_free_private(board);
1770 }
1771 
1772 static gpib_interface_t ni_pcmcia_interface = {
1773 	.name = "ni_pcmcia",
1774 	.attach = ni_pcmcia_attach,
1775 	.detach = ni_pcmcia_detach,
1776 	.read = tnt4882_accel_read,
1777 	.write = tnt4882_accel_write,
1778 	.command = tnt4882_command,
1779 	.take_control = tnt4882_take_control,
1780 	.go_to_standby = tnt4882_go_to_standby,
1781 	.request_system_control = tnt4882_request_system_control,
1782 	.interface_clear = tnt4882_interface_clear,
1783 	.remote_enable = tnt4882_remote_enable,
1784 	.enable_eos = tnt4882_enable_eos,
1785 	.disable_eos = tnt4882_disable_eos,
1786 	.parallel_poll = tnt4882_parallel_poll,
1787 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1788 	.parallel_poll_response = tnt4882_parallel_poll_response,
1789 	.local_parallel_poll_mode = NULL, // XXX
1790 	.line_status = tnt4882_line_status,
1791 	.update_status = tnt4882_update_status,
1792 	.primary_address = tnt4882_primary_address,
1793 	.secondary_address = tnt4882_secondary_address,
1794 	.serial_poll_response = tnt4882_serial_poll_response,
1795 	.serial_poll_status = tnt4882_serial_poll_status,
1796 	.t1_delay = tnt4882_t1_delay,
1797 	.return_to_local = tnt4882_return_to_local,
1798 };
1799 
1800 static gpib_interface_t ni_pcmcia_accel_interface = {
1801 	.name = "ni_pcmcia_accel",
1802 	.attach = ni_pcmcia_attach,
1803 	.detach = ni_pcmcia_detach,
1804 	.read = tnt4882_accel_read,
1805 	.write = tnt4882_accel_write,
1806 	.command = tnt4882_command,
1807 	.take_control = tnt4882_take_control,
1808 	.go_to_standby = tnt4882_go_to_standby,
1809 	.request_system_control = tnt4882_request_system_control,
1810 	.interface_clear = tnt4882_interface_clear,
1811 	.remote_enable = tnt4882_remote_enable,
1812 	.enable_eos = tnt4882_enable_eos,
1813 	.disable_eos = tnt4882_disable_eos,
1814 	.parallel_poll = tnt4882_parallel_poll,
1815 	.parallel_poll_configure = tnt4882_parallel_poll_configure,
1816 	.parallel_poll_response = tnt4882_parallel_poll_response,
1817 	.local_parallel_poll_mode = NULL, // XXX
1818 	.line_status = tnt4882_line_status,
1819 	.update_status = tnt4882_update_status,
1820 	.primary_address = tnt4882_primary_address,
1821 	.secondary_address = tnt4882_secondary_address,
1822 	.serial_poll_response = tnt4882_serial_poll_response,
1823 	.serial_poll_status = tnt4882_serial_poll_status,
1824 	.t1_delay = tnt4882_t1_delay,
1825 	.return_to_local = tnt4882_return_to_local,
1826 };
1827 
1828 #endif	// CONFIG_GPIB_PCMCIA
1829 
1830 module_init(tnt4882_init_module);
1831 module_exit(tnt4882_exit_module);
1832