cec_gpib.c 10 KB

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