saa7146_video.c 34 KB

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