Subversion Repositories shark

Rev

Rev 455 | Rev 457 | Go to most recent revision | Details | Compare with Previous | 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
        }
89
        //if (c)
90
        //      complete(c);
91
}
92
 
93
static struct kobj_type ktype_device = {
94
        .release        = device_release,
95
        .sysfs_ops      = &dev_sysfs_ops,
96
        .default_attrs  = dev_default_attrs,
97
};
98
 
99
 
100
static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj)
101
{
102
        struct kobj_type *ktype = get_ktype(kobj);
103
 
104
        if (ktype == &ktype_device) {
105
                struct device *dev = to_dev(kobj);
106
                if (dev->bus)
107
                        return 1;
108
        }
109
        return 0;
110
}
111
 
112
static char *dev_hotplug_name(struct kset *kset, struct kobject *kobj)
113
{
114
        struct device *dev = to_dev(kobj);
115
 
116
        return dev->bus->name;
117
}
118
 
119
static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
120
                        int num_envp, char *buffer, int buffer_size)
121
{
122
        struct device *dev = to_dev(kobj);
123
        int retval = 0;
124
 
125
        if (dev->bus->hotplug) {
126
                /* have the bus specific function add its stuff */
127
                retval = dev->bus->hotplug (dev, envp, num_envp, buffer, buffer_size);
128
                        if (retval) {
129
                        pr_debug ("%s - hotplug() returned %d\n",
130
                                  __FUNCTION__, retval);
131
                }
132
        }
133
 
134
        return retval;
135
}
136
 
137
static struct kset_hotplug_ops device_hotplug_ops = {
138
        .filter =       dev_hotplug_filter,
139
        .name =         dev_hotplug_name,
140
        .hotplug =      dev_hotplug,
141
};
142
 
143
/**
144
 *      device_subsys - structure to be registered with kobject core.
145
 */
146
 
147
decl_subsys(devices, &ktype_device, &device_hotplug_ops);
148
 
149
 
150
/**
151
 *      device_create_file - create sysfs attribute file for device.
152
 *      @dev:   device.
153
 *      @attr:  device attribute descriptor.
154
 */
155
 
156
int device_create_file(struct device * dev, struct device_attribute * attr)
157
{
158
        int error = 0;
159
        if (get_device(dev)) {
160
                error = 0;//sysfs_create_file(&dev->kobj,&attr->attr);
161
                put_device(dev);
162
        }
163
        return error;
164
}
165
 
166
/**
167
 *      device_remove_file - remove sysfs attribute file.
168
 *      @dev:   device.
169
 *      @attr:  device attribute descriptor.
170
 */
171
 
172
void device_remove_file(struct device * dev, struct device_attribute * attr)
173
{
174
        if (get_device(dev)) {
175
                //sysfs_remove_file(&dev->kobj,&attr->attr);
176
                put_device(dev);
177
        }
178
}
179
 
180
 
181
/**
182
 *      device_initialize - init device structure.
183
 *      @dev:   device.
184
 *
185
 *      This prepares the device for use by other layers,
186
 *      including adding it to the device hierarchy.
187
 *      It is the first half of device_register(), if called by
188
 *      that, though it can also be called separately, so one
189
 *      may use @dev's fields (e.g. the refcount).
190
 */
191
 
192
void device_initialize(struct device *dev)
193
{
194
        kobj_set_kset_s(dev,devices_subsys);
195
        kobject_init(&dev->kobj);
196
        INIT_LIST_HEAD(&dev->node);
197
        INIT_LIST_HEAD(&dev->children);
198
        INIT_LIST_HEAD(&dev->driver_list);
199
        INIT_LIST_HEAD(&dev->bus_list);
200
}
201
 
202
/**
203
 *      device_add - add device to device hierarchy.
204
 *      @dev:   device.
205
 *
206
 *      This is part 2 of device_register(), though may be called
207
 *      separately _iff_ device_initialize() has been called separately.
208
 *
209
 *      This adds it to the kobject hierarchy via kobject_add(), adds it
210
 *      to the global and sibling lists for the device, then
211
 *      adds it to the other relevant subsystems of the driver model.
212
 */
213
int device_add(struct device *dev)
214
{
215
        struct device * parent;
216
        int error;
217
 
218
        dev = get_device(dev);
219
        if (!dev || !strlen(dev->bus_id))
220
                return -EINVAL;
221
 
222
        parent = get_device(dev->parent);
223
 
224
        pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
225
 
226
        /* first, register with generic layer. */
227
        kobject_set_name(&dev->kobj,dev->bus_id);
228
        if (parent)
229
                dev->kobj.parent = &parent->kobj;
230
 
231
        if ((error = kobject_add(&dev->kobj)))
232
                goto Error;
233
        //if ((error = device_pm_add(dev)))
234
        //      goto PMError;
235
        if ((error = bus_add_device(dev)))
236
                goto BusError;
237
        //down_write(&devices_subsys.rwsem);
238
        if (parent)
239
                list_add_tail(&dev->node,&parent->children);
240
        //up_write(&devices_subsys.rwsem);
241
 
242
        /* notify platform of device entry */
243
        if (platform_notify)
244
                platform_notify(dev);
245
 Done:
246
        put_device(dev);
247
        return error;
248
 BusError:
249
        //device_pm_remove(dev);
250
 //PMError:
251
        kobject_unregister(&dev->kobj);
252
 Error:
253
        if (parent)
254
                put_device(parent);
255
        goto Done;
256
}
257
 
258
 
259
/**
260
 *      device_register - register a device with the system.
261
 *      @dev:   pointer to the device structure
262
 *
263
 *      This happens in two clean steps - initialize the device
264
 *      and add it to the system. The two steps can be called
265
 *      separately, but this is the easiest and most common.
266
 *      I.e. you should only call the two helpers separately if
267
 *      have a clearly defined need to use and refcount the device
268
 *      before it is added to the hierarchy.
269
 */
270
 
271
int device_register(struct device *dev)
272
{
273
        device_initialize(dev);
274
        return device_add(dev);
275
}
276
 
277
 
278
/**
279
 *      get_device - increment reference count for device.
280
 *      @dev:   device.
281
 *
282
 *      This simply forwards the call to kobject_get(), though
283
 *      we do take care to provide for the case that we get a NULL
284
 *      pointer passed in.
285
 */
286
 
287
struct device * get_device(struct device * dev)
288
{
289
        return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
290
}
291
 
292
 
293
/**
294
 *      put_device - decrement reference count.
295
 *      @dev:   device in question.
296
 */
297
void put_device(struct device * dev)
298
{
299
        kobject_put(&dev->kobj);
300
}
301
 
302
 
303
/**
304
 *      device_del - delete device from system.
305
 *      @dev:   device.
306
 *
307
 *      This is the first part of the device unregistration
308
 *      sequence. This removes the device from the lists we control
309
 *      from here, has it removed from the other driver model
310
 *      subsystems it was added to in device_add(), and removes it
311
 *      from the kobject hierarchy.
312
 *
313
 *      NOTE: this should be called manually _iff_ device_add() was
314
 *      also called manually.
315
 */
316
 
317
void device_del(struct device * dev)
318
{
319
        struct device * parent = dev->parent;
320
 
321
        //down_write(&devices_subsys.rwsem);
322
        if (parent)
323
                list_del_init(&dev->node);
324
        //up_write(&devices_subsys.rwsem);
325
 
326
        /* Notify the platform of the removal, in case they
327
         * need to do anything...
328
         */
329
        if (platform_notify_remove)
330
                platform_notify_remove(dev);
331
        bus_remove_device(dev);
332
        //device_pm_remove(dev);
333
        kobject_del(&dev->kobj);
334
        if (parent)
335
                put_device(parent);
336
}
337
 
338
/**
339
 *      device_unregister - unregister device from system.
340
 *      @dev:   device going away.
341
 *
342
 *      We do this in two parts, like we do device_register(). First,
343
 *      we remove it from all the subsystems with device_del(), then
344
 *      we decrement the reference count via put_device(). If that
345
 *      is the final reference count, the device will be cleaned up
346
 *      via device_release() above. Otherwise, the structure will
347
 *      stick around until the final reference to the device is dropped.
348
 */
349
void device_unregister(struct device * dev)
350
{
351
        pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
352
        device_del(dev);
353
        put_device(dev);
354
}
355
 
356
 
357
/**
358
 *      device_unregister_wait - Unregister device and wait for it to be freed.
359
 *      @dev: Device to unregister.
360
 *
361
 *      For the cases where the caller needs to wait for all references to
362
 *      be dropped from the device before continuing (e.g. modules with
363
 *      statically allocated devices), this function uses a completion struct
364
 *      to wait, along with a matching complete() in device_release() above.
365
 */
366
 
367
void device_unregister_wait(struct device * dev)
368
{
369
        struct completion c;
370
        init_completion(&c);
371
        dev->complete = &c;
372
        device_unregister(dev);
373
        //wait_for_completion(&c);
374
}
375
 
376
/**
377
 *      device_for_each_child - device child iterator.
378
 *      @dev:   parent struct device.
379
 *      @data:  data for the callback.
380
 *      @fn:    function to be called for each device.
381
 *
382
 *      Iterate over @dev's child devices, and call @fn for each,
383
 *      passing it @data.
384
 *
385
 *      We check the return of @fn each time. If it returns anything
386
 *      other than 0, we break out and return that value.
387
 */
388
int device_for_each_child(struct device * dev, void * data,
389
                     int (*fn)(struct device *, void *))
390
{
391
        struct device * child;
392
        int error = 0;
393
 
394
        //down_read(&devices_subsys.rwsem);
395
        list_for_each_entry(child,&dev->children,node) {
396
                if((error = fn(child,data)))
397
                        break;
398
        }
399
        //up_read(&devices_subsys.rwsem);
400
        return error;
401
}
402
 
403
int __init devices_init(void)
404
{
405
        return subsystem_register(&devices_subsys);
406
}
407
 
408
EXPORT_SYMBOL(device_for_each_child);
409
 
410
EXPORT_SYMBOL(device_initialize);
411
EXPORT_SYMBOL(device_add);
412
EXPORT_SYMBOL(device_register);
413
 
414
EXPORT_SYMBOL(device_del);
415
EXPORT_SYMBOL(device_unregister);
416
EXPORT_SYMBOL(device_unregister_wait);
417
EXPORT_SYMBOL(get_device);
418
EXPORT_SYMBOL(put_device);
419
 
420
EXPORT_SYMBOL(device_create_file);
421
EXPORT_SYMBOL(device_remove_file);