alsps_factory.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "inc/alsps_factory.h"
  2. static int alsps_factory_open(struct inode *inode, struct file *file)
  3. {
  4. file->private_data = alsps_context_obj;
  5. if (file->private_data == NULL) {
  6. ALSPS_ERR("null pointer!!\n");
  7. return -EINVAL;
  8. }
  9. return nonseekable_open(inode, file);
  10. }
  11. static int alsps_factory_release(struct inode *inode, struct file *file)
  12. {
  13. file->private_data = NULL;
  14. return 0;
  15. }
  16. static long alsps_factory_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  17. {
  18. long err = 0;
  19. struct alsps_context *cxt = alsps_context_obj;
  20. void __user *ptr = (void __user *) arg;
  21. int dat = 0;
  22. uint32_t enable = 0;
  23. int ps_cali = 0;
  24. int threshold_data[2];
  25. if (_IOC_DIR(cmd) & _IOC_READ)
  26. err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  27. else if (_IOC_DIR(cmd) & _IOC_WRITE)
  28. err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  29. if (err) {
  30. ALSPS_LOG("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
  31. return -EFAULT;
  32. }
  33. switch (cmd) {
  34. case ALSPS_SET_PS_MODE:
  35. if (copy_from_user(&enable, ptr, sizeof(enable))) {
  36. err = -EFAULT;
  37. break;
  38. }
  39. ALSPS_LOG("ALSPS_SET_PS_MODE enable: %d!\n", enable);
  40. if (cxt->ps_ctl.enable_nodata != NULL) {
  41. err = cxt->ps_ctl.enable_nodata(enable);
  42. if (err < 0) {
  43. ALSPS_LOG("ALSPS_SET_PS_MODE fail!\n");
  44. break;
  45. }
  46. }
  47. break;
  48. case ALSPS_GET_PS_RAW_DATA:
  49. if (cxt->ps_data.ps_get_raw_data != NULL) {
  50. err = cxt->ps_data.ps_get_raw_data(&dat);
  51. if (err < 0) {
  52. ALSPS_LOG("ALSPS_GET_PS_RAW_DATA fail!\n");
  53. break;
  54. }
  55. }
  56. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  57. err = -EFAULT;
  58. break;
  59. }
  60. break;
  61. case ALSPS_SET_ALS_MODE:
  62. if (copy_from_user(&enable, ptr, sizeof(enable))) {
  63. err = -EFAULT;
  64. break;
  65. }
  66. ALSPS_LOG("ALSPS_SET_ALS_MODE enable: %d!\n", enable);
  67. if (cxt->als_ctl.enable_nodata != NULL) {
  68. err = cxt->als_ctl.enable_nodata(enable);
  69. if (err < 0) {
  70. ALSPS_LOG("ALSPS_SET_ALS_MODE fail!\n");
  71. break;
  72. }
  73. }
  74. break;
  75. case ALSPS_GET_ALS_RAW_DATA:
  76. if (cxt->als_data.als_get_raw_data != NULL) {
  77. err = cxt->als_data.als_get_raw_data(&dat);
  78. if (err < 0) {
  79. ALSPS_LOG("ALSPS_GET_ALS_RAW_DATA fail!\n");
  80. break;
  81. }
  82. }
  83. if (copy_to_user(ptr, &dat, sizeof(dat))) {
  84. err = -EFAULT;
  85. break;
  86. }
  87. break;
  88. case ALSPS_GET_PS_TEST_RESULT:
  89. if (cxt->ps_ctl.ps_threshold_setting != NULL) {
  90. err = cxt->ps_ctl.ps_threshold_setting(GET_TH_RESULT, threshold_data);
  91. if (err < 0) {
  92. ALSPS_LOG("ALSPS_GET_PS_TEST_RESULT fail!\n");
  93. break;
  94. }
  95. }
  96. if (copy_to_user(ptr, &threshold_data[0], sizeof(threshold_data[0]))) {
  97. err = -EFAULT;
  98. break;
  99. }
  100. break;
  101. case ALSPS_GET_PS_THRESHOLD_HIGH:
  102. if (cxt->ps_ctl.ps_threshold_setting != NULL) {
  103. err = cxt->ps_ctl.ps_threshold_setting(GET_TH_HIGH, threshold_data);
  104. if (err < 0) {
  105. ALSPS_LOG("ALSPS_GET_PS_THRESHOLD_HIGH fail!\n");
  106. break;
  107. }
  108. }
  109. if (copy_to_user(ptr, &threshold_data[0], sizeof(threshold_data[0]))) {
  110. err = -EFAULT;
  111. break;
  112. }
  113. break;
  114. case ALSPS_GET_PS_THRESHOLD_LOW:
  115. if (cxt->ps_ctl.ps_threshold_setting != NULL) {
  116. err = cxt->ps_ctl.ps_threshold_setting(GET_TH_LOW, threshold_data);
  117. if (err < 0) {
  118. ALSPS_LOG("ALSPS_GET_PS_THRESHOLD_LOW fail!\n");
  119. break;
  120. }
  121. }
  122. if (copy_to_user(ptr, &threshold_data[0], sizeof(threshold_data[0]))) {
  123. err = -EFAULT;
  124. break;
  125. }
  126. break;
  127. case ALSPS_SET_PS_THRESHOLD:
  128. if (copy_from_user(threshold_data, ptr, sizeof(threshold_data))) {
  129. err = -EFAULT;
  130. break;
  131. }
  132. if (cxt->ps_ctl.ps_threshold_setting != NULL) {
  133. err = cxt->ps_ctl.ps_threshold_setting(SET_TH, threshold_data);
  134. if (err < 0) {
  135. ALSPS_LOG("ALSPS_GET_PS_THRESHOLD_LOW fail!\n");
  136. break;
  137. }
  138. }
  139. break;
  140. case ALSPS_IOCTL_SET_CALI:
  141. if (copy_from_user(&ps_cali, ptr, sizeof(ps_cali))) {
  142. err = -EFAULT;
  143. break;
  144. }
  145. if (cxt->ps_ctl.ps_calibration != NULL) {
  146. err = cxt->ps_ctl.ps_calibration(SETCALI, ps_cali);
  147. if (err < 0) {
  148. ALSPS_LOG("ALSPS_IOCTL_SET_CALI fail!\n");
  149. break;
  150. }
  151. }
  152. break;
  153. case ALSPS_IOCTL_GET_CALI:
  154. if (cxt->ps_ctl.ps_calibration != NULL) {
  155. err = cxt->ps_ctl.ps_calibration(GETCALI, ps_cali);
  156. if (err < 0) {
  157. ALSPS_LOG("ALSPS_IOCTL_GET_CALI fail!\n");
  158. break;
  159. }
  160. }
  161. if (copy_to_user(ptr, &ps_cali, sizeof(ps_cali))) {
  162. err = -EFAULT;
  163. break;
  164. }
  165. break;
  166. case ALSPS_IOCTL_CLR_CALI:
  167. if (copy_from_user(&dat, ptr, sizeof(dat))) {
  168. err = -EFAULT;
  169. break;
  170. }
  171. if (cxt->ps_ctl.ps_calibration != NULL) {
  172. err = cxt->ps_ctl.ps_calibration(CLRCALI, dat);
  173. if (err < 0) {
  174. ALSPS_LOG("ALSPS_IOCTL_CLR_CALI fail!\n");
  175. break;
  176. }
  177. }
  178. break;
  179. default:
  180. ALSPS_ERR("unknown IOCTL: 0x%08x\n", cmd);
  181. err = -ENOIOCTLCMD;
  182. break;
  183. }
  184. return err;
  185. }
  186. static const struct file_operations alsps_factory_fops = {
  187. .open = alsps_factory_open,
  188. .release = alsps_factory_release,
  189. .unlocked_ioctl = alsps_factory_unlocked_ioctl,
  190. };
  191. static struct miscdevice alsps_factory_device = {
  192. .minor = MISC_DYNAMIC_MINOR,
  193. .name = "als_ps",
  194. .fops = &alsps_factory_fops,
  195. };
  196. int alsps_factory_device_init(void)
  197. {
  198. int error = 0;
  199. struct alsps_context *cxt = alsps_context_obj;
  200. if (!cxt->als_ctl.is_use_common_factory && !cxt->ps_ctl.is_use_common_factory) {
  201. ALSPS_LOG("Node of '/dev/als_ps' has already existed!\n");
  202. return -1;
  203. }
  204. error = misc_register(&alsps_factory_device);
  205. if (error) {
  206. ALSPS_LOG("alsps_factory_device register failed\n");
  207. error = -1;
  208. }
  209. return error;
  210. }