Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame^] | 28 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/system.h> |
| 30 | #include <asm/irq.h> |
| 31 | |
| 32 | #define IN_CARD_SERVICES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #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 | |
| 44 | static ssize_t pccard_show_type(struct class_device *dev, char *buf) |
| 45 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | struct pcmcia_socket *s = to_socket(dev); |
| 47 | |
| 48 | if (!(s->state & SOCKET_PRESENT)) |
| 49 | return -ENODEV; |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 50 | if (s->state & SOCKET_CARDBUS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | return sprintf(buf, "32-bit\n"); |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 52 | return sprintf(buf, "16-bit\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 54 | static CLASS_DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | static ssize_t pccard_show_voltage(struct class_device *dev, char *buf) |
| 57 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | struct pcmcia_socket *s = to_socket(dev); |
| 59 | |
| 60 | if (!(s->state & SOCKET_PRESENT)) |
| 61 | return -ENODEV; |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 62 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 67 | static CLASS_DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | static 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 Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 76 | static CLASS_DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | static 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 Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 85 | static CLASS_DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | |
| 88 | static 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 | } |
| 100 | static CLASS_DEVICE_ATTR(card_insert, 0200, NULL, pccard_store_insert); |
| 101 | |
Dominik Brodowski | db1019c | 2006-01-10 19:19:37 +0100 | [diff] [blame] | 102 | |
| 103 | static 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 | |
| 109 | static 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 | } |
| 124 | static CLASS_DEVICE_ATTR(card_pm_state, 0644, pccard_show_card_pm_state, pccard_store_card_pm_state); |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | static 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 | } |
| 138 | static CLASS_DEVICE_ATTR(card_eject, 0200, NULL, pccard_store_eject); |
| 139 | |
| 140 | |
| 141 | static 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 | |
| 147 | static 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 | } |
| 165 | static CLASS_DEVICE_ATTR(card_irq_mask, 0600, pccard_show_irq_mask, pccard_store_irq_mask); |
| 166 | |
| 167 | |
| 168 | static 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 | |
| 174 | static 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 Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 183 | if (!s->resource_setup_done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | s->resource_setup_done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | spin_unlock_irqrestore(&s->lock, flags); |
| 186 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame^] | 187 | mutex_lock(&s->skt_mutex); |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 188 | 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 Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame^] | 196 | mutex_unlock(&s->skt_mutex); |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return count; |
| 199 | } |
| 200 | static CLASS_DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource); |
| 201 | |
| 202 | |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 203 | static 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 | |
| 270 | static 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 Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 298 | static 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 Brodowski | 8084b37 | 2005-12-11 21:18:26 +0100 | [diff] [blame] | 313 | cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 314 | if (!cis) |
| 315 | return -ENOMEM; |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 316 | |
| 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 Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame^] | 326 | mutex_lock(&s->skt_mutex); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 327 | if ((s->callback) && (s->state & SOCKET_PRESENT) && |
| 328 | !(s->state & SOCKET_CARDBUS)) { |
| 329 | if (try_module_get(s->callback->owner)) { |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 330 | s->callback->requery(s); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 331 | module_put(s->callback->owner); |
| 332 | } |
| 333 | } |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame^] | 334 | mutex_unlock(&s->skt_mutex); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
| 338 | return (ret); |
| 339 | } |
| 340 | |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | static 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 Brodowski | db1019c | 2006-01-10 19:19:37 +0100 | [diff] [blame] | 348 | &class_device_attr_card_pm_state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | &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 Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 355 | static struct bin_attribute pccard_cis_attr = { |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 356 | .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE}, |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 357 | .size = 0x200, |
| 358 | .read = pccard_show_cis, |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 359 | .write = pccard_store_cis, |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 360 | }; |
| 361 | |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 362 | static int __devinit pccard_sysfs_add_socket(struct class_device *class_dev, |
| 363 | struct class_interface *class_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 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 Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 373 | if (!ret) |
| 374 | ret = sysfs_create_bin_file(&class_dev->kobj, &pccard_cis_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | return ret; |
| 377 | } |
| 378 | |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 379 | static void __devexit pccard_sysfs_remove_socket(struct class_device *class_dev, |
| 380 | struct class_interface *class_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | struct class_device_attribute **attr; |
| 383 | |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 384 | sysfs_remove_bin_file(&class_dev->kobj, &pccard_cis_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | for (attr = pccard_socket_attributes; *attr; attr++) |
| 386 | class_device_remove_file(class_dev, *attr); |
| 387 | } |
| 388 | |
| 389 | struct 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 | }; |