xref: /linux/drivers/gpib/eastwood/fluke_gpib.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  * GPIB Driver for Fluke cda devices.  Basically, its a driver for a (bugfixed)
5  * cb7210 connected to channel 0 of a pl330 dma controller.
6  *    Author: Frank Mori Hess <fmh6jj@gmail.com>
7  *   copyright: (C) 2006, 2010, 2015 Fluke Corporation
8  ***************************************************************************/
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define dev_fmt pr_fmt
12 #define DRV_NAME KBUILD_MODNAME
13 
14 #include "fluke_gpib.h"
15 
16 #include "gpibP.h"
17 #include <linux/dma-mapping.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 
23 MODULE_LICENSE("GPL");
24 MODULE_DESCRIPTION("GPIB Driver for Fluke cda devices");
25 
26 static int fluke_attach_holdoff_all(struct gpib_board *board,
27 				    const struct gpib_board_config *config);
28 static int fluke_attach_holdoff_end(struct gpib_board *board,
29 				    const struct gpib_board_config *config);
30 static void fluke_detach(struct gpib_board *board);
31 static int fluke_config_dma(struct gpib_board *board, int output);
32 static irqreturn_t fluke_gpib_internal_interrupt(struct gpib_board *board);
33 
34 static struct platform_device *fluke_gpib_pdev;
35 
fluke_locking_read_byte(struct nec7210_priv * nec_priv,unsigned int register_number)36 static u8 fluke_locking_read_byte(struct nec7210_priv *nec_priv, unsigned int register_number)
37 {
38 	u8 retval;
39 	unsigned long flags;
40 
41 	spin_lock_irqsave(&nec_priv->register_page_lock, flags);
42 	retval = fluke_read_byte_nolock(nec_priv, register_number);
43 	spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
44 	return retval;
45 }
46 
fluke_locking_write_byte(struct nec7210_priv * nec_priv,u8 byte,unsigned int register_number)47 static void fluke_locking_write_byte(struct nec7210_priv *nec_priv, u8 byte,
48 				     unsigned int register_number)
49 {
50 	unsigned long flags;
51 
52 	spin_lock_irqsave(&nec_priv->register_page_lock, flags);
53 	fluke_write_byte_nolock(nec_priv, byte, register_number);
54 	spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
55 }
56 
57 // wrappers for interface functions
fluke_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)58 static int fluke_read(struct gpib_board *board, u8 *buffer, size_t length, int *end,
59 		      size_t *bytes_read)
60 {
61 	struct fluke_priv *priv = board->private_data;
62 
63 	return nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
64 }
65 
fluke_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)66 static int fluke_write(struct gpib_board *board, u8 *buffer, size_t length,
67 		       int send_eoi, size_t *bytes_written)
68 {
69 	struct fluke_priv *priv = board->private_data;
70 
71 	return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
72 }
73 
fluke_command(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)74 static int fluke_command(struct gpib_board *board, u8 *buffer,
75 			 size_t length, size_t *bytes_written)
76 {
77 	struct fluke_priv *priv = board->private_data;
78 
79 	return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
80 }
81 
fluke_take_control(struct gpib_board * board,int synchronous)82 static int fluke_take_control(struct gpib_board *board, int synchronous)
83 {
84 	struct fluke_priv *priv = board->private_data;
85 
86 	return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
87 }
88 
fluke_go_to_standby(struct gpib_board * board)89 static int fluke_go_to_standby(struct gpib_board *board)
90 {
91 	struct fluke_priv *priv = board->private_data;
92 
93 	return nec7210_go_to_standby(board, &priv->nec7210_priv);
94 }
95 
fluke_request_system_control(struct gpib_board * board,int request_control)96 static int fluke_request_system_control(struct gpib_board *board, int request_control)
97 {
98 	struct fluke_priv *priv = board->private_data;
99 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
100 
101 	return nec7210_request_system_control(board, nec_priv, request_control);
102 }
103 
fluke_interface_clear(struct gpib_board * board,int assert)104 static void fluke_interface_clear(struct gpib_board *board, int assert)
105 {
106 	struct fluke_priv *priv = board->private_data;
107 
108 	nec7210_interface_clear(board, &priv->nec7210_priv, assert);
109 }
110 
fluke_remote_enable(struct gpib_board * board,int enable)111 static void fluke_remote_enable(struct gpib_board *board, int enable)
112 {
113 	struct fluke_priv *priv = board->private_data;
114 
115 	nec7210_remote_enable(board, &priv->nec7210_priv, enable);
116 }
117 
fluke_enable_eos(struct gpib_board * board,u8 eos_byte,int compare_8_bits)118 static int fluke_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits)
119 {
120 	struct fluke_priv *priv = board->private_data;
121 
122 	return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
123 }
124 
fluke_disable_eos(struct gpib_board * board)125 static void fluke_disable_eos(struct gpib_board *board)
126 {
127 	struct fluke_priv *priv = board->private_data;
128 
129 	nec7210_disable_eos(board, &priv->nec7210_priv);
130 }
131 
fluke_update_status(struct gpib_board * board,unsigned int clear_mask)132 static unsigned int fluke_update_status(struct gpib_board *board, unsigned int clear_mask)
133 {
134 	struct fluke_priv *priv = board->private_data;
135 
136 	return nec7210_update_status(board, &priv->nec7210_priv, clear_mask);
137 }
138 
fluke_primary_address(struct gpib_board * board,unsigned int address)139 static int fluke_primary_address(struct gpib_board *board, unsigned int address)
140 {
141 	struct fluke_priv *priv = board->private_data;
142 
143 	return nec7210_primary_address(board, &priv->nec7210_priv, address);
144 }
145 
fluke_secondary_address(struct gpib_board * board,unsigned int address,int enable)146 static int fluke_secondary_address(struct gpib_board *board, unsigned int address, int enable)
147 {
148 	struct fluke_priv *priv = board->private_data;
149 
150 	return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
151 }
152 
fluke_parallel_poll(struct gpib_board * board,u8 * result)153 static int fluke_parallel_poll(struct gpib_board *board, u8 *result)
154 {
155 	struct fluke_priv *priv = board->private_data;
156 
157 	return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
158 }
159 
fluke_parallel_poll_configure(struct gpib_board * board,u8 configuration)160 static void fluke_parallel_poll_configure(struct gpib_board *board, u8 configuration)
161 {
162 	struct fluke_priv *priv = board->private_data;
163 
164 	nec7210_parallel_poll_configure(board, &priv->nec7210_priv, configuration);
165 }
166 
fluke_parallel_poll_response(struct gpib_board * board,int ist)167 static void fluke_parallel_poll_response(struct gpib_board *board, int ist)
168 {
169 	struct fluke_priv *priv = board->private_data;
170 
171 	nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
172 }
173 
fluke_serial_poll_response(struct gpib_board * board,u8 status)174 static void fluke_serial_poll_response(struct gpib_board *board, u8 status)
175 {
176 	struct fluke_priv *priv = board->private_data;
177 
178 	nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
179 }
180 
fluke_serial_poll_status(struct gpib_board * board)181 static u8 fluke_serial_poll_status(struct gpib_board *board)
182 {
183 	struct fluke_priv *priv = board->private_data;
184 
185 	return nec7210_serial_poll_status(board, &priv->nec7210_priv);
186 }
187 
fluke_return_to_local(struct gpib_board * board)188 static void fluke_return_to_local(struct gpib_board *board)
189 {
190 	struct fluke_priv *priv = board->private_data;
191 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
192 
193 	write_byte(nec_priv, AUX_RTL2, AUXMR);
194 	udelay(1);
195 	write_byte(nec_priv, AUX_RTL, AUXMR);
196 }
197 
fluke_line_status(const struct gpib_board * board)198 static int fluke_line_status(const struct gpib_board *board)
199 {
200 	int status = VALID_ALL;
201 	int bsr_bits;
202 	struct fluke_priv *e_priv;
203 
204 	e_priv = board->private_data;
205 
206 	bsr_bits = fluke_paged_read_byte(e_priv, BUS_STATUS, BUS_STATUS_PAGE);
207 
208 	if ((bsr_bits & BSR_REN_BIT) == 0)
209 		status |= BUS_REN;
210 	if ((bsr_bits & BSR_IFC_BIT) == 0)
211 		status |= BUS_IFC;
212 	if ((bsr_bits & BSR_SRQ_BIT) == 0)
213 		status |= BUS_SRQ;
214 	if ((bsr_bits & BSR_EOI_BIT) == 0)
215 		status |= BUS_EOI;
216 	if ((bsr_bits & BSR_NRFD_BIT) == 0)
217 		status |= BUS_NRFD;
218 	if ((bsr_bits & BSR_NDAC_BIT) == 0)
219 		status |= BUS_NDAC;
220 	if ((bsr_bits & BSR_DAV_BIT) == 0)
221 		status |= BUS_DAV;
222 	if ((bsr_bits & BSR_ATN_BIT) == 0)
223 		status |= BUS_ATN;
224 
225 	return status;
226 }
227 
fluke_t1_delay(struct gpib_board * board,unsigned int nano_sec)228 static int fluke_t1_delay(struct gpib_board *board, unsigned int nano_sec)
229 {
230 	struct fluke_priv *e_priv = board->private_data;
231 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
232 	unsigned int retval;
233 
234 	retval = nec7210_t1_delay(board, nec_priv, nano_sec);
235 
236 	if (nano_sec <= 350) {
237 		write_byte(nec_priv, AUX_HI_SPEED, AUXMR);
238 		retval = 350;
239 	} else {
240 		write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
241 	}
242 	return retval;
243 }
244 
lacs_or_read_ready(struct gpib_board * board)245 static int lacs_or_read_ready(struct gpib_board *board)
246 {
247 	const struct fluke_priv *e_priv = board->private_data;
248 	const struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
249 	unsigned long flags;
250 	int retval;
251 
252 	spin_lock_irqsave(&board->spinlock, flags);
253 	retval = test_bit(LACS_NUM, &board->status) || test_bit(READ_READY_BN, &nec_priv->state);
254 	spin_unlock_irqrestore(&board->spinlock, flags);
255 	return retval;
256 }
257 
258 /*
259  * Wait until it is possible for a read to do something useful.  This
260  * is not essential, it only exists to prevent RFD holdoff from being released pointlessly.
261  */
wait_for_read(struct gpib_board * board)262 static int wait_for_read(struct gpib_board *board)
263 {
264 	struct fluke_priv *e_priv = board->private_data;
265 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
266 	int retval = 0;
267 
268 	if (wait_event_interruptible(board->wait,
269 				     lacs_or_read_ready(board) ||
270 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
271 				     test_bit(TIMO_NUM, &board->status)))
272 		retval = -ERESTARTSYS;
273 
274 	if (test_bit(TIMO_NUM, &board->status))
275 		retval = -ETIMEDOUT;
276 	if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
277 		retval = -EINTR;
278 	return retval;
279 }
280 
281 /*
282  * Check if the SH state machine is in SGNS.  We check twice since there is a very small chance
283  * we could be blowing through SGNS from SIDS to SDYS if there is already a
284  * byte available in the handshake state machine.  We are interested
285  * in the case where the handshake is stuck in SGNS due to no byte being
286  * available to the chip (and thus we can be confident a dma transfer will
287  * result in at least one byte making it into the chip).  This matters
288  * because we want to be confident before sending a "send eoi" auxilary
289  * command that we will be able to also put the associated data byte
290  * in the chip before any potential timeout.
291  */
source_handshake_is_sgns(struct fluke_priv * e_priv)292 static int source_handshake_is_sgns(struct fluke_priv *e_priv)
293 {
294 	int i;
295 
296 	for (i = 0; i < 2; ++i)	{
297 		if ((fluke_paged_read_byte(e_priv, STATE1_REG, STATE1_PAGE) &
298 		     SOURCE_HANDSHAKE_MASK) != SOURCE_HANDSHAKE_SGNS_BITS) {
299 			return 0;
300 		}
301 	}
302 	return 1;
303 }
304 
source_handshake_is_sids_or_sgns(struct fluke_priv * e_priv)305 static int source_handshake_is_sids_or_sgns(struct fluke_priv *e_priv)
306 {
307 	unsigned int source_handshake_bits;
308 
309 	source_handshake_bits = fluke_paged_read_byte(e_priv, STATE1_REG, STATE1_PAGE) &
310 		SOURCE_HANDSHAKE_MASK;
311 
312 	return (source_handshake_bits == SOURCE_HANDSHAKE_SGNS_BITS) ||
313 		(source_handshake_bits == SOURCE_HANDSHAKE_SIDS_BITS);
314 }
315 
316 /*
317  * Wait until the gpib chip is ready to accept a data out byte.
318  * If the chip is SGNS it is probably waiting for a a byte to
319  * be written to it.
320  */
wait_for_data_out_ready(struct gpib_board * board)321 static int wait_for_data_out_ready(struct gpib_board *board)
322 {
323 	struct fluke_priv *e_priv = board->private_data;
324 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
325 	int retval = 0;
326 
327 	if (wait_event_interruptible(board->wait,
328 				     (test_bit(TACS_NUM, &board->status) &&
329 				      source_handshake_is_sgns(e_priv)) ||
330 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
331 				     test_bit(TIMO_NUM, &board->status)))
332 		retval = -ERESTARTSYS;
333 	if (test_bit(TIMO_NUM, &board->status))
334 		retval = -ETIMEDOUT;
335 	if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
336 		retval = -EINTR;
337 	return retval;
338 }
339 
wait_for_sids_or_sgns(struct gpib_board * board)340 static int wait_for_sids_or_sgns(struct gpib_board *board)
341 {
342 	struct fluke_priv *e_priv = board->private_data;
343 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
344 	int retval = 0;
345 
346 	if (wait_event_interruptible(board->wait,
347 				     source_handshake_is_sids_or_sgns(e_priv) ||
348 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
349 				     test_bit(TIMO_NUM, &board->status)))
350 		retval = -ERESTARTSYS;
351 
352 	if (test_bit(TIMO_NUM, &board->status))
353 		retval = -ETIMEDOUT;
354 	if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
355 		retval = -EINTR;
356 	return retval;
357 }
358 
fluke_dma_callback(void * arg)359 static void fluke_dma_callback(void *arg)
360 {
361 	struct gpib_board *board = arg;
362 	struct fluke_priv *e_priv = board->private_data;
363 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
364 	unsigned long flags;
365 
366 	spin_lock_irqsave(&board->spinlock, flags);
367 
368 	nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE | HR_DIIE, HR_DOIE | HR_DIIE);
369 	wake_up_interruptible(&board->wait);
370 
371 	fluke_gpib_internal_interrupt(board);
372 	clear_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state);
373 	clear_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state);
374 
375 	spin_unlock_irqrestore(&board->spinlock, flags);
376 }
377 
fluke_dma_write(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)378 static int fluke_dma_write(struct gpib_board *board, u8 *buffer, size_t length,
379 			   size_t *bytes_written)
380 {
381 	struct fluke_priv *e_priv = board->private_data;
382 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
383 	unsigned long flags;
384 	int retval = 0;
385 	dma_addr_t address;
386 	struct dma_async_tx_descriptor *tx_desc;
387 
388 	*bytes_written = 0;
389 
390 	if (WARN_ON_ONCE(length > e_priv->dma_buffer_size))
391 		return -EFAULT;
392 	dmaengine_terminate_all(e_priv->dma_channel);
393 	// write-clear counter
394 	writel(0x0, e_priv->write_transfer_counter);
395 
396 	memcpy(e_priv->dma_buffer, buffer, length);
397 	address = dma_map_single(board->dev, e_priv->dma_buffer,
398 				 length, DMA_TO_DEVICE);
399 	/* program dma controller */
400 	retval = fluke_config_dma(board, 1);
401 	if (retval)
402 		goto cleanup;
403 
404 	tx_desc = dmaengine_prep_slave_single(e_priv->dma_channel, address, length, DMA_MEM_TO_DEV,
405 					      DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
406 	if (!tx_desc) {
407 		dev_err(board->gpib_dev, "failed to allocate dma transmit descriptor\n");
408 		retval = -ENOMEM;
409 		goto cleanup;
410 	}
411 	tx_desc->callback = fluke_dma_callback;
412 	tx_desc->callback_param = board;
413 
414 	spin_lock_irqsave(&board->spinlock, flags);
415 	nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE, 0);
416 	nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, HR_DMAO);
417 	dmaengine_submit(tx_desc);
418 	dma_async_issue_pending(e_priv->dma_channel);
419 
420 	clear_bit(WRITE_READY_BN, &nec_priv->state);
421 	set_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state);
422 
423 	spin_unlock_irqrestore(&board->spinlock, flags);
424 
425 	// suspend until message is sent
426 	if (wait_event_interruptible(board->wait,
427 				     ((readl(e_priv->write_transfer_counter) &
428 				       write_transfer_counter_mask) == length) ||
429 				     test_bit(BUS_ERROR_BN, &nec_priv->state) ||
430 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
431 				     test_bit(TIMO_NUM, &board->status))) {
432 		retval = -ERESTARTSYS;
433 	}
434 	if (test_bit(TIMO_NUM, &board->status))
435 		retval = -ETIMEDOUT;
436 	if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
437 		retval = -EINTR;
438 	if (test_and_clear_bit(BUS_ERROR_BN, &nec_priv->state))
439 		retval = -EIO;
440 	// disable board's dma
441 	nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, 0);
442 
443 	dmaengine_terminate_all(e_priv->dma_channel);
444 	// make sure fluke_dma_callback got called
445 	if (test_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state))
446 		fluke_dma_callback(board);
447 
448 	/*
449 	 * if everything went fine, try to wait until last byte is actually
450 	 * transmitted across gpib (but don't try _too_ hard)
451 	 */
452 	if (retval == 0)
453 		retval = wait_for_sids_or_sgns(board);
454 
455 	*bytes_written = readl(e_priv->write_transfer_counter) & write_transfer_counter_mask;
456 	if (WARN_ON_ONCE(*bytes_written > length))
457 		return -EFAULT;
458 
459 cleanup:
460 	dma_unmap_single(board->dev, address, length, DMA_TO_DEVICE);
461 	return retval;
462 }
463 
fluke_accel_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)464 static int fluke_accel_write(struct gpib_board *board, u8 *buffer, size_t length,
465 			     int send_eoi, size_t *bytes_written)
466 {
467 	struct fluke_priv *e_priv = board->private_data;
468 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
469 	size_t remainder = length;
470 	size_t transfer_size;
471 	ssize_t retval = 0;
472 	size_t dma_remainder = remainder;
473 
474 	if (!e_priv->dma_channel) {
475 		dev_err(board->gpib_dev, "No dma channel available, cannot do accel write.");
476 		return -ENXIO;
477 	}
478 
479 	*bytes_written = 0;
480 	if (length < 1)
481 		return 0;
482 
483 	clear_bit(DEV_CLEAR_BN, &nec_priv->state); // XXX FIXME
484 
485 	if (send_eoi)
486 		--dma_remainder;
487 
488 	while (dma_remainder > 0) {
489 		size_t num_bytes;
490 
491 		retval = wait_for_data_out_ready(board);
492 		if (retval < 0)
493 			break;
494 
495 		transfer_size = (e_priv->dma_buffer_size < dma_remainder) ?
496 			e_priv->dma_buffer_size : dma_remainder;
497 		retval = fluke_dma_write(board, buffer, transfer_size, &num_bytes);
498 		*bytes_written += num_bytes;
499 		if (retval < 0)
500 			break;
501 		dma_remainder -= num_bytes;
502 		remainder -= num_bytes;
503 		buffer += num_bytes;
504 		if (need_resched())
505 			schedule();
506 	}
507 	if (retval < 0)
508 		return retval;
509 	// handle sending of last byte with eoi
510 	if (send_eoi) {
511 		size_t num_bytes;
512 
513 		if (WARN_ON_ONCE(remainder != 1))
514 			return -EFAULT;
515 
516 		/*
517 		 * wait until we are sure we will be able to write the data byte
518 		 * into the chip before we send AUX_SEOI.  This prevents a timeout
519 		 * scenerio where we send AUX_SEOI but then timeout without getting
520 		 * any bytes into the gpib chip.  This will result in the first byte
521 		 * of the next write having a spurious EOI set on the first byte.
522 		 */
523 		retval = wait_for_data_out_ready(board);
524 		if (retval < 0)
525 			return retval;
526 
527 		write_byte(nec_priv, AUX_SEOI, AUXMR);
528 		retval = fluke_dma_write(board, buffer, remainder, &num_bytes);
529 		*bytes_written += num_bytes;
530 		if (retval < 0)
531 			return retval;
532 		remainder -= num_bytes;
533 	}
534 	return 0;
535 }
536 
fluke_get_dma_residue(struct dma_chan * chan,dma_cookie_t cookie)537 static int fluke_get_dma_residue(struct dma_chan *chan, dma_cookie_t cookie)
538 {
539 	struct dma_tx_state state;
540 	int result;
541 
542 	result = dmaengine_pause(chan);
543 	if (result < 0) {
544 		pr_err("dma pause failed?\n");
545 		return result;
546 	}
547 	dmaengine_tx_status(chan, cookie, &state);
548 	/*
549 	 * hardware doesn't support resume, so dont call this
550 	 * method unless the dma transfer is done.
551 	 */
552 	return state.residue;
553 }
554 
fluke_dma_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)555 static int fluke_dma_read(struct gpib_board *board, u8 *buffer,
556 			  size_t length, int *end, size_t *bytes_read)
557 {
558 	struct fluke_priv *e_priv = board->private_data;
559 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
560 	int retval = 0;
561 	unsigned long flags;
562 	int residue;
563 	dma_addr_t bus_address;
564 	struct dma_async_tx_descriptor *tx_desc;
565 	dma_cookie_t dma_cookie;
566 	int i;
567 	static const int timeout = 10;
568 
569 	*bytes_read = 0;
570 	*end = 0;
571 	if (length == 0)
572 		return 0;
573 
574 	bus_address = dma_map_single(board->dev, e_priv->dma_buffer,
575 				     length, DMA_FROM_DEVICE);
576 
577 	/* program dma controller */
578 	retval = fluke_config_dma(board, 0);
579 	if (retval) {
580 		dma_unmap_single(board->dev, bus_address, length, DMA_FROM_DEVICE);
581 		return retval;
582 	}
583 	tx_desc = dmaengine_prep_slave_single(e_priv->dma_channel,
584 					      bus_address, length, DMA_DEV_TO_MEM,
585 					      DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
586 	if (!tx_desc) {
587 		dev_err(board->gpib_dev, "failed to allocate dma transmit descriptor\n");
588 		dma_unmap_single(NULL, bus_address, length, DMA_FROM_DEVICE);
589 		return -EIO;
590 	}
591 	tx_desc->callback = fluke_dma_callback;
592 	tx_desc->callback_param = board;
593 
594 	spin_lock_irqsave(&board->spinlock, flags);
595 	// enable nec7210 dma
596 	nec7210_set_reg_bits(nec_priv, IMR1, HR_DIIE, 0);
597 	nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, HR_DMAI);
598 
599 	dma_cookie = dmaengine_submit(tx_desc);
600 	dma_async_issue_pending(e_priv->dma_channel);
601 
602 	set_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state);
603 	clear_bit(READ_READY_BN, &nec_priv->state);
604 
605 	spin_unlock_irqrestore(&board->spinlock, flags);
606 	// wait for data to transfer
607 	if (wait_event_interruptible(board->wait,
608 				     test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state) == 0 ||
609 				     test_bit(RECEIVED_END_BN, &nec_priv->state) ||
610 				     test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
611 				     test_bit(TIMO_NUM, &board->status))) {
612 		retval = -ERESTARTSYS;
613 	}
614 	if (test_bit(TIMO_NUM, &board->status))
615 		retval = -ETIMEDOUT;
616 	if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
617 		retval = -EINTR;
618 
619 	/*
620 	 * If we woke up because of end, wait until the dma transfer has pulled
621 	 * the data byte associated with the end before we cancel the dma transfer.
622 	 */
623 	if (test_bit(RECEIVED_END_BN, &nec_priv->state)) {
624 		for (i = 0; i < timeout; ++i) {
625 			if (test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state) == 0)
626 				break;
627 			if ((read_byte(nec_priv, ADR0) & DATA_IN_STATUS) == 0)
628 				break;
629 			usleep_range(10, 15);
630 		}
631 		if (i == timeout)
632 			pr_warn("fluke_gpib: timeout waiting for dma to transfer end data byte.\n");
633 	}
634 
635 	// stop the dma transfer
636 	nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
637 	/*
638 	 * delay a little just to make sure any bytes in dma controller's fifo get
639 	 * written to memory before we disable it
640 	 */
641 	usleep_range(10, 15);
642 	residue = fluke_get_dma_residue(e_priv->dma_channel, dma_cookie);
643 	if (WARN_ON_ONCE(residue > length || residue < 0))
644 		return -EFAULT;
645 	*bytes_read += length - residue;
646 	dmaengine_terminate_all(e_priv->dma_channel);
647 	// make sure fluke_dma_callback got called
648 	if (test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state))
649 		fluke_dma_callback(board);
650 
651 	dma_unmap_single(board->dev, bus_address, length, DMA_FROM_DEVICE);
652 	memcpy(buffer, e_priv->dma_buffer, *bytes_read);
653 
654 	/*
655 	 * If we got an end interrupt, figure out if it was
656 	 * associated with the last byte we dma'd or with a
657 	 * byte still sitting on the cb7210.
658 	 */
659 	spin_lock_irqsave(&board->spinlock, flags);
660 	if (test_bit(READ_READY_BN, &nec_priv->state) == 0) {
661 		/*
662 		 * There is no byte sitting on the cb7210.  If we
663 		 * saw an end interrupt, we need to deal with it now
664 		 */
665 		if (test_and_clear_bit(RECEIVED_END_BN, &nec_priv->state))
666 			*end = 1;
667 	}
668 	spin_unlock_irqrestore(&board->spinlock, flags);
669 
670 	return retval;
671 }
672 
fluke_accel_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)673 static int fluke_accel_read(struct gpib_board *board, u8 *buffer, size_t length,
674 			    int *end, size_t *bytes_read)
675 {
676 	struct fluke_priv *e_priv = board->private_data;
677 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
678 	size_t remain = length;
679 	size_t transfer_size;
680 	int retval = 0;
681 	size_t dma_nbytes;
682 
683 	*end = 0;
684 	*bytes_read = 0;
685 
686 	smp_mb__before_atomic();
687 	clear_bit(DEV_CLEAR_BN, &nec_priv->state); // XXX FIXME
688 	smp_mb__after_atomic();
689 
690 	retval = wait_for_read(board);
691 	if (retval < 0)
692 		return retval;
693 
694 	nec7210_release_rfd_holdoff(board, nec_priv);
695 
696 	while (remain > 0) {
697 		transfer_size = (e_priv->dma_buffer_size < remain) ?
698 			e_priv->dma_buffer_size : remain;
699 		retval = fluke_dma_read(board, buffer, transfer_size, end, &dma_nbytes);
700 		remain -= dma_nbytes;
701 		buffer += dma_nbytes;
702 		*bytes_read += dma_nbytes;
703 		if (*end)
704 			break;
705 		if (retval < 0)
706 			return retval;
707 		if (need_resched())
708 			schedule();
709 	}
710 
711 	return retval;
712 }
713 
714 static struct gpib_interface fluke_unaccel_interface = {
715 	.name = "fluke_unaccel",
716 	.attach = fluke_attach_holdoff_all,
717 	.detach = fluke_detach,
718 	.read = fluke_read,
719 	.write = fluke_write,
720 	.command = fluke_command,
721 	.take_control = fluke_take_control,
722 	.go_to_standby = fluke_go_to_standby,
723 	.request_system_control = fluke_request_system_control,
724 	.interface_clear = fluke_interface_clear,
725 	.remote_enable = fluke_remote_enable,
726 	.enable_eos = fluke_enable_eos,
727 	.disable_eos = fluke_disable_eos,
728 	.parallel_poll = fluke_parallel_poll,
729 	.parallel_poll_configure = fluke_parallel_poll_configure,
730 	.parallel_poll_response = fluke_parallel_poll_response,
731 	.line_status = fluke_line_status,
732 	.update_status = fluke_update_status,
733 	.primary_address = fluke_primary_address,
734 	.secondary_address = fluke_secondary_address,
735 	.serial_poll_response = fluke_serial_poll_response,
736 	.serial_poll_status = fluke_serial_poll_status,
737 	.t1_delay = fluke_t1_delay,
738 	.return_to_local = fluke_return_to_local,
739 };
740 
741 /*
742  * fluke_hybrid uses dma for writes but not for reads.  Added
743  * to deal with occasional corruption of bytes seen when doing dma
744  * reads.  From looking at the cb7210 vhdl, I believe the corruption
745  * is due to a hardware bug triggered by the cpu reading a cb7210
746  *		}
747  * register just as the dma controller is also doing a read.
748  */
749 
750 static struct gpib_interface fluke_hybrid_interface = {
751 	.name = "fluke_hybrid",
752 	.attach = fluke_attach_holdoff_all,
753 	.detach = fluke_detach,
754 	.read = fluke_read,
755 	.write = fluke_accel_write,
756 	.command = fluke_command,
757 	.take_control = fluke_take_control,
758 	.go_to_standby = fluke_go_to_standby,
759 	.request_system_control = fluke_request_system_control,
760 	.interface_clear = fluke_interface_clear,
761 	.remote_enable = fluke_remote_enable,
762 	.enable_eos = fluke_enable_eos,
763 	.disable_eos = fluke_disable_eos,
764 	.parallel_poll = fluke_parallel_poll,
765 	.parallel_poll_configure = fluke_parallel_poll_configure,
766 	.parallel_poll_response = fluke_parallel_poll_response,
767 	.line_status = fluke_line_status,
768 	.update_status = fluke_update_status,
769 	.primary_address = fluke_primary_address,
770 	.secondary_address = fluke_secondary_address,
771 	.serial_poll_response = fluke_serial_poll_response,
772 	.serial_poll_status = fluke_serial_poll_status,
773 	.t1_delay = fluke_t1_delay,
774 	.return_to_local = fluke_return_to_local,
775 };
776 
777 static struct gpib_interface fluke_interface = {
778 	.name = "fluke",
779 	.attach = fluke_attach_holdoff_end,
780 	.detach = fluke_detach,
781 	.read = fluke_accel_read,
782 	.write = fluke_accel_write,
783 	.command = fluke_command,
784 	.take_control = fluke_take_control,
785 	.go_to_standby = fluke_go_to_standby,
786 	.request_system_control = fluke_request_system_control,
787 	.interface_clear = fluke_interface_clear,
788 	.remote_enable = fluke_remote_enable,
789 	.enable_eos = fluke_enable_eos,
790 	.disable_eos = fluke_disable_eos,
791 	.parallel_poll = fluke_parallel_poll,
792 	.parallel_poll_configure = fluke_parallel_poll_configure,
793 	.parallel_poll_response = fluke_parallel_poll_response,
794 	.line_status = fluke_line_status,
795 	.update_status = fluke_update_status,
796 	.primary_address = fluke_primary_address,
797 	.secondary_address = fluke_secondary_address,
798 	.serial_poll_response = fluke_serial_poll_response,
799 	.serial_poll_status = fluke_serial_poll_status,
800 	.t1_delay = fluke_t1_delay,
801 	.return_to_local = fluke_return_to_local,
802 };
803 
fluke_gpib_internal_interrupt(struct gpib_board * board)804 irqreturn_t fluke_gpib_internal_interrupt(struct gpib_board *board)
805 {
806 	int status0, status1, status2;
807 	struct fluke_priv *priv = board->private_data;
808 	struct nec7210_priv *nec_priv = &priv->nec7210_priv;
809 	int retval = IRQ_NONE;
810 
811 	if (read_byte(nec_priv, ADR0) & DATA_IN_STATUS)
812 		set_bit(READ_READY_BN, &nec_priv->state);
813 
814 	status0 = fluke_paged_read_byte(priv, ISR0_IMR0, ISR0_IMR0_PAGE);
815 	status1 = read_byte(nec_priv, ISR1);
816 	status2 = read_byte(nec_priv, ISR2);
817 
818 	if (status0 & FLUKE_IFCI_BIT) {
819 		push_gpib_event(board, EVENT_IFC);
820 		retval = IRQ_HANDLED;
821 	}
822 
823 	if (nec7210_interrupt_have_status(board, nec_priv, status1, status2) == IRQ_HANDLED)
824 		retval = IRQ_HANDLED;
825 
826 	if (read_byte(nec_priv, ADR0) & DATA_IN_STATUS)	{
827 		if (test_bit(RFD_HOLDOFF_BN, &nec_priv->state))
828 			set_bit(READ_READY_BN, &nec_priv->state);
829 		else
830 			clear_bit(READ_READY_BN, &nec_priv->state);
831 	}
832 
833 	if (retval == IRQ_HANDLED)
834 		wake_up_interruptible(&board->wait);
835 
836 	return retval;
837 }
838 
fluke_gpib_interrupt(int irq,void * arg)839 static irqreturn_t fluke_gpib_interrupt(int irq, void *arg)
840 {
841 	struct gpib_board *board = arg;
842 	unsigned long flags;
843 	irqreturn_t retval;
844 
845 	spin_lock_irqsave(&board->spinlock, flags);
846 	retval = fluke_gpib_internal_interrupt(board);
847 	spin_unlock_irqrestore(&board->spinlock, flags);
848 	return retval;
849 }
850 
fluke_allocate_private(struct gpib_board * board)851 static int fluke_allocate_private(struct gpib_board *board)
852 {
853 	struct fluke_priv *priv;
854 
855 	board->private_data = kzalloc_obj(struct fluke_priv);
856 	if (!board->private_data)
857 		return -ENOMEM;
858 	priv = board->private_data;
859 	init_nec7210_private(&priv->nec7210_priv);
860 	priv->dma_buffer_size = 0x7ff;
861 	priv->dma_buffer = kmalloc(priv->dma_buffer_size, GFP_KERNEL);
862 	if (!priv->dma_buffer)
863 		return -ENOMEM;
864 	return 0;
865 }
866 
fluke_generic_detach(struct gpib_board * board)867 static void fluke_generic_detach(struct gpib_board *board)
868 {
869 	if (board->private_data) {
870 		struct fluke_priv *e_priv = board->private_data;
871 
872 		kfree(e_priv->dma_buffer);
873 		kfree(board->private_data);
874 		board->private_data = NULL;
875 	}
876 }
877 
878 // generic part of attach functions shared by all cb7210 boards
fluke_generic_attach(struct gpib_board * board)879 static int fluke_generic_attach(struct gpib_board *board)
880 {
881 	struct fluke_priv *e_priv;
882 	struct nec7210_priv *nec_priv;
883 	int retval;
884 
885 	board->status = 0;
886 
887 	retval = fluke_allocate_private(board);
888 	if (retval)
889 		return retval;
890 	e_priv = board->private_data;
891 	nec_priv = &e_priv->nec7210_priv;
892 	nec_priv->read_byte = fluke_locking_read_byte;
893 	nec_priv->write_byte = fluke_locking_write_byte;
894 	nec_priv->offset = fluke_reg_offset;
895 	nec_priv->type = CB7210;
896 	return 0;
897 }
898 
fluke_config_dma(struct gpib_board * board,int output)899 static int fluke_config_dma(struct gpib_board *board, int output)
900 {
901 	struct fluke_priv *e_priv = board->private_data;
902 	struct dma_slave_config config;
903 
904 	config.src_maxburst = 1;
905 	config.dst_maxburst = 1;
906 	config.device_fc = true;
907 
908 	if (output) {
909 		config.direction = DMA_MEM_TO_DEV;
910 		config.src_addr = 0;
911 		config.dst_addr = e_priv->dma_port_res->start;
912 		config.src_addr_width = 1;
913 		config.dst_addr_width = 1;
914 	} else {
915 		config.direction = DMA_DEV_TO_MEM;
916 		config.src_addr = e_priv->dma_port_res->start;
917 		config.dst_addr = 0;
918 		config.src_addr_width = 1;
919 		config.dst_addr_width = 1;
920 	}
921 	return dmaengine_slave_config(e_priv->dma_channel, &config);
922 }
923 
fluke_init(struct fluke_priv * e_priv,struct gpib_board * board,int handshake_mode)924 static int fluke_init(struct fluke_priv *e_priv, struct gpib_board *board, int handshake_mode)
925 {
926 	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
927 
928 	nec7210_board_reset(nec_priv, board);
929 	write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
930 	/*
931 	 * set clock register for driving frequency
932 	 * ICR should be set to clock in megahertz (1-15) and to zero
933 	 * for clocks faster than 15 MHz (max 20MHz)
934 	 */
935 	write_byte(nec_priv, ICR | 10, AUXMR);
936 	nec7210_set_handshake_mode(board, nec_priv, handshake_mode);
937 
938 	nec7210_board_online(nec_priv, board);
939 
940 	/* poll so we can detect ATN changes */
941 	if (gpib_request_pseudo_irq(board, fluke_gpib_interrupt)) {
942 		dev_err(board->gpib_dev, "failed to allocate pseudo_irq\n");
943 		return -EINVAL;
944 	}
945 
946 	fluke_paged_write_byte(e_priv, FLUKE_IFCIE_BIT, ISR0_IMR0, ISR0_IMR0_PAGE);
947 	return 0;
948 }
949 
950 /*
951  * This function is passed to dma_request_channel() in order to
952  * select the pl330 dma channel which has been hardwired to
953  * the gpib controller.
954  */
gpib_dma_channel_filter(struct dma_chan * chan,void * filter_param)955 static bool gpib_dma_channel_filter(struct dma_chan *chan, void *filter_param)
956 {
957 	// select the channel which is wired to the gpib chip
958 	return chan->chan_id == 0;
959 }
960 
fluke_attach_impl(struct gpib_board * board,const struct gpib_board_config * config,unsigned int handshake_mode)961 static int fluke_attach_impl(struct gpib_board *board, const struct gpib_board_config *config,
962 			     unsigned int handshake_mode)
963 {
964 	struct fluke_priv *e_priv;
965 	struct nec7210_priv *nec_priv;
966 	int isr_flags = 0;
967 	int retval;
968 	int irq;
969 	struct resource *res;
970 	dma_cap_mask_t dma_cap;
971 
972 	if (!fluke_gpib_pdev) {
973 		dev_err(board->gpib_dev, "No fluke device was found, attach failed.\n");
974 		return -ENODEV;
975 	}
976 
977 	retval = fluke_generic_attach(board);
978 	if (retval)
979 		return retval;
980 
981 	e_priv = board->private_data;
982 	nec_priv = &e_priv->nec7210_priv;
983 	nec_priv->offset = fluke_reg_offset;
984 	board->dev = &fluke_gpib_pdev->dev;
985 
986 	res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 0);
987 	if (!res) {
988 		dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource\n");
989 		return -ENODEV;
990 	}
991 
992 	if (request_mem_region(res->start,
993 			       resource_size(res),
994 			       fluke_gpib_pdev->name) == NULL) {
995 		dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
996 		return -ENXIO;
997 	}
998 	e_priv->gpib_iomem_res = res;
999 
1000 	nec_priv->mmiobase = ioremap(e_priv->gpib_iomem_res->start,
1001 				     resource_size(e_priv->gpib_iomem_res));
1002 	if (!nec_priv->mmiobase) {
1003 		dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
1004 		return -ENOMEM;
1005 	}
1006 
1007 	res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 1);
1008 	if (!res) {
1009 		dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for gpib dma port\n");
1010 		return -ENODEV;
1011 	}
1012 	if (request_mem_region(res->start,
1013 			       resource_size(res),
1014 			       fluke_gpib_pdev->name) == NULL) {
1015 		dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
1016 		return -ENXIO;
1017 	}
1018 	e_priv->dma_port_res = res;
1019 
1020 	res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 2);
1021 	if (!res) {
1022 		dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for write transfer counter\n");
1023 		return -ENODEV;
1024 	}
1025 
1026 	if (request_mem_region(res->start,
1027 			       resource_size(res),
1028 			       fluke_gpib_pdev->name) == NULL) {
1029 		dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
1030 		return -ENXIO;
1031 	}
1032 	e_priv->write_transfer_counter_res = res;
1033 
1034 	e_priv->write_transfer_counter = ioremap(e_priv->write_transfer_counter_res->start,
1035 						 resource_size(e_priv->write_transfer_counter_res));
1036 	if (!e_priv->write_transfer_counter) {
1037 		dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
1038 		return -ENOMEM;
1039 	}
1040 
1041 	irq = platform_get_irq(fluke_gpib_pdev, 0);
1042 	if (irq < 0)
1043 		return -EBUSY;
1044 	retval = request_irq(irq, fluke_gpib_interrupt, isr_flags, fluke_gpib_pdev->name, board);
1045 	if (retval) {
1046 		dev_err(&fluke_gpib_pdev->dev,
1047 			"cannot register interrupt handler err=%d\n",
1048 			retval);
1049 		return retval;
1050 	}
1051 	e_priv->irq = irq;
1052 
1053 	dma_cap_zero(dma_cap);
1054 	dma_cap_set(DMA_SLAVE, dma_cap);
1055 	e_priv->dma_channel = dma_request_channel(dma_cap, gpib_dma_channel_filter, NULL);
1056 	if (!e_priv->dma_channel) {
1057 		dev_err(board->gpib_dev, "failed to allocate a dma channel.\n");
1058 		/*
1059 		 * we don't error out here because unaccel interface will still
1060 		 * work without dma
1061 		 */
1062 	}
1063 
1064 	return fluke_init(e_priv, board, handshake_mode);
1065 }
1066 
fluke_attach_holdoff_all(struct gpib_board * board,const struct gpib_board_config * config)1067 int fluke_attach_holdoff_all(struct gpib_board *board, const struct gpib_board_config *config)
1068 {
1069 	return fluke_attach_impl(board, config, HR_HLDA);
1070 }
1071 
fluke_attach_holdoff_end(struct gpib_board * board,const struct gpib_board_config * config)1072 int fluke_attach_holdoff_end(struct gpib_board *board, const struct gpib_board_config *config)
1073 {
1074 	return fluke_attach_impl(board, config, HR_HLDE);
1075 }
1076 
fluke_detach(struct gpib_board * board)1077 void fluke_detach(struct gpib_board *board)
1078 {
1079 	struct fluke_priv *e_priv = board->private_data;
1080 	struct nec7210_priv *nec_priv;
1081 
1082 	if (e_priv) {
1083 		if (e_priv->dma_channel)
1084 			dma_release_channel(e_priv->dma_channel);
1085 		gpib_free_pseudo_irq(board);
1086 		nec_priv = &e_priv->nec7210_priv;
1087 
1088 		if (nec_priv->mmiobase) {
1089 			fluke_paged_write_byte(e_priv, 0, ISR0_IMR0, ISR0_IMR0_PAGE);
1090 			nec7210_board_reset(nec_priv, board);
1091 		}
1092 		if (e_priv->irq)
1093 			free_irq(e_priv->irq, board);
1094 		if (e_priv->write_transfer_counter_res) {
1095 			release_mem_region(e_priv->write_transfer_counter_res->start,
1096 					   resource_size(e_priv->write_transfer_counter_res));
1097 		}
1098 		if (e_priv->dma_port_res) {
1099 			release_mem_region(e_priv->dma_port_res->start,
1100 					   resource_size(e_priv->dma_port_res));
1101 		}
1102 		if (e_priv->gpib_iomem_res)
1103 			release_mem_region(e_priv->gpib_iomem_res->start,
1104 					   resource_size(e_priv->gpib_iomem_res));
1105 	}
1106 	fluke_generic_detach(board);
1107 }
1108 
fluke_gpib_probe(struct platform_device * pdev)1109 static int fluke_gpib_probe(struct platform_device *pdev)
1110 {
1111 	fluke_gpib_pdev = pdev;
1112 	return 0;
1113 }
1114 
1115 static const struct of_device_id fluke_gpib_of_match[] = {
1116 	{ .compatible = "flk,fgpib-4.0"},
1117 	{ {0} }
1118 };
1119 MODULE_DEVICE_TABLE(of, fluke_gpib_of_match);
1120 
1121 static struct platform_driver fluke_gpib_platform_driver = {
1122 	.driver = {
1123 		.name = DRV_NAME,
1124 		.of_match_table = fluke_gpib_of_match,
1125 	},
1126 	.probe = &fluke_gpib_probe
1127 };
1128 
fluke_init_module(void)1129 static int __init fluke_init_module(void)
1130 {
1131 	int result;
1132 
1133 	result = platform_driver_register(&fluke_gpib_platform_driver);
1134 	if (result) {
1135 		pr_err("platform_driver_register failed: error = %d\n", result);
1136 		return result;
1137 	}
1138 
1139 	result = gpib_register_driver(&fluke_unaccel_interface, THIS_MODULE);
1140 	if (result) {
1141 		pr_err("gpib_register_driver failed: error = %d\n", result);
1142 		goto err_unaccel;
1143 	}
1144 
1145 	result = gpib_register_driver(&fluke_hybrid_interface, THIS_MODULE);
1146 	if (result) {
1147 		pr_err("gpib_register_driver failed: error = %d\n", result);
1148 		goto err_hybrid;
1149 	}
1150 
1151 	result = gpib_register_driver(&fluke_interface, THIS_MODULE);
1152 	if (result) {
1153 		pr_err("gpib_register_driver failed: error = %d\n", result);
1154 		goto err_interface;
1155 	}
1156 
1157 	return 0;
1158 
1159 err_interface:
1160 	gpib_unregister_driver(&fluke_hybrid_interface);
1161 err_hybrid:
1162 	gpib_unregister_driver(&fluke_unaccel_interface);
1163 err_unaccel:
1164 	platform_driver_unregister(&fluke_gpib_platform_driver);
1165 
1166 	return result;
1167 }
1168 
fluke_exit_module(void)1169 static void __exit fluke_exit_module(void)
1170 {
1171 	gpib_unregister_driver(&fluke_unaccel_interface);
1172 	gpib_unregister_driver(&fluke_hybrid_interface);
1173 	gpib_unregister_driver(&fluke_interface);
1174 	platform_driver_unregister(&fluke_gpib_platform_driver);
1175 }
1176 
1177 module_init(fluke_init_module);
1178 module_exit(fluke_exit_module);
1179