cx18-ioctl.c 22 KB

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