uio_aec.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include <linux/slab.h>
  30. #define PCI_VENDOR_ID_AEC 0xaecb
  31. #define PCI_DEVICE_ID_AEC_VITCLTC 0x6250
  32. #define INT_ENABLE_ADDR 0xFC
  33. #define INT_ENABLE 0x10
  34. #define INT_DISABLE 0x0
  35. #define INT_MASK_ADDR 0x2E
  36. #define INT_MASK_ALL 0x3F
  37. #define INTA_DRVR_ADDR 0xFE
  38. #define INTA_ENABLED_FLAG 0x08
  39. #define INTA_FLAG 0x01
  40. #define MAILBOX 0x0F
  41. static struct pci_device_id ids[] = {
  42. { PCI_DEVICE(PCI_VENDOR_ID_AEC, PCI_DEVICE_ID_AEC_VITCLTC), },
  43. { 0, }
  44. };
  45. MODULE_DEVICE_TABLE(pci, ids);
  46. static irqreturn_t aectc_irq(int irq, struct uio_info *dev_info)
  47. {
  48. void __iomem *int_flag = dev_info->priv + INTA_DRVR_ADDR;
  49. unsigned char status = ioread8(int_flag);
  50. if ((status & INTA_ENABLED_FLAG) && (status & INTA_FLAG)) {
  51. /* application writes 0x00 to 0x2F to get next interrupt */
  52. status = ioread8(dev_info->priv + MAILBOX);
  53. return IRQ_HANDLED;
  54. }
  55. return IRQ_NONE;
  56. }
  57. static void print_board_data(struct pci_dev *pdev, struct uio_info *i)
  58. {
  59. dev_info(&pdev->dev, "PCI-TC board vendor: %x%x number: %x%x"
  60. " revision: %c%c\n",
  61. ioread8(i->priv + 0x01),
  62. ioread8(i->priv + 0x00),
  63. ioread8(i->priv + 0x03),
  64. ioread8(i->priv + 0x02),
  65. ioread8(i->priv + 0x06),
  66. ioread8(i->priv + 0x07));
  67. }
  68. static int probe(struct pci_dev *pdev, const struct pci_device_id *id)
  69. {
  70. struct uio_info *info;
  71. int ret;
  72. info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
  73. if (!info)
  74. return -ENOMEM;
  75. if (pci_enable_device(pdev))
  76. goto out_free;
  77. if (pci_request_regions(pdev, "aectc"))
  78. goto out_disable;
  79. info->name = "aectc";
  80. info->port[0].start = pci_resource_start(pdev, 0);
  81. if (!info->port[0].start)
  82. goto out_release;
  83. info->priv = pci_iomap(pdev, 0, 0);
  84. if (!info->priv)
  85. goto out_release;
  86. info->port[0].size = pci_resource_len(pdev, 0);
  87. info->port[0].porttype = UIO_PORT_GPIO;
  88. info->version = "0.0.1";
  89. info->irq = pdev->irq;
  90. info->irq_flags = IRQF_SHARED;
  91. info->handler = aectc_irq;
  92. print_board_data(pdev, info);
  93. ret = uio_register_device(&pdev->dev, info);
  94. if (ret)
  95. goto out_unmap;
  96. iowrite32(INT_ENABLE, info->priv + INT_ENABLE_ADDR);
  97. iowrite8(INT_MASK_ALL, info->priv + INT_MASK_ADDR);
  98. if (!(ioread8(info->priv + INTA_DRVR_ADDR)
  99. & INTA_ENABLED_FLAG))
  100. dev_err(&pdev->dev, "aectc: interrupts not enabled\n");
  101. pci_set_drvdata(pdev, info);
  102. return 0;
  103. out_unmap:
  104. pci_iounmap(pdev, info->priv);
  105. out_release:
  106. pci_release_regions(pdev);
  107. out_disable:
  108. pci_disable_device(pdev);
  109. out_free:
  110. kfree(info);
  111. return -ENODEV;
  112. }
  113. static void remove(struct pci_dev *pdev)
  114. {
  115. struct uio_info *info = pci_get_drvdata(pdev);
  116. /* disable interrupts */
  117. iowrite8(INT_DISABLE, info->priv + INT_MASK_ADDR);
  118. iowrite32(INT_DISABLE, info->priv + INT_ENABLE_ADDR);
  119. /* read mailbox to ensure board drops irq */
  120. ioread8(info->priv + MAILBOX);
  121. uio_unregister_device(info);
  122. pci_release_regions(pdev);
  123. pci_disable_device(pdev);
  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. module_pci_driver(pci_driver);
  134. MODULE_LICENSE("GPL");