saa7146_video.c 35 KB

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