blob: 600b340e3550ae3ad70da8c2420d4721ae01c684 [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);
Markus Rechberger9c755412005-11-08 21:37:52 -080076
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080077static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030079static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080082module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020083module_param_array(video_nr, int, NULL, 0444);
84module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085module_param_array(radio_nr, int, NULL, 0444);
86MODULE_PARM_DESC(card, "card type");
87MODULE_PARM_DESC(video_nr, "video device numbers");
88MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
89MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080090
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030091static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030092module_param(video_debug, int, 0644);
93MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080094
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020095/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
96static unsigned long em28xx_devused;
97
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080098/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020099/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800100static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800101 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200102 .id = V4L2_CID_AUDIO_VOLUME,
103 .type = V4L2_CTRL_TYPE_INTEGER,
104 .name = "Volume",
105 .minimum = 0x0,
106 .maximum = 0x1f,
107 .step = 0x1,
108 .default_value = 0x1f,
109 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300110 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200111 .id = V4L2_CID_AUDIO_MUTE,
112 .type = V4L2_CTRL_TYPE_BOOLEAN,
113 .name = "Mute",
114 .minimum = 0,
115 .maximum = 1,
116 .step = 1,
117 .default_value = 1,
118 .flags = 0,
119 }
120};
121
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800122static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800123
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300124/* ------------------------------------------------------------------
125 DMA and thread functions
126 ------------------------------------------------------------------*/
127
128/*
129 * Announces that a buffer were filled and request the next
130 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300131static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300132 struct em28xx_dmaqueue *dma_q,
133 struct em28xx_buffer *buf)
134{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300135 /* Advice that buffer was filled */
136 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
137 buf->vb.state = VIDEOBUF_DONE;
138 buf->vb.field_count++;
139 do_gettimeofday(&buf->vb.ts);
140
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300141 dev->isoc_ctl.buf = NULL;
142
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300143 list_del(&buf->vb.queue);
144 wake_up(&buf->vb.done);
145}
146
147/*
148 * Identify the buffer header type and properly handles
149 */
150static void em28xx_copy_video(struct em28xx *dev,
151 struct em28xx_dmaqueue *dma_q,
152 struct em28xx_buffer *buf,
153 unsigned char *p,
154 unsigned char *outp, unsigned long len)
155{
156 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300157 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300158 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159
160 if (dma_q->pos + len > buf->vb.size)
161 len = buf->vb.size - dma_q->pos;
162
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300163 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300164 em28xx_isocdbg("frame is not complete\n");
165 len += 4;
166 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300167 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
169 startread = p;
170 remain = len;
171
172 /* Interlaces frame */
173 if (buf->top_field)
174 fieldstart = outp;
175 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300176 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300177
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300178 linesdone = dma_q->pos / bytesperline;
179 currlinedone = dma_q->pos % bytesperline;
180 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300181 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300182 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300183 lencopy = lencopy > remain ? remain : lencopy;
184
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300185 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300186 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300187 ((char *)startwrite + lencopy) -
188 ((char *)outp + buf->vb.size));
189 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300190 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300191 if (lencopy <= 0)
192 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300193 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300194
195 remain -= lencopy;
196
197 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300198 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300199 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300200 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201 lencopy = remain;
202 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300205 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300206 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300207 ((char *)startwrite + lencopy) -
208 ((char *)outp + buf->vb.size));
209 lencopy = remain = (char *)outp + buf->vb.size -
210 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300211 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300212 if (lencopy <= 0)
213 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300214
215 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216
217 remain -= lencopy;
218 }
219
220 dma_q->pos += len;
221}
222
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300223static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 int packet, int status)
225{
226 char *errmsg = "Unknown";
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229 case -ENOENT:
230 errmsg = "unlinked synchronuously";
231 break;
232 case -ECONNRESET:
233 errmsg = "unlinked asynchronuously";
234 break;
235 case -ENOSR:
236 errmsg = "Buffer error (overrun)";
237 break;
238 case -EPIPE:
239 errmsg = "Stalled (device not responding)";
240 break;
241 case -EOVERFLOW:
242 errmsg = "Babble (bad cable?)";
243 break;
244 case -EPROTO:
245 errmsg = "Bit-stuff error (bad cable?)";
246 break;
247 case -EILSEQ:
248 errmsg = "CRC/Timeout (could be anything)";
249 break;
250 case -ETIME:
251 errmsg = "Device does not respond";
252 break;
253 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300254 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300255 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
256 } else {
257 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
258 packet, status, errmsg);
259 }
260}
261
262/*
263 * video-buf generic routine to get the next available buffer
264 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300265static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300266 struct em28xx_buffer **buf)
267{
268 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300269 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300270
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300271 if (list_empty(&dma_q->active)) {
272 em28xx_isocdbg("No active queue to serve\n");
273 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300274 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300275 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300276 }
277
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300278 /* Get the next buffer */
279 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
280
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300281 /* Cleans up buffer - Usefull for testing for frame/URB loss */
282 outp = videobuf_to_vmalloc(&(*buf)->vb);
283 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300284
285 dev->isoc_ctl.buf = *buf;
286
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300287 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300288}
289
290/*
291 * Controls the isoc copy of each urb packet
292 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300293static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300294{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300295 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300296 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300297 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300298 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300299 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300300
301 if (!dev)
302 return 0;
303
304 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
305 return 0;
306
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300307 if (urb->status < 0) {
308 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300309 if (urb->status == -ENOENT)
310 return 0;
311 }
312
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300313 buf = dev->isoc_ctl.buf;
314 if (buf != NULL)
315 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300316
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300317 for (i = 0; i < urb->number_of_packets; i++) {
318 int status = urb->iso_frame_desc[i].status;
319
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300320 if (status < 0) {
321 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300322 if (urb->iso_frame_desc[i].status != -EPROTO)
323 continue;
324 }
325
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300326 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300327
328 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300329 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300330 continue;
331 }
332 if (urb->iso_frame_desc[i].actual_length >
333 dev->max_pkt_size) {
334 em28xx_isocdbg("packet bigger than packet size");
335 continue;
336 }
337
338 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300339
340 /* FIXME: incomplete buffer checks where removed to make
341 logic simpler. Impacts of those changes should be evaluated
342 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300343 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
344 em28xx_isocdbg("VBI HEADER!!!\n");
345 /* FIXME: Should add vbi copy */
346 continue;
347 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300348 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300349 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
350 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300351
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300352 if (!(p[2] & 1)) {
353 if (buf != NULL)
354 buffer_filled(dev, dma_q, buf);
355 get_next_buf(dma_q, &buf);
356 if (buf == NULL)
357 outp = NULL;
358 else
359 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300360 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300361
362 if (buf != NULL) {
363 if (p[2] & 1)
364 buf->top_field = 0;
365 else
366 buf->top_field = 1;
367 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300368
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300371 if (buf != NULL)
372 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300373 }
374 return rc;
375}
376
377/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300378 Videobuf operations
379 ------------------------------------------------------------------*/
380
381static int
382buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
383{
384 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300385 struct em28xx *dev = fh->dev;
386 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300387
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300388 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389 if (0 == *count)
390 *count = EM28XX_DEF_BUF;
391
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300392 if (*count < EM28XX_MIN_BUF)
393 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300394
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300395 /* Ask tuner to go to analog mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300396 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300397 f.frequency = dev->ctl_freq;
398
399 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
400
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300401 return 0;
402}
403
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300404/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300405static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
406{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300407 struct em28xx_fh *fh = vq->priv_data;
408 struct em28xx *dev = fh->dev;
409 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300410 if (in_interrupt())
411 BUG();
412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413 /* We used to wait for the buffer to finish here, but this didn't work
414 because, as we were keeping the state as VIDEOBUF_QUEUED,
415 videobuf_queue_cancel marked it as finished for us.
416 (Also, it could wedge forever if the hardware was misconfigured.)
417
418 This should be safe; by the time we get here, the buffer isn't
419 queued anymore. If we ever start marking the buffers as
420 VIDEOBUF_ACTIVE, it won't be, though.
421 */
422 spin_lock_irqsave(&dev->slock, flags);
423 if (dev->isoc_ctl.buf == buf)
424 dev->isoc_ctl.buf = NULL;
425 spin_unlock_irqrestore(&dev->slock, flags);
426
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300427 videobuf_vmalloc_free(&buf->vb);
428 buf->vb.state = VIDEOBUF_NEEDS_INIT;
429}
430
431static int
432buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
433 enum v4l2_field field)
434{
435 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300436 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300437 struct em28xx *dev = fh->dev;
438 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440 /* FIXME: It assumes depth = 16 */
441 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300442 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443
444 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
445 return -EINVAL;
446
Brandon Philips05612972008-04-13 14:57:01 -0300447 buf->vb.width = dev->width;
448 buf->vb.height = dev->height;
449 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300450
451 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300452 rc = videobuf_iolock(vq, &buf->vb, NULL);
453 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 }
456
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300458 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300459
460 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300461 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
462 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300463 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300464 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465 goto fail;
466 }
467
468 buf->vb.state = VIDEOBUF_PREPARED;
469 return 0;
470
471fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300472 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473 return rc;
474}
475
476static void
477buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
478{
479 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
480 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300481 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300482 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300484 buf->vb.state = VIDEOBUF_QUEUED;
485 list_add_tail(&buf->vb.queue, &vidq->active);
486
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300487}
488
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300489static void buffer_release(struct videobuf_queue *vq,
490 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300492 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300494 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300496 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300497
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300498 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300499}
500
501static struct videobuf_queue_ops em28xx_video_qops = {
502 .buf_setup = buffer_setup,
503 .buf_prepare = buffer_prepare,
504 .buf_queue = buffer_queue,
505 .buf_release = buffer_release,
506};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800507
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300508/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800509
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800510/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800511 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800512 * inits registers with sane defaults
513 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800514static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800515{
516
517 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300518 if (!dev->is_em2800)
519 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800520
521 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200522
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300523/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
524/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
525 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200526
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800527 dev->mute = 1; /* maybe not the right place... */
528 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300529
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800530 em28xx_outfmt_set_yuv422(dev);
531 em28xx_colorlevels_set_default(dev);
532 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800533
534 return 0;
535}
536
537/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800538 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800539 * configure i2c attached devices
540 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800541static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800542{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300543 struct v4l2_routing route;
544
545 route.input = INPUT(dev->ctl_input)->vmux;
546 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100547 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300548 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300549 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800550}
551
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800552static void video_mux(struct em28xx *dev, int index)
553{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300554 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800555
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300556 route.input = INPUT(index)->vmux;
557 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800558 dev->ctl_input = index;
559 dev->ctl_ainput = INPUT(index)->amux;
560
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300561 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800562
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800563 if (dev->has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300564 if (dev->i2s_speed) {
565 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
566 &dev->i2s_speed);
567 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300568 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300569 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300570 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300571 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
572 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800573 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300574
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300575 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800576}
577
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300578/* Usage lock check functions */
579static int res_get(struct em28xx_fh *fh)
580{
581 struct em28xx *dev = fh->dev;
582 int rc = 0;
583
584 /* This instance already has stream_on */
585 if (fh->stream_on)
586 return rc;
587
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300588 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300589 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300590
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300591 mutex_lock(&dev->lock);
592 dev->stream_on = 1;
593 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300594 mutex_unlock(&dev->lock);
595 return rc;
596}
597
598static int res_check(struct em28xx_fh *fh)
599{
600 return (fh->stream_on);
601}
602
603static void res_free(struct em28xx_fh *fh)
604{
605 struct em28xx *dev = fh->dev;
606
607 mutex_lock(&dev->lock);
608 fh->stream_on = 0;
609 dev->stream_on = 0;
610 mutex_unlock(&dev->lock);
611}
612
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800613/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300614 * em28xx_get_ctrl()
615 * return the current saturation, brightness or contrast, mute state
616 */
617static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
618{
619 switch (ctrl->id) {
620 case V4L2_CID_AUDIO_MUTE:
621 ctrl->value = dev->mute;
622 return 0;
623 case V4L2_CID_AUDIO_VOLUME:
624 ctrl->value = dev->volume;
625 return 0;
626 default:
627 return -EINVAL;
628 }
629}
630
631/*
632 * em28xx_set_ctrl()
633 * mute or set new saturation, brightness or contrast
634 */
635static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
636{
637 switch (ctrl->id) {
638 case V4L2_CID_AUDIO_MUTE:
639 if (ctrl->value != dev->mute) {
640 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300641 return em28xx_audio_analog_set(dev);
642 }
643 return 0;
644 case V4L2_CID_AUDIO_VOLUME:
645 dev->volume = ctrl->value;
646 return em28xx_audio_analog_set(dev);
647 default:
648 return -EINVAL;
649 }
650}
651
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300652static int check_dev(struct em28xx *dev)
653{
654 if (dev->state & DEV_DISCONNECTED) {
655 em28xx_errdev("v4l2 ioctl: device not present\n");
656 return -ENODEV;
657 }
658
659 if (dev->state & DEV_MISCONFIGURED) {
660 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
661 "close and open it again\n");
662 return -EIO;
663 }
664 return 0;
665}
666
667static void get_scale(struct em28xx *dev,
668 unsigned int width, unsigned int height,
669 unsigned int *hscale, unsigned int *vscale)
670{
671 unsigned int maxw = norm_maxw(dev);
672 unsigned int maxh = norm_maxh(dev);
673
674 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
675 if (*hscale >= 0x4000)
676 *hscale = 0x3fff;
677
678 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
679 if (*vscale >= 0x4000)
680 *vscale = 0x3fff;
681}
682
683/* ------------------------------------------------------------------
684 IOCTL vidioc handling
685 ------------------------------------------------------------------*/
686
Hans Verkuil78b526a2008-05-28 12:16:41 -0300687static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300688 struct v4l2_format *f)
689{
690 struct em28xx_fh *fh = priv;
691 struct em28xx *dev = fh->dev;
692
693 mutex_lock(&dev->lock);
694
695 f->fmt.pix.width = dev->width;
696 f->fmt.pix.height = dev->height;
697 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300698 f->fmt.pix.bytesperline = dev->width * 2;
699 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300700 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
701
702 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
703 f->fmt.pix.field = dev->interlaced ?
704 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
705
706 mutex_unlock(&dev->lock);
707 return 0;
708}
709
Hans Verkuil78b526a2008-05-28 12:16:41 -0300710static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300711 struct v4l2_format *f)
712{
713 struct em28xx_fh *fh = priv;
714 struct em28xx *dev = fh->dev;
715 int width = f->fmt.pix.width;
716 int height = f->fmt.pix.height;
717 unsigned int maxw = norm_maxw(dev);
718 unsigned int maxh = norm_maxh(dev);
719 unsigned int hscale, vscale;
720
721 /* width must even because of the YUYV format
722 height must be even because of interlacing */
723 height &= 0xfffe;
724 width &= 0xfffe;
725
726 if (height < 32)
727 height = 32;
728 if (height > maxh)
729 height = maxh;
730 if (width < 48)
731 width = 48;
732 if (width > maxw)
733 width = maxw;
734
735 mutex_lock(&dev->lock);
736
737 if (dev->is_em2800) {
738 /* the em2800 can only scale down to 50% */
739 if (height % (maxh / 2))
740 height = maxh;
741 if (width % (maxw / 2))
742 width = maxw;
743 /* according to empiatech support */
744 /* the MaxPacketSize is to small to support */
745 /* framesizes larger than 640x480 @ 30 fps */
746 /* or 640x576 @ 25 fps. As this would cut */
747 /* of a part of the image we prefer */
748 /* 360x576 or 360x480 for now */
749 if (width == maxw && height == maxh)
750 width /= 2;
751 }
752
753 get_scale(dev, width, height, &hscale, &vscale);
754
755 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
756 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
757
758 f->fmt.pix.width = width;
759 f->fmt.pix.height = height;
760 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
761 f->fmt.pix.bytesperline = width * 2;
762 f->fmt.pix.sizeimage = width * 2 * height;
763 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
764 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
765
766 mutex_unlock(&dev->lock);
767 return 0;
768}
769
Hans Verkuil78b526a2008-05-28 12:16:41 -0300770static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300771 struct v4l2_format *f)
772{
773 struct em28xx_fh *fh = priv;
774 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300775 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300776
777 rc = check_dev(dev);
778 if (rc < 0)
779 return rc;
780
Hans Verkuil78b526a2008-05-28 12:16:41 -0300781 vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300782
783 mutex_lock(&dev->lock);
784
Brandon Philips05612972008-04-13 14:57:01 -0300785 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
786 em28xx_errdev("%s queue busy\n", __func__);
787 rc = -EBUSY;
788 goto out;
789 }
790
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300791 if (dev->stream_on && !fh->stream_on) {
792 em28xx_errdev("%s device in use by another fh\n", __func__);
793 rc = -EBUSY;
794 goto out;
795 }
796
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300797 /* set new image size */
798 dev->width = f->fmt.pix.width;
799 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300800 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
801
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300802 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300803 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300804
Brandon Philips05612972008-04-13 14:57:01 -0300805 rc = 0;
806
807out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300809 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300810}
811
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300812static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813{
814 struct em28xx_fh *fh = priv;
815 struct em28xx *dev = fh->dev;
816 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817 int rc;
818
819 rc = check_dev(dev);
820 if (rc < 0)
821 return rc;
822
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300823 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300824 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300825 mutex_unlock(&dev->lock);
826
827 /* Adjusts width/height, if needed */
828 f.fmt.pix.width = dev->width;
829 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300830 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300831
832 mutex_lock(&dev->lock);
833
834 /* set new image size */
835 dev->width = f.fmt.pix.width;
836 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300837 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
838
839 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300840 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300841
842 mutex_unlock(&dev->lock);
843 return 0;
844}
845
846static const char *iname[] = {
847 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
848 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
849 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
850 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
851 [EM28XX_VMUX_SVIDEO] = "S-Video",
852 [EM28XX_VMUX_TELEVISION] = "Television",
853 [EM28XX_VMUX_CABLE] = "Cable TV",
854 [EM28XX_VMUX_DVB] = "DVB",
855 [EM28XX_VMUX_DEBUG] = "for debug only",
856};
857
858static int vidioc_enum_input(struct file *file, void *priv,
859 struct v4l2_input *i)
860{
861 struct em28xx_fh *fh = priv;
862 struct em28xx *dev = fh->dev;
863 unsigned int n;
864
865 n = i->index;
866 if (n >= MAX_EM28XX_INPUT)
867 return -EINVAL;
868 if (0 == INPUT(n)->type)
869 return -EINVAL;
870
871 i->index = n;
872 i->type = V4L2_INPUT_TYPE_CAMERA;
873
874 strcpy(i->name, iname[INPUT(n)->type]);
875
876 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
877 (EM28XX_VMUX_CABLE == INPUT(n)->type))
878 i->type = V4L2_INPUT_TYPE_TUNER;
879
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300880 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300881
882 return 0;
883}
884
885static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
886{
887 struct em28xx_fh *fh = priv;
888 struct em28xx *dev = fh->dev;
889
890 *i = dev->ctl_input;
891
892 return 0;
893}
894
895static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
896{
897 struct em28xx_fh *fh = priv;
898 struct em28xx *dev = fh->dev;
899 int rc;
900
901 rc = check_dev(dev);
902 if (rc < 0)
903 return rc;
904
905 if (i >= MAX_EM28XX_INPUT)
906 return -EINVAL;
907 if (0 == INPUT(i)->type)
908 return -EINVAL;
909
910 mutex_lock(&dev->lock);
911
912 video_mux(dev, i);
913
914 mutex_unlock(&dev->lock);
915 return 0;
916}
917
918static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
919{
920 struct em28xx_fh *fh = priv;
921 struct em28xx *dev = fh->dev;
922 unsigned int index = a->index;
923
924 if (a->index > 1)
925 return -EINVAL;
926
927 index = dev->ctl_ainput;
928
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300929 if (index == 0)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300930 strcpy(a->name, "Television");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300931 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300932 strcpy(a->name, "Line In");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300933
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300934 a->capability = V4L2_AUDCAP_STEREO;
935 a->index = index;
936
937 return 0;
938}
939
940static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
941{
942 struct em28xx_fh *fh = priv;
943 struct em28xx *dev = fh->dev;
944
945 if (a->index != dev->ctl_ainput)
946 return -EINVAL;
947
948 return 0;
949}
950
951static int vidioc_queryctrl(struct file *file, void *priv,
952 struct v4l2_queryctrl *qc)
953{
954 struct em28xx_fh *fh = priv;
955 struct em28xx *dev = fh->dev;
956 int id = qc->id;
957 int i;
958 int rc;
959
960 rc = check_dev(dev);
961 if (rc < 0)
962 return rc;
963
964 memset(qc, 0, sizeof(*qc));
965
966 qc->id = id;
967
968 if (!dev->has_msp34xx) {
969 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
970 if (qc->id && qc->id == em28xx_qctrl[i].id) {
971 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
972 return 0;
973 }
974 }
975 }
976 mutex_lock(&dev->lock);
977 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
978 mutex_unlock(&dev->lock);
979
980 if (qc->type)
981 return 0;
982 else
983 return -EINVAL;
984}
985
986static int vidioc_g_ctrl(struct file *file, void *priv,
987 struct v4l2_control *ctrl)
988{
989 struct em28xx_fh *fh = priv;
990 struct em28xx *dev = fh->dev;
991 int rc;
992
993 rc = check_dev(dev);
994 if (rc < 0)
995 return rc;
996 mutex_lock(&dev->lock);
997
998 if (!dev->has_msp34xx)
999 rc = em28xx_get_ctrl(dev, ctrl);
1000 else
1001 rc = -EINVAL;
1002
1003 if (rc == -EINVAL) {
1004 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1005 rc = 0;
1006 }
1007
1008 mutex_unlock(&dev->lock);
1009 return rc;
1010}
1011
1012static int vidioc_s_ctrl(struct file *file, void *priv,
1013 struct v4l2_control *ctrl)
1014{
1015 struct em28xx_fh *fh = priv;
1016 struct em28xx *dev = fh->dev;
1017 u8 i;
1018 int rc;
1019
1020 rc = check_dev(dev);
1021 if (rc < 0)
1022 return rc;
1023
1024 mutex_lock(&dev->lock);
1025
1026 if (dev->has_msp34xx)
1027 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1028 else {
1029 rc = 1;
1030 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1031 if (ctrl->id == em28xx_qctrl[i].id) {
1032 if (ctrl->value < em28xx_qctrl[i].minimum ||
1033 ctrl->value > em28xx_qctrl[i].maximum) {
1034 rc = -ERANGE;
1035 break;
1036 }
1037
1038 rc = em28xx_set_ctrl(dev, ctrl);
1039 break;
1040 }
1041 }
1042 }
1043
1044 /* Control not found - try to send it to the attached devices */
1045 if (rc == 1) {
1046 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1047 rc = 0;
1048 }
1049
1050 mutex_unlock(&dev->lock);
1051 return rc;
1052}
1053
1054static int vidioc_g_tuner(struct file *file, void *priv,
1055 struct v4l2_tuner *t)
1056{
1057 struct em28xx_fh *fh = priv;
1058 struct em28xx *dev = fh->dev;
1059 int rc;
1060
1061 rc = check_dev(dev);
1062 if (rc < 0)
1063 return rc;
1064
1065 if (0 != t->index)
1066 return -EINVAL;
1067
1068 strcpy(t->name, "Tuner");
1069
1070 mutex_lock(&dev->lock);
1071
1072 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1073
1074 mutex_unlock(&dev->lock);
1075 return 0;
1076}
1077
1078static int vidioc_s_tuner(struct file *file, void *priv,
1079 struct v4l2_tuner *t)
1080{
1081 struct em28xx_fh *fh = priv;
1082 struct em28xx *dev = fh->dev;
1083 int rc;
1084
1085 rc = check_dev(dev);
1086 if (rc < 0)
1087 return rc;
1088
1089 if (0 != t->index)
1090 return -EINVAL;
1091
1092 mutex_lock(&dev->lock);
1093
1094 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1095
1096 mutex_unlock(&dev->lock);
1097 return 0;
1098}
1099
1100static int vidioc_g_frequency(struct file *file, void *priv,
1101 struct v4l2_frequency *f)
1102{
1103 struct em28xx_fh *fh = priv;
1104 struct em28xx *dev = fh->dev;
1105
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001106 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001107 f->frequency = dev->ctl_freq;
1108
1109 return 0;
1110}
1111
1112static int vidioc_s_frequency(struct file *file, void *priv,
1113 struct v4l2_frequency *f)
1114{
1115 struct em28xx_fh *fh = priv;
1116 struct em28xx *dev = fh->dev;
1117 int rc;
1118
1119 rc = check_dev(dev);
1120 if (rc < 0)
1121 return rc;
1122
1123 if (0 != f->tuner)
1124 return -EINVAL;
1125
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001126 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1127 return -EINVAL;
1128 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001129 return -EINVAL;
1130
1131 mutex_lock(&dev->lock);
1132
1133 dev->ctl_freq = f->frequency;
1134 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1135
1136 mutex_unlock(&dev->lock);
1137 return 0;
1138}
1139
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001140#ifdef CONFIG_VIDEO_ADV_DEBUG
1141static int em28xx_reg_len(int reg)
1142{
1143 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001144 case EM28XX_R40_AC97LSB:
1145 case EM28XX_R30_HSCALELOW:
1146 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001147 return 2;
1148 default:
1149 return 1;
1150 }
1151}
1152
1153static int vidioc_g_register(struct file *file, void *priv,
1154 struct v4l2_register *reg)
1155{
1156 struct em28xx_fh *fh = priv;
1157 struct em28xx *dev = fh->dev;
1158 int ret;
1159
1160 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1161 return -EINVAL;
1162
1163 if (em28xx_reg_len(reg->reg) == 1) {
1164 ret = em28xx_read_reg(dev, reg->reg);
1165 if (ret < 0)
1166 return ret;
1167
1168 reg->val = ret;
1169 } else {
Al Viroa954b662008-05-21 00:32:51 -03001170 __le64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001171 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1172 reg->reg, (char *)&val, 2);
1173 if (ret < 0)
1174 return ret;
1175
Al Viroa954b662008-05-21 00:32:51 -03001176 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001177 }
1178
1179 return 0;
1180}
1181
1182static int vidioc_s_register(struct file *file, void *priv,
1183 struct v4l2_register *reg)
1184{
1185 struct em28xx_fh *fh = priv;
1186 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001187 __le64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001188
Al Viroa954b662008-05-21 00:32:51 -03001189 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001190
1191 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1192 em28xx_reg_len(reg->reg));
1193}
1194#endif
1195
1196
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001197static int vidioc_cropcap(struct file *file, void *priv,
1198 struct v4l2_cropcap *cc)
1199{
1200 struct em28xx_fh *fh = priv;
1201 struct em28xx *dev = fh->dev;
1202
1203 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1204 return -EINVAL;
1205
1206 cc->bounds.left = 0;
1207 cc->bounds.top = 0;
1208 cc->bounds.width = dev->width;
1209 cc->bounds.height = dev->height;
1210 cc->defrect = cc->bounds;
1211 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1212 cc->pixelaspect.denominator = 59;
1213
1214 return 0;
1215}
1216
1217static int vidioc_streamon(struct file *file, void *priv,
1218 enum v4l2_buf_type type)
1219{
1220 struct em28xx_fh *fh = priv;
1221 struct em28xx *dev = fh->dev;
1222 int rc;
1223
1224 rc = check_dev(dev);
1225 if (rc < 0)
1226 return rc;
1227
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001228
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001229 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001230 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001231
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001232 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001233}
1234
1235static int vidioc_streamoff(struct file *file, void *priv,
1236 enum v4l2_buf_type type)
1237{
1238 struct em28xx_fh *fh = priv;
1239 struct em28xx *dev = fh->dev;
1240 int rc;
1241
1242 rc = check_dev(dev);
1243 if (rc < 0)
1244 return rc;
1245
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001246 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1247 return -EINVAL;
1248 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001249 return -EINVAL;
1250
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001251 videobuf_streamoff(&fh->vb_vidq);
1252 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001253
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001254 return 0;
1255}
1256
1257static int vidioc_querycap(struct file *file, void *priv,
1258 struct v4l2_capability *cap)
1259{
1260 struct em28xx_fh *fh = priv;
1261 struct em28xx *dev = fh->dev;
1262
1263 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1264 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1265 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1266
1267 cap->version = EM28XX_VERSION_CODE;
1268
1269 cap->capabilities =
1270 V4L2_CAP_SLICED_VBI_CAPTURE |
1271 V4L2_CAP_VIDEO_CAPTURE |
1272 V4L2_CAP_AUDIO |
1273 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1274
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001275 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001276 cap->capabilities |= V4L2_CAP_TUNER;
1277
1278 return 0;
1279}
1280
Hans Verkuil78b526a2008-05-28 12:16:41 -03001281static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001282 struct v4l2_fmtdesc *fmtd)
1283{
1284 if (fmtd->index != 0)
1285 return -EINVAL;
1286
1287 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1288 strcpy(fmtd->description, "Packed YUY2");
1289 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1290 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1291
1292 return 0;
1293}
1294
1295/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001296static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001297 struct v4l2_format *f)
1298{
1299 struct em28xx_fh *fh = priv;
1300 struct em28xx *dev = fh->dev;
1301 int rc;
1302
1303 rc = check_dev(dev);
1304 if (rc < 0)
1305 return rc;
1306
1307 mutex_lock(&dev->lock);
1308
1309 f->fmt.sliced.service_set = 0;
1310
1311 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1312
1313 if (f->fmt.sliced.service_set == 0)
1314 rc = -EINVAL;
1315
1316 mutex_unlock(&dev->lock);
1317 return rc;
1318}
1319
Hans Verkuil78b526a2008-05-28 12:16:41 -03001320static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001321 struct v4l2_format *f)
1322{
1323 struct em28xx_fh *fh = priv;
1324 struct em28xx *dev = fh->dev;
1325 int rc;
1326
1327 rc = check_dev(dev);
1328 if (rc < 0)
1329 return rc;
1330
1331 mutex_lock(&dev->lock);
1332 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1333 mutex_unlock(&dev->lock);
1334
1335 if (f->fmt.sliced.service_set == 0)
1336 return -EINVAL;
1337
1338 return 0;
1339}
1340
1341
1342static int vidioc_reqbufs(struct file *file, void *priv,
1343 struct v4l2_requestbuffers *rb)
1344{
1345 struct em28xx_fh *fh = priv;
1346 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001347 int rc;
1348
1349 rc = check_dev(dev);
1350 if (rc < 0)
1351 return rc;
1352
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001353 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001354}
1355
1356static int vidioc_querybuf(struct file *file, void *priv,
1357 struct v4l2_buffer *b)
1358{
1359 struct em28xx_fh *fh = priv;
1360 struct em28xx *dev = fh->dev;
1361 int rc;
1362
1363 rc = check_dev(dev);
1364 if (rc < 0)
1365 return rc;
1366
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001367 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001368}
1369
1370static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1371{
1372 struct em28xx_fh *fh = priv;
1373 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001374 int rc;
1375
1376 rc = check_dev(dev);
1377 if (rc < 0)
1378 return rc;
1379
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001380 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381}
1382
1383static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1384{
1385 struct em28xx_fh *fh = priv;
1386 struct em28xx *dev = fh->dev;
1387 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001388
1389 rc = check_dev(dev);
1390 if (rc < 0)
1391 return rc;
1392
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001393 return (videobuf_dqbuf(&fh->vb_vidq, b,
1394 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001395}
1396
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001397#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001398static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001399{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001400 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001401
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001402 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001403}
1404#endif
1405
1406
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001407/* ----------------------------------------------------------- */
1408/* RADIO ESPECIFIC IOCTLS */
1409/* ----------------------------------------------------------- */
1410
1411static int radio_querycap(struct file *file, void *priv,
1412 struct v4l2_capability *cap)
1413{
1414 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1415
1416 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1417 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1418 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1419
1420 cap->version = EM28XX_VERSION_CODE;
1421 cap->capabilities = V4L2_CAP_TUNER;
1422 return 0;
1423}
1424
1425static int radio_g_tuner(struct file *file, void *priv,
1426 struct v4l2_tuner *t)
1427{
1428 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1429
1430 if (unlikely(t->index > 0))
1431 return -EINVAL;
1432
1433 strcpy(t->name, "Radio");
1434 t->type = V4L2_TUNER_RADIO;
1435
1436 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1437 return 0;
1438}
1439
1440static int radio_enum_input(struct file *file, void *priv,
1441 struct v4l2_input *i)
1442{
1443 if (i->index != 0)
1444 return -EINVAL;
1445 strcpy(i->name, "Radio");
1446 i->type = V4L2_INPUT_TYPE_TUNER;
1447
1448 return 0;
1449}
1450
1451static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1452{
1453 if (unlikely(a->index))
1454 return -EINVAL;
1455
1456 strcpy(a->name, "Radio");
1457 return 0;
1458}
1459
1460static int radio_s_tuner(struct file *file, void *priv,
1461 struct v4l2_tuner *t)
1462{
1463 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1464
1465 if (0 != t->index)
1466 return -EINVAL;
1467
1468 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1469
1470 return 0;
1471}
1472
1473static int radio_s_audio(struct file *file, void *fh,
1474 struct v4l2_audio *a)
1475{
1476 return 0;
1477}
1478
1479static int radio_s_input(struct file *file, void *fh, unsigned int i)
1480{
1481 return 0;
1482}
1483
1484static int radio_queryctrl(struct file *file, void *priv,
1485 struct v4l2_queryctrl *qc)
1486{
1487 int i;
1488
1489 if (qc->id < V4L2_CID_BASE ||
1490 qc->id >= V4L2_CID_LASTP1)
1491 return -EINVAL;
1492
1493 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1494 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1495 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1496 return 0;
1497 }
1498 }
1499
1500 return -EINVAL;
1501}
1502
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001503/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001504 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001505 * inits the device and starts isoc transfer
1506 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001507static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001508{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001509 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001510 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001511 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001512 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001513 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001514
Hans Verkuild56dc612008-07-30 08:43:36 -03001515 lock_kernel();
Trent Piephoa991f442007-10-10 05:37:43 -03001516 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001517 if (h->vdev->minor == minor) {
1518 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001519 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001520 }
1521 if (h->vbi_dev->minor == minor) {
1522 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001523 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001524 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001525 if (h->radio_dev &&
1526 h->radio_dev->minor == minor) {
1527 radio = 1;
1528 dev = h;
1529 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001530 }
Hans Verkuild56dc612008-07-30 08:43:36 -03001531 if (NULL == dev) {
1532 unlock_kernel();
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001533 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -03001534 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001535
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001536 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001537 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001538
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001539
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001540 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001541 if (!fh) {
1542 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuild56dc612008-07-30 08:43:36 -03001543 unlock_kernel();
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001544 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001545 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001546 mutex_lock(&dev->lock);
1547 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001548 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001549 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001550 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001551
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001552 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001553 dev->width = norm_maxw(dev);
1554 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001555 dev->hscale = 0;
1556 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001557
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001558 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001559 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001560 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001561
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001562 /* Needed, since GPIO might have disabled power of
1563 some i2c device
1564 */
1565 em28xx_config_i2c(dev);
1566
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001567 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001568 if (fh->radio) {
1569 em28xx_videodbg("video_open: setting radio device\n");
1570 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1571 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001572
1573 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001574
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001575 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1576 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1577 sizeof(struct em28xx_buffer), fh);
1578
Ingo Molnar3593cab2006-02-07 06:49:14 -02001579 mutex_unlock(&dev->lock);
Hans Verkuild56dc612008-07-30 08:43:36 -03001580 unlock_kernel();
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001581
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001582 return errCode;
1583}
1584
1585/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001586 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001587 * unregisters the v4l2,i2c and usb devices
1588 * called when the device gets disconected or at module unload
1589*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001590static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001591{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001592
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001593 /*FIXME: I2C IR should be disconnected */
1594
1595 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1596 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1597 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001598 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001599 if (dev->sbutton_input_dev)
1600 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001601 if (dev->radio_dev) {
1602 if (-1 != dev->radio_dev->minor)
1603 video_unregister_device(dev->radio_dev);
1604 else
1605 video_device_release(dev->radio_dev);
1606 dev->radio_dev = NULL;
1607 }
1608 if (dev->vbi_dev) {
1609 if (-1 != dev->vbi_dev->minor)
1610 video_unregister_device(dev->vbi_dev);
1611 else
1612 video_device_release(dev->vbi_dev);
1613 dev->vbi_dev = NULL;
1614 }
1615 if (dev->vdev) {
1616 if (-1 != dev->vdev->minor)
1617 video_unregister_device(dev->vdev);
1618 else
1619 video_device_release(dev->vdev);
1620 dev->vdev = NULL;
1621 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001622 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001623 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001624
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001625 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001626 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001627}
1628
1629/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001630 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001631 * stops streaming and deallocates all resources allocated by the v4l2
1632 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001633 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001634static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001635{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001636 struct em28xx_fh *fh = filp->private_data;
1637 struct em28xx *dev = fh->dev;
1638 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001639
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001640 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001641
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001642
1643 if (res_check(fh))
1644 res_free(fh);
1645
Ingo Molnar3593cab2006-02-07 06:49:14 -02001646 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001647
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001648 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001649 videobuf_stop(&fh->vb_vidq);
1650 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001651
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001652 /* the device is already disconnect,
1653 free the remaining resources */
1654 if (dev->state & DEV_DISCONNECTED) {
1655 em28xx_release_resources(dev);
1656 mutex_unlock(&dev->lock);
1657 kfree(dev);
1658 return 0;
1659 }
1660
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001661 /* do this before setting alternate! */
1662 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001663 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001664
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001665 /* set alternate 0 */
1666 dev->alt = 0;
1667 em28xx_videodbg("setting alternate 0\n");
1668 errCode = usb_set_interface(dev->udev, 0, 0);
1669 if (errCode < 0) {
1670 em28xx_errdev("cannot change alternate number to "
1671 "0 (error=%i)\n", errCode);
1672 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001674 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001675 dev->users--;
1676 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001677 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678 return 0;
1679}
1680
1681/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001682 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001683 * will allocate buffers when called for the first time
1684 */
1685static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001686em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001687 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001688{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001689 struct em28xx_fh *fh = filp->private_data;
1690 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001691 int rc;
1692
1693 rc = check_dev(dev);
1694 if (rc < 0)
1695 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001696
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001697 /* FIXME: read() is not prepared to allow changing the video
1698 resolution while streaming. Seems a bug at em28xx_set_fmt
1699 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001700
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001701 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1702 if (unlikely(res_get(fh)))
1703 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001704
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001705 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1706 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001707 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001708 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001709}
1710
1711/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001712 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713 * will allocate buffers when called for the first time
1714 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001715static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001716{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001717 struct em28xx_fh *fh = filp->private_data;
1718 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001719 int rc;
1720
1721 rc = check_dev(dev);
1722 if (rc < 0)
1723 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001724
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001725 if (unlikely(res_get(fh) < 0))
1726 return POLLERR;
1727
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001728 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1729 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001731 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001732}
1733
1734/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001735 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001737static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001739 struct em28xx_fh *fh = filp->private_data;
1740 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001741 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001742
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001743 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001744 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001745
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001746 rc = check_dev(dev);
1747 if (rc < 0)
1748 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001749
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001750 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001751
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001752 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1753 (unsigned long)vma->vm_start,
1754 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1755 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001756
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001757 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001758}
1759
Arjan van de Venfa027c22007-02-12 00:55:33 -08001760static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001761 .owner = THIS_MODULE,
1762 .open = em28xx_v4l2_open,
1763 .release = em28xx_v4l2_close,
1764 .read = em28xx_v4l2_read,
1765 .poll = em28xx_v4l2_poll,
1766 .mmap = em28xx_v4l2_mmap,
1767 .ioctl = video_ioctl2,
1768 .llseek = no_llseek,
1769 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001770};
1771
Hans Verkuila3998102008-07-21 02:57:38 -03001772static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001773 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001774 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1775 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1776 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1777 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001778 .vidioc_g_audio = vidioc_g_audio,
1779 .vidioc_s_audio = vidioc_s_audio,
1780 .vidioc_cropcap = vidioc_cropcap,
1781
Hans Verkuil78b526a2008-05-28 12:16:41 -03001782 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1783 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1784 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001785
1786 .vidioc_reqbufs = vidioc_reqbufs,
1787 .vidioc_querybuf = vidioc_querybuf,
1788 .vidioc_qbuf = vidioc_qbuf,
1789 .vidioc_dqbuf = vidioc_dqbuf,
1790 .vidioc_s_std = vidioc_s_std,
1791 .vidioc_enum_input = vidioc_enum_input,
1792 .vidioc_g_input = vidioc_g_input,
1793 .vidioc_s_input = vidioc_s_input,
1794 .vidioc_queryctrl = vidioc_queryctrl,
1795 .vidioc_g_ctrl = vidioc_g_ctrl,
1796 .vidioc_s_ctrl = vidioc_s_ctrl,
1797 .vidioc_streamon = vidioc_streamon,
1798 .vidioc_streamoff = vidioc_streamoff,
1799 .vidioc_g_tuner = vidioc_g_tuner,
1800 .vidioc_s_tuner = vidioc_s_tuner,
1801 .vidioc_g_frequency = vidioc_g_frequency,
1802 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001803#ifdef CONFIG_VIDEO_ADV_DEBUG
1804 .vidioc_g_register = vidioc_g_register,
1805 .vidioc_s_register = vidioc_s_register,
1806#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001807#ifdef CONFIG_VIDEO_V4L1_COMPAT
1808 .vidiocgmbuf = vidiocgmbuf,
1809#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001810};
1811
1812static const struct video_device em28xx_video_template = {
1813 .fops = &em28xx_v4l_fops,
1814 .release = video_device_release,
1815 .ioctl_ops = &video_ioctl_ops,
1816
1817 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001818
1819 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001820 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001821};
1822
Hans Verkuila3998102008-07-21 02:57:38 -03001823static const struct file_operations radio_fops = {
1824 .owner = THIS_MODULE,
1825 .open = em28xx_v4l2_open,
1826 .release = em28xx_v4l2_close,
1827 .ioctl = video_ioctl2,
1828 .compat_ioctl = v4l_compat_ioctl32,
1829 .llseek = no_llseek,
1830};
1831
1832static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001833 .vidioc_querycap = radio_querycap,
1834 .vidioc_g_tuner = radio_g_tuner,
1835 .vidioc_enum_input = radio_enum_input,
1836 .vidioc_g_audio = radio_g_audio,
1837 .vidioc_s_tuner = radio_s_tuner,
1838 .vidioc_s_audio = radio_s_audio,
1839 .vidioc_s_input = radio_s_input,
1840 .vidioc_queryctrl = radio_queryctrl,
1841 .vidioc_g_ctrl = vidioc_g_ctrl,
1842 .vidioc_s_ctrl = vidioc_s_ctrl,
1843 .vidioc_g_frequency = vidioc_g_frequency,
1844 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001845#ifdef CONFIG_VIDEO_ADV_DEBUG
1846 .vidioc_g_register = vidioc_g_register,
1847 .vidioc_s_register = vidioc_s_register,
1848#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001849};
1850
Hans Verkuila3998102008-07-21 02:57:38 -03001851static struct video_device em28xx_radio_template = {
1852 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001853 .fops = &radio_fops,
1854 .ioctl_ops = &radio_ioctl_ops,
1855 .minor = -1,
1856};
1857
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001858/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001859
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001860
1861static LIST_HEAD(em28xx_extension_devlist);
1862static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1863
1864int em28xx_register_extension(struct em28xx_ops *ops)
1865{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001866 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001867
1868 mutex_lock(&em28xx_extension_devlist_lock);
1869 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001870 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1871 if (dev)
1872 ops->init(dev);
1873 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001874 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1875 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001876 return 0;
1877}
1878EXPORT_SYMBOL(em28xx_register_extension);
1879
1880void em28xx_unregister_extension(struct em28xx_ops *ops)
1881{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001882 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001883
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001884 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1885 if (dev)
1886 ops->fini(dev);
1887 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001888
1889 mutex_lock(&em28xx_extension_devlist_lock);
1890 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1891 list_del(&ops->next);
1892 mutex_unlock(&em28xx_extension_devlist_lock);
1893}
1894EXPORT_SYMBOL(em28xx_unregister_extension);
1895
Adrian Bunk532fe652008-01-28 22:10:48 -03001896static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1897 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03001898 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001899{
1900 struct video_device *vfd;
1901
1902 vfd = video_device_alloc();
1903 if (NULL == vfd)
1904 return NULL;
1905 *vfd = *template;
1906 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001907 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001908 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001909 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001910
1911 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1912 dev->name, type_name);
1913
1914 return vfd;
1915}
1916
1917
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001918/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001919 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001920 * allocates and inits the device structs, registers i2c bus and v4l device
1921 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001922static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001923 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001924{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001925 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001926 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001927 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001928 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001929 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001930
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001931 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001932 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001933 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001934 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001935 init_waitqueue_head(&dev->wait_frame);
1936 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001937
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001938 dev->em28xx_write_regs = em28xx_write_regs;
1939 dev->em28xx_read_reg = em28xx_read_reg;
1940 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1941 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1942 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001943 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001944
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001945 em28xx_pre_card_setup(dev);
1946
1947 errCode = em28xx_config(dev);
1948 if (errCode) {
1949 em28xx_errdev("error configuring device\n");
1950 em28xx_devused &= ~(1<<dev->devno);
1951 kfree(dev);
1952 return -ENOMEM;
1953 }
1954
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001955 /* register i2c bus */
1956 em28xx_i2c_register(dev);
1957
1958 /* Do board specific init and eeprom reading */
1959 em28xx_card_setup(dev);
1960
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03001961 /* Configure audio */
1962 em28xx_audio_analog_set(dev);
1963
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001964 /* configure the device */
1965 em28xx_config_i2c(dev);
1966
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001967 /* set default norm */
1968 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001969
1970 maxw = norm_maxw(dev);
1971 maxh = norm_maxh(dev);
1972
1973 /* set default image size */
1974 dev->width = maxw;
1975 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001976 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001977 dev->hscale = 0;
1978 dev->vscale = 0;
1979 dev->ctl_input = 2;
1980
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001981 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001982
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001983 list_add_tail(&dev->devlist, &em28xx_devlist);
1984
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001985 /* allocate and fill video video_device struct */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03001986 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001987 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001988 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001989 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001990 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001991
1992 /* register v4l2 video video_device */
1993 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1994 video_nr[dev->devno]);
1995 if (retval) {
1996 em28xx_errdev("unable to register video device (error=%i).\n",
1997 retval);
1998 goto fail_unreg;
1999 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002000
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002001 /* Allocate and fill vbi video_device struct */
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03002002 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002003 /* register v4l2 vbi video_device */
2004 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2005 vbi_nr[dev->devno]) < 0) {
2006 em28xx_errdev("unable to register vbi device\n");
2007 retval = -ENODEV;
2008 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002009 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002010
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002011 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Hans Verkuil0ea6bc82008-07-26 08:26:43 -03002012 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002013 if (NULL == dev->radio_dev) {
2014 em28xx_errdev("cannot allocate video_device.\n");
2015 goto fail_unreg;
2016 }
2017 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2018 radio_nr[dev->devno]);
2019 if (retval < 0) {
2020 em28xx_errdev("can't register radio device\n");
2021 goto fail_unreg;
2022 }
2023 em28xx_info("Registered radio device as /dev/radio%d\n",
2024 dev->radio_dev->minor & 0x1f);
2025 }
2026
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002027 /* init video dma queues */
2028 INIT_LIST_HEAD(&dev->vidq.active);
2029 INIT_LIST_HEAD(&dev->vidq.queued);
2030
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002031
Sascha Sommer5a804152007-11-03 21:22:38 -03002032 if (dev->has_msp34xx) {
2033 /* Send a reset to other chips via gpio */
2034 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2035 msleep(3);
2036 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2037 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002038 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002039
Sascha Sommer5a804152007-11-03 21:22:38 -03002040 video_mux(dev, 0);
2041
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002042 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2043 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2044 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002045
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002046 mutex_lock(&em28xx_extension_devlist_lock);
2047 if (!list_empty(&em28xx_extension_devlist)) {
2048 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2049 if (ops->id)
2050 ops->init(dev);
2051 }
2052 }
2053 mutex_unlock(&em28xx_extension_devlist_lock);
2054
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002055 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002056
2057fail_unreg:
2058 em28xx_release_resources(dev);
2059 mutex_unlock(&dev->lock);
2060 kfree(dev);
2061 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002062}
2063
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002064#if defined(CONFIG_MODULES) && defined(MODULE)
2065static void request_module_async(struct work_struct *work)
2066{
2067 struct em28xx *dev = container_of(work,
2068 struct em28xx, request_module_wk);
2069
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002070 if (dev->has_audio_class)
2071 request_module("snd-usb-audio");
2072 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002073 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002074
2075 if (dev->has_dvb)
2076 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002077}
2078
2079static void request_modules(struct em28xx *dev)
2080{
2081 INIT_WORK(&dev->request_module_wk, request_module_async);
2082 schedule_work(&dev->request_module_wk);
2083}
2084#else
2085#define request_modules(dev)
2086#endif /* CONFIG_MODULES */
2087
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002088/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002089 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002090 * checks for supported devices
2091 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002092static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093 const struct usb_device_id *id)
2094{
2095 const struct usb_endpoint_descriptor *endpoint;
2096 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002097 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002098 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002099 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002100 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002101
2102 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002103 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2104
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002105 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002106 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2107 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002108
2109 /* Don't register audio interfaces */
2110 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002111 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002112 udev->descriptor.idVendor,
2113 udev->descriptor.idProduct,
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002114 ifnum,
2115 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002116
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002117 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002118 return -ENODEV;
2119 }
2120
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002121 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002122 udev->descriptor.idVendor,
2123 udev->descriptor.idProduct,
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002124 ifnum,
2125 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002126
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002127 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2128
Michael Opdenacker59c51592007-05-09 08:57:56 +02002129 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002130 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2131 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002132 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002133 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002134 return -ENODEV;
2135 }
2136 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002137 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002138 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002139 return -ENODEV;
2140 }
2141
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002142 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002143 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2144 EM28XX_MAXBOARDS);
2145 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002146 return -ENOMEM;
2147 }
2148
2149 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002150 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002151 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002152 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002153 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002154 return -ENOMEM;
2155 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002156
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002157 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002158 dev->devno = nr;
2159 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002160 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002161
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002162 /* Checks if audio is provided by some interface */
2163 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2164 uif = udev->config->interface[i];
2165 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2166 dev->has_audio_class = 1;
2167 break;
2168 }
2169 }
2170
2171 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2172 dev->has_audio_class ? "Has" : "Doesn't have");
2173
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002174 /* compute alternate max packet sizes */
2175 uif = udev->actconfig->interface[0];
2176
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002177 dev->num_alt = uif->num_altsetting;
2178 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2179/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2180 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2181
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002182 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002183 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002184 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002185 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002186 return -ENOMEM;
2187 }
2188
2189 for (i = 0; i < dev->num_alt ; i++) {
2190 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2191 wMaxPacketSize);
2192 dev->alt_max_pkt_size[i] =
2193 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002194 em28xx_info("Alternate setting %i, max size= %i\n", i,
2195 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002196 }
2197
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002198 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002199 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002200
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002201 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002202 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002203 if (retval)
2204 return retval;
2205
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002206 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002207
2208 /* save our data pointer in this interface device */
2209 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002210
2211 request_modules(dev);
2212
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002213 return 0;
2214}
2215
2216/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002217 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002218 * called when the device gets diconencted
2219 * video device will be unregistered on v4l2_close in case it is still open
2220 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002221static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002222{
Sascha Sommer5a804152007-11-03 21:22:38 -03002223 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002224 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002225
2226 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002227 usb_set_intfdata(interface, NULL);
2228
2229 if (!dev)
2230 return;
2231
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002232 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002233
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002234 /* wait until all current v4l2 io is finished then deallocate
2235 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002236 mutex_lock(&dev->lock);
2237
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002238 wake_up_interruptible_all(&dev->open);
2239
2240 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002241 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002242 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002243 "deallocation are deferred on close.\n",
2244 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2245
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002246 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002247 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002248 dev->state |= DEV_DISCONNECTED;
2249 wake_up_interruptible(&dev->wait_frame);
2250 wake_up_interruptible(&dev->wait_stream);
2251 } else {
2252 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002253 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002254 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002255 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002256
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002257 mutex_lock(&em28xx_extension_devlist_lock);
2258 if (!list_empty(&em28xx_extension_devlist)) {
2259 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2260 ops->fini(dev);
2261 }
2262 }
2263 mutex_unlock(&em28xx_extension_devlist_lock);
2264
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002265 if (!dev->users) {
2266 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002267 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002268 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002269}
2270
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002271static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002272 .name = "em28xx",
2273 .probe = em28xx_usb_probe,
2274 .disconnect = em28xx_usb_disconnect,
2275 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002276};
2277
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002278static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002279{
2280 int result;
2281
2282 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002283 (EM28XX_VERSION_CODE >> 16) & 0xff,
2284 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002285#ifdef SNAPSHOT
2286 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2287 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2288#endif
2289
2290 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002291 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002292 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002293 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002294 " usb_register failed. Error number %d.\n", result);
2295
2296 return result;
2297}
2298
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002299static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002300{
2301 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002302 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002303}
2304
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002305module_init(em28xx_module_init);
2306module_exit(em28xx_module_exit);