^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) * A dummy STM device for stm/stm_source class testing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2014, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * STM class implements generic infrastructure for System Trace Module devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * as defined in MIPI STPv2 specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #undef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/stm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <uapi/linux/stm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static ssize_t notrace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) dummy_stm_packet(struct stm_data *stm_data, unsigned int master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned int channel, unsigned int packet, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned int size, const unsigned char *payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u64 pl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) pl = *(u64 *)payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (size < 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) pl &= (1ull << (size * 8)) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) trace_printk("[%u:%u] [pkt: %x/%x] (%llx)\n", master, channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) packet, size, pl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return size;
^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) #define DUMMY_STM_MAX 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static struct stm_data dummy_stm[DUMMY_STM_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int nr_dummies = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) module_param(nr_dummies, int, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static unsigned int fail_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) module_param(fail_mode, int, 0600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static unsigned int master_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) module_param(master_min, int, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static unsigned int master_max = STP_MASTER_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) module_param(master_max, int, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static unsigned int nr_channels = STP_CHANNEL_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) module_param(nr_channels, int, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int dummy_stm_link(struct stm_data *data, unsigned int master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (fail_mode && (channel & fail_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int dummy_stm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int i, ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (nr_dummies < 0 || nr_dummies > DUMMY_STM_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (master_min > master_max ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) master_max > STP_MASTER_MAX ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) nr_channels > STP_CHANNEL_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) for (i = 0; i < nr_dummies; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dummy_stm[i].name = kasprintf(GFP_KERNEL, "dummy_stm.%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!dummy_stm[i].name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto fail_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) dummy_stm[i].sw_start = master_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) dummy_stm[i].sw_end = master_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) dummy_stm[i].sw_nchannels = nr_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dummy_stm[i].packet = dummy_stm_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) dummy_stm[i].link = dummy_stm_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ret = stm_register_device(NULL, &dummy_stm[i], THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto fail_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) fail_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) for (i--; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) stm_unregister_device(&dummy_stm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) fail_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) kfree(dummy_stm[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static void dummy_stm_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (i = 0; i < nr_dummies; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) stm_unregister_device(&dummy_stm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) kfree(dummy_stm[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) module_init(dummy_stm_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) module_exit(dummy_stm_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) MODULE_DESCRIPTION("dummy_stm device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");