cx18-ioctl.c 26 KB

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