blob: 80e36bc407dac811ab1b6e9e54c1b8bc36f79e77 [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/system.h>
27#include <asm/irq.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <pcmcia/cs_types.h>
30#include <pcmcia/ss.h>
31#include <pcmcia/cs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <pcmcia/cistpl.h>
33#include <pcmcia/cisreg.h>
34#include <pcmcia/ds.h>
35#include "cs_internal.h"
36
37#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
38
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020039static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
40 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct pcmcia_socket *s = to_socket(dev);
43
44 if (!(s->state & SOCKET_PRESENT))
45 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010046 if (s->state & SOCKET_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return sprintf(buf, "32-bit\n");
Dominik Brodowski002dbb22005-12-08 23:50:36 +010048 return sprintf(buf, "16-bit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020050static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020052static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
53 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct pcmcia_socket *s = to_socket(dev);
56
57 if (!(s->state & SOCKET_PRESENT))
58 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010059 if (s->socket.Vcc)
60 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
61 s->socket.Vcc % 10);
62 return sprintf(buf, "X.XV\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020064static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020066static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
67 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct pcmcia_socket *s = to_socket(dev);
70 if (!(s->state & SOCKET_PRESENT))
71 return -ENODEV;
72 return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
73}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020074static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020076static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
77 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct pcmcia_socket *s = to_socket(dev);
80 if (!(s->state & SOCKET_PRESENT))
81 return -ENODEV;
82 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
83}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020084static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020087static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
88 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct pcmcia_socket *s = to_socket(dev);
91
92 if (!count)
93 return -EINVAL;
94
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010095 pcmcia_parse_uevents(s, PCMCIA_UEVENT_INSERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010097 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020099static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100101
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200102static ssize_t pccard_show_card_pm_state(struct device *dev,
103 struct device_attribute *attr,
104 char *buf)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100105{
106 struct pcmcia_socket *s = to_socket(dev);
107 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
108}
109
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200110static ssize_t pccard_store_card_pm_state(struct device *dev,
111 struct device_attribute *attr,
112 const char *buf, size_t count)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100113{
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100114 struct pcmcia_socket *s = to_socket(dev);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100115 ssize_t ret = count;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100116
117 if (!count)
118 return -EINVAL;
119
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100120 if (!strncmp(buf, "off", 3))
121 pcmcia_parse_uevents(s, PCMCIA_UEVENT_SUSPEND);
122 else {
123 if (!strncmp(buf, "on", 2))
124 pcmcia_parse_uevents(s, PCMCIA_UEVENT_RESUME);
125 else
126 ret = -EINVAL;
127 }
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100128
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100129 return ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100130}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200131static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100132
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200133static ssize_t pccard_store_eject(struct device *dev,
134 struct device_attribute *attr,
135 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct pcmcia_socket *s = to_socket(dev);
138
139 if (!count)
140 return -EINVAL;
141
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100142 pcmcia_parse_uevents(s, PCMCIA_UEVENT_EJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100144 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200146static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200149static ssize_t pccard_show_irq_mask(struct device *dev,
150 struct device_attribute *attr,
151 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct pcmcia_socket *s = to_socket(dev);
154 return sprintf(buf, "0x%04x\n", s->irq_mask);
155}
156
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200157static ssize_t pccard_store_irq_mask(struct device *dev,
158 struct device_attribute *attr,
159 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 ssize_t ret;
162 struct pcmcia_socket *s = to_socket(dev);
163 u32 mask;
164
165 if (!count)
166 return -EINVAL;
167
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100168 ret = sscanf(buf, "0x%x\n", &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 if (ret == 1) {
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100171 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 s->irq_mask &= mask;
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100173 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ret = 0;
175 }
176
177 return ret ? ret : count;
178}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200179static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200182static ssize_t pccard_show_resource(struct device *dev,
183 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct pcmcia_socket *s = to_socket(dev);
186 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
187}
188
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200189static ssize_t pccard_store_resource(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct pcmcia_socket *s = to_socket(dev);
194
195 if (!count)
196 return -EINVAL;
197
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100198 mutex_lock(&s->ops_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700199 if (!s->resource_setup_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 s->resource_setup_done = 1;
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100201 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100203 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return count;
206}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200207static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David Brownell4356d732008-04-28 01:03:20 -0700209static struct attribute *pccard_socket_attributes[] = {
210 &dev_attr_card_type.attr,
211 &dev_attr_card_voltage.attr,
212 &dev_attr_card_vpp.attr,
213 &dev_attr_card_vcc.attr,
214 &dev_attr_card_insert.attr,
215 &dev_attr_card_pm_state.attr,
216 &dev_attr_card_eject.attr,
217 &dev_attr_card_irq_mask.attr,
218 &dev_attr_available_resources_setup_done.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 NULL,
220};
221
David Brownell4356d732008-04-28 01:03:20 -0700222static const struct attribute_group socket_attrs = {
223 .attrs = pccard_socket_attributes,
224};
225
David Brownell4356d732008-04-28 01:03:20 -0700226int pccard_sysfs_add_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +0100228 return sysfs_create_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
David Brownell4356d732008-04-28 01:03:20 -0700231void pccard_sysfs_remove_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
David Brownell4356d732008-04-28 01:03:20 -0700233 sysfs_remove_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}