ivtv-ioctl.c 53 KB

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