uio_aec.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * uio_aec.c -- simple driver for Adrienne Electronics Corp time code PCI device
  3. *
  4. * Copyright (C) 2008 Brandon Philips <brandon@ifup.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc., 59
  17. * Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/cdev.h>
  25. #include <linux/fs.h>
  26. #include <linux/io.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/uio_driver.h>
  29. #define PCI_VENDOR_ID_AEC 0xaecb
  30. #define PCI_DEVICE_ID_AEC_VITCLTC 0x6250
  31. #define INT_ENABLE_ADDR 0xFC
  32. #define INT_ENABLE 0x10
  33. #define INT_DISABLE 0x0
  34. #define INT_MASK_ADDR 0x2E
  35. #define INT_MASK_ALL 0x3F
  36. #define INTA_DRVR_ADDR 0xFE
  37. #define INTA_ENABLED_FLAG 0x08
  38. #define INTA_FLAG 0x01
  39. #define MAILBOX 0x0F
  40. static struct pci_device_id ids[] = {
  41. { PCI_DEVICE(PCI_VENDOR_ID_AEC, PCI_DEVICE_ID_AEC_VITCLTC), },
  42. { 0, }
  43. };
  44. MODULE_DEVICE_TABLE(pci, ids);
  45. static irqreturn_t aectc_irq(int irq, struct uio_info *dev_info)
  46. {
  47. void __iomem *int_flag = dev_info->priv + INTA_DRVR_ADDR;
  48. unsigned char status = ioread8(int_flag);
  49. if ((status & INTA_ENABLED_FLAG) && (status & INTA_FLAG)) {
  50. /* application writes 0x00 to 0x2F to get next interrupt */
  51. status = ioread8(dev_info->priv + MAILBOX);
  52. return IRQ_HANDLED;
  53. }
  54. return IRQ_NONE;
  55. }
  56. static void print_board_data(struct pci_dev *pdev, struct uio_info *i)
  57. {
  58. dev_info(&pdev->dev, "PCI-TC board vendor: %x%x number: %x%x"
  59. " revision: %c%c\n",
  60. ioread8(i->priv + 0x01),
  61. ioread8(i->priv + 0x00),
  62. ioread8(i->priv + 0x03),
  63. ioread8(i->priv + 0x02),
  64. ioread8(i->priv + 0x06),
  65. ioread8(i->priv + 0x07));
  66. }
  67. static int __devinit probe(struct pci_dev *pdev, const struct pci_device_id *id)
  68. {
  69. struct uio_info *info;
  70. int ret;
  71. info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
  72. if (!info)
  73. return -ENOMEM;
  74. if (pci_enable_device(pdev))
  75. goto out_free;
  76. if (pci_request_regions(pdev, "aectc"))
  77. goto out_disable;
  78. info->name = "aectc";
  79. info->port[0].start = pci_resource_start(pdev, 0);
  80. if (!info->port[0].start)
  81. goto out_release;
  82. info->priv = pci_iomap(pdev, 0, 0);
  83. if (!info->priv)
  84. goto out_release;
  85. info->port[0].size = pci_resource_len(pdev, 0);
  86. info->port[0].porttype = UIO_PORT_GPIO;
  87. info->version = "0.0.1";
  88. info->irq = pdev->irq;
  89. info->irq_flags = IRQF_SHARED;
  90. info->handler = aectc_irq;
  91. print_board_data(pdev, info);
  92. ret = uio_register_device(&pdev->dev, info);
  93. if (ret)
  94. goto out_unmap;
  95. iowrite32(INT_ENABLE, info->priv + INT_ENABLE_ADDR);
  96. iowrite8(INT_MASK_ALL, info->priv + INT_MASK_ADDR);
  97. if (!(ioread8(info->priv + INTA_DRVR_ADDR)
  98. & INTA_ENABLED_FLAG))
  99. dev_err(&pdev->dev, "aectc: interrupts not enabled\n");
  100. pci_set_drvdata(pdev, info);
  101. return 0;
  102. out_unmap:
  103. pci_iounmap(pdev, info->priv);
  104. out_release:
  105. pci_release_regions(pdev);
  106. out_disable:
  107. pci_disable_device(pdev);
  108. out_free:
  109. kfree(info);
  110. return -ENODEV;
  111. }
  112. static void remove(struct pci_dev *pdev)
  113. {
  114. struct uio_info *info = pci_get_drvdata(pdev);
  115. /* disable interrupts */
  116. iowrite8(INT_DISABLE, info->priv + INT_MASK_ADDR);
  117. iowrite32(INT_DISABLE, info->priv + INT_ENABLE_ADDR);
  118. /* read mailbox to ensure board drops irq */
  119. ioread8(info->priv + MAILBOX);
  120. uio_unregister_device(info);
  121. pci_release_regions(pdev);
  122. pci_disable_device(pdev);
  123. pci_set_drvdata(pdev, NULL);
  124. iounmap(info->priv);
  125. kfree(info);
  126. }
  127. static struct pci_driver pci_driver = {
  128. .name = "aectc",
  129. .id_table = ids,
  130. .probe = probe,
  131. .remove = remove,
  132. };
  133. static int __init aectc_init(void)
  134. {
  135. return pci_register_driver(&pci_driver);
  136. }
  137. static void __exit aectc_exit(void)
  138. {
  139. pci_unregister_driver(&pci_driver);
  140. }
  141. MODULE_LICENSE("GPL");
  142. module_init(aectc_init);
  143. module_exit(aectc_exit);