ivtv-ioctl.c 52 KB

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