]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (9911): em28xx: vidioc_try_fmt_vid_cap() doesn't need any lock
[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
78b526a4 787 vidioc_try_fmt_vid_cap(file, priv, f);
a225452e 788
5a80415b
SS
789 mutex_lock(&dev->lock);
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;
195a4ef6 831 mutex_unlock(&dev->lock);
a6c2ba28 832
195a4ef6
MCC
833 /* Adjusts width/height, if needed */
834 f.fmt.pix.width = dev->width;
835 f.fmt.pix.height = dev->height;
78b526a4 836 vidioc_try_fmt_vid_cap(file, priv, &f);
a6c2ba28 837
195a4ef6 838 mutex_lock(&dev->lock);
a6c2ba28 839
195a4ef6
MCC
840 /* set new image size */
841 dev->width = f.fmt.pix.width;
842 dev->height = f.fmt.pix.height;
195a4ef6 843 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
a6c2ba28 844
195a4ef6 845 em28xx_resolution_set(dev);
7d497f8a 846 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
a6c2ba28 847
195a4ef6
MCC
848 mutex_unlock(&dev->lock);
849 return 0;
850}
9e31ced8 851
195a4ef6
MCC
852static const char *iname[] = {
853 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
854 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
855 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
856 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
857 [EM28XX_VMUX_SVIDEO] = "S-Video",
858 [EM28XX_VMUX_TELEVISION] = "Television",
859 [EM28XX_VMUX_CABLE] = "Cable TV",
860 [EM28XX_VMUX_DVB] = "DVB",
861 [EM28XX_VMUX_DEBUG] = "for debug only",
862};
9e31ced8 863
195a4ef6
MCC
864static int vidioc_enum_input(struct file *file, void *priv,
865 struct v4l2_input *i)
866{
867 struct em28xx_fh *fh = priv;
868 struct em28xx *dev = fh->dev;
869 unsigned int n;
9e31ced8 870
195a4ef6
MCC
871 n = i->index;
872 if (n >= MAX_EM28XX_INPUT)
873 return -EINVAL;
874 if (0 == INPUT(n)->type)
875 return -EINVAL;
9e31ced8 876
195a4ef6
MCC
877 i->index = n;
878 i->type = V4L2_INPUT_TYPE_CAMERA;
a6c2ba28 879
195a4ef6 880 strcpy(i->name, iname[INPUT(n)->type]);
a6c2ba28 881
195a4ef6
MCC
882 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
883 (EM28XX_VMUX_CABLE == INPUT(n)->type))
884 i->type = V4L2_INPUT_TYPE_TUNER;
885
7d497f8a 886 i->std = dev->vdev->tvnorms;
195a4ef6
MCC
887
888 return 0;
a6c2ba28
AM
889}
890
195a4ef6 891static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
a6c2ba28 892{
195a4ef6
MCC
893 struct em28xx_fh *fh = priv;
894 struct em28xx *dev = fh->dev;
a6c2ba28 895
195a4ef6
MCC
896 *i = dev->ctl_input;
897
898 return 0;
899}
900
901static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
902{
903 struct em28xx_fh *fh = priv;
904 struct em28xx *dev = fh->dev;
905 int rc;
906
907 rc = check_dev(dev);
908 if (rc < 0)
909 return rc;
910
911 if (i >= MAX_EM28XX_INPUT)
912 return -EINVAL;
913 if (0 == INPUT(i)->type)
914 return -EINVAL;
a225452e 915
5a80415b 916 mutex_lock(&dev->lock);
a6c2ba28 917
195a4ef6
MCC
918 video_mux(dev, i);
919
920 mutex_unlock(&dev->lock);
921 return 0;
922}
923
924static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
925{
926 struct em28xx_fh *fh = priv;
927 struct em28xx *dev = fh->dev;
195a4ef6 928
35ae6f04
MCC
929 switch (a->index) {
930 case EM28XX_AMUX_VIDEO:
195a4ef6 931 strcpy(a->name, "Television");
35ae6f04
MCC
932 break;
933 case EM28XX_AMUX_LINE_IN:
195a4ef6 934 strcpy(a->name, "Line In");
35ae6f04
MCC
935 break;
936 case EM28XX_AMUX_VIDEO2:
937 strcpy(a->name, "Television alt");
938 break;
939 case EM28XX_AMUX_PHONE:
940 strcpy(a->name, "Phone");
941 break;
942 case EM28XX_AMUX_MIC:
943 strcpy(a->name, "Mic");
944 break;
945 case EM28XX_AMUX_CD:
946 strcpy(a->name, "CD");
947 break;
948 case EM28XX_AMUX_AUX:
949 strcpy(a->name, "Aux");
950 break;
951 case EM28XX_AMUX_PCM_OUT:
952 strcpy(a->name, "PCM");
953 break;
954 default:
955 return -EINVAL;
956 }
6ea54d93 957
35ae6f04 958 a->index = dev->ctl_ainput;
195a4ef6 959 a->capability = V4L2_AUDCAP_STEREO;
195a4ef6
MCC
960
961 return 0;
962}
963
964static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
965{
966 struct em28xx_fh *fh = priv;
967 struct em28xx *dev = fh->dev;
968
35ae6f04
MCC
969 dev->ctl_ainput = INPUT(a->index)->amux;
970 dev->ctl_aoutput = INPUT(a->index)->aout;
e879b8eb
MCC
971
972 if (!dev->ctl_aoutput)
973 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
195a4ef6
MCC
974 return 0;
975}
976
977static int vidioc_queryctrl(struct file *file, void *priv,
978 struct v4l2_queryctrl *qc)
979{
980 struct em28xx_fh *fh = priv;
981 struct em28xx *dev = fh->dev;
982 int id = qc->id;
983 int i;
984 int rc;
985
986 rc = check_dev(dev);
987 if (rc < 0)
988 return rc;
989
990 memset(qc, 0, sizeof(*qc));
991
992 qc->id = id;
993
505b6d0b 994 if (!dev->board.has_msp34xx) {
195a4ef6
MCC
995 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
996 if (qc->id && qc->id == em28xx_qctrl[i].id) {
997 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
998 return 0;
a6c2ba28
AM
999 }
1000 }
195a4ef6
MCC
1001 }
1002 mutex_lock(&dev->lock);
1003 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1004 mutex_unlock(&dev->lock);
a6c2ba28 1005
195a4ef6
MCC
1006 if (qc->type)
1007 return 0;
1008 else
1009 return -EINVAL;
1010}
a6c2ba28 1011
195a4ef6
MCC
1012static int vidioc_g_ctrl(struct file *file, void *priv,
1013 struct v4l2_control *ctrl)
1014{
1015 struct em28xx_fh *fh = priv;
1016 struct em28xx *dev = fh->dev;
1017 int rc;
a6c2ba28 1018
195a4ef6
MCC
1019 rc = check_dev(dev);
1020 if (rc < 0)
1021 return rc;
1022 mutex_lock(&dev->lock);
a6c2ba28 1023
505b6d0b 1024 if (!dev->board.has_msp34xx)
195a4ef6
MCC
1025 rc = em28xx_get_ctrl(dev, ctrl);
1026 else
1027 rc = -EINVAL;
1028
1029 if (rc == -EINVAL) {
1030 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1031 rc = 0;
1032 }
1033
1034 mutex_unlock(&dev->lock);
1035 return rc;
1036}
1037
1038static int vidioc_s_ctrl(struct file *file, void *priv,
1039 struct v4l2_control *ctrl)
1040{
1041 struct em28xx_fh *fh = priv;
1042 struct em28xx *dev = fh->dev;
1043 u8 i;
1044 int rc;
1045
1046 rc = check_dev(dev);
1047 if (rc < 0)
1048 return rc;
1049
1050 mutex_lock(&dev->lock);
1051
505b6d0b 1052 if (dev->board.has_msp34xx)
195a4ef6
MCC
1053 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1054 else {
1055 rc = 1;
1056 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1057 if (ctrl->id == em28xx_qctrl[i].id) {
1058 if (ctrl->value < em28xx_qctrl[i].minimum ||
1059 ctrl->value > em28xx_qctrl[i].maximum) {
1060 rc = -ERANGE;
1061 break;
1062 }
1063
1064 rc = em28xx_set_ctrl(dev, ctrl);
1065 break;
1066 }
a6c2ba28
AM
1067 }
1068 }
1069
195a4ef6
MCC
1070 /* Control not found - try to send it to the attached devices */
1071 if (rc == 1) {
1072 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1073 rc = 0;
1074 }
1075
5a80415b 1076 mutex_unlock(&dev->lock);
195a4ef6 1077 return rc;
a6c2ba28
AM
1078}
1079
195a4ef6
MCC
1080static int vidioc_g_tuner(struct file *file, void *priv,
1081 struct v4l2_tuner *t)
a6c2ba28 1082{
195a4ef6
MCC
1083 struct em28xx_fh *fh = priv;
1084 struct em28xx *dev = fh->dev;
1085 int rc;
1086
1087 rc = check_dev(dev);
1088 if (rc < 0)
1089 return rc;
1090
1091 if (0 != t->index)
1092 return -EINVAL;
1093
1094 strcpy(t->name, "Tuner");
1095
1096 mutex_lock(&dev->lock);
1097
1098 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1099
1100 mutex_unlock(&dev->lock);
1101 return 0;
a6c2ba28
AM
1102}
1103
195a4ef6
MCC
1104static int vidioc_s_tuner(struct file *file, void *priv,
1105 struct v4l2_tuner *t)
a6c2ba28 1106{
195a4ef6
MCC
1107 struct em28xx_fh *fh = priv;
1108 struct em28xx *dev = fh->dev;
1109 int rc;
63337dd3 1110
195a4ef6
MCC
1111 rc = check_dev(dev);
1112 if (rc < 0)
1113 return rc;
1114
1115 if (0 != t->index)
1116 return -EINVAL;
1117
1118 mutex_lock(&dev->lock);
1119
1120 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1121
1122 mutex_unlock(&dev->lock);
1123 return 0;
a6c2ba28
AM
1124}
1125
195a4ef6
MCC
1126static int vidioc_g_frequency(struct file *file, void *priv,
1127 struct v4l2_frequency *f)
1128{
1129 struct em28xx_fh *fh = priv;
1130 struct em28xx *dev = fh->dev;
a6c2ba28 1131
0be43754 1132 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
195a4ef6
MCC
1133 f->frequency = dev->ctl_freq;
1134
1135 return 0;
1136}
1137
1138static int vidioc_s_frequency(struct file *file, void *priv,
1139 struct v4l2_frequency *f)
a6c2ba28 1140{
195a4ef6
MCC
1141 struct em28xx_fh *fh = priv;
1142 struct em28xx *dev = fh->dev;
1143 int rc;
9c75541f 1144
195a4ef6
MCC
1145 rc = check_dev(dev);
1146 if (rc < 0)
1147 return rc;
1148
1149 if (0 != f->tuner)
1150 return -EINVAL;
1151
0be43754
MCC
1152 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1153 return -EINVAL;
1154 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
195a4ef6 1155 return -EINVAL;
a225452e
MCC
1156
1157 mutex_lock(&dev->lock);
a3a048ce 1158
195a4ef6
MCC
1159 dev->ctl_freq = f->frequency;
1160 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
a6c2ba28 1161
195a4ef6
MCC
1162 mutex_unlock(&dev->lock);
1163 return 0;
1164}
a6c2ba28 1165
1e7ad56f
MCC
1166#ifdef CONFIG_VIDEO_ADV_DEBUG
1167static int em28xx_reg_len(int reg)
1168{
1169 switch (reg) {
41facaa4
MCC
1170 case EM28XX_R40_AC97LSB:
1171 case EM28XX_R30_HSCALELOW:
1172 case EM28XX_R32_VSCALELOW:
1e7ad56f
MCC
1173 return 2;
1174 default:
1175 return 1;
1176 }
1177}
1178
1179static int vidioc_g_register(struct file *file, void *priv,
1180 struct v4l2_register *reg)
1181{
1182 struct em28xx_fh *fh = priv;
1183 struct em28xx *dev = fh->dev;
1184 int ret;
1185
1186 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1187 return -EINVAL;
1188
1189 if (em28xx_reg_len(reg->reg) == 1) {
1190 ret = em28xx_read_reg(dev, reg->reg);
1191 if (ret < 0)
1192 return ret;
1193
1194 reg->val = ret;
1195 } else {
a954b668 1196 __le64 val = 0;
1e7ad56f
MCC
1197 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1198 reg->reg, (char *)&val, 2);
1199 if (ret < 0)
1200 return ret;
1201
a954b668 1202 reg->val = le64_to_cpu(val);
1e7ad56f
MCC
1203 }
1204
1205 return 0;
1206}
1207
1208static int vidioc_s_register(struct file *file, void *priv,
1209 struct v4l2_register *reg)
1210{
1211 struct em28xx_fh *fh = priv;
1212 struct em28xx *dev = fh->dev;
a954b668 1213 __le64 buf;
1e7ad56f 1214
a954b668 1215 buf = cpu_to_le64(reg->val);
1e7ad56f
MCC
1216
1217 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1218 em28xx_reg_len(reg->reg));
1219}
1220#endif
1221
1222
195a4ef6
MCC
1223static int vidioc_cropcap(struct file *file, void *priv,
1224 struct v4l2_cropcap *cc)
1225{
1226 struct em28xx_fh *fh = priv;
1227 struct em28xx *dev = fh->dev;
1228
1229 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
a6c2ba28 1230 return -EINVAL;
a6c2ba28 1231
195a4ef6
MCC
1232 cc->bounds.left = 0;
1233 cc->bounds.top = 0;
1234 cc->bounds.width = dev->width;
1235 cc->bounds.height = dev->height;
1236 cc->defrect = cc->bounds;
1237 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1238 cc->pixelaspect.denominator = 59;
1239
1240 return 0;
1241}
1242
1243static int vidioc_streamon(struct file *file, void *priv,
1244 enum v4l2_buf_type type)
1245{
1246 struct em28xx_fh *fh = priv;
1247 struct em28xx *dev = fh->dev;
1248 int rc;
1249
1250 rc = check_dev(dev);
1251 if (rc < 0)
1252 return rc;
1253
195a4ef6 1254
29b59417
MCC
1255 mutex_lock(&dev->lock);
1256 rc = res_get(fh);
1257 mutex_unlock(&dev->lock);
1258
1259 if (unlikely(rc < 0))
1260 return rc;
a6c2ba28 1261
ad0ebb96 1262 return (videobuf_streamon(&fh->vb_vidq));
195a4ef6
MCC
1263}
1264
1265static int vidioc_streamoff(struct file *file, void *priv,
1266 enum v4l2_buf_type type)
1267{
1268 struct em28xx_fh *fh = priv;
1269 struct em28xx *dev = fh->dev;
1270 int rc;
1271
1272 rc = check_dev(dev);
1273 if (rc < 0)
1274 return rc;
1275
ad0ebb96
MCC
1276 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1277 return -EINVAL;
1278 if (type != fh->type)
195a4ef6
MCC
1279 return -EINVAL;
1280
ad0ebb96 1281 videobuf_streamoff(&fh->vb_vidq);
7831364f 1282 mutex_lock(&dev->lock);
ad0ebb96 1283 res_free(fh);
7831364f 1284 mutex_unlock(&dev->lock);
a6c2ba28 1285
a6c2ba28
AM
1286 return 0;
1287}
1288
195a4ef6
MCC
1289static int vidioc_querycap(struct file *file, void *priv,
1290 struct v4l2_capability *cap)
a6c2ba28 1291{
195a4ef6
MCC
1292 struct em28xx_fh *fh = priv;
1293 struct em28xx *dev = fh->dev;
1294
1295 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1296 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
af128a10 1297 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
195a4ef6
MCC
1298
1299 cap->version = EM28XX_VERSION_CODE;
1300
1301 cap->capabilities =
1302 V4L2_CAP_SLICED_VBI_CAPTURE |
1303 V4L2_CAP_VIDEO_CAPTURE |
1304 V4L2_CAP_AUDIO |
1305 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1306
ed086314 1307 if (dev->tuner_type != TUNER_ABSENT)
195a4ef6
MCC
1308 cap->capabilities |= V4L2_CAP_TUNER;
1309
1310 return 0;
c0477ad9
MCC
1311}
1312
78b526a4 1313static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
195a4ef6 1314 struct v4l2_fmtdesc *fmtd)
a6c2ba28 1315{
195a4ef6 1316 if (fmtd->index != 0)
c0477ad9 1317 return -EINVAL;
195a4ef6
MCC
1318
1319 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1320 strcpy(fmtd->description, "Packed YUY2");
1321 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1322 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1323
1324 return 0;
c0477ad9
MCC
1325}
1326
195a4ef6 1327/* Sliced VBI ioctls */
78b526a4 1328static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6 1329 struct v4l2_format *f)
a6c2ba28 1330{
195a4ef6
MCC
1331 struct em28xx_fh *fh = priv;
1332 struct em28xx *dev = fh->dev;
1333 int rc;
a6c2ba28 1334
195a4ef6
MCC
1335 rc = check_dev(dev);
1336 if (rc < 0)
1337 return rc;
a6c2ba28 1338
195a4ef6
MCC
1339 mutex_lock(&dev->lock);
1340
1341 f->fmt.sliced.service_set = 0;
1342
1343 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1344
1345 if (f->fmt.sliced.service_set == 0)
1346 rc = -EINVAL;
1347
1348 mutex_unlock(&dev->lock);
1349 return rc;
1350}
1351
78b526a4 1352static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
195a4ef6
MCC
1353 struct v4l2_format *f)
1354{
1355 struct em28xx_fh *fh = priv;
1356 struct em28xx *dev = fh->dev;
1357 int rc;
1358
1359 rc = check_dev(dev);
1360 if (rc < 0)
1361 return rc;
1362
1363 mutex_lock(&dev->lock);
1364 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1365 mutex_unlock(&dev->lock);
1366
1367 if (f->fmt.sliced.service_set == 0)
1368 return -EINVAL;
a6c2ba28
AM
1369
1370 return 0;
1371}
1372
195a4ef6
MCC
1373
1374static int vidioc_reqbufs(struct file *file, void *priv,
1375 struct v4l2_requestbuffers *rb)
a6c2ba28 1376{
195a4ef6
MCC
1377 struct em28xx_fh *fh = priv;
1378 struct em28xx *dev = fh->dev;
195a4ef6 1379 int rc;
a6c2ba28 1380
195a4ef6
MCC
1381 rc = check_dev(dev);
1382 if (rc < 0)
1383 return rc;
a6c2ba28 1384
ad0ebb96 1385 return (videobuf_reqbufs(&fh->vb_vidq, rb));
a6c2ba28
AM
1386}
1387
195a4ef6
MCC
1388static int vidioc_querybuf(struct file *file, void *priv,
1389 struct v4l2_buffer *b)
e5589bef 1390{
195a4ef6
MCC
1391 struct em28xx_fh *fh = priv;
1392 struct em28xx *dev = fh->dev;
1393 int rc;
e5589bef 1394
195a4ef6
MCC
1395 rc = check_dev(dev);
1396 if (rc < 0)
1397 return rc;
2d50f847 1398
ad0ebb96 1399 return (videobuf_querybuf(&fh->vb_vidq, b));
195a4ef6
MCC
1400}
1401
1402static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1403{
1404 struct em28xx_fh *fh = priv;
1405 struct em28xx *dev = fh->dev;
195a4ef6
MCC
1406 int rc;
1407
1408 rc = check_dev(dev);
1409 if (rc < 0)
1410 return rc;
1411
ad0ebb96 1412 return (videobuf_qbuf(&fh->vb_vidq, b));
e5589bef
MCC
1413}
1414
195a4ef6 1415static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2d50f847 1416{
195a4ef6
MCC
1417 struct em28xx_fh *fh = priv;
1418 struct em28xx *dev = fh->dev;
1419 int rc;
195a4ef6
MCC
1420
1421 rc = check_dev(dev);
1422 if (rc < 0)
1423 return rc;
2d50f847 1424
ad0ebb96
MCC
1425 return (videobuf_dqbuf(&fh->vb_vidq, b,
1426 file->f_flags & O_NONBLOCK));
1427}
2d50f847 1428
ad0ebb96 1429#ifdef CONFIG_VIDEO_V4L1_COMPAT
f245e549 1430static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
ad0ebb96 1431{
f245e549 1432 struct em28xx_fh *fh = priv;
2d50f847 1433
f245e549 1434 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
195a4ef6 1435}
ad0ebb96
MCC
1436#endif
1437
2d50f847 1438
0be43754
MCC
1439/* ----------------------------------------------------------- */
1440/* RADIO ESPECIFIC IOCTLS */
1441/* ----------------------------------------------------------- */
1442
1443static int radio_querycap(struct file *file, void *priv,
1444 struct v4l2_capability *cap)
1445{
1446 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1447
1448 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1449 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
af128a10 1450 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
0be43754
MCC
1451
1452 cap->version = EM28XX_VERSION_CODE;
1453 cap->capabilities = V4L2_CAP_TUNER;
1454 return 0;
1455}
1456
1457static int radio_g_tuner(struct file *file, void *priv,
1458 struct v4l2_tuner *t)
1459{
1460 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1461
1462 if (unlikely(t->index > 0))
1463 return -EINVAL;
1464
1465 strcpy(t->name, "Radio");
1466 t->type = V4L2_TUNER_RADIO;
1467
1468 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1469 return 0;
1470}
1471
1472static int radio_enum_input(struct file *file, void *priv,
1473 struct v4l2_input *i)
1474{
1475 if (i->index != 0)
1476 return -EINVAL;
1477 strcpy(i->name, "Radio");
1478 i->type = V4L2_INPUT_TYPE_TUNER;
1479
1480 return 0;
1481}
1482
1483static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1484{
1485 if (unlikely(a->index))
1486 return -EINVAL;
1487
1488 strcpy(a->name, "Radio");
1489 return 0;
1490}
1491
1492static int radio_s_tuner(struct file *file, void *priv,
1493 struct v4l2_tuner *t)
1494{
1495 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1496
1497 if (0 != t->index)
1498 return -EINVAL;
1499
1500 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1501
1502 return 0;
1503}
1504
1505static int radio_s_audio(struct file *file, void *fh,
1506 struct v4l2_audio *a)
1507{
1508 return 0;
1509}
1510
1511static int radio_s_input(struct file *file, void *fh, unsigned int i)
1512{
1513 return 0;
1514}
1515
1516static int radio_queryctrl(struct file *file, void *priv,
1517 struct v4l2_queryctrl *qc)
1518{
1519 int i;
1520
1521 if (qc->id < V4L2_CID_BASE ||
1522 qc->id >= V4L2_CID_LASTP1)
1523 return -EINVAL;
1524
1525 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1526 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1527 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1528 return 0;
1529 }
1530 }
1531
1532 return -EINVAL;
1533}
1534
195a4ef6
MCC
1535/*
1536 * em28xx_v4l2_open()
1537 * inits the device and starts isoc transfer
1538 */
1539static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1540{
1541 int minor = iminor(inode);
0be43754 1542 int errCode = 0, radio = 0;
6ea54d93 1543 struct em28xx *h, *dev = NULL;
195a4ef6 1544 struct em28xx_fh *fh;
d7aa8020 1545 enum v4l2_buf_type fh_type = 0;
2d50f847 1546
818a557e 1547 mutex_lock(&em28xx_devlist_mutex);
195a4ef6
MCC
1548 list_for_each_entry(h, &em28xx_devlist, devlist) {
1549 if (h->vdev->minor == minor) {
1550 dev = h;
d7aa8020 1551 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
195a4ef6
MCC
1552 }
1553 if (h->vbi_dev->minor == minor) {
1554 dev = h;
d7aa8020 1555 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
195a4ef6 1556 }
0be43754
MCC
1557 if (h->radio_dev &&
1558 h->radio_dev->minor == minor) {
1559 radio = 1;
1560 dev = h;
1561 }
2d50f847 1562 }
818a557e
MCC
1563 mutex_unlock(&em28xx_devlist_mutex);
1564 if (NULL == dev)
195a4ef6 1565 return -ENODEV;
818a557e
MCC
1566
1567 mutex_lock(&dev->lock);
2d50f847 1568
195a4ef6 1569 em28xx_videodbg("open minor=%d type=%s users=%d\n",
d7aa8020 1570 minor, v4l2_type_names[fh_type], dev->users);
2d50f847 1571
9aeb4b05 1572
c67ec53f 1573 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
195a4ef6
MCC
1574 if (!fh) {
1575 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
818a557e 1576 mutex_unlock(&dev->lock);
195a4ef6
MCC
1577 return -ENOMEM;
1578 }
195a4ef6 1579 fh->dev = dev;
0be43754 1580 fh->radio = radio;
d7aa8020 1581 fh->type = fh_type;
195a4ef6 1582 filp->private_data = fh;
9aeb4b05 1583
d7aa8020 1584 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
195a4ef6
MCC
1585 dev->width = norm_maxw(dev);
1586 dev->height = norm_maxh(dev);
195a4ef6
MCC
1587 dev->hscale = 0;
1588 dev->vscale = 0;
2d50f847 1589
c67ec53f 1590 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
3687e1e6 1591 em28xx_set_alternate(dev);
195a4ef6 1592 em28xx_resolution_set(dev);
2d50f847 1593
c67ec53f
MCC
1594 /* Needed, since GPIO might have disabled power of
1595 some i2c device
1596 */
1597 em28xx_config_i2c(dev);
1598
2d50f847 1599 }
0be43754
MCC
1600 if (fh->radio) {
1601 em28xx_videodbg("video_open: setting radio device\n");
1602 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1603 }
2d50f847 1604
195a4ef6 1605 dev->users++;
2d50f847 1606
ad0ebb96
MCC
1607 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1608 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1609 sizeof(struct em28xx_buffer), fh);
1610
195a4ef6 1611 mutex_unlock(&dev->lock);
c67ec53f 1612
195a4ef6 1613 return errCode;
2d50f847 1614}
e5589bef 1615
a6c2ba28 1616/*
195a4ef6
MCC
1617 * em28xx_realease_resources()
1618 * unregisters the v4l2,i2c and usb devices
1619 * called when the device gets disconected or at module unload
1620*/
1621static void em28xx_release_resources(struct em28xx *dev)
a6c2ba28 1622{
a6c2ba28 1623
195a4ef6 1624 /*FIXME: I2C IR should be disconnected */
a6c2ba28 1625
195a4ef6 1626 list_del(&dev->devlist);
a9fc52bc
DH
1627 if (dev->sbutton_input_dev)
1628 em28xx_deregister_snapshot_button(dev);
a924a499
MCC
1629
1630 if (dev->ir)
1631 em28xx_ir_fini(dev);
1632
0be43754
MCC
1633 if (dev->radio_dev) {
1634 if (-1 != dev->radio_dev->minor)
1635 video_unregister_device(dev->radio_dev);
1636 else
1637 video_device_release(dev->radio_dev);
1638 dev->radio_dev = NULL;
1639 }
1640 if (dev->vbi_dev) {
49240444
DH
1641 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1642 dev->vbi_dev->num);
0be43754
MCC
1643 if (-1 != dev->vbi_dev->minor)
1644 video_unregister_device(dev->vbi_dev);
1645 else
1646 video_device_release(dev->vbi_dev);
1647 dev->vbi_dev = NULL;
1648 }
1649 if (dev->vdev) {
49240444
DH
1650 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1651 dev->vdev->num);
0be43754
MCC
1652 if (-1 != dev->vdev->minor)
1653 video_unregister_device(dev->vdev);
1654 else
1655 video_device_release(dev->vdev);
1656 dev->vdev = NULL;
1657 }
195a4ef6
MCC
1658 em28xx_i2c_unregister(dev);
1659 usb_put_dev(dev->udev);
a6c2ba28 1660
195a4ef6 1661 /* Mark device as unused */
6ea54d93 1662 em28xx_devused &= ~(1<<dev->devno);
195a4ef6 1663}
a6c2ba28 1664
195a4ef6
MCC
1665/*
1666 * em28xx_v4l2_close()
6ea54d93
DSL
1667 * stops streaming and deallocates all resources allocated by the v4l2
1668 * calls and ioctls
195a4ef6
MCC
1669 */
1670static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1671{
1672 struct em28xx_fh *fh = filp->private_data;
1673 struct em28xx *dev = fh->dev;
1674 int errCode;
a6c2ba28 1675
195a4ef6 1676 em28xx_videodbg("users=%d\n", dev->users);
a6c2ba28 1677
a6c2ba28 1678
7831364f 1679 mutex_lock(&dev->lock);
195a4ef6
MCC
1680 if (res_check(fh))
1681 res_free(fh);
a6c2ba28 1682
195a4ef6 1683 if (dev->users == 1) {
ad0ebb96
MCC
1684 videobuf_stop(&fh->vb_vidq);
1685 videobuf_mmap_free(&fh->vb_vidq);
a6c2ba28 1686
195a4ef6
MCC
1687 /* the device is already disconnect,
1688 free the remaining resources */
1689 if (dev->state & DEV_DISCONNECTED) {
1690 em28xx_release_resources(dev);
1691 mutex_unlock(&dev->lock);
1692 kfree(dev);
1693 return 0;
1694 }
a6c2ba28 1695
eb6c9634
MCC
1696 /* Save some power by putting tuner to sleep */
1697 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1698
d7aa8020
AT
1699 /* do this before setting alternate! */
1700 em28xx_uninit_isoc(dev);
2fe3e2ee 1701 em28xx_set_mode(dev, EM28XX_SUSPEND);
d7aa8020 1702
195a4ef6
MCC
1703 /* set alternate 0 */
1704 dev->alt = 0;
1705 em28xx_videodbg("setting alternate 0\n");
1706 errCode = usb_set_interface(dev->udev, 0, 0);
1707 if (errCode < 0) {
1708 em28xx_errdev("cannot change alternate number to "
1709 "0 (error=%i)\n", errCode);
1710 }
9aeb4b05 1711 }
195a4ef6
MCC
1712 kfree(fh);
1713 dev->users--;
1714 wake_up_interruptible_nr(&dev->open, 1);
1715 mutex_unlock(&dev->lock);
1716 return 0;
1717}
a6c2ba28 1718
195a4ef6
MCC
1719/*
1720 * em28xx_v4l2_read()
1721 * will allocate buffers when called for the first time
1722 */
1723static ssize_t
6ea54d93 1724em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
f245e549 1725 loff_t *pos)
195a4ef6 1726{
195a4ef6
MCC
1727 struct em28xx_fh *fh = filp->private_data;
1728 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
1729 int rc;
1730
1731 rc = check_dev(dev);
1732 if (rc < 0)
1733 return rc;
a6c2ba28 1734
195a4ef6
MCC
1735 /* FIXME: read() is not prepared to allow changing the video
1736 resolution while streaming. Seems a bug at em28xx_set_fmt
1737 */
a6c2ba28 1738
ad0ebb96 1739 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
29b59417
MCC
1740 mutex_lock(&dev->lock);
1741 rc = res_get(fh);
1742 mutex_unlock(&dev->lock);
1743
1744 if (unlikely(rc < 0))
1745 return rc;
195a4ef6 1746
ad0ebb96
MCC
1747 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1748 filp->f_flags & O_NONBLOCK);
195a4ef6 1749 }
ad0ebb96 1750 return 0;
a6c2ba28
AM
1751}
1752
1753/*
195a4ef6
MCC
1754 * em28xx_v4l2_poll()
1755 * will allocate buffers when called for the first time
a6c2ba28 1756 */
195a4ef6 1757static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
a6c2ba28 1758{
a3a048ce 1759 struct em28xx_fh *fh = filp->private_data;
195a4ef6 1760 struct em28xx *dev = fh->dev;
ad0ebb96
MCC
1761 int rc;
1762
1763 rc = check_dev(dev);
1764 if (rc < 0)
1765 return rc;
a6c2ba28 1766
29b59417
MCC
1767 mutex_lock(&dev->lock);
1768 rc = res_get(fh);
1769 mutex_unlock(&dev->lock);
1770
1771 if (unlikely(rc < 0))
195a4ef6 1772 return POLLERR;
a6c2ba28 1773
ad0ebb96
MCC
1774 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1775 return POLLERR;
a6c2ba28 1776
ad0ebb96 1777 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
195a4ef6 1778}
a6c2ba28 1779
195a4ef6
MCC
1780/*
1781 * em28xx_v4l2_mmap()
1782 */
1783static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1784{
1785 struct em28xx_fh *fh = filp->private_data;
1786 struct em28xx *dev = fh->dev;
ad0ebb96 1787 int rc;
a6c2ba28 1788
ad0ebb96
MCC
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))
1798 return rc;
1799
ad0ebb96 1800 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
a6c2ba28 1801
ad0ebb96
MCC
1802 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1803 (unsigned long)vma->vm_start,
1804 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1805 rc);
a6c2ba28 1806
ad0ebb96 1807 return rc;
a6c2ba28
AM
1808}
1809
fa027c2a 1810static const struct file_operations em28xx_v4l_fops = {
195a4ef6
MCC
1811 .owner = THIS_MODULE,
1812 .open = em28xx_v4l2_open,
1813 .release = em28xx_v4l2_close,
1814 .read = em28xx_v4l2_read,
1815 .poll = em28xx_v4l2_poll,
1816 .mmap = em28xx_v4l2_mmap,
1817 .ioctl = video_ioctl2,
1818 .llseek = no_llseek,
1819 .compat_ioctl = v4l_compat_ioctl32,
1820};
17cbe2e5 1821
a399810c 1822static const struct v4l2_ioctl_ops video_ioctl_ops = {
195a4ef6 1823 .vidioc_querycap = vidioc_querycap,
78b526a4
HV
1824 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1825 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1826 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1827 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
195a4ef6
MCC
1828 .vidioc_g_audio = vidioc_g_audio,
1829 .vidioc_s_audio = vidioc_s_audio,
1830 .vidioc_cropcap = vidioc_cropcap,
1831
78b526a4
HV
1832 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1833 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1834 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
195a4ef6
MCC
1835
1836 .vidioc_reqbufs = vidioc_reqbufs,
1837 .vidioc_querybuf = vidioc_querybuf,
1838 .vidioc_qbuf = vidioc_qbuf,
1839 .vidioc_dqbuf = vidioc_dqbuf,
1840 .vidioc_s_std = vidioc_s_std,
1841 .vidioc_enum_input = vidioc_enum_input,
1842 .vidioc_g_input = vidioc_g_input,
1843 .vidioc_s_input = vidioc_s_input,
1844 .vidioc_queryctrl = vidioc_queryctrl,
1845 .vidioc_g_ctrl = vidioc_g_ctrl,
1846 .vidioc_s_ctrl = vidioc_s_ctrl,
1847 .vidioc_streamon = vidioc_streamon,
1848 .vidioc_streamoff = vidioc_streamoff,
1849 .vidioc_g_tuner = vidioc_g_tuner,
1850 .vidioc_s_tuner = vidioc_s_tuner,
1851 .vidioc_g_frequency = vidioc_g_frequency,
1852 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
1853#ifdef CONFIG_VIDEO_ADV_DEBUG
1854 .vidioc_g_register = vidioc_g_register,
1855 .vidioc_s_register = vidioc_s_register,
1856#endif
ad0ebb96
MCC
1857#ifdef CONFIG_VIDEO_V4L1_COMPAT
1858 .vidiocgmbuf = vidiocgmbuf,
1859#endif
a399810c
HV
1860};
1861
1862static const struct video_device em28xx_video_template = {
1863 .fops = &em28xx_v4l_fops,
1864 .release = video_device_release,
1865 .ioctl_ops = &video_ioctl_ops,
1866
1867 .minor = -1,
195a4ef6
MCC
1868
1869 .tvnorms = V4L2_STD_ALL,
7d497f8a 1870 .current_norm = V4L2_STD_PAL,
a6c2ba28
AM
1871};
1872
a399810c
HV
1873static const struct file_operations radio_fops = {
1874 .owner = THIS_MODULE,
1875 .open = em28xx_v4l2_open,
1876 .release = em28xx_v4l2_close,
1877 .ioctl = video_ioctl2,
1878 .compat_ioctl = v4l_compat_ioctl32,
1879 .llseek = no_llseek,
1880};
1881
1882static const struct v4l2_ioctl_ops radio_ioctl_ops = {
0be43754
MCC
1883 .vidioc_querycap = radio_querycap,
1884 .vidioc_g_tuner = radio_g_tuner,
1885 .vidioc_enum_input = radio_enum_input,
1886 .vidioc_g_audio = radio_g_audio,
1887 .vidioc_s_tuner = radio_s_tuner,
1888 .vidioc_s_audio = radio_s_audio,
1889 .vidioc_s_input = radio_s_input,
1890 .vidioc_queryctrl = radio_queryctrl,
1891 .vidioc_g_ctrl = vidioc_g_ctrl,
1892 .vidioc_s_ctrl = vidioc_s_ctrl,
1893 .vidioc_g_frequency = vidioc_g_frequency,
1894 .vidioc_s_frequency = vidioc_s_frequency,
1e7ad56f
MCC
1895#ifdef CONFIG_VIDEO_ADV_DEBUG
1896 .vidioc_g_register = vidioc_g_register,
1897 .vidioc_s_register = vidioc_s_register,
1898#endif
0be43754
MCC
1899};
1900
a399810c
HV
1901static struct video_device em28xx_radio_template = {
1902 .name = "em28xx-radio",
a399810c
HV
1903 .fops = &radio_fops,
1904 .ioctl_ops = &radio_ioctl_ops,
1905 .minor = -1,
1906};
1907
6ea54d93 1908/******************************** usb interface ******************************/
a6c2ba28 1909
6d79468d
MCC
1910
1911static LIST_HEAD(em28xx_extension_devlist);
1912static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1913
1914int em28xx_register_extension(struct em28xx_ops *ops)
1915{
0367ca1b 1916 struct em28xx *dev = NULL;
6d79468d 1917
818a557e 1918 mutex_lock(&em28xx_devlist_mutex);
6d79468d
MCC
1919 mutex_lock(&em28xx_extension_devlist_lock);
1920 list_add_tail(&ops->next, &em28xx_extension_devlist);
0367ca1b
DH
1921 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1922 if (dev)
1923 ops->init(dev);
1924 }
6d79468d
MCC
1925 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1926 mutex_unlock(&em28xx_extension_devlist_lock);
818a557e 1927 mutex_unlock(&em28xx_devlist_mutex);
6d79468d
MCC
1928 return 0;
1929}
1930EXPORT_SYMBOL(em28xx_register_extension);
1931
1932void em28xx_unregister_extension(struct em28xx_ops *ops)
1933{
0367ca1b 1934 struct em28xx *dev = NULL;
6d79468d 1935
818a557e 1936 mutex_lock(&em28xx_devlist_mutex);
0367ca1b
DH
1937 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1938 if (dev)
1939 ops->fini(dev);
1940 }
6d79468d
MCC
1941
1942 mutex_lock(&em28xx_extension_devlist_lock);
1943 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1944 list_del(&ops->next);
1945 mutex_unlock(&em28xx_extension_devlist_lock);
818a557e 1946 mutex_unlock(&em28xx_devlist_mutex);
6d79468d
MCC
1947}
1948EXPORT_SYMBOL(em28xx_unregister_extension);
1949
532fe652
AB
1950static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1951 const struct video_device *template,
532fe652 1952 const char *type_name)
0be43754
MCC
1953{
1954 struct video_device *vfd;
1955
1956 vfd = video_device_alloc();
1957 if (NULL == vfd)
1958 return NULL;
1959 *vfd = *template;
1960 vfd->minor = -1;
5e85e732 1961 vfd->parent = &dev->udev->dev;
0be43754 1962 vfd->release = video_device_release;
e9e6040d 1963 vfd->debug = video_debug;
0be43754
MCC
1964
1965 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1966 dev->name, type_name);
1967
1968 return vfd;
1969}
1970
818a557e
MCC
1971static int register_analog_devices(struct em28xx *dev)
1972{
1973 int ret;
1974
1975 /* allocate and fill video video_device struct */
1976 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1977 if (!dev->vdev) {
1978 em28xx_errdev("cannot allocate video_device.\n");
1979 return -ENODEV;
1980 }
1981
1982 /* register v4l2 video video_device */
1983 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1984 video_nr[dev->devno]);
1985 if (ret) {
1986 em28xx_errdev("unable to register video device (error=%i).\n",
1987 ret);
1988 return ret;
1989 }
1990
1991 /* Allocate and fill vbi video_device struct */
1992 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1993
1994 /* register v4l2 vbi video_device */
1995 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1996 vbi_nr[dev->devno]);
1997 if (ret < 0) {
1998 em28xx_errdev("unable to register vbi device\n");
1999 return ret;
2000 }
2001
2002 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2003 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2004 if (!dev->radio_dev) {
2005 em28xx_errdev("cannot allocate video_device.\n");
2006 return -ENODEV;
2007 }
2008 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2009 radio_nr[dev->devno]);
2010 if (ret < 0) {
2011 em28xx_errdev("can't register radio device\n");
2012 return ret;
2013 }
2014 em28xx_info("Registered radio device as /dev/radio%d\n",
2015 dev->radio_dev->num);
2016 }
2017
2018 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2019 dev->vdev->num, dev->vbi_dev->num);
2020
2021 return 0;
2022}
2023
2024
a6c2ba28 2025/*
3acf2809 2026 * em28xx_init_dev()
a6c2ba28
AM
2027 * allocates and inits the device structs, registers i2c bus and v4l device
2028 */
3acf2809 2029static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
03910cc3 2030 int minor)
a6c2ba28 2031{
6d79468d 2032 struct em28xx_ops *ops = NULL;
3acf2809 2033 struct em28xx *dev = *devhandle;
a6c2ba28 2034 int retval = -ENOMEM;
7d497f8a 2035 int errCode;
a6c2ba28 2036 unsigned int maxh, maxw;
a6c2ba28 2037
a6c2ba28 2038 dev->udev = udev;
3593cab5 2039 mutex_init(&dev->lock);
f2a2e491 2040 mutex_init(&dev->ctrl_urb_lock);
d7aa8020 2041 spin_lock_init(&dev->slock);
a6c2ba28 2042 init_waitqueue_head(&dev->open);
a3a048ce
MCC
2043 init_waitqueue_head(&dev->wait_frame);
2044 init_waitqueue_head(&dev->wait_stream);
a6c2ba28 2045
3acf2809
MCC
2046 dev->em28xx_write_regs = em28xx_write_regs;
2047 dev->em28xx_read_reg = em28xx_read_reg;
2048 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2049 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2050 dev->em28xx_read_reg_req = em28xx_read_reg_req;
505b6d0b 2051 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
a6c2ba28 2052
a94e95b4 2053 em28xx_pre_card_setup(dev);
03910cc3 2054
3acf2809 2055 errCode = em28xx_config(dev);
a6c2ba28 2056 if (errCode) {
3acf2809 2057 em28xx_errdev("error configuring device\n");
a6c2ba28
AM
2058 return -ENOMEM;
2059 }
2060
a6c2ba28 2061 /* register i2c bus */
f2a01a00
DSL
2062 errCode = em28xx_i2c_register(dev);
2063 if (errCode < 0) {
2064 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2065 __func__, errCode);
2066 return errCode;
2067 }
a6c2ba28
AM
2068
2069 /* Do board specific init and eeprom reading */
3acf2809 2070 em28xx_card_setup(dev);
a6c2ba28 2071
3abee53e 2072 /* Configure audio */
35643943 2073 errCode = em28xx_audio_setup(dev);
f2a01a00 2074 if (errCode < 0) {
35643943 2075 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
f2a01a00 2076 __func__, errCode);
f2a01a00 2077 }
3abee53e 2078
a6c2ba28 2079 /* configure the device */
3acf2809 2080 em28xx_config_i2c(dev);
a6c2ba28 2081
7d497f8a
MCC
2082 /* set default norm */
2083 dev->norm = em28xx_video_template.current_norm;
03910cc3
MCC
2084
2085 maxw = norm_maxw(dev);
2086 maxh = norm_maxh(dev);
2087
2088 /* set default image size */
2089 dev->width = maxw;
2090 dev->height = maxh;
2091 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
03910cc3
MCC
2092 dev->hscale = 0;
2093 dev->vscale = 0;
2094 dev->ctl_input = 2;
2095
3acf2809 2096 errCode = em28xx_config(dev);
f2a01a00
DSL
2097 if (errCode < 0) {
2098 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2099 __func__, errCode);
2100 return errCode;
2101 }
a6c2ba28 2102
ad0ebb96
MCC
2103 /* init video dma queues */
2104 INIT_LIST_HEAD(&dev->vidq.active);
2105 INIT_LIST_HEAD(&dev->vidq.queued);
2106
a6c2ba28 2107
505b6d0b 2108 if (dev->board.has_msp34xx) {
5a80415b 2109 /* Send a reset to other chips via gpio */
2a29a0d7 2110 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
f2a01a00
DSL
2111 if (errCode < 0) {
2112 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2113 __func__, errCode);
2114 return errCode;
2115 }
5a80415b 2116 msleep(3);
f2a01a00 2117
2a29a0d7 2118 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
f2a01a00
DSL
2119 if (errCode < 0) {
2120 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2121 __func__, errCode);
2122 return errCode;
2123 }
5a80415b 2124 msleep(3);
5a80415b 2125 }
195a4ef6 2126
5a80415b
SS
2127 video_mux(dev, 0);
2128
818a557e
MCC
2129 mutex_lock(&em28xx_devlist_mutex);
2130 list_add_tail(&dev->devlist, &em28xx_devlist);
2131 retval = register_analog_devices(dev);
2132 if (retval < 0) {
2133 em28xx_release_resources(dev);
2134 mutex_unlock(&em28xx_devlist_mutex);
2135 goto fail_reg_devices;
2136 }
a6c2ba28 2137
6d79468d
MCC
2138 mutex_lock(&em28xx_extension_devlist_lock);
2139 if (!list_empty(&em28xx_extension_devlist)) {
2140 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2141 if (ops->id)
2142 ops->init(dev);
2143 }
2144 }
2145 mutex_unlock(&em28xx_extension_devlist_lock);
818a557e 2146 mutex_unlock(&em28xx_devlist_mutex);
6d79468d 2147
eb6c9634
MCC
2148 /* Save some power by putting tuner to sleep */
2149 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2150
a6c2ba28 2151 return 0;
0be43754 2152
818a557e 2153fail_reg_devices:
0be43754 2154 mutex_unlock(&dev->lock);
0be43754 2155 return retval;
a6c2ba28
AM
2156}
2157
d7448a8d
MCC
2158#if defined(CONFIG_MODULES) && defined(MODULE)
2159static void request_module_async(struct work_struct *work)
2160{
2161 struct em28xx *dev = container_of(work,
2162 struct em28xx, request_module_wk);
2163
3f4dfe2a
MCC
2164 if (dev->has_audio_class)
2165 request_module("snd-usb-audio");
24a613e4 2166 else if (dev->has_alsa_audio)
d7448a8d 2167 request_module("em28xx-alsa");
3aefb79a 2168
505b6d0b 2169 if (dev->board.has_dvb)
3aefb79a 2170 request_module("em28xx-dvb");
d7448a8d
MCC
2171}
2172
2173static void request_modules(struct em28xx *dev)
2174{
2175 INIT_WORK(&dev->request_module_wk, request_module_async);
2176 schedule_work(&dev->request_module_wk);
2177}
2178#else
2179#define request_modules(dev)
2180#endif /* CONFIG_MODULES */
2181
a6c2ba28 2182/*
3acf2809 2183 * em28xx_usb_probe()
a6c2ba28
AM
2184 * checks for supported devices
2185 */
3acf2809 2186static int em28xx_usb_probe(struct usb_interface *interface,
a6c2ba28
AM
2187 const struct usb_device_id *id)
2188{
2189 const struct usb_endpoint_descriptor *endpoint;
2190 struct usb_device *udev;
9d4d9c05 2191 struct usb_interface *uif;
3acf2809 2192 struct em28xx *dev = NULL;
a6c2ba28 2193 int retval = -ENODEV;
95ea4705 2194 int i, nr, ifnum, isoc_pipe;
6a18eaf6 2195 char *speed;
8c239989 2196 char descr[255] = "";
a6c2ba28
AM
2197
2198 udev = usb_get_dev(interface_to_usbdev(interface));
d5e52653
MCC
2199 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2200
e5589bef 2201 /* Check to see next free device and mark as used */
6ea54d93
DSL
2202 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2203 em28xx_devused |= 1<<nr;
a6c2ba28 2204
596d92d5 2205 /* Don't register audio interfaces */
91cad0f2 2206 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
6a18eaf6
MCC
2207 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2208 "interface %i, class %i\n",
2209 le16_to_cpu(udev->descriptor.idVendor),
2210 le16_to_cpu(udev->descriptor.idProduct),
2211 ifnum,
2212 interface->altsetting[0].desc.bInterfaceClass);
e5589bef 2213
6ea54d93 2214 em28xx_devused &= ~(1<<nr);
596d92d5 2215 return -ENODEV;
91cad0f2
MCC
2216 }
2217
95ea4705 2218 endpoint = &interface->cur_altsetting->endpoint[0].desc;
d5e52653 2219
59c51591 2220 /* check if the device has the iso in endpoint at the correct place */
95ea4705
DH
2221 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2222 USB_ENDPOINT_XFER_ISOC &&
2223 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2224 {
2225 /* It's a newer em2874/em2875 device */
2226 isoc_pipe = 0;
2227 } else {
c9455fbb 2228 int check_interface = 1;
95ea4705
DH
2229 isoc_pipe = 1;
2230 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2231 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
c9455fbb
MCC
2232 USB_ENDPOINT_XFER_ISOC)
2233 check_interface = 0;
2234
2235 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2236 check_interface = 0;
2237
2238 if (!check_interface) {
2239 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2240 "interface %i, class %i found.\n",
6a18eaf6
MCC
2241 le16_to_cpu(udev->descriptor.idVendor),
2242 le16_to_cpu(udev->descriptor.idProduct),
c9455fbb
MCC
2243 ifnum,
2244 interface->altsetting[0].desc.bInterfaceClass);
2245
2246 em28xx_err(DRIVER_NAME " This is an anciliary "
2247 "interface not used by the driver\n");
2248
95ea4705
DH
2249 em28xx_devused &= ~(1<<nr);
2250 return -ENODEV;
2251 }
6a18eaf6 2252 }
c9455fbb 2253
6a18eaf6
MCC
2254 switch (udev->speed) {
2255 case USB_SPEED_LOW:
2256 speed = "1.5";
2257 break;
2258 case USB_SPEED_UNKNOWN:
2259 case USB_SPEED_FULL:
2260 speed = "12";
2261 break;
2262 case USB_SPEED_HIGH:
2263 speed = "480";
2264 break;
2265 default:
2266 speed = "unknown";
a6c2ba28
AM
2267 }
2268
8c239989
MCC
2269 if (udev->manufacturer)
2270 strlcpy(descr, udev->manufacturer, sizeof(descr));
2271
2272 if (udev->product) {
2273 if (*descr)
2274 strlcat(descr, " ", sizeof(descr));
2275 strlcat(descr, udev->product, sizeof(descr));
2276 }
2277 if (*descr)
2278 strlcat(descr, " ", sizeof(descr));
2279
2280 printk(DRIVER_NAME ": New device %s@ %s Mbps "
6a18eaf6 2281 "(%04x:%04x, interface %d, class %d)\n",
8c239989 2282 descr,
6a18eaf6
MCC
2283 speed,
2284 le16_to_cpu(udev->descriptor.idVendor),
2285 le16_to_cpu(udev->descriptor.idProduct),
2286 ifnum,
2287 interface->altsetting->desc.bInterfaceNumber);
c9455fbb 2288
19478843 2289 if (nr >= EM28XX_MAXBOARDS) {
6ea54d93
DSL
2290 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2291 EM28XX_MAXBOARDS);
2292 em28xx_devused &= ~(1<<nr);
596d92d5
MCC
2293 return -ENOMEM;
2294 }
2295
2296 /* allocate memory for our device state and initialize it */
7408187d 2297 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
596d92d5 2298 if (dev == NULL) {
3acf2809 2299 em28xx_err(DRIVER_NAME ": out of memory!\n");
6ea54d93 2300 em28xx_devused &= ~(1<<nr);
596d92d5
MCC
2301 return -ENOMEM;
2302 }
596d92d5 2303
e5589bef 2304 snprintf(dev->name, 29, "em28xx #%d", nr);
03910cc3
MCC
2305 dev->devno = nr;
2306 dev->model = id->driver_info;
3687e1e6 2307 dev->alt = -1;
e5589bef 2308
d7448a8d
MCC
2309 /* Checks if audio is provided by some interface */
2310 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2311 uif = udev->config->interface[i];
2312 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2313 dev->has_audio_class = 1;
2314 break;
2315 }
2316 }
2317
9d4d9c05
MCC
2318 /* compute alternate max packet sizes */
2319 uif = udev->actconfig->interface[0];
2320
6ea54d93 2321 dev->num_alt = uif->num_altsetting;
1bee0184 2322 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
6ea54d93
DSL
2323/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2324 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2325
9d4d9c05 2326 if (dev->alt_max_pkt_size == NULL) {
e5589bef 2327 em28xx_errdev("out of memory!\n");
6ea54d93 2328 em28xx_devused &= ~(1<<nr);
1207cf84 2329 kfree(dev);
9d4d9c05
MCC
2330 return -ENOMEM;
2331 }
2332
2333 for (i = 0; i < dev->num_alt ; i++) {
95ea4705 2334 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
9d4d9c05
MCC
2335 wMaxPacketSize);
2336 dev->alt_max_pkt_size[i] =
2337 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1bee0184 2338 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
6ea54d93 2339 dev->alt_max_pkt_size[i]);
9d4d9c05
MCC
2340 }
2341
6ea54d93 2342 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
03910cc3 2343 dev->model = card[nr];
596d92d5 2344
a6c2ba28 2345 /* allocate device struct */
03910cc3 2346 retval = em28xx_init_dev(&dev, udev, nr);
625ff167
MCC
2347 if (retval) {
2348 em28xx_devused &= ~(1<<dev->devno);
2349 kfree(dev);
2350
a6c2ba28 2351 return retval;
625ff167 2352 }
a6c2ba28 2353
a6c2ba28
AM
2354 /* save our data pointer in this interface device */
2355 usb_set_intfdata(interface, dev);
d7448a8d
MCC
2356
2357 request_modules(dev);
2358
a6c2ba28
AM
2359 return 0;
2360}
2361
2362/*
3acf2809 2363 * em28xx_usb_disconnect()
a6c2ba28
AM
2364 * called when the device gets diconencted
2365 * video device will be unregistered on v4l2_close in case it is still open
2366 */
3acf2809 2367static void em28xx_usb_disconnect(struct usb_interface *interface)
a6c2ba28 2368{
5a80415b 2369 struct em28xx *dev;
6d79468d 2370 struct em28xx_ops *ops = NULL;
5a80415b
SS
2371
2372 dev = usb_get_intfdata(interface);
a6c2ba28
AM
2373 usb_set_intfdata(interface, NULL);
2374
2375 if (!dev)
2376 return;
2377
5a80415b 2378 em28xx_info("disconnecting %s\n", dev->vdev->name);
a6c2ba28 2379
6ea54d93
DSL
2380 /* wait until all current v4l2 io is finished then deallocate
2381 resources */
3593cab5 2382 mutex_lock(&dev->lock);
a6c2ba28 2383
a6c2ba28
AM
2384 wake_up_interruptible_all(&dev->open);
2385
2386 if (dev->users) {
3acf2809 2387 em28xx_warn
a6c2ba28 2388 ("device /dev/video%d is open! Deregistration and memory "
e5589bef 2389 "deallocation are deferred on close.\n",
dd89601d 2390 dev->vdev->num);
e5589bef 2391
a6c2ba28 2392 dev->state |= DEV_MISCONFIGURED;
3acf2809 2393 em28xx_uninit_isoc(dev);
a6c2ba28
AM
2394 dev->state |= DEV_DISCONNECTED;
2395 wake_up_interruptible(&dev->wait_frame);
2396 wake_up_interruptible(&dev->wait_stream);
2397 } else {
2398 dev->state |= DEV_DISCONNECTED;
3acf2809 2399 em28xx_release_resources(dev);
a6c2ba28 2400 }
3593cab5 2401 mutex_unlock(&dev->lock);
a6c2ba28 2402
6d79468d
MCC
2403 mutex_lock(&em28xx_extension_devlist_lock);
2404 if (!list_empty(&em28xx_extension_devlist)) {
2405 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2406 ops->fini(dev);
2407 }
2408 }
2409 mutex_unlock(&em28xx_extension_devlist_lock);
2410
9d4d9c05
MCC
2411 if (!dev->users) {
2412 kfree(dev->alt_max_pkt_size);
a6c2ba28 2413 kfree(dev);
9d4d9c05 2414 }
a6c2ba28
AM
2415}
2416
3acf2809 2417static struct usb_driver em28xx_usb_driver = {
3acf2809
MCC
2418 .name = "em28xx",
2419 .probe = em28xx_usb_probe,
2420 .disconnect = em28xx_usb_disconnect,
2421 .id_table = em28xx_id_table,
a6c2ba28
AM
2422};
2423
3acf2809 2424static int __init em28xx_module_init(void)
a6c2ba28
AM
2425{
2426 int result;
2427
2428 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
3acf2809
MCC
2429 (EM28XX_VERSION_CODE >> 16) & 0xff,
2430 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
a6c2ba28
AM
2431#ifdef SNAPSHOT
2432 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2433 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2434#endif
2435
2436 /* register this driver with the USB subsystem */
3acf2809 2437 result = usb_register(&em28xx_usb_driver);
a6c2ba28 2438 if (result)
3acf2809 2439 em28xx_err(DRIVER_NAME
a6c2ba28
AM
2440 " usb_register failed. Error number %d.\n", result);
2441
2442 return result;
2443}
2444
3acf2809 2445static void __exit em28xx_module_exit(void)
a6c2ba28
AM
2446{
2447 /* deregister this driver with the USB subsystem */
3acf2809 2448 usb_deregister(&em28xx_usb_driver);
a6c2ba28
AM
2449}
2450
3acf2809
MCC
2451module_init(em28xx_module_init);
2452module_exit(em28xx_module_exit);