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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/system.h> |
| 29 | #include <asm/irq.h> |
| 30 | |
| 31 | #define IN_CARD_SERVICES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct pcmcia_socket *s = to_socket(dev); |
| 46 | |
| 47 | if (!(s->state & SOCKET_PRESENT)) |
| 48 | return -ENODEV; |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 49 | if (s->state & SOCKET_CARDBUS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return sprintf(buf, "32-bit\n"); |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 51 | return sprintf(buf, "16-bit\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 53 | static CLASS_DEVICE_ATTR(card_type, 0444, pccard_show_type, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | static ssize_t pccard_show_voltage(struct class_device *dev, char *buf) |
| 56 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | struct pcmcia_socket *s = to_socket(dev); |
| 58 | |
| 59 | if (!(s->state & SOCKET_PRESENT)) |
| 60 | return -ENODEV; |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 66 | static CLASS_DEVICE_ATTR(card_voltage, 0444, pccard_show_voltage, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 75 | static CLASS_DEVICE_ATTR(card_vpp, 0444, pccard_show_vpp, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dominik Brodowski | 002dbb2 | 2005-12-08 23:50:36 +0100 | [diff] [blame] | 84 | static CLASS_DEVICE_ATTR(card_vcc, 0444, pccard_show_vcc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dominik Brodowski | db1019c | 2006-01-10 19:19:37 +0100 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 182 | if (!s->resource_setup_done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | s->resource_setup_done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | spin_unlock_irqrestore(&s->lock, flags); |
| 185 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 186 | mutex_lock(&s->skt_mutex); |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 187 | 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 Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 195 | mutex_unlock(&s->skt_mutex); |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return count; |
| 198 | } |
| 199 | static CLASS_DEVICE_ATTR(available_resources_setup_done, 0600, pccard_show_resource, pccard_store_resource); |
| 200 | |
| 201 | |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 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 | |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 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; |
| 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 Brodowski | 8084b37 | 2005-12-11 21:18:26 +0100 | [diff] [blame] | 312 | cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 313 | if (!cis) |
| 314 | return -ENOMEM; |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 315 | |
| 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 Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 325 | mutex_lock(&s->skt_mutex); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 326 | if ((s->callback) && (s->state & SOCKET_PRESENT) && |
| 327 | !(s->state & SOCKET_CARDBUS)) { |
| 328 | if (try_module_get(s->callback->owner)) { |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 329 | s->callback->requery(s); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 330 | module_put(s->callback->owner); |
| 331 | } |
| 332 | } |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 333 | mutex_unlock(&s->skt_mutex); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | return (ret); |
| 338 | } |
| 339 | |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | static 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 Brodowski | db1019c | 2006-01-10 19:19:37 +0100 | [diff] [blame] | 347 | &class_device_attr_card_pm_state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | &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 Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 354 | static struct bin_attribute pccard_cis_attr = { |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 355 | .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR, .owner = THIS_MODULE}, |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 356 | .size = 0x200, |
| 357 | .read = pccard_show_cis, |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 358 | .write = pccard_store_cis, |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 361 | static int __devinit pccard_sysfs_add_socket(struct class_device *class_dev, |
| 362 | struct class_interface *class_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 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 Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 372 | if (!ret) |
| 373 | ret = sysfs_create_bin_file(&class_dev->kobj, &pccard_cis_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
Dmitry Torokhov | d8539d8 | 2005-09-15 02:01:36 -0500 | [diff] [blame] | 378 | static void __devexit pccard_sysfs_remove_socket(struct class_device *class_dev, |
| 379 | struct class_interface *class_intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
| 381 | struct class_device_attribute **attr; |
| 382 | |
Dominik Brodowski | 7f299bc | 2005-06-27 16:28:08 -0700 | [diff] [blame] | 383 | sysfs_remove_bin_file(&class_dev->kobj, &pccard_cis_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | for (attr = pccard_socket_attributes; *attr; attr++) |
| 385 | class_device_remove_file(class_dev, *attr); |
| 386 | } |
| 387 | |
| 388 | struct 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 | }; |