saa7164-vbi.c 33 KB

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