intel_scu_ipcutil.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 u32 major;
  26. #define MAX_FW_SIZE 264192
  27. /* ioctl commnds */
  28. #define INTE_SCU_IPC_REGISTER_READ 0
  29. #define INTE_SCU_IPC_REGISTER_WRITE 1
  30. #define INTE_SCU_IPC_REGISTER_UPDATE 2
  31. #define INTE_SCU_IPC_FW_UPDATE 0xA2
  32. struct scu_ipc_data {
  33. u32 count; /* No. of registers */
  34. u16 addr[5]; /* Register addresses */
  35. u8 data[5]; /* Register data */
  36. u8 mask; /* Valid for read-modify-write */
  37. };
  38. /**
  39. * scu_reg_access - implement register access ioctls
  40. * @cmd: command we are doing (read/write/update)
  41. * @data: kernel copy of ioctl data
  42. *
  43. * Allow the user to perform register accesses on the SCU via the
  44. * kernel interface
  45. */
  46. static int scu_reg_access(u32 cmd, struct scu_ipc_data *data)
  47. {
  48. int count = data->count;
  49. if (count == 0 || count == 3 || count > 4)
  50. return -EINVAL;
  51. switch (cmd) {
  52. case INTE_SCU_IPC_REGISTER_READ:
  53. return intel_scu_ipc_readv(data->addr, data->data, count);
  54. case INTE_SCU_IPC_REGISTER_WRITE:
  55. return intel_scu_ipc_writev(data->addr, data->data, count);
  56. case INTE_SCU_IPC_REGISTER_UPDATE:
  57. return intel_scu_ipc_update_register(data->addr[0],
  58. data->data[0], data->mask);
  59. default:
  60. return -ENOTTY;
  61. }
  62. }
  63. /**
  64. * scu_ipc_ioctl - control ioctls for the SCU
  65. * @fp: file handle of the SCU device
  66. * @cmd: ioctl coce
  67. * @arg: pointer to user passed structure
  68. *
  69. * Support the I/O and firmware flashing interfaces of the SCU
  70. */
  71. static long scu_ipc_ioctl(struct file *fp, unsigned int cmd,
  72. unsigned long arg)
  73. {
  74. int ret;
  75. struct scu_ipc_data data;
  76. void __user *argp = (void __user *)arg;
  77. if (!capable(CAP_SYS_RAWIO))
  78. return -EPERM;
  79. if (cmd == INTE_SCU_IPC_FW_UPDATE) {
  80. u8 *fwbuf = kmalloc(MAX_FW_SIZE, GFP_KERNEL);
  81. if (fwbuf == NULL)
  82. return -ENOMEM;
  83. if (copy_from_user(fwbuf, (u8 *)arg, MAX_FW_SIZE)) {
  84. kfree(fwbuf);
  85. return -EFAULT;
  86. }
  87. ret = intel_scu_ipc_fw_update(fwbuf, MAX_FW_SIZE);
  88. kfree(fwbuf);
  89. return ret;
  90. } else {
  91. if (copy_from_user(&data, argp, sizeof(struct scu_ipc_data)))
  92. return -EFAULT;
  93. ret = scu_reg_access(cmd, &data);
  94. if (ret < 0)
  95. return ret;
  96. if (copy_to_user(argp, &data, sizeof(struct scu_ipc_data)))
  97. return -EFAULT;
  98. return 0;
  99. }
  100. }
  101. static const struct file_operations scu_ipc_fops = {
  102. .unlocked_ioctl = scu_ipc_ioctl,
  103. };
  104. static int __init ipc_module_init(void)
  105. {
  106. return register_chrdev(0, "intel_mid_scu", &scu_ipc_fops);
  107. }
  108. static void __exit ipc_module_exit(void)
  109. {
  110. unregister_chrdev(major, "intel_mid_scu");
  111. }
  112. module_init(ipc_module_init);
  113. module_exit(ipc_module_exit);
  114. MODULE_LICENSE("GPL v2");
  115. MODULE_DESCRIPTION("Utility driver for intel scu ipc");
  116. MODULE_AUTHOR("Sreedhara <sreedhara.ds@intel.com>");