Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1 | /* |
| 2 | * cx18 driver initialization and card probing |
| 3 | * |
| 4 | * Derived from ivtv-driver.c |
| 5 | * |
| 6 | * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> |
Andy Walls | c641d09 | 2008-09-01 00:40:41 -0300 | [diff] [blame] | 7 | * Copyright (C) 2008 Andy Walls <awalls@radix.net> |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 22 | * 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include "cx18-driver.h" |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 26 | #include "cx18-io.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 27 | #include "cx18-version.h" |
| 28 | #include "cx18-cards.h" |
| 29 | #include "cx18-i2c.h" |
| 30 | #include "cx18-irq.h" |
| 31 | #include "cx18-gpio.h" |
| 32 | #include "cx18-firmware.h" |
| 33 | #include "cx18-streams.h" |
| 34 | #include "cx18-av-core.h" |
| 35 | #include "cx18-scb.h" |
| 36 | #include "cx18-mailbox.h" |
| 37 | #include "cx18-ioctl.h" |
| 38 | #include "tuner-xc2028.h" |
| 39 | |
| 40 | #include <media/tveeprom.h> |
| 41 | |
| 42 | |
| 43 | /* var to keep track of the number of array elements in use */ |
| 44 | int cx18_cards_active; |
| 45 | |
| 46 | /* If you have already X v4l cards, then set this to X. This way |
| 47 | the device numbers stay matched. Example: you have a WinTV card |
| 48 | without radio and a Compro H900 with. Normally this would give a |
| 49 | video1 device together with a radio0 device for the Compro. By |
| 50 | setting this to 1 you ensure that radio0 is now also radio1. */ |
| 51 | int cx18_first_minor; |
| 52 | |
| 53 | /* Master variable for all cx18 info */ |
| 54 | struct cx18 *cx18_cards[CX18_MAX_CARDS]; |
| 55 | |
| 56 | /* Protects cx18_cards_active */ |
| 57 | DEFINE_SPINLOCK(cx18_cards_lock); |
| 58 | |
| 59 | /* add your revision and whatnot here */ |
| 60 | static struct pci_device_id cx18_pci_tbl[] __devinitdata = { |
| 61 | {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418, |
| 62 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
| 63 | {0,} |
| 64 | }; |
| 65 | |
| 66 | MODULE_DEVICE_TABLE(pci, cx18_pci_tbl); |
| 67 | |
| 68 | /* Parameter declarations */ |
| 69 | static int cardtype[CX18_MAX_CARDS]; |
| 70 | static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1, |
| 71 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 72 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 73 | -1, -1, -1, -1, -1, -1, -1, -1 }; |
| 74 | static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1, |
| 75 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 76 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 77 | -1, -1, -1, -1, -1, -1, -1, -1 }; |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 78 | static unsigned cardtype_c = 1; |
| 79 | static unsigned tuner_c = 1; |
| 80 | static unsigned radio_c = 1; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 81 | static char pal[] = "--"; |
| 82 | static char secam[] = "--"; |
| 83 | static char ntsc[] = "-"; |
| 84 | |
| 85 | /* Buffers */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 86 | static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 87 | static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS; |
| 88 | static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 89 | static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS; |
| 90 | static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS; |
| 91 | static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS; |
| 92 | |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 93 | static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE; |
| 94 | static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE; |
| 95 | static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE; |
| 96 | static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE; |
| 97 | /* VBI bufsize based on standards supported by card tuner for now */ |
| 98 | static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE; |
| 99 | |
| 100 | static int enc_ts_bufs = -1; |
| 101 | static int enc_mpg_bufs = -1; |
| 102 | static int enc_idx_bufs = -1; |
| 103 | static int enc_yuv_bufs = -1; |
| 104 | static int enc_vbi_bufs = -1; |
| 105 | static int enc_pcm_bufs = -1; |
| 106 | |
| 107 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 108 | static int cx18_pci_latency = 1; |
| 109 | |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 110 | static int mmio_ndelay; |
| 111 | static int retry_mmio = 1; |
| 112 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 113 | int cx18_debug; |
| 114 | |
| 115 | module_param_array(tuner, int, &tuner_c, 0644); |
| 116 | module_param_array(radio, bool, &radio_c, 0644); |
| 117 | module_param_array(cardtype, int, &cardtype_c, 0644); |
| 118 | module_param_string(pal, pal, sizeof(pal), 0644); |
| 119 | module_param_string(secam, secam, sizeof(secam), 0644); |
| 120 | module_param_string(ntsc, ntsc, sizeof(ntsc), 0644); |
| 121 | module_param_named(debug, cx18_debug, int, 0644); |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 122 | module_param(mmio_ndelay, int, 0644); |
| 123 | module_param(retry_mmio, int, 0644); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 124 | module_param(cx18_pci_latency, int, 0644); |
| 125 | module_param(cx18_first_minor, int, 0644); |
| 126 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 127 | module_param(enc_ts_buffers, int, 0644); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 128 | module_param(enc_mpg_buffers, int, 0644); |
| 129 | module_param(enc_idx_buffers, int, 0644); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 130 | module_param(enc_yuv_buffers, int, 0644); |
| 131 | module_param(enc_vbi_buffers, int, 0644); |
| 132 | module_param(enc_pcm_buffers, int, 0644); |
| 133 | |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 134 | module_param(enc_ts_bufsize, int, 0644); |
| 135 | module_param(enc_mpg_bufsize, int, 0644); |
| 136 | module_param(enc_idx_bufsize, int, 0644); |
| 137 | module_param(enc_yuv_bufsize, int, 0644); |
| 138 | /* VBI bufsize based on standards supported by card tuner for now */ |
| 139 | module_param(enc_pcm_bufsize, int, 0644); |
| 140 | |
| 141 | module_param(enc_ts_bufs, int, 0644); |
| 142 | module_param(enc_mpg_bufs, int, 0644); |
| 143 | module_param(enc_idx_bufs, int, 0644); |
| 144 | module_param(enc_yuv_bufs, int, 0644); |
| 145 | module_param(enc_vbi_bufs, int, 0644); |
| 146 | module_param(enc_pcm_bufs, int, 0644); |
| 147 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 148 | MODULE_PARM_DESC(tuner, "Tuner type selection,\n" |
| 149 | "\t\t\tsee tuner.h for values"); |
| 150 | MODULE_PARM_DESC(radio, |
| 151 | "Enable or disable the radio. Use only if autodetection\n" |
| 152 | "\t\t\tfails. 0 = disable, 1 = enable"); |
| 153 | MODULE_PARM_DESC(cardtype, |
| 154 | "Only use this option if your card is not detected properly.\n" |
| 155 | "\t\tSpecify card type:\n" |
| 156 | "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n" |
| 157 | "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n" |
| 158 | "\t\t\t 3 = Compro VideoMate H900\n" |
| 159 | "\t\t\t 4 = Yuan MPC718\n" |
Sri Deevi | 03c2808 | 2008-06-21 11:06:44 -0300 | [diff] [blame] | 160 | "\t\t\t 5 = Conexant Raptor PAL/SECAM\n" |
Andy Walls | 9eee4fb | 2008-10-04 20:28:40 -0300 | [diff] [blame] | 161 | "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n" |
Andy Walls | 6ce9ee5 | 2009-01-19 18:31:22 -0300 | [diff] [blame] | 162 | "\t\t\t 7 = Leadtek WinFast PVR2100/DVR3100 H\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 163 | "\t\t\t 0 = Autodetect (default)\n" |
| 164 | "\t\t\t-1 = Ignore this card\n\t\t"); |
| 165 | MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60"); |
| 166 | MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC"); |
| 167 | MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K"); |
| 168 | MODULE_PARM_DESC(debug, |
| 169 | "Debug level (bitmask). Default: 0\n" |
| 170 | "\t\t\t 1/0x0001: warning\n" |
| 171 | "\t\t\t 2/0x0002: info\n" |
| 172 | "\t\t\t 4/0x0004: mailbox\n" |
| 173 | "\t\t\t 8/0x0008: dma\n" |
| 174 | "\t\t\t 16/0x0010: ioctl\n" |
| 175 | "\t\t\t 32/0x0020: file\n" |
| 176 | "\t\t\t 64/0x0040: i2c\n" |
| 177 | "\t\t\t128/0x0080: irq\n" |
| 178 | "\t\t\t256/0x0100: high volume\n"); |
| 179 | MODULE_PARM_DESC(cx18_pci_latency, |
| 180 | "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n" |
| 181 | "\t\t\tDefault: Yes"); |
Andy Walls | d267d85 | 2008-09-28 21:46:02 -0300 | [diff] [blame] | 182 | MODULE_PARM_DESC(retry_mmio, |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 183 | "(Deprecated) MMIO writes are now always checked and retried\n" |
| 184 | "\t\t\tEffectively: 1 [Yes]"); |
Andy Walls | c641d09 | 2008-09-01 00:40:41 -0300 | [diff] [blame] | 185 | MODULE_PARM_DESC(mmio_ndelay, |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 186 | "(Deprecated) MMIO accesses are now never purposely delayed\n" |
| 187 | "\t\t\tEffectively: 0 ns"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 188 | MODULE_PARM_DESC(enc_ts_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 189 | "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 190 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 191 | MODULE_PARM_DESC(enc_ts_bufsize, |
| 192 | "Size of an encoder TS buffer (kB)\n" |
| 193 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE)); |
| 194 | MODULE_PARM_DESC(enc_ts_bufs, |
| 195 | "Number of encoder TS buffers\n" |
| 196 | "\t\t\tDefault is computed from other enc_ts_* parameters"); |
| 197 | MODULE_PARM_DESC(enc_mpg_buffers, |
| 198 | "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n" |
| 199 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS)); |
| 200 | MODULE_PARM_DESC(enc_mpg_bufsize, |
| 201 | "Size of an encoder MPG buffer (kB)\n" |
| 202 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE)); |
| 203 | MODULE_PARM_DESC(enc_mpg_bufs, |
| 204 | "Number of encoder MPG buffers\n" |
| 205 | "\t\t\tDefault is computed from other enc_mpg_* parameters"); |
| 206 | MODULE_PARM_DESC(enc_idx_buffers, |
| 207 | "Encoder IDX buffer memory (MB). (enc_idx_bufs can override)\n" |
| 208 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFFERS)); |
| 209 | MODULE_PARM_DESC(enc_idx_bufsize, |
| 210 | "Size of an encoder IDX buffer (kB)\n" |
| 211 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFSIZE)); |
| 212 | MODULE_PARM_DESC(enc_idx_bufs, |
| 213 | "Number of encoder IDX buffers\n" |
| 214 | "\t\t\tDefault is computed from other enc_idx_* parameters"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 215 | MODULE_PARM_DESC(enc_yuv_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 216 | "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 217 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 218 | MODULE_PARM_DESC(enc_yuv_bufsize, |
| 219 | "Size of an encoder YUV buffer (kB)\n" |
| 220 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFSIZE)); |
| 221 | MODULE_PARM_DESC(enc_yuv_bufs, |
| 222 | "Number of encoder YUV buffers\n" |
| 223 | "\t\t\tDefault is computed from other enc_yuv_* parameters"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 224 | MODULE_PARM_DESC(enc_vbi_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 225 | "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 226 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 227 | MODULE_PARM_DESC(enc_vbi_bufs, |
| 228 | "Number of encoder VBI buffers\n" |
| 229 | "\t\t\tDefault is computed from enc_vbi_buffers & tuner std"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 230 | MODULE_PARM_DESC(enc_pcm_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 231 | "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 232 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 233 | MODULE_PARM_DESC(enc_pcm_bufsize, |
| 234 | "Size of an encoder PCM buffer (kB)\n" |
| 235 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE)); |
| 236 | MODULE_PARM_DESC(enc_pcm_bufs, |
| 237 | "Number of encoder PCM buffers\n" |
| 238 | "\t\t\tDefault is computed from other enc_pcm_* parameters"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 239 | |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 240 | MODULE_PARM_DESC(cx18_first_minor, "Set kernel number assigned to first card"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 241 | |
| 242 | MODULE_AUTHOR("Hans Verkuil"); |
| 243 | MODULE_DESCRIPTION("CX23418 driver"); |
| 244 | MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder"); |
| 245 | MODULE_LICENSE("GPL"); |
| 246 | |
| 247 | MODULE_VERSION(CX18_VERSION); |
| 248 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 249 | /* Generic utility functions */ |
| 250 | int cx18_msleep_timeout(unsigned int msecs, int intr) |
| 251 | { |
Andy Walls | 330c6ec | 2008-11-08 14:19:37 -0300 | [diff] [blame] | 252 | long int timeout = msecs_to_jiffies(msecs); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 253 | int sig; |
| 254 | |
| 255 | do { |
| 256 | set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); |
| 257 | timeout = schedule_timeout(timeout); |
| 258 | sig = intr ? signal_pending(current) : 0; |
| 259 | } while (!sig && timeout); |
| 260 | return sig; |
| 261 | } |
| 262 | |
| 263 | /* Release ioremapped memory */ |
| 264 | static void cx18_iounmap(struct cx18 *cx) |
| 265 | { |
| 266 | if (cx == NULL) |
| 267 | return; |
| 268 | |
| 269 | /* Release io memory */ |
| 270 | if (cx->enc_mem != NULL) { |
| 271 | CX18_DEBUG_INFO("releasing enc_mem\n"); |
| 272 | iounmap(cx->enc_mem); |
| 273 | cx->enc_mem = NULL; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* Hauppauge card? get values from tveeprom */ |
| 278 | void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv) |
| 279 | { |
| 280 | u8 eedata[256]; |
| 281 | |
| 282 | cx->i2c_client[0].addr = 0xA0 >> 1; |
| 283 | tveeprom_read(&cx->i2c_client[0], eedata, sizeof(eedata)); |
| 284 | tveeprom_hauppauge_analog(&cx->i2c_client[0], tv, eedata); |
| 285 | } |
| 286 | |
| 287 | static void cx18_process_eeprom(struct cx18 *cx) |
| 288 | { |
| 289 | struct tveeprom tv; |
| 290 | |
| 291 | cx18_read_eeprom(cx, &tv); |
| 292 | |
| 293 | /* Many thanks to Steven Toth from Hauppauge for providing the |
| 294 | model numbers */ |
Hans Verkuil | 1d08160 | 2008-05-12 14:45:19 -0300 | [diff] [blame] | 295 | /* Note: the Samsung memory models cannot be reliably determined |
| 296 | from the model number. Use the cardtype module option if you |
| 297 | have one of these preproduction models. */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 298 | switch (tv.model) { |
Hans Verkuil | 1d08160 | 2008-05-12 14:45:19 -0300 | [diff] [blame] | 299 | case 74000 ... 74999: |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 300 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
| 301 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 302 | case 0: |
| 303 | CX18_ERR("Invalid EEPROM\n"); |
| 304 | return; |
| 305 | default: |
| 306 | CX18_ERR("Unknown model %d, defaulting to HVR-1600\n", tv.model); |
| 307 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | cx->v4l2_cap = cx->card->v4l2_capabilities; |
| 312 | cx->card_name = cx->card->name; |
| 313 | cx->card_i2c = cx->card->i2c; |
| 314 | |
| 315 | CX18_INFO("Autodetected %s\n", cx->card_name); |
| 316 | |
| 317 | if (tv.tuner_type == TUNER_ABSENT) |
| 318 | CX18_ERR("tveeprom cannot autodetect tuner!"); |
| 319 | |
| 320 | if (cx->options.tuner == -1) |
| 321 | cx->options.tuner = tv.tuner_type; |
| 322 | if (cx->options.radio == -1) |
| 323 | cx->options.radio = (tv.has_radio != 0); |
| 324 | |
| 325 | if (cx->std != 0) |
| 326 | /* user specified tuner standard */ |
| 327 | return; |
| 328 | |
| 329 | /* autodetect tuner standard */ |
| 330 | if (tv.tuner_formats & V4L2_STD_PAL) { |
| 331 | CX18_DEBUG_INFO("PAL tuner detected\n"); |
| 332 | cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H; |
| 333 | } else if (tv.tuner_formats & V4L2_STD_NTSC) { |
| 334 | CX18_DEBUG_INFO("NTSC tuner detected\n"); |
| 335 | cx->std |= V4L2_STD_NTSC_M; |
| 336 | } else if (tv.tuner_formats & V4L2_STD_SECAM) { |
| 337 | CX18_DEBUG_INFO("SECAM tuner detected\n"); |
| 338 | cx->std |= V4L2_STD_SECAM_L; |
| 339 | } else { |
| 340 | CX18_INFO("No tuner detected, default to NTSC-M\n"); |
| 341 | cx->std |= V4L2_STD_NTSC_M; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static v4l2_std_id cx18_parse_std(struct cx18 *cx) |
| 346 | { |
| 347 | switch (pal[0]) { |
| 348 | case '6': |
| 349 | return V4L2_STD_PAL_60; |
| 350 | case 'b': |
| 351 | case 'B': |
| 352 | case 'g': |
| 353 | case 'G': |
| 354 | return V4L2_STD_PAL_BG; |
| 355 | case 'h': |
| 356 | case 'H': |
| 357 | return V4L2_STD_PAL_H; |
| 358 | case 'n': |
| 359 | case 'N': |
| 360 | if (pal[1] == 'c' || pal[1] == 'C') |
| 361 | return V4L2_STD_PAL_Nc; |
| 362 | return V4L2_STD_PAL_N; |
| 363 | case 'i': |
| 364 | case 'I': |
| 365 | return V4L2_STD_PAL_I; |
| 366 | case 'd': |
| 367 | case 'D': |
| 368 | case 'k': |
| 369 | case 'K': |
| 370 | return V4L2_STD_PAL_DK; |
| 371 | case 'M': |
| 372 | case 'm': |
| 373 | return V4L2_STD_PAL_M; |
| 374 | case '-': |
| 375 | break; |
| 376 | default: |
| 377 | CX18_WARN("pal= argument not recognised\n"); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | switch (secam[0]) { |
| 382 | case 'b': |
| 383 | case 'B': |
| 384 | case 'g': |
| 385 | case 'G': |
| 386 | case 'h': |
| 387 | case 'H': |
| 388 | return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H; |
| 389 | case 'd': |
| 390 | case 'D': |
| 391 | case 'k': |
| 392 | case 'K': |
| 393 | return V4L2_STD_SECAM_DK; |
| 394 | case 'l': |
| 395 | case 'L': |
| 396 | if (secam[1] == 'C' || secam[1] == 'c') |
| 397 | return V4L2_STD_SECAM_LC; |
| 398 | return V4L2_STD_SECAM_L; |
| 399 | case '-': |
| 400 | break; |
| 401 | default: |
| 402 | CX18_WARN("secam= argument not recognised\n"); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | switch (ntsc[0]) { |
| 407 | case 'm': |
| 408 | case 'M': |
| 409 | return V4L2_STD_NTSC_M; |
| 410 | case 'j': |
| 411 | case 'J': |
| 412 | return V4L2_STD_NTSC_M_JP; |
| 413 | case 'k': |
| 414 | case 'K': |
| 415 | return V4L2_STD_NTSC_M_KR; |
| 416 | case '-': |
| 417 | break; |
| 418 | default: |
| 419 | CX18_WARN("ntsc= argument not recognised\n"); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /* no match found */ |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static void cx18_process_options(struct cx18 *cx) |
| 428 | { |
| 429 | int i, j; |
| 430 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 431 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 432 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers; |
| 433 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 434 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers; |
| 435 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers; |
| 436 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 437 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */ |
| 438 | |
| 439 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs; |
| 440 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs; |
| 441 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs; |
| 442 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs; |
| 443 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs; |
| 444 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs; |
| 445 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */ |
| 446 | |
| 447 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize; |
| 448 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize; |
| 449 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize; |
| 450 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize; |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame^] | 451 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 452 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize; |
| 453 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */ |
| 454 | |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame^] | 455 | /* Ensure stream_buffers & stream_buf_size are valid */ |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 456 | for (i = 0; i < CX18_MAX_STREAMS; i++) { |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame^] | 457 | if (cx->stream_buffers[i] == 0 || /* User said 0 buffers */ |
| 458 | cx->options.megabytes[i] <= 0 || /* User said 0 MB total */ |
| 459 | cx->stream_buf_size[i] <= 0) { /* User said buf size 0 */ |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 460 | cx->options.megabytes[i] = 0; |
| 461 | cx->stream_buffers[i] = 0; |
| 462 | cx->stream_buf_size[i] = 0; |
| 463 | continue; |
| 464 | } |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame^] | 465 | /* |
| 466 | * VBI is a special case where the stream_buf_size is fixed |
| 467 | * and already in bytes |
| 468 | */ |
| 469 | if (i == CX18_ENC_STREAM_TYPE_VBI) { |
| 470 | if (cx->stream_buffers[i] < 0) { |
| 471 | cx->stream_buffers[i] = |
| 472 | cx->options.megabytes[i] * 1024 * 1024 |
| 473 | / cx->stream_buf_size[i]; |
| 474 | } else { |
| 475 | /* N.B. This might round down to 0 */ |
| 476 | cx->options.megabytes[i] = |
| 477 | cx->stream_buffers[i] |
| 478 | * cx->stream_buf_size[i]/(1024 * 1024); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 479 | } |
| 480 | continue; |
| 481 | } |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame^] | 482 | /* All other streams have stream_buf_size in kB at this point */ |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 483 | if (cx->stream_buffers[i] < 0) { |
| 484 | cx->stream_buffers[i] = cx->options.megabytes[i] * 1024 |
| 485 | / cx->stream_buf_size[i]; |
| 486 | } else { |
| 487 | /* N.B. This might round down to 0 */ |
| 488 | cx->options.megabytes[i] = |
| 489 | cx->stream_buffers[i] * cx->stream_buf_size[i] / 1024; |
| 490 | } |
| 491 | cx->stream_buf_size[i] *= 1024; /* convert from kB to bytes */ |
| 492 | } |
| 493 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 494 | cx->options.cardtype = cardtype[cx->num]; |
| 495 | cx->options.tuner = tuner[cx->num]; |
| 496 | cx->options.radio = radio[cx->num]; |
| 497 | |
| 498 | cx->std = cx18_parse_std(cx); |
| 499 | if (cx->options.cardtype == -1) { |
| 500 | CX18_INFO("Ignore card\n"); |
| 501 | return; |
| 502 | } |
| 503 | cx->card = cx18_get_card(cx->options.cardtype - 1); |
| 504 | if (cx->card) |
| 505 | CX18_INFO("User specified %s card\n", cx->card->name); |
| 506 | else if (cx->options.cardtype != 0) |
| 507 | CX18_ERR("Unknown user specified type, trying to autodetect card\n"); |
| 508 | if (cx->card == NULL) { |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 509 | if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 510 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
| 511 | CX18_INFO("Autodetected Hauppauge card\n"); |
| 512 | } |
| 513 | } |
| 514 | if (cx->card == NULL) { |
| 515 | for (i = 0; (cx->card = cx18_get_card(i)); i++) { |
| 516 | if (cx->card->pci_list == NULL) |
| 517 | continue; |
| 518 | for (j = 0; cx->card->pci_list[j].device; j++) { |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 519 | if (cx->pci_dev->device != |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 520 | cx->card->pci_list[j].device) |
| 521 | continue; |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 522 | if (cx->pci_dev->subsystem_vendor != |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 523 | cx->card->pci_list[j].subsystem_vendor) |
| 524 | continue; |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 525 | if (cx->pci_dev->subsystem_device != |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 526 | cx->card->pci_list[j].subsystem_device) |
| 527 | continue; |
| 528 | CX18_INFO("Autodetected %s card\n", cx->card->name); |
| 529 | goto done; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | done: |
| 534 | |
| 535 | if (cx->card == NULL) { |
| 536 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
Bjorn Helgaas | 29e66a6 | 2008-09-04 17:24:51 -0300 | [diff] [blame] | 537 | CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n", |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 538 | cx->pci_dev->vendor, cx->pci_dev->device); |
Bjorn Helgaas | 29e66a6 | 2008-09-04 17:24:51 -0300 | [diff] [blame] | 539 | CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n", |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 540 | cx->pci_dev->subsystem_vendor, |
| 541 | cx->pci_dev->subsystem_device); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 542 | CX18_ERR("Defaulting to %s card\n", cx->card->name); |
| 543 | CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n"); |
| 544 | CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n"); |
| 545 | CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n"); |
| 546 | } |
| 547 | cx->v4l2_cap = cx->card->v4l2_capabilities; |
| 548 | cx->card_name = cx->card->name; |
| 549 | cx->card_i2c = cx->card->i2c; |
| 550 | } |
| 551 | |
| 552 | /* Precondition: the cx18 structure has been memset to 0. Only |
| 553 | the dev and num fields have been filled in. |
| 554 | No assumptions on the card type may be made here (see cx18_init_struct2 |
| 555 | for that). |
| 556 | */ |
| 557 | static int __devinit cx18_init_struct1(struct cx18 *cx) |
| 558 | { |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 559 | int i; |
| 560 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 561 | cx->base_addr = pci_resource_start(cx->pci_dev, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 562 | |
| 563 | mutex_init(&cx->serialize_lock); |
| 564 | mutex_init(&cx->i2c_bus_lock[0]); |
| 565 | mutex_init(&cx->i2c_bus_lock[1]); |
Andy Walls | 8abdd00 | 2008-07-13 19:05:25 -0300 | [diff] [blame] | 566 | mutex_init(&cx->gpio_lock); |
Andy Walls | 72c2d6d | 2008-11-06 01:15:41 -0300 | [diff] [blame] | 567 | mutex_init(&cx->epu2apu_mb_lock); |
| 568 | mutex_init(&cx->epu2cpu_mb_lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 569 | |
Andy Walls | 572bfea | 2008-11-25 21:43:05 -0300 | [diff] [blame] | 570 | cx->work_queue = create_singlethread_workqueue(cx->name); |
| 571 | if (cx->work_queue == NULL) { |
| 572 | CX18_ERR("Unable to create work hander thread\n"); |
| 573 | return -ENOMEM; |
| 574 | } |
| 575 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 576 | for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) { |
| 577 | cx->epu_work_order[i].cx = cx; |
| 578 | cx->epu_work_order[i].str = cx->epu_debug_str; |
| 579 | INIT_WORK(&cx->epu_work_order[i].work, cx18_epu_work_handler); |
| 580 | } |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 581 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 582 | /* start counting open_id at 1 */ |
| 583 | cx->open_id = 1; |
| 584 | |
| 585 | /* Initial settings */ |
| 586 | cx2341x_fill_defaults(&cx->params); |
| 587 | cx->temporal_strength = cx->params.video_temporal_filter; |
| 588 | cx->spatial_strength = cx->params.video_spatial_filter; |
| 589 | cx->filter_mode = cx->params.video_spatial_filter_mode | |
| 590 | (cx->params.video_temporal_filter_mode << 1) | |
| 591 | (cx->params.video_median_filter_type << 2); |
| 592 | cx->params.port = CX2341X_PORT_MEMORY; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 593 | cx->params.capabilities = |
| 594 | CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_AC3 | CX2341X_CAP_HAS_SLICED_VBI; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 595 | init_waitqueue_head(&cx->cap_w); |
| 596 | init_waitqueue_head(&cx->mb_apu_waitq); |
| 597 | init_waitqueue_head(&cx->mb_cpu_waitq); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 598 | init_waitqueue_head(&cx->dma_waitq); |
| 599 | |
| 600 | /* VBI */ |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 601 | cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 602 | cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced; |
Andy Walls | af009cf | 2008-12-12 20:00:29 -0300 | [diff] [blame] | 603 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /* Second initialization part. Here the card type has been |
| 608 | autodetected. */ |
| 609 | static void __devinit cx18_init_struct2(struct cx18 *cx) |
| 610 | { |
| 611 | int i; |
| 612 | |
| 613 | for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++) |
| 614 | if (cx->card->video_inputs[i].video_type == 0) |
| 615 | break; |
| 616 | cx->nof_inputs = i; |
| 617 | for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++) |
| 618 | if (cx->card->audio_inputs[i].audio_type == 0) |
| 619 | break; |
| 620 | cx->nof_audio_inputs = i; |
| 621 | |
| 622 | /* Find tuner input */ |
| 623 | for (i = 0; i < cx->nof_inputs; i++) { |
| 624 | if (cx->card->video_inputs[i].video_type == |
| 625 | CX18_CARD_INPUT_VID_TUNER) |
| 626 | break; |
| 627 | } |
| 628 | if (i == cx->nof_inputs) |
| 629 | i = 0; |
| 630 | cx->active_input = i; |
| 631 | cx->audio_input = cx->card->video_inputs[i].audio_index; |
| 632 | cx->av_state.vid_input = CX18_AV_COMPOSITE7; |
| 633 | cx->av_state.aud_input = CX18_AV_AUDIO8; |
| 634 | cx->av_state.audclk_freq = 48000; |
| 635 | cx->av_state.audmode = V4L2_TUNER_MODE_LANG1; |
| 636 | cx->av_state.vbi_line_offset = 8; |
| 637 | } |
| 638 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 639 | static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 640 | const struct pci_device_id *pci_id) |
| 641 | { |
| 642 | u16 cmd; |
| 643 | unsigned char pci_latency; |
| 644 | |
| 645 | CX18_DEBUG_INFO("Enabling pci device\n"); |
| 646 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 647 | if (pci_enable_device(pci_dev)) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 648 | CX18_ERR("Can't enable device %d!\n", cx->num); |
| 649 | return -EIO; |
| 650 | } |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 651 | if (pci_set_dma_mask(pci_dev, 0xffffffff)) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 652 | CX18_ERR("No suitable DMA available on card %d.\n", cx->num); |
| 653 | return -EIO; |
| 654 | } |
| 655 | if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) { |
| 656 | CX18_ERR("Cannot request encoder memory region on card %d.\n", cx->num); |
| 657 | return -EIO; |
| 658 | } |
| 659 | |
Andy Walls | 4519064 | 2008-08-29 16:10:21 -0300 | [diff] [blame] | 660 | /* Enable bus mastering and memory mapped IO for the CX23418 */ |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 661 | pci_read_config_word(pci_dev, PCI_COMMAND, &cmd); |
Andy Walls | 4519064 | 2008-08-29 16:10:21 -0300 | [diff] [blame] | 662 | cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 663 | pci_write_config_word(pci_dev, PCI_COMMAND, cmd); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 664 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 665 | pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &cx->card_rev); |
| 666 | pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 667 | |
| 668 | if (pci_latency < 64 && cx18_pci_latency) { |
| 669 | CX18_INFO("Unreasonably low latency timer, " |
| 670 | "setting to 64 (was %d)\n", pci_latency); |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 671 | pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64); |
| 672 | pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 673 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 674 | |
| 675 | CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, " |
| 676 | "irq: %d, latency: %d, memory: 0x%lx\n", |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 677 | cx->pci_dev->device, cx->card_rev, pci_dev->bus->number, |
| 678 | PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn), |
| 679 | cx->pci_dev->irq, pci_latency, (unsigned long)cx->base_addr); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
Hans Verkuil | 6a4a793 | 2008-05-01 09:34:54 -0300 | [diff] [blame] | 684 | #ifdef MODULE |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 685 | static u32 cx18_request_module(struct cx18 *cx, u32 hw, |
| 686 | const char *name, u32 id) |
| 687 | { |
| 688 | if ((hw & id) == 0) |
| 689 | return hw; |
Andy Walls | be411df | 2009-01-17 13:37:36 -0300 | [diff] [blame] | 690 | if (request_module("%s", name) != 0) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 691 | CX18_ERR("Failed to load module %s\n", name); |
| 692 | return hw & ~id; |
| 693 | } |
| 694 | CX18_DEBUG_INFO("Loaded module %s\n", name); |
| 695 | return hw; |
| 696 | } |
Hans Verkuil | 6a4a793 | 2008-05-01 09:34:54 -0300 | [diff] [blame] | 697 | #endif |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 698 | |
| 699 | static void cx18_load_and_init_modules(struct cx18 *cx) |
| 700 | { |
| 701 | u32 hw = cx->card->hw_all; |
| 702 | int i; |
| 703 | |
Hans Verkuil | 6a4a793 | 2008-05-01 09:34:54 -0300 | [diff] [blame] | 704 | #ifdef MODULE |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 705 | /* load modules */ |
Hans Verkuil | 58ae1c2 | 2008-11-03 08:06:51 -0300 | [diff] [blame] | 706 | #ifdef CONFIG_MEDIA_TUNER_MODULE |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 707 | hw = cx18_request_module(cx, hw, "tuner", CX18_HW_TUNER); |
| 708 | #endif |
Hans Verkuil | 58ae1c2 | 2008-11-03 08:06:51 -0300 | [diff] [blame] | 709 | #ifdef CONFIG_VIDEO_CS5345_MODULE |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 710 | hw = cx18_request_module(cx, hw, "cs5345", CX18_HW_CS5345); |
| 711 | #endif |
Hans Verkuil | 6a4a793 | 2008-05-01 09:34:54 -0300 | [diff] [blame] | 712 | #endif |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 713 | |
| 714 | /* check which i2c devices are actually found */ |
| 715 | for (i = 0; i < 32; i++) { |
| 716 | u32 device = 1 << i; |
| 717 | |
| 718 | if (!(device & hw)) |
| 719 | continue; |
| 720 | if (device == CX18_HW_GPIO || device == CX18_HW_TVEEPROM || |
| 721 | device == CX18_HW_CX23418 || device == CX18_HW_DVB) { |
| 722 | /* These 'devices' do not use i2c probing */ |
| 723 | cx->hw_flags |= device; |
| 724 | continue; |
| 725 | } |
| 726 | cx18_i2c_register(cx, i); |
| 727 | if (cx18_i2c_hw_addr(cx, device) > 0) |
| 728 | cx->hw_flags |= device; |
| 729 | } |
| 730 | |
| 731 | hw = cx->hw_flags; |
| 732 | } |
| 733 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 734 | static int __devinit cx18_probe(struct pci_dev *pci_dev, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 735 | const struct pci_device_id *pci_id) |
| 736 | { |
| 737 | int retval = 0; |
Andy Walls | ff08657 | 2008-10-18 08:51:28 -0300 | [diff] [blame] | 738 | int i; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 739 | u32 devtype; |
| 740 | struct cx18 *cx; |
| 741 | |
| 742 | spin_lock(&cx18_cards_lock); |
| 743 | |
| 744 | /* Make sure we've got a place for this card */ |
| 745 | if (cx18_cards_active == CX18_MAX_CARDS) { |
| 746 | printk(KERN_ERR "cx18: Maximum number of cards detected (%d).\n", |
| 747 | cx18_cards_active); |
| 748 | spin_unlock(&cx18_cards_lock); |
| 749 | return -ENOMEM; |
| 750 | } |
| 751 | |
| 752 | cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC); |
Harvey Harrison | cb6969e | 2008-05-06 20:42:32 -0700 | [diff] [blame] | 753 | if (!cx) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 754 | spin_unlock(&cx18_cards_lock); |
| 755 | return -ENOMEM; |
| 756 | } |
| 757 | cx18_cards[cx18_cards_active] = cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 758 | cx->num = cx18_cards_active++; |
Jean Delvare | b4ac3c8 | 2008-05-13 18:27:15 -0300 | [diff] [blame] | 759 | snprintf(cx->name, sizeof(cx->name), "cx18-%d", cx->num); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 760 | CX18_INFO("Initializing card #%d\n", cx->num); |
| 761 | |
| 762 | spin_unlock(&cx18_cards_lock); |
| 763 | |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 764 | cx->pci_dev = pci_dev; |
| 765 | retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev); |
| 766 | if (retval) { |
| 767 | CX18_ERR("Call to v4l2_device_register() failed\n"); |
| 768 | goto err; |
| 769 | } |
| 770 | CX18_DEBUG_INFO("registered v4l2_device name: %s\n", cx->v4l2_dev.name); |
| 771 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 772 | cx18_process_options(cx); |
| 773 | if (cx->options.cardtype == -1) { |
| 774 | retval = -ENODEV; |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 775 | goto unregister_v4l2; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 776 | } |
| 777 | if (cx18_init_struct1(cx)) { |
| 778 | retval = -ENOMEM; |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 779 | goto unregister_v4l2; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr); |
| 783 | |
| 784 | /* PCI Device Setup */ |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 785 | retval = cx18_setup_pci(cx, pci_dev, pci_id); |
Andy Walls | 572bfea | 2008-11-25 21:43:05 -0300 | [diff] [blame] | 786 | if (retval != 0) |
| 787 | goto free_workqueue; |
| 788 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 789 | /* map io memory */ |
| 790 | CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n", |
| 791 | cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE); |
| 792 | cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET, |
| 793 | CX18_MEM_SIZE); |
| 794 | if (!cx->enc_mem) { |
| 795 | CX18_ERR("ioremap failed, perhaps increasing __VMALLOC_RESERVE in page.h\n"); |
| 796 | CX18_ERR("or disabling CONFIG_HIGHMEM4G into the kernel would help\n"); |
| 797 | retval = -ENOMEM; |
| 798 | goto free_mem; |
| 799 | } |
| 800 | cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET; |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 801 | devtype = cx18_read_reg(cx, 0xC72028); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 802 | switch (devtype & 0xff000000) { |
| 803 | case 0xff000000: |
| 804 | CX18_INFO("cx23418 revision %08x (A)\n", devtype); |
| 805 | break; |
| 806 | case 0x01000000: |
| 807 | CX18_INFO("cx23418 revision %08x (B)\n", devtype); |
| 808 | break; |
| 809 | default: |
| 810 | CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype); |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | cx18_init_power(cx, 1); |
| 815 | cx18_init_memory(cx); |
| 816 | |
Al Viro | 990c81c | 2008-05-21 00:32:01 -0300 | [diff] [blame] | 817 | cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 818 | cx18_init_scb(cx); |
| 819 | |
| 820 | cx18_gpio_init(cx); |
| 821 | |
| 822 | /* active i2c */ |
| 823 | CX18_DEBUG_INFO("activating i2c...\n"); |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 824 | retval = init_cx18_i2c(cx); |
| 825 | if (retval) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 826 | CX18_ERR("Could not initialize i2c\n"); |
| 827 | goto free_map; |
| 828 | } |
| 829 | |
| 830 | CX18_DEBUG_INFO("Active card count: %d.\n", cx18_cards_active); |
| 831 | |
| 832 | if (cx->card->hw_all & CX18_HW_TVEEPROM) { |
| 833 | /* Based on the model number the cardtype may be changed. |
| 834 | The PCI IDs are not always reliable. */ |
| 835 | cx18_process_eeprom(cx); |
| 836 | } |
| 837 | if (cx->card->comment) |
| 838 | CX18_INFO("%s", cx->card->comment); |
| 839 | if (cx->card->v4l2_capabilities == 0) { |
| 840 | retval = -ENODEV; |
| 841 | goto free_i2c; |
| 842 | } |
| 843 | cx18_init_memory(cx); |
Andy Walls | fd6b9c9 | 2008-12-14 21:26:25 -0300 | [diff] [blame] | 844 | cx18_init_scb(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 845 | |
| 846 | /* Register IRQ */ |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 847 | retval = request_irq(cx->pci_dev->irq, cx18_irq_handler, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 848 | IRQF_SHARED | IRQF_DISABLED, cx->name, (void *)cx); |
| 849 | if (retval) { |
| 850 | CX18_ERR("Failed to register irq %d\n", retval); |
| 851 | goto free_i2c; |
| 852 | } |
| 853 | |
| 854 | if (cx->std == 0) |
| 855 | cx->std = V4L2_STD_NTSC_M; |
| 856 | |
| 857 | if (cx->options.tuner == -1) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 858 | for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) { |
| 859 | if ((cx->std & cx->card->tuners[i].std) == 0) |
| 860 | continue; |
| 861 | cx->options.tuner = cx->card->tuners[i].tuner; |
| 862 | break; |
| 863 | } |
| 864 | } |
| 865 | /* if no tuner was found, then pick the first tuner in the card list */ |
| 866 | if (cx->options.tuner == -1 && cx->card->tuners[0].std) { |
| 867 | cx->std = cx->card->tuners[0].std; |
Hans Verkuil | c3cb4d9 | 2008-06-27 23:27:25 -0300 | [diff] [blame] | 868 | if (cx->std & V4L2_STD_PAL) |
| 869 | cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H; |
| 870 | else if (cx->std & V4L2_STD_NTSC) |
| 871 | cx->std = V4L2_STD_NTSC_M; |
| 872 | else if (cx->std & V4L2_STD_SECAM) |
| 873 | cx->std = V4L2_STD_SECAM_L; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 874 | cx->options.tuner = cx->card->tuners[0].tuner; |
| 875 | } |
| 876 | if (cx->options.radio == -1) |
| 877 | cx->options.radio = (cx->card->radio_input.audio_type != 0); |
| 878 | |
| 879 | /* The card is now fully identified, continue with card-specific |
| 880 | initialization. */ |
| 881 | cx18_init_struct2(cx); |
| 882 | |
| 883 | cx18_load_and_init_modules(cx); |
| 884 | |
| 885 | if (cx->std & V4L2_STD_525_60) { |
| 886 | cx->is_60hz = 1; |
| 887 | cx->is_out_60hz = 1; |
| 888 | } else { |
| 889 | cx->is_50hz = 1; |
| 890 | cx->is_out_50hz = 1; |
| 891 | } |
| 892 | cx->params.video_gop_size = cx->is_60hz ? 15 : 12; |
| 893 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 894 | if (cx->options.radio > 0) |
| 895 | cx->v4l2_cap |= V4L2_CAP_RADIO; |
| 896 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 897 | if (cx->options.tuner > -1) { |
| 898 | struct tuner_setup setup; |
| 899 | |
| 900 | setup.addr = ADDR_UNSET; |
| 901 | setup.type = cx->options.tuner; |
| 902 | setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */ |
| 903 | setup.tuner_callback = (setup.type == TUNER_XC2028) ? |
| 904 | cx18_reset_tuner_gpio : NULL; |
| 905 | cx18_call_i2c_clients(cx, TUNER_SET_TYPE_ADDR, &setup); |
| 906 | if (setup.type == TUNER_XC2028) { |
| 907 | static struct xc2028_ctrl ctrl = { |
| 908 | .fname = XC2028_DEFAULT_FIRMWARE, |
| 909 | .max_len = 64, |
| 910 | }; |
| 911 | struct v4l2_priv_tun_config cfg = { |
| 912 | .tuner = cx->options.tuner, |
| 913 | .priv = &ctrl, |
| 914 | }; |
| 915 | cx18_call_i2c_clients(cx, TUNER_SET_CONFIG, &cfg); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /* The tuner is fixed to the standard. The other inputs (e.g. S-Video) |
| 920 | are not. */ |
| 921 | cx->tuner_std = cx->std; |
| 922 | |
Hans Verkuil | 5e7fdc5 | 2008-05-30 10:51:53 -0300 | [diff] [blame] | 923 | retval = cx18_streams_setup(cx); |
| 924 | if (retval) { |
| 925 | CX18_ERR("Error %d setting up streams\n", retval); |
| 926 | goto free_irq; |
| 927 | } |
| 928 | retval = cx18_streams_register(cx); |
| 929 | if (retval) { |
| 930 | CX18_ERR("Error %d registering devices\n", retval); |
| 931 | goto free_streams; |
| 932 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 933 | |
| 934 | CX18_INFO("Initialized card #%d: %s\n", cx->num, cx->card_name); |
| 935 | |
| 936 | return 0; |
| 937 | |
| 938 | free_streams: |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 939 | cx18_streams_cleanup(cx, 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 940 | free_irq: |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 941 | free_irq(cx->pci_dev->irq, (void *)cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 942 | free_i2c: |
| 943 | exit_cx18_i2c(cx); |
| 944 | free_map: |
| 945 | cx18_iounmap(cx); |
| 946 | free_mem: |
| 947 | release_mem_region(cx->base_addr, CX18_MEM_SIZE); |
| 948 | free_workqueue: |
Andy Walls | 572bfea | 2008-11-25 21:43:05 -0300 | [diff] [blame] | 949 | destroy_workqueue(cx->work_queue); |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 950 | unregister_v4l2: |
| 951 | v4l2_device_unregister(&cx->v4l2_dev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 952 | err: |
| 953 | if (retval == 0) |
| 954 | retval = -ENODEV; |
| 955 | CX18_ERR("Error %d on initialization\n", retval); |
| 956 | |
Andy Walls | ff08657 | 2008-10-18 08:51:28 -0300 | [diff] [blame] | 957 | i = cx->num; |
| 958 | spin_lock(&cx18_cards_lock); |
| 959 | kfree(cx18_cards[i]); |
| 960 | cx18_cards[i] = NULL; |
| 961 | spin_unlock(&cx18_cards_lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 962 | return retval; |
| 963 | } |
| 964 | |
| 965 | int cx18_init_on_first_open(struct cx18 *cx) |
| 966 | { |
| 967 | int video_input; |
| 968 | int fw_retry_count = 3; |
| 969 | struct v4l2_frequency vf; |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 970 | struct cx18_open_id fh; |
| 971 | |
| 972 | fh.cx = cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 973 | |
| 974 | if (test_bit(CX18_F_I_FAILED, &cx->i_flags)) |
| 975 | return -ENXIO; |
| 976 | |
| 977 | if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags)) |
| 978 | return 0; |
| 979 | |
| 980 | while (--fw_retry_count > 0) { |
| 981 | /* load firmware */ |
| 982 | if (cx18_firmware_init(cx) == 0) |
| 983 | break; |
| 984 | if (fw_retry_count > 1) |
| 985 | CX18_WARN("Retry loading firmware\n"); |
| 986 | } |
| 987 | |
| 988 | if (fw_retry_count == 0) { |
| 989 | set_bit(CX18_F_I_FAILED, &cx->i_flags); |
| 990 | return -ENXIO; |
| 991 | } |
| 992 | set_bit(CX18_F_I_LOADED_FW, &cx->i_flags); |
| 993 | |
Andy Walls | 350145a | 2009-01-04 21:51:17 -0300 | [diff] [blame] | 994 | /* |
| 995 | * Init the firmware twice to work around a silicon bug |
| 996 | * with the digital TS. |
| 997 | * |
| 998 | * The second firmware load requires us to normalize the APU state, |
| 999 | * or the audio for the first analog capture will be badly incorrect. |
| 1000 | * |
| 1001 | * I can't seem to call APU_RESETAI and have it succeed without the |
| 1002 | * APU capturing audio, so we start and stop it here to do the reset |
| 1003 | */ |
| 1004 | |
| 1005 | /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */ |
| 1006 | cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0); |
| 1007 | cx18_vapi(cx, CX18_APU_RESETAI, 0); |
| 1008 | cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1009 | |
| 1010 | fw_retry_count = 3; |
| 1011 | while (--fw_retry_count > 0) { |
| 1012 | /* load firmware */ |
| 1013 | if (cx18_firmware_init(cx) == 0) |
| 1014 | break; |
| 1015 | if (fw_retry_count > 1) |
| 1016 | CX18_WARN("Retry loading firmware\n"); |
| 1017 | } |
| 1018 | |
| 1019 | if (fw_retry_count == 0) { |
| 1020 | set_bit(CX18_F_I_FAILED, &cx->i_flags); |
| 1021 | return -ENXIO; |
| 1022 | } |
| 1023 | |
Andy Walls | d5c02f6 | 2009-01-30 22:48:40 -0300 | [diff] [blame] | 1024 | /* |
| 1025 | * The second firmware load requires us to normalize the APU state, |
| 1026 | * or the audio for the first analog capture will be badly incorrect. |
| 1027 | * |
| 1028 | * I can't seem to call APU_RESETAI and have it succeed without the |
| 1029 | * APU capturing audio, so we start and stop it here to do the reset |
| 1030 | */ |
| 1031 | |
| 1032 | /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */ |
| 1033 | cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0); |
| 1034 | cx18_vapi(cx, CX18_APU_RESETAI, 0); |
| 1035 | cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG); |
| 1036 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1037 | vf.tuner = 0; |
| 1038 | vf.type = V4L2_TUNER_ANALOG_TV; |
| 1039 | vf.frequency = 6400; /* the tuner 'baseline' frequency */ |
| 1040 | |
| 1041 | /* Set initial frequency. For PAL/SECAM broadcasts no |
| 1042 | 'default' channel exists AFAIK. */ |
| 1043 | if (cx->std == V4L2_STD_NTSC_M_JP) |
| 1044 | vf.frequency = 1460; /* ch. 1 91250*16/1000 */ |
| 1045 | else if (cx->std & V4L2_STD_NTSC_M) |
| 1046 | vf.frequency = 1076; /* ch. 4 67250*16/1000 */ |
| 1047 | |
| 1048 | video_input = cx->active_input; |
| 1049 | cx->active_input++; /* Force update of input */ |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 1050 | cx18_s_input(NULL, &fh, video_input); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1051 | |
| 1052 | /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code |
| 1053 | in one place. */ |
| 1054 | cx->std++; /* Force full standard initialization */ |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 1055 | cx18_s_std(NULL, &fh, &cx->tuner_std); |
| 1056 | cx18_s_frequency(NULL, &fh, &vf); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1057 | return 0; |
| 1058 | } |
| 1059 | |
Andy Walls | 18b5dc2 | 2008-11-16 17:15:01 -0300 | [diff] [blame] | 1060 | static void cx18_cancel_epu_work_orders(struct cx18 *cx) |
| 1061 | { |
| 1062 | int i; |
| 1063 | for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) |
| 1064 | cancel_work_sync(&cx->epu_work_order[i].work); |
| 1065 | } |
| 1066 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1067 | static void cx18_remove(struct pci_dev *pci_dev) |
| 1068 | { |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 1069 | struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev); |
| 1070 | struct cx18 *cx = container_of(v4l2_dev, struct cx18, v4l2_dev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1071 | |
| 1072 | CX18_DEBUG_INFO("Removing Card #%d\n", cx->num); |
| 1073 | |
| 1074 | /* Stop all captures */ |
| 1075 | CX18_DEBUG_INFO("Stopping all streams\n"); |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 1076 | if (atomic_read(&cx->tot_capturing) > 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1077 | cx18_stop_all_captures(cx); |
| 1078 | |
| 1079 | /* Interrupts */ |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 1080 | cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU); |
| 1081 | cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1082 | |
| 1083 | cx18_halt_firmware(cx); |
| 1084 | |
Andy Walls | 18b5dc2 | 2008-11-16 17:15:01 -0300 | [diff] [blame] | 1085 | cx18_cancel_epu_work_orders(cx); |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 1086 | |
Andy Walls | 572bfea | 2008-11-25 21:43:05 -0300 | [diff] [blame] | 1087 | destroy_workqueue(cx->work_queue); |
| 1088 | |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 1089 | cx18_streams_cleanup(cx, 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1090 | |
| 1091 | exit_cx18_i2c(cx); |
| 1092 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 1093 | free_irq(cx->pci_dev->irq, (void *)cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1094 | |
Hans Verkuil | cba627a | 2008-05-12 14:48:26 -0300 | [diff] [blame] | 1095 | cx18_iounmap(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1096 | |
| 1097 | release_mem_region(cx->base_addr, CX18_MEM_SIZE); |
| 1098 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 1099 | pci_disable_device(cx->pci_dev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1100 | |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 1101 | v4l2_device_unregister(v4l2_dev); |
| 1102 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1103 | CX18_INFO("Removed %s, card #%d\n", cx->card_name, cx->num); |
| 1104 | } |
| 1105 | |
| 1106 | /* define a pci_driver for card detection */ |
| 1107 | static struct pci_driver cx18_pci_driver = { |
| 1108 | .name = "cx18", |
| 1109 | .id_table = cx18_pci_tbl, |
| 1110 | .probe = cx18_probe, |
| 1111 | .remove = cx18_remove, |
| 1112 | }; |
| 1113 | |
| 1114 | static int module_start(void) |
| 1115 | { |
| 1116 | printk(KERN_INFO "cx18: Start initialization, version %s\n", CX18_VERSION); |
| 1117 | |
| 1118 | memset(cx18_cards, 0, sizeof(cx18_cards)); |
| 1119 | |
| 1120 | /* Validate parameters */ |
| 1121 | if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) { |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 1122 | printk(KERN_ERR "cx18: Exiting, cx18_first_minor must be between 0 and %d\n", |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1123 | CX18_MAX_CARDS - 1); |
| 1124 | return -1; |
| 1125 | } |
| 1126 | |
| 1127 | if (cx18_debug < 0 || cx18_debug > 511) { |
| 1128 | cx18_debug = 0; |
| 1129 | printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n"); |
| 1130 | } |
| 1131 | |
| 1132 | if (pci_register_driver(&cx18_pci_driver)) { |
| 1133 | printk(KERN_ERR "cx18: Error detecting PCI card\n"); |
| 1134 | return -ENODEV; |
| 1135 | } |
| 1136 | printk(KERN_INFO "cx18: End initialization\n"); |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | static void module_cleanup(void) |
| 1141 | { |
| 1142 | int i; |
| 1143 | |
| 1144 | pci_unregister_driver(&cx18_pci_driver); |
| 1145 | |
| 1146 | for (i = 0; i < cx18_cards_active; i++) { |
| 1147 | if (cx18_cards[i] == NULL) |
| 1148 | continue; |
| 1149 | kfree(cx18_cards[i]); |
| 1150 | } |
Andy Walls | 18b5dc2 | 2008-11-16 17:15:01 -0300 | [diff] [blame] | 1151 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | module_init(module_start); |
| 1155 | module_exit(module_cleanup); |