2 * socket_sysfs.c -- most of socket-related sysfs output
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.
8 * (C) 2003 - 2004 Dominik Brodowski
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/major.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <linux/timer.h>
22 #include <linux/ioport.h>
23 #include <linux/delay.h>
25 #include <linux/device.h>
26 #include <linux/mutex.h>
27 #include <asm/system.h>
30 #include <pcmcia/cs_types.h>
31 #include <pcmcia/ss.h>
32 #include <pcmcia/cs.h>
33 #include <pcmcia/cistpl.h>
34 #include <pcmcia/cisreg.h>
35 #include <pcmcia/ds.h>
36 #include "cs_internal.h"
38 #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
40 static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
43 struct pcmcia_socket *s = to_socket(dev);
45 if (!(s->state & SOCKET_PRESENT))
47 if (s->state & SOCKET_CARDBUS)
48 return sprintf(buf, "32-bit\n");
49 return sprintf(buf, "16-bit\n");
51 static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
53 static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
56 struct pcmcia_socket *s = to_socket(dev);
58 if (!(s->state & SOCKET_PRESENT))
61 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
63 return sprintf(buf, "X.XV\n");
65 static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
67 static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
70 struct pcmcia_socket *s = to_socket(dev);
71 if (!(s->state & SOCKET_PRESENT))
73 return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
75 static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
77 static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
80 struct pcmcia_socket *s = to_socket(dev);
81 if (!(s->state & SOCKET_PRESENT))
83 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
85 static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
88 static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
89 const char *buf, size_t count)
92 struct pcmcia_socket *s = to_socket(dev);
97 ret = pcmcia_insert_card(s);
99 return ret ? ret : count;
101 static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
104 static ssize_t pccard_show_card_pm_state(struct device *dev,
105 struct device_attribute *attr,
108 struct pcmcia_socket *s = to_socket(dev);
109 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
112 static ssize_t pccard_store_card_pm_state(struct device *dev,
113 struct device_attribute *attr,
114 const char *buf, size_t count)
116 ssize_t ret = -EINVAL;
117 struct pcmcia_socket *s = to_socket(dev);
122 if (!(s->state & SOCKET_SUSPEND) && !strncmp(buf, "off", 3))
123 ret = pcmcia_suspend_card(s);
124 else if ((s->state & SOCKET_SUSPEND) && !strncmp(buf, "on", 2))
125 ret = pcmcia_resume_card(s);
127 return ret ? -ENODEV : count;
129 static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
131 static ssize_t pccard_store_eject(struct device *dev,
132 struct device_attribute *attr,
133 const char *buf, size_t count)
136 struct pcmcia_socket *s = to_socket(dev);
141 ret = pcmcia_eject_card(s);
143 return ret ? ret : count;
145 static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
148 static ssize_t pccard_show_irq_mask(struct device *dev,
149 struct device_attribute *attr,
152 struct pcmcia_socket *s = to_socket(dev);
153 return sprintf(buf, "0x%04x\n", s->irq_mask);
156 static ssize_t pccard_store_irq_mask(struct device *dev,
157 struct device_attribute *attr,
158 const char *buf, size_t count)
161 struct pcmcia_socket *s = to_socket(dev);
167 ret = sscanf (buf, "0x%x\n", &mask);
174 return ret ? ret : count;
176 static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
179 static ssize_t pccard_show_resource(struct device *dev,
180 struct device_attribute *attr, char *buf)
182 struct pcmcia_socket *s = to_socket(dev);
183 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
186 static ssize_t pccard_store_resource(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
191 struct pcmcia_socket *s = to_socket(dev);
196 spin_lock_irqsave(&s->lock, flags);
197 if (!s->resource_setup_done)
198 s->resource_setup_done = 1;
199 spin_unlock_irqrestore(&s->lock, flags);
201 mutex_lock(&s->skt_mutex);
203 (s->state & SOCKET_PRESENT) &&
204 !(s->state & SOCKET_CARDBUS)) {
205 if (try_module_get(s->callback->owner)) {
206 s->callback->requery(s, 0);
207 module_put(s->callback->owner);
210 mutex_unlock(&s->skt_mutex);
214 static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
217 static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, loff_t off, size_t count)
226 tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL);
230 tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL);
236 memset(&tuple, 0, sizeof(tuple_t));
238 tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON;
239 tuple.DesiredTuple = RETURN_FIRST_TUPLE;
240 tuple.TupleOffset = 0;
242 status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple);
244 tuple.TupleData = tuplebuffer;
245 tuple.TupleDataMax = 255;
246 memset(tuplebuffer, 0, sizeof(u_char) * 255);
248 status = pccard_get_tuple_data(s, &tuple);
252 if (off < (pointer + 2 + tuple.TupleDataLen)) {
253 tempbuffer[0] = tuple.TupleCode & 0xff;
254 tempbuffer[1] = tuple.TupleLink & 0xff;
255 for (i = 0; i < tuple.TupleDataLen; i++)
256 tempbuffer[i + 2] = tuplebuffer[i] & 0xff;
258 for (i = 0; i < (2 + tuple.TupleDataLen); i++) {
259 if (((i + pointer) >= off) &&
260 (i + pointer) < (off + count)) {
261 buf[ret] = tempbuffer[i];
267 pointer += 2 + tuple.TupleDataLen;
269 if (pointer >= (off + count))
272 if (tuple.TupleCode == CISTPL_END)
274 status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple);
284 static ssize_t pccard_show_cis(struct kobject *kobj,
285 struct bin_attribute *bin_attr,
286 char *buf, loff_t off, size_t count)
288 unsigned int size = 0x200;
293 struct pcmcia_socket *s;
296 if (off + count > size)
299 s = to_socket(container_of(kobj, struct device, kobj));
301 if (!(s->state & SOCKET_PRESENT))
303 if (pccard_validate_cis(s, BIND_FN_ALL, &chains))
308 count = pccard_extract_cis(s, buf, off, count);
314 static ssize_t pccard_store_cis(struct kobject *kobj,
315 struct bin_attribute *bin_attr,
316 char *buf, loff_t off, size_t count)
318 struct pcmcia_socket *s = to_socket(container_of(kobj, struct device, kobj));
328 if (!(s->state & SOCKET_PRESENT))
331 cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
335 cis->Length = count + 1;
336 memcpy(cis->Data, buf, count);
338 error = pcmcia_replace_cis(s, cis);
343 mutex_lock(&s->skt_mutex);
344 if ((s->callback) && (s->state & SOCKET_PRESENT) &&
345 !(s->state & SOCKET_CARDBUS)) {
346 if (try_module_get(s->callback->owner)) {
347 s->callback->requery(s, 1);
348 module_put(s->callback->owner);
351 mutex_unlock(&s->skt_mutex);
357 static struct attribute *pccard_socket_attributes[] = {
358 &dev_attr_card_type.attr,
359 &dev_attr_card_voltage.attr,
360 &dev_attr_card_vpp.attr,
361 &dev_attr_card_vcc.attr,
362 &dev_attr_card_insert.attr,
363 &dev_attr_card_pm_state.attr,
364 &dev_attr_card_eject.attr,
365 &dev_attr_card_irq_mask.attr,
366 &dev_attr_available_resources_setup_done.attr,
370 static const struct attribute_group socket_attrs = {
371 .attrs = pccard_socket_attributes,
374 static struct bin_attribute pccard_cis_attr = {
375 .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR },
377 .read = pccard_show_cis,
378 .write = pccard_store_cis,
381 int pccard_sysfs_add_socket(struct device *dev)
385 ret = sysfs_create_group(&dev->kobj, &socket_attrs);
387 ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
389 sysfs_remove_group(&dev->kobj, &socket_attrs);
394 void pccard_sysfs_remove_socket(struct device *dev)
396 sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
397 sysfs_remove_group(&dev->kobj, &socket_attrs);