intel_scu_ipcutil.c 3.0 KB

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