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