File Index
Files and directories
Sign in
FILE

cxusb-analog.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/usb/dvb-usb/cxusb-analog.c C
   1// SPDX-License-Identifier: GPL-2.0+
   2//
   3// DVB USB compliant linux driver for Conexant USB reference design -
   4// (analog part).
   5//
   6// Copyright (C) 2011, 2017, 2018
   7//	Maciej S. Szmigiero (mail@maciej.szmigiero.name)
   8//
   9// In case there are new analog / DVB-T hybrid devices released in the market
  10// using the same general design as Medion MD95700: a CX25840 video decoder
  11// outputting a BT.656 stream to a USB bridge chip which then forwards it to
  12// the host in isochronous USB packets this code should be made generic, with
  13// board specific bits implemented via separate card structures.
  14//
  15// This is, however, unlikely as the Medion model was released
  16// years ago (in 2005).
  17//
  18// TODO:
  19//  * audio support,
  20//  * finish radio support (requires audio of course),
  21//  * VBI support,
  22//  * controls support
  23
  24#include <linux/bitops.h>
  25#include <linux/device.h>
  26#include <linux/slab.h>
  27#include <linux/string.h>
  28#include <linux/ktime.h>
  29#include <linux/vmalloc.h>
  30#include <media/drv-intf/cx25840.h>
  31#include <media/tuner.h>
  32#include <media/v4l2-fh.h>
  33#include <media/v4l2-ioctl.h>
  34#include <media/v4l2-subdev.h>
  35#include <media/videobuf2-vmalloc.h>
  36
  37#include "cxusb.h"
  38
  39static int cxusb_medion_v_queue_setup(struct vb2_queue *q,
  40				      unsigned int *num_buffers,
  41				      unsigned int *num_planes,
  42				      unsigned int sizes[],
  43				      struct device *alloc_devs[])
  44{
  45	struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
  46	struct cxusb_medion_dev *cxdev = dvbdev->priv;
  47	unsigned int size = cxdev->width * cxdev->height * 2;
  48
  49	if (*num_planes > 0) {
  50		if (*num_planes != 1)
  51			return -EINVAL;
  52
  53		if (sizes[0] < size)
  54			return -EINVAL;
  55	} else {
  56		*num_planes = 1;
  57		sizes[0] = size;
  58	}
  59
  60	return 0;
  61}
  62
  63static int cxusb_medion_v_buf_init(struct vb2_buffer *vb)
  64{
  65	struct dvb_usb_device *dvbdev = vb2_get_drv_priv(vb->vb2_queue);
  66	struct cxusb_medion_dev *cxdev = dvbdev->priv;
  67
  68	cxusb_vprintk(dvbdev, OPS, "buffer init\n");
  69
  70	if (vb2_plane_size(vb, 0) < cxdev->width * cxdev->height * 2)
  71		return -ENOMEM;
  72
  73	cxusb_vprintk(dvbdev, OPS, "buffer OK\n");
  74
  75	return 0;
  76}
  77
  78static void cxusb_auxbuf_init(struct dvb_usb_device *dvbdev,
  79			      struct cxusb_medion_auxbuf *auxbuf,
  80			      u8 *buf, unsigned int len)
  81{
  82	cxusb_vprintk(dvbdev, AUXB, "initializing auxbuf of len %u\n", len);
  83
  84	auxbuf->buf = buf;
  85	auxbuf->len = len;
  86	auxbuf->paylen = 0;
  87}
  88
  89static void cxusb_auxbuf_head_trim(struct dvb_usb_device *dvbdev,
  90				   struct cxusb_medion_auxbuf *auxbuf,
  91				   unsigned int pos)
  92{
  93	if (pos == 0)
  94		return;
  95
  96	if (WARN_ON(pos > auxbuf->paylen))
  97		return;
  98
  99	cxusb_vprintk(dvbdev, AUXB,
 100		      "trimming auxbuf len by %u to %u\n",
 101		      pos, auxbuf->paylen - pos);
 102
 103	memmove(auxbuf->buf, auxbuf->buf + pos, auxbuf->paylen - pos);
 104	auxbuf->paylen -= pos;
 105}
 106
 107static unsigned int cxusb_auxbuf_paylen(struct cxusb_medion_auxbuf *auxbuf)
 108{
 109	return auxbuf->paylen;
 110}
 111
 112static bool cxusb_auxbuf_make_space(struct dvb_usb_device *dvbdev,
 113				    struct cxusb_medion_auxbuf *auxbuf,
 114				    unsigned int howmuch)
 115{
 116	unsigned int freespace;
 117
 118	if (WARN_ON(howmuch >= auxbuf->len))
 119		howmuch = auxbuf->len - 1;
 120
 121	freespace = auxbuf->len - cxusb_auxbuf_paylen(auxbuf);
 122
 123	cxusb_vprintk(dvbdev, AUXB, "freespace is %u\n", freespace);
 124
 125	if (freespace >= howmuch)
 126		return true;
 127
 128	howmuch -= freespace;
 129
 130	cxusb_vprintk(dvbdev, AUXB, "will overwrite %u bytes of buffer\n",
 131		      howmuch);
 132
 133	cxusb_auxbuf_head_trim(dvbdev, auxbuf, howmuch);
 134
 135	return false;
 136}
 137
 138/* returns false if some data was overwritten */
 139static bool cxusb_auxbuf_append_urb(struct dvb_usb_device *dvbdev,
 140				    struct cxusb_medion_auxbuf *auxbuf,
 141				    struct urb *urb)
 142{
 143	unsigned long len;
 144	int i;
 145	bool ret;
 146
 147	for (i = 0, len = 0; i < urb->number_of_packets; i++)
 148		len += urb->iso_frame_desc[i].actual_length;
 149
 150	ret = cxusb_auxbuf_make_space(dvbdev, auxbuf, len);
 151
 152	for (i = 0; i < urb->number_of_packets; i++) {
 153		unsigned int to_copy;
 154
 155		to_copy = urb->iso_frame_desc[i].actual_length;
 156
 157		memcpy(auxbuf->buf + auxbuf->paylen, urb->transfer_buffer +
 158		       urb->iso_frame_desc[i].offset, to_copy);
 159
 160		auxbuf->paylen += to_copy;
 161	}
 162
 163	return ret;
 164}
 165
 166static bool cxusb_auxbuf_copy(struct cxusb_medion_auxbuf *auxbuf,
 167			      unsigned int pos, unsigned char *dest,
 168			      unsigned int len)
 169{
 170	if (pos + len > auxbuf->paylen)
 171		return false;
 172
 173	memcpy(dest, auxbuf->buf + pos, len);
 174
 175	return true;
 176}
 177
 178static bool cxusb_medion_cf_refc_fld_chg(struct dvb_usb_device *dvbdev,
 179					 struct cxusb_bt656_params *bt656,
 180					 bool firstfield,
 181					 unsigned int maxlines,
 182					 unsigned int maxlinesamples,
 183					 unsigned char buf[4])
 184{
 185	bool firstfield_code = (buf[3] & CXUSB_BT656_FIELD_MASK) ==
 186		CXUSB_BT656_FIELD_1;
 187	unsigned int remlines;
 188
 189	if (bt656->line == 0 || firstfield == firstfield_code)
 190		return false;
 191
 192	if (bt656->fmode == LINE_SAMPLES) {
 193		unsigned int remsamples = maxlinesamples -
 194			bt656->linesamples;
 195
 196		cxusb_vprintk(dvbdev, BT656,
 197			      "field %c after line %u field change\n",
 198			      firstfield ? '1' : '2', bt656->line);
 199
 200		if (bt656->buf && remsamples > 0) {
 201			memset(bt656->buf, 0, remsamples);
 202			bt656->buf += remsamples;
 203
 204			cxusb_vprintk(dvbdev, BT656,
 205				      "field %c line %u %u samples still remaining (of %u)\n",
 206				      firstfield ? '1' : '2',
 207				      bt656->line, remsamples,
 208				      maxlinesamples);
 209		}
 210
 211		bt656->line++;
 212	}
 213
 214	remlines = maxlines - bt656->line;
 215	if (bt656->buf && remlines > 0) {
 216		memset(bt656->buf, 0, remlines * maxlinesamples);
 217		bt656->buf += remlines * maxlinesamples;
 218
 219		cxusb_vprintk(dvbdev, BT656,
 220			      "field %c %u lines still remaining (of %u)\n",
 221			      firstfield ? '1' : '2', remlines,
 222			      maxlines);
 223	}
 224
 225	return true;
 226}
 227
 228static void cxusb_medion_cf_refc_start_sch(struct dvb_usb_device *dvbdev,
 229					   struct cxusb_bt656_params *bt656,
 230					   bool firstfield,
 231					   unsigned char buf[4])
 232{
 233	bool firstfield_code = (buf[3] & CXUSB_BT656_FIELD_MASK) ==
 234		CXUSB_BT656_FIELD_1;
 235	bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
 236		CXUSB_BT656_SEAV_SAV;
 237	bool vbi_code = (buf[3] & CXUSB_BT656_VBI_MASK) ==
 238		CXUSB_BT656_VBI_ON;
 239
 240	if (!sav_code || firstfield != firstfield_code)
 241		return;
 242
 243	if (!vbi_code) {
 244		cxusb_vprintk(dvbdev, BT656, "line start @ pos %u\n",
 245			      bt656->pos);
 246
 247		bt656->linesamples = 0;
 248		bt656->fmode = LINE_SAMPLES;
 249	} else {
 250		cxusb_vprintk(dvbdev, BT656, "VBI start @ pos %u\n",
 251			      bt656->pos);
 252
 253		bt656->fmode = VBI_SAMPLES;
 254	}
 255}
 256
 257static void cxusb_medion_cf_refc_line_smpl(struct dvb_usb_device *dvbdev,
 258					   struct cxusb_bt656_params *bt656,
 259					   bool firstfield,
 260					   unsigned int maxlinesamples,
 261					   unsigned char buf[4])
 262{
 263	bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
 264		CXUSB_BT656_SEAV_SAV;
 265	unsigned int remsamples;
 266
 267	if (sav_code)
 268		cxusb_vprintk(dvbdev, BT656,
 269			      "SAV in line samples @ line %u, pos %u\n",
 270			      bt656->line, bt656->pos);
 271
 272	remsamples = maxlinesamples - bt656->linesamples;
 273	if (bt656->buf && remsamples > 0) {
 274		memset(bt656->buf, 0, remsamples);
 275		bt656->buf += remsamples;
 276
 277		cxusb_vprintk(dvbdev, BT656,
 278			      "field %c line %u %u samples still remaining (of %u)\n",
 279			      firstfield ? '1' : '2', bt656->line, remsamples,
 280			      maxlinesamples);
 281	}
 282
 283	bt656->fmode = START_SEARCH;
 284	bt656->line++;
 285}
 286
 287static void cxusb_medion_cf_refc_vbi_smpl(struct dvb_usb_device *dvbdev,
 288					  struct cxusb_bt656_params *bt656,
 289					  unsigned char buf[4])
 290{
 291	bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
 292		CXUSB_BT656_SEAV_SAV;
 293
 294	if (sav_code)
 295		cxusb_vprintk(dvbdev, BT656, "SAV in VBI samples @ pos %u\n",
 296			      bt656->pos);
 297
 298	bt656->fmode = START_SEARCH;
 299}
 300
 301/* returns whether the whole 4-byte code should be skipped in the buffer */
 302static bool cxusb_medion_cf_ref_code(struct dvb_usb_device *dvbdev,
 303				     struct cxusb_bt656_params *bt656,
 304				     bool firstfield,
 305				     unsigned int maxlines,
 306				     unsigned int maxlinesamples,
 307				     unsigned char buf[4])
 308{
 309	if (bt656->fmode == START_SEARCH) {
 310		cxusb_medion_cf_refc_start_sch(dvbdev, bt656, firstfield, buf);
 311	} else if (bt656->fmode == LINE_SAMPLES) {
 312		cxusb_medion_cf_refc_line_smpl(dvbdev, bt656, firstfield,
 313					       maxlinesamples, buf);
 314		return false;
 315	} else if (bt656->fmode == VBI_SAMPLES) {
 316		cxusb_medion_cf_refc_vbi_smpl(dvbdev, bt656, buf);
 317		return false;
 318	}
 319
 320	return true;
 321}
 322
 323static bool cxusb_medion_cs_start_sch(struct dvb_usb_device *dvbdev,
 324				      struct cxusb_medion_auxbuf *auxbuf,
 325				      struct cxusb_bt656_params *bt656,
 326				      unsigned int maxlinesamples)
 327{
 328	unsigned char buf[64];
 329	unsigned int idx;
 330	unsigned int tocheck = clamp_t(size_t, maxlinesamples / 4, 3,
 331				       sizeof(buf));
 332
 333	if (!cxusb_auxbuf_copy(auxbuf, bt656->pos + 1, buf, tocheck))
 334		return false;
 335
 336	for (idx = 0; idx <= tocheck - 3; idx++)
 337		if (memcmp(buf + idx, CXUSB_BT656_PREAMBLE, 3) == 0) {
 338			bt656->pos += (1 + idx);
 339			return true;
 340		}
 341
 342	cxusb_vprintk(dvbdev, BT656, "line %u early start, pos %u\n",
 343		      bt656->line, bt656->pos);
 344
 345	bt656->linesamples = 0;
 346	bt656->fmode = LINE_SAMPLES;
 347
 348	return true;
 349}
 350
 351static void cxusb_medion_cs_line_smpl(struct cxusb_bt656_params *bt656,
 352				      unsigned int maxlinesamples,
 353				      unsigned char val)
 354{
 355	if (bt656->buf)
 356		*(bt656->buf++) = val;
 357
 358	bt656->linesamples++;
 359	bt656->pos++;
 360
 361	if (bt656->linesamples >= maxlinesamples) {
 362		bt656->fmode = START_SEARCH;
 363		bt656->line++;
 364	}
 365}
 366
 367static bool cxusb_medion_copy_samples(struct dvb_usb_device *dvbdev,
 368				      struct cxusb_medion_auxbuf *auxbuf,
 369				      struct cxusb_bt656_params *bt656,
 370				      unsigned int maxlinesamples,
 371				      unsigned char val)
 372{
 373	if (bt656->fmode == START_SEARCH && bt656->line > 0)
 374		return cxusb_medion_cs_start_sch(dvbdev, auxbuf, bt656,
 375						 maxlinesamples);
 376	else if (bt656->fmode == LINE_SAMPLES)
 377		cxusb_medion_cs_line_smpl(bt656, maxlinesamples, val);
 378	else /* TODO: copy VBI samples */
 379		bt656->pos++;
 380
 381	return true;
 382}
 383
 384static bool cxusb_medion_copy_field(struct dvb_usb_device *dvbdev,
 385				    struct cxusb_medion_auxbuf *auxbuf,
 386				    struct cxusb_bt656_params *bt656,
 387				    bool firstfield,
 388				    unsigned int maxlines,
 389				    unsigned int maxlinesmpls)
 390{
 391	while (bt656->line < maxlines) {
 392		unsigned char val;
 393
 394		if (!cxusb_auxbuf_copy(auxbuf, bt656->pos, &val, 1))
 395			break;
 396
 397		if (val == CXUSB_BT656_PREAMBLE[0]) {
 398			unsigned char buf[4];
 399
 400			buf[0] = val;
 401			if (!cxusb_auxbuf_copy(auxbuf, bt656->pos + 1,
 402					       buf + 1, 3))
 403				break;
 404
 405			if (buf[1] == CXUSB_BT656_PREAMBLE[1] &&
 406			    buf[2] == CXUSB_BT656_PREAMBLE[2]) {
 407				/*
 408				 * is this a field change?
 409				 * if so, terminate copying the current field
 410				 */
 411				if (cxusb_medion_cf_refc_fld_chg(dvbdev,
 412								 bt656,
 413								 firstfield,
 414								 maxlines,
 415								 maxlinesmpls,
 416								 buf))
 417					return true;
 418
 419				if (cxusb_medion_cf_ref_code(dvbdev, bt656,
 420							     firstfield,
 421							     maxlines,
 422							     maxlinesmpls,
 423							     buf))
 424					bt656->pos += 4;
 425
 426				continue;
 427			}
 428		}
 429
 430		if (!cxusb_medion_copy_samples(dvbdev, auxbuf, bt656,
 431					       maxlinesmpls, val))
 432			break;
 433	}
 434
 435	if (bt656->line < maxlines) {
 436		cxusb_vprintk(dvbdev, BT656,
 437			      "end of buffer pos = %u, line = %u\n",
 438			      bt656->pos, bt656->line);
 439		return false;
 440	}
 441
 442	return true;
 443}
 444
 445static bool cxusb_medion_v_process_auxbuf(struct cxusb_medion_dev *cxdev,
 446					  bool reset)
 447{
 448	struct dvb_usb_device *dvbdev = cxdev->dvbdev;
 449	struct cxusb_bt656_params *bt656 = &cxdev->bt656;
 450
 451	/*
 452	 * if this is a new frame
 453	 * fetch a buffer from list
 454	 */
 455	if (bt656->mode == NEW_FRAME) {
 456		if (!list_empty(&cxdev->buflist)) {
 457			cxdev->vbuf =
 458				list_first_entry(&cxdev->buflist,
 459						 struct cxusb_medion_vbuffer,
 460						 list);
 461			list_del(&cxdev->vbuf->list);
 462		} else {
 463			dev_warn(&dvbdev->udev->dev, "no free buffers\n");
 464		}
 465	}
 466
 467	if (bt656->mode == NEW_FRAME || reset) {
 468		cxusb_vprintk(dvbdev, URB, "will copy field 1\n");
 469		bt656->pos = 0;
 470		bt656->mode = FIRST_FIELD;
 471		bt656->fmode = START_SEARCH;
 472		bt656->line = 0;
 473
 474		if (cxdev->vbuf) {
 475			cxdev->vbuf->vb2.vb2_buf.timestamp = ktime_get_ns();
 476			bt656->buf = vb2_plane_vaddr(&cxdev->vbuf->vb2.vb2_buf,
 477						     0);
 478		}
 479	}
 480
 481	if (bt656->mode == FIRST_FIELD) {
 482		if (!cxusb_medion_copy_field(dvbdev, &cxdev->auxbuf, bt656,
 483					     true, cxdev->height / 2,
 484					     cxdev->width * 2))
 485			return false;
 486
 487		/*
 488		 * do not trim buffer there in case
 489		 * we need to reset the search later
 490		 */
 491
 492		cxusb_vprintk(dvbdev, URB, "will copy field 2\n");
 493		bt656->mode = SECOND_FIELD;
 494		bt656->fmode = START_SEARCH;
 495		bt656->line = 0;
 496	}
 497
 498	if (bt656->mode == SECOND_FIELD) {
 499		if (!cxusb_medion_copy_field(dvbdev, &cxdev->auxbuf, bt656,
 500					     false, cxdev->height / 2,
 501					     cxdev->width * 2))
 502			return false;
 503
 504		cxusb_auxbuf_head_trim(dvbdev, &cxdev->auxbuf, bt656->pos);
 505
 506		bt656->mode = NEW_FRAME;
 507
 508		if (cxdev->vbuf) {
 509			vb2_set_plane_payload(&cxdev->vbuf->vb2.vb2_buf, 0,
 510					      cxdev->width * cxdev->height * 2);
 511
 512			cxdev->vbuf->vb2.field = cxdev->field_order;
 513			cxdev->vbuf->vb2.sequence = cxdev->vbuf_sequence++;
 514
 515			vb2_buffer_done(&cxdev->vbuf->vb2.vb2_buf,
 516					VB2_BUF_STATE_DONE);
 517
 518			cxdev->vbuf = NULL;
 519			cxdev->bt656.buf = NULL;
 520
 521			cxusb_vprintk(dvbdev, URB, "frame done\n");
 522		} else {
 523			cxusb_vprintk(dvbdev, URB, "frame skipped\n");
 524			cxdev->vbuf_sequence++;
 525		}
 526	}
 527
 528	return true;
 529}
 530
 531static bool cxusb_medion_v_complete_handle_urb(struct cxusb_medion_dev *cxdev,
 532					       bool *auxbuf_reset)
 533{
 534	struct dvb_usb_device *dvbdev = cxdev->dvbdev;
 535	unsigned int urbn;
 536	struct urb *urb;
 537	int ret;
 538
 539	*auxbuf_reset = false;
 540
 541	urbn = cxdev->nexturb;
 542	if (!test_bit(urbn, &cxdev->urbcomplete))
 543		return false;
 544
 545	clear_bit(urbn, &cxdev->urbcomplete);
 546
 547	do {
 548		cxdev->nexturb++;
 549		cxdev->nexturb %= CXUSB_VIDEO_URBS;
 550		urb = cxdev->streamurbs[cxdev->nexturb];
 551	} while (!urb);
 552
 553	urb = cxdev->streamurbs[urbn];
 554	cxusb_vprintk(dvbdev, URB, "URB %u status = %d\n", urbn, urb->status);
 555
 556	if (urb->status == 0 || urb->status == -EXDEV) {
 557		int i;
 558		unsigned long len;
 559
 560		for (i = 0, len = 0; i < urb->number_of_packets; i++)
 561			len += urb->iso_frame_desc[i].actual_length;
 562
 563		cxusb_vprintk(dvbdev, URB, "URB %u data len = %lu\n", urbn,
 564			      len);
 565
 566		if (len > 0) {
 567			cxusb_vprintk(dvbdev, URB, "appending URB\n");
 568
 569			/*
 570			 * append new data to auxbuf while
 571			 * overwriting old data if necessary
 572			 *
 573			 * if any overwrite happens then we can no
 574			 * longer rely on consistency of the whole
 575			 * data so let's start again the current
 576			 * auxbuf frame assembling process from
 577			 * the beginning
 578			 */
 579			*auxbuf_reset =
 580				!cxusb_auxbuf_append_urb(dvbdev,
 581							 &cxdev->auxbuf,
 582							 urb);
 583		}
 584	}
 585
 586	cxusb_vprintk(dvbdev, URB, "URB %u resubmit\n", urbn);
 587
 588	ret = usb_submit_urb(urb, GFP_KERNEL);
 589	if (ret != 0)
 590		dev_err(&dvbdev->udev->dev,
 591			"unable to resubmit URB %u (%d), you'll have to restart streaming\n",
 592			urbn, ret);
 593
 594	/* next URB is complete already? reschedule us then to handle it */
 595	return test_bit(cxdev->nexturb, &cxdev->urbcomplete);
 596}
 597
 598static void cxusb_medion_v_complete_work(struct work_struct *work)
 599{
 600	struct cxusb_medion_dev *cxdev = container_of(work,
 601						      struct cxusb_medion_dev,
 602						      urbwork);
 603	struct dvb_usb_device *dvbdev = cxdev->dvbdev;
 604	bool auxbuf_reset;
 605	bool reschedule;
 606
 607	mutex_lock(cxdev->videodev->lock);
 608
 609	cxusb_vprintk(dvbdev, URB, "worker called, stop_streaming = %d\n",
 610		      (int)cxdev->stop_streaming);
 611
 612	if (cxdev->stop_streaming)
 613		goto unlock;
 614
 615	reschedule = cxusb_medion_v_complete_handle_urb(cxdev, &auxbuf_reset);
 616
 617	if (cxusb_medion_v_process_auxbuf(cxdev, auxbuf_reset))
 618		/* reschedule us until auxbuf no longer can produce any frame */
 619		reschedule = true;
 620
 621	if (reschedule) {
 622		cxusb_vprintk(dvbdev, URB, "rescheduling worker\n");
 623		schedule_work(&cxdev->urbwork);
 624	}
 625
 626unlock:
 627	mutex_unlock(cxdev->videodev->lock);
 628}
 629
 630static void cxusb_medion_v_complete(struct urb *u)
 631{
 632	struct dvb_usb_device *dvbdev = u->context;
 633	struct cxusb_medion_dev *cxdev = dvbdev->priv;
 634	unsigned int i;
 635
 636	for (i = 0; i < CXUSB_VIDEO_URBS; i++)
 637		if (cxdev->streamurbs[i] == u)
 638			break;
 639
 640	if (i >= CXUSB_VIDEO_URBS) {
 641		dev_err(&dvbdev->udev->dev,
 642			"complete on unknown URB\n");
 643		return;
 644	}
 645
 646	cxusb_vprintk(dvbdev, URB, "URB %u complete\n", i);
 647
 648	set_bit(i, &cxdev->urbcomplete);
 649	schedule_work(&cxdev->urbwork);
 650}
 651
 652static void cxusb_medion_urbs_free(struct cxusb_medion_dev *cxdev)
 653{
 654	unsigned int i;
 655
 656	for (i = 0; i < CXUSB_VIDEO_URBS; i++)
 657		if (cxdev->streamurbs[i]) {
 658			kfree(cxdev->streamurbs[i]->transfer_buffer);
 659			usb_free_urb(cxdev->streamurbs[i]);
 660			cxdev->streamurbs[i] = NULL;
 661		}
 662}
 663
 664static void cxusb_medion_return_buffers(struct cxusb_medion_dev *cxdev,
 665					bool requeue)
 666{
 667	struct cxusb_medion_vbuffer *vbuf, *vbuf_tmp;
 668
 669	list_for_each_entry_safe(vbuf, vbuf_tmp, &cxdev->buflist,
 670				 list) {
 671		list_del(&vbuf->list);
 672		vb2_buffer_done(&vbuf->vb2.vb2_buf,
 673				requeue ? VB2_BUF_STATE_QUEUED :
 674				VB2_BUF_STATE_ERROR);
 675	}
 676
 677	if (cxdev->vbuf) {
 678		vb2_buffer_done(&cxdev->vbuf->vb2.vb2_buf,
 679				requeue ? VB2_BUF_STATE_QUEUED :
 680				VB2_BUF_STATE_ERROR);
 681
 682		cxdev->vbuf = NULL;
 683		cxdev->bt656.buf = NULL;
 684	}
 685}
 686
 687static int cxusb_medion_v_ss_auxbuf_alloc(struct cxusb_medion_dev *cxdev,
 688					  int *npackets)
 689{
 690	struct dvb_usb_device *dvbdev = cxdev->dvbdev;
 691	u8 *buf;
 692	unsigned int framelen, urblen, auxbuflen;
 693
 694	framelen = (cxdev->width * 2 + 4 + 4) *
 695		(cxdev->height + 50 /* VBI lines */);
 696
 697	/*
 698	 * try to fit a whole frame into each URB, as long as doing so
 699	 * does not require very high order memory allocations
 700	 */
 701	BUILD_BUG_ON(CXUSB_VIDEO_URB_MAX_SIZE / CXUSB_VIDEO_PKT_SIZE >
 702		     CXUSB_VIDEO_MAX_FRAME_PKTS);
 703	*npackets = min_t(int, (framelen + CXUSB_VIDEO_PKT_SIZE - 1) /
 704			  CXUSB_VIDEO_PKT_SIZE,
 705			  CXUSB_VIDEO_URB_MAX_SIZE / CXUSB_VIDEO_PKT_SIZE);
 706	urblen = *npackets * CXUSB_VIDEO_PKT_SIZE;
 707
 708	cxusb_vprintk(dvbdev, URB,
 709		      "each URB will have %d packets for total of %u bytes (%u x %u @ %u)\n",
 710		      *npackets, urblen, (unsigned int)cxdev->width,
 711		      (unsigned int)cxdev->height, framelen);
 712
 713	auxbuflen = framelen + urblen;
 714
 715	buf = vmalloc(auxbuflen);
 716	if (!buf)
 717		return -ENOMEM;
 718
 719	cxusb_auxbuf_init(dvbdev, &cxdev->auxbuf, buf, auxbuflen);
 720
 721	return 0;
 722}
 723
 724static u32 cxusb_medion_norm2field_order(v4l2_std_id norm)
 725{
 726	bool is625 = norm & V4L2_STD_625_50;
 727	bool is525 = norm & V4L2_STD_525_60;
 728
 729	if (!is625 && !is525)
 730		return V4L2_FIELD_NONE;
 731
 732	if (is625 && is525)
 733		return V4L2_FIELD_NONE;
 734
 735	if (is625)
 736		return V4L2_FIELD_SEQ_TB;
 737	else /* is525 */
 738		return V4L2_FIELD_SEQ_BT;
 739}
 740
 741static u32 cxusb_medion_field_order(struct cxusb_medion_dev *cxdev)
 742{
 743	struct dvb_usb_device *dvbdev = cxdev->dvbdev;
 744	u32 field;
 745	int ret;
 746	v4l2_std_id norm;
 747
 748	/* TV tuner is PAL-only so it is always TB */
 749	if (cxdev->input == 0)
 750		return V4L2_FIELD_SEQ_TB;
 751
 752	field = cxusb_medion_norm2field_order(cxdev->norm);
 753	if (field != V4L2_FIELD_NONE)
 754		return field;
 755
 756	ret = v4l2_subdev_call(cxdev->cx25840, video, g_std, &norm);
 757	if (ret != 0) {
 758		cxusb_vprintk(dvbdev, OPS,
 759			      "cannot get current standard for input %u\n",
 760			      (unsigned int)cxdev->input);
 761	} else {
 762		field = cxusb_medion_norm2field_order(norm);
 763		if (field != V4L2_FIELD_NONE)
 764			return field;
 765	}
 766
 767	dev_warn(&dvbdev->udev->dev,
 768		 "cannot determine field order for the current standard setup and received signal, using TB\n");
 769	return V4L2_FIELD_SEQ_TB;
 770}
 771
 772static int cxusb_medion_v_start_streaming(struct vb2_queue *q,
 773					  unsigned int count)
 774{
 775	struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
 776	struct cxusb_medion_dev *cxdev = dvbdev->priv;
 777	u8 streamon_params[2] = { 0x03, 0x00 };
 778	int npackets, i;
 779	int ret;
 780
 781	cxusb_vprintk(dvbdev, OPS, "should start streaming\n");
 782
 783	if (cxdev->stop_streaming) {
 784		/* stream is being stopped */
 785		ret = -EBUSY;
 786		goto ret_retbufs;
 787	}
 788
 789	cxdev->field_order = cxusb_medion_field_order(cxdev);
 790
 791	ret = v4l2_subdev_call(cxdev->cx25840, video, s_stream, 1);
 792	if (ret != 0) {
 793		dev_err(&dvbdev->udev->dev,
 794			"unable to start stream (%d)\n", ret);
 795		goto ret_retbufs;
 796	}
 797
 798	ret = cxusb_ctrl_msg(dvbdev, CMD_STREAMING_ON, streamon_params, 2,
 799			     NULL, 0);
 800	if (ret != 0) {
 801		dev_err(&dvbdev->udev->dev,
 802			"unable to start streaming (%d)\n", ret);
 803		goto ret_unstream_cx;
 804	}
 805
 806	ret = cxusb_medion_v_ss_auxbuf_alloc(cxdev, &npackets);
 807	if (ret != 0)
 808		goto ret_unstream_md;
 809
 810	for (i = 0; i < CXUSB_VIDEO_URBS; i++) {
 811		int framen;
 812		u8 *streambuf;
 813		struct urb *surb;
 814
 815		/*
 816		 * TODO: change this to an array of single pages to avoid
 817		 * doing a large continuous allocation when (if)
 818		 * s-g isochronous USB transfers are supported
 819		 */
 820		streambuf = kmalloc_array(npackets, CXUSB_VIDEO_PKT_SIZE,
 821					  GFP_KERNEL);
 822		if (!streambuf) {
 823			if (i < 2) {
 824				ret = -ENOMEM;
 825				goto ret_freeab;
 826			}
 827			break;
 828		}
 829
 830		surb = usb_alloc_urb(npackets, GFP_KERNEL);
 831		if (!surb) {
 832			kfree(streambuf);
 833			ret = -ENOMEM;
 834			goto ret_freeu;
 835		}
 836
 837		cxdev->streamurbs[i] = surb;
 838		surb->dev = dvbdev->udev;
 839		surb->context = dvbdev;
 840		surb->pipe = usb_rcvisocpipe(dvbdev->udev, 2);
 841
 842		surb->interval = 1;
 843		surb->transfer_flags = URB_ISO_ASAP;
 844
 845		surb->transfer_buffer = streambuf;
 846
 847		surb->complete = cxusb_medion_v_complete;
 848		surb->number_of_packets = npackets;
 849		surb->transfer_buffer_length = npackets * CXUSB_VIDEO_PKT_SIZE;
 850
 851		for (framen = 0; framen < npackets; framen++) {
 852			surb->iso_frame_desc[framen].offset =
 853				CXUSB_VIDEO_PKT_SIZE * framen;
 854
 855			surb->iso_frame_desc[framen].length =
 856				CXUSB_VIDEO_PKT_SIZE;
 857		}
 858	}
 859
 860	cxdev->urbcomplete = 0;
 861	cxdev->nexturb = 0;
 862	cxdev->vbuf_sequence = 0;
 863
 864	cxdev->vbuf = NULL;
 865	cxdev->bt656.mode = NEW_FRAME;
 866	cxdev->bt656.buf = NULL;
 867
 868	for (i = 0; i < CXUSB_VIDEO_URBS; i++)
 869		if (cxdev->streamurbs[i]) {
 870			ret = usb_submit_urb(cxdev->streamurbs[i],
 871					     GFP_KERNEL);
 872			if (ret != 0)
 873				dev_err(&dvbdev->udev->dev,
 874					"URB %d submission failed (%d)\n", i,
 875					ret);
 876		}
 877
 878	return 0;
 879
 880ret_freeu:
 881	cxusb_medion_urbs_free(cxdev);
 882
 883ret_freeab:
 884	vfree(cxdev->auxbuf.buf);
 885
 886ret_unstream_md:
 887	cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
 888
 889ret_unstream_cx:
 890	v4l2_subdev_call(cxdev->cx25840, video, s_stream, 0);
 891
 892ret_retbufs:
 893	cxusb_medion_return_buffers(cxdev, true);
 894
 895	return ret;
 896}
 897
 898static void cxusb_medion_v_stop_streaming(struct vb2_queue *q)
 899{
 900	struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
 901	struct cxusb_medion_dev *cxdev = dvbdev->priv;
 902	int ret;
 903	unsigned int i;
 904
 905	cxusb_vprintk(dvbdev, OPS, "should stop streaming\n");
 906
 907	if (WARN_ON(cxdev->stop_streaming))
 908		return;
 909
 910	cxdev->stop_streaming = true;
 911
 912	cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
 913
 914	ret = v4l2_subdev_call(cxdev->cx25840, video, s_stream, 0);
 915	if (ret != 0)
 916		dev_err(&dvbdev->udev->dev, "unable to stop stream (%d)\n",
 917			ret);
 918
 919	/* let URB completion run */
 920	mutex_unlock(cxdev->videodev->lock);
 921
 922	for (i = 0; i < CXUSB_VIDEO_URBS; i++)
 923		if (cxdev->streamurbs[i])
 924			usb_kill_urb(cxdev->streamurbs[i]);
 925
 926	flush_work(&cxdev->urbwork);
 927
 928	mutex_lock(cxdev->videodev->lock);
 929
 930	/* free transfer buffer and URB */
 931	vfree(cxdev->auxbuf.buf);
 932
 933	cxusb_medion_urbs_free(cxdev);
 934
 935	cxusb_medion_return_buffers(cxdev, false);
 936
 937	cxdev->stop_streaming = false;
 938}
 939
 940static void cxusub_medion_v_buf_queue(struct vb2_buffer *vb)
 941{
 942	struct vb2_v4l2_buffer *v4l2buf = to_vb2_v4l2_buffer(vb);
 943	struct cxusb_medion_vbuffer *vbuf =
 944		container_of(v4l2buf, struct cxusb_medion_vbuffer, vb2);
 945	struct dvb_usb_device *dvbdev = vb2_get_drv_priv(vb->vb2_queue);
 946	struct cxusb_medion_dev *cxdev = dvbdev->priv;
 947
 948	/* cxusb_vprintk(dvbdev, OPS, "mmmm.. a fresh buffer...\n"); */
 949
 950	list_add_tail(&vbuf->list, &cxdev->buflist);
 951}
 952
 953static const struct vb2_ops cxdev_video_qops = {
 954	.queue_setup = cxusb_medion_v_queue_setup,
 955	.buf_init = cxusb_medion_v_buf_init,
 956	.start_streaming = cxusb_medion_v_start_streaming,
 957	.stop_streaming = cxusb_medion_v_stop_streaming,
 958	.buf_queue = cxusub_medion_v_buf_queue,
 959};
 960
 961static const __u32 videocaps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
 962	V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
 963static const __u32 radiocaps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
 964
 965static int cxusb_medion_v_querycap(struct file *file, void *fh,
 966				   struct v4l2_capability *cap)
 967{
 968	struct dvb_usb_device *dvbdev = video_drvdata(file);
 969
 970	strscpy(cap->driver, dvbdev->udev->dev.driver->name,
 971		sizeof(cap->driver));
 972	strscpy(cap->card, "Medion 95700", sizeof(cap->card));
 973	usb_make_path(dvbdev->udev, cap->bus_info, sizeof(cap->bus_info));
 974
 975	cap->capabilities = videocaps | radiocaps | V4L2_CAP_DEVICE_CAPS;
 976
 977	return 0;
 978}
 979
 980static int cxusb_medion_v_enum_fmt_vid_cap(struct file *file, void *fh,
 981					   struct v4l2_fmtdesc *f)
 982{
 983	if (f->index != 0)
 984		return -EINVAL;
 985
 986	f->pixelformat = V4L2_PIX_FMT_UYVY;
 987
 988	return 0;
 989}
 990
 991static int cxusb_medion_g_fmt_vid_cap(struct file *file, void *fh,
 992				      struct v4l2_format *f)
 993{
 994	struct dvb_usb_device *dvbdev = video_drvdata(file);
 995	struct cxusb_medion_dev *cxdev = dvbdev->priv;
 996
 997	f->fmt.pix.width = cxdev->width;
 998	f->fmt.pix.height = cxdev->height;
 999	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1000	f->fmt.pix.field = vb2_start_streaming_called(&cxdev->videoqueue) ?
1001		cxdev->field_order : cxusb_medion_field_order(cxdev);
1002	f->fmt.pix.bytesperline = cxdev->width * 2;
1003	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1004	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
1005
1006	return 0;
1007}
1008
1009static int cxusb_medion_try_s_fmt_vid_cap(struct file *file,
1010					  struct v4l2_format *f,
1011					  bool isset)
1012{
1013	struct dvb_usb_device *dvbdev = video_drvdata(file);
1014	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1015	struct v4l2_subdev_format subfmt = {
1016		.which = isset ? V4L2_SUBDEV_FORMAT_ACTIVE :
1017			 V4L2_SUBDEV_FORMAT_TRY,
1018	};
1019	u32 field;
1020	int ret;
1021
1022	if (isset && vb2_is_busy(&cxdev->videoqueue))
1023		return -EBUSY;
1024
1025	field = vb2_start_streaming_called(&cxdev->videoqueue) ?
1026		cxdev->field_order : cxusb_medion_field_order(cxdev);
1027
1028	subfmt.format.width = f->fmt.pix.width & ~1;
1029	subfmt.format.height = f->fmt.pix.height & ~1;
1030	subfmt.format.code = MEDIA_BUS_FMT_FIXED;
1031	subfmt.format.field = field;
1032	subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
1033
1034	ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
1035	if (ret != 0)
1036		return ret;
1037
1038	f->fmt.pix.width = subfmt.format.width;
1039	f->fmt.pix.height = subfmt.format.height;
1040	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1041	f->fmt.pix.field = field;
1042	f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
1043	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
1044	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1045
1046	if (isset) {
1047		cxdev->width = f->fmt.pix.width;
1048		cxdev->height = f->fmt.pix.height;
1049	}
1050
1051	return 0;
1052}
1053
1054static int cxusb_medion_try_fmt_vid_cap(struct file *file, void *fh,
1055					struct v4l2_format *f)
1056{
1057	return cxusb_medion_try_s_fmt_vid_cap(file, f, false);
1058}
1059
1060static int cxusb_medion_s_fmt_vid_cap(struct file *file, void *fh,
1061				      struct v4l2_format *f)
1062{
1063	return cxusb_medion_try_s_fmt_vid_cap(file, f, true);
1064}
1065
1066static const struct {
1067	struct v4l2_input input;
1068	u32 inputcfg;
1069} cxusb_medion_inputs[] = {
1070	{ .input = { .name = "TV tuner", .type = V4L2_INPUT_TYPE_TUNER,
1071		     .tuner = 0, .std = V4L2_STD_PAL },
1072	  .inputcfg = CX25840_COMPOSITE2, },
1073
1074	{  .input = { .name = "Composite", .type = V4L2_INPUT_TYPE_CAMERA,
1075		     .std = V4L2_STD_ALL },
1076	   .inputcfg = CX25840_COMPOSITE1, },
1077
1078	{  .input = { .name = "S-Video", .type = V4L2_INPUT_TYPE_CAMERA,
1079		      .std = V4L2_STD_ALL },
1080	   .inputcfg = CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 }
1081};
1082
1083#define CXUSB_INPUT_CNT ARRAY_SIZE(cxusb_medion_inputs)
1084
1085static int cxusb_medion_enum_input(struct file *file, void *fh,
1086				   struct v4l2_input *inp)
1087{
1088	struct dvb_usb_device *dvbdev = video_drvdata(file);
1089	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1090	u32 index = inp->index;
1091
1092	if (index >= CXUSB_INPUT_CNT)
1093		return -EINVAL;
1094
1095	*inp = cxusb_medion_inputs[index].input;
1096	inp->index = index;
1097	inp->capabilities |= V4L2_IN_CAP_STD;
1098
1099	if (index == cxdev->input) {
1100		int ret;
1101		u32 status = 0;
1102
1103		ret = v4l2_subdev_call(cxdev->cx25840, video, g_input_status,
1104				       &status);
1105		if (ret != 0)
1106			dev_warn(&dvbdev->udev->dev,
1107				 "cx25840 input status query failed (%d)\n",
1108				 ret);
1109		else
1110			inp->status = status;
1111	}
1112
1113	return 0;
1114}
1115
1116static int cxusb_medion_g_input(struct file *file, void *fh,
1117				unsigned int *i)
1118{
1119	struct dvb_usb_device *dvbdev = video_drvdata(file);
1120	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1121
1122	*i = cxdev->input;
1123
1124	return 0;
1125}
1126
1127static int cxusb_medion_set_norm(struct cxusb_medion_dev *cxdev,
1128				 v4l2_std_id norm)
1129{
1130	struct dvb_usb_device *dvbdev = cxdev->dvbdev;
1131	int ret;
1132
1133	cxusb_vprintk(dvbdev, OPS,
1134		      "trying to set standard for input %u to %lx\n",
1135		      (unsigned int)cxdev->input,
1136		      (unsigned long)norm);
1137
1138	/* no autodetection support */
1139	if (norm == V4L2_STD_UNKNOWN)
1140		return -EINVAL;
1141
1142	/* on composite or S-Video any std is acceptable */
1143	if (cxdev->input != 0) {
1144		ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
1145		if (ret)
1146			return ret;
1147
1148		goto ret_savenorm;
1149	}
1150
1151	/* TV tuner is only able to demodulate PAL */
1152	if ((norm & ~V4L2_STD_PAL) != 0)
1153		return -EINVAL;
1154
1155	ret = v4l2_subdev_call(cxdev->tda9887, video, s_std, norm);
1156	if (ret != 0) {
1157		dev_err(&dvbdev->udev->dev,
1158			"tda9887 norm setup failed (%d)\n",
1159			ret);
1160		return ret;
1161	}
1162
1163	ret = v4l2_subdev_call(cxdev->tuner, video, s_std, norm);
1164	if (ret != 0) {
1165		dev_err(&dvbdev->udev->dev,
1166			"tuner norm setup failed (%d)\n",
1167			ret);
1168		return ret;
1169	}
1170
1171	ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
1172	if (ret != 0) {
1173		dev_err(&dvbdev->udev->dev,
1174			"cx25840 norm setup failed (%d)\n",
1175			ret);
1176		return ret;
1177	}
1178
1179ret_savenorm:
1180	cxdev->norm = norm;
1181
1182	return 0;
1183}
1184
1185static int cxusb_medion_s_input(struct file *file, void *fh,
1186				unsigned int i)
1187{
1188	struct dvb_usb_device *dvbdev = video_drvdata(file);
1189	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1190	int ret;
1191	v4l2_std_id norm;
1192
1193	if (i >= CXUSB_INPUT_CNT)
1194		return -EINVAL;
1195
1196	ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
1197			       cxusb_medion_inputs[i].inputcfg, 0, 0);
1198	if (ret != 0)
1199		return ret;
1200
1201	cxdev->input = i;
1202	cxdev->videodev->tvnorms = cxusb_medion_inputs[i].input.std;
1203
1204	norm = cxdev->norm & cxusb_medion_inputs[i].input.std;
1205	if (norm == 0)
1206		norm = cxusb_medion_inputs[i].input.std;
1207
1208	cxusb_medion_set_norm(cxdev, norm);
1209
1210	return 0;
1211}
1212
1213static int cxusb_medion_g_tuner(struct file *file, void *fh,
1214				struct v4l2_tuner *tuner)
1215{
1216	struct dvb_usb_device *dvbdev = video_drvdata(file);
1217	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1218	struct video_device *vdev = video_devdata(file);
1219	int ret;
1220
1221	if (tuner->index != 0)
1222		return -EINVAL;
1223
1224	if (vdev->vfl_type == VFL_TYPE_VIDEO)
1225		tuner->type = V4L2_TUNER_ANALOG_TV;
1226	else
1227		tuner->type = V4L2_TUNER_RADIO;
1228
1229	tuner->capability = 0;
1230	tuner->afc = 0;
1231
1232	/*
1233	 * fills:
1234	 * always: capability (static), rangelow (static), rangehigh (static)
1235	 * radio mode: afc (may fail silently), rxsubchans (static), audmode
1236	 */
1237	ret = v4l2_subdev_call(cxdev->tda9887, tuner, g_tuner, tuner);
1238	if (ret != 0)
1239		return ret;
1240
1241	/*
1242	 * fills:
1243	 * always: capability (static), rangelow (static), rangehigh (static)
1244	 * radio mode: rxsubchans (always stereo), audmode,
1245	 * signal (might be wrong)
1246	 */
1247	ret = v4l2_subdev_call(cxdev->tuner, tuner, g_tuner, tuner);
1248	if (ret != 0)
1249		return ret;
1250
1251	tuner->signal = 0;
1252
1253	/*
1254	 * fills: TV mode: capability, rxsubchans, audmode, signal
1255	 */
1256	ret = v4l2_subdev_call(cxdev->cx25840, tuner, g_tuner, tuner);
1257	if (ret != 0)
1258		return ret;
1259
1260	if (vdev->vfl_type == VFL_TYPE_VIDEO)
1261		strscpy(tuner->name, "TV tuner", sizeof(tuner->name));
1262	else
1263		strscpy(tuner->name, "Radio tuner", sizeof(tuner->name));
1264
1265	memset(tuner->reserved, 0, sizeof(tuner->reserved));
1266
1267	return 0;
1268}
1269
1270static int cxusb_medion_s_tuner(struct file *file, void *fh,
1271				const struct v4l2_tuner *tuner)
1272{
1273	struct dvb_usb_device *dvbdev = video_drvdata(file);
1274	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1275	struct video_device *vdev = video_devdata(file);
1276	int ret;
1277
1278	if (tuner->index != 0)
1279		return -EINVAL;
1280
1281	ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_tuner, tuner);
1282	if (ret != 0)
1283		return ret;
1284
1285	ret = v4l2_subdev_call(cxdev->tuner, tuner, s_tuner, tuner);
1286	if (ret != 0)
1287		return ret;
1288
1289	/*
1290	 * make sure that cx25840 is in a correct TV / radio mode,
1291	 * since calls above may have changed it for tuner / IF demod
1292	 */
1293	if (vdev->vfl_type == VFL_TYPE_VIDEO)
1294		v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
1295	else
1296		v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
1297
1298	return v4l2_subdev_call(cxdev->cx25840, tuner, s_tuner, tuner);
1299}
1300
1301static int cxusb_medion_g_frequency(struct file *file, void *fh,
1302				    struct v4l2_frequency *freq)
1303{
1304	struct dvb_usb_device *dvbdev = video_drvdata(file);
1305	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1306
1307	if (freq->tuner != 0)
1308		return -EINVAL;
1309
1310	return v4l2_subdev_call(cxdev->tuner, tuner, g_frequency, freq);
1311}
1312
1313static int cxusb_medion_s_frequency(struct file *file, void *fh,
1314				    const struct v4l2_frequency *freq)
1315{
1316	struct dvb_usb_device *dvbdev = video_drvdata(file);
1317	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1318	struct video_device *vdev = video_devdata(file);
1319	int ret;
1320
1321	if (freq->tuner != 0)
1322		return -EINVAL;
1323
1324	ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_frequency, freq);
1325	if (ret != 0)
1326		return ret;
1327
1328	ret = v4l2_subdev_call(cxdev->tuner, tuner, s_frequency, freq);
1329	if (ret != 0)
1330		return ret;
1331
1332	/*
1333	 * make sure that cx25840 is in a correct TV / radio mode,
1334	 * since calls above may have changed it for tuner / IF demod
1335	 */
1336	if (vdev->vfl_type == VFL_TYPE_VIDEO)
1337		v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
1338	else
1339		v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
1340
1341	return v4l2_subdev_call(cxdev->cx25840, tuner, s_frequency, freq);
1342}
1343
1344static int cxusb_medion_g_std(struct file *file, void *fh,
1345			      v4l2_std_id *norm)
1346{
1347	struct dvb_usb_device *dvbdev = video_drvdata(file);
1348	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1349
1350	*norm = cxdev->norm;
1351
1352	if (*norm == V4L2_STD_UNKNOWN)
1353		return -ENODATA;
1354
1355	return 0;
1356}
1357
1358static int cxusb_medion_s_std(struct file *file, void *fh,
1359			      v4l2_std_id norm)
1360{
1361	struct dvb_usb_device *dvbdev = video_drvdata(file);
1362	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1363
1364	return cxusb_medion_set_norm(cxdev, norm);
1365}
1366
1367static int cxusb_medion_querystd(struct file *file, void *fh,
1368				 v4l2_std_id *norm)
1369{
1370	struct dvb_usb_device *dvbdev = video_drvdata(file);
1371	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1372	v4l2_std_id norm_mask;
1373	int ret;
1374
1375	/*
1376	 * make sure we don't have improper std bits set for the TV tuner
1377	 * (could happen when no signal was present yet after reset)
1378	 */
1379	if (cxdev->input == 0)
1380		norm_mask = V4L2_STD_PAL;
1381	else
1382		norm_mask = V4L2_STD_ALL;
1383
1384	ret = v4l2_subdev_call(cxdev->cx25840, video, querystd, norm);
1385	if (ret != 0) {
1386		cxusb_vprintk(dvbdev, OPS,
1387			      "cannot get detected standard for input %u\n",
1388			      (unsigned int)cxdev->input);
1389		return ret;
1390	}
1391
1392	cxusb_vprintk(dvbdev, OPS, "input %u detected standard is %lx\n",
1393		      (unsigned int)cxdev->input, (unsigned long)*norm);
1394	*norm &= norm_mask;
1395
1396	return 0;
1397}
1398
1399static int cxusb_medion_log_status(struct file *file, void *fh)
1400{
1401	struct dvb_usb_device *dvbdev = video_drvdata(file);
1402	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1403
1404	v4l2_device_call_all(&cxdev->v4l2dev, 0, core, log_status);
1405
1406	return 0;
1407}
1408
1409static const struct v4l2_ioctl_ops cxusb_video_ioctl = {
1410	.vidioc_querycap = cxusb_medion_v_querycap,
1411	.vidioc_enum_fmt_vid_cap = cxusb_medion_v_enum_fmt_vid_cap,
1412	.vidioc_g_fmt_vid_cap = cxusb_medion_g_fmt_vid_cap,
1413	.vidioc_s_fmt_vid_cap = cxusb_medion_s_fmt_vid_cap,
1414	.vidioc_try_fmt_vid_cap = cxusb_medion_try_fmt_vid_cap,
1415	.vidioc_enum_input = cxusb_medion_enum_input,
1416	.vidioc_g_input = cxusb_medion_g_input,
1417	.vidioc_s_input = cxusb_medion_s_input,
1418	.vidioc_g_tuner = cxusb_medion_g_tuner,
1419	.vidioc_s_tuner = cxusb_medion_s_tuner,
1420	.vidioc_g_frequency = cxusb_medion_g_frequency,
1421	.vidioc_s_frequency = cxusb_medion_s_frequency,
1422	.vidioc_g_std = cxusb_medion_g_std,
1423	.vidioc_s_std = cxusb_medion_s_std,
1424	.vidioc_querystd = cxusb_medion_querystd,
1425	.vidioc_log_status = cxusb_medion_log_status,
1426	.vidioc_reqbufs = vb2_ioctl_reqbufs,
1427	.vidioc_querybuf = vb2_ioctl_querybuf,
1428	.vidioc_qbuf = vb2_ioctl_qbuf,
1429	.vidioc_dqbuf = vb2_ioctl_dqbuf,
1430	.vidioc_create_bufs = vb2_ioctl_create_bufs,
1431	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1432	.vidioc_streamon = vb2_ioctl_streamon,
1433	.vidioc_streamoff = vb2_ioctl_streamoff
1434};
1435
1436static const struct v4l2_ioctl_ops cxusb_radio_ioctl = {
1437	.vidioc_querycap = cxusb_medion_v_querycap,
1438	.vidioc_g_tuner = cxusb_medion_g_tuner,
1439	.vidioc_s_tuner = cxusb_medion_s_tuner,
1440	.vidioc_g_frequency = cxusb_medion_g_frequency,
1441	.vidioc_s_frequency = cxusb_medion_s_frequency,
1442	.vidioc_log_status = cxusb_medion_log_status
1443};
1444
1445/*
1446 * in principle, this should be const, but s_io_pin_config is declared
1447 * to take non-const, and gcc complains
1448 */
1449static struct v4l2_subdev_io_pin_config cxusub_medion_pin_config[] = {
1450	{ .pin = CX25840_PIN_DVALID_PRGM0, .function = CX25840_PAD_DEFAULT,
1451	  .strength = CX25840_PIN_DRIVE_MEDIUM },
1452	{ .pin = CX25840_PIN_PLL_CLK_PRGM7, .function = CX25840_PAD_AUX_PLL },
1453	{ .pin = CX25840_PIN_HRESET_PRGM2, .function = CX25840_PAD_ACTIVE,
1454	  .strength = CX25840_PIN_DRIVE_MEDIUM }
1455};
1456
1457int cxusb_medion_analog_init(struct dvb_usb_device *dvbdev)
1458{
1459	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1460	u8 tuner_analog_msg_data[] = { 0x9c, 0x60, 0x85, 0x54 };
1461	struct i2c_msg tuner_analog_msg = { .addr = 0x61, .flags = 0,
1462					    .buf = tuner_analog_msg_data,
1463					    .len =
1464					    sizeof(tuner_analog_msg_data) };
1465	struct v4l2_subdev_format subfmt = {
1466		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
1467	};
1468	int ret;
1469
1470	/* switch tuner to analog mode so IF demod will become accessible */
1471	ret = i2c_transfer(&dvbdev->i2c_adap, &tuner_analog_msg, 1);
1472	if (ret != 1)
1473		dev_warn(&dvbdev->udev->dev,
1474			 "tuner analog switch failed (%d)\n", ret);
1475
1476	/*
1477	 * cx25840 might have lost power during mode switching so we need
1478	 * to set it again
1479	 */
1480	ret = v4l2_subdev_call(cxdev->cx25840, core, reset, 0);
1481	if (ret != 0)
1482		dev_warn(&dvbdev->udev->dev,
1483			 "cx25840 reset failed (%d)\n", ret);
1484
1485	ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
1486			       CX25840_COMPOSITE1, 0, 0);
1487	if (ret != 0)
1488		dev_warn(&dvbdev->udev->dev,
1489			 "cx25840 initial input setting failed (%d)\n", ret);
1490
1491	/* composite */
1492	cxdev->input = 1;
1493	cxdev->videodev->tvnorms = V4L2_STD_ALL;
1494	cxdev->norm = V4L2_STD_PAL;
1495
1496	/* TODO: setup audio samples insertion */
1497
1498	ret = v4l2_subdev_call(cxdev->cx25840, core, s_io_pin_config,
1499			       ARRAY_SIZE(cxusub_medion_pin_config),
1500			       cxusub_medion_pin_config);
1501	if (ret != 0)
1502		dev_warn(&dvbdev->udev->dev,
1503			 "cx25840 pin config failed (%d)\n", ret);
1504
1505	/* make sure that we aren't in radio mode */
1506	v4l2_subdev_call(cxdev->tda9887, video, s_std, cxdev->norm);
1507	v4l2_subdev_call(cxdev->tuner, video, s_std, cxdev->norm);
1508	v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
1509
1510	subfmt.format.width = cxdev->width;
1511	subfmt.format.height = cxdev->height;
1512	subfmt.format.code = MEDIA_BUS_FMT_FIXED;
1513	subfmt.format.field = V4L2_FIELD_SEQ_TB;
1514	subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
1515
1516	ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
1517	if (ret != 0)
1518		dev_warn(&dvbdev->udev->dev,
1519			 "cx25840 format set failed (%d)\n", ret);
1520
1521	if (ret == 0) {
1522		cxdev->width = subfmt.format.width;
1523		cxdev->height = subfmt.format.height;
1524	}
1525
1526	return 0;
1527}
1528
1529static int cxusb_videoradio_open(struct file *f)
1530{
1531	struct dvb_usb_device *dvbdev = video_drvdata(f);
1532	int ret;
1533
1534	/*
1535	 * no locking needed since this call only modifies analog
1536	 * state if there are no other analog handles currenly
1537	 * opened so ops done via them cannot create a conflict
1538	 */
1539	ret = cxusb_medion_get(dvbdev, CXUSB_OPEN_ANALOG);
1540	if (ret != 0)
1541		return ret;
1542
1543	ret = v4l2_fh_open(f);
1544	if (ret != 0)
1545		goto ret_release;
1546
1547	cxusb_vprintk(dvbdev, OPS, "got open\n");
1548
1549	return 0;
1550
1551ret_release:
1552	cxusb_medion_put(dvbdev);
1553
1554	return ret;
1555}
1556
1557static int cxusb_videoradio_release(struct file *f)
1558{
1559	struct video_device *vdev = video_devdata(f);
1560	struct dvb_usb_device *dvbdev = video_drvdata(f);
1561	int ret;
1562
1563	cxusb_vprintk(dvbdev, OPS, "got release\n");
1564
1565	if (vdev->vfl_type == VFL_TYPE_VIDEO)
1566		ret = vb2_fop_release(f);
1567	else
1568		ret = v4l2_fh_release(f);
1569
1570	cxusb_medion_put(dvbdev);
1571
1572	return ret;
1573}
1574
1575static const struct v4l2_file_operations cxusb_video_fops = {
1576	.owner = THIS_MODULE,
1577	.read = vb2_fop_read,
1578	.poll = vb2_fop_poll,
1579	.unlocked_ioctl = video_ioctl2,
1580	.mmap = vb2_fop_mmap,
1581	.open = cxusb_videoradio_open,
1582	.release = cxusb_videoradio_release
1583};
1584
1585static const struct v4l2_file_operations cxusb_radio_fops = {
1586	.owner = THIS_MODULE,
1587	.unlocked_ioctl = video_ioctl2,
1588	.open = cxusb_videoradio_open,
1589	.release = cxusb_videoradio_release
1590};
1591
1592static void cxusb_medion_v4l2_release(struct v4l2_device *v4l2_dev)
1593{
1594	struct cxusb_medion_dev *cxdev =
1595		container_of(v4l2_dev, struct cxusb_medion_dev, v4l2dev);
1596	struct dvb_usb_device *dvbdev = cxdev->dvbdev;
1597
1598	cxusb_vprintk(dvbdev, OPS, "v4l2 device release\n");
1599
1600	v4l2_device_unregister(&cxdev->v4l2dev);
1601
1602	mutex_destroy(&cxdev->dev_lock);
1603
1604	while (completion_done(&cxdev->v4l2_release))
1605		schedule();
1606
1607	complete(&cxdev->v4l2_release);
1608}
1609
1610static void cxusb_medion_videodev_release(struct video_device *vdev)
1611{
1612	struct dvb_usb_device *dvbdev = video_get_drvdata(vdev);
1613
1614	cxusb_vprintk(dvbdev, OPS, "video device release\n");
1615
1616	video_device_release(vdev);
1617}
1618
1619static int cxusb_medion_register_analog_video(struct dvb_usb_device *dvbdev)
1620{
1621	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1622	int ret;
1623
1624	cxdev->videoqueue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1625	cxdev->videoqueue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ |
1626		VB2_DMABUF;
1627	cxdev->videoqueue.ops = &cxdev_video_qops;
1628	cxdev->videoqueue.mem_ops = &vb2_vmalloc_memops;
1629	cxdev->videoqueue.drv_priv = dvbdev;
1630	cxdev->videoqueue.buf_struct_size =
1631		sizeof(struct cxusb_medion_vbuffer);
1632	cxdev->videoqueue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1633	cxdev->videoqueue.min_queued_buffers = 6;
1634	cxdev->videoqueue.lock = &cxdev->dev_lock;
1635
1636	ret = vb2_queue_init(&cxdev->videoqueue);
1637	if (ret) {
1638		dev_err(&dvbdev->udev->dev,
1639			"video queue init failed, ret = %d\n", ret);
1640		return ret;
1641	}
1642
1643	cxdev->videodev = video_device_alloc();
1644	if (!cxdev->videodev) {
1645		dev_err(&dvbdev->udev->dev, "video device alloc failed\n");
1646		return -ENOMEM;
1647	}
1648
1649	cxdev->videodev->device_caps = videocaps;
1650	cxdev->videodev->fops = &cxusb_video_fops;
1651	cxdev->videodev->v4l2_dev = &cxdev->v4l2dev;
1652	cxdev->videodev->queue = &cxdev->videoqueue;
1653	strscpy(cxdev->videodev->name, "cxusb", sizeof(cxdev->videodev->name));
1654	cxdev->videodev->vfl_dir = VFL_DIR_RX;
1655	cxdev->videodev->ioctl_ops = &cxusb_video_ioctl;
1656	cxdev->videodev->tvnorms = V4L2_STD_ALL;
1657	cxdev->videodev->release = cxusb_medion_videodev_release;
1658	cxdev->videodev->lock = &cxdev->dev_lock;
1659	video_set_drvdata(cxdev->videodev, dvbdev);
1660
1661	ret = video_register_device(cxdev->videodev, VFL_TYPE_VIDEO, -1);
1662	if (ret) {
1663		dev_err(&dvbdev->udev->dev,
1664			"video device register failed, ret = %d\n", ret);
1665		goto ret_vrelease;
1666	}
1667
1668	return 0;
1669
1670ret_vrelease:
1671	video_device_release(cxdev->videodev);
1672	return ret;
1673}
1674
1675static int cxusb_medion_register_analog_radio(struct dvb_usb_device *dvbdev)
1676{
1677	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1678	int ret;
1679
1680	cxdev->radiodev = video_device_alloc();
1681	if (!cxdev->radiodev) {
1682		dev_err(&dvbdev->udev->dev, "radio device alloc failed\n");
1683		return -ENOMEM;
1684	}
1685
1686	cxdev->radiodev->device_caps = radiocaps;
1687	cxdev->radiodev->fops = &cxusb_radio_fops;
1688	cxdev->radiodev->v4l2_dev = &cxdev->v4l2dev;
1689	strscpy(cxdev->radiodev->name, "cxusb", sizeof(cxdev->radiodev->name));
1690	cxdev->radiodev->vfl_dir = VFL_DIR_RX;
1691	cxdev->radiodev->ioctl_ops = &cxusb_radio_ioctl;
1692	cxdev->radiodev->release = video_device_release;
1693	cxdev->radiodev->lock = &cxdev->dev_lock;
1694	video_set_drvdata(cxdev->radiodev, dvbdev);
1695
1696	ret = video_register_device(cxdev->radiodev, VFL_TYPE_RADIO, -1);
1697	if (ret) {
1698		dev_err(&dvbdev->udev->dev,
1699			"radio device register failed, ret = %d\n", ret);
1700		video_device_release(cxdev->radiodev);
1701		return ret;
1702	}
1703
1704	return 0;
1705}
1706
1707static int cxusb_medion_register_analog_subdevs(struct dvb_usb_device *dvbdev)
1708{
1709	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1710	int ret;
1711	struct tuner_setup tun_setup;
1712
1713	/* attach cx25840 capture chip */
1714	cxdev->cx25840 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
1715					     &dvbdev->i2c_adap,
1716					     "cx25840", 0x44, NULL);
1717	if (!cxdev->cx25840) {
1718		dev_err(&dvbdev->udev->dev, "cx25840 not found\n");
1719		return -ENODEV;
1720	}
1721
1722	/*
1723	 * Initialize cx25840 chip by calling its subdevice init core op.
1724	 *
1725	 * This switches it into the generic mode that disables some of
1726	 * ivtv-related hacks in the cx25840 driver while allowing setting
1727	 * of the chip video output configuration (passed in the call below
1728	 * as the last argument).
1729	 */
1730	ret = v4l2_subdev_call(cxdev->cx25840, core, init,
1731			       CX25840_VCONFIG_FMT_BT656 |
1732			       CX25840_VCONFIG_RES_8BIT |
1733			       CX25840_VCONFIG_VBIRAW_DISABLED |
1734			       CX25840_VCONFIG_ANCDATA_DISABLED |
1735			       CX25840_VCONFIG_ACTIVE_COMPOSITE |
1736			       CX25840_VCONFIG_VALID_ANDACTIVE |
1737			       CX25840_VCONFIG_HRESETW_NORMAL |
1738			       CX25840_VCONFIG_CLKGATE_NONE |
1739			       CX25840_VCONFIG_DCMODE_DWORDS);
1740	if (ret != 0) {
1741		dev_err(&dvbdev->udev->dev,
1742			"cx25840 init failed (%d)\n", ret);
1743		return ret;
1744	}
1745
1746	/* attach analog tuner */
1747	cxdev->tuner = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
1748					   &dvbdev->i2c_adap,
1749					   "tuner", 0x61, NULL);
1750	if (!cxdev->tuner) {
1751		dev_err(&dvbdev->udev->dev, "tuner not found\n");
1752		return -ENODEV;
1753	}
1754
1755	/* configure it */
1756	memset(&tun_setup, 0, sizeof(tun_setup));
1757	tun_setup.addr = 0x61;
1758	tun_setup.type = TUNER_PHILIPS_FMD1216ME_MK3;
1759	tun_setup.mode_mask = T_RADIO | T_ANALOG_TV;
1760	v4l2_subdev_call(cxdev->tuner, tuner, s_type_addr, &tun_setup);
1761
1762	/* attach IF demod */
1763	cxdev->tda9887 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
1764					     &dvbdev->i2c_adap,
1765					     "tuner", 0x43, NULL);
1766	if (!cxdev->tda9887) {
1767		dev_err(&dvbdev->udev->dev, "tda9887 not found\n");
1768		return -ENODEV;
1769	}
1770
1771	return 0;
1772}
1773
1774int cxusb_medion_register_analog(struct dvb_usb_device *dvbdev)
1775{
1776	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1777	int ret;
1778
1779	mutex_init(&cxdev->dev_lock);
1780
1781	init_completion(&cxdev->v4l2_release);
1782
1783	cxdev->v4l2dev.release = cxusb_medion_v4l2_release;
1784
1785	ret = v4l2_device_register(&dvbdev->udev->dev, &cxdev->v4l2dev);
1786	if (ret != 0) {
1787		dev_err(&dvbdev->udev->dev,
1788			"V4L2 device registration failed, ret = %d\n", ret);
1789		mutex_destroy(&cxdev->dev_lock);
1790		return ret;
1791	}
1792
1793	ret = cxusb_medion_register_analog_subdevs(dvbdev);
1794	if (ret)
1795		goto ret_unregister;
1796
1797	INIT_WORK(&cxdev->urbwork, cxusb_medion_v_complete_work);
1798	INIT_LIST_HEAD(&cxdev->buflist);
1799
1800	cxdev->width = 320;
1801	cxdev->height = 240;
1802
1803	ret = cxusb_medion_register_analog_video(dvbdev);
1804	if (ret)
1805		goto ret_unregister;
1806
1807	ret = cxusb_medion_register_analog_radio(dvbdev);
1808	if (ret)
1809		goto ret_vunreg;
1810
1811	return 0;
1812
1813ret_vunreg:
1814	vb2_video_unregister_device(cxdev->videodev);
1815
1816ret_unregister:
1817	v4l2_device_put(&cxdev->v4l2dev);
1818	wait_for_completion(&cxdev->v4l2_release);
1819
1820	return ret;
1821}
1822
1823void cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev)
1824{
1825	struct cxusb_medion_dev *cxdev = dvbdev->priv;
1826
1827	cxusb_vprintk(dvbdev, OPS, "unregistering analog\n");
1828
1829	video_unregister_device(cxdev->radiodev);
1830	vb2_video_unregister_device(cxdev->videodev);
1831
1832	v4l2_device_put(&cxdev->v4l2dev);
1833	wait_for_completion(&cxdev->v4l2_release);
1834
1835	cxusb_vprintk(dvbdev, OPS, "analog unregistered\n");
1836}
~/ Read-only file preview.