]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * socket_sysfs.c -- most of socket-related sysfs output | |
3 | * | |
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. | |
7 | * | |
8 | * (C) 2003 - 2004 Dominik Brodowski | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/moduleparam.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
1da177e4 LT |
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> | |
7fe908dd | 27 | #include <linux/mutex.h> |
1da177e4 LT |
28 | #include <asm/system.h> |
29 | #include <asm/irq.h> | |
30 | ||
31 | #define IN_CARD_SERVICES | |
1da177e4 LT |
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" | |
40 | ||
41 | #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev) | |
42 | ||
43 | static ssize_t pccard_show_type(struct class_device *dev, char *buf) | |
44 | { | |
1da177e4 LT |
45 | struct pcmcia_socket *s = to_socket(dev); |
46 | ||
47 | if (!(s->state & SOCKET_PRESENT)) | |
48 | return -ENODEV; | |
002dbb2d | 49 | if (s->state & SOCKET_CARDBUS) |
1da177e4 | 50 | return sprintf(buf, "32-bit\n"); |
002dbb2d | 51 | return sprintf(buf, "16-bit\n"); |
1da177e4 | 52 | } |
002dbb2d | 53 | static CLASS_DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL); |
1da177e4 LT |
54 | |
55 | static ssize_t pccard_show_voltage(struct class_device *dev, char *buf) | |
56 | { | |
1da177e4 LT |
57 | struct pcmcia_socket *s = to_socket(dev); |
58 | ||
59 | if (!(s->state & SOCKET_PRESENT)) | |
60 | return -ENODEV; | |
002dbb2d DB |
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"); | |
1da177e4 | 65 | } |
002dbb2d | 66 | static CLASS_DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL); |
1da177e4 LT |
67 | |
68 | static ssize_t pccard_show_vpp(struct class_device *dev, char *buf) | |
69 | { | |
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); | |
74 | } | |
002dbb2d | 75 | static CLASS_DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL); |
1da177e4 LT |
76 | |
77 | static ssize_t pccard_show_vcc(struct class_device *dev, char *buf) | |
78 | { | |
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); | |
83 | } | |
002dbb2d | 84 | static CLASS_DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL); |
1da177e4 LT |
85 | |
86 | ||
87 | static ssize_t pccard_store_insert(struct class_device *dev, const char *buf, size_t count) | |
88 | { | |
89 | ssize_t ret; | |
90 | struct pcmcia_socket *s = to_socket(dev); | |
91 | ||
92 | if (!count) | |
93 | return -EINVAL; | |
94 | ||
95 | ret = pcmcia_insert_card(s); | |
96 | ||
97 | return ret ? ret : count; | |
98 | } | |
99 | static CLASS_DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert); | |
100 | ||
db1019ca DB |
101 | |
102 | static ssize_t pccard_show_card_pm_state(struct class_device *dev, char *buf) | |
103 | { | |
104 | struct pcmcia_socket *s = to_socket(dev); | |
105 | return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on"); | |
106 | } | |
107 | ||
108 | static ssize_t pccard_store_card_pm_state(struct class_device *dev, const char *buf, size_t count) | |
109 | { | |
110 | ssize_t ret = -EINVAL; | |
111 | struct pcmcia_socket *s = to_socket(dev); | |
112 | ||
113 | if (!count) | |
114 | return -EINVAL; | |
115 | ||
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); | |
120 | ||
121 | return ret ? -ENODEV : count; | |
122 | } | |
123 | static CLASS_DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state); | |
124 | ||
1da177e4 LT |
125 | static ssize_t pccard_store_eject(struct class_device *dev, const char *buf, size_t count) |
126 | { | |
127 | ssize_t ret; | |
128 | struct pcmcia_socket *s = to_socket(dev); | |
129 | ||
130 | if (!count) | |
131 | return -EINVAL; | |
132 | ||
133 | ret = pcmcia_eject_card(s); | |
134 | ||
135 | return ret ? ret : count; | |
136 | } | |
137 | static CLASS_DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject); | |
138 | ||
139 | ||
140 | static ssize_t pccard_show_irq_mask(struct class_device *dev, char *buf) | |
141 | { | |
142 | struct pcmcia_socket *s = to_socket(dev); | |
143 | return sprintf(buf, "0x%04x\n", s->irq_mask); | |
144 | } | |
145 | ||
146 | static ssize_t pccard_store_irq_mask(struct class_device *dev, const char *buf, size_t count) | |
147 | { | |
148 | ssize_t ret; | |
149 | struct pcmcia_socket *s = to_socket(dev); | |
150 | u32 mask; | |
151 | ||
152 | if (!count) | |
153 | return -EINVAL; | |
154 | ||
155 | ret = sscanf (buf, "0x%x\n", &mask); | |
156 | ||
157 | if (ret == 1) { | |
158 | s->irq_mask &= mask; | |
159 | ret = 0; | |
160 | } | |
161 | ||
162 | return ret ? ret : count; | |
163 | } | |
164 | static CLASS_DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask); | |
165 | ||
166 | ||
167 | static ssize_t pccard_show_resource(struct class_device *dev, char *buf) | |
168 | { | |
169 | struct pcmcia_socket *s = to_socket(dev); | |
170 | return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no"); | |
171 | } | |
172 | ||
173 | static ssize_t pccard_store_resource(struct class_device *dev, const char *buf, size_t count) | |
174 | { | |
175 | unsigned long flags; | |
176 | struct pcmcia_socket *s = to_socket(dev); | |
177 | ||
178 | if (!count) | |
179 | return -EINVAL; | |
180 | ||
181 | spin_lock_irqsave(&s->lock, flags); | |
e2f0b534 | 182 | if (!s->resource_setup_done) |
1da177e4 | 183 | s->resource_setup_done = 1; |
e2f0b534 | 184 | spin_unlock_irqrestore(&s->lock, flags); |
1da177e4 | 185 | |
7fe908dd | 186 | mutex_lock(&s->skt_mutex); |
e2f0b534 DB |
187 | if ((s->callback) && |
188 | (s->state & SOCKET_PRESENT) && | |
189 | !(s->state & SOCKET_CARDBUS)) { | |
190 | if (try_module_get(s->callback->owner)) { | |
4ae1cbf1 | 191 | s->callback->requery(s, 0); |
e2f0b534 | 192 | module_put(s->callback->owner); |
1da177e4 | 193 | } |
1da177e4 | 194 | } |
7fe908dd | 195 | mutex_unlock(&s->skt_mutex); |
1da177e4 LT |
196 | |
197 | return count; | |
198 | } | |
199 | static CLASS_DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource); | |
200 | ||
201 | ||
7f299bcc DB |
202 | static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, loff_t off, size_t count) |
203 | { | |
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; | |
210 | ||
211 | tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL); | |
212 | if (!tuplebuffer) | |
213 | return -ENOMEM; | |
214 | ||
215 | tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL); | |
216 | if (!tempbuffer) { | |
217 | ret = -ENOMEM; | |
218 | goto free_tuple; | |
219 | } | |
220 | ||
221 | memset(&tuple, 0, sizeof(tuple_t)); | |
222 | ||
223 | tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON; | |
224 | tuple.DesiredTuple = RETURN_FIRST_TUPLE; | |
225 | tuple.TupleOffset = 0; | |
226 | ||
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); | |
232 | ||
233 | status = pccard_get_tuple_data(s, &tuple); | |
234 | if (status) | |
235 | break; | |
236 | ||
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; | |
242 | ||
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++; | |
248 | } | |
249 | } | |
250 | } | |
251 | ||
252 | pointer += 2 + tuple.TupleDataLen; | |
253 | ||
254 | if (pointer >= (off + count)) | |
255 | break; | |
256 | ||
257 | if (tuple.TupleCode == CISTPL_END) | |
258 | break; | |
259 | status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple); | |
260 | } | |
261 | ||
262 | kfree(tempbuffer); | |
263 | free_tuple: | |
264 | kfree(tuplebuffer); | |
265 | ||
266 | return (ret); | |
267 | } | |
268 | ||
269 | static ssize_t pccard_show_cis(struct kobject *kobj, char *buf, loff_t off, size_t count) | |
270 | { | |
271 | unsigned int size = 0x200; | |
272 | ||
273 | if (off >= size) | |
274 | count = 0; | |
275 | else { | |
276 | struct pcmcia_socket *s; | |
277 | cisinfo_t cisinfo; | |
278 | ||
279 | if (off + count > size) | |
280 | count = size - off; | |
281 | ||
282 | s = to_socket(container_of(kobj, struct class_device, kobj)); | |
283 | ||
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; | |
290 | ||
291 | count = pccard_extract_cis(s, buf, off, count); | |
292 | } | |
293 | ||
294 | return (count); | |
295 | } | |
296 | ||
ff1fa9ef DB |
297 | static ssize_t pccard_store_cis(struct kobject *kobj, char *buf, loff_t off, size_t count) |
298 | { | |
299 | struct pcmcia_socket *s = to_socket(container_of(kobj, struct class_device, kobj)); | |
300 | cisdump_t *cis; | |
d834c165 | 301 | int error; |
ff1fa9ef DB |
302 | |
303 | if (off) | |
304 | return -EINVAL; | |
305 | ||
306 | if (count >= 0x200) | |
307 | return -EINVAL; | |
308 | ||
309 | if (!(s->state & SOCKET_PRESENT)) | |
310 | return -ENODEV; | |
311 | ||
8084b372 | 312 | cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL); |
ff1fa9ef DB |
313 | if (!cis) |
314 | return -ENOMEM; | |
ff1fa9ef DB |
315 | |
316 | cis->Length = count + 1; | |
317 | memcpy(cis->Data, buf, count); | |
318 | ||
d834c165 | 319 | error = pcmcia_replace_cis(s, cis); |
ff1fa9ef | 320 | kfree(cis); |
d834c165 LT |
321 | if (error) |
322 | return -EIO; | |
ff1fa9ef | 323 | |
d834c165 LT |
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)) { | |
4ae1cbf1 | 328 | s->callback->requery(s, 1); |
d834c165 | 329 | module_put(s->callback->owner); |
ff1fa9ef | 330 | } |
ff1fa9ef | 331 | } |
d834c165 | 332 | mutex_unlock(&s->skt_mutex); |
ff1fa9ef | 333 | |
d834c165 | 334 | return count; |
ff1fa9ef DB |
335 | } |
336 | ||
7f299bcc | 337 | |
1da177e4 LT |
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, | |
db1019ca | 344 | &class_device_attr_card_pm_state, |
1da177e4 LT |
345 | &class_device_attr_card_eject, |
346 | &class_device_attr_card_irq_mask, | |
347 | &class_device_attr_available_resources_setup_done, | |
348 | NULL, | |
349 | }; | |
350 | ||
7f299bcc | 351 | static struct bin_attribute pccard_cis_attr = { |
ff1fa9ef | 352 | .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE}, |
7f299bcc DB |
353 | .size = 0x200, |
354 | .read = pccard_show_cis, | |
ff1fa9ef | 355 | .write = pccard_store_cis, |
7f299bcc DB |
356 | }; |
357 | ||
d8539d81 DT |
358 | static int __devinit pccard_sysfs_add_socket(struct class_device *class_dev, |
359 | struct class_interface *class_intf) | |
1da177e4 LT |
360 | { |
361 | struct class_device_attribute **attr; | |
362 | int ret = 0; | |
363 | ||
364 | for (attr = pccard_socket_attributes; *attr; attr++) { | |
365 | ret = class_device_create_file(class_dev, *attr); | |
366 | if (ret) | |
367 | break; | |
368 | } | |
7f299bcc DB |
369 | if (!ret) |
370 | ret = sysfs_create_bin_file(&class_dev->kobj, &pccard_cis_attr); | |
1da177e4 LT |
371 | |
372 | return ret; | |
373 | } | |
374 | ||
d8539d81 DT |
375 | static void __devexit pccard_sysfs_remove_socket(struct class_device *class_dev, |
376 | struct class_interface *class_intf) | |
1da177e4 LT |
377 | { |
378 | struct class_device_attribute **attr; | |
379 | ||
7f299bcc | 380 | sysfs_remove_bin_file(&class_dev->kobj, &pccard_cis_attr); |
1da177e4 LT |
381 | for (attr = pccard_socket_attributes; *attr; attr++) |
382 | class_device_remove_file(class_dev, *attr); | |
383 | } | |
384 | ||
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), | |
389 | }; |