vivid-vid-common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. .is_yuv = true,
  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. .is_yuv = true,
  56. .planes = 1,
  57. .buffers = 1,
  58. },
  59. {
  60. .fourcc = V4L2_PIX_FMT_YVYU,
  61. .vdownsampling = { 1 },
  62. .bit_depth = { 16 },
  63. .is_yuv = true,
  64. .planes = 1,
  65. .buffers = 1,
  66. },
  67. {
  68. .fourcc = V4L2_PIX_FMT_VYUY,
  69. .vdownsampling = { 1 },
  70. .bit_depth = { 16 },
  71. .is_yuv = true,
  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. .is_yuv = true,
  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. .is_yuv = true,
  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. .is_yuv = true,
  96. .planes = 3,
  97. .buffers = 1,
  98. },
  99. {
  100. .fourcc = V4L2_PIX_FMT_NV12,
  101. .vdownsampling = { 1, 2 },
  102. .bit_depth = { 8, 8 },
  103. .is_yuv = true,
  104. .planes = 2,
  105. .buffers = 1,
  106. },
  107. {
  108. .fourcc = V4L2_PIX_FMT_NV21,
  109. .vdownsampling = { 1, 2 },
  110. .bit_depth = { 8, 8 },
  111. .is_yuv = true,
  112. .planes = 2,
  113. .buffers = 1,
  114. },
  115. {
  116. .fourcc = V4L2_PIX_FMT_NV16,
  117. .vdownsampling = { 1, 1 },
  118. .bit_depth = { 8, 8 },
  119. .is_yuv = true,
  120. .planes = 2,
  121. .buffers = 1,
  122. },
  123. {
  124. .fourcc = V4L2_PIX_FMT_NV61,
  125. .vdownsampling = { 1, 1 },
  126. .bit_depth = { 8, 8 },
  127. .is_yuv = true,
  128. .planes = 2,
  129. .buffers = 1,
  130. },
  131. {
  132. .fourcc = V4L2_PIX_FMT_NV24,
  133. .vdownsampling = { 1, 1 },
  134. .bit_depth = { 8, 16 },
  135. .is_yuv = true,
  136. .planes = 2,
  137. .buffers = 1,
  138. },
  139. {
  140. .fourcc = V4L2_PIX_FMT_NV42,
  141. .vdownsampling = { 1, 1 },
  142. .bit_depth = { 8, 16 },
  143. .is_yuv = true,
  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. .is_yuv = true,
  183. .planes = 1,
  184. .buffers = 1,
  185. },
  186. {
  187. .fourcc = V4L2_PIX_FMT_Y16,
  188. .vdownsampling = { 1 },
  189. .bit_depth = { 16 },
  190. .is_yuv = true,
  191. .planes = 1,
  192. .buffers = 1,
  193. },
  194. {
  195. .fourcc = V4L2_PIX_FMT_Y16_BE,
  196. .vdownsampling = { 1 },
  197. .bit_depth = { 16 },
  198. .is_yuv = true,
  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. /* Multiplanar formats */
  444. {
  445. .fourcc = V4L2_PIX_FMT_NV16M,
  446. .vdownsampling = { 1, 1 },
  447. .bit_depth = { 8, 8 },
  448. .is_yuv = true,
  449. .planes = 2,
  450. .buffers = 2,
  451. .data_offset = { PLANE0_DATA_OFFSET, 0 },
  452. },
  453. {
  454. .fourcc = V4L2_PIX_FMT_NV61M,
  455. .vdownsampling = { 1, 1 },
  456. .bit_depth = { 8, 8 },
  457. .is_yuv = true,
  458. .planes = 2,
  459. .buffers = 2,
  460. .data_offset = { 0, PLANE0_DATA_OFFSET },
  461. },
  462. {
  463. .fourcc = V4L2_PIX_FMT_YUV420M,
  464. .vdownsampling = { 1, 2, 2 },
  465. .bit_depth = { 8, 4, 4 },
  466. .is_yuv = true,
  467. .planes = 3,
  468. .buffers = 3,
  469. },
  470. {
  471. .fourcc = V4L2_PIX_FMT_YVU420M,
  472. .vdownsampling = { 1, 2, 2 },
  473. .bit_depth = { 8, 4, 4 },
  474. .is_yuv = true,
  475. .planes = 3,
  476. .buffers = 3,
  477. },
  478. {
  479. .fourcc = V4L2_PIX_FMT_NV12M,
  480. .vdownsampling = { 1, 2 },
  481. .bit_depth = { 8, 8 },
  482. .is_yuv = true,
  483. .planes = 2,
  484. .buffers = 2,
  485. },
  486. {
  487. .fourcc = V4L2_PIX_FMT_NV21M,
  488. .vdownsampling = { 1, 2 },
  489. .bit_depth = { 8, 8 },
  490. .is_yuv = true,
  491. .planes = 2,
  492. .buffers = 2,
  493. },
  494. {
  495. .fourcc = V4L2_PIX_FMT_YUV422M,
  496. .vdownsampling = { 1, 1, 1 },
  497. .bit_depth = { 8, 4, 4 },
  498. .is_yuv = true,
  499. .planes = 3,
  500. .buffers = 3,
  501. },
  502. {
  503. .fourcc = V4L2_PIX_FMT_YVU422M,
  504. .vdownsampling = { 1, 1, 1 },
  505. .bit_depth = { 8, 4, 4 },
  506. .is_yuv = true,
  507. .planes = 3,
  508. .buffers = 3,
  509. },
  510. {
  511. .fourcc = V4L2_PIX_FMT_YUV444M,
  512. .vdownsampling = { 1, 1, 1 },
  513. .bit_depth = { 8, 8, 8 },
  514. .is_yuv = true,
  515. .planes = 3,
  516. .buffers = 3,
  517. },
  518. {
  519. .fourcc = V4L2_PIX_FMT_YVU444M,
  520. .vdownsampling = { 1, 1, 1 },
  521. .bit_depth = { 8, 8, 8 },
  522. .is_yuv = true,
  523. .planes = 3,
  524. .buffers = 3,
  525. },
  526. };
  527. /* There are this many multiplanar formats in the list */
  528. #define VIVID_MPLANAR_FORMATS 10
  529. const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
  530. {
  531. const struct vivid_fmt *fmt;
  532. unsigned k;
  533. for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
  534. fmt = &vivid_formats[k];
  535. if (fmt->fourcc == pixelformat)
  536. if (fmt->buffers == 1 || dev->multiplanar)
  537. return fmt;
  538. }
  539. return NULL;
  540. }
  541. bool vivid_vid_can_loop(struct vivid_dev *dev)
  542. {
  543. if (dev->src_rect.width != dev->sink_rect.width ||
  544. dev->src_rect.height != dev->sink_rect.height)
  545. return false;
  546. if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
  547. return false;
  548. if (dev->field_cap != dev->field_out)
  549. return false;
  550. /*
  551. * While this can be supported, it is just too much work
  552. * to actually implement.
  553. */
  554. if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
  555. dev->field_cap == V4L2_FIELD_SEQ_BT)
  556. return false;
  557. if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
  558. if (!(dev->std_cap & V4L2_STD_525_60) !=
  559. !(dev->std_out & V4L2_STD_525_60))
  560. return false;
  561. return true;
  562. }
  563. if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
  564. return true;
  565. return false;
  566. }
  567. void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
  568. {
  569. struct v4l2_event ev = {
  570. .type = V4L2_EVENT_SOURCE_CHANGE,
  571. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  572. };
  573. unsigned i;
  574. for (i = 0; i < dev->num_inputs; i++) {
  575. ev.id = i;
  576. if (dev->input_type[i] == type) {
  577. if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
  578. v4l2_event_queue(&dev->vid_cap_dev, &ev);
  579. if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
  580. v4l2_event_queue(&dev->vbi_cap_dev, &ev);
  581. }
  582. }
  583. }
  584. /*
  585. * Conversion function that converts a single-planar format to a
  586. * single-plane multiplanar format.
  587. */
  588. void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
  589. {
  590. struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
  591. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  592. const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
  593. bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
  594. memset(mp->reserved, 0, sizeof(mp->reserved));
  595. mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
  596. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  597. mp->width = pix->width;
  598. mp->height = pix->height;
  599. mp->pixelformat = pix->pixelformat;
  600. mp->field = pix->field;
  601. mp->colorspace = pix->colorspace;
  602. mp->xfer_func = pix->xfer_func;
  603. mp->ycbcr_enc = pix->ycbcr_enc;
  604. mp->quantization = pix->quantization;
  605. mp->num_planes = 1;
  606. mp->flags = pix->flags;
  607. ppix->sizeimage = pix->sizeimage;
  608. ppix->bytesperline = pix->bytesperline;
  609. memset(ppix->reserved, 0, sizeof(ppix->reserved));
  610. }
  611. int fmt_sp2mp_func(struct file *file, void *priv,
  612. struct v4l2_format *f, fmtfunc func)
  613. {
  614. struct v4l2_format fmt;
  615. struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
  616. struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
  617. struct v4l2_pix_format *pix = &f->fmt.pix;
  618. int ret;
  619. /* Converts to a mplane format */
  620. fmt_sp2mp(f, &fmt);
  621. /* Passes it to the generic mplane format function */
  622. ret = func(file, priv, &fmt);
  623. /* Copies back the mplane data to the single plane format */
  624. pix->width = mp->width;
  625. pix->height = mp->height;
  626. pix->pixelformat = mp->pixelformat;
  627. pix->field = mp->field;
  628. pix->colorspace = mp->colorspace;
  629. pix->xfer_func = mp->xfer_func;
  630. pix->ycbcr_enc = mp->ycbcr_enc;
  631. pix->quantization = mp->quantization;
  632. pix->sizeimage = ppix->sizeimage;
  633. pix->bytesperline = ppix->bytesperline;
  634. pix->flags = mp->flags;
  635. return ret;
  636. }
  637. int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
  638. {
  639. unsigned w = r->width;
  640. unsigned h = r->height;
  641. /* sanitize w and h in case someone passes ~0 as the value */
  642. w &= 0xffff;
  643. h &= 0xffff;
  644. if (!(flags & V4L2_SEL_FLAG_LE)) {
  645. w++;
  646. h++;
  647. if (w < 2)
  648. w = 2;
  649. if (h < 2)
  650. h = 2;
  651. }
  652. if (!(flags & V4L2_SEL_FLAG_GE)) {
  653. if (w > MAX_WIDTH)
  654. w = MAX_WIDTH;
  655. if (h > MAX_HEIGHT)
  656. h = MAX_HEIGHT;
  657. }
  658. w = w & ~1;
  659. h = h & ~1;
  660. if (w < 2 || h < 2)
  661. return -ERANGE;
  662. if (w > MAX_WIDTH || h > MAX_HEIGHT)
  663. return -ERANGE;
  664. if (r->top < 0)
  665. r->top = 0;
  666. if (r->left < 0)
  667. r->left = 0;
  668. /* sanitize left and top in case someone passes ~0 as the value */
  669. r->left &= 0xfffe;
  670. r->top &= 0xfffe;
  671. if (r->left + w > MAX_WIDTH)
  672. r->left = MAX_WIDTH - w;
  673. if (r->top + h > MAX_HEIGHT)
  674. r->top = MAX_HEIGHT - h;
  675. if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
  676. (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
  677. (r->width != w || r->height != h))
  678. return -ERANGE;
  679. r->width = w;
  680. r->height = h;
  681. return 0;
  682. }
  683. int vivid_enum_fmt_vid(struct file *file, void *priv,
  684. struct v4l2_fmtdesc *f)
  685. {
  686. struct vivid_dev *dev = video_drvdata(file);
  687. const struct vivid_fmt *fmt;
  688. if (f->index >= ARRAY_SIZE(vivid_formats) -
  689. (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
  690. return -EINVAL;
  691. fmt = &vivid_formats[f->index];
  692. f->pixelformat = fmt->fourcc;
  693. return 0;
  694. }
  695. int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
  696. struct v4l2_fmtdesc *f)
  697. {
  698. struct vivid_dev *dev = video_drvdata(file);
  699. if (!dev->multiplanar)
  700. return -ENOTTY;
  701. return vivid_enum_fmt_vid(file, priv, f);
  702. }
  703. int vidioc_enum_fmt_vid(struct file *file, void *priv,
  704. struct v4l2_fmtdesc *f)
  705. {
  706. struct vivid_dev *dev = video_drvdata(file);
  707. if (dev->multiplanar)
  708. return -ENOTTY;
  709. return vivid_enum_fmt_vid(file, priv, f);
  710. }
  711. int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  712. {
  713. struct vivid_dev *dev = video_drvdata(file);
  714. struct video_device *vdev = video_devdata(file);
  715. if (vdev->vfl_dir == VFL_DIR_RX) {
  716. if (!vivid_is_sdtv_cap(dev))
  717. return -ENODATA;
  718. *id = dev->std_cap;
  719. } else {
  720. if (!vivid_is_svid_out(dev))
  721. return -ENODATA;
  722. *id = dev->std_out;
  723. }
  724. return 0;
  725. }
  726. int vidioc_g_dv_timings(struct file *file, void *_fh,
  727. struct v4l2_dv_timings *timings)
  728. {
  729. struct vivid_dev *dev = video_drvdata(file);
  730. struct video_device *vdev = video_devdata(file);
  731. if (vdev->vfl_dir == VFL_DIR_RX) {
  732. if (!vivid_is_hdmi_cap(dev))
  733. return -ENODATA;
  734. *timings = dev->dv_timings_cap;
  735. } else {
  736. if (!vivid_is_hdmi_out(dev))
  737. return -ENODATA;
  738. *timings = dev->dv_timings_out;
  739. }
  740. return 0;
  741. }
  742. int vidioc_enum_dv_timings(struct file *file, void *_fh,
  743. struct v4l2_enum_dv_timings *timings)
  744. {
  745. struct vivid_dev *dev = video_drvdata(file);
  746. struct video_device *vdev = video_devdata(file);
  747. if (vdev->vfl_dir == VFL_DIR_RX) {
  748. if (!vivid_is_hdmi_cap(dev))
  749. return -ENODATA;
  750. } else {
  751. if (!vivid_is_hdmi_out(dev))
  752. return -ENODATA;
  753. }
  754. return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
  755. NULL, NULL);
  756. }
  757. int vidioc_dv_timings_cap(struct file *file, void *_fh,
  758. struct v4l2_dv_timings_cap *cap)
  759. {
  760. struct vivid_dev *dev = video_drvdata(file);
  761. struct video_device *vdev = video_devdata(file);
  762. if (vdev->vfl_dir == VFL_DIR_RX) {
  763. if (!vivid_is_hdmi_cap(dev))
  764. return -ENODATA;
  765. } else {
  766. if (!vivid_is_hdmi_out(dev))
  767. return -ENODATA;
  768. }
  769. *cap = vivid_dv_timings_cap;
  770. return 0;
  771. }
  772. int vidioc_g_edid(struct file *file, void *_fh,
  773. struct v4l2_edid *edid)
  774. {
  775. struct vivid_dev *dev = video_drvdata(file);
  776. struct video_device *vdev = video_devdata(file);
  777. struct cec_adapter *adap;
  778. memset(edid->reserved, 0, sizeof(edid->reserved));
  779. if (vdev->vfl_dir == VFL_DIR_RX) {
  780. if (edid->pad >= dev->num_inputs)
  781. return -EINVAL;
  782. if (dev->input_type[edid->pad] != HDMI)
  783. return -EINVAL;
  784. adap = dev->cec_rx_adap;
  785. } else {
  786. unsigned int bus_idx;
  787. if (edid->pad >= dev->num_outputs)
  788. return -EINVAL;
  789. if (dev->output_type[edid->pad] != HDMI)
  790. return -EINVAL;
  791. bus_idx = dev->cec_output2bus_map[edid->pad];
  792. adap = dev->cec_tx_adap[bus_idx];
  793. }
  794. if (edid->start_block == 0 && edid->blocks == 0) {
  795. edid->blocks = dev->edid_blocks;
  796. return 0;
  797. }
  798. if (dev->edid_blocks == 0)
  799. return -ENODATA;
  800. if (edid->start_block >= dev->edid_blocks)
  801. return -EINVAL;
  802. if (edid->start_block + edid->blocks > dev->edid_blocks)
  803. edid->blocks = dev->edid_blocks - edid->start_block;
  804. memcpy(edid->edid, dev->edid, edid->blocks * 128);
  805. cec_set_edid_phys_addr(edid->edid, edid->blocks * 128, adap->phys_addr);
  806. return 0;
  807. }