akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 2 | em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 3 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 4 | Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> |
| 5 | Markus Rechberger <mrechberger@gmail.com> |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 6 | Mauro Carvalho Chehab <mchehab@brturbo.com.br> |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 7 | Sascha Sommer <saschasommer@freenet.de> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 8 | |
Mauro Carvalho Chehab | 439090d | 2006-01-23 17:10:54 -0200 | [diff] [blame] | 9 | Some parts based on SN9C10x PC Camera Controllers GPL driver made |
| 10 | by Luca Risolia <luca.risolia@studio.unibo.it> |
| 11 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 12 | 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 Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 31 | #include <linux/bitmap.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 32 | #include <linux/usb.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 33 | #include <linux/i2c.h> |
Mauro Carvalho Chehab | b296fc6 | 2005-11-08 21:38:37 -0800 | [diff] [blame] | 34 | #include <linux/version.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 35 | #include <linux/video_decoder.h> |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 36 | #include <linux/mutex.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 37 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 38 | #include "em28xx.h" |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 39 | #include <media/tuner.h> |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 40 | #include <media/v4l2-common.h> |
Hans Verkuil | 2474ed4 | 2006-03-19 12:35:57 -0300 | [diff] [blame] | 41 | #include <media/msp3400.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 42 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 43 | #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ |
| 44 | "Markus Rechberger <mrechberger@gmail.com>, " \ |
| 45 | "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \ |
| 46 | "Sascha Sommer <saschasommer@freenet.de>" |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 47 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 48 | #define DRIVER_NAME "em28xx" |
| 49 | #define DRIVER_DESC "Empia em28xx based USB video device driver" |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 50 | #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 51 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 52 | #define em28xx_videodbg(fmt, arg...) do {\ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 53 | if (video_debug) \ |
| 54 | printk(KERN_INFO "%s %s :"fmt, \ |
Jean Delvare | f85c657 | 2005-12-19 08:53:59 -0200 | [diff] [blame] | 55 | dev->name, __FUNCTION__ , ##arg); } while (0) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 56 | |
| 57 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 58 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 59 | MODULE_LICENSE("GPL"); |
| 60 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 61 | static LIST_HEAD(em28xx_devlist); |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 62 | |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 63 | static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 64 | static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
| 65 | static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 66 | module_param_array(card, int, NULL, 0444); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 67 | module_param_array(video_nr, int, NULL, 0444); |
| 68 | module_param_array(vbi_nr, int, NULL, 0444); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 69 | MODULE_PARM_DESC(card,"card type"); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 70 | MODULE_PARM_DESC(video_nr,"video device numbers"); |
| 71 | MODULE_PARM_DESC(vbi_nr,"vbi device numbers"); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 72 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 73 | static int tuner = -1; |
| 74 | module_param(tuner, int, 0444); |
| 75 | MODULE_PARM_DESC(tuner, "tuner type"); |
| 76 | |
| 77 | static unsigned int video_debug = 0; |
| 78 | module_param(video_debug,int,0644); |
| 79 | MODULE_PARM_DESC(video_debug,"enable debug messages [video]"); |
| 80 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 81 | /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */ |
| 82 | static unsigned long em28xx_devused; |
| 83 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 84 | /* supported tv norms */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 85 | static struct em28xx_tvnorm tvnorms[] = { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 86 | { |
| 87 | .name = "PAL", |
| 88 | .id = V4L2_STD_PAL, |
| 89 | .mode = VIDEO_MODE_PAL, |
| 90 | }, { |
| 91 | .name = "NTSC", |
| 92 | .id = V4L2_STD_NTSC, |
| 93 | .mode = VIDEO_MODE_NTSC, |
| 94 | }, { |
| 95 | .name = "SECAM", |
| 96 | .id = V4L2_STD_SECAM, |
| 97 | .mode = VIDEO_MODE_SECAM, |
| 98 | }, { |
| 99 | .name = "PAL-M", |
| 100 | .id = V4L2_STD_PAL_M, |
| 101 | .mode = VIDEO_MODE_PAL, |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | #define TVNORMS ARRAY_SIZE(tvnorms) |
| 106 | |
| 107 | /* supported controls */ |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 108 | /* Common to all boards */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 109 | static struct v4l2_queryctrl em28xx_qctrl[] = { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 110 | { |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 111 | .id = V4L2_CID_AUDIO_VOLUME, |
| 112 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 113 | .name = "Volume", |
| 114 | .minimum = 0x0, |
| 115 | .maximum = 0x1f, |
| 116 | .step = 0x1, |
| 117 | .default_value = 0x1f, |
| 118 | .flags = 0, |
| 119 | },{ |
| 120 | .id = V4L2_CID_AUDIO_MUTE, |
| 121 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 122 | .name = "Mute", |
| 123 | .minimum = 0, |
| 124 | .maximum = 1, |
| 125 | .step = 1, |
| 126 | .default_value = 1, |
| 127 | .flags = 0, |
| 128 | } |
| 129 | }; |
| 130 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 131 | static struct usb_driver em28xx_usb_driver; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 132 | |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 133 | static DEFINE_MUTEX(em28xx_sysfs_lock); |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 134 | static DECLARE_RWSEM(em28xx_disconnect); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 135 | |
| 136 | /********************* v4l2 interface ******************************************/ |
| 137 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 138 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 139 | * em28xx_config() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 140 | * inits registers with sane defaults |
| 141 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 142 | static int em28xx_config(struct em28xx *dev) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 143 | { |
| 144 | |
| 145 | /* Sets I2C speed to 100 KHz */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 146 | em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 147 | |
| 148 | /* enable vbi capturing */ |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 149 | |
Markus Rechberger | 9475fb1 | 2006-02-27 00:07:34 -0300 | [diff] [blame] | 150 | /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */ |
| 151 | /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */ |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 152 | em28xx_write_regs_req(dev,0x00,0x11,"\x51",1); |
| 153 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 154 | em28xx_audio_usb_mute(dev, 1); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 155 | dev->mute = 1; /* maybe not the right place... */ |
| 156 | dev->volume = 0x1f; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 157 | em28xx_audio_analog_set(dev); |
| 158 | em28xx_audio_analog_setup(dev); |
| 159 | em28xx_outfmt_set_yuv422(dev); |
| 160 | em28xx_colorlevels_set_default(dev); |
| 161 | em28xx_compression_disable(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 167 | * em28xx_config_i2c() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 168 | * configure i2c attached devices |
| 169 | */ |
Adrian Bunk | 943a490 | 2005-12-01 00:51:35 -0800 | [diff] [blame] | 170 | static void em28xx_config_i2c(struct em28xx *dev) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 171 | { |
| 172 | struct v4l2_frequency f; |
Mauro Carvalho Chehab | f5762e4 | 2006-03-13 13:31:31 -0300 | [diff] [blame] | 173 | em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL); |
| 174 | em28xx_i2c_call_clients(dev, VIDIOC_S_INPUT, &dev->ctl_input); |
| 175 | em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 176 | |
| 177 | /* configure tuner */ |
| 178 | f.tuner = 0; |
| 179 | f.type = V4L2_TUNER_ANALOG_TV; |
| 180 | f.frequency = 9076; /* FIXME:remove magic number */ |
| 181 | dev->ctl_freq = f.frequency; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 182 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 183 | |
| 184 | /* configure tda9887 */ |
| 185 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 186 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 187 | /* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 191 | * em28xx_empty_framequeues() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 192 | * prepare queues for incoming and outgoing frames |
| 193 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 194 | static void em28xx_empty_framequeues(struct em28xx *dev) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 195 | { |
| 196 | u32 i; |
| 197 | |
| 198 | INIT_LIST_HEAD(&dev->inqueue); |
| 199 | INIT_LIST_HEAD(&dev->outqueue); |
| 200 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 201 | for (i = 0; i < EM28XX_NUM_FRAMES; i++) { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 202 | dev->frame[i].state = F_UNUSED; |
| 203 | dev->frame[i].buf.bytesused = 0; |
| 204 | } |
| 205 | } |
| 206 | |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 207 | static void video_mux(struct em28xx *dev, int index) |
| 208 | { |
| 209 | int input, ainput; |
| 210 | |
| 211 | input = INPUT(index)->vmux; |
| 212 | dev->ctl_input = index; |
| 213 | dev->ctl_ainput = INPUT(index)->amux; |
| 214 | |
Mauro Carvalho Chehab | f5762e4 | 2006-03-13 13:31:31 -0300 | [diff] [blame] | 215 | em28xx_i2c_call_clients(dev, VIDIOC_S_INPUT, &input); |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 216 | |
| 217 | em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput); |
| 218 | |
| 219 | if (dev->has_msp34xx) { |
Hans Verkuil | 2474ed4 | 2006-03-19 12:35:57 -0300 | [diff] [blame] | 220 | struct v4l2_routing route; |
| 221 | |
Mauro Carvalho Chehab | 9bb13a6 | 2006-01-09 15:25:37 -0200 | [diff] [blame] | 222 | if (dev->i2s_speed) |
| 223 | em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed); |
Hans Verkuil | 2474ed4 | 2006-03-19 12:35:57 -0300 | [diff] [blame] | 224 | route.input = dev->ctl_ainput; |
Hans Verkuil | 0715172 | 2006-04-01 18:03:23 -0300 | [diff] [blame] | 225 | route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); |
Hans Verkuil | 2474ed4 | 2006-03-19 12:35:57 -0300 | [diff] [blame] | 226 | /* Note: this is msp3400 specific */ |
| 227 | em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route); |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 228 | ainput = EM28XX_AUDIO_SRC_TUNER; |
| 229 | em28xx_audio_source(dev, ainput); |
| 230 | } else { |
| 231 | switch (dev->ctl_ainput) { |
Markus Rechberger | 9475fb1 | 2006-02-27 00:07:34 -0300 | [diff] [blame] | 232 | case 0: |
| 233 | ainput = EM28XX_AUDIO_SRC_TUNER; |
| 234 | break; |
| 235 | default: |
| 236 | ainput = EM28XX_AUDIO_SRC_LINE; |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 237 | } |
| 238 | em28xx_audio_source(dev, ainput); |
| 239 | } |
| 240 | } |
| 241 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 242 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 243 | * em28xx_v4l2_open() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 244 | * inits the device and starts isoc transfer |
| 245 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 246 | static int em28xx_v4l2_open(struct inode *inode, struct file *filp) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 247 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 248 | int minor = iminor(inode); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 249 | int errCode = 0; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 250 | struct em28xx *h,*dev = NULL; |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 251 | struct list_head *list; |
| 252 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 253 | list_for_each(list,&em28xx_devlist) { |
| 254 | h = list_entry(list, struct em28xx, devlist); |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 255 | if (h->vdev->minor == minor) { |
| 256 | dev = h; |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 257 | dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 258 | } |
| 259 | if (h->vbi_dev->minor == minor) { |
| 260 | dev = h; |
| 261 | dev->type = V4L2_BUF_TYPE_VBI_CAPTURE; |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 262 | } |
| 263 | } |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 264 | if (NULL == dev) |
| 265 | return -ENODEV; |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 266 | |
| 267 | filp->private_data=dev; |
| 268 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 269 | em28xx_videodbg("open minor=%d type=%s users=%d\n", |
| 270 | minor,v4l2_type_names[dev->type],dev->users); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 271 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 272 | if (!down_read_trylock(&em28xx_disconnect)) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 273 | return -ERESTARTSYS; |
| 274 | |
| 275 | if (dev->users) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 276 | em28xx_warn("this driver can be opened only once\n"); |
| 277 | up_read(&em28xx_disconnect); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 278 | return -EBUSY; |
| 279 | } |
| 280 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 281 | mutex_init(&dev->fileop_lock); /* to 1 == available */ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 282 | spin_lock_init(&dev->queue_lock); |
| 283 | init_waitqueue_head(&dev->wait_frame); |
| 284 | init_waitqueue_head(&dev->wait_stream); |
| 285 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 286 | mutex_lock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 287 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 288 | if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 289 | em28xx_set_alternate(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 290 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 291 | dev->width = norm_maxw(dev); |
| 292 | dev->height = norm_maxh(dev); |
| 293 | dev->frame_size = dev->width * dev->height * 2; |
| 294 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ |
| 295 | dev->bytesperline = dev->width * 2; |
| 296 | dev->hscale = 0; |
| 297 | dev->vscale = 0; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 298 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 299 | em28xx_capture_start(dev, 1); |
| 300 | em28xx_resolution_set(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 301 | |
Markus Rechberger | e5d4a56 | 2006-02-07 06:49:13 -0200 | [diff] [blame] | 302 | /* device needs to be initialized before isoc transfer */ |
| 303 | video_mux(dev, 0); |
| 304 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 305 | /* start the transfer */ |
| 306 | errCode = em28xx_init_isoc(dev); |
| 307 | if (errCode) |
| 308 | goto err; |
| 309 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 310 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 311 | |
| 312 | dev->users++; |
| 313 | filp->private_data = dev; |
| 314 | dev->io = IO_NONE; |
| 315 | dev->stream = STREAM_OFF; |
| 316 | dev->num_frames = 0; |
| 317 | |
| 318 | /* prepare queues */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 319 | em28xx_empty_framequeues(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 320 | |
| 321 | dev->state |= DEV_INITIALIZED; |
| 322 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 323 | err: |
| 324 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 325 | up_read(&em28xx_disconnect); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 326 | return errCode; |
| 327 | } |
| 328 | |
| 329 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 330 | * em28xx_realease_resources() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 331 | * unregisters the v4l2,i2c and usb devices |
| 332 | * called when the device gets disconected or at module unload |
| 333 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 334 | static void em28xx_release_resources(struct em28xx *dev) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 335 | { |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 336 | mutex_lock(&em28xx_sysfs_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 337 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 338 | /*FIXME: I2C IR should be disconnected */ |
| 339 | |
| 340 | em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n", |
| 341 | dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN, |
| 342 | dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN); |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 343 | list_del(&dev->devlist); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 344 | video_unregister_device(dev->vdev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 345 | video_unregister_device(dev->vbi_dev); |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 346 | em28xx_i2c_unregister(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 347 | usb_put_dev(dev->udev); |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 348 | mutex_unlock(&em28xx_sysfs_lock); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 349 | |
| 350 | |
| 351 | /* Mark device as unused */ |
| 352 | em28xx_devused&=~(1<<dev->devno); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 356 | * em28xx_v4l2_close() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 357 | * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls |
| 358 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 359 | static int em28xx_v4l2_close(struct inode *inode, struct file *filp) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 360 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 361 | int errCode; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 362 | struct em28xx *dev=filp->private_data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 363 | |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 364 | em28xx_videodbg("users=%d\n", dev->users); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 365 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 366 | mutex_lock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 367 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 368 | em28xx_uninit_isoc(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 369 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 370 | em28xx_release_buffers(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 371 | |
| 372 | /* the device is already disconnect, free the remaining resources */ |
| 373 | if (dev->state & DEV_DISCONNECTED) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 374 | em28xx_release_resources(dev); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 375 | mutex_unlock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 376 | kfree(dev); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* set alternate 0 */ |
| 381 | dev->alt = 0; |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 382 | em28xx_videodbg("setting alternate 0\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 383 | errCode = usb_set_interface(dev->udev, 0, 0); |
| 384 | if (errCode < 0) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 385 | em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 386 | errCode); |
| 387 | } |
| 388 | |
| 389 | dev->users--; |
| 390 | wake_up_interruptible_nr(&dev->open, 1); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 391 | mutex_unlock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 396 | * em28xx_v4l2_read() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 397 | * will allocate buffers when called for the first time |
| 398 | */ |
| 399 | static ssize_t |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 400 | em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 401 | loff_t * f_pos) |
| 402 | { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 403 | struct em28xx_frame_t *f, *i; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 404 | unsigned long lock_flags; |
| 405 | int ret = 0; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 406 | struct em28xx *dev = filp->private_data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 407 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 408 | if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 409 | em28xx_videodbg("V4l2_Buf_type_videocapture is set\n"); |
| 410 | } |
| 411 | if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) { |
| 412 | em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n"); |
| 413 | em28xx_videodbg("not supported yet! ...\n"); |
| 414 | if (copy_to_user(buf, "", 1)) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 415 | mutex_unlock(&dev->fileop_lock); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 416 | return -EFAULT; |
| 417 | } |
| 418 | return (1); |
| 419 | } |
| 420 | if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) { |
| 421 | em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n"); |
| 422 | em28xx_videodbg("not supported yet! ...\n"); |
| 423 | if (copy_to_user(buf, "", 1)) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 424 | mutex_unlock(&dev->fileop_lock); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 425 | return -EFAULT; |
| 426 | } |
| 427 | return (1); |
| 428 | } |
| 429 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 430 | if (mutex_lock_interruptible(&dev->fileop_lock)) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 431 | return -ERESTARTSYS; |
| 432 | |
| 433 | if (dev->state & DEV_DISCONNECTED) { |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 434 | em28xx_videodbg("device not present\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 435 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 436 | return -ENODEV; |
| 437 | } |
| 438 | |
| 439 | if (dev->state & DEV_MISCONFIGURED) { |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 440 | em28xx_videodbg("device misconfigured; close and open it again\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 441 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 442 | return -EIO; |
| 443 | } |
| 444 | |
| 445 | if (dev->io == IO_MMAP) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 446 | em28xx_videodbg ("IO method is set to mmap; close and open" |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 447 | " the device again to choose the read method\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 448 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 449 | return -EINVAL; |
| 450 | } |
| 451 | |
| 452 | if (dev->io == IO_NONE) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 453 | if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) { |
| 454 | em28xx_errdev("read failed, not enough memory\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 455 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 456 | return -ENOMEM; |
| 457 | } |
| 458 | dev->io = IO_READ; |
| 459 | dev->stream = STREAM_ON; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 460 | em28xx_queue_unusedframes(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | if (!count) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 464 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | if (list_empty(&dev->outqueue)) { |
| 469 | if (filp->f_flags & O_NONBLOCK) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 470 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 471 | return -EAGAIN; |
| 472 | } |
| 473 | ret = wait_event_interruptible |
| 474 | (dev->wait_frame, |
| 475 | (!list_empty(&dev->outqueue)) || |
| 476 | (dev->state & DEV_DISCONNECTED)); |
| 477 | if (ret) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 478 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 479 | return ret; |
| 480 | } |
| 481 | if (dev->state & DEV_DISCONNECTED) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 482 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 483 | return -ENODEV; |
| 484 | } |
| 485 | } |
| 486 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 487 | f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 488 | |
| 489 | spin_lock_irqsave(&dev->queue_lock, lock_flags); |
| 490 | list_for_each_entry(i, &dev->outqueue, frame) |
| 491 | i->state = F_UNUSED; |
| 492 | INIT_LIST_HEAD(&dev->outqueue); |
| 493 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); |
| 494 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 495 | em28xx_queue_unusedframes(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 496 | |
| 497 | if (count > f->buf.length) |
| 498 | count = f->buf.length; |
| 499 | |
| 500 | if (copy_to_user(buf, f->bufmem, count)) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 501 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 502 | return -EFAULT; |
| 503 | } |
| 504 | *f_pos += count; |
| 505 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 506 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 507 | |
| 508 | return count; |
| 509 | } |
| 510 | |
| 511 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 512 | * em28xx_v4l2_poll() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 513 | * will allocate buffers when called for the first time |
| 514 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 515 | static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 516 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 517 | unsigned int mask = 0; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 518 | struct em28xx *dev = filp->private_data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 519 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 520 | if (mutex_lock_interruptible(&dev->fileop_lock)) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 521 | return POLLERR; |
| 522 | |
| 523 | if (dev->state & DEV_DISCONNECTED) { |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 524 | em28xx_videodbg("device not present\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 525 | } else if (dev->state & DEV_MISCONFIGURED) { |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 526 | em28xx_videodbg("device is misconfigured; close and open it again\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 527 | } else { |
| 528 | if (dev->io == IO_NONE) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 529 | if (!em28xx_request_buffers |
| 530 | (dev, EM28XX_NUM_READ_FRAMES)) { |
| 531 | em28xx_warn |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 532 | ("poll() failed, not enough memory\n"); |
| 533 | } else { |
| 534 | dev->io = IO_READ; |
| 535 | dev->stream = STREAM_ON; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (dev->io == IO_READ) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 540 | em28xx_queue_unusedframes(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 541 | poll_wait(filp, &dev->wait_frame, wait); |
| 542 | |
| 543 | if (!list_empty(&dev->outqueue)) |
| 544 | mask |= POLLIN | POLLRDNORM; |
| 545 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 546 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 547 | |
| 548 | return mask; |
| 549 | } |
| 550 | } |
| 551 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 552 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 553 | return POLLERR; |
| 554 | } |
| 555 | |
| 556 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 557 | * em28xx_vm_open() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 558 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 559 | static void em28xx_vm_open(struct vm_area_struct *vma) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 560 | { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 561 | struct em28xx_frame_t *f = vma->vm_private_data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 562 | f->vma_use_count++; |
| 563 | } |
| 564 | |
| 565 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 566 | * em28xx_vm_close() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 567 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 568 | static void em28xx_vm_close(struct vm_area_struct *vma) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 569 | { |
| 570 | /* NOTE: buffers are not freed here */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 571 | struct em28xx_frame_t *f = vma->vm_private_data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 572 | f->vma_use_count--; |
| 573 | } |
| 574 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 575 | static struct vm_operations_struct em28xx_vm_ops = { |
| 576 | .open = em28xx_vm_open, |
| 577 | .close = em28xx_vm_close, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 578 | }; |
| 579 | |
| 580 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 581 | * em28xx_v4l2_mmap() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 582 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 583 | static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 584 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 585 | unsigned long size = vma->vm_end - vma->vm_start, |
Sascha Sommer | 3639c86 | 2005-12-12 00:37:30 -0800 | [diff] [blame] | 586 | start = vma->vm_start; |
| 587 | void *pos; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 588 | u32 i; |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 589 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 590 | struct em28xx *dev = filp->private_data; |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 591 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 592 | if (mutex_lock_interruptible(&dev->fileop_lock)) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 593 | return -ERESTARTSYS; |
| 594 | |
| 595 | if (dev->state & DEV_DISCONNECTED) { |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 596 | em28xx_videodbg("mmap: device not present\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 597 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 598 | return -ENODEV; |
| 599 | } |
| 600 | |
| 601 | if (dev->state & DEV_MISCONFIGURED) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 602 | em28xx_videodbg ("mmap: Device is misconfigured; close and " |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 603 | "open it again\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 604 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 605 | return -EIO; |
| 606 | } |
| 607 | |
| 608 | if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || |
| 609 | size != PAGE_ALIGN(dev->frame[0].buf.length)) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 610 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 611 | return -EINVAL; |
| 612 | } |
| 613 | |
| 614 | for (i = 0; i < dev->num_frames; i++) { |
| 615 | if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff) |
| 616 | break; |
| 617 | } |
| 618 | if (i == dev->num_frames) { |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 619 | em28xx_videodbg("mmap: user supplied mapping address is out of range\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 620 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 621 | return -EINVAL; |
| 622 | } |
| 623 | |
| 624 | /* VM_IO is eventually going to replace PageReserved altogether */ |
| 625 | vma->vm_flags |= VM_IO; |
| 626 | vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ |
| 627 | |
Sascha Sommer | 3639c86 | 2005-12-12 00:37:30 -0800 | [diff] [blame] | 628 | pos = dev->frame[i].bufmem; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 629 | while (size > 0) { /* size is page-aligned */ |
Sascha Sommer | 3639c86 | 2005-12-12 00:37:30 -0800 | [diff] [blame] | 630 | if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { |
| 631 | em28xx_videodbg("mmap: vm_insert_page failed\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 632 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 633 | return -EAGAIN; |
| 634 | } |
| 635 | start += PAGE_SIZE; |
| 636 | pos += PAGE_SIZE; |
| 637 | size -= PAGE_SIZE; |
| 638 | } |
| 639 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 640 | vma->vm_ops = &em28xx_vm_ops; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 641 | vma->vm_private_data = &dev->frame[i]; |
| 642 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 643 | em28xx_vm_open(vma); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 644 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 649 | * em28xx_get_ctrl() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 650 | * return the current saturation, brightness or contrast, mute state |
| 651 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 652 | static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 653 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 654 | switch (ctrl->id) { |
| 655 | case V4L2_CID_AUDIO_MUTE: |
| 656 | ctrl->value = dev->mute; |
| 657 | return 0; |
| 658 | case V4L2_CID_AUDIO_VOLUME: |
| 659 | ctrl->value = dev->volume; |
| 660 | return 0; |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 661 | default: |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | } |
| 665 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 666 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 667 | * em28xx_set_ctrl() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 668 | * mute or set new saturation, brightness or contrast |
| 669 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 670 | static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 671 | { |
| 672 | switch (ctrl->id) { |
| 673 | case V4L2_CID_AUDIO_MUTE: |
| 674 | if (ctrl->value != dev->mute) { |
| 675 | dev->mute = ctrl->value; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 676 | em28xx_audio_usb_mute(dev, ctrl->value); |
| 677 | return em28xx_audio_analog_set(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 678 | } |
| 679 | return 0; |
| 680 | case V4L2_CID_AUDIO_VOLUME: |
| 681 | dev->volume = ctrl->value; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 682 | return em28xx_audio_analog_set(dev); |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 683 | default: |
| 684 | return -EINVAL; |
| 685 | } |
| 686 | } |
| 687 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 688 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 689 | * em28xx_stream_interrupt() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 690 | * stops streaming |
| 691 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 692 | static int em28xx_stream_interrupt(struct em28xx *dev) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 693 | { |
| 694 | int ret = 0; |
| 695 | |
| 696 | /* stop reading from the device */ |
| 697 | |
| 698 | dev->stream = STREAM_INTERRUPT; |
| 699 | ret = wait_event_timeout(dev->wait_stream, |
| 700 | (dev->stream == STREAM_OFF) || |
| 701 | (dev->state & DEV_DISCONNECTED), |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 702 | EM28XX_URB_TIMEOUT); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 703 | if (dev->state & DEV_DISCONNECTED) |
| 704 | return -ENODEV; |
| 705 | else if (ret) { |
| 706 | dev->state |= DEV_MISCONFIGURED; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 707 | em28xx_videodbg("device is misconfigured; close and " |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 708 | "open /dev/video%d again\n", |
| 709 | dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 716 | static int em28xx_set_norm(struct em28xx *dev, int width, int height) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 717 | { |
| 718 | unsigned int hscale, vscale; |
| 719 | unsigned int maxh, maxw; |
| 720 | |
| 721 | maxw = norm_maxw(dev); |
| 722 | maxh = norm_maxh(dev); |
| 723 | |
| 724 | /* width must even because of the YUYV format */ |
| 725 | /* height must be even because of interlacing */ |
| 726 | height &= 0xfffe; |
| 727 | width &= 0xfffe; |
| 728 | |
| 729 | if (height < 32) |
| 730 | height = 32; |
| 731 | if (height > maxh) |
| 732 | height = maxh; |
| 733 | if (width < 48) |
| 734 | width = 48; |
| 735 | if (width > maxw) |
| 736 | width = maxw; |
| 737 | |
| 738 | if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000) |
| 739 | hscale = 0x3fff; |
| 740 | width = (((unsigned long)maxw) << 12) / (hscale + 4096L); |
| 741 | |
| 742 | if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000) |
| 743 | vscale = 0x3fff; |
| 744 | height = (((unsigned long)maxh) << 12) / (vscale + 4096L); |
| 745 | |
| 746 | /* set new image size */ |
| 747 | dev->width = width; |
| 748 | dev->height = height; |
| 749 | dev->frame_size = dev->width * dev->height * 2; |
| 750 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ |
| 751 | dev->bytesperline = dev->width * 2; |
| 752 | dev->hscale = hscale; |
| 753 | dev->vscale = vscale; |
| 754 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 755 | em28xx_resolution_set(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 760 | static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format) |
| 761 | { |
| 762 | em28xx_videodbg("VIDIOC_G_FMT: type=%s\n", |
| 763 | (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ? |
| 764 | "V4L2_BUF_TYPE_VIDEO_CAPTURE" : |
| 765 | (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ? |
| 766 | "V4L2_BUF_TYPE_VBI_CAPTURE" : |
| 767 | (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ? |
| 768 | "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " : |
| 769 | "not supported"); |
| 770 | |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 771 | switch (format->type) { |
| 772 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 773 | { |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 774 | format->fmt.pix.width = dev->width; |
| 775 | format->fmt.pix.height = dev->height; |
| 776 | format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 777 | format->fmt.pix.bytesperline = dev->bytesperline; |
| 778 | format->fmt.pix.sizeimage = dev->frame_size; |
| 779 | format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 780 | format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */ |
| 781 | |
| 782 | em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width, |
| 783 | dev->height); |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | |
| 787 | case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE: |
| 788 | { |
| 789 | format->fmt.sliced.service_set=0; |
| 790 | |
| 791 | em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format); |
| 792 | |
| 793 | if (format->fmt.sliced.service_set==0) |
| 794 | return -EINVAL; |
| 795 | |
| 796 | break; |
| 797 | } |
| 798 | |
| 799 | default: |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | return (0); |
| 803 | } |
| 804 | |
| 805 | static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format) |
| 806 | { |
| 807 | u32 i; |
| 808 | int ret = 0; |
| 809 | int width = format->fmt.pix.width; |
| 810 | int height = format->fmt.pix.height; |
| 811 | unsigned int hscale, vscale; |
| 812 | unsigned int maxh, maxw; |
| 813 | |
| 814 | maxw = norm_maxw(dev); |
| 815 | maxh = norm_maxh(dev); |
| 816 | |
| 817 | em28xx_videodbg("%s: type=%s\n", |
| 818 | cmd == VIDIOC_TRY_FMT ? |
| 819 | "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT", |
| 820 | format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? |
| 821 | "V4L2_BUF_TYPE_VIDEO_CAPTURE" : |
| 822 | format->type == V4L2_BUF_TYPE_VBI_CAPTURE ? |
| 823 | "V4L2_BUF_TYPE_VBI_CAPTURE " : |
| 824 | "not supported"); |
| 825 | |
| 826 | if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) { |
| 827 | em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format); |
| 828 | |
| 829 | if (format->fmt.sliced.service_set==0) |
| 830 | return -EINVAL; |
| 831 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 832 | return 0; |
| 833 | } |
| 834 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 835 | |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 836 | if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 837 | return -EINVAL; |
| 838 | |
| 839 | em28xx_videodbg("%s: requested %dx%d\n", |
| 840 | cmd == VIDIOC_TRY_FMT ? |
| 841 | "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT", |
| 842 | format->fmt.pix.width, format->fmt.pix.height); |
| 843 | |
| 844 | /* FIXME: Move some code away from here */ |
| 845 | /* width must even because of the YUYV format */ |
| 846 | /* height must be even because of interlacing */ |
| 847 | height &= 0xfffe; |
| 848 | width &= 0xfffe; |
| 849 | |
| 850 | if (height < 32) |
| 851 | height = 32; |
| 852 | if (height > maxh) |
| 853 | height = maxh; |
| 854 | if (width < 48) |
| 855 | width = 48; |
| 856 | if (width > maxw) |
| 857 | width = maxw; |
| 858 | |
| 859 | if(dev->is_em2800){ |
| 860 | /* the em2800 can only scale down to 50% */ |
| 861 | if(height % (maxh / 2)) |
| 862 | height=maxh; |
| 863 | if(width % (maxw / 2)) |
| 864 | width=maxw; |
| 865 | /* according to empiatech support */ |
| 866 | /* the MaxPacketSize is to small to support */ |
| 867 | /* framesizes larger than 640x480 @ 30 fps */ |
| 868 | /* or 640x576 @ 25 fps. As this would cut */ |
| 869 | /* of a part of the image we prefer */ |
| 870 | /* 360x576 or 360x480 for now */ |
| 871 | if(width == maxw && height == maxh) |
| 872 | width /= 2; |
| 873 | } |
| 874 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 875 | if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000) |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 876 | hscale = 0x3fff; |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 877 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 878 | width = (((unsigned long)maxw) << 12) / (hscale + 4096L); |
| 879 | |
| 880 | if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000) |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 881 | vscale = 0x3fff; |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 882 | |
| 883 | height = (((unsigned long)maxh) << 12) / (vscale + 4096L); |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 884 | |
| 885 | format->fmt.pix.width = width; |
| 886 | format->fmt.pix.height = height; |
| 887 | format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 888 | format->fmt.pix.bytesperline = width * 2; |
| 889 | format->fmt.pix.sizeimage = width * 2 * height; |
| 890 | format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 891 | format->fmt.pix.field = V4L2_FIELD_INTERLACED; |
| 892 | |
| 893 | em28xx_videodbg("%s: returned %dx%d (%d, %d)\n", |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 894 | cmd == VIDIOC_TRY_FMT ? |
| 895 | "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT", |
| 896 | format->fmt.pix.width, format->fmt.pix.height, hscale, vscale); |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 897 | |
| 898 | if (cmd == VIDIOC_TRY_FMT) |
| 899 | return 0; |
| 900 | |
| 901 | for (i = 0; i < dev->num_frames; i++) |
| 902 | if (dev->frame[i].vma_use_count) { |
| 903 | em28xx_videodbg("VIDIOC_S_FMT failed. " |
| 904 | "Unmap the buffers first.\n"); |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | |
| 908 | /* stop io in case it is already in progress */ |
| 909 | if (dev->stream == STREAM_ON) { |
| 910 | em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n"); |
| 911 | if ((ret = em28xx_stream_interrupt(dev))) |
| 912 | return ret; |
| 913 | } |
| 914 | |
| 915 | em28xx_release_buffers(dev); |
| 916 | dev->io = IO_NONE; |
| 917 | |
| 918 | /* set new image size */ |
| 919 | dev->width = width; |
| 920 | dev->height = height; |
| 921 | dev->frame_size = dev->width * dev->height * 2; |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 922 | dev->field_size = dev->frame_size >> 1; |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 923 | dev->bytesperline = dev->width * 2; |
| 924 | dev->hscale = hscale; |
| 925 | dev->vscale = vscale; |
| 926 | em28xx_uninit_isoc(dev); |
| 927 | em28xx_set_alternate(dev); |
| 928 | em28xx_capture_start(dev, 1); |
| 929 | em28xx_resolution_set(dev); |
| 930 | em28xx_init_isoc(dev); |
| 931 | |
| 932 | return 0; |
| 933 | } |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 934 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 935 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 936 | * em28xx_v4l2_do_ioctl() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 937 | * This function is _not_ called directly, but from |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 938 | * em28xx_v4l2_ioctl. Userspace |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 939 | * copying is done already, arg is a kernel pointer. |
| 940 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 941 | static int em28xx_do_ioctl(struct inode *inode, struct file *filp, |
| 942 | struct em28xx *dev, unsigned int cmd, void *arg, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 943 | v4l2_kioctl driver_ioctl) |
| 944 | { |
| 945 | int ret; |
| 946 | |
| 947 | switch (cmd) { |
| 948 | /* ---------- tv norms ---------- */ |
| 949 | case VIDIOC_ENUMSTD: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 950 | { |
| 951 | struct v4l2_standard *e = arg; |
| 952 | unsigned int i; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 953 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 954 | i = e->index; |
| 955 | if (i >= TVNORMS) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 956 | return -EINVAL; |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 957 | ret = v4l2_video_std_construct(e, tvnorms[e->index].id, |
| 958 | tvnorms[e->index].name); |
| 959 | e->index = i; |
| 960 | if (ret < 0) |
| 961 | return ret; |
| 962 | return 0; |
| 963 | } |
| 964 | case VIDIOC_G_STD: |
| 965 | { |
| 966 | v4l2_std_id *id = arg; |
| 967 | |
| 968 | *id = dev->tvnorm->id; |
| 969 | return 0; |
| 970 | } |
| 971 | case VIDIOC_S_STD: |
| 972 | { |
| 973 | v4l2_std_id *id = arg; |
| 974 | unsigned int i; |
| 975 | |
| 976 | for (i = 0; i < TVNORMS; i++) |
| 977 | if (*id == tvnorms[i].id) |
| 978 | break; |
| 979 | if (i == TVNORMS) |
| 980 | for (i = 0; i < TVNORMS; i++) |
| 981 | if (*id & tvnorms[i].id) |
| 982 | break; |
| 983 | if (i == TVNORMS) |
| 984 | return -EINVAL; |
| 985 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 986 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 987 | dev->tvnorm = &tvnorms[i]; |
| 988 | |
| 989 | em28xx_set_norm(dev, dev->width, dev->height); |
| 990 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 991 | em28xx_i2c_call_clients(dev, VIDIOC_S_STD, |
| 992 | &dev->tvnorm->id); |
| 993 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 994 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | /* ------ input switching ---------- */ |
| 1000 | case VIDIOC_ENUMINPUT: |
| 1001 | { |
| 1002 | struct v4l2_input *i = arg; |
| 1003 | unsigned int n; |
| 1004 | static const char *iname[] = { |
| 1005 | [EM28XX_VMUX_COMPOSITE1] = "Composite1", |
| 1006 | [EM28XX_VMUX_COMPOSITE2] = "Composite2", |
| 1007 | [EM28XX_VMUX_COMPOSITE3] = "Composite3", |
| 1008 | [EM28XX_VMUX_COMPOSITE4] = "Composite4", |
| 1009 | [EM28XX_VMUX_SVIDEO] = "S-Video", |
| 1010 | [EM28XX_VMUX_TELEVISION] = "Television", |
| 1011 | [EM28XX_VMUX_CABLE] = "Cable TV", |
| 1012 | [EM28XX_VMUX_DVB] = "DVB", |
| 1013 | [EM28XX_VMUX_DEBUG] = "for debug only", |
| 1014 | }; |
| 1015 | |
| 1016 | n = i->index; |
| 1017 | if (n >= MAX_EM28XX_INPUT) |
| 1018 | return -EINVAL; |
| 1019 | if (0 == INPUT(n)->type) |
| 1020 | return -EINVAL; |
| 1021 | memset(i, 0, sizeof(*i)); |
| 1022 | i->index = n; |
| 1023 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 1024 | strcpy(i->name, iname[INPUT(n)->type]); |
| 1025 | if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) || |
| 1026 | (EM28XX_VMUX_CABLE == INPUT(n)->type)) |
| 1027 | i->type = V4L2_INPUT_TYPE_TUNER; |
| 1028 | for (n = 0; n < ARRAY_SIZE(tvnorms); n++) |
| 1029 | i->std |= tvnorms[n].id; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | case VIDIOC_G_INPUT: |
| 1033 | { |
| 1034 | int *i = arg; |
| 1035 | *i = dev->ctl_input; |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | case VIDIOC_S_INPUT: |
| 1040 | { |
| 1041 | int *index = arg; |
| 1042 | |
| 1043 | if (*index >= MAX_EM28XX_INPUT) |
| 1044 | return -EINVAL; |
| 1045 | if (0 == INPUT(*index)->type) |
| 1046 | return -EINVAL; |
| 1047 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1048 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1049 | video_mux(dev, *index); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1050 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | case VIDIOC_G_AUDIO: |
| 1055 | { |
| 1056 | struct v4l2_audio *a = arg; |
| 1057 | unsigned int index = a->index; |
| 1058 | |
| 1059 | if (a->index > 1) |
| 1060 | return -EINVAL; |
| 1061 | memset(a, 0, sizeof(*a)); |
| 1062 | index = dev->ctl_ainput; |
| 1063 | |
| 1064 | if (index == 0) { |
| 1065 | strcpy(a->name, "Television"); |
| 1066 | } else { |
| 1067 | strcpy(a->name, "Line In"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1068 | } |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1069 | a->capability = V4L2_AUDCAP_STEREO; |
| 1070 | a->index = index; |
| 1071 | return 0; |
| 1072 | } |
| 1073 | case VIDIOC_S_AUDIO: |
| 1074 | { |
| 1075 | struct v4l2_audio *a = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1076 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1077 | if (a->index != dev->ctl_ainput) |
| 1078 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1079 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | /* --- controls ---------------------------------------------- */ |
| 1084 | case VIDIOC_QUERYCTRL: |
| 1085 | { |
| 1086 | struct v4l2_queryctrl *qc = arg; |
| 1087 | int i, id=qc->id; |
| 1088 | |
| 1089 | memset(qc,0,sizeof(*qc)); |
| 1090 | qc->id=id; |
| 1091 | |
| 1092 | if (!dev->has_msp34xx) { |
| 1093 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { |
| 1094 | if (qc->id && qc->id == em28xx_qctrl[i].id) { |
| 1095 | memcpy(qc, &(em28xx_qctrl[i]), |
| 1096 | sizeof(*qc)); |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1099 | } |
| 1100 | } |
Mauro Carvalho Chehab | f5762e4 | 2006-03-13 13:31:31 -0300 | [diff] [blame] | 1101 | em28xx_i2c_call_clients(dev,cmd,qc); |
| 1102 | if (qc->type) |
| 1103 | return 0; |
| 1104 | else |
| 1105 | return -EINVAL; |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1106 | } |
| 1107 | case VIDIOC_G_CTRL: |
| 1108 | { |
| 1109 | struct v4l2_control *ctrl = arg; |
| 1110 | int retval=-EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1111 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1112 | if (!dev->has_msp34xx) |
| 1113 | retval=em28xx_get_ctrl(dev, ctrl); |
| 1114 | if (retval==-EINVAL) { |
Mauro Carvalho Chehab | f5762e4 | 2006-03-13 13:31:31 -0300 | [diff] [blame] | 1115 | em28xx_i2c_call_clients(dev,cmd,arg); |
| 1116 | return 0; |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1117 | } else return retval; |
| 1118 | } |
| 1119 | case VIDIOC_S_CTRL: |
| 1120 | { |
| 1121 | struct v4l2_control *ctrl = arg; |
| 1122 | u8 i; |
| 1123 | |
| 1124 | if (!dev->has_msp34xx){ |
| 1125 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { |
| 1126 | if (ctrl->id == em28xx_qctrl[i].id) { |
| 1127 | if (ctrl->value < |
| 1128 | em28xx_qctrl[i].minimum |
| 1129 | || ctrl->value > |
| 1130 | em28xx_qctrl[i].maximum) |
| 1131 | return -ERANGE; |
| 1132 | return em28xx_set_ctrl(dev, ctrl); |
| 1133 | } |
| 1134 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Mauro Carvalho Chehab | f5762e4 | 2006-03-13 13:31:31 -0300 | [diff] [blame] | 1137 | em28xx_i2c_call_clients(dev,cmd,arg); |
| 1138 | return 0; |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1139 | } |
| 1140 | /* --- tuner ioctls ------------------------------------------ */ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1141 | case VIDIOC_G_TUNER: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1142 | { |
| 1143 | struct v4l2_tuner *t = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1144 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1145 | if (0 != t->index) |
| 1146 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1147 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1148 | memset(t, 0, sizeof(*t)); |
| 1149 | strcpy(t->name, "Tuner"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1150 | mutex_lock(&dev->lock); |
Hans Verkuil | ab4cecf | 2006-04-01 16:40:21 -0300 | [diff] [blame] | 1151 | /* let clients fill in the remainder of this struct */ |
| 1152 | em28xx_i2c_call_clients(dev, cmd, t); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1153 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1154 | em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal, |
| 1155 | t->afc); |
| 1156 | return 0; |
| 1157 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1158 | case VIDIOC_S_TUNER: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1159 | { |
| 1160 | struct v4l2_tuner *t = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1161 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1162 | if (0 != t->index) |
| 1163 | return -EINVAL; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1164 | mutex_lock(&dev->lock); |
Hans Verkuil | ab4cecf | 2006-04-01 16:40:21 -0300 | [diff] [blame] | 1165 | /* let clients handle this */ |
| 1166 | em28xx_i2c_call_clients(dev, cmd, t); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1167 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1170 | case VIDIOC_G_FREQUENCY: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1171 | { |
| 1172 | struct v4l2_frequency *f = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1173 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1174 | memset(f, 0, sizeof(*f)); |
| 1175 | f->type = V4L2_TUNER_ANALOG_TV; |
| 1176 | f->frequency = dev->ctl_freq; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1177 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1178 | return 0; |
| 1179 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1180 | case VIDIOC_S_FREQUENCY: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1181 | { |
| 1182 | struct v4l2_frequency *f = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1183 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1184 | if (0 != f->tuner) |
| 1185 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1186 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1187 | if (V4L2_TUNER_ANALOG_TV != f->type) |
| 1188 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1189 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1190 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1191 | dev->ctl_freq = f->frequency; |
| 1192 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1193 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1194 | return 0; |
| 1195 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1196 | case VIDIOC_CROPCAP: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1197 | { |
| 1198 | struct v4l2_cropcap *cc = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1199 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1200 | if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1201 | return -EINVAL; |
| 1202 | cc->bounds.left = 0; |
| 1203 | cc->bounds.top = 0; |
| 1204 | cc->bounds.width = dev->width; |
| 1205 | cc->bounds.height = dev->height; |
| 1206 | cc->defrect = cc->bounds; |
| 1207 | cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */ |
| 1208 | cc->pixelaspect.denominator = 59; |
| 1209 | return 0; |
| 1210 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1211 | case VIDIOC_STREAMON: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1212 | { |
| 1213 | int *type = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1214 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1215 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1216 | || dev->io != IO_MMAP) |
| 1217 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1218 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1219 | if (list_empty(&dev->inqueue)) |
| 1220 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1221 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1222 | dev->stream = STREAM_ON; /* FIXME: Start video capture here? */ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1223 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1224 | em28xx_videodbg("VIDIOC_STREAMON: starting stream\n"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1225 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1226 | return 0; |
| 1227 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1228 | case VIDIOC_STREAMOFF: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1229 | { |
| 1230 | int *type = arg; |
| 1231 | int ret; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1232 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1233 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1234 | || dev->io != IO_MMAP) |
| 1235 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1236 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1237 | if (dev->stream == STREAM_ON) { |
| 1238 | em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n"); |
| 1239 | if ((ret = em28xx_stream_interrupt(dev))) |
| 1240 | return ret; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1241 | } |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1242 | em28xx_empty_framequeues(dev); |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1246 | default: |
| 1247 | return v4l_compat_translate_ioctl(inode, filp, cmd, arg, |
| 1248 | driver_ioctl); |
| 1249 | } |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1254 | * em28xx_v4l2_do_ioctl() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1255 | * This function is _not_ called directly, but from |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1256 | * em28xx_v4l2_ioctl. Userspace |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1257 | * copying is done already, arg is a kernel pointer. |
| 1258 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1259 | static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1260 | unsigned int cmd, void *arg) |
| 1261 | { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1262 | struct em28xx *dev = filp->private_data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1263 | |
| 1264 | if (!dev) |
| 1265 | return -ENODEV; |
| 1266 | |
| 1267 | if (video_debug > 1) |
Michael Krufky | 5e453dc | 2006-01-09 15:32:31 -0200 | [diff] [blame] | 1268 | v4l_print_ioctl(dev->name,cmd); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1269 | |
| 1270 | switch (cmd) { |
| 1271 | |
| 1272 | /* --- capabilities ------------------------------------------ */ |
| 1273 | case VIDIOC_QUERYCAP: |
| 1274 | { |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1275 | struct v4l2_capability *cap = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1276 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1277 | memset(cap, 0, sizeof(*cap)); |
| 1278 | strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); |
| 1279 | strlcpy(cap->card, em28xx_boards[dev->model].name, |
| 1280 | sizeof(cap->card)); |
| 1281 | strlcpy(cap->bus_info, dev->udev->dev.bus_id, |
| 1282 | sizeof(cap->bus_info)); |
| 1283 | cap->version = EM28XX_VERSION_CODE; |
| 1284 | cap->capabilities = |
| 1285 | V4L2_CAP_SLICED_VBI_CAPTURE | |
| 1286 | V4L2_CAP_VIDEO_CAPTURE | |
| 1287 | V4L2_CAP_AUDIO | |
| 1288 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
| 1289 | if (dev->has_tuner) |
| 1290 | cap->capabilities |= V4L2_CAP_TUNER; |
| 1291 | return 0; |
| 1292 | } |
| 1293 | /* --- capture ioctls ---------------------------------------- */ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1294 | case VIDIOC_ENUM_FMT: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1295 | { |
| 1296 | struct v4l2_fmtdesc *fmtd = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1297 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1298 | if (fmtd->index != 0) |
| 1299 | return -EINVAL; |
| 1300 | memset(fmtd, 0, sizeof(*fmtd)); |
| 1301 | fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1302 | strcpy(fmtd->description, "Packed YUY2"); |
| 1303 | fmtd->pixelformat = V4L2_PIX_FMT_YUYV; |
| 1304 | memset(fmtd->reserved, 0, sizeof(fmtd->reserved)); |
| 1305 | return 0; |
| 1306 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1307 | case VIDIOC_G_FMT: |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1308 | return em28xx_get_fmt(dev, (struct v4l2_format *) arg); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1309 | |
| 1310 | case VIDIOC_TRY_FMT: |
| 1311 | case VIDIOC_S_FMT: |
Mauro Carvalho Chehab | 2d50f84 | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1312 | return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1313 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1314 | case VIDIOC_REQBUFS: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1315 | { |
| 1316 | struct v4l2_requestbuffers *rb = arg; |
| 1317 | u32 i; |
| 1318 | int ret; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1319 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1320 | if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1321 | rb->memory != V4L2_MEMORY_MMAP) |
| 1322 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1323 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1324 | if (dev->io == IO_READ) { |
| 1325 | em28xx_videodbg ("method is set to read;" |
| 1326 | " close and open the device again to" |
| 1327 | " choose the mmap I/O method\n"); |
| 1328 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1331 | for (i = 0; i < dev->num_frames; i++) |
| 1332 | if (dev->frame[i].vma_use_count) { |
| 1333 | em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n"); |
| 1334 | return -EINVAL; |
| 1335 | } |
| 1336 | |
| 1337 | if (dev->stream == STREAM_ON) { |
| 1338 | em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n"); |
| 1339 | if ((ret = em28xx_stream_interrupt(dev))) |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | em28xx_empty_framequeues(dev); |
| 1344 | |
| 1345 | em28xx_release_buffers(dev); |
| 1346 | if (rb->count) |
| 1347 | rb->count = |
| 1348 | em28xx_request_buffers(dev, rb->count); |
| 1349 | |
| 1350 | dev->frame_current = NULL; |
| 1351 | |
| 1352 | em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n", |
| 1353 | rb->count); |
| 1354 | dev->io = rb->count ? IO_MMAP : IO_NONE; |
| 1355 | return 0; |
| 1356 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1357 | case VIDIOC_QUERYBUF: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1358 | { |
| 1359 | struct v4l2_buffer *b = arg; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1360 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1361 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1362 | b->index >= dev->num_frames || dev->io != IO_MMAP) |
| 1363 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1364 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1365 | memcpy(b, &dev->frame[b->index].buf, sizeof(*b)); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1366 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1367 | if (dev->frame[b->index].vma_use_count) { |
| 1368 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1369 | } |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1370 | if (dev->frame[b->index].state == F_DONE) |
| 1371 | b->flags |= V4L2_BUF_FLAG_DONE; |
| 1372 | else if (dev->frame[b->index].state != F_UNUSED) |
| 1373 | b->flags |= V4L2_BUF_FLAG_QUEUED; |
| 1374 | return 0; |
| 1375 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1376 | case VIDIOC_QBUF: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1377 | { |
| 1378 | struct v4l2_buffer *b = arg; |
| 1379 | unsigned long lock_flags; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1380 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1381 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 1382 | b->index >= dev->num_frames || dev->io != IO_MMAP) { |
| 1383 | return -EINVAL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1384 | } |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1385 | |
| 1386 | if (dev->frame[b->index].state != F_UNUSED) { |
| 1387 | return -EAGAIN; |
| 1388 | } |
| 1389 | dev->frame[b->index].state = F_QUEUED; |
| 1390 | |
| 1391 | /* add frame to fifo */ |
| 1392 | spin_lock_irqsave(&dev->queue_lock, lock_flags); |
| 1393 | list_add_tail(&dev->frame[b->index].frame, |
| 1394 | &dev->inqueue); |
| 1395 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); |
| 1396 | |
| 1397 | return 0; |
| 1398 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1399 | case VIDIOC_DQBUF: |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1400 | { |
| 1401 | struct v4l2_buffer *b = arg; |
| 1402 | struct em28xx_frame_t *f; |
| 1403 | unsigned long lock_flags; |
| 1404 | int ret = 0; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1405 | |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1406 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1407 | || dev->io != IO_MMAP) |
| 1408 | return -EINVAL; |
| 1409 | |
| 1410 | if (list_empty(&dev->outqueue)) { |
| 1411 | if (dev->stream == STREAM_OFF) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1412 | return -EINVAL; |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1413 | if (filp->f_flags & O_NONBLOCK) |
| 1414 | return -EAGAIN; |
| 1415 | ret = wait_event_interruptible |
| 1416 | (dev->wait_frame, |
| 1417 | (!list_empty(&dev->outqueue)) || |
| 1418 | (dev->state & DEV_DISCONNECTED)); |
| 1419 | if (ret) |
| 1420 | return ret; |
| 1421 | if (dev->state & DEV_DISCONNECTED) |
| 1422 | return -ENODEV; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1423 | } |
Mauro Carvalho Chehab | 9aeb4b0 | 2006-01-23 17:11:09 -0200 | [diff] [blame] | 1424 | |
| 1425 | spin_lock_irqsave(&dev->queue_lock, lock_flags); |
| 1426 | f = list_entry(dev->outqueue.next, |
| 1427 | struct em28xx_frame_t, frame); |
| 1428 | list_del(dev->outqueue.next); |
| 1429 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); |
| 1430 | |
| 1431 | f->state = F_UNUSED; |
| 1432 | memcpy(b, &f->buf, sizeof(*b)); |
| 1433 | |
| 1434 | if (f->vma_use_count) |
| 1435 | b->flags |= V4L2_BUF_FLAG_MAPPED; |
| 1436 | |
| 1437 | return 0; |
| 1438 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1439 | default: |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1440 | return em28xx_do_ioctl(inode, filp, dev, cmd, arg, |
| 1441 | em28xx_video_do_ioctl); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1442 | } |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
| 1446 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1447 | * em28xx_v4l2_ioctl() |
| 1448 | * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1449 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1450 | static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1451 | unsigned int cmd, unsigned long arg) |
| 1452 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1453 | int ret = 0; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1454 | struct em28xx *dev = filp->private_data; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1455 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1456 | if (mutex_lock_interruptible(&dev->fileop_lock)) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1457 | return -ERESTARTSYS; |
| 1458 | |
| 1459 | if (dev->state & DEV_DISCONNECTED) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1460 | em28xx_errdev("v4l2 ioctl: device not present\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1461 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1462 | return -ENODEV; |
| 1463 | } |
| 1464 | |
| 1465 | if (dev->state & DEV_MISCONFIGURED) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1466 | em28xx_errdev |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1467 | ("v4l2 ioctl: device is misconfigured; close and open it again\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1468 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1469 | return -EIO; |
| 1470 | } |
| 1471 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1472 | ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1473 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1474 | mutex_unlock(&dev->fileop_lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1475 | |
| 1476 | return ret; |
| 1477 | } |
| 1478 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1479 | static struct file_operations em28xx_v4l_fops = { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1480 | .owner = THIS_MODULE, |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1481 | .open = em28xx_v4l2_open, |
| 1482 | .release = em28xx_v4l2_close, |
| 1483 | .ioctl = em28xx_v4l2_ioctl, |
| 1484 | .read = em28xx_v4l2_read, |
| 1485 | .poll = em28xx_v4l2_poll, |
| 1486 | .mmap = em28xx_v4l2_mmap, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1487 | .llseek = no_llseek, |
Mauro Carvalho Chehab | 17cbe2e | 2006-01-09 15:24:58 -0200 | [diff] [blame] | 1488 | .compat_ioctl = v4l_compat_ioctl32, |
| 1489 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1490 | }; |
| 1491 | |
| 1492 | /******************************** usb interface *****************************************/ |
| 1493 | |
| 1494 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1495 | * em28xx_init_dev() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1496 | * allocates and inits the device structs, registers i2c bus and v4l device |
| 1497 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1498 | static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1499 | int minor, int model) |
| 1500 | { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1501 | struct em28xx *dev = *devhandle; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1502 | int retval = -ENOMEM; |
| 1503 | int errCode, i; |
| 1504 | unsigned int maxh, maxw; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1505 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1506 | dev->udev = udev; |
| 1507 | dev->model = model; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1508 | mutex_init(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1509 | init_waitqueue_head(&dev->open); |
| 1510 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1511 | dev->em28xx_write_regs = em28xx_write_regs; |
| 1512 | dev->em28xx_read_reg = em28xx_read_reg; |
| 1513 | dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len; |
| 1514 | dev->em28xx_write_regs_req = em28xx_write_regs_req; |
| 1515 | dev->em28xx_read_reg_req = em28xx_read_reg_req; |
| 1516 | dev->is_em2800 = em28xx_boards[model].is_em2800; |
| 1517 | dev->has_tuner = em28xx_boards[model].has_tuner; |
| 1518 | dev->has_msp34xx = em28xx_boards[model].has_msp34xx; |
| 1519 | dev->tda9887_conf = em28xx_boards[model].tda9887_conf; |
| 1520 | dev->decoder = em28xx_boards[model].decoder; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1521 | |
| 1522 | if (tuner >= 0) |
| 1523 | dev->tuner_type = tuner; |
| 1524 | else |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1525 | dev->tuner_type = em28xx_boards[model].tuner_type; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1526 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1527 | dev->video_inputs = em28xx_boards[model].vchannels; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1528 | |
| 1529 | for (i = 0; i < TVNORMS; i++) |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1530 | if (em28xx_boards[model].norm == tvnorms[i].mode) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1531 | break; |
| 1532 | if (i == TVNORMS) |
| 1533 | i = 0; |
| 1534 | |
| 1535 | dev->tvnorm = &tvnorms[i]; /* set default norm */ |
| 1536 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1537 | em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1538 | |
| 1539 | maxw = norm_maxw(dev); |
| 1540 | maxh = norm_maxh(dev); |
| 1541 | |
| 1542 | /* set default image size */ |
| 1543 | dev->width = maxw; |
| 1544 | dev->height = maxh; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1545 | dev->interlaced = EM28XX_INTERLACED_DEFAULT; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1546 | dev->field_size = dev->width * dev->height; |
| 1547 | dev->frame_size = |
| 1548 | dev->interlaced ? dev->field_size << 1 : dev->field_size; |
| 1549 | dev->bytesperline = dev->width * 2; |
| 1550 | dev->hscale = 0; |
| 1551 | dev->vscale = 0; |
| 1552 | dev->ctl_input = 2; |
| 1553 | |
| 1554 | /* setup video picture settings for saa7113h */ |
| 1555 | memset(&dev->vpic, 0, sizeof(dev->vpic)); |
| 1556 | dev->vpic.colour = 128 << 8; |
| 1557 | dev->vpic.hue = 128 << 8; |
| 1558 | dev->vpic.brightness = 128 << 8; |
| 1559 | dev->vpic.contrast = 192 << 8; |
| 1560 | dev->vpic.whiteness = 128 << 8; /* This one isn't used */ |
| 1561 | dev->vpic.depth = 16; |
| 1562 | dev->vpic.palette = VIDEO_PALETTE_YUV422; |
| 1563 | |
Markus Rechberger | a94e95b | 2006-01-23 17:11:10 -0200 | [diff] [blame] | 1564 | em28xx_pre_card_setup(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1565 | #ifdef CONFIG_MODULES |
| 1566 | /* request some modules */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1567 | if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114) |
Mauro Carvalho Chehab | f5762e4 | 2006-03-13 13:31:31 -0300 | [diff] [blame] | 1568 | request_module("saa7115"); |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1569 | if (dev->decoder == EM28XX_TVP5150) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1570 | request_module("tvp5150"); |
| 1571 | if (dev->has_tuner) |
| 1572 | request_module("tuner"); |
| 1573 | if (dev->tda9887_conf) |
| 1574 | request_module("tda9887"); |
| 1575 | #endif |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1576 | errCode = em28xx_config(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1577 | if (errCode) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1578 | em28xx_errdev("error configuring device\n"); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1579 | em28xx_devused&=~(1<<dev->devno); |
Eric Sesterhenn | 7c908fb | 2006-04-11 18:19:33 -0300 | [diff] [blame] | 1580 | kfree(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1581 | return -ENOMEM; |
| 1582 | } |
| 1583 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1584 | mutex_lock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1585 | /* register i2c bus */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1586 | em28xx_i2c_register(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1587 | |
| 1588 | /* Do board specific init and eeprom reading */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1589 | em28xx_card_setup(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1590 | |
| 1591 | /* configure the device */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1592 | em28xx_config_i2c(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1593 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1594 | mutex_unlock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1595 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1596 | errCode = em28xx_config(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1597 | |
| 1598 | #ifdef CONFIG_MODULES |
| 1599 | if (dev->has_msp34xx) |
| 1600 | request_module("msp3400"); |
| 1601 | #endif |
| 1602 | /* allocate and fill v4l2 device struct */ |
| 1603 | dev->vdev = video_device_alloc(); |
| 1604 | if (NULL == dev->vdev) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1605 | em28xx_errdev("cannot allocate video_device.\n"); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1606 | em28xx_devused&=~(1<<dev->devno); |
Eric Sesterhenn | 7c908fb | 2006-04-11 18:19:33 -0300 | [diff] [blame] | 1607 | kfree(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1608 | return -ENOMEM; |
| 1609 | } |
| 1610 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1611 | dev->vbi_dev = video_device_alloc(); |
| 1612 | if (NULL == dev->vbi_dev) { |
| 1613 | em28xx_errdev("cannot allocate video_device.\n"); |
| 1614 | kfree(dev->vdev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1615 | em28xx_devused&=~(1<<dev->devno); |
Eric Sesterhenn | 7c908fb | 2006-04-11 18:19:33 -0300 | [diff] [blame] | 1616 | kfree(dev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1617 | return -ENOMEM; |
| 1618 | } |
| 1619 | |
| 1620 | /* Fills VBI device info */ |
| 1621 | dev->vbi_dev->type = VFL_TYPE_VBI; |
| 1622 | dev->vbi_dev->hardware = 0; |
| 1623 | dev->vbi_dev->fops = &em28xx_v4l_fops; |
| 1624 | dev->vbi_dev->minor = -1; |
| 1625 | dev->vbi_dev->dev = &dev->udev->dev; |
| 1626 | dev->vbi_dev->release = video_device_release; |
| 1627 | snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s", |
| 1628 | "em28xx",dev->devno,"vbi"); |
| 1629 | |
| 1630 | /* Fills CAPTURE device info */ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1631 | dev->vdev->type = VID_TYPE_CAPTURE; |
| 1632 | if (dev->has_tuner) |
| 1633 | dev->vdev->type |= VID_TYPE_TUNER; |
| 1634 | dev->vdev->hardware = 0; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1635 | dev->vdev->fops = &em28xx_v4l_fops; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1636 | dev->vdev->minor = -1; |
| 1637 | dev->vdev->dev = &dev->udev->dev; |
| 1638 | dev->vdev->release = video_device_release; |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1639 | snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s", |
| 1640 | "em28xx",dev->devno,"video"); |
| 1641 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1642 | list_add_tail(&dev->devlist,&em28xx_devlist); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1643 | |
| 1644 | /* register v4l2 device */ |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1645 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1646 | if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, |
| 1647 | video_nr[dev->devno]))) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1648 | em28xx_errdev("unable to register video device (error=%i).\n", |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1649 | retval); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1650 | mutex_unlock(&dev->lock); |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 1651 | list_del(&dev->devlist); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1652 | video_device_release(dev->vdev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1653 | em28xx_devused&=~(1<<dev->devno); |
Eric Sesterhenn | 7c908fb | 2006-04-11 18:19:33 -0300 | [diff] [blame] | 1654 | kfree(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1655 | return -ENODEV; |
| 1656 | } |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1657 | |
| 1658 | if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI, |
| 1659 | vbi_nr[dev->devno]) < 0) { |
| 1660 | printk("unable to register vbi device\n"); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1661 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1662 | list_del(&dev->devlist); |
| 1663 | video_device_release(dev->vbi_dev); |
| 1664 | video_device_release(dev->vdev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1665 | em28xx_devused&=~(1<<dev->devno); |
Eric Sesterhenn | 7c908fb | 2006-04-11 18:19:33 -0300 | [diff] [blame] | 1666 | kfree(dev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1667 | return -ENODEV; |
| 1668 | } else { |
| 1669 | printk("registered VBI\n"); |
| 1670 | } |
| 1671 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1672 | if (dev->has_msp34xx) { |
| 1673 | /* Send a reset to other chips via gpio */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1674 | em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1675 | udelay(2500); |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1676 | em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1677 | udelay(2500); |
| 1678 | |
| 1679 | } |
| 1680 | video_mux(dev, 0); |
| 1681 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1682 | mutex_unlock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1683 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1684 | em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n", |
| 1685 | dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN, |
| 1686 | dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1687 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1692 | * em28xx_usb_probe() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1693 | * checks for supported devices |
| 1694 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1695 | static int em28xx_usb_probe(struct usb_interface *interface, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1696 | const struct usb_device_id *id) |
| 1697 | { |
| 1698 | const struct usb_endpoint_descriptor *endpoint; |
| 1699 | struct usb_device *udev; |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1700 | struct usb_interface *uif; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1701 | struct em28xx *dev = NULL; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1702 | int retval = -ENODEV; |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1703 | int model,i,nr,ifnum; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1704 | |
| 1705 | udev = usb_get_dev(interface_to_usbdev(interface)); |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1706 | ifnum = interface->altsetting[0].desc.bInterfaceNumber; |
| 1707 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1708 | /* Check to see next free device and mark as used */ |
| 1709 | nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS); |
| 1710 | em28xx_devused|=1<<nr; |
Mauro Carvalho Chehab | 91cad0f | 2005-11-08 21:38:13 -0800 | [diff] [blame] | 1711 | |
| 1712 | /* Don't register audio interfaces */ |
| 1713 | if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1714 | em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n", |
Mauro Carvalho Chehab | 91cad0f | 2005-11-08 21:38:13 -0800 | [diff] [blame] | 1715 | udev->descriptor.idVendor,udev->descriptor.idProduct, |
| 1716 | ifnum, |
| 1717 | interface->altsetting[0].desc.bInterfaceClass); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1718 | |
| 1719 | em28xx_devused&=~(1<<nr); |
Mauro Carvalho Chehab | 91cad0f | 2005-11-08 21:38:13 -0800 | [diff] [blame] | 1720 | return -ENODEV; |
| 1721 | } |
| 1722 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1723 | em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n", |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1724 | udev->descriptor.idVendor,udev->descriptor.idProduct, |
| 1725 | ifnum, |
| 1726 | interface->altsetting[0].desc.bInterfaceClass); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1727 | |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1728 | endpoint = &interface->cur_altsetting->endpoint[1].desc; |
| 1729 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1730 | /* check if the the device has the iso in endpoint at the correct place */ |
| 1731 | if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != |
| 1732 | USB_ENDPOINT_XFER_ISOC) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1733 | em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1734 | em28xx_devused&=~(1<<nr); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1735 | return -ENODEV; |
| 1736 | } |
| 1737 | if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1738 | em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1739 | em28xx_devused&=~(1<<nr); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1740 | return -ENODEV; |
| 1741 | } |
| 1742 | |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1743 | model=id->driver_info; |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1744 | |
Mauro Carvalho Chehab | 1947884 | 2006-03-14 17:24:57 -0300 | [diff] [blame] | 1745 | if (nr >= EM28XX_MAXBOARDS) { |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1746 | printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1747 | em28xx_devused&=~(1<<nr); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1748 | return -ENOMEM; |
| 1749 | } |
| 1750 | |
| 1751 | /* allocate memory for our device state and initialize it */ |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 1752 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1753 | if (dev == NULL) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1754 | em28xx_err(DRIVER_NAME ": out of memory!\n"); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1755 | em28xx_devused&=~(1<<nr); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1756 | return -ENOMEM; |
| 1757 | } |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1758 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1759 | snprintf(dev->name, 29, "em28xx #%d", nr); |
| 1760 | dev->devno=nr; |
| 1761 | |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1762 | /* compute alternate max packet sizes */ |
| 1763 | uif = udev->actconfig->interface[0]; |
| 1764 | |
| 1765 | dev->num_alt=uif->num_altsetting; |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1766 | em28xx_info("Alternate settings: %i\n",dev->num_alt); |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1767 | // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* |
| 1768 | dev->alt_max_pkt_size = kmalloc(32* |
| 1769 | dev->num_alt,GFP_KERNEL); |
| 1770 | if (dev->alt_max_pkt_size == NULL) { |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1771 | em28xx_errdev("out of memory!\n"); |
| 1772 | em28xx_devused&=~(1<<nr); |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1773 | return -ENOMEM; |
| 1774 | } |
| 1775 | |
| 1776 | for (i = 0; i < dev->num_alt ; i++) { |
| 1777 | u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc. |
| 1778 | wMaxPacketSize); |
| 1779 | dev->alt_max_pkt_size[i] = |
| 1780 | (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1781 | em28xx_info("Alternate setting %i, max size= %i\n",i, |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1782 | dev->alt_max_pkt_size[i]); |
| 1783 | } |
| 1784 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1785 | if ((card[nr]>=0)&&(card[nr]<em28xx_bcount)) |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1786 | model=card[nr]; |
| 1787 | |
| 1788 | if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) { |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1789 | em28xx_errdev( "Your board has no eeprom inside it and thus can't\n" |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1790 | "%s: be autodetected. Please pass card=<n> insmod option to\n" |
| 1791 | "%s: workaround that. Redirect complaints to the vendor of\n" |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1792 | "%s: the TV card. Generic type will be used." |
| 1793 | "%s: Best regards,\n" |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1794 | "%s: -- tux\n", |
| 1795 | dev->name,dev->name,dev->name,dev->name,dev->name); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1796 | em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n", |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1797 | dev->name); |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1798 | for (i = 0; i < em28xx_bcount; i++) { |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1799 | em28xx_errdev(" card=%d -> %s\n", i, |
| 1800 | em28xx_boards[i].name); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1804 | /* allocate device struct */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1805 | retval = em28xx_init_dev(&dev, udev, nr, model); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1806 | if (retval) |
| 1807 | return retval; |
| 1808 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1809 | em28xx_info("Found %s\n", em28xx_boards[model].name); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1810 | |
| 1811 | /* save our data pointer in this interface device */ |
| 1812 | usb_set_intfdata(interface, dev); |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1817 | * em28xx_usb_disconnect() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1818 | * called when the device gets diconencted |
| 1819 | * video device will be unregistered on v4l2_close in case it is still open |
| 1820 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1821 | static void em28xx_usb_disconnect(struct usb_interface *interface) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1822 | { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1823 | struct em28xx *dev = usb_get_intfdata(interface); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1824 | usb_set_intfdata(interface, NULL); |
| 1825 | |
| 1826 | if (!dev) |
| 1827 | return; |
| 1828 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1829 | down_write(&em28xx_disconnect); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1830 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1831 | mutex_lock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1832 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1833 | em28xx_info("disconnecting %s\n", dev->vdev->name); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1834 | |
| 1835 | wake_up_interruptible_all(&dev->open); |
| 1836 | |
| 1837 | if (dev->users) { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1838 | em28xx_warn |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1839 | ("device /dev/video%d is open! Deregistration and memory " |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1840 | "deallocation are deferred on close.\n", |
| 1841 | dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN); |
| 1842 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1843 | dev->state |= DEV_MISCONFIGURED; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1844 | em28xx_uninit_isoc(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1845 | dev->state |= DEV_DISCONNECTED; |
| 1846 | wake_up_interruptible(&dev->wait_frame); |
| 1847 | wake_up_interruptible(&dev->wait_stream); |
| 1848 | } else { |
| 1849 | dev->state |= DEV_DISCONNECTED; |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1850 | em28xx_release_resources(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1851 | } |
| 1852 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1853 | mutex_unlock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1854 | |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1855 | if (!dev->users) { |
| 1856 | kfree(dev->alt_max_pkt_size); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1857 | kfree(dev); |
Mauro Carvalho Chehab | 9d4d9c0 | 2005-11-08 21:38:52 -0800 | [diff] [blame] | 1858 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1859 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1860 | up_write(&em28xx_disconnect); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1863 | static struct usb_driver em28xx_usb_driver = { |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1864 | .name = "em28xx", |
| 1865 | .probe = em28xx_usb_probe, |
| 1866 | .disconnect = em28xx_usb_disconnect, |
| 1867 | .id_table = em28xx_id_table, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1868 | }; |
| 1869 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1870 | static int __init em28xx_module_init(void) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1871 | { |
| 1872 | int result; |
| 1873 | |
| 1874 | printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n", |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1875 | (EM28XX_VERSION_CODE >> 16) & 0xff, |
| 1876 | (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1877 | #ifdef SNAPSHOT |
| 1878 | printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n", |
| 1879 | SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100); |
| 1880 | #endif |
| 1881 | |
| 1882 | /* register this driver with the USB subsystem */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1883 | result = usb_register(&em28xx_usb_driver); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1884 | if (result) |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1885 | em28xx_err(DRIVER_NAME |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1886 | " usb_register failed. Error number %d.\n", result); |
| 1887 | |
| 1888 | return result; |
| 1889 | } |
| 1890 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1891 | static void __exit em28xx_module_exit(void) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1892 | { |
| 1893 | /* deregister this driver with the USB subsystem */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1894 | usb_deregister(&em28xx_usb_driver); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1897 | module_init(em28xx_module_init); |
| 1898 | module_exit(em28xx_module_exit); |