Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
  4 *               2005-2007 Takahiro Hirofuchi
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7#include <ctype.h>
  8#include <limits.h>
  9#include <stdint.h>
 10#include <stdio.h>
 11#include <stdlib.h>
 12#include <string.h>
 13
 14#include <getopt.h>
 15#include <unistd.h>
 16
 17#include "vhci_driver.h"
 18#include "usbip_common.h"
 19#include "usbip_network.h"
 20#include "usbip.h"
 21
 22static const char usbip_detach_usage_string[] =
 23	"usbip detach <args>\n"
 24	"    -p, --port=<port>    " USBIP_VHCI_DRV_NAME
 25	" port the device is on\n";
 26
 27void usbip_detach_usage(void)
 28{
 29	printf("usage: %s", usbip_detach_usage_string);
 30}
 31
 32static int detach_port(char *port)
 33{
 34	int ret = 0;
 35	uint8_t portnum;
 36	char path[PATH_MAX+1];
 37	int i;
 38	struct usbip_imported_device *idev;
 39	int found = 0;
 40
 41	unsigned int port_len = strlen(port);
 42
 43	for (unsigned int i = 0; i < port_len; i++)
 44		if (!isdigit(port[i])) {
 45			err("invalid port %s", port);
 46			return -1;
 47		}
 48
 49	portnum = atoi(port);
 50
 51	ret = usbip_vhci_driver_open();
 52	if (ret < 0) {
 53		err("open vhci_driver");
 54		return -1;
 55	}
 56
 57	/* check for invalid port */
 58	for (i = 0; i < vhci_driver->nports; i++) {
 59		idev = &vhci_driver->idev[i];
 60
 61		if (idev->port == portnum) {
 62			found = 1;
 63			if (idev->status != VDEV_ST_NULL)
 64				break;
 65			info("Port %d is already detached!\n", idev->port);
 66			goto call_driver_close;
 67		}
 68	}
 69
 70	if (!found) {
 71		err("Invalid port %s > maxports %d",
 72			port, vhci_driver->nports);
 73		goto call_driver_close;
 74	}
 75
 76	/* remove the port state file */
 
 77	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", portnum);
 78
 79	remove(path);
 80	rmdir(VHCI_STATE_PATH);
 81
 82	ret = usbip_vhci_detach_device(portnum);
 83	if (ret < 0) {
 84		ret = -1;
 85		err("Port %d detach request failed!\n", portnum);
 86		goto call_driver_close;
 87	}
 88	info("Port %d is now detached!\n", portnum);
 89
 90call_driver_close:
 
 
 
 91	usbip_vhci_driver_close();
 92
 93	return ret;
 94}
 95
 96int usbip_detach(int argc, char *argv[])
 97{
 98	static const struct option opts[] = {
 99		{ "port", required_argument, NULL, 'p' },
100		{ NULL, 0, NULL, 0 }
101	};
102	int opt;
103	int ret = -1;
104
105	for (;;) {
106		opt = getopt_long(argc, argv, "p:", opts, NULL);
107
108		if (opt == -1)
109			break;
110
111		switch (opt) {
112		case 'p':
113			ret = detach_port(optarg);
114			goto out;
115		default:
116			goto err_out;
117		}
118	}
119
120err_out:
121	usbip_detach_usage();
122out:
123	return ret;
124}
v4.6
 
  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 <ctype.h>
 20#include <limits.h>
 21#include <stdint.h>
 22#include <stdio.h>
 23#include <stdlib.h>
 24#include <string.h>
 25
 26#include <getopt.h>
 27#include <unistd.h>
 28
 29#include "vhci_driver.h"
 30#include "usbip_common.h"
 31#include "usbip_network.h"
 32#include "usbip.h"
 33
 34static const char usbip_detach_usage_string[] =
 35	"usbip detach <args>\n"
 36	"    -p, --port=<port>    " USBIP_VHCI_DRV_NAME
 37	" port the device is on\n";
 38
 39void usbip_detach_usage(void)
 40{
 41	printf("usage: %s", usbip_detach_usage_string);
 42}
 43
 44static int detach_port(char *port)
 45{
 46	int ret;
 47	uint8_t portnum;
 48	char path[PATH_MAX+1];
 
 
 
 49
 50	unsigned int port_len = strlen(port);
 51
 52	for (unsigned int i = 0; i < port_len; 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}