Subversion Repositories shark

Rev

Rev 456 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
455 giacomo 1
/*
2
 * drivers/base/core.c - core driver model code (device registration, etc)
3
 *
4
 * Copyright (c) 2002-3 Patrick Mochel
5
 * Copyright (c) 2002-3 Open Source Development Labs
6
 *
7
 * This file is released under the GPLv2
8
 *
9
 */
10
 
11
#undef DEBUG
12
 
13
#include <linuxcomp.h>
14
 
15
#include <linux/device.h>
16
#include <linux/err.h>
17
#include <linux/init.h>
18
#include <linux/module.h>
19
#include <linux/slab.h>
20
#include <linux/string.h>
21
 
22
#include <asm/semaphore.h>
23
 
24
#include "base.h"
25
 
26
int (*platform_notify)(struct device * dev) = NULL;
27
int (*platform_notify_remove)(struct device * dev) = NULL;
28
 
29
/*
30
 * sysfs bindings for devices.
31
 */
32
 
33
#define to_dev(obj) container_of(obj,struct device,kobj)
34
#define to_dev_attr(_attr) container_of(_attr,struct device_attribute,attr)
35
 
36
extern struct attribute * dev_default_attrs[];
37
 
38
static ssize_t
39
dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
40
{
41
        struct device_attribute * dev_attr = to_dev_attr(attr);
42
        struct device * dev = to_dev(kobj);
43
        ssize_t ret = 0;
44
 
45
        if (dev_attr->show)
46
                ret = dev_attr->show(dev,buf);
47
        return ret;
48
}
49
 
50
static ssize_t
51
dev_attr_store(struct kobject * kobj, struct attribute * attr,
52
               const char * buf, size_t count)
53
{
54
        struct device_attribute * dev_attr = to_dev_attr(attr);
55
        struct device * dev = to_dev(kobj);
56
        ssize_t ret = 0;
57
 
58
        if (dev_attr->store)
59
                ret = dev_attr->store(dev,buf,count);
60
        return ret;
61
}
62
 
63
static struct sysfs_ops dev_sysfs_ops = {
64
        .show   = dev_attr_show,
65
        .store  = dev_attr_store,
66
};
67
 
68
 
69
/**
70
 *      device_release - free device structure.
71
 *      @kobj:  device's kobject.
72
 *
73
 *      This is called once the reference count for the object
74
 *      reaches 0. We forward the call to the device's release
75
 *      method, which should handle actually freeing the structure.
76
 */
77
static void device_release(struct kobject * kobj)
78
{
79
        struct device * dev = to_dev(kobj);
80
        //struct completion * c = dev->complete;
81
 
82
        if (dev->release)
83
                dev->release(dev);
84
        else {
85
                printk(KERN_ERR "Device '%s' does not have a release() function, "
86
                        "it is broken and must be fixed.\n",
87
                        dev->bus_id);
88
                WARN_ON(1);
89
        }
90
        //if (c)
91
        //      complete(c);
92
}
93
 
94
static struct kobj_type ktype_device = {
95
        .release        = device_release,
96
        .sysfs_ops      = &dev_sysfs_ops,
97
        .default_attrs  = dev_default_attrs,
98
};
99
 
100
 
101
static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj)
102
{
103
        struct kobj_type *ktype = get_ktype(kobj);
104
 
105
        if (ktype == &ktype_device) {
106
                struct device *dev = to_dev(kobj);
107
                if (dev->bus)
108
                        return 1;
109
        }
110
        return 0;
111
}
112
 
113
static char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
114
{
115
        struct device *dev = to_dev(kobj);
116
 
117
        return dev->bus->name;
118
}
119
 
120
static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
121
                        int num_envp, char *buffer, int buffer_size)
122
{
123
        struct device *dev = to_dev(kobj);
124
        int retval = 0;
125
 
126
        if (dev->bus->hotplug) {
127
                /* have the bus specific function add its stuff */
128
                retval = dev->bus->hotplug (dev, envp, num_envp, buffer, buffer_size);
129
                        if (retval) {
130
                        pr_debug ("%s - hotplug() returned %d\n",
131
                                  __FUNCTION__, retval);
132
                }
133
        }
134
 
135
        return retval;
136
}
137
 
138
static struct kset_hotplug_ops device_hotplug_ops = {
139
        .filter =       dev_hotplug_filter,
140
        .name =         dev_hotplug_name,
141
        .hotplug =      dev_hotplug,
142
};
143
 
144
/**
145
 *      device_subsys - structure to be registered with kobject core.
146
 */
147
 
148
decl_subsys(devices, &ktype_device, &device_hotplug_ops);
149
 
150
 
151
/**
152
 *      device_create_file - create sysfs attribute file for device.
153
 *      @dev:   device.
154
 *      @attr:  device attribute descriptor.
155
 */
156
 
157
int device_create_file(struct device * dev, struct device_attribute * attr)
158
{
159
        int error = 0;
160
        if (get_device(dev)) {
161
                error = 0;//sysfs_create_file(&dev->kobj,&attr->attr);
162
                put_device(dev);
163
        }
164
        return error;
165
}
166
 
167
/**
168
 *      device_remove_file - remove sysfs attribute file.
169
 *      @dev:   device.
170
 *      @attr:  device attribute descriptor.
171
 */
172
 
173
void device_remove_file(struct device * dev, struct device_attribute * attr)
174
{
175
        if (get_device(dev)) {
176
                //sysfs_remove_file(&dev->kobj,&attr->attr);
177
                put_device(dev);
178
        }
179
}
180
 
181
 
182
/**
183
 *      device_initialize - init device structure.
184
 *      @dev:   device.
185
 *
186
 *      This prepares the device for use by other layers,
187
 *      including adding it to the device hierarchy.
188
 *      It is the first half of device_register(), if called by
189
 *      that, though it can also be called separately, so one
190
 *      may use @dev's fields (e.g. the refcount).
191
 */
192
 
193
void device_initialize(struct device *dev)
194
{
195
        kobj_set_kset_s(dev,devices_subsys);
196
        kobject_init(&dev->kobj);
197
        INIT_LIST_HEAD(&dev->node);
198
        INIT_LIST_HEAD(&dev->children);
199
        INIT_LIST_HEAD(&dev->driver_list);
200
        INIT_LIST_HEAD(&dev->bus_list);
201
}
202
 
203
/**
204
 *      device_add - add device to device hierarchy.
205
 *      @dev:   device.
206
 *
207
 *      This is part 2 of device_register(), though may be called
208
 *      separately _iff_ device_initialize() has been called separately.
209
 *
210
 *      This adds it to the kobject hierarchy via kobject_add(), adds it
211
 *      to the global and sibling lists for the device, then
212
 *      adds it to the other relevant subsystems of the driver model.
213
 */
214
int device_add(struct device *dev)
215
{
216
        struct device * parent;
217
        int error;
218
 
219
        dev = get_device(dev);
220
        if (!dev || !strlen(dev->bus_id))
221
                return -EINVAL;
222
 
223
        parent = get_device(dev->parent);
224
 
225
        pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
226
 
227
        /* first, register with generic layer. */
228
        kobject_set_name(&dev->kobj,dev->bus_id);
229
        if (parent)
230
                dev->kobj.parent = &parent->kobj;
231
 
232
        if ((error = kobject_add(&dev->kobj)))
233
                goto Error;
234
        //if ((error = device_pm_add(dev)))
235
        //      goto PMError;
236
        if ((error = bus_add_device(dev)))
237
                goto BusError;
238
        //down_write(&devices_subsys.rwsem);
239
        if (parent)
240
                list_add_tail(&dev->node,&parent->children);
241
        //up_write(&devices_subsys.rwsem);
242
 
243
        /* notify platform of device entry */
244
        if (platform_notify)
245
                platform_notify(dev);
246
 Done:
247
        put_device(dev);
248
        return error;
249
 BusError:
250
        //device_pm_remove(dev);
251
 //PMError:
252
        kobject_unregister(&dev->kobj);
253
 Error:
254
        if (parent)
255
                put_device(parent);
256
        goto Done;
257
}
258
 
259
 
260
/**
261
 *      device_register - register a device with the system.
262
 *      @dev:   pointer to the device structure
263
 *
264
 *      This happens in two clean steps - initialize the device
265
 *      and add it to the system. The two steps can be called
266
 *      separately, but this is the easiest and most common.
267
 *      I.e. you should only call the two helpers separately if
268
 *      have a clearly defined need to use and refcount the device
269
 *      before it is added to the hierarchy.
270
 */
271
 
272
int device_register(struct device *dev)
273
{
274
        device_initialize(dev);
275
        return device_add(dev);
276
}
277
 
278
 
279
/**
280
 *      get_device - increment reference count for device.
281
 *      @dev:   device.
282
 *
283
 *      This simply forwards the call to kobject_get(), though
284
 *      we do take care to provide for the case that we get a NULL
285
 *      pointer passed in.
286
 */
287
 
288
struct device * get_device(struct device * dev)
289
{
290
        return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
291
}
292
 
293
 
294
/**
295
 *      put_device - decrement reference count.
296
 *      @dev:   device in question.
297
 */
298
void put_device(struct device * dev)
299
{
300
        kobject_put(&dev->kobj);
301
}
302
 
303
 
304
/**
305
 *      device_del - delete device from system.
306
 *      @dev:   device.
307
 *
308
 *      This is the first part of the device unregistration
309
 *      sequence. This removes the device from the lists we control
310
 *      from here, has it removed from the other driver model
311
 *      subsystems it was added to in device_add(), and removes it
312
 *      from the kobject hierarchy.
313
 *
314
 *      NOTE: this should be called manually _iff_ device_add() was
315
 *      also called manually.
316
 */
317
 
318
void device_del(struct device * dev)
319
{
320
        struct device * parent = dev->parent;
321
 
322
        //down_write(&devices_subsys.rwsem);
323
        if (parent)
324
                list_del_init(&dev->node);
325
        //up_write(&devices_subsys.rwsem);
326
 
327
        /* Notify the platform of the removal, in case they
328
         * need to do anything...
329
         */
330
        if (platform_notify_remove)
331
                platform_notify_remove(dev);
332
        bus_remove_device(dev);
333
        //device_pm_remove(dev);
334
        kobject_del(&dev->kobj);
335
        if (parent)
336
                put_device(parent);
337
}
338
 
339
/**
340
 *      device_unregister - unregister device from system.
341
 *      @dev:   device going away.
342
 *
343
 *      We do this in two parts, like we do device_register(). First,
344
 *      we remove it from all the subsystems with device_del(), then
345
 *      we decrement the reference count via put_device(). If that
346
 *      is the final reference count, the device will be cleaned up
347
 *      via device_release() above. Otherwise, the structure will
348
 *      stick around until the final reference to the device is dropped.
349
 */
350
void device_unregister(struct device * dev)
351
{
352
        pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
353
        device_del(dev);
354
        put_device(dev);
355
}
356
 
357
 
358
/**
359
 *      device_unregister_wait - Unregister device and wait for it to be freed.
360
 *      @dev: Device to unregister.
361
 *
362
 *      For the cases where the caller needs to wait for all references to
363
 *      be dropped from the device before continuing (e.g. modules with
364
 *      statically allocated devices), this function uses a completion struct
365
 *      to wait, along with a matching complete() in device_release() above.
366
 */
367
 
368
void device_unregister_wait(struct device * dev)
369
{
370
        struct completion c;
371
        init_completion(&c);
372
        dev->complete = &c;
373
        device_unregister(dev);
374
        //wait_for_completion(&c);
375
}
376
 
377
/**
378
 *      device_for_each_child - device child iterator.
379
 *      @dev:   parent struct device.
380
 *      @data:  data for the callback.
381
 *      @fn:    function to be called for each device.
382
 *
383
 *      Iterate over @dev's child devices, and call @fn for each,
384
 *      passing it @data.
385
 *
386
 *      We check the return of @fn each time. If it returns anything
387
 *      other than 0, we break out and return that value.
388
 */
389
int device_for_each_child(struct device * dev, void * data,
390
                     int (*fn)(struct device *, void *))
391
{
392
        struct device * child;
393
        int error = 0;
394
 
395
        //down_read(&devices_subsys.rwsem);
396
        list_for_each_entry(child,&dev->children,node) {
397
                if((error = fn(child,data)))
398
                        break;
399
        }
400
        //up_read(&devices_subsys.rwsem);
401
        return error;
402
}
403
 
404
int __init devices_init(void)
405
{
406
        return subsystem_register(&devices_subsys);
407
}
408
 
409
EXPORT_SYMBOL(device_for_each_child);
410
 
411
EXPORT_SYMBOL(device_initialize);
412
EXPORT_SYMBOL(device_add);
413
EXPORT_SYMBOL(device_register);
414
 
415
EXPORT_SYMBOL(device_del);
416
EXPORT_SYMBOL(device_unregister);
417
EXPORT_SYMBOL(device_unregister_wait);
418
EXPORT_SYMBOL(get_device);
419
EXPORT_SYMBOL(put_device);
420
 
421
EXPORT_SYMBOL(device_create_file);
422
EXPORT_SYMBOL(device_remove_file);