xref: /linux/drivers/gpib/cb7210/cb7210.c (revision 2cddfc2e8fc78c13b0f5286ea5dd48cdf527ad41)
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
855 static int cb7210_allocate_private(struct gpib_board *board)
856 {
857 	struct cb7210_priv *priv;
858 
859 	board->private_data = kmalloc(sizeof(struct cb7210_priv), GFP_KERNEL);
860 	if (!board->private_data)
861 		return -ENOMEM;
862 	priv = board->private_data;
863 	memset(priv, 0, sizeof(struct cb7210_priv));
864 	init_nec7210_private(&priv->nec7210_priv);
865 	return 0;
866 }
867 
868 static void cb7210_generic_detach(struct gpib_board *board)
869 {
870 	kfree(board->private_data);
871 	board->private_data = NULL;
872 }
873 
874 // generic part of attach functions shared by all cb7210 boards
875 static int cb7210_generic_attach(struct gpib_board *board)
876 {
877 	struct cb7210_priv *cb_priv;
878 	struct nec7210_priv *nec_priv;
879 
880 	board->status = 0;
881 
882 	if (cb7210_allocate_private(board))
883 		return -ENOMEM;
884 	cb_priv = board->private_data;
885 	nec_priv = &cb_priv->nec7210_priv;
886 	nec_priv->read_byte = nec7210_locking_ioport_read_byte;
887 	nec_priv->write_byte = nec7210_locking_ioport_write_byte;
888 	nec_priv->offset = cb7210_reg_offset;
889 	nec_priv->type = CB7210;
890 	return 0;
891 }
892 
893 static int cb7210_init(struct cb7210_priv *cb_priv, struct gpib_board *board)
894 {
895 	struct nec7210_priv *nec_priv = &cb_priv->nec7210_priv;
896 
897 	cb7210_write_byte(cb_priv, HS_RESET7210, HS_INT_LEVEL);
898 	cb7210_write_byte(cb_priv, irq_bits(cb_priv->irq), HS_INT_LEVEL);
899 
900 	nec7210_board_reset(nec_priv, board);
901 	cb7210_write_byte(cb_priv, HS_TX_ENABLE | HS_RX_ENABLE | HS_CLR_SRQ_INT |
902 			  HS_CLR_EOI_EMPTY_INT | HS_CLR_HF_INT, HS_MODE);
903 
904 	cb_priv->hs_mode_bits = HS_HF_INT_EN;
905 	cb7210_write_byte(cb_priv, cb_priv->hs_mode_bits, HS_MODE);
906 
907 	write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
908 	/*
909 	 * set clock register for maximum (20 MHz) driving frequency
910 	 * ICR should be set to clock in megahertz (1-15) and to zero
911 	 * for clocks faster than 15 MHz (max 20MHz)
912 	 */
913 	write_byte(nec_priv, ICR | 0, AUXMR);
914 
915 	if (cb_priv->pci_chip == PCI_CHIP_QUANCOM) {
916 		/* change interrupt polarity */
917 		nec_priv->auxb_bits |= HR_INV;
918 		write_byte(nec_priv, nec_priv->auxb_bits, AUXMR);
919 	}
920 	nec7210_board_online(nec_priv, board);
921 
922 	/* poll so we can detect assertion of ATN */
923 	if (gpib_request_pseudo_irq(board, cb_pci_interrupt)) {
924 		pr_err("failed to allocate pseudo_irq\n");
925 		return -1;
926 	}
927 	return 0;
928 }
929 
930 static int cb_pci_attach(struct gpib_board *board, const struct gpib_board_config *config)
931 {
932 	struct cb7210_priv *cb_priv;
933 	struct nec7210_priv *nec_priv;
934 	int isr_flags = 0;
935 	int bits;
936 	int retval;
937 
938 	retval = cb7210_generic_attach(board);
939 	if (retval)
940 		return retval;
941 
942 	cb_priv = board->private_data;
943 	nec_priv = &cb_priv->nec7210_priv;
944 
945 	cb_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_CBOARDS,
946 						  PCI_DEVICE_ID_CBOARDS_PCI_GPIB, NULL);
947 	if (cb_priv->pci_device)
948 		cb_priv->pci_chip = PCI_CHIP_AMCC_S5933;
949 	if (!cb_priv->pci_device) {
950 		cb_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_CBOARDS,
951 							  PCI_DEVICE_ID_CBOARDS_CPCI_GPIB, NULL);
952 		if (cb_priv->pci_device)
953 			cb_priv->pci_chip = PCI_CHIP_AMCC_S5933;
954 	}
955 	if (!cb_priv->pci_device) {
956 		cb_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_QUANCOM,
957 							  PCI_DEVICE_ID_QUANCOM_GPIB, NULL);
958 		if (cb_priv->pci_device) {
959 			cb_priv->pci_chip = PCI_CHIP_QUANCOM;
960 			nec_priv->offset = 4;
961 		}
962 	}
963 	if (!cb_priv->pci_device) {
964 		dev_err(board->gpib_dev, "no supported boards found.\n");
965 		return -ENODEV;
966 	}
967 
968 	if (pci_enable_device(cb_priv->pci_device)) {
969 		dev_err(board->gpib_dev, "error enabling pci device\n");
970 		return -EIO;
971 	}
972 
973 	if (pci_request_regions(cb_priv->pci_device, DRV_NAME))
974 		return -EBUSY;
975 	switch (cb_priv->pci_chip) {
976 	case PCI_CHIP_AMCC_S5933:
977 		cb_priv->amcc_iobase = pci_resource_start(cb_priv->pci_device, 0);
978 		nec_priv->iobase = pci_resource_start(cb_priv->pci_device, 1);
979 		cb_priv->fifo_iobase = pci_resource_start(cb_priv->pci_device, 2);
980 		break;
981 	case PCI_CHIP_QUANCOM:
982 		nec_priv->iobase = pci_resource_start(cb_priv->pci_device, 0);
983 		cb_priv->fifo_iobase = nec_priv->iobase;
984 		break;
985 	default:
986 		dev_err(board->gpib_dev, "bug! unhandled pci_chip=%i\n", cb_priv->pci_chip);
987 		return -EIO;
988 	}
989 	isr_flags |= IRQF_SHARED;
990 	if (request_irq(cb_priv->pci_device->irq, cb_pci_interrupt, isr_flags, DRV_NAME, board)) {
991 		dev_err(board->gpib_dev, "can't request IRQ %d\n",
992 			cb_priv->pci_device->irq);
993 		return -EBUSY;
994 	}
995 	cb_priv->irq = cb_priv->pci_device->irq;
996 
997 	switch (cb_priv->pci_chip) {
998 	case PCI_CHIP_AMCC_S5933:
999 		// make sure mailbox flags are clear
1000 		inl(cb_priv->amcc_iobase + INCOMING_MAILBOX_REG(3));
1001 		// enable interrupts on amccs5933 chip
1002 		bits = INBOX_FULL_INTR_BIT | INBOX_BYTE_BITS(3) | INBOX_SELECT_BITS(3) |
1003 			INBOX_INTR_CS_BIT;
1004 		outl(bits, cb_priv->amcc_iobase + INTCSR_REG);
1005 		break;
1006 	default:
1007 		break;
1008 	}
1009 	return cb7210_init(cb_priv, board);
1010 }
1011 
1012 static void cb_pci_detach(struct gpib_board *board)
1013 {
1014 	struct cb7210_priv *cb_priv = board->private_data;
1015 	struct nec7210_priv *nec_priv;
1016 
1017 	if (cb_priv) {
1018 		gpib_free_pseudo_irq(board);
1019 		nec_priv = &cb_priv->nec7210_priv;
1020 		if (cb_priv->irq) {
1021 			// disable amcc interrupts
1022 			outl(0, cb_priv->amcc_iobase + INTCSR_REG);
1023 			free_irq(cb_priv->irq, board);
1024 		}
1025 		if (nec_priv->iobase) {
1026 			nec7210_board_reset(nec_priv, board);
1027 			pci_release_regions(cb_priv->pci_device);
1028 		}
1029 		if (cb_priv->pci_device)
1030 			pci_dev_put(cb_priv->pci_device);
1031 	}
1032 	cb7210_generic_detach(board);
1033 }
1034 
1035 static int cb_isa_attach(struct gpib_board *board, const struct gpib_board_config *config)
1036 {
1037 	int isr_flags = 0;
1038 	struct cb7210_priv *cb_priv;
1039 	struct nec7210_priv *nec_priv;
1040 	unsigned int bits;
1041 	int retval;
1042 
1043 	retval = cb7210_generic_attach(board);
1044 	if (retval)
1045 		return retval;
1046 	cb_priv = board->private_data;
1047 	nec_priv = &cb_priv->nec7210_priv;
1048 	if (!request_region(config->ibbase, cb7210_iosize, DRV_NAME)) {
1049 		dev_err(board->gpib_dev, "ioports starting at 0x%x are already in use\n",
1050 			config->ibbase);
1051 		return -EBUSY;
1052 	}
1053 	nec_priv->iobase = config->ibbase;
1054 	cb_priv->fifo_iobase = nec7210_iobase(cb_priv);
1055 
1056 	bits = irq_bits(config->ibirq);
1057 	if (bits == 0)
1058 		dev_err(board->gpib_dev, "board incapable of using irq %i, try 2-5, 7, 10, or 11\n",
1059 			config->ibirq);
1060 
1061 	// install interrupt handler
1062 	if (request_irq(config->ibirq, cb7210_interrupt, isr_flags, DRV_NAME, board)) {
1063 		dev_err(board->gpib_dev, "failed to obtain IRQ %d\n", config->ibirq);
1064 		return -EBUSY;
1065 	}
1066 	cb_priv->irq = config->ibirq;
1067 
1068 	return cb7210_init(cb_priv, board);
1069 }
1070 
1071 static void cb_isa_detach(struct gpib_board *board)
1072 {
1073 	struct cb7210_priv *cb_priv = board->private_data;
1074 	struct nec7210_priv *nec_priv;
1075 
1076 	if (cb_priv) {
1077 		gpib_free_pseudo_irq(board);
1078 		nec_priv = &cb_priv->nec7210_priv;
1079 		if (cb_priv->irq)
1080 			free_irq(cb_priv->irq, board);
1081 		if (nec_priv->iobase) {
1082 			nec7210_board_reset(nec_priv, board);
1083 			release_region(nec7210_iobase(cb_priv), cb7210_iosize);
1084 		}
1085 	}
1086 	cb7210_generic_detach(board);
1087 }
1088 
1089 static int cb7210_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
1090 {
1091 	return 0;
1092 }
1093 
1094 static const struct pci_device_id cb7210_pci_table[] = {
1095 	{PCI_VENDOR_ID_CBOARDS, PCI_DEVICE_ID_CBOARDS_PCI_GPIB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1096 	{PCI_VENDOR_ID_CBOARDS, PCI_DEVICE_ID_CBOARDS_CPCI_GPIB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1097 	{PCI_VENDOR_ID_QUANCOM, PCI_DEVICE_ID_QUANCOM_GPIB, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1098 	{ 0 }
1099 };
1100 MODULE_DEVICE_TABLE(pci, cb7210_pci_table);
1101 
1102 static struct pci_driver cb7210_pci_driver = {
1103 	.name = DRV_NAME,
1104 	.id_table = cb7210_pci_table,
1105 	.probe = &cb7210_pci_probe
1106 };
1107 
1108 /***************************************************************************
1109  *  Support for computer boards pcmcia-gpib card
1110  *
1111  *  Based on gpib PCMCIA client driver written by Claus Schroeter
1112  *  (clausi@chemie.fu-berlin.de), which was adapted from the
1113  *  pcmcia skeleton example (presumably David Hinds)
1114  ***************************************************************************/
1115 
1116 #ifdef CONFIG_GPIB_PCMCIA
1117 
1118 #include <linux/kernel.h>
1119 #include <linux/ptrace.h>
1120 #include <linux/timer.h>
1121 #include <linux/io.h>
1122 
1123 #include <pcmcia/cistpl.h>
1124 #include <pcmcia/ds.h>
1125 
1126 /*
1127  * The event() function is this driver's Card Services event handler.
1128  * It will be called by Card Services when an appropriate card status
1129  * event is received.  The config() and release() entry points are
1130  * used to configure or release a socket, in response to card insertion
1131  * and ejection events.	 They are invoked from the gpib event
1132  * handler.
1133  */
1134 
1135 static int cb_gpib_config(struct pcmcia_device	*link);
1136 static void cb_gpib_release(struct pcmcia_device  *link);
1137 static int cb_pcmcia_attach(struct gpib_board *board, const struct gpib_board_config *config);
1138 static void cb_pcmcia_detach(struct gpib_board *board);
1139 
1140 /*
1141  *  A linked list of "instances" of the gpib device.  Each actual
1142  *  PCMCIA card corresponds to one device instance, and is described
1143  *  by one dev_link_t structure (defined in ds.h).
1144  *
1145  *  You may not want to use a linked list for this -- for example, the
1146  *  memory card driver uses an array of dev_link_t pointers, where minor
1147  *  device numbers are used to derive the corresponding array index.
1148  */
1149 
1150 static	struct pcmcia_device  *curr_dev;
1151 
1152 /*
1153  *  A dev_link_t structure has fields for most things that are needed
1154  *  to keep track of a socket, but there will usually be some device
1155  *  specific information that also needs to be kept track of.  The
1156  *  'priv' pointer in a dev_link_t structure can be used to point to
1157  *  a device-specific private data structure, like this.
1158  *
1159  *  A driver needs to provide a dev_node_t structure for each device
1160  *  on a card.	In some cases, there is only one device per card (for
1161  *  example, ethernet cards, modems).  In other cases, there may be
1162  *  many actual or logical devices (SCSI adapters, memory cards with
1163  *  multiple partitions).  The dev_node_t structures need to be kept
1164  *  in a linked list starting at the 'dev' field of a dev_link_t
1165  *  structure.	We allocate them in the card's private data structure,
1166  * because they generally can't be allocated dynamically.
1167  */
1168 
1169 struct local_info {
1170 	struct pcmcia_device	*p_dev;
1171 	struct gpib_board		*dev;
1172 };
1173 
1174 /*
1175  *  gpib_attach() creates an "instance" of the driver, allocating
1176  *  local data structures for one device.  The device is registered
1177  *  with Card Services.
1178  *
1179  *  The dev_link structure is initialized, but we don't actually
1180  *  configure the card at this point -- we wait until we receive a
1181  *  card insertion event.
1182  */
1183 
1184 static int cb_gpib_probe(struct pcmcia_device *link)
1185 {
1186 	struct local_info *info;
1187 	int ret;
1188 
1189 	/* Allocate space for private device-specific data */
1190 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1191 	if (!info)
1192 		return -ENOMEM;
1193 
1194 	info->p_dev = link;
1195 	link->priv = info;
1196 
1197 	/* The io structure describes IO port mapping */
1198 	link->resource[0]->end = 16;
1199 	link->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
1200 	link->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
1201 	link->resource[1]->end = 16;
1202 	link->resource[1]->flags &= ~IO_DATA_PATH_WIDTH;
1203 	link->resource[1]->flags |= IO_DATA_PATH_WIDTH_16;
1204 	link->io_lines = 10;
1205 
1206 	/* General socket configuration */
1207 	link->config_flags = CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
1208 	link->config_index = 1;
1209 	link->config_regs = PRESENT_OPTION;
1210 
1211 	/* Register with Card Services */
1212 	curr_dev = link;
1213 	ret = cb_gpib_config(link);
1214 	if (ret)
1215 		goto free_info;
1216 
1217 	return 0;
1218 
1219 free_info:
1220 	kfree(info);
1221 	return ret;
1222 }
1223 
1224 /*
1225  *   This deletes a driver "instance".  The device is de-registered
1226  *   with Card Services.  If it has been released, all local data
1227  *   structures are freed.  Otherwise, the structures will be freed
1228  *   when the device is released.
1229  */
1230 
1231 static void cb_gpib_remove(struct pcmcia_device *link)
1232 {
1233 	struct local_info *info = link->priv;
1234 	//struct struct gpib_board *dev = info->dev;
1235 
1236 	if (info->dev)
1237 		cb_pcmcia_detach(info->dev);
1238 	cb_gpib_release(link);
1239 
1240 	//free_netdev(dev);
1241 	kfree(info);
1242 }
1243 
1244 static int cb_gpib_config_iteration(struct pcmcia_device *link, void *priv_data)
1245 {
1246 	return pcmcia_request_io(link);
1247 }
1248 
1249 /*
1250  *   gpib_config() is scheduled to run after a CARD_INSERTION event
1251  *   is received, to configure the PCMCIA socket, and to make the
1252  *   ethernet device available to the system.
1253  */
1254 
1255 static int cb_gpib_config(struct pcmcia_device  *link)
1256 {
1257 	int retval;
1258 
1259 	retval = pcmcia_loop_config(link, &cb_gpib_config_iteration, NULL);
1260 	if (retval) {
1261 		dev_warn(&link->dev, "no configuration found\n");
1262 		cb_gpib_release(link);
1263 		return -ENODEV;
1264 	}
1265 
1266 	/*
1267 	 *  This actually configures the PCMCIA socket -- setting up
1268 	 *  the I/O windows and the interrupt mapping.
1269 	 */
1270 	retval = pcmcia_enable_device(link);
1271 	if (retval) {
1272 		dev_warn(&link->dev, "pcmcia_enable_device failed\n");
1273 		cb_gpib_release(link);
1274 		return -ENODEV;
1275 	}
1276 
1277 	return 0;
1278 } /* gpib_config */
1279 
1280 /*
1281  * After a card is removed, gpib_release() will unregister the net
1282  * device, and release the PCMCIA configuration.  If the device is
1283  * still open, this will be postponed until it is closed.
1284  */
1285 
1286 static void cb_gpib_release(struct pcmcia_device *link)
1287 {
1288 	pcmcia_disable_device(link);
1289 }
1290 
1291 static int cb_gpib_suspend(struct pcmcia_device *link)
1292 {
1293 	if (link->open)
1294 		dev_warn(&link->dev, "Device still open\n");
1295 
1296 	return 0;
1297 }
1298 
1299 static int cb_gpib_resume(struct pcmcia_device *link)
1300 {
1301 	return cb_gpib_config(link);
1302 }
1303 
1304 /*====================================================================*/
1305 
1306 static struct pcmcia_device_id cb_pcmcia_ids[] = {
1307 	PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x0005),
1308 	PCMCIA_DEVICE_NULL
1309 };
1310 MODULE_DEVICE_TABLE(pcmcia, cb_pcmcia_ids);
1311 
1312 static struct pcmcia_driver cb_gpib_cs_driver = {
1313 	.name           = "cb_gpib_cs",
1314 	.owner		= THIS_MODULE,
1315 	.id_table	= cb_pcmcia_ids,
1316 	.probe		= cb_gpib_probe,
1317 	.remove		= cb_gpib_remove,
1318 	.suspend	= cb_gpib_suspend,
1319 	.resume		= cb_gpib_resume,
1320 };
1321 
1322 static void cb_pcmcia_cleanup_module(void)
1323 {
1324 	pcmcia_unregister_driver(&cb_gpib_cs_driver);
1325 }
1326 
1327 static struct gpib_interface cb_pcmcia_unaccel_interface = {
1328 	.name = "cbi_pcmcia_unaccel",
1329 	.attach = cb_pcmcia_attach,
1330 	.detach = cb_pcmcia_detach,
1331 	.read = cb7210_read,
1332 	.write = cb7210_write,
1333 	.command = cb7210_command,
1334 	.take_control = cb7210_take_control,
1335 	.go_to_standby = cb7210_go_to_standby,
1336 	.request_system_control = cb7210_request_system_control,
1337 	.interface_clear = cb7210_interface_clear,
1338 	.remote_enable = cb7210_remote_enable,
1339 	.enable_eos = cb7210_enable_eos,
1340 	.disable_eos = cb7210_disable_eos,
1341 	.parallel_poll = cb7210_parallel_poll,
1342 	.parallel_poll_configure = cb7210_parallel_poll_configure,
1343 	.parallel_poll_response = cb7210_parallel_poll_response,
1344 	.local_parallel_poll_mode = NULL, // XXX
1345 	.line_status = cb7210_line_status,
1346 	.update_status = cb7210_update_status,
1347 	.primary_address = cb7210_primary_address,
1348 	.secondary_address = cb7210_secondary_address,
1349 	.serial_poll_response = cb7210_serial_poll_response,
1350 	.serial_poll_status = cb7210_serial_poll_status,
1351 	.t1_delay = cb7210_t1_delay,
1352 	.return_to_local = cb7210_return_to_local,
1353 };
1354 
1355 static struct gpib_interface cb_pcmcia_interface = {
1356 	.name = "cbi_pcmcia",
1357 	.attach = cb_pcmcia_attach,
1358 	.detach = cb_pcmcia_detach,
1359 	.read = cb7210_accel_read,
1360 	.write = cb7210_accel_write,
1361 	.command = cb7210_command,
1362 	.take_control = cb7210_take_control,
1363 	.go_to_standby = cb7210_go_to_standby,
1364 	.request_system_control = cb7210_request_system_control,
1365 	.interface_clear = cb7210_interface_clear,
1366 	.remote_enable = cb7210_remote_enable,
1367 	.enable_eos = cb7210_enable_eos,
1368 	.disable_eos = cb7210_disable_eos,
1369 	.parallel_poll = cb7210_parallel_poll,
1370 	.parallel_poll_configure = cb7210_parallel_poll_configure,
1371 	.parallel_poll_response = cb7210_parallel_poll_response,
1372 	.local_parallel_poll_mode = NULL, // XXX
1373 	.line_status = cb7210_line_status,
1374 	.update_status = cb7210_update_status,
1375 	.primary_address = cb7210_primary_address,
1376 	.secondary_address = cb7210_secondary_address,
1377 	.serial_poll_response = cb7210_serial_poll_response,
1378 	.serial_poll_status = cb7210_serial_poll_status,
1379 	.t1_delay = cb7210_t1_delay,
1380 	.return_to_local = cb7210_return_to_local,
1381 };
1382 
1383 static struct gpib_interface cb_pcmcia_accel_interface = {
1384 	.name = "cbi_pcmcia_accel",
1385 	.attach = cb_pcmcia_attach,
1386 	.detach = cb_pcmcia_detach,
1387 	.read = cb7210_accel_read,
1388 	.write = cb7210_accel_write,
1389 	.command = cb7210_command,
1390 	.take_control = cb7210_take_control,
1391 	.go_to_standby = cb7210_go_to_standby,
1392 	.request_system_control = cb7210_request_system_control,
1393 	.interface_clear = cb7210_interface_clear,
1394 	.remote_enable = cb7210_remote_enable,
1395 	.enable_eos = cb7210_enable_eos,
1396 	.disable_eos = cb7210_disable_eos,
1397 	.parallel_poll = cb7210_parallel_poll,
1398 	.parallel_poll_configure = cb7210_parallel_poll_configure,
1399 	.parallel_poll_response = cb7210_parallel_poll_response,
1400 	.local_parallel_poll_mode = NULL, // XXX
1401 	.line_status = cb7210_line_status,
1402 	.update_status = cb7210_update_status,
1403 	.primary_address = cb7210_primary_address,
1404 	.secondary_address = cb7210_secondary_address,
1405 	.serial_poll_response = cb7210_serial_poll_response,
1406 	.serial_poll_status = cb7210_serial_poll_status,
1407 	.t1_delay = cb7210_t1_delay,
1408 	.return_to_local = cb7210_return_to_local,
1409 };
1410 
1411 static int cb_pcmcia_attach(struct gpib_board *board, const struct gpib_board_config *config)
1412 {
1413 	struct cb7210_priv *cb_priv;
1414 	struct nec7210_priv *nec_priv;
1415 	int retval;
1416 
1417 	if (!curr_dev) {
1418 		dev_err(board->gpib_dev, "no cb pcmcia cards found\n");
1419 		return -ENODEV;
1420 	}
1421 
1422 	retval = cb7210_generic_attach(board);
1423 	if (retval)
1424 		return retval;
1425 
1426 	cb_priv = board->private_data;
1427 	nec_priv = &cb_priv->nec7210_priv;
1428 
1429 	if (!request_region(curr_dev->resource[0]->start, resource_size(curr_dev->resource[0]),
1430 			    DRV_NAME))	{
1431 		dev_err(board->gpib_dev, "ioports starting at 0x%lx are already in use\n",
1432 			(unsigned long)curr_dev->resource[0]->start);
1433 		return -EBUSY;
1434 	}
1435 	nec_priv->iobase = curr_dev->resource[0]->start;
1436 	cb_priv->fifo_iobase = curr_dev->resource[0]->start;
1437 
1438 	if (request_irq(curr_dev->irq, cb7210_interrupt, IRQF_SHARED, DRV_NAME, board)) {
1439 		dev_err(board->gpib_dev, "failed to request IRQ %d\n", curr_dev->irq);
1440 		return -EBUSY;
1441 	}
1442 	cb_priv->irq = curr_dev->irq;
1443 
1444 	return cb7210_init(cb_priv, board);
1445 }
1446 
1447 static void cb_pcmcia_detach(struct gpib_board *board)
1448 {
1449 	struct cb7210_priv *cb_priv = board->private_data;
1450 	struct nec7210_priv *nec_priv;
1451 
1452 	if (cb_priv) {
1453 		nec_priv = &cb_priv->nec7210_priv;
1454 		gpib_free_pseudo_irq(board);
1455 		if (cb_priv->irq)
1456 			free_irq(cb_priv->irq, board);
1457 		if (nec_priv->iobase) {
1458 			nec7210_board_reset(nec_priv, board);
1459 			release_region(nec7210_iobase(cb_priv), cb7210_iosize);
1460 		}
1461 	}
1462 	cb7210_generic_detach(board);
1463 }
1464 
1465 #endif /* CONFIG_GPIB_PCMCIA */
1466 
1467 static int __init cb7210_init_module(void)
1468 {
1469 	int ret;
1470 
1471 	ret = pci_register_driver(&cb7210_pci_driver);
1472 	if (ret) {
1473 		pr_err("pci_register_driver failed: error = %d\n", ret);
1474 		return ret;
1475 	}
1476 
1477 	ret = gpib_register_driver(&cb_pci_interface, THIS_MODULE);
1478 	if (ret) {
1479 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1480 		goto err_pci;
1481 	}
1482 
1483 	ret = gpib_register_driver(&cb_isa_interface, THIS_MODULE);
1484 	if (ret) {
1485 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1486 		goto err_isa;
1487 	}
1488 
1489 	ret = gpib_register_driver(&cb_pci_accel_interface, THIS_MODULE);
1490 	if (ret) {
1491 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1492 		goto err_pci_accel;
1493 	}
1494 
1495 	ret = gpib_register_driver(&cb_pci_unaccel_interface, THIS_MODULE);
1496 	if (ret) {
1497 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1498 		goto err_pci_unaccel;
1499 	}
1500 
1501 	ret = gpib_register_driver(&cb_isa_accel_interface, THIS_MODULE);
1502 	if (ret) {
1503 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1504 		goto err_isa_accel;
1505 	}
1506 
1507 	ret = gpib_register_driver(&cb_isa_unaccel_interface, THIS_MODULE);
1508 	if (ret) {
1509 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1510 		goto err_isa_unaccel;
1511 	}
1512 
1513 #ifdef CONFIG_GPIB_PCMCIA
1514 	ret = gpib_register_driver(&cb_pcmcia_interface, THIS_MODULE);
1515 	if (ret) {
1516 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1517 		goto err_pcmcia;
1518 	}
1519 
1520 	ret = gpib_register_driver(&cb_pcmcia_accel_interface, THIS_MODULE);
1521 	if (ret) {
1522 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1523 		goto err_pcmcia_accel;
1524 	}
1525 
1526 	ret = gpib_register_driver(&cb_pcmcia_unaccel_interface, THIS_MODULE);
1527 	if (ret) {
1528 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1529 		goto err_pcmcia_unaccel;
1530 	}
1531 
1532 	ret = pcmcia_register_driver(&cb_gpib_cs_driver);
1533 	if (ret) {
1534 		pr_err("pcmcia_register_driver failed: error = %d\n", ret);
1535 		goto err_pcmcia_driver;
1536 	}
1537 #endif
1538 
1539 	return 0;
1540 
1541 #ifdef CONFIG_GPIB_PCMCIA
1542 err_pcmcia_driver:
1543 	gpib_unregister_driver(&cb_pcmcia_unaccel_interface);
1544 err_pcmcia_unaccel:
1545 	gpib_unregister_driver(&cb_pcmcia_accel_interface);
1546 err_pcmcia_accel:
1547 	gpib_unregister_driver(&cb_pcmcia_interface);
1548 err_pcmcia:
1549 #endif
1550 	gpib_unregister_driver(&cb_isa_unaccel_interface);
1551 err_isa_unaccel:
1552 	gpib_unregister_driver(&cb_isa_accel_interface);
1553 err_isa_accel:
1554 	gpib_unregister_driver(&cb_pci_unaccel_interface);
1555 err_pci_unaccel:
1556 	gpib_unregister_driver(&cb_pci_accel_interface);
1557 err_pci_accel:
1558 	gpib_unregister_driver(&cb_isa_interface);
1559 err_isa:
1560 	gpib_unregister_driver(&cb_pci_interface);
1561 err_pci:
1562 	pci_unregister_driver(&cb7210_pci_driver);
1563 
1564 	return ret;
1565 }
1566 
1567 static void __exit cb7210_exit_module(void)
1568 {
1569 	gpib_unregister_driver(&cb_pci_interface);
1570 	gpib_unregister_driver(&cb_isa_interface);
1571 	gpib_unregister_driver(&cb_pci_accel_interface);
1572 	gpib_unregister_driver(&cb_pci_unaccel_interface);
1573 	gpib_unregister_driver(&cb_isa_accel_interface);
1574 	gpib_unregister_driver(&cb_isa_unaccel_interface);
1575 #ifdef CONFIG_GPIB_PCMCIA
1576 	gpib_unregister_driver(&cb_pcmcia_interface);
1577 	gpib_unregister_driver(&cb_pcmcia_accel_interface);
1578 	gpib_unregister_driver(&cb_pcmcia_unaccel_interface);
1579 	cb_pcmcia_cleanup_module();
1580 #endif
1581 
1582 	pci_unregister_driver(&cb7210_pci_driver);
1583 }
1584 
1585 module_init(cb7210_init_module);
1586 module_exit(cb7210_exit_module);
1587