2626#define to_drv (node ) container_of(node, struct device_driver, kobj.entry)
2727
2828
29- /**
30- * device_bind_driver - bind a driver to one device.
31- * @dev: device.
32- *
33- * Allow manual attachment of a driver to a device.
34- * Caller must have already set @dev->driver.
35- *
36- * Note that this does not modify the bus reference count
37- * nor take the bus's rwsem. Please verify those are accounted
38- * for before calling this. (It is ok to call with no other effort
39- * from a driver's probe() method.)
40- *
41- * This function must be called with @dev->sem held.
42- */
43- int device_bind_driver (struct device * dev )
29+ static void driver_bound (struct device * dev )
4430{
45- int ret ;
46-
4731 if (klist_node_attached (& dev -> knode_driver )) {
4832 printk (KERN_WARNING "%s: device %s already bound\n" ,
4933 __FUNCTION__ , kobject_name (& dev -> kobj ));
50- return 0 ;
34+ return ;
5135 }
5236
5337 pr_debug ("bound device '%s' to driver '%s'\n" ,
@@ -58,6 +42,12 @@ int device_bind_driver(struct device *dev)
5842 BUS_NOTIFY_BOUND_DRIVER , dev );
5943
6044 klist_add_tail (& dev -> knode_driver , & dev -> driver -> klist_devices );
45+ }
46+
47+ static int driver_sysfs_add (struct device * dev )
48+ {
49+ int ret ;
50+
6151 ret = sysfs_create_link (& dev -> driver -> kobj , & dev -> kobj ,
6252 kobject_name (& dev -> kobj ));
6353 if (ret == 0 ) {
@@ -70,6 +60,36 @@ int device_bind_driver(struct device *dev)
7060 return ret ;
7161}
7262
63+ static void driver_sysfs_remove (struct device * dev )
64+ {
65+ struct device_driver * drv = dev -> driver ;
66+
67+ if (drv ) {
68+ sysfs_remove_link (& drv -> kobj , kobject_name (& dev -> kobj ));
69+ sysfs_remove_link (& dev -> kobj , "driver" );
70+ }
71+ }
72+
73+ /**
74+ * device_bind_driver - bind a driver to one device.
75+ * @dev: device.
76+ *
77+ * Allow manual attachment of a driver to a device.
78+ * Caller must have already set @dev->driver.
79+ *
80+ * Note that this does not modify the bus reference count
81+ * nor take the bus's rwsem. Please verify those are accounted
82+ * for before calling this. (It is ok to call with no other effort
83+ * from a driver's probe() method.)
84+ *
85+ * This function must be called with @dev->sem held.
86+ */
87+ int device_bind_driver (struct device * dev )
88+ {
89+ driver_bound (dev );
90+ return driver_sysfs_add (dev );
91+ }
92+
7393struct stupid_thread_structure {
7494 struct device_driver * drv ;
7595 struct device * dev ;
@@ -90,30 +110,32 @@ static int really_probe(void *void_data)
90110 drv -> bus -> name , drv -> name , dev -> bus_id );
91111
92112 dev -> driver = drv ;
113+ if (driver_sysfs_add (dev )) {
114+ printk (KERN_ERR "%s: driver_sysfs_add(%s) failed\n" ,
115+ __FUNCTION__ , dev -> bus_id );
116+ goto probe_failed ;
117+ }
118+
93119 if (dev -> bus -> probe ) {
94120 ret = dev -> bus -> probe (dev );
95- if (ret ) {
96- dev -> driver = NULL ;
121+ if (ret )
97122 goto probe_failed ;
98- }
99123 } else if (drv -> probe ) {
100124 ret = drv -> probe (dev );
101- if (ret ) {
102- dev -> driver = NULL ;
125+ if (ret )
103126 goto probe_failed ;
104- }
105- }
106- if (device_bind_driver (dev )) {
107- printk (KERN_ERR "%s: device_bind_driver(%s) failed\n" ,
108- __FUNCTION__ , dev -> bus_id );
109- /* How does undo a ->probe? We're screwed. */
110127 }
128+
129+ driver_bound (dev );
111130 ret = 1 ;
112131 pr_debug ("%s: Bound Device %s to Driver %s\n" ,
113132 drv -> bus -> name , dev -> bus_id , drv -> name );
114133 goto done ;
115134
116135probe_failed :
136+ driver_sysfs_remove (dev );
137+ dev -> driver = NULL ;
138+
117139 if (ret == - ENODEV || ret == - ENXIO ) {
118140 /* Driver matched, but didn't support device
119141 * or device not found.
@@ -289,7 +311,7 @@ static void __device_release_driver(struct device * dev)
289311 drv = dev -> driver ;
290312 if (drv ) {
291313 get_driver (drv );
292- sysfs_remove_link ( & drv -> kobj , kobject_name ( & dev -> kobj ) );
314+ driver_sysfs_remove ( dev );
293315 sysfs_remove_link (& dev -> kobj , "driver" );
294316 klist_remove (& dev -> knode_driver );
295317
0 commit comments