Subversion Repositories shark

Rev

Rev 535 | Details | Compare with Previous | 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
{
459 giacomo 69
        //struct device_driver * drv = to_driver(kobj);
455 giacomo 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;
535 giacomo 266
 
455 giacomo 267
        if (dev->bus->match(dev,drv)) {
268
                dev->driver = drv;
269
                if (drv->probe) {
270
                        if ((error = drv->probe(dev))) {
271
                                dev->driver = NULL;
272
                                return error;
273
                        }
274
                }
275
                device_bind_driver(dev);
276
                error = 0;
277
        }
278
        return error;
279
}
280
 
281
 
282
/**
283
 *      device_attach - try to attach device to a driver.
284
 *      @dev:   device.
285
 *
286
 *      Walk the list of drivers that the bus has and call bus_match()
287
 *      for each pair. If a compatible pair is found, break out and return.
288
 */
289
static int device_attach(struct device * dev)
290
{
291
        struct bus_type * bus = dev->bus;
292
        struct list_head * entry;
293
        int error;
294
 
295
        if (dev->driver) {
296
                device_bind_driver(dev);
297
                return 1;
298
        }
299
 
300
        if (bus->match) {
301
                list_for_each(entry,&bus->drivers.list) {
302
                        struct device_driver * drv = to_drv(entry);
303
                        error = bus_match(dev,drv);
304
                        if (!error )  
305
                                /* success, driver matched */
306
                                return 1;
307
                        if (error != -ENODEV)
308
                                /* driver matched but the probe failed */
309
                                printk(KERN_WARNING
310
                                    "%s: probe of %s failed with error %d\n",
311
                                    drv->name, dev->bus_id, error);
312
                }
313
        }
314
 
315
        return 0;
316
}
317
 
318
 
319
/**
320
 *      driver_attach - try to bind driver to devices.
321
 *      @drv:   driver.
322
 *
323
 *      Walk the list of devices that the bus has on it and try to match
324
 *      the driver with each one.
325
 *      If bus_match() returns 0 and the @dev->driver is set, we've found
326
 *      a compatible pair.
327
 *
328
 *      Note that we ignore the -ENODEV error from bus_match(), since it's
329
 *      perfectly valid for a driver not to bind to any devices.
330
 */
331
void driver_attach(struct device_driver * drv)
332
{
333
        struct bus_type * bus = drv->bus;
334
        struct list_head * entry;
335
        int error;
336
 
337
        if (!bus->match)
338
                return;
339
 
340
        list_for_each(entry,&bus->devices.list) {
341
                struct device * dev = container_of(entry,struct device,bus_list);
342
                if (!dev->driver) {
343
                        error = bus_match(dev,drv);
344
                        if (error && (error != -ENODEV))
345
                                /* driver matched but the probe failed */
346
                                printk(KERN_WARNING
347
                                    "%s: probe of %s failed with error %d\n",
348
                                    drv->name, dev->bus_id, error);
349
                }
350
        }
351
}
352
 
353
 
354
/**
355
 *      device_release_driver - manually detach device from driver.
356
 *      @dev:   device.
357
 *
358
 *      Manually detach device from driver.
359
 *      Note that this is called without incrementing the bus
360
 *      reference count nor taking the bus's rwsem. Be sure that
361
 *      those are accounted for before calling this function.
362
 */
363
 
364
void device_release_driver(struct device * dev)
365
{
366
        struct device_driver * drv = dev->driver;
367
        if (drv) {
368
                //sysfs_remove_link(&drv->kobj,kobject_name(&dev->kobj));
369
                list_del_init(&dev->driver_list);
370
                //device_detach_shutdown(dev);
371
                if (drv->remove)
372
                        drv->remove(dev);
373
                dev->driver = NULL;
374
        }
375
}
376
 
377
 
378
/**
379
 *      driver_detach - detach driver from all devices it controls.
380
 *      @drv:   driver.
381
 */
382
 
383
static void driver_detach(struct device_driver * drv)
384
{
385
        struct list_head * entry, * next;
386
        list_for_each_safe(entry,next,&drv->devices) {
387
                struct device * dev = container_of(entry,struct device,driver_list);
388
                device_release_driver(dev);
389
        }
390
 
391
}
392
 
393
/**
394
 *      bus_add_device - add device to bus
395
 *      @dev:   device being added
396
 *
397
 *      - Add the device to its bus's list of devices.
398
 *      - Try to attach to driver.
399
 *      - Create link to device's physical location.
400
 */
401
int bus_add_device(struct device * dev)
402
{
403
        struct bus_type * bus = get_bus(dev->bus);
404
        int error = 0;
405
 
406
        if (bus) {
407
                //down_write(&dev->bus->subsys.rwsem);
408
                pr_debug("bus %s: add device %s\n",bus->name,dev->bus_id);
409
                list_add_tail(&dev->bus_list,&dev->bus->devices.list);
410
                device_attach(dev);
411
                //up_write(&dev->bus->subsys.rwsem);
412
                //sysfs_create_link(&bus->devices.kobj,&dev->kobj,dev->bus_id);
413
        }
414
        return error;
415
}
416
 
417
/**
418
 *      bus_remove_device - remove device from bus
419
 *      @dev:   device to be removed
420
 *
421
 *      - Remove symlink from bus's directory.
422
 *      - Delete device from bus's list.
423
 *      - Detach from its driver.
424
 *      - Drop reference taken in bus_add_device().
425
 */
426
void bus_remove_device(struct device * dev)
427
{
428
        if (dev->bus) {
429
                //sysfs_remove_link(&dev->bus->devices.kobj,dev->bus_id);
430
                //down_write(&dev->bus->subsys.rwsem);
431
                pr_debug("bus %s: remove device %s\n",dev->bus->name,dev->bus_id);
432
                device_release_driver(dev);
433
                list_del_init(&dev->bus_list);
434
                //up_write(&dev->bus->subsys.rwsem);
435
                put_bus(dev->bus);
436
        }
437
}
438
 
439
 
440
/**
441
 *      bus_add_driver - Add a driver to the bus.
442
 *      @drv:   driver.
443
 *
444
 */
445
int bus_add_driver(struct device_driver * drv)
446
{
447
        struct bus_type * bus = get_bus(drv->bus);
448
        int error = 0;
449
 
450
        if (bus) {
451
                pr_debug("bus %s: add driver %s\n",bus->name,drv->name);
452
                kobject_set_name(&drv->kobj,drv->name);
453
                drv->kobj.kset = &bus->drivers;
454
                if ((error = kobject_register(&drv->kobj))) {
455
                        put_bus(bus);
456
                        return error;
457
                }
458
 
459
                //down_write(&bus->subsys.rwsem);
460
                driver_attach(drv);
461
                //up_write(&bus->subsys.rwsem);
462
 
463
        }
464
        return error;
465
}
466
 
467
 
468
/**
469
 *      bus_remove_driver - delete driver from bus's knowledge.
470
 *      @drv:   driver.
471
 *
472
 *      Detach the driver from the devices it controls, and remove
473
 *      it from its bus's list of drivers. Finally, we drop the reference
474
 *      to the bus we took in bus_add_driver().
475
 */
476
 
477
void bus_remove_driver(struct device_driver * drv)
478
{
479
        if (drv->bus) {
480
                //down_write(&drv->bus->subsys.rwsem);
481
                pr_debug("bus %s: remove driver %s\n",drv->bus->name,drv->name);
482
                driver_detach(drv);
483
                //up_write(&drv->bus->subsys.rwsem);
484
                kobject_unregister(&drv->kobj);
485
                put_bus(drv->bus);
486
        }
487
}
488
 
489
 
490
/* Helper for bus_rescan_devices's iter */
491
static int bus_rescan_devices_helper(struct device *dev, void *data)
492
{
493
        int *count = data;
494
 
495
        if (!dev->driver && device_attach(dev))
496
                (*count)++;
497
 
498
        return 0;
499
}
500
 
501
 
502
/**
503
 *      bus_rescan_devices - rescan devices on the bus for possible drivers
504
 *      @bus:   the bus to scan.
505
 *
506
 *      This function will look for devices on the bus with no driver
507
 *      attached and rescan it against existing drivers to see if it
508
 *      matches any. Calls device_attach(). Returns the number of devices
509
 *      that were sucessfully bound to a driver.
510
 */
511
int bus_rescan_devices(struct bus_type * bus)
512
{
513
        int count = 0;
514
 
515
        bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
516
 
517
        return count;
518
}
519
 
520
 
521
struct bus_type * get_bus(struct bus_type * bus)
522
{
523
        return bus ? container_of(subsys_get(&bus->subsys),struct bus_type,subsys) : NULL;
524
}
525
 
526
void put_bus(struct bus_type * bus)
527
{
528
        subsys_put(&bus->subsys);
529
}
530
 
531
 
532
/**
533
 *      find_bus - locate bus by name.
534
 *      @name:  name of bus.
535
 *
536
 *      Call kset_find_obj() to iterate over list of buses to
537
 *      find a bus by name. Return bus if found.
538
 */
539
 
540
struct bus_type * find_bus(char * name)
541
{
542
        struct kobject * k = kset_find_obj(&bus_subsys.kset,name);
543
        return k ? to_bus(k) : NULL;
544
}
545
 
546
/**
547
 *      bus_register - register a bus with the system.
548
 *      @bus:   bus.
549
 *
550
 *      Once we have that, we registered the bus with the kobject
551
 *      infrastructure, then register the children subsystems it has:
552
 *      the devices and drivers that belong to the bus.
553
 */
554
int bus_register(struct bus_type * bus)
555
{
556
        kobject_set_name(&bus->subsys.kset.kobj,bus->name);
557
        subsys_set_kset(bus,bus_subsys);
558
        subsystem_register(&bus->subsys);
559
 
560
        kobject_set_name(&bus->devices.kobj, "devices");
561
        bus->devices.subsys = &bus->subsys;
562
        kset_register(&bus->devices);
563
 
564
        kobject_set_name(&bus->drivers.kobj, "drivers");
565
        bus->drivers.subsys = &bus->subsys;
566
        bus->drivers.ktype = &ktype_driver;
567
        kset_register(&bus->drivers);
568
 
569
        pr_debug("bus type '%s' registered\n",bus->name);
570
        return 0;
571
}
572
 
573
 
574
/**
575
 *      bus_unregister - remove a bus from the system
576
 *      @bus:   bus.
577
 *
578
 *      Unregister the child subsystems and the bus itself.
579
 *      Finally, we call put_bus() to release the refcount
580
 */
581
void bus_unregister(struct bus_type * bus)
582
{
583
        pr_debug("bus %s: unregistering\n",bus->name);
584
        kset_unregister(&bus->drivers);
585
        kset_unregister(&bus->devices);
586
        subsystem_unregister(&bus->subsys);
587
}
588
 
589
int __init buses_init(void)
590
{
591
        return subsystem_register(&bus_subsys);
592
}
593
 
594
 
595
EXPORT_SYMBOL(bus_for_each_dev);
596
EXPORT_SYMBOL(bus_for_each_drv);
597
 
598
EXPORT_SYMBOL(device_bind_driver);
599
EXPORT_SYMBOL(device_release_driver);
600
 
601
EXPORT_SYMBOL(bus_add_device);
602
EXPORT_SYMBOL(bus_remove_device);
603
EXPORT_SYMBOL(bus_register);
604
EXPORT_SYMBOL(bus_unregister);
605
EXPORT_SYMBOL(bus_rescan_devices);
606
EXPORT_SYMBOL(get_bus);
607
EXPORT_SYMBOL(put_bus);
608
EXPORT_SYMBOL(find_bus);
609
 
610
EXPORT_SYMBOL(bus_create_file);
611
EXPORT_SYMBOL(bus_remove_file);