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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Sample skeleton USB driver.
* This driver provides a framework for developing USB client drivers.
*
* As a simplistic example, usbskel implements a data transfer by reading
* raw configuration data, which every USB device has. It is expected that
* the caller will issue an initial 4-byte read to get the total length of the
* first configuration, and follow up with a second read, passing the total
* length to read the entire configuration cloud.
*
* The device has four states (refer to usbai.h):
* USB_DEV_ONLINE: In action or ready for action.
* USB_DEV_DISCONNECTED: Hotplug removed, or device not present/correct on
* resume (CPR).
* USB_DEV_SUSPENDED: Device has been suspended along with the system.
* USB_DEV_PWRED_DOWN: Device has been powered down. (Note that this
* driver supports only two power states, powered down and
* full power.)
*
* In order to avoid race conditions between driver entry points,
* access to the device is serialized. Race conditions are an issue in
* particular between disconnect event callbacks, detach, power, open
* and data transfer callbacks. The functions usbskel_serialize/release_access
* are implemented for this purpose.
*
* Mutexes should never be held when making calls into USBA or when
* sleeping.
*
* pm_busy_component and pm_idle_component mark the device as busy or idle to
* the system. These functions are paired, and are called only from code
* bracketed by usbskel_serialize_access and usbskel_release_access.
*
* NOTE: PM and CPR will be enabled at a later release of S10.
*/
#if defined(lint) && !defined(DEBUG)
#define DEBUG
#endif
#define USBDRV_MAJOR_VER 2
#define USBDRV_MINOR_VER 0
/* Uncomment to enable Power Management, when the OS PM framework is ready. */
/*
* #ifndef USBSKEL_PM
* #define USBSKEL_PM
* #endif
*/
/*
* Uncomment to enable Check Point Resume (system suspend and resume) when the
* OS CPR framework is ready.
*/
/*
* #ifndef USBSKEL_CPR
* #define USBSKEL_CPR
* #endif
*/
#include <sys/usb/usba.h>
#include <sys/strsun.h>
#include <sys/usb/clients/usbskel/usbskel.h>
int usbskel_errlevel = USBSKEL_LOG_LOG;
static char *name = "usbskl"; /* Driver name, used all over. */
/*
* Boolean set to whether or not to dump the device's descriptor tree.
* Can be changed with the usblog_dumptree property in a usbskel.conf file.
*/
static boolean_t usbskel_dumptree;
/*
* Function Prototypes
*/
static int usbskel_attach(dev_info_t *, ddi_attach_cmd_t);
static int usbskel_detach(dev_info_t *, ddi_detach_cmd_t);
static int usbskel_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int usbskel_cleanup(dev_info_t *, usbskel_state_t *);
static int usbskel_open(dev_t *, int, int, cred_t *);
static int usbskel_close(dev_t, int, int, cred_t *);
static int usbskel_read(dev_t, struct uio *uip_p, cred_t *);
static int usbskel_strategy(struct buf *);
static void usbskel_minphys(struct buf *);
static void usbskel_normal_callback(usb_pipe_handle_t, usb_ctrl_req_t *);
static void usbskel_exception_callback(usb_pipe_handle_t, usb_ctrl_req_t *);
static int usbskel_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int usbskel_disconnect_callback(dev_info_t *);
static int usbskel_reconnect_callback(dev_info_t *);
static void usbskel_restore_device_state(dev_info_t *, usbskel_state_t *);
static int usbskel_cpr_suspend(dev_info_t *);
static void usbskel_cpr_resume(dev_info_t *);
static int usbskel_test_and_adjust_device_state(usbskel_state_t *);
static int usbskel_open_pipes(usbskel_state_t *);
static void usbskel_close_pipes(usbskel_state_t *);
static int usbskel_pm_busy_component(usbskel_state_t *);
static void usbskel_pm_idle_component(usbskel_state_t *);
static int usbskel_power(dev_info_t *, int, int);
#ifdef USBSKEL_PM
static int usbskel_init_power_mgmt(usbskel_state_t *);
static void usbskel_destroy_power_mgmt(usbskel_state_t *);
#endif
static int usbskel_serialize_access(usbskel_state_t *, boolean_t);
static void usbskel_release_access(usbskel_state_t *);
static int usbskel_check_same_device(usbskel_state_t *);
/*PRINTFLIKE3*/
static void usbskel_log(usbskel_state_t *, int, char *, ...);
/* _NOTE is an advice for locklint. Locklint checks lock use for deadlocks. */
_NOTE(SCHEME_PROTECTS_DATA("unique per call", usb_ctrl_req))
_NOTE(SCHEME_PROTECTS_DATA("unique per call", buf))
/* module loading stuff */
struct cb_ops usbskel_cb_ops = {
usbskel_open, /* open */
usbskel_close, /* close */
usbskel_strategy, /* strategy */
nulldev, /* print */
nulldev, /* dump */
usbskel_read, /* read */
nodev, /* write */
usbskel_ioctl, /* ioctl */
nulldev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
NULL, /* streamtab */
D_MP
};
static struct dev_ops usbskel_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
usbskel_info, /* info */
nulldev, /* identify */
nulldev, /* probe */
usbskel_attach, /* attach */
usbskel_detach, /* detach */
nodev, /* reset */
&usbskel_cb_ops, /* driver operations */
NULL, /* bus operations */
usbskel_power, /* power */
ddi_quiesce_not_needed, /* devo_quiesce */
};
static struct modldrv usbskel_modldrv = {
&mod_driverops,
"USB skeleton driver",
&usbskel_ops
};
static struct modlinkage modlinkage = {
MODREV_1,
&usbskel_modldrv,
NULL
};
/* local variables */
/* Soft state structures */
#define USBSKEL_INITIAL_SOFT_SPACE 1
static void *usbskel_statep;
/*
* Module-wide initialization routine.
*/
int
_init(void)
{
int rval;
usbskel_log(NULL, USBSKEL_LOG_LOG, "usbskel _init");
if ((rval = ddi_soft_state_init(&usbskel_statep,
sizeof (usbskel_state_t), USBSKEL_INITIAL_SOFT_SPACE)) != 0) {
return (rval);
}
if ((rval = mod_install(&modlinkage)) != 0) {
ddi_soft_state_fini(&usbskel_statep);
}
usbskel_log(NULL, USBSKEL_LOG_LOG, "usbskel _init done");
return (rval);
}
/*
* Module-wide tear-down routine.
*/
int
_fini(void)
{
int rval;
usbskel_log(NULL, USBSKEL_LOG_LOG, "usbskel _fini");
if ((rval = mod_remove(&modlinkage)) != 0) {
return (rval);
}
ddi_soft_state_fini(&usbskel_statep);
usbskel_log(NULL, USBSKEL_LOG_LOG, "usbskel _fini done");
return (rval);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/*
* usbskel_info:
* Get minor number, soft state structure, etc.
*/
/*ARGSUSED*/
static int
usbskel_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
void *arg, void **result)
{
usbskel_state_t *usbskelp;
int error = DDI_FAILURE;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
if ((usbskelp = ddi_get_soft_state(usbskel_statep,
getminor((dev_t)arg))) != NULL) {
*result = usbskelp->usbskel_dip;
if (*result != NULL) {
error = DDI_SUCCESS;
}
} else {
*result = NULL;
}
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)(uintptr_t)getminor((dev_t)arg);
error = DDI_SUCCESS;
break;
default:
break;
}
return (error);
}
/*
* usbskel_attach:
* Attach or resume.
*
* For attach, initialize state and device, including:
* state variables, locks, device node
* device registration with system
* power management, hotplugging
* For resume, restore device and state
*/
static int
usbskel_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int instance = ddi_get_instance(dip);
char *devinst;
int devinstlen;
usbskel_state_t *usbskelp = NULL;
usb_reg_parse_lvl_t parse_level;
usb_ep_data_t *ep_datap;
int status;
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
usbskel_cpr_resume(dip);
/*
* Always return success to work around enumeration failures.
* This works around an issue where devices which are present
* before a suspend and absent upon resume could cause a system
* panic on resume.
*/
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (ddi_soft_state_zalloc(usbskel_statep, instance) == DDI_SUCCESS) {
usbskelp = ddi_get_soft_state(usbskel_statep, instance);
}
if (usbskelp == NULL) {
return (DDI_FAILURE);
}
usbskelp->usbskel_dip = dip;
devinst = kmem_zalloc(USB_MAXSTRINGLEN, KM_SLEEP);
devinstlen = snprintf(devinst, USB_MAXSTRINGLEN, "%s%d: ",
ddi_driver_name(dip), instance);
usbskelp->usbskel_devinst = kmem_zalloc(devinstlen + 1, KM_SLEEP);
(void) strncpy(usbskelp->usbskel_devinst, devinst, devinstlen);
kmem_free(devinst, USB_MAXSTRINGLEN);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "Attach: enter for attach");
usbskel_dumptree = (ddi_prop_exists(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "usbskel_dumptree") == 1);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "Tree will %sbe dumped",
((usbskel_dumptree) ? "" : "not "));
parse_level = (usb_reg_parse_lvl_t)ddi_prop_get_int(
DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"usbskel_parse_level", USB_PARSE_LVL_ALL);
switch (parse_level) {
case USB_PARSE_LVL_NONE:
/* This driver needs a tree. */
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"parse_level requested to NOT DUMP");
parse_level = USB_PARSE_LVL_IF;
/*FALLTHROUGH*/
case USB_PARSE_LVL_IF:
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"parse_level set to dump specific interface");
break;
case USB_PARSE_LVL_CFG:
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"parse_level set to dump specific config");
break;
case USB_PARSE_LVL_ALL:
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"parse_level set to dump everything");
break;
default:
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"attach: parse_level will default to dump everything");
parse_level = USB_PARSE_LVL_ALL;
}
if ((status = usb_client_attach(dip, USBDRV_VERSION, 0)) !=
USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"attach: usb_client_attach failed, error code:%d", status);
goto fail;
}
if ((status = usb_get_dev_data(dip, &usbskelp->usbskel_reg, parse_level,
0)) != USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"attach: usb_get_dev_data failed, error code:%d", status);
goto fail;
}
if (usbskel_dumptree) {
(void) usb_print_descr_tree(
usbskelp->usbskel_dip, usbskelp->usbskel_reg);
}
/*
* Get the descriptor for an intr pipe at alt 0 of current interface.
* This will be used later to open the pipe.
*/
if ((ep_datap = usb_lookup_ep_data(dip, usbskelp->usbskel_reg,
usbskelp->usbskel_reg->dev_curr_if, 0, 0,
USB_EP_ATTR_INTR, USB_EP_DIR_IN)) == NULL) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"attach: Error getting intr endpoint descriptor");
goto fail;
}
usbskelp->usbskel_intr_ep_descr = ep_datap->ep_descr;
usb_free_descr_tree(dip, usbskelp->usbskel_reg);
mutex_init(&usbskelp->usbskel_mutex, NULL, MUTEX_DRIVER,
usbskelp->usbskel_reg->dev_iblock_cookie);
cv_init(&usbskelp->usbskel_serial_cv, NULL, CV_DRIVER, NULL);
usbskelp->usbskel_serial_inuse = B_FALSE;
usbskelp->usbskel_locks_initialized = B_TRUE;
/* create minor node */
if (ddi_create_minor_node(dip, name, S_IFCHR, instance,
"usb_skeleton", 0) != DDI_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"attach: Error creating minor node");
goto fail;
}
/* Put online before PM init as can get power managed afterward. */
usbskelp->usbskel_dev_state = USB_DEV_ONLINE;
#ifdef USBSKEL_PM
/* initialize power management */
if (usbskel_init_power_mgmt(usbskelp) != USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"attach: Could not initialize power mgmt");
}
#endif
if (usb_register_hotplug_cbs(dip, usbskel_disconnect_callback,
usbskel_reconnect_callback) != USB_SUCCESS) {
goto fail;
}
/* Report device */
ddi_report_dev(dip);
return (DDI_SUCCESS);
fail:
if (usbskelp) {
(void) usbskel_cleanup(dip, usbskelp);
}
return (DDI_FAILURE);
}
/*
* usbskel_detach:
* detach or suspend driver instance
*
* Note: in detach, only contention threads is from pm and disconnnect.
*/
static int
usbskel_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int instance = ddi_get_instance(dip);
usbskel_state_t *usbskelp =
ddi_get_soft_state(usbskel_statep, instance);
int rval = DDI_FAILURE;
switch (cmd) {
case DDI_DETACH:
mutex_enter(&usbskelp->usbskel_mutex);
ASSERT((usbskelp->usbskel_drv_state & USBSKEL_OPEN) == 0);
mutex_exit(&usbskelp->usbskel_mutex);
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"Detach: enter for detach");
rval = usbskel_cleanup(dip, usbskelp);
break;
case DDI_SUSPEND:
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"Detach: enter for suspend");
rval = usbskel_cpr_suspend(dip);
default:
break;
}
return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
}
/*
* usbskel_cleanup:
* clean up the driver state for detach
*/
static int
usbskel_cleanup(dev_info_t *dip, usbskel_state_t *usbskelp)
{
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "Cleanup: enter");
if (usbskelp->usbskel_locks_initialized) {
/* This must be done 1st to prevent more events from coming. */
usb_unregister_hotplug_cbs(dip);
/*
* At this point, no new activity can be initiated. The driver
* has disabled hotplug callbacks. The Solaris framework has
* disabled new opens on a device being detached, and does not
* allow detaching an open device.
*
* The following ensures that all driver activity has drained.
*/
mutex_enter(&usbskelp->usbskel_mutex);
(void) usbskel_serialize_access(usbskelp, USBSKEL_SER_NOSIG);
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
#ifdef USBSKEL_PM
/* All device activity has died down. */
usbskel_destroy_power_mgmt(usbskelp);
#endif
/* start dismantling */
ddi_remove_minor_node(dip, NULL);
cv_destroy(&usbskelp->usbskel_serial_cv);
mutex_destroy(&usbskelp->usbskel_mutex);
}
usb_client_detach(dip, usbskelp->usbskel_reg);
if (usbskelp->usbskel_devinst != NULL) {
kmem_free(usbskelp->usbskel_devinst,
strlen(usbskelp->usbskel_devinst) + 1);
}
ddi_soft_state_free(usbskel_statep, ddi_get_instance(dip));
ddi_prop_remove_all(dip);
return (USB_SUCCESS);
}
/*ARGSUSED*/
static int
usbskel_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
{
usbskel_state_t *usbskelp =
ddi_get_soft_state(usbskel_statep, getminor(*devp));
int rval = 0;
if (usbskelp == NULL) {
return (ENXIO);
}
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "open: enter");
/*
* Keep it simple: one client at a time.
* Exclusive open only
*/
mutex_enter(&usbskelp->usbskel_mutex);
if ((usbskelp->usbskel_drv_state & USBSKEL_OPEN) != 0) {
mutex_exit(&usbskelp->usbskel_mutex);
return (EBUSY);
}
usbskelp->usbskel_drv_state |= USBSKEL_OPEN;
/*
* This is in place so that a disconnect or CPR doesn't interfere with
* pipe opens.
*/
if (usbskel_serialize_access(usbskelp, USBSKEL_SER_SIG) == 0) {
usbskelp->usbskel_drv_state &= ~USBSKEL_OPEN;
mutex_exit(&usbskelp->usbskel_mutex);
return (EINTR);
}
mutex_exit(&usbskelp->usbskel_mutex);
if (usbskel_pm_busy_component(usbskelp) != DDI_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"open: Error raising power");
rval = EIO;
goto done;
}
mutex_enter(&usbskelp->usbskel_mutex);
/* Fail if device is no longer ready. */
if (usbskelp->usbskel_dev_state != USB_DEV_ONLINE) {
mutex_exit(&usbskelp->usbskel_mutex);
rval = EIO;
goto done;
}
mutex_exit(&usbskelp->usbskel_mutex);
if (usbskel_open_pipes(usbskelp) != USB_SUCCESS) {
rval = EIO;
goto done;
}
/* Device specific initialization goes here. */
done:
if (rval != 0) {
mutex_enter(&usbskelp->usbskel_mutex);
usbskelp->usbskel_drv_state &= ~USBSKEL_OPEN;
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
usbskel_pm_idle_component(usbskelp);
} else {
/* Device is idle until it is used. */
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
}
return (rval);
}
/*ARGSUSED*/
static int
usbskel_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
{
usbskel_state_t *usbskelp =
ddi_get_soft_state(usbskel_statep, getminor(dev));
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "close: enter");
mutex_enter(&usbskelp->usbskel_mutex);
(void) usbskel_serialize_access(usbskelp, USBSKEL_SER_NOSIG);
mutex_exit(&usbskelp->usbskel_mutex);
/* Perform device session cleanup here. */
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "close: cleaning up...");
/*
* USBA automatically flushes/resets active non-default pipes
* when they are closed. We can't reset default pipe, but we
* can wait for all requests on it from this dip to drain.
*/
(void) usb_pipe_drain_reqs(usbskelp->usbskel_dip,
usbskelp->usbskel_reg->dev_default_ph, 0,
USB_FLAGS_SLEEP, NULL, 0);
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_close_pipes(usbskelp);
usbskelp->usbskel_drv_state &= ~USBSKEL_OPEN;
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
usbskel_pm_idle_component(usbskelp);
return (0);
}
/*ARGSUSED*/
static int
usbskel_read(dev_t dev, struct uio *uio_p, cred_t *cred_p)
{
usbskel_state_t *usbskelp =
ddi_get_soft_state(usbskel_statep, getminor(dev));
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "read enter");
return (physio(usbskel_strategy, NULL, dev, B_READ,
usbskel_minphys, uio_p));
}
/*
* strategy:
* Called through physio to setup and start the transfer.
*/
static int
usbskel_strategy(struct buf *bp)
{
usbskel_state_t *usbskelp = ddi_get_soft_state(usbskel_statep,
getminor(bp->b_edev));
usb_pipe_handle_t pipe = usbskelp->usbskel_reg->dev_default_ph;
usb_ctrl_req_t *request;
int status;
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "strategy enter");
/*
* Initialize residual count here in case transfer doesn't even get
* started.
*/
bp->b_resid = bp->b_bcount;
/* Needed as this is a character driver. */
if (bp->b_flags & (B_PHYS | B_PAGEIO)) {
bp_mapin(bp);
}
mutex_enter(&usbskelp->usbskel_mutex);
(void) usbskel_serialize_access(usbskelp, USBSKEL_SER_NOSIG);
/* Make sure device has not been disconnected. */
if (usbskelp->usbskel_dev_state != USB_DEV_ONLINE) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_strategy: device can't be accessed");
mutex_exit(&usbskelp->usbskel_mutex);
goto fail;
}
mutex_exit(&usbskelp->usbskel_mutex);
/*
* Since every device has raw configuration data, set up a control
* transfer to read the raw configuration data. In a production driver
* a read would probably be done on a pipe other than the default pipe,
* and would be reading data streamed by the device.
*/
/* Allocate and initialize the request. */
if ((bp->b_private = request = usb_alloc_ctrl_req(
usbskelp->usbskel_dip, bp->b_bcount, USB_FLAGS_SLEEP)) ==
NULL) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_read: Error allocating request");
goto fail;
}
request->ctrl_bmRequestType =
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD |
USB_DEV_REQ_RCPT_DEV;
request->ctrl_bRequest = USB_REQ_GET_DESCR;
/* For now, return only the first configuration. */
request->ctrl_wValue = USB_DESCR_TYPE_SETUP_CFG | 0;
request->ctrl_wIndex = 0;
request->ctrl_wLength = bp->b_bcount;
request->ctrl_timeout = 3;
/* Autoclearing automatically set on default pipe. */
request->ctrl_attributes = USB_ATTRS_SHORT_XFER_OK;
request->ctrl_cb = usbskel_normal_callback;
request->ctrl_exc_cb = usbskel_exception_callback;
/* Hook the req to the bp, so callback knows where to put the data. */
/* Now both bp and request know about each other. */
request->ctrl_client_private = (usb_opaque_t)bp;
/*
* Issue the request asynchronously. Physio will block waiting for an
* "interrupt" which comes as a callback. The callback calls biodone
* to release physio from its wait.
*/
if ((status = usb_pipe_ctrl_xfer(pipe, request, USB_FLAGS_NOSLEEP)) !=
USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_strategy: can't start transfer: status: %d",
status);
goto fail;
}
/*
* Normally, usbskel_release_access() and usbskel_pm_idle_component
* is called in callback handler.
*/
return (0);
fail:
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
bioerror(bp, EIO);
biodone(bp);
return (0);
}
static void
usbskel_minphys(struct buf *bp)
{
/* the config cloud is limited to 64k */
if (bp->b_bcount > USBSKEL_REQUEST_SIZE) {
bp->b_bcount = USBSKEL_REQUEST_SIZE;
}
minphys(bp);
}
/*
* usbskel_normal_callback:
* Completion handler for successful transfer.
* Copy data from mblk returned by USBA, into
* buffer passed by physio, to get it back to user.
* Idle device
* update counts, etc.
* release request.
* signal completion via biodone
*/
/*ARGSUSED*/
static void
usbskel_normal_callback(usb_pipe_handle_t pipe, usb_ctrl_req_t *request)
{
struct buf *bp = (struct buf *)request->ctrl_client_private;
usbskel_state_t *usbskelp = ddi_get_soft_state(usbskel_statep,
getminor(bp->b_edev));
mblk_t *data = request->ctrl_data;
int amt_transferred = MBLKL(data);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "normal callback enter");
ASSERT((request->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0);
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"at entry, b_bcount = %lu, b_resid = %lu, trans = %d", bp->b_bcount,
bp->b_resid, amt_transferred);
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
/* Copy data out of mblk, into buffer. */
if (amt_transferred) {
bcopy(data->b_rptr, bp->b_un.b_addr, amt_transferred);
}
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"normal callback: transferring %d bytes from 0x%p to 0x%p",
amt_transferred, (void *)data, (void *)(bp->b_un.b_addr));
/* Unhook. */
bp->b_private = NULL;
request->ctrl_client_private = NULL;
/* Free request. */
usb_free_ctrl_req(request);
/* Finish up. */
bp->b_resid = bp->b_bcount - amt_transferred;
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"at exit, b_bcount = %lu, b_resid = %lu, trans = %d", bp->b_bcount,
bp->b_resid, amt_transferred);
biodone(bp);
}
/*
* usbskel_exception_callback:
* Completion handler for an erred transfer.
* Copy data from mblk returned by USBA, if any, into
* buffer passed by physio, to get it back to user.
* Idle device
* update counts, etc.
* release request.
* signal completion via biodone
*/
/*ARGSUSED*/
static void
usbskel_exception_callback(usb_pipe_handle_t pipe, usb_ctrl_req_t *request)
{
struct buf *bp = (struct buf *)request->ctrl_client_private;
usbskel_state_t *usbskelp = ddi_get_soft_state(usbskel_statep,
getminor(bp->b_edev));
mblk_t *data = request->ctrl_data;
int amt_transferred = (data ? MBLKL(data) : 0);
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"at except cb entry, b_bcount = %lu, b_resid = %lu, trans = %d",
bp->b_bcount, bp->b_resid, amt_transferred);
ASSERT((request->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0);
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
/* Copy data, if any, out of mblk, into buffer. */
if (amt_transferred) {
bcopy(data, bp->b_un.b_addr, amt_transferred);
}
bp->b_resid = bp->b_bcount - amt_transferred;
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"exception cb: req = 0x%p, cr = %d\n\t cb_flags = 0x%x "
"data = 0x%p, amt xfered = %d", (void *)request,
request->ctrl_completion_reason, request->ctrl_cb_flags,
(void *)(request->ctrl_data), amt_transferred);
/* Unhook */
bp->b_private = NULL;
request->ctrl_client_private = NULL;
/* Free request. */
usb_free_ctrl_req(request);
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"at except cb exit, b_bcount = %lu, b_resid = %lu, trans = %d",
bp->b_bcount, bp->b_resid, amt_transferred);
bioerror(bp, EIO);
biodone(bp);
}
/*
* XXX Empty ioctl for now.
*/
/*ARGSUSED*/
static int
usbskel_ioctl(dev_t dev, int cmd, intptr_t arg,
int mode, cred_t *cred_p, int *rval_p)
{
usbskel_state_t *usbskelp =
ddi_get_soft_state(usbskel_statep, getminor(dev));
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "ioctl enter");
return (ENOTTY);
}
/*
* usbskel_disconnect_callback:
* Called when device hotplug-removed.
* Close pipes. (This does not attempt to contact device.)
* Set state to DISCONNECTED
*/
static int
usbskel_disconnect_callback(dev_info_t *dip)
{
int instance = ddi_get_instance(dip);
usbskel_state_t *usbskelp =
ddi_get_soft_state(usbskel_statep, instance);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "disconnect: enter");
mutex_enter(&usbskelp->usbskel_mutex);
(void) usbskel_serialize_access(usbskelp, USBSKEL_SER_NOSIG);
/*
* Save any state of device or IO in progress required by
* usbskel_restore_device_state for proper device "thawing" later.
*/
usbskelp->usbskel_dev_state = USB_DEV_DISCONNECTED;
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
return (USB_SUCCESS);
}
/*
* usbskel_reconnect_callback:
* Called with device hotplug-inserted
* Restore state
*/
static int
usbskel_reconnect_callback(dev_info_t *dip)
{
int instance = ddi_get_instance(dip);
usbskel_state_t *usbskelp =
ddi_get_soft_state(usbskel_statep, instance);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "reconnect: enter");
mutex_enter(&usbskelp->usbskel_mutex);
(void) usbskel_serialize_access(usbskelp, USBSKEL_SER_NOSIG);
usbskel_restore_device_state(dip, usbskelp);
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
return (USB_SUCCESS);
}
/*
* usbskel_restore_device_state:
* Called during hotplug-reconnect and resume.
* reenable power management
* Verify the device is the same as before the disconnect/suspend.
* Restore device state
* Thaw any IO which was frozen.
* Quiesce device. (Other routines will activate if thawed IO.)
* Set device online.
* Leave device disconnected if there are problems.
*/
static void
usbskel_restore_device_state(dev_info_t *dip, usbskel_state_t *usbskelp)
{
int rval;
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"usbskel_restore_device_state: enter");
ASSERT(mutex_owned(&usbskelp->usbskel_mutex));
ASSERT((usbskelp->usbskel_dev_state == USB_DEV_DISCONNECTED) ||
(usbskelp->usbskel_dev_state == USB_DEV_SUSPENDED));
mutex_exit(&usbskelp->usbskel_mutex);
if (usbskel_pm_busy_component(usbskelp) != DDI_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_restore_device_state: Error raising power");
goto fail;
}
/* Check if we are talking to the same device */
if (usbskel_check_same_device(usbskelp) != USB_SUCCESS) {
goto fail;
}
mutex_enter(&usbskelp->usbskel_mutex);
if ((rval = usbskel_test_and_adjust_device_state(usbskelp)) !=
USB_SUCCESS) {
mutex_exit(&usbskelp->usbskel_mutex);
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_restore_device_state: "
"Error adjusting device: rval = %d", rval);
goto fail;
}
usbskelp->usbskel_dev_state = USB_DEV_ONLINE;
mutex_exit(&usbskelp->usbskel_mutex);
if (usbskelp->usbskel_pm) {
/* Failure here means device disappeared again. */
if (usb_handle_remote_wakeup(dip, USB_REMOTE_WAKEUP_ENABLE) !=
USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"device may or may not be accessible. "
"Please verify reconnection");
}
usbskel_pm_idle_component(usbskelp);
}
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"usbskel_restore_device_state: end");
return;
fail:
/* change the device state from suspended to disconnected */
mutex_enter(&usbskelp->usbskel_mutex);
usbskelp->usbskel_dev_state = USB_DEV_DISCONNECTED;
mutex_exit(&usbskelp->usbskel_mutex);
usbskel_pm_idle_component(usbskelp);
mutex_enter(&usbskelp->usbskel_mutex);
}
/*
* usbskel_cpr_suspend:
* Clean up device.
* Wait for any IO to finish, then close pipes.
* Quiesce device.
*/
static int
usbskel_cpr_suspend(dev_info_t *dip)
{
int instance = ddi_get_instance(dip);
usbskel_state_t *usbskelp = ddi_get_soft_state(usbskel_statep,
instance);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "suspend enter");
/* Serialize to prevent races with detach, open, device access. */
mutex_enter(&usbskelp->usbskel_mutex);
(void) usbskel_serialize_access(usbskelp, USBSKEL_SER_NOSIG);
mutex_exit(&usbskelp->usbskel_mutex);
if (usbskel_pm_busy_component(usbskelp) != DDI_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"suspend: Error raising power");
usbskel_pm_idle_component(usbskelp);
return (USB_FAILURE);
}
mutex_enter(&usbskelp->usbskel_mutex);
/*
* Set dev_state to suspended so other driver threads don't start any
* new I/O. In a real driver, there may be draining of requests done
* afterwards, and we don't want the draining to compete with new
* requests being queued.
*/
/* Don't suspend if the device is open. */
if ((usbskelp->usbskel_drv_state & USBSKEL_OPEN) != 0) {
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"suspend: Device is open. Can't suspend");
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
usbskel_pm_idle_component(usbskelp);
return (USB_FAILURE);
}
/* Access device here to clean it up. */
usbskelp->usbskel_dev_state = USB_DEV_SUSPENDED;
/*
* Save any state of device required by usbskel_restore_device_state
* for proper device "thawing" later.
*/
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
usbskel_pm_idle_component(usbskelp);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "suspend: success");
return (USB_SUCCESS);
}
/*
* usbskel_cpr_resume:
*
* usbskel_restore_device_state marks success by putting device back online
*/
static void
usbskel_cpr_resume(dev_info_t *dip)
{
int instance = ddi_get_instance(dip);
usbskel_state_t *usbskelp = ddi_get_soft_state(usbskel_statep,
instance);
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "resume: enter");
/*
* NOTE: A pm_raise_power in usbskel_restore_device_state will bring
* the power-up state of device into synch with the system.
*/
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_restore_device_state(dip, usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
}
/*
* usbskel_test_and_adjust_device_state:
* Place any device-specific initialization or sanity verification here.
*/
static int
usbskel_test_and_adjust_device_state(usbskel_state_t *usbskelp)
{
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "test and adjust enter");
return (USB_SUCCESS);
}
/*
* usbskel_open_pipes:
* Open any pipes other than default pipe.
* Mutex is assumed to be held.
*/
static int
usbskel_open_pipes(usbskel_state_t *usbskelp)
{
int rval = USB_SUCCESS;
usb_pipe_policy_t pipe_policy;
usb_pipe_handle_t pipe_handle;
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "open_pipes enter");
bzero(&pipe_policy, sizeof (pipe_policy));
/*
* Allow that pipes can support at least two asynchronous operations
* going on simultaneously. Operations include asynchronous callbacks,
* resets, closures.
*/
pipe_policy.pp_max_async_reqs = 2;
if ((rval = usb_pipe_open(usbskelp->usbskel_dip,
&usbskelp->usbskel_intr_ep_descr, &pipe_policy,
USB_FLAGS_SLEEP, &pipe_handle)) != USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_open_pipes: Error opening intr pipe: status = %d",
rval);
rval = USB_FAILURE;
}
mutex_enter(&usbskelp->usbskel_mutex);
usbskelp->usbskel_intr_ph = pipe_handle;
mutex_exit(&usbskelp->usbskel_mutex);
/*
* At this point, polling could be started on the pipe by making an
* asynchronous input request on the pipe. Allocate a request by
* calling usb_alloc_intr_req(9F) with a zero length, initialize
* attributes with USB_ATTRS_SHORT_XFER_OK | USB_ATTRS_AUTOCLEARING,
* initialize length to be packetsize of the endpoint, specify the
* callbacks. Pass this request to usb_pipe_intr_xfer to start polling.
* Call usb_pipe_stop_intr_poling(9F) to stop polling.
*/
return (rval);
}
/*
* usbskel_close_pipes:
* Close pipes. Mutex is assumed to be held.
*/
/*ARGSUSED*/
static void
usbskel_close_pipes(usbskel_state_t *usbskelp)
{
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "close_pipes enter");
if (usbskelp->usbskel_intr_ph) {
usb_pipe_handle_t pipe_handle = usbskelp->usbskel_intr_ph;
usbskelp->usbskel_intr_ph = NULL;
mutex_exit(&usbskelp->usbskel_mutex);
usb_pipe_close(usbskelp->usbskel_dip, pipe_handle,
USB_FLAGS_SLEEP, NULL, 0);
mutex_enter(&usbskelp->usbskel_mutex);
}
}
static int
usbskel_pm_busy_component(usbskel_state_t *usbskelp)
{
int rval = DDI_SUCCESS;
mutex_enter(&usbskelp->usbskel_mutex);
if (usbskelp->usbskel_pm != NULL) {
usbskelp->usbskel_pm->usbskel_pm_busy++;
mutex_exit(&usbskelp->usbskel_mutex);
if (pm_busy_component(usbskelp->usbskel_dip, 0) ==
DDI_SUCCESS) {
(void) pm_raise_power(
usbskelp->usbskel_dip, 0, USB_DEV_OS_FULL_PWR);
mutex_enter(&usbskelp->usbskel_mutex);
} else {
mutex_enter(&usbskelp->usbskel_mutex);
usbskelp->usbskel_pm->usbskel_pm_busy--;
rval = DDI_FAILURE;
}
}
mutex_exit(&usbskelp->usbskel_mutex);
return (rval);
}
static void
usbskel_pm_idle_component(usbskel_state_t *usbskelp)
{
mutex_enter(&usbskelp->usbskel_mutex);
if (usbskelp->usbskel_pm != NULL) {
mutex_exit(&usbskelp->usbskel_mutex);
if (pm_idle_component(usbskelp->usbskel_dip, 0) ==
DDI_SUCCESS) {
mutex_enter(&usbskelp->usbskel_mutex);
ASSERT(usbskelp->usbskel_pm->usbskel_pm_busy > 0);
usbskelp->usbskel_pm->usbskel_pm_busy--;
mutex_exit(&usbskelp->usbskel_mutex);
}
mutex_enter(&usbskelp->usbskel_mutex);
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"usbskel_pm_idle_component: %d",
usbskelp->usbskel_pm->usbskel_pm_busy);
}
mutex_exit(&usbskelp->usbskel_mutex);
}
/*
* usbskel_power :
* Power entry point, the workhorse behind pm_raise_power, pm_lower_power,
* usb_req_raise_power and usb_req_lower_power.
*/
/* ARGSUSED */
static int
usbskel_power(dev_info_t *dip, int comp, int level)
{
usbskel_state_t *usbskelp;
usbskel_power_t *pm;
int rval = USB_FAILURE;
usbskelp = ddi_get_soft_state(usbskel_statep, ddi_get_instance(dip));
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"usbskel_power: enter: level = %d", level);
mutex_enter(&usbskelp->usbskel_mutex);
(void) usbskel_serialize_access(usbskelp, USBSKEL_SER_NOSIG);
/*
* If we are disconnected/suspended, return success. Note that if we
* return failure, bringing down the system will hang when
* PM tries to power up all devices
*/
if ((usbskelp->usbskel_dev_state == USB_DEV_DISCONNECTED) ||
(usbskelp->usbskel_dev_state == USB_DEV_SUSPENDED)) {
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"usbskel_power: disconnected/suspended "
"dev_state=%d", usbskelp->usbskel_dev_state);
rval = USB_SUCCESS;
goto done;
}
if (usbskelp->usbskel_pm == NULL) {
goto done;
}
pm = usbskelp->usbskel_pm;
/* Check if we are transitioning to a legal power level */
if (USB_DEV_PWRSTATE_OK(pm->usbskel_pwr_states, level)) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_power: illegal power level = %d "
"pwr_states: %x", level, pm->usbskel_pwr_states);
goto done;
}
switch (level) {
case USB_DEV_OS_PWR_OFF :
/* fail attempt to go to low power if busy */
if (pm->usbskel_pm_busy) {
goto done;
}
if (usbskelp->usbskel_dev_state == USB_DEV_ONLINE) {
usbskelp->usbskel_dev_state = USB_DEV_PWRED_DOWN;
usbskelp->usbskel_pm->usbskel_current_power =
USB_DEV_OS_PWR_OFF;
} else {
rval = USB_SUCCESS;
}
break;
case USB_DEV_OS_FULL_PWR :
/*
* PM framework tries to put us in full power during system
* shutdown.
*/
usbskelp->usbskel_dev_state = USB_DEV_ONLINE;
usbskelp->usbskel_pm->usbskel_current_power =
USB_DEV_OS_FULL_PWR;
break;
/* Levels 1 and 2 are not supported by this driver to keep it simple. */
default:
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"usbskel_power: power level %d not supported", level);
break;
}
done:
usbskel_release_access(usbskelp);
mutex_exit(&usbskelp->usbskel_mutex);
return ((rval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
}
#ifdef USBSKEL_PM
/*
* usbskel_init_power_mgmt:
* Initialize power management and remote wakeup functionality.
* No mutex is necessary in this function as it's called only by attach.
*/
static int
usbskel_init_power_mgmt(usbskel_state_t *usbskelp)
{
int rval = USB_FAILURE;
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "init_power_mgmt enter");
/*
* If remote wakeup is not available you may not want to do
* power management.
*/
if (usb_handle_remote_wakeup(usbskelp->usbskel_dip,
USB_REMOTE_WAKEUP_ENABLE) == USB_SUCCESS) {
usbskel_power_t *usbskelpm;
uint_t pwr_states;
/* Allocate the state structure */
usbskelpm = kmem_zalloc(sizeof (usbskel_power_t), KM_SLEEP);
usbskelp->usbskel_pm = usbskelpm;
usbskelpm->usbskel_state = usbskelp;
usbskelpm->usbskel_pm_capabilities = 0;
usbskelpm->usbskel_current_power = USB_DEV_OS_FULL_PWR;
if ((rval = usb_create_pm_components(
usbskelp->usbskel_dip, &pwr_states)) == USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_LOG,
"usbskel_init_power_mgmt: created PM components");
usbskelpm->usbskel_pwr_states =
(uint8_t)pwr_states;
(void) pm_raise_power(
usbskelp->usbskel_dip, 0, USB_DEV_OS_FULL_PWR);
} else {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_init_power_mgmt: create_pm_compts failed");
}
} else {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_init_power_mgmt: failure enabling remote wakeup");
}
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "usbskel_init_power_mgmt: end");
return (rval);
}
/*
* usbskel_destroy_power_mgmt:
* Shut down and destroy power management and remote wakeup functionality.
*/
static void
usbskel_destroy_power_mgmt(usbskel_state_t *usbskelp)
{
usbskel_log(usbskelp, USBSKEL_LOG_LOG, "destroy_power_mgmt enter");
ASSERT(!mutex_owned(&usbskelp->usbskel_mutex));
if (usbskelp->usbskel_pm) {
(void) usbskel_pm_busy_component(usbskelp);
mutex_enter(&usbskelp->usbskel_mutex);
if (usbskelp->usbskel_dev_state != USB_DEV_DISCONNECTED) {
int rval;
mutex_exit(&usbskelp->usbskel_mutex);
if ((rval = usb_handle_remote_wakeup(
usbskelp->usbskel_dip,
USB_REMOTE_WAKEUP_DISABLE)) !=
USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_destroy_power_mgmt: "
"Error disabling rmt wakeup: rval = %d",
rval);
}
} else {
mutex_exit(&usbskelp->usbskel_mutex);
}
/*
* Since remote wakeup is disabled now,
* no one can raise power
* and get to device once power is lowered here.
*/
pm_lower_power(usbskelp->usbskel_dip, 0, USB_DEV_OS_PWR_OFF);
usbskel_pm_idle_component(usbskelp);
kmem_free(usbskelp->usbskel_pm, sizeof (usbskel_power_t));
usbskelp->usbskel_pm = NULL;
}
}
#endif
/*
* usbskel_serialize_access:
* Get the serial synchronization object before returning.
*
* Arguments:
* usbskelp - Pointer to usbskel state structure
* waitsig - Set to:
* USBSKEL_SER_SIG - to wait such that a signal can interrupt
* USBSKEL_SER_NOSIG - to wait such that a signal cannot interrupt
*/
static int
usbskel_serialize_access(usbskel_state_t *usbskelp, boolean_t waitsig)
{
int rval = 1;
ASSERT(mutex_owned(&usbskelp->usbskel_mutex));
while (usbskelp->usbskel_serial_inuse) {
if (waitsig == USBSKEL_SER_SIG) {
rval = cv_wait_sig(&usbskelp->usbskel_serial_cv,
&usbskelp->usbskel_mutex);
} else {
cv_wait(&usbskelp->usbskel_serial_cv,
&usbskelp->usbskel_mutex);
}
}
usbskelp->usbskel_serial_inuse = B_TRUE;
return (rval);
}
/*
* usbskel_release_access:
* Release the serial synchronization object.
*/
static void
usbskel_release_access(usbskel_state_t *usbskelp)
{
ASSERT(mutex_owned(&usbskelp->usbskel_mutex));
usbskelp->usbskel_serial_inuse = B_FALSE;
cv_broadcast(&usbskelp->usbskel_serial_cv);
}
/*
* usbskel_check_same_device:
* Check if the device connected to the port is the same as
* the previous device that was in the port. The previous device is
* represented by the dip on record for the port. Print a message
* if the device is different. Can block.
*
* return values:
* USB_SUCCESS: same device
* USB_INVALID_VERSION not same device
* USB_FAILURE: Failure processing request
*/
static int
usbskel_check_same_device(usbskel_state_t *usbskelp)
{
usb_dev_descr_t *orig_usb_dev_descr;
usb_dev_descr_t usb_dev_descr;
mblk_t *pdata = NULL;
int rval;
char *buf;
usb_cr_t completion_reason;
usb_cb_flags_t cb_flags;
boolean_t match = B_TRUE;
usb_ctrl_setup_t setup = {
USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD |
USB_DEV_REQ_RCPT_DEV,
USB_REQ_GET_DESCR, /* bRequest */
USB_DESCR_TYPE_SETUP_DEV, /* wValue */
0, /* wIndex */
USB_DEV_DESCR_SIZE, /* wLength */
0 /* request attributes */
};
ASSERT(!mutex_owned(&usbskelp->usbskel_mutex));
orig_usb_dev_descr = usbskelp->usbskel_reg->dev_descr;
/* get the "new" device descriptor */
rval = usb_pipe_ctrl_xfer_wait(usbskelp->usbskel_reg->dev_default_ph,
&setup, &pdata, &completion_reason, &cb_flags, USB_FLAGS_SLEEP);
if (rval != USB_SUCCESS) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"usbskel_check_same_device: "
"getting device descriptor failed "
"rval=%d, cr=%d, cb=0x%x\n",
rval, completion_reason, cb_flags);
freemsg(pdata);
return (USB_FAILURE);
}
ASSERT(pdata != NULL);
(void) usb_parse_data("2cs4c3s4c", pdata->b_rptr,
MBLKL(pdata), &usb_dev_descr,
sizeof (usb_dev_descr_t));
freemsg(pdata);
pdata = NULL;
/* Always check the device descriptor length. */
if (usb_dev_descr.bLength != USB_DEV_DESCR_SIZE) {
match = B_FALSE;
/* Always check the device descriptor. */
} else if (bcmp(orig_usb_dev_descr,
(char *)&usb_dev_descr, USB_DEV_DESCR_SIZE) != 0) {
match = B_FALSE;
}
/* if requested & this device has a serial number check and compare */
if ((match == B_TRUE) &&
(usbskelp->usbskel_reg->dev_serial != NULL)) {
buf = kmem_alloc(USB_MAXSTRINGLEN, KM_SLEEP);
if (usb_get_string_descr(usbskelp->usbskel_dip, USB_LANG_ID,
usb_dev_descr.iSerialNumber, buf,
USB_MAXSTRINGLEN) == USB_SUCCESS) {
match =
(strcmp(buf,
usbskelp->usbskel_reg->dev_serial) == 0);
}
kmem_free(buf, USB_MAXSTRINGLEN);
}
if (match == B_FALSE) {
usbskel_log(usbskelp, USBSKEL_LOG_CONSOLE,
"Device is not identical to the "
"previous one this port.\n"
"Please disconnect and reconnect");
return (USB_INVALID_VERSION);
}
return (USB_SUCCESS);
}
/*
* usbskel_log:
* Switchable logging to logfile and screen.
*
* Arguments:
* usbskelp: usbskel state pointer.
* if NULL, driver name and instance won't print with the message
* msglevel:
* if USBSKEL_LOG_LOG, goes only to logfile.
* (usbskel_errlevel must be set to USBSKEL_LOG_LOG too.)
* if USBSKEL_LOG_CONSOLE, goes to both logfile and screen
* (usbskel_errlevel can be either value for this to work.)
* cmn_err_level: error level passed to cmn_err(9F)
* format and args: as you would call cmn_err, except without special
* first routing character.
*
* Do not call this in an interrupt context, since kmem_alloc can sleep.
*/
static void
usbskel_log(usbskel_state_t *usbskelp, int msglevel, char *formatarg, ...)
{
va_list ap;
if (msglevel <= usbskel_errlevel) {
char *format;
int formatlen = strlen(formatarg) + 2; /* '!' and NULL char */
int devinst_start = 0;
/* Allocate extra room if driver name and instance is present */
if (usbskelp != NULL) {
formatlen += strlen(usbskelp->usbskel_devinst);
}
format = kmem_zalloc(formatlen, KM_SLEEP);
if (msglevel == USBSKEL_LOG_LOG) {
format[0] = '!';
devinst_start = 1;
}
if (usbskelp != NULL) {
(void) strcpy(&format[devinst_start],
usbskelp->usbskel_devinst);
}
va_start(ap, formatarg);
(void) strcat(format, formatarg);
vcmn_err(CE_CONT, format, ap);
va_end(ap);
kmem_free(format, formatlen);
}
}
|