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/pci.h>
26 #include <linux/device.h>
27 #include <linux/mutex.h>
28 #include <asm/system.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
))
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
))
62 return sprintf(buf
, "%d.%dV\n", 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
))
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
))
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
)
90 struct pcmcia_socket
*s
= to_socket(dev
);
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
);
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
)
128 struct pcmcia_socket
*s
= to_socket(dev
);
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
)
149 struct pcmcia_socket
*s
= to_socket(dev
);
155 ret
= sscanf (buf
, "0x%x\n", &mask
);
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
)
176 struct pcmcia_socket
*s
= to_socket(dev
);
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
);
188 (s
->state
& SOCKET_PRESENT
) &&
189 !(s
->state
& SOCKET_CARDBUS
)) {
190 if (try_module_get(s
->callback
->owner
)) {
191 s
->callback
->requery(s
);
192 module_put(s
->callback
->owner
);
195 mutex_unlock(&s
->skt_mutex
);
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
)
211 tuplebuffer
= kmalloc(sizeof(u_char
) * 256, GFP_KERNEL
);
215 tempbuffer
= kmalloc(sizeof(u_char
) * 258, GFP_KERNEL
);
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
);
229 tuple
.TupleData
= tuplebuffer
;
230 tuple
.TupleDataMax
= 255;
231 memset(tuplebuffer
, 0, sizeof(u_char
) * 255);
233 status
= pccard_get_tuple_data(s
, &tuple
);
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
];
252 pointer
+= 2 + tuple
.TupleDataLen
;
254 if (pointer
>= (off
+ count
))
257 if (tuple
.TupleCode
== CISTPL_END
)
259 status
= pccard_get_next_tuple(s
, BIND_FN_ALL
, &tuple
);
269 static ssize_t
pccard_show_cis(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
271 unsigned int size
= 0x200;
276 struct pcmcia_socket
*s
;
279 if (off
+ count
> size
)
282 s
= to_socket(container_of(kobj
, struct class_device
, kobj
));
284 if (!(s
->state
& SOCKET_PRESENT
))
286 if (pccard_validate_cis(s
, BIND_FN_ALL
, &cisinfo
))
291 count
= pccard_extract_cis(s
, buf
, off
, 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
));
309 if (!(s
->state
& SOCKET_PRESENT
))
312 cis
= kzalloc(sizeof(cisdump_t
), GFP_KERNEL
);
316 cis
->Length
= count
+ 1;
317 memcpy(cis
->Data
, buf
, count
);
319 if (pcmcia_replace_cis(s
, cis
))
325 mutex_lock(&s
->skt_mutex
);
326 if ((s
->callback
) && (s
->state
& SOCKET_PRESENT
) &&
327 !(s
->state
& SOCKET_CARDBUS
)) {
328 if (try_module_get(s
->callback
->owner
)) {
329 s
->callback
->requery(s
);
330 module_put(s
->callback
->owner
);
333 mutex_unlock(&s
->skt_mutex
);
341 static struct class_device_attribute
*pccard_socket_attributes
[] = {
342 &class_device_attr_card_type
,
343 &class_device_attr_card_voltage
,
344 &class_device_attr_card_vpp
,
345 &class_device_attr_card_vcc
,
346 &class_device_attr_card_insert
,
347 &class_device_attr_card_pm_state
,
348 &class_device_attr_card_eject
,
349 &class_device_attr_card_irq_mask
,
350 &class_device_attr_available_resources_setup_done
,
354 static struct bin_attribute pccard_cis_attr
= {
355 .attr
= { .name
= "cis", .mode
= S_IRUGO
| S_IWUSR
, .owner
= THIS_MODULE
},
357 .read
= pccard_show_cis
,
358 .write
= pccard_store_cis
,
361 static int __devinit
pccard_sysfs_add_socket(struct class_device
*class_dev
,
362 struct class_interface
*class_intf
)
364 struct class_device_attribute
**attr
;
367 for (attr
= pccard_socket_attributes
; *attr
; attr
++) {
368 ret
= class_device_create_file(class_dev
, *attr
);
373 ret
= sysfs_create_bin_file(&class_dev
->kobj
, &pccard_cis_attr
);
378 static void __devexit
pccard_sysfs_remove_socket(struct class_device
*class_dev
,
379 struct class_interface
*class_intf
)
381 struct class_device_attribute
**attr
;
383 sysfs_remove_bin_file(&class_dev
->kobj
, &pccard_cis_attr
);
384 for (attr
= pccard_socket_attributes
; *attr
; attr
++)
385 class_device_remove_file(class_dev
, *attr
);
388 struct class_interface pccard_sysfs_interface
= {
389 .class = &pcmcia_socket_class
,
390 .add
= &pccard_sysfs_add_socket
,
391 .remove
= __devexit_p(&pccard_sysfs_remove_socket
),