cx18-ioctl.c 22 KB

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