Thank you Alan.

Following your advice, I rewrote the program and it worked just as I expected. I really appreciate your help.

I attached my program, though it's not sophisticated.

Thank you again, Yoichi.



Sorry, it's my mistake, not yours -- I gave you incomplete advice.

What you need to do is something like this:

        struct usbdev_ioctl ctrl;

        ctrl.ioctl_code = USBDEVFS_DISCONNECT;
        ctrl.ifno = interface number of the interface bound to
                        the usb-storage driver,
                        look in /proc/bus/usb/devices to see what it is

        p = &ctrl;
        if (ioctl(d, USBDEVFS_IOCTL, p) == ERR) ...

Alan Stern


/* usbdetach.c - send an ioctl request to disconnect or re-connect a usb device.
 *
 * Synopsis: usbdetach [-c | -d] path ifno
 *
 * Description:
 *  Disconnect or re-connect a usb device. The default behavior is "disconnect".
 *
 * "path" is the path to the device file of the device you want to
 * disconnect. Usually it takes the form of /proc/bus/usb/BBB/DDD, where
 * BBB is the bus number and DDD is the device number.
 *
 * "ifno" is the interface number of the interface you want to disconnect.
 * See /proc/bus/usb to find your interface number.
 *
 * -c option tells the program to connect the device again.
 *
 * -d option tells the program to disconnect the device.
 *
 *      Copyright (C) 2004  Yoichi Aso (aso-pc(AT)granite.phys.s.u-tokyo.ac.jp)
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */



#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#define ERR -1

void usage()
{
  fprintf(stderr, "Usage: usbdetach [-c | -d] path ifno\n ");
  exit(1);
}

int main(argc, argv)
     int argc;
     char *argv[];
{
  struct usbdevfs_ioctl ctrl;
  struct usbdevfs_ioctl *p;
  int c,d;
  char *path;
  int ifnum;
  int connect = 0;
  extern int optind;

  // Parse options

  while((c = getopt(argc, argv, "cd")) != ERR)
    {
      switch(c)
	{
	case 'c':
	  connect = 1;
	  break;
	case 'd':
	  connect = 0;
	  break;
	default:
	  usage();
	}
    }

  if ((argc - optind) < 2)
    usage();

  path = argv[optind];
  ifnum = atoi(argv[optind]);

  // Prepare for the ioctl.

  if(connect)
    ctrl.ioctl_code = USBDEVFS_CONNECT;
  else
    ctrl.ioctl_code = USBDEVFS_DISCONNECT;

  ctrl.ifno = ifnum;
  p = &ctrl;

  // Open the device file.

  if((d = open(path, O_RDWR | O_NONBLOCK)) == ERR)
    {
      perror("open");
      exit(1);
    }

  // ioctl.

  if(ioctl(d, USBDEVFS_IOCTL, p) == ERR)
    {
      perror("ioctl");
      exit(1);
    }

  close(d);
  return 0;
}

Reply via email to