hid-sensor-custom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * hid-sensor-custom.c
  3. * Copyright (c) 2015, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/kfifo.h>
  19. #include <linux/sched.h>
  20. #include <linux/wait.h>
  21. #include <linux/poll.h>
  22. #include <linux/bsearch.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/hid-sensor-hub.h>
  25. #define HID_CUSTOM_NAME_LENGTH 64
  26. #define HID_CUSTOM_MAX_CORE_ATTRS 10
  27. #define HID_CUSTOM_TOTAL_ATTRS (HID_CUSTOM_MAX_CORE_ATTRS + 1)
  28. #define HID_CUSTOM_FIFO_SIZE 4096
  29. #define HID_CUSTOM_MAX_FEATURE_BYTES 64
  30. struct hid_sensor_custom_field {
  31. int report_id;
  32. char group_name[HID_CUSTOM_NAME_LENGTH];
  33. struct hid_sensor_hub_attribute_info attribute;
  34. struct device_attribute sd_attrs[HID_CUSTOM_MAX_CORE_ATTRS];
  35. char attr_name[HID_CUSTOM_TOTAL_ATTRS][HID_CUSTOM_NAME_LENGTH];
  36. struct attribute *attrs[HID_CUSTOM_TOTAL_ATTRS];
  37. struct attribute_group hid_custom_attribute_group;
  38. };
  39. struct hid_sensor_custom {
  40. struct mutex mutex;
  41. struct platform_device *pdev;
  42. struct hid_sensor_hub_device *hsdev;
  43. struct hid_sensor_hub_callbacks callbacks;
  44. int sensor_field_count;
  45. struct hid_sensor_custom_field *fields;
  46. int input_field_count;
  47. int input_report_size;
  48. int input_report_recd_size;
  49. bool input_skip_sample;
  50. bool enable;
  51. struct hid_sensor_custom_field *power_state;
  52. struct hid_sensor_custom_field *report_state;
  53. struct miscdevice custom_dev;
  54. struct kfifo data_fifo;
  55. unsigned long misc_opened;
  56. wait_queue_head_t wait;
  57. };
  58. /* Header for each sample to user space via dev interface */
  59. struct hid_sensor_sample {
  60. u32 usage_id;
  61. u64 timestamp;
  62. u32 raw_len;
  63. } __packed;
  64. static struct attribute hid_custom_attrs[] = {
  65. {.name = "name", .mode = S_IRUGO},
  66. {.name = "units", .mode = S_IRUGO},
  67. {.name = "unit-expo", .mode = S_IRUGO},
  68. {.name = "minimum", .mode = S_IRUGO},
  69. {.name = "maximum", .mode = S_IRUGO},
  70. {.name = "size", .mode = S_IRUGO},
  71. {.name = "value", .mode = S_IWUSR | S_IRUGO},
  72. {.name = NULL}
  73. };
  74. static const struct hid_custom_usage_desc {
  75. int usage_id;
  76. char *desc;
  77. } hid_custom_usage_desc_table[] = {
  78. {0x200201, "event-sensor-state"},
  79. {0x200202, "event-sensor-event"},
  80. {0x200301, "property-friendly-name"},
  81. {0x200302, "property-persistent-unique-id"},
  82. {0x200303, "property-sensor-status"},
  83. {0x200304, "property-min-report-interval"},
  84. {0x200305, "property-sensor-manufacturer"},
  85. {0x200306, "property-sensor-model"},
  86. {0x200307, "property-sensor-serial-number"},
  87. {0x200308, "property-sensor-description"},
  88. {0x200309, "property-sensor-connection-type"},
  89. {0x20030A, "property-sensor-device-path"},
  90. {0x20030B, "property-hardware-revision"},
  91. {0x20030C, "property-firmware-version"},
  92. {0x20030D, "property-release-date"},
  93. {0x20030E, "property-report-interval"},
  94. {0x20030F, "property-change-sensitivity-absolute"},
  95. {0x200310, "property-change-sensitivity-percent-range"},
  96. {0x200311, "property-change-sensitivity-percent-relative"},
  97. {0x200312, "property-accuracy"},
  98. {0x200313, "property-resolution"},
  99. {0x200314, "property-maximum"},
  100. {0x200315, "property-minimum"},
  101. {0x200316, "property-reporting-state"},
  102. {0x200317, "property-sampling-rate"},
  103. {0x200318, "property-response-curve"},
  104. {0x200319, "property-power-state"},
  105. {0x200540, "data-field-custom"},
  106. {0x200541, "data-field-custom-usage"},
  107. {0x200542, "data-field-custom-boolean-array"},
  108. {0x200543, "data-field-custom-value"},
  109. {0x200544, "data-field-custom-value_1"},
  110. {0x200545, "data-field-custom-value_2"},
  111. {0x200546, "data-field-custom-value_3"},
  112. {0x200547, "data-field-custom-value_4"},
  113. {0x200548, "data-field-custom-value_5"},
  114. {0x200549, "data-field-custom-value_6"},
  115. {0x20054A, "data-field-custom-value_7"},
  116. {0x20054B, "data-field-custom-value_8"},
  117. {0x20054C, "data-field-custom-value_9"},
  118. {0x20054D, "data-field-custom-value_10"},
  119. {0x20054E, "data-field-custom-value_11"},
  120. {0x20054F, "data-field-custom-value_12"},
  121. {0x200550, "data-field-custom-value_13"},
  122. {0x200551, "data-field-custom-value_14"},
  123. {0x200552, "data-field-custom-value_15"},
  124. {0x200553, "data-field-custom-value_16"},
  125. {0x200554, "data-field-custom-value_17"},
  126. {0x200555, "data-field-custom-value_18"},
  127. {0x200556, "data-field-custom-value_19"},
  128. {0x200557, "data-field-custom-value_20"},
  129. {0x200558, "data-field-custom-value_21"},
  130. {0x200559, "data-field-custom-value_22"},
  131. {0x20055A, "data-field-custom-value_23"},
  132. {0x20055B, "data-field-custom-value_24"},
  133. {0x20055C, "data-field-custom-value_25"},
  134. {0x20055D, "data-field-custom-value_26"},
  135. {0x20055E, "data-field-custom-value_27"},
  136. {0x20055F, "data-field-custom-value_28"},
  137. };
  138. static int usage_id_cmp(const void *p1, const void *p2)
  139. {
  140. if (*(int *)p1 < *(int *)p2)
  141. return -1;
  142. if (*(int *)p1 > *(int *)p2)
  143. return 1;
  144. return 0;
  145. }
  146. static ssize_t enable_sensor_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct platform_device *pdev = to_platform_device(dev);
  150. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
  151. return sprintf(buf, "%d\n", sensor_inst->enable);
  152. }
  153. static int set_power_report_state(struct hid_sensor_custom *sensor_inst,
  154. bool state)
  155. {
  156. int power_val = -1;
  157. int report_val = -1;
  158. u32 power_state_usage_id;
  159. u32 report_state_usage_id;
  160. int ret;
  161. /*
  162. * It is possible that the power/report state ids are not present.
  163. * In this case this function will return success. But if the
  164. * ids are present, then it will return error if set fails.
  165. */
  166. if (state) {
  167. power_state_usage_id =
  168. HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM;
  169. report_state_usage_id =
  170. HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM;
  171. } else {
  172. power_state_usage_id =
  173. HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM;
  174. report_state_usage_id =
  175. HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM;
  176. }
  177. if (sensor_inst->power_state)
  178. power_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
  179. sensor_inst->power_state->attribute.report_id,
  180. sensor_inst->power_state->attribute.index,
  181. power_state_usage_id);
  182. if (sensor_inst->report_state)
  183. report_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
  184. sensor_inst->report_state->attribute.report_id,
  185. sensor_inst->report_state->attribute.index,
  186. report_state_usage_id);
  187. if (power_val >= 0) {
  188. power_val +=
  189. sensor_inst->power_state->attribute.logical_minimum;
  190. ret = sensor_hub_set_feature(sensor_inst->hsdev,
  191. sensor_inst->power_state->attribute.report_id,
  192. sensor_inst->power_state->attribute.index,
  193. sizeof(power_val),
  194. &power_val);
  195. if (ret) {
  196. hid_err(sensor_inst->hsdev->hdev,
  197. "Set power state failed\n");
  198. return ret;
  199. }
  200. }
  201. if (report_val >= 0) {
  202. report_val +=
  203. sensor_inst->report_state->attribute.logical_minimum;
  204. ret = sensor_hub_set_feature(sensor_inst->hsdev,
  205. sensor_inst->report_state->attribute.report_id,
  206. sensor_inst->report_state->attribute.index,
  207. sizeof(report_val),
  208. &report_val);
  209. if (ret) {
  210. hid_err(sensor_inst->hsdev->hdev,
  211. "Set report state failed\n");
  212. return ret;
  213. }
  214. }
  215. return 0;
  216. }
  217. static ssize_t enable_sensor_store(struct device *dev,
  218. struct device_attribute *attr,
  219. const char *buf, size_t count)
  220. {
  221. struct platform_device *pdev = to_platform_device(dev);
  222. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
  223. int value;
  224. int ret = -EINVAL;
  225. if (kstrtoint(buf, 0, &value) != 0)
  226. return -EINVAL;
  227. mutex_lock(&sensor_inst->mutex);
  228. if (value && !sensor_inst->enable) {
  229. ret = sensor_hub_device_open(sensor_inst->hsdev);
  230. if (ret)
  231. goto unlock_state;
  232. ret = set_power_report_state(sensor_inst, true);
  233. if (ret) {
  234. sensor_hub_device_close(sensor_inst->hsdev);
  235. goto unlock_state;
  236. }
  237. sensor_inst->enable = true;
  238. } else if (!value && sensor_inst->enable) {
  239. ret = set_power_report_state(sensor_inst, false);
  240. sensor_hub_device_close(sensor_inst->hsdev);
  241. sensor_inst->enable = false;
  242. }
  243. unlock_state:
  244. mutex_unlock(&sensor_inst->mutex);
  245. if (ret < 0)
  246. return ret;
  247. return count;
  248. }
  249. static DEVICE_ATTR_RW(enable_sensor);
  250. static struct attribute *enable_sensor_attrs[] = {
  251. &dev_attr_enable_sensor.attr,
  252. NULL,
  253. };
  254. static struct attribute_group enable_sensor_attr_group = {
  255. .attrs = enable_sensor_attrs,
  256. };
  257. static ssize_t show_value(struct device *dev, struct device_attribute *attr,
  258. char *buf)
  259. {
  260. struct platform_device *pdev = to_platform_device(dev);
  261. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
  262. struct hid_sensor_hub_attribute_info *attribute;
  263. int index, usage, field_index;
  264. char name[HID_CUSTOM_NAME_LENGTH];
  265. bool feature = false;
  266. bool input = false;
  267. int value = 0;
  268. if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
  269. name) == 3) {
  270. feature = true;
  271. field_index = index + sensor_inst->input_field_count;
  272. } else if (sscanf(attr->attr.name, "input-%x-%x-%s", &index, &usage,
  273. name) == 3) {
  274. input = true;
  275. field_index = index;
  276. } else
  277. return -EINVAL;
  278. if (!strncmp(name, "value", strlen("value"))) {
  279. u32 report_id;
  280. int ret;
  281. attribute = &sensor_inst->fields[field_index].attribute;
  282. report_id = attribute->report_id;
  283. if (feature) {
  284. u8 values[HID_CUSTOM_MAX_FEATURE_BYTES];
  285. int len = 0;
  286. u64 value = 0;
  287. int i = 0;
  288. ret = sensor_hub_get_feature(sensor_inst->hsdev,
  289. report_id,
  290. index,
  291. sizeof(values), values);
  292. if (ret < 0)
  293. return ret;
  294. while (i < ret) {
  295. if (i + attribute->size > ret) {
  296. len += snprintf(&buf[len],
  297. PAGE_SIZE - len,
  298. "%d ", values[i]);
  299. break;
  300. }
  301. switch (attribute->size) {
  302. case 2:
  303. value = (u64) *(u16 *)&values[i];
  304. i += attribute->size;
  305. break;
  306. case 4:
  307. value = (u64) *(u32 *)&values[i];
  308. i += attribute->size;
  309. break;
  310. case 8:
  311. value = *(u64 *)&values[i];
  312. i += attribute->size;
  313. break;
  314. default:
  315. value = (u64) values[i];
  316. ++i;
  317. break;
  318. }
  319. len += snprintf(&buf[len], PAGE_SIZE - len,
  320. "%lld ", value);
  321. }
  322. len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
  323. return len;
  324. } else if (input)
  325. value = sensor_hub_input_attr_get_raw_value(
  326. sensor_inst->hsdev,
  327. sensor_inst->hsdev->usage,
  328. usage, report_id,
  329. SENSOR_HUB_SYNC);
  330. } else if (!strncmp(name, "units", strlen("units")))
  331. value = sensor_inst->fields[field_index].attribute.units;
  332. else if (!strncmp(name, "unit-expo", strlen("unit-expo")))
  333. value = sensor_inst->fields[field_index].attribute.unit_expo;
  334. else if (!strncmp(name, "size", strlen("size")))
  335. value = sensor_inst->fields[field_index].attribute.size;
  336. else if (!strncmp(name, "minimum", strlen("minimum")))
  337. value = sensor_inst->fields[field_index].attribute.
  338. logical_minimum;
  339. else if (!strncmp(name, "maximum", strlen("maximum")))
  340. value = sensor_inst->fields[field_index].attribute.
  341. logical_maximum;
  342. else if (!strncmp(name, "name", strlen("name"))) {
  343. struct hid_custom_usage_desc *usage_desc;
  344. usage_desc = bsearch(&usage, hid_custom_usage_desc_table,
  345. ARRAY_SIZE(hid_custom_usage_desc_table),
  346. sizeof(struct hid_custom_usage_desc),
  347. usage_id_cmp);
  348. if (usage_desc)
  349. return snprintf(buf, PAGE_SIZE, "%s\n",
  350. usage_desc->desc);
  351. else
  352. return sprintf(buf, "not-specified\n");
  353. } else
  354. return -EINVAL;
  355. return sprintf(buf, "%d\n", value);
  356. }
  357. static ssize_t store_value(struct device *dev, struct device_attribute *attr,
  358. const char *buf, size_t count)
  359. {
  360. struct platform_device *pdev = to_platform_device(dev);
  361. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
  362. int index, field_index, usage;
  363. char name[HID_CUSTOM_NAME_LENGTH];
  364. int value;
  365. if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
  366. name) == 3) {
  367. field_index = index + sensor_inst->input_field_count;
  368. } else
  369. return -EINVAL;
  370. if (!strncmp(name, "value", strlen("value"))) {
  371. u32 report_id;
  372. int ret;
  373. if (kstrtoint(buf, 0, &value) != 0)
  374. return -EINVAL;
  375. report_id = sensor_inst->fields[field_index].attribute.
  376. report_id;
  377. ret = sensor_hub_set_feature(sensor_inst->hsdev, report_id,
  378. index, sizeof(value), &value);
  379. } else
  380. return -EINVAL;
  381. return count;
  382. }
  383. static int hid_sensor_capture_sample(struct hid_sensor_hub_device *hsdev,
  384. unsigned usage_id, size_t raw_len,
  385. char *raw_data, void *priv)
  386. {
  387. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
  388. struct hid_sensor_sample header;
  389. /* If any error occurs in a sample, rest of the fields are ignored */
  390. if (sensor_inst->input_skip_sample) {
  391. hid_err(sensor_inst->hsdev->hdev, "Skipped remaining data\n");
  392. return 0;
  393. }
  394. hid_dbg(sensor_inst->hsdev->hdev, "%s received %d of %d\n", __func__,
  395. (int) (sensor_inst->input_report_recd_size + raw_len),
  396. sensor_inst->input_report_size);
  397. if (!test_bit(0, &sensor_inst->misc_opened))
  398. return 0;
  399. if (!sensor_inst->input_report_recd_size) {
  400. int required_size = sizeof(struct hid_sensor_sample) +
  401. sensor_inst->input_report_size;
  402. header.usage_id = hsdev->usage;
  403. header.raw_len = sensor_inst->input_report_size;
  404. header.timestamp = ktime_get_real_ns();
  405. if (kfifo_avail(&sensor_inst->data_fifo) >= required_size) {
  406. kfifo_in(&sensor_inst->data_fifo,
  407. (unsigned char *)&header,
  408. sizeof(header));
  409. } else
  410. sensor_inst->input_skip_sample = true;
  411. }
  412. if (kfifo_avail(&sensor_inst->data_fifo) >= raw_len)
  413. kfifo_in(&sensor_inst->data_fifo, (unsigned char *)raw_data,
  414. raw_len);
  415. sensor_inst->input_report_recd_size += raw_len;
  416. return 0;
  417. }
  418. static int hid_sensor_send_event(struct hid_sensor_hub_device *hsdev,
  419. unsigned usage_id, void *priv)
  420. {
  421. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
  422. if (!test_bit(0, &sensor_inst->misc_opened))
  423. return 0;
  424. sensor_inst->input_report_recd_size = 0;
  425. sensor_inst->input_skip_sample = false;
  426. wake_up(&sensor_inst->wait);
  427. return 0;
  428. }
  429. static int hid_sensor_custom_add_field(struct hid_sensor_custom *sensor_inst,
  430. int index, int report_type,
  431. struct hid_report *report,
  432. struct hid_field *field)
  433. {
  434. struct hid_sensor_custom_field *sensor_field;
  435. void *fields;
  436. fields = krealloc(sensor_inst->fields,
  437. (sensor_inst->sensor_field_count + 1) *
  438. sizeof(struct hid_sensor_custom_field), GFP_KERNEL);
  439. if (!fields) {
  440. kfree(sensor_inst->fields);
  441. return -ENOMEM;
  442. }
  443. sensor_inst->fields = fields;
  444. sensor_field = &sensor_inst->fields[sensor_inst->sensor_field_count];
  445. sensor_field->attribute.usage_id = sensor_inst->hsdev->usage;
  446. if (field->logical)
  447. sensor_field->attribute.attrib_id = field->logical;
  448. else
  449. sensor_field->attribute.attrib_id = field->usage[0].hid;
  450. sensor_field->attribute.index = index;
  451. sensor_field->attribute.report_id = report->id;
  452. sensor_field->attribute.units = field->unit;
  453. sensor_field->attribute.unit_expo = field->unit_exponent;
  454. sensor_field->attribute.size = (field->report_size / 8);
  455. sensor_field->attribute.logical_minimum = field->logical_minimum;
  456. sensor_field->attribute.logical_maximum = field->logical_maximum;
  457. if (report_type == HID_FEATURE_REPORT)
  458. snprintf(sensor_field->group_name,
  459. sizeof(sensor_field->group_name), "feature-%x-%x",
  460. sensor_field->attribute.index,
  461. sensor_field->attribute.attrib_id);
  462. else if (report_type == HID_INPUT_REPORT) {
  463. snprintf(sensor_field->group_name,
  464. sizeof(sensor_field->group_name),
  465. "input-%x-%x", sensor_field->attribute.index,
  466. sensor_field->attribute.attrib_id);
  467. sensor_inst->input_field_count++;
  468. sensor_inst->input_report_size += (field->report_size *
  469. field->report_count) / 8;
  470. }
  471. memset(&sensor_field->hid_custom_attribute_group, 0,
  472. sizeof(struct attribute_group));
  473. sensor_inst->sensor_field_count++;
  474. return 0;
  475. }
  476. static int hid_sensor_custom_add_fields(struct hid_sensor_custom *sensor_inst,
  477. struct hid_report_enum *report_enum,
  478. int report_type)
  479. {
  480. int i;
  481. int ret;
  482. struct hid_report *report;
  483. struct hid_field *field;
  484. struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
  485. list_for_each_entry(report, &report_enum->report_list, list) {
  486. for (i = 0; i < report->maxfield; ++i) {
  487. field = report->field[i];
  488. if (field->maxusage &&
  489. ((field->usage[0].collection_index >=
  490. hsdev->start_collection_index) &&
  491. (field->usage[0].collection_index <
  492. hsdev->end_collection_index))) {
  493. ret = hid_sensor_custom_add_field(sensor_inst,
  494. i,
  495. report_type,
  496. report,
  497. field);
  498. if (ret)
  499. return ret;
  500. }
  501. }
  502. }
  503. return 0;
  504. }
  505. static int hid_sensor_custom_add_attributes(struct hid_sensor_custom
  506. *sensor_inst)
  507. {
  508. struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
  509. struct hid_device *hdev = hsdev->hdev;
  510. int ret = -1;
  511. int i, j;
  512. for (j = 0; j < HID_REPORT_TYPES; ++j) {
  513. if (j == HID_OUTPUT_REPORT)
  514. continue;
  515. ret = hid_sensor_custom_add_fields(sensor_inst,
  516. &hdev->report_enum[j], j);
  517. if (ret)
  518. return ret;
  519. }
  520. /* Create sysfs attributes */
  521. for (i = 0; i < sensor_inst->sensor_field_count; ++i) {
  522. j = 0;
  523. while (j < HID_CUSTOM_TOTAL_ATTRS &&
  524. hid_custom_attrs[j].name) {
  525. struct device_attribute *device_attr;
  526. device_attr = &sensor_inst->fields[i].sd_attrs[j];
  527. snprintf((char *)&sensor_inst->fields[i].attr_name[j],
  528. HID_CUSTOM_NAME_LENGTH, "%s-%s",
  529. sensor_inst->fields[i].group_name,
  530. hid_custom_attrs[j].name);
  531. sysfs_attr_init(&device_attr->attr);
  532. device_attr->attr.name =
  533. (char *)&sensor_inst->fields[i].attr_name[j];
  534. device_attr->attr.mode = hid_custom_attrs[j].mode;
  535. device_attr->show = show_value;
  536. if (hid_custom_attrs[j].mode & S_IWUSR)
  537. device_attr->store = store_value;
  538. sensor_inst->fields[i].attrs[j] = &device_attr->attr;
  539. ++j;
  540. }
  541. sensor_inst->fields[i].attrs[j] = NULL;
  542. sensor_inst->fields[i].hid_custom_attribute_group.attrs =
  543. sensor_inst->fields[i].attrs;
  544. sensor_inst->fields[i].hid_custom_attribute_group.name =
  545. sensor_inst->fields[i].group_name;
  546. ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
  547. &sensor_inst->fields[i].
  548. hid_custom_attribute_group);
  549. if (ret)
  550. break;
  551. /* For power or report field store indexes */
  552. if (sensor_inst->fields[i].attribute.attrib_id ==
  553. HID_USAGE_SENSOR_PROY_POWER_STATE)
  554. sensor_inst->power_state = &sensor_inst->fields[i];
  555. else if (sensor_inst->fields[i].attribute.attrib_id ==
  556. HID_USAGE_SENSOR_PROP_REPORT_STATE)
  557. sensor_inst->report_state = &sensor_inst->fields[i];
  558. }
  559. return ret;
  560. }
  561. static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom *
  562. sensor_inst)
  563. {
  564. int i;
  565. for (i = 0; i < sensor_inst->sensor_field_count; ++i)
  566. sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
  567. &sensor_inst->fields[i].
  568. hid_custom_attribute_group);
  569. kfree(sensor_inst->fields);
  570. }
  571. static ssize_t hid_sensor_custom_read(struct file *file, char __user *buf,
  572. size_t count, loff_t *f_ps)
  573. {
  574. struct hid_sensor_custom *sensor_inst;
  575. unsigned int copied;
  576. int ret;
  577. sensor_inst = container_of(file->private_data,
  578. struct hid_sensor_custom, custom_dev);
  579. if (count < sizeof(struct hid_sensor_sample))
  580. return -EINVAL;
  581. do {
  582. if (kfifo_is_empty(&sensor_inst->data_fifo)) {
  583. if (file->f_flags & O_NONBLOCK)
  584. return -EAGAIN;
  585. ret = wait_event_interruptible(sensor_inst->wait,
  586. !kfifo_is_empty(&sensor_inst->data_fifo));
  587. if (ret)
  588. return ret;
  589. }
  590. ret = kfifo_to_user(&sensor_inst->data_fifo, buf, count,
  591. &copied);
  592. if (ret)
  593. return ret;
  594. } while (copied == 0);
  595. return copied;
  596. }
  597. static int hid_sensor_custom_release(struct inode *inode, struct file *file)
  598. {
  599. struct hid_sensor_custom *sensor_inst;
  600. sensor_inst = container_of(file->private_data,
  601. struct hid_sensor_custom, custom_dev);
  602. clear_bit(0, &sensor_inst->misc_opened);
  603. return 0;
  604. }
  605. static int hid_sensor_custom_open(struct inode *inode, struct file *file)
  606. {
  607. struct hid_sensor_custom *sensor_inst;
  608. sensor_inst = container_of(file->private_data,
  609. struct hid_sensor_custom, custom_dev);
  610. /* We essentially have single reader and writer */
  611. if (test_and_set_bit(0, &sensor_inst->misc_opened))
  612. return -EBUSY;
  613. return nonseekable_open(inode, file);
  614. }
  615. static unsigned int hid_sensor_custom_poll(struct file *file,
  616. struct poll_table_struct *wait)
  617. {
  618. struct hid_sensor_custom *sensor_inst;
  619. unsigned int mask = 0;
  620. sensor_inst = container_of(file->private_data,
  621. struct hid_sensor_custom, custom_dev);
  622. poll_wait(file, &sensor_inst->wait, wait);
  623. if (!kfifo_is_empty(&sensor_inst->data_fifo))
  624. mask = POLLIN | POLLRDNORM;
  625. return mask;
  626. }
  627. static const struct file_operations hid_sensor_custom_fops = {
  628. .open = hid_sensor_custom_open,
  629. .read = hid_sensor_custom_read,
  630. .release = hid_sensor_custom_release,
  631. .poll = hid_sensor_custom_poll,
  632. .llseek = noop_llseek,
  633. };
  634. static int hid_sensor_custom_dev_if_add(struct hid_sensor_custom *sensor_inst)
  635. {
  636. int ret;
  637. ret = kfifo_alloc(&sensor_inst->data_fifo, HID_CUSTOM_FIFO_SIZE,
  638. GFP_KERNEL);
  639. if (ret)
  640. return ret;
  641. init_waitqueue_head(&sensor_inst->wait);
  642. sensor_inst->custom_dev.minor = MISC_DYNAMIC_MINOR;
  643. sensor_inst->custom_dev.name = dev_name(&sensor_inst->pdev->dev);
  644. sensor_inst->custom_dev.fops = &hid_sensor_custom_fops,
  645. ret = misc_register(&sensor_inst->custom_dev);
  646. if (ret) {
  647. kfifo_free(&sensor_inst->data_fifo);
  648. return ret;
  649. }
  650. return 0;
  651. }
  652. static void hid_sensor_custom_dev_if_remove(struct hid_sensor_custom
  653. *sensor_inst)
  654. {
  655. wake_up(&sensor_inst->wait);
  656. misc_deregister(&sensor_inst->custom_dev);
  657. kfifo_free(&sensor_inst->data_fifo);
  658. }
  659. static int hid_sensor_custom_probe(struct platform_device *pdev)
  660. {
  661. struct hid_sensor_custom *sensor_inst;
  662. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  663. int ret;
  664. sensor_inst = devm_kzalloc(&pdev->dev, sizeof(*sensor_inst),
  665. GFP_KERNEL);
  666. if (!sensor_inst)
  667. return -ENOMEM;
  668. sensor_inst->callbacks.capture_sample = hid_sensor_capture_sample;
  669. sensor_inst->callbacks.send_event = hid_sensor_send_event;
  670. sensor_inst->callbacks.pdev = pdev;
  671. sensor_inst->hsdev = hsdev;
  672. sensor_inst->pdev = pdev;
  673. mutex_init(&sensor_inst->mutex);
  674. platform_set_drvdata(pdev, sensor_inst);
  675. ret = sensor_hub_register_callback(hsdev, hsdev->usage,
  676. &sensor_inst->callbacks);
  677. if (ret < 0) {
  678. dev_err(&pdev->dev, "callback reg failed\n");
  679. return ret;
  680. }
  681. ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
  682. &enable_sensor_attr_group);
  683. if (ret)
  684. goto err_remove_callback;
  685. ret = hid_sensor_custom_add_attributes(sensor_inst);
  686. if (ret)
  687. goto err_remove_group;
  688. ret = hid_sensor_custom_dev_if_add(sensor_inst);
  689. if (ret)
  690. goto err_remove_attributes;
  691. return 0;
  692. err_remove_attributes:
  693. hid_sensor_custom_remove_attributes(sensor_inst);
  694. err_remove_group:
  695. sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
  696. &enable_sensor_attr_group);
  697. err_remove_callback:
  698. sensor_hub_remove_callback(hsdev, hsdev->usage);
  699. return ret;
  700. }
  701. static int hid_sensor_custom_remove(struct platform_device *pdev)
  702. {
  703. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
  704. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  705. hid_sensor_custom_dev_if_remove(sensor_inst);
  706. hid_sensor_custom_remove_attributes(sensor_inst);
  707. sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
  708. &enable_sensor_attr_group);
  709. sensor_hub_remove_callback(hsdev, hsdev->usage);
  710. return 0;
  711. }
  712. static struct platform_device_id hid_sensor_custom_ids[] = {
  713. {
  714. .name = "HID-SENSOR-2000e1",
  715. },
  716. {
  717. .name = "HID-SENSOR-2000e2",
  718. },
  719. { /* sentinel */ }
  720. };
  721. MODULE_DEVICE_TABLE(platform, hid_sensor_custom_ids);
  722. static struct platform_driver hid_sensor_custom_platform_driver = {
  723. .id_table = hid_sensor_custom_ids,
  724. .driver = {
  725. .name = KBUILD_MODNAME,
  726. },
  727. .probe = hid_sensor_custom_probe,
  728. .remove = hid_sensor_custom_remove,
  729. };
  730. module_platform_driver(hid_sensor_custom_platform_driver);
  731. MODULE_DESCRIPTION("HID Sensor Custom and Generic sensor Driver");
  732. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  733. MODULE_LICENSE("GPL");