Linux 2.6.21.1
[linux/fpc-iii.git] / drivers / pcmcia / socket_sysfs.c
blobea5765c3bdc0aa0bd4e808babe261f2ca6f5f9c5
1 /*
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
9 */
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>
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>
25 #include <linux/pci.h>
26 #include <linux/device.h>
27 #include <linux/mutex.h>
28 #include <asm/system.h>
29 #include <asm/irq.h>
31 #define IN_CARD_SERVICES
32 #include <pcmcia/cs_types.h>
33 #include <pcmcia/ss.h>
34 #include <pcmcia/cs.h>
35 #include <pcmcia/bulkmem.h>
36 #include <pcmcia/cistpl.h>
37 #include <pcmcia/cisreg.h>
38 #include <pcmcia/ds.h>
39 #include "cs_internal.h"
41 #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
43 static ssize_t pccard_show_type(struct device *dev, struct device_attribute *attr,
44 char *buf)
46 struct pcmcia_socket *s = to_socket(dev);
48 if (!(s->state & SOCKET_PRESENT))
49 return -ENODEV;
50 if (s->state & SOCKET_CARDBUS)
51 return sprintf(buf, "32-bit\n");
52 return sprintf(buf, "16-bit\n");
54 static DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
56 static ssize_t pccard_show_voltage(struct device *dev, struct device_attribute *attr,
57 char *buf)
59 struct pcmcia_socket *s = to_socket(dev);
61 if (!(s->state & SOCKET_PRESENT))
62 return -ENODEV;
63 if (s->socket.Vcc)
64 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
65 s->socket.Vcc % 10);
66 return sprintf(buf, "X.XV\n");
68 static DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
70 static ssize_t pccard_show_vpp(struct device *dev, struct device_attribute *attr,
71 char *buf)
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.Vpp / 10, s->socket.Vpp % 10);
78 static DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
80 static ssize_t pccard_show_vcc(struct device *dev, struct device_attribute *attr,
81 char *buf)
83 struct pcmcia_socket *s = to_socket(dev);
84 if (!(s->state & SOCKET_PRESENT))
85 return -ENODEV;
86 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
88 static DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
91 static ssize_t pccard_store_insert(struct device *dev, struct device_attribute *attr,
92 const char *buf, size_t count)
94 ssize_t ret;
95 struct pcmcia_socket *s = to_socket(dev);
97 if (!count)
98 return -EINVAL;
100 ret = pcmcia_insert_card(s);
102 return ret ? ret : count;
104 static DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
107 static ssize_t pccard_show_card_pm_state(struct device *dev,
108 struct device_attribute *attr,
109 char *buf)
111 struct pcmcia_socket *s = to_socket(dev);
112 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
115 static ssize_t pccard_store_card_pm_state(struct device *dev,
116 struct device_attribute *attr,
117 const char *buf, size_t count)
119 ssize_t ret = -EINVAL;
120 struct pcmcia_socket *s = to_socket(dev);
122 if (!count)
123 return -EINVAL;
125 if (!(s->state & SOCKET_SUSPEND) && !strncmp(buf, "off", 3))
126 ret = pcmcia_suspend_card(s);
127 else if ((s->state & SOCKET_SUSPEND) && !strncmp(buf, "on", 2))
128 ret = pcmcia_resume_card(s);
130 return ret ? -ENODEV : count;
132 static DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
134 static ssize_t pccard_store_eject(struct device *dev,
135 struct device_attribute *attr,
136 const char *buf, size_t count)
138 ssize_t ret;
139 struct pcmcia_socket *s = to_socket(dev);
141 if (!count)
142 return -EINVAL;
144 ret = pcmcia_eject_card(s);
146 return ret ? ret : count;
148 static DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
151 static ssize_t pccard_show_irq_mask(struct device *dev,
152 struct device_attribute *attr,
153 char *buf)
155 struct pcmcia_socket *s = to_socket(dev);
156 return sprintf(buf, "0x%04x\n", s->irq_mask);
159 static ssize_t pccard_store_irq_mask(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t count)
163 ssize_t ret;
164 struct pcmcia_socket *s = to_socket(dev);
165 u32 mask;
167 if (!count)
168 return -EINVAL;
170 ret = sscanf (buf, "0x%x\n", &mask);
172 if (ret == 1) {
173 s->irq_mask &= mask;
174 ret = 0;
177 return ret ? ret : count;
179 static DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
182 static ssize_t pccard_show_resource(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct pcmcia_socket *s = to_socket(dev);
186 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
189 static ssize_t pccard_store_resource(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t count)
193 unsigned long flags;
194 struct pcmcia_socket *s = to_socket(dev);
196 if (!count)
197 return -EINVAL;
199 spin_lock_irqsave(&s->lock, flags);
200 if (!s->resource_setup_done)
201 s->resource_setup_done = 1;
202 spin_unlock_irqrestore(&s->lock, flags);
204 mutex_lock(&s->skt_mutex);
205 if ((s->callback) &&
206 (s->state & SOCKET_PRESENT) &&
207 !(s->state & SOCKET_CARDBUS)) {
208 if (try_module_get(s->callback->owner)) {
209 s->callback->requery(s, 0);
210 module_put(s->callback->owner);
213 mutex_unlock(&s->skt_mutex);
215 return count;
217 static DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
220 static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, loff_t off, size_t count)
222 tuple_t tuple;
223 int status, i;
224 loff_t pointer = 0;
225 ssize_t ret = 0;
226 u_char *tuplebuffer;
227 u_char *tempbuffer;
229 tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL);
230 if (!tuplebuffer)
231 return -ENOMEM;
233 tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL);
234 if (!tempbuffer) {
235 ret = -ENOMEM;
236 goto free_tuple;
239 memset(&tuple, 0, sizeof(tuple_t));
241 tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON;
242 tuple.DesiredTuple = RETURN_FIRST_TUPLE;
243 tuple.TupleOffset = 0;
245 status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple);
246 while (!status) {
247 tuple.TupleData = tuplebuffer;
248 tuple.TupleDataMax = 255;
249 memset(tuplebuffer, 0, sizeof(u_char) * 255);
251 status = pccard_get_tuple_data(s, &tuple);
252 if (status)
253 break;
255 if (off < (pointer + 2 + tuple.TupleDataLen)) {
256 tempbuffer[0] = tuple.TupleCode & 0xff;
257 tempbuffer[1] = tuple.TupleLink & 0xff;
258 for (i = 0; i < tuple.TupleDataLen; i++)
259 tempbuffer[i + 2] = tuplebuffer[i] & 0xff;
261 for (i = 0; i < (2 + tuple.TupleDataLen); i++) {
262 if (((i + pointer) >= off) &&
263 (i + pointer) < (off + count)) {
264 buf[ret] = tempbuffer[i];
265 ret++;
270 pointer += 2 + tuple.TupleDataLen;
272 if (pointer >= (off + count))
273 break;
275 if (tuple.TupleCode == CISTPL_END)
276 break;
277 status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple);
280 kfree(tempbuffer);
281 free_tuple:
282 kfree(tuplebuffer);
284 return (ret);
287 static ssize_t pccard_show_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
289 unsigned int size = 0x200;
291 if (off >= size)
292 count = 0;
293 else {
294 struct pcmcia_socket *s;
295 cisinfo_t cisinfo;
297 if (off + count > size)
298 count = size - off;
300 s = to_socket(container_of(kobj, struct device, kobj));
302 if (!(s->state & SOCKET_PRESENT))
303 return -ENODEV;
304 if (pccard_validate_cis(s, BIND_FN_ALL, &cisinfo))
305 return -EIO;
306 if (!cisinfo.Chains)
307 return -ENODATA;
309 count = pccard_extract_cis(s, buf, off, count);
312 return (count);
315 static ssize_t pccard_store_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
317 struct pcmcia_socket *s = to_socket(container_of(kobj, struct device, kobj));
318 cisdump_t *cis;
319 int error;
321 if (off)
322 return -EINVAL;
324 if (count >= 0x200)
325 return -EINVAL;
327 if (!(s->state & SOCKET_PRESENT))
328 return -ENODEV;
330 cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
331 if (!cis)
332 return -ENOMEM;
334 cis->Length = count + 1;
335 memcpy(cis->Data, buf, count);
337 error = pcmcia_replace_cis(s, cis);
338 kfree(cis);
339 if (error)
340 return -EIO;
342 mutex_lock(&s->skt_mutex);
343 if ((s->callback) && (s->state & SOCKET_PRESENT) &&
344 !(s->state & SOCKET_CARDBUS)) {
345 if (try_module_get(s->callback->owner)) {
346 s->callback->requery(s, 1);
347 module_put(s->callback->owner);
350 mutex_unlock(&s->skt_mutex);
352 return count;
356 static struct device_attribute *pccard_socket_attributes[] = {
357 &dev_attr_card_type,
358 &dev_attr_card_voltage,
359 &dev_attr_card_vpp,
360 &dev_attr_card_vcc,
361 &dev_attr_card_insert,
362 &dev_attr_card_pm_state,
363 &dev_attr_card_eject,
364 &dev_attr_card_irq_mask,
365 &dev_attr_available_resources_setup_done,
366 NULL,
369 static struct bin_attribute pccard_cis_attr = {
370 .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE},
371 .size = 0x200,
372 .read = pccard_show_cis,
373 .write = pccard_store_cis,
376 static int __devinit pccard_sysfs_add_socket(struct device *dev,
377 struct class_interface *class_intf)
379 struct device_attribute **attr;
380 int ret = 0;
382 for (attr = pccard_socket_attributes; *attr; attr++) {
383 ret = device_create_file(dev, *attr);
384 if (ret)
385 break;
387 if (!ret)
388 ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
390 return ret;
393 static void __devexit pccard_sysfs_remove_socket(struct device *dev,
394 struct class_interface *class_intf)
396 struct device_attribute **attr;
398 sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
399 for (attr = pccard_socket_attributes; *attr; attr++)
400 device_remove_file(dev, *attr);
403 struct class_interface pccard_sysfs_interface = {
404 .class = &pcmcia_socket_class,
405 .add_dev = &pccard_sysfs_add_socket,
406 .remove_dev = __devexit_p(&pccard_sysfs_remove_socket),