intel_scu_ipcutil.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Intel SCU IPC mechanism
  4. *
  5. * (C) Copyright 2008-2010 Intel Corporation
  6. * Author: Sreedhara DS (sreedhara.ds@intel.com)
  7. *
  8. * This driver provides IOCTL interfaces to call Intel SCU IPC driver API.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/fcntl.h>
  12. #include <linux/fs.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/intel_scu_ipc.h>
  20. static int major;
  21. /* IOCTL commands */
  22. #define INTE_SCU_IPC_REGISTER_READ 0
  23. #define INTE_SCU_IPC_REGISTER_WRITE 1
  24. #define INTE_SCU_IPC_REGISTER_UPDATE 2
  25. struct scu_ipc_data {
  26. u32 count; /* No. of registers */
  27. u16 addr[5]; /* Register addresses */
  28. u8 data[5]; /* Register data */
  29. u8 mask; /* Valid for read-modify-write */
  30. };
  31. /**
  32. * scu_reg_access - implement register access ioctls
  33. * @cmd: command we are doing (read/write/update)
  34. * @data: kernel copy of ioctl data
  35. *
  36. * Allow the user to perform register accesses on the SCU via the
  37. * kernel interface
  38. */
  39. static int scu_reg_access(u32 cmd, struct scu_ipc_data *data)
  40. {
  41. unsigned int count = data->count;
  42. if (count == 0 || count == 3 || count > 4)
  43. return -EINVAL;
  44. switch (cmd) {
  45. case INTE_SCU_IPC_REGISTER_READ:
  46. return intel_scu_ipc_readv(data->addr, data->data, count);
  47. case INTE_SCU_IPC_REGISTER_WRITE:
  48. return intel_scu_ipc_writev(data->addr, data->data, count);
  49. case INTE_SCU_IPC_REGISTER_UPDATE:
  50. return intel_scu_ipc_update_register(data->addr[0],
  51. data->data[0], data->mask);
  52. default:
  53. return -ENOTTY;
  54. }
  55. }
  56. /**
  57. * scu_ipc_ioctl - control ioctls for the SCU
  58. * @fp: file handle of the SCU device
  59. * @cmd: ioctl coce
  60. * @arg: pointer to user passed structure
  61. *
  62. * Support the I/O and firmware flashing interfaces of the SCU
  63. */
  64. static long scu_ipc_ioctl(struct file *fp, unsigned int cmd,
  65. unsigned long arg)
  66. {
  67. int ret;
  68. struct scu_ipc_data data;
  69. void __user *argp = (void __user *)arg;
  70. if (!capable(CAP_SYS_RAWIO))
  71. return -EPERM;
  72. if (copy_from_user(&data, argp, sizeof(struct scu_ipc_data)))
  73. return -EFAULT;
  74. ret = scu_reg_access(cmd, &data);
  75. if (ret < 0)
  76. return ret;
  77. if (copy_to_user(argp, &data, sizeof(struct scu_ipc_data)))
  78. return -EFAULT;
  79. return 0;
  80. }
  81. static const struct file_operations scu_ipc_fops = {
  82. .unlocked_ioctl = scu_ipc_ioctl,
  83. };
  84. static int __init ipc_module_init(void)
  85. {
  86. major = register_chrdev(0, "intel_mid_scu", &scu_ipc_fops);
  87. if (major < 0)
  88. return major;
  89. return 0;
  90. }
  91. static void __exit ipc_module_exit(void)
  92. {
  93. unregister_chrdev(major, "intel_mid_scu");
  94. }
  95. module_init(ipc_module_init);
  96. module_exit(ipc_module_exit);
  97. MODULE_LICENSE("GPL v2");
  98. MODULE_DESCRIPTION("Utility driver for intel scu ipc");
  99. MODULE_AUTHOR("Sreedhara <sreedhara.ds@intel.com>");