cx18-ioctl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * cx18 ioctl system call
  3. *
  4. * Derived from ivtv-ioctl.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-version.h"
  25. #include "cx18-mailbox.h"
  26. #include "cx18-i2c.h"
  27. #include "cx18-queue.h"
  28. #include "cx18-fileops.h"
  29. #include "cx18-vbi.h"
  30. #include "cx18-audio.h"
  31. #include "cx18-video.h"
  32. #include "cx18-streams.h"
  33. #include "cx18-ioctl.h"
  34. #include "cx18-gpio.h"
  35. #include "cx18-controls.h"
  36. #include "cx18-cards.h"
  37. #include "cx18-av-core.h"
  38. #include <media/tveeprom.h>
  39. #include <media/v4l2-chip-ident.h>
  40. #include <linux/i2c-id.h>
  41. u16 cx18_service2vbi(int type)
  42. {
  43. switch (type) {
  44. case V4L2_SLICED_TELETEXT_B:
  45. return CX18_SLICED_TYPE_TELETEXT_B;
  46. case V4L2_SLICED_CAPTION_525:
  47. return CX18_SLICED_TYPE_CAPTION_525;
  48. case V4L2_SLICED_WSS_625:
  49. return CX18_SLICED_TYPE_WSS_625;
  50. case V4L2_SLICED_VPS:
  51. return CX18_SLICED_TYPE_VPS;
  52. default:
  53. return 0;
  54. }
  55. }
  56. static int valid_service_line(int field, int line, int is_pal)
  57. {
  58. return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
  59. (!is_pal && line >= 10 && line < 22);
  60. }
  61. static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
  62. {
  63. u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
  64. int i;
  65. set = set & valid_set;
  66. if (set == 0 || !valid_service_line(field, line, is_pal))
  67. return 0;
  68. if (!is_pal) {
  69. if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
  70. return V4L2_SLICED_CAPTION_525;
  71. } else {
  72. if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
  73. return V4L2_SLICED_VPS;
  74. if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
  75. return V4L2_SLICED_WSS_625;
  76. if (line == 23)
  77. return 0;
  78. }
  79. for (i = 0; i < 32; i++) {
  80. if ((1 << i) & set)
  81. return 1 << i;
  82. }
  83. return 0;
  84. }
  85. void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  86. {
  87. u16 set = fmt->service_set;
  88. int f, l;
  89. fmt->service_set = 0;
  90. for (f = 0; f < 2; f++) {
  91. for (l = 0; l < 24; l++)
  92. fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
  93. }
  94. }
  95. u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
  96. {
  97. int f, l;
  98. u16 set = 0;
  99. for (f = 0; f < 2; f++) {
  100. for (l = 0; l < 24; l++)
  101. set |= fmt->service_lines[f][l];
  102. }
  103. return set;
  104. }
  105. static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
  106. {
  107. struct v4l2_register *regs = arg;
  108. unsigned long flags;
  109. if (!capable(CAP_SYS_ADMIN))
  110. return -EPERM;
  111. if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
  112. return -EINVAL;
  113. spin_lock_irqsave(&cx18_cards_lock, flags);
  114. if (cmd == VIDIOC_DBG_G_REGISTER)
  115. regs->val = read_enc(regs->reg);
  116. else
  117. write_enc(regs->val, regs->reg);
  118. spin_unlock_irqrestore(&cx18_cards_lock, flags);
  119. return 0;
  120. }
  121. static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
  122. struct v4l2_format *fmt)
  123. {
  124. struct cx18_open_id *id = fh;
  125. struct cx18 *cx = id->cx;
  126. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  127. pixfmt->width = cx->params.width;
  128. pixfmt->height = cx->params.height;
  129. pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  130. pixfmt->field = V4L2_FIELD_INTERLACED;
  131. pixfmt->priv = 0;
  132. if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
  133. pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
  134. /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
  135. pixfmt->sizeimage =
  136. pixfmt->height * pixfmt->width +
  137. pixfmt->height * (pixfmt->width / 2);
  138. pixfmt->bytesperline = 720;
  139. } else {
  140. pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
  141. pixfmt->sizeimage = 128 * 1024;
  142. pixfmt->bytesperline = 0;
  143. }
  144. return 0;
  145. }
  146. static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
  147. struct v4l2_format *fmt)
  148. {
  149. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  150. struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
  151. CX18_DEBUG_IOCTL("VIDIOC_G_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
  152. vbifmt->sampling_rate = 27000000;
  153. vbifmt->offset = 248;
  154. vbifmt->samples_per_line = cx->vbi.raw_decoder_line_size - 4;
  155. vbifmt->sample_format = V4L2_PIX_FMT_GREY;
  156. vbifmt->start[0] = cx->vbi.start[0];
  157. vbifmt->start[1] = cx->vbi.start[1];
  158. vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
  159. vbifmt->flags = 0;
  160. vbifmt->reserved[0] = 0;
  161. vbifmt->reserved[1] = 0;
  162. return 0;
  163. }
  164. static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
  165. struct v4l2_format *fmt)
  166. {
  167. return -EINVAL;
  168. }
  169. static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
  170. struct v4l2_format *fmt)
  171. {
  172. struct cx18_open_id *id = fh;
  173. struct cx18 *cx = id->cx;
  174. int w = fmt->fmt.pix.width;
  175. int h = fmt->fmt.pix.height;
  176. CX18_DEBUG_IOCTL("VIDIOC_TRY_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  177. w = min(w, 720);
  178. w = max(w, 1);
  179. h = min(h, cx->is_50hz ? 576 : 480);
  180. h = max(h, 2);
  181. cx18_g_fmt_vid_cap(file, fh, fmt);
  182. fmt->fmt.pix.width = w;
  183. fmt->fmt.pix.height = h;
  184. return 0;
  185. }
  186. static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
  187. struct v4l2_format *fmt)
  188. {
  189. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  190. CX18_DEBUG_IOCTL("VIDIOC_TRY_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
  191. return cx18_g_fmt_vbi_cap(file, fh, fmt);
  192. }
  193. static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
  194. struct v4l2_format *fmt)
  195. {
  196. return -EINVAL;
  197. }
  198. static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
  199. struct v4l2_format *fmt)
  200. {
  201. struct cx18_open_id *id = fh;
  202. struct cx18 *cx = id->cx;
  203. int ret;
  204. int w = fmt->fmt.pix.width;
  205. int h = fmt->fmt.pix.height;
  206. ret = v4l2_prio_check(&cx->prio, &id->prio);
  207. if (ret)
  208. return ret;
  209. CX18_DEBUG_IOCTL("VIDIOC_S_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  210. ret = cx18_try_fmt_vid_cap(file, fh, fmt);
  211. if (ret)
  212. return ret;
  213. if (cx->params.width == w && cx->params.height == h)
  214. return 0;
  215. if (atomic_read(&cx->ana_capturing) > 0)
  216. return -EBUSY;
  217. cx->params.width = w;
  218. cx->params.height = h;
  219. cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
  220. return cx18_g_fmt_vid_cap(file, fh, fmt);
  221. }
  222. static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
  223. struct v4l2_format *fmt)
  224. {
  225. struct cx18_open_id *id = fh;
  226. struct cx18 *cx = id->cx;
  227. int ret;
  228. ret = v4l2_prio_check(&cx->prio, &id->prio);
  229. if (ret)
  230. return ret;
  231. CX18_DEBUG_IOCTL("VIDIOC_S_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
  232. if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
  233. cx->vbi.sliced_in->service_set &&
  234. atomic_read(&cx->ana_capturing) > 0)
  235. return -EBUSY;
  236. cx->vbi.sliced_in->service_set = 0;
  237. cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
  238. return cx18_g_fmt_vbi_cap(file, fh, fmt);
  239. }
  240. static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
  241. struct v4l2_format *fmt)
  242. {
  243. return -EINVAL;
  244. }
  245. static int cx18_g_chip_ident(struct file *file, void *fh,
  246. struct v4l2_chip_ident *chip)
  247. {
  248. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  249. CX18_DEBUG_IOCTL("VIDIOC_G_CHIP_IDENT\n");
  250. chip->ident = V4L2_IDENT_NONE;
  251. chip->revision = 0;
  252. if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
  253. if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
  254. chip->ident = V4L2_IDENT_CX23418;
  255. return 0;
  256. }
  257. if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  258. return cx18_i2c_id(cx, chip->match_chip, VIDIOC_G_CHIP_IDENT,
  259. chip);
  260. if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
  261. return cx18_call_i2c_client(cx, chip->match_chip,
  262. VIDIOC_G_CHIP_IDENT, chip);
  263. return -EINVAL;
  264. }
  265. static int cx18_g_register(struct file *file, void *fh,
  266. struct v4l2_register *reg)
  267. {
  268. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  269. CX18_DEBUG_IOCTL("VIDIOC_DBG_G_REGISTER\n");
  270. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  271. return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
  272. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  273. return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
  274. reg);
  275. return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
  276. reg);
  277. }
  278. static int cx18_s_register(struct file *file, void *fh,
  279. struct v4l2_register *reg)
  280. {
  281. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  282. CX18_DEBUG_IOCTL("VIDIOC_DBG_S_REGISTER\n");
  283. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  284. return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
  285. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  286. return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
  287. reg);
  288. return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
  289. reg);
  290. }
  291. static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
  292. {
  293. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  294. CX18_DEBUG_IOCTL("VIDIOC_G_PRIORITY\n");
  295. *p = v4l2_prio_max(&cx->prio);
  296. return 0;
  297. }
  298. static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
  299. {
  300. struct cx18_open_id *id = fh;
  301. struct cx18 *cx = id->cx;
  302. CX18_DEBUG_IOCTL("VIDIOC_S_PRIORITY\n");
  303. return v4l2_prio_change(&cx->prio, &id->prio, prio);
  304. }
  305. static int cx18_querycap(struct file *file, void *fh,
  306. struct v4l2_capability *vcap)
  307. {
  308. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  309. CX18_DEBUG_IOCTL("VIDIOC_QUERYCAP\n");
  310. strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
  311. strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
  312. strlcpy(vcap->bus_info, pci_name(cx->dev), sizeof(vcap->bus_info));
  313. vcap->version = CX18_DRIVER_VERSION; /* version */
  314. vcap->capabilities = cx->v4l2_cap; /* capabilities */
  315. return 0;
  316. }
  317. static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
  318. {
  319. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  320. CX18_DEBUG_IOCTL("VIDIOC_ENUMAUDIO\n");
  321. return cx18_get_audio_input(cx, vin->index, vin);
  322. }
  323. static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
  324. {
  325. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  326. CX18_DEBUG_IOCTL("VIDIOC_G_AUDIO\n");
  327. vin->index = cx->audio_input;
  328. return cx18_get_audio_input(cx, vin->index, vin);
  329. }
  330. static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
  331. {
  332. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  333. CX18_DEBUG_IOCTL("VIDIOC_S_AUDIO\n");
  334. if (vout->index >= cx->nof_audio_inputs)
  335. return -EINVAL;
  336. cx->audio_input = vout->index;
  337. cx18_audio_set_io(cx);
  338. return 0;
  339. }
  340. static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  341. {
  342. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  343. CX18_DEBUG_IOCTL("VIDIOC_ENUMINPUT\n");
  344. /* set it to defaults from our table */
  345. return cx18_get_input(cx, vin->index, vin);
  346. }
  347. static int cx18_cropcap(struct file *file, void *fh,
  348. struct v4l2_cropcap *cropcap)
  349. {
  350. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  351. CX18_DEBUG_IOCTL("VIDIOC_CROPCAP\n");
  352. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  353. return -EINVAL;
  354. cropcap->bounds.top = cropcap->bounds.left = 0;
  355. cropcap->bounds.width = 720;
  356. cropcap->bounds.height = cx->is_50hz ? 576 : 480;
  357. cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
  358. cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
  359. cropcap->defrect = cropcap->bounds;
  360. return 0;
  361. }
  362. static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  363. {
  364. struct cx18_open_id *id = fh;
  365. struct cx18 *cx = id->cx;
  366. int ret;
  367. ret = v4l2_prio_check(&cx->prio, &id->prio);
  368. if (ret)
  369. return ret;
  370. CX18_DEBUG_IOCTL("VIDIOC_S_CROP\n");
  371. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  372. return -EINVAL;
  373. return cx18_av_cmd(cx, VIDIOC_S_CROP, crop);
  374. }
  375. static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  376. {
  377. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  378. CX18_DEBUG_IOCTL("VIDIOC_G_CROP\n");
  379. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  380. return -EINVAL;
  381. return cx18_av_cmd(cx, VIDIOC_G_CROP, crop);
  382. }
  383. static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
  384. struct v4l2_fmtdesc *fmt)
  385. {
  386. static struct v4l2_fmtdesc formats[] = {
  387. { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
  388. "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
  389. },
  390. { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
  391. "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
  392. }
  393. };
  394. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  395. CX18_DEBUG_IOCTL("VIDIOC_ENUM_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  396. if (fmt->index > 1)
  397. return -EINVAL;
  398. *fmt = formats[fmt->index];
  399. return 0;
  400. }
  401. static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
  402. {
  403. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  404. CX18_DEBUG_IOCTL("VIDIOC_G_INPUT\n");
  405. *i = cx->active_input;
  406. return 0;
  407. }
  408. int cx18_s_input(struct file *file, void *fh, unsigned int inp)
  409. {
  410. struct cx18_open_id *id = fh;
  411. struct cx18 *cx = id->cx;
  412. int ret;
  413. ret = v4l2_prio_check(&cx->prio, &id->prio);
  414. if (ret)
  415. return ret;
  416. CX18_DEBUG_IOCTL("VIDIOC_S_INPUT\n");
  417. if (inp < 0 || inp >= cx->nof_inputs)
  418. return -EINVAL;
  419. if (inp == cx->active_input) {
  420. CX18_DEBUG_INFO("Input unchanged\n");
  421. return 0;
  422. }
  423. CX18_DEBUG_INFO("Changing input from %d to %d\n",
  424. cx->active_input, inp);
  425. cx->active_input = inp;
  426. /* Set the audio input to whatever is appropriate for the input type. */
  427. cx->audio_input = cx->card->video_inputs[inp].audio_index;
  428. /* prevent others from messing with the streams until
  429. we're finished changing inputs. */
  430. cx18_mute(cx);
  431. cx18_video_set_io(cx);
  432. cx18_audio_set_io(cx);
  433. cx18_unmute(cx);
  434. return 0;
  435. }
  436. static int cx18_g_frequency(struct file *file, void *fh,
  437. struct v4l2_frequency *vf)
  438. {
  439. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  440. CX18_DEBUG_IOCTL("VIDIOC_G_FREQUENCY\n");
  441. if (vf->tuner != 0)
  442. return -EINVAL;
  443. cx18_call_i2c_clients(cx, VIDIOC_G_FREQUENCY, vf);
  444. return 0;
  445. }
  446. int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  447. {
  448. struct cx18_open_id *id = fh;
  449. struct cx18 *cx = id->cx;
  450. int ret;
  451. ret = v4l2_prio_check(&cx->prio, &id->prio);
  452. if (ret)
  453. return ret;
  454. CX18_DEBUG_IOCTL("VIDIOC_S_FREQUENCY\n");
  455. if (vf->tuner != 0)
  456. return -EINVAL;
  457. cx18_mute(cx);
  458. CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
  459. cx18_call_i2c_clients(cx, VIDIOC_S_FREQUENCY, vf);
  460. cx18_unmute(cx);
  461. return 0;
  462. }
  463. static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
  464. {
  465. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  466. CX18_DEBUG_IOCTL("VIDIOC_G_STD\n");
  467. *std = cx->std;
  468. return 0;
  469. }
  470. int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
  471. {
  472. struct cx18_open_id *id = fh;
  473. struct cx18 *cx = id->cx;
  474. int ret;
  475. ret = v4l2_prio_check(&cx->prio, &id->prio);
  476. if (ret)
  477. return ret;
  478. CX18_DEBUG_IOCTL("VIDIOC_S_STD\n");
  479. if ((*std & V4L2_STD_ALL) == 0)
  480. return -EINVAL;
  481. if (*std == cx->std)
  482. return 0;
  483. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
  484. atomic_read(&cx->ana_capturing) > 0) {
  485. /* Switching standard would turn off the radio or mess
  486. with already running streams, prevent that by
  487. returning EBUSY. */
  488. return -EBUSY;
  489. }
  490. cx->std = *std;
  491. cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
  492. cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
  493. cx->params.width = 720;
  494. cx->params.height = cx->is_50hz ? 576 : 480;
  495. cx->vbi.count = cx->is_50hz ? 18 : 12;
  496. cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
  497. cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
  498. cx->vbi.sliced_decoder_line_size = cx->is_60hz ? 272 : 284;
  499. CX18_DEBUG_INFO("Switching standard to %llx.\n",
  500. (unsigned long long) cx->std);
  501. /* Tuner */
  502. cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
  503. return 0;
  504. }
  505. static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  506. {
  507. struct cx18_open_id *id = fh;
  508. struct cx18 *cx = id->cx;
  509. int ret;
  510. ret = v4l2_prio_check(&cx->prio, &id->prio);
  511. if (ret)
  512. return ret;
  513. CX18_DEBUG_IOCTL("VIDIOC_S_TUNER\n");
  514. if (vt->index != 0)
  515. return -EINVAL;
  516. /* Setting tuner can only set audio mode */
  517. cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
  518. return 0;
  519. }
  520. static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  521. {
  522. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  523. CX18_DEBUG_IOCTL("VIDIOC_G_TUNER\n");
  524. if (vt->index != 0)
  525. return -EINVAL;
  526. cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
  527. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  528. strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
  529. vt->type = V4L2_TUNER_RADIO;
  530. } else {
  531. strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
  532. vt->type = V4L2_TUNER_ANALOG_TV;
  533. }
  534. return 0;
  535. }
  536. static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
  537. struct v4l2_sliced_vbi_cap *cap)
  538. {
  539. return -EINVAL;
  540. }
  541. static int cx18_g_enc_index(struct file *file, void *fh,
  542. struct v4l2_enc_idx *idx)
  543. {
  544. return -EINVAL;
  545. }
  546. static int cx18_encoder_cmd(struct file *file, void *fh,
  547. struct v4l2_encoder_cmd *enc)
  548. {
  549. struct cx18_open_id *id = fh;
  550. struct cx18 *cx = id->cx;
  551. CX18_DEBUG_IOCTL("VIDIOC_ENCODER_CMD:\n");
  552. switch (enc->cmd) {
  553. case V4L2_ENC_CMD_START:
  554. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  555. enc->flags = 0;
  556. return cx18_start_capture(id);
  557. case V4L2_ENC_CMD_STOP:
  558. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  559. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  560. cx18_stop_capture(id,
  561. enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
  562. break;
  563. case V4L2_ENC_CMD_PAUSE:
  564. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  565. enc->flags = 0;
  566. if (!atomic_read(&cx->ana_capturing))
  567. return -EPERM;
  568. if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  569. return 0;
  570. cx18_mute(cx);
  571. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, cx18_find_handle(cx));
  572. break;
  573. case V4L2_ENC_CMD_RESUME:
  574. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  575. enc->flags = 0;
  576. if (!atomic_read(&cx->ana_capturing))
  577. return -EPERM;
  578. if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  579. return 0;
  580. cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, cx18_find_handle(cx));
  581. cx18_unmute(cx);
  582. break;
  583. default:
  584. CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  585. return -EINVAL;
  586. }
  587. return 0;
  588. }
  589. static int cx18_try_encoder_cmd(struct file *file, void *fh,
  590. struct v4l2_encoder_cmd *enc)
  591. {
  592. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  593. CX18_DEBUG_IOCTL("VIDIOC_TRY_ENCDOER_CMD:\n");
  594. switch (enc->cmd) {
  595. case V4L2_ENC_CMD_START:
  596. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  597. enc->flags = 0;
  598. break;
  599. case V4L2_ENC_CMD_STOP:
  600. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  601. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  602. break;
  603. case V4L2_ENC_CMD_PAUSE:
  604. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  605. enc->flags = 0;
  606. break;
  607. case V4L2_ENC_CMD_RESUME:
  608. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  609. enc->flags = 0;
  610. break;
  611. default:
  612. CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  613. return -EINVAL;
  614. }
  615. return 0;
  616. }
  617. static int cx18_log_status(struct file *file, void *fh)
  618. {
  619. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  620. struct v4l2_input vidin;
  621. struct v4l2_audio audin;
  622. int i;
  623. CX18_DEBUG_IOCTL("VIDIOC_LOG_STATUS\n");
  624. CX18_INFO("================= START STATUS CARD #%d =================\n", cx->num);
  625. if (cx->hw_flags & CX18_HW_TVEEPROM) {
  626. struct tveeprom tv;
  627. cx18_read_eeprom(cx, &tv);
  628. }
  629. cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
  630. cx18_get_input(cx, cx->active_input, &vidin);
  631. cx18_get_audio_input(cx, cx->audio_input, &audin);
  632. CX18_INFO("Video Input: %s\n", vidin.name);
  633. CX18_INFO("Audio Input: %s\n", audin.name);
  634. CX18_INFO("Tuner: %s\n",
  635. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
  636. cx2341x_log_status(&cx->params, cx->name);
  637. CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
  638. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  639. struct cx18_stream *s = &cx->streams[i];
  640. if (s->v4l2dev == NULL || s->buffers == 0)
  641. continue;
  642. CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
  643. s->name, s->s_flags,
  644. (s->buffers - s->q_free.buffers) * 100 / s->buffers,
  645. (s->buffers * s->buf_size) / 1024, s->buffers);
  646. }
  647. CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
  648. (long long)cx->mpg_data_received,
  649. (long long)cx->vbi_data_inserted);
  650. CX18_INFO("================== END STATUS CARD #%d ==================\n", cx->num);
  651. return 0;
  652. }
  653. static int cx18_default(struct file *file, void *fh, int cmd, void *arg)
  654. {
  655. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  656. switch (cmd) {
  657. case VIDIOC_INT_S_AUDIO_ROUTING: {
  658. struct v4l2_routing *route = arg;
  659. CX18_DEBUG_IOCTL("VIDIOC_INT_S_AUDIO_ROUTING (%d, %d)\n",
  660. route->input, route->output);
  661. cx18_audio_set_route(cx, route);
  662. break;
  663. }
  664. case VIDIOC_INT_RESET: {
  665. u32 val = *(u32 *)arg;
  666. CX18_DEBUG_IOCTL("VIDIOC_INT_RESET (%#10x)\n", val);
  667. /* No op right now */
  668. /* cx18_av_cmd(cx, cmd, arg) */
  669. /* cx18_call_i2c_clients(cx, cmd, arg) */
  670. break;
  671. }
  672. default:
  673. if (cx18_debug & CX18_DBGFLG_IOCTL) {
  674. printk(KERN_INFO "cx18%d ioctl: unsupported cmd: ",
  675. cx->num);
  676. v4l_printk_ioctl(cmd);
  677. printk("\n");
  678. }
  679. return -EINVAL;
  680. }
  681. return 0;
  682. }
  683. int cx18_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  684. unsigned long arg)
  685. {
  686. struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
  687. struct cx18 *cx = id->cx;
  688. int res;
  689. mutex_lock(&cx->serialize_lock);
  690. res = video_ioctl2(inode, filp, cmd, arg);
  691. mutex_unlock(&cx->serialize_lock);
  692. return res;
  693. }
  694. void cx18_set_funcs(struct video_device *vdev)
  695. {
  696. vdev->vidioc_querycap = cx18_querycap;
  697. vdev->vidioc_g_priority = cx18_g_priority;
  698. vdev->vidioc_s_priority = cx18_s_priority;
  699. vdev->vidioc_s_audio = cx18_s_audio;
  700. vdev->vidioc_g_audio = cx18_g_audio;
  701. vdev->vidioc_enumaudio = cx18_enumaudio;
  702. vdev->vidioc_enum_input = cx18_enum_input;
  703. vdev->vidioc_cropcap = cx18_cropcap;
  704. vdev->vidioc_s_crop = cx18_s_crop;
  705. vdev->vidioc_g_crop = cx18_g_crop;
  706. vdev->vidioc_g_input = cx18_g_input;
  707. vdev->vidioc_s_input = cx18_s_input;
  708. vdev->vidioc_g_frequency = cx18_g_frequency;
  709. vdev->vidioc_s_frequency = cx18_s_frequency;
  710. vdev->vidioc_s_tuner = cx18_s_tuner;
  711. vdev->vidioc_g_tuner = cx18_g_tuner;
  712. vdev->vidioc_g_enc_index = cx18_g_enc_index;
  713. vdev->vidioc_g_std = cx18_g_std;
  714. vdev->vidioc_s_std = cx18_s_std;
  715. vdev->vidioc_log_status = cx18_log_status;
  716. vdev->vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap;
  717. vdev->vidioc_encoder_cmd = cx18_encoder_cmd;
  718. vdev->vidioc_try_encoder_cmd = cx18_try_encoder_cmd;
  719. vdev->vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap;
  720. vdev->vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap;
  721. vdev->vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap;
  722. vdev->vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap;
  723. vdev->vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap;
  724. vdev->vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap;
  725. vdev->vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap;
  726. vdev->vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap;
  727. vdev->vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap;
  728. vdev->vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap;
  729. vdev->vidioc_g_chip_ident = cx18_g_chip_ident;
  730. vdev->vidioc_g_register = cx18_g_register;
  731. vdev->vidioc_s_register = cx18_s_register;
  732. vdev->vidioc_default = cx18_default;
  733. vdev->vidioc_queryctrl = cx18_queryctrl;
  734. vdev->vidioc_querymenu = cx18_querymenu;
  735. vdev->vidioc_g_ctrl = cx18_g_ctrl;
  736. vdev->vidioc_s_ctrl = cx18_s_ctrl;
  737. vdev->vidioc_g_ext_ctrls = cx18_g_ext_ctrls;
  738. vdev->vidioc_s_ext_ctrls = cx18_s_ext_ctrls;
  739. vdev->vidioc_try_ext_ctrls = cx18_try_ext_ctrls;
  740. }