cx18-ioctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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@md.metrocast.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. u16 cx18_service2vbi(int type)
  43. {
  44. switch (type) {
  45. case V4L2_SLICED_TELETEXT_B:
  46. return CX18_SLICED_TYPE_TELETEXT_B;
  47. case V4L2_SLICED_CAPTION_525:
  48. return CX18_SLICED_TYPE_CAPTION_525;
  49. case V4L2_SLICED_WSS_625:
  50. return CX18_SLICED_TYPE_WSS_625;
  51. case V4L2_SLICED_VPS:
  52. return CX18_SLICED_TYPE_VPS;
  53. default:
  54. return 0;
  55. }
  56. }
  57. /* Check if VBI services are allowed on the (field, line) for the video std */
  58. static int valid_service_line(int field, int line, int is_pal)
  59. {
  60. return (is_pal && line >= 6 &&
  61. ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
  62. (!is_pal && line >= 10 && line < 22);
  63. }
  64. /*
  65. * For a (field, line, std) and inbound potential set of services for that line,
  66. * return the first valid service of those passed in the incoming set for that
  67. * line in priority order:
  68. * CC, VPS, or WSS over TELETEXT for well known lines
  69. * TELETEXT, before VPS, before CC, before WSS, for other lines
  70. */
  71. static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
  72. {
  73. u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
  74. int i;
  75. set = set & valid_set;
  76. if (set == 0 || !valid_service_line(field, line, is_pal))
  77. return 0;
  78. if (!is_pal) {
  79. if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
  80. return V4L2_SLICED_CAPTION_525;
  81. } else {
  82. if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
  83. return V4L2_SLICED_VPS;
  84. if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
  85. return V4L2_SLICED_WSS_625;
  86. if (line == 23)
  87. return 0;
  88. }
  89. for (i = 0; i < 32; i++) {
  90. if ((1 << i) & set)
  91. return 1 << i;
  92. }
  93. return 0;
  94. }
  95. /*
  96. * Expand the service_set of *fmt into valid service_lines for the std,
  97. * and clear the passed in fmt->service_set
  98. */
  99. void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  100. {
  101. u16 set = fmt->service_set;
  102. int f, l;
  103. fmt->service_set = 0;
  104. for (f = 0; f < 2; f++) {
  105. for (l = 0; l < 24; l++)
  106. fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
  107. }
  108. }
  109. /*
  110. * Sanitize the service_lines in *fmt per the video std, and return 1
  111. * if any service_line is left as valid after santization
  112. */
  113. static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  114. {
  115. int f, l;
  116. u16 set = 0;
  117. for (f = 0; f < 2; f++) {
  118. for (l = 0; l < 24; l++) {
  119. fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
  120. set |= fmt->service_lines[f][l];
  121. }
  122. }
  123. return set != 0;
  124. }
  125. /* Compute the service_set from the assumed valid service_lines of *fmt */
  126. u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
  127. {
  128. int f, l;
  129. u16 set = 0;
  130. for (f = 0; f < 2; f++) {
  131. for (l = 0; l < 24; l++)
  132. set |= fmt->service_lines[f][l];
  133. }
  134. return set;
  135. }
  136. static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
  137. struct v4l2_format *fmt)
  138. {
  139. struct cx18_open_id *id = fh2id(fh);
  140. struct cx18 *cx = id->cx;
  141. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  142. pixfmt->width = cx->cxhdl.width;
  143. pixfmt->height = cx->cxhdl.height;
  144. pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  145. pixfmt->field = V4L2_FIELD_INTERLACED;
  146. pixfmt->priv = 0;
  147. if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
  148. pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
  149. /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
  150. pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
  151. pixfmt->bytesperline = 720;
  152. } else {
  153. pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
  154. pixfmt->sizeimage = 128 * 1024;
  155. pixfmt->bytesperline = 0;
  156. }
  157. return 0;
  158. }
  159. static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
  160. struct v4l2_format *fmt)
  161. {
  162. struct cx18 *cx = fh2id(fh)->cx;
  163. struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
  164. vbifmt->sampling_rate = 27000000;
  165. vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
  166. vbifmt->samples_per_line = vbi_active_samples - 4;
  167. vbifmt->sample_format = V4L2_PIX_FMT_GREY;
  168. vbifmt->start[0] = cx->vbi.start[0];
  169. vbifmt->start[1] = cx->vbi.start[1];
  170. vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
  171. vbifmt->flags = 0;
  172. vbifmt->reserved[0] = 0;
  173. vbifmt->reserved[1] = 0;
  174. return 0;
  175. }
  176. static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
  177. struct v4l2_format *fmt)
  178. {
  179. struct cx18 *cx = fh2id(fh)->cx;
  180. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  181. /* sane, V4L2 spec compliant, defaults */
  182. vbifmt->reserved[0] = 0;
  183. vbifmt->reserved[1] = 0;
  184. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  185. memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
  186. vbifmt->service_set = 0;
  187. /*
  188. * Fetch the configured service_lines and total service_set from the
  189. * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
  190. * fmt->fmt.sliced under valid calling conditions
  191. */
  192. if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
  193. return -EINVAL;
  194. /* Ensure V4L2 spec compliant output */
  195. vbifmt->reserved[0] = 0;
  196. vbifmt->reserved[1] = 0;
  197. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  198. vbifmt->service_set = cx18_get_service_set(vbifmt);
  199. return 0;
  200. }
  201. static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
  202. struct v4l2_format *fmt)
  203. {
  204. struct cx18_open_id *id = fh2id(fh);
  205. struct cx18 *cx = id->cx;
  206. int w = fmt->fmt.pix.width;
  207. int h = fmt->fmt.pix.height;
  208. int min_h = 2;
  209. w = min(w, 720);
  210. w = max(w, 2);
  211. if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
  212. /* YUV height must be a multiple of 32 */
  213. h &= ~0x1f;
  214. min_h = 32;
  215. }
  216. h = min(h, cx->is_50hz ? 576 : 480);
  217. h = max(h, min_h);
  218. cx18_g_fmt_vid_cap(file, fh, fmt);
  219. fmt->fmt.pix.width = w;
  220. fmt->fmt.pix.height = h;
  221. return 0;
  222. }
  223. static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
  224. struct v4l2_format *fmt)
  225. {
  226. return cx18_g_fmt_vbi_cap(file, fh, fmt);
  227. }
  228. static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
  229. struct v4l2_format *fmt)
  230. {
  231. struct cx18 *cx = fh2id(fh)->cx;
  232. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  233. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  234. vbifmt->reserved[0] = 0;
  235. vbifmt->reserved[1] = 0;
  236. /* If given a service set, expand it validly & clear passed in set */
  237. if (vbifmt->service_set)
  238. cx18_expand_service_set(vbifmt, cx->is_50hz);
  239. /* Sanitize the service_lines, and compute the new set if any valid */
  240. if (check_service_set(vbifmt, cx->is_50hz))
  241. vbifmt->service_set = cx18_get_service_set(vbifmt);
  242. return 0;
  243. }
  244. static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
  245. struct v4l2_format *fmt)
  246. {
  247. struct cx18_open_id *id = fh2id(fh);
  248. struct cx18 *cx = id->cx;
  249. struct v4l2_mbus_framefmt mbus_fmt;
  250. int ret;
  251. int w, h;
  252. ret = cx18_try_fmt_vid_cap(file, fh, fmt);
  253. if (ret)
  254. return ret;
  255. w = fmt->fmt.pix.width;
  256. h = fmt->fmt.pix.height;
  257. if (cx->cxhdl.width == w && cx->cxhdl.height == h)
  258. return 0;
  259. if (atomic_read(&cx->ana_capturing) > 0)
  260. return -EBUSY;
  261. mbus_fmt.width = cx->cxhdl.width = w;
  262. mbus_fmt.height = cx->cxhdl.height = h;
  263. mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
  264. v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
  265. return cx18_g_fmt_vid_cap(file, fh, fmt);
  266. }
  267. static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
  268. struct v4l2_format *fmt)
  269. {
  270. struct cx18_open_id *id = fh2id(fh);
  271. struct cx18 *cx = id->cx;
  272. int ret;
  273. /*
  274. * Changing the Encoder's Raw VBI parameters won't have any effect
  275. * if any analog capture is ongoing
  276. */
  277. if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
  278. return -EBUSY;
  279. /*
  280. * Set the digitizer registers for raw active VBI.
  281. * Note cx18_av_vbi_wipes out alot of the passed in fmt under valid
  282. * calling conditions
  283. */
  284. ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
  285. if (ret)
  286. return ret;
  287. /* Store our new v4l2 (non-)sliced VBI state */
  288. cx->vbi.sliced_in->service_set = 0;
  289. cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
  290. return cx18_g_fmt_vbi_cap(file, fh, fmt);
  291. }
  292. static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
  293. struct v4l2_format *fmt)
  294. {
  295. struct cx18_open_id *id = fh2id(fh);
  296. struct cx18 *cx = id->cx;
  297. int ret;
  298. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  299. cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
  300. /*
  301. * Changing the Encoder's Raw VBI parameters won't have any effect
  302. * if any analog capture is ongoing
  303. */
  304. if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
  305. return -EBUSY;
  306. /*
  307. * Set the service_lines requested in the digitizer/slicer registers.
  308. * Note, cx18_av_vbi() wipes some "impossible" service lines in the
  309. * passed in fmt->fmt.sliced under valid calling conditions
  310. */
  311. ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
  312. if (ret)
  313. return ret;
  314. /* Store our current v4l2 sliced VBI settings */
  315. cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
  316. memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
  317. return 0;
  318. }
  319. static int cx18_g_chip_ident(struct file *file, void *fh,
  320. struct v4l2_dbg_chip_ident *chip)
  321. {
  322. struct cx18 *cx = fh2id(fh)->cx;
  323. int err = 0;
  324. chip->ident = V4L2_IDENT_NONE;
  325. chip->revision = 0;
  326. switch (chip->match.type) {
  327. case V4L2_CHIP_MATCH_HOST:
  328. switch (chip->match.addr) {
  329. case 0:
  330. chip->ident = V4L2_IDENT_CX23418;
  331. chip->revision = cx18_read_reg(cx, 0xC72028);
  332. break;
  333. case 1:
  334. /*
  335. * The A/V decoder is always present, but in the rare
  336. * case that the card doesn't have analog, we don't
  337. * use it. We find it w/o using the cx->sd_av pointer
  338. */
  339. cx18_call_hw(cx, CX18_HW_418_AV,
  340. core, g_chip_ident, chip);
  341. break;
  342. default:
  343. /*
  344. * Could return ident = V4L2_IDENT_UNKNOWN if we had
  345. * other host chips at higher addresses, but we don't
  346. */
  347. err = -EINVAL; /* per V4L2 spec */
  348. break;
  349. }
  350. break;
  351. case V4L2_CHIP_MATCH_I2C_DRIVER:
  352. /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
  353. cx18_call_all(cx, core, g_chip_ident, chip);
  354. break;
  355. case V4L2_CHIP_MATCH_I2C_ADDR:
  356. /*
  357. * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
  358. * to look if a chip is at the address with no driver. That's a
  359. * dangerous thing to do with EEPROMs anyway.
  360. */
  361. cx18_call_all(cx, core, g_chip_ident, chip);
  362. break;
  363. default:
  364. err = -EINVAL;
  365. break;
  366. }
  367. return err;
  368. }
  369. #ifdef CONFIG_VIDEO_ADV_DEBUG
  370. static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
  371. {
  372. struct v4l2_dbg_register *regs = arg;
  373. if (!capable(CAP_SYS_ADMIN))
  374. return -EPERM;
  375. if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
  376. return -EINVAL;
  377. regs->size = 4;
  378. if (cmd == VIDIOC_DBG_S_REGISTER)
  379. cx18_write_enc(cx, regs->val, regs->reg);
  380. else
  381. regs->val = cx18_read_enc(cx, regs->reg);
  382. return 0;
  383. }
  384. static int cx18_g_register(struct file *file, void *fh,
  385. struct v4l2_dbg_register *reg)
  386. {
  387. struct cx18 *cx = fh2id(fh)->cx;
  388. if (v4l2_chip_match_host(&reg->match))
  389. return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
  390. /* FIXME - errors shouldn't be ignored */
  391. cx18_call_all(cx, core, g_register, reg);
  392. return 0;
  393. }
  394. static int cx18_s_register(struct file *file, void *fh,
  395. struct v4l2_dbg_register *reg)
  396. {
  397. struct cx18 *cx = fh2id(fh)->cx;
  398. if (v4l2_chip_match_host(&reg->match))
  399. return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
  400. /* FIXME - errors shouldn't be ignored */
  401. cx18_call_all(cx, core, s_register, reg);
  402. return 0;
  403. }
  404. #endif
  405. static int cx18_querycap(struct file *file, void *fh,
  406. struct v4l2_capability *vcap)
  407. {
  408. struct cx18 *cx = fh2id(fh)->cx;
  409. strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
  410. strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
  411. snprintf(vcap->bus_info, sizeof(vcap->bus_info),
  412. "PCI:%s", pci_name(cx->pci_dev));
  413. vcap->version = CX18_DRIVER_VERSION; /* version */
  414. vcap->capabilities = cx->v4l2_cap; /* capabilities */
  415. return 0;
  416. }
  417. static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
  418. {
  419. struct cx18 *cx = fh2id(fh)->cx;
  420. return cx18_get_audio_input(cx, vin->index, vin);
  421. }
  422. static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
  423. {
  424. struct cx18 *cx = fh2id(fh)->cx;
  425. vin->index = cx->audio_input;
  426. return cx18_get_audio_input(cx, vin->index, vin);
  427. }
  428. static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
  429. {
  430. struct cx18 *cx = fh2id(fh)->cx;
  431. if (vout->index >= cx->nof_audio_inputs)
  432. return -EINVAL;
  433. cx->audio_input = vout->index;
  434. cx18_audio_set_io(cx);
  435. return 0;
  436. }
  437. static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  438. {
  439. struct cx18 *cx = fh2id(fh)->cx;
  440. /* set it to defaults from our table */
  441. return cx18_get_input(cx, vin->index, vin);
  442. }
  443. static int cx18_cropcap(struct file *file, void *fh,
  444. struct v4l2_cropcap *cropcap)
  445. {
  446. struct cx18 *cx = fh2id(fh)->cx;
  447. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  448. return -EINVAL;
  449. cropcap->bounds.top = cropcap->bounds.left = 0;
  450. cropcap->bounds.width = 720;
  451. cropcap->bounds.height = cx->is_50hz ? 576 : 480;
  452. cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
  453. cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
  454. cropcap->defrect = cropcap->bounds;
  455. return 0;
  456. }
  457. static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  458. {
  459. struct cx18_open_id *id = fh2id(fh);
  460. struct cx18 *cx = id->cx;
  461. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  462. return -EINVAL;
  463. CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
  464. return -EINVAL;
  465. }
  466. static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  467. {
  468. struct cx18 *cx = fh2id(fh)->cx;
  469. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  470. return -EINVAL;
  471. CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
  472. return -EINVAL;
  473. }
  474. static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
  475. struct v4l2_fmtdesc *fmt)
  476. {
  477. static struct v4l2_fmtdesc formats[] = {
  478. { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
  479. "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
  480. },
  481. { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
  482. "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
  483. }
  484. };
  485. if (fmt->index > 1)
  486. return -EINVAL;
  487. *fmt = formats[fmt->index];
  488. return 0;
  489. }
  490. static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
  491. {
  492. struct cx18 *cx = fh2id(fh)->cx;
  493. *i = cx->active_input;
  494. return 0;
  495. }
  496. int cx18_s_input(struct file *file, void *fh, unsigned int inp)
  497. {
  498. struct cx18_open_id *id = fh2id(fh);
  499. struct cx18 *cx = id->cx;
  500. if (inp >= cx->nof_inputs)
  501. return -EINVAL;
  502. if (inp == cx->active_input) {
  503. CX18_DEBUG_INFO("Input unchanged\n");
  504. return 0;
  505. }
  506. CX18_DEBUG_INFO("Changing input from %d to %d\n",
  507. cx->active_input, inp);
  508. cx->active_input = inp;
  509. /* Set the audio input to whatever is appropriate for the input type. */
  510. cx->audio_input = cx->card->video_inputs[inp].audio_index;
  511. /* prevent others from messing with the streams until
  512. we're finished changing inputs. */
  513. cx18_mute(cx);
  514. cx18_video_set_io(cx);
  515. cx18_audio_set_io(cx);
  516. cx18_unmute(cx);
  517. return 0;
  518. }
  519. static int cx18_g_frequency(struct file *file, void *fh,
  520. struct v4l2_frequency *vf)
  521. {
  522. struct cx18 *cx = fh2id(fh)->cx;
  523. if (vf->tuner != 0)
  524. return -EINVAL;
  525. cx18_call_all(cx, tuner, g_frequency, vf);
  526. return 0;
  527. }
  528. int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
  529. {
  530. struct cx18_open_id *id = fh2id(fh);
  531. struct cx18 *cx = id->cx;
  532. if (vf->tuner != 0)
  533. return -EINVAL;
  534. cx18_mute(cx);
  535. CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
  536. cx18_call_all(cx, tuner, s_frequency, vf);
  537. cx18_unmute(cx);
  538. return 0;
  539. }
  540. static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
  541. {
  542. struct cx18 *cx = fh2id(fh)->cx;
  543. *std = cx->std;
  544. return 0;
  545. }
  546. int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
  547. {
  548. struct cx18_open_id *id = fh2id(fh);
  549. struct cx18 *cx = id->cx;
  550. if ((*std & V4L2_STD_ALL) == 0)
  551. return -EINVAL;
  552. if (*std == cx->std)
  553. return 0;
  554. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
  555. atomic_read(&cx->ana_capturing) > 0) {
  556. /* Switching standard would turn off the radio or mess
  557. with already running streams, prevent that by
  558. returning EBUSY. */
  559. return -EBUSY;
  560. }
  561. cx->std = *std;
  562. cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
  563. cx->is_50hz = !cx->is_60hz;
  564. cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
  565. cx->cxhdl.width = 720;
  566. cx->cxhdl.height = cx->is_50hz ? 576 : 480;
  567. cx->vbi.count = cx->is_50hz ? 18 : 12;
  568. cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
  569. cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
  570. CX18_DEBUG_INFO("Switching standard to %llx.\n",
  571. (unsigned long long) cx->std);
  572. /* Tuner */
  573. cx18_call_all(cx, core, s_std, cx->std);
  574. return 0;
  575. }
  576. static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  577. {
  578. struct cx18_open_id *id = fh2id(fh);
  579. struct cx18 *cx = id->cx;
  580. if (vt->index != 0)
  581. return -EINVAL;
  582. cx18_call_all(cx, tuner, s_tuner, vt);
  583. return 0;
  584. }
  585. static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  586. {
  587. struct cx18 *cx = fh2id(fh)->cx;
  588. if (vt->index != 0)
  589. return -EINVAL;
  590. cx18_call_all(cx, tuner, g_tuner, vt);
  591. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  592. strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
  593. vt->type = V4L2_TUNER_RADIO;
  594. } else {
  595. strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
  596. vt->type = V4L2_TUNER_ANALOG_TV;
  597. }
  598. return 0;
  599. }
  600. static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
  601. struct v4l2_sliced_vbi_cap *cap)
  602. {
  603. struct cx18 *cx = fh2id(fh)->cx;
  604. int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
  605. int f, l;
  606. if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
  607. return -EINVAL;
  608. cap->service_set = 0;
  609. for (f = 0; f < 2; f++) {
  610. for (l = 0; l < 24; l++) {
  611. if (valid_service_line(f, l, cx->is_50hz)) {
  612. /*
  613. * We can find all v4l2 supported vbi services
  614. * for the standard, on a valid line for the std
  615. */
  616. cap->service_lines[f][l] = set;
  617. cap->service_set |= set;
  618. } else
  619. cap->service_lines[f][l] = 0;
  620. }
  621. }
  622. for (f = 0; f < 3; f++)
  623. cap->reserved[f] = 0;
  624. return 0;
  625. }
  626. static int _cx18_process_idx_data(struct cx18_buffer *buf,
  627. struct v4l2_enc_idx *idx)
  628. {
  629. int consumed, remaining;
  630. struct v4l2_enc_idx_entry *e_idx;
  631. struct cx18_enc_idx_entry *e_buf;
  632. /* Frame type lookup: 1=I, 2=P, 4=B */
  633. const int mapping[8] = {
  634. -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
  635. -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
  636. };
  637. /*
  638. * Assumption here is that a buf holds an integral number of
  639. * struct cx18_enc_idx_entry objects and is properly aligned.
  640. * This is enforced by the module options on IDX buffer sizes.
  641. */
  642. remaining = buf->bytesused - buf->readpos;
  643. consumed = 0;
  644. e_idx = &idx->entry[idx->entries];
  645. e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
  646. while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
  647. idx->entries < V4L2_ENC_IDX_ENTRIES) {
  648. e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
  649. | le32_to_cpu(e_buf->offset_low);
  650. e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
  651. | le32_to_cpu(e_buf->pts_low);
  652. e_idx->length = le32_to_cpu(e_buf->length);
  653. e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
  654. e_idx->reserved[0] = 0;
  655. e_idx->reserved[1] = 0;
  656. idx->entries++;
  657. e_idx = &idx->entry[idx->entries];
  658. e_buf++;
  659. remaining -= sizeof(struct cx18_enc_idx_entry);
  660. consumed += sizeof(struct cx18_enc_idx_entry);
  661. }
  662. /* Swallow any partial entries at the end, if there are any */
  663. if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
  664. consumed += remaining;
  665. buf->readpos += consumed;
  666. return consumed;
  667. }
  668. static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
  669. struct v4l2_enc_idx *idx)
  670. {
  671. if (s->type != CX18_ENC_STREAM_TYPE_IDX)
  672. return -EINVAL;
  673. if (mdl->curr_buf == NULL)
  674. mdl->curr_buf = list_first_entry(&mdl->buf_list,
  675. struct cx18_buffer, list);
  676. if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
  677. /*
  678. * For some reason we've exhausted the buffers, but the MDL
  679. * object still said some data was unread.
  680. * Fix that and bail out.
  681. */
  682. mdl->readpos = mdl->bytesused;
  683. return 0;
  684. }
  685. list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
  686. /* Skip any empty buffers in the MDL */
  687. if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
  688. continue;
  689. mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
  690. /* exit when MDL drained or request satisfied */
  691. if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
  692. mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
  693. mdl->readpos >= mdl->bytesused)
  694. break;
  695. }
  696. return 0;
  697. }
  698. static int cx18_g_enc_index(struct file *file, void *fh,
  699. struct v4l2_enc_idx *idx)
  700. {
  701. struct cx18 *cx = fh2id(fh)->cx;
  702. struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  703. s32 tmp;
  704. struct cx18_mdl *mdl;
  705. if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
  706. return -EINVAL;
  707. /* Compute the best case number of entries we can buffer */
  708. tmp = s->buffers -
  709. s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
  710. if (tmp <= 0)
  711. tmp = 1;
  712. tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
  713. /* Fill out the header of the return structure */
  714. idx->entries = 0;
  715. idx->entries_cap = tmp;
  716. memset(idx->reserved, 0, sizeof(idx->reserved));
  717. /* Pull IDX MDLs and buffers from q_full and populate the entries */
  718. do {
  719. mdl = cx18_dequeue(s, &s->q_full);
  720. if (mdl == NULL) /* No more IDX data right now */
  721. break;
  722. /* Extract the Index entry data from the MDL and buffers */
  723. cx18_process_idx_data(s, mdl, idx);
  724. if (mdl->readpos < mdl->bytesused) {
  725. /* We finished with data remaining, push the MDL back */
  726. cx18_push(s, mdl, &s->q_full);
  727. break;
  728. }
  729. /* We drained this MDL, schedule it to go to the firmware */
  730. cx18_enqueue(s, mdl, &s->q_free);
  731. } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
  732. /* Tell the work handler to send free IDX MDLs to the firmware */
  733. cx18_stream_load_fw_queue(s);
  734. return 0;
  735. }
  736. static int cx18_encoder_cmd(struct file *file, void *fh,
  737. struct v4l2_encoder_cmd *enc)
  738. {
  739. struct cx18_open_id *id = fh2id(fh);
  740. struct cx18 *cx = id->cx;
  741. u32 h;
  742. switch (enc->cmd) {
  743. case V4L2_ENC_CMD_START:
  744. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  745. enc->flags = 0;
  746. return cx18_start_capture(id);
  747. case V4L2_ENC_CMD_STOP:
  748. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  749. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  750. cx18_stop_capture(id,
  751. enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
  752. break;
  753. case V4L2_ENC_CMD_PAUSE:
  754. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  755. enc->flags = 0;
  756. if (!atomic_read(&cx->ana_capturing))
  757. return -EPERM;
  758. if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  759. return 0;
  760. h = cx18_find_handle(cx);
  761. if (h == CX18_INVALID_TASK_HANDLE) {
  762. CX18_ERR("Can't find valid task handle for "
  763. "V4L2_ENC_CMD_PAUSE\n");
  764. return -EBADFD;
  765. }
  766. cx18_mute(cx);
  767. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
  768. break;
  769. case V4L2_ENC_CMD_RESUME:
  770. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  771. enc->flags = 0;
  772. if (!atomic_read(&cx->ana_capturing))
  773. return -EPERM;
  774. if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  775. return 0;
  776. h = cx18_find_handle(cx);
  777. if (h == CX18_INVALID_TASK_HANDLE) {
  778. CX18_ERR("Can't find valid task handle for "
  779. "V4L2_ENC_CMD_RESUME\n");
  780. return -EBADFD;
  781. }
  782. cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
  783. cx18_unmute(cx);
  784. break;
  785. default:
  786. CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  787. return -EINVAL;
  788. }
  789. return 0;
  790. }
  791. static int cx18_try_encoder_cmd(struct file *file, void *fh,
  792. struct v4l2_encoder_cmd *enc)
  793. {
  794. struct cx18 *cx = fh2id(fh)->cx;
  795. switch (enc->cmd) {
  796. case V4L2_ENC_CMD_START:
  797. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
  798. enc->flags = 0;
  799. break;
  800. case V4L2_ENC_CMD_STOP:
  801. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
  802. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  803. break;
  804. case V4L2_ENC_CMD_PAUSE:
  805. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
  806. enc->flags = 0;
  807. break;
  808. case V4L2_ENC_CMD_RESUME:
  809. CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
  810. enc->flags = 0;
  811. break;
  812. default:
  813. CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
  814. return -EINVAL;
  815. }
  816. return 0;
  817. }
  818. static int cx18_log_status(struct file *file, void *fh)
  819. {
  820. struct cx18 *cx = fh2id(fh)->cx;
  821. struct v4l2_input vidin;
  822. struct v4l2_audio audin;
  823. int i;
  824. CX18_INFO("================= START STATUS CARD #%d "
  825. "=================\n", cx->instance);
  826. CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
  827. if (cx->hw_flags & CX18_HW_TVEEPROM) {
  828. struct tveeprom tv;
  829. cx18_read_eeprom(cx, &tv);
  830. }
  831. cx18_call_all(cx, core, log_status);
  832. cx18_get_input(cx, cx->active_input, &vidin);
  833. cx18_get_audio_input(cx, cx->audio_input, &audin);
  834. CX18_INFO("Video Input: %s\n", vidin.name);
  835. CX18_INFO("Audio Input: %s\n", audin.name);
  836. mutex_lock(&cx->gpio_lock);
  837. CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
  838. cx->gpio_dir, cx->gpio_val);
  839. mutex_unlock(&cx->gpio_lock);
  840. CX18_INFO("Tuner: %s\n",
  841. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
  842. v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
  843. CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
  844. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  845. struct cx18_stream *s = &cx->streams[i];
  846. if (s->video_dev == NULL || s->buffers == 0)
  847. continue;
  848. CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
  849. s->name, s->s_flags,
  850. atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
  851. / s->buffers,
  852. (s->buffers * s->buf_size) / 1024, s->buffers);
  853. }
  854. CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
  855. (long long)cx->mpg_data_received,
  856. (long long)cx->vbi_data_inserted);
  857. CX18_INFO("================== END STATUS CARD #%d "
  858. "==================\n", cx->instance);
  859. return 0;
  860. }
  861. static long cx18_default(struct file *file, void *fh, bool valid_prio,
  862. int cmd, void *arg)
  863. {
  864. struct cx18 *cx = fh2id(fh)->cx;
  865. switch (cmd) {
  866. case VIDIOC_INT_RESET: {
  867. u32 val = *(u32 *)arg;
  868. if ((val == 0) || (val & 0x01))
  869. cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
  870. (u32) CX18_GPIO_RESET_Z8F0811);
  871. break;
  872. }
  873. default:
  874. return -EINVAL;
  875. }
  876. return 0;
  877. }
  878. long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
  879. unsigned long arg)
  880. {
  881. struct video_device *vfd = video_devdata(filp);
  882. struct cx18_open_id *id = file2id(filp);
  883. struct cx18 *cx = id->cx;
  884. long res;
  885. mutex_lock(&cx->serialize_lock);
  886. if (cx18_debug & CX18_DBGFLG_IOCTL)
  887. vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  888. res = video_ioctl2(filp, cmd, arg);
  889. vfd->debug = 0;
  890. mutex_unlock(&cx->serialize_lock);
  891. return res;
  892. }
  893. static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
  894. .vidioc_querycap = cx18_querycap,
  895. .vidioc_s_audio = cx18_s_audio,
  896. .vidioc_g_audio = cx18_g_audio,
  897. .vidioc_enumaudio = cx18_enumaudio,
  898. .vidioc_enum_input = cx18_enum_input,
  899. .vidioc_cropcap = cx18_cropcap,
  900. .vidioc_s_crop = cx18_s_crop,
  901. .vidioc_g_crop = cx18_g_crop,
  902. .vidioc_g_input = cx18_g_input,
  903. .vidioc_s_input = cx18_s_input,
  904. .vidioc_g_frequency = cx18_g_frequency,
  905. .vidioc_s_frequency = cx18_s_frequency,
  906. .vidioc_s_tuner = cx18_s_tuner,
  907. .vidioc_g_tuner = cx18_g_tuner,
  908. .vidioc_g_enc_index = cx18_g_enc_index,
  909. .vidioc_g_std = cx18_g_std,
  910. .vidioc_s_std = cx18_s_std,
  911. .vidioc_log_status = cx18_log_status,
  912. .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
  913. .vidioc_encoder_cmd = cx18_encoder_cmd,
  914. .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
  915. .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
  916. .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
  917. .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
  918. .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
  919. .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
  920. .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
  921. .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
  922. .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
  923. .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
  924. .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
  925. .vidioc_g_chip_ident = cx18_g_chip_ident,
  926. #ifdef CONFIG_VIDEO_ADV_DEBUG
  927. .vidioc_g_register = cx18_g_register,
  928. .vidioc_s_register = cx18_s_register,
  929. #endif
  930. .vidioc_default = cx18_default,
  931. };
  932. void cx18_set_funcs(struct video_device *vdev)
  933. {
  934. vdev->ioctl_ops = &cx18_ioctl_ops;
  935. }