summaryrefslogtreecommitdiff
path: root/drivers/media/v4l2-core/v4l2-subdev.c
blob: 4c6198c48dd612241841a41d26b4de8fced6ea3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
// SPDX-License-Identifier: GPL-2.0-only
/*
 * V4L2 sub-device
 *
 * Copyright (C) 2010 Nokia Corporation
 *
 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 *	    Sakari Ailus <sakari.ailus@iki.fi>
 */

#include <linux/export.h>
#include <linux/ioctl.h>
#include <linux/leds.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/overflow.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/version.h>
#include <linux/videodev2.h>

#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-ioctl.h>

#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
/*
 * The Streams API is an experimental feature. To use the Streams API, set
 * 'v4l2_subdev_enable_streams_api' to 1 below.
 */

static bool v4l2_subdev_enable_streams_api;
#endif

/*
 * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
 * of streams.
 *
 * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
 * restricts the total number of streams in a pad, although the stream ID is
 * not restricted.
 */
#define V4L2_SUBDEV_MAX_STREAM_ID 63

#include "v4l2-subdev-priv.h"

#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
{
	struct v4l2_subdev_state *state;
	static struct lock_class_key key;

	state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
	if (IS_ERR(state))
		return PTR_ERR(state);

	fh->state = state;

	return 0;
}

static void subdev_fh_free(struct v4l2_subdev_fh *fh)
{
	__v4l2_subdev_state_free(fh->state);
	fh->state = NULL;
}

static int subdev_open(struct file *file)
{
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
	struct v4l2_subdev_fh *subdev_fh;
	int ret;

	subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
	if (subdev_fh == NULL)
		return -ENOMEM;

	ret = subdev_fh_init(subdev_fh, sd);
	if (ret) {
		kfree(subdev_fh);
		return ret;
	}

	v4l2_fh_init(&subdev_fh->vfh, vdev);
	v4l2_fh_add(&subdev_fh->vfh);
	file->private_data = &subdev_fh->vfh;

	if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
		struct module *owner;

		owner = sd->entity.graph_obj.mdev->dev->driver->owner;
		if (!try_module_get(owner)) {
			ret = -EBUSY;
			goto err;
		}
		subdev_fh->owner = owner;
	}

	if (sd->internal_ops && sd->internal_ops->open) {
		ret = sd->internal_ops->open(sd, subdev_fh);
		if (ret < 0)
			goto err;
	}

	return 0;

err:
	module_put(subdev_fh->owner);
	v4l2_fh_del(&subdev_fh->vfh);
	v4l2_fh_exit(&subdev_fh->vfh);
	subdev_fh_free(subdev_fh);
	kfree(subdev_fh);

	return ret;
}

static int subdev_close(struct file *file)
{
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
	struct v4l2_fh *vfh = file->private_data;
	struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);

	if (sd->internal_ops && sd->internal_ops->close)
		sd->internal_ops->close(sd, subdev_fh);
	module_put(subdev_fh->owner);
	v4l2_fh_del(vfh);
	v4l2_fh_exit(vfh);
	subdev_fh_free(subdev_fh);
	kfree(subdev_fh);
	file->private_data = NULL;

	return 0;
}
#else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
static int subdev_open(struct file *file)
{
	return -ENODEV;
}

static int subdev_close(struct file *file)
{
	return -ENODEV;
}
#endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */

static inline int check_which(u32 which)
{
	if (which != V4L2_SUBDEV_FORMAT_TRY &&
	    which != V4L2_SUBDEV_FORMAT_ACTIVE)
		return -EINVAL;

	return 0;
}

static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
{
#if defined(CONFIG_MEDIA_CONTROLLER)
	if (sd->entity.num_pads) {
		if (pad >= sd->entity.num_pads)
			return -EINVAL;
		return 0;
	}
#endif
	/* allow pad 0 on subdevices not registered as media entities */
	if (pad > 0)
		return -EINVAL;
	return 0;
}

static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
		       u32 which, u32 pad, u32 stream)
{
	if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
		if (!v4l2_subdev_state_get_format(state, pad, stream))
			return -EINVAL;
		return 0;
#else
		return -EINVAL;
#endif
	}

	if (stream != 0)
		return -EINVAL;

	if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
		return -EINVAL;

	return 0;
}

static inline int check_format(struct v4l2_subdev *sd,
			       struct v4l2_subdev_state *state,
			       struct v4l2_subdev_format *format)
{
	if (!format)
		return -EINVAL;

	return check_which(format->which) ? : check_pad(sd, format->pad) ? :
	       check_state(sd, state, format->which, format->pad, format->stream);
}

static int call_get_fmt(struct v4l2_subdev *sd,
			struct v4l2_subdev_state *state,
			struct v4l2_subdev_format *format)
{
	return check_format(sd, state, format) ? :
	       sd->ops->pad->get_fmt(sd, state, format);
}

static int call_set_fmt(struct v4l2_subdev *sd,
			struct v4l2_subdev_state *state,
			struct v4l2_subdev_format *format)
{
	return check_format(sd, state, format) ? :
	       sd->ops->pad->set_fmt(sd, state, format);
}

static int call_enum_mbus_code(struct v4l2_subdev *sd,
			       struct v4l2_subdev_state *state,
			       struct v4l2_subdev_mbus_code_enum *code)
{
	if (!code)
		return -EINVAL;

	return check_which(code->which) ? : check_pad(sd, code->pad) ? :
	       check_state(sd, state, code->which, code->pad, code->stream) ? :
	       sd->ops->pad->enum_mbus_code(sd, state, code);
}

static int call_enum_frame_size(struct v4l2_subdev *sd,
				struct v4l2_subdev_state *state,
				struct v4l2_subdev_frame_size_enum *fse)
{
	if (!fse)
		return -EINVAL;

	return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
	       check_state(sd, state, fse->which, fse->pad, fse->stream) ? :
	       sd->ops->pad->enum_frame_size(sd, state, fse);
}

static int call_enum_frame_interval(struct v4l2_subdev *sd,
				    struct v4l2_subdev_state *state,
				    struct v4l2_subdev_frame_interval_enum *fie)
{
	if (!fie)
		return -EINVAL;

	return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
	       check_state(sd, state, fie->which, fie->pad, fie->stream) ? :
	       sd->ops->pad->enum_frame_interval(sd, state, fie);
}

static inline int check_selection(struct v4l2_subdev *sd,
				  struct v4l2_subdev_state *state,
				  struct v4l2_subdev_selection *sel)
{
	if (!sel)
		return -EINVAL;

	return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
	       check_state(sd, state, sel->which, sel->pad, sel->stream);
}

static int call_get_selection(struct v4l2_subdev *sd,
			      struct v4l2_subdev_state *state,
			      struct v4l2_subdev_selection *sel)
{
	return check_selection(sd, state, sel) ? :
	       sd->ops->pad->get_selection(sd, state, sel);
}

static int call_set_selection(struct v4l2_subdev *sd,
			      struct v4l2_subdev_state *state,
			      struct v4l2_subdev_selection *sel)
{
	return check_selection(sd, state, sel) ? :
	       sd->ops->pad->set_selection(sd, state, sel);
}

static inline int check_frame_interval(struct v4l2_subdev *sd,
				       struct v4l2_subdev_state *state,
				       struct v4l2_subdev_frame_interval *fi)
{
	if (!fi)
		return -EINVAL;

	return check_which(fi->which) ? : check_pad(sd, fi->pad) ? :
	       check_state(sd, state, fi->which, fi->pad, fi->stream);
}

static int call_get_frame_interval(struct v4l2_subdev *sd,
				   struct v4l2_subdev_state *state,
				   struct v4l2_subdev_frame_interval *fi)
{
	return check_frame_interval(sd, state, fi) ? :
	       sd->ops->pad->get_frame_interval(sd, state, fi);
}

static int call_set_frame_interval(struct v4l2_subdev *sd,
				   struct v4l2_subdev_state *state,
				   struct v4l2_subdev_frame_interval *fi)
{
	return check_frame_interval(sd, state, fi) ? :
	       sd->ops->pad->set_frame_interval(sd, state, fi);
}

static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
			       struct v4l2_mbus_frame_desc *fd)
{
	unsigned int i;
	int ret;

	memset(fd, 0, sizeof(*fd));

	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
	if (ret)
		return ret;

	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
		"unknown");

	for (i = 0; i < fd->num_entries; i++) {
		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
		char buf[20] = "";

		if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
			WARN_ON(snprintf(buf, sizeof(buf),
					 ", vc %u, dt 0x%02x",
					 entry->bus.csi2.vc,
					 entry->bus.csi2.dt) >= sizeof(buf));

		dev_dbg(sd->dev,
			"\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
			entry->stream, entry->pixelcode, entry->length,
			entry->flags, buf);
	}

	return 0;
}

static inline int check_edid(struct v4l2_subdev *sd,
			     struct v4l2_subdev_edid *edid)
{
	if (!edid)
		return -EINVAL;

	if (edid->blocks && edid->edid == NULL)
		return -EINVAL;

	return check_pad(sd, edid->pad);
}

static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
{
	return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
}

static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
{
	return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
}

static int call_dv_timings_cap(struct v4l2_subdev *sd,
			       struct v4l2_dv_timings_cap *cap)
{
	if (!cap)
		return -EINVAL;

	return check_pad(sd, cap->pad) ? :
	       sd->ops->pad->dv_timings_cap(sd, cap);
}

static int call_enum_dv_timings(struct v4l2_subdev *sd,
				struct v4l2_enum_dv_timings *dvt)
{
	if (!dvt)
		return -EINVAL;

	return check_pad(sd, dvt->pad) ? :
	       sd->ops->pad->enum_dv_timings(sd, dvt);
}

static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
				struct v4l2_mbus_config *config)
{
	return check_pad(sd, pad) ? :
	       sd->ops->pad->get_mbus_config(sd, pad, config);
}

static int call_s_stream(struct v4l2_subdev *sd, int enable)
{
	int ret;

	/*
	 * The .s_stream() operation must never be called to start or stop an
	 * already started or stopped subdev. Catch offenders but don't return
	 * an error yet to avoid regressions.
	 *
	 * As .s_stream() is mutually exclusive with the .enable_streams() and
	 * .disable_streams() operation, we can use the enabled_streams field
	 * to store the subdev streaming state.
	 */
	if (WARN_ON(!!sd->enabled_streams == !!enable))
		return 0;

#if IS_REACHABLE(CONFIG_LEDS_CLASS)
	if (!IS_ERR_OR_NULL(sd->privacy_led)) {
		if (enable)
			led_set_brightness(sd->privacy_led,
					   sd->privacy_led->max_brightness);
		else
			led_set_brightness(sd->privacy_led, 0);
	}
#endif
	ret = sd->ops->video->s_stream(sd, enable);

	if (!enable && ret < 0) {
		dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
		ret = 0;
	}

	if (!ret)
		sd->enabled_streams = enable ? BIT(0) : 0;

	return ret;
}

#ifdef CONFIG_MEDIA_CONTROLLER
/*
 * Create state-management wrapper for pad ops dealing with subdev state. The
 * wrapper handles the case where the caller does not provide the called
 * subdev's state. This should be removed when all the callers are fixed.
 */
#define DEFINE_STATE_WRAPPER(f, arg_type)                                  \
	static int call_##f##_state(struct v4l2_subdev *sd,                \
				    struct v4l2_subdev_state *_state,      \
				    arg_type *arg)                         \
	{                                                                  \
		struct v4l2_subdev_state *state = _state;                  \
		int ret;                                                   \
		if (!_state)                                               \
			state = v4l2_subdev_lock_and_get_active_state(sd); \
		ret = call_##f(sd, state, arg);                            \
		if (!_state && state)                                      \
			v4l2_subdev_unlock_state(state);                   \
		return ret;                                                \
	}

#else /* CONFIG_MEDIA_CONTROLLER */

#define DEFINE_STATE_WRAPPER(f, arg_type)                            \
	static int call_##f##_state(struct v4l2_subdev *sd,          \
				    struct v4l2_subdev_state *state, \
				    arg_type *arg)                   \
	{                                                            \
		return call_##f(sd, state, arg);                     \
	}

#endif /* CONFIG_MEDIA_CONTROLLER */

DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);

static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
	.get_fmt		= call_get_fmt_state,
	.set_fmt		= call_set_fmt_state,
	.enum_mbus_code		= call_enum_mbus_code_state,
	.enum_frame_size	= call_enum_frame_size_state,
	.enum_frame_interval	= call_enum_frame_interval_state,
	.get_selection		= call_get_selection_state,
	.set_selection		= call_set_selection_state,
	.get_frame_interval	= call_get_frame_interval,
	.set_frame_interval	= call_set_frame_interval,
	.get_edid		= call_get_edid,
	.set_edid		= call_set_edid,
	.dv_timings_cap		= call_dv_timings_cap,
	.enum_dv_timings	= call_enum_dv_timings,
	.get_frame_desc		= call_get_frame_desc,
	.get_mbus_config	= call_get_mbus_config,
};

static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
	.s_stream		= call_s_stream,
};

const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
	.pad	= &v4l2_subdev_call_pad_wrappers,
	.video	= &v4l2_subdev_call_video_wrappers,
};
EXPORT_SYMBOL(v4l2_subdev_call_wrappers);

#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)

static struct v4l2_subdev_state *
subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
		       unsigned int cmd, void *arg)
{
	u32 which;

	switch (cmd) {
	default:
		return NULL;
	case VIDIOC_SUBDEV_G_FMT:
	case VIDIOC_SUBDEV_S_FMT:
		which = ((struct v4l2_subdev_format *)arg)->which;
		break;
	case VIDIOC_SUBDEV_G_CROP:
	case VIDIOC_SUBDEV_S_CROP:
		which = ((struct v4l2_subdev_crop *)arg)->which;
		break;
	case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
		which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
		break;
	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
		which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
		break;
	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
		which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
		break;
	case VIDIOC_SUBDEV_G_SELECTION:
	case VIDIOC_SUBDEV_S_SELECTION:
		which = ((struct v4l2_subdev_selection *)arg)->which;
		break;
	case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
	case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
		struct v4l2_subdev_frame_interval *fi = arg;

		if (!(subdev_fh->client_caps &
		      V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH))
			fi->which = V4L2_SUBDEV_FORMAT_ACTIVE;

		which = fi->which;
		break;
	}
	case VIDIOC_SUBDEV_G_ROUTING:
	case VIDIOC_SUBDEV_S_ROUTING:
		which = ((struct v4l2_subdev_routing *)arg)->which;
		break;
	}

	return which == V4L2_SUBDEV_FORMAT_TRY ?
			     subdev_fh->state :
			     v4l2_subdev_get_unlocked_active_state(sd);
}

static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
			    struct v4l2_subdev_state *state)
{
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
	struct v4l2_fh *vfh = file->private_data;
	struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
	bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
	bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
	bool client_supports_streams = subdev_fh->client_caps &
				       V4L2_SUBDEV_CLIENT_CAP_STREAMS;
	int rval;

	/*
	 * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
	 * Remove this when the API is no longer experimental.
	 */
	if (!v4l2_subdev_enable_streams_api)
		streams_subdev = false;

	switch (cmd) {
	case VIDIOC_SUBDEV_QUERYCAP: {
		struct v4l2_subdev_capability *cap = arg;

		memset(cap->reserved, 0, sizeof(cap->reserved));
		cap->version = LINUX_VERSION_CODE;
		cap->capabilities =
			(ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
			(streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);

		return 0;
	}

	case VIDIOC_QUERYCTRL:
		/*
		 * TODO: this really should be folded into v4l2_queryctrl (this
		 * currently returns -EINVAL for NULL control handlers).
		 * However, v4l2_queryctrl() is still called directly by
		 * drivers as well and until that has been addressed I believe
		 * it is safer to do the check here. The same is true for the
		 * other control ioctls below.
		 */
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_queryctrl(vfh->ctrl_handler, arg);

	case VIDIOC_QUERY_EXT_CTRL:
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);

	case VIDIOC_QUERYMENU:
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_querymenu(vfh->ctrl_handler, arg);

	case VIDIOC_G_CTRL:
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_g_ctrl(vfh->ctrl_handler, arg);

	case VIDIOC_S_CTRL:
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);

	case VIDIOC_G_EXT_CTRLS:
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_g_ext_ctrls(vfh->ctrl_handler,
					vdev, sd->v4l2_dev->mdev, arg);

	case VIDIOC_S_EXT_CTRLS:
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
					vdev, sd->v4l2_dev->mdev, arg);

	case VIDIOC_TRY_EXT_CTRLS:
		if (!vfh->ctrl_handler)
			return -ENOTTY;
		return v4l2_try_ext_ctrls(vfh->ctrl_handler,
					  vdev, sd->v4l2_dev->mdev, arg);

	case VIDIOC_DQEVENT:
		if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
			return -ENOIOCTLCMD;

		return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);

	case VIDIOC_SUBSCRIBE_EVENT:
		return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);

	case VIDIOC_UNSUBSCRIBE_EVENT:
		return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);

#ifdef CONFIG_VIDEO_ADV_DEBUG
	case VIDIOC_DBG_G_REGISTER:
	{
		struct v4l2_dbg_register *p = arg;

		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		return v4l2_subdev_call(sd, core, g_register, p);
	}
	case VIDIOC_DBG_S_REGISTER:
	{
		struct v4l2_dbg_register *p = arg;

		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;
		return v4l2_subdev_call(sd, core, s_register, p);
	}
	case VIDIOC_DBG_G_CHIP_INFO:
	{
		struct v4l2_dbg_chip_info *p = arg;

		if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
			return -EINVAL;
		if (sd->ops->core && sd->ops->core->s_register)
			p->flags |= V4L2_CHIP_FL_WRITABLE;
		if (sd->ops->core && sd->ops->core->g_register)
			p->flags |= V4L2_CHIP_FL_READABLE;
		strscpy(p->name, sd->name, sizeof(p->name));
		return 0;
	}
#endif

	case VIDIOC_LOG_STATUS: {
		int ret;

		pr_info("%s: =================  START STATUS  =================\n",
			sd->name);
		ret = v4l2_subdev_call(sd, core, log_status);
		pr_info("%s: ==================  END STATUS  ==================\n",
			sd->name);
		return ret;
	}

	case VIDIOC_SUBDEV_G_FMT: {
		struct v4l2_subdev_format *format = arg;

		if (!client_supports_streams)
			format->stream = 0;

		memset(format->reserved, 0, sizeof(format->reserved));
		memset(format->format.reserved, 0, sizeof(format->format.reserved));
		return v4l2_subdev_call(sd, pad, get_fmt, state, format);
	}

	case VIDIOC_SUBDEV_S_FMT: {
		struct v4l2_subdev_format *format = arg;

		if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
			return -EPERM;

		if (!client_supports_streams)
			format->stream = 0;

		memset(format->reserved, 0, sizeof(format->reserved));
		memset(format->format.reserved, 0, sizeof(format->format.reserved));
		return v4l2_subdev_call(sd, pad, set_fmt, state, format);
	}

	case VIDIOC_SUBDEV_G_CROP: {
		struct v4l2_subdev_crop *crop = arg;
		struct v4l2_subdev_selection sel;

		if (!client_supports_streams)
			crop->stream = 0;

		memset(crop->reserved, 0, sizeof(crop->reserved));
		memset(&sel, 0, sizeof(sel));
		sel.which = crop->which;
		sel.pad = crop->pad;
		sel.target = V4L2_SEL_TGT_CROP;

		rval = v4l2_subdev_call(
			sd, pad, get_selection, state, &sel);

		crop->rect = sel.r;

		return rval;
	}

	case VIDIOC_SUBDEV_S_CROP: {
		struct v4l2_subdev_crop *crop = arg;
		struct v4l2_subdev_selection sel;

		if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
			return -EPERM;

		if (!client_supports_streams)
			crop->stream = 0;

		memset(crop->reserved, 0, sizeof(crop->reserved));
		memset(&sel, 0, sizeof(sel));
		sel.which = crop->which;
		sel.pad = crop->pad;
		sel.target = V4L2_SEL_TGT_CROP;
		sel.r = crop->rect;

		rval = v4l2_subdev_call(
			sd, pad, set_selection, state, &sel);

		crop->rect = sel.r;

		return rval;
	}

	case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
		struct v4l2_subdev_mbus_code_enum *code = arg;

		if (!client_supports_streams)
			code->stream = 0;

		memset(code->reserved, 0, sizeof(code->reserved));
		return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
					code);
	}

	case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
		struct v4l2_subdev_frame_size_enum *fse = arg;

		if (!client_supports_streams)
			fse->stream = 0;

		memset(fse->reserved, 0, sizeof(fse->reserved));
		return v4l2_subdev_call(sd, pad, enum_frame_size, state,
					fse);
	}

	case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
		struct v4l2_subdev_frame_interval *fi = arg;

		if (!client_supports_streams)
			fi->stream = 0;

		memset(fi->reserved, 0, sizeof(fi->reserved));
		return v4l2_subdev_call(sd, pad, get_frame_interval, state, fi);
	}

	case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
		struct v4l2_subdev_frame_interval *fi = arg;

		if (!client_supports_streams)
			fi->stream = 0;

		if (fi->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
			return -EPERM;

		memset(fi->reserved, 0, sizeof(fi->reserved));
		return v4l2_subdev_call(sd, pad, set_frame_interval, state, fi);
	}

	case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
		struct v4l2_subdev_frame_interval_enum *fie = arg;

		if (!client_supports_streams)
			fie->stream = 0;

		memset(fie->reserved, 0, sizeof(fie->reserved));
		return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
					fie);
	}

	case VIDIOC_SUBDEV_G_SELECTION: {
		struct v4l2_subdev_selection *sel = arg;

		if (!client_supports_streams)
			sel->stream = 0;

		memset(sel->reserved, 0, sizeof(sel->reserved));
		return v4l2_subdev_call(
			sd, pad, get_selection, state, sel);
	}

	case VIDIOC_SUBDEV_S_SELECTION: {
		struct v4l2_subdev_selection *sel = arg;

		if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
			return -EPERM;

		if (!client_supports_streams)
			sel->stream = 0;

		memset(sel->reserved, 0, sizeof(sel->reserved));
		return v4l2_subdev_call(
			sd, pad, set_selection, state, sel);
	}

	case VIDIOC_G_EDID: {
		struct v4l2_subdev_edid *edid = arg;

		return v4l2_subdev_call(sd, pad, get_edid, edid);
	}

	case VIDIOC_S_EDID: {
		struct v4l2_subdev_edid *edid = arg;

		return v4l2_subdev_call(sd, pad, set_edid, edid);
	}

	case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
		struct v4l2_dv_timings_cap *cap = arg;

		return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
	}

	case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
		struct v4l2_enum_dv_timings *dvt = arg;

		return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
	}

	case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
		return v4l2_subdev_call(sd, video, query_dv_timings, arg);

	case VIDIOC_SUBDEV_G_DV_TIMINGS:
		return v4l2_subdev_call(sd, video, g_dv_timings, arg);

	case VIDIOC_SUBDEV_S_DV_TIMINGS:
		if (ro_subdev)
			return -EPERM;

		return v4l2_subdev_call(sd, video, s_dv_timings, arg);

	case VIDIOC_SUBDEV_G_STD:
		return v4l2_subdev_call(sd, video, g_std, arg);

	case VIDIOC_SUBDEV_S_STD: {
		v4l2_std_id *std = arg;

		if (ro_subdev)
			return -EPERM;

		return v4l2_subdev_call(sd, video, s_std, *std);
	}

	case VIDIOC_SUBDEV_ENUMSTD: {
		struct v4l2_standard *p = arg;
		v4l2_std_id id;

		if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
			return -EINVAL;

		return v4l_video_std_enumstd(p, id);
	}

	case VIDIOC_SUBDEV_QUERYSTD:
		return v4l2_subdev_call(sd, video, querystd, arg);

	case VIDIOC_SUBDEV_G_ROUTING: {
		struct v4l2_subdev_routing *routing = arg;
		struct v4l2_subdev_krouting *krouting;

		if (!v4l2_subdev_enable_streams_api)
			return -ENOIOCTLCMD;

		if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
			return -ENOIOCTLCMD;

		memset(routing->reserved, 0, sizeof(routing->reserved));

		krouting = &state->routing;

		if (routing->num_routes < krouting->num_routes) {
			routing->num_routes = krouting->num_routes;
			return -ENOSPC;
		}

		memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
		       krouting->routes,
		       krouting->num_routes * sizeof(*krouting->routes));
		routing->num_routes = krouting->num_routes;

		return 0;
	}

	case VIDIOC_SUBDEV_S_ROUTING: {
		struct v4l2_subdev_routing *routing = arg;
		struct v4l2_subdev_route *routes =
			(struct v4l2_subdev_route *)(uintptr_t)routing->routes;
		struct v4l2_subdev_krouting krouting = {};
		unsigned int i;

		if (!v4l2_subdev_enable_streams_api)
			return -ENOIOCTLCMD;

		if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
			return -ENOIOCTLCMD;

		if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
			return -EPERM;

		memset(routing->reserved, 0, sizeof(routing->reserved));

		for (i = 0; i < routing->num_routes; ++i) {
			const struct v4l2_subdev_route *route = &routes[i];
			const struct media_pad *pads = sd->entity.pads;

			if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
			    route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
				return -EINVAL;

			if (route->sink_pad >= sd->entity.num_pads)
				return -EINVAL;

			if (!(pads[route->sink_pad].flags &
			      MEDIA_PAD_FL_SINK))
				return -EINVAL;

			if (route->source_pad >= sd->entity.num_pads)
				return -EINVAL;

			if (!(pads[route->source_pad].flags &
			      MEDIA_PAD_FL_SOURCE))
				return -EINVAL;
		}

		krouting.num_routes = routing->num_routes;
		krouting.routes = routes;

		return v4l2_subdev_call(sd, pad, set_routing, state,
					routing->which, &krouting);
	}

	case VIDIOC_SUBDEV_G_CLIENT_CAP: {
		struct v4l2_subdev_client_capability *client_cap = arg;

		client_cap->capabilities = subdev_fh->client_caps;

		return 0;
	}

	case VIDIOC_SUBDEV_S_CLIENT_CAP: {
		struct v4l2_subdev_client_capability *client_cap = arg;

		/*
		 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
		 * enabled. Remove this when streams API is no longer
		 * experimental.
		 */
		if (!v4l2_subdev_enable_streams_api)
			client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;

		/* Filter out unsupported capabilities */
		client_cap->capabilities &= (V4L2_SUBDEV_CLIENT_CAP_STREAMS |
					     V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH);

		subdev_fh->client_caps = client_cap->capabilities;

		return 0;
	}

	default:
		return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
	}

	return 0;
}

static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
{
	struct video_device *vdev = video_devdata(file);
	struct mutex *lock = vdev->lock;
	long ret = -ENODEV;

	if (lock && mutex_lock_interruptible(lock))
		return -ERESTARTSYS;

	if (video_is_registered(vdev)) {
		struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
		struct v4l2_fh *vfh = file->private_data;
		struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
		struct v4l2_subdev_state *state;

		state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);

		if (state)
			v4l2_subdev_lock_state(state);

		ret = subdev_do_ioctl(file, cmd, arg, state);

		if (state)
			v4l2_subdev_unlock_state(state);
	}

	if (lock)
		mutex_unlock(lock);
	return ret;
}

static long subdev_ioctl(struct file *file, unsigned int cmd,
	unsigned long arg)
{
	return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
}

#ifdef CONFIG_COMPAT
static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
	unsigned long arg)
{
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);

	return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
}
#endif

#else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
static long subdev_ioctl(struct file *file, unsigned int cmd,
			 unsigned long arg)
{
	return -ENODEV;
}

#ifdef CONFIG_COMPAT
static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
				  unsigned long arg)
{
	return -ENODEV;
}
#endif
#endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */

static __poll_t subdev_poll(struct file *file, poll_table *wait)
{
	struct video_device *vdev = video_devdata(file);
	struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
	struct v4l2_fh *fh = file->private_data;

	if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
		return EPOLLERR;

	poll_wait(file, &fh->wait, wait);

	if (v4l2_event_pending(fh))
		return EPOLLPRI;

	return 0;
}

const struct v4l2_file_operations v4l2_subdev_fops = {
	.owner = THIS_MODULE,
	.open = subdev_open,
	.unlocked_ioctl = subdev_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl32 = subdev_compat_ioctl32,
#endif
	.release = subdev_close,
	.poll = subdev_poll,
};

#ifdef CONFIG_MEDIA_CONTROLLER

int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
				      struct fwnode_endpoint *endpoint)
{
	struct fwnode_handle *fwnode;
	struct v4l2_subdev *sd;

	if (!is_media_entity_v4l2_subdev(entity))
		return -EINVAL;

	sd = media_entity_to_v4l2_subdev(entity);

	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
	fwnode_handle_put(fwnode);

	if (device_match_fwnode(sd->dev, fwnode))
		return endpoint->port;

	return -ENXIO;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);

int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
				      struct media_link *link,
				      struct v4l2_subdev_format *source_fmt,
				      struct v4l2_subdev_format *sink_fmt)
{
	bool pass = true;

	/* The width, height and code must match. */
	if (source_fmt->format.width != sink_fmt->format.width) {
		dev_dbg(sd->entity.graph_obj.mdev->dev,
			"%s: width does not match (source %u, sink %u)\n",
			__func__,
			source_fmt->format.width, sink_fmt->format.width);
		pass = false;
	}

	if (source_fmt->format.height != sink_fmt->format.height) {
		dev_dbg(sd->entity.graph_obj.mdev->dev,
			"%s: height does not match (source %u, sink %u)\n",
			__func__,
			source_fmt->format.height, sink_fmt->format.height);
		pass = false;
	}

	if (source_fmt->format.code != sink_fmt->format.code) {
		dev_dbg(sd->entity.graph_obj.mdev->dev,
			"%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
			__func__,
			source_fmt->format.code, sink_fmt->format.code);
		pass = false;
	}

	/* The field order must match, or the sink field order must be NONE
	 * to support interlaced hardware connected to bridges that support
	 * progressive formats only.
	 */
	if (source_fmt->format.field != sink_fmt->format.field &&
	    sink_fmt->format.field != V4L2_FIELD_NONE) {
		dev_dbg(sd->entity.graph_obj.mdev->dev,
			"%s: field does not match (source %u, sink %u)\n",
			__func__,
			source_fmt->format.field, sink_fmt->format.field);
		pass = false;
	}

	if (pass)
		return 0;

	dev_dbg(sd->entity.graph_obj.mdev->dev,
		"%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
		link->source->entity->name, link->source->index,
		link->sink->entity->name, link->sink->index);

	return -EPIPE;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);

static int
v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
				     struct v4l2_subdev_format *fmt,
				     bool states_locked)
{
	struct v4l2_subdev_state *state;
	struct v4l2_subdev *sd;
	int ret;

	if (!is_media_entity_v4l2_subdev(pad->entity)) {
		WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
		     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
		     pad->entity->function, pad->entity->name);

		return -EINVAL;
	}

	sd = media_entity_to_v4l2_subdev(pad->entity);

	fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
	fmt->pad = pad->index;
	fmt->stream = stream;

	if (states_locked)
		state = v4l2_subdev_get_locked_active_state(sd);
	else
		state = v4l2_subdev_lock_and_get_active_state(sd);

	ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);

	if (!states_locked && state)
		v4l2_subdev_unlock_state(state);

	return ret;
}

#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)

static void __v4l2_link_validate_get_streams(struct media_pad *pad,
					     u64 *streams_mask,
					     bool states_locked)
{
	struct v4l2_subdev_route *route;
	struct v4l2_subdev_state *state;
	struct v4l2_subdev *subdev;

	subdev = media_entity_to_v4l2_subdev(pad->entity);

	*streams_mask = 0;

	if (states_locked)
		state = v4l2_subdev_get_locked_active_state(subdev);
	else
		state = v4l2_subdev_lock_and_get_active_state(subdev);

	if (WARN_ON(!state))
		return;

	for_each_active_route(&state->routing, route) {
		u32 route_pad;
		u32 route_stream;

		if (pad->flags & MEDIA_PAD_FL_SOURCE) {
			route_pad = route->source_pad;
			route_stream = route->source_stream;
		} else {
			route_pad = route->sink_pad;
			route_stream = route->sink_stream;
		}

		if (route_pad != pad->index)
			continue;

		*streams_mask |= BIT_ULL(route_stream);
	}

	if (!states_locked)
		v4l2_subdev_unlock_state(state);
}

#endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */

static void v4l2_link_validate_get_streams(struct media_pad *pad,
					   u64 *streams_mask,
					   bool states_locked)
{
	struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);

	if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
		/* Non-streams subdevs have an implicit stream 0 */
		*streams_mask = BIT_ULL(0);
		return;
	}

#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
	__v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
#else
	/* This shouldn't happen */
	*streams_mask = 0;
#endif
}

static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
{
	struct v4l2_subdev *sink_subdev =
		media_entity_to_v4l2_subdev(link->sink->entity);
	struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
	u64 source_streams_mask;
	u64 sink_streams_mask;
	u64 dangling_sink_streams;
	u32 stream;
	int ret;

	dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
		link->source->entity->name, link->source->index,
		link->sink->entity->name, link->sink->index);

	v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
	v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);

	/*
	 * It is ok to have more source streams than sink streams as extra
	 * source streams can just be ignored by the receiver, but having extra
	 * sink streams is an error as streams must have a source.
	 */
	dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
				sink_streams_mask;
	if (dangling_sink_streams) {
		dev_err(dev, "Dangling sink streams: mask %#llx\n",
			dangling_sink_streams);
		return -EINVAL;
	}

	/* Validate source and sink stream formats */

	for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
		struct v4l2_subdev_format sink_fmt, source_fmt;

		if (!(sink_streams_mask & BIT_ULL(stream)))
			continue;

		dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
			link->source->entity->name, link->source->index, stream,
			link->sink->entity->name, link->sink->index, stream);

		ret = v4l2_subdev_link_validate_get_format(link->source, stream,
							   &source_fmt, states_locked);
		if (ret < 0) {
			dev_dbg(dev,
				"Failed to get format for \"%s\":%u:%u (but that's ok)\n",
				link->source->entity->name, link->source->index,
				stream);
			continue;
		}

		ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
							   &sink_fmt, states_locked);
		if (ret < 0) {
			dev_dbg(dev,
				"Failed to get format for \"%s\":%u:%u (but that's ok)\n",
				link->sink->entity->name, link->sink->index,
				stream);
			continue;
		}

		/* TODO: add stream number to link_validate() */
		ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
				       &source_fmt, &sink_fmt);
		if (!ret)
			continue;

		if (ret != -ENOIOCTLCMD)
			return ret;

		ret = v4l2_subdev_link_validate_default(sink_subdev, link,
							&source_fmt, &sink_fmt);

		if (ret)
			return ret;
	}

	return 0;
}

int v4l2_subdev_link_validate(struct media_link *link)
{
	struct v4l2_subdev *source_sd, *sink_sd;
	struct v4l2_subdev_state *source_state, *sink_state;
	bool states_locked;
	int ret;

	if (!is_media_entity_v4l2_subdev(link->sink->entity) ||
	    !is_media_entity_v4l2_subdev(link->source->entity)) {
		pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n",
			     !is_media_entity_v4l2_subdev(link->sink->entity) ?
			     "sink" : "source",
			     link->source->entity->name, link->source->index,
			     link->sink->entity->name, link->sink->index);
		return 0;
	}

	sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
	source_sd = media_entity_to_v4l2_subdev(link->source->entity);

	sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
	source_state = v4l2_subdev_get_unlocked_active_state(source_sd);

	states_locked = sink_state && source_state;

	if (states_locked) {
		v4l2_subdev_lock_state(sink_state);
		v4l2_subdev_lock_state(source_state);
	}

	ret = v4l2_subdev_link_validate_locked(link, states_locked);

	if (states_locked) {
		v4l2_subdev_unlock_state(sink_state);
		v4l2_subdev_unlock_state(source_state);
	}

	return ret;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);

bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
				  unsigned int pad0, unsigned int pad1)
{
	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
	struct v4l2_subdev_krouting *routing;
	struct v4l2_subdev_state *state;
	unsigned int i;

	state = v4l2_subdev_lock_and_get_active_state(sd);

	routing = &state->routing;

	for (i = 0; i < routing->num_routes; ++i) {
		struct v4l2_subdev_route *route = &routing->routes[i];

		if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
			continue;

		if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
		    (route->source_pad == pad0 && route->sink_pad == pad1)) {
			v4l2_subdev_unlock_state(state);
			return true;
		}
	}

	v4l2_subdev_unlock_state(state);

	return false;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);

struct v4l2_subdev_state *
__v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
			  struct lock_class_key *lock_key)
{
	struct v4l2_subdev_state *state;
	int ret;

	state = kzalloc(sizeof(*state), GFP_KERNEL);
	if (!state)
		return ERR_PTR(-ENOMEM);

	__mutex_init(&state->_lock, lock_name, lock_key);
	if (sd->state_lock)
		state->lock = sd->state_lock;
	else
		state->lock = &state->_lock;

	state->sd = sd;

	/* Drivers that support streams do not need the legacy pad config */
	if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
		state->pads = kvcalloc(sd->entity.num_pads,
				       sizeof(*state->pads), GFP_KERNEL);
		if (!state->pads) {
			ret = -ENOMEM;
			goto err;
		}
	}

	if (sd->internal_ops && sd->internal_ops->init_state) {
		/*
		 * There can be no race at this point, but we lock the state
		 * anyway to satisfy lockdep checks.
		 */
		v4l2_subdev_lock_state(state);
		ret = sd->internal_ops->init_state(sd, state);
		v4l2_subdev_unlock_state(state);

		if (ret)
			goto err;
	}

	return state;

err:
	if (state && state->pads)
		kvfree(state->pads);

	kfree(state);

	return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);

void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
{
	if (!state)
		return;

	mutex_destroy(&state->_lock);

	kfree(state->routing.routes);
	kvfree(state->stream_configs.configs);
	kvfree(state->pads);
	kfree(state);
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);

int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
				struct lock_class_key *key)
{
	struct v4l2_subdev_state *state;

	state = __v4l2_subdev_state_alloc(sd, name, key);
	if (IS_ERR(state))
		return PTR_ERR(state);

	sd->active_state = state;

	return 0;
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);

void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
{
	struct v4l2_async_subdev_endpoint *ase, *ase_tmp;

	__v4l2_subdev_state_free(sd->active_state);
	sd->active_state = NULL;

	/* Uninitialised sub-device, bail out here. */
	if (!sd->async_subdev_endpoint_list.next)
		return;

	list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
				 async_subdev_endpoint_entry) {
		list_del(&ase->async_subdev_endpoint_entry);

		kfree(ase);
	}
}
EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);

struct v4l2_mbus_framefmt *
__v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
			       unsigned int pad, u32 stream)
{
	struct v4l2_subdev_stream_configs *stream_configs;
	unsigned int i;

	if (WARN_ON_ONCE(!state))
		return NULL;

	if (state->pads) {
		if (stream)
			return NULL;

		if (pad >= state->sd->entity.num_pads)
			return NULL;

		return &state->pads[pad].format;
	}

	lockdep_assert_held(state->lock);

	stream_configs = &state->stream_configs;

	for (i = 0; i < stream_configs->num_configs; ++i) {
		if (stream_configs->configs[i].pad == pad &&
		    stream_configs->configs[i].stream == stream)
			return &stream_configs->configs[i].fmt;
	}

	return NULL;
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_format);

struct v4l2_rect *
__v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
			     u32 stream)
{
	struct v4l2_subdev_stream_configs *stream_configs;
	unsigned int i;

	if (WARN_ON_ONCE(!state))
		return NULL;

	if (state->pads) {
		if (stream)
			return NULL;

		if (pad >= state->sd->entity.num_pads)
			return NULL;

		return &state->pads[pad].crop;
	}

	lockdep_assert_held(state->lock);

	stream_configs = &state->stream_configs;

	for (i = 0; i < stream_configs->num_configs; ++i) {
		if (stream_configs->configs[i].pad == pad &&
		    stream_configs->configs[i].stream == stream)
			return &stream_configs->configs[i].crop;
	}

	return NULL;
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_crop);

struct v4l2_rect *
__v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
				unsigned int pad, u32 stream)
{
	struct v4l2_subdev_stream_configs *stream_configs;
	unsigned int i;

	if (WARN_ON_ONCE(!state))
		return NULL;

	if (state->pads) {
		if (stream)
			return NULL;

		if (pad >= state->sd->entity.num_pads)
			return NULL;

		return &state->pads[pad].compose;
	}

	lockdep_assert_held(state->lock);

	stream_configs = &state->stream_configs;

	for (i = 0; i < stream_configs->num_configs; ++i) {
		if (stream_configs->configs[i].pad == pad &&
		    stream_configs->configs[i].stream == stream)
			return &stream_configs->configs[i].compose;
	}

	return NULL;
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_compose);

struct v4l2_fract *
__v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
				 unsigned int pad, u32 stream)
{
	struct v4l2_subdev_stream_configs *stream_configs;
	unsigned int i;

	if (WARN_ON(!state))
		return NULL;

	lockdep_assert_held(state->lock);

	if (state->pads) {
		if (stream)
			return NULL;

		if (pad >= state->sd->entity.num_pads)
			return NULL;

		return &state->pads[pad].interval;
	}

	lockdep_assert_held(state->lock);

	stream_configs = &state->stream_configs;

	for (i = 0; i < stream_configs->num_configs; ++i) {
		if (stream_configs->configs[i].pad == pad &&
		    stream_configs->configs[i].stream == stream)
			return &stream_configs->configs[i].interval;
	}

	return NULL;
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_interval);

#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)

static int
v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
				const struct v4l2_subdev_krouting *routing)
{
	struct v4l2_subdev_stream_configs new_configs = { 0 };
	struct v4l2_subdev_route *route;
	u32 idx;

	/* Count number of formats needed */
	for_each_active_route(routing, route) {
		/*
		 * Each route needs a format on both ends of the route.
		 */
		new_configs.num_configs += 2;
	}

	if (new_configs.num_configs) {
		new_configs.configs = kvcalloc(new_configs.num_configs,
					       sizeof(*new_configs.configs),
					       GFP_KERNEL);

		if (!new_configs.configs)
			return -ENOMEM;
	}

	/*
	 * Fill in the 'pad' and stream' value for each item in the array from
	 * the routing table
	 */
	idx = 0;

	for_each_active_route(routing, route) {
		new_configs.configs[idx].pad = route->sink_pad;
		new_configs.configs[idx].stream = route->sink_stream;

		idx++;

		new_configs.configs[idx].pad = route->source_pad;
		new_configs.configs[idx].stream = route->source_stream;

		idx++;
	}

	kvfree(stream_configs->configs);
	*stream_configs = new_configs;

	return 0;
}

int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
			struct v4l2_subdev_format *format)
{
	struct v4l2_mbus_framefmt *fmt;

	fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
	if (!fmt)
		return -EINVAL;

	format->format = *fmt;

	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);

int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
				   struct v4l2_subdev_state *state,
				   struct v4l2_subdev_frame_interval *fi)
{
	struct v4l2_fract *interval;

	interval = v4l2_subdev_state_get_interval(state, fi->pad, fi->stream);
	if (!interval)
		return -EINVAL;

	fi->interval = *interval;

	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_interval);

int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
			    struct v4l2_subdev_state *state,
			    const struct v4l2_subdev_krouting *routing)
{
	struct v4l2_subdev_krouting *dst = &state->routing;
	const struct v4l2_subdev_krouting *src = routing;
	struct v4l2_subdev_krouting new_routing = { 0 };
	size_t bytes;
	int r;

	if (unlikely(check_mul_overflow((size_t)src->num_routes,
					sizeof(*src->routes), &bytes)))
		return -EOVERFLOW;

	lockdep_assert_held(state->lock);

	if (src->num_routes > 0) {
		new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
		if (!new_routing.routes)
			return -ENOMEM;
	}

	new_routing.num_routes = src->num_routes;

	r = v4l2_subdev_init_stream_configs(&state->stream_configs,
					    &new_routing);
	if (r) {
		kfree(new_routing.routes);
		return r;
	}

	kfree(dst->routes);
	*dst = new_routing;

	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);

struct v4l2_subdev_route *
__v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
				struct v4l2_subdev_route *route)
{
	if (route)
		++route;
	else
		route = &routing->routes[0];

	for (; route < routing->routes + routing->num_routes; ++route) {
		if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
			continue;

		return route;
	}

	return NULL;
}
EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);

int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
				     struct v4l2_subdev_state *state,
				     const struct v4l2_subdev_krouting *routing,
				     const struct v4l2_mbus_framefmt *fmt)
{
	struct v4l2_subdev_stream_configs *stream_configs;
	unsigned int i;
	int ret;

	ret = v4l2_subdev_set_routing(sd, state, routing);
	if (ret)
		return ret;

	stream_configs = &state->stream_configs;

	for (i = 0; i < stream_configs->num_configs; ++i)
		stream_configs->configs[i].fmt = *fmt;

	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);

int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
					  u32 pad, u32 stream, u32 *other_pad,
					  u32 *other_stream)
{
	unsigned int i;

	for (i = 0; i < routing->num_routes; ++i) {
		struct v4l2_subdev_route *route = &routing->routes[i];

		if (route->source_pad == pad &&
		    route->source_stream == stream) {
			if (other_pad)
				*other_pad = route->sink_pad;
			if (other_stream)
				*other_stream = route->sink_stream;
			return 0;
		}

		if (route->sink_pad == pad && route->sink_stream == stream) {
			if (other_pad)
				*other_pad = route->source_pad;
			if (other_stream)
				*other_stream = route->source_stream;
			return 0;
		}
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);

struct v4l2_mbus_framefmt *
v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
					     u32 pad, u32 stream)
{
	u32 other_pad, other_stream;
	int ret;

	ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
						    pad, stream,
						    &other_pad, &other_stream);
	if (ret)
		return NULL;

	return v4l2_subdev_state_get_format(state, other_pad, other_stream);
}
EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);

u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
				    u32 pad0, u32 pad1, u64 *streams)
{
	const struct v4l2_subdev_krouting *routing = &state->routing;
	struct v4l2_subdev_route *route;
	u64 streams0 = 0;
	u64 streams1 = 0;

	for_each_active_route(routing, route) {
		if (route->sink_pad == pad0 && route->source_pad == pad1 &&
		    (*streams & BIT_ULL(route->sink_stream))) {
			streams0 |= BIT_ULL(route->sink_stream);
			streams1 |= BIT_ULL(route->source_stream);
		}
		if (route->source_pad == pad0 && route->sink_pad == pad1 &&
		    (*streams & BIT_ULL(route->source_stream))) {
			streams0 |= BIT_ULL(route->source_stream);
			streams1 |= BIT_ULL(route->sink_stream);
		}
	}

	*streams = streams0;
	return streams1;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);

int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
				 const struct v4l2_subdev_krouting *routing,
				 enum v4l2_subdev_routing_restriction disallow)
{
	u32 *remote_pads = NULL;
	unsigned int i, j;
	int ret = -EINVAL;

	if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
			V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
		remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
				      GFP_KERNEL);
		if (!remote_pads)
			return -ENOMEM;

		for (i = 0; i < sd->entity.num_pads; ++i)
			remote_pads[i] = U32_MAX;
	}

	for (i = 0; i < routing->num_routes; ++i) {
		const struct v4l2_subdev_route *route = &routing->routes[i];

		/* Validate the sink and source pad numbers. */
		if (route->sink_pad >= sd->entity.num_pads ||
		    !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
			dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
				i, route->sink_pad);
			goto out;
		}

		if (route->source_pad >= sd->entity.num_pads ||
		    !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
			dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
				i, route->source_pad);
			goto out;
		}

		/*
		 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
		 * sink pad must be routed to a single source pad.
		 */
		if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
			if (remote_pads[route->sink_pad] != U32_MAX &&
			    remote_pads[route->sink_pad] != route->source_pad) {
				dev_dbg(sd->dev,
					"route %u attempts to mix %s streams\n",
					i, "sink");
				goto out;
			}
		}

		/*
		 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
		 * source pad must originate from a single sink pad.
		 */
		if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
			if (remote_pads[route->source_pad] != U32_MAX &&
			    remote_pads[route->source_pad] != route->sink_pad) {
				dev_dbg(sd->dev,
					"route %u attempts to mix %s streams\n",
					i, "source");
				goto out;
			}
		}

		/*
		 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
		 * side can not do stream multiplexing, i.e. there can be only
		 * a single stream in a sink pad.
		 */
		if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
			if (remote_pads[route->sink_pad] != U32_MAX) {
				dev_dbg(sd->dev,
					"route %u attempts to multiplex on %s pad %u\n",
					i, "sink", route->sink_pad);
				goto out;
			}
		}

		/*
		 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
		 * source side can not do stream multiplexing, i.e. there can
		 * be only a single stream in a source pad.
		 */
		if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
			if (remote_pads[route->source_pad] != U32_MAX) {
				dev_dbg(sd->dev,
					"route %u attempts to multiplex on %s pad %u\n",
					i, "source", route->source_pad);
				goto out;
			}
		}

		if (remote_pads) {
			remote_pads[route->sink_pad] = route->source_pad;
			remote_pads[route->source_pad] = route->sink_pad;
		}

		for (j = i + 1; j < routing->num_routes; ++j) {
			const struct v4l2_subdev_route *r = &routing->routes[j];

			/*
			 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
			 * originate from the same (sink) stream.
			 */
			if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
			    route->sink_pad == r->sink_pad &&
			    route->sink_stream == r->sink_stream) {
				dev_dbg(sd->dev,
					"routes %u and %u originate from same sink (%u/%u)\n",
					i, j, route->sink_pad,
					route->sink_stream);
				goto out;
			}

			/*
			 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
			 * at the same (source) stream.
			 */
			if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
			    route->source_pad == r->source_pad &&
			    route->source_stream == r->source_stream) {
				dev_dbg(sd->dev,
					"routes %u and %u end at same source (%u/%u)\n",
					i, j, route->source_pad,
					route->source_stream);
				goto out;
			}
		}
	}

	ret = 0;

out:
	kfree(remote_pads);
	return ret;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);

static int v4l2_subdev_enable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
					       u64 streams_mask)
{
	struct device *dev = sd->entity.graph_obj.mdev->dev;
	unsigned int i;
	int ret;

	/*
	 * The subdev doesn't implement pad-based stream enable, fall back
	 * on the .s_stream() operation. This can only be done for subdevs that
	 * have a single source pad, as sd->enabled_streams is global to the
	 * subdev.
	 */
	if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
		return -EOPNOTSUPP;

	for (i = 0; i < sd->entity.num_pads; ++i) {
		if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
			return -EOPNOTSUPP;
	}

	if (sd->enabled_streams & streams_mask) {
		dev_dbg(dev, "set of streams %#llx already enabled on %s:%u\n",
			streams_mask, sd->entity.name, pad);
		return -EALREADY;
	}

	/* Start streaming when the first streams are enabled. */
	if (!sd->enabled_streams) {
		ret = v4l2_subdev_call(sd, video, s_stream, 1);
		if (ret)
			return ret;
	}

	sd->enabled_streams |= streams_mask;

	return 0;
}

int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
			       u64 streams_mask)
{
	struct device *dev = sd->entity.graph_obj.mdev->dev;
	struct v4l2_subdev_state *state;
	u64 found_streams = 0;
	unsigned int i;
	int ret;

	/* A few basic sanity checks first. */
	if (pad >= sd->entity.num_pads)
		return -EINVAL;

	if (!streams_mask)
		return 0;

	/* Fallback on .s_stream() if .enable_streams() isn't available. */
	if (!sd->ops->pad || !sd->ops->pad->enable_streams)
		return v4l2_subdev_enable_streams_fallback(sd, pad,
							   streams_mask);

	state = v4l2_subdev_lock_and_get_active_state(sd);

	/*
	 * Verify that the requested streams exist and that they are not
	 * already enabled.
	 */
	for (i = 0; i < state->stream_configs.num_configs; ++i) {
		struct v4l2_subdev_stream_config *cfg =
			&state->stream_configs.configs[i];

		if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
			continue;

		found_streams |= BIT_ULL(cfg->stream);

		if (cfg->enabled) {
			dev_dbg(dev, "stream %u already enabled on %s:%u\n",
				cfg->stream, sd->entity.name, pad);
			ret = -EALREADY;
			goto done;
		}
	}

	if (found_streams != streams_mask) {
		dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
			streams_mask & ~found_streams, sd->entity.name, pad);
		ret = -EINVAL;
		goto done;
	}

	dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);

	/* Call the .enable_streams() operation. */
	ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
			       streams_mask);
	if (ret) {
		dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
			streams_mask, ret);
		goto done;
	}

	/* Mark the streams as enabled. */
	for (i = 0; i < state->stream_configs.num_configs; ++i) {
		struct v4l2_subdev_stream_config *cfg =
			&state->stream_configs.configs[i];

		if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
			cfg->enabled = true;
	}

done:
	v4l2_subdev_unlock_state(state);

	return ret;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);

static int v4l2_subdev_disable_streams_fallback(struct v4l2_subdev *sd, u32 pad,
						u64 streams_mask)
{
	struct device *dev = sd->entity.graph_obj.mdev->dev;
	unsigned int i;
	int ret;

	/*
	 * If the subdev doesn't implement pad-based stream enable, fall  back
	 * on the .s_stream() operation. This can only be done for subdevs that
	 * have a single source pad, as sd->enabled_streams is global to the
	 * subdev.
	 */
	if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
		return -EOPNOTSUPP;

	for (i = 0; i < sd->entity.num_pads; ++i) {
		if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE)
			return -EOPNOTSUPP;
	}

	if ((sd->enabled_streams & streams_mask) != streams_mask) {
		dev_dbg(dev, "set of streams %#llx already disabled on %s:%u\n",
			streams_mask, sd->entity.name, pad);
		return -EALREADY;
	}

	/* Stop streaming when the last streams are disabled. */
	if (!(sd->enabled_streams & ~streams_mask)) {
		ret = v4l2_subdev_call(sd, video, s_stream, 0);
		if (ret)
			return ret;
	}

	sd->enabled_streams &= ~streams_mask;

	return 0;
}

int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
				u64 streams_mask)
{
	struct device *dev = sd->entity.graph_obj.mdev->dev;
	struct v4l2_subdev_state *state;
	u64 found_streams = 0;
	unsigned int i;
	int ret;

	/* A few basic sanity checks first. */
	if (pad >= sd->entity.num_pads)
		return -EINVAL;

	if (!streams_mask)
		return 0;

	/* Fallback on .s_stream() if .disable_streams() isn't available. */
	if (!sd->ops->pad || !sd->ops->pad->disable_streams)
		return v4l2_subdev_disable_streams_fallback(sd, pad,
							    streams_mask);

	state = v4l2_subdev_lock_and_get_active_state(sd);

	/*
	 * Verify that the requested streams exist and that they are not
	 * already disabled.
	 */
	for (i = 0; i < state->stream_configs.num_configs; ++i) {
		struct v4l2_subdev_stream_config *cfg =
			&state->stream_configs.configs[i];

		if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
			continue;

		found_streams |= BIT_ULL(cfg->stream);

		if (!cfg->enabled) {
			dev_dbg(dev, "stream %u already disabled on %s:%u\n",
				cfg->stream, sd->entity.name, pad);
			ret = -EALREADY;
			goto done;
		}
	}

	if (found_streams != streams_mask) {
		dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
			streams_mask & ~found_streams, sd->entity.name, pad);
		ret = -EINVAL;
		goto done;
	}

	dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);

	/* Call the .disable_streams() operation. */
	ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
			       streams_mask);
	if (ret) {
		dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
			streams_mask, ret);
		goto done;
	}

	/* Mark the streams as disabled. */
	for (i = 0; i < state->stream_configs.num_configs; ++i) {
		struct v4l2_subdev_stream_config *cfg =
			&state->stream_configs.configs[i];

		if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
			cfg->enabled = false;
	}

done:
	v4l2_subdev_unlock_state(state);

	return ret;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);

int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
{
	struct v4l2_subdev_state *state;
	struct v4l2_subdev_route *route;
	struct media_pad *pad;
	u64 source_mask = 0;
	int pad_index = -1;

	/*
	 * Find the source pad. This helper is meant for subdevs that have a
	 * single source pad, so failures shouldn't happen, but catch them
	 * loudly nonetheless as they indicate a driver bug.
	 */
	media_entity_for_each_pad(&sd->entity, pad) {
		if (pad->flags & MEDIA_PAD_FL_SOURCE) {
			pad_index = pad->index;
			break;
		}
	}

	if (WARN_ON(pad_index == -1))
		return -EINVAL;

	/*
	 * As there's a single source pad, just collect all the source streams.
	 */
	state = v4l2_subdev_lock_and_get_active_state(sd);

	for_each_active_route(&state->routing, route)
		source_mask |= BIT_ULL(route->source_stream);

	v4l2_subdev_unlock_state(state);

	if (enable)
		return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
	else
		return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
}
EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);

#endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */

#endif /* CONFIG_MEDIA_CONTROLLER */

void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
{
	INIT_LIST_HEAD(&sd->list);
	BUG_ON(!ops);
	sd->ops = ops;
	sd->v4l2_dev = NULL;
	sd->flags = 0;
	sd->name[0] = '\0';
	sd->grp_id = 0;
	sd->dev_priv = NULL;
	sd->host_priv = NULL;
	sd->privacy_led = NULL;
	INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
#if defined(CONFIG_MEDIA_CONTROLLER)
	sd->entity.name = sd->name;
	sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
	sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
#endif
}
EXPORT_SYMBOL(v4l2_subdev_init);

void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
			      const struct v4l2_event *ev)
{
	v4l2_event_queue(sd->devnode, ev);
	v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
}
EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);

int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
{
#if IS_REACHABLE(CONFIG_LEDS_CLASS)
	sd->privacy_led = led_get(sd->dev, "privacy-led");
	if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
		return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
				     "getting privacy LED\n");

	if (!IS_ERR_OR_NULL(sd->privacy_led)) {
		mutex_lock(&sd->privacy_led->led_access);
		led_sysfs_disable(sd->privacy_led);
		led_trigger_remove(sd->privacy_led);
		led_set_brightness(sd->privacy_led, 0);
		mutex_unlock(&sd->privacy_led->led_access);
	}
#endif
	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);

void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
{
#if IS_REACHABLE(CONFIG_LEDS_CLASS)
	if (!IS_ERR_OR_NULL(sd->privacy_led)) {
		mutex_lock(&sd->privacy_led->led_access);
		led_sysfs_enable(sd->privacy_led);
		mutex_unlock(&sd->privacy_led->led_access);
		led_put(sd->privacy_led);
	}
#endif
}
EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);