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