av7110_av.c 39 KB

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