saa7164-vbi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include "saa7164.h"
  22. static struct saa7164_tvnorm saa7164_tvnorms[] = {
  23. {
  24. .name = "NTSC-M",
  25. .id = V4L2_STD_NTSC_M,
  26. }, {
  27. .name = "NTSC-JP",
  28. .id = V4L2_STD_NTSC_M_JP,
  29. }
  30. };
  31. static const u32 saa7164_v4l2_ctrls[] = {
  32. 0
  33. };
  34. /* Take the encoder configuration from the port struct and
  35. * flush it to the hardware.
  36. */
  37. static void saa7164_vbi_configure(struct saa7164_port *port)
  38. {
  39. struct saa7164_dev *dev = port->dev;
  40. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  41. port->vbi_params.width = port->width;
  42. port->vbi_params.height = port->height;
  43. port->vbi_params.is_50hz =
  44. (port->encodernorm.id & V4L2_STD_625_50) != 0;
  45. /* Set up the DIF (enable it) for analog mode by default */
  46. saa7164_api_initialize_dif(port);
  47. /* Configure the correct video standard */
  48. #if 0
  49. saa7164_api_configure_dif(port, port->encodernorm.id);
  50. #endif
  51. #if 0
  52. /* Ensure the audio decoder is correct configured */
  53. saa7164_api_set_audio_std(port);
  54. #endif
  55. dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
  56. }
  57. static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
  58. {
  59. struct list_head *c, *n, *p, *q, *l, *v;
  60. struct saa7164_dev *dev = port->dev;
  61. struct saa7164_buffer *buf;
  62. struct saa7164_user_buffer *ubuf;
  63. /* Remove any allocated buffers */
  64. mutex_lock(&port->dmaqueue_lock);
  65. dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
  66. list_for_each_safe(c, n, &port->dmaqueue.list) {
  67. buf = list_entry(c, struct saa7164_buffer, list);
  68. list_del(c);
  69. saa7164_buffer_dealloc(buf);
  70. }
  71. dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
  72. list_for_each_safe(p, q, &port->list_buf_used.list) {
  73. ubuf = list_entry(p, struct saa7164_user_buffer, list);
  74. list_del(p);
  75. saa7164_buffer_dealloc_user(ubuf);
  76. }
  77. dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
  78. list_for_each_safe(l, v, &port->list_buf_free.list) {
  79. ubuf = list_entry(l, struct saa7164_user_buffer, list);
  80. list_del(l);
  81. saa7164_buffer_dealloc_user(ubuf);
  82. }
  83. mutex_unlock(&port->dmaqueue_lock);
  84. dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
  85. return 0;
  86. }
  87. /* Dynamic buffer switch at vbi start time */
  88. static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
  89. {
  90. struct saa7164_dev *dev = port->dev;
  91. struct saa7164_buffer *buf;
  92. struct saa7164_user_buffer *ubuf;
  93. struct tmHWStreamParameters *params = &port->hw_streamingparams;
  94. int result = -ENODEV, i;
  95. int len = 0;
  96. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  97. /* TODO: NTSC SPECIFIC */
  98. /* Init and establish defaults */
  99. params->samplesperline = 1440;
  100. params->numberoflines = 12;
  101. params->numberoflines = 18;
  102. params->pitch = 1600;
  103. params->pitch = 1440;
  104. params->numpagetables = 2 +
  105. ((params->numberoflines * params->pitch) / PAGE_SIZE);
  106. params->bitspersample = 8;
  107. params->linethreshold = 0;
  108. params->pagetablelistvirt = NULL;
  109. params->pagetablelistphys = NULL;
  110. params->numpagetableentries = port->hwcfg.buffercount;
  111. /* Allocate the PCI resources, buffers (hard) */
  112. for (i = 0; i < port->hwcfg.buffercount; i++) {
  113. buf = saa7164_buffer_alloc(port,
  114. params->numberoflines *
  115. params->pitch);
  116. if (!buf) {
  117. printk(KERN_ERR "%s() failed "
  118. "(errno = %d), unable to allocate buffer\n",
  119. __func__, result);
  120. result = -ENOMEM;
  121. goto failed;
  122. } else {
  123. mutex_lock(&port->dmaqueue_lock);
  124. list_add_tail(&buf->list, &port->dmaqueue.list);
  125. mutex_unlock(&port->dmaqueue_lock);
  126. }
  127. }
  128. /* Allocate some kernel buffers for copying
  129. * to userpsace.
  130. */
  131. len = params->numberoflines * params->pitch;
  132. if (vbi_buffers < 16)
  133. vbi_buffers = 16;
  134. if (vbi_buffers > 512)
  135. vbi_buffers = 512;
  136. for (i = 0; i < vbi_buffers; i++) {
  137. ubuf = saa7164_buffer_alloc_user(dev, len);
  138. if (ubuf) {
  139. mutex_lock(&port->dmaqueue_lock);
  140. list_add_tail(&ubuf->list, &port->list_buf_free.list);
  141. mutex_unlock(&port->dmaqueue_lock);
  142. }
  143. }
  144. result = 0;
  145. failed:
  146. return result;
  147. }
  148. static int saa7164_vbi_initialize(struct saa7164_port *port)
  149. {
  150. saa7164_vbi_configure(port);
  151. return 0;
  152. }
  153. /* -- V4L2 --------------------------------------------------------- */
  154. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
  155. {
  156. struct saa7164_vbi_fh *fh = file->private_data;
  157. struct saa7164_port *port = fh->port;
  158. struct saa7164_dev *dev = port->dev;
  159. unsigned int i;
  160. dprintk(DBGLVL_VBI, "%s(id=0x%x)\n", __func__, (u32)id);
  161. for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
  162. if (id & saa7164_tvnorms[i].id)
  163. break;
  164. }
  165. if (i == ARRAY_SIZE(saa7164_tvnorms))
  166. return -EINVAL;
  167. port->encodernorm = saa7164_tvnorms[i];
  168. port->std = id;
  169. /* Update the audio decoder while is not running in
  170. * auto detect mode.
  171. */
  172. saa7164_api_set_audio_std(port);
  173. dprintk(DBGLVL_VBI, "%s(id=0x%x) OK\n", __func__, (u32)id);
  174. return 0;
  175. }
  176. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  177. {
  178. struct saa7164_encoder_fh *fh = file->private_data;
  179. struct saa7164_port *port = fh->port;
  180. *id = port->std;
  181. return 0;
  182. }
  183. static int vidioc_enum_input(struct file *file, void *priv,
  184. struct v4l2_input *i)
  185. {
  186. int n;
  187. char *inputs[] = { "tuner", "composite", "svideo", "aux",
  188. "composite 2", "svideo 2", "aux 2" };
  189. if (i->index >= 7)
  190. return -EINVAL;
  191. strcpy(i->name, inputs[i->index]);
  192. if (i->index == 0)
  193. i->type = V4L2_INPUT_TYPE_TUNER;
  194. else
  195. i->type = V4L2_INPUT_TYPE_CAMERA;
  196. for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
  197. i->std |= saa7164_tvnorms[n].id;
  198. return 0;
  199. }
  200. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  201. {
  202. struct saa7164_vbi_fh *fh = file->private_data;
  203. struct saa7164_port *port = fh->port;
  204. struct saa7164_dev *dev = port->dev;
  205. if (saa7164_api_get_videomux(port) != SAA_OK)
  206. return -EIO;
  207. *i = (port->mux_input - 1);
  208. dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, *i);
  209. return 0;
  210. }
  211. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  212. {
  213. struct saa7164_vbi_fh *fh = file->private_data;
  214. struct saa7164_port *port = fh->port;
  215. struct saa7164_dev *dev = port->dev;
  216. dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, i);
  217. if (i >= 7)
  218. return -EINVAL;
  219. port->mux_input = i + 1;
  220. if (saa7164_api_set_videomux(port) != SAA_OK)
  221. return -EIO;
  222. return 0;
  223. }
  224. static int vidioc_g_tuner(struct file *file, void *priv,
  225. struct v4l2_tuner *t)
  226. {
  227. struct saa7164_vbi_fh *fh = file->private_data;
  228. struct saa7164_port *port = fh->port;
  229. struct saa7164_dev *dev = port->dev;
  230. if (0 != t->index)
  231. return -EINVAL;
  232. strcpy(t->name, "tuner");
  233. t->type = V4L2_TUNER_ANALOG_TV;
  234. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
  235. dprintk(DBGLVL_VBI, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
  236. return 0;
  237. }
  238. static int vidioc_s_tuner(struct file *file, void *priv,
  239. const struct v4l2_tuner *t)
  240. {
  241. /* Update the A/V core */
  242. return 0;
  243. }
  244. static int vidioc_g_frequency(struct file *file, void *priv,
  245. struct v4l2_frequency *f)
  246. {
  247. struct saa7164_vbi_fh *fh = file->private_data;
  248. struct saa7164_port *port = fh->port;
  249. f->type = V4L2_TUNER_ANALOG_TV;
  250. f->frequency = port->freq;
  251. return 0;
  252. }
  253. static int vidioc_s_frequency(struct file *file, void *priv,
  254. const struct v4l2_frequency *f)
  255. {
  256. struct saa7164_vbi_fh *fh = file->private_data;
  257. struct saa7164_port *port = fh->port;
  258. struct saa7164_dev *dev = port->dev;
  259. struct saa7164_port *tsport;
  260. struct dvb_frontend *fe;
  261. /* TODO: Pull this for the std */
  262. struct analog_parameters params = {
  263. .mode = V4L2_TUNER_ANALOG_TV,
  264. .audmode = V4L2_TUNER_MODE_STEREO,
  265. .std = port->encodernorm.id,
  266. .frequency = f->frequency
  267. };
  268. /* Stop the encoder */
  269. dprintk(DBGLVL_VBI, "%s() frequency=%d tuner=%d\n", __func__,
  270. f->frequency, f->tuner);
  271. if (f->tuner != 0)
  272. return -EINVAL;
  273. if (f->type != V4L2_TUNER_ANALOG_TV)
  274. return -EINVAL;
  275. port->freq = f->frequency;
  276. /* Update the hardware */
  277. if (port->nr == SAA7164_PORT_VBI1)
  278. tsport = &dev->ports[SAA7164_PORT_TS1];
  279. else
  280. if (port->nr == SAA7164_PORT_VBI2)
  281. tsport = &dev->ports[SAA7164_PORT_TS2];
  282. else
  283. BUG();
  284. fe = tsport->dvb.frontend;
  285. if (fe && fe->ops.tuner_ops.set_analog_params)
  286. fe->ops.tuner_ops.set_analog_params(fe, &params);
  287. else
  288. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  289. saa7164_vbi_initialize(port);
  290. return 0;
  291. }
  292. static int vidioc_g_ctrl(struct file *file, void *priv,
  293. struct v4l2_control *ctl)
  294. {
  295. struct saa7164_vbi_fh *fh = file->private_data;
  296. struct saa7164_port *port = fh->port;
  297. struct saa7164_dev *dev = port->dev;
  298. dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
  299. ctl->id, ctl->value);
  300. switch (ctl->id) {
  301. case V4L2_CID_BRIGHTNESS:
  302. ctl->value = port->ctl_brightness;
  303. break;
  304. case V4L2_CID_CONTRAST:
  305. ctl->value = port->ctl_contrast;
  306. break;
  307. case V4L2_CID_SATURATION:
  308. ctl->value = port->ctl_saturation;
  309. break;
  310. case V4L2_CID_HUE:
  311. ctl->value = port->ctl_hue;
  312. break;
  313. case V4L2_CID_SHARPNESS:
  314. ctl->value = port->ctl_sharpness;
  315. break;
  316. case V4L2_CID_AUDIO_VOLUME:
  317. ctl->value = port->ctl_volume;
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. return 0;
  323. }
  324. static int vidioc_s_ctrl(struct file *file, void *priv,
  325. struct v4l2_control *ctl)
  326. {
  327. struct saa7164_vbi_fh *fh = file->private_data;
  328. struct saa7164_port *port = fh->port;
  329. struct saa7164_dev *dev = port->dev;
  330. int ret = 0;
  331. dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
  332. ctl->id, ctl->value);
  333. switch (ctl->id) {
  334. case V4L2_CID_BRIGHTNESS:
  335. if ((ctl->value >= 0) && (ctl->value <= 255)) {
  336. port->ctl_brightness = ctl->value;
  337. saa7164_api_set_usercontrol(port,
  338. PU_BRIGHTNESS_CONTROL);
  339. } else
  340. ret = -EINVAL;
  341. break;
  342. case V4L2_CID_CONTRAST:
  343. if ((ctl->value >= 0) && (ctl->value <= 255)) {
  344. port->ctl_contrast = ctl->value;
  345. saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
  346. } else
  347. ret = -EINVAL;
  348. break;
  349. case V4L2_CID_SATURATION:
  350. if ((ctl->value >= 0) && (ctl->value <= 255)) {
  351. port->ctl_saturation = ctl->value;
  352. saa7164_api_set_usercontrol(port,
  353. PU_SATURATION_CONTROL);
  354. } else
  355. ret = -EINVAL;
  356. break;
  357. case V4L2_CID_HUE:
  358. if ((ctl->value >= 0) && (ctl->value <= 255)) {
  359. port->ctl_hue = ctl->value;
  360. saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
  361. } else
  362. ret = -EINVAL;
  363. break;
  364. case V4L2_CID_SHARPNESS:
  365. if ((ctl->value >= 0) && (ctl->value <= 255)) {
  366. port->ctl_sharpness = ctl->value;
  367. saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
  368. } else
  369. ret = -EINVAL;
  370. break;
  371. case V4L2_CID_AUDIO_VOLUME:
  372. if ((ctl->value >= -83) && (ctl->value <= 24)) {
  373. port->ctl_volume = ctl->value;
  374. saa7164_api_set_audio_volume(port, port->ctl_volume);
  375. } else
  376. ret = -EINVAL;
  377. break;
  378. default:
  379. ret = -EINVAL;
  380. }
  381. return ret;
  382. }
  383. static int saa7164_get_ctrl(struct saa7164_port *port,
  384. struct v4l2_ext_control *ctrl)
  385. {
  386. struct saa7164_vbi_params *params = &port->vbi_params;
  387. switch (ctrl->id) {
  388. case V4L2_CID_MPEG_STREAM_TYPE:
  389. ctrl->value = params->stream_type;
  390. break;
  391. case V4L2_CID_MPEG_AUDIO_MUTE:
  392. ctrl->value = params->ctl_mute;
  393. break;
  394. case V4L2_CID_MPEG_VIDEO_ASPECT:
  395. ctrl->value = params->ctl_aspect;
  396. break;
  397. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  398. ctrl->value = params->refdist;
  399. break;
  400. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  401. ctrl->value = params->gop_size;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. return 0;
  407. }
  408. static int vidioc_g_ext_ctrls(struct file *file, void *priv,
  409. struct v4l2_ext_controls *ctrls)
  410. {
  411. struct saa7164_vbi_fh *fh = file->private_data;
  412. struct saa7164_port *port = fh->port;
  413. int i, err = 0;
  414. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  415. for (i = 0; i < ctrls->count; i++) {
  416. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  417. err = saa7164_get_ctrl(port, ctrl);
  418. if (err) {
  419. ctrls->error_idx = i;
  420. break;
  421. }
  422. }
  423. return err;
  424. }
  425. return -EINVAL;
  426. }
  427. static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
  428. {
  429. int ret = -EINVAL;
  430. switch (ctrl->id) {
  431. case V4L2_CID_MPEG_STREAM_TYPE:
  432. if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
  433. (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
  434. ret = 0;
  435. break;
  436. case V4L2_CID_MPEG_AUDIO_MUTE:
  437. if ((ctrl->value >= 0) &&
  438. (ctrl->value <= 1))
  439. ret = 0;
  440. break;
  441. case V4L2_CID_MPEG_VIDEO_ASPECT:
  442. if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
  443. (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
  444. ret = 0;
  445. break;
  446. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  447. if ((ctrl->value >= 0) &&
  448. (ctrl->value <= 255))
  449. ret = 0;
  450. break;
  451. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  452. if ((ctrl->value >= 1) &&
  453. (ctrl->value <= 3))
  454. ret = 0;
  455. break;
  456. default:
  457. ret = -EINVAL;
  458. }
  459. return ret;
  460. }
  461. static int vidioc_try_ext_ctrls(struct file *file, void *priv,
  462. struct v4l2_ext_controls *ctrls)
  463. {
  464. int i, err = 0;
  465. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  466. for (i = 0; i < ctrls->count; i++) {
  467. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  468. err = saa7164_try_ctrl(ctrl, 0);
  469. if (err) {
  470. ctrls->error_idx = i;
  471. break;
  472. }
  473. }
  474. return err;
  475. }
  476. return -EINVAL;
  477. }
  478. static int saa7164_set_ctrl(struct saa7164_port *port,
  479. struct v4l2_ext_control *ctrl)
  480. {
  481. struct saa7164_vbi_params *params = &port->vbi_params;
  482. int ret = 0;
  483. switch (ctrl->id) {
  484. case V4L2_CID_MPEG_STREAM_TYPE:
  485. params->stream_type = ctrl->value;
  486. break;
  487. case V4L2_CID_MPEG_AUDIO_MUTE:
  488. params->ctl_mute = ctrl->value;
  489. ret = saa7164_api_audio_mute(port, params->ctl_mute);
  490. if (ret != SAA_OK) {
  491. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
  492. ret);
  493. ret = -EIO;
  494. }
  495. break;
  496. case V4L2_CID_MPEG_VIDEO_ASPECT:
  497. params->ctl_aspect = ctrl->value;
  498. ret = saa7164_api_set_aspect_ratio(port);
  499. if (ret != SAA_OK) {
  500. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
  501. ret);
  502. ret = -EIO;
  503. }
  504. break;
  505. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  506. params->refdist = ctrl->value;
  507. break;
  508. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  509. params->gop_size = ctrl->value;
  510. break;
  511. default:
  512. return -EINVAL;
  513. }
  514. /* TODO: Update the hardware */
  515. return ret;
  516. }
  517. static int vidioc_s_ext_ctrls(struct file *file, void *priv,
  518. struct v4l2_ext_controls *ctrls)
  519. {
  520. struct saa7164_vbi_fh *fh = file->private_data;
  521. struct saa7164_port *port = fh->port;
  522. int i, err = 0;
  523. if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  524. for (i = 0; i < ctrls->count; i++) {
  525. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  526. err = saa7164_try_ctrl(ctrl, 0);
  527. if (err) {
  528. ctrls->error_idx = i;
  529. break;
  530. }
  531. err = saa7164_set_ctrl(port, ctrl);
  532. if (err) {
  533. ctrls->error_idx = i;
  534. break;
  535. }
  536. }
  537. return err;
  538. }
  539. return -EINVAL;
  540. }
  541. static int vidioc_querycap(struct file *file, void *priv,
  542. struct v4l2_capability *cap)
  543. {
  544. struct saa7164_vbi_fh *fh = file->private_data;
  545. struct saa7164_port *port = fh->port;
  546. struct saa7164_dev *dev = port->dev;
  547. strcpy(cap->driver, dev->name);
  548. strlcpy(cap->card, saa7164_boards[dev->board].name,
  549. sizeof(cap->card));
  550. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  551. cap->capabilities =
  552. V4L2_CAP_VBI_CAPTURE |
  553. V4L2_CAP_READWRITE |
  554. 0;
  555. cap->capabilities |= V4L2_CAP_TUNER;
  556. cap->version = 0;
  557. return 0;
  558. }
  559. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  560. struct v4l2_fmtdesc *f)
  561. {
  562. if (f->index != 0)
  563. return -EINVAL;
  564. strlcpy(f->description, "VBI", sizeof(f->description));
  565. f->pixelformat = V4L2_PIX_FMT_MPEG;
  566. return 0;
  567. }
  568. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  569. struct v4l2_format *f)
  570. {
  571. struct saa7164_vbi_fh *fh = file->private_data;
  572. struct saa7164_port *port = fh->port;
  573. struct saa7164_dev *dev = port->dev;
  574. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  575. f->fmt.pix.bytesperline = 0;
  576. f->fmt.pix.sizeimage =
  577. port->ts_packet_size * port->ts_packet_count;
  578. f->fmt.pix.colorspace = 0;
  579. f->fmt.pix.width = port->width;
  580. f->fmt.pix.height = port->height;
  581. dprintk(DBGLVL_VBI, "VIDIOC_G_FMT: w: %d, h: %d\n",
  582. port->width, port->height);
  583. return 0;
  584. }
  585. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  586. struct v4l2_format *f)
  587. {
  588. struct saa7164_vbi_fh *fh = file->private_data;
  589. struct saa7164_port *port = fh->port;
  590. struct saa7164_dev *dev = port->dev;
  591. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  592. f->fmt.pix.bytesperline = 0;
  593. f->fmt.pix.sizeimage =
  594. port->ts_packet_size * port->ts_packet_count;
  595. f->fmt.pix.colorspace = 0;
  596. dprintk(DBGLVL_VBI, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
  597. port->width, port->height);
  598. return 0;
  599. }
  600. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  601. struct v4l2_format *f)
  602. {
  603. struct saa7164_vbi_fh *fh = file->private_data;
  604. struct saa7164_port *port = fh->port;
  605. struct saa7164_dev *dev = port->dev;
  606. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  607. f->fmt.pix.bytesperline = 0;
  608. f->fmt.pix.sizeimage =
  609. port->ts_packet_size * port->ts_packet_count;
  610. f->fmt.pix.colorspace = 0;
  611. dprintk(DBGLVL_VBI, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
  612. f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
  613. return 0;
  614. }
  615. static int fill_queryctrl(struct saa7164_vbi_params *params,
  616. struct v4l2_queryctrl *c)
  617. {
  618. switch (c->id) {
  619. case V4L2_CID_BRIGHTNESS:
  620. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
  621. case V4L2_CID_CONTRAST:
  622. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
  623. case V4L2_CID_SATURATION:
  624. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
  625. case V4L2_CID_HUE:
  626. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
  627. case V4L2_CID_SHARPNESS:
  628. return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
  629. case V4L2_CID_MPEG_AUDIO_MUTE:
  630. return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
  631. case V4L2_CID_AUDIO_VOLUME:
  632. return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
  633. case V4L2_CID_MPEG_STREAM_TYPE:
  634. return v4l2_ctrl_query_fill(c,
  635. V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
  636. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  637. 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
  638. case V4L2_CID_MPEG_VIDEO_ASPECT:
  639. return v4l2_ctrl_query_fill(c,
  640. V4L2_MPEG_VIDEO_ASPECT_1x1,
  641. V4L2_MPEG_VIDEO_ASPECT_221x100,
  642. 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
  643. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  644. return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
  645. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  646. return v4l2_ctrl_query_fill(c,
  647. 1, 3, 1, 1);
  648. default:
  649. return -EINVAL;
  650. }
  651. }
  652. static int vidioc_queryctrl(struct file *file, void *priv,
  653. struct v4l2_queryctrl *c)
  654. {
  655. struct saa7164_vbi_fh *fh = priv;
  656. struct saa7164_port *port = fh->port;
  657. int i, next;
  658. u32 id = c->id;
  659. memset(c, 0, sizeof(*c));
  660. next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
  661. c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
  662. for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
  663. if (next) {
  664. if (c->id < saa7164_v4l2_ctrls[i])
  665. c->id = saa7164_v4l2_ctrls[i];
  666. else
  667. continue;
  668. }
  669. if (c->id == saa7164_v4l2_ctrls[i])
  670. return fill_queryctrl(&port->vbi_params, c);
  671. if (c->id < saa7164_v4l2_ctrls[i])
  672. break;
  673. }
  674. return -EINVAL;
  675. }
  676. static int saa7164_vbi_stop_port(struct saa7164_port *port)
  677. {
  678. struct saa7164_dev *dev = port->dev;
  679. int ret;
  680. ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
  681. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  682. printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
  683. __func__, ret);
  684. ret = -EIO;
  685. } else {
  686. dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
  687. ret = 0;
  688. }
  689. return ret;
  690. }
  691. static int saa7164_vbi_acquire_port(struct saa7164_port *port)
  692. {
  693. struct saa7164_dev *dev = port->dev;
  694. int ret;
  695. ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  696. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  697. printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
  698. __func__, ret);
  699. ret = -EIO;
  700. } else {
  701. dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
  702. ret = 0;
  703. }
  704. return ret;
  705. }
  706. static int saa7164_vbi_pause_port(struct saa7164_port *port)
  707. {
  708. struct saa7164_dev *dev = port->dev;
  709. int ret;
  710. ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  711. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  712. printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
  713. __func__, ret);
  714. ret = -EIO;
  715. } else {
  716. dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
  717. ret = 0;
  718. }
  719. return ret;
  720. }
  721. /* Firmware is very windows centric, meaning you have to transition
  722. * the part through AVStream / KS Windows stages, forwards or backwards.
  723. * States are: stopped, acquired (h/w), paused, started.
  724. * We have to leave here will all of the soft buffers on the free list,
  725. * else the cfg_post() func won't have soft buffers to correctly configure.
  726. */
  727. static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
  728. {
  729. struct saa7164_dev *dev = port->dev;
  730. struct saa7164_buffer *buf;
  731. struct saa7164_user_buffer *ubuf;
  732. struct list_head *c, *n;
  733. int ret;
  734. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  735. ret = saa7164_vbi_pause_port(port);
  736. ret = saa7164_vbi_acquire_port(port);
  737. ret = saa7164_vbi_stop_port(port);
  738. dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
  739. port->nr);
  740. /* Reset the state of any allocated buffer resources */
  741. mutex_lock(&port->dmaqueue_lock);
  742. /* Reset the hard and soft buffer state */
  743. list_for_each_safe(c, n, &port->dmaqueue.list) {
  744. buf = list_entry(c, struct saa7164_buffer, list);
  745. buf->flags = SAA7164_BUFFER_FREE;
  746. buf->pos = 0;
  747. }
  748. list_for_each_safe(c, n, &port->list_buf_used.list) {
  749. ubuf = list_entry(c, struct saa7164_user_buffer, list);
  750. ubuf->pos = 0;
  751. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  752. }
  753. mutex_unlock(&port->dmaqueue_lock);
  754. /* Free any allocated resources */
  755. saa7164_vbi_buffers_dealloc(port);
  756. dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
  757. return ret;
  758. }
  759. static int saa7164_vbi_start_streaming(struct saa7164_port *port)
  760. {
  761. struct saa7164_dev *dev = port->dev;
  762. int result, ret = 0;
  763. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  764. port->done_first_interrupt = 0;
  765. /* allocate all of the PCIe DMA buffer resources on the fly,
  766. * allowing switching between TS and PS payloads without
  767. * requiring a complete driver reload.
  768. */
  769. saa7164_vbi_buffers_alloc(port);
  770. /* Configure the encoder with any cache values */
  771. #if 0
  772. saa7164_api_set_encoder(port);
  773. saa7164_api_get_encoder(port);
  774. #endif
  775. /* Place the empty buffers on the hardware */
  776. saa7164_buffer_cfg_port(port);
  777. /* Negotiate format */
  778. if (saa7164_api_set_vbi_format(port) != SAA_OK) {
  779. printk(KERN_ERR "%s() No supported VBI format\n", __func__);
  780. ret = -EIO;
  781. goto out;
  782. }
  783. /* Acquire the hardware */
  784. result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  785. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  786. printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
  787. __func__, result);
  788. ret = -EIO;
  789. goto out;
  790. } else
  791. dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
  792. /* Pause the hardware */
  793. result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  794. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  795. printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
  796. __func__, result);
  797. /* Stop the hardware, regardless */
  798. result = saa7164_vbi_stop_port(port);
  799. if (result != SAA_OK) {
  800. printk(KERN_ERR "%s() pause/forced stop transition "
  801. "failed, res = 0x%x\n", __func__, result);
  802. }
  803. ret = -EIO;
  804. goto out;
  805. } else
  806. dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
  807. /* Start the hardware */
  808. result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
  809. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  810. printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
  811. __func__, result);
  812. /* Stop the hardware, regardless */
  813. result = saa7164_vbi_acquire_port(port);
  814. result = saa7164_vbi_stop_port(port);
  815. if (result != SAA_OK) {
  816. printk(KERN_ERR "%s() run/forced stop transition "
  817. "failed, res = 0x%x\n", __func__, result);
  818. }
  819. ret = -EIO;
  820. } else
  821. dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
  822. out:
  823. return ret;
  824. }
  825. static int saa7164_vbi_fmt(struct file *file, void *priv,
  826. struct v4l2_format *f)
  827. {
  828. /* ntsc */
  829. f->fmt.vbi.samples_per_line = 1600;
  830. f->fmt.vbi.samples_per_line = 1440;
  831. f->fmt.vbi.sampling_rate = 27000000;
  832. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  833. f->fmt.vbi.offset = 0;
  834. f->fmt.vbi.flags = 0;
  835. f->fmt.vbi.start[0] = 10;
  836. f->fmt.vbi.count[0] = 18;
  837. f->fmt.vbi.start[1] = 263 + 10 + 1;
  838. f->fmt.vbi.count[1] = 18;
  839. return 0;
  840. }
  841. static int fops_open(struct file *file)
  842. {
  843. struct saa7164_dev *dev;
  844. struct saa7164_port *port;
  845. struct saa7164_vbi_fh *fh;
  846. port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
  847. if (!port)
  848. return -ENODEV;
  849. dev = port->dev;
  850. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  851. /* allocate + initialize per filehandle data */
  852. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  853. if (NULL == fh)
  854. return -ENOMEM;
  855. file->private_data = fh;
  856. fh->port = port;
  857. return 0;
  858. }
  859. static int fops_release(struct file *file)
  860. {
  861. struct saa7164_vbi_fh *fh = file->private_data;
  862. struct saa7164_port *port = fh->port;
  863. struct saa7164_dev *dev = port->dev;
  864. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  865. /* Shut device down on last close */
  866. if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
  867. if (atomic_dec_return(&port->v4l_reader_count) == 0) {
  868. /* stop vbi capture then cancel buffers */
  869. saa7164_vbi_stop_streaming(port);
  870. }
  871. }
  872. file->private_data = NULL;
  873. kfree(fh);
  874. return 0;
  875. }
  876. static struct
  877. saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
  878. {
  879. struct saa7164_user_buffer *ubuf = NULL;
  880. struct saa7164_dev *dev = port->dev;
  881. u32 crc;
  882. mutex_lock(&port->dmaqueue_lock);
  883. if (!list_empty(&port->list_buf_used.list)) {
  884. ubuf = list_first_entry(&port->list_buf_used.list,
  885. struct saa7164_user_buffer, list);
  886. if (crc_checking) {
  887. crc = crc32(0, ubuf->data, ubuf->actual_size);
  888. if (crc != ubuf->crc) {
  889. printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
  890. __func__,
  891. ubuf, ubuf->crc, crc);
  892. }
  893. }
  894. }
  895. mutex_unlock(&port->dmaqueue_lock);
  896. dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
  897. return ubuf;
  898. }
  899. static ssize_t fops_read(struct file *file, char __user *buffer,
  900. size_t count, loff_t *pos)
  901. {
  902. struct saa7164_vbi_fh *fh = file->private_data;
  903. struct saa7164_port *port = fh->port;
  904. struct saa7164_user_buffer *ubuf = NULL;
  905. struct saa7164_dev *dev = port->dev;
  906. int ret = 0;
  907. int rem, cnt;
  908. u8 *p;
  909. port->last_read_msecs_diff = port->last_read_msecs;
  910. port->last_read_msecs = jiffies_to_msecs(jiffies);
  911. port->last_read_msecs_diff = port->last_read_msecs -
  912. port->last_read_msecs_diff;
  913. saa7164_histogram_update(&port->read_interval,
  914. port->last_read_msecs_diff);
  915. if (*pos) {
  916. printk(KERN_ERR "%s() ESPIPE\n", __func__);
  917. return -ESPIPE;
  918. }
  919. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  920. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  921. if (saa7164_vbi_initialize(port) < 0) {
  922. printk(KERN_ERR "%s() EINVAL\n", __func__);
  923. return -EINVAL;
  924. }
  925. saa7164_vbi_start_streaming(port);
  926. msleep(200);
  927. }
  928. }
  929. /* blocking wait for buffer */
  930. if ((file->f_flags & O_NONBLOCK) == 0) {
  931. if (wait_event_interruptible(port->wait_read,
  932. saa7164_vbi_next_buf(port))) {
  933. printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
  934. return -ERESTARTSYS;
  935. }
  936. }
  937. /* Pull the first buffer from the used list */
  938. ubuf = saa7164_vbi_next_buf(port);
  939. while ((count > 0) && ubuf) {
  940. /* set remaining bytes to copy */
  941. rem = ubuf->actual_size - ubuf->pos;
  942. cnt = rem > count ? count : rem;
  943. p = ubuf->data + ubuf->pos;
  944. dprintk(DBGLVL_VBI,
  945. "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
  946. __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
  947. if (copy_to_user(buffer, p, cnt)) {
  948. printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
  949. if (!ret) {
  950. printk(KERN_ERR "%s() EFAULT\n", __func__);
  951. ret = -EFAULT;
  952. }
  953. goto err;
  954. }
  955. ubuf->pos += cnt;
  956. count -= cnt;
  957. buffer += cnt;
  958. ret += cnt;
  959. if (ubuf->pos > ubuf->actual_size)
  960. printk(KERN_ERR "read() pos > actual, huh?\n");
  961. if (ubuf->pos == ubuf->actual_size) {
  962. /* finished with current buffer, take next buffer */
  963. /* Requeue the buffer on the free list */
  964. ubuf->pos = 0;
  965. mutex_lock(&port->dmaqueue_lock);
  966. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  967. mutex_unlock(&port->dmaqueue_lock);
  968. /* Dequeue next */
  969. if ((file->f_flags & O_NONBLOCK) == 0) {
  970. if (wait_event_interruptible(port->wait_read,
  971. saa7164_vbi_next_buf(port))) {
  972. break;
  973. }
  974. }
  975. ubuf = saa7164_vbi_next_buf(port);
  976. }
  977. }
  978. err:
  979. if (!ret && !ubuf) {
  980. printk(KERN_ERR "%s() EAGAIN\n", __func__);
  981. ret = -EAGAIN;
  982. }
  983. return ret;
  984. }
  985. static unsigned int fops_poll(struct file *file, poll_table *wait)
  986. {
  987. struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
  988. struct saa7164_port *port = fh->port;
  989. unsigned int mask = 0;
  990. port->last_poll_msecs_diff = port->last_poll_msecs;
  991. port->last_poll_msecs = jiffies_to_msecs(jiffies);
  992. port->last_poll_msecs_diff = port->last_poll_msecs -
  993. port->last_poll_msecs_diff;
  994. saa7164_histogram_update(&port->poll_interval,
  995. port->last_poll_msecs_diff);
  996. if (!video_is_registered(port->v4l_device))
  997. return -EIO;
  998. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  999. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  1000. if (saa7164_vbi_initialize(port) < 0)
  1001. return -EINVAL;
  1002. saa7164_vbi_start_streaming(port);
  1003. msleep(200);
  1004. }
  1005. }
  1006. /* blocking wait for buffer */
  1007. if ((file->f_flags & O_NONBLOCK) == 0) {
  1008. if (wait_event_interruptible(port->wait_read,
  1009. saa7164_vbi_next_buf(port))) {
  1010. return -ERESTARTSYS;
  1011. }
  1012. }
  1013. /* Pull the first buffer from the used list */
  1014. if (!list_empty(&port->list_buf_used.list))
  1015. mask |= POLLIN | POLLRDNORM;
  1016. return mask;
  1017. }
  1018. static const struct v4l2_file_operations vbi_fops = {
  1019. .owner = THIS_MODULE,
  1020. .open = fops_open,
  1021. .release = fops_release,
  1022. .read = fops_read,
  1023. .poll = fops_poll,
  1024. .unlocked_ioctl = video_ioctl2,
  1025. };
  1026. static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
  1027. .vidioc_s_std = vidioc_s_std,
  1028. .vidioc_g_std = vidioc_g_std,
  1029. .vidioc_enum_input = vidioc_enum_input,
  1030. .vidioc_g_input = vidioc_g_input,
  1031. .vidioc_s_input = vidioc_s_input,
  1032. .vidioc_g_tuner = vidioc_g_tuner,
  1033. .vidioc_s_tuner = vidioc_s_tuner,
  1034. .vidioc_g_frequency = vidioc_g_frequency,
  1035. .vidioc_s_frequency = vidioc_s_frequency,
  1036. .vidioc_s_ctrl = vidioc_s_ctrl,
  1037. .vidioc_g_ctrl = vidioc_g_ctrl,
  1038. .vidioc_querycap = vidioc_querycap,
  1039. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1040. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1041. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1042. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1043. .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
  1044. .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
  1045. .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
  1046. .vidioc_queryctrl = vidioc_queryctrl,
  1047. .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
  1048. .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
  1049. .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
  1050. };
  1051. static struct video_device saa7164_vbi_template = {
  1052. .name = "saa7164",
  1053. .fops = &vbi_fops,
  1054. .ioctl_ops = &vbi_ioctl_ops,
  1055. .minor = -1,
  1056. .tvnorms = SAA7164_NORMS,
  1057. };
  1058. static struct video_device *saa7164_vbi_alloc(
  1059. struct saa7164_port *port,
  1060. struct pci_dev *pci,
  1061. struct video_device *template,
  1062. char *type)
  1063. {
  1064. struct video_device *vfd;
  1065. struct saa7164_dev *dev = port->dev;
  1066. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  1067. vfd = video_device_alloc();
  1068. if (NULL == vfd)
  1069. return NULL;
  1070. *vfd = *template;
  1071. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
  1072. type, saa7164_boards[dev->board].name);
  1073. vfd->v4l2_dev = &dev->v4l2_dev;
  1074. vfd->release = video_device_release;
  1075. return vfd;
  1076. }
  1077. int saa7164_vbi_register(struct saa7164_port *port)
  1078. {
  1079. struct saa7164_dev *dev = port->dev;
  1080. int result = -ENODEV;
  1081. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  1082. if (port->type != SAA7164_MPEG_VBI)
  1083. BUG();
  1084. /* Sanity check that the PCI configuration space is active */
  1085. if (port->hwcfg.BARLocation == 0) {
  1086. printk(KERN_ERR "%s() failed "
  1087. "(errno = %d), NO PCI configuration\n",
  1088. __func__, result);
  1089. result = -ENOMEM;
  1090. goto failed;
  1091. }
  1092. /* Establish VBI defaults here */
  1093. /* Allocate and register the video device node */
  1094. port->v4l_device = saa7164_vbi_alloc(port,
  1095. dev->pci, &saa7164_vbi_template, "vbi");
  1096. if (!port->v4l_device) {
  1097. printk(KERN_INFO "%s: can't allocate vbi device\n",
  1098. dev->name);
  1099. result = -ENOMEM;
  1100. goto failed;
  1101. }
  1102. port->std = V4L2_STD_NTSC_M;
  1103. video_set_drvdata(port->v4l_device, port);
  1104. result = video_register_device(port->v4l_device,
  1105. VFL_TYPE_VBI, -1);
  1106. if (result < 0) {
  1107. printk(KERN_INFO "%s: can't register vbi device\n",
  1108. dev->name);
  1109. /* TODO: We're going to leak here if we don't dealloc
  1110. The buffers above. The unreg function can't deal wit it.
  1111. */
  1112. goto failed;
  1113. }
  1114. printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
  1115. dev->name, port->v4l_device->num);
  1116. /* Configure the hardware defaults */
  1117. result = 0;
  1118. failed:
  1119. return result;
  1120. }
  1121. void saa7164_vbi_unregister(struct saa7164_port *port)
  1122. {
  1123. struct saa7164_dev *dev = port->dev;
  1124. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  1125. if (port->type != SAA7164_MPEG_VBI)
  1126. BUG();
  1127. if (port->v4l_device) {
  1128. if (port->v4l_device->minor != -1)
  1129. video_unregister_device(port->v4l_device);
  1130. else
  1131. video_device_release(port->v4l_device);
  1132. port->v4l_device = NULL;
  1133. }
  1134. }