blob: 90a11b5ac0b10af738bf69acb87faae78e7e3f25 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080035#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030042#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030043#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080044
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080045#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
46 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030047 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080049
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050#define DRIVER_NAME "em28xx"
51#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030052#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080053
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080054#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080055 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030057 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080058
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030059static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030060module_param(isoc_debug, int, 0644);
61MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030062
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030063#define em28xx_isocdbg(fmt, arg...) \
64do {\
65 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030066 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030067 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030070
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080071MODULE_AUTHOR(DRIVER_AUTHOR);
72MODULE_DESCRIPTION(DRIVER_DESC);
73MODULE_LICENSE("GPL");
74
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080075static LIST_HEAD(em28xx_devlist);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -030076static DEFINE_MUTEX(em28xx_devlist_mutex);
Markus Rechberger9c755412005-11-08 21:37:52 -080077
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080078static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020079static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030080static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
82
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080083module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020084module_param_array(video_nr, int, NULL, 0444);
85module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030086module_param_array(radio_nr, int, NULL, 0444);
87MODULE_PARM_DESC(card, "card type");
88MODULE_PARM_DESC(video_nr, "video device numbers");
89MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
90MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080091
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030092static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030093module_param(video_debug, int, 0644);
94MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080095
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020096/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
97static unsigned long em28xx_devused;
98
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080099/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200100/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800101static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800102 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200103 .id = V4L2_CID_AUDIO_VOLUME,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Volume",
106 .minimum = 0x0,
107 .maximum = 0x1f,
108 .step = 0x1,
109 .default_value = 0x1f,
110 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300111 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200112 .id = V4L2_CID_AUDIO_MUTE,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 .name = "Mute",
115 .minimum = 0,
116 .maximum = 1,
117 .step = 1,
118 .default_value = 1,
119 .flags = 0,
120 }
121};
122
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800123static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800124
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300125/* ------------------------------------------------------------------
126 DMA and thread functions
127 ------------------------------------------------------------------*/
128
129/*
130 * Announces that a buffer were filled and request the next
131 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300132static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300133 struct em28xx_dmaqueue *dma_q,
134 struct em28xx_buffer *buf)
135{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300136 /* Advice that buffer was filled */
137 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
138 buf->vb.state = VIDEOBUF_DONE;
139 buf->vb.field_count++;
140 do_gettimeofday(&buf->vb.ts);
141
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300142 dev->isoc_ctl.buf = NULL;
143
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300144 list_del(&buf->vb.queue);
145 wake_up(&buf->vb.done);
146}
147
148/*
149 * Identify the buffer header type and properly handles
150 */
151static void em28xx_copy_video(struct em28xx *dev,
152 struct em28xx_dmaqueue *dma_q,
153 struct em28xx_buffer *buf,
154 unsigned char *p,
155 unsigned char *outp, unsigned long len)
156{
157 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300159 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160
161 if (dma_q->pos + len > buf->vb.size)
162 len = buf->vb.size - dma_q->pos;
163
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300164 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300165 em28xx_isocdbg("frame is not complete\n");
166 len += 4;
167 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300168 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300169
170 startread = p;
171 remain = len;
172
173 /* Interlaces frame */
174 if (buf->top_field)
175 fieldstart = outp;
176 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300177 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300178
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300179 linesdone = dma_q->pos / bytesperline;
180 currlinedone = dma_q->pos % bytesperline;
181 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300183 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300184 lencopy = lencopy > remain ? remain : lencopy;
185
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300186 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300187 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300188 ((char *)startwrite + lencopy) -
189 ((char *)outp + buf->vb.size));
190 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300191 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300192 if (lencopy <= 0)
193 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300194 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300195
196 remain -= lencopy;
197
198 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300199 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300200 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300201 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202 lencopy = remain;
203 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300204 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300205
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
210 lencopy = remain = (char *)outp + buf->vb.size -
211 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300212 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300213 if (lencopy <= 0)
214 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215
216 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300217
218 remain -= lencopy;
219 }
220
221 dma_q->pos += len;
222}
223
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300224static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300225 int packet, int status)
226{
227 char *errmsg = "Unknown";
228
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300229 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300230 case -ENOENT:
231 errmsg = "unlinked synchronuously";
232 break;
233 case -ECONNRESET:
234 errmsg = "unlinked asynchronuously";
235 break;
236 case -ENOSR:
237 errmsg = "Buffer error (overrun)";
238 break;
239 case -EPIPE:
240 errmsg = "Stalled (device not responding)";
241 break;
242 case -EOVERFLOW:
243 errmsg = "Babble (bad cable?)";
244 break;
245 case -EPROTO:
246 errmsg = "Bit-stuff error (bad cable?)";
247 break;
248 case -EILSEQ:
249 errmsg = "CRC/Timeout (could be anything)";
250 break;
251 case -ETIME:
252 errmsg = "Device does not respond";
253 break;
254 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300255 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300256 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
257 } else {
258 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
259 packet, status, errmsg);
260 }
261}
262
263/*
264 * video-buf generic routine to get the next available buffer
265 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300266static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300267 struct em28xx_buffer **buf)
268{
269 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300270 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300271
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300272 if (list_empty(&dma_q->active)) {
273 em28xx_isocdbg("No active queue to serve\n");
274 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300275 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300276 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300277 }
278
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300279 /* Get the next buffer */
280 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
281
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300282 /* Cleans up buffer - Usefull for testing for frame/URB loss */
283 outp = videobuf_to_vmalloc(&(*buf)->vb);
284 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300285
286 dev->isoc_ctl.buf = *buf;
287
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300288 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300289}
290
291/*
292 * Controls the isoc copy of each urb packet
293 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300294static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300296 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300297 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300298 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300300 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300301
302 if (!dev)
303 return 0;
304
305 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
306 return 0;
307
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300308 if (urb->status < 0) {
309 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300310 if (urb->status == -ENOENT)
311 return 0;
312 }
313
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300314 buf = dev->isoc_ctl.buf;
315 if (buf != NULL)
316 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300317
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300318 for (i = 0; i < urb->number_of_packets; i++) {
319 int status = urb->iso_frame_desc[i].status;
320
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300321 if (status < 0) {
322 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 if (urb->iso_frame_desc[i].status != -EPROTO)
324 continue;
325 }
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328
329 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300330 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300331 continue;
332 }
333 if (urb->iso_frame_desc[i].actual_length >
334 dev->max_pkt_size) {
335 em28xx_isocdbg("packet bigger than packet size");
336 continue;
337 }
338
339 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300340
341 /* FIXME: incomplete buffer checks where removed to make
342 logic simpler. Impacts of those changes should be evaluated
343 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300344 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
345 em28xx_isocdbg("VBI HEADER!!!\n");
346 /* FIXME: Should add vbi copy */
347 continue;
348 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300349 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300350 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
351 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300352
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300353 if (!(p[2] & 1)) {
354 if (buf != NULL)
355 buffer_filled(dev, dma_q, buf);
356 get_next_buf(dma_q, &buf);
357 if (buf == NULL)
358 outp = NULL;
359 else
360 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300361 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300362
363 if (buf != NULL) {
364 if (p[2] & 1)
365 buf->top_field = 0;
366 else
367 buf->top_field = 1;
368 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300369
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300371 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300372 if (buf != NULL)
373 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300374 }
375 return rc;
376}
377
378/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300379 Videobuf operations
380 ------------------------------------------------------------------*/
381
382static int
383buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
384{
385 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300386 struct em28xx *dev = fh->dev;
387 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300388
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300389 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300390 if (0 == *count)
391 *count = EM28XX_DEF_BUF;
392
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300393 if (*count < EM28XX_MIN_BUF)
394 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300395
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300396 /* Ask tuner to go to analog mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300397 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300398 f.frequency = dev->ctl_freq;
399
400 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
401
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300402 return 0;
403}
404
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300405/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300406static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
407{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300408 struct em28xx_fh *fh = vq->priv_data;
409 struct em28xx *dev = fh->dev;
410 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300411 if (in_interrupt())
412 BUG();
413
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300414 /* We used to wait for the buffer to finish here, but this didn't work
415 because, as we were keeping the state as VIDEOBUF_QUEUED,
416 videobuf_queue_cancel marked it as finished for us.
417 (Also, it could wedge forever if the hardware was misconfigured.)
418
419 This should be safe; by the time we get here, the buffer isn't
420 queued anymore. If we ever start marking the buffers as
421 VIDEOBUF_ACTIVE, it won't be, though.
422 */
423 spin_lock_irqsave(&dev->slock, flags);
424 if (dev->isoc_ctl.buf == buf)
425 dev->isoc_ctl.buf = NULL;
426 spin_unlock_irqrestore(&dev->slock, flags);
427
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300428 videobuf_vmalloc_free(&buf->vb);
429 buf->vb.state = VIDEOBUF_NEEDS_INIT;
430}
431
432static int
433buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
434 enum v4l2_field field)
435{
436 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300437 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438 struct em28xx *dev = fh->dev;
439 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441 /* FIXME: It assumes depth = 16 */
442 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300443 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
445 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
446 return -EINVAL;
447
Brandon Philips05612972008-04-13 14:57:01 -0300448 buf->vb.width = dev->width;
449 buf->vb.height = dev->height;
450 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300451
452 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300453 rc = videobuf_iolock(vq, &buf->vb, NULL);
454 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 }
457
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300458 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300459 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300460
461 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300462 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
463 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300464 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300465 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 goto fail;
467 }
468
469 buf->vb.state = VIDEOBUF_PREPARED;
470 return 0;
471
472fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300473 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300474 return rc;
475}
476
477static void
478buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
479{
480 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
481 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300482 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300485 buf->vb.state = VIDEOBUF_QUEUED;
486 list_add_tail(&buf->vb.queue, &vidq->active);
487
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300488}
489
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300490static void buffer_release(struct videobuf_queue *vq,
491 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300492{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300493 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300495 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300497 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300498
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300499 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300500}
501
502static struct videobuf_queue_ops em28xx_video_qops = {
503 .buf_setup = buffer_setup,
504 .buf_prepare = buffer_prepare,
505 .buf_queue = buffer_queue,
506 .buf_release = buffer_release,
507};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800508
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300509/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800510
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800512 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800513 * inits registers with sane defaults
514 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800515static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800516{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300517 int retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800518
519 /* Sets I2C speed to 100 KHz */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300520 if (!dev->is_em2800) {
521 retval = em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
522 if (retval < 0) {
523 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
524 __func__, retval);
525 return retval;
526 }
527 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800528
529 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200530
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300531/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
532/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
533 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200534
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800535 dev->mute = 1; /* maybe not the right place... */
536 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300537
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800538 em28xx_outfmt_set_yuv422(dev);
539 em28xx_colorlevels_set_default(dev);
540 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800541
542 return 0;
543}
544
545/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800546 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800547 * configure i2c attached devices
548 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800549static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800550{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300551 struct v4l2_routing route;
552
553 route.input = INPUT(dev->ctl_input)->vmux;
554 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100555 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300556 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300557 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800558}
559
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800560static void video_mux(struct em28xx *dev, int index)
561{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300562 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800563
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300564 route.input = INPUT(index)->vmux;
565 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800566 dev->ctl_input = index;
567 dev->ctl_ainput = INPUT(index)->amux;
568
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300569 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800570
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800571 if (dev->has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300572 if (dev->i2s_speed) {
573 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
574 &dev->i2s_speed);
575 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300576 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300577 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300578 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300579 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
580 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800581 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300582
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300583 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800584}
585
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300586/* Usage lock check functions */
587static int res_get(struct em28xx_fh *fh)
588{
589 struct em28xx *dev = fh->dev;
590 int rc = 0;
591
592 /* This instance already has stream_on */
593 if (fh->stream_on)
594 return rc;
595
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300596 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300597 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300598
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300599 mutex_lock(&dev->lock);
600 dev->stream_on = 1;
601 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300602 mutex_unlock(&dev->lock);
603 return rc;
604}
605
606static int res_check(struct em28xx_fh *fh)
607{
608 return (fh->stream_on);
609}
610
611static void res_free(struct em28xx_fh *fh)
612{
613 struct em28xx *dev = fh->dev;
614
615 mutex_lock(&dev->lock);
616 fh->stream_on = 0;
617 dev->stream_on = 0;
618 mutex_unlock(&dev->lock);
619}
620
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800621/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300622 * em28xx_get_ctrl()
623 * return the current saturation, brightness or contrast, mute state
624 */
625static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
626{
627 switch (ctrl->id) {
628 case V4L2_CID_AUDIO_MUTE:
629 ctrl->value = dev->mute;
630 return 0;
631 case V4L2_CID_AUDIO_VOLUME:
632 ctrl->value = dev->volume;
633 return 0;
634 default:
635 return -EINVAL;
636 }
637}
638
639/*
640 * em28xx_set_ctrl()
641 * mute or set new saturation, brightness or contrast
642 */
643static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
644{
645 switch (ctrl->id) {
646 case V4L2_CID_AUDIO_MUTE:
647 if (ctrl->value != dev->mute) {
648 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300649 return em28xx_audio_analog_set(dev);
650 }
651 return 0;
652 case V4L2_CID_AUDIO_VOLUME:
653 dev->volume = ctrl->value;
654 return em28xx_audio_analog_set(dev);
655 default:
656 return -EINVAL;
657 }
658}
659
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300660static int check_dev(struct em28xx *dev)
661{
662 if (dev->state & DEV_DISCONNECTED) {
663 em28xx_errdev("v4l2 ioctl: device not present\n");
664 return -ENODEV;
665 }
666
667 if (dev->state & DEV_MISCONFIGURED) {
668 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
669 "close and open it again\n");
670 return -EIO;
671 }
672 return 0;
673}
674
675static void get_scale(struct em28xx *dev,
676 unsigned int width, unsigned int height,
677 unsigned int *hscale, unsigned int *vscale)
678{
679 unsigned int maxw = norm_maxw(dev);
680 unsigned int maxh = norm_maxh(dev);
681
682 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
683 if (*hscale >= 0x4000)
684 *hscale = 0x3fff;
685
686 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
687 if (*vscale >= 0x4000)
688 *vscale = 0x3fff;
689}
690
691/* ------------------------------------------------------------------
692 IOCTL vidioc handling
693 ------------------------------------------------------------------*/
694
Hans Verkuil78b526a2008-05-28 12:16:41 -0300695static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300696 struct v4l2_format *f)
697{
698 struct em28xx_fh *fh = priv;
699 struct em28xx *dev = fh->dev;
700
701 mutex_lock(&dev->lock);
702
703 f->fmt.pix.width = dev->width;
704 f->fmt.pix.height = dev->height;
705 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300706 f->fmt.pix.bytesperline = dev->width * 2;
707 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300708 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
709
710 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
711 f->fmt.pix.field = dev->interlaced ?
712 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
713
714 mutex_unlock(&dev->lock);
715 return 0;
716}
717
Hans Verkuil78b526a2008-05-28 12:16:41 -0300718static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300719 struct v4l2_format *f)
720{
721 struct em28xx_fh *fh = priv;
722 struct em28xx *dev = fh->dev;
723 int width = f->fmt.pix.width;
724 int height = f->fmt.pix.height;
725 unsigned int maxw = norm_maxw(dev);
726 unsigned int maxh = norm_maxh(dev);
727 unsigned int hscale, vscale;
728
729 /* width must even because of the YUYV format
730 height must be even because of interlacing */
731 height &= 0xfffe;
732 width &= 0xfffe;
733
734 if (height < 32)
735 height = 32;
736 if (height > maxh)
737 height = maxh;
738 if (width < 48)
739 width = 48;
740 if (width > maxw)
741 width = maxw;
742
743 mutex_lock(&dev->lock);
744
745 if (dev->is_em2800) {
746 /* the em2800 can only scale down to 50% */
747 if (height % (maxh / 2))
748 height = maxh;
749 if (width % (maxw / 2))
750 width = maxw;
751 /* according to empiatech support */
752 /* the MaxPacketSize is to small to support */
753 /* framesizes larger than 640x480 @ 30 fps */
754 /* or 640x576 @ 25 fps. As this would cut */
755 /* of a part of the image we prefer */
756 /* 360x576 or 360x480 for now */
757 if (width == maxw && height == maxh)
758 width /= 2;
759 }
760
761 get_scale(dev, width, height, &hscale, &vscale);
762
763 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
764 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
765
766 f->fmt.pix.width = width;
767 f->fmt.pix.height = height;
768 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
769 f->fmt.pix.bytesperline = width * 2;
770 f->fmt.pix.sizeimage = width * 2 * height;
771 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
772 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
773
774 mutex_unlock(&dev->lock);
775 return 0;
776}
777
Hans Verkuil78b526a2008-05-28 12:16:41 -0300778static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300779 struct v4l2_format *f)
780{
781 struct em28xx_fh *fh = priv;
782 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300783 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300784
785 rc = check_dev(dev);
786 if (rc < 0)
787 return rc;
788
Hans Verkuil78b526a2008-05-28 12:16:41 -0300789 vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300790
791 mutex_lock(&dev->lock);
792
Brandon Philips05612972008-04-13 14:57:01 -0300793 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
794 em28xx_errdev("%s queue busy\n", __func__);
795 rc = -EBUSY;
796 goto out;
797 }
798
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300799 if (dev->stream_on && !fh->stream_on) {
800 em28xx_errdev("%s device in use by another fh\n", __func__);
801 rc = -EBUSY;
802 goto out;
803 }
804
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300805 /* set new image size */
806 dev->width = f->fmt.pix.width;
807 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
809
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300810 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812
Brandon Philips05612972008-04-13 14:57:01 -0300813 rc = 0;
814
815out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300816 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300817 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300818}
819
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300820static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300821{
822 struct em28xx_fh *fh = priv;
823 struct em28xx *dev = fh->dev;
824 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300825 int rc;
826
827 rc = check_dev(dev);
828 if (rc < 0)
829 return rc;
830
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300831 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300832 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300833 mutex_unlock(&dev->lock);
834
835 /* Adjusts width/height, if needed */
836 f.fmt.pix.width = dev->width;
837 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300838 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300839
840 mutex_lock(&dev->lock);
841
842 /* set new image size */
843 dev->width = f.fmt.pix.width;
844 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300845 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
846
847 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300848 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300849
850 mutex_unlock(&dev->lock);
851 return 0;
852}
853
854static const char *iname[] = {
855 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
856 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
857 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
858 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
859 [EM28XX_VMUX_SVIDEO] = "S-Video",
860 [EM28XX_VMUX_TELEVISION] = "Television",
861 [EM28XX_VMUX_CABLE] = "Cable TV",
862 [EM28XX_VMUX_DVB] = "DVB",
863 [EM28XX_VMUX_DEBUG] = "for debug only",
864};
865
866static int vidioc_enum_input(struct file *file, void *priv,
867 struct v4l2_input *i)
868{
869 struct em28xx_fh *fh = priv;
870 struct em28xx *dev = fh->dev;
871 unsigned int n;
872
873 n = i->index;
874 if (n >= MAX_EM28XX_INPUT)
875 return -EINVAL;
876 if (0 == INPUT(n)->type)
877 return -EINVAL;
878
879 i->index = n;
880 i->type = V4L2_INPUT_TYPE_CAMERA;
881
882 strcpy(i->name, iname[INPUT(n)->type]);
883
884 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
885 (EM28XX_VMUX_CABLE == INPUT(n)->type))
886 i->type = V4L2_INPUT_TYPE_TUNER;
887
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300888 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300889
890 return 0;
891}
892
893static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
894{
895 struct em28xx_fh *fh = priv;
896 struct em28xx *dev = fh->dev;
897
898 *i = dev->ctl_input;
899
900 return 0;
901}
902
903static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
904{
905 struct em28xx_fh *fh = priv;
906 struct em28xx *dev = fh->dev;
907 int rc;
908
909 rc = check_dev(dev);
910 if (rc < 0)
911 return rc;
912
913 if (i >= MAX_EM28XX_INPUT)
914 return -EINVAL;
915 if (0 == INPUT(i)->type)
916 return -EINVAL;
917
918 mutex_lock(&dev->lock);
919
920 video_mux(dev, i);
921
922 mutex_unlock(&dev->lock);
923 return 0;
924}
925
926static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
927{
928 struct em28xx_fh *fh = priv;
929 struct em28xx *dev = fh->dev;
930 unsigned int index = a->index;
931
932 if (a->index > 1)
933 return -EINVAL;
934
935 index = dev->ctl_ainput;
936
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300937 if (index == 0)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300938 strcpy(a->name, "Television");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300939 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300940 strcpy(a->name, "Line In");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300941
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300942 a->capability = V4L2_AUDCAP_STEREO;
943 a->index = index;
944
945 return 0;
946}
947
948static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
949{
950 struct em28xx_fh *fh = priv;
951 struct em28xx *dev = fh->dev;
952
953 if (a->index != dev->ctl_ainput)
954 return -EINVAL;
955
956 return 0;
957}
958
959static int vidioc_queryctrl(struct file *file, void *priv,
960 struct v4l2_queryctrl *qc)
961{
962 struct em28xx_fh *fh = priv;
963 struct em28xx *dev = fh->dev;
964 int id = qc->id;
965 int i;
966 int rc;
967
968 rc = check_dev(dev);
969 if (rc < 0)
970 return rc;
971
972 memset(qc, 0, sizeof(*qc));
973
974 qc->id = id;
975
976 if (!dev->has_msp34xx) {
977 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
978 if (qc->id && qc->id == em28xx_qctrl[i].id) {
979 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
980 return 0;
981 }
982 }
983 }
984 mutex_lock(&dev->lock);
985 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
986 mutex_unlock(&dev->lock);
987
988 if (qc->type)
989 return 0;
990 else
991 return -EINVAL;
992}
993
994static int vidioc_g_ctrl(struct file *file, void *priv,
995 struct v4l2_control *ctrl)
996{
997 struct em28xx_fh *fh = priv;
998 struct em28xx *dev = fh->dev;
999 int rc;
1000
1001 rc = check_dev(dev);
1002 if (rc < 0)
1003 return rc;
1004 mutex_lock(&dev->lock);
1005
1006 if (!dev->has_msp34xx)
1007 rc = em28xx_get_ctrl(dev, ctrl);
1008 else
1009 rc = -EINVAL;
1010
1011 if (rc == -EINVAL) {
1012 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1013 rc = 0;
1014 }
1015
1016 mutex_unlock(&dev->lock);
1017 return rc;
1018}
1019
1020static int vidioc_s_ctrl(struct file *file, void *priv,
1021 struct v4l2_control *ctrl)
1022{
1023 struct em28xx_fh *fh = priv;
1024 struct em28xx *dev = fh->dev;
1025 u8 i;
1026 int rc;
1027
1028 rc = check_dev(dev);
1029 if (rc < 0)
1030 return rc;
1031
1032 mutex_lock(&dev->lock);
1033
1034 if (dev->has_msp34xx)
1035 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1036 else {
1037 rc = 1;
1038 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1039 if (ctrl->id == em28xx_qctrl[i].id) {
1040 if (ctrl->value < em28xx_qctrl[i].minimum ||
1041 ctrl->value > em28xx_qctrl[i].maximum) {
1042 rc = -ERANGE;
1043 break;
1044 }
1045
1046 rc = em28xx_set_ctrl(dev, ctrl);
1047 break;
1048 }
1049 }
1050 }
1051
1052 /* Control not found - try to send it to the attached devices */
1053 if (rc == 1) {
1054 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1055 rc = 0;
1056 }
1057
1058 mutex_unlock(&dev->lock);
1059 return rc;
1060}
1061
1062static int vidioc_g_tuner(struct file *file, void *priv,
1063 struct v4l2_tuner *t)
1064{
1065 struct em28xx_fh *fh = priv;
1066 struct em28xx *dev = fh->dev;
1067 int rc;
1068
1069 rc = check_dev(dev);
1070 if (rc < 0)
1071 return rc;
1072
1073 if (0 != t->index)
1074 return -EINVAL;
1075
1076 strcpy(t->name, "Tuner");
1077
1078 mutex_lock(&dev->lock);
1079
1080 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1081
1082 mutex_unlock(&dev->lock);
1083 return 0;
1084}
1085
1086static int vidioc_s_tuner(struct file *file, void *priv,
1087 struct v4l2_tuner *t)
1088{
1089 struct em28xx_fh *fh = priv;
1090 struct em28xx *dev = fh->dev;
1091 int rc;
1092
1093 rc = check_dev(dev);
1094 if (rc < 0)
1095 return rc;
1096
1097 if (0 != t->index)
1098 return -EINVAL;
1099
1100 mutex_lock(&dev->lock);
1101
1102 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1103
1104 mutex_unlock(&dev->lock);
1105 return 0;
1106}
1107
1108static int vidioc_g_frequency(struct file *file, void *priv,
1109 struct v4l2_frequency *f)
1110{
1111 struct em28xx_fh *fh = priv;
1112 struct em28xx *dev = fh->dev;
1113
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001114 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001115 f->frequency = dev->ctl_freq;
1116
1117 return 0;
1118}
1119
1120static int vidioc_s_frequency(struct file *file, void *priv,
1121 struct v4l2_frequency *f)
1122{
1123 struct em28xx_fh *fh = priv;
1124 struct em28xx *dev = fh->dev;
1125 int rc;
1126
1127 rc = check_dev(dev);
1128 if (rc < 0)
1129 return rc;
1130
1131 if (0 != f->tuner)
1132 return -EINVAL;
1133
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001134 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1135 return -EINVAL;
1136 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001137 return -EINVAL;
1138
1139 mutex_lock(&dev->lock);
1140
1141 dev->ctl_freq = f->frequency;
1142 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1143
1144 mutex_unlock(&dev->lock);
1145 return 0;
1146}
1147
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001148#ifdef CONFIG_VIDEO_ADV_DEBUG
1149static int em28xx_reg_len(int reg)
1150{
1151 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001152 case EM28XX_R40_AC97LSB:
1153 case EM28XX_R30_HSCALELOW:
1154 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001155 return 2;
1156 default:
1157 return 1;
1158 }
1159}
1160
1161static int vidioc_g_register(struct file *file, void *priv,
1162 struct v4l2_register *reg)
1163{
1164 struct em28xx_fh *fh = priv;
1165 struct em28xx *dev = fh->dev;
1166 int ret;
1167
1168 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1169 return -EINVAL;
1170
1171 if (em28xx_reg_len(reg->reg) == 1) {
1172 ret = em28xx_read_reg(dev, reg->reg);
1173 if (ret < 0)
1174 return ret;
1175
1176 reg->val = ret;
1177 } else {
Al Viroa954b662008-05-21 00:32:51 -03001178 __le64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001179 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1180 reg->reg, (char *)&val, 2);
1181 if (ret < 0)
1182 return ret;
1183
Al Viroa954b662008-05-21 00:32:51 -03001184 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001185 }
1186
1187 return 0;
1188}
1189
1190static int vidioc_s_register(struct file *file, void *priv,
1191 struct v4l2_register *reg)
1192{
1193 struct em28xx_fh *fh = priv;
1194 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001195 __le64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001196
Al Viroa954b662008-05-21 00:32:51 -03001197 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001198
1199 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1200 em28xx_reg_len(reg->reg));
1201}
1202#endif
1203
1204
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001205static int vidioc_cropcap(struct file *file, void *priv,
1206 struct v4l2_cropcap *cc)
1207{
1208 struct em28xx_fh *fh = priv;
1209 struct em28xx *dev = fh->dev;
1210
1211 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1212 return -EINVAL;
1213
1214 cc->bounds.left = 0;
1215 cc->bounds.top = 0;
1216 cc->bounds.width = dev->width;
1217 cc->bounds.height = dev->height;
1218 cc->defrect = cc->bounds;
1219 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1220 cc->pixelaspect.denominator = 59;
1221
1222 return 0;
1223}
1224
1225static int vidioc_streamon(struct file *file, void *priv,
1226 enum v4l2_buf_type type)
1227{
1228 struct em28xx_fh *fh = priv;
1229 struct em28xx *dev = fh->dev;
1230 int rc;
1231
1232 rc = check_dev(dev);
1233 if (rc < 0)
1234 return rc;
1235
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001236
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001237 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001238 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001239
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001240 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001241}
1242
1243static int vidioc_streamoff(struct file *file, void *priv,
1244 enum v4l2_buf_type type)
1245{
1246 struct em28xx_fh *fh = priv;
1247 struct em28xx *dev = fh->dev;
1248 int rc;
1249
1250 rc = check_dev(dev);
1251 if (rc < 0)
1252 return rc;
1253
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001254 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1255 return -EINVAL;
1256 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001257 return -EINVAL;
1258
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001259 videobuf_streamoff(&fh->vb_vidq);
1260 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001261
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001262 return 0;
1263}
1264
1265static int vidioc_querycap(struct file *file, void *priv,
1266 struct v4l2_capability *cap)
1267{
1268 struct em28xx_fh *fh = priv;
1269 struct em28xx *dev = fh->dev;
1270
1271 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1272 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1273 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1274
1275 cap->version = EM28XX_VERSION_CODE;
1276
1277 cap->capabilities =
1278 V4L2_CAP_SLICED_VBI_CAPTURE |
1279 V4L2_CAP_VIDEO_CAPTURE |
1280 V4L2_CAP_AUDIO |
1281 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1282
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001283 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001284 cap->capabilities |= V4L2_CAP_TUNER;
1285
1286 return 0;
1287}
1288
Hans Verkuil78b526a2008-05-28 12:16:41 -03001289static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290 struct v4l2_fmtdesc *fmtd)
1291{
1292 if (fmtd->index != 0)
1293 return -EINVAL;
1294
1295 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1296 strcpy(fmtd->description, "Packed YUY2");
1297 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1298 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1299
1300 return 0;
1301}
1302
1303/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001304static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001305 struct v4l2_format *f)
1306{
1307 struct em28xx_fh *fh = priv;
1308 struct em28xx *dev = fh->dev;
1309 int rc;
1310
1311 rc = check_dev(dev);
1312 if (rc < 0)
1313 return rc;
1314
1315 mutex_lock(&dev->lock);
1316
1317 f->fmt.sliced.service_set = 0;
1318
1319 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1320
1321 if (f->fmt.sliced.service_set == 0)
1322 rc = -EINVAL;
1323
1324 mutex_unlock(&dev->lock);
1325 return rc;
1326}
1327
Hans Verkuil78b526a2008-05-28 12:16:41 -03001328static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001329 struct v4l2_format *f)
1330{
1331 struct em28xx_fh *fh = priv;
1332 struct em28xx *dev = fh->dev;
1333 int rc;
1334
1335 rc = check_dev(dev);
1336 if (rc < 0)
1337 return rc;
1338
1339 mutex_lock(&dev->lock);
1340 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1341 mutex_unlock(&dev->lock);
1342
1343 if (f->fmt.sliced.service_set == 0)
1344 return -EINVAL;
1345
1346 return 0;
1347}
1348
1349
1350static int vidioc_reqbufs(struct file *file, void *priv,
1351 struct v4l2_requestbuffers *rb)
1352{
1353 struct em28xx_fh *fh = priv;
1354 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001355 int rc;
1356
1357 rc = check_dev(dev);
1358 if (rc < 0)
1359 return rc;
1360
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001361 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001362}
1363
1364static int vidioc_querybuf(struct file *file, void *priv,
1365 struct v4l2_buffer *b)
1366{
1367 struct em28xx_fh *fh = priv;
1368 struct em28xx *dev = fh->dev;
1369 int rc;
1370
1371 rc = check_dev(dev);
1372 if (rc < 0)
1373 return rc;
1374
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001375 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001376}
1377
1378static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1379{
1380 struct em28xx_fh *fh = priv;
1381 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 int rc;
1383
1384 rc = check_dev(dev);
1385 if (rc < 0)
1386 return rc;
1387
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001388 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389}
1390
1391static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1392{
1393 struct em28xx_fh *fh = priv;
1394 struct em28xx *dev = fh->dev;
1395 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001396
1397 rc = check_dev(dev);
1398 if (rc < 0)
1399 return rc;
1400
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001401 return (videobuf_dqbuf(&fh->vb_vidq, b,
1402 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001403}
1404
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001405#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001406static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001407{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001408 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001409
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001410 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001411}
1412#endif
1413
1414
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001415/* ----------------------------------------------------------- */
1416/* RADIO ESPECIFIC IOCTLS */
1417/* ----------------------------------------------------------- */
1418
1419static int radio_querycap(struct file *file, void *priv,
1420 struct v4l2_capability *cap)
1421{
1422 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1423
1424 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1425 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1426 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1427
1428 cap->version = EM28XX_VERSION_CODE;
1429 cap->capabilities = V4L2_CAP_TUNER;
1430 return 0;
1431}
1432
1433static int radio_g_tuner(struct file *file, void *priv,
1434 struct v4l2_tuner *t)
1435{
1436 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1437
1438 if (unlikely(t->index > 0))
1439 return -EINVAL;
1440
1441 strcpy(t->name, "Radio");
1442 t->type = V4L2_TUNER_RADIO;
1443
1444 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1445 return 0;
1446}
1447
1448static int radio_enum_input(struct file *file, void *priv,
1449 struct v4l2_input *i)
1450{
1451 if (i->index != 0)
1452 return -EINVAL;
1453 strcpy(i->name, "Radio");
1454 i->type = V4L2_INPUT_TYPE_TUNER;
1455
1456 return 0;
1457}
1458
1459static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1460{
1461 if (unlikely(a->index))
1462 return -EINVAL;
1463
1464 strcpy(a->name, "Radio");
1465 return 0;
1466}
1467
1468static int radio_s_tuner(struct file *file, void *priv,
1469 struct v4l2_tuner *t)
1470{
1471 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1472
1473 if (0 != t->index)
1474 return -EINVAL;
1475
1476 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1477
1478 return 0;
1479}
1480
1481static int radio_s_audio(struct file *file, void *fh,
1482 struct v4l2_audio *a)
1483{
1484 return 0;
1485}
1486
1487static int radio_s_input(struct file *file, void *fh, unsigned int i)
1488{
1489 return 0;
1490}
1491
1492static int radio_queryctrl(struct file *file, void *priv,
1493 struct v4l2_queryctrl *qc)
1494{
1495 int i;
1496
1497 if (qc->id < V4L2_CID_BASE ||
1498 qc->id >= V4L2_CID_LASTP1)
1499 return -EINVAL;
1500
1501 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1502 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1503 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1504 return 0;
1505 }
1506 }
1507
1508 return -EINVAL;
1509}
1510
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001511/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001512 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001513 * inits the device and starts isoc transfer
1514 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001515static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001516{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001517 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001518 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001519 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001520 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001521 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001522
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001523 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001524 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001525 if (h->vdev->minor == minor) {
1526 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001527 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001528 }
1529 if (h->vbi_dev->minor == minor) {
1530 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001531 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001532 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001533 if (h->radio_dev &&
1534 h->radio_dev->minor == minor) {
1535 radio = 1;
1536 dev = h;
1537 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001538 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001539 mutex_unlock(&em28xx_devlist_mutex);
1540 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001541 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001542
1543 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001544
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001545 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001546 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001547
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001548
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001549 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001550 if (!fh) {
1551 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001552 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001553 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001554 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001555 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001556 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001557 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001558 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001559
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001560 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001561 dev->width = norm_maxw(dev);
1562 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001563 dev->hscale = 0;
1564 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001565
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001566 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001567 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001568 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001569
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001570 /* Needed, since GPIO might have disabled power of
1571 some i2c device
1572 */
1573 em28xx_config_i2c(dev);
1574
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001575 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001576 if (fh->radio) {
1577 em28xx_videodbg("video_open: setting radio device\n");
1578 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1579 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001580
1581 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001582
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001583 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1584 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1585 sizeof(struct em28xx_buffer), fh);
1586
Ingo Molnar3593cab2006-02-07 06:49:14 -02001587 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001588
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001589 return errCode;
1590}
1591
1592/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001593 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001594 * unregisters the v4l2,i2c and usb devices
1595 * called when the device gets disconected or at module unload
1596*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001597static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001598{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001599
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001600 /*FIXME: I2C IR should be disconnected */
1601
1602 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
Hans Verkuildd896012008-10-04 08:36:54 -03001603 dev->vdev->num, dev->vbi_dev->num);
Markus Rechberger9c755412005-11-08 21:37:52 -08001604 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001605 if (dev->sbutton_input_dev)
1606 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001607 if (dev->radio_dev) {
1608 if (-1 != dev->radio_dev->minor)
1609 video_unregister_device(dev->radio_dev);
1610 else
1611 video_device_release(dev->radio_dev);
1612 dev->radio_dev = NULL;
1613 }
1614 if (dev->vbi_dev) {
1615 if (-1 != dev->vbi_dev->minor)
1616 video_unregister_device(dev->vbi_dev);
1617 else
1618 video_device_release(dev->vbi_dev);
1619 dev->vbi_dev = NULL;
1620 }
1621 if (dev->vdev) {
1622 if (-1 != dev->vdev->minor)
1623 video_unregister_device(dev->vdev);
1624 else
1625 video_device_release(dev->vdev);
1626 dev->vdev = NULL;
1627 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001628 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001629 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001630
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001631 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001632 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001633}
1634
1635/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001636 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001637 * stops streaming and deallocates all resources allocated by the v4l2
1638 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001639 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001640static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001641{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001642 struct em28xx_fh *fh = filp->private_data;
1643 struct em28xx *dev = fh->dev;
1644 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001645
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001646 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001647
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001648
1649 if (res_check(fh))
1650 res_free(fh);
1651
Ingo Molnar3593cab2006-02-07 06:49:14 -02001652 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001653
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001654 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001655 videobuf_stop(&fh->vb_vidq);
1656 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001657
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001658 /* the device is already disconnect,
1659 free the remaining resources */
1660 if (dev->state & DEV_DISCONNECTED) {
1661 em28xx_release_resources(dev);
1662 mutex_unlock(&dev->lock);
1663 kfree(dev);
1664 return 0;
1665 }
1666
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001667 /* do this before setting alternate! */
1668 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001669 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001670
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001671 /* set alternate 0 */
1672 dev->alt = 0;
1673 em28xx_videodbg("setting alternate 0\n");
1674 errCode = usb_set_interface(dev->udev, 0, 0);
1675 if (errCode < 0) {
1676 em28xx_errdev("cannot change alternate number to "
1677 "0 (error=%i)\n", errCode);
1678 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001679 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001680 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001681 dev->users--;
1682 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001683 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001684 return 0;
1685}
1686
1687/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001688 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001689 * will allocate buffers when called for the first time
1690 */
1691static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001692em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001693 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001694{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001695 struct em28xx_fh *fh = filp->private_data;
1696 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001697 int rc;
1698
1699 rc = check_dev(dev);
1700 if (rc < 0)
1701 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001702
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001703 /* FIXME: read() is not prepared to allow changing the video
1704 resolution while streaming. Seems a bug at em28xx_set_fmt
1705 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001706
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001707 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1708 if (unlikely(res_get(fh)))
1709 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001710
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001711 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1712 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001713 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001714 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001715}
1716
1717/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001718 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001719 * will allocate buffers when called for the first time
1720 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001721static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001723 struct em28xx_fh *fh = filp->private_data;
1724 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001725 int rc;
1726
1727 rc = check_dev(dev);
1728 if (rc < 0)
1729 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001731 if (unlikely(res_get(fh) < 0))
1732 return POLLERR;
1733
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001734 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1735 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001737 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738}
1739
1740/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001741 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001742 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001743static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001744{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001745 struct em28xx_fh *fh = filp->private_data;
1746 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001747 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001748
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001749 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001750 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001751
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001752 rc = check_dev(dev);
1753 if (rc < 0)
1754 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001755
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001756 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001757
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001758 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1759 (unsigned long)vma->vm_start,
1760 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1761 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001762
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001763 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001764}
1765
Arjan van de Venfa027c22007-02-12 00:55:33 -08001766static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001767 .owner = THIS_MODULE,
1768 .open = em28xx_v4l2_open,
1769 .release = em28xx_v4l2_close,
1770 .read = em28xx_v4l2_read,
1771 .poll = em28xx_v4l2_poll,
1772 .mmap = em28xx_v4l2_mmap,
1773 .ioctl = video_ioctl2,
1774 .llseek = no_llseek,
1775 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001776};
1777
Hans Verkuila3998102008-07-21 02:57:38 -03001778static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001779 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001780 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1781 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1782 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1783 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001784 .vidioc_g_audio = vidioc_g_audio,
1785 .vidioc_s_audio = vidioc_s_audio,
1786 .vidioc_cropcap = vidioc_cropcap,
1787
Hans Verkuil78b526a2008-05-28 12:16:41 -03001788 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1789 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1790 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001791
1792 .vidioc_reqbufs = vidioc_reqbufs,
1793 .vidioc_querybuf = vidioc_querybuf,
1794 .vidioc_qbuf = vidioc_qbuf,
1795 .vidioc_dqbuf = vidioc_dqbuf,
1796 .vidioc_s_std = vidioc_s_std,
1797 .vidioc_enum_input = vidioc_enum_input,
1798 .vidioc_g_input = vidioc_g_input,
1799 .vidioc_s_input = vidioc_s_input,
1800 .vidioc_queryctrl = vidioc_queryctrl,
1801 .vidioc_g_ctrl = vidioc_g_ctrl,
1802 .vidioc_s_ctrl = vidioc_s_ctrl,
1803 .vidioc_streamon = vidioc_streamon,
1804 .vidioc_streamoff = vidioc_streamoff,
1805 .vidioc_g_tuner = vidioc_g_tuner,
1806 .vidioc_s_tuner = vidioc_s_tuner,
1807 .vidioc_g_frequency = vidioc_g_frequency,
1808 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001809#ifdef CONFIG_VIDEO_ADV_DEBUG
1810 .vidioc_g_register = vidioc_g_register,
1811 .vidioc_s_register = vidioc_s_register,
1812#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001813#ifdef CONFIG_VIDEO_V4L1_COMPAT
1814 .vidiocgmbuf = vidiocgmbuf,
1815#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001816};
1817
1818static const struct video_device em28xx_video_template = {
1819 .fops = &em28xx_v4l_fops,
1820 .release = video_device_release,
1821 .ioctl_ops = &video_ioctl_ops,
1822
1823 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001824
1825 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001826 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001827};
1828
Hans Verkuila3998102008-07-21 02:57:38 -03001829static const struct file_operations radio_fops = {
1830 .owner = THIS_MODULE,
1831 .open = em28xx_v4l2_open,
1832 .release = em28xx_v4l2_close,
1833 .ioctl = video_ioctl2,
1834 .compat_ioctl = v4l_compat_ioctl32,
1835 .llseek = no_llseek,
1836};
1837
1838static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001839 .vidioc_querycap = radio_querycap,
1840 .vidioc_g_tuner = radio_g_tuner,
1841 .vidioc_enum_input = radio_enum_input,
1842 .vidioc_g_audio = radio_g_audio,
1843 .vidioc_s_tuner = radio_s_tuner,
1844 .vidioc_s_audio = radio_s_audio,
1845 .vidioc_s_input = radio_s_input,
1846 .vidioc_queryctrl = radio_queryctrl,
1847 .vidioc_g_ctrl = vidioc_g_ctrl,
1848 .vidioc_s_ctrl = vidioc_s_ctrl,
1849 .vidioc_g_frequency = vidioc_g_frequency,
1850 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001851#ifdef CONFIG_VIDEO_ADV_DEBUG
1852 .vidioc_g_register = vidioc_g_register,
1853 .vidioc_s_register = vidioc_s_register,
1854#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001855};
1856
Hans Verkuila3998102008-07-21 02:57:38 -03001857static struct video_device em28xx_radio_template = {
1858 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001859 .fops = &radio_fops,
1860 .ioctl_ops = &radio_ioctl_ops,
1861 .minor = -1,
1862};
1863
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001864/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001865
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001866
1867static LIST_HEAD(em28xx_extension_devlist);
1868static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1869
1870int em28xx_register_extension(struct em28xx_ops *ops)
1871{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001872 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001873
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001874 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001875 mutex_lock(&em28xx_extension_devlist_lock);
1876 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001877 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1878 if (dev)
1879 ops->init(dev);
1880 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001881 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1882 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001883 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001884 return 0;
1885}
1886EXPORT_SYMBOL(em28xx_register_extension);
1887
1888void em28xx_unregister_extension(struct em28xx_ops *ops)
1889{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001890 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001891
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001892 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001893 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1894 if (dev)
1895 ops->fini(dev);
1896 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001897
1898 mutex_lock(&em28xx_extension_devlist_lock);
1899 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1900 list_del(&ops->next);
1901 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001902 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001903}
1904EXPORT_SYMBOL(em28xx_unregister_extension);
1905
Adrian Bunk532fe652008-01-28 22:10:48 -03001906static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1907 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03001908 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001909{
1910 struct video_device *vfd;
1911
1912 vfd = video_device_alloc();
1913 if (NULL == vfd)
1914 return NULL;
1915 *vfd = *template;
1916 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001917 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001918 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001919 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001920
1921 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1922 dev->name, type_name);
1923
1924 return vfd;
1925}
1926
1927
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001928static int register_analog_devices(struct em28xx *dev)
1929{
1930 int ret;
1931
1932 /* allocate and fill video video_device struct */
1933 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1934 if (!dev->vdev) {
1935 em28xx_errdev("cannot allocate video_device.\n");
1936 return -ENODEV;
1937 }
1938
1939 /* register v4l2 video video_device */
1940 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1941 video_nr[dev->devno]);
1942 if (ret) {
1943 em28xx_errdev("unable to register video device (error=%i).\n",
1944 ret);
1945 return ret;
1946 }
1947
1948 /* Allocate and fill vbi video_device struct */
1949 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1950
1951 /* register v4l2 vbi video_device */
1952 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1953 vbi_nr[dev->devno]);
1954 if (ret < 0) {
1955 em28xx_errdev("unable to register vbi device\n");
1956 return ret;
1957 }
1958
1959 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1960 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
1961 if (!dev->radio_dev) {
1962 em28xx_errdev("cannot allocate video_device.\n");
1963 return -ENODEV;
1964 }
1965 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1966 radio_nr[dev->devno]);
1967 if (ret < 0) {
1968 em28xx_errdev("can't register radio device\n");
1969 return ret;
1970 }
1971 em28xx_info("Registered radio device as /dev/radio%d\n",
1972 dev->radio_dev->num);
1973 }
1974
1975 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1976 dev->vdev->num, dev->vbi_dev->num);
1977
1978 return 0;
1979}
1980
1981
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001982/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001983 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001984 * allocates and inits the device structs, registers i2c bus and v4l device
1985 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001986static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001987 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001988{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001989 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001990 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001991 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001992 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001993 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001994
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001995 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001996 mutex_init(&dev->lock);
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03001997 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001998 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001999 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002000 init_waitqueue_head(&dev->wait_frame);
2001 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002002
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002003 dev->em28xx_write_regs = em28xx_write_regs;
2004 dev->em28xx_read_reg = em28xx_read_reg;
2005 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2006 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2007 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002008 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002009
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002010 em28xx_pre_card_setup(dev);
2011
2012 errCode = em28xx_config(dev);
2013 if (errCode) {
2014 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002015 return -ENOMEM;
2016 }
2017
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002018 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002019 errCode = em28xx_i2c_register(dev);
2020 if (errCode < 0) {
2021 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2022 __func__, errCode);
2023 return errCode;
2024 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002025
2026 /* Do board specific init and eeprom reading */
2027 em28xx_card_setup(dev);
2028
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002029 /* Configure audio */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002030 errCode = em28xx_audio_analog_set(dev);
2031 if (errCode < 0) {
2032 em28xx_errdev("%s: em28xx_audio_analog_set - errCode [%d]!\n",
2033 __func__, errCode);
2034 return errCode;
2035 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002036
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002037 /* configure the device */
2038 em28xx_config_i2c(dev);
2039
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002040 /* set default norm */
2041 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002042
2043 maxw = norm_maxw(dev);
2044 maxh = norm_maxh(dev);
2045
2046 /* set default image size */
2047 dev->width = maxw;
2048 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002049 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002050 dev->hscale = 0;
2051 dev->vscale = 0;
2052 dev->ctl_input = 2;
2053
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002054 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002055 if (errCode < 0) {
2056 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2057 __func__, errCode);
2058 return errCode;
2059 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002060
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002061 /* init video dma queues */
2062 INIT_LIST_HEAD(&dev->vidq.active);
2063 INIT_LIST_HEAD(&dev->vidq.queued);
2064
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002065
Sascha Sommer5a804152007-11-03 21:22:38 -03002066 if (dev->has_msp34xx) {
2067 /* Send a reset to other chips via gpio */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002068 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2069 if (errCode < 0) {
2070 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2071 __func__, errCode);
2072 return errCode;
2073 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002074 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002075
2076 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2077 if (errCode < 0) {
2078 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2079 __func__, errCode);
2080 return errCode;
2081 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002082 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002083 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002084
Sascha Sommer5a804152007-11-03 21:22:38 -03002085 video_mux(dev, 0);
2086
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002087 mutex_lock(&em28xx_devlist_mutex);
2088 list_add_tail(&dev->devlist, &em28xx_devlist);
2089 retval = register_analog_devices(dev);
2090 if (retval < 0) {
2091 em28xx_release_resources(dev);
2092 mutex_unlock(&em28xx_devlist_mutex);
2093 goto fail_reg_devices;
2094 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002095
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002096 mutex_lock(&em28xx_extension_devlist_lock);
2097 if (!list_empty(&em28xx_extension_devlist)) {
2098 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2099 if (ops->id)
2100 ops->init(dev);
2101 }
2102 }
2103 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002104 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002105
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002106 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002107
2108fail_unreg:
2109 em28xx_release_resources(dev);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002110fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002111 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002112 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002113}
2114
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002115#if defined(CONFIG_MODULES) && defined(MODULE)
2116static void request_module_async(struct work_struct *work)
2117{
2118 struct em28xx *dev = container_of(work,
2119 struct em28xx, request_module_wk);
2120
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002121 if (dev->has_audio_class)
2122 request_module("snd-usb-audio");
2123 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002124 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002125
2126 if (dev->has_dvb)
2127 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002128}
2129
2130static void request_modules(struct em28xx *dev)
2131{
2132 INIT_WORK(&dev->request_module_wk, request_module_async);
2133 schedule_work(&dev->request_module_wk);
2134}
2135#else
2136#define request_modules(dev)
2137#endif /* CONFIG_MODULES */
2138
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002139/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002140 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002141 * checks for supported devices
2142 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002143static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002144 const struct usb_device_id *id)
2145{
2146 const struct usb_endpoint_descriptor *endpoint;
2147 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002148 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002149 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002150 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002151 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002152
2153 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002154 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2155
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002156 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002157 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2158 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002159
2160 /* Don't register audio interfaces */
2161 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002162 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002163 udev->descriptor.idVendor,
2164 udev->descriptor.idProduct,
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002165 ifnum,
2166 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002167
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002168 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002169 return -ENODEV;
2170 }
2171
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002172 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002173 udev->descriptor.idVendor,
2174 udev->descriptor.idProduct,
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002175 ifnum,
2176 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002177
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002178 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2179
Michael Opdenacker59c51592007-05-09 08:57:56 +02002180 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002181 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2182 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002183 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002184 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002185 return -ENODEV;
2186 }
2187 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002188 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002189 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002190 return -ENODEV;
2191 }
2192
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002193 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002194 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2195 EM28XX_MAXBOARDS);
2196 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002197 return -ENOMEM;
2198 }
2199
2200 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002201 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002202 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002203 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002204 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002205 return -ENOMEM;
2206 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002207
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002208 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002209 dev->devno = nr;
2210 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002211 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002212
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002213 /* Checks if audio is provided by some interface */
2214 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2215 uif = udev->config->interface[i];
2216 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2217 dev->has_audio_class = 1;
2218 break;
2219 }
2220 }
2221
2222 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2223 dev->has_audio_class ? "Has" : "Doesn't have");
2224
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002225 /* compute alternate max packet sizes */
2226 uif = udev->actconfig->interface[0];
2227
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002228 dev->num_alt = uif->num_altsetting;
2229 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2230/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2231 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2232
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002233 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002234 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002235 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002236 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002237 return -ENOMEM;
2238 }
2239
2240 for (i = 0; i < dev->num_alt ; i++) {
2241 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2242 wMaxPacketSize);
2243 dev->alt_max_pkt_size[i] =
2244 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002245 em28xx_info("Alternate setting %i, max size= %i\n", i,
2246 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002247 }
2248
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002249 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002250 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002251
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002252 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002253 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002254 if (retval) {
2255 em28xx_devused &= ~(1<<dev->devno);
2256 kfree(dev);
2257
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002258 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002259 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002260
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002261 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002262
2263 /* save our data pointer in this interface device */
2264 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002265
2266 request_modules(dev);
2267
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002268 return 0;
2269}
2270
2271/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002272 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002273 * called when the device gets diconencted
2274 * video device will be unregistered on v4l2_close in case it is still open
2275 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002276static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002277{
Sascha Sommer5a804152007-11-03 21:22:38 -03002278 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002279 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002280
2281 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002282 usb_set_intfdata(interface, NULL);
2283
2284 if (!dev)
2285 return;
2286
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002287 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002289 /* wait until all current v4l2 io is finished then deallocate
2290 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002291 mutex_lock(&dev->lock);
2292
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002293 wake_up_interruptible_all(&dev->open);
2294
2295 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002296 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002298 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002299 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002300
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002301 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002302 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002303 dev->state |= DEV_DISCONNECTED;
2304 wake_up_interruptible(&dev->wait_frame);
2305 wake_up_interruptible(&dev->wait_stream);
2306 } else {
2307 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002308 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002309 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002310 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002311
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002312 mutex_lock(&em28xx_extension_devlist_lock);
2313 if (!list_empty(&em28xx_extension_devlist)) {
2314 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2315 ops->fini(dev);
2316 }
2317 }
2318 mutex_unlock(&em28xx_extension_devlist_lock);
2319
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002320 if (!dev->users) {
2321 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002322 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002323 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002324}
2325
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002326static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002327 .name = "em28xx",
2328 .probe = em28xx_usb_probe,
2329 .disconnect = em28xx_usb_disconnect,
2330 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002331};
2332
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002333static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002334{
2335 int result;
2336
2337 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002338 (EM28XX_VERSION_CODE >> 16) & 0xff,
2339 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002340#ifdef SNAPSHOT
2341 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2342 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2343#endif
2344
2345 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002346 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002347 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002348 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002349 " usb_register failed. Error number %d.\n", result);
2350
2351 return result;
2352}
2353
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002354static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002355{
2356 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002357 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002358}
2359
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002360module_init(em28xx_module_init);
2361module_exit(em28xx_module_exit);