p_sys-t.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MIPI SyS-T framing protocol for STM devices.
  4. * Copyright (c) 2018, Intel Corporation.
  5. */
  6. #include <linux/configfs.h>
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/slab.h>
  10. #include <linux/uuid.h>
  11. #include <linux/stm.h>
  12. #include "stm.h"
  13. enum sys_t_message_type {
  14. MIPI_SYST_TYPE_BUILD = 0,
  15. MIPI_SYST_TYPE_SHORT32,
  16. MIPI_SYST_TYPE_STRING,
  17. MIPI_SYST_TYPE_CATALOG,
  18. MIPI_SYST_TYPE_RAW = 6,
  19. MIPI_SYST_TYPE_SHORT64,
  20. MIPI_SYST_TYPE_CLOCK,
  21. };
  22. enum sys_t_message_severity {
  23. MIPI_SYST_SEVERITY_MAX = 0,
  24. MIPI_SYST_SEVERITY_FATAL,
  25. MIPI_SYST_SEVERITY_ERROR,
  26. MIPI_SYST_SEVERITY_WARNING,
  27. MIPI_SYST_SEVERITY_INFO,
  28. MIPI_SYST_SEVERITY_USER1,
  29. MIPI_SYST_SEVERITY_USER2,
  30. MIPI_SYST_SEVERITY_DEBUG,
  31. };
  32. enum sys_t_message_build_subtype {
  33. MIPI_SYST_BUILD_ID_COMPACT32 = 0,
  34. MIPI_SYST_BUILD_ID_COMPACT64,
  35. MIPI_SYST_BUILD_ID_LONG,
  36. };
  37. enum sys_t_message_clock_subtype {
  38. MIPI_SYST_CLOCK_TRANSPORT_SYNC = 1,
  39. };
  40. enum sys_t_message_string_subtype {
  41. MIPI_SYST_STRING_GENERIC = 1,
  42. MIPI_SYST_STRING_FUNCTIONENTER,
  43. MIPI_SYST_STRING_FUNCTIONEXIT,
  44. MIPI_SYST_STRING_INVALIDPARAM = 5,
  45. MIPI_SYST_STRING_ASSERT = 7,
  46. MIPI_SYST_STRING_PRINTF_32 = 11,
  47. MIPI_SYST_STRING_PRINTF_64 = 12,
  48. };
  49. #define MIPI_SYST_TYPE(t) ((u32)(MIPI_SYST_TYPE_ ## t))
  50. #define MIPI_SYST_SEVERITY(s) ((u32)(MIPI_SYST_SEVERITY_ ## s) << 4)
  51. #define MIPI_SYST_OPT_LOC BIT(8)
  52. #define MIPI_SYST_OPT_LEN BIT(9)
  53. #define MIPI_SYST_OPT_CHK BIT(10)
  54. #define MIPI_SYST_OPT_TS BIT(11)
  55. #define MIPI_SYST_UNIT(u) ((u32)(u) << 12)
  56. #define MIPI_SYST_ORIGIN(o) ((u32)(o) << 16)
  57. #define MIPI_SYST_OPT_GUID BIT(23)
  58. #define MIPI_SYST_SUBTYPE(s) ((u32)(MIPI_SYST_ ## s) << 24)
  59. #define MIPI_SYST_UNITLARGE(u) (MIPI_SYST_UNIT(u & 0xf) | \
  60. MIPI_SYST_ORIGIN(u >> 4))
  61. #define MIPI_SYST_TYPES(t, s) (MIPI_SYST_TYPE(t) | \
  62. MIPI_SYST_SUBTYPE(t ## _ ## s))
  63. #define DATA_HEADER (MIPI_SYST_TYPES(STRING, GENERIC) | \
  64. MIPI_SYST_SEVERITY(INFO) | \
  65. MIPI_SYST_OPT_GUID)
  66. #define CLOCK_SYNC_HEADER (MIPI_SYST_TYPES(CLOCK, TRANSPORT_SYNC) | \
  67. MIPI_SYST_SEVERITY(MAX))
  68. struct sys_t_policy_node {
  69. uuid_t uuid;
  70. bool do_len;
  71. unsigned long ts_interval;
  72. unsigned long clocksync_interval;
  73. };
  74. struct sys_t_output {
  75. struct sys_t_policy_node node;
  76. unsigned long ts_jiffies;
  77. unsigned long clocksync_jiffies;
  78. };
  79. static void sys_t_policy_node_init(void *priv)
  80. {
  81. struct sys_t_policy_node *pn = priv;
  82. uuid_gen(&pn->uuid);
  83. }
  84. static int sys_t_output_open(void *priv, struct stm_output *output)
  85. {
  86. struct sys_t_policy_node *pn = priv;
  87. struct sys_t_output *opriv;
  88. opriv = kzalloc(sizeof(*opriv), GFP_ATOMIC);
  89. if (!opriv)
  90. return -ENOMEM;
  91. memcpy(&opriv->node, pn, sizeof(opriv->node));
  92. output->pdrv_private = opriv;
  93. return 0;
  94. }
  95. static void sys_t_output_close(struct stm_output *output)
  96. {
  97. kfree(output->pdrv_private);
  98. }
  99. static ssize_t sys_t_policy_uuid_show(struct config_item *item,
  100. char *page)
  101. {
  102. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  103. return sprintf(page, "%pU\n", &pn->uuid);
  104. }
  105. static ssize_t
  106. sys_t_policy_uuid_store(struct config_item *item, const char *page,
  107. size_t count)
  108. {
  109. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  110. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  111. int ret;
  112. mutex_lock(mutexp);
  113. ret = uuid_parse(page, &pn->uuid);
  114. mutex_unlock(mutexp);
  115. return ret < 0 ? ret : count;
  116. }
  117. CONFIGFS_ATTR(sys_t_policy_, uuid);
  118. static ssize_t sys_t_policy_do_len_show(struct config_item *item,
  119. char *page)
  120. {
  121. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  122. return sprintf(page, "%d\n", pn->do_len);
  123. }
  124. static ssize_t
  125. sys_t_policy_do_len_store(struct config_item *item, const char *page,
  126. size_t count)
  127. {
  128. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  129. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  130. int ret;
  131. mutex_lock(mutexp);
  132. ret = kstrtobool(page, &pn->do_len);
  133. mutex_unlock(mutexp);
  134. return ret ? ret : count;
  135. }
  136. CONFIGFS_ATTR(sys_t_policy_, do_len);
  137. static ssize_t sys_t_policy_ts_interval_show(struct config_item *item,
  138. char *page)
  139. {
  140. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  141. return sprintf(page, "%u\n", jiffies_to_msecs(pn->ts_interval));
  142. }
  143. static ssize_t
  144. sys_t_policy_ts_interval_store(struct config_item *item, const char *page,
  145. size_t count)
  146. {
  147. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  148. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  149. unsigned int ms;
  150. int ret;
  151. mutex_lock(mutexp);
  152. ret = kstrtouint(page, 10, &ms);
  153. mutex_unlock(mutexp);
  154. if (!ret) {
  155. pn->ts_interval = msecs_to_jiffies(ms);
  156. return count;
  157. }
  158. return ret;
  159. }
  160. CONFIGFS_ATTR(sys_t_policy_, ts_interval);
  161. static ssize_t sys_t_policy_clocksync_interval_show(struct config_item *item,
  162. char *page)
  163. {
  164. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  165. return sprintf(page, "%u\n", jiffies_to_msecs(pn->clocksync_interval));
  166. }
  167. static ssize_t
  168. sys_t_policy_clocksync_interval_store(struct config_item *item,
  169. const char *page, size_t count)
  170. {
  171. struct mutex *mutexp = &item->ci_group->cg_subsys->su_mutex;
  172. struct sys_t_policy_node *pn = to_pdrv_policy_node(item);
  173. unsigned int ms;
  174. int ret;
  175. mutex_lock(mutexp);
  176. ret = kstrtouint(page, 10, &ms);
  177. mutex_unlock(mutexp);
  178. if (!ret) {
  179. pn->clocksync_interval = msecs_to_jiffies(ms);
  180. return count;
  181. }
  182. return ret;
  183. }
  184. CONFIGFS_ATTR(sys_t_policy_, clocksync_interval);
  185. static struct configfs_attribute *sys_t_policy_attrs[] = {
  186. &sys_t_policy_attr_uuid,
  187. &sys_t_policy_attr_do_len,
  188. &sys_t_policy_attr_ts_interval,
  189. &sys_t_policy_attr_clocksync_interval,
  190. NULL,
  191. };
  192. static inline bool sys_t_need_ts(struct sys_t_output *op)
  193. {
  194. if (op->node.ts_interval &&
  195. time_after(jiffies, op->ts_jiffies + op->node.ts_interval)) {
  196. op->ts_jiffies = jiffies;
  197. return true;
  198. }
  199. return false;
  200. }
  201. static bool sys_t_need_clock_sync(struct sys_t_output *op)
  202. {
  203. if (op->node.clocksync_interval &&
  204. time_after(jiffies,
  205. op->clocksync_jiffies + op->node.clocksync_interval)) {
  206. op->clocksync_jiffies = jiffies;
  207. return true;
  208. }
  209. return false;
  210. }
  211. static ssize_t
  212. sys_t_clock_sync(struct stm_data *data, unsigned int m, unsigned int c)
  213. {
  214. u32 header = CLOCK_SYNC_HEADER;
  215. const unsigned char nil = 0;
  216. u64 payload[2]; /* Clock value and frequency */
  217. ssize_t sz;
  218. sz = data->packet(data, m, c, STP_PACKET_DATA, STP_PACKET_TIMESTAMPED,
  219. 4, (u8 *)&header);
  220. if (sz <= 0)
  221. return sz;
  222. payload[0] = ktime_get_real_ns();
  223. payload[1] = NSEC_PER_SEC;
  224. sz = stm_data_write(data, m, c, false, &payload, sizeof(payload));
  225. if (sz <= 0)
  226. return sz;
  227. data->packet(data, m, c, STP_PACKET_FLAG, 0, 0, &nil);
  228. return sizeof(header) + sizeof(payload);
  229. }
  230. static ssize_t sys_t_write(struct stm_data *data, struct stm_output *output,
  231. unsigned int chan, const char *buf, size_t count)
  232. {
  233. struct sys_t_output *op = output->pdrv_private;
  234. unsigned int c = output->channel + chan;
  235. unsigned int m = output->master;
  236. const unsigned char nil = 0;
  237. u32 header = DATA_HEADER;
  238. u8 uuid[UUID_SIZE];
  239. ssize_t sz;
  240. /* We require an existing policy node to proceed */
  241. if (!op)
  242. return -EINVAL;
  243. if (sys_t_need_clock_sync(op)) {
  244. sz = sys_t_clock_sync(data, m, c);
  245. if (sz <= 0)
  246. return sz;
  247. }
  248. if (op->node.do_len)
  249. header |= MIPI_SYST_OPT_LEN;
  250. if (sys_t_need_ts(op))
  251. header |= MIPI_SYST_OPT_TS;
  252. /*
  253. * STP framing rules for SyS-T frames:
  254. * * the first packet of the SyS-T frame is timestamped;
  255. * * the last packet is a FLAG.
  256. */
  257. /* Message layout: HEADER / GUID / [LENGTH /][TIMESTAMP /] DATA */
  258. /* HEADER */
  259. sz = data->packet(data, m, c, STP_PACKET_DATA, STP_PACKET_TIMESTAMPED,
  260. 4, (u8 *)&header);
  261. if (sz <= 0)
  262. return sz;
  263. /* GUID */
  264. export_uuid(uuid, &op->node.uuid);
  265. sz = stm_data_write(data, m, c, false, uuid, sizeof(op->node.uuid));
  266. if (sz <= 0)
  267. return sz;
  268. /* [LENGTH] */
  269. if (op->node.do_len) {
  270. u16 length = count;
  271. sz = data->packet(data, m, c, STP_PACKET_DATA, 0, 2,
  272. (u8 *)&length);
  273. if (sz <= 0)
  274. return sz;
  275. }
  276. /* [TIMESTAMP] */
  277. if (header & MIPI_SYST_OPT_TS) {
  278. u64 ts = ktime_get_real_ns();
  279. sz = stm_data_write(data, m, c, false, &ts, sizeof(ts));
  280. if (sz <= 0)
  281. return sz;
  282. }
  283. /* DATA */
  284. sz = stm_data_write(data, m, c, false, buf, count);
  285. if (sz > 0)
  286. data->packet(data, m, c, STP_PACKET_FLAG, 0, 0, &nil);
  287. return sz;
  288. }
  289. static const struct stm_protocol_driver sys_t_pdrv = {
  290. .owner = THIS_MODULE,
  291. .name = "p_sys-t",
  292. .priv_sz = sizeof(struct sys_t_policy_node),
  293. .write = sys_t_write,
  294. .policy_attr = sys_t_policy_attrs,
  295. .policy_node_init = sys_t_policy_node_init,
  296. .output_open = sys_t_output_open,
  297. .output_close = sys_t_output_close,
  298. };
  299. static int sys_t_stm_init(void)
  300. {
  301. return stm_register_protocol(&sys_t_pdrv);
  302. }
  303. static void sys_t_stm_exit(void)
  304. {
  305. stm_unregister_protocol(&sys_t_pdrv);
  306. }
  307. module_init(sys_t_stm_init);
  308. module_exit(sys_t_stm_exit);
  309. MODULE_LICENSE("GPL v2");
  310. MODULE_DESCRIPTION("MIPI SyS-T STM framing protocol driver");
  311. MODULE_AUTHOR("Alexander Shishkin <[email protected]>");