cx18-ioctl.c 22 KB

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