Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * PCI HotPlug Controller Core | |
3 | * | |
4 | * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com) | |
5 | * Copyright (C) 2001-2002 IBM Corp. | |
6 | * | |
7 | * All rights reserved. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or (at | |
12 | * your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
17 | * NON INFRINGEMENT. See the GNU General Public License for more | |
18 | * details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | * | |
fb5f4d7a | 24 | * Send feedback to <kristen.c.accardi@intel.com> |
1da177e4 LT |
25 | * |
26 | */ | |
27 | ||
1da177e4 LT |
28 | #include <linux/module.h> |
29 | #include <linux/moduleparam.h> | |
30 | #include <linux/kernel.h> | |
31 | #include <linux/types.h> | |
32 | #include <linux/list.h> | |
7a54f25c GKH |
33 | #include <linux/kobject.h> |
34 | #include <linux/sysfs.h> | |
1da177e4 LT |
35 | #include <linux/pagemap.h> |
36 | #include <linux/slab.h> | |
1da177e4 LT |
37 | #include <linux/init.h> |
38 | #include <linux/mount.h> | |
39 | #include <linux/namei.h> | |
40 | #include <linux/pci.h> | |
7a54f25c | 41 | #include <linux/pci_hotplug.h> |
1da177e4 | 42 | #include <asm/uaccess.h> |
f46753c5 | 43 | #include "../pci.h" |
1da177e4 LT |
44 | |
45 | #define MY_NAME "pci_hotplug" | |
46 | ||
66bef8c0 | 47 | #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0) |
1da177e4 LT |
48 | #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg) |
49 | #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg) | |
50 | #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg) | |
51 | ||
52 | ||
53 | /* local variables */ | |
54 | static int debug; | |
55 | ||
56 | #define DRIVER_VERSION "0.5" | |
57 | #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>" | |
58 | #define DRIVER_DESC "PCI Hot Plug PCI Core" | |
59 | ||
60 | ||
61 | ////////////////////////////////////////////////////////////////// | |
62 | ||
63 | static LIST_HEAD(pci_hotplug_slot_list); | |
f46753c5 | 64 | static DEFINE_SPINLOCK(pci_hotplug_slot_list_lock); |
1da177e4 | 65 | |
1da177e4 LT |
66 | /* these strings match up with the values in pci_bus_speed */ |
67 | static char *pci_bus_speed_strings[] = { | |
68 | "33 MHz PCI", /* 0x00 */ | |
69 | "66 MHz PCI", /* 0x01 */ | |
70 | "66 MHz PCIX", /* 0x02 */ | |
71 | "100 MHz PCIX", /* 0x03 */ | |
72 | "133 MHz PCIX", /* 0x04 */ | |
73 | NULL, /* 0x05 */ | |
74 | NULL, /* 0x06 */ | |
75 | NULL, /* 0x07 */ | |
76 | NULL, /* 0x08 */ | |
77 | "66 MHz PCIX 266", /* 0x09 */ | |
78 | "100 MHz PCIX 266", /* 0x0a */ | |
79 | "133 MHz PCIX 266", /* 0x0b */ | |
80 | NULL, /* 0x0c */ | |
81 | NULL, /* 0x0d */ | |
82 | NULL, /* 0x0e */ | |
83 | NULL, /* 0x0f */ | |
84 | NULL, /* 0x10 */ | |
85 | "66 MHz PCIX 533", /* 0x11 */ | |
86 | "100 MHz PCIX 533", /* 0x12 */ | |
87 | "133 MHz PCIX 533", /* 0x13 */ | |
88 | "25 GBps PCI-E", /* 0x14 */ | |
89 | }; | |
90 | ||
91 | #ifdef CONFIG_HOTPLUG_PCI_CPCI | |
92 | extern int cpci_hotplug_init(int debug); | |
93 | extern void cpci_hotplug_exit(void); | |
94 | #else | |
95 | static inline int cpci_hotplug_init(int debug) { return 0; } | |
96 | static inline void cpci_hotplug_exit(void) { } | |
97 | #endif | |
98 | ||
99 | /* Weee, fun with macros... */ | |
100 | #define GET_STATUS(name,type) \ | |
101 | static int get_##name (struct hotplug_slot *slot, type *value) \ | |
102 | { \ | |
103 | struct hotplug_slot_ops *ops = slot->ops; \ | |
104 | int retval = 0; \ | |
105 | if (try_module_get(ops->owner)) { \ | |
106 | if (ops->get_##name) \ | |
41ece382 | 107 | retval = ops->get_##name(slot, value); \ |
1da177e4 LT |
108 | else \ |
109 | *value = slot->info->name; \ | |
110 | module_put(ops->owner); \ | |
111 | } \ | |
112 | return retval; \ | |
113 | } | |
114 | ||
115 | GET_STATUS(power_status, u8) | |
116 | GET_STATUS(attention_status, u8) | |
117 | GET_STATUS(latch_status, u8) | |
118 | GET_STATUS(adapter_status, u8) | |
1da177e4 LT |
119 | GET_STATUS(max_bus_speed, enum pci_bus_speed) |
120 | GET_STATUS(cur_bus_speed, enum pci_bus_speed) | |
121 | ||
f46753c5 | 122 | static ssize_t power_read_file(struct pci_slot *slot, char *buf) |
1da177e4 LT |
123 | { |
124 | int retval; | |
125 | u8 value; | |
126 | ||
f46753c5 | 127 | retval = get_power_status(slot->hotplug, &value); |
1da177e4 LT |
128 | if (retval) |
129 | goto exit; | |
130 | retval = sprintf (buf, "%d\n", value); | |
131 | exit: | |
132 | return retval; | |
133 | } | |
134 | ||
f46753c5 | 135 | static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf, |
1da177e4 LT |
136 | size_t count) |
137 | { | |
f46753c5 | 138 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
139 | unsigned long lpower; |
140 | u8 power; | |
141 | int retval = 0; | |
142 | ||
143 | lpower = simple_strtoul (buf, NULL, 10); | |
144 | power = (u8)(lpower & 0xff); | |
145 | dbg ("power = %d\n", power); | |
146 | ||
147 | if (!try_module_get(slot->ops->owner)) { | |
148 | retval = -ENODEV; | |
149 | goto exit; | |
150 | } | |
151 | switch (power) { | |
152 | case 0: | |
153 | if (slot->ops->disable_slot) | |
154 | retval = slot->ops->disable_slot(slot); | |
155 | break; | |
156 | ||
157 | case 1: | |
158 | if (slot->ops->enable_slot) | |
159 | retval = slot->ops->enable_slot(slot); | |
160 | break; | |
161 | ||
162 | default: | |
163 | err ("Illegal value specified for power\n"); | |
164 | retval = -EINVAL; | |
165 | } | |
166 | module_put(slot->ops->owner); | |
167 | ||
168 | exit: | |
169 | if (retval) | |
170 | return retval; | |
171 | return count; | |
172 | } | |
173 | ||
f46753c5 | 174 | static struct pci_slot_attribute hotplug_slot_attr_power = { |
1da177e4 LT |
175 | .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR}, |
176 | .show = power_read_file, | |
177 | .store = power_write_file | |
178 | }; | |
179 | ||
f46753c5 | 180 | static ssize_t attention_read_file(struct pci_slot *slot, char *buf) |
1da177e4 LT |
181 | { |
182 | int retval; | |
183 | u8 value; | |
184 | ||
f46753c5 | 185 | retval = get_attention_status(slot->hotplug, &value); |
1da177e4 LT |
186 | if (retval) |
187 | goto exit; | |
f46753c5 | 188 | retval = sprintf(buf, "%d\n", value); |
1da177e4 LT |
189 | |
190 | exit: | |
191 | return retval; | |
192 | } | |
193 | ||
f46753c5 | 194 | static ssize_t attention_write_file(struct pci_slot *slot, const char *buf, |
1da177e4 LT |
195 | size_t count) |
196 | { | |
f46753c5 | 197 | struct hotplug_slot_ops *ops = slot->hotplug->ops; |
1da177e4 LT |
198 | unsigned long lattention; |
199 | u8 attention; | |
200 | int retval = 0; | |
201 | ||
202 | lattention = simple_strtoul (buf, NULL, 10); | |
203 | attention = (u8)(lattention & 0xff); | |
204 | dbg (" - attention = %d\n", attention); | |
205 | ||
f46753c5 | 206 | if (!try_module_get(ops->owner)) { |
1da177e4 LT |
207 | retval = -ENODEV; |
208 | goto exit; | |
209 | } | |
f46753c5 AC |
210 | if (ops->set_attention_status) |
211 | retval = ops->set_attention_status(slot->hotplug, attention); | |
212 | module_put(ops->owner); | |
1da177e4 LT |
213 | |
214 | exit: | |
215 | if (retval) | |
216 | return retval; | |
217 | return count; | |
218 | } | |
219 | ||
f46753c5 | 220 | static struct pci_slot_attribute hotplug_slot_attr_attention = { |
1da177e4 LT |
221 | .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR}, |
222 | .show = attention_read_file, | |
223 | .store = attention_write_file | |
224 | }; | |
225 | ||
f46753c5 | 226 | static ssize_t latch_read_file(struct pci_slot *slot, char *buf) |
1da177e4 LT |
227 | { |
228 | int retval; | |
229 | u8 value; | |
230 | ||
f46753c5 | 231 | retval = get_latch_status(slot->hotplug, &value); |
1da177e4 LT |
232 | if (retval) |
233 | goto exit; | |
234 | retval = sprintf (buf, "%d\n", value); | |
235 | ||
236 | exit: | |
237 | return retval; | |
238 | } | |
239 | ||
f46753c5 | 240 | static struct pci_slot_attribute hotplug_slot_attr_latch = { |
1da177e4 LT |
241 | .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO}, |
242 | .show = latch_read_file, | |
243 | }; | |
244 | ||
f46753c5 | 245 | static ssize_t presence_read_file(struct pci_slot *slot, char *buf) |
1da177e4 LT |
246 | { |
247 | int retval; | |
248 | u8 value; | |
249 | ||
f46753c5 | 250 | retval = get_adapter_status(slot->hotplug, &value); |
1da177e4 LT |
251 | if (retval) |
252 | goto exit; | |
253 | retval = sprintf (buf, "%d\n", value); | |
254 | ||
255 | exit: | |
256 | return retval; | |
257 | } | |
258 | ||
f46753c5 | 259 | static struct pci_slot_attribute hotplug_slot_attr_presence = { |
1da177e4 LT |
260 | .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO}, |
261 | .show = presence_read_file, | |
262 | }; | |
263 | ||
1da177e4 LT |
264 | static char *unknown_speed = "Unknown bus speed"; |
265 | ||
f46753c5 | 266 | static ssize_t max_bus_speed_read_file(struct pci_slot *slot, char *buf) |
1da177e4 LT |
267 | { |
268 | char *speed_string; | |
269 | int retval; | |
270 | enum pci_bus_speed value; | |
271 | ||
f46753c5 | 272 | retval = get_max_bus_speed(slot->hotplug, &value); |
1da177e4 LT |
273 | if (retval) |
274 | goto exit; | |
275 | ||
276 | if (value == PCI_SPEED_UNKNOWN) | |
277 | speed_string = unknown_speed; | |
278 | else | |
279 | speed_string = pci_bus_speed_strings[value]; | |
280 | ||
281 | retval = sprintf (buf, "%s\n", speed_string); | |
282 | ||
283 | exit: | |
284 | return retval; | |
285 | } | |
286 | ||
f46753c5 | 287 | static struct pci_slot_attribute hotplug_slot_attr_max_bus_speed = { |
1da177e4 LT |
288 | .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO}, |
289 | .show = max_bus_speed_read_file, | |
290 | }; | |
291 | ||
f46753c5 | 292 | static ssize_t cur_bus_speed_read_file(struct pci_slot *slot, char *buf) |
1da177e4 LT |
293 | { |
294 | char *speed_string; | |
295 | int retval; | |
296 | enum pci_bus_speed value; | |
297 | ||
f46753c5 | 298 | retval = get_cur_bus_speed(slot->hotplug, &value); |
1da177e4 LT |
299 | if (retval) |
300 | goto exit; | |
301 | ||
302 | if (value == PCI_SPEED_UNKNOWN) | |
303 | speed_string = unknown_speed; | |
304 | else | |
305 | speed_string = pci_bus_speed_strings[value]; | |
306 | ||
307 | retval = sprintf (buf, "%s\n", speed_string); | |
308 | ||
309 | exit: | |
310 | return retval; | |
311 | } | |
312 | ||
f46753c5 | 313 | static struct pci_slot_attribute hotplug_slot_attr_cur_bus_speed = { |
1da177e4 LT |
314 | .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO}, |
315 | .show = cur_bus_speed_read_file, | |
316 | }; | |
317 | ||
f46753c5 | 318 | static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf, |
1da177e4 LT |
319 | size_t count) |
320 | { | |
f46753c5 | 321 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
322 | unsigned long ltest; |
323 | u32 test; | |
324 | int retval = 0; | |
325 | ||
326 | ltest = simple_strtoul (buf, NULL, 10); | |
327 | test = (u32)(ltest & 0xffffffff); | |
328 | dbg ("test = %d\n", test); | |
329 | ||
330 | if (!try_module_get(slot->ops->owner)) { | |
331 | retval = -ENODEV; | |
332 | goto exit; | |
333 | } | |
334 | if (slot->ops->hardware_test) | |
335 | retval = slot->ops->hardware_test(slot, test); | |
336 | module_put(slot->ops->owner); | |
337 | ||
338 | exit: | |
339 | if (retval) | |
340 | return retval; | |
341 | return count; | |
342 | } | |
343 | ||
f46753c5 | 344 | static struct pci_slot_attribute hotplug_slot_attr_test = { |
1da177e4 LT |
345 | .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR}, |
346 | .store = test_write_file | |
347 | }; | |
348 | ||
f46753c5 | 349 | static int has_power_file(struct pci_slot *pci_slot) |
1da177e4 | 350 | { |
f46753c5 | 351 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
352 | if ((!slot) || (!slot->ops)) |
353 | return -ENODEV; | |
354 | if ((slot->ops->enable_slot) || | |
355 | (slot->ops->disable_slot) || | |
356 | (slot->ops->get_power_status)) | |
357 | return 0; | |
358 | return -ENOENT; | |
359 | } | |
360 | ||
f46753c5 | 361 | static int has_attention_file(struct pci_slot *pci_slot) |
1da177e4 | 362 | { |
f46753c5 | 363 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
364 | if ((!slot) || (!slot->ops)) |
365 | return -ENODEV; | |
366 | if ((slot->ops->set_attention_status) || | |
367 | (slot->ops->get_attention_status)) | |
368 | return 0; | |
369 | return -ENOENT; | |
370 | } | |
371 | ||
f46753c5 | 372 | static int has_latch_file(struct pci_slot *pci_slot) |
1da177e4 | 373 | { |
f46753c5 | 374 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
375 | if ((!slot) || (!slot->ops)) |
376 | return -ENODEV; | |
377 | if (slot->ops->get_latch_status) | |
378 | return 0; | |
379 | return -ENOENT; | |
380 | } | |
381 | ||
f46753c5 | 382 | static int has_adapter_file(struct pci_slot *pci_slot) |
1da177e4 | 383 | { |
f46753c5 | 384 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
385 | if ((!slot) || (!slot->ops)) |
386 | return -ENODEV; | |
387 | if (slot->ops->get_adapter_status) | |
388 | return 0; | |
389 | return -ENOENT; | |
390 | } | |
391 | ||
f46753c5 | 392 | static int has_max_bus_speed_file(struct pci_slot *pci_slot) |
1da177e4 | 393 | { |
f46753c5 | 394 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
395 | if ((!slot) || (!slot->ops)) |
396 | return -ENODEV; | |
397 | if (slot->ops->get_max_bus_speed) | |
398 | return 0; | |
399 | return -ENOENT; | |
400 | } | |
401 | ||
f46753c5 | 402 | static int has_cur_bus_speed_file(struct pci_slot *pci_slot) |
1da177e4 | 403 | { |
f46753c5 | 404 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
405 | if ((!slot) || (!slot->ops)) |
406 | return -ENODEV; | |
407 | if (slot->ops->get_cur_bus_speed) | |
408 | return 0; | |
409 | return -ENOENT; | |
410 | } | |
411 | ||
f46753c5 | 412 | static int has_test_file(struct pci_slot *pci_slot) |
1da177e4 | 413 | { |
f46753c5 | 414 | struct hotplug_slot *slot = pci_slot->hotplug; |
1da177e4 LT |
415 | if ((!slot) || (!slot->ops)) |
416 | return -ENODEV; | |
417 | if (slot->ops->hardware_test) | |
418 | return 0; | |
419 | return -ENOENT; | |
420 | } | |
421 | ||
f46753c5 | 422 | static int fs_add_slot(struct pci_slot *slot) |
1da177e4 | 423 | { |
660a0e8f | 424 | int retval = 0; |
1da177e4 | 425 | |
660a0e8f GKH |
426 | if (has_power_file(slot) == 0) { |
427 | retval = sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr); | |
428 | if (retval) | |
429 | goto exit_power; | |
430 | } | |
1da177e4 | 431 | |
660a0e8f GKH |
432 | if (has_attention_file(slot) == 0) { |
433 | retval = sysfs_create_file(&slot->kobj, | |
434 | &hotplug_slot_attr_attention.attr); | |
435 | if (retval) | |
436 | goto exit_attention; | |
437 | } | |
1da177e4 | 438 | |
660a0e8f GKH |
439 | if (has_latch_file(slot) == 0) { |
440 | retval = sysfs_create_file(&slot->kobj, | |
441 | &hotplug_slot_attr_latch.attr); | |
442 | if (retval) | |
443 | goto exit_latch; | |
444 | } | |
1da177e4 | 445 | |
660a0e8f GKH |
446 | if (has_adapter_file(slot) == 0) { |
447 | retval = sysfs_create_file(&slot->kobj, | |
448 | &hotplug_slot_attr_presence.attr); | |
449 | if (retval) | |
450 | goto exit_adapter; | |
451 | } | |
1da177e4 | 452 | |
660a0e8f GKH |
453 | if (has_max_bus_speed_file(slot) == 0) { |
454 | retval = sysfs_create_file(&slot->kobj, | |
455 | &hotplug_slot_attr_max_bus_speed.attr); | |
456 | if (retval) | |
457 | goto exit_max_speed; | |
458 | } | |
459 | ||
460 | if (has_cur_bus_speed_file(slot) == 0) { | |
461 | retval = sysfs_create_file(&slot->kobj, | |
462 | &hotplug_slot_attr_cur_bus_speed.attr); | |
463 | if (retval) | |
464 | goto exit_cur_speed; | |
465 | } | |
466 | ||
467 | if (has_test_file(slot) == 0) { | |
468 | retval = sysfs_create_file(&slot->kobj, | |
469 | &hotplug_slot_attr_test.attr); | |
470 | if (retval) | |
471 | goto exit_test; | |
472 | } | |
473 | ||
474 | goto exit; | |
475 | ||
476 | exit_test: | |
1da177e4 | 477 | if (has_cur_bus_speed_file(slot) == 0) |
660a0e8f | 478 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr); |
1da177e4 | 479 | |
660a0e8f GKH |
480 | exit_cur_speed: |
481 | if (has_max_bus_speed_file(slot) == 0) | |
482 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr); | |
1da177e4 | 483 | |
660a0e8f | 484 | exit_max_speed: |
660a0e8f GKH |
485 | if (has_adapter_file(slot) == 0) |
486 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr); | |
487 | ||
488 | exit_adapter: | |
489 | if (has_latch_file(slot) == 0) | |
490 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr); | |
491 | ||
492 | exit_latch: | |
493 | if (has_attention_file(slot) == 0) | |
494 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr); | |
495 | ||
496 | exit_attention: | |
497 | if (has_power_file(slot) == 0) | |
498 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr); | |
499 | exit_power: | |
500 | exit: | |
501 | return retval; | |
1da177e4 LT |
502 | } |
503 | ||
f46753c5 | 504 | static void fs_remove_slot(struct pci_slot *slot) |
1da177e4 LT |
505 | { |
506 | if (has_power_file(slot) == 0) | |
507 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr); | |
508 | ||
509 | if (has_attention_file(slot) == 0) | |
510 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr); | |
511 | ||
512 | if (has_latch_file(slot) == 0) | |
513 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr); | |
514 | ||
515 | if (has_adapter_file(slot) == 0) | |
516 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr); | |
517 | ||
1da177e4 LT |
518 | if (has_max_bus_speed_file(slot) == 0) |
519 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr); | |
520 | ||
521 | if (has_cur_bus_speed_file(slot) == 0) | |
522 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr); | |
523 | ||
524 | if (has_test_file(slot) == 0) | |
525 | sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr); | |
526 | } | |
527 | ||
528 | static struct hotplug_slot *get_slot_from_name (const char *name) | |
529 | { | |
530 | struct hotplug_slot *slot; | |
531 | struct list_head *tmp; | |
532 | ||
f46753c5 | 533 | spin_lock(&pci_hotplug_slot_list_lock); |
1da177e4 LT |
534 | list_for_each (tmp, &pci_hotplug_slot_list) { |
535 | slot = list_entry (tmp, struct hotplug_slot, slot_list); | |
536 | if (strcmp(slot->name, name) == 0) | |
f46753c5 | 537 | goto out; |
1da177e4 | 538 | } |
f46753c5 AC |
539 | slot = NULL; |
540 | out: | |
541 | spin_unlock(&pci_hotplug_slot_list_lock); | |
542 | return slot; | |
1da177e4 LT |
543 | } |
544 | ||
545 | /** | |
546 | * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem | |
547 | * @slot: pointer to the &struct hotplug_slot to register | |
548 | * | |
549 | * Registers a hotplug slot with the pci hotplug subsystem, which will allow | |
550 | * userspace interaction to the slot. | |
551 | * | |
552 | * Returns 0 if successful, anything else for an error. | |
553 | */ | |
f46753c5 | 554 | int pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus, int slot_nr) |
1da177e4 LT |
555 | { |
556 | int result; | |
f46753c5 | 557 | struct pci_slot *pci_slot; |
a86161b3 | 558 | struct hotplug_slot *tmp; |
1da177e4 LT |
559 | |
560 | if (slot == NULL) | |
561 | return -ENODEV; | |
562 | if ((slot->info == NULL) || (slot->ops == NULL)) | |
563 | return -EINVAL; | |
564 | if (slot->release == NULL) { | |
a6f29a98 | 565 | dbg("Why are you trying to register a hotplug slot " |
1da177e4 LT |
566 | "without a proper release function?\n"); |
567 | return -EINVAL; | |
568 | } | |
569 | ||
a86161b3 KK |
570 | /* Check if we have already registered a slot with the same name. */ |
571 | tmp = get_slot_from_name(slot->name); | |
572 | if (tmp) | |
573 | return -EEXIST; | |
574 | ||
8344b568 AC |
575 | /* |
576 | * No problems if we call this interface from both ACPI_PCI_SLOT | |
577 | * driver and call it here again. If we've already created the | |
578 | * pci_slot, the interface will simply bump the refcount. | |
579 | */ | |
f46753c5 AC |
580 | pci_slot = pci_create_slot(bus, slot_nr, slot->name); |
581 | if (IS_ERR(pci_slot)) | |
582 | return PTR_ERR(pci_slot); | |
583 | ||
584 | if (pci_slot->hotplug) { | |
585 | dbg("%s: already claimed\n", __func__); | |
586 | pci_destroy_slot(pci_slot); | |
587 | return -EBUSY; | |
1da177e4 | 588 | } |
64dbcac3 | 589 | |
f46753c5 AC |
590 | slot->pci_slot = pci_slot; |
591 | pci_slot->hotplug = slot; | |
592 | ||
8344b568 AC |
593 | /* |
594 | * Allow pcihp drivers to override the ACPI_PCI_SLOT name. | |
595 | */ | |
596 | if (strcmp(kobject_name(&pci_slot->kobj), slot->name)) { | |
597 | result = kobject_rename(&pci_slot->kobj, slot->name); | |
598 | if (result) { | |
599 | pci_destroy_slot(pci_slot); | |
600 | return result; | |
601 | } | |
602 | } | |
603 | ||
f46753c5 AC |
604 | spin_lock(&pci_hotplug_slot_list_lock); |
605 | list_add(&slot->slot_list, &pci_hotplug_slot_list); | |
606 | spin_unlock(&pci_hotplug_slot_list_lock); | |
607 | ||
608 | result = fs_add_slot(pci_slot); | |
609 | kobject_uevent(&pci_slot->kobj, KOBJ_ADD); | |
610 | dbg("Added slot %s to the list\n", slot->name); | |
611 | ||
1da177e4 | 612 | |
1da177e4 LT |
613 | return result; |
614 | } | |
615 | ||
616 | /** | |
617 | * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem | |
618 | * @slot: pointer to the &struct hotplug_slot to deregister | |
619 | * | |
620 | * The @slot must have been registered with the pci hotplug subsystem | |
621 | * previously with a call to pci_hp_register(). | |
622 | * | |
623 | * Returns 0 if successful, anything else for an error. | |
624 | */ | |
f46753c5 | 625 | int pci_hp_deregister(struct hotplug_slot *hotplug) |
1da177e4 LT |
626 | { |
627 | struct hotplug_slot *temp; | |
f46753c5 | 628 | struct pci_slot *slot; |
1da177e4 | 629 | |
f46753c5 | 630 | if (!hotplug) |
1da177e4 LT |
631 | return -ENODEV; |
632 | ||
f46753c5 AC |
633 | temp = get_slot_from_name(hotplug->name); |
634 | if (temp != hotplug) | |
1da177e4 | 635 | return -ENODEV; |
1da177e4 | 636 | |
f46753c5 AC |
637 | spin_lock(&pci_hotplug_slot_list_lock); |
638 | list_del(&hotplug->slot_list); | |
639 | spin_unlock(&pci_hotplug_slot_list_lock); | |
640 | ||
641 | slot = hotplug->pci_slot; | |
642 | fs_remove_slot(slot); | |
643 | dbg("Removed slot %s from the list\n", hotplug->name); | |
644 | ||
645 | hotplug->release(hotplug); | |
646 | slot->hotplug = NULL; | |
647 | pci_destroy_slot(slot); | |
648 | ||
1da177e4 LT |
649 | return 0; |
650 | } | |
651 | ||
652 | /** | |
653 | * pci_hp_change_slot_info - changes the slot's information structure in the core | |
654 | * @slot: pointer to the slot whose info has changed | |
655 | * @info: pointer to the info copy into the slot's info structure | |
656 | * | |
657 | * @slot must have been registered with the pci | |
658 | * hotplug subsystem previously with a call to pci_hp_register(). | |
659 | * | |
660 | * Returns 0 if successful, anything else for an error. | |
661 | */ | |
f46753c5 | 662 | int __must_check pci_hp_change_slot_info(struct hotplug_slot *hotplug, |
660a0e8f | 663 | struct hotplug_slot_info *info) |
1da177e4 | 664 | { |
f46753c5 AC |
665 | struct pci_slot *slot; |
666 | if (!hotplug || !info) | |
1da177e4 | 667 | return -ENODEV; |
f46753c5 | 668 | slot = hotplug->pci_slot; |
1da177e4 | 669 | |
f46753c5 | 670 | memcpy(hotplug->info, info, sizeof(struct hotplug_slot_info)); |
1da177e4 LT |
671 | |
672 | return 0; | |
673 | } | |
674 | ||
675 | static int __init pci_hotplug_init (void) | |
676 | { | |
677 | int result; | |
0fed80f7 | 678 | |
1da177e4 LT |
679 | result = cpci_hotplug_init(debug); |
680 | if (result) { | |
681 | err ("cpci_hotplug_init with error %d\n", result); | |
f46753c5 | 682 | goto err_cpci; |
1da177e4 LT |
683 | } |
684 | ||
685 | info (DRIVER_DESC " version: " DRIVER_VERSION "\n"); | |
81ace5cd | 686 | |
f46753c5 | 687 | err_cpci: |
1da177e4 LT |
688 | return result; |
689 | } | |
690 | ||
691 | static void __exit pci_hotplug_exit (void) | |
692 | { | |
693 | cpci_hotplug_exit(); | |
1da177e4 LT |
694 | } |
695 | ||
696 | module_init(pci_hotplug_init); | |
697 | module_exit(pci_hotplug_exit); | |
698 | ||
699 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
700 | MODULE_DESCRIPTION(DRIVER_DESC); | |
701 | MODULE_LICENSE("GPL"); | |
702 | module_param(debug, bool, 0644); | |
703 | MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); | |
704 | ||
1da177e4 LT |
705 | EXPORT_SYMBOL_GPL(pci_hp_register); |
706 | EXPORT_SYMBOL_GPL(pci_hp_deregister); | |
707 | EXPORT_SYMBOL_GPL(pci_hp_change_slot_info); |