blob: d1b220a1e1abb2f92bda817e0e70a52309c98d8a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * socket_sysfs.c -- most of socket-related sysfs output
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * (C) 2003 - 2004 Dominik Brodowski
6 */
7
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/string.h>
13#include <linux/major.h>
14#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
16#include <linux/interrupt.h>
17#include <linux/timer.h>
18#include <linux/ioport.h>
19#include <linux/delay.h>
20#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
Dominik Brodowski7fe908d2006-01-10 21:20:36 +010022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/irq.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <pcmcia/ss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <pcmcia/cistpl.h>
27#include <pcmcia/cisreg.h>
28#include <pcmcia/ds.h>
29#include "cs_internal.h"
30
31#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
32
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020033static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
34 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct pcmcia_socket *s = to_socket(dev);
37
38 if (!(s->state & SOCKET_PRESENT))
39 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010040 if (s->state & SOCKET_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return sprintf(buf, "32-bit\n");
Dominik Brodowski002dbb22005-12-08 23:50:36 +010042 return sprintf(buf, "16-bit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020044static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020046static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
47 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct pcmcia_socket *s = to_socket(dev);
50
51 if (!(s->state & SOCKET_PRESENT))
52 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010053 if (s->socket.Vcc)
54 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
55 s->socket.Vcc % 10);
56 return sprintf(buf, "X.XV\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020058static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020060static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
61 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct pcmcia_socket *s = to_socket(dev);
64 if (!(s->state & SOCKET_PRESENT))
65 return -ENODEV;
66 return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
67}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020068static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020070static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
71 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct pcmcia_socket *s = to_socket(dev);
74 if (!(s->state & SOCKET_PRESENT))
75 return -ENODEV;
76 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
77}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020078static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020081static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
82 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct pcmcia_socket *s = to_socket(dev);
85
86 if (!count)
87 return -EINVAL;
88
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010089 pcmcia_parse_uevents(s, PCMCIA_UEVENT_INSERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Dominik Brodowskif971dbd2010-01-17 18:13:31 +010091 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020093static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Dominik Brodowskidb1019c2006-01-10 19:19:37 +010095
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020096static ssize_t pccard_show_card_pm_state(struct device *dev,
97 struct device_attribute *attr,
98 char *buf)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +010099{
100 struct pcmcia_socket *s = to_socket(dev);
101 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
102}
103
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200104static ssize_t pccard_store_card_pm_state(struct device *dev,
105 struct device_attribute *attr,
106 const char *buf, size_t count)
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100107{
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100108 struct pcmcia_socket *s = to_socket(dev);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100109 ssize_t ret = count;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100110
111 if (!count)
112 return -EINVAL;
113
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100114 if (!strncmp(buf, "off", 3))
115 pcmcia_parse_uevents(s, PCMCIA_UEVENT_SUSPEND);
116 else {
117 if (!strncmp(buf, "on", 2))
118 pcmcia_parse_uevents(s, PCMCIA_UEVENT_RESUME);
119 else
120 ret = -EINVAL;
121 }
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100122
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100123 return ret;
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100124}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200125static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100126
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200127static ssize_t pccard_store_eject(struct device *dev,
128 struct device_attribute *attr,
129 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct pcmcia_socket *s = to_socket(dev);
132
133 if (!count)
134 return -EINVAL;
135
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100136 pcmcia_parse_uevents(s, PCMCIA_UEVENT_EJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100138 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200140static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200143static ssize_t pccard_show_irq_mask(struct device *dev,
144 struct device_attribute *attr,
145 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct pcmcia_socket *s = to_socket(dev);
148 return sprintf(buf, "0x%04x\n", s->irq_mask);
149}
150
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200151static ssize_t pccard_store_irq_mask(struct device *dev,
152 struct device_attribute *attr,
153 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 ssize_t ret;
156 struct pcmcia_socket *s = to_socket(dev);
157 u32 mask;
158
159 if (!count)
160 return -EINVAL;
161
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100162 ret = sscanf(buf, "0x%x\n", &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (ret == 1) {
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100165 mutex_lock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 s->irq_mask &= mask;
Dominik Brodowski64d8d462010-01-16 01:14:38 +0100167 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 ret = 0;
169 }
170
171 return ret ? ret : count;
172}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200173static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200176static ssize_t pccard_show_resource(struct device *dev,
177 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct pcmcia_socket *s = to_socket(dev);
180 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
181}
182
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200183static ssize_t pccard_store_resource(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct pcmcia_socket *s = to_socket(dev);
188
189 if (!count)
190 return -EINVAL;
191
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100192 mutex_lock(&s->ops_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700193 if (!s->resource_setup_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 s->resource_setup_done = 1;
Dominik Brodowski00ce99f2010-01-16 09:14:11 +0100195 mutex_unlock(&s->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100197 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return count;
200}
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200201static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
David Brownell4356d732008-04-28 01:03:20 -0700203static struct attribute *pccard_socket_attributes[] = {
204 &dev_attr_card_type.attr,
205 &dev_attr_card_voltage.attr,
206 &dev_attr_card_vpp.attr,
207 &dev_attr_card_vcc.attr,
208 &dev_attr_card_insert.attr,
209 &dev_attr_card_pm_state.attr,
210 &dev_attr_card_eject.attr,
211 &dev_attr_card_irq_mask.attr,
212 &dev_attr_available_resources_setup_done.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 NULL,
214};
215
David Brownell4356d732008-04-28 01:03:20 -0700216static const struct attribute_group socket_attrs = {
217 .attrs = pccard_socket_attributes,
218};
219
David Brownell4356d732008-04-28 01:03:20 -0700220int pccard_sysfs_add_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Dominik Brodowski6e7b51a2010-01-06 13:57:43 +0100222 return sysfs_create_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
David Brownell4356d732008-04-28 01:03:20 -0700225void pccard_sysfs_remove_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
David Brownell4356d732008-04-28 01:03:20 -0700227 sysfs_remove_group(&dev->kobj, &socket_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}