if (bus) {
                pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
-               device_attach(dev);
+               error = device_attach(dev);
                klist_add_tail(&bus->klist_devices, &dev->knode_bus);
-               device_add_attrs(bus, dev);
-               sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
-               sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
+               if (error >= 0)
+                       error = device_add_attrs(bus, dev);
+               if (!error) {
+                       sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
+                       sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
+               }
        }
        return error;
 }
 {
        int *count = data;
 
-       if (!dev->driver && device_attach(dev))
+       if (!dev->driver && (device_attach(dev) > 0))
                (*count)++;
 
        return 0;
 
  *     driver_probe_device() for each pair. If a compatible
  *     pair is found, break out and return.
  *
- *     Returns 1 if the device was bound to a driver; 0 otherwise.
+ *     Returns 1 if the device was bound to a driver;
+ *     0 if no matching device was found; error code otherwise.
  */
 int device_attach(struct device * dev)
 {