saa7164-vbi.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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 vidioc_log_status(struct file *file, void *priv)
  608. {
  609. return 0;
  610. }
  611. static int fill_queryctrl(struct saa7164_vbi_params *params,
  612. struct v4l2_queryctrl *c)
  613. {
  614. switch (c->id) {
  615. case V4L2_CID_BRIGHTNESS:
  616. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
  617. case V4L2_CID_CONTRAST:
  618. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
  619. case V4L2_CID_SATURATION:
  620. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
  621. case V4L2_CID_HUE:
  622. return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
  623. case V4L2_CID_SHARPNESS:
  624. return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
  625. case V4L2_CID_MPEG_AUDIO_MUTE:
  626. return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
  627. case V4L2_CID_AUDIO_VOLUME:
  628. return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
  629. case V4L2_CID_MPEG_STREAM_TYPE:
  630. return v4l2_ctrl_query_fill(c,
  631. V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
  632. V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
  633. 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
  634. case V4L2_CID_MPEG_VIDEO_ASPECT:
  635. return v4l2_ctrl_query_fill(c,
  636. V4L2_MPEG_VIDEO_ASPECT_1x1,
  637. V4L2_MPEG_VIDEO_ASPECT_221x100,
  638. 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
  639. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  640. return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
  641. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  642. return v4l2_ctrl_query_fill(c,
  643. 1, 3, 1, 1);
  644. default:
  645. return -EINVAL;
  646. }
  647. }
  648. static int vidioc_queryctrl(struct file *file, void *priv,
  649. struct v4l2_queryctrl *c)
  650. {
  651. struct saa7164_vbi_fh *fh = priv;
  652. struct saa7164_port *port = fh->port;
  653. int i, next;
  654. u32 id = c->id;
  655. memset(c, 0, sizeof(*c));
  656. next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
  657. c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
  658. for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
  659. if (next) {
  660. if (c->id < saa7164_v4l2_ctrls[i])
  661. c->id = saa7164_v4l2_ctrls[i];
  662. else
  663. continue;
  664. }
  665. if (c->id == saa7164_v4l2_ctrls[i])
  666. return fill_queryctrl(&port->vbi_params, c);
  667. if (c->id < saa7164_v4l2_ctrls[i])
  668. break;
  669. }
  670. return -EINVAL;
  671. }
  672. static int saa7164_vbi_stop_port(struct saa7164_port *port)
  673. {
  674. struct saa7164_dev *dev = port->dev;
  675. int ret;
  676. ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
  677. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  678. printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
  679. __func__, ret);
  680. ret = -EIO;
  681. } else {
  682. dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
  683. ret = 0;
  684. }
  685. return ret;
  686. }
  687. static int saa7164_vbi_acquire_port(struct saa7164_port *port)
  688. {
  689. struct saa7164_dev *dev = port->dev;
  690. int ret;
  691. ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  692. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  693. printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
  694. __func__, ret);
  695. ret = -EIO;
  696. } else {
  697. dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
  698. ret = 0;
  699. }
  700. return ret;
  701. }
  702. static int saa7164_vbi_pause_port(struct saa7164_port *port)
  703. {
  704. struct saa7164_dev *dev = port->dev;
  705. int ret;
  706. ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  707. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  708. printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
  709. __func__, ret);
  710. ret = -EIO;
  711. } else {
  712. dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
  713. ret = 0;
  714. }
  715. return ret;
  716. }
  717. /* Firmware is very windows centric, meaning you have to transition
  718. * the part through AVStream / KS Windows stages, forwards or backwards.
  719. * States are: stopped, acquired (h/w), paused, started.
  720. * We have to leave here will all of the soft buffers on the free list,
  721. * else the cfg_post() func won't have soft buffers to correctly configure.
  722. */
  723. static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
  724. {
  725. struct saa7164_dev *dev = port->dev;
  726. struct saa7164_buffer *buf;
  727. struct saa7164_user_buffer *ubuf;
  728. struct list_head *c, *n;
  729. int ret;
  730. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  731. ret = saa7164_vbi_pause_port(port);
  732. ret = saa7164_vbi_acquire_port(port);
  733. ret = saa7164_vbi_stop_port(port);
  734. dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
  735. port->nr);
  736. /* Reset the state of any allocated buffer resources */
  737. mutex_lock(&port->dmaqueue_lock);
  738. /* Reset the hard and soft buffer state */
  739. list_for_each_safe(c, n, &port->dmaqueue.list) {
  740. buf = list_entry(c, struct saa7164_buffer, list);
  741. buf->flags = SAA7164_BUFFER_FREE;
  742. buf->pos = 0;
  743. }
  744. list_for_each_safe(c, n, &port->list_buf_used.list) {
  745. ubuf = list_entry(c, struct saa7164_user_buffer, list);
  746. ubuf->pos = 0;
  747. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  748. }
  749. mutex_unlock(&port->dmaqueue_lock);
  750. /* Free any allocated resources */
  751. saa7164_vbi_buffers_dealloc(port);
  752. dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
  753. return ret;
  754. }
  755. static int saa7164_vbi_start_streaming(struct saa7164_port *port)
  756. {
  757. struct saa7164_dev *dev = port->dev;
  758. int result, ret = 0;
  759. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  760. port->done_first_interrupt = 0;
  761. /* allocate all of the PCIe DMA buffer resources on the fly,
  762. * allowing switching between TS and PS payloads without
  763. * requiring a complete driver reload.
  764. */
  765. saa7164_vbi_buffers_alloc(port);
  766. /* Configure the encoder with any cache values */
  767. #if 0
  768. saa7164_api_set_encoder(port);
  769. saa7164_api_get_encoder(port);
  770. #endif
  771. /* Place the empty buffers on the hardware */
  772. saa7164_buffer_cfg_port(port);
  773. /* Negotiate format */
  774. if (saa7164_api_set_vbi_format(port) != SAA_OK) {
  775. printk(KERN_ERR "%s() No supported VBI format\n", __func__);
  776. ret = -EIO;
  777. goto out;
  778. }
  779. /* Acquire the hardware */
  780. result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  781. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  782. printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
  783. __func__, result);
  784. ret = -EIO;
  785. goto out;
  786. } else
  787. dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
  788. /* Pause the hardware */
  789. result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  790. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  791. printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
  792. __func__, result);
  793. /* Stop the hardware, regardless */
  794. result = saa7164_vbi_stop_port(port);
  795. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  796. printk(KERN_ERR "%s() pause/forced stop transition "
  797. "failed, res = 0x%x\n", __func__, result);
  798. }
  799. ret = -EIO;
  800. goto out;
  801. } else
  802. dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
  803. /* Start the hardware */
  804. result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
  805. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  806. printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
  807. __func__, result);
  808. /* Stop the hardware, regardless */
  809. result = saa7164_vbi_acquire_port(port);
  810. result = saa7164_vbi_stop_port(port);
  811. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  812. printk(KERN_ERR "%s() run/forced stop transition "
  813. "failed, res = 0x%x\n", __func__, result);
  814. }
  815. ret = -EIO;
  816. } else
  817. dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
  818. out:
  819. return ret;
  820. }
  821. int saa7164_vbi_fmt(struct file *file, void *priv, struct v4l2_format *f)
  822. {
  823. /* ntsc */
  824. f->fmt.vbi.samples_per_line = 1600;
  825. f->fmt.vbi.samples_per_line = 1440;
  826. f->fmt.vbi.sampling_rate = 27000000;
  827. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  828. f->fmt.vbi.offset = 0;
  829. f->fmt.vbi.flags = 0;
  830. f->fmt.vbi.start[0] = 10;
  831. f->fmt.vbi.count[0] = 18;
  832. f->fmt.vbi.start[1] = 263 + 10 + 1;
  833. f->fmt.vbi.count[1] = 18;
  834. return 0;
  835. }
  836. static int fops_open(struct file *file)
  837. {
  838. struct saa7164_dev *dev;
  839. struct saa7164_port *port;
  840. struct saa7164_vbi_fh *fh;
  841. port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
  842. if (!port)
  843. return -ENODEV;
  844. dev = port->dev;
  845. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  846. /* allocate + initialize per filehandle data */
  847. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  848. if (NULL == fh)
  849. return -ENOMEM;
  850. file->private_data = fh;
  851. fh->port = port;
  852. return 0;
  853. }
  854. static int fops_release(struct file *file)
  855. {
  856. struct saa7164_vbi_fh *fh = file->private_data;
  857. struct saa7164_port *port = fh->port;
  858. struct saa7164_dev *dev = port->dev;
  859. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  860. /* Shut device down on last close */
  861. if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
  862. if (atomic_dec_return(&port->v4l_reader_count) == 0) {
  863. /* stop vbi capture then cancel buffers */
  864. saa7164_vbi_stop_streaming(port);
  865. }
  866. }
  867. file->private_data = NULL;
  868. kfree(fh);
  869. return 0;
  870. }
  871. struct saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
  872. {
  873. struct saa7164_user_buffer *ubuf = NULL;
  874. struct saa7164_dev *dev = port->dev;
  875. u32 crc;
  876. mutex_lock(&port->dmaqueue_lock);
  877. if (!list_empty(&port->list_buf_used.list)) {
  878. ubuf = list_first_entry(&port->list_buf_used.list,
  879. struct saa7164_user_buffer, list);
  880. if (crc_checking) {
  881. crc = crc32(0, ubuf->data, ubuf->actual_size);
  882. if (crc != ubuf->crc) {
  883. printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
  884. __func__,
  885. ubuf, ubuf->crc, crc);
  886. }
  887. }
  888. }
  889. mutex_unlock(&port->dmaqueue_lock);
  890. dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
  891. return ubuf;
  892. }
  893. static ssize_t fops_read(struct file *file, char __user *buffer,
  894. size_t count, loff_t *pos)
  895. {
  896. struct saa7164_vbi_fh *fh = file->private_data;
  897. struct saa7164_port *port = fh->port;
  898. struct saa7164_user_buffer *ubuf = NULL;
  899. struct saa7164_dev *dev = port->dev;
  900. int ret = 0;
  901. int rem, cnt;
  902. u8 *p;
  903. port->last_read_msecs_diff = port->last_read_msecs;
  904. port->last_read_msecs = jiffies_to_msecs(jiffies);
  905. port->last_read_msecs_diff = port->last_read_msecs -
  906. port->last_read_msecs_diff;
  907. saa7164_histogram_update(&port->read_interval,
  908. port->last_read_msecs_diff);
  909. if (*pos) {
  910. printk(KERN_ERR "%s() ESPIPE\n", __func__);
  911. return -ESPIPE;
  912. }
  913. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  914. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  915. if (saa7164_vbi_initialize(port) < 0) {
  916. printk(KERN_ERR "%s() EINVAL\n", __func__);
  917. return -EINVAL;
  918. }
  919. saa7164_vbi_start_streaming(port);
  920. msleep(200);
  921. }
  922. }
  923. /* blocking wait for buffer */
  924. if ((file->f_flags & O_NONBLOCK) == 0) {
  925. if (wait_event_interruptible(port->wait_read,
  926. saa7164_vbi_next_buf(port))) {
  927. printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
  928. return -ERESTARTSYS;
  929. }
  930. }
  931. /* Pull the first buffer from the used list */
  932. ubuf = saa7164_vbi_next_buf(port);
  933. while ((count > 0) && ubuf) {
  934. /* set remaining bytes to copy */
  935. rem = ubuf->actual_size - ubuf->pos;
  936. cnt = rem > count ? count : rem;
  937. p = ubuf->data + ubuf->pos;
  938. dprintk(DBGLVL_VBI,
  939. "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
  940. __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
  941. if (copy_to_user(buffer, p, cnt)) {
  942. printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
  943. if (!ret) {
  944. printk(KERN_ERR "%s() EFAULT\n", __func__);
  945. ret = -EFAULT;
  946. }
  947. goto err;
  948. }
  949. ubuf->pos += cnt;
  950. count -= cnt;
  951. buffer += cnt;
  952. ret += cnt;
  953. if (ubuf->pos > ubuf->actual_size)
  954. printk(KERN_ERR "read() pos > actual, huh?\n");
  955. if (ubuf->pos == ubuf->actual_size) {
  956. /* finished with current buffer, take next buffer */
  957. /* Requeue the buffer on the free list */
  958. ubuf->pos = 0;
  959. mutex_lock(&port->dmaqueue_lock);
  960. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  961. mutex_unlock(&port->dmaqueue_lock);
  962. /* Dequeue next */
  963. if ((file->f_flags & O_NONBLOCK) == 0) {
  964. if (wait_event_interruptible(port->wait_read,
  965. saa7164_vbi_next_buf(port))) {
  966. break;
  967. }
  968. }
  969. ubuf = saa7164_vbi_next_buf(port);
  970. }
  971. }
  972. err:
  973. if (!ret && !ubuf) {
  974. printk(KERN_ERR "%s() EAGAIN\n", __func__);
  975. ret = -EAGAIN;
  976. }
  977. return ret;
  978. }
  979. static unsigned int fops_poll(struct file *file, poll_table *wait)
  980. {
  981. struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
  982. struct saa7164_port *port = fh->port;
  983. unsigned int mask = 0;
  984. port->last_poll_msecs_diff = port->last_poll_msecs;
  985. port->last_poll_msecs = jiffies_to_msecs(jiffies);
  986. port->last_poll_msecs_diff = port->last_poll_msecs -
  987. port->last_poll_msecs_diff;
  988. saa7164_histogram_update(&port->poll_interval,
  989. port->last_poll_msecs_diff);
  990. if (!video_is_registered(port->v4l_device))
  991. return -EIO;
  992. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  993. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  994. if (saa7164_vbi_initialize(port) < 0)
  995. return -EINVAL;
  996. saa7164_vbi_start_streaming(port);
  997. msleep(200);
  998. }
  999. }
  1000. /* blocking wait for buffer */
  1001. if ((file->f_flags & O_NONBLOCK) == 0) {
  1002. if (wait_event_interruptible(port->wait_read,
  1003. saa7164_vbi_next_buf(port))) {
  1004. return -ERESTARTSYS;
  1005. }
  1006. }
  1007. /* Pull the first buffer from the used list */
  1008. if (!list_empty(&port->list_buf_used.list))
  1009. mask |= POLLIN | POLLRDNORM;
  1010. return mask;
  1011. }
  1012. static const struct v4l2_file_operations vbi_fops = {
  1013. .owner = THIS_MODULE,
  1014. .open = fops_open,
  1015. .release = fops_release,
  1016. .read = fops_read,
  1017. .poll = fops_poll,
  1018. .unlocked_ioctl = video_ioctl2,
  1019. };
  1020. static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
  1021. .vidioc_s_std = vidioc_s_std,
  1022. .vidioc_enum_input = vidioc_enum_input,
  1023. .vidioc_g_input = vidioc_g_input,
  1024. .vidioc_s_input = vidioc_s_input,
  1025. .vidioc_g_tuner = vidioc_g_tuner,
  1026. .vidioc_s_tuner = vidioc_s_tuner,
  1027. .vidioc_g_frequency = vidioc_g_frequency,
  1028. .vidioc_s_frequency = vidioc_s_frequency,
  1029. .vidioc_s_ctrl = vidioc_s_ctrl,
  1030. .vidioc_g_ctrl = vidioc_g_ctrl,
  1031. .vidioc_querycap = vidioc_querycap,
  1032. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1033. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1034. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1035. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1036. .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
  1037. .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
  1038. .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
  1039. .vidioc_log_status = vidioc_log_status,
  1040. .vidioc_queryctrl = vidioc_queryctrl,
  1041. #if 0
  1042. .vidioc_g_chip_ident = saa7164_g_chip_ident,
  1043. #endif
  1044. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1045. #if 0
  1046. .vidioc_g_register = saa7164_g_register,
  1047. .vidioc_s_register = saa7164_s_register,
  1048. #endif
  1049. #endif
  1050. .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
  1051. .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
  1052. .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
  1053. };
  1054. static struct video_device saa7164_vbi_template = {
  1055. .name = "saa7164",
  1056. .fops = &vbi_fops,
  1057. .ioctl_ops = &vbi_ioctl_ops,
  1058. .minor = -1,
  1059. .tvnorms = SAA7164_NORMS,
  1060. .current_norm = V4L2_STD_NTSC_M,
  1061. };
  1062. static struct video_device *saa7164_vbi_alloc(
  1063. struct saa7164_port *port,
  1064. struct pci_dev *pci,
  1065. struct video_device *template,
  1066. char *type)
  1067. {
  1068. struct video_device *vfd;
  1069. struct saa7164_dev *dev = port->dev;
  1070. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  1071. vfd = video_device_alloc();
  1072. if (NULL == vfd)
  1073. return NULL;
  1074. *vfd = *template;
  1075. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
  1076. type, saa7164_boards[dev->board].name);
  1077. vfd->parent = &pci->dev;
  1078. vfd->release = video_device_release;
  1079. return vfd;
  1080. }
  1081. int saa7164_vbi_register(struct saa7164_port *port)
  1082. {
  1083. struct saa7164_dev *dev = port->dev;
  1084. int result = -ENODEV;
  1085. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  1086. if (port->type != SAA7164_MPEG_VBI)
  1087. BUG();
  1088. /* Sanity check that the PCI configuration space is active */
  1089. if (port->hwcfg.BARLocation == 0) {
  1090. printk(KERN_ERR "%s() failed "
  1091. "(errno = %d), NO PCI configuration\n",
  1092. __func__, result);
  1093. result = -ENOMEM;
  1094. goto failed;
  1095. }
  1096. /* Establish VBI defaults here */
  1097. /* Allocate and register the video device node */
  1098. port->v4l_device = saa7164_vbi_alloc(port,
  1099. dev->pci, &saa7164_vbi_template, "vbi");
  1100. if (!port->v4l_device) {
  1101. printk(KERN_INFO "%s: can't allocate vbi device\n",
  1102. dev->name);
  1103. result = -ENOMEM;
  1104. goto failed;
  1105. }
  1106. video_set_drvdata(port->v4l_device, port);
  1107. result = video_register_device(port->v4l_device,
  1108. VFL_TYPE_VBI, -1);
  1109. if (result < 0) {
  1110. printk(KERN_INFO "%s: can't register vbi device\n",
  1111. dev->name);
  1112. /* TODO: We're going to leak here if we don't dealloc
  1113. The buffers above. The unreg function can't deal wit it.
  1114. */
  1115. goto failed;
  1116. }
  1117. printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
  1118. dev->name, port->v4l_device->num);
  1119. /* Configure the hardware defaults */
  1120. result = 0;
  1121. failed:
  1122. return result;
  1123. }
  1124. void saa7164_vbi_unregister(struct saa7164_port *port)
  1125. {
  1126. struct saa7164_dev *dev = port->dev;
  1127. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  1128. if (port->type != SAA7164_MPEG_VBI)
  1129. BUG();
  1130. if (port->v4l_device) {
  1131. if (port->v4l_device->minor != -1)
  1132. video_unregister_device(port->v4l_device);
  1133. else
  1134. video_device_release(port->v4l_device);
  1135. port->v4l_device = NULL;
  1136. }
  1137. }