dma-sysfs.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/sh/drivers/dma/dma-sysfs.c
  4. *
  5. * sysfs interface for SH DMA API
  6. *
  7. * Copyright (C) 2004 - 2006 Paul Mundt
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/stat.h>
  12. #include <linux/device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/err.h>
  15. #include <linux/string.h>
  16. #include <asm/dma.h>
  17. static const struct bus_type dma_subsys = {
  18. .name = "dma",
  19. .dev_name = "dma",
  20. };
  21. static ssize_t dma_show_devices(struct device *dev,
  22. struct device_attribute *attr, char *buf)
  23. {
  24. ssize_t len = 0;
  25. int i;
  26. for (i = 0; i < 16; i++) {
  27. struct dma_info *info = get_dma_info(i);
  28. struct dma_channel *channel = get_dma_channel(i);
  29. if (unlikely(!info) || !channel)
  30. continue;
  31. len += sprintf(buf + len, "%2d: %14s %s\n",
  32. channel->chan, info->name,
  33. channel->dev_id);
  34. }
  35. return len;
  36. }
  37. static DEVICE_ATTR(devices, S_IRUGO, dma_show_devices, NULL);
  38. static int __init dma_subsys_init(void)
  39. {
  40. struct device *dev_root;
  41. int ret;
  42. ret = subsys_system_register(&dma_subsys, NULL);
  43. if (unlikely(ret))
  44. return ret;
  45. dev_root = bus_get_dev_root(&dma_subsys);
  46. if (dev_root) {
  47. ret = device_create_file(dev_root, &dev_attr_devices);
  48. put_device(dev_root);
  49. }
  50. return ret;
  51. }
  52. postcore_initcall(dma_subsys_init);
  53. static ssize_t dma_show_dev_id(struct device *dev,
  54. struct device_attribute *attr, char *buf)
  55. {
  56. struct dma_channel *channel = to_dma_channel(dev);
  57. return sprintf(buf, "%s\n", channel->dev_id);
  58. }
  59. static ssize_t dma_store_dev_id(struct device *dev,
  60. struct device_attribute *attr,
  61. const char *buf, size_t count)
  62. {
  63. struct dma_channel *channel = to_dma_channel(dev);
  64. strcpy(channel->dev_id, buf);
  65. return count;
  66. }
  67. static DEVICE_ATTR(dev_id, S_IRUGO | S_IWUSR, dma_show_dev_id, dma_store_dev_id);
  68. static ssize_t dma_store_config(struct device *dev,
  69. struct device_attribute *attr,
  70. const char *buf, size_t count)
  71. {
  72. struct dma_channel *channel = to_dma_channel(dev);
  73. unsigned long config;
  74. config = simple_strtoul(buf, NULL, 0);
  75. dma_configure_channel(channel->vchan, config);
  76. return count;
  77. }
  78. static DEVICE_ATTR(config, S_IWUSR, NULL, dma_store_config);
  79. static ssize_t dma_show_mode(struct device *dev,
  80. struct device_attribute *attr, char *buf)
  81. {
  82. struct dma_channel *channel = to_dma_channel(dev);
  83. return sprintf(buf, "0x%08x\n", channel->mode);
  84. }
  85. static ssize_t dma_store_mode(struct device *dev,
  86. struct device_attribute *attr,
  87. const char *buf, size_t count)
  88. {
  89. struct dma_channel *channel = to_dma_channel(dev);
  90. channel->mode = simple_strtoul(buf, NULL, 0);
  91. return count;
  92. }
  93. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, dma_show_mode, dma_store_mode);
  94. #define dma_ro_attr(field, fmt) \
  95. static ssize_t dma_show_##field(struct device *dev, \
  96. struct device_attribute *attr, char *buf)\
  97. { \
  98. struct dma_channel *channel = to_dma_channel(dev); \
  99. return sprintf(buf, fmt, channel->field); \
  100. } \
  101. static DEVICE_ATTR(field, S_IRUGO, dma_show_##field, NULL);
  102. dma_ro_attr(count, "0x%08x\n");
  103. dma_ro_attr(flags, "0x%08lx\n");
  104. int dma_create_sysfs_files(struct dma_channel *chan, struct dma_info *info)
  105. {
  106. struct device *dev = &chan->dev;
  107. char name[16];
  108. int ret;
  109. dev->id = chan->vchan;
  110. dev->bus = &dma_subsys;
  111. ret = device_register(dev);
  112. if (ret)
  113. return ret;
  114. ret |= device_create_file(dev, &dev_attr_dev_id);
  115. ret |= device_create_file(dev, &dev_attr_count);
  116. ret |= device_create_file(dev, &dev_attr_mode);
  117. ret |= device_create_file(dev, &dev_attr_flags);
  118. ret |= device_create_file(dev, &dev_attr_config);
  119. if (unlikely(ret)) {
  120. dev_err(&info->pdev->dev, "Failed creating attrs\n");
  121. return ret;
  122. }
  123. snprintf(name, sizeof(name), "dma%d", chan->chan);
  124. return sysfs_create_link(&info->pdev->dev.kobj, &dev->kobj, name);
  125. }
  126. void dma_remove_sysfs_files(struct dma_channel *chan, struct dma_info *info)
  127. {
  128. struct device *dev = &chan->dev;
  129. char name[16];
  130. device_remove_file(dev, &dev_attr_dev_id);
  131. device_remove_file(dev, &dev_attr_count);
  132. device_remove_file(dev, &dev_attr_mode);
  133. device_remove_file(dev, &dev_attr_flags);
  134. device_remove_file(dev, &dev_attr_config);
  135. snprintf(name, sizeof(name), "dma%d", chan->chan);
  136. sysfs_remove_link(&info->pdev->dev.kobj, name);
  137. device_unregister(dev);
  138. }