3131
3232#include <rtthread.h>
3333#include <rthw.h>
34+ #include "drv_hardlock.h"
3435
3536#if defined(RT_USING_POSIX_DEVIO )
36- #include <dfs_posix.h>
37- #include <poll.h>
38- #include <termios.h>
37+ #include <dfs_posix.h>
38+ #include <poll.h>
39+ #include <termios.h>
3940#endif
4041
41- #define AI2D_LOG_ENABLE
42- struct ai_2d_dev_handle {
43- rt_wqueue_t * wait ;
42+ #define AI2D_CMD_LOCK 0
43+ #define AI2D_CMD_TRYLOCK 1
44+ #define AI2D_CMD_UNLOCK 2
45+ static hardlock_type g_ai2d_lock = HARDLOCK_MAX ;
46+
47+ struct ai_2d_dev_handle
48+ {
49+ rt_wqueue_t * wait ;
50+ rt_bool_t is_lock ;
4451};
4552
4653
47- #ifdef AI2D_LOG_ENABLE
4854#define ai_2d_log (s ...) rt_kprintf(s)
49- #else
50- static int ai_2d_log (const char * fmt , ...)
51- {
52- return 0 ;
53- }
54- #endif
5555
56- #define ai_2d_info (s ...) do { \
57- ai_2d_log("<ai_2d> "); \
58- ai_2d_log(s); \
59- ai_2d_log("\r\n"); \
56+ #define ai_2d_info (s ...) do { \
57+ ai_2d_log("<ai_2d> "); \
58+ ai_2d_log(s); \
59+ ai_2d_log("\r\n"); \
6060} while (0)
6161
6262#define ai_2d_err (s ...) do { \
63- ai_2d_log("<err>[%s:%d] ", __func__, __LINE__); \
64- ai_2d_log(s); \
65- ai_2d_log("\r\n"); \
63+ ai_2d_log("<err>[%s:%d] ", __func__, __LINE__); \
64+ ai_2d_log(s); \
65+ ai_2d_log("\r\n"); \
6666} while (0)
6767
6868static struct rt_device g_ai_2d_device = {0 };
@@ -74,14 +74,16 @@ static int ai_2d_device_open(struct dfs_file *file)
7474 struct ai_2d_dev_handle * handle ;
7575 rt_device_t device ;
7676
77- handle = rt_malloc (sizeof (struct ai_2d_dev_handle ));
78- if (handle == RT_NULL ) {
77+ handle = rt_malloc (sizeof (struct ai_2d_dev_handle ));
78+ if (handle == RT_NULL )
79+ {
7980 ai_2d_err ("malloc failed\n" );
8081 return -1 ;
8182 }
8283 device = (rt_device_t )file -> vnode -> data ;
8384 handle -> wait = & device -> wait_queue ;
84- file -> data = (void * )handle ;
85+ handle -> is_lock = RT_FALSE ;
86+ file -> data = (void * )handle ;
8587 return RT_EOK ;
8688}
8789
@@ -90,9 +92,14 @@ static int ai_2d_device_close(struct dfs_file *file)
9092 struct ai_2d_dev_handle * handle ;
9193
9294 handle = (struct ai_2d_dev_handle * )file -> data ;
93- if (handle == RT_NULL ) {
95+ if (handle == RT_NULL )
96+ {
9497 ai_2d_err ("try to close a invalid handle" );
95- return - RT_EINVAL ;
98+ return - RT_EINVAL ;
99+ }
100+ if (handle -> is_lock )
101+ {
102+ kd_hardlock_unlock (g_ai2d_lock );
96103 }
97104 rt_free (handle );
98105 file -> data = RT_NULL ;
@@ -101,15 +108,55 @@ static int ai_2d_device_close(struct dfs_file *file)
101108
102109static int ai_2d_device_ioctl (struct dfs_file * file , int cmd , void * args )
103110{
104- return 0 ;
111+ struct ai_2d_dev_handle * handle ;
112+ int ret = -1 ;
113+
114+ handle = (struct ai_2d_dev_handle * )file -> data ;
115+ if (g_ai2d_lock == HARDLOCK_MAX )
116+ return ret ;
117+
118+ if (cmd == AI2D_CMD_LOCK )
119+ {
120+ if (handle -> is_lock == RT_TRUE )
121+ {
122+ return 0 ;
123+ }
124+ while (kd_hardlock_lock (g_ai2d_lock ));
125+ handle -> is_lock = RT_TRUE ;
126+ ret = 0 ;
127+ }
128+ else if (cmd == AI2D_CMD_UNLOCK )
129+ {
130+ if (handle -> is_lock == RT_FALSE )
131+ {
132+ return 0 ;
133+ }
134+ kd_hardlock_unlock (g_ai2d_lock );
135+ handle -> is_lock = RT_FALSE ;
136+ ret = 0 ;
137+ }
138+ else if (cmd == AI2D_CMD_TRYLOCK )
139+ {
140+ if (handle -> is_lock == RT_TRUE )
141+ {
142+ return 0 ;
143+ }
144+ if (!kd_hardlock_lock (g_ai2d_lock ))
145+ {
146+ handle -> is_lock = RT_TRUE ;
147+ ret = 0 ;
148+ }
149+ }
150+ return ret ;
105151}
106152
107153int ai_2d_device_poll (struct dfs_file * file , struct rt_pollreq * req )
108154{
109155 struct ai_2d_dev_handle * handle ;
110156 unsigned int flags ;
111- handle = (struct ai_2d_dev_handle * )file -> data ;
112- if (!handle ) {
157+ handle = (struct ai_2d_dev_handle * )file -> data ;
158+ if (!handle )
159+ {
113160 ai_2d_err ("ai_2d_dev_handle NULL!" );
114161 return - EINVAL ;
115162 }
@@ -118,7 +165,8 @@ int ai_2d_device_poll(struct dfs_file *file, struct rt_pollreq *req)
118165 return POLLIN ;
119166}
120167
121- static const struct dfs_file_ops ai_2d_input_fops = {
168+ static const struct dfs_file_ops ai_2d_input_fops =
169+ {
122170 .open = ai_2d_device_open ,
123171 .close = ai_2d_device_close ,
124172 .ioctl = ai_2d_device_ioctl ,
@@ -130,14 +178,15 @@ static void irq_callback(int irq, void *data)
130178{
131179 rt_wqueue_t * wait = (rt_wqueue_t * )data ;
132180 volatile rt_uint32_t * write_addr = (rt_uint32_t * )((char * )gnne_base_addr + 0xca0 );
133- if (gnne_base_addr == RT_NULL ) {
181+ if (gnne_base_addr == RT_NULL )
182+ {
134183 ai_2d_err ("ai2d interrupts while the hardware is not yet initialized\n" );
135184 }
136185 write_addr [0 ] = 1 ;
137186 write_addr [1 ] = 0 ;
138187 write_addr [2 ] = 0 ;
139188 write_addr [3 ] = 0 ;
140- rt_wqueue_wakeup (wait , (void * )POLLIN );
189+ rt_wqueue_wakeup (wait , (void * )POLLIN );
141190 rt_event_send (& g_ai_2d_event , 0x1 );
142191}
143192
@@ -147,13 +196,15 @@ int ai_2d_device_init(void)
147196 rt_device_t ai_2d_device = & g_ai_2d_device ;
148197
149198 ret = rt_event_init (& g_ai_2d_event , "ai_2d_event" , RT_IPC_FLAG_PRIO );
150- if (ret ) {
199+ if (ret )
200+ {
151201 ai_2d_err ("event init failed\n" );
152202 return - ENOMEM ;
153203 }
154204
155205 ret = rt_device_register (ai_2d_device , "ai_2d_device" , RT_DEVICE_FLAG_RDWR );
156- if (ret ) {
206+ if (ret )
207+ {
157208 ai_2d_err ("ai_2d_device register fail\n" );
158209 return ret ;
159210 }
@@ -164,9 +215,20 @@ int ai_2d_device_init(void)
164215
165216 ai_2d_device -> fops = & ai_2d_input_fops ;
166217
218+ if (kd_request_lock (HARDLOCK_AI2D ))
219+ {
220+ ai_2d_err ("fail to request hardlock-%d\n" , HARDLOCK_AI2D );
221+ }
222+ else
223+ {
224+ g_ai2d_lock = HARDLOCK_AI2D ;
225+ }
226+
167227#ifndef RT_FASTBOOT
168- if (!ret )
228+ if (!ret )
229+ {
169230 ai_2d_info ("%s OK\n" , __func__ );
231+ }
170232#endif
171233 return ret ;
172- }
234+ }
0 commit comments