Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * Support for a cx23416 mpeg encoder via cx2388x host port. |
| 4 | * "blackbird" reference design. |
| 5 | * |
| 6 | * (c) 2004 Jelle Foks <jelle@foks.8m.com> |
| 7 | * (c) 2004 Gerd Knorr <kraxel@bytesex.org> |
| 8 | * |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 9 | * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 10 | * - video_ioctl2 conversion |
| 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/), |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/moduleparam.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/firmware.h> |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 36 | #include <media/v4l2-common.h> |
| 37 | #include <media/cx2341x.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include "cx88.h" |
| 40 | |
| 41 | MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards"); |
Mauro Carvalho Chehab | d21838d | 2006-01-09 15:25:21 -0200 | [diff] [blame] | 42 | MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | MODULE_LICENSE("GPL"); |
| 44 | |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 45 | static unsigned int mpegbufs = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | module_param(mpegbufs,int,0644); |
| 47 | MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32"); |
| 48 | |
| 49 | static unsigned int debug = 0; |
| 50 | module_param(debug,int,0644); |
| 51 | MODULE_PARM_DESC(debug,"enable debug messages [blackbird]"); |
| 52 | |
| 53 | #define dprintk(level,fmt, arg...) if (debug >= level) \ |
| 54 | printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg) |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* ------------------------------------------------------------------ */ |
| 58 | |
Hans Verkuil | 2547223 | 2007-05-30 09:39:46 -0300 | [diff] [blame^] | 59 | #define BLACKBIRD_FIRM_IMAGE_SIZE 376836 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* defines below are from ivtv-driver.h */ |
| 62 | |
| 63 | #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF |
| 64 | |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 65 | /* Firmware API commands */ |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 66 | #define IVTV_API_STD_TIMEOUT 500 |
| 67 | |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 68 | enum blackbird_capture_type { |
| 69 | BLACKBIRD_MPEG_CAPTURE, |
| 70 | BLACKBIRD_RAW_CAPTURE, |
| 71 | BLACKBIRD_RAW_PASSTHRU_CAPTURE |
| 72 | }; |
| 73 | enum blackbird_capture_bits { |
| 74 | BLACKBIRD_RAW_BITS_NONE = 0x00, |
| 75 | BLACKBIRD_RAW_BITS_YUV_CAPTURE = 0x01, |
| 76 | BLACKBIRD_RAW_BITS_PCM_CAPTURE = 0x02, |
| 77 | BLACKBIRD_RAW_BITS_VBI_CAPTURE = 0x04, |
| 78 | BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08, |
| 79 | BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE = 0x10 |
| 80 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 81 | enum blackbird_capture_end { |
| 82 | BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */ |
| 83 | BLACKBIRD_END_NOW, /* stop immediately, no irq */ |
| 84 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 85 | enum blackbird_framerate { |
| 86 | BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */ |
| 87 | BLACKBIRD_FRAMERATE_PAL_25 /* PAL: 25fps */ |
| 88 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 89 | enum blackbird_stream_port { |
| 90 | BLACKBIRD_OUTPUT_PORT_MEMORY, |
| 91 | BLACKBIRD_OUTPUT_PORT_STREAMING, |
| 92 | BLACKBIRD_OUTPUT_PORT_SERIAL |
| 93 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 94 | enum blackbird_data_xfer_status { |
| 95 | BLACKBIRD_MORE_BUFFERS_FOLLOW, |
| 96 | BLACKBIRD_LAST_BUFFER, |
| 97 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 98 | enum blackbird_picture_mask { |
| 99 | BLACKBIRD_PICTURE_MASK_NONE, |
| 100 | BLACKBIRD_PICTURE_MASK_I_FRAMES, |
| 101 | BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3, |
| 102 | BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7, |
| 103 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 104 | enum blackbird_vbi_mode_bits { |
| 105 | BLACKBIRD_VBI_BITS_SLICED, |
| 106 | BLACKBIRD_VBI_BITS_RAW, |
| 107 | }; |
| 108 | enum blackbird_vbi_insertion_bits { |
| 109 | BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA, |
| 110 | BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1, |
| 111 | BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1, |
| 112 | BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1, |
| 113 | BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1, |
| 114 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 115 | enum blackbird_dma_unit { |
| 116 | BLACKBIRD_DMA_BYTES, |
| 117 | BLACKBIRD_DMA_FRAMES, |
| 118 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 119 | enum blackbird_dma_transfer_status_bits { |
| 120 | BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01, |
| 121 | BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04, |
| 122 | BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10, |
| 123 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 124 | enum blackbird_pause { |
| 125 | BLACKBIRD_PAUSE_ENCODING, |
| 126 | BLACKBIRD_RESUME_ENCODING, |
| 127 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 128 | enum blackbird_copyright { |
| 129 | BLACKBIRD_COPYRIGHT_OFF, |
| 130 | BLACKBIRD_COPYRIGHT_ON, |
| 131 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 132 | enum blackbird_notification_type { |
| 133 | BLACKBIRD_NOTIFICATION_REFRESH, |
| 134 | }; |
| 135 | enum blackbird_notification_status { |
| 136 | BLACKBIRD_NOTIFICATION_OFF, |
| 137 | BLACKBIRD_NOTIFICATION_ON, |
| 138 | }; |
| 139 | enum blackbird_notification_mailbox { |
| 140 | BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1, |
| 141 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 142 | enum blackbird_field1_lines { |
| 143 | BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */ |
| 144 | BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */ |
| 145 | BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */ |
| 146 | }; |
| 147 | enum blackbird_field2_lines { |
| 148 | BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */ |
| 149 | BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */ |
| 150 | BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */ |
| 151 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 152 | enum blackbird_custom_data_type { |
| 153 | BLACKBIRD_CUSTOM_EXTENSION_USR_DATA, |
| 154 | BLACKBIRD_CUSTOM_PRIVATE_PACKET, |
| 155 | }; |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 156 | enum blackbird_mute { |
| 157 | BLACKBIRD_UNMUTE, |
| 158 | BLACKBIRD_MUTE, |
| 159 | }; |
| 160 | enum blackbird_mute_video_mask { |
| 161 | BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00, |
| 162 | BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000, |
| 163 | BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000, |
| 164 | }; |
| 165 | enum blackbird_mute_video_shift { |
| 166 | BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8, |
| 167 | BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16, |
| 168 | BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24, |
| 169 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | /* Registers */ |
| 172 | #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/) |
| 173 | #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/) |
| 174 | #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/) |
| 175 | #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/) |
| 176 | #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/) |
| 177 | #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/) |
| 178 | |
| 179 | /* ------------------------------------------------------------------ */ |
| 180 | |
| 181 | static void host_setup(struct cx88_core *core) |
| 182 | { |
| 183 | /* toggle reset of the host */ |
| 184 | cx_write(MO_GPHST_SOFT_RST, 1); |
| 185 | udelay(100); |
| 186 | cx_write(MO_GPHST_SOFT_RST, 0); |
| 187 | udelay(100); |
| 188 | |
| 189 | /* host port setup */ |
| 190 | cx_write(MO_GPHST_WSC, 0x44444444U); |
| 191 | cx_write(MO_GPHST_XFR, 0); |
| 192 | cx_write(MO_GPHST_WDTH, 15); |
| 193 | cx_write(MO_GPHST_HDSHK, 0); |
| 194 | cx_write(MO_GPHST_MUX16, 0x44448888U); |
| 195 | cx_write(MO_GPHST_MODE, 0); |
| 196 | } |
| 197 | |
| 198 | /* ------------------------------------------------------------------ */ |
| 199 | |
| 200 | #define P1_MDATA0 0x390000 |
| 201 | #define P1_MDATA1 0x390001 |
| 202 | #define P1_MDATA2 0x390002 |
| 203 | #define P1_MDATA3 0x390003 |
| 204 | #define P1_MADDR2 0x390004 |
| 205 | #define P1_MADDR1 0x390005 |
| 206 | #define P1_MADDR0 0x390006 |
| 207 | #define P1_RDATA0 0x390008 |
| 208 | #define P1_RDATA1 0x390009 |
| 209 | #define P1_RDATA2 0x39000A |
| 210 | #define P1_RDATA3 0x39000B |
| 211 | #define P1_RADDR0 0x39000C |
| 212 | #define P1_RADDR1 0x39000D |
| 213 | #define P1_RRDWR 0x39000E |
| 214 | |
| 215 | static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state) |
| 216 | { |
| 217 | unsigned long timeout = jiffies + msecs_to_jiffies(1); |
| 218 | u32 gpio0,need; |
| 219 | |
| 220 | need = state ? 2 : 0; |
| 221 | for (;;) { |
| 222 | gpio0 = cx_read(MO_GP0_IO) & 2; |
| 223 | if (need == gpio0) |
| 224 | return 0; |
| 225 | if (time_after(jiffies,timeout)) |
| 226 | return -1; |
| 227 | udelay(1); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | static int memory_write(struct cx88_core *core, u32 address, u32 value) |
| 232 | { |
| 233 | /* Warning: address is dword address (4 bytes) */ |
| 234 | cx_writeb(P1_MDATA0, (unsigned int)value); |
| 235 | cx_writeb(P1_MDATA1, (unsigned int)(value >> 8)); |
| 236 | cx_writeb(P1_MDATA2, (unsigned int)(value >> 16)); |
| 237 | cx_writeb(P1_MDATA3, (unsigned int)(value >> 24)); |
| 238 | cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40); |
| 239 | cx_writeb(P1_MADDR1, (unsigned int)(address >> 8)); |
| 240 | cx_writeb(P1_MADDR0, (unsigned int)address); |
| 241 | cx_read(P1_MDATA0); |
| 242 | cx_read(P1_MADDR0); |
| 243 | |
| 244 | return wait_ready_gpio0_bit1(core,1); |
| 245 | } |
| 246 | |
| 247 | static int memory_read(struct cx88_core *core, u32 address, u32 *value) |
| 248 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 249 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | u32 val; |
| 251 | |
| 252 | /* Warning: address is dword address (4 bytes) */ |
| 253 | cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0); |
| 254 | cx_writeb(P1_MADDR1, (unsigned int)(address >> 8)); |
| 255 | cx_writeb(P1_MADDR0, (unsigned int)address); |
| 256 | cx_read(P1_MADDR0); |
| 257 | |
| 258 | retval = wait_ready_gpio0_bit1(core,1); |
| 259 | |
| 260 | cx_writeb(P1_MDATA3, 0); |
| 261 | val = (unsigned char)cx_read(P1_MDATA3) << 24; |
| 262 | cx_writeb(P1_MDATA2, 0); |
| 263 | val |= (unsigned char)cx_read(P1_MDATA2) << 16; |
| 264 | cx_writeb(P1_MDATA1, 0); |
| 265 | val |= (unsigned char)cx_read(P1_MDATA1) << 8; |
| 266 | cx_writeb(P1_MDATA0, 0); |
| 267 | val |= (unsigned char)cx_read(P1_MDATA0); |
| 268 | |
| 269 | *value = val; |
| 270 | return retval; |
| 271 | } |
| 272 | |
| 273 | static int register_write(struct cx88_core *core, u32 address, u32 value) |
| 274 | { |
| 275 | cx_writeb(P1_RDATA0, (unsigned int)value); |
| 276 | cx_writeb(P1_RDATA1, (unsigned int)(value >> 8)); |
| 277 | cx_writeb(P1_RDATA2, (unsigned int)(value >> 16)); |
| 278 | cx_writeb(P1_RDATA3, (unsigned int)(value >> 24)); |
| 279 | cx_writeb(P1_RADDR0, (unsigned int)address); |
| 280 | cx_writeb(P1_RADDR1, (unsigned int)(address >> 8)); |
| 281 | cx_writeb(P1_RRDWR, 1); |
| 282 | cx_read(P1_RDATA0); |
| 283 | cx_read(P1_RADDR0); |
| 284 | |
| 285 | return wait_ready_gpio0_bit1(core,1); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | static int register_read(struct cx88_core *core, u32 address, u32 *value) |
| 290 | { |
| 291 | int retval; |
| 292 | u32 val; |
| 293 | |
| 294 | cx_writeb(P1_RADDR0, (unsigned int)address); |
| 295 | cx_writeb(P1_RADDR1, (unsigned int)(address >> 8)); |
| 296 | cx_writeb(P1_RRDWR, 0); |
| 297 | cx_read(P1_RADDR0); |
| 298 | |
| 299 | retval = wait_ready_gpio0_bit1(core,1); |
| 300 | val = (unsigned char)cx_read(P1_RDATA0); |
| 301 | val |= (unsigned char)cx_read(P1_RDATA1) << 8; |
| 302 | val |= (unsigned char)cx_read(P1_RDATA2) << 16; |
| 303 | val |= (unsigned char)cx_read(P1_RDATA3) << 24; |
| 304 | |
| 305 | *value = val; |
| 306 | return retval; |
| 307 | } |
| 308 | |
| 309 | /* ------------------------------------------------------------------ */ |
| 310 | |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 311 | static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 313 | struct cx8802_dev *dev = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | unsigned long timeout; |
| 315 | u32 value, flag, retval; |
| 316 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | dprintk(1,"%s: 0x%X\n", __FUNCTION__, command); |
| 319 | |
| 320 | /* this may not be 100% safe if we can't read any memory location |
| 321 | without side effects */ |
| 322 | memory_read(dev->core, dev->mailbox - 4, &value); |
| 323 | if (value != 0x12345678) { |
| 324 | dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n"); |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | memory_read(dev->core, dev->mailbox, &flag); |
| 329 | if (flag) { |
| 330 | dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | flag |= 1; /* tell 'em we're working on it */ |
| 335 | memory_write(dev->core, dev->mailbox, flag); |
| 336 | |
| 337 | /* write command + args + fill remaining with zeros */ |
| 338 | memory_write(dev->core, dev->mailbox + 1, command); /* command code */ |
| 339 | memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */ |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 340 | for (i = 0; i < in; i++) { |
| 341 | memory_write(dev->core, dev->mailbox + 4 + i, data[i]); |
| 342 | dprintk(1, "API Input %d = %d\n", i, data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 344 | for (; i < CX2341X_MBOX_MAX_DATA; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | memory_write(dev->core, dev->mailbox + 4 + i, 0); |
| 346 | |
| 347 | flag |= 3; /* tell 'em we're done writing */ |
| 348 | memory_write(dev->core, dev->mailbox, flag); |
| 349 | |
| 350 | /* wait for firmware to handle the API command */ |
| 351 | timeout = jiffies + msecs_to_jiffies(10); |
| 352 | for (;;) { |
| 353 | memory_read(dev->core, dev->mailbox, &flag); |
| 354 | if (0 != (flag & 4)) |
| 355 | break; |
| 356 | if (time_after(jiffies,timeout)) { |
| 357 | dprintk(0, "ERROR: API Mailbox timeout\n"); |
| 358 | return -1; |
| 359 | } |
| 360 | udelay(10); |
| 361 | } |
| 362 | |
| 363 | /* read output values */ |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 364 | for (i = 0; i < out; i++) { |
| 365 | memory_read(dev->core, dev->mailbox + 4 + i, data + i); |
| 366 | dprintk(1, "API Output %d = %d\n", i, data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | memory_read(dev->core, dev->mailbox + 2, &retval); |
| 370 | dprintk(1, "API result = %d\n",retval); |
| 371 | |
| 372 | flag = 0; |
| 373 | memory_write(dev->core, dev->mailbox, flag); |
| 374 | return retval; |
| 375 | } |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 376 | /* ------------------------------------------------------------------ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 378 | /* We don't need to call the API often, so using just one mailbox will probably suffice */ |
| 379 | static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command, |
| 380 | u32 inputcnt, u32 outputcnt, ...) |
| 381 | { |
| 382 | u32 data[CX2341X_MBOX_MAX_DATA]; |
| 383 | va_list vargs; |
| 384 | int i, err; |
| 385 | |
| 386 | va_start(vargs, outputcnt); |
| 387 | |
| 388 | for (i = 0; i < inputcnt; i++) { |
| 389 | data[i] = va_arg(vargs, int); |
| 390 | } |
| 391 | err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data); |
| 392 | for (i = 0; i < outputcnt; i++) { |
| 393 | int *vptr = va_arg(vargs, int *); |
| 394 | *vptr = data[i]; |
| 395 | } |
| 396 | va_end(vargs); |
| 397 | return err; |
| 398 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | static int blackbird_find_mailbox(struct cx8802_dev *dev) |
| 401 | { |
| 402 | u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456}; |
| 403 | int signaturecnt=0; |
| 404 | u32 value; |
| 405 | int i; |
| 406 | |
Michael Krufky | af70dbd | 2007-03-03 10:10:43 -0200 | [diff] [blame] | 407 | for (i = 0; i < dev->fw_size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | memory_read(dev->core, i, &value); |
| 409 | if (value == signature[signaturecnt]) |
| 410 | signaturecnt++; |
| 411 | else |
| 412 | signaturecnt = 0; |
| 413 | if (4 == signaturecnt) { |
| 414 | dprintk(1, "Mailbox signature found\n"); |
| 415 | return i+1; |
| 416 | } |
| 417 | } |
| 418 | dprintk(0, "Mailbox signature values not found!\n"); |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | static int blackbird_load_firmware(struct cx8802_dev *dev) |
| 423 | { |
| 424 | static const unsigned char magic[8] = { |
| 425 | 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa |
| 426 | }; |
| 427 | const struct firmware *firmware; |
| 428 | int i, retval = 0; |
| 429 | u32 value = 0; |
| 430 | u32 checksum = 0; |
| 431 | u32 *dataptr; |
| 432 | |
| 433 | retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 434 | retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST); |
| 435 | retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640); |
| 436 | retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | msleep(1); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 438 | retval |= register_write(dev->core, IVTV_REG_APU, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | if (retval < 0) |
| 441 | dprintk(0, "Error with register_write\n"); |
| 442 | |
Michael Krufky | 48c3575 | 2006-05-22 10:32:00 -0300 | [diff] [blame] | 443 | retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | &dev->pci->dev); |
Mauro Carvalho Chehab | 674434c | 2005-12-12 00:37:28 -0800 | [diff] [blame] | 445 | |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (retval != 0) { |
| 448 | dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n", |
Michael Krufky | 48c3575 | 2006-05-22 10:32:00 -0300 | [diff] [blame] | 449 | CX2341X_FIRM_ENC_FILENAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | dprintk(0, "Please fix your hotplug setup, the board will " |
| 451 | "not work without firmware loaded!\n"); |
| 452 | return -1; |
| 453 | } |
| 454 | |
Hans Verkuil | 2547223 | 2007-05-30 09:39:46 -0300 | [diff] [blame^] | 455 | if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) { |
| 456 | dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n", |
| 457 | firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE); |
Magnus Damm | 73ca66b | 2006-07-10 04:44:09 -0700 | [diff] [blame] | 458 | release_firmware(firmware); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return -1; |
| 460 | } |
Michael Krufky | af70dbd | 2007-03-03 10:10:43 -0200 | [diff] [blame] | 461 | dev->fw_size = firmware->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | if (0 != memcmp(firmware->data, magic, 8)) { |
| 464 | dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n"); |
Magnus Damm | 73ca66b | 2006-07-10 04:44:09 -0700 | [diff] [blame] | 465 | release_firmware(firmware); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return -1; |
| 467 | } |
| 468 | |
| 469 | /* transfer to the chip */ |
| 470 | dprintk(1,"Loading firmware ...\n"); |
| 471 | dataptr = (u32*)firmware->data; |
| 472 | for (i = 0; i < (firmware->size >> 2); i++) { |
| 473 | value = *dataptr; |
| 474 | checksum += ~value; |
| 475 | memory_write(dev->core, i, value); |
| 476 | dataptr++; |
| 477 | } |
| 478 | |
| 479 | /* read back to verify with the checksum */ |
| 480 | for (i--; i >= 0; i--) { |
| 481 | memory_read(dev->core, i, &value); |
| 482 | checksum -= ~value; |
| 483 | } |
| 484 | if (checksum) { |
| 485 | dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n"); |
Magnus Damm | 73ca66b | 2006-07-10 04:44:09 -0700 | [diff] [blame] | 486 | release_firmware(firmware); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return -1; |
| 488 | } |
| 489 | release_firmware(firmware); |
| 490 | dprintk(0, "Firmware upload successful.\n"); |
| 491 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 492 | retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST); |
| 493 | retval |= register_read(dev->core, IVTV_REG_SPU, &value); |
| 494 | retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | msleep(1); |
| 496 | |
| 497 | retval |= register_read(dev->core, IVTV_REG_VPU, &value); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 498 | retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | if (retval < 0) |
| 501 | dprintk(0, "Error with register_write\n"); |
| 502 | return 0; |
| 503 | } |
| 504 | |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 505 | /** |
| 506 | Settings used by the windows tv app for PVR2000: |
| 507 | ================================================================================================================= |
| 508 | Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode |
| 509 | ----------------------------------------------------------------------------------------------------------------- |
| 510 | MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo |
| 511 | MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo |
| 512 | VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo |
| 513 | DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo |
| 514 | DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo |
| 515 | ================================================================================================================= |
| 516 | *DB: "DirectBurn" |
| 517 | */ |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 518 | |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 519 | static void blackbird_codec_settings(struct cx8802_dev *dev) |
| 520 | { |
| 521 | /* assign frame size */ |
| 522 | blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0, |
| 523 | dev->height, dev->width); |
| 524 | |
| 525 | dev->params.width = dev->width; |
| 526 | dev->params.height = dev->height; |
Mauro Carvalho Chehab | ed10b06 | 2007-01-20 13:58:39 -0300 | [diff] [blame] | 527 | dev->params.is_50hz = (dev->core->tvnorm & V4L2_STD_625_50) != 0; |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 528 | |
| 529 | cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params); |
| 530 | } |
| 531 | |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 532 | static struct v4l2_mpeg_compression default_mpeg_params = { |
| 533 | .st_type = V4L2_MPEG_PS_2, |
| 534 | .st_bitrate = { |
| 535 | .mode = V4L2_BITRATE_CBR, |
| 536 | .min = 0, |
| 537 | .target = 0, |
| 538 | .max = 0 |
| 539 | }, |
| 540 | .ts_pid_pmt = 16, |
| 541 | .ts_pid_audio = 260, |
| 542 | .ts_pid_video = 256, |
| 543 | .ts_pid_pcr = 259, |
| 544 | .ps_size = 0, |
| 545 | .au_type = V4L2_MPEG_AU_2_II, |
| 546 | .au_bitrate = { |
| 547 | .mode = V4L2_BITRATE_CBR, |
| 548 | .min = 224, |
| 549 | .target = 224, |
| 550 | .max = 224 |
| 551 | }, |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 552 | .au_sample_rate = 48000, |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 553 | .au_pesid = 0, |
| 554 | .vi_type = V4L2_MPEG_VI_2, |
| 555 | .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3, |
| 556 | .vi_bitrate = { |
| 557 | .mode = V4L2_BITRATE_CBR, |
| 558 | .min = 4000, |
| 559 | .target = 4500, |
| 560 | .max = 6000 |
| 561 | }, |
| 562 | .vi_frame_rate = 25, |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 563 | .vi_frames_per_gop = 12, |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 564 | .vi_bframes_count = 2, |
| 565 | .vi_pesid = 0, |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 566 | .closed_gops = 1, |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 567 | .pulldown = 0 |
| 568 | }; |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | static int blackbird_initialize_codec(struct cx8802_dev *dev) |
| 571 | { |
| 572 | struct cx88_core *core = dev->core; |
| 573 | int version; |
| 574 | int retval; |
| 575 | |
| 576 | dprintk(1,"Initialize codec\n"); |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 577 | retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (retval < 0) { |
| 579 | /* ping was not successful, reset and upload firmware */ |
| 580 | cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */ |
| 581 | msleep(1); |
| 582 | cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */ |
| 583 | msleep(1); |
| 584 | retval = blackbird_load_firmware(dev); |
| 585 | if (retval < 0) |
| 586 | return retval; |
| 587 | |
| 588 | dev->mailbox = blackbird_find_mailbox(dev); |
| 589 | if (dev->mailbox < 0) |
| 590 | return -1; |
| 591 | |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 592 | retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | if (retval < 0) { |
| 594 | dprintk(0, "ERROR: Firmware ping failed!\n"); |
| 595 | return -1; |
| 596 | } |
| 597 | |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 598 | retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (retval < 0) { |
| 600 | dprintk(0, "ERROR: Firmware get encoder version failed!\n"); |
| 601 | return -1; |
| 602 | } |
| 603 | dprintk(0, "Firmware version is 0x%08x\n", version); |
| 604 | } |
| 605 | msleep(1); |
| 606 | |
| 607 | cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */ |
| 608 | cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */ |
| 609 | cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */ |
| 610 | cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */ |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | blackbird_codec_settings(dev); |
| 613 | msleep(1); |
| 614 | |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 615 | /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef); |
| 616 | blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0); |
| 617 | blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */ |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 618 | blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0, |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 619 | BLACKBIRD_FIELD1_SAA7115, |
Michael Krufky | 8a17ef97 | 2006-04-13 18:43:50 -0300 | [diff] [blame] | 620 | BLACKBIRD_FIELD2_SAA7115 |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 621 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 623 | /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */ |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 624 | blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0, |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 625 | BLACKBIRD_CUSTOM_EXTENSION_USR_DATA, |
| 626 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 627 | |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 628 | /* initialize the video input */ |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 629 | blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
| 631 | msleep(1); |
| 632 | |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 633 | blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | msleep(1); |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 635 | blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | msleep(1); |
| 637 | |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 638 | /* start capturing to the host interface */ |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 639 | /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */ |
| 640 | blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 641 | BLACKBIRD_MPEG_CAPTURE, |
| 642 | BLACKBIRD_RAW_BITS_NONE |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 643 | ); |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 644 | msleep(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 646 | blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* ------------------------------------------------------------------ */ |
| 651 | |
| 652 | static int bb_buf_setup(struct videobuf_queue *q, |
| 653 | unsigned int *count, unsigned int *size) |
| 654 | { |
| 655 | struct cx8802_fh *fh = q->priv_data; |
| 656 | |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 657 | fh->dev->ts_packet_size = 188 * 4; /* was: 512 */ |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 658 | fh->dev->ts_packet_count = mpegbufs; /* was: 100 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count; |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 661 | *count = fh->dev->ts_packet_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int |
| 666 | bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, |
| 667 | enum v4l2_field field) |
| 668 | { |
| 669 | struct cx8802_fh *fh = q->priv_data; |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 670 | return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static void |
| 674 | bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) |
| 675 | { |
| 676 | struct cx8802_fh *fh = q->priv_data; |
| 677 | cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb); |
| 678 | } |
| 679 | |
| 680 | static void |
| 681 | bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb) |
| 682 | { |
Mauro Carvalho Chehab | c7b0ac0 | 2006-03-10 12:29:15 -0300 | [diff] [blame] | 683 | cx88_free_buffer(q, (struct cx88_buffer*)vb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static struct videobuf_queue_ops blackbird_qops = { |
| 687 | .buf_setup = bb_buf_setup, |
| 688 | .buf_prepare = bb_buf_prepare, |
| 689 | .buf_queue = bb_buf_queue, |
| 690 | .buf_release = bb_buf_release, |
| 691 | }; |
| 692 | |
| 693 | /* ------------------------------------------------------------------ */ |
| 694 | |
Michael Krufky | 38a2713 | 2006-06-26 23:42:39 -0300 | [diff] [blame] | 695 | static const u32 *ctrl_classes[] = { |
| 696 | cx88_user_ctrls, |
| 697 | cx2341x_mpeg_ctrls, |
| 698 | NULL |
| 699 | }; |
| 700 | |
| 701 | static int blackbird_queryctrl(struct cx8802_dev *dev, struct v4l2_queryctrl *qctrl) |
| 702 | { |
| 703 | qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id); |
| 704 | if (qctrl->id == 0) |
| 705 | return -EINVAL; |
| 706 | |
| 707 | /* Standard V4L2 controls */ |
| 708 | if (cx8800_ctrl_query(qctrl) == 0) |
| 709 | return 0; |
| 710 | |
| 711 | /* MPEG V4L2 controls */ |
| 712 | if (cx2341x_ctrl_query(&dev->params, qctrl)) |
| 713 | qctrl->flags |= V4L2_CTRL_FLAG_DISABLED; |
| 714 | return 0; |
| 715 | } |
| 716 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 717 | /* ------------------------------------------------------------------ */ |
| 718 | /* IOCTL Handlers */ |
| 719 | |
| 720 | static int vidioc_querymenu (struct file *file, void *priv, |
| 721 | struct v4l2_querymenu *qmenu) |
Michael Krufky | 38a2713 | 2006-06-26 23:42:39 -0300 | [diff] [blame] | 722 | { |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 723 | struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev; |
Michael Krufky | 38a2713 | 2006-06-26 23:42:39 -0300 | [diff] [blame] | 724 | struct v4l2_queryctrl qctrl; |
| 725 | |
| 726 | qctrl.id = qmenu->id; |
| 727 | blackbird_queryctrl(dev, &qctrl); |
| 728 | return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id)); |
| 729 | } |
| 730 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 731 | static int vidioc_querycap (struct file *file, void *priv, |
| 732 | struct v4l2_capability *cap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | { |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 734 | struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev; |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 735 | struct cx88_core *core = dev->core; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 737 | strcpy(cap->driver, "cx88_blackbird"); |
| 738 | strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card)); |
| 739 | sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci)); |
| 740 | cap->version = CX88_VERSION_CODE; |
| 741 | cap->capabilities = |
| 742 | V4L2_CAP_VIDEO_CAPTURE | |
| 743 | V4L2_CAP_READWRITE | |
| 744 | V4L2_CAP_STREAMING; |
| 745 | if (UNSET != core->tuner_type) |
| 746 | cap->capabilities |= V4L2_CAP_TUNER; |
Mauro Carvalho Chehab | ed10b06 | 2007-01-20 13:58:39 -0300 | [diff] [blame] | 747 | return 0; |
| 748 | } |
| 749 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 750 | static int vidioc_enum_fmt_cap (struct file *file, void *priv, |
| 751 | struct v4l2_fmtdesc *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 753 | if (f->index != 0) |
| 754 | return -EINVAL; |
| 755 | |
| 756 | strlcpy(f->description, "MPEG", sizeof(f->description)); |
| 757 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 758 | f->pixelformat = V4L2_PIX_FMT_MPEG; |
| 759 | return 0; |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 762 | static int vidioc_g_fmt_cap (struct file *file, void *priv, |
| 763 | struct v4l2_format *f) |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 764 | { |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 765 | struct cx8802_fh *fh = priv; |
| 766 | struct cx8802_dev *dev = fh->dev; |
| 767 | |
| 768 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 769 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 770 | f->fmt.pix.bytesperline = 0; |
| 771 | f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */ |
| 772 | f->fmt.pix.colorspace = 0; |
| 773 | f->fmt.pix.width = dev->width; |
| 774 | f->fmt.pix.height = dev->height; |
| 775 | f->fmt.pix.field = fh->mpegq.field; |
| 776 | dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n", |
| 777 | dev->width, dev->height, fh->mpegq.field ); |
| 778 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 781 | static int vidioc_try_fmt_cap (struct file *file, void *priv, |
| 782 | struct v4l2_format *f) |
| 783 | { |
| 784 | struct cx8802_fh *fh = priv; |
| 785 | struct cx8802_dev *dev = fh->dev; |
| 786 | |
| 787 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 788 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 789 | f->fmt.pix.bytesperline = 0; |
| 790 | f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */; |
| 791 | f->fmt.pix.colorspace = 0; |
| 792 | dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n", |
| 793 | dev->width, dev->height, fh->mpegq.field ); |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static int vidioc_s_fmt_cap (struct file *file, void *priv, |
| 798 | struct v4l2_format *f) |
| 799 | { |
| 800 | struct cx8802_fh *fh = priv; |
| 801 | struct cx8802_dev *dev = fh->dev; |
| 802 | struct cx88_core *core = dev->core; |
| 803 | |
| 804 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 805 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 806 | f->fmt.pix.bytesperline = 0; |
| 807 | f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */; |
| 808 | f->fmt.pix.colorspace = 0; |
| 809 | dev->width = f->fmt.pix.width; |
| 810 | dev->height = f->fmt.pix.height; |
| 811 | fh->mpegq.field = f->fmt.pix.field; |
| 812 | cx88_set_scale(core, f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field); |
| 813 | blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0, |
| 814 | f->fmt.pix.height, f->fmt.pix.width); |
| 815 | dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n", |
| 816 | f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field ); |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p) |
| 821 | { |
| 822 | struct cx8802_fh *fh = priv; |
| 823 | return (videobuf_reqbufs(&fh->mpegq, p)); |
| 824 | } |
| 825 | |
| 826 | static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p) |
| 827 | { |
| 828 | struct cx8802_fh *fh = priv; |
| 829 | return (videobuf_querybuf(&fh->mpegq, p)); |
| 830 | } |
| 831 | |
| 832 | static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p) |
| 833 | { |
| 834 | struct cx8802_fh *fh = priv; |
| 835 | return (videobuf_qbuf(&fh->mpegq, p)); |
| 836 | } |
| 837 | |
| 838 | static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p) |
| 839 | { |
| 840 | struct cx8802_fh *fh = priv; |
| 841 | return (videobuf_dqbuf(&fh->mpegq, p, |
| 842 | file->f_flags & O_NONBLOCK)); |
| 843 | } |
| 844 | |
| 845 | static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
| 846 | { |
| 847 | struct cx8802_fh *fh = priv; |
| 848 | return videobuf_streamon(&fh->mpegq); |
| 849 | } |
| 850 | |
| 851 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
| 852 | { |
| 853 | struct cx8802_fh *fh = priv; |
| 854 | return videobuf_streamoff(&fh->mpegq); |
| 855 | } |
| 856 | |
| 857 | static int vidioc_g_mpegcomp (struct file *file, void *fh, |
| 858 | struct v4l2_mpeg_compression *f) |
| 859 | { |
| 860 | printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. " |
| 861 | "Replace with VIDIOC_G_EXT_CTRLS!"); |
| 862 | memcpy(f,&default_mpeg_params,sizeof(*f)); |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static int vidioc_s_mpegcomp (struct file *file, void *fh, |
| 867 | struct v4l2_mpeg_compression *f) |
| 868 | { |
| 869 | printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. " |
| 870 | "Replace with VIDIOC_S_EXT_CTRLS!"); |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int vidioc_g_ext_ctrls (struct file *file, void *priv, |
| 875 | struct v4l2_ext_controls *f) |
| 876 | { |
| 877 | struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev; |
| 878 | |
| 879 | if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG) |
| 880 | return -EINVAL; |
| 881 | return cx2341x_ext_ctrls(&dev->params, f, VIDIOC_G_EXT_CTRLS); |
| 882 | } |
| 883 | |
| 884 | static int vidioc_s_ext_ctrls (struct file *file, void *priv, |
| 885 | struct v4l2_ext_controls *f) |
| 886 | { |
| 887 | struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev; |
| 888 | struct cx2341x_mpeg_params p; |
| 889 | int err; |
| 890 | |
| 891 | if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG) |
| 892 | return -EINVAL; |
| 893 | p = dev->params; |
| 894 | err = cx2341x_ext_ctrls(&p, f, VIDIOC_S_EXT_CTRLS); |
| 895 | if (!err) { |
| 896 | err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p); |
| 897 | dev->params = p; |
| 898 | } |
| 899 | return err; |
| 900 | } |
| 901 | |
| 902 | static int vidioc_try_ext_ctrls (struct file *file, void *priv, |
| 903 | struct v4l2_ext_controls *f) |
| 904 | { |
| 905 | struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev; |
| 906 | struct cx2341x_mpeg_params p; |
| 907 | int err; |
| 908 | |
| 909 | if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG) |
| 910 | return -EINVAL; |
| 911 | p = dev->params; |
| 912 | err = cx2341x_ext_ctrls(&p, f, VIDIOC_TRY_EXT_CTRLS); |
| 913 | |
| 914 | return err; |
| 915 | } |
| 916 | |
| 917 | static int vidioc_s_frequency (struct file *file, void *priv, |
| 918 | struct v4l2_frequency *f) |
| 919 | { |
| 920 | struct cx8802_fh *fh = priv; |
| 921 | struct cx8802_dev *dev = fh->dev; |
| 922 | struct cx88_core *core = dev->core; |
| 923 | |
| 924 | blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, |
| 925 | BLACKBIRD_END_NOW, |
| 926 | BLACKBIRD_MPEG_CAPTURE, |
| 927 | BLACKBIRD_RAW_BITS_NONE); |
| 928 | cx88_set_freq (core,f); |
| 929 | blackbird_initialize_codec(dev); |
| 930 | cx88_set_scale(dev->core, dev->width, dev->height, |
| 931 | fh->mpegq.field); |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | static int vidioc_log_status (struct file *file, void *priv) |
| 936 | { |
| 937 | struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev; |
| 938 | struct cx88_core *core = dev->core; |
| 939 | char name[32 + 2]; |
| 940 | |
| 941 | snprintf(name, sizeof(name), "%s/2", core->name); |
| 942 | printk("%s/2: ============ START LOG STATUS ============\n", |
| 943 | core->name); |
| 944 | cx88_call_i2c_clients(core, VIDIOC_LOG_STATUS, NULL); |
| 945 | cx2341x_log_status(&dev->params, name); |
| 946 | printk("%s/2: ============= END LOG STATUS =============\n", |
| 947 | core->name); |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int vidioc_queryctrl (struct file *file, void *priv, |
| 952 | struct v4l2_queryctrl *qctrl) |
| 953 | { |
| 954 | struct cx8802_dev *dev = ((struct cx8802_fh *)priv)->dev; |
| 955 | |
| 956 | if (blackbird_queryctrl(dev, qctrl) == 0) |
| 957 | return 0; |
| 958 | |
| 959 | qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id); |
| 960 | if (unlikely(qctrl->id == 0)) |
| 961 | return -EINVAL; |
| 962 | return cx8800_ctrl_query(qctrl); |
| 963 | } |
| 964 | |
| 965 | static int vidioc_enum_input (struct file *file, void *priv, |
| 966 | struct v4l2_input *i) |
| 967 | { |
| 968 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 969 | return cx88_enum_input (core,i); |
| 970 | } |
| 971 | |
| 972 | static int vidioc_g_ctrl (struct file *file, void *priv, |
| 973 | struct v4l2_control *ctl) |
| 974 | { |
| 975 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 976 | return |
| 977 | cx88_get_control(core,ctl); |
| 978 | } |
| 979 | |
| 980 | static int vidioc_s_ctrl (struct file *file, void *priv, |
| 981 | struct v4l2_control *ctl) |
| 982 | { |
| 983 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 984 | return |
| 985 | cx88_set_control(core,ctl); |
| 986 | } |
| 987 | |
| 988 | static int vidioc_g_frequency (struct file *file, void *priv, |
| 989 | struct v4l2_frequency *f) |
| 990 | { |
| 991 | struct cx8802_fh *fh = priv; |
| 992 | struct cx88_core *core = fh->dev->core; |
| 993 | |
| 994 | if (unlikely(UNSET == core->tuner_type)) |
| 995 | return -EINVAL; |
| 996 | |
| 997 | f->type = V4L2_TUNER_ANALOG_TV; |
| 998 | f->frequency = core->freq; |
| 999 | cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f); |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | static int vidioc_g_input (struct file *file, void *priv, unsigned int *i) |
| 1005 | { |
| 1006 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 1007 | |
| 1008 | *i = core->input; |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static int vidioc_s_input (struct file *file, void *priv, unsigned int i) |
| 1013 | { |
| 1014 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 1015 | |
| 1016 | if (i >= 4) |
| 1017 | return -EINVAL; |
| 1018 | |
| 1019 | mutex_lock(&core->lock); |
| 1020 | cx88_newstation(core); |
| 1021 | cx88_video_mux(core,i); |
| 1022 | mutex_unlock(&core->lock); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static int vidioc_g_tuner (struct file *file, void *priv, |
| 1027 | struct v4l2_tuner *t) |
| 1028 | { |
| 1029 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 1030 | u32 reg; |
| 1031 | |
| 1032 | if (unlikely(UNSET == core->tuner_type)) |
| 1033 | return -EINVAL; |
Jelle Foks | f057131 | 2007-05-21 14:56:17 -0300 | [diff] [blame] | 1034 | if (0 != t->index) |
| 1035 | return -EINVAL; |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 1036 | |
| 1037 | strcpy(t->name, "Television"); |
| 1038 | t->type = V4L2_TUNER_ANALOG_TV; |
| 1039 | t->capability = V4L2_TUNER_CAP_NORM; |
| 1040 | t->rangehigh = 0xffffffffUL; |
| 1041 | |
| 1042 | cx88_get_stereo(core ,t); |
| 1043 | reg = cx_read(MO_DEVICE_STATUS); |
| 1044 | t->signal = (reg & (1<<5)) ? 0xffff : 0x0000; |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static int vidioc_s_tuner (struct file *file, void *priv, |
| 1049 | struct v4l2_tuner *t) |
| 1050 | { |
| 1051 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 1052 | |
| 1053 | if (UNSET == core->tuner_type) |
| 1054 | return -EINVAL; |
| 1055 | if (0 != t->index) |
| 1056 | return -EINVAL; |
| 1057 | |
| 1058 | cx88_set_stereo(core, t->audmode, 1); |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id) |
| 1063 | { |
| 1064 | struct cx88_core *core = ((struct cx8802_fh *)priv)->dev->core; |
| 1065 | |
| 1066 | mutex_lock(&core->lock); |
| 1067 | cx88_set_tvnorm(core,*id); |
| 1068 | mutex_unlock(&core->lock); |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | /* FIXME: cx88_ioctl_hook not implemented */ |
| 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | static int mpeg_open(struct inode *inode, struct file *file) |
| 1075 | { |
| 1076 | int minor = iminor(inode); |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1077 | struct cx8802_dev *dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | struct cx8802_fh *fh; |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1079 | struct cx8802_driver *drv = NULL; |
| 1080 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Jelle Foks | 49c6b46 | 2006-11-18 15:47:11 -0300 | [diff] [blame] | 1082 | dev = cx8802_get_device(inode); |
| 1083 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1084 | dprintk( 1, "%s\n", __FUNCTION__); |
| 1085 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1086 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | return -ENODEV; |
| 1088 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1089 | /* Make sure we can acquire the hardware */ |
| 1090 | drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD); |
| 1091 | if (drv) { |
| 1092 | err = drv->request_acquire(drv); |
| 1093 | if(err != 0) { |
| 1094 | dprintk(1,"%s: Unable to acquire hardware, %d\n", __FUNCTION__, err); |
| 1095 | return err; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | if (blackbird_initialize_codec(dev) < 0) { |
| 1100 | if (drv) |
| 1101 | drv->request_release(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | return -EINVAL; |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | dprintk(1,"open minor=%d\n",minor); |
| 1105 | |
| 1106 | /* allocate + initialize per filehandle data */ |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 1107 | fh = kzalloc(sizeof(*fh),GFP_KERNEL); |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1108 | if (NULL == fh) { |
| 1109 | if (drv) |
| 1110 | drv->request_release(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | return -ENOMEM; |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | file->private_data = fh; |
| 1114 | fh->dev = dev; |
| 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | videobuf_queue_init(&fh->mpegq, &blackbird_qops, |
| 1117 | dev->pci, &dev->slock, |
| 1118 | V4L2_BUF_TYPE_VIDEO_CAPTURE, |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 1119 | V4L2_FIELD_INTERLACED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | sizeof(struct cx88_buffer), |
| 1121 | fh); |
Catalin Climov | 3162942 | 2005-11-08 21:36:17 -0800 | [diff] [blame] | 1122 | |
| 1123 | /* FIXME: locking against other video device */ |
| 1124 | cx88_set_scale(dev->core, dev->width, dev->height, |
| 1125 | fh->mpegq.field); |
| 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | static int mpeg_release(struct inode *inode, struct file *file) |
| 1131 | { |
| 1132 | struct cx8802_fh *fh = file->private_data; |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1133 | struct cx8802_dev *dev = NULL; |
| 1134 | struct cx8802_driver *drv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Michael Krufky | 855dbad | 2006-05-22 10:31:54 -0300 | [diff] [blame] | 1136 | /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */ |
| 1137 | blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 1138 | BLACKBIRD_END_NOW, |
| 1139 | BLACKBIRD_MPEG_CAPTURE, |
| 1140 | BLACKBIRD_RAW_BITS_NONE |
| 1141 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
Valentin Zagura | 0b5f56d | 2006-04-20 22:56:25 -0300 | [diff] [blame] | 1143 | cx8802_cancel_buffers(fh->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | /* stop mpeg capture */ |
| 1145 | if (fh->mpegq.streaming) |
| 1146 | videobuf_streamoff(&fh->mpegq); |
| 1147 | if (fh->mpegq.reading) |
| 1148 | videobuf_read_stop(&fh->mpegq); |
| 1149 | |
| 1150 | videobuf_mmap_free(&fh->mpegq); |
| 1151 | file->private_data = NULL; |
| 1152 | kfree(fh); |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1153 | |
| 1154 | /* Make sure we release the hardware */ |
| 1155 | dev = cx8802_get_device(inode); |
| 1156 | if (dev == NULL) |
| 1157 | return -ENODEV; |
| 1158 | |
| 1159 | drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD); |
| 1160 | if (drv) |
| 1161 | drv->request_release(drv); |
| 1162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | static ssize_t |
| 1167 | mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos) |
| 1168 | { |
| 1169 | struct cx8802_fh *fh = file->private_data; |
| 1170 | |
| 1171 | return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0, |
| 1172 | file->f_flags & O_NONBLOCK); |
| 1173 | } |
| 1174 | |
| 1175 | static unsigned int |
| 1176 | mpeg_poll(struct file *file, struct poll_table_struct *wait) |
| 1177 | { |
| 1178 | struct cx8802_fh *fh = file->private_data; |
| 1179 | |
| 1180 | return videobuf_poll_stream(file, &fh->mpegq, wait); |
| 1181 | } |
| 1182 | |
| 1183 | static int |
| 1184 | mpeg_mmap(struct file *file, struct vm_area_struct * vma) |
| 1185 | { |
| 1186 | struct cx8802_fh *fh = file->private_data; |
| 1187 | |
| 1188 | return videobuf_mmap_mapper(&fh->mpegq, vma); |
| 1189 | } |
| 1190 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 1191 | static const struct file_operations mpeg_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | { |
| 1193 | .owner = THIS_MODULE, |
| 1194 | .open = mpeg_open, |
| 1195 | .release = mpeg_release, |
| 1196 | .read = mpeg_read, |
| 1197 | .poll = mpeg_poll, |
| 1198 | .mmap = mpeg_mmap, |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 1199 | .ioctl = video_ioctl2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | .llseek = no_llseek, |
| 1201 | }; |
| 1202 | |
| 1203 | static struct video_device cx8802_mpeg_template = |
| 1204 | { |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 1205 | .name = "cx8802", |
| 1206 | .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER, |
| 1207 | .fops = &mpeg_fops, |
| 1208 | .minor = -1, |
| 1209 | .vidioc_querymenu = vidioc_querymenu, |
| 1210 | .vidioc_querycap = vidioc_querycap, |
| 1211 | .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap, |
| 1212 | .vidioc_g_fmt_cap = vidioc_g_fmt_cap, |
| 1213 | .vidioc_try_fmt_cap = vidioc_try_fmt_cap, |
| 1214 | .vidioc_s_fmt_cap = vidioc_s_fmt_cap, |
| 1215 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1216 | .vidioc_querybuf = vidioc_querybuf, |
| 1217 | .vidioc_qbuf = vidioc_qbuf, |
| 1218 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1219 | .vidioc_streamon = vidioc_streamon, |
| 1220 | .vidioc_streamoff = vidioc_streamoff, |
| 1221 | .vidioc_g_mpegcomp = vidioc_g_mpegcomp, |
| 1222 | .vidioc_s_mpegcomp = vidioc_s_mpegcomp, |
| 1223 | .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls, |
| 1224 | .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls, |
| 1225 | .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls, |
| 1226 | .vidioc_s_frequency = vidioc_s_frequency, |
| 1227 | .vidioc_log_status = vidioc_log_status, |
| 1228 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1229 | .vidioc_enum_input = vidioc_enum_input, |
| 1230 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1231 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1232 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1233 | .vidioc_g_input = vidioc_g_input, |
| 1234 | .vidioc_s_input = vidioc_s_input, |
| 1235 | .vidioc_g_tuner = vidioc_g_tuner, |
| 1236 | .vidioc_s_tuner = vidioc_s_tuner, |
| 1237 | .vidioc_s_std = vidioc_s_std, |
| 1238 | .tvnorms = CX88_NORMS, |
Mauro Carvalho Chehab | a4b662f | 2007-01-20 13:59:41 -0300 | [diff] [blame] | 1239 | .current_norm = V4L2_STD_NTSC_M, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | }; |
| 1241 | |
| 1242 | /* ------------------------------------------------------------------ */ |
| 1243 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1244 | /* The CX8802 MPEG API will call this when we can use the hardware */ |
| 1245 | static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv) |
| 1246 | { |
| 1247 | struct cx88_core *core = drv->core; |
| 1248 | int err = 0; |
| 1249 | |
| 1250 | switch (core->board) { |
| 1251 | case CX88_BOARD_HAUPPAUGE_HVR1300: |
| 1252 | /* By default, core setup will leave the cx22702 out of reset, on the bus. |
| 1253 | * We left the hardware on power up with the cx22702 active. |
| 1254 | * We're being given access to re-arrange the GPIOs. |
| 1255 | * Take the bus off the cx22702 and put the cx23416 on it. |
| 1256 | */ |
| 1257 | cx_clear(MO_GP0_IO, 0x00000080); /* cx22702 in reset */ |
| 1258 | cx_set(MO_GP0_IO, 0x00000004); /* Disable the cx22702 */ |
| 1259 | break; |
| 1260 | default: |
| 1261 | err = -ENODEV; |
| 1262 | } |
| 1263 | return err; |
| 1264 | } |
| 1265 | |
| 1266 | /* The CX8802 MPEG API will call this when we need to release the hardware */ |
| 1267 | static int cx8802_blackbird_advise_release(struct cx8802_driver *drv) |
| 1268 | { |
| 1269 | struct cx88_core *core = drv->core; |
| 1270 | int err = 0; |
| 1271 | |
| 1272 | switch (core->board) { |
| 1273 | case CX88_BOARD_HAUPPAUGE_HVR1300: |
| 1274 | /* Exit leaving the cx23416 on the bus */ |
| 1275 | break; |
| 1276 | default: |
| 1277 | err = -ENODEV; |
| 1278 | } |
| 1279 | return err; |
| 1280 | } |
| 1281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | static void blackbird_unregister_video(struct cx8802_dev *dev) |
| 1283 | { |
| 1284 | if (dev->mpeg_dev) { |
| 1285 | if (-1 != dev->mpeg_dev->minor) |
| 1286 | video_unregister_device(dev->mpeg_dev); |
| 1287 | else |
| 1288 | video_device_release(dev->mpeg_dev); |
| 1289 | dev->mpeg_dev = NULL; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | static int blackbird_register_video(struct cx8802_dev *dev) |
| 1294 | { |
| 1295 | int err; |
| 1296 | |
| 1297 | dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci, |
| 1298 | &cx8802_mpeg_template,"mpeg"); |
| 1299 | err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1); |
| 1300 | if (err < 0) { |
| 1301 | printk(KERN_INFO "%s/2: can't register mpeg device\n", |
| 1302 | dev->core->name); |
| 1303 | return err; |
| 1304 | } |
| 1305 | printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n", |
| 1306 | dev->core->name,dev->mpeg_dev->minor & 0x1f); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | /* ----------------------------------------------------------- */ |
| 1311 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1312 | static int cx8802_blackbird_probe(struct cx8802_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | { |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1314 | struct cx88_core *core = drv->core; |
| 1315 | struct cx8802_dev *dev = core->dvbdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | int err; |
| 1317 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1318 | dprintk( 1, "%s\n", __FUNCTION__); |
| 1319 | dprintk( 1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n", |
| 1320 | core->board, |
| 1321 | core->name, |
| 1322 | core->pci_bus, |
| 1323 | core->pci_slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
| 1325 | err = -ENODEV; |
Michael Krufky | 48d5e80 | 2006-09-25 14:09:10 -0300 | [diff] [blame] | 1326 | if (!(cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | goto fail_core; |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | dev->width = 720; |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 1330 | dev->height = 576; |
Hans Verkuil | f022156 | 2006-06-18 16:11:06 -0300 | [diff] [blame] | 1331 | cx2341x_fill_defaults(&dev->params); |
Hans Verkuil | 45ad9f8 | 2006-06-21 17:04:13 -0300 | [diff] [blame] | 1332 | dev->params.port = CX2341X_PORT_STREAMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 1334 | cx8802_mpeg_template.current_norm = core->tvnorm; |
| 1335 | |
Mauro Carvalho Chehab | ed10b06 | 2007-01-20 13:58:39 -0300 | [diff] [blame] | 1336 | if (core->tvnorm & V4L2_STD_525_60) { |
Michael Krufky | 45f87a2 | 2006-06-22 21:47:07 -0300 | [diff] [blame] | 1337 | dev->height = 480; |
| 1338 | } else { |
| 1339 | dev->height = 576; |
Steven Toth | 0345c38 | 2006-01-09 15:25:17 -0200 | [diff] [blame] | 1340 | } |
| 1341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | /* blackbird stuff */ |
| 1343 | printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n", |
| 1344 | core->name); |
| 1345 | host_setup(dev->core); |
| 1346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | blackbird_register_video(dev); |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 1348 | |
| 1349 | /* initial device configuration: needed ? */ |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 1350 | mutex_lock(&dev->core->lock); |
| 1351 | // init_controls(core); |
| 1352 | cx88_set_tvnorm(core,core->tvnorm); |
| 1353 | cx88_video_mux(core,0); |
| 1354 | mutex_unlock(&dev->core->lock); |
Mauro Carvalho Chehab | e52e98a | 2005-09-09 13:03:41 -0700 | [diff] [blame] | 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | return 0; |
| 1357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | fail_core: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | return err; |
| 1360 | } |
| 1361 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1362 | static int cx8802_blackbird_remove(struct cx8802_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | /* blackbird */ |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1365 | blackbird_unregister_video(drv->core->dvbdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1367 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1370 | static struct cx8802_driver cx8802_blackbird_driver = { |
| 1371 | .type_id = CX88_MPEG_BLACKBIRD, |
| 1372 | .hw_access = CX8802_DRVCTL_SHARED, |
| 1373 | .probe = cx8802_blackbird_probe, |
| 1374 | .remove = cx8802_blackbird_remove, |
| 1375 | .advise_acquire = cx8802_blackbird_advise_acquire, |
| 1376 | .advise_release = cx8802_blackbird_advise_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | }; |
| 1378 | |
| 1379 | static int blackbird_init(void) |
| 1380 | { |
| 1381 | printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n", |
| 1382 | (CX88_VERSION_CODE >> 16) & 0xff, |
| 1383 | (CX88_VERSION_CODE >> 8) & 0xff, |
| 1384 | CX88_VERSION_CODE & 0xff); |
| 1385 | #ifdef SNAPSHOT |
| 1386 | printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n", |
| 1387 | SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100); |
| 1388 | #endif |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1389 | return cx8802_register_driver(&cx8802_blackbird_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | static void blackbird_fini(void) |
| 1393 | { |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1394 | cx8802_unregister_driver(&cx8802_blackbird_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | module_init(blackbird_init); |
| 1398 | module_exit(blackbird_fini); |
| 1399 | |
Mauro Carvalho Chehab | b3c4ee7 | 2007-01-20 13:59:38 -0300 | [diff] [blame] | 1400 | module_param_named(video_debug,cx8802_mpeg_template.debug, int, 0644); |
| 1401 | MODULE_PARM_DESC(debug,"enable debug messages [video]"); |
Steven Toth | 6c5be74 | 2006-12-02 21:15:51 -0200 | [diff] [blame] | 1402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | /* ----------------------------------------------------------- */ |
| 1404 | /* |
| 1405 | * Local variables: |
| 1406 | * c-basic-offset: 8 |
| 1407 | * End: |
Mauro Carvalho Chehab | b45009b | 2005-06-23 22:05:03 -0700 | [diff] [blame] | 1408 | * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | */ |