saa7164-encoder.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. #include "saa7164.h"
  18. #define ENCODER_MAX_BITRATE 6500000
  19. #define ENCODER_MIN_BITRATE 1000000
  20. #define ENCODER_DEF_BITRATE 5000000
  21. /*
  22. * This is a dummy non-zero value for the sizeimage field of v4l2_pix_format.
  23. * It is not actually used for anything since this driver does not support
  24. * stream I/O, only read(), and because this driver produces an MPEG stream
  25. * and not discrete frames. But the V4L2 spec doesn't allow for this value
  26. * to be 0, so set it to 0x10000 instead.
  27. *
  28. * If we ever change this driver to support stream I/O, then this field
  29. * will be the size of the streaming buffers.
  30. */
  31. #define SAA7164_SIZEIMAGE (0x10000)
  32. static struct saa7164_tvnorm saa7164_tvnorms[] = {
  33. {
  34. .name = "NTSC-M",
  35. .id = V4L2_STD_NTSC_M,
  36. }, {
  37. .name = "NTSC-JP",
  38. .id = V4L2_STD_NTSC_M_JP,
  39. }
  40. };
  41. /* Take the encoder configuration form the port struct and
  42. * flush it to the hardware.
  43. */
  44. static void saa7164_encoder_configure(struct saa7164_port *port)
  45. {
  46. struct saa7164_dev *dev = port->dev;
  47. dprintk(DBGLVL_ENC, "%s()\n", __func__);
  48. port->encoder_params.width = port->width;
  49. port->encoder_params.height = port->height;
  50. port->encoder_params.is_50hz =
  51. (port->encodernorm.id & V4L2_STD_625_50) != 0;
  52. /* Set up the DIF (enable it) for analog mode by default */
  53. saa7164_api_initialize_dif(port);
  54. /* Configure the correct video standard */
  55. saa7164_api_configure_dif(port, port->encodernorm.id);
  56. /* Ensure the audio decoder is correct configured */
  57. saa7164_api_set_audio_std(port);
  58. }
  59. static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
  60. {
  61. struct list_head *c, *n, *p, *q, *l, *v;
  62. struct saa7164_dev *dev = port->dev;
  63. struct saa7164_buffer *buf;
  64. struct saa7164_user_buffer *ubuf;
  65. /* Remove any allocated buffers */
  66. mutex_lock(&port->dmaqueue_lock);
  67. dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
  68. list_for_each_safe(c, n, &port->dmaqueue.list) {
  69. buf = list_entry(c, struct saa7164_buffer, list);
  70. list_del(c);
  71. saa7164_buffer_dealloc(buf);
  72. }
  73. dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
  74. list_for_each_safe(p, q, &port->list_buf_used.list) {
  75. ubuf = list_entry(p, struct saa7164_user_buffer, list);
  76. list_del(p);
  77. saa7164_buffer_dealloc_user(ubuf);
  78. }
  79. dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
  80. list_for_each_safe(l, v, &port->list_buf_free.list) {
  81. ubuf = list_entry(l, struct saa7164_user_buffer, list);
  82. list_del(l);
  83. saa7164_buffer_dealloc_user(ubuf);
  84. }
  85. mutex_unlock(&port->dmaqueue_lock);
  86. dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
  87. return 0;
  88. }
  89. /* Dynamic buffer switch at encoder start time */
  90. static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
  91. {
  92. struct saa7164_dev *dev = port->dev;
  93. struct saa7164_buffer *buf;
  94. struct saa7164_user_buffer *ubuf;
  95. struct tmHWStreamParameters *params = &port->hw_streamingparams;
  96. int result = -ENODEV, i;
  97. int len = 0;
  98. dprintk(DBGLVL_ENC, "%s()\n", __func__);
  99. if (port->encoder_params.stream_type ==
  100. V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
  101. dprintk(DBGLVL_ENC,
  102. "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
  103. __func__);
  104. params->samplesperline = 128;
  105. params->numberoflines = 256;
  106. params->pitch = 128;
  107. params->numpagetables = 2 +
  108. ((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
  109. } else
  110. if (port->encoder_params.stream_type ==
  111. V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
  112. dprintk(DBGLVL_ENC,
  113. "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
  114. __func__);
  115. params->samplesperline = 188;
  116. params->numberoflines = 312;
  117. params->pitch = 188;
  118. params->numpagetables = 2 +
  119. ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
  120. } else
  121. BUG();
  122. /* Init and establish defaults */
  123. params->bitspersample = 8;
  124. params->linethreshold = 0;
  125. params->pagetablelistvirt = NULL;
  126. params->pagetablelistphys = NULL;
  127. params->numpagetableentries = port->hwcfg.buffercount;
  128. /* Allocate the PCI resources, buffers (hard) */
  129. for (i = 0; i < port->hwcfg.buffercount; i++) {
  130. buf = saa7164_buffer_alloc(port,
  131. params->numberoflines *
  132. params->pitch);
  133. if (!buf) {
  134. printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
  135. __func__, result);
  136. result = -ENOMEM;
  137. goto failed;
  138. } else {
  139. mutex_lock(&port->dmaqueue_lock);
  140. list_add_tail(&buf->list, &port->dmaqueue.list);
  141. mutex_unlock(&port->dmaqueue_lock);
  142. }
  143. }
  144. /* Allocate some kernel buffers for copying
  145. * to userpsace.
  146. */
  147. len = params->numberoflines * params->pitch;
  148. if (encoder_buffers < 16)
  149. encoder_buffers = 16;
  150. if (encoder_buffers > 512)
  151. encoder_buffers = 512;
  152. for (i = 0; i < encoder_buffers; i++) {
  153. ubuf = saa7164_buffer_alloc_user(dev, len);
  154. if (ubuf) {
  155. mutex_lock(&port->dmaqueue_lock);
  156. list_add_tail(&ubuf->list, &port->list_buf_free.list);
  157. mutex_unlock(&port->dmaqueue_lock);
  158. }
  159. }
  160. result = 0;
  161. failed:
  162. return result;
  163. }
  164. static int saa7164_encoder_initialize(struct saa7164_port *port)
  165. {
  166. saa7164_encoder_configure(port);
  167. return 0;
  168. }
  169. /* -- V4L2 --------------------------------------------------------- */
  170. int saa7164_s_std(struct saa7164_port *port, v4l2_std_id id)
  171. {
  172. struct saa7164_dev *dev = port->dev;
  173. unsigned int i;
  174. dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)id);
  175. for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
  176. if (id & saa7164_tvnorms[i].id)
  177. break;
  178. }
  179. if (i == ARRAY_SIZE(saa7164_tvnorms))
  180. return -EINVAL;
  181. port->encodernorm = saa7164_tvnorms[i];
  182. port->std = id;
  183. /* Update the audio decoder while is not running in
  184. * auto detect mode.
  185. */
  186. saa7164_api_set_audio_std(port);
  187. dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)id);
  188. return 0;
  189. }
  190. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
  191. {
  192. struct saa7164_encoder_fh *fh = file->private_data;
  193. return saa7164_s_std(fh->port, id);
  194. }
  195. int saa7164_g_std(struct saa7164_port *port, v4l2_std_id *id)
  196. {
  197. *id = port->std;
  198. return 0;
  199. }
  200. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  201. {
  202. struct saa7164_encoder_fh *fh = file->private_data;
  203. return saa7164_g_std(fh->port, id);
  204. }
  205. int saa7164_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  206. {
  207. static const char * const inputs[] = {
  208. "tuner", "composite", "svideo", "aux",
  209. "composite 2", "svideo 2", "aux 2"
  210. };
  211. int n;
  212. if (i->index >= 7)
  213. return -EINVAL;
  214. strcpy(i->name, inputs[i->index]);
  215. if (i->index == 0)
  216. i->type = V4L2_INPUT_TYPE_TUNER;
  217. else
  218. i->type = V4L2_INPUT_TYPE_CAMERA;
  219. for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
  220. i->std |= saa7164_tvnorms[n].id;
  221. return 0;
  222. }
  223. int saa7164_g_input(struct saa7164_port *port, unsigned int *i)
  224. {
  225. struct saa7164_dev *dev = port->dev;
  226. if (saa7164_api_get_videomux(port) != SAA_OK)
  227. return -EIO;
  228. *i = (port->mux_input - 1);
  229. dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
  230. return 0;
  231. }
  232. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  233. {
  234. struct saa7164_encoder_fh *fh = file->private_data;
  235. return saa7164_g_input(fh->port, i);
  236. }
  237. int saa7164_s_input(struct saa7164_port *port, unsigned int i)
  238. {
  239. struct saa7164_dev *dev = port->dev;
  240. dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
  241. if (i >= 7)
  242. return -EINVAL;
  243. port->mux_input = i + 1;
  244. if (saa7164_api_set_videomux(port) != SAA_OK)
  245. return -EIO;
  246. return 0;
  247. }
  248. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  249. {
  250. struct saa7164_encoder_fh *fh = file->private_data;
  251. return saa7164_s_input(fh->port, i);
  252. }
  253. int saa7164_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  254. {
  255. struct saa7164_encoder_fh *fh = file->private_data;
  256. struct saa7164_port *port = fh->port;
  257. struct saa7164_dev *dev = port->dev;
  258. if (0 != t->index)
  259. return -EINVAL;
  260. strcpy(t->name, "tuner");
  261. t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
  262. t->rangelow = SAA7164_TV_MIN_FREQ;
  263. t->rangehigh = SAA7164_TV_MAX_FREQ;
  264. dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
  265. return 0;
  266. }
  267. int saa7164_s_tuner(struct file *file, void *priv,
  268. const struct v4l2_tuner *t)
  269. {
  270. if (0 != t->index)
  271. return -EINVAL;
  272. /* Update the A/V core */
  273. return 0;
  274. }
  275. int saa7164_g_frequency(struct saa7164_port *port, struct v4l2_frequency *f)
  276. {
  277. if (f->tuner)
  278. return -EINVAL;
  279. f->frequency = port->freq;
  280. return 0;
  281. }
  282. static int vidioc_g_frequency(struct file *file, void *priv,
  283. struct v4l2_frequency *f)
  284. {
  285. struct saa7164_encoder_fh *fh = file->private_data;
  286. return saa7164_g_frequency(fh->port, f);
  287. }
  288. int saa7164_s_frequency(struct saa7164_port *port,
  289. const struct v4l2_frequency *f)
  290. {
  291. struct saa7164_dev *dev = port->dev;
  292. struct saa7164_port *tsport;
  293. struct dvb_frontend *fe;
  294. /* TODO: Pull this for the std */
  295. struct analog_parameters params = {
  296. .mode = V4L2_TUNER_ANALOG_TV,
  297. .audmode = V4L2_TUNER_MODE_STEREO,
  298. .std = port->encodernorm.id,
  299. .frequency = f->frequency
  300. };
  301. /* Stop the encoder */
  302. dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
  303. f->frequency, f->tuner);
  304. if (f->tuner != 0)
  305. return -EINVAL;
  306. port->freq = clamp(f->frequency,
  307. SAA7164_TV_MIN_FREQ, SAA7164_TV_MAX_FREQ);
  308. /* Update the hardware */
  309. if (port->nr == SAA7164_PORT_ENC1)
  310. tsport = &dev->ports[SAA7164_PORT_TS1];
  311. else if (port->nr == SAA7164_PORT_ENC2)
  312. tsport = &dev->ports[SAA7164_PORT_TS2];
  313. else
  314. BUG();
  315. fe = tsport->dvb.frontend;
  316. if (fe && fe->ops.tuner_ops.set_analog_params)
  317. fe->ops.tuner_ops.set_analog_params(fe, &params);
  318. else
  319. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  320. saa7164_encoder_initialize(port);
  321. return 0;
  322. }
  323. static int vidioc_s_frequency(struct file *file, void *priv,
  324. const struct v4l2_frequency *f)
  325. {
  326. struct saa7164_encoder_fh *fh = file->private_data;
  327. return saa7164_s_frequency(fh->port, f);
  328. }
  329. static int saa7164_s_ctrl(struct v4l2_ctrl *ctrl)
  330. {
  331. struct saa7164_port *port =
  332. container_of(ctrl->handler, struct saa7164_port, ctrl_handler);
  333. struct saa7164_encoder_params *params = &port->encoder_params;
  334. int ret = 0;
  335. switch (ctrl->id) {
  336. case V4L2_CID_BRIGHTNESS:
  337. port->ctl_brightness = ctrl->val;
  338. saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
  339. break;
  340. case V4L2_CID_CONTRAST:
  341. port->ctl_contrast = ctrl->val;
  342. saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
  343. break;
  344. case V4L2_CID_SATURATION:
  345. port->ctl_saturation = ctrl->val;
  346. saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
  347. break;
  348. case V4L2_CID_HUE:
  349. port->ctl_hue = ctrl->val;
  350. saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
  351. break;
  352. case V4L2_CID_SHARPNESS:
  353. port->ctl_sharpness = ctrl->val;
  354. saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
  355. break;
  356. case V4L2_CID_AUDIO_VOLUME:
  357. port->ctl_volume = ctrl->val;
  358. saa7164_api_set_audio_volume(port, port->ctl_volume);
  359. break;
  360. case V4L2_CID_MPEG_VIDEO_BITRATE:
  361. params->bitrate = ctrl->val;
  362. break;
  363. case V4L2_CID_MPEG_STREAM_TYPE:
  364. params->stream_type = ctrl->val;
  365. break;
  366. case V4L2_CID_MPEG_AUDIO_MUTE:
  367. params->ctl_mute = ctrl->val;
  368. ret = saa7164_api_audio_mute(port, params->ctl_mute);
  369. if (ret != SAA_OK) {
  370. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
  371. ret);
  372. ret = -EIO;
  373. }
  374. break;
  375. case V4L2_CID_MPEG_VIDEO_ASPECT:
  376. params->ctl_aspect = ctrl->val;
  377. ret = saa7164_api_set_aspect_ratio(port);
  378. if (ret != SAA_OK) {
  379. printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
  380. ret);
  381. ret = -EIO;
  382. }
  383. break;
  384. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  385. params->bitrate_mode = ctrl->val;
  386. break;
  387. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  388. params->refdist = ctrl->val;
  389. break;
  390. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  391. params->bitrate_peak = ctrl->val;
  392. break;
  393. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  394. params->gop_size = ctrl->val;
  395. break;
  396. default:
  397. ret = -EINVAL;
  398. }
  399. return ret;
  400. }
  401. static int vidioc_querycap(struct file *file, void *priv,
  402. struct v4l2_capability *cap)
  403. {
  404. struct saa7164_encoder_fh *fh = file->private_data;
  405. struct saa7164_port *port = fh->port;
  406. struct saa7164_dev *dev = port->dev;
  407. strcpy(cap->driver, dev->name);
  408. strlcpy(cap->card, saa7164_boards[dev->board].name,
  409. sizeof(cap->card));
  410. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  411. cap->device_caps =
  412. V4L2_CAP_VIDEO_CAPTURE |
  413. V4L2_CAP_READWRITE |
  414. V4L2_CAP_TUNER;
  415. cap->capabilities = cap->device_caps |
  416. V4L2_CAP_VBI_CAPTURE |
  417. V4L2_CAP_DEVICE_CAPS;
  418. return 0;
  419. }
  420. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  421. struct v4l2_fmtdesc *f)
  422. {
  423. if (f->index != 0)
  424. return -EINVAL;
  425. strlcpy(f->description, "MPEG", sizeof(f->description));
  426. f->pixelformat = V4L2_PIX_FMT_MPEG;
  427. return 0;
  428. }
  429. static int vidioc_fmt_vid_cap(struct file *file, void *priv,
  430. struct v4l2_format *f)
  431. {
  432. struct saa7164_encoder_fh *fh = file->private_data;
  433. struct saa7164_port *port = fh->port;
  434. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  435. f->fmt.pix.bytesperline = 0;
  436. f->fmt.pix.sizeimage = SAA7164_SIZEIMAGE;
  437. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  438. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  439. f->fmt.pix.width = port->width;
  440. f->fmt.pix.height = port->height;
  441. return 0;
  442. }
  443. static int saa7164_encoder_stop_port(struct saa7164_port *port)
  444. {
  445. struct saa7164_dev *dev = port->dev;
  446. int ret;
  447. ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
  448. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  449. printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
  450. __func__, ret);
  451. ret = -EIO;
  452. } else {
  453. dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__);
  454. ret = 0;
  455. }
  456. return ret;
  457. }
  458. static int saa7164_encoder_acquire_port(struct saa7164_port *port)
  459. {
  460. struct saa7164_dev *dev = port->dev;
  461. int ret;
  462. ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  463. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  464. printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
  465. __func__, ret);
  466. ret = -EIO;
  467. } else {
  468. dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
  469. ret = 0;
  470. }
  471. return ret;
  472. }
  473. static int saa7164_encoder_pause_port(struct saa7164_port *port)
  474. {
  475. struct saa7164_dev *dev = port->dev;
  476. int ret;
  477. ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  478. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  479. printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
  480. __func__, ret);
  481. ret = -EIO;
  482. } else {
  483. dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
  484. ret = 0;
  485. }
  486. return ret;
  487. }
  488. /* Firmware is very windows centric, meaning you have to transition
  489. * the part through AVStream / KS Windows stages, forwards or backwards.
  490. * States are: stopped, acquired (h/w), paused, started.
  491. * We have to leave here will all of the soft buffers on the free list,
  492. * else the cfg_post() func won't have soft buffers to correctly configure.
  493. */
  494. static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
  495. {
  496. struct saa7164_dev *dev = port->dev;
  497. struct saa7164_buffer *buf;
  498. struct saa7164_user_buffer *ubuf;
  499. struct list_head *c, *n;
  500. int ret;
  501. dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
  502. ret = saa7164_encoder_pause_port(port);
  503. ret = saa7164_encoder_acquire_port(port);
  504. ret = saa7164_encoder_stop_port(port);
  505. dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
  506. port->nr);
  507. /* Reset the state of any allocated buffer resources */
  508. mutex_lock(&port->dmaqueue_lock);
  509. /* Reset the hard and soft buffer state */
  510. list_for_each_safe(c, n, &port->dmaqueue.list) {
  511. buf = list_entry(c, struct saa7164_buffer, list);
  512. buf->flags = SAA7164_BUFFER_FREE;
  513. buf->pos = 0;
  514. }
  515. list_for_each_safe(c, n, &port->list_buf_used.list) {
  516. ubuf = list_entry(c, struct saa7164_user_buffer, list);
  517. ubuf->pos = 0;
  518. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  519. }
  520. mutex_unlock(&port->dmaqueue_lock);
  521. /* Free any allocated resources */
  522. saa7164_encoder_buffers_dealloc(port);
  523. dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
  524. return ret;
  525. }
  526. static int saa7164_encoder_start_streaming(struct saa7164_port *port)
  527. {
  528. struct saa7164_dev *dev = port->dev;
  529. int result, ret = 0;
  530. dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
  531. port->done_first_interrupt = 0;
  532. /* allocate all of the PCIe DMA buffer resources on the fly,
  533. * allowing switching between TS and PS payloads without
  534. * requiring a complete driver reload.
  535. */
  536. saa7164_encoder_buffers_alloc(port);
  537. /* Configure the encoder with any cache values */
  538. saa7164_api_set_encoder(port);
  539. saa7164_api_get_encoder(port);
  540. /* Place the empty buffers on the hardware */
  541. saa7164_buffer_cfg_port(port);
  542. /* Acquire the hardware */
  543. result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  544. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  545. printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
  546. __func__, result);
  547. /* Stop the hardware, regardless */
  548. result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
  549. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  550. printk(KERN_ERR "%s() acquire/forced stop transition failed, res = 0x%x\n",
  551. __func__, result);
  552. }
  553. ret = -EIO;
  554. goto out;
  555. } else
  556. dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
  557. /* Pause the hardware */
  558. result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  559. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  560. printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
  561. __func__, result);
  562. /* Stop the hardware, regardless */
  563. result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
  564. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  565. printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
  566. __func__, result);
  567. }
  568. ret = -EIO;
  569. goto out;
  570. } else
  571. dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
  572. /* Start the hardware */
  573. result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
  574. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  575. printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
  576. __func__, result);
  577. /* Stop the hardware, regardless */
  578. result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
  579. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  580. printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
  581. __func__, result);
  582. }
  583. ret = -EIO;
  584. } else
  585. dprintk(DBGLVL_ENC, "%s() Running\n", __func__);
  586. out:
  587. return ret;
  588. }
  589. static int fops_open(struct file *file)
  590. {
  591. struct saa7164_dev *dev;
  592. struct saa7164_port *port;
  593. struct saa7164_encoder_fh *fh;
  594. port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
  595. if (!port)
  596. return -ENODEV;
  597. dev = port->dev;
  598. dprintk(DBGLVL_ENC, "%s()\n", __func__);
  599. /* allocate + initialize per filehandle data */
  600. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  601. if (NULL == fh)
  602. return -ENOMEM;
  603. fh->port = port;
  604. v4l2_fh_init(&fh->fh, video_devdata(file));
  605. v4l2_fh_add(&fh->fh);
  606. file->private_data = fh;
  607. return 0;
  608. }
  609. static int fops_release(struct file *file)
  610. {
  611. struct saa7164_encoder_fh *fh = file->private_data;
  612. struct saa7164_port *port = fh->port;
  613. struct saa7164_dev *dev = port->dev;
  614. dprintk(DBGLVL_ENC, "%s()\n", __func__);
  615. /* Shut device down on last close */
  616. if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
  617. if (atomic_dec_return(&port->v4l_reader_count) == 0) {
  618. /* stop mpeg capture then cancel buffers */
  619. saa7164_encoder_stop_streaming(port);
  620. }
  621. }
  622. v4l2_fh_del(&fh->fh);
  623. v4l2_fh_exit(&fh->fh);
  624. kfree(fh);
  625. return 0;
  626. }
  627. static struct
  628. saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
  629. {
  630. struct saa7164_user_buffer *ubuf = NULL;
  631. struct saa7164_dev *dev = port->dev;
  632. u32 crc;
  633. mutex_lock(&port->dmaqueue_lock);
  634. if (!list_empty(&port->list_buf_used.list)) {
  635. ubuf = list_first_entry(&port->list_buf_used.list,
  636. struct saa7164_user_buffer, list);
  637. if (crc_checking) {
  638. crc = crc32(0, ubuf->data, ubuf->actual_size);
  639. if (crc != ubuf->crc) {
  640. printk(KERN_ERR
  641. "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
  642. __func__,
  643. ubuf, ubuf->crc, crc);
  644. }
  645. }
  646. }
  647. mutex_unlock(&port->dmaqueue_lock);
  648. dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
  649. return ubuf;
  650. }
  651. static ssize_t fops_read(struct file *file, char __user *buffer,
  652. size_t count, loff_t *pos)
  653. {
  654. struct saa7164_encoder_fh *fh = file->private_data;
  655. struct saa7164_port *port = fh->port;
  656. struct saa7164_user_buffer *ubuf = NULL;
  657. struct saa7164_dev *dev = port->dev;
  658. int ret = 0;
  659. int rem, cnt;
  660. u8 *p;
  661. port->last_read_msecs_diff = port->last_read_msecs;
  662. port->last_read_msecs = jiffies_to_msecs(jiffies);
  663. port->last_read_msecs_diff = port->last_read_msecs -
  664. port->last_read_msecs_diff;
  665. saa7164_histogram_update(&port->read_interval,
  666. port->last_read_msecs_diff);
  667. if (*pos) {
  668. printk(KERN_ERR "%s() ESPIPE\n", __func__);
  669. return -ESPIPE;
  670. }
  671. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  672. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  673. if (saa7164_encoder_initialize(port) < 0) {
  674. printk(KERN_ERR "%s() EINVAL\n", __func__);
  675. return -EINVAL;
  676. }
  677. saa7164_encoder_start_streaming(port);
  678. msleep(200);
  679. }
  680. }
  681. /* blocking wait for buffer */
  682. if ((file->f_flags & O_NONBLOCK) == 0) {
  683. if (wait_event_interruptible(port->wait_read,
  684. saa7164_enc_next_buf(port))) {
  685. printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
  686. return -ERESTARTSYS;
  687. }
  688. }
  689. /* Pull the first buffer from the used list */
  690. ubuf = saa7164_enc_next_buf(port);
  691. while ((count > 0) && ubuf) {
  692. /* set remaining bytes to copy */
  693. rem = ubuf->actual_size - ubuf->pos;
  694. cnt = rem > count ? count : rem;
  695. p = ubuf->data + ubuf->pos;
  696. dprintk(DBGLVL_ENC,
  697. "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
  698. __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
  699. if (copy_to_user(buffer, p, cnt)) {
  700. printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
  701. if (!ret) {
  702. printk(KERN_ERR "%s() EFAULT\n", __func__);
  703. ret = -EFAULT;
  704. }
  705. goto err;
  706. }
  707. ubuf->pos += cnt;
  708. count -= cnt;
  709. buffer += cnt;
  710. ret += cnt;
  711. if (ubuf->pos > ubuf->actual_size)
  712. printk(KERN_ERR "read() pos > actual, huh?\n");
  713. if (ubuf->pos == ubuf->actual_size) {
  714. /* finished with current buffer, take next buffer */
  715. /* Requeue the buffer on the free list */
  716. ubuf->pos = 0;
  717. mutex_lock(&port->dmaqueue_lock);
  718. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  719. mutex_unlock(&port->dmaqueue_lock);
  720. /* Dequeue next */
  721. if ((file->f_flags & O_NONBLOCK) == 0) {
  722. if (wait_event_interruptible(port->wait_read,
  723. saa7164_enc_next_buf(port))) {
  724. break;
  725. }
  726. }
  727. ubuf = saa7164_enc_next_buf(port);
  728. }
  729. }
  730. err:
  731. if (!ret && !ubuf)
  732. ret = -EAGAIN;
  733. return ret;
  734. }
  735. static __poll_t fops_poll(struct file *file, poll_table *wait)
  736. {
  737. __poll_t req_events = poll_requested_events(wait);
  738. struct saa7164_encoder_fh *fh =
  739. (struct saa7164_encoder_fh *)file->private_data;
  740. struct saa7164_port *port = fh->port;
  741. __poll_t mask = v4l2_ctrl_poll(file, wait);
  742. port->last_poll_msecs_diff = port->last_poll_msecs;
  743. port->last_poll_msecs = jiffies_to_msecs(jiffies);
  744. port->last_poll_msecs_diff = port->last_poll_msecs -
  745. port->last_poll_msecs_diff;
  746. saa7164_histogram_update(&port->poll_interval,
  747. port->last_poll_msecs_diff);
  748. if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
  749. return mask;
  750. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  751. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  752. if (saa7164_encoder_initialize(port) < 0)
  753. return mask | EPOLLERR;
  754. saa7164_encoder_start_streaming(port);
  755. msleep(200);
  756. }
  757. }
  758. /* Pull the first buffer from the used list */
  759. if (!list_empty(&port->list_buf_used.list))
  760. mask |= EPOLLIN | EPOLLRDNORM;
  761. return mask;
  762. }
  763. static const struct v4l2_ctrl_ops saa7164_ctrl_ops = {
  764. .s_ctrl = saa7164_s_ctrl,
  765. };
  766. static const struct v4l2_file_operations mpeg_fops = {
  767. .owner = THIS_MODULE,
  768. .open = fops_open,
  769. .release = fops_release,
  770. .read = fops_read,
  771. .poll = fops_poll,
  772. .unlocked_ioctl = video_ioctl2,
  773. };
  774. static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
  775. .vidioc_s_std = vidioc_s_std,
  776. .vidioc_g_std = vidioc_g_std,
  777. .vidioc_enum_input = saa7164_enum_input,
  778. .vidioc_g_input = vidioc_g_input,
  779. .vidioc_s_input = vidioc_s_input,
  780. .vidioc_g_tuner = saa7164_g_tuner,
  781. .vidioc_s_tuner = saa7164_s_tuner,
  782. .vidioc_g_frequency = vidioc_g_frequency,
  783. .vidioc_s_frequency = vidioc_s_frequency,
  784. .vidioc_querycap = vidioc_querycap,
  785. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  786. .vidioc_g_fmt_vid_cap = vidioc_fmt_vid_cap,
  787. .vidioc_try_fmt_vid_cap = vidioc_fmt_vid_cap,
  788. .vidioc_s_fmt_vid_cap = vidioc_fmt_vid_cap,
  789. .vidioc_log_status = v4l2_ctrl_log_status,
  790. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  791. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  792. };
  793. static struct video_device saa7164_mpeg_template = {
  794. .name = "saa7164",
  795. .fops = &mpeg_fops,
  796. .ioctl_ops = &mpeg_ioctl_ops,
  797. .minor = -1,
  798. .tvnorms = SAA7164_NORMS,
  799. };
  800. static struct video_device *saa7164_encoder_alloc(
  801. struct saa7164_port *port,
  802. struct pci_dev *pci,
  803. struct video_device *template,
  804. char *type)
  805. {
  806. struct video_device *vfd;
  807. struct saa7164_dev *dev = port->dev;
  808. dprintk(DBGLVL_ENC, "%s()\n", __func__);
  809. vfd = video_device_alloc();
  810. if (NULL == vfd)
  811. return NULL;
  812. *vfd = *template;
  813. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
  814. type, saa7164_boards[dev->board].name);
  815. vfd->v4l2_dev = &dev->v4l2_dev;
  816. vfd->release = video_device_release;
  817. return vfd;
  818. }
  819. int saa7164_encoder_register(struct saa7164_port *port)
  820. {
  821. struct saa7164_dev *dev = port->dev;
  822. struct v4l2_ctrl_handler *hdl = &port->ctrl_handler;
  823. int result = -ENODEV;
  824. dprintk(DBGLVL_ENC, "%s()\n", __func__);
  825. BUG_ON(port->type != SAA7164_MPEG_ENCODER);
  826. /* Sanity check that the PCI configuration space is active */
  827. if (port->hwcfg.BARLocation == 0) {
  828. printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
  829. __func__, result);
  830. result = -ENOMEM;
  831. goto failed;
  832. }
  833. /* Establish encoder defaults here */
  834. /* Set default TV standard */
  835. port->encodernorm = saa7164_tvnorms[0];
  836. port->width = 720;
  837. port->mux_input = 1; /* Composite */
  838. port->video_format = EU_VIDEO_FORMAT_MPEG_2;
  839. port->audio_format = 0;
  840. port->video_resolution = 0;
  841. port->freq = SAA7164_TV_MIN_FREQ;
  842. v4l2_ctrl_handler_init(hdl, 14);
  843. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  844. V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
  845. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  846. V4L2_CID_CONTRAST, 0, 255, 1, 66);
  847. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  848. V4L2_CID_SATURATION, 0, 255, 1, 62);
  849. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  850. V4L2_CID_HUE, 0, 255, 1, 128);
  851. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  852. V4L2_CID_SHARPNESS, 0x0, 0x0f, 1, 8);
  853. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  854. V4L2_CID_MPEG_AUDIO_MUTE, 0x0, 0x01, 1, 0);
  855. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  856. V4L2_CID_AUDIO_VOLUME, -83, 24, 1, 20);
  857. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  858. V4L2_CID_MPEG_VIDEO_BITRATE,
  859. ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
  860. 100000, ENCODER_DEF_BITRATE);
  861. v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
  862. V4L2_CID_MPEG_STREAM_TYPE,
  863. V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 0,
  864. V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
  865. v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
  866. V4L2_CID_MPEG_VIDEO_ASPECT,
  867. V4L2_MPEG_VIDEO_ASPECT_221x100, 0,
  868. V4L2_MPEG_VIDEO_ASPECT_4x3);
  869. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  870. V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, 15);
  871. v4l2_ctrl_new_std_menu(hdl, &saa7164_ctrl_ops,
  872. V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
  873. V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
  874. V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
  875. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  876. V4L2_CID_MPEG_VIDEO_B_FRAMES, 1, 3, 1, 1);
  877. v4l2_ctrl_new_std(hdl, &saa7164_ctrl_ops,
  878. V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
  879. ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
  880. 100000, ENCODER_DEF_BITRATE);
  881. if (hdl->error) {
  882. result = hdl->error;
  883. goto failed;
  884. }
  885. port->std = V4L2_STD_NTSC_M;
  886. if (port->encodernorm.id & V4L2_STD_525_60)
  887. port->height = 480;
  888. else
  889. port->height = 576;
  890. /* Allocate and register the video device node */
  891. port->v4l_device = saa7164_encoder_alloc(port,
  892. dev->pci, &saa7164_mpeg_template, "mpeg");
  893. if (!port->v4l_device) {
  894. printk(KERN_INFO "%s: can't allocate mpeg device\n",
  895. dev->name);
  896. result = -ENOMEM;
  897. goto failed;
  898. }
  899. port->v4l_device->ctrl_handler = hdl;
  900. v4l2_ctrl_handler_setup(hdl);
  901. video_set_drvdata(port->v4l_device, port);
  902. result = video_register_device(port->v4l_device,
  903. VFL_TYPE_GRABBER, -1);
  904. if (result < 0) {
  905. printk(KERN_INFO "%s: can't register mpeg device\n",
  906. dev->name);
  907. /* TODO: We're going to leak here if we don't dealloc
  908. The buffers above. The unreg function can't deal wit it.
  909. */
  910. goto failed;
  911. }
  912. printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
  913. dev->name, port->v4l_device->num);
  914. /* Configure the hardware defaults */
  915. saa7164_api_set_videomux(port);
  916. saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
  917. saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
  918. saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
  919. saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
  920. saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
  921. saa7164_api_audio_mute(port, 0);
  922. saa7164_api_set_audio_volume(port, 20);
  923. saa7164_api_set_aspect_ratio(port);
  924. /* Disable audio standard detection, it's buggy */
  925. saa7164_api_set_audio_detection(port, 0);
  926. saa7164_api_set_encoder(port);
  927. saa7164_api_get_encoder(port);
  928. result = 0;
  929. failed:
  930. return result;
  931. }
  932. void saa7164_encoder_unregister(struct saa7164_port *port)
  933. {
  934. struct saa7164_dev *dev = port->dev;
  935. dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
  936. BUG_ON(port->type != SAA7164_MPEG_ENCODER);
  937. if (port->v4l_device) {
  938. if (port->v4l_device->minor != -1)
  939. video_unregister_device(port->v4l_device);
  940. else
  941. video_device_release(port->v4l_device);
  942. port->v4l_device = NULL;
  943. }
  944. v4l2_ctrl_handler_free(&port->ctrl_handler);
  945. dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
  946. }