socket_sysfs.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/irq.h>
  26. #include <pcmcia/ss.h>
  27. #include <pcmcia/cistpl.h>
  28. #include <pcmcia/cisreg.h>
  29. #include <pcmcia/ds.h>
  30. #include "cs_internal.h"
  31. #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
  32. static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct pcmcia_socket *s = to_socket(dev);
  36. if (!(s->state & SOCKET_PRESENT))
  37. return -ENODEV;
  38. if (s->state & SOCKET_CARDBUS)
  39. return sprintf(buf, "32-bit\n");
  40. return sprintf(buf, "16-bit\n");
  41. }
  42. static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
  43. static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
  44. char *buf)
  45. {
  46. struct pcmcia_socket *s = to_socket(dev);
  47. if (!(s->state & SOCKET_PRESENT))
  48. return -ENODEV;
  49. if (s->socket.Vcc)
  50. return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
  51. s->socket.Vcc % 10);
  52. return sprintf(buf, "X.XV\n");
  53. }
  54. static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
  55. static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct pcmcia_socket *s = to_socket(dev);
  59. if (!(s->state & SOCKET_PRESENT))
  60. return -ENODEV;
  61. return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
  62. }
  63. static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
  64. static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
  65. char *buf)
  66. {
  67. struct pcmcia_socket *s = to_socket(dev);
  68. if (!(s->state & SOCKET_PRESENT))
  69. return -ENODEV;
  70. return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
  71. }
  72. static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
  73. static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
  74. const char *buf, size_t count)
  75. {
  76. struct pcmcia_socket *s = to_socket(dev);
  77. if (!count)
  78. return -EINVAL;
  79. pcmcia_parse_uevents(s, PCMCIA_UEVENT_INSERT);
  80. return count;
  81. }
  82. static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
  83. static ssize_t pccard_show_card_pm_state(struct device *dev,
  84. struct device_attribute *attr,
  85. char *buf)
  86. {
  87. struct pcmcia_socket *s = to_socket(dev);
  88. return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
  89. }
  90. static ssize_t pccard_store_card_pm_state(struct device *dev,
  91. struct device_attribute *attr,
  92. const char *buf, size_t count)
  93. {
  94. struct pcmcia_socket *s = to_socket(dev);
  95. ssize_t ret = count;
  96. if (!count)
  97. return -EINVAL;
  98. if (!strncmp(buf, "off", 3))
  99. pcmcia_parse_uevents(s, PCMCIA_UEVENT_SUSPEND);
  100. else {
  101. if (!strncmp(buf, "on", 2))
  102. pcmcia_parse_uevents(s, PCMCIA_UEVENT_RESUME);
  103. else
  104. ret = -EINVAL;
  105. }
  106. return ret;
  107. }
  108. static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
  109. static ssize_t pccard_store_eject(struct device *dev,
  110. struct device_attribute *attr,
  111. const char *buf, size_t count)
  112. {
  113. struct pcmcia_socket *s = to_socket(dev);
  114. if (!count)
  115. return -EINVAL;
  116. pcmcia_parse_uevents(s, PCMCIA_UEVENT_EJECT);
  117. return count;
  118. }
  119. static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
  120. static ssize_t pccard_show_irq_mask(struct device *dev,
  121. struct device_attribute *attr,
  122. char *buf)
  123. {
  124. struct pcmcia_socket *s = to_socket(dev);
  125. return sprintf(buf, "0x%04x\n", s->irq_mask);
  126. }
  127. static ssize_t pccard_store_irq_mask(struct device *dev,
  128. struct device_attribute *attr,
  129. const char *buf, size_t count)
  130. {
  131. ssize_t ret;
  132. struct pcmcia_socket *s = to_socket(dev);
  133. u32 mask;
  134. if (!count)
  135. return -EINVAL;
  136. ret = sscanf(buf, "0x%x\n", &mask);
  137. if (ret == 1) {
  138. mutex_lock(&s->ops_mutex);
  139. s->irq_mask &= mask;
  140. mutex_unlock(&s->ops_mutex);
  141. ret = 0;
  142. }
  143. return ret ? ret : count;
  144. }
  145. static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
  146. static ssize_t pccard_show_resource(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct pcmcia_socket *s = to_socket(dev);
  150. return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
  151. }
  152. static ssize_t pccard_store_resource(struct device *dev,
  153. struct device_attribute *attr,
  154. const char *buf, size_t count)
  155. {
  156. struct pcmcia_socket *s = to_socket(dev);
  157. if (!count)
  158. return -EINVAL;
  159. mutex_lock(&s->ops_mutex);
  160. if (!s->resource_setup_done)
  161. s->resource_setup_done = 1;
  162. mutex_unlock(&s->ops_mutex);
  163. pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
  164. return count;
  165. }
  166. static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
  167. static struct attribute *pccard_socket_attributes[] = {
  168. &dev_attr_card_type.attr,
  169. &dev_attr_card_voltage.attr,
  170. &dev_attr_card_vpp.attr,
  171. &dev_attr_card_vcc.attr,
  172. &dev_attr_card_insert.attr,
  173. &dev_attr_card_pm_state.attr,
  174. &dev_attr_card_eject.attr,
  175. &dev_attr_card_irq_mask.attr,
  176. &dev_attr_available_resources_setup_done.attr,
  177. NULL,
  178. };
  179. static const struct attribute_group socket_attrs = {
  180. .attrs = pccard_socket_attributes,
  181. };
  182. int pccard_sysfs_add_socket(struct device *dev)
  183. {
  184. return sysfs_create_group(&dev->kobj, &socket_attrs);
  185. }
  186. void pccard_sysfs_remove_socket(struct device *dev)
  187. {
  188. sysfs_remove_group(&dev->kobj, &socket_attrs);
  189. }