mag_factory.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "inc/mag_factory.h"
  2. static int mag_factory_open(struct inode *inode, struct file *file)
  3. {
  4. file->private_data = mag_context_obj;
  5. if (file->private_data == NULL) {
  6. MAG_ERR("null pointer!!\n");
  7. return -EINVAL;
  8. }
  9. return nonseekable_open(inode, file);
  10. }
  11. static int mag_factory_release(struct inode *inode, struct file *file)
  12. {
  13. file->private_data = NULL;
  14. return 0;
  15. }
  16. static long mag_factory_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  17. {
  18. void __user *data;
  19. long err = 0;
  20. struct mag_context *cxt = mag_context_obj;
  21. int x, y, z, status;
  22. char strbuf[256];
  23. if (_IOC_DIR(cmd) & _IOC_READ)
  24. err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  25. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  26. err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  27. if (err) {
  28. MAG_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  29. return -EFAULT;
  30. }
  31. switch (cmd) {
  32. case MSENSOR_IOCTL_SENSOR_ENABLE:
  33. if (cxt->mag_ctl.m_enable != NULL) {
  34. err = cxt->mag_ctl.m_enable(1);
  35. if (err < 0) {
  36. MAG_ERR("MSENSOR_IOCTL_SENSOR_ENABLE read data fail!\n");
  37. break;
  38. }
  39. }
  40. break;
  41. case MSENSOR_IOCTL_READ_SENSORDATA:
  42. data = (void __user *) arg;
  43. if (data == NULL) {
  44. err = -EINVAL;
  45. break;
  46. }
  47. if (cxt->mag_dev_data.get_data_m != NULL) {
  48. err = cxt->mag_dev_data.get_raw_data(&x, &y, &z);
  49. if (err < 0) {
  50. MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA read data fail!\n");
  51. break;
  52. }
  53. sprintf(strbuf, "%x %x %x", x, y, z);
  54. MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA read data : (%d, %d, %d)!\n", x, y, z);
  55. MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA read strbuf : (%s)!\n", strbuf);
  56. if (copy_to_user(data, strbuf, strlen(strbuf)+1)) {
  57. err = -EFAULT;
  58. break;
  59. }
  60. } else
  61. MAG_ERR("MSENSOR_IOCTL_READ_SENSORDATA NULL!\n");
  62. break;
  63. case MSENSOR_IOCTL_READ_FACTORY_SENSORDATA:
  64. data = (void __user *) arg;
  65. if (data == NULL) {
  66. err = -EINVAL;
  67. break;
  68. }
  69. if (cxt->mag_dev_data.get_data_o != NULL) {
  70. err = cxt->mag_dev_data.get_data_o(&x, &y, &z, &status);
  71. if (err < 0) {
  72. MAG_ERR("MSENSOR_IOCTL_READ_FACTORY_SENSORDATA read data fail!\n");
  73. break;
  74. }
  75. sprintf(strbuf, "%x %x %x %x %x", x, y, z, status, 1);
  76. MAG_ERR("MSENSOR_IOCTL_READ_FACTORY_SENSORDATA read data : (%d, %d, %d, %d)!\n",
  77. x, y, z, status);
  78. if (copy_to_user(data, strbuf, strlen(strbuf)+1)) {
  79. err = -EFAULT;
  80. break;
  81. }
  82. } else
  83. MAG_ERR("MSENSOR_IOCTL_READ_FACTORY_SENSORDATA NULL!\n ");
  84. break;
  85. default:
  86. MAG_ERR("unknown IOCTL: 0x%08x\n", cmd);
  87. err = -ENOIOCTLCMD;
  88. break;
  89. }
  90. return err;
  91. }
  92. #if IS_ENABLED(CONFIG_COMPAT)
  93. static long compat_mag_factory_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  94. {
  95. if (!filp->f_op || !filp->f_op->unlocked_ioctl) {
  96. MAG_ERR("compat_ion_ioctl file has no f_op or no f_op->unlocked_ioctl.\n");
  97. return -ENOTTY;
  98. }
  99. switch (cmd) {
  100. /* case COMPAT_MSENSOR_IOCTL_INIT:
  101. case COMPAT_MSENSOR_IOCTL_SET_POSTURE:
  102. case COMPAT_MSENSOR_IOCTL_SET_CALIDATA:
  103. case COMPAT_MSENSOR_IOCTL_READ_CHIPINFO: */
  104. case COMPAT_MSENSOR_IOCTL_READ_SENSORDATA:
  105. /* case COMPAT_MSENSOR_IOCTL_READ_POSTUREDATA:
  106. case COMPAT_MSENSOR_IOCTL_READ_CALIDATA:
  107. case COMPAT_MSENSOR_IOCTL_READ_CONTROL:
  108. case COMPAT_MSENSOR_IOCTL_SET_CONTROL:
  109. case COMPAT_MSENSOR_IOCTL_SET_MODE: */
  110. case COMPAT_MSENSOR_IOCTL_SENSOR_ENABLE:
  111. case COMPAT_MSENSOR_IOCTL_READ_FACTORY_SENSORDATA: {
  112. MAG_LOG("compat_ion_ioctl : MSENSOR_IOCTL_XXX command is 0x%x\n", cmd);
  113. return filp->f_op->unlocked_ioctl(filp, cmd,
  114. (unsigned long)compat_ptr(arg));
  115. }
  116. default: {
  117. MAG_ERR("compat_ion_ioctl : No such command!! 0x%x\n", cmd);
  118. return -ENOIOCTLCMD;
  119. }
  120. }
  121. }
  122. #endif
  123. /*----------------------------------------------------------------------------*/
  124. static const struct file_operations mag_factory_fops = {
  125. .open = mag_factory_open,
  126. .release = mag_factory_release,
  127. .unlocked_ioctl = mag_factory_unlocked_ioctl,
  128. #if IS_ENABLED(CONFIG_COMPAT)
  129. .compat_ioctl = compat_mag_factory_unlocked_ioctl,
  130. #endif
  131. };
  132. static struct miscdevice mag_factory_device = {
  133. .minor = MISC_DYNAMIC_MINOR,
  134. .name = "msensor",
  135. .fops = &mag_factory_fops,
  136. };
  137. int mag_factory_device_init(void)
  138. {
  139. int error = 0;
  140. struct mag_context *cxt = mag_context_obj;
  141. if (!cxt->mag_ctl.is_use_common_factory) {
  142. MAG_LOG("Node of '/dev/msensor' has already existed!\n");
  143. return -1;
  144. }
  145. error = misc_register(&mag_factory_device);
  146. if (error) {
  147. MAG_ERR("mag_factory_device register failed\n");
  148. error = -1;
  149. }
  150. return error;
  151. }