blob: 08278016e58dde68a2d8526d6b3cf0d6e9c1a9ef [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>
18#include <linux/slab.h>
19#include <linux/mm.h>
20#include <linux/interrupt.h>
21#include <linux/timer.h>
22#include <linux/ioport.h>
23#include <linux/delay.h>
24#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/device.h>
Dominik Brodowski7fe908d2006-01-10 21:20:36 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/system.h>
28#include <asm/irq.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <pcmcia/cs_types.h>
31#include <pcmcia/ss.h>
32#include <pcmcia/cs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <pcmcia/cistpl.h>
34#include <pcmcia/cisreg.h>
35#include <pcmcia/ds.h>
36#include "cs_internal.h"
37
38#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
39
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020040static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
41 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct pcmcia_socket *s = to_socket(dev);
44
45 if (!(s->state & SOCKET_PRESENT))
46 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010047 if (s->state & SOCKET_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return sprintf(buf, "32-bit\n");
Dominik Brodowski002dbb22005-12-08 23:50:36 +010049 return sprintf(buf, "16-bit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020051static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020053static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
54 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct pcmcia_socket *s = to_socket(dev);
57
58 if (!(s->state & SOCKET_PRESENT))
59 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010060 if (s->socket.Vcc)
61 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
62 s->socket.Vcc % 10);
63 return sprintf(buf, "X.XV\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020065static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020067static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
68 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct pcmcia_socket *s = to_socket(dev);
71 if (!(s->state & SOCKET_PRESENT))
72 return -ENODEV;
73 return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
74}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020075static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020077static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
78 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct pcmcia_socket *s = to_socket(dev);
81 if (!(s->state & SOCKET_PRESENT))
82 return -ENODEV;
83 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
84}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020085static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020088static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
89 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct pcmcia_socket *s = to_socket(dev);
92
93 if (!count)
94 return -EINVAL;
95
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010096 pcmcia_parse_uevents(s, PCMCIA_UEVENT_INSERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010098 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200100static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100102
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200103static ssize_t pccard_show_card_pm_state(struct device *dev,
104 struct device_attribute *attr,
105 char *buf)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100106{
107 struct pcmcia_socket *s = to_socket(dev);
108 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
109}
110
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200111static ssize_t pccard_store_card_pm_state(struct device *dev,
112 struct device_attribute *attr,
113 const char *buf, size_t count)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100114{
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100115 struct pcmcia_socket *s = to_socket(dev);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100116 ssize_t ret = count;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100117
118 if (!count)
119 return -EINVAL;
120
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100121 if (!strncmp(buf, "off", 3))
122 pcmcia_parse_uevents(s, PCMCIA_UEVENT_SUSPEND);
123 else {
124 if (!strncmp(buf, "on", 2))
125 pcmcia_parse_uevents(s, PCMCIA_UEVENT_RESUME);
126 else
127 ret = -EINVAL;
128 }
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100129
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100130 return ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100131}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200132static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100133
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200134static ssize_t pccard_store_eject(struct device *dev,
135 struct device_attribute *attr,
136 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct pcmcia_socket *s = to_socket(dev);
139
140 if (!count)
141 return -EINVAL;
142
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100143 pcmcia_parse_uevents(s, PCMCIA_UEVENT_EJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100145 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200147static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200150static ssize_t pccard_show_irq_mask(struct device *dev,
151 struct device_attribute *attr,
152 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct pcmcia_socket *s = to_socket(dev);
155 return sprintf(buf, "0x%04x\n", s->irq_mask);
156}
157
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200158static ssize_t pccard_store_irq_mask(struct device *dev,
159 struct device_attribute *attr,
160 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 ssize_t ret;
163 struct pcmcia_socket *s = to_socket(dev);
164 u32 mask;
165
166 if (!count)
167 return -EINVAL;
168
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100169 ret = sscanf(buf, "0x%x\n", &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (ret == 1) {
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100172 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 s->irq_mask &= mask;
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100174 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ret = 0;
176 }
177
178 return ret ? ret : count;
179}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200180static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200183static ssize_t pccard_show_resource(struct device *dev,
184 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct pcmcia_socket *s = to_socket(dev);
187 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
188}
189
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200190static ssize_t pccard_store_resource(struct device *dev,
191 struct device_attribute *attr,
192 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct pcmcia_socket *s = to_socket(dev);
195
196 if (!count)
197 return -EINVAL;
198
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100199 mutex_lock(&s->ops_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700200 if (!s->resource_setup_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 s->resource_setup_done = 1;
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100202 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100204 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return count;
207}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200208static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
David Brownell4356d732008-04-28 01:03:20 -0700210static struct attribute *pccard_socket_attributes[] = {
211 &dev_attr_card_type.attr,
212 &dev_attr_card_voltage.attr,
213 &dev_attr_card_vpp.attr,
214 &dev_attr_card_vcc.attr,
215 &dev_attr_card_insert.attr,
216 &dev_attr_card_pm_state.attr,
217 &dev_attr_card_eject.attr,
218 &dev_attr_card_irq_mask.attr,
219 &dev_attr_available_resources_setup_done.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 NULL,
221};
222
David Brownell4356d732008-04-28 01:03:20 -0700223static const struct attribute_group socket_attrs = {
224 .attrs = pccard_socket_attributes,
225};
226
David Brownell4356d732008-04-28 01:03:20 -0700227int pccard_sysfs_add_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +0100229 return sysfs_create_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
David Brownell4356d732008-04-28 01:03:20 -0700232void pccard_sysfs_remove_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David Brownell4356d732008-04-28 01:03:20 -0700234 sysfs_remove_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}