vivid-vid-common.c 20 KB

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