usbWebcam.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. ********************************************************************************
  3. * USB UVC Driver
  4. *
  5. * (c) Copyright 2010-2015, All winners Co,Ld.
  6. * All Right Reserved
  7. *
  8. * FileName : usbWebcam.h
  9. *
  10. * Author : Kingvan.Tong
  11. *
  12. * Date : 2013.03.26
  13. *
  14. * Description : USB Webcam
  15. *
  16. * Others : NULL
  17. *
  18. * History:
  19. * <time> <version > <author> <desc>
  20. * 2013.03.26 1.0 Kingvan.Tong build this file
  21. *
  22. ********************************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "usb_os_platform.h"
  27. #include "error.h"
  28. #include "video.h"
  29. #include "uvcvideo.h"
  30. #include "usbWebcam.h"
  31. #include "hal_mem.h"
  32. #include "hal_log.h"
  33. #include "drv_webcam.h"
  34. static usbWebcam_t *g_usbWebcam = NULL;
  35. typedef __s32 (*active_buf) (struct uvc_streaming *stream) ;
  36. static active_buf p_func;
  37. static __s32 transport(__u32 addr0, __u32 addr1, __u32 addr2)
  38. {
  39. UVCDev_t *UVCDev = NULL;
  40. UVCDev = g_usbWebcam->UVCDev;
  41. hal_log_info("usb:(%x,%x,%x),(%x,%x,%x), %x,%x\n", addr0, addr1, addr2,
  42. UVCDev->frame_buf0, UVCDev->frame_buf1, UVCDev->frame_buf2, (unsigned int)UVCDev->active_buf, (unsigned int)g_usbWebcam->uvc_irq);
  43. if((void *)addr0 != NULL)
  44. {
  45. UVCDev->frame_len = 640*480;
  46. UVCDev->exchange_buf = g_usbWebcam->uvc_irq;
  47. UVCDev->frame_buf0 = addr0;
  48. UVCDev->frame_buf1 = 0;
  49. UVCDev->frame_buf2 = 0;
  50. if(UVCDev->active_buf==NULL)
  51. {
  52. //printf("UVCDev->active_buf==NULL p_func=%x\n",p_func);
  53. UVCDev->active_buf = p_func;
  54. }
  55. UVCDev->active_buf(UVCDev->streams);
  56. }
  57. else
  58. {
  59. if(UVCDev->active_buf) {
  60. p_func = UVCDev->active_buf;
  61. }
  62. UVCDev->frame_len = 0;
  63. UVCDev->active_buf = NULL;
  64. UVCDev->exchange_buf = NULL;
  65. UVCDev->frame_buf0 = 0;
  66. UVCDev->frame_buf1 = 0;
  67. UVCDev->frame_buf2 = 0;
  68. //printf("transport UVCDev->active_buf==NULL\n");
  69. }
  70. return 0;
  71. }
  72. __s32 usbWebcam_probe(UVCDev_t *UVCDev)
  73. {
  74. usbWebcam_t *usbWebcam = NULL;
  75. __u8 err = 0;
  76. __s32 ret = 0;
  77. extern __s32 DRV_WEBCAM_MInit(void);
  78. extern __s32 DEV_WEBCAM_Ioctrl(__hdle hWebcam, __u32 cmd, __s32 aux, void *pbuffer);
  79. if(UVCDev == NULL || g_usbWebcam == NULL){
  80. hal_log_err("ERR: usbWebcamProbe: input error, 0x%x,0x%x\n", (unsigned int)UVCDev, (unsigned int)g_usbWebcam);
  81. return -1;
  82. }
  83. usbWebcam = g_usbWebcam;
  84. usbWebcam->UVCDev = UVCDev;
  85. UVCDev->Extern = usbWebcam;
  86. DRV_WEBCAM_MInit();
  87. // usbWebcam->webcamHdl = fopen("b:\\UVC\\WEBCAM","r+");
  88. if( usbWebcam->webcamHdl == NULL )
  89. {
  90. __s32 i;
  91. hal_log_info("don't fined drv_webcam\n");
  92. for( i = 0; i < UVC_URBS; ++i )
  93. {
  94. UVCDev->urb_mem[i] = usbWebcam->urb_mem[i];
  95. }
  96. } else {
  97. printf("%s %d %s usbWebcam->webcamHdl == NULL!\n", __FILE__, __LINE__, __func__);
  98. }
  99. usbWebcam_v4l2_init(UVCDev);
  100. ret = usbWebcam_v4l2_capture_on(UVCDev);
  101. if( usbWebcam->webcamHdl )
  102. {
  103. DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_GET_IRQ_CB, 0, (void*)(&usbWebcam->uvc_irq));
  104. DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_SET_TRANSPORT_CB, 0, (void*)transport);
  105. DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_WEBCAM_INSERT, 0, 0);
  106. }
  107. /* Notice: 由于新的设备上来以后,系统的设备管理告知应用程序,因此只能由驱动告知应用程序 */
  108. // USB_OS_esKSRV_SendMsg(KMSG_USR_SYSTEM_WEBCAM_PLUGIN, KMSG_PRIO_HIGH);
  109. hal_log_info("webcam plug in message...\n");
  110. return ret;
  111. }
  112. void* usbWebcam_init(void)
  113. {
  114. usbWebcam_t *usbWebcam = NULL;
  115. __u8 err = 0;
  116. __s32 ret = 0;
  117. /* 创建一个Webcam设备,并且初始化 */
  118. usbWebcam = (usbWebcam_t *)hal_malloc(sizeof(usbWebcam_t));
  119. if(usbWebcam == NULL){
  120. hal_log_err("ERR: hal_malloc failed\n");
  121. return NULL;
  122. }
  123. memset(usbWebcam, 0, sizeof(usbWebcam_t));
  124. g_usbWebcam = usbWebcam;
  125. {
  126. __s32 i;
  127. for( i = 0; i < UVC_URBS; ++i )
  128. {
  129. usbWebcam->urb_mem[i] = (__u32)hal_malloc(UVC_MAX_PACKETS*3);
  130. if (!usbWebcam->urb_mem[i]) {
  131. hal_log_info("esMEMS_Malloc fail\n");
  132. }
  133. }
  134. }
  135. return (void*)usbWebcam;
  136. }
  137. __s32 usbWebcam_remove(UVCDev_t * UVCDev)
  138. {
  139. usbWebcam_t *usbWebcam = NULL;
  140. extern __s32 DEV_WEBCAM_Ioctrl(__hdle hWebcam, __u32 cmd, __s32 aux, void *pbuffer);
  141. if(UVCDev == NULL){
  142. hal_log_err("ERR: usbWebcamRemove: input error\n");
  143. return -1;
  144. }
  145. usbWebcam = UVCDev->Extern;
  146. if(usbWebcam == NULL){
  147. hal_log_err("ERR: usbWebcamRemove: usbWebcam == NULL\n");
  148. return -1;
  149. }
  150. if( usbWebcam->webcamHdl )
  151. {
  152. DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_SET_TRANSPORT_CB, 0, 0);
  153. DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_WEBCAM_MOVED, (__s32)UVCDev->frame_buf0, (void*)UVCDev->frame_buf1);
  154. }
  155. // USB_OS_esKSRV_SendMsg(KMSG_USR_SYSTEM_WEBCAM_PLUGOUT, KMSG_PRIO_HIGH);
  156. hal_log_info("webcam plug out message...\n");
  157. usbWebcam->UVCDev = NULL;
  158. UVCDev->Extern = NULL;
  159. UVCDev->exchange_buf = NULL;
  160. UVCDev->frame_buf0 = 0;
  161. UVCDev->frame_buf1 = 0;
  162. UVCDev->frame_buf2 = 0;
  163. usbWebcam_v4l2_capture_off(UVCDev);//关中断发生器
  164. usbWebcam_v4l2_exit(UVCDev);
  165. if( usbWebcam->webcamHdl ) {
  166. fclose(usbWebcam->webcamHdl);
  167. }
  168. return 0;
  169. }
  170. __s32 usbWebcam_exit(void* arg)
  171. {
  172. usbWebcam_t *usbWebcam = (usbWebcam_t *)arg;
  173. __u8 err = 0;
  174. if(usbWebcam == NULL){
  175. hal_log_err("ERR: usbWebcamRemove: usbWebcam == NULL\n");
  176. return -1;
  177. }
  178. {
  179. __s32 i;
  180. for (i = 0; i < UVC_URBS; ++i) {
  181. if (usbWebcam->urb_mem[i]) {
  182. hal_free((void*)usbWebcam->urb_mem[i]);
  183. usbWebcam->urb_mem[i] = 0;
  184. }
  185. }
  186. }
  187. hal_free(usbWebcam);
  188. return 0;
  189. }