blob: 06d76879bde27abf1c55af5396e90167af4ae725 [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 Chehab4ac97912005-11-08 21:37:43 -08006 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/init.h>
25#include <linux/list.h>
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080029#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080030#include <linux/version.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080031#include <linux/video_decoder.h>
32
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080033#include "em28xx.h"
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080034#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080036#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
37 "Markus Rechberger <mrechberger@gmail.com>, " \
38 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
39 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080040
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080041#define DRIVER_NAME "em28xx"
42#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080043#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080044
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080045#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080046 if (video_debug) \
47 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020048 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080049
50MODULE_AUTHOR(DRIVER_AUTHOR);
51MODULE_DESCRIPTION(DRIVER_DESC);
52MODULE_LICENSE("GPL");
53
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080054static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080055
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080056static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080057module_param_array(card, int, NULL, 0444);
58MODULE_PARM_DESC(card,"card type");
59
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080060static int tuner = -1;
61module_param(tuner, int, 0444);
62MODULE_PARM_DESC(tuner, "tuner type");
63
64static unsigned int video_debug = 0;
65module_param(video_debug,int,0644);
66MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
67
68/* supported tv norms */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080069static struct em28xx_tvnorm tvnorms[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080070 {
71 .name = "PAL",
72 .id = V4L2_STD_PAL,
73 .mode = VIDEO_MODE_PAL,
74 }, {
75 .name = "NTSC",
76 .id = V4L2_STD_NTSC,
77 .mode = VIDEO_MODE_NTSC,
78 }, {
79 .name = "SECAM",
80 .id = V4L2_STD_SECAM,
81 .mode = VIDEO_MODE_SECAM,
82 }, {
83 .name = "PAL-M",
84 .id = V4L2_STD_PAL_M,
85 .mode = VIDEO_MODE_PAL,
86 }
87};
88
Markus Rechbergera38a7d42005-11-08 21:38:06 -080089static const unsigned char saa7114_i2c_init[] = {
90 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
91 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
92 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
93 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
94 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
95 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
96 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
97 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
98 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
99 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
100 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
101 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
102 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
103 0xbe,0x00,0xbf,0x00
104};
105
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800106#define TVNORMS ARRAY_SIZE(tvnorms)
107
108/* supported controls */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800109static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800110 {
111 .id = V4L2_CID_BRIGHTNESS,
112 .type = V4L2_CTRL_TYPE_INTEGER,
113 .name = "Brightness",
114 .minimum = -128,
115 .maximum = 127,
116 .step = 1,
117 .default_value = 0,
118 .flags = 0,
119 },{
120 .id = V4L2_CID_CONTRAST,
121 .type = V4L2_CTRL_TYPE_INTEGER,
122 .name = "Contrast",
123 .minimum = 0x0,
124 .maximum = 0x1f,
125 .step = 0x1,
126 .default_value = 0x10,
127 .flags = 0,
128 },{
129 .id = V4L2_CID_SATURATION,
130 .type = V4L2_CTRL_TYPE_INTEGER,
131 .name = "Saturation",
132 .minimum = 0x0,
133 .maximum = 0x1f,
134 .step = 0x1,
135 .default_value = 0x10,
136 .flags = 0,
137 },{
138 .id = V4L2_CID_AUDIO_VOLUME,
139 .type = V4L2_CTRL_TYPE_INTEGER,
140 .name = "Volume",
141 .minimum = 0x0,
142 .maximum = 0x1f,
143 .step = 0x1,
144 .default_value = 0x1f,
145 .flags = 0,
146 },{
147 .id = V4L2_CID_AUDIO_MUTE,
148 .type = V4L2_CTRL_TYPE_BOOLEAN,
149 .name = "Mute",
150 .minimum = 0,
151 .maximum = 1,
152 .step = 1,
153 .default_value = 1,
154 .flags = 0,
155 },{
156 .id = V4L2_CID_RED_BALANCE,
157 .type = V4L2_CTRL_TYPE_INTEGER,
158 .name = "Red chroma balance",
159 .minimum = -128,
160 .maximum = 127,
161 .step = 1,
162 .default_value = 0,
163 .flags = 0,
164 },{
165 .id = V4L2_CID_BLUE_BALANCE,
166 .type = V4L2_CTRL_TYPE_INTEGER,
167 .name = "Blue chroma balance",
168 .minimum = -128,
169 .maximum = 127,
170 .step = 1,
171 .default_value = 0,
172 .flags = 0,
173 },{
174 .id = V4L2_CID_GAMMA,
175 .type = V4L2_CTRL_TYPE_INTEGER,
176 .name = "Gamma",
177 .minimum = 0x0,
178 .maximum = 0x3f,
179 .step = 0x1,
180 .default_value = 0x20,
181 .flags = 0,
182 }
183};
184
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800185static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800186
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800187static DECLARE_MUTEX(em28xx_sysfs_lock);
188static DECLARE_RWSEM(em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800189
190/********************* v4l2 interface ******************************************/
191
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800192/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800193 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800194 * inits registers with sane defaults
195 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800196static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800197{
198
199 /* Sets I2C speed to 100 KHz */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800200 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800201
202 /* enable vbi capturing */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800203 em28xx_audio_usb_mute(dev, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800204 dev->mute = 1; /* maybe not the right place... */
205 dev->volume = 0x1f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800206 em28xx_audio_analog_set(dev);
207 em28xx_audio_analog_setup(dev);
208 em28xx_outfmt_set_yuv422(dev);
209 em28xx_colorlevels_set_default(dev);
210 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800211
212 return 0;
213}
214
215/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800216 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800217 * configure i2c attached devices
218 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800219static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800220{
221 struct v4l2_frequency f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800222 struct video_decoder_init em28xx_vdi = {.data = NULL };
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800223
224
225 /* configure decoder */
Markus Rechbergera38a7d42005-11-08 21:38:06 -0800226 if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800227 em28xx_vdi.data=saa7114_i2c_init;
228 em28xx_vdi.len=sizeof(saa7114_i2c_init);
Markus Rechbergera38a7d42005-11-08 21:38:06 -0800229 }
230
231
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800232 em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi);
233 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
234/* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
235/* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
236/* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
237/* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800238
239 /* configure tuner */
240 f.tuner = 0;
241 f.type = V4L2_TUNER_ANALOG_TV;
242 f.frequency = 9076; /* FIXME:remove magic number */
243 dev->ctl_freq = f.frequency;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800244 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800245
246 /* configure tda9887 */
247
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800248
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800249/* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800250}
251
252/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800253 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800254 * prepare queues for incoming and outgoing frames
255 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800256static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800257{
258 u32 i;
259
260 INIT_LIST_HEAD(&dev->inqueue);
261 INIT_LIST_HEAD(&dev->outqueue);
262
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800263 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800264 dev->frame[i].state = F_UNUSED;
265 dev->frame[i].buf.bytesused = 0;
266 }
267}
268
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800269static void video_mux(struct em28xx *dev, int index)
270{
271 int input, ainput;
272
273 input = INPUT(index)->vmux;
274 dev->ctl_input = index;
275 dev->ctl_ainput = INPUT(index)->amux;
276
277 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
278
279
280 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput);
281
282 if (dev->has_msp34xx) {
283 em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput);
284 ainput = EM28XX_AUDIO_SRC_TUNER;
285 em28xx_audio_source(dev, ainput);
286 } else {
287 switch (dev->ctl_ainput) {
288 case 0:
289 ainput = EM28XX_AUDIO_SRC_TUNER;
290 break;
291 default:
292 ainput = EM28XX_AUDIO_SRC_LINE;
293 }
294 em28xx_audio_source(dev, ainput);
295 }
296}
297
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800298/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800299 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800300 * inits the device and starts isoc transfer
301 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800302static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800303{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800304 int minor = iminor(inode);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800305 int errCode = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800306 struct em28xx *h,*dev = NULL;
Markus Rechberger9c755412005-11-08 21:37:52 -0800307 struct list_head *list;
308
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800309 list_for_each(list,&em28xx_devlist) {
310 h = list_entry(list, struct em28xx, devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -0800311 if (h->vdev->minor == minor) {
312 dev = h;
313 }
314 }
315
316 filp->private_data=dev;
317
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800318
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800319 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800320
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800321 if (!down_read_trylock(&em28xx_disconnect))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800322 return -ERESTARTSYS;
323
324 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800325 em28xx_warn("this driver can be opened only once\n");
326 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800327 return -EBUSY;
328 }
329
330/* if(dev->vbi_dev->minor == minor){
331 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
332 }*/
333 if (dev->vdev->minor == minor) {
334 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
335 }
336
337 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
338 spin_lock_init(&dev->queue_lock);
339 init_waitqueue_head(&dev->wait_frame);
340 init_waitqueue_head(&dev->wait_stream);
341
342 down(&dev->lock);
343
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800344 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800345
346 dev->width = norm_maxw(dev);
347 dev->height = norm_maxh(dev);
348 dev->frame_size = dev->width * dev->height * 2;
349 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
350 dev->bytesperline = dev->width * 2;
351 dev->hscale = 0;
352 dev->vscale = 0;
353
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800354 em28xx_capture_start(dev, 1);
355 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800356
357 /* start the transfer */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800358 errCode = em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800359 if (errCode)
360 goto err;
361
362 dev->users++;
363 filp->private_data = dev;
364 dev->io = IO_NONE;
365 dev->stream = STREAM_OFF;
366 dev->num_frames = 0;
367
368 /* prepare queues */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800369 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800370
371 dev->state |= DEV_INITIALIZED;
372
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800373 video_mux(dev, 0);
374
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800375 err:
376 up(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800377 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800378 return errCode;
379}
380
381/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800382 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800383 * unregisters the v4l2,i2c and usb devices
384 * called when the device gets disconected or at module unload
385*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800386static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800387{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800388 down(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800389
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800390 em28xx_info("V4L2 device /dev/video%d deregistered\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800391 dev->vdev->minor);
Markus Rechberger9c755412005-11-08 21:37:52 -0800392 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800393 video_unregister_device(dev->vdev);
394/* video_unregister_device(dev->vbi_dev); */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800395 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800396 usb_put_dev(dev->udev);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800397 up(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800398}
399
400/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800401 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800402 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
403 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800404static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800405{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800406 int errCode;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800407 struct em28xx *dev=filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800408
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800409 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800410
411 down(&dev->lock);
412
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800413 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800414
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800415 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800416
417 /* the device is already disconnect, free the remaining resources */
418 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800419 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800420 up(&dev->lock);
421 kfree(dev);
422 return 0;
423 }
424
425 /* set alternate 0 */
426 dev->alt = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800427 em28xx_videodbg("setting alternate 0\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800428 errCode = usb_set_interface(dev->udev, 0, 0);
429 if (errCode < 0) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800430 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800431 errCode);
432 }
433
434 dev->users--;
435 wake_up_interruptible_nr(&dev->open, 1);
436 up(&dev->lock);
437 return 0;
438}
439
440/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800441 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800442 * will allocate buffers when called for the first time
443 */
444static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800445em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800446 loff_t * f_pos)
447{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800448 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800449 unsigned long lock_flags;
450 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800451 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800452
453 if (down_interruptible(&dev->fileop_lock))
454 return -ERESTARTSYS;
455
456 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800457 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800458 up(&dev->fileop_lock);
459 return -ENODEV;
460 }
461
462 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800463 em28xx_videodbg("device misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800464 up(&dev->fileop_lock);
465 return -EIO;
466 }
467
468 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800469 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800470 " the device again to choose the read method\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800471 up(&dev->fileop_lock);
472 return -EINVAL;
473 }
474
475 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800476 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
477 em28xx_errdev("read failed, not enough memory\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800478 up(&dev->fileop_lock);
479 return -ENOMEM;
480 }
481 dev->io = IO_READ;
482 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800483 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800484 }
485
486 if (!count) {
487 up(&dev->fileop_lock);
488 return 0;
489 }
490
491 if (list_empty(&dev->outqueue)) {
492 if (filp->f_flags & O_NONBLOCK) {
493 up(&dev->fileop_lock);
494 return -EAGAIN;
495 }
496 ret = wait_event_interruptible
497 (dev->wait_frame,
498 (!list_empty(&dev->outqueue)) ||
499 (dev->state & DEV_DISCONNECTED));
500 if (ret) {
501 up(&dev->fileop_lock);
502 return ret;
503 }
504 if (dev->state & DEV_DISCONNECTED) {
505 up(&dev->fileop_lock);
506 return -ENODEV;
507 }
508 }
509
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800510 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511
512 spin_lock_irqsave(&dev->queue_lock, lock_flags);
513 list_for_each_entry(i, &dev->outqueue, frame)
514 i->state = F_UNUSED;
515 INIT_LIST_HEAD(&dev->outqueue);
516 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
517
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800518 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800519
520 if (count > f->buf.length)
521 count = f->buf.length;
522
523 if (copy_to_user(buf, f->bufmem, count)) {
524 up(&dev->fileop_lock);
525 return -EFAULT;
526 }
527 *f_pos += count;
528
529 up(&dev->fileop_lock);
530
531 return count;
532}
533
534/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800535 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800536 * will allocate buffers when called for the first time
537 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800538static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800539{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800540 unsigned int mask = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800541 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800542
543 if (down_interruptible(&dev->fileop_lock))
544 return POLLERR;
545
546 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800547 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800548 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800549 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800550 } else {
551 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800552 if (!em28xx_request_buffers
553 (dev, EM28XX_NUM_READ_FRAMES)) {
554 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800555 ("poll() failed, not enough memory\n");
556 } else {
557 dev->io = IO_READ;
558 dev->stream = STREAM_ON;
559 }
560 }
561
562 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800563 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800564 poll_wait(filp, &dev->wait_frame, wait);
565
566 if (!list_empty(&dev->outqueue))
567 mask |= POLLIN | POLLRDNORM;
568
569 up(&dev->fileop_lock);
570
571 return mask;
572 }
573 }
574
575 up(&dev->fileop_lock);
576 return POLLERR;
577}
578
579/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800580 * em28xx_vm_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800581 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800582static void em28xx_vm_open(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800583{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800584 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800585 f->vma_use_count++;
586}
587
588/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800589 * em28xx_vm_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800590 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800591static void em28xx_vm_close(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800592{
593 /* NOTE: buffers are not freed here */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800594 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800595 f->vma_use_count--;
596}
597
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800598static struct vm_operations_struct em28xx_vm_ops = {
599 .open = em28xx_vm_open,
600 .close = em28xx_vm_close,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800601};
602
603/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800604 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800605 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800606static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800607{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800608 unsigned long size = vma->vm_end - vma->vm_start,
Sascha Sommer3639c862005-12-12 00:37:30 -0800609 start = vma->vm_start;
610 void *pos;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800611 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -0800612
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800613 struct em28xx *dev = filp->private_data;
Markus Rechberger9c755412005-11-08 21:37:52 -0800614
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800615 if (down_interruptible(&dev->fileop_lock))
616 return -ERESTARTSYS;
617
618 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800619 em28xx_videodbg("mmap: device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800620 up(&dev->fileop_lock);
621 return -ENODEV;
622 }
623
624 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800625 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800626 "open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800627 up(&dev->fileop_lock);
628 return -EIO;
629 }
630
631 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
632 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
633 up(&dev->fileop_lock);
634 return -EINVAL;
635 }
636
637 for (i = 0; i < dev->num_frames; i++) {
638 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
639 break;
640 }
641 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800642 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800643 up(&dev->fileop_lock);
644 return -EINVAL;
645 }
646
647 /* VM_IO is eventually going to replace PageReserved altogether */
648 vma->vm_flags |= VM_IO;
649 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
650
Sascha Sommer3639c862005-12-12 00:37:30 -0800651 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800652 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -0800653 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
654 em28xx_videodbg("mmap: vm_insert_page failed\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800655 up(&dev->fileop_lock);
656 return -EAGAIN;
657 }
658 start += PAGE_SIZE;
659 pos += PAGE_SIZE;
660 size -= PAGE_SIZE;
661 }
662
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800663 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800664 vma->vm_private_data = &dev->frame[i];
665
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800666 em28xx_vm_open(vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800667 up(&dev->fileop_lock);
668 return 0;
669}
670
671/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800672 * em28xx_get_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800673 * return the current saturation, brightness or contrast, mute state
674 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800675static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800676{
677 s32 tmp;
678 switch (ctrl->id) {
679 case V4L2_CID_AUDIO_MUTE:
680 ctrl->value = dev->mute;
681 return 0;
682 case V4L2_CID_AUDIO_VOLUME:
683 ctrl->value = dev->volume;
684 return 0;
685 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800686 if ((tmp = em28xx_brightness_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800687 return -EIO;
688 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
689 return 0;
690 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800691 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800692 return -EIO;
693 return 0;
694 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800695 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800696 return -EIO;
697 return 0;
698 case V4L2_CID_RED_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800699 if ((tmp = em28xx_v_balance_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800700 return -EIO;
701 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
702 return 0;
703 case V4L2_CID_BLUE_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800704 if ((tmp = em28xx_u_balance_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800705 return -EIO;
706 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
707 return 0;
708 case V4L2_CID_GAMMA:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800709 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800710 return -EIO;
711 return 0;
712 default:
713 return -EINVAL;
714 }
715}
716
717/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800718 * em28xx_set_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800719 * mute or set new saturation, brightness or contrast
720 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800721static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800722{
723 switch (ctrl->id) {
724 case V4L2_CID_AUDIO_MUTE:
725 if (ctrl->value != dev->mute) {
726 dev->mute = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800727 em28xx_audio_usb_mute(dev, ctrl->value);
728 return em28xx_audio_analog_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800729 }
730 return 0;
731 case V4L2_CID_AUDIO_VOLUME:
732 dev->volume = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800733 return em28xx_audio_analog_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800734 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800735 return em28xx_brightness_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800736 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800737 return em28xx_contrast_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800738 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800739 return em28xx_saturation_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800740 case V4L2_CID_RED_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800741 return em28xx_v_balance_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800742 case V4L2_CID_BLUE_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800743 return em28xx_u_balance_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800744 case V4L2_CID_GAMMA:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800745 return em28xx_gamma_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800746 default:
747 return -EINVAL;
748 }
749}
750
751/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800752 * em28xx_stream_interrupt()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800753 * stops streaming
754 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800755static int em28xx_stream_interrupt(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800756{
757 int ret = 0;
758
759 /* stop reading from the device */
760
761 dev->stream = STREAM_INTERRUPT;
762 ret = wait_event_timeout(dev->wait_stream,
763 (dev->stream == STREAM_OFF) ||
764 (dev->state & DEV_DISCONNECTED),
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800765 EM28XX_URB_TIMEOUT);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800766 if (dev->state & DEV_DISCONNECTED)
767 return -ENODEV;
768 else if (ret) {
769 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800770 em28xx_videodbg("device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800771 "open /dev/video%d again\n", dev->vdev->minor);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800772 return ret;
773 }
774
775 return 0;
776}
777
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800778static int em28xx_set_norm(struct em28xx *dev, int width, int height)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800779{
780 unsigned int hscale, vscale;
781 unsigned int maxh, maxw;
782
783 maxw = norm_maxw(dev);
784 maxh = norm_maxh(dev);
785
786 /* width must even because of the YUYV format */
787 /* height must be even because of interlacing */
788 height &= 0xfffe;
789 width &= 0xfffe;
790
791 if (height < 32)
792 height = 32;
793 if (height > maxh)
794 height = maxh;
795 if (width < 48)
796 width = 48;
797 if (width > maxw)
798 width = maxw;
799
800 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
801 hscale = 0x3fff;
802 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
803
804 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
805 vscale = 0x3fff;
806 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
807
808 /* set new image size */
809 dev->width = width;
810 dev->height = height;
811 dev->frame_size = dev->width * dev->height * 2;
812 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
813 dev->bytesperline = dev->width * 2;
814 dev->hscale = hscale;
815 dev->vscale = vscale;
816
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800817 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800818
819 return 0;
820}
821
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800822/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800823 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800824 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800825 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800826 * copying is done already, arg is a kernel pointer.
827 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800828static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
829 struct em28xx *dev, unsigned int cmd, void *arg,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800830 v4l2_kioctl driver_ioctl)
831{
832 int ret;
833
834 switch (cmd) {
835 /* ---------- tv norms ---------- */
836 case VIDIOC_ENUMSTD:
837 {
838 struct v4l2_standard *e = arg;
839 unsigned int i;
840
841 i = e->index;
842 if (i >= TVNORMS)
843 return -EINVAL;
844 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
845 tvnorms[e->index].name);
846 e->index = i;
847 if (ret < 0)
848 return ret;
849 return 0;
850 }
851 case VIDIOC_G_STD:
852 {
853 v4l2_std_id *id = arg;
854
855 *id = dev->tvnorm->id;
856 return 0;
857 }
858 case VIDIOC_S_STD:
859 {
860 v4l2_std_id *id = arg;
861 unsigned int i;
862
863 for (i = 0; i < TVNORMS; i++)
864 if (*id == tvnorms[i].id)
865 break;
866 if (i == TVNORMS)
867 for (i = 0; i < TVNORMS; i++)
868 if (*id & tvnorms[i].id)
869 break;
870 if (i == TVNORMS)
871 return -EINVAL;
872
873 down(&dev->lock);
874 dev->tvnorm = &tvnorms[i];
875
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800876 em28xx_set_norm(dev, dev->width, dev->height);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800877
878/*
879 dev->width=norm_maxw(dev);
880 dev->height=norm_maxh(dev);
881 dev->frame_size=dev->width*dev->height*2;
882 dev->field_size=dev->frame_size>>1;
883 dev->bytesperline=dev->width*2;
884 dev->hscale=0;
885 dev->vscale=0;
886
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800887 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800888*/
889/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800890 em28xx_uninit_isoc(dev);
891 em28xx_set_alternate(dev);
892 em28xx_capture_start(dev, 1);
893 em28xx_resolution_set(dev);
894 em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800895*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800896 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800897 &tvnorms[i].mode);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800898 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800899 &dev->tvnorm->id);
900
901 up(&dev->lock);
902
903 return 0;
904 }
905
906 /* ------ input switching ---------- */
907 case VIDIOC_ENUMINPUT:
908 {
909 struct v4l2_input *i = arg;
910 unsigned int n;
911 static const char *iname[] = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800912 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
913 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
914 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
915 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
916 [EM28XX_VMUX_SVIDEO] = "S-Video",
917 [EM28XX_VMUX_TELEVISION] = "Television",
918 [EM28XX_VMUX_CABLE] = "Cable TV",
919 [EM28XX_VMUX_DVB] = "DVB",
920 [EM28XX_VMUX_DEBUG] = "for debug only",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800921 };
922
923 n = i->index;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800924 if (n >= MAX_EM28XX_INPUT)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800925 return -EINVAL;
926 if (0 == INPUT(n)->type)
927 return -EINVAL;
928 memset(i, 0, sizeof(*i));
929 i->index = n;
930 i->type = V4L2_INPUT_TYPE_CAMERA;
931 strcpy(i->name, iname[INPUT(n)->type]);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800932 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
933 (EM28XX_VMUX_CABLE == INPUT(n)->type))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800934 i->type = V4L2_INPUT_TYPE_TUNER;
935 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
936 i->std |= tvnorms[n].id;
937 return 0;
938 }
939
940 case VIDIOC_G_INPUT:
941 {
942 int *i = arg;
943 *i = dev->ctl_input;
944
945 return 0;
946 }
947
948 case VIDIOC_S_INPUT:
949 {
950 int *index = arg;
951
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800952 if (*index >= MAX_EM28XX_INPUT)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800953 return -EINVAL;
954 if (0 == INPUT(*index)->type)
955 return -EINVAL;
956
957 down(&dev->lock);
958 video_mux(dev, *index);
959 up(&dev->lock);
960
961 return 0;
962 }
963
964 case VIDIOC_G_AUDIO:
965 {
966 struct v4l2_audio *a = arg;
967 unsigned int index = a->index;
968
969 if (a->index > 1)
970 return -EINVAL;
971 memset(a, 0, sizeof(*a));
972 index = dev->ctl_ainput;
973
974 if (index == 0) {
975 strcpy(a->name, "Television");
976 } else {
977 strcpy(a->name, "Line In");
978 }
979 a->capability = V4L2_AUDCAP_STEREO;
980 a->index = index;
981 return 0;
982 }
983
984 case VIDIOC_S_AUDIO:
985 {
986 struct v4l2_audio *a = arg;
987 if (a->index != dev->ctl_ainput)
988 return -EINVAL;
989
990 return 0;
991 }
992
993 /* --- controls ---------------------------------------------- */
994 case VIDIOC_QUERYCTRL:
995 {
996 struct v4l2_queryctrl *qc = arg;
997 u8 i, n;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800998 n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800999 for (i = 0; i < n; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001000 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1001 memcpy(qc, &(em28xx_qctrl[i]),
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001002 sizeof(*qc));
1003 return 0;
1004 }
1005
1006 return -EINVAL;
1007 }
1008
1009 case VIDIOC_G_CTRL:
1010 {
1011 struct v4l2_control *ctrl = arg;
1012
1013
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001014 return em28xx_get_ctrl(dev, ctrl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001015 }
1016
1017 case VIDIOC_S_CTRL_OLD: /* ??? */
1018 case VIDIOC_S_CTRL:
1019 {
1020 struct v4l2_control *ctrl = arg;
1021 u8 i, n;
1022
1023
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001024 n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001025 for (i = 0; i < n; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001026 if (ctrl->id == em28xx_qctrl[i].id) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001027 if (ctrl->value <
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001028 em28xx_qctrl[i].minimum
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001029 || ctrl->value >
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001030 em28xx_qctrl[i].maximum)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001031 return -ERANGE;
1032
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001033 return em28xx_set_ctrl(dev, ctrl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001034 }
1035 return -EINVAL;
1036 }
1037
1038 /* --- tuner ioctls ------------------------------------------ */
1039 case VIDIOC_G_TUNER:
1040 {
1041 struct v4l2_tuner *t = arg;
1042 int status = 0;
1043
1044 if (0 != t->index)
1045 return -EINVAL;
1046
1047 memset(t, 0, sizeof(*t));
1048 strcpy(t->name, "Tuner");
1049 t->type = V4L2_TUNER_ANALOG_TV;
1050 t->capability = V4L2_TUNER_CAP_NORM;
1051 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1052/* t->signal = 0xffff;*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001053/* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001054 /* No way to get signal strength? */
1055 down(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001056 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001057 &status);
1058 up(&dev->lock);
1059 t->signal =
1060 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1061
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001062 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001063 t->afc);
1064 return 0;
1065 }
1066 case VIDIOC_S_TUNER:
1067 {
1068 struct v4l2_tuner *t = arg;
1069 int status = 0;
1070
1071 if (0 != t->index)
1072 return -EINVAL;
1073 memset(t, 0, sizeof(*t));
1074 strcpy(t->name, "Tuner");
1075 t->type = V4L2_TUNER_ANALOG_TV;
1076 t->capability = V4L2_TUNER_CAP_NORM;
1077 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1078/* t->signal = 0xffff; */
1079 /* No way to get signal strength? */
1080 down(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001081 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001082 &status);
1083 up(&dev->lock);
1084 t->signal =
1085 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1086
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001087 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001088 t->signal, t->afc);
1089 return 0;
1090 }
1091 case VIDIOC_G_FREQUENCY:
1092 {
1093 struct v4l2_frequency *f = arg;
1094
1095 memset(f, 0, sizeof(*f));
1096 f->type = V4L2_TUNER_ANALOG_TV;
1097 f->frequency = dev->ctl_freq;
1098
1099 return 0;
1100 }
1101 case VIDIOC_S_FREQUENCY:
1102 {
1103 struct v4l2_frequency *f = arg;
1104
1105 if (0 != f->tuner)
1106 return -EINVAL;
1107
1108 if (V4L2_TUNER_ANALOG_TV != f->type)
1109 return -EINVAL;
1110
1111 down(&dev->lock);
1112 dev->ctl_freq = f->frequency;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001113 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001114 up(&dev->lock);
1115 return 0;
1116 }
1117
1118 case VIDIOC_CROPCAP:
1119 {
1120 struct v4l2_cropcap *cc = arg;
1121
1122 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001123 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001124 cc->bounds.left = 0;
1125 cc->bounds.top = 0;
1126 cc->bounds.width = dev->width;
1127 cc->bounds.height = dev->height;
1128 cc->defrect = cc->bounds;
1129 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1130 cc->pixelaspect.denominator = 59;
1131 return 0;
1132 }
1133 case VIDIOC_STREAMON:
1134 {
1135 int *type = arg;
1136
1137 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1138 || dev->io != IO_MMAP)
1139 return -EINVAL;
1140
1141 if (list_empty(&dev->inqueue))
1142 return -EINVAL;
1143
1144 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1145
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001146 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001147
1148 return 0;
1149 }
1150 case VIDIOC_STREAMOFF:
1151 {
1152 int *type = arg;
1153 int ret;
1154
1155 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1156 || dev->io != IO_MMAP)
1157 return -EINVAL;
1158
1159 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001160 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001161 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001162 return ret;
1163 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001164 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001165
1166 return 0;
1167 }
1168 default:
1169 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1170 driver_ioctl);
1171 }
1172 return 0;
1173}
1174
1175/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001176 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001177 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001178 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001179 * copying is done already, arg is a kernel pointer.
1180 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001181static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001182 unsigned int cmd, void *arg)
1183{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001184 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001185
1186 if (!dev)
1187 return -ENODEV;
1188
1189 if (video_debug > 1)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001190 em28xx_print_ioctl(dev->name,cmd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001191
1192 switch (cmd) {
1193
1194 /* --- capabilities ------------------------------------------ */
1195 case VIDIOC_QUERYCAP:
1196 {
1197 struct v4l2_capability *cap = arg;
1198
1199 memset(cap, 0, sizeof(*cap));
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001200 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1201 strlcpy(cap->card, em28xx_boards[dev->model].name,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001202 sizeof(cap->card));
1203 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1204 sizeof(cap->bus_info));
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001205 cap->version = EM28XX_VERSION_CODE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001206 cap->capabilities =
1207 V4L2_CAP_VIDEO_CAPTURE |
1208 V4L2_CAP_AUDIO |
1209 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1210 if (dev->has_tuner)
1211 cap->capabilities |= V4L2_CAP_TUNER;
1212 return 0;
1213 }
1214
1215 /* --- capture ioctls ---------------------------------------- */
1216 case VIDIOC_ENUM_FMT:
1217 {
1218 struct v4l2_fmtdesc *fmtd = arg;
1219
1220 if (fmtd->index != 0)
1221 return -EINVAL;
1222 memset(fmtd, 0, sizeof(*fmtd));
1223 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1224 strcpy(fmtd->description, "Packed YUY2");
1225 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1226 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1227 return 0;
1228 }
1229
1230 case VIDIOC_G_FMT:
1231 {
1232 struct v4l2_format *format = arg;
1233
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001234 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001235 format->type ==
1236 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1237 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1238 V4L2_BUF_TYPE_VBI_CAPTURE ?
1239 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1240 "not supported");
1241
1242 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1243 return -EINVAL;
1244
1245 format->fmt.pix.width = dev->width;
1246 format->fmt.pix.height = dev->height;
1247 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1248 format->fmt.pix.bytesperline = dev->bytesperline;
1249 format->fmt.pix.sizeimage = dev->frame_size;
1250 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1251 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1252
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001253 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001254 dev->height);
1255 return 0;
1256 }
1257
1258 case VIDIOC_TRY_FMT:
1259 case VIDIOC_S_FMT:
1260 {
1261 struct v4l2_format *format = arg;
1262 u32 i;
1263 int ret = 0;
1264 int width = format->fmt.pix.width;
1265 int height = format->fmt.pix.height;
1266 unsigned int hscale, vscale;
1267 unsigned int maxh, maxw;
1268
1269 maxw = norm_maxw(dev);
1270 maxh = norm_maxh(dev);
1271
1272/* int both_fields; */
1273
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001274 em28xx_videodbg("%s: type=%s\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001275 cmd ==
1276 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1277 "VIDIOC_S_FMT",
1278 format->type ==
1279 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1280 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1281 V4L2_BUF_TYPE_VBI_CAPTURE ?
1282 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1283 "not supported");
1284
1285 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1286 return -EINVAL;
1287
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001288 em28xx_videodbg("%s: requested %dx%d\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001289 cmd ==
1290 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1291 "VIDIOC_S_FMT", format->fmt.pix.width,
1292 format->fmt.pix.height);
1293
1294 /* FIXME: Move some code away from here */
1295 /* width must even because of the YUYV format */
1296 /* height must be even because of interlacing */
1297 height &= 0xfffe;
1298 width &= 0xfffe;
1299
1300 if (height < 32)
1301 height = 32;
1302 if (height > maxh)
1303 height = maxh;
1304 if (width < 48)
1305 width = 48;
1306 if (width > maxw)
1307 width = maxw;
1308
Sascha Sommer74458e62005-11-08 21:37:33 -08001309 if(dev->is_em2800){
Sascha Sommer52c02fc2005-11-08 21:38:10 -08001310 /* the em2800 can only scale down to 50% */
Sascha Sommer74458e62005-11-08 21:37:33 -08001311 if(height % (maxh / 2))
1312 height=maxh;
1313 if(width % (maxw / 2))
1314 width=maxw;
Sascha Sommer52c02fc2005-11-08 21:38:10 -08001315 /* according to empiatech support */
1316 /* the MaxPacketSize is to small to support */
1317 /* framesizes larger than 640x480 @ 30 fps */
1318 /* or 640x576 @ 25 fps. As this would cut */
1319 /* of a part of the image we prefer */
1320 /* 360x576 or 360x480 for now */
Sascha Sommer74458e62005-11-08 21:37:33 -08001321 if(width == maxw && height == maxh)
1322 width /= 2;
1323 }
1324
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001325 if ((hscale =
1326 (((unsigned long)maxw) << 12) / width - 4096L) >=
1327 0x4000)
1328 hscale = 0x3fff;
1329 width =
1330 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1331
1332 if ((vscale =
1333 (((unsigned long)maxh) << 12) / height - 4096L) >=
1334 0x4000)
1335 vscale = 0x3fff;
1336 height =
1337 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1338
1339 format->fmt.pix.width = width;
1340 format->fmt.pix.height = height;
1341 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1342 format->fmt.pix.bytesperline = width * 2;
1343 format->fmt.pix.sizeimage = width * 2 * height;
1344 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1345 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1346
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001347 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001348 cmd ==
1349 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1350 "VIDIOC_S_FMT", format->fmt.pix.width,
1351 format->fmt.pix.height, hscale, vscale);
1352
1353 if (cmd == VIDIOC_TRY_FMT)
1354 return 0;
1355
1356 for (i = 0; i < dev->num_frames; i++)
1357 if (dev->frame[i].vma_use_count) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001358 em28xx_videodbg("VIDIOC_S_FMT failed. "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001359 "Unmap the buffers first.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001360 return -EINVAL;
1361 }
1362
1363 /* stop io in case it is already in progress */
1364 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001365 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001366 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001367 return ret;
1368 }
1369
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001370 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001371 dev->io = IO_NONE;
1372
1373 /* set new image size */
1374 dev->width = width;
1375 dev->height = height;
1376 dev->frame_size = dev->width * dev->height * 2;
1377 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1378 dev->bytesperline = dev->width * 2;
1379 dev->hscale = hscale;
1380 dev->vscale = vscale;
1381/* dev->both_fileds = both_fileds; */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001382 em28xx_uninit_isoc(dev);
1383 em28xx_set_alternate(dev);
1384 em28xx_capture_start(dev, 1);
1385 em28xx_resolution_set(dev);
1386 em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001387
1388 return 0;
1389 }
1390
1391 /* --- streaming capture ------------------------------------- */
1392 case VIDIOC_REQBUFS:
1393 {
1394 struct v4l2_requestbuffers *rb = arg;
1395 u32 i;
1396 int ret;
1397
1398 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1399 rb->memory != V4L2_MEMORY_MMAP)
1400 return -EINVAL;
1401
1402 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001403 em28xx_videodbg ("method is set to read;"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001404 " close and open the device again to"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001405 " choose the mmap I/O method\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001406 return -EINVAL;
1407 }
1408
1409 for (i = 0; i < dev->num_frames; i++)
1410 if (dev->frame[i].vma_use_count) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001411 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001412 return -EINVAL;
1413 }
1414
1415 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001416 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001417 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001418 return ret;
1419 }
1420
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001421 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001422
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001423 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001424 if (rb->count)
1425 rb->count =
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001426 em28xx_request_buffers(dev, rb->count);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001427
1428 dev->frame_current = NULL;
1429
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001430 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001431 rb->count);
1432 dev->io = rb->count ? IO_MMAP : IO_NONE;
1433 return 0;
1434 }
1435
1436 case VIDIOC_QUERYBUF:
1437 {
1438 struct v4l2_buffer *b = arg;
1439
1440 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1441 b->index >= dev->num_frames || dev->io != IO_MMAP)
1442 return -EINVAL;
1443
1444 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1445
1446 if (dev->frame[b->index].vma_use_count) {
1447 b->flags |= V4L2_BUF_FLAG_MAPPED;
1448 }
1449 if (dev->frame[b->index].state == F_DONE)
1450 b->flags |= V4L2_BUF_FLAG_DONE;
1451 else if (dev->frame[b->index].state != F_UNUSED)
1452 b->flags |= V4L2_BUF_FLAG_QUEUED;
1453 return 0;
1454 }
1455 case VIDIOC_QBUF:
1456 {
1457 struct v4l2_buffer *b = arg;
1458 unsigned long lock_flags;
1459
1460 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1461 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1462 return -EINVAL;
1463 }
1464
1465 if (dev->frame[b->index].state != F_UNUSED) {
1466 return -EAGAIN;
1467 }
1468 dev->frame[b->index].state = F_QUEUED;
1469
1470 /* add frame to fifo */
1471 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1472 list_add_tail(&dev->frame[b->index].frame,
1473 &dev->inqueue);
1474 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1475
1476 return 0;
1477 }
1478 case VIDIOC_DQBUF:
1479 {
1480 struct v4l2_buffer *b = arg;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001481 struct em28xx_frame_t *f;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001482 unsigned long lock_flags;
1483 int ret = 0;
1484
1485 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1486 || dev->io != IO_MMAP)
1487 return -EINVAL;
1488
1489 if (list_empty(&dev->outqueue)) {
1490 if (dev->stream == STREAM_OFF)
1491 return -EINVAL;
1492 if (filp->f_flags & O_NONBLOCK)
1493 return -EAGAIN;
1494 ret = wait_event_interruptible
1495 (dev->wait_frame,
1496 (!list_empty(&dev->outqueue)) ||
1497 (dev->state & DEV_DISCONNECTED));
1498 if (ret)
1499 return ret;
1500 if (dev->state & DEV_DISCONNECTED)
1501 return -ENODEV;
1502 }
1503
1504 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1505 f = list_entry(dev->outqueue.next,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001506 struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001507 list_del(dev->outqueue.next);
1508 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1509
1510 f->state = F_UNUSED;
1511 memcpy(b, &f->buf, sizeof(*b));
1512
1513 if (f->vma_use_count)
1514 b->flags |= V4L2_BUF_FLAG_MAPPED;
1515
1516 return 0;
1517 }
1518 default:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001519 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1520 em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001521 }
1522 return 0;
1523}
1524
1525/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001526 * em28xx_v4l2_ioctl()
1527 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001528 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001529static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001530 unsigned int cmd, unsigned long arg)
1531{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001532 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001533 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001534
1535 if (down_interruptible(&dev->fileop_lock))
1536 return -ERESTARTSYS;
1537
1538 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001539 em28xx_errdev("v4l2 ioctl: device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001540 up(&dev->fileop_lock);
1541 return -ENODEV;
1542 }
1543
1544 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001545 em28xx_errdev
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001546 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1547 up(&dev->fileop_lock);
1548 return -EIO;
1549 }
1550
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001551 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001552
1553 up(&dev->fileop_lock);
1554
1555 return ret;
1556}
1557
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001558static struct file_operations em28xx_v4l_fops = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001559 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001560 .open = em28xx_v4l2_open,
1561 .release = em28xx_v4l2_close,
1562 .ioctl = em28xx_v4l2_ioctl,
1563 .read = em28xx_v4l2_read,
1564 .poll = em28xx_v4l2_poll,
1565 .mmap = em28xx_v4l2_mmap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001566 .llseek = no_llseek,
1567};
1568
1569/******************************** usb interface *****************************************/
1570
1571/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001572 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001573 * allocates and inits the device structs, registers i2c bus and v4l device
1574 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001575static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001576 int minor, int model)
1577{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001578 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001579 int retval = -ENOMEM;
1580 int errCode, i;
1581 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001582
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001583 dev->udev = udev;
1584 dev->model = model;
1585 init_MUTEX(&dev->lock);
1586 init_waitqueue_head(&dev->open);
1587
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001588 dev->em28xx_write_regs = em28xx_write_regs;
1589 dev->em28xx_read_reg = em28xx_read_reg;
1590 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1591 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1592 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1593 dev->is_em2800 = em28xx_boards[model].is_em2800;
1594 dev->has_tuner = em28xx_boards[model].has_tuner;
1595 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1596 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1597 dev->decoder = em28xx_boards[model].decoder;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001598
1599 if (tuner >= 0)
1600 dev->tuner_type = tuner;
1601 else
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001602 dev->tuner_type = em28xx_boards[model].tuner_type;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001603
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001604 dev->video_inputs = em28xx_boards[model].vchannels;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001605
1606 for (i = 0; i < TVNORMS; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001607 if (em28xx_boards[model].norm == tvnorms[i].mode)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001608 break;
1609 if (i == TVNORMS)
1610 i = 0;
1611
1612 dev->tvnorm = &tvnorms[i]; /* set default norm */
1613
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001614 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001615
1616 maxw = norm_maxw(dev);
1617 maxh = norm_maxh(dev);
1618
1619 /* set default image size */
1620 dev->width = maxw;
1621 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001622 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001623 dev->field_size = dev->width * dev->height;
1624 dev->frame_size =
1625 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1626 dev->bytesperline = dev->width * 2;
1627 dev->hscale = 0;
1628 dev->vscale = 0;
1629 dev->ctl_input = 2;
1630
1631 /* setup video picture settings for saa7113h */
1632 memset(&dev->vpic, 0, sizeof(dev->vpic));
1633 dev->vpic.colour = 128 << 8;
1634 dev->vpic.hue = 128 << 8;
1635 dev->vpic.brightness = 128 << 8;
1636 dev->vpic.contrast = 192 << 8;
1637 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1638 dev->vpic.depth = 16;
1639 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1640
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001641#ifdef CONFIG_MODULES
1642 /* request some modules */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001643 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001644 request_module("saa711x");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001645 if (dev->decoder == EM28XX_TVP5150)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001646 request_module("tvp5150");
1647 if (dev->has_tuner)
1648 request_module("tuner");
1649 if (dev->tda9887_conf)
1650 request_module("tda9887");
1651#endif
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001652 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001653 if (errCode) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001654 em28xx_errdev("error configuring device\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001655 kfree(dev);
1656 return -ENOMEM;
1657 }
1658
1659 down(&dev->lock);
1660 /* register i2c bus */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001661 em28xx_i2c_register(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001662
1663 /* Do board specific init and eeprom reading */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001664 em28xx_card_setup(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001665
1666 /* configure the device */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001667 em28xx_config_i2c(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001668
1669 up(&dev->lock);
1670
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001671 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001672
1673#ifdef CONFIG_MODULES
1674 if (dev->has_msp34xx)
1675 request_module("msp3400");
1676#endif
1677 /* allocate and fill v4l2 device struct */
1678 dev->vdev = video_device_alloc();
1679 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001680 em28xx_errdev("cannot allocate video_device.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001681 kfree(dev);
1682 return -ENOMEM;
1683 }
1684
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001685 dev->vdev->type = VID_TYPE_CAPTURE;
1686 if (dev->has_tuner)
1687 dev->vdev->type |= VID_TYPE_TUNER;
1688 dev->vdev->hardware = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001689 dev->vdev->fops = &em28xx_v4l_fops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001690 dev->vdev->minor = -1;
1691 dev->vdev->dev = &dev->udev->dev;
1692 dev->vdev->release = video_device_release;
1693 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001694 "em28xx video");
1695 list_add_tail(&dev->devlist,&em28xx_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001696
1697 /* register v4l2 device */
1698 down(&dev->lock);
1699 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001700 em28xx_errdev("unable to register video device (error=%i).\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001701 retval);
1702 up(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001703 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001704 video_device_release(dev->vdev);
1705 kfree(dev);
1706 return -ENODEV;
1707 }
1708 if (dev->has_msp34xx) {
1709 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001710 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001711 udelay(2500);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001712 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713 udelay(2500);
1714
1715 }
1716 video_mux(dev, 0);
1717
1718 up(&dev->lock);
1719
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001720 em28xx_info("V4L2 device registered as /dev/video%d\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001721 dev->vdev->minor);
1722
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001723 return 0;
1724}
1725
1726/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001727 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728 * checks for supported devices
1729 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001730static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001731 const struct usb_device_id *id)
1732{
1733 const struct usb_endpoint_descriptor *endpoint;
1734 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001735 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001736 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737 int retval = -ENODEV;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001738 int model,i,nr,ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001739
1740 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001741 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1742
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001743
1744 /* Don't register audio interfaces */
1745 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001746 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001747 udev->descriptor.idVendor,udev->descriptor.idProduct,
1748 ifnum,
1749 interface->altsetting[0].desc.bInterfaceClass);
1750 return -ENODEV;
1751 }
1752
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001753 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001754 udev->descriptor.idVendor,udev->descriptor.idProduct,
1755 ifnum,
1756 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001757
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001758 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1759
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760 /* check if the the device has the iso in endpoint at the correct place */
1761 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1762 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001763 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001764 return -ENODEV;
1765 }
1766 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001767 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001768 return -ENODEV;
1769 }
1770
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001771 model=id->driver_info;
1772 nr=interface->minor;
1773
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001774 if (nr>EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001775 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001776 return -ENOMEM;
1777 }
1778
1779 /* allocate memory for our device state and initialize it */
1780 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1781 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001782 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001783 return -ENOMEM;
1784 }
1785 memset(dev, 0, sizeof(*dev));
1786
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001787 /* compute alternate max packet sizes */
1788 uif = udev->actconfig->interface[0];
1789
1790 dev->num_alt=uif->num_altsetting;
1791 printk(DRIVER_NAME ": Alternate settings: %i\n",dev->num_alt);
1792// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1793 dev->alt_max_pkt_size = kmalloc(32*
1794 dev->num_alt,GFP_KERNEL);
1795 if (dev->alt_max_pkt_size == NULL) {
1796 em28xx_err(DRIVER_NAME ": out of memory!\n");
1797 return -ENOMEM;
1798 }
1799
1800 for (i = 0; i < dev->num_alt ; i++) {
1801 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1802 wMaxPacketSize);
1803 dev->alt_max_pkt_size[i] =
1804 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1805 printk(DRIVER_NAME ": Alternate setting %i, max size= %i\n",i,
1806 dev->alt_max_pkt_size[i]);
1807 }
1808
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001809 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001810
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001811 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001812 model=card[nr];
1813
1814 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1815 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1816 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1817 "%s: workaround that. Redirect complaints to the vendor of\n"
1818 "%s: the TV card. Best regards,\n"
1819 "%s: -- tux\n",
1820 dev->name,dev->name,dev->name,dev->name,dev->name);
1821 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1822 dev->name);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001823 for (i = 0; i < em28xx_bcount; i++) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001824 printk("%s: card=%d -> %s\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001825 dev->name, i, em28xx_boards[i].name);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001826 }
1827 }
1828
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001829 /* allocate device struct */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001830 retval = em28xx_init_dev(&dev, udev, nr, model);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001831 if (retval)
1832 return retval;
1833
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001834 em28xx_info("Found %s\n", em28xx_boards[model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001835
1836 /* save our data pointer in this interface device */
1837 usb_set_intfdata(interface, dev);
1838 return 0;
1839}
1840
1841/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001842 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001843 * called when the device gets diconencted
1844 * video device will be unregistered on v4l2_close in case it is still open
1845 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001846static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001848 struct em28xx *dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001849 usb_set_intfdata(interface, NULL);
1850
1851 if (!dev)
1852 return;
1853
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001854 down_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001855
1856 down(&dev->lock);
1857
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001858 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001859
1860 wake_up_interruptible_all(&dev->open);
1861
1862 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001863 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001864 ("device /dev/video%d is open! Deregistration and memory "
1865 "deallocation are deferred on close.\n", dev->vdev->minor);
1866 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001867 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001868 dev->state |= DEV_DISCONNECTED;
1869 wake_up_interruptible(&dev->wait_frame);
1870 wake_up_interruptible(&dev->wait_stream);
1871 } else {
1872 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001873 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001874 }
1875
1876 up(&dev->lock);
1877
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001878 if (!dev->users) {
1879 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001880 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001881 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001882
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001883 up_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001884}
1885
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001886static struct usb_driver em28xx_usb_driver = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001887 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001888 .name = "em28xx",
1889 .probe = em28xx_usb_probe,
1890 .disconnect = em28xx_usb_disconnect,
1891 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001892};
1893
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001894static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001895{
1896 int result;
1897
1898 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001899 (EM28XX_VERSION_CODE >> 16) & 0xff,
1900 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001901#ifdef SNAPSHOT
1902 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1903 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1904#endif
1905
1906 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001907 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001908 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001909 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001910 " usb_register failed. Error number %d.\n", result);
1911
1912 return result;
1913}
1914
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001915static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001916{
1917 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001918 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001919}
1920
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001921module_init(em28xx_module_init);
1922module_exit(em28xx_module_exit);