blob: 4abe6701a7700894624b17ad7db40547248b2f67 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -02009 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080012 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020031#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080034#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080037
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080038#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020039#include <media/v4l2-common.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030040#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030041#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080043#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030045 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_NAME "em28xx"
49#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030050#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080052#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080053 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020055 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
57MODULE_AUTHOR(DRIVER_AUTHOR);
58MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_LICENSE("GPL");
60
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080061static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080062
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080063static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020064static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030065static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
66static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
67
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080068module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020069module_param_array(video_nr, int, NULL, 0444);
70module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030071module_param_array(radio_nr, int, NULL, 0444);
72MODULE_PARM_DESC(card, "card type");
73MODULE_PARM_DESC(video_nr, "video device numbers");
74MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
75MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080076
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080077static unsigned int video_debug = 0;
78module_param(video_debug,int,0644);
79MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
80
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020081/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
82static unsigned long em28xx_devused;
83
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080084/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020085/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080086static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080087 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020088 .id = V4L2_CID_AUDIO_VOLUME,
89 .type = V4L2_CTRL_TYPE_INTEGER,
90 .name = "Volume",
91 .minimum = 0x0,
92 .maximum = 0x1f,
93 .step = 0x1,
94 .default_value = 0x1f,
95 .flags = 0,
96 },{
97 .id = V4L2_CID_AUDIO_MUTE,
98 .type = V4L2_CTRL_TYPE_BOOLEAN,
99 .name = "Mute",
100 .minimum = 0,
101 .maximum = 1,
102 .step = 1,
103 .default_value = 1,
104 .flags = 0,
105 }
106};
107
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800108static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800109
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800110
111/********************* v4l2 interface ******************************************/
112
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800113/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800114 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800115 * inits registers with sane defaults
116 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800117static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800118{
119
120 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300121 if (!dev->is_em2800)
122 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800123
124 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200125
Markus Rechberger9475fb12006-02-27 00:07:34 -0300126/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
127/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200128 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
129
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800130 dev->mute = 1; /* maybe not the right place... */
131 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300132
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800133 em28xx_outfmt_set_yuv422(dev);
134 em28xx_colorlevels_set_default(dev);
135 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800136
137 return 0;
138}
139
140/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800141 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800142 * configure i2c attached devices
143 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800144static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800145{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300146 struct v4l2_routing route;
147
148 route.input = INPUT(dev->ctl_input)->vmux;
149 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100150 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300151 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300152 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800153}
154
155/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800156 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800157 * prepare queues for incoming and outgoing frames
158 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800159static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800160{
161 u32 i;
162
163 INIT_LIST_HEAD(&dev->inqueue);
164 INIT_LIST_HEAD(&dev->outqueue);
165
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800166 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800167 dev->frame[i].state = F_UNUSED;
168 dev->frame[i].buf.bytesused = 0;
169 }
170}
171
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800172static void video_mux(struct em28xx *dev, int index)
173{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300174 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800175
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300176 route.input = INPUT(index)->vmux;
177 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800178 dev->ctl_input = index;
179 dev->ctl_ainput = INPUT(index)->amux;
180
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300181 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800182
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800183 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200184 if (dev->i2s_speed)
185 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300186 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300187 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300188 /* Note: this is msp3400 specific */
189 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800190 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300191
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300192 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800193}
194
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300195/* Usage lock check functions */
196static int res_get(struct em28xx_fh *fh)
197{
198 struct em28xx *dev = fh->dev;
199 int rc = 0;
200
201 /* This instance already has stream_on */
202 if (fh->stream_on)
203 return rc;
204
205 mutex_lock(&dev->lock);
206
207 if (dev->stream_on)
208 rc = -EINVAL;
209 else {
210 dev->stream_on = 1;
211 fh->stream_on = 1;
212 }
213
214 mutex_unlock(&dev->lock);
215 return rc;
216}
217
218static int res_check(struct em28xx_fh *fh)
219{
220 return (fh->stream_on);
221}
222
223static void res_free(struct em28xx_fh *fh)
224{
225 struct em28xx *dev = fh->dev;
226
227 mutex_lock(&dev->lock);
228 fh->stream_on = 0;
229 dev->stream_on = 0;
230 mutex_unlock(&dev->lock);
231}
232
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800233/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300234 * em28xx_vm_open()
235 */
236static void em28xx_vm_open(struct vm_area_struct *vma)
237{
238 struct em28xx_frame_t *f = vma->vm_private_data;
239 f->vma_use_count++;
240}
241
242/*
243 * em28xx_vm_close()
244 */
245static void em28xx_vm_close(struct vm_area_struct *vma)
246{
247 /* NOTE: buffers are not freed here */
248 struct em28xx_frame_t *f = vma->vm_private_data;
249
250 if (f->vma_use_count)
251 f->vma_use_count--;
252}
253
254static struct vm_operations_struct em28xx_vm_ops = {
255 .open = em28xx_vm_open,
256 .close = em28xx_vm_close,
257};
258
259
260/*
261 * em28xx_get_ctrl()
262 * return the current saturation, brightness or contrast, mute state
263 */
264static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
265{
266 switch (ctrl->id) {
267 case V4L2_CID_AUDIO_MUTE:
268 ctrl->value = dev->mute;
269 return 0;
270 case V4L2_CID_AUDIO_VOLUME:
271 ctrl->value = dev->volume;
272 return 0;
273 default:
274 return -EINVAL;
275 }
276}
277
278/*
279 * em28xx_set_ctrl()
280 * mute or set new saturation, brightness or contrast
281 */
282static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
283{
284 switch (ctrl->id) {
285 case V4L2_CID_AUDIO_MUTE:
286 if (ctrl->value != dev->mute) {
287 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300288 return em28xx_audio_analog_set(dev);
289 }
290 return 0;
291 case V4L2_CID_AUDIO_VOLUME:
292 dev->volume = ctrl->value;
293 return em28xx_audio_analog_set(dev);
294 default:
295 return -EINVAL;
296 }
297}
298
299/*
300 * em28xx_stream_interrupt()
301 * stops streaming
302 */
303static int em28xx_stream_interrupt(struct em28xx *dev)
304{
305 int rc = 0;
306
307 /* stop reading from the device */
308
309 dev->stream = STREAM_INTERRUPT;
310 rc = wait_event_timeout(dev->wait_stream,
311 (dev->stream == STREAM_OFF) ||
312 (dev->state & DEV_DISCONNECTED),
313 EM28XX_URB_TIMEOUT);
314
315 if (rc) {
316 dev->state |= DEV_MISCONFIGURED;
317 em28xx_videodbg("device is misconfigured; close and "
318 "open /dev/video%d again\n",
319 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
320 return rc;
321 }
322
323 return 0;
324}
325
326
327static int check_dev(struct em28xx *dev)
328{
329 if (dev->state & DEV_DISCONNECTED) {
330 em28xx_errdev("v4l2 ioctl: device not present\n");
331 return -ENODEV;
332 }
333
334 if (dev->state & DEV_MISCONFIGURED) {
335 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
336 "close and open it again\n");
337 return -EIO;
338 }
339 return 0;
340}
341
342static void get_scale(struct em28xx *dev,
343 unsigned int width, unsigned int height,
344 unsigned int *hscale, unsigned int *vscale)
345{
346 unsigned int maxw = norm_maxw(dev);
347 unsigned int maxh = norm_maxh(dev);
348
349 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
350 if (*hscale >= 0x4000)
351 *hscale = 0x3fff;
352
353 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
354 if (*vscale >= 0x4000)
355 *vscale = 0x3fff;
356}
357
358/* ------------------------------------------------------------------
359 IOCTL vidioc handling
360 ------------------------------------------------------------------*/
361
362static int vidioc_g_fmt_cap(struct file *file, void *priv,
363 struct v4l2_format *f)
364{
365 struct em28xx_fh *fh = priv;
366 struct em28xx *dev = fh->dev;
367
368 mutex_lock(&dev->lock);
369
370 f->fmt.pix.width = dev->width;
371 f->fmt.pix.height = dev->height;
372 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
373 f->fmt.pix.bytesperline = dev->bytesperline;
374 f->fmt.pix.sizeimage = dev->frame_size;
375 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
376
377 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
378 f->fmt.pix.field = dev->interlaced ?
379 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
380
381 mutex_unlock(&dev->lock);
382 return 0;
383}
384
385static int vidioc_try_fmt_cap(struct file *file, void *priv,
386 struct v4l2_format *f)
387{
388 struct em28xx_fh *fh = priv;
389 struct em28xx *dev = fh->dev;
390 int width = f->fmt.pix.width;
391 int height = f->fmt.pix.height;
392 unsigned int maxw = norm_maxw(dev);
393 unsigned int maxh = norm_maxh(dev);
394 unsigned int hscale, vscale;
395
396 /* width must even because of the YUYV format
397 height must be even because of interlacing */
398 height &= 0xfffe;
399 width &= 0xfffe;
400
401 if (height < 32)
402 height = 32;
403 if (height > maxh)
404 height = maxh;
405 if (width < 48)
406 width = 48;
407 if (width > maxw)
408 width = maxw;
409
410 mutex_lock(&dev->lock);
411
412 if (dev->is_em2800) {
413 /* the em2800 can only scale down to 50% */
414 if (height % (maxh / 2))
415 height = maxh;
416 if (width % (maxw / 2))
417 width = maxw;
418 /* according to empiatech support */
419 /* the MaxPacketSize is to small to support */
420 /* framesizes larger than 640x480 @ 30 fps */
421 /* or 640x576 @ 25 fps. As this would cut */
422 /* of a part of the image we prefer */
423 /* 360x576 or 360x480 for now */
424 if (width == maxw && height == maxh)
425 width /= 2;
426 }
427
428 get_scale(dev, width, height, &hscale, &vscale);
429
430 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
431 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
432
433 f->fmt.pix.width = width;
434 f->fmt.pix.height = height;
435 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
436 f->fmt.pix.bytesperline = width * 2;
437 f->fmt.pix.sizeimage = width * 2 * height;
438 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
439 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
440
441 mutex_unlock(&dev->lock);
442 return 0;
443}
444
445static int vidioc_s_fmt_cap(struct file *file, void *priv,
446 struct v4l2_format *f)
447{
448 struct em28xx_fh *fh = priv;
449 struct em28xx *dev = fh->dev;
450 int rc, i;
451
452 rc = check_dev(dev);
453 if (rc < 0)
454 return rc;
455
456 vidioc_try_fmt_cap(file, priv, f);
457
458 mutex_lock(&dev->lock);
459
460 for (i = 0; i < dev->num_frames; i++)
461 if (dev->frame[i].vma_use_count) {
462 em28xx_videodbg("VIDIOC_S_FMT failed. "
463 "Unmap the buffers first.\n");
464 rc = -EINVAL;
465 goto err;
466 }
467
468 /* stop io in case it is already in progress */
469 if (dev->stream == STREAM_ON) {
470 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
471 rc = em28xx_stream_interrupt(dev);
472 if (rc < 0)
473 goto err;
474 }
475
476 em28xx_release_buffers(dev);
477 dev->io = IO_NONE;
478
479 /* set new image size */
480 dev->width = f->fmt.pix.width;
481 dev->height = f->fmt.pix.height;
482 dev->frame_size = dev->width * dev->height * 2;
483 dev->field_size = dev->frame_size >> 1;
484 dev->bytesperline = dev->width * 2;
485 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
486
487 /* FIXME: This is really weird! Why capture is starting with
488 this ioctl ???
489 */
490 em28xx_uninit_isoc(dev);
491 em28xx_set_alternate(dev);
492 em28xx_capture_start(dev, 1);
493 em28xx_resolution_set(dev);
494 em28xx_init_isoc(dev);
495 rc = 0;
496
497err:
498 mutex_unlock(&dev->lock);
499 return rc;
500}
501
502static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
503{
504 struct em28xx_fh *fh = priv;
505 struct em28xx *dev = fh->dev;
506 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300507 int rc;
508
509 rc = check_dev(dev);
510 if (rc < 0)
511 return rc;
512
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300513 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300514 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300515 mutex_unlock(&dev->lock);
516
517 /* Adjusts width/height, if needed */
518 f.fmt.pix.width = dev->width;
519 f.fmt.pix.height = dev->height;
520 vidioc_try_fmt_cap(file, priv, &f);
521
522 mutex_lock(&dev->lock);
523
524 /* set new image size */
525 dev->width = f.fmt.pix.width;
526 dev->height = f.fmt.pix.height;
527 dev->frame_size = dev->width * dev->height * 2;
528 dev->field_size = dev->frame_size >> 1;
529 dev->bytesperline = dev->width * 2;
530 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
531
532 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300533 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300534
535 mutex_unlock(&dev->lock);
536 return 0;
537}
538
539static const char *iname[] = {
540 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
541 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
542 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
543 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
544 [EM28XX_VMUX_SVIDEO] = "S-Video",
545 [EM28XX_VMUX_TELEVISION] = "Television",
546 [EM28XX_VMUX_CABLE] = "Cable TV",
547 [EM28XX_VMUX_DVB] = "DVB",
548 [EM28XX_VMUX_DEBUG] = "for debug only",
549};
550
551static int vidioc_enum_input(struct file *file, void *priv,
552 struct v4l2_input *i)
553{
554 struct em28xx_fh *fh = priv;
555 struct em28xx *dev = fh->dev;
556 unsigned int n;
557
558 n = i->index;
559 if (n >= MAX_EM28XX_INPUT)
560 return -EINVAL;
561 if (0 == INPUT(n)->type)
562 return -EINVAL;
563
564 i->index = n;
565 i->type = V4L2_INPUT_TYPE_CAMERA;
566
567 strcpy(i->name, iname[INPUT(n)->type]);
568
569 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
570 (EM28XX_VMUX_CABLE == INPUT(n)->type))
571 i->type = V4L2_INPUT_TYPE_TUNER;
572
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300573 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300574
575 return 0;
576}
577
578static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
579{
580 struct em28xx_fh *fh = priv;
581 struct em28xx *dev = fh->dev;
582
583 *i = dev->ctl_input;
584
585 return 0;
586}
587
588static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
589{
590 struct em28xx_fh *fh = priv;
591 struct em28xx *dev = fh->dev;
592 int rc;
593
594 rc = check_dev(dev);
595 if (rc < 0)
596 return rc;
597
598 if (i >= MAX_EM28XX_INPUT)
599 return -EINVAL;
600 if (0 == INPUT(i)->type)
601 return -EINVAL;
602
603 mutex_lock(&dev->lock);
604
605 video_mux(dev, i);
606
607 mutex_unlock(&dev->lock);
608 return 0;
609}
610
611static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
612{
613 struct em28xx_fh *fh = priv;
614 struct em28xx *dev = fh->dev;
615 unsigned int index = a->index;
616
617 if (a->index > 1)
618 return -EINVAL;
619
620 index = dev->ctl_ainput;
621
622 if (index == 0) {
623 strcpy(a->name, "Television");
624 } else {
625 strcpy(a->name, "Line In");
626 }
627 a->capability = V4L2_AUDCAP_STEREO;
628 a->index = index;
629
630 return 0;
631}
632
633static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
634{
635 struct em28xx_fh *fh = priv;
636 struct em28xx *dev = fh->dev;
637
638 if (a->index != dev->ctl_ainput)
639 return -EINVAL;
640
641 return 0;
642}
643
644static int vidioc_queryctrl(struct file *file, void *priv,
645 struct v4l2_queryctrl *qc)
646{
647 struct em28xx_fh *fh = priv;
648 struct em28xx *dev = fh->dev;
649 int id = qc->id;
650 int i;
651 int rc;
652
653 rc = check_dev(dev);
654 if (rc < 0)
655 return rc;
656
657 memset(qc, 0, sizeof(*qc));
658
659 qc->id = id;
660
661 if (!dev->has_msp34xx) {
662 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
663 if (qc->id && qc->id == em28xx_qctrl[i].id) {
664 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
665 return 0;
666 }
667 }
668 }
669 mutex_lock(&dev->lock);
670 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
671 mutex_unlock(&dev->lock);
672
673 if (qc->type)
674 return 0;
675 else
676 return -EINVAL;
677}
678
679static int vidioc_g_ctrl(struct file *file, void *priv,
680 struct v4l2_control *ctrl)
681{
682 struct em28xx_fh *fh = priv;
683 struct em28xx *dev = fh->dev;
684 int rc;
685
686 rc = check_dev(dev);
687 if (rc < 0)
688 return rc;
689 mutex_lock(&dev->lock);
690
691 if (!dev->has_msp34xx)
692 rc = em28xx_get_ctrl(dev, ctrl);
693 else
694 rc = -EINVAL;
695
696 if (rc == -EINVAL) {
697 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
698 rc = 0;
699 }
700
701 mutex_unlock(&dev->lock);
702 return rc;
703}
704
705static int vidioc_s_ctrl(struct file *file, void *priv,
706 struct v4l2_control *ctrl)
707{
708 struct em28xx_fh *fh = priv;
709 struct em28xx *dev = fh->dev;
710 u8 i;
711 int rc;
712
713 rc = check_dev(dev);
714 if (rc < 0)
715 return rc;
716
717 mutex_lock(&dev->lock);
718
719 if (dev->has_msp34xx)
720 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
721 else {
722 rc = 1;
723 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
724 if (ctrl->id == em28xx_qctrl[i].id) {
725 if (ctrl->value < em28xx_qctrl[i].minimum ||
726 ctrl->value > em28xx_qctrl[i].maximum) {
727 rc = -ERANGE;
728 break;
729 }
730
731 rc = em28xx_set_ctrl(dev, ctrl);
732 break;
733 }
734 }
735 }
736
737 /* Control not found - try to send it to the attached devices */
738 if (rc == 1) {
739 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
740 rc = 0;
741 }
742
743 mutex_unlock(&dev->lock);
744 return rc;
745}
746
747static int vidioc_g_tuner(struct file *file, void *priv,
748 struct v4l2_tuner *t)
749{
750 struct em28xx_fh *fh = priv;
751 struct em28xx *dev = fh->dev;
752 int rc;
753
754 rc = check_dev(dev);
755 if (rc < 0)
756 return rc;
757
758 if (0 != t->index)
759 return -EINVAL;
760
761 strcpy(t->name, "Tuner");
762
763 mutex_lock(&dev->lock);
764
765 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
766
767 mutex_unlock(&dev->lock);
768 return 0;
769}
770
771static int vidioc_s_tuner(struct file *file, void *priv,
772 struct v4l2_tuner *t)
773{
774 struct em28xx_fh *fh = priv;
775 struct em28xx *dev = fh->dev;
776 int rc;
777
778 rc = check_dev(dev);
779 if (rc < 0)
780 return rc;
781
782 if (0 != t->index)
783 return -EINVAL;
784
785 mutex_lock(&dev->lock);
786
787 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
788
789 mutex_unlock(&dev->lock);
790 return 0;
791}
792
793static int vidioc_g_frequency(struct file *file, void *priv,
794 struct v4l2_frequency *f)
795{
796 struct em28xx_fh *fh = priv;
797 struct em28xx *dev = fh->dev;
798
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -0300799 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300800 f->frequency = dev->ctl_freq;
801
802 return 0;
803}
804
805static int vidioc_s_frequency(struct file *file, void *priv,
806 struct v4l2_frequency *f)
807{
808 struct em28xx_fh *fh = priv;
809 struct em28xx *dev = fh->dev;
810 int rc;
811
812 rc = check_dev(dev);
813 if (rc < 0)
814 return rc;
815
816 if (0 != f->tuner)
817 return -EINVAL;
818
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -0300819 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
820 return -EINVAL;
821 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822 return -EINVAL;
823
824 mutex_lock(&dev->lock);
825
826 dev->ctl_freq = f->frequency;
827 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
828
829 mutex_unlock(&dev->lock);
830 return 0;
831}
832
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -0300833#ifdef CONFIG_VIDEO_ADV_DEBUG
834static int em28xx_reg_len(int reg)
835{
836 switch (reg) {
837 case AC97LSB_REG:
838 case HSCALELOW_REG:
839 case VSCALELOW_REG:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -0300840 return 2;
841 default:
842 return 1;
843 }
844}
845
846static int vidioc_g_register(struct file *file, void *priv,
847 struct v4l2_register *reg)
848{
849 struct em28xx_fh *fh = priv;
850 struct em28xx *dev = fh->dev;
851 int ret;
852
853 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
854 return -EINVAL;
855
856 if (em28xx_reg_len(reg->reg) == 1) {
857 ret = em28xx_read_reg(dev, reg->reg);
858 if (ret < 0)
859 return ret;
860
861 reg->val = ret;
862 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -0300863 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -0300864 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
865 reg->reg, (char *)&val, 2);
866 if (ret < 0)
867 return ret;
868
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -0300869 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -0300870 }
871
872 return 0;
873}
874
875static int vidioc_s_register(struct file *file, void *priv,
876 struct v4l2_register *reg)
877{
878 struct em28xx_fh *fh = priv;
879 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -0300880 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -0300881
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -0300882 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -0300883
884 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
885 em28xx_reg_len(reg->reg));
886}
887#endif
888
889
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300890static int vidioc_cropcap(struct file *file, void *priv,
891 struct v4l2_cropcap *cc)
892{
893 struct em28xx_fh *fh = priv;
894 struct em28xx *dev = fh->dev;
895
896 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
897 return -EINVAL;
898
899 cc->bounds.left = 0;
900 cc->bounds.top = 0;
901 cc->bounds.width = dev->width;
902 cc->bounds.height = dev->height;
903 cc->defrect = cc->bounds;
904 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
905 cc->pixelaspect.denominator = 59;
906
907 return 0;
908}
909
910static int vidioc_streamon(struct file *file, void *priv,
911 enum v4l2_buf_type type)
912{
913 struct em28xx_fh *fh = priv;
914 struct em28xx *dev = fh->dev;
915 int rc;
916
917 rc = check_dev(dev);
918 if (rc < 0)
919 return rc;
920
921 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
922 return -EINVAL;
923
924 if (list_empty(&dev->inqueue))
925 return -EINVAL;
926
927 mutex_lock(&dev->lock);
928
929 if (unlikely(res_get(fh) < 0)) {
930 mutex_unlock(&dev->lock);
931 return -EBUSY;
932 }
933
934 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
935
936 mutex_unlock(&dev->lock);
937 return 0;
938}
939
940static int vidioc_streamoff(struct file *file, void *priv,
941 enum v4l2_buf_type type)
942{
943 struct em28xx_fh *fh = priv;
944 struct em28xx *dev = fh->dev;
945 int rc;
946
947 rc = check_dev(dev);
948 if (rc < 0)
949 return rc;
950
951 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
952 return -EINVAL;
953
954 mutex_lock(&dev->lock);
955
956 if (dev->stream == STREAM_ON) {
957 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
958 rc = em28xx_stream_interrupt(dev);
959 if (rc < 0) {
960 mutex_unlock(&dev->lock);
961 return rc;
962 }
963 }
964
965 em28xx_empty_framequeues(dev);
966
967 mutex_unlock(&dev->lock);
968 return 0;
969}
970
971static int vidioc_querycap(struct file *file, void *priv,
972 struct v4l2_capability *cap)
973{
974 struct em28xx_fh *fh = priv;
975 struct em28xx *dev = fh->dev;
976
977 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
978 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
979 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
980
981 cap->version = EM28XX_VERSION_CODE;
982
983 cap->capabilities =
984 V4L2_CAP_SLICED_VBI_CAPTURE |
985 V4L2_CAP_VIDEO_CAPTURE |
986 V4L2_CAP_AUDIO |
987 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
988
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -0300989 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300990 cap->capabilities |= V4L2_CAP_TUNER;
991
992 return 0;
993}
994
995static int vidioc_enum_fmt_cap(struct file *file, void *priv,
996 struct v4l2_fmtdesc *fmtd)
997{
998 if (fmtd->index != 0)
999 return -EINVAL;
1000
1001 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1002 strcpy(fmtd->description, "Packed YUY2");
1003 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1004 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1005
1006 return 0;
1007}
1008
1009/* Sliced VBI ioctls */
1010static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1011 struct v4l2_format *f)
1012{
1013 struct em28xx_fh *fh = priv;
1014 struct em28xx *dev = fh->dev;
1015 int rc;
1016
1017 rc = check_dev(dev);
1018 if (rc < 0)
1019 return rc;
1020
1021 mutex_lock(&dev->lock);
1022
1023 f->fmt.sliced.service_set = 0;
1024
1025 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1026
1027 if (f->fmt.sliced.service_set == 0)
1028 rc = -EINVAL;
1029
1030 mutex_unlock(&dev->lock);
1031 return rc;
1032}
1033
1034static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1035 struct v4l2_format *f)
1036{
1037 struct em28xx_fh *fh = priv;
1038 struct em28xx *dev = fh->dev;
1039 int rc;
1040
1041 rc = check_dev(dev);
1042 if (rc < 0)
1043 return rc;
1044
1045 mutex_lock(&dev->lock);
1046 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1047 mutex_unlock(&dev->lock);
1048
1049 if (f->fmt.sliced.service_set == 0)
1050 return -EINVAL;
1051
1052 return 0;
1053}
1054
1055
1056static int vidioc_reqbufs(struct file *file, void *priv,
1057 struct v4l2_requestbuffers *rb)
1058{
1059 struct em28xx_fh *fh = priv;
1060 struct em28xx *dev = fh->dev;
1061 u32 i;
1062 int rc;
1063
1064 rc = check_dev(dev);
1065 if (rc < 0)
1066 return rc;
1067
1068 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1069 rb->memory != V4L2_MEMORY_MMAP)
1070 return -EINVAL;
1071
1072 if (dev->io == IO_READ) {
1073 em28xx_videodbg("method is set to read;"
1074 " close and open the device again to"
1075 " choose the mmap I/O method\n");
1076 return -EINVAL;
1077 }
1078
1079 for (i = 0; i < dev->num_frames; i++)
1080 if (dev->frame[i].vma_use_count) {
1081 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1082 "previous buffers are still mapped\n");
1083 return -EINVAL;
1084 }
1085
1086 mutex_lock(&dev->lock);
1087
1088 if (dev->stream == STREAM_ON) {
1089 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1090 rc = em28xx_stream_interrupt(dev);
1091 if (rc < 0) {
1092 mutex_unlock(&dev->lock);
1093 return rc;
1094 }
1095 }
1096
1097 em28xx_empty_framequeues(dev);
1098
1099 em28xx_release_buffers(dev);
1100 if (rb->count)
1101 rb->count = em28xx_request_buffers(dev, rb->count);
1102
1103 dev->frame_current = NULL;
1104 dev->io = rb->count ? IO_MMAP : IO_NONE;
1105
1106 mutex_unlock(&dev->lock);
1107 return 0;
1108}
1109
1110static int vidioc_querybuf(struct file *file, void *priv,
1111 struct v4l2_buffer *b)
1112{
1113 struct em28xx_fh *fh = priv;
1114 struct em28xx *dev = fh->dev;
1115 int rc;
1116
1117 rc = check_dev(dev);
1118 if (rc < 0)
1119 return rc;
1120
1121 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1122 b->index >= dev->num_frames || dev->io != IO_MMAP)
1123 return -EINVAL;
1124
1125 mutex_lock(&dev->lock);
1126
1127 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1128
1129 if (dev->frame[b->index].vma_use_count)
1130 b->flags |= V4L2_BUF_FLAG_MAPPED;
1131
1132 if (dev->frame[b->index].state == F_DONE)
1133 b->flags |= V4L2_BUF_FLAG_DONE;
1134 else if (dev->frame[b->index].state != F_UNUSED)
1135 b->flags |= V4L2_BUF_FLAG_QUEUED;
1136
1137 mutex_unlock(&dev->lock);
1138 return 0;
1139}
1140
1141static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1142{
1143 struct em28xx_fh *fh = priv;
1144 struct em28xx *dev = fh->dev;
1145 unsigned long lock_flags;
1146 int rc;
1147
1148 rc = check_dev(dev);
1149 if (rc < 0)
1150 return rc;
1151
1152 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP ||
1153 b->index >= dev->num_frames)
1154 return -EINVAL;
1155
1156 if (dev->frame[b->index].state != F_UNUSED)
1157 return -EAGAIN;
1158
1159 dev->frame[b->index].state = F_QUEUED;
1160
1161 /* add frame to fifo */
1162 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1163 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1164 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1165
1166 return 0;
1167}
1168
1169static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1170{
1171 struct em28xx_fh *fh = priv;
1172 struct em28xx *dev = fh->dev;
1173 int rc;
1174 struct em28xx_frame_t *f;
1175 unsigned long lock_flags;
1176
1177 rc = check_dev(dev);
1178 if (rc < 0)
1179 return rc;
1180
1181 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
1182 return -EINVAL;
1183
1184 if (list_empty(&dev->outqueue)) {
1185 if (dev->stream == STREAM_OFF)
1186 return -EINVAL;
1187
1188 if (file->f_flags & O_NONBLOCK)
1189 return -EAGAIN;
1190
1191 rc = wait_event_interruptible(dev->wait_frame,
1192 (!list_empty(&dev->outqueue)) ||
1193 (dev->state & DEV_DISCONNECTED));
1194 if (rc)
1195 return rc;
1196
1197 if (dev->state & DEV_DISCONNECTED)
1198 return -ENODEV;
1199 }
1200
1201 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1202 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1203 list_del(dev->outqueue.next);
1204 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1205
1206 f->state = F_UNUSED;
1207 memcpy(b, &f->buf, sizeof(*b));
1208
1209 if (f->vma_use_count)
1210 b->flags |= V4L2_BUF_FLAG_MAPPED;
1211
1212 return 0;
1213}
1214
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001215/* ----------------------------------------------------------- */
1216/* RADIO ESPECIFIC IOCTLS */
1217/* ----------------------------------------------------------- */
1218
1219static int radio_querycap(struct file *file, void *priv,
1220 struct v4l2_capability *cap)
1221{
1222 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1223
1224 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1225 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1226 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1227
1228 cap->version = EM28XX_VERSION_CODE;
1229 cap->capabilities = V4L2_CAP_TUNER;
1230 return 0;
1231}
1232
1233static int radio_g_tuner(struct file *file, void *priv,
1234 struct v4l2_tuner *t)
1235{
1236 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1237
1238 if (unlikely(t->index > 0))
1239 return -EINVAL;
1240
1241 strcpy(t->name, "Radio");
1242 t->type = V4L2_TUNER_RADIO;
1243
1244 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1245 return 0;
1246}
1247
1248static int radio_enum_input(struct file *file, void *priv,
1249 struct v4l2_input *i)
1250{
1251 if (i->index != 0)
1252 return -EINVAL;
1253 strcpy(i->name, "Radio");
1254 i->type = V4L2_INPUT_TYPE_TUNER;
1255
1256 return 0;
1257}
1258
1259static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1260{
1261 if (unlikely(a->index))
1262 return -EINVAL;
1263
1264 strcpy(a->name, "Radio");
1265 return 0;
1266}
1267
1268static int radio_s_tuner(struct file *file, void *priv,
1269 struct v4l2_tuner *t)
1270{
1271 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1272
1273 if (0 != t->index)
1274 return -EINVAL;
1275
1276 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1277
1278 return 0;
1279}
1280
1281static int radio_s_audio(struct file *file, void *fh,
1282 struct v4l2_audio *a)
1283{
1284 return 0;
1285}
1286
1287static int radio_s_input(struct file *file, void *fh, unsigned int i)
1288{
1289 return 0;
1290}
1291
1292static int radio_queryctrl(struct file *file, void *priv,
1293 struct v4l2_queryctrl *qc)
1294{
1295 int i;
1296
1297 if (qc->id < V4L2_CID_BASE ||
1298 qc->id >= V4L2_CID_LASTP1)
1299 return -EINVAL;
1300
1301 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1302 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1303 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1304 return 0;
1305 }
1306 }
1307
1308 return -EINVAL;
1309}
1310
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001311/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001312 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001313 * inits the device and starts isoc transfer
1314 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001315static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001316{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001317 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001318 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001319 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001320 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001321
Trent Piephoa991f442007-10-10 05:37:43 -03001322 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001323 if (h->vdev->minor == minor) {
1324 dev = h;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001325 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1326 }
1327 if (h->vbi_dev->minor == minor) {
1328 dev = h;
1329 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001330 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001331 if (h->radio_dev &&
1332 h->radio_dev->minor == minor) {
1333 radio = 1;
1334 dev = h;
1335 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001336 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001337 if (NULL == dev)
1338 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001339
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001340 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1341 minor,v4l2_type_names[dev->type],dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001342
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001343 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001344
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001345 if (!fh) {
1346 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1347 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001348 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001349 mutex_lock(&dev->lock);
1350 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001351 fh->radio = radio;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001352 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001353
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001354 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001355 dev->width = norm_maxw(dev);
1356 dev->height = norm_maxh(dev);
1357 dev->frame_size = dev->width * dev->height * 2;
1358 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1359 dev->bytesperline = dev->width * 2;
1360 dev->hscale = 0;
1361 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001362
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001363 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001364 em28xx_capture_start(dev, 1);
1365 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001366
Markus Rechbergere5d4a562006-02-07 06:49:13 -02001367
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001368 /* start the transfer */
1369 errCode = em28xx_init_isoc(dev);
1370 if (errCode)
1371 goto err;
1372
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001373 em28xx_empty_framequeues(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001374 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001375 if (fh->radio) {
1376 em28xx_videodbg("video_open: setting radio device\n");
1377 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1378 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001379
1380 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001381
Ingo Molnar3593cab2006-02-07 06:49:14 -02001382err:
1383 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001384 return errCode;
1385}
1386
1387/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001388 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001389 * unregisters the v4l2,i2c and usb devices
1390 * called when the device gets disconected or at module unload
1391*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001392static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001393{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001394
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001395 /*FIXME: I2C IR should be disconnected */
1396
1397 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1398 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1399 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001400 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001401 if (dev->radio_dev) {
1402 if (-1 != dev->radio_dev->minor)
1403 video_unregister_device(dev->radio_dev);
1404 else
1405 video_device_release(dev->radio_dev);
1406 dev->radio_dev = NULL;
1407 }
1408 if (dev->vbi_dev) {
1409 if (-1 != dev->vbi_dev->minor)
1410 video_unregister_device(dev->vbi_dev);
1411 else
1412 video_device_release(dev->vbi_dev);
1413 dev->vbi_dev = NULL;
1414 }
1415 if (dev->vdev) {
1416 if (-1 != dev->vdev->minor)
1417 video_unregister_device(dev->vdev);
1418 else
1419 video_device_release(dev->vdev);
1420 dev->vdev = NULL;
1421 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001422 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001423 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001424
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001425 /* Mark device as unused */
1426 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001427}
1428
1429/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001430 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001431 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1432 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001433static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001434{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001435 struct em28xx_fh *fh = filp->private_data;
1436 struct em28xx *dev = fh->dev;
1437 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001438
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001439 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001440
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001441
1442 if (res_check(fh))
1443 res_free(fh);
1444
Ingo Molnar3593cab2006-02-07 06:49:14 -02001445 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001446
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001447 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001448 em28xx_uninit_isoc(dev);
1449 em28xx_release_buffers(dev);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001450 dev->io = IO_NONE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001451
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001452 /* the device is already disconnect,
1453 free the remaining resources */
1454 if (dev->state & DEV_DISCONNECTED) {
1455 em28xx_release_resources(dev);
1456 mutex_unlock(&dev->lock);
1457 kfree(dev);
1458 return 0;
1459 }
1460
1461 /* set alternate 0 */
1462 dev->alt = 0;
1463 em28xx_videodbg("setting alternate 0\n");
1464 errCode = usb_set_interface(dev->udev, 0, 0);
1465 if (errCode < 0) {
1466 em28xx_errdev("cannot change alternate number to "
1467 "0 (error=%i)\n", errCode);
1468 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001469 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001470 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001471 dev->users--;
1472 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001473 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001474 return 0;
1475}
1476
1477/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001478 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001479 * will allocate buffers when called for the first time
1480 */
1481static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001482em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001483 loff_t * f_pos)
1484{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001485 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001486 unsigned long lock_flags;
1487 int ret = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001488 struct em28xx_fh *fh = filp->private_data;
1489 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001490
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001491 /* FIXME: read() is not prepared to allow changing the video
1492 resolution while streaming. Seems a bug at em28xx_set_fmt
1493 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001494
1495 if (unlikely(res_get(fh) < 0))
1496 return -EBUSY;
1497
Sascha Sommer5a804152007-11-03 21:22:38 -03001498 mutex_lock(&dev->lock);
1499
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001500 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001501 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001502
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001503 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1504 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1505 em28xx_videodbg("not supported yet! ...\n");
1506 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001507 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001508 return -EFAULT;
1509 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001510 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001511 return (1);
1512 }
1513 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1514 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1515 em28xx_videodbg("not supported yet! ...\n");
1516 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001517 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001518 return -EFAULT;
1519 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001520 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001521 return (1);
1522 }
1523
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001524 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001525 em28xx_videodbg("device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001526 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001527 return -ENODEV;
1528 }
1529
1530 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001531 em28xx_videodbg("device misconfigured; close and open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001532 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001533 return -EIO;
1534 }
1535
1536 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001537 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001538 " the device again to choose the read method\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001539 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001540 return -EINVAL;
1541 }
1542
1543 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001544 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1545 em28xx_errdev("read failed, not enough memory\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001546 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001547 return -ENOMEM;
1548 }
1549 dev->io = IO_READ;
1550 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001551 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001552 }
1553
1554 if (!count) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001555 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001556 return 0;
1557 }
1558
1559 if (list_empty(&dev->outqueue)) {
1560 if (filp->f_flags & O_NONBLOCK) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001561 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001562 return -EAGAIN;
1563 }
1564 ret = wait_event_interruptible
1565 (dev->wait_frame,
1566 (!list_empty(&dev->outqueue)) ||
1567 (dev->state & DEV_DISCONNECTED));
1568 if (ret) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001569 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001570 return ret;
1571 }
1572 if (dev->state & DEV_DISCONNECTED) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001573 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001574 return -ENODEV;
1575 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001576 dev->video_bytesread = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001577 }
1578
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001579 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001580
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001581 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001582
1583 if (count > f->buf.length)
1584 count = f->buf.length;
1585
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001586 if ((dev->video_bytesread + count) > dev->frame_size)
1587 count = dev->frame_size - dev->video_bytesread;
1588
1589 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1590 em28xx_err("Error while copying to user\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001591 return -EFAULT;
1592 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001593 dev->video_bytesread += count;
1594
1595 if (dev->video_bytesread == dev->frame_size) {
1596 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1597 list_for_each_entry(i, &dev->outqueue, frame)
1598 i->state = F_UNUSED;
1599 INIT_LIST_HEAD(&dev->outqueue);
1600 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1601
1602 em28xx_queue_unusedframes(dev);
1603 dev->video_bytesread = 0;
1604 }
1605
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001606 *f_pos += count;
1607
Sascha Sommer5a804152007-11-03 21:22:38 -03001608 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001609
1610 return count;
1611}
1612
1613/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001614 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001615 * will allocate buffers when called for the first time
1616 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001617static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001618{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001619 unsigned int mask = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001620 struct em28xx_fh *fh = filp->private_data;
1621 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001622
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001623 if (unlikely(res_get(fh) < 0))
1624 return POLLERR;
1625
Sascha Sommer5a804152007-11-03 21:22:38 -03001626 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001627
1628 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001629 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001630 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001631 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001632 } else {
1633 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001634 if (!em28xx_request_buffers
1635 (dev, EM28XX_NUM_READ_FRAMES)) {
1636 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001637 ("poll() failed, not enough memory\n");
1638 } else {
1639 dev->io = IO_READ;
1640 dev->stream = STREAM_ON;
1641 }
1642 }
1643
1644 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001645 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001646 poll_wait(filp, &dev->wait_frame, wait);
1647
1648 if (!list_empty(&dev->outqueue))
1649 mask |= POLLIN | POLLRDNORM;
1650
Sascha Sommer5a804152007-11-03 21:22:38 -03001651 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001652
1653 return mask;
1654 }
1655 }
1656
Sascha Sommer5a804152007-11-03 21:22:38 -03001657 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001658 return POLLERR;
1659}
1660
1661/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001662 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001663 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001664static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001665{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001666 struct em28xx_fh *fh = filp->private_data;
1667 struct em28xx *dev = fh->dev;
1668 unsigned long size = vma->vm_end - vma->vm_start;
1669 unsigned long start = vma->vm_start;
1670 void *pos;
1671 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -08001672
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001673 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001674 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001675
1676 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001677
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001679 em28xx_videodbg("mmap: device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001680 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001681 return -ENODEV;
1682 }
1683
1684 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001685 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001686 "open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001687 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001688 return -EIO;
1689 }
1690
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001691 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001692 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001693 return -EINVAL;
1694 }
1695
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001696 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1697 size = PAGE_ALIGN(dev->frame[0].buf.length);
1698
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001699 for (i = 0; i < dev->num_frames; i++) {
1700 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1701 break;
1702 }
1703 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001704 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001705 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706 return -EINVAL;
1707 }
1708
1709 /* VM_IO is eventually going to replace PageReserved altogether */
1710 vma->vm_flags |= VM_IO;
1711 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1712
Sascha Sommer3639c862005-12-12 00:37:30 -08001713 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -08001715 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1716 em28xx_videodbg("mmap: vm_insert_page failed\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001717 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718 return -EAGAIN;
1719 }
1720 start += PAGE_SIZE;
1721 pos += PAGE_SIZE;
1722 size -= PAGE_SIZE;
1723 }
1724
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001725 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001726 vma->vm_private_data = &dev->frame[i];
1727
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001728 em28xx_vm_open(vma);
Sascha Sommer5a804152007-11-03 21:22:38 -03001729 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730 return 0;
1731}
1732
Arjan van de Venfa027c22007-02-12 00:55:33 -08001733static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001734 .owner = THIS_MODULE,
1735 .open = em28xx_v4l2_open,
1736 .release = em28xx_v4l2_close,
1737 .read = em28xx_v4l2_read,
1738 .poll = em28xx_v4l2_poll,
1739 .mmap = em28xx_v4l2_mmap,
1740 .ioctl = video_ioctl2,
1741 .llseek = no_llseek,
1742 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001743};
1744
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001745static const struct file_operations radio_fops = {
1746 .owner = THIS_MODULE,
1747 .open = em28xx_v4l2_open,
1748 .release = em28xx_v4l2_close,
1749 .ioctl = video_ioctl2,
1750 .compat_ioctl = v4l_compat_ioctl32,
1751 .llseek = no_llseek,
1752};
1753
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001754static const struct video_device em28xx_video_template = {
1755 .fops = &em28xx_v4l_fops,
1756 .release = video_device_release,
1757
1758 .minor = -1,
1759 .vidioc_querycap = vidioc_querycap,
1760 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1761 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1762 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1763 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1764 .vidioc_g_audio = vidioc_g_audio,
1765 .vidioc_s_audio = vidioc_s_audio,
1766 .vidioc_cropcap = vidioc_cropcap,
1767
1768 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1769 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1770 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1771
1772 .vidioc_reqbufs = vidioc_reqbufs,
1773 .vidioc_querybuf = vidioc_querybuf,
1774 .vidioc_qbuf = vidioc_qbuf,
1775 .vidioc_dqbuf = vidioc_dqbuf,
1776 .vidioc_s_std = vidioc_s_std,
1777 .vidioc_enum_input = vidioc_enum_input,
1778 .vidioc_g_input = vidioc_g_input,
1779 .vidioc_s_input = vidioc_s_input,
1780 .vidioc_queryctrl = vidioc_queryctrl,
1781 .vidioc_g_ctrl = vidioc_g_ctrl,
1782 .vidioc_s_ctrl = vidioc_s_ctrl,
1783 .vidioc_streamon = vidioc_streamon,
1784 .vidioc_streamoff = vidioc_streamoff,
1785 .vidioc_g_tuner = vidioc_g_tuner,
1786 .vidioc_s_tuner = vidioc_s_tuner,
1787 .vidioc_g_frequency = vidioc_g_frequency,
1788 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001789#ifdef CONFIG_VIDEO_ADV_DEBUG
1790 .vidioc_g_register = vidioc_g_register,
1791 .vidioc_s_register = vidioc_s_register,
1792#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001793
1794 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001795 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001796};
1797
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001798static struct video_device em28xx_radio_template = {
1799 .name = "em28xx-radio",
1800 .type = VID_TYPE_TUNER,
1801 .fops = &radio_fops,
1802 .minor = -1,
1803 .vidioc_querycap = radio_querycap,
1804 .vidioc_g_tuner = radio_g_tuner,
1805 .vidioc_enum_input = radio_enum_input,
1806 .vidioc_g_audio = radio_g_audio,
1807 .vidioc_s_tuner = radio_s_tuner,
1808 .vidioc_s_audio = radio_s_audio,
1809 .vidioc_s_input = radio_s_input,
1810 .vidioc_queryctrl = radio_queryctrl,
1811 .vidioc_g_ctrl = vidioc_g_ctrl,
1812 .vidioc_s_ctrl = vidioc_s_ctrl,
1813 .vidioc_g_frequency = vidioc_g_frequency,
1814 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001815#ifdef CONFIG_VIDEO_ADV_DEBUG
1816 .vidioc_g_register = vidioc_g_register,
1817 .vidioc_s_register = vidioc_s_register,
1818#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001819};
1820
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001821/******************************** usb interface *****************************************/
1822
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001823
1824static LIST_HEAD(em28xx_extension_devlist);
1825static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1826
1827int em28xx_register_extension(struct em28xx_ops *ops)
1828{
1829 struct em28xx *h, *dev = NULL;
1830
1831 list_for_each_entry(h, &em28xx_devlist, devlist)
1832 dev = h;
1833
1834 mutex_lock(&em28xx_extension_devlist_lock);
1835 list_add_tail(&ops->next, &em28xx_extension_devlist);
1836 if (dev)
1837 ops->init(dev);
1838
1839 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1840 mutex_unlock(&em28xx_extension_devlist_lock);
1841
1842 return 0;
1843}
1844EXPORT_SYMBOL(em28xx_register_extension);
1845
1846void em28xx_unregister_extension(struct em28xx_ops *ops)
1847{
1848 struct em28xx *h, *dev = NULL;
1849
1850 list_for_each_entry(h, &em28xx_devlist, devlist)
1851 dev = h;
1852
1853 if (dev)
1854 ops->fini(dev);
1855
1856 mutex_lock(&em28xx_extension_devlist_lock);
1857 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1858 list_del(&ops->next);
1859 mutex_unlock(&em28xx_extension_devlist_lock);
1860}
1861EXPORT_SYMBOL(em28xx_unregister_extension);
1862
Adrian Bunk532fe652008-01-28 22:10:48 -03001863static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1864 const struct video_device *template,
1865 const int type,
1866 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001867{
1868 struct video_device *vfd;
1869
1870 vfd = video_device_alloc();
1871 if (NULL == vfd)
1872 return NULL;
1873 *vfd = *template;
1874 vfd->minor = -1;
1875 vfd->dev = &dev->udev->dev;
1876 vfd->release = video_device_release;
1877 vfd->type = type;
1878
1879 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1880 dev->name, type_name);
1881
1882 return vfd;
1883}
1884
1885
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001887 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001888 * allocates and inits the device structs, registers i2c bus and v4l device
1889 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001890static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001891 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001892{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001893 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001894 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001895 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001896 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001897 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001898
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001899 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001900 mutex_init(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001901 spin_lock_init(&dev->queue_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001902 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001903 init_waitqueue_head(&dev->wait_frame);
1904 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001905
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001906 dev->em28xx_write_regs = em28xx_write_regs;
1907 dev->em28xx_read_reg = em28xx_read_reg;
1908 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1909 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1910 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001911 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001912
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03001913 errCode = em28xx_read_reg(dev, CHIPID_REG);
1914 if (errCode >= 0)
1915 em28xx_info("em28xx chip ID = %d\n", errCode);
1916
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001917 em28xx_pre_card_setup(dev);
1918
1919 errCode = em28xx_config(dev);
1920 if (errCode) {
1921 em28xx_errdev("error configuring device\n");
1922 em28xx_devused &= ~(1<<dev->devno);
1923 kfree(dev);
1924 return -ENOMEM;
1925 }
1926
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001927 /* register i2c bus */
1928 em28xx_i2c_register(dev);
1929
1930 /* Do board specific init and eeprom reading */
1931 em28xx_card_setup(dev);
1932
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03001933 /* Configure audio */
1934 em28xx_audio_analog_set(dev);
1935
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001936 /* configure the device */
1937 em28xx_config_i2c(dev);
1938
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001939 /* set default norm */
1940 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001941
1942 maxw = norm_maxw(dev);
1943 maxh = norm_maxh(dev);
1944
1945 /* set default image size */
1946 dev->width = maxw;
1947 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001948 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001949 dev->field_size = dev->width * dev->height;
1950 dev->frame_size =
1951 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1952 dev->bytesperline = dev->width * 2;
1953 dev->hscale = 0;
1954 dev->vscale = 0;
1955 dev->ctl_input = 2;
1956
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001957 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001958
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001959 list_add_tail(&dev->devlist, &em28xx_devlist);
1960
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001961 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001962 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1963 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001964 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001965 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001966 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001967 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001968 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001969 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001970
1971 /* register v4l2 video video_device */
1972 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1973 video_nr[dev->devno]);
1974 if (retval) {
1975 em28xx_errdev("unable to register video device (error=%i).\n",
1976 retval);
1977 goto fail_unreg;
1978 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001979
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001980 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001981 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1982 VFL_TYPE_VBI, "vbi");
1983 /* register v4l2 vbi video_device */
1984 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1985 vbi_nr[dev->devno]) < 0) {
1986 em28xx_errdev("unable to register vbi device\n");
1987 retval = -ENODEV;
1988 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001989 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001990
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001991 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1992 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
1993 VFL_TYPE_RADIO, "radio");
1994 if (NULL == dev->radio_dev) {
1995 em28xx_errdev("cannot allocate video_device.\n");
1996 goto fail_unreg;
1997 }
1998 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1999 radio_nr[dev->devno]);
2000 if (retval < 0) {
2001 em28xx_errdev("can't register radio device\n");
2002 goto fail_unreg;
2003 }
2004 em28xx_info("Registered radio device as /dev/radio%d\n",
2005 dev->radio_dev->minor & 0x1f);
2006 }
2007
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002008
Sascha Sommer5a804152007-11-03 21:22:38 -03002009 if (dev->has_msp34xx) {
2010 /* Send a reset to other chips via gpio */
2011 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2012 msleep(3);
2013 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2014 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002015 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002016
Sascha Sommer5a804152007-11-03 21:22:38 -03002017 video_mux(dev, 0);
2018
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002019 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2020 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2021 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002022
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002023 mutex_lock(&em28xx_extension_devlist_lock);
2024 if (!list_empty(&em28xx_extension_devlist)) {
2025 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2026 if (ops->id)
2027 ops->init(dev);
2028 }
2029 }
2030 mutex_unlock(&em28xx_extension_devlist_lock);
2031
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002032 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002033
2034fail_unreg:
2035 em28xx_release_resources(dev);
2036 mutex_unlock(&dev->lock);
2037 kfree(dev);
2038 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002039}
2040
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002041#if defined(CONFIG_MODULES) && defined(MODULE)
2042static void request_module_async(struct work_struct *work)
2043{
2044 struct em28xx *dev = container_of(work,
2045 struct em28xx, request_module_wk);
2046
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002047 if (dev->has_audio_class)
2048 request_module("snd-usb-audio");
2049 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002050 request_module("em28xx-alsa");
2051}
2052
2053static void request_modules(struct em28xx *dev)
2054{
2055 INIT_WORK(&dev->request_module_wk, request_module_async);
2056 schedule_work(&dev->request_module_wk);
2057}
2058#else
2059#define request_modules(dev)
2060#endif /* CONFIG_MODULES */
2061
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002062/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002063 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002064 * checks for supported devices
2065 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002066static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002067 const struct usb_device_id *id)
2068{
2069 const struct usb_endpoint_descriptor *endpoint;
2070 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002071 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002072 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002074 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002075
2076 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002077 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2078
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002079 /* Check to see next free device and mark as used */
2080 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2081 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002082
2083 /* Don't register audio interfaces */
2084 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002085 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002086 udev->descriptor.idVendor,udev->descriptor.idProduct,
2087 ifnum,
2088 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002089
2090 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002091 return -ENODEV;
2092 }
2093
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002094 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002095 udev->descriptor.idVendor,udev->descriptor.idProduct,
2096 ifnum,
2097 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002098
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002099 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2100
Michael Opdenacker59c51592007-05-09 08:57:56 +02002101 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002102 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2103 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002104 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002105 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002106 return -ENODEV;
2107 }
2108 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002109 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002110 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002111 return -ENODEV;
2112 }
2113
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002114 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002115 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002116 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002117 return -ENOMEM;
2118 }
2119
2120 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002121 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002122 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002123 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002124 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002125 return -ENOMEM;
2126 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002127
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002128 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002129 dev->devno = nr;
2130 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002131 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002132
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002133 /* Checks if audio is provided by some interface */
2134 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2135 uif = udev->config->interface[i];
2136 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2137 dev->has_audio_class = 1;
2138 break;
2139 }
2140 }
2141
2142 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2143 dev->has_audio_class ? "Has" : "Doesn't have");
2144
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002145 /* compute alternate max packet sizes */
2146 uif = udev->actconfig->interface[0];
2147
2148 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002149 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002150// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2151 dev->alt_max_pkt_size = kmalloc(32*
2152 dev->num_alt,GFP_KERNEL);
2153 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002154 em28xx_errdev("out of memory!\n");
2155 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002156 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002157 return -ENOMEM;
2158 }
2159
2160 for (i = 0; i < dev->num_alt ; i++) {
2161 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2162 wMaxPacketSize);
2163 dev->alt_max_pkt_size[i] =
2164 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002165 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002166 dev->alt_max_pkt_size[i]);
2167 }
2168
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002169 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002170 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002171
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002172 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002173 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002174 if (retval)
2175 return retval;
2176
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002177 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002178
2179 /* save our data pointer in this interface device */
2180 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002181
2182 request_modules(dev);
2183
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184 return 0;
2185}
2186
2187/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002188 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002189 * called when the device gets diconencted
2190 * video device will be unregistered on v4l2_close in case it is still open
2191 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002192static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002193{
Sascha Sommer5a804152007-11-03 21:22:38 -03002194 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002195 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002196
2197 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198 usb_set_intfdata(interface, NULL);
2199
2200 if (!dev)
2201 return;
2202
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002203 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002204
Sascha Sommer5a804152007-11-03 21:22:38 -03002205 /* wait until all current v4l2 io is finished then deallocate resources */
2206 mutex_lock(&dev->lock);
2207
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002208 wake_up_interruptible_all(&dev->open);
2209
2210 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002211 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002213 "deallocation are deferred on close.\n",
2214 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2215
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002216 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002217 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002218 dev->state |= DEV_DISCONNECTED;
2219 wake_up_interruptible(&dev->wait_frame);
2220 wake_up_interruptible(&dev->wait_stream);
2221 } else {
2222 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002223 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002224 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002225 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002226
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002227 mutex_lock(&em28xx_extension_devlist_lock);
2228 if (!list_empty(&em28xx_extension_devlist)) {
2229 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2230 ops->fini(dev);
2231 }
2232 }
2233 mutex_unlock(&em28xx_extension_devlist_lock);
2234
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002235 if (!dev->users) {
2236 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002237 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002238 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002239}
2240
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002241static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002242 .name = "em28xx",
2243 .probe = em28xx_usb_probe,
2244 .disconnect = em28xx_usb_disconnect,
2245 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002246};
2247
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002248static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002249{
2250 int result;
2251
2252 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002253 (EM28XX_VERSION_CODE >> 16) & 0xff,
2254 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002255#ifdef SNAPSHOT
2256 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2257 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2258#endif
2259
2260 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002261 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002262 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002263 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002264 " usb_register failed. Error number %d.\n", result);
2265
2266 return result;
2267}
2268
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002269static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002270{
2271 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002272 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002273}
2274
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002275module_init(em28xx_module_init);
2276module_exit(em28xx_module_exit);