saa7146_video.c 36 KB

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