solo6x10-v4l2-enc.c 38 KB

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