hid-sensor-custom.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * hid-sensor-custom.c
  4. * Copyright (c) 2015, Intel Corporation.
  5. */
  6. #include <linux/ctype.h>
  7. #include <linux/dmi.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/sched.h>
  14. #include <linux/wait.h>
  15. #include <linux/poll.h>
  16. #include <linux/bsearch.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/hid-sensor-hub.h>
  19. #define HID_CUSTOM_NAME_LENGTH 64
  20. #define HID_CUSTOM_MAX_CORE_ATTRS 10
  21. #define HID_CUSTOM_TOTAL_ATTRS (HID_CUSTOM_MAX_CORE_ATTRS + 1)
  22. #define HID_CUSTOM_FIFO_SIZE 4096
  23. #define HID_CUSTOM_MAX_FEATURE_BYTES 64
  24. #define HID_SENSOR_USAGE_LENGTH (4 + 1)
  25. struct hid_sensor_custom_field {
  26. int report_id;
  27. char group_name[HID_CUSTOM_NAME_LENGTH];
  28. struct hid_sensor_hub_attribute_info attribute;
  29. struct device_attribute sd_attrs[HID_CUSTOM_MAX_CORE_ATTRS];
  30. char attr_name[HID_CUSTOM_TOTAL_ATTRS][HID_CUSTOM_NAME_LENGTH];
  31. struct attribute *attrs[HID_CUSTOM_TOTAL_ATTRS];
  32. struct attribute_group hid_custom_attribute_group;
  33. };
  34. struct hid_sensor_custom {
  35. struct mutex mutex;
  36. struct platform_device *pdev;
  37. struct hid_sensor_hub_device *hsdev;
  38. struct hid_sensor_hub_callbacks callbacks;
  39. int sensor_field_count;
  40. struct hid_sensor_custom_field *fields;
  41. int input_field_count;
  42. int input_report_size;
  43. int input_report_recd_size;
  44. bool input_skip_sample;
  45. bool enable;
  46. struct hid_sensor_custom_field *power_state;
  47. struct hid_sensor_custom_field *report_state;
  48. struct miscdevice custom_dev;
  49. struct kfifo data_fifo;
  50. unsigned long misc_opened;
  51. wait_queue_head_t wait;
  52. struct platform_device *custom_pdev;
  53. };
  54. /* Header for each sample to user space via dev interface */
  55. struct hid_sensor_sample {
  56. u32 usage_id;
  57. u64 timestamp;
  58. u32 raw_len;
  59. } __packed;
  60. static struct attribute hid_custom_attrs[HID_CUSTOM_TOTAL_ATTRS] = {
  61. {.name = "name", .mode = S_IRUGO},
  62. {.name = "units", .mode = S_IRUGO},
  63. {.name = "unit-expo", .mode = S_IRUGO},
  64. {.name = "minimum", .mode = S_IRUGO},
  65. {.name = "maximum", .mode = S_IRUGO},
  66. {.name = "size", .mode = S_IRUGO},
  67. {.name = "value", .mode = S_IWUSR | S_IRUGO},
  68. {.name = NULL}
  69. };
  70. static const struct hid_custom_usage_desc {
  71. int usage_id;
  72. char *desc;
  73. } hid_custom_usage_desc_table[] = {
  74. {0x200201, "event-sensor-state"},
  75. {0x200202, "event-sensor-event"},
  76. {0x200301, "property-friendly-name"},
  77. {0x200302, "property-persistent-unique-id"},
  78. {0x200303, "property-sensor-status"},
  79. {0x200304, "property-min-report-interval"},
  80. {0x200305, "property-sensor-manufacturer"},
  81. {0x200306, "property-sensor-model"},
  82. {0x200307, "property-sensor-serial-number"},
  83. {0x200308, "property-sensor-description"},
  84. {0x200309, "property-sensor-connection-type"},
  85. {0x20030A, "property-sensor-device-path"},
  86. {0x20030B, "property-hardware-revision"},
  87. {0x20030C, "property-firmware-version"},
  88. {0x20030D, "property-release-date"},
  89. {0x20030E, "property-report-interval"},
  90. {0x20030F, "property-change-sensitivity-absolute"},
  91. {0x200310, "property-change-sensitivity-percent-range"},
  92. {0x200311, "property-change-sensitivity-percent-relative"},
  93. {0x200312, "property-accuracy"},
  94. {0x200313, "property-resolution"},
  95. {0x200314, "property-maximum"},
  96. {0x200315, "property-minimum"},
  97. {0x200316, "property-reporting-state"},
  98. {0x200317, "property-sampling-rate"},
  99. {0x200318, "property-response-curve"},
  100. {0x200319, "property-power-state"},
  101. {0x200540, "data-field-custom"},
  102. {0x200541, "data-field-custom-usage"},
  103. {0x200542, "data-field-custom-boolean-array"},
  104. {0x200543, "data-field-custom-value"},
  105. {0x200544, "data-field-custom-value_1"},
  106. {0x200545, "data-field-custom-value_2"},
  107. {0x200546, "data-field-custom-value_3"},
  108. {0x200547, "data-field-custom-value_4"},
  109. {0x200548, "data-field-custom-value_5"},
  110. {0x200549, "data-field-custom-value_6"},
  111. {0x20054A, "data-field-custom-value_7"},
  112. {0x20054B, "data-field-custom-value_8"},
  113. {0x20054C, "data-field-custom-value_9"},
  114. {0x20054D, "data-field-custom-value_10"},
  115. {0x20054E, "data-field-custom-value_11"},
  116. {0x20054F, "data-field-custom-value_12"},
  117. {0x200550, "data-field-custom-value_13"},
  118. {0x200551, "data-field-custom-value_14"},
  119. {0x200552, "data-field-custom-value_15"},
  120. {0x200553, "data-field-custom-value_16"},
  121. {0x200554, "data-field-custom-value_17"},
  122. {0x200555, "data-field-custom-value_18"},
  123. {0x200556, "data-field-custom-value_19"},
  124. {0x200557, "data-field-custom-value_20"},
  125. {0x200558, "data-field-custom-value_21"},
  126. {0x200559, "data-field-custom-value_22"},
  127. {0x20055A, "data-field-custom-value_23"},
  128. {0x20055B, "data-field-custom-value_24"},
  129. {0x20055C, "data-field-custom-value_25"},
  130. {0x20055D, "data-field-custom-value_26"},
  131. {0x20055E, "data-field-custom-value_27"},
  132. {0x20055F, "data-field-custom-value_28"},
  133. };
  134. static int usage_id_cmp(const void *p1, const void *p2)
  135. {
  136. if (*(int *)p1 < *(int *)p2)
  137. return -1;
  138. if (*(int *)p1 > *(int *)p2)
  139. return 1;
  140. return 0;
  141. }
  142. static ssize_t enable_sensor_show(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
  146. return sysfs_emit(buf, "%d\n", sensor_inst->enable);
  147. }
  148. static int set_power_report_state(struct hid_sensor_custom *sensor_inst,
  149. bool state)
  150. {
  151. int power_val = -1;
  152. int report_val = -1;
  153. u32 power_state_usage_id;
  154. u32 report_state_usage_id;
  155. int ret;
  156. /*
  157. * It is possible that the power/report state ids are not present.
  158. * In this case this function will return success. But if the
  159. * ids are present, then it will return error if set fails.
  160. */
  161. if (state) {
  162. power_state_usage_id =
  163. HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM;
  164. report_state_usage_id =
  165. HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM;
  166. } else {
  167. power_state_usage_id =
  168. HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM;
  169. report_state_usage_id =
  170. HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM;
  171. }
  172. if (sensor_inst->power_state)
  173. power_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
  174. sensor_inst->power_state->attribute.report_id,
  175. sensor_inst->power_state->attribute.index,
  176. power_state_usage_id);
  177. if (sensor_inst->report_state)
  178. report_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
  179. sensor_inst->report_state->attribute.report_id,
  180. sensor_inst->report_state->attribute.index,
  181. report_state_usage_id);
  182. if (power_val >= 0) {
  183. power_val +=
  184. sensor_inst->power_state->attribute.logical_minimum;
  185. ret = sensor_hub_set_feature(sensor_inst->hsdev,
  186. sensor_inst->power_state->attribute.report_id,
  187. sensor_inst->power_state->attribute.index,
  188. sizeof(power_val),
  189. &power_val);
  190. if (ret) {
  191. hid_err(sensor_inst->hsdev->hdev,
  192. "Set power state failed\n");
  193. return ret;
  194. }
  195. }
  196. if (report_val >= 0) {
  197. report_val +=
  198. sensor_inst->report_state->attribute.logical_minimum;
  199. ret = sensor_hub_set_feature(sensor_inst->hsdev,
  200. sensor_inst->report_state->attribute.report_id,
  201. sensor_inst->report_state->attribute.index,
  202. sizeof(report_val),
  203. &report_val);
  204. if (ret) {
  205. hid_err(sensor_inst->hsdev->hdev,
  206. "Set report state failed\n");
  207. return ret;
  208. }
  209. }
  210. return 0;
  211. }
  212. static ssize_t enable_sensor_store(struct device *dev,
  213. struct device_attribute *attr,
  214. const char *buf, size_t count)
  215. {
  216. struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
  217. int value;
  218. int ret = -EINVAL;
  219. if (kstrtoint(buf, 0, &value) != 0)
  220. return -EINVAL;
  221. mutex_lock(&sensor_inst->mutex);
  222. if (value && !sensor_inst->enable) {
  223. ret = sensor_hub_device_open(sensor_inst->hsdev);
  224. if (ret)
  225. goto unlock_state;
  226. ret = set_power_report_state(sensor_inst, true);
  227. if (ret) {
  228. sensor_hub_device_close(sensor_inst->hsdev);
  229. goto unlock_state;
  230. }
  231. sensor_inst->enable = true;
  232. } else if (!value && sensor_inst->enable) {
  233. ret = set_power_report_state(sensor_inst, false);
  234. sensor_hub_device_close(sensor_inst->hsdev);
  235. sensor_inst->enable = false;
  236. }
  237. unlock_state:
  238. mutex_unlock(&sensor_inst->mutex);
  239. if (ret < 0)
  240. return ret;
  241. return count;
  242. }
  243. static DEVICE_ATTR_RW(enable_sensor);
  244. static struct attribute *enable_sensor_attrs[] = {
  245. &dev_attr_enable_sensor.attr,
  246. NULL,
  247. };
  248. static const struct attribute_group enable_sensor_attr_group = {
  249. .attrs = enable_sensor_attrs,
  250. };
  251. static ssize_t show_value(struct device *dev, struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
  255. struct hid_sensor_hub_attribute_info *attribute;
  256. int index, usage, field_index;
  257. char name[HID_CUSTOM_NAME_LENGTH];
  258. bool feature = false;
  259. bool input = false;
  260. int value = 0;
  261. if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
  262. name) == 3) {
  263. feature = true;
  264. field_index = index + sensor_inst->input_field_count;
  265. } else if (sscanf(attr->attr.name, "input-%x-%x-%s", &index, &usage,
  266. name) == 3) {
  267. input = true;
  268. field_index = index;
  269. } else
  270. return -EINVAL;
  271. if (!strncmp(name, "value", strlen("value"))) {
  272. u32 report_id;
  273. int ret;
  274. attribute = &sensor_inst->fields[field_index].attribute;
  275. report_id = attribute->report_id;
  276. if (feature) {
  277. u8 values[HID_CUSTOM_MAX_FEATURE_BYTES];
  278. int len = 0;
  279. u64 value = 0;
  280. int i = 0;
  281. ret = sensor_hub_get_feature(sensor_inst->hsdev,
  282. report_id,
  283. index,
  284. sizeof(values), values);
  285. if (ret < 0)
  286. return ret;
  287. while (i < ret) {
  288. if (i + attribute->size > ret) {
  289. len += scnprintf(&buf[len],
  290. PAGE_SIZE - len,
  291. "%d ", values[i]);
  292. break;
  293. }
  294. switch (attribute->size) {
  295. case 2:
  296. value = (u64) *(u16 *)&values[i];
  297. i += attribute->size;
  298. break;
  299. case 4:
  300. value = (u64) *(u32 *)&values[i];
  301. i += attribute->size;
  302. break;
  303. case 8:
  304. value = *(u64 *)&values[i];
  305. i += attribute->size;
  306. break;
  307. default:
  308. value = (u64) values[i];
  309. ++i;
  310. break;
  311. }
  312. len += scnprintf(&buf[len], PAGE_SIZE - len,
  313. "%lld ", value);
  314. }
  315. len += scnprintf(&buf[len], PAGE_SIZE - len, "\n");
  316. return len;
  317. } else if (input)
  318. value = sensor_hub_input_attr_get_raw_value(
  319. sensor_inst->hsdev,
  320. sensor_inst->hsdev->usage,
  321. usage, report_id,
  322. SENSOR_HUB_SYNC, false);
  323. } else if (!strncmp(name, "units", strlen("units")))
  324. value = sensor_inst->fields[field_index].attribute.units;
  325. else if (!strncmp(name, "unit-expo", strlen("unit-expo")))
  326. value = sensor_inst->fields[field_index].attribute.unit_expo;
  327. else if (!strncmp(name, "size", strlen("size")))
  328. value = sensor_inst->fields[field_index].attribute.size;
  329. else if (!strncmp(name, "minimum", strlen("minimum")))
  330. value = sensor_inst->fields[field_index].attribute.
  331. logical_minimum;
  332. else if (!strncmp(name, "maximum", strlen("maximum")))
  333. value = sensor_inst->fields[field_index].attribute.
  334. logical_maximum;
  335. else if (!strncmp(name, "name", strlen("name"))) {
  336. struct hid_custom_usage_desc *usage_desc;
  337. usage_desc = bsearch(&usage, hid_custom_usage_desc_table,
  338. ARRAY_SIZE(hid_custom_usage_desc_table),
  339. sizeof(struct hid_custom_usage_desc),
  340. usage_id_cmp);
  341. if (usage_desc)
  342. return sysfs_emit(buf, "%s\n", usage_desc->desc);
  343. else
  344. return sysfs_emit(buf, "not-specified\n");
  345. } else
  346. return -EINVAL;
  347. return sysfs_emit(buf, "%d\n", value);
  348. }
  349. static ssize_t store_value(struct device *dev, struct device_attribute *attr,
  350. const char *buf, size_t count)
  351. {
  352. struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
  353. int index, field_index, usage;
  354. char name[HID_CUSTOM_NAME_LENGTH];
  355. int value, ret;
  356. if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
  357. name) == 3) {
  358. field_index = index + sensor_inst->input_field_count;
  359. } else
  360. return -EINVAL;
  361. if (!strncmp(name, "value", strlen("value"))) {
  362. u32 report_id;
  363. if (kstrtoint(buf, 0, &value) != 0)
  364. return -EINVAL;
  365. report_id = sensor_inst->fields[field_index].attribute.
  366. report_id;
  367. ret = sensor_hub_set_feature(sensor_inst->hsdev, report_id,
  368. index, sizeof(value), &value);
  369. if (ret)
  370. return ret;
  371. } else
  372. return -EINVAL;
  373. return count;
  374. }
  375. static int hid_sensor_capture_sample(struct hid_sensor_hub_device *hsdev,
  376. unsigned usage_id, size_t raw_len,
  377. char *raw_data, void *priv)
  378. {
  379. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
  380. struct hid_sensor_sample header;
  381. /* If any error occurs in a sample, rest of the fields are ignored */
  382. if (sensor_inst->input_skip_sample) {
  383. hid_err(sensor_inst->hsdev->hdev, "Skipped remaining data\n");
  384. return 0;
  385. }
  386. hid_dbg(sensor_inst->hsdev->hdev, "%s received %d of %d\n", __func__,
  387. (int) (sensor_inst->input_report_recd_size + raw_len),
  388. sensor_inst->input_report_size);
  389. if (!test_bit(0, &sensor_inst->misc_opened))
  390. return 0;
  391. if (!sensor_inst->input_report_recd_size) {
  392. int required_size = sizeof(struct hid_sensor_sample) +
  393. sensor_inst->input_report_size;
  394. header.usage_id = hsdev->usage;
  395. header.raw_len = sensor_inst->input_report_size;
  396. header.timestamp = ktime_get_real_ns();
  397. if (kfifo_avail(&sensor_inst->data_fifo) >= required_size) {
  398. kfifo_in(&sensor_inst->data_fifo,
  399. (unsigned char *)&header,
  400. sizeof(header));
  401. } else
  402. sensor_inst->input_skip_sample = true;
  403. }
  404. if (kfifo_avail(&sensor_inst->data_fifo) >= raw_len)
  405. kfifo_in(&sensor_inst->data_fifo, (unsigned char *)raw_data,
  406. raw_len);
  407. sensor_inst->input_report_recd_size += raw_len;
  408. return 0;
  409. }
  410. static int hid_sensor_send_event(struct hid_sensor_hub_device *hsdev,
  411. unsigned usage_id, void *priv)
  412. {
  413. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
  414. if (!test_bit(0, &sensor_inst->misc_opened))
  415. return 0;
  416. sensor_inst->input_report_recd_size = 0;
  417. sensor_inst->input_skip_sample = false;
  418. wake_up(&sensor_inst->wait);
  419. return 0;
  420. }
  421. static int hid_sensor_custom_add_field(struct hid_sensor_custom *sensor_inst,
  422. int index, int report_type,
  423. struct hid_report *report,
  424. struct hid_field *field)
  425. {
  426. struct hid_sensor_custom_field *sensor_field;
  427. void *fields;
  428. fields = krealloc(sensor_inst->fields,
  429. (sensor_inst->sensor_field_count + 1) *
  430. sizeof(struct hid_sensor_custom_field), GFP_KERNEL);
  431. if (!fields) {
  432. kfree(sensor_inst->fields);
  433. return -ENOMEM;
  434. }
  435. sensor_inst->fields = fields;
  436. sensor_field = &sensor_inst->fields[sensor_inst->sensor_field_count];
  437. sensor_field->attribute.usage_id = sensor_inst->hsdev->usage;
  438. if (field->logical)
  439. sensor_field->attribute.attrib_id = field->logical;
  440. else
  441. sensor_field->attribute.attrib_id = field->usage[0].hid;
  442. sensor_field->attribute.index = index;
  443. sensor_field->attribute.report_id = report->id;
  444. sensor_field->attribute.units = field->unit;
  445. sensor_field->attribute.unit_expo = field->unit_exponent;
  446. sensor_field->attribute.size = (field->report_size / 8);
  447. sensor_field->attribute.logical_minimum = field->logical_minimum;
  448. sensor_field->attribute.logical_maximum = field->logical_maximum;
  449. if (report_type == HID_FEATURE_REPORT)
  450. snprintf(sensor_field->group_name,
  451. sizeof(sensor_field->group_name), "feature-%x-%x",
  452. sensor_field->attribute.index,
  453. sensor_field->attribute.attrib_id);
  454. else if (report_type == HID_INPUT_REPORT) {
  455. snprintf(sensor_field->group_name,
  456. sizeof(sensor_field->group_name),
  457. "input-%x-%x", sensor_field->attribute.index,
  458. sensor_field->attribute.attrib_id);
  459. sensor_inst->input_field_count++;
  460. sensor_inst->input_report_size += (field->report_size *
  461. field->report_count) / 8;
  462. }
  463. memset(&sensor_field->hid_custom_attribute_group, 0,
  464. sizeof(struct attribute_group));
  465. sensor_inst->sensor_field_count++;
  466. return 0;
  467. }
  468. static int hid_sensor_custom_add_fields(struct hid_sensor_custom *sensor_inst,
  469. struct hid_report_enum *report_enum,
  470. int report_type)
  471. {
  472. int i;
  473. int ret;
  474. struct hid_report *report;
  475. struct hid_field *field;
  476. struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
  477. list_for_each_entry(report, &report_enum->report_list, list) {
  478. for (i = 0; i < report->maxfield; ++i) {
  479. field = report->field[i];
  480. if (field->maxusage &&
  481. ((field->usage[0].collection_index >=
  482. hsdev->start_collection_index) &&
  483. (field->usage[0].collection_index <
  484. hsdev->end_collection_index))) {
  485. ret = hid_sensor_custom_add_field(sensor_inst,
  486. i,
  487. report_type,
  488. report,
  489. field);
  490. if (ret)
  491. return ret;
  492. }
  493. }
  494. }
  495. return 0;
  496. }
  497. static int hid_sensor_custom_add_attributes(struct hid_sensor_custom
  498. *sensor_inst)
  499. {
  500. struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
  501. struct hid_device *hdev = hsdev->hdev;
  502. int ret = -1;
  503. int i, j;
  504. for (j = 0; j < HID_REPORT_TYPES; ++j) {
  505. if (j == HID_OUTPUT_REPORT)
  506. continue;
  507. ret = hid_sensor_custom_add_fields(sensor_inst,
  508. &hdev->report_enum[j], j);
  509. if (ret)
  510. return ret;
  511. }
  512. /* Create sysfs attributes */
  513. for (i = 0; i < sensor_inst->sensor_field_count; ++i) {
  514. j = 0;
  515. while (j < HID_CUSTOM_TOTAL_ATTRS &&
  516. hid_custom_attrs[j].name) {
  517. struct device_attribute *device_attr;
  518. device_attr = &sensor_inst->fields[i].sd_attrs[j];
  519. snprintf((char *)&sensor_inst->fields[i].attr_name[j],
  520. HID_CUSTOM_NAME_LENGTH, "%s-%s",
  521. sensor_inst->fields[i].group_name,
  522. hid_custom_attrs[j].name);
  523. sysfs_attr_init(&device_attr->attr);
  524. device_attr->attr.name =
  525. (char *)&sensor_inst->fields[i].attr_name[j];
  526. device_attr->attr.mode = hid_custom_attrs[j].mode;
  527. device_attr->show = show_value;
  528. if (hid_custom_attrs[j].mode & S_IWUSR)
  529. device_attr->store = store_value;
  530. sensor_inst->fields[i].attrs[j] = &device_attr->attr;
  531. ++j;
  532. }
  533. sensor_inst->fields[i].attrs[j] = NULL;
  534. sensor_inst->fields[i].hid_custom_attribute_group.attrs =
  535. sensor_inst->fields[i].attrs;
  536. sensor_inst->fields[i].hid_custom_attribute_group.name =
  537. sensor_inst->fields[i].group_name;
  538. ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
  539. &sensor_inst->fields[i].
  540. hid_custom_attribute_group);
  541. if (ret)
  542. break;
  543. /* For power or report field store indexes */
  544. if (sensor_inst->fields[i].attribute.attrib_id ==
  545. HID_USAGE_SENSOR_PROY_POWER_STATE)
  546. sensor_inst->power_state = &sensor_inst->fields[i];
  547. else if (sensor_inst->fields[i].attribute.attrib_id ==
  548. HID_USAGE_SENSOR_PROP_REPORT_STATE)
  549. sensor_inst->report_state = &sensor_inst->fields[i];
  550. }
  551. return ret;
  552. }
  553. static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom *
  554. sensor_inst)
  555. {
  556. int i;
  557. for (i = 0; i < sensor_inst->sensor_field_count; ++i)
  558. sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
  559. &sensor_inst->fields[i].
  560. hid_custom_attribute_group);
  561. kfree(sensor_inst->fields);
  562. }
  563. static ssize_t hid_sensor_custom_read(struct file *file, char __user *buf,
  564. size_t count, loff_t *f_ps)
  565. {
  566. struct hid_sensor_custom *sensor_inst;
  567. unsigned int copied;
  568. int ret;
  569. sensor_inst = container_of(file->private_data,
  570. struct hid_sensor_custom, custom_dev);
  571. if (count < sizeof(struct hid_sensor_sample))
  572. return -EINVAL;
  573. do {
  574. if (kfifo_is_empty(&sensor_inst->data_fifo)) {
  575. if (file->f_flags & O_NONBLOCK)
  576. return -EAGAIN;
  577. ret = wait_event_interruptible(sensor_inst->wait,
  578. !kfifo_is_empty(&sensor_inst->data_fifo));
  579. if (ret)
  580. return ret;
  581. }
  582. ret = kfifo_to_user(&sensor_inst->data_fifo, buf, count,
  583. &copied);
  584. if (ret)
  585. return ret;
  586. } while (copied == 0);
  587. return copied;
  588. }
  589. static int hid_sensor_custom_release(struct inode *inode, struct file *file)
  590. {
  591. struct hid_sensor_custom *sensor_inst;
  592. sensor_inst = container_of(file->private_data,
  593. struct hid_sensor_custom, custom_dev);
  594. clear_bit(0, &sensor_inst->misc_opened);
  595. return 0;
  596. }
  597. static int hid_sensor_custom_open(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. /* We essentially have single reader and writer */
  603. if (test_and_set_bit(0, &sensor_inst->misc_opened))
  604. return -EBUSY;
  605. return stream_open(inode, file);
  606. }
  607. static __poll_t hid_sensor_custom_poll(struct file *file,
  608. struct poll_table_struct *wait)
  609. {
  610. struct hid_sensor_custom *sensor_inst;
  611. __poll_t mask = 0;
  612. sensor_inst = container_of(file->private_data,
  613. struct hid_sensor_custom, custom_dev);
  614. poll_wait(file, &sensor_inst->wait, wait);
  615. if (!kfifo_is_empty(&sensor_inst->data_fifo))
  616. mask = EPOLLIN | EPOLLRDNORM;
  617. return mask;
  618. }
  619. static const struct file_operations hid_sensor_custom_fops = {
  620. .open = hid_sensor_custom_open,
  621. .read = hid_sensor_custom_read,
  622. .release = hid_sensor_custom_release,
  623. .poll = hid_sensor_custom_poll,
  624. .llseek = noop_llseek,
  625. };
  626. static int hid_sensor_custom_dev_if_add(struct hid_sensor_custom *sensor_inst)
  627. {
  628. int ret;
  629. ret = kfifo_alloc(&sensor_inst->data_fifo, HID_CUSTOM_FIFO_SIZE,
  630. GFP_KERNEL);
  631. if (ret)
  632. return ret;
  633. init_waitqueue_head(&sensor_inst->wait);
  634. sensor_inst->custom_dev.minor = MISC_DYNAMIC_MINOR;
  635. sensor_inst->custom_dev.name = dev_name(&sensor_inst->pdev->dev);
  636. sensor_inst->custom_dev.fops = &hid_sensor_custom_fops;
  637. ret = misc_register(&sensor_inst->custom_dev);
  638. if (ret) {
  639. kfifo_free(&sensor_inst->data_fifo);
  640. return ret;
  641. }
  642. return 0;
  643. }
  644. static void hid_sensor_custom_dev_if_remove(struct hid_sensor_custom
  645. *sensor_inst)
  646. {
  647. wake_up(&sensor_inst->wait);
  648. misc_deregister(&sensor_inst->custom_dev);
  649. kfifo_free(&sensor_inst->data_fifo);
  650. }
  651. /*
  652. * Match a known custom sensor.
  653. * tag and luid is mandatory.
  654. */
  655. struct hid_sensor_custom_match {
  656. const char *tag;
  657. const char *luid;
  658. const char *model;
  659. const char *manufacturer;
  660. bool check_dmi;
  661. struct dmi_system_id dmi;
  662. };
  663. /*
  664. * Custom sensor properties used for matching.
  665. */
  666. struct hid_sensor_custom_properties {
  667. u16 serial_num[HID_CUSTOM_MAX_FEATURE_BYTES];
  668. u16 model[HID_CUSTOM_MAX_FEATURE_BYTES];
  669. u16 manufacturer[HID_CUSTOM_MAX_FEATURE_BYTES];
  670. };
  671. static const struct hid_sensor_custom_match hid_sensor_custom_known_table[] = {
  672. /*
  673. * Intel Integrated Sensor Hub (ISH)
  674. */
  675. { /* Intel ISH hinge */
  676. .tag = "INT",
  677. .luid = "020B000000000000",
  678. .manufacturer = "INTEL",
  679. },
  680. /*
  681. * Lenovo Intelligent Sensing Solution (LISS)
  682. */
  683. { /* ambient light */
  684. .tag = "LISS",
  685. .luid = "0041010200000082",
  686. .model = "STK3X3X Sensor",
  687. .manufacturer = "Vendor 258",
  688. .check_dmi = true,
  689. .dmi.matches = {
  690. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  691. }
  692. },
  693. { /* human presence */
  694. .tag = "LISS",
  695. .luid = "0226000171AC0081",
  696. .model = "VL53L1_HOD Sensor",
  697. .manufacturer = "ST_MICRO",
  698. .check_dmi = true,
  699. .dmi.matches = {
  700. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  701. }
  702. },
  703. {}
  704. };
  705. static bool hid_sensor_custom_prop_match_str(const u16 *prop, const char *match,
  706. size_t count)
  707. {
  708. while (count-- && *prop && *match) {
  709. if (*prop != (u16) *match)
  710. return false;
  711. prop++;
  712. match++;
  713. }
  714. return (count == -1) || *prop == (u16)*match;
  715. }
  716. static int hid_sensor_custom_get_prop(struct hid_sensor_hub_device *hsdev,
  717. u32 prop_usage_id, size_t prop_size,
  718. u16 *prop)
  719. {
  720. struct hid_sensor_hub_attribute_info prop_attr = { 0 };
  721. int ret;
  722. memset(prop, 0, prop_size);
  723. ret = sensor_hub_input_get_attribute_info(hsdev, HID_FEATURE_REPORT,
  724. hsdev->usage, prop_usage_id,
  725. &prop_attr);
  726. if (ret < 0)
  727. return ret;
  728. ret = sensor_hub_get_feature(hsdev, prop_attr.report_id,
  729. prop_attr.index, prop_size, prop);
  730. if (ret < 0) {
  731. hid_err(hsdev->hdev, "Failed to get sensor property %08x %d\n",
  732. prop_usage_id, ret);
  733. return ret;
  734. }
  735. return 0;
  736. }
  737. static bool
  738. hid_sensor_custom_do_match(struct hid_sensor_hub_device *hsdev,
  739. const struct hid_sensor_custom_match *match,
  740. const struct hid_sensor_custom_properties *prop)
  741. {
  742. struct dmi_system_id dmi[] = { match->dmi, { 0 } };
  743. if (!hid_sensor_custom_prop_match_str(prop->serial_num, "LUID:", 5) ||
  744. !hid_sensor_custom_prop_match_str(prop->serial_num + 5, match->luid,
  745. HID_CUSTOM_MAX_FEATURE_BYTES - 5))
  746. return false;
  747. if (match->model &&
  748. !hid_sensor_custom_prop_match_str(prop->model, match->model,
  749. HID_CUSTOM_MAX_FEATURE_BYTES))
  750. return false;
  751. if (match->manufacturer &&
  752. !hid_sensor_custom_prop_match_str(prop->manufacturer, match->manufacturer,
  753. HID_CUSTOM_MAX_FEATURE_BYTES))
  754. return false;
  755. if (match->check_dmi && !dmi_check_system(dmi))
  756. return false;
  757. return true;
  758. }
  759. static int
  760. hid_sensor_custom_properties_get(struct hid_sensor_hub_device *hsdev,
  761. struct hid_sensor_custom_properties *prop)
  762. {
  763. int ret;
  764. ret = hid_sensor_custom_get_prop(hsdev,
  765. HID_USAGE_SENSOR_PROP_SERIAL_NUM,
  766. HID_CUSTOM_MAX_FEATURE_BYTES,
  767. prop->serial_num);
  768. if (ret < 0)
  769. return ret;
  770. /*
  771. * Ignore errors on the following model and manufacturer properties.
  772. * Because these are optional, it is not an error if they are missing.
  773. */
  774. hid_sensor_custom_get_prop(hsdev, HID_USAGE_SENSOR_PROP_MODEL,
  775. HID_CUSTOM_MAX_FEATURE_BYTES,
  776. prop->model);
  777. hid_sensor_custom_get_prop(hsdev, HID_USAGE_SENSOR_PROP_MANUFACTURER,
  778. HID_CUSTOM_MAX_FEATURE_BYTES,
  779. prop->manufacturer);
  780. return 0;
  781. }
  782. static int
  783. hid_sensor_custom_get_known(struct hid_sensor_hub_device *hsdev,
  784. const struct hid_sensor_custom_match **known)
  785. {
  786. int ret;
  787. const struct hid_sensor_custom_match *match =
  788. hid_sensor_custom_known_table;
  789. struct hid_sensor_custom_properties *prop;
  790. prop = kmalloc(sizeof(struct hid_sensor_custom_properties), GFP_KERNEL);
  791. if (!prop)
  792. return -ENOMEM;
  793. ret = hid_sensor_custom_properties_get(hsdev, prop);
  794. if (ret < 0)
  795. goto out;
  796. while (match->tag) {
  797. if (hid_sensor_custom_do_match(hsdev, match, prop)) {
  798. *known = match;
  799. ret = 0;
  800. goto out;
  801. }
  802. match++;
  803. }
  804. ret = -ENODATA;
  805. out:
  806. kfree(prop);
  807. return ret;
  808. }
  809. static struct platform_device *
  810. hid_sensor_register_platform_device(struct platform_device *pdev,
  811. struct hid_sensor_hub_device *hsdev,
  812. const struct hid_sensor_custom_match *match)
  813. {
  814. char real_usage[HID_SENSOR_USAGE_LENGTH] = { 0 };
  815. struct platform_device *custom_pdev;
  816. const char *dev_name;
  817. char *c;
  818. memcpy(real_usage, match->luid, 4);
  819. /* usage id are all lowcase */
  820. for (c = real_usage; *c != '\0'; c++)
  821. *c = tolower(*c);
  822. /* HID-SENSOR-TAG-REAL_USAGE_ID */
  823. dev_name = kasprintf(GFP_KERNEL, "HID-SENSOR-%s-%s",
  824. match->tag, real_usage);
  825. if (!dev_name)
  826. return ERR_PTR(-ENOMEM);
  827. custom_pdev = platform_device_register_data(pdev->dev.parent, dev_name,
  828. PLATFORM_DEVID_AUTO, hsdev,
  829. sizeof(*hsdev));
  830. kfree(dev_name);
  831. return custom_pdev;
  832. }
  833. static int hid_sensor_custom_probe(struct platform_device *pdev)
  834. {
  835. struct hid_sensor_custom *sensor_inst;
  836. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  837. int ret;
  838. const struct hid_sensor_custom_match *match;
  839. sensor_inst = devm_kzalloc(&pdev->dev, sizeof(*sensor_inst),
  840. GFP_KERNEL);
  841. if (!sensor_inst)
  842. return -ENOMEM;
  843. sensor_inst->callbacks.capture_sample = hid_sensor_capture_sample;
  844. sensor_inst->callbacks.send_event = hid_sensor_send_event;
  845. sensor_inst->callbacks.pdev = pdev;
  846. sensor_inst->hsdev = hsdev;
  847. sensor_inst->pdev = pdev;
  848. mutex_init(&sensor_inst->mutex);
  849. platform_set_drvdata(pdev, sensor_inst);
  850. ret = hid_sensor_custom_get_known(hsdev, &match);
  851. if (!ret) {
  852. sensor_inst->custom_pdev =
  853. hid_sensor_register_platform_device(pdev, hsdev, match);
  854. ret = PTR_ERR_OR_ZERO(sensor_inst->custom_pdev);
  855. if (ret) {
  856. dev_err(&pdev->dev,
  857. "register_platform_device failed\n");
  858. return ret;
  859. }
  860. return 0;
  861. }
  862. ret = sensor_hub_register_callback(hsdev, hsdev->usage,
  863. &sensor_inst->callbacks);
  864. if (ret < 0) {
  865. dev_err(&pdev->dev, "callback reg failed\n");
  866. return ret;
  867. }
  868. ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
  869. &enable_sensor_attr_group);
  870. if (ret)
  871. goto err_remove_callback;
  872. ret = hid_sensor_custom_add_attributes(sensor_inst);
  873. if (ret)
  874. goto err_remove_group;
  875. ret = hid_sensor_custom_dev_if_add(sensor_inst);
  876. if (ret)
  877. goto err_remove_attributes;
  878. return 0;
  879. err_remove_attributes:
  880. hid_sensor_custom_remove_attributes(sensor_inst);
  881. err_remove_group:
  882. sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
  883. &enable_sensor_attr_group);
  884. err_remove_callback:
  885. sensor_hub_remove_callback(hsdev, hsdev->usage);
  886. return ret;
  887. }
  888. static void hid_sensor_custom_remove(struct platform_device *pdev)
  889. {
  890. struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
  891. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  892. if (sensor_inst->custom_pdev) {
  893. platform_device_unregister(sensor_inst->custom_pdev);
  894. return;
  895. }
  896. hid_sensor_custom_dev_if_remove(sensor_inst);
  897. hid_sensor_custom_remove_attributes(sensor_inst);
  898. sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
  899. &enable_sensor_attr_group);
  900. sensor_hub_remove_callback(hsdev, hsdev->usage);
  901. }
  902. static const struct platform_device_id hid_sensor_custom_ids[] = {
  903. {
  904. .name = "HID-SENSOR-2000e1",
  905. },
  906. {
  907. .name = "HID-SENSOR-2000e2",
  908. },
  909. { /* sentinel */ }
  910. };
  911. MODULE_DEVICE_TABLE(platform, hid_sensor_custom_ids);
  912. static struct platform_driver hid_sensor_custom_platform_driver = {
  913. .id_table = hid_sensor_custom_ids,
  914. .driver = {
  915. .name = KBUILD_MODNAME,
  916. },
  917. .probe = hid_sensor_custom_probe,
  918. .remove_new = hid_sensor_custom_remove,
  919. };
  920. module_platform_driver(hid_sensor_custom_platform_driver);
  921. MODULE_DESCRIPTION("HID Sensor Custom and Generic sensor Driver");
  922. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  923. MODULE_LICENSE("GPL");