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