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
cec_interrupt(int irq,void * arg)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 gpib_board_config_t *config);
44
45 static void cec_pci_detach(struct gpib_board *board);
46
47 // wrappers for interface functions
cec_read(struct gpib_board * board,uint8_t * buffer,size_t length,int * end,size_t * bytes_read)48 static int cec_read(struct gpib_board *board, uint8_t *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
cec_write(struct gpib_board * board,uint8_t * buffer,size_t length,int send_eoi,size_t * bytes_written)56 static int cec_write(struct gpib_board *board, uint8_t *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
cec_command(struct gpib_board * board,uint8_t * buffer,size_t length,size_t * bytes_written)64 static int cec_command(struct gpib_board *board, uint8_t *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
cec_take_control(struct gpib_board * board,int synchronous)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
cec_go_to_standby(struct gpib_board * board)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
cec_request_system_control(struct gpib_board * board,int request_control)86 static void cec_request_system_control(struct gpib_board *board, int request_control)
87 {
88 struct cec_priv *priv = board->private_data;
89
90 nec7210_request_system_control(board, &priv->nec7210_priv, request_control);
91 }
92
cec_interface_clear(struct gpib_board * board,int assert)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
cec_remote_enable(struct gpib_board * board,int enable)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
cec_enable_eos(struct gpib_board * board,uint8_t eos_byte,int compare_8_bits)107 static int cec_enable_eos(struct gpib_board *board, uint8_t 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
cec_disable_eos(struct gpib_board * board)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
cec_update_status(struct gpib_board * board,unsigned int clear_mask)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
cec_primary_address(struct gpib_board * board,unsigned int address)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
cec_secondary_address(struct gpib_board * board,unsigned int address,int enable)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
cec_parallel_poll(struct gpib_board * board,uint8_t * result)142 static int cec_parallel_poll(struct gpib_board *board, uint8_t *result)
143 {
144 struct cec_priv *priv = board->private_data;
145
146 return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
147 }
148
cec_parallel_poll_configure(struct gpib_board * board,uint8_t config)149 static void cec_parallel_poll_configure(struct gpib_board *board, uint8_t config)
150 {
151 struct cec_priv *priv = board->private_data;
152
153 nec7210_parallel_poll_configure(board, &priv->nec7210_priv, config);
154 }
155
cec_parallel_poll_response(struct gpib_board * board,int ist)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
cec_serial_poll_response(struct gpib_board * board,uint8_t status)163 static void cec_serial_poll_response(struct gpib_board *board, uint8_t status)
164 {
165 struct cec_priv *priv = board->private_data;
166
167 nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
168 }
169
cec_serial_poll_status(struct gpib_board * board)170 static uint8_t 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
cec_t1_delay(struct gpib_board * board,unsigned int nano_sec)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
cec_return_to_local(struct gpib_board * board)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 gpib_interface_t 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
cec_allocate_private(struct gpib_board * board)219 static int cec_allocate_private(struct gpib_board *board)
220 {
221 struct cec_priv *priv;
222
223 board->private_data = kmalloc(sizeof(struct cec_priv), GFP_KERNEL);
224 if (!board->private_data)
225 return -1;
226 priv = board->private_data;
227 memset(priv, 0, sizeof(struct cec_priv));
228 init_nec7210_private(&priv->nec7210_priv);
229 return 0;
230 }
231
cec_free_private(struct gpib_board * board)232 static void cec_free_private(struct gpib_board *board)
233 {
234 kfree(board->private_data);
235 board->private_data = NULL;
236 }
237
cec_generic_attach(struct gpib_board * board)238 static int cec_generic_attach(struct gpib_board *board)
239 {
240 struct cec_priv *cec_priv;
241 struct nec7210_priv *nec_priv;
242
243 board->status = 0;
244
245 if (cec_allocate_private(board))
246 return -ENOMEM;
247 cec_priv = board->private_data;
248 nec_priv = &cec_priv->nec7210_priv;
249 nec_priv->read_byte = nec7210_ioport_read_byte;
250 nec_priv->write_byte = nec7210_ioport_write_byte;
251 nec_priv->offset = cec_reg_offset;
252 nec_priv->type = NEC7210; // guess
253 return 0;
254 }
255
cec_init(struct cec_priv * cec_priv,const struct gpib_board * board)256 static void cec_init(struct cec_priv *cec_priv, const struct gpib_board *board)
257 {
258 struct nec7210_priv *nec_priv = &cec_priv->nec7210_priv;
259
260 nec7210_board_reset(nec_priv, board);
261
262 /* set internal counter register for 8 MHz input clock */
263 write_byte(nec_priv, ICR | 8, AUXMR);
264
265 nec7210_board_online(nec_priv, board);
266 }
267
cec_pci_attach(struct gpib_board * board,const gpib_board_config_t * config)268 static int cec_pci_attach(struct gpib_board *board, const gpib_board_config_t *config)
269 {
270 struct cec_priv *cec_priv;
271 struct nec7210_priv *nec_priv;
272 int isr_flags = 0;
273 int retval;
274
275 retval = cec_generic_attach(board);
276 if (retval)
277 return retval;
278
279 cec_priv = board->private_data;
280 nec_priv = &cec_priv->nec7210_priv;
281
282 // find board
283 cec_priv->pci_device = NULL;
284 while ((cec_priv->pci_device =
285 gpib_pci_get_device(config, CEC_VENDOR_ID,
286 CEC_DEV_ID, cec_priv->pci_device))) {
287 // check for board with plx9050 controller
288 if (cec_priv->pci_device->subsystem_device == CEC_SUBID)
289 break;
290 }
291 if (!cec_priv->pci_device) {
292 dev_err(board->gpib_dev, "no cec PCI board found\n");
293 return -ENODEV;
294 }
295
296 if (pci_enable_device(cec_priv->pci_device)) {
297 dev_err(board->gpib_dev, "error enabling pci device\n");
298 return -EIO;
299 }
300
301 if (pci_request_regions(cec_priv->pci_device, "cec-gpib"))
302 return -EBUSY;
303
304 cec_priv->plx_iobase = pci_resource_start(cec_priv->pci_device, 1);
305 nec_priv->iobase = pci_resource_start(cec_priv->pci_device, 3);
306
307 isr_flags |= IRQF_SHARED;
308 if (request_irq(cec_priv->pci_device->irq, cec_interrupt, isr_flags, DRV_NAME, board)) {
309 dev_err(board->gpib_dev, "failed to obtain IRQ %d\n", cec_priv->pci_device->irq);
310 return -EBUSY;
311 }
312 cec_priv->irq = cec_priv->pci_device->irq;
313 if (gpib_request_pseudo_irq(board, cec_interrupt)) {
314 dev_err(board->gpib_dev, "failed to allocate pseudo irq\n");
315 return -1;
316 }
317 cec_init(cec_priv, board);
318
319 // enable interrupts on plx chip
320 outl(PLX9050_LINTR1_EN_BIT | PLX9050_LINTR1_POLARITY_BIT | PLX9050_PCI_INTR_EN_BIT,
321 cec_priv->plx_iobase + PLX9050_INTCSR_REG);
322
323 return 0;
324 }
325
cec_pci_detach(struct gpib_board * board)326 static void cec_pci_detach(struct gpib_board *board)
327 {
328 struct cec_priv *cec_priv = board->private_data;
329 struct nec7210_priv *nec_priv;
330
331 if (cec_priv) {
332 nec_priv = &cec_priv->nec7210_priv;
333 gpib_free_pseudo_irq(board);
334 if (cec_priv->irq) {
335 // disable plx9050 interrupts
336 outl(0, cec_priv->plx_iobase + PLX9050_INTCSR_REG);
337 free_irq(cec_priv->irq, board);
338 }
339 if (nec_priv->iobase) {
340 nec7210_board_reset(nec_priv, board);
341 pci_release_regions(cec_priv->pci_device);
342 }
343 if (cec_priv->pci_device)
344 pci_dev_put(cec_priv->pci_device);
345 }
346 cec_free_private(board);
347 }
348
cec_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)349 static int cec_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
350 {
351 return 0;
352 }
353
354 static const struct pci_device_id cec_pci_table[] = {
355 {CEC_VENDOR_ID, CEC_DEV_ID, PCI_ANY_ID, CEC_SUBID, 0, 0, 0 },
356 {0}
357 };
358 MODULE_DEVICE_TABLE(pci, cec_pci_table);
359
360 static struct pci_driver cec_pci_driver = {
361 .name = DRV_NAME,
362 .id_table = cec_pci_table,
363 .probe = &cec_pci_probe
364 };
365
cec_init_module(void)366 static int __init cec_init_module(void)
367 {
368 int result;
369
370 result = pci_register_driver(&cec_pci_driver);
371 if (result) {
372 pr_err("pci_register_driver failed: error = %d\n", result);
373 return result;
374 }
375
376 result = gpib_register_driver(&cec_pci_interface, THIS_MODULE);
377 if (result) {
378 pr_err("gpib_register_driver failed: error = %d\n", result);
379 return result;
380 }
381
382 return 0;
383 }
384
cec_exit_module(void)385 static void cec_exit_module(void)
386 {
387 gpib_unregister_driver(&cec_pci_interface);
388
389 pci_unregister_driver(&cec_pci_driver);
390 }
391
392 module_init(cec_init_module);
393 module_exit(cec_exit_module);
394