]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (9925): tuner-core: add debug msg's when asking tuner to sleep
[net-next-2.6.git] / drivers / media / video / em28xx / em28xx-video.c
CommitLineData
a6c2ba28 1/*
6ea54d93
DSL
2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
a6c2ba28 4
f7abcd38
MCC
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
2e7c6dc3 7 Mauro Carvalho Chehab <mchehab@infradead.org>
f7abcd38 8 Sascha Sommer <saschasommer@freenet.de>
a6c2ba28 9
439090d7
MCC
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
a6c2ba28
AM
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
e5589bef 32#include <linux/bitmap.h>
a6c2ba28 33#include <linux/usb.h>
a6c2ba28 34#include <linux/i2c.h>
b296fc60 35#include <linux/version.h>
6d35c8f6 36#include <linux/mm.h>
1e4baed3 37#include <linux/mutex.h>
a6c2ba28 38
f7abcd38 39#include "em28xx.h"
c0477ad9 40#include <media/v4l2-common.h>
35ea11ff 41#include <media/v4l2-ioctl.h>
2474ed44 42#include <media/msp3400.h>
ed086314 43#include <media/tuner.h>
a6c2ba28 44
f7abcd38
MCC
45#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
46 "Markus Rechberger <mrechberger@gmail.com>, " \
2e7c6dc3 47 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
f7abcd38 48 "Sascha Sommer <saschasommer@freenet.de>"
a6c2ba28 49
f7abcd38
MCC
50#define DRIVER_NAME "em28xx"
51#define DRIVER_DESC "Empia em28xx based USB video device driver"
195a4ef6 52#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
a6c2ba28 53
3acf2809 54#define em28xx_videodbg(fmt, arg...) do {\
4ac97914
MCC
55 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
d80e134d 57 dev->name, __func__ , ##arg); } while (0)
a6c2ba28 58
ad0ebb96 59static unsigned int isoc_debug;
f245e549
MCC
60module_param(isoc_debug, int, 0644);
61MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
ad0ebb96 62
6ea54d93
DSL
63#define em28xx_isocdbg(fmt, arg...) \
64do {\
65 if (isoc_debug) { \
ad0ebb96 66 printk(KERN_INFO "%s %s :"fmt, \
6ea54d93
DSL
67 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
ad0ebb96 70
a6c2ba28
AM
71MODULE_AUTHOR(DRIVER_AUTHOR);
72MODULE_DESCRIPTION(DRIVER_DESC);
73MODULE_LICENSE("GPL");
74
3acf2809 75static LIST_HEAD(em28xx_devlist);
818a557e 76static DEFINE_MUTEX(em28xx_devlist_mutex);
9c75541f 77
9d4d9c05 78static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
e5589bef 79static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
0be43754
MCC
80static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
82
596d92d5 83module_param_array(card, int, NULL, 0444);
e5589bef
MCC
84module_param_array(video_nr, int, NULL, 0444);
85module_param_array(vbi_nr, int, NULL, 0444);
0be43754
MCC
86module_param_array(radio_nr, int, NULL, 0444);
87MODULE_PARM_DESC(card, "card type");
88MODULE_PARM_DESC(video_nr, "video device numbers");
89MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
90MODULE_PARM_DESC(radio_nr, "radio device numbers");
596d92d5 91
ff699e6b 92static unsigned int video_debug;
6ea54d93
DSL
93module_param(video_debug, int, 0644);
94MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
a6c2ba28 95
e5589bef
MCC
96/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
97static unsigned long em28xx_devused;
98
a6c2ba28 99/* supported controls */
c0477ad9 100/* Common to all boards */
3acf2809 101static struct v4l2_queryctrl em28xx_qctrl[] = {
c0477ad9
MCC
102 {
103 .id = V4L2_CID_AUDIO_VOLUME,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Volume",
106 .minimum = 0x0,
107 .maximum = 0x1f,
108 .step = 0x1,
109 .default_value = 0x1f,
110 .flags = 0,
6ea54d93 111 }, {
c0477ad9
MCC
112 .id = V4L2_CID_AUDIO_MUTE,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 .name = "Mute",
115 .minimum = 0,
116 .maximum = 1,
117 .step = 1,
118 .default_value = 1,
119 .flags = 0,
120 }
121};
122
3acf2809 123static struct usb_driver em28xx_usb_driver;
a6c2ba28 124
ad0ebb96
MCC
125/* ------------------------------------------------------------------
126 DMA and thread functions
127 ------------------------------------------------------------------*/
128
129/*
130 * Announces that a buffer were filled and request the next
131 */
f245e549 132static inline void buffer_filled(struct em28xx *dev,
ad0ebb96
MCC
133 struct em28xx_dmaqueue *dma_q,
134 struct em28xx_buffer *buf)
135{
ad0ebb96
MCC
136 /* Advice that buffer was filled */
137 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
138 buf->vb.state = VIDEOBUF_DONE;
139 buf->vb.field_count++;
140 do_gettimeofday(&buf->vb.ts);
141
cb784724
MCC
142 dev->isoc_ctl.buf = NULL;
143
ad0ebb96
MCC
144 list_del(&buf->vb.queue);
145 wake_up(&buf->vb.done);
146}
147
148/*
149 * Identify the buffer header type and properly handles
150 */
151static void em28xx_copy_video(struct em28xx *dev,
152 struct em28xx_dmaqueue *dma_q,
153 struct em28xx_buffer *buf,
154 unsigned char *p,
155 unsigned char *outp, unsigned long len)
156{
157 void *fieldstart, *startwrite, *startread;
f245e549 158 int linesdone, currlinedone, offset, lencopy, remain;
44dc733c 159 int bytesperline = dev->width << 1;
ad0ebb96
MCC
160
161 if (dma_q->pos + len > buf->vb.size)
162 len = buf->vb.size - dma_q->pos;
163
d7aa8020 164 if (p[0] != 0x88 && p[0] != 0x22) {
ad0ebb96
MCC
165 em28xx_isocdbg("frame is not complete\n");
166 len += 4;
167 } else
f245e549 168 p += 4;
ad0ebb96
MCC
169
170 startread = p;
171 remain = len;
172
173 /* Interlaces frame */
174 if (buf->top_field)
175 fieldstart = outp;
176 else
44dc733c 177 fieldstart = outp + bytesperline;
ad0ebb96 178
44dc733c
MCC
179 linesdone = dma_q->pos / bytesperline;
180 currlinedone = dma_q->pos % bytesperline;
181 offset = linesdone * bytesperline * 2 + currlinedone;
ad0ebb96 182 startwrite = fieldstart + offset;
44dc733c 183 lencopy = bytesperline - currlinedone;
ad0ebb96
MCC
184 lencopy = lencopy > remain ? remain : lencopy;
185
f245e549 186 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
ea8df7e0 187 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
f245e549
MCC
188 ((char *)startwrite + lencopy) -
189 ((char *)outp + buf->vb.size));
190 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
d7aa8020 191 }
e0fadfd3
AT
192 if (lencopy <= 0)
193 return;
d7aa8020 194 memcpy(startwrite, startread, lencopy);
ad0ebb96
MCC
195
196 remain -= lencopy;
197
198 while (remain > 0) {
44dc733c 199 startwrite += lencopy + bytesperline;
ad0ebb96 200 startread += lencopy;
44dc733c 201 if (bytesperline > remain)
ad0ebb96
MCC
202 lencopy = remain;
203 else
44dc733c 204 lencopy = bytesperline;
ad0ebb96 205
f245e549 206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
ea8df7e0 207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
f245e549
MCC
208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
210 lencopy = remain = (char *)outp + buf->vb.size -
211 (char *)startwrite;
d7aa8020 212 }
f245e549
MCC
213 if (lencopy <= 0)
214 break;
d7aa8020
AT
215
216 memcpy(startwrite, startread, lencopy);
ad0ebb96
MCC
217
218 remain -= lencopy;
219 }
220
221 dma_q->pos += len;
222}
223
f245e549 224static inline void print_err_status(struct em28xx *dev,
ad0ebb96
MCC
225 int packet, int status)
226{
227 char *errmsg = "Unknown";
228
f245e549 229 switch (status) {
ad0ebb96
MCC
230 case -ENOENT:
231 errmsg = "unlinked synchronuously";
232 break;
233 case -ECONNRESET:
234 errmsg = "unlinked asynchronuously";
235 break;
236 case -ENOSR:
237 errmsg = "Buffer error (overrun)";
238 break;
239 case -EPIPE:
240 errmsg = "Stalled (device not responding)";
241 break;
242 case -EOVERFLOW:
243 errmsg = "Babble (bad cable?)";
244 break;
245 case -EPROTO:
246 errmsg = "Bit-stuff error (bad cable?)";
247 break;
248 case -EILSEQ:
249 errmsg = "CRC/Timeout (could be anything)";
250 break;
251 case -ETIME:
252 errmsg = "Device does not respond";
253 break;
254 }
f245e549 255 if (packet < 0) {
ad0ebb96
MCC
256 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
257 } else {
258 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
259 packet, status, errmsg);
260 }
261}
262
263/*
264 * video-buf generic routine to get the next available buffer
265 */
3b5fa928 266static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
ad0ebb96
MCC
267 struct em28xx_buffer **buf)
268{
269 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
dbecb44c 270 char *outp;
ad0ebb96 271
dbecb44c
MCC
272 if (list_empty(&dma_q->active)) {
273 em28xx_isocdbg("No active queue to serve\n");
dbecb44c
MCC
274 dev->isoc_ctl.buf = NULL;
275 *buf = NULL;
3b5fa928 276 return;
dbecb44c
MCC
277 }
278
dbecb44c
MCC
279 /* Get the next buffer */
280 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
281
dbecb44c
MCC
282 /* Cleans up buffer - Usefull for testing for frame/URB loss */
283 outp = videobuf_to_vmalloc(&(*buf)->vb);
284 memset(outp, 0, (*buf)->vb.size);
cb784724
MCC
285
286 dev->isoc_ctl.buf = *buf;
287
3b5fa928 288 return;
ad0ebb96
MCC
289}
290
291/*
292 * Controls the isoc copy of each urb packet
293 */
579f72e4 294static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
ad0ebb96 295{
d7aa8020 296 struct em28xx_buffer *buf;
ad0ebb96 297 struct em28xx_dmaqueue *dma_q = urb->context;
3b5fa928 298 unsigned char *outp = NULL;
ad0ebb96 299 int i, len = 0, rc = 1;
d7aa8020 300 unsigned char *p;
ad0ebb96
MCC
301
302 if (!dev)
303 return 0;
304
305 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
306 return 0;
307
f245e549
MCC
308 if (urb->status < 0) {
309 print_err_status(dev, -1, urb->status);
ad0ebb96
MCC
310 if (urb->status == -ENOENT)
311 return 0;
312 }
313
3b5fa928
AT
314 buf = dev->isoc_ctl.buf;
315 if (buf != NULL)
316 outp = videobuf_to_vmalloc(&buf->vb);
d7aa8020 317
ad0ebb96
MCC
318 for (i = 0; i < urb->number_of_packets; i++) {
319 int status = urb->iso_frame_desc[i].status;
320
f245e549
MCC
321 if (status < 0) {
322 print_err_status(dev, i, status);
ad0ebb96
MCC
323 if (urb->iso_frame_desc[i].status != -EPROTO)
324 continue;
325 }
326
f245e549 327 len = urb->iso_frame_desc[i].actual_length - 4;
ad0ebb96
MCC
328
329 if (urb->iso_frame_desc[i].actual_length <= 0) {
d7aa8020 330 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
ad0ebb96
MCC
331 continue;
332 }
333 if (urb->iso_frame_desc[i].actual_length >
334 dev->max_pkt_size) {
335 em28xx_isocdbg("packet bigger than packet size");
336 continue;
337 }
338
339 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
ad0ebb96
MCC
340
341 /* FIXME: incomplete buffer checks where removed to make
342 logic simpler. Impacts of those changes should be evaluated
343 */
b4916f8c
MCC
344 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
345 em28xx_isocdbg("VBI HEADER!!!\n");
346 /* FIXME: Should add vbi copy */
347 continue;
348 }
d7aa8020 349 if (p[0] == 0x22 && p[1] == 0x5a) {
78bb3949
MCC
350 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
351 len, (p[2] & 1)? "odd" : "even");
d7aa8020 352
3b5fa928
AT
353 if (!(p[2] & 1)) {
354 if (buf != NULL)
355 buffer_filled(dev, dma_q, buf);
356 get_next_buf(dma_q, &buf);
357 if (buf == NULL)
358 outp = NULL;
359 else
360 outp = videobuf_to_vmalloc(&buf->vb);
361 }
362
363 if (buf != NULL) {
364 if (p[2] & 1)
365 buf->top_field = 0;
366 else
367 buf->top_field = 1;
e0fadfd3 368 }
b4916f8c 369
ad0ebb96 370 dma_q->pos = 0;
ad0ebb96 371 }
3b5fa928
AT
372 if (buf != NULL)
373 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
ad0ebb96
MCC
374 }
375 return rc;
376}
377
ad0ebb96
MCC
378/* ------------------------------------------------------------------
379 Videobuf operations
380 ------------------------------------------------------------------*/
381
382static int
383buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
384{
385 struct em28xx_fh *fh = vq->priv_data;
d2d9fbfd
MCC
386 struct em28xx *dev = fh->dev;
387 struct v4l2_frequency f;
ad0ebb96 388
d7aa8020 389 *size = 16 * fh->dev->width * fh->dev->height >> 3;
ad0ebb96
MCC
390 if (0 == *count)
391 *count = EM28XX_DEF_BUF;
392
f245e549
MCC
393 if (*count < EM28XX_MIN_BUF)
394 *count = EM28XX_MIN_BUF;
ad0ebb96 395
d2d9fbfd 396 /* Ask tuner to go to analog mode */
6ea54d93 397 memset(&f, 0, sizeof(f));
d2d9fbfd
MCC
398 f.frequency = dev->ctl_freq;
399
400 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
401
ad0ebb96
MCC
402 return 0;
403}
404
3b5fa928 405/* This is called *without* dev->slock held; please keep it that way */
ad0ebb96
MCC
406static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
407{
3b5fa928
AT
408 struct em28xx_fh *fh = vq->priv_data;
409 struct em28xx *dev = fh->dev;
410 unsigned long flags = 0;
ad0ebb96
MCC
411 if (in_interrupt())
412 BUG();
413
3b5fa928
AT
414 /* We used to wait for the buffer to finish here, but this didn't work
415 because, as we were keeping the state as VIDEOBUF_QUEUED,
416 videobuf_queue_cancel marked it as finished for us.
417 (Also, it could wedge forever if the hardware was misconfigured.)
418
419 This should be safe; by the time we get here, the buffer isn't
420 queued anymore. If we ever start marking the buffers as
421 VIDEOBUF_ACTIVE, it won't be, though.
422 */
423 spin_lock_irqsave(&dev->slock, flags);
424 if (dev->isoc_ctl.buf == buf)
425 dev->isoc_ctl.buf = NULL;
426 spin_unlock_irqrestore(&dev->slock, flags);
427
ad0ebb96
MCC
428 videobuf_vmalloc_free(&buf->vb);
429 buf->vb.state = VIDEOBUF_NEEDS_INIT;
430}
431
432static int
433buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
434 enum v4l2_field field)
435{
436 struct em28xx_fh *fh = vq->priv_data;
f245e549 437 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
ad0ebb96
MCC
438 struct em28xx *dev = fh->dev;
439 int rc = 0, urb_init = 0;
ad0ebb96 440
ad0ebb96
MCC
441 /* FIXME: It assumes depth = 16 */
442 /* The only currently supported format is 16 bits/pixel */
d7aa8020 443 buf->vb.size = 16 * dev->width * dev->height >> 3;
ad0ebb96
MCC
444
445 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
446 return -EINVAL;
447
05612975
BP
448 buf->vb.width = dev->width;
449 buf->vb.height = dev->height;
450 buf->vb.field = field;
ad0ebb96
MCC
451
452 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
d7aa8020
AT
453 rc = videobuf_iolock(vq, &buf->vb, NULL);
454 if (rc < 0)
ad0ebb96 455 goto fail;
ad0ebb96
MCC
456 }
457
ad0ebb96 458 if (!dev->isoc_ctl.num_bufs)
f245e549 459 urb_init = 1;
ad0ebb96
MCC
460
461 if (urb_init) {
579f72e4
AT
462 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
463 EM28XX_NUM_BUFS, dev->max_pkt_size,
c67ec53f 464 em28xx_isoc_copy);
f245e549 465 if (rc < 0)
ad0ebb96
MCC
466 goto fail;
467 }
468
469 buf->vb.state = VIDEOBUF_PREPARED;
470 return 0;
471
472fail:
f245e549 473 free_buffer(vq, buf);
ad0ebb96
MCC
474 return rc;
475}
476
477static void
478buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
479{
480 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
481 struct em28xx_fh *fh = vq->priv_data;
f245e549 482 struct em28xx *dev = fh->dev;
ad0ebb96 483 struct em28xx_dmaqueue *vidq = &dev->vidq;
ad0ebb96 484
d7aa8020
AT
485 buf->vb.state = VIDEOBUF_QUEUED;
486 list_add_tail(&buf->vb.queue, &vidq->active);
487
ad0ebb96
MCC
488}
489
6ea54d93
DSL
490static void buffer_release(struct videobuf_queue *vq,
491 struct videobuf_buffer *vb)
ad0ebb96 492{
f245e549 493 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
ad0ebb96 494 struct em28xx_fh *fh = vq->priv_data;
f245e549 495 struct em28xx *dev = (struct em28xx *)fh->dev;
ad0ebb96 496
d7aa8020 497 em28xx_isocdbg("em28xx: called buffer_release\n");
ad0ebb96 498
f245e549 499 free_buffer(vq, buf);
ad0ebb96
MCC
500}
501
502static struct videobuf_queue_ops em28xx_video_qops = {
503 .buf_setup = buffer_setup,
504 .buf_prepare = buffer_prepare,
505 .buf_queue = buffer_queue,
506 .buf_release = buffer_release,
507};
a6c2ba28 508
6ea54d93 509/********************* v4l2 interface **************************************/
a6c2ba28 510
a6c2ba28 511/*
3acf2809 512 * em28xx_config()
a6c2ba28
AM
513 * inits registers with sane defaults
514 */
3acf2809 515static int em28xx_config(struct em28xx *dev)
a6c2ba28 516{
f2a01a00 517 int retval;
a6c2ba28
AM
518
519 /* Sets I2C speed to 100 KHz */
505b6d0b 520 if (!dev->board.is_em2800) {
2a29a0d7 521 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
f2a01a00
DSL
522 if (retval < 0) {
523 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
524 __func__, retval);
525 return retval;
526 }
527 }
a6c2ba28
AM
528
529 /* enable vbi capturing */
e5589bef 530
2a29a0d7
MCC
531/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
532/* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
533 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
e5589bef 534
a6c2ba28
AM
535 dev->mute = 1; /* maybe not the right place... */
536 dev->volume = 0x1f;
539c96d0 537
3acf2809
MCC
538 em28xx_outfmt_set_yuv422(dev);
539 em28xx_colorlevels_set_default(dev);
540 em28xx_compression_disable(dev);
a6c2ba28
AM
541
542 return 0;
543}
544
545/*
3acf2809 546 * em28xx_config_i2c()
a6c2ba28
AM
547 * configure i2c attached devices
548 */
943a4902 549static void em28xx_config_i2c(struct em28xx *dev)
a6c2ba28 550{
c7c0b34c 551 struct v4l2_routing route;
231ffc9c 552 int zero = 0;
c7c0b34c
HV
553
554 route.input = INPUT(dev->ctl_input)->vmux;
555 route.output = 0;
231ffc9c 556 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
c7c0b34c 557 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
f5762e44 558 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
a6c2ba28
AM
559}
560
eac94356
MCC
561static void video_mux(struct em28xx *dev, int index)
562{
c7c0b34c 563 struct v4l2_routing route;
eac94356 564
c7c0b34c
HV
565 route.input = INPUT(index)->vmux;
566 route.output = 0;
eac94356
MCC
567 dev->ctl_input = index;
568 dev->ctl_ainput = INPUT(index)->amux;
35ae6f04 569 dev->ctl_aoutput = INPUT(index)->aout;
eac94356 570
e879b8eb
MCC
571 if (!dev->ctl_aoutput)
572 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
573
c7c0b34c 574 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
eac94356 575
505b6d0b 576 if (dev->board.has_msp34xx) {
6ea54d93
DSL
577 if (dev->i2s_speed) {
578 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
579 &dev->i2s_speed);
580 }
2474ed44 581 route.input = dev->ctl_ainput;
07151724 582 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
2474ed44 583 /* Note: this is msp3400 specific */
6ea54d93
DSL
584 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
585 &route);
eac94356 586 }
539c96d0 587
00b8730f 588 em28xx_audio_analog_set(dev);
eac94356
MCC
589}
590
a225452e
MCC
591/* Usage lock check functions */
592static int res_get(struct em28xx_fh *fh)
593{
594 struct em28xx *dev = fh->dev;
595 int rc = 0;
596
597 /* This instance already has stream_on */
598 if (fh->stream_on)
599 return rc;
600
a225452e 601 if (dev->stream_on)
29b59417 602 return -EBUSY;
a225452e 603
e74153d4
MCC
604 dev->stream_on = 1;
605 fh->stream_on = 1;
a225452e
MCC
606 return rc;
607}
608
609static int res_check(struct em28xx_fh *fh)
610{
611 return (fh->stream_on);
612}
613
614static void res_free(struct em28xx_fh *fh)
615{
616 struct em28xx *dev = fh->dev;
617
a225452e
MCC
618 fh->stream_on = 0;
619 dev->stream_on = 0;
a225452e
MCC
620}
621
195a4ef6
MCC
622/*
623 * em28xx_get_ctrl()
624 * return the current saturation, brightness or contrast, mute state
625 */
626static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
627{
628 switch (ctrl->id) {
629 case V4L2_CID_AUDIO_MUTE:
630 ctrl->value = dev->mute;
631 return 0;
632 case V4L2_CID_AUDIO_VOLUME:
633 ctrl->value = dev->volume;
634 return 0;
635 default:
636 return -EINVAL;
a6c2ba28 637 }
195a4ef6 638}
a6c2ba28 639
195a4ef6
MCC
640/*
641 * em28xx_set_ctrl()
642 * mute or set new saturation, brightness or contrast
643 */
644static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
645{
646 switch (ctrl->id) {
647 case V4L2_CID_AUDIO_MUTE:
648 if (ctrl->value != dev->mute) {
649 dev->mute = ctrl->value;
195a4ef6
MCC
650 return em28xx_audio_analog_set(dev);
651 }
652 return 0;
653 case V4L2_CID_AUDIO_VOLUME:
654 dev->volume = ctrl->value;
655 return em28xx_audio_analog_set(dev);
656 default:
657 return -EINVAL;
658 }
659}
a6c2ba28 660
195a4ef6
MCC
661static int check_dev(struct em28xx *dev)
662{
663 if (dev->state & DEV_DISCONNECTED) {
664 em28xx_errdev("v4l2 ioctl: device not present\n");
665 return -ENODEV;
e5589bef 666 }
a6c2ba28 667
195a4ef6
MCC
668 if (dev->state & DEV_MISCONFIGURED) {
669 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
670 "close and open it again\n");
671 return -EIO;
672 }
673 return 0;
674}
a6c2ba28 675
195a4ef6
MCC
676static void get_scale(struct em28xx *dev,
677 unsigned int width, unsigned int height,
678 unsigned int *hscale, unsigned int *vscale)
679{
680 unsigned int maxw = norm_maxw(dev);
681 unsigned int maxh = norm_maxh(dev);
682
683 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
684 if (*hscale >= 0x4000)
685 *hscale = 0x3fff;
686
687 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
688 if (*vscale >= 0x4000)
689 *vscale = 0x3fff;
a6c2ba28
AM
690}
691
195a4ef6
MCC
692/* ------------------------------------------------------------------
693 IOCTL vidioc handling
694 ------------------------------------------------------------------*/
695
78b526a4 696static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 697 struct v4l2_format *f)
a6c2ba28 698{
195a4ef6
MCC
699 struct em28xx_fh *fh = priv;
700 struct em28xx *dev = fh->dev;
a6c2ba28 701
195a4ef6 702 mutex_lock(&dev->lock);
e5589bef 703
195a4ef6
MCC
704 f->fmt.pix.width = dev->width;
705 f->fmt.pix.height = dev->height;
706 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
44dc733c
MCC
707 f->fmt.pix.bytesperline = dev->width * 2;
708 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
195a4ef6 709 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
e5589bef 710
195a4ef6
MCC
711 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
712 f->fmt.pix.field = dev->interlaced ?
713 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
e5589bef 714
195a4ef6
MCC
715 mutex_unlock(&dev->lock);
716 return 0;
a6c2ba28
AM
717}
718
78b526a4 719static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 720 struct v4l2_format *f)
a6c2ba28 721{
195a4ef6
MCC
722 struct em28xx_fh *fh = priv;
723 struct em28xx *dev = fh->dev;
724 int width = f->fmt.pix.width;
725 int height = f->fmt.pix.height;
726 unsigned int maxw = norm_maxw(dev);
727 unsigned int maxh = norm_maxh(dev);
728 unsigned int hscale, vscale;
729
730 /* width must even because of the YUYV format
731 height must be even because of interlacing */
732 height &= 0xfffe;
cf8c91c3 733 width &= 0xfffe;
a6c2ba28 734
cf8c91c3 735 if (unlikely(height < 32))
195a4ef6 736 height = 32;
cf8c91c3 737 if (unlikely(height > maxh))
195a4ef6 738 height = maxh;
cf8c91c3 739 if (unlikely(width < 48))
195a4ef6 740 width = 48;
cf8c91c3 741 if (unlikely(width > maxw))
195a4ef6 742 width = maxw;
a6c2ba28 743
505b6d0b 744 if (dev->board.is_em2800) {
195a4ef6
MCC
745 /* the em2800 can only scale down to 50% */
746 if (height % (maxh / 2))
747 height = maxh;
748 if (width % (maxw / 2))
749 width = maxw;
750 /* according to empiatech support */
751 /* the MaxPacketSize is to small to support */
752 /* framesizes larger than 640x480 @ 30 fps */
753 /* or 640x576 @ 25 fps. As this would cut */
754 /* of a part of the image we prefer */
755 /* 360x576 or 360x480 for now */
756 if (width == maxw && height == maxh)
757 width /= 2;
758 }
a225452e 759
195a4ef6 760 get_scale(dev, width, height, &hscale, &vscale);
a6c2ba28 761
195a4ef6
MCC
762 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
763 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
a6c2ba28 764
195a4ef6
MCC
765 f->fmt.pix.width = width;
766 f->fmt.pix.height = height;
767 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
768 f->fmt.pix.bytesperline = width * 2;
769 f->fmt.pix.sizeimage = width * 2 * height;
770 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
771 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
a6c2ba28 772
a6c2ba28
AM
773 return 0;
774}
775
78b526a4 776static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 777 struct v4l2_format *f)
a6c2ba28 778{
195a4ef6
MCC
779 struct em28xx_fh *fh = priv;
780 struct em28xx *dev = fh->dev;
ad0ebb96 781 int rc;
a6c2ba28 782
195a4ef6
MCC
783 rc = check_dev(dev);
784 if (rc < 0)
785 return rc;
a225452e 786
5a80415b
SS
787 mutex_lock(&dev->lock);
788
efc52a94
MCC
789 vidioc_try_fmt_vid_cap(file, priv, f);
790
05612975
BP
791 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
792 em28xx_errdev("%s queue busy\n", __func__);
793 rc = -EBUSY;
794 goto out;
795 }
796
0ea13e6e
AT
797 if (dev->stream_on && !fh->stream_on) {
798 em28xx_errdev("%s device in use by another fh\n", __func__);
799 rc = -EBUSY;
800 goto out;
801 }
802
195a4ef6
MCC
803 /* set new image size */
804 dev->width = f->fmt.pix.width;
805 dev->height = f->fmt.pix.height;
195a4ef6 806 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
a6c2ba28 807
195a4ef6 808 em28xx_set_alternate(dev);
195a4ef6 809 em28xx_resolution_set(dev);
195a4ef6 810
05612975
BP
811 rc = 0;
812
813out:
195a4ef6 814 mutex_unlock(&dev->lock);
05612975 815 return rc;
195a4ef6
MCC
816}
817
6ea54d93 818static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
195a4ef6
MCC
819{
820 struct em28xx_fh *fh = priv;
821 struct em28xx *dev = fh->dev;
822 struct v4l2_format f;
195a4ef6
MCC
823 int rc;
824
825 rc = check_dev(dev);
826 if (rc < 0)
827 return rc;
828
195a4ef6 829 mutex_lock(&dev->lock);
7d497f8a 830 dev->norm = *norm;
a6c2ba28 831
195a4ef6
MCC
832 /* Adjusts width/height, if needed */
833 f.fmt.pix.width = dev->width;
834 f.fmt.pix.height = dev->height;
78b526a4 835 vidioc_try_fmt_vid_cap(file, priv, &f);
a6c2ba28 836
195a4ef6
MCC
837 /* set new image size */
838 dev->width = f.fmt.pix.width;
839 dev->height = f.fmt.pix.height;
195a4ef6 840 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
a6c2ba28 841
195a4ef6 842 em28xx_resolution_set(dev);
7d497f8a 843 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
a6c2ba28 844
195a4ef6
MCC
845 mutex_unlock(&dev->lock);
846 return 0;
847}
9e31ced8 848
195a4ef6
MCC
849static const char *iname[] = {
850 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
851 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
852 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
853 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
854 [EM28XX_VMUX_SVIDEO] = "S-Video",
855 [EM28XX_VMUX_TELEVISION] = "Television",
856 [EM28XX_VMUX_CABLE] = "Cable TV",
857 [EM28XX_VMUX_DVB] = "DVB",
858 [EM28XX_VMUX_DEBUG] = "for debug only",
859};
9e31ced8 860
195a4ef6
MCC
861static int vidioc_enum_input(struct file *file, void *priv,
862 struct v4l2_input *i)
863{
864 struct em28xx_fh *fh = priv;
865 struct em28xx *dev = fh->dev;
866 unsigned int n;
9e31ced8 867
195a4ef6
MCC
868 n = i->index;
869 if (n >= MAX_EM28XX_INPUT)
870 return -EINVAL;
871 if (0 == INPUT(n)->type)
872 return -EINVAL;
9e31ced8 873
195a4ef6
MCC
874 i->index = n;
875 i->type = V4L2_INPUT_TYPE_CAMERA;
a6c2ba28 876
195a4ef6 877 strcpy(i->name, iname[INPUT(n)->type]);
a6c2ba28 878
195a4ef6
MCC
879 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
880 (EM28XX_VMUX_CABLE == INPUT(n)->type))
881 i->type = V4L2_INPUT_TYPE_TUNER;
882
7d497f8a 883 i->std = dev->vdev->tvnorms;
195a4ef6
MCC
884
885 return 0;
a6c2ba28
AM
886}
887
195a4ef6 888static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
a6c2ba28 889{
195a4ef6
MCC
890 struct em28xx_fh *fh = priv;
891 struct em28xx *dev = fh->dev;
a6c2ba28 892
195a4ef6
MCC
893 *i = dev->ctl_input;
894
895 return 0;
896}
897
898static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
899{
900 struct em28xx_fh *fh = priv;
901 struct em28xx *dev = fh->dev;
902 int rc;
903
904 rc = check_dev(dev);
905 if (rc < 0)
906 return rc;
907
908 if (i >= MAX_EM28XX_INPUT)
909 return -EINVAL;
910 if (0 == INPUT(i)->type)
911 return -EINVAL;
a225452e 912
5a80415b 913 mutex_lock(&dev->lock);
a6c2ba28 914
195a4ef6
MCC
915 video_mux(dev, i);
916
917 mutex_unlock(&dev->lock);
918 return 0;
919}
920
921static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
922{
923 struct em28xx_fh *fh = priv;
924 struct em28xx *dev = fh->dev;
195a4ef6 925
35ae6f04
MCC
926 switch (a->index) {
927 case EM28XX_AMUX_VIDEO:
195a4ef6 928 strcpy(a->name, "Television");
35ae6f04
MCC
929 break;
930 case EM28XX_AMUX_LINE_IN:
195a4ef6 931 strcpy(a->name, "Line In");
35ae6f04
MCC
932 break;
933 case EM28XX_AMUX_VIDEO2:
934 strcpy(a->name, "Television alt");
935 break;
936 case EM28XX_AMUX_PHONE:
937 strcpy(a->name, "Phone");
938 break;
939 case EM28XX_AMUX_MIC:
940 strcpy(a->name, "Mic");
941 break;
942 case EM28XX_AMUX_CD:
943 strcpy(a->name, "CD");
944 break;
945 case EM28XX_AMUX_AUX:
946 strcpy(a->name, "Aux");
947 break;
948 case EM28XX_AMUX_PCM_OUT:
949 strcpy(a->name, "PCM");
950 break;
951 default:
952 return -EINVAL;
953 }
6ea54d93 954
35ae6f04 955 a->index = dev->ctl_ainput;
195a4ef6 956 a->capability = V4L2_AUDCAP_STEREO;
195a4ef6
MCC
957
958 return 0;
959}
960
961static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
962{
963 struct em28xx_fh *fh = priv;
964 struct em28xx *dev = fh->dev;
965
efc52a94
MCC
966 mutex_lock(&dev->lock);
967
35ae6f04
MCC
968 dev->ctl_ainput = INPUT(a->index)->amux;
969 dev->ctl_aoutput = INPUT(a->index)->aout;
e879b8eb
MCC
970
971 if (!dev->ctl_aoutput)
972 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
efc52a94
MCC
973
974 mutex_unlock(&dev->lock);
195a4ef6
MCC
975 return 0;
976}
977
978static int vidioc_queryctrl(struct file *file, void *priv,
979 struct v4l2_queryctrl *qc)
980{
981 struct em28xx_fh *fh = priv;
982 struct em28xx *dev = fh->dev;
983 int id = qc->id;
984 int i;
985 int rc;
986
987 rc = check_dev(dev);
988 if (rc < 0)
989 return rc;
990
991 memset(qc, 0, sizeof(*qc));
992
993 qc->id = id;
994
505b6d0b 995 if (!dev->board.has_msp34xx) {
195a4ef6
MCC
996 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
997 if (qc->id && qc->id == em28xx_qctrl[i].id) {
998 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
999 return 0;
a6c2ba28
AM
1000 }
1001 }
195a4ef6
MCC
1002 }
1003 mutex_lock(&dev->lock);
1004 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1005 mutex_unlock(&dev->lock);
a6c2ba28 1006
195a4ef6
MCC
1007 if (qc->type)
1008 return 0;
1009 else
1010 return -EINVAL;
1011}
a6c2ba28 1012
195a4ef6
MCC
1013static int vidioc_g_ctrl(struct file *file, void *priv,
1014 struct v4l2_control *ctrl)
1015{
1016 struct em28xx_fh *fh = priv;
1017 struct em28xx *dev = fh->dev;
1018 int rc;
a6c2ba28 1019
195a4ef6
MCC
1020 rc = check_dev(dev);
1021 if (rc < 0)
1022 return rc;
efc52a94 1023
195a4ef6 1024 mutex_lock(&dev->lock);
a6c2ba28 1025
505b6d0b 1026 if (!dev->board.has_msp34xx)
195a4ef6
MCC
1027 rc = em28xx_get_ctrl(dev, ctrl);
1028 else
1029 rc = -EINVAL;
1030
1031 if (rc == -EINVAL) {
1032 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1033 rc = 0;
1034 }
1035
1036 mutex_unlock(&dev->lock);
1037 return rc;
1038}
1039
1040static int vidioc_s_ctrl(struct file *file, void *priv,
1041 struct v4l2_control *ctrl)
1042{
1043 struct em28xx_fh *fh = priv;
1044 struct em28xx *dev = fh->dev;
1045 u8 i;
1046 int rc;
1047
1048 rc = check_dev(dev);
1049 if (rc < 0)
1050 return rc;
1051
1052 mutex_lock(&dev->lock);
1053
505b6d0b 1054 if (dev->board.has_msp34xx)
195a4ef6
MCC
1055 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1056 else {
1057 rc = 1;
1058 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1059 if (ctrl->id == em28xx_qctrl[i].id) {
1060 if (ctrl->value < em28xx_qctrl[i].minimum ||
1061 ctrl->value > em28xx_qctrl[i].maximum) {
1062 rc = -ERANGE;
1063 break;
1064 }
1065
1066 rc = em28xx_set_ctrl(dev, ctrl);
1067 break;
1068 }
a6c2ba28
AM
1069 }
1070 }
1071
195a4ef6
MCC
1072 /* Control not found - try to send it to the attached devices */
1073 if (rc == 1) {
1074 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1075 rc = 0;
1076 }
1077
5a80415b 1078 mutex_unlock(&dev->lock);
195a4ef6 1079 return rc;
a6c2ba28
AM
1080}
1081
195a4ef6
MCC
1082static int vidioc_g_tuner(struct file *file, void *priv,
1083 struct v4l2_tuner *t)
a6c2ba28 1084{
195a4ef6
MCC
1085 struct em28xx_fh *fh = priv;
1086 struct em28xx *dev = fh->dev;
1087 int rc;
1088
1089 rc = check_dev(dev);
1090 if (rc < 0)
1091 return rc;
1092
1093 if (0 != t->index)
1094 return -EINVAL;
1095
1096 strcpy(t->name, "Tuner");
1097
1098 mutex_lock(&dev->lock);
1099
1100 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1101
1102 mutex_unlock(&dev->lock);
1103 return 0;
a6c2ba28
AM
1104}
1105
195a4ef6
MCC
1106static int vidioc_s_tuner(struct file *file, void *priv,
1107 struct v4l2_tuner *t)
a6c2ba28 1108{
195a4ef6
MCC
1109 struct em28xx_fh *fh = priv;
1110 struct em28xx *dev = fh->dev;
1111 int rc;
63337dd3 1112
195a4ef6
MCC
1113 rc = check_dev(dev);
1114 if (rc < 0)
1115 return rc;
1116
1117 if (0 != t->index)
1118 return -EINVAL;
1119
1120 mutex_lock(&dev->lock);
1121
1122 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1123
1124 mutex_unlock(&dev->lock);
1125 return 0;
a6c2ba28
AM
1126}
1127
195a4ef6
MCC
1128static int vidioc_g_frequency(struct file *file, void *priv,
1129 struct v4l2_frequency *f)
1130{
1131 struct em28xx_fh *fh = priv;
1132 struct em28xx *dev = fh->dev;
a6c2ba28 1133
efc52a94 1134 mutex_lock(&dev->lock);
0be43754 1135 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
195a4ef6 1136 f->frequency = dev->ctl_freq;
efc52a94 1137 mutex_unlock(&dev->lock);
195a4ef6
MCC
1138
1139 return 0;
1140}
1141
1142static int vidioc_s_frequency(struct file *file, void *priv,
1143 struct v4l2_frequency *f)
a6c2ba28 1144{
195a4ef6
MCC
1145 struct em28xx_fh *fh = priv;
1146 struct em28xx *dev = fh->dev;
1147 int rc;
9c75541f 1148
195a4ef6
MCC
1149 rc = check_dev(dev);
1150 if (rc < 0)
1151 return rc;
1152
1153 if (0 != f->tuner)
1154 return -EINVAL;
1155
0be43754
MCC
1156 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1157 return -EINVAL;
1158 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
195a4ef6 1159 return -EINVAL;
a225452e
MCC
1160
1161 mutex_lock(&dev->lock);
a3a048ce 1162
195a4ef6
MCC
1163 dev->ctl_freq = f->frequency;
1164 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
a6c2ba28 1165
195a4ef6 1166 mutex_unlock(&dev->lock);
efc52a94 1167
195a4ef6
MCC
1168 return 0;
1169}
a6c2ba28 1170
1e7ad56f
MCC
1171#ifdef CONFIG_VIDEO_ADV_DEBUG
1172static int em28xx_reg_len(int reg)
1173{
1174 switch (reg) {
41facaa4
MCC
1175 case EM28XX_R40_AC97LSB:
1176 case EM28XX_R30_HSCALELOW:
1177 case EM28XX_R32_VSCALELOW:
1e7ad56f
MCC
1178 return 2;
1179 default:
1180 return 1;
1181 }
1182}
1183
1184static int vidioc_g_register(struct file *file, void *priv,
1185 struct v4l2_register *reg)
1186{
1187 struct em28xx_fh *fh = priv;
1188 struct em28xx *dev = fh->dev;
1189 int ret;
1190
1191 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1192 return -EINVAL;
1193
1194 if (em28xx_reg_len(reg->reg) == 1) {
efc52a94 1195 mutex_lock(&dev->lock);
1e7ad56f 1196 ret = em28xx_read_reg(dev, reg->reg);
efc52a94
MCC
1197 mutex_unlock(&dev->lock);
1198
1e7ad56f
MCC
1199 if (ret < 0)
1200 return ret;
1201
1202 reg->val = ret;
1203 } else {
a954b668 1204 __le64 val = 0;
efc52a94 1205 mutex_lock(&dev->lock);
1e7ad56f
MCC
1206 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1207 reg->reg, (char *)&val, 2);
efc52a94 1208 mutex_unlock(&dev->lock);
1e7ad56f
MCC
1209 if (ret < 0)
1210 return ret;
1211
a954b668 1212 reg->val = le64_to_cpu(val);
1e7ad56f
MCC
1213 }
1214
1215 return 0;
1216}
1217
1218static int vidioc_s_register(struct file *file, void *priv,
1219 struct v4l2_register *reg)
1220{
1221 struct em28xx_fh *fh = priv;
1222 struct em28xx *dev = fh->dev;
a954b668 1223 __le64 buf;
efc52a94 1224 int rc;
1e7ad56f 1225
a954b668 1226 buf = cpu_to_le64(reg->val);
1e7ad56f 1227
efc52a94
MCC
1228 mutex_lock(&dev->lock);
1229 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1230 em28xx_reg_len(reg->reg));
1231 mutex_unlock(&dev->lock);
1232
1233 return rc;
1e7ad56f
MCC
1234}
1235#endif
1236
1237
195a4ef6
MCC
1238static int vidioc_cropcap(struct file *file, void *priv,
1239 struct v4l2_cropcap *cc)
1240{
1241 struct em28xx_fh *fh = priv;
1242 struct em28xx *dev = fh->dev;
1243
1244 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
a6c2ba28 1245 return -EINVAL;
a6c2ba28 1246
195a4ef6
MCC
1247 cc->bounds.left = 0;
1248 cc->bounds.top = 0;
1249 cc->bounds.width = dev->width;
1250 cc->bounds.height = dev->height;
1251 cc->defrect = cc->bounds;
1252 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1253 cc->pixelaspect.denominator = 59;
1254
1255 return 0;
1256}
1257
1258static int vidioc_streamon(struct file *file, void *priv,
1259 enum v4l2_buf_type type)
1260{
1261 struct em28xx_fh *fh = priv;
1262 struct em28xx *dev = fh->dev;
1263 int rc;
1264
1265 rc = check_dev(dev);
1266 if (rc < 0)
1267 return rc;
1268
195a4ef6 1269
29b59417
MCC
1270 mutex_lock(&dev->lock);
1271 rc = res_get(fh);
29b59417
MCC
1272
1273 if (unlikely(rc < 0))
1274 return rc;
a6c2ba28 1275
efc52a94
MCC
1276 rc = videobuf_streamon(&fh->vb_vidq);
1277
1278 mutex_unlock(&dev->lock);
1279
1280 return rc;
195a4ef6
MCC
1281}
1282
1283static int vidioc_streamoff(struct file *file, void *priv,
1284 enum v4l2_buf_type type)
1285{
1286 struct em28xx_fh *fh = priv;
1287 struct em28xx *dev = fh->dev;
1288 int rc;
1289
1290 rc = check_dev(dev);
1291 if (rc < 0)
1292 return rc;
1293
ad0ebb96
MCC
1294 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1295 return -EINVAL;
1296 if (type != fh->type)
195a4ef6
MCC
1297 return -EINVAL;
1298
7831364f 1299 mutex_lock(&dev->lock);
efc52a94
MCC
1300
1301 videobuf_streamoff(&fh->vb_vidq);
ad0ebb96 1302 res_free(fh);
efc52a94 1303
7831364f 1304 mutex_unlock(&dev->lock);
a6c2ba28 1305
a6c2ba28
AM
1306 return 0;
1307}
1308
195a4ef6
MCC
1309static int vidioc_querycap(struct file *file, void *priv,
1310 struct v4l2_capability *cap)
a6c2ba28 1311{
195a4ef6
MCC
1312 struct em28xx_fh *fh = priv;
1313 struct em28xx *dev = fh->dev;
1314
1315 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1316 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
af128a10 1317 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
195a4ef6
MCC
1318
1319 cap->version = EM28XX_VERSION_CODE;
1320
1321 cap->capabilities =
1322 V4L2_CAP_SLICED_VBI_CAPTURE |
1323 V4L2_CAP_VIDEO_CAPTURE |
1324 V4L2_CAP_AUDIO |
1325 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1326
ed086314 1327 if (dev->tuner_type != TUNER_ABSENT)
195a4ef6
MCC
1328 cap->capabilities |= V4L2_CAP_TUNER;
1329
1330 return 0;
c0477ad9
MCC
1331}
1332
78b526a4 1333static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 1334 struct v4l2_fmtdesc *fmtd)
a6c2ba28 1335{
195a4ef6 1336 if (fmtd->index != 0)
c0477ad9 1337 return -EINVAL;
195a4ef6
MCC
1338
1339 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1340 strcpy(fmtd->description, "Packed YUY2");
1341 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1342 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1343
1344 return 0;
c0477ad9
MCC
1345}
1346
195a4ef6 1347/* Sliced VBI ioctls */
78b526a4 1348static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6 1349 struct v4l2_format *f)
a6c2ba28 1350{
195a4ef6
MCC
1351 struct em28xx_fh *fh = priv;
1352 struct em28xx *dev = fh->dev;
1353 int rc;
a6c2ba28 1354
195a4ef6
MCC
1355 rc = check_dev(dev);
1356 if (rc < 0)
1357 return rc;
a6c2ba28 1358
195a4ef6
MCC
1359 mutex_lock(&dev->lock);
1360
1361 f->fmt.sliced.service_set = 0;
1362
1363 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1364
1365 if (f->fmt.sliced.service_set == 0)
1366 rc = -EINVAL;
1367
1368 mutex_unlock(&dev->lock);
1369 return rc;
1370}
1371
78b526a4 1372static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6
MCC
1373 struct v4l2_format *f)
1374{
1375 struct em28xx_fh *fh = priv;
1376 struct em28xx *dev = fh->dev;
1377 int rc;
1378
1379 rc = check_dev(dev);
1380 if (rc < 0)
1381 return rc;
1382
1383 mutex_lock(&dev->lock);
1384 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1385 mutex_unlock(&dev->lock);
1386
1387 if (f->fmt.sliced.service_set == 0)
1388 return -EINVAL;
a6c2ba28
AM
1389
1390 return 0;
1391}
1392
195a4ef6
MCC
1393
1394static int vidioc_reqbufs(struct file *file, void *priv,
1395 struct v4l2_requestbuffers *rb)
a6c2ba28 1396{
195a4ef6
MCC
1397 struct em28xx_fh *fh = priv;
1398 struct em28xx *dev = fh->dev;
195a4ef6 1399 int rc;
a6c2ba28 1400
195a4ef6
MCC
1401 rc = check_dev(dev);
1402 if (rc < 0)
1403 return rc;
a6c2ba28 1404
ad0ebb96 1405 return (videobuf_reqbufs(&fh->vb_vidq, rb));
a6c2ba28
AM
1406}
1407
195a4ef6
MCC
1408static int vidioc_querybuf(struct file *file, void *priv,
1409 struct v4l2_buffer *b)
e5589bef 1410{
195a4ef6
MCC
1411 struct em28xx_fh *fh = priv;
1412 struct em28xx *dev = fh->dev;
1413 int rc;
e5589bef 1414
195a4ef6
MCC
1415 rc = check_dev(dev);
1416 if (rc < 0)
1417 return rc;
2d50f847 1418
ad0ebb96 1419 return (videobuf_querybuf(&fh->vb_vidq, b));
195a4ef6
MCC
1420}
1421
1422static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1423{
1424 struct em28xx_fh *fh = priv;
1425 struct em28xx *dev = fh->dev;
195a4ef6
MCC
1426 int rc;
1427
1428 rc = check_dev(dev);
1429 if (rc < 0)
1430 return rc;
1431
ad0ebb96 1432 return (videobuf_qbuf(&fh->vb_vidq, b));
e5589bef
MCC
1433}
1434
195a4ef6 1435static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2d50f847 1436{
195a4ef6
MCC
1437 struct em28xx_fh *fh = priv;
1438 struct em28xx *dev = fh->dev;
1439 int rc;
195a4ef6
MCC
1440
1441 rc = check_dev(dev);
1442 if (rc < 0)
1443 return rc;
2d50f847 1444
ad0ebb96
MCC
1445 return (videobuf_dqbuf(&fh->vb_vidq, b,
1446 file->f_flags & O_NONBLOCK));
1447}
2d50f847 1448
ad0ebb96 1449#ifdef CONFIG_VIDEO_V4L1_COMPAT
f245e549 1450static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
ad0ebb96 1451{
f245e549 1452 struct em28xx_fh *fh = priv;
2d50f847 1453
f245e549 1454 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
195a4ef6 1455}
ad0ebb96
MCC
1456#endif
1457
2d50f847 1458
0be43754
MCC
1459/* ----------------------------------------------------------- */
1460/* RADIO ESPECIFIC IOCTLS */
1461/* ----------------------------------------------------------- */
1462
1463static int radio_querycap(struct file *file, void *priv,
1464 struct v4l2_capability *cap)
1465{
1466 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1467
1468 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1469 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
af128a10 1470 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
0be43754
MCC
1471
1472 cap->version = EM28XX_VERSION_CODE;
1473 cap->capabilities = V4L2_CAP_TUNER;
1474 return 0;
1475}
1476
1477static int radio_g_tuner(struct file *file, void *priv,
1478 struct v4l2_tuner *t)
1479{
1480 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1481
1482 if (unlikely(t->index > 0))
1483 return -EINVAL;
1484
1485 strcpy(t->name, "Radio");
1486 t->type = V4L2_TUNER_RADIO;
1487
efc52a94 1488 mutex_lock(&dev->lock);
0be43754 1489 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
efc52a94
MCC
1490 mutex_unlock(&dev->lock);
1491
0be43754
MCC
1492 return 0;
1493}
1494
1495static int radio_enum_input(struct file *file, void *priv,
1496 struct v4l2_input *i)
1497{
1498 if (i->index != 0)
1499 return -EINVAL;
1500 strcpy(i->name, "Radio");
1501 i->type = V4L2_INPUT_TYPE_TUNER;
1502
1503 return 0;
1504}
1505
1506static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1507{
1508 if (unlikely(a->index))
1509 return -EINVAL;
1510
1511 strcpy(a->name, "Radio");
1512 return 0;
1513}
1514
1515static int radio_s_tuner(struct file *file, void *priv,
1516 struct v4l2_tuner *t)
1517{
1518 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1519
1520 if (0 != t->index)
1521 return -EINVAL;
1522
efc52a94 1523 mutex_lock(&dev->lock);
0be43754 1524 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
efc52a94 1525 mutex_unlock(&dev->lock);
0be43754
MCC
1526
1527 return 0;
1528}
1529
1530static int radio_s_audio(struct file *file, void *fh,
1531 struct v4l2_audio *a)
1532{
1533 return 0;
1534}
1535
1536static int radio_s_input(struct file *file, void *fh, unsigned int i)
1537{
1538 return 0;
1539}
1540
1541static int radio_queryctrl(struct file *file, void *priv,
1542 struct v4l2_queryctrl *qc)
1543{
1544 int i;
1545
1546 if (qc->id < V4L2_CID_BASE ||
1547 qc->id >= V4L2_CID_LASTP1)
1548 return -EINVAL;
1549
1550 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1551 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1552 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1553 return 0;
1554 }
1555 }
1556
1557 return -EINVAL;
1558}
1559
195a4ef6
MCC
1560/*
1561 * em28xx_v4l2_open()
1562 * inits the device and starts isoc transfer
1563 */
1564static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1565{
1566 int minor = iminor(inode);
0be43754 1567 int errCode = 0, radio = 0;
6ea54d93 1568 struct em28xx *h, *dev = NULL;
195a4ef6 1569 struct em28xx_fh *fh;
d7aa8020 1570 enum v4l2_buf_type fh_type = 0;
2d50f847 1571
818a557e 1572 mutex_lock(&em28xx_devlist_mutex);
195a4ef6
MCC
1573 list_for_each_entry(h, &em28xx_devlist, devlist) {
1574 if (h->vdev->minor == minor) {
1575 dev = h;
d7aa8020 1576 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
195a4ef6
MCC
1577 }
1578 if (h->vbi_dev->minor == minor) {
1579 dev = h;
d7aa8020 1580 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
195a4ef6 1581 }
0be43754
MCC
1582 if (h->radio_dev &&
1583 h->radio_dev->minor == minor) {
1584 radio = 1;
1585 dev = h;
1586 }
2d50f847 1587 }
818a557e 1588 mutex_unlock(&em28xx_devlist_mutex);
efc52a94 1589
818a557e 1590 if (NULL == dev)
195a4ef6 1591 return -ENODEV;
818a557e
MCC
1592
1593 mutex_lock(&dev->lock);
2d50f847 1594
195a4ef6 1595 em28xx_videodbg("open minor=%d type=%s users=%d\n",
d7aa8020 1596 minor, v4l2_type_names[fh_type], dev->users);
2d50f847 1597
9aeb4b05 1598
c67ec53f 1599 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
195a4ef6
MCC
1600 if (!fh) {
1601 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
818a557e 1602 mutex_unlock(&dev->lock);
195a4ef6
MCC
1603 return -ENOMEM;
1604 }
195a4ef6 1605 fh->dev = dev;
0be43754 1606 fh->radio = radio;
d7aa8020 1607 fh->type = fh_type;
195a4ef6 1608 filp->private_data = fh;
9aeb4b05 1609
d7aa8020 1610 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
195a4ef6
MCC
1611 dev->width = norm_maxw(dev);
1612 dev->height = norm_maxh(dev);
195a4ef6
MCC
1613 dev->hscale = 0;
1614 dev->vscale = 0;
2d50f847 1615
c67ec53f 1616 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
3687e1e6 1617 em28xx_set_alternate(dev);
195a4ef6 1618 em28xx_resolution_set(dev);
2d50f847 1619
c67ec53f
MCC
1620 /* Needed, since GPIO might have disabled power of
1621 some i2c device
1622 */
1623 em28xx_config_i2c(dev);
1624
2d50f847 1625 }
0be43754
MCC
1626 if (fh->radio) {
1627 em28xx_videodbg("video_open: setting radio device\n");
1628 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1629 }
2d50f847 1630
195a4ef6 1631 dev->users++;
2d50f847 1632
ad0ebb96
MCC
1633 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1634 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1635 sizeof(struct em28xx_buffer), fh);
1636
195a4ef6 1637 mutex_unlock(&dev->lock);
c67ec53f 1638
195a4ef6 1639 return errCode;
2d50f847 1640}
e5589bef 1641
a6c2ba28 1642/*
195a4ef6
MCC
1643 * em28xx_realease_resources()
1644 * unregisters the v4l2,i2c and usb devices
1645 * called when the device gets disconected or at module unload
1646*/
1647static void em28xx_release_resources(struct em28xx *dev)
a6c2ba28 1648{
a6c2ba28 1649
195a4ef6 1650 /*FIXME: I2C IR should be disconnected */
a6c2ba28 1651
195a4ef6 1652 list_del(&dev->devlist);
a9fc52bc
DH
1653 if (dev->sbutton_input_dev)
1654 em28xx_deregister_snapshot_button(dev);
a924a499
MCC
1655
1656 if (dev->ir)
1657 em28xx_ir_fini(dev);
1658
0be43754
MCC
1659 if (dev->radio_dev) {
1660 if (-1 != dev->radio_dev->minor)
1661 video_unregister_device(dev->radio_dev);
1662 else
1663 video_device_release(dev->radio_dev);
1664 dev->radio_dev = NULL;
1665 }
1666 if (dev->vbi_dev) {
49240444
DH
1667 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1668 dev->vbi_dev->num);
0be43754
MCC
1669 if (-1 != dev->vbi_dev->minor)
1670 video_unregister_device(dev->vbi_dev);
1671 else
1672 video_device_release(dev->vbi_dev);
1673 dev->vbi_dev = NULL;
1674 }
1675 if (dev->vdev) {
49240444
DH
1676 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1677 dev->vdev->num);
0be43754
MCC
1678 if (-1 != dev->vdev->minor)
1679 video_unregister_device(dev->vdev);
1680 else
1681 video_device_release(dev->vdev);
1682 dev->vdev = NULL;
1683 }
195a4ef6
MCC
1684 em28xx_i2c_unregister(dev);
1685 usb_put_dev(dev->udev);
a6c2ba28 1686
195a4ef6 1687 /* Mark device as unused */
6ea54d93 1688 em28xx_devused &= ~(1<<dev->devno);
195a4ef6 1689}
a6c2ba28 1690
195a4ef6
MCC
1691/*
1692 * em28xx_v4l2_close()
6ea54d93
DSL
1693 * stops streaming and deallocates all resources allocated by the v4l2
1694 * calls and ioctls
195a4ef6
MCC
1695 */
1696static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1697{
1698 struct em28xx_fh *fh = filp->private_data;
1699 struct em28xx *dev = fh->dev;
1700 int errCode;
a6c2ba28 1701
195a4ef6 1702 em28xx_videodbg("users=%d\n", dev->users);
a6c2ba28 1703
a6c2ba28 1704
7831364f 1705 mutex_lock(&dev->lock);
195a4ef6
MCC
1706 if (res_check(fh))
1707 res_free(fh);
a6c2ba28 1708
195a4ef6 1709 if (dev->users == 1) {
ad0ebb96
MCC
1710 videobuf_stop(&fh->vb_vidq);
1711 videobuf_mmap_free(&fh->vb_vidq);
a6c2ba28 1712
195a4ef6
MCC
1713 /* the device is already disconnect,
1714 free the remaining resources */
1715 if (dev->state & DEV_DISCONNECTED) {
1716 em28xx_release_resources(dev);
1717 mutex_unlock(&dev->lock);
1718 kfree(dev);
1719 return 0;
1720 }
a6c2ba28 1721
eb6c9634
MCC
1722 /* Save some power by putting tuner to sleep */
1723 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1724
d7aa8020
AT
1725 /* do this before setting alternate! */
1726 em28xx_uninit_isoc(dev);
2fe3e2ee 1727 em28xx_set_mode(dev, EM28XX_SUSPEND);
d7aa8020 1728
195a4ef6
MCC
1729 /* set alternate 0 */
1730 dev->alt = 0;
1731 em28xx_videodbg("setting alternate 0\n");
1732 errCode = usb_set_interface(dev->udev, 0, 0);
1733 if (errCode < 0) {
1734 em28xx_errdev("cannot change alternate number to "
1735 "0 (error=%i)\n", errCode);
1736 }
9aeb4b05 1737 }
195a4ef6
MCC
1738 kfree(fh);
1739 dev->users--;
1740 wake_up_interruptible_nr(&dev->open, 1);
1741 mutex_unlock(&dev->lock);
1742 return 0;
1743}
a6c2ba28 1744
195a4ef6
MCC
1745/*
1746 * em28xx_v4l2_read()
1747 * will allocate buffers when called for the first time
1748 */
1749static ssize_t
6ea54d93 1750em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
f245e549 1751 loff_t *pos)
195a4ef6 1752{
195a4ef6
MCC
1753 struct em28xx_fh *fh = filp->private_data;
1754 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
1755 int rc;
1756
1757 rc = check_dev(dev);
1758 if (rc < 0)
1759 return rc;
a6c2ba28 1760
195a4ef6
MCC
1761 /* FIXME: read() is not prepared to allow changing the video
1762 resolution while streaming. Seems a bug at em28xx_set_fmt
1763 */
a6c2ba28 1764
ad0ebb96 1765 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
29b59417
MCC
1766 mutex_lock(&dev->lock);
1767 rc = res_get(fh);
1768 mutex_unlock(&dev->lock);
1769
1770 if (unlikely(rc < 0))
1771 return rc;
195a4ef6 1772
ad0ebb96
MCC
1773 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1774 filp->f_flags & O_NONBLOCK);
195a4ef6 1775 }
ad0ebb96 1776 return 0;
a6c2ba28
AM
1777}
1778
1779/*
195a4ef6
MCC
1780 * em28xx_v4l2_poll()
1781 * will allocate buffers when called for the first time
a6c2ba28 1782 */
195a4ef6 1783static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
a6c2ba28 1784{
a3a048ce 1785 struct em28xx_fh *fh = filp->private_data;
195a4ef6 1786 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
1787 int rc;
1788
1789 rc = check_dev(dev);
1790 if (rc < 0)
1791 return rc;
a6c2ba28 1792
29b59417
MCC
1793 mutex_lock(&dev->lock);
1794 rc = res_get(fh);
1795 mutex_unlock(&dev->lock);
1796
1797 if (unlikely(rc < 0))
195a4ef6 1798 return POLLERR;
a6c2ba28 1799
ad0ebb96
MCC
1800 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1801 return POLLERR;
a6c2ba28 1802
ad0ebb96 1803 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
195a4ef6 1804}
a6c2ba28 1805
195a4ef6
MCC
1806/*
1807 * em28xx_v4l2_mmap()
1808 */
1809static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1810{
1811 struct em28xx_fh *fh = filp->private_data;
1812 struct em28xx *dev = fh->dev;
ad0ebb96 1813 int rc;
a6c2ba28 1814
ad0ebb96
MCC
1815 rc = check_dev(dev);
1816 if (rc < 0)
1817 return rc;
a6c2ba28 1818
29b59417
MCC
1819 mutex_lock(&dev->lock);
1820 rc = res_get(fh);
1821 mutex_unlock(&dev->lock);
1822
1823 if (unlikely(rc < 0))
1824 return rc;
1825
ad0ebb96 1826 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
a6c2ba28 1827
ad0ebb96
MCC
1828 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1829 (unsigned long)vma->vm_start,
1830 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1831 rc);
a6c2ba28 1832
ad0ebb96 1833 return rc;
a6c2ba28
AM
1834}
1835
fa027c2a 1836static const struct file_operations em28xx_v4l_fops = {
195a4ef6
MCC
1837 .owner = THIS_MODULE,
1838 .open = em28xx_v4l2_open,
1839 .release = em28xx_v4l2_close,
1840 .read = em28xx_v4l2_read,
1841 .poll = em28xx_v4l2_poll,
1842 .mmap = em28xx_v4l2_mmap,
1843 .ioctl = video_ioctl2,
1844 .llseek = no_llseek,
1845 .compat_ioctl = v4l_compat_ioctl32,
1846};
17cbe2e5 1847
a399810c 1848static const struct v4l2_ioctl_ops video_ioctl_ops = {
195a4ef6 1849 .vidioc_querycap = vidioc_querycap,
78b526a4
HV
1850 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1851 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1852 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1853 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
195a4ef6
MCC
1854 .vidioc_g_audio = vidioc_g_audio,
1855 .vidioc_s_audio = vidioc_s_audio,
1856 .vidioc_cropcap = vidioc_cropcap,
1857
78b526a4
HV
1858 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1859 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1860 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
195a4ef6
MCC
1861
1862 .vidioc_reqbufs = vidioc_reqbufs,
1863 .vidioc_querybuf = vidioc_querybuf,
1864 .vidioc_qbuf = vidioc_qbuf,
1865 .vidioc_dqbuf = vidioc_dqbuf,
1866 .vidioc_s_std = vidioc_s_std,
1867 .vidioc_enum_input = vidioc_enum_input,
1868 .vidioc_g_input = vidioc_g_input,
1869 .vidioc_s_input = vidioc_s_input,
1870 .vidioc_queryctrl = vidioc_queryctrl,
1871 .vidioc_g_ctrl = vidioc_g_ctrl,
1872 .vidioc_s_ctrl = vidioc_s_ctrl,
1873 .vidioc_streamon = vidioc_streamon,
1874 .vidioc_streamoff = vidioc_streamoff,
1875 .vidioc_g_tuner = vidioc_g_tuner,
1876 .vidioc_s_tuner = vidioc_s_tuner,
1877 .vidioc_g_frequency = vidioc_g_frequency,
1878 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
1879#ifdef CONFIG_VIDEO_ADV_DEBUG
1880 .vidioc_g_register = vidioc_g_register,
1881 .vidioc_s_register = vidioc_s_register,
1882#endif
ad0ebb96
MCC
1883#ifdef CONFIG_VIDEO_V4L1_COMPAT
1884 .vidiocgmbuf = vidiocgmbuf,
1885#endif
a399810c
HV
1886};
1887
1888static const struct video_device em28xx_video_template = {
1889 .fops = &em28xx_v4l_fops,
1890 .release = video_device_release,
1891 .ioctl_ops = &video_ioctl_ops,
1892
1893 .minor = -1,
195a4ef6
MCC
1894
1895 .tvnorms = V4L2_STD_ALL,
7d497f8a 1896 .current_norm = V4L2_STD_PAL,
a6c2ba28
AM
1897};
1898
a399810c
HV
1899static const struct file_operations radio_fops = {
1900 .owner = THIS_MODULE,
1901 .open = em28xx_v4l2_open,
1902 .release = em28xx_v4l2_close,
1903 .ioctl = video_ioctl2,
1904 .compat_ioctl = v4l_compat_ioctl32,
1905 .llseek = no_llseek,
1906};
1907
1908static const struct v4l2_ioctl_ops radio_ioctl_ops = {
0be43754
MCC
1909 .vidioc_querycap = radio_querycap,
1910 .vidioc_g_tuner = radio_g_tuner,
1911 .vidioc_enum_input = radio_enum_input,
1912 .vidioc_g_audio = radio_g_audio,
1913 .vidioc_s_tuner = radio_s_tuner,
1914 .vidioc_s_audio = radio_s_audio,
1915 .vidioc_s_input = radio_s_input,
1916 .vidioc_queryctrl = radio_queryctrl,
1917 .vidioc_g_ctrl = vidioc_g_ctrl,
1918 .vidioc_s_ctrl = vidioc_s_ctrl,
1919 .vidioc_g_frequency = vidioc_g_frequency,
1920 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
1921#ifdef CONFIG_VIDEO_ADV_DEBUG
1922 .vidioc_g_register = vidioc_g_register,
1923 .vidioc_s_register = vidioc_s_register,
1924#endif
0be43754
MCC
1925};
1926
a399810c
HV
1927static struct video_device em28xx_radio_template = {
1928 .name = "em28xx-radio",
a399810c
HV
1929 .fops = &radio_fops,
1930 .ioctl_ops = &radio_ioctl_ops,
1931 .minor = -1,
1932};
1933
6ea54d93 1934/******************************** usb interface ******************************/
a6c2ba28 1935
6d79468d
MCC
1936
1937static LIST_HEAD(em28xx_extension_devlist);
1938static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1939
1940int em28xx_register_extension(struct em28xx_ops *ops)
1941{
0367ca1b 1942 struct em28xx *dev = NULL;
6d79468d 1943
818a557e 1944 mutex_lock(&em28xx_devlist_mutex);
6d79468d
MCC
1945 mutex_lock(&em28xx_extension_devlist_lock);
1946 list_add_tail(&ops->next, &em28xx_extension_devlist);
0367ca1b
DH
1947 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1948 if (dev)
1949 ops->init(dev);
1950 }
6d79468d
MCC
1951 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1952 mutex_unlock(&em28xx_extension_devlist_lock);
818a557e 1953 mutex_unlock(&em28xx_devlist_mutex);
6d79468d
MCC
1954 return 0;
1955}
1956EXPORT_SYMBOL(em28xx_register_extension);
1957
1958void em28xx_unregister_extension(struct em28xx_ops *ops)
1959{
0367ca1b 1960 struct em28xx *dev = NULL;
6d79468d 1961
818a557e 1962 mutex_lock(&em28xx_devlist_mutex);
0367ca1b
DH
1963 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1964 if (dev)
1965 ops->fini(dev);
1966 }
6d79468d
MCC
1967
1968 mutex_lock(&em28xx_extension_devlist_lock);
1969 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1970 list_del(&ops->next);
1971 mutex_unlock(&em28xx_extension_devlist_lock);
818a557e 1972 mutex_unlock(&em28xx_devlist_mutex);
6d79468d
MCC
1973}
1974EXPORT_SYMBOL(em28xx_unregister_extension);
1975
532fe652
AB
1976static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1977 const struct video_device *template,
532fe652 1978 const char *type_name)
0be43754
MCC
1979{
1980 struct video_device *vfd;
1981
1982 vfd = video_device_alloc();
1983 if (NULL == vfd)
1984 return NULL;
1985 *vfd = *template;
1986 vfd->minor = -1;
5e85e732 1987 vfd->parent = &dev->udev->dev;
0be43754 1988 vfd->release = video_device_release;
e9e6040d 1989 vfd->debug = video_debug;
0be43754
MCC
1990
1991 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1992 dev->name, type_name);
1993
1994 return vfd;
1995}
1996
818a557e
MCC
1997static int register_analog_devices(struct em28xx *dev)
1998{
1999 int ret;
2000
2001 /* allocate and fill video video_device struct */
2002 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2003 if (!dev->vdev) {
2004 em28xx_errdev("cannot allocate video_device.\n");
2005 return -ENODEV;
2006 }
2007
2008 /* register v4l2 video video_device */
2009 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2010 video_nr[dev->devno]);
2011 if (ret) {
2012 em28xx_errdev("unable to register video device (error=%i).\n",
2013 ret);
2014 return ret;
2015 }
2016
2017 /* Allocate and fill vbi video_device struct */
2018 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2019
2020 /* register v4l2 vbi video_device */
2021 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2022 vbi_nr[dev->devno]);
2023 if (ret < 0) {
2024 em28xx_errdev("unable to register vbi device\n");
2025 return ret;
2026 }
2027
2028 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2029 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2030 if (!dev->radio_dev) {
2031 em28xx_errdev("cannot allocate video_device.\n");
2032 return -ENODEV;
2033 }
2034 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2035 radio_nr[dev->devno]);
2036 if (ret < 0) {
2037 em28xx_errdev("can't register radio device\n");
2038 return ret;
2039 }
2040 em28xx_info("Registered radio device as /dev/radio%d\n",
2041 dev->radio_dev->num);
2042 }
2043
2044 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2045 dev->vdev->num, dev->vbi_dev->num);
2046
2047 return 0;
2048}
2049
2050
a6c2ba28 2051/*
3acf2809 2052 * em28xx_init_dev()
a6c2ba28
AM
2053 * allocates and inits the device structs, registers i2c bus and v4l device
2054 */
3acf2809 2055static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
03910cc3 2056 int minor)
a6c2ba28 2057{
6d79468d 2058 struct em28xx_ops *ops = NULL;
3acf2809 2059 struct em28xx *dev = *devhandle;
a6c2ba28 2060 int retval = -ENOMEM;
7d497f8a 2061 int errCode;
a6c2ba28 2062 unsigned int maxh, maxw;
a6c2ba28 2063
a6c2ba28 2064 dev->udev = udev;
f2a2e491 2065 mutex_init(&dev->ctrl_urb_lock);
d7aa8020 2066 spin_lock_init(&dev->slock);
a6c2ba28 2067 init_waitqueue_head(&dev->open);
a3a048ce
MCC
2068 init_waitqueue_head(&dev->wait_frame);
2069 init_waitqueue_head(&dev->wait_stream);
a6c2ba28 2070
3acf2809
MCC
2071 dev->em28xx_write_regs = em28xx_write_regs;
2072 dev->em28xx_read_reg = em28xx_read_reg;
2073 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2074 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2075 dev->em28xx_read_reg_req = em28xx_read_reg_req;
505b6d0b 2076 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
a6c2ba28 2077
a94e95b4 2078 em28xx_pre_card_setup(dev);
03910cc3 2079
3acf2809 2080 errCode = em28xx_config(dev);
a6c2ba28 2081 if (errCode) {
3acf2809 2082 em28xx_errdev("error configuring device\n");
a6c2ba28
AM
2083 return -ENOMEM;
2084 }
2085
a6c2ba28 2086 /* register i2c bus */
f2a01a00
DSL
2087 errCode = em28xx_i2c_register(dev);
2088 if (errCode < 0) {
2089 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2090 __func__, errCode);
2091 return errCode;
2092 }
a6c2ba28
AM
2093
2094 /* Do board specific init and eeprom reading */
3acf2809 2095 em28xx_card_setup(dev);
a6c2ba28 2096
3abee53e 2097 /* Configure audio */
35643943 2098 errCode = em28xx_audio_setup(dev);
f2a01a00 2099 if (errCode < 0) {
35643943 2100 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
f2a01a00 2101 __func__, errCode);
f2a01a00 2102 }
3abee53e 2103
a6c2ba28 2104 /* configure the device */
3acf2809 2105 em28xx_config_i2c(dev);
a6c2ba28 2106
7d497f8a
MCC
2107 /* set default norm */
2108 dev->norm = em28xx_video_template.current_norm;
03910cc3
MCC
2109
2110 maxw = norm_maxw(dev);
2111 maxh = norm_maxh(dev);
2112
2113 /* set default image size */
2114 dev->width = maxw;
2115 dev->height = maxh;
2116 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
03910cc3
MCC
2117 dev->hscale = 0;
2118 dev->vscale = 0;
2119 dev->ctl_input = 2;
2120
3acf2809 2121 errCode = em28xx_config(dev);
f2a01a00
DSL
2122 if (errCode < 0) {
2123 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2124 __func__, errCode);
2125 return errCode;
2126 }
a6c2ba28 2127
ad0ebb96
MCC
2128 /* init video dma queues */
2129 INIT_LIST_HEAD(&dev->vidq.active);
2130 INIT_LIST_HEAD(&dev->vidq.queued);
2131
a6c2ba28 2132
505b6d0b 2133 if (dev->board.has_msp34xx) {
5a80415b 2134 /* Send a reset to other chips via gpio */
2a29a0d7 2135 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
f2a01a00
DSL
2136 if (errCode < 0) {
2137 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2138 __func__, errCode);
2139 return errCode;
2140 }
5a80415b 2141 msleep(3);
f2a01a00 2142
2a29a0d7 2143 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
f2a01a00
DSL
2144 if (errCode < 0) {
2145 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2146 __func__, errCode);
2147 return errCode;
2148 }
5a80415b 2149 msleep(3);
5a80415b 2150 }
195a4ef6 2151
5a80415b
SS
2152 video_mux(dev, 0);
2153
818a557e
MCC
2154 mutex_lock(&em28xx_devlist_mutex);
2155 list_add_tail(&dev->devlist, &em28xx_devlist);
2156 retval = register_analog_devices(dev);
2157 if (retval < 0) {
2158 em28xx_release_resources(dev);
2159 mutex_unlock(&em28xx_devlist_mutex);
2160 goto fail_reg_devices;
2161 }
a6c2ba28 2162
6d79468d
MCC
2163 mutex_lock(&em28xx_extension_devlist_lock);
2164 if (!list_empty(&em28xx_extension_devlist)) {
2165 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2166 if (ops->id)
2167 ops->init(dev);
2168 }
2169 }
2170 mutex_unlock(&em28xx_extension_devlist_lock);
818a557e 2171 mutex_unlock(&em28xx_devlist_mutex);
6d79468d 2172
eb6c9634
MCC
2173 /* Save some power by putting tuner to sleep */
2174 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2175
a6c2ba28 2176 return 0;
0be43754 2177
818a557e 2178fail_reg_devices:
0be43754 2179 return retval;
a6c2ba28
AM
2180}
2181
d7448a8d
MCC
2182#if defined(CONFIG_MODULES) && defined(MODULE)
2183static void request_module_async(struct work_struct *work)
2184{
2185 struct em28xx *dev = container_of(work,
2186 struct em28xx, request_module_wk);
2187
3f4dfe2a
MCC
2188 if (dev->has_audio_class)
2189 request_module("snd-usb-audio");
24a613e4 2190 else if (dev->has_alsa_audio)
d7448a8d 2191 request_module("em28xx-alsa");
3aefb79a 2192
505b6d0b 2193 if (dev->board.has_dvb)
3aefb79a 2194 request_module("em28xx-dvb");
d7448a8d
MCC
2195}
2196
2197static void request_modules(struct em28xx *dev)
2198{
2199 INIT_WORK(&dev->request_module_wk, request_module_async);
2200 schedule_work(&dev->request_module_wk);
2201}
2202#else
2203#define request_modules(dev)
2204#endif /* CONFIG_MODULES */
2205
a6c2ba28 2206/*
3acf2809 2207 * em28xx_usb_probe()
a6c2ba28
AM
2208 * checks for supported devices
2209 */
3acf2809 2210static int em28xx_usb_probe(struct usb_interface *interface,
a6c2ba28
AM
2211 const struct usb_device_id *id)
2212{
2213 const struct usb_endpoint_descriptor *endpoint;
2214 struct usb_device *udev;
9d4d9c05 2215 struct usb_interface *uif;
3acf2809 2216 struct em28xx *dev = NULL;
a6c2ba28 2217 int retval = -ENODEV;
95ea4705 2218 int i, nr, ifnum, isoc_pipe;
6a18eaf6 2219 char *speed;
8c239989 2220 char descr[255] = "";
a6c2ba28
AM
2221
2222 udev = usb_get_dev(interface_to_usbdev(interface));
d5e52653
MCC
2223 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2224
e5589bef 2225 /* Check to see next free device and mark as used */
6ea54d93
DSL
2226 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2227 em28xx_devused |= 1<<nr;
a6c2ba28 2228
596d92d5 2229 /* Don't register audio interfaces */
91cad0f2 2230 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
6a18eaf6
MCC
2231 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2232 "interface %i, class %i\n",
2233 le16_to_cpu(udev->descriptor.idVendor),
2234 le16_to_cpu(udev->descriptor.idProduct),
2235 ifnum,
2236 interface->altsetting[0].desc.bInterfaceClass);
e5589bef 2237
6ea54d93 2238 em28xx_devused &= ~(1<<nr);
596d92d5 2239 return -ENODEV;
91cad0f2
MCC
2240 }
2241
95ea4705 2242 endpoint = &interface->cur_altsetting->endpoint[0].desc;
d5e52653 2243
59c51591 2244 /* check if the device has the iso in endpoint at the correct place */
95ea4705
DH
2245 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2246 USB_ENDPOINT_XFER_ISOC &&
2247 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2248 {
2249 /* It's a newer em2874/em2875 device */
2250 isoc_pipe = 0;
2251 } else {
c9455fbb 2252 int check_interface = 1;
95ea4705
DH
2253 isoc_pipe = 1;
2254 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2255 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
c9455fbb
MCC
2256 USB_ENDPOINT_XFER_ISOC)
2257 check_interface = 0;
2258
2259 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2260 check_interface = 0;
2261
2262 if (!check_interface) {
2263 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2264 "interface %i, class %i found.\n",
6a18eaf6
MCC
2265 le16_to_cpu(udev->descriptor.idVendor),
2266 le16_to_cpu(udev->descriptor.idProduct),
c9455fbb
MCC
2267 ifnum,
2268 interface->altsetting[0].desc.bInterfaceClass);
2269
2270 em28xx_err(DRIVER_NAME " This is an anciliary "
2271 "interface not used by the driver\n");
2272
95ea4705
DH
2273 em28xx_devused &= ~(1<<nr);
2274 return -ENODEV;
2275 }
6a18eaf6 2276 }
c9455fbb 2277
6a18eaf6
MCC
2278 switch (udev->speed) {
2279 case USB_SPEED_LOW:
2280 speed = "1.5";
2281 break;
2282 case USB_SPEED_UNKNOWN:
2283 case USB_SPEED_FULL:
2284 speed = "12";
2285 break;
2286 case USB_SPEED_HIGH:
2287 speed = "480";
2288 break;
2289 default:
2290 speed = "unknown";
a6c2ba28
AM
2291 }
2292
8c239989
MCC
2293 if (udev->manufacturer)
2294 strlcpy(descr, udev->manufacturer, sizeof(descr));
2295
2296 if (udev->product) {
2297 if (*descr)
2298 strlcat(descr, " ", sizeof(descr));
2299 strlcat(descr, udev->product, sizeof(descr));
2300 }
2301 if (*descr)
2302 strlcat(descr, " ", sizeof(descr));
2303
2304 printk(DRIVER_NAME ": New device %s@ %s Mbps "
6a18eaf6 2305 "(%04x:%04x, interface %d, class %d)\n",
8c239989 2306 descr,
6a18eaf6
MCC
2307 speed,
2308 le16_to_cpu(udev->descriptor.idVendor),
2309 le16_to_cpu(udev->descriptor.idProduct),
2310 ifnum,
2311 interface->altsetting->desc.bInterfaceNumber);
c9455fbb 2312
19478843 2313 if (nr >= EM28XX_MAXBOARDS) {
6ea54d93
DSL
2314 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2315 EM28XX_MAXBOARDS);
2316 em28xx_devused &= ~(1<<nr);
596d92d5
MCC
2317 return -ENOMEM;
2318 }
2319
2320 /* allocate memory for our device state and initialize it */
7408187d 2321 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
596d92d5 2322 if (dev == NULL) {
3acf2809 2323 em28xx_err(DRIVER_NAME ": out of memory!\n");
6ea54d93 2324 em28xx_devused &= ~(1<<nr);
596d92d5
MCC
2325 return -ENOMEM;
2326 }
596d92d5 2327
e5589bef 2328 snprintf(dev->name, 29, "em28xx #%d", nr);
03910cc3
MCC
2329 dev->devno = nr;
2330 dev->model = id->driver_info;
3687e1e6 2331 dev->alt = -1;
e5589bef 2332
d7448a8d
MCC
2333 /* Checks if audio is provided by some interface */
2334 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2335 uif = udev->config->interface[i];
2336 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2337 dev->has_audio_class = 1;
2338 break;
2339 }
2340 }
2341
9d4d9c05
MCC
2342 /* compute alternate max packet sizes */
2343 uif = udev->actconfig->interface[0];
2344
6ea54d93 2345 dev->num_alt = uif->num_altsetting;
1bee0184 2346 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
6ea54d93
DSL
2347/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2348 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2349
9d4d9c05 2350 if (dev->alt_max_pkt_size == NULL) {
e5589bef 2351 em28xx_errdev("out of memory!\n");
6ea54d93 2352 em28xx_devused &= ~(1<<nr);
1207cf84 2353 kfree(dev);
9d4d9c05
MCC
2354 return -ENOMEM;
2355 }
2356
2357 for (i = 0; i < dev->num_alt ; i++) {
95ea4705 2358 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
9d4d9c05
MCC
2359 wMaxPacketSize);
2360 dev->alt_max_pkt_size[i] =
2361 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1bee0184 2362 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
6ea54d93 2363 dev->alt_max_pkt_size[i]);
9d4d9c05
MCC
2364 }
2365
6ea54d93 2366 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
03910cc3 2367 dev->model = card[nr];
596d92d5 2368
a6c2ba28 2369 /* allocate device struct */
efc52a94
MCC
2370 mutex_init(&dev->lock);
2371 mutex_lock(&dev->lock);
03910cc3 2372 retval = em28xx_init_dev(&dev, udev, nr);
625ff167
MCC
2373 if (retval) {
2374 em28xx_devused &= ~(1<<dev->devno);
2375 kfree(dev);
2376
a6c2ba28 2377 return retval;
625ff167 2378 }
a6c2ba28 2379
a6c2ba28
AM
2380 /* save our data pointer in this interface device */
2381 usb_set_intfdata(interface, dev);
d7448a8d
MCC
2382
2383 request_modules(dev);
2384
efc52a94
MCC
2385 /* Should be the last thing to do, to avoid newer udev's to
2386 open the device before fully initializing it
2387 */
2388 mutex_unlock(&dev->lock);
2389
a6c2ba28
AM
2390 return 0;
2391}
2392
2393/*
3acf2809 2394 * em28xx_usb_disconnect()
a6c2ba28
AM
2395 * called when the device gets diconencted
2396 * video device will be unregistered on v4l2_close in case it is still open
2397 */
3acf2809 2398static void em28xx_usb_disconnect(struct usb_interface *interface)
a6c2ba28 2399{
5a80415b 2400 struct em28xx *dev;
6d79468d 2401 struct em28xx_ops *ops = NULL;
5a80415b
SS
2402
2403 dev = usb_get_intfdata(interface);
a6c2ba28
AM
2404 usb_set_intfdata(interface, NULL);
2405
2406 if (!dev)
2407 return;
2408
5a80415b 2409 em28xx_info("disconnecting %s\n", dev->vdev->name);
a6c2ba28 2410
6ea54d93
DSL
2411 /* wait until all current v4l2 io is finished then deallocate
2412 resources */
3593cab5 2413 mutex_lock(&dev->lock);
a6c2ba28 2414
a6c2ba28
AM
2415 wake_up_interruptible_all(&dev->open);
2416
2417 if (dev->users) {
3acf2809 2418 em28xx_warn
a6c2ba28 2419 ("device /dev/video%d is open! Deregistration and memory "
e5589bef 2420 "deallocation are deferred on close.\n",
dd89601d 2421 dev->vdev->num);
e5589bef 2422
a6c2ba28 2423 dev->state |= DEV_MISCONFIGURED;
3acf2809 2424 em28xx_uninit_isoc(dev);
a6c2ba28
AM
2425 dev->state |= DEV_DISCONNECTED;
2426 wake_up_interruptible(&dev->wait_frame);
2427 wake_up_interruptible(&dev->wait_stream);
2428 } else {
2429 dev->state |= DEV_DISCONNECTED;
3acf2809 2430 em28xx_release_resources(dev);
a6c2ba28 2431 }
3593cab5 2432 mutex_unlock(&dev->lock);
a6c2ba28 2433
6d79468d
MCC
2434 mutex_lock(&em28xx_extension_devlist_lock);
2435 if (!list_empty(&em28xx_extension_devlist)) {
2436 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2437 ops->fini(dev);
2438 }
2439 }
2440 mutex_unlock(&em28xx_extension_devlist_lock);
2441
9d4d9c05
MCC
2442 if (!dev->users) {
2443 kfree(dev->alt_max_pkt_size);
a6c2ba28 2444 kfree(dev);
9d4d9c05 2445 }
a6c2ba28
AM
2446}
2447
3acf2809 2448static struct usb_driver em28xx_usb_driver = {
3acf2809
MCC
2449 .name = "em28xx",
2450 .probe = em28xx_usb_probe,
2451 .disconnect = em28xx_usb_disconnect,
2452 .id_table = em28xx_id_table,
a6c2ba28
AM
2453};
2454
3acf2809 2455static int __init em28xx_module_init(void)
a6c2ba28
AM
2456{
2457 int result;
2458
2459 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
3acf2809
MCC
2460 (EM28XX_VERSION_CODE >> 16) & 0xff,
2461 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
a6c2ba28
AM
2462#ifdef SNAPSHOT
2463 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2464 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2465#endif
2466
2467 /* register this driver with the USB subsystem */
3acf2809 2468 result = usb_register(&em28xx_usb_driver);
a6c2ba28 2469 if (result)
3acf2809 2470 em28xx_err(DRIVER_NAME
a6c2ba28
AM
2471 " usb_register failed. Error number %d.\n", result);
2472
2473 return result;
2474}
2475
3acf2809 2476static void __exit em28xx_module_exit(void)
a6c2ba28
AM
2477{
2478 /* deregister this driver with the USB subsystem */
3acf2809 2479 usb_deregister(&em28xx_usb_driver);
a6c2ba28
AM
2480}
2481
3acf2809
MCC
2482module_init(em28xx_module_init);
2483module_exit(em28xx_module_exit);