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