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