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