solo6x10-v4l2-enc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/kthread.h>
  23. #include <linux/freezer.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-event.h>
  27. #include <media/videobuf2-dma-sg.h>
  28. #include "solo6x10.h"
  29. #include "solo6x10-tw28.h"
  30. #include "solo6x10-jpeg.h"
  31. #define MIN_VID_BUFFERS 2
  32. #define FRAME_BUF_SIZE (400 * 1024)
  33. #define MP4_QS 16
  34. #define DMA_ALIGN 4096
  35. /* 6010 M4V */
  36. static u8 vop_6010_ntsc_d1[] = {
  37. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  38. 0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
  39. 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  40. 0x1f, 0x4c, 0x58, 0x10, 0xf0, 0x71, 0x18, 0x3f,
  41. };
  42. static u8 vop_6010_ntsc_cif[] = {
  43. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  44. 0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
  45. 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  46. 0x1f, 0x4c, 0x2c, 0x10, 0x78, 0x51, 0x18, 0x3f,
  47. };
  48. static u8 vop_6010_pal_d1[] = {
  49. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  50. 0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
  51. 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  52. 0x1f, 0x4c, 0x58, 0x11, 0x20, 0x71, 0x18, 0x3f,
  53. };
  54. static u8 vop_6010_pal_cif[] = {
  55. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  56. 0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
  57. 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  58. 0x1f, 0x4c, 0x2c, 0x10, 0x90, 0x51, 0x18, 0x3f,
  59. };
  60. /* 6110 h.264 */
  61. static u8 vop_6110_ntsc_d1[] = {
  62. 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  63. 0x9a, 0x74, 0x05, 0x81, 0xec, 0x80, 0x00, 0x00,
  64. 0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
  65. };
  66. static u8 vop_6110_ntsc_cif[] = {
  67. 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  68. 0x9a, 0x74, 0x0b, 0x0f, 0xc8, 0x00, 0x00, 0x00,
  69. 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
  70. };
  71. static u8 vop_6110_pal_d1[] = {
  72. 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  73. 0x9a, 0x74, 0x05, 0x80, 0x93, 0x20, 0x00, 0x00,
  74. 0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
  75. };
  76. static u8 vop_6110_pal_cif[] = {
  77. 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  78. 0x9a, 0x74, 0x0b, 0x04, 0xb2, 0x00, 0x00, 0x00,
  79. 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
  80. };
  81. typedef __le32 vop_header[16];
  82. struct solo_enc_buf {
  83. enum solo_enc_types type;
  84. const vop_header *vh;
  85. int motion;
  86. };
  87. static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
  88. {
  89. struct solo_dev *solo_dev = solo_enc->solo_dev;
  90. return (solo_dev->motion_mask >> solo_enc->ch) & 1;
  91. }
  92. static int solo_motion_detected(struct solo_enc_dev *solo_enc)
  93. {
  94. struct solo_dev *solo_dev = solo_enc->solo_dev;
  95. unsigned long flags;
  96. u32 ch_mask = 1 << solo_enc->ch;
  97. int ret = 0;
  98. spin_lock_irqsave(&solo_enc->motion_lock, flags);
  99. if (solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS) & ch_mask) {
  100. solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, ch_mask);
  101. ret = 1;
  102. }
  103. spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
  104. return ret;
  105. }
  106. static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
  107. {
  108. struct solo_dev *solo_dev = solo_enc->solo_dev;
  109. u32 mask = 1 << solo_enc->ch;
  110. unsigned long flags;
  111. spin_lock_irqsave(&solo_enc->motion_lock, flags);
  112. if (on)
  113. solo_dev->motion_mask |= mask;
  114. else
  115. solo_dev->motion_mask &= ~mask;
  116. solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, mask);
  117. solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
  118. SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
  119. (SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
  120. spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
  121. }
  122. void solo_update_mode(struct solo_enc_dev *solo_enc)
  123. {
  124. struct solo_dev *solo_dev = solo_enc->solo_dev;
  125. int vop_len;
  126. u8 *vop;
  127. solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
  128. solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
  129. if (solo_enc->mode == SOLO_ENC_MODE_CIF) {
  130. solo_enc->width = solo_dev->video_hsize >> 1;
  131. solo_enc->height = solo_dev->video_vsize;
  132. if (solo_dev->type == SOLO_DEV_6110) {
  133. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
  134. vop = vop_6110_ntsc_cif;
  135. vop_len = sizeof(vop_6110_ntsc_cif);
  136. } else {
  137. vop = vop_6110_pal_cif;
  138. vop_len = sizeof(vop_6110_pal_cif);
  139. }
  140. } else {
  141. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
  142. vop = vop_6010_ntsc_cif;
  143. vop_len = sizeof(vop_6010_ntsc_cif);
  144. } else {
  145. vop = vop_6010_pal_cif;
  146. vop_len = sizeof(vop_6010_pal_cif);
  147. }
  148. }
  149. } else {
  150. solo_enc->width = solo_dev->video_hsize;
  151. solo_enc->height = solo_dev->video_vsize << 1;
  152. solo_enc->bw_weight <<= 2;
  153. if (solo_dev->type == SOLO_DEV_6110) {
  154. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
  155. vop = vop_6110_ntsc_d1;
  156. vop_len = sizeof(vop_6110_ntsc_d1);
  157. } else {
  158. vop = vop_6110_pal_d1;
  159. vop_len = sizeof(vop_6110_pal_d1);
  160. }
  161. } else {
  162. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
  163. vop = vop_6010_ntsc_d1;
  164. vop_len = sizeof(vop_6010_ntsc_d1);
  165. } else {
  166. vop = vop_6010_pal_d1;
  167. vop_len = sizeof(vop_6010_pal_d1);
  168. }
  169. }
  170. }
  171. memcpy(solo_enc->vop, vop, vop_len);
  172. /* Some fixups for 6010/M4V */
  173. if (solo_dev->type == SOLO_DEV_6010) {
  174. u16 fps = solo_dev->fps * 1000;
  175. u16 interval = solo_enc->interval * 1000;
  176. vop = solo_enc->vop;
  177. /* Frame rate and interval */
  178. vop[22] = fps >> 4;
  179. vop[23] = ((fps << 4) & 0xf0) | 0x0c
  180. | ((interval >> 13) & 0x3);
  181. vop[24] = (interval >> 5) & 0xff;
  182. vop[25] = ((interval << 3) & 0xf8) | 0x04;
  183. }
  184. solo_enc->vop_len = vop_len;
  185. /* Now handle the jpeg header */
  186. vop = solo_enc->jpeg_header;
  187. vop[SOF0_START + 5] = 0xff & (solo_enc->height >> 8);
  188. vop[SOF0_START + 6] = 0xff & solo_enc->height;
  189. vop[SOF0_START + 7] = 0xff & (solo_enc->width >> 8);
  190. vop[SOF0_START + 8] = 0xff & solo_enc->width;
  191. memcpy(vop + DQT_START,
  192. jpeg_dqt[solo_g_jpeg_qp(solo_dev, solo_enc->ch)], DQT_LEN);
  193. }
  194. static int solo_enc_on(struct solo_enc_dev *solo_enc)
  195. {
  196. u8 ch = solo_enc->ch;
  197. struct solo_dev *solo_dev = solo_enc->solo_dev;
  198. u8 interval;
  199. solo_update_mode(solo_enc);
  200. /* Make sure to do a bandwidth check */
  201. if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
  202. return -EBUSY;
  203. solo_enc->sequence = 0;
  204. solo_dev->enc_bw_remain -= solo_enc->bw_weight;
  205. if (solo_enc->type == SOLO_ENC_TYPE_EXT)
  206. solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
  207. /* Disable all encoding for this channel */
  208. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
  209. /* Common for both std and ext encoding */
  210. solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
  211. solo_enc->interlaced ? 1 : 0);
  212. if (solo_enc->interlaced)
  213. interval = solo_enc->interval - 1;
  214. else
  215. interval = solo_enc->interval;
  216. /* Standard encoding only */
  217. solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
  218. solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
  219. solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
  220. /* Extended encoding only */
  221. solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
  222. solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
  223. solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
  224. /* Enables the standard encoder */
  225. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
  226. return 0;
  227. }
  228. static void solo_enc_off(struct solo_enc_dev *solo_enc)
  229. {
  230. struct solo_dev *solo_dev = solo_enc->solo_dev;
  231. solo_dev->enc_bw_remain += solo_enc->bw_weight;
  232. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
  233. solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
  234. }
  235. static int enc_get_mpeg_dma(struct solo_dev *solo_dev, dma_addr_t dma,
  236. unsigned int off, unsigned int size)
  237. {
  238. int ret;
  239. if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
  240. return -EINVAL;
  241. /* Single shot */
  242. if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
  243. return solo_p2m_dma_t(solo_dev, 0, dma,
  244. SOLO_MP4E_EXT_ADDR(solo_dev) + off, size,
  245. 0, 0);
  246. }
  247. /* Buffer wrap */
  248. ret = solo_p2m_dma_t(solo_dev, 0, dma,
  249. SOLO_MP4E_EXT_ADDR(solo_dev) + off,
  250. SOLO_MP4E_EXT_SIZE(solo_dev) - off, 0, 0);
  251. if (!ret) {
  252. ret = solo_p2m_dma_t(solo_dev, 0,
  253. dma + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
  254. SOLO_MP4E_EXT_ADDR(solo_dev),
  255. size + off - SOLO_MP4E_EXT_SIZE(solo_dev), 0, 0);
  256. }
  257. return ret;
  258. }
  259. /* Build a descriptor queue out of an SG list and send it to the P2M for
  260. * processing. */
  261. static int solo_send_desc(struct solo_enc_dev *solo_enc, int skip,
  262. struct sg_table *vbuf, int off, int size,
  263. unsigned int base, unsigned int base_size)
  264. {
  265. struct solo_dev *solo_dev = solo_enc->solo_dev;
  266. struct scatterlist *sg;
  267. int i;
  268. int ret;
  269. if (WARN_ON_ONCE(size > FRAME_BUF_SIZE))
  270. return -EINVAL;
  271. solo_enc->desc_count = 1;
  272. for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
  273. struct solo_p2m_desc *desc;
  274. dma_addr_t dma;
  275. int len;
  276. int left = base_size - off;
  277. desc = &solo_enc->desc_items[solo_enc->desc_count++];
  278. dma = sg_dma_address(sg);
  279. len = sg_dma_len(sg);
  280. /* We assume this is smaller than the scatter size */
  281. BUG_ON(skip >= len);
  282. if (skip) {
  283. len -= skip;
  284. dma += skip;
  285. size -= skip;
  286. skip = 0;
  287. }
  288. len = min(len, size);
  289. if (len <= left) {
  290. /* Single descriptor */
  291. solo_p2m_fill_desc(desc, 0, dma, base + off,
  292. len, 0, 0);
  293. } else {
  294. /* Buffer wrap */
  295. /* XXX: Do these as separate DMA requests, to avoid
  296. timeout errors triggered by awkwardly sized
  297. descriptors. See
  298. <https://github.com/bluecherrydvr/solo6x10/issues/8>
  299. */
  300. ret = solo_p2m_dma_t(solo_dev, 0, dma, base + off,
  301. left, 0, 0);
  302. if (ret)
  303. return ret;
  304. ret = solo_p2m_dma_t(solo_dev, 0, dma + left, base,
  305. len - left, 0, 0);
  306. if (ret)
  307. return ret;
  308. solo_enc->desc_count--;
  309. }
  310. size -= len;
  311. if (size <= 0)
  312. break;
  313. off += len;
  314. if (off >= base_size)
  315. off -= base_size;
  316. /* Because we may use two descriptors per loop */
  317. if (solo_enc->desc_count >= (solo_enc->desc_nelts - 1)) {
  318. ret = solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
  319. solo_enc->desc_dma,
  320. solo_enc->desc_count - 1);
  321. if (ret)
  322. return ret;
  323. solo_enc->desc_count = 1;
  324. }
  325. }
  326. if (solo_enc->desc_count <= 1)
  327. return 0;
  328. return solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
  329. solo_enc->desc_dma, solo_enc->desc_count - 1);
  330. }
  331. /* Extract values from VOP header - VE_STATUSxx */
  332. static inline int vop_interlaced(const vop_header *vh)
  333. {
  334. return (__le32_to_cpu((*vh)[0]) >> 30) & 1;
  335. }
  336. static inline u8 vop_channel(const vop_header *vh)
  337. {
  338. return (__le32_to_cpu((*vh)[0]) >> 24) & 0x1F;
  339. }
  340. static inline u8 vop_type(const vop_header *vh)
  341. {
  342. return (__le32_to_cpu((*vh)[0]) >> 22) & 3;
  343. }
  344. static inline u32 vop_mpeg_size(const vop_header *vh)
  345. {
  346. return __le32_to_cpu((*vh)[0]) & 0xFFFFF;
  347. }
  348. static inline u8 vop_hsize(const vop_header *vh)
  349. {
  350. return (__le32_to_cpu((*vh)[1]) >> 8) & 0xFF;
  351. }
  352. static inline u8 vop_vsize(const vop_header *vh)
  353. {
  354. return __le32_to_cpu((*vh)[1]) & 0xFF;
  355. }
  356. static inline u32 vop_mpeg_offset(const vop_header *vh)
  357. {
  358. return __le32_to_cpu((*vh)[2]);
  359. }
  360. static inline u32 vop_jpeg_offset(const vop_header *vh)
  361. {
  362. return __le32_to_cpu((*vh)[3]);
  363. }
  364. static inline u32 vop_jpeg_size(const vop_header *vh)
  365. {
  366. return __le32_to_cpu((*vh)[4]) & 0xFFFFF;
  367. }
  368. static inline u32 vop_sec(const vop_header *vh)
  369. {
  370. return __le32_to_cpu((*vh)[5]);
  371. }
  372. static inline u32 vop_usec(const vop_header *vh)
  373. {
  374. return __le32_to_cpu((*vh)[6]);
  375. }
  376. static int solo_fill_jpeg(struct solo_enc_dev *solo_enc,
  377. struct vb2_buffer *vb, const vop_header *vh)
  378. {
  379. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  380. struct solo_dev *solo_dev = solo_enc->solo_dev;
  381. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  382. int frame_size;
  383. vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
  384. if (vb2_plane_size(vb, 0) < vop_jpeg_size(vh) + solo_enc->jpeg_len)
  385. return -EIO;
  386. frame_size = ALIGN(vop_jpeg_size(vh) + solo_enc->jpeg_len, DMA_ALIGN);
  387. vb2_set_plane_payload(vb, 0, vop_jpeg_size(vh) + solo_enc->jpeg_len);
  388. return solo_send_desc(solo_enc, solo_enc->jpeg_len, sgt,
  389. vop_jpeg_offset(vh) - SOLO_JPEG_EXT_ADDR(solo_dev),
  390. frame_size, SOLO_JPEG_EXT_ADDR(solo_dev),
  391. SOLO_JPEG_EXT_SIZE(solo_dev));
  392. }
  393. static int solo_fill_mpeg(struct solo_enc_dev *solo_enc,
  394. struct vb2_buffer *vb, const vop_header *vh)
  395. {
  396. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  397. struct solo_dev *solo_dev = solo_enc->solo_dev;
  398. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  399. int frame_off, frame_size;
  400. int skip = 0;
  401. if (vb2_plane_size(vb, 0) < vop_mpeg_size(vh))
  402. return -EIO;
  403. /* If this is a key frame, add extra header */
  404. vbuf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME |
  405. V4L2_BUF_FLAG_BFRAME);
  406. if (!vop_type(vh)) {
  407. skip = solo_enc->vop_len;
  408. vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
  409. vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh) +
  410. solo_enc->vop_len);
  411. } else {
  412. vbuf->flags |= V4L2_BUF_FLAG_PFRAME;
  413. vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh));
  414. }
  415. /* Now get the actual mpeg payload */
  416. frame_off = (vop_mpeg_offset(vh) - SOLO_MP4E_EXT_ADDR(solo_dev) +
  417. sizeof(*vh)) % SOLO_MP4E_EXT_SIZE(solo_dev);
  418. frame_size = ALIGN(vop_mpeg_size(vh) + skip, DMA_ALIGN);
  419. return solo_send_desc(solo_enc, skip, sgt, frame_off, frame_size,
  420. SOLO_MP4E_EXT_ADDR(solo_dev),
  421. SOLO_MP4E_EXT_SIZE(solo_dev));
  422. }
  423. static int solo_enc_fillbuf(struct solo_enc_dev *solo_enc,
  424. struct vb2_buffer *vb, struct solo_enc_buf *enc_buf)
  425. {
  426. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  427. const vop_header *vh = enc_buf->vh;
  428. int ret;
  429. switch (solo_enc->fmt) {
  430. case V4L2_PIX_FMT_MPEG4:
  431. case V4L2_PIX_FMT_H264:
  432. ret = solo_fill_mpeg(solo_enc, vb, vh);
  433. break;
  434. default: /* V4L2_PIX_FMT_MJPEG */
  435. ret = solo_fill_jpeg(solo_enc, vb, vh);
  436. break;
  437. }
  438. if (!ret) {
  439. vbuf->sequence = solo_enc->sequence++;
  440. vb->timestamp = ktime_get_ns();
  441. /* Check for motion flags */
  442. if (solo_is_motion_on(solo_enc) && enc_buf->motion) {
  443. struct v4l2_event ev = {
  444. .type = V4L2_EVENT_MOTION_DET,
  445. .u.motion_det = {
  446. .flags
  447. = V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ,
  448. .frame_sequence = vbuf->sequence,
  449. .region_mask = enc_buf->motion ? 1 : 0,
  450. },
  451. };
  452. v4l2_event_queue(solo_enc->vfd, &ev);
  453. }
  454. }
  455. vb2_buffer_done(vb, ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  456. return ret;
  457. }
  458. static void solo_enc_handle_one(struct solo_enc_dev *solo_enc,
  459. struct solo_enc_buf *enc_buf)
  460. {
  461. struct solo_vb2_buf *vb;
  462. unsigned long flags;
  463. mutex_lock(&solo_enc->lock);
  464. if (solo_enc->type != enc_buf->type)
  465. goto unlock;
  466. spin_lock_irqsave(&solo_enc->av_lock, flags);
  467. if (list_empty(&solo_enc->vidq_active)) {
  468. spin_unlock_irqrestore(&solo_enc->av_lock, flags);
  469. goto unlock;
  470. }
  471. vb = list_first_entry(&solo_enc->vidq_active, struct solo_vb2_buf,
  472. list);
  473. list_del(&vb->list);
  474. spin_unlock_irqrestore(&solo_enc->av_lock, flags);
  475. solo_enc_fillbuf(solo_enc, &vb->vb.vb2_buf, enc_buf);
  476. unlock:
  477. mutex_unlock(&solo_enc->lock);
  478. }
  479. void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
  480. {
  481. wake_up_interruptible_all(&solo_dev->ring_thread_wait);
  482. }
  483. static void solo_handle_ring(struct solo_dev *solo_dev)
  484. {
  485. for (;;) {
  486. struct solo_enc_dev *solo_enc;
  487. struct solo_enc_buf enc_buf;
  488. u32 mpeg_current, off;
  489. u8 ch;
  490. u8 cur_q;
  491. /* Check if the hardware has any new ones in the queue */
  492. cur_q = solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xff;
  493. if (cur_q == solo_dev->enc_idx)
  494. break;
  495. mpeg_current = solo_reg_read(solo_dev,
  496. SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
  497. solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
  498. ch = (mpeg_current >> 24) & 0x1f;
  499. off = mpeg_current & 0x00ffffff;
  500. if (ch >= SOLO_MAX_CHANNELS) {
  501. ch -= SOLO_MAX_CHANNELS;
  502. enc_buf.type = SOLO_ENC_TYPE_EXT;
  503. } else
  504. enc_buf.type = SOLO_ENC_TYPE_STD;
  505. solo_enc = solo_dev->v4l2_enc[ch];
  506. if (solo_enc == NULL) {
  507. dev_err(&solo_dev->pdev->dev,
  508. "Got spurious packet for channel %d\n", ch);
  509. continue;
  510. }
  511. /* FAIL... */
  512. if (enc_get_mpeg_dma(solo_dev, solo_dev->vh_dma, off,
  513. sizeof(vop_header)))
  514. continue;
  515. enc_buf.vh = solo_dev->vh_buf;
  516. /* Sanity check */
  517. if (vop_mpeg_offset(enc_buf.vh) !=
  518. SOLO_MP4E_EXT_ADDR(solo_dev) + off)
  519. continue;
  520. if (solo_motion_detected(solo_enc))
  521. enc_buf.motion = 1;
  522. else
  523. enc_buf.motion = 0;
  524. solo_enc_handle_one(solo_enc, &enc_buf);
  525. }
  526. }
  527. static int solo_ring_thread(void *data)
  528. {
  529. struct solo_dev *solo_dev = data;
  530. DECLARE_WAITQUEUE(wait, current);
  531. set_freezable();
  532. add_wait_queue(&solo_dev->ring_thread_wait, &wait);
  533. for (;;) {
  534. long timeout = schedule_timeout_interruptible(HZ);
  535. if (timeout == -ERESTARTSYS || kthread_should_stop())
  536. break;
  537. solo_handle_ring(solo_dev);
  538. try_to_freeze();
  539. }
  540. remove_wait_queue(&solo_dev->ring_thread_wait, &wait);
  541. return 0;
  542. }
  543. static int solo_enc_queue_setup(struct vb2_queue *q,
  544. unsigned int *num_buffers,
  545. unsigned int *num_planes, unsigned int sizes[],
  546. struct device *alloc_devs[])
  547. {
  548. sizes[0] = FRAME_BUF_SIZE;
  549. *num_planes = 1;
  550. if (*num_buffers < MIN_VID_BUFFERS)
  551. *num_buffers = MIN_VID_BUFFERS;
  552. return 0;
  553. }
  554. static void solo_enc_buf_queue(struct vb2_buffer *vb)
  555. {
  556. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  557. struct vb2_queue *vq = vb->vb2_queue;
  558. struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vq);
  559. struct solo_vb2_buf *solo_vb =
  560. container_of(vbuf, struct solo_vb2_buf, vb);
  561. spin_lock(&solo_enc->av_lock);
  562. list_add_tail(&solo_vb->list, &solo_enc->vidq_active);
  563. spin_unlock(&solo_enc->av_lock);
  564. }
  565. static int solo_ring_start(struct solo_dev *solo_dev)
  566. {
  567. solo_dev->ring_thread = kthread_run(solo_ring_thread, solo_dev,
  568. SOLO6X10_NAME "_ring");
  569. if (IS_ERR(solo_dev->ring_thread)) {
  570. int err = PTR_ERR(solo_dev->ring_thread);
  571. solo_dev->ring_thread = NULL;
  572. return err;
  573. }
  574. solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
  575. return 0;
  576. }
  577. static void solo_ring_stop(struct solo_dev *solo_dev)
  578. {
  579. if (solo_dev->ring_thread) {
  580. kthread_stop(solo_dev->ring_thread);
  581. solo_dev->ring_thread = NULL;
  582. }
  583. solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
  584. }
  585. static int solo_enc_start_streaming(struct vb2_queue *q, unsigned int count)
  586. {
  587. struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
  588. return solo_enc_on(solo_enc);
  589. }
  590. static void solo_enc_stop_streaming(struct vb2_queue *q)
  591. {
  592. struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
  593. unsigned long flags;
  594. spin_lock_irqsave(&solo_enc->av_lock, flags);
  595. solo_enc_off(solo_enc);
  596. while (!list_empty(&solo_enc->vidq_active)) {
  597. struct solo_vb2_buf *buf = list_entry(
  598. solo_enc->vidq_active.next,
  599. struct solo_vb2_buf, list);
  600. list_del(&buf->list);
  601. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  602. }
  603. spin_unlock_irqrestore(&solo_enc->av_lock, flags);
  604. }
  605. static void solo_enc_buf_finish(struct vb2_buffer *vb)
  606. {
  607. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  608. struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vb->vb2_queue);
  609. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  610. switch (solo_enc->fmt) {
  611. case V4L2_PIX_FMT_MPEG4:
  612. case V4L2_PIX_FMT_H264:
  613. if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME)
  614. sg_copy_from_buffer(sgt->sgl, sgt->nents,
  615. solo_enc->vop, solo_enc->vop_len);
  616. break;
  617. default: /* V4L2_PIX_FMT_MJPEG */
  618. sg_copy_from_buffer(sgt->sgl, sgt->nents,
  619. solo_enc->jpeg_header, solo_enc->jpeg_len);
  620. break;
  621. }
  622. }
  623. static const struct vb2_ops solo_enc_video_qops = {
  624. .queue_setup = solo_enc_queue_setup,
  625. .buf_queue = solo_enc_buf_queue,
  626. .buf_finish = solo_enc_buf_finish,
  627. .start_streaming = solo_enc_start_streaming,
  628. .stop_streaming = solo_enc_stop_streaming,
  629. .wait_prepare = vb2_ops_wait_prepare,
  630. .wait_finish = vb2_ops_wait_finish,
  631. };
  632. static int solo_enc_querycap(struct file *file, void *priv,
  633. struct v4l2_capability *cap)
  634. {
  635. struct solo_enc_dev *solo_enc = video_drvdata(file);
  636. struct solo_dev *solo_dev = solo_enc->solo_dev;
  637. strcpy(cap->driver, SOLO6X10_NAME);
  638. snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
  639. solo_enc->ch);
  640. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  641. pci_name(solo_dev->pdev));
  642. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  643. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  644. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  645. return 0;
  646. }
  647. static int solo_enc_enum_input(struct file *file, void *priv,
  648. struct v4l2_input *input)
  649. {
  650. struct solo_enc_dev *solo_enc = video_drvdata(file);
  651. struct solo_dev *solo_dev = solo_enc->solo_dev;
  652. if (input->index)
  653. return -EINVAL;
  654. snprintf(input->name, sizeof(input->name), "Encoder %d",
  655. solo_enc->ch + 1);
  656. input->type = V4L2_INPUT_TYPE_CAMERA;
  657. input->std = solo_enc->vfd->tvnorms;
  658. if (!tw28_get_video_status(solo_dev, solo_enc->ch))
  659. input->status = V4L2_IN_ST_NO_SIGNAL;
  660. return 0;
  661. }
  662. static int solo_enc_set_input(struct file *file, void *priv,
  663. unsigned int index)
  664. {
  665. if (index)
  666. return -EINVAL;
  667. return 0;
  668. }
  669. static int solo_enc_get_input(struct file *file, void *priv,
  670. unsigned int *index)
  671. {
  672. *index = 0;
  673. return 0;
  674. }
  675. static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
  676. struct v4l2_fmtdesc *f)
  677. {
  678. struct solo_enc_dev *solo_enc = video_drvdata(file);
  679. int dev_type = solo_enc->solo_dev->type;
  680. switch (f->index) {
  681. case 0:
  682. switch (dev_type) {
  683. case SOLO_DEV_6010:
  684. f->pixelformat = V4L2_PIX_FMT_MPEG4;
  685. strcpy(f->description, "MPEG-4 part 2");
  686. break;
  687. case SOLO_DEV_6110:
  688. f->pixelformat = V4L2_PIX_FMT_H264;
  689. strcpy(f->description, "H.264");
  690. break;
  691. }
  692. break;
  693. case 1:
  694. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  695. strcpy(f->description, "MJPEG");
  696. break;
  697. default:
  698. return -EINVAL;
  699. }
  700. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  701. return 0;
  702. }
  703. static inline int solo_valid_pixfmt(u32 pixfmt, int dev_type)
  704. {
  705. return (pixfmt == V4L2_PIX_FMT_H264 && dev_type == SOLO_DEV_6110)
  706. || (pixfmt == V4L2_PIX_FMT_MPEG4 && dev_type == SOLO_DEV_6010)
  707. || pixfmt == V4L2_PIX_FMT_MJPEG ? 0 : -EINVAL;
  708. }
  709. static int solo_enc_try_fmt_cap(struct file *file, void *priv,
  710. struct v4l2_format *f)
  711. {
  712. struct solo_enc_dev *solo_enc = video_drvdata(file);
  713. struct solo_dev *solo_dev = solo_enc->solo_dev;
  714. struct v4l2_pix_format *pix = &f->fmt.pix;
  715. if (solo_valid_pixfmt(pix->pixelformat, solo_dev->type))
  716. return -EINVAL;
  717. if (pix->width < solo_dev->video_hsize ||
  718. pix->height < solo_dev->video_vsize << 1) {
  719. /* Default to CIF 1/2 size */
  720. pix->width = solo_dev->video_hsize >> 1;
  721. pix->height = solo_dev->video_vsize;
  722. } else {
  723. /* Full frame */
  724. pix->width = solo_dev->video_hsize;
  725. pix->height = solo_dev->video_vsize << 1;
  726. }
  727. switch (pix->field) {
  728. case V4L2_FIELD_NONE:
  729. case V4L2_FIELD_INTERLACED:
  730. break;
  731. case V4L2_FIELD_ANY:
  732. default:
  733. pix->field = V4L2_FIELD_INTERLACED;
  734. break;
  735. }
  736. /* Just set these */
  737. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  738. pix->sizeimage = FRAME_BUF_SIZE;
  739. pix->bytesperline = 0;
  740. pix->priv = 0;
  741. return 0;
  742. }
  743. static int solo_enc_set_fmt_cap(struct file *file, void *priv,
  744. struct v4l2_format *f)
  745. {
  746. struct solo_enc_dev *solo_enc = video_drvdata(file);
  747. struct solo_dev *solo_dev = solo_enc->solo_dev;
  748. struct v4l2_pix_format *pix = &f->fmt.pix;
  749. int ret;
  750. if (vb2_is_busy(&solo_enc->vidq))
  751. return -EBUSY;
  752. ret = solo_enc_try_fmt_cap(file, priv, f);
  753. if (ret)
  754. return ret;
  755. if (pix->width == solo_dev->video_hsize)
  756. solo_enc->mode = SOLO_ENC_MODE_D1;
  757. else
  758. solo_enc->mode = SOLO_ENC_MODE_CIF;
  759. /* This does not change the encoder at all */
  760. solo_enc->fmt = pix->pixelformat;
  761. /*
  762. * More information is needed about these 'extended' types. As far
  763. * as I can tell these are basically additional video streams with
  764. * different MPEG encoding attributes that can run in parallel with
  765. * the main stream. If so, then this should be implemented as a
  766. * second video node. Abusing priv like this is certainly not the
  767. * right approach.
  768. if (pix->priv)
  769. solo_enc->type = SOLO_ENC_TYPE_EXT;
  770. */
  771. solo_update_mode(solo_enc);
  772. return 0;
  773. }
  774. static int solo_enc_get_fmt_cap(struct file *file, void *priv,
  775. struct v4l2_format *f)
  776. {
  777. struct solo_enc_dev *solo_enc = video_drvdata(file);
  778. struct v4l2_pix_format *pix = &f->fmt.pix;
  779. pix->width = solo_enc->width;
  780. pix->height = solo_enc->height;
  781. pix->pixelformat = solo_enc->fmt;
  782. pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
  783. V4L2_FIELD_NONE;
  784. pix->sizeimage = FRAME_BUF_SIZE;
  785. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  786. pix->priv = 0;
  787. return 0;
  788. }
  789. static int solo_enc_g_std(struct file *file, void *priv, v4l2_std_id *i)
  790. {
  791. struct solo_enc_dev *solo_enc = video_drvdata(file);
  792. struct solo_dev *solo_dev = solo_enc->solo_dev;
  793. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
  794. *i = V4L2_STD_NTSC_M;
  795. else
  796. *i = V4L2_STD_PAL;
  797. return 0;
  798. }
  799. static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id std)
  800. {
  801. struct solo_enc_dev *solo_enc = video_drvdata(file);
  802. return solo_set_video_type(solo_enc->solo_dev, std & V4L2_STD_625_50);
  803. }
  804. static int solo_enum_framesizes(struct file *file, void *priv,
  805. struct v4l2_frmsizeenum *fsize)
  806. {
  807. struct solo_enc_dev *solo_enc = video_drvdata(file);
  808. struct solo_dev *solo_dev = solo_enc->solo_dev;
  809. if (solo_valid_pixfmt(fsize->pixel_format, solo_dev->type))
  810. return -EINVAL;
  811. switch (fsize->index) {
  812. case 0:
  813. fsize->discrete.width = solo_dev->video_hsize >> 1;
  814. fsize->discrete.height = solo_dev->video_vsize;
  815. break;
  816. case 1:
  817. fsize->discrete.width = solo_dev->video_hsize;
  818. fsize->discrete.height = solo_dev->video_vsize << 1;
  819. break;
  820. default:
  821. return -EINVAL;
  822. }
  823. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  824. return 0;
  825. }
  826. static int solo_enum_frameintervals(struct file *file, void *priv,
  827. struct v4l2_frmivalenum *fintv)
  828. {
  829. struct solo_enc_dev *solo_enc = video_drvdata(file);
  830. struct solo_dev *solo_dev = solo_enc->solo_dev;
  831. if (solo_valid_pixfmt(fintv->pixel_format, solo_dev->type))
  832. return -EINVAL;
  833. if (fintv->index)
  834. return -EINVAL;
  835. if ((fintv->width != solo_dev->video_hsize >> 1 ||
  836. fintv->height != solo_dev->video_vsize) &&
  837. (fintv->width != solo_dev->video_hsize ||
  838. fintv->height != solo_dev->video_vsize << 1))
  839. return -EINVAL;
  840. fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  841. fintv->stepwise.min.numerator = 1;
  842. fintv->stepwise.min.denominator = solo_dev->fps;
  843. fintv->stepwise.max.numerator = 15;
  844. fintv->stepwise.max.denominator = solo_dev->fps;
  845. fintv->stepwise.step.numerator = 1;
  846. fintv->stepwise.step.denominator = solo_dev->fps;
  847. return 0;
  848. }
  849. static int solo_g_parm(struct file *file, void *priv,
  850. struct v4l2_streamparm *sp)
  851. {
  852. struct solo_enc_dev *solo_enc = video_drvdata(file);
  853. struct v4l2_captureparm *cp = &sp->parm.capture;
  854. cp->capability = V4L2_CAP_TIMEPERFRAME;
  855. cp->timeperframe.numerator = solo_enc->interval;
  856. cp->timeperframe.denominator = solo_enc->solo_dev->fps;
  857. cp->capturemode = 0;
  858. /* XXX: Shouldn't we be able to get/set this from videobuf? */
  859. cp->readbuffers = 2;
  860. return 0;
  861. }
  862. static inline int calc_interval(u8 fps, u32 n, u32 d)
  863. {
  864. if (!n || !d)
  865. return 1;
  866. if (d == fps)
  867. return n;
  868. n *= fps;
  869. return min(15U, n / d + (n % d >= (fps >> 1)));
  870. }
  871. static int solo_s_parm(struct file *file, void *priv,
  872. struct v4l2_streamparm *sp)
  873. {
  874. struct solo_enc_dev *solo_enc = video_drvdata(file);
  875. struct v4l2_fract *t = &sp->parm.capture.timeperframe;
  876. u8 fps = solo_enc->solo_dev->fps;
  877. if (vb2_is_streaming(&solo_enc->vidq))
  878. return -EBUSY;
  879. solo_enc->interval = calc_interval(fps, t->numerator, t->denominator);
  880. solo_update_mode(solo_enc);
  881. return solo_g_parm(file, priv, sp);
  882. }
  883. static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
  884. {
  885. struct solo_enc_dev *solo_enc =
  886. container_of(ctrl->handler, struct solo_enc_dev, hdl);
  887. struct solo_dev *solo_dev = solo_enc->solo_dev;
  888. int err;
  889. switch (ctrl->id) {
  890. case V4L2_CID_BRIGHTNESS:
  891. case V4L2_CID_CONTRAST:
  892. case V4L2_CID_SATURATION:
  893. case V4L2_CID_HUE:
  894. case V4L2_CID_SHARPNESS:
  895. return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
  896. ctrl->val);
  897. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  898. solo_enc->gop = ctrl->val;
  899. solo_reg_write(solo_dev, SOLO_VE_CH_GOP(solo_enc->ch), solo_enc->gop);
  900. solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(solo_enc->ch), solo_enc->gop);
  901. return 0;
  902. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
  903. solo_enc->qp = ctrl->val;
  904. solo_reg_write(solo_dev, SOLO_VE_CH_QP(solo_enc->ch), solo_enc->qp);
  905. solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(solo_enc->ch), solo_enc->qp);
  906. return 0;
  907. case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
  908. solo_enc->motion_thresh = ctrl->val << 8;
  909. if (!solo_enc->motion_global || !solo_enc->motion_enabled)
  910. return 0;
  911. return solo_set_motion_threshold(solo_dev, solo_enc->ch,
  912. solo_enc->motion_thresh);
  913. case V4L2_CID_DETECT_MD_MODE:
  914. solo_enc->motion_global = ctrl->val == V4L2_DETECT_MD_MODE_GLOBAL;
  915. solo_enc->motion_enabled = ctrl->val > V4L2_DETECT_MD_MODE_DISABLED;
  916. if (ctrl->val) {
  917. if (solo_enc->motion_global)
  918. err = solo_set_motion_threshold(solo_dev, solo_enc->ch,
  919. solo_enc->motion_thresh);
  920. else
  921. err = solo_set_motion_block(solo_dev, solo_enc->ch,
  922. solo_enc->md_thresholds->p_cur.p_u16);
  923. if (err)
  924. return err;
  925. }
  926. solo_motion_toggle(solo_enc, ctrl->val);
  927. return 0;
  928. case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
  929. if (solo_enc->motion_enabled && !solo_enc->motion_global)
  930. return solo_set_motion_block(solo_dev, solo_enc->ch,
  931. solo_enc->md_thresholds->p_new.p_u16);
  932. break;
  933. case V4L2_CID_OSD_TEXT:
  934. strcpy(solo_enc->osd_text, ctrl->p_new.p_char);
  935. return solo_osd_print(solo_enc);
  936. default:
  937. return -EINVAL;
  938. }
  939. return 0;
  940. }
  941. static int solo_subscribe_event(struct v4l2_fh *fh,
  942. const struct v4l2_event_subscription *sub)
  943. {
  944. switch (sub->type) {
  945. case V4L2_EVENT_MOTION_DET:
  946. /* Allow for up to 30 events (1 second for NTSC) to be
  947. * stored. */
  948. return v4l2_event_subscribe(fh, sub, 30, NULL);
  949. default:
  950. return v4l2_ctrl_subscribe_event(fh, sub);
  951. }
  952. }
  953. static const struct v4l2_file_operations solo_enc_fops = {
  954. .owner = THIS_MODULE,
  955. .open = v4l2_fh_open,
  956. .release = vb2_fop_release,
  957. .read = vb2_fop_read,
  958. .poll = vb2_fop_poll,
  959. .mmap = vb2_fop_mmap,
  960. .unlocked_ioctl = video_ioctl2,
  961. };
  962. static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
  963. .vidioc_querycap = solo_enc_querycap,
  964. .vidioc_s_std = solo_enc_s_std,
  965. .vidioc_g_std = solo_enc_g_std,
  966. /* Input callbacks */
  967. .vidioc_enum_input = solo_enc_enum_input,
  968. .vidioc_s_input = solo_enc_set_input,
  969. .vidioc_g_input = solo_enc_get_input,
  970. /* Video capture format callbacks */
  971. .vidioc_enum_fmt_vid_cap = solo_enc_enum_fmt_cap,
  972. .vidioc_try_fmt_vid_cap = solo_enc_try_fmt_cap,
  973. .vidioc_s_fmt_vid_cap = solo_enc_set_fmt_cap,
  974. .vidioc_g_fmt_vid_cap = solo_enc_get_fmt_cap,
  975. /* Streaming I/O */
  976. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  977. .vidioc_querybuf = vb2_ioctl_querybuf,
  978. .vidioc_qbuf = vb2_ioctl_qbuf,
  979. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  980. .vidioc_streamon = vb2_ioctl_streamon,
  981. .vidioc_streamoff = vb2_ioctl_streamoff,
  982. /* Frame size and interval */
  983. .vidioc_enum_framesizes = solo_enum_framesizes,
  984. .vidioc_enum_frameintervals = solo_enum_frameintervals,
  985. /* Video capture parameters */
  986. .vidioc_s_parm = solo_s_parm,
  987. .vidioc_g_parm = solo_g_parm,
  988. /* Logging and events */
  989. .vidioc_log_status = v4l2_ctrl_log_status,
  990. .vidioc_subscribe_event = solo_subscribe_event,
  991. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  992. };
  993. static const struct video_device solo_enc_template = {
  994. .name = SOLO6X10_NAME,
  995. .fops = &solo_enc_fops,
  996. .ioctl_ops = &solo_enc_ioctl_ops,
  997. .minor = -1,
  998. .release = video_device_release,
  999. .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
  1000. };
  1001. static const struct v4l2_ctrl_ops solo_ctrl_ops = {
  1002. .s_ctrl = solo_s_ctrl,
  1003. };
  1004. static const struct v4l2_ctrl_config solo_osd_text_ctrl = {
  1005. .ops = &solo_ctrl_ops,
  1006. .id = V4L2_CID_OSD_TEXT,
  1007. .name = "OSD Text",
  1008. .type = V4L2_CTRL_TYPE_STRING,
  1009. .max = OSD_TEXT_MAX,
  1010. .step = 1,
  1011. };
  1012. /* Motion Detection Threshold matrix */
  1013. static const struct v4l2_ctrl_config solo_md_thresholds = {
  1014. .ops = &solo_ctrl_ops,
  1015. .id = V4L2_CID_DETECT_MD_THRESHOLD_GRID,
  1016. .dims = { SOLO_MOTION_SZ, SOLO_MOTION_SZ },
  1017. .def = SOLO_DEF_MOT_THRESH,
  1018. .max = 65535,
  1019. .step = 1,
  1020. };
  1021. static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
  1022. u8 ch, unsigned nr)
  1023. {
  1024. struct solo_enc_dev *solo_enc;
  1025. struct v4l2_ctrl_handler *hdl;
  1026. int ret;
  1027. solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
  1028. if (!solo_enc)
  1029. return ERR_PTR(-ENOMEM);
  1030. hdl = &solo_enc->hdl;
  1031. v4l2_ctrl_handler_init(hdl, 10);
  1032. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1033. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1034. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1035. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1036. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1037. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1038. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1039. V4L2_CID_HUE, 0, 255, 1, 128);
  1040. if (tw28_has_sharpness(solo_dev, ch))
  1041. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1042. V4L2_CID_SHARPNESS, 0, 15, 1, 0);
  1043. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1044. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, solo_dev->fps);
  1045. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1046. V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 31, 1, SOLO_DEFAULT_QP);
  1047. v4l2_ctrl_new_std_menu(hdl, &solo_ctrl_ops,
  1048. V4L2_CID_DETECT_MD_MODE,
  1049. V4L2_DETECT_MD_MODE_THRESHOLD_GRID, 0,
  1050. V4L2_DETECT_MD_MODE_DISABLED);
  1051. v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
  1052. V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD, 0, 0xff, 1,
  1053. SOLO_DEF_MOT_THRESH >> 8);
  1054. v4l2_ctrl_new_custom(hdl, &solo_osd_text_ctrl, NULL);
  1055. solo_enc->md_thresholds =
  1056. v4l2_ctrl_new_custom(hdl, &solo_md_thresholds, NULL);
  1057. if (hdl->error) {
  1058. ret = hdl->error;
  1059. goto hdl_free;
  1060. }
  1061. solo_enc->solo_dev = solo_dev;
  1062. solo_enc->ch = ch;
  1063. mutex_init(&solo_enc->lock);
  1064. spin_lock_init(&solo_enc->av_lock);
  1065. INIT_LIST_HEAD(&solo_enc->vidq_active);
  1066. solo_enc->fmt = (solo_dev->type == SOLO_DEV_6010) ?
  1067. V4L2_PIX_FMT_MPEG4 : V4L2_PIX_FMT_H264;
  1068. solo_enc->type = SOLO_ENC_TYPE_STD;
  1069. solo_enc->qp = SOLO_DEFAULT_QP;
  1070. solo_enc->gop = solo_dev->fps;
  1071. solo_enc->interval = 1;
  1072. solo_enc->mode = SOLO_ENC_MODE_CIF;
  1073. solo_enc->motion_global = true;
  1074. solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
  1075. solo_enc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1076. solo_enc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  1077. solo_enc->vidq.ops = &solo_enc_video_qops;
  1078. solo_enc->vidq.mem_ops = &vb2_dma_sg_memops;
  1079. solo_enc->vidq.drv_priv = solo_enc;
  1080. solo_enc->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
  1081. solo_enc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1082. solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
  1083. solo_enc->vidq.lock = &solo_enc->lock;
  1084. solo_enc->vidq.dev = &solo_dev->pdev->dev;
  1085. ret = vb2_queue_init(&solo_enc->vidq);
  1086. if (ret)
  1087. goto hdl_free;
  1088. solo_update_mode(solo_enc);
  1089. spin_lock_init(&solo_enc->motion_lock);
  1090. /* Initialize this per encoder */
  1091. solo_enc->jpeg_len = sizeof(jpeg_header);
  1092. memcpy(solo_enc->jpeg_header, jpeg_header, solo_enc->jpeg_len);
  1093. solo_enc->desc_nelts = 32;
  1094. solo_enc->desc_items = pci_alloc_consistent(solo_dev->pdev,
  1095. sizeof(struct solo_p2m_desc) *
  1096. solo_enc->desc_nelts,
  1097. &solo_enc->desc_dma);
  1098. ret = -ENOMEM;
  1099. if (solo_enc->desc_items == NULL)
  1100. goto hdl_free;
  1101. solo_enc->vfd = video_device_alloc();
  1102. if (!solo_enc->vfd)
  1103. goto pci_free;
  1104. *solo_enc->vfd = solo_enc_template;
  1105. solo_enc->vfd->v4l2_dev = &solo_dev->v4l2_dev;
  1106. solo_enc->vfd->ctrl_handler = hdl;
  1107. solo_enc->vfd->queue = &solo_enc->vidq;
  1108. solo_enc->vfd->lock = &solo_enc->lock;
  1109. video_set_drvdata(solo_enc->vfd, solo_enc);
  1110. ret = video_register_device(solo_enc->vfd, VFL_TYPE_GRABBER, nr);
  1111. if (ret < 0)
  1112. goto vdev_release;
  1113. snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
  1114. "%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
  1115. solo_enc->vfd->num);
  1116. return solo_enc;
  1117. vdev_release:
  1118. video_device_release(solo_enc->vfd);
  1119. pci_free:
  1120. pci_free_consistent(solo_enc->solo_dev->pdev,
  1121. sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
  1122. solo_enc->desc_items, solo_enc->desc_dma);
  1123. hdl_free:
  1124. v4l2_ctrl_handler_free(hdl);
  1125. kfree(solo_enc);
  1126. return ERR_PTR(ret);
  1127. }
  1128. static void solo_enc_free(struct solo_enc_dev *solo_enc)
  1129. {
  1130. if (solo_enc == NULL)
  1131. return;
  1132. pci_free_consistent(solo_enc->solo_dev->pdev,
  1133. sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
  1134. solo_enc->desc_items, solo_enc->desc_dma);
  1135. video_unregister_device(solo_enc->vfd);
  1136. v4l2_ctrl_handler_free(&solo_enc->hdl);
  1137. kfree(solo_enc);
  1138. }
  1139. int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
  1140. {
  1141. int i;
  1142. init_waitqueue_head(&solo_dev->ring_thread_wait);
  1143. solo_dev->vh_size = sizeof(vop_header);
  1144. solo_dev->vh_buf = pci_alloc_consistent(solo_dev->pdev,
  1145. solo_dev->vh_size,
  1146. &solo_dev->vh_dma);
  1147. if (solo_dev->vh_buf == NULL)
  1148. return -ENOMEM;
  1149. for (i = 0; i < solo_dev->nr_chans; i++) {
  1150. solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i, nr);
  1151. if (IS_ERR(solo_dev->v4l2_enc[i]))
  1152. break;
  1153. }
  1154. if (i != solo_dev->nr_chans) {
  1155. int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
  1156. while (i--)
  1157. solo_enc_free(solo_dev->v4l2_enc[i]);
  1158. pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
  1159. solo_dev->vh_buf, solo_dev->vh_dma);
  1160. solo_dev->vh_buf = NULL;
  1161. return ret;
  1162. }
  1163. if (solo_dev->type == SOLO_DEV_6010)
  1164. solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
  1165. else
  1166. solo_dev->enc_bw_remain = solo_dev->fps * 4 * 5;
  1167. dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
  1168. solo_dev->v4l2_enc[0]->vfd->num,
  1169. solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
  1170. return solo_ring_start(solo_dev);
  1171. }
  1172. void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
  1173. {
  1174. int i;
  1175. solo_ring_stop(solo_dev);
  1176. for (i = 0; i < solo_dev->nr_chans; i++)
  1177. solo_enc_free(solo_dev->v4l2_enc[i]);
  1178. if (solo_dev->vh_buf)
  1179. pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
  1180. solo_dev->vh_buf, solo_dev->vh_dma);
  1181. }