cx18-ioctl.c 22 KB

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