Subversion Repositories shark

Rev

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

Rev Author Line No. Line
455 giacomo 1
/*
2
 * bus.c - bus driver management
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/module.h>
17
#include <linux/errno.h>
18
#include <linux/init.h>
19
#include <linux/string.h>
20
#include "base.h"
21
 
22
#define to_dev(node) container_of(node,struct device,bus_list)
23
#define to_drv(node) container_of(node,struct device_driver,kobj.entry)
24
 
25
#define to_bus_attr(_attr) container_of(_attr,struct bus_attribute,attr)
26
#define to_bus(obj) container_of(obj,struct bus_type,subsys.kset.kobj)
27
 
28
/*
29
 * sysfs bindings for drivers
30
 */
31
 
32
#define to_drv_attr(_attr) container_of(_attr,struct driver_attribute,attr)
33
#define to_driver(obj) container_of(obj, struct device_driver, kobj)
34
 
35
 
36
static ssize_t
37
drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
38
{
39
        struct driver_attribute * drv_attr = to_drv_attr(attr);
40
        struct device_driver * drv = to_driver(kobj);
41
        ssize_t ret = 0;
42
 
43
        if (drv_attr->show)
44
                ret = drv_attr->show(drv,buf);
45
        return ret;
46
}
47
 
48
static ssize_t
49
drv_attr_store(struct kobject * kobj, struct attribute * attr,
50
               const char * buf, size_t count)
51
{
52
        struct driver_attribute * drv_attr = to_drv_attr(attr);
53
        struct device_driver * drv = to_driver(kobj);
54
        ssize_t ret = 0;
55
 
56
        if (drv_attr->store)
57
                ret = drv_attr->store(drv,buf,count);
58
        return ret;
59
}
60
 
61
static struct sysfs_ops driver_sysfs_ops = {
62
        .show   = drv_attr_show,
63
        .store  = drv_attr_store,
64
};
65
 
66
 
67
static void driver_release(struct kobject * kobj)
68
{
69
        struct device_driver * drv = to_driver(kobj);
70
        //up(&drv->unload_sem);
71
}
72
 
73
static struct kobj_type ktype_driver = {
74
        .sysfs_ops      = &driver_sysfs_ops,
75
        .release        = driver_release,
76
};
77
 
78
 
79
/*
80
 * sysfs bindings for buses
81
 */
82
 
83
 
84
static ssize_t
85
bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
86
{
87
        struct bus_attribute * bus_attr = to_bus_attr(attr);
88
        struct bus_type * bus = to_bus(kobj);
89
        ssize_t ret = 0;
90
 
91
        if (bus_attr->show)
92
                ret = bus_attr->show(bus,buf);
93
        return ret;
94
}
95
 
96
static ssize_t
97
bus_attr_store(struct kobject * kobj, struct attribute * attr,
98
               const char * buf, size_t count)
99
{
100
        struct bus_attribute * bus_attr = to_bus_attr(attr);
101
        struct bus_type * bus = to_bus(kobj);
102
        ssize_t ret = 0;
103
 
104
        if (bus_attr->store)
105
                ret = bus_attr->store(bus,buf,count);
106
        return ret;
107
}
108
 
109
static struct sysfs_ops bus_sysfs_ops = {
110
        .show   = bus_attr_show,
111
        .store  = bus_attr_store,
112
};
113
 
114
int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
115
{
116
        int error;
117
        if (get_bus(bus)) {
118
                error = 0;//sysfs_create_file(&bus->subsys.kset.kobj,&attr->attr);
119
                put_bus(bus);
120
        } else
121
                error = -EINVAL;
122
        return error;
123
}
124
 
125
void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
126
{
127
        if (get_bus(bus)) {
128
                //sysfs_remove_file(&bus->subsys.kset.kobj,&attr->attr);
129
                put_bus(bus);
130
        }
131
}
132
 
133
static struct kobj_type ktype_bus = {
134
        .sysfs_ops      = &bus_sysfs_ops,
135
 
136
};
137
 
138
decl_subsys(bus,&ktype_bus,NULL);
139
 
140
/**
141
 *      bus_for_each_dev - device iterator.
142
 *      @bus:   bus type.
143
 *      @start: device to start iterating from.
144
 *      @data:  data for the callback.
145
 *      @fn:    function to be called for each device.
146
 *
147
 *      Iterate over @bus's list of devices, and call @fn for each,
148
 *      passing it @data. If @start is not NULL, we use that device to
149
 *      begin iterating from.
150
 *
151
 *      We check the return of @fn each time. If it returns anything
152
 *      other than 0, we break out and return that value.
153
 *
154
 *      NOTE: The device that returns a non-zero value is not retained
155
 *      in any way, nor is its refcount incremented. If the caller needs
156
 *      to retain this data, it should do, and increment the reference
157
 *      count in the supplied callback.
158
 */
159
int bus_for_each_dev(struct bus_type * bus, struct device * start,
160
                     void * data, int (*fn)(struct device *, void *))
161
{
162
        struct list_head * head, * entry;
163
        int error = 0;
164
 
165
        if (!(bus = get_bus(bus)))
166
                return -EINVAL;
167
 
168
        head = start ? &start->bus_list : &bus->devices.list;
169
 
170
        //down_read(&bus->subsys.rwsem);
171
        list_for_each(entry,head) {
172
                struct device * dev = get_device(to_dev(entry));
173
                error = fn(dev,data);
174
                put_device(dev);
175
                if (error)
176
                        break;
177
        }
178
        //up_read(&bus->subsys.rwsem);
179
        put_bus(bus);
180
        return error;
181
}
182
 
183
/**
184
 *      bus_for_each_drv - driver iterator
185
 *      @bus:   bus we're dealing with.
186
 *      @start: driver to start iterating on.
187
 *      @data:  data to pass to the callback.
188
 *      @fn:    function to call for each driver.
189
 *
190
 *      This is nearly identical to the device iterator above.
191
 *      We iterate over each driver that belongs to @bus, and call
192
 *      @fn for each. If @fn returns anything but 0, we break out
193
 *      and return it. If @start is not NULL, we use it as the head
194
 *      of the list.
195
 *
196
 *      NOTE: we don't return the driver that returns a non-zero
197
 *      value, nor do we leave the reference count incremented for that
198
 *      driver. If the caller needs to know that info, it must set it
199
 *      in the callback. It must also be sure to increment the refcount
200
 *      so it doesn't disappear before returning to the caller.
201
 */
202
 
203
int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
204
                     void * data, int (*fn)(struct device_driver *, void *))
205
{
206
        struct list_head * head, * entry;
207
        int error = 0;
208
 
209
        if(!(bus = get_bus(bus)))
210
                return -EINVAL;
211
 
212
        head = start ? &start->kobj.entry : &bus->drivers.list;
213
 
214
        //down_read(&bus->subsys.rwsem);
215
        list_for_each(entry,head) {
216
                struct device_driver * drv = get_driver(to_drv(entry));
217
                error = fn(drv,data);
218
                put_driver(drv);
219
                if(error)
220
                        break;
221
        }
222
        //up_read(&bus->subsys.rwsem);
223
        put_bus(bus);
224
        return error;
225
}
226
 
227
/**
228
 *      device_bind_driver - bind a driver to one device.
229
 *      @dev:   device.
230
 *
231
 *      Allow manual attachment of a driver to a deivce.
232
 *      Caller must have already set @dev->driver.
233
 *
234
 *      Note that this does not modify the bus reference count
235
 *      nor take the bus's rwsem. Please verify those are accounted
236
 *      for before calling this. (It is ok to call with no other effort
237
 *      from a driver's probe() method.)
238
 */
239
 
240
void device_bind_driver(struct device * dev)
241
{
242
        pr_debug("bound device '%s' to driver '%s'\n",
243
                 dev->bus_id,dev->driver->name);
244
        list_add_tail(&dev->driver_list,&dev->driver->devices);
245
        //sysfs_create_link(&dev->driver->kobj,&dev->kobj,kobject_name(&dev->kobj));
246
}
247
 
248
 
249
/**
250
 *      bus_match - check compatibility between device & driver.
251
 *      @dev:   device.
252
 *      @drv:   driver.
253
 *
254
 *      First, we call the bus's match function, which should compare
255
 *      the device IDs the driver supports with the device IDs of the
256
 *      device. Note we don't do this ourselves because we don't know
257
 *      the format of the ID structures, nor what is to be considered
258
 *      a match and what is not.
259
 *     
260
 *      If we find a match, we call @drv->probe(@dev) if it exists, and
261
 *      call attach() above.
262
 */
263
static int bus_match(struct device * dev, struct device_driver * drv)
264
{
265
        int error = -ENODEV;
266
        if (dev->bus->match(dev,drv)) {
267
                dev->driver = drv;
268
                if (drv->probe) {
269
                        if ((error = drv->probe(dev))) {
270
                                dev->driver = NULL;
271
                                return error;
272
                        }
273
                }
274
                device_bind_driver(dev);
275
                error = 0;
276
        }
277
        return error;
278
}
279
 
280
 
281
/**
282
 *      device_attach - try to attach device to a driver.
283
 *      @dev:   device.
284
 *
285
 *      Walk the list of drivers that the bus has and call bus_match()
286
 *      for each pair. If a compatible pair is found, break out and return.
287
 */
288
static int device_attach(struct device * dev)
289
{
290
        struct bus_type * bus = dev->bus;
291
        struct list_head * entry;
292
        int error;
293
 
294
        if (dev->driver) {
295
                device_bind_driver(dev);
296
                return 1;
297
        }
298
 
299
        if (bus->match) {
300
                list_for_each(entry,&bus->drivers.list) {
301
                        struct device_driver * drv = to_drv(entry);
302
                        error = bus_match(dev,drv);
303
                        if (!error )  
304
                                /* success, driver matched */
305
                                return 1;
306
                        if (error != -ENODEV)
307
                                /* driver matched but the probe failed */
308
                                printk(KERN_WARNING
309
                                    "%s: probe of %s failed with error %d\n",
310
                                    drv->name, dev->bus_id, error);
311
                }
312
        }
313
 
314
        return 0;
315
}
316
 
317
 
318
/**
319
 *      driver_attach - try to bind driver to devices.
320
 *      @drv:   driver.
321
 *
322
 *      Walk the list of devices that the bus has on it and try to match
323
 *      the driver with each one.
324
 *      If bus_match() returns 0 and the @dev->driver is set, we've found
325
 *      a compatible pair.
326
 *
327
 *      Note that we ignore the -ENODEV error from bus_match(), since it's
328
 *      perfectly valid for a driver not to bind to any devices.
329
 */
330
void driver_attach(struct device_driver * drv)
331
{
332
        struct bus_type * bus = drv->bus;
333
        struct list_head * entry;
334
        int error;
335
 
336
        if (!bus->match)
337
                return;
338
 
339
        list_for_each(entry,&bus->devices.list) {
340
                struct device * dev = container_of(entry,struct device,bus_list);
341
                if (!dev->driver) {
342
                        error = bus_match(dev,drv);
343
                        if (error && (error != -ENODEV))
344
                                /* driver matched but the probe failed */
345
                                printk(KERN_WARNING
346
                                    "%s: probe of %s failed with error %d\n",
347
                                    drv->name, dev->bus_id, error);
348
                }
349
        }
350
}
351
 
352
 
353
/**
354
 *      device_release_driver - manually detach device from driver.
355
 *      @dev:   device.
356
 *
357
 *      Manually detach device from driver.
358
 *      Note that this is called without incrementing the bus
359
 *      reference count nor taking the bus's rwsem. Be sure that
360
 *      those are accounted for before calling this function.
361
 */
362
 
363
void device_release_driver(struct device * dev)
364
{
365
        struct device_driver * drv = dev->driver;
366
        if (drv) {
367
                //sysfs_remove_link(&drv->kobj,kobject_name(&dev->kobj));
368
                list_del_init(&dev->driver_list);
369
                //device_detach_shutdown(dev);
370
                if (drv->remove)
371
                        drv->remove(dev);
372
                dev->driver = NULL;
373
        }
374
}
375
 
376
 
377
/**
378
 *      driver_detach - detach driver from all devices it controls.
379
 *      @drv:   driver.
380
 */
381
 
382
static void driver_detach(struct device_driver * drv)
383
{
384
        struct list_head * entry, * next;
385
        list_for_each_safe(entry,next,&drv->devices) {
386
                struct device * dev = container_of(entry,struct device,driver_list);
387
                device_release_driver(dev);
388
        }
389
 
390
}
391
 
392
/**
393
 *      bus_add_device - add device to bus
394
 *      @dev:   device being added
395
 *
396
 *      - Add the device to its bus's list of devices.
397
 *      - Try to attach to driver.
398
 *      - Create link to device's physical location.
399
 */
400
int bus_add_device(struct device * dev)
401
{
402
        struct bus_type * bus = get_bus(dev->bus);
403
        int error = 0;
404
 
405
        if (bus) {
406
                //down_write(&dev->bus->subsys.rwsem);
407
                pr_debug("bus %s: add device %s\n",bus->name,dev->bus_id);
408
                list_add_tail(&dev->bus_list,&dev->bus->devices.list);
409
                device_attach(dev);
410
                //up_write(&dev->bus->subsys.rwsem);
411
                //sysfs_create_link(&bus->devices.kobj,&dev->kobj,dev->bus_id);
412
        }
413
        return error;
414
}
415
 
416
/**
417
 *      bus_remove_device - remove device from bus
418
 *      @dev:   device to be removed
419
 *
420
 *      - Remove symlink from bus's directory.
421
 *      - Delete device from bus's list.
422
 *      - Detach from its driver.
423
 *      - Drop reference taken in bus_add_device().
424
 */
425
void bus_remove_device(struct device * dev)
426
{
427
        if (dev->bus) {
428
                //sysfs_remove_link(&dev->bus->devices.kobj,dev->bus_id);
429
                //down_write(&dev->bus->subsys.rwsem);
430
                pr_debug("bus %s: remove device %s\n",dev->bus->name,dev->bus_id);
431
                device_release_driver(dev);
432
                list_del_init(&dev->bus_list);
433
                //up_write(&dev->bus->subsys.rwsem);
434
                put_bus(dev->bus);
435
        }
436
}
437
 
438
 
439
/**
440
 *      bus_add_driver - Add a driver to the bus.
441
 *      @drv:   driver.
442
 *
443
 */
444
int bus_add_driver(struct device_driver * drv)
445
{
446
        struct bus_type * bus = get_bus(drv->bus);
447
        int error = 0;
448
 
449
        if (bus) {
450
                pr_debug("bus %s: add driver %s\n",bus->name,drv->name);
451
                kobject_set_name(&drv->kobj,drv->name);
452
                drv->kobj.kset = &bus->drivers;
453
                if ((error = kobject_register(&drv->kobj))) {
454
                        put_bus(bus);
455
                        return error;
456
                }
457
 
458
                //down_write(&bus->subsys.rwsem);
459
                driver_attach(drv);
460
                //up_write(&bus->subsys.rwsem);
461
 
462
        }
463
        return error;
464
}
465
 
466
 
467
/**
468
 *      bus_remove_driver - delete driver from bus's knowledge.
469
 *      @drv:   driver.
470
 *
471
 *      Detach the driver from the devices it controls, and remove
472
 *      it from its bus's list of drivers. Finally, we drop the reference
473
 *      to the bus we took in bus_add_driver().
474
 */
475
 
476
void bus_remove_driver(struct device_driver * drv)
477
{
478
        if (drv->bus) {
479
                //down_write(&drv->bus->subsys.rwsem);
480
                pr_debug("bus %s: remove driver %s\n",drv->bus->name,drv->name);
481
                driver_detach(drv);
482
                //up_write(&drv->bus->subsys.rwsem);
483
                kobject_unregister(&drv->kobj);
484
                put_bus(drv->bus);
485
        }
486
}
487
 
488
 
489
/* Helper for bus_rescan_devices's iter */
490
static int bus_rescan_devices_helper(struct device *dev, void *data)
491
{
492
        int *count = data;
493
 
494
        if (!dev->driver && device_attach(dev))
495
                (*count)++;
496
 
497
        return 0;
498
}
499
 
500
 
501
/**
502
 *      bus_rescan_devices - rescan devices on the bus for possible drivers
503
 *      @bus:   the bus to scan.
504
 *
505
 *      This function will look for devices on the bus with no driver
506
 *      attached and rescan it against existing drivers to see if it
507
 *      matches any. Calls device_attach(). Returns the number of devices
508
 *      that were sucessfully bound to a driver.
509
 */
510
int bus_rescan_devices(struct bus_type * bus)
511
{
512
        int count = 0;
513
 
514
        bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
515
 
516
        return count;
517
}
518
 
519
 
520
struct bus_type * get_bus(struct bus_type * bus)
521
{
522
        return bus ? container_of(subsys_get(&bus->subsys),struct bus_type,subsys) : NULL;
523
}
524
 
525
void put_bus(struct bus_type * bus)
526
{
527
        subsys_put(&bus->subsys);
528
}
529
 
530
 
531
/**
532
 *      find_bus - locate bus by name.
533
 *      @name:  name of bus.
534
 *
535
 *      Call kset_find_obj() to iterate over list of buses to
536
 *      find a bus by name. Return bus if found.
537
 */
538
 
539
struct bus_type * find_bus(char * name)
540
{
541
        struct kobject * k = kset_find_obj(&bus_subsys.kset,name);
542
        return k ? to_bus(k) : NULL;
543
}
544
 
545
/**
546
 *      bus_register - register a bus with the system.
547
 *      @bus:   bus.
548
 *
549
 *      Once we have that, we registered the bus with the kobject
550
 *      infrastructure, then register the children subsystems it has:
551
 *      the devices and drivers that belong to the bus.
552
 */
553
int bus_register(struct bus_type * bus)
554
{
555
        kobject_set_name(&bus->subsys.kset.kobj,bus->name);
556
        subsys_set_kset(bus,bus_subsys);
557
        subsystem_register(&bus->subsys);
558
 
559
        kobject_set_name(&bus->devices.kobj, "devices");
560
        bus->devices.subsys = &bus->subsys;
561
        kset_register(&bus->devices);
562
 
563
        kobject_set_name(&bus->drivers.kobj, "drivers");
564
        bus->drivers.subsys = &bus->subsys;
565
        bus->drivers.ktype = &ktype_driver;
566
        kset_register(&bus->drivers);
567
 
568
        pr_debug("bus type '%s' registered\n",bus->name);
569
        return 0;
570
}
571
 
572
 
573
/**
574
 *      bus_unregister - remove a bus from the system
575
 *      @bus:   bus.
576
 *
577
 *      Unregister the child subsystems and the bus itself.
578
 *      Finally, we call put_bus() to release the refcount
579
 */
580
void bus_unregister(struct bus_type * bus)
581
{
582
        pr_debug("bus %s: unregistering\n",bus->name);
583
        kset_unregister(&bus->drivers);
584
        kset_unregister(&bus->devices);
585
        subsystem_unregister(&bus->subsys);
586
}
587
 
588
int __init buses_init(void)
589
{
590
        return subsystem_register(&bus_subsys);
591
}
592
 
593
 
594
EXPORT_SYMBOL(bus_for_each_dev);
595
EXPORT_SYMBOL(bus_for_each_drv);
596
 
597
EXPORT_SYMBOL(device_bind_driver);
598
EXPORT_SYMBOL(device_release_driver);
599
 
600
EXPORT_SYMBOL(bus_add_device);
601
EXPORT_SYMBOL(bus_remove_device);
602
EXPORT_SYMBOL(bus_register);
603
EXPORT_SYMBOL(bus_unregister);
604
EXPORT_SYMBOL(bus_rescan_devices);
605
EXPORT_SYMBOL(get_bus);
606
EXPORT_SYMBOL(put_bus);
607
EXPORT_SYMBOL(find_bus);
608
 
609
EXPORT_SYMBOL(bus_create_file);
610
EXPORT_SYMBOL(bus_remove_file);