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
|
/* $NetBSD: ustir.c,v 1.51 2022/03/12 21:15:25 riastradh Exp $ */
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by David Sainty <dsainty@NetBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.51 2022/03/12 21:15:25 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#ifdef USTIR_DEBUG_IOCTLS
#include <sys/ioctl.h>
#include <dev/usb/ustir.h>
#endif
#include <dev/usb/usb.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/ustirreg.h>
#include <dev/ir/ir.h>
#include <dev/ir/irdaio.h>
#include <dev/ir/irframevar.h>
#include <dev/ir/sir.h>
#ifdef USTIR_DEBUG
#define DPRINTFN(n,x) if (ustirdebug>(n)) printf x
int ustirdebug = 0;
#else
#define DPRINTFN(n,x)
#endif
/* Max size with framing. */
#define MAX_USTIR_OUTPUT_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + STIR_OUTPUT_HEADER_SIZE + 4)
#define USTIR_NSPEEDS 9
struct ustir_speedrec {
unsigned int speed;
unsigned int config;
};
Static struct ustir_speedrec const ustir_speeds[USTIR_NSPEEDS] = {
{ 4000000, STIR_BRMODE_4000000 },
{ 1152000, STIR_BRMODE_1152000 },
{ 576000, STIR_BRMODE_576000 },
{ 115200, STIR_BRMODE_115200 },
{ 57600, STIR_BRMODE_57600 },
{ 38400, STIR_BRMODE_38400 },
{ 19200, STIR_BRMODE_19200 },
{ 9600, STIR_BRMODE_9600 },
{ 2400, STIR_BRMODE_2400 }
};
struct ustir_softc {
device_t sc_dev;
struct usbd_device *sc_udev;
struct usbd_interface *sc_iface;
enum {
USTIR_INIT_NONE,
USTIR_INIT_INITED
} sc_init_state;
uint8_t *sc_ur_buf; /* Unencapsulated frame */
u_int sc_ur_framelen;
uint8_t *sc_rd_buf; /* Raw incoming data stream */
size_t sc_rd_index;
int sc_rd_addr;
struct usbd_pipe *sc_rd_pipe;
struct usbd_xfer *sc_rd_xfer;
u_int sc_rd_count;
int sc_rd_readinprogress;
u_int sc_rd_expectdataticks;
u_char sc_rd_err;
struct framestate sc_framestate;
struct lwp *sc_thread;
struct selinfo sc_rd_sel;
uint8_t *sc_wr_buf;
int sc_wr_addr;
int sc_wr_stalewrite;
struct usbd_xfer *sc_wr_xfer;
struct usbd_pipe *sc_wr_pipe;
struct selinfo sc_wr_sel;
enum {
udir_input, /* Receiving data */
udir_output, /* Transmitting data */
udir_stalled, /* Error preventing data flow */
udir_idle /* Neither receiving nor transmitting */
} sc_direction;
struct ustir_speedrec const *sc_speedrec;
device_t sc_child;
struct irda_params sc_params;
int sc_refcnt;
char sc_closing;
char sc_dying;
};
/* True if we cannot safely read data from the device */
#define USTIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0)
#define USTIR_WR_TIMEOUT 200
Static int ustir_open(void *, int, int, struct lwp *);
Static int ustir_close(void *, int, int, struct lwp *);
Static int ustir_read(void *, struct uio *, int);
Static int ustir_write(void *, struct uio *, int);
Static int ustir_set_params(void *, struct irda_params *);
Static int ustir_get_speeds(void *, int *);
Static int ustir_get_turnarounds(void *, int *);
Static int ustir_poll(void *, int, struct lwp *);
Static int ustir_kqfilter(void *, struct knote *);
#ifdef USTIR_DEBUG_IOCTLS
Static int ustir_ioctl(void *, u_long, void *, int, struct lwp *);
#endif
Static struct irframe_methods const ustir_methods = {
ustir_open, ustir_close, ustir_read, ustir_write, ustir_poll,
ustir_kqfilter, ustir_set_params, ustir_get_speeds,
ustir_get_turnarounds,
#ifdef USTIR_DEBUG_IOCTLS
ustir_ioctl
#endif
};
Static void ustir_rd_cb(struct usbd_xfer *, void *, usbd_status);
Static usbd_status ustir_start_read(struct ustir_softc *);
Static void ustir_periodic(struct ustir_softc *);
Static void ustir_thread(void *);
static usbd_status
ustir_read_reg(struct ustir_softc *sc, unsigned int reg, uint8_t *data)
{
usb_device_request_t req;
req.bmRequestType = UT_READ_VENDOR_DEVICE;
req.bRequest = STIR_CMD_READMULTIREG;
USETW(req.wValue, 0);
USETW(req.wIndex, reg);
USETW(req.wLength, 1);
return usbd_do_request(sc->sc_udev, &req, data);
}
static usbd_status
ustir_write_reg(struct ustir_softc *sc, unsigned int reg, uint8_t data)
{
usb_device_request_t req;
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = STIR_CMD_WRITESINGLEREG;
USETW(req.wValue, data);
USETW(req.wIndex, reg);
USETW(req.wLength, 0);
return usbd_do_request(sc->sc_udev, &req, NULL);
}
#ifdef USTIR_DEBUG
static void
ustir_dumpdata(uint8_t const *data, size_t dlen, char const *desc)
{
size_t bdindex;
printf("%s: (%lx)", desc, (unsigned long)dlen);
for (bdindex = 0; bdindex < dlen; bdindex++)
printf(" %02x", (unsigned int)data[bdindex]);
printf("\n");
}
#endif
static int ustir_match(device_t, cfdata_t, void *);
static void ustir_attach(device_t, device_t, void *);
static void ustir_childdet(device_t, device_t);
static int ustir_detach(device_t, int);
static int ustir_activate(device_t, enum devact);
CFATTACH_DECL2_NEW(ustir, sizeof(struct ustir_softc), ustir_match,
ustir_attach, ustir_detach, ustir_activate, NULL, ustir_childdet);
static int
ustir_match(device_t parent, cfdata_t match, void *aux)
{
struct usb_attach_arg *uaa = aux;
DPRINTFN(50,("ustir_match\n"));
if (uaa->uaa_vendor == USB_VENDOR_SIGMATEL &&
uaa->uaa_product == USB_PRODUCT_SIGMATEL_IRDA)
return UMATCH_VENDOR_PRODUCT;
return UMATCH_NONE;
}
static void
ustir_attach(device_t parent, device_t self, void *aux)
{
struct ustir_softc *sc = device_private(self);
struct usb_attach_arg *uaa = aux;
struct usbd_device *dev = uaa->uaa_device;
struct usbd_interface *iface;
char *devinfop;
usb_endpoint_descriptor_t *ed;
uint8_t epcount;
int i;
struct ir_attach_args ia;
DPRINTFN(10,("ustir_attach: sc=%p\n", sc));
sc->sc_dev = self;
sc->sc_init_state = USTIR_INIT_NONE;
aprint_naive("\n");
aprint_normal("\n");
devinfop = usbd_devinfo_alloc(dev, 0);
aprint_normal_dev(self, "%s\n", devinfop);
usbd_devinfo_free(devinfop);
if (usbd_set_config_index(dev, 0, 1)
|| usbd_device2interface_handle(dev, 0, &iface)) {
aprint_error_dev(self, "Configuration failed\n");
return;
}
sc->sc_udev = dev;
sc->sc_iface = iface;
epcount = 0;
(void)usbd_endpoint_count(iface, &epcount);
sc->sc_rd_addr = -1;
sc->sc_wr_addr = -1;
for (i = 0; i < epcount; i++) {
ed = usbd_interface2endpoint_descriptor(iface, i);
if (ed == NULL) {
aprint_error_dev(self, "couldn't get ep %d\n", i);
return;
}
if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
sc->sc_rd_addr = ed->bEndpointAddress;
} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
sc->sc_wr_addr = ed->bEndpointAddress;
}
}
if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
aprint_error_dev(self, "missing endpoint\n");
return;
}
DPRINTFN(10, ("ustir_attach: %p\n", sc->sc_udev));
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
ia.ia_type = IR_TYPE_IRFRAME;
ia.ia_methods = &ustir_methods;
ia.ia_handle = sc;
sc->sc_child = config_found(self, &ia, ir_print, CFARGS_NONE);
selinit(&sc->sc_rd_sel);
selinit(&sc->sc_wr_sel);
sc->sc_init_state = USTIR_INIT_INITED;
return;
}
static void
ustir_childdet(device_t self, device_t child)
{
struct ustir_softc *sc = device_private(self);
KASSERT(sc->sc_child == child);
sc->sc_child = NULL;
}
static int
ustir_detach(device_t self, int flags)
{
struct ustir_softc *sc = device_private(self);
int s;
int rv = 0;
DPRINTFN(0, ("ustir_detach: sc=%p flags=%d\n", sc, flags));
sc->sc_closing = sc->sc_dying = 1;
wakeup(&sc->sc_thread);
while (sc->sc_thread != NULL)
tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
/* Abort all pipes. Causes processes waiting for transfer to wake. */
if (sc->sc_rd_pipe != NULL) {
usbd_abort_pipe(sc->sc_rd_pipe);
}
if (sc->sc_wr_pipe != NULL) {
usbd_abort_pipe(sc->sc_wr_pipe);
}
if (sc->sc_rd_xfer != NULL) {
usbd_destroy_xfer(sc->sc_rd_xfer);
sc->sc_rd_xfer = NULL;
sc->sc_rd_buf = NULL;
}
if (sc->sc_wr_xfer != NULL) {
usbd_destroy_xfer(sc->sc_wr_xfer);
sc->sc_wr_xfer = NULL;
sc->sc_wr_buf = NULL;
}
if (sc->sc_rd_pipe != NULL) {
usbd_close_pipe(sc->sc_rd_pipe);
sc->sc_rd_pipe = NULL;
}
if (sc->sc_wr_pipe != NULL) {
usbd_close_pipe(sc->sc_wr_pipe);
sc->sc_wr_pipe = NULL;
}
wakeup(&sc->sc_ur_framelen);
wakeup(&sc->sc_wr_buf);
s = splusb();
if (--sc->sc_refcnt >= 0) {
/* Wait for processes to go away. */
usb_detach_waitold(sc->sc_dev);
}
splx(s);
if (sc->sc_child != NULL)
rv = config_detach(sc->sc_child, flags);
if (sc->sc_init_state >= USTIR_INIT_INITED) {
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
sc->sc_dev);
seldestroy(&sc->sc_rd_sel);
seldestroy(&sc->sc_wr_sel);
}
return rv;
}
/* Returns 0 if more data required, 1 if a complete frame was extracted */
static int
deframe_rd_ur(struct ustir_softc *sc)
{
while (sc->sc_rd_index < sc->sc_rd_count) {
uint8_t const *buf;
size_t buflen;
enum frameresult fresult;
buf = &sc->sc_rd_buf[sc->sc_rd_index];
buflen = sc->sc_rd_count - sc->sc_rd_index;
fresult = deframe_process(&sc->sc_framestate, &buf, &buflen);
sc->sc_rd_index = sc->sc_rd_count - buflen;
DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult));
switch (fresult) {
case FR_IDLE:
case FR_INPROGRESS:
case FR_FRAMEBADFCS:
case FR_FRAMEMALFORMED:
case FR_BUFFEROVERRUN:
break;
case FR_FRAMEOK:
sc->sc_ur_framelen = sc->sc_framestate.bufindex;
wakeup(&sc->sc_ur_framelen); /* XXX should use flag */
selnotify(&sc->sc_rd_sel, 0, 0);
return 1;
}
}
/* Reset indices into USB-side buffer */
sc->sc_rd_index = sc->sc_rd_count = 0;
return 0;
}
/*
* Direction transitions:
*
* ustir_periodic() can switch the direction from:
*
* output -> idle
* output -> stalled
* stalled -> idle
* idle -> input
*
* ustir_rd_cb() can switch the direction from:
*
* input -> stalled
* input -> idle
*
* ustir_write() can switch the direction from:
*
* idle -> output
*/
Static void
ustir_periodic(struct ustir_softc *sc)
{
DPRINTFN(60, ("%s: direction = %d\n",
__func__, sc->sc_direction));
if (sc->sc_direction == udir_output ||
sc->sc_direction == udir_stalled) {
usbd_status err;
uint8_t regval;
DPRINTFN(60, ("%s: reading status register\n",
__func__));
err = ustir_read_reg(sc, STIR_REG_STATUS,
®val);
if (err != USBD_NORMAL_COMPLETION) {
aprint_error_dev(sc->sc_dev,
"status register read failed: %s\n",
usbd_errstr(err));
} else {
DPRINTFN(10, ("%s: status register = %#x\n",
__func__,
(unsigned int)regval));
if (sc->sc_direction == udir_output &&
!(regval & STIR_RSTATUS_FFDIR))
/* Output has completed */
sc->sc_direction = udir_idle;
if (regval & STIR_RSTATUS_FFOVER) {
/*
* On an overrun the FIFO hangs, and
* any data bulk transfers will stall.
* Reset the FIFO.
*/
sc->sc_direction = udir_stalled;
DPRINTFN(10, ("%s: clearing FIFO error\n",
__func__));
err = ustir_write_reg(sc, STIR_REG_STATUS,
STIR_RSTATUS_FFCLR);
/* XXX if we fail partway through
* this, we may not recover? */
if (err == USBD_NORMAL_COMPLETION)
err = ustir_write_reg(sc,
STIR_REG_STATUS,
0);
if (err != USBD_NORMAL_COMPLETION) {
aprint_error_dev(sc->sc_dev,
"FIFO reset failed: %s\n",
usbd_errstr(err));
} else {
/* FIFO reset */
sc->sc_direction = udir_idle;
}
}
}
}
if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) {
/*
* In a stale write case, we need to check if the
* write has completed. Once that has happened, the
* write is no longer stale.
*
* But note that we may immediately start a read poll...
*/
sc->sc_wr_stalewrite = 0;
wakeup(&sc->sc_wr_buf);
}
if (!sc->sc_rd_readinprogress &&
(sc->sc_direction == udir_idle ||
sc->sc_direction == udir_input))
/* Do a read poll if appropriate... */
ustir_start_read(sc);
}
Static void
ustir_thread(void *arg)
{
struct ustir_softc *sc = arg;
DPRINTFN(20, ("%s: starting polling thread\n", __func__));
while (!sc->sc_closing) {
if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
ustir_periodic(sc);
if (!sc->sc_closing) {
int error;
error = tsleep(&sc->sc_thread, PWAIT,
"ustir", hz / 10);
if (error == EWOULDBLOCK &&
sc->sc_rd_expectdataticks > 0)
/*
* After a timeout decrement the tick
* counter within which time we expect
* data to arrive if we are receiving
* data...
*/
sc->sc_rd_expectdataticks--;
}
}
DPRINTFN(20, ("%s: exiting polling thread\n", __func__));
sc->sc_thread = NULL;
wakeup(&sc->sc_closing);
if (--sc->sc_refcnt < 0)
usb_detach_wakeupold(sc->sc_dev);
kthread_exit(0);
}
Static void
ustir_rd_cb(struct usbd_xfer *xfer, void *priv,
usbd_status status)
{
struct ustir_softc *sc = priv;
uint32_t size;
DPRINTFN(60, ("%s: sc=%p\n", __func__, sc));
/* Read is no longer in progress */
sc->sc_rd_readinprogress = 0;
if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */
return;
if (status) {
size = 0;
sc->sc_rd_err = 1;
if (sc->sc_direction == udir_input ||
sc->sc_direction == udir_idle) {
/*
* Receive error, probably need to clear error
* condition.
*/
sc->sc_direction = udir_stalled;
}
} else {
usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
}
sc->sc_rd_index = 0;
sc->sc_rd_count = size;
DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60),
("%s: sc=%p size=%u, err=%d\n", __func__,
sc, size, sc->sc_rd_err));
#ifdef USTIR_DEBUG
if (ustirdebug >= 20 && size > 0)
ustir_dumpdata(sc->sc_rd_buf, size, __func__);
#endif
if (!deframe_rd_ur(sc)) {
if (!deframe_isclear(&sc->sc_framestate) && size == 0 &&
sc->sc_rd_expectdataticks == 0) {
/*
* Expected data, but didn't get it
* within expected time...
*/
DPRINTFN(5,("%s: incoming packet timeout\n",
__func__));
deframe_clear(&sc->sc_framestate);
} else if (size > 0) {
/*
* If we also received actual data, reset the
* data read timeout and wake up the possibly
* sleeping thread...
*/
sc->sc_rd_expectdataticks = 2;
wakeup(&sc->sc_thread);
}
}
/*
* Check if incoming data has stopped, or that we cannot
* safely read any more data. In the case of the latter we
* must switch to idle so that a write will not block...
*/
if (sc->sc_direction == udir_input &&
((size == 0 && sc->sc_rd_expectdataticks == 0) ||
USTIR_BLOCK_RX_DATA(sc))) {
DPRINTFN(8,("%s: idling on packet timeout, "
"complete frame, or no data\n", __func__));
sc->sc_direction = udir_idle;
/* Wake up for possible output */
wakeup(&sc->sc_wr_buf);
selnotify(&sc->sc_wr_sel, 0, 0);
}
}
Static usbd_status
ustir_start_read(struct ustir_softc *sc)
{
usbd_status err;
DPRINTFN(60,("%s: sc=%p, size=%d\n", __func__, sc,
sc->sc_params.maxsize));
if (sc->sc_dying)
return USBD_IOERROR;
if (USTIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) {
/*
* Can't start reading just yet. Since we aren't
* going to start a read, have to switch direction to
* idle.
*/
sc->sc_direction = udir_idle;
return USBD_NORMAL_COMPLETION;
}
/* Starting a read... */
sc->sc_rd_readinprogress = 1;
sc->sc_direction = udir_input;
if (sc->sc_rd_err) {
sc->sc_rd_err = 0;
DPRINTFN(0, ("%s: clear stall\n", __func__));
usbd_clear_endpoint_stall(sc->sc_rd_pipe);
}
usbd_setup_xfer(sc->sc_rd_xfer, sc, sc->sc_rd_buf,
sc->sc_params.maxsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
ustir_rd_cb);
err = usbd_transfer(sc->sc_rd_xfer);
if (err != USBD_IN_PROGRESS) {
DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err));
return err;
}
return USBD_NORMAL_COMPLETION;
}
Static int
ustir_activate(device_t self, enum devact act)
{
struct ustir_softc *sc = device_private(self);
switch (act) {
case DVACT_DEACTIVATE:
sc->sc_dying = 1;
return 0;
default:
return EOPNOTSUPP;
}
}
/* ARGSUSED */
Static int
ustir_open(void *h, int flag, int mode,
struct lwp *l)
{
struct ustir_softc *sc = h;
int error;
usbd_status err;
DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
if (err != USBD_NORMAL_COMPLETION) {
error = EIO;
goto bad1;
}
err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
if (err != USBD_NORMAL_COMPLETION) {
error = EIO;
goto bad2;
}
error = usbd_create_xfer(sc->sc_rd_pipe, IRDA_MAX_FRAME_SIZE,
0, 0, &sc->sc_rd_xfer);
if (error)
goto bad3;
sc->sc_rd_buf = usbd_get_buffer(sc->sc_rd_xfer);
error = usbd_create_xfer(sc->sc_wr_pipe,
IRDA_MAX_FRAME_SIZE + STIR_OUTPUT_HEADER_SIZE,
USBD_FORCE_SHORT_XFER, 0, &sc->sc_wr_xfer);
if (error)
goto bad4;
sc->sc_wr_buf = usbd_get_buffer(sc->sc_wr_xfer);
sc->sc_ur_buf = kmem_alloc(IRDA_MAX_FRAME_SIZE, KM_SLEEP);
sc->sc_rd_index = sc->sc_rd_count = 0;
sc->sc_closing = 0;
sc->sc_rd_readinprogress = 0;
sc->sc_rd_expectdataticks = 0;
sc->sc_ur_framelen = 0;
sc->sc_rd_err = 0;
sc->sc_wr_stalewrite = 0;
sc->sc_speedrec = NULL;
sc->sc_direction = udir_idle;
sc->sc_params.speed = 0;
sc->sc_params.ebofs = 0;
sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
/* Increment reference for thread */
sc->sc_refcnt++;
error = kthread_create(PRI_NONE, 0, NULL, ustir_thread, sc,
&sc->sc_thread, "%s", device_xname(sc->sc_dev));
if (error) {
sc->sc_refcnt--;
goto bad5;
}
return 0;
bad5:
usbd_destroy_xfer(sc->sc_wr_xfer);
sc->sc_wr_xfer = NULL;
bad4:
usbd_destroy_xfer(sc->sc_rd_xfer);
sc->sc_rd_xfer = NULL;
bad3:
usbd_close_pipe(sc->sc_wr_pipe);
sc->sc_wr_pipe = NULL;
bad2:
usbd_close_pipe(sc->sc_rd_pipe);
sc->sc_rd_pipe = NULL;
bad1:
return error;
}
/* ARGSUSED */
Static int
ustir_close(void *h, int flag, int mode,
struct lwp *l)
{
struct ustir_softc *sc = h;
DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
sc->sc_refcnt++;
sc->sc_rd_readinprogress = 1;
sc->sc_closing = 1;
wakeup(&sc->sc_thread);
while (sc->sc_thread != NULL)
tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
if (sc->sc_rd_pipe != NULL) {
usbd_abort_pipe(sc->sc_rd_pipe);
sc->sc_rd_pipe = NULL;
}
if (sc->sc_wr_pipe != NULL) {
usbd_abort_pipe(sc->sc_wr_pipe);
sc->sc_wr_pipe = NULL;
}
if (sc->sc_rd_xfer != NULL) {
usbd_destroy_xfer(sc->sc_rd_xfer);
sc->sc_rd_xfer = NULL;
sc->sc_rd_buf = NULL;
}
if (sc->sc_wr_xfer != NULL) {
usbd_destroy_xfer(sc->sc_wr_xfer);
sc->sc_wr_xfer = NULL;
sc->sc_wr_buf = NULL;
}
if (sc->sc_ur_buf != NULL) {
kmem_free(sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
sc->sc_ur_buf = NULL;
}
if (sc->sc_rd_pipe != NULL) {
usbd_close_pipe(sc->sc_rd_pipe);
sc->sc_rd_pipe = NULL;
}
if (sc->sc_wr_pipe != NULL) {
usbd_close_pipe(sc->sc_wr_pipe);
sc->sc_wr_pipe = NULL;
}
if (--sc->sc_refcnt < 0)
usb_detach_wakeupold(sc->sc_dev);
return 0;
}
/* ARGSUSED */
Static int
ustir_read(void *h, struct uio *uio, int flag)
{
struct ustir_softc *sc = h;
int s;
int error;
u_int uframelen;
DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
if (sc->sc_dying)
return EIO;
#ifdef DIAGNOSTIC
if (sc->sc_rd_buf == NULL)
return EINVAL;
#endif
sc->sc_refcnt++;
if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
/* Possibly wake up polling thread */
wakeup(&sc->sc_thread);
do {
s = splusb();
while (sc->sc_ur_framelen == 0) {
DPRINTFN(5,("%s: calling tsleep()\n", __func__));
error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH,
"usirrd", 0);
if (sc->sc_dying)
error = EIO;
if (error) {
splx(s);
DPRINTFN(0, ("%s: tsleep() = %d\n",
__func__, error));
goto ret;
}
}
splx(s);
uframelen = sc->sc_ur_framelen;
DPRINTFN(1,("%s: sc=%p framelen=%u, hdr=0x%02x\n",
__func__, sc, uframelen, sc->sc_ur_buf[0]));
if (uframelen > uio->uio_resid)
error = EINVAL;
else
error = uiomove(sc->sc_ur_buf, uframelen, uio);
sc->sc_ur_framelen = 0;
if (!deframe_rd_ur(sc) && uframelen > 0) {
/*
* Need to wait for another read to obtain a
* complete frame... If we also obtained
* actual data, wake up the possibly sleeping
* thread immediately...
*/
wakeup(&sc->sc_thread);
}
} while (uframelen == 0);
DPRINTFN(1,("%s: return %d\n", __func__, error));
ret:
if (--sc->sc_refcnt < 0)
usb_detach_wakeupold(sc->sc_dev);
return error;
}
/* ARGSUSED */
Static int
ustir_write(void *h, struct uio *uio, int flag)
{
struct ustir_softc *sc = h;
usbd_status err;
uint32_t wrlen;
int error, sirlength;
uint8_t *wrbuf;
int s;
DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
if (sc->sc_dying)
return EIO;
#ifdef DIAGNOSTIC
if (sc->sc_wr_buf == NULL)
return EINVAL;
#endif
wrlen = uio->uio_resid;
if (wrlen > sc->sc_params.maxsize)
return EINVAL;
sc->sc_refcnt++;
if (!USTIR_BLOCK_RX_DATA(sc)) {
/*
* If reads are not blocked, determine what action we
* should potentially take...
*/
if (sc->sc_direction == udir_output) {
/*
* If the last operation was an output, wait for the
* polling thread to check for incoming data.
*/
sc->sc_wr_stalewrite = 1;
wakeup(&sc->sc_thread);
} else if (!sc->sc_rd_readinprogress &&
(sc->sc_direction == udir_idle ||
sc->sc_direction == udir_input)) {
/* If idle, check for input before outputting */
ustir_start_read(sc);
}
}
s = splusb();
while (sc->sc_wr_stalewrite ||
(sc->sc_direction != udir_output &&
sc->sc_direction != udir_idle)) {
DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, "
"calling tsleep()\n", __func__,
sc, sc->sc_wr_stalewrite, sc->sc_direction));
error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH,
"usirwr", 0);
if (sc->sc_dying)
error = EIO;
if (error) {
splx(s);
DPRINTFN(0, ("%s: tsleep() = %d\n", __func__,
error));
goto ret;
}
}
splx(s);
wrbuf = sc->sc_wr_buf;
/* Build header */
wrbuf[0] = STIR_OUTPUT_HEADER_BYTE0;
wrbuf[1] = STIR_OUTPUT_HEADER_BYTE1;
sirlength = irda_sir_frame(&wrbuf[STIR_OUTPUT_HEADER_SIZE],
MAX_USTIR_OUTPUT_FRAME -
STIR_OUTPUT_HEADER_SIZE,
uio, sc->sc_params.ebofs);
if (sirlength < 0) {
error = -sirlength;
} else {
uint32_t btlen;
DPRINTFN(1, ("%s: transfer %u bytes\n", __func__,
(unsigned int)wrlen));
wrbuf[2] = sirlength & 0xff;
wrbuf[3] = (sirlength >> 8) & 0xff;
btlen = STIR_OUTPUT_HEADER_SIZE + sirlength;
sc->sc_direction = udir_output;
#ifdef USTIR_DEBUG
if (ustirdebug >= 20)
ustir_dumpdata(wrbuf, btlen, __func__);
#endif
err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
USBD_FORCE_SHORT_XFER, USTIR_WR_TIMEOUT, wrbuf, &btlen);
DPRINTFN(2, ("%s: err=%d\n", __func__, err));
if (err != USBD_NORMAL_COMPLETION) {
if (err == USBD_INTERRUPTED)
error = EINTR;
else if (err == USBD_TIMEOUT)
error = ETIMEDOUT;
else
error = EIO;
} else {
error = 0;
}
}
ret:
if (--sc->sc_refcnt < 0)
usb_detach_wakeupold(sc->sc_dev);
DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
return error;
}
Static int
ustir_poll(void *h, int events, struct lwp *l)
{
struct ustir_softc *sc = h;
int revents = 0;
DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
if (events & (POLLOUT | POLLWRNORM)) {
if (sc->sc_direction != udir_input) {
revents |= events & (POLLOUT | POLLWRNORM);
} else {
DPRINTFN(2,("%s: recording write select\n",
__func__));
selrecord(l, &sc->sc_wr_sel);
}
}
if (events & (POLLIN | POLLRDNORM)) {
if (sc->sc_ur_framelen != 0) {
DPRINTFN(2,("%s: have data\n", __func__));
revents |= events & (POLLIN | POLLRDNORM);
} else {
DPRINTFN(2,("%s: recording read select\n",
__func__));
selrecord(l, &sc->sc_rd_sel);
}
}
return revents;
}
static void
filt_ustirrdetach(struct knote *kn)
{
struct ustir_softc *sc = kn->kn_hook;
int s;
s = splusb();
selremove_knote(&sc->sc_rd_sel, kn);
splx(s);
}
/* ARGSUSED */
static int
filt_ustirread(struct knote *kn, long hint)
{
struct ustir_softc *sc = kn->kn_hook;
kn->kn_data = sc->sc_ur_framelen;
return kn->kn_data > 0;
}
static void
filt_ustirwdetach(struct knote *kn)
{
struct ustir_softc *sc = kn->kn_hook;
int s;
s = splusb();
selremove_knote(&sc->sc_wr_sel, kn);
splx(s);
}
/* ARGSUSED */
static int
filt_ustirwrite(struct knote *kn, long hint)
{
struct ustir_softc *sc = kn->kn_hook;
kn->kn_data = 0;
return sc->sc_direction != udir_input;
}
static const struct filterops ustirread_filtops = {
.f_flags = FILTEROP_ISFD,
.f_attach = NULL,
.f_detach = filt_ustirrdetach,
.f_event = filt_ustirread,
};
static const struct filterops ustirwrite_filtops = {
.f_flags = FILTEROP_ISFD,
.f_attach = NULL,
.f_detach = filt_ustirwdetach,
.f_event = filt_ustirwrite,
};
Static int
ustir_kqfilter(void *h, struct knote *kn)
{
struct ustir_softc *sc = h;
struct selinfo *sip;
int s;
switch (kn->kn_filter) {
case EVFILT_READ:
sip = &sc->sc_rd_sel;
kn->kn_fop = &ustirread_filtops;
break;
case EVFILT_WRITE:
sip = &sc->sc_wr_sel;
kn->kn_fop = &ustirwrite_filtops;
break;
default:
return EINVAL;
}
kn->kn_hook = sc;
s = splusb();
selrecord_knote(sip, kn);
splx(s);
return 0;
}
#ifdef USTIR_DEBUG_IOCTLS
Static int ustir_ioctl(void *h, u_long cmd, void *addr, int flag, struct lwp *l)
{
struct ustir_softc *sc = h;
int error;
unsigned int regnum;
usbd_status err;
uint8_t regdata = 0;
if (sc->sc_dying)
return EIO;
sc->sc_refcnt++;
error = 0;
switch (cmd) {
case USTIR_READ_REGISTER:
regnum = *(unsigned int *)addr;
if (regnum > STIR_MAX_REG) {
error = EINVAL;
break;
}
err = ustir_read_reg(sc, regnum, ®data);
DPRINTFN(10, ("%s: regget(%u) = %#x\n", __func__,
regnum, (unsigned int)regdata));
*(unsigned int *)addr = regdata;
if (err != USBD_NORMAL_COMPLETION) {
printf("%s: register read failed: %s\n",
device_xname(sc->sc_dev),
usbd_errstr(err));
error = EIO;
}
break;
case USTIR_WRITE_REGISTER:
regnum = *(unsigned int *)addr;
regdata = (regnum >> 8) & 0xff;
regnum = regnum & 0xff;
if (regnum > STIR_MAX_REG) {
error = EINVAL;
break;
}
DPRINTFN(10, ("%s: regset(%u, %#x)\n", __func__,
regnum, (unsigned int)regdata));
err = ustir_write_reg(sc, regnum, regdata);
if (err != USBD_NORMAL_COMPLETION) {
printf("%s: register write failed: %s\n",
device_xname(sc->sc_dev),
usbd_errstr(err));
error = EIO;
}
break;
case USTIR_DEBUG_LEVEL:
#ifdef USTIR_DEBUG
ustirdebug = *(int *)addr;
#endif
break;
case USTIR_DEBUG_OPERATION:
break;
default:
error = EINVAL;
break;
}
if (--sc->sc_refcnt < 0)
usb_detach_wakeupold(sc->sc_dev);
return error;
}
#endif
Static int
ustir_set_params(void *h, struct irda_params *p)
{
struct ustir_softc *sc = h;
struct ustir_speedrec const *speedblk;
int i;
DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
sc, p->speed, p->ebofs, p->maxsize));
if (sc->sc_dying)
return EIO;
speedblk = NULL;
if (sc->sc_speedrec == NULL || p->speed != sc->sc_speedrec->speed) {
/* find speed */
for (i = 0; i < USTIR_NSPEEDS; i++) {
if (ustir_speeds[i].speed == p->speed) {
speedblk = &ustir_speeds[i];
goto found2;
}
}
/* no good value found */
return EINVAL;
found2:
;
}
if (p->maxsize != sc->sc_params.maxsize) {
if (p->maxsize > IRDA_MAX_FRAME_SIZE)
return EINVAL;
sc->sc_params.maxsize = p->maxsize;
}
sc->sc_params = *p;
if (speedblk != NULL) {
usbd_status err;
uint8_t regmode;
uint8_t regbrate;
sc->sc_speedrec = speedblk;
regmode = STIR_BRMODE_MODEREG(speedblk->config);
regbrate = STIR_BRMODE_BRATEREG(speedblk->config);
/*
* FFSPRST must be set to enable the FIFO.
*/
regmode |= STIR_RMODE_FFSPRST;
DPRINTFN(10, ("%s: setting BRATE = %x\n", __func__,
(unsigned int)regbrate));
err = ustir_write_reg(sc, STIR_REG_BRATE, regbrate);
if (err == USBD_NORMAL_COMPLETION) {
DPRINTFN(10, ("%s: setting MODE = %x\n", __func__,
(unsigned int)regmode));
err = ustir_write_reg(sc, STIR_REG_MODE, regmode);
}
if (err != USBD_NORMAL_COMPLETION) {
DPRINTFN(10, ("%s: error setting register: %s\n",
__func__, usbd_errstr(err)));
return EIO;
}
}
return 0;
}
Static int
ustir_get_speeds(void *h, int *speeds)
{
struct ustir_softc *sc = h;
DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
if (sc->sc_dying)
return EIO;
/* All these speeds are supported */
*speeds = IRDA_SPEED_4000000 |
IRDA_SPEED_1152000 |
IRDA_SPEED_576000 |
IRDA_SPEED_115200 |
IRDA_SPEED_57600 |
IRDA_SPEED_38400 |
IRDA_SPEED_19200 |
IRDA_SPEED_9600 |
IRDA_SPEED_2400;
return 0;
}
Static int
ustir_get_turnarounds(void *h, int *turnarounds)
{
struct ustir_softc *sc = h;
DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
if (sc->sc_dying)
return EIO;
/*
* Documentation is on the light side with respect to
* turnaround time for this device.
*/
*turnarounds = IRDA_TURNT_10000;
return 0;
}
|