1 /*
2 *
3 * Copyright (c) 2016, Oracle and/or its affiliates.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <config.h>
21
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <strings.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <wait.h>
32 #include <unistd.h>
33 #include <aio.h>
34 #include <libdevinfo.h>
35 #include <sys/nvpair.h>
36 #include <sys/devctl.h>
37 #include <sys/usb/clients/ugen/usb_ugen.h>
38 #include <sys/usb/usba.h>
39 #include <sys/pci.h>
40
41 #include "libusbi.h"
42 #include "sunos_usb.h"
43
44 #define UPDATEDRV_PATH "/usr/sbin/update_drv"
45 #define UPDATEDRV "update_drv"
46
47 #define DEFAULT_LISTSIZE 6
48
49 typedef struct {
50 int nargs;
51 int listsize;
52 char **string;
53 } string_list_t;
54
55 /*
56 * Backend functions
57 */
58 static int sunos_get_device_list(struct libusb_context *,
59 struct discovered_devs **);
60 static int sunos_open(struct libusb_device_handle *);
61 static void sunos_close(struct libusb_device_handle *);
62 static int sunos_get_active_config_descriptor(struct libusb_device *,
63 void *, size_t);
64 static int sunos_get_config_descriptor(struct libusb_device *, uint8_t,
65 void *, size_t);
66 static int sunos_get_configuration(struct libusb_device_handle *, uint8_t *);
67 static int sunos_set_configuration(struct libusb_device_handle *, int);
68 static int sunos_claim_interface(struct libusb_device_handle *, uint8_t);
69 static int sunos_release_interface(struct libusb_device_handle *, uint8_t);
70 static int sunos_set_interface_altsetting(struct libusb_device_handle *,
71 uint8_t, uint8_t);
72 static int sunos_clear_halt(struct libusb_device_handle *, unsigned char);
73 static void sunos_destroy_device(struct libusb_device *);
74 static int sunos_submit_transfer(struct usbi_transfer *);
75 static int sunos_cancel_transfer(struct usbi_transfer *);
76 static int sunos_handle_transfer_completion(struct usbi_transfer *);
77 static int sunos_kernel_driver_active(struct libusb_device_handle *, uint8_t);
78 static int sunos_detach_kernel_driver(struct libusb_device_handle *, uint8_t);
79 static int sunos_attach_kernel_driver(struct libusb_device_handle *, uint8_t);
80 static int sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv);
81 static int sunos_usb_ioctl(struct libusb_device *dev, int cmd);
82
sunos_get_link(di_devlink_t devlink,void * arg)83 static int sunos_get_link(di_devlink_t devlink, void *arg)
84 {
85 walk_link_t *larg = (walk_link_t *)arg;
86 const char *p;
87 const char *q;
88
89 if (larg->path) {
90 char *content = (char *)di_devlink_content(devlink);
91 char *start = strstr(content, "/devices/");
92 start += strlen("/devices");
93 usbi_dbg("%s", start);
94
95 /* line content must have minor node */
96 if (start == NULL ||
97 strncmp(start, larg->path, larg->len) != 0 ||
98 start[larg->len] != ':')
99 return (DI_WALK_CONTINUE);
100 }
101
102 p = di_devlink_path(devlink);
103 q = strrchr(p, '/');
104 usbi_dbg("%s", q);
105
106 *(larg->linkpp) = strndup(p, strlen(p) - strlen(q));
107
108 return (DI_WALK_TERMINATE);
109 }
110
111
sunos_physpath_to_devlink(const char * node_path,const char * match,char ** link_path)112 static int sunos_physpath_to_devlink(
113 const char *node_path, const char *match, char **link_path)
114 {
115 walk_link_t larg;
116 di_devlink_handle_t hdl;
117
118 *link_path = NULL;
119 larg.linkpp = link_path;
120 if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
121 usbi_dbg("di_devlink_init failure");
122 return (-1);
123 }
124
125 larg.len = strlen(node_path);
126 larg.path = (char *)node_path;
127
128 (void) di_devlink_walk(hdl, match, NULL, DI_PRIMARY_LINK,
129 (void *)&larg, sunos_get_link);
130
131 (void) di_devlink_fini(&hdl);
132
133 if (*link_path == NULL) {
134 usbi_dbg("there is no devlink for this path");
135 return (-1);
136 }
137
138 return 0;
139 }
140
141 static int
sunos_usb_ioctl(struct libusb_device * dev,int cmd)142 sunos_usb_ioctl(struct libusb_device *dev, int cmd)
143 {
144 int fd;
145 nvlist_t *nvlist;
146 char *end;
147 char *phypath;
148 char *hubpath;
149 char path_arg[PATH_MAX];
150 sunos_dev_priv_t *dpriv;
151 devctl_ap_state_t devctl_ap_state;
152 struct devctl_iocdata iocdata;
153
154 dpriv = usbi_get_device_priv(dev);
155 phypath = dpriv->phypath;
156
157 end = strrchr(phypath, '/');
158 if (end == NULL)
159 return (-1);
160 hubpath = strndup(phypath, end - phypath);
161 if (hubpath == NULL)
162 return (-1);
163
164 end = strrchr(hubpath, '@');
165 if (end == NULL) {
166 free(hubpath);
167 return (-1);
168 }
169 end++;
170 usbi_dbg("unitaddr: %s", end);
171
172 nvlist_alloc(&nvlist, NV_UNIQUE_NAME_TYPE, KM_NOSLEEP);
173 nvlist_add_int32(nvlist, "port", dev->port_number);
174 //find the hub path
175 snprintf(path_arg, sizeof(path_arg), "/devices%s:hubd", hubpath);
176 usbi_dbg("ioctl hub path: %s", path_arg);
177
178 fd = open(path_arg, O_RDONLY);
179 if (fd < 0) {
180 usbi_err(DEVICE_CTX(dev), "open failed: errno %d (%s)", errno, strerror(errno));
181 nvlist_free(nvlist);
182 free(hubpath);
183 return (-1);
184 }
185
186 memset(&iocdata, 0, sizeof(iocdata));
187 memset(&devctl_ap_state, 0, sizeof(devctl_ap_state));
188
189 nvlist_pack(nvlist, (char **)&iocdata.nvl_user, &iocdata.nvl_usersz, NV_ENCODE_NATIVE, 0);
190
191 iocdata.cmd = DEVCTL_AP_GETSTATE;
192 iocdata.flags = 0;
193 iocdata.c_nodename = (char *)"hub";
194 iocdata.c_unitaddr = end;
195 iocdata.cpyout_buf = &devctl_ap_state;
196 usbi_dbg("%p, %" PRIuPTR, iocdata.nvl_user, iocdata.nvl_usersz);
197
198 errno = 0;
199 if (ioctl(fd, DEVCTL_AP_GETSTATE, &iocdata) == -1) {
200 usbi_err(DEVICE_CTX(dev), "ioctl failed: fd %d, cmd %x, errno %d (%s)",
201 fd, DEVCTL_AP_GETSTATE, errno, strerror(errno));
202 } else {
203 usbi_dbg("dev rstate: %d", devctl_ap_state.ap_rstate);
204 usbi_dbg("dev ostate: %d", devctl_ap_state.ap_ostate);
205 }
206
207 errno = 0;
208 iocdata.cmd = cmd;
209 if (ioctl(fd, (int)cmd, &iocdata) != 0) {
210 usbi_err(DEVICE_CTX(dev), "ioctl failed: fd %d, cmd %x, errno %d (%s)",
211 fd, cmd, errno, strerror(errno));
212 sleep(2);
213 }
214
215 close(fd);
216 free(iocdata.nvl_user);
217 nvlist_free(nvlist);
218 free(hubpath);
219
220 return (-errno);
221 }
222
223 static int
sunos_kernel_driver_active(struct libusb_device_handle * dev_handle,uint8_t interface)224 sunos_kernel_driver_active(struct libusb_device_handle *dev_handle, uint8_t interface)
225 {
226 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev_handle->dev);
227
228 UNUSED(interface);
229
230 usbi_dbg("%s", dpriv->ugenpath);
231
232 return (dpriv->ugenpath == NULL);
233 }
234
235 /*
236 * Private functions
237 */
238 static int _errno_to_libusb(int);
239 static int sunos_usb_get_status(int fd);
240
241 static string_list_t *
sunos_new_string_list(void)242 sunos_new_string_list(void)
243 {
244 string_list_t *list;
245
246 list = calloc(1, sizeof(string_list_t));
247 if (list == NULL)
248 return (NULL);
249 list->string = calloc(DEFAULT_LISTSIZE, sizeof(char *));
250 if (list->string == NULL) {
251 free(list);
252 return (NULL);
253 }
254 list->nargs = 0;
255 list->listsize = DEFAULT_LISTSIZE;
256
257 return (list);
258 }
259
260 static int
sunos_append_to_string_list(string_list_t * list,const char * arg)261 sunos_append_to_string_list(string_list_t *list, const char *arg)
262 {
263 char *str = strdup(arg);
264
265 if (str == NULL)
266 return (-1);
267
268 if ((list->nargs + 1) == list->listsize) { /* +1 is for NULL */
269 char **tmp = realloc(list->string,
270 sizeof(char *) * (list->listsize + 1));
271 if (tmp == NULL) {
272 free(str);
273 return (-1);
274 }
275 list->string = tmp;
276 list->string[list->listsize++] = NULL;
277 }
278 list->string[list->nargs++] = str;
279
280 return (0);
281 }
282
283 static void
sunos_free_string_list(string_list_t * list)284 sunos_free_string_list(string_list_t *list)
285 {
286 int i;
287
288 for (i = 0; i < list->nargs; i++) {
289 free(list->string[i]);
290 }
291
292 free(list->string);
293 free(list);
294 }
295
296 static char **
sunos_build_argv_list(string_list_t * list)297 sunos_build_argv_list(string_list_t *list)
298 {
299 return (list->string);
300 }
301
302
303 static int
sunos_exec_command(struct libusb_context * ctx,const char * path,string_list_t * list)304 sunos_exec_command(struct libusb_context *ctx, const char *path,
305 string_list_t *list)
306 {
307 pid_t pid;
308 int status;
309 int waitstat;
310 int exit_status;
311 char **argv_list;
312
313 argv_list = sunos_build_argv_list(list);
314 if (argv_list == NULL)
315 return (-1);
316
317 pid = fork();
318 if (pid == 0) {
319 /* child */
320 execv(path, argv_list);
321 _exit(127);
322 } else if (pid > 0) {
323 /* parent */
324 do {
325 waitstat = waitpid(pid, &status, 0);
326 } while ((waitstat == -1 && errno == EINTR) ||
327 (waitstat == 0 && !WIFEXITED(status) && !WIFSIGNALED(status)));
328
329 if (waitstat == 0) {
330 if (WIFEXITED(status))
331 exit_status = WEXITSTATUS(status);
332 else
333 exit_status = WTERMSIG(status);
334 } else {
335 usbi_err(ctx, "waitpid failed: errno %d (%s)", errno, strerror(errno));
336 exit_status = -1;
337 }
338 } else {
339 /* fork failed */
340 usbi_err(ctx, "fork failed: errno %d (%s)", errno, strerror(errno));
341 exit_status = -1;
342 }
343
344 return (exit_status);
345 }
346
347 static int
sunos_detach_kernel_driver(struct libusb_device_handle * dev_handle,uint8_t interface_number)348 sunos_detach_kernel_driver(struct libusb_device_handle *dev_handle,
349 uint8_t interface_number)
350 {
351 struct libusb_context *ctx = HANDLE_CTX(dev_handle);
352 string_list_t *list;
353 char path_arg[PATH_MAX];
354 sunos_dev_priv_t *dpriv;
355 int r;
356
357 UNUSED(interface_number);
358
359 dpriv = usbi_get_device_priv(dev_handle->dev);
360 snprintf(path_arg, sizeof(path_arg), "\'\"%s\"\'", dpriv->phypath);
361 usbi_dbg("%s", path_arg);
362
363 list = sunos_new_string_list();
364 if (list == NULL)
365 return (LIBUSB_ERROR_NO_MEM);
366
367 /* attach ugen driver */
368 r = 0;
369 r |= sunos_append_to_string_list(list, UPDATEDRV);
370 r |= sunos_append_to_string_list(list, "-a"); /* add rule */
371 r |= sunos_append_to_string_list(list, "-i"); /* specific device */
372 r |= sunos_append_to_string_list(list, path_arg); /* physical path */
373 r |= sunos_append_to_string_list(list, "ugen");
374 if (r) {
375 sunos_free_string_list(list);
376 return (LIBUSB_ERROR_NO_MEM);
377 }
378
379 r = sunos_exec_command(ctx, UPDATEDRV_PATH, list);
380 sunos_free_string_list(list);
381 if (r < 0)
382 return (LIBUSB_ERROR_OTHER);
383
384 /* reconfigure the driver node */
385 r = 0;
386 r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_DISCONNECT);
387 r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE);
388 if (r)
389 usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed");
390
391 snprintf(path_arg, sizeof(path_arg), "^usb/%x.%x",
392 dev_handle->dev->device_descriptor.idVendor,
393 dev_handle->dev->device_descriptor.idProduct);
394 sunos_physpath_to_devlink(dpriv->phypath, path_arg, &dpriv->ugenpath);
395
396 if (access(dpriv->ugenpath, F_OK) == -1) {
397 usbi_err(HANDLE_CTX(dev_handle), "fail to detach kernel driver");
398 return (LIBUSB_ERROR_IO);
399 }
400
401 return sunos_usb_open_ep0(usbi_get_device_handle_priv(dev_handle), dpriv);
402 }
403
404 static int
sunos_attach_kernel_driver(struct libusb_device_handle * dev_handle,uint8_t interface_number)405 sunos_attach_kernel_driver(struct libusb_device_handle *dev_handle,
406 uint8_t interface_number)
407 {
408 struct libusb_context *ctx = HANDLE_CTX(dev_handle);
409 string_list_t *list;
410 char path_arg[PATH_MAX];
411 sunos_dev_priv_t *dpriv;
412 int r;
413
414 UNUSED(interface_number);
415
416 /* we open the dev in detach driver, so we need close it first. */
417 sunos_close(dev_handle);
418
419 dpriv = usbi_get_device_priv(dev_handle->dev);
420 snprintf(path_arg, sizeof(path_arg), "\'\"%s\"\'", dpriv->phypath);
421 usbi_dbg("%s", path_arg);
422
423 list = sunos_new_string_list();
424 if (list == NULL)
425 return (LIBUSB_ERROR_NO_MEM);
426
427 /* detach ugen driver */
428 r = 0;
429 r |= sunos_append_to_string_list(list, UPDATEDRV);
430 r |= sunos_append_to_string_list(list, "-d"); /* add rule */
431 r |= sunos_append_to_string_list(list, "-i"); /* specific device */
432 r |= sunos_append_to_string_list(list, path_arg); /* physical path */
433 r |= sunos_append_to_string_list(list, "ugen");
434 if (r) {
435 sunos_free_string_list(list);
436 return (LIBUSB_ERROR_NO_MEM);
437 }
438
439 r = sunos_exec_command(ctx, UPDATEDRV_PATH, list);
440 sunos_free_string_list(list);
441 if (r < 0)
442 return (LIBUSB_ERROR_OTHER);
443
444 /* reconfigure the driver node */
445 r = 0;
446 r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE);
447 r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_DISCONNECT);
448 r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE);
449 if (r)
450 usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed");
451
452 return 0;
453 }
454
455 static int
sunos_fill_in_dev_info(di_node_t node,struct libusb_device * dev)456 sunos_fill_in_dev_info(di_node_t node, struct libusb_device *dev)
457 {
458 int proplen;
459 int *i, n, *addr, *port_prop;
460 char *phypath;
461 uint8_t *rdata;
462 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev);
463 char match_str[PATH_MAX];
464
465 /* Device descriptors */
466 proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
467 "usb-dev-descriptor", &rdata);
468 if (proplen <= 0) {
469 return (LIBUSB_ERROR_IO);
470 }
471 bcopy(rdata, &dev->device_descriptor, LIBUSB_DT_DEVICE_SIZE);
472
473 /* Raw configuration descriptors */
474 proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
475 "usb-raw-cfg-descriptors", &rdata);
476 if (proplen <= 0) {
477 usbi_dbg("can't find raw config descriptors");
478
479 return (LIBUSB_ERROR_IO);
480 }
481 dpriv->raw_cfgdescr = calloc(1, proplen);
482 if (dpriv->raw_cfgdescr == NULL) {
483 return (LIBUSB_ERROR_NO_MEM);
484 } else {
485 bcopy(rdata, dpriv->raw_cfgdescr, proplen);
486 dpriv->cfgvalue = ((struct libusb_config_descriptor *)
487 rdata)->bConfigurationValue;
488 }
489
490 n = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &port_prop);
491
492 if ((n != 1) || (*port_prop <= 0)) {
493 return (LIBUSB_ERROR_IO);
494 }
495 dev->port_number = *port_prop;
496
497 /* device physical path */
498 phypath = di_devfs_path(node);
499 if (phypath) {
500 dpriv->phypath = strdup(phypath);
501 snprintf(match_str, sizeof(match_str), "^usb/%x.%x",
502 dev->device_descriptor.idVendor,
503 dev->device_descriptor.idProduct);
504 usbi_dbg("match is %s", match_str);
505 sunos_physpath_to_devlink(dpriv->phypath, match_str, &dpriv->ugenpath);
506 di_devfs_path_free(phypath);
507
508 } else {
509 free(dpriv->raw_cfgdescr);
510
511 return (LIBUSB_ERROR_IO);
512 }
513
514 /* address */
515 n = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "assigned-address", &addr);
516 if (n != 1 || *addr == 0) {
517 usbi_dbg("can't get address");
518 } else {
519 dev->device_address = *addr;
520 }
521
522 /* speed */
523 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "low-speed", &i) >= 0) {
524 dev->speed = LIBUSB_SPEED_LOW;
525 } else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "high-speed", &i) >= 0) {
526 dev->speed = LIBUSB_SPEED_HIGH;
527 } else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "full-speed", &i) >= 0) {
528 dev->speed = LIBUSB_SPEED_FULL;
529 } else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "super-speed", &i) >= 0) {
530 dev->speed = LIBUSB_SPEED_SUPER;
531 }
532
533 usbi_dbg("vid=%x pid=%x, path=%s, bus_nmber=0x%x, port_number=%d, speed=%d",
534 dev->device_descriptor.idVendor, dev->device_descriptor.idProduct,
535 dpriv->phypath, dev->bus_number, dev->port_number, dev->speed);
536
537 return (LIBUSB_SUCCESS);
538 }
539
540 static int
sunos_add_devices(di_devlink_t link,void * arg)541 sunos_add_devices(di_devlink_t link, void *arg)
542 {
543 struct devlink_cbarg *largs = (struct devlink_cbarg *)arg;
544 struct node_args *nargs;
545 di_node_t myself, dn;
546 uint64_t session_id = 0;
547 uint64_t sid = 0;
548 uint64_t bdf = 0;
549 struct libusb_device *dev;
550 sunos_dev_priv_t *devpriv;
551 int n, *j;
552 int i = 0;
553 int *addr_prop;
554 uint8_t bus_number = 0;
555 uint32_t * regbuf = NULL;
556 uint32_t reg;
557
558 UNUSED(link);
559
560 nargs = (struct node_args *)largs->nargs;
561 myself = largs->myself;
562
563 /*
564 * Construct session ID.
565 * session ID = dev_addr | hub addr |parent hub addr|...|root hub bdf
566 * 8 bits 8bits 8 bits 16bits
567 */
568 if (myself == DI_NODE_NIL)
569 return (DI_WALK_CONTINUE);
570
571 dn = myself;
572 /* find the root hub */
573 while (di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "root-hub", &j) != 0) {
574 usbi_dbg("find_root_hub:%s", di_devfs_path(dn));
575 n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn,
576 "assigned-address", &addr_prop);
577 session_id |= ((addr_prop[0] & 0xff) << i++ * 8);
578 dn = di_parent_node(dn);
579 }
580
581 /* dn is the root hub node */
582 n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "reg", (int **)®buf);
583 reg = regbuf[0];
584 bdf = (PCI_REG_BUS_G(reg) << 8) | (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg);
585 /* bdf must larger than i*8 bits */
586 session_id |= (bdf << i * 8);
587 bus_number = (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg);
588
589 usbi_dbg("device bus address=%s:%x, name:%s",
590 di_bus_addr(myself), bus_number, di_node_name(dn));
591 usbi_dbg("session id org:%" PRIx64, session_id);
592
593 /* dn is the usb device */
594 for (dn = di_child_node(myself); dn != DI_NODE_NIL; dn = di_sibling_node(dn)) {
595 usbi_dbg("device path:%s", di_devfs_path(dn));
596 /* skip hub devices, because its driver can not been unload */
597 if (di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "usb-port-count", &addr_prop) != -1)
598 continue;
599 /* usb_addr */
600 n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn,
601 "assigned-address", &addr_prop);
602 if ((n != 1) || (addr_prop[0] == 0)) {
603 usbi_dbg("cannot get valid usb_addr");
604 continue;
605 }
606
607 sid = (session_id << 8) | (addr_prop[0] & 0xff) ;
608 usbi_dbg("session id %" PRIX64, sid);
609
610 dev = usbi_get_device_by_session_id(nargs->ctx, sid);
611 if (dev == NULL) {
612 dev = usbi_alloc_device(nargs->ctx, sid);
613 if (dev == NULL) {
614 usbi_dbg("can't alloc device");
615 continue;
616 }
617 devpriv = usbi_get_device_priv(dev);
618 dev->bus_number = bus_number;
619
620 if (sunos_fill_in_dev_info(dn, dev) != LIBUSB_SUCCESS) {
621 libusb_unref_device(dev);
622 usbi_dbg("get information fail");
623 continue;
624 }
625 if (usbi_sanitize_device(dev) < 0) {
626 libusb_unref_device(dev);
627 usbi_dbg("sanatize failed: ");
628 return (DI_WALK_TERMINATE);
629 }
630 } else {
631 devpriv = usbi_get_device_priv(dev);
632 usbi_dbg("Dev %s exists", devpriv->ugenpath);
633 }
634
635 if (discovered_devs_append(*(nargs->discdevs), dev) == NULL) {
636 usbi_dbg("cannot append device");
637 }
638
639 /*
640 * we alloc and hence ref this dev. We don't need to ref it
641 * hereafter. Front end or app should take care of their ref.
642 */
643 libusb_unref_device(dev);
644
645 usbi_dbg("Device %s %s id=0x%" PRIx64 ", devcount:%" PRIuPTR
646 ", bdf=%" PRIx64,
647 devpriv->ugenpath, di_devfs_path(dn), (uint64_t)sid,
648 (*nargs->discdevs)->len, bdf);
649 }
650
651 return (DI_WALK_CONTINUE);
652 }
653
654 static int
sunos_walk_minor_node_link(di_node_t node,void * args)655 sunos_walk_minor_node_link(di_node_t node, void *args)
656 {
657 di_minor_t minor = DI_MINOR_NIL;
658 char *minor_path;
659 struct devlink_cbarg arg;
660 struct node_args *nargs = (struct node_args *)args;
661 di_devlink_handle_t devlink_hdl = nargs->dlink_hdl;
662
663 /* walk each minor to find usb devices */
664 while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
665 minor_path = di_devfs_minor_path(minor);
666 arg.nargs = args;
667 arg.myself = node;
668 arg.minor = minor;
669 (void) di_devlink_walk(devlink_hdl,
670 "^usb/hub[0-9]+", minor_path,
671 DI_PRIMARY_LINK, (void *)&arg, sunos_add_devices);
672 di_devfs_path_free(minor_path);
673 }
674
675 /* switch to a different node */
676 nargs->last_ugenpath = NULL;
677
678 return (DI_WALK_CONTINUE);
679 }
680
681 int
sunos_get_device_list(struct libusb_context * ctx,struct discovered_devs ** discdevs)682 sunos_get_device_list(struct libusb_context * ctx,
683 struct discovered_devs **discdevs)
684 {
685 di_node_t root_node;
686 struct node_args args;
687 di_devlink_handle_t devlink_hdl;
688
689 args.ctx = ctx;
690 args.discdevs = discdevs;
691 args.last_ugenpath = NULL;
692 if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
693 usbi_dbg("di_int() failed: errno %d (%s)", errno, strerror(errno));
694 return (LIBUSB_ERROR_IO);
695 }
696
697 if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) {
698 di_fini(root_node);
699 usbi_dbg("di_devlink_init() failed: errno %d (%s)", errno, strerror(errno));
700
701 return (LIBUSB_ERROR_IO);
702 }
703 args.dlink_hdl = devlink_hdl;
704
705 /* walk each node to find USB devices */
706 if (di_walk_node(root_node, DI_WALK_SIBFIRST, &args,
707 sunos_walk_minor_node_link) == -1) {
708 usbi_dbg("di_walk_node() failed: errno %d (%s)", errno, strerror(errno));
709 di_fini(root_node);
710
711 return (LIBUSB_ERROR_IO);
712 }
713
714 di_fini(root_node);
715 di_devlink_fini(&devlink_hdl);
716
717 usbi_dbg("%zu devices", (*discdevs)->len);
718
719 return ((*discdevs)->len);
720 }
721
722 static int
sunos_usb_open_ep0(sunos_dev_handle_priv_t * hpriv,sunos_dev_priv_t * dpriv)723 sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv)
724 {
725 char filename[PATH_MAX + 1];
726
727 if (hpriv->eps[0].datafd > 0) {
728 return (LIBUSB_SUCCESS);
729 }
730 snprintf(filename, PATH_MAX, "%s/cntrl0", dpriv->ugenpath);
731
732 usbi_dbg("opening %s", filename);
733 hpriv->eps[0].datafd = open(filename, O_RDWR);
734 if (hpriv->eps[0].datafd < 0) {
735 return(_errno_to_libusb(errno));
736 }
737
738 snprintf(filename, PATH_MAX, "%s/cntrl0stat", dpriv->ugenpath);
739 hpriv->eps[0].statfd = open(filename, O_RDONLY);
740 if (hpriv->eps[0].statfd < 0) {
741 close(hpriv->eps[0].datafd);
742 hpriv->eps[0].datafd = -1;
743
744 return(_errno_to_libusb(errno));
745 }
746
747 return (LIBUSB_SUCCESS);
748 }
749
750 static void
sunos_usb_close_all_eps(sunos_dev_handle_priv_t * hdev)751 sunos_usb_close_all_eps(sunos_dev_handle_priv_t *hdev)
752 {
753 int i;
754
755 /* not close ep0 */
756 for (i = 1; i < USB_MAXENDPOINTS; i++) {
757 if (hdev->eps[i].datafd != -1) {
758 (void) close(hdev->eps[i].datafd);
759 hdev->eps[i].datafd = -1;
760 }
761 if (hdev->eps[i].statfd != -1) {
762 (void) close(hdev->eps[i].statfd);
763 hdev->eps[i].statfd = -1;
764 }
765 }
766 }
767
768 static void
sunos_usb_close_ep0(sunos_dev_handle_priv_t * hdev)769 sunos_usb_close_ep0(sunos_dev_handle_priv_t *hdev)
770 {
771 if (hdev->eps[0].datafd >= 0) {
772 close(hdev->eps[0].datafd);
773 close(hdev->eps[0].statfd);
774 hdev->eps[0].datafd = -1;
775 hdev->eps[0].statfd = -1;
776 }
777 }
778
779 static uchar_t
sunos_usb_ep_index(uint8_t ep_addr)780 sunos_usb_ep_index(uint8_t ep_addr)
781 {
782 return ((ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK) +
783 ((ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? 16 : 0));
784 }
785
786 static int
sunos_find_interface(struct libusb_device_handle * hdev,uint8_t endpoint,uint8_t * interface)787 sunos_find_interface(struct libusb_device_handle *hdev,
788 uint8_t endpoint, uint8_t *interface)
789 {
790 struct libusb_config_descriptor *config;
791 int r;
792 int iface_idx;
793
794 r = libusb_get_active_config_descriptor(hdev->dev, &config);
795 if (r < 0) {
796 return (LIBUSB_ERROR_INVALID_PARAM);
797 }
798
799 for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {
800 const struct libusb_interface *iface =
801 &config->interface[iface_idx];
802 int altsetting_idx;
803
804 for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting;
805 altsetting_idx++) {
806 const struct libusb_interface_descriptor *altsetting =
807 &iface->altsetting[altsetting_idx];
808 int ep_idx;
809
810 for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints;
811 ep_idx++) {
812 const struct libusb_endpoint_descriptor *ep =
813 &altsetting->endpoint[ep_idx];
814 if (ep->bEndpointAddress == endpoint) {
815 *interface = iface_idx;
816 libusb_free_config_descriptor(config);
817
818 return (LIBUSB_SUCCESS);
819 }
820 }
821 }
822 }
823 libusb_free_config_descriptor(config);
824
825 return (LIBUSB_ERROR_INVALID_PARAM);
826 }
827
828 static int
sunos_check_device_and_status_open(struct libusb_device_handle * hdl,uint8_t ep_addr,int ep_type)829 sunos_check_device_and_status_open(struct libusb_device_handle *hdl,
830 uint8_t ep_addr, int ep_type)
831 {
832 char filename[PATH_MAX + 1], statfilename[PATH_MAX + 1];
833 char cfg_num[16], alt_num[16];
834 int fd, fdstat, mode;
835 uint8_t ifc = 0;
836 uint8_t ep_index;
837 sunos_dev_handle_priv_t *hpriv;
838
839 usbi_dbg("open ep 0x%02x", ep_addr);
840 hpriv = usbi_get_device_handle_priv(hdl);
841 ep_index = sunos_usb_ep_index(ep_addr);
842 /* ep already opened */
843 if ((hpriv->eps[ep_index].datafd > 0) &&
844 (hpriv->eps[ep_index].statfd > 0)) {
845 usbi_dbg("ep 0x%02x already opened, return success",
846 ep_addr);
847
848 return (0);
849 }
850
851 if (sunos_find_interface(hdl, ep_addr, &ifc) < 0) {
852 usbi_dbg("can't find interface for endpoint 0x%02x",
853 ep_addr);
854
855 return (EACCES);
856 }
857
858 /* create filename */
859 if (hpriv->config_index > 0) {
860 (void) snprintf(cfg_num, sizeof(cfg_num), "cfg%d",
861 hpriv->config_index + 1);
862 } else {
863 bzero(cfg_num, sizeof(cfg_num));
864 }
865
866 if (hpriv->altsetting[ifc] > 0) {
867 (void) snprintf(alt_num, sizeof(alt_num), ".%d",
868 hpriv->altsetting[ifc]);
869 } else {
870 bzero(alt_num, sizeof(alt_num));
871 }
872
873 (void) snprintf(filename, PATH_MAX, "%s/%sif%d%s%s%d",
874 hpriv->dpriv->ugenpath, cfg_num, ifc, alt_num,
875 (ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? "in" :
876 "out", (ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK));
877 (void) snprintf(statfilename, PATH_MAX, "%sstat", filename);
878
879 /*
880 * In case configuration has been switched, the xfer endpoint needs
881 * to be opened before the status endpoint, due to a ugen issue.
882 * However, to enable the one transfer mode for an Interrupt-In pipe,
883 * the status endpoint needs to be opened before the xfer endpoint.
884 * So, open the xfer mode first and close it immediately
885 * as a workaround. This will handle the configuration switch.
886 * Then, open the status endpoint. If for an Interrupt-in pipe,
887 * write the USB_EP_INTR_ONE_XFER control to the status endpoint
888 * to enable the one transfer mode. Then, re-open the xfer mode.
889 */
890 if (ep_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
891 mode = O_RDWR;
892 } else if (ep_addr & LIBUSB_ENDPOINT_IN) {
893 mode = O_RDONLY;
894 } else {
895 mode = O_WRONLY;
896 }
897 /* Open the xfer endpoint first */
898 if ((fd = open(filename, mode)) == -1) {
899 usbi_dbg("can't open %s: errno %d (%s)", filename, errno,
900 strerror(errno));
901
902 return (errno);
903 }
904 /* And immediately close the xfer endpoint */
905 (void) close(fd);
906
907 /*
908 * Open the status endpoint.
909 * If for an Interrupt-IN pipe, need to enable the one transfer mode
910 * by writing USB_EP_INTR_ONE_XFER control to the status endpoint
911 * before opening the xfer endpoint
912 */
913 if ((ep_type == LIBUSB_TRANSFER_TYPE_INTERRUPT) &&
914 (ep_addr & LIBUSB_ENDPOINT_IN)) {
915 char control = USB_EP_INTR_ONE_XFER;
916 ssize_t count;
917
918 /* Open the status endpoint with RDWR */
919 if ((fdstat = open(statfilename, O_RDWR)) == -1) {
920 usbi_dbg("can't open %s RDWR: errno %d (%s)",
921 statfilename, errno, strerror(errno));
922
923 return (errno);
924 } else {
925 count = write(fdstat, &control, sizeof(control));
926 if (count != 1) {
927 /* this should have worked */
928 usbi_dbg("can't write to %s: errno %d (%s)",
929 statfilename, errno, strerror(errno));
930 (void) close(fdstat);
931
932 return (errno);
933 }
934 }
935 } else {
936 if ((fdstat = open(statfilename, O_RDONLY)) == -1) {
937 usbi_dbg("can't open %s: errno %d (%s)", statfilename, errno,
938 strerror(errno));
939
940 return (errno);
941 }
942 }
943
944 /* Re-open the xfer endpoint */
945 if ((fd = open(filename, mode)) == -1) {
946 usbi_dbg("can't open %s: errno %d (%s)", filename, errno,
947 strerror(errno));
948 (void) close(fdstat);
949
950 return (errno);
951 }
952
953 hpriv->eps[ep_index].datafd = fd;
954 hpriv->eps[ep_index].statfd = fdstat;
955 usbi_dbg("ep=0x%02x datafd=%d, statfd=%d", ep_addr, fd, fdstat);
956
957 return (0);
958 }
959
960 int
sunos_open(struct libusb_device_handle * handle)961 sunos_open(struct libusb_device_handle *handle)
962 {
963 sunos_dev_handle_priv_t *hpriv;
964 sunos_dev_priv_t *dpriv;
965 int i;
966 int ret;
967
968 hpriv = usbi_get_device_handle_priv(handle);
969 dpriv = usbi_get_device_priv(handle->dev);
970 hpriv->dpriv = dpriv;
971
972 /* set all file descriptors to "closed" */
973 for (i = 0; i < USB_MAXENDPOINTS; i++) {
974 hpriv->eps[i].datafd = -1;
975 hpriv->eps[i].statfd = -1;
976 }
977
978 if (sunos_kernel_driver_active(handle, 0)) {
979 /* pretend we can open the device */
980 return (LIBUSB_SUCCESS);
981 }
982
983 if ((ret = sunos_usb_open_ep0(hpriv, dpriv)) != LIBUSB_SUCCESS) {
984 usbi_dbg("fail: %d", ret);
985 return (ret);
986 }
987
988 return (LIBUSB_SUCCESS);
989 }
990
991 void
sunos_close(struct libusb_device_handle * handle)992 sunos_close(struct libusb_device_handle *handle)
993 {
994 sunos_dev_handle_priv_t *hpriv;
995
996 usbi_dbg(" ");
997
998 hpriv = usbi_get_device_handle_priv(handle);
999
1000 sunos_usb_close_all_eps(hpriv);
1001 sunos_usb_close_ep0(hpriv);
1002 }
1003
1004 int
sunos_get_active_config_descriptor(struct libusb_device * dev,void * buf,size_t len)1005 sunos_get_active_config_descriptor(struct libusb_device *dev,
1006 void *buf, size_t len)
1007 {
1008 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev);
1009 struct libusb_config_descriptor *cfg;
1010 int proplen;
1011 di_node_t node;
1012 uint8_t *rdata;
1013
1014 /*
1015 * Keep raw configuration descriptors updated, in case config
1016 * has ever been changed through setCfg.
1017 */
1018 if ((node = di_init(dpriv->phypath, DINFOCPYALL)) == DI_NODE_NIL) {
1019 usbi_dbg("di_int() failed: errno %d (%s)", errno,
1020 strerror(errno));
1021 return (LIBUSB_ERROR_IO);
1022 }
1023 proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
1024 "usb-raw-cfg-descriptors", &rdata);
1025 if (proplen <= 0) {
1026 usbi_dbg("can't find raw config descriptors");
1027
1028 return (LIBUSB_ERROR_IO);
1029 }
1030 dpriv->raw_cfgdescr = realloc(dpriv->raw_cfgdescr, proplen);
1031 if (dpriv->raw_cfgdescr == NULL) {
1032 return (LIBUSB_ERROR_NO_MEM);
1033 } else {
1034 bcopy(rdata, dpriv->raw_cfgdescr, proplen);
1035 dpriv->cfgvalue = ((struct libusb_config_descriptor *)
1036 rdata)->bConfigurationValue;
1037 }
1038 di_fini(node);
1039
1040 cfg = (struct libusb_config_descriptor *)dpriv->raw_cfgdescr;
1041 len = MIN(len, libusb_le16_to_cpu(cfg->wTotalLength));
1042 memcpy(buf, dpriv->raw_cfgdescr, len);
1043 usbi_dbg("path:%s len %zu", dpriv->phypath, len);
1044
1045 return (len);
1046 }
1047
1048 int
sunos_get_config_descriptor(struct libusb_device * dev,uint8_t idx,void * buf,size_t len)1049 sunos_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
1050 void *buf, size_t len)
1051 {
1052 UNUSED(idx);
1053 /* XXX */
1054 return(sunos_get_active_config_descriptor(dev, buf, len));
1055 }
1056
1057 int
sunos_get_configuration(struct libusb_device_handle * handle,uint8_t * config)1058 sunos_get_configuration(struct libusb_device_handle *handle, uint8_t *config)
1059 {
1060 sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev);
1061
1062 *config = dpriv->cfgvalue;
1063
1064 usbi_dbg("bConfigurationValue %u", *config);
1065
1066 return (LIBUSB_SUCCESS);
1067 }
1068
1069 int
sunos_set_configuration(struct libusb_device_handle * handle,int config)1070 sunos_set_configuration(struct libusb_device_handle *handle, int config)
1071 {
1072 sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev);
1073 sunos_dev_handle_priv_t *hpriv;
1074
1075 usbi_dbg("bConfigurationValue %d", config);
1076 hpriv = usbi_get_device_handle_priv(handle);
1077
1078 if (dpriv->ugenpath == NULL)
1079 return (LIBUSB_ERROR_NOT_SUPPORTED);
1080
1081 if (config < 1)
1082 return (LIBUSB_ERROR_NOT_SUPPORTED);
1083
1084 dpriv->cfgvalue = config;
1085 hpriv->config_index = config - 1;
1086
1087 return (LIBUSB_SUCCESS);
1088 }
1089
1090 int
sunos_claim_interface(struct libusb_device_handle * handle,uint8_t iface)1091 sunos_claim_interface(struct libusb_device_handle *handle, uint8_t iface)
1092 {
1093 UNUSED(handle);
1094
1095 usbi_dbg("iface %u", iface);
1096
1097 return (LIBUSB_SUCCESS);
1098 }
1099
1100 int
sunos_release_interface(struct libusb_device_handle * handle,uint8_t iface)1101 sunos_release_interface(struct libusb_device_handle *handle, uint8_t iface)
1102 {
1103 sunos_dev_handle_priv_t *hpriv = usbi_get_device_handle_priv(handle);
1104
1105 usbi_dbg("iface %u", iface);
1106
1107 /* XXX: can we release it? */
1108 hpriv->altsetting[iface] = 0;
1109
1110 return (LIBUSB_SUCCESS);
1111 }
1112
1113 int
sunos_set_interface_altsetting(struct libusb_device_handle * handle,uint8_t iface,uint8_t altsetting)1114 sunos_set_interface_altsetting(struct libusb_device_handle *handle, uint8_t iface,
1115 uint8_t altsetting)
1116 {
1117 sunos_dev_priv_t *dpriv = usbi_get_device_priv(handle->dev);
1118 sunos_dev_handle_priv_t *hpriv = usbi_get_device_handle_priv(handle);
1119
1120 usbi_dbg("iface %u, setting %u", iface, altsetting);
1121
1122 if (dpriv->ugenpath == NULL)
1123 return (LIBUSB_ERROR_NOT_FOUND);
1124
1125 /* XXX: can we switch altsetting? */
1126 hpriv->altsetting[iface] = altsetting;
1127
1128 return (LIBUSB_SUCCESS);
1129 }
1130
1131 static void
usb_dump_data(const void * data,size_t size)1132 usb_dump_data(const void *data, size_t size)
1133 {
1134 const uint8_t *p = data;
1135 size_t i;
1136
1137 if (getenv("LIBUSB_DEBUG") == NULL) {
1138 return;
1139 }
1140
1141 (void) fprintf(stderr, "data dump:");
1142 for (i = 0; i < size; i++) {
1143 if (i % 16 == 0) {
1144 (void) fprintf(stderr, "\n%08zx ", i);
1145 }
1146 (void) fprintf(stderr, "%02x ", p[i]);
1147 }
1148 (void) fprintf(stderr, "\n");
1149 }
1150
1151 static void
sunos_async_callback(union sigval arg)1152 sunos_async_callback(union sigval arg)
1153 {
1154 struct sunos_transfer_priv *tpriv =
1155 (struct sunos_transfer_priv *)arg.sival_ptr;
1156 struct libusb_transfer *xfer = tpriv->transfer;
1157 struct aiocb *aiocb = &tpriv->aiocb;
1158 int ret;
1159 sunos_dev_handle_priv_t *hpriv;
1160 uint8_t ep;
1161 libusb_device_handle *dev_handle;
1162
1163 dev_handle = xfer->dev_handle;
1164
1165 /* libusb can forcibly interrupt transfer in do_close() */
1166 if (dev_handle != NULL) {
1167 hpriv = usbi_get_device_handle_priv(dev_handle);
1168 ep = sunos_usb_ep_index(xfer->endpoint);
1169
1170 ret = aio_error(aiocb);
1171 if (ret != 0) {
1172 xfer->status = sunos_usb_get_status(hpriv->eps[ep].statfd);
1173 } else {
1174 xfer->actual_length =
1175 LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer)->transferred =
1176 aio_return(aiocb);
1177 }
1178
1179 usb_dump_data(xfer->buffer, xfer->actual_length);
1180
1181 usbi_dbg("ret=%d, len=%d, actual_len=%d", ret, xfer->length,
1182 xfer->actual_length);
1183
1184 /* async notification */
1185 usbi_signal_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer));
1186 }
1187 }
1188
1189 static int
sunos_do_async_io(struct libusb_transfer * transfer)1190 sunos_do_async_io(struct libusb_transfer *transfer)
1191 {
1192 int ret = -1;
1193 struct aiocb *aiocb;
1194 sunos_dev_handle_priv_t *hpriv;
1195 uint8_t ep;
1196 struct sunos_transfer_priv *tpriv;
1197
1198 usbi_dbg(" ");
1199
1200 tpriv = usbi_get_transfer_priv(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer));
1201 hpriv = usbi_get_device_handle_priv(transfer->dev_handle);
1202 ep = sunos_usb_ep_index(transfer->endpoint);
1203
1204 tpriv->transfer = transfer;
1205 aiocb = &tpriv->aiocb;
1206 bzero(aiocb, sizeof(*aiocb));
1207 aiocb->aio_fildes = hpriv->eps[ep].datafd;
1208 aiocb->aio_buf = transfer->buffer;
1209 aiocb->aio_nbytes = transfer->length;
1210 aiocb->aio_lio_opcode =
1211 ((transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK) ==
1212 LIBUSB_ENDPOINT_IN) ? LIO_READ:LIO_WRITE;
1213 aiocb->aio_sigevent.sigev_notify = SIGEV_THREAD;
1214 aiocb->aio_sigevent.sigev_value.sival_ptr = tpriv;
1215 aiocb->aio_sigevent.sigev_notify_function = sunos_async_callback;
1216
1217 if (aiocb->aio_lio_opcode == LIO_READ) {
1218 ret = aio_read(aiocb);
1219 } else {
1220 ret = aio_write(aiocb);
1221 }
1222
1223 return (ret);
1224 }
1225
1226 /* return the number of bytes read/written */
1227 static ssize_t
usb_do_io(int fd,int stat_fd,void * data,size_t size,int flag,int * status)1228 usb_do_io(int fd, int stat_fd, void *data, size_t size, int flag, int *status)
1229 {
1230 int error;
1231 ssize_t ret = -1;
1232
1233 usbi_dbg("usb_do_io(): datafd=%d statfd=%d size=0x%zx flag=%s",
1234 fd, stat_fd, size, flag? "WRITE":"READ");
1235
1236 switch (flag) {
1237 case READ:
1238 errno = 0;
1239 ret = read(fd, data, size);
1240 usb_dump_data(data, size);
1241 break;
1242 case WRITE:
1243 usb_dump_data(data, size);
1244 errno = 0;
1245 ret = write(fd, data, size);
1246 break;
1247 }
1248
1249 usbi_dbg("usb_do_io(): amount=%zd", ret);
1250
1251 if (ret < 0) {
1252 int save_errno = errno;
1253
1254 usbi_dbg("TID=%x io %s errno %d (%s)", pthread_self(),
1255 flag?"WRITE":"READ", errno, strerror(errno));
1256
1257 /* sunos_usb_get_status will do a read and overwrite errno */
1258 error = sunos_usb_get_status(stat_fd);
1259 usbi_dbg("io status=%d errno %d (%s)", error,
1260 save_errno, strerror(save_errno));
1261
1262 if (status) {
1263 *status = save_errno;
1264 }
1265
1266 return (save_errno);
1267
1268 } else if (status) {
1269 *status = 0;
1270 }
1271
1272 return (ret);
1273 }
1274
1275 static int
solaris_submit_ctrl_on_default(struct libusb_transfer * transfer)1276 solaris_submit_ctrl_on_default(struct libusb_transfer *transfer)
1277 {
1278 ssize_t ret = -1, setup_ret;
1279 int status;
1280 sunos_dev_handle_priv_t *hpriv;
1281 struct libusb_device_handle *hdl = transfer->dev_handle;
1282 uint16_t wLength;
1283 uint8_t *data = transfer->buffer;
1284
1285 hpriv = usbi_get_device_handle_priv(hdl);
1286 wLength = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;
1287
1288 if (hpriv->eps[0].datafd == -1) {
1289 usbi_dbg("ep0 not opened");
1290
1291 return (LIBUSB_ERROR_NOT_FOUND);
1292 }
1293
1294 if ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) {
1295 usbi_dbg("IN request");
1296 ret = usb_do_io(hpriv->eps[0].datafd,
1297 hpriv->eps[0].statfd, data, LIBUSB_CONTROL_SETUP_SIZE,
1298 WRITE, &status);
1299 } else {
1300 usbi_dbg("OUT request");
1301 ret = usb_do_io(hpriv->eps[0].datafd, hpriv->eps[0].statfd,
1302 transfer->buffer, transfer->length, WRITE,
1303 (int *)&transfer->status);
1304 }
1305
1306 setup_ret = ret;
1307 if (ret < (ssize_t)LIBUSB_CONTROL_SETUP_SIZE) {
1308 usbi_dbg("error sending control msg: %zd", ret);
1309
1310 return (LIBUSB_ERROR_IO);
1311 }
1312
1313 ret = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;
1314
1315 /* Read the remaining bytes for IN request */
1316 if ((wLength) && ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) ==
1317 LIBUSB_ENDPOINT_IN)) {
1318 usbi_dbg("DATA: %d", transfer->length - (int)setup_ret);
1319 ret = usb_do_io(hpriv->eps[0].datafd,
1320 hpriv->eps[0].statfd,
1321 transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE,
1322 wLength, READ, (int *)&transfer->status);
1323 }
1324
1325 if (ret >= 0) {
1326 LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->transferred = ret;
1327 }
1328 usbi_dbg("Done: ctrl data bytes %zd", ret);
1329
1330 /**
1331 * Sync transfer handling.
1332 * We should release transfer lock here and later get it back
1333 * as usbi_handle_transfer_completion() takes its own transfer lock.
1334 */
1335 usbi_mutex_unlock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock);
1336 ret = usbi_handle_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer),
1337 transfer->status);
1338 usbi_mutex_lock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock);
1339
1340 return (ret);
1341 }
1342
1343 int
sunos_clear_halt(struct libusb_device_handle * handle,unsigned char endpoint)1344 sunos_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
1345 {
1346 int ret;
1347
1348 usbi_dbg("endpoint=0x%02x", endpoint);
1349
1350 ret = libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT |
1351 LIBUSB_RECIPIENT_ENDPOINT | LIBUSB_REQUEST_TYPE_STANDARD,
1352 LIBUSB_REQUEST_CLEAR_FEATURE, 0, endpoint, NULL, 0, 1000);
1353
1354 usbi_dbg("ret=%d", ret);
1355
1356 return (ret);
1357 }
1358
1359 void
sunos_destroy_device(struct libusb_device * dev)1360 sunos_destroy_device(struct libusb_device *dev)
1361 {
1362 sunos_dev_priv_t *dpriv = usbi_get_device_priv(dev);
1363
1364 usbi_dbg("destroy everything");
1365 free(dpriv->raw_cfgdescr);
1366 free(dpriv->ugenpath);
1367 free(dpriv->phypath);
1368 }
1369
1370 int
sunos_submit_transfer(struct usbi_transfer * itransfer)1371 sunos_submit_transfer(struct usbi_transfer *itransfer)
1372 {
1373 struct libusb_transfer *transfer;
1374 struct libusb_device_handle *hdl;
1375 int err = 0;
1376
1377 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1378 hdl = transfer->dev_handle;
1379
1380 err = sunos_check_device_and_status_open(hdl,
1381 transfer->endpoint, transfer->type);
1382 if (err < 0) {
1383
1384 return (_errno_to_libusb(err));
1385 }
1386
1387 switch (transfer->type) {
1388 case LIBUSB_TRANSFER_TYPE_CONTROL:
1389 /* sync transfer */
1390 usbi_dbg("CTRL transfer: %d", transfer->length);
1391 err = solaris_submit_ctrl_on_default(transfer);
1392 break;
1393
1394 case LIBUSB_TRANSFER_TYPE_BULK:
1395 /* fallthru */
1396 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1397 if (transfer->type == LIBUSB_TRANSFER_TYPE_BULK)
1398 usbi_dbg("BULK transfer: %d", transfer->length);
1399 else
1400 usbi_dbg("INTR transfer: %d", transfer->length);
1401 err = sunos_do_async_io(transfer);
1402 break;
1403
1404 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1405 /* Isochronous/Stream is not supported */
1406
1407 /* fallthru */
1408 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
1409 if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
1410 usbi_dbg("ISOC transfer: %d", transfer->length);
1411 else
1412 usbi_dbg("BULK STREAM transfer: %d", transfer->length);
1413 err = LIBUSB_ERROR_NOT_SUPPORTED;
1414 break;
1415 }
1416
1417 return (err);
1418 }
1419
1420 int
sunos_cancel_transfer(struct usbi_transfer * itransfer)1421 sunos_cancel_transfer(struct usbi_transfer *itransfer)
1422 {
1423 sunos_xfer_priv_t *tpriv;
1424 sunos_dev_handle_priv_t *hpriv;
1425 struct libusb_transfer *transfer;
1426 struct aiocb *aiocb;
1427 uint8_t ep;
1428 int ret;
1429
1430 tpriv = usbi_get_transfer_priv(itransfer);
1431 aiocb = &tpriv->aiocb;
1432 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1433 hpriv = usbi_get_device_handle_priv(transfer->dev_handle);
1434 ep = sunos_usb_ep_index(transfer->endpoint);
1435
1436 ret = aio_cancel(hpriv->eps[ep].datafd, aiocb);
1437
1438 usbi_dbg("aio->fd=%d fd=%d ret = %d, %s", aiocb->aio_fildes,
1439 hpriv->eps[ep].datafd, ret, (ret == AIO_CANCELED)?
1440 strerror(0):strerror(errno));
1441
1442 if (ret != AIO_CANCELED) {
1443 ret = _errno_to_libusb(errno);
1444 } else {
1445 /*
1446 * we don't need to call usbi_handle_transfer_cancellation(),
1447 * because we'll handle everything in sunos_async_callback.
1448 */
1449 ret = LIBUSB_SUCCESS;
1450 }
1451
1452 return (ret);
1453 }
1454
1455 int
sunos_handle_transfer_completion(struct usbi_transfer * itransfer)1456 sunos_handle_transfer_completion(struct usbi_transfer *itransfer)
1457 {
1458 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
1459 }
1460
1461 int
_errno_to_libusb(int err)1462 _errno_to_libusb(int err)
1463 {
1464 usbi_dbg("error: %s (%d)", strerror(err), err);
1465
1466 switch (err) {
1467 case EIO:
1468 return (LIBUSB_ERROR_IO);
1469 case EACCES:
1470 return (LIBUSB_ERROR_ACCESS);
1471 case ENOENT:
1472 return (LIBUSB_ERROR_NO_DEVICE);
1473 case ENOMEM:
1474 return (LIBUSB_ERROR_NO_MEM);
1475 case ETIMEDOUT:
1476 return (LIBUSB_ERROR_TIMEOUT);
1477 }
1478
1479 return (LIBUSB_ERROR_OTHER);
1480 }
1481
1482 /*
1483 * sunos_usb_get_status:
1484 * gets status of endpoint
1485 *
1486 * Returns: ugen's last cmd status
1487 */
1488 static int
sunos_usb_get_status(int fd)1489 sunos_usb_get_status(int fd)
1490 {
1491 int status;
1492 ssize_t ret;
1493
1494 usbi_dbg("sunos_usb_get_status(): fd=%d", fd);
1495
1496 ret = read(fd, &status, sizeof(status));
1497 if (ret == sizeof(status)) {
1498 switch (status) {
1499 case USB_LC_STAT_NOERROR:
1500 usbi_dbg("No Error");
1501 break;
1502 case USB_LC_STAT_CRC:
1503 usbi_dbg("CRC Timeout Detected\n");
1504 break;
1505 case USB_LC_STAT_BITSTUFFING:
1506 usbi_dbg("Bit Stuffing Violation\n");
1507 break;
1508 case USB_LC_STAT_DATA_TOGGLE_MM:
1509 usbi_dbg("Data Toggle Mismatch\n");
1510 break;
1511 case USB_LC_STAT_STALL:
1512 usbi_dbg("End Point Stalled\n");
1513 break;
1514 case USB_LC_STAT_DEV_NOT_RESP:
1515 usbi_dbg("Device is Not Responding\n");
1516 break;
1517 case USB_LC_STAT_PID_CHECKFAILURE:
1518 usbi_dbg("PID Check Failure\n");
1519 break;
1520 case USB_LC_STAT_UNEXP_PID:
1521 usbi_dbg("Unexpected PID\n");
1522 break;
1523 case USB_LC_STAT_DATA_OVERRUN:
1524 usbi_dbg("Data Exceeded Size\n");
1525 break;
1526 case USB_LC_STAT_DATA_UNDERRUN:
1527 usbi_dbg("Less data received\n");
1528 break;
1529 case USB_LC_STAT_BUFFER_OVERRUN:
1530 usbi_dbg("Buffer Size Exceeded\n");
1531 break;
1532 case USB_LC_STAT_BUFFER_UNDERRUN:
1533 usbi_dbg("Buffer Underrun\n");
1534 break;
1535 case USB_LC_STAT_TIMEOUT:
1536 usbi_dbg("Command Timed Out\n");
1537 break;
1538 case USB_LC_STAT_NOT_ACCESSED:
1539 usbi_dbg("Not Accessed by h/w\n");
1540 break;
1541 case USB_LC_STAT_UNSPECIFIED_ERR:
1542 usbi_dbg("Unspecified Error\n");
1543 break;
1544 case USB_LC_STAT_NO_BANDWIDTH:
1545 usbi_dbg("No Bandwidth\n");
1546 break;
1547 case USB_LC_STAT_HW_ERR:
1548 usbi_dbg("Host Controller h/w Error\n");
1549 break;
1550 case USB_LC_STAT_SUSPENDED:
1551 usbi_dbg("Device was Suspended\n");
1552 break;
1553 case USB_LC_STAT_DISCONNECTED:
1554 usbi_dbg("Device was Disconnected\n");
1555 break;
1556 case USB_LC_STAT_INTR_BUF_FULL:
1557 usbi_dbg("Interrupt buffer was full\n");
1558 break;
1559 case USB_LC_STAT_INVALID_REQ:
1560 usbi_dbg("Request was Invalid\n");
1561 break;
1562 case USB_LC_STAT_INTERRUPTED:
1563 usbi_dbg("Request was Interrupted\n");
1564 break;
1565 case USB_LC_STAT_NO_RESOURCES:
1566 usbi_dbg("No resources available for "
1567 "request\n");
1568 break;
1569 case USB_LC_STAT_INTR_POLLING_FAILED:
1570 usbi_dbg("Failed to Restart Poll");
1571 break;
1572 default:
1573 usbi_dbg("Error Not Determined %d\n",
1574 status);
1575 break;
1576 }
1577 } else {
1578 usbi_dbg("read stat error: %s",strerror(errno));
1579 status = -1;
1580 }
1581
1582 return (status);
1583 }
1584
1585 const struct usbi_os_backend usbi_backend = {
1586 .name = "Solaris",
1587 .caps = 0,
1588 .get_device_list = sunos_get_device_list,
1589 .get_active_config_descriptor = sunos_get_active_config_descriptor,
1590 .get_config_descriptor = sunos_get_config_descriptor,
1591 .open = sunos_open,
1592 .close = sunos_close,
1593 .get_configuration = sunos_get_configuration,
1594 .set_configuration = sunos_set_configuration,
1595 .claim_interface = sunos_claim_interface,
1596 .release_interface = sunos_release_interface,
1597 .set_interface_altsetting = sunos_set_interface_altsetting,
1598 .clear_halt = sunos_clear_halt,
1599 .kernel_driver_active = sunos_kernel_driver_active,
1600 .detach_kernel_driver = sunos_detach_kernel_driver,
1601 .attach_kernel_driver = sunos_attach_kernel_driver,
1602 .destroy_device = sunos_destroy_device,
1603 .submit_transfer = sunos_submit_transfer,
1604 .cancel_transfer = sunos_cancel_transfer,
1605 .handle_transfer_completion = sunos_handle_transfer_completion,
1606 .device_priv_size = sizeof(sunos_dev_priv_t),
1607 .device_handle_priv_size = sizeof(sunos_dev_handle_priv_t),
1608 .transfer_priv_size = sizeof(sunos_xfer_priv_t),
1609 };
1610