av7110_av.c 39 KB

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