cx18-ioctl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 (v4l2_subdev_call(cx->sd_av, video, 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. v4l2_subdev_call(cx->sd_av, video, 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 = v4l2_subdev_call(cx->sd_av, video, 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 = v4l2_subdev_call(cx->sd_av, video, 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. int err = 0;
  332. chip->ident = V4L2_IDENT_NONE;
  333. chip->revision = 0;
  334. switch (chip->match.type) {
  335. case V4L2_CHIP_MATCH_HOST:
  336. switch (chip->match.addr) {
  337. case 0:
  338. chip->ident = V4L2_IDENT_CX23418;
  339. chip->revision = cx18_read_reg(cx, 0xC72028);
  340. break;
  341. case 1:
  342. /*
  343. * The A/V decoder is always present, but in the rare
  344. * case that the card doesn't have analog, we don't
  345. * use it. We find it w/o using the cx->sd_av pointer
  346. */
  347. cx18_call_hw(cx, CX18_HW_418_AV,
  348. core, g_chip_ident, chip);
  349. break;
  350. default:
  351. /*
  352. * Could return ident = V4L2_IDENT_UNKNOWN if we had
  353. * other host chips at higher addresses, but we don't
  354. */
  355. err = -EINVAL; /* per V4L2 spec */
  356. break;
  357. }
  358. break;
  359. case V4L2_CHIP_MATCH_I2C_DRIVER:
  360. /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
  361. cx18_call_all(cx, core, g_chip_ident, chip);
  362. break;
  363. case V4L2_CHIP_MATCH_I2C_ADDR:
  364. /*
  365. * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
  366. * to look if a chip is at the address with no driver. That's a
  367. * dangerous thing to do with EEPROMs anyway.
  368. */
  369. cx18_call_all(cx, core, g_chip_ident, chip);
  370. break;
  371. default:
  372. err = -EINVAL;
  373. break;
  374. }
  375. return err;
  376. }
  377. #ifdef CONFIG_VIDEO_ADV_DEBUG
  378. static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
  379. {
  380. struct v4l2_dbg_register *regs = arg;
  381. if (!capable(CAP_SYS_ADMIN))
  382. return -EPERM;
  383. if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
  384. return -EINVAL;
  385. regs->size = 4;
  386. if (cmd == VIDIOC_DBG_S_REGISTER)
  387. cx18_write_enc(cx, regs->val, regs->reg);
  388. else
  389. regs->val = cx18_read_enc(cx, regs->reg);
  390. return 0;
  391. }
  392. static int cx18_g_register(struct file *file, void *fh,
  393. struct v4l2_dbg_register *reg)
  394. {
  395. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  396. if (v4l2_chip_match_host(&reg->match))
  397. return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
  398. /* FIXME - errors shouldn't be ignored */
  399. cx18_call_all(cx, core, g_register, reg);
  400. return 0;
  401. }
  402. static int cx18_s_register(struct file *file, void *fh,
  403. struct v4l2_dbg_register *reg)
  404. {
  405. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  406. if (v4l2_chip_match_host(&reg->match))
  407. return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
  408. /* FIXME - errors shouldn't be ignored */
  409. cx18_call_all(cx, core, s_register, reg);
  410. return 0;
  411. }
  412. #endif
  413. static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
  414. {
  415. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  416. *p = v4l2_prio_max(&cx->prio);
  417. return 0;
  418. }
  419. static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
  420. {
  421. struct cx18_open_id *id = fh;
  422. struct cx18 *cx = id->cx;
  423. return v4l2_prio_change(&cx->prio, &id->prio, prio);
  424. }
  425. static int cx18_querycap(struct file *file, void *fh,
  426. struct v4l2_capability *vcap)
  427. {
  428. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  429. strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
  430. strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
  431. snprintf(vcap->bus_info, sizeof(vcap->bus_info),
  432. "PCI:%s", pci_name(cx->pci_dev));
  433. vcap->version = CX18_DRIVER_VERSION; /* version */
  434. vcap->capabilities = cx->v4l2_cap; /* capabilities */
  435. return 0;
  436. }
  437. static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
  438. {
  439. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  440. return cx18_get_audio_input(cx, vin->index, vin);
  441. }
  442. static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
  443. {
  444. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  445. vin->index = cx->audio_input;
  446. return cx18_get_audio_input(cx, vin->index, vin);
  447. }
  448. static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
  449. {
  450. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  451. if (vout->index >= cx->nof_audio_inputs)
  452. return -EINVAL;
  453. cx->audio_input = vout->index;
  454. cx18_audio_set_io(cx);
  455. return 0;
  456. }
  457. static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  458. {
  459. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  460. /* set it to defaults from our table */
  461. return cx18_get_input(cx, vin->index, vin);
  462. }
  463. static int cx18_cropcap(struct file *file, void *fh,
  464. struct v4l2_cropcap *cropcap)
  465. {
  466. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  467. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  468. return -EINVAL;
  469. cropcap->bounds.top = cropcap->bounds.left = 0;
  470. cropcap->bounds.width = 720;
  471. cropcap->bounds.height = cx->is_50hz ? 576 : 480;
  472. cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
  473. cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
  474. cropcap->defrect = cropcap->bounds;
  475. return 0;
  476. }
  477. static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  478. {
  479. struct cx18_open_id *id = fh;
  480. struct cx18 *cx = id->cx;
  481. int ret;
  482. ret = v4l2_prio_check(&cx->prio, &id->prio);
  483. if (ret)
  484. return ret;
  485. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  486. return -EINVAL;
  487. CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
  488. return -EINVAL;
  489. }
  490. static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  491. {
  492. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  493. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  494. return -EINVAL;
  495. CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
  496. return -EINVAL;
  497. }
  498. static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
  499. struct v4l2_fmtdesc *fmt)
  500. {
  501. static struct v4l2_fmtdesc formats[] = {
  502. { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
  503. "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
  504. },
  505. { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
  506. "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
  507. }
  508. };
  509. if (fmt->index > 1)
  510. return -EINVAL;
  511. *fmt = formats[fmt->index];
  512. return 0;
  513. }
  514. static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
  515. {
  516. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  517. *i = cx->active_input;
  518. return 0;
  519. }
  520. int cx18_s_input(struct file *file, void *fh, unsigned int inp)
  521. {
  522. struct cx18_open_id *id = fh;
  523. struct cx18 *cx = id->cx;
  524. int ret;
  525. ret = v4l2_prio_check(&cx->prio, &id->prio);
  526. if (ret)
  527. return ret;
  528. if (inp >= cx->nof_inputs)
  529. return -EINVAL;
  530. if (inp == cx->active_input) {
  531. CX18_DEBUG_INFO("Input unchanged\n");
  532. return 0;
  533. }
  534. CX18_DEBUG_INFO("Changing input from %d to %d\n",
  535. cx->active_input, inp);
  536. cx->active_input = inp;
  537. /* Set the audio input to whatever is appropriate for the input type. */
  538. cx->audio_input = cx->card->video_inputs[inp].audio_index;
  539. /* prevent others from messing with the streams until
  540. we're finished changing inputs. */
  541. cx18_mute(cx);
  542. cx18_video_set_io(cx);
  543. cx18_audio_set_io(cx);
  544. cx18_unmute(cx);
  545. return 0;
  546. }
  547. static int cx18_g_frequency(struct file *file, void *fh,
  548. struct v4l2_frequency *vf)
  549. {
  550. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  551. if (vf->tuner != 0)
  552. return -EINVAL;
  553. cx18_call_all(cx, tuner, g_frequency, vf);
  554. return 0;
  555. }
  556. int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  557. {
  558. struct cx18_open_id *id = fh;
  559. struct cx18 *cx = id->cx;
  560. int ret;
  561. ret = v4l2_prio_check(&cx->prio, &id->prio);
  562. if (ret)
  563. return ret;
  564. if (vf->tuner != 0)
  565. return -EINVAL;
  566. cx18_mute(cx);
  567. CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
  568. cx18_call_all(cx, tuner, s_frequency, vf);
  569. cx18_unmute(cx);
  570. return 0;
  571. }
  572. static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
  573. {
  574. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  575. *std = cx->std;
  576. return 0;
  577. }
  578. int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
  579. {
  580. struct cx18_open_id *id = fh;
  581. struct cx18 *cx = id->cx;
  582. int ret;
  583. ret = v4l2_prio_check(&cx->prio, &id->prio);
  584. if (ret)
  585. return ret;
  586. if ((*std & V4L2_STD_ALL) == 0)
  587. return -EINVAL;
  588. if (*std == cx->std)
  589. return 0;
  590. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
  591. atomic_read(&cx->ana_capturing) > 0) {
  592. /* Switching standard would turn off the radio or mess
  593. with already running streams, prevent that by
  594. returning EBUSY. */
  595. return -EBUSY;
  596. }
  597. cx->std = *std;
  598. cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
  599. cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
  600. cx->params.width = 720;
  601. cx->params.height = cx->is_50hz ? 576 : 480;
  602. cx->vbi.count = cx->is_50hz ? 18 : 12;
  603. cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
  604. cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
  605. CX18_DEBUG_INFO("Switching standard to %llx.\n",
  606. (unsigned long long) cx->std);
  607. /* Tuner */
  608. cx18_call_all(cx, core, s_std, cx->std);
  609. return 0;
  610. }
  611. static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  612. {
  613. struct cx18_open_id *id = fh;
  614. struct cx18 *cx = id->cx;
  615. int ret;
  616. ret = v4l2_prio_check(&cx->prio, &id->prio);
  617. if (ret)
  618. return ret;
  619. if (vt->index != 0)
  620. return -EINVAL;
  621. cx18_call_all(cx, tuner, s_tuner, vt);
  622. return 0;
  623. }
  624. static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  625. {
  626. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  627. if (vt->index != 0)
  628. return -EINVAL;
  629. cx18_call_all(cx, tuner, g_tuner, vt);
  630. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  631. strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
  632. vt->type = V4L2_TUNER_RADIO;
  633. } else {
  634. strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
  635. vt->type = V4L2_TUNER_ANALOG_TV;
  636. }
  637. return 0;
  638. }
  639. static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
  640. struct v4l2_sliced_vbi_cap *cap)
  641. {
  642. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  643. int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
  644. int f, l;
  645. if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
  646. return -EINVAL;
  647. cap->service_set = 0;
  648. for (f = 0; f < 2; f++) {
  649. for (l = 0; l < 24; l++) {
  650. if (valid_service_line(f, l, cx->is_50hz)) {
  651. /*
  652. * We can find all v4l2 supported vbi services
  653. * for the standard, on a valid line for the std
  654. */
  655. cap->service_lines[f][l] = set;
  656. cap->service_set |= set;
  657. } else
  658. cap->service_lines[f][l] = 0;
  659. }
  660. }
  661. for (f = 0; f < 3; f++)
  662. cap->reserved[f] = 0;
  663. return 0;
  664. }
  665. static int cx18_g_enc_index(struct file *file, void *fh,
  666. struct v4l2_enc_idx *idx)
  667. {
  668. return -EINVAL;
  669. }
  670. static int cx18_encoder_cmd(struct file *file, void *fh,
  671. struct v4l2_encoder_cmd *enc)
  672. {
  673. struct cx18_open_id *id = fh;
  674. struct cx18 *cx = id->cx;
  675. u32 h;
  676. switch (enc->cmd) {
  677. case V4L2_ENC_CMD_START:
  678. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  679. enc->flags = 0;
  680. return cx18_start_capture(id);
  681. case V4L2_ENC_CMD_STOP:
  682. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  683. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  684. cx18_stop_capture(id,
  685. enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
  686. break;
  687. case V4L2_ENC_CMD_PAUSE:
  688. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  689. enc->flags = 0;
  690. if (!atomic_read(&cx->ana_capturing))
  691. return -EPERM;
  692. if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  693. return 0;
  694. h = cx18_find_handle(cx);
  695. if (h == CX18_INVALID_TASK_HANDLE) {
  696. CX18_ERR("Can't find valid task handle for "
  697. "V4L2_ENC_CMD_PAUSE\n");
  698. return -EBADFD;
  699. }
  700. cx18_mute(cx);
  701. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
  702. break;
  703. case V4L2_ENC_CMD_RESUME:
  704. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  705. enc->flags = 0;
  706. if (!atomic_read(&cx->ana_capturing))
  707. return -EPERM;
  708. if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  709. return 0;
  710. h = cx18_find_handle(cx);
  711. if (h == CX18_INVALID_TASK_HANDLE) {
  712. CX18_ERR("Can't find valid task handle for "
  713. "V4L2_ENC_CMD_RESUME\n");
  714. return -EBADFD;
  715. }
  716. cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
  717. cx18_unmute(cx);
  718. break;
  719. default:
  720. CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  721. return -EINVAL;
  722. }
  723. return 0;
  724. }
  725. static int cx18_try_encoder_cmd(struct file *file, void *fh,
  726. struct v4l2_encoder_cmd *enc)
  727. {
  728. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  729. switch (enc->cmd) {
  730. case V4L2_ENC_CMD_START:
  731. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  732. enc->flags = 0;
  733. break;
  734. case V4L2_ENC_CMD_STOP:
  735. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  736. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  737. break;
  738. case V4L2_ENC_CMD_PAUSE:
  739. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  740. enc->flags = 0;
  741. break;
  742. case V4L2_ENC_CMD_RESUME:
  743. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  744. enc->flags = 0;
  745. break;
  746. default:
  747. CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  748. return -EINVAL;
  749. }
  750. return 0;
  751. }
  752. static int cx18_log_status(struct file *file, void *fh)
  753. {
  754. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  755. struct v4l2_input vidin;
  756. struct v4l2_audio audin;
  757. int i;
  758. CX18_INFO("================= START STATUS CARD #%d "
  759. "=================\n", cx->instance);
  760. CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
  761. if (cx->hw_flags & CX18_HW_TVEEPROM) {
  762. struct tveeprom tv;
  763. cx18_read_eeprom(cx, &tv);
  764. }
  765. cx18_call_all(cx, core, log_status);
  766. cx18_get_input(cx, cx->active_input, &vidin);
  767. cx18_get_audio_input(cx, cx->audio_input, &audin);
  768. CX18_INFO("Video Input: %s\n", vidin.name);
  769. CX18_INFO("Audio Input: %s\n", audin.name);
  770. mutex_lock(&cx->gpio_lock);
  771. CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
  772. cx->gpio_dir, cx->gpio_val);
  773. mutex_unlock(&cx->gpio_lock);
  774. CX18_INFO("Tuner: %s\n",
  775. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
  776. cx2341x_log_status(&cx->params, cx->v4l2_dev.name);
  777. CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
  778. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  779. struct cx18_stream *s = &cx->streams[i];
  780. if (s->video_dev == NULL || s->buffers == 0)
  781. continue;
  782. CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
  783. s->name, s->s_flags,
  784. atomic_read(&s->q_full.buffers) * 100 / s->buffers,
  785. (s->buffers * s->buf_size) / 1024, s->buffers);
  786. }
  787. CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
  788. (long long)cx->mpg_data_received,
  789. (long long)cx->vbi_data_inserted);
  790. CX18_INFO("================== END STATUS CARD #%d "
  791. "==================\n", cx->instance);
  792. return 0;
  793. }
  794. static long cx18_default(struct file *file, void *fh, int cmd, void *arg)
  795. {
  796. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  797. switch (cmd) {
  798. case VIDIOC_INT_RESET: {
  799. u32 val = *(u32 *)arg;
  800. if ((val == 0) || (val & 0x01))
  801. cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
  802. (u32) CX18_GPIO_RESET_Z8F0811);
  803. break;
  804. }
  805. default:
  806. return -EINVAL;
  807. }
  808. return 0;
  809. }
  810. long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
  811. unsigned long arg)
  812. {
  813. struct video_device *vfd = video_devdata(filp);
  814. struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
  815. struct cx18 *cx = id->cx;
  816. long res;
  817. mutex_lock(&cx->serialize_lock);
  818. /* FIXME - consolidate v4l2_prio_check()'s here */
  819. if (cx18_debug & CX18_DBGFLG_IOCTL)
  820. vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  821. res = video_ioctl2(filp, cmd, arg);
  822. vfd->debug = 0;
  823. mutex_unlock(&cx->serialize_lock);
  824. return res;
  825. }
  826. static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
  827. .vidioc_querycap = cx18_querycap,
  828. .vidioc_g_priority = cx18_g_priority,
  829. .vidioc_s_priority = cx18_s_priority,
  830. .vidioc_s_audio = cx18_s_audio,
  831. .vidioc_g_audio = cx18_g_audio,
  832. .vidioc_enumaudio = cx18_enumaudio,
  833. .vidioc_enum_input = cx18_enum_input,
  834. .vidioc_cropcap = cx18_cropcap,
  835. .vidioc_s_crop = cx18_s_crop,
  836. .vidioc_g_crop = cx18_g_crop,
  837. .vidioc_g_input = cx18_g_input,
  838. .vidioc_s_input = cx18_s_input,
  839. .vidioc_g_frequency = cx18_g_frequency,
  840. .vidioc_s_frequency = cx18_s_frequency,
  841. .vidioc_s_tuner = cx18_s_tuner,
  842. .vidioc_g_tuner = cx18_g_tuner,
  843. .vidioc_g_enc_index = cx18_g_enc_index,
  844. .vidioc_g_std = cx18_g_std,
  845. .vidioc_s_std = cx18_s_std,
  846. .vidioc_log_status = cx18_log_status,
  847. .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
  848. .vidioc_encoder_cmd = cx18_encoder_cmd,
  849. .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
  850. .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
  851. .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
  852. .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
  853. .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
  854. .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
  855. .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
  856. .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
  857. .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
  858. .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
  859. .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
  860. .vidioc_g_chip_ident = cx18_g_chip_ident,
  861. #ifdef CONFIG_VIDEO_ADV_DEBUG
  862. .vidioc_g_register = cx18_g_register,
  863. .vidioc_s_register = cx18_s_register,
  864. #endif
  865. .vidioc_default = cx18_default,
  866. .vidioc_queryctrl = cx18_queryctrl,
  867. .vidioc_querymenu = cx18_querymenu,
  868. .vidioc_g_ext_ctrls = cx18_g_ext_ctrls,
  869. .vidioc_s_ext_ctrls = cx18_s_ext_ctrls,
  870. .vidioc_try_ext_ctrls = cx18_try_ext_ctrls,
  871. };
  872. void cx18_set_funcs(struct video_device *vdev)
  873. {
  874. vdev->ioctl_ops = &cx18_ioctl_ops;
  875. }