xref: /linux/drivers/gpib/cb7210/cb7210.c (revision 8934827db5403eae57d4537114a9ff88b0a8460f)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  * Measurement Computing boards using cb7210.2 and cbi488.2 chips
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 "cb7210.h"
13 #include <linux/ioport.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <asm/dma.h>
18 #include <linux/bitops.h>
19 #include <linux/pci.h>
20 #include <linux/pci_ids.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include "gpib_pci_ids.h"
25 #include "quancom_pci.h"
26 
27 MODULE_LICENSE("GPL");
28 MODULE_DESCRIPTION("GPIB driver Measurement Computing boards using cb7210.2 and cbi488.2");
29 
30 static int cb7210_read(struct gpib_board *board, u8 *buffer, size_t length,
31 		       int *end, size_t *bytes_read);
32 
have_fifo_word(const struct cb7210_priv * cb_priv)33 	static inline int have_fifo_word(const struct cb7210_priv *cb_priv)
34 {
35 	if (((cb7210_read_byte(cb_priv, HS_STATUS)) &
36 	     (HS_RX_MSB_NOT_EMPTY | HS_RX_LSB_NOT_EMPTY)) ==
37 	    (HS_RX_MSB_NOT_EMPTY | HS_RX_LSB_NOT_EMPTY))
38 		return 1;
39 	else
40 		return 0;
41 }
42 
input_fifo_enable(struct gpib_board * board,int enable)43 static inline void input_fifo_enable(struct gpib_board *board, int enable)
44 {
45 	struct cb7210_priv *cb_priv = board->private_data;
46 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
47 	unsigned long flags;
48 
49 	spin_lock_irqsave(&board->spinlock, flags);
50 
51 	if (enable) {
52 		cb_priv->in_fifo_half_full = 0;
53 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
54 
55 		cb7210_write_byte(cb_priv, HS_RX_ENABLE | HS_TX_ENABLE | HS_CLR_SRQ_INT |
56 				  HS_CLR_EOI_EMPTY_INT | HS_CLR_HF_INT | cb_priv->hs_mode_bits,
57 				  HS_MODE);
58 
59 		cb_priv->hs_mode_bits &= ~HS_ENABLE_MASK;
60 		cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, HS_MODE);
61 
62 		cb7210_write_byte(cb_priv, irq_bits(cb_priv->irq), HS_INT_LEVEL);
63 
64 		cb_priv->hs_mode_bits |= HS_RX_ENABLE;
65 		cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, HS_MODE);
66 	} else {
67 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
68 
69 		cb_priv->hs_mode_bits &= ~HS_ENABLE_MASK;
70 		cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, nec7210_iobase(cb_priv) +
71 				  HS_MODE);
72 
73 		clear_bit(READ_READY_BN, &nec_priv->state);
74 	}
75 
76 	spin_unlock_irqrestore(&board->spinlock, flags);
77 }
78 
fifo_read(struct gpib_board * board,struct cb7210_priv * cb_priv,u8 * buffer,size_t length,int * end,size_t * bytes_read)79 static int fifo_read(struct gpib_board *board, struct cb7210_priv *cb_priv, u8 *buffer,
80 		     size_t length, int *end, size_t *bytes_read)
81 {
82 	ssize_t retval = 0;
83 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
84 	int hs_status;
85 	u16 word;
86 	unsigned long flags;
87 
88 	*bytes_read = 0;
89 	if (cb_priv->fifo_iobase == 0)	{
90 		dev_err(board->gpib_dev, "fifo iobase is zero!\n");
91 		return -EIO;
92 	}
93 	*end = 0;
94 	if (length <= cb7210_fifo_size)	{
95 		dev_err(board->gpib_dev, " bug! fifo read length < fifo size\n");
96 		return -EINVAL;
97 	}
98 
99 	input_fifo_enable(board, 1);
100 
101 	while (*bytes_read + cb7210_fifo_size < length)	{
102 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, HR_DMAI);
103 
104 		if (wait_event_interruptible(board->wait,
105 					     (cb_priv->in_fifo_half_full &&
106 					      have_fifo_word(cb_priv)) ||
107 					     test_bit(RECEIVED_END_BN, &nec_priv->state) ||
108 					     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
109 					     test_bit(TIMO_NUM, &board->status))) {
110 			retval = -ERESTARTSYS;
111 			nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
112 			break;
113 		}
114 
115 		spin_lock_irqsave(&board->spinlock, flags);
116 
117 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
118 
119 		while (have_fifo_word(cb_priv))	{
120 			word = inw(cb_priv->fifo_iobase + DIR);
121 			buffer[(*bytes_read)++] = word & 0xff;
122 			buffer[(*bytes_read)++] = (word >> 8) & 0xff;
123 		}
124 
125 		cb_priv->in_fifo_half_full = 0;
126 
127 		hs_status = cb7210_read_byte(cb_priv, HS_STATUS);
128 
129 		spin_unlock_irqrestore(&board->spinlock, flags);
130 
131 		if (test_and_clear_bit(RECEIVED_END_BN, &nec_priv->state)) {
132 			*end = 1;
133 			break;
134 		}
135 		if (hs_status & HS_FIFO_FULL)
136 			break;
137 		if (test_bit(TIMO_NUM, &board->status))	{
138 			retval = -ETIMEDOUT;
139 			break;
140 		}
141 		if (test_bit(DEV_CLEAR_BN, &nec_priv->state)) {
142 			retval = -EINTR;
143 			break;
144 		}
145 	}
146 	hs_status = cb7210_read_byte(cb_priv, HS_STATUS);
147 	if (hs_status & HS_RX_LSB_NOT_EMPTY) {
148 		word = inw(cb_priv->fifo_iobase + DIR);
149 		buffer[(*bytes_read)++] = word & 0xff;
150 	}
151 
152 	input_fifo_enable(board, 0);
153 
154 	if (wait_event_interruptible(board->wait,
155 				     test_bit(READ_READY_BN, &nec_priv->state) ||
156 				     test_bit(RECEIVED_END_BN, &nec_priv->state) ||
157 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
158 				     test_bit(TIMO_NUM, &board->status))) {
159 		retval = -ERESTARTSYS;
160 	}
161 	if (test_bit(TIMO_NUM, &board->status))
162 		retval = -ETIMEDOUT;
163 	if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
164 		retval = -EINTR;
165 	if (test_bit(READ_READY_BN, &nec_priv->state)) {
166 		nec7210_set_handshake_mode(board, nec_priv, HR_HLDA);
167 		buffer[(*bytes_read)++] = nec7210_read_data_in(board, nec_priv, end);
168 	}
169 
170 	return retval;
171 }
172 
cb7210_accel_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)173 static int cb7210_accel_read(struct gpib_board *board, u8 *buffer,
174 			     size_t length, int *end, size_t *bytes_read)
175 {
176 	ssize_t retval;
177 	struct cb7210_priv *cb_priv = board->private_data;
178 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
179 	size_t num_bytes;
180 
181 	*bytes_read = 0;
182 	// deal with limitations of fifo
183 	if (length < cb7210_fifo_size + 3 || (nec_priv->auxa_bits & HR_REOS))
184 		return cb7210_read(board, buffer, length, end, bytes_read);
185 	*end = 0;
186 
187 	nec7210_release_rfd_holdoff(board, nec_priv);
188 
189 	if (wait_event_interruptible(board->wait,
190 				     test_bit(READ_READY_BN, &nec_priv->state) ||
191 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
192 				     test_bit(TIMO_NUM, &board->status))) {
193 		return -ERESTARTSYS;
194 	}
195 	if (test_bit(TIMO_NUM, &board->status))
196 		return -ETIMEDOUT;
197 	if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
198 		return -EINTR;
199 
200 	nec7210_set_handshake_mode(board, nec_priv, HR_HLDE);
201 	buffer[(*bytes_read)++] = nec7210_read_data_in(board, nec_priv, end);
202 	if (*end)
203 		return 0;
204 
205 	nec7210_release_rfd_holdoff(board, nec_priv);
206 
207 	retval = fifo_read(board, cb_priv, &buffer[*bytes_read], length - *bytes_read - 1,
208 			   end, &num_bytes);
209 	*bytes_read += num_bytes;
210 	if (retval < 0)
211 		return retval;
212 	if (*end)
213 		return 0;
214 
215 	retval = cb7210_read(board, &buffer[*bytes_read], 1, end, &num_bytes);
216 	*bytes_read += num_bytes;
217 	if (retval < 0)
218 		return retval;
219 
220 	return 0;
221 }
222 
output_fifo_empty(const struct cb7210_priv * cb_priv)223 static int output_fifo_empty(const struct cb7210_priv *cb_priv)
224 {
225 	if ((cb7210_read_byte(cb_priv, HS_STATUS) & (HS_TX_MSB_NOT_EMPTY | HS_TX_LSB_NOT_EMPTY))
226 	    == 0)
227 		return 1;
228 	else
229 		return 0;
230 }
231 
output_fifo_enable(struct gpib_board * board,int enable)232 static inline void output_fifo_enable(struct gpib_board *board, int enable)
233 {
234 	struct cb7210_priv *cb_priv = board->private_data;
235 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
236 	unsigned long flags;
237 
238 	spin_lock_irqsave(&board->spinlock, flags);
239 
240 	if (enable) {
241 		nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE, 0);
242 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, HR_DMAO);
243 
244 		cb7210_write_byte(cb_priv, HS_RX_ENABLE | HS_TX_ENABLE | HS_CLR_SRQ_INT |
245 				  HS_CLR_EOI_EMPTY_INT | HS_CLR_HF_INT | cb_priv->hs_mode_bits,
246 				  HS_MODE);
247 
248 		cb_priv->hs_mode_bits &= ~HS_ENABLE_MASK;
249 		cb_priv->hs_mode_bits |= HS_TX_ENABLE;
250 		cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, HS_MODE);
251 
252 		cb7210_write_byte(cb_priv, irq_bits(cb_priv->irq), HS_INT_LEVEL);
253 
254 		clear_bit(WRITE_READY_BN, &nec_priv->state);
255 
256 	} else {
257 		cb_priv->hs_mode_bits &= ~HS_ENABLE_MASK;
258 		cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, HS_MODE);
259 
260 		nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, 0);
261 		nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE, HR_DOIE);
262 	}
263 
264 	spin_unlock_irqrestore(&board->spinlock, flags);
265 }
266 
fifo_write(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)267 static int fifo_write(struct gpib_board *board, u8 *buffer, size_t length,
268 		      size_t *bytes_written)
269 {
270 	size_t count = 0;
271 	ssize_t retval = 0;
272 	struct cb7210_priv *cb_priv = board->private_data;
273 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
274 	unsigned int num_bytes, i;
275 	unsigned long flags;
276 
277 	*bytes_written = 0;
278 	if (cb_priv->fifo_iobase == 0) {
279 		dev_err(board->gpib_dev, "fifo iobase is zero!\n");
280 		return -EINVAL;
281 	}
282 	if (length == 0)
283 		return 0;
284 
285 	clear_bit(DEV_CLEAR_BN, &nec_priv->state);
286 	clear_bit(BUS_ERROR_BN, &nec_priv->state);
287 
288 	output_fifo_enable(board, 1);
289 
290 	while (count < length) {
291 		// wait until byte is ready to be sent
292 		if (wait_event_interruptible(board->wait,
293 					     cb_priv->out_fifo_half_empty ||
294 					     output_fifo_empty(cb_priv) ||
295 					     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
296 					     test_bit(BUS_ERROR_BN, &nec_priv->state) ||
297 					     test_bit(TIMO_NUM, &board->status))) {
298 			retval = -ERESTARTSYS;
299 			break;
300 		}
301 		if (test_bit(TIMO_NUM, &board->status) ||
302 		    test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
303 		    test_bit(BUS_ERROR_BN, &nec_priv->state))
304 			break;
305 
306 		if (output_fifo_empty(cb_priv))
307 			num_bytes = cb7210_fifo_size - cb7210_fifo_width;
308 		else
309 			num_bytes = cb7210_fifo_size / 2;
310 		if (num_bytes + count > length)
311 			num_bytes = length - count;
312 		if (num_bytes % cb7210_fifo_width) {
313 			dev_err(board->gpib_dev, " bug! fifo write with odd number of bytes\n");
314 			retval = -EINVAL;
315 			break;
316 		}
317 
318 		spin_lock_irqsave(&board->spinlock, flags);
319 		for (i = 0; i < num_bytes / cb7210_fifo_width; i++) {
320 			u16 word;
321 
322 			word = buffer[count++] & 0xff;
323 			word |= (buffer[count++] << 8) & 0xff00;
324 			outw(word, cb_priv->fifo_iobase + CDOR);
325 		}
326 		cb_priv->out_fifo_half_empty = 0;
327 		cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits |
328 				  HS_CLR_EOI_EMPTY_INT | HS_CLR_HF_INT, HS_MODE);
329 		cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, HS_MODE);
330 		spin_unlock_irqrestore(&board->spinlock, flags);
331 	}
332 	// wait last byte has been sent
333 	if (wait_event_interruptible(board->wait,
334 				     output_fifo_empty(cb_priv) ||
335 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
336 				     test_bit(BUS_ERROR_BN, &nec_priv->state) ||
337 				     test_bit(TIMO_NUM, &board->status))) {
338 		retval = -ERESTARTSYS;
339 	}
340 	if (test_bit(TIMO_NUM, &board->status))
341 		retval = -ETIMEDOUT;
342 	if (test_bit(BUS_ERROR_BN, &nec_priv->state))
343 		retval = -EIO;
344 	if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
345 		retval = -EINTR;
346 
347 	output_fifo_enable(board, 0);
348 
349 	*bytes_written = count;
350 	return retval;
351 }
352 
cb7210_accel_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)353 static int cb7210_accel_write(struct gpib_board *board, u8 *buffer,
354 			      size_t length, int send_eoi, size_t *bytes_written)
355 {
356 	struct cb7210_priv *cb_priv = board->private_data;
357 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
358 	unsigned long fast_chunk_size, leftover;
359 	int retval;
360 	size_t num_bytes;
361 
362 	*bytes_written = 0;
363 	if (length > cb7210_fifo_width)
364 		fast_chunk_size = length - 1;
365 	else
366 		fast_chunk_size = 0;
367 	fast_chunk_size -= fast_chunk_size % cb7210_fifo_width;
368 	leftover = length - fast_chunk_size;
369 
370 	retval = fifo_write(board, buffer, fast_chunk_size, &num_bytes);
371 	*bytes_written += num_bytes;
372 	if (retval < 0)
373 		return retval;
374 
375 	retval = nec7210_write(board, nec_priv, buffer + fast_chunk_size, leftover,
376 			       send_eoi, &num_bytes);
377 	*bytes_written += num_bytes;
378 	return retval;
379 }
380 
cb7210_line_status(const struct gpib_board * board)381 static int cb7210_line_status(const struct gpib_board *board)
382 {
383 	int status = VALID_ALL;
384 	int bsr_bits;
385 	struct cb7210_priv *cb_priv;
386 
387 	cb_priv = board->private_data;
388 
389 	bsr_bits = cb7210_paged_read_byte(cb_priv, BUS_STATUS, BUS_STATUS_PAGE);
390 
391 	if ((bsr_bits & BSR_REN_BIT) == 0)
392 		status |= BUS_REN;
393 	if ((bsr_bits & BSR_IFC_BIT) == 0)
394 		status |= BUS_IFC;
395 	if ((bsr_bits & BSR_SRQ_BIT) == 0)
396 		status |= BUS_SRQ;
397 	if ((bsr_bits & BSR_EOI_BIT) == 0)
398 		status |= BUS_EOI;
399 	if ((bsr_bits & BSR_NRFD_BIT) == 0)
400 		status |= BUS_NRFD;
401 	if ((bsr_bits & BSR_NDAC_BIT) == 0)
402 		status |= BUS_NDAC;
403 	if ((bsr_bits & BSR_DAV_BIT) == 0)
404 		status |= BUS_DAV;
405 	if ((bsr_bits & BSR_ATN_BIT) == 0)
406 		status |= BUS_ATN;
407 
408 	return status;
409 }
410 
cb7210_t1_delay(struct gpib_board * board,unsigned int nano_sec)411 static int cb7210_t1_delay(struct gpib_board *board, unsigned int nano_sec)
412 {
413 	struct cb7210_priv *cb_priv = board->private_data;
414 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
415 	unsigned int retval;
416 
417 	retval = nec7210_t1_delay(board, nec_priv, nano_sec);
418 
419 	if (nano_sec <= 350) {
420 		write_byte(nec_priv, AUX_HI_SPEED, AUXMR);
421 		retval = 350;
422 	} else {
423 		write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
424 	}
425 	return retval;
426 }
427 
428 static irqreturn_t cb7210_locked_internal_interrupt(struct gpib_board *board);
429 
430 /*
431  * GPIB interrupt service routines
432  */
433 
cb_pci_interrupt(int irq,void * arg)434 static irqreturn_t cb_pci_interrupt(int irq, void *arg)
435 {
436 	int bits;
437 	struct gpib_board *board = arg;
438 	struct cb7210_priv *priv = board->private_data;
439 
440 	// first task check if this is really our interrupt in a shared irq environment
441 	switch (priv->pci_chip)	{
442 	case PCI_CHIP_AMCC_S5933:
443 		if ((inl(priv->amcc_iobase + INTCSR_REG) &
444 		     (INBOX_INTR_CS_BIT | INTR_ASSERTED_BIT)) == 0)
445 			return IRQ_NONE;
446 
447 		// read incoming mailbox to clear mailbox full flag
448 		inl(priv->amcc_iobase + INCOMING_MAILBOX_REG(3));
449 		// clear amccs5933 interrupt
450 		bits = INBOX_FULL_INTR_BIT | INBOX_BYTE_BITS(3) |
451 			INBOX_SELECT_BITS(3) |	INBOX_INTR_CS_BIT;
452 		outl(bits, priv->amcc_iobase + INTCSR_REG);
453 		break;
454 	case PCI_CHIP_QUANCOM:
455 		if ((inb(nec7210_iobase(priv) + QUANCOM_IRQ_CONTROL_STATUS_REG) &
456 		     QUANCOM_IRQ_ASSERTED_BIT))
457 			outb(QUANCOM_IRQ_ENABLE_BIT, nec7210_iobase(priv) +
458 			     QUANCOM_IRQ_CONTROL_STATUS_REG);
459 		break;
460 	default:
461 		break;
462 	}
463 	return cb7210_locked_internal_interrupt(arg);
464 }
465 
cb7210_internal_interrupt(struct gpib_board * board)466 static irqreturn_t cb7210_internal_interrupt(struct gpib_board *board)
467 {
468 	int hs_status, status1, status2;
469 	struct cb7210_priv *priv = board->private_data;
470 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
471 	int clear_bits;
472 
473 	if ((priv->hs_mode_bits & HS_ENABLE_MASK)) {
474 		status1 = 0;
475 		hs_status = cb7210_read_byte(priv, HS_STATUS);
476 	} else {
477 		hs_status = 0;
478 		status1 = read_byte(nec_priv, ISR1);
479 	}
480 	status2 = read_byte(nec_priv, ISR2);
481 	nec7210_interrupt_have_status(board, nec_priv, status1, status2);
482 
483 	dev_dbg(board->gpib_dev, "status 0x%x, mode 0x%x\n", hs_status, priv->hs_mode_bits);
484 
485 	clear_bits = 0;
486 
487 	if (hs_status & HS_HALF_FULL) {
488 		if (priv->hs_mode_bits & HS_TX_ENABLE)
489 			priv->out_fifo_half_empty = 1;
490 		else if (priv->hs_mode_bits & HS_RX_ENABLE)
491 			priv->in_fifo_half_full = 1;
492 		clear_bits |= HS_CLR_HF_INT;
493 	}
494 
495 	if (hs_status & HS_SRQ_INT) {
496 		set_bit(SRQI_NUM, &board->status);
497 		clear_bits |= HS_CLR_SRQ_INT;
498 	}
499 
500 	if ((hs_status & HS_EOI_INT)) {
501 		clear_bits |= HS_CLR_EOI_EMPTY_INT;
502 		set_bit(RECEIVED_END_BN, &nec_priv->state);
503 		if ((nec_priv->auxa_bits & HR_HANDSHAKE_MASK) == HR_HLDE)
504 			set_bit(RFD_HOLDOFF_BN, &nec_priv->state);
505 	}
506 
507 	if ((priv->hs_mode_bits & HS_TX_ENABLE) &&
508 	    (hs_status & (HS_TX_MSB_NOT_EMPTY | HS_TX_LSB_NOT_EMPTY)) == 0)
509 		clear_bits |= HS_CLR_EOI_EMPTY_INT;
510 
511 	if (clear_bits) {
512 		cb7210_write_byte(priv, priv->hs_mode_bits | clear_bits, HS_MODE);
513 		cb7210_write_byte(priv, priv->hs_mode_bits, HS_MODE);
514 		wake_up_interruptible(&board->wait);
515 	}
516 
517 	return IRQ_HANDLED;
518 }
519 
cb7210_locked_internal_interrupt(struct gpib_board * board)520 static irqreturn_t cb7210_locked_internal_interrupt(struct gpib_board *board)
521 {
522 	unsigned long flags;
523 	irqreturn_t retval;
524 
525 	spin_lock_irqsave(&board->spinlock, flags);
526 	retval = cb7210_internal_interrupt(board);
527 	spin_unlock_irqrestore(&board->spinlock, flags);
528 	return retval;
529 }
530 
cb7210_interrupt(int irq,void * arg)531 static irqreturn_t cb7210_interrupt(int irq, void *arg)
532 {
533 	return cb7210_internal_interrupt(arg);
534 }
535 
536 static int cb_pci_attach(struct gpib_board *board, const struct gpib_board_config *config);
537 static int cb_isa_attach(struct gpib_board *board, const struct gpib_board_config *config);
538 
539 static void cb_pci_detach(struct gpib_board *board);
540 static void cb_isa_detach(struct gpib_board *board);
541 
542 // wrappers for interface functions
cb7210_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)543 static int cb7210_read(struct gpib_board *board, u8 *buffer, size_t length,
544 		       int *end, size_t *bytes_read)
545 {
546 	struct cb7210_priv *priv = board->private_data;
547 
548 	return nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
549 }
550 
cb7210_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)551 static int cb7210_write(struct gpib_board *board, u8 *buffer, size_t length,
552 			int send_eoi, size_t *bytes_written)
553 {
554 	struct cb7210_priv *priv = board->private_data;
555 
556 	return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
557 }
558 
cb7210_command(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)559 static int cb7210_command(struct gpib_board *board, u8 *buffer, size_t length,
560 			  size_t *bytes_written)
561 {
562 	struct cb7210_priv *priv = board->private_data;
563 
564 	return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
565 }
566 
cb7210_take_control(struct gpib_board * board,int synchronous)567 static int cb7210_take_control(struct gpib_board *board, int synchronous)
568 {
569 	struct cb7210_priv *priv = board->private_data;
570 
571 	return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
572 }
573 
cb7210_go_to_standby(struct gpib_board * board)574 static int cb7210_go_to_standby(struct gpib_board *board)
575 {
576 	struct cb7210_priv *priv = board->private_data;
577 
578 	return nec7210_go_to_standby(board, &priv->nec7210_priv);
579 }
580 
cb7210_request_system_control(struct gpib_board * board,int request_control)581 static int cb7210_request_system_control(struct gpib_board *board, int request_control)
582 {
583 	struct cb7210_priv *priv = board->private_data;
584 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
585 
586 	if (request_control)
587 		priv->hs_mode_bits |= HS_SYS_CONTROL;
588 	else
589 		priv->hs_mode_bits &= ~HS_SYS_CONTROL;
590 
591 	cb7210_write_byte(priv, priv->hs_mode_bits, HS_MODE);
592 	return nec7210_request_system_control(board, nec_priv, request_control);
593 }
594 
cb7210_interface_clear(struct gpib_board * board,int assert)595 static void cb7210_interface_clear(struct gpib_board *board, int assert)
596 {
597 	struct cb7210_priv *priv = board->private_data;
598 
599 	nec7210_interface_clear(board, &priv->nec7210_priv, assert);
600 }
601 
cb7210_remote_enable(struct gpib_board * board,int enable)602 static void cb7210_remote_enable(struct gpib_board *board, int enable)
603 {
604 	struct cb7210_priv *priv = board->private_data;
605 
606 	nec7210_remote_enable(board, &priv->nec7210_priv, enable);
607 }
608 
cb7210_enable_eos(struct gpib_board * board,u8 eos_byte,int compare_8_bits)609 static int cb7210_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits)
610 {
611 	struct cb7210_priv *priv = board->private_data;
612 
613 	return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
614 }
615 
cb7210_disable_eos(struct gpib_board * board)616 static void cb7210_disable_eos(struct gpib_board *board)
617 {
618 	struct cb7210_priv *priv = board->private_data;
619 
620 	nec7210_disable_eos(board, &priv->nec7210_priv);
621 }
622 
cb7210_update_status(struct gpib_board * board,unsigned int clear_mask)623 static unsigned int cb7210_update_status(struct gpib_board *board, unsigned int clear_mask)
624 {
625 	struct cb7210_priv *priv = board->private_data;
626 
627 	return nec7210_update_status(board, &priv->nec7210_priv, clear_mask);
628 }
629 
cb7210_primary_address(struct gpib_board * board,unsigned int address)630 static int cb7210_primary_address(struct gpib_board *board, unsigned int address)
631 {
632 	struct cb7210_priv *priv = board->private_data;
633 
634 	return nec7210_primary_address(board, &priv->nec7210_priv, address);
635 }
636 
cb7210_secondary_address(struct gpib_board * board,unsigned int address,int enable)637 static int cb7210_secondary_address(struct gpib_board *board, unsigned int address, int enable)
638 {
639 	struct cb7210_priv *priv = board->private_data;
640 
641 	return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
642 }
643 
cb7210_parallel_poll(struct gpib_board * board,u8 * result)644 static int cb7210_parallel_poll(struct gpib_board *board, u8 *result)
645 {
646 	struct cb7210_priv *priv = board->private_data;
647 
648 	return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
649 }
650 
cb7210_parallel_poll_configure(struct gpib_board * board,u8 configuration)651 static void cb7210_parallel_poll_configure(struct gpib_board *board, u8 configuration)
652 {
653 	struct cb7210_priv *priv = board->private_data;
654 
655 	nec7210_parallel_poll_configure(board, &priv->nec7210_priv, configuration);
656 }
657 
cb7210_parallel_poll_response(struct gpib_board * board,int ist)658 static void cb7210_parallel_poll_response(struct gpib_board *board, int ist)
659 {
660 	struct cb7210_priv *priv = board->private_data;
661 
662 	nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
663 }
664 
cb7210_serial_poll_response(struct gpib_board * board,u8 status)665 static void cb7210_serial_poll_response(struct gpib_board *board, u8 status)
666 {
667 	struct cb7210_priv *priv = board->private_data;
668 
669 	nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
670 }
671 
cb7210_serial_poll_status(struct gpib_board * board)672 static u8 cb7210_serial_poll_status(struct gpib_board *board)
673 {
674 	struct cb7210_priv *priv = board->private_data;
675 
676 	return nec7210_serial_poll_status(board, &priv->nec7210_priv);
677 }
678 
cb7210_return_to_local(struct gpib_board * board)679 static void cb7210_return_to_local(struct gpib_board *board)
680 {
681 	struct cb7210_priv *priv = board->private_data;
682 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
683 
684 	write_byte(nec_priv, AUX_RTL2, AUXMR);
685 	udelay(1);
686 	write_byte(nec_priv, AUX_RTL, AUXMR);
687 }
688 
689 static struct gpib_interface cb_pci_unaccel_interface = {
690 	.name = "cbi_pci_unaccel",
691 	.attach = cb_pci_attach,
692 	.detach = cb_pci_detach,
693 	.read = cb7210_read,
694 	.write = cb7210_write,
695 	.command = cb7210_command,
696 	.take_control = cb7210_take_control,
697 	.go_to_standby = cb7210_go_to_standby,
698 	.request_system_control = cb7210_request_system_control,
699 	.interface_clear = cb7210_interface_clear,
700 	.remote_enable = cb7210_remote_enable,
701 	.enable_eos = cb7210_enable_eos,
702 	.disable_eos = cb7210_disable_eos,
703 	.parallel_poll = cb7210_parallel_poll,
704 	.parallel_poll_configure = cb7210_parallel_poll_configure,
705 	.parallel_poll_response = cb7210_parallel_poll_response,
706 	.local_parallel_poll_mode = NULL, // XXX
707 	.line_status = cb7210_line_status,
708 	.update_status = cb7210_update_status,
709 	.primary_address = cb7210_primary_address,
710 	.secondary_address = cb7210_secondary_address,
711 	.serial_poll_response = cb7210_serial_poll_response,
712 	.serial_poll_status = cb7210_serial_poll_status,
713 	.t1_delay = cb7210_t1_delay,
714 	.return_to_local = cb7210_return_to_local,
715 };
716 
717 static struct gpib_interface cb_pci_accel_interface = {
718 	.name = "cbi_pci_accel",
719 	.attach = cb_pci_attach,
720 	.detach = cb_pci_detach,
721 	.read = cb7210_accel_read,
722 	.write = cb7210_accel_write,
723 	.command = cb7210_command,
724 	.take_control = cb7210_take_control,
725 	.go_to_standby = cb7210_go_to_standby,
726 	.request_system_control = cb7210_request_system_control,
727 	.interface_clear = cb7210_interface_clear,
728 	.remote_enable = cb7210_remote_enable,
729 	.enable_eos = cb7210_enable_eos,
730 	.disable_eos = cb7210_disable_eos,
731 	.parallel_poll = cb7210_parallel_poll,
732 	.parallel_poll_configure = cb7210_parallel_poll_configure,
733 	.parallel_poll_response = cb7210_parallel_poll_response,
734 	.local_parallel_poll_mode = NULL, // XXX
735 	.line_status = cb7210_line_status,
736 	.update_status = cb7210_update_status,
737 	.primary_address = cb7210_primary_address,
738 	.secondary_address = cb7210_secondary_address,
739 	.serial_poll_response = cb7210_serial_poll_response,
740 	.serial_poll_status = cb7210_serial_poll_status,
741 	.t1_delay = cb7210_t1_delay,
742 	.return_to_local = cb7210_return_to_local,
743 };
744 
745 static struct gpib_interface cb_pci_interface = {
746 	.name = "cbi_pci",
747 	.attach = cb_pci_attach,
748 	.detach = cb_pci_detach,
749 	.read = cb7210_accel_read,
750 	.write = cb7210_accel_write,
751 	.command = cb7210_command,
752 	.take_control = cb7210_take_control,
753 	.go_to_standby = cb7210_go_to_standby,
754 	.request_system_control = cb7210_request_system_control,
755 	.interface_clear = cb7210_interface_clear,
756 	.remote_enable = cb7210_remote_enable,
757 	.enable_eos = cb7210_enable_eos,
758 	.disable_eos = cb7210_disable_eos,
759 	.parallel_poll = cb7210_parallel_poll,
760 	.parallel_poll_configure = cb7210_parallel_poll_configure,
761 	.parallel_poll_response = cb7210_parallel_poll_response,
762 	.line_status = cb7210_line_status,
763 	.update_status = cb7210_update_status,
764 	.primary_address = cb7210_primary_address,
765 	.secondary_address = cb7210_secondary_address,
766 	.serial_poll_response = cb7210_serial_poll_response,
767 	.serial_poll_status = cb7210_serial_poll_status,
768 	.t1_delay = cb7210_t1_delay,
769 	.return_to_local = cb7210_return_to_local,
770 };
771 
772 static struct gpib_interface cb_isa_unaccel_interface = {
773 	.name = "cbi_isa_unaccel",
774 	.attach = cb_isa_attach,
775 	.detach = cb_isa_detach,
776 	.read = cb7210_read,
777 	.write = cb7210_write,
778 	.command = cb7210_command,
779 	.take_control = cb7210_take_control,
780 	.go_to_standby = cb7210_go_to_standby,
781 	.request_system_control = cb7210_request_system_control,
782 	.interface_clear = cb7210_interface_clear,
783 	.remote_enable = cb7210_remote_enable,
784 	.enable_eos = cb7210_enable_eos,
785 	.disable_eos = cb7210_disable_eos,
786 	.parallel_poll = cb7210_parallel_poll,
787 	.parallel_poll_configure = cb7210_parallel_poll_configure,
788 	.parallel_poll_response = cb7210_parallel_poll_response,
789 	.local_parallel_poll_mode = NULL, // XXX
790 	.line_status = cb7210_line_status,
791 	.update_status = cb7210_update_status,
792 	.primary_address = cb7210_primary_address,
793 	.secondary_address = cb7210_secondary_address,
794 	.serial_poll_response = cb7210_serial_poll_response,
795 	.serial_poll_status = cb7210_serial_poll_status,
796 	.t1_delay = cb7210_t1_delay,
797 	.return_to_local = cb7210_return_to_local,
798 };
799 
800 static struct gpib_interface cb_isa_interface = {
801 	.name = "cbi_isa",
802 	.attach = cb_isa_attach,
803 	.detach = cb_isa_detach,
804 	.read = cb7210_accel_read,
805 	.write = cb7210_accel_write,
806 	.command = cb7210_command,
807 	.take_control = cb7210_take_control,
808 	.go_to_standby = cb7210_go_to_standby,
809 	.request_system_control = cb7210_request_system_control,
810 	.interface_clear = cb7210_interface_clear,
811 	.remote_enable = cb7210_remote_enable,
812 	.enable_eos = cb7210_enable_eos,
813 	.disable_eos = cb7210_disable_eos,
814 	.parallel_poll = cb7210_parallel_poll,
815 	.parallel_poll_configure = cb7210_parallel_poll_configure,
816 	.parallel_poll_response = cb7210_parallel_poll_response,
817 	.line_status = cb7210_line_status,
818 	.update_status = cb7210_update_status,
819 	.primary_address = cb7210_primary_address,
820 	.secondary_address = cb7210_secondary_address,
821 	.serial_poll_response = cb7210_serial_poll_response,
822 	.serial_poll_status = cb7210_serial_poll_status,
823 	.t1_delay = cb7210_t1_delay,
824 	.return_to_local = cb7210_return_to_local,
825 };
826 
827 static struct gpib_interface cb_isa_accel_interface = {
828 	.name = "cbi_isa_accel",
829 	.attach = cb_isa_attach,
830 	.detach = cb_isa_detach,
831 	.read = cb7210_accel_read,
832 	.write = cb7210_accel_write,
833 	.command = cb7210_command,
834 	.take_control = cb7210_take_control,
835 	.go_to_standby = cb7210_go_to_standby,
836 	.request_system_control = cb7210_request_system_control,
837 	.interface_clear = cb7210_interface_clear,
838 	.remote_enable = cb7210_remote_enable,
839 	.enable_eos = cb7210_enable_eos,
840 	.disable_eos = cb7210_disable_eos,
841 	.parallel_poll = cb7210_parallel_poll,
842 	.parallel_poll_configure = cb7210_parallel_poll_configure,
843 	.parallel_poll_response = cb7210_parallel_poll_response,
844 	.local_parallel_poll_mode = NULL, // XXX
845 	.line_status = cb7210_line_status,
846 	.update_status = cb7210_update_status,
847 	.primary_address = cb7210_primary_address,
848 	.secondary_address = cb7210_secondary_address,
849 	.serial_poll_response = cb7210_serial_poll_response,
850 	.serial_poll_status = cb7210_serial_poll_status,
851 	.t1_delay = cb7210_t1_delay,
852 	.return_to_local = cb7210_return_to_local,
853 };
854 
cb7210_allocate_private(struct gpib_board * board)855 static int cb7210_allocate_private(struct gpib_board *board)
856 {
857 	struct cb7210_priv *priv;
858 
859 	board->private_data = kzalloc_obj(struct cb7210_priv, GFP_KERNEL);
860 	if (!board->private_data)
861 		return -ENOMEM;
862 	priv = board->private_data;
863 	init_nec7210_private(&priv->nec7210_priv);
864 	return 0;
865 }
866 
cb7210_generic_detach(struct gpib_board * board)867 static void cb7210_generic_detach(struct gpib_board *board)
868 {
869 	kfree(board->private_data);
870 	board->private_data = NULL;
871 }
872 
873 // generic part of attach functions shared by all cb7210 boards
cb7210_generic_attach(struct gpib_board * board)874 static int cb7210_generic_attach(struct gpib_board *board)
875 {
876 	struct cb7210_priv *cb_priv;
877 	struct nec7210_priv *nec_priv;
878 	int retval;
879 
880 	board->status = 0;
881 
882 	retval = cb7210_allocate_private(board);
883 	if (retval)
884 		return retval;
885 	cb_priv = board->private_data;
886 	nec_priv = &cb_priv->nec7210_priv;
887 	nec_priv->read_byte = nec7210_locking_ioport_read_byte;
888 	nec_priv->write_byte = nec7210_locking_ioport_write_byte;
889 	nec_priv->offset = cb7210_reg_offset;
890 	nec_priv->type = CB7210;
891 	return 0;
892 }
893 
cb7210_init(struct cb7210_priv * cb_priv,struct gpib_board * board)894 static int cb7210_init(struct cb7210_priv *cb_priv, struct gpib_board *board)
895 {
896 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
897 
898 	cb7210_write_byte(cb_priv, HS_RESET7210, HS_INT_LEVEL);
899 	cb7210_write_byte(cb_priv, irq_bits(cb_priv->irq), HS_INT_LEVEL);
900 
901 	nec7210_board_reset(nec_priv, board);
902 	cb7210_write_byte(cb_priv, HS_TX_ENABLE | HS_RX_ENABLE | HS_CLR_SRQ_INT |
903 			  HS_CLR_EOI_EMPTY_INT | HS_CLR_HF_INT, HS_MODE);
904 
905 	cb_priv->hs_mode_bits = HS_HF_INT_EN;
906 	cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, HS_MODE);
907 
908 	write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
909 	/*
910 	 * set clock register for maximum (20 MHz) driving frequency
911 	 * ICR should be set to clock in megahertz (1-15) and to zero
912 	 * for clocks faster than 15 MHz (max 20MHz)
913 	 */
914 	write_byte(nec_priv, ICR | 0, AUXMR);
915 
916 	if (cb_priv->pci_chip == PCI_CHIP_QUANCOM) {
917 		/* change interrupt polarity */
918 		nec_priv->auxb_bits |= HR_INV;
919 		write_byte(nec_priv, nec_priv->auxb_bits, AUXMR);
920 	}
921 	nec7210_board_online(nec_priv, board);
922 
923 	/* poll so we can detect assertion of ATN */
924 	if (gpib_request_pseudo_irq(board, cb_pci_interrupt)) {
925 		pr_err("failed to allocate pseudo_irq\n");
926 		return -1;
927 	}
928 	return 0;
929 }
930 
cb_pci_attach(struct gpib_board * board,const struct gpib_board_config * config)931 static int cb_pci_attach(struct gpib_board *board, const struct gpib_board_config *config)
932 {
933 	struct cb7210_priv *cb_priv;
934 	struct nec7210_priv *nec_priv;
935 	int isr_flags = 0;
936 	int bits;
937 	int retval;
938 
939 	retval = cb7210_generic_attach(board);
940 	if (retval)
941 		return retval;
942 
943 	cb_priv = board->private_data;
944 	nec_priv = &cb_priv->nec7210_priv;
945 
946 	cb_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_CBOARDS,
947 						  PCI_DEVICE_ID_CBOARDS_PCI_GPIB, NULL);
948 	if (cb_priv->pci_device)
949 		cb_priv->pci_chip = PCI_CHIP_AMCC_S5933;
950 	if (!cb_priv->pci_device) {
951 		cb_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_CBOARDS,
952 							  PCI_DEVICE_ID_CBOARDS_CPCI_GPIB, NULL);
953 		if (cb_priv->pci_device)
954 			cb_priv->pci_chip = PCI_CHIP_AMCC_S5933;
955 	}
956 	if (!cb_priv->pci_device) {
957 		cb_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_QUANCOM,
958 							  PCI_DEVICE_ID_QUANCOM_GPIB, NULL);
959 		if (cb_priv->pci_device) {
960 			cb_priv->pci_chip = PCI_CHIP_QUANCOM;
961 			nec_priv->offset = 4;
962 		}
963 	}
964 	if (!cb_priv->pci_device) {
965 		dev_err(board->gpib_dev, "no supported boards found.\n");
966 		return -ENODEV;
967 	}
968 
969 	if (pci_enable_device(cb_priv->pci_device)) {
970 		dev_err(board->gpib_dev, "error enabling pci device\n");
971 		return -EIO;
972 	}
973 
974 	if (pci_request_regions(cb_priv->pci_device, DRV_NAME))
975 		return -EBUSY;
976 	switch (cb_priv->pci_chip) {
977 	case PCI_CHIP_AMCC_S5933:
978 		cb_priv->amcc_iobase = pci_resource_start(cb_priv->pci_device, 0);
979 		nec_priv->iobase = pci_resource_start(cb_priv->pci_device, 1);
980 		cb_priv->fifo_iobase = pci_resource_start(cb_priv->pci_device, 2);
981 		break;
982 	case PCI_CHIP_QUANCOM:
983 		nec_priv->iobase = pci_resource_start(cb_priv->pci_device, 0);
984 		cb_priv->fifo_iobase = nec_priv->iobase;
985 		break;
986 	default:
987 		dev_err(board->gpib_dev, "bug! unhandled pci_chip=%i\n", cb_priv->pci_chip);
988 		return -EIO;
989 	}
990 	isr_flags |= IRQF_SHARED;
991 	if (request_irq(cb_priv->pci_device->irq, cb_pci_interrupt, isr_flags, DRV_NAME, board)) {
992 		dev_err(board->gpib_dev, "can't request IRQ %d\n",
993 			cb_priv->pci_device->irq);
994 		return -EBUSY;
995 	}
996 	cb_priv->irq = cb_priv->pci_device->irq;
997 
998 	switch (cb_priv->pci_chip) {
999 	case PCI_CHIP_AMCC_S5933:
1000 		// make sure mailbox flags are clear
1001 		inl(cb_priv->amcc_iobase + INCOMING_MAILBOX_REG(3));
1002 		// enable interrupts on amccs5933 chip
1003 		bits = INBOX_FULL_INTR_BIT | INBOX_BYTE_BITS(3) | INBOX_SELECT_BITS(3) |
1004 			INBOX_INTR_CS_BIT;
1005 		outl(bits, cb_priv->amcc_iobase + INTCSR_REG);
1006 		break;
1007 	default:
1008 		break;
1009 	}
1010 	return cb7210_init(cb_priv, board);
1011 }
1012 
cb_pci_detach(struct gpib_board * board)1013 static void cb_pci_detach(struct gpib_board *board)
1014 {
1015 	struct cb7210_priv *cb_priv = board->private_data;
1016 	struct nec7210_priv *nec_priv;
1017 
1018 	if (cb_priv) {
1019 		gpib_free_pseudo_irq(board);
1020 		nec_priv = &cb_priv->nec7210_priv;
1021 		if (cb_priv->irq) {
1022 			// disable amcc interrupts
1023 			outl(0, cb_priv->amcc_iobase + INTCSR_REG);
1024 			free_irq(cb_priv->irq, board);
1025 		}
1026 		if (nec_priv->iobase) {
1027 			nec7210_board_reset(nec_priv, board);
1028 			pci_release_regions(cb_priv->pci_device);
1029 		}
1030 		if (cb_priv->pci_device)
1031 			pci_dev_put(cb_priv->pci_device);
1032 	}
1033 	cb7210_generic_detach(board);
1034 }
1035 
cb_isa_attach(struct gpib_board * board,const struct gpib_board_config * config)1036 static int cb_isa_attach(struct gpib_board *board, const struct gpib_board_config *config)
1037 {
1038 	int isr_flags = 0;
1039 	struct cb7210_priv *cb_priv;
1040 	struct nec7210_priv *nec_priv;
1041 	unsigned int bits;
1042 	int retval;
1043 
1044 	retval = cb7210_generic_attach(board);
1045 	if (retval)
1046 		return retval;
1047 	cb_priv = board->private_data;
1048 	nec_priv = &cb_priv->nec7210_priv;
1049 	if (!request_region(config->ibbase, cb7210_iosize, DRV_NAME)) {
1050 		dev_err(board->gpib_dev, "ioports starting at 0x%x are already in use\n",
1051 			config->ibbase);
1052 		return -EBUSY;
1053 	}
1054 	nec_priv->iobase = config->ibbase;
1055 	cb_priv->fifo_iobase = nec7210_iobase(cb_priv);
1056 
1057 	bits = irq_bits(config->ibirq);
1058 	if (bits == 0)
1059 		dev_err(board->gpib_dev, "board incapable of using irq %i, try 2-5, 7, 10, or 11\n",
1060 			config->ibirq);
1061 
1062 	// install interrupt handler
1063 	if (request_irq(config->ibirq, cb7210_interrupt, isr_flags, DRV_NAME, board)) {
1064 		dev_err(board->gpib_dev, "failed to obtain IRQ %d\n", config->ibirq);
1065 		return -EBUSY;
1066 	}
1067 	cb_priv->irq = config->ibirq;
1068 
1069 	return cb7210_init(cb_priv, board);
1070 }
1071 
cb_isa_detach(struct gpib_board * board)1072 static void cb_isa_detach(struct gpib_board *board)
1073 {
1074 	struct cb7210_priv *cb_priv = board->private_data;
1075 	struct nec7210_priv *nec_priv;
1076 
1077 	if (cb_priv) {
1078 		gpib_free_pseudo_irq(board);
1079 		nec_priv = &cb_priv->nec7210_priv;
1080 		if (cb_priv->irq)
1081 			free_irq(cb_priv->irq, board);
1082 		if (nec_priv->iobase) {
1083 			nec7210_board_reset(nec_priv, board);
1084 			release_region(nec7210_iobase(cb_priv), cb7210_iosize);
1085 		}
1086 	}
1087 	cb7210_generic_detach(board);
1088 }
1089 
cb7210_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)1090 static int cb7210_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1091 {
1092 	return 0;
1093 }
1094 
1095 static const struct pci_device_id cb7210_pci_table[] = {
1096 	{PCI_VENDOR_ID_CBOARDS, PCI_DEVICE_ID_CBOARDS_PCI_GPIB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1097 	{PCI_VENDOR_ID_CBOARDS, PCI_DEVICE_ID_CBOARDS_CPCI_GPIB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1098 	{PCI_VENDOR_ID_QUANCOM, PCI_DEVICE_ID_QUANCOM_GPIB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1099 	{ 0 }
1100 };
1101 MODULE_DEVICE_TABLE(pci, cb7210_pci_table);
1102 
1103 static struct pci_driver cb7210_pci_driver = {
1104 	.name = DRV_NAME,
1105 	.id_table = cb7210_pci_table,
1106 	.probe = &cb7210_pci_probe
1107 };
1108 
1109 /***************************************************************************
1110  *  Support for computer boards pcmcia-gpib card
1111  *
1112  *  Based on gpib PCMCIA client driver written by Claus Schroeter
1113  *  (clausi@chemie.fu-berlin.de), which was adapted from the
1114  *  pcmcia skeleton example (presumably David Hinds)
1115  ***************************************************************************/
1116 
1117 #ifdef CONFIG_GPIB_PCMCIA
1118 
1119 #include <linux/kernel.h>
1120 #include <linux/ptrace.h>
1121 #include <linux/timer.h>
1122 #include <linux/io.h>
1123 
1124 #include <pcmcia/cistpl.h>
1125 #include <pcmcia/ds.h>
1126 
1127 /*
1128  * The event() function is this driver's Card Services event handler.
1129  * It will be called by Card Services when an appropriate card status
1130  * event is received.  The config() and release() entry points are
1131  * used to configure or release a socket, in response to card insertion
1132  * and ejection events.	 They are invoked from the gpib event
1133  * handler.
1134  */
1135 
1136 static int cb_gpib_config(struct pcmcia_device	*link);
1137 static void cb_gpib_release(struct pcmcia_device  *link);
1138 static int cb_pcmcia_attach(struct gpib_board *board, const struct gpib_board_config *config);
1139 static void cb_pcmcia_detach(struct gpib_board *board);
1140 
1141 /*
1142  *  A linked list of "instances" of the gpib device.  Each actual
1143  *  PCMCIA card corresponds to one device instance, and is described
1144  *  by one dev_link_t structure (defined in ds.h).
1145  *
1146  *  You may not want to use a linked list for this -- for example, the
1147  *  memory card driver uses an array of dev_link_t pointers, where minor
1148  *  device numbers are used to derive the corresponding array index.
1149  */
1150 
1151 static	struct pcmcia_device  *curr_dev;
1152 
1153 /*
1154  *  A dev_link_t structure has fields for most things that are needed
1155  *  to keep track of a socket, but there will usually be some device
1156  *  specific information that also needs to be kept track of.  The
1157  *  'priv' pointer in a dev_link_t structure can be used to point to
1158  *  a device-specific private data structure, like this.
1159  *
1160  *  A driver needs to provide a dev_node_t structure for each device
1161  *  on a card.	In some cases, there is only one device per card (for
1162  *  example, ethernet cards, modems).  In other cases, there may be
1163  *  many actual or logical devices (SCSI adapters, memory cards with
1164  *  multiple partitions).  The dev_node_t structures need to be kept
1165  *  in a linked list starting at the 'dev' field of a dev_link_t
1166  *  structure.	We allocate them in the card's private data structure,
1167  * because they generally can't be allocated dynamically.
1168  */
1169 
1170 struct local_info {
1171 	struct pcmcia_device	*p_dev;
1172 	struct gpib_board		*dev;
1173 };
1174 
1175 /*
1176  *  gpib_attach() creates an "instance" of the driver, allocating
1177  *  local data structures for one device.  The device is registered
1178  *  with Card Services.
1179  *
1180  *  The dev_link structure is initialized, but we don't actually
1181  *  configure the card at this point -- we wait until we receive a
1182  *  card insertion event.
1183  */
1184 
cb_gpib_probe(struct pcmcia_device * link)1185 static int cb_gpib_probe(struct pcmcia_device *link)
1186 {
1187 	struct local_info *info;
1188 	int ret;
1189 
1190 	/* Allocate space for private device-specific data */
1191 	info = kzalloc_obj(*info, GFP_KERNEL);
1192 	if (!info)
1193 		return -ENOMEM;
1194 
1195 	info->p_dev = link;
1196 	link->priv = info;
1197 
1198 	/* The io structure describes IO port mapping */
1199 	link->resource[0]->end = 16;
1200 	link->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
1201 	link->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
1202 	link->resource[1]->end = 16;
1203 	link->resource[1]->flags &= ~IO_DATA_PATH_WIDTH;
1204 	link->resource[1]->flags |= IO_DATA_PATH_WIDTH_16;
1205 	link->io_lines = 10;
1206 
1207 	/* General socket configuration */
1208 	link->config_flags = CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
1209 	link->config_index = 1;
1210 	link->config_regs = PRESENT_OPTION;
1211 
1212 	/* Register with Card Services */
1213 	curr_dev = link;
1214 	ret = cb_gpib_config(link);
1215 	if (ret)
1216 		goto free_info;
1217 
1218 	return 0;
1219 
1220 free_info:
1221 	kfree(info);
1222 	return ret;
1223 }
1224 
1225 /*
1226  *   This deletes a driver "instance".  The device is de-registered
1227  *   with Card Services.  If it has been released, all local data
1228  *   structures are freed.  Otherwise, the structures will be freed
1229  *   when the device is released.
1230  */
1231 
cb_gpib_remove(struct pcmcia_device * link)1232 static void cb_gpib_remove(struct pcmcia_device *link)
1233 {
1234 	struct local_info *info = link->priv;
1235 	//struct struct gpib_board *dev = info->dev;
1236 
1237 	if (info->dev)
1238 		cb_pcmcia_detach(info->dev);
1239 	cb_gpib_release(link);
1240 
1241 	//free_netdev(dev);
1242 	kfree(info);
1243 }
1244 
cb_gpib_config_iteration(struct pcmcia_device * link,void * priv_data)1245 static int cb_gpib_config_iteration(struct pcmcia_device *link, void *priv_data)
1246 {
1247 	return pcmcia_request_io(link);
1248 }
1249 
1250 /*
1251  *   gpib_config() is scheduled to run after a CARD_INSERTION event
1252  *   is received, to configure the PCMCIA socket, and to make the
1253  *   ethernet device available to the system.
1254  */
1255 
cb_gpib_config(struct pcmcia_device * link)1256 static int cb_gpib_config(struct pcmcia_device  *link)
1257 {
1258 	int retval;
1259 
1260 	retval = pcmcia_loop_config(link, &cb_gpib_config_iteration, NULL);
1261 	if (retval) {
1262 		dev_warn(&link->dev, "no configuration found\n");
1263 		cb_gpib_release(link);
1264 		return -ENODEV;
1265 	}
1266 
1267 	/*
1268 	 *  This actually configures the PCMCIA socket -- setting up
1269 	 *  the I/O windows and the interrupt mapping.
1270 	 */
1271 	retval = pcmcia_enable_device(link);
1272 	if (retval) {
1273 		dev_warn(&link->dev, "pcmcia_enable_device failed\n");
1274 		cb_gpib_release(link);
1275 		return -ENODEV;
1276 	}
1277 
1278 	return 0;
1279 } /* gpib_config */
1280 
1281 /*
1282  * After a card is removed, gpib_release() will unregister the net
1283  * device, and release the PCMCIA configuration.  If the device is
1284  * still open, this will be postponed until it is closed.
1285  */
1286 
cb_gpib_release(struct pcmcia_device * link)1287 static void cb_gpib_release(struct pcmcia_device *link)
1288 {
1289 	pcmcia_disable_device(link);
1290 }
1291 
cb_gpib_suspend(struct pcmcia_device * link)1292 static int cb_gpib_suspend(struct pcmcia_device *link)
1293 {
1294 	if (link->open)
1295 		dev_warn(&link->dev, "Device still open\n");
1296 
1297 	return 0;
1298 }
1299 
cb_gpib_resume(struct pcmcia_device * link)1300 static int cb_gpib_resume(struct pcmcia_device *link)
1301 {
1302 	return cb_gpib_config(link);
1303 }
1304 
1305 /*====================================================================*/
1306 
1307 static struct pcmcia_device_id cb_pcmcia_ids[] = {
1308 	PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x0005),
1309 	PCMCIA_DEVICE_NULL
1310 };
1311 MODULE_DEVICE_TABLE(pcmcia, cb_pcmcia_ids);
1312 
1313 static struct pcmcia_driver cb_gpib_cs_driver = {
1314 	.name           = "cb_gpib_cs",
1315 	.owner		= THIS_MODULE,
1316 	.id_table	= cb_pcmcia_ids,
1317 	.probe		= cb_gpib_probe,
1318 	.remove		= cb_gpib_remove,
1319 	.suspend	= cb_gpib_suspend,
1320 	.resume		= cb_gpib_resume,
1321 };
1322 
cb_pcmcia_cleanup_module(void)1323 static void cb_pcmcia_cleanup_module(void)
1324 {
1325 	pcmcia_unregister_driver(&cb_gpib_cs_driver);
1326 }
1327 
1328 static struct gpib_interface cb_pcmcia_unaccel_interface = {
1329 	.name = "cbi_pcmcia_unaccel",
1330 	.attach = cb_pcmcia_attach,
1331 	.detach = cb_pcmcia_detach,
1332 	.read = cb7210_read,
1333 	.write = cb7210_write,
1334 	.command = cb7210_command,
1335 	.take_control = cb7210_take_control,
1336 	.go_to_standby = cb7210_go_to_standby,
1337 	.request_system_control = cb7210_request_system_control,
1338 	.interface_clear = cb7210_interface_clear,
1339 	.remote_enable = cb7210_remote_enable,
1340 	.enable_eos = cb7210_enable_eos,
1341 	.disable_eos = cb7210_disable_eos,
1342 	.parallel_poll = cb7210_parallel_poll,
1343 	.parallel_poll_configure = cb7210_parallel_poll_configure,
1344 	.parallel_poll_response = cb7210_parallel_poll_response,
1345 	.local_parallel_poll_mode = NULL, // XXX
1346 	.line_status = cb7210_line_status,
1347 	.update_status = cb7210_update_status,
1348 	.primary_address = cb7210_primary_address,
1349 	.secondary_address = cb7210_secondary_address,
1350 	.serial_poll_response = cb7210_serial_poll_response,
1351 	.serial_poll_status = cb7210_serial_poll_status,
1352 	.t1_delay = cb7210_t1_delay,
1353 	.return_to_local = cb7210_return_to_local,
1354 };
1355 
1356 static struct gpib_interface cb_pcmcia_interface = {
1357 	.name = "cbi_pcmcia",
1358 	.attach = cb_pcmcia_attach,
1359 	.detach = cb_pcmcia_detach,
1360 	.read = cb7210_accel_read,
1361 	.write = cb7210_accel_write,
1362 	.command = cb7210_command,
1363 	.take_control = cb7210_take_control,
1364 	.go_to_standby = cb7210_go_to_standby,
1365 	.request_system_control = cb7210_request_system_control,
1366 	.interface_clear = cb7210_interface_clear,
1367 	.remote_enable = cb7210_remote_enable,
1368 	.enable_eos = cb7210_enable_eos,
1369 	.disable_eos = cb7210_disable_eos,
1370 	.parallel_poll = cb7210_parallel_poll,
1371 	.parallel_poll_configure = cb7210_parallel_poll_configure,
1372 	.parallel_poll_response = cb7210_parallel_poll_response,
1373 	.local_parallel_poll_mode = NULL, // XXX
1374 	.line_status = cb7210_line_status,
1375 	.update_status = cb7210_update_status,
1376 	.primary_address = cb7210_primary_address,
1377 	.secondary_address = cb7210_secondary_address,
1378 	.serial_poll_response = cb7210_serial_poll_response,
1379 	.serial_poll_status = cb7210_serial_poll_status,
1380 	.t1_delay = cb7210_t1_delay,
1381 	.return_to_local = cb7210_return_to_local,
1382 };
1383 
1384 static struct gpib_interface cb_pcmcia_accel_interface = {
1385 	.name = "cbi_pcmcia_accel",
1386 	.attach = cb_pcmcia_attach,
1387 	.detach = cb_pcmcia_detach,
1388 	.read = cb7210_accel_read,
1389 	.write = cb7210_accel_write,
1390 	.command = cb7210_command,
1391 	.take_control = cb7210_take_control,
1392 	.go_to_standby = cb7210_go_to_standby,
1393 	.request_system_control = cb7210_request_system_control,
1394 	.interface_clear = cb7210_interface_clear,
1395 	.remote_enable = cb7210_remote_enable,
1396 	.enable_eos = cb7210_enable_eos,
1397 	.disable_eos = cb7210_disable_eos,
1398 	.parallel_poll = cb7210_parallel_poll,
1399 	.parallel_poll_configure = cb7210_parallel_poll_configure,
1400 	.parallel_poll_response = cb7210_parallel_poll_response,
1401 	.local_parallel_poll_mode = NULL, // XXX
1402 	.line_status = cb7210_line_status,
1403 	.update_status = cb7210_update_status,
1404 	.primary_address = cb7210_primary_address,
1405 	.secondary_address = cb7210_secondary_address,
1406 	.serial_poll_response = cb7210_serial_poll_response,
1407 	.serial_poll_status = cb7210_serial_poll_status,
1408 	.t1_delay = cb7210_t1_delay,
1409 	.return_to_local = cb7210_return_to_local,
1410 };
1411 
cb_pcmcia_attach(struct gpib_board * board,const struct gpib_board_config * config)1412 static int cb_pcmcia_attach(struct gpib_board *board, const struct gpib_board_config *config)
1413 {
1414 	struct cb7210_priv *cb_priv;
1415 	struct nec7210_priv *nec_priv;
1416 	int retval;
1417 
1418 	if (!curr_dev) {
1419 		dev_err(board->gpib_dev, "no cb pcmcia cards found\n");
1420 		return -ENODEV;
1421 	}
1422 
1423 	retval = cb7210_generic_attach(board);
1424 	if (retval)
1425 		return retval;
1426 
1427 	cb_priv = board->private_data;
1428 	nec_priv = &cb_priv->nec7210_priv;
1429 
1430 	if (!request_region(curr_dev->resource[0]->start, resource_size(curr_dev->resource[0]),
1431 			    DRV_NAME))	{
1432 		dev_err(board->gpib_dev, "ioports starting at 0x%lx are already in use\n",
1433 			(unsigned long)curr_dev->resource[0]->start);
1434 		return -EBUSY;
1435 	}
1436 	nec_priv->iobase = curr_dev->resource[0]->start;
1437 	cb_priv->fifo_iobase = curr_dev->resource[0]->start;
1438 
1439 	if (request_irq(curr_dev->irq, cb7210_interrupt, IRQF_SHARED, DRV_NAME, board)) {
1440 		dev_err(board->gpib_dev, "failed to request IRQ %d\n", curr_dev->irq);
1441 		return -EBUSY;
1442 	}
1443 	cb_priv->irq = curr_dev->irq;
1444 
1445 	return cb7210_init(cb_priv, board);
1446 }
1447 
cb_pcmcia_detach(struct gpib_board * board)1448 static void cb_pcmcia_detach(struct gpib_board *board)
1449 {
1450 	struct cb7210_priv *cb_priv = board->private_data;
1451 	struct nec7210_priv *nec_priv;
1452 
1453 	if (cb_priv) {
1454 		nec_priv = &cb_priv->nec7210_priv;
1455 		gpib_free_pseudo_irq(board);
1456 		if (cb_priv->irq)
1457 			free_irq(cb_priv->irq, board);
1458 		if (nec_priv->iobase) {
1459 			nec7210_board_reset(nec_priv, board);
1460 			release_region(nec7210_iobase(cb_priv), cb7210_iosize);
1461 		}
1462 	}
1463 	cb7210_generic_detach(board);
1464 }
1465 
1466 #endif /* CONFIG_GPIB_PCMCIA */
1467 
cb7210_init_module(void)1468 static int __init cb7210_init_module(void)
1469 {
1470 	int ret;
1471 
1472 	ret = pci_register_driver(&cb7210_pci_driver);
1473 	if (ret) {
1474 		pr_err("pci_register_driver failed: error = %d\n", ret);
1475 		return ret;
1476 	}
1477 
1478 	ret = gpib_register_driver(&cb_pci_interface, THIS_MODULE);
1479 	if (ret) {
1480 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1481 		goto err_pci;
1482 	}
1483 
1484 	ret = gpib_register_driver(&cb_isa_interface, THIS_MODULE);
1485 	if (ret) {
1486 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1487 		goto err_isa;
1488 	}
1489 
1490 	ret = gpib_register_driver(&cb_pci_accel_interface, THIS_MODULE);
1491 	if (ret) {
1492 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1493 		goto err_pci_accel;
1494 	}
1495 
1496 	ret = gpib_register_driver(&cb_pci_unaccel_interface, THIS_MODULE);
1497 	if (ret) {
1498 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1499 		goto err_pci_unaccel;
1500 	}
1501 
1502 	ret = gpib_register_driver(&cb_isa_accel_interface, THIS_MODULE);
1503 	if (ret) {
1504 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1505 		goto err_isa_accel;
1506 	}
1507 
1508 	ret = gpib_register_driver(&cb_isa_unaccel_interface, THIS_MODULE);
1509 	if (ret) {
1510 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1511 		goto err_isa_unaccel;
1512 	}
1513 
1514 #ifdef CONFIG_GPIB_PCMCIA
1515 	ret = gpib_register_driver(&cb_pcmcia_interface, THIS_MODULE);
1516 	if (ret) {
1517 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1518 		goto err_pcmcia;
1519 	}
1520 
1521 	ret = gpib_register_driver(&cb_pcmcia_accel_interface, THIS_MODULE);
1522 	if (ret) {
1523 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1524 		goto err_pcmcia_accel;
1525 	}
1526 
1527 	ret = gpib_register_driver(&cb_pcmcia_unaccel_interface, THIS_MODULE);
1528 	if (ret) {
1529 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1530 		goto err_pcmcia_unaccel;
1531 	}
1532 
1533 	ret = pcmcia_register_driver(&cb_gpib_cs_driver);
1534 	if (ret) {
1535 		pr_err("pcmcia_register_driver failed: error = %d\n", ret);
1536 		goto err_pcmcia_driver;
1537 	}
1538 #endif
1539 
1540 	return 0;
1541 
1542 #ifdef CONFIG_GPIB_PCMCIA
1543 err_pcmcia_driver:
1544 	gpib_unregister_driver(&cb_pcmcia_unaccel_interface);
1545 err_pcmcia_unaccel:
1546 	gpib_unregister_driver(&cb_pcmcia_accel_interface);
1547 err_pcmcia_accel:
1548 	gpib_unregister_driver(&cb_pcmcia_interface);
1549 err_pcmcia:
1550 #endif
1551 	gpib_unregister_driver(&cb_isa_unaccel_interface);
1552 err_isa_unaccel:
1553 	gpib_unregister_driver(&cb_isa_accel_interface);
1554 err_isa_accel:
1555 	gpib_unregister_driver(&cb_pci_unaccel_interface);
1556 err_pci_unaccel:
1557 	gpib_unregister_driver(&cb_pci_accel_interface);
1558 err_pci_accel:
1559 	gpib_unregister_driver(&cb_isa_interface);
1560 err_isa:
1561 	gpib_unregister_driver(&cb_pci_interface);
1562 err_pci:
1563 	pci_unregister_driver(&cb7210_pci_driver);
1564 
1565 	return ret;
1566 }
1567 
cb7210_exit_module(void)1568 static void __exit cb7210_exit_module(void)
1569 {
1570 	gpib_unregister_driver(&cb_pci_interface);
1571 	gpib_unregister_driver(&cb_isa_interface);
1572 	gpib_unregister_driver(&cb_pci_accel_interface);
1573 	gpib_unregister_driver(&cb_pci_unaccel_interface);
1574 	gpib_unregister_driver(&cb_isa_accel_interface);
1575 	gpib_unregister_driver(&cb_isa_unaccel_interface);
1576 #ifdef CONFIG_GPIB_PCMCIA
1577 	gpib_unregister_driver(&cb_pcmcia_interface);
1578 	gpib_unregister_driver(&cb_pcmcia_accel_interface);
1579 	gpib_unregister_driver(&cb_pcmcia_unaccel_interface);
1580 	cb_pcmcia_cleanup_module();
1581 #endif
1582 
1583 	pci_unregister_driver(&cb7210_pci_driver);
1584 }
1585 
1586 module_init(cb7210_init_module);
1587 module_exit(cb7210_exit_module);
1588