solo6x10-v4l2-enc.c 38 KB

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