blob: 064728f67740748781aece3669423f24047647fc [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
140 list_del(&buf->vb.queue);
141 wake_up(&buf->vb.done);
142}
143
144/*
145 * Identify the buffer header type and properly handles
146 */
147static void em28xx_copy_video(struct em28xx *dev,
148 struct em28xx_dmaqueue *dma_q,
149 struct em28xx_buffer *buf,
150 unsigned char *p,
151 unsigned char *outp, unsigned long len)
152{
153 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300154 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300155
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300156 if (dev->frame_size != buf->vb.size) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300157 em28xx_errdev("size %i and buf.length %lu are different!\n",
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300158 dev->frame_size, buf->vb.size);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 return;
160 }
161
162 if (dma_q->pos + len > buf->vb.size)
163 len = buf->vb.size - dma_q->pos;
164
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300165 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300166 em28xx_isocdbg("frame is not complete\n");
167 len += 4;
168 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300169 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300170
171 startread = p;
172 remain = len;
173
174 /* Interlaces frame */
175 if (buf->top_field)
176 fieldstart = outp;
177 else
178 fieldstart = outp + dev->bytesperline;
179
180 linesdone = dma_q->pos / dev->bytesperline;
181 currlinedone = dma_q->pos % dev->bytesperline;
182 offset = linesdone * dev->bytesperline * 2 + currlinedone;
183 startwrite = fieldstart + offset;
184 lencopy = dev->bytesperline - currlinedone;
185 lencopy = lencopy > remain ? remain : lencopy;
186
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300187 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300188 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300189 ((char *)startwrite + lencopy) -
190 ((char *)outp + buf->vb.size));
191 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300192 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300193 if (lencopy <= 0)
194 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300195 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300196
197 remain -= lencopy;
198
199 while (remain > 0) {
200 startwrite += lencopy + dev->bytesperline;
201 startread += lencopy;
202 if (dev->bytesperline > remain)
203 lencopy = remain;
204 else
205 lencopy = dev->bytesperline;
206
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300207 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300208 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300209 ((char *)startwrite + lencopy) -
210 ((char *)outp + buf->vb.size));
211 lencopy = remain = (char *)outp + buf->vb.size -
212 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300213 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300214 if (lencopy <= 0)
215 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300216
217 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300218
219 remain -= lencopy;
220 }
221
222 dma_q->pos += len;
223}
224
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300225static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 int packet, int status)
227{
228 char *errmsg = "Unknown";
229
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300231 case -ENOENT:
232 errmsg = "unlinked synchronuously";
233 break;
234 case -ECONNRESET:
235 errmsg = "unlinked asynchronuously";
236 break;
237 case -ENOSR:
238 errmsg = "Buffer error (overrun)";
239 break;
240 case -EPIPE:
241 errmsg = "Stalled (device not responding)";
242 break;
243 case -EOVERFLOW:
244 errmsg = "Babble (bad cable?)";
245 break;
246 case -EPROTO:
247 errmsg = "Bit-stuff error (bad cable?)";
248 break;
249 case -EILSEQ:
250 errmsg = "CRC/Timeout (could be anything)";
251 break;
252 case -ETIME:
253 errmsg = "Device does not respond";
254 break;
255 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300257 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
258 } else {
259 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
260 packet, status, errmsg);
261 }
262}
263
264/*
265 * video-buf generic routine to get the next available buffer
266 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300267static inline int get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300268 struct em28xx_buffer **buf)
269{
270 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
271
272 if (list_empty(&dma_q->active)) {
273 em28xx_isocdbg("No active queue to serve\n");
274 return 0;
275 }
276
277 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
278
279 return 1;
280}
281
282/*
283 * Controls the isoc copy of each urb packet
284 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300285static inline int em28xx_isoc_copy(struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300286{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300287 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300288 struct em28xx_dmaqueue *dma_q = urb->context;
289 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300290 unsigned char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300291 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300292 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300293
294 if (!dev)
295 return 0;
296
297 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
298 return 0;
299
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300300 if (urb->status < 0) {
301 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300302 if (urb->status == -ENOENT)
303 return 0;
304 }
305
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300306 buf = dev->isoc_ctl.buf;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300307
308 if (!buf) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300309 rc = get_next_buf(dma_q, &buf);
310 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300311 return rc;
312 }
313
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300314 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300315
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300316 for (i = 0; i < urb->number_of_packets; i++) {
317 int status = urb->iso_frame_desc[i].status;
318
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300319 if (status < 0) {
320 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300321 if (urb->iso_frame_desc[i].status != -EPROTO)
322 continue;
323 }
324
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300325 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300326
327 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300328 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300329 continue;
330 }
331 if (urb->iso_frame_desc[i].actual_length >
332 dev->max_pkt_size) {
333 em28xx_isocdbg("packet bigger than packet size");
334 continue;
335 }
336
337 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300338
339 /* FIXME: incomplete buffer checks where removed to make
340 logic simpler. Impacts of those changes should be evaluated
341 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300342 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300343 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
344 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300345
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300346 if (p[2] & 1)
347 buf->top_field = 0;
348 else {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300349 if (buf->receiving) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300350 buffer_filled(dev, dma_q, buf);
351 rc = get_next_buf(dma_q, &buf);
352 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300353 return rc;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300354
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300355 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300356 }
357
358 buf->top_field = 1;
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300359 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300360 buf->receiving = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300361 dma_q->pos = 0;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300362 } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300363 em28xx_isocdbg("VBI HEADER!!!\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364 }
365
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300366 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300367
368 /* FIXME: Should add vbi copy */
369 }
370 return rc;
371}
372
373/* ------------------------------------------------------------------
374 URB control
375 ------------------------------------------------------------------*/
376
377/*
378 * IRQ callback, called by URB callback
379 */
380static void em28xx_irq_callback(struct urb *urb)
381{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300382 struct em28xx_dmaqueue *dma_q = urb->context;
383 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300384 int rc, i;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300385 unsigned long flags;
386
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300387 spin_lock_irqsave(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300388
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389 /* Copy data from URB */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300390 rc = em28xx_isoc_copy(urb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300391
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300392 /* Reset urb buffers */
393 for (i = 0; i < urb->number_of_packets; i++) {
394 urb->iso_frame_desc[i].status = 0;
395 urb->iso_frame_desc[i].actual_length = 0;
396 }
397 urb->status = 0;
398
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300399 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
400 if (urb->status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300401 em28xx_err("urb resubmit failed (error=%i)\n",
402 urb->status);
403 }
404
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300405 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300406}
407
408/*
409 * Stop and Deallocate URBs
410 */
411static void em28xx_uninit_isoc(struct em28xx *dev)
412{
413 struct urb *urb;
414 int i;
415
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300416 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
417
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300418 dev->isoc_ctl.nfields = -1;
419 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300420 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300421 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300422 if (urb) {
423 usb_kill_urb(urb);
424 usb_unlink_urb(urb);
425 if (dev->isoc_ctl.transfer_buffer[i]) {
426 usb_buffer_free(dev->udev,
427 urb->transfer_buffer_length,
428 dev->isoc_ctl.transfer_buffer[i],
429 urb->transfer_dma);
430 }
431 usb_free_urb(urb);
432 dev->isoc_ctl.urb[i] = NULL;
433 }
434 dev->isoc_ctl.transfer_buffer[i] = NULL;
435 }
436
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300437 kfree(dev->isoc_ctl.urb);
438 kfree(dev->isoc_ctl.transfer_buffer);
439 dev->isoc_ctl.urb = NULL;
440 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300442 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300444 em28xx_capture_start(dev, 0);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300445}
446
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300447/*
448 * Allocate URBs and start IRQ
449 */
450static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
451 int num_bufs)
452{
453 struct em28xx_dmaqueue *dma_q = &dev->vidq;
454 int i;
455 int sb_size, pipe;
456 struct urb *urb;
457 int j, k;
458
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300459 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
460
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461 /* De-allocates all pending stuff */
462 em28xx_uninit_isoc(dev);
463
464 dev->isoc_ctl.num_bufs = num_bufs;
465
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300466 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300467 if (!dev->isoc_ctl.urb) {
468 em28xx_errdev("cannot alloc memory for usb buffers\n");
469 return -ENOMEM;
470 }
471
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300472 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473 GFP_KERNEL);
474 if (!dev->isoc_ctl.urb) {
475 em28xx_errdev("cannot allocate memory for usbtransfer\n");
476 kfree(dev->isoc_ctl.urb);
477 return -ENOMEM;
478 }
479
480 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
481
482 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
483
484 /* allocate urbs and transfer buffers */
485 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
486 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
487 if (!urb) {
488 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
489 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300490 return -ENOMEM;
491 }
492 dev->isoc_ctl.urb[i] = urb;
493
494 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
495 sb_size, GFP_KERNEL, &urb->transfer_dma);
496 if (!dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300497 em28xx_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300498 " buffer %i%s\n",
499 sb_size, i,
500 in_interrupt()?" while in int":"");
501 em28xx_uninit_isoc(dev);
502 return -ENOMEM;
503 }
504 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
505
506 /* FIXME: this is a hack - should be
507 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
508 should also be using 'desc.bInterval'
509 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300510 pipe = usb_rcvisocpipe(dev->udev, 0x82);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300511 usb_fill_int_urb(urb, dev->udev, pipe,
512 dev->isoc_ctl.transfer_buffer[i], sb_size,
513 em28xx_irq_callback, dma_q, 1);
514
515 urb->number_of_packets = max_packets;
516 urb->transfer_flags = URB_ISO_ASAP;
517
518 k = 0;
519 for (j = 0; j < max_packets; j++) {
520 urb->iso_frame_desc[j].offset = k;
521 urb->iso_frame_desc[j].length =
522 dev->isoc_ctl.max_pkt_size;
523 k += dev->isoc_ctl.max_pkt_size;
524 }
525 }
526
527 return 0;
528}
529
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300530static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300531{
532 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300533 int i, rc = 0;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300534
535 em28xx_videodbg("Called em28xx_start_thread\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300536
537 init_waitqueue_head(&dma_q->wq);
538
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300539 em28xx_capture_start(dev, 1);
540
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300541 /* submit urbs and enables IRQ */
542 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
543 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
544 if (rc) {
545 em28xx_err("submit of urb %i failed (error=%i)\n", i,
546 rc);
547 em28xx_uninit_isoc(dev);
548 return rc;
549 }
550 }
551
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300552 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300553 return rc;
554
555 return 0;
556}
557
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300558/* ------------------------------------------------------------------
559 Videobuf operations
560 ------------------------------------------------------------------*/
561
562static int
563buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
564{
565 struct em28xx_fh *fh = vq->priv_data;
566
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300567 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300568 if (0 == *count)
569 *count = EM28XX_DEF_BUF;
570
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300571 if (*count < EM28XX_MIN_BUF)
572 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300573
574 return 0;
575}
576
577static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
578{
579 if (in_interrupt())
580 BUG();
581
582 videobuf_waiton(&buf->vb, 0, 0);
583 videobuf_vmalloc_free(&buf->vb);
584 buf->vb.state = VIDEOBUF_NEEDS_INIT;
585}
586
587static int
588buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
589 enum v4l2_field field)
590{
591 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300592 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300593 struct em28xx *dev = fh->dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300594 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300595 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300596
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300597 /* BUG_ON(NULL == fh->fmt); */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300598
599 /* FIXME: It assumes depth = 16 */
600 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300601 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300602
603 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
604 return -EINVAL;
605
Brandon Philips05612972008-04-13 14:57:01 -0300606 buf->fmt = fh->fmt;
607 buf->vb.width = dev->width;
608 buf->vb.height = dev->height;
609 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300610
611 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300612 rc = videobuf_iolock(vq, &buf->vb, NULL);
613 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300614 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300615 }
616
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300617 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300618 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300619
620 if (urb_init) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300621 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
622 EM28XX_NUM_BUFS);
623 if (rc < 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300624 goto fail;
625
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300626 rc = em28xx_start_thread(vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300627 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300628 goto fail;
629 }
630
631 buf->vb.state = VIDEOBUF_PREPARED;
632 return 0;
633
634fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300635 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300636 return rc;
637}
638
639static void
640buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
641{
642 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
643 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300644 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300645 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300646
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300647 buf->vb.state = VIDEOBUF_QUEUED;
648 list_add_tail(&buf->vb.queue, &vidq->active);
649
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300650}
651
652static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
653{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300654 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300655 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300656 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300657
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300658 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300659
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300660 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300661}
662
663static struct videobuf_queue_ops em28xx_video_qops = {
664 .buf_setup = buffer_setup,
665 .buf_prepare = buffer_prepare,
666 .buf_queue = buffer_queue,
667 .buf_release = buffer_release,
668};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800669
670/********************* v4l2 interface ******************************************/
671
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800672/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800673 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800674 * inits registers with sane defaults
675 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800676static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800677{
678
679 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300680 if (!dev->is_em2800)
681 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800682
683 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200684
Markus Rechberger9475fb12006-02-27 00:07:34 -0300685/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
686/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200687 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
688
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800689 dev->mute = 1; /* maybe not the right place... */
690 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300691
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800692 em28xx_outfmt_set_yuv422(dev);
693 em28xx_colorlevels_set_default(dev);
694 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800695
696 return 0;
697}
698
699/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800700 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800701 * configure i2c attached devices
702 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800703static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800704{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300705 struct v4l2_routing route;
706
707 route.input = INPUT(dev->ctl_input)->vmux;
708 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100709 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300710 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300711 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800712}
713
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800714static void video_mux(struct em28xx *dev, int index)
715{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300716 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800717
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300718 route.input = INPUT(index)->vmux;
719 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800720 dev->ctl_input = index;
721 dev->ctl_ainput = INPUT(index)->amux;
722
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300723 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800724
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800725 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200726 if (dev->i2s_speed)
727 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300728 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300729 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300730 /* Note: this is msp3400 specific */
731 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800732 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300733
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300734 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800735}
736
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300737/* Usage lock check functions */
738static int res_get(struct em28xx_fh *fh)
739{
740 struct em28xx *dev = fh->dev;
741 int rc = 0;
742
743 /* This instance already has stream_on */
744 if (fh->stream_on)
745 return rc;
746
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300747 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300748 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300749
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300750 mutex_lock(&dev->lock);
751 dev->stream_on = 1;
752 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300753 mutex_unlock(&dev->lock);
754 return rc;
755}
756
757static int res_check(struct em28xx_fh *fh)
758{
759 return (fh->stream_on);
760}
761
762static void res_free(struct em28xx_fh *fh)
763{
764 struct em28xx *dev = fh->dev;
765
766 mutex_lock(&dev->lock);
767 fh->stream_on = 0;
768 dev->stream_on = 0;
769 mutex_unlock(&dev->lock);
770}
771
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800772/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300773 * em28xx_get_ctrl()
774 * return the current saturation, brightness or contrast, mute state
775 */
776static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
777{
778 switch (ctrl->id) {
779 case V4L2_CID_AUDIO_MUTE:
780 ctrl->value = dev->mute;
781 return 0;
782 case V4L2_CID_AUDIO_VOLUME:
783 ctrl->value = dev->volume;
784 return 0;
785 default:
786 return -EINVAL;
787 }
788}
789
790/*
791 * em28xx_set_ctrl()
792 * mute or set new saturation, brightness or contrast
793 */
794static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
795{
796 switch (ctrl->id) {
797 case V4L2_CID_AUDIO_MUTE:
798 if (ctrl->value != dev->mute) {
799 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300800 return em28xx_audio_analog_set(dev);
801 }
802 return 0;
803 case V4L2_CID_AUDIO_VOLUME:
804 dev->volume = ctrl->value;
805 return em28xx_audio_analog_set(dev);
806 default:
807 return -EINVAL;
808 }
809}
810
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811static int check_dev(struct em28xx *dev)
812{
813 if (dev->state & DEV_DISCONNECTED) {
814 em28xx_errdev("v4l2 ioctl: device not present\n");
815 return -ENODEV;
816 }
817
818 if (dev->state & DEV_MISCONFIGURED) {
819 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
820 "close and open it again\n");
821 return -EIO;
822 }
823 return 0;
824}
825
826static void get_scale(struct em28xx *dev,
827 unsigned int width, unsigned int height,
828 unsigned int *hscale, unsigned int *vscale)
829{
830 unsigned int maxw = norm_maxw(dev);
831 unsigned int maxh = norm_maxh(dev);
832
833 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
834 if (*hscale >= 0x4000)
835 *hscale = 0x3fff;
836
837 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
838 if (*vscale >= 0x4000)
839 *vscale = 0x3fff;
840}
841
842/* ------------------------------------------------------------------
843 IOCTL vidioc handling
844 ------------------------------------------------------------------*/
845
846static int vidioc_g_fmt_cap(struct file *file, void *priv,
847 struct v4l2_format *f)
848{
849 struct em28xx_fh *fh = priv;
850 struct em28xx *dev = fh->dev;
851
852 mutex_lock(&dev->lock);
853
854 f->fmt.pix.width = dev->width;
855 f->fmt.pix.height = dev->height;
856 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
857 f->fmt.pix.bytesperline = dev->bytesperline;
858 f->fmt.pix.sizeimage = dev->frame_size;
859 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
860
861 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
862 f->fmt.pix.field = dev->interlaced ?
863 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
864
865 mutex_unlock(&dev->lock);
866 return 0;
867}
868
869static int vidioc_try_fmt_cap(struct file *file, void *priv,
870 struct v4l2_format *f)
871{
872 struct em28xx_fh *fh = priv;
873 struct em28xx *dev = fh->dev;
874 int width = f->fmt.pix.width;
875 int height = f->fmt.pix.height;
876 unsigned int maxw = norm_maxw(dev);
877 unsigned int maxh = norm_maxh(dev);
878 unsigned int hscale, vscale;
879
880 /* width must even because of the YUYV format
881 height must be even because of interlacing */
882 height &= 0xfffe;
883 width &= 0xfffe;
884
885 if (height < 32)
886 height = 32;
887 if (height > maxh)
888 height = maxh;
889 if (width < 48)
890 width = 48;
891 if (width > maxw)
892 width = maxw;
893
894 mutex_lock(&dev->lock);
895
896 if (dev->is_em2800) {
897 /* the em2800 can only scale down to 50% */
898 if (height % (maxh / 2))
899 height = maxh;
900 if (width % (maxw / 2))
901 width = maxw;
902 /* according to empiatech support */
903 /* the MaxPacketSize is to small to support */
904 /* framesizes larger than 640x480 @ 30 fps */
905 /* or 640x576 @ 25 fps. As this would cut */
906 /* of a part of the image we prefer */
907 /* 360x576 or 360x480 for now */
908 if (width == maxw && height == maxh)
909 width /= 2;
910 }
911
912 get_scale(dev, width, height, &hscale, &vscale);
913
914 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
915 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
916
917 f->fmt.pix.width = width;
918 f->fmt.pix.height = height;
919 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
920 f->fmt.pix.bytesperline = width * 2;
921 f->fmt.pix.sizeimage = width * 2 * height;
922 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
923 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
924
925 mutex_unlock(&dev->lock);
926 return 0;
927}
928
929static int vidioc_s_fmt_cap(struct file *file, void *priv,
930 struct v4l2_format *f)
931{
932 struct em28xx_fh *fh = priv;
933 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300934 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300935
936 rc = check_dev(dev);
937 if (rc < 0)
938 return rc;
939
940 vidioc_try_fmt_cap(file, priv, f);
941
942 mutex_lock(&dev->lock);
943
Brandon Philips05612972008-04-13 14:57:01 -0300944 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
945 em28xx_errdev("%s queue busy\n", __func__);
946 rc = -EBUSY;
947 goto out;
948 }
949
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300950 if (dev->stream_on && !fh->stream_on) {
951 em28xx_errdev("%s device in use by another fh\n", __func__);
952 rc = -EBUSY;
953 goto out;
954 }
955
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300956 /* set new image size */
957 dev->width = f->fmt.pix.width;
958 dev->height = f->fmt.pix.height;
959 dev->frame_size = dev->width * dev->height * 2;
960 dev->field_size = dev->frame_size >> 1;
961 dev->bytesperline = dev->width * 2;
962 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
963
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300964 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300965 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966
Brandon Philips05612972008-04-13 14:57:01 -0300967 rc = 0;
968
969out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300970 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300971 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300972}
973
974static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
975{
976 struct em28xx_fh *fh = priv;
977 struct em28xx *dev = fh->dev;
978 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300979 int rc;
980
981 rc = check_dev(dev);
982 if (rc < 0)
983 return rc;
984
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300985 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300986 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300987 mutex_unlock(&dev->lock);
988
989 /* Adjusts width/height, if needed */
990 f.fmt.pix.width = dev->width;
991 f.fmt.pix.height = dev->height;
992 vidioc_try_fmt_cap(file, priv, &f);
993
994 mutex_lock(&dev->lock);
995
996 /* set new image size */
997 dev->width = f.fmt.pix.width;
998 dev->height = f.fmt.pix.height;
999 dev->frame_size = dev->width * dev->height * 2;
1000 dev->field_size = dev->frame_size >> 1;
1001 dev->bytesperline = dev->width * 2;
1002 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1003
1004 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001005 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001006
1007 mutex_unlock(&dev->lock);
1008 return 0;
1009}
1010
1011static const char *iname[] = {
1012 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1013 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1014 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1015 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1016 [EM28XX_VMUX_SVIDEO] = "S-Video",
1017 [EM28XX_VMUX_TELEVISION] = "Television",
1018 [EM28XX_VMUX_CABLE] = "Cable TV",
1019 [EM28XX_VMUX_DVB] = "DVB",
1020 [EM28XX_VMUX_DEBUG] = "for debug only",
1021};
1022
1023static int vidioc_enum_input(struct file *file, void *priv,
1024 struct v4l2_input *i)
1025{
1026 struct em28xx_fh *fh = priv;
1027 struct em28xx *dev = fh->dev;
1028 unsigned int n;
1029
1030 n = i->index;
1031 if (n >= MAX_EM28XX_INPUT)
1032 return -EINVAL;
1033 if (0 == INPUT(n)->type)
1034 return -EINVAL;
1035
1036 i->index = n;
1037 i->type = V4L2_INPUT_TYPE_CAMERA;
1038
1039 strcpy(i->name, iname[INPUT(n)->type]);
1040
1041 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1042 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1043 i->type = V4L2_INPUT_TYPE_TUNER;
1044
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001045 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001046
1047 return 0;
1048}
1049
1050static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1051{
1052 struct em28xx_fh *fh = priv;
1053 struct em28xx *dev = fh->dev;
1054
1055 *i = dev->ctl_input;
1056
1057 return 0;
1058}
1059
1060static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1061{
1062 struct em28xx_fh *fh = priv;
1063 struct em28xx *dev = fh->dev;
1064 int rc;
1065
1066 rc = check_dev(dev);
1067 if (rc < 0)
1068 return rc;
1069
1070 if (i >= MAX_EM28XX_INPUT)
1071 return -EINVAL;
1072 if (0 == INPUT(i)->type)
1073 return -EINVAL;
1074
1075 mutex_lock(&dev->lock);
1076
1077 video_mux(dev, i);
1078
1079 mutex_unlock(&dev->lock);
1080 return 0;
1081}
1082
1083static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1084{
1085 struct em28xx_fh *fh = priv;
1086 struct em28xx *dev = fh->dev;
1087 unsigned int index = a->index;
1088
1089 if (a->index > 1)
1090 return -EINVAL;
1091
1092 index = dev->ctl_ainput;
1093
1094 if (index == 0) {
1095 strcpy(a->name, "Television");
1096 } else {
1097 strcpy(a->name, "Line In");
1098 }
1099 a->capability = V4L2_AUDCAP_STEREO;
1100 a->index = index;
1101
1102 return 0;
1103}
1104
1105static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1106{
1107 struct em28xx_fh *fh = priv;
1108 struct em28xx *dev = fh->dev;
1109
1110 if (a->index != dev->ctl_ainput)
1111 return -EINVAL;
1112
1113 return 0;
1114}
1115
1116static int vidioc_queryctrl(struct file *file, void *priv,
1117 struct v4l2_queryctrl *qc)
1118{
1119 struct em28xx_fh *fh = priv;
1120 struct em28xx *dev = fh->dev;
1121 int id = qc->id;
1122 int i;
1123 int rc;
1124
1125 rc = check_dev(dev);
1126 if (rc < 0)
1127 return rc;
1128
1129 memset(qc, 0, sizeof(*qc));
1130
1131 qc->id = id;
1132
1133 if (!dev->has_msp34xx) {
1134 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1135 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1136 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1137 return 0;
1138 }
1139 }
1140 }
1141 mutex_lock(&dev->lock);
1142 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1143 mutex_unlock(&dev->lock);
1144
1145 if (qc->type)
1146 return 0;
1147 else
1148 return -EINVAL;
1149}
1150
1151static int vidioc_g_ctrl(struct file *file, void *priv,
1152 struct v4l2_control *ctrl)
1153{
1154 struct em28xx_fh *fh = priv;
1155 struct em28xx *dev = fh->dev;
1156 int rc;
1157
1158 rc = check_dev(dev);
1159 if (rc < 0)
1160 return rc;
1161 mutex_lock(&dev->lock);
1162
1163 if (!dev->has_msp34xx)
1164 rc = em28xx_get_ctrl(dev, ctrl);
1165 else
1166 rc = -EINVAL;
1167
1168 if (rc == -EINVAL) {
1169 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1170 rc = 0;
1171 }
1172
1173 mutex_unlock(&dev->lock);
1174 return rc;
1175}
1176
1177static int vidioc_s_ctrl(struct file *file, void *priv,
1178 struct v4l2_control *ctrl)
1179{
1180 struct em28xx_fh *fh = priv;
1181 struct em28xx *dev = fh->dev;
1182 u8 i;
1183 int rc;
1184
1185 rc = check_dev(dev);
1186 if (rc < 0)
1187 return rc;
1188
1189 mutex_lock(&dev->lock);
1190
1191 if (dev->has_msp34xx)
1192 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1193 else {
1194 rc = 1;
1195 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1196 if (ctrl->id == em28xx_qctrl[i].id) {
1197 if (ctrl->value < em28xx_qctrl[i].minimum ||
1198 ctrl->value > em28xx_qctrl[i].maximum) {
1199 rc = -ERANGE;
1200 break;
1201 }
1202
1203 rc = em28xx_set_ctrl(dev, ctrl);
1204 break;
1205 }
1206 }
1207 }
1208
1209 /* Control not found - try to send it to the attached devices */
1210 if (rc == 1) {
1211 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1212 rc = 0;
1213 }
1214
1215 mutex_unlock(&dev->lock);
1216 return rc;
1217}
1218
1219static int vidioc_g_tuner(struct file *file, void *priv,
1220 struct v4l2_tuner *t)
1221{
1222 struct em28xx_fh *fh = priv;
1223 struct em28xx *dev = fh->dev;
1224 int rc;
1225
1226 rc = check_dev(dev);
1227 if (rc < 0)
1228 return rc;
1229
1230 if (0 != t->index)
1231 return -EINVAL;
1232
1233 strcpy(t->name, "Tuner");
1234
1235 mutex_lock(&dev->lock);
1236
1237 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1238
1239 mutex_unlock(&dev->lock);
1240 return 0;
1241}
1242
1243static int vidioc_s_tuner(struct file *file, void *priv,
1244 struct v4l2_tuner *t)
1245{
1246 struct em28xx_fh *fh = priv;
1247 struct em28xx *dev = fh->dev;
1248 int rc;
1249
1250 rc = check_dev(dev);
1251 if (rc < 0)
1252 return rc;
1253
1254 if (0 != t->index)
1255 return -EINVAL;
1256
1257 mutex_lock(&dev->lock);
1258
1259 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1260
1261 mutex_unlock(&dev->lock);
1262 return 0;
1263}
1264
1265static int vidioc_g_frequency(struct file *file, void *priv,
1266 struct v4l2_frequency *f)
1267{
1268 struct em28xx_fh *fh = priv;
1269 struct em28xx *dev = fh->dev;
1270
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001271 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001272 f->frequency = dev->ctl_freq;
1273
1274 return 0;
1275}
1276
1277static int vidioc_s_frequency(struct file *file, void *priv,
1278 struct v4l2_frequency *f)
1279{
1280 struct em28xx_fh *fh = priv;
1281 struct em28xx *dev = fh->dev;
1282 int rc;
1283
1284 rc = check_dev(dev);
1285 if (rc < 0)
1286 return rc;
1287
1288 if (0 != f->tuner)
1289 return -EINVAL;
1290
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001291 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1292 return -EINVAL;
1293 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001294 return -EINVAL;
1295
1296 mutex_lock(&dev->lock);
1297
1298 dev->ctl_freq = f->frequency;
1299 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1300
1301 mutex_unlock(&dev->lock);
1302 return 0;
1303}
1304
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001305#ifdef CONFIG_VIDEO_ADV_DEBUG
1306static int em28xx_reg_len(int reg)
1307{
1308 switch (reg) {
1309 case AC97LSB_REG:
1310 case HSCALELOW_REG:
1311 case VSCALELOW_REG:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001312 return 2;
1313 default:
1314 return 1;
1315 }
1316}
1317
1318static int vidioc_g_register(struct file *file, void *priv,
1319 struct v4l2_register *reg)
1320{
1321 struct em28xx_fh *fh = priv;
1322 struct em28xx *dev = fh->dev;
1323 int ret;
1324
1325 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1326 return -EINVAL;
1327
1328 if (em28xx_reg_len(reg->reg) == 1) {
1329 ret = em28xx_read_reg(dev, reg->reg);
1330 if (ret < 0)
1331 return ret;
1332
1333 reg->val = ret;
1334 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001335 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001336 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1337 reg->reg, (char *)&val, 2);
1338 if (ret < 0)
1339 return ret;
1340
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001341 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001342 }
1343
1344 return 0;
1345}
1346
1347static int vidioc_s_register(struct file *file, void *priv,
1348 struct v4l2_register *reg)
1349{
1350 struct em28xx_fh *fh = priv;
1351 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001352 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001353
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001354 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001355
1356 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1357 em28xx_reg_len(reg->reg));
1358}
1359#endif
1360
1361
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001362static int vidioc_cropcap(struct file *file, void *priv,
1363 struct v4l2_cropcap *cc)
1364{
1365 struct em28xx_fh *fh = priv;
1366 struct em28xx *dev = fh->dev;
1367
1368 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1369 return -EINVAL;
1370
1371 cc->bounds.left = 0;
1372 cc->bounds.top = 0;
1373 cc->bounds.width = dev->width;
1374 cc->bounds.height = dev->height;
1375 cc->defrect = cc->bounds;
1376 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1377 cc->pixelaspect.denominator = 59;
1378
1379 return 0;
1380}
1381
1382static int vidioc_streamon(struct file *file, void *priv,
1383 enum v4l2_buf_type type)
1384{
1385 struct em28xx_fh *fh = priv;
1386 struct em28xx *dev = fh->dev;
1387 int rc;
1388
1389 rc = check_dev(dev);
1390 if (rc < 0)
1391 return rc;
1392
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001393
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001394 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001395 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001396
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001397 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001398}
1399
1400static int vidioc_streamoff(struct file *file, void *priv,
1401 enum v4l2_buf_type type)
1402{
1403 struct em28xx_fh *fh = priv;
1404 struct em28xx *dev = fh->dev;
1405 int rc;
1406
1407 rc = check_dev(dev);
1408 if (rc < 0)
1409 return rc;
1410
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001411 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1412 return -EINVAL;
1413 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001414 return -EINVAL;
1415
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001416 videobuf_streamoff(&fh->vb_vidq);
1417 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001418
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001419 return 0;
1420}
1421
1422static int vidioc_querycap(struct file *file, void *priv,
1423 struct v4l2_capability *cap)
1424{
1425 struct em28xx_fh *fh = priv;
1426 struct em28xx *dev = fh->dev;
1427
1428 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1429 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1430 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1431
1432 cap->version = EM28XX_VERSION_CODE;
1433
1434 cap->capabilities =
1435 V4L2_CAP_SLICED_VBI_CAPTURE |
1436 V4L2_CAP_VIDEO_CAPTURE |
1437 V4L2_CAP_AUDIO |
1438 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1439
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001440 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001441 cap->capabilities |= V4L2_CAP_TUNER;
1442
1443 return 0;
1444}
1445
1446static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1447 struct v4l2_fmtdesc *fmtd)
1448{
1449 if (fmtd->index != 0)
1450 return -EINVAL;
1451
1452 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1453 strcpy(fmtd->description, "Packed YUY2");
1454 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1455 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1456
1457 return 0;
1458}
1459
1460/* Sliced VBI ioctls */
1461static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1462 struct v4l2_format *f)
1463{
1464 struct em28xx_fh *fh = priv;
1465 struct em28xx *dev = fh->dev;
1466 int rc;
1467
1468 rc = check_dev(dev);
1469 if (rc < 0)
1470 return rc;
1471
1472 mutex_lock(&dev->lock);
1473
1474 f->fmt.sliced.service_set = 0;
1475
1476 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1477
1478 if (f->fmt.sliced.service_set == 0)
1479 rc = -EINVAL;
1480
1481 mutex_unlock(&dev->lock);
1482 return rc;
1483}
1484
1485static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1486 struct v4l2_format *f)
1487{
1488 struct em28xx_fh *fh = priv;
1489 struct em28xx *dev = fh->dev;
1490 int rc;
1491
1492 rc = check_dev(dev);
1493 if (rc < 0)
1494 return rc;
1495
1496 mutex_lock(&dev->lock);
1497 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1498 mutex_unlock(&dev->lock);
1499
1500 if (f->fmt.sliced.service_set == 0)
1501 return -EINVAL;
1502
1503 return 0;
1504}
1505
1506
1507static int vidioc_reqbufs(struct file *file, void *priv,
1508 struct v4l2_requestbuffers *rb)
1509{
1510 struct em28xx_fh *fh = priv;
1511 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001512 int rc;
1513
1514 rc = check_dev(dev);
1515 if (rc < 0)
1516 return rc;
1517
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001518 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001519}
1520
1521static int vidioc_querybuf(struct file *file, void *priv,
1522 struct v4l2_buffer *b)
1523{
1524 struct em28xx_fh *fh = priv;
1525 struct em28xx *dev = fh->dev;
1526 int rc;
1527
1528 rc = check_dev(dev);
1529 if (rc < 0)
1530 return rc;
1531
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001532 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001533}
1534
1535static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1536{
1537 struct em28xx_fh *fh = priv;
1538 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001539 int rc;
1540
1541 rc = check_dev(dev);
1542 if (rc < 0)
1543 return rc;
1544
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001545 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001546}
1547
1548static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1549{
1550 struct em28xx_fh *fh = priv;
1551 struct em28xx *dev = fh->dev;
1552 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001553
1554 rc = check_dev(dev);
1555 if (rc < 0)
1556 return rc;
1557
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001558 return (videobuf_dqbuf(&fh->vb_vidq, b,
1559 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001560}
1561
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001562#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001563static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001564{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001565 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001566
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001567 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001568}
1569#endif
1570
1571
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001572/* ----------------------------------------------------------- */
1573/* RADIO ESPECIFIC IOCTLS */
1574/* ----------------------------------------------------------- */
1575
1576static int radio_querycap(struct file *file, void *priv,
1577 struct v4l2_capability *cap)
1578{
1579 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1580
1581 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1582 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1583 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1584
1585 cap->version = EM28XX_VERSION_CODE;
1586 cap->capabilities = V4L2_CAP_TUNER;
1587 return 0;
1588}
1589
1590static int radio_g_tuner(struct file *file, void *priv,
1591 struct v4l2_tuner *t)
1592{
1593 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1594
1595 if (unlikely(t->index > 0))
1596 return -EINVAL;
1597
1598 strcpy(t->name, "Radio");
1599 t->type = V4L2_TUNER_RADIO;
1600
1601 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1602 return 0;
1603}
1604
1605static int radio_enum_input(struct file *file, void *priv,
1606 struct v4l2_input *i)
1607{
1608 if (i->index != 0)
1609 return -EINVAL;
1610 strcpy(i->name, "Radio");
1611 i->type = V4L2_INPUT_TYPE_TUNER;
1612
1613 return 0;
1614}
1615
1616static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1617{
1618 if (unlikely(a->index))
1619 return -EINVAL;
1620
1621 strcpy(a->name, "Radio");
1622 return 0;
1623}
1624
1625static int radio_s_tuner(struct file *file, void *priv,
1626 struct v4l2_tuner *t)
1627{
1628 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1629
1630 if (0 != t->index)
1631 return -EINVAL;
1632
1633 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1634
1635 return 0;
1636}
1637
1638static int radio_s_audio(struct file *file, void *fh,
1639 struct v4l2_audio *a)
1640{
1641 return 0;
1642}
1643
1644static int radio_s_input(struct file *file, void *fh, unsigned int i)
1645{
1646 return 0;
1647}
1648
1649static int radio_queryctrl(struct file *file, void *priv,
1650 struct v4l2_queryctrl *qc)
1651{
1652 int i;
1653
1654 if (qc->id < V4L2_CID_BASE ||
1655 qc->id >= V4L2_CID_LASTP1)
1656 return -EINVAL;
1657
1658 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1659 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1660 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1661 return 0;
1662 }
1663 }
1664
1665 return -EINVAL;
1666}
1667
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001668/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001669 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001670 * inits the device and starts isoc transfer
1671 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001672static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001674 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001675 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001676 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001677 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001678 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001679
Trent Piephoa991f442007-10-10 05:37:43 -03001680 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001681 if (h->vdev->minor == minor) {
1682 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001683 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001684 }
1685 if (h->vbi_dev->minor == minor) {
1686 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001687 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001688 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001689 if (h->radio_dev &&
1690 h->radio_dev->minor == minor) {
1691 radio = 1;
1692 dev = h;
1693 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001694 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001695 if (NULL == dev)
1696 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001697
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001698 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001699 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001700
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001701 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001702
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001703 if (!fh) {
1704 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1705 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001707 mutex_lock(&dev->lock);
1708 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001709 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001710 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001711 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001712
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001713 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001714 dev->width = norm_maxw(dev);
1715 dev->height = norm_maxh(dev);
1716 dev->frame_size = dev->width * dev->height * 2;
1717 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1718 dev->bytesperline = dev->width * 2;
1719 dev->hscale = 0;
1720 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001721
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001722 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001723 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001724
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001725 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001726 if (fh->radio) {
1727 em28xx_videodbg("video_open: setting radio device\n");
1728 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1729 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
1731 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001732
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001733 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1734 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1735 sizeof(struct em28xx_buffer), fh);
1736
Ingo Molnar3593cab2006-02-07 06:49:14 -02001737 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738 return errCode;
1739}
1740
1741/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001742 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001743 * unregisters the v4l2,i2c and usb devices
1744 * called when the device gets disconected or at module unload
1745*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001746static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001748
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001749 /*FIXME: I2C IR should be disconnected */
1750
1751 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1752 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1753 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001754 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001755 if (dev->radio_dev) {
1756 if (-1 != dev->radio_dev->minor)
1757 video_unregister_device(dev->radio_dev);
1758 else
1759 video_device_release(dev->radio_dev);
1760 dev->radio_dev = NULL;
1761 }
1762 if (dev->vbi_dev) {
1763 if (-1 != dev->vbi_dev->minor)
1764 video_unregister_device(dev->vbi_dev);
1765 else
1766 video_device_release(dev->vbi_dev);
1767 dev->vbi_dev = NULL;
1768 }
1769 if (dev->vdev) {
1770 if (-1 != dev->vdev->minor)
1771 video_unregister_device(dev->vdev);
1772 else
1773 video_device_release(dev->vdev);
1774 dev->vdev = NULL;
1775 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001776 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001777 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001778
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001779 /* Mark device as unused */
1780 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781}
1782
1783/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001784 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001785 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1786 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001787static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001788{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001789 struct em28xx_fh *fh = filp->private_data;
1790 struct em28xx *dev = fh->dev;
1791 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001792
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001793 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001794
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001795
1796 if (res_check(fh))
1797 res_free(fh);
1798
Ingo Molnar3593cab2006-02-07 06:49:14 -02001799 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001800
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001801 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001802 videobuf_stop(&fh->vb_vidq);
1803 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001804
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001805 /* the device is already disconnect,
1806 free the remaining resources */
1807 if (dev->state & DEV_DISCONNECTED) {
1808 em28xx_release_resources(dev);
1809 mutex_unlock(&dev->lock);
1810 kfree(dev);
1811 return 0;
1812 }
1813
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001814 /* do this before setting alternate! */
1815 em28xx_uninit_isoc(dev);
1816
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001817 /* set alternate 0 */
1818 dev->alt = 0;
1819 em28xx_videodbg("setting alternate 0\n");
1820 errCode = usb_set_interface(dev->udev, 0, 0);
1821 if (errCode < 0) {
1822 em28xx_errdev("cannot change alternate number to "
1823 "0 (error=%i)\n", errCode);
1824 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001825 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001826 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001827 dev->users--;
1828 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001829 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001830 return 0;
1831}
1832
1833/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001834 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001835 * will allocate buffers when called for the first time
1836 */
1837static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001838em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001839 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001840{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001841 struct em28xx_fh *fh = filp->private_data;
1842 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001843 int rc;
1844
1845 rc = check_dev(dev);
1846 if (rc < 0)
1847 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001848
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001849 /* FIXME: read() is not prepared to allow changing the video
1850 resolution while streaming. Seems a bug at em28xx_set_fmt
1851 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001852
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001853 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1854 if (unlikely(res_get(fh)))
1855 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001856
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001857 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1858 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001859 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001860 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001861}
1862
1863/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001864 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001865 * will allocate buffers when called for the first time
1866 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001867static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001868{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001869 struct em28xx_fh *fh = filp->private_data;
1870 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001871 int rc;
1872
1873 rc = check_dev(dev);
1874 if (rc < 0)
1875 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001876
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001877 if (unlikely(res_get(fh) < 0))
1878 return POLLERR;
1879
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001880 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1881 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001882
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001883 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001884}
1885
1886/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001887 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001888 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001889static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
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;
Markus Rechberger9c755412005-11-08 21:37:52 -08001894
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001895 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001896 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001897
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001898 rc = check_dev(dev);
1899 if (rc < 0)
1900 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001901
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001902 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001903
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001904 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1905 (unsigned long)vma->vm_start,
1906 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1907 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001908
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001909 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001910}
1911
Arjan van de Venfa027c22007-02-12 00:55:33 -08001912static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001913 .owner = THIS_MODULE,
1914 .open = em28xx_v4l2_open,
1915 .release = em28xx_v4l2_close,
1916 .read = em28xx_v4l2_read,
1917 .poll = em28xx_v4l2_poll,
1918 .mmap = em28xx_v4l2_mmap,
1919 .ioctl = video_ioctl2,
1920 .llseek = no_llseek,
1921 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001922};
1923
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001924static const struct file_operations radio_fops = {
1925 .owner = THIS_MODULE,
1926 .open = em28xx_v4l2_open,
1927 .release = em28xx_v4l2_close,
1928 .ioctl = video_ioctl2,
1929 .compat_ioctl = v4l_compat_ioctl32,
1930 .llseek = no_llseek,
1931};
1932
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001933static const struct video_device em28xx_video_template = {
1934 .fops = &em28xx_v4l_fops,
1935 .release = video_device_release,
1936
1937 .minor = -1,
1938 .vidioc_querycap = vidioc_querycap,
1939 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1940 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1941 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1942 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1943 .vidioc_g_audio = vidioc_g_audio,
1944 .vidioc_s_audio = vidioc_s_audio,
1945 .vidioc_cropcap = vidioc_cropcap,
1946
1947 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1948 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1949 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1950
1951 .vidioc_reqbufs = vidioc_reqbufs,
1952 .vidioc_querybuf = vidioc_querybuf,
1953 .vidioc_qbuf = vidioc_qbuf,
1954 .vidioc_dqbuf = vidioc_dqbuf,
1955 .vidioc_s_std = vidioc_s_std,
1956 .vidioc_enum_input = vidioc_enum_input,
1957 .vidioc_g_input = vidioc_g_input,
1958 .vidioc_s_input = vidioc_s_input,
1959 .vidioc_queryctrl = vidioc_queryctrl,
1960 .vidioc_g_ctrl = vidioc_g_ctrl,
1961 .vidioc_s_ctrl = vidioc_s_ctrl,
1962 .vidioc_streamon = vidioc_streamon,
1963 .vidioc_streamoff = vidioc_streamoff,
1964 .vidioc_g_tuner = vidioc_g_tuner,
1965 .vidioc_s_tuner = vidioc_s_tuner,
1966 .vidioc_g_frequency = vidioc_g_frequency,
1967 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001968#ifdef CONFIG_VIDEO_ADV_DEBUG
1969 .vidioc_g_register = vidioc_g_register,
1970 .vidioc_s_register = vidioc_s_register,
1971#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001972#ifdef CONFIG_VIDEO_V4L1_COMPAT
1973 .vidiocgmbuf = vidiocgmbuf,
1974#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001975
1976 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001977 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001978};
1979
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001980static struct video_device em28xx_radio_template = {
1981 .name = "em28xx-radio",
1982 .type = VID_TYPE_TUNER,
1983 .fops = &radio_fops,
1984 .minor = -1,
1985 .vidioc_querycap = radio_querycap,
1986 .vidioc_g_tuner = radio_g_tuner,
1987 .vidioc_enum_input = radio_enum_input,
1988 .vidioc_g_audio = radio_g_audio,
1989 .vidioc_s_tuner = radio_s_tuner,
1990 .vidioc_s_audio = radio_s_audio,
1991 .vidioc_s_input = radio_s_input,
1992 .vidioc_queryctrl = radio_queryctrl,
1993 .vidioc_g_ctrl = vidioc_g_ctrl,
1994 .vidioc_s_ctrl = vidioc_s_ctrl,
1995 .vidioc_g_frequency = vidioc_g_frequency,
1996 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001997#ifdef CONFIG_VIDEO_ADV_DEBUG
1998 .vidioc_g_register = vidioc_g_register,
1999 .vidioc_s_register = vidioc_s_register,
2000#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002001};
2002
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002003/******************************** usb interface *****************************************/
2004
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002005
2006static LIST_HEAD(em28xx_extension_devlist);
2007static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2008
2009int em28xx_register_extension(struct em28xx_ops *ops)
2010{
2011 struct em28xx *h, *dev = NULL;
2012
2013 list_for_each_entry(h, &em28xx_devlist, devlist)
2014 dev = h;
2015
2016 mutex_lock(&em28xx_extension_devlist_lock);
2017 list_add_tail(&ops->next, &em28xx_extension_devlist);
2018 if (dev)
2019 ops->init(dev);
2020
2021 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2022 mutex_unlock(&em28xx_extension_devlist_lock);
2023
2024 return 0;
2025}
2026EXPORT_SYMBOL(em28xx_register_extension);
2027
2028void em28xx_unregister_extension(struct em28xx_ops *ops)
2029{
2030 struct em28xx *h, *dev = NULL;
2031
2032 list_for_each_entry(h, &em28xx_devlist, devlist)
2033 dev = h;
2034
2035 if (dev)
2036 ops->fini(dev);
2037
2038 mutex_lock(&em28xx_extension_devlist_lock);
2039 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2040 list_del(&ops->next);
2041 mutex_unlock(&em28xx_extension_devlist_lock);
2042}
2043EXPORT_SYMBOL(em28xx_unregister_extension);
2044
Adrian Bunk532fe652008-01-28 22:10:48 -03002045static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2046 const struct video_device *template,
2047 const int type,
2048 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002049{
2050 struct video_device *vfd;
2051
2052 vfd = video_device_alloc();
2053 if (NULL == vfd)
2054 return NULL;
2055 *vfd = *template;
2056 vfd->minor = -1;
2057 vfd->dev = &dev->udev->dev;
2058 vfd->release = video_device_release;
2059 vfd->type = type;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03002060 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002061
2062 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2063 dev->name, type_name);
2064
2065 return vfd;
2066}
2067
2068
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002069/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002070 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002071 * allocates and inits the device structs, registers i2c bus and v4l device
2072 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002073static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002074 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002075{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002076 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002077 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002078 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002079 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002080 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002081
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002082 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002083 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002084 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002085 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002086 init_waitqueue_head(&dev->wait_frame);
2087 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002088
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002089 dev->em28xx_write_regs = em28xx_write_regs;
2090 dev->em28xx_read_reg = em28xx_read_reg;
2091 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2092 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2093 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002094 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002095
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002096 errCode = em28xx_read_reg(dev, CHIPID_REG);
2097 if (errCode >= 0)
2098 em28xx_info("em28xx chip ID = %d\n", errCode);
2099
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002100 em28xx_pre_card_setup(dev);
2101
2102 errCode = em28xx_config(dev);
2103 if (errCode) {
2104 em28xx_errdev("error configuring device\n");
2105 em28xx_devused &= ~(1<<dev->devno);
2106 kfree(dev);
2107 return -ENOMEM;
2108 }
2109
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002110 /* register i2c bus */
2111 em28xx_i2c_register(dev);
2112
2113 /* Do board specific init and eeprom reading */
2114 em28xx_card_setup(dev);
2115
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002116 /* Configure audio */
2117 em28xx_audio_analog_set(dev);
2118
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002119 /* configure the device */
2120 em28xx_config_i2c(dev);
2121
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002122 /* set default norm */
2123 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002124
2125 maxw = norm_maxw(dev);
2126 maxh = norm_maxh(dev);
2127
2128 /* set default image size */
2129 dev->width = maxw;
2130 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002131 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002132 dev->field_size = dev->width * dev->height;
2133 dev->frame_size =
2134 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2135 dev->bytesperline = dev->width * 2;
2136 dev->hscale = 0;
2137 dev->vscale = 0;
2138 dev->ctl_input = 2;
2139
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002140 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002141
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002142 list_add_tail(&dev->devlist, &em28xx_devlist);
2143
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002144 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002145 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2146 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002147 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002148 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002149 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002150 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03002151 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002152 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002153
2154 /* register v4l2 video video_device */
2155 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2156 video_nr[dev->devno]);
2157 if (retval) {
2158 em28xx_errdev("unable to register video device (error=%i).\n",
2159 retval);
2160 goto fail_unreg;
2161 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002162
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002163 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002164 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2165 VFL_TYPE_VBI, "vbi");
2166 /* register v4l2 vbi video_device */
2167 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2168 vbi_nr[dev->devno]) < 0) {
2169 em28xx_errdev("unable to register vbi device\n");
2170 retval = -ENODEV;
2171 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002172 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002173
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002174 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2175 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2176 VFL_TYPE_RADIO, "radio");
2177 if (NULL == dev->radio_dev) {
2178 em28xx_errdev("cannot allocate video_device.\n");
2179 goto fail_unreg;
2180 }
2181 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2182 radio_nr[dev->devno]);
2183 if (retval < 0) {
2184 em28xx_errdev("can't register radio device\n");
2185 goto fail_unreg;
2186 }
2187 em28xx_info("Registered radio device as /dev/radio%d\n",
2188 dev->radio_dev->minor & 0x1f);
2189 }
2190
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002191 /* init video dma queues */
2192 INIT_LIST_HEAD(&dev->vidq.active);
2193 INIT_LIST_HEAD(&dev->vidq.queued);
2194
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002195
Sascha Sommer5a804152007-11-03 21:22:38 -03002196 if (dev->has_msp34xx) {
2197 /* Send a reset to other chips via gpio */
2198 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2199 msleep(3);
2200 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2201 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002202 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002203
Sascha Sommer5a804152007-11-03 21:22:38 -03002204 video_mux(dev, 0);
2205
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002206 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2207 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2208 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002209
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002210 mutex_lock(&em28xx_extension_devlist_lock);
2211 if (!list_empty(&em28xx_extension_devlist)) {
2212 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2213 if (ops->id)
2214 ops->init(dev);
2215 }
2216 }
2217 mutex_unlock(&em28xx_extension_devlist_lock);
2218
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002219 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002220
2221fail_unreg:
2222 em28xx_release_resources(dev);
2223 mutex_unlock(&dev->lock);
2224 kfree(dev);
2225 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002226}
2227
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002228#if defined(CONFIG_MODULES) && defined(MODULE)
2229static void request_module_async(struct work_struct *work)
2230{
2231 struct em28xx *dev = container_of(work,
2232 struct em28xx, request_module_wk);
2233
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002234 if (dev->has_audio_class)
2235 request_module("snd-usb-audio");
2236 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002237 request_module("em28xx-alsa");
2238}
2239
2240static void request_modules(struct em28xx *dev)
2241{
2242 INIT_WORK(&dev->request_module_wk, request_module_async);
2243 schedule_work(&dev->request_module_wk);
2244}
2245#else
2246#define request_modules(dev)
2247#endif /* CONFIG_MODULES */
2248
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002249/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002250 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002251 * checks for supported devices
2252 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002253static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002254 const struct usb_device_id *id)
2255{
2256 const struct usb_endpoint_descriptor *endpoint;
2257 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002258 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002259 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002260 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002261 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002262
2263 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002264 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2265
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002266 /* Check to see next free device and mark as used */
2267 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2268 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002269
2270 /* Don't register audio interfaces */
2271 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002272 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002273 udev->descriptor.idVendor,udev->descriptor.idProduct,
2274 ifnum,
2275 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002276
2277 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002278 return -ENODEV;
2279 }
2280
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002281 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002282 udev->descriptor.idVendor,udev->descriptor.idProduct,
2283 ifnum,
2284 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002285
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002286 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2287
Michael Opdenacker59c51592007-05-09 08:57:56 +02002288 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002289 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2290 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002291 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002292 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002293 return -ENODEV;
2294 }
2295 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002296 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002297 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002298 return -ENODEV;
2299 }
2300
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002301 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002302 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002303 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002304 return -ENOMEM;
2305 }
2306
2307 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002308 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002309 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002310 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002311 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002312 return -ENOMEM;
2313 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002314
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002315 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002316 dev->devno = nr;
2317 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002318 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002319
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002320 /* Checks if audio is provided by some interface */
2321 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2322 uif = udev->config->interface[i];
2323 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2324 dev->has_audio_class = 1;
2325 break;
2326 }
2327 }
2328
2329 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2330 dev->has_audio_class ? "Has" : "Doesn't have");
2331
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002332 /* compute alternate max packet sizes */
2333 uif = udev->actconfig->interface[0];
2334
2335 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002336 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002337// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2338 dev->alt_max_pkt_size = kmalloc(32*
2339 dev->num_alt,GFP_KERNEL);
2340 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002341 em28xx_errdev("out of memory!\n");
2342 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002343 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002344 return -ENOMEM;
2345 }
2346
2347 for (i = 0; i < dev->num_alt ; i++) {
2348 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2349 wMaxPacketSize);
2350 dev->alt_max_pkt_size[i] =
2351 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002352 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002353 dev->alt_max_pkt_size[i]);
2354 }
2355
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002356 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002357 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002358
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002359 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002360 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002361 if (retval)
2362 return retval;
2363
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002364 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002365
2366 /* save our data pointer in this interface device */
2367 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002368
2369 request_modules(dev);
2370
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002371 return 0;
2372}
2373
2374/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002375 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002376 * called when the device gets diconencted
2377 * video device will be unregistered on v4l2_close in case it is still open
2378 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002379static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002380{
Sascha Sommer5a804152007-11-03 21:22:38 -03002381 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002382 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002383
2384 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002385 usb_set_intfdata(interface, NULL);
2386
2387 if (!dev)
2388 return;
2389
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002390 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002391
Sascha Sommer5a804152007-11-03 21:22:38 -03002392 /* wait until all current v4l2 io is finished then deallocate resources */
2393 mutex_lock(&dev->lock);
2394
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002395 wake_up_interruptible_all(&dev->open);
2396
2397 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002398 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002399 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002400 "deallocation are deferred on close.\n",
2401 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2402
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002403 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002404 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002405 dev->state |= DEV_DISCONNECTED;
2406 wake_up_interruptible(&dev->wait_frame);
2407 wake_up_interruptible(&dev->wait_stream);
2408 } else {
2409 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002410 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002411 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002412 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002413
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002414 mutex_lock(&em28xx_extension_devlist_lock);
2415 if (!list_empty(&em28xx_extension_devlist)) {
2416 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2417 ops->fini(dev);
2418 }
2419 }
2420 mutex_unlock(&em28xx_extension_devlist_lock);
2421
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002422 if (!dev->users) {
2423 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002424 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002425 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002426}
2427
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002428static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002429 .name = "em28xx",
2430 .probe = em28xx_usb_probe,
2431 .disconnect = em28xx_usb_disconnect,
2432 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002433};
2434
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002435static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002436{
2437 int result;
2438
2439 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002440 (EM28XX_VERSION_CODE >> 16) & 0xff,
2441 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002442#ifdef SNAPSHOT
2443 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2444 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2445#endif
2446
2447 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002448 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002449 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002450 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002451 " usb_register failed. Error number %d.\n", result);
2452
2453 return result;
2454}
2455
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002456static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002457{
2458 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002459 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002460}
2461
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002462module_init(em28xx_module_init);
2463module_exit(em28xx_module_exit);