barometer_factory.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "inc/barometer_factory.h"
  2. static int baro_factory_open(struct inode *inode, struct file *file)
  3. {
  4. file->private_data = baro_context_obj;
  5. if (file->private_data == NULL) {
  6. BARO_ERR("null pointer!!\n");
  7. return -EINVAL;
  8. }
  9. return nonseekable_open(inode, file);
  10. }
  11. static int baro_factory_release(struct inode *inode, struct file *file)
  12. {
  13. file->private_data = NULL;
  14. return 0;
  15. }
  16. static long baro_factory_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  17. {
  18. /* void __user *data; */
  19. long err = 0;
  20. struct baro_context *cxt = baro_context_obj;
  21. void __user *ptr = (void __user *)arg;
  22. int dat;
  23. /* uint32_t enable = 0; */
  24. /* int ps_cali; */
  25. /* int threshold_data[2]; */
  26. if (_IOC_DIR(cmd) & _IOC_READ)
  27. err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  28. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  29. err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  30. if (err) {
  31. BARO_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  32. return -EFAULT;
  33. }
  34. switch (cmd) {
  35. case BAROMETER_IOCTL_INIT:
  36. BARO_LOG("BAROMETER_IOCTL_INIT enable\n");
  37. if (cxt->baro_ctl.enable_nodata != NULL) {
  38. err = cxt->baro_ctl.enable_nodata(1);
  39. if (err < 0) {
  40. BARO_ERR("BAROMETER_IOCTL_INIT fail!\n");
  41. break;
  42. }
  43. }
  44. break;
  45. case BAROMETER_GET_PRESS_DATA:
  46. if (cxt->baro_data.get_raw_data != NULL) {
  47. err = cxt->baro_data.get_raw_data(TYPE_PRESS, &dat);
  48. if (err < 0) {
  49. BARO_ERR("BAROMETER_GET_PRESS_DATA fail!\n");
  50. break;
  51. }
  52. }
  53. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  54. err = -EFAULT;
  55. break;
  56. }
  57. break;
  58. case BAROMETER_GET_TEMP_DATA:
  59. if (cxt->baro_data.get_raw_data != NULL) {
  60. err = cxt->baro_data.get_raw_data(TYPE_TEMP, &dat);
  61. if (err < 0) {
  62. BARO_ERR("BAROMETER_GET_PRESS_DATA fail!\n");
  63. break;
  64. }
  65. }
  66. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  67. err = -EFAULT;
  68. break;
  69. }
  70. break;
  71. default:
  72. BARO_ERR("unknown IOCTL: 0x%08x\n", cmd);
  73. err = -ENOIOCTLCMD;
  74. break;
  75. }
  76. return err;
  77. }
  78. #if IS_ENABLED(CONFIG_COMPAT)
  79. static long compat_baro_factory_unlocked_ioctl(struct file *filp, unsigned int cmd,
  80. unsigned long arg)
  81. {
  82. if (!filp->f_op || !filp->f_op->unlocked_ioctl) {
  83. BARO_ERR("compat_ion_ioctl file has no f_op or no f_op->unlocked_ioctl.\n");
  84. return -ENOTTY;
  85. }
  86. switch (cmd) {
  87. case COMPAT_BAROMETER_IOCTL_INIT:
  88. /* case COMPAT_BAROMETER_IOCTL_READ_CHIPINFO: */
  89. case COMPAT_BAROMETER_GET_PRESS_DATA:
  90. case COMPAT_BAROMETER_GET_TEMP_DATA: {
  91. BARO_LOG("compat_ion_ioctl : BAROMETER_IOCTL_XXX command is 0x%x\n", cmd);
  92. return filp->f_op->unlocked_ioctl(filp, cmd,
  93. (unsigned long)compat_ptr(arg));
  94. }
  95. default:
  96. BARO_ERR("compat_ion_ioctl : No such command!! 0x%x\n", cmd);
  97. return -ENOIOCTLCMD;
  98. }
  99. }
  100. #endif
  101. /*----------------------------------------------------------------------------*/
  102. static const struct file_operations baro_factory_fops = {
  103. .open = baro_factory_open,
  104. .release = baro_factory_release,
  105. .unlocked_ioctl = baro_factory_unlocked_ioctl,
  106. #if IS_ENABLED(CONFIG_COMPAT)
  107. .compat_ioctl = compat_baro_factory_unlocked_ioctl,
  108. #endif
  109. };
  110. static struct miscdevice baro_factory_device = {
  111. .minor = MISC_DYNAMIC_MINOR,
  112. .name = "barometer",
  113. .fops = &baro_factory_fops,
  114. };
  115. int baro_factory_device_init(void)
  116. {
  117. int error = 0;
  118. struct baro_context *cxt = baro_context_obj;
  119. if (!cxt->baro_ctl.is_use_common_factory) {
  120. BARO_LOG("Node of '/dev/barometer' has already existed!\n");
  121. return -1;
  122. }
  123. error = misc_register(&baro_factory_device);
  124. if (error) {
  125. BARO_ERR("baro_factory_device register failed\n");
  126. error = -1;
  127. }
  128. return error;
  129. }