blob: 83c6b31133a86962e62745248f7143586c758a3c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
15#include <linux/config.h>
16#include <linux/string.h>
17#include <linux/major.h>
18#include <linux/errno.h>
19#include <linux/slab.h>
20#include <linux/mm.h>
21#include <linux/interrupt.h>
22#include <linux/timer.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
25#include <linux/pm.h>
26#include <linux/pci.h>
27#include <linux/device.h>
Dominik Brodowski7fe908d2006-01-10 21:20:36 +010028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/system.h>
30#include <asm/irq.h>
31
32#define IN_CARD_SERVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <pcmcia/cs_types.h>
34#include <pcmcia/ss.h>
35#include <pcmcia/cs.h>
36#include <pcmcia/bulkmem.h>
37#include <pcmcia/cistpl.h>
38#include <pcmcia/cisreg.h>
39#include <pcmcia/ds.h>
40#include "cs_internal.h"
41
42#define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev)
43
44static ssize_t pccard_show_type(struct class_device *dev, char *buf)
45{
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct pcmcia_socket *s = to_socket(dev);
47
48 if (!(s->state & SOCKET_PRESENT))
49 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010050 if (s->state & SOCKET_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return sprintf(buf, "32-bit\n");
Dominik Brodowski002dbb22005-12-08 23:50:36 +010052 return sprintf(buf, "16-bit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010054static CLASS_DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static ssize_t pccard_show_voltage(struct class_device *dev, char *buf)
57{
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct pcmcia_socket *s = to_socket(dev);
59
60 if (!(s->state & SOCKET_PRESENT))
61 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010062 if (s->socket.Vcc)
63 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10,
64 s->socket.Vcc % 10);
65 return sprintf(buf, "X.XV\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010067static CLASS_DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static ssize_t pccard_show_vpp(struct class_device *dev, char *buf)
70{
71 struct pcmcia_socket *s = to_socket(dev);
72 if (!(s->state & SOCKET_PRESENT))
73 return -ENODEV;
74 return sprintf(buf, "%d.%dV\n", s->socket.Vpp / 10, s->socket.Vpp % 10);
75}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010076static CLASS_DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static ssize_t pccard_show_vcc(struct class_device *dev, char *buf)
79{
80 struct pcmcia_socket *s = to_socket(dev);
81 if (!(s->state & SOCKET_PRESENT))
82 return -ENODEV;
83 return sprintf(buf, "%d.%dV\n", s->socket.Vcc / 10, s->socket.Vcc % 10);
84}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010085static CLASS_DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
88static ssize_t pccard_store_insert(struct class_device *dev, const char *buf, size_t count)
89{
90 ssize_t ret;
91 struct pcmcia_socket *s = to_socket(dev);
92
93 if (!count)
94 return -EINVAL;
95
96 ret = pcmcia_insert_card(s);
97
98 return ret ? ret : count;
99}
100static CLASS_DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
101
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100102
103static ssize_t pccard_show_card_pm_state(struct class_device *dev, char *buf)
104{
105 struct pcmcia_socket *s = to_socket(dev);
106 return sprintf(buf, "%s\n", s->state & SOCKET_SUSPEND ? "off" : "on");
107}
108
109static ssize_t pccard_store_card_pm_state(struct class_device *dev, const char *buf, size_t count)
110{
111 ssize_t ret = -EINVAL;
112 struct pcmcia_socket *s = to_socket(dev);
113
114 if (!count)
115 return -EINVAL;
116
117 if (!(s->state & SOCKET_SUSPEND) && !strncmp(buf, "off", 3))
118 ret = pcmcia_suspend_card(s);
119 else if ((s->state & SOCKET_SUSPEND) && !strncmp(buf, "on", 2))
120 ret = pcmcia_resume_card(s);
121
122 return ret ? -ENODEV : count;
123}
124static CLASS_DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static ssize_t pccard_store_eject(struct class_device *dev, const char *buf, size_t count)
127{
128 ssize_t ret;
129 struct pcmcia_socket *s = to_socket(dev);
130
131 if (!count)
132 return -EINVAL;
133
134 ret = pcmcia_eject_card(s);
135
136 return ret ? ret : count;
137}
138static CLASS_DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
139
140
141static ssize_t pccard_show_irq_mask(struct class_device *dev, char *buf)
142{
143 struct pcmcia_socket *s = to_socket(dev);
144 return sprintf(buf, "0x%04x\n", s->irq_mask);
145}
146
147static ssize_t pccard_store_irq_mask(struct class_device *dev, const char *buf, size_t count)
148{
149 ssize_t ret;
150 struct pcmcia_socket *s = to_socket(dev);
151 u32 mask;
152
153 if (!count)
154 return -EINVAL;
155
156 ret = sscanf (buf, "0x%x\n", &mask);
157
158 if (ret == 1) {
159 s->irq_mask &= mask;
160 ret = 0;
161 }
162
163 return ret ? ret : count;
164}
165static CLASS_DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
166
167
168static ssize_t pccard_show_resource(struct class_device *dev, char *buf)
169{
170 struct pcmcia_socket *s = to_socket(dev);
171 return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
172}
173
174static ssize_t pccard_store_resource(struct class_device *dev, const char *buf, size_t count)
175{
176 unsigned long flags;
177 struct pcmcia_socket *s = to_socket(dev);
178
179 if (!count)
180 return -EINVAL;
181
182 spin_lock_irqsave(&s->lock, flags);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700183 if (!s->resource_setup_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 s->resource_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_unlock_irqrestore(&s->lock, flags);
186
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100187 mutex_lock(&s->skt_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700188 if ((s->callback) &&
189 (s->state & SOCKET_PRESENT) &&
190 !(s->state & SOCKET_CARDBUS)) {
191 if (try_module_get(s->callback->owner)) {
192 s->callback->requery(s);
193 module_put(s->callback->owner);
194 }
195 }
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100196 mutex_unlock(&s->skt_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return count;
199}
200static CLASS_DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
201
202
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700203static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, loff_t off, size_t count)
204{
205 tuple_t tuple;
206 int status, i;
207 loff_t pointer = 0;
208 ssize_t ret = 0;
209 u_char *tuplebuffer;
210 u_char *tempbuffer;
211
212 tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL);
213 if (!tuplebuffer)
214 return -ENOMEM;
215
216 tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL);
217 if (!tempbuffer) {
218 ret = -ENOMEM;
219 goto free_tuple;
220 }
221
222 memset(&tuple, 0, sizeof(tuple_t));
223
224 tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON;
225 tuple.DesiredTuple = RETURN_FIRST_TUPLE;
226 tuple.TupleOffset = 0;
227
228 status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple);
229 while (!status) {
230 tuple.TupleData = tuplebuffer;
231 tuple.TupleDataMax = 255;
232 memset(tuplebuffer, 0, sizeof(u_char) * 255);
233
234 status = pccard_get_tuple_data(s, &tuple);
235 if (status)
236 break;
237
238 if (off < (pointer + 2 + tuple.TupleDataLen)) {
239 tempbuffer[0] = tuple.TupleCode & 0xff;
240 tempbuffer[1] = tuple.TupleLink & 0xff;
241 for (i = 0; i < tuple.TupleDataLen; i++)
242 tempbuffer[i + 2] = tuplebuffer[i] & 0xff;
243
244 for (i = 0; i < (2 + tuple.TupleDataLen); i++) {
245 if (((i + pointer) >= off) &&
246 (i + pointer) < (off + count)) {
247 buf[ret] = tempbuffer[i];
248 ret++;
249 }
250 }
251 }
252
253 pointer += 2 + tuple.TupleDataLen;
254
255 if (pointer >= (off + count))
256 break;
257
258 if (tuple.TupleCode == CISTPL_END)
259 break;
260 status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple);
261 }
262
263 kfree(tempbuffer);
264 free_tuple:
265 kfree(tuplebuffer);
266
267 return (ret);
268}
269
270static ssize_t pccard_show_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
271{
272 unsigned int size = 0x200;
273
274 if (off >= size)
275 count = 0;
276 else {
277 struct pcmcia_socket *s;
278 cisinfo_t cisinfo;
279
280 if (off + count > size)
281 count = size - off;
282
283 s = to_socket(container_of(kobj, struct class_device, kobj));
284
285 if (!(s->state & SOCKET_PRESENT))
286 return -ENODEV;
287 if (pccard_validate_cis(s, BIND_FN_ALL, &cisinfo))
288 return -EIO;
289 if (!cisinfo.Chains)
290 return -ENODATA;
291
292 count = pccard_extract_cis(s, buf, off, count);
293 }
294
295 return (count);
296}
297
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700298static ssize_t pccard_store_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
299{
300 struct pcmcia_socket *s = to_socket(container_of(kobj, struct class_device, kobj));
301 cisdump_t *cis;
302 ssize_t ret = count;
303
304 if (off)
305 return -EINVAL;
306
307 if (count >= 0x200)
308 return -EINVAL;
309
310 if (!(s->state & SOCKET_PRESENT))
311 return -ENODEV;
312
Dominik Brodowski8084b372005-12-11 21:18:26 +0100313 cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700314 if (!cis)
315 return -ENOMEM;
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700316
317 cis->Length = count + 1;
318 memcpy(cis->Data, buf, count);
319
320 if (pcmcia_replace_cis(s, cis))
321 ret = -EIO;
322
323 kfree(cis);
324
325 if (!ret) {
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100326 mutex_lock(&s->skt_mutex);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700327 if ((s->callback) && (s->state & SOCKET_PRESENT) &&
328 !(s->state & SOCKET_CARDBUS)) {
329 if (try_module_get(s->callback->owner)) {
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700330 s->callback->requery(s);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700331 module_put(s->callback->owner);
332 }
333 }
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100334 mutex_unlock(&s->skt_mutex);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700335 }
336
337
338 return (ret);
339}
340
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static struct class_device_attribute *pccard_socket_attributes[] = {
343 &class_device_attr_card_type,
344 &class_device_attr_card_voltage,
345 &class_device_attr_card_vpp,
346 &class_device_attr_card_vcc,
347 &class_device_attr_card_insert,
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100348 &class_device_attr_card_pm_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 &class_device_attr_card_eject,
350 &class_device_attr_card_irq_mask,
351 &class_device_attr_available_resources_setup_done,
352 NULL,
353};
354
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700355static struct bin_attribute pccard_cis_attr = {
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700356 .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE},
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700357 .size = 0x200,
358 .read = pccard_show_cis,
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700359 .write = pccard_store_cis,
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700360};
361
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500362static int __devinit pccard_sysfs_add_socket(struct class_device *class_dev,
363 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct class_device_attribute **attr;
366 int ret = 0;
367
368 for (attr = pccard_socket_attributes; *attr; attr++) {
369 ret = class_device_create_file(class_dev, *attr);
370 if (ret)
371 break;
372 }
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700373 if (!ret)
374 ret = sysfs_create_bin_file(&class_dev->kobj, &pccard_cis_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 return ret;
377}
378
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500379static void __devexit pccard_sysfs_remove_socket(struct class_device *class_dev,
380 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct class_device_attribute **attr;
383
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700384 sysfs_remove_bin_file(&class_dev->kobj, &pccard_cis_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 for (attr = pccard_socket_attributes; *attr; attr++)
386 class_device_remove_file(class_dev, *attr);
387}
388
389struct class_interface pccard_sysfs_interface = {
390 .class = &pcmcia_socket_class,
391 .add = &pccard_sysfs_add_socket,
392 .remove = __devexit_p(&pccard_sysfs_remove_socket),
393};