saa7164-encoder.c 38 KB

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