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 
35 static usbWebcam_t *g_usbWebcam = NULL;
36 typedef __s32 (*active_buf) (struct uvc_streaming *stream) ;
37 
38 static active_buf p_func;
39 
transport(__u32 addr0,__u32 addr1,__u32 addr2)40 static __s32  transport(__u32 addr0, __u32 addr1, __u32 addr2)
41 {
42     UVCDev_t *UVCDev = NULL;
43 
44     UVCDev = g_usbWebcam->UVCDev;
45 
46     hal_log_info("usb:(%x,%x,%x),(%x,%x,%x), %x,%x\n", addr0, addr1, addr2,
47             UVCDev->frame_buf0, UVCDev->frame_buf1, UVCDev->frame_buf2, (unsigned int)UVCDev->active_buf, (unsigned int)g_usbWebcam->uvc_irq);
48     if((void *)addr0 != NULL)
49     {
50         UVCDev->frame_len  = 640*480;
51         UVCDev->exchange_buf = g_usbWebcam->uvc_irq;
52 
53         UVCDev->frame_buf0      = addr0;
54         UVCDev->frame_buf1      = 0;
55         UVCDev->frame_buf2      = 0;
56 
57         if(UVCDev->active_buf==NULL)
58         {
59             //printf("UVCDev->active_buf==NULL p_func=%x\n",p_func);
60             UVCDev->active_buf = p_func;
61         }
62         UVCDev->active_buf(UVCDev->streams);
63     }
64     else
65     {
66         if(UVCDev->active_buf) {
67             p_func = UVCDev->active_buf;
68         }
69         UVCDev->frame_len       = 0;
70         UVCDev->active_buf      = NULL;
71         UVCDev->exchange_buf    = NULL;
72         UVCDev->frame_buf0      = 0;
73         UVCDev->frame_buf1      = 0;
74         UVCDev->frame_buf2      = 0;
75         //printf("transport UVCDev->active_buf==NULL\n");
76     }
77 
78     return 0;
79 }
80 
usbWebcam_probe(UVCDev_t * UVCDev)81 __s32 usbWebcam_probe(UVCDev_t *UVCDev)
82 {
83     usbWebcam_t *usbWebcam = NULL;
84     __u8 err = 0;
85     __s32 ret = 0;
86     extern __s32 DRV_WEBCAM_MInit(void);
87     extern __s32 DEV_WEBCAM_Ioctrl(__hdle hWebcam, __u32 cmd, __s32 aux, void *pbuffer);
88 
89     if(UVCDev == NULL || g_usbWebcam == NULL){
90         hal_log_err("ERR: usbWebcamProbe: input error, 0x%x,0x%x\n", (unsigned int)UVCDev, (unsigned int)g_usbWebcam);
91         return -1;
92     }
93     usbWebcam = g_usbWebcam;
94 
95     usbWebcam->UVCDev = UVCDev;
96     UVCDev->Extern = usbWebcam;
97 
98     DRV_WEBCAM_MInit();
99 //  usbWebcam->webcamHdl = fopen("b:\\UVC\\WEBCAM","r+");
100     if( usbWebcam->webcamHdl == NULL )
101     {
102         __s32 i;
103 
104         hal_log_info("don't fined drv_webcam\n");
105         for( i = 0; i < UVC_URBS; ++i )
106         {
107             UVCDev->urb_mem[i] = usbWebcam->urb_mem[i];
108         }
109     } else {
110         printf("%s %d %s usbWebcam->webcamHdl == NULL!\n", __FILE__, __LINE__, __func__);
111     }
112 
113     usbWebcam_v4l2_init(UVCDev);
114 
115     ret = usbWebcam_v4l2_capture_on(UVCDev);
116 
117     if( usbWebcam->webcamHdl )
118     {
119         DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_GET_IRQ_CB, 0, (void*)(&usbWebcam->uvc_irq));
120         DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_SET_TRANSPORT_CB, 0, (void*)transport);
121         DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_WEBCAM_INSERT, 0, 0);
122     }
123 
124     /* Notice: 由于新的设备上来以后,系统的设备管理告知应用程序,因此只能由驱动告知应用程序 */
125 //  USB_OS_esKSRV_SendMsg(KMSG_USR_SYSTEM_WEBCAM_PLUGIN, KMSG_PRIO_HIGH);
126     hal_log_info("webcam plug in message...\n");
127 
128     return ret;
129 }
130 
usbWebcam_init(void)131 void* usbWebcam_init(void)
132 {
133     usbWebcam_t *usbWebcam = NULL;
134     __u8 err = 0;
135     __s32 ret = 0;
136 
137     /* 创建一个Webcam设备,并且初始化 */
138     usbWebcam = (usbWebcam_t *)hal_malloc(sizeof(usbWebcam_t));
139     if(usbWebcam == NULL){
140         hal_log_err("ERR: hal_malloc failed\n");
141         return NULL;
142     }
143 
144     memset(usbWebcam, 0, sizeof(usbWebcam_t));
145     g_usbWebcam = usbWebcam;
146     {
147         __s32 i;
148 
149         for( i = 0; i < UVC_URBS; ++i )
150         {
151             usbWebcam->urb_mem[i] = (__u32)hal_malloc(UVC_MAX_PACKETS*3);
152             if (!usbWebcam->urb_mem[i]) {
153                 hal_log_info("esMEMS_Malloc fail\n");
154             }
155         }
156     }
157 
158     return (void*)usbWebcam;
159 
160 }
161 
usbWebcam_remove(UVCDev_t * UVCDev)162 __s32 usbWebcam_remove(UVCDev_t * UVCDev)
163 {
164     usbWebcam_t *usbWebcam = NULL;
165     extern __s32 DEV_WEBCAM_Ioctrl(__hdle hWebcam, __u32 cmd, __s32 aux, void *pbuffer);
166 
167     if(UVCDev == NULL){
168         hal_log_err("ERR: usbWebcamRemove: input error\n");
169         return -1;
170     }
171 
172     usbWebcam = UVCDev->Extern;
173     if(usbWebcam == NULL){
174         hal_log_err("ERR: usbWebcamRemove: usbWebcam == NULL\n");
175         return -1;
176     }
177 
178     if( usbWebcam->webcamHdl )
179     {
180         DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_SET_TRANSPORT_CB, 0, 0);
181         DEV_WEBCAM_Ioctrl(usbWebcam->webcamHdl, DRV_WENCAM_CMD_WEBCAM_MOVED, (__s32)UVCDev->frame_buf0, (void*)UVCDev->frame_buf1);
182     }
183 
184 //  USB_OS_esKSRV_SendMsg(KMSG_USR_SYSTEM_WEBCAM_PLUGOUT, KMSG_PRIO_HIGH);
185     hal_log_info("webcam plug out message...\n");
186 
187     usbWebcam->UVCDev = NULL;
188     UVCDev->Extern = NULL;
189 
190     UVCDev->exchange_buf    = NULL;
191     UVCDev->frame_buf0      = 0;
192     UVCDev->frame_buf1      = 0;
193     UVCDev->frame_buf2      = 0;
194 
195     usbWebcam_v4l2_capture_off(UVCDev);//关中断发生器
196 
197     usbWebcam_v4l2_exit(UVCDev);
198 
199     if( usbWebcam->webcamHdl ) {
200         fclose(usbWebcam->webcamHdl);
201     }
202 
203     return 0;
204 }
205 
usbWebcam_exit(void * arg)206 __s32 usbWebcam_exit(void* arg)
207 {
208     usbWebcam_t *usbWebcam = (usbWebcam_t *)arg;
209     __u8 err = 0;
210 
211 
212     if(usbWebcam == NULL){
213         hal_log_err("ERR: usbWebcamRemove: usbWebcam == NULL\n");
214         return -1;
215     }
216 
217     {
218         __s32 i;
219 
220         for (i = 0; i < UVC_URBS; ++i) {
221             if (usbWebcam->urb_mem[i]) {
222                 hal_free((void*)usbWebcam->urb_mem[i]);
223                 usbWebcam->urb_mem[i] = 0;
224             }
225         }
226     }
227 
228     hal_free(usbWebcam);
229 
230     return 0;
231 }
232 
233 
234 
235 
236