saa7164-vbi.c 34 KB

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