cx18-ioctl.c 26 KB

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