saa7146_video.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. #include <media/saa7146_vv.h>
  2. #include <media/v4l2-chip-ident.h>
  3. #include <linux/module.h>
  4. static int max_memory = 32;
  5. module_param(max_memory, int, 0644);
  6. MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
  7. #define IS_CAPTURE_ACTIVE(fh) \
  8. (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
  9. #define IS_OVERLAY_ACTIVE(fh) \
  10. (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
  11. /* format descriptions for capture and preview */
  12. static struct saa7146_format formats[] = {
  13. {
  14. .name = "RGB-8 (3-3-2)",
  15. .pixelformat = V4L2_PIX_FMT_RGB332,
  16. .trans = RGB08_COMPOSED,
  17. .depth = 8,
  18. .flags = 0,
  19. }, {
  20. .name = "RGB-16 (5/B-6/G-5/R)",
  21. .pixelformat = V4L2_PIX_FMT_RGB565,
  22. .trans = RGB16_COMPOSED,
  23. .depth = 16,
  24. .flags = 0,
  25. }, {
  26. .name = "RGB-24 (B-G-R)",
  27. .pixelformat = V4L2_PIX_FMT_BGR24,
  28. .trans = RGB24_COMPOSED,
  29. .depth = 24,
  30. .flags = 0,
  31. }, {
  32. .name = "RGB-32 (B-G-R)",
  33. .pixelformat = V4L2_PIX_FMT_BGR32,
  34. .trans = RGB32_COMPOSED,
  35. .depth = 32,
  36. .flags = 0,
  37. }, {
  38. .name = "RGB-32 (R-G-B)",
  39. .pixelformat = V4L2_PIX_FMT_RGB32,
  40. .trans = RGB32_COMPOSED,
  41. .depth = 32,
  42. .flags = 0,
  43. .swap = 0x2,
  44. }, {
  45. .name = "Greyscale-8",
  46. .pixelformat = V4L2_PIX_FMT_GREY,
  47. .trans = Y8,
  48. .depth = 8,
  49. .flags = 0,
  50. }, {
  51. .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
  52. .pixelformat = V4L2_PIX_FMT_YUV422P,
  53. .trans = YUV422_DECOMPOSED,
  54. .depth = 16,
  55. .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
  56. }, {
  57. .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
  58. .pixelformat = V4L2_PIX_FMT_YVU420,
  59. .trans = YUV420_DECOMPOSED,
  60. .depth = 12,
  61. .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
  62. }, {
  63. .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
  64. .pixelformat = V4L2_PIX_FMT_YUV420,
  65. .trans = YUV420_DECOMPOSED,
  66. .depth = 12,
  67. .flags = FORMAT_IS_PLANAR,
  68. }, {
  69. .name = "YUV 4:2:2 (U-Y-V-Y)",
  70. .pixelformat = V4L2_PIX_FMT_UYVY,
  71. .trans = YUV422_COMPOSED,
  72. .depth = 16,
  73. .flags = 0,
  74. }
  75. };
  76. /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
  77. due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
  78. (like V4L2_PIX_FMT_YUYV) ... 8-( */
  79. static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
  80. struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
  81. {
  82. int i, j = NUM_FORMATS;
  83. for (i = 0; i < j; i++) {
  84. if (formats[i].pixelformat == fourcc) {
  85. return formats+i;
  86. }
  87. }
  88. DEB_D(("unknown pixelformat:'%4.4s'\n",(char *)&fourcc));
  89. return NULL;
  90. }
  91. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
  92. int saa7146_start_preview(struct saa7146_fh *fh)
  93. {
  94. struct saa7146_dev *dev = fh->dev;
  95. struct saa7146_vv *vv = dev->vv_data;
  96. struct v4l2_format fmt;
  97. int ret = 0, err = 0;
  98. DEB_EE(("dev:%p, fh:%p\n",dev,fh));
  99. /* check if we have overlay informations */
  100. if( NULL == fh->ov.fh ) {
  101. DEB_D(("no overlay data available. try S_FMT first.\n"));
  102. return -EAGAIN;
  103. }
  104. /* check if streaming capture is running */
  105. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  106. DEB_D(("streaming capture is active.\n"));
  107. return -EBUSY;
  108. }
  109. /* check if overlay is running */
  110. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  111. if (vv->video_fh == fh) {
  112. DEB_D(("overlay is already active.\n"));
  113. return 0;
  114. }
  115. DEB_D(("overlay is already active in another open.\n"));
  116. return -EBUSY;
  117. }
  118. if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
  119. DEB_D(("cannot get necessary overlay resources\n"));
  120. return -EBUSY;
  121. }
  122. fmt.fmt.win = fh->ov.win;
  123. err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
  124. if (0 != err) {
  125. saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
  126. return -EBUSY;
  127. }
  128. fh->ov.win = fmt.fmt.win;
  129. vv->ov_data = &fh->ov;
  130. DEB_D(("%dx%d+%d+%d %s field=%s\n",
  131. fh->ov.win.w.width,fh->ov.win.w.height,
  132. fh->ov.win.w.left,fh->ov.win.w.top,
  133. vv->ov_fmt->name,v4l2_field_names[fh->ov.win.field]));
  134. if (0 != (ret = saa7146_enable_overlay(fh))) {
  135. DEB_D(("enabling overlay failed: %d\n",ret));
  136. saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
  137. return ret;
  138. }
  139. vv->video_status = STATUS_OVERLAY;
  140. vv->video_fh = fh;
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(saa7146_start_preview);
  144. int saa7146_stop_preview(struct saa7146_fh *fh)
  145. {
  146. struct saa7146_dev *dev = fh->dev;
  147. struct saa7146_vv *vv = dev->vv_data;
  148. DEB_EE(("dev:%p, fh:%p\n",dev,fh));
  149. /* check if streaming capture is running */
  150. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  151. DEB_D(("streaming capture is active.\n"));
  152. return -EBUSY;
  153. }
  154. /* check if overlay is running at all */
  155. if ((vv->video_status & STATUS_OVERLAY) == 0) {
  156. DEB_D(("no active overlay.\n"));
  157. return 0;
  158. }
  159. if (vv->video_fh != fh) {
  160. DEB_D(("overlay is active, but in another open.\n"));
  161. return -EBUSY;
  162. }
  163. vv->video_status = 0;
  164. vv->video_fh = NULL;
  165. saa7146_disable_overlay(fh);
  166. saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(saa7146_stop_preview);
  170. /********************************************************************************/
  171. /* device controls */
  172. static struct v4l2_queryctrl controls[] = {
  173. {
  174. .id = V4L2_CID_BRIGHTNESS,
  175. .name = "Brightness",
  176. .minimum = 0,
  177. .maximum = 255,
  178. .step = 1,
  179. .default_value = 128,
  180. .type = V4L2_CTRL_TYPE_INTEGER,
  181. .flags = V4L2_CTRL_FLAG_SLIDER,
  182. },{
  183. .id = V4L2_CID_CONTRAST,
  184. .name = "Contrast",
  185. .minimum = 0,
  186. .maximum = 127,
  187. .step = 1,
  188. .default_value = 64,
  189. .type = V4L2_CTRL_TYPE_INTEGER,
  190. .flags = V4L2_CTRL_FLAG_SLIDER,
  191. },{
  192. .id = V4L2_CID_SATURATION,
  193. .name = "Saturation",
  194. .minimum = 0,
  195. .maximum = 127,
  196. .step = 1,
  197. .default_value = 64,
  198. .type = V4L2_CTRL_TYPE_INTEGER,
  199. .flags = V4L2_CTRL_FLAG_SLIDER,
  200. },{
  201. .id = V4L2_CID_VFLIP,
  202. .name = "Vertical Flip",
  203. .minimum = 0,
  204. .maximum = 1,
  205. .type = V4L2_CTRL_TYPE_BOOLEAN,
  206. },{
  207. .id = V4L2_CID_HFLIP,
  208. .name = "Horizontal Flip",
  209. .minimum = 0,
  210. .maximum = 1,
  211. .type = V4L2_CTRL_TYPE_BOOLEAN,
  212. },
  213. };
  214. static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
  215. #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
  216. static struct v4l2_queryctrl* ctrl_by_id(int id)
  217. {
  218. int i;
  219. for (i = 0; i < NUM_CONTROLS; i++)
  220. if (controls[i].id == id)
  221. return controls+i;
  222. return NULL;
  223. }
  224. /********************************************************************************/
  225. /* common pagetable functions */
  226. static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
  227. {
  228. struct pci_dev *pci = dev->pci;
  229. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  230. struct scatterlist *list = dma->sglist;
  231. int length = dma->sglen;
  232. struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
  233. DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
  234. if( 0 != IS_PLANAR(sfmt->trans)) {
  235. struct saa7146_pgtable *pt1 = &buf->pt[0];
  236. struct saa7146_pgtable *pt2 = &buf->pt[1];
  237. struct saa7146_pgtable *pt3 = &buf->pt[2];
  238. __le32 *ptr1, *ptr2, *ptr3;
  239. __le32 fill;
  240. int size = buf->fmt->width*buf->fmt->height;
  241. int i,p,m1,m2,m3,o1,o2;
  242. switch( sfmt->depth ) {
  243. case 12: {
  244. /* create some offsets inside the page table */
  245. m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
  246. m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
  247. m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
  248. o1 = size%PAGE_SIZE;
  249. o2 = (size+(size/4))%PAGE_SIZE;
  250. DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
  251. break;
  252. }
  253. case 16: {
  254. /* create some offsets inside the page table */
  255. m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
  256. m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
  257. m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
  258. o1 = size%PAGE_SIZE;
  259. o2 = (size+(size/2))%PAGE_SIZE;
  260. DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
  261. break;
  262. }
  263. default: {
  264. return -1;
  265. }
  266. }
  267. ptr1 = pt1->cpu;
  268. ptr2 = pt2->cpu;
  269. ptr3 = pt3->cpu;
  270. /* walk all pages, copy all page addresses to ptr1 */
  271. for (i = 0; i < length; i++, list++) {
  272. for (p = 0; p * 4096 < list->length; p++, ptr1++) {
  273. *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
  274. }
  275. }
  276. /*
  277. ptr1 = pt1->cpu;
  278. for(j=0;j<40;j++) {
  279. printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
  280. }
  281. */
  282. /* if we have a user buffer, the first page may not be
  283. aligned to a page boundary. */
  284. pt1->offset = dma->sglist->offset;
  285. pt2->offset = pt1->offset+o1;
  286. pt3->offset = pt1->offset+o2;
  287. /* create video-dma2 page table */
  288. ptr1 = pt1->cpu;
  289. for(i = m1; i <= m2 ; i++, ptr2++) {
  290. *ptr2 = ptr1[i];
  291. }
  292. fill = *(ptr2-1);
  293. for(;i<1024;i++,ptr2++) {
  294. *ptr2 = fill;
  295. }
  296. /* create video-dma3 page table */
  297. ptr1 = pt1->cpu;
  298. for(i = m2; i <= m3; i++,ptr3++) {
  299. *ptr3 = ptr1[i];
  300. }
  301. fill = *(ptr3-1);
  302. for(;i<1024;i++,ptr3++) {
  303. *ptr3 = fill;
  304. }
  305. /* finally: finish up video-dma1 page table */
  306. ptr1 = pt1->cpu+m1;
  307. fill = pt1->cpu[m1];
  308. for(i=m1;i<1024;i++,ptr1++) {
  309. *ptr1 = fill;
  310. }
  311. /*
  312. ptr1 = pt1->cpu;
  313. ptr2 = pt2->cpu;
  314. ptr3 = pt3->cpu;
  315. for(j=0;j<40;j++) {
  316. printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
  317. }
  318. for(j=0;j<40;j++) {
  319. printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
  320. }
  321. for(j=0;j<40;j++) {
  322. printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
  323. }
  324. */
  325. } else {
  326. struct saa7146_pgtable *pt = &buf->pt[0];
  327. return saa7146_pgtable_build_single(pci, pt, list, length);
  328. }
  329. return 0;
  330. }
  331. /********************************************************************************/
  332. /* file operations */
  333. static int video_begin(struct saa7146_fh *fh)
  334. {
  335. struct saa7146_dev *dev = fh->dev;
  336. struct saa7146_vv *vv = dev->vv_data;
  337. struct saa7146_format *fmt = NULL;
  338. unsigned int resource;
  339. int ret = 0, err = 0;
  340. DEB_EE(("dev:%p, fh:%p\n",dev,fh));
  341. if ((vv->video_status & STATUS_CAPTURE) != 0) {
  342. if (vv->video_fh == fh) {
  343. DEB_S(("already capturing.\n"));
  344. return 0;
  345. }
  346. DEB_S(("already capturing in another open.\n"));
  347. return -EBUSY;
  348. }
  349. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  350. DEB_S(("warning: suspending overlay video for streaming capture.\n"));
  351. vv->ov_suspend = vv->video_fh;
  352. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  353. if (0 != err) {
  354. DEB_D(("suspending video failed. aborting\n"));
  355. return err;
  356. }
  357. }
  358. fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
  359. /* we need to have a valid format set here */
  360. BUG_ON(NULL == fmt);
  361. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  362. resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
  363. } else {
  364. resource = RESOURCE_DMA1_HPS;
  365. }
  366. ret = saa7146_res_get(fh, resource);
  367. if (0 == ret) {
  368. DEB_S(("cannot get capture resource %d\n",resource));
  369. if (vv->ov_suspend != NULL) {
  370. saa7146_start_preview(vv->ov_suspend);
  371. vv->ov_suspend = NULL;
  372. }
  373. return -EBUSY;
  374. }
  375. /* clear out beginning of streaming bit (rps register 0)*/
  376. saa7146_write(dev, MC2, MASK_27 );
  377. /* enable rps0 irqs */
  378. SAA7146_IER_ENABLE(dev, MASK_27);
  379. vv->video_fh = fh;
  380. vv->video_status = STATUS_CAPTURE;
  381. return 0;
  382. }
  383. static int video_end(struct saa7146_fh *fh, struct file *file)
  384. {
  385. struct saa7146_dev *dev = fh->dev;
  386. struct saa7146_vv *vv = dev->vv_data;
  387. struct saa7146_format *fmt = NULL;
  388. unsigned long flags;
  389. unsigned int resource;
  390. u32 dmas = 0;
  391. DEB_EE(("dev:%p, fh:%p\n",dev,fh));
  392. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  393. DEB_S(("not capturing.\n"));
  394. return 0;
  395. }
  396. if (vv->video_fh != fh) {
  397. DEB_S(("capturing, but in another open.\n"));
  398. return -EBUSY;
  399. }
  400. fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
  401. /* we need to have a valid format set here */
  402. BUG_ON(NULL == fmt);
  403. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  404. resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
  405. dmas = MASK_22 | MASK_21 | MASK_20;
  406. } else {
  407. resource = RESOURCE_DMA1_HPS;
  408. dmas = MASK_22;
  409. }
  410. spin_lock_irqsave(&dev->slock,flags);
  411. /* disable rps0 */
  412. saa7146_write(dev, MC1, MASK_28);
  413. /* disable rps0 irqs */
  414. SAA7146_IER_DISABLE(dev, MASK_27);
  415. /* shut down all used video dma transfers */
  416. saa7146_write(dev, MC1, dmas);
  417. spin_unlock_irqrestore(&dev->slock, flags);
  418. vv->video_fh = NULL;
  419. vv->video_status = 0;
  420. saa7146_res_free(fh, resource);
  421. if (vv->ov_suspend != NULL) {
  422. saa7146_start_preview(vv->ov_suspend);
  423. vv->ov_suspend = NULL;
  424. }
  425. return 0;
  426. }
  427. static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  428. {
  429. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  430. strcpy((char *)cap->driver, "saa7146 v4l2");
  431. strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
  432. sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
  433. cap->version = SAA7146_VERSION_CODE;
  434. cap->capabilities =
  435. V4L2_CAP_VIDEO_CAPTURE |
  436. V4L2_CAP_VIDEO_OVERLAY |
  437. V4L2_CAP_READWRITE |
  438. V4L2_CAP_STREAMING;
  439. cap->capabilities |= dev->ext_vv_data->capabilities;
  440. return 0;
  441. }
  442. static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  443. {
  444. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  445. struct saa7146_vv *vv = dev->vv_data;
  446. *fb = vv->ov_fb;
  447. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  448. return 0;
  449. }
  450. static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  451. {
  452. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  453. struct saa7146_vv *vv = dev->vv_data;
  454. struct saa7146_format *fmt;
  455. DEB_EE(("VIDIOC_S_FBUF\n"));
  456. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  457. return -EPERM;
  458. /* check args */
  459. fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
  460. if (NULL == fmt)
  461. return -EINVAL;
  462. /* planar formats are not allowed for overlay video, clipping and video dma would clash */
  463. if (fmt->flags & FORMAT_IS_PLANAR)
  464. DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",
  465. (char *)&fmt->pixelformat));
  466. /* check if overlay is running */
  467. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  468. if (vv->video_fh != fh) {
  469. DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
  470. return -EBUSY;
  471. }
  472. }
  473. /* ok, accept it */
  474. vv->ov_fb = *fb;
  475. vv->ov_fmt = fmt;
  476. if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
  477. vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
  478. DEB_D(("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline));
  479. }
  480. return 0;
  481. }
  482. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  483. {
  484. if (f->index >= NUM_FORMATS)
  485. return -EINVAL;
  486. strlcpy((char *)f->description, formats[f->index].name,
  487. sizeof(f->description));
  488. f->pixelformat = formats[f->index].pixelformat;
  489. return 0;
  490. }
  491. static int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
  492. {
  493. const struct v4l2_queryctrl *ctrl;
  494. if ((c->id < V4L2_CID_BASE ||
  495. c->id >= V4L2_CID_LASTP1) &&
  496. (c->id < V4L2_CID_PRIVATE_BASE ||
  497. c->id >= V4L2_CID_PRIVATE_LASTP1))
  498. return -EINVAL;
  499. ctrl = ctrl_by_id(c->id);
  500. if (ctrl == NULL)
  501. return -EINVAL;
  502. DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n", c->id));
  503. *c = *ctrl;
  504. return 0;
  505. }
  506. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  507. {
  508. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  509. struct saa7146_vv *vv = dev->vv_data;
  510. const struct v4l2_queryctrl *ctrl;
  511. u32 value = 0;
  512. ctrl = ctrl_by_id(c->id);
  513. if (NULL == ctrl)
  514. return -EINVAL;
  515. switch (c->id) {
  516. case V4L2_CID_BRIGHTNESS:
  517. value = saa7146_read(dev, BCS_CTRL);
  518. c->value = 0xff & (value >> 24);
  519. DEB_D(("V4L2_CID_BRIGHTNESS: %d\n", c->value));
  520. break;
  521. case V4L2_CID_CONTRAST:
  522. value = saa7146_read(dev, BCS_CTRL);
  523. c->value = 0x7f & (value >> 16);
  524. DEB_D(("V4L2_CID_CONTRAST: %d\n", c->value));
  525. break;
  526. case V4L2_CID_SATURATION:
  527. value = saa7146_read(dev, BCS_CTRL);
  528. c->value = 0x7f & (value >> 0);
  529. DEB_D(("V4L2_CID_SATURATION: %d\n", c->value));
  530. break;
  531. case V4L2_CID_VFLIP:
  532. c->value = vv->vflip;
  533. DEB_D(("V4L2_CID_VFLIP: %d\n", c->value));
  534. break;
  535. case V4L2_CID_HFLIP:
  536. c->value = vv->hflip;
  537. DEB_D(("V4L2_CID_HFLIP: %d\n", c->value));
  538. break;
  539. default:
  540. return -EINVAL;
  541. }
  542. return 0;
  543. }
  544. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  545. {
  546. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  547. struct saa7146_vv *vv = dev->vv_data;
  548. const struct v4l2_queryctrl *ctrl;
  549. ctrl = ctrl_by_id(c->id);
  550. if (NULL == ctrl) {
  551. DEB_D(("unknown control %d\n", c->id));
  552. return -EINVAL;
  553. }
  554. switch (ctrl->type) {
  555. case V4L2_CTRL_TYPE_BOOLEAN:
  556. case V4L2_CTRL_TYPE_MENU:
  557. case V4L2_CTRL_TYPE_INTEGER:
  558. if (c->value < ctrl->minimum)
  559. c->value = ctrl->minimum;
  560. if (c->value > ctrl->maximum)
  561. c->value = ctrl->maximum;
  562. break;
  563. default:
  564. /* nothing */;
  565. }
  566. switch (c->id) {
  567. case V4L2_CID_BRIGHTNESS: {
  568. u32 value = saa7146_read(dev, BCS_CTRL);
  569. value &= 0x00ffffff;
  570. value |= (c->value << 24);
  571. saa7146_write(dev, BCS_CTRL, value);
  572. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  573. break;
  574. }
  575. case V4L2_CID_CONTRAST: {
  576. u32 value = saa7146_read(dev, BCS_CTRL);
  577. value &= 0xff00ffff;
  578. value |= (c->value << 16);
  579. saa7146_write(dev, BCS_CTRL, value);
  580. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  581. break;
  582. }
  583. case V4L2_CID_SATURATION: {
  584. u32 value = saa7146_read(dev, BCS_CTRL);
  585. value &= 0xffffff00;
  586. value |= (c->value << 0);
  587. saa7146_write(dev, BCS_CTRL, value);
  588. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  589. break;
  590. }
  591. case V4L2_CID_HFLIP:
  592. /* fixme: we can support changing VFLIP and HFLIP here... */
  593. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  594. DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
  595. return -EBUSY;
  596. }
  597. vv->hflip = c->value;
  598. break;
  599. case V4L2_CID_VFLIP:
  600. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  601. DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
  602. return -EBUSY;
  603. }
  604. vv->vflip = c->value;
  605. break;
  606. default:
  607. return -EINVAL;
  608. }
  609. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  610. saa7146_stop_preview(fh);
  611. saa7146_start_preview(fh);
  612. }
  613. return 0;
  614. }
  615. static int vidioc_g_parm(struct file *file, void *fh,
  616. struct v4l2_streamparm *parm)
  617. {
  618. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  619. struct saa7146_vv *vv = dev->vv_data;
  620. parm->parm.capture.readbuffers = 1;
  621. v4l2_video_std_frame_period(vv->standard->id,
  622. &parm->parm.capture.timeperframe);
  623. return 0;
  624. }
  625. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  626. {
  627. f->fmt.pix = ((struct saa7146_fh *)fh)->video_fmt;
  628. return 0;
  629. }
  630. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
  631. {
  632. f->fmt.win = ((struct saa7146_fh *)fh)->ov.win;
  633. return 0;
  634. }
  635. static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
  636. {
  637. f->fmt.vbi = ((struct saa7146_fh *)fh)->vbi_fmt;
  638. return 0;
  639. }
  640. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  641. {
  642. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  643. struct saa7146_vv *vv = dev->vv_data;
  644. struct saa7146_format *fmt;
  645. enum v4l2_field field;
  646. int maxw, maxh;
  647. int calc_bpl;
  648. DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
  649. fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
  650. if (NULL == fmt)
  651. return -EINVAL;
  652. field = f->fmt.pix.field;
  653. maxw = vv->standard->h_max_out;
  654. maxh = vv->standard->v_max_out;
  655. if (V4L2_FIELD_ANY == field) {
  656. field = (f->fmt.pix.height > maxh / 2)
  657. ? V4L2_FIELD_INTERLACED
  658. : V4L2_FIELD_BOTTOM;
  659. }
  660. switch (field) {
  661. case V4L2_FIELD_ALTERNATE:
  662. vv->last_field = V4L2_FIELD_TOP;
  663. maxh = maxh / 2;
  664. break;
  665. case V4L2_FIELD_TOP:
  666. case V4L2_FIELD_BOTTOM:
  667. vv->last_field = V4L2_FIELD_INTERLACED;
  668. maxh = maxh / 2;
  669. break;
  670. case V4L2_FIELD_INTERLACED:
  671. vv->last_field = V4L2_FIELD_INTERLACED;
  672. break;
  673. default:
  674. DEB_D(("no known field mode '%d'.\n", field));
  675. return -EINVAL;
  676. }
  677. f->fmt.pix.field = field;
  678. if (f->fmt.pix.width > maxw)
  679. f->fmt.pix.width = maxw;
  680. if (f->fmt.pix.height > maxh)
  681. f->fmt.pix.height = maxh;
  682. calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
  683. if (f->fmt.pix.bytesperline < calc_bpl)
  684. f->fmt.pix.bytesperline = calc_bpl;
  685. if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
  686. f->fmt.pix.bytesperline = calc_bpl;
  687. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  688. DEB_D(("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n", f->fmt.pix.width,
  689. f->fmt.pix.height, f->fmt.pix.bytesperline, f->fmt.pix.sizeimage));
  690. return 0;
  691. }
  692. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
  693. {
  694. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  695. struct saa7146_vv *vv = dev->vv_data;
  696. struct v4l2_window *win = &f->fmt.win;
  697. enum v4l2_field field;
  698. int maxw, maxh;
  699. DEB_EE(("dev:%p\n", dev));
  700. if (NULL == vv->ov_fb.base) {
  701. DEB_D(("no fb base set.\n"));
  702. return -EINVAL;
  703. }
  704. if (NULL == vv->ov_fmt) {
  705. DEB_D(("no fb fmt set.\n"));
  706. return -EINVAL;
  707. }
  708. if (win->w.width < 48 || win->w.height < 32) {
  709. DEB_D(("min width/height. (%d,%d)\n", win->w.width, win->w.height));
  710. return -EINVAL;
  711. }
  712. if (win->clipcount > 16) {
  713. DEB_D(("clipcount too big.\n"));
  714. return -EINVAL;
  715. }
  716. field = win->field;
  717. maxw = vv->standard->h_max_out;
  718. maxh = vv->standard->v_max_out;
  719. if (V4L2_FIELD_ANY == field) {
  720. field = (win->w.height > maxh / 2)
  721. ? V4L2_FIELD_INTERLACED
  722. : V4L2_FIELD_TOP;
  723. }
  724. switch (field) {
  725. case V4L2_FIELD_TOP:
  726. case V4L2_FIELD_BOTTOM:
  727. case V4L2_FIELD_ALTERNATE:
  728. maxh = maxh / 2;
  729. break;
  730. case V4L2_FIELD_INTERLACED:
  731. break;
  732. default:
  733. DEB_D(("no known field mode '%d'.\n", field));
  734. return -EINVAL;
  735. }
  736. win->field = field;
  737. if (win->w.width > maxw)
  738. win->w.width = maxw;
  739. if (win->w.height > maxh)
  740. win->w.height = maxh;
  741. return 0;
  742. }
  743. static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
  744. {
  745. struct saa7146_fh *fh = __fh;
  746. struct saa7146_dev *dev = fh->dev;
  747. struct saa7146_vv *vv = dev->vv_data;
  748. int err;
  749. DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh));
  750. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  751. DEB_EE(("streaming capture is active\n"));
  752. return -EBUSY;
  753. }
  754. err = vidioc_try_fmt_vid_cap(file, fh, f);
  755. if (0 != err)
  756. return err;
  757. fh->video_fmt = f->fmt.pix;
  758. DEB_EE(("set to pixelformat '%4.4s'\n", (char *)&fh->video_fmt.pixelformat));
  759. return 0;
  760. }
  761. static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
  762. {
  763. struct saa7146_fh *fh = __fh;
  764. struct saa7146_dev *dev = fh->dev;
  765. struct saa7146_vv *vv = dev->vv_data;
  766. int err;
  767. DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh));
  768. err = vidioc_try_fmt_vid_overlay(file, fh, f);
  769. if (0 != err)
  770. return err;
  771. fh->ov.win = f->fmt.win;
  772. fh->ov.nclips = f->fmt.win.clipcount;
  773. if (fh->ov.nclips > 16)
  774. fh->ov.nclips = 16;
  775. if (copy_from_user(fh->ov.clips, f->fmt.win.clips,
  776. sizeof(struct v4l2_clip) * fh->ov.nclips)) {
  777. return -EFAULT;
  778. }
  779. /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
  780. fh->ov.fh = fh;
  781. /* check if our current overlay is active */
  782. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  783. saa7146_stop_preview(fh);
  784. saa7146_start_preview(fh);
  785. }
  786. return 0;
  787. }
  788. static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
  789. {
  790. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  791. struct saa7146_vv *vv = dev->vv_data;
  792. *norm = vv->standard->id;
  793. return 0;
  794. }
  795. /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
  796. PAL / NTSC / SECAM. if your hardware does not (or does more)
  797. -- override this function in your extension */
  798. /*
  799. case VIDIOC_ENUMSTD:
  800. {
  801. struct v4l2_standard *e = arg;
  802. if (e->index < 0 )
  803. return -EINVAL;
  804. if( e->index < dev->ext_vv_data->num_stds ) {
  805. DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e->index));
  806. v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
  807. return 0;
  808. }
  809. return -EINVAL;
  810. }
  811. */
  812. static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
  813. {
  814. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  815. struct saa7146_vv *vv = dev->vv_data;
  816. int found = 0;
  817. int err, i;
  818. DEB_EE(("VIDIOC_S_STD\n"));
  819. if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
  820. DEB_D(("cannot change video standard while streaming capture is active\n"));
  821. return -EBUSY;
  822. }
  823. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  824. vv->ov_suspend = vv->video_fh;
  825. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  826. if (0 != err) {
  827. DEB_D(("suspending video failed. aborting\n"));
  828. return err;
  829. }
  830. }
  831. for (i = 0; i < dev->ext_vv_data->num_stds; i++)
  832. if (*id & dev->ext_vv_data->stds[i].id)
  833. break;
  834. if (i != dev->ext_vv_data->num_stds) {
  835. vv->standard = &dev->ext_vv_data->stds[i];
  836. if (NULL != dev->ext_vv_data->std_callback)
  837. dev->ext_vv_data->std_callback(dev, vv->standard);
  838. found = 1;
  839. }
  840. if (vv->ov_suspend != NULL) {
  841. saa7146_start_preview(vv->ov_suspend);
  842. vv->ov_suspend = NULL;
  843. }
  844. if (!found) {
  845. DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
  846. return -EINVAL;
  847. }
  848. DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name));
  849. return 0;
  850. }
  851. static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
  852. {
  853. int err;
  854. DEB_D(("VIDIOC_OVERLAY on:%d\n", on));
  855. if (on)
  856. err = saa7146_start_preview(fh);
  857. else
  858. err = saa7146_stop_preview(fh);
  859. return err;
  860. }
  861. static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
  862. {
  863. struct saa7146_fh *fh = __fh;
  864. if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  865. return videobuf_reqbufs(&fh->video_q, b);
  866. if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  867. return videobuf_reqbufs(&fh->vbi_q, b);
  868. return -EINVAL;
  869. }
  870. static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  871. {
  872. struct saa7146_fh *fh = __fh;
  873. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  874. return videobuf_querybuf(&fh->video_q, buf);
  875. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  876. return videobuf_querybuf(&fh->vbi_q, buf);
  877. return -EINVAL;
  878. }
  879. static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  880. {
  881. struct saa7146_fh *fh = __fh;
  882. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  883. return videobuf_qbuf(&fh->video_q, buf);
  884. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  885. return videobuf_qbuf(&fh->vbi_q, buf);
  886. return -EINVAL;
  887. }
  888. static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  889. {
  890. struct saa7146_fh *fh = __fh;
  891. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  892. return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
  893. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  894. return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
  895. return -EINVAL;
  896. }
  897. static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
  898. {
  899. struct saa7146_fh *fh = __fh;
  900. int err;
  901. DEB_D(("VIDIOC_STREAMON, type:%d\n", type));
  902. err = video_begin(fh);
  903. if (err)
  904. return err;
  905. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  906. return videobuf_streamon(&fh->video_q);
  907. if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
  908. return videobuf_streamon(&fh->vbi_q);
  909. return -EINVAL;
  910. }
  911. static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
  912. {
  913. struct saa7146_fh *fh = __fh;
  914. struct saa7146_dev *dev = fh->dev;
  915. struct saa7146_vv *vv = dev->vv_data;
  916. int err;
  917. DEB_D(("VIDIOC_STREAMOFF, type:%d\n", type));
  918. /* ugly: we need to copy some checks from video_end(),
  919. because videobuf_streamoff() relies on the capture running.
  920. check and fix this */
  921. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  922. DEB_S(("not capturing.\n"));
  923. return 0;
  924. }
  925. if (vv->video_fh != fh) {
  926. DEB_S(("capturing, but in another open.\n"));
  927. return -EBUSY;
  928. }
  929. err = -EINVAL;
  930. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  931. err = videobuf_streamoff(&fh->video_q);
  932. else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
  933. err = videobuf_streamoff(&fh->vbi_q);
  934. if (0 != err) {
  935. DEB_D(("warning: videobuf_streamoff() failed.\n"));
  936. video_end(fh, file);
  937. } else {
  938. err = video_end(fh, file);
  939. }
  940. return err;
  941. }
  942. static int vidioc_g_chip_ident(struct file *file, void *__fh,
  943. struct v4l2_dbg_chip_ident *chip)
  944. {
  945. struct saa7146_fh *fh = __fh;
  946. struct saa7146_dev *dev = fh->dev;
  947. chip->ident = V4L2_IDENT_NONE;
  948. chip->revision = 0;
  949. if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
  950. chip->ident = V4L2_IDENT_SAA7146;
  951. return 0;
  952. }
  953. return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  954. core, g_chip_ident, chip);
  955. }
  956. const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
  957. .vidioc_querycap = vidioc_querycap,
  958. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  959. .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
  960. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  961. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  962. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  963. .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  964. .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  965. .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  966. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  967. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  968. .vidioc_overlay = vidioc_overlay,
  969. .vidioc_g_fbuf = vidioc_g_fbuf,
  970. .vidioc_s_fbuf = vidioc_s_fbuf,
  971. .vidioc_reqbufs = vidioc_reqbufs,
  972. .vidioc_querybuf = vidioc_querybuf,
  973. .vidioc_qbuf = vidioc_qbuf,
  974. .vidioc_dqbuf = vidioc_dqbuf,
  975. .vidioc_g_std = vidioc_g_std,
  976. .vidioc_s_std = vidioc_s_std,
  977. .vidioc_queryctrl = vidioc_queryctrl,
  978. .vidioc_g_ctrl = vidioc_g_ctrl,
  979. .vidioc_s_ctrl = vidioc_s_ctrl,
  980. .vidioc_streamon = vidioc_streamon,
  981. .vidioc_streamoff = vidioc_streamoff,
  982. .vidioc_g_parm = vidioc_g_parm,
  983. };
  984. /*********************************************************************************/
  985. /* buffer handling functions */
  986. static int buffer_activate (struct saa7146_dev *dev,
  987. struct saa7146_buf *buf,
  988. struct saa7146_buf *next)
  989. {
  990. struct saa7146_vv *vv = dev->vv_data;
  991. buf->vb.state = VIDEOBUF_ACTIVE;
  992. saa7146_set_capture(dev,buf,next);
  993. mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
  994. return 0;
  995. }
  996. static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
  997. {
  998. saa7146_pgtable_free(dev->pci, &buf->pt[0]);
  999. saa7146_pgtable_free(dev->pci, &buf->pt[1]);
  1000. saa7146_pgtable_free(dev->pci, &buf->pt[2]);
  1001. }
  1002. static int buffer_prepare(struct videobuf_queue *q,
  1003. struct videobuf_buffer *vb, enum v4l2_field field)
  1004. {
  1005. struct file *file = q->priv_data;
  1006. struct saa7146_fh *fh = file->private_data;
  1007. struct saa7146_dev *dev = fh->dev;
  1008. struct saa7146_vv *vv = dev->vv_data;
  1009. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  1010. int size,err = 0;
  1011. DEB_CAP(("vbuf:%p\n",vb));
  1012. /* sanity checks */
  1013. if (fh->video_fmt.width < 48 ||
  1014. fh->video_fmt.height < 32 ||
  1015. fh->video_fmt.width > vv->standard->h_max_out ||
  1016. fh->video_fmt.height > vv->standard->v_max_out) {
  1017. DEB_D(("w (%d) / h (%d) out of bounds.\n",fh->video_fmt.width,fh->video_fmt.height));
  1018. return -EINVAL;
  1019. }
  1020. size = fh->video_fmt.sizeimage;
  1021. if (0 != buf->vb.baddr && buf->vb.bsize < size) {
  1022. DEB_D(("size mismatch.\n"));
  1023. return -EINVAL;
  1024. }
  1025. DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
  1026. fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
  1027. if (buf->vb.width != fh->video_fmt.width ||
  1028. buf->vb.bytesperline != fh->video_fmt.bytesperline ||
  1029. buf->vb.height != fh->video_fmt.height ||
  1030. buf->vb.size != size ||
  1031. buf->vb.field != field ||
  1032. buf->vb.field != fh->video_fmt.field ||
  1033. buf->fmt != &fh->video_fmt) {
  1034. saa7146_dma_free(dev,q,buf);
  1035. }
  1036. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  1037. struct saa7146_format *sfmt;
  1038. buf->vb.bytesperline = fh->video_fmt.bytesperline;
  1039. buf->vb.width = fh->video_fmt.width;
  1040. buf->vb.height = fh->video_fmt.height;
  1041. buf->vb.size = size;
  1042. buf->vb.field = field;
  1043. buf->fmt = &fh->video_fmt;
  1044. buf->vb.field = fh->video_fmt.field;
  1045. sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
  1046. release_all_pagetables(dev, buf);
  1047. if( 0 != IS_PLANAR(sfmt->trans)) {
  1048. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  1049. saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
  1050. saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
  1051. } else {
  1052. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  1053. }
  1054. err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
  1055. if (err)
  1056. goto oops;
  1057. err = saa7146_pgtable_build(dev,buf);
  1058. if (err)
  1059. goto oops;
  1060. }
  1061. buf->vb.state = VIDEOBUF_PREPARED;
  1062. buf->activate = buffer_activate;
  1063. return 0;
  1064. oops:
  1065. DEB_D(("error out.\n"));
  1066. saa7146_dma_free(dev,q,buf);
  1067. return err;
  1068. }
  1069. static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  1070. {
  1071. struct file *file = q->priv_data;
  1072. struct saa7146_fh *fh = file->private_data;
  1073. if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
  1074. *count = MAX_SAA7146_CAPTURE_BUFFERS;
  1075. *size = fh->video_fmt.sizeimage;
  1076. /* check if we exceed the "max_memory" parameter */
  1077. if( (*count * *size) > (max_memory*1048576) ) {
  1078. *count = (max_memory*1048576) / *size;
  1079. }
  1080. DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
  1081. return 0;
  1082. }
  1083. static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1084. {
  1085. struct file *file = q->priv_data;
  1086. struct saa7146_fh *fh = file->private_data;
  1087. struct saa7146_dev *dev = fh->dev;
  1088. struct saa7146_vv *vv = dev->vv_data;
  1089. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  1090. DEB_CAP(("vbuf:%p\n",vb));
  1091. saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
  1092. }
  1093. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1094. {
  1095. struct file *file = q->priv_data;
  1096. struct saa7146_fh *fh = file->private_data;
  1097. struct saa7146_dev *dev = fh->dev;
  1098. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  1099. DEB_CAP(("vbuf:%p\n",vb));
  1100. saa7146_dma_free(dev,q,buf);
  1101. release_all_pagetables(dev, buf);
  1102. }
  1103. static struct videobuf_queue_ops video_qops = {
  1104. .buf_setup = buffer_setup,
  1105. .buf_prepare = buffer_prepare,
  1106. .buf_queue = buffer_queue,
  1107. .buf_release = buffer_release,
  1108. };
  1109. /********************************************************************************/
  1110. /* file operations */
  1111. static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
  1112. {
  1113. INIT_LIST_HEAD(&vv->video_q.queue);
  1114. init_timer(&vv->video_q.timeout);
  1115. vv->video_q.timeout.function = saa7146_buffer_timeout;
  1116. vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
  1117. vv->video_q.dev = dev;
  1118. /* set some default values */
  1119. vv->standard = &dev->ext_vv_data->stds[0];
  1120. /* FIXME: what's this? */
  1121. vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
  1122. vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
  1123. }
  1124. static int video_open(struct saa7146_dev *dev, struct file *file)
  1125. {
  1126. struct saa7146_fh *fh = file->private_data;
  1127. struct saa7146_format *sfmt;
  1128. fh->video_fmt.width = 384;
  1129. fh->video_fmt.height = 288;
  1130. fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
  1131. fh->video_fmt.bytesperline = 0;
  1132. fh->video_fmt.field = V4L2_FIELD_ANY;
  1133. sfmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
  1134. fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
  1135. videobuf_queue_sg_init(&fh->video_q, &video_qops,
  1136. &dev->pci->dev, &dev->slock,
  1137. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1138. V4L2_FIELD_INTERLACED,
  1139. sizeof(struct saa7146_buf),
  1140. file, &dev->v4l2_lock);
  1141. return 0;
  1142. }
  1143. static void video_close(struct saa7146_dev *dev, struct file *file)
  1144. {
  1145. struct saa7146_fh *fh = file->private_data;
  1146. struct saa7146_vv *vv = dev->vv_data;
  1147. struct videobuf_queue *q = &fh->video_q;
  1148. int err;
  1149. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  1150. err = video_end(fh, file);
  1151. } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
  1152. err = saa7146_stop_preview(fh);
  1153. }
  1154. videobuf_stop(q);
  1155. /* hmm, why is this function declared void? */
  1156. /* return err */
  1157. }
  1158. static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
  1159. {
  1160. struct saa7146_vv *vv = dev->vv_data;
  1161. struct saa7146_dmaqueue *q = &vv->video_q;
  1162. spin_lock(&dev->slock);
  1163. DEB_CAP(("called.\n"));
  1164. /* only finish the buffer if we have one... */
  1165. if( NULL != q->curr ) {
  1166. saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
  1167. }
  1168. saa7146_buffer_next(dev,q,0);
  1169. spin_unlock(&dev->slock);
  1170. }
  1171. static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1172. {
  1173. struct saa7146_fh *fh = file->private_data;
  1174. struct saa7146_dev *dev = fh->dev;
  1175. struct saa7146_vv *vv = dev->vv_data;
  1176. ssize_t ret = 0;
  1177. DEB_EE(("called.\n"));
  1178. if ((vv->video_status & STATUS_CAPTURE) != 0) {
  1179. /* fixme: should we allow read() captures while streaming capture? */
  1180. if (vv->video_fh == fh) {
  1181. DEB_S(("already capturing.\n"));
  1182. return -EBUSY;
  1183. }
  1184. DEB_S(("already capturing in another open.\n"));
  1185. return -EBUSY;
  1186. }
  1187. ret = video_begin(fh);
  1188. if( 0 != ret) {
  1189. goto out;
  1190. }
  1191. ret = videobuf_read_one(&fh->video_q , data, count, ppos,
  1192. file->f_flags & O_NONBLOCK);
  1193. if (ret != 0) {
  1194. video_end(fh, file);
  1195. } else {
  1196. ret = video_end(fh, file);
  1197. }
  1198. out:
  1199. /* restart overlay if it was active before */
  1200. if (vv->ov_suspend != NULL) {
  1201. saa7146_start_preview(vv->ov_suspend);
  1202. vv->ov_suspend = NULL;
  1203. }
  1204. return ret;
  1205. }
  1206. struct saa7146_use_ops saa7146_video_uops = {
  1207. .init = video_init,
  1208. .open = video_open,
  1209. .release = video_close,
  1210. .irq_done = video_irq_done,
  1211. .read = video_read,
  1212. };