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