-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.c
1060 lines (886 loc) · 27 KB
/
main.c
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
#include <psp2kern/kernel/modulemgr.h>
#include <psp2kern/kernel/threadmgr.h>
#include <psp2kern/kernel/sysmem.h>
#include <psp2kern/kernel/cpu.h>
#include <psp2kern/udcd.h>
#include <psp2kern/display.h>
#include <psp2kern/lowio/iftu.h>
#include <taihen.h>
#include <string.h>
#include "usb_descriptors.h"
#include "uvc.h"
#ifdef DEBUG
#include "log.h"
#include "draw.h"
#include "console.h"
#define LOG(s, ...) \
do { \
char __buffer[128]; \
snprintf(__buffer, sizeof(__buffer), s, ##__VA_ARGS__); \
/*LOG_TO_FILE(__buffer);*/ \
console_print(__buffer); \
} while (0)
#else
#define LOG(...) (void)0
#endif
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#define UNUSED(x) ((void)x)
#define UVC_DRIVER_NAME "VITAUVC00"
#define UVC_USB_PID 0x1337
#define MAX_UVC_VIDEO_FRAME_SIZE VIDEO_FRAME_SIZE_NV12(1280, 720)
#define UVC_PAYLOAD_HEADER_SIZE 12
#define UVC_PAYLOAD_SIZE(frame_size) (UVC_PAYLOAD_HEADER_SIZE + (frame_size))
#define MAX_UVC_PAYLOAD_TRANSFER_SIZE UVC_PAYLOAD_SIZE(MAX_UVC_VIDEO_FRAME_SIZE)
#define SCE_DISPLAY_PIXELFORMAT_BGRA5551 0x50000000
int ksceOledDisplayOn();
int ksceOledDisplayOff();
int ksceOledGetBrightness();
int ksceOledSetBrightness(int brightness);
int ksceLcdDisplayOn();
int ksceLcdDisplayOff();
int ksceLcdGetBrightness();
int ksceLcdSetBrightness(int brightness);
/* Copied from DolceSDK */
typedef struct SceIftuPlaneState_updated {
SceIftuFrameBuf fb;
unsigned int unk20; /* not observed to be non-zero */
unsigned int unk24; /* not observed to be non-zero */
unsigned int unk28; /* not observed to be non-zero */
unsigned int src_w; /* inverse scaling factor in 16.16 fixed point, greater than or equal to 0.25 */
unsigned int src_h; /* inverse scaling factor in 16.16 fixed point, greater than or equal to 0.25 */
unsigned int dst_x; /* offset into the destination buffer */
unsigned int dst_y; /* offset into the destination buffer */
unsigned int src_x; /* offset into the source buffer in 8.8 fixed point, strictly less than 4.0 */
unsigned int src_y; /* offset into the source buffer in 8.8 fixed point, strictly less than 4.0 */
unsigned int crop_top;
unsigned int crop_bot;
unsigned int crop_left;
unsigned int crop_right;
} SceIftuPlaneState_updated;
/*
* We want the data field (raw pixel data) to be aligned to 16B for the IFTU CSC to work properly.
* It seems the USB controller is fine with data aligned to 4B (starts reading from header field).
*/
#define UVC_FRAME_PADDING_SIZE (16 - UVC_PAYLOAD_HEADER_SIZE)
struct uvc_frame {
unsigned char padding[UVC_FRAME_PADDING_SIZE];
unsigned char header[UVC_PAYLOAD_HEADER_SIZE];
unsigned char data[];
} __attribute__((packed));
static const struct uvc_streaming_control uvc_probe_control_setting_default = {
.bmHint = 0,
.bFormatIndex = FORMAT_INDEX_UNCOMPRESSED_NV12,
.bFrameIndex = 1,
.dwFrameInterval = FPS_TO_INTERVAL(60),
.wKeyFrameRate = 0,
.wPFrameRate = 0,
.wCompQuality = 0,
.wCompWindowSize = 0,
.wDelay = 0,
.dwMaxVideoFrameSize = MAX_UVC_VIDEO_FRAME_SIZE,
.dwMaxPayloadTransferSize = MAX_UVC_PAYLOAD_TRANSFER_SIZE,
.dwClockFrequency = 0,
.bmFramingInfo = 0,
.bPreferedVersion = 1,
.bMinVersion = 0,
.bMaxVersion = 0,
};
static struct uvc_streaming_control uvc_probe_control_setting;
static struct {
unsigned char buffer[64];
SceUdcdEP0DeviceRequest ep0_req;
} pending_recv;
static SceUID uvc_thread_id;
static SceUID uvc_event_flag_id;
static int uvc_thread_run;
static int stream;
static SceUID uvc_frame_buffer_uid = -1;
static struct uvc_frame *uvc_frame_buffer_addr;
SceUID uvc_frame_req_evflag;
static int uvc_frame_init(unsigned int size);
static int uvc_frame_term();
#if defined(DISPLAY_OFF_OLED) || defined(DISPLAY_OFF_LCD)
static int prev_brightness;
#endif
static int usb_ep0_req_send(const void *data, unsigned int size)
{
static SceUdcdDeviceRequest req;
req = (SceUdcdDeviceRequest){
.endpoint = &endpoints[0],
.data = (void *)data,
.attributes = 0,
.size = size,
.isControlRequest = 0,
.onComplete = NULL,
.transmitted = 0,
.returnCode = 0,
.next = NULL,
.unused = NULL,
.physicalAddress = NULL
};
return ksceUdcdReqSend(&req);
}
static void usb_ep0_req_recv_on_complete(SceUdcdDeviceRequest *req);
static int usb_ep0_enqueue_recv_for_req(const SceUdcdEP0DeviceRequest *ep0_req)
{
static SceUdcdDeviceRequest req;
pending_recv.ep0_req = *ep0_req;
req = (SceUdcdDeviceRequest){
.endpoint = &endpoints[0],
.data = (void *)pending_recv.buffer,
.attributes = 0,
.size = pending_recv.ep0_req.wLength,
.isControlRequest = 0,
.onComplete = &usb_ep0_req_recv_on_complete,
.transmitted = 0,
.returnCode = 0,
.next = NULL,
.unused = NULL,
.physicalAddress = NULL
};
ksceKernelDcacheInvalidateRange(pending_recv.buffer,
pending_recv.ep0_req.wLength);
return ksceUdcdReqRecv(&req);
}
static int uvc_frame_req_init(void)
{
uvc_frame_req_evflag = ksceKernelCreateEventFlag("uvc_frame_req_evflag", 0, 0, NULL);
if (uvc_frame_req_evflag < 0) {
return uvc_frame_req_evflag;
}
return 0;
}
static int uvc_frame_req_fini(void)
{
int ret;
ret = ksceKernelDeleteEventFlag(uvc_frame_req_evflag);
if (ret < 0)
return ret;
return 0;
}
static void uvc_frame_req_submit_phycont_on_complete(SceUdcdDeviceRequest *req)
{
ksceKernelSetEventFlag(uvc_frame_req_evflag, 1);
}
static int uvc_frame_req_submit_phycont(const void *data, unsigned int size)
{
static SceUdcdDeviceRequest req;
int ret;
req = (SceUdcdDeviceRequest){
.endpoint = &endpoints[1],
.data = (void *)data,
.attributes = SCE_UDCD_DEVICE_REQUEST_ATTR_PHYCONT,
.size = size,
.isControlRequest = 0,
.onComplete = uvc_frame_req_submit_phycont_on_complete,
.transmitted = 0,
.returnCode = 0,
.next = NULL,
.unused = NULL,
.physicalAddress = NULL
};
ret = ksceUdcdReqSend(&req);
if (ret < 0)
return ret;
ret = ksceKernelWaitEventFlagCB(uvc_frame_req_evflag, 1, SCE_EVENT_WAITOR |
SCE_EVENT_WAITCLEAR_PAT, NULL, NULL);
return ret;
}
static void uvc_handle_video_streaming_req_recv(const SceUdcdEP0DeviceRequest *req)
{
struct uvc_streaming_control *streaming_control =
(struct uvc_streaming_control *)pending_recv.buffer;
switch (req->wValue >> 8) {
case UVC_VS_PROBE_CONTROL:
switch (req->bRequest) {
case UVC_SET_CUR:
uvc_probe_control_setting.bFormatIndex = streaming_control->bFormatIndex;
uvc_probe_control_setting.bFrameIndex = streaming_control->bFrameIndex;
uvc_probe_control_setting.dwFrameInterval = streaming_control->dwFrameInterval;
LOG("Probe SET_CUR, bFormatIndex: %d, bmFramingInfo: %x\n",
uvc_probe_control_setting.bFormatIndex,
uvc_probe_control_setting.bmFramingInfo);
break;
}
break;
case UVC_VS_COMMIT_CONTROL:
switch (req->bRequest) {
case UVC_SET_CUR:
uvc_probe_control_setting.bFormatIndex = streaming_control->bFormatIndex;
uvc_probe_control_setting.bFrameIndex = streaming_control->bFrameIndex;
uvc_probe_control_setting.dwFrameInterval = streaming_control->dwFrameInterval;
LOG("Commit SET_CUR, bFormatIndex: %d, bmFramingInfo: %x\n",
uvc_probe_control_setting.bFormatIndex,
uvc_probe_control_setting.bmFramingInfo);
stream = 1;
ksceKernelSetEventFlag(uvc_event_flag_id, 1);
break;
}
break;
}
}
void usb_ep0_req_recv_on_complete(SceUdcdDeviceRequest *req)
{
switch (pending_recv.ep0_req.wIndex & 0xFF) {
case STREAM_INTERFACE:
uvc_handle_video_streaming_req_recv(&pending_recv.ep0_req);
break;
}
}
static void uvc_handle_interface_ctrl_req(const SceUdcdEP0DeviceRequest *req)
{
LOG(" uvc_handle_interface_ctrl_req\n");
}
static void uvc_handle_input_terminal_req(const SceUdcdEP0DeviceRequest *req)
{
LOG(" uvc_handle_input_terminal_req %x, %x\n", req->wValue, req->bRequest);
}
static void uvc_handle_output_terminal_req(const SceUdcdEP0DeviceRequest *req)
{
LOG(" uvc_handle_output_terminal_req\n");
}
static void uvc_handle_video_streaming_req(const SceUdcdEP0DeviceRequest *req)
{
LOG(" uvc_handle_video_streaming_req %x, %x\n", req->wValue, req->bRequest);
switch (req->wValue >> 8) {
case UVC_VS_PROBE_CONTROL:
switch (req->bRequest) {
case UVC_GET_INFO:
break;
case UVC_GET_LEN:
break;
case UVC_GET_MIN:
case UVC_GET_MAX:
case UVC_GET_DEF:
LOG("Probe GET_DEF, bFormatIndex: %d, bmFramingInfo: %x\n",
uvc_probe_control_setting_default.bFormatIndex,
uvc_probe_control_setting_default.bmFramingInfo);
usb_ep0_req_send(&uvc_probe_control_setting_default,
sizeof(uvc_probe_control_setting_default));
break;
case UVC_GET_CUR:
LOG("Probe GET_CUR, bFormatIndex: %d, bmFramingInfo: %x\n",
uvc_probe_control_setting.bFormatIndex,
uvc_probe_control_setting.bmFramingInfo);
ksceKernelDcacheCleanRange(&uvc_probe_control_setting,
sizeof(uvc_probe_control_setting));
usb_ep0_req_send(&uvc_probe_control_setting,
sizeof(uvc_probe_control_setting));
break;
case UVC_SET_CUR:
usb_ep0_enqueue_recv_for_req(req);
break;
}
break;
case UVC_VS_COMMIT_CONTROL:
switch (req->bRequest) {
case UVC_GET_INFO:
break;
case UVC_GET_LEN:
break;
case UVC_GET_CUR:
ksceKernelDcacheCleanRange(&uvc_probe_control_setting,
sizeof(uvc_probe_control_setting));
usb_ep0_req_send(&uvc_probe_control_setting,
sizeof(uvc_probe_control_setting));
break;
case UVC_SET_CUR:
usb_ep0_enqueue_recv_for_req(req);
break;
}
break;
}
}
static void uvc_handle_video_abort(void)
{
LOG("uvc_handle_video_abort\n");
if (stream) {
stream = 0;
ksceUdcdClearFIFO(&endpoints[1]);
ksceUdcdReqCancelAll(&endpoints[1]);
}
}
static void uvc_handle_set_interface(const SceUdcdEP0DeviceRequest *req)
{
LOG("uvc_handle_set_interface %x %x\n", req->wIndex, req->wValue);
/* MAC OS sends Set Interface Alternate Setting 0 command after
* stopping to stream. This application needs to stop streaming. */
if ((req->wIndex == STREAM_INTERFACE) && (req->wValue == 0))
uvc_handle_video_abort();
}
static void uvc_handle_clear_feature(const SceUdcdEP0DeviceRequest *req)
{
LOG("uvc_handle_clear_feature\n");
/* Windows OS sends Clear Feature Request after it stops streaming,
* however MAC OS sends clear feature request right after it sends a
* Commit -> SET_CUR request. Hence, stop streaming only of streaming
* has started. */
switch (req->wValue) {
case USB_FEATURE_ENDPOINT_HALT:
if ((req->wIndex & USB_ENDPOINT_ADDRESS_MASK) ==
endpoints[1].endpointNumber) {
uvc_handle_video_abort();
}
break;
}
}
static int uvc_udcd_process_request(int recipient, int arg, SceUdcdEP0DeviceRequest *req, void *user_data)
{
LOG("usb_driver_process_request(recipient: %x, arg: %x)\n", recipient, arg);
LOG(" request: %x type: %x wValue: %x wIndex: %x wLength: %x\n",
req->bRequest, req->bmRequestType, req->wValue, req->wIndex, req->wLength);
if (arg < 0)
return -1;
switch (req->bmRequestType) {
case USB_CTRLTYPE_DIR_DEVICE2HOST |
USB_CTRLTYPE_TYPE_CLASS |
USB_CTRLTYPE_REC_INTERFACE: /* 0xA1 */
case USB_CTRLTYPE_DIR_HOST2DEVICE |
USB_CTRLTYPE_TYPE_CLASS |
USB_CTRLTYPE_REC_INTERFACE: /* 0x21 */
switch (req->wIndex & 0xFF) {
case CONTROL_INTERFACE:
switch (req->wIndex >> 8) {
case INTERFACE_CTRL_ID:
uvc_handle_interface_ctrl_req(req);
break;
case INPUT_TERMINAL_ID:
uvc_handle_input_terminal_req(req);
break;
case OUTPUT_TERMINAL_ID:
uvc_handle_output_terminal_req(req);
break;
}
break;
case STREAM_INTERFACE:
uvc_handle_video_streaming_req(req);
break;
}
break;
case USB_CTRLTYPE_DIR_HOST2DEVICE |
USB_CTRLTYPE_TYPE_STANDARD |
USB_CTRLTYPE_REC_INTERFACE: /* 0x01 */
switch (req->bRequest) {
case USB_REQ_SET_INTERFACE:
uvc_handle_set_interface(req);
break;
}
break;
case USB_CTRLTYPE_DIR_HOST2DEVICE |
USB_CTRLTYPE_TYPE_STANDARD |
USB_CTRLTYPE_REC_ENDPOINT: /* 0x02 */
switch (req->bRequest) {
case USB_REQ_CLEAR_FEATURE:
uvc_handle_clear_feature(req);
break;
}
break;
case USB_CTRLTYPE_DIR_DEVICE2HOST |
USB_CTRLTYPE_TYPE_STANDARD |
USB_CTRLTYPE_REC_DEVICE: /* 0x80 */
switch (req->wValue >> 8) {
case 0x0A: /* USB_DT_DEBUG */
break;
}
break;
default:
LOG("Unknown bmRequestType: 0x%02X\n", req->bmRequestType);
}
return 0;
}
static int uvc_udcd_change_setting(int interfaceNumber, int alternateSetting, int bus)
{
LOG("uvc_udcd_change %d %d\n", interfaceNumber, alternateSetting);
return 0;
}
static int uvc_udcd_attach(int usb_version, void *user_data)
{
LOG("uvc_udcd_attach %d\n", usb_version);
ksceUdcdClearFIFO(&endpoints[1]);
#if defined(DISPLAY_OFF_OLED)
prev_brightness = ksceOledGetBrightness();
ksceOledDisplayOff();
#elif defined(DISPLAY_OFF_LCD)
prev_brightness = ksceLcdGetBrightness();
ksceLcdDisplayOff();
#endif
return 0;
}
static void uvc_udcd_detach(void *user_data)
{
LOG("uvc_udcd_detach\n");
uvc_handle_video_abort();
#if defined(DISPLAY_OFF_OLED)
ksceOledDisplayOn();
ksceOledSetBrightness(prev_brightness);
#elif defined(DISPLAY_OFF_LCD)
ksceLcdDisplayOn();
ksceLcdSetBrightness(prev_brightness);
#endif
}
static void uvc_udcd_configure(int usb_version, int desc_count, SceUdcdInterfaceSettings *settings, void *user_data)
{
LOG("uvc_udcd_configure %d %d %p %d\n", usb_version, desc_count, settings, settings->numDescriptors);
}
static int uvc_driver_start(int size, void *p, void *user_data)
{
LOG("uvc_driver_start\n");
return 0;
}
static int uvc_driver_stop(int size, void *p, void *user_data)
{
LOG("uvc_driver_stop\n");
return 0;
}
static SceUdcdDriver uvc_udcd_driver = {
.driverName = UVC_DRIVER_NAME,
.numEndpoints = 2,
.endpoints = endpoints,
.interface = &interface,
.descriptor_hi = &devdesc_hi,
.configuration_hi = &config_hi,
.descriptor = &devdesc_full,
.configuration = &config_full,
.stringDescriptors = NULL,
.stringDescriptorProduct = &string_descriptor_product,
.stringDescriptorSerial = &string_descriptor_serial,
.processRequest = &uvc_udcd_process_request,
.changeSetting = &uvc_udcd_change_setting,
.attach = &uvc_udcd_attach,
.detach = &uvc_udcd_detach,
.configure = &uvc_udcd_configure,
.start = &uvc_driver_start,
.stop = &uvc_driver_stop,
.user_data = NULL
};
static unsigned int uvc_frame_transfer(struct uvc_frame *frame,
unsigned int frame_size,
int fid, int eof)
{
int ret;
frame->header[0] = UVC_PAYLOAD_HEADER_SIZE;
frame->header[1] = UVC_STREAM_EOH;
if (fid)
frame->header[1] |= UVC_STREAM_FID;
if (eof)
frame->header[1] |= UVC_STREAM_EOF;
ret = uvc_frame_req_submit_phycont(frame->header, frame_size);
if (ret < 0) {
LOG("Error sending frame: 0x%08X\n", ret);
return ret;
}
return 0;
}
int uvc_start(void);
int uvc_stop(void);
static inline unsigned int display_to_iftu_pixelformat(unsigned int fmt)
{
switch (fmt) {
case SCE_DISPLAY_PIXELFORMAT_A8B8G8R8:
default:
return SCE_IFTU_PIXELFORMAT_BGRX8888;
case SCE_DISPLAY_PIXELFORMAT_BGRA5551:
return SCE_IFTU_PIXELFORMAT_BGRA5551;
}
}
static inline unsigned int display_pixelformat_bpp(unsigned int fmt)
{
switch (fmt) {
case SCE_DISPLAY_PIXELFORMAT_A8B8G8R8:
default:
return 4;
case SCE_DISPLAY_PIXELFORMAT_BGRA5551:
return 2;
}
}
static int frame_convert_to_nv12(int fid, const SceDisplayFrameBufInfo *fb_info,
int dst_width, int dst_height)
{
uintptr_t dst_paddr;
uintptr_t src_paddr = fb_info->paddr;
unsigned int src_width = fb_info->framebuf.width;
unsigned int src_width_aligned = ALIGN(src_width, 16);
unsigned int src_pitch = fb_info->framebuf.pitch;
unsigned int src_height = fb_info->framebuf.height;
unsigned int src_pixelfmt = fb_info->framebuf.pixelformat;
unsigned int src_pixelfmt_bpp = display_pixelformat_bpp(src_pixelfmt);
unsigned char *uvc_frame_data = uvc_frame_buffer_addr->data;
static SceIftuCscParams RGB_to_YCbCr_JPEG_csc_params = {
0, 0x202, 0x3FF,
0, 0x3FF, 0,
{
{ 0x99, 0x12C, 0x3A},
{0xFAA, 0xF57, 0x100},
{0x100, 0xF2A, 0xFD7}
}
};
ksceKernelGetPaddr(uvc_frame_data, &dst_paddr);
SceIftuConvParams params;
memset(¶ms, 0, sizeof(params));
params.size = sizeof(params);
params.unk04 = 1;
params.csc_params1 = &RGB_to_YCbCr_JPEG_csc_params;
params.csc_params2 = NULL;
params.csc_control = 1;
params.unk14 = 0;
params.unk18 = 0;
params.unk1C = 0;
params.alpha = 0xFF;
params.unk24 = 0;
SceIftuPlaneState_updated src;
memset(&src, 0, sizeof(src));
src.fb.pixelformat = display_to_iftu_pixelformat(src_pixelfmt);
src.fb.width = src_width_aligned;
src.fb.height = src_height;
src.fb.leftover_stride = (src_pitch - src_width_aligned) * src_pixelfmt_bpp;
src.fb.leftover_align = 0;
src.fb.paddr0 = src_paddr;
src.unk20 = 0;
src.unk24 = 0;
src.unk28 = 0;
src.src_w = (src_width * 0x10000) / dst_width;
src.src_h = (src_height * 0x10000) / dst_height;
src.dst_x = 0;
src.dst_y = 0;
src.src_x = 0;
src.src_y = 0;
src.crop_top = 0;
src.crop_bot = 0;
src.crop_left = 0;
src.crop_right = 0;
SceIftuFrameBuf dst;
memset(&dst, 0, sizeof(dst));
dst.pixelformat = SCE_IFTU_PIXELFORMAT_NV12;
dst.width = dst_width;
dst.height = dst_height;
dst.leftover_stride = 0;
dst.leftover_align = 0;
dst.paddr0 = dst_paddr;
dst.paddr1 = dst_paddr + dst_width * dst_height;
return ksceIftuCsc(&dst, (SceIftuPlaneState *)&src, ¶ms);
}
static int convert_and_send_frame_nv12(int fid, const SceDisplayFrameBufInfo *fb_info,
int dst_width, int dst_height)
{
int ret;
uint64_t time1, time2, time3;
UNUSED(time1);
UNUSED(time2);
UNUSED(time3);
time1 = ksceKernelGetSystemTimeWide();
ret = frame_convert_to_nv12(fid, fb_info, dst_width, dst_height);
if (ret < 0)
return ret;
time2 = ksceKernelGetSystemTimeWide();
ret = uvc_frame_transfer(uvc_frame_buffer_addr,
UVC_PAYLOAD_SIZE(VIDEO_FRAME_SIZE_NV12(dst_width, dst_height)),
fid, 1);
if (ret < 0)
return ret;
time3 = ksceKernelGetSystemTimeWide();
LOG("NV12 CSC: %lldus xfer: %lldus\n", time2 - time1, time3 - time2);
return 0;
}
static int send_frame(void)
{
static int fid = 0;
int ret;
SceDisplayFrameBufInfo fb_info;
int head = ksceDisplayGetPrimaryHead();
memset(&fb_info, 0, sizeof(fb_info));
fb_info.size = sizeof(fb_info);
ret = ksceDisplayGetProcFrameBufInternal(-1, head, 0, &fb_info);
if (ret < 0 || fb_info.paddr == 0)
ret = ksceDisplayGetProcFrameBufInternal(-1, head, 1, &fb_info);
if (ret < 0)
return ret;
switch (uvc_probe_control_setting.bFormatIndex) {
case FORMAT_INDEX_UNCOMPRESSED_NV12: {
const struct UVC_FRAME_UNCOMPRESSED(2) *frames =
video_streaming_descriptors.frames_uncompressed_nv12;
int cur_frame_index = uvc_probe_control_setting.bFrameIndex;
int dst_width = frames[cur_frame_index - 1].wWidth;
int dst_height = frames[cur_frame_index - 1].wHeight;
static int last_frame_index = 0;
if (uvc_frame_buffer_uid < 0 || cur_frame_index != last_frame_index) {
uvc_frame_term();
ret = uvc_frame_init(UVC_FRAME_PADDING_SIZE + VIDEO_FRAME_SIZE_NV12(dst_width, dst_height));
if (ret < 0) {
LOG("Error allocating the UVC frame (0x%08X)\n", ret);
return ret;
} else {
last_frame_index = cur_frame_index;
}
}
ret = convert_and_send_frame_nv12(fid, &fb_info, dst_width, dst_height);
if (ret < 0) {
LOG("Error sending NV12 frame: 0x%08X\n", ret);
return ret;
}
break;
}
}
if (ret < 0) {
stream = 0;
return ret;
}
fid ^= 1;
return 0;
}
static int display_vblank_cb_func(int notifyId, int notifyCount, int notifyArg, void *common)
{
static unsigned int frames = 0;
unsigned int elapsed;
/*LOG("VBlank: %d, %d, %d, %p\n", notifyId, notifyCount, notifyArg, common);*/
if (!stream)
return 0;
/*
* VBlanks occur at ~60FPS.
*/
frames += notifyCount;
elapsed = FPS_TO_INTERVAL(60 / frames);
if (elapsed >= uvc_probe_control_setting.dwFrameInterval) {
ksceKernelSetEventFlag(uvc_event_flag_id, 1);
frames = 0;
}
return 0;
}
static int uvc_thread(SceSize args, void *argp)
{
SceUID display_vblank_cb_uid;
#if 0
/*
* Wait until the MTP driver starts to takeover.
*/
SceUdcdWaitParam wait_param;
memset(&wait_param, 0, sizeof(wait_param));
wait_param.status = SCE_UDCD_STATUS_ACTIVATED;
wait_param.driverName = "USB_MTP_Driver";
ksceUdcdWaitState(&wait_param, 0);
ksceKernelDelayThread(250 * 1000);
#endif
stream = 0;
uvc_start();
display_vblank_cb_uid = ksceKernelCreateCallback("uvc_display_vblank", 0,
display_vblank_cb_func, NULL);
ksceDisplayRegisterVblankStartCallback(display_vblank_cb_uid);
while (uvc_thread_run) {
unsigned int out_bits;
int ret = ksceKernelWaitEventFlagCB(uvc_event_flag_id, 1,
SCE_EVENT_WAITOR | SCE_EVENT_WAITCLEAR_PAT,
&out_bits, (SceUInt32[]){1000000});
if (ret == 0 && stream)
send_frame();
else if (ret == 0x80028005) /* SCE_KERNEL_ERROR_WAIT_TIMEOUT */
uvc_frame_term();
}
ksceDisplayUnregisterVblankStartCallback(display_vblank_cb_uid);
ksceKernelDeleteCallback(display_vblank_cb_uid);
uvc_stop();
return 0;
}
static int uvc_frame_init(unsigned int size)
{
int ret;
const int use_cdram = 0;
SceKernelAllocMemBlockKernelOpt opt;
SceKernelMemBlockType type;
SceKernelAllocMemBlockKernelOpt *optp;
if (use_cdram) {
type = 0x40408006;
size = ALIGN(size, 256 * 1024);
optp = NULL;
} else {
type = 0x10208006;
size = ALIGN(size, 4 * 1024);
memset(&opt, 0, sizeof(opt));
opt.size = sizeof(opt);
opt.attr = SCE_KERNEL_ALLOC_MEMBLOCK_ATTR_PHYCONT |
SCE_KERNEL_ALLOC_MEMBLOCK_ATTR_HAS_ALIGNMENT;
opt.alignment = 4 * 1024;
optp = &opt;
}
uvc_frame_buffer_uid = ksceKernelAllocMemBlock("uvc_frame_buffer", type, size, optp);
if (uvc_frame_buffer_uid < 0) {
LOG("Error allocating CSC dest memory: 0x%08X\n", uvc_frame_buffer_uid);
return uvc_frame_buffer_uid;
}
ret = ksceKernelGetMemBlockBase(uvc_frame_buffer_uid, (void **)&uvc_frame_buffer_addr);
if (ret < 0) {
LOG("Error getting CSC desr memory addr: 0x%08X\n", ret);
ksceKernelFreeMemBlock(uvc_frame_buffer_uid);
uvc_frame_buffer_uid = -1;
return ret;
}
return 0;
}
static int uvc_frame_term()
{
if (uvc_frame_buffer_uid >= 0) {
ksceKernelFreeMemBlock(uvc_frame_buffer_uid);
uvc_frame_buffer_uid = -1;
}
return 0;
}
int uvc_start(void)
{
int ret;
/*
* Wait until there's a framebuffer set.
*/
ksceDisplayWaitSetFrameBufCB();
#ifndef DEBUG
/*
* Wait until LiveArea is more or less ready.
*/
ksceKernelDelayThreadCB(15 * 1000 * 1000);
#endif
ret = ksceUdcdDeactivate();
if (ret < 0 && ret != SCE_UDCD_ERROR_INVALID_ARGUMENT) {
LOG("Error deactivating UDCD (0x%08X)\n", ret);
return ret;
}
ksceUdcdStop("USB_MTP_Driver", 0, NULL);
ksceUdcdStop("USBPSPCommunicationDriver", 0, NULL);
ksceUdcdStop("USBSerDriver", 0, NULL);
ksceUdcdStop("USBDeviceControllerDriver", 0, NULL);
ret = ksceUdcdStart("USBDeviceControllerDriver", 0, NULL);
if (ret < 0) {
LOG("Error starting the USBDeviceControllerDriver driver (0x%08X)\n", ret);
return ret;
}
ret = ksceUdcdStart(UVC_DRIVER_NAME, 0, NULL);
if (ret < 0) {
LOG("Error starting the " UVC_DRIVER_NAME " driver (0x%08X)\n", ret);
goto err_start_uvc_driver;
}
ret = ksceUdcdActivate(UVC_USB_PID);
if (ret < 0) {
LOG("Error activating the " UVC_DRIVER_NAME " driver (0x%08X)\n", ret);
goto err_activate;
}
ret = uvc_frame_req_init();
if (ret < 0) {
LOG("Error allocating USB request (0x%08X)\n", ret);
goto err_alloc_uvc_frame_req;
}
/*
* Set the current streaming settings to the default ones.
*/
memcpy(&uvc_probe_control_setting, &uvc_probe_control_setting_default,
sizeof(uvc_probe_control_setting));
return 0;
err_alloc_uvc_frame_req:
ksceUdcdDeactivate();
err_activate:
ksceUdcdStop(UVC_DRIVER_NAME, 0, NULL);
err_start_uvc_driver:
ksceUdcdStop("USBDeviceControllerDriver", 0, NULL);
return ret;
}
int uvc_stop(void)
{
ksceUdcdDeactivate();
ksceUdcdStop(UVC_DRIVER_NAME, 0, NULL);
ksceUdcdStop("USBDeviceControllerDriver", 0, NULL);
ksceUdcdStart("USBDeviceControllerDriver", 0, NULL);
ksceUdcdStart("USB_MTP_Driver", 0, NULL);
ksceUdcdActivate(0x4E4);
uvc_frame_term();
return 0;
}
static SceUID SceUdcd_sub_01E1128C_hook_uid = -1;
static tai_hook_ref_t SceUdcd_sub_01E1128C_ref;
static int SceUdcd_sub_01E1128C_hook_func(const SceUdcdConfigDescriptor *config_descriptor, void *desc_data)
{
int ret;
SceUdcdConfigDescriptor *dst = desc_data;
ret = TAI_CONTINUE(int, SceUdcd_sub_01E1128C_ref, config_descriptor, desc_data);
/*
* SceUdcd doesn't use the extra and extraLength members of the
* SceUdcdConfigDescriptor struct, so we have to manually patch
* it to add custom descriptors.
*/
if (dst->wTotalLength == config_descriptor->wTotalLength) {
memmove(desc_data + USB_DT_CONFIG_SIZE + sizeof(interface_association_descriptor),
desc_data + USB_DT_CONFIG_SIZE,
config_descriptor->wTotalLength - USB_DT_CONFIG_SIZE);
memcpy(desc_data + USB_DT_CONFIG_SIZE, interface_association_descriptor,
sizeof(interface_association_descriptor));
dst->wTotalLength += sizeof(interface_association_descriptor);
ksceKernelDcacheCleanRange(desc_data, dst->wTotalLength);
}
return ret;
}
void _start() __attribute__((weak, alias("module_start")));
int module_start(SceSize argc, const void *args)
{
int ret;
tai_module_info_t SceUdcd_modinfo;
#ifdef DEBUG
log_reset();
framebuffer_map();
console_init();
#endif
LOG("udcd_uvc by xerpi\n");
SceUdcd_modinfo.size = sizeof(SceUdcd_modinfo);
taiGetModuleInfoForKernel(KERNEL_PID, "SceUdcd", &SceUdcd_modinfo);
SceUdcd_sub_01E1128C_hook_uid = taiHookFunctionOffsetForKernel(KERNEL_PID,
&SceUdcd_sub_01E1128C_ref, SceUdcd_modinfo.modid, 0,
0x01E1128C - 0x01E10000, 1, SceUdcd_sub_01E1128C_hook_func);
uvc_thread_id = ksceKernelCreateThread("uvc_thread", uvc_thread,
0x3C, 0x1000, 0, 0x10000, 0);
if (uvc_thread_id < 0) {
LOG("Error creating the UVC thread (0x%08X)\n", uvc_thread_id);
goto err_return;
}
uvc_event_flag_id = ksceKernelCreateEventFlag("uvc_event_flag", 0,
0, NULL);
if (uvc_event_flag_id < 0) {