adsp_factory.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2012, Samsung Electronics Co. Ltd. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <net/sock.h>
  19. #include <net/netlink.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netlink.h>
  22. #include <linux/mutex.h>
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include "adsp.h"
  26. /* The netlink socket */
  27. struct adsp_data *data;
  28. struct class *sensors_factory_class;
  29. static atomic_t sensor_count;
  30. DEFINE_MUTEX(factory_mutex);
  31. unsigned int raw_data_stream;
  32. EXPORT_SYMBOL(raw_data_stream);
  33. struct mutex raw_stream_lock;
  34. EXPORT_SYMBOL(raw_stream_lock);
  35. /* Function used to send message to the user space */
  36. int adsp_unicast(struct msg_data param, int message, int flags, u32 pid)
  37. {
  38. struct sk_buff *skb;
  39. struct nlmsghdr *nlh;
  40. struct msg_data *msg;
  41. int ret = -1;
  42. skb = nlmsg_new(sizeof(struct msg_data), GFP_KERNEL);
  43. nlh = nlmsg_put(skb, pid, 0, message, sizeof(struct msg_data), flags);
  44. if (nlh == NULL){
  45. nlmsg_free(skb);
  46. return -EMSGSIZE;
  47. }
  48. msg = nlmsg_data(nlh);
  49. msg->sensor_type = param.sensor_type;
  50. msg->param1 = param.param1;
  51. msg->param2 = param.param2;
  52. msg->param3 = param.param3;
  53. NETLINK_CB(skb).dst_group = 0;
  54. ret = nlmsg_unicast(data->adsp_skt, skb, PID);
  55. return ret;
  56. }
  57. static void set_sensor_attr(struct device *dev,
  58. struct device_attribute *attributes[])
  59. {
  60. int i;
  61. for (i = 0; attributes[i] != NULL; i++)
  62. if ((device_create_file(dev, attributes[i])) < 0)
  63. pr_err("[FACTORY] fail device_create_file"\
  64. "(dev, attributes[%d])\n", i);
  65. }
  66. #ifdef CONFIG_ADSP_FACTORY
  67. extern struct class* get_adsp_sensor_class(void);
  68. #endif
  69. int adsp_factory_register(char *dev_name, int type,
  70. struct device_attribute *attributes[], struct adsp_fac_ctl *fact_ctl)
  71. {
  72. int ret = 0;
  73. pr_err("[FACTORY] %s\n", __func__);
  74. data->ctl[type] = fact_ctl;
  75. #ifdef CONFIG_ADSP_FACTORY
  76. sensors_factory_class = get_adsp_sensor_class();
  77. #endif
  78. data->sensor_device[type] =
  79. device_create(sensors_factory_class,
  80. NULL, 0, data, "%s", dev_name);
  81. if (IS_ERR(data->sensor_device[type])) {
  82. ret = PTR_ERR(data->sensor_device[type]);
  83. pr_err("[FACTORY] %s: device_create failed![%d]\n",
  84. __func__, ret);
  85. return ret;
  86. }
  87. set_sensor_attr(data->sensor_device[type], attributes);
  88. atomic_inc(&sensor_count);
  89. data->ctl[type]->receive_data_fnc(data, CALLBACK_REGISTER_SUCCESS);
  90. return ret;
  91. }
  92. int adsp_factory_start_timer(const unsigned int ms)
  93. {
  94. return mod_timer(&data->command_timer, jiffies + msecs_to_jiffies(ms));
  95. }
  96. static int process_received_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  97. {
  98. switch (nlh->nlmsg_type) {
  99. case NETLINK_MESSAGE_RAW_DATA_RCVD:
  100. {
  101. struct sensor_value *pdata =
  102. (struct sensor_value*)NLMSG_DATA(nlh);
  103. data->sensor_data[pdata->sensor_type] = *pdata;
  104. if (raw_data_stream & 1 << pdata->sensor_type)
  105. data->data_ready_flag |= 1 << pdata->sensor_type;
  106. break;
  107. }
  108. case NETLINK_MESSAGE_REACTIVE_ALERT_RCVD:
  109. {
  110. struct sensor_value *pdata =
  111. (struct sensor_value*)NLMSG_DATA(nlh);
  112. pr_info("NETLINK_MESSAGE_REACTIVE_ALERT_RCVD type=%d,motion=%d\n",
  113. pdata->sensor_type, pdata->reactive_alert);
  114. data->reactive_alert = pdata->reactive_alert;
  115. if (data->reactive_alert != 4 && data->reactive_alert != 1)
  116. data->alert_ready_flag |= 1 << pdata->sensor_type;
  117. break;
  118. }
  119. case NETLINK_MESSAGE_CALIB_DATA_RCVD:
  120. {
  121. struct sensor_calib_value *pdata =
  122. (struct sensor_calib_value*)NLMSG_DATA(nlh);
  123. pr_info("NETLINK_MESSAGE_CALIB_DATA_RCVD type=%d,x=%d,y=%d,z=%d\n",
  124. pdata->sensor_type, pdata->x, pdata->y, pdata->z);
  125. data->sensor_calib_data[pdata->sensor_type] = *pdata;
  126. data->calib_ready_flag |= 1 << pdata->sensor_type;
  127. break;
  128. }
  129. case NETLINK_MESSAGE_CALIB_STORE_RCVD:
  130. {
  131. struct sensor_calib_store_result *pdata =
  132. (struct sensor_calib_store_result*)NLMSG_DATA(nlh);
  133. pr_info("NETLINK_MESSAGE_CALIB_STORE_RCVD type=%d,nCalibstoreresult=%d\n",
  134. pdata->sensor_type, pdata->nCalibstoreresult);
  135. data->sensor_calib_result[pdata->sensor_type] = *pdata;
  136. data->calib_store_ready_flag |= 1 << pdata->sensor_type;
  137. break;
  138. }
  139. case NETLINK_MESSAGE_SELFTEST_SHOW_RCVD:
  140. {
  141. struct sensor_selftest_show_result *pdata =
  142. (struct sensor_selftest_show_result*)NLMSG_DATA(nlh);
  143. pr_info("NETLINK_MESSAGE_SELFTEST_SHOW_RCVD type=%d, SelftestResult1=%d, SelftestResult2=%d\n",
  144. pdata->sensor_type, pdata->nSelftestresult1, pdata->nSelftestresult2);
  145. data->sensor_selftest_result[pdata->sensor_type] = *pdata;
  146. data->selftest_ready_flag |= 1 << pdata->sensor_type;
  147. break;
  148. }
  149. case NETLINK_MESSAGE_STOP_RAW_DATA:
  150. {
  151. struct sensor_stop_value *pdata =
  152. (struct sensor_stop_value*)NLMSG_DATA(nlh);
  153. pr_info("NETLINK_MESSAGE_STOP_RAW_DATA type=%d, StopResult=%d\n",
  154. pdata->sensor_type, pdata->result);
  155. data->sensor_stop_data[pdata->sensor_type] = *pdata;
  156. data->data_ready_flag &= 0 << pdata->sensor_type;
  157. break;
  158. }
  159. case NETLINK_MESSAGE_GYRO_TEMP_RCVD:
  160. {
  161. struct sensor_value *pdata =
  162. (struct sensor_value*)NLMSG_DATA(nlh);
  163. pr_info("NETLINK_MESSAGE_RAW_DATA_RCVD type=%d,x=%d,y=%d,z=%d\n",
  164. pdata->sensor_type, pdata->x, pdata->y, pdata->z);
  165. data->gyro_temp = pdata->temperature;
  166. data->data_ready |= ADSP_DATA_GYRO_TEMP;
  167. break;
  168. }
  169. default:
  170. break;
  171. }
  172. return 0;
  173. }
  174. static void factory_receive_skb(struct sk_buff *skb)
  175. {
  176. struct nlmsghdr *nlh;
  177. int len;
  178. int err;
  179. nlh = (struct nlmsghdr*)skb->data;
  180. len = skb->len;
  181. while (NLMSG_OK(nlh, len)) {
  182. err = process_received_msg(skb, nlh);
  183. /* if err or if this message says it wants a response */
  184. if (err || (nlh->nlmsg_flags & NLM_F_ACK))
  185. netlink_ack(skb, nlh, err);
  186. nlh = NLMSG_NEXT(nlh, len);
  187. }
  188. }
  189. static void factory_adsp_command_timer(unsigned long value)
  190. {
  191. int i;
  192. for (i = 0; i < ADSP_FACTORY_SENSOR_MAX; i++) {
  193. if (data->ctl[i])
  194. data->ctl[i]->receive_data_fnc(data,
  195. CALLBACK_TIMER_EXPIRED);
  196. }
  197. }
  198. /* Receive messages from netlink socket. */
  199. static void factory_test_result_receive(struct sk_buff *skb)
  200. {
  201. mutex_lock(&factory_mutex);
  202. factory_receive_skb(skb);
  203. mutex_unlock(&factory_mutex);
  204. }
  205. static int __devinit factory_adsp_init(void)
  206. {
  207. pr_info("[FACTORY] %s\n", __func__);
  208. data = kzalloc(sizeof(*data), GFP_KERNEL);
  209. data->adsp_skt = netlink_kernel_create(&init_net, NETLINK_ADSP_FAC, 0,
  210. factory_test_result_receive, NULL, THIS_MODULE);
  211. data->data_ready_flag = 0;
  212. data->calib_ready_flag = 0;
  213. data->calib_store_ready_flag = 0;
  214. data->selftest_ready_flag = 0;
  215. init_timer(&data->command_timer);
  216. data->command_timer.function = factory_adsp_command_timer;
  217. data->command_timer.data = (unsigned int)NULL;
  218. data->command_timer.expires = 0xffffffffL;
  219. add_timer(&data->command_timer);
  220. mutex_init(&raw_stream_lock);
  221. pr_err("[FACTORY] %s: Timer Init \n", __func__);
  222. return 0;
  223. }
  224. module_init(factory_adsp_init);
  225. static void __devexit factory_adsp_exit(void)
  226. {
  227. del_timer(&data->command_timer);
  228. mutex_destroy(&raw_stream_lock);
  229. pr_info("[FACTORY] %s\n", __func__);
  230. }
  231. module_exit(factory_adsp_exit);
  232. MODULE_DESCRIPTION("Support for factory test sensors (adsp)");
  233. MODULE_LICENSE("GPL");