blob: 10928dccaecc37c39f940fd71fb6cee6425aef26 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -02009 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080012 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020031#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080034#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080037
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080038#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020039#include <media/v4l2-common.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030040#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030041#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080043#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030045 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_NAME "em28xx"
49#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030050#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080052#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080053 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030055 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030057static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030058module_param(isoc_debug, int, 0644);
59MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060
61#define em28xx_isocdbg(fmt, arg...) do {\
62 if (isoc_debug) \
63 printk(KERN_INFO "%s %s :"fmt, \
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030064 dev->name, __func__ , ##arg); } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030065
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030066/* Limits minimum and default number of buffers */
67#define EM28XX_MIN_BUF 4
68#define EM28XX_DEF_BUF 8
69
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080070MODULE_AUTHOR(DRIVER_AUTHOR);
71MODULE_DESCRIPTION(DRIVER_DESC);
72MODULE_LICENSE("GPL");
73
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080074static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080075
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080076static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030078static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080081module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020082module_param_array(video_nr, int, NULL, 0444);
83module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084module_param_array(radio_nr, int, NULL, 0444);
85MODULE_PARM_DESC(card, "card type");
86MODULE_PARM_DESC(video_nr, "video device numbers");
87MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080089
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030090static unsigned int video_debug;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080091module_param(video_debug,int,0644);
92MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
93
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020094/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
95static unsigned long em28xx_devused;
96
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080097/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020098/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080099static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800100 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200101 .id = V4L2_CID_AUDIO_VOLUME,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Volume",
104 .minimum = 0x0,
105 .maximum = 0x1f,
106 .step = 0x1,
107 .default_value = 0x1f,
108 .flags = 0,
109 },{
110 .id = V4L2_CID_AUDIO_MUTE,
111 .type = V4L2_CTRL_TYPE_BOOLEAN,
112 .name = "Mute",
113 .minimum = 0,
114 .maximum = 1,
115 .step = 1,
116 .default_value = 1,
117 .flags = 0,
118 }
119};
120
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800121static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800122
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300123/* ------------------------------------------------------------------
124 DMA and thread functions
125 ------------------------------------------------------------------*/
126
127/*
128 * Announces that a buffer were filled and request the next
129 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300130static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300131 struct em28xx_dmaqueue *dma_q,
132 struct em28xx_buffer *buf)
133{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300134 /* Advice that buffer was filled */
135 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
136 buf->vb.state = VIDEOBUF_DONE;
137 buf->vb.field_count++;
138 do_gettimeofday(&buf->vb.ts);
139
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300140 dev->isoc_ctl.buf = NULL;
141
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300142 list_del(&buf->vb.queue);
143 wake_up(&buf->vb.done);
144}
145
146/*
147 * Identify the buffer header type and properly handles
148 */
149static void em28xx_copy_video(struct em28xx *dev,
150 struct em28xx_dmaqueue *dma_q,
151 struct em28xx_buffer *buf,
152 unsigned char *p,
153 unsigned char *outp, unsigned long len)
154{
155 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300156 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300157
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158 if (dev->frame_size != buf->vb.size) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 em28xx_errdev("size %i and buf.length %lu are different!\n",
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300160 dev->frame_size, buf->vb.size);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161 return;
162 }
163
164 if (dma_q->pos + len > buf->vb.size)
165 len = buf->vb.size - dma_q->pos;
166
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300167 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168 em28xx_isocdbg("frame is not complete\n");
169 len += 4;
170 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300171 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300172
173 startread = p;
174 remain = len;
175
176 /* Interlaces frame */
177 if (buf->top_field)
178 fieldstart = outp;
179 else
180 fieldstart = outp + dev->bytesperline;
181
182 linesdone = dma_q->pos / dev->bytesperline;
183 currlinedone = dma_q->pos % dev->bytesperline;
184 offset = linesdone * dev->bytesperline * 2 + currlinedone;
185 startwrite = fieldstart + offset;
186 lencopy = dev->bytesperline - currlinedone;
187 lencopy = lencopy > remain ? remain : lencopy;
188
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300189 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300190 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300191 ((char *)startwrite + lencopy) -
192 ((char *)outp + buf->vb.size));
193 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300194 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300195 if (lencopy <= 0)
196 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300197 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300198
199 remain -= lencopy;
200
201 while (remain > 0) {
202 startwrite += lencopy + dev->bytesperline;
203 startread += lencopy;
204 if (dev->bytesperline > remain)
205 lencopy = remain;
206 else
207 lencopy = dev->bytesperline;
208
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300209 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300210 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300211 ((char *)startwrite + lencopy) -
212 ((char *)outp + buf->vb.size));
213 lencopy = remain = (char *)outp + buf->vb.size -
214 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300216 if (lencopy <= 0)
217 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300218
219 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300220
221 remain -= lencopy;
222 }
223
224 dma_q->pos += len;
225}
226
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300227static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300228 int packet, int status)
229{
230 char *errmsg = "Unknown";
231
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300232 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300233 case -ENOENT:
234 errmsg = "unlinked synchronuously";
235 break;
236 case -ECONNRESET:
237 errmsg = "unlinked asynchronuously";
238 break;
239 case -ENOSR:
240 errmsg = "Buffer error (overrun)";
241 break;
242 case -EPIPE:
243 errmsg = "Stalled (device not responding)";
244 break;
245 case -EOVERFLOW:
246 errmsg = "Babble (bad cable?)";
247 break;
248 case -EPROTO:
249 errmsg = "Bit-stuff error (bad cable?)";
250 break;
251 case -EILSEQ:
252 errmsg = "CRC/Timeout (could be anything)";
253 break;
254 case -ETIME:
255 errmsg = "Device does not respond";
256 break;
257 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300258 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300259 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
260 } else {
261 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
262 packet, status, errmsg);
263 }
264}
265
266/*
267 * video-buf generic routine to get the next available buffer
268 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300269static inline int get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300270 struct em28xx_buffer **buf)
271{
272 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300273 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300274
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300275 if (list_empty(&dma_q->active)) {
276 em28xx_isocdbg("No active queue to serve\n");
277 dev->isoc_ctl.buf = NULL;
278 return 0;
279 }
280
281 /* Check if the last buffer were fully filled */
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300282 *buf = dev->isoc_ctl.buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300283
284 /* Nobody is waiting on this buffer - discards */
285 if (*buf && !waitqueue_active(&(*buf)->vb.done)) {
286 dev->isoc_ctl.buf = NULL;
287 *buf = NULL;
288 }
289
290 /* Returns the last buffer, to be filled with remaining data */
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300291 if (*buf)
292 return 1;
293
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300294 /* Get the next buffer */
295 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
296
297 /* Nobody is waiting on the next buffer. returns */
298 if (!*buf || !waitqueue_active(&(*buf)->vb.done)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299 em28xx_isocdbg("No active queue to serve\n");
300 return 0;
301 }
302
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300303 /* Cleans up buffer - Usefull for testing for frame/URB loss */
304 outp = videobuf_to_vmalloc(&(*buf)->vb);
305 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300306
307 dev->isoc_ctl.buf = *buf;
308
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300309 return 1;
310}
311
312/*
313 * Controls the isoc copy of each urb packet
314 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300315static inline int em28xx_isoc_copy(struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300316{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300317 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300318 struct em28xx_dmaqueue *dma_q = urb->context;
319 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300320 unsigned char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300321 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300322 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323
324 if (!dev)
325 return 0;
326
327 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
328 return 0;
329
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300330 if (urb->status < 0) {
331 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300332 if (urb->status == -ENOENT)
333 return 0;
334 }
335
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300336 rc = get_next_buf(dma_q, &buf);
337 if (rc <= 0)
338 return rc;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300339
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300340 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300341
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300342 for (i = 0; i < urb->number_of_packets; i++) {
343 int status = urb->iso_frame_desc[i].status;
344
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300345 if (status < 0) {
346 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300347 if (urb->iso_frame_desc[i].status != -EPROTO)
348 continue;
349 }
350
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300351 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300352
353 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300354 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300355 continue;
356 }
357 if (urb->iso_frame_desc[i].actual_length >
358 dev->max_pkt_size) {
359 em28xx_isocdbg("packet bigger than packet size");
360 continue;
361 }
362
363 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364
365 /* FIXME: incomplete buffer checks where removed to make
366 logic simpler. Impacts of those changes should be evaluated
367 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300368 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300369 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
370 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300371
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300372 if (p[2] & 1)
373 buf->top_field = 0;
374 else {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300375 if (buf->receiving) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300376 buffer_filled(dev, dma_q, buf);
377 rc = get_next_buf(dma_q, &buf);
378 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300379 return rc;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300380 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300381 }
382
383 buf->top_field = 1;
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300384 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300385 buf->receiving = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300386 dma_q->pos = 0;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300387 } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300388 em28xx_isocdbg("VBI HEADER!!!\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389 }
390
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300391 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300392
393 /* FIXME: Should add vbi copy */
394 }
395 return rc;
396}
397
398/* ------------------------------------------------------------------
399 URB control
400 ------------------------------------------------------------------*/
401
402/*
403 * IRQ callback, called by URB callback
404 */
405static void em28xx_irq_callback(struct urb *urb)
406{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300407 struct em28xx_dmaqueue *dma_q = urb->context;
408 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300409 int rc, i;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300410
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300411 /* Copy data from URB */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300412 spin_lock(&dev->slock);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300413 rc = em28xx_isoc_copy(urb);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300414 spin_unlock(&dev->slock);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300415
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300416 /* Reset urb buffers */
417 for (i = 0; i < urb->number_of_packets; i++) {
418 urb->iso_frame_desc[i].status = 0;
419 urb->iso_frame_desc[i].actual_length = 0;
420 }
421 urb->status = 0;
422
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300423 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
424 if (urb->status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300425 em28xx_err("urb resubmit failed (error=%i)\n",
426 urb->status);
427 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300428}
429
430/*
431 * Stop and Deallocate URBs
432 */
433static void em28xx_uninit_isoc(struct em28xx *dev)
434{
435 struct urb *urb;
436 int i;
437
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300438 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
439
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300440 dev->isoc_ctl.nfields = -1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300442 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443 if (urb) {
444 usb_kill_urb(urb);
445 usb_unlink_urb(urb);
446 if (dev->isoc_ctl.transfer_buffer[i]) {
447 usb_buffer_free(dev->udev,
448 urb->transfer_buffer_length,
449 dev->isoc_ctl.transfer_buffer[i],
450 urb->transfer_dma);
451 }
452 usb_free_urb(urb);
453 dev->isoc_ctl.urb[i] = NULL;
454 }
455 dev->isoc_ctl.transfer_buffer[i] = NULL;
456 }
457
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300458 kfree(dev->isoc_ctl.urb);
459 kfree(dev->isoc_ctl.transfer_buffer);
460 dev->isoc_ctl.urb = NULL;
461 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300462
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300463 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300464
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300465 em28xx_capture_start(dev, 0);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466}
467
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300468/*
469 * Allocate URBs and start IRQ
470 */
471static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
472 int num_bufs)
473{
474 struct em28xx_dmaqueue *dma_q = &dev->vidq;
475 int i;
476 int sb_size, pipe;
477 struct urb *urb;
478 int j, k;
479
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300480 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
481
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300482 /* De-allocates all pending stuff */
483 em28xx_uninit_isoc(dev);
484
485 dev->isoc_ctl.num_bufs = num_bufs;
486
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300487 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300488 if (!dev->isoc_ctl.urb) {
489 em28xx_errdev("cannot alloc memory for usb buffers\n");
490 return -ENOMEM;
491 }
492
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300493 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 GFP_KERNEL);
495 if (!dev->isoc_ctl.urb) {
496 em28xx_errdev("cannot allocate memory for usbtransfer\n");
497 kfree(dev->isoc_ctl.urb);
498 return -ENOMEM;
499 }
500
501 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300502 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503
504 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
505
506 /* allocate urbs and transfer buffers */
507 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
508 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
509 if (!urb) {
510 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
511 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300512 return -ENOMEM;
513 }
514 dev->isoc_ctl.urb[i] = urb;
515
516 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
517 sb_size, GFP_KERNEL, &urb->transfer_dma);
518 if (!dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300519 em28xx_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300520 " buffer %i%s\n",
521 sb_size, i,
522 in_interrupt()?" while in int":"");
523 em28xx_uninit_isoc(dev);
524 return -ENOMEM;
525 }
526 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
527
528 /* FIXME: this is a hack - should be
529 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
530 should also be using 'desc.bInterval'
531 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300532 pipe = usb_rcvisocpipe(dev->udev, 0x82);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300533 usb_fill_int_urb(urb, dev->udev, pipe,
534 dev->isoc_ctl.transfer_buffer[i], sb_size,
535 em28xx_irq_callback, dma_q, 1);
536
537 urb->number_of_packets = max_packets;
538 urb->transfer_flags = URB_ISO_ASAP;
539
540 k = 0;
541 for (j = 0; j < max_packets; j++) {
542 urb->iso_frame_desc[j].offset = k;
543 urb->iso_frame_desc[j].length =
544 dev->isoc_ctl.max_pkt_size;
545 k += dev->isoc_ctl.max_pkt_size;
546 }
547 }
548
549 return 0;
550}
551
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300552static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300553{
554 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300555 int i, rc = 0;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300556
557 em28xx_videodbg("Called em28xx_start_thread\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300558
559 init_waitqueue_head(&dma_q->wq);
560
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300561 em28xx_capture_start(dev, 1);
562
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300563 /* submit urbs and enables IRQ */
564 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
565 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
566 if (rc) {
567 em28xx_err("submit of urb %i failed (error=%i)\n", i,
568 rc);
569 em28xx_uninit_isoc(dev);
570 return rc;
571 }
572 }
573
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300574 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300575 return rc;
576
577 return 0;
578}
579
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300580/* ------------------------------------------------------------------
581 Videobuf operations
582 ------------------------------------------------------------------*/
583
584static int
585buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
586{
587 struct em28xx_fh *fh = vq->priv_data;
588
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300589 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300590 if (0 == *count)
591 *count = EM28XX_DEF_BUF;
592
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300593 if (*count < EM28XX_MIN_BUF)
594 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300595
596 return 0;
597}
598
599static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
600{
601 if (in_interrupt())
602 BUG();
603
604 videobuf_waiton(&buf->vb, 0, 0);
605 videobuf_vmalloc_free(&buf->vb);
606 buf->vb.state = VIDEOBUF_NEEDS_INIT;
607}
608
609static int
610buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
611 enum v4l2_field field)
612{
613 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300614 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300615 struct em28xx *dev = fh->dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300616 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300617 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300618
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300619 /* BUG_ON(NULL == fh->fmt); */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300620
621 /* FIXME: It assumes depth = 16 */
622 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300623 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300624
625 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
626 return -EINVAL;
627
Brandon Philips05612972008-04-13 14:57:01 -0300628 buf->fmt = fh->fmt;
629 buf->vb.width = dev->width;
630 buf->vb.height = dev->height;
631 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300632
633 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300634 rc = videobuf_iolock(vq, &buf->vb, NULL);
635 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300636 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300637 }
638
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300639 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300640 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300641
642 if (urb_init) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300643 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
644 EM28XX_NUM_BUFS);
645 if (rc < 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300646 goto fail;
647
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300648 rc = em28xx_start_thread(vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300649 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300650 goto fail;
651 }
652
653 buf->vb.state = VIDEOBUF_PREPARED;
654 return 0;
655
656fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300657 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300658 return rc;
659}
660
661static void
662buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
663{
664 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
665 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300666 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300667 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300668
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300669 buf->vb.state = VIDEOBUF_QUEUED;
670 list_add_tail(&buf->vb.queue, &vidq->active);
671
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300672}
673
674static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
675{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300676 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300677 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300678 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300679
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300680 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300681
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300682 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300683}
684
685static struct videobuf_queue_ops em28xx_video_qops = {
686 .buf_setup = buffer_setup,
687 .buf_prepare = buffer_prepare,
688 .buf_queue = buffer_queue,
689 .buf_release = buffer_release,
690};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800691
692/********************* v4l2 interface ******************************************/
693
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800694/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800695 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800696 * inits registers with sane defaults
697 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800698static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800699{
700
701 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300702 if (!dev->is_em2800)
703 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800704
705 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200706
Markus Rechberger9475fb12006-02-27 00:07:34 -0300707/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
708/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200709 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
710
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800711 dev->mute = 1; /* maybe not the right place... */
712 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300713
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800714 em28xx_outfmt_set_yuv422(dev);
715 em28xx_colorlevels_set_default(dev);
716 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800717
718 return 0;
719}
720
721/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800722 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800723 * configure i2c attached devices
724 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800725static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800726{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300727 struct v4l2_routing route;
728
729 route.input = INPUT(dev->ctl_input)->vmux;
730 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100731 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300732 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300733 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800734}
735
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800736static void video_mux(struct em28xx *dev, int index)
737{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300738 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800739
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300740 route.input = INPUT(index)->vmux;
741 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800742 dev->ctl_input = index;
743 dev->ctl_ainput = INPUT(index)->amux;
744
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300745 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800746
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800747 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200748 if (dev->i2s_speed)
749 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300750 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300751 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300752 /* Note: this is msp3400 specific */
753 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800754 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300755
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300756 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800757}
758
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300759/* Usage lock check functions */
760static int res_get(struct em28xx_fh *fh)
761{
762 struct em28xx *dev = fh->dev;
763 int rc = 0;
764
765 /* This instance already has stream_on */
766 if (fh->stream_on)
767 return rc;
768
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300769 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300770 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300771
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300772 mutex_lock(&dev->lock);
773 dev->stream_on = 1;
774 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300775 mutex_unlock(&dev->lock);
776 return rc;
777}
778
779static int res_check(struct em28xx_fh *fh)
780{
781 return (fh->stream_on);
782}
783
784static void res_free(struct em28xx_fh *fh)
785{
786 struct em28xx *dev = fh->dev;
787
788 mutex_lock(&dev->lock);
789 fh->stream_on = 0;
790 dev->stream_on = 0;
791 mutex_unlock(&dev->lock);
792}
793
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800794/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300795 * em28xx_get_ctrl()
796 * return the current saturation, brightness or contrast, mute state
797 */
798static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
799{
800 switch (ctrl->id) {
801 case V4L2_CID_AUDIO_MUTE:
802 ctrl->value = dev->mute;
803 return 0;
804 case V4L2_CID_AUDIO_VOLUME:
805 ctrl->value = dev->volume;
806 return 0;
807 default:
808 return -EINVAL;
809 }
810}
811
812/*
813 * em28xx_set_ctrl()
814 * mute or set new saturation, brightness or contrast
815 */
816static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
817{
818 switch (ctrl->id) {
819 case V4L2_CID_AUDIO_MUTE:
820 if (ctrl->value != dev->mute) {
821 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822 return em28xx_audio_analog_set(dev);
823 }
824 return 0;
825 case V4L2_CID_AUDIO_VOLUME:
826 dev->volume = ctrl->value;
827 return em28xx_audio_analog_set(dev);
828 default:
829 return -EINVAL;
830 }
831}
832
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300833static int check_dev(struct em28xx *dev)
834{
835 if (dev->state & DEV_DISCONNECTED) {
836 em28xx_errdev("v4l2 ioctl: device not present\n");
837 return -ENODEV;
838 }
839
840 if (dev->state & DEV_MISCONFIGURED) {
841 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
842 "close and open it again\n");
843 return -EIO;
844 }
845 return 0;
846}
847
848static void get_scale(struct em28xx *dev,
849 unsigned int width, unsigned int height,
850 unsigned int *hscale, unsigned int *vscale)
851{
852 unsigned int maxw = norm_maxw(dev);
853 unsigned int maxh = norm_maxh(dev);
854
855 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
856 if (*hscale >= 0x4000)
857 *hscale = 0x3fff;
858
859 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
860 if (*vscale >= 0x4000)
861 *vscale = 0x3fff;
862}
863
864/* ------------------------------------------------------------------
865 IOCTL vidioc handling
866 ------------------------------------------------------------------*/
867
868static int vidioc_g_fmt_cap(struct file *file, void *priv,
869 struct v4l2_format *f)
870{
871 struct em28xx_fh *fh = priv;
872 struct em28xx *dev = fh->dev;
873
874 mutex_lock(&dev->lock);
875
876 f->fmt.pix.width = dev->width;
877 f->fmt.pix.height = dev->height;
878 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
879 f->fmt.pix.bytesperline = dev->bytesperline;
880 f->fmt.pix.sizeimage = dev->frame_size;
881 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
882
883 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
884 f->fmt.pix.field = dev->interlaced ?
885 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
886
887 mutex_unlock(&dev->lock);
888 return 0;
889}
890
891static int vidioc_try_fmt_cap(struct file *file, void *priv,
892 struct v4l2_format *f)
893{
894 struct em28xx_fh *fh = priv;
895 struct em28xx *dev = fh->dev;
896 int width = f->fmt.pix.width;
897 int height = f->fmt.pix.height;
898 unsigned int maxw = norm_maxw(dev);
899 unsigned int maxh = norm_maxh(dev);
900 unsigned int hscale, vscale;
901
902 /* width must even because of the YUYV format
903 height must be even because of interlacing */
904 height &= 0xfffe;
905 width &= 0xfffe;
906
907 if (height < 32)
908 height = 32;
909 if (height > maxh)
910 height = maxh;
911 if (width < 48)
912 width = 48;
913 if (width > maxw)
914 width = maxw;
915
916 mutex_lock(&dev->lock);
917
918 if (dev->is_em2800) {
919 /* the em2800 can only scale down to 50% */
920 if (height % (maxh / 2))
921 height = maxh;
922 if (width % (maxw / 2))
923 width = maxw;
924 /* according to empiatech support */
925 /* the MaxPacketSize is to small to support */
926 /* framesizes larger than 640x480 @ 30 fps */
927 /* or 640x576 @ 25 fps. As this would cut */
928 /* of a part of the image we prefer */
929 /* 360x576 or 360x480 for now */
930 if (width == maxw && height == maxh)
931 width /= 2;
932 }
933
934 get_scale(dev, width, height, &hscale, &vscale);
935
936 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
937 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
938
939 f->fmt.pix.width = width;
940 f->fmt.pix.height = height;
941 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
942 f->fmt.pix.bytesperline = width * 2;
943 f->fmt.pix.sizeimage = width * 2 * height;
944 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
945 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
946
947 mutex_unlock(&dev->lock);
948 return 0;
949}
950
951static int vidioc_s_fmt_cap(struct file *file, void *priv,
952 struct v4l2_format *f)
953{
954 struct em28xx_fh *fh = priv;
955 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300956 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300957
958 rc = check_dev(dev);
959 if (rc < 0)
960 return rc;
961
962 vidioc_try_fmt_cap(file, priv, f);
963
964 mutex_lock(&dev->lock);
965
Brandon Philips05612972008-04-13 14:57:01 -0300966 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
967 em28xx_errdev("%s queue busy\n", __func__);
968 rc = -EBUSY;
969 goto out;
970 }
971
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300972 if (dev->stream_on && !fh->stream_on) {
973 em28xx_errdev("%s device in use by another fh\n", __func__);
974 rc = -EBUSY;
975 goto out;
976 }
977
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300978 /* set new image size */
979 dev->width = f->fmt.pix.width;
980 dev->height = f->fmt.pix.height;
981 dev->frame_size = dev->width * dev->height * 2;
982 dev->field_size = dev->frame_size >> 1;
983 dev->bytesperline = dev->width * 2;
984 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
985
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300986 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300987 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300988
Brandon Philips05612972008-04-13 14:57:01 -0300989 rc = 0;
990
991out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300992 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300993 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300994}
995
996static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
997{
998 struct em28xx_fh *fh = priv;
999 struct em28xx *dev = fh->dev;
1000 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001001 int rc;
1002
1003 rc = check_dev(dev);
1004 if (rc < 0)
1005 return rc;
1006
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001007 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001008 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001009 mutex_unlock(&dev->lock);
1010
1011 /* Adjusts width/height, if needed */
1012 f.fmt.pix.width = dev->width;
1013 f.fmt.pix.height = dev->height;
1014 vidioc_try_fmt_cap(file, priv, &f);
1015
1016 mutex_lock(&dev->lock);
1017
1018 /* set new image size */
1019 dev->width = f.fmt.pix.width;
1020 dev->height = f.fmt.pix.height;
1021 dev->frame_size = dev->width * dev->height * 2;
1022 dev->field_size = dev->frame_size >> 1;
1023 dev->bytesperline = dev->width * 2;
1024 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1025
1026 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001027 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001028
1029 mutex_unlock(&dev->lock);
1030 return 0;
1031}
1032
1033static const char *iname[] = {
1034 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1035 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1036 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1037 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1038 [EM28XX_VMUX_SVIDEO] = "S-Video",
1039 [EM28XX_VMUX_TELEVISION] = "Television",
1040 [EM28XX_VMUX_CABLE] = "Cable TV",
1041 [EM28XX_VMUX_DVB] = "DVB",
1042 [EM28XX_VMUX_DEBUG] = "for debug only",
1043};
1044
1045static int vidioc_enum_input(struct file *file, void *priv,
1046 struct v4l2_input *i)
1047{
1048 struct em28xx_fh *fh = priv;
1049 struct em28xx *dev = fh->dev;
1050 unsigned int n;
1051
1052 n = i->index;
1053 if (n >= MAX_EM28XX_INPUT)
1054 return -EINVAL;
1055 if (0 == INPUT(n)->type)
1056 return -EINVAL;
1057
1058 i->index = n;
1059 i->type = V4L2_INPUT_TYPE_CAMERA;
1060
1061 strcpy(i->name, iname[INPUT(n)->type]);
1062
1063 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1064 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1065 i->type = V4L2_INPUT_TYPE_TUNER;
1066
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001067 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068
1069 return 0;
1070}
1071
1072static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1073{
1074 struct em28xx_fh *fh = priv;
1075 struct em28xx *dev = fh->dev;
1076
1077 *i = dev->ctl_input;
1078
1079 return 0;
1080}
1081
1082static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1083{
1084 struct em28xx_fh *fh = priv;
1085 struct em28xx *dev = fh->dev;
1086 int rc;
1087
1088 rc = check_dev(dev);
1089 if (rc < 0)
1090 return rc;
1091
1092 if (i >= MAX_EM28XX_INPUT)
1093 return -EINVAL;
1094 if (0 == INPUT(i)->type)
1095 return -EINVAL;
1096
1097 mutex_lock(&dev->lock);
1098
1099 video_mux(dev, i);
1100
1101 mutex_unlock(&dev->lock);
1102 return 0;
1103}
1104
1105static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1106{
1107 struct em28xx_fh *fh = priv;
1108 struct em28xx *dev = fh->dev;
1109 unsigned int index = a->index;
1110
1111 if (a->index > 1)
1112 return -EINVAL;
1113
1114 index = dev->ctl_ainput;
1115
1116 if (index == 0) {
1117 strcpy(a->name, "Television");
1118 } else {
1119 strcpy(a->name, "Line In");
1120 }
1121 a->capability = V4L2_AUDCAP_STEREO;
1122 a->index = index;
1123
1124 return 0;
1125}
1126
1127static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1128{
1129 struct em28xx_fh *fh = priv;
1130 struct em28xx *dev = fh->dev;
1131
1132 if (a->index != dev->ctl_ainput)
1133 return -EINVAL;
1134
1135 return 0;
1136}
1137
1138static int vidioc_queryctrl(struct file *file, void *priv,
1139 struct v4l2_queryctrl *qc)
1140{
1141 struct em28xx_fh *fh = priv;
1142 struct em28xx *dev = fh->dev;
1143 int id = qc->id;
1144 int i;
1145 int rc;
1146
1147 rc = check_dev(dev);
1148 if (rc < 0)
1149 return rc;
1150
1151 memset(qc, 0, sizeof(*qc));
1152
1153 qc->id = id;
1154
1155 if (!dev->has_msp34xx) {
1156 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1157 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1158 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1159 return 0;
1160 }
1161 }
1162 }
1163 mutex_lock(&dev->lock);
1164 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1165 mutex_unlock(&dev->lock);
1166
1167 if (qc->type)
1168 return 0;
1169 else
1170 return -EINVAL;
1171}
1172
1173static int vidioc_g_ctrl(struct file *file, void *priv,
1174 struct v4l2_control *ctrl)
1175{
1176 struct em28xx_fh *fh = priv;
1177 struct em28xx *dev = fh->dev;
1178 int rc;
1179
1180 rc = check_dev(dev);
1181 if (rc < 0)
1182 return rc;
1183 mutex_lock(&dev->lock);
1184
1185 if (!dev->has_msp34xx)
1186 rc = em28xx_get_ctrl(dev, ctrl);
1187 else
1188 rc = -EINVAL;
1189
1190 if (rc == -EINVAL) {
1191 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1192 rc = 0;
1193 }
1194
1195 mutex_unlock(&dev->lock);
1196 return rc;
1197}
1198
1199static int vidioc_s_ctrl(struct file *file, void *priv,
1200 struct v4l2_control *ctrl)
1201{
1202 struct em28xx_fh *fh = priv;
1203 struct em28xx *dev = fh->dev;
1204 u8 i;
1205 int rc;
1206
1207 rc = check_dev(dev);
1208 if (rc < 0)
1209 return rc;
1210
1211 mutex_lock(&dev->lock);
1212
1213 if (dev->has_msp34xx)
1214 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1215 else {
1216 rc = 1;
1217 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1218 if (ctrl->id == em28xx_qctrl[i].id) {
1219 if (ctrl->value < em28xx_qctrl[i].minimum ||
1220 ctrl->value > em28xx_qctrl[i].maximum) {
1221 rc = -ERANGE;
1222 break;
1223 }
1224
1225 rc = em28xx_set_ctrl(dev, ctrl);
1226 break;
1227 }
1228 }
1229 }
1230
1231 /* Control not found - try to send it to the attached devices */
1232 if (rc == 1) {
1233 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1234 rc = 0;
1235 }
1236
1237 mutex_unlock(&dev->lock);
1238 return rc;
1239}
1240
1241static int vidioc_g_tuner(struct file *file, void *priv,
1242 struct v4l2_tuner *t)
1243{
1244 struct em28xx_fh *fh = priv;
1245 struct em28xx *dev = fh->dev;
1246 int rc;
1247
1248 rc = check_dev(dev);
1249 if (rc < 0)
1250 return rc;
1251
1252 if (0 != t->index)
1253 return -EINVAL;
1254
1255 strcpy(t->name, "Tuner");
1256
1257 mutex_lock(&dev->lock);
1258
1259 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1260
1261 mutex_unlock(&dev->lock);
1262 return 0;
1263}
1264
1265static int vidioc_s_tuner(struct file *file, void *priv,
1266 struct v4l2_tuner *t)
1267{
1268 struct em28xx_fh *fh = priv;
1269 struct em28xx *dev = fh->dev;
1270 int rc;
1271
1272 rc = check_dev(dev);
1273 if (rc < 0)
1274 return rc;
1275
1276 if (0 != t->index)
1277 return -EINVAL;
1278
1279 mutex_lock(&dev->lock);
1280
1281 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1282
1283 mutex_unlock(&dev->lock);
1284 return 0;
1285}
1286
1287static int vidioc_g_frequency(struct file *file, void *priv,
1288 struct v4l2_frequency *f)
1289{
1290 struct em28xx_fh *fh = priv;
1291 struct em28xx *dev = fh->dev;
1292
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001293 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001294 f->frequency = dev->ctl_freq;
1295
1296 return 0;
1297}
1298
1299static int vidioc_s_frequency(struct file *file, void *priv,
1300 struct v4l2_frequency *f)
1301{
1302 struct em28xx_fh *fh = priv;
1303 struct em28xx *dev = fh->dev;
1304 int rc;
1305
1306 rc = check_dev(dev);
1307 if (rc < 0)
1308 return rc;
1309
1310 if (0 != f->tuner)
1311 return -EINVAL;
1312
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001313 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1314 return -EINVAL;
1315 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001316 return -EINVAL;
1317
1318 mutex_lock(&dev->lock);
1319
1320 dev->ctl_freq = f->frequency;
1321 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1322
1323 mutex_unlock(&dev->lock);
1324 return 0;
1325}
1326
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001327#ifdef CONFIG_VIDEO_ADV_DEBUG
1328static int em28xx_reg_len(int reg)
1329{
1330 switch (reg) {
1331 case AC97LSB_REG:
1332 case HSCALELOW_REG:
1333 case VSCALELOW_REG:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001334 return 2;
1335 default:
1336 return 1;
1337 }
1338}
1339
1340static int vidioc_g_register(struct file *file, void *priv,
1341 struct v4l2_register *reg)
1342{
1343 struct em28xx_fh *fh = priv;
1344 struct em28xx *dev = fh->dev;
1345 int ret;
1346
1347 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1348 return -EINVAL;
1349
1350 if (em28xx_reg_len(reg->reg) == 1) {
1351 ret = em28xx_read_reg(dev, reg->reg);
1352 if (ret < 0)
1353 return ret;
1354
1355 reg->val = ret;
1356 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001357 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001358 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1359 reg->reg, (char *)&val, 2);
1360 if (ret < 0)
1361 return ret;
1362
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001363 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001364 }
1365
1366 return 0;
1367}
1368
1369static int vidioc_s_register(struct file *file, void *priv,
1370 struct v4l2_register *reg)
1371{
1372 struct em28xx_fh *fh = priv;
1373 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001374 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001375
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001376 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001377
1378 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1379 em28xx_reg_len(reg->reg));
1380}
1381#endif
1382
1383
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001384static int vidioc_cropcap(struct file *file, void *priv,
1385 struct v4l2_cropcap *cc)
1386{
1387 struct em28xx_fh *fh = priv;
1388 struct em28xx *dev = fh->dev;
1389
1390 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1391 return -EINVAL;
1392
1393 cc->bounds.left = 0;
1394 cc->bounds.top = 0;
1395 cc->bounds.width = dev->width;
1396 cc->bounds.height = dev->height;
1397 cc->defrect = cc->bounds;
1398 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1399 cc->pixelaspect.denominator = 59;
1400
1401 return 0;
1402}
1403
1404static int vidioc_streamon(struct file *file, void *priv,
1405 enum v4l2_buf_type type)
1406{
1407 struct em28xx_fh *fh = priv;
1408 struct em28xx *dev = fh->dev;
1409 int rc;
1410
1411 rc = check_dev(dev);
1412 if (rc < 0)
1413 return rc;
1414
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001415
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001416 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001417 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001418
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001419 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001420}
1421
1422static int vidioc_streamoff(struct file *file, void *priv,
1423 enum v4l2_buf_type type)
1424{
1425 struct em28xx_fh *fh = priv;
1426 struct em28xx *dev = fh->dev;
1427 int rc;
1428
1429 rc = check_dev(dev);
1430 if (rc < 0)
1431 return rc;
1432
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001433 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1434 return -EINVAL;
1435 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001436 return -EINVAL;
1437
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001438 videobuf_streamoff(&fh->vb_vidq);
1439 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001440
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001441 return 0;
1442}
1443
1444static int vidioc_querycap(struct file *file, void *priv,
1445 struct v4l2_capability *cap)
1446{
1447 struct em28xx_fh *fh = priv;
1448 struct em28xx *dev = fh->dev;
1449
1450 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1451 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1452 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1453
1454 cap->version = EM28XX_VERSION_CODE;
1455
1456 cap->capabilities =
1457 V4L2_CAP_SLICED_VBI_CAPTURE |
1458 V4L2_CAP_VIDEO_CAPTURE |
1459 V4L2_CAP_AUDIO |
1460 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1461
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001462 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001463 cap->capabilities |= V4L2_CAP_TUNER;
1464
1465 return 0;
1466}
1467
1468static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1469 struct v4l2_fmtdesc *fmtd)
1470{
1471 if (fmtd->index != 0)
1472 return -EINVAL;
1473
1474 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1475 strcpy(fmtd->description, "Packed YUY2");
1476 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1477 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1478
1479 return 0;
1480}
1481
1482/* Sliced VBI ioctls */
1483static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1484 struct v4l2_format *f)
1485{
1486 struct em28xx_fh *fh = priv;
1487 struct em28xx *dev = fh->dev;
1488 int rc;
1489
1490 rc = check_dev(dev);
1491 if (rc < 0)
1492 return rc;
1493
1494 mutex_lock(&dev->lock);
1495
1496 f->fmt.sliced.service_set = 0;
1497
1498 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1499
1500 if (f->fmt.sliced.service_set == 0)
1501 rc = -EINVAL;
1502
1503 mutex_unlock(&dev->lock);
1504 return rc;
1505}
1506
1507static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1508 struct v4l2_format *f)
1509{
1510 struct em28xx_fh *fh = priv;
1511 struct em28xx *dev = fh->dev;
1512 int rc;
1513
1514 rc = check_dev(dev);
1515 if (rc < 0)
1516 return rc;
1517
1518 mutex_lock(&dev->lock);
1519 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1520 mutex_unlock(&dev->lock);
1521
1522 if (f->fmt.sliced.service_set == 0)
1523 return -EINVAL;
1524
1525 return 0;
1526}
1527
1528
1529static int vidioc_reqbufs(struct file *file, void *priv,
1530 struct v4l2_requestbuffers *rb)
1531{
1532 struct em28xx_fh *fh = priv;
1533 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001534 int rc;
1535
1536 rc = check_dev(dev);
1537 if (rc < 0)
1538 return rc;
1539
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001540 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001541}
1542
1543static int vidioc_querybuf(struct file *file, void *priv,
1544 struct v4l2_buffer *b)
1545{
1546 struct em28xx_fh *fh = priv;
1547 struct em28xx *dev = fh->dev;
1548 int rc;
1549
1550 rc = check_dev(dev);
1551 if (rc < 0)
1552 return rc;
1553
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001554 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555}
1556
1557static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1558{
1559 struct em28xx_fh *fh = priv;
1560 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001561 int rc;
1562
1563 rc = check_dev(dev);
1564 if (rc < 0)
1565 return rc;
1566
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001567 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001568}
1569
1570static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1571{
1572 struct em28xx_fh *fh = priv;
1573 struct em28xx *dev = fh->dev;
1574 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001575
1576 rc = check_dev(dev);
1577 if (rc < 0)
1578 return rc;
1579
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001580 return (videobuf_dqbuf(&fh->vb_vidq, b,
1581 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582}
1583
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001584#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001585static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001586{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001587 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001588
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001589 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001590}
1591#endif
1592
1593
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001594/* ----------------------------------------------------------- */
1595/* RADIO ESPECIFIC IOCTLS */
1596/* ----------------------------------------------------------- */
1597
1598static int radio_querycap(struct file *file, void *priv,
1599 struct v4l2_capability *cap)
1600{
1601 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1602
1603 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1604 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1605 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1606
1607 cap->version = EM28XX_VERSION_CODE;
1608 cap->capabilities = V4L2_CAP_TUNER;
1609 return 0;
1610}
1611
1612static int radio_g_tuner(struct file *file, void *priv,
1613 struct v4l2_tuner *t)
1614{
1615 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1616
1617 if (unlikely(t->index > 0))
1618 return -EINVAL;
1619
1620 strcpy(t->name, "Radio");
1621 t->type = V4L2_TUNER_RADIO;
1622
1623 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1624 return 0;
1625}
1626
1627static int radio_enum_input(struct file *file, void *priv,
1628 struct v4l2_input *i)
1629{
1630 if (i->index != 0)
1631 return -EINVAL;
1632 strcpy(i->name, "Radio");
1633 i->type = V4L2_INPUT_TYPE_TUNER;
1634
1635 return 0;
1636}
1637
1638static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1639{
1640 if (unlikely(a->index))
1641 return -EINVAL;
1642
1643 strcpy(a->name, "Radio");
1644 return 0;
1645}
1646
1647static int radio_s_tuner(struct file *file, void *priv,
1648 struct v4l2_tuner *t)
1649{
1650 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1651
1652 if (0 != t->index)
1653 return -EINVAL;
1654
1655 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1656
1657 return 0;
1658}
1659
1660static int radio_s_audio(struct file *file, void *fh,
1661 struct v4l2_audio *a)
1662{
1663 return 0;
1664}
1665
1666static int radio_s_input(struct file *file, void *fh, unsigned int i)
1667{
1668 return 0;
1669}
1670
1671static int radio_queryctrl(struct file *file, void *priv,
1672 struct v4l2_queryctrl *qc)
1673{
1674 int i;
1675
1676 if (qc->id < V4L2_CID_BASE ||
1677 qc->id >= V4L2_CID_LASTP1)
1678 return -EINVAL;
1679
1680 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1681 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1682 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1683 return 0;
1684 }
1685 }
1686
1687 return -EINVAL;
1688}
1689
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001690/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001691 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001692 * inits the device and starts isoc transfer
1693 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001694static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001695{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001696 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001697 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001698 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001699 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001700 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001701
Trent Piephoa991f442007-10-10 05:37:43 -03001702 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001703 if (h->vdev->minor == minor) {
1704 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001705 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001706 }
1707 if (h->vbi_dev->minor == minor) {
1708 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001709 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001710 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001711 if (h->radio_dev &&
1712 h->radio_dev->minor == minor) {
1713 radio = 1;
1714 dev = h;
1715 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001716 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001717 if (NULL == dev)
1718 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001719
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001720 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001721 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001723 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001724
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001725 if (!fh) {
1726 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1727 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001729 mutex_lock(&dev->lock);
1730 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001731 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001732 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001733 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001734
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001735 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001736 dev->width = norm_maxw(dev);
1737 dev->height = norm_maxh(dev);
1738 dev->frame_size = dev->width * dev->height * 2;
1739 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1740 dev->bytesperline = dev->width * 2;
1741 dev->hscale = 0;
1742 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001743
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001744 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001745 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001746
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001747 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001748 if (fh->radio) {
1749 em28xx_videodbg("video_open: setting radio device\n");
1750 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1751 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001752
1753 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001754
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001755 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1756 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1757 sizeof(struct em28xx_buffer), fh);
1758
Ingo Molnar3593cab2006-02-07 06:49:14 -02001759 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760 return errCode;
1761}
1762
1763/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001764 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001765 * unregisters the v4l2,i2c and usb devices
1766 * called when the device gets disconected or at module unload
1767*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001768static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001769{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001770
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001771 /*FIXME: I2C IR should be disconnected */
1772
1773 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1774 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1775 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001776 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001777 if (dev->radio_dev) {
1778 if (-1 != dev->radio_dev->minor)
1779 video_unregister_device(dev->radio_dev);
1780 else
1781 video_device_release(dev->radio_dev);
1782 dev->radio_dev = NULL;
1783 }
1784 if (dev->vbi_dev) {
1785 if (-1 != dev->vbi_dev->minor)
1786 video_unregister_device(dev->vbi_dev);
1787 else
1788 video_device_release(dev->vbi_dev);
1789 dev->vbi_dev = NULL;
1790 }
1791 if (dev->vdev) {
1792 if (-1 != dev->vdev->minor)
1793 video_unregister_device(dev->vdev);
1794 else
1795 video_device_release(dev->vdev);
1796 dev->vdev = NULL;
1797 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001798 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001799 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001800
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001801 /* Mark device as unused */
1802 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001803}
1804
1805/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001806 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001807 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1808 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001809static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001811 struct em28xx_fh *fh = filp->private_data;
1812 struct em28xx *dev = fh->dev;
1813 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001814
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001815 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001816
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001817
1818 if (res_check(fh))
1819 res_free(fh);
1820
Ingo Molnar3593cab2006-02-07 06:49:14 -02001821 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001823 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001824 videobuf_stop(&fh->vb_vidq);
1825 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001826
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001827 /* the device is already disconnect,
1828 free the remaining resources */
1829 if (dev->state & DEV_DISCONNECTED) {
1830 em28xx_release_resources(dev);
1831 mutex_unlock(&dev->lock);
1832 kfree(dev);
1833 return 0;
1834 }
1835
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001836 /* do this before setting alternate! */
1837 em28xx_uninit_isoc(dev);
1838
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001839 /* set alternate 0 */
1840 dev->alt = 0;
1841 em28xx_videodbg("setting alternate 0\n");
1842 errCode = usb_set_interface(dev->udev, 0, 0);
1843 if (errCode < 0) {
1844 em28xx_errdev("cannot change alternate number to "
1845 "0 (error=%i)\n", errCode);
1846 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001848 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001849 dev->users--;
1850 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001851 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001852 return 0;
1853}
1854
1855/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001856 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001857 * will allocate buffers when called for the first time
1858 */
1859static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001860em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001861 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001862{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001863 struct em28xx_fh *fh = filp->private_data;
1864 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001865 int rc;
1866
1867 rc = check_dev(dev);
1868 if (rc < 0)
1869 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001870
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001871 /* FIXME: read() is not prepared to allow changing the video
1872 resolution while streaming. Seems a bug at em28xx_set_fmt
1873 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001874
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001875 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1876 if (unlikely(res_get(fh)))
1877 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001878
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001879 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1880 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001881 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001882 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001883}
1884
1885/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001886 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001887 * will allocate buffers when called for the first time
1888 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001889static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001890{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001891 struct em28xx_fh *fh = filp->private_data;
1892 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001893 int rc;
1894
1895 rc = check_dev(dev);
1896 if (rc < 0)
1897 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001898
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001899 if (unlikely(res_get(fh) < 0))
1900 return POLLERR;
1901
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001902 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1903 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001904
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001905 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001906}
1907
1908/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001909 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001910 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001911static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001912{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001913 struct em28xx_fh *fh = filp->private_data;
1914 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001915 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001916
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001917 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001918 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001919
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001920 rc = check_dev(dev);
1921 if (rc < 0)
1922 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001923
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001924 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001925
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001926 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1927 (unsigned long)vma->vm_start,
1928 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1929 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001930
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001931 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001932}
1933
Arjan van de Venfa027c22007-02-12 00:55:33 -08001934static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001935 .owner = THIS_MODULE,
1936 .open = em28xx_v4l2_open,
1937 .release = em28xx_v4l2_close,
1938 .read = em28xx_v4l2_read,
1939 .poll = em28xx_v4l2_poll,
1940 .mmap = em28xx_v4l2_mmap,
1941 .ioctl = video_ioctl2,
1942 .llseek = no_llseek,
1943 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001944};
1945
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001946static const struct file_operations radio_fops = {
1947 .owner = THIS_MODULE,
1948 .open = em28xx_v4l2_open,
1949 .release = em28xx_v4l2_close,
1950 .ioctl = video_ioctl2,
1951 .compat_ioctl = v4l_compat_ioctl32,
1952 .llseek = no_llseek,
1953};
1954
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001955static const struct video_device em28xx_video_template = {
1956 .fops = &em28xx_v4l_fops,
1957 .release = video_device_release,
1958
1959 .minor = -1,
1960 .vidioc_querycap = vidioc_querycap,
1961 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1962 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1963 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1964 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1965 .vidioc_g_audio = vidioc_g_audio,
1966 .vidioc_s_audio = vidioc_s_audio,
1967 .vidioc_cropcap = vidioc_cropcap,
1968
1969 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1970 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1971 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1972
1973 .vidioc_reqbufs = vidioc_reqbufs,
1974 .vidioc_querybuf = vidioc_querybuf,
1975 .vidioc_qbuf = vidioc_qbuf,
1976 .vidioc_dqbuf = vidioc_dqbuf,
1977 .vidioc_s_std = vidioc_s_std,
1978 .vidioc_enum_input = vidioc_enum_input,
1979 .vidioc_g_input = vidioc_g_input,
1980 .vidioc_s_input = vidioc_s_input,
1981 .vidioc_queryctrl = vidioc_queryctrl,
1982 .vidioc_g_ctrl = vidioc_g_ctrl,
1983 .vidioc_s_ctrl = vidioc_s_ctrl,
1984 .vidioc_streamon = vidioc_streamon,
1985 .vidioc_streamoff = vidioc_streamoff,
1986 .vidioc_g_tuner = vidioc_g_tuner,
1987 .vidioc_s_tuner = vidioc_s_tuner,
1988 .vidioc_g_frequency = vidioc_g_frequency,
1989 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001990#ifdef CONFIG_VIDEO_ADV_DEBUG
1991 .vidioc_g_register = vidioc_g_register,
1992 .vidioc_s_register = vidioc_s_register,
1993#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001994#ifdef CONFIG_VIDEO_V4L1_COMPAT
1995 .vidiocgmbuf = vidiocgmbuf,
1996#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001997
1998 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001999 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002000};
2001
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002002static struct video_device em28xx_radio_template = {
2003 .name = "em28xx-radio",
2004 .type = VID_TYPE_TUNER,
2005 .fops = &radio_fops,
2006 .minor = -1,
2007 .vidioc_querycap = radio_querycap,
2008 .vidioc_g_tuner = radio_g_tuner,
2009 .vidioc_enum_input = radio_enum_input,
2010 .vidioc_g_audio = radio_g_audio,
2011 .vidioc_s_tuner = radio_s_tuner,
2012 .vidioc_s_audio = radio_s_audio,
2013 .vidioc_s_input = radio_s_input,
2014 .vidioc_queryctrl = radio_queryctrl,
2015 .vidioc_g_ctrl = vidioc_g_ctrl,
2016 .vidioc_s_ctrl = vidioc_s_ctrl,
2017 .vidioc_g_frequency = vidioc_g_frequency,
2018 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002019#ifdef CONFIG_VIDEO_ADV_DEBUG
2020 .vidioc_g_register = vidioc_g_register,
2021 .vidioc_s_register = vidioc_s_register,
2022#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002023};
2024
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002025/******************************** usb interface *****************************************/
2026
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002027
2028static LIST_HEAD(em28xx_extension_devlist);
2029static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2030
2031int em28xx_register_extension(struct em28xx_ops *ops)
2032{
2033 struct em28xx *h, *dev = NULL;
2034
2035 list_for_each_entry(h, &em28xx_devlist, devlist)
2036 dev = h;
2037
2038 mutex_lock(&em28xx_extension_devlist_lock);
2039 list_add_tail(&ops->next, &em28xx_extension_devlist);
2040 if (dev)
2041 ops->init(dev);
2042
2043 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2044 mutex_unlock(&em28xx_extension_devlist_lock);
2045
2046 return 0;
2047}
2048EXPORT_SYMBOL(em28xx_register_extension);
2049
2050void em28xx_unregister_extension(struct em28xx_ops *ops)
2051{
2052 struct em28xx *h, *dev = NULL;
2053
2054 list_for_each_entry(h, &em28xx_devlist, devlist)
2055 dev = h;
2056
2057 if (dev)
2058 ops->fini(dev);
2059
2060 mutex_lock(&em28xx_extension_devlist_lock);
2061 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2062 list_del(&ops->next);
2063 mutex_unlock(&em28xx_extension_devlist_lock);
2064}
2065EXPORT_SYMBOL(em28xx_unregister_extension);
2066
Adrian Bunk532fe652008-01-28 22:10:48 -03002067static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2068 const struct video_device *template,
2069 const int type,
2070 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002071{
2072 struct video_device *vfd;
2073
2074 vfd = video_device_alloc();
2075 if (NULL == vfd)
2076 return NULL;
2077 *vfd = *template;
2078 vfd->minor = -1;
2079 vfd->dev = &dev->udev->dev;
2080 vfd->release = video_device_release;
2081 vfd->type = type;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03002082 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002083
2084 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2085 dev->name, type_name);
2086
2087 return vfd;
2088}
2089
2090
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002091/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002092 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093 * allocates and inits the device structs, registers i2c bus and v4l device
2094 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002095static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002096 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002097{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002098 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002099 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002100 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002101 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002102 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002103
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002104 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002105 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002106 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002107 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002108 init_waitqueue_head(&dev->wait_frame);
2109 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002110
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002111 dev->em28xx_write_regs = em28xx_write_regs;
2112 dev->em28xx_read_reg = em28xx_read_reg;
2113 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2114 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2115 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002116 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002117
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002118 errCode = em28xx_read_reg(dev, CHIPID_REG);
2119 if (errCode >= 0)
2120 em28xx_info("em28xx chip ID = %d\n", errCode);
2121
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002122 em28xx_pre_card_setup(dev);
2123
2124 errCode = em28xx_config(dev);
2125 if (errCode) {
2126 em28xx_errdev("error configuring device\n");
2127 em28xx_devused &= ~(1<<dev->devno);
2128 kfree(dev);
2129 return -ENOMEM;
2130 }
2131
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002132 /* register i2c bus */
2133 em28xx_i2c_register(dev);
2134
2135 /* Do board specific init and eeprom reading */
2136 em28xx_card_setup(dev);
2137
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002138 /* Configure audio */
2139 em28xx_audio_analog_set(dev);
2140
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002141 /* configure the device */
2142 em28xx_config_i2c(dev);
2143
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002144 /* set default norm */
2145 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002146
2147 maxw = norm_maxw(dev);
2148 maxh = norm_maxh(dev);
2149
2150 /* set default image size */
2151 dev->width = maxw;
2152 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002153 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002154 dev->field_size = dev->width * dev->height;
2155 dev->frame_size =
2156 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2157 dev->bytesperline = dev->width * 2;
2158 dev->hscale = 0;
2159 dev->vscale = 0;
2160 dev->ctl_input = 2;
2161
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002162 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002163
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002164 list_add_tail(&dev->devlist, &em28xx_devlist);
2165
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002166 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002167 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2168 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002169 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002170 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002171 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002172 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03002173 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002174 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002175
2176 /* register v4l2 video video_device */
2177 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2178 video_nr[dev->devno]);
2179 if (retval) {
2180 em28xx_errdev("unable to register video device (error=%i).\n",
2181 retval);
2182 goto fail_unreg;
2183 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002185 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002186 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2187 VFL_TYPE_VBI, "vbi");
2188 /* register v4l2 vbi video_device */
2189 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2190 vbi_nr[dev->devno]) < 0) {
2191 em28xx_errdev("unable to register vbi device\n");
2192 retval = -ENODEV;
2193 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002194 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002195
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002196 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2197 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2198 VFL_TYPE_RADIO, "radio");
2199 if (NULL == dev->radio_dev) {
2200 em28xx_errdev("cannot allocate video_device.\n");
2201 goto fail_unreg;
2202 }
2203 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2204 radio_nr[dev->devno]);
2205 if (retval < 0) {
2206 em28xx_errdev("can't register radio device\n");
2207 goto fail_unreg;
2208 }
2209 em28xx_info("Registered radio device as /dev/radio%d\n",
2210 dev->radio_dev->minor & 0x1f);
2211 }
2212
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002213 /* init video dma queues */
2214 INIT_LIST_HEAD(&dev->vidq.active);
2215 INIT_LIST_HEAD(&dev->vidq.queued);
2216
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002217
Sascha Sommer5a804152007-11-03 21:22:38 -03002218 if (dev->has_msp34xx) {
2219 /* Send a reset to other chips via gpio */
2220 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2221 msleep(3);
2222 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2223 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002224 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002225
Sascha Sommer5a804152007-11-03 21:22:38 -03002226 video_mux(dev, 0);
2227
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002228 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2229 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2230 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002231
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002232 mutex_lock(&em28xx_extension_devlist_lock);
2233 if (!list_empty(&em28xx_extension_devlist)) {
2234 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2235 if (ops->id)
2236 ops->init(dev);
2237 }
2238 }
2239 mutex_unlock(&em28xx_extension_devlist_lock);
2240
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002242
2243fail_unreg:
2244 em28xx_release_resources(dev);
2245 mutex_unlock(&dev->lock);
2246 kfree(dev);
2247 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002248}
2249
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002250#if defined(CONFIG_MODULES) && defined(MODULE)
2251static void request_module_async(struct work_struct *work)
2252{
2253 struct em28xx *dev = container_of(work,
2254 struct em28xx, request_module_wk);
2255
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002256 if (dev->has_audio_class)
2257 request_module("snd-usb-audio");
2258 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002259 request_module("em28xx-alsa");
2260}
2261
2262static void request_modules(struct em28xx *dev)
2263{
2264 INIT_WORK(&dev->request_module_wk, request_module_async);
2265 schedule_work(&dev->request_module_wk);
2266}
2267#else
2268#define request_modules(dev)
2269#endif /* CONFIG_MODULES */
2270
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002271/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002272 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002273 * checks for supported devices
2274 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002275static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002276 const struct usb_device_id *id)
2277{
2278 const struct usb_endpoint_descriptor *endpoint;
2279 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002280 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002281 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002282 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002283 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002284
2285 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002286 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2287
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002288 /* Check to see next free device and mark as used */
2289 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2290 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002291
2292 /* Don't register audio interfaces */
2293 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002294 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002295 udev->descriptor.idVendor,udev->descriptor.idProduct,
2296 ifnum,
2297 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002298
2299 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002300 return -ENODEV;
2301 }
2302
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002303 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002304 udev->descriptor.idVendor,udev->descriptor.idProduct,
2305 ifnum,
2306 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002307
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002308 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2309
Michael Opdenacker59c51592007-05-09 08:57:56 +02002310 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002311 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2312 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002313 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002314 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002315 return -ENODEV;
2316 }
2317 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002318 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002319 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002320 return -ENODEV;
2321 }
2322
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002323 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002324 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002325 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002326 return -ENOMEM;
2327 }
2328
2329 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002330 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002331 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002332 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002333 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002334 return -ENOMEM;
2335 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002336
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002337 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002338 dev->devno = nr;
2339 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002340 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002341
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002342 /* Checks if audio is provided by some interface */
2343 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2344 uif = udev->config->interface[i];
2345 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2346 dev->has_audio_class = 1;
2347 break;
2348 }
2349 }
2350
2351 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2352 dev->has_audio_class ? "Has" : "Doesn't have");
2353
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002354 /* compute alternate max packet sizes */
2355 uif = udev->actconfig->interface[0];
2356
2357 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002358 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002359// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2360 dev->alt_max_pkt_size = kmalloc(32*
2361 dev->num_alt,GFP_KERNEL);
2362 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002363 em28xx_errdev("out of memory!\n");
2364 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002365 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002366 return -ENOMEM;
2367 }
2368
2369 for (i = 0; i < dev->num_alt ; i++) {
2370 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2371 wMaxPacketSize);
2372 dev->alt_max_pkt_size[i] =
2373 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002374 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002375 dev->alt_max_pkt_size[i]);
2376 }
2377
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002378 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002379 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002380
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002381 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002382 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002383 if (retval)
2384 return retval;
2385
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002386 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002387
2388 /* save our data pointer in this interface device */
2389 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002390
2391 request_modules(dev);
2392
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002393 return 0;
2394}
2395
2396/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002397 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002398 * called when the device gets diconencted
2399 * video device will be unregistered on v4l2_close in case it is still open
2400 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002401static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002402{
Sascha Sommer5a804152007-11-03 21:22:38 -03002403 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002404 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002405
2406 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002407 usb_set_intfdata(interface, NULL);
2408
2409 if (!dev)
2410 return;
2411
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002412 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002413
Sascha Sommer5a804152007-11-03 21:22:38 -03002414 /* wait until all current v4l2 io is finished then deallocate resources */
2415 mutex_lock(&dev->lock);
2416
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002417 wake_up_interruptible_all(&dev->open);
2418
2419 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002420 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002421 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002422 "deallocation are deferred on close.\n",
2423 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2424
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002425 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002426 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002427 dev->state |= DEV_DISCONNECTED;
2428 wake_up_interruptible(&dev->wait_frame);
2429 wake_up_interruptible(&dev->wait_stream);
2430 } else {
2431 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002432 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002433 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002434 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002435
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002436 mutex_lock(&em28xx_extension_devlist_lock);
2437 if (!list_empty(&em28xx_extension_devlist)) {
2438 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2439 ops->fini(dev);
2440 }
2441 }
2442 mutex_unlock(&em28xx_extension_devlist_lock);
2443
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002444 if (!dev->users) {
2445 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002446 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002447 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002448}
2449
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002450static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002451 .name = "em28xx",
2452 .probe = em28xx_usb_probe,
2453 .disconnect = em28xx_usb_disconnect,
2454 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002455};
2456
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002457static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002458{
2459 int result;
2460
2461 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002462 (EM28XX_VERSION_CODE >> 16) & 0xff,
2463 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002464#ifdef SNAPSHOT
2465 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2466 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2467#endif
2468
2469 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002470 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002471 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002472 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002473 " usb_register failed. Error number %d.\n", result);
2474
2475 return result;
2476}
2477
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002478static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002479{
2480 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002481 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002482}
2483
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002484module_init(em28xx_module_init);
2485module_exit(em28xx_module_exit);