PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
ps2cam.c
Go to the documentation of this file.
1/*-----------------------------------------------------------------------
2- PSX2 OpenSource Project -
3- (C)2004 lion[PS2Dev] -
4- (C)2004 PS2Dev.org -
5-----------------------------------------------------------------------*/
6
12//(1) Please keep this file neat.
13//(2) This irx must always be backwords compatible.
14#include <stdio.h>
15#include <sysclib.h>
16#include <tamtypes.h>
17#include <thbase.h>
18#include <sifcmd.h>
19#include <sifrpc.h>
20#include <loadcore.h>
21#include <ioman.h>
22#include <usbd.h>
23#include <usbd_macro.h>
24#include <thsemap.h>
25#include <sysmem.h>
26
27
28#include "ps2cam.h"
29
30#define MODNAME "PS2 USB camera driver"
31
32IRX_ID(MODNAME, 1, 1);
33
34#define BIND_RPC_ID 0x00FD000 +2
35#define DRIVER_VERSON_MAJOR 1
36#define DRIVER_VERSON_MINOR 0
37
38#define MAX_CAM_DEVICE 2
39#define MAX_CAM_DEVICE_HANDLE 2
40
41
42static SifRpcDataQueue_t rpc_queue __attribute__((__aligned__(16)));
43static SifRpcServerData_t rpc_server __attribute__((__aligned__(16)));
44static int _rpc_buffer[1024] __attribute__((__aligned__(4)));
45//static int threadId;
46static int maintain_thread;
47sceUsbdLddOps cam_driver = {NULL,
48 NULL,
49 "ps2cam",
53 0, 0, 0, 0, 0, NULL };
54
55
56
57char campacket[896] __attribute__((__aligned__(16)));
58char irx_initialized = 0;
59int ps2cam_sema=0;
60CAMERA_DEVICE Camera[MAX_CAM_DEVICE];
61PS2CAM_DEVICE_HANDLE CamHandle[MAX_CAM_DEVICE_HANDLE];
62
63
64
65
66
67
68
69int _start( int argc, char *argv[])
70{
71 iop_thread_t param;
72 int th;
73
74 (void)argc;
75 (void)argv;
76
78
79
80 /*create thread*/
81 param.attr = TH_C;
82 param.thread = rpcMainThread;
83 param.priority = 40;
84 param.stacksize = 0x800;
85 param.option = 0;
86
87
88 th = CreateThread(&param);
89
90 if (th > 0)
91 {
92 StartThread(th,NULL);
93 return MODULE_RESIDENT_END;
94 }
95 else
96 {
97 return MODULE_NO_RESIDENT_END;
98 }
99}
100
101
102
103
104
105
106
107
110{
111 int i;
112 iop_sema_t sema;
113
114 printf("PS2 USB Camera Driver v.%d.%d ((C) www.ps2dev.org)\n",DRIVER_VERSON_MAJOR,DRIVER_VERSON_MINOR);
115
116
117 memset(&Camera[0], 0, sizeof(Camera));
118 memset(&CamHandle[0], 0, sizeof(CamHandle));
119
120 for(i=0;i<MAX_CAM_DEVICE_HANDLE;i++)
121 {
122 CamHandle[i].fd = i+1;
123 }
124
125 //setup sema
126
127
128 sema.initial= 1;
129 sema.max = 1;
130 sema.option = 0;
131 sema.attr = 0;
132 ps2cam_sema = CreateSema(&sema);
133
134
135 //connect to usb.irx
136 return sceUsbdRegisterLdd(&cam_driver);
137}
138
139
140
141
143int PS2CamProbe(int devId)
144{
145 static short count;
146 int i;
150
151
152 dev = sceUsbdScanStaticDescriptor(devId, NULL, USB_DT_DEVICE);
153 conf = sceUsbdScanStaticDescriptor(devId, dev, USB_DT_CONFIG);
154 intf = (UsbInterfaceDescriptor *) ((char *) conf + conf->bLength);
155
156
157
158
159 // check for a free device slot. if none just ignore new device
160 count=0;
161
162 for(i=0;i<MAX_CAM_DEVICE;i++)
163 {
164 if(Camera[i].status==0)count++;
165 }
166
167 if(count <= 0) return 0;
168
169
170 if(intf->bInterfaceClass == USB_CLASS_VENDOR_SPEC)
171 {
172 // eyetoy 1
173 if(dev->idVendor == PS2CAM_VEND_SONY && dev->idProduct == PS2CAM_PROD_EYETOY)
174 {
175 return 1;
176 }
177
178 // eyetoy 2
179 if(dev->idVendor == PS2CAM_VEND_SONY && dev->idProduct == PS2CAM_PROD_EYETOY2)
180 {
181 return 1;
182 }
183
184 // eyetoy 3
185 if(dev->idVendor == PS2CAM_VEND_SONY && dev->idProduct == PS2CAM_PROD_EYETOY3)
186 {
187 return 1;
188 }
189
190 // eyetoy 4
191 if(dev->idVendor == PS2CAM_VEND_SONY && dev->idProduct == PS2CAM_PROD_EYETOY4)
192 {
193 return 1;
194 }
195
196 // D-Link VisualStream DSB-C310
197 if(dev->idVendor == PS2CAM_VEND_DLINK && dev->idProduct == PS2CAM_PROD_VISUALSTREAM)
198 {
199 return 1;
200 }
201 }
202
203
204 return 0;
205}
206
207
208
209
210
211
213int PS2CamConnect(int devId)
214{
215
216 int i;
218
219 UsbInterfaceDescriptor *intf0,*intf1;
221 CAMERA_DEVICE *cam = NULL;
222 iop_thread_t param;
223
224
225 printf("camera was connected\n");
226
227
228
229 dev = sceUsbdScanStaticDescriptor(devId, NULL, USB_DT_DEVICE);
230
231 intf0 = sceUsbdScanStaticDescriptor(devId, dev, USB_DT_INTERFACE);
232 intf1 = sceUsbdScanStaticDescriptor(devId, intf0, USB_DT_INTERFACE);
233
234 endp1= (UsbEndpointDescriptor *) ((char *) intf1 + intf1->bLength);
235
236
237
238
239 //get a free device slot
240 for(i=0;i<MAX_CAM_DEVICE;i++)
241 {
242 if(Camera[i].status == 0)
243 {
244 cam = (CAMERA_DEVICE *)&Camera[i];
245
246 cam->status = CAM_STATUS_CONNECTED;
247 break;
248 }
249 }
250
251 if (cam == NULL)
252 {
253 return 0;
254 }
255
256
257 cam->device_id = devId;
258
259 cam->controll = sceUsbdOpenPipe(devId, NULL);
260 cam->stream = sceUsbdOpenPipe(devId, endp1);
261 cam->stream_pocket_size = (endp1->wMaxPacketSizeHB * 256 + endp1->wMaxPacketSizeLB);
262
263
264
265
266
267
268
269
270
271
272
273
274
275 /*create thread that will execute funtions that cant be called*/
276 /*in this funtion*/
277 param.attr = TH_C;
278 param.thread = (void *)PS2CamInitializeNewDevice;
279 param.priority = 40;
280 param.stacksize = 0x800;
281 param.option = 0;
282
283 maintain_thread = CreateThread(&param);
284
285
286 StartThread(maintain_thread, cam);
287
288 return 0;
289}
290
291
292
293
294
295
297int PS2CamDisconnect(int devId)
298{
299 int i;
300 CAMERA_DEVICE *cam = NULL;
301
302 printf("camera was unplugged\n");
303
304 //find device in slots and remove/clear/free it
305 for(i=0;i<MAX_CAM_DEVICE;i++)
306 {
307 if(Camera[i].device_id == devId)
308 {
309 cam = (CAMERA_DEVICE *)&Camera[i];
310
311 cam->status = CAM_STATUS_NOTCONNECTED;
312 break;
313 }
314 }
315
316
317
318 // inform any handle that is using this device
319
320 //find device handle and set the status
321 for(i=0;i<MAX_CAM_DEVICE_HANDLE;i++)
322 {
323 if(CamHandle[i].cam == cam)
324 {
325 CamHandle[i].status = -1;
326 }
327 }
328
329
330
331 // close endpoints
332 sceUsbdClosePipe(cam->controll);
333 sceUsbdClosePipe(cam->stream );
334
335 cam->status = CAM_STATUS_NOTCONNECTED;
336
337 return 0;
338}
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359/* SIF RPC SERVER FUNCTIONS */
360
361
362void rpcMainThread(void* param)
363{
364 //int ret=-1;
365 int tid;
366
367 (void)param;
368
369 sceSifInitRpc(0);
370 tid = GetThreadId();
371 sceSifSetRpcQueue(&rpc_queue, tid);
372 sceSifRegisterRpc(&rpc_server, BIND_RPC_ID, (void *) rpcCommandHandler, (u8 *) &_rpc_buffer, 0, 0, &rpc_queue);
373 sceSifRpcLoop(&rpc_queue);
374}
375
376/* CAMERA HIGH LEVEL FUNCTIONS */
377
378
379
380
383{
384 unsigned char *temp_str;
386
387
389
390 camStopStream(cam);
391 PS2CamSelectInterface(cam,0,0);
392 camStartStream(cam);
393
395
396/* camStopStream(dev);
397 setReg16(dev, 0x30, 384);
398 camStartStream(dev);
399*/
400 camStopStream(cam);
401 PS2CamSelectInterface(cam,0,EYETOY_ALTERNATE_SIZE_384);
402 camStartStream(cam);
403
404 cam->status = CAM_STATUS_CONNECTEDREADY;
405
406
407
408 // connected message (alloc som mem and get device string then print it and free the mem we alloced)
409 d = sceUsbdScanStaticDescriptor(cam->device_id, NULL, USB_DT_DEVICE);
410 temp_str = AllocSysMemory(0, 128, 0);
411 temp_str[0]=0x00;
412 PS2CamGetDeviceSring(cam, d->iProduct, (char *)temp_str, 128);
413 printf("cam initialized(%s)\n",temp_str);
414 FreeSysMemory(temp_str);
415
416
417
418 DeleteThread(maintain_thread);
419
420 return;
421
422}
423
424
425
426
427
430{
431 int ret;
432
433
434 WaitSema(ps2cam_sema);
435
436 ret = sceUsbdSetConfiguration(dev->controll, id, PS2CamCallback, (void*)ps2cam_sema);
437
438 if(ret != USB_RC_OK)
439 {
440 printf("Usb: Error sending set_configuration\n");
441 }
442 else
443 {
444 WaitSema(ps2cam_sema);
445 }
446
447 SignalSema(ps2cam_sema);
448}
449
450
451
454{
455 //int i;
456 static int width,height;
457
458
459 camResetDevice(dev);
460
461 //stop cam before we change settings then start it back later
462 camStopStream(dev);
463
464 camEnableSystem(dev);
465
466 /*set red led pin to out before turning it on*/
467 setReg8(dev,EYETOY_GPIO_IO_CTRL0, 0xee);
468
469 camSetUsbInit(dev);
470
471
472 //turn red led off by default
473 camTurnOffRedLed(dev);
474
475
476
477
478
479
480
481
482 /* some info sent to cam while peeking at
483 the usb log under windows
484 */
485 setReg8(dev, EYETOY_CREG_PWDN, 0x03);
486 setReg8(dev, EYETOY_CREG_EN_CLK0, 0x9f);
487 setReg8(dev, EYETOY_CREG_EN_CLK1, 0x0f);
488 setReg8(dev, 0xa2, 0x20);
489 setReg8(dev, 0xa3, 0x18);
490 setReg8(dev, 0xa4, 0x04);
491 setReg8(dev, 0xa5, 0x28);
492
493 setReg8(dev, 0x37, 0x00);
494 setReg8(dev, EYETOY_CREG_AUDIO_CLK, 0x02);
495 setReg8(dev, EYETOY_IREG_FRAR, 0x1f);
496 setReg8(dev, 0x17, 0x50);
497 setReg8(dev, 0x37, 0x00);
498
499 setReg8(dev, 0x40, 0xff);
500 setReg8(dev, 0x46, 0x00);
501 setReg8(dev, EYETOY_CREG_CAMERA_CLK,0x04);
502// setReg8(dev, 0xff, 0x00 );
503
504
505
506
507
508
509
510 // my Y Enhancement
511 setReg8(dev, 0xa8, 0x00 );
512 setReg8(dev, 0xa9, 0x20 );
513 setReg8(dev, 0xaa, 0x00 );
514
515
516
517
518
519
520 /* Select 8-bit input mode */
521 setReg8Mask(dev, EYETOY_IREG_DFR, 0x10, 0x10);
522
523 // 320x240
524 width = 320;
525 height= 240;
526
527 //set rez
528 setReg8(dev, EYETOY_IREG_H_SIZE, width>>4);
529 setReg8(dev, EYETOY_IREG_V_SIZE, height>>3);
530 //set offset
531 setReg8(dev, EYETOY_IREG_X_OFFSETL, 0x00);
532 setReg8(dev, EYETOY_IREG_X_OFFSETH, 0x00);
533 setReg8(dev, EYETOY_IREG_Y_OFFSETL, 0x00);
534 setReg8(dev, EYETOY_IREG_Y_OFFSETH, 0x00);
535 //div & color
536 //setReg8(dev, EYETOY_IREG_DIVIDER, 0x11);
537 camSetDIVIDER(dev, 1, 0, 1, 0);
538 setReg8(dev, EYETOY_IREG_FORMAT, 0x03);
539 setReg8(dev, 0x26, 0x00);
540
541
542 // 30 fps
543 setReg8(dev, 0xa4, 0x0c);
544 setReg8(dev, 0x23, 0xff);
545
546
547
548 // start cam after changing settings
549 camStartStream(dev);
550
551}
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566/* CAMERA LOW LEVEL FUNCTIONS */
567
568
570void PS2CamCallback(int resultCode, int bytes, void *arg)
571{
572 if(resultCode !=0)
573 printf("callback: result= %d, bytes= %d, arg= %p \n", resultCode, bytes, arg);
574
575 SignalSema(ps2cam_sema);
576}
577
578
580int setReg8(CAMERA_DEVICE *dev, unsigned char reg_id, unsigned char value)
581{
582 int ret;
583
584 WaitSema(ps2cam_sema);
585
586 ret = sceUsbdControlTransfer(dev->controll, USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1, 0, (unsigned short)reg_id, 1, &value, PS2CamCallback, (void*)ps2cam_sema);
587
588 if(ret != USB_RC_OK)
589 {
590 printf("sceUsbdControlTransfer failed in 'setReg8'.\n");
591 ret = -1;
592 }
593 else
594 {
595 WaitSema(ps2cam_sema);
596 }
597
598 SignalSema(ps2cam_sema);
599 return ret;
600}
601
602
603
604
606int getReg8(CAMERA_DEVICE *dev, unsigned char reg_id, unsigned char *value)
607{
608 int ret;
609
610 WaitSema(ps2cam_sema);
611
612 ret = sceUsbdControlTransfer(dev->controll,USB_DIR_IN|USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1, 0,(unsigned short)reg_id, 1, value, PS2CamCallback, (void*)ps2cam_sema);
613
614 if(ret != USB_RC_OK)
615 {
616 printf("sceUsbdControlTransfer failed in 'getRegValue'.\n");
617 ret = -1;
618 }
619 else
620 {
621 WaitSema(ps2cam_sema);
622 }
623
624
625 SignalSema(ps2cam_sema);
626 return ret;
627}
628
629
630
631
633int setReg8Mask(CAMERA_DEVICE *dev,unsigned char reg_id, unsigned char value, unsigned char mask)
634{
635 int ret;
636 unsigned char current, final;
637
638 ret = getReg8(dev, reg_id, &current);
639
640 if (ret < 0) return -1;
641
642 current &= (~mask);
643 value &= mask;
644 final = current | value;
645
646 ret = setReg8(dev, reg_id, final);
647 return ret;
648}
649
650int setReg16(CAMERA_DEVICE *dev, unsigned char reg_id, unsigned short value)
651{
652 int ret;
653
654 WaitSema(ps2cam_sema);
655
656 ret = sceUsbdControlTransfer(dev->controll,USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1, 0, (unsigned short)reg_id, 2, &value, PS2CamCallback, (void*)ps2cam_sema);
657
658
659 if(ret != USB_RC_OK)
660 {
661 printf("Usb: setReg16 Error (%d)\n",ret);
662 }else
663 {
664 WaitSema(ps2cam_sema);
665 }
666
667 SignalSema(ps2cam_sema);
668 return 0;
669}
670
671
672
674void PS2CamGetDeviceSring(CAMERA_DEVICE* dev, int index, char *str, int strmax)
675{
676 static char buff[50];
678 int ret;
679
680
681 WaitSema(ps2cam_sema);
682
683
684 ret = sceUsbdControlTransfer(dev->controll, 0x80, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) | index, 0, sizeof(buff), (char *)&buff[0], PS2CamCallback, (void*)ps2cam_sema);
685
686 if(ret != USB_RC_OK)
687 {
688 printf("Usb: Error getting string (%d)\n",ret);
689 }
690 else
691 {
692 int i;
693
694
695 WaitSema(ps2cam_sema);
696
697 sd = (UsbStringDescriptor *)&buff[0];
698 for(i=0;(i<(sd->bLength-4)/2 && i<strmax-1);i++)
699 {
700 str[i] = sd->wData[i] & 0xFF;
701 str[i+1] = 0x00;
702 }
703 }
704
705
706 SignalSema(ps2cam_sema);
707}
708
709
710
712int PS2CamSelectInterface(CAMERA_DEVICE* dev, int interface, int altSetting)
713{
714 int ret;
715 //int semh;
716 //iop_sema_t s;
717
718
719 WaitSema(ps2cam_sema);
720
721
722 ret = sceUsbdSetInterface(dev->controll, interface, altSetting, PS2CamCallback, (void*)ps2cam_sema);
723
724 if(ret != USB_RC_OK)
725 {
726 printf("Usb: PS2CamSelectInterface Error...\n");
727 }
728 else
729 {
730 WaitSema(ps2cam_sema);
731 }
732
733 SignalSema(ps2cam_sema);
734 return ret;
735}
736
737
738
739
740
741
744{
745 setReg8(dev,EYETOY_CREG_RESET1, 0x0f);
746 setReg8(dev,EYETOY_CREG_RESET1, 0x00);
747}
748
749
750void camEnableAutoLaunch(CAMERA_DEVICE *dev)
751{
752 setReg8(dev, EYETOY_CREG_SNAPSHOT, 23);
753}
754
755
756void camDisableAutoLaunch(CAMERA_DEVICE *dev)
757{
758 setReg8(dev, EYETOY_CREG_SNAPSHOT, 23);
759}
760
761
762void camClearSnapButton(CAMERA_DEVICE *dev)
763{
764 setReg8Mask(dev, EYETOY_CREG_SNAPSHOT, 03,02);
765}
766
767
768int camCheckAutoLaunch(CAMERA_DEVICE *dev)
769{
770 (void)dev;
771
772 //setReg8Mask(dev, EYETOY_CREG_SNAPSHOT, 10,10);
773 return 0;
774}
775
776
779{
780 setReg8(dev,0x5a, 0x6d);
781 setReg8(dev,0x53, 0x9b);
782 setReg8(dev,0x54, 0x0f);
783 setReg8(dev,0x5d, 0x03);
784 setReg8(dev,0x49, 0x01);
785 setReg8(dev,0x48, 0x00);
786}
787
788
791{
792 setReg8Mask(dev, EYETOY_CREG_EN_CLK0, 0x9b,0x9b);
793}
794
795
798{
799 setReg8(dev,0x51, 0x0f);
800 setReg8(dev,0x51, 0x00);
801}
802
803
806{
807 setReg8(dev,0x51, 0x0f);
808 setReg8(dev,0x51, 0x00);
809 setReg8(dev,0x22, 0x00);
810}
811
812
815{
816 setReg8(dev,0x51, 0x0f);
817 setReg8(dev,0x51, 0x00);
818 setReg8(dev,0x22, 0x1d);
819}
820
821
824{
825 setReg8Mask(dev, EYETOY_GPIO_DATA_OUT0, 0x01,0x01);
826}
827
828
831{
832 setReg8Mask(dev, EYETOY_GPIO_DATA_OUT0, 0x00,0x01);
833}
834
835
838{
839
840 setReg8(dev, EYETOY_CREG_RESET1, 0x00);
841}
842
843
844void camStopStream(CAMERA_DEVICE *dev)
845{
846
847 setReg8(dev, EYETOY_CREG_RESET1, 0x0f);
848}
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871int read_rslt;
872int read_byts;
873
874void PS2CamReadDataCallback(int resultCode, int bytes, void *arg)
875{
876 (void)arg;
877
878 //printf("read_data_callback: result= %d, bytes= %d, arg= %p \n", resultCode, bytes, arg);
879
880 read_rslt = resultCode;
881 read_byts = bytes;
882 SignalSema(ps2cam_sema);
883}
884
885
886
887
888int PS2CamReadData(CAMERA_DEVICE* dev, void *addr, int size)
889{
890 int ret;
891
892 WaitSema(ps2cam_sema);
893
894 ret = sceUsbdIsochronousTransfer(dev->stream, addr, size, 0, PS2CamReadDataCallback, (void*)ps2cam_sema);
895
896 if(ret != USB_RC_OK)
897 {
898 printf("Usb: Error sending sceUsbdIsochronousTransfer\n");
899 return -1;
900 }
901 else
902 {
903 WaitSema(ps2cam_sema);
904 }
905
906 SignalSema(ps2cam_sema);
907 return read_byts;
908}
909
910
911
912
913
914
915
916
917
918
919
920
921
922/* CAMERA MISC FUNTIONS */
923
924
925
926
927
928
929
930
931
932
933
934
935/* CAMERA RPC/EXPORTED FUNTIONS */
936
937
940{
941 static unsigned short ver[2];
942 int *ret;
943
944 ret = (int *)&ver[0];
945 ver[0] =DRIVER_VERSON_MAJOR;
946 ver[1] =DRIVER_VERSON_MINOR;
947
948 return *ret;
949}
950
951
952
954int PS2CamInit(int mode)
955{
956 (void)mode;
957
958 irx_initialized = 1;
959
960 return 0;
961}
962
963
964
967{
968 int i;
969 static int count;
970
971 if(! irx_initialized)
972 return CAM_ERROR_NOTINIT;
973
974
975 //scan device slots for camara(s)
976 for(i=0;i<MAX_CAM_DEVICE;i++)
977 {
978 if(Camera[i].status > 0)
979 {
980 count++;
981 }
982 }
983
984 return count;
985}
986
987
988
990int PS2CamOpenDevice(int device_index)
991{
992 int i;
993 CAMERA_DEVICE *cam = NULL;
994 PS2CAM_DEVICE_HANDLE *handle = NULL;
995 int index;
996
997 if(! irx_initialized)
998 return CAM_ERROR_NOTINIT;
999
1000 //search for a connected camera
1001 for(i=0,index=0;i<MAX_CAM_DEVICE;i++)
1002 {
1003 if(Camera[i].status > 0)
1004 {
1005 if(index == device_index)
1006 {
1007 cam = &Camera[i];
1008 break;
1009 }
1010
1011 index++;
1012 }
1013
1014
1015 //see the error code to return
1016 if((i+1) == (MAX_CAM_DEVICE))
1017 {
1018 if(index == 0)return CAM_ERROR_NODEVICE;
1019 if(index < device_index)return CAM_ERROR_BADRANGE;
1020 return CAM_ERROR_UNKNOWN;
1021 }
1022 }
1023
1024
1025
1026 //find a free device handle slot for the device
1027 for(i=0;i<MAX_CAM_DEVICE_HANDLE;i++)
1028 {
1029 if(CamHandle[i].status == 0)
1030 {
1031 handle = &CamHandle[i];
1032 break;
1033 }
1034
1035
1036 //see error to return
1037 if((i+1)== MAX_CAM_DEVICE_HANDLE)
1038 {
1039 return CAM_ERROR_MAXHANDLE;
1040 }
1041 }
1042
1043 if (handle == NULL)
1044 {
1045 return CAM_ERROR_MAXHANDLE;
1046 }
1047
1048
1049 //now we point handle attribute to the camera
1050 handle->status = 1;
1051 handle->cam = cam;
1052
1053 return handle->fd;
1054}
1055
1056
1057
1059int PS2CamCloseDevice(int handle)
1060{
1061 int i;
1062
1063 if(! irx_initialized)
1064 return CAM_ERROR_NOTINIT;
1065
1066 //find device handle and clear the status
1067 for(i=0;i<MAX_CAM_DEVICE_HANDLE;i++)
1068 {
1069 if(CamHandle[i].fd == handle)
1070 {
1071 CamHandle[i].status=0;
1072 break;
1073 }
1074
1075
1076 //see error to return
1077 if((i+1)== MAX_CAM_DEVICE_HANDLE)
1078 {
1079 return CAM_ERROR_BADHANDLE;
1080 }
1081 }
1082
1083 return 0;
1084}
1085
1086
1087
1090{
1091 CAMERA_DEVICE *dev;
1092
1093
1094
1095 if(! irx_initialized)
1096 return CAM_ERROR_NOTINIT;
1097
1098 if(handle > MAX_CAM_DEVICE_HANDLE || handle <= 0 || CamHandle[handle-1].status == 0)
1099 return CAM_ERROR_BADHANDLE;
1100
1101 if(CamHandle[handle-1].status == -1)
1103
1104 dev = CamHandle[handle-1].cam;
1105
1106
1107 return dev->status;
1108}
1109
1110
1111
1113int PS2CamGetDeviceInfo(int handle,int *info)
1114{
1115 static unsigned int size;
1118 PS2CAM_DEVICE_INFO *ptr;
1119 CAMERA_DEVICE *cam;
1120
1121 if(! irx_initialized)
1122 return CAM_ERROR_NOTINIT;
1123
1124 if(handle > MAX_CAM_DEVICE_HANDLE || handle <= 0 || CamHandle[handle-1].status == 0)
1125 return CAM_ERROR_BADHANDLE;
1126
1127 if(CamHandle[handle-1].status == -1)
1129
1130 ptr = (PS2CAM_DEVICE_INFO *)info;
1131 size = ptr->ssize;
1132
1133
1134
1135 if(size > sizeof(PS2CAM_DEVICE_INFO))
1136 {
1137 size = sizeof(PS2CAM_DEVICE_INFO);
1138 }
1139
1140
1141
1142 cam = CamHandle[handle-1].cam;
1143
1144 //get descripters
1145 dev = sceUsbdScanStaticDescriptor(cam->device_id, NULL, USB_DT_DEVICE);
1146
1147
1148 // now collect inf
1149 inf.ssize = size;
1150 inf.vendor_id = dev->idVendor;
1151 inf.product_id = dev->idProduct;
1152
1153 PS2CamGetDeviceSring(cam, dev->iManufacturer, (char *)&inf.vendor_name[0] , 32);
1154 PS2CamGetDeviceSring(cam, dev->iProduct, (char *)&inf.product_name[0], 32);
1155
1156
1157 if(dev->idProduct == PS2CAM_PROD_EYETOY)
1158 {
1159 strcpy((char *)&inf.model[0],"SLEH-00030\0");
1160 }
1161 else if(dev->idProduct == PS2CAM_PROD_EYETOY2)
1162 {
1163 strcpy((char *)&inf.model[0],"SLEH-00031\0");
1164 }
1165 else
1166 {
1167 strcpy((char *)&inf.model[0],"UNKNOWN\0");
1168 }
1169
1170 memcpy(info, &inf, size);
1171
1172 return 0;
1173}
1174
1175
1176
1178int PS2CamSetDeviceBandwidth(int handle, char bandwidth)
1179{
1180 int i;
1181 CAMERA_DEVICE *cam;
1182
1186
1187 if(! irx_initialized)
1188 return CAM_ERROR_NOTINIT;
1189
1190 if(handle > MAX_CAM_DEVICE_HANDLE || handle <= 0 || CamHandle[handle-1].status == 0)
1191 return CAM_ERROR_BADHANDLE;
1192
1193 if(CamHandle[handle-1].status == -1)
1195
1196
1197 cam = CamHandle[handle-1].cam;
1198
1199 cam->bandwidth = bandwidth;
1200
1201
1202
1203 //search for enpoint to open
1204 dev = sceUsbdScanStaticDescriptor(cam->device_id, NULL, USB_DT_DEVICE);
1205 intf = sceUsbdScanStaticDescriptor(cam->device_id, dev, USB_DT_INTERFACE);
1206
1207 for(i=0;i<cam->bandwidth;i++)
1208 {
1209 intf = sceUsbdScanStaticDescriptor(cam->device_id, intf, USB_DT_INTERFACE);
1210 }
1211
1212 endp= (UsbEndpointDescriptor *) ((char *) intf + intf->bLength);
1213
1214
1215 // close old endpoint and open new one
1216 sceUsbdClosePipe(cam->stream);
1217 cam->stream = sceUsbdOpenPipe(cam->device_id, endp);
1218
1219 cam->stream_pocket_size = (endp->wMaxPacketSizeHB * 256 + endp->wMaxPacketSizeLB);
1220 printf("bandwidth =%d\n",cam->stream_pocket_size);
1221
1222
1223
1224 camStopStream(cam);
1225
1226 switch(bandwidth)
1227 {
1228 case 0:
1230 break;
1231 case 1:
1232 PS2CamSelectInterface(cam,0,EYETOY_ALTERNATE_SIZE_384);
1233 break;
1234 case 2:
1235 PS2CamSelectInterface(cam,0,EYETOY_ALTERNATE_SIZE_512);
1236 break;
1237 case 3:
1238 PS2CamSelectInterface(cam,0,EYETOY_ALTERNATE_SIZE_768);
1239 break;
1240 case 4:
1241 PS2CamSelectInterface(cam,0,EYETOY_ALTERNATE_SIZE_896);
1242 break;
1243
1244 }
1245
1246 setReg8(cam, EYETOY_CREG_RESET1, 0x00);
1247
1248 return 0;
1249}
1250
1251
1252
1254int PS2CamReadPacket(int handle)
1255{
1256
1257 CAMERA_DEVICE *cam;
1258 int ret;
1259
1260
1261
1262 if(! irx_initialized)
1263 return CAM_ERROR_NOTINIT;
1264
1265 if(handle > MAX_CAM_DEVICE_HANDLE || handle <= 0 || CamHandle[handle-1].status == 0)
1266 return CAM_ERROR_BADHANDLE;
1267
1268 if(CamHandle[handle-1].status == -1)
1270
1271
1272 cam = CamHandle[handle-1].cam;
1273
1274 if(cam == NULL) return CAM_ERROR_INVALIDDEVICE;
1275 if(cam->status < 2) return CAM_ERROR_DEVNOTREADY;
1276
1277
1278
1279
1280 switch(cam->bandwidth)
1281 {
1283 ret =0;
1284 break;
1285 case EYETOY_ALTERNATE_SIZE_384:
1286 ret = PS2CamReadData(cam, &campacket[0], 384);
1287 break;
1288 case EYETOY_ALTERNATE_SIZE_512:
1289 ret = PS2CamReadData(cam, &campacket[0], 512);
1290 break;
1291 case EYETOY_ALTERNATE_SIZE_768:
1292 ret = PS2CamReadData(cam, &campacket[0], 768);
1293 break;
1294 case EYETOY_ALTERNATE_SIZE_896:
1295 ret = PS2CamReadData(cam, &campacket[0], 896);
1296 break;
1297 default :
1298 ret = PS2CamReadData(cam, &campacket[0], 384);
1299 break;
1300 }
1301
1302 return ret;
1303}
1304
1305
1306
1308int PS2CamSetLEDMode(int handle, int mode)
1309{
1310 CAMERA_DEVICE *cam;
1311
1312 if(! irx_initialized)
1313 return CAM_ERROR_NOTINIT;
1314
1315 if(handle > MAX_CAM_DEVICE_HANDLE || handle <= 0 || CamHandle[handle-1].status == 0)
1316 return CAM_ERROR_BADHANDLE;
1317
1318 if(CamHandle[handle-1].status == -1)
1320
1321
1322 cam = CamHandle[handle-1].cam;
1323
1324 if(cam == NULL) return CAM_ERROR_INVALIDDEVICE;
1325 if(cam->status < 2) return CAM_ERROR_DEVNOTREADY;
1326
1327
1328
1329 switch(mode)
1330 {
1331 case 0://off
1332 camTurnOffRedLed(cam);
1333 break;
1334 case 1://on
1335 camTurnOnRedLed(cam);
1336 break;
1337// case 2://flash
1338//
1339// break;
1340 default:
1341 camTurnOffRedLed(cam);
1342 break;
1343 }
1344
1345
1346 return 0;
1347}
1348
1349
1350
1351
1353int PS2CamSetDeviceConfig(int handle, void *config)
1354{
1355 CAMERA_DEVICE *cam;
1357
1358 if(! irx_initialized)
1359 return CAM_ERROR_NOTINIT;
1360
1361 if(handle > MAX_CAM_DEVICE_HANDLE || handle <= 0 || CamHandle[handle-1].status == 0)
1362 return CAM_ERROR_BADHANDLE;
1363
1364 if(CamHandle[handle-1].status == -1)
1366
1367
1368 cam = CamHandle[handle-1].cam;
1369
1370 if(cam == NULL) return CAM_ERROR_INVALIDDEVICE;
1371 if(cam->status < 2) return CAM_ERROR_DEVNOTREADY;
1372
1373
1374
1375 cfg = (PS2CAM_DEVICE_CONFIG *)config;
1376
1377 // stop data
1378 camStopStream(cam);
1379
1380
1381 //set rez
1382 if(cfg->mask & CAM_CONFIG_MASK_DIMENSION)
1383 {
1384
1385 setReg8(cam, EYETOY_IREG_H_SIZE, cfg->width >>4);
1386 setReg8(cam, EYETOY_IREG_V_SIZE, cfg->height >>3);
1387 }
1388
1389 //set offset
1390 if(cfg->mask & CAM_CONFIG_MASK_OFFSET)
1391 {
1392 unsigned char *p;
1393
1394 p = (unsigned char *)&cfg->x_offset;
1395 setReg8(cam, EYETOY_IREG_X_OFFSETL, p[0]);
1396 setReg8(cam, EYETOY_IREG_X_OFFSETH, p[1]);
1397
1398 p = (unsigned char *)&cfg->y_offset;
1399 setReg8(cam, EYETOY_IREG_Y_OFFSETL, p[0]);
1400 setReg8(cam, EYETOY_IREG_Y_OFFSETH, p[1]);
1401 }
1402
1403 //set divider
1404 if(cfg->mask & CAM_CONFIG_MASK_DIVIDER)
1405 {
1406 if(cfg->h_divider > 4)cfg->h_divider = 4;
1407 if(cfg->v_divider > 4)cfg->v_divider = 4;
1408
1409 camSetDIVIDER(cam, cfg->h_divider, 0, cfg->v_divider, 0);
1410 }
1411
1412 //set framerate
1413 if(cfg->mask & CAM_CONFIG_MASK_FRAMERATE)
1414 {
1415
1416 switch(cfg->framerate)
1417 {
1418 case 5:
1419 setReg8(cam, 0xa4, 0x04);
1420 setReg8(cam, 0x23, 0x1b);
1421 break;
1422 case 10:
1423 setReg8(cam, 0xa4, 0x04);
1424 setReg8(cam, 0x23, 0x1f);
1425 break;
1426 case 15:
1427 setReg8(cam, 0xa4, 0x04);
1428 setReg8(cam, 0x23, 0xff);
1429 break;
1430 case 25:
1431 setReg8(cam, 0xa4, 0x0c);
1432 setReg8(cam, 0x23, 0x1f);
1433 break;
1434 case 30:
1435 setReg8(cam, 0xa4, 0x0c);
1436 setReg8(cam, 0x23, 0xff);
1437 break;
1438 default:
1439 setReg8(cam, 0xa4, 0x0c);
1440 setReg8(cam, 0x23, 0xff);
1441 break;
1442 }
1443 }
1444
1445
1446 // restart data
1447 camStartStream(cam);
1448
1449 return 0;
1450}
1451
1452
1453void *rpcCommandHandler(u32 command, void *buffer, int size)
1454{
1455 int* buf = (int*) buffer;
1456 //char* ptr = (char*) buffer;
1457 int ret = 0;
1458
1459 (void)size;
1460
1461 switch (command)
1462 {
1464 ret = PS2CamGetIRXVersion();
1465
1466 break;
1468 ret = PS2CamInit(buf[0]);
1469
1470 break;
1472 ret = PS2CamGetDeviceCount();
1473
1474 break;
1475 case PS2CAM_RPC_OPENDEVICE:
1476 ret = PS2CamOpenDevice(buf[0]);
1477
1478 break;
1479 case PS2CAM_RPC_CLOSEDEVICE:
1480 ret = PS2CamCloseDevice(buf[0]);
1481
1482 break;
1484 ret = PS2CamGetDeviceStatus(buf[0]);
1485
1486 break;
1488 ret = PS2CamGetDeviceInfo(buf[0],&buf[1]);
1489
1490 break;
1492 ret = PS2CamSetDeviceBandwidth(buf[0], buf[1]);
1493
1494 break;
1496 ret = PS2CamReadPacket(buf[0]);
1497
1498 //get mem address so i can access it from ee
1499 buf[1] = (int )&campacket[0];
1500
1501 break;
1502
1504 ret = PS2CamSetLEDMode(buf[0], buf[1]);
1505
1506 break;
1507 case PS2CAM_RPC_SETDEVCONFIG:
1508 ret = PS2CamSetDeviceConfig(buf[0], &buf[1]);
1509
1510 break;
1511 default:
1513
1514 break;
1515 }
1516
1517
1518 buf[0] = ret;
1519
1520
1521 return buffer;
1522}
1523
1524
1525
1526
1527
1528
1529/*EOF*/
void PS2CamSetDeviceConfiguration(CAMERA_DEVICE *dev, int id)
Definition ps2cam.c:429
void camResetDevice(CAMERA_DEVICE *dev)
Definition ps2cam.c:743
void PS2CamCallback(int resultCode, int bytes, void *arg)
Definition ps2cam.c:570
int PS2CamDisconnect(int devId)
Definition ps2cam.c:297
int PS2CamReadPacket(int handle)
Definition ps2cam.c:1254
int getReg8(CAMERA_DEVICE *dev, unsigned char reg_id, unsigned char *value)
Definition ps2cam.c:606
void camStartStream(CAMERA_DEVICE *dev)
Definition ps2cam.c:837
int PS2CamGetDeviceCount(void)
Definition ps2cam.c:966
int PS2CamSetLEDMode(int handle, int mode)
Definition ps2cam.c:1308
int PS2CamConnect(int devId)
Definition ps2cam.c:213
int PS2CamGetDeviceInfo(int handle, int *info)
Definition ps2cam.c:1113
int setReg8Mask(CAMERA_DEVICE *dev, unsigned char reg_id, unsigned char value, unsigned char mask)
Definition ps2cam.c:633
int PS2CamInit(int mode)
Definition ps2cam.c:954
int PS2CamCloseDevice(int handle)
Definition ps2cam.c:1059
int PS2CamProbe(int devId)
Definition ps2cam.c:143
void PS2CamSetDeviceDefaults(CAMERA_DEVICE *dev)
Definition ps2cam.c:453
void camTurnOffRedLed(CAMERA_DEVICE *dev)
Definition ps2cam.c:830
int PS2CamSetDeviceBandwidth(int handle, char bandwidth)
Definition ps2cam.c:1178
int PS2CamSelectInterface(CAMERA_DEVICE *dev, int interface, int altSetting)
Definition ps2cam.c:712
void camResetUsb(CAMERA_DEVICE *dev)
Definition ps2cam.c:797
int PS2CamGetIRXVersion(void)
Definition ps2cam.c:939
void camDisableSystem(CAMERA_DEVICE *dev)
Definition ps2cam.c:790
void camEnableSystem(CAMERA_DEVICE *dev)
Definition ps2cam.c:778
void camTurnOnRedLed(CAMERA_DEVICE *dev)
Definition ps2cam.c:823
void camSetUsbInit(CAMERA_DEVICE *dev)
Definition ps2cam.c:805
int PS2CamSetDeviceConfig(int handle, void *config)
Definition ps2cam.c:1353
int PS2CamOpenDevice(int device_index)
Definition ps2cam.c:990
void PS2CamGetDeviceSring(CAMERA_DEVICE *dev, int index, char *str, int strmax)
Definition ps2cam.c:674
int PS2CamInitDriver(void)
Definition ps2cam.c:109
void camSetUsbWork(CAMERA_DEVICE *dev)
Definition ps2cam.c:814
int PS2CamGetDeviceStatus(int handle)
Definition ps2cam.c:1089
int setReg8(CAMERA_DEVICE *dev, unsigned char reg_id, unsigned char value)
Definition ps2cam.c:580
void PS2CamInitializeNewDevice(CAMERA_DEVICE *cam)
Definition ps2cam.c:382
unsigned short framerate
Definition ps2cam_rpc.h:140
#define PS2CAM_VEND_DLINK
Definition ps2cam.h:43
unsigned short x_offset
Definition ps2cam_rpc.h:132
unsigned short width
Definition ps2cam_rpc.h:128
unsigned char v_divider
Definition ps2cam_rpc.h:138
unsigned short vendor_id
Definition ps2cam_rpc.h:109
unsigned int mask
Definition ps2cam_rpc.h:126
unsigned char vendor_name[32]
Definition ps2cam_rpc.h:113
#define EYETOY_ALTERNATE_SIZE_0
Definition ps2cam.h:102
unsigned int ssize
Definition ps2cam_rpc.h:107
unsigned short product_id
Definition ps2cam_rpc.h:111
unsigned char product_name[32]
Definition ps2cam_rpc.h:115
unsigned char h_divider
Definition ps2cam_rpc.h:136
#define PS2CAM_PROD_EYETOY4
Definition ps2cam.h:55
#define PS2CAM_PROD_EYETOY
Definition ps2cam.h:49
unsigned char model[16]
Definition ps2cam_rpc.h:117
#define PS2CAM_VEND_SONY
Definition ps2cam.h:41
unsigned short y_offset
Definition ps2cam_rpc.h:134
#define PS2CAM_PROD_VISUALSTREAM
Definition ps2cam.h:58
#define PS2CAM_PROD_EYETOY2
Definition ps2cam.h:51
#define PS2CAM_PROD_EYETOY3
Definition ps2cam.h:53
unsigned short height
Definition ps2cam_rpc.h:130
#define CAM_STATUS_CONNECTED
Definition ps2cam_rpc.h:45
#define CAM_ERROR_DISCONNECTED
Definition ps2cam_rpc.h:75
#define CAM_ERROR_COMMUNKNOWN
Definition ps2cam_rpc.h:61
#define PS2CAM_RPC_GETDEVSTATUS
Definition ps2cam_rpc.h:24
#define CAM_ERROR_BADHANDLE
Definition ps2cam_rpc.h:73
#define PS2CAM_RPC_SETDEVBANDWIDTH
Definition ps2cam_rpc.h:28
#define CAM_ERROR_BADRANGE
Definition ps2cam_rpc.h:67
#define PS2CAM_RPC_GETIRXVERSION
Definition ps2cam_rpc.h:16
#define CAM_ERROR_UNKNOWN
Definition ps2cam_rpc.h:69
#define CAM_ERROR_MAXHANDLE
Definition ps2cam_rpc.h:71
#define PS2CAM_RPC_GETDEVCOUNT
Definition ps2cam_rpc.h:20
#define PS2CAM_RPC_SETLEDMODE
Definition ps2cam_rpc.h:32
#define PS2CAM_RPC_GETDEVINFO
Definition ps2cam_rpc.h:26
#define PS2CAM_RPC_INITIALIZE
Definition ps2cam_rpc.h:18
#define CAM_ERROR_DEVNOTREADY
Definition ps2cam_rpc.h:63
#define CAM_STATUS_NOTCONNECTED
Definition ps2cam_rpc.h:43
#define CAM_ERROR_NODEVICE
Definition ps2cam_rpc.h:65
#define CAM_STATUS_CONNECTEDREADY
Definition ps2cam_rpc.h:47
#define PS2CAM_RPC_READPACKET
Definition ps2cam_rpc.h:30
#define USB_RC_OK
Definition usbd.h:238
u32 count
start sector of fragmented bd/file