Skip to content

Commit 70433a1

Browse files
Junghak Sungmchehab
authored andcommitted
[media] media: videobuf2: Refactor vb2_fileio_data and vb2_thread
Replace v4l2-stuffs with common things in struct vb2_fileio_data and vb2_thread(). Signed-off-by: Junghak Sung <[email protected]> Signed-off-by: Geunyoung Kim <[email protected]> Acked-by: Seung-Woo Kim <[email protected]> Acked-by: Inki Dae <[email protected]> Acked-by: Hans Verkuil <[email protected]> Signed-off-by: Hans Verkuil <[email protected]> Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent dcbc216 commit 70433a1

File tree

1 file changed

+49
-55
lines changed

1 file changed

+49
-55
lines changed

drivers/media/v4l2-core/videobuf2-v4l2.c

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,10 @@ struct vb2_fileio_buf {
959959
* or write function.
960960
*/
961961
struct vb2_fileio_data {
962-
struct v4l2_requestbuffers req;
963-
struct v4l2_plane p;
964-
struct v4l2_buffer b;
962+
unsigned int count;
963+
unsigned int type;
964+
unsigned int memory;
965+
struct vb2_buffer *b;
965966
struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
966967
unsigned int cur_index;
967968
unsigned int initial_index;
@@ -1014,18 +1015,22 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)
10141015
if (fileio == NULL)
10151016
return -ENOMEM;
10161017

1018+
fileio->b = kzalloc(q->buf_struct_size, GFP_KERNEL);
1019+
if (fileio->b == NULL)
1020+
return -ENOMEM;
1021+
10171022
fileio->read_once = q->fileio_read_once;
10181023
fileio->write_immediately = q->fileio_write_immediately;
10191024

10201025
/*
10211026
* Request buffers and use MMAP type to force driver
10221027
* to allocate buffers by itself.
10231028
*/
1024-
fileio->req.count = count;
1025-
fileio->req.memory = VB2_MEMORY_MMAP;
1026-
fileio->req.type = q->type;
1029+
fileio->count = count;
1030+
fileio->memory = VB2_MEMORY_MMAP;
1031+
fileio->type = q->type;
10271032
q->fileio = fileio;
1028-
ret = vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
1033+
ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
10291034
if (ret)
10301035
goto err_kfree;
10311036

@@ -1054,24 +1059,17 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)
10541059
* Read mode requires pre queuing of all buffers.
10551060
*/
10561061
if (read) {
1057-
bool is_multiplanar = q->is_multiplanar;
1058-
10591062
/*
10601063
* Queue all buffers.
10611064
*/
10621065
for (i = 0; i < q->num_buffers; i++) {
1063-
struct v4l2_buffer *b = &fileio->b;
1066+
struct vb2_buffer *b = fileio->b;
10641067

1065-
memset(b, 0, sizeof(*b));
1068+
memset(b, 0, q->buf_struct_size);
10661069
b->type = q->type;
1067-
if (is_multiplanar) {
1068-
memset(&fileio->p, 0, sizeof(fileio->p));
1069-
b->m.planes = &fileio->p;
1070-
b->length = 1;
1071-
}
10721070
b->memory = q->memory;
10731071
b->index = i;
1074-
ret = vb2_internal_qbuf(q, b);
1072+
ret = vb2_core_qbuf(q, i, b);
10751073
if (ret)
10761074
goto err_reqbufs;
10771075
fileio->bufs[i].queued = 1;
@@ -1094,8 +1092,8 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)
10941092
return ret;
10951093

10961094
err_reqbufs:
1097-
fileio->req.count = 0;
1098-
vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
1095+
fileio->count = 0;
1096+
vb2_core_reqbufs(q, fileio->memory, &fileio->count);
10991097

11001098
err_kfree:
11011099
q->fileio = NULL;
@@ -1114,8 +1112,9 @@ static int __vb2_cleanup_fileio(struct vb2_queue *q)
11141112
if (fileio) {
11151113
vb2_core_streamoff(q, q->type);
11161114
q->fileio = NULL;
1117-
fileio->req.count = 0;
1118-
vb2_reqbufs(q, &fileio->req);
1115+
fileio->count = 0;
1116+
vb2_core_reqbufs(q, fileio->memory, &fileio->count);
1117+
kfree(fileio->b);
11191118
kfree(fileio);
11201119
dprintk(3, "file io emulator closed\n");
11211120
}
@@ -1168,24 +1167,21 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_
11681167
*/
11691168
index = fileio->cur_index;
11701169
if (index >= q->num_buffers) {
1170+
struct vb2_buffer *b = fileio->b;
1171+
11711172
/*
11721173
* Call vb2_dqbuf to get buffer back.
11731174
*/
1174-
memset(&fileio->b, 0, sizeof(fileio->b));
1175-
fileio->b.type = q->type;
1176-
fileio->b.memory = q->memory;
1177-
if (is_multiplanar) {
1178-
memset(&fileio->p, 0, sizeof(fileio->p));
1179-
fileio->b.m.planes = &fileio->p;
1180-
fileio->b.length = 1;
1181-
}
1182-
ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
1175+
memset(b, 0, q->buf_struct_size);
1176+
b->type = q->type;
1177+
b->memory = q->memory;
1178+
ret = vb2_core_dqbuf(q, b, nonblock);
11831179
dprintk(5, "vb2_dqbuf result: %d\n", ret);
11841180
if (ret)
11851181
return ret;
11861182
fileio->dq_count += 1;
11871183

1188-
fileio->cur_index = index = fileio->b.index;
1184+
fileio->cur_index = index = b->index;
11891185
buf = &fileio->bufs[index];
11901186

11911187
/*
@@ -1197,8 +1193,8 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_
11971193
: vb2_plane_size(q->bufs[index], 0);
11981194
/* Compensate for data_offset on read in the multiplanar case. */
11991195
if (is_multiplanar && read &&
1200-
fileio->b.m.planes[0].data_offset < buf->size) {
1201-
buf->pos = fileio->b.m.planes[0].data_offset;
1196+
b->planes[0].data_offset < buf->size) {
1197+
buf->pos = b->planes[0].data_offset;
12021198
buf->size -= buf->pos;
12031199
}
12041200
} else {
@@ -1237,6 +1233,8 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_
12371233
* Queue next buffer if required.
12381234
*/
12391235
if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
1236+
struct vb2_buffer *b = fileio->b;
1237+
12401238
/*
12411239
* Check if this is the last buffer to read.
12421240
*/
@@ -1248,20 +1246,15 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_
12481246
/*
12491247
* Call vb2_qbuf and give buffer to the driver.
12501248
*/
1251-
memset(&fileio->b, 0, sizeof(fileio->b));
1252-
fileio->b.type = q->type;
1253-
fileio->b.memory = q->memory;
1254-
fileio->b.index = index;
1255-
fileio->b.bytesused = buf->pos;
1256-
if (is_multiplanar) {
1257-
memset(&fileio->p, 0, sizeof(fileio->p));
1258-
fileio->p.bytesused = buf->pos;
1259-
fileio->b.m.planes = &fileio->p;
1260-
fileio->b.length = 1;
1261-
}
1249+
memset(b, 0, q->buf_struct_size);
1250+
b->type = q->type;
1251+
b->memory = q->memory;
1252+
b->index = index;
1253+
b->planes[0].bytesused = buf->pos;
1254+
12621255
if (copy_timestamp)
1263-
v4l2_get_timestamp(&fileio->b.timestamp);
1264-
ret = vb2_internal_qbuf(q, &fileio->b);
1256+
b->timestamp = ktime_get_ns();
1257+
ret = vb2_core_qbuf(q, index, b);
12651258
dprintk(5, "vb2_dbuf result: %d\n", ret);
12661259
if (ret)
12671260
return ret;
@@ -1338,36 +1331,37 @@ static int vb2_thread(void *data)
13381331

13391332
for (;;) {
13401333
struct vb2_buffer *vb;
1334+
struct vb2_buffer *b = fileio->b;
13411335

13421336
/*
13431337
* Call vb2_dqbuf to get buffer back.
13441338
*/
1345-
memset(&fileio->b, 0, sizeof(fileio->b));
1346-
fileio->b.type = q->type;
1347-
fileio->b.memory = q->memory;
1339+
memset(b, 0, q->buf_struct_size);
1340+
b->type = q->type;
1341+
b->memory = q->memory;
13481342
if (prequeue) {
1349-
fileio->b.index = index++;
1343+
b->index = index++;
13501344
prequeue--;
13511345
} else {
13521346
call_void_qop(q, wait_finish, q);
13531347
if (!threadio->stop)
1354-
ret = vb2_internal_dqbuf(q, &fileio->b, 0);
1348+
ret = vb2_core_dqbuf(q, b, 0);
13551349
call_void_qop(q, wait_prepare, q);
13561350
dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
13571351
}
13581352
if (ret || threadio->stop)
13591353
break;
13601354
try_to_freeze();
13611355

1362-
vb = q->bufs[fileio->b.index];
1363-
if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR))
1356+
vb = q->bufs[b->index];
1357+
if (b->state == VB2_BUF_STATE_DONE)
13641358
if (threadio->fnc(vb, threadio->priv))
13651359
break;
13661360
call_void_qop(q, wait_finish, q);
13671361
if (copy_timestamp)
1368-
v4l2_get_timestamp(&fileio->b.timestamp);
1362+
b->timestamp = ktime_get_ns();
13691363
if (!threadio->stop)
1370-
ret = vb2_internal_qbuf(q, &fileio->b);
1364+
ret = vb2_core_qbuf(q, b->index, b);
13711365
call_void_qop(q, wait_prepare, q);
13721366
if (ret || threadio->stop)
13731367
break;

0 commit comments

Comments
 (0)