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