solo6x10-v4l2-enc.c 37 KB

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