ivtv-ioctl.c 52 KB

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