saa7164-vbi.c 33 KB

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