saa7164-vbi.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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) {
  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) {
  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. static int saa7164_vbi_fmt(struct file *file, void *priv,
  818. struct v4l2_format *f)
  819. {
  820. /* ntsc */
  821. f->fmt.vbi.samples_per_line = 1600;
  822. f->fmt.vbi.samples_per_line = 1440;
  823. f->fmt.vbi.sampling_rate = 27000000;
  824. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  825. f->fmt.vbi.offset = 0;
  826. f->fmt.vbi.flags = 0;
  827. f->fmt.vbi.start[0] = 10;
  828. f->fmt.vbi.count[0] = 18;
  829. f->fmt.vbi.start[1] = 263 + 10 + 1;
  830. f->fmt.vbi.count[1] = 18;
  831. return 0;
  832. }
  833. static int fops_open(struct file *file)
  834. {
  835. struct saa7164_dev *dev;
  836. struct saa7164_port *port;
  837. struct saa7164_vbi_fh *fh;
  838. port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
  839. if (!port)
  840. return -ENODEV;
  841. dev = port->dev;
  842. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  843. /* allocate + initialize per filehandle data */
  844. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  845. if (NULL == fh)
  846. return -ENOMEM;
  847. file->private_data = fh;
  848. fh->port = port;
  849. return 0;
  850. }
  851. static int fops_release(struct file *file)
  852. {
  853. struct saa7164_vbi_fh *fh = file->private_data;
  854. struct saa7164_port *port = fh->port;
  855. struct saa7164_dev *dev = port->dev;
  856. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  857. /* Shut device down on last close */
  858. if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
  859. if (atomic_dec_return(&port->v4l_reader_count) == 0) {
  860. /* stop vbi capture then cancel buffers */
  861. saa7164_vbi_stop_streaming(port);
  862. }
  863. }
  864. file->private_data = NULL;
  865. kfree(fh);
  866. return 0;
  867. }
  868. static struct
  869. saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
  870. {
  871. struct saa7164_user_buffer *ubuf = NULL;
  872. struct saa7164_dev *dev = port->dev;
  873. u32 crc;
  874. mutex_lock(&port->dmaqueue_lock);
  875. if (!list_empty(&port->list_buf_used.list)) {
  876. ubuf = list_first_entry(&port->list_buf_used.list,
  877. struct saa7164_user_buffer, list);
  878. if (crc_checking) {
  879. crc = crc32(0, ubuf->data, ubuf->actual_size);
  880. if (crc != ubuf->crc) {
  881. printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
  882. __func__,
  883. ubuf, ubuf->crc, crc);
  884. }
  885. }
  886. }
  887. mutex_unlock(&port->dmaqueue_lock);
  888. dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
  889. return ubuf;
  890. }
  891. static ssize_t fops_read(struct file *file, char __user *buffer,
  892. size_t count, loff_t *pos)
  893. {
  894. struct saa7164_vbi_fh *fh = file->private_data;
  895. struct saa7164_port *port = fh->port;
  896. struct saa7164_user_buffer *ubuf = NULL;
  897. struct saa7164_dev *dev = port->dev;
  898. int ret = 0;
  899. int rem, cnt;
  900. u8 *p;
  901. port->last_read_msecs_diff = port->last_read_msecs;
  902. port->last_read_msecs = jiffies_to_msecs(jiffies);
  903. port->last_read_msecs_diff = port->last_read_msecs -
  904. port->last_read_msecs_diff;
  905. saa7164_histogram_update(&port->read_interval,
  906. port->last_read_msecs_diff);
  907. if (*pos) {
  908. printk(KERN_ERR "%s() ESPIPE\n", __func__);
  909. return -ESPIPE;
  910. }
  911. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  912. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  913. if (saa7164_vbi_initialize(port) < 0) {
  914. printk(KERN_ERR "%s() EINVAL\n", __func__);
  915. return -EINVAL;
  916. }
  917. saa7164_vbi_start_streaming(port);
  918. msleep(200);
  919. }
  920. }
  921. /* blocking wait for buffer */
  922. if ((file->f_flags & O_NONBLOCK) == 0) {
  923. if (wait_event_interruptible(port->wait_read,
  924. saa7164_vbi_next_buf(port))) {
  925. printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
  926. return -ERESTARTSYS;
  927. }
  928. }
  929. /* Pull the first buffer from the used list */
  930. ubuf = saa7164_vbi_next_buf(port);
  931. while ((count > 0) && ubuf) {
  932. /* set remaining bytes to copy */
  933. rem = ubuf->actual_size - ubuf->pos;
  934. cnt = rem > count ? count : rem;
  935. p = ubuf->data + ubuf->pos;
  936. dprintk(DBGLVL_VBI,
  937. "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
  938. __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
  939. if (copy_to_user(buffer, p, cnt)) {
  940. printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
  941. if (!ret) {
  942. printk(KERN_ERR "%s() EFAULT\n", __func__);
  943. ret = -EFAULT;
  944. }
  945. goto err;
  946. }
  947. ubuf->pos += cnt;
  948. count -= cnt;
  949. buffer += cnt;
  950. ret += cnt;
  951. if (ubuf->pos > ubuf->actual_size)
  952. printk(KERN_ERR "read() pos > actual, huh?\n");
  953. if (ubuf->pos == ubuf->actual_size) {
  954. /* finished with current buffer, take next buffer */
  955. /* Requeue the buffer on the free list */
  956. ubuf->pos = 0;
  957. mutex_lock(&port->dmaqueue_lock);
  958. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  959. mutex_unlock(&port->dmaqueue_lock);
  960. /* Dequeue next */
  961. if ((file->f_flags & O_NONBLOCK) == 0) {
  962. if (wait_event_interruptible(port->wait_read,
  963. saa7164_vbi_next_buf(port))) {
  964. break;
  965. }
  966. }
  967. ubuf = saa7164_vbi_next_buf(port);
  968. }
  969. }
  970. err:
  971. if (!ret && !ubuf) {
  972. printk(KERN_ERR "%s() EAGAIN\n", __func__);
  973. ret = -EAGAIN;
  974. }
  975. return ret;
  976. }
  977. static unsigned int fops_poll(struct file *file, poll_table *wait)
  978. {
  979. struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
  980. struct saa7164_port *port = fh->port;
  981. unsigned int mask = 0;
  982. port->last_poll_msecs_diff = port->last_poll_msecs;
  983. port->last_poll_msecs = jiffies_to_msecs(jiffies);
  984. port->last_poll_msecs_diff = port->last_poll_msecs -
  985. port->last_poll_msecs_diff;
  986. saa7164_histogram_update(&port->poll_interval,
  987. port->last_poll_msecs_diff);
  988. if (!video_is_registered(port->v4l_device))
  989. return -EIO;
  990. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  991. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  992. if (saa7164_vbi_initialize(port) < 0)
  993. return -EINVAL;
  994. saa7164_vbi_start_streaming(port);
  995. msleep(200);
  996. }
  997. }
  998. /* blocking wait for buffer */
  999. if ((file->f_flags & O_NONBLOCK) == 0) {
  1000. if (wait_event_interruptible(port->wait_read,
  1001. saa7164_vbi_next_buf(port))) {
  1002. return -ERESTARTSYS;
  1003. }
  1004. }
  1005. /* Pull the first buffer from the used list */
  1006. if (!list_empty(&port->list_buf_used.list))
  1007. mask |= POLLIN | POLLRDNORM;
  1008. return mask;
  1009. }
  1010. static const struct v4l2_file_operations vbi_fops = {
  1011. .owner = THIS_MODULE,
  1012. .open = fops_open,
  1013. .release = fops_release,
  1014. .read = fops_read,
  1015. .poll = fops_poll,
  1016. .unlocked_ioctl = video_ioctl2,
  1017. };
  1018. static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
  1019. .vidioc_s_std = vidioc_s_std,
  1020. .vidioc_enum_input = vidioc_enum_input,
  1021. .vidioc_g_input = vidioc_g_input,
  1022. .vidioc_s_input = vidioc_s_input,
  1023. .vidioc_g_tuner = vidioc_g_tuner,
  1024. .vidioc_s_tuner = vidioc_s_tuner,
  1025. .vidioc_g_frequency = vidioc_g_frequency,
  1026. .vidioc_s_frequency = vidioc_s_frequency,
  1027. .vidioc_s_ctrl = vidioc_s_ctrl,
  1028. .vidioc_g_ctrl = vidioc_g_ctrl,
  1029. .vidioc_querycap = vidioc_querycap,
  1030. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1031. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1032. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1033. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1034. .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
  1035. .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
  1036. .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
  1037. .vidioc_queryctrl = vidioc_queryctrl,
  1038. #if 0
  1039. .vidioc_g_chip_ident = saa7164_g_chip_ident,
  1040. #endif
  1041. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1042. #if 0
  1043. .vidioc_g_register = saa7164_g_register,
  1044. .vidioc_s_register = saa7164_s_register,
  1045. #endif
  1046. #endif
  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. .current_norm = V4L2_STD_NTSC_M,
  1058. };
  1059. static struct video_device *saa7164_vbi_alloc(
  1060. struct saa7164_port *port,
  1061. struct pci_dev *pci,
  1062. struct video_device *template,
  1063. char *type)
  1064. {
  1065. struct video_device *vfd;
  1066. struct saa7164_dev *dev = port->dev;
  1067. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  1068. vfd = video_device_alloc();
  1069. if (NULL == vfd)
  1070. return NULL;
  1071. *vfd = *template;
  1072. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
  1073. type, saa7164_boards[dev->board].name);
  1074. vfd->parent = &pci->dev;
  1075. vfd->release = video_device_release;
  1076. return vfd;
  1077. }
  1078. int saa7164_vbi_register(struct saa7164_port *port)
  1079. {
  1080. struct saa7164_dev *dev = port->dev;
  1081. int result = -ENODEV;
  1082. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  1083. if (port->type != SAA7164_MPEG_VBI)
  1084. BUG();
  1085. /* Sanity check that the PCI configuration space is active */
  1086. if (port->hwcfg.BARLocation == 0) {
  1087. printk(KERN_ERR "%s() failed "
  1088. "(errno = %d), NO PCI configuration\n",
  1089. __func__, result);
  1090. result = -ENOMEM;
  1091. goto failed;
  1092. }
  1093. /* Establish VBI defaults here */
  1094. /* Allocate and register the video device node */
  1095. port->v4l_device = saa7164_vbi_alloc(port,
  1096. dev->pci, &saa7164_vbi_template, "vbi");
  1097. if (!port->v4l_device) {
  1098. printk(KERN_INFO "%s: can't allocate vbi device\n",
  1099. dev->name);
  1100. result = -ENOMEM;
  1101. goto failed;
  1102. }
  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. }