[PATCH] pcmcia: kzalloc conversion
[linux-3.10.git] / drivers / pcmcia / socket_sysfs.c
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>
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>
28 #include <asm/system.h>
29 #include <asm/irq.h>
30
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"
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 {
45         struct pcmcia_socket *s = to_socket(dev);
46
47         if (!(s->state & SOCKET_PRESENT))
48                 return -ENODEV;
49         if (s->state & SOCKET_CARDBUS)
50                 return sprintf(buf, "32-bit\n");
51         return sprintf(buf, "16-bit\n");
52 }
53 static CLASS_DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL);
54
55 static ssize_t pccard_show_voltage(struct class_device *dev, char *buf)
56 {
57         struct pcmcia_socket *s = to_socket(dev);
58
59         if (!(s->state & SOCKET_PRESENT))
60                 return -ENODEV;
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");
65 }
66 static CLASS_DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL);
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 }
75 static CLASS_DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL);
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 }
84 static CLASS_DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL);
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
101 static ssize_t pccard_store_eject(struct class_device *dev, const char *buf, size_t count)
102 {
103         ssize_t ret;
104         struct pcmcia_socket *s = to_socket(dev);
105
106         if (!count)
107                 return -EINVAL;
108
109         ret = pcmcia_eject_card(s);
110
111         return ret ? ret : count;
112 }
113 static CLASS_DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject);
114
115
116 static ssize_t pccard_show_irq_mask(struct class_device *dev, char *buf)
117 {
118         struct pcmcia_socket *s = to_socket(dev);
119         return sprintf(buf, "0x%04x\n", s->irq_mask);
120 }
121
122 static ssize_t pccard_store_irq_mask(struct class_device *dev, const char *buf, size_t count)
123 {
124         ssize_t ret;
125         struct pcmcia_socket *s = to_socket(dev);
126         u32 mask;
127
128         if (!count)
129                 return -EINVAL;
130
131         ret = sscanf (buf, "0x%x\n", &mask);
132
133         if (ret == 1) {
134                 s->irq_mask &= mask;
135                 ret = 0;
136         }
137
138         return ret ? ret : count;
139 }
140 static CLASS_DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask);
141
142
143 static ssize_t pccard_show_resource(struct class_device *dev, char *buf)
144 {
145         struct pcmcia_socket *s = to_socket(dev);
146         return sprintf(buf, "%s\n", s->resource_setup_done ? "yes" : "no");
147 }
148
149 static ssize_t pccard_store_resource(struct class_device *dev, const char *buf, size_t count)
150 {
151         unsigned long flags;
152         struct pcmcia_socket *s = to_socket(dev);
153
154         if (!count)
155                 return -EINVAL;
156
157         spin_lock_irqsave(&s->lock, flags);
158         if (!s->resource_setup_done)
159                 s->resource_setup_done = 1;
160         spin_unlock_irqrestore(&s->lock, flags);
161
162         down(&s->skt_sem);
163         if ((s->callback) &&
164             (s->state & SOCKET_PRESENT) &&
165             !(s->state & SOCKET_CARDBUS)) {
166                 if (try_module_get(s->callback->owner)) {
167                         s->callback->requery(s);
168                         module_put(s->callback->owner);
169                 }
170         }
171         up(&s->skt_sem);
172
173         return count;
174 }
175 static CLASS_DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource);
176
177
178 static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, loff_t off, size_t count)
179 {
180         tuple_t tuple;
181         int status, i;
182         loff_t pointer = 0;
183         ssize_t ret = 0;
184         u_char *tuplebuffer;
185         u_char *tempbuffer;
186
187         tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL);
188         if (!tuplebuffer)
189                 return -ENOMEM;
190
191         tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL);
192         if (!tempbuffer) {
193                 ret = -ENOMEM;
194                 goto free_tuple;
195         }
196
197         memset(&tuple, 0, sizeof(tuple_t));
198
199         tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON;
200         tuple.DesiredTuple = RETURN_FIRST_TUPLE;
201         tuple.TupleOffset = 0;
202
203         status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple);
204         while (!status) {
205                 tuple.TupleData = tuplebuffer;
206                 tuple.TupleDataMax = 255;
207                 memset(tuplebuffer, 0, sizeof(u_char) * 255);
208
209                 status = pccard_get_tuple_data(s, &tuple);
210                 if (status)
211                         break;
212
213                 if (off < (pointer + 2 + tuple.TupleDataLen)) {
214                         tempbuffer[0] = tuple.TupleCode & 0xff;
215                         tempbuffer[1] = tuple.TupleLink & 0xff;
216                         for (i = 0; i < tuple.TupleDataLen; i++)
217                                 tempbuffer[i + 2] = tuplebuffer[i] & 0xff;
218
219                         for (i = 0; i < (2 + tuple.TupleDataLen); i++) {
220                                 if (((i + pointer) >= off) &&
221                                     (i + pointer) < (off + count)) {
222                                         buf[ret] = tempbuffer[i];
223                                         ret++;
224                                 }
225                         }
226                 }
227
228                 pointer += 2 + tuple.TupleDataLen;
229
230                 if (pointer >= (off + count))
231                         break;
232
233                 if (tuple.TupleCode == CISTPL_END)
234                         break;
235                 status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple);
236         }
237
238         kfree(tempbuffer);
239  free_tuple:
240         kfree(tuplebuffer);
241
242         return (ret);
243 }
244
245 static ssize_t pccard_show_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
246 {
247         unsigned int size = 0x200;
248
249         if (off >= size)
250                 count = 0;
251         else {
252                 struct pcmcia_socket *s;
253                 cisinfo_t cisinfo;
254
255                 if (off + count > size)
256                         count = size - off;
257
258                 s = to_socket(container_of(kobj, struct class_device, kobj));
259
260                 if (!(s->state & SOCKET_PRESENT))
261                         return -ENODEV;
262                 if (pccard_validate_cis(s, BIND_FN_ALL, &cisinfo))
263                         return -EIO;
264                 if (!cisinfo.Chains)
265                         return -ENODATA;
266
267                 count = pccard_extract_cis(s, buf, off, count);
268         }
269
270         return (count);
271 }
272
273 static ssize_t pccard_store_cis(struct kobject *kobj, char *buf, loff_t off, size_t count)
274 {
275         struct pcmcia_socket *s = to_socket(container_of(kobj, struct class_device, kobj));
276         cisdump_t *cis;
277         ssize_t ret = count;
278
279         if (off)
280                 return -EINVAL;
281
282         if (count >= 0x200)
283                 return -EINVAL;
284
285         if (!(s->state & SOCKET_PRESENT))
286                 return -ENODEV;
287
288         cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
289         if (!cis)
290                 return -ENOMEM;
291
292         cis->Length = count + 1;
293         memcpy(cis->Data, buf, count);
294
295         if (pcmcia_replace_cis(s, cis))
296                 ret  = -EIO;
297
298         kfree(cis);
299
300         if (!ret) {
301                 down(&s->skt_sem);
302                 if ((s->callback) && (s->state & SOCKET_PRESENT) &&
303                     !(s->state & SOCKET_CARDBUS)) {
304                         if (try_module_get(s->callback->owner)) {
305                                 s->callback->requery(s);
306                                 module_put(s->callback->owner);
307                         }
308                 }
309                 up(&s->skt_sem);
310         }
311
312
313         return (ret);
314 }
315
316
317 static struct class_device_attribute *pccard_socket_attributes[] = {
318         &class_device_attr_card_type,
319         &class_device_attr_card_voltage,
320         &class_device_attr_card_vpp,
321         &class_device_attr_card_vcc,
322         &class_device_attr_card_insert,
323         &class_device_attr_card_eject,
324         &class_device_attr_card_irq_mask,
325         &class_device_attr_available_resources_setup_done,
326         NULL,
327 };
328
329 static struct bin_attribute pccard_cis_attr = {
330         .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE},
331         .size = 0x200,
332         .read = pccard_show_cis,
333         .write = pccard_store_cis,
334 };
335
336 static int __devinit pccard_sysfs_add_socket(struct class_device *class_dev,
337                                              struct class_interface *class_intf)
338 {
339         struct class_device_attribute **attr;
340         int ret = 0;
341
342         for (attr = pccard_socket_attributes; *attr; attr++) {
343                 ret = class_device_create_file(class_dev, *attr);
344                 if (ret)
345                         break;
346         }
347         if (!ret)
348                 ret = sysfs_create_bin_file(&class_dev->kobj, &pccard_cis_attr);
349
350         return ret;
351 }
352
353 static void __devexit pccard_sysfs_remove_socket(struct class_device *class_dev,
354                                                  struct class_interface *class_intf)
355 {
356         struct class_device_attribute **attr;
357
358         sysfs_remove_bin_file(&class_dev->kobj, &pccard_cis_attr);
359         for (attr = pccard_socket_attributes; *attr; attr++)
360                 class_device_remove_file(class_dev, *attr);
361 }
362
363 struct class_interface pccard_sysfs_interface = {
364         .class = &pcmcia_socket_class,
365         .add = &pccard_sysfs_add_socket,
366         .remove = __devexit_p(&pccard_sysfs_remove_socket),
367 };