dma-sysfs.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 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. int ret;
  41. ret = subsys_system_register(&dma_subsys, NULL);
  42. if (unlikely(ret))
  43. return ret;
  44. return device_create_file(dma_subsys.dev_root, &dev_attr_devices);
  45. }
  46. postcore_initcall(dma_subsys_init);
  47. static ssize_t dma_show_dev_id(struct device *dev,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. struct dma_channel *channel = to_dma_channel(dev);
  51. return sprintf(buf, "%s\n", channel->dev_id);
  52. }
  53. static ssize_t dma_store_dev_id(struct device *dev,
  54. struct device_attribute *attr,
  55. const char *buf, size_t count)
  56. {
  57. struct dma_channel *channel = to_dma_channel(dev);
  58. strcpy(channel->dev_id, buf);
  59. return count;
  60. }
  61. static DEVICE_ATTR(dev_id, S_IRUGO | S_IWUSR, dma_show_dev_id, dma_store_dev_id);
  62. static ssize_t dma_store_config(struct device *dev,
  63. struct device_attribute *attr,
  64. const char *buf, size_t count)
  65. {
  66. struct dma_channel *channel = to_dma_channel(dev);
  67. unsigned long config;
  68. config = simple_strtoul(buf, NULL, 0);
  69. dma_configure_channel(channel->vchan, config);
  70. return count;
  71. }
  72. static DEVICE_ATTR(config, S_IWUSR, NULL, dma_store_config);
  73. static ssize_t dma_show_mode(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. struct dma_channel *channel = to_dma_channel(dev);
  77. return sprintf(buf, "0x%08x\n", channel->mode);
  78. }
  79. static ssize_t dma_store_mode(struct device *dev,
  80. struct device_attribute *attr,
  81. const char *buf, size_t count)
  82. {
  83. struct dma_channel *channel = to_dma_channel(dev);
  84. channel->mode = simple_strtoul(buf, NULL, 0);
  85. return count;
  86. }
  87. static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, dma_show_mode, dma_store_mode);
  88. #define dma_ro_attr(field, fmt) \
  89. static ssize_t dma_show_##field(struct device *dev, \
  90. struct device_attribute *attr, char *buf)\
  91. { \
  92. struct dma_channel *channel = to_dma_channel(dev); \
  93. return sprintf(buf, fmt, channel->field); \
  94. } \
  95. static DEVICE_ATTR(field, S_IRUGO, dma_show_##field, NULL);
  96. dma_ro_attr(count, "0x%08x\n");
  97. dma_ro_attr(flags, "0x%08lx\n");
  98. int dma_create_sysfs_files(struct dma_channel *chan, struct dma_info *info)
  99. {
  100. struct device *dev = &chan->dev;
  101. char name[16];
  102. int ret;
  103. dev->id = chan->vchan;
  104. dev->bus = &dma_subsys;
  105. ret = device_register(dev);
  106. if (ret)
  107. return ret;
  108. ret |= device_create_file(dev, &dev_attr_dev_id);
  109. ret |= device_create_file(dev, &dev_attr_count);
  110. ret |= device_create_file(dev, &dev_attr_mode);
  111. ret |= device_create_file(dev, &dev_attr_flags);
  112. ret |= device_create_file(dev, &dev_attr_config);
  113. if (unlikely(ret)) {
  114. dev_err(&info->pdev->dev, "Failed creating attrs\n");
  115. return ret;
  116. }
  117. snprintf(name, sizeof(name), "dma%d", chan->chan);
  118. return sysfs_create_link(&info->pdev->dev.kobj, &dev->kobj, name);
  119. }
  120. void dma_remove_sysfs_files(struct dma_channel *chan, struct dma_info *info)
  121. {
  122. struct device *dev = &chan->dev;
  123. char name[16];
  124. device_remove_file(dev, &dev_attr_dev_id);
  125. device_remove_file(dev, &dev_attr_count);
  126. device_remove_file(dev, &dev_attr_mode);
  127. device_remove_file(dev, &dev_attr_flags);
  128. device_remove_file(dev, &dev_attr_config);
  129. snprintf(name, sizeof(name), "dma%d", chan->chan);
  130. sysfs_remove_link(&info->pdev->dev.kobj, name);
  131. device_unregister(dev);
  132. }