1 /*
2  * Copyright 2009-2017 Citrix Ltd and other contributors
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include <stdlib.h>
16 
17 #include <libxl.h>
18 #include <libxl_utils.h>
19 #include <libxlutil.h>
20 
21 #include "xl.h"
22 #include "xl_utils.h"
23 #include "xl_parse.h"
24 
main_usbctrl_attach(int argc,char ** argv)25 int main_usbctrl_attach(int argc, char **argv)
26 {
27     uint32_t domid;
28     int opt, rc = 0;
29     libxl_device_usbctrl usbctrl;
30 
31     SWITCH_FOREACH_OPT(opt, "", NULL, "usbctrl-attach", 1) {
32         /* No options */
33     }
34 
35     domid = find_domain(argv[optind++]);
36 
37     libxl_device_usbctrl_init(&usbctrl);
38 
39     for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) {
40         if (parse_usbctrl_config(&usbctrl, *argv))
41             return 1;
42     }
43 
44     rc = libxl_device_usbctrl_add(ctx, domid, &usbctrl, 0);
45     if (rc) {
46         fprintf(stderr, "libxl_device_usbctrl_add failed.\n");
47         rc = 1;
48     }
49 
50     libxl_device_usbctrl_dispose(&usbctrl);
51     return rc;
52 }
53 
main_usbctrl_detach(int argc,char ** argv)54 int main_usbctrl_detach(int argc, char **argv)
55 {
56     uint32_t domid;
57     int opt, devid, rc;
58     libxl_device_usbctrl usbctrl;
59 
60     SWITCH_FOREACH_OPT(opt, "", NULL, "usbctrl-detach", 2) {
61         /* No options */
62     }
63 
64     domid = find_domain(argv[optind]);
65     devid = atoi(argv[optind+1]);
66 
67     libxl_device_usbctrl_init(&usbctrl);
68     if (libxl_devid_to_device_usbctrl(ctx, domid, devid, &usbctrl)) {
69         fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
70         return 1;
71     }
72 
73     rc = libxl_device_usbctrl_remove(ctx, domid, &usbctrl, 0);
74     if (rc) {
75         fprintf(stderr, "libxl_device_usbctrl_remove failed.\n");
76         rc = 1;
77     }
78 
79     libxl_device_usbctrl_dispose(&usbctrl);
80     return rc;
81 
82 }
83 
main_usbdev_attach(int argc,char ** argv)84 int main_usbdev_attach(int argc, char **argv)
85 {
86     uint32_t domid;
87     int opt, rc;
88     libxl_device_usbdev usbdev;
89 
90     SWITCH_FOREACH_OPT(opt, "", NULL, "usbdev-attach", 2) {
91         /* No options */
92     }
93 
94     libxl_device_usbdev_init(&usbdev);
95 
96     domid = find_domain(argv[optind++]);
97 
98     for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) {
99         if (parse_usbdev_config(&usbdev, *argv))
100             return 1;
101     }
102 
103     rc = libxl_device_usbdev_add(ctx, domid, &usbdev, 0);
104     if (rc) {
105         fprintf(stderr, "libxl_device_usbdev_add failed.\n");
106         rc = 1;
107     }
108 
109     libxl_device_usbdev_dispose(&usbdev);
110     return rc;
111 }
112 
main_usbdev_detach(int argc,char ** argv)113 int main_usbdev_detach(int argc, char **argv)
114 {
115     uint32_t domid;
116     int ctrl, port;
117     int opt, rc = 1;
118     libxl_device_usbdev usbdev;
119 
120     SWITCH_FOREACH_OPT(opt, "", NULL, "usbdev-detach", 3) {
121         /* No options */
122     }
123 
124     domid = find_domain(argv[optind]);
125     ctrl = atoi(argv[optind+1]);
126     port = atoi(argv[optind+2]);
127 
128     if (argc - optind > 3) {
129         fprintf(stderr, "Invalid arguments.\n");
130         return 1;
131     }
132 
133     libxl_device_usbdev_init(&usbdev);
134     if (libxl_ctrlport_to_device_usbdev(ctx, domid, ctrl, port, &usbdev)) {
135         fprintf(stderr, "Unknown device at controller %d port %d.\n",
136                 ctrl, port);
137         return 1;
138     }
139 
140     rc = libxl_device_usbdev_remove(ctx, domid, &usbdev, 0);
141     if (rc) {
142         fprintf(stderr, "libxl_device_usbdev_remove failed.\n");
143         rc = 1;
144     }
145 
146     libxl_device_usbdev_dispose(&usbdev);
147     return rc;
148 }
149 
main_usblist(int argc,char ** argv)150 int main_usblist(int argc, char **argv)
151 {
152     uint32_t domid;
153     libxl_device_usbctrl *usbctrls;
154     libxl_usbctrlinfo usbctrlinfo;
155     int numctrl, i, j, opt;
156 
157     SWITCH_FOREACH_OPT(opt, "", NULL, "usb-list", 1) {
158         /* No options */
159     }
160 
161     domid = find_domain(argv[optind++]);
162 
163     if (argc > optind) {
164         fprintf(stderr, "Invalid arguments.\n");
165         exit(-1);
166     }
167 
168     usbctrls = libxl_device_usbctrl_list(ctx, domid, &numctrl);
169     if (!usbctrls) {
170         return 0;
171     }
172 
173     for (i = 0; i < numctrl; ++i) {
174         printf("%-6s %-12s %-3s %-5s %-7s %-5s\n",
175                 "Devid", "Type", "BE", "state", "usb-ver", "ports");
176 
177         libxl_usbctrlinfo_init(&usbctrlinfo);
178 
179         if (!libxl_device_usbctrl_getinfo(ctx, domid,
180                                 &usbctrls[i], &usbctrlinfo)) {
181             printf("%-6d %-12s %-3d %-5d %-7d %-5d\n",
182                     usbctrlinfo.devid,
183                     libxl_usbctrl_type_to_string(usbctrlinfo.type),
184                     usbctrlinfo.backend_id, usbctrlinfo.state,
185                     usbctrlinfo.version, usbctrlinfo.ports);
186 
187             for (j = 1; j <= usbctrlinfo.ports; j++) {
188                 libxl_device_usbdev usbdev;
189 
190                 libxl_device_usbdev_init(&usbdev);
191 
192                 printf("  Port %d:", j);
193 
194                 if (!libxl_ctrlport_to_device_usbdev(ctx, domid,
195                                                      usbctrlinfo.devid,
196                                                      j, &usbdev)) {
197                     printf(" Bus %03x Device %03x\n",
198                            usbdev.u.hostdev.hostbus,
199                            usbdev.u.hostdev.hostaddr);
200                 } else {
201                     printf("\n");
202                 }
203 
204                 libxl_device_usbdev_dispose(&usbdev);
205             }
206         }
207 
208         libxl_usbctrlinfo_dispose(&usbctrlinfo);
209     }
210 
211     libxl_device_usbctrl_list_free(usbctrls, numctrl);
212     return 0;
213 }
214 
215 
216 /*
217  * Local variables:
218  * mode: C
219  * c-basic-offset: 4
220  * indent-tabs-mode: nil
221  * End:
222  */
223