blob: 18b8568c634ca9223c97cd077837e7b133e13995 [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>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080036#include <linux/video_decoder.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030041#include <media/msp3400.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080043#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030045 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_NAME "em28xx"
49#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080050#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080052#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080053 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020055 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
57MODULE_AUTHOR(DRIVER_AUTHOR);
58MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_LICENSE("GPL");
60
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080061static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080062
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080063static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020064static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080066module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020067module_param_array(video_nr, int, NULL, 0444);
68module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080069MODULE_PARM_DESC(card,"card type");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020070MODULE_PARM_DESC(video_nr,"video device numbers");
71MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073static unsigned int video_debug = 0;
74module_param(video_debug,int,0644);
75MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
76
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
78static unsigned long em28xx_devused;
79
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080080/* supported tv norms */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080081static struct em28xx_tvnorm tvnorms[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080082 {
83 .name = "PAL",
84 .id = V4L2_STD_PAL,
85 .mode = VIDEO_MODE_PAL,
86 }, {
87 .name = "NTSC",
88 .id = V4L2_STD_NTSC,
89 .mode = VIDEO_MODE_NTSC,
90 }, {
91 .name = "SECAM",
92 .id = V4L2_STD_SECAM,
93 .mode = VIDEO_MODE_SECAM,
94 }, {
95 .name = "PAL-M",
96 .id = V4L2_STD_PAL_M,
97 .mode = VIDEO_MODE_PAL,
98 }
99};
100
101#define TVNORMS ARRAY_SIZE(tvnorms)
102
103/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200104/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800105static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800106 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200107 .id = V4L2_CID_AUDIO_VOLUME,
108 .type = V4L2_CTRL_TYPE_INTEGER,
109 .name = "Volume",
110 .minimum = 0x0,
111 .maximum = 0x1f,
112 .step = 0x1,
113 .default_value = 0x1f,
114 .flags = 0,
115 },{
116 .id = V4L2_CID_AUDIO_MUTE,
117 .type = V4L2_CTRL_TYPE_BOOLEAN,
118 .name = "Mute",
119 .minimum = 0,
120 .maximum = 1,
121 .step = 1,
122 .default_value = 1,
123 .flags = 0,
124 }
125};
126
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800127static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200129static DEFINE_MUTEX(em28xx_sysfs_lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800130static DECLARE_RWSEM(em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800131
132/********************* v4l2 interface ******************************************/
133
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800134/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800135 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800136 * inits registers with sane defaults
137 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800138static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800139{
140
141 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300142 if (!dev->is_em2800)
143 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800144
145 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200146
Markus Rechberger9475fb12006-02-27 00:07:34 -0300147/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
148/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200149 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
150
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800151 em28xx_audio_usb_mute(dev, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800152 dev->mute = 1; /* maybe not the right place... */
153 dev->volume = 0x1f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800154 em28xx_audio_analog_set(dev);
155 em28xx_audio_analog_setup(dev);
156 em28xx_outfmt_set_yuv422(dev);
157 em28xx_colorlevels_set_default(dev);
158 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800159
160 return 0;
161}
162
163/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800164 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800165 * configure i2c attached devices
166 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800167static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800168{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300169 struct v4l2_routing route;
170
171 route.input = INPUT(dev->ctl_input)->vmux;
172 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100173 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300174 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300175 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800176}
177
178/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800179 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800180 * prepare queues for incoming and outgoing frames
181 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800182static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800183{
184 u32 i;
185
186 INIT_LIST_HEAD(&dev->inqueue);
187 INIT_LIST_HEAD(&dev->outqueue);
188
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800189 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800190 dev->frame[i].state = F_UNUSED;
191 dev->frame[i].buf.bytesused = 0;
192 }
193}
194
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800195static void video_mux(struct em28xx *dev, int index)
196{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300197 int ainput;
198 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800199
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300200 route.input = INPUT(index)->vmux;
201 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800202 dev->ctl_input = index;
203 dev->ctl_ainput = INPUT(index)->amux;
204
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300205 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800206
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300207 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,route.input,dev->ctl_ainput);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800208
209 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200210 if (dev->i2s_speed)
211 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300212 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300213 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300214 /* Note: this is msp3400 specific */
215 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800216 ainput = EM28XX_AUDIO_SRC_TUNER;
217 em28xx_audio_source(dev, ainput);
218 } else {
219 switch (dev->ctl_ainput) {
Markus Rechberger9475fb12006-02-27 00:07:34 -0300220 case 0:
221 ainput = EM28XX_AUDIO_SRC_TUNER;
222 break;
223 default:
224 ainput = EM28XX_AUDIO_SRC_LINE;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800225 }
226 em28xx_audio_source(dev, ainput);
227 }
228}
229
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800230/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800231 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800232 * inits the device and starts isoc transfer
233 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800234static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800235{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800236 int minor = iminor(inode);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800237 int errCode = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800238 struct em28xx *h,*dev = NULL;
Markus Rechberger9c755412005-11-08 21:37:52 -0800239
Trent Piephoa991f442007-10-10 05:37:43 -0300240 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -0800241 if (h->vdev->minor == minor) {
242 dev = h;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200243 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
244 }
245 if (h->vbi_dev->minor == minor) {
246 dev = h;
247 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -0800248 }
249 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200250 if (NULL == dev)
251 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -0800252
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200253 em28xx_videodbg("open minor=%d type=%s users=%d\n",
254 minor,v4l2_type_names[dev->type],dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800255
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800256 if (!down_read_trylock(&em28xx_disconnect))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800257 return -ERESTARTSYS;
258
259 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800260 em28xx_warn("this driver can be opened only once\n");
261 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800262 return -EBUSY;
263 }
264
Ingo Molnar3593cab2006-02-07 06:49:14 -0200265 mutex_init(&dev->fileop_lock); /* to 1 == available */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800266 spin_lock_init(&dev->queue_lock);
267 init_waitqueue_head(&dev->wait_frame);
268 init_waitqueue_head(&dev->wait_stream);
269
Ingo Molnar3593cab2006-02-07 06:49:14 -0200270 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800271
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200272 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
273 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800274
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200275 dev->width = norm_maxw(dev);
276 dev->height = norm_maxh(dev);
277 dev->frame_size = dev->width * dev->height * 2;
278 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
279 dev->bytesperline = dev->width * 2;
280 dev->hscale = 0;
281 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800282
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200283 em28xx_capture_start(dev, 1);
284 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800285
Markus Rechbergere5d4a562006-02-07 06:49:13 -0200286 /* device needs to be initialized before isoc transfer */
287 video_mux(dev, 0);
288
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200289 /* start the transfer */
290 errCode = em28xx_init_isoc(dev);
291 if (errCode)
292 goto err;
293
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200294 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800295
296 dev->users++;
297 filp->private_data = dev;
298 dev->io = IO_NONE;
299 dev->stream = STREAM_OFF;
300 dev->num_frames = 0;
301
302 /* prepare queues */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800303 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800304
305 dev->state |= DEV_INITIALIZED;
306
Ingo Molnar3593cab2006-02-07 06:49:14 -0200307err:
308 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800309 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800310 return errCode;
311}
312
313/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800314 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800315 * unregisters the v4l2,i2c and usb devices
316 * called when the device gets disconected or at module unload
317*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800318static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800319{
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200320 mutex_lock(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800321
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200322 /*FIXME: I2C IR should be disconnected */
323
324 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
325 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
326 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -0800327 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800328 video_unregister_device(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200329 video_unregister_device(dev->vbi_dev);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800330 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800331 usb_put_dev(dev->udev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200332 mutex_unlock(&em28xx_sysfs_lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200333
334
335 /* Mark device as unused */
336 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800337}
338
339/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800340 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800341 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
342 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800343static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800344{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800345 int errCode;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800346 struct em28xx *dev=filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800347
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800348 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800349
Ingo Molnar3593cab2006-02-07 06:49:14 -0200350 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800351
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800352 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800353
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800354 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800355
356 /* the device is already disconnect, free the remaining resources */
357 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800358 em28xx_release_resources(dev);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200359 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800360 kfree(dev);
361 return 0;
362 }
363
364 /* set alternate 0 */
365 dev->alt = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800366 em28xx_videodbg("setting alternate 0\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800367 errCode = usb_set_interface(dev->udev, 0, 0);
368 if (errCode < 0) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800369 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800370 errCode);
371 }
372
373 dev->users--;
374 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200375 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800376 return 0;
377}
378
379/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800380 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800381 * will allocate buffers when called for the first time
382 */
383static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800384em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800385 loff_t * f_pos)
386{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800387 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800388 unsigned long lock_flags;
389 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800390 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800391
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200392 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
393 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
394 }
395 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
396 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
397 em28xx_videodbg("not supported yet! ...\n");
398 if (copy_to_user(buf, "", 1)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200399 mutex_unlock(&dev->fileop_lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200400 return -EFAULT;
401 }
402 return (1);
403 }
404 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
405 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
406 em28xx_videodbg("not supported yet! ...\n");
407 if (copy_to_user(buf, "", 1)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200408 mutex_unlock(&dev->fileop_lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200409 return -EFAULT;
410 }
411 return (1);
412 }
413
Ingo Molnar3593cab2006-02-07 06:49:14 -0200414 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800415 return -ERESTARTSYS;
416
417 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800418 em28xx_videodbg("device not present\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200419 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800420 return -ENODEV;
421 }
422
423 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800424 em28xx_videodbg("device misconfigured; close and open it again\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200425 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800426 return -EIO;
427 }
428
429 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800430 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800431 " the device again to choose the read method\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200432 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800433 return -EINVAL;
434 }
435
436 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800437 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
438 em28xx_errdev("read failed, not enough memory\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200439 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800440 return -ENOMEM;
441 }
442 dev->io = IO_READ;
443 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800444 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800445 }
446
447 if (!count) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200448 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800449 return 0;
450 }
451
452 if (list_empty(&dev->outqueue)) {
453 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200454 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800455 return -EAGAIN;
456 }
457 ret = wait_event_interruptible
458 (dev->wait_frame,
459 (!list_empty(&dev->outqueue)) ||
460 (dev->state & DEV_DISCONNECTED));
461 if (ret) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200462 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800463 return ret;
464 }
465 if (dev->state & DEV_DISCONNECTED) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200466 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800467 return -ENODEV;
468 }
469 }
470
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800471 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800472
473 spin_lock_irqsave(&dev->queue_lock, lock_flags);
474 list_for_each_entry(i, &dev->outqueue, frame)
475 i->state = F_UNUSED;
476 INIT_LIST_HEAD(&dev->outqueue);
477 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
478
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800479 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800480
481 if (count > f->buf.length)
482 count = f->buf.length;
483
484 if (copy_to_user(buf, f->bufmem, count)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200485 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800486 return -EFAULT;
487 }
488 *f_pos += count;
489
Ingo Molnar3593cab2006-02-07 06:49:14 -0200490 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800491
492 return count;
493}
494
495/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800496 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800497 * will allocate buffers when called for the first time
498 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800499static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800500{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800501 unsigned int mask = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800502 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800503
Ingo Molnar3593cab2006-02-07 06:49:14 -0200504 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800505 return POLLERR;
506
507 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800508 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800509 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800510 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511 } else {
512 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800513 if (!em28xx_request_buffers
514 (dev, EM28XX_NUM_READ_FRAMES)) {
515 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800516 ("poll() failed, not enough memory\n");
517 } else {
518 dev->io = IO_READ;
519 dev->stream = STREAM_ON;
520 }
521 }
522
523 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800524 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800525 poll_wait(filp, &dev->wait_frame, wait);
526
527 if (!list_empty(&dev->outqueue))
528 mask |= POLLIN | POLLRDNORM;
529
Ingo Molnar3593cab2006-02-07 06:49:14 -0200530 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800531
532 return mask;
533 }
534 }
535
Ingo Molnar3593cab2006-02-07 06:49:14 -0200536 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800537 return POLLERR;
538}
539
540/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800541 * em28xx_vm_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800542 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800543static void em28xx_vm_open(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800544{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800545 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800546 f->vma_use_count++;
547}
548
549/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800550 * em28xx_vm_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800551 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800552static void em28xx_vm_close(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800553{
554 /* NOTE: buffers are not freed here */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800555 struct em28xx_frame_t *f = vma->vm_private_data;
Mauro Carvalho Chehab63337dd2007-11-10 10:26:20 -0300556
557 if (f->vma_use_count)
558 f->vma_use_count--;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800559}
560
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800561static struct vm_operations_struct em28xx_vm_ops = {
562 .open = em28xx_vm_open,
563 .close = em28xx_vm_close,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800564};
565
566/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800567 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800568 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800569static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800570{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800571 unsigned long size = vma->vm_end - vma->vm_start,
Sascha Sommer3639c862005-12-12 00:37:30 -0800572 start = vma->vm_start;
573 void *pos;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800574 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -0800575
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800576 struct em28xx *dev = filp->private_data;
Markus Rechberger9c755412005-11-08 21:37:52 -0800577
Ingo Molnar3593cab2006-02-07 06:49:14 -0200578 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800579 return -ERESTARTSYS;
580
581 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800582 em28xx_videodbg("mmap: device not present\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200583 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800584 return -ENODEV;
585 }
586
587 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800588 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800589 "open it again\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200590 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800591 return -EIO;
592 }
593
594 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
595 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200596 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800597 return -EINVAL;
598 }
599
600 for (i = 0; i < dev->num_frames; i++) {
601 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
602 break;
603 }
604 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800605 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200606 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800607 return -EINVAL;
608 }
609
610 /* VM_IO is eventually going to replace PageReserved altogether */
611 vma->vm_flags |= VM_IO;
612 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
613
Sascha Sommer3639c862005-12-12 00:37:30 -0800614 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800615 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -0800616 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
617 em28xx_videodbg("mmap: vm_insert_page failed\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200618 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800619 return -EAGAIN;
620 }
621 start += PAGE_SIZE;
622 pos += PAGE_SIZE;
623 size -= PAGE_SIZE;
624 }
625
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800626 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800627 vma->vm_private_data = &dev->frame[i];
628
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800629 em28xx_vm_open(vma);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200630 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800631 return 0;
632}
633
634/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800635 * em28xx_get_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800636 * return the current saturation, brightness or contrast, mute state
637 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800638static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800639{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800640 switch (ctrl->id) {
641 case V4L2_CID_AUDIO_MUTE:
642 ctrl->value = dev->mute;
643 return 0;
644 case V4L2_CID_AUDIO_VOLUME:
645 ctrl->value = dev->volume;
646 return 0;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200647 default:
648 return -EINVAL;
649 }
650}
651
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800652/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800653 * em28xx_set_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800654 * mute or set new saturation, brightness or contrast
655 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800656static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800657{
658 switch (ctrl->id) {
659 case V4L2_CID_AUDIO_MUTE:
660 if (ctrl->value != dev->mute) {
661 dev->mute = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800662 em28xx_audio_usb_mute(dev, ctrl->value);
663 return em28xx_audio_analog_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800664 }
665 return 0;
666 case V4L2_CID_AUDIO_VOLUME:
667 dev->volume = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800668 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200669 default:
670 return -EINVAL;
671 }
672}
673
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800674/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800675 * em28xx_stream_interrupt()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800676 * stops streaming
677 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800678static int em28xx_stream_interrupt(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800679{
680 int ret = 0;
681
682 /* stop reading from the device */
683
684 dev->stream = STREAM_INTERRUPT;
685 ret = wait_event_timeout(dev->wait_stream,
686 (dev->stream == STREAM_OFF) ||
687 (dev->state & DEV_DISCONNECTED),
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800688 EM28XX_URB_TIMEOUT);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800689 if (dev->state & DEV_DISCONNECTED)
690 return -ENODEV;
691 else if (ret) {
692 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800693 em28xx_videodbg("device is misconfigured; close and "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200694 "open /dev/video%d again\n",
695 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800696 return ret;
697 }
698
699 return 0;
700}
701
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800702static int em28xx_set_norm(struct em28xx *dev, int width, int height)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800703{
704 unsigned int hscale, vscale;
705 unsigned int maxh, maxw;
706
707 maxw = norm_maxw(dev);
708 maxh = norm_maxh(dev);
709
710 /* width must even because of the YUYV format */
711 /* height must be even because of interlacing */
712 height &= 0xfffe;
713 width &= 0xfffe;
714
715 if (height < 32)
716 height = 32;
717 if (height > maxh)
718 height = maxh;
719 if (width < 48)
720 width = 48;
721 if (width > maxw)
722 width = maxw;
723
724 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
725 hscale = 0x3fff;
726 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
727
728 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
729 vscale = 0x3fff;
730 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
731
732 /* set new image size */
733 dev->width = width;
734 dev->height = height;
735 dev->frame_size = dev->width * dev->height * 2;
736 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
737 dev->bytesperline = dev->width * 2;
738 dev->hscale = hscale;
739 dev->vscale = vscale;
740
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800741 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800742
743 return 0;
744}
745
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200746static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
747{
748 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
749 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
750 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
751 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
752 "V4L2_BUF_TYPE_VBI_CAPTURE" :
753 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
754 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
755 "not supported");
756
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200757 switch (format->type) {
758 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
759 {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200760 format->fmt.pix.width = dev->width;
761 format->fmt.pix.height = dev->height;
762 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
763 format->fmt.pix.bytesperline = dev->bytesperline;
764 format->fmt.pix.sizeimage = dev->frame_size;
765 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
766 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
767
768 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
769 dev->height);
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200770 break;
771 }
772
773 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
774 {
775 format->fmt.sliced.service_set=0;
776
777 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
778
779 if (format->fmt.sliced.service_set==0)
780 return -EINVAL;
781
782 break;
783 }
784
785 default:
786 return -EINVAL;
787 }
788 return (0);
789}
790
791static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
792{
793 u32 i;
794 int ret = 0;
795 int width = format->fmt.pix.width;
796 int height = format->fmt.pix.height;
797 unsigned int hscale, vscale;
798 unsigned int maxh, maxw;
799
800 maxw = norm_maxw(dev);
801 maxh = norm_maxh(dev);
802
803 em28xx_videodbg("%s: type=%s\n",
804 cmd == VIDIOC_TRY_FMT ?
805 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
806 format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
807 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
808 format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
809 "V4L2_BUF_TYPE_VBI_CAPTURE " :
810 "not supported");
811
812 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
813 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
814
815 if (format->fmt.sliced.service_set==0)
816 return -EINVAL;
817
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200818 return 0;
819 }
820
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200821
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200822 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
823 return -EINVAL;
824
825 em28xx_videodbg("%s: requested %dx%d\n",
826 cmd == VIDIOC_TRY_FMT ?
827 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
828 format->fmt.pix.width, format->fmt.pix.height);
829
830 /* FIXME: Move some code away from here */
831 /* width must even because of the YUYV format */
832 /* height must be even because of interlacing */
833 height &= 0xfffe;
834 width &= 0xfffe;
835
836 if (height < 32)
837 height = 32;
838 if (height > maxh)
839 height = maxh;
840 if (width < 48)
841 width = 48;
842 if (width > maxw)
843 width = maxw;
844
845 if(dev->is_em2800){
846 /* the em2800 can only scale down to 50% */
847 if(height % (maxh / 2))
848 height=maxh;
849 if(width % (maxw / 2))
850 width=maxw;
851 /* according to empiatech support */
852 /* the MaxPacketSize is to small to support */
853 /* framesizes larger than 640x480 @ 30 fps */
854 /* or 640x576 @ 25 fps. As this would cut */
855 /* of a part of the image we prefer */
856 /* 360x576 or 360x480 for now */
857 if(width == maxw && height == maxh)
858 width /= 2;
859 }
860
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200861 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200862 hscale = 0x3fff;
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200863
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200864 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
865
866 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200867 vscale = 0x3fff;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200868
869 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200870
871 format->fmt.pix.width = width;
872 format->fmt.pix.height = height;
873 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
874 format->fmt.pix.bytesperline = width * 2;
875 format->fmt.pix.sizeimage = width * 2 * height;
876 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
877 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
878
879 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200880 cmd == VIDIOC_TRY_FMT ?
881 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
882 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200883
884 if (cmd == VIDIOC_TRY_FMT)
885 return 0;
886
887 for (i = 0; i < dev->num_frames; i++)
888 if (dev->frame[i].vma_use_count) {
889 em28xx_videodbg("VIDIOC_S_FMT failed. "
890 "Unmap the buffers first.\n");
891 return -EINVAL;
892 }
893
894 /* stop io in case it is already in progress */
895 if (dev->stream == STREAM_ON) {
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200896 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200897 if ((ret = em28xx_stream_interrupt(dev)))
898 return ret;
899 }
900
901 em28xx_release_buffers(dev);
902 dev->io = IO_NONE;
903
904 /* set new image size */
905 dev->width = width;
906 dev->height = height;
907 dev->frame_size = dev->width * dev->height * 2;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200908 dev->field_size = dev->frame_size >> 1;
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200909 dev->bytesperline = dev->width * 2;
910 dev->hscale = hscale;
911 dev->vscale = vscale;
912 em28xx_uninit_isoc(dev);
913 em28xx_set_alternate(dev);
914 em28xx_capture_start(dev, 1);
915 em28xx_resolution_set(dev);
916 em28xx_init_isoc(dev);
917
918 return 0;
919}
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200920
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800921/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800922 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800923 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800924 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800925 * copying is done already, arg is a kernel pointer.
926 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800927static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
928 struct em28xx *dev, unsigned int cmd, void *arg,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800929 v4l2_kioctl driver_ioctl)
930{
931 int ret;
932
933 switch (cmd) {
934 /* ---------- tv norms ---------- */
935 case VIDIOC_ENUMSTD:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200936 {
937 struct v4l2_standard *e = arg;
938 unsigned int i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800939
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200940 i = e->index;
941 if (i >= TVNORMS)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800942 return -EINVAL;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200943 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
944 tvnorms[e->index].name);
945 e->index = i;
946 if (ret < 0)
947 return ret;
948 return 0;
949 }
950 case VIDIOC_G_STD:
951 {
952 v4l2_std_id *id = arg;
953
954 *id = dev->tvnorm->id;
955 return 0;
956 }
957 case VIDIOC_S_STD:
958 {
959 v4l2_std_id *id = arg;
960 unsigned int i;
961
962 for (i = 0; i < TVNORMS; i++)
963 if (*id == tvnorms[i].id)
964 break;
965 if (i == TVNORMS)
966 for (i = 0; i < TVNORMS; i++)
967 if (*id & tvnorms[i].id)
968 break;
969 if (i == TVNORMS)
970 return -EINVAL;
971
Ingo Molnar3593cab2006-02-07 06:49:14 -0200972 mutex_lock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200973 dev->tvnorm = &tvnorms[i];
974
975 em28xx_set_norm(dev, dev->width, dev->height);
976
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200977 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
978 &dev->tvnorm->id);
979
Ingo Molnar3593cab2006-02-07 06:49:14 -0200980 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200981
982 return 0;
983 }
984
985 /* ------ input switching ---------- */
986 case VIDIOC_ENUMINPUT:
987 {
988 struct v4l2_input *i = arg;
989 unsigned int n;
990 static const char *iname[] = {
991 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
992 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
993 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
994 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
995 [EM28XX_VMUX_SVIDEO] = "S-Video",
996 [EM28XX_VMUX_TELEVISION] = "Television",
997 [EM28XX_VMUX_CABLE] = "Cable TV",
998 [EM28XX_VMUX_DVB] = "DVB",
999 [EM28XX_VMUX_DEBUG] = "for debug only",
1000 };
1001
1002 n = i->index;
1003 if (n >= MAX_EM28XX_INPUT)
1004 return -EINVAL;
1005 if (0 == INPUT(n)->type)
1006 return -EINVAL;
1007 memset(i, 0, sizeof(*i));
1008 i->index = n;
1009 i->type = V4L2_INPUT_TYPE_CAMERA;
1010 strcpy(i->name, iname[INPUT(n)->type]);
1011 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1012 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1013 i->type = V4L2_INPUT_TYPE_TUNER;
1014 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1015 i->std |= tvnorms[n].id;
1016 return 0;
1017 }
1018 case VIDIOC_G_INPUT:
1019 {
1020 int *i = arg;
1021 *i = dev->ctl_input;
1022
1023 return 0;
1024 }
1025 case VIDIOC_S_INPUT:
1026 {
1027 int *index = arg;
1028
1029 if (*index >= MAX_EM28XX_INPUT)
1030 return -EINVAL;
1031 if (0 == INPUT(*index)->type)
1032 return -EINVAL;
1033
Ingo Molnar3593cab2006-02-07 06:49:14 -02001034 mutex_lock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001035 video_mux(dev, *index);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001036 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001037
1038 return 0;
1039 }
1040 case VIDIOC_G_AUDIO:
1041 {
1042 struct v4l2_audio *a = arg;
1043 unsigned int index = a->index;
1044
1045 if (a->index > 1)
1046 return -EINVAL;
1047 memset(a, 0, sizeof(*a));
1048 index = dev->ctl_ainput;
1049
1050 if (index == 0) {
1051 strcpy(a->name, "Television");
1052 } else {
1053 strcpy(a->name, "Line In");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001054 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001055 a->capability = V4L2_AUDCAP_STEREO;
1056 a->index = index;
1057 return 0;
1058 }
1059 case VIDIOC_S_AUDIO:
1060 {
1061 struct v4l2_audio *a = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001062
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001063 if (a->index != dev->ctl_ainput)
1064 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001065
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001066 return 0;
1067 }
1068
1069 /* --- controls ---------------------------------------------- */
1070 case VIDIOC_QUERYCTRL:
1071 {
1072 struct v4l2_queryctrl *qc = arg;
1073 int i, id=qc->id;
1074
1075 memset(qc,0,sizeof(*qc));
1076 qc->id=id;
1077
1078 if (!dev->has_msp34xx) {
1079 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1080 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1081 memcpy(qc, &(em28xx_qctrl[i]),
1082 sizeof(*qc));
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001083 return 0;
1084 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001085 }
1086 }
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001087 em28xx_i2c_call_clients(dev,cmd,qc);
1088 if (qc->type)
1089 return 0;
1090 else
1091 return -EINVAL;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001092 }
1093 case VIDIOC_G_CTRL:
1094 {
1095 struct v4l2_control *ctrl = arg;
1096 int retval=-EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001097
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001098 if (!dev->has_msp34xx)
1099 retval=em28xx_get_ctrl(dev, ctrl);
1100 if (retval==-EINVAL) {
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001101 em28xx_i2c_call_clients(dev,cmd,arg);
1102 return 0;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001103 } else return retval;
1104 }
1105 case VIDIOC_S_CTRL:
1106 {
1107 struct v4l2_control *ctrl = arg;
1108 u8 i;
1109
1110 if (!dev->has_msp34xx){
1111 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1112 if (ctrl->id == em28xx_qctrl[i].id) {
1113 if (ctrl->value <
1114 em28xx_qctrl[i].minimum
1115 || ctrl->value >
1116 em28xx_qctrl[i].maximum)
1117 return -ERANGE;
1118 return em28xx_set_ctrl(dev, ctrl);
1119 }
1120 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001121 }
1122
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001123 em28xx_i2c_call_clients(dev,cmd,arg);
1124 return 0;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001125 }
1126 /* --- tuner ioctls ------------------------------------------ */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001127 case VIDIOC_G_TUNER:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001128 {
1129 struct v4l2_tuner *t = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001130
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001131 if (0 != t->index)
1132 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001133
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001134 memset(t, 0, sizeof(*t));
1135 strcpy(t->name, "Tuner");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001136 mutex_lock(&dev->lock);
Hans Verkuilab4cecf2006-04-01 16:40:21 -03001137 /* let clients fill in the remainder of this struct */
1138 em28xx_i2c_call_clients(dev, cmd, t);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001139 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001140 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1141 t->afc);
1142 return 0;
1143 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001144 case VIDIOC_S_TUNER:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001145 {
1146 struct v4l2_tuner *t = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001147
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001148 if (0 != t->index)
1149 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001150 mutex_lock(&dev->lock);
Hans Verkuilab4cecf2006-04-01 16:40:21 -03001151 /* let clients handle this */
1152 em28xx_i2c_call_clients(dev, cmd, t);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001153 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001154 return 0;
1155 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001156 case VIDIOC_G_FREQUENCY:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001157 {
1158 struct v4l2_frequency *f = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001159
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001160 memset(f, 0, sizeof(*f));
1161 f->type = V4L2_TUNER_ANALOG_TV;
1162 f->frequency = dev->ctl_freq;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001163
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001164 return 0;
1165 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001166 case VIDIOC_S_FREQUENCY:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001167 {
1168 struct v4l2_frequency *f = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001169
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001170 if (0 != f->tuner)
1171 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001172
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001173 if (V4L2_TUNER_ANALOG_TV != f->type)
1174 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001175
Ingo Molnar3593cab2006-02-07 06:49:14 -02001176 mutex_lock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001177 dev->ctl_freq = f->frequency;
1178 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001179 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001180 return 0;
1181 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001182 case VIDIOC_CROPCAP:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001183 {
1184 struct v4l2_cropcap *cc = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001185
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001186 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1187 return -EINVAL;
1188 cc->bounds.left = 0;
1189 cc->bounds.top = 0;
1190 cc->bounds.width = dev->width;
1191 cc->bounds.height = dev->height;
1192 cc->defrect = cc->bounds;
1193 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1194 cc->pixelaspect.denominator = 59;
1195 return 0;
1196 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001197 case VIDIOC_STREAMON:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001198 {
1199 int *type = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001200
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001201 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1202 || dev->io != IO_MMAP)
1203 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001204
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001205 if (list_empty(&dev->inqueue))
1206 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001207
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001208 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001209
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001210 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001211
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001212 return 0;
1213 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001214 case VIDIOC_STREAMOFF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001215 {
1216 int *type = arg;
1217 int ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001218
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001219 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1220 || dev->io != IO_MMAP)
1221 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001222
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001223 if (dev->stream == STREAM_ON) {
1224 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1225 if ((ret = em28xx_stream_interrupt(dev)))
1226 return ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001227 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001228 em28xx_empty_framequeues(dev);
1229
1230 return 0;
1231 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001232 default:
1233 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1234 driver_ioctl);
1235 }
1236 return 0;
1237}
1238
1239/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001240 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001241 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001242 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001243 * copying is done already, arg is a kernel pointer.
1244 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001245static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001246 unsigned int cmd, void *arg)
1247{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001248 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001249
1250 if (!dev)
1251 return -ENODEV;
1252
1253 if (video_debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -02001254 v4l_print_ioctl(dev->name,cmd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001255
1256 switch (cmd) {
1257
1258 /* --- capabilities ------------------------------------------ */
1259 case VIDIOC_QUERYCAP:
1260 {
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001261 struct v4l2_capability *cap = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001262
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001263 memset(cap, 0, sizeof(*cap));
1264 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1265 strlcpy(cap->card, em28xx_boards[dev->model].name,
1266 sizeof(cap->card));
1267 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1268 sizeof(cap->bus_info));
1269 cap->version = EM28XX_VERSION_CODE;
1270 cap->capabilities =
1271 V4L2_CAP_SLICED_VBI_CAPTURE |
1272 V4L2_CAP_VIDEO_CAPTURE |
1273 V4L2_CAP_AUDIO |
1274 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1275 if (dev->has_tuner)
1276 cap->capabilities |= V4L2_CAP_TUNER;
1277 return 0;
1278 }
1279 /* --- capture ioctls ---------------------------------------- */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001280 case VIDIOC_ENUM_FMT:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001281 {
1282 struct v4l2_fmtdesc *fmtd = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001283
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001284 if (fmtd->index != 0)
1285 return -EINVAL;
1286 memset(fmtd, 0, sizeof(*fmtd));
1287 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1288 strcpy(fmtd->description, "Packed YUY2");
1289 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1290 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1291 return 0;
1292 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001293 case VIDIOC_G_FMT:
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001294 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001295
1296 case VIDIOC_TRY_FMT:
1297 case VIDIOC_S_FMT:
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -02001298 return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001299
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001300 case VIDIOC_REQBUFS:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001301 {
1302 struct v4l2_requestbuffers *rb = arg;
1303 u32 i;
1304 int ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001305
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001306 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1307 rb->memory != V4L2_MEMORY_MMAP)
1308 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001309
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001310 if (dev->io == IO_READ) {
1311 em28xx_videodbg ("method is set to read;"
1312 " close and open the device again to"
1313 " choose the mmap I/O method\n");
1314 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001315 }
1316
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001317 for (i = 0; i < dev->num_frames; i++)
1318 if (dev->frame[i].vma_use_count) {
1319 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1320 return -EINVAL;
1321 }
1322
1323 if (dev->stream == STREAM_ON) {
1324 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1325 if ((ret = em28xx_stream_interrupt(dev)))
1326 return ret;
1327 }
1328
1329 em28xx_empty_framequeues(dev);
1330
1331 em28xx_release_buffers(dev);
1332 if (rb->count)
1333 rb->count =
1334 em28xx_request_buffers(dev, rb->count);
1335
1336 dev->frame_current = NULL;
1337
1338 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1339 rb->count);
1340 dev->io = rb->count ? IO_MMAP : IO_NONE;
1341 return 0;
1342 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001343 case VIDIOC_QUERYBUF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001344 {
1345 struct v4l2_buffer *b = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001346
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001347 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1348 b->index >= dev->num_frames || dev->io != IO_MMAP)
1349 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001350
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001351 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001352
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001353 if (dev->frame[b->index].vma_use_count) {
1354 b->flags |= V4L2_BUF_FLAG_MAPPED;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001355 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001356 if (dev->frame[b->index].state == F_DONE)
1357 b->flags |= V4L2_BUF_FLAG_DONE;
1358 else if (dev->frame[b->index].state != F_UNUSED)
1359 b->flags |= V4L2_BUF_FLAG_QUEUED;
1360 return 0;
1361 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001362 case VIDIOC_QBUF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001363 {
1364 struct v4l2_buffer *b = arg;
1365 unsigned long lock_flags;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001366
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001367 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1368 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1369 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001370 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001371
1372 if (dev->frame[b->index].state != F_UNUSED) {
1373 return -EAGAIN;
1374 }
1375 dev->frame[b->index].state = F_QUEUED;
1376
1377 /* add frame to fifo */
1378 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1379 list_add_tail(&dev->frame[b->index].frame,
1380 &dev->inqueue);
1381 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1382
1383 return 0;
1384 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001385 case VIDIOC_DQBUF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001386 {
1387 struct v4l2_buffer *b = arg;
1388 struct em28xx_frame_t *f;
1389 unsigned long lock_flags;
1390 int ret = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001391
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001392 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1393 || dev->io != IO_MMAP)
1394 return -EINVAL;
1395
1396 if (list_empty(&dev->outqueue)) {
1397 if (dev->stream == STREAM_OFF)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001398 return -EINVAL;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001399 if (filp->f_flags & O_NONBLOCK)
1400 return -EAGAIN;
1401 ret = wait_event_interruptible
1402 (dev->wait_frame,
1403 (!list_empty(&dev->outqueue)) ||
1404 (dev->state & DEV_DISCONNECTED));
1405 if (ret)
1406 return ret;
1407 if (dev->state & DEV_DISCONNECTED)
1408 return -ENODEV;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001409 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001410
1411 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1412 f = list_entry(dev->outqueue.next,
1413 struct em28xx_frame_t, frame);
1414 list_del(dev->outqueue.next);
1415 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1416
1417 f->state = F_UNUSED;
1418 memcpy(b, &f->buf, sizeof(*b));
1419
1420 if (f->vma_use_count)
1421 b->flags |= V4L2_BUF_FLAG_MAPPED;
1422
1423 return 0;
1424 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001425 default:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001426 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1427 em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001428 }
1429 return 0;
1430}
1431
1432/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001433 * em28xx_v4l2_ioctl()
1434 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001435 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001436static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001437 unsigned int cmd, unsigned long arg)
1438{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001439 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001440 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001441
Ingo Molnar3593cab2006-02-07 06:49:14 -02001442 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001443 return -ERESTARTSYS;
1444
1445 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001446 em28xx_errdev("v4l2 ioctl: device not present\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001447 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001448 return -ENODEV;
1449 }
1450
1451 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001452 em28xx_errdev
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001453 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001454 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001455 return -EIO;
1456 }
1457
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001458 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001459
Ingo Molnar3593cab2006-02-07 06:49:14 -02001460 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001461
1462 return ret;
1463}
1464
Arjan van de Venfa027c22007-02-12 00:55:33 -08001465static const struct file_operations em28xx_v4l_fops = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001466 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001467 .open = em28xx_v4l2_open,
1468 .release = em28xx_v4l2_close,
1469 .ioctl = em28xx_v4l2_ioctl,
1470 .read = em28xx_v4l2_read,
1471 .poll = em28xx_v4l2_poll,
1472 .mmap = em28xx_v4l2_mmap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001473 .llseek = no_llseek,
Mauro Carvalho Chehab17cbe2e2006-01-09 15:24:58 -02001474 .compat_ioctl = v4l_compat_ioctl32,
1475
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001476};
1477
1478/******************************** usb interface *****************************************/
1479
1480/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001481 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001482 * allocates and inits the device structs, registers i2c bus and v4l device
1483 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001484static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001485 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001486{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001487 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001488 int retval = -ENOMEM;
1489 int errCode, i;
1490 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001491
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001492 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001493 mutex_init(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001494 init_waitqueue_head(&dev->open);
1495
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001496 dev->em28xx_write_regs = em28xx_write_regs;
1497 dev->em28xx_read_reg = em28xx_read_reg;
1498 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1499 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1500 dev->em28xx_read_reg_req = em28xx_read_reg_req;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001501
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001502 /* setup video picture settings for saa7113h */
1503 memset(&dev->vpic, 0, sizeof(dev->vpic));
1504 dev->vpic.colour = 128 << 8;
1505 dev->vpic.hue = 128 << 8;
1506 dev->vpic.brightness = 128 << 8;
1507 dev->vpic.contrast = 192 << 8;
1508 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1509 dev->vpic.depth = 16;
1510 dev->vpic.palette = VIDEO_PALETTE_YUV422;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001511
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001512 em28xx_pre_card_setup(dev);
1513
1514 errCode = em28xx_config(dev);
1515 if (errCode) {
1516 em28xx_errdev("error configuring device\n");
1517 em28xx_devused &= ~(1<<dev->devno);
1518 kfree(dev);
1519 return -ENOMEM;
1520 }
1521
1522 mutex_lock(&dev->lock);
1523
1524 /* register i2c bus */
1525 em28xx_i2c_register(dev);
1526
1527 /* Do board specific init and eeprom reading */
1528 em28xx_card_setup(dev);
1529
1530 /* configure the device */
1531 em28xx_config_i2c(dev);
1532
1533 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001534
1535 for (i = 0; i < TVNORMS; i++)
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001536 if (em28xx_boards[dev->model].norm == tvnorms[i].mode)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001537 break;
1538 if (i == TVNORMS)
1539 i = 0;
1540
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001541 dev->tvnorm = &tvnorms[i]; /* set default norm */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001542
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001543 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001544
1545 maxw = norm_maxw(dev);
1546 maxh = norm_maxh(dev);
1547
1548 /* set default image size */
1549 dev->width = maxw;
1550 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001551 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001552 dev->field_size = dev->width * dev->height;
1553 dev->frame_size =
1554 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1555 dev->bytesperline = dev->width * 2;
1556 dev->hscale = 0;
1557 dev->vscale = 0;
1558 dev->ctl_input = 2;
1559
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001560 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001561
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001562 /* allocate and fill v4l2 device struct */
1563 dev->vdev = video_device_alloc();
1564 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001565 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001566 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001567 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001568 return -ENOMEM;
1569 }
1570
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001571 dev->vbi_dev = video_device_alloc();
1572 if (NULL == dev->vbi_dev) {
1573 em28xx_errdev("cannot allocate video_device.\n");
1574 kfree(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001575 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001576 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001577 return -ENOMEM;
1578 }
1579
1580 /* Fills VBI device info */
1581 dev->vbi_dev->type = VFL_TYPE_VBI;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001582 dev->vbi_dev->fops = &em28xx_v4l_fops;
1583 dev->vbi_dev->minor = -1;
1584 dev->vbi_dev->dev = &dev->udev->dev;
1585 dev->vbi_dev->release = video_device_release;
1586 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1587 "em28xx",dev->devno,"vbi");
1588
1589 /* Fills CAPTURE device info */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001590 dev->vdev->type = VID_TYPE_CAPTURE;
1591 if (dev->has_tuner)
1592 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001593 dev->vdev->fops = &em28xx_v4l_fops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001594 dev->vdev->minor = -1;
1595 dev->vdev->dev = &dev->udev->dev;
1596 dev->vdev->release = video_device_release;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001597 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1598 "em28xx",dev->devno,"video");
1599
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001600 list_add_tail(&dev->devlist,&em28xx_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001601
1602 /* register v4l2 device */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001603 mutex_lock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001604 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1605 video_nr[dev->devno]))) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001606 em28xx_errdev("unable to register video device (error=%i).\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001607 retval);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001608 mutex_unlock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001609 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001611 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001612 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001613 return -ENODEV;
1614 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001615
1616 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1617 vbi_nr[dev->devno]) < 0) {
1618 printk("unable to register vbi device\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001619 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001620 list_del(&dev->devlist);
1621 video_device_release(dev->vbi_dev);
1622 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001623 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001624 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001625 return -ENODEV;
1626 } else {
1627 printk("registered VBI\n");
1628 }
1629
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001630 if (dev->has_msp34xx) {
1631 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001632 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
David Brownell9a2816c2006-12-27 09:53:44 -03001633 msleep(3);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001634 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
David Brownell9a2816c2006-12-27 09:53:44 -03001635 msleep(3);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001636
1637 }
1638 video_mux(dev, 0);
1639
Ingo Molnar3593cab2006-02-07 06:49:14 -02001640 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001641
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001642 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1643 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1644 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001645
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001646 return 0;
1647}
1648
1649/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001650 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001651 * checks for supported devices
1652 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001653static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001654 const struct usb_device_id *id)
1655{
1656 const struct usb_endpoint_descriptor *endpoint;
1657 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001658 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001659 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001661 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001662
1663 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001664 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1665
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001666 /* Check to see next free device and mark as used */
1667 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1668 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001669
1670 /* Don't register audio interfaces */
1671 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001672 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001673 udev->descriptor.idVendor,udev->descriptor.idProduct,
1674 ifnum,
1675 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001676
1677 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001678 return -ENODEV;
1679 }
1680
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001681 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001682 udev->descriptor.idVendor,udev->descriptor.idProduct,
1683 ifnum,
1684 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001685
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001686 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1687
Michael Opdenacker59c51592007-05-09 08:57:56 +02001688 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001689 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1690 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001691 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001692 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001693 return -ENODEV;
1694 }
1695 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001696 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001697 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001698 return -ENODEV;
1699 }
1700
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03001701 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001702 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001703 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001704 return -ENOMEM;
1705 }
1706
1707 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001708 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001709 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001710 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001711 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001712 return -ENOMEM;
1713 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001714
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001715 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001716 dev->devno = nr;
1717 dev->model = id->driver_info;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001718
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001719 /* compute alternate max packet sizes */
1720 uif = udev->actconfig->interface[0];
1721
1722 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001723 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001724// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1725 dev->alt_max_pkt_size = kmalloc(32*
1726 dev->num_alt,GFP_KERNEL);
1727 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001728 em28xx_errdev("out of memory!\n");
1729 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02001730 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001731 return -ENOMEM;
1732 }
1733
1734 for (i = 0; i < dev->num_alt ; i++) {
1735 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1736 wMaxPacketSize);
1737 dev->alt_max_pkt_size[i] =
1738 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001739 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001740 dev->alt_max_pkt_size[i]);
1741 }
1742
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001743 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001744 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001745
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001746 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001747 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001748 if (retval)
1749 return retval;
1750
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001751 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001752
1753 /* save our data pointer in this interface device */
1754 usb_set_intfdata(interface, dev);
1755 return 0;
1756}
1757
1758/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001759 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760 * called when the device gets diconencted
1761 * video device will be unregistered on v4l2_close in case it is still open
1762 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001763static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001764{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001765 struct em28xx *dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001766 usb_set_intfdata(interface, NULL);
1767
1768 if (!dev)
1769 return;
1770
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001771 down_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001772
Ingo Molnar3593cab2006-02-07 06:49:14 -02001773 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001774
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001775 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001776
1777 wake_up_interruptible_all(&dev->open);
1778
1779 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001780 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001782 "deallocation are deferred on close.\n",
1783 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1784
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001785 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001786 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001787 dev->state |= DEV_DISCONNECTED;
1788 wake_up_interruptible(&dev->wait_frame);
1789 wake_up_interruptible(&dev->wait_stream);
1790 } else {
1791 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001792 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001793 }
1794
Ingo Molnar3593cab2006-02-07 06:49:14 -02001795 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001796
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001797 if (!dev->users) {
1798 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001799 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001800 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001801
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001802 up_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001803}
1804
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001805static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001806 .name = "em28xx",
1807 .probe = em28xx_usb_probe,
1808 .disconnect = em28xx_usb_disconnect,
1809 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810};
1811
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001812static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001813{
1814 int result;
1815
1816 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001817 (EM28XX_VERSION_CODE >> 16) & 0xff,
1818 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001819#ifdef SNAPSHOT
1820 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1821 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1822#endif
1823
1824 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001825 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001826 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001827 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001828 " usb_register failed. Error number %d.\n", result);
1829
1830 return result;
1831}
1832
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001833static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001834{
1835 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001836 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001837}
1838
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001839module_init(em28xx_module_init);
1840module_exit(em28xx_module_exit);