ivtv-ioctl.c 53 KB

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