ivtv-ioctl.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  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. itv->vbi.sliced_in->service_set = 0;
  506. itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
  507. return ivtv_g_fmt_vbi_cap(file, fh, fmt);
  508. }
  509. static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  510. {
  511. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  512. struct ivtv_open_id *id = fh;
  513. struct ivtv *itv = id->itv;
  514. int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
  515. if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
  516. return ret;
  517. check_service_set(vbifmt, itv->is_50hz);
  518. if (atomic_read(&itv->capturing) > 0)
  519. return -EBUSY;
  520. itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
  521. memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
  522. return 0;
  523. }
  524. static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
  525. {
  526. struct ivtv_open_id *id = fh;
  527. struct ivtv *itv = id->itv;
  528. struct yuv_playback_info *yi = &itv->yuv_info;
  529. int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
  530. if (ret)
  531. return ret;
  532. if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
  533. return 0;
  534. /* Return now if we already have some frame data */
  535. if (yi->stream_size)
  536. return -EBUSY;
  537. yi->v4l2_src_w = fmt->fmt.pix.width;
  538. yi->v4l2_src_h = fmt->fmt.pix.height;
  539. switch (fmt->fmt.pix.field) {
  540. case V4L2_FIELD_NONE:
  541. yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
  542. break;
  543. case V4L2_FIELD_ANY:
  544. yi->lace_mode = IVTV_YUV_MODE_AUTO;
  545. break;
  546. case V4L2_FIELD_INTERLACED_BT:
  547. yi->lace_mode =
  548. IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
  549. break;
  550. case V4L2_FIELD_INTERLACED_TB:
  551. default:
  552. yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
  553. break;
  554. }
  555. yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
  556. if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
  557. itv->dma_data_req_size =
  558. 1080 * ((yi->v4l2_src_h + 31) & ~31);
  559. /* Force update of yuv registers */
  560. yi->yuv_forced_update = 1;
  561. return 0;
  562. }
  563. static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
  564. {
  565. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  566. int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
  567. if (ret == 0) {
  568. itv->osd_chroma_key = fmt->fmt.win.chromakey;
  569. itv->osd_global_alpha = fmt->fmt.win.global_alpha;
  570. ivtv_set_osd_alpha(itv);
  571. }
  572. return ret;
  573. }
  574. static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_chip_ident *chip)
  575. {
  576. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  577. chip->ident = V4L2_IDENT_NONE;
  578. chip->revision = 0;
  579. if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
  580. if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
  581. chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
  582. return 0;
  583. }
  584. if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  585. return ivtv_i2c_id(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
  586. if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
  587. return ivtv_call_i2c_client(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
  588. return -EINVAL;
  589. }
  590. #ifdef CONFIG_VIDEO_ADV_DEBUG
  591. static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
  592. {
  593. struct v4l2_register *regs = arg;
  594. unsigned long flags;
  595. volatile u8 __iomem *reg_start;
  596. if (!capable(CAP_SYS_ADMIN))
  597. return -EPERM;
  598. if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
  599. reg_start = itv->reg_mem - IVTV_REG_OFFSET;
  600. else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
  601. regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
  602. reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
  603. else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
  604. reg_start = itv->enc_mem;
  605. else
  606. return -EINVAL;
  607. spin_lock_irqsave(&ivtv_cards_lock, flags);
  608. if (cmd == VIDIOC_DBG_G_REGISTER)
  609. regs->val = readl(regs->reg + reg_start);
  610. else
  611. writel(regs->val, regs->reg + reg_start);
  612. spin_unlock_irqrestore(&ivtv_cards_lock, flags);
  613. return 0;
  614. }
  615. static int ivtv_g_register(struct file *file, void *fh, struct v4l2_register *reg)
  616. {
  617. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  618. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  619. return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
  620. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  621. return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
  622. return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
  623. }
  624. static int ivtv_s_register(struct file *file, void *fh, struct v4l2_register *reg)
  625. {
  626. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  627. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  628. return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
  629. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  630. return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
  631. return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
  632. }
  633. #endif
  634. static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
  635. {
  636. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  637. *p = v4l2_prio_max(&itv->prio);
  638. return 0;
  639. }
  640. static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
  641. {
  642. struct ivtv_open_id *id = fh;
  643. struct ivtv *itv = id->itv;
  644. return v4l2_prio_change(&itv->prio, &id->prio, prio);
  645. }
  646. static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
  647. {
  648. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  649. strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
  650. strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
  651. strlcpy(vcap->bus_info, pci_name(itv->dev), sizeof(vcap->bus_info));
  652. vcap->version = IVTV_DRIVER_VERSION; /* version */
  653. vcap->capabilities = itv->v4l2_cap; /* capabilities */
  654. return 0;
  655. }
  656. static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
  657. {
  658. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  659. return ivtv_get_audio_input(itv, vin->index, vin);
  660. }
  661. static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
  662. {
  663. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  664. vin->index = itv->audio_input;
  665. return ivtv_get_audio_input(itv, vin->index, vin);
  666. }
  667. static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
  668. {
  669. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  670. if (vout->index >= itv->nof_audio_inputs)
  671. return -EINVAL;
  672. itv->audio_input = vout->index;
  673. ivtv_audio_set_io(itv);
  674. return 0;
  675. }
  676. static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
  677. {
  678. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  679. /* set it to defaults from our table */
  680. return ivtv_get_audio_output(itv, vin->index, vin);
  681. }
  682. static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
  683. {
  684. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  685. vin->index = 0;
  686. return ivtv_get_audio_output(itv, vin->index, vin);
  687. }
  688. static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
  689. {
  690. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  691. return ivtv_get_audio_output(itv, vout->index, vout);
  692. }
  693. static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  694. {
  695. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  696. /* set it to defaults from our table */
  697. return ivtv_get_input(itv, vin->index, vin);
  698. }
  699. static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
  700. {
  701. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  702. return ivtv_get_output(itv, vout->index, vout);
  703. }
  704. static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
  705. {
  706. struct ivtv_open_id *id = fh;
  707. struct ivtv *itv = id->itv;
  708. struct yuv_playback_info *yi = &itv->yuv_info;
  709. int streamtype;
  710. streamtype = id->type;
  711. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  712. return -EINVAL;
  713. cropcap->bounds.top = cropcap->bounds.left = 0;
  714. cropcap->bounds.width = 720;
  715. if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  716. cropcap->bounds.height = itv->is_50hz ? 576 : 480;
  717. cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
  718. cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
  719. } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
  720. if (yi->track_osd) {
  721. cropcap->bounds.width = yi->osd_full_w;
  722. cropcap->bounds.height = yi->osd_full_h;
  723. } else {
  724. cropcap->bounds.width = 720;
  725. cropcap->bounds.height =
  726. itv->is_out_50hz ? 576 : 480;
  727. }
  728. cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
  729. cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
  730. } else {
  731. cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
  732. cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
  733. cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
  734. }
  735. cropcap->defrect = cropcap->bounds;
  736. return 0;
  737. }
  738. static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  739. {
  740. struct ivtv_open_id *id = fh;
  741. struct ivtv *itv = id->itv;
  742. struct yuv_playback_info *yi = &itv->yuv_info;
  743. int streamtype;
  744. streamtype = id->type;
  745. if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
  746. printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
  747. /* Should be replaced */
  748. /* v4l_printk_ioctl(VIDIOC_S_CROP); */
  749. }
  750. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  751. (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
  752. if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
  753. yi->main_rect = crop->c;
  754. return 0;
  755. } else {
  756. if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
  757. crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
  758. itv->main_rect = crop->c;
  759. return 0;
  760. }
  761. }
  762. return -EINVAL;
  763. }
  764. return -EINVAL;
  765. }
  766. static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  767. {
  768. struct ivtv_open_id *id = fh;
  769. struct ivtv *itv = id->itv;
  770. struct yuv_playback_info *yi = &itv->yuv_info;
  771. int streamtype;
  772. streamtype = id->type;
  773. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  774. (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
  775. if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
  776. crop->c = yi->main_rect;
  777. else
  778. crop->c = itv->main_rect;
  779. return 0;
  780. }
  781. return -EINVAL;
  782. }
  783. static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  784. {
  785. static struct v4l2_fmtdesc formats[] = {
  786. { 0, 0, 0,
  787. "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
  788. { 0, 0, 0, 0 }
  789. },
  790. { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
  791. "MPEG", V4L2_PIX_FMT_MPEG,
  792. { 0, 0, 0, 0 }
  793. }
  794. };
  795. enum v4l2_buf_type type = fmt->type;
  796. if (fmt->index > 1)
  797. return -EINVAL;
  798. *fmt = formats[fmt->index];
  799. fmt->type = type;
  800. return 0;
  801. }
  802. static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  803. {
  804. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  805. static struct v4l2_fmtdesc formats[] = {
  806. { 0, 0, 0,
  807. "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
  808. { 0, 0, 0, 0 }
  809. },
  810. { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
  811. "MPEG", V4L2_PIX_FMT_MPEG,
  812. { 0, 0, 0, 0 }
  813. }
  814. };
  815. enum v4l2_buf_type type = fmt->type;
  816. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  817. return -EINVAL;
  818. if (fmt->index > 1)
  819. return -EINVAL;
  820. *fmt = formats[fmt->index];
  821. fmt->type = type;
  822. return 0;
  823. }
  824. static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
  825. {
  826. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  827. *i = itv->active_input;
  828. return 0;
  829. }
  830. int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
  831. {
  832. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  833. if (inp < 0 || inp >= itv->nof_inputs)
  834. return -EINVAL;
  835. if (inp == itv->active_input) {
  836. IVTV_DEBUG_INFO("Input unchanged\n");
  837. return 0;
  838. }
  839. if (atomic_read(&itv->capturing) > 0) {
  840. return -EBUSY;
  841. }
  842. IVTV_DEBUG_INFO("Changing input from %d to %d\n",
  843. itv->active_input, inp);
  844. itv->active_input = inp;
  845. /* Set the audio input to whatever is appropriate for the
  846. input type. */
  847. itv->audio_input = itv->card->video_inputs[inp].audio_index;
  848. /* prevent others from messing with the streams until
  849. we're finished changing inputs. */
  850. ivtv_mute(itv);
  851. ivtv_video_set_io(itv);
  852. ivtv_audio_set_io(itv);
  853. ivtv_unmute(itv);
  854. return 0;
  855. }
  856. static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
  857. {
  858. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  859. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  860. return -EINVAL;
  861. *i = itv->active_output;
  862. return 0;
  863. }
  864. static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
  865. {
  866. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  867. struct v4l2_routing route;
  868. if (outp >= itv->card->nof_outputs)
  869. return -EINVAL;
  870. if (outp == itv->active_output) {
  871. IVTV_DEBUG_INFO("Output unchanged\n");
  872. return 0;
  873. }
  874. IVTV_DEBUG_INFO("Changing output from %d to %d\n",
  875. itv->active_output, outp);
  876. itv->active_output = outp;
  877. route.input = SAA7127_INPUT_TYPE_NORMAL;
  878. route.output = itv->card->video_outputs[outp].video_output;
  879. ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  880. return 0;
  881. }
  882. static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  883. {
  884. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  885. if (vf->tuner != 0)
  886. return -EINVAL;
  887. ivtv_call_i2c_clients(itv, VIDIOC_G_FREQUENCY, vf);
  888. return 0;
  889. }
  890. int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  891. {
  892. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  893. if (vf->tuner != 0)
  894. return -EINVAL;
  895. ivtv_mute(itv);
  896. IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
  897. ivtv_call_i2c_clients(itv, VIDIOC_S_FREQUENCY, vf);
  898. ivtv_unmute(itv);
  899. return 0;
  900. }
  901. static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
  902. {
  903. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  904. *std = itv->std;
  905. return 0;
  906. }
  907. int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
  908. {
  909. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  910. struct yuv_playback_info *yi = &itv->yuv_info;
  911. if ((*std & V4L2_STD_ALL) == 0)
  912. return -EINVAL;
  913. if (*std == itv->std)
  914. return 0;
  915. if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
  916. atomic_read(&itv->capturing) > 0 ||
  917. atomic_read(&itv->decoding) > 0) {
  918. /* Switching standard would turn off the radio or mess
  919. with already running streams, prevent that by
  920. returning EBUSY. */
  921. return -EBUSY;
  922. }
  923. itv->std = *std;
  924. itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
  925. itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
  926. itv->params.width = 720;
  927. itv->params.height = itv->is_50hz ? 576 : 480;
  928. itv->vbi.count = itv->is_50hz ? 18 : 12;
  929. itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
  930. itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
  931. if (itv->hw_flags & IVTV_HW_CX25840)
  932. itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
  933. IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
  934. /* Tuner */
  935. ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
  936. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  937. /* set display standard */
  938. itv->std_out = *std;
  939. itv->is_out_60hz = itv->is_60hz;
  940. itv->is_out_50hz = itv->is_50hz;
  941. ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
  942. ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
  943. itv->main_rect.left = itv->main_rect.top = 0;
  944. itv->main_rect.width = 720;
  945. itv->main_rect.height = itv->params.height;
  946. ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
  947. 720, itv->main_rect.height, 0, 0);
  948. yi->main_rect = itv->main_rect;
  949. if (!itv->osd_info) {
  950. yi->osd_full_w = 720;
  951. yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
  952. }
  953. }
  954. return 0;
  955. }
  956. static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  957. {
  958. struct ivtv_open_id *id = fh;
  959. struct ivtv *itv = id->itv;
  960. if (vt->index != 0)
  961. return -EINVAL;
  962. ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
  963. return 0;
  964. }
  965. static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  966. {
  967. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  968. if (vt->index != 0)
  969. return -EINVAL;
  970. ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
  971. if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
  972. strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
  973. vt->type = V4L2_TUNER_RADIO;
  974. } else {
  975. strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
  976. vt->type = V4L2_TUNER_ANALOG_TV;
  977. }
  978. return 0;
  979. }
  980. static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
  981. {
  982. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  983. int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
  984. int f, l;
  985. if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  986. for (f = 0; f < 2; f++) {
  987. for (l = 0; l < 24; l++) {
  988. if (valid_service_line(f, l, itv->is_50hz))
  989. cap->service_lines[f][l] = set;
  990. }
  991. }
  992. return 0;
  993. }
  994. if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  995. if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
  996. return -EINVAL;
  997. if (itv->is_60hz) {
  998. cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
  999. cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
  1000. } else {
  1001. cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
  1002. cap->service_lines[0][16] = V4L2_SLICED_VPS;
  1003. }
  1004. return 0;
  1005. }
  1006. return -EINVAL;
  1007. }
  1008. static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
  1009. {
  1010. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1011. struct v4l2_enc_idx_entry *e = idx->entry;
  1012. int entries;
  1013. int i;
  1014. entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
  1015. IVTV_MAX_PGM_INDEX;
  1016. if (entries > V4L2_ENC_IDX_ENTRIES)
  1017. entries = V4L2_ENC_IDX_ENTRIES;
  1018. idx->entries = 0;
  1019. for (i = 0; i < entries; i++) {
  1020. *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
  1021. if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
  1022. idx->entries++;
  1023. e++;
  1024. }
  1025. }
  1026. itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
  1027. return 0;
  1028. }
  1029. static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
  1030. {
  1031. struct ivtv_open_id *id = fh;
  1032. struct ivtv *itv = id->itv;
  1033. switch (enc->cmd) {
  1034. case V4L2_ENC_CMD_START:
  1035. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  1036. enc->flags = 0;
  1037. return ivtv_start_capture(id);
  1038. case V4L2_ENC_CMD_STOP:
  1039. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  1040. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  1041. ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
  1042. return 0;
  1043. case V4L2_ENC_CMD_PAUSE:
  1044. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  1045. enc->flags = 0;
  1046. if (!atomic_read(&itv->capturing))
  1047. return -EPERM;
  1048. if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
  1049. return 0;
  1050. ivtv_mute(itv);
  1051. ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
  1052. break;
  1053. case V4L2_ENC_CMD_RESUME:
  1054. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  1055. enc->flags = 0;
  1056. if (!atomic_read(&itv->capturing))
  1057. return -EPERM;
  1058. if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
  1059. return 0;
  1060. ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
  1061. ivtv_unmute(itv);
  1062. break;
  1063. default:
  1064. IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  1065. return -EINVAL;
  1066. }
  1067. return 0;
  1068. }
  1069. static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
  1070. {
  1071. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1072. switch (enc->cmd) {
  1073. case V4L2_ENC_CMD_START:
  1074. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  1075. enc->flags = 0;
  1076. return 0;
  1077. case V4L2_ENC_CMD_STOP:
  1078. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  1079. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  1080. return 0;
  1081. case V4L2_ENC_CMD_PAUSE:
  1082. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  1083. enc->flags = 0;
  1084. return 0;
  1085. case V4L2_ENC_CMD_RESUME:
  1086. IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  1087. enc->flags = 0;
  1088. return 0;
  1089. default:
  1090. IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  1091. return -EINVAL;
  1092. }
  1093. }
  1094. static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  1095. {
  1096. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1097. u32 data[CX2341X_MBOX_MAX_DATA];
  1098. struct yuv_playback_info *yi = &itv->yuv_info;
  1099. int pixfmt;
  1100. static u32 pixel_format[16] = {
  1101. V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
  1102. V4L2_PIX_FMT_RGB565,
  1103. V4L2_PIX_FMT_RGB555,
  1104. V4L2_PIX_FMT_RGB444,
  1105. V4L2_PIX_FMT_RGB32,
  1106. 0,
  1107. 0,
  1108. 0,
  1109. V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
  1110. V4L2_PIX_FMT_YUV565,
  1111. V4L2_PIX_FMT_YUV555,
  1112. V4L2_PIX_FMT_YUV444,
  1113. V4L2_PIX_FMT_YUV32,
  1114. 0,
  1115. 0,
  1116. 0,
  1117. };
  1118. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
  1119. return -EINVAL;
  1120. if (!itv->osd_video_pbase)
  1121. return -EINVAL;
  1122. fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
  1123. V4L2_FBUF_CAP_GLOBAL_ALPHA;
  1124. ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
  1125. data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
  1126. pixfmt = (data[0] >> 3) & 0xf;
  1127. fb->fmt.pixelformat = pixel_format[pixfmt];
  1128. fb->fmt.width = itv->osd_rect.width;
  1129. fb->fmt.height = itv->osd_rect.height;
  1130. fb->fmt.field = V4L2_FIELD_INTERLACED;
  1131. fb->fmt.bytesperline = fb->fmt.width;
  1132. fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1133. fb->fmt.field = V4L2_FIELD_INTERLACED;
  1134. fb->fmt.priv = 0;
  1135. if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
  1136. fb->fmt.bytesperline *= 2;
  1137. if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
  1138. fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
  1139. fb->fmt.bytesperline *= 2;
  1140. fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
  1141. fb->base = (void *)itv->osd_video_pbase;
  1142. fb->flags = 0;
  1143. if (itv->osd_chroma_key_state)
  1144. fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1145. if (itv->osd_global_alpha_state)
  1146. fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
  1147. pixfmt &= 7;
  1148. /* no local alpha for RGB565 or unknown formats */
  1149. if (pixfmt == 1 || pixfmt > 4)
  1150. return 0;
  1151. /* 16-bit formats have inverted local alpha */
  1152. if (pixfmt == 2 || pixfmt == 3)
  1153. fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
  1154. else
  1155. fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
  1156. if (itv->osd_local_alpha_state) {
  1157. /* 16-bit formats have inverted local alpha */
  1158. if (pixfmt == 2 || pixfmt == 3)
  1159. fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
  1160. else
  1161. fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1162. }
  1163. if (yi->track_osd)
  1164. fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
  1165. return 0;
  1166. }
  1167. static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  1168. {
  1169. struct ivtv_open_id *id = fh;
  1170. struct ivtv *itv = id->itv;
  1171. struct yuv_playback_info *yi = &itv->yuv_info;
  1172. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
  1173. return -EINVAL;
  1174. if (!itv->osd_video_pbase)
  1175. return -EINVAL;
  1176. itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
  1177. itv->osd_local_alpha_state =
  1178. (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
  1179. itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
  1180. ivtv_set_osd_alpha(itv);
  1181. yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
  1182. return ivtv_g_fbuf(file, fh, fb);
  1183. }
  1184. static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
  1185. {
  1186. struct ivtv_open_id *id = fh;
  1187. struct ivtv *itv = id->itv;
  1188. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
  1189. return -EINVAL;
  1190. ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
  1191. return 0;
  1192. }
  1193. static int ivtv_log_status(struct file *file, void *fh)
  1194. {
  1195. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1196. u32 data[CX2341X_MBOX_MAX_DATA];
  1197. int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
  1198. struct v4l2_input vidin;
  1199. struct v4l2_audio audin;
  1200. int i;
  1201. IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
  1202. IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
  1203. if (itv->hw_flags & IVTV_HW_TVEEPROM) {
  1204. struct tveeprom tv;
  1205. ivtv_read_eeprom(itv, &tv);
  1206. }
  1207. ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
  1208. ivtv_get_input(itv, itv->active_input, &vidin);
  1209. ivtv_get_audio_input(itv, itv->audio_input, &audin);
  1210. IVTV_INFO("Video Input: %s\n", vidin.name);
  1211. IVTV_INFO("Audio Input: %s%s\n", audin.name,
  1212. (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
  1213. if (has_output) {
  1214. struct v4l2_output vidout;
  1215. struct v4l2_audioout audout;
  1216. int mode = itv->output_mode;
  1217. static const char * const output_modes[5] = {
  1218. "None",
  1219. "MPEG Streaming",
  1220. "YUV Streaming",
  1221. "YUV Frames",
  1222. "Passthrough",
  1223. };
  1224. static const char * const audio_modes[5] = {
  1225. "Stereo",
  1226. "Left",
  1227. "Right",
  1228. "Mono",
  1229. "Swapped"
  1230. };
  1231. static const char * const alpha_mode[4] = {
  1232. "None",
  1233. "Global",
  1234. "Local",
  1235. "Global and Local"
  1236. };
  1237. static const char * const pixel_format[16] = {
  1238. "ARGB Indexed",
  1239. "RGB 5:6:5",
  1240. "ARGB 1:5:5:5",
  1241. "ARGB 1:4:4:4",
  1242. "ARGB 8:8:8:8",
  1243. "5",
  1244. "6",
  1245. "7",
  1246. "AYUV Indexed",
  1247. "YUV 5:6:5",
  1248. "AYUV 1:5:5:5",
  1249. "AYUV 1:4:4:4",
  1250. "AYUV 8:8:8:8",
  1251. "13",
  1252. "14",
  1253. "15",
  1254. };
  1255. ivtv_get_output(itv, itv->active_output, &vidout);
  1256. ivtv_get_audio_output(itv, 0, &audout);
  1257. IVTV_INFO("Video Output: %s\n", vidout.name);
  1258. IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
  1259. audio_modes[itv->audio_stereo_mode],
  1260. audio_modes[itv->audio_bilingual_mode]);
  1261. if (mode < 0 || mode > OUT_PASSTHROUGH)
  1262. mode = OUT_NONE;
  1263. IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
  1264. ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
  1265. data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
  1266. IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
  1267. data[0] & 1 ? "On" : "Off",
  1268. alpha_mode[(data[0] >> 1) & 0x3],
  1269. pixel_format[(data[0] >> 3) & 0xf]);
  1270. }
  1271. IVTV_INFO("Tuner: %s\n",
  1272. test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
  1273. cx2341x_log_status(&itv->params, itv->name);
  1274. IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
  1275. for (i = 0; i < IVTV_MAX_STREAMS; i++) {
  1276. struct ivtv_stream *s = &itv->streams[i];
  1277. if (s->v4l2dev == NULL || s->buffers == 0)
  1278. continue;
  1279. IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
  1280. (s->buffers - s->q_free.buffers) * 100 / s->buffers,
  1281. (s->buffers * s->buf_size) / 1024, s->buffers);
  1282. }
  1283. IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
  1284. IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
  1285. return 0;
  1286. }
  1287. static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
  1288. {
  1289. struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
  1290. struct ivtv *itv = id->itv;
  1291. int nonblocking = filp->f_flags & O_NONBLOCK;
  1292. struct ivtv_stream *s = &itv->streams[id->type];
  1293. switch (cmd) {
  1294. case IVTV_IOC_DMA_FRAME: {
  1295. struct ivtv_dma_frame *args = arg;
  1296. IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
  1297. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1298. return -EINVAL;
  1299. if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1300. return -EINVAL;
  1301. if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
  1302. return 0;
  1303. if (ivtv_start_decoding(id, id->type)) {
  1304. return -EBUSY;
  1305. }
  1306. if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
  1307. ivtv_release_stream(s);
  1308. return -EBUSY;
  1309. }
  1310. /* Mark that this file handle started the UDMA_YUV mode */
  1311. id->yuv_frames = 1;
  1312. if (args->y_source == NULL)
  1313. return 0;
  1314. return ivtv_yuv_prep_frame(itv, args);
  1315. }
  1316. case VIDEO_GET_PTS: {
  1317. u32 data[CX2341X_MBOX_MAX_DATA];
  1318. u64 *pts = arg;
  1319. IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
  1320. if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
  1321. *pts = s->dma_pts;
  1322. break;
  1323. }
  1324. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1325. return -EINVAL;
  1326. if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
  1327. *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
  1328. (u64)itv->last_dec_timing[1];
  1329. break;
  1330. }
  1331. *pts = 0;
  1332. if (atomic_read(&itv->decoding)) {
  1333. if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
  1334. IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
  1335. return -EIO;
  1336. }
  1337. memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
  1338. set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
  1339. *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
  1340. /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
  1341. }
  1342. break;
  1343. }
  1344. case VIDEO_GET_FRAME_COUNT: {
  1345. u32 data[CX2341X_MBOX_MAX_DATA];
  1346. u64 *frame = arg;
  1347. IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
  1348. if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
  1349. *frame = 0;
  1350. break;
  1351. }
  1352. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1353. return -EINVAL;
  1354. if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
  1355. *frame = itv->last_dec_timing[0];
  1356. break;
  1357. }
  1358. *frame = 0;
  1359. if (atomic_read(&itv->decoding)) {
  1360. if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
  1361. IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
  1362. return -EIO;
  1363. }
  1364. memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
  1365. set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
  1366. *frame = data[0];
  1367. }
  1368. break;
  1369. }
  1370. case VIDEO_PLAY: {
  1371. struct video_command vc;
  1372. IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
  1373. memset(&vc, 0, sizeof(vc));
  1374. vc.cmd = VIDEO_CMD_PLAY;
  1375. return ivtv_video_command(itv, id, &vc, 0);
  1376. }
  1377. case VIDEO_STOP: {
  1378. struct video_command vc;
  1379. IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
  1380. memset(&vc, 0, sizeof(vc));
  1381. vc.cmd = VIDEO_CMD_STOP;
  1382. vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
  1383. return ivtv_video_command(itv, id, &vc, 0);
  1384. }
  1385. case VIDEO_FREEZE: {
  1386. struct video_command vc;
  1387. IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
  1388. memset(&vc, 0, sizeof(vc));
  1389. vc.cmd = VIDEO_CMD_FREEZE;
  1390. return ivtv_video_command(itv, id, &vc, 0);
  1391. }
  1392. case VIDEO_CONTINUE: {
  1393. struct video_command vc;
  1394. IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
  1395. memset(&vc, 0, sizeof(vc));
  1396. vc.cmd = VIDEO_CMD_CONTINUE;
  1397. return ivtv_video_command(itv, id, &vc, 0);
  1398. }
  1399. case VIDEO_COMMAND:
  1400. case VIDEO_TRY_COMMAND: {
  1401. struct video_command *vc = arg;
  1402. int try = (cmd == VIDEO_TRY_COMMAND);
  1403. if (try)
  1404. IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
  1405. else
  1406. IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
  1407. return ivtv_video_command(itv, id, vc, try);
  1408. }
  1409. case VIDEO_GET_EVENT: {
  1410. struct video_event *ev = arg;
  1411. DEFINE_WAIT(wait);
  1412. IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
  1413. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1414. return -EINVAL;
  1415. memset(ev, 0, sizeof(*ev));
  1416. set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
  1417. while (1) {
  1418. if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
  1419. ev->type = VIDEO_EVENT_DECODER_STOPPED;
  1420. else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
  1421. ev->type = VIDEO_EVENT_VSYNC;
  1422. ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
  1423. VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
  1424. if (itv->output_mode == OUT_UDMA_YUV &&
  1425. (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
  1426. IVTV_YUV_MODE_PROGRESSIVE) {
  1427. ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
  1428. }
  1429. }
  1430. if (ev->type)
  1431. return 0;
  1432. if (nonblocking)
  1433. return -EAGAIN;
  1434. /* Wait for event. Note that serialize_lock is locked,
  1435. so to allow other processes to access the driver while
  1436. we are waiting unlock first and later lock again. */
  1437. mutex_unlock(&itv->serialize_lock);
  1438. prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
  1439. if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
  1440. schedule();
  1441. finish_wait(&itv->event_waitq, &wait);
  1442. mutex_lock(&itv->serialize_lock);
  1443. if (signal_pending(current)) {
  1444. /* return if a signal was received */
  1445. IVTV_DEBUG_INFO("User stopped wait for event\n");
  1446. return -EINTR;
  1447. }
  1448. }
  1449. break;
  1450. }
  1451. default:
  1452. return -EINVAL;
  1453. }
  1454. return 0;
  1455. }
  1456. static int ivtv_default(struct file *file, void *fh, int cmd, void *arg)
  1457. {
  1458. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  1459. switch (cmd) {
  1460. case VIDIOC_INT_S_AUDIO_ROUTING: {
  1461. struct v4l2_routing *route = arg;
  1462. ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
  1463. break;
  1464. }
  1465. case VIDIOC_INT_RESET: {
  1466. u32 val = *(u32 *)arg;
  1467. if ((val == 0 && itv->options.newi2c) || (val & 0x01))
  1468. ivtv_reset_ir_gpio(itv);
  1469. if (val & 0x02)
  1470. itv->video_dec_func(itv, cmd, NULL);
  1471. break;
  1472. }
  1473. default:
  1474. return -EINVAL;
  1475. }
  1476. return 0;
  1477. }
  1478. static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
  1479. unsigned int cmd, unsigned long arg)
  1480. {
  1481. struct video_device *vfd = video_devdata(filp);
  1482. struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
  1483. int ret;
  1484. /* Filter dvb ioctls that cannot be handled by the v4l ioctl framework */
  1485. switch (cmd) {
  1486. case VIDEO_SELECT_SOURCE:
  1487. IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
  1488. if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
  1489. return -EINVAL;
  1490. return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
  1491. case AUDIO_SET_MUTE:
  1492. IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
  1493. itv->speed_mute_audio = arg;
  1494. return 0;
  1495. case AUDIO_CHANNEL_SELECT:
  1496. IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
  1497. if (arg > AUDIO_STEREO_SWAPPED)
  1498. return -EINVAL;
  1499. itv->audio_stereo_mode = arg;
  1500. ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
  1501. return 0;
  1502. case AUDIO_BILINGUAL_CHANNEL_SELECT:
  1503. IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
  1504. if (arg > AUDIO_STEREO_SWAPPED)
  1505. return -EINVAL;
  1506. itv->audio_bilingual_mode = arg;
  1507. ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
  1508. return 0;
  1509. case IVTV_IOC_DMA_FRAME:
  1510. case VIDEO_GET_PTS:
  1511. case VIDEO_GET_FRAME_COUNT:
  1512. case VIDEO_GET_EVENT:
  1513. case VIDEO_PLAY:
  1514. case VIDEO_STOP:
  1515. case VIDEO_FREEZE:
  1516. case VIDEO_CONTINUE:
  1517. case VIDEO_COMMAND:
  1518. case VIDEO_TRY_COMMAND:
  1519. return ivtv_decoder_ioctls(filp, cmd, (void *)arg);
  1520. default:
  1521. break;
  1522. }
  1523. /* check priority */
  1524. switch (cmd) {
  1525. case VIDIOC_S_CTRL:
  1526. case VIDIOC_S_STD:
  1527. case VIDIOC_S_INPUT:
  1528. case VIDIOC_S_OUTPUT:
  1529. case VIDIOC_S_TUNER:
  1530. case VIDIOC_S_FREQUENCY:
  1531. case VIDIOC_S_FMT:
  1532. case VIDIOC_S_CROP:
  1533. case VIDIOC_S_AUDIO:
  1534. case VIDIOC_S_AUDOUT:
  1535. case VIDIOC_S_EXT_CTRLS:
  1536. case VIDIOC_S_FBUF:
  1537. case VIDIOC_OVERLAY:
  1538. ret = v4l2_prio_check(&itv->prio, &id->prio);
  1539. if (ret)
  1540. return ret;
  1541. }
  1542. if (ivtv_debug & IVTV_DBGFLG_IOCTL)
  1543. vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  1544. ret = video_ioctl2(inode, filp, cmd, arg);
  1545. vfd->debug = 0;
  1546. return ret;
  1547. }
  1548. int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  1549. unsigned long arg)
  1550. {
  1551. struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
  1552. struct ivtv *itv = id->itv;
  1553. int res;
  1554. mutex_lock(&itv->serialize_lock);
  1555. res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
  1556. mutex_unlock(&itv->serialize_lock);
  1557. return res;
  1558. }
  1559. static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
  1560. .vidioc_querycap = ivtv_querycap,
  1561. .vidioc_g_priority = ivtv_g_priority,
  1562. .vidioc_s_priority = ivtv_s_priority,
  1563. .vidioc_s_audio = ivtv_s_audio,
  1564. .vidioc_g_audio = ivtv_g_audio,
  1565. .vidioc_enumaudio = ivtv_enumaudio,
  1566. .vidioc_s_audout = ivtv_s_audout,
  1567. .vidioc_g_audout = ivtv_g_audout,
  1568. .vidioc_enum_input = ivtv_enum_input,
  1569. .vidioc_enum_output = ivtv_enum_output,
  1570. .vidioc_enumaudout = ivtv_enumaudout,
  1571. .vidioc_cropcap = ivtv_cropcap,
  1572. .vidioc_s_crop = ivtv_s_crop,
  1573. .vidioc_g_crop = ivtv_g_crop,
  1574. .vidioc_g_input = ivtv_g_input,
  1575. .vidioc_s_input = ivtv_s_input,
  1576. .vidioc_g_output = ivtv_g_output,
  1577. .vidioc_s_output = ivtv_s_output,
  1578. .vidioc_g_frequency = ivtv_g_frequency,
  1579. .vidioc_s_frequency = ivtv_s_frequency,
  1580. .vidioc_s_tuner = ivtv_s_tuner,
  1581. .vidioc_g_tuner = ivtv_g_tuner,
  1582. .vidioc_g_enc_index = ivtv_g_enc_index,
  1583. .vidioc_g_fbuf = ivtv_g_fbuf,
  1584. .vidioc_s_fbuf = ivtv_s_fbuf,
  1585. .vidioc_g_std = ivtv_g_std,
  1586. .vidioc_s_std = ivtv_s_std,
  1587. .vidioc_overlay = ivtv_overlay,
  1588. .vidioc_log_status = ivtv_log_status,
  1589. .vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap,
  1590. .vidioc_encoder_cmd = ivtv_encoder_cmd,
  1591. .vidioc_try_encoder_cmd = ivtv_try_encoder_cmd,
  1592. .vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out,
  1593. .vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap,
  1594. .vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap,
  1595. .vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap,
  1596. .vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out,
  1597. .vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay,
  1598. .vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out,
  1599. .vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap,
  1600. .vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap,
  1601. .vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap,
  1602. .vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out,
  1603. .vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay,
  1604. .vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out,
  1605. .vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap,
  1606. .vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap,
  1607. .vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap,
  1608. .vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out,
  1609. .vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay,
  1610. .vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out,
  1611. .vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap,
  1612. .vidioc_g_chip_ident = ivtv_g_chip_ident,
  1613. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1614. .vidioc_g_register = ivtv_g_register,
  1615. .vidioc_s_register = ivtv_s_register,
  1616. #endif
  1617. .vidioc_default = ivtv_default,
  1618. .vidioc_queryctrl = ivtv_queryctrl,
  1619. .vidioc_querymenu = ivtv_querymenu,
  1620. .vidioc_g_ext_ctrls = ivtv_g_ext_ctrls,
  1621. .vidioc_s_ext_ctrls = ivtv_s_ext_ctrls,
  1622. .vidioc_try_ext_ctrls = ivtv_try_ext_ctrls,
  1623. };
  1624. void ivtv_set_funcs(struct video_device *vdev)
  1625. {
  1626. vdev->ioctl_ops = &ivtv_ioctl_ops;
  1627. }