saa7146_video.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <media/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_format *fmt = NULL;
  337. unsigned long flags;
  338. unsigned int resource;
  339. u32 dmas = 0;
  340. DEB_EE("dev:%p, fh:%p\n", dev, fh);
  341. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  342. DEB_S("not capturing\n");
  343. return 0;
  344. }
  345. if (vv->video_fh != fh) {
  346. DEB_S("capturing, but in another open\n");
  347. return -EBUSY;
  348. }
  349. fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat);
  350. /* we need to have a valid format set here */
  351. BUG_ON(NULL == fmt);
  352. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  353. resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
  354. dmas = MASK_22 | MASK_21 | MASK_20;
  355. } else {
  356. resource = RESOURCE_DMA1_HPS;
  357. dmas = MASK_22;
  358. }
  359. spin_lock_irqsave(&dev->slock,flags);
  360. /* disable rps0 */
  361. saa7146_write(dev, MC1, MASK_28);
  362. /* disable rps0 irqs */
  363. SAA7146_IER_DISABLE(dev, MASK_27);
  364. /* shut down all used video dma transfers */
  365. saa7146_write(dev, MC1, dmas);
  366. spin_unlock_irqrestore(&dev->slock, flags);
  367. vv->video_fh = NULL;
  368. vv->video_status = 0;
  369. saa7146_res_free(fh, resource);
  370. if (vv->ov_suspend != NULL) {
  371. saa7146_start_preview(vv->ov_suspend);
  372. vv->ov_suspend = NULL;
  373. }
  374. return 0;
  375. }
  376. static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  377. {
  378. struct video_device *vdev = video_devdata(file);
  379. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  380. strcpy((char *)cap->driver, "saa7146 v4l2");
  381. strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
  382. sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
  383. cap->device_caps =
  384. V4L2_CAP_VIDEO_CAPTURE |
  385. V4L2_CAP_VIDEO_OVERLAY |
  386. V4L2_CAP_READWRITE |
  387. V4L2_CAP_STREAMING;
  388. cap->device_caps |= dev->ext_vv_data->capabilities;
  389. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  390. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  391. cap->device_caps &=
  392. ~(V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_OUTPUT);
  393. else
  394. cap->device_caps &=
  395. ~(V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_AUDIO);
  396. return 0;
  397. }
  398. static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  399. {
  400. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  401. struct saa7146_vv *vv = dev->vv_data;
  402. *fb = vv->ov_fb;
  403. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  404. fb->flags = V4L2_FBUF_FLAG_PRIMARY;
  405. return 0;
  406. }
  407. static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
  408. {
  409. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  410. struct saa7146_vv *vv = dev->vv_data;
  411. struct saa7146_format *fmt;
  412. DEB_EE("VIDIOC_S_FBUF\n");
  413. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  414. return -EPERM;
  415. /* check args */
  416. fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
  417. if (NULL == fmt)
  418. return -EINVAL;
  419. /* planar formats are not allowed for overlay video, clipping and video dma would clash */
  420. if (fmt->flags & FORMAT_IS_PLANAR)
  421. DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
  422. (char *)&fmt->pixelformat);
  423. /* check if overlay is running */
  424. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  425. if (vv->video_fh != fh) {
  426. DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
  427. return -EBUSY;
  428. }
  429. }
  430. /* ok, accept it */
  431. vv->ov_fb = *fb;
  432. vv->ov_fmt = fmt;
  433. if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
  434. vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
  435. DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
  436. }
  437. return 0;
  438. }
  439. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  440. {
  441. if (f->index >= NUM_FORMATS)
  442. return -EINVAL;
  443. strlcpy((char *)f->description, formats[f->index].name,
  444. sizeof(f->description));
  445. f->pixelformat = formats[f->index].pixelformat;
  446. return 0;
  447. }
  448. int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
  449. {
  450. struct saa7146_dev *dev = container_of(ctrl->handler,
  451. struct saa7146_dev, ctrl_handler);
  452. struct saa7146_vv *vv = dev->vv_data;
  453. u32 val;
  454. switch (ctrl->id) {
  455. case V4L2_CID_BRIGHTNESS:
  456. val = saa7146_read(dev, BCS_CTRL);
  457. val &= 0x00ffffff;
  458. val |= (ctrl->val << 24);
  459. saa7146_write(dev, BCS_CTRL, val);
  460. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  461. break;
  462. case V4L2_CID_CONTRAST:
  463. val = saa7146_read(dev, BCS_CTRL);
  464. val &= 0xff00ffff;
  465. val |= (ctrl->val << 16);
  466. saa7146_write(dev, BCS_CTRL, val);
  467. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  468. break;
  469. case V4L2_CID_SATURATION:
  470. val = saa7146_read(dev, BCS_CTRL);
  471. val &= 0xffffff00;
  472. val |= (ctrl->val << 0);
  473. saa7146_write(dev, BCS_CTRL, val);
  474. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  475. break;
  476. case V4L2_CID_HFLIP:
  477. /* fixme: we can support changing VFLIP and HFLIP here... */
  478. if ((vv->video_status & STATUS_CAPTURE))
  479. return -EBUSY;
  480. vv->hflip = ctrl->val;
  481. break;
  482. case V4L2_CID_VFLIP:
  483. if ((vv->video_status & STATUS_CAPTURE))
  484. return -EBUSY;
  485. vv->vflip = ctrl->val;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
  491. struct saa7146_fh *fh = vv->video_fh;
  492. saa7146_stop_preview(fh);
  493. saa7146_start_preview(fh);
  494. }
  495. return 0;
  496. }
  497. static int vidioc_g_parm(struct file *file, void *fh,
  498. struct v4l2_streamparm *parm)
  499. {
  500. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  501. struct saa7146_vv *vv = dev->vv_data;
  502. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  503. return -EINVAL;
  504. parm->parm.capture.readbuffers = 1;
  505. v4l2_video_std_frame_period(vv->standard->id,
  506. &parm->parm.capture.timeperframe);
  507. return 0;
  508. }
  509. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  510. {
  511. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  512. struct saa7146_vv *vv = dev->vv_data;
  513. f->fmt.pix = vv->video_fmt;
  514. return 0;
  515. }
  516. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
  517. {
  518. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  519. struct saa7146_vv *vv = dev->vv_data;
  520. f->fmt.win = vv->ov.win;
  521. return 0;
  522. }
  523. static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
  524. {
  525. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  526. struct saa7146_vv *vv = dev->vv_data;
  527. f->fmt.vbi = vv->vbi_fmt;
  528. return 0;
  529. }
  530. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  531. {
  532. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  533. struct saa7146_vv *vv = dev->vv_data;
  534. struct saa7146_format *fmt;
  535. enum v4l2_field field;
  536. int maxw, maxh;
  537. int calc_bpl;
  538. DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
  539. fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
  540. if (NULL == fmt)
  541. return -EINVAL;
  542. field = f->fmt.pix.field;
  543. maxw = vv->standard->h_max_out;
  544. maxh = vv->standard->v_max_out;
  545. if (V4L2_FIELD_ANY == field) {
  546. field = (f->fmt.pix.height > maxh / 2)
  547. ? V4L2_FIELD_INTERLACED
  548. : V4L2_FIELD_BOTTOM;
  549. }
  550. switch (field) {
  551. case V4L2_FIELD_ALTERNATE:
  552. vv->last_field = V4L2_FIELD_TOP;
  553. maxh = maxh / 2;
  554. break;
  555. case V4L2_FIELD_TOP:
  556. case V4L2_FIELD_BOTTOM:
  557. vv->last_field = V4L2_FIELD_INTERLACED;
  558. maxh = maxh / 2;
  559. break;
  560. case V4L2_FIELD_INTERLACED:
  561. vv->last_field = V4L2_FIELD_INTERLACED;
  562. break;
  563. default:
  564. DEB_D("no known field mode '%d'\n", field);
  565. return -EINVAL;
  566. }
  567. f->fmt.pix.field = field;
  568. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  569. if (f->fmt.pix.width > maxw)
  570. f->fmt.pix.width = maxw;
  571. if (f->fmt.pix.height > maxh)
  572. f->fmt.pix.height = maxh;
  573. calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
  574. if (f->fmt.pix.bytesperline < calc_bpl)
  575. f->fmt.pix.bytesperline = calc_bpl;
  576. if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
  577. f->fmt.pix.bytesperline = calc_bpl;
  578. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  579. DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
  580. f->fmt.pix.width, f->fmt.pix.height,
  581. f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
  582. return 0;
  583. }
  584. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
  585. {
  586. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  587. struct saa7146_vv *vv = dev->vv_data;
  588. struct v4l2_window *win = &f->fmt.win;
  589. enum v4l2_field field;
  590. int maxw, maxh;
  591. DEB_EE("dev:%p\n", dev);
  592. if (NULL == vv->ov_fb.base) {
  593. DEB_D("no fb base set\n");
  594. return -EINVAL;
  595. }
  596. if (NULL == vv->ov_fmt) {
  597. DEB_D("no fb fmt set\n");
  598. return -EINVAL;
  599. }
  600. if (win->w.width < 48 || win->w.height < 32) {
  601. DEB_D("min width/height. (%d,%d)\n",
  602. win->w.width, win->w.height);
  603. return -EINVAL;
  604. }
  605. if (win->clipcount > 16) {
  606. DEB_D("clipcount too big\n");
  607. return -EINVAL;
  608. }
  609. field = win->field;
  610. maxw = vv->standard->h_max_out;
  611. maxh = vv->standard->v_max_out;
  612. if (V4L2_FIELD_ANY == field) {
  613. field = (win->w.height > maxh / 2)
  614. ? V4L2_FIELD_INTERLACED
  615. : V4L2_FIELD_TOP;
  616. }
  617. switch (field) {
  618. case V4L2_FIELD_TOP:
  619. case V4L2_FIELD_BOTTOM:
  620. case V4L2_FIELD_ALTERNATE:
  621. maxh = maxh / 2;
  622. break;
  623. case V4L2_FIELD_INTERLACED:
  624. break;
  625. default:
  626. DEB_D("no known field mode '%d'\n", field);
  627. return -EINVAL;
  628. }
  629. win->field = field;
  630. if (win->w.width > maxw)
  631. win->w.width = maxw;
  632. if (win->w.height > maxh)
  633. win->w.height = maxh;
  634. return 0;
  635. }
  636. static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
  637. {
  638. struct saa7146_fh *fh = __fh;
  639. struct saa7146_dev *dev = fh->dev;
  640. struct saa7146_vv *vv = dev->vv_data;
  641. int err;
  642. DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
  643. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  644. DEB_EE("streaming capture is active\n");
  645. return -EBUSY;
  646. }
  647. err = vidioc_try_fmt_vid_cap(file, fh, f);
  648. if (0 != err)
  649. return err;
  650. vv->video_fmt = f->fmt.pix;
  651. DEB_EE("set to pixelformat '%4.4s'\n",
  652. (char *)&vv->video_fmt.pixelformat);
  653. return 0;
  654. }
  655. static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
  656. {
  657. struct saa7146_fh *fh = __fh;
  658. struct saa7146_dev *dev = fh->dev;
  659. struct saa7146_vv *vv = dev->vv_data;
  660. int err;
  661. DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
  662. err = vidioc_try_fmt_vid_overlay(file, fh, f);
  663. if (0 != err)
  664. return err;
  665. vv->ov.win = f->fmt.win;
  666. vv->ov.nclips = f->fmt.win.clipcount;
  667. if (vv->ov.nclips > 16)
  668. vv->ov.nclips = 16;
  669. if (copy_from_user(vv->ov.clips, f->fmt.win.clips,
  670. sizeof(struct v4l2_clip) * vv->ov.nclips)) {
  671. return -EFAULT;
  672. }
  673. /* vv->ov.fh is used to indicate that we have valid overlay informations, too */
  674. vv->ov.fh = fh;
  675. /* check if our current overlay is active */
  676. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  677. saa7146_stop_preview(fh);
  678. saa7146_start_preview(fh);
  679. }
  680. return 0;
  681. }
  682. static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
  683. {
  684. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  685. struct saa7146_vv *vv = dev->vv_data;
  686. *norm = vv->standard->id;
  687. return 0;
  688. }
  689. /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
  690. PAL / NTSC / SECAM. if your hardware does not (or does more)
  691. -- override this function in your extension */
  692. /*
  693. case VIDIOC_ENUMSTD:
  694. {
  695. struct v4l2_standard *e = arg;
  696. if (e->index < 0 )
  697. return -EINVAL;
  698. if( e->index < dev->ext_vv_data->num_stds ) {
  699. DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
  700. v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
  701. return 0;
  702. }
  703. return -EINVAL;
  704. }
  705. */
  706. static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
  707. {
  708. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  709. struct saa7146_vv *vv = dev->vv_data;
  710. int found = 0;
  711. int err, i;
  712. DEB_EE("VIDIOC_S_STD\n");
  713. if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
  714. DEB_D("cannot change video standard while streaming capture is active\n");
  715. return -EBUSY;
  716. }
  717. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  718. vv->ov_suspend = vv->video_fh;
  719. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  720. if (0 != err) {
  721. DEB_D("suspending video failed. aborting\n");
  722. return err;
  723. }
  724. }
  725. for (i = 0; i < dev->ext_vv_data->num_stds; i++)
  726. if (id & dev->ext_vv_data->stds[i].id)
  727. break;
  728. if (i != dev->ext_vv_data->num_stds) {
  729. vv->standard = &dev->ext_vv_data->stds[i];
  730. if (NULL != dev->ext_vv_data->std_callback)
  731. dev->ext_vv_data->std_callback(dev, vv->standard);
  732. found = 1;
  733. }
  734. if (vv->ov_suspend != NULL) {
  735. saa7146_start_preview(vv->ov_suspend);
  736. vv->ov_suspend = NULL;
  737. }
  738. if (!found) {
  739. DEB_EE("VIDIOC_S_STD: standard not found\n");
  740. return -EINVAL;
  741. }
  742. DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
  743. return 0;
  744. }
  745. static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
  746. {
  747. int err;
  748. DEB_D("VIDIOC_OVERLAY on:%d\n", on);
  749. if (on)
  750. err = saa7146_start_preview(fh);
  751. else
  752. err = saa7146_stop_preview(fh);
  753. return err;
  754. }
  755. static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
  756. {
  757. struct saa7146_fh *fh = __fh;
  758. if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  759. return videobuf_reqbufs(&fh->video_q, b);
  760. if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  761. return videobuf_reqbufs(&fh->vbi_q, b);
  762. return -EINVAL;
  763. }
  764. static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  765. {
  766. struct saa7146_fh *fh = __fh;
  767. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  768. return videobuf_querybuf(&fh->video_q, buf);
  769. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  770. return videobuf_querybuf(&fh->vbi_q, buf);
  771. return -EINVAL;
  772. }
  773. static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  774. {
  775. struct saa7146_fh *fh = __fh;
  776. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  777. return videobuf_qbuf(&fh->video_q, buf);
  778. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  779. return videobuf_qbuf(&fh->vbi_q, buf);
  780. return -EINVAL;
  781. }
  782. static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  783. {
  784. struct saa7146_fh *fh = __fh;
  785. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  786. return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
  787. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  788. return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
  789. return -EINVAL;
  790. }
  791. static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
  792. {
  793. struct saa7146_fh *fh = __fh;
  794. int err;
  795. DEB_D("VIDIOC_STREAMON, type:%d\n", type);
  796. err = video_begin(fh);
  797. if (err)
  798. return err;
  799. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  800. return videobuf_streamon(&fh->video_q);
  801. if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
  802. return videobuf_streamon(&fh->vbi_q);
  803. return -EINVAL;
  804. }
  805. static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
  806. {
  807. struct saa7146_fh *fh = __fh;
  808. struct saa7146_dev *dev = fh->dev;
  809. struct saa7146_vv *vv = dev->vv_data;
  810. int err;
  811. DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
  812. /* ugly: we need to copy some checks from video_end(),
  813. because videobuf_streamoff() relies on the capture running.
  814. check and fix this */
  815. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  816. DEB_S("not capturing\n");
  817. return 0;
  818. }
  819. if (vv->video_fh != fh) {
  820. DEB_S("capturing, but in another open\n");
  821. return -EBUSY;
  822. }
  823. err = -EINVAL;
  824. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  825. err = videobuf_streamoff(&fh->video_q);
  826. else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
  827. err = videobuf_streamoff(&fh->vbi_q);
  828. if (0 != err) {
  829. DEB_D("warning: videobuf_streamoff() failed\n");
  830. video_end(fh, file);
  831. } else {
  832. err = video_end(fh, file);
  833. }
  834. return err;
  835. }
  836. const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
  837. .vidioc_querycap = vidioc_querycap,
  838. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  839. .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
  840. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  841. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  842. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  843. .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  844. .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  845. .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  846. .vidioc_overlay = vidioc_overlay,
  847. .vidioc_g_fbuf = vidioc_g_fbuf,
  848. .vidioc_s_fbuf = vidioc_s_fbuf,
  849. .vidioc_reqbufs = vidioc_reqbufs,
  850. .vidioc_querybuf = vidioc_querybuf,
  851. .vidioc_qbuf = vidioc_qbuf,
  852. .vidioc_dqbuf = vidioc_dqbuf,
  853. .vidioc_g_std = vidioc_g_std,
  854. .vidioc_s_std = vidioc_s_std,
  855. .vidioc_streamon = vidioc_streamon,
  856. .vidioc_streamoff = vidioc_streamoff,
  857. .vidioc_g_parm = vidioc_g_parm,
  858. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  859. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  860. };
  861. const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops = {
  862. .vidioc_querycap = vidioc_querycap,
  863. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  864. .vidioc_reqbufs = vidioc_reqbufs,
  865. .vidioc_querybuf = vidioc_querybuf,
  866. .vidioc_qbuf = vidioc_qbuf,
  867. .vidioc_dqbuf = vidioc_dqbuf,
  868. .vidioc_g_std = vidioc_g_std,
  869. .vidioc_s_std = vidioc_s_std,
  870. .vidioc_streamon = vidioc_streamon,
  871. .vidioc_streamoff = vidioc_streamoff,
  872. .vidioc_g_parm = vidioc_g_parm,
  873. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  874. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  875. };
  876. /*********************************************************************************/
  877. /* buffer handling functions */
  878. static int buffer_activate (struct saa7146_dev *dev,
  879. struct saa7146_buf *buf,
  880. struct saa7146_buf *next)
  881. {
  882. struct saa7146_vv *vv = dev->vv_data;
  883. buf->vb.state = VIDEOBUF_ACTIVE;
  884. saa7146_set_capture(dev,buf,next);
  885. mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT);
  886. return 0;
  887. }
  888. static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
  889. {
  890. saa7146_pgtable_free(dev->pci, &buf->pt[0]);
  891. saa7146_pgtable_free(dev->pci, &buf->pt[1]);
  892. saa7146_pgtable_free(dev->pci, &buf->pt[2]);
  893. }
  894. static int buffer_prepare(struct videobuf_queue *q,
  895. struct videobuf_buffer *vb, enum v4l2_field field)
  896. {
  897. struct file *file = q->priv_data;
  898. struct saa7146_fh *fh = file->private_data;
  899. struct saa7146_dev *dev = fh->dev;
  900. struct saa7146_vv *vv = dev->vv_data;
  901. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  902. int size,err = 0;
  903. DEB_CAP("vbuf:%p\n", vb);
  904. /* sanity checks */
  905. if (vv->video_fmt.width < 48 ||
  906. vv->video_fmt.height < 32 ||
  907. vv->video_fmt.width > vv->standard->h_max_out ||
  908. vv->video_fmt.height > vv->standard->v_max_out) {
  909. DEB_D("w (%d) / h (%d) out of bounds\n",
  910. vv->video_fmt.width, vv->video_fmt.height);
  911. return -EINVAL;
  912. }
  913. size = vv->video_fmt.sizeimage;
  914. if (0 != buf->vb.baddr && buf->vb.bsize < size) {
  915. DEB_D("size mismatch\n");
  916. return -EINVAL;
  917. }
  918. DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
  919. vv->video_fmt.width, vv->video_fmt.height,
  920. size, v4l2_field_names[vv->video_fmt.field]);
  921. if (buf->vb.width != vv->video_fmt.width ||
  922. buf->vb.bytesperline != vv->video_fmt.bytesperline ||
  923. buf->vb.height != vv->video_fmt.height ||
  924. buf->vb.size != size ||
  925. buf->vb.field != field ||
  926. buf->vb.field != vv->video_fmt.field ||
  927. buf->fmt != &vv->video_fmt) {
  928. saa7146_dma_free(dev,q,buf);
  929. }
  930. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  931. struct saa7146_format *sfmt;
  932. buf->vb.bytesperline = vv->video_fmt.bytesperline;
  933. buf->vb.width = vv->video_fmt.width;
  934. buf->vb.height = vv->video_fmt.height;
  935. buf->vb.size = size;
  936. buf->vb.field = field;
  937. buf->fmt = &vv->video_fmt;
  938. buf->vb.field = vv->video_fmt.field;
  939. sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
  940. release_all_pagetables(dev, buf);
  941. if( 0 != IS_PLANAR(sfmt->trans)) {
  942. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  943. saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
  944. saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
  945. } else {
  946. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  947. }
  948. err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
  949. if (err)
  950. goto oops;
  951. err = saa7146_pgtable_build(dev,buf);
  952. if (err)
  953. goto oops;
  954. }
  955. buf->vb.state = VIDEOBUF_PREPARED;
  956. buf->activate = buffer_activate;
  957. return 0;
  958. oops:
  959. DEB_D("error out\n");
  960. saa7146_dma_free(dev,q,buf);
  961. return err;
  962. }
  963. static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  964. {
  965. struct file *file = q->priv_data;
  966. struct saa7146_fh *fh = file->private_data;
  967. struct saa7146_vv *vv = fh->dev->vv_data;
  968. if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
  969. *count = MAX_SAA7146_CAPTURE_BUFFERS;
  970. *size = vv->video_fmt.sizeimage;
  971. /* check if we exceed the "max_memory" parameter */
  972. if( (*count * *size) > (max_memory*1048576) ) {
  973. *count = (max_memory*1048576) / *size;
  974. }
  975. DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
  976. return 0;
  977. }
  978. static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  979. {
  980. struct file *file = q->priv_data;
  981. struct saa7146_fh *fh = file->private_data;
  982. struct saa7146_dev *dev = fh->dev;
  983. struct saa7146_vv *vv = dev->vv_data;
  984. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  985. DEB_CAP("vbuf:%p\n", vb);
  986. saa7146_buffer_queue(fh->dev, &vv->video_dmaq, buf);
  987. }
  988. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  989. {
  990. struct file *file = q->priv_data;
  991. struct saa7146_fh *fh = file->private_data;
  992. struct saa7146_dev *dev = fh->dev;
  993. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  994. DEB_CAP("vbuf:%p\n", vb);
  995. saa7146_dma_free(dev,q,buf);
  996. release_all_pagetables(dev, buf);
  997. }
  998. static struct videobuf_queue_ops video_qops = {
  999. .buf_setup = buffer_setup,
  1000. .buf_prepare = buffer_prepare,
  1001. .buf_queue = buffer_queue,
  1002. .buf_release = buffer_release,
  1003. };
  1004. /********************************************************************************/
  1005. /* file operations */
  1006. static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
  1007. {
  1008. INIT_LIST_HEAD(&vv->video_dmaq.queue);
  1009. init_timer(&vv->video_dmaq.timeout);
  1010. vv->video_dmaq.timeout.function = saa7146_buffer_timeout;
  1011. vv->video_dmaq.timeout.data = (unsigned long)(&vv->video_dmaq);
  1012. vv->video_dmaq.dev = dev;
  1013. /* set some default values */
  1014. vv->standard = &dev->ext_vv_data->stds[0];
  1015. /* FIXME: what's this? */
  1016. vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
  1017. vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
  1018. }
  1019. static int video_open(struct saa7146_dev *dev, struct file *file)
  1020. {
  1021. struct saa7146_fh *fh = file->private_data;
  1022. videobuf_queue_sg_init(&fh->video_q, &video_qops,
  1023. &dev->pci->dev, &dev->slock,
  1024. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1025. V4L2_FIELD_INTERLACED,
  1026. sizeof(struct saa7146_buf),
  1027. file, &dev->v4l2_lock);
  1028. return 0;
  1029. }
  1030. static void video_close(struct saa7146_dev *dev, struct file *file)
  1031. {
  1032. struct saa7146_fh *fh = file->private_data;
  1033. struct saa7146_vv *vv = dev->vv_data;
  1034. struct videobuf_queue *q = &fh->video_q;
  1035. if (IS_CAPTURE_ACTIVE(fh) != 0)
  1036. video_end(fh, file);
  1037. else if (IS_OVERLAY_ACTIVE(fh) != 0)
  1038. saa7146_stop_preview(fh);
  1039. videobuf_stop(q);
  1040. /* hmm, why is this function declared void? */
  1041. }
  1042. static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
  1043. {
  1044. struct saa7146_vv *vv = dev->vv_data;
  1045. struct saa7146_dmaqueue *q = &vv->video_dmaq;
  1046. spin_lock(&dev->slock);
  1047. DEB_CAP("called\n");
  1048. /* only finish the buffer if we have one... */
  1049. if( NULL != q->curr ) {
  1050. saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
  1051. }
  1052. saa7146_buffer_next(dev,q,0);
  1053. spin_unlock(&dev->slock);
  1054. }
  1055. static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1056. {
  1057. struct saa7146_fh *fh = file->private_data;
  1058. struct saa7146_dev *dev = fh->dev;
  1059. struct saa7146_vv *vv = dev->vv_data;
  1060. ssize_t ret = 0;
  1061. DEB_EE("called\n");
  1062. if ((vv->video_status & STATUS_CAPTURE) != 0) {
  1063. /* fixme: should we allow read() captures while streaming capture? */
  1064. if (vv->video_fh == fh) {
  1065. DEB_S("already capturing\n");
  1066. return -EBUSY;
  1067. }
  1068. DEB_S("already capturing in another open\n");
  1069. return -EBUSY;
  1070. }
  1071. ret = video_begin(fh);
  1072. if( 0 != ret) {
  1073. goto out;
  1074. }
  1075. ret = videobuf_read_one(&fh->video_q , data, count, ppos,
  1076. file->f_flags & O_NONBLOCK);
  1077. if (ret != 0) {
  1078. video_end(fh, file);
  1079. } else {
  1080. ret = video_end(fh, file);
  1081. }
  1082. out:
  1083. /* restart overlay if it was active before */
  1084. if (vv->ov_suspend != NULL) {
  1085. saa7146_start_preview(vv->ov_suspend);
  1086. vv->ov_suspend = NULL;
  1087. }
  1088. return ret;
  1089. }
  1090. struct saa7146_use_ops saa7146_video_uops = {
  1091. .init = video_init,
  1092. .open = video_open,
  1093. .release = video_close,
  1094. .irq_done = video_irq_done,
  1095. .read = video_read,
  1096. };