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