dummy_stm.c 2.6 KB

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