cx18-ioctl.c 22 KB

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