socket_sysfs.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * socket_sysfs.c -- most of socket-related sysfs output
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * (C) 2003 - 2004 Dominik Brodowski
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/major.h>
  16. #include <linux/errno.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/timer.h>
  20. #include <linux/ioport.h>
  21. #include <linux/delay.h>
  22. #include <linux/pm.h>
  23. #include <linux/device.h>
  24. #include <linux/mutex.h>
  25. #include <asm/system.h>
  26. #include <asm/irq.h>
  27. #include <pcmcia/ss.h>
  28. #include <pcmcia/cistpl.h>
  29. #include <pcmcia/cisreg.h>
  30. #include <pcmcia/ds.h>
  31. #include "cs_internal.h"
  32. #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
  33. static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
  34. char *buf)
  35. {
  36. struct pcmcia_socket *s = to_socket(dev);
  37. if (!(s->state & SOCKET_PRESENT))
  38. return -ENODEV;
  39. if (s->state & SOCKET_CARDBUS)
  40. return sprintf(buf, "32-bit\n");
  41. return sprintf(buf, "16-bit\n");
  42. }
  43. static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
  44. static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
  45. char *buf)
  46. {
  47. struct pcmcia_socket *s = to_socket(dev);
  48. if (!(s->state & SOCKET_PRESENT))
  49. return -ENODEV;
  50. if (s->socket.Vcc)
  51. return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
  52. s->socket.Vcc % 10);
  53. return sprintf(buf, "X.XV\n");
  54. }
  55. static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
  56. static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
  57. char *buf)
  58. {
  59. struct pcmcia_socket *s = to_socket(dev);
  60. if (!(s->state & SOCKET_PRESENT))
  61. return -ENODEV;
  62. return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
  63. }
  64. static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
  65. static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
  66. char *buf)
  67. {
  68. struct pcmcia_socket *s = to_socket(dev);
  69. if (!(s->state & SOCKET_PRESENT))
  70. return -ENODEV;
  71. return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
  72. }
  73. static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
  74. static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
  75. const char *buf, size_t count)
  76. {
  77. struct pcmcia_socket *s = to_socket(dev);
  78. if (!count)
  79. return -EINVAL;
  80. pcmcia_parse_uevents(s, PCMCIA_UEVENT_INSERT);
  81. return count;
  82. }
  83. static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
  84. static ssize_t pccard_show_card_pm_state(struct device *dev,
  85. struct device_attribute *attr,
  86. char *buf)
  87. {
  88. struct pcmcia_socket *s = to_socket(dev);
  89. return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
  90. }
  91. static ssize_t pccard_store_card_pm_state(struct device *dev,
  92. struct device_attribute *attr,
  93. const char *buf, size_t count)
  94. {
  95. struct pcmcia_socket *s = to_socket(dev);
  96. ssize_t ret = count;
  97. if (!count)
  98. return -EINVAL;
  99. if (!strncmp(buf, "off", 3))
  100. pcmcia_parse_uevents(s, PCMCIA_UEVENT_SUSPEND);
  101. else {
  102. if (!strncmp(buf, "on", 2))
  103. pcmcia_parse_uevents(s, PCMCIA_UEVENT_RESUME);
  104. else
  105. ret = -EINVAL;
  106. }
  107. return ret;
  108. }
  109. static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
  110. static ssize_t pccard_store_eject(struct device *dev,
  111. struct device_attribute *attr,
  112. const char *buf, size_t count)
  113. {
  114. struct pcmcia_socket *s = to_socket(dev);
  115. if (!count)
  116. return -EINVAL;
  117. pcmcia_parse_uevents(s, PCMCIA_UEVENT_EJECT);
  118. return count;
  119. }
  120. static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
  121. static ssize_t pccard_show_irq_mask(struct device *dev,
  122. struct device_attribute *attr,
  123. char *buf)
  124. {
  125. struct pcmcia_socket *s = to_socket(dev);
  126. return sprintf(buf, "0x%04x\n", s->irq_mask);
  127. }
  128. static ssize_t pccard_store_irq_mask(struct device *dev,
  129. struct device_attribute *attr,
  130. const char *buf, size_t count)
  131. {
  132. ssize_t ret;
  133. struct pcmcia_socket *s = to_socket(dev);
  134. u32 mask;
  135. if (!count)
  136. return -EINVAL;
  137. ret = sscanf(buf, "0x%x\n", &mask);
  138. if (ret == 1) {
  139. mutex_lock(&s->ops_mutex);
  140. s->irq_mask &= mask;
  141. mutex_unlock(&s->ops_mutex);
  142. ret = 0;
  143. }
  144. return ret ? ret : count;
  145. }
  146. static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
  147. static ssize_t pccard_show_resource(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct pcmcia_socket *s = to_socket(dev);
  151. return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
  152. }
  153. static ssize_t pccard_store_resource(struct device *dev,
  154. struct device_attribute *attr,
  155. const char *buf, size_t count)
  156. {
  157. struct pcmcia_socket *s = to_socket(dev);
  158. if (!count)
  159. return -EINVAL;
  160. mutex_lock(&s->ops_mutex);
  161. if (!s->resource_setup_done)
  162. s->resource_setup_done = 1;
  163. mutex_unlock(&s->ops_mutex);
  164. pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
  165. return count;
  166. }
  167. static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
  168. static struct attribute *pccard_socket_attributes[] = {
  169. &dev_attr_card_type.attr,
  170. &dev_attr_card_voltage.attr,
  171. &dev_attr_card_vpp.attr,
  172. &dev_attr_card_vcc.attr,
  173. &dev_attr_card_insert.attr,
  174. &dev_attr_card_pm_state.attr,
  175. &dev_attr_card_eject.attr,
  176. &dev_attr_card_irq_mask.attr,
  177. &dev_attr_available_resources_setup_done.attr,
  178. NULL,
  179. };
  180. static const struct attribute_group socket_attrs = {
  181. .attrs = pccard_socket_attributes,
  182. };
  183. int pccard_sysfs_add_socket(struct device *dev)
  184. {
  185. return sysfs_create_group(&dev->kobj, &socket_attrs);
  186. }
  187. void pccard_sysfs_remove_socket(struct device *dev)
  188. {
  189. sysfs_remove_group(&dev->kobj, &socket_attrs);
  190. }