cx18-ioctl.c 26 KB

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