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 int retval; 242 243 board->status = 0; 244 245 retval = cec_allocate_private(board); 246 if (retval) 247 return retval; 248 cec_priv = board->private_data; 249 nec_priv = &cec_priv->nec7210_priv; 250 nec_priv->read_byte = nec7210_ioport_read_byte; 251 nec_priv->write_byte = nec7210_ioport_write_byte; 252 nec_priv->offset = cec_reg_offset; 253 nec_priv->type = NEC7210; // guess 254 return 0; 255 } 256 257 static void cec_init(struct cec_priv *cec_priv, const struct gpib_board *board) 258 { 259 struct nec7210_priv *nec_priv = &cec_priv->nec7210_priv; 260 261 nec7210_board_reset(nec_priv, board); 262 263 /* set internal counter register for 8 MHz input clock */ 264 write_byte(nec_priv, ICR | 8, AUXMR); 265 266 nec7210_board_online(nec_priv, board); 267 } 268 269 static int cec_pci_attach(struct gpib_board *board, const struct gpib_board_config *config) 270 { 271 struct cec_priv *cec_priv; 272 struct nec7210_priv *nec_priv; 273 int isr_flags = 0; 274 int retval; 275 276 retval = cec_generic_attach(board); 277 if (retval) 278 return retval; 279 280 cec_priv = board->private_data; 281 nec_priv = &cec_priv->nec7210_priv; 282 283 // find board 284 cec_priv->pci_device = NULL; 285 while ((cec_priv->pci_device = 286 gpib_pci_get_device(config, CEC_VENDOR_ID, 287 CEC_DEV_ID, cec_priv->pci_device))) { 288 // check for board with plx9050 controller 289 if (cec_priv->pci_device->subsystem_device == CEC_SUBID) 290 break; 291 } 292 if (!cec_priv->pci_device) { 293 dev_err(board->gpib_dev, "no cec PCI board found\n"); 294 return -ENODEV; 295 } 296 297 if (pci_enable_device(cec_priv->pci_device)) { 298 dev_err(board->gpib_dev, "error enabling pci device\n"); 299 return -EIO; 300 } 301 302 if (pci_request_regions(cec_priv->pci_device, "cec-gpib")) 303 return -EBUSY; 304 305 cec_priv->plx_iobase = pci_resource_start(cec_priv->pci_device, 1); 306 nec_priv->iobase = pci_resource_start(cec_priv->pci_device, 3); 307 308 isr_flags |= IRQF_SHARED; 309 if (request_irq(cec_priv->pci_device->irq, cec_interrupt, isr_flags, DRV_NAME, board)) { 310 dev_err(board->gpib_dev, "failed to obtain IRQ %d\n", cec_priv->pci_device->irq); 311 return -EBUSY; 312 } 313 cec_priv->irq = cec_priv->pci_device->irq; 314 if (gpib_request_pseudo_irq(board, cec_interrupt)) { 315 dev_err(board->gpib_dev, "failed to allocate pseudo irq\n"); 316 return -1; 317 } 318 cec_init(cec_priv, board); 319 320 // enable interrupts on plx chip 321 outl(PLX9050_LINTR1_EN_BIT | PLX9050_LINTR1_POLARITY_BIT | PLX9050_PCI_INTR_EN_BIT, 322 cec_priv->plx_iobase + PLX9050_INTCSR_REG); 323 324 return 0; 325 } 326 327 static void cec_pci_detach(struct gpib_board *board) 328 { 329 struct cec_priv *cec_priv = board->private_data; 330 struct nec7210_priv *nec_priv; 331 332 if (cec_priv) { 333 nec_priv = &cec_priv->nec7210_priv; 334 gpib_free_pseudo_irq(board); 335 if (cec_priv->irq) { 336 // disable plx9050 interrupts 337 outl(0, cec_priv->plx_iobase + PLX9050_INTCSR_REG); 338 free_irq(cec_priv->irq, board); 339 } 340 if (nec_priv->iobase) { 341 nec7210_board_reset(nec_priv, board); 342 pci_release_regions(cec_priv->pci_device); 343 } 344 if (cec_priv->pci_device) 345 pci_dev_put(cec_priv->pci_device); 346 } 347 cec_free_private(board); 348 } 349 350 static int cec_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) 351 { 352 return 0; 353 } 354 355 static const struct pci_device_id cec_pci_table[] = { 356 {CEC_VENDOR_ID, CEC_DEV_ID, PCI_ANY_ID, CEC_SUBID, 0, 0, 0 }, 357 {0} 358 }; 359 MODULE_DEVICE_TABLE(pci, cec_pci_table); 360 361 static struct pci_driver cec_pci_driver = { 362 .name = DRV_NAME, 363 .id_table = cec_pci_table, 364 .probe = &cec_pci_probe 365 }; 366 367 static int __init cec_init_module(void) 368 { 369 int result; 370 371 result = pci_register_driver(&cec_pci_driver); 372 if (result) { 373 pr_err("pci_register_driver failed: error = %d\n", result); 374 return result; 375 } 376 377 result = gpib_register_driver(&cec_pci_interface, THIS_MODULE); 378 if (result) { 379 pr_err("gpib_register_driver failed: error = %d\n", result); 380 return result; 381 } 382 383 return 0; 384 } 385 386 static void cec_exit_module(void) 387 { 388 gpib_unregister_driver(&cec_pci_interface); 389 390 pci_unregister_driver(&cec_pci_driver); 391 } 392 393 module_init(cec_init_module); 394 module_exit(cec_exit_module); 395