[PATCH] notifiers: fix blocking_notifier_call_chain() scalability
[linux/fpc-iii.git] / drivers / pcmcia / socket_sysfs.c
blobb005602d6b5376e92f3b62f6e4a9d8fb1acccfe3
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 class_device *dev, char *buf)
45 struct pcmcia_socket *s = to_socket(dev);
47 if (!(s->state & SOCKET_PRESENT))
48 return -ENODEV;
49 if (s->state & SOCKET_CARDBUS)
50 return sprintf(buf, "32-bit\n");
51 return sprintf(buf, "16-bit\n");
53 static CLASS_DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
55 static ssize_t pccard_show_voltage(struct class_device *dev, char *buf)
57 struct pcmcia_socket *s = to_socket(dev);
59 if (!(s->state & SOCKET_PRESENT))
60 return -ENODEV;
61 if (s->socket.Vcc)
62 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
63 s->socket.Vcc % 10);
64 return sprintf(buf, "X.XV\n");
66 static CLASS_DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
68 static ssize_t pccard_show_vpp(struct class_device *dev, char *buf)
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);
75 static CLASS_DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
77 static ssize_t pccard_show_vcc(struct class_device *dev, char *buf)
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);
84 static CLASS_DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
87 static ssize_t pccard_store_insert(struct class_device *dev, const char *buf, size_t count)
89 ssize_t ret;
90 struct pcmcia_socket *s = to_socket(dev);
92 if (!count)
93 return -EINVAL;
95 ret = pcmcia_insert_card(s);
97 return ret ? ret : count;
99 static CLASS_DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
102 static ssize_t pccard_show_card_pm_state(struct class_device *dev, char *buf)
104 struct pcmcia_socket *s = to_socket(dev);
105 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
108 static ssize_t pccard_store_card_pm_state(struct class_device *dev, const char *buf, size_t count)
110 ssize_t ret = -EINVAL;
111 struct pcmcia_socket *s = to_socket(dev);
113 if (!count)
114 return -EINVAL;
116 if (!(s->state & SOCKET_SUSPEND) && !strncmp(buf, "off", 3))
117 ret = pcmcia_suspend_card(s);
118 else if ((s->state & SOCKET_SUSPEND) && !strncmp(buf, "on", 2))
119 ret = pcmcia_resume_card(s);
121 return ret ? -ENODEV : count;
123 static CLASS_DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
125 static ssize_t pccard_store_eject(struct class_device *dev, const char *buf, size_t count)
127 ssize_t ret;
128 struct pcmcia_socket *s = to_socket(dev);
130 if (!count)
131 return -EINVAL;
133 ret = pcmcia_eject_card(s);
135 return ret ? ret : count;
137 static CLASS_DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
140 static ssize_t pccard_show_irq_mask(struct class_device *dev, char *buf)
142 struct pcmcia_socket *s = to_socket(dev);
143 return sprintf(buf, "0x%04x\n", s->irq_mask);
146 static ssize_t pccard_store_irq_mask(struct class_device *dev, const char *buf, size_t count)
148 ssize_t ret;
149 struct pcmcia_socket *s = to_socket(dev);
150 u32 mask;
152 if (!count)
153 return -EINVAL;
155 ret = sscanf (buf, "0x%x\n", &mask);
157 if (ret == 1) {
158 s->irq_mask &= mask;
159 ret = 0;
162 return ret ? ret : count;
164 static CLASS_DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
167 static ssize_t pccard_show_resource(struct class_device *dev, char *buf)
169 struct pcmcia_socket *s = to_socket(dev);
170 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
173 static ssize_t pccard_store_resource(struct class_device *dev, const char *buf, size_t count)
175 unsigned long flags;
176 struct pcmcia_socket *s = to_socket(dev);
178 if (!count)
179 return -EINVAL;
181 spin_lock_irqsave(&s->lock, flags);
182 if (!s->resource_setup_done)
183 s->resource_setup_done = 1;
184 spin_unlock_irqrestore(&s->lock, flags);
186 mutex_lock(&s->skt_mutex);
187 if ((s->callback) &&
188 (s->state & SOCKET_PRESENT) &&
189 !(s->state & SOCKET_CARDBUS)) {
190 if (try_module_get(s->callback->owner)) {
191 s->callback->requery(s, 0);
192 module_put(s->callback->owner);
195 mutex_unlock(&s->skt_mutex);
197 return count;
199 static CLASS_DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
202 static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, loff_t off, size_t count)
204 tuple_t tuple;
205 int status, i;
206 loff_t pointer = 0;
207 ssize_t ret = 0;
208 u_char *tuplebuffer;
209 u_char *tempbuffer;
211 tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL);
212 if (!tuplebuffer)
213 return -ENOMEM;
215 tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL);
216 if (!tempbuffer) {
217 ret = -ENOMEM;
218 goto free_tuple;
221 memset(&tuple, 0, sizeof(tuple_t));
223 tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON;
224 tuple.DesiredTuple = RETURN_FIRST_TUPLE;
225 tuple.TupleOffset = 0;
227 status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple);
228 while (!status) {
229 tuple.TupleData = tuplebuffer;
230 tuple.TupleDataMax = 255;
231 memset(tuplebuffer, 0, sizeof(u_char) * 255);
233 status = pccard_get_tuple_data(s, &tuple);
234 if (status)
235 break;
237 if (off < (pointer + 2 + tuple.TupleDataLen)) {
238 tempbuffer[0] = tuple.TupleCode & 0xff;
239 tempbuffer[1] = tuple.TupleLink & 0xff;
240 for (i = 0; i < tuple.TupleDataLen; i++)
241 tempbuffer[i + 2] = tuplebuffer[i] & 0xff;
243 for (i = 0; i < (2 + tuple.TupleDataLen); i++) {
244 if (((i + pointer) >= off) &&
245 (i + pointer) < (off + count)) {
246 buf[ret] = tempbuffer[i];
247 ret++;
252 pointer += 2 + tuple.TupleDataLen;
254 if (pointer >= (off + count))
255 break;
257 if (tuple.TupleCode == CISTPL_END)
258 break;
259 status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple);
262 kfree(tempbuffer);
263 free_tuple:
264 kfree(tuplebuffer);
266 return (ret);
269 static ssize_t pccard_show_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
271 unsigned int size = 0x200;
273 if (off >= size)
274 count = 0;
275 else {
276 struct pcmcia_socket *s;
277 cisinfo_t cisinfo;
279 if (off + count > size)
280 count = size - off;
282 s = to_socket(container_of(kobj, struct class_device, kobj));
284 if (!(s->state & SOCKET_PRESENT))
285 return -ENODEV;
286 if (pccard_validate_cis(s, BIND_FN_ALL, &cisinfo))
287 return -EIO;
288 if (!cisinfo.Chains)
289 return -ENODATA;
291 count = pccard_extract_cis(s, buf, off, count);
294 return (count);
297 static ssize_t pccard_store_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
299 struct pcmcia_socket *s = to_socket(container_of(kobj, struct class_device, kobj));
300 cisdump_t *cis;
301 int error;
303 if (off)
304 return -EINVAL;
306 if (count >= 0x200)
307 return -EINVAL;
309 if (!(s->state & SOCKET_PRESENT))
310 return -ENODEV;
312 cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
313 if (!cis)
314 return -ENOMEM;
316 cis->Length = count + 1;
317 memcpy(cis->Data, buf, count);
319 error = pcmcia_replace_cis(s, cis);
320 kfree(cis);
321 if (error)
322 return -EIO;
324 mutex_lock(&s->skt_mutex);
325 if ((s->callback) && (s->state & SOCKET_PRESENT) &&
326 !(s->state & SOCKET_CARDBUS)) {
327 if (try_module_get(s->callback->owner)) {
328 s->callback->requery(s, 1);
329 module_put(s->callback->owner);
332 mutex_unlock(&s->skt_mutex);
334 return count;
338 static struct class_device_attribute *pccard_socket_attributes[] = {
339 &class_device_attr_card_type,
340 &class_device_attr_card_voltage,
341 &class_device_attr_card_vpp,
342 &class_device_attr_card_vcc,
343 &class_device_attr_card_insert,
344 &class_device_attr_card_pm_state,
345 &class_device_attr_card_eject,
346 &class_device_attr_card_irq_mask,
347 &class_device_attr_available_resources_setup_done,
348 NULL,
351 static struct bin_attribute pccard_cis_attr = {
352 .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE},
353 .size = 0x200,
354 .read = pccard_show_cis,
355 .write = pccard_store_cis,
358 static int __devinit pccard_sysfs_add_socket(struct class_device *class_dev,
359 struct class_interface *class_intf)
361 struct class_device_attribute **attr;
362 int ret = 0;
364 for (attr = pccard_socket_attributes; *attr; attr++) {
365 ret = class_device_create_file(class_dev, *attr);
366 if (ret)
367 break;
369 if (!ret)
370 ret = sysfs_create_bin_file(&class_dev->kobj, &pccard_cis_attr);
372 return ret;
375 static void __devexit pccard_sysfs_remove_socket(struct class_device *class_dev,
376 struct class_interface *class_intf)
378 struct class_device_attribute **attr;
380 sysfs_remove_bin_file(&class_dev->kobj, &pccard_cis_attr);
381 for (attr = pccard_socket_attributes; *attr; attr++)
382 class_device_remove_file(class_dev, *attr);
385 struct class_interface pccard_sysfs_interface = {
386 .class = &pcmcia_socket_class,
387 .add = &pccard_sysfs_add_socket,
388 .remove = __devexit_p(&pccard_sysfs_remove_socket),