saa7146_video.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. #include <linux/kernel.h>
  7. static int max_memory = 32;
  8. module_param(max_memory, int, 0644);
  9. MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
  10. #define IS_CAPTURE_ACTIVE(fh) \
  11. (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
  12. #define IS_OVERLAY_ACTIVE(fh) \
  13. (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
  14. /* format descriptions for capture and preview */
  15. static struct saa7146_format formats[] = {
  16. {
  17. .name = "RGB-8 (3-3-2)",
  18. .pixelformat = V4L2_PIX_FMT_RGB332,
  19. .trans = RGB08_COMPOSED,
  20. .depth = 8,
  21. .flags = 0,
  22. }, {
  23. .name = "RGB-16 (5/B-6/G-5/R)",
  24. .pixelformat = V4L2_PIX_FMT_RGB565,
  25. .trans = RGB16_COMPOSED,
  26. .depth = 16,
  27. .flags = 0,
  28. }, {
  29. .name = "RGB-24 (B-G-R)",
  30. .pixelformat = V4L2_PIX_FMT_BGR24,
  31. .trans = RGB24_COMPOSED,
  32. .depth = 24,
  33. .flags = 0,
  34. }, {
  35. .name = "RGB-32 (B-G-R)",
  36. .pixelformat = V4L2_PIX_FMT_BGR32,
  37. .trans = RGB32_COMPOSED,
  38. .depth = 32,
  39. .flags = 0,
  40. }, {
  41. .name = "RGB-32 (R-G-B)",
  42. .pixelformat = V4L2_PIX_FMT_RGB32,
  43. .trans = RGB32_COMPOSED,
  44. .depth = 32,
  45. .flags = 0,
  46. .swap = 0x2,
  47. }, {
  48. .name = "Greyscale-8",
  49. .pixelformat = V4L2_PIX_FMT_GREY,
  50. .trans = Y8,
  51. .depth = 8,
  52. .flags = 0,
  53. }, {
  54. .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
  55. .pixelformat = V4L2_PIX_FMT_YUV422P,
  56. .trans = YUV422_DECOMPOSED,
  57. .depth = 16,
  58. .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
  59. }, {
  60. .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
  61. .pixelformat = V4L2_PIX_FMT_YVU420,
  62. .trans = YUV420_DECOMPOSED,
  63. .depth = 12,
  64. .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
  65. }, {
  66. .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
  67. .pixelformat = V4L2_PIX_FMT_YUV420,
  68. .trans = YUV420_DECOMPOSED,
  69. .depth = 12,
  70. .flags = FORMAT_IS_PLANAR,
  71. }, {
  72. .name = "YUV 4:2:2 (U-Y-V-Y)",
  73. .pixelformat = V4L2_PIX_FMT_UYVY,
  74. .trans = YUV422_COMPOSED,
  75. .depth = 16,
  76. .flags = 0,
  77. }
  78. };
  79. /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
  80. due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
  81. (like V4L2_PIX_FMT_YUYV) ... 8-( */
  82. struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
  83. {
  84. int i;
  85. for (i = 0; i < ARRAY_SIZE(formats); 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 >= ARRAY_SIZE(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 const 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. timer_setup(&vv->video_dmaq.timeout, saa7146_buffer_timeout, 0);
  1013. vv->video_dmaq.dev = dev;
  1014. /* set some default values */
  1015. vv->standard = &dev->ext_vv_data->stds[0];
  1016. /* FIXME: what's this? */
  1017. vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
  1018. vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
  1019. }
  1020. static int video_open(struct saa7146_dev *dev, struct file *file)
  1021. {
  1022. struct saa7146_fh *fh = file->private_data;
  1023. videobuf_queue_sg_init(&fh->video_q, &video_qops,
  1024. &dev->pci->dev, &dev->slock,
  1025. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1026. V4L2_FIELD_INTERLACED,
  1027. sizeof(struct saa7146_buf),
  1028. file, &dev->v4l2_lock);
  1029. return 0;
  1030. }
  1031. static void video_close(struct saa7146_dev *dev, struct file *file)
  1032. {
  1033. struct saa7146_fh *fh = file->private_data;
  1034. struct saa7146_vv *vv = dev->vv_data;
  1035. struct videobuf_queue *q = &fh->video_q;
  1036. if (IS_CAPTURE_ACTIVE(fh) != 0)
  1037. video_end(fh, file);
  1038. else if (IS_OVERLAY_ACTIVE(fh) != 0)
  1039. saa7146_stop_preview(fh);
  1040. videobuf_stop(q);
  1041. /* hmm, why is this function declared void? */
  1042. }
  1043. static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
  1044. {
  1045. struct saa7146_vv *vv = dev->vv_data;
  1046. struct saa7146_dmaqueue *q = &vv->video_dmaq;
  1047. spin_lock(&dev->slock);
  1048. DEB_CAP("called\n");
  1049. /* only finish the buffer if we have one... */
  1050. if( NULL != q->curr ) {
  1051. saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
  1052. }
  1053. saa7146_buffer_next(dev,q,0);
  1054. spin_unlock(&dev->slock);
  1055. }
  1056. static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1057. {
  1058. struct saa7146_fh *fh = file->private_data;
  1059. struct saa7146_dev *dev = fh->dev;
  1060. struct saa7146_vv *vv = dev->vv_data;
  1061. ssize_t ret = 0;
  1062. DEB_EE("called\n");
  1063. if ((vv->video_status & STATUS_CAPTURE) != 0) {
  1064. /* fixme: should we allow read() captures while streaming capture? */
  1065. if (vv->video_fh == fh) {
  1066. DEB_S("already capturing\n");
  1067. return -EBUSY;
  1068. }
  1069. DEB_S("already capturing in another open\n");
  1070. return -EBUSY;
  1071. }
  1072. ret = video_begin(fh);
  1073. if( 0 != ret) {
  1074. goto out;
  1075. }
  1076. ret = videobuf_read_one(&fh->video_q , data, count, ppos,
  1077. file->f_flags & O_NONBLOCK);
  1078. if (ret != 0) {
  1079. video_end(fh, file);
  1080. } else {
  1081. ret = video_end(fh, file);
  1082. }
  1083. out:
  1084. /* restart overlay if it was active before */
  1085. if (vv->ov_suspend != NULL) {
  1086. saa7146_start_preview(vv->ov_suspend);
  1087. vv->ov_suspend = NULL;
  1088. }
  1089. return ret;
  1090. }
  1091. const struct saa7146_use_ops saa7146_video_uops = {
  1092. .init = video_init,
  1093. .open = video_open,
  1094. .release = video_close,
  1095. .irq_done = video_irq_done,
  1096. .read = video_read,
  1097. };