u_ether_configfs.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. CONFIGFS_ATTR_STRUCT(f_##_f_##_opts); \
  19. CONFIGFS_ATTR_OPS(f_##_f_##_opts); \
  20. \
  21. static void _f_##_attr_release(struct config_item *item) \
  22. { \
  23. struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \
  24. \
  25. usb_put_function_instance(&opts->func_inst); \
  26. } \
  27. \
  28. static struct configfs_item_operations _f_##_item_ops = { \
  29. .release = _f_##_attr_release, \
  30. .show_attribute = f_##_f_##_opts_attr_show, \
  31. .store_attribute = f_##_f_##_opts_attr_store, \
  32. }
  33. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(_f_) \
  34. static ssize_t _f_##_opts_dev_addr_show(struct f_##_f_##_opts *opts, \
  35. char *page) \
  36. { \
  37. int result; \
  38. \
  39. mutex_lock(&opts->lock); \
  40. result = gether_get_dev_addr(opts->net, page, PAGE_SIZE); \
  41. mutex_unlock(&opts->lock); \
  42. \
  43. return result; \
  44. } \
  45. \
  46. static ssize_t _f_##_opts_dev_addr_store(struct f_##_f_##_opts *opts, \
  47. const char *page, size_t len)\
  48. { \
  49. int ret; \
  50. \
  51. mutex_lock(&opts->lock); \
  52. if (opts->refcnt) { \
  53. mutex_unlock(&opts->lock); \
  54. return -EBUSY; \
  55. } \
  56. \
  57. ret = gether_set_dev_addr(opts->net, page); \
  58. mutex_unlock(&opts->lock); \
  59. if (!ret) \
  60. ret = len; \
  61. return ret; \
  62. } \
  63. \
  64. static struct f_##_f_##_opts_attribute f_##_f_##_opts_dev_addr = \
  65. __CONFIGFS_ATTR(dev_addr, S_IRUGO | S_IWUSR, \
  66. _f_##_opts_dev_addr_show, \
  67. _f_##_opts_dev_addr_store)
  68. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(_f_) \
  69. static ssize_t _f_##_opts_host_addr_show(struct f_##_f_##_opts *opts, \
  70. char *page) \
  71. { \
  72. int result; \
  73. \
  74. mutex_lock(&opts->lock); \
  75. result = gether_get_host_addr(opts->net, page, PAGE_SIZE); \
  76. mutex_unlock(&opts->lock); \
  77. \
  78. return result; \
  79. } \
  80. \
  81. static ssize_t _f_##_opts_host_addr_store(struct f_##_f_##_opts *opts, \
  82. const char *page, size_t len)\
  83. { \
  84. int ret; \
  85. \
  86. mutex_lock(&opts->lock); \
  87. if (opts->refcnt) { \
  88. mutex_unlock(&opts->lock); \
  89. return -EBUSY; \
  90. } \
  91. \
  92. ret = gether_set_host_addr(opts->net, page); \
  93. mutex_unlock(&opts->lock); \
  94. if (!ret) \
  95. ret = len; \
  96. return ret; \
  97. } \
  98. \
  99. static struct f_##_f_##_opts_attribute f_##_f_##_opts_host_addr = \
  100. __CONFIGFS_ATTR(host_addr, S_IRUGO | S_IWUSR, \
  101. _f_##_opts_host_addr_show, \
  102. _f_##_opts_host_addr_store)
  103. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(_f_) \
  104. static ssize_t _f_##_opts_qmult_show(struct f_##_f_##_opts *opts, \
  105. char *page) \
  106. { \
  107. unsigned qmult; \
  108. \
  109. mutex_lock(&opts->lock); \
  110. qmult = gether_get_qmult(opts->net); \
  111. mutex_unlock(&opts->lock); \
  112. return sprintf(page, "%d", qmult); \
  113. } \
  114. \
  115. static ssize_t _f_##_opts_qmult_store(struct f_##_f_##_opts *opts, \
  116. const char *page, size_t len)\
  117. { \
  118. u8 val; \
  119. int ret; \
  120. \
  121. mutex_lock(&opts->lock); \
  122. if (opts->refcnt) { \
  123. ret = -EBUSY; \
  124. goto out; \
  125. } \
  126. \
  127. ret = kstrtou8(page, 0, &val); \
  128. if (ret) \
  129. goto out; \
  130. \
  131. gether_set_qmult(opts->net, val); \
  132. ret = len; \
  133. out: \
  134. mutex_unlock(&opts->lock); \
  135. return ret; \
  136. } \
  137. \
  138. static struct f_##_f_##_opts_attribute f_##_f_##_opts_qmult = \
  139. __CONFIGFS_ATTR(qmult, S_IRUGO | S_IWUSR, \
  140. _f_##_opts_qmult_show, \
  141. _f_##_opts_qmult_store)
  142. #define USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(_f_) \
  143. static ssize_t _f_##_opts_ifname_show(struct f_##_f_##_opts *opts, \
  144. char *page) \
  145. { \
  146. int ret; \
  147. \
  148. mutex_lock(&opts->lock); \
  149. ret = gether_get_ifname(opts->net, page, PAGE_SIZE); \
  150. mutex_unlock(&opts->lock); \
  151. \
  152. return ret; \
  153. } \
  154. \
  155. static struct f_##_f_##_opts_attribute f_##_f_##_opts_ifname = \
  156. __CONFIGFS_ATTR_RO(ifname, _f_##_opts_ifname_show)
  157. #endif /* __U_ETHER_CONFIGFS_H */