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