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