av7110_av.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. /*
  2. * av7110_av.c: audio and video MPEG decoder stuff
  3. *
  4. * Copyright (C) 1999-2002 Ralph Metzler
  5. * & Marcus Metzler for convergence integrated media GmbH
  6. *
  7. * originally based on code by:
  8. * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * To obtain the license, point your browser to
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. *
  25. * the project's page is at https://linuxtv.org
  26. */
  27. #include <linux/types.h>
  28. #include <linux/kernel.h>
  29. #include <linux/string.h>
  30. #include <linux/delay.h>
  31. #include <linux/fs.h>
  32. #include "av7110.h"
  33. #include "av7110_hw.h"
  34. #include "av7110_av.h"
  35. #include "av7110_ipack.h"
  36. /* MPEG-2 (ISO 13818 / H.222.0) stream types */
  37. #define PROG_STREAM_MAP 0xBC
  38. #define PRIVATE_STREAM1 0xBD
  39. #define PADDING_STREAM 0xBE
  40. #define PRIVATE_STREAM2 0xBF
  41. #define AUDIO_STREAM_S 0xC0
  42. #define AUDIO_STREAM_E 0xDF
  43. #define VIDEO_STREAM_S 0xE0
  44. #define VIDEO_STREAM_E 0xEF
  45. #define ECM_STREAM 0xF0
  46. #define EMM_STREAM 0xF1
  47. #define DSM_CC_STREAM 0xF2
  48. #define ISO13522_STREAM 0xF3
  49. #define PROG_STREAM_DIR 0xFF
  50. #define PTS_DTS_FLAGS 0xC0
  51. //pts_dts flags
  52. #define PTS_ONLY 0x80
  53. #define PTS_DTS 0xC0
  54. #define TS_SIZE 188
  55. #define TRANS_ERROR 0x80
  56. #define PAY_START 0x40
  57. #define TRANS_PRIO 0x20
  58. #define PID_MASK_HI 0x1F
  59. //flags
  60. #define TRANS_SCRMBL1 0x80
  61. #define TRANS_SCRMBL2 0x40
  62. #define ADAPT_FIELD 0x20
  63. #define PAYLOAD 0x10
  64. #define COUNT_MASK 0x0F
  65. // adaptation flags
  66. #define DISCON_IND 0x80
  67. #define RAND_ACC_IND 0x40
  68. #define ES_PRI_IND 0x20
  69. #define PCR_FLAG 0x10
  70. #define OPCR_FLAG 0x08
  71. #define SPLICE_FLAG 0x04
  72. #define TRANS_PRIV 0x02
  73. #define ADAP_EXT_FLAG 0x01
  74. // adaptation extension flags
  75. #define LTW_FLAG 0x80
  76. #define PIECE_RATE 0x40
  77. #define SEAM_SPLICE 0x20
  78. static void p_to_t(u8 const *buf, long int length, u16 pid,
  79. u8 *counter, struct dvb_demux_feed *feed);
  80. static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len);
  81. int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
  82. {
  83. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) p2t->priv;
  84. if (!(dvbdmxfeed->ts_type & TS_PACKET))
  85. return 0;
  86. if (buf[3] == 0xe0) // video PES do not have a length in TS
  87. buf[4] = buf[5] = 0;
  88. if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
  89. return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
  90. &dvbdmxfeed->feed.ts, NULL);
  91. else
  92. return dvb_filter_pes2ts(p2t, buf, len, 1);
  93. }
  94. static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
  95. {
  96. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) priv;
  97. dvbdmxfeed->cb.ts(data, 188, NULL, 0,
  98. &dvbdmxfeed->feed.ts, NULL);
  99. return 0;
  100. }
  101. int av7110_av_start_record(struct av7110 *av7110, int av,
  102. struct dvb_demux_feed *dvbdmxfeed)
  103. {
  104. int ret = 0;
  105. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  106. dprintk(2, "av7110:%p, , dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
  107. if (av7110->playing || (av7110->rec_mode & av))
  108. return -EBUSY;
  109. av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
  110. dvbdmx->recording = 1;
  111. av7110->rec_mode |= av;
  112. switch (av7110->rec_mode) {
  113. case RP_AUDIO:
  114. dvb_filter_pes2ts_init(&av7110->p2t[0],
  115. dvbdmx->pesfilter[0]->pid,
  116. dvb_filter_pes2ts_cb,
  117. (void *) dvbdmx->pesfilter[0]);
  118. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
  119. break;
  120. case RP_VIDEO:
  121. dvb_filter_pes2ts_init(&av7110->p2t[1],
  122. dvbdmx->pesfilter[1]->pid,
  123. dvb_filter_pes2ts_cb,
  124. (void *) dvbdmx->pesfilter[1]);
  125. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
  126. break;
  127. case RP_AV:
  128. dvb_filter_pes2ts_init(&av7110->p2t[0],
  129. dvbdmx->pesfilter[0]->pid,
  130. dvb_filter_pes2ts_cb,
  131. (void *) dvbdmx->pesfilter[0]);
  132. dvb_filter_pes2ts_init(&av7110->p2t[1],
  133. dvbdmx->pesfilter[1]->pid,
  134. dvb_filter_pes2ts_cb,
  135. (void *) dvbdmx->pesfilter[1]);
  136. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AV_PES, 0);
  137. break;
  138. }
  139. return ret;
  140. }
  141. int av7110_av_start_play(struct av7110 *av7110, int av)
  142. {
  143. int ret = 0;
  144. dprintk(2, "av7110:%p, \n", av7110);
  145. if (av7110->rec_mode)
  146. return -EBUSY;
  147. if (av7110->playing & av)
  148. return -EBUSY;
  149. av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
  150. if (av7110->playing == RP_NONE) {
  151. av7110_ipack_reset(&av7110->ipack[0]);
  152. av7110_ipack_reset(&av7110->ipack[1]);
  153. }
  154. av7110->playing |= av;
  155. switch (av7110->playing) {
  156. case RP_AUDIO:
  157. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
  158. break;
  159. case RP_VIDEO:
  160. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
  161. av7110->sinfo = 0;
  162. break;
  163. case RP_AV:
  164. av7110->sinfo = 0;
  165. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
  166. break;
  167. }
  168. return ret;
  169. }
  170. int av7110_av_stop(struct av7110 *av7110, int av)
  171. {
  172. int ret = 0;
  173. dprintk(2, "av7110:%p, \n", av7110);
  174. if (!(av7110->playing & av) && !(av7110->rec_mode & av))
  175. return 0;
  176. av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
  177. if (av7110->playing) {
  178. av7110->playing &= ~av;
  179. switch (av7110->playing) {
  180. case RP_AUDIO:
  181. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
  182. break;
  183. case RP_VIDEO:
  184. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
  185. break;
  186. case RP_NONE:
  187. ret = av7110_set_vidmode(av7110, av7110->vidmode);
  188. break;
  189. }
  190. } else {
  191. av7110->rec_mode &= ~av;
  192. switch (av7110->rec_mode) {
  193. case RP_AUDIO:
  194. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
  195. break;
  196. case RP_VIDEO:
  197. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
  198. break;
  199. case RP_NONE:
  200. break;
  201. }
  202. }
  203. return ret;
  204. }
  205. int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
  206. {
  207. int len;
  208. u32 sync;
  209. u16 blen;
  210. if (!dlen) {
  211. wake_up(&buf->queue);
  212. return -1;
  213. }
  214. while (1) {
  215. len = dvb_ringbuffer_avail(buf);
  216. if (len < 6) {
  217. wake_up(&buf->queue);
  218. return -1;
  219. }
  220. sync = DVB_RINGBUFFER_PEEK(buf, 0) << 24;
  221. sync |= DVB_RINGBUFFER_PEEK(buf, 1) << 16;
  222. sync |= DVB_RINGBUFFER_PEEK(buf, 2) << 8;
  223. sync |= DVB_RINGBUFFER_PEEK(buf, 3);
  224. if (((sync &~ 0x0f) == 0x000001e0) ||
  225. ((sync &~ 0x1f) == 0x000001c0) ||
  226. (sync == 0x000001bd))
  227. break;
  228. printk("resync\n");
  229. DVB_RINGBUFFER_SKIP(buf, 1);
  230. }
  231. blen = DVB_RINGBUFFER_PEEK(buf, 4) << 8;
  232. blen |= DVB_RINGBUFFER_PEEK(buf, 5);
  233. blen += 6;
  234. if (len < blen || blen > dlen) {
  235. //printk("buffer empty - avail %d blen %u dlen %d\n", len, blen, dlen);
  236. wake_up(&buf->queue);
  237. return -1;
  238. }
  239. dvb_ringbuffer_read(buf, dest, (size_t) blen);
  240. dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
  241. (unsigned long) buf->pread, (unsigned long) buf->pwrite);
  242. wake_up(&buf->queue);
  243. return blen;
  244. }
  245. int av7110_set_volume(struct av7110 *av7110, unsigned int volleft,
  246. unsigned int volright)
  247. {
  248. unsigned int vol, val, balance = 0;
  249. int err;
  250. dprintk(2, "av7110:%p, \n", av7110);
  251. av7110->mixer.volume_left = volleft;
  252. av7110->mixer.volume_right = volright;
  253. switch (av7110->adac_type) {
  254. case DVB_ADAC_TI:
  255. volleft = (volleft * 256) / 1036;
  256. volright = (volright * 256) / 1036;
  257. if (volleft > 0x3f)
  258. volleft = 0x3f;
  259. if (volright > 0x3f)
  260. volright = 0x3f;
  261. if ((err = SendDAC(av7110, 3, 0x80 + volleft)))
  262. return err;
  263. return SendDAC(av7110, 4, volright);
  264. case DVB_ADAC_CRYSTAL:
  265. volleft = 127 - volleft / 2;
  266. volright = 127 - volright / 2;
  267. i2c_writereg(av7110, 0x20, 0x03, volleft);
  268. i2c_writereg(av7110, 0x20, 0x04, volright);
  269. return 0;
  270. case DVB_ADAC_MSP34x0:
  271. vol = (volleft > volright) ? volleft : volright;
  272. val = (vol * 0x73 / 255) << 8;
  273. if (vol > 0)
  274. balance = ((volright - volleft) * 127) / vol;
  275. msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
  276. msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
  277. msp_writereg(av7110, MSP_WR_DSP, 0x0006, val); /* headphonesr */
  278. return 0;
  279. case DVB_ADAC_MSP34x5:
  280. vol = (volleft > volright) ? volleft : volright;
  281. val = (vol * 0x73 / 255) << 8;
  282. if (vol > 0)
  283. balance = ((volright - volleft) * 127) / vol;
  284. msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
  285. msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
  286. return 0;
  287. }
  288. return 0;
  289. }
  290. int av7110_set_vidmode(struct av7110 *av7110, enum av7110_video_mode mode)
  291. {
  292. int ret;
  293. dprintk(2, "av7110:%p, \n", av7110);
  294. ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
  295. if (!ret && !av7110->playing) {
  296. ret = ChangePIDs(av7110, av7110->pids[DMX_PES_VIDEO],
  297. av7110->pids[DMX_PES_AUDIO],
  298. av7110->pids[DMX_PES_TELETEXT],
  299. 0, av7110->pids[DMX_PES_PCR]);
  300. if (!ret)
  301. ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
  302. }
  303. return ret;
  304. }
  305. static enum av7110_video_mode sw2mode[16] = {
  306. AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
  307. AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_PAL,
  308. AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_NTSC,
  309. AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
  310. AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
  311. AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
  312. AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
  313. AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
  314. };
  315. static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
  316. {
  317. int i;
  318. int hsize, vsize;
  319. int sw;
  320. u8 *p;
  321. int ret = 0;
  322. dprintk(2, "av7110:%p, \n", av7110);
  323. if (av7110->sinfo)
  324. return 0;
  325. for (i = 7; i < count - 10; i++) {
  326. p = buf + i;
  327. if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
  328. continue;
  329. p += 4;
  330. hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
  331. vsize = ((p[1] &0x0F) << 8) | (p[2]);
  332. sw = (p[3] & 0x0F);
  333. ret = av7110_set_vidmode(av7110, sw2mode[sw]);
  334. if (!ret) {
  335. dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
  336. av7110->sinfo = 1;
  337. }
  338. break;
  339. }
  340. return ret;
  341. }
  342. /****************************************************************************
  343. * I/O buffer management and control
  344. ****************************************************************************/
  345. static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
  346. const u8 *buf, unsigned long count)
  347. {
  348. unsigned long todo = count;
  349. int free;
  350. while (todo > 0) {
  351. if (dvb_ringbuffer_free(rbuf) < 2048) {
  352. if (wait_event_interruptible(rbuf->queue,
  353. (dvb_ringbuffer_free(rbuf) >= 2048)))
  354. return count - todo;
  355. }
  356. free = dvb_ringbuffer_free(rbuf);
  357. if (free > todo)
  358. free = todo;
  359. dvb_ringbuffer_write(rbuf, buf, free);
  360. todo -= free;
  361. buf += free;
  362. }
  363. return count - todo;
  364. }
  365. static void play_video_cb(u8 *buf, int count, void *priv)
  366. {
  367. struct av7110 *av7110 = (struct av7110 *) priv;
  368. dprintk(2, "av7110:%p, \n", av7110);
  369. if ((buf[3] & 0xe0) == 0xe0) {
  370. get_video_format(av7110, buf, count);
  371. aux_ring_buffer_write(&av7110->avout, buf, count);
  372. } else
  373. aux_ring_buffer_write(&av7110->aout, buf, count);
  374. }
  375. static void play_audio_cb(u8 *buf, int count, void *priv)
  376. {
  377. struct av7110 *av7110 = (struct av7110 *) priv;
  378. dprintk(2, "av7110:%p, \n", av7110);
  379. aux_ring_buffer_write(&av7110->aout, buf, count);
  380. }
  381. #define FREE_COND_TS (dvb_ringbuffer_free(rb) >= 4096)
  382. static ssize_t ts_play(struct av7110 *av7110, const char __user *buf,
  383. unsigned long count, int nonblock, int type)
  384. {
  385. struct dvb_ringbuffer *rb;
  386. u8 *kb;
  387. unsigned long todo = count;
  388. dprintk(2, "%s: type %d cnt %lu\n", __func__, type, count);
  389. rb = (type) ? &av7110->avout : &av7110->aout;
  390. kb = av7110->kbuf[type];
  391. if (!kb)
  392. return -ENOBUFS;
  393. if (nonblock && !FREE_COND_TS)
  394. return -EWOULDBLOCK;
  395. while (todo >= TS_SIZE) {
  396. if (!FREE_COND_TS) {
  397. if (nonblock)
  398. return count - todo;
  399. if (wait_event_interruptible(rb->queue, FREE_COND_TS))
  400. return count - todo;
  401. }
  402. if (copy_from_user(kb, buf, TS_SIZE))
  403. return -EFAULT;
  404. write_ts_to_decoder(av7110, type, kb, TS_SIZE);
  405. todo -= TS_SIZE;
  406. buf += TS_SIZE;
  407. }
  408. return count - todo;
  409. }
  410. #define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
  411. dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
  412. static ssize_t dvb_play(struct av7110 *av7110, const char __user *buf,
  413. unsigned long count, int nonblock, int type)
  414. {
  415. unsigned long todo = count, n;
  416. dprintk(2, "av7110:%p, \n", av7110);
  417. if (!av7110->kbuf[type])
  418. return -ENOBUFS;
  419. if (nonblock && !FREE_COND)
  420. return -EWOULDBLOCK;
  421. while (todo > 0) {
  422. if (!FREE_COND) {
  423. if (nonblock)
  424. return count - todo;
  425. if (wait_event_interruptible(av7110->avout.queue,
  426. FREE_COND))
  427. return count - todo;
  428. }
  429. n = todo;
  430. if (n > IPACKS * 2)
  431. n = IPACKS * 2;
  432. if (copy_from_user(av7110->kbuf[type], buf, n))
  433. return -EFAULT;
  434. av7110_ipack_instant_repack(av7110->kbuf[type], n,
  435. &av7110->ipack[type]);
  436. todo -= n;
  437. buf += n;
  438. }
  439. return count - todo;
  440. }
  441. static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
  442. unsigned long count, int nonblock, int type)
  443. {
  444. unsigned long todo = count, n;
  445. dprintk(2, "av7110:%p, \n", av7110);
  446. if (!av7110->kbuf[type])
  447. return -ENOBUFS;
  448. if (nonblock && !FREE_COND)
  449. return -EWOULDBLOCK;
  450. while (todo > 0) {
  451. if (!FREE_COND) {
  452. if (nonblock)
  453. return count - todo;
  454. if (wait_event_interruptible(av7110->avout.queue,
  455. FREE_COND))
  456. return count - todo;
  457. }
  458. n = todo;
  459. if (n > IPACKS * 2)
  460. n = IPACKS * 2;
  461. av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
  462. todo -= n;
  463. buf += n;
  464. }
  465. return count - todo;
  466. }
  467. static ssize_t dvb_aplay(struct av7110 *av7110, const char __user *buf,
  468. unsigned long count, int nonblock, int type)
  469. {
  470. unsigned long todo = count, n;
  471. dprintk(2, "av7110:%p, \n", av7110);
  472. if (!av7110->kbuf[type])
  473. return -ENOBUFS;
  474. if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
  475. return -EWOULDBLOCK;
  476. while (todo > 0) {
  477. if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
  478. if (nonblock)
  479. return count - todo;
  480. if (wait_event_interruptible(av7110->aout.queue,
  481. (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
  482. return count-todo;
  483. }
  484. n = todo;
  485. if (n > IPACKS * 2)
  486. n = IPACKS * 2;
  487. if (copy_from_user(av7110->kbuf[type], buf, n))
  488. return -EFAULT;
  489. av7110_ipack_instant_repack(av7110->kbuf[type], n,
  490. &av7110->ipack[type]);
  491. todo -= n;
  492. buf += n;
  493. }
  494. return count - todo;
  495. }
  496. void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
  497. {
  498. memset(p->pes, 0, TS_SIZE);
  499. p->counter = 0;
  500. p->pos = 0;
  501. p->frags = 0;
  502. if (feed)
  503. p->feed = feed;
  504. }
  505. static void clear_p2t(struct av7110_p2t *p)
  506. {
  507. memset(p->pes, 0, TS_SIZE);
  508. // p->counter = 0;
  509. p->pos = 0;
  510. p->frags = 0;
  511. }
  512. static int find_pes_header(u8 const *buf, long int length, int *frags)
  513. {
  514. int c = 0;
  515. int found = 0;
  516. *frags = 0;
  517. while (c < length - 3 && !found) {
  518. if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
  519. buf[c + 2] == 0x01) {
  520. switch ( buf[c + 3] ) {
  521. case PROG_STREAM_MAP:
  522. case PRIVATE_STREAM2:
  523. case PROG_STREAM_DIR:
  524. case ECM_STREAM :
  525. case EMM_STREAM :
  526. case PADDING_STREAM :
  527. case DSM_CC_STREAM :
  528. case ISO13522_STREAM:
  529. case PRIVATE_STREAM1:
  530. case AUDIO_STREAM_S ... AUDIO_STREAM_E:
  531. case VIDEO_STREAM_S ... VIDEO_STREAM_E:
  532. found = 1;
  533. break;
  534. default:
  535. c++;
  536. break;
  537. }
  538. } else
  539. c++;
  540. }
  541. if (c == length - 3 && !found) {
  542. if (buf[length - 1] == 0x00)
  543. *frags = 1;
  544. if (buf[length - 2] == 0x00 &&
  545. buf[length - 1] == 0x00)
  546. *frags = 2;
  547. if (buf[length - 3] == 0x00 &&
  548. buf[length - 2] == 0x00 &&
  549. buf[length - 1] == 0x01)
  550. *frags = 3;
  551. return -1;
  552. }
  553. return c;
  554. }
  555. void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
  556. {
  557. int c, c2, l, add;
  558. int check, rest;
  559. c = 0;
  560. c2 = 0;
  561. if (p->frags){
  562. check = 0;
  563. switch(p->frags) {
  564. case 1:
  565. if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
  566. check = 1;
  567. c += 2;
  568. }
  569. break;
  570. case 2:
  571. if (buf[c] == 0x01) {
  572. check = 1;
  573. c++;
  574. }
  575. break;
  576. case 3:
  577. check = 1;
  578. }
  579. if (check) {
  580. switch (buf[c]) {
  581. case PROG_STREAM_MAP:
  582. case PRIVATE_STREAM2:
  583. case PROG_STREAM_DIR:
  584. case ECM_STREAM :
  585. case EMM_STREAM :
  586. case PADDING_STREAM :
  587. case DSM_CC_STREAM :
  588. case ISO13522_STREAM:
  589. case PRIVATE_STREAM1:
  590. case AUDIO_STREAM_S ... AUDIO_STREAM_E:
  591. case VIDEO_STREAM_S ... VIDEO_STREAM_E:
  592. p->pes[0] = 0x00;
  593. p->pes[1] = 0x00;
  594. p->pes[2] = 0x01;
  595. p->pes[3] = buf[c];
  596. p->pos = 4;
  597. memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
  598. c += (TS_SIZE - 4) - p->pos;
  599. p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
  600. clear_p2t(p);
  601. break;
  602. default:
  603. c = 0;
  604. break;
  605. }
  606. }
  607. p->frags = 0;
  608. }
  609. if (p->pos) {
  610. c2 = find_pes_header(buf + c, length - c, &p->frags);
  611. if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
  612. l = c2+c;
  613. else
  614. l = (TS_SIZE - 4) - p->pos;
  615. memcpy(p->pes + p->pos, buf, l);
  616. c += l;
  617. p->pos += l;
  618. p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
  619. clear_p2t(p);
  620. }
  621. add = 0;
  622. while (c < length) {
  623. c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
  624. if (c2 >= 0) {
  625. c2 += c + add;
  626. if (c2 > c){
  627. p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
  628. c = c2;
  629. clear_p2t(p);
  630. add = 0;
  631. } else
  632. add = 1;
  633. } else {
  634. l = length - c;
  635. rest = l % (TS_SIZE - 4);
  636. l -= rest;
  637. p_to_t(buf + c, l, pid, &p->counter, p->feed);
  638. memcpy(p->pes, buf + c + l, rest);
  639. p->pos = rest;
  640. c = length;
  641. }
  642. }
  643. }
  644. static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
  645. {
  646. int i;
  647. int c = 0;
  648. int fill;
  649. u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
  650. fill = (TS_SIZE - 4) - length;
  651. if (pes_start)
  652. tshead[1] = 0x40;
  653. if (fill)
  654. tshead[3] = 0x30;
  655. tshead[1] |= (u8)((pid & 0x1F00) >> 8);
  656. tshead[2] |= (u8)(pid & 0x00FF);
  657. tshead[3] |= ((*counter)++ & 0x0F);
  658. memcpy(buf, tshead, 4);
  659. c += 4;
  660. if (fill) {
  661. buf[4] = fill - 1;
  662. c++;
  663. if (fill > 1) {
  664. buf[5] = 0x00;
  665. c++;
  666. }
  667. for (i = 6; i < fill + 4; i++) {
  668. buf[i] = 0xFF;
  669. c++;
  670. }
  671. }
  672. return c;
  673. }
  674. static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
  675. struct dvb_demux_feed *feed)
  676. {
  677. int l, pes_start;
  678. u8 obuf[TS_SIZE];
  679. long c = 0;
  680. pes_start = 0;
  681. if (length > 3 &&
  682. buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
  683. switch (buf[3]) {
  684. case PROG_STREAM_MAP:
  685. case PRIVATE_STREAM2:
  686. case PROG_STREAM_DIR:
  687. case ECM_STREAM :
  688. case EMM_STREAM :
  689. case PADDING_STREAM :
  690. case DSM_CC_STREAM :
  691. case ISO13522_STREAM:
  692. case PRIVATE_STREAM1:
  693. case AUDIO_STREAM_S ... AUDIO_STREAM_E:
  694. case VIDEO_STREAM_S ... VIDEO_STREAM_E:
  695. pes_start = 1;
  696. break;
  697. default:
  698. break;
  699. }
  700. while (c < length) {
  701. memset(obuf, 0, TS_SIZE);
  702. if (length - c >= (TS_SIZE - 4)){
  703. l = write_ts_header2(pid, counter, pes_start,
  704. obuf, (TS_SIZE - 4));
  705. memcpy(obuf + l, buf + c, TS_SIZE - l);
  706. c += TS_SIZE - l;
  707. } else {
  708. l = write_ts_header2(pid, counter, pes_start,
  709. obuf, length - c);
  710. memcpy(obuf + l, buf + c, TS_SIZE - l);
  711. c = length;
  712. }
  713. feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, NULL);
  714. pes_start = 0;
  715. }
  716. }
  717. static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len)
  718. {
  719. struct ipack *ipack = &av7110->ipack[type];
  720. if (buf[1] & TRANS_ERROR) {
  721. av7110_ipack_reset(ipack);
  722. return -1;
  723. }
  724. if (!(buf[3] & PAYLOAD))
  725. return -1;
  726. if (buf[1] & PAY_START)
  727. av7110_ipack_flush(ipack);
  728. if (buf[3] & ADAPT_FIELD) {
  729. len -= buf[4] + 1;
  730. buf += buf[4] + 1;
  731. if (!len)
  732. return 0;
  733. }
  734. av7110_ipack_instant_repack(buf + 4, len - 4, ipack);
  735. return 0;
  736. }
  737. int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
  738. {
  739. struct dvb_demux *demux = feed->demux;
  740. struct av7110 *av7110 = (struct av7110 *) demux->priv;
  741. dprintk(2, "av7110:%p, \n", av7110);
  742. if (av7110->full_ts && demux->dmx.frontend->source != DMX_MEMORY_FE)
  743. return 0;
  744. switch (feed->pes_type) {
  745. case 0:
  746. if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
  747. return -EINVAL;
  748. break;
  749. case 1:
  750. if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
  751. return -EINVAL;
  752. break;
  753. default:
  754. return -1;
  755. }
  756. return write_ts_to_decoder(av7110, feed->pes_type, buf, len);
  757. }
  758. /******************************************************************************
  759. * Video MPEG decoder events
  760. ******************************************************************************/
  761. void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
  762. {
  763. struct dvb_video_events *events = &av7110->video_events;
  764. int wp;
  765. spin_lock_bh(&events->lock);
  766. wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
  767. if (wp == events->eventr) {
  768. events->overflow = 1;
  769. events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
  770. }
  771. //FIXME: timestamp?
  772. memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
  773. events->eventw = wp;
  774. spin_unlock_bh(&events->lock);
  775. wake_up_interruptible(&events->wait_queue);
  776. }
  777. static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
  778. {
  779. struct dvb_video_events *events = &av7110->video_events;
  780. if (events->overflow) {
  781. events->overflow = 0;
  782. return -EOVERFLOW;
  783. }
  784. if (events->eventw == events->eventr) {
  785. int ret;
  786. if (flags & O_NONBLOCK)
  787. return -EWOULDBLOCK;
  788. ret = wait_event_interruptible(events->wait_queue,
  789. events->eventw != events->eventr);
  790. if (ret < 0)
  791. return ret;
  792. }
  793. spin_lock_bh(&events->lock);
  794. memcpy(event, &events->events[events->eventr],
  795. sizeof(struct video_event));
  796. events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
  797. spin_unlock_bh(&events->lock);
  798. return 0;
  799. }
  800. /******************************************************************************
  801. * DVB device file operations
  802. ******************************************************************************/
  803. static __poll_t dvb_video_poll(struct file *file, poll_table *wait)
  804. {
  805. struct dvb_device *dvbdev = file->private_data;
  806. struct av7110 *av7110 = dvbdev->priv;
  807. __poll_t mask = 0;
  808. dprintk(2, "av7110:%p, \n", av7110);
  809. if ((file->f_flags & O_ACCMODE) != O_RDONLY)
  810. poll_wait(file, &av7110->avout.queue, wait);
  811. poll_wait(file, &av7110->video_events.wait_queue, wait);
  812. if (av7110->video_events.eventw != av7110->video_events.eventr)
  813. mask = EPOLLPRI;
  814. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  815. if (av7110->playing) {
  816. if (FREE_COND)
  817. mask |= (EPOLLOUT | EPOLLWRNORM);
  818. } else {
  819. /* if not playing: may play if asked for */
  820. mask |= (EPOLLOUT | EPOLLWRNORM);
  821. }
  822. }
  823. return mask;
  824. }
  825. static ssize_t dvb_video_write(struct file *file, const char __user *buf,
  826. size_t count, loff_t *ppos)
  827. {
  828. struct dvb_device *dvbdev = file->private_data;
  829. struct av7110 *av7110 = dvbdev->priv;
  830. unsigned char c;
  831. dprintk(2, "av7110:%p, \n", av7110);
  832. if ((file->f_flags & O_ACCMODE) == O_RDONLY)
  833. return -EPERM;
  834. if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
  835. return -EPERM;
  836. if (get_user(c, buf))
  837. return -EFAULT;
  838. if (c == 0x47 && count % TS_SIZE == 0)
  839. return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
  840. else
  841. return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
  842. }
  843. static __poll_t dvb_audio_poll(struct file *file, poll_table *wait)
  844. {
  845. struct dvb_device *dvbdev = file->private_data;
  846. struct av7110 *av7110 = dvbdev->priv;
  847. __poll_t mask = 0;
  848. dprintk(2, "av7110:%p, \n", av7110);
  849. poll_wait(file, &av7110->aout.queue, wait);
  850. if (av7110->playing) {
  851. if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
  852. mask |= (EPOLLOUT | EPOLLWRNORM);
  853. } else /* if not playing: may play if asked for */
  854. mask = (EPOLLOUT | EPOLLWRNORM);
  855. return mask;
  856. }
  857. static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
  858. size_t count, loff_t *ppos)
  859. {
  860. struct dvb_device *dvbdev = file->private_data;
  861. struct av7110 *av7110 = dvbdev->priv;
  862. unsigned char c;
  863. dprintk(2, "av7110:%p, \n", av7110);
  864. if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
  865. printk(KERN_ERR "not audio source memory\n");
  866. return -EPERM;
  867. }
  868. if (get_user(c, buf))
  869. return -EFAULT;
  870. if (c == 0x47 && count % TS_SIZE == 0)
  871. return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
  872. else
  873. return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
  874. }
  875. static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
  876. #define MIN_IFRAME 400000
  877. static int play_iframe(struct av7110 *av7110, char __user *buf, unsigned int len, int nonblock)
  878. {
  879. unsigned i, n;
  880. int progressive = 0;
  881. int match = 0;
  882. dprintk(2, "av7110:%p, \n", av7110);
  883. if (len == 0)
  884. return 0;
  885. if (!(av7110->playing & RP_VIDEO)) {
  886. if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
  887. return -EBUSY;
  888. }
  889. /* search in buf for instances of 00 00 01 b5 1? */
  890. for (i = 0; i < len; i++) {
  891. unsigned char c;
  892. if (get_user(c, buf + i))
  893. return -EFAULT;
  894. if (match == 5) {
  895. progressive = c & 0x08;
  896. match = 0;
  897. }
  898. if (c == 0x00) {
  899. match = (match == 1 || match == 2) ? 2 : 1;
  900. continue;
  901. }
  902. switch (match++) {
  903. case 2: if (c == 0x01)
  904. continue;
  905. break;
  906. case 3: if (c == 0xb5)
  907. continue;
  908. break;
  909. case 4: if ((c & 0xf0) == 0x10)
  910. continue;
  911. break;
  912. }
  913. match = 0;
  914. }
  915. /* setting n always > 1, fixes problems when playing stillframes
  916. consisting of I- and P-Frames */
  917. n = MIN_IFRAME / len + 1;
  918. /* FIXME: nonblock? */
  919. dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
  920. for (i = 0; i < n; i++)
  921. dvb_play(av7110, buf, len, 0, 1);
  922. av7110_ipack_flush(&av7110->ipack[1]);
  923. if (progressive)
  924. return vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
  925. else
  926. return 0;
  927. }
  928. static int dvb_video_ioctl(struct file *file,
  929. unsigned int cmd, void *parg)
  930. {
  931. struct dvb_device *dvbdev = file->private_data;
  932. struct av7110 *av7110 = dvbdev->priv;
  933. unsigned long arg = (unsigned long) parg;
  934. int ret = 0;
  935. dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
  936. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  937. if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
  938. cmd != VIDEO_GET_SIZE ) {
  939. return -EPERM;
  940. }
  941. }
  942. if (mutex_lock_interruptible(&av7110->ioctl_mutex))
  943. return -ERESTARTSYS;
  944. switch (cmd) {
  945. case VIDEO_STOP:
  946. av7110->videostate.play_state = VIDEO_STOPPED;
  947. if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
  948. ret = av7110_av_stop(av7110, RP_VIDEO);
  949. else
  950. ret = vidcom(av7110, AV_VIDEO_CMD_STOP,
  951. av7110->videostate.video_blank ? 0 : 1);
  952. if (!ret)
  953. av7110->trickmode = TRICK_NONE;
  954. break;
  955. case VIDEO_PLAY:
  956. av7110->trickmode = TRICK_NONE;
  957. if (av7110->videostate.play_state == VIDEO_FREEZED) {
  958. av7110->videostate.play_state = VIDEO_PLAYING;
  959. ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
  960. if (ret)
  961. break;
  962. }
  963. if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
  964. if (av7110->playing == RP_AV) {
  965. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
  966. if (ret)
  967. break;
  968. av7110->playing &= ~RP_VIDEO;
  969. }
  970. ret = av7110_av_start_play(av7110, RP_VIDEO);
  971. }
  972. if (!ret)
  973. ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
  974. if (!ret)
  975. av7110->videostate.play_state = VIDEO_PLAYING;
  976. break;
  977. case VIDEO_FREEZE:
  978. av7110->videostate.play_state = VIDEO_FREEZED;
  979. if (av7110->playing & RP_VIDEO)
  980. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
  981. else
  982. ret = vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
  983. if (!ret)
  984. av7110->trickmode = TRICK_FREEZE;
  985. break;
  986. case VIDEO_CONTINUE:
  987. if (av7110->playing & RP_VIDEO)
  988. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
  989. if (!ret)
  990. ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
  991. if (!ret) {
  992. av7110->videostate.play_state = VIDEO_PLAYING;
  993. av7110->trickmode = TRICK_NONE;
  994. }
  995. break;
  996. case VIDEO_SELECT_SOURCE:
  997. av7110->videostate.stream_source = (video_stream_source_t) arg;
  998. break;
  999. case VIDEO_SET_BLANK:
  1000. av7110->videostate.video_blank = (int) arg;
  1001. break;
  1002. case VIDEO_GET_STATUS:
  1003. memcpy(parg, &av7110->videostate, sizeof(struct video_status));
  1004. break;
  1005. case VIDEO_GET_EVENT:
  1006. ret = dvb_video_get_event(av7110, parg, file->f_flags);
  1007. break;
  1008. case VIDEO_GET_SIZE:
  1009. memcpy(parg, &av7110->video_size, sizeof(video_size_t));
  1010. break;
  1011. case VIDEO_SET_DISPLAY_FORMAT:
  1012. {
  1013. video_displayformat_t format = (video_displayformat_t) arg;
  1014. switch (format) {
  1015. case VIDEO_PAN_SCAN:
  1016. av7110->display_panscan = VID_PAN_SCAN_PREF;
  1017. break;
  1018. case VIDEO_LETTER_BOX:
  1019. av7110->display_panscan = VID_VC_AND_PS_PREF;
  1020. break;
  1021. case VIDEO_CENTER_CUT_OUT:
  1022. av7110->display_panscan = VID_CENTRE_CUT_PREF;
  1023. break;
  1024. default:
  1025. ret = -EINVAL;
  1026. }
  1027. if (ret < 0)
  1028. break;
  1029. av7110->videostate.display_format = format;
  1030. ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
  1031. 1, av7110->display_panscan);
  1032. break;
  1033. }
  1034. case VIDEO_SET_FORMAT:
  1035. if (arg > 1) {
  1036. ret = -EINVAL;
  1037. break;
  1038. }
  1039. av7110->display_ar = arg;
  1040. ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
  1041. 1, (u16) arg);
  1042. break;
  1043. case VIDEO_STILLPICTURE:
  1044. {
  1045. struct video_still_picture *pic =
  1046. (struct video_still_picture *) parg;
  1047. av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
  1048. dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
  1049. ret = play_iframe(av7110, pic->iFrame, pic->size,
  1050. file->f_flags & O_NONBLOCK);
  1051. break;
  1052. }
  1053. case VIDEO_FAST_FORWARD:
  1054. //note: arg is ignored by firmware
  1055. if (av7110->playing & RP_VIDEO)
  1056. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
  1057. __Scan_I, 2, AV_PES, 0);
  1058. else
  1059. ret = vidcom(av7110, AV_VIDEO_CMD_FFWD, arg);
  1060. if (!ret) {
  1061. av7110->trickmode = TRICK_FAST;
  1062. av7110->videostate.play_state = VIDEO_PLAYING;
  1063. }
  1064. break;
  1065. case VIDEO_SLOWMOTION:
  1066. if (av7110->playing&RP_VIDEO) {
  1067. if (av7110->trickmode != TRICK_SLOW)
  1068. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
  1069. if (!ret)
  1070. ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
  1071. } else {
  1072. ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
  1073. if (!ret)
  1074. ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 0);
  1075. if (!ret)
  1076. ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
  1077. }
  1078. if (!ret) {
  1079. av7110->trickmode = TRICK_SLOW;
  1080. av7110->videostate.play_state = VIDEO_PLAYING;
  1081. }
  1082. break;
  1083. case VIDEO_GET_CAPABILITIES:
  1084. *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
  1085. VIDEO_CAP_SYS | VIDEO_CAP_PROG;
  1086. break;
  1087. case VIDEO_CLEAR_BUFFER:
  1088. dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
  1089. av7110_ipack_reset(&av7110->ipack[1]);
  1090. if (av7110->playing == RP_AV) {
  1091. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
  1092. __Play, 2, AV_PES, 0);
  1093. if (ret)
  1094. break;
  1095. if (av7110->trickmode == TRICK_FAST)
  1096. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
  1097. __Scan_I, 2, AV_PES, 0);
  1098. if (av7110->trickmode == TRICK_SLOW) {
  1099. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
  1100. __Slow, 2, 0, 0);
  1101. if (!ret)
  1102. ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
  1103. }
  1104. if (av7110->trickmode == TRICK_FREEZE)
  1105. ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 1);
  1106. }
  1107. break;
  1108. case VIDEO_SET_STREAMTYPE:
  1109. break;
  1110. default:
  1111. ret = -ENOIOCTLCMD;
  1112. break;
  1113. }
  1114. mutex_unlock(&av7110->ioctl_mutex);
  1115. return ret;
  1116. }
  1117. static int dvb_audio_ioctl(struct file *file,
  1118. unsigned int cmd, void *parg)
  1119. {
  1120. struct dvb_device *dvbdev = file->private_data;
  1121. struct av7110 *av7110 = dvbdev->priv;
  1122. unsigned long arg = (unsigned long) parg;
  1123. int ret = 0;
  1124. dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
  1125. if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
  1126. (cmd != AUDIO_GET_STATUS))
  1127. return -EPERM;
  1128. if (mutex_lock_interruptible(&av7110->ioctl_mutex))
  1129. return -ERESTARTSYS;
  1130. switch (cmd) {
  1131. case AUDIO_STOP:
  1132. if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
  1133. ret = av7110_av_stop(av7110, RP_AUDIO);
  1134. else
  1135. ret = audcom(av7110, AUDIO_CMD_MUTE);
  1136. if (!ret)
  1137. av7110->audiostate.play_state = AUDIO_STOPPED;
  1138. break;
  1139. case AUDIO_PLAY:
  1140. if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
  1141. ret = av7110_av_start_play(av7110, RP_AUDIO);
  1142. if (!ret)
  1143. ret = audcom(av7110, AUDIO_CMD_UNMUTE);
  1144. if (!ret)
  1145. av7110->audiostate.play_state = AUDIO_PLAYING;
  1146. break;
  1147. case AUDIO_PAUSE:
  1148. ret = audcom(av7110, AUDIO_CMD_MUTE);
  1149. if (!ret)
  1150. av7110->audiostate.play_state = AUDIO_PAUSED;
  1151. break;
  1152. case AUDIO_CONTINUE:
  1153. if (av7110->audiostate.play_state == AUDIO_PAUSED) {
  1154. av7110->audiostate.play_state = AUDIO_PLAYING;
  1155. ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
  1156. }
  1157. break;
  1158. case AUDIO_SELECT_SOURCE:
  1159. av7110->audiostate.stream_source = (audio_stream_source_t) arg;
  1160. break;
  1161. case AUDIO_SET_MUTE:
  1162. {
  1163. ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
  1164. if (!ret)
  1165. av7110->audiostate.mute_state = (int) arg;
  1166. break;
  1167. }
  1168. case AUDIO_SET_AV_SYNC:
  1169. av7110->audiostate.AV_sync_state = (int) arg;
  1170. ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
  1171. break;
  1172. case AUDIO_SET_BYPASS_MODE:
  1173. if (FW_VERSION(av7110->arm_app) < 0x2621)
  1174. ret = -EINVAL;
  1175. av7110->audiostate.bypass_mode = (int)arg;
  1176. break;
  1177. case AUDIO_CHANNEL_SELECT:
  1178. av7110->audiostate.channel_select = (audio_channel_select_t) arg;
  1179. switch(av7110->audiostate.channel_select) {
  1180. case AUDIO_STEREO:
  1181. ret = audcom(av7110, AUDIO_CMD_STEREO);
  1182. if (!ret) {
  1183. if (av7110->adac_type == DVB_ADAC_CRYSTAL)
  1184. i2c_writereg(av7110, 0x20, 0x02, 0x49);
  1185. else if (av7110->adac_type == DVB_ADAC_MSP34x5)
  1186. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220);
  1187. }
  1188. break;
  1189. case AUDIO_MONO_LEFT:
  1190. ret = audcom(av7110, AUDIO_CMD_MONO_L);
  1191. if (!ret) {
  1192. if (av7110->adac_type == DVB_ADAC_CRYSTAL)
  1193. i2c_writereg(av7110, 0x20, 0x02, 0x4a);
  1194. else if (av7110->adac_type == DVB_ADAC_MSP34x5)
  1195. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0200);
  1196. }
  1197. break;
  1198. case AUDIO_MONO_RIGHT:
  1199. ret = audcom(av7110, AUDIO_CMD_MONO_R);
  1200. if (!ret) {
  1201. if (av7110->adac_type == DVB_ADAC_CRYSTAL)
  1202. i2c_writereg(av7110, 0x20, 0x02, 0x45);
  1203. else if (av7110->adac_type == DVB_ADAC_MSP34x5)
  1204. msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0210);
  1205. }
  1206. break;
  1207. default:
  1208. ret = -EINVAL;
  1209. break;
  1210. }
  1211. break;
  1212. case AUDIO_GET_STATUS:
  1213. memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
  1214. break;
  1215. case AUDIO_GET_CAPABILITIES:
  1216. if (FW_VERSION(av7110->arm_app) < 0x2621)
  1217. *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
  1218. else
  1219. *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
  1220. AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
  1221. break;
  1222. case AUDIO_CLEAR_BUFFER:
  1223. dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
  1224. av7110_ipack_reset(&av7110->ipack[0]);
  1225. if (av7110->playing == RP_AV)
  1226. ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
  1227. __Play, 2, AV_PES, 0);
  1228. break;
  1229. case AUDIO_SET_ID:
  1230. break;
  1231. case AUDIO_SET_MIXER:
  1232. {
  1233. struct audio_mixer *amix = (struct audio_mixer *)parg;
  1234. ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
  1235. break;
  1236. }
  1237. case AUDIO_SET_STREAMTYPE:
  1238. break;
  1239. default:
  1240. ret = -ENOIOCTLCMD;
  1241. }
  1242. mutex_unlock(&av7110->ioctl_mutex);
  1243. return ret;
  1244. }
  1245. static int dvb_video_open(struct inode *inode, struct file *file)
  1246. {
  1247. struct dvb_device *dvbdev = file->private_data;
  1248. struct av7110 *av7110 = dvbdev->priv;
  1249. int err;
  1250. dprintk(2, "av7110:%p, \n", av7110);
  1251. if ((err = dvb_generic_open(inode, file)) < 0)
  1252. return err;
  1253. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  1254. dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
  1255. dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
  1256. av7110->video_blank = 1;
  1257. av7110->audiostate.AV_sync_state = 1;
  1258. av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
  1259. /* empty event queue */
  1260. av7110->video_events.eventr = av7110->video_events.eventw = 0;
  1261. }
  1262. return 0;
  1263. }
  1264. static int dvb_video_release(struct inode *inode, struct file *file)
  1265. {
  1266. struct dvb_device *dvbdev = file->private_data;
  1267. struct av7110 *av7110 = dvbdev->priv;
  1268. dprintk(2, "av7110:%p, \n", av7110);
  1269. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  1270. av7110_av_stop(av7110, RP_VIDEO);
  1271. }
  1272. return dvb_generic_release(inode, file);
  1273. }
  1274. static int dvb_audio_open(struct inode *inode, struct file *file)
  1275. {
  1276. struct dvb_device *dvbdev = file->private_data;
  1277. struct av7110 *av7110 = dvbdev->priv;
  1278. int err = dvb_generic_open(inode, file);
  1279. dprintk(2, "av7110:%p, \n", av7110);
  1280. if (err < 0)
  1281. return err;
  1282. dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
  1283. av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
  1284. return 0;
  1285. }
  1286. static int dvb_audio_release(struct inode *inode, struct file *file)
  1287. {
  1288. struct dvb_device *dvbdev = file->private_data;
  1289. struct av7110 *av7110 = dvbdev->priv;
  1290. dprintk(2, "av7110:%p, \n", av7110);
  1291. av7110_av_stop(av7110, RP_AUDIO);
  1292. return dvb_generic_release(inode, file);
  1293. }
  1294. /******************************************************************************
  1295. * driver registration
  1296. ******************************************************************************/
  1297. static const struct file_operations dvb_video_fops = {
  1298. .owner = THIS_MODULE,
  1299. .write = dvb_video_write,
  1300. .unlocked_ioctl = dvb_generic_ioctl,
  1301. .open = dvb_video_open,
  1302. .release = dvb_video_release,
  1303. .poll = dvb_video_poll,
  1304. .llseek = noop_llseek,
  1305. };
  1306. static struct dvb_device dvbdev_video = {
  1307. .priv = NULL,
  1308. .users = 6,
  1309. .readers = 5, /* arbitrary */
  1310. .writers = 1,
  1311. .fops = &dvb_video_fops,
  1312. .kernel_ioctl = dvb_video_ioctl,
  1313. };
  1314. static const struct file_operations dvb_audio_fops = {
  1315. .owner = THIS_MODULE,
  1316. .write = dvb_audio_write,
  1317. .unlocked_ioctl = dvb_generic_ioctl,
  1318. .open = dvb_audio_open,
  1319. .release = dvb_audio_release,
  1320. .poll = dvb_audio_poll,
  1321. .llseek = noop_llseek,
  1322. };
  1323. static struct dvb_device dvbdev_audio = {
  1324. .priv = NULL,
  1325. .users = 1,
  1326. .writers = 1,
  1327. .fops = &dvb_audio_fops,
  1328. .kernel_ioctl = dvb_audio_ioctl,
  1329. };
  1330. int av7110_av_register(struct av7110 *av7110)
  1331. {
  1332. av7110->audiostate.AV_sync_state = 0;
  1333. av7110->audiostate.mute_state = 0;
  1334. av7110->audiostate.play_state = AUDIO_STOPPED;
  1335. av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
  1336. av7110->audiostate.channel_select = AUDIO_STEREO;
  1337. av7110->audiostate.bypass_mode = 0;
  1338. av7110->videostate.video_blank = 0;
  1339. av7110->videostate.play_state = VIDEO_STOPPED;
  1340. av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
  1341. av7110->videostate.video_format = VIDEO_FORMAT_4_3;
  1342. av7110->videostate.display_format = VIDEO_LETTER_BOX;
  1343. av7110->display_ar = VIDEO_FORMAT_4_3;
  1344. av7110->display_panscan = VID_VC_AND_PS_PREF;
  1345. init_waitqueue_head(&av7110->video_events.wait_queue);
  1346. spin_lock_init(&av7110->video_events.lock);
  1347. av7110->video_events.eventw = av7110->video_events.eventr = 0;
  1348. av7110->video_events.overflow = 0;
  1349. memset(&av7110->video_size, 0, sizeof (video_size_t));
  1350. dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
  1351. &dvbdev_video, av7110, DVB_DEVICE_VIDEO, 0);
  1352. dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
  1353. &dvbdev_audio, av7110, DVB_DEVICE_AUDIO, 0);
  1354. return 0;
  1355. }
  1356. void av7110_av_unregister(struct av7110 *av7110)
  1357. {
  1358. dvb_unregister_device(av7110->audio_dev);
  1359. dvb_unregister_device(av7110->video_dev);
  1360. }
  1361. int av7110_av_init(struct av7110 *av7110)
  1362. {
  1363. void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
  1364. int i, ret;
  1365. for (i = 0; i < 2; i++) {
  1366. struct ipack *ipack = av7110->ipack + i;
  1367. ret = av7110_ipack_init(ipack, IPACKS, play[i]);
  1368. if (ret < 0) {
  1369. if (i)
  1370. av7110_ipack_free(--ipack);
  1371. goto out;
  1372. }
  1373. ipack->data = av7110;
  1374. }
  1375. dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
  1376. dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
  1377. av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
  1378. av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
  1379. out:
  1380. return ret;
  1381. }
  1382. void av7110_av_exit(struct av7110 *av7110)
  1383. {
  1384. av7110_ipack_free(&av7110->ipack[0]);
  1385. av7110_ipack_free(&av7110->ipack[1]);
  1386. }