sunos_usb.c 43 KB

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