@@ -1387,11 +1387,13 @@ static int __prepare_dmabuf(struct vb2_buffer *vb)
1387
1387
for (plane = 0 ; plane < vb -> num_planes ; ++ plane ) {
1388
1388
struct dma_buf * dbuf = dma_buf_get (planes [plane ].m .fd );
1389
1389
1390
+ planes [plane ].dbuf = dbuf ;
1391
+
1390
1392
if (IS_ERR_OR_NULL (dbuf )) {
1391
1393
dprintk (q , 1 , "invalid dmabuf fd for plane %d\n" ,
1392
1394
plane );
1393
1395
ret = - EINVAL ;
1394
- goto err ;
1396
+ goto err_put_planes ;
1395
1397
}
1396
1398
1397
1399
/* use DMABUF size if length is not provided */
@@ -1402,96 +1404,97 @@ static int __prepare_dmabuf(struct vb2_buffer *vb)
1402
1404
dprintk (q , 1 , "invalid dmabuf length %u for plane %d, minimum length %u\n" ,
1403
1405
planes [plane ].length , plane ,
1404
1406
vb -> planes [plane ].min_length );
1405
- dma_buf_put (dbuf );
1406
1407
ret = - EINVAL ;
1407
- goto err ;
1408
+ goto err_put_planes ;
1408
1409
}
1409
1410
1410
1411
/* Skip the plane if already verified */
1411
1412
if (dbuf == vb -> planes [plane ].dbuf &&
1412
- vb -> planes [plane ].length == planes [plane ].length ) {
1413
- dma_buf_put (dbuf );
1413
+ vb -> planes [plane ].length == planes [plane ].length )
1414
1414
continue ;
1415
- }
1416
1415
1417
1416
dprintk (q , 3 , "buffer for plane %d changed\n" , plane );
1418
1417
1419
- if (!reacquired ) {
1420
- reacquired = true;
1418
+ reacquired = true;
1419
+ }
1420
+
1421
+ if (reacquired ) {
1422
+ if (vb -> planes [0 ].mem_priv ) {
1421
1423
vb -> copied_timestamp = 0 ;
1422
1424
call_void_vb_qop (vb , buf_cleanup , vb );
1425
+ __vb2_buf_dmabuf_put (vb );
1423
1426
}
1424
1427
1425
- /* Release previously acquired memory if present */
1426
- __vb2_plane_dmabuf_put (vb , & vb -> planes [plane ]);
1427
-
1428
- /* Acquire each plane's memory */
1429
- mem_priv = call_ptr_memop (attach_dmabuf ,
1430
- vb ,
1431
- q -> alloc_devs [plane ] ? : q -> dev ,
1432
- dbuf ,
1433
- planes [plane ].length );
1434
- if (IS_ERR (mem_priv )) {
1435
- dprintk (q , 1 , "failed to attach dmabuf\n" );
1436
- ret = PTR_ERR (mem_priv );
1437
- dma_buf_put (dbuf );
1438
- goto err ;
1439
- }
1440
-
1441
- vb -> planes [plane ].dbuf = dbuf ;
1442
- vb -> planes [plane ].mem_priv = mem_priv ;
1443
- }
1428
+ for (plane = 0 ; plane < vb -> num_planes ; ++ plane ) {
1429
+ /* Acquire each plane's memory */
1430
+ mem_priv = call_ptr_memop (attach_dmabuf ,
1431
+ vb ,
1432
+ q -> alloc_devs [plane ] ? : q -> dev ,
1433
+ planes [plane ].dbuf ,
1434
+ planes [plane ].length );
1435
+ if (IS_ERR (mem_priv )) {
1436
+ dprintk (q , 1 , "failed to attach dmabuf\n" );
1437
+ ret = PTR_ERR (mem_priv );
1438
+ goto err_put_vb2_buf ;
1439
+ }
1444
1440
1445
- /*
1446
- * This pins the buffer(s) with dma_buf_map_attachment()). It's done
1447
- * here instead just before the DMA, while queueing the buffer(s) so
1448
- * userspace knows sooner rather than later if the dma-buf map fails.
1449
- */
1450
- for (plane = 0 ; plane < vb -> num_planes ; ++ plane ) {
1451
- if (vb -> planes [plane ].dbuf_mapped )
1452
- continue ;
1441
+ vb -> planes [plane ].dbuf = planes [plane ].dbuf ;
1442
+ vb -> planes [plane ].mem_priv = mem_priv ;
1453
1443
1454
- ret = call_memop (vb , map_dmabuf , vb -> planes [plane ].mem_priv );
1455
- if (ret ) {
1456
- dprintk (q , 1 , "failed to map dmabuf for plane %d\n" ,
1457
- plane );
1458
- goto err ;
1444
+ /*
1445
+ * This pins the buffer(s) with dma_buf_map_attachment()). It's done
1446
+ * here instead just before the DMA, while queueing the buffer(s) so
1447
+ * userspace knows sooner rather than later if the dma-buf map fails.
1448
+ */
1449
+ ret = call_memop (vb , map_dmabuf , vb -> planes [plane ].mem_priv );
1450
+ if (ret ) {
1451
+ dprintk (q , 1 , "failed to map dmabuf for plane %d\n" ,
1452
+ plane );
1453
+ goto err_put_vb2_buf ;
1454
+ }
1455
+ vb -> planes [plane ].dbuf_mapped = 1 ;
1459
1456
}
1460
- vb -> planes [plane ].dbuf_mapped = 1 ;
1461
- }
1462
1457
1463
- /*
1464
- * Now that everything is in order, copy relevant information
1465
- * provided by userspace.
1466
- */
1467
- for (plane = 0 ; plane < vb -> num_planes ; ++ plane ) {
1468
- vb -> planes [plane ].bytesused = planes [plane ].bytesused ;
1469
- vb -> planes [plane ].length = planes [plane ].length ;
1470
- vb -> planes [plane ].m .fd = planes [plane ].m .fd ;
1471
- vb -> planes [plane ].data_offset = planes [plane ].data_offset ;
1472
- }
1458
+ /*
1459
+ * Now that everything is in order, copy relevant information
1460
+ * provided by userspace.
1461
+ */
1462
+ for (plane = 0 ; plane < vb -> num_planes ; ++ plane ) {
1463
+ vb -> planes [plane ].bytesused = planes [plane ].bytesused ;
1464
+ vb -> planes [plane ].length = planes [plane ].length ;
1465
+ vb -> planes [plane ].m .fd = planes [plane ].m .fd ;
1466
+ vb -> planes [plane ].data_offset = planes [plane ].data_offset ;
1467
+ }
1473
1468
1474
- if (reacquired ) {
1475
1469
/*
1476
1470
* Call driver-specific initialization on the newly acquired buffer,
1477
1471
* if provided.
1478
1472
*/
1479
1473
ret = call_vb_qop (vb , buf_init , vb );
1480
1474
if (ret ) {
1481
1475
dprintk (q , 1 , "buffer initialization failed\n" );
1482
- goto err ;
1476
+ goto err_put_vb2_buf ;
1483
1477
}
1478
+ } else {
1479
+ for (plane = 0 ; plane < vb -> num_planes ; ++ plane )
1480
+ dma_buf_put (planes [plane ].dbuf );
1484
1481
}
1485
1482
1486
1483
ret = call_vb_qop (vb , buf_prepare , vb );
1487
1484
if (ret ) {
1488
1485
dprintk (q , 1 , "buffer preparation failed\n" );
1489
1486
call_void_vb_qop (vb , buf_cleanup , vb );
1490
- goto err ;
1487
+ goto err_put_vb2_buf ;
1491
1488
}
1492
1489
1493
1490
return 0 ;
1494
- err :
1491
+
1492
+ err_put_planes :
1493
+ for (plane = 0 ; plane < vb -> num_planes ; ++ plane ) {
1494
+ if (!IS_ERR_OR_NULL (planes [plane ].dbuf ))
1495
+ dma_buf_put (planes [plane ].dbuf );
1496
+ }
1497
+ err_put_vb2_buf :
1495
1498
/* In case of errors, release planes that were already acquired */
1496
1499
__vb2_buf_dmabuf_put (vb );
1497
1500
0 commit comments