ivtv-ioctl.c 52 KB

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