blob: c5d7476da4712fca78e185be6a6dff74a7b948c2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Dominik Brodowski7fe908d2006-01-10 21:20:36 +010027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/system.h>
29#include <asm/irq.h>
30
31#define IN_CARD_SERVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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
43static ssize_t pccard_show_type(struct class_device *dev, char *buf)
44{
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct pcmcia_socket *s = to_socket(dev);
46
47 if (!(s->state & SOCKET_PRESENT))
48 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010049 if (s->state & SOCKET_CARDBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return sprintf(buf, "32-bit\n");
Dominik Brodowski002dbb22005-12-08 23:50:36 +010051 return sprintf(buf, "16-bit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010053static CLASS_DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static ssize_t pccard_show_voltage(struct class_device *dev, char *buf)
56{
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct pcmcia_socket *s = to_socket(dev);
58
59 if (!(s->state & SOCKET_PRESENT))
60 return -ENODEV;
Dominik Brodowski002dbb22005-12-08 23:50:36 +010061 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010066static CLASS_DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static 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}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010075static CLASS_DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static 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}
Dominik Brodowski002dbb22005-12-08 23:50:36 +010084static CLASS_DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86
87static 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}
99static CLASS_DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert);
100
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100101
102static 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
108static 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}
123static CLASS_DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static 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}
137static CLASS_DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
138
139
140static 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
146static 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}
164static CLASS_DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
165
166
167static 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
173static 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);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700182 if (!s->resource_setup_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 s->resource_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 spin_unlock_irqrestore(&s->lock, flags);
185
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100186 mutex_lock(&s->skt_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700187 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);
192 module_put(s->callback->owner);
193 }
194 }
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100195 mutex_unlock(&s->skt_mutex);
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return count;
198}
199static CLASS_DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
200
201
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700202static 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
269static 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
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700297static 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;
301 ssize_t ret = count;
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
Dominik Brodowski8084b372005-12-11 21:18:26 +0100312 cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700313 if (!cis)
314 return -ENOMEM;
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700315
316 cis->Length = count + 1;
317 memcpy(cis->Data, buf, count);
318
319 if (pcmcia_replace_cis(s, cis))
320 ret = -EIO;
321
322 kfree(cis);
323
324 if (!ret) {
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100325 mutex_lock(&s->skt_mutex);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700326 if ((s->callback) && (s->state & SOCKET_PRESENT) &&
327 !(s->state & SOCKET_CARDBUS)) {
328 if (try_module_get(s->callback->owner)) {
Dominik Brodowskie2f0b532005-06-27 16:28:17 -0700329 s->callback->requery(s);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700330 module_put(s->callback->owner);
331 }
332 }
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100333 mutex_unlock(&s->skt_mutex);
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700334 }
335
336
337 return (ret);
338}
339
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static 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,
Dominik Brodowskidb1019c2006-01-10 19:19:37 +0100347 &class_device_attr_card_pm_state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 &class_device_attr_card_eject,
349 &class_device_attr_card_irq_mask,
350 &class_device_attr_available_resources_setup_done,
351 NULL,
352};
353
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700354static struct bin_attribute pccard_cis_attr = {
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700355 .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE},
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700356 .size = 0x200,
357 .read = pccard_show_cis,
Dominik Brodowskiff1fa9e2005-06-27 16:28:09 -0700358 .write = pccard_store_cis,
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700359};
360
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500361static int __devinit pccard_sysfs_add_socket(struct class_device *class_dev,
362 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct class_device_attribute **attr;
365 int ret = 0;
366
367 for (attr = pccard_socket_attributes; *attr; attr++) {
368 ret = class_device_create_file(class_dev, *attr);
369 if (ret)
370 break;
371 }
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700372 if (!ret)
373 ret = sysfs_create_bin_file(&class_dev->kobj, &pccard_cis_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 return ret;
376}
377
Dmitry Torokhovd8539d82005-09-15 02:01:36 -0500378static void __devexit pccard_sysfs_remove_socket(struct class_device *class_dev,
379 struct class_interface *class_intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct class_device_attribute **attr;
382
Dominik Brodowski7f299bc2005-06-27 16:28:08 -0700383 sysfs_remove_bin_file(&class_dev->kobj, &pccard_cis_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 for (attr = pccard_socket_attributes; *attr; attr++)
385 class_device_remove_file(class_dev, *attr);
386}
387
388struct class_interface pccard_sysfs_interface = {
389 .class = &pcmcia_socket_class,
390 .add = &pccard_sysfs_add_socket,
391 .remove = __devexit_p(&pccard_sysfs_remove_socket),
392};