dummy_stm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * A dummy STM device for stm/stm_source class testing.
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * STM class implements generic infrastructure for System Trace Module devices
  15. * as defined in MIPI STPv2 specification.
  16. */
  17. #undef DEBUG
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/stm.h>
  22. static ssize_t
  23. dummy_stm_packet(struct stm_data *stm_data, unsigned int master,
  24. unsigned int channel, unsigned int packet, unsigned int flags,
  25. unsigned int size, const unsigned char *payload)
  26. {
  27. #ifdef DEBUG
  28. u64 pl = 0;
  29. if (payload)
  30. pl = *(u64 *)payload;
  31. if (size < 8)
  32. pl &= (1ull << (size * 8)) - 1;
  33. trace_printk("[%u:%u] [pkt: %x/%x] (%llx)\n", master, channel,
  34. packet, size, pl);
  35. #endif
  36. return size;
  37. }
  38. #define DUMMY_STM_MAX 32
  39. static struct stm_data dummy_stm[DUMMY_STM_MAX];
  40. static int nr_dummies = 4;
  41. module_param(nr_dummies, int, 0600);
  42. static unsigned int dummy_stm_nr;
  43. static unsigned int fail_mode;
  44. module_param(fail_mode, int, 0600);
  45. static int dummy_stm_link(struct stm_data *data, unsigned int master,
  46. unsigned int channel)
  47. {
  48. if (fail_mode && (channel & fail_mode))
  49. return -EINVAL;
  50. return 0;
  51. }
  52. static int dummy_stm_init(void)
  53. {
  54. int i, ret = -ENOMEM, __nr_dummies = ACCESS_ONCE(nr_dummies);
  55. if (__nr_dummies < 0 || __nr_dummies > DUMMY_STM_MAX)
  56. return -EINVAL;
  57. for (i = 0; i < __nr_dummies; i++) {
  58. dummy_stm[i].name = kasprintf(GFP_KERNEL, "dummy_stm.%d", i);
  59. if (!dummy_stm[i].name)
  60. goto fail_unregister;
  61. dummy_stm[i].sw_start = 0x0000;
  62. dummy_stm[i].sw_end = 0xffff;
  63. dummy_stm[i].sw_nchannels = 0xffff;
  64. dummy_stm[i].packet = dummy_stm_packet;
  65. dummy_stm[i].link = dummy_stm_link;
  66. ret = stm_register_device(NULL, &dummy_stm[i], THIS_MODULE);
  67. if (ret)
  68. goto fail_free;
  69. }
  70. dummy_stm_nr = __nr_dummies;
  71. return 0;
  72. fail_unregister:
  73. for (i--; i >= 0; i--) {
  74. stm_unregister_device(&dummy_stm[i]);
  75. fail_free:
  76. kfree(dummy_stm[i].name);
  77. }
  78. return ret;
  79. }
  80. static void dummy_stm_exit(void)
  81. {
  82. int i;
  83. for (i = 0; i < dummy_stm_nr; i++) {
  84. stm_unregister_device(&dummy_stm[i]);
  85. kfree(dummy_stm[i].name);
  86. }
  87. }
  88. module_init(dummy_stm_init);
  89. module_exit(dummy_stm_exit);
  90. MODULE_LICENSE("GPL v2");
  91. MODULE_DESCRIPTION("dummy_stm device");
  92. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");