19ea393d8SAlexander Shishkin // SPDX-License-Identifier: GPL-2.0
22c415381SAlexander Shishkin /*
32c415381SAlexander Shishkin * A dummy STM device for stm/stm_source class testing.
42c415381SAlexander Shishkin * Copyright (c) 2014, Intel Corporation.
52c415381SAlexander Shishkin *
62c415381SAlexander Shishkin * STM class implements generic infrastructure for System Trace Module devices
72c415381SAlexander Shishkin * as defined in MIPI STPv2 specification.
82c415381SAlexander Shishkin */
92c415381SAlexander Shishkin
102c415381SAlexander Shishkin #undef DEBUG
112c415381SAlexander Shishkin #include <linux/kernel.h>
122c415381SAlexander Shishkin #include <linux/module.h>
132c415381SAlexander Shishkin #include <linux/slab.h>
142c415381SAlexander Shishkin #include <linux/stm.h>
15*4f0c7c6aSAlexander Shishkin #include <uapi/linux/stm.h>
162c415381SAlexander Shishkin
179b5e3ff6SChunyan Zhang static ssize_t notrace
dummy_stm_packet(struct stm_data * stm_data,unsigned int master,unsigned int channel,unsigned int packet,unsigned int flags,unsigned int size,const unsigned char * payload)182c415381SAlexander Shishkin dummy_stm_packet(struct stm_data *stm_data, unsigned int master,
192c415381SAlexander Shishkin unsigned int channel, unsigned int packet, unsigned int flags,
202c415381SAlexander Shishkin unsigned int size, const unsigned char *payload)
212c415381SAlexander Shishkin {
222c415381SAlexander Shishkin #ifdef DEBUG
232c415381SAlexander Shishkin u64 pl = 0;
242c415381SAlexander Shishkin
252c415381SAlexander Shishkin if (payload)
262c415381SAlexander Shishkin pl = *(u64 *)payload;
272c415381SAlexander Shishkin
282c415381SAlexander Shishkin if (size < 8)
292c415381SAlexander Shishkin pl &= (1ull << (size * 8)) - 1;
302c415381SAlexander Shishkin trace_printk("[%u:%u] [pkt: %x/%x] (%llx)\n", master, channel,
312c415381SAlexander Shishkin packet, size, pl);
322c415381SAlexander Shishkin #endif
332c415381SAlexander Shishkin return size;
342c415381SAlexander Shishkin }
352c415381SAlexander Shishkin
36bcfdf8afSAlexander Shishkin #define DUMMY_STM_MAX 32
37bcfdf8afSAlexander Shishkin
38bcfdf8afSAlexander Shishkin static struct stm_data dummy_stm[DUMMY_STM_MAX];
39bcfdf8afSAlexander Shishkin
40bcfdf8afSAlexander Shishkin static int nr_dummies = 4;
41bcfdf8afSAlexander Shishkin
42118b4515SAlexander Shishkin module_param(nr_dummies, int, 0400);
432c415381SAlexander Shishkin
44adcde635SAlexander Shishkin static unsigned int fail_mode;
45adcde635SAlexander Shishkin
46adcde635SAlexander Shishkin module_param(fail_mode, int, 0600);
47adcde635SAlexander Shishkin
48*4f0c7c6aSAlexander Shishkin static unsigned int master_min;
49*4f0c7c6aSAlexander Shishkin
50*4f0c7c6aSAlexander Shishkin module_param(master_min, int, 0400);
51*4f0c7c6aSAlexander Shishkin
52*4f0c7c6aSAlexander Shishkin static unsigned int master_max = STP_MASTER_MAX;
53*4f0c7c6aSAlexander Shishkin
54*4f0c7c6aSAlexander Shishkin module_param(master_max, int, 0400);
55*4f0c7c6aSAlexander Shishkin
56*4f0c7c6aSAlexander Shishkin static unsigned int nr_channels = STP_CHANNEL_MAX;
57*4f0c7c6aSAlexander Shishkin
58*4f0c7c6aSAlexander Shishkin module_param(nr_channels, int, 0400);
59*4f0c7c6aSAlexander Shishkin
dummy_stm_link(struct stm_data * data,unsigned int master,unsigned int channel)60adcde635SAlexander Shishkin static int dummy_stm_link(struct stm_data *data, unsigned int master,
61adcde635SAlexander Shishkin unsigned int channel)
62adcde635SAlexander Shishkin {
63adcde635SAlexander Shishkin if (fail_mode && (channel & fail_mode))
64adcde635SAlexander Shishkin return -EINVAL;
65adcde635SAlexander Shishkin
66adcde635SAlexander Shishkin return 0;
67adcde635SAlexander Shishkin }
68adcde635SAlexander Shishkin
dummy_stm_init(void)692c415381SAlexander Shishkin static int dummy_stm_init(void)
702c415381SAlexander Shishkin {
71118b4515SAlexander Shishkin int i, ret = -ENOMEM;
72bcfdf8afSAlexander Shishkin
73118b4515SAlexander Shishkin if (nr_dummies < 0 || nr_dummies > DUMMY_STM_MAX)
74bcfdf8afSAlexander Shishkin return -EINVAL;
75bcfdf8afSAlexander Shishkin
76*4f0c7c6aSAlexander Shishkin if (master_min > master_max ||
77*4f0c7c6aSAlexander Shishkin master_max > STP_MASTER_MAX ||
78*4f0c7c6aSAlexander Shishkin nr_channels > STP_CHANNEL_MAX)
79*4f0c7c6aSAlexander Shishkin return -EINVAL;
80*4f0c7c6aSAlexander Shishkin
81118b4515SAlexander Shishkin for (i = 0; i < nr_dummies; i++) {
82bcfdf8afSAlexander Shishkin dummy_stm[i].name = kasprintf(GFP_KERNEL, "dummy_stm.%d", i);
83bcfdf8afSAlexander Shishkin if (!dummy_stm[i].name)
84bcfdf8afSAlexander Shishkin goto fail_unregister;
85bcfdf8afSAlexander Shishkin
86*4f0c7c6aSAlexander Shishkin dummy_stm[i].sw_start = master_min;
87*4f0c7c6aSAlexander Shishkin dummy_stm[i].sw_end = master_max;
88*4f0c7c6aSAlexander Shishkin dummy_stm[i].sw_nchannels = nr_channels;
89bcfdf8afSAlexander Shishkin dummy_stm[i].packet = dummy_stm_packet;
90adcde635SAlexander Shishkin dummy_stm[i].link = dummy_stm_link;
91bcfdf8afSAlexander Shishkin
92bcfdf8afSAlexander Shishkin ret = stm_register_device(NULL, &dummy_stm[i], THIS_MODULE);
93bcfdf8afSAlexander Shishkin if (ret)
94bcfdf8afSAlexander Shishkin goto fail_free;
95bcfdf8afSAlexander Shishkin }
96bcfdf8afSAlexander Shishkin
97bcfdf8afSAlexander Shishkin return 0;
98bcfdf8afSAlexander Shishkin
99bcfdf8afSAlexander Shishkin fail_unregister:
100bcfdf8afSAlexander Shishkin for (i--; i >= 0; i--) {
101bcfdf8afSAlexander Shishkin stm_unregister_device(&dummy_stm[i]);
102bcfdf8afSAlexander Shishkin fail_free:
103bcfdf8afSAlexander Shishkin kfree(dummy_stm[i].name);
104bcfdf8afSAlexander Shishkin }
105bcfdf8afSAlexander Shishkin
106bcfdf8afSAlexander Shishkin return ret;
107bcfdf8afSAlexander Shishkin
1082c415381SAlexander Shishkin }
1092c415381SAlexander Shishkin
dummy_stm_exit(void)1102c415381SAlexander Shishkin static void dummy_stm_exit(void)
1112c415381SAlexander Shishkin {
112bcfdf8afSAlexander Shishkin int i;
113bcfdf8afSAlexander Shishkin
114118b4515SAlexander Shishkin for (i = 0; i < nr_dummies; i++) {
115bcfdf8afSAlexander Shishkin stm_unregister_device(&dummy_stm[i]);
116bcfdf8afSAlexander Shishkin kfree(dummy_stm[i].name);
117bcfdf8afSAlexander Shishkin }
1182c415381SAlexander Shishkin }
1192c415381SAlexander Shishkin
1202c415381SAlexander Shishkin module_init(dummy_stm_init);
1212c415381SAlexander Shishkin module_exit(dummy_stm_exit);
1222c415381SAlexander Shishkin
1232c415381SAlexander Shishkin MODULE_LICENSE("GPL v2");
1242c415381SAlexander Shishkin MODULE_DESCRIPTION("dummy_stm device");
1252c415381SAlexander Shishkin MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
126