usbconfig.c (86a06bed757ecac4e7f37a439117bd51de6a1f5c) usbconfig.c (eabe30fc9c3f2d6a3bc2fdcce18a0b9543b6c96e)
1/* $FreeBSD$ */
2/*-
1/* $FreeBSD$ */
2/*-
3 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the

--- 15 unchanged lines hidden (view full) ---

27#include <stdio.h>
28#include <stdlib.h>
29#include <stdint.h>
30#include <err.h>
31#include <string.h>
32#include <pwd.h>
33#include <grp.h>
34#include <errno.h>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the

--- 15 unchanged lines hidden (view full) ---

27#include <stdio.h>
28#include <stdlib.h>
29#include <stdint.h>
30#include <err.h>
31#include <string.h>
32#include <pwd.h>
33#include <grp.h>
34#include <errno.h>
35#include <ctype.h>
36
37#include <libusb20_desc.h>
38#include <libusb20.h>
39
40#include "dump.h"
41
42struct options {
43 const char *quirkname;
35
36#include <libusb20_desc.h>
37#include <libusb20.h>
38
39#include "dump.h"
40
41struct options {
42 const char *quirkname;
44 void *buffer;
45 int template;
46 gid_t gid;
47 uid_t uid;
48 mode_t mode;
49 uint32_t got_any;
43 gid_t gid;
44 uid_t uid;
45 mode_t mode;
46 uint32_t got_any;
50 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
51 uint16_t bus;
52 uint16_t addr;
53 uint16_t iface;
54 uint16_t vid;
55 uint16_t pid;
56 uint16_t lo_rev; /* inclusive */
57 uint16_t hi_rev; /* inclusive */
47 uint16_t bus;
48 uint16_t addr;
49 uint16_t iface;
50 uint16_t vid;
51 uint16_t pid;
52 uint16_t lo_rev; /* inclusive */
53 uint16_t hi_rev; /* inclusive */
58 uint8_t string_index;
59 uint8_t config_index;
60 uint8_t alt_index;
61 uint8_t got_list:1;
62 uint8_t got_bus:1;
63 uint8_t got_addr:1;
64 uint8_t got_iface:1;
65 uint8_t got_set_config:1;
66 uint8_t got_set_alt:1;
54 uint8_t config_index;
55 uint8_t alt_index;
56 uint8_t got_list:1;
57 uint8_t got_bus:1;
58 uint8_t got_addr:1;
59 uint8_t got_iface:1;
60 uint8_t got_set_config:1;
61 uint8_t got_set_alt:1;
67 uint8_t got_set_template:1;
68 uint8_t got_get_template:1;
62 uint8_t got_set_owner:1;
63 uint8_t got_set_perm:1;
69 uint8_t got_suspend:1;
70 uint8_t got_resume:1;
71 uint8_t got_reset:1;
72 uint8_t got_power_off:1;
73 uint8_t got_power_save:1;
74 uint8_t got_power_on:1;
75 uint8_t got_dump_device_quirks:1;
76 uint8_t got_dump_quirk_names:1;
77 uint8_t got_dump_device_desc:1;
78 uint8_t got_dump_curr_config:1;
79 uint8_t got_dump_all_config:1;
80 uint8_t got_dump_info:1;
64 uint8_t got_suspend:1;
65 uint8_t got_resume:1;
66 uint8_t got_reset:1;
67 uint8_t got_power_off:1;
68 uint8_t got_power_save:1;
69 uint8_t got_power_on:1;
70 uint8_t got_dump_device_quirks:1;
71 uint8_t got_dump_quirk_names:1;
72 uint8_t got_dump_device_desc:1;
73 uint8_t got_dump_curr_config:1;
74 uint8_t got_dump_all_config:1;
75 uint8_t got_dump_info:1;
81 uint8_t got_show_iface_driver:1;
76 uint8_t got_dump_access:1;
82 uint8_t got_remove_device_quirk:1;
83 uint8_t got_add_device_quirk:1;
77 uint8_t got_remove_device_quirk:1;
78 uint8_t got_add_device_quirk:1;
84 uint8_t got_remove_quirk:1;
85 uint8_t got_add_quirk:1;
86 uint8_t got_dump_string:1;
87 uint8_t got_do_request:1;
88};
89
90struct token {
91 const char *name;
92 uint8_t value;
93 uint8_t narg;
94};
95
96enum {
97 T_UNIT,
98 T_ADDR,
79};
80
81struct token {
82 const char *name;
83 uint8_t value;
84 uint8_t narg;
85};
86
87enum {
88 T_UNIT,
89 T_ADDR,
99 T_UGEN,
100 T_IFACE,
101 T_SET_CONFIG,
102 T_SET_ALT,
90 T_IFACE,
91 T_SET_CONFIG,
92 T_SET_ALT,
103 T_SET_TEMPLATE,
104 T_GET_TEMPLATE,
93 T_SET_OWNER,
94 T_SET_PERM,
105 T_ADD_DEVICE_QUIRK,
106 T_REMOVE_DEVICE_QUIRK,
95 T_ADD_DEVICE_QUIRK,
96 T_REMOVE_DEVICE_QUIRK,
107 T_ADD_QUIRK,
108 T_REMOVE_QUIRK,
109 T_SHOW_IFACE_DRIVER,
110 T_DUMP_QUIRK_NAMES,
111 T_DUMP_DEVICE_QUIRKS,
112 T_DUMP_DEVICE_DESC,
113 T_DUMP_CURR_CONFIG_DESC,
114 T_DUMP_ALL_CONFIG_DESC,
97 T_DUMP_QUIRK_NAMES,
98 T_DUMP_DEVICE_QUIRKS,
99 T_DUMP_DEVICE_DESC,
100 T_DUMP_CURR_CONFIG_DESC,
101 T_DUMP_ALL_CONFIG_DESC,
115 T_DUMP_STRING,
102 T_DUMP_ACCESS,
116 T_DUMP_INFO,
117 T_SUSPEND,
118 T_RESUME,
119 T_POWER_OFF,
120 T_POWER_SAVE,
121 T_POWER_ON,
122 T_RESET,
123 T_LIST,
103 T_DUMP_INFO,
104 T_SUSPEND,
105 T_RESUME,
106 T_POWER_OFF,
107 T_POWER_SAVE,
108 T_POWER_ON,
109 T_RESET,
110 T_LIST,
124 T_DO_REQUEST,
125};
126
127static struct options options;
128
129static const struct token token[] = {
130 {"-u", T_UNIT, 1},
131 {"-a", T_ADDR, 1},
111};
112
113static struct options options;
114
115static const struct token token[] = {
116 {"-u", T_UNIT, 1},
117 {"-a", T_ADDR, 1},
132 {"-d", T_UGEN, 1},
133 {"-i", T_IFACE, 1},
134 {"set_config", T_SET_CONFIG, 1},
135 {"set_alt", T_SET_ALT, 1},
118 {"-i", T_IFACE, 1},
119 {"set_config", T_SET_CONFIG, 1},
120 {"set_alt", T_SET_ALT, 1},
136 {"set_template", T_SET_TEMPLATE, 1},
137 {"get_template", T_GET_TEMPLATE, 0},
121 {"set_owner", T_SET_OWNER, 1},
122 {"set_perm", T_SET_PERM, 1},
138 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
139 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
123 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
124 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
140 {"add_quirk", T_ADD_QUIRK, 1},
141 {"remove_quirk", T_REMOVE_QUIRK, 1},
142 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
143 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
144 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
145 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
146 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
125 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
126 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
127 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
128 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
129 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
147 {"dump_string", T_DUMP_STRING, 1},
130 {"dump_access", T_DUMP_ACCESS, 0},
148 {"dump_info", T_DUMP_INFO, 0},
131 {"dump_info", T_DUMP_INFO, 0},
149 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
150 {"suspend", T_SUSPEND, 0},
151 {"resume", T_RESUME, 0},
152 {"power_off", T_POWER_OFF, 0},
153 {"power_save", T_POWER_SAVE, 0},
154 {"power_on", T_POWER_ON, 0},
155 {"reset", T_RESET, 0},
156 {"list", T_LIST, 0},
132 {"suspend", T_SUSPEND, 0},
133 {"resume", T_RESUME, 0},
134 {"power_off", T_POWER_OFF, 0},
135 {"power_save", T_POWER_SAVE, 0},
136 {"power_on", T_POWER_ON, 0},
137 {"reset", T_RESET, 0},
138 {"list", T_LIST, 0},
157 {"do_request", T_DO_REQUEST, 5},
158};
159
160static void
161be_dev_remove_quirk(struct libusb20_backend *pbe,
162 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
163 const char *str)
164{
165 struct libusb20_quirk q;
139};
140
141static void
142be_dev_remove_quirk(struct libusb20_backend *pbe,
143 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
144 const char *str)
145{
146 struct libusb20_quirk q;
166 int error;
147 int err;
167
168 memset(&q, 0, sizeof(q));
169
170 q.vid = vid;
171 q.pid = pid;
172 q.bcdDeviceLow = lorev;
173 q.bcdDeviceHigh = hirev;
174 strlcpy(q.quirkname, str, sizeof(q.quirkname));
175
148
149 memset(&q, 0, sizeof(q));
150
151 q.vid = vid;
152 q.pid = pid;
153 q.bcdDeviceLow = lorev;
154 q.bcdDeviceHigh = hirev;
155 strlcpy(q.quirkname, str, sizeof(q.quirkname));
156
176 error = libusb20_be_remove_dev_quirk(pbe, &q);
177 if (error) {
178 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
157 err = libusb20_be_remove_dev_quirk(pbe, &q);
158 if (err) {
159 printf("Removing quirk '%s' failed, continuing.\n", str);
179 }
180 return;
181}
182
183static void
184be_dev_add_quirk(struct libusb20_backend *pbe,
185 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
186 const char *str)
187{
188 struct libusb20_quirk q;
160 }
161 return;
162}
163
164static void
165be_dev_add_quirk(struct libusb20_backend *pbe,
166 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
167 const char *str)
168{
169 struct libusb20_quirk q;
189 int error;
170 int err;
190
191 memset(&q, 0, sizeof(q));
192
193 q.vid = vid;
194 q.pid = pid;
195 q.bcdDeviceLow = lorev;
196 q.bcdDeviceHigh = hirev;
197 strlcpy(q.quirkname, str, sizeof(q.quirkname));
198
171
172 memset(&q, 0, sizeof(q));
173
174 q.vid = vid;
175 q.pid = pid;
176 q.bcdDeviceLow = lorev;
177 q.bcdDeviceHigh = hirev;
178 strlcpy(q.quirkname, str, sizeof(q.quirkname));
179
199 error = libusb20_be_add_dev_quirk(pbe, &q);
200 if (error) {
201 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
180 err = libusb20_be_add_dev_quirk(pbe, &q);
181 if (err) {
182 printf("Adding quirk '%s' failed, continuing.\n", str);
202 }
203 return;
204}
205
206static uint8_t
207get_token(const char *str, uint8_t narg)
208{
209 uint8_t n;
210
211 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
212 if (strcasecmp(str, token[n].name) == 0) {
213 if (token[n].narg > narg) {
183 }
184 return;
185}
186
187static uint8_t
188get_token(const char *str, uint8_t narg)
189{
190 uint8_t n;
191
192 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
193 if (strcasecmp(str, token[n].name) == 0) {
194 if (token[n].narg > narg) {
214 /* too few arguments */
195 /* to little arguments */
215 break;
216 }
217 return (token[n].value);
218 }
219 }
220 return (0 - 1);
221}
222

--- 9 unchanged lines hidden (view full) ---

232 err(1, "%s", name);
233 }
234 if (*ep != '\0') {
235 errx(1, "%s: illegal %s name", name, type);
236 }
237 return (val);
238}
239
196 break;
197 }
198 return (token[n].value);
199 }
200 }
201 return (0 - 1);
202}
203

--- 9 unchanged lines hidden (view full) ---

213 err(1, "%s", name);
214 }
215 if (*ep != '\0') {
216 errx(1, "%s: illegal %s name", name, type);
217 }
218 return (val);
219}
220
240static int
241get_int(const char *s)
221static gid_t
222a_gid(const char *s)
242{
223{
243 int val;
224 struct group *gr;
225
226 if (*s == '\0') {
227 /* empty group ID */
228 return ((gid_t)-1);
229 }
230 return ((gr = getgrnam(s)) ? gr->gr_gid : num_id(s, "group"));
231}
232
233static uid_t
234a_uid(const char *s)
235{
236 struct passwd *pw;
237
238 if (*s == '\0') {
239 /* empty user ID */
240 return ((uid_t)-1);
241 }
242 return ((pw = getpwnam(s)) ? pw->pw_uid : num_id(s, "user"));
243}
244
245static mode_t
246a_mode(const char *s)
247{
248 uint16_t val;
244 char *ep;
245
246 errno = 0;
249 char *ep;
250
251 errno = 0;
247 val = strtoul(s, &ep, 0);
252 val = strtoul(s, &ep, 8);
248 if (errno) {
249 err(1, "%s", s);
250 }
251 if (*ep != '\0') {
253 if (errno) {
254 err(1, "%s", s);
255 }
256 if (*ep != '\0') {
252 errx(1, "illegal number: %s", s);
257 errx(1, "illegal permissions: %s", s);
253 }
254 return val;
255}
256
257static void
258 }
259 return val;
260}
261
262static void
258duplicate_option(const char *ptr)
259{
260 fprintf(stderr, "Syntax error: "
261 "Duplicate option: '%s'\n", ptr);
262 exit(1);
263}
264
265static void
266usage(void)
267{
263usage(void)
264{
268 fprintf(stderr, ""
265 printf(""
269 "usbconfig - configure the USB subsystem" "\n"
270 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
266 "usbconfig - configure the USB subsystem" "\n"
267 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
271 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
272 "commands:" "\n"
268 "commands:" "\n"
273 " set_config <cfg_index>" "\n"
274 " set_alt <alt_index>" "\n"
275 " set_template <template>" "\n"
276 " get_template" "\n"
269 " set_config <num>" "\n"
270 " set_alt <altno>" "\n"
271 " set_owner <user:group>" "\n"
272 " set_perm <mode>" "\n"
277 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
278 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
273 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
274 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
279 " add_quirk <quirk>" "\n"
280 " remove_quirk <quirk>" "\n"
281 " dump_quirk_names" "\n"
282 " dump_device_quirks" "\n"
283 " dump_device_desc" "\n"
284 " dump_curr_config_desc" "\n"
285 " dump_all_config_desc" "\n"
275 " dump_quirk_names" "\n"
276 " dump_device_quirks" "\n"
277 " dump_device_desc" "\n"
278 " dump_curr_config_desc" "\n"
279 " dump_all_config_desc" "\n"
286 " dump_string <index>" "\n"
280 " dump_access" "\n"
287 " dump_info" "\n"
281 " dump_info" "\n"
288 " show_ifdrv" "\n"
289 " suspend" "\n"
290 " resume" "\n"
291 " power_off" "\n"
292 " power_save" "\n"
293 " power_on" "\n"
294 " reset" "\n"
295 " list" "\n"
282 " suspend" "\n"
283 " resume" "\n"
284 " power_off" "\n"
285 " power_save" "\n"
286 " power_on" "\n"
287 " reset" "\n"
288 " list" "\n"
296 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
297 );
298 exit(1);
299}
300
301static void
302reset_options(struct options *opt)
303{
289 );
290 exit(1);
291}
292
293static void
294reset_options(struct options *opt)
295{
304 if (opt->buffer)
305 free(opt->buffer);
306 memset(opt, 0, sizeof(*opt));
307 return;
308}
309
310static void
311flush_command(struct libusb20_backend *pbe, struct options *opt)
312{
313 struct libusb20_device *pdev = NULL;

--- 4 unchanged lines hidden (view full) ---

318 if ((opt->got_suspend +
319 opt->got_resume +
320 opt->got_reset +
321 opt->got_set_config +
322 opt->got_set_alt +
323 opt->got_power_save +
324 opt->got_power_on +
325 opt->got_power_off) > 1) {
296 memset(opt, 0, sizeof(*opt));
297 return;
298}
299
300static void
301flush_command(struct libusb20_backend *pbe, struct options *opt)
302{
303 struct libusb20_device *pdev = NULL;

--- 4 unchanged lines hidden (view full) ---

308 if ((opt->got_suspend +
309 opt->got_resume +
310 opt->got_reset +
311 opt->got_set_config +
312 opt->got_set_alt +
313 opt->got_power_save +
314 opt->got_power_on +
315 opt->got_power_off) > 1) {
326 err(1, "can only specify one of 'set_config', "
316 err(1, "cannot only specify one of 'set_config', "
327 "'set_alt', 'reset', 'suspend', 'resume', "
328 "'power_save', 'power_on' and 'power_off' "
329 "at the same time!");
330 }
317 "'set_alt', 'reset', 'suspend', 'resume', "
318 "'power_save', 'power_on' and 'power_off' "
319 "at the same time!");
320 }
321 if (opt->got_dump_access) {
322 opt->got_any--;
323 dump_be_access(pbe);
324 }
331 if (opt->got_dump_quirk_names) {
332 opt->got_any--;
333 dump_be_quirk_names(pbe);
334 }
335 if (opt->got_dump_device_quirks) {
336 opt->got_any--;
337 dump_be_dev_quirks(pbe);
338 }
339 if (opt->got_remove_device_quirk) {
340 opt->got_any--;
341 be_dev_remove_quirk(pbe,
342 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
343 }
344 if (opt->got_add_device_quirk) {
345 opt->got_any--;
346 be_dev_add_quirk(pbe,
347 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
348 }
325 if (opt->got_dump_quirk_names) {
326 opt->got_any--;
327 dump_be_quirk_names(pbe);
328 }
329 if (opt->got_dump_device_quirks) {
330 opt->got_any--;
331 dump_be_dev_quirks(pbe);
332 }
333 if (opt->got_remove_device_quirk) {
334 opt->got_any--;
335 be_dev_remove_quirk(pbe,
336 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
337 }
338 if (opt->got_add_device_quirk) {
339 opt->got_any--;
340 be_dev_add_quirk(pbe,
341 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
342 }
349 if (opt->got_set_template) {
350 opt->got_any--;
351 if (libusb20_be_set_template(pbe, opt->template)) {
352 fprintf(stderr, "Setting USB template %u failed, "
353 "continuing.\n", opt->template);
354 }
355 }
356 if (opt->got_get_template) {
357 opt->got_any--;
358 if (libusb20_be_get_template(pbe, &opt->template))
359 printf("USB template: <unknown>\n");
360 else
361 printf("USB template: %u\n", opt->template);
362 }
363 if (opt->got_any == 0) {
364 /*
365 * do not scan through all the devices if there are no valid
366 * options
367 */
368 goto done;
369 }
370 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
371
372 if (opt->got_bus &&
373 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
374 continue;
375 }
376 if (opt->got_addr &&
377 (libusb20_dev_get_address(pdev) != opt->addr)) {
378 continue;
379 }
380 matches++;
381
343 if (opt->got_any == 0) {
344 /*
345 * do not scan through all the devices if there are no valid
346 * options
347 */
348 goto done;
349 }
350 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
351
352 if (opt->got_bus &&
353 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
354 continue;
355 }
356 if (opt->got_addr &&
357 (libusb20_dev_get_address(pdev) != opt->addr)) {
358 continue;
359 }
360 matches++;
361
382 if (opt->got_remove_quirk) {
383 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
384
385 ddesc = libusb20_dev_get_device_desc(pdev);
362 /* do owner and permissions first */
386
363
387 be_dev_remove_quirk(pbe,
388 ddesc->idVendor, ddesc->idProduct,
389 ddesc->bcdDevice, ddesc->bcdDevice,
390 opt->quirkname);
364 if (opt->got_bus && opt->got_addr && opt->got_iface) {
365 if (opt->got_set_owner) {
366 if (libusb20_dev_set_iface_owner(pdev,
367 opt->iface, opt->uid, opt->gid)) {
368 err(1, "setting owner and group failed\n");
369 }
370 }
371 if (opt->got_set_perm) {
372 if (libusb20_dev_set_iface_perm(pdev,
373 opt->iface, opt->mode)) {
374 err(1, "setting mode failed\n");
375 }
376 }
377 } else if (opt->got_bus && opt->got_addr) {
378 if (opt->got_set_owner) {
379 if (libusb20_dev_set_owner(pdev,
380 opt->uid, opt->gid)) {
381 err(1, "setting owner and group failed\n");
382 }
383 }
384 if (opt->got_set_perm) {
385 if (libusb20_dev_set_perm(pdev,
386 opt->mode)) {
387 err(1, "setting mode failed\n");
388 }
389 }
390 } else if (opt->got_bus) {
391 if (opt->got_set_owner) {
392 if (libusb20_bus_set_owner(pbe, opt->bus,
393 opt->uid, opt->gid)) {
394 err(1, "setting owner and group failed\n");
395 }
396 }
397 if (opt->got_set_perm) {
398 if (libusb20_bus_set_perm(pbe, opt->bus,
399 opt->mode)) {
400 err(1, "setting mode failed\n");
401 }
402 }
403 } else {
404 if (opt->got_set_owner) {
405 if (libusb20_be_set_owner(pbe,
406 opt->uid, opt->gid)) {
407 err(1, "setting owner and group failed\n");
408 }
409 }
410 if (opt->got_set_perm) {
411 if (libusb20_be_set_perm(pbe,
412 opt->mode)) {
413 err(1, "setting mode failed\n");
414 }
415 }
391 }
392
416 }
417
393 if (opt->got_add_quirk) {
394 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
395
396 ddesc = libusb20_dev_get_device_desc(pdev);
397
398 be_dev_add_quirk(pbe,
399 ddesc->idVendor, ddesc->idProduct,
400 ddesc->bcdDevice, ddesc->bcdDevice,
401 opt->quirkname);
402 }
403
404 if (libusb20_dev_open(pdev, 0)) {
405 err(1, "could not open device");
406 }
418 if (libusb20_dev_open(pdev, 0)) {
419 err(1, "could not open device");
420 }
407 if (opt->got_dump_string) {
408 dump_string_by_index(pdev, opt->string_index);
409 }
410 if (opt->got_do_request) {
411 uint16_t actlen;
412 uint16_t t;
413
414 if (libusb20_dev_request_sync(pdev, &opt->setup,
415 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
416 printf("REQUEST = <ERROR>\n");
417 } else if (!(opt->setup.bmRequestType &
418 LIBUSB20_ENDPOINT_IN)) {
419 printf("REQUEST = <OK>\n");
420 } else {
421 t = actlen;
422 printf("REQUEST = <");
423 for (t = 0; t != actlen; t++) {
424 printf("0x%02x%s",
425 ((uint8_t *)opt->buffer)[t],
426 (t == (actlen - 1)) ? "" : " ");
427 }
428 printf("><");
429 for (t = 0; t != actlen; t++) {
430 char c;
431
432 c = ((uint8_t *)opt->buffer)[t];
433 if ((c != '<') &&
434 (c != '>') && isprint(c)) {
435 putchar(c);
436 }
437 }
438 printf(">\n");
439 }
440 }
441 if (opt->got_set_config) {
442 if (libusb20_dev_set_config_index(pdev,
443 opt->config_index)) {
444 err(1, "could not set config index");
445 }
446 }
447 if (opt->got_set_alt) {
421 if (opt->got_set_config) {
422 if (libusb20_dev_set_config_index(pdev,
423 opt->config_index)) {
424 err(1, "could not set config index");
425 }
426 }
427 if (opt->got_set_alt) {
448 if (libusb20_dev_set_alt_index(pdev, opt->iface,
449 opt->alt_index)) {
428 if (libusb20_dev_set_alt_index(pdev, opt->iface, opt->alt_index)) {
450 err(1, "could not set alternate setting");
451 }
452 }
453 if (opt->got_reset) {
454 if (libusb20_dev_reset(pdev)) {
455 err(1, "could not reset device");
456 }
457 }
458 if (opt->got_suspend) {
429 err(1, "could not set alternate setting");
430 }
431 }
432 if (opt->got_reset) {
433 if (libusb20_dev_reset(pdev)) {
434 err(1, "could not reset device");
435 }
436 }
437 if (opt->got_suspend) {
459 if (libusb20_dev_set_power_mode(pdev,
460 LIBUSB20_POWER_SUSPEND)) {
438 if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_SUSPEND)) {
461 err(1, "could not set suspend");
462 }
463 }
464 if (opt->got_resume) {
439 err(1, "could not set suspend");
440 }
441 }
442 if (opt->got_resume) {
465 if (libusb20_dev_set_power_mode(pdev,
466 LIBUSB20_POWER_RESUME)) {
443 if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_RESUME)) {
467 err(1, "could not set resume");
468 }
469 }
470 if (opt->got_power_off) {
444 err(1, "could not set resume");
445 }
446 }
447 if (opt->got_power_off) {
471 if (libusb20_dev_set_power_mode(pdev,
472 LIBUSB20_POWER_OFF)) {
448 if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_OFF)) {
473 err(1, "could not set power OFF");
474 }
475 }
476 if (opt->got_power_save) {
449 err(1, "could not set power OFF");
450 }
451 }
452 if (opt->got_power_save) {
477 if (libusb20_dev_set_power_mode(pdev,
478 LIBUSB20_POWER_SAVE)) {
453 if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_SAVE)) {
479 err(1, "could not set power SAVE");
480 }
481 }
482 if (opt->got_power_on) {
454 err(1, "could not set power SAVE");
455 }
456 }
457 if (opt->got_power_on) {
483 if (libusb20_dev_set_power_mode(pdev,
484 LIBUSB20_POWER_ON)) {
458 if (libusb20_dev_set_power_mode(pdev, LIBUSB20_POWER_ON)) {
485 err(1, "could not set power ON");
486 }
487 }
488 dump_any =
489 (opt->got_dump_device_desc ||
490 opt->got_dump_curr_config ||
491 opt->got_dump_all_config ||
459 err(1, "could not set power ON");
460 }
461 }
462 dump_any =
463 (opt->got_dump_device_desc ||
464 opt->got_dump_curr_config ||
465 opt->got_dump_all_config ||
492 opt->got_dump_info);
466 opt->got_dump_info ||
467 opt->got_dump_access);
493
494 if (opt->got_list || dump_any) {
468
469 if (opt->got_list || dump_any) {
495 dump_device_info(pdev,
496 opt->got_show_iface_driver);
470 dump_device_info(pdev);
497 }
471 }
472 if (opt->got_dump_access) {
473 printf("\n");
474 dump_device_access(pdev, opt->got_iface ?
475 opt->iface : 0xFF);
476 }
498 if (opt->got_dump_device_desc) {
499 printf("\n");
500 dump_device_desc(pdev);
501 }
502 if (opt->got_dump_all_config) {
503 printf("\n");
504 dump_config(pdev, 1);
505 } else if (opt->got_dump_curr_config) {

--- 4 unchanged lines hidden (view full) ---

510 printf("\n");
511 }
512 if (libusb20_dev_close(pdev)) {
513 err(1, "could not close device");
514 }
515 }
516
517 if (matches == 0) {
477 if (opt->got_dump_device_desc) {
478 printf("\n");
479 dump_device_desc(pdev);
480 }
481 if (opt->got_dump_all_config) {
482 printf("\n");
483 dump_config(pdev, 1);
484 } else if (opt->got_dump_curr_config) {

--- 4 unchanged lines hidden (view full) ---

489 printf("\n");
490 }
491 if (libusb20_dev_close(pdev)) {
492 err(1, "could not close device");
493 }
494 }
495
496 if (matches == 0) {
518 printf("No device match or lack of permissions.\n");
497 printf("No device match\n");
519 }
520done:
521 reset_options(opt);
522
523 return;
524}
525
526int
527main(int argc, char **argv)
528{
529 struct libusb20_backend *pbe;
530 struct options *opt = &options;
498 }
499done:
500 reset_options(opt);
501
502 return;
503}
504
505int
506main(int argc, char **argv)
507{
508 struct libusb20_backend *pbe;
509 struct options *opt = &options;
531 const char *ptr;
532 int unit;
533 int addr;
510 char *cp;
534 int n;
535 int t;
536
537 if (argc < 1) {
538 usage();
539 }
540 pbe = libusb20_be_alloc_default();
541 if (pbe == NULL)
542 err(1, "could not access USB backend\n");
543
544 for (n = 1; n != argc; n++) {
545
546 /* get number of additional options */
547 t = (argc - n - 1);
548 if (t > 255)
549 t = 255;
550 switch (get_token(argv[n], t)) {
511 int n;
512 int t;
513
514 if (argc < 1) {
515 usage();
516 }
517 pbe = libusb20_be_alloc_default();
518 if (pbe == NULL)
519 err(1, "could not access USB backend\n");
520
521 for (n = 1; n != argc; n++) {
522
523 /* get number of additional options */
524 t = (argc - n - 1);
525 if (t > 255)
526 t = 255;
527 switch (get_token(argv[n], t)) {
551 case T_ADD_QUIRK:
552 if (opt->got_add_quirk) {
553 flush_command(pbe, opt);
554 }
555 opt->quirkname = argv[n + 1];
556 n++;
557
558 opt->got_add_quirk = 1;
559 opt->got_any++;
560 break;
561
562 case T_REMOVE_QUIRK:
563 if (opt->got_remove_quirk) {
564 flush_command(pbe, opt);
565 }
566 opt->quirkname = argv[n + 1];
567 n++;
568
569 opt->got_remove_quirk = 1;
570 opt->got_any++;
571 break;
572
573 case T_ADD_DEVICE_QUIRK:
574 if (opt->got_add_device_quirk) {
575 flush_command(pbe, opt);
576 }
577 opt->vid = num_id(argv[n + 1], "Vendor ID");
578 opt->pid = num_id(argv[n + 2], "Product ID");
579 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
580 opt->hi_rev = num_id(argv[n + 4], "High Revision");

--- 14 unchanged lines hidden (view full) ---

595 opt->hi_rev = num_id(argv[n + 4], "High Revision");
596 opt->quirkname = argv[n + 5];
597 n += 5;
598 opt->got_remove_device_quirk = 1;
599 opt->got_any++;
600 break;
601
602 case T_DUMP_QUIRK_NAMES:
528 case T_ADD_DEVICE_QUIRK:
529 if (opt->got_add_device_quirk) {
530 flush_command(pbe, opt);
531 }
532 opt->vid = num_id(argv[n + 1], "Vendor ID");
533 opt->pid = num_id(argv[n + 2], "Product ID");
534 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
535 opt->hi_rev = num_id(argv[n + 4], "High Revision");

--- 14 unchanged lines hidden (view full) ---

550 opt->hi_rev = num_id(argv[n + 4], "High Revision");
551 opt->quirkname = argv[n + 5];
552 n += 5;
553 opt->got_remove_device_quirk = 1;
554 opt->got_any++;
555 break;
556
557 case T_DUMP_QUIRK_NAMES:
603 if (opt->got_dump_quirk_names)
604 duplicate_option(argv[n]);
605 opt->got_dump_quirk_names = 1;
606 opt->got_any++;
607 break;
608
609 case T_DUMP_DEVICE_QUIRKS:
558 opt->got_dump_quirk_names = 1;
559 opt->got_any++;
560 break;
561
562 case T_DUMP_DEVICE_QUIRKS:
610 if (opt->got_dump_device_quirks)
611 duplicate_option(argv[n]);
612 opt->got_dump_device_quirks = 1;
613 opt->got_any++;
614 break;
615
563 opt->got_dump_device_quirks = 1;
564 opt->got_any++;
565 break;
566
616 case T_SHOW_IFACE_DRIVER:
617 opt->got_show_iface_driver = 1;
618 break;
619
620 case T_UGEN:
621 if (opt->got_any) {
622 /* allow multiple commands on the same line */
623 flush_command(pbe, opt);
624 }
625 ptr = argv[n + 1];
626
627 if ((ptr[0] == 'u') &&
628 (ptr[1] == 'g') &&
629 (ptr[2] == 'e') &&
630 (ptr[3] == 'n'))
631 ptr += 4;
632
633 if ((sscanf(ptr, "%d.%d",
634 &unit, &addr) != 2) ||
635 (unit < 0) || (unit > 65535) ||
636 (addr < 0) || (addr > 65535)) {
637 errx(1, "cannot "
638 "parse '%s'", argv[n + 1]);
639 }
640 opt->bus = unit;
641 opt->addr = addr;
642 opt->got_bus = 1;
643; opt->got_addr = 1;
644 n++;
645 break;
646
647 case T_UNIT:
648 if (opt->got_any) {
649 /* allow multiple commands on the same line */
650 flush_command(pbe, opt);
651 }
652 opt->bus = num_id(argv[n + 1], "busnum");
653 opt->got_bus = 1;
654 n++;

--- 4 unchanged lines hidden (view full) ---

659 n++;
660 break;
661 case T_IFACE:
662 opt->iface = num_id(argv[n + 1], "iface");
663 opt->got_iface = 1;
664 n++;
665 break;
666 case T_SET_CONFIG:
567 case T_UNIT:
568 if (opt->got_any) {
569 /* allow multiple commands on the same line */
570 flush_command(pbe, opt);
571 }
572 opt->bus = num_id(argv[n + 1], "busnum");
573 opt->got_bus = 1;
574 n++;

--- 4 unchanged lines hidden (view full) ---

579 n++;
580 break;
581 case T_IFACE:
582 opt->iface = num_id(argv[n + 1], "iface");
583 opt->got_iface = 1;
584 n++;
585 break;
586 case T_SET_CONFIG:
667 if (opt->got_set_config)
668 duplicate_option(argv[n]);
669 opt->config_index = num_id(argv[n + 1], "cfg_index");
587 opt->config_index = num_id(argv[n + 1], "confindex");
670 opt->got_set_config = 1;
671 opt->got_any++;
672 n++;
673 break;
674 case T_SET_ALT:
588 opt->got_set_config = 1;
589 opt->got_any++;
590 n++;
591 break;
592 case T_SET_ALT:
675 if (opt->got_set_alt)
676 duplicate_option(argv[n]);
677 opt->alt_index = num_id(argv[n + 1], "cfg_index");
593 opt->alt_index = num_id(argv[n + 1], "confindex");
678 opt->got_set_alt = 1;
679 opt->got_any++;
680 n++;
681 break;
594 opt->got_set_alt = 1;
595 opt->got_any++;
596 n++;
597 break;
682 case T_SET_TEMPLATE:
683 if (opt->got_set_template)
684 duplicate_option(argv[n]);
685 opt->template = get_int(argv[n + 1]);
686 opt->got_set_template = 1;
598 case T_SET_OWNER:
599 cp = argv[n + 1];
600 cp = strchr(cp, ':');
601 if (cp == NULL) {
602 err(1, "missing colon in '%s'!", argv[n + 1]);
603 }
604 *(cp++) = '\0';
605 opt->gid = a_gid(cp);
606 opt->uid = a_uid(argv[n + 1]);
607 opt->got_set_owner = 1;
687 opt->got_any++;
688 n++;
689 break;
608 opt->got_any++;
609 n++;
610 break;
690 case T_GET_TEMPLATE:
691 if (opt->got_get_template)
692 duplicate_option(argv[n]);
693 opt->got_get_template = 1;
611 case T_SET_PERM:
612 opt->mode = a_mode(argv[n + 1]);
613 opt->got_set_perm = 1;
694 opt->got_any++;
614 opt->got_any++;
615 n++;
695 break;
696 case T_DUMP_DEVICE_DESC:
616 break;
617 case T_DUMP_DEVICE_DESC:
697 if (opt->got_dump_device_desc)
698 duplicate_option(argv[n]);
699 opt->got_dump_device_desc = 1;
700 opt->got_any++;
701 break;
702 case T_DUMP_CURR_CONFIG_DESC:
618 opt->got_dump_device_desc = 1;
619 opt->got_any++;
620 break;
621 case T_DUMP_CURR_CONFIG_DESC:
703 if (opt->got_dump_curr_config)
704 duplicate_option(argv[n]);
705 opt->got_dump_curr_config = 1;
706 opt->got_any++;
707 break;
708 case T_DUMP_ALL_CONFIG_DESC:
622 opt->got_dump_curr_config = 1;
623 opt->got_any++;
624 break;
625 case T_DUMP_ALL_CONFIG_DESC:
709 if (opt->got_dump_all_config)
710 duplicate_option(argv[n]);
711 opt->got_dump_all_config = 1;
712 opt->got_any++;
713 break;
714 case T_DUMP_INFO:
626 opt->got_dump_all_config = 1;
627 opt->got_any++;
628 break;
629 case T_DUMP_INFO:
715 if (opt->got_dump_info)
716 duplicate_option(argv[n]);
717 opt->got_dump_info = 1;
718 opt->got_any++;
719 break;
630 opt->got_dump_info = 1;
631 opt->got_any++;
632 break;
720 case T_DUMP_STRING:
721 if (opt->got_dump_string)
722 duplicate_option(argv[n]);
723 opt->string_index = num_id(argv[n + 1], "str_index");
724 opt->got_dump_string = 1;
633 case T_DUMP_ACCESS:
634 opt->got_dump_access = 1;
725 opt->got_any++;
635 opt->got_any++;
726 n++;
727 break;
728 case T_SUSPEND:
636 break;
637 case T_SUSPEND:
729 if (opt->got_suspend)
730 duplicate_option(argv[n]);
731 opt->got_suspend = 1;
732 opt->got_any++;
733 break;
734 case T_RESUME:
638 opt->got_suspend = 1;
639 opt->got_any++;
640 break;
641 case T_RESUME:
735 if (opt->got_resume)
736 duplicate_option(argv[n]);
737 opt->got_resume = 1;
738 opt->got_any++;
739 break;
740 case T_POWER_OFF:
642 opt->got_resume = 1;
643 opt->got_any++;
644 break;
645 case T_POWER_OFF:
741 if (opt->got_power_off)
742 duplicate_option(argv[n]);
743 opt->got_power_off = 1;
744 opt->got_any++;
745 break;
746 case T_POWER_SAVE:
646 opt->got_power_off = 1;
647 opt->got_any++;
648 break;
649 case T_POWER_SAVE:
747 if (opt->got_power_save)
748 duplicate_option(argv[n]);
749 opt->got_power_save = 1;
750 opt->got_any++;
751 break;
752 case T_POWER_ON:
650 opt->got_power_save = 1;
651 opt->got_any++;
652 break;
653 case T_POWER_ON:
753 if (opt->got_power_on)
754 duplicate_option(argv[n]);
755 opt->got_power_on = 1;
756 opt->got_any++;
757 break;
758 case T_RESET:
654 opt->got_power_on = 1;
655 opt->got_any++;
656 break;
657 case T_RESET:
759 if (opt->got_reset)
760 duplicate_option(argv[n]);
761 opt->got_reset = 1;
762 opt->got_any++;
763 break;
764 case T_LIST:
658 opt->got_reset = 1;
659 opt->got_any++;
660 break;
661 case T_LIST:
765 if (opt->got_list)
766 duplicate_option(argv[n]);
767 opt->got_list = 1;
768 opt->got_any++;
769 break;
662 opt->got_list = 1;
663 opt->got_any++;
664 break;
770 case T_DO_REQUEST:
771 if (opt->got_do_request)
772 duplicate_option(argv[n]);
773 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
774 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
775 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
776 opt->setup.wValue = num_id(argv[n + 3], "wVal");
777 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
778 opt->setup.wLength = num_id(argv[n + 5], "wLen");
779 if (opt->setup.wLength != 0) {
780 opt->buffer = malloc(opt->setup.wLength);
781 } else {
782 opt->buffer = NULL;
783 }
784
785 n += 5;
786
787 if (!(opt->setup.bmRequestType &
788 LIBUSB20_ENDPOINT_IN)) {
789 /* copy in data */
790 t = (argc - n - 1);
791 if (t < opt->setup.wLength) {
792 err(1, "request data missing");
793 }
794 t = opt->setup.wLength;
795 while (t--) {
796 ((uint8_t *)opt->buffer)[t] =
797 num_id(argv[n + t + 1], "req_data");
798 }
799 n += opt->setup.wLength;
800 }
801 opt->got_do_request = 1;
802 opt->got_any++;
803 break;
804 default:
805 usage();
806 break;
807 }
808 }
809 if (opt->got_any) {
810 /* flush out last command */
811 flush_command(pbe, opt);
812 } else {
813 /* list all the devices */
814 opt->got_list = 1;
815 opt->got_any++;
816 flush_command(pbe, opt);
817 }
818 /* release data */
819 libusb20_be_free(pbe);
820
821 return (0);
822}
665 default:
666 usage();
667 break;
668 }
669 }
670 if (opt->got_any) {
671 /* flush out last command */
672 flush_command(pbe, opt);
673 } else {
674 /* list all the devices */
675 opt->got_list = 1;
676 opt->got_any++;
677 flush_command(pbe, opt);
678 }
679 /* release data */
680 libusb20_be_free(pbe);
681
682 return (0);
683}