ivtv-ioctl.c 52 KB

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