patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / pci / hotplug / acpiphp_core.c
1 /*
2  * ACPI PCI Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2001 IBM Corp.
7  * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
8  * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
9  * Copyright (C) 2002,2003 NEC Corporation
10  *
11  * All rights reserved.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or (at
16  * your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
21  * NON INFRINGEMENT.  See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  * Send feedback to <gregkh@us.ibm.com>,
29  *                  <t-kochi@bq.jp.nec.com>
30  *
31  */
32
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36
37 #include <linux/kernel.h>
38 #include <linux/pci.h>
39 #include <linux/slab.h>
40 #include <linux/smp.h>
41 #include <linux/smp_lock.h>
42 #include "pci_hotplug.h"
43 #include "acpiphp.h"
44
45 static LIST_HEAD(slot_list);
46
47 #define MY_NAME "acpiphp"
48
49 static int debug;
50 int acpiphp_debug;
51
52 /* local variables */
53 static int num_slots;
54
55 #define DRIVER_VERSION  "0.4"
56 #define DRIVER_AUTHOR   "Greg Kroah-Hartman <gregkh@us.ibm.com>, Takayoshi Kochi <t-kochi@bq.jp.nec.com>"
57 #define DRIVER_DESC     "ACPI Hot Plug PCI Controller Driver"
58
59 MODULE_AUTHOR(DRIVER_AUTHOR);
60 MODULE_DESCRIPTION(DRIVER_DESC);
61 MODULE_LICENSE("GPL");
62 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
63 module_param(debug, bool, 644);
64
65 static int enable_slot          (struct hotplug_slot *slot);
66 static int disable_slot         (struct hotplug_slot *slot);
67 static int set_attention_status (struct hotplug_slot *slot, u8 value);
68 static int get_power_status     (struct hotplug_slot *slot, u8 *value);
69 static int get_address          (struct hotplug_slot *slot, u32 *value);
70 static int get_latch_status     (struct hotplug_slot *slot, u8 *value);
71 static int get_adapter_status   (struct hotplug_slot *slot, u8 *value);
72
73 static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
74         .owner                  = THIS_MODULE,
75         .enable_slot            = enable_slot,
76         .disable_slot           = disable_slot,
77         .set_attention_status   = set_attention_status,
78         .get_power_status       = get_power_status,
79         .get_latch_status       = get_latch_status,
80         .get_adapter_status     = get_adapter_status,
81         .get_address            = get_address,
82 };
83
84 /**
85  * enable_slot - power on and enable a slot
86  * @hotplug_slot: slot to enable
87  *
88  * Actual tasks are done in acpiphp_enable_slot()
89  *
90  */
91 static int enable_slot(struct hotplug_slot *hotplug_slot)
92 {
93         struct slot *slot = hotplug_slot->private;
94
95         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
96
97         /* enable the specified slot */
98         return acpiphp_enable_slot(slot->acpi_slot);
99 }
100
101
102 /**
103  * disable_slot - disable and power off a slot
104  * @hotplug_slot: slot to disable
105  *
106  * Actual tasks are done in acpiphp_disable_slot()
107  *
108  */
109 static int disable_slot(struct hotplug_slot *hotplug_slot)
110 {
111         struct slot *slot = hotplug_slot->private;
112
113         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
114
115         /* disable the specified slot */
116         return acpiphp_disable_slot(slot->acpi_slot);
117 }
118
119
120 /**
121  * set_attention_status - set attention LED
122  *
123  * TBD:
124  * ACPI doesn't have known method to manipulate
125  * attention status LED.
126  *
127  */
128 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
129 {
130         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
131
132         switch (status) {
133                 case 0:
134                         /* FIXME turn light off */
135                         hotplug_slot->info->attention_status = 0;
136                         break;
137
138                 case 1:
139                 default:
140                         /* FIXME turn light on */
141                         hotplug_slot->info->attention_status = 1;
142                         break;
143         }
144
145         return 0;
146 }
147
148 /**
149  * get_power_status - get power status of a slot
150  * @hotplug_slot: slot to get status
151  * @value: pointer to store status
152  *
153  * Some platforms may not implement _STA method properly.
154  * In that case, the value returned may not be reliable.
155  *
156  */
157 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
158 {
159         struct slot *slot = hotplug_slot->private;
160
161         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
162
163         *value = acpiphp_get_power_status(slot->acpi_slot);
164
165         return 0;
166 }
167
168 /**
169  * get_latch_status - get latch status of a slot
170  * @hotplug_slot: slot to get status
171  * @value: pointer to store status
172  *
173  * ACPI doesn't provide any formal means to access latch status.
174  * Instead, we fake latch status from _STA
175  *
176  */
177 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
178 {
179         struct slot *slot = hotplug_slot->private;
180
181         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
182
183         *value = acpiphp_get_latch_status(slot->acpi_slot);
184
185         return 0;
186 }
187
188
189 /**
190  * get_adapter_status - get adapter status of a slot
191  * @hotplug_slot: slot to get status
192  * @value: pointer to store status
193  *
194  * ACPI doesn't provide any formal means to access adapter status.
195  * Instead, we fake adapter status from _STA
196  *
197  */
198 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
199 {
200         struct slot *slot = hotplug_slot->private;
201
202         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
203
204         *value = acpiphp_get_adapter_status(slot->acpi_slot);
205
206         return 0;
207 }
208
209
210 /**
211  * get_address - get pci address of a slot
212  * @hotplug_slot: slot to get status
213  * @busdev: pointer to struct pci_busdev (seg, bus, dev)
214  *
215  */
216 static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
217 {
218         struct slot *slot = hotplug_slot->private;
219
220         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
221
222         *value = acpiphp_get_address(slot->acpi_slot);
223
224         return 0;
225 }
226
227 static int __init init_acpi(void)
228 {
229         int retval;
230
231         /* initialize internal data structure etc. */
232         retval = acpiphp_glue_init();
233
234         /* read initial number of slots */
235         if (!retval) {
236                 num_slots = acpiphp_get_num_slots();
237                 if (num_slots == 0)
238                         retval = -ENODEV;
239         }
240
241         return retval;
242 }
243
244
245 /**
246  * make_slot_name - make a slot name that appears in pcihpfs
247  * @slot: slot to name
248  *
249  */
250 static void make_slot_name(struct slot *slot)
251 {
252         snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%u",
253                  slot->acpi_slot->sun);
254 }
255
256 /**
257  * release_slot - free up the memory used by a slot
258  * @hotplug_slot: slot to free
259  */
260 static void release_slot(struct hotplug_slot *hotplug_slot)
261 {
262         struct slot *slot = hotplug_slot->private;
263
264         dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
265
266         kfree(slot->hotplug_slot->info);
267         kfree(slot->hotplug_slot->name);
268         kfree(slot->hotplug_slot);
269         kfree(slot);
270 }
271
272 /**
273  * init_slots - initialize 'struct slot' structures for each slot
274  *
275  */
276 static int __init init_slots(void)
277 {
278         struct slot *slot;
279         int retval = -ENOMEM;
280         int i;
281
282         for (i = 0; i < num_slots; ++i) {
283                 slot = kmalloc(sizeof(struct slot), GFP_KERNEL);
284                 if (!slot)
285                         goto error;
286                 memset(slot, 0, sizeof(struct slot));
287
288                 slot->hotplug_slot = kmalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
289                 if (!slot->hotplug_slot)
290                         goto error_slot;
291                 memset(slot->hotplug_slot, 0, sizeof(struct hotplug_slot));
292
293                 slot->hotplug_slot->info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
294                 if (!slot->hotplug_slot->info)
295                         goto error_hpslot;
296                 memset(slot->hotplug_slot->info, 0, sizeof(struct hotplug_slot_info));
297
298                 slot->hotplug_slot->name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
299                 if (!slot->hotplug_slot->name)
300                         goto error_info;
301
302                 slot->number = i;
303
304                 slot->hotplug_slot->private = slot;
305                 slot->hotplug_slot->release = &release_slot;
306                 slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
307
308                 slot->acpi_slot = get_slot_from_id(i);
309                 slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
310                 slot->hotplug_slot->info->attention_status = acpiphp_get_attention_status(slot->acpi_slot);
311                 slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
312                 slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
313                 slot->hotplug_slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
314                 slot->hotplug_slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
315
316                 make_slot_name(slot);
317
318                 retval = pci_hp_register(slot->hotplug_slot);
319                 if (retval) {
320                         err("pci_hp_register failed with error %d\n", retval);
321                         goto error_name;
322                 }
323
324                 /* add slot to our internal list */
325                 list_add(&slot->slot_list, &slot_list);
326                 info("Slot [%s] registered\n", slot->hotplug_slot->name);
327         }
328
329         return 0;
330 error_name:
331         kfree(slot->hotplug_slot->name);
332 error_info:
333         kfree(slot->hotplug_slot->info);
334 error_hpslot:
335         kfree(slot->hotplug_slot);
336 error_slot:
337         kfree(slot);
338 error:
339         return retval;
340 }
341
342
343 static void __exit cleanup_slots (void)
344 {
345         struct list_head *tmp, *n;
346         struct slot *slot;
347
348         list_for_each_safe (tmp, n, &slot_list) {
349                 /* memory will be freed in release_slot callback */
350                 slot = list_entry(tmp, struct slot, slot_list);
351                 list_del(&slot->slot_list);
352                 pci_hp_deregister(slot->hotplug_slot);
353         }
354 }
355
356
357 static int __init acpiphp_init(void)
358 {
359         int retval;
360
361         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
362
363         acpiphp_debug = debug;
364
365         /* read all the ACPI info from the system */
366         retval = init_acpi();
367         if (retval)
368                 return retval;
369
370         return init_slots();
371 }
372
373
374 static void __exit acpiphp_exit(void)
375 {
376         cleanup_slots();
377         /* deallocate internal data structures etc. */
378         acpiphp_glue_exit();
379 }
380
381 module_init(acpiphp_init);
382 module_exit(acpiphp_exit);