1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// SPDX-License-Identifier: GPL-2.0

/***************************************************************************
 *   copyright            : (C) 2002 by Frank Mori Hess
 ***************************************************************************/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define dev_fmt pr_fmt
#define DRV_NAME KBUILD_MODNAME

#include "cec.h"
#include <linux/pci.h>
#include <linux/io.h>
#include <linux/bitops.h>
#include <asm/dma.h>
#include <linux/module.h>
#include <linux/slab.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("GPIB driver for CEC PCI and PCMCIA boards");

/*
 * GPIB interrupt service routines
 */

static irqreturn_t cec_interrupt(int irq, void *arg)
{
	struct gpib_board *board = arg;
	struct cec_priv *priv = board->private_data;
	unsigned long flags;
	irqreturn_t retval;

	spin_lock_irqsave(&board->spinlock, flags);
	retval = nec7210_interrupt(board, &priv->nec7210_priv);
	spin_unlock_irqrestore(&board->spinlock, flags);
	return retval;
}

#define CEC_VENDOR_ID 0x12fc
#define CEC_DEV_ID    0x5cec
#define CEC_SUBID 0x9050

static int cec_pci_attach(struct gpib_board *board, const gpib_board_config_t *config);

static void cec_pci_detach(struct gpib_board *board);

// wrappers for interface functions
static int cec_read(struct gpib_board *board, uint8_t *buffer, size_t length, int *end,
		    size_t *bytes_read)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
}

static int cec_write(struct gpib_board *board, uint8_t *buffer, size_t length, int send_eoi,
		     size_t *bytes_written)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
}

static int cec_command(struct gpib_board *board, uint8_t *buffer,
		       size_t length, size_t *bytes_written)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
}

static int cec_take_control(struct gpib_board *board, int synchronous)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
}

static int cec_go_to_standby(struct gpib_board *board)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_go_to_standby(board, &priv->nec7210_priv);
}

static void cec_request_system_control(struct gpib_board *board, int request_control)
{
	struct cec_priv *priv = board->private_data;

	nec7210_request_system_control(board, &priv->nec7210_priv, request_control);
}

static void cec_interface_clear(struct gpib_board *board, int assert)
{
	struct cec_priv *priv = board->private_data;

	nec7210_interface_clear(board, &priv->nec7210_priv, assert);
}

static void cec_remote_enable(struct gpib_board *board, int enable)
{
	struct cec_priv *priv = board->private_data;

	nec7210_remote_enable(board, &priv->nec7210_priv, enable);
}

static int cec_enable_eos(struct gpib_board *board, uint8_t eos_byte, int compare_8_bits)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
}

static void cec_disable_eos(struct gpib_board *board)
{
	struct cec_priv *priv = board->private_data;

	nec7210_disable_eos(board, &priv->nec7210_priv);
}

static unsigned int cec_update_status(struct gpib_board *board, unsigned int clear_mask)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_update_status(board, &priv->nec7210_priv, clear_mask);
}

static int cec_primary_address(struct gpib_board *board, unsigned int address)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_primary_address(board, &priv->nec7210_priv, address);
}

static int cec_secondary_address(struct gpib_board *board, unsigned int address, int enable)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
}

static int cec_parallel_poll(struct gpib_board *board, uint8_t *result)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
}

static void cec_parallel_poll_configure(struct gpib_board *board, uint8_t config)
{
	struct cec_priv *priv = board->private_data;

	nec7210_parallel_poll_configure(board, &priv->nec7210_priv, config);
}

static void cec_parallel_poll_response(struct gpib_board *board, int ist)
{
	struct cec_priv *priv = board->private_data;

	nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
}

static void cec_serial_poll_response(struct gpib_board *board, uint8_t status)
{
	struct cec_priv *priv = board->private_data;

	nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
}

static uint8_t cec_serial_poll_status(struct gpib_board *board)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_serial_poll_status(board, &priv->nec7210_priv);
}

static int cec_t1_delay(struct gpib_board *board, unsigned int nano_sec)
{
	struct cec_priv *priv = board->private_data;

	return nec7210_t1_delay(board, &priv->nec7210_priv, nano_sec);
}

static void cec_return_to_local(struct gpib_board *board)
{
	struct cec_priv *priv = board->private_data;

	nec7210_return_to_local(board, &priv->nec7210_priv);
}

static gpib_interface_t cec_pci_interface = {
	.name = "cec_pci",
	.attach = cec_pci_attach,
	.detach = cec_pci_detach,
	.read = cec_read,
	.write = cec_write,
	.command = cec_command,
	.take_control = cec_take_control,
	.go_to_standby = cec_go_to_standby,
	.request_system_control = cec_request_system_control,
	.interface_clear = cec_interface_clear,
	.remote_enable = cec_remote_enable,
	.enable_eos = cec_enable_eos,
	.disable_eos = cec_disable_eos,
	.parallel_poll = cec_parallel_poll,
	.parallel_poll_configure = cec_parallel_poll_configure,
	.parallel_poll_response = cec_parallel_poll_response,
	.local_parallel_poll_mode = NULL, // XXX
	.line_status = NULL,	//XXX
	.update_status = cec_update_status,
	.primary_address = cec_primary_address,
	.secondary_address = cec_secondary_address,
	.serial_poll_response = cec_serial_poll_response,
	.serial_poll_status = cec_serial_poll_status,
	.t1_delay = cec_t1_delay,
	.return_to_local = cec_return_to_local,
};

static int cec_allocate_private(struct gpib_board *board)
{
	struct cec_priv *priv;

	board->private_data = kmalloc(sizeof(struct cec_priv), GFP_KERNEL);
	if (!board->private_data)
		return -1;
	priv = board->private_data;
	memset(priv, 0, sizeof(struct cec_priv));
	init_nec7210_private(&priv->nec7210_priv);
	return 0;
}

static void cec_free_private(struct gpib_board *board)
{
	kfree(board->private_data);
	board->private_data = NULL;
}

static int cec_generic_attach(struct gpib_board *board)
{
	struct cec_priv *cec_priv;
	struct nec7210_priv *nec_priv;

	board->status = 0;

	if (cec_allocate_private(board))
		return -ENOMEM;
	cec_priv = board->private_data;
	nec_priv = &cec_priv->nec7210_priv;
	nec_priv->read_byte = nec7210_ioport_read_byte;
	nec_priv->write_byte = nec7210_ioport_write_byte;
	nec_priv->offset = cec_reg_offset;
	nec_priv->type = NEC7210;	// guess
	return 0;
}

static void cec_init(struct cec_priv *cec_priv, const struct gpib_board *board)
{
	struct nec7210_priv *nec_priv = &cec_priv->nec7210_priv;

	nec7210_board_reset(nec_priv, board);

	/* set internal counter register for 8 MHz input clock */
	write_byte(nec_priv, ICR | 8, AUXMR);

	nec7210_board_online(nec_priv, board);
}

static int cec_pci_attach(struct gpib_board *board, const gpib_board_config_t *config)
{
	struct cec_priv *cec_priv;
	struct nec7210_priv *nec_priv;
	int isr_flags = 0;
	int retval;

	retval = cec_generic_attach(board);
	if (retval)
		return retval;

	cec_priv = board->private_data;
	nec_priv = &cec_priv->nec7210_priv;

	// find board
	cec_priv->pci_device = NULL;
	while ((cec_priv->pci_device =
		gpib_pci_get_device(config, CEC_VENDOR_ID,
				    CEC_DEV_ID, cec_priv->pci_device)))	{
		// check for board with plx9050 controller
		if (cec_priv->pci_device->subsystem_device == CEC_SUBID)
			break;
	}
	if (!cec_priv->pci_device) {
		dev_err(board->gpib_dev, "no cec PCI board found\n");
		return -ENODEV;
	}

	if (pci_enable_device(cec_priv->pci_device)) {
		dev_err(board->gpib_dev, "error enabling pci device\n");
		return -EIO;
	}

	if (pci_request_regions(cec_priv->pci_device, "cec-gpib"))
		return -EBUSY;

	cec_priv->plx_iobase = pci_resource_start(cec_priv->pci_device, 1);
		nec_priv->iobase = pci_resource_start(cec_priv->pci_device, 3);

	isr_flags |= IRQF_SHARED;
	if (request_irq(cec_priv->pci_device->irq, cec_interrupt, isr_flags, DRV_NAME, board)) {
		dev_err(board->gpib_dev, "failed to obtain IRQ %d\n", cec_priv->pci_device->irq);
		return -EBUSY;
	}
	cec_priv->irq = cec_priv->pci_device->irq;
	if (gpib_request_pseudo_irq(board, cec_interrupt)) {
		dev_err(board->gpib_dev, "failed to allocate pseudo irq\n");
		return -1;
	}
	cec_init(cec_priv, board);

	// enable interrupts on plx chip
	outl(PLX9050_LINTR1_EN_BIT | PLX9050_LINTR1_POLARITY_BIT | PLX9050_PCI_INTR_EN_BIT,
	     cec_priv->plx_iobase + PLX9050_INTCSR_REG);

	return 0;
}

static void cec_pci_detach(struct gpib_board *board)
{
	struct cec_priv *cec_priv = board->private_data;
	struct nec7210_priv *nec_priv;

	if (cec_priv) {
		nec_priv = &cec_priv->nec7210_priv;
		gpib_free_pseudo_irq(board);
		if (cec_priv->irq) {
			// disable plx9050 interrupts
			outl(0, cec_priv->plx_iobase + PLX9050_INTCSR_REG);
			free_irq(cec_priv->irq, board);
		}
		if (nec_priv->iobase) {
			nec7210_board_reset(nec_priv, board);
			pci_release_regions(cec_priv->pci_device);
		}
		if (cec_priv->pci_device)
			pci_dev_put(cec_priv->pci_device);
	}
	cec_free_private(board);
}

static int cec_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
	return 0;
}

static const struct pci_device_id cec_pci_table[] = {
	{CEC_VENDOR_ID, CEC_DEV_ID, PCI_ANY_ID, CEC_SUBID, 0, 0, 0 },
	{0}
};
MODULE_DEVICE_TABLE(pci, cec_pci_table);

static struct pci_driver cec_pci_driver = {
	.name = DRV_NAME,
	.id_table = cec_pci_table,
	.probe = &cec_pci_probe
};

static int __init cec_init_module(void)
{
	int result;

	result = pci_register_driver(&cec_pci_driver);
	if (result) {
		pr_err("pci_register_driver failed: error = %d\n", result);
		return result;
	}

	result = gpib_register_driver(&cec_pci_interface, THIS_MODULE);
	if (result) {
		pr_err("gpib_register_driver failed: error = %d\n", result);
		return result;
	}

	return 0;
}

static void cec_exit_module(void)
{
	gpib_unregister_driver(&cec_pci_interface);

	pci_unregister_driver(&cec_pci_driver);
}

module_init(cec_init_module);
module_exit(cec_exit_module);
最肯忘卻古人詩 最不屑一顧是相思