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 #define IN_CARD_SERVICES
31 #include <pcmcia/cs_types.h>
32 #include <pcmcia/ss.h>
33 #include <pcmcia/cs.h>
34 #include <pcmcia/bulkmem.h>
35 #include <pcmcia/cistpl.h>
36 #include <pcmcia/cisreg.h>
37 #include <pcmcia/ds.h>
38 #include "cs_internal.h"
40 #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
42 static ssize_t
pccard_show_type(struct device
*dev
, struct device_attribute
*attr
,
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 DEVICE_ATTR(card_type
, 0444, pccard_show_type
, NULL
);
55 static ssize_t
pccard_show_voltage(struct device
*dev
, struct device_attribute
*attr
,
58 struct pcmcia_socket
*s
= to_socket(dev
);
60 if (!(s
->state
& SOCKET_PRESENT
))
63 return sprintf(buf
, "%d.%dV\n", s
->socket
.Vcc
/ 10,
65 return sprintf(buf
, "X.XV\n");
67 static DEVICE_ATTR(card_voltage
, 0444, pccard_show_voltage
, NULL
);
69 static ssize_t
pccard_show_vpp(struct device
*dev
, struct device_attribute
*attr
,
72 struct pcmcia_socket
*s
= to_socket(dev
);
73 if (!(s
->state
& SOCKET_PRESENT
))
75 return sprintf(buf
, "%d.%dV\n", s
->socket
.Vpp
/ 10, s
->socket
.Vpp
% 10);
77 static DEVICE_ATTR(card_vpp
, 0444, pccard_show_vpp
, NULL
);
79 static ssize_t
pccard_show_vcc(struct device
*dev
, struct device_attribute
*attr
,
82 struct pcmcia_socket
*s
= to_socket(dev
);
83 if (!(s
->state
& SOCKET_PRESENT
))
85 return sprintf(buf
, "%d.%dV\n", s
->socket
.Vcc
/ 10, s
->socket
.Vcc
% 10);
87 static DEVICE_ATTR(card_vcc
, 0444, pccard_show_vcc
, NULL
);
90 static ssize_t
pccard_store_insert(struct device
*dev
, struct device_attribute
*attr
,
91 const char *buf
, size_t count
)
94 struct pcmcia_socket
*s
= to_socket(dev
);
99 ret
= pcmcia_insert_card(s
);
101 return ret
? ret
: count
;
103 static DEVICE_ATTR(card_insert
, 0200, NULL
, pccard_store_insert
);
106 static ssize_t
pccard_show_card_pm_state(struct device
*dev
,
107 struct device_attribute
*attr
,
110 struct pcmcia_socket
*s
= to_socket(dev
);
111 return sprintf(buf
, "%s\n", s
->state
& SOCKET_SUSPEND
? "off" : "on");
114 static ssize_t
pccard_store_card_pm_state(struct device
*dev
,
115 struct device_attribute
*attr
,
116 const char *buf
, size_t count
)
118 ssize_t ret
= -EINVAL
;
119 struct pcmcia_socket
*s
= to_socket(dev
);
124 if (!(s
->state
& SOCKET_SUSPEND
) && !strncmp(buf
, "off", 3))
125 ret
= pcmcia_suspend_card(s
);
126 else if ((s
->state
& SOCKET_SUSPEND
) && !strncmp(buf
, "on", 2))
127 ret
= pcmcia_resume_card(s
);
129 return ret
? -ENODEV
: count
;
131 static DEVICE_ATTR(card_pm_state
, 0644, pccard_show_card_pm_state
, pccard_store_card_pm_state
);
133 static ssize_t
pccard_store_eject(struct device
*dev
,
134 struct device_attribute
*attr
,
135 const char *buf
, size_t count
)
138 struct pcmcia_socket
*s
= to_socket(dev
);
143 ret
= pcmcia_eject_card(s
);
145 return ret
? ret
: count
;
147 static DEVICE_ATTR(card_eject
, 0200, NULL
, pccard_store_eject
);
150 static ssize_t
pccard_show_irq_mask(struct device
*dev
,
151 struct device_attribute
*attr
,
154 struct pcmcia_socket
*s
= to_socket(dev
);
155 return sprintf(buf
, "0x%04x\n", s
->irq_mask
);
158 static ssize_t
pccard_store_irq_mask(struct device
*dev
,
159 struct device_attribute
*attr
,
160 const char *buf
, size_t count
)
163 struct pcmcia_socket
*s
= to_socket(dev
);
169 ret
= sscanf (buf
, "0x%x\n", &mask
);
176 return ret
? ret
: count
;
178 static DEVICE_ATTR(card_irq_mask
, 0600, pccard_show_irq_mask
, pccard_store_irq_mask
);
181 static ssize_t
pccard_show_resource(struct device
*dev
,
182 struct device_attribute
*attr
, char *buf
)
184 struct pcmcia_socket
*s
= to_socket(dev
);
185 return sprintf(buf
, "%s\n", s
->resource_setup_done
? "yes" : "no");
188 static ssize_t
pccard_store_resource(struct device
*dev
,
189 struct device_attribute
*attr
,
190 const char *buf
, size_t count
)
193 struct pcmcia_socket
*s
= to_socket(dev
);
198 spin_lock_irqsave(&s
->lock
, flags
);
199 if (!s
->resource_setup_done
)
200 s
->resource_setup_done
= 1;
201 spin_unlock_irqrestore(&s
->lock
, flags
);
203 mutex_lock(&s
->skt_mutex
);
205 (s
->state
& SOCKET_PRESENT
) &&
206 !(s
->state
& SOCKET_CARDBUS
)) {
207 if (try_module_get(s
->callback
->owner
)) {
208 s
->callback
->requery(s
, 0);
209 module_put(s
->callback
->owner
);
212 mutex_unlock(&s
->skt_mutex
);
216 static DEVICE_ATTR(available_resources_setup_done
, 0600, pccard_show_resource
, pccard_store_resource
);
219 static ssize_t
pccard_extract_cis(struct pcmcia_socket
*s
, char *buf
, loff_t off
, size_t count
)
228 tuplebuffer
= kmalloc(sizeof(u_char
) * 256, GFP_KERNEL
);
232 tempbuffer
= kmalloc(sizeof(u_char
) * 258, GFP_KERNEL
);
238 memset(&tuple
, 0, sizeof(tuple_t
));
240 tuple
.Attributes
= TUPLE_RETURN_LINK
| TUPLE_RETURN_COMMON
;
241 tuple
.DesiredTuple
= RETURN_FIRST_TUPLE
;
242 tuple
.TupleOffset
= 0;
244 status
= pccard_get_first_tuple(s
, BIND_FN_ALL
, &tuple
);
246 tuple
.TupleData
= tuplebuffer
;
247 tuple
.TupleDataMax
= 255;
248 memset(tuplebuffer
, 0, sizeof(u_char
) * 255);
250 status
= pccard_get_tuple_data(s
, &tuple
);
254 if (off
< (pointer
+ 2 + tuple
.TupleDataLen
)) {
255 tempbuffer
[0] = tuple
.TupleCode
& 0xff;
256 tempbuffer
[1] = tuple
.TupleLink
& 0xff;
257 for (i
= 0; i
< tuple
.TupleDataLen
; i
++)
258 tempbuffer
[i
+ 2] = tuplebuffer
[i
] & 0xff;
260 for (i
= 0; i
< (2 + tuple
.TupleDataLen
); i
++) {
261 if (((i
+ pointer
) >= off
) &&
262 (i
+ pointer
) < (off
+ count
)) {
263 buf
[ret
] = tempbuffer
[i
];
269 pointer
+= 2 + tuple
.TupleDataLen
;
271 if (pointer
>= (off
+ count
))
274 if (tuple
.TupleCode
== CISTPL_END
)
276 status
= pccard_get_next_tuple(s
, BIND_FN_ALL
, &tuple
);
286 static ssize_t
pccard_show_cis(struct kobject
*kobj
,
287 struct bin_attribute
*bin_attr
,
288 char *buf
, loff_t off
, size_t count
)
290 unsigned int size
= 0x200;
295 struct pcmcia_socket
*s
;
298 if (off
+ count
> size
)
301 s
= to_socket(container_of(kobj
, struct device
, kobj
));
303 if (!(s
->state
& SOCKET_PRESENT
))
305 if (pccard_validate_cis(s
, BIND_FN_ALL
, &cisinfo
))
310 count
= pccard_extract_cis(s
, buf
, off
, count
);
316 static ssize_t
pccard_store_cis(struct kobject
*kobj
,
317 struct bin_attribute
*bin_attr
,
318 char *buf
, loff_t off
, size_t count
)
320 struct pcmcia_socket
*s
= to_socket(container_of(kobj
, struct device
, kobj
));
330 if (!(s
->state
& SOCKET_PRESENT
))
333 cis
= kzalloc(sizeof(cisdump_t
), GFP_KERNEL
);
337 cis
->Length
= count
+ 1;
338 memcpy(cis
->Data
, buf
, count
);
340 error
= pcmcia_replace_cis(s
, cis
);
345 mutex_lock(&s
->skt_mutex
);
346 if ((s
->callback
) && (s
->state
& SOCKET_PRESENT
) &&
347 !(s
->state
& SOCKET_CARDBUS
)) {
348 if (try_module_get(s
->callback
->owner
)) {
349 s
->callback
->requery(s
, 1);
350 module_put(s
->callback
->owner
);
353 mutex_unlock(&s
->skt_mutex
);
359 static struct attribute
*pccard_socket_attributes
[] = {
360 &dev_attr_card_type
.attr
,
361 &dev_attr_card_voltage
.attr
,
362 &dev_attr_card_vpp
.attr
,
363 &dev_attr_card_vcc
.attr
,
364 &dev_attr_card_insert
.attr
,
365 &dev_attr_card_pm_state
.attr
,
366 &dev_attr_card_eject
.attr
,
367 &dev_attr_card_irq_mask
.attr
,
368 &dev_attr_available_resources_setup_done
.attr
,
372 static const struct attribute_group socket_attrs
= {
373 .attrs
= pccard_socket_attributes
,
376 static struct bin_attribute pccard_cis_attr
= {
377 .attr
= { .name
= "cis", .mode
= S_IRUGO
| S_IWUSR
},
379 .read
= pccard_show_cis
,
380 .write
= pccard_store_cis
,
383 int pccard_sysfs_add_socket(struct device
*dev
)
387 ret
= sysfs_create_group(&dev
->kobj
, &socket_attrs
);
389 ret
= sysfs_create_bin_file(&dev
->kobj
, &pccard_cis_attr
);
391 sysfs_remove_group(&dev
->kobj
, &socket_attrs
);
396 void pccard_sysfs_remove_socket(struct device
*dev
)
398 sysfs_remove_bin_file(&dev
->kobj
, &pccard_cis_attr
);
399 sysfs_remove_group(&dev
->kobj
, &socket_attrs
);