Camera[1] 驱动V4L2分析

Posted we1less

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Camera[1] 驱动V4L2分析相关的知识,希望对你有一定的参考价值。

//获取内核版本
godv@godv-OptiPlex-7070:~$ uname -r
4.15.0-142-generic

  这里是一个linux内核的查询网站

https://lxr.missinglinkelectronics.com/linux/

insmod  xxx.ko    //驱动加载命令

modprobe    xxx    //依赖加载        modprobe -r    xxx     //卸载驱动


//字符设备驱动:
 static const struct file_operations v4l2_fops = {                                                                
     .owner = THIS_MODULE,
     .read = v4l2_read,
     .write = v4l2_write,
     .open = v4l2_open,
     .get_unmapped_area = v4l2_get_unmapped_area,
     .mmap = v4l2_mmap,
     .unlocked_ioctl = v4l2_ioctl,
     .release = v4l2_release,
     .poll = v4l2_poll,
     .llseek = no_llseek,
 };
		
//-------------------------------------------------------
//v4l2相关的操作方法:
 static const struct v4l2_file_operations vivi_fops = {
     .owner      = THIS_MODULE,
     .open           = v4l2_fh_open,                                                                              
     .release        = vb2_fop_release,
     .read           = vb2_fop_read,
     .poll       = vb2_fop_poll,
     .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
     .mmap           = vb2_fop_mmap,
 };
	

 摄像头驱动程序必需的11个ioctl:

//摄像头驱动程序必需的11个ioctl:
 /*表示它是一个摄像头设备*/
 .vidioc_querycap      = vidioc_querycap,
 /* 用于列举、获得、测试、设置摄像头的数据的格式 */
 vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
 .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
 /* 缓冲区操作: 申请/查询/放入队列/取出队列 */
 .vidioc_reqbufs       = vidioc_reqbufs,
 .vidioc_querybuf      = vidioc_querybuf,
 .vidioc_qbuf          = vidioc_qbuf,
 .vidioc_dqbuf         = vidioc_dqbuf,
 /*启动/停止*/
 .vidioc_streamon      = vidioc_streamon,
 .vidioc_streamoff     = vidioc_streamoff,

//命令码
vidioc_querycap   VIDIOC_QUERYCAP			
vidioc_s_fmt      VIDIOC_S_FMT			
vidioc_reqbufs    VIDIOC_REQBUFS			
			      VIDIOC_QUERYBUF
				  VIDIOC_STREAMON
				  VIDIOC_DQBUF

android 中相机的启动服务

Android中camera的框架结构
	4.1camera服务注册过程
	    sp<ProcessState> proc(ProcessState::self());
        sp<IServiceManager> sm = defaultServiceManager();                                                            
        CameraService::instantiate();
        ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();

		 static void instantiate() { publish(); }
					return sm->addService(String16("media.camera", new CameraService());  
					
	4.2客户端获取服务的过程
		private void initCamera()  
		{  
			Camera mCamera = Camera.open();  
			Camera.Parameters mParameters = mCamera.getParameters();  
			mParameters.setPictureFormat(PixelFormat.JPEG);  
			mCamera.setParameters(mParameters);  
			mCamera.setPreviewDisplay(mSurfaceHolder);  
			mCamera.startPreview();  
			mCamera.takePicture(null, null , mJpegCallback);  
		}  

		Vi -t Camera
		base/core/java/android/hardware/Camera.java
				public static Camera open() {
						int numberOfCameras = getNumberOfCameras();
						CameraInfo cameraInfo = new CameraInfo();                                                               
						for (int i = 0; i < numberOfCameras; i++) {
							getCameraInfo(i, cameraInfo);
							if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
								return new Camera(i);
									int err = cameraInitNormal(cameraId);
										cameraInitVersion(cameraId, CAMERA_HAL_API_VERSION_NORMAL_CONNECT);   
											native_setup(new WeakReference<Camera>(this), cameraId, halVersion, ActivityThread.currentOpPackageName());

			base/core/jni/android_hardware_Camera.cpp:959
			 952 static JNINativeMethod camMethods[] = {
					{ "getNumberOfCameras","()I",(void *)android_hardware_Camera_getNumberOfCameras },				
					{ "native_setup","(Ljava/lang/Object;IILjava/lang/String;)I",(void*)android_hardware_Camera_native_setup },       
					        camera = Camera::connect(cameraId, clientName, Camera::USE_CALLING_UID);   
								CameraBaseT::connect(cameraId, clientPackageName, clientUid);
								typedef CameraBase<TCam>         CameraBaseT;    
									const sp<ICameraService>& cs = getCameraService();
										binder = sm->getService(String16(kCameraServiceName));   
	
	4.3服务执行过程
		void CameraService::onFirstRef()
			int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,(const hw_module_t **)&rawModule);  "camera"
					module->methods->open
	
	
		linux@ubuntu:~/fspad-733-6.0/androidM/hardware/libhardware/modules/camera$ vi CameraHAL.cpp 
				camera_module_t HAL_MODULE_INFO_SYM __attribute__ ((visibility("default"))) = { 
					common : { 
						tag                : HARDWARE_MODULE_TAG,
						module_api_version : CAMERA_MODULE_API_VERSION_2_2,
						hal_api_version    : HARDWARE_HAL_API_VERSION,
						id                 : CAMERA_HARDWARE_MODULE_ID,
						name               : "Default Camera HAL",                                                                   
						author             : "The Android Open Source Project",
						methods            : &gCameraModuleMethods,
						dso                : NULL,
						reserved           : {0},
					},  
					get_number_of_cameras : get_number_of_cameras,
					get_camera_info       : get_camera_info,
					set_callbacks         : set_callbacks,
					get_vendor_tag_ops    : get_vendor_tag_ops,
					open_legacy           : NULL,
					set_torch_mode        : NULL,
					init                  : NULL,
					reserved              : {0},
				};

			static hw_module_methods_t gCameraModuleMethods = {
				open : open_dev                                                                                                  
					gCameraHAL.open(mod, name, dev);
					static CameraHAL gCameraHAL(2);
						mCameras[id]->open(mod, dev);
							mCameras = new Camera*[mNumberOfCameras];  


					int Camera::open(const hw_module_t *module, hw_device_t **device)
					{
						camera3_device_t mDevice;
					    memset(&mTemplates, 0, sizeof(mTemplates));
						mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0;
						mDevice.common.close  = close_device;
						mDevice.ops           = const_cast<camera3_device_ops_t*>(&sOps);
						mDevice.priv          = this;

						mDevice.common.module = const_cast<hw_module_t*>(module);
						*device = &mDevice.common;
						return 0;
					}

以上是关于Camera[1] 驱动V4L2分析的主要内容,如果未能解决你的问题,请参考以下文章

android摄像头(camera)之 v4l2的c测试代码

android camera V4L2 FIMC

android camera V4L2 FIMC

camera驱动

Camera | 5.Linux v4l2架构(基于rk3568)

1. 摄像头V4L2驱动框架分析