xref: /linux/drivers/gpib/cec/cec_gpib.c (revision b47077a4c03e50b14a03633773bf549cb2f0a38f)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  *   copyright            : (C) 2002 by Frank Mori Hess
5  ***************************************************************************/
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #define dev_fmt pr_fmt
9 #define DRV_NAME KBUILD_MODNAME
10 
11 #include "cec.h"
12 #include <linux/pci.h>
13 #include <linux/io.h>
14 #include <linux/bitops.h>
15 #include <asm/dma.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 
19 MODULE_LICENSE("GPL");
20 MODULE_DESCRIPTION("GPIB driver for CEC PCI and PCMCIA boards");
21 
22 /*
23  * GPIB interrupt service routines
24  */
25 
26 static irqreturn_t cec_interrupt(int irq, void *arg)
27 {
28 	struct gpib_board *board = arg;
29 	struct cec_priv *priv = board->private_data;
30 	unsigned long flags;
31 	irqreturn_t retval;
32 
33 	spin_lock_irqsave(&board->spinlock, flags);
34 	retval = nec7210_interrupt(board, &priv->nec7210_priv);
35 	spin_unlock_irqrestore(&board->spinlock, flags);
36 	return retval;
37 }
38 
39 #define CEC_VENDOR_ID 0x12fc
40 #define CEC_DEV_ID    0x5cec
41 #define CEC_SUBID 0x9050
42 
43 static int cec_pci_attach(struct gpib_board *board, const struct gpib_board_config *config);
44 
45 static void cec_pci_detach(struct gpib_board *board);
46 
47 // wrappers for interface functions
48 static int cec_read(struct gpib_board *board, u8 *buffer, size_t length, int *end,
49 		    size_t *bytes_read)
50 {
51 	struct cec_priv *priv = board->private_data;
52 
53 	return nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
54 }
55 
56 static int cec_write(struct gpib_board *board, u8 *buffer, size_t length, int send_eoi,
57 		     size_t *bytes_written)
58 {
59 	struct cec_priv *priv = board->private_data;
60 
61 	return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
62 }
63 
64 static int cec_command(struct gpib_board *board, u8 *buffer,
65 		       size_t length, size_t *bytes_written)
66 {
67 	struct cec_priv *priv = board->private_data;
68 
69 	return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
70 }
71 
72 static int cec_take_control(struct gpib_board *board, int synchronous)
73 {
74 	struct cec_priv *priv = board->private_data;
75 
76 	return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
77 }
78 
79 static int cec_go_to_standby(struct gpib_board *board)
80 {
81 	struct cec_priv *priv = board->private_data;
82 
83 	return nec7210_go_to_standby(board, &priv->nec7210_priv);
84 }
85 
86 static int cec_request_system_control(struct gpib_board *board, int request_control)
87 {
88 	struct cec_priv *priv = board->private_data;
89 
90 	return nec7210_request_system_control(board, &priv->nec7210_priv, request_control);
91 }
92 
93 static void cec_interface_clear(struct gpib_board *board, int assert)
94 {
95 	struct cec_priv *priv = board->private_data;
96 
97 	nec7210_interface_clear(board, &priv->nec7210_priv, assert);
98 }
99 
100 static void cec_remote_enable(struct gpib_board *board, int enable)
101 {
102 	struct cec_priv *priv = board->private_data;
103 
104 	nec7210_remote_enable(board, &priv->nec7210_priv, enable);
105 }
106 
107 static int cec_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits)
108 {
109 	struct cec_priv *priv = board->private_data;
110 
111 	return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
112 }
113 
114 static void cec_disable_eos(struct gpib_board *board)
115 {
116 	struct cec_priv *priv = board->private_data;
117 
118 	nec7210_disable_eos(board, &priv->nec7210_priv);
119 }
120 
121 static unsigned int cec_update_status(struct gpib_board *board, unsigned int clear_mask)
122 {
123 	struct cec_priv *priv = board->private_data;
124 
125 	return nec7210_update_status(board, &priv->nec7210_priv, clear_mask);
126 }
127 
128 static int cec_primary_address(struct gpib_board *board, unsigned int address)
129 {
130 	struct cec_priv *priv = board->private_data;
131 
132 	return nec7210_primary_address(board, &priv->nec7210_priv, address);
133 }
134 
135 static int cec_secondary_address(struct gpib_board *board, unsigned int address, int enable)
136 {
137 	struct cec_priv *priv = board->private_data;
138 
139 	return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
140 }
141 
142 static int cec_parallel_poll(struct gpib_board *board, u8 *result)
143 {
144 	struct cec_priv *priv = board->private_data;
145 
146 	return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
147 }
148 
149 static void cec_parallel_poll_configure(struct gpib_board *board, u8 config)
150 {
151 	struct cec_priv *priv = board->private_data;
152 
153 	nec7210_parallel_poll_configure(board, &priv->nec7210_priv, config);
154 }
155 
156 static void cec_parallel_poll_response(struct gpib_board *board, int ist)
157 {
158 	struct cec_priv *priv = board->private_data;
159 
160 	nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
161 }
162 
163 static void cec_serial_poll_response(struct gpib_board *board, u8 status)
164 {
165 	struct cec_priv *priv = board->private_data;
166 
167 	nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
168 }
169 
170 static u8 cec_serial_poll_status(struct gpib_board *board)
171 {
172 	struct cec_priv *priv = board->private_data;
173 
174 	return nec7210_serial_poll_status(board, &priv->nec7210_priv);
175 }
176 
177 static int cec_t1_delay(struct gpib_board *board, unsigned int nano_sec)
178 {
179 	struct cec_priv *priv = board->private_data;
180 
181 	return nec7210_t1_delay(board, &priv->nec7210_priv, nano_sec);
182 }
183 
184 static void cec_return_to_local(struct gpib_board *board)
185 {
186 	struct cec_priv *priv = board->private_data;
187 
188 	nec7210_return_to_local(board, &priv->nec7210_priv);
189 }
190 
191 static struct gpib_interface cec_pci_interface = {
192 	.name = "cec_pci",
193 	.attach = cec_pci_attach,
194 	.detach = cec_pci_detach,
195 	.read = cec_read,
196 	.write = cec_write,
197 	.command = cec_command,
198 	.take_control = cec_take_control,
199 	.go_to_standby = cec_go_to_standby,
200 	.request_system_control = cec_request_system_control,
201 	.interface_clear = cec_interface_clear,
202 	.remote_enable = cec_remote_enable,
203 	.enable_eos = cec_enable_eos,
204 	.disable_eos = cec_disable_eos,
205 	.parallel_poll = cec_parallel_poll,
206 	.parallel_poll_configure = cec_parallel_poll_configure,
207 	.parallel_poll_response = cec_parallel_poll_response,
208 	.local_parallel_poll_mode = NULL, // XXX
209 	.line_status = NULL,	// XXX
210 	.update_status = cec_update_status,
211 	.primary_address = cec_primary_address,
212 	.secondary_address = cec_secondary_address,
213 	.serial_poll_response = cec_serial_poll_response,
214 	.serial_poll_status = cec_serial_poll_status,
215 	.t1_delay = cec_t1_delay,
216 	.return_to_local = cec_return_to_local,
217 };
218 
219 static int cec_allocate_private(struct gpib_board *board)
220 {
221 	struct cec_priv *priv;
222 
223 	board->private_data = kzalloc(sizeof(struct cec_priv), GFP_KERNEL);
224 	if (!board->private_data)
225 		return -ENOMEM;
226 	priv = board->private_data;
227 	init_nec7210_private(&priv->nec7210_priv);
228 	return 0;
229 }
230 
231 static void cec_free_private(struct gpib_board *board)
232 {
233 	kfree(board->private_data);
234 	board->private_data = NULL;
235 }
236 
237 static int cec_generic_attach(struct gpib_board *board)
238 {
239 	struct cec_priv *cec_priv;
240 	struct nec7210_priv *nec_priv;
241 
242 	board->status = 0;
243 
244 	if (cec_allocate_private(board))
245 		return -ENOMEM;
246 	cec_priv = board->private_data;
247 	nec_priv = &cec_priv->nec7210_priv;
248 	nec_priv->read_byte = nec7210_ioport_read_byte;
249 	nec_priv->write_byte = nec7210_ioport_write_byte;
250 	nec_priv->offset = cec_reg_offset;
251 	nec_priv->type = NEC7210;	// guess
252 	return 0;
253 }
254 
255 static void cec_init(struct cec_priv *cec_priv, const struct gpib_board *board)
256 {
257 	struct nec7210_priv *nec_priv = &cec_priv->nec7210_priv;
258 
259 	nec7210_board_reset(nec_priv, board);
260 
261 	/* set internal counter register for 8 MHz input clock */
262 	write_byte(nec_priv, ICR | 8, AUXMR);
263 
264 	nec7210_board_online(nec_priv, board);
265 }
266 
267 static int cec_pci_attach(struct gpib_board *board, const struct gpib_board_config *config)
268 {
269 	struct cec_priv *cec_priv;
270 	struct nec7210_priv *nec_priv;
271 	int isr_flags = 0;
272 	int retval;
273 
274 	retval = cec_generic_attach(board);
275 	if (retval)
276 		return retval;
277 
278 	cec_priv = board->private_data;
279 	nec_priv = &cec_priv->nec7210_priv;
280 
281 	// find board
282 	cec_priv->pci_device = NULL;
283 	while ((cec_priv->pci_device =
284 		gpib_pci_get_device(config, CEC_VENDOR_ID,
285 				    CEC_DEV_ID, cec_priv->pci_device)))	{
286 		// check for board with plx9050 controller
287 		if (cec_priv->pci_device->subsystem_device == CEC_SUBID)
288 			break;
289 	}
290 	if (!cec_priv->pci_device) {
291 		dev_err(board->gpib_dev, "no cec PCI board found\n");
292 		return -ENODEV;
293 	}
294 
295 	if (pci_enable_device(cec_priv->pci_device)) {
296 		dev_err(board->gpib_dev, "error enabling pci device\n");
297 		return -EIO;
298 	}
299 
300 	if (pci_request_regions(cec_priv->pci_device, "cec-gpib"))
301 		return -EBUSY;
302 
303 	cec_priv->plx_iobase = pci_resource_start(cec_priv->pci_device, 1);
304 	nec_priv->iobase = pci_resource_start(cec_priv->pci_device, 3);
305 
306 	isr_flags |= IRQF_SHARED;
307 	if (request_irq(cec_priv->pci_device->irq, cec_interrupt, isr_flags, DRV_NAME, board)) {
308 		dev_err(board->gpib_dev, "failed to obtain IRQ %d\n", cec_priv->pci_device->irq);
309 		return -EBUSY;
310 	}
311 	cec_priv->irq = cec_priv->pci_device->irq;
312 	if (gpib_request_pseudo_irq(board, cec_interrupt)) {
313 		dev_err(board->gpib_dev, "failed to allocate pseudo irq\n");
314 		return -1;
315 	}
316 	cec_init(cec_priv, board);
317 
318 	// enable interrupts on plx chip
319 	outl(PLX9050_LINTR1_EN_BIT | PLX9050_LINTR1_POLARITY_BIT | PLX9050_PCI_INTR_EN_BIT,
320 	     cec_priv->plx_iobase + PLX9050_INTCSR_REG);
321 
322 	return 0;
323 }
324 
325 static void cec_pci_detach(struct gpib_board *board)
326 {
327 	struct cec_priv *cec_priv = board->private_data;
328 	struct nec7210_priv *nec_priv;
329 
330 	if (cec_priv) {
331 		nec_priv = &cec_priv->nec7210_priv;
332 		gpib_free_pseudo_irq(board);
333 		if (cec_priv->irq) {
334 			// disable plx9050 interrupts
335 			outl(0, cec_priv->plx_iobase + PLX9050_INTCSR_REG);
336 			free_irq(cec_priv->irq, board);
337 		}
338 		if (nec_priv->iobase) {
339 			nec7210_board_reset(nec_priv, board);
340 			pci_release_regions(cec_priv->pci_device);
341 		}
342 		if (cec_priv->pci_device)
343 			pci_dev_put(cec_priv->pci_device);
344 	}
345 	cec_free_private(board);
346 }
347 
348 static int cec_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
349 {
350 	return 0;
351 }
352 
353 static const struct pci_device_id cec_pci_table[] = {
354 	{CEC_VENDOR_ID, CEC_DEV_ID, PCI_ANY_ID, CEC_SUBID, 0, 0, 0 },
355 	{0}
356 };
357 MODULE_DEVICE_TABLE(pci, cec_pci_table);
358 
359 static struct pci_driver cec_pci_driver = {
360 	.name = DRV_NAME,
361 	.id_table = cec_pci_table,
362 	.probe = &cec_pci_probe
363 };
364 
365 static int __init cec_init_module(void)
366 {
367 	int result;
368 
369 	result = pci_register_driver(&cec_pci_driver);
370 	if (result) {
371 		pr_err("pci_register_driver failed: error = %d\n", result);
372 		return result;
373 	}
374 
375 	result = gpib_register_driver(&cec_pci_interface, THIS_MODULE);
376 	if (result) {
377 		pr_err("gpib_register_driver failed: error = %d\n", result);
378 		return result;
379 	}
380 
381 	return 0;
382 }
383 
384 static void cec_exit_module(void)
385 {
386 	gpib_unregister_driver(&cec_pci_interface);
387 
388 	pci_unregister_driver(&cec_pci_driver);
389 }
390 
391 module_init(cec_init_module);
392 module_exit(cec_exit_module);
393