summaryrefslogtreecommitdiff
path: root/libusb/os/sunos_usb.c
blob: cbca379a83ddf6c254043d8f4a9da4bc9b188d9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
/*
 *
 * Copyright (c) 2016, Oracle and/or its affiliates.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include <unistd.h>
#include <aio.h>
#include <libdevinfo.h>
#include <sys/nvpair.h>
#include <sys/devctl.h>
#include <sys/usb/clients/ugen/usb_ugen.h>
#include <errno.h>
#include <sys/usb/usba.h>
#include <sys/pci.h>

#include "libusbi.h"
#include "sunos_usb.h"

#define UPDATEDRV_PATH	"/usr/sbin/update_drv"
#define UPDATEDRV	"update_drv"

#define	DEFAULT_LISTSIZE	6

typedef struct {
	int	nargs;
	int	listsize;
	char	**string;
} string_list_t;

/*
 * Backend functions
 */
static int sunos_init(struct libusb_context *);
static void sunos_exit(struct libusb_context *);
static int sunos_get_device_list(struct libusb_context *,
    struct discovered_devs **);
static int sunos_open(struct libusb_device_handle *);
static void sunos_close(struct libusb_device_handle *);
static int sunos_get_device_descriptor(struct libusb_device *,
    uint8_t*, int *);
static int sunos_get_active_config_descriptor(struct libusb_device *,
    uint8_t*, size_t, int *);
static int sunos_get_config_descriptor(struct libusb_device *, uint8_t,
    uint8_t*, size_t, int *);
static int sunos_get_configuration(struct libusb_device_handle *, int *);
static int sunos_set_configuration(struct libusb_device_handle *, int);
static int sunos_claim_interface(struct libusb_device_handle *, int);
static int sunos_release_interface(struct libusb_device_handle *, int);
static int sunos_set_interface_altsetting(struct libusb_device_handle *,
    int, int);
static int sunos_clear_halt(struct libusb_device_handle *, uint8_t);
static int sunos_reset_device(struct libusb_device_handle *);
static void sunos_destroy_device(struct libusb_device *);
static int sunos_submit_transfer(struct usbi_transfer *);
static int sunos_cancel_transfer(struct usbi_transfer *);
static void sunos_clear_transfer_priv(struct usbi_transfer *);
static int sunos_handle_transfer_completion(struct usbi_transfer *);
static int sunos_clock_gettime(int, struct timespec *);
static int sunos_kernel_driver_active(struct libusb_device_handle *, int interface);
static int sunos_detach_kernel_driver (struct libusb_device_handle *dev, int interface_number);
static int sunos_attach_kernel_driver (struct libusb_device_handle *dev, int interface_number);
static int sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv);
static int sunos_usb_ioctl(struct libusb_device *dev, int cmd);

static struct devctl_iocdata iocdata;
static int sunos_get_link(di_devlink_t devlink, void *arg)
{
	walk_link_t *larg = (walk_link_t *)arg;
	const char *p;
	const char *q;

	if (larg->path) {
		char *content = (char *)di_devlink_content(devlink);
		char *start = strstr(content, "/devices/");
		start += strlen("/devices");
		usbi_dbg("%s", start);

		/* line content must have minor node */
		if (start == NULL ||
		    strncmp(start, larg->path, larg->len) != 0 ||
		    start[larg->len] != ':')
			return (DI_WALK_CONTINUE);
	}

	p = di_devlink_path(devlink);
	q = strrchr(p, '/');
	usbi_dbg("%s", q);

	*(larg->linkpp) = strndup(p, strlen(p) - strlen(q));

	return (DI_WALK_TERMINATE);
}


static int sunos_physpath_to_devlink(
	const char *node_path, const char *match, char **link_path)
{
	walk_link_t larg;
	di_devlink_handle_t hdl;

	*link_path = NULL;
	larg.linkpp = link_path;
	if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
		usbi_dbg("di_devlink_init failure");
		return (-1);
	}

	larg.len = strlen(node_path);
	larg.path = (char *)node_path;

	(void) di_devlink_walk(hdl, match, NULL, DI_PRIMARY_LINK,
	    (void *)&larg, sunos_get_link);

	(void) di_devlink_fini(&hdl);

	if (*link_path == NULL) {
		usbi_dbg("there is no devlink for this path");
		return (-1);
	}

	return 0;
}

static int
sunos_usb_ioctl(struct libusb_device *dev, int cmd)
{
	int fd;
	nvlist_t *nvlist;
	char *end;
	char *phypath;
	char *hubpath;
	char path_arg[PATH_MAX];
	sunos_dev_priv_t *dpriv;
	devctl_ap_state_t devctl_ap_state;

	dpriv = (sunos_dev_priv_t *)dev->os_priv;
	phypath = dpriv->phypath;

	end = strrchr(phypath, '/');
	if (end == NULL)
		return (-1);
	hubpath = strndup(phypath, end - phypath);
	if (hubpath == NULL)
		return (-1);

	end = strrchr(hubpath, '@');
	if (end == NULL) {
		free(hubpath);
		return (-1);
	}
	end++;
	usbi_dbg("unitaddr: %s", end);

	nvlist_alloc(&nvlist, NV_UNIQUE_NAME_TYPE, KM_NOSLEEP);
	nvlist_add_int32(nvlist, "port", dev->port_number);
	//find the hub path
	snprintf(path_arg, sizeof(path_arg), "/devices%s:hubd", hubpath);
	usbi_dbg("ioctl hub path: %s", path_arg);

	fd = open(path_arg, O_RDONLY);
	if (fd < 0) {
		usbi_err(DEVICE_CTX(dev), "open failed: %d (%s)", errno, strerror(errno));
		nvlist_free(nvlist);
		free(hubpath);
		return (-1);
	}

	memset(&iocdata, 0, sizeof(iocdata));
	memset(&devctl_ap_state, 0, sizeof(devctl_ap_state));

	nvlist_pack(nvlist, (char **)&iocdata.nvl_user, &iocdata.nvl_usersz, NV_ENCODE_NATIVE, 0);

	iocdata.cmd = DEVCTL_AP_GETSTATE;
	iocdata.flags = 0;
	iocdata.c_nodename = "hub";
	iocdata.c_unitaddr = end;
	iocdata.cpyout_buf = &devctl_ap_state;
	usbi_dbg("%p, %d", iocdata.nvl_user, iocdata.nvl_usersz);

	errno = 0;
	if (ioctl(fd, DEVCTL_AP_GETSTATE, &iocdata) == -1) {
		usbi_err(DEVICE_CTX(dev), "ioctl failed: fd %d, cmd %x, errno %d (%s)",
			 fd, DEVCTL_AP_GETSTATE, errno, strerror(errno));
	} else {
		usbi_dbg("dev rstate: %d", devctl_ap_state.ap_rstate);
		usbi_dbg("dev ostate: %d", devctl_ap_state.ap_ostate);
	}

	errno = 0;
	iocdata.cmd = cmd;
	if (ioctl(fd, (int)cmd, &iocdata) != 0) {
		usbi_err(DEVICE_CTX(dev), "ioctl failed: fd %d, cmd %x, errno %d (%s)",
			 fd, cmd, errno, strerror(errno));
		sleep(2);
	}

	close(fd);
	free(iocdata.nvl_user);
	nvlist_free(nvlist);
	free(hubpath);

	return (-errno);
}

static int
sunos_kernel_driver_active(struct libusb_device_handle *dev, int interface)
{
	sunos_dev_priv_t *dpriv;
	dpriv = (sunos_dev_priv_t *)dev->dev->os_priv;

	usbi_dbg("%s", dpriv->ugenpath);

	return (dpriv->ugenpath == NULL);
}

/*
 * Private functions
 */
static int _errno_to_libusb(int);
static int sunos_usb_get_status(int fd);

static int sunos_init(struct libusb_context *ctx)
{
	return (LIBUSB_SUCCESS);
}

static void sunos_exit(struct libusb_context *ctx)
{
	usbi_dbg("");
}

static string_list_t *
sunos_new_string_list(void)
{
	string_list_t *list;

	list = calloc(1, sizeof (string_list_t));
	if (list == NULL)
		return (NULL);
	list->string = calloc(DEFAULT_LISTSIZE, sizeof (char *));
	if (list->string == NULL)
		return (NULL);
	list->nargs = 0;
	list->listsize = DEFAULT_LISTSIZE;

	return (list);
}

static int
sunos_append_to_string_list(string_list_t *list, const char *arg)
{
	char	*str = strdup(arg);

	if (str == NULL)
		return (-1);

	if ((list->nargs + 1) == list->listsize) { /* +1 is for NULL */
		char	**tmp = realloc(list->string,
		    sizeof (char *) * (list->listsize + 1));
		if (tmp == NULL) {
			free(str);
			return (-1);
		}
		list->string = tmp;
		list->string[list->listsize++] = NULL;
	}
	list->string[list->nargs++] = str;

	return (0);
}

static void
sunos_free_string_list(string_list_t *list)
{
	int	i;

	for (i = 0; i < list->nargs; i++) {
		free(list->string[i]);
	}

	free(list->string);
	free(list);
}

static char **
sunos_build_argv_list(string_list_t *list)
{
	return (list->string);
}


static int
sunos_exec_command(struct libusb_context *ctx, const char *path,
	string_list_t *list)
{
	pid_t pid;
	int status;
	int waitstat;
	int exit_status;
	char **argv_list;

	argv_list = sunos_build_argv_list(list);
	if (argv_list == NULL)
		return (-1);

	pid = fork();
	if (pid == 0) {
		/* child */
		execv(path, argv_list);
		_exit(127);
	} else if (pid > 0) {
		/* parent */
		do {
			waitstat = waitpid(pid, &status, 0);
		} while ((waitstat == -1 && errno == EINTR) ||
			 (waitstat == 0 && !WIFEXITED(status) && !WIFSIGNALED(status)));

		if (waitstat == 0) {
			if (WIFEXITED(status))
				exit_status = WEXITSTATUS(status);
			else
				exit_status = WTERMSIG(status);
		} else {
			usbi_err(ctx, "waitpid failed: errno %d (%s)", errno, strerror(errno));
			exit_status = -1;
		}
	} else {
		/* fork failed */
		usbi_err(ctx, "fork failed: errno %d (%s)", errno, strerror(errno));
		exit_status = -1;
	}

	return (exit_status);
}

static int
sunos_detach_kernel_driver(struct libusb_device_handle *dev_handle,
	int interface_number)
{
	struct libusb_context *ctx = HANDLE_CTX(dev_handle);
	string_list_t *list;
	char path_arg[PATH_MAX];
	sunos_dev_priv_t *dpriv;
	int r;

	dpriv = (sunos_dev_priv_t *)dev_handle->dev->os_priv;
	snprintf(path_arg, sizeof(path_arg), "\'\"%s\"\'", dpriv->phypath);
	usbi_dbg("%s", path_arg);

	list = sunos_new_string_list();
	if (list == NULL)
		return (LIBUSB_ERROR_NO_MEM);

	/* attach ugen driver */
	r = 0;
	r |= sunos_append_to_string_list(list, UPDATEDRV);
	r |= sunos_append_to_string_list(list, "-a"); /* add rule */
	r |= sunos_append_to_string_list(list, "-i"); /* specific device */
	r |= sunos_append_to_string_list(list, path_arg); /* physical path */
	r |= sunos_append_to_string_list(list, "ugen");
	if (r) {
		sunos_free_string_list(list);
		return (LIBUSB_ERROR_NO_MEM);
	}

	r = sunos_exec_command(ctx, UPDATEDRV_PATH, list);
	sunos_free_string_list(list);
	if (r < 0)
		return (LIBUSB_ERROR_OTHER);

	/* reconfigure the driver node */
	r = 0;
	r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_DISCONNECT);
	r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE);
	if (r)
		usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed");

	snprintf(path_arg, sizeof(path_arg), "^usb/%x.%x",
	    libusb_le16_to_cpu(dpriv->dev_descr.idVendor),
	    libusb_le16_to_cpu(dpriv->dev_descr.idProduct));
	sunos_physpath_to_devlink(dpriv->phypath, path_arg, &dpriv->ugenpath);

	if (access(dpriv->ugenpath, F_OK) == -1) {
		usbi_err(HANDLE_CTX(dev_handle), "fail to detach kernel driver");
		return (LIBUSB_ERROR_IO);
	}

	return sunos_usb_open_ep0((sunos_dev_handle_priv_t *)dev_handle->os_priv, dpriv);
}

static int
sunos_attach_kernel_driver(struct libusb_device_handle *dev_handle,
	int interface_number)
{
	struct libusb_context *ctx = HANDLE_CTX(dev_handle);
	string_list_t *list;
	char path_arg[PATH_MAX];
	sunos_dev_priv_t *dpriv;
	int r;

	/* we open the dev in detach driver, so we need close it first. */
	sunos_close(dev_handle);

	dpriv = (sunos_dev_priv_t *)dev_handle->dev->os_priv;
	snprintf(path_arg, sizeof(path_arg), "\'\"%s\"\'", dpriv->phypath);
	usbi_dbg("%s", path_arg);

	list = sunos_new_string_list();
	if (list == NULL)
		return (LIBUSB_ERROR_NO_MEM);

	/* detach ugen driver */
	r = 0;
	r |= sunos_append_to_string_list(list, UPDATEDRV);
	r |= sunos_append_to_string_list(list, "-d"); /* add rule */
	r |= sunos_append_to_string_list(list, "-i"); /* specific device */
	r |= sunos_append_to_string_list(list, path_arg); /* physical path */
	r |= sunos_append_to_string_list(list, "ugen");
	if (r) {
		sunos_free_string_list(list);
		return (LIBUSB_ERROR_NO_MEM);
	}

	r = sunos_exec_command(ctx, UPDATEDRV_PATH, list);
	sunos_free_string_list(list);
	if (r < 0)
		return (LIBUSB_ERROR_OTHER);

	/* reconfigure the driver node */
	r = 0;
	r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE);
	r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_DISCONNECT);
	r |= sunos_usb_ioctl(dev_handle->dev, DEVCTL_AP_CONFIGURE);
	if (r)
		usbi_warn(HANDLE_CTX(dev_handle), "one or more ioctls failed");

	return 0;
}

static int
sunos_fill_in_dev_info(di_node_t node, struct libusb_device *dev)
{
	int	proplen;
	int	*i, n, *addr, *port_prop;
	char	*phypath;
	uint8_t	*rdata;
	struct libusb_device_descriptor	*descr;
	sunos_dev_priv_t	*dpriv = (sunos_dev_priv_t *)dev->os_priv;
	char	match_str[PATH_MAX];

	/* Device descriptors */
	proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
	    "usb-dev-descriptor", &rdata);
	if (proplen <= 0) {

		return (LIBUSB_ERROR_IO);
	}

	descr = (struct libusb_device_descriptor *)rdata;
	bcopy(descr, &dpriv->dev_descr, LIBUSB_DT_DEVICE_SIZE);
	dpriv->dev_descr.bcdUSB = libusb_cpu_to_le16(descr->bcdUSB);
	dpriv->dev_descr.idVendor = libusb_cpu_to_le16(descr->idVendor);
	dpriv->dev_descr.idProduct = libusb_cpu_to_le16(descr->idProduct);
	dpriv->dev_descr.bcdDevice = libusb_cpu_to_le16(descr->bcdDevice);

	/* Raw configuration descriptors */
	proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
	    "usb-raw-cfg-descriptors", &rdata);
	if (proplen <= 0) {
		usbi_dbg("can't find raw config descriptors");

		return (LIBUSB_ERROR_IO);
	}
	dpriv->raw_cfgdescr = calloc(1, proplen);
	if (dpriv->raw_cfgdescr == NULL) {
		return (LIBUSB_ERROR_NO_MEM);
	} else {
		bcopy(rdata, dpriv->raw_cfgdescr, proplen);
		dpriv->cfgvalue = ((struct libusb_config_descriptor *)
		    rdata)->bConfigurationValue;
	}

	n = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &port_prop);

	if ((n != 1) || (*port_prop <= 0)) {
		return (LIBUSB_ERROR_IO);
	}
	dev->port_number = *port_prop;

	/* device physical path */
	phypath = di_devfs_path(node);
	if (phypath) {
		dpriv->phypath = strdup(phypath);
		snprintf(match_str, sizeof(match_str), "^usb/%x.%x",
		    libusb_le16_to_cpu(dpriv->dev_descr.idVendor),
		    libusb_le16_to_cpu(dpriv->dev_descr.idProduct));
		usbi_dbg("match is %s", match_str);
		sunos_physpath_to_devlink(dpriv->phypath, match_str,  &dpriv->ugenpath);
		di_devfs_path_free(phypath);

	} else {
		free(dpriv->raw_cfgdescr);

		return (LIBUSB_ERROR_IO);
	}

	/* address */
	n = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "assigned-address", &addr);
	if (n != 1 || *addr == 0) {
		usbi_dbg("can't get address");
	} else {
		dev->device_address = *addr;
	}

	/* speed */
	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "low-speed", &i) >= 0) {
		dev->speed = LIBUSB_SPEED_LOW;
	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "high-speed", &i) >= 0) {
		dev->speed = LIBUSB_SPEED_HIGH;
	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "full-speed", &i) >= 0) {
		dev->speed = LIBUSB_SPEED_FULL;
	} else if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "super-speed", &i) >= 0) {
		dev->speed = LIBUSB_SPEED_SUPER;
	}

	usbi_dbg("vid=%x pid=%x, path=%s, bus_nmber=0x%x, port_number=%d, "
	    "speed=%d",
	    libusb_le16_to_cpu(dpriv->dev_descr.idVendor),
	    libusb_le16_to_cpu(dpriv->dev_descr.idProduct),
	    dpriv->phypath, dev->bus_number, dev->port_number, dev->speed);

	return (LIBUSB_SUCCESS);
}

static int
sunos_add_devices(di_devlink_t link, void *arg)
{
	struct devlink_cbarg	*largs = (struct devlink_cbarg *)arg;
	struct node_args	*nargs;
	di_node_t		myself, dn;
	uint64_t		session_id = 0;
	uint64_t		sid = 0;
	uint64_t		bdf = 0;
	struct libusb_device	*dev;
	sunos_dev_priv_t	*devpriv;
	int			n, *j;
	int			i = 0;
	int			*addr_prop;
	uint8_t			bus_number = 0;
	uint32_t * 		regbuf = NULL;
	uint32_t		reg;

	nargs = (struct node_args *)largs->nargs;
	myself = largs->myself;

	/*
	 * Construct session ID.
	 * session ID = dev_addr | hub addr |parent hub addr|...|root hub bdf
	 * 		8 bits       8bits          8 bits               16bits
	 */
	if (myself == DI_NODE_NIL)
		return (DI_WALK_CONTINUE);

	dn = myself;
	/* find the root hub */
	while (di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "root-hub", &j) != 0) {
		usbi_dbg("find_root_hub:%s", di_devfs_path(dn));
		n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn,
				"assigned-address", &addr_prop);
		session_id |= ((addr_prop[0] & 0xff) << i++ * 8);
		dn = di_parent_node(dn);
	}

	/* dn is the root hub node */
	n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "reg", (int **)&regbuf);
	reg = regbuf[0];
	bdf = (PCI_REG_BUS_G(reg) << 8) | (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg);
	/* bdf must larger than i*8 bits */
	session_id |= (bdf << i * 8);
	bus_number = (PCI_REG_DEV_G(reg) << 3) | PCI_REG_FUNC_G(reg);

	usbi_dbg("device bus address=%s:%x, name:%s",
	    di_bus_addr(myself), bus_number, di_node_name(dn));
	usbi_dbg("session id org:%lx", session_id);

	/* dn is the usb device */
	for (dn = di_child_node(myself); dn != DI_NODE_NIL; dn = di_sibling_node(dn)) {
		usbi_dbg("device path:%s", di_devfs_path(dn));
		/* skip hub devices, because its driver can not been unload */
		if (di_prop_lookup_ints(DDI_DEV_T_ANY, dn, "usb-port-count", &addr_prop) != -1)
			continue;
		/* usb_addr */
		n = di_prop_lookup_ints(DDI_DEV_T_ANY, dn,
		    "assigned-address", &addr_prop);
		if ((n != 1) || (addr_prop[0] == 0)) {
			usbi_dbg("cannot get valid usb_addr");
			continue;
		}

		sid = (session_id << 8) | (addr_prop[0] & 0xff) ;
		usbi_dbg("session id %lx", sid);

		dev = usbi_get_device_by_session_id(nargs->ctx, sid);
		if (dev == NULL) {
			dev = usbi_alloc_device(nargs->ctx, sid);
			if (dev == NULL) {
				usbi_dbg("can't alloc device");
				continue;
			}
			devpriv = (sunos_dev_priv_t *)dev->os_priv;
			dev->bus_number = bus_number;

			if (sunos_fill_in_dev_info(dn, dev) != LIBUSB_SUCCESS) {
				libusb_unref_device(dev);
				usbi_dbg("get infomation fail");
				continue;
			}
			if (usbi_sanitize_device(dev) < 0) {
				libusb_unref_device(dev);
				usbi_dbg("sanatize failed: ");
				return (DI_WALK_TERMINATE);
			}
		} else {
			devpriv = (sunos_dev_priv_t *)dev->os_priv;
			usbi_dbg("Dev %s exists", devpriv->ugenpath);
		}

		if (discovered_devs_append(*(nargs->discdevs), dev) == NULL) {
			usbi_dbg("cannot append device");
		}

		/*
		 * we alloc and hence ref this dev. We don't need to ref it
		 * hereafter. Front end or app should take care of their ref.
		 */
		libusb_unref_device(dev);

		usbi_dbg("Device %s %s id=0x%llx, devcount:%d, bdf=%x",
		    devpriv->ugenpath, di_devfs_path(dn), (uint64_t)sid,
		    (*nargs->discdevs)->len, bdf);
	}

	return (DI_WALK_CONTINUE);
}

static int
sunos_walk_minor_node_link(di_node_t node, void *args)
{
        di_minor_t minor = DI_MINOR_NIL;
        char *minor_path;
        struct devlink_cbarg arg;
	struct node_args *nargs = (struct node_args *)args;
	di_devlink_handle_t devlink_hdl = nargs->dlink_hdl;

	/* walk each minor to find usb devices */
        while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
                minor_path = di_devfs_minor_path(minor);
                arg.nargs = args;
		arg.myself = node;
                arg.minor = minor;
                (void) di_devlink_walk(devlink_hdl,
		    "^usb/hub[0-9]+", minor_path,
		    DI_PRIMARY_LINK, (void *)&arg, sunos_add_devices);
                di_devfs_path_free(minor_path);
        }

	/* switch to a different node */
	nargs->last_ugenpath = NULL;

	return (DI_WALK_CONTINUE);
}

int
sunos_get_device_list(struct libusb_context * ctx,
	struct discovered_devs **discdevs)
{
	di_node_t root_node;
	struct node_args args;
	di_devlink_handle_t devlink_hdl;

	args.ctx = ctx;
	args.discdevs = discdevs;
	args.last_ugenpath = NULL;
	if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
		usbi_dbg("di_int() failed: %s", strerror(errno));
		return (LIBUSB_ERROR_IO);
	}

	if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) {
		di_fini(root_node);
		usbi_dbg("di_devlink_init() failed: %s", strerror(errno));

		return (LIBUSB_ERROR_IO);
	}
	args.dlink_hdl = devlink_hdl;

	/* walk each node to find USB devices */
	if (di_walk_node(root_node, DI_WALK_SIBFIRST, &args,
	    sunos_walk_minor_node_link) == -1) {
		usbi_dbg("di_walk_node() failed: %s", strerror(errno));
		di_fini(root_node);

		return (LIBUSB_ERROR_IO);
	}

	di_fini(root_node);
	di_devlink_fini(&devlink_hdl);

	usbi_dbg("%d devices", (*discdevs)->len);

	return ((*discdevs)->len);
}

static int
sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv)
{
	char filename[PATH_MAX + 1];

	if (hpriv->eps[0].datafd > 0) {

		return (LIBUSB_SUCCESS);
	}
	snprintf(filename, PATH_MAX, "%s/cntrl0", dpriv->ugenpath);

	usbi_dbg("opening %s", filename);
	hpriv->eps[0].datafd = open(filename, O_RDWR);
	if (hpriv->eps[0].datafd < 0) {
		return(_errno_to_libusb(errno));
	}

	snprintf(filename, PATH_MAX, "%s/cntrl0stat", dpriv->ugenpath);
	hpriv->eps[0].statfd = open(filename, O_RDONLY);
	if (hpriv->eps[0].statfd < 0) {
		close(hpriv->eps[0].datafd);
		hpriv->eps[0].datafd = -1;

		return(_errno_to_libusb(errno));
	}

	return (LIBUSB_SUCCESS);
}

static void
sunos_usb_close_all_eps(sunos_dev_handle_priv_t *hdev)
{
	int i;

	/* not close ep0 */
	for (i = 1; i < USB_MAXENDPOINTS; i++) {
		if (hdev->eps[i].datafd != -1) {
			(void) close(hdev->eps[i].datafd);
			hdev->eps[i].datafd = -1;
		}
		if (hdev->eps[i].statfd != -1) {
			(void) close(hdev->eps[i].statfd);
			hdev->eps[i].statfd = -1;
		}
	}
}

static void
sunos_usb_close_ep0(sunos_dev_handle_priv_t *hdev, sunos_dev_priv_t *dpriv)
{
	if (hdev->eps[0].datafd >= 0) {
		close(hdev->eps[0].datafd);
		close(hdev->eps[0].statfd);
		hdev->eps[0].datafd = -1;
		hdev->eps[0].statfd = -1;
	}
}

static uchar_t
sunos_usb_ep_index(uint8_t ep_addr)
{
	return ((ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK) +
	    ((ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? 16 : 0));
}

static int
sunos_find_interface(struct libusb_device_handle *hdev,
    uint8_t endpoint, uint8_t *interface)
{
	struct libusb_config_descriptor *config;
	int r;
	int iface_idx;

	r = libusb_get_active_config_descriptor(hdev->dev, &config);
	if (r < 0) {
		return (LIBUSB_ERROR_INVALID_PARAM);
	}

	for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {
		const struct libusb_interface *iface =
		    &config->interface[iface_idx];
		int altsetting_idx;

		for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting;
		    altsetting_idx++) {
			const struct libusb_interface_descriptor *altsetting =
			    &iface->altsetting[altsetting_idx];
			int ep_idx;

			for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints;
			    ep_idx++) {
				const struct libusb_endpoint_descriptor *ep =
					&altsetting->endpoint[ep_idx];
				if (ep->bEndpointAddress == endpoint) {
					*interface = iface_idx;
					libusb_free_config_descriptor(config);

					return (LIBUSB_SUCCESS);
				}
			}
		}
	}
	libusb_free_config_descriptor(config);

	return (LIBUSB_ERROR_INVALID_PARAM);
}

static int
sunos_check_device_and_status_open(struct libusb_device_handle *hdl,
    uint8_t ep_addr, int ep_type)
{
	char	filename[PATH_MAX + 1], statfilename[PATH_MAX + 1];
	char	cfg_num[16], alt_num[16];
	int	fd, fdstat, mode;
	uint8_t	ifc = 0;
	uint8_t	ep_index;
	sunos_dev_handle_priv_t *hpriv;

	usbi_dbg("open ep 0x%02x", ep_addr);
	hpriv = (sunos_dev_handle_priv_t *)hdl->os_priv;
	ep_index = sunos_usb_ep_index(ep_addr);
	/* ep already opened */
	if ((hpriv->eps[ep_index].datafd > 0) &&
	    (hpriv->eps[ep_index].statfd > 0)) {
		usbi_dbg("ep 0x%02x already opened, return success",
			ep_addr);

		return (0);
	}

	if (sunos_find_interface(hdl, ep_addr, &ifc) < 0) {
		usbi_dbg("can't find interface for endpoint 0x%02x",
		    ep_addr);

		return (EACCES);
	}

	/* create filename */
	if (hpriv->config_index > 0) {
		(void) snprintf(cfg_num, sizeof (cfg_num), "cfg%d",
		    hpriv->config_index + 1);
	} else {
		bzero(cfg_num, sizeof (cfg_num));
	}

	if (hpriv->altsetting[ifc] > 0) {
		(void) snprintf(alt_num, sizeof (alt_num), ".%d",
		    hpriv->altsetting[ifc]);
	} else {
		bzero(alt_num, sizeof (alt_num));
	}

	(void) snprintf(filename, PATH_MAX, "%s/%sif%d%s%s%d",
	    hpriv->dpriv->ugenpath, cfg_num, ifc, alt_num,
	    (ep_addr & LIBUSB_ENDPOINT_DIR_MASK) ? "in" :
	    "out", (ep_addr & LIBUSB_ENDPOINT_ADDRESS_MASK));
	(void) snprintf(statfilename, PATH_MAX, "%sstat", filename);

	/*
	 * In case configuration has been switched, the xfer endpoint needs
	 * to be opened before the status endpoint, due to a ugen issue.
	 * However, to enable the one transfer mode for an Interrupt-In pipe,
	 * the status endpoint needs to be opened before the xfer endpoint.
	 * So, open the xfer mode first and close it immediately
	 * as a workaround. This will handle the configuration switch.
	 * Then, open the status endpoint.  If for an Interrupt-in pipe,
	 * write the USB_EP_INTR_ONE_XFER control to the status endpoint
	 * to enable the one transfer mode.  Then, re-open the xfer mode.
	 */
	if (ep_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
		mode = O_RDWR;
	} else if (ep_addr & LIBUSB_ENDPOINT_IN) {
		mode = O_RDONLY;
	} else {
		mode = O_WRONLY;
	}
	/* Open the xfer endpoint first */
	if ((fd = open(filename, mode)) == -1) {
		usbi_dbg("can't open %s: %d(%s)", filename, errno,
		    strerror(errno));

		return (errno);
	}
	/* And immediately close the xfer endpoint */
	(void) close(fd);

	/*
	 * Open the status endpoint.
	 * If for an Interrupt-IN pipe, need to enable the one transfer mode
	 * by writing USB_EP_INTR_ONE_XFER control to the status endpoint
	 * before opening the xfer endpoint
	 */
	if ((ep_type == LIBUSB_TRANSFER_TYPE_INTERRUPT) &&
	    (ep_addr & LIBUSB_ENDPOINT_IN)) {
		char	control = USB_EP_INTR_ONE_XFER;
		int	count;

		/* Open the status endpoint with RDWR */
		if ((fdstat = open(statfilename, O_RDWR)) == -1) {
			usbi_dbg("can't open %s RDWR: %d",
				statfilename, errno);

			return (errno);
		} else {
			count = write(fdstat, &control, sizeof (control));
			if (count != 1) {
				/* this should have worked */
				usbi_dbg("can't write to %s: %d",
					statfilename, errno);
				(void) close(fdstat);

				return (errno);
			}
		}
	} else {
		if ((fdstat = open(statfilename, O_RDONLY)) == -1) {
			usbi_dbg("can't open %s: %d", statfilename, errno);

			return (errno);
		}
	}

	/* Re-open the xfer endpoint */
	if ((fd = open(filename, mode)) == -1) {
		usbi_dbg("can't open %s: %d(%s)", filename, errno,
		    strerror(errno));
		(void) close(fdstat);

		return (errno);
	}

	hpriv->eps[ep_index].datafd = fd;
	hpriv->eps[ep_index].statfd = fdstat;
	usbi_dbg("ep=0x%02x datafd=%d, statfd=%d", ep_addr, fd, fdstat);

	return (0);
}

int
sunos_open(struct libusb_device_handle *handle)
{
	sunos_dev_handle_priv_t	*hpriv;
	sunos_dev_priv_t	*dpriv;
	int	i;
	int	ret;

	hpriv = (sunos_dev_handle_priv_t *)handle->os_priv;
	dpriv = (sunos_dev_priv_t *)handle->dev->os_priv;
	hpriv->dpriv = dpriv;

	/* set all file descriptors to "closed" */
	for (i = 0; i < USB_MAXENDPOINTS; i++) {
		hpriv->eps[i].datafd = -1;
		hpriv->eps[i].statfd = -1;
	}

	if (sunos_kernel_driver_active(handle, 0)) {
		/* pretend we can open the device */
		return (LIBUSB_SUCCESS);
	}

	if ((ret = sunos_usb_open_ep0(hpriv, dpriv)) != LIBUSB_SUCCESS) {
		usbi_dbg("fail: %d", ret);
		return (ret);
	}

	return (LIBUSB_SUCCESS);
}

void
sunos_close(struct libusb_device_handle *handle)
{
	sunos_dev_handle_priv_t *hpriv;
	sunos_dev_priv_t *dpriv;

	usbi_dbg("");
	if (!handle) {
		return;
	}

	hpriv = (sunos_dev_handle_priv_t *)handle->os_priv;
	if (!hpriv) {
		return;
	}
	dpriv = (sunos_dev_priv_t *)handle->dev->os_priv;
	if (!dpriv) {
		return;
	}

	sunos_usb_close_all_eps(hpriv);
	sunos_usb_close_ep0(hpriv, dpriv);
}

int
sunos_get_device_descriptor(struct libusb_device *dev, uint8_t *buf,
    int *host_endian)
{
	sunos_dev_priv_t *dpriv = (sunos_dev_priv_t *)dev->os_priv;

	memcpy(buf, &dpriv->dev_descr, LIBUSB_DT_DEVICE_SIZE);
	*host_endian = 0;

	return (LIBUSB_SUCCESS);
}

int
sunos_get_active_config_descriptor(struct libusb_device *dev,
    uint8_t *buf, size_t len, int *host_endian)
{
	sunos_dev_priv_t *dpriv = (sunos_dev_priv_t *)dev->os_priv;
	struct libusb_config_descriptor *cfg;
	int proplen;
	di_node_t node;
	uint8_t	*rdata;

	/*
	 * Keep raw configuration descriptors updated, in case config
	 * has ever been changed through setCfg.
	 */
	if ((node = di_init(dpriv->phypath, DINFOCPYALL)) == DI_NODE_NIL) {
		usbi_dbg("di_int() failed: %s", strerror(errno));
		return (LIBUSB_ERROR_IO);
	}
	proplen = di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
	    "usb-raw-cfg-descriptors", &rdata);
	if (proplen <= 0) {
		usbi_dbg("can't find raw config descriptors");

		return (LIBUSB_ERROR_IO);
	}
	dpriv->raw_cfgdescr = realloc(dpriv->raw_cfgdescr, proplen);
	if (dpriv->raw_cfgdescr == NULL) {
		return (LIBUSB_ERROR_NO_MEM);
	} else {
		bcopy(rdata, dpriv->raw_cfgdescr, proplen);
		dpriv->cfgvalue = ((struct libusb_config_descriptor *)
		    rdata)->bConfigurationValue;
	}
	di_fini(node);

	cfg = (struct libusb_config_descriptor *)dpriv->raw_cfgdescr;
	len = MIN(len, libusb_le16_to_cpu(cfg->wTotalLength));
	memcpy(buf, dpriv->raw_cfgdescr, len);
	*host_endian = 0;
	usbi_dbg("path:%s len %d", dpriv->phypath, len);

	return (len);
}

int
sunos_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
    uint8_t *buf, size_t len, int *host_endian)
{
	/* XXX */
	return(sunos_get_active_config_descriptor(dev, buf, len, host_endian));
}

int
sunos_get_configuration(struct libusb_device_handle *handle, int *config)
{
	sunos_dev_priv_t *dpriv = (sunos_dev_priv_t *)handle->dev->os_priv;

	*config = dpriv->cfgvalue;

	usbi_dbg("bConfigurationValue %d", *config);

	return (LIBUSB_SUCCESS);
}

int
sunos_set_configuration(struct libusb_device_handle *handle, int config)
{
	sunos_dev_priv_t *dpriv = (sunos_dev_priv_t *)handle->dev->os_priv;
	sunos_dev_handle_priv_t *hpriv;

	usbi_dbg("bConfigurationValue %d", config);
	hpriv = (sunos_dev_handle_priv_t *)handle->os_priv;

	if (dpriv->ugenpath == NULL)
		return (LIBUSB_ERROR_NOT_SUPPORTED);

	if (config < 1 || config > dpriv->dev_descr.bNumConfigurations)
		return (LIBUSB_ERROR_INVALID_PARAM);

	dpriv->cfgvalue = config;
	hpriv->config_index = config - 1;

	return (LIBUSB_SUCCESS);
}

int
sunos_claim_interface(struct libusb_device_handle *handle, int iface)
{
	usbi_dbg("iface %d", iface);
	if (iface < 0) {
		return (LIBUSB_ERROR_INVALID_PARAM);
	}

	return (LIBUSB_SUCCESS);
}

int
sunos_release_interface(struct libusb_device_handle *handle, int iface)
{
	sunos_dev_handle_priv_t *hpriv =
	    (sunos_dev_handle_priv_t *)handle->os_priv;

	usbi_dbg("iface %d", iface);
	if (iface < 0) {
		return (LIBUSB_ERROR_INVALID_PARAM);
	}

	/* XXX: can we release it? */
	hpriv->altsetting[iface] = 0;

	return (LIBUSB_SUCCESS);
}

int
sunos_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
    int altsetting)
{
	sunos_dev_priv_t *dpriv = (sunos_dev_priv_t *)handle->dev->os_priv;
	sunos_dev_handle_priv_t *hpriv =
	    (sunos_dev_handle_priv_t *)handle->os_priv;

	usbi_dbg("iface %d, setting %d", iface, altsetting);

	if (iface < 0 || altsetting < 0) {
		return (LIBUSB_ERROR_INVALID_PARAM);
	}
	if (dpriv->ugenpath == NULL)
		return (LIBUSB_ERROR_NOT_FOUND);

	/* XXX: can we switch altsetting? */
	hpriv->altsetting[iface] = altsetting;

	return (LIBUSB_SUCCESS);
}

static void
usb_dump_data(unsigned char *data, size_t size)
{
	int i;

	if (getenv("LIBUSB_DEBUG") == NULL) {
		return;
	}

	(void) fprintf(stderr, "data dump:");
	for (i = 0; i < size; i++) {
		if (i % 16 == 0) {
			(void) fprintf(stderr, "\n%08x	", i);
		}
		(void) fprintf(stderr, "%02x ", (uchar_t)data[i]);
	}
	(void) fprintf(stderr, "\n");
}

static void
sunos_async_callback(union sigval arg)
{
	struct sunos_transfer_priv *tpriv =
	    (struct sunos_transfer_priv *)arg.sival_ptr;
	struct libusb_transfer *xfer = tpriv->transfer;
	struct aiocb *aiocb = &tpriv->aiocb;
	int ret;
	sunos_dev_handle_priv_t *hpriv;
	uint8_t ep;
	libusb_device_handle *dev_handle;

	dev_handle = xfer->dev_handle;

	/* libusb can forcibly interrupt transfer in do_close() */
	if (dev_handle != NULL) {
		hpriv = (sunos_dev_handle_priv_t *)dev_handle->os_priv;
		ep = sunos_usb_ep_index(xfer->endpoint);

		ret = aio_error(aiocb);
		if (ret != 0) {
			xfer->status = sunos_usb_get_status(hpriv->eps[ep].statfd);
		} else {
			xfer->actual_length =
			    LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer)->transferred =
			    aio_return(aiocb);
		}

		usb_dump_data(xfer->buffer, xfer->actual_length);

		usbi_dbg("ret=%d, len=%d, actual_len=%d", ret, xfer->length,
		    xfer->actual_length);

		/* async notification */
		usbi_signal_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(xfer));
	}
}

static int
sunos_do_async_io(struct libusb_transfer *transfer)
{
	int ret = -1;
	struct aiocb *aiocb;
	sunos_dev_handle_priv_t *hpriv;
	uint8_t ep;
	struct sunos_transfer_priv *tpriv;

	usbi_dbg("");

	tpriv = usbi_transfer_get_os_priv(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer));
	hpriv = (sunos_dev_handle_priv_t *)transfer->dev_handle->os_priv;
	ep = sunos_usb_ep_index(transfer->endpoint);

	tpriv->transfer = transfer;
	aiocb = &tpriv->aiocb;
	bzero(aiocb, sizeof (*aiocb));
	aiocb->aio_fildes = hpriv->eps[ep].datafd;
	aiocb->aio_buf = transfer->buffer;
	aiocb->aio_nbytes = transfer->length;
	aiocb->aio_lio_opcode =
	    ((transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK) ==
	    LIBUSB_ENDPOINT_IN) ? LIO_READ:LIO_WRITE;
	aiocb->aio_sigevent.sigev_notify = SIGEV_THREAD;
	aiocb->aio_sigevent.sigev_value.sival_ptr = tpriv;
	aiocb->aio_sigevent.sigev_notify_function = sunos_async_callback;

	if (aiocb->aio_lio_opcode == LIO_READ) {
		ret = aio_read(aiocb);
	} else {
		ret = aio_write(aiocb);
	}

	return (ret);
}

/* return the number of bytes read/written */
static int
usb_do_io(int fd, int stat_fd, char *data, size_t size, int flag, int *status)
{
	int error;
	int ret = -1;

	usbi_dbg("usb_do_io(): datafd=%d statfd=%d size=0x%x flag=%s",
	    fd, stat_fd, size, flag? "WRITE":"READ");

	switch (flag) {
	case READ:
		errno = 0;
		ret = read(fd, data, size);
		usb_dump_data(data, size);
		break;
	case WRITE:
		usb_dump_data(data, size);
		errno = 0;
		ret = write(fd, data, size);
		break;
	}

	usbi_dbg("usb_do_io(): amount=%d", ret);

	if (ret < 0) {
		int save_errno = errno;

		usbi_dbg("TID=%x io %s errno=%d(%s) ret=%d", pthread_self(),
		    flag?"WRITE":"READ", errno, strerror(errno), ret);

		/* sunos_usb_get_status will do a read and overwrite errno */
		error = sunos_usb_get_status(stat_fd);
		usbi_dbg("io status=%d errno=%d(%s)", error,
			save_errno, strerror(save_errno));

		if (status) {
			*status = save_errno;
		}

		return (save_errno);

	} else if (status) {
		*status = 0;
	}

	return (ret);
}

static int
solaris_submit_ctrl_on_default(struct libusb_transfer *transfer)
{
	int		ret = -1, setup_ret;
	int		status;
	sunos_dev_handle_priv_t *hpriv;
	struct		libusb_device_handle *hdl = transfer->dev_handle;
	uint16_t	wLength;
	uint8_t		*data = transfer->buffer;

	hpriv = (sunos_dev_handle_priv_t *)hdl->os_priv;
	wLength = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;

	if (hpriv->eps[0].datafd == -1) {
		usbi_dbg("ep0 not opened");

		return (LIBUSB_ERROR_NOT_FOUND);
	}

	if ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) {
		usbi_dbg("IN request");
		ret = usb_do_io(hpriv->eps[0].datafd,
		    hpriv->eps[0].statfd, (char *)data, LIBUSB_CONTROL_SETUP_SIZE,
		    WRITE, (int *)&status);
	} else {
		usbi_dbg("OUT request");
		ret = usb_do_io(hpriv->eps[0].datafd, hpriv->eps[0].statfd,
		    transfer->buffer, transfer->length, WRITE,
		    (int *)&transfer->status);
	}

	setup_ret = ret;
	if (ret < LIBUSB_CONTROL_SETUP_SIZE) {
		usbi_dbg("error sending control msg: %d", ret);

		return (LIBUSB_ERROR_IO);
	}

	ret = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;

	/* Read the remaining bytes for IN request */
	if ((wLength) && ((data[0] & LIBUSB_ENDPOINT_DIR_MASK) ==
	    LIBUSB_ENDPOINT_IN)) {
		usbi_dbg("DATA: %d", transfer->length - setup_ret);
		ret = usb_do_io(hpriv->eps[0].datafd,
			hpriv->eps[0].statfd,
			(char *)transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE,
			wLength, READ, (int *)&transfer->status);
	}

	if (ret >= 0) {
		transfer->actual_length = ret;
		LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->transferred = ret;
	}
	usbi_dbg("Done: ctrl data bytes %d", ret);

	/**
	 * Sync transfer handling.
 	 * We should release transfer lock here and later get it back
	 * as usbi_handle_transfer_completion() takes its own transfer lock.
	 */
	usbi_mutex_unlock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock);
	ret = usbi_handle_transfer_completion(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer),
	    transfer->status);
	usbi_mutex_lock(&LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)->lock);

	return (ret);
}

int
sunos_clear_halt(struct libusb_device_handle *handle, uint8_t endpoint)
{
	int ret;

	usbi_dbg("endpoint=0x%02x", endpoint);

	ret = libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT |
	    LIBUSB_RECIPIENT_ENDPOINT | LIBUSB_REQUEST_TYPE_STANDARD,
	    LIBUSB_REQUEST_CLEAR_FEATURE, 0, endpoint, NULL, 0, 1000);

	usbi_dbg("ret=%d", ret);

	return (ret);
}

int
sunos_reset_device(struct libusb_device_handle *handle)
{
	usbi_dbg("");

	return (LIBUSB_ERROR_NOT_SUPPORTED);
}

void
sunos_destroy_device(struct libusb_device *dev)
{
	sunos_dev_priv_t *dpriv = (sunos_dev_priv_t *)dev->os_priv;
	usbi_dbg("destroy everyting");
	free(dpriv->raw_cfgdescr);
	free(dpriv->ugenpath);
	free(dpriv->phypath);
}

int
sunos_submit_transfer(struct usbi_transfer *itransfer)
{
	struct	libusb_transfer *transfer;
	struct	libusb_device_handle *hdl;
	int	err = 0;

	transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
	hdl = transfer->dev_handle;

	err = sunos_check_device_and_status_open(hdl,
	    transfer->endpoint, transfer->type);
	if (err < 0) {

		return (_errno_to_libusb(err));
	}

	switch (transfer->type) {
	case LIBUSB_TRANSFER_TYPE_CONTROL:
		/* sync transfer */
		usbi_dbg("CTRL transfer: %d", transfer->length);
		err = solaris_submit_ctrl_on_default(transfer);
		break;

	case LIBUSB_TRANSFER_TYPE_BULK:
		/* fallthru */
	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
		if (transfer->type == LIBUSB_TRANSFER_TYPE_BULK)
			usbi_dbg("BULK transfer: %d", transfer->length);
		else
			usbi_dbg("INTR transfer: %d", transfer->length);
		err = sunos_do_async_io(transfer);
		break;

	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
		/* Isochronous/Stream is not supported */

		/* fallthru */
	case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
		if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
			usbi_dbg("ISOC transfer: %d", transfer->length);
		else
			usbi_dbg("BULK STREAM transfer: %d", transfer->length);
		err = LIBUSB_ERROR_NOT_SUPPORTED;
		break;
	}

	return (err);
}

int
sunos_cancel_transfer(struct usbi_transfer *itransfer)
{
	sunos_xfer_priv_t	*tpriv;
	sunos_dev_handle_priv_t	*hpriv;
	struct libusb_transfer	*transfer;
	struct aiocb	*aiocb;
	uint8_t		ep;
	int		ret;

	tpriv = usbi_transfer_get_os_priv(itransfer);
	aiocb = &tpriv->aiocb;
	transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
	hpriv = (sunos_dev_handle_priv_t *)transfer->dev_handle->os_priv;
	ep = sunos_usb_ep_index(transfer->endpoint);

	ret = aio_cancel(hpriv->eps[ep].datafd, aiocb);

	usbi_dbg("aio->fd=%d fd=%d ret = %d, %s", aiocb->aio_fildes,
	    hpriv->eps[ep].datafd, ret, (ret == AIO_CANCELED)?
	    strerror(0):strerror(errno));

	if (ret != AIO_CANCELED) {
		ret = _errno_to_libusb(errno);
	} else {
	/*
	 * we don't need to call usbi_handle_transfer_cancellation(),
	 * because we'll handle everything in sunos_async_callback.
	 */
		ret = LIBUSB_SUCCESS;
	}

	return (ret);
}

void
sunos_clear_transfer_priv(struct usbi_transfer *itransfer)
{
	usbi_dbg("");

	/* Nothing to do */
}

int
sunos_handle_transfer_completion(struct usbi_transfer *itransfer)
{
	return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
}

int
sunos_clock_gettime(int clkid, struct timespec *tp)
{
	if (clkid == USBI_CLOCK_REALTIME)
		return clock_gettime(CLOCK_REALTIME, tp);

	if (clkid == USBI_CLOCK_MONOTONIC)
		return clock_gettime(CLOCK_MONOTONIC, tp);

	return (LIBUSB_ERROR_INVALID_PARAM);
}

int
_errno_to_libusb(int err)
{
	usbi_dbg("error: %s (%d)", strerror(err), err);

	switch (err) {
	case EIO:
		return (LIBUSB_ERROR_IO);
	case EACCES:
		return (LIBUSB_ERROR_ACCESS);
	case ENOENT:
		return (LIBUSB_ERROR_NO_DEVICE);
	case ENOMEM:
		return (LIBUSB_ERROR_NO_MEM);
	case ETIMEDOUT:
		return (LIBUSB_ERROR_TIMEOUT);
	}

	return (LIBUSB_ERROR_OTHER);
}

/*
 * sunos_usb_get_status:
 *	gets status of endpoint
 *
 * Returns: ugen's last cmd status
 */
static int
sunos_usb_get_status(int fd)
{
	int status, ret;

	usbi_dbg("sunos_usb_get_status(): fd=%d", fd);

	ret = read(fd, &status, sizeof (status));
	if (ret == sizeof (status)) {
		switch (status) {
		case USB_LC_STAT_NOERROR:
			usbi_dbg("No Error");
			break;
		case USB_LC_STAT_CRC:
			usbi_dbg("CRC Timeout Detected\n");
			break;
		case USB_LC_STAT_BITSTUFFING:
			usbi_dbg("Bit Stuffing Violation\n");
			break;
		case USB_LC_STAT_DATA_TOGGLE_MM:
			usbi_dbg("Data Toggle Mismatch\n");
			break;
		case USB_LC_STAT_STALL:
			usbi_dbg("End Point Stalled\n");
			break;
		case USB_LC_STAT_DEV_NOT_RESP:
			usbi_dbg("Device is Not Responding\n");
			break;
		case USB_LC_STAT_PID_CHECKFAILURE:
			usbi_dbg("PID Check Failure\n");
			break;
		case USB_LC_STAT_UNEXP_PID:
			usbi_dbg("Unexpected PID\n");
			break;
		case USB_LC_STAT_DATA_OVERRUN:
			usbi_dbg("Data Exceeded Size\n");
			break;
		case USB_LC_STAT_DATA_UNDERRUN:
			usbi_dbg("Less data received\n");
			break;
		case USB_LC_STAT_BUFFER_OVERRUN:
			usbi_dbg("Buffer Size Exceeded\n");
			break;
		case USB_LC_STAT_BUFFER_UNDERRUN:
			usbi_dbg("Buffer Underrun\n");
			break;
		case USB_LC_STAT_TIMEOUT:
			usbi_dbg("Command Timed Out\n");
			break;
		case USB_LC_STAT_NOT_ACCESSED:
			usbi_dbg("Not Accessed by h/w\n");
			break;
		case USB_LC_STAT_UNSPECIFIED_ERR:
			usbi_dbg("Unspecified Error\n");
			break;
		case USB_LC_STAT_NO_BANDWIDTH:
			usbi_dbg("No Bandwidth\n");
			break;
		case USB_LC_STAT_HW_ERR:
			usbi_dbg("Host Controller h/w Error\n");
			break;
		case USB_LC_STAT_SUSPENDED:
			usbi_dbg("Device was Suspended\n");
			break;
		case USB_LC_STAT_DISCONNECTED:
			usbi_dbg("Device was Disconnected\n");
			break;
		case USB_LC_STAT_INTR_BUF_FULL:
			usbi_dbg("Interrupt buffer was full\n");
			break;
		case USB_LC_STAT_INVALID_REQ:
			usbi_dbg("Request was Invalid\n");
			break;
		case USB_LC_STAT_INTERRUPTED:
			usbi_dbg("Request was Interrupted\n");
			break;
		case USB_LC_STAT_NO_RESOURCES:
			usbi_dbg("No resources available for "
			    "request\n");
			break;
		case USB_LC_STAT_INTR_POLLING_FAILED:
			usbi_dbg("Failed to Restart Poll");
			break;
		default:
			usbi_dbg("Error Not Determined %d\n",
			    status);
			break;
		}
	} else {
		usbi_dbg("read stat error: %s",strerror(errno));
		status = -1;
	}

	return (status);
}

#ifdef USBI_TIMERFD_AVAILABLE
static clockid_t op_get_timerfd_clockid(void)
{
       return CLOCK_MONOTONIC;
}
#endif

const struct usbi_os_backend usbi_backend = {
        .name = "Solaris",
        .caps = 0,
        .init = sunos_init,
        .exit = sunos_exit,
        .get_device_list = sunos_get_device_list,
        .get_device_descriptor = sunos_get_device_descriptor,
        .get_active_config_descriptor = sunos_get_active_config_descriptor,
        .get_config_descriptor = sunos_get_config_descriptor,
        .hotplug_poll = NULL,
        .open = sunos_open,
        .close = sunos_close,
        .get_configuration = sunos_get_configuration,
        .set_configuration = sunos_set_configuration,

        .claim_interface = sunos_claim_interface,
        .release_interface = sunos_release_interface,
        .set_interface_altsetting = sunos_set_interface_altsetting,
        .clear_halt = sunos_clear_halt,
        .reset_device = sunos_reset_device, /* TODO */
        .alloc_streams = NULL,
        .free_streams = NULL,
        .kernel_driver_active = sunos_kernel_driver_active,
        .detach_kernel_driver = sunos_detach_kernel_driver,
        .attach_kernel_driver = sunos_attach_kernel_driver,
        .destroy_device = sunos_destroy_device,
        .submit_transfer = sunos_submit_transfer,
        .cancel_transfer = sunos_cancel_transfer,
	.handle_events = NULL,
        .clear_transfer_priv = sunos_clear_transfer_priv,
        .handle_transfer_completion = sunos_handle_transfer_completion,
        .clock_gettime = sunos_clock_gettime,
#ifdef USBI_TIMERFD_AVAILABLE
        .get_timerfd_clockid = op_get_timerfd_clockid,
#endif
        .device_priv_size = sizeof(sunos_dev_priv_t),
        .device_handle_priv_size = sizeof(sunos_dev_handle_priv_t),
        .transfer_priv_size = sizeof(sunos_xfer_priv_t),
};