Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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