Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Copyright (C) 2005 Mike Isely <isely@pobox.com> |
| 6 | * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/device.h> // for linux/firmware.h |
| 24 | #include <linux/firmware.h> |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 25 | #include "pvrusb2-util.h" |
| 26 | #include "pvrusb2-encoder.h" |
| 27 | #include "pvrusb2-hdw-internal.h" |
| 28 | #include "pvrusb2-debug.h" |
Michael Krufky | 8d36436 | 2007-01-22 02:17:55 -0300 | [diff] [blame] | 29 | #include "pvrusb2-fx2-cmd.h" |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 30 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /* Firmware mailbox flags - definitions found from ivtv */ |
| 34 | #define IVTV_MBOX_FIRMWARE_DONE 0x00000004 |
| 35 | #define IVTV_MBOX_DRIVER_DONE 0x00000002 |
| 36 | #define IVTV_MBOX_DRIVER_BUSY 0x00000001 |
| 37 | |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 38 | #define MBOX_BASE 0x44 |
| 39 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 40 | |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 41 | static int pvr2_encoder_write_words(struct pvr2_hdw *hdw, |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 42 | unsigned int offs, |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 43 | const u32 *data, unsigned int dlen) |
| 44 | { |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 45 | unsigned int idx,addr; |
| 46 | unsigned int bAddr; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 47 | int ret; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 48 | unsigned int chunkCnt; |
| 49 | |
| 50 | /* |
| 51 | |
| 52 | Format: First byte must be 0x01. Remaining 32 bit words are |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 53 | spread out into chunks of 7 bytes each, with the first 4 bytes |
| 54 | being the data word (little endian), and the next 3 bytes |
| 55 | being the address where that data word is to be written (big |
| 56 | endian). Repeat request for additional words, with offset |
| 57 | adjusted accordingly. |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 58 | |
| 59 | */ |
| 60 | while (dlen) { |
| 61 | chunkCnt = 8; |
| 62 | if (chunkCnt > dlen) chunkCnt = dlen; |
| 63 | memset(hdw->cmd_buffer,0,sizeof(hdw->cmd_buffer)); |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 64 | bAddr = 0; |
| 65 | hdw->cmd_buffer[bAddr++] = FX2CMD_MEM_WRITE_DWORD; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 66 | for (idx = 0; idx < chunkCnt; idx++) { |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 67 | addr = idx + offs; |
| 68 | hdw->cmd_buffer[bAddr+6] = (addr & 0xffu); |
| 69 | hdw->cmd_buffer[bAddr+5] = ((addr>>8) & 0xffu); |
| 70 | hdw->cmd_buffer[bAddr+4] = ((addr>>16) & 0xffu); |
| 71 | PVR2_DECOMPOSE_LE(hdw->cmd_buffer, bAddr,data[idx]); |
| 72 | bAddr += 7; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 73 | } |
| 74 | ret = pvr2_send_request(hdw, |
| 75 | hdw->cmd_buffer,1+(chunkCnt*7), |
Mike Isely | a0fd1cb | 2006-06-30 11:35:28 -0300 | [diff] [blame] | 76 | NULL,0); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 77 | if (ret) return ret; |
| 78 | data += chunkCnt; |
| 79 | dlen -= chunkCnt; |
| 80 | offs += chunkCnt; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 87 | static int pvr2_encoder_read_words(struct pvr2_hdw *hdw, |
| 88 | unsigned int offs, |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 89 | u32 *data, unsigned int dlen) |
| 90 | { |
| 91 | unsigned int idx; |
| 92 | int ret; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 93 | unsigned int chunkCnt; |
| 94 | |
| 95 | /* |
| 96 | |
| 97 | Format: First byte must be 0x02 (status check) or 0x28 (read |
| 98 | back block of 32 bit words). Next 6 bytes must be zero, |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 99 | followed by a single byte of MBOX_BASE+offset for portion to |
| 100 | be read. Returned data is packed set of 32 bits words that |
| 101 | were read. |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 102 | |
| 103 | */ |
| 104 | |
| 105 | while (dlen) { |
| 106 | chunkCnt = 16; |
| 107 | if (chunkCnt > dlen) chunkCnt = dlen; |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 108 | if (chunkCnt < 16) chunkCnt = 1; |
Michael Krufky | 8d36436 | 2007-01-22 02:17:55 -0300 | [diff] [blame] | 109 | hdw->cmd_buffer[0] = |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 110 | ((chunkCnt == 1) ? |
| 111 | FX2CMD_MEM_READ_DWORD : FX2CMD_MEM_READ_64BYTES); |
| 112 | hdw->cmd_buffer[1] = 0; |
| 113 | hdw->cmd_buffer[2] = 0; |
| 114 | hdw->cmd_buffer[3] = 0; |
| 115 | hdw->cmd_buffer[4] = 0; |
| 116 | hdw->cmd_buffer[5] = ((offs>>16) & 0xffu); |
| 117 | hdw->cmd_buffer[6] = ((offs>>8) & 0xffu); |
| 118 | hdw->cmd_buffer[7] = (offs & 0xffu); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 119 | ret = pvr2_send_request(hdw, |
| 120 | hdw->cmd_buffer,8, |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 121 | hdw->cmd_buffer, |
| 122 | (chunkCnt == 1 ? 4 : 16 * 4)); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 123 | if (ret) return ret; |
| 124 | |
| 125 | for (idx = 0; idx < chunkCnt; idx++) { |
| 126 | data[idx] = PVR2_COMPOSE_LE(hdw->cmd_buffer,idx*4); |
| 127 | } |
| 128 | data += chunkCnt; |
| 129 | dlen -= chunkCnt; |
| 130 | offs += chunkCnt; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 137 | /* This prototype is set up to be compatible with the |
| 138 | cx2341x_mbox_func prototype in cx2341x.h, which should be in |
| 139 | kernels 2.6.18 or later. We do this so that we can enable |
| 140 | cx2341x.ko to write to our encoder (by handing it a pointer to this |
| 141 | function). For earlier kernels this doesn't really matter. */ |
| 142 | static int pvr2_encoder_cmd(void *ctxt, |
| 143 | int cmd, |
| 144 | int arg_cnt_send, |
| 145 | int arg_cnt_recv, |
| 146 | u32 *argp) |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 147 | { |
| 148 | unsigned int poll_count; |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 149 | unsigned int try_count = 0; |
| 150 | int retry_flag; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 151 | int ret = 0; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 152 | unsigned int idx; |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 153 | /* These sizes look to be limited by the FX2 firmware implementation */ |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 154 | u32 wrData[16]; |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 155 | u32 rdData[16]; |
| 156 | struct pvr2_hdw *hdw = (struct pvr2_hdw *)ctxt; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 157 | |
Mike Isely | c05c046 | 2006-06-25 20:04:25 -0300 | [diff] [blame] | 158 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 159 | /* |
| 160 | |
| 161 | The encoder seems to speak entirely using blocks 32 bit words. |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 162 | In ivtv driver terms, this is a mailbox at MBOX_BASE which we |
| 163 | populate with data and watch what the hardware does with it. |
| 164 | The first word is a set of flags used to control the |
| 165 | transaction, the second word is the command to execute, the |
| 166 | third byte is zero (ivtv driver suggests that this is some |
| 167 | kind of return value), and the fourth byte is a specified |
| 168 | timeout (windows driver always uses 0x00060000 except for one |
| 169 | case when it is zero). All successive words are the argument |
| 170 | words for the command. |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 171 | |
| 172 | First, write out the entire set of words, with the first word |
| 173 | being zero. |
| 174 | |
| 175 | Next, write out just the first word again, but set it to |
| 176 | IVTV_MBOX_DRIVER_DONE | IVTV_DRIVER_BUSY this time (which |
| 177 | probably means "go"). |
| 178 | |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 179 | Next, read back the return count words. Check the first word, |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 180 | which should have IVTV_MBOX_FIRMWARE_DONE set. If however |
| 181 | that bit is not set, then the command isn't done so repeat the |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 182 | read until it is set. |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 183 | |
| 184 | Finally, write out just the first word again, but set it to |
| 185 | 0x0 this time (which probably means "idle"). |
| 186 | |
| 187 | */ |
| 188 | |
Ahmed S. Darwish | eca8ebf | 2007-01-20 00:35:03 -0300 | [diff] [blame] | 189 | if (arg_cnt_send > (ARRAY_SIZE(wrData) - 4)) { |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 190 | pvr2_trace( |
| 191 | PVR2_TRACE_ERROR_LEGS, |
| 192 | "Failed to write cx23416 command" |
| 193 | " - too many input arguments" |
Mauro Carvalho Chehab | 69b04f0 | 2007-01-21 22:02:35 -0300 | [diff] [blame] | 194 | " (was given %u limit %lu)", |
| 195 | arg_cnt_send, (long unsigned) ARRAY_SIZE(wrData) - 4); |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | } |
| 198 | |
Ahmed S. Darwish | eca8ebf | 2007-01-20 00:35:03 -0300 | [diff] [blame] | 199 | if (arg_cnt_recv > (ARRAY_SIZE(rdData) - 4)) { |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 200 | pvr2_trace( |
| 201 | PVR2_TRACE_ERROR_LEGS, |
| 202 | "Failed to write cx23416 command" |
| 203 | " - too many return arguments" |
Mauro Carvalho Chehab | 69b04f0 | 2007-01-21 22:02:35 -0300 | [diff] [blame] | 204 | " (was given %u limit %lu)", |
| 205 | arg_cnt_recv, (long unsigned) ARRAY_SIZE(rdData) - 4); |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 206 | return -EINVAL; |
| 207 | } |
| 208 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 209 | |
| 210 | LOCK_TAKE(hdw->ctl_lock); do { |
| 211 | |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 212 | retry_flag = 0; |
| 213 | try_count++; |
| 214 | ret = 0; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 215 | wrData[0] = 0; |
| 216 | wrData[1] = cmd; |
| 217 | wrData[2] = 0; |
| 218 | wrData[3] = 0x00060000; |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 219 | for (idx = 0; idx < arg_cnt_send; idx++) { |
| 220 | wrData[idx+4] = argp[idx]; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 221 | } |
Ahmed S. Darwish | eca8ebf | 2007-01-20 00:35:03 -0300 | [diff] [blame] | 222 | for (; idx < ARRAY_SIZE(wrData) - 4; idx++) { |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 223 | wrData[idx+4] = 0; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 224 | } |
| 225 | |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 226 | ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,idx); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 227 | if (ret) break; |
| 228 | wrData[0] = IVTV_MBOX_DRIVER_DONE|IVTV_MBOX_DRIVER_BUSY; |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 229 | ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 230 | if (ret) break; |
| 231 | poll_count = 0; |
| 232 | while (1) { |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 233 | poll_count++; |
| 234 | ret = pvr2_encoder_read_words(hdw,MBOX_BASE,rdData, |
| 235 | arg_cnt_recv+4); |
| 236 | if (ret) { |
| 237 | break; |
| 238 | } |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 239 | if (rdData[0] & IVTV_MBOX_FIRMWARE_DONE) { |
| 240 | break; |
| 241 | } |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 242 | if (rdData[0] && (poll_count < 1000)) continue; |
| 243 | if (!rdData[0]) { |
| 244 | retry_flag = !0; |
| 245 | pvr2_trace( |
| 246 | PVR2_TRACE_ERROR_LEGS, |
| 247 | "Encoder timed out waiting for us" |
| 248 | "; arranging to retry"); |
| 249 | } else { |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 250 | pvr2_trace( |
| 251 | PVR2_TRACE_ERROR_LEGS, |
| 252 | "***WARNING*** device's encoder" |
| 253 | " appears to be stuck" |
| 254 | " (status=0%08x)",rdData[0]); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 255 | } |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 256 | pvr2_trace( |
| 257 | PVR2_TRACE_ERROR_LEGS, |
| 258 | "Encoder command: 0x%02x",cmd); |
| 259 | for (idx = 4; idx < arg_cnt_send; idx++) { |
| 260 | pvr2_trace( |
| 261 | PVR2_TRACE_ERROR_LEGS, |
| 262 | "Encoder arg%d: 0x%08x", |
| 263 | idx-3,wrData[idx]); |
| 264 | } |
| 265 | ret = -EBUSY; |
| 266 | break; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 267 | } |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 268 | if (retry_flag) { |
| 269 | if (try_count < 20) continue; |
| 270 | pvr2_trace( |
| 271 | PVR2_TRACE_ERROR_LEGS, |
| 272 | "Too many retries..."); |
| 273 | ret = -EBUSY; |
| 274 | } |
| 275 | if (ret) { |
| 276 | pvr2_trace( |
| 277 | PVR2_TRACE_ERROR_LEGS, |
| 278 | "Giving up on command." |
| 279 | " It is likely that" |
| 280 | " this is a bad idea..."); |
| 281 | break; |
| 282 | } |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 283 | wrData[0] = 0x7; |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 284 | for (idx = 0; idx < arg_cnt_recv; idx++) { |
| 285 | argp[idx] = rdData[idx+4]; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | wrData[0] = 0x0; |
Mike Isely | c43000e | 2007-01-28 15:41:12 -0300 | [diff] [blame] | 289 | ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 290 | if (ret) break; |
| 291 | |
| 292 | } while(0); LOCK_GIVE(hdw->ctl_lock); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 297 | |
| 298 | static int pvr2_encoder_vcmd(struct pvr2_hdw *hdw, int cmd, |
| 299 | int args, ...) |
| 300 | { |
| 301 | va_list vl; |
| 302 | unsigned int idx; |
| 303 | u32 data[12]; |
| 304 | |
Ahmed S. Darwish | eca8ebf | 2007-01-20 00:35:03 -0300 | [diff] [blame] | 305 | if (args > ARRAY_SIZE(data)) { |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 306 | pvr2_trace( |
| 307 | PVR2_TRACE_ERROR_LEGS, |
| 308 | "Failed to write cx23416 command" |
| 309 | " - too many arguments" |
Mauro Carvalho Chehab | 69b04f0 | 2007-01-21 22:02:35 -0300 | [diff] [blame] | 310 | " (was given %u limit %lu)", |
| 311 | args, (long unsigned) ARRAY_SIZE(data)); |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 312 | return -EINVAL; |
| 313 | } |
| 314 | |
| 315 | va_start(vl, args); |
| 316 | for (idx = 0; idx < args; idx++) { |
| 317 | data[idx] = va_arg(vl, u32); |
| 318 | } |
| 319 | va_end(vl); |
| 320 | |
| 321 | return pvr2_encoder_cmd(hdw,cmd,args,0,data); |
| 322 | } |
| 323 | |
Mike Isely | 6fe7d2c | 2007-01-28 15:42:56 -0300 | [diff] [blame^] | 324 | |
| 325 | /* This implements some extra setup for the encoder that seems to be |
| 326 | specific to the PVR USB2 hardware. */ |
| 327 | int pvr2_encoder_prep_config(struct pvr2_hdw *hdw) |
| 328 | { |
| 329 | int ret = 0; |
| 330 | int encMisc3Arg = 0; |
| 331 | |
| 332 | #if 0 |
| 333 | /* This inexplicable bit happens in the Hauppage windows |
| 334 | driver (for both 24xxx and 29xxx devices). However I |
| 335 | currently see no difference in behavior with or without |
| 336 | this stuff. Leave this here as a note of its existence, |
| 337 | but don't use it. */ |
| 338 | LOCK_TAKE(hdw->ctl_lock); do { |
| 339 | u32 dat[1]; |
| 340 | dat[0] = 0x80000640; |
| 341 | pvr2_encoder_write_words(hdw,0x01fe,dat,1); |
| 342 | pvr2_encoder_write_words(hdw,0x023e,dat,1); |
| 343 | } while(0); LOCK_GIVE(hdw->ctl_lock); |
| 344 | #endif |
| 345 | |
| 346 | /* Mike Isely <isely@pobox.com> 26-Jan-2006 The windows driver |
| 347 | sends the following list of ENC_MISC commands (for both |
| 348 | 24xxx and 29xxx devices). Meanings are not entirely clear, |
| 349 | however without the ENC_MISC(3,1) command then we risk |
| 350 | random perpetual video corruption whenever the video input |
| 351 | breaks up for a moment (like when switching channels). */ |
| 352 | |
| 353 | |
| 354 | #if 0 |
| 355 | /* This ENC_MISC(5,0) command seems to hurt 29xxx sync |
| 356 | performance on channel changes, but is not a problem on |
| 357 | 24xxx devices. */ |
| 358 | ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 5,0,0,0); |
| 359 | #endif |
| 360 | |
| 361 | /* This ENC_MISC(3,encMisc3Arg) command is critical - without |
| 362 | it there will eventually be video corruption. Also, the |
| 363 | 29xxx case is strange - the Windows driver is passing 1 |
| 364 | regardless of device type but if we have 1 for 29xxx device |
| 365 | the video turns sluggish. */ |
| 366 | switch (hdw->hdw_type) { |
| 367 | case PVR2_HDW_TYPE_24XXX: encMisc3Arg = 1; break; |
| 368 | case PVR2_HDW_TYPE_29XXX: encMisc3Arg = 0; break; |
| 369 | default: break; |
| 370 | } |
| 371 | ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 3, |
| 372 | encMisc3Arg,0,0); |
| 373 | |
| 374 | ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 8,0,0,0); |
| 375 | |
| 376 | #if 0 |
| 377 | /* This ENC_MISC(4,1) command is poisonous, so it is commented |
| 378 | out. But I'm leaving it here anyway to document its |
| 379 | existence in the Windows driver. The effect of this |
| 380 | command is that apps displaying the stream become sluggish |
| 381 | with stuttering video. */ |
| 382 | ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 4,1,0,0); |
| 383 | #endif |
| 384 | |
| 385 | ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 0,3,0,0); |
| 386 | ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4,15,0,0,0); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 391 | int pvr2_encoder_configure(struct pvr2_hdw *hdw) |
| 392 | { |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 393 | int ret; |
| 394 | pvr2_trace(PVR2_TRACE_ENCODER,"pvr2_encoder_configure" |
| 395 | " (cx2341x module)"); |
| 396 | hdw->enc_ctl_state.port = CX2341X_PORT_STREAMING; |
| 397 | hdw->enc_ctl_state.width = hdw->res_hor_val; |
| 398 | hdw->enc_ctl_state.height = hdw->res_ver_val; |
| 399 | hdw->enc_ctl_state.is_50hz = ((hdw->std_mask_cur & |
| 400 | (V4L2_STD_NTSC|V4L2_STD_PAL_M)) ? |
| 401 | 0 : 1); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 402 | |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 403 | ret = 0; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 404 | |
Mike Isely | 6fe7d2c | 2007-01-28 15:42:56 -0300 | [diff] [blame^] | 405 | ret |= pvr2_encoder_prep_config(hdw); |
| 406 | |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 407 | if (!ret) ret = pvr2_encoder_vcmd( |
| 408 | hdw,CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, |
| 409 | 0xf0, 0xf0); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 410 | |
| 411 | /* setup firmware to notify us about some events (don't know why...) */ |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 412 | if (!ret) ret = pvr2_encoder_vcmd( |
| 413 | hdw,CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, |
| 414 | 0, 0, 0x10000000, 0xffffffff); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 415 | |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 416 | if (!ret) ret = pvr2_encoder_vcmd( |
| 417 | hdw,CX2341X_ENC_SET_VBI_LINE, 5, |
| 418 | 0xffffffff,0,0,0,0); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 419 | |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 420 | if (ret) { |
| 421 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
Mauro Carvalho Chehab | 58f56cb | 2006-08-30 05:44:31 -0300 | [diff] [blame] | 422 | "Failed to configure cx23416"); |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 423 | return ret; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 424 | } |
| 425 | |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 426 | ret = cx2341x_update(hdw,pvr2_encoder_cmd, |
Mike Isely | a0fd1cb | 2006-06-30 11:35:28 -0300 | [diff] [blame] | 427 | (hdw->enc_cur_valid ? &hdw->enc_cur_state : NULL), |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 428 | &hdw->enc_ctl_state); |
| 429 | if (ret) { |
| 430 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 431 | "Error from cx2341x module code=%d",ret); |
| 432 | return ret; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 433 | } |
| 434 | |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 435 | ret = 0; |
| 436 | |
| 437 | if (!ret) ret = pvr2_encoder_vcmd( |
| 438 | hdw, CX2341X_ENC_INITIALIZE_INPUT, 0); |
| 439 | |
| 440 | if (ret) { |
| 441 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
Mauro Carvalho Chehab | 58f56cb | 2006-08-30 05:44:31 -0300 | [diff] [blame] | 442 | "Failed to initialize cx23416 video input"); |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG); |
| 447 | memcpy(&hdw->enc_cur_state,&hdw->enc_ctl_state, |
| 448 | sizeof(struct cx2341x_mpeg_params)); |
| 449 | hdw->enc_cur_valid = !0; |
| 450 | return 0; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 451 | } |
| 452 | |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 453 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 454 | int pvr2_encoder_start(struct pvr2_hdw *hdw) |
| 455 | { |
| 456 | int status; |
| 457 | |
| 458 | /* unmask some interrupts */ |
| 459 | pvr2_write_register(hdw, 0x0048, 0xbfffffff); |
| 460 | |
| 461 | /* change some GPIO data */ |
| 462 | pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000481); |
| 463 | pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000); |
| 464 | |
Pantelis Koukousoulas | 275b2e2 | 2006-12-27 23:05:19 -0300 | [diff] [blame] | 465 | pvr2_encoder_vcmd(hdw,CX2341X_ENC_MUTE_VIDEO,1, |
| 466 | hdw->input_val == PVR2_CVAL_INPUT_RADIO ? 1 : 0); |
| 467 | |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 468 | switch (hdw->config) { |
| 469 | case pvr2_config_vbi: |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 470 | status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2, |
| 471 | 0x01,0x14); |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 472 | break; |
| 473 | case pvr2_config_mpeg: |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 474 | status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2, |
| 475 | 0,0x13); |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 476 | break; |
| 477 | default: /* Unhandled cases for now */ |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 478 | status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2, |
| 479 | 0,0x13); |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 480 | break; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 481 | } |
| 482 | if (!status) { |
| 483 | hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_RUN); |
| 484 | } |
| 485 | return status; |
| 486 | } |
| 487 | |
| 488 | int pvr2_encoder_stop(struct pvr2_hdw *hdw) |
| 489 | { |
| 490 | int status; |
| 491 | |
| 492 | /* mask all interrupts */ |
| 493 | pvr2_write_register(hdw, 0x0048, 0xffffffff); |
| 494 | |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 495 | switch (hdw->config) { |
| 496 | case pvr2_config_vbi: |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 497 | status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3, |
| 498 | 0x01,0x01,0x14); |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 499 | break; |
| 500 | case pvr2_config_mpeg: |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 501 | status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3, |
| 502 | 0x01,0,0x13); |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 503 | break; |
| 504 | default: /* Unhandled cases for now */ |
Mike Isely | eacbe7c | 2006-06-25 20:04:06 -0300 | [diff] [blame] | 505 | status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3, |
| 506 | 0x01,0,0x13); |
Mike Isely | 16eb40d | 2006-12-30 18:27:32 -0300 | [diff] [blame] | 507 | break; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* change some GPIO data */ |
| 511 | /* Note: Bit d7 of dir appears to control the LED. So we shut it |
| 512 | off here. */ |
| 513 | pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000401); |
| 514 | pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000); |
| 515 | |
| 516 | if (!status) { |
| 517 | hdw->subsys_enabled_mask &= ~(1<<PVR2_SUBSYS_B_ENC_RUN); |
| 518 | } |
| 519 | return status; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | /* |
| 524 | Stuff for Emacs to see, in order to encourage consistent editing style: |
| 525 | *** Local Variables: *** |
| 526 | *** mode: c *** |
| 527 | *** fill-column: 70 *** |
| 528 | *** tab-width: 8 *** |
| 529 | *** c-basic-offset: 8 *** |
| 530 | *** End: *** |
| 531 | */ |