ivtv-ioctl.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. /*
  2. ioctl system call
  3. Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
  4. Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "ivtv-driver.h"
  18. #include "ivtv-version.h"
  19. #include "ivtv-mailbox.h"
  20. #include "ivtv-i2c.h"
  21. #include "ivtv-queue.h"
  22. #include "ivtv-fileops.h"
  23. #include "ivtv-vbi.h"
  24. #include "ivtv-routing.h"
  25. #include "ivtv-streams.h"
  26. #include "ivtv-yuv.h"
  27. #include "ivtv-ioctl.h"
  28. #include "ivtv-gpio.h"
  29. #include "ivtv-controls.h"
  30. #include "ivtv-cards.h"
  31. #include <media/saa7127.h>
  32. #include <media/tveeprom.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <linux/dvb/audio.h>
  35. #include <linux/i2c-id.h>
  36. u16 ivtv_service2vbi(int type)
  37. {
  38. switch (type) {
  39. case V4L2_SLICED_TELETEXT_B:
  40. return IVTV_SLICED_TYPE_TELETEXT_B;
  41. case V4L2_SLICED_CAPTION_525:
  42. return IVTV_SLICED_TYPE_CAPTION_525;
  43. case V4L2_SLICED_WSS_625:
  44. return IVTV_SLICED_TYPE_WSS_625;
  45. case V4L2_SLICED_VPS:
  46. return IVTV_SLICED_TYPE_VPS;
  47. default:
  48. return 0;
  49. }
  50. }
  51. static int valid_service_line(int field, int line, int is_pal)
  52. {
  53. return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
  54. (!is_pal && line >= 10 && line < 22);
  55. }
  56. static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
  57. {
  58. u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
  59. int i;
  60. set = set & valid_set;
  61. if (set == 0 || !valid_service_line(field, line, is_pal)) {
  62. return 0;
  63. }
  64. if (!is_pal) {
  65. if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
  66. return V4L2_SLICED_CAPTION_525;
  67. }
  68. else {
  69. if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
  70. return V4L2_SLICED_VPS;
  71. if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
  72. return V4L2_SLICED_WSS_625;
  73. if (line == 23)
  74. return 0;
  75. }
  76. for (i = 0; i < 32; i++) {
  77. if ((1 << i) & set)
  78. return 1 << i;
  79. }
  80. return 0;
  81. }
  82. void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  83. {
  84. u16 set = fmt->service_set;
  85. int f, l;
  86. fmt->service_set = 0;
  87. for (f = 0; f < 2; f++) {
  88. for (l = 0; l < 24; l++) {
  89. fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
  90. }
  91. }
  92. }
  93. static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  94. {
  95. int f, l;
  96. u16 set = 0;
  97. for (f = 0; f < 2; f++) {
  98. for (l = 0; l < 24; l++) {
  99. fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
  100. set |= fmt->service_lines[f][l];
  101. }
  102. }
  103. return set != 0;
  104. }
  105. u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
  106. {
  107. int f, l;
  108. u16 set = 0;
  109. for (f = 0; f < 2; f++) {
  110. for (l = 0; l < 24; l++) {
  111. set |= fmt->service_lines[f][l];
  112. }
  113. }
  114. return set;
  115. }
  116. void ivtv_set_osd_alpha(struct ivtv *itv)
  117. {
  118. ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
  119. itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
  120. ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
  121. }
  122. int ivtv_set_speed(struct ivtv *itv, int speed)
  123. {
  124. u32 data[CX2341X_MBOX_MAX_DATA];
  125. struct ivtv_stream *s;
  126. int single_step = (speed == 1 || speed == -1);
  127. DEFINE_WAIT(wait);
  128. if (speed == 0) speed = 1000;
  129. /* No change? */
  130. if (speed == itv->speed && !single_step)
  131. return 0;
  132. s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
  133. if (single_step && (speed < 0) == (itv->speed < 0)) {
  134. /* Single step video and no need to change direction */
  135. ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
  136. itv->speed = speed;
  137. return 0;
  138. }
  139. if (single_step)
  140. /* Need to change direction */
  141. speed = speed < 0 ? -1000 : 1000;
  142. data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
  143. data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
  144. data[1] = (speed < 0);
  145. data[2] = speed < 0 ? 3 : 7;
  146. data[3] = itv->params.video_b_frames;
  147. data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
  148. data[5] = 0;
  149. data[6] = 0;
  150. if (speed == 1500 || speed == -1500) data[0] |= 1;
  151. else if (speed == 2000 || speed == -2000) data[0] |= 2;
  152. else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
  153. else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
  154. /* If not decoding, just change speed setting */
  155. if (atomic_read(&itv->decoding) > 0) {
  156. int got_sig = 0;
  157. /* Stop all DMA and decoding activity */
  158. ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
  159. /* Wait for any DMA to finish */
  160. prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
  161. while (itv->i_flags & IVTV_F_I_DMA) {
  162. got_sig = signal_pending(current);
  163. if (got_sig)
  164. break;
  165. got_sig = 0;
  166. schedule();
  167. }
  168. finish_wait(&itv->dma_waitq, &wait);
  169. if (got_sig)
  170. return -EINTR;
  171. /* Change Speed safely */
  172. ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
  173. IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
  174. data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
  175. }
  176. if (single_step) {
  177. speed = (speed < 0) ? -1 : 1;
  178. ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
  179. }
  180. itv->speed = speed;
  181. return 0;
  182. }
  183. static int ivtv_validate_speed(int cur_speed, int new_speed)
  184. {
  185. int fact = new_speed < 0 ? -1 : 1;
  186. int s;
  187. if (cur_speed == 0)
  188. cur_speed = 1000;
  189. if (new_speed < 0)
  190. new_speed = -new_speed;
  191. if (cur_speed < 0)
  192. cur_speed = -cur_speed;
  193. if (cur_speed <= new_speed) {
  194. if (new_speed > 1500)
  195. return fact * 2000;
  196. if (new_speed > 1000)
  197. return fact * 1500;
  198. }
  199. else {
  200. if (new_speed >= 2000)
  201. return fact * 2000;
  202. if (new_speed >= 1500)
  203. return fact * 1500;
  204. if (new_speed >= 1000)
  205. return fact * 1000;
  206. }
  207. if (new_speed == 0)
  208. return 1000;
  209. if (new_speed == 1 || new_speed == 1000)
  210. return fact * new_speed;
  211. s = new_speed;
  212. new_speed = 1000 / new_speed;
  213. if (1000 / cur_speed == new_speed)
  214. new_speed += (cur_speed < s) ? -1 : 1;
  215. if (new_speed > 60) return 1000 / (fact * 60);
  216. return 1000 / (fact * new_speed);
  217. }
  218. static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
  219. struct video_command *vc, int try)
  220. {
  221. struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
  222. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  223. return -EINVAL;
  224. switch (vc->cmd) {
  225. case VIDEO_CMD_PLAY: {
  226. vc->flags = 0;
  227. vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
  228. if (vc->play.speed < 0)
  229. vc->play.format = VIDEO_PLAY_FMT_GOP;
  230. if (try) break;
  231. if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
  232. return -EBUSY;
  233. if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
  234. /* forces ivtv_set_speed to be called */
  235. itv->speed = 0;
  236. }
  237. return ivtv_start_decoding(id, vc->play.speed);
  238. }
  239. case VIDEO_CMD_STOP:
  240. vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
  241. if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
  242. vc->stop.pts = 0;
  243. if (try) break;
  244. if (atomic_read(&itv->decoding) == 0)
  245. return 0;
  246. if (itv->output_mode != OUT_MPG)
  247. return -EBUSY;
  248. itv->output_mode = OUT_NONE;
  249. return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
  250. case VIDEO_CMD_FREEZE:
  251. vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
  252. if (try) break;
  253. if (itv->output_mode != OUT_MPG)
  254. return -EBUSY;
  255. if (atomic_read(&itv->decoding) > 0) {
  256. ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
  257. (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
  258. set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
  259. }
  260. break;
  261. case VIDEO_CMD_CONTINUE:
  262. vc->flags = 0;
  263. if (try) break;
  264. if (itv->output_mode != OUT_MPG)
  265. return -EBUSY;
  266. if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
  267. int speed = itv->speed;
  268. itv->speed = 0;
  269. return ivtv_start_decoding(id, speed);
  270. }
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. return 0;
  276. }
  277. static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
  278. {
  279. struct v4l2_register *regs = arg;
  280. unsigned long flags;
  281. volatile u8 __iomem *reg_start;
  282. if (!capable(CAP_SYS_ADMIN))
  283. return -EPERM;
  284. if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
  285. reg_start = itv->reg_mem - IVTV_REG_OFFSET;
  286. else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
  287. regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
  288. reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
  289. else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
  290. reg_start = itv->enc_mem;
  291. else
  292. return -EINVAL;
  293. spin_lock_irqsave(&ivtv_cards_lock, flags);
  294. if (cmd == VIDIOC_DBG_G_REGISTER) {
  295. regs->val = readl(regs->reg + reg_start);
  296. } else {
  297. writel(regs->val, regs->reg + reg_start);
  298. }
  299. spin_unlock_irqrestore(&ivtv_cards_lock, flags);
  300. return 0;
  301. }
  302. static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  303. {
  304. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  305. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  306. vbifmt->reserved[0] = 0;
  307. vbifmt->reserved[1] = 0;
  308. if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
  309. return -EINVAL;
  310. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  311. if (itv->is_60hz) {
  312. vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
  313. vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
  314. } else {
  315. vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
  316. vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
  317. }
  318. vbifmt->service_set = ivtv_get_service_set(vbifmt);
  319. return 0;
  320. }
  321. static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  322. {
  323. struct ivtv_open_id *id = fh;
  324. struct ivtv *itv = id->itv;
  325. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  326. pixfmt->width = itv->params.width;
  327. pixfmt->height = itv->params.height;
  328. pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  329. pixfmt->field = V4L2_FIELD_INTERLACED;
  330. pixfmt->priv = 0;
  331. if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
  332. pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
  333. /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
  334. pixfmt->sizeimage =
  335. pixfmt->height * pixfmt->width +
  336. pixfmt->height * (pixfmt->width / 2);
  337. pixfmt->bytesperline = 720;
  338. } else {
  339. pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
  340. pixfmt->sizeimage = 128 * 1024;
  341. pixfmt->bytesperline = 0;
  342. }
  343. return 0;
  344. }
  345. static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  346. {
  347. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  348. struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
  349. vbifmt->sampling_rate = 27000000;
  350. vbifmt->offset = 248;
  351. vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
  352. vbifmt->sample_format = V4L2_PIX_FMT_GREY;
  353. vbifmt->start[0] = itv->vbi.start[0];
  354. vbifmt->start[1] = itv->vbi.start[1];
  355. vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
  356. vbifmt->flags = 0;
  357. vbifmt->reserved[0] = 0;
  358. vbifmt->reserved[1] = 0;
  359. return 0;
  360. }
  361. static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  362. {
  363. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  364. struct ivtv_open_id *id = fh;
  365. struct ivtv *itv = id->itv;
  366. vbifmt->reserved[0] = 0;
  367. vbifmt->reserved[1] = 0;
  368. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  369. if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
  370. vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
  371. V4L2_SLICED_VBI_525;
  372. ivtv_expand_service_set(vbifmt, itv->is_50hz);
  373. return 0;
  374. }
  375. itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
  376. vbifmt->service_set = ivtv_get_service_set(vbifmt);
  377. return 0;
  378. }
  379. static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
  380. {
  381. struct ivtv_open_id *id = fh;
  382. struct ivtv *itv = id->itv;
  383. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  384. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  385. return -EINVAL;
  386. pixfmt->width = itv->main_rect.width;
  387. pixfmt->height = itv->main_rect.height;
  388. pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  389. pixfmt->field = V4L2_FIELD_INTERLACED;
  390. pixfmt->priv = 0;
  391. if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
  392. switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
  393. case IVTV_YUV_MODE_INTERLACED:
  394. pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
  395. V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
  396. break;
  397. case IVTV_YUV_MODE_PROGRESSIVE:
  398. pixfmt->field = V4L2_FIELD_NONE;
  399. break;
  400. default:
  401. pixfmt->field = V4L2_FIELD_ANY;
  402. break;
  403. }
  404. pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
  405. pixfmt->bytesperline = 720;
  406. pixfmt->width = itv->yuv_info.v4l2_src_w;
  407. pixfmt->height = itv->yuv_info.v4l2_src_h;
  408. /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
  409. pixfmt->sizeimage =
  410. 1080 * ((pixfmt->height + 31) & ~31);
  411. } else {
  412. pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
  413. pixfmt->sizeimage = 128 * 1024;
  414. pixfmt->bytesperline = 0;
  415. }
  416. return 0;
  417. }
  418. static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
  419. {
  420. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  421. struct v4l2_window *winfmt = &fmt->fmt.win;
  422. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  423. return -EINVAL;
  424. winfmt->chromakey = itv->osd_chroma_key;
  425. winfmt->global_alpha = itv->osd_global_alpha;
  426. winfmt->field = V4L2_FIELD_INTERLACED;
  427. winfmt->clips = NULL;
  428. winfmt->clipcount = 0;
  429. winfmt->bitmap = NULL;
  430. winfmt->w.top = winfmt->w.left = 0;
  431. winfmt->w.width = itv->osd_rect.width;
  432. winfmt->w.height = itv->osd_rect.height;
  433. return 0;
  434. }
  435. static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  436. {
  437. return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
  438. }
  439. static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  440. {
  441. struct ivtv_open_id *id = fh;
  442. struct ivtv *itv = id->itv;
  443. int w = fmt->fmt.pix.width;
  444. int h = fmt->fmt.pix.height;
  445. w = min(w, 720);
  446. w = max(w, 1);
  447. h = min(h, itv->is_50hz ? 576 : 480);
  448. h = max(h, 2);
  449. ivtv_g_fmt_vid_cap(file, fh, fmt);
  450. fmt->fmt.pix.width = w;
  451. fmt->fmt.pix.height = h;
  452. return 0;
  453. }
  454. static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  455. {
  456. return ivtv_g_fmt_vbi_cap(file, fh, fmt);
  457. }
  458. static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  459. {
  460. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  461. struct ivtv_open_id *id = fh;
  462. struct ivtv *itv = id->itv;
  463. if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
  464. return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
  465. /* set sliced VBI capture format */
  466. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  467. vbifmt->reserved[0] = 0;
  468. vbifmt->reserved[1] = 0;
  469. if (vbifmt->service_set)
  470. ivtv_expand_service_set(vbifmt, itv->is_50hz);
  471. check_service_set(vbifmt, itv->is_50hz);
  472. vbifmt->service_set = ivtv_get_service_set(vbifmt);
  473. return 0;
  474. }
  475. static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
  476. {
  477. struct ivtv_open_id *id = fh;
  478. s32 w, h;
  479. int field;
  480. int ret;
  481. w = fmt->fmt.pix.width;
  482. h = fmt->fmt.pix.height;
  483. field = fmt->fmt.pix.field;
  484. ret = ivtv_g_fmt_vid_out(file, fh, fmt);
  485. fmt->fmt.pix.width = w;
  486. fmt->fmt.pix.height = h;
  487. if (!ret && id->type == IVTV_DEC_STREAM_TYPE_YUV) {
  488. fmt->fmt.pix.field = field;
  489. if (fmt->fmt.pix.width < 2)
  490. fmt->fmt.pix.width = 2;
  491. if (fmt->fmt.pix.width > 720)
  492. fmt->fmt.pix.width = 720;
  493. if (fmt->fmt.pix.height < 2)
  494. fmt->fmt.pix.height = 2;
  495. if (fmt->fmt.pix.height > 576)
  496. fmt->fmt.pix.height = 576;
  497. }
  498. return ret;
  499. }
  500. static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
  501. {
  502. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  503. u32 chromakey = fmt->fmt.win.chromakey;
  504. u8 global_alpha = fmt->fmt.win.global_alpha;
  505. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  506. return -EINVAL;
  507. ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
  508. fmt->fmt.win.chromakey = chromakey;
  509. fmt->fmt.win.global_alpha = global_alpha;
  510. return 0;
  511. }
  512. static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
  513. {
  514. return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
  515. }
  516. static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  517. {
  518. struct ivtv_open_id *id = fh;
  519. struct ivtv *itv = id->itv;
  520. struct cx2341x_mpeg_params *p = &itv->params;
  521. int w = fmt->fmt.pix.width;
  522. int h = fmt->fmt.pix.height;
  523. int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
  524. if (ret)
  525. return ret;
  526. if (p->width == w && p->height == h)
  527. return 0;
  528. if (atomic_read(&itv->capturing) > 0)
  529. return -EBUSY;
  530. p->width = w;
  531. p->height = h;
  532. if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
  533. fmt->fmt.pix.width /= 2;
  534. itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
  535. return ivtv_g_fmt_vid_cap(file, fh, fmt);
  536. }
  537. static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  538. {
  539. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  540. itv->vbi.sliced_in->service_set = 0;
  541. itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
  542. return ivtv_g_fmt_vbi_cap(file, fh, fmt);
  543. }
  544. static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  545. {
  546. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  547. struct ivtv_open_id *id = fh;
  548. struct ivtv *itv = id->itv;
  549. int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
  550. if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
  551. return ret;
  552. if (check_service_set(vbifmt, itv->is_50hz) == 0)
  553. return -EINVAL;
  554. if (atomic_read(&itv->capturing) > 0)
  555. return -EBUSY;
  556. itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
  557. memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
  558. return 0;
  559. }
  560. static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
  561. {
  562. struct ivtv_open_id *id = fh;
  563. struct ivtv *itv = id->itv;
  564. struct yuv_playback_info *yi = &itv->yuv_info;
  565. int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
  566. if (ret)
  567. return ret;
  568. if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
  569. return 0;
  570. /* Return now if we already have some frame data */
  571. if (yi->stream_size)
  572. return -EBUSY;
  573. yi->v4l2_src_w = fmt->fmt.pix.width;
  574. yi->v4l2_src_h = fmt->fmt.pix.height;
  575. switch (fmt->fmt.pix.field) {
  576. case V4L2_FIELD_NONE:
  577. yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
  578. break;
  579. case V4L2_FIELD_ANY:
  580. yi->lace_mode = IVTV_YUV_MODE_AUTO;
  581. break;
  582. case V4L2_FIELD_INTERLACED_BT:
  583. yi->lace_mode =
  584. IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
  585. break;
  586. case V4L2_FIELD_INTERLACED_TB:
  587. default:
  588. yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
  589. break;
  590. }
  591. yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
  592. if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
  593. itv->dma_data_req_size =
  594. 1080 * ((yi->v4l2_src_h + 31) & ~31);
  595. /* Force update of yuv registers */
  596. yi->yuv_forced_update = 1;
  597. return 0;
  598. }
  599. static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
  600. {
  601. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  602. int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
  603. if (ret == 0) {
  604. itv->osd_chroma_key = fmt->fmt.win.chromakey;
  605. itv->osd_global_alpha = fmt->fmt.win.global_alpha;
  606. ivtv_set_osd_alpha(itv);
  607. }
  608. return ret;
  609. }
  610. static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_chip_ident *chip)
  611. {
  612. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  613. chip->ident = V4L2_IDENT_NONE;
  614. chip->revision = 0;
  615. if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
  616. if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
  617. chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
  618. return 0;
  619. }
  620. if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  621. return ivtv_i2c_id(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
  622. if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
  623. return ivtv_call_i2c_client(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
  624. return -EINVAL;
  625. }
  626. static int ivtv_g_register(struct file *file, void *fh, struct v4l2_register *reg)
  627. {
  628. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  629. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  630. return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
  631. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  632. return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
  633. return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
  634. }
  635. static int ivtv_s_register(struct file *file, void *fh, struct v4l2_register *reg)
  636. {
  637. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  638. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  639. return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
  640. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  641. return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
  642. return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
  643. }
  644. static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
  645. {
  646. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  647. *p = v4l2_prio_max(&itv->prio);
  648. return 0;
  649. }
  650. static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
  651. {
  652. struct ivtv_open_id *id = fh;
  653. struct ivtv *itv = id->itv;
  654. return v4l2_prio_change(&itv->prio, &id->prio, prio);
  655. }
  656. static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
  657. {
  658. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  659. strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
  660. strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
  661. strlcpy(vcap->bus_info, pci_name(itv->dev), sizeof(vcap->bus_info));
  662. vcap->version = IVTV_DRIVER_VERSION; /* version */
  663. vcap->capabilities = itv->v4l2_cap; /* capabilities */
  664. return 0;
  665. }
  666. static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
  667. {
  668. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  669. return ivtv_get_audio_input(itv, vin->index, vin);
  670. }
  671. static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
  672. {
  673. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  674. vin->index = itv->audio_input;
  675. return ivtv_get_audio_input(itv, vin->index, vin);
  676. }
  677. static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
  678. {
  679. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  680. if (vout->index >= itv->nof_audio_inputs)
  681. return -EINVAL;
  682. itv->audio_input = vout->index;
  683. ivtv_audio_set_io(itv);
  684. return 0;
  685. }
  686. static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
  687. {
  688. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  689. /* set it to defaults from our table */
  690. return ivtv_get_audio_output(itv, vin->index, vin);
  691. }
  692. static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
  693. {
  694. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  695. vin->index = 0;
  696. return ivtv_get_audio_output(itv, vin->index, vin);
  697. }
  698. static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
  699. {
  700. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  701. return ivtv_get_audio_output(itv, vout->index, vout);
  702. }
  703. static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  704. {
  705. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  706. /* set it to defaults from our table */
  707. return ivtv_get_input(itv, vin->index, vin);
  708. }
  709. static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
  710. {
  711. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  712. return ivtv_get_output(itv, vout->index, vout);
  713. }
  714. static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
  715. {
  716. struct ivtv_open_id *id = fh;
  717. struct ivtv *itv = id->itv;
  718. struct yuv_playback_info *yi = &itv->yuv_info;
  719. int streamtype;
  720. streamtype = id->type;
  721. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  722. return -EINVAL;
  723. cropcap->bounds.top = cropcap->bounds.left = 0;
  724. cropcap->bounds.width = 720;
  725. if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  726. cropcap->bounds.height = itv->is_50hz ? 576 : 480;
  727. cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
  728. cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
  729. } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
  730. if (yi->track_osd) {
  731. cropcap->bounds.width = yi->osd_full_w;
  732. cropcap->bounds.height = yi->osd_full_h;
  733. } else {
  734. cropcap->bounds.width = 720;
  735. cropcap->bounds.height =
  736. itv->is_out_50hz ? 576 : 480;
  737. }
  738. cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
  739. cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
  740. } else {
  741. cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
  742. cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
  743. cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
  744. }
  745. cropcap->defrect = cropcap->bounds;
  746. return 0;
  747. }
  748. static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  749. {
  750. struct ivtv_open_id *id = fh;
  751. struct ivtv *itv = id->itv;
  752. struct yuv_playback_info *yi = &itv->yuv_info;
  753. int streamtype;
  754. streamtype = id->type;
  755. if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
  756. printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
  757. /* Should be replaced */
  758. /* v4l_printk_ioctl(VIDIOC_S_CROP); */
  759. }
  760. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  761. (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
  762. if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
  763. yi->main_rect = crop->c;
  764. return 0;
  765. } else {
  766. if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
  767. crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
  768. itv->main_rect = crop->c;
  769. return 0;
  770. }
  771. }
  772. return -EINVAL;
  773. }
  774. return -EINVAL;
  775. }
  776. static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  777. {
  778. struct ivtv_open_id *id = fh;
  779. struct ivtv *itv = id->itv;
  780. struct yuv_playback_info *yi = &itv->yuv_info;
  781. int streamtype;
  782. streamtype = id->type;
  783. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  784. (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
  785. if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
  786. crop->c = yi->main_rect;
  787. else
  788. crop->c = itv->main_rect;
  789. return 0;
  790. }
  791. return -EINVAL;
  792. }
  793. static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  794. {
  795. static struct v4l2_fmtdesc formats[] = {
  796. { 0, 0, 0,
  797. "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
  798. { 0, 0, 0, 0 }
  799. },
  800. { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
  801. "MPEG", V4L2_PIX_FMT_MPEG,
  802. { 0, 0, 0, 0 }
  803. }
  804. };
  805. enum v4l2_buf_type type = fmt->type;
  806. if (fmt->index > 1)
  807. return -EINVAL;
  808. *fmt = formats[fmt->index];
  809. fmt->type = type;
  810. return 0;
  811. }
  812. static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  813. {
  814. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  815. static struct v4l2_fmtdesc formats[] = {
  816. { 0, 0, 0,
  817. "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
  818. { 0, 0, 0, 0 }
  819. },
  820. { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
  821. "MPEG", V4L2_PIX_FMT_MPEG,
  822. { 0, 0, 0, 0 }
  823. }
  824. };
  825. enum v4l2_buf_type type = fmt->type;
  826. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  827. return -EINVAL;
  828. if (fmt->index > 1)
  829. return -EINVAL;
  830. *fmt = formats[fmt->index];
  831. fmt->type = type;
  832. return 0;
  833. }
  834. static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
  835. {
  836. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  837. *i = itv->active_input;
  838. return 0;
  839. }
  840. int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
  841. {
  842. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  843. if (inp < 0 || inp >= itv->nof_inputs)
  844. return -EINVAL;
  845. if (inp == itv->active_input) {
  846. IVTV_DEBUG_INFO("Input unchanged\n");
  847. return 0;
  848. }
  849. if (atomic_read(&itv->capturing) > 0) {
  850. return -EBUSY;
  851. }
  852. IVTV_DEBUG_INFO("Changing input from %d to %d\n",
  853. itv->active_input, inp);
  854. itv->active_input = inp;
  855. /* Set the audio input to whatever is appropriate for the
  856. input type. */
  857. itv->audio_input = itv->card->video_inputs[inp].audio_index;
  858. /* prevent others from messing with the streams until
  859. we're finished changing inputs. */
  860. ivtv_mute(itv);
  861. ivtv_video_set_io(itv);
  862. ivtv_audio_set_io(itv);
  863. ivtv_unmute(itv);
  864. return 0;
  865. }
  866. static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
  867. {
  868. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  869. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  870. return -EINVAL;
  871. *i = itv->active_output;
  872. return 0;
  873. }
  874. static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
  875. {
  876. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  877. struct v4l2_routing route;
  878. if (outp >= itv->card->nof_outputs)
  879. return -EINVAL;
  880. if (outp == itv->active_output) {
  881. IVTV_DEBUG_INFO("Output unchanged\n");
  882. return 0;
  883. }
  884. IVTV_DEBUG_INFO("Changing output from %d to %d\n",
  885. itv->active_output, outp);
  886. itv->active_output = outp;
  887. route.input = SAA7127_INPUT_TYPE_NORMAL;
  888. route.output = itv->card->video_outputs[outp].video_output;
  889. ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  890. return 0;
  891. }
  892. static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  893. {
  894. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  895. if (vf->tuner != 0)
  896. return -EINVAL;
  897. ivtv_call_i2c_clients(itv, VIDIOC_G_FREQUENCY, vf);
  898. return 0;
  899. }
  900. int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  901. {
  902. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  903. if (vf->tuner != 0)
  904. return -EINVAL;
  905. ivtv_mute(itv);
  906. IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
  907. ivtv_call_i2c_clients(itv, VIDIOC_S_FREQUENCY, vf);
  908. ivtv_unmute(itv);
  909. return 0;
  910. }
  911. static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
  912. {
  913. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  914. *std = itv->std;
  915. return 0;
  916. }
  917. int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
  918. {
  919. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  920. struct yuv_playback_info *yi = &itv->yuv_info;
  921. if ((*std & V4L2_STD_ALL) == 0)
  922. return -EINVAL;
  923. if (*std == itv->std)
  924. return 0;
  925. if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
  926. atomic_read(&itv->capturing) > 0 ||
  927. atomic_read(&itv->decoding) > 0) {
  928. /* Switching standard would turn off the radio or mess
  929. with already running streams, prevent that by
  930. returning EBUSY. */
  931. return -EBUSY;
  932. }
  933. itv->std = *std;
  934. itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
  935. itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
  936. itv->params.width = 720;
  937. itv->params.height = itv->is_50hz ? 576 : 480;
  938. itv->vbi.count = itv->is_50hz ? 18 : 12;
  939. itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
  940. itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
  941. if (itv->hw_flags & IVTV_HW_CX25840)
  942. itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
  943. IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
  944. /* Tuner */
  945. ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
  946. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  947. /* set display standard */
  948. itv->std_out = *std;
  949. itv->is_out_60hz = itv->is_60hz;
  950. itv->is_out_50hz = itv->is_50hz;
  951. ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
  952. ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
  953. itv->main_rect.left = itv->main_rect.top = 0;
  954. itv->main_rect.width = 720;
  955. itv->main_rect.height = itv->params.height;
  956. ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
  957. 720, itv->main_rect.height, 0, 0);
  958. yi->main_rect = itv->main_rect;
  959. if (!itv->osd_info) {
  960. yi->osd_full_w = 720;
  961. yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
  962. }
  963. }
  964. return 0;
  965. }
  966. static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  967. {
  968. struct ivtv_open_id *id = fh;
  969. struct ivtv *itv = id->itv;
  970. if (vt->index != 0)
  971. return -EINVAL;
  972. ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
  973. return 0;
  974. }
  975. static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  976. {
  977. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  978. if (vt->index != 0)
  979. return -EINVAL;
  980. ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
  981. if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
  982. strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
  983. vt->type = V4L2_TUNER_RADIO;
  984. } else {
  985. strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
  986. vt->type = V4L2_TUNER_ANALOG_TV;
  987. }
  988. return 0;
  989. }
  990. static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
  991. {
  992. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  993. int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
  994. int f, l;
  995. if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  996. for (f = 0; f < 2; f++) {
  997. for (l = 0; l < 24; l++) {
  998. if (valid_service_line(f, l, itv->is_50hz))
  999. cap->service_lines[f][l] = set;
  1000. }
  1001. }
  1002. return 0;
  1003. }
  1004. if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  1005. if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
  1006. return -EINVAL;
  1007. if (itv->is_60hz) {
  1008. cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
  1009. cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
  1010. } else {
  1011. cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
  1012. cap->service_lines[0][16] = V4L2_SLICED_VPS;
  1013. }
  1014. return 0;
  1015. }
  1016. return -EINVAL;
  1017. }
  1018. static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
  1019. {
  1020. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1021. struct v4l2_enc_idx_entry *e = idx->entry;
  1022. int entries;
  1023. int i;
  1024. entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
  1025. IVTV_MAX_PGM_INDEX;
  1026. if (entries > V4L2_ENC_IDX_ENTRIES)
  1027. entries = V4L2_ENC_IDX_ENTRIES;
  1028. idx->entries = 0;
  1029. for (i = 0; i < entries; i++) {
  1030. *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
  1031. if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
  1032. idx->entries++;
  1033. e++;
  1034. }
  1035. }
  1036. itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
  1037. return 0;
  1038. }
  1039. static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
  1040. {
  1041. struct ivtv_open_id *id = fh;
  1042. struct ivtv *itv = id->itv;
  1043. switch (enc->cmd) {
  1044. case V4L2_ENC_CMD_START:
  1045. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  1046. enc->flags = 0;
  1047. return ivtv_start_capture(id);
  1048. case V4L2_ENC_CMD_STOP:
  1049. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  1050. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  1051. ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
  1052. return 0;
  1053. case V4L2_ENC_CMD_PAUSE:
  1054. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  1055. enc->flags = 0;
  1056. if (!atomic_read(&itv->capturing))
  1057. return -EPERM;
  1058. if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
  1059. return 0;
  1060. ivtv_mute(itv);
  1061. ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
  1062. break;
  1063. case V4L2_ENC_CMD_RESUME:
  1064. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  1065. enc->flags = 0;
  1066. if (!atomic_read(&itv->capturing))
  1067. return -EPERM;
  1068. if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
  1069. return 0;
  1070. ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
  1071. ivtv_unmute(itv);
  1072. break;
  1073. default:
  1074. IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  1075. return -EINVAL;
  1076. }
  1077. return 0;
  1078. }
  1079. static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
  1080. {
  1081. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1082. switch (enc->cmd) {
  1083. case V4L2_ENC_CMD_START:
  1084. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  1085. enc->flags = 0;
  1086. return 0;
  1087. case V4L2_ENC_CMD_STOP:
  1088. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  1089. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  1090. return 0;
  1091. case V4L2_ENC_CMD_PAUSE:
  1092. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  1093. enc->flags = 0;
  1094. return 0;
  1095. case V4L2_ENC_CMD_RESUME:
  1096. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  1097. enc->flags = 0;
  1098. return 0;
  1099. default:
  1100. IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  1101. return -EINVAL;
  1102. }
  1103. }
  1104. static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  1105. {
  1106. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1107. u32 data[CX2341X_MBOX_MAX_DATA];
  1108. struct yuv_playback_info *yi = &itv->yuv_info;
  1109. int pixfmt;
  1110. static u32 pixel_format[16] = {
  1111. V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
  1112. V4L2_PIX_FMT_RGB565,
  1113. V4L2_PIX_FMT_RGB555,
  1114. V4L2_PIX_FMT_RGB444,
  1115. V4L2_PIX_FMT_RGB32,
  1116. 0,
  1117. 0,
  1118. 0,
  1119. V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
  1120. V4L2_PIX_FMT_YUV565,
  1121. V4L2_PIX_FMT_YUV555,
  1122. V4L2_PIX_FMT_YUV444,
  1123. V4L2_PIX_FMT_YUV32,
  1124. 0,
  1125. 0,
  1126. 0,
  1127. };
  1128. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
  1129. return -EINVAL;
  1130. fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
  1131. V4L2_FBUF_CAP_GLOBAL_ALPHA;
  1132. ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
  1133. data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
  1134. pixfmt = (data[0] >> 3) & 0xf;
  1135. fb->fmt.pixelformat = pixel_format[pixfmt];
  1136. fb->fmt.width = itv->osd_rect.width;
  1137. fb->fmt.height = itv->osd_rect.height;
  1138. fb->fmt.field = V4L2_FIELD_INTERLACED;
  1139. fb->fmt.bytesperline = fb->fmt.width;
  1140. if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
  1141. fb->fmt.bytesperline *= 2;
  1142. if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
  1143. fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
  1144. fb->fmt.bytesperline *= 2;
  1145. fb->base = (void *)itv->osd_video_pbase;
  1146. fb->flags = 0;
  1147. if (itv->osd_chroma_key_state)
  1148. fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1149. if (itv->osd_global_alpha_state)
  1150. fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
  1151. pixfmt &= 7;
  1152. /* no local alpha for RGB565 or unknown formats */
  1153. if (pixfmt == 1 || pixfmt > 4)
  1154. return 0;
  1155. /* 16-bit formats have inverted local alpha */
  1156. if (pixfmt == 2 || pixfmt == 3)
  1157. fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
  1158. else
  1159. fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
  1160. if (itv->osd_local_alpha_state) {
  1161. /* 16-bit formats have inverted local alpha */
  1162. if (pixfmt == 2 || pixfmt == 3)
  1163. fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
  1164. else
  1165. fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1166. }
  1167. if (yi->track_osd)
  1168. fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
  1169. return 0;
  1170. }
  1171. static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  1172. {
  1173. struct ivtv_open_id *id = fh;
  1174. struct ivtv *itv = id->itv;
  1175. struct yuv_playback_info *yi = &itv->yuv_info;
  1176. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
  1177. return -EINVAL;
  1178. itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
  1179. itv->osd_local_alpha_state =
  1180. (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
  1181. itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
  1182. ivtv_set_osd_alpha(itv);
  1183. yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
  1184. return ivtv_g_fbuf(file, fh, fb);
  1185. }
  1186. static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
  1187. {
  1188. struct ivtv_open_id *id = fh;
  1189. struct ivtv *itv = id->itv;
  1190. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
  1191. return -EINVAL;
  1192. ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
  1193. return 0;
  1194. }
  1195. static int ivtv_log_status(struct file *file, void *fh)
  1196. {
  1197. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1198. u32 data[CX2341X_MBOX_MAX_DATA];
  1199. int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
  1200. struct v4l2_input vidin;
  1201. struct v4l2_audio audin;
  1202. int i;
  1203. IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
  1204. IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
  1205. if (itv->hw_flags & IVTV_HW_TVEEPROM) {
  1206. struct tveeprom tv;
  1207. ivtv_read_eeprom(itv, &tv);
  1208. }
  1209. ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
  1210. ivtv_get_input(itv, itv->active_input, &vidin);
  1211. ivtv_get_audio_input(itv, itv->audio_input, &audin);
  1212. IVTV_INFO("Video Input: %s\n", vidin.name);
  1213. IVTV_INFO("Audio Input: %s%s\n", audin.name,
  1214. (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
  1215. if (has_output) {
  1216. struct v4l2_output vidout;
  1217. struct v4l2_audioout audout;
  1218. int mode = itv->output_mode;
  1219. static const char * const output_modes[5] = {
  1220. "None",
  1221. "MPEG Streaming",
  1222. "YUV Streaming",
  1223. "YUV Frames",
  1224. "Passthrough",
  1225. };
  1226. static const char * const audio_modes[5] = {
  1227. "Stereo",
  1228. "Left",
  1229. "Right",
  1230. "Mono",
  1231. "Swapped"
  1232. };
  1233. static const char * const alpha_mode[4] = {
  1234. "None",
  1235. "Global",
  1236. "Local",
  1237. "Global and Local"
  1238. };
  1239. static const char * const pixel_format[16] = {
  1240. "ARGB Indexed",
  1241. "RGB 5:6:5",
  1242. "ARGB 1:5:5:5",
  1243. "ARGB 1:4:4:4",
  1244. "ARGB 8:8:8:8",
  1245. "5",
  1246. "6",
  1247. "7",
  1248. "AYUV Indexed",
  1249. "YUV 5:6:5",
  1250. "AYUV 1:5:5:5",
  1251. "AYUV 1:4:4:4",
  1252. "AYUV 8:8:8:8",
  1253. "13",
  1254. "14",
  1255. "15",
  1256. };
  1257. ivtv_get_output(itv, itv->active_output, &vidout);
  1258. ivtv_get_audio_output(itv, 0, &audout);
  1259. IVTV_INFO("Video Output: %s\n", vidout.name);
  1260. IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
  1261. audio_modes[itv->audio_stereo_mode],
  1262. audio_modes[itv->audio_bilingual_mode]);
  1263. if (mode < 0 || mode > OUT_PASSTHROUGH)
  1264. mode = OUT_NONE;
  1265. IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
  1266. ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
  1267. data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
  1268. IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
  1269. data[0] & 1 ? "On" : "Off",
  1270. alpha_mode[(data[0] >> 1) & 0x3],
  1271. pixel_format[(data[0] >> 3) & 0xf]);
  1272. }
  1273. IVTV_INFO("Tuner: %s\n",
  1274. test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
  1275. cx2341x_log_status(&itv->params, itv->name);
  1276. IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
  1277. for (i = 0; i < IVTV_MAX_STREAMS; i++) {
  1278. struct ivtv_stream *s = &itv->streams[i];
  1279. if (s->v4l2dev == NULL || s->buffers == 0)
  1280. continue;
  1281. IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
  1282. (s->buffers - s->q_free.buffers) * 100 / s->buffers,
  1283. (s->buffers * s->buf_size) / 1024, s->buffers);
  1284. }
  1285. IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
  1286. IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
  1287. return 0;
  1288. }
  1289. static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
  1290. {
  1291. struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
  1292. struct ivtv *itv = id->itv;
  1293. int nonblocking = filp->f_flags & O_NONBLOCK;
  1294. struct ivtv_stream *s = &itv->streams[id->type];
  1295. switch (cmd) {
  1296. case IVTV_IOC_DMA_FRAME: {
  1297. struct ivtv_dma_frame *args = arg;
  1298. IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
  1299. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1300. return -EINVAL;
  1301. if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1302. return -EINVAL;
  1303. if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
  1304. return 0;
  1305. if (ivtv_claim_stream(id, id->type)) {
  1306. return -EBUSY;
  1307. }
  1308. if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
  1309. ivtv_release_stream(s);
  1310. return -EBUSY;
  1311. }
  1312. /* Mark that this file handle started the UDMA_YUV mode */
  1313. id->yuv_frames = 1;
  1314. if (args->y_source == NULL)
  1315. return 0;
  1316. return ivtv_yuv_prep_frame(itv, args);
  1317. }
  1318. case VIDEO_GET_PTS: {
  1319. u32 data[CX2341X_MBOX_MAX_DATA];
  1320. u64 *pts = arg;
  1321. IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
  1322. if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
  1323. *pts = s->dma_pts;
  1324. break;
  1325. }
  1326. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1327. return -EINVAL;
  1328. if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
  1329. *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
  1330. (u64)itv->last_dec_timing[1];
  1331. break;
  1332. }
  1333. *pts = 0;
  1334. if (atomic_read(&itv->decoding)) {
  1335. if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
  1336. IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
  1337. return -EIO;
  1338. }
  1339. memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
  1340. set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
  1341. *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
  1342. /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
  1343. }
  1344. break;
  1345. }
  1346. case VIDEO_GET_FRAME_COUNT: {
  1347. u32 data[CX2341X_MBOX_MAX_DATA];
  1348. u64 *frame = arg;
  1349. IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
  1350. if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
  1351. *frame = 0;
  1352. break;
  1353. }
  1354. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1355. return -EINVAL;
  1356. if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
  1357. *frame = itv->last_dec_timing[0];
  1358. break;
  1359. }
  1360. *frame = 0;
  1361. if (atomic_read(&itv->decoding)) {
  1362. if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
  1363. IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
  1364. return -EIO;
  1365. }
  1366. memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
  1367. set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
  1368. *frame = data[0];
  1369. }
  1370. break;
  1371. }
  1372. case VIDEO_PLAY: {
  1373. struct video_command vc;
  1374. IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
  1375. memset(&vc, 0, sizeof(vc));
  1376. vc.cmd = VIDEO_CMD_PLAY;
  1377. return ivtv_video_command(itv, id, &vc, 0);
  1378. }
  1379. case VIDEO_STOP: {
  1380. struct video_command vc;
  1381. IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
  1382. memset(&vc, 0, sizeof(vc));
  1383. vc.cmd = VIDEO_CMD_STOP;
  1384. vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
  1385. return ivtv_video_command(itv, id, &vc, 0);
  1386. }
  1387. case VIDEO_FREEZE: {
  1388. struct video_command vc;
  1389. IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
  1390. memset(&vc, 0, sizeof(vc));
  1391. vc.cmd = VIDEO_CMD_FREEZE;
  1392. return ivtv_video_command(itv, id, &vc, 0);
  1393. }
  1394. case VIDEO_CONTINUE: {
  1395. struct video_command vc;
  1396. IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
  1397. memset(&vc, 0, sizeof(vc));
  1398. vc.cmd = VIDEO_CMD_CONTINUE;
  1399. return ivtv_video_command(itv, id, &vc, 0);
  1400. }
  1401. case VIDEO_COMMAND:
  1402. case VIDEO_TRY_COMMAND: {
  1403. struct video_command *vc = arg;
  1404. int try = (cmd == VIDEO_TRY_COMMAND);
  1405. if (try)
  1406. IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
  1407. else
  1408. IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
  1409. return ivtv_video_command(itv, id, vc, try);
  1410. }
  1411. case VIDEO_GET_EVENT: {
  1412. struct video_event *ev = arg;
  1413. DEFINE_WAIT(wait);
  1414. IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
  1415. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1416. return -EINVAL;
  1417. memset(ev, 0, sizeof(*ev));
  1418. set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
  1419. while (1) {
  1420. if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
  1421. ev->type = VIDEO_EVENT_DECODER_STOPPED;
  1422. else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
  1423. ev->type = VIDEO_EVENT_VSYNC;
  1424. ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
  1425. VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
  1426. if (itv->output_mode == OUT_UDMA_YUV &&
  1427. (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
  1428. IVTV_YUV_MODE_PROGRESSIVE) {
  1429. ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
  1430. }
  1431. }
  1432. if (ev->type)
  1433. return 0;
  1434. if (nonblocking)
  1435. return -EAGAIN;
  1436. /* Wait for event. Note that serialize_lock is locked,
  1437. so to allow other processes to access the driver while
  1438. we are waiting unlock first and later lock again. */
  1439. mutex_unlock(&itv->serialize_lock);
  1440. prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
  1441. if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
  1442. schedule();
  1443. finish_wait(&itv->event_waitq, &wait);
  1444. mutex_lock(&itv->serialize_lock);
  1445. if (signal_pending(current)) {
  1446. /* return if a signal was received */
  1447. IVTV_DEBUG_INFO("User stopped wait for event\n");
  1448. return -EINTR;
  1449. }
  1450. }
  1451. break;
  1452. }
  1453. default:
  1454. return -EINVAL;
  1455. }
  1456. return 0;
  1457. }
  1458. static int ivtv_default(struct file *file, void *fh, int cmd, void *arg)
  1459. {
  1460. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1461. switch (cmd) {
  1462. case VIDIOC_INT_S_AUDIO_ROUTING: {
  1463. struct v4l2_routing *route = arg;
  1464. ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
  1465. break;
  1466. }
  1467. case VIDIOC_INT_RESET: {
  1468. u32 val = *(u32 *)arg;
  1469. if ((val == 0 && itv->options.newi2c) || (val & 0x01))
  1470. ivtv_reset_ir_gpio(itv);
  1471. if (val & 0x02)
  1472. itv->video_dec_func(itv, cmd, NULL);
  1473. break;
  1474. }
  1475. default:
  1476. return -EINVAL;
  1477. }
  1478. return 0;
  1479. }
  1480. static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
  1481. unsigned int cmd, unsigned long arg)
  1482. {
  1483. struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
  1484. int ret;
  1485. /* Filter dvb ioctls that cannot be handled by the v4l ioctl framework */
  1486. switch (cmd) {
  1487. case VIDEO_SELECT_SOURCE:
  1488. IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
  1489. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1490. return -EINVAL;
  1491. return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
  1492. case AUDIO_SET_MUTE:
  1493. IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
  1494. itv->speed_mute_audio = arg;
  1495. return 0;
  1496. case AUDIO_CHANNEL_SELECT:
  1497. IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
  1498. if (arg > AUDIO_STEREO_SWAPPED)
  1499. return -EINVAL;
  1500. itv->audio_stereo_mode = arg;
  1501. ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
  1502. return 0;
  1503. case AUDIO_BILINGUAL_CHANNEL_SELECT:
  1504. IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
  1505. if (arg > AUDIO_STEREO_SWAPPED)
  1506. return -EINVAL;
  1507. itv->audio_bilingual_mode = arg;
  1508. ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
  1509. return 0;
  1510. case IVTV_IOC_DMA_FRAME:
  1511. case VIDEO_GET_PTS:
  1512. case VIDEO_GET_FRAME_COUNT:
  1513. case VIDEO_GET_EVENT:
  1514. case VIDEO_PLAY:
  1515. case VIDEO_STOP:
  1516. case VIDEO_FREEZE:
  1517. case VIDEO_CONTINUE:
  1518. case VIDEO_COMMAND:
  1519. case VIDEO_TRY_COMMAND:
  1520. return ivtv_decoder_ioctls(filp, cmd, (void *)arg);
  1521. default:
  1522. break;
  1523. }
  1524. /* check priority */
  1525. switch (cmd) {
  1526. case VIDIOC_S_CTRL:
  1527. case VIDIOC_S_STD:
  1528. case VIDIOC_S_INPUT:
  1529. case VIDIOC_S_OUTPUT:
  1530. case VIDIOC_S_TUNER:
  1531. case VIDIOC_S_FREQUENCY:
  1532. case VIDIOC_S_FMT:
  1533. case VIDIOC_S_CROP:
  1534. case VIDIOC_S_AUDIO:
  1535. case VIDIOC_S_AUDOUT:
  1536. case VIDIOC_S_EXT_CTRLS:
  1537. case VIDIOC_S_FBUF:
  1538. case VIDIOC_OVERLAY:
  1539. ret = v4l2_prio_check(&itv->prio, &id->prio);
  1540. if (ret)
  1541. return ret;
  1542. }
  1543. if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
  1544. printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
  1545. v4l_printk_ioctl(cmd);
  1546. printk("\n");
  1547. }
  1548. return video_ioctl2(inode, filp, cmd, arg);
  1549. }
  1550. int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  1551. unsigned long arg)
  1552. {
  1553. struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
  1554. struct ivtv *itv = id->itv;
  1555. int res;
  1556. mutex_lock(&itv->serialize_lock);
  1557. res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
  1558. mutex_unlock(&itv->serialize_lock);
  1559. return res;
  1560. }
  1561. void ivtv_set_funcs(struct video_device *vdev)
  1562. {
  1563. vdev->vidioc_querycap = ivtv_querycap;
  1564. vdev->vidioc_g_priority = ivtv_g_priority;
  1565. vdev->vidioc_s_priority = ivtv_s_priority;
  1566. vdev->vidioc_s_audio = ivtv_s_audio;
  1567. vdev->vidioc_g_audio = ivtv_g_audio;
  1568. vdev->vidioc_enumaudio = ivtv_enumaudio;
  1569. vdev->vidioc_s_audout = ivtv_s_audout;
  1570. vdev->vidioc_g_audout = ivtv_g_audout;
  1571. vdev->vidioc_enum_input = ivtv_enum_input;
  1572. vdev->vidioc_enum_output = ivtv_enum_output;
  1573. vdev->vidioc_enumaudout = ivtv_enumaudout;
  1574. vdev->vidioc_cropcap = ivtv_cropcap;
  1575. vdev->vidioc_s_crop = ivtv_s_crop;
  1576. vdev->vidioc_g_crop = ivtv_g_crop;
  1577. vdev->vidioc_g_input = ivtv_g_input;
  1578. vdev->vidioc_s_input = ivtv_s_input;
  1579. vdev->vidioc_g_output = ivtv_g_output;
  1580. vdev->vidioc_s_output = ivtv_s_output;
  1581. vdev->vidioc_g_frequency = ivtv_g_frequency;
  1582. vdev->vidioc_s_frequency = ivtv_s_frequency;
  1583. vdev->vidioc_s_tuner = ivtv_s_tuner;
  1584. vdev->vidioc_g_tuner = ivtv_g_tuner;
  1585. vdev->vidioc_g_enc_index = ivtv_g_enc_index;
  1586. vdev->vidioc_g_fbuf = ivtv_g_fbuf;
  1587. vdev->vidioc_s_fbuf = ivtv_s_fbuf;
  1588. vdev->vidioc_g_std = ivtv_g_std;
  1589. vdev->vidioc_s_std = ivtv_s_std;
  1590. vdev->vidioc_overlay = ivtv_overlay;
  1591. vdev->vidioc_log_status = ivtv_log_status;
  1592. vdev->vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap;
  1593. vdev->vidioc_encoder_cmd = ivtv_encoder_cmd;
  1594. vdev->vidioc_try_encoder_cmd = ivtv_try_encoder_cmd;
  1595. vdev->vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out;
  1596. vdev->vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap;
  1597. vdev->vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap;
  1598. vdev->vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap;
  1599. vdev->vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out;
  1600. vdev->vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay;
  1601. vdev->vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out;
  1602. vdev->vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap;
  1603. vdev->vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap;
  1604. vdev->vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap;
  1605. vdev->vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out;
  1606. vdev->vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay;
  1607. vdev->vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out;
  1608. vdev->vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap;
  1609. vdev->vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap;
  1610. vdev->vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap;
  1611. vdev->vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out;
  1612. vdev->vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay;
  1613. vdev->vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out;
  1614. vdev->vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap;
  1615. vdev->vidioc_g_chip_ident = ivtv_g_chip_ident;
  1616. vdev->vidioc_g_register = ivtv_g_register;
  1617. vdev->vidioc_s_register = ivtv_s_register;
  1618. vdev->vidioc_default = ivtv_default;
  1619. vdev->vidioc_queryctrl = ivtv_queryctrl;
  1620. vdev->vidioc_querymenu = ivtv_querymenu;
  1621. vdev->vidioc_g_ctrl = ivtv_g_ctrl;
  1622. vdev->vidioc_s_ctrl = ivtv_s_ctrl;
  1623. vdev->vidioc_g_ext_ctrls = ivtv_g_ext_ctrls;
  1624. vdev->vidioc_s_ext_ctrls = ivtv_s_ext_ctrls;
  1625. vdev->vidioc_try_ext_ctrls = ivtv_try_ext_ctrls;
  1626. }