v4l2-enc.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  1. /*
  2. * Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com
  3. * Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/kthread.h>
  22. #include <linux/freezer.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/videobuf-dma-sg.h>
  26. #include "solo6x10.h"
  27. #include "tw28.h"
  28. #include "jpeg.h"
  29. #define MIN_VID_BUFFERS 4
  30. #define FRAME_BUF_SIZE (128 * 1024)
  31. #define MP4_QS 16
  32. static int solo_enc_thread(void *data);
  33. extern unsigned video_nr;
  34. struct solo_enc_fh {
  35. struct solo_enc_dev *enc;
  36. u32 fmt;
  37. u16 rd_idx;
  38. u8 enc_on;
  39. enum solo_enc_types type;
  40. struct videobuf_queue vidq;
  41. struct list_head vidq_active;
  42. struct task_struct *kthread;
  43. struct p2m_desc desc[SOLO_NR_P2M_DESC];
  44. };
  45. static const u32 solo_user_ctrls[] = {
  46. V4L2_CID_BRIGHTNESS,
  47. V4L2_CID_CONTRAST,
  48. V4L2_CID_SATURATION,
  49. V4L2_CID_HUE,
  50. V4L2_CID_SHARPNESS,
  51. 0
  52. };
  53. static const u32 solo_mpeg_ctrls[] = {
  54. V4L2_CID_MPEG_VIDEO_ENCODING,
  55. V4L2_CID_MPEG_VIDEO_GOP_SIZE,
  56. 0
  57. };
  58. static const u32 solo_private_ctrls[] = {
  59. V4L2_CID_MOTION_ENABLE,
  60. V4L2_CID_MOTION_THRESHOLD,
  61. 0
  62. };
  63. static const u32 solo_fmtx_ctrls[] = {
  64. V4L2_CID_RDS_TX_RADIO_TEXT,
  65. 0
  66. };
  67. static const u32 *solo_ctrl_classes[] = {
  68. solo_user_ctrls,
  69. solo_mpeg_ctrls,
  70. solo_fmtx_ctrls,
  71. solo_private_ctrls,
  72. NULL
  73. };
  74. static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
  75. {
  76. struct solo_dev *solo_dev = solo_enc->solo_dev;
  77. u8 ch = solo_enc->ch;
  78. if (solo_dev->motion_mask & (1 << ch))
  79. return 1;
  80. return 0;
  81. }
  82. static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
  83. {
  84. struct solo_dev *solo_dev = solo_enc->solo_dev;
  85. u8 ch = solo_enc->ch;
  86. spin_lock(&solo_enc->lock);
  87. if (on)
  88. solo_dev->motion_mask |= (1 << ch);
  89. else
  90. solo_dev->motion_mask &= ~(1 << ch);
  91. /* Do this regardless of if we are turning on or off */
  92. solo_reg_write(solo_enc->solo_dev, SOLO_VI_MOT_CLEAR,
  93. 1 << solo_enc->ch);
  94. solo_enc->motion_detected = 0;
  95. solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
  96. SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
  97. (SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
  98. if (solo_dev->motion_mask)
  99. solo_irq_on(solo_dev, SOLO_IRQ_MOTION);
  100. else
  101. solo_irq_off(solo_dev, SOLO_IRQ_MOTION);
  102. spin_unlock(&solo_enc->lock);
  103. }
  104. /* Should be called with solo_enc->lock held */
  105. static void solo_update_mode(struct solo_enc_dev *solo_enc)
  106. {
  107. struct solo_dev *solo_dev = solo_enc->solo_dev;
  108. assert_spin_locked(&solo_enc->lock);
  109. solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
  110. solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
  111. switch (solo_enc->mode) {
  112. case SOLO_ENC_MODE_CIF:
  113. solo_enc->width = solo_dev->video_hsize >> 1;
  114. solo_enc->height = solo_dev->video_vsize;
  115. break;
  116. case SOLO_ENC_MODE_D1:
  117. solo_enc->width = solo_dev->video_hsize;
  118. solo_enc->height = solo_dev->video_vsize << 1;
  119. solo_enc->bw_weight <<= 2;
  120. break;
  121. default:
  122. WARN(1, "mode is unknown\n");
  123. }
  124. }
  125. /* Should be called with solo_enc->lock held */
  126. static int solo_enc_on(struct solo_enc_fh *fh)
  127. {
  128. struct solo_enc_dev *solo_enc = fh->enc;
  129. u8 ch = solo_enc->ch;
  130. struct solo_dev *solo_dev = solo_enc->solo_dev;
  131. u8 interval;
  132. assert_spin_locked(&solo_enc->lock);
  133. if (fh->enc_on)
  134. return 0;
  135. solo_update_mode(solo_enc);
  136. /* Make sure to bw check on first reader */
  137. if (!atomic_read(&solo_enc->readers)) {
  138. if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
  139. return -EBUSY;
  140. else
  141. solo_dev->enc_bw_remain -= solo_enc->bw_weight;
  142. }
  143. fh->enc_on = 1;
  144. fh->rd_idx = solo_enc->solo_dev->enc_wr_idx;
  145. if (fh->type == SOLO_ENC_TYPE_EXT)
  146. solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
  147. if (atomic_inc_return(&solo_enc->readers) > 1)
  148. return 0;
  149. /* Disable all encoding for this channel */
  150. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
  151. /* Common for both std and ext encoding */
  152. solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
  153. solo_enc->interlaced ? 1 : 0);
  154. if (solo_enc->interlaced)
  155. interval = solo_enc->interval - 1;
  156. else
  157. interval = solo_enc->interval;
  158. /* Standard encoding only */
  159. solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
  160. solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
  161. solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
  162. /* Extended encoding only */
  163. solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
  164. solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
  165. solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
  166. /* Enables the standard encoder */
  167. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
  168. /* Settle down Beavis... */
  169. mdelay(10);
  170. return 0;
  171. }
  172. static void solo_enc_off(struct solo_enc_fh *fh)
  173. {
  174. struct solo_enc_dev *solo_enc = fh->enc;
  175. struct solo_dev *solo_dev = solo_enc->solo_dev;
  176. if (!fh->enc_on)
  177. return;
  178. if (fh->kthread) {
  179. kthread_stop(fh->kthread);
  180. fh->kthread = NULL;
  181. }
  182. solo_dev->enc_bw_remain += solo_enc->bw_weight;
  183. fh->enc_on = 0;
  184. if (atomic_dec_return(&solo_enc->readers) > 0)
  185. return;
  186. solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
  187. solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
  188. }
  189. static int solo_start_fh_thread(struct solo_enc_fh *fh)
  190. {
  191. struct solo_enc_dev *solo_enc = fh->enc;
  192. fh->kthread = kthread_run(solo_enc_thread, fh, SOLO6X10_NAME "_enc");
  193. /* Oops, we had a problem */
  194. if (IS_ERR(fh->kthread)) {
  195. spin_lock(&solo_enc->lock);
  196. solo_enc_off(fh);
  197. spin_unlock(&solo_enc->lock);
  198. return PTR_ERR(fh->kthread);
  199. }
  200. return 0;
  201. }
  202. static void enc_reset_gop(struct solo_dev *solo_dev, u8 ch)
  203. {
  204. BUG_ON(ch >= solo_dev->nr_chans);
  205. solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), 1);
  206. solo_dev->v4l2_enc[ch]->reset_gop = 1;
  207. }
  208. static int enc_gop_reset(struct solo_dev *solo_dev, u8 ch, u8 vop)
  209. {
  210. BUG_ON(ch >= solo_dev->nr_chans);
  211. if (!solo_dev->v4l2_enc[ch]->reset_gop)
  212. return 0;
  213. if (vop)
  214. return 1;
  215. solo_dev->v4l2_enc[ch]->reset_gop = 0;
  216. solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch),
  217. solo_dev->v4l2_enc[ch]->gop);
  218. return 0;
  219. }
  220. static void enc_write_sg(struct scatterlist *sglist, void *buf, int size)
  221. {
  222. struct scatterlist *sg;
  223. u8 *src = buf;
  224. for (sg = sglist; sg && size > 0; sg = sg_next(sg)) {
  225. u8 *p = sg_virt(sg);
  226. size_t len = sg_dma_len(sg);
  227. int i;
  228. for (i = 0; i < len && size; i++)
  229. p[i] = *(src++);
  230. }
  231. }
  232. static int enc_get_mpeg_dma_sg(struct solo_dev *solo_dev,
  233. struct p2m_desc *desc,
  234. struct scatterlist *sglist, int skip,
  235. unsigned int off, unsigned int size)
  236. {
  237. int ret;
  238. if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
  239. return -EINVAL;
  240. if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
  241. return solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_MP4E,
  242. desc, 0, sglist, skip,
  243. SOLO_MP4E_EXT_ADDR(solo_dev) + off, size);
  244. }
  245. /* Buffer wrap */
  246. ret = solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_MP4E, desc, 0,
  247. sglist, skip, SOLO_MP4E_EXT_ADDR(solo_dev) + off,
  248. SOLO_MP4E_EXT_SIZE(solo_dev) - off);
  249. ret |= solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_MP4E, desc, 0,
  250. sglist, skip + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
  251. SOLO_MP4E_EXT_ADDR(solo_dev),
  252. size + off - SOLO_MP4E_EXT_SIZE(solo_dev));
  253. return ret;
  254. }
  255. static int enc_get_mpeg_dma_t(struct solo_dev *solo_dev,
  256. dma_addr_t buf, unsigned int off,
  257. unsigned int size)
  258. {
  259. int ret;
  260. if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
  261. return -EINVAL;
  262. if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
  263. return solo_p2m_dma_t(solo_dev, SOLO_P2M_DMA_ID_MP4E, 0, buf,
  264. SOLO_MP4E_EXT_ADDR(solo_dev) + off, size);
  265. }
  266. /* Buffer wrap */
  267. ret = solo_p2m_dma_t(solo_dev, SOLO_P2M_DMA_ID_MP4E, 0, buf,
  268. SOLO_MP4E_EXT_ADDR(solo_dev) + off,
  269. SOLO_MP4E_EXT_SIZE(solo_dev) - off);
  270. ret |= solo_p2m_dma_t(solo_dev, SOLO_P2M_DMA_ID_MP4E, 0,
  271. buf + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
  272. SOLO_MP4E_EXT_ADDR(solo_dev),
  273. size + off - SOLO_MP4E_EXT_SIZE(solo_dev));
  274. return ret;
  275. }
  276. static int enc_get_mpeg_dma(struct solo_dev *solo_dev, void *buf,
  277. unsigned int off, unsigned int size)
  278. {
  279. int ret;
  280. dma_addr_t dma_addr = pci_map_single(solo_dev->pdev, buf, size,
  281. PCI_DMA_FROMDEVICE);
  282. ret = enc_get_mpeg_dma_t(solo_dev, dma_addr, off, size);
  283. pci_unmap_single(solo_dev->pdev, dma_addr, size, PCI_DMA_FROMDEVICE);
  284. return ret;
  285. }
  286. static int enc_get_jpeg_dma_sg(struct solo_dev *solo_dev,
  287. struct p2m_desc *desc,
  288. struct scatterlist *sglist, int skip,
  289. unsigned int off, unsigned int size)
  290. {
  291. int ret;
  292. if (off > SOLO_JPEG_EXT_SIZE(solo_dev))
  293. return -EINVAL;
  294. if (off + size <= SOLO_JPEG_EXT_SIZE(solo_dev)) {
  295. return solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_JPEG,
  296. desc, 0, sglist, skip,
  297. SOLO_JPEG_EXT_ADDR(solo_dev) + off, size);
  298. }
  299. /* Buffer wrap */
  300. ret = solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_JPEG, desc, 0,
  301. sglist, skip, SOLO_JPEG_EXT_ADDR(solo_dev) + off,
  302. SOLO_JPEG_EXT_SIZE(solo_dev) - off);
  303. ret |= solo_p2m_dma_sg(solo_dev, SOLO_P2M_DMA_ID_JPEG, desc, 0,
  304. sglist, skip + SOLO_JPEG_EXT_SIZE(solo_dev) - off,
  305. SOLO_JPEG_EXT_ADDR(solo_dev),
  306. size + off - SOLO_JPEG_EXT_SIZE(solo_dev));
  307. return ret;
  308. }
  309. /* Returns true of __chk is within the first __range bytes of __off */
  310. #define OFF_IN_RANGE(__off, __range, __chk) \
  311. ((__off <= __chk) && ((__off + __range) >= __chk))
  312. static void solo_jpeg_header(struct solo_enc_dev *solo_enc,
  313. struct videobuf_dmabuf *vbuf)
  314. {
  315. struct scatterlist *sg;
  316. void *src = jpeg_header;
  317. size_t copied = 0;
  318. size_t to_copy = sizeof(jpeg_header);
  319. for (sg = vbuf->sglist; sg && copied < to_copy; sg = sg_next(sg)) {
  320. size_t this_copy = min(sg_dma_len(sg),
  321. (unsigned int)(to_copy - copied));
  322. u8 *p = sg_virt(sg);
  323. memcpy(p, src + copied, this_copy);
  324. if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 5))
  325. p[(SOF0_START + 5) - copied] =
  326. 0xff & (solo_enc->height >> 8);
  327. if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 6))
  328. p[(SOF0_START + 6) - copied] = 0xff & solo_enc->height;
  329. if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 7))
  330. p[(SOF0_START + 7) - copied] =
  331. 0xff & (solo_enc->width >> 8);
  332. if (OFF_IN_RANGE(copied, this_copy, SOF0_START + 8))
  333. p[(SOF0_START + 8) - copied] = 0xff & solo_enc->width;
  334. copied += this_copy;
  335. }
  336. }
  337. static int solo_fill_jpeg(struct solo_enc_fh *fh, struct solo_enc_buf *enc_buf,
  338. struct videobuf_buffer *vb,
  339. struct videobuf_dmabuf *vbuf)
  340. {
  341. struct solo_dev *solo_dev = fh->enc->solo_dev;
  342. int size = enc_buf->jpeg_size;
  343. /* Copy the header first (direct write) */
  344. solo_jpeg_header(fh->enc, vbuf);
  345. vb->size = size + sizeof(jpeg_header);
  346. /* Grab the jpeg frame */
  347. return enc_get_jpeg_dma_sg(solo_dev, fh->desc, vbuf->sglist,
  348. sizeof(jpeg_header),
  349. enc_buf->jpeg_off, size);
  350. }
  351. static inline int vop_interlaced(__le32 *vh)
  352. {
  353. return (__le32_to_cpu(vh[0]) >> 30) & 1;
  354. }
  355. static inline u32 vop_size(__le32 *vh)
  356. {
  357. return __le32_to_cpu(vh[0]) & 0xFFFFF;
  358. }
  359. static inline u8 vop_hsize(__le32 *vh)
  360. {
  361. return (__le32_to_cpu(vh[1]) >> 8) & 0xFF;
  362. }
  363. static inline u8 vop_vsize(__le32 *vh)
  364. {
  365. return __le32_to_cpu(vh[1]) & 0xFF;
  366. }
  367. /* must be called with *bits % 8 = 0 */
  368. static void write_bytes(u8 **out, unsigned *bits, const u8 *src, unsigned count)
  369. {
  370. memcpy(*out, src, count);
  371. *out += count;
  372. *bits += count * 8;
  373. }
  374. static void write_bits(u8 **out, unsigned *bits, u32 value, unsigned count)
  375. {
  376. value <<= 32 - count; // shift to the right
  377. while (count--) {
  378. **out <<= 1;
  379. **out |= !!(value & (1 << 31)); /* MSB */
  380. value <<= 1;
  381. if (++(*bits) % 8 == 0)
  382. (*out)++;
  383. }
  384. }
  385. static void write_ue(u8 **out, unsigned *bits, unsigned value) /* H.264 only */
  386. {
  387. uint32_t max = 0, cnt = 0;
  388. while (value > max) {
  389. max = (max + 2) * 2 - 2;
  390. cnt++;
  391. }
  392. write_bits(out, bits, 1, cnt + 1);
  393. write_bits(out, bits, ~(max - value), cnt);
  394. }
  395. static void write_se(u8 **out, unsigned *bits, int value) /* H.264 only */
  396. {
  397. if (value <= 0)
  398. write_ue(out, bits, -value * 2);
  399. else
  400. write_ue(out, bits, value * 2 - 1);
  401. }
  402. static void write_mpeg4_end(u8 **out, unsigned *bits)
  403. {
  404. write_bits(out, bits, 0, 1);
  405. /* align on 32-bit boundary */
  406. if (*bits % 32)
  407. write_bits(out, bits, 0xFFFFFFFF, 32 - *bits % 32);
  408. }
  409. static void write_h264_end(u8 **out, unsigned *bits, int align)
  410. {
  411. write_bits(out, bits, 1, 1);
  412. while ((*bits) % 8)
  413. write_bits(out, bits, 0, 1);
  414. if (align)
  415. while ((*bits) % 32)
  416. write_bits(out, bits, 0, 1);
  417. }
  418. static void mpeg4_write_vol(u8 **out, struct solo_dev *solo_dev,
  419. __le32 *vh, unsigned fps, unsigned interval)
  420. {
  421. static const u8 hdr[] = {
  422. 0, 0, 1, 0x00 /* video_object_start_code */,
  423. 0, 0, 1, 0x20 /* video_object_layer_start_code */
  424. };
  425. unsigned bits = 0;
  426. unsigned width = vop_hsize(vh) << 4;
  427. unsigned height = vop_vsize(vh) << 4;
  428. unsigned interlaced = vop_interlaced(vh);
  429. write_bytes(out, &bits, hdr, sizeof(hdr));
  430. write_bits(out, &bits, 0, 1); /* random_accessible_vol */
  431. write_bits(out, &bits, 0x04, 8); /* video_object_type_indication: main */
  432. write_bits(out, &bits, 1, 1); /* is_object_layer_identifier */
  433. write_bits(out, &bits, 2, 4); /* video_object_layer_verid: table V2-39 */
  434. write_bits(out, &bits, 0, 3); /* video_object_layer_priority */
  435. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
  436. write_bits(out, &bits, 3, 4); /* aspect_ratio_info, assuming 4:3 */
  437. else
  438. write_bits(out, &bits, 2, 4);
  439. write_bits(out, &bits, 1, 1); /* vol_control_parameters */
  440. write_bits(out, &bits, 1, 2); /* chroma_format: 4:2:0 */
  441. write_bits(out, &bits, 1, 1); /* low_delay */
  442. write_bits(out, &bits, 0, 1); /* vbv_parameters */
  443. write_bits(out, &bits, 0, 2); /* video_object_layer_shape: rectangular */
  444. write_bits(out, &bits, 1, 1); /* marker_bit */
  445. write_bits(out, &bits, fps, 16); /* vop_time_increment_resolution */
  446. write_bits(out, &bits, 1, 1); /* marker_bit */
  447. write_bits(out, &bits, 1, 1); /* fixed_vop_rate */
  448. write_bits(out, &bits, interval, 15); /* fixed_vop_time_increment */
  449. write_bits(out, &bits, 1, 1); /* marker_bit */
  450. write_bits(out, &bits, width, 13); /* video_object_layer_width */
  451. write_bits(out, &bits, 1, 1); /* marker_bit */
  452. write_bits(out, &bits, height, 13); /* video_object_layer_height */
  453. write_bits(out, &bits, 1, 1); /* marker_bit */
  454. write_bits(out, &bits, interlaced, 1); /* interlaced */
  455. write_bits(out, &bits, 1, 1); /* obmc_disable */
  456. write_bits(out, &bits, 0, 2); /* sprite_enable */
  457. write_bits(out, &bits, 0, 1); /* not_8_bit */
  458. write_bits(out, &bits, 1, 0); /* quant_type */
  459. write_bits(out, &bits, 0, 1); /* load_intra_quant_mat */
  460. write_bits(out, &bits, 0, 1); /* load_nonintra_quant_mat */
  461. write_bits(out, &bits, 0, 1); /* quarter_sample */
  462. write_bits(out, &bits, 1, 1); /* complexity_estimation_disable */
  463. write_bits(out, &bits, 1, 1); /* resync_marker_disable */
  464. write_bits(out, &bits, 0, 1); /* data_partitioned */
  465. write_bits(out, &bits, 0, 1); /* newpred_enable */
  466. write_bits(out, &bits, 0, 1); /* reduced_resolution_vop_enable */
  467. write_bits(out, &bits, 0, 1); /* scalability */
  468. write_mpeg4_end(out, &bits);
  469. }
  470. static void h264_write_vol(u8 **out, struct solo_dev *solo_dev, __le32 *vh)
  471. {
  472. static const u8 sps[] = {
  473. 0, 0, 0, 1 /* start code */, 0x67, 66 /* profile_idc */,
  474. 0 /* constraints */, 30 /* level_idc */
  475. };
  476. static const u8 pps[] = {
  477. 0, 0, 0, 1 /* start code */, 0x68
  478. };
  479. unsigned bits = 0;
  480. unsigned mbs_w = vop_hsize(vh);
  481. unsigned mbs_h = vop_vsize(vh);
  482. write_bytes(out, &bits, sps, sizeof(sps));
  483. write_ue(out, &bits, 0); /* seq_parameter_set_id */
  484. write_ue(out, &bits, 5); /* log2_max_frame_num_minus4 */
  485. write_ue(out, &bits, 0); /* pic_order_cnt_type */
  486. write_ue(out, &bits, 6); /* log2_max_pic_order_cnt_lsb_minus4 */
  487. write_ue(out, &bits, 1); /* max_num_ref_frames */
  488. write_bits(out, &bits, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
  489. write_ue(out, &bits, mbs_w - 1); /* pic_width_in_mbs_minus1 */
  490. write_ue(out, &bits, mbs_h - 1); /* pic_height_in_map_units_minus1 */
  491. write_bits(out, &bits, 1, 1); /* frame_mbs_only_flag */
  492. write_bits(out, &bits, 1, 1); /* direct_8x8_frame_field_flag */
  493. write_bits(out, &bits, 0, 1); /* frame_cropping_flag */
  494. write_bits(out, &bits, 0, 1); /* vui_parameters_present_flag */
  495. write_h264_end(out, &bits, 0);
  496. write_bytes(out, &bits, pps, sizeof(pps));
  497. write_ue(out, &bits, 0); /* pic_parameter_set_id */
  498. write_ue(out, &bits, 0); /* seq_parameter_set_id */
  499. write_bits(out, &bits, 0, 1); /* entropy_coding_mode_flag */
  500. write_bits(out, &bits, 0, 1); /* bottom_field_pic_order_in_frame_present_flag */
  501. write_ue(out, &bits, 0); /* num_slice_groups_minus1 */
  502. write_ue(out, &bits, 0); /* num_ref_idx_l0_default_active_minus1 */
  503. write_ue(out, &bits, 0); /* num_ref_idx_l1_default_active_minus1 */
  504. write_bits(out, &bits, 0, 1); /* weighted_pred_flag */
  505. write_bits(out, &bits, 0, 2); /* weighted_bipred_idc */
  506. write_se(out, &bits, 0); /* pic_init_qp_minus26 */
  507. write_se(out, &bits, 0); /* pic_init_qs_minus26 */
  508. write_se(out, &bits, 2); /* chroma_qp_index_offset */
  509. write_bits(out, &bits, 0, 1); /* deblocking_filter_control_present_flag */
  510. write_bits(out, &bits, 1, 1); /* constrained_intra_pred_flag */
  511. write_bits(out, &bits, 0, 1); /* redundant_pic_cnt_present_flag */
  512. write_h264_end(out, &bits, 1);
  513. }
  514. static int solo_fill_mpeg(struct solo_enc_fh *fh, struct solo_enc_buf *enc_buf,
  515. struct videobuf_buffer *vb,
  516. struct videobuf_dmabuf *vbuf)
  517. {
  518. struct solo_enc_dev *solo_enc = fh->enc;
  519. struct solo_dev *solo_dev = solo_enc->solo_dev;
  520. #define VH_WORDS 16
  521. #define MAX_VOL_HEADER_LENGTH 64
  522. __le32 vh[VH_WORDS];
  523. int ret;
  524. int frame_size, frame_off;
  525. int skip = 0;
  526. if (WARN_ON_ONCE(enc_buf->size <= sizeof(vh)))
  527. return -EINVAL;
  528. /* First get the hardware vop header (not real mpeg) */
  529. ret = enc_get_mpeg_dma(solo_dev, vh, enc_buf->off, sizeof(vh));
  530. if (WARN_ON_ONCE(ret))
  531. return ret;
  532. if (WARN_ON_ONCE(vop_size(vh) > enc_buf->size))
  533. return -EINVAL;
  534. vb->width = vop_hsize(vh) << 4;
  535. vb->height = vop_vsize(vh) << 4;
  536. vb->size = vop_size(vh);
  537. /* If this is a key frame, add extra m4v header */
  538. if (!enc_buf->vop) {
  539. u8 header[MAX_VOL_HEADER_LENGTH], *out = header;
  540. if (solo_dev->flags & FLAGS_6110)
  541. h264_write_vol(&out, solo_dev, vh);
  542. else
  543. mpeg4_write_vol(&out, solo_dev, vh,
  544. solo_dev->fps * 1000,
  545. solo_enc->interval * 1000);
  546. skip = out - header;
  547. enc_write_sg(vbuf->sglist, header, skip);
  548. /* Adjust the dma buffer past this header */
  549. vb->size += skip;
  550. }
  551. /* Now get the actual mpeg payload */
  552. frame_off = (enc_buf->off + sizeof(vh)) % SOLO_MP4E_EXT_SIZE(solo_dev);
  553. frame_size = enc_buf->size - sizeof(vh);
  554. ret = enc_get_mpeg_dma_sg(solo_dev, fh->desc, vbuf->sglist,
  555. skip, frame_off, frame_size);
  556. WARN_ON_ONCE(ret);
  557. return ret;
  558. }
  559. static void solo_enc_fillbuf(struct solo_enc_fh *fh,
  560. struct videobuf_buffer *vb)
  561. {
  562. struct solo_enc_dev *solo_enc = fh->enc;
  563. struct solo_dev *solo_dev = solo_enc->solo_dev;
  564. struct solo_enc_buf *enc_buf = NULL;
  565. struct videobuf_dmabuf *vbuf;
  566. int ret;
  567. int error = 1;
  568. u16 idx = fh->rd_idx;
  569. while (idx != solo_dev->enc_wr_idx) {
  570. struct solo_enc_buf *ebuf = &solo_dev->enc_buf[idx];
  571. idx = (idx + 1) % SOLO_NR_RING_BUFS;
  572. if (ebuf->ch != solo_enc->ch)
  573. continue;
  574. if (fh->fmt == V4L2_PIX_FMT_MPEG) {
  575. if (fh->type == ebuf->type) {
  576. enc_buf = ebuf;
  577. break;
  578. }
  579. } else {
  580. /* For mjpeg, keep reading to the newest frame */
  581. enc_buf = ebuf;
  582. }
  583. }
  584. fh->rd_idx = idx;
  585. if (WARN_ON_ONCE(!enc_buf))
  586. goto buf_err;
  587. if ((fh->fmt == V4L2_PIX_FMT_MPEG &&
  588. vb->bsize < enc_buf->size) ||
  589. (fh->fmt == V4L2_PIX_FMT_MJPEG &&
  590. vb->bsize < (enc_buf->jpeg_size + sizeof(jpeg_header)))) {
  591. WARN_ON_ONCE(1);
  592. goto buf_err;
  593. }
  594. vbuf = videobuf_to_dma(vb);
  595. if (WARN_ON_ONCE(!vbuf))
  596. goto buf_err;
  597. if (fh->fmt == V4L2_PIX_FMT_MPEG)
  598. ret = solo_fill_mpeg(fh, enc_buf, vb, vbuf);
  599. else
  600. ret = solo_fill_jpeg(fh, enc_buf, vb, vbuf);
  601. if (!ret)
  602. error = 0;
  603. buf_err:
  604. if (error) {
  605. vb->state = VIDEOBUF_ERROR;
  606. } else {
  607. vb->field_count++;
  608. vb->ts = enc_buf->ts;
  609. vb->state = VIDEOBUF_DONE;
  610. }
  611. wake_up(&vb->done);
  612. return;
  613. }
  614. static void solo_enc_thread_try(struct solo_enc_fh *fh)
  615. {
  616. struct solo_enc_dev *solo_enc = fh->enc;
  617. struct solo_dev *solo_dev = solo_enc->solo_dev;
  618. struct videobuf_buffer *vb;
  619. for (;;) {
  620. spin_lock(&solo_enc->lock);
  621. if (fh->rd_idx == solo_dev->enc_wr_idx)
  622. break;
  623. if (list_empty(&fh->vidq_active))
  624. break;
  625. vb = list_first_entry(&fh->vidq_active,
  626. struct videobuf_buffer, queue);
  627. if (!waitqueue_active(&vb->done))
  628. break;
  629. list_del(&vb->queue);
  630. spin_unlock(&solo_enc->lock);
  631. solo_enc_fillbuf(fh, vb);
  632. }
  633. assert_spin_locked(&solo_enc->lock);
  634. spin_unlock(&solo_enc->lock);
  635. }
  636. static int solo_enc_thread(void *data)
  637. {
  638. struct solo_enc_fh *fh = data;
  639. struct solo_enc_dev *solo_enc = fh->enc;
  640. DECLARE_WAITQUEUE(wait, current);
  641. set_freezable();
  642. add_wait_queue(&solo_enc->thread_wait, &wait);
  643. for (;;) {
  644. long timeout = schedule_timeout_interruptible(HZ);
  645. if (timeout == -ERESTARTSYS || kthread_should_stop())
  646. break;
  647. solo_enc_thread_try(fh);
  648. try_to_freeze();
  649. }
  650. remove_wait_queue(&solo_enc->thread_wait, &wait);
  651. return 0;
  652. }
  653. void solo_motion_isr(struct solo_dev *solo_dev)
  654. {
  655. u32 status;
  656. int i;
  657. solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_MOTION);
  658. status = solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS);
  659. for (i = 0; i < solo_dev->nr_chans; i++) {
  660. struct solo_enc_dev *solo_enc = solo_dev->v4l2_enc[i];
  661. BUG_ON(solo_enc == NULL);
  662. if (solo_enc->motion_detected)
  663. continue;
  664. if (!(status & (1 << i)))
  665. continue;
  666. solo_enc->motion_detected = 1;
  667. }
  668. }
  669. void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
  670. {
  671. struct solo_enc_buf *enc_buf;
  672. u32 mpeg_current, mpeg_next, mpeg_size;
  673. u32 jpeg_current, jpeg_next, jpeg_size;
  674. u32 reg_mpeg_size;
  675. u8 cur_q, vop_type;
  676. u8 ch;
  677. enum solo_enc_types enc_type;
  678. solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_ENCODER);
  679. cur_q = ((solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xF) + 1) % MP4_QS;
  680. reg_mpeg_size = ((solo_reg_read(solo_dev, SOLO_VE_STATE(0)) & 0xFFFFF) + 64 + 8) & ~7;
  681. while (solo_dev->enc_idx != cur_q) {
  682. mpeg_current = solo_reg_read(solo_dev,
  683. SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
  684. jpeg_current = solo_reg_read(solo_dev,
  685. SOLO_VE_JPEG_QUE(solo_dev->enc_idx));
  686. solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
  687. mpeg_next = solo_reg_read(solo_dev,
  688. SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
  689. jpeg_next = solo_reg_read(solo_dev,
  690. SOLO_VE_JPEG_QUE(solo_dev->enc_idx));
  691. ch = (mpeg_current >> 24) & 0x1f;
  692. if (ch >= SOLO_MAX_CHANNELS) {
  693. ch -= SOLO_MAX_CHANNELS;
  694. enc_type = SOLO_ENC_TYPE_EXT;
  695. } else
  696. enc_type = SOLO_ENC_TYPE_STD;
  697. vop_type = (mpeg_current >> 29) & 3;
  698. mpeg_current &= 0x00ffffff;
  699. mpeg_next &= 0x00ffffff;
  700. jpeg_current &= 0x00ffffff;
  701. jpeg_next &= 0x00ffffff;
  702. mpeg_size = (SOLO_MP4E_EXT_SIZE(solo_dev) +
  703. mpeg_next - mpeg_current) %
  704. SOLO_MP4E_EXT_SIZE(solo_dev);
  705. jpeg_size = (SOLO_JPEG_EXT_SIZE(solo_dev) +
  706. jpeg_next - jpeg_current) %
  707. SOLO_JPEG_EXT_SIZE(solo_dev);
  708. /* XXX I think this means we had a ring overflow? */
  709. if (mpeg_current > mpeg_next && mpeg_size != reg_mpeg_size) {
  710. enc_reset_gop(solo_dev, ch);
  711. continue;
  712. }
  713. /* When resetting the GOP, skip frames until I-frame */
  714. if (enc_gop_reset(solo_dev, ch, vop_type))
  715. continue;
  716. enc_buf = &solo_dev->enc_buf[solo_dev->enc_wr_idx];
  717. enc_buf->vop = vop_type;
  718. enc_buf->ch = ch;
  719. enc_buf->off = mpeg_current;
  720. enc_buf->size = mpeg_size;
  721. enc_buf->jpeg_off = jpeg_current;
  722. enc_buf->jpeg_size = jpeg_size;
  723. enc_buf->type = enc_type;
  724. do_gettimeofday(&enc_buf->ts);
  725. solo_dev->enc_wr_idx = (solo_dev->enc_wr_idx + 1) %
  726. SOLO_NR_RING_BUFS;
  727. wake_up_interruptible(&solo_dev->v4l2_enc[ch]->thread_wait);
  728. }
  729. return;
  730. }
  731. static int solo_enc_buf_setup(struct videobuf_queue *vq, unsigned int *count,
  732. unsigned int *size)
  733. {
  734. *size = FRAME_BUF_SIZE;
  735. if (*count < MIN_VID_BUFFERS)
  736. *count = MIN_VID_BUFFERS;
  737. return 0;
  738. }
  739. static int solo_enc_buf_prepare(struct videobuf_queue *vq,
  740. struct videobuf_buffer *vb,
  741. enum v4l2_field field)
  742. {
  743. struct solo_enc_fh *fh = vq->priv_data;
  744. struct solo_enc_dev *solo_enc = fh->enc;
  745. vb->size = FRAME_BUF_SIZE;
  746. if (vb->baddr != 0 && vb->bsize < vb->size)
  747. return -EINVAL;
  748. /* These properties only change when queue is idle */
  749. vb->width = solo_enc->width;
  750. vb->height = solo_enc->height;
  751. vb->field = field;
  752. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  753. int rc = videobuf_iolock(vq, vb, NULL);
  754. if (rc < 0) {
  755. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  756. videobuf_dma_unmap(vq->dev, dma);
  757. videobuf_dma_free(dma);
  758. vb->state = VIDEOBUF_NEEDS_INIT;
  759. return rc;
  760. }
  761. }
  762. vb->state = VIDEOBUF_PREPARED;
  763. return 0;
  764. }
  765. static void solo_enc_buf_queue(struct videobuf_queue *vq,
  766. struct videobuf_buffer *vb)
  767. {
  768. struct solo_enc_fh *fh = vq->priv_data;
  769. vb->state = VIDEOBUF_QUEUED;
  770. list_add_tail(&vb->queue, &fh->vidq_active);
  771. wake_up_interruptible(&fh->enc->thread_wait);
  772. }
  773. static void solo_enc_buf_release(struct videobuf_queue *vq,
  774. struct videobuf_buffer *vb)
  775. {
  776. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  777. videobuf_dma_unmap(vq->dev, dma);
  778. videobuf_dma_free(dma);
  779. vb->state = VIDEOBUF_NEEDS_INIT;
  780. }
  781. static struct videobuf_queue_ops solo_enc_video_qops = {
  782. .buf_setup = solo_enc_buf_setup,
  783. .buf_prepare = solo_enc_buf_prepare,
  784. .buf_queue = solo_enc_buf_queue,
  785. .buf_release = solo_enc_buf_release,
  786. };
  787. static unsigned int solo_enc_poll(struct file *file,
  788. struct poll_table_struct *wait)
  789. {
  790. struct solo_enc_fh *fh = file->private_data;
  791. return videobuf_poll_stream(file, &fh->vidq, wait);
  792. }
  793. static int solo_enc_mmap(struct file *file, struct vm_area_struct *vma)
  794. {
  795. struct solo_enc_fh *fh = file->private_data;
  796. return videobuf_mmap_mapper(&fh->vidq, vma);
  797. }
  798. static int solo_enc_open(struct file *file)
  799. {
  800. struct solo_enc_dev *solo_enc = video_drvdata(file);
  801. struct solo_enc_fh *fh;
  802. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  803. if (fh == NULL)
  804. return -ENOMEM;
  805. fh->enc = solo_enc;
  806. file->private_data = fh;
  807. INIT_LIST_HEAD(&fh->vidq_active);
  808. fh->fmt = V4L2_PIX_FMT_MPEG;
  809. fh->type = SOLO_ENC_TYPE_STD;
  810. videobuf_queue_sg_init(&fh->vidq, &solo_enc_video_qops,
  811. &solo_enc->solo_dev->pdev->dev,
  812. &solo_enc->lock,
  813. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  814. V4L2_FIELD_INTERLACED,
  815. sizeof(struct videobuf_buffer), fh, NULL);
  816. return 0;
  817. }
  818. static ssize_t solo_enc_read(struct file *file, char __user *data,
  819. size_t count, loff_t *ppos)
  820. {
  821. struct solo_enc_fh *fh = file->private_data;
  822. struct solo_enc_dev *solo_enc = fh->enc;
  823. /* Make sure the encoder is on */
  824. if (!fh->enc_on) {
  825. int ret;
  826. spin_lock(&solo_enc->lock);
  827. ret = solo_enc_on(fh);
  828. spin_unlock(&solo_enc->lock);
  829. if (ret)
  830. return ret;
  831. ret = solo_start_fh_thread(fh);
  832. if (ret)
  833. return ret;
  834. }
  835. return videobuf_read_stream(&fh->vidq, data, count, ppos, 0,
  836. file->f_flags & O_NONBLOCK);
  837. }
  838. static int solo_enc_release(struct file *file)
  839. {
  840. struct solo_enc_fh *fh = file->private_data;
  841. struct solo_enc_dev *solo_enc = fh->enc;
  842. videobuf_stop(&fh->vidq);
  843. videobuf_mmap_free(&fh->vidq);
  844. spin_lock(&solo_enc->lock);
  845. solo_enc_off(fh);
  846. spin_unlock(&solo_enc->lock);
  847. kfree(fh);
  848. return 0;
  849. }
  850. static int solo_enc_querycap(struct file *file, void *priv,
  851. struct v4l2_capability *cap)
  852. {
  853. struct solo_enc_fh *fh = priv;
  854. struct solo_enc_dev *solo_enc = fh->enc;
  855. struct solo_dev *solo_dev = solo_enc->solo_dev;
  856. strcpy(cap->driver, SOLO6X10_NAME);
  857. snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
  858. solo_enc->ch);
  859. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI %s",
  860. pci_name(solo_dev->pdev));
  861. cap->version = SOLO6X10_VER_NUM;
  862. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  863. V4L2_CAP_READWRITE |
  864. V4L2_CAP_STREAMING;
  865. return 0;
  866. }
  867. static int solo_enc_enum_input(struct file *file, void *priv,
  868. struct v4l2_input *input)
  869. {
  870. struct solo_enc_fh *fh = priv;
  871. struct solo_enc_dev *solo_enc = fh->enc;
  872. struct solo_dev *solo_dev = solo_enc->solo_dev;
  873. if (input->index)
  874. return -EINVAL;
  875. snprintf(input->name, sizeof(input->name), "Encoder %d",
  876. solo_enc->ch + 1);
  877. input->type = V4L2_INPUT_TYPE_CAMERA;
  878. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
  879. input->std = V4L2_STD_NTSC_M;
  880. else
  881. input->std = V4L2_STD_PAL_B;
  882. if (!tw28_get_video_status(solo_dev, solo_enc->ch))
  883. input->status = V4L2_IN_ST_NO_SIGNAL;
  884. return 0;
  885. }
  886. static int solo_enc_set_input(struct file *file, void *priv, unsigned int index)
  887. {
  888. if (index)
  889. return -EINVAL;
  890. return 0;
  891. }
  892. static int solo_enc_get_input(struct file *file, void *priv,
  893. unsigned int *index)
  894. {
  895. *index = 0;
  896. return 0;
  897. }
  898. static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
  899. struct v4l2_fmtdesc *f)
  900. {
  901. switch (f->index) {
  902. case 0:
  903. f->pixelformat = V4L2_PIX_FMT_MPEG;
  904. strcpy(f->description, "MPEG-4 AVC");
  905. break;
  906. case 1:
  907. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  908. strcpy(f->description, "MJPEG");
  909. break;
  910. default:
  911. return -EINVAL;
  912. }
  913. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  914. return 0;
  915. }
  916. static int solo_enc_try_fmt_cap(struct file *file, void *priv,
  917. struct v4l2_format *f)
  918. {
  919. struct solo_enc_fh *fh = priv;
  920. struct solo_enc_dev *solo_enc = fh->enc;
  921. struct solo_dev *solo_dev = solo_enc->solo_dev;
  922. struct v4l2_pix_format *pix = &f->fmt.pix;
  923. if (pix->pixelformat != V4L2_PIX_FMT_MPEG &&
  924. pix->pixelformat != V4L2_PIX_FMT_MJPEG)
  925. return -EINVAL;
  926. /* We cannot change width/height in mid read */
  927. if (atomic_read(&solo_enc->readers) > 0) {
  928. if (pix->width != solo_enc->width ||
  929. pix->height != solo_enc->height)
  930. return -EBUSY;
  931. }
  932. if (pix->width < solo_dev->video_hsize ||
  933. pix->height < solo_dev->video_vsize << 1) {
  934. /* Default to CIF 1/2 size */
  935. pix->width = solo_dev->video_hsize >> 1;
  936. pix->height = solo_dev->video_vsize;
  937. } else {
  938. /* Full frame */
  939. pix->width = solo_dev->video_hsize;
  940. pix->height = solo_dev->video_vsize << 1;
  941. }
  942. if (pix->field == V4L2_FIELD_ANY)
  943. pix->field = V4L2_FIELD_INTERLACED;
  944. else if (pix->field != V4L2_FIELD_INTERLACED)
  945. pix->field = V4L2_FIELD_INTERLACED;
  946. /* Just set these */
  947. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  948. pix->sizeimage = FRAME_BUF_SIZE;
  949. return 0;
  950. }
  951. static int solo_enc_set_fmt_cap(struct file *file, void *priv,
  952. struct v4l2_format *f)
  953. {
  954. struct solo_enc_fh *fh = priv;
  955. struct solo_enc_dev *solo_enc = fh->enc;
  956. struct solo_dev *solo_dev = solo_enc->solo_dev;
  957. struct v4l2_pix_format *pix = &f->fmt.pix;
  958. int ret;
  959. spin_lock(&solo_enc->lock);
  960. ret = solo_enc_try_fmt_cap(file, priv, f);
  961. if (ret) {
  962. spin_unlock(&solo_enc->lock);
  963. return ret;
  964. }
  965. if (pix->width == solo_dev->video_hsize)
  966. solo_enc->mode = SOLO_ENC_MODE_D1;
  967. else
  968. solo_enc->mode = SOLO_ENC_MODE_CIF;
  969. /* This does not change the encoder at all */
  970. fh->fmt = pix->pixelformat;
  971. if (pix->priv)
  972. fh->type = SOLO_ENC_TYPE_EXT;
  973. ret = solo_enc_on(fh);
  974. spin_unlock(&solo_enc->lock);
  975. if (ret)
  976. return ret;
  977. return solo_start_fh_thread(fh);
  978. }
  979. static int solo_enc_get_fmt_cap(struct file *file, void *priv,
  980. struct v4l2_format *f)
  981. {
  982. struct solo_enc_fh *fh = priv;
  983. struct solo_enc_dev *solo_enc = fh->enc;
  984. struct v4l2_pix_format *pix = &f->fmt.pix;
  985. pix->width = solo_enc->width;
  986. pix->height = solo_enc->height;
  987. pix->pixelformat = fh->fmt;
  988. pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
  989. V4L2_FIELD_NONE;
  990. pix->sizeimage = FRAME_BUF_SIZE;
  991. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  992. return 0;
  993. }
  994. static int solo_enc_reqbufs(struct file *file, void *priv,
  995. struct v4l2_requestbuffers *req)
  996. {
  997. struct solo_enc_fh *fh = priv;
  998. return videobuf_reqbufs(&fh->vidq, req);
  999. }
  1000. static int solo_enc_querybuf(struct file *file, void *priv,
  1001. struct v4l2_buffer *buf)
  1002. {
  1003. struct solo_enc_fh *fh = priv;
  1004. return videobuf_querybuf(&fh->vidq, buf);
  1005. }
  1006. static int solo_enc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  1007. {
  1008. struct solo_enc_fh *fh = priv;
  1009. return videobuf_qbuf(&fh->vidq, buf);
  1010. }
  1011. static int solo_enc_dqbuf(struct file *file, void *priv,
  1012. struct v4l2_buffer *buf)
  1013. {
  1014. struct solo_enc_fh *fh = priv;
  1015. struct solo_enc_dev *solo_enc = fh->enc;
  1016. int ret;
  1017. /* Make sure the encoder is on */
  1018. if (!fh->enc_on) {
  1019. spin_lock(&solo_enc->lock);
  1020. ret = solo_enc_on(fh);
  1021. spin_unlock(&solo_enc->lock);
  1022. if (ret)
  1023. return ret;
  1024. ret = solo_start_fh_thread(fh);
  1025. if (ret)
  1026. return ret;
  1027. }
  1028. ret = videobuf_dqbuf(&fh->vidq, buf, file->f_flags & O_NONBLOCK);
  1029. if (ret)
  1030. return ret;
  1031. /* Signal motion detection */
  1032. if (solo_is_motion_on(solo_enc)) {
  1033. buf->flags |= V4L2_BUF_FLAG_MOTION_ON;
  1034. if (solo_enc->motion_detected) {
  1035. buf->flags |= V4L2_BUF_FLAG_MOTION_DETECTED;
  1036. solo_reg_write(solo_enc->solo_dev, SOLO_VI_MOT_CLEAR,
  1037. 1 << solo_enc->ch);
  1038. solo_enc->motion_detected = 0;
  1039. }
  1040. }
  1041. /* Check for key frame on mpeg data */
  1042. if (fh->fmt == V4L2_PIX_FMT_MPEG) {
  1043. struct videobuf_dmabuf *vbuf =
  1044. videobuf_to_dma(fh->vidq.bufs[buf->index]);
  1045. if (vbuf) {
  1046. u8 *p = sg_virt(vbuf->sglist);
  1047. if (p[3] == 0x00)
  1048. buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
  1049. else
  1050. buf->flags |= V4L2_BUF_FLAG_PFRAME;
  1051. }
  1052. }
  1053. return 0;
  1054. }
  1055. static int solo_enc_streamon(struct file *file, void *priv,
  1056. enum v4l2_buf_type i)
  1057. {
  1058. struct solo_enc_fh *fh = priv;
  1059. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1060. return -EINVAL;
  1061. return videobuf_streamon(&fh->vidq);
  1062. }
  1063. static int solo_enc_streamoff(struct file *file, void *priv,
  1064. enum v4l2_buf_type i)
  1065. {
  1066. struct solo_enc_fh *fh = priv;
  1067. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1068. return -EINVAL;
  1069. return videobuf_streamoff(&fh->vidq);
  1070. }
  1071. static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id *i)
  1072. {
  1073. return 0;
  1074. }
  1075. static int solo_enum_framesizes(struct file *file, void *priv,
  1076. struct v4l2_frmsizeenum *fsize)
  1077. {
  1078. struct solo_enc_fh *fh = priv;
  1079. struct solo_dev *solo_dev = fh->enc->solo_dev;
  1080. if (fsize->pixel_format != V4L2_PIX_FMT_MPEG)
  1081. return -EINVAL;
  1082. switch (fsize->index) {
  1083. case 0:
  1084. fsize->discrete.width = solo_dev->video_hsize >> 1;
  1085. fsize->discrete.height = solo_dev->video_vsize;
  1086. break;
  1087. case 1:
  1088. fsize->discrete.width = solo_dev->video_hsize;
  1089. fsize->discrete.height = solo_dev->video_vsize << 1;
  1090. break;
  1091. default:
  1092. return -EINVAL;
  1093. }
  1094. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1095. return 0;
  1096. }
  1097. static int solo_enum_frameintervals(struct file *file, void *priv,
  1098. struct v4l2_frmivalenum *fintv)
  1099. {
  1100. struct solo_enc_fh *fh = priv;
  1101. struct solo_dev *solo_dev = fh->enc->solo_dev;
  1102. if (fintv->pixel_format != V4L2_PIX_FMT_MPEG || fintv->index)
  1103. return -EINVAL;
  1104. fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
  1105. fintv->stepwise.min.numerator = solo_dev->fps;
  1106. fintv->stepwise.min.denominator = 1;
  1107. fintv->stepwise.max.numerator = solo_dev->fps;
  1108. fintv->stepwise.max.denominator = 15;
  1109. fintv->stepwise.step.numerator = 1;
  1110. fintv->stepwise.step.denominator = 1;
  1111. return 0;
  1112. }
  1113. static int solo_g_parm(struct file *file, void *priv,
  1114. struct v4l2_streamparm *sp)
  1115. {
  1116. struct solo_enc_fh *fh = priv;
  1117. struct solo_enc_dev *solo_enc = fh->enc;
  1118. struct solo_dev *solo_dev = solo_enc->solo_dev;
  1119. struct v4l2_captureparm *cp = &sp->parm.capture;
  1120. cp->capability = V4L2_CAP_TIMEPERFRAME;
  1121. cp->timeperframe.numerator = solo_enc->interval;
  1122. cp->timeperframe.denominator = solo_dev->fps;
  1123. cp->capturemode = 0;
  1124. /* XXX: Shouldn't we be able to get/set this from videobuf? */
  1125. cp->readbuffers = 2;
  1126. return 0;
  1127. }
  1128. static int solo_s_parm(struct file *file, void *priv,
  1129. struct v4l2_streamparm *sp)
  1130. {
  1131. struct solo_enc_fh *fh = priv;
  1132. struct solo_enc_dev *solo_enc = fh->enc;
  1133. struct solo_dev *solo_dev = solo_enc->solo_dev;
  1134. struct v4l2_captureparm *cp = &sp->parm.capture;
  1135. spin_lock(&solo_enc->lock);
  1136. if (atomic_read(&solo_enc->readers) > 0) {
  1137. spin_unlock(&solo_enc->lock);
  1138. return -EBUSY;
  1139. }
  1140. if ((cp->timeperframe.numerator == 0) ||
  1141. (cp->timeperframe.denominator == 0)) {
  1142. /* reset framerate */
  1143. cp->timeperframe.numerator = 1;
  1144. cp->timeperframe.denominator = solo_dev->fps;
  1145. }
  1146. if (cp->timeperframe.denominator != solo_dev->fps)
  1147. cp->timeperframe.denominator = solo_dev->fps;
  1148. if (cp->timeperframe.numerator > 15)
  1149. cp->timeperframe.numerator = 15;
  1150. solo_enc->interval = cp->timeperframe.numerator;
  1151. cp->capability = V4L2_CAP_TIMEPERFRAME;
  1152. solo_enc->gop = max(solo_dev->fps / solo_enc->interval, 1);
  1153. solo_update_mode(solo_enc);
  1154. spin_unlock(&solo_enc->lock);
  1155. return 0;
  1156. }
  1157. static int solo_queryctrl(struct file *file, void *priv,
  1158. struct v4l2_queryctrl *qc)
  1159. {
  1160. struct solo_enc_fh *fh = priv;
  1161. struct solo_enc_dev *solo_enc = fh->enc;
  1162. struct solo_dev *solo_dev = solo_enc->solo_dev;
  1163. qc->id = v4l2_ctrl_next(solo_ctrl_classes, qc->id);
  1164. if (!qc->id)
  1165. return -EINVAL;
  1166. switch (qc->id) {
  1167. case V4L2_CID_BRIGHTNESS:
  1168. case V4L2_CID_CONTRAST:
  1169. case V4L2_CID_SATURATION:
  1170. case V4L2_CID_HUE:
  1171. return v4l2_ctrl_query_fill(qc, 0x00, 0xff, 1, 0x80);
  1172. case V4L2_CID_SHARPNESS:
  1173. return v4l2_ctrl_query_fill(qc, 0x00, 0x0f, 1, 0x00);
  1174. case V4L2_CID_MPEG_VIDEO_ENCODING:
  1175. return v4l2_ctrl_query_fill(
  1176. qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
  1177. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
  1178. V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
  1179. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1180. return v4l2_ctrl_query_fill(qc, 1, 255, 1, solo_dev->fps);
  1181. #ifdef PRIVATE_CIDS
  1182. case V4L2_CID_MOTION_THRESHOLD:
  1183. qc->flags |= V4L2_CTRL_FLAG_SLIDER;
  1184. qc->type = V4L2_CTRL_TYPE_INTEGER;
  1185. qc->minimum = 0;
  1186. qc->maximum = 0xffff;
  1187. qc->step = 1;
  1188. qc->default_value = SOLO_DEF_MOT_THRESH;
  1189. strlcpy(qc->name, "Motion Detection Threshold",
  1190. sizeof(qc->name));
  1191. return 0;
  1192. case V4L2_CID_MOTION_ENABLE:
  1193. qc->type = V4L2_CTRL_TYPE_BOOLEAN;
  1194. qc->minimum = 0;
  1195. qc->maximum = qc->step = 1;
  1196. qc->default_value = 0;
  1197. strlcpy(qc->name, "Motion Detection Enable", sizeof(qc->name));
  1198. return 0;
  1199. #else
  1200. case V4L2_CID_MOTION_THRESHOLD:
  1201. return v4l2_ctrl_query_fill(qc, 0, 0xffff, 1,
  1202. SOLO_DEF_MOT_THRESH);
  1203. case V4L2_CID_MOTION_ENABLE:
  1204. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  1205. #endif
  1206. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1207. qc->type = V4L2_CTRL_TYPE_STRING;
  1208. qc->minimum = 0;
  1209. qc->maximum = OSD_TEXT_MAX;
  1210. qc->step = 1;
  1211. qc->default_value = 0;
  1212. strlcpy(qc->name, "OSD Text", sizeof(qc->name));
  1213. return 0;
  1214. }
  1215. return -EINVAL;
  1216. }
  1217. static int solo_querymenu(struct file *file, void *priv,
  1218. struct v4l2_querymenu *qmenu)
  1219. {
  1220. struct v4l2_queryctrl qctrl;
  1221. int err;
  1222. qctrl.id = qmenu->id;
  1223. err = solo_queryctrl(file, priv, &qctrl);
  1224. if (err)
  1225. return err;
  1226. return v4l2_ctrl_query_menu(qmenu, &qctrl, NULL);
  1227. }
  1228. static int solo_g_ctrl(struct file *file, void *priv,
  1229. struct v4l2_control *ctrl)
  1230. {
  1231. struct solo_enc_fh *fh = priv;
  1232. struct solo_enc_dev *solo_enc = fh->enc;
  1233. struct solo_dev *solo_dev = solo_enc->solo_dev;
  1234. switch (ctrl->id) {
  1235. case V4L2_CID_BRIGHTNESS:
  1236. case V4L2_CID_CONTRAST:
  1237. case V4L2_CID_SATURATION:
  1238. case V4L2_CID_HUE:
  1239. case V4L2_CID_SHARPNESS:
  1240. return tw28_get_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
  1241. &ctrl->value);
  1242. case V4L2_CID_MPEG_VIDEO_ENCODING:
  1243. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
  1244. break;
  1245. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1246. ctrl->value = solo_enc->gop;
  1247. break;
  1248. case V4L2_CID_MOTION_THRESHOLD:
  1249. ctrl->value = solo_enc->motion_thresh;
  1250. break;
  1251. case V4L2_CID_MOTION_ENABLE:
  1252. ctrl->value = solo_is_motion_on(solo_enc);
  1253. break;
  1254. default:
  1255. return -EINVAL;
  1256. }
  1257. return 0;
  1258. }
  1259. static int solo_s_ctrl(struct file *file, void *priv,
  1260. struct v4l2_control *ctrl)
  1261. {
  1262. struct solo_enc_fh *fh = priv;
  1263. struct solo_enc_dev *solo_enc = fh->enc;
  1264. struct solo_dev *solo_dev = solo_enc->solo_dev;
  1265. switch (ctrl->id) {
  1266. case V4L2_CID_BRIGHTNESS:
  1267. case V4L2_CID_CONTRAST:
  1268. case V4L2_CID_SATURATION:
  1269. case V4L2_CID_HUE:
  1270. case V4L2_CID_SHARPNESS:
  1271. return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
  1272. ctrl->value);
  1273. case V4L2_CID_MPEG_VIDEO_ENCODING:
  1274. if (ctrl->value != V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
  1275. return -ERANGE;
  1276. break;
  1277. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  1278. if (ctrl->value < 1 || ctrl->value > 255)
  1279. return -ERANGE;
  1280. solo_enc->gop = ctrl->value;
  1281. solo_reg_write(solo_dev, SOLO_VE_CH_GOP(solo_enc->ch),
  1282. solo_enc->gop);
  1283. solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(solo_enc->ch),
  1284. solo_enc->gop);
  1285. break;
  1286. case V4L2_CID_MOTION_THRESHOLD:
  1287. /* TODO accept value on lower 16-bits and use high
  1288. * 16-bits to assign the value to a specific block */
  1289. if (ctrl->value < 0 || ctrl->value > 0xffff)
  1290. return -ERANGE;
  1291. solo_enc->motion_thresh = ctrl->value;
  1292. solo_set_motion_threshold(solo_dev, solo_enc->ch, ctrl->value);
  1293. break;
  1294. case V4L2_CID_MOTION_ENABLE:
  1295. solo_motion_toggle(solo_enc, ctrl->value);
  1296. break;
  1297. default:
  1298. return -EINVAL;
  1299. }
  1300. return 0;
  1301. }
  1302. static int solo_s_ext_ctrls(struct file *file, void *priv,
  1303. struct v4l2_ext_controls *ctrls)
  1304. {
  1305. struct solo_enc_fh *fh = priv;
  1306. struct solo_enc_dev *solo_enc = fh->enc;
  1307. int i;
  1308. for (i = 0; i < ctrls->count; i++) {
  1309. struct v4l2_ext_control *ctrl = (ctrls->controls + i);
  1310. int err;
  1311. switch (ctrl->id) {
  1312. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1313. if (ctrl->size - 1 > OSD_TEXT_MAX)
  1314. err = -ERANGE;
  1315. else {
  1316. err = copy_from_user(solo_enc->osd_text,
  1317. ctrl->string,
  1318. OSD_TEXT_MAX);
  1319. solo_enc->osd_text[OSD_TEXT_MAX] = '\0';
  1320. if (!err)
  1321. err = solo_osd_print(solo_enc);
  1322. }
  1323. break;
  1324. default:
  1325. err = -EINVAL;
  1326. }
  1327. if (err < 0) {
  1328. ctrls->error_idx = i;
  1329. return err;
  1330. }
  1331. }
  1332. return 0;
  1333. }
  1334. static int solo_g_ext_ctrls(struct file *file, void *priv,
  1335. struct v4l2_ext_controls *ctrls)
  1336. {
  1337. struct solo_enc_fh *fh = priv;
  1338. struct solo_enc_dev *solo_enc = fh->enc;
  1339. int i;
  1340. for (i = 0; i < ctrls->count; i++) {
  1341. struct v4l2_ext_control *ctrl = (ctrls->controls + i);
  1342. int err;
  1343. switch (ctrl->id) {
  1344. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1345. if (ctrl->size < OSD_TEXT_MAX) {
  1346. ctrl->size = OSD_TEXT_MAX;
  1347. err = -ENOSPC;
  1348. } else {
  1349. err = copy_to_user(ctrl->string,
  1350. solo_enc->osd_text,
  1351. OSD_TEXT_MAX);
  1352. }
  1353. break;
  1354. default:
  1355. err = -EINVAL;
  1356. }
  1357. if (err < 0) {
  1358. ctrls->error_idx = i;
  1359. return err;
  1360. }
  1361. }
  1362. return 0;
  1363. }
  1364. static const struct v4l2_file_operations solo_enc_fops = {
  1365. .owner = THIS_MODULE,
  1366. .open = solo_enc_open,
  1367. .release = solo_enc_release,
  1368. .read = solo_enc_read,
  1369. .poll = solo_enc_poll,
  1370. .mmap = solo_enc_mmap,
  1371. .ioctl = video_ioctl2,
  1372. };
  1373. static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
  1374. .vidioc_querycap = solo_enc_querycap,
  1375. .vidioc_s_std = solo_enc_s_std,
  1376. /* Input callbacks */
  1377. .vidioc_enum_input = solo_enc_enum_input,
  1378. .vidioc_s_input = solo_enc_set_input,
  1379. .vidioc_g_input = solo_enc_get_input,
  1380. /* Video capture format callbacks */
  1381. .vidioc_enum_fmt_vid_cap = solo_enc_enum_fmt_cap,
  1382. .vidioc_try_fmt_vid_cap = solo_enc_try_fmt_cap,
  1383. .vidioc_s_fmt_vid_cap = solo_enc_set_fmt_cap,
  1384. .vidioc_g_fmt_vid_cap = solo_enc_get_fmt_cap,
  1385. /* Streaming I/O */
  1386. .vidioc_reqbufs = solo_enc_reqbufs,
  1387. .vidioc_querybuf = solo_enc_querybuf,
  1388. .vidioc_qbuf = solo_enc_qbuf,
  1389. .vidioc_dqbuf = solo_enc_dqbuf,
  1390. .vidioc_streamon = solo_enc_streamon,
  1391. .vidioc_streamoff = solo_enc_streamoff,
  1392. /* Frame size and interval */
  1393. .vidioc_enum_framesizes = solo_enum_framesizes,
  1394. .vidioc_enum_frameintervals = solo_enum_frameintervals,
  1395. /* Video capture parameters */
  1396. .vidioc_s_parm = solo_s_parm,
  1397. .vidioc_g_parm = solo_g_parm,
  1398. /* Controls */
  1399. .vidioc_queryctrl = solo_queryctrl,
  1400. .vidioc_querymenu = solo_querymenu,
  1401. .vidioc_g_ctrl = solo_g_ctrl,
  1402. .vidioc_s_ctrl = solo_s_ctrl,
  1403. .vidioc_g_ext_ctrls = solo_g_ext_ctrls,
  1404. .vidioc_s_ext_ctrls = solo_s_ext_ctrls,
  1405. };
  1406. static struct video_device solo_enc_template = {
  1407. .name = SOLO6X10_NAME,
  1408. .fops = &solo_enc_fops,
  1409. .ioctl_ops = &solo_enc_ioctl_ops,
  1410. .minor = -1,
  1411. .release = video_device_release,
  1412. .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_B,
  1413. .current_norm = V4L2_STD_NTSC_M,
  1414. };
  1415. static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev, u8 ch)
  1416. {
  1417. struct solo_enc_dev *solo_enc;
  1418. int ret;
  1419. solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
  1420. if (!solo_enc)
  1421. return ERR_PTR(-ENOMEM);
  1422. solo_enc->vfd = video_device_alloc();
  1423. if (!solo_enc->vfd) {
  1424. kfree(solo_enc);
  1425. return ERR_PTR(-ENOMEM);
  1426. }
  1427. solo_enc->solo_dev = solo_dev;
  1428. solo_enc->ch = ch;
  1429. *solo_enc->vfd = solo_enc_template;
  1430. solo_enc->vfd->parent = &solo_dev->pdev->dev;
  1431. ret = video_register_device(solo_enc->vfd, VFL_TYPE_GRABBER,
  1432. video_nr);
  1433. if (ret < 0) {
  1434. video_device_release(solo_enc->vfd);
  1435. kfree(solo_enc);
  1436. return ERR_PTR(ret);
  1437. }
  1438. video_set_drvdata(solo_enc->vfd, solo_enc);
  1439. snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
  1440. "%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
  1441. solo_enc->vfd->num);
  1442. if (video_nr != -1)
  1443. video_nr++;
  1444. spin_lock_init(&solo_enc->lock);
  1445. init_waitqueue_head(&solo_enc->thread_wait);
  1446. atomic_set(&solo_enc->readers, 0);
  1447. solo_enc->qp = SOLO_DEFAULT_QP;
  1448. solo_enc->gop = solo_dev->fps;
  1449. solo_enc->interval = 1;
  1450. solo_enc->mode = SOLO_ENC_MODE_CIF;
  1451. solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
  1452. spin_lock(&solo_enc->lock);
  1453. solo_update_mode(solo_enc);
  1454. spin_unlock(&solo_enc->lock);
  1455. return solo_enc;
  1456. }
  1457. static void solo_enc_free(struct solo_enc_dev *solo_enc)
  1458. {
  1459. if (solo_enc == NULL)
  1460. return;
  1461. video_unregister_device(solo_enc->vfd);
  1462. kfree(solo_enc);
  1463. }
  1464. int solo_enc_v4l2_init(struct solo_dev *solo_dev)
  1465. {
  1466. int i;
  1467. for (i = 0; i < solo_dev->nr_chans; i++) {
  1468. solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i);
  1469. if (IS_ERR(solo_dev->v4l2_enc[i]))
  1470. break;
  1471. }
  1472. if (i != solo_dev->nr_chans) {
  1473. int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
  1474. while (i--)
  1475. solo_enc_free(solo_dev->v4l2_enc[i]);
  1476. return ret;
  1477. }
  1478. /* D1@MAX-FPS * 4 */
  1479. solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
  1480. dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
  1481. solo_dev->v4l2_enc[0]->vfd->num,
  1482. solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
  1483. return 0;
  1484. }
  1485. void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
  1486. {
  1487. int i;
  1488. solo_irq_off(solo_dev, SOLO_IRQ_MOTION);
  1489. for (i = 0; i < solo_dev->nr_chans; i++)
  1490. solo_enc_free(solo_dev->v4l2_enc[i]);
  1491. }