1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ///////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2010 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ///////////////////////////////////////////////////////////////////////////////
94
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file mdrvPVR_IframeLUT.c
98 /// @brief PVR IframeLUT API Interface
99 /// @author MStar Semiconductor,Inc.
100 /// @attention
101 ///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
104 //-------------------------------------------------------------------------------------------------
105 // Include Files
106 //-------------------------------------------------------------------------------------------------
107 #ifdef MSOS_TYPE_LINUX_KERNEL
108 #include <linux/string.h>
109 #include <linux/errno.h>
110 #else
111 #include <stdio.h>
112 #include <string.h>
113 #endif
114
115 #include "MsTypes.h"
116 #include "drvPVR_IframeLUT_v2.h"
117 #include "drvPVR_IframeLUT_private.h"
118 #include "utopia.h"
119 #include "utopia_dapi.h"
120 #include "MsOS.h"
121
122 #define PVR_IFRAMELUT_DBGERR(_f,_a...) {if(_iframelutdbglevel >= E_PVR_IFRAMELUT_DBG_ERR) {printf(_f, ##_a);}}
123 #define PVR_IFRAMELUT_DBGINFO(_f,_a...) {if(_iframelutdbglevel >= E_PVR_IFRAMELUT_DBG_INFO){printf(_f, ##_a);}}
124
125 static EN_PVR_IFRAMELUT_DBGLEVEL _iframelutdbglevel = E_PVR_IFRAMELUT_DBG_INFO;
126
127 // This function will be called by Utopia2.0 framework
PVR_IFRAMELUTRegisterToUtopia(FUtopiaOpen ModuleType)128 void PVR_IFRAMELUTRegisterToUtopia(FUtopiaOpen ModuleType)
129 {
130 // UtopiaModuleCreate and UtopiaResourceCreate would deal with share memory.
131 //
132 // Using mstar driver semutex in linux kernel to allocate and map share memory.
133 // When create share memory, semutex allocate pages and add them to a linked-list in its fd's private data,
134 // and then, mmap is called to use vm_insert_page to map these pages to static _pu8ShareMem.
135 //
136 // All utopia drivers' resources are on the same memory space _pu8ShareMem.
137 // static _ShmHdr records information about _pu8ShareMem usage (current client number, current offset ...)
138 //
139 // client id != module, every resource has a client id.
140 //
141 // MsOS_SHM_Context[] is a table in _ShmHdr, when a resource is created (by using shm_malloc() -> MsOS_SHM_GetId(MSOS_SHM_CREATE)),
142 // client number++, and a new entry is inserted into the MsOS_SHM_Context[].
143 //
144 // The created resource addr returned by MsOS_SHM_GetId(MSOS_SHM_CREATE) is _pu8ShareMem + u32Offset.
145 // The created resource id returned by MsOS_SHM_GetId(MSOS_SHM_CREATE) is the table id (client id) of MsOS_SHM_Context[]
146 // These 2 information would be stored in shm_info_table.
147 //
148 // When we want to get resource addr (pointer), use id to look-up shm_info_table,
149 // just like shmid2va does.
150 //
151 PVR_IFRAMELUT_DBGINFO("[%s][%d] start\n", __FUNCTION__, __LINE__);
152 // 1. create module and register it.
153 void *pUtopiaModule = NULL;
154 // malloc UTOPIA_MODULE,
155 // and shm_malloc UTOPIA_MODULE_SHM and module private
156 //
157 // It seems that module private is unused.
158 //
159 // There could be lots of resource pools belong to a specific module, these resource pools consist of a linked-list.
160 // UTOPIA_MODULE_SHM records the head of this linked-list.
161 //
162 // After UtopiaModuleCreate called, there is no resource pool in the module,
163 // resource pool will be created when UtopiaResourceRegister called.
164 UtopiaModuleCreate(MODULE_PVR_IFRAMELUT, 0, &pUtopiaModule);
165 // psUtopiaPrivate is a linked-list stores all modules,
166 // add the module we created to the head of this linked-list.
167 UtopiaModuleRegister(pUtopiaModule);
168
169 // 2. register functions to the created module, after registered here,
170 // the UtopiaOpen/UtopiaIoctl/UtopiaClose function calls will be re-directed to these registered functions.
171 UtopiaModuleSetupFunctionPtr(pUtopiaModule, (FUtopiaOpen)PVR_IFRAMELUTOpen, (FUtopiaClose)PVR_IFRAMELUTClose, (FUtopiaIOctl)PVR_IFRAMELUTIoctl);
172
173 // 3. create resource and register it to a specific resource pool.
174 void* psResource = NULL;
175 // This function directly return SUCCESS
176 UtopiaModuleAddResourceStart(pUtopiaModule, E_PVR_IFRAMELUT_RESOURCE_POOL_0);
177 MS_U32 u32ResSize = 0;
178 _MDrv_PVR_IframeLUT_GetCap(PVR_IFRAMELUT_CAP_RESOURCE_SIZE, (void*)(&u32ResSize));
179 // Create a resource called "pvr_iframelut".
180 // shm_malloc UTOPIA_RESOURCE and resource private.
181 // resource private is where we store static PVR_IFRAMELUT_RESOURCE_PRIVATE
182 UtopiaResourceCreate("pvr_iframelut", u32ResSize, &psResource);
183 // Register "pvr_iframelut" resource to a resource pool called "E_PVR_IFRAMELUT_RESOURCE_POOL_0".
184 // Apparently, there is no resource pool called "E_PVR_IFRAMELUT_RESOURCE_POOL_0",
185 // so this resource pool will be created first.
186 //
187 // There could be lots of resource belong to a specific resource pool, resources are a linked-list in a resource pool.
188 //
189 // If cannot find "pvr_iframelut" resource in resource pool "E_PVR_IFRAMELUT_RESOURCE_POOL_0",
190 // add "pvr_iframelut" resource to the head of this resource pool.
191 //
192 // We can create more resources and add it to the same resource pool.
193 UtopiaResourceRegister(pUtopiaModule, psResource, E_PVR_IFRAMELUT_RESOURCE_POOL_0);
194
195 // Create semaphore for a specific resource pool,
196 // the value of this semaphore would be initialized to # of resources in this resource pool.
197 UtopiaModuleAddResourceEnd(pUtopiaModule, E_PVR_IFRAMELUT_RESOURCE_POOL_0);
198 PVR_IFRAMELUT_DBGINFO("[%s][%d] end\n", __FUNCTION__, __LINE__);
199 }
200
201 // u32ModuleVersion and pAttribute are for polymorphism.
202 // Using u32ModuleVersion or pAttribute to let function pointers point to different functions.
203 // It would result in same symbol, different version, different behavior.
PVR_IFRAMELUTOpen(void ** ppInstance,MS_U32 u32ModuleVersion,void * pAttribute)204 MS_U32 PVR_IFRAMELUTOpen(void** ppInstance, MS_U32 u32ModuleVersion, void* pAttribute)
205 {
206 PVR_IFRAMELUT_DBGINFO("[%s][%d] start\n", __FUNCTION__, __LINE__);
207 if(ppInstance == 0)
208 {
209 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTOpen] ppInstance is NULL!\n");
210 return UTOPIA_STATUS_PARAMETER_ERROR;
211 }
212
213 PVR_IFRAMELUT_INSTANT_PRIVATE *pInstancePri = NULL;
214
215 // malloc UTOPIA_INSTANCE and instance private.
216 // instance pricate is where stores all function pointers.
217 UtopiaInstanceCreate(sizeof(PVR_IFRAMELUT_INSTANT_PRIVATE), ppInstance);
218
219 // There is no where to register instance.
220 // We store instance in a static variable by ourselves.
221
222 // When UtopiaOpen is called, module would be linked to instance,
223 // (store module in instance->psModule)
224 // so we can get module from instance later.
225
226 // setup functions in instance private, assign functions to function ptrs in instance private
227 UtopiaInstanceGetPrivate(*ppInstance, (void**)&pInstancePri);
228 pInstancePri->fpPVRIframeLUTInit = (IOCTL_PVR_IframeLUT_Init)_MDrv_PVR_IframeLUT_Init;
229 pInstancePri->fpPVRIframeLUTOpen = (IOCTL_PVR_IframeLUT_Open)_MDrv_PVR_IframeLUT_Open;
230 pInstancePri->fpPVRIframeLUTSetVdecType = (IOCTL_PVR_IframeLUT_SetVdecType)_MDrv_PVR_IframeLUT_SetVdecType;
231 pInstancePri->fpPVRIframeLUTGetWritePtr = (IOCTL_PVR_IframeLUT_GetWritePtr)_MDrv_PVR_IframeLUT_GetWritePtr;
232 pInstancePri->fpPVRIframeLUTClose = (IOCTL_PVR_IframeLUT_Close)_MDrv_PVR_IframeLUT_Close;
233 pInstancePri->fpPVRIframeLUTExit = (IOCTL_PVR_IframeLUT_Exit)_MDrv_PVR_IframeLUT_Exit;
234
235 PVR_IFRAMELUT_DBGINFO("[%s][%d] end\n", __FUNCTION__, __LINE__);
236 return UTOPIA_STATUS_SUCCESS;
237 }
238
PVR_IFRAMELUTIoctl(void * pInstance,MS_U32 u32Cmd,void * pArgs)239 MS_U32 PVR_IFRAMELUTIoctl(void* pInstance, MS_U32 u32Cmd, void* pArgs)
240 {
241 PVR_IFRAMELUT_DBGINFO("[%s][%d]\n", __FUNCTION__, __LINE__);
242 if(pInstance == NULL)
243 {
244 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] pInstance is NULL!\n");
245 return UTOPIA_STATUS_FAIL;
246 }
247
248 MS_U32 u32Res = UTOPIA_STATUS_SUCCESS;
249 PVR_IFRAMELUT_INSTANT_PRIVATE *pInstancePrivate = NULL;
250 void *pModule = NULL;
251 void *pResource = NULL;
252 void *pResourcePrivate = NULL;
253
254 PVR_IFRAMELUT_DBGINFO("[%s][%d] UtopiaInstanceGetPrivate\n", __FUNCTION__, __LINE__);
255 // Get instance private from instance
256 if(UtopiaInstanceGetPrivate(pInstance, (void*)&pInstancePrivate) != 0)
257 {
258 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] UtopiaInstanceGetPrivate fail\n");
259 return UTOPIA_STATUS_FAIL;
260 }
261
262 PVR_IFRAMELUT_DBGINFO("[%s][%d] UtopiaInstanceGetModule\n", __FUNCTION__, __LINE__);
263 // Get module from instance
264 if(UtopiaInstanceGetModule(pInstance, &pModule) != 0)
265 {
266 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] UtopiaInstanceGetModule fail\n");
267 return UTOPIA_STATUS_FAIL;
268 }
269
270 PVR_IFRAMELUT_DBGINFO("[%s][%d] UtopiaResourceObtain\n", __FUNCTION__, __LINE__);
271 // Get resource from module, semaphore--
272 if(UtopiaResourceObtain(pModule, E_PVR_IFRAMELUT_RESOURCE_POOL_0, &pResource) != 0)
273 {
274 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] UtopiaResourceObtain fail\n");
275 return UTOPIA_STATUS_FAIL;
276 }
277
278 PVR_IFRAMELUT_DBGINFO("[%s][%d] UtopiaResourceGetPrivate\n", __FUNCTION__, __LINE__);
279 // Get resource private from resource
280 if(UtopiaResourceGetPrivate(pResource, &pResourcePrivate) != 0)
281 {
282 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] UtopiaResourceGetPrivatefail\n");
283 UtopiaResourceRelease(pResource);
284 return UTOPIA_STATUS_FAIL;
285 }
286
287 // Initialize resource private, and assign resource private pointer to static variable _lut_res
288 if(_MDrv_PVR_IframeLUT_InitResource(pResourcePrivate) != E_PVR_IFRAMELUT_RESULT_OK)
289 {
290 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Init share memory failed \n");
291 UtopiaResourceRelease(pResource);
292 return UTOPIA_STATUS_FAIL;
293 }
294
295 switch(u32Cmd)
296 {
297 // General API
298 case E_MDrv_CMD_PVR_IframeLUT_Init:
299 PVR_IFRAMELUT_DBGINFO("[%s][%d] CMD Init\n", __FUNCTION__, __LINE__);
300 if(pInstancePrivate->fpPVRIframeLUTInit() != E_PVR_IFRAMELUT_RESULT_OK)
301 {
302 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Init fail\n");
303 u32Res = UTOPIA_STATUS_FAIL;
304 }
305 break;
306 case E_MDrv_CMD_PVR_IframeLUT_Open:
307 PVR_IFRAMELUT_DBGINFO("[%s][%d] CMD Open\n", __FUNCTION__, __LINE__);
308 PVR_IFRAMELUT_OPEN_PARAM *pOpenParam = (PVR_IFRAMELUT_OPEN_PARAM *)pArgs;
309 if(pInstancePrivate->fpPVRIframeLUTOpen(pOpenParam) != E_PVR_IFRAMELUT_RESULT_OK)
310 {
311 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Open fail\n");
312 u32Res = UTOPIA_STATUS_FAIL;
313 }
314 break;
315 case E_MDrv_CMD_PVR_IframeLUT_SetVdecType:
316 PVR_IFRAMELUT_DBGINFO("[%s][%d] CMD Set Vdec Type\n", __FUNCTION__, __LINE__);
317 PVR_IFRAMELUT_SETVDECTYPE_PARAM *pSetVdecTypeParam = (PVR_IFRAMELUT_SETVDECTYPE_PARAM *)pArgs;
318 if(pInstancePrivate->fpPVRIframeLUTSetVdecType(pSetVdecTypeParam->u32PvrEng, pSetVdecTypeParam->eVdecType) != E_PVR_IFRAMELUT_RESULT_OK)
319 {
320 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Set Vdec Type fail\n");
321 u32Res = UTOPIA_STATUS_FAIL;
322 }
323 break;
324 case E_MDrv_CMD_PVR_IframeLUT_GetWritePtr:
325 PVR_IFRAMELUT_DBGINFO("[%s][%d] CMD Get Write Ptr\n", __FUNCTION__, __LINE__);
326 PVR_IFRAMELUT_GETWRITEPTR_PARAM *pGetWritePtrParam = (PVR_IFRAMELUT_GETWRITEPTR_PARAM *)pArgs;
327 if(pInstancePrivate->fpPVRIframeLUTGetWritePtr(pGetWritePtrParam->u32PvrEng, pGetWritePtrParam->pu32WritePtr) != E_PVR_IFRAMELUT_RESULT_OK)
328 {
329 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Get Write Ptr fail\n");
330 u32Res = UTOPIA_STATUS_FAIL;
331 }
332 break;
333 case E_MDrv_CMD_PVR_IframeLUT_Close:
334 PVR_IFRAMELUT_DBGINFO("[%s][%d] CMD Close\n", __FUNCTION__, __LINE__);
335 PVR_IFRAMELUT_CLOSE_PARAM *pCloseParam = (PVR_IFRAMELUT_CLOSE_PARAM *)pArgs;
336 if(pInstancePrivate->fpPVRIframeLUTClose(*pCloseParam) != E_PVR_IFRAMELUT_RESULT_OK)
337 {
338 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Close fail\n");
339 u32Res = UTOPIA_STATUS_FAIL;
340 }
341 break;
342 case E_MDrv_CMD_PVR_IframeLUT_Exit:
343 PVR_IFRAMELUT_DBGINFO("[%s][%d] CMD Exit\n", __FUNCTION__, __LINE__);
344 if(pInstancePrivate->fpPVRIframeLUTExit() != E_PVR_IFRAMELUT_RESULT_OK)
345 {
346 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Exit fail\n");
347 u32Res = UTOPIA_STATUS_FAIL;
348 }
349 break;
350 default:
351 PVR_IFRAMELUT_DBGERR("[PVR_IframeLUTIoctl] Unknown command!\n");
352 u32Res = UTOPIA_STATUS_NOT_SUPPORTED;
353 break;
354 }
355 // semaphore++
356 UtopiaResourceRelease(pResource);
357 return u32Res;
358 }
359
PVR_IFRAMELUTClose(void * pInstance)360 MS_U32 PVR_IFRAMELUTClose(void* pInstance)
361 {
362 PVR_IFRAMELUT_DBGINFO("[%s][%d]\n", __FUNCTION__, __LINE__);
363 UtopiaInstanceDelete(pInstance);
364 return UTOPIA_STATUS_SUCCESS;
365 }
366