xref: /linux/drivers/staging/gpib/ines/ines_gpib.c (revision a100922a3855eb35ecd465f1d558546b1e144445)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  *    copyright		   : (C) 1999 Axel Dziemba (axel.dziemba@ines.de)
5  *			    (C) 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 "ines.h"
13 
14 #include <linux/pci.h>
15 #include <linux/pci_ids.h>
16 #include <linux/bitops.h>
17 #include <asm/dma.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include "gpib_pci_ids.h"
24 
25 MODULE_LICENSE("GPL");
26 MODULE_DESCRIPTION("GPIB driver for Ines iGPIB 72010");
27 
28 static irqreturn_t ines_interrupt(struct gpib_board *board);
29 
ines_line_status(const struct gpib_board * board)30 static int ines_line_status(const struct gpib_board *board)
31 {
32 	int status = VALID_ALL;
33 	int bcm_bits;
34 	struct ines_priv *ines_priv;
35 
36 	ines_priv = board->private_data;
37 
38 	bcm_bits = ines_inb(ines_priv, BUS_CONTROL_MONITOR);
39 
40 	if (bcm_bits & BCM_REN_BIT)
41 		status |= BUS_REN;
42 	if (bcm_bits & BCM_IFC_BIT)
43 		status |= BUS_IFC;
44 	if (bcm_bits & BCM_SRQ_BIT)
45 		status |= BUS_SRQ;
46 	if (bcm_bits & BCM_EOI_BIT)
47 		status |= BUS_EOI;
48 	if (bcm_bits & BCM_NRFD_BIT)
49 		status |= BUS_NRFD;
50 	if (bcm_bits & BCM_NDAC_BIT)
51 		status |= BUS_NDAC;
52 	if (bcm_bits & BCM_DAV_BIT)
53 		status |= BUS_DAV;
54 	if (bcm_bits & BCM_ATN_BIT)
55 		status |= BUS_ATN;
56 
57 	return status;
58 }
59 
ines_set_xfer_counter(struct ines_priv * priv,unsigned int count)60 static void ines_set_xfer_counter(struct ines_priv *priv, unsigned int count)
61 {
62 	if (count > 0xffff) {
63 		pr_err("bug! tried to set xfer counter > 0xffff\n");
64 		return;
65 	}
66 	ines_outb(priv, (count >> 8) & 0xff, XFER_COUNT_UPPER);
67 	ines_outb(priv, count & 0xff, XFER_COUNT_LOWER);
68 }
69 
ines_t1_delay(struct gpib_board * board,unsigned int nano_sec)70 static int ines_t1_delay(struct gpib_board *board, unsigned int nano_sec)
71 {
72 	struct ines_priv *ines_priv = board->private_data;
73 	struct nec7210_priv *nec_priv = &ines_priv->nec7210_priv;
74 	unsigned int retval;
75 
76 	retval = nec7210_t1_delay(board, nec_priv, nano_sec);
77 
78 	if (nano_sec <= 250) {
79 		write_byte(nec_priv, INES_AUXD | INES_FOLLOWING_T1_250ns |
80 			   INES_INITIAL_T1_2000ns, AUXMR);
81 		retval = 250;
82 	} else if (nano_sec <= 350) {
83 		write_byte(nec_priv, INES_AUXD | INES_FOLLOWING_T1_350ns |
84 			   INES_INITIAL_T1_2000ns, AUXMR);
85 		retval = 350;
86 	} else {
87 		write_byte(nec_priv, INES_AUXD | INES_FOLLOWING_T1_500ns |
88 			   INES_INITIAL_T1_2000ns, AUXMR);
89 		retval = 500;
90 	}
91 
92 	return retval;
93 }
94 
num_in_fifo_bytes(struct ines_priv * ines_priv)95 static inline unsigned short num_in_fifo_bytes(struct ines_priv *ines_priv)
96 {
97 	return ines_inb(ines_priv, IN_FIFO_COUNT);
98 }
99 
pio_read(struct gpib_board * board,struct ines_priv * ines_priv,u8 * buffer,size_t length,size_t * nbytes)100 static ssize_t pio_read(struct gpib_board *board, struct ines_priv *ines_priv, u8 *buffer,
101 			size_t length, size_t *nbytes)
102 {
103 	ssize_t retval = 0;
104 	unsigned int num_fifo_bytes, i;
105 	struct nec7210_priv *nec_priv = &ines_priv->nec7210_priv;
106 
107 	*nbytes = 0;
108 	while (*nbytes < length) {
109 		if (wait_event_interruptible(board->wait,
110 					     num_in_fifo_bytes(ines_priv) ||
111 					     test_bit(RECEIVED_END_BN, &nec_priv->state) ||
112 					     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
113 					     test_bit(TIMO_NUM, &board->status)))
114 			return -ERESTARTSYS;
115 
116 		if (test_bit(TIMO_NUM, &board->status))
117 			return -ETIMEDOUT;
118 		if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
119 			return -EINTR;
120 
121 		num_fifo_bytes = num_in_fifo_bytes(ines_priv);
122 		if (num_fifo_bytes + *nbytes > length)
123 			num_fifo_bytes = length - *nbytes;
124 
125 		for (i = 0; i < num_fifo_bytes; i++)
126 			buffer[(*nbytes)++] = read_byte(nec_priv, DIR);
127 		if (test_bit(RECEIVED_END_BN, &nec_priv->state) &&
128 		    num_in_fifo_bytes(ines_priv) == 0)
129 			break;
130 		if (need_resched())
131 			schedule();
132 	}
133 	/* make sure RECEIVED_END is in sync */
134 	ines_interrupt(board);
135 	return retval;
136 }
137 
ines_accel_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)138 static int ines_accel_read(struct gpib_board *board, u8 *buffer,
139 			   size_t length, int *end, size_t *bytes_read)
140 {
141 	ssize_t retval = 0;
142 	struct ines_priv *ines_priv = board->private_data;
143 	struct nec7210_priv *nec_priv = &ines_priv->nec7210_priv;
144 	int counter_setting;
145 
146 	*end = 0;
147 	*bytes_read = 0;
148 	if (length == 0)
149 		return 0;
150 
151 	clear_bit(DEV_CLEAR_BN, &nec_priv->state);
152 
153 	write_byte(nec_priv, INES_RFD_HLD_IMMEDIATE, AUXMR);
154 
155 	//clear in fifo
156 	nec7210_set_reg_bits(nec_priv, ADMR, IN_FIFO_ENABLE_BIT, 0);
157 	nec7210_set_reg_bits(nec_priv, ADMR, IN_FIFO_ENABLE_BIT, IN_FIFO_ENABLE_BIT);
158 
159 	ines_priv->extend_mode_bits |= LAST_BYTE_HANDLING_BIT;
160 	ines_priv->extend_mode_bits &= ~XFER_COUNTER_OUTPUT_BIT & ~XFER_COUNTER_ENABLE_BIT;
161 	ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
162 
163 	counter_setting = length - num_in_fifo_bytes(ines_priv);
164 	if (counter_setting > 0) {
165 		ines_set_xfer_counter(ines_priv, length);
166 		ines_priv->extend_mode_bits |= XFER_COUNTER_ENABLE_BIT;
167 		ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
168 
169 		// holdoff on END
170 		nec7210_set_handshake_mode(board, nec_priv, HR_HLDE);
171 		/* release rfd holdoff */
172 		write_byte(nec_priv, AUX_FH, AUXMR);
173 	}
174 
175 	retval = pio_read(board, ines_priv, buffer, length, bytes_read);
176 	ines_priv->extend_mode_bits &= ~XFER_COUNTER_ENABLE_BIT;
177 	ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
178 	if (retval < 0)	{
179 		write_byte(nec_priv, INES_RFD_HLD_IMMEDIATE, AUXMR);
180 		return retval;
181 	}
182 	if (test_and_clear_bit(RECEIVED_END_BN, &nec_priv->state))
183 		*end = 1;
184 
185 	return retval;
186 }
187 
188 static const int out_fifo_size = 0xff;
189 
num_out_fifo_bytes(struct ines_priv * ines_priv)190 static inline unsigned short num_out_fifo_bytes(struct ines_priv *ines_priv)
191 {
192 	return ines_inb(ines_priv, OUT_FIFO_COUNT);
193 }
194 
ines_write_wait(struct gpib_board * board,struct ines_priv * ines_priv,unsigned int fifo_threshold)195 static int ines_write_wait(struct gpib_board *board, struct ines_priv *ines_priv,
196 			   unsigned int fifo_threshold)
197 {
198 	struct nec7210_priv *nec_priv = &ines_priv->nec7210_priv;
199 
200 	// wait until byte is ready to be sent
201 	if (wait_event_interruptible(board->wait,
202 				     num_out_fifo_bytes(ines_priv) < fifo_threshold ||
203 				     test_bit(BUS_ERROR_BN, &nec_priv->state) ||
204 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
205 				     test_bit(TIMO_NUM, &board->status)))
206 		return -ERESTARTSYS;
207 
208 	if (test_bit(BUS_ERROR_BN, &nec_priv->state))
209 		return -EIO;
210 	if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
211 		return -EINTR;
212 	if (test_bit(TIMO_NUM, &board->status))
213 		return -ETIMEDOUT;
214 
215 	return 0;
216 }
217 
ines_accel_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)218 static int ines_accel_write(struct gpib_board *board, u8 *buffer, size_t length,
219 			    int send_eoi, size_t *bytes_written)
220 {
221 	size_t count = 0;
222 	ssize_t retval = 0;
223 	struct ines_priv *ines_priv = board->private_data;
224 	struct nec7210_priv *nec_priv = &ines_priv->nec7210_priv;
225 	unsigned int num_bytes, i;
226 
227 	*bytes_written = 0;
228 	//clear out fifo
229 	nec7210_set_reg_bits(nec_priv, ADMR, OUT_FIFO_ENABLE_BIT, 0);
230 	nec7210_set_reg_bits(nec_priv, ADMR, OUT_FIFO_ENABLE_BIT, OUT_FIFO_ENABLE_BIT);
231 
232 	ines_priv->extend_mode_bits |= XFER_COUNTER_OUTPUT_BIT;
233 	ines_priv->extend_mode_bits &= ~XFER_COUNTER_ENABLE_BIT;
234 	ines_priv->extend_mode_bits &= ~LAST_BYTE_HANDLING_BIT;
235 	ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
236 
237 	ines_set_xfer_counter(ines_priv, length);
238 	if (send_eoi)
239 		ines_priv->extend_mode_bits |= LAST_BYTE_HANDLING_BIT;
240 	ines_priv->extend_mode_bits |= XFER_COUNTER_ENABLE_BIT;
241 	ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
242 
243 	while (count < length) {
244 		retval = ines_write_wait(board, ines_priv, out_fifo_size);
245 		if (retval < 0)
246 			break;
247 
248 		num_bytes = out_fifo_size - num_out_fifo_bytes(ines_priv);
249 		if (num_bytes + count > length)
250 			num_bytes = length - count;
251 		for (i = 0; i < num_bytes; i++)
252 			write_byte(nec_priv, buffer[count++], CDOR);
253 	}
254 	if (retval < 0)	{
255 		ines_priv->extend_mode_bits &= ~XFER_COUNTER_ENABLE_BIT;
256 		ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
257 		*bytes_written = length - num_out_fifo_bytes(ines_priv);
258 		return retval;
259 	}
260 	// wait last byte has been sent
261 	retval = ines_write_wait(board, ines_priv, 1);
262 	ines_priv->extend_mode_bits &= ~XFER_COUNTER_ENABLE_BIT;
263 	ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
264 	*bytes_written = length - num_out_fifo_bytes(ines_priv);
265 
266 	return retval;
267 }
268 
ines_pci_interrupt(int irq,void * arg)269 static irqreturn_t ines_pci_interrupt(int irq, void *arg)
270 {
271 	struct gpib_board *board = arg;
272 	struct ines_priv *priv = board->private_data;
273 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
274 
275 	if (priv->pci_chip_type == PCI_CHIP_QUANCOM) {
276 		if ((inb(nec_priv->iobase +
277 			 QUANCOM_IRQ_CONTROL_STATUS_REG) &
278 		     QUANCOM_IRQ_ASSERTED_BIT))
279 			outb(QUANCOM_IRQ_ENABLE_BIT, nec_priv->iobase +
280 			     QUANCOM_IRQ_CONTROL_STATUS_REG);
281 	}
282 
283 	return ines_interrupt(board);
284 }
285 
ines_interrupt(struct gpib_board * board)286 static irqreturn_t ines_interrupt(struct gpib_board *board)
287 {
288 	struct ines_priv *priv = board->private_data;
289 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
290 	unsigned int isr3_bits, isr4_bits;
291 	unsigned long flags;
292 	int wake = 0;
293 
294 	spin_lock_irqsave(&board->spinlock, flags);
295 
296 	nec7210_interrupt(board, nec_priv);
297 	isr3_bits = ines_inb(priv, ISR3);
298 	isr4_bits = ines_inb(priv, ISR4);
299 	if (isr3_bits & IFC_ACTIVE_BIT)	{
300 		push_gpib_event(board, EVENT_IFC);
301 		wake++;
302 	}
303 	if (isr3_bits & FIFO_ERROR_BIT)
304 		dev_err(board->gpib_dev, "fifo error\n");
305 	if (isr3_bits & XFER_COUNT_BIT)
306 		wake++;
307 
308 	if (isr4_bits & (IN_FIFO_WATERMARK_BIT | IN_FIFO_FULL_BIT | OUT_FIFO_WATERMARK_BIT |
309 			 OUT_FIFO_EMPTY_BIT))
310 		wake++;
311 
312 	if (wake)
313 		wake_up_interruptible(&board->wait);
314 	spin_unlock_irqrestore(&board->spinlock, flags);
315 	return IRQ_HANDLED;
316 }
317 
318 static int ines_pci_attach(struct gpib_board *board, const struct gpib_board_config *config);
319 static int ines_pci_accel_attach(struct gpib_board *board, const struct gpib_board_config *config);
320 static int ines_isa_attach(struct gpib_board *board, const struct gpib_board_config *config);
321 
322 static void ines_pci_detach(struct gpib_board *board);
323 static void ines_isa_detach(struct gpib_board *board);
324 
325 enum ines_pci_vendor_ids {
326 	PCI_VENDOR_ID_INES_QUICKLOGIC = 0x16da
327 };
328 
329 enum ines_pci_device_ids {
330 	PCI_DEVICE_ID_INES_GPIB_AMCC = 0x8507,
331 	PCI_DEVICE_ID_INES_GPIB_QL5030 = 0x11,
332 };
333 
334 enum ines_pci_subdevice_ids {
335 	PCI_SUBDEVICE_ID_INES_GPIB = 0x1072
336 };
337 
338 static struct pci_device_id ines_pci_table[] = {
339 	{PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_PLX,
340 	 PCI_SUBDEVICE_ID_INES_GPIB, 0, 0, 0},
341 	{PCI_VENDOR_ID_AMCC, PCI_DEVICE_ID_INES_GPIB_AMCC, PCI_VENDOR_ID_AMCC,
342 	 PCI_SUBDEVICE_ID_INES_GPIB, 0, 0, 0},
343 	{PCI_VENDOR_ID_INES_QUICKLOGIC, PCI_DEVICE_ID_INES_GPIB_QL5030,
344 	 PCI_VENDOR_ID_INES_QUICKLOGIC, PCI_DEVICE_ID_INES_GPIB_QL5030, 0, 0, 0},
345 	{PCI_DEVICE(PCI_VENDOR_ID_QUANCOM, PCI_DEVICE_ID_QUANCOM_GPIB)},
346 	{0}
347 };
348 MODULE_DEVICE_TABLE(pci, ines_pci_table);
349 
350 struct ines_pci_id {
351 	unsigned int vendor_id;
352 	unsigned int device_id;
353 	int subsystem_vendor_id;
354 	int subsystem_device_id;
355 	unsigned int gpib_region;
356 	unsigned int io_offset;
357 	enum ines_pci_chip pci_chip_type;
358 };
359 
360 static struct ines_pci_id pci_ids[] = {
361 	{.vendor_id = PCI_VENDOR_ID_PLX,
362 	 .device_id = PCI_DEVICE_ID_PLX_9050,
363 	 .subsystem_vendor_id = PCI_VENDOR_ID_PLX,
364 	 .subsystem_device_id = PCI_SUBDEVICE_ID_INES_GPIB,
365 	 .gpib_region = 2,
366 	 .io_offset = 1,
367 	 .pci_chip_type = PCI_CHIP_PLX9050,
368 	},
369 	{.vendor_id = PCI_VENDOR_ID_AMCC,
370 	 .device_id = PCI_DEVICE_ID_INES_GPIB_AMCC,
371 	 .subsystem_vendor_id = PCI_VENDOR_ID_AMCC,
372 	 .subsystem_device_id = PCI_SUBDEVICE_ID_INES_GPIB,
373 	 .gpib_region = 1,
374 	 .io_offset = 1,
375 	 .pci_chip_type = PCI_CHIP_AMCC5920,
376 	},
377 	{.vendor_id = PCI_VENDOR_ID_INES_QUICKLOGIC,
378 	 .device_id = PCI_DEVICE_ID_INES_GPIB_QL5030,
379 	 .subsystem_vendor_id = PCI_VENDOR_ID_INES_QUICKLOGIC,
380 	 .subsystem_device_id = PCI_DEVICE_ID_INES_GPIB_QL5030,
381 	 .gpib_region = 1,
382 	 .io_offset = 1,
383 	 .pci_chip_type = PCI_CHIP_QUICKLOGIC5030,
384 	},
385 	{.vendor_id = PCI_VENDOR_ID_QUANCOM,
386 	 .device_id = PCI_DEVICE_ID_QUANCOM_GPIB,
387 	 .subsystem_vendor_id = -1,
388 	 .subsystem_device_id = -1,
389 	 .gpib_region = 0,
390 	 .io_offset = 4,
391 	 .pci_chip_type = PCI_CHIP_QUANCOM,
392 	},
393 };
394 
395 static const int num_pci_chips = ARRAY_SIZE(pci_ids);
396 
397 // wrappers for interface functions
ines_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)398 static int ines_read(struct gpib_board *board, u8 *buffer, size_t length,
399 		     int *end, size_t *bytes_read)
400 {
401 	struct ines_priv *priv = board->private_data;
402 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
403 	ssize_t retval;
404 	int dummy;
405 
406 	retval = nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
407 	if (retval < 0)	{
408 		write_byte(nec_priv, INES_RFD_HLD_IMMEDIATE, AUXMR);
409 
410 		set_bit(RFD_HOLDOFF_BN, &nec_priv->state);
411 
412 		nec7210_read_data_in(board, nec_priv, &dummy);
413 	}
414 	return retval;
415 }
416 
ines_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)417 static int ines_write(struct gpib_board *board, u8 *buffer, size_t length, int send_eoi,
418 		      size_t *bytes_written)
419 {
420 	struct ines_priv *priv = board->private_data;
421 
422 	return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
423 }
424 
ines_command(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)425 static int ines_command(struct gpib_board *board, u8 *buffer, size_t length, size_t *bytes_written)
426 {
427 	struct ines_priv *priv = board->private_data;
428 
429 	return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
430 }
431 
ines_take_control(struct gpib_board * board,int synchronous)432 static int ines_take_control(struct gpib_board *board, int synchronous)
433 {
434 	struct ines_priv *priv = board->private_data;
435 
436 	return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
437 }
438 
ines_go_to_standby(struct gpib_board * board)439 static int ines_go_to_standby(struct gpib_board *board)
440 {
441 	struct ines_priv *priv = board->private_data;
442 
443 	return nec7210_go_to_standby(board, &priv->nec7210_priv);
444 }
445 
ines_request_system_control(struct gpib_board * board,int request_control)446 static int ines_request_system_control(struct gpib_board *board, int request_control)
447 {
448 	struct ines_priv *priv = board->private_data;
449 
450 	return nec7210_request_system_control(board, &priv->nec7210_priv, request_control);
451 }
452 
ines_interface_clear(struct gpib_board * board,int assert)453 static void ines_interface_clear(struct gpib_board *board, int assert)
454 {
455 	struct ines_priv *priv = board->private_data;
456 
457 	nec7210_interface_clear(board, &priv->nec7210_priv, assert);
458 }
459 
ines_remote_enable(struct gpib_board * board,int enable)460 static void ines_remote_enable(struct gpib_board *board, int enable)
461 {
462 	struct ines_priv *priv = board->private_data;
463 
464 	nec7210_remote_enable(board, &priv->nec7210_priv, enable);
465 }
466 
ines_enable_eos(struct gpib_board * board,u8 eos_byte,int compare_8_bits)467 static int ines_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits)
468 {
469 	struct ines_priv *priv = board->private_data;
470 
471 	return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
472 }
473 
ines_disable_eos(struct gpib_board * board)474 static void ines_disable_eos(struct gpib_board *board)
475 {
476 	struct ines_priv *priv = board->private_data;
477 
478 	nec7210_disable_eos(board, &priv->nec7210_priv);
479 }
480 
ines_update_status(struct gpib_board * board,unsigned int clear_mask)481 static unsigned int ines_update_status(struct gpib_board *board, unsigned int clear_mask)
482 {
483 	struct ines_priv *priv = board->private_data;
484 
485 	return nec7210_update_status(board, &priv->nec7210_priv, clear_mask);
486 }
487 
ines_primary_address(struct gpib_board * board,unsigned int address)488 static int ines_primary_address(struct gpib_board *board, unsigned int address)
489 {
490 	struct ines_priv *priv = board->private_data;
491 
492 	return nec7210_primary_address(board, &priv->nec7210_priv, address);
493 }
494 
ines_secondary_address(struct gpib_board * board,unsigned int address,int enable)495 static int ines_secondary_address(struct gpib_board *board, unsigned int address, int enable)
496 {
497 	struct ines_priv *priv = board->private_data;
498 
499 	return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
500 }
501 
ines_parallel_poll(struct gpib_board * board,u8 * result)502 static int ines_parallel_poll(struct gpib_board *board, u8 *result)
503 {
504 	struct ines_priv *priv = board->private_data;
505 
506 	return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
507 }
508 
ines_parallel_poll_configure(struct gpib_board * board,u8 config)509 static void ines_parallel_poll_configure(struct gpib_board *board, u8 config)
510 {
511 	struct ines_priv *priv = board->private_data;
512 
513 	nec7210_parallel_poll_configure(board, &priv->nec7210_priv, config);
514 }
515 
ines_parallel_poll_response(struct gpib_board * board,int ist)516 static void ines_parallel_poll_response(struct gpib_board *board, int ist)
517 {
518 	struct ines_priv *priv = board->private_data;
519 
520 	nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
521 }
522 
ines_serial_poll_response(struct gpib_board * board,u8 status)523 static void ines_serial_poll_response(struct gpib_board *board, u8 status)
524 {
525 	struct ines_priv *priv = board->private_data;
526 
527 	nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
528 }
529 
ines_serial_poll_status(struct gpib_board * board)530 static u8 ines_serial_poll_status(struct gpib_board *board)
531 {
532 	struct ines_priv *priv = board->private_data;
533 
534 	return nec7210_serial_poll_status(board, &priv->nec7210_priv);
535 }
536 
ines_return_to_local(struct gpib_board * board)537 static void ines_return_to_local(struct gpib_board *board)
538 {
539 	struct ines_priv *priv = board->private_data;
540 
541 	nec7210_return_to_local(board, &priv->nec7210_priv);
542 }
543 
544 static struct gpib_interface ines_pci_unaccel_interface = {
545 	.name = "ines_pci_unaccel",
546 	.attach = ines_pci_attach,
547 	.detach = ines_pci_detach,
548 	.read = ines_read,
549 	.write = ines_write,
550 	.command = ines_command,
551 	.take_control = ines_take_control,
552 	.go_to_standby = ines_go_to_standby,
553 	.request_system_control = ines_request_system_control,
554 	.interface_clear = ines_interface_clear,
555 	.remote_enable = ines_remote_enable,
556 	.enable_eos = ines_enable_eos,
557 	.disable_eos = ines_disable_eos,
558 	.parallel_poll = ines_parallel_poll,
559 	.parallel_poll_configure = ines_parallel_poll_configure,
560 	.parallel_poll_response = ines_parallel_poll_response,
561 	.local_parallel_poll_mode = NULL, // XXX
562 	.line_status = ines_line_status,
563 	.update_status = ines_update_status,
564 	.primary_address = ines_primary_address,
565 	.secondary_address = ines_secondary_address,
566 	.serial_poll_response = ines_serial_poll_response,
567 	.serial_poll_status = ines_serial_poll_status,
568 	.t1_delay = ines_t1_delay,
569 	.return_to_local = ines_return_to_local,
570 };
571 
572 static struct gpib_interface ines_pci_interface = {
573 	.name = "ines_pci",
574 	.attach = ines_pci_accel_attach,
575 	.detach = ines_pci_detach,
576 	.read = ines_accel_read,
577 	.write = ines_accel_write,
578 	.command = ines_command,
579 	.take_control = ines_take_control,
580 	.go_to_standby = ines_go_to_standby,
581 	.request_system_control = ines_request_system_control,
582 	.interface_clear = ines_interface_clear,
583 	.remote_enable = ines_remote_enable,
584 	.enable_eos = ines_enable_eos,
585 	.disable_eos = ines_disable_eos,
586 	.parallel_poll = ines_parallel_poll,
587 	.parallel_poll_configure = ines_parallel_poll_configure,
588 	.parallel_poll_response = ines_parallel_poll_response,
589 	.local_parallel_poll_mode = NULL, // XXX
590 	.line_status = ines_line_status,
591 	.update_status = ines_update_status,
592 	.primary_address = ines_primary_address,
593 	.secondary_address = ines_secondary_address,
594 	.serial_poll_response = ines_serial_poll_response,
595 	.serial_poll_status = ines_serial_poll_status,
596 	.t1_delay = ines_t1_delay,
597 	.return_to_local = ines_return_to_local,
598 };
599 
600 static struct gpib_interface ines_pci_accel_interface = {
601 	.name = "ines_pci_accel",
602 	.attach = ines_pci_accel_attach,
603 	.detach = ines_pci_detach,
604 	.read = ines_accel_read,
605 	.write = ines_accel_write,
606 	.command = ines_command,
607 	.take_control = ines_take_control,
608 	.go_to_standby = ines_go_to_standby,
609 	.request_system_control = ines_request_system_control,
610 	.interface_clear = ines_interface_clear,
611 	.remote_enable = ines_remote_enable,
612 	.enable_eos = ines_enable_eos,
613 	.disable_eos = ines_disable_eos,
614 	.parallel_poll = ines_parallel_poll,
615 	.parallel_poll_configure = ines_parallel_poll_configure,
616 	.parallel_poll_response = ines_parallel_poll_response,
617 	.local_parallel_poll_mode = NULL, // XXX
618 	.line_status = ines_line_status,
619 	.update_status = ines_update_status,
620 	.primary_address = ines_primary_address,
621 	.secondary_address = ines_secondary_address,
622 	.serial_poll_response = ines_serial_poll_response,
623 	.serial_poll_status = ines_serial_poll_status,
624 	.t1_delay = ines_t1_delay,
625 	.return_to_local = ines_return_to_local,
626 };
627 
628 static struct gpib_interface ines_isa_interface = {
629 	.name = "ines_isa",
630 	.attach = ines_isa_attach,
631 	.detach = ines_isa_detach,
632 	.read = ines_accel_read,
633 	.write = ines_accel_write,
634 	.command = ines_command,
635 	.take_control = ines_take_control,
636 	.go_to_standby = ines_go_to_standby,
637 	.request_system_control = ines_request_system_control,
638 	.interface_clear = ines_interface_clear,
639 	.remote_enable = ines_remote_enable,
640 	.enable_eos = ines_enable_eos,
641 	.disable_eos = ines_disable_eos,
642 	.parallel_poll = ines_parallel_poll,
643 	.parallel_poll_configure = ines_parallel_poll_configure,
644 	.parallel_poll_response = ines_parallel_poll_response,
645 	.local_parallel_poll_mode = NULL, // XXX
646 	.line_status = ines_line_status,
647 	.update_status = ines_update_status,
648 	.primary_address = ines_primary_address,
649 	.secondary_address = ines_secondary_address,
650 	.serial_poll_response = ines_serial_poll_response,
651 	.serial_poll_status = ines_serial_poll_status,
652 	.t1_delay = ines_t1_delay,
653 	.return_to_local = ines_return_to_local,
654 };
655 
ines_allocate_private(struct gpib_board * board)656 static int ines_allocate_private(struct gpib_board *board)
657 {
658 	struct ines_priv *priv;
659 
660 	board->private_data = kmalloc(sizeof(struct ines_priv), GFP_KERNEL);
661 	if (!board->private_data)
662 		return -1;
663 	priv = board->private_data;
664 	memset(priv, 0, sizeof(struct ines_priv));
665 	init_nec7210_private(&priv->nec7210_priv);
666 	return 0;
667 }
668 
ines_free_private(struct gpib_board * board)669 static void ines_free_private(struct gpib_board *board)
670 {
671 	kfree(board->private_data);
672 	board->private_data = NULL;
673 }
674 
ines_generic_attach(struct gpib_board * board)675 static int ines_generic_attach(struct gpib_board *board)
676 {
677 	struct ines_priv *ines_priv;
678 	struct nec7210_priv *nec_priv;
679 
680 	board->status = 0;
681 
682 	if (ines_allocate_private(board))
683 		return -ENOMEM;
684 	ines_priv = board->private_data;
685 	nec_priv = &ines_priv->nec7210_priv;
686 	nec_priv->read_byte = nec7210_ioport_read_byte;
687 	nec_priv->write_byte = nec7210_ioport_write_byte;
688 	nec_priv->offset = 1;
689 	nec_priv->type = IGPIB7210;
690 	ines_priv->pci_chip_type = PCI_CHIP_NONE;
691 
692 	return 0;
693 }
694 
ines_online(struct ines_priv * ines_priv,const struct gpib_board * board,int use_accel)695 static void ines_online(struct ines_priv *ines_priv, const struct gpib_board *board, int use_accel)
696 {
697 	struct nec7210_priv *nec_priv = &ines_priv->nec7210_priv;
698 
699 	/* ines doesn't seem to use internal count register */
700 	write_byte(nec_priv, ICR | 0, AUXMR);
701 
702 	write_byte(nec_priv, INES_AUX_XMODE, AUXMR);
703 	write_byte(nec_priv, INES_RFD_HLD_IMMEDIATE, AUXMR);
704 
705 	set_bit(RFD_HOLDOFF_BN, &nec_priv->state);
706 
707 	write_byte(nec_priv, INES_AUXD | 0, AUXMR);
708 	ines_outb(ines_priv, 0, XDMA_CONTROL);
709 	ines_priv->extend_mode_bits = 0;
710 	ines_outb(ines_priv, ines_priv->extend_mode_bits, EXTEND_MODE);
711 	if (use_accel) {
712 		ines_outb(ines_priv, 0x80, OUT_FIFO_WATERMARK);
713 		ines_outb(ines_priv, 0x80, IN_FIFO_WATERMARK);
714 		ines_outb(ines_priv, IFC_ACTIVE_BIT | ATN_ACTIVE_BIT |
715 			  FIFO_ERROR_BIT | XFER_COUNT_BIT, IMR3);
716 		ines_outb(ines_priv, IN_FIFO_WATERMARK_BIT | IN_FIFO_FULL_BIT |
717 			  OUT_FIFO_WATERMARK_BIT | OUT_FIFO_EMPTY_BIT, IMR4);
718 	} else {
719 		nec7210_set_reg_bits(nec_priv, ADMR, IN_FIFO_ENABLE_BIT | OUT_FIFO_ENABLE_BIT, 0);
720 		ines_outb(ines_priv, IFC_ACTIVE_BIT | FIFO_ERROR_BIT, IMR3);
721 		ines_outb(ines_priv, 0, IMR4);
722 	}
723 
724 	nec7210_board_online(nec_priv, board);
725 	if (use_accel)
726 		nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE | HR_DIIE, 0);
727 }
728 
ines_common_pci_attach(struct gpib_board * board,const struct gpib_board_config * config)729 static int ines_common_pci_attach(struct gpib_board *board, const struct gpib_board_config *config)
730 {
731 	struct ines_priv *ines_priv;
732 	struct nec7210_priv *nec_priv;
733 	int isr_flags = 0;
734 	int retval;
735 	struct ines_pci_id found_id;
736 	unsigned int i;
737 	struct pci_dev *pdev;
738 
739 	memset(&found_id, 0, sizeof(found_id));
740 
741 	retval = ines_generic_attach(board);
742 	if (retval)
743 		return retval;
744 
745 	ines_priv = board->private_data;
746 	nec_priv = &ines_priv->nec7210_priv;
747 
748 	// find board
749 	ines_priv->pci_device = NULL;
750 	for (i = 0; i < num_pci_chips && !ines_priv->pci_device; i++) {
751 		pdev = NULL;
752 		do {
753 			if (pci_ids[i].subsystem_vendor_id >= 0 &&
754 			    pci_ids[i].subsystem_device_id >= 0)
755 				pdev = pci_get_subsys(pci_ids[i].vendor_id, pci_ids[i].device_id,
756 						      pci_ids[i].subsystem_vendor_id,
757 						      pci_ids[i].subsystem_device_id, pdev);
758 			else
759 				pdev = pci_get_device(pci_ids[i].vendor_id, pci_ids[i].device_id,
760 						      pdev);
761 			if (!pdev)
762 				break;
763 			if (config->pci_bus >= 0 && config->pci_bus != pdev->bus->number)
764 				continue;
765 			if (config->pci_slot >= 0 && config->pci_slot != PCI_SLOT(pdev->devfn))
766 				continue;
767 			found_id = pci_ids[i];
768 			ines_priv->pci_device = pdev;
769 			break;
770 		} while (1);
771 	}
772 	if (!ines_priv->pci_device) {
773 		dev_err(board->gpib_dev, "could not find ines PCI board\n");
774 		return -1;
775 	}
776 
777 	if (pci_enable_device(ines_priv->pci_device)) {
778 		dev_err(board->gpib_dev, "error enabling pci device\n");
779 		return -1;
780 	}
781 
782 	if (pci_request_regions(ines_priv->pci_device, DRV_NAME))
783 		return -1;
784 	nec_priv->iobase = pci_resource_start(ines_priv->pci_device,
785 					      found_id.gpib_region);
786 
787 	ines_priv->pci_chip_type = found_id.pci_chip_type;
788 	nec_priv->offset = found_id.io_offset;
789 	switch (ines_priv->pci_chip_type) {
790 	case PCI_CHIP_PLX9050:
791 		ines_priv->plx_iobase = pci_resource_start(ines_priv->pci_device, 1);
792 		break;
793 	case PCI_CHIP_AMCC5920:
794 		ines_priv->amcc_iobase = pci_resource_start(ines_priv->pci_device, 0);
795 		break;
796 	case PCI_CHIP_QUANCOM:
797 		break;
798 	case PCI_CHIP_QUICKLOGIC5030:
799 		break;
800 	default:
801 		dev_err(board->gpib_dev, "unspecified chip type? (bug)\n");
802 		nec_priv->iobase = 0;
803 		pci_release_regions(ines_priv->pci_device);
804 		return -1;
805 	}
806 
807 	nec7210_board_reset(nec_priv, board);
808 #ifdef QUANCOM_PCI
809 	if (ines_priv->pci_chip_type == PCI_CHIP_QUANCOM) {
810 		/* change interrupt polarity */
811 		nec_priv->auxb_bits |= HR_INV;
812 		ines_outb(ines_priv, nec_priv->auxb_bits, AUXMR);
813 	}
814 #endif
815 	isr_flags |= IRQF_SHARED;
816 	if (request_irq(ines_priv->pci_device->irq, ines_pci_interrupt, isr_flags,
817 			DRV_NAME, board)) {
818 		dev_err(board->gpib_dev, "can't request IRQ %d\n", ines_priv->pci_device->irq);
819 		return -1;
820 	}
821 	ines_priv->irq = ines_priv->pci_device->irq;
822 
823 	// enable interrupts on pci chip
824 	switch (ines_priv->pci_chip_type) {
825 	case PCI_CHIP_PLX9050:
826 		outl(PLX9050_LINTR1_EN_BIT | PLX9050_LINTR1_POLARITY_BIT | PLX9050_PCI_INTR_EN_BIT,
827 		     ines_priv->plx_iobase + PLX9050_INTCSR_REG);
828 		break;
829 	case PCI_CHIP_AMCC5920:
830 	{
831 		static const int region = 1;
832 		static const int num_wait_states = 7;
833 		u32 bits;
834 
835 		bits = amcc_prefetch_bits(region, PREFETCH_DISABLED);
836 		bits |= amcc_PTADR_mode_bit(region);
837 		bits |= amcc_disable_write_fifo_bit(region);
838 		bits |= amcc_wait_state_bits(region, num_wait_states);
839 		outl(bits, ines_priv->amcc_iobase + AMCC_PASS_THRU_REG);
840 		outl(AMCC_ADDON_INTR_ENABLE_BIT, ines_priv->amcc_iobase + AMCC_INTCS_REG);
841 	}
842 	break;
843 	case PCI_CHIP_QUANCOM:
844 		outb(QUANCOM_IRQ_ENABLE_BIT, nec_priv->iobase +
845 		     QUANCOM_IRQ_CONTROL_STATUS_REG);
846 		break;
847 	case PCI_CHIP_QUICKLOGIC5030:
848 		break;
849 	default:
850 		dev_err(board->gpib_dev, "unspecified chip type? (bug)\n");
851 		return -1;
852 	}
853 
854 	return 0;
855 }
856 
ines_pci_attach(struct gpib_board * board,const struct gpib_board_config * config)857 static int ines_pci_attach(struct gpib_board *board, const struct gpib_board_config *config)
858 {
859 	struct ines_priv *ines_priv;
860 	int retval;
861 
862 	retval = ines_common_pci_attach(board, config);
863 	if (retval < 0)
864 		return retval;
865 
866 	ines_priv = board->private_data;
867 	ines_online(ines_priv, board, 0);
868 
869 	return 0;
870 }
871 
ines_pci_accel_attach(struct gpib_board * board,const struct gpib_board_config * config)872 static int ines_pci_accel_attach(struct gpib_board *board, const struct gpib_board_config *config)
873 {
874 	struct ines_priv *ines_priv;
875 	int retval;
876 
877 	retval = ines_common_pci_attach(board, config);
878 	if (retval < 0)
879 		return retval;
880 
881 	ines_priv = board->private_data;
882 	ines_online(ines_priv, board, 1);
883 
884 	return 0;
885 }
886 
887 static const int ines_isa_iosize = 0x20;
888 
ines_isa_attach(struct gpib_board * board,const struct gpib_board_config * config)889 static int ines_isa_attach(struct gpib_board *board, const struct gpib_board_config *config)
890 {
891 	struct ines_priv *ines_priv;
892 	struct nec7210_priv *nec_priv;
893 	int isr_flags = 0;
894 	int retval;
895 
896 	retval = ines_generic_attach(board);
897 	if (retval)
898 		return retval;
899 
900 	ines_priv = board->private_data;
901 	nec_priv = &ines_priv->nec7210_priv;
902 
903 	if (!request_region(config->ibbase, ines_isa_iosize, DRV_NAME)) {
904 		dev_err(board->gpib_dev, "ioports at 0x%x already in use\n",
905 			config->ibbase);
906 		return -EBUSY;
907 	}
908 	nec_priv->iobase = config->ibbase;
909 	nec_priv->offset = 1;
910 	nec7210_board_reset(nec_priv, board);
911 	if (request_irq(config->ibirq, ines_pci_interrupt, isr_flags, DRV_NAME, board)) {
912 		dev_err(board->gpib_dev, "failed to allocate IRQ %d\n", config->ibirq);
913 		return -1;
914 	}
915 	ines_priv->irq = config->ibirq;
916 	ines_online(ines_priv, board, 1);
917 	return 0;
918 }
919 
ines_pci_detach(struct gpib_board * board)920 static void ines_pci_detach(struct gpib_board *board)
921 {
922 	struct ines_priv *ines_priv = board->private_data;
923 	struct nec7210_priv *nec_priv;
924 
925 	if (ines_priv) {
926 		nec_priv = &ines_priv->nec7210_priv;
927 		if (ines_priv->irq) {
928 			// disable interrupts
929 			switch (ines_priv->pci_chip_type) {
930 			case PCI_CHIP_AMCC5920:
931 				if (ines_priv->plx_iobase)
932 					outl(0, ines_priv->plx_iobase + PLX9050_INTCSR_REG);
933 				break;
934 			case PCI_CHIP_QUANCOM:
935 				if (nec_priv->iobase)
936 					outb(0, nec_priv->iobase +
937 					     QUANCOM_IRQ_CONTROL_STATUS_REG);
938 				break;
939 			default:
940 				break;
941 			}
942 			free_irq(ines_priv->irq, board);
943 		}
944 		if (nec_priv->iobase) {
945 			nec7210_board_reset(nec_priv, board);
946 			pci_release_regions(ines_priv->pci_device);
947 		}
948 		if (ines_priv->pci_device)
949 			pci_dev_put(ines_priv->pci_device);
950 	}
951 	ines_free_private(board);
952 }
953 
ines_isa_detach(struct gpib_board * board)954 static void ines_isa_detach(struct gpib_board *board)
955 {
956 	struct ines_priv *ines_priv = board->private_data;
957 	struct nec7210_priv *nec_priv;
958 
959 	if (ines_priv) {
960 		nec_priv = &ines_priv->nec7210_priv;
961 		if (ines_priv->irq)
962 			free_irq(ines_priv->irq, board);
963 		if (nec_priv->iobase) {
964 			nec7210_board_reset(nec_priv, board);
965 			release_region(nec_priv->iobase, ines_isa_iosize);
966 		}
967 	}
968 	ines_free_private(board);
969 }
970 
ines_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)971 static int ines_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
972 {
973 	return 0;
974 }
975 
976 static struct pci_driver ines_pci_driver = {
977 	.name = "ines_gpib",
978 	.id_table = ines_pci_table,
979 	.probe = &ines_pci_probe
980 };
981 
982 #ifdef CONFIG_GPIB_PCMCIA
983 
984 #include <linux/kernel.h>
985 #include <linux/ptrace.h>
986 #include <linux/string.h>
987 #include <linux/timer.h>
988 
989 #include <pcmcia/cistpl.h>
990 #include <pcmcia/ds.h>
991 #include <pcmcia/cisreg.h>
992 
993 static const int ines_pcmcia_iosize = 0x20;
994 
995 /*
996  * The event() function is this driver's Card Services event handler.
997  * It will be called by Card Services when an appropriate card status
998  * event is received.  The config() and release() entry points are
999  * used to configure or release a socket, in response to card insertion
1000  * and ejection events.  They are invoked from the gpib event
1001  * handler.
1002  */
1003 
1004 static int ines_gpib_config(struct pcmcia_device  *link);
1005 static void ines_gpib_release(struct pcmcia_device  *link);
1006 static int ines_pcmcia_attach(struct gpib_board *board, const struct gpib_board_config *config);
1007 static int ines_pcmcia_accel_attach(struct gpib_board *board,
1008 				    const struct gpib_board_config *config);
1009 static void ines_pcmcia_detach(struct gpib_board *board);
1010 static int ines_common_pcmcia_attach(struct gpib_board *board);
1011 /*
1012  * A linked list of "instances" of the gpib device.  Each actual
1013  * PCMCIA card corresponds to one device instance, and is described
1014  * by one dev_link_t structure (defined in ds.h).
1015  *
1016  * You may not want to use a linked list for this -- for example, the
1017  * memory card driver uses an array of dev_link_t pointers, where minor
1018  * device numbers are used to derive the corresponding array index.
1019  */
1020 
1021 static struct pcmcia_device *curr_dev;
1022 
1023 /*
1024  * A dev_link_t structure has fields for most things that are needed
1025  * to keep track of a socket, but there will usually be some device
1026  * specific information that also needs to be kept track of.  The
1027  * 'priv' pointer in a dev_link_t structure can be used to point to
1028  * a device-specific private data structure, like this.
1029  *
1030  * A driver needs to provide a dev_node_t structure for each device
1031  * on a card.	In some cases, there is only one device per card (for
1032  * example, ethernet cards, modems).  In other cases, there may be
1033  * many actual or logical devices (SCSI adapters, memory cards with
1034  * multiple partitions).  The dev_node_t structures need to be kept
1035  * in a linked list starting at the 'dev' field of a dev_link_t
1036  * structure.	We allocate them in the card's private data structure,
1037  * because they generally can't be allocated dynamically.
1038  */
1039 
1040 struct local_info {
1041 	struct pcmcia_device	*p_dev;
1042 	struct gpib_board		*dev;
1043 	u_short manfid;
1044 	u_short cardid;
1045 };
1046 
1047 /*
1048  * gpib_attach() creates an "instance" of the driver, allocating
1049  * local data structures for one device.  The device is registered
1050  * with Card Services.
1051  *
1052  * The dev_link structure is initialized, but we don't actually
1053  * configure the card at this point -- we wait until we receive a
1054  * card insertion event.
1055  */
ines_gpib_probe(struct pcmcia_device * link)1056 static int ines_gpib_probe(struct pcmcia_device *link)
1057 {
1058 	struct local_info *info;
1059 
1060 //	int ret, i;
1061 
1062 	/* Allocate space for private device-specific data */
1063 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1064 	if (!info)
1065 		return -ENOMEM;
1066 
1067 	info->p_dev = link;
1068 	link->priv = info;
1069 
1070 	/* The io structure describes IO port mapping */
1071 	link->resource[0]->end = 32;
1072 	link->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
1073 	link->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
1074 	link->io_lines = 5;
1075 
1076 	/* General socket configuration */
1077 	link->config_flags = CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
1078 
1079 	/* Register with Card Services */
1080 	curr_dev = link;
1081 	return ines_gpib_config(link);
1082 }
1083 
1084 /*
1085  * This deletes a driver "instance".	The device is de-registered
1086  * with Card Services.  If it has been released, all local data
1087  * structures are freed.  Otherwise, the structures will be freed
1088  * when the device is released.
1089  */
ines_gpib_remove(struct pcmcia_device * link)1090 static void ines_gpib_remove(struct pcmcia_device *link)
1091 {
1092 	struct local_info *info = link->priv;
1093 	//struct struct gpib_board *dev = info->dev;
1094 
1095 	if (info->dev)
1096 		ines_pcmcia_detach(info->dev);
1097 	ines_gpib_release(link);
1098 
1099 	//free_netdev(dev);
1100 	kfree(info);
1101 }
1102 
ines_gpib_config_iteration(struct pcmcia_device * link,void * priv_data)1103 static int ines_gpib_config_iteration(struct pcmcia_device *link, void *priv_data)
1104 {
1105 	return pcmcia_request_io(link);
1106 }
1107 
1108 /*
1109  * gpib_config() is scheduled to run after a CARD_INSERTION event
1110  * is received, to configure the PCMCIA socket, and to make the
1111  * device available to the system.
1112  */
ines_gpib_config(struct pcmcia_device * link)1113 static int ines_gpib_config(struct pcmcia_device *link)
1114 {
1115 	int retval;
1116 	void __iomem *virt;
1117 
1118 	retval = pcmcia_loop_config(link, &ines_gpib_config_iteration, NULL);
1119 	if (retval) {
1120 		dev_warn(&link->dev, "no configuration found\n");
1121 		ines_gpib_release(link);
1122 		return -ENODEV;
1123 	}
1124 
1125 	dev_dbg(&link->dev, "ines_cs: manufacturer: 0x%x card: 0x%x\n",
1126 		link->manf_id, link->card_id);
1127 
1128 	/*
1129 	 * for the ines card we have to setup the configuration registers in
1130 	 * attribute memory here
1131 	 */
1132 	link->resource[2]->flags |= WIN_MEMORY_TYPE_AM | WIN_DATA_WIDTH_8 | WIN_ENABLE;
1133 	link->resource[2]->end = 0x1000;
1134 	retval = pcmcia_request_window(link, link->resource[2], 250);
1135 	if (retval) {
1136 		dev_warn(&link->dev, "pcmcia_request_window failed\n");
1137 		ines_gpib_release(link);
1138 		return -ENODEV;
1139 	}
1140 	retval = pcmcia_map_mem_page(link, link->resource[2], 0);
1141 	if (retval) {
1142 		dev_warn(&link->dev, "pcmcia_map_mem_page failed\n");
1143 		ines_gpib_release(link);
1144 		return -ENODEV;
1145 	}
1146 	virt = ioremap(link->resource[2]->start, resource_size(link->resource[2]));
1147 	writeb((link->resource[2]->start >> 2) & 0xff, virt + 0xf0); // IOWindow base
1148 	iounmap(virt);
1149 
1150 	/*
1151 	 * This actually configures the PCMCIA socket -- setting up
1152 	 * the I/O windows and the interrupt mapping.
1153 	 */
1154 	retval = pcmcia_enable_device(link);
1155 	if (retval) {
1156 		ines_gpib_release(link);
1157 		return -ENODEV;
1158 	}
1159 	return 0;
1160 } /* gpib_config */
1161 
1162 /*
1163  * After a card is removed, gpib_release() will unregister the net
1164  * device, and release the PCMCIA configuration.  If the device is
1165  * still open, this will be postponed until it is closed.
1166  */
1167 
ines_gpib_release(struct pcmcia_device * link)1168 static void ines_gpib_release(struct pcmcia_device *link)
1169 {
1170 	pcmcia_disable_device(link);
1171 } /* gpib_release */
1172 
ines_gpib_suspend(struct pcmcia_device * link)1173 static int ines_gpib_suspend(struct pcmcia_device *link)
1174 {
1175 	//struct local_info *info = link->priv;
1176 	//struct struct gpib_board *dev = info->dev;
1177 
1178 	if (link->open)
1179 		dev_err(&link->dev, "Device still open\n");
1180 	//netif_device_detach(dev);
1181 
1182 	return 0;
1183 }
1184 
ines_gpib_resume(struct pcmcia_device * link)1185 static int ines_gpib_resume(struct pcmcia_device *link)
1186 {
1187 	//struct local_info_t *info = link->priv;
1188 	//struct struct gpib_board *dev = info->dev;
1189 
1190 	/*if (link->open) {
1191 	 *	ni_gpib_probe(dev);	/ really?
1192 	 *	//netif_device_attach(dev);
1193 	 *}
1194 	 */
1195 	return ines_gpib_config(link);
1196 }
1197 
1198 static struct pcmcia_device_id ines_pcmcia_ids[] = {
1199 	PCMCIA_DEVICE_MANF_CARD(0x01b4, 0x4730),
1200 	PCMCIA_DEVICE_NULL
1201 };
1202 MODULE_DEVICE_TABLE(pcmcia, ines_pcmcia_ids);
1203 
1204 static struct pcmcia_driver ines_gpib_cs_driver = {
1205 	.owner		= THIS_MODULE,
1206 	.name		= "ines_gpib_cs",
1207 	.id_table	= ines_pcmcia_ids,
1208 	.probe		= ines_gpib_probe,
1209 	.remove		= ines_gpib_remove,
1210 	.suspend	= ines_gpib_suspend,
1211 	.resume		= ines_gpib_resume,
1212 };
1213 
ines_pcmcia_cleanup_module(void)1214 static void ines_pcmcia_cleanup_module(void)
1215 {
1216 	pcmcia_unregister_driver(&ines_gpib_cs_driver);
1217 }
1218 
1219 static struct gpib_interface ines_pcmcia_unaccel_interface = {
1220 	.name = "ines_pcmcia_unaccel",
1221 	.attach = ines_pcmcia_attach,
1222 	.detach = ines_pcmcia_detach,
1223 	.read = ines_read,
1224 	.write = ines_write,
1225 	.command = ines_command,
1226 	.take_control = ines_take_control,
1227 	.go_to_standby = ines_go_to_standby,
1228 	.request_system_control = ines_request_system_control,
1229 	.interface_clear = ines_interface_clear,
1230 	.remote_enable = ines_remote_enable,
1231 	.enable_eos = ines_enable_eos,
1232 	.disable_eos = ines_disable_eos,
1233 	.parallel_poll = ines_parallel_poll,
1234 	.parallel_poll_configure = ines_parallel_poll_configure,
1235 	.parallel_poll_response = ines_parallel_poll_response,
1236 	.local_parallel_poll_mode = NULL, // XXX
1237 	.line_status = ines_line_status,
1238 	.update_status = ines_update_status,
1239 	.primary_address = ines_primary_address,
1240 	.secondary_address = ines_secondary_address,
1241 	.serial_poll_response = ines_serial_poll_response,
1242 	.serial_poll_status = ines_serial_poll_status,
1243 	.t1_delay = ines_t1_delay,
1244 	.return_to_local = ines_return_to_local,
1245 };
1246 
1247 static struct gpib_interface ines_pcmcia_accel_interface = {
1248 	.name = "ines_pcmcia_accel",
1249 	.attach = ines_pcmcia_accel_attach,
1250 	.detach = ines_pcmcia_detach,
1251 	.read = ines_accel_read,
1252 	.write = ines_accel_write,
1253 	.command = ines_command,
1254 	.take_control = ines_take_control,
1255 	.go_to_standby = ines_go_to_standby,
1256 	.request_system_control = ines_request_system_control,
1257 	.interface_clear = ines_interface_clear,
1258 	.remote_enable = ines_remote_enable,
1259 	.enable_eos = ines_enable_eos,
1260 	.disable_eos = ines_disable_eos,
1261 	.parallel_poll = ines_parallel_poll,
1262 	.parallel_poll_configure = ines_parallel_poll_configure,
1263 	.parallel_poll_response = ines_parallel_poll_response,
1264 	.local_parallel_poll_mode = NULL, // XXX
1265 	.line_status = ines_line_status,
1266 	.update_status = ines_update_status,
1267 	.primary_address = ines_primary_address,
1268 	.secondary_address = ines_secondary_address,
1269 	.serial_poll_response = ines_serial_poll_response,
1270 	.serial_poll_status = ines_serial_poll_status,
1271 	.t1_delay = ines_t1_delay,
1272 	.return_to_local = ines_return_to_local,
1273 };
1274 
1275 static struct gpib_interface ines_pcmcia_interface = {
1276 	.name = "ines_pcmcia",
1277 	.attach = ines_pcmcia_accel_attach,
1278 	.detach = ines_pcmcia_detach,
1279 	.read = ines_accel_read,
1280 	.write = ines_accel_write,
1281 	.command = ines_command,
1282 	.take_control = ines_take_control,
1283 	.go_to_standby = ines_go_to_standby,
1284 	.request_system_control = ines_request_system_control,
1285 	.interface_clear = ines_interface_clear,
1286 	.remote_enable = ines_remote_enable,
1287 	.enable_eos = ines_enable_eos,
1288 	.disable_eos = ines_disable_eos,
1289 	.parallel_poll = ines_parallel_poll,
1290 	.parallel_poll_configure = ines_parallel_poll_configure,
1291 	.parallel_poll_response = ines_parallel_poll_response,
1292 	.local_parallel_poll_mode = NULL, // XXX
1293 	.line_status = ines_line_status,
1294 	.update_status = ines_update_status,
1295 	.primary_address = ines_primary_address,
1296 	.secondary_address = ines_secondary_address,
1297 	.serial_poll_response = ines_serial_poll_response,
1298 	.serial_poll_status = ines_serial_poll_status,
1299 	.t1_delay = ines_t1_delay,
1300 	.return_to_local = ines_return_to_local,
1301 };
1302 
ines_pcmcia_interrupt(int irq,void * arg)1303 static irqreturn_t ines_pcmcia_interrupt(int irq, void *arg)
1304 {
1305 	struct gpib_board *board = arg;
1306 
1307 	return ines_interrupt(board);
1308 }
1309 
ines_common_pcmcia_attach(struct gpib_board * board)1310 static int ines_common_pcmcia_attach(struct gpib_board *board)
1311 {
1312 	struct ines_priv *ines_priv;
1313 	struct nec7210_priv *nec_priv;
1314 	int retval;
1315 
1316 	if (!curr_dev) {
1317 		dev_err(board->gpib_dev, "no ines pcmcia cards found\n");
1318 		return -1;
1319 	}
1320 
1321 	retval = ines_generic_attach(board);
1322 	if (retval)
1323 		return retval;
1324 
1325 	ines_priv = board->private_data;
1326 	nec_priv = &ines_priv->nec7210_priv;
1327 
1328 	if (!request_region(curr_dev->resource[0]->start,
1329 			    resource_size(curr_dev->resource[0]), DRV_NAME)) {
1330 		dev_err(board->gpib_dev, "ioports at 0x%lx already in use\n",
1331 			(unsigned long)(curr_dev->resource[0]->start));
1332 		return -1;
1333 	}
1334 
1335 	nec_priv->iobase = curr_dev->resource[0]->start;
1336 
1337 	nec7210_board_reset(nec_priv, board);
1338 
1339 	if (request_irq(curr_dev->irq, ines_pcmcia_interrupt, IRQF_SHARED,
1340 			"pcmcia-gpib", board))	{
1341 		dev_err(board->gpib_dev, "can't request IRQ %d\n", curr_dev->irq);
1342 		return -1;
1343 	}
1344 	ines_priv->irq = curr_dev->irq;
1345 
1346 	return 0;
1347 }
1348 
ines_pcmcia_attach(struct gpib_board * board,const struct gpib_board_config * config)1349 static int ines_pcmcia_attach(struct gpib_board *board, const struct gpib_board_config *config)
1350 {
1351 	struct ines_priv *ines_priv;
1352 	int retval;
1353 
1354 	retval = ines_common_pcmcia_attach(board);
1355 	if (retval < 0)
1356 		return retval;
1357 
1358 	ines_priv = board->private_data;
1359 	ines_online(ines_priv, board, 0);
1360 
1361 	return 0;
1362 }
1363 
ines_pcmcia_accel_attach(struct gpib_board * board,const struct gpib_board_config * config)1364 static int ines_pcmcia_accel_attach(struct gpib_board *board,
1365 				    const struct gpib_board_config *config)
1366 {
1367 	struct ines_priv *ines_priv;
1368 	int retval;
1369 
1370 	retval = ines_common_pcmcia_attach(board);
1371 	if (retval < 0)
1372 		return retval;
1373 
1374 	ines_priv = board->private_data;
1375 	ines_online(ines_priv, board, 1);
1376 
1377 	return 0;
1378 }
1379 
ines_pcmcia_detach(struct gpib_board * board)1380 static void ines_pcmcia_detach(struct gpib_board *board)
1381 {
1382 	struct ines_priv *ines_priv = board->private_data;
1383 	struct nec7210_priv *nec_priv;
1384 
1385 	if (ines_priv) {
1386 		nec_priv = &ines_priv->nec7210_priv;
1387 		if (ines_priv->irq)
1388 			free_irq(ines_priv->irq, board);
1389 		if (nec_priv->iobase) {
1390 			nec7210_board_reset(nec_priv, board);
1391 			release_region(nec_priv->iobase, ines_pcmcia_iosize);
1392 		}
1393 	}
1394 	ines_free_private(board);
1395 }
1396 
1397 #endif /* CONFIG_GPIB_PCMCIA */
1398 
ines_init_module(void)1399 static int __init ines_init_module(void)
1400 {
1401 	int ret;
1402 
1403 	ret = pci_register_driver(&ines_pci_driver);
1404 	if (ret) {
1405 		pr_err("pci_register_driver failed: error = %d\n", ret);
1406 		return ret;
1407 	}
1408 
1409 	ret = gpib_register_driver(&ines_pci_interface, THIS_MODULE);
1410 	if (ret) {
1411 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1412 		goto err_pci;
1413 	}
1414 
1415 	ret = gpib_register_driver(&ines_pci_unaccel_interface, THIS_MODULE);
1416 	if (ret) {
1417 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1418 		goto err_pci_unaccel;
1419 	}
1420 
1421 	ret = gpib_register_driver(&ines_pci_accel_interface, THIS_MODULE);
1422 	if (ret) {
1423 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1424 		goto err_pci_accel;
1425 	}
1426 
1427 	ret = gpib_register_driver(&ines_isa_interface, THIS_MODULE);
1428 	if (ret) {
1429 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1430 		goto err_isa;
1431 	}
1432 
1433 #ifdef CONFIG_GPIB_PCMCIA
1434 	ret = gpib_register_driver(&ines_pcmcia_interface, THIS_MODULE);
1435 	if (ret) {
1436 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1437 		goto err_pcmcia;
1438 	}
1439 
1440 	ret = gpib_register_driver(&ines_pcmcia_unaccel_interface, THIS_MODULE);
1441 	if (ret) {
1442 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1443 		goto err_pcmcia_unaccel;
1444 	}
1445 
1446 	ret = gpib_register_driver(&ines_pcmcia_accel_interface, THIS_MODULE);
1447 	if (ret) {
1448 		pr_err("gpib_register_driver failed: error = %d\n", ret);
1449 		goto err_pcmcia_accel;
1450 	}
1451 
1452 	ret = pcmcia_register_driver(&ines_gpib_cs_driver);
1453 	if (ret) {
1454 		pr_err("pcmcia_register_driver failed: error = %d\n", ret);
1455 		goto err_pcmcia_driver;
1456 	}
1457 #endif
1458 
1459 	return 0;
1460 
1461 #ifdef CONFIG_GPIB_PCMCIA
1462 err_pcmcia_driver:
1463 	gpib_unregister_driver(&ines_pcmcia_accel_interface);
1464 err_pcmcia_accel:
1465 	gpib_unregister_driver(&ines_pcmcia_unaccel_interface);
1466 err_pcmcia_unaccel:
1467 	gpib_unregister_driver(&ines_pcmcia_interface);
1468 err_pcmcia:
1469 #endif
1470 	gpib_unregister_driver(&ines_isa_interface);
1471 err_isa:
1472 	gpib_unregister_driver(&ines_pci_accel_interface);
1473 err_pci_accel:
1474 	gpib_unregister_driver(&ines_pci_unaccel_interface);
1475 err_pci_unaccel:
1476 	gpib_unregister_driver(&ines_pci_interface);
1477 err_pci:
1478 	pci_unregister_driver(&ines_pci_driver);
1479 
1480 	return ret;
1481 }
1482 
ines_exit_module(void)1483 static void __exit ines_exit_module(void)
1484 {
1485 	gpib_unregister_driver(&ines_pci_interface);
1486 	gpib_unregister_driver(&ines_pci_unaccel_interface);
1487 	gpib_unregister_driver(&ines_pci_accel_interface);
1488 	gpib_unregister_driver(&ines_isa_interface);
1489 #ifdef CONFIG_GPIB_PCMCIA
1490 	gpib_unregister_driver(&ines_pcmcia_interface);
1491 	gpib_unregister_driver(&ines_pcmcia_unaccel_interface);
1492 	gpib_unregister_driver(&ines_pcmcia_accel_interface);
1493 	ines_pcmcia_cleanup_module();
1494 #endif
1495 
1496 	pci_unregister_driver(&ines_pci_driver);
1497 }
1498 
1499 module_init(ines_init_module);
1500 module_exit(ines_exit_module);
1501