blob: d6881514d38e701c8913e8d57cbd47ab2a5a1d27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/string.h>
16#include <linux/major.h>
17#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mm.h>
19#include <linux/interrupt.h>
20#include <linux/timer.h>
21#include <linux/ioport.h>
22#include <linux/delay.h>
23#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/device.h>
Dominik Brodowski7fe908d2006-01-10 21:20:36 +010025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/irq.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <pcmcia/ss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <pcmcia/cistpl.h>
30#include <pcmcia/cisreg.h>
31#include <pcmcia/ds.h>
32#include "cs_internal.h"
33
34#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
35
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020036static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
37 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct pcmcia_socket *s = to_socket(dev);
40
41 if (!(s->state & SOCKET_PRESENT))
42 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010043 if (s->state & SOCKET_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return sprintf(buf, "32-bit\n");
Dominik Brodowski002dbb22005-12-08 23:50:36 +010045 return sprintf(buf, "16-bit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020047static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020049static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
50 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct pcmcia_socket *s = to_socket(dev);
53
54 if (!(s->state & SOCKET_PRESENT))
55 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010056 if (s->socket.Vcc)
57 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
58 s->socket.Vcc % 10);
59 return sprintf(buf, "X.XV\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020061static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020063static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
64 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct pcmcia_socket *s = to_socket(dev);
67 if (!(s->state & SOCKET_PRESENT))
68 return -ENODEV;
69 return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
70}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020071static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020073static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
74 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct pcmcia_socket *s = to_socket(dev);
77 if (!(s->state & SOCKET_PRESENT))
78 return -ENODEV;
79 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
80}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020081static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020084static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
85 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct pcmcia_socket *s = to_socket(dev);
88
89 if (!count)
90 return -EINVAL;
91
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010092 pcmcia_parse_uevents(s, PCMCIA_UEVENT_INSERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010094 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020096static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dominik Brodowskidb1019c2006-01-10 19:19:37 +010098
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020099static ssize_t pccard_show_card_pm_state(struct device *dev,
100 struct device_attribute *attr,
101 char *buf)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100102{
103 struct pcmcia_socket *s = to_socket(dev);
104 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
105}
106
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200107static ssize_t pccard_store_card_pm_state(struct device *dev,
108 struct device_attribute *attr,
109 const char *buf, size_t count)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100110{
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100111 struct pcmcia_socket *s = to_socket(dev);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100112 ssize_t ret = count;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100113
114 if (!count)
115 return -EINVAL;
116
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100117 if (!strncmp(buf, "off", 3))
118 pcmcia_parse_uevents(s, PCMCIA_UEVENT_SUSPEND);
119 else {
120 if (!strncmp(buf, "on", 2))
121 pcmcia_parse_uevents(s, PCMCIA_UEVENT_RESUME);
122 else
123 ret = -EINVAL;
124 }
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100125
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100126 return ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100127}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200128static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100129
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200130static ssize_t pccard_store_eject(struct device *dev,
131 struct device_attribute *attr,
132 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct pcmcia_socket *s = to_socket(dev);
135
136 if (!count)
137 return -EINVAL;
138
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100139 pcmcia_parse_uevents(s, PCMCIA_UEVENT_EJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100141 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200143static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200146static ssize_t pccard_show_irq_mask(struct device *dev,
147 struct device_attribute *attr,
148 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct pcmcia_socket *s = to_socket(dev);
151 return sprintf(buf, "0x%04x\n", s->irq_mask);
152}
153
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200154static ssize_t pccard_store_irq_mask(struct device *dev,
155 struct device_attribute *attr,
156 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 ssize_t ret;
159 struct pcmcia_socket *s = to_socket(dev);
160 u32 mask;
161
162 if (!count)
163 return -EINVAL;
164
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100165 ret = sscanf(buf, "0x%x\n", &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (ret == 1) {
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100168 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 s->irq_mask &= mask;
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100170 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ret = 0;
172 }
173
174 return ret ? ret : count;
175}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200176static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200179static ssize_t pccard_show_resource(struct device *dev,
180 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct pcmcia_socket *s = to_socket(dev);
183 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
184}
185
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200186static ssize_t pccard_store_resource(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct pcmcia_socket *s = to_socket(dev);
191
192 if (!count)
193 return -EINVAL;
194
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100195 mutex_lock(&s->ops_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700196 if (!s->resource_setup_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 s->resource_setup_done = 1;
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100198 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100200 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return count;
203}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200204static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
David Brownell4356d732008-04-28 01:03:20 -0700206static struct attribute *pccard_socket_attributes[] = {
207 &dev_attr_card_type.attr,
208 &dev_attr_card_voltage.attr,
209 &dev_attr_card_vpp.attr,
210 &dev_attr_card_vcc.attr,
211 &dev_attr_card_insert.attr,
212 &dev_attr_card_pm_state.attr,
213 &dev_attr_card_eject.attr,
214 &dev_attr_card_irq_mask.attr,
215 &dev_attr_available_resources_setup_done.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 NULL,
217};
218
David Brownell4356d732008-04-28 01:03:20 -0700219static const struct attribute_group socket_attrs = {
220 .attrs = pccard_socket_attributes,
221};
222
David Brownell4356d732008-04-28 01:03:20 -0700223int pccard_sysfs_add_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +0100225 return sysfs_create_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
David Brownell4356d732008-04-28 01:03:20 -0700228void pccard_sysfs_remove_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
David Brownell4356d732008-04-28 01:03:20 -0700230 sysfs_remove_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}