blob: c2901f13eb2583eada0a99ebb8696305e8a0651e [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>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080041
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080042#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
43 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030044 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080045 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_NAME "em28xx"
48#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030049#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080051#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080052 if (video_debug) \
53 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020054 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
56MODULE_AUTHOR(DRIVER_AUTHOR);
57MODULE_DESCRIPTION(DRIVER_DESC);
58MODULE_LICENSE("GPL");
59
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080060static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080061
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080062static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020063static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
64static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080065module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020066module_param_array(video_nr, int, NULL, 0444);
67module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080068MODULE_PARM_DESC(card,"card type");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020069MODULE_PARM_DESC(video_nr,"video device numbers");
70MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080071
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072static unsigned int video_debug = 0;
73module_param(video_debug,int,0644);
74MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
75
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020076/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
77static unsigned long em28xx_devused;
78
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080079/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020080/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080081static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080082 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020083 .id = V4L2_CID_AUDIO_VOLUME,
84 .type = V4L2_CTRL_TYPE_INTEGER,
85 .name = "Volume",
86 .minimum = 0x0,
87 .maximum = 0x1f,
88 .step = 0x1,
89 .default_value = 0x1f,
90 .flags = 0,
91 },{
92 .id = V4L2_CID_AUDIO_MUTE,
93 .type = V4L2_CTRL_TYPE_BOOLEAN,
94 .name = "Mute",
95 .minimum = 0,
96 .maximum = 1,
97 .step = 1,
98 .default_value = 1,
99 .flags = 0,
100 }
101};
102
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800103static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800104
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800105
106/********************* v4l2 interface ******************************************/
107
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800108/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800109 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800110 * inits registers with sane defaults
111 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800112static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800113{
114
115 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300116 if (!dev->is_em2800)
117 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800118
119 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200120
Markus Rechberger9475fb12006-02-27 00:07:34 -0300121/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
122/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200123 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
124
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800125 em28xx_audio_usb_mute(dev, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800126 dev->mute = 1; /* maybe not the right place... */
127 dev->volume = 0x1f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800128 em28xx_audio_analog_set(dev);
129 em28xx_audio_analog_setup(dev);
130 em28xx_outfmt_set_yuv422(dev);
131 em28xx_colorlevels_set_default(dev);
132 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800133
134 return 0;
135}
136
137/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800138 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800139 * configure i2c attached devices
140 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800141static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800142{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300143 struct v4l2_routing route;
144
145 route.input = INPUT(dev->ctl_input)->vmux;
146 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100147 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300148 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300149 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800150}
151
152/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800153 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800154 * prepare queues for incoming and outgoing frames
155 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800156static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800157{
158 u32 i;
159
160 INIT_LIST_HEAD(&dev->inqueue);
161 INIT_LIST_HEAD(&dev->outqueue);
162
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800163 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800164 dev->frame[i].state = F_UNUSED;
165 dev->frame[i].buf.bytesused = 0;
166 }
167}
168
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800169static void video_mux(struct em28xx *dev, int index)
170{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300171 int ainput;
172 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800173
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300174 route.input = INPUT(index)->vmux;
175 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800176 dev->ctl_input = index;
177 dev->ctl_ainput = INPUT(index)->amux;
178
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300179 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800180
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800181 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200182 if (dev->i2s_speed)
183 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300184 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300185 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300186 /* Note: this is msp3400 specific */
187 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800188 ainput = EM28XX_AUDIO_SRC_TUNER;
189 em28xx_audio_source(dev, ainput);
190 } else {
191 switch (dev->ctl_ainput) {
Markus Rechberger9475fb12006-02-27 00:07:34 -0300192 case 0:
193 ainput = EM28XX_AUDIO_SRC_TUNER;
194 break;
195 default:
196 ainput = EM28XX_AUDIO_SRC_LINE;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800197 }
198 em28xx_audio_source(dev, ainput);
199 }
200}
201
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300202/* Usage lock check functions */
203static int res_get(struct em28xx_fh *fh)
204{
205 struct em28xx *dev = fh->dev;
206 int rc = 0;
207
208 /* This instance already has stream_on */
209 if (fh->stream_on)
210 return rc;
211
212 mutex_lock(&dev->lock);
213
214 if (dev->stream_on)
215 rc = -EINVAL;
216 else {
217 dev->stream_on = 1;
218 fh->stream_on = 1;
219 }
220
221 mutex_unlock(&dev->lock);
222 return rc;
223}
224
225static int res_check(struct em28xx_fh *fh)
226{
227 return (fh->stream_on);
228}
229
230static void res_free(struct em28xx_fh *fh)
231{
232 struct em28xx *dev = fh->dev;
233
234 mutex_lock(&dev->lock);
235 fh->stream_on = 0;
236 dev->stream_on = 0;
237 mutex_unlock(&dev->lock);
238}
239
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800240/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300241 * em28xx_vm_open()
242 */
243static void em28xx_vm_open(struct vm_area_struct *vma)
244{
245 struct em28xx_frame_t *f = vma->vm_private_data;
246 f->vma_use_count++;
247}
248
249/*
250 * em28xx_vm_close()
251 */
252static void em28xx_vm_close(struct vm_area_struct *vma)
253{
254 /* NOTE: buffers are not freed here */
255 struct em28xx_frame_t *f = vma->vm_private_data;
256
257 if (f->vma_use_count)
258 f->vma_use_count--;
259}
260
261static struct vm_operations_struct em28xx_vm_ops = {
262 .open = em28xx_vm_open,
263 .close = em28xx_vm_close,
264};
265
266
267/*
268 * em28xx_get_ctrl()
269 * return the current saturation, brightness or contrast, mute state
270 */
271static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
272{
273 switch (ctrl->id) {
274 case V4L2_CID_AUDIO_MUTE:
275 ctrl->value = dev->mute;
276 return 0;
277 case V4L2_CID_AUDIO_VOLUME:
278 ctrl->value = dev->volume;
279 return 0;
280 default:
281 return -EINVAL;
282 }
283}
284
285/*
286 * em28xx_set_ctrl()
287 * mute or set new saturation, brightness or contrast
288 */
289static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
290{
291 switch (ctrl->id) {
292 case V4L2_CID_AUDIO_MUTE:
293 if (ctrl->value != dev->mute) {
294 dev->mute = ctrl->value;
295 em28xx_audio_usb_mute(dev, ctrl->value);
296 return em28xx_audio_analog_set(dev);
297 }
298 return 0;
299 case V4L2_CID_AUDIO_VOLUME:
300 dev->volume = ctrl->value;
301 return em28xx_audio_analog_set(dev);
302 default:
303 return -EINVAL;
304 }
305}
306
307/*
308 * em28xx_stream_interrupt()
309 * stops streaming
310 */
311static int em28xx_stream_interrupt(struct em28xx *dev)
312{
313 int rc = 0;
314
315 /* stop reading from the device */
316
317 dev->stream = STREAM_INTERRUPT;
318 rc = wait_event_timeout(dev->wait_stream,
319 (dev->stream == STREAM_OFF) ||
320 (dev->state & DEV_DISCONNECTED),
321 EM28XX_URB_TIMEOUT);
322
323 if (rc) {
324 dev->state |= DEV_MISCONFIGURED;
325 em28xx_videodbg("device is misconfigured; close and "
326 "open /dev/video%d again\n",
327 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
328 return rc;
329 }
330
331 return 0;
332}
333
334
335static int check_dev(struct em28xx *dev)
336{
337 if (dev->state & DEV_DISCONNECTED) {
338 em28xx_errdev("v4l2 ioctl: device not present\n");
339 return -ENODEV;
340 }
341
342 if (dev->state & DEV_MISCONFIGURED) {
343 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
344 "close and open it again\n");
345 return -EIO;
346 }
347 return 0;
348}
349
350static void get_scale(struct em28xx *dev,
351 unsigned int width, unsigned int height,
352 unsigned int *hscale, unsigned int *vscale)
353{
354 unsigned int maxw = norm_maxw(dev);
355 unsigned int maxh = norm_maxh(dev);
356
357 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
358 if (*hscale >= 0x4000)
359 *hscale = 0x3fff;
360
361 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
362 if (*vscale >= 0x4000)
363 *vscale = 0x3fff;
364}
365
366/* ------------------------------------------------------------------
367 IOCTL vidioc handling
368 ------------------------------------------------------------------*/
369
370static int vidioc_g_fmt_cap(struct file *file, void *priv,
371 struct v4l2_format *f)
372{
373 struct em28xx_fh *fh = priv;
374 struct em28xx *dev = fh->dev;
375
376 mutex_lock(&dev->lock);
377
378 f->fmt.pix.width = dev->width;
379 f->fmt.pix.height = dev->height;
380 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
381 f->fmt.pix.bytesperline = dev->bytesperline;
382 f->fmt.pix.sizeimage = dev->frame_size;
383 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
384
385 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
386 f->fmt.pix.field = dev->interlaced ?
387 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
388
389 mutex_unlock(&dev->lock);
390 return 0;
391}
392
393static int vidioc_try_fmt_cap(struct file *file, void *priv,
394 struct v4l2_format *f)
395{
396 struct em28xx_fh *fh = priv;
397 struct em28xx *dev = fh->dev;
398 int width = f->fmt.pix.width;
399 int height = f->fmt.pix.height;
400 unsigned int maxw = norm_maxw(dev);
401 unsigned int maxh = norm_maxh(dev);
402 unsigned int hscale, vscale;
403
404 /* width must even because of the YUYV format
405 height must be even because of interlacing */
406 height &= 0xfffe;
407 width &= 0xfffe;
408
409 if (height < 32)
410 height = 32;
411 if (height > maxh)
412 height = maxh;
413 if (width < 48)
414 width = 48;
415 if (width > maxw)
416 width = maxw;
417
418 mutex_lock(&dev->lock);
419
420 if (dev->is_em2800) {
421 /* the em2800 can only scale down to 50% */
422 if (height % (maxh / 2))
423 height = maxh;
424 if (width % (maxw / 2))
425 width = maxw;
426 /* according to empiatech support */
427 /* the MaxPacketSize is to small to support */
428 /* framesizes larger than 640x480 @ 30 fps */
429 /* or 640x576 @ 25 fps. As this would cut */
430 /* of a part of the image we prefer */
431 /* 360x576 or 360x480 for now */
432 if (width == maxw && height == maxh)
433 width /= 2;
434 }
435
436 get_scale(dev, width, height, &hscale, &vscale);
437
438 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
439 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
440
441 f->fmt.pix.width = width;
442 f->fmt.pix.height = height;
443 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
444 f->fmt.pix.bytesperline = width * 2;
445 f->fmt.pix.sizeimage = width * 2 * height;
446 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
447 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
448
449 mutex_unlock(&dev->lock);
450 return 0;
451}
452
453static int vidioc_s_fmt_cap(struct file *file, void *priv,
454 struct v4l2_format *f)
455{
456 struct em28xx_fh *fh = priv;
457 struct em28xx *dev = fh->dev;
458 int rc, i;
459
460 rc = check_dev(dev);
461 if (rc < 0)
462 return rc;
463
464 vidioc_try_fmt_cap(file, priv, f);
465
466 mutex_lock(&dev->lock);
467
468 for (i = 0; i < dev->num_frames; i++)
469 if (dev->frame[i].vma_use_count) {
470 em28xx_videodbg("VIDIOC_S_FMT failed. "
471 "Unmap the buffers first.\n");
472 rc = -EINVAL;
473 goto err;
474 }
475
476 /* stop io in case it is already in progress */
477 if (dev->stream == STREAM_ON) {
478 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
479 rc = em28xx_stream_interrupt(dev);
480 if (rc < 0)
481 goto err;
482 }
483
484 em28xx_release_buffers(dev);
485 dev->io = IO_NONE;
486
487 /* set new image size */
488 dev->width = f->fmt.pix.width;
489 dev->height = f->fmt.pix.height;
490 dev->frame_size = dev->width * dev->height * 2;
491 dev->field_size = dev->frame_size >> 1;
492 dev->bytesperline = dev->width * 2;
493 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
494
495 /* FIXME: This is really weird! Why capture is starting with
496 this ioctl ???
497 */
498 em28xx_uninit_isoc(dev);
499 em28xx_set_alternate(dev);
500 em28xx_capture_start(dev, 1);
501 em28xx_resolution_set(dev);
502 em28xx_init_isoc(dev);
503 rc = 0;
504
505err:
506 mutex_unlock(&dev->lock);
507 return rc;
508}
509
510static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
511{
512 struct em28xx_fh *fh = priv;
513 struct em28xx *dev = fh->dev;
514 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300515 int rc;
516
517 rc = check_dev(dev);
518 if (rc < 0)
519 return rc;
520
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300521 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300522 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300523 mutex_unlock(&dev->lock);
524
525 /* Adjusts width/height, if needed */
526 f.fmt.pix.width = dev->width;
527 f.fmt.pix.height = dev->height;
528 vidioc_try_fmt_cap(file, priv, &f);
529
530 mutex_lock(&dev->lock);
531
532 /* set new image size */
533 dev->width = f.fmt.pix.width;
534 dev->height = f.fmt.pix.height;
535 dev->frame_size = dev->width * dev->height * 2;
536 dev->field_size = dev->frame_size >> 1;
537 dev->bytesperline = dev->width * 2;
538 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
539
540 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300541 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300542
543 mutex_unlock(&dev->lock);
544 return 0;
545}
546
547static const char *iname[] = {
548 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
549 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
550 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
551 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
552 [EM28XX_VMUX_SVIDEO] = "S-Video",
553 [EM28XX_VMUX_TELEVISION] = "Television",
554 [EM28XX_VMUX_CABLE] = "Cable TV",
555 [EM28XX_VMUX_DVB] = "DVB",
556 [EM28XX_VMUX_DEBUG] = "for debug only",
557};
558
559static int vidioc_enum_input(struct file *file, void *priv,
560 struct v4l2_input *i)
561{
562 struct em28xx_fh *fh = priv;
563 struct em28xx *dev = fh->dev;
564 unsigned int n;
565
566 n = i->index;
567 if (n >= MAX_EM28XX_INPUT)
568 return -EINVAL;
569 if (0 == INPUT(n)->type)
570 return -EINVAL;
571
572 i->index = n;
573 i->type = V4L2_INPUT_TYPE_CAMERA;
574
575 strcpy(i->name, iname[INPUT(n)->type]);
576
577 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
578 (EM28XX_VMUX_CABLE == INPUT(n)->type))
579 i->type = V4L2_INPUT_TYPE_TUNER;
580
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300581 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300582
583 return 0;
584}
585
586static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
587{
588 struct em28xx_fh *fh = priv;
589 struct em28xx *dev = fh->dev;
590
591 *i = dev->ctl_input;
592
593 return 0;
594}
595
596static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
597{
598 struct em28xx_fh *fh = priv;
599 struct em28xx *dev = fh->dev;
600 int rc;
601
602 rc = check_dev(dev);
603 if (rc < 0)
604 return rc;
605
606 if (i >= MAX_EM28XX_INPUT)
607 return -EINVAL;
608 if (0 == INPUT(i)->type)
609 return -EINVAL;
610
611 mutex_lock(&dev->lock);
612
613 video_mux(dev, i);
614
615 mutex_unlock(&dev->lock);
616 return 0;
617}
618
619static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
620{
621 struct em28xx_fh *fh = priv;
622 struct em28xx *dev = fh->dev;
623 unsigned int index = a->index;
624
625 if (a->index > 1)
626 return -EINVAL;
627
628 index = dev->ctl_ainput;
629
630 if (index == 0) {
631 strcpy(a->name, "Television");
632 } else {
633 strcpy(a->name, "Line In");
634 }
635 a->capability = V4L2_AUDCAP_STEREO;
636 a->index = index;
637
638 return 0;
639}
640
641static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
642{
643 struct em28xx_fh *fh = priv;
644 struct em28xx *dev = fh->dev;
645
646 if (a->index != dev->ctl_ainput)
647 return -EINVAL;
648
649 return 0;
650}
651
652static int vidioc_queryctrl(struct file *file, void *priv,
653 struct v4l2_queryctrl *qc)
654{
655 struct em28xx_fh *fh = priv;
656 struct em28xx *dev = fh->dev;
657 int id = qc->id;
658 int i;
659 int rc;
660
661 rc = check_dev(dev);
662 if (rc < 0)
663 return rc;
664
665 memset(qc, 0, sizeof(*qc));
666
667 qc->id = id;
668
669 if (!dev->has_msp34xx) {
670 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
671 if (qc->id && qc->id == em28xx_qctrl[i].id) {
672 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
673 return 0;
674 }
675 }
676 }
677 mutex_lock(&dev->lock);
678 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
679 mutex_unlock(&dev->lock);
680
681 if (qc->type)
682 return 0;
683 else
684 return -EINVAL;
685}
686
687static int vidioc_g_ctrl(struct file *file, void *priv,
688 struct v4l2_control *ctrl)
689{
690 struct em28xx_fh *fh = priv;
691 struct em28xx *dev = fh->dev;
692 int rc;
693
694 rc = check_dev(dev);
695 if (rc < 0)
696 return rc;
697 mutex_lock(&dev->lock);
698
699 if (!dev->has_msp34xx)
700 rc = em28xx_get_ctrl(dev, ctrl);
701 else
702 rc = -EINVAL;
703
704 if (rc == -EINVAL) {
705 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
706 rc = 0;
707 }
708
709 mutex_unlock(&dev->lock);
710 return rc;
711}
712
713static int vidioc_s_ctrl(struct file *file, void *priv,
714 struct v4l2_control *ctrl)
715{
716 struct em28xx_fh *fh = priv;
717 struct em28xx *dev = fh->dev;
718 u8 i;
719 int rc;
720
721 rc = check_dev(dev);
722 if (rc < 0)
723 return rc;
724
725 mutex_lock(&dev->lock);
726
727 if (dev->has_msp34xx)
728 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
729 else {
730 rc = 1;
731 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
732 if (ctrl->id == em28xx_qctrl[i].id) {
733 if (ctrl->value < em28xx_qctrl[i].minimum ||
734 ctrl->value > em28xx_qctrl[i].maximum) {
735 rc = -ERANGE;
736 break;
737 }
738
739 rc = em28xx_set_ctrl(dev, ctrl);
740 break;
741 }
742 }
743 }
744
745 /* Control not found - try to send it to the attached devices */
746 if (rc == 1) {
747 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
748 rc = 0;
749 }
750
751 mutex_unlock(&dev->lock);
752 return rc;
753}
754
755static int vidioc_g_tuner(struct file *file, void *priv,
756 struct v4l2_tuner *t)
757{
758 struct em28xx_fh *fh = priv;
759 struct em28xx *dev = fh->dev;
760 int rc;
761
762 rc = check_dev(dev);
763 if (rc < 0)
764 return rc;
765
766 if (0 != t->index)
767 return -EINVAL;
768
769 strcpy(t->name, "Tuner");
770
771 mutex_lock(&dev->lock);
772
773 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
774
775 mutex_unlock(&dev->lock);
776 return 0;
777}
778
779static int vidioc_s_tuner(struct file *file, void *priv,
780 struct v4l2_tuner *t)
781{
782 struct em28xx_fh *fh = priv;
783 struct em28xx *dev = fh->dev;
784 int rc;
785
786 rc = check_dev(dev);
787 if (rc < 0)
788 return rc;
789
790 if (0 != t->index)
791 return -EINVAL;
792
793 mutex_lock(&dev->lock);
794
795 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
796
797 mutex_unlock(&dev->lock);
798 return 0;
799}
800
801static int vidioc_g_frequency(struct file *file, void *priv,
802 struct v4l2_frequency *f)
803{
804 struct em28xx_fh *fh = priv;
805 struct em28xx *dev = fh->dev;
806
807 f->type = V4L2_TUNER_ANALOG_TV;
808 f->frequency = dev->ctl_freq;
809
810 return 0;
811}
812
813static int vidioc_s_frequency(struct file *file, void *priv,
814 struct v4l2_frequency *f)
815{
816 struct em28xx_fh *fh = priv;
817 struct em28xx *dev = fh->dev;
818 int rc;
819
820 rc = check_dev(dev);
821 if (rc < 0)
822 return rc;
823
824 if (0 != f->tuner)
825 return -EINVAL;
826
827 if (V4L2_TUNER_ANALOG_TV != f->type)
828 return -EINVAL;
829
830 mutex_lock(&dev->lock);
831
832 dev->ctl_freq = f->frequency;
833 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
834
835 mutex_unlock(&dev->lock);
836 return 0;
837}
838
839static int vidioc_cropcap(struct file *file, void *priv,
840 struct v4l2_cropcap *cc)
841{
842 struct em28xx_fh *fh = priv;
843 struct em28xx *dev = fh->dev;
844
845 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
846 return -EINVAL;
847
848 cc->bounds.left = 0;
849 cc->bounds.top = 0;
850 cc->bounds.width = dev->width;
851 cc->bounds.height = dev->height;
852 cc->defrect = cc->bounds;
853 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
854 cc->pixelaspect.denominator = 59;
855
856 return 0;
857}
858
859static int vidioc_streamon(struct file *file, void *priv,
860 enum v4l2_buf_type type)
861{
862 struct em28xx_fh *fh = priv;
863 struct em28xx *dev = fh->dev;
864 int rc;
865
866 rc = check_dev(dev);
867 if (rc < 0)
868 return rc;
869
870 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
871 return -EINVAL;
872
873 if (list_empty(&dev->inqueue))
874 return -EINVAL;
875
876 mutex_lock(&dev->lock);
877
878 if (unlikely(res_get(fh) < 0)) {
879 mutex_unlock(&dev->lock);
880 return -EBUSY;
881 }
882
883 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
884
885 mutex_unlock(&dev->lock);
886 return 0;
887}
888
889static int vidioc_streamoff(struct file *file, void *priv,
890 enum v4l2_buf_type type)
891{
892 struct em28xx_fh *fh = priv;
893 struct em28xx *dev = fh->dev;
894 int rc;
895
896 rc = check_dev(dev);
897 if (rc < 0)
898 return rc;
899
900 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
901 return -EINVAL;
902
903 mutex_lock(&dev->lock);
904
905 if (dev->stream == STREAM_ON) {
906 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
907 rc = em28xx_stream_interrupt(dev);
908 if (rc < 0) {
909 mutex_unlock(&dev->lock);
910 return rc;
911 }
912 }
913
914 em28xx_empty_framequeues(dev);
915
916 mutex_unlock(&dev->lock);
917 return 0;
918}
919
920static int vidioc_querycap(struct file *file, void *priv,
921 struct v4l2_capability *cap)
922{
923 struct em28xx_fh *fh = priv;
924 struct em28xx *dev = fh->dev;
925
926 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
927 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
928 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
929
930 cap->version = EM28XX_VERSION_CODE;
931
932 cap->capabilities =
933 V4L2_CAP_SLICED_VBI_CAPTURE |
934 V4L2_CAP_VIDEO_CAPTURE |
935 V4L2_CAP_AUDIO |
936 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
937
938 if (dev->has_tuner)
939 cap->capabilities |= V4L2_CAP_TUNER;
940
941 return 0;
942}
943
944static int vidioc_enum_fmt_cap(struct file *file, void *priv,
945 struct v4l2_fmtdesc *fmtd)
946{
947 if (fmtd->index != 0)
948 return -EINVAL;
949
950 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
951 strcpy(fmtd->description, "Packed YUY2");
952 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
953 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
954
955 return 0;
956}
957
958/* Sliced VBI ioctls */
959static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
960 struct v4l2_format *f)
961{
962 struct em28xx_fh *fh = priv;
963 struct em28xx *dev = fh->dev;
964 int rc;
965
966 rc = check_dev(dev);
967 if (rc < 0)
968 return rc;
969
970 mutex_lock(&dev->lock);
971
972 f->fmt.sliced.service_set = 0;
973
974 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
975
976 if (f->fmt.sliced.service_set == 0)
977 rc = -EINVAL;
978
979 mutex_unlock(&dev->lock);
980 return rc;
981}
982
983static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
984 struct v4l2_format *f)
985{
986 struct em28xx_fh *fh = priv;
987 struct em28xx *dev = fh->dev;
988 int rc;
989
990 rc = check_dev(dev);
991 if (rc < 0)
992 return rc;
993
994 mutex_lock(&dev->lock);
995 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
996 mutex_unlock(&dev->lock);
997
998 if (f->fmt.sliced.service_set == 0)
999 return -EINVAL;
1000
1001 return 0;
1002}
1003
1004
1005static int vidioc_reqbufs(struct file *file, void *priv,
1006 struct v4l2_requestbuffers *rb)
1007{
1008 struct em28xx_fh *fh = priv;
1009 struct em28xx *dev = fh->dev;
1010 u32 i;
1011 int rc;
1012
1013 rc = check_dev(dev);
1014 if (rc < 0)
1015 return rc;
1016
1017 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1018 rb->memory != V4L2_MEMORY_MMAP)
1019 return -EINVAL;
1020
1021 if (dev->io == IO_READ) {
1022 em28xx_videodbg("method is set to read;"
1023 " close and open the device again to"
1024 " choose the mmap I/O method\n");
1025 return -EINVAL;
1026 }
1027
1028 for (i = 0; i < dev->num_frames; i++)
1029 if (dev->frame[i].vma_use_count) {
1030 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1031 "previous buffers are still mapped\n");
1032 return -EINVAL;
1033 }
1034
1035 mutex_lock(&dev->lock);
1036
1037 if (dev->stream == STREAM_ON) {
1038 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1039 rc = em28xx_stream_interrupt(dev);
1040 if (rc < 0) {
1041 mutex_unlock(&dev->lock);
1042 return rc;
1043 }
1044 }
1045
1046 em28xx_empty_framequeues(dev);
1047
1048 em28xx_release_buffers(dev);
1049 if (rb->count)
1050 rb->count = em28xx_request_buffers(dev, rb->count);
1051
1052 dev->frame_current = NULL;
1053 dev->io = rb->count ? IO_MMAP : IO_NONE;
1054
1055 mutex_unlock(&dev->lock);
1056 return 0;
1057}
1058
1059static int vidioc_querybuf(struct file *file, void *priv,
1060 struct v4l2_buffer *b)
1061{
1062 struct em28xx_fh *fh = priv;
1063 struct em28xx *dev = fh->dev;
1064 int rc;
1065
1066 rc = check_dev(dev);
1067 if (rc < 0)
1068 return rc;
1069
1070 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1071 b->index >= dev->num_frames || dev->io != IO_MMAP)
1072 return -EINVAL;
1073
1074 mutex_lock(&dev->lock);
1075
1076 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1077
1078 if (dev->frame[b->index].vma_use_count)
1079 b->flags |= V4L2_BUF_FLAG_MAPPED;
1080
1081 if (dev->frame[b->index].state == F_DONE)
1082 b->flags |= V4L2_BUF_FLAG_DONE;
1083 else if (dev->frame[b->index].state != F_UNUSED)
1084 b->flags |= V4L2_BUF_FLAG_QUEUED;
1085
1086 mutex_unlock(&dev->lock);
1087 return 0;
1088}
1089
1090static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1091{
1092 struct em28xx_fh *fh = priv;
1093 struct em28xx *dev = fh->dev;
1094 unsigned long lock_flags;
1095 int rc;
1096
1097 rc = check_dev(dev);
1098 if (rc < 0)
1099 return rc;
1100
1101 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP ||
1102 b->index >= dev->num_frames)
1103 return -EINVAL;
1104
1105 if (dev->frame[b->index].state != F_UNUSED)
1106 return -EAGAIN;
1107
1108 dev->frame[b->index].state = F_QUEUED;
1109
1110 /* add frame to fifo */
1111 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1112 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1113 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1114
1115 return 0;
1116}
1117
1118static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1119{
1120 struct em28xx_fh *fh = priv;
1121 struct em28xx *dev = fh->dev;
1122 int rc;
1123 struct em28xx_frame_t *f;
1124 unsigned long lock_flags;
1125
1126 rc = check_dev(dev);
1127 if (rc < 0)
1128 return rc;
1129
1130 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
1131 return -EINVAL;
1132
1133 if (list_empty(&dev->outqueue)) {
1134 if (dev->stream == STREAM_OFF)
1135 return -EINVAL;
1136
1137 if (file->f_flags & O_NONBLOCK)
1138 return -EAGAIN;
1139
1140 rc = wait_event_interruptible(dev->wait_frame,
1141 (!list_empty(&dev->outqueue)) ||
1142 (dev->state & DEV_DISCONNECTED));
1143 if (rc)
1144 return rc;
1145
1146 if (dev->state & DEV_DISCONNECTED)
1147 return -ENODEV;
1148 }
1149
1150 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1151 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1152 list_del(dev->outqueue.next);
1153 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1154
1155 f->state = F_UNUSED;
1156 memcpy(b, &f->buf, sizeof(*b));
1157
1158 if (f->vma_use_count)
1159 b->flags |= V4L2_BUF_FLAG_MAPPED;
1160
1161 return 0;
1162}
1163
1164/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001165 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001166 * inits the device and starts isoc transfer
1167 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001168static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001169{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001170 int minor = iminor(inode);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001171 int errCode = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001172 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001173 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001174
Trent Piephoa991f442007-10-10 05:37:43 -03001175 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001176 if (h->vdev->minor == minor) {
1177 dev = h;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001178 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1179 }
1180 if (h->vbi_dev->minor == minor) {
1181 dev = h;
1182 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001183 }
1184 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001185 if (NULL == dev)
1186 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001187
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001188 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1189 minor,v4l2_type_names[dev->type],dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001190
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001191 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001192
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001193 if (!fh) {
1194 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1195 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001196 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001197 mutex_lock(&dev->lock);
1198 fh->dev = dev;
1199 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001200
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001201 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001202 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001203
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001204 dev->width = norm_maxw(dev);
1205 dev->height = norm_maxh(dev);
1206 dev->frame_size = dev->width * dev->height * 2;
1207 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1208 dev->bytesperline = dev->width * 2;
1209 dev->hscale = 0;
1210 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001211
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001212 em28xx_capture_start(dev, 1);
1213 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001214
Markus Rechbergere5d4a562006-02-07 06:49:13 -02001215
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001216 /* start the transfer */
1217 errCode = em28xx_init_isoc(dev);
1218 if (errCode)
1219 goto err;
1220
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001221 em28xx_empty_framequeues(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001222 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001223
1224 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001225
Ingo Molnar3593cab2006-02-07 06:49:14 -02001226err:
1227 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001228 return errCode;
1229}
1230
1231/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001232 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001233 * unregisters the v4l2,i2c and usb devices
1234 * called when the device gets disconected or at module unload
1235*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001236static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001237{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001238
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001239 /*FIXME: I2C IR should be disconnected */
1240
1241 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1242 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1243 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001244 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001245 video_unregister_device(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001246 video_unregister_device(dev->vbi_dev);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001247 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001248 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001249
1250
1251 /* Mark device as unused */
1252 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001253}
1254
1255/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001256 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001257 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1258 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001259static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001260{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001261 struct em28xx_fh *fh = filp->private_data;
1262 struct em28xx *dev = fh->dev;
1263 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001264
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001265 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001266
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001267
1268 if (res_check(fh))
1269 res_free(fh);
1270
Ingo Molnar3593cab2006-02-07 06:49:14 -02001271 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001272
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001273 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001274 em28xx_uninit_isoc(dev);
1275 em28xx_release_buffers(dev);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001276 dev->io = IO_NONE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001277
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001278 /* the device is already disconnect,
1279 free the remaining resources */
1280 if (dev->state & DEV_DISCONNECTED) {
1281 em28xx_release_resources(dev);
1282 mutex_unlock(&dev->lock);
1283 kfree(dev);
1284 return 0;
1285 }
1286
1287 /* set alternate 0 */
1288 dev->alt = 0;
1289 em28xx_videodbg("setting alternate 0\n");
1290 errCode = usb_set_interface(dev->udev, 0, 0);
1291 if (errCode < 0) {
1292 em28xx_errdev("cannot change alternate number to "
1293 "0 (error=%i)\n", errCode);
1294 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001295 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001296 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001297 dev->users--;
1298 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001299 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001300 return 0;
1301}
1302
1303/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001304 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001305 * will allocate buffers when called for the first time
1306 */
1307static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001308em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001309 loff_t * f_pos)
1310{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001311 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001312 unsigned long lock_flags;
1313 int ret = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001314 struct em28xx_fh *fh = filp->private_data;
1315 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001316
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001317 /* FIXME: read() is not prepared to allow changing the video
1318 resolution while streaming. Seems a bug at em28xx_set_fmt
1319 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001320
1321 if (unlikely(res_get(fh) < 0))
1322 return -EBUSY;
1323
Sascha Sommer5a804152007-11-03 21:22:38 -03001324 mutex_lock(&dev->lock);
1325
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001326 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001327 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001328
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001329 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1330 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1331 em28xx_videodbg("not supported yet! ...\n");
1332 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001333 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001334 return -EFAULT;
1335 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001336 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001337 return (1);
1338 }
1339 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1340 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1341 em28xx_videodbg("not supported yet! ...\n");
1342 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001343 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001344 return -EFAULT;
1345 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001346 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001347 return (1);
1348 }
1349
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001350 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001351 em28xx_videodbg("device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001352 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001353 return -ENODEV;
1354 }
1355
1356 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001357 em28xx_videodbg("device misconfigured; close and open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001358 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001359 return -EIO;
1360 }
1361
1362 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001363 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001364 " the device again to choose the read method\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001365 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001366 return -EINVAL;
1367 }
1368
1369 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001370 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1371 em28xx_errdev("read failed, not enough memory\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001372 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001373 return -ENOMEM;
1374 }
1375 dev->io = IO_READ;
1376 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001377 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001378 }
1379
1380 if (!count) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001381 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001382 return 0;
1383 }
1384
1385 if (list_empty(&dev->outqueue)) {
1386 if (filp->f_flags & O_NONBLOCK) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001387 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001388 return -EAGAIN;
1389 }
1390 ret = wait_event_interruptible
1391 (dev->wait_frame,
1392 (!list_empty(&dev->outqueue)) ||
1393 (dev->state & DEV_DISCONNECTED));
1394 if (ret) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001395 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001396 return ret;
1397 }
1398 if (dev->state & DEV_DISCONNECTED) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001399 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001400 return -ENODEV;
1401 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001402 dev->video_bytesread = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001403 }
1404
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001405 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001406
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001407 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001408
1409 if (count > f->buf.length)
1410 count = f->buf.length;
1411
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001412 if ((dev->video_bytesread + count) > dev->frame_size)
1413 count = dev->frame_size - dev->video_bytesread;
1414
1415 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1416 em28xx_err("Error while copying to user\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001417 return -EFAULT;
1418 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001419 dev->video_bytesread += count;
1420
1421 if (dev->video_bytesread == dev->frame_size) {
1422 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1423 list_for_each_entry(i, &dev->outqueue, frame)
1424 i->state = F_UNUSED;
1425 INIT_LIST_HEAD(&dev->outqueue);
1426 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1427
1428 em28xx_queue_unusedframes(dev);
1429 dev->video_bytesread = 0;
1430 }
1431
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001432 *f_pos += count;
1433
Sascha Sommer5a804152007-11-03 21:22:38 -03001434 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001435
1436 return count;
1437}
1438
1439/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001440 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001441 * will allocate buffers when called for the first time
1442 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001443static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001444{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001445 unsigned int mask = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001446 struct em28xx_fh *fh = filp->private_data;
1447 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001448
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001449 if (unlikely(res_get(fh) < 0))
1450 return POLLERR;
1451
Sascha Sommer5a804152007-11-03 21:22:38 -03001452 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001453
1454 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001455 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001456 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001457 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001458 } else {
1459 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001460 if (!em28xx_request_buffers
1461 (dev, EM28XX_NUM_READ_FRAMES)) {
1462 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001463 ("poll() failed, not enough memory\n");
1464 } else {
1465 dev->io = IO_READ;
1466 dev->stream = STREAM_ON;
1467 }
1468 }
1469
1470 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001471 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001472 poll_wait(filp, &dev->wait_frame, wait);
1473
1474 if (!list_empty(&dev->outqueue))
1475 mask |= POLLIN | POLLRDNORM;
1476
Sascha Sommer5a804152007-11-03 21:22:38 -03001477 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001478
1479 return mask;
1480 }
1481 }
1482
Sascha Sommer5a804152007-11-03 21:22:38 -03001483 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001484 return POLLERR;
1485}
1486
1487/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001488 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001489 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001490static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001491{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001492 struct em28xx_fh *fh = filp->private_data;
1493 struct em28xx *dev = fh->dev;
1494 unsigned long size = vma->vm_end - vma->vm_start;
1495 unsigned long start = vma->vm_start;
1496 void *pos;
1497 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -08001498
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001499 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001500 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001501
1502 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001503
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001504 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001505 em28xx_videodbg("mmap: device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001506 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001507 return -ENODEV;
1508 }
1509
1510 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001511 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001512 "open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001513 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001514 return -EIO;
1515 }
1516
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001517 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001518 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001519 return -EINVAL;
1520 }
1521
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001522 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1523 size = PAGE_ALIGN(dev->frame[0].buf.length);
1524
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001525 for (i = 0; i < dev->num_frames; i++) {
1526 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1527 break;
1528 }
1529 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001530 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001531 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001532 return -EINVAL;
1533 }
1534
1535 /* VM_IO is eventually going to replace PageReserved altogether */
1536 vma->vm_flags |= VM_IO;
1537 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1538
Sascha Sommer3639c862005-12-12 00:37:30 -08001539 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001540 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -08001541 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1542 em28xx_videodbg("mmap: vm_insert_page failed\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001543 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001544 return -EAGAIN;
1545 }
1546 start += PAGE_SIZE;
1547 pos += PAGE_SIZE;
1548 size -= PAGE_SIZE;
1549 }
1550
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001551 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001552 vma->vm_private_data = &dev->frame[i];
1553
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001554 em28xx_vm_open(vma);
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
Arjan van de Venfa027c22007-02-12 00:55:33 -08001559static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001560 .owner = THIS_MODULE,
1561 .open = em28xx_v4l2_open,
1562 .release = em28xx_v4l2_close,
1563 .read = em28xx_v4l2_read,
1564 .poll = em28xx_v4l2_poll,
1565 .mmap = em28xx_v4l2_mmap,
1566 .ioctl = video_ioctl2,
1567 .llseek = no_llseek,
1568 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001569};
1570
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001571static const struct video_device em28xx_video_template = {
1572 .fops = &em28xx_v4l_fops,
1573 .release = video_device_release,
1574
1575 .minor = -1,
1576 .vidioc_querycap = vidioc_querycap,
1577 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1578 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1579 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1580 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1581 .vidioc_g_audio = vidioc_g_audio,
1582 .vidioc_s_audio = vidioc_s_audio,
1583 .vidioc_cropcap = vidioc_cropcap,
1584
1585 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1586 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1587 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1588
1589 .vidioc_reqbufs = vidioc_reqbufs,
1590 .vidioc_querybuf = vidioc_querybuf,
1591 .vidioc_qbuf = vidioc_qbuf,
1592 .vidioc_dqbuf = vidioc_dqbuf,
1593 .vidioc_s_std = vidioc_s_std,
1594 .vidioc_enum_input = vidioc_enum_input,
1595 .vidioc_g_input = vidioc_g_input,
1596 .vidioc_s_input = vidioc_s_input,
1597 .vidioc_queryctrl = vidioc_queryctrl,
1598 .vidioc_g_ctrl = vidioc_g_ctrl,
1599 .vidioc_s_ctrl = vidioc_s_ctrl,
1600 .vidioc_streamon = vidioc_streamon,
1601 .vidioc_streamoff = vidioc_streamoff,
1602 .vidioc_g_tuner = vidioc_g_tuner,
1603 .vidioc_s_tuner = vidioc_s_tuner,
1604 .vidioc_g_frequency = vidioc_g_frequency,
1605 .vidioc_s_frequency = vidioc_s_frequency,
1606
1607 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001608 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001609};
1610
1611
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001612/******************************** usb interface *****************************************/
1613
1614/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001615 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001616 * allocates and inits the device structs, registers i2c bus and v4l device
1617 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001618static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001619 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001620{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001621 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001622 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001623 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001624 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001625
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001626 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001627 mutex_init(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001628 spin_lock_init(&dev->queue_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001629 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001630 init_waitqueue_head(&dev->wait_frame);
1631 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001632
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001633 dev->em28xx_write_regs = em28xx_write_regs;
1634 dev->em28xx_read_reg = em28xx_read_reg;
1635 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1636 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1637 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001638 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001639
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001640 em28xx_pre_card_setup(dev);
1641
1642 errCode = em28xx_config(dev);
1643 if (errCode) {
1644 em28xx_errdev("error configuring device\n");
1645 em28xx_devused &= ~(1<<dev->devno);
1646 kfree(dev);
1647 return -ENOMEM;
1648 }
1649
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001650 /* register i2c bus */
1651 em28xx_i2c_register(dev);
1652
1653 /* Do board specific init and eeprom reading */
1654 em28xx_card_setup(dev);
1655
1656 /* configure the device */
1657 em28xx_config_i2c(dev);
1658
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001659 /* set default norm */
1660 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001661
1662 maxw = norm_maxw(dev);
1663 maxh = norm_maxh(dev);
1664
1665 /* set default image size */
1666 dev->width = maxw;
1667 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001668 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001669 dev->field_size = dev->width * dev->height;
1670 dev->frame_size =
1671 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1672 dev->bytesperline = dev->width * 2;
1673 dev->hscale = 0;
1674 dev->vscale = 0;
1675 dev->ctl_input = 2;
1676
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001677 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001679 /* allocate and fill video video_device struct */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001680 dev->vdev = video_device_alloc();
1681 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001682 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001683 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001684 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001685 return -ENOMEM;
1686 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001687 memcpy(dev->vdev, &em28xx_video_template,
1688 sizeof(em28xx_video_template));
1689 dev->vdev->type = VID_TYPE_CAPTURE;
1690 if (dev->has_tuner)
1691 dev->vdev->type |= VID_TYPE_TUNER;
1692 dev->vdev->dev = &dev->udev->dev;
1693 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name),
1694 "%s#%d %s", "em28xx", dev->devno, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001695
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001696 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001697 dev->vbi_dev = video_device_alloc();
1698 if (NULL == dev->vbi_dev) {
1699 em28xx_errdev("cannot allocate video_device.\n");
1700 kfree(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001701 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001702 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001703 return -ENOMEM;
1704 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001705 memcpy(dev->vbi_dev, &em28xx_video_template,
1706 sizeof(em28xx_video_template));
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001707 dev->vbi_dev->type = VFL_TYPE_VBI;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001708 dev->vbi_dev->dev = &dev->udev->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001709 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name),
1710 "%s#%d %s", "em28xx", dev->devno, "vbi");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001711
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001712 list_add_tail(&dev->devlist,&em28xx_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713
Sascha Sommer5a804152007-11-03 21:22:38 -03001714 if (dev->has_msp34xx) {
1715 /* Send a reset to other chips via gpio */
1716 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1717 msleep(3);
1718 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1719 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03001720 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001721
Sascha Sommer5a804152007-11-03 21:22:38 -03001722 video_mux(dev, 0);
1723
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001724 /* register v4l2 video video_device */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001725 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1726 video_nr[dev->devno]))) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001727 em28xx_errdev("unable to register video device (error=%i).\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728 retval);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001729 mutex_unlock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001730 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001731 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001732 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001733 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001734 return -ENODEV;
1735 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001736
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001737 /* register v4l2 vbi video_device */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001738 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1739 vbi_nr[dev->devno]) < 0) {
1740 printk("unable to register vbi device\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001741 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001742 list_del(&dev->devlist);
1743 video_device_release(dev->vbi_dev);
1744 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001745 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001746 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001747 return -ENODEV;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001748 }
1749
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001750 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1751 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1752 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001753
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001754 return 0;
1755}
1756
1757/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001758 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001759 * checks for supported devices
1760 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001761static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001762 const struct usb_device_id *id)
1763{
1764 const struct usb_endpoint_descriptor *endpoint;
1765 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001766 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001767 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001768 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001769 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001770
1771 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001772 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1773
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001774 /* Check to see next free device and mark as used */
1775 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1776 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001777
1778 /* Don't register audio interfaces */
1779 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001780 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001781 udev->descriptor.idVendor,udev->descriptor.idProduct,
1782 ifnum,
1783 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001784
1785 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001786 return -ENODEV;
1787 }
1788
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001789 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001790 udev->descriptor.idVendor,udev->descriptor.idProduct,
1791 ifnum,
1792 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001793
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001794 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1795
Michael Opdenacker59c51592007-05-09 08:57:56 +02001796 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001797 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1798 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001799 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001800 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001801 return -ENODEV;
1802 }
1803 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001804 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001805 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001806 return -ENODEV;
1807 }
1808
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03001809 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001810 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001811 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001812 return -ENOMEM;
1813 }
1814
1815 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001816 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001817 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001818 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001819 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001820 return -ENOMEM;
1821 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001822
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001823 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001824 dev->devno = nr;
1825 dev->model = id->driver_info;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001826
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001827 /* compute alternate max packet sizes */
1828 uif = udev->actconfig->interface[0];
1829
1830 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001831 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001832// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1833 dev->alt_max_pkt_size = kmalloc(32*
1834 dev->num_alt,GFP_KERNEL);
1835 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001836 em28xx_errdev("out of memory!\n");
1837 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02001838 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001839 return -ENOMEM;
1840 }
1841
1842 for (i = 0; i < dev->num_alt ; i++) {
1843 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1844 wMaxPacketSize);
1845 dev->alt_max_pkt_size[i] =
1846 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001847 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001848 dev->alt_max_pkt_size[i]);
1849 }
1850
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001851 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001852 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001853
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001854 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001855 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001856 if (retval)
1857 return retval;
1858
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001859 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001860
1861 /* save our data pointer in this interface device */
1862 usb_set_intfdata(interface, dev);
1863 return 0;
1864}
1865
1866/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001867 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001868 * called when the device gets diconencted
1869 * video device will be unregistered on v4l2_close in case it is still open
1870 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001871static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001872{
Sascha Sommer5a804152007-11-03 21:22:38 -03001873 struct em28xx *dev;
1874
1875 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001876 usb_set_intfdata(interface, NULL);
1877
1878 if (!dev)
1879 return;
1880
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001881 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001882
Sascha Sommer5a804152007-11-03 21:22:38 -03001883 /* wait until all current v4l2 io is finished then deallocate resources */
1884 mutex_lock(&dev->lock);
1885
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886 wake_up_interruptible_all(&dev->open);
1887
1888 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001889 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001890 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001891 "deallocation are deferred on close.\n",
1892 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1893
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001894 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001895 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001896 dev->state |= DEV_DISCONNECTED;
1897 wake_up_interruptible(&dev->wait_frame);
1898 wake_up_interruptible(&dev->wait_stream);
1899 } else {
1900 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001901 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001902 }
1903
Sascha Sommer5a804152007-11-03 21:22:38 -03001904
Ingo Molnar3593cab2006-02-07 06:49:14 -02001905 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001906
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001907 if (!dev->users) {
1908 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001909 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001910 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001911
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001912}
1913
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001914static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001915 .name = "em28xx",
1916 .probe = em28xx_usb_probe,
1917 .disconnect = em28xx_usb_disconnect,
1918 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001919};
1920
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001921static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001922{
1923 int result;
1924
1925 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001926 (EM28XX_VERSION_CODE >> 16) & 0xff,
1927 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001928#ifdef SNAPSHOT
1929 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1930 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1931#endif
1932
1933 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001934 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001935 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001936 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001937 " usb_register failed. Error number %d.\n", result);
1938
1939 return result;
1940}
1941
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001942static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001943{
1944 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001945 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001946}
1947
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001948module_init(em28xx_module_init);
1949module_exit(em28xx_module_exit);