u_ether_configfs.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * u_ether_configfs.h
  3. *
  4. * Utility definitions for configfs support in USB Ethernet functions
  5. *
  6. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  7. * http://www.samsung.com
  8. *
  9. * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #ifndef __U_ETHER_CONFIGFS_H
  16. #define __U_ETHER_CONFIGFS_H
  17. #define USB_ETHERNET_CONFIGFS_ITEM(_f_) \
  18. static void _f_##_attr_release(struct config_item *item) \
  19. { \
  20. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  21. \
  22. usb_put_function_instance(&opts->func_inst); \
  23. } \
  24. \
  25. static struct configfs_item_operations _f_##_item_ops = { \
  26. .release = _f_##_attr_release, \
  27. }
  28. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(_f_) \
  29. static ssize_t _f_##_opts_dev_addr_show(struct config_item *item, \
  30. char *page) \
  31. { \
  32. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  33. int result; \
  34. \
  35. mutex_lock(&opts->lock); \
  36. result = gether_get_dev_addr(opts->net, page, PAGE_SIZE); \
  37. mutex_unlock(&opts->lock); \
  38. \
  39. return result; \
  40. } \
  41. \
  42. static ssize_t _f_##_opts_dev_addr_store(struct config_item *item, \
  43. const char *page, size_t len)\
  44. { \
  45. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  46. int ret; \
  47. \
  48. mutex_lock(&opts->lock); \
  49. if (opts->refcnt) { \
  50. mutex_unlock(&opts->lock); \
  51. return -EBUSY; \
  52. } \
  53. \
  54. ret = gether_set_dev_addr(opts->net, page); \
  55. mutex_unlock(&opts->lock); \
  56. if (!ret) \
  57. ret = len; \
  58. return ret; \
  59. } \
  60. \
  61. CONFIGFS_ATTR(_f_##_opts_, dev_addr)
  62. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(_f_) \
  63. static ssize_t _f_##_opts_host_addr_show(struct config_item *item, \
  64. char *page) \
  65. { \
  66. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  67. int result; \
  68. \
  69. mutex_lock(&opts->lock); \
  70. result = gether_get_host_addr(opts->net, page, PAGE_SIZE); \
  71. mutex_unlock(&opts->lock); \
  72. \
  73. return result; \
  74. } \
  75. \
  76. static ssize_t _f_##_opts_host_addr_store(struct config_item *item, \
  77. const char *page, size_t len)\
  78. { \
  79. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  80. int ret; \
  81. \
  82. mutex_lock(&opts->lock); \
  83. if (opts->refcnt) { \
  84. mutex_unlock(&opts->lock); \
  85. return -EBUSY; \
  86. } \
  87. \
  88. ret = gether_set_host_addr(opts->net, page); \
  89. mutex_unlock(&opts->lock); \
  90. if (!ret) \
  91. ret = len; \
  92. return ret; \
  93. } \
  94. \
  95. CONFIGFS_ATTR(_f_##_opts_, host_addr)
  96. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(_f_) \
  97. static ssize_t _f_##_opts_qmult_show(struct config_item *item, \
  98. char *page) \
  99. { \
  100. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  101. unsigned qmult; \
  102. \
  103. mutex_lock(&opts->lock); \
  104. qmult = gether_get_qmult(opts->net); \
  105. mutex_unlock(&opts->lock); \
  106. return sprintf(page, "%d", qmult); \
  107. } \
  108. \
  109. static ssize_t _f_##_opts_qmult_store(struct config_item *item, \
  110. const char *page, size_t len)\
  111. { \
  112. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  113. u8 val; \
  114. int ret; \
  115. \
  116. mutex_lock(&opts->lock); \
  117. if (opts->refcnt) { \
  118. ret = -EBUSY; \
  119. goto out; \
  120. } \
  121. \
  122. ret = kstrtou8(page, 0, &val); \
  123. if (ret) \
  124. goto out; \
  125. \
  126. gether_set_qmult(opts->net, val); \
  127. ret = len; \
  128. out: \
  129. mutex_unlock(&opts->lock); \
  130. return ret; \
  131. } \
  132. \
  133. CONFIGFS_ATTR(_f_##_opts_, qmult)
  134. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(_f_) \
  135. static ssize_t _f_##_opts_ifname_show(struct config_item *item, \
  136. char *page) \
  137. { \
  138. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  139. int ret; \
  140. \
  141. mutex_lock(&opts->lock); \
  142. ret = gether_get_ifname(opts->net, page, PAGE_SIZE); \
  143. mutex_unlock(&opts->lock); \
  144. \
  145. return ret; \
  146. } \
  147. \
  148. CONFIGFS_ATTR_RO(_f_##_opts_, ifname)
  149. #endif /* __U_ETHER_CONFIGFS_H */