saa7146_video.c 37 KB

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