Usb "отключение включение питания" в Linux

Материал из support.qbpro.ru

Linux управление питанием на usb

  • и так, для начало необходимо собрать или взять ядро с включенным режимом CONFIG_USB_SUSPEND
  • теперь можно "выключать или включать" - на самом деле переводим в режим засыпания или выводим из него, командами:
#выключаем...
sh -c "echo suspend > /sys/bus/usb/devices/5-1/power/level"
#и включаем...
sh -c "echo auto > /sys/bus/usb/devices/5-1/power/level"
  • второй вариант..
$ echo 0 > /sys/bus/usb/devices/8-1/power/autosuspend
$ echo auto > /sys/bus/usb/devices/8-1/power/level

Для включения:
$ echo 2 > /sys/bus/usb/devices/8-1/power/autosuspend
$ echo on > /sys/bus/usb/devices/8-1/power/level

есть еще решение через usbreset: код на с

 /* usbreset -- send a USB port reset to a USB device */

 /*

 http://marc.info/?l=linux-usb-users&m=116827193506484&w=2

 and needs mounted usbfs filesystem

	  sudo mount -t usbfs none /proc/bus/usb

 There is a way to suspend a USB device.  In order to use it,
 you must have a kernel with CONFIG_PM_SYSFS_DEPRECATED turned on.  To
 suspend a device, do (as root):

	  echo -n 2 >/sys/bus/usb/devices/.../power/state

 where the "..." is the ID for your device.  To unsuspend, do the same
 thing but with a "0" instead of the "2" above.

 Note that this mechanism is slated to be removed from the kernel within
 the next year.  Hopefully some other mechanism will take its place.

 > To reset a
 > device?

 Here's a program to do it.  You invoke it as either

	  usbreset /proc/bus/usb/BBB/DDD
 or
	  usbreset /dev/usbB.D

 depending on how your system is set up, where BBB and DDD are the bus and
 device address numbers.

 Alan Stern

 */

 #include <stdio.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/ioctl.h>

 #include <linux/usbdevice_fs.h>


 int main(int argc, char **argv)
 {
	  const char *filename;
	  int fd;
	  int rc;

	  if (argc != 2) {
		  fprintf(stderr, "Usage: usbreset device-filename\n");
		  return 1;
	  }
	  filename = argv[1];

	  fd = open(filename, O_WRONLY);
	  if (fd < 0) {
		  perror("Error opening output file");
		  return 1;
	  }

	  printf("Resetting USB device %s\n", filename);
	  rc = ioctl(fd, USBDEVFS_RESET, 0);
	  if (rc < 0) {
		  perror("Error in ioctl");
		  return 1;
	  }
	  printf("Reset successful\n");

	  close(fd);
	  return 0;
 }

создаем файл usbreset.c собираем командой:

make usbreset.c -o usbreset

использование:

смотрим устройство lsusb

# lsusb
 Bus 007 Device 002: ID 0458:003a KYE Systems Corp. (Mouse Systems) NetScroll+ Mini Traveler / Genius NetScroll 120
 Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
 Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
 Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
 Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
 Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
 Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
 Bus 001 Device 005: ID 5986:0102 Acer, Inc Crystal Eye Webcam
 Bus 001 Device 004: ID 0bda:0158 Realtek Semiconductor Corp. USB 2.0 multicard reader
 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

сбрасываем например мышь:

# ./usbreset /dev/bus/usb/007/002
 Resetting USB device /dev/bus/usb/007/002
 Reset successful