int ret;
int sg_fd;
const gchar *device;
- const gchar *sysfs_path;
struct udev *udev;
struct udev_device *udevice;
struct udev_device *udevice_usb_interface;
gchar *usb_interface_name;
size_t usb_interface_name_len;
FILE *f;
+ struct stat statbuf;
udev = NULL;
udevice = NULL;
ret = 1;
sg_fd = -1;
- if (argc != 3)
+ if (argc != 2)
{
usage ();
goto out;
}
device = argv[1];
- sysfs_path = argv[2];
+
+ if (stat (device, &statbuf) != 0)
+ {
+ g_printerr ("Error statting %s: %m\n", device);
+ goto out;
+ }
+ if (statbuf.st_rdev == 0)
+ {
+ g_printerr ("%s is not a special device file\n", device);
+ goto out;
+ }
sg_fd = sg_cmds_open_device (device, 1 /* read_only */, 1);
if (sg_fd < 0)
goto out;
}
+ g_printerr ("Detaching device: %s\n", device);
+
+ g_printerr ("SYNCHRONIZE CACHE: ");
if (sg_ll_sync_cache_10 (sg_fd, 0, /* sync_nv */
0, /* immed */
0, /* group */
0 /* verbose */
) != 0)
{
- g_printerr ("Error SYNCHRONIZE CACHE for %s: %m\n", device);
+ g_printerr ("FAILED: %m\n");
/* this is not a catastrophe, carry on */
+ g_printerr ("(Continuing despite SYNCHRONIZE CACHE failure.)\n");
+ }
+ else
+ {
+ g_printerr ("OK\n");
}
+ g_printerr ("STOP UNIT: ");
if (sg_ll_start_stop_unit (sg_fd, 0, /* immed */
0, /* pc_mod__fl_num */
0, /* power_cond */
0 /* verbose */
) != 0)
{
- g_printerr ("Error STOP UNIT for %s: %m\n", device);
+ g_printerr ("FAILED: %m\n");
goto out;
}
+ else
+ {
+ g_printerr ("OK\n");
+ }
/* OK, close the device */
sg_cmds_close_device (sg_fd);
goto out;
}
- udevice = udev_device_new_from_syspath (udev, sysfs_path);
+ udevice = udev_device_new_from_devnum (udev, 'b', statbuf.st_rdev);
if (udevice == NULL)
{
- g_printerr ("No udev device for %s: %m\n", sysfs_path);
+ g_printerr ("No udev device for device %s (devnum 0x%08x): %m\n", device, (gint) statbuf.st_rdev);
goto out;
}
udevice_usb_interface = udev_device_get_parent_with_subsystem_devtype (udevice, "usb", "usb_interface");
if (udevice_usb_interface == NULL)
{
- g_printerr ("No usb parent interface for %s: %m\n", sysfs_path);
+ g_printerr ("No usb parent interface for %s: %m\n", device);
goto out;
}
usb_interface_name = g_path_get_basename (udev_device_get_devpath (udevice_usb_interface));
usb_interface_name_len = strlen (usb_interface_name);
+ g_printerr ("Unbind USB interface driver: ");
unbind_path = g_strdup_printf ("%s/driver/unbind", udev_device_get_syspath (udevice_usb_interface));
f = fopen (unbind_path, "w");
if (f == NULL)
goto out;
}
fclose (f);
+ g_printerr ("OK\n");
+
/* If this is the only USB interface on the device, also suspend the
* USB device to e.g. make the lights on the device power off.
*/
ret = 0;
+ g_printerr ("Suspend USB Device: ");
+
udevice_usb_device = udev_device_get_parent_with_subsystem_devtype (udevice, "usb", "usb_device");
if (udevice_usb_device != NULL)
{
f = fopen (power_level_path, "w");
if (f == NULL)
{
- g_printerr ("Cannot open %s for writing: %m\n", unbind_path);
+ g_printerr ("FAILED: Cannot open %s for writing: %m\n", unbind_path);
}
else
{
if (fwrite (suspend_str, sizeof(char), strlen (suspend_str), f) < strlen (suspend_str))
{
- g_printerr ("Error writing %s to %s: %m\n", power_level_path, suspend_str);
+ g_printerr ("FAILED: Error writing %s to %s: %m\n", power_level_path, suspend_str);
+ }
+ else
+ {
+ g_printerr ("OK\n");
}
fclose (f);
}
}
+ else
+ {
+ g_printerr ("Skipping since multiple USB interfaces exist.\n");
+ }
}
out: