cx18-ioctl.c 26 KB

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