blob: 38528ffdc0b3f7869b73e0a59677a9e0399ca3d7 [file] [log] [blame]
Alexander Shishkin9ea393d2018-03-28 18:43:57 +03001// SPDX-License-Identifier: GPL-2.0
Alexander Shishkin2c415382015-09-22 15:47:12 +03002/*
3 * A dummy STM device for stm/stm_source class testing.
4 * Copyright (c) 2014, Intel Corporation.
5 *
Alexander Shishkin2c415382015-09-22 15:47:12 +03006 * STM class implements generic infrastructure for System Trace Module devices
7 * as defined in MIPI STPv2 specification.
8 */
9
10#undef DEBUG
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/stm.h>
Alexander Shishkin4f0c7c62018-02-23 13:19:43 +020015#include <uapi/linux/stm.h>
Alexander Shishkin2c415382015-09-22 15:47:12 +030016
Chunyan Zhang9b5e3ff2016-11-21 15:57:22 +080017static ssize_t notrace
Alexander Shishkin2c415382015-09-22 15:47:12 +030018dummy_stm_packet(struct stm_data *stm_data, unsigned int master,
19 unsigned int channel, unsigned int packet, unsigned int flags,
20 unsigned int size, const unsigned char *payload)
21{
22#ifdef DEBUG
23 u64 pl = 0;
24
25 if (payload)
26 pl = *(u64 *)payload;
27
28 if (size < 8)
29 pl &= (1ull << (size * 8)) - 1;
30 trace_printk("[%u:%u] [pkt: %x/%x] (%llx)\n", master, channel,
31 packet, size, pl);
32#endif
33 return size;
34}
35
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +020036#define DUMMY_STM_MAX 32
37
38static struct stm_data dummy_stm[DUMMY_STM_MAX];
39
40static int nr_dummies = 4;
41
Alexander Shishkin118b4512016-03-04 16:22:33 +020042module_param(nr_dummies, int, 0400);
Alexander Shishkin2c415382015-09-22 15:47:12 +030043
Alexander Shishkinadcde632016-02-15 19:12:10 +020044static unsigned int fail_mode;
45
46module_param(fail_mode, int, 0600);
47
Alexander Shishkin4f0c7c62018-02-23 13:19:43 +020048static unsigned int master_min;
49
50module_param(master_min, int, 0400);
51
52static unsigned int master_max = STP_MASTER_MAX;
53
54module_param(master_max, int, 0400);
55
56static unsigned int nr_channels = STP_CHANNEL_MAX;
57
58module_param(nr_channels, int, 0400);
59
Alexander Shishkinadcde632016-02-15 19:12:10 +020060static int dummy_stm_link(struct stm_data *data, unsigned int master,
61 unsigned int channel)
62{
63 if (fail_mode && (channel & fail_mode))
64 return -EINVAL;
65
66 return 0;
67}
68
Alexander Shishkin2c415382015-09-22 15:47:12 +030069static int dummy_stm_init(void)
70{
Alexander Shishkin118b4512016-03-04 16:22:33 +020071 int i, ret = -ENOMEM;
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +020072
Alexander Shishkin118b4512016-03-04 16:22:33 +020073 if (nr_dummies < 0 || nr_dummies > DUMMY_STM_MAX)
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +020074 return -EINVAL;
75
Alexander Shishkin4f0c7c62018-02-23 13:19:43 +020076 if (master_min > master_max ||
77 master_max > STP_MASTER_MAX ||
78 nr_channels > STP_CHANNEL_MAX)
79 return -EINVAL;
80
Alexander Shishkin118b4512016-03-04 16:22:33 +020081 for (i = 0; i < nr_dummies; i++) {
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +020082 dummy_stm[i].name = kasprintf(GFP_KERNEL, "dummy_stm.%d", i);
83 if (!dummy_stm[i].name)
84 goto fail_unregister;
85
Alexander Shishkin4f0c7c62018-02-23 13:19:43 +020086 dummy_stm[i].sw_start = master_min;
87 dummy_stm[i].sw_end = master_max;
88 dummy_stm[i].sw_nchannels = nr_channels;
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +020089 dummy_stm[i].packet = dummy_stm_packet;
Alexander Shishkinadcde632016-02-15 19:12:10 +020090 dummy_stm[i].link = dummy_stm_link;
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +020091
92 ret = stm_register_device(NULL, &dummy_stm[i], THIS_MODULE);
93 if (ret)
94 goto fail_free;
95 }
96
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +020097 return 0;
98
99fail_unregister:
100 for (i--; i >= 0; i--) {
101 stm_unregister_device(&dummy_stm[i]);
102fail_free:
103 kfree(dummy_stm[i].name);
104 }
105
106 return ret;
107
Alexander Shishkin2c415382015-09-22 15:47:12 +0300108}
109
110static void dummy_stm_exit(void)
111{
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +0200112 int i;
113
Alexander Shishkin118b4512016-03-04 16:22:33 +0200114 for (i = 0; i < nr_dummies; i++) {
Alexander Shishkinbcfdf8a2016-02-15 19:12:03 +0200115 stm_unregister_device(&dummy_stm[i]);
116 kfree(dummy_stm[i].name);
117 }
Alexander Shishkin2c415382015-09-22 15:47:12 +0300118}
119
120module_init(dummy_stm_init);
121module_exit(dummy_stm_exit);
122
123MODULE_LICENSE("GPL v2");
124MODULE_DESCRIPTION("dummy_stm device");
125MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");