cx18-ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * cx18 ioctl system call
  3. *
  4. * Derived from ivtv-ioctl.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-version.h"
  25. #include "cx18-mailbox.h"
  26. #include "cx18-i2c.h"
  27. #include "cx18-queue.h"
  28. #include "cx18-fileops.h"
  29. #include "cx18-vbi.h"
  30. #include "cx18-audio.h"
  31. #include "cx18-video.h"
  32. #include "cx18-streams.h"
  33. #include "cx18-ioctl.h"
  34. #include "cx18-gpio.h"
  35. #include "cx18-controls.h"
  36. #include "cx18-cards.h"
  37. #include "cx18-av-core.h"
  38. #include <media/tveeprom.h>
  39. #include <media/v4l2-chip-ident.h>
  40. #include <linux/i2c-id.h>
  41. u16 service2vbi(int type)
  42. {
  43. switch (type) {
  44. case V4L2_SLICED_TELETEXT_B:
  45. return CX18_SLICED_TYPE_TELETEXT_B;
  46. case V4L2_SLICED_CAPTION_525:
  47. return CX18_SLICED_TYPE_CAPTION_525;
  48. case V4L2_SLICED_WSS_625:
  49. return CX18_SLICED_TYPE_WSS_625;
  50. case V4L2_SLICED_VPS:
  51. return CX18_SLICED_TYPE_VPS;
  52. default:
  53. return 0;
  54. }
  55. }
  56. static int valid_service_line(int field, int line, int is_pal)
  57. {
  58. return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
  59. (!is_pal && line >= 10 && line < 22);
  60. }
  61. static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
  62. {
  63. u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
  64. int i;
  65. set = set & valid_set;
  66. if (set == 0 || !valid_service_line(field, line, is_pal))
  67. return 0;
  68. if (!is_pal) {
  69. if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
  70. return V4L2_SLICED_CAPTION_525;
  71. } else {
  72. if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
  73. return V4L2_SLICED_VPS;
  74. if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
  75. return V4L2_SLICED_WSS_625;
  76. if (line == 23)
  77. return 0;
  78. }
  79. for (i = 0; i < 32; i++) {
  80. if ((1 << i) & set)
  81. return 1 << i;
  82. }
  83. return 0;
  84. }
  85. void expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  86. {
  87. u16 set = fmt->service_set;
  88. int f, l;
  89. fmt->service_set = 0;
  90. for (f = 0; f < 2; f++) {
  91. for (l = 0; l < 24; l++)
  92. fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
  93. }
  94. }
  95. static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
  96. {
  97. int f, l;
  98. u16 set = 0;
  99. for (f = 0; f < 2; f++) {
  100. for (l = 0; l < 24; l++) {
  101. fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
  102. set |= fmt->service_lines[f][l];
  103. }
  104. }
  105. return set != 0;
  106. }
  107. u16 get_service_set(struct v4l2_sliced_vbi_format *fmt)
  108. {
  109. int f, l;
  110. u16 set = 0;
  111. for (f = 0; f < 2; f++) {
  112. for (l = 0; l < 24; l++)
  113. set |= fmt->service_lines[f][l];
  114. }
  115. return set;
  116. }
  117. static const struct {
  118. v4l2_std_id std;
  119. char *name;
  120. } enum_stds[] = {
  121. { V4L2_STD_PAL_BG | V4L2_STD_PAL_H, "PAL-BGH" },
  122. { V4L2_STD_PAL_DK, "PAL-DK" },
  123. { V4L2_STD_PAL_I, "PAL-I" },
  124. { V4L2_STD_PAL_M, "PAL-M" },
  125. { V4L2_STD_PAL_N, "PAL-N" },
  126. { V4L2_STD_PAL_Nc, "PAL-Nc" },
  127. { V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, "SECAM-BGH" },
  128. { V4L2_STD_SECAM_DK, "SECAM-DK" },
  129. { V4L2_STD_SECAM_L, "SECAM-L" },
  130. { V4L2_STD_SECAM_LC, "SECAM-L'" },
  131. { V4L2_STD_NTSC_M, "NTSC-M" },
  132. { V4L2_STD_NTSC_M_JP, "NTSC-J" },
  133. { V4L2_STD_NTSC_M_KR, "NTSC-K" },
  134. };
  135. static const struct v4l2_standard cx18_std_60hz = {
  136. .frameperiod = {.numerator = 1001, .denominator = 30000},
  137. .framelines = 525,
  138. };
  139. static const struct v4l2_standard cx18_std_50hz = {
  140. .frameperiod = { .numerator = 1, .denominator = 25 },
  141. .framelines = 625,
  142. };
  143. static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
  144. {
  145. struct v4l2_register *regs = arg;
  146. unsigned long flags;
  147. if (!capable(CAP_SYS_ADMIN))
  148. return -EPERM;
  149. if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
  150. return -EINVAL;
  151. spin_lock_irqsave(&cx18_cards_lock, flags);
  152. if (cmd == VIDIOC_DBG_G_REGISTER)
  153. regs->val = read_enc(regs->reg);
  154. else
  155. write_enc(regs->val, regs->reg);
  156. spin_unlock_irqrestore(&cx18_cards_lock, flags);
  157. return 0;
  158. }
  159. static int cx18_get_fmt(struct cx18 *cx, int streamtype, struct v4l2_format *fmt)
  160. {
  161. switch (fmt->type) {
  162. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  163. fmt->fmt.pix.width = cx->params.width;
  164. fmt->fmt.pix.height = cx->params.height;
  165. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  166. fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
  167. if (streamtype == CX18_ENC_STREAM_TYPE_YUV) {
  168. fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
  169. /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
  170. fmt->fmt.pix.sizeimage =
  171. fmt->fmt.pix.height * fmt->fmt.pix.width +
  172. fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
  173. } else {
  174. fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  175. fmt->fmt.pix.sizeimage = 128 * 1024;
  176. }
  177. break;
  178. case V4L2_BUF_TYPE_VBI_CAPTURE:
  179. fmt->fmt.vbi.sampling_rate = 27000000;
  180. fmt->fmt.vbi.offset = 248;
  181. fmt->fmt.vbi.samples_per_line = cx->vbi.raw_decoder_line_size - 4;
  182. fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  183. fmt->fmt.vbi.start[0] = cx->vbi.start[0];
  184. fmt->fmt.vbi.start[1] = cx->vbi.start[1];
  185. fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = cx->vbi.count;
  186. break;
  187. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  188. {
  189. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  190. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  191. memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
  192. memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
  193. cx18_av_cmd(cx, VIDIOC_G_FMT, fmt);
  194. vbifmt->service_set = get_service_set(vbifmt);
  195. break;
  196. }
  197. default:
  198. return -EINVAL;
  199. }
  200. return 0;
  201. }
  202. static int cx18_try_or_set_fmt(struct cx18 *cx, int streamtype,
  203. struct v4l2_format *fmt, int set_fmt)
  204. {
  205. struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
  206. u16 set;
  207. /* set window size */
  208. if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  209. int w = fmt->fmt.pix.width;
  210. int h = fmt->fmt.pix.height;
  211. if (w > 720)
  212. w = 720;
  213. else if (w < 1)
  214. w = 1;
  215. if (h > (cx->is_50hz ? 576 : 480))
  216. h = (cx->is_50hz ? 576 : 480);
  217. else if (h < 2)
  218. h = 2;
  219. cx18_get_fmt(cx, streamtype, fmt);
  220. fmt->fmt.pix.width = w;
  221. fmt->fmt.pix.height = h;
  222. if (!set_fmt || (cx->params.width == w && cx->params.height == h))
  223. return 0;
  224. if (atomic_read(&cx->capturing) > 0)
  225. return -EBUSY;
  226. cx->params.width = w;
  227. cx->params.height = h;
  228. if (w != 720 || h != (cx->is_50hz ? 576 : 480))
  229. cx->params.video_temporal_filter = 0;
  230. else
  231. cx->params.video_temporal_filter = 8;
  232. cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
  233. return cx18_get_fmt(cx, streamtype, fmt);
  234. }
  235. /* set raw VBI format */
  236. if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  237. if (set_fmt && streamtype == CX18_ENC_STREAM_TYPE_VBI &&
  238. cx->vbi.sliced_in->service_set &&
  239. atomic_read(&cx->capturing) > 0)
  240. return -EBUSY;
  241. if (set_fmt) {
  242. cx->vbi.sliced_in->service_set = 0;
  243. cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
  244. }
  245. return cx18_get_fmt(cx, streamtype, fmt);
  246. }
  247. /* any else but sliced VBI capture is an error */
  248. if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
  249. return -EINVAL;
  250. /* TODO: implement sliced VBI, for now silently return 0 */
  251. return 0;
  252. /* set sliced VBI capture format */
  253. vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
  254. memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
  255. if (vbifmt->service_set)
  256. expand_service_set(vbifmt, cx->is_50hz);
  257. set = check_service_set(vbifmt, cx->is_50hz);
  258. vbifmt->service_set = get_service_set(vbifmt);
  259. if (!set_fmt)
  260. return 0;
  261. if (set == 0)
  262. return -EINVAL;
  263. if (atomic_read(&cx->capturing) > 0 && cx->vbi.sliced_in->service_set == 0)
  264. return -EBUSY;
  265. cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
  266. memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
  267. return 0;
  268. }
  269. static int cx18_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
  270. {
  271. struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
  272. struct cx18 *cx = id->cx;
  273. struct v4l2_register *reg = arg;
  274. switch (cmd) {
  275. /* ioctls to allow direct access to the encoder registers for testing */
  276. case VIDIOC_DBG_G_REGISTER:
  277. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  278. return cx18_cxc(cx, cmd, arg);
  279. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  280. return cx18_i2c_id(cx, reg->match_chip, cmd, arg);
  281. return cx18_call_i2c_client(cx, reg->match_chip, cmd, arg);
  282. case VIDIOC_DBG_S_REGISTER:
  283. if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
  284. return cx18_cxc(cx, cmd, arg);
  285. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  286. return cx18_i2c_id(cx, reg->match_chip, cmd, arg);
  287. return cx18_call_i2c_client(cx, reg->match_chip, cmd, arg);
  288. case VIDIOC_G_CHIP_IDENT: {
  289. struct v4l2_chip_ident *chip = arg;
  290. chip->ident = V4L2_IDENT_NONE;
  291. chip->revision = 0;
  292. if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
  293. if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) {
  294. struct v4l2_chip_ident *chip = arg;
  295. chip->ident = V4L2_IDENT_CX23418;
  296. }
  297. return 0;
  298. }
  299. if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
  300. return cx18_i2c_id(cx, reg->match_chip, cmd, arg);
  301. if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
  302. return cx18_call_i2c_client(cx, reg->match_chip, cmd, arg);
  303. return -EINVAL;
  304. }
  305. case VIDIOC_INT_S_AUDIO_ROUTING: {
  306. struct v4l2_routing *route = arg;
  307. cx18_audio_set_route(cx, route);
  308. break;
  309. }
  310. default:
  311. return -EINVAL;
  312. }
  313. return 0;
  314. }
  315. int cx18_v4l2_ioctls(struct cx18 *cx, struct file *filp, unsigned cmd, void *arg)
  316. {
  317. struct cx18_open_id *id = NULL;
  318. if (filp)
  319. id = (struct cx18_open_id *)filp->private_data;
  320. switch (cmd) {
  321. case VIDIOC_G_PRIORITY:
  322. {
  323. enum v4l2_priority *p = arg;
  324. *p = v4l2_prio_max(&cx->prio);
  325. break;
  326. }
  327. case VIDIOC_S_PRIORITY:
  328. {
  329. enum v4l2_priority *prio = arg;
  330. return v4l2_prio_change(&cx->prio, &id->prio, *prio);
  331. }
  332. case VIDIOC_QUERYCAP:{
  333. struct v4l2_capability *vcap = arg;
  334. memset(vcap, 0, sizeof(*vcap));
  335. strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
  336. strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
  337. strlcpy(vcap->bus_info, pci_name(cx->dev), sizeof(vcap->bus_info));
  338. vcap->version = CX18_DRIVER_VERSION; /* version */
  339. vcap->capabilities = cx->v4l2_cap; /* capabilities */
  340. /* reserved.. must set to 0! */
  341. vcap->reserved[0] = vcap->reserved[1] =
  342. vcap->reserved[2] = vcap->reserved[3] = 0;
  343. break;
  344. }
  345. case VIDIOC_ENUMAUDIO:{
  346. struct v4l2_audio *vin = arg;
  347. return cx18_get_audio_input(cx, vin->index, vin);
  348. }
  349. case VIDIOC_G_AUDIO:{
  350. struct v4l2_audio *vin = arg;
  351. vin->index = cx->audio_input;
  352. return cx18_get_audio_input(cx, vin->index, vin);
  353. }
  354. case VIDIOC_S_AUDIO:{
  355. struct v4l2_audio *vout = arg;
  356. if (vout->index >= cx->nof_audio_inputs)
  357. return -EINVAL;
  358. cx->audio_input = vout->index;
  359. cx18_audio_set_io(cx);
  360. break;
  361. }
  362. case VIDIOC_ENUMINPUT:{
  363. struct v4l2_input *vin = arg;
  364. /* set it to defaults from our table */
  365. return cx18_get_input(cx, vin->index, vin);
  366. }
  367. case VIDIOC_TRY_FMT:
  368. case VIDIOC_S_FMT: {
  369. struct v4l2_format *fmt = arg;
  370. return cx18_try_or_set_fmt(cx, id->type, fmt, cmd == VIDIOC_S_FMT);
  371. }
  372. case VIDIOC_G_FMT: {
  373. struct v4l2_format *fmt = arg;
  374. int type = fmt->type;
  375. memset(fmt, 0, sizeof(*fmt));
  376. fmt->type = type;
  377. return cx18_get_fmt(cx, id->type, fmt);
  378. }
  379. case VIDIOC_CROPCAP: {
  380. struct v4l2_cropcap *cropcap = arg;
  381. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  382. return -EINVAL;
  383. cropcap->bounds.top = cropcap->bounds.left = 0;
  384. cropcap->bounds.width = 720;
  385. cropcap->bounds.height = cx->is_50hz ? 576 : 480;
  386. cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
  387. cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
  388. cropcap->defrect = cropcap->bounds;
  389. return 0;
  390. }
  391. case VIDIOC_S_CROP: {
  392. struct v4l2_crop *crop = arg;
  393. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  394. return -EINVAL;
  395. return cx18_av_cmd(cx, VIDIOC_S_CROP, arg);
  396. }
  397. case VIDIOC_G_CROP: {
  398. struct v4l2_crop *crop = arg;
  399. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  400. return -EINVAL;
  401. return cx18_av_cmd(cx, VIDIOC_G_CROP, arg);
  402. }
  403. case VIDIOC_ENUM_FMT: {
  404. static struct v4l2_fmtdesc formats[] = {
  405. { 0, 0, 0,
  406. "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12,
  407. { 0, 0, 0, 0 }
  408. },
  409. { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
  410. "MPEG", V4L2_PIX_FMT_MPEG,
  411. { 0, 0, 0, 0 }
  412. }
  413. };
  414. struct v4l2_fmtdesc *fmt = arg;
  415. enum v4l2_buf_type type = fmt->type;
  416. switch (type) {
  417. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  418. break;
  419. default:
  420. return -EINVAL;
  421. }
  422. if (fmt->index > 1)
  423. return -EINVAL;
  424. *fmt = formats[fmt->index];
  425. fmt->type = type;
  426. return 0;
  427. }
  428. case VIDIOC_G_INPUT:{
  429. *(int *)arg = cx->active_input;
  430. break;
  431. }
  432. case VIDIOC_S_INPUT:{
  433. int inp = *(int *)arg;
  434. if (inp < 0 || inp >= cx->nof_inputs)
  435. return -EINVAL;
  436. if (inp == cx->active_input) {
  437. CX18_DEBUG_INFO("Input unchanged\n");
  438. break;
  439. }
  440. CX18_DEBUG_INFO("Changing input from %d to %d\n",
  441. cx->active_input, inp);
  442. cx->active_input = inp;
  443. /* Set the audio input to whatever is appropriate for the
  444. input type. */
  445. cx->audio_input = cx->card->video_inputs[inp].audio_index;
  446. /* prevent others from messing with the streams until
  447. we're finished changing inputs. */
  448. cx18_mute(cx);
  449. cx18_video_set_io(cx);
  450. cx18_audio_set_io(cx);
  451. cx18_unmute(cx);
  452. break;
  453. }
  454. case VIDIOC_G_FREQUENCY:{
  455. struct v4l2_frequency *vf = arg;
  456. if (vf->tuner != 0)
  457. return -EINVAL;
  458. cx18_call_i2c_clients(cx, cmd, arg);
  459. break;
  460. }
  461. case VIDIOC_S_FREQUENCY:{
  462. struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
  463. if (vf.tuner != 0)
  464. return -EINVAL;
  465. cx18_mute(cx);
  466. CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
  467. cx18_call_i2c_clients(cx, cmd, &vf);
  468. cx18_unmute(cx);
  469. break;
  470. }
  471. case VIDIOC_ENUMSTD:{
  472. struct v4l2_standard *vs = arg;
  473. int idx = vs->index;
  474. if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
  475. return -EINVAL;
  476. *vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
  477. cx18_std_60hz : cx18_std_50hz;
  478. vs->index = idx;
  479. vs->id = enum_stds[idx].std;
  480. strlcpy(vs->name, enum_stds[idx].name, sizeof(vs->name));
  481. break;
  482. }
  483. case VIDIOC_G_STD:{
  484. *(v4l2_std_id *) arg = cx->std;
  485. break;
  486. }
  487. case VIDIOC_S_STD: {
  488. v4l2_std_id std = *(v4l2_std_id *) arg;
  489. if ((std & V4L2_STD_ALL) == 0)
  490. return -EINVAL;
  491. if (std == cx->std)
  492. break;
  493. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
  494. atomic_read(&cx->capturing) > 0) {
  495. /* Switching standard would turn off the radio or mess
  496. with already running streams, prevent that by
  497. returning EBUSY. */
  498. return -EBUSY;
  499. }
  500. cx->std = std;
  501. cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
  502. cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
  503. cx->params.width = 720;
  504. cx->params.height = cx->is_50hz ? 576 : 480;
  505. cx->vbi.count = cx->is_50hz ? 18 : 12;
  506. cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
  507. cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
  508. cx->vbi.sliced_decoder_line_size = cx->is_60hz ? 272 : 284;
  509. CX18_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)cx->std);
  510. /* Tuner */
  511. cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
  512. break;
  513. }
  514. case VIDIOC_S_TUNER: { /* Setting tuner can only set audio mode */
  515. struct v4l2_tuner *vt = arg;
  516. if (vt->index != 0)
  517. return -EINVAL;
  518. cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
  519. break;
  520. }
  521. case VIDIOC_G_TUNER: {
  522. struct v4l2_tuner *vt = arg;
  523. if (vt->index != 0)
  524. return -EINVAL;
  525. memset(vt, 0, sizeof(*vt));
  526. cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
  527. if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  528. strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
  529. vt->type = V4L2_TUNER_RADIO;
  530. } else {
  531. strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
  532. vt->type = V4L2_TUNER_ANALOG_TV;
  533. }
  534. break;
  535. }
  536. case VIDIOC_G_SLICED_VBI_CAP: {
  537. struct v4l2_sliced_vbi_cap *cap = arg;
  538. int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
  539. int f, l;
  540. enum v4l2_buf_type type = cap->type;
  541. memset(cap, 0, sizeof(*cap));
  542. cap->type = type;
  543. if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  544. for (f = 0; f < 2; f++) {
  545. for (l = 0; l < 24; l++) {
  546. if (valid_service_line(f, l, cx->is_50hz))
  547. cap->service_lines[f][l] = set;
  548. }
  549. }
  550. return 0;
  551. }
  552. return -EINVAL;
  553. }
  554. case VIDIOC_ENCODER_CMD:
  555. case VIDIOC_TRY_ENCODER_CMD: {
  556. struct v4l2_encoder_cmd *enc = arg;
  557. int try = cmd == VIDIOC_TRY_ENCODER_CMD;
  558. memset(&enc->raw, 0, sizeof(enc->raw));
  559. switch (enc->cmd) {
  560. case V4L2_ENC_CMD_START:
  561. enc->flags = 0;
  562. if (try)
  563. return 0;
  564. return cx18_start_capture(id);
  565. case V4L2_ENC_CMD_STOP:
  566. enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
  567. if (try)
  568. return 0;
  569. cx18_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
  570. return 0;
  571. case V4L2_ENC_CMD_PAUSE:
  572. enc->flags = 0;
  573. if (try)
  574. return 0;
  575. if (!atomic_read(&cx->capturing))
  576. return -EPERM;
  577. if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  578. return 0;
  579. cx18_mute(cx);
  580. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, cx18_find_handle(cx));
  581. break;
  582. case V4L2_ENC_CMD_RESUME:
  583. enc->flags = 0;
  584. if (try)
  585. return 0;
  586. if (!atomic_read(&cx->capturing))
  587. return -EPERM;
  588. if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  589. return 0;
  590. cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, cx18_find_handle(cx));
  591. cx18_unmute(cx);
  592. break;
  593. default:
  594. return -EINVAL;
  595. }
  596. break;
  597. }
  598. case VIDIOC_LOG_STATUS:
  599. {
  600. struct v4l2_input vidin;
  601. struct v4l2_audio audin;
  602. int i;
  603. CX18_INFO("================= START STATUS CARD #%d =================\n", cx->num);
  604. if (cx->hw_flags & CX18_HW_TVEEPROM) {
  605. struct tveeprom tv;
  606. cx18_read_eeprom(cx, &tv);
  607. }
  608. cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
  609. cx18_get_input(cx, cx->active_input, &vidin);
  610. cx18_get_audio_input(cx, cx->audio_input, &audin);
  611. CX18_INFO("Video Input: %s\n", vidin.name);
  612. CX18_INFO("Audio Input: %s\n", audin.name);
  613. CX18_INFO("Tuner: %s\n",
  614. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ?
  615. "Radio" : "TV");
  616. cx2341x_log_status(&cx->params, cx->name);
  617. CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
  618. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  619. struct cx18_stream *s = &cx->streams[i];
  620. if (s->v4l2dev == NULL || s->buffers == 0)
  621. continue;
  622. CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
  623. s->name, s->s_flags,
  624. (s->buffers - s->q_free.buffers) * 100 / s->buffers,
  625. (s->buffers * s->buf_size) / 1024, s->buffers);
  626. }
  627. CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
  628. (long long)cx->mpg_data_received,
  629. (long long)cx->vbi_data_inserted);
  630. CX18_INFO("================== END STATUS CARD #%d ==================\n", cx->num);
  631. break;
  632. }
  633. default:
  634. return -EINVAL;
  635. }
  636. return 0;
  637. }
  638. static int cx18_v4l2_do_ioctl(struct inode *inode, struct file *filp,
  639. unsigned int cmd, void *arg)
  640. {
  641. struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
  642. struct cx18 *cx = id->cx;
  643. int ret;
  644. /* check priority */
  645. switch (cmd) {
  646. case VIDIOC_S_CTRL:
  647. case VIDIOC_S_STD:
  648. case VIDIOC_S_INPUT:
  649. case VIDIOC_S_TUNER:
  650. case VIDIOC_S_FREQUENCY:
  651. case VIDIOC_S_FMT:
  652. case VIDIOC_S_CROP:
  653. case VIDIOC_S_EXT_CTRLS:
  654. ret = v4l2_prio_check(&cx->prio, &id->prio);
  655. if (ret)
  656. return ret;
  657. }
  658. switch (cmd) {
  659. case VIDIOC_DBG_G_REGISTER:
  660. case VIDIOC_DBG_S_REGISTER:
  661. case VIDIOC_G_CHIP_IDENT:
  662. case VIDIOC_INT_S_AUDIO_ROUTING:
  663. case VIDIOC_INT_RESET:
  664. if (cx18_debug & CX18_DBGFLG_IOCTL) {
  665. printk(KERN_INFO "cx18%d ioctl: ", cx->num);
  666. v4l_printk_ioctl(cmd);
  667. }
  668. return cx18_debug_ioctls(filp, cmd, arg);
  669. case VIDIOC_G_PRIORITY:
  670. case VIDIOC_S_PRIORITY:
  671. case VIDIOC_QUERYCAP:
  672. case VIDIOC_ENUMINPUT:
  673. case VIDIOC_G_INPUT:
  674. case VIDIOC_S_INPUT:
  675. case VIDIOC_G_FMT:
  676. case VIDIOC_S_FMT:
  677. case VIDIOC_TRY_FMT:
  678. case VIDIOC_ENUM_FMT:
  679. case VIDIOC_CROPCAP:
  680. case VIDIOC_G_CROP:
  681. case VIDIOC_S_CROP:
  682. case VIDIOC_G_FREQUENCY:
  683. case VIDIOC_S_FREQUENCY:
  684. case VIDIOC_ENUMSTD:
  685. case VIDIOC_G_STD:
  686. case VIDIOC_S_STD:
  687. case VIDIOC_S_TUNER:
  688. case VIDIOC_G_TUNER:
  689. case VIDIOC_ENUMAUDIO:
  690. case VIDIOC_S_AUDIO:
  691. case VIDIOC_G_AUDIO:
  692. case VIDIOC_G_SLICED_VBI_CAP:
  693. case VIDIOC_LOG_STATUS:
  694. case VIDIOC_G_ENC_INDEX:
  695. case VIDIOC_ENCODER_CMD:
  696. case VIDIOC_TRY_ENCODER_CMD:
  697. if (cx18_debug & CX18_DBGFLG_IOCTL) {
  698. printk(KERN_INFO "cx18%d ioctl: ", cx->num);
  699. v4l_printk_ioctl(cmd);
  700. }
  701. return cx18_v4l2_ioctls(cx, filp, cmd, arg);
  702. case VIDIOC_QUERYMENU:
  703. case VIDIOC_QUERYCTRL:
  704. case VIDIOC_S_CTRL:
  705. case VIDIOC_G_CTRL:
  706. case VIDIOC_S_EXT_CTRLS:
  707. case VIDIOC_G_EXT_CTRLS:
  708. case VIDIOC_TRY_EXT_CTRLS:
  709. if (cx18_debug & CX18_DBGFLG_IOCTL) {
  710. printk(KERN_INFO "cx18%d ioctl: ", cx->num);
  711. v4l_printk_ioctl(cmd);
  712. }
  713. return cx18_control_ioctls(cx, cmd, arg);
  714. case 0x00005401: /* Handle isatty() calls */
  715. return -EINVAL;
  716. default:
  717. return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
  718. cx18_v4l2_do_ioctl);
  719. }
  720. return 0;
  721. }
  722. int cx18_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  723. unsigned long arg)
  724. {
  725. struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
  726. struct cx18 *cx = id->cx;
  727. int res;
  728. mutex_lock(&cx->serialize_lock);
  729. res = video_usercopy(inode, filp, cmd, arg, cx18_v4l2_do_ioctl);
  730. mutex_unlock(&cx->serialize_lock);
  731. return res;
  732. }