syscall.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * pci_syscall.c
  3. *
  4. * For architectures where we want to allow direct access
  5. * to the PCI config stuff - it would probably be preferable
  6. * on PCs too, but there people just do it by hand with the
  7. * magic northbridge registers..
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/pci.h>
  11. #include <linux/syscalls.h>
  12. #include <asm/uaccess.h>
  13. #include "pci.h"
  14. SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn,
  15. unsigned long, off, unsigned long, len, void __user *, buf)
  16. {
  17. struct pci_dev *dev;
  18. u8 byte;
  19. u16 word;
  20. u32 dword;
  21. long err;
  22. long cfg_ret;
  23. if (!capable(CAP_SYS_ADMIN))
  24. return -EPERM;
  25. err = -ENODEV;
  26. dev = pci_get_bus_and_slot(bus, dfn);
  27. if (!dev)
  28. goto error;
  29. switch (len) {
  30. case 1:
  31. cfg_ret = pci_user_read_config_byte(dev, off, &byte);
  32. break;
  33. case 2:
  34. cfg_ret = pci_user_read_config_word(dev, off, &word);
  35. break;
  36. case 4:
  37. cfg_ret = pci_user_read_config_dword(dev, off, &dword);
  38. break;
  39. default:
  40. err = -EINVAL;
  41. goto error;
  42. }
  43. err = -EIO;
  44. if (cfg_ret != PCIBIOS_SUCCESSFUL)
  45. goto error;
  46. switch (len) {
  47. case 1:
  48. err = put_user(byte, (unsigned char __user *)buf);
  49. break;
  50. case 2:
  51. err = put_user(word, (unsigned short __user *)buf);
  52. break;
  53. case 4:
  54. err = put_user(dword, (unsigned int __user *)buf);
  55. break;
  56. }
  57. pci_dev_put(dev);
  58. return err;
  59. error:
  60. /* ??? XFree86 doesn't even check the return value. They
  61. just look for 0xffffffff in the output, since that's what
  62. they get instead of a machine check on x86. */
  63. switch (len) {
  64. case 1:
  65. put_user(-1, (unsigned char __user *)buf);
  66. break;
  67. case 2:
  68. put_user(-1, (unsigned short __user *)buf);
  69. break;
  70. case 4:
  71. put_user(-1, (unsigned int __user *)buf);
  72. break;
  73. }
  74. pci_dev_put(dev);
  75. return err;
  76. }
  77. SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
  78. unsigned long, off, unsigned long, len, void __user *, buf)
  79. {
  80. struct pci_dev *dev;
  81. u8 byte;
  82. u16 word;
  83. u32 dword;
  84. int err = 0;
  85. if (!capable(CAP_SYS_ADMIN))
  86. return -EPERM;
  87. dev = pci_get_bus_and_slot(bus, dfn);
  88. if (!dev)
  89. return -ENODEV;
  90. switch (len) {
  91. case 1:
  92. err = get_user(byte, (u8 __user *)buf);
  93. if (err)
  94. break;
  95. err = pci_user_write_config_byte(dev, off, byte);
  96. if (err != PCIBIOS_SUCCESSFUL)
  97. err = -EIO;
  98. break;
  99. case 2:
  100. err = get_user(word, (u16 __user *)buf);
  101. if (err)
  102. break;
  103. err = pci_user_write_config_word(dev, off, word);
  104. if (err != PCIBIOS_SUCCESSFUL)
  105. err = -EIO;
  106. break;
  107. case 4:
  108. err = get_user(dword, (u32 __user *)buf);
  109. if (err)
  110. break;
  111. err = pci_user_write_config_dword(dev, off, dword);
  112. if (err != PCIBIOS_SUCCESSFUL)
  113. err = -EIO;
  114. break;
  115. default:
  116. err = -EINVAL;
  117. break;
  118. }
  119. pci_dev_put(dev);
  120. return err;
  121. }