vivid-vid-common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vivid-vid-common.c - common video support functions.
  4. *
  5. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/videodev2.h>
  11. #include <linux/v4l2-dv-timings.h>
  12. #include <media/v4l2-common.h>
  13. #include <media/v4l2-event.h>
  14. #include <media/v4l2-dv-timings.h>
  15. #include "vivid-core.h"
  16. #include "vivid-vid-common.h"
  17. const struct v4l2_dv_timings_cap vivid_dv_timings_cap = {
  18. .type = V4L2_DV_BT_656_1120,
  19. /* keep this initialization for compatibility with GCC < 4.4.6 */
  20. .reserved = { 0 },
  21. V4L2_INIT_BT_TIMINGS(16, MAX_WIDTH, 16, MAX_HEIGHT, 14000000, 775000000,
  22. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  23. V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
  24. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED)
  25. };
  26. /* ------------------------------------------------------------------
  27. Basic structures
  28. ------------------------------------------------------------------*/
  29. struct vivid_fmt vivid_formats[] = {
  30. {
  31. .fourcc = V4L2_PIX_FMT_YUYV,
  32. .vdownsampling = { 1 },
  33. .bit_depth = { 16 },
  34. .color_enc = TGP_COLOR_ENC_YCBCR,
  35. .planes = 1,
  36. .buffers = 1,
  37. .data_offset = { PLANE0_DATA_OFFSET },
  38. },
  39. {
  40. .fourcc = V4L2_PIX_FMT_UYVY,
  41. .vdownsampling = { 1 },
  42. .bit_depth = { 16 },
  43. .color_enc = TGP_COLOR_ENC_YCBCR,
  44. .planes = 1,
  45. .buffers = 1,
  46. },
  47. {
  48. .fourcc = V4L2_PIX_FMT_YVYU,
  49. .vdownsampling = { 1 },
  50. .bit_depth = { 16 },
  51. .color_enc = TGP_COLOR_ENC_YCBCR,
  52. .planes = 1,
  53. .buffers = 1,
  54. },
  55. {
  56. .fourcc = V4L2_PIX_FMT_VYUY,
  57. .vdownsampling = { 1 },
  58. .bit_depth = { 16 },
  59. .color_enc = TGP_COLOR_ENC_YCBCR,
  60. .planes = 1,
  61. .buffers = 1,
  62. },
  63. {
  64. .fourcc = V4L2_PIX_FMT_YUV422P,
  65. .vdownsampling = { 1, 1, 1 },
  66. .bit_depth = { 8, 4, 4 },
  67. .color_enc = TGP_COLOR_ENC_YCBCR,
  68. .planes = 3,
  69. .buffers = 1,
  70. },
  71. {
  72. .fourcc = V4L2_PIX_FMT_YUV420,
  73. .vdownsampling = { 1, 2, 2 },
  74. .bit_depth = { 8, 4, 4 },
  75. .color_enc = TGP_COLOR_ENC_YCBCR,
  76. .planes = 3,
  77. .buffers = 1,
  78. },
  79. {
  80. .fourcc = V4L2_PIX_FMT_YVU420,
  81. .vdownsampling = { 1, 2, 2 },
  82. .bit_depth = { 8, 4, 4 },
  83. .color_enc = TGP_COLOR_ENC_YCBCR,
  84. .planes = 3,
  85. .buffers = 1,
  86. },
  87. {
  88. .fourcc = V4L2_PIX_FMT_NV12,
  89. .vdownsampling = { 1, 2 },
  90. .bit_depth = { 8, 8 },
  91. .color_enc = TGP_COLOR_ENC_YCBCR,
  92. .planes = 2,
  93. .buffers = 1,
  94. },
  95. {
  96. .fourcc = V4L2_PIX_FMT_NV21,
  97. .vdownsampling = { 1, 2 },
  98. .bit_depth = { 8, 8 },
  99. .color_enc = TGP_COLOR_ENC_YCBCR,
  100. .planes = 2,
  101. .buffers = 1,
  102. },
  103. {
  104. .fourcc = V4L2_PIX_FMT_NV16,
  105. .vdownsampling = { 1, 1 },
  106. .bit_depth = { 8, 8 },
  107. .color_enc = TGP_COLOR_ENC_YCBCR,
  108. .planes = 2,
  109. .buffers = 1,
  110. },
  111. {
  112. .fourcc = V4L2_PIX_FMT_NV61,
  113. .vdownsampling = { 1, 1 },
  114. .bit_depth = { 8, 8 },
  115. .color_enc = TGP_COLOR_ENC_YCBCR,
  116. .planes = 2,
  117. .buffers = 1,
  118. },
  119. {
  120. .fourcc = V4L2_PIX_FMT_NV24,
  121. .vdownsampling = { 1, 1 },
  122. .bit_depth = { 8, 16 },
  123. .color_enc = TGP_COLOR_ENC_YCBCR,
  124. .planes = 2,
  125. .buffers = 1,
  126. },
  127. {
  128. .fourcc = V4L2_PIX_FMT_NV42,
  129. .vdownsampling = { 1, 1 },
  130. .bit_depth = { 8, 16 },
  131. .color_enc = TGP_COLOR_ENC_YCBCR,
  132. .planes = 2,
  133. .buffers = 1,
  134. },
  135. {
  136. .fourcc = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
  137. .vdownsampling = { 1 },
  138. .bit_depth = { 16 },
  139. .planes = 1,
  140. .buffers = 1,
  141. .alpha_mask = 0x8000,
  142. },
  143. {
  144. .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
  145. .vdownsampling = { 1 },
  146. .bit_depth = { 16 },
  147. .planes = 1,
  148. .buffers = 1,
  149. },
  150. {
  151. .fourcc = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
  152. .vdownsampling = { 1 },
  153. .bit_depth = { 16 },
  154. .planes = 1,
  155. .buffers = 1,
  156. .alpha_mask = 0xf000,
  157. },
  158. {
  159. .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
  160. .vdownsampling = { 1 },
  161. .bit_depth = { 32 },
  162. .planes = 1,
  163. .buffers = 1,
  164. .alpha_mask = 0x000000ff,
  165. },
  166. {
  167. .fourcc = V4L2_PIX_FMT_GREY,
  168. .vdownsampling = { 1 },
  169. .bit_depth = { 8 },
  170. .color_enc = TGP_COLOR_ENC_LUMA,
  171. .planes = 1,
  172. .buffers = 1,
  173. },
  174. {
  175. .fourcc = V4L2_PIX_FMT_Y10,
  176. .vdownsampling = { 1 },
  177. .bit_depth = { 16 },
  178. .color_enc = TGP_COLOR_ENC_LUMA,
  179. .planes = 1,
  180. .buffers = 1,
  181. },
  182. {
  183. .fourcc = V4L2_PIX_FMT_Y12,
  184. .vdownsampling = { 1 },
  185. .bit_depth = { 16 },
  186. .color_enc = TGP_COLOR_ENC_LUMA,
  187. .planes = 1,
  188. .buffers = 1,
  189. },
  190. {
  191. .fourcc = V4L2_PIX_FMT_Y16,
  192. .vdownsampling = { 1 },
  193. .bit_depth = { 16 },
  194. .color_enc = TGP_COLOR_ENC_LUMA,
  195. .planes = 1,
  196. .buffers = 1,
  197. },
  198. {
  199. .fourcc = V4L2_PIX_FMT_Y16_BE,
  200. .vdownsampling = { 1 },
  201. .bit_depth = { 16 },
  202. .color_enc = TGP_COLOR_ENC_LUMA,
  203. .planes = 1,
  204. .buffers = 1,
  205. },
  206. {
  207. .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
  208. .vdownsampling = { 1 },
  209. .bit_depth = { 8 },
  210. .planes = 1,
  211. .buffers = 1,
  212. },
  213. {
  214. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  215. .vdownsampling = { 1 },
  216. .bit_depth = { 16 },
  217. .planes = 1,
  218. .buffers = 1,
  219. .can_do_overlay = true,
  220. },
  221. {
  222. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  223. .vdownsampling = { 1 },
  224. .bit_depth = { 16 },
  225. .planes = 1,
  226. .buffers = 1,
  227. .can_do_overlay = true,
  228. },
  229. {
  230. .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
  231. .vdownsampling = { 1 },
  232. .bit_depth = { 16 },
  233. .planes = 1,
  234. .buffers = 1,
  235. },
  236. {
  237. .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
  238. .vdownsampling = { 1 },
  239. .bit_depth = { 16 },
  240. .planes = 1,
  241. .buffers = 1,
  242. },
  243. {
  244. .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
  245. .vdownsampling = { 1 },
  246. .bit_depth = { 16 },
  247. .planes = 1,
  248. .buffers = 1,
  249. .alpha_mask = 0x00f0,
  250. },
  251. {
  252. .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
  253. .vdownsampling = { 1 },
  254. .bit_depth = { 16 },
  255. .planes = 1,
  256. .buffers = 1,
  257. .can_do_overlay = true,
  258. },
  259. {
  260. .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
  261. .vdownsampling = { 1 },
  262. .bit_depth = { 16 },
  263. .planes = 1,
  264. .buffers = 1,
  265. .can_do_overlay = true,
  266. },
  267. {
  268. .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
  269. .vdownsampling = { 1 },
  270. .bit_depth = { 16 },
  271. .planes = 1,
  272. .buffers = 1,
  273. .can_do_overlay = true,
  274. .alpha_mask = 0x8000,
  275. },
  276. {
  277. .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
  278. .vdownsampling = { 1 },
  279. .bit_depth = { 16 },
  280. .planes = 1,
  281. .buffers = 1,
  282. },
  283. {
  284. .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
  285. .vdownsampling = { 1 },
  286. .bit_depth = { 16 },
  287. .planes = 1,
  288. .buffers = 1,
  289. },
  290. {
  291. .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
  292. .vdownsampling = { 1 },
  293. .bit_depth = { 16 },
  294. .planes = 1,
  295. .buffers = 1,
  296. .alpha_mask = 0x0080,
  297. },
  298. {
  299. .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
  300. .vdownsampling = { 1 },
  301. .bit_depth = { 24 },
  302. .planes = 1,
  303. .buffers = 1,
  304. },
  305. {
  306. .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
  307. .vdownsampling = { 1 },
  308. .bit_depth = { 24 },
  309. .planes = 1,
  310. .buffers = 1,
  311. },
  312. {
  313. .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
  314. .vdownsampling = { 1 },
  315. .bit_depth = { 32 },
  316. .planes = 1,
  317. .buffers = 1,
  318. },
  319. {
  320. .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
  321. .vdownsampling = { 1 },
  322. .bit_depth = { 32 },
  323. .planes = 1,
  324. .buffers = 1,
  325. },
  326. {
  327. .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
  328. .vdownsampling = { 1 },
  329. .bit_depth = { 32 },
  330. .planes = 1,
  331. .buffers = 1,
  332. },
  333. {
  334. .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
  335. .vdownsampling = { 1 },
  336. .bit_depth = { 32 },
  337. .planes = 1,
  338. .buffers = 1,
  339. },
  340. {
  341. .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
  342. .vdownsampling = { 1 },
  343. .bit_depth = { 32 },
  344. .planes = 1,
  345. .buffers = 1,
  346. },
  347. {
  348. .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
  349. .vdownsampling = { 1 },
  350. .bit_depth = { 32 },
  351. .planes = 1,
  352. .buffers = 1,
  353. .alpha_mask = 0x000000ff,
  354. },
  355. {
  356. .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
  357. .vdownsampling = { 1 },
  358. .bit_depth = { 32 },
  359. .planes = 1,
  360. .buffers = 1,
  361. .alpha_mask = 0xff000000,
  362. },
  363. {
  364. .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
  365. .vdownsampling = { 1 },
  366. .bit_depth = { 8 },
  367. .planes = 1,
  368. .buffers = 1,
  369. },
  370. {
  371. .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
  372. .vdownsampling = { 1 },
  373. .bit_depth = { 8 },
  374. .planes = 1,
  375. .buffers = 1,
  376. },
  377. {
  378. .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
  379. .vdownsampling = { 1 },
  380. .bit_depth = { 8 },
  381. .planes = 1,
  382. .buffers = 1,
  383. },
  384. {
  385. .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
  386. .vdownsampling = { 1 },
  387. .bit_depth = { 8 },
  388. .planes = 1,
  389. .buffers = 1,
  390. },
  391. {
  392. .fourcc = V4L2_PIX_FMT_SBGGR10, /* Bayer BG/GR */
  393. .vdownsampling = { 1 },
  394. .bit_depth = { 16 },
  395. .planes = 1,
  396. .buffers = 1,
  397. },
  398. {
  399. .fourcc = V4L2_PIX_FMT_SGBRG10, /* Bayer GB/RG */
  400. .vdownsampling = { 1 },
  401. .bit_depth = { 16 },
  402. .planes = 1,
  403. .buffers = 1,
  404. },
  405. {
  406. .fourcc = V4L2_PIX_FMT_SGRBG10, /* Bayer GR/BG */
  407. .vdownsampling = { 1 },
  408. .bit_depth = { 16 },
  409. .planes = 1,
  410. .buffers = 1,
  411. },
  412. {
  413. .fourcc = V4L2_PIX_FMT_SRGGB10, /* Bayer RG/GB */
  414. .vdownsampling = { 1 },
  415. .bit_depth = { 16 },
  416. .planes = 1,
  417. .buffers = 1,
  418. },
  419. {
  420. .fourcc = V4L2_PIX_FMT_SBGGR12, /* Bayer BG/GR */
  421. .vdownsampling = { 1 },
  422. .bit_depth = { 16 },
  423. .planes = 1,
  424. .buffers = 1,
  425. },
  426. {
  427. .fourcc = V4L2_PIX_FMT_SGBRG12, /* Bayer GB/RG */
  428. .vdownsampling = { 1 },
  429. .bit_depth = { 16 },
  430. .planes = 1,
  431. .buffers = 1,
  432. },
  433. {
  434. .fourcc = V4L2_PIX_FMT_SGRBG12, /* Bayer GR/BG */
  435. .vdownsampling = { 1 },
  436. .bit_depth = { 16 },
  437. .planes = 1,
  438. .buffers = 1,
  439. },
  440. {
  441. .fourcc = V4L2_PIX_FMT_SRGGB12, /* Bayer RG/GB */
  442. .vdownsampling = { 1 },
  443. .bit_depth = { 16 },
  444. .planes = 1,
  445. .buffers = 1,
  446. },
  447. {
  448. .fourcc = V4L2_PIX_FMT_HSV24, /* HSV 24bits */
  449. .color_enc = TGP_COLOR_ENC_HSV,
  450. .vdownsampling = { 1 },
  451. .bit_depth = { 24 },
  452. .planes = 1,
  453. .buffers = 1,
  454. },
  455. {
  456. .fourcc = V4L2_PIX_FMT_HSV32, /* HSV 32bits */
  457. .color_enc = TGP_COLOR_ENC_HSV,
  458. .vdownsampling = { 1 },
  459. .bit_depth = { 32 },
  460. .planes = 1,
  461. .buffers = 1,
  462. },
  463. /* Multiplanar formats */
  464. {
  465. .fourcc = V4L2_PIX_FMT_NV16M,
  466. .vdownsampling = { 1, 1 },
  467. .bit_depth = { 8, 8 },
  468. .color_enc = TGP_COLOR_ENC_YCBCR,
  469. .planes = 2,
  470. .buffers = 2,
  471. .data_offset = { PLANE0_DATA_OFFSET, 0 },
  472. },
  473. {
  474. .fourcc = V4L2_PIX_FMT_NV61M,
  475. .vdownsampling = { 1, 1 },
  476. .bit_depth = { 8, 8 },
  477. .color_enc = TGP_COLOR_ENC_YCBCR,
  478. .planes = 2,
  479. .buffers = 2,
  480. .data_offset = { 0, PLANE0_DATA_OFFSET },
  481. },
  482. {
  483. .fourcc = V4L2_PIX_FMT_YUV420M,
  484. .vdownsampling = { 1, 2, 2 },
  485. .bit_depth = { 8, 4, 4 },
  486. .color_enc = TGP_COLOR_ENC_YCBCR,
  487. .planes = 3,
  488. .buffers = 3,
  489. },
  490. {
  491. .fourcc = V4L2_PIX_FMT_YVU420M,
  492. .vdownsampling = { 1, 2, 2 },
  493. .bit_depth = { 8, 4, 4 },
  494. .color_enc = TGP_COLOR_ENC_YCBCR,
  495. .planes = 3,
  496. .buffers = 3,
  497. },
  498. {
  499. .fourcc = V4L2_PIX_FMT_NV12M,
  500. .vdownsampling = { 1, 2 },
  501. .bit_depth = { 8, 8 },
  502. .color_enc = TGP_COLOR_ENC_YCBCR,
  503. .planes = 2,
  504. .buffers = 2,
  505. },
  506. {
  507. .fourcc = V4L2_PIX_FMT_NV21M,
  508. .vdownsampling = { 1, 2 },
  509. .bit_depth = { 8, 8 },
  510. .color_enc = TGP_COLOR_ENC_YCBCR,
  511. .planes = 2,
  512. .buffers = 2,
  513. },
  514. {
  515. .fourcc = V4L2_PIX_FMT_YUV422M,
  516. .vdownsampling = { 1, 1, 1 },
  517. .bit_depth = { 8, 4, 4 },
  518. .color_enc = TGP_COLOR_ENC_YCBCR,
  519. .planes = 3,
  520. .buffers = 3,
  521. },
  522. {
  523. .fourcc = V4L2_PIX_FMT_YVU422M,
  524. .vdownsampling = { 1, 1, 1 },
  525. .bit_depth = { 8, 4, 4 },
  526. .color_enc = TGP_COLOR_ENC_YCBCR,
  527. .planes = 3,
  528. .buffers = 3,
  529. },
  530. {
  531. .fourcc = V4L2_PIX_FMT_YUV444M,
  532. .vdownsampling = { 1, 1, 1 },
  533. .bit_depth = { 8, 8, 8 },
  534. .color_enc = TGP_COLOR_ENC_YCBCR,
  535. .planes = 3,
  536. .buffers = 3,
  537. },
  538. {
  539. .fourcc = V4L2_PIX_FMT_YVU444M,
  540. .vdownsampling = { 1, 1, 1 },
  541. .bit_depth = { 8, 8, 8 },
  542. .color_enc = TGP_COLOR_ENC_YCBCR,
  543. .planes = 3,
  544. .buffers = 3,
  545. },
  546. };
  547. /* There are this many multiplanar formats in the list */
  548. #define VIVID_MPLANAR_FORMATS 10
  549. const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
  550. {
  551. const struct vivid_fmt *fmt;
  552. unsigned k;
  553. for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
  554. fmt = &vivid_formats[k];
  555. if (fmt->fourcc == pixelformat)
  556. if (fmt->buffers == 1 || dev->multiplanar)
  557. return fmt;
  558. }
  559. return NULL;
  560. }
  561. bool vivid_vid_can_loop(struct vivid_dev *dev)
  562. {
  563. if (dev->src_rect.width != dev->sink_rect.width ||
  564. dev->src_rect.height != dev->sink_rect.height)
  565. return false;
  566. if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
  567. return false;
  568. if (dev->field_cap != dev->field_out)
  569. return false;
  570. /*
  571. * While this can be supported, it is just too much work
  572. * to actually implement.
  573. */
  574. if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
  575. dev->field_cap == V4L2_FIELD_SEQ_BT)
  576. return false;
  577. if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
  578. if (!(dev->std_cap & V4L2_STD_525_60) !=
  579. !(dev->std_out & V4L2_STD_525_60))
  580. return false;
  581. return true;
  582. }
  583. if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
  584. return true;
  585. return false;
  586. }
  587. void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
  588. {
  589. struct v4l2_event ev = {
  590. .type = V4L2_EVENT_SOURCE_CHANGE,
  591. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  592. };
  593. unsigned i;
  594. for (i = 0; i < dev->num_inputs; i++) {
  595. ev.id = i;
  596. if (dev->input_type[i] == type) {
  597. if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
  598. v4l2_event_queue(&dev->vid_cap_dev, &ev);
  599. if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
  600. v4l2_event_queue(&dev->vbi_cap_dev, &ev);
  601. }
  602. }
  603. }
  604. /*
  605. * Conversion function that converts a single-planar format to a
  606. * single-plane multiplanar format.
  607. */
  608. void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
  609. {
  610. struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
  611. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  612. const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
  613. bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
  614. memset(mp->reserved, 0, sizeof(mp->reserved));
  615. mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  616. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  617. mp->width = pix->width;
  618. mp->height = pix->height;
  619. mp->pixelformat = pix->pixelformat;
  620. mp->field = pix->field;
  621. mp->colorspace = pix->colorspace;
  622. mp->xfer_func = pix->xfer_func;
  623. /* Also copies hsv_enc */
  624. mp->ycbcr_enc = pix->ycbcr_enc;
  625. mp->quantization = pix->quantization;
  626. mp->num_planes = 1;
  627. mp->flags = pix->flags;
  628. ppix->sizeimage = pix->sizeimage;
  629. ppix->bytesperline = pix->bytesperline;
  630. memset(ppix->reserved, 0, sizeof(ppix->reserved));
  631. }
  632. int fmt_sp2mp_func(struct file *file, void *priv,
  633. struct v4l2_format *f, fmtfunc func)
  634. {
  635. struct v4l2_format fmt;
  636. struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
  637. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  638. struct v4l2_pix_format *pix = &f->fmt.pix;
  639. int ret;
  640. /* Converts to a mplane format */
  641. fmt_sp2mp(f, &fmt);
  642. /* Passes it to the generic mplane format function */
  643. ret = func(file, priv, &fmt);
  644. /* Copies back the mplane data to the single plane format */
  645. pix->width = mp->width;
  646. pix->height = mp->height;
  647. pix->pixelformat = mp->pixelformat;
  648. pix->field = mp->field;
  649. pix->colorspace = mp->colorspace;
  650. pix->xfer_func = mp->xfer_func;
  651. /* Also copies hsv_enc */
  652. pix->ycbcr_enc = mp->ycbcr_enc;
  653. pix->quantization = mp->quantization;
  654. pix->sizeimage = ppix->sizeimage;
  655. pix->bytesperline = ppix->bytesperline;
  656. pix->flags = mp->flags;
  657. return ret;
  658. }
  659. int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
  660. {
  661. unsigned w = r->width;
  662. unsigned h = r->height;
  663. /* sanitize w and h in case someone passes ~0 as the value */
  664. w &= 0xffff;
  665. h &= 0xffff;
  666. if (!(flags & V4L2_SEL_FLAG_LE)) {
  667. w++;
  668. h++;
  669. if (w < 2)
  670. w = 2;
  671. if (h < 2)
  672. h = 2;
  673. }
  674. if (!(flags & V4L2_SEL_FLAG_GE)) {
  675. if (w > MAX_WIDTH)
  676. w = MAX_WIDTH;
  677. if (h > MAX_HEIGHT)
  678. h = MAX_HEIGHT;
  679. }
  680. w = w & ~1;
  681. h = h & ~1;
  682. if (w < 2 || h < 2)
  683. return -ERANGE;
  684. if (w > MAX_WIDTH || h > MAX_HEIGHT)
  685. return -ERANGE;
  686. if (r->top < 0)
  687. r->top = 0;
  688. if (r->left < 0)
  689. r->left = 0;
  690. /* sanitize left and top in case someone passes ~0 as the value */
  691. r->left &= 0xfffe;
  692. r->top &= 0xfffe;
  693. if (r->left + w > MAX_WIDTH)
  694. r->left = MAX_WIDTH - w;
  695. if (r->top + h > MAX_HEIGHT)
  696. r->top = MAX_HEIGHT - h;
  697. if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
  698. (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
  699. (r->width != w || r->height != h))
  700. return -ERANGE;
  701. r->width = w;
  702. r->height = h;
  703. return 0;
  704. }
  705. int vivid_enum_fmt_vid(struct file *file, void *priv,
  706. struct v4l2_fmtdesc *f)
  707. {
  708. struct vivid_dev *dev = video_drvdata(file);
  709. const struct vivid_fmt *fmt;
  710. if (f->index >= ARRAY_SIZE(vivid_formats) -
  711. (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
  712. return -EINVAL;
  713. fmt = &vivid_formats[f->index];
  714. f->pixelformat = fmt->fourcc;
  715. return 0;
  716. }
  717. int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
  718. struct v4l2_fmtdesc *f)
  719. {
  720. struct vivid_dev *dev = video_drvdata(file);
  721. if (!dev->multiplanar)
  722. return -ENOTTY;
  723. return vivid_enum_fmt_vid(file, priv, f);
  724. }
  725. int vidioc_enum_fmt_vid(struct file *file, void *priv,
  726. struct v4l2_fmtdesc *f)
  727. {
  728. struct vivid_dev *dev = video_drvdata(file);
  729. if (dev->multiplanar)
  730. return -ENOTTY;
  731. return vivid_enum_fmt_vid(file, priv, f);
  732. }
  733. int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  734. {
  735. struct vivid_dev *dev = video_drvdata(file);
  736. struct video_device *vdev = video_devdata(file);
  737. if (vdev->vfl_dir == VFL_DIR_RX) {
  738. if (!vivid_is_sdtv_cap(dev))
  739. return -ENODATA;
  740. *id = dev->std_cap;
  741. } else {
  742. if (!vivid_is_svid_out(dev))
  743. return -ENODATA;
  744. *id = dev->std_out;
  745. }
  746. return 0;
  747. }
  748. int vidioc_g_dv_timings(struct file *file, void *_fh,
  749. struct v4l2_dv_timings *timings)
  750. {
  751. struct vivid_dev *dev = video_drvdata(file);
  752. struct video_device *vdev = video_devdata(file);
  753. if (vdev->vfl_dir == VFL_DIR_RX) {
  754. if (!vivid_is_hdmi_cap(dev))
  755. return -ENODATA;
  756. *timings = dev->dv_timings_cap;
  757. } else {
  758. if (!vivid_is_hdmi_out(dev))
  759. return -ENODATA;
  760. *timings = dev->dv_timings_out;
  761. }
  762. return 0;
  763. }
  764. int vidioc_enum_dv_timings(struct file *file, void *_fh,
  765. struct v4l2_enum_dv_timings *timings)
  766. {
  767. struct vivid_dev *dev = video_drvdata(file);
  768. struct video_device *vdev = video_devdata(file);
  769. if (vdev->vfl_dir == VFL_DIR_RX) {
  770. if (!vivid_is_hdmi_cap(dev))
  771. return -ENODATA;
  772. } else {
  773. if (!vivid_is_hdmi_out(dev))
  774. return -ENODATA;
  775. }
  776. return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
  777. NULL, NULL);
  778. }
  779. int vidioc_dv_timings_cap(struct file *file, void *_fh,
  780. struct v4l2_dv_timings_cap *cap)
  781. {
  782. struct vivid_dev *dev = video_drvdata(file);
  783. struct video_device *vdev = video_devdata(file);
  784. if (vdev->vfl_dir == VFL_DIR_RX) {
  785. if (!vivid_is_hdmi_cap(dev))
  786. return -ENODATA;
  787. } else {
  788. if (!vivid_is_hdmi_out(dev))
  789. return -ENODATA;
  790. }
  791. *cap = vivid_dv_timings_cap;
  792. return 0;
  793. }
  794. int vidioc_g_edid(struct file *file, void *_fh,
  795. struct v4l2_edid *edid)
  796. {
  797. struct vivid_dev *dev = video_drvdata(file);
  798. struct video_device *vdev = video_devdata(file);
  799. struct cec_adapter *adap;
  800. memset(edid->reserved, 0, sizeof(edid->reserved));
  801. if (vdev->vfl_dir == VFL_DIR_RX) {
  802. if (edid->pad >= dev->num_inputs)
  803. return -EINVAL;
  804. if (dev->input_type[edid->pad] != HDMI)
  805. return -EINVAL;
  806. adap = dev->cec_rx_adap;
  807. } else {
  808. unsigned int bus_idx;
  809. if (edid->pad >= dev->num_outputs)
  810. return -EINVAL;
  811. if (dev->output_type[edid->pad] != HDMI)
  812. return -EINVAL;
  813. bus_idx = dev->cec_output2bus_map[edid->pad];
  814. adap = dev->cec_tx_adap[bus_idx];
  815. }
  816. if (edid->start_block == 0 && edid->blocks == 0) {
  817. edid->blocks = dev->edid_blocks;
  818. return 0;
  819. }
  820. if (dev->edid_blocks == 0)
  821. return -ENODATA;
  822. if (edid->start_block >= dev->edid_blocks)
  823. return -EINVAL;
  824. if (edid->blocks > dev->edid_blocks - edid->start_block)
  825. edid->blocks = dev->edid_blocks - edid->start_block;
  826. if (adap)
  827. v4l2_set_edid_phys_addr(dev->edid, dev->edid_blocks * 128, adap->phys_addr);
  828. memcpy(edid->edid, dev->edid + edid->start_block * 128, edid->blocks * 128);
  829. return 0;
  830. }