linux/drivers/staging/usbip/userspace/src/usbip_detach.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
   3 *               2005-2007 Takahiro Hirofuchi
   4 *
   5 * This program is free software: you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation, either version 2 of the License, or
   8 * (at your option) any later version.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17 */
  18
  19#include <sysfs/libsysfs.h>
  20
  21#include <ctype.h>
  22#include <limits.h>
  23#include <stdint.h>
  24#include <stdio.h>
  25#include <stdlib.h>
  26#include <string.h>
  27
  28#include <getopt.h>
  29#include <unistd.h>
  30
  31#include "vhci_driver.h"
  32#include "usbip_common.h"
  33#include "usbip_network.h"
  34#include "usbip.h"
  35
  36static const char usbip_detach_usage_string[] =
  37        "usbip detach <args>\n"
  38        "    -p, --port=<port>    " USBIP_VHCI_DRV_NAME
  39        " port the device is on\n";
  40
  41void usbip_detach_usage(void)
  42{
  43        printf("usage: %s", usbip_detach_usage_string);
  44}
  45
  46static int detach_port(char *port)
  47{
  48        int ret;
  49        uint8_t portnum;
  50        char path[PATH_MAX+1];
  51
  52        for (unsigned int i = 0; i < strlen(port); i++)
  53                if (!isdigit(port[i])) {
  54                        err("invalid port %s", port);
  55                        return -1;
  56                }
  57
  58        /* check max port */
  59
  60        portnum = atoi(port);
  61
  62        /* remove the port state file */
  63
  64        snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum);
  65
  66        remove(path);
  67        rmdir(VHCI_STATE_PATH);
  68
  69        ret = usbip_vhci_driver_open();
  70        if (ret < 0) {
  71                err("open vhci_driver");
  72                return -1;
  73        }
  74
  75        ret = usbip_vhci_detach_device(portnum);
  76        if (ret < 0)
  77                return -1;
  78
  79        usbip_vhci_driver_close();
  80
  81        return ret;
  82}
  83
  84int usbip_detach(int argc, char *argv[])
  85{
  86        static const struct option opts[] = {
  87                { "port", required_argument, NULL, 'p' },
  88                { NULL, 0, NULL, 0 }
  89        };
  90        int opt;
  91        int ret = -1;
  92
  93        for (;;) {
  94                opt = getopt_long(argc, argv, "p:", opts, NULL);
  95
  96                if (opt == -1)
  97                        break;
  98
  99                switch (opt) {
 100                case 'p':
 101                        ret = detach_port(optarg);
 102                        goto out;
 103                default:
 104                        goto err_out;
 105                }
 106        }
 107
 108err_out:
 109        usbip_detach_usage();
 110out:
 111        return ret;
 112}
 113