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 | 6afdeaf | 2010-05-23 18:53:35 -0300 | [diff] [blame] | 7 | * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.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" |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 33 | #include "cx18-queue.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 34 | #include "cx18-streams.h" |
| 35 | #include "cx18-av-core.h" |
| 36 | #include "cx18-scb.h" |
| 37 | #include "cx18-mailbox.h" |
| 38 | #include "cx18-ioctl.h" |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 39 | #include "cx18-controls.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 40 | #include "tuner-xc2028.h" |
Santosh Nayak | 3907b01 | 2012-02-21 06:13:31 -0300 | [diff] [blame] | 41 | #include <linux/dma-mapping.h> |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 42 | #include <media/tveeprom.h> |
| 43 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 44 | /* If you have already X v4l cards, then set this to X. This way |
| 45 | the device numbers stay matched. Example: you have a WinTV card |
| 46 | without radio and a Compro H900 with. Normally this would give a |
| 47 | video1 device together with a radio0 device for the Compro. By |
| 48 | setting this to 1 you ensure that radio0 is now also radio1. */ |
| 49 | int cx18_first_minor; |
| 50 | |
Devin Heitmueller | d68b687 | 2009-11-20 01:15:54 -0300 | [diff] [blame] | 51 | /* Callback for registering extensions */ |
| 52 | int (*cx18_ext_init)(struct cx18 *); |
| 53 | EXPORT_SYMBOL(cx18_ext_init); |
| 54 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 55 | /* add your revision and whatnot here */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 56 | static struct pci_device_id cx18_pci_tbl[] = { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 57 | {PCI_VENDOR_ID_CX, PCI_DEVICE_ID_CX23418, |
| 58 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
| 59 | {0,} |
| 60 | }; |
| 61 | |
| 62 | MODULE_DEVICE_TABLE(pci, cx18_pci_tbl); |
| 63 | |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 64 | static atomic_t cx18_instance = ATOMIC_INIT(0); |
| 65 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 66 | /* Parameter declarations */ |
| 67 | static int cardtype[CX18_MAX_CARDS]; |
| 68 | static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1, |
| 69 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 70 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 71 | -1, -1, -1, -1, -1, -1, -1, -1 }; |
| 72 | static int radio[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1, |
| 73 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 74 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 75 | -1, -1, -1, -1, -1, -1, -1, -1 }; |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 76 | static unsigned cardtype_c = 1; |
| 77 | static unsigned tuner_c = 1; |
Danny Kukawka | b3f5bad | 2012-02-22 09:24:55 -0300 | [diff] [blame] | 78 | static unsigned radio_c = 1; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 79 | static char pal[] = "--"; |
| 80 | static char secam[] = "--"; |
| 81 | static char ntsc[] = "-"; |
| 82 | |
| 83 | /* Buffers */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 84 | static int enc_ts_buffers = CX18_DEFAULT_ENC_TS_BUFFERS; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 85 | static int enc_mpg_buffers = CX18_DEFAULT_ENC_MPG_BUFFERS; |
| 86 | static int enc_idx_buffers = CX18_DEFAULT_ENC_IDX_BUFFERS; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 87 | static int enc_yuv_buffers = CX18_DEFAULT_ENC_YUV_BUFFERS; |
| 88 | static int enc_vbi_buffers = CX18_DEFAULT_ENC_VBI_BUFFERS; |
| 89 | static int enc_pcm_buffers = CX18_DEFAULT_ENC_PCM_BUFFERS; |
| 90 | |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 91 | static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE; |
| 92 | static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE; |
| 93 | static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE; |
| 94 | static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 95 | static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE; |
| 96 | |
| 97 | static int enc_ts_bufs = -1; |
| 98 | static int enc_mpg_bufs = -1; |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 99 | static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 100 | static int enc_yuv_bufs = -1; |
| 101 | static int enc_vbi_bufs = -1; |
| 102 | static int enc_pcm_bufs = -1; |
| 103 | |
| 104 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 105 | static int cx18_pci_latency = 1; |
| 106 | |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 107 | static int mmio_ndelay; |
| 108 | static int retry_mmio = 1; |
| 109 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 110 | int cx18_debug; |
| 111 | |
| 112 | module_param_array(tuner, int, &tuner_c, 0644); |
Danny Kukawka | b3f5bad | 2012-02-22 09:24:55 -0300 | [diff] [blame] | 113 | module_param_array(radio, int, &radio_c, 0644); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 114 | module_param_array(cardtype, int, &cardtype_c, 0644); |
| 115 | module_param_string(pal, pal, sizeof(pal), 0644); |
| 116 | module_param_string(secam, secam, sizeof(secam), 0644); |
| 117 | module_param_string(ntsc, ntsc, sizeof(ntsc), 0644); |
| 118 | module_param_named(debug, cx18_debug, int, 0644); |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 119 | module_param(mmio_ndelay, int, 0644); |
| 120 | module_param(retry_mmio, int, 0644); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 121 | module_param(cx18_pci_latency, int, 0644); |
| 122 | module_param(cx18_first_minor, int, 0644); |
| 123 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 124 | module_param(enc_ts_buffers, int, 0644); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 125 | module_param(enc_mpg_buffers, int, 0644); |
| 126 | module_param(enc_idx_buffers, int, 0644); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 127 | module_param(enc_yuv_buffers, int, 0644); |
| 128 | module_param(enc_vbi_buffers, int, 0644); |
| 129 | module_param(enc_pcm_buffers, int, 0644); |
| 130 | |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 131 | module_param(enc_ts_bufsize, int, 0644); |
| 132 | module_param(enc_mpg_bufsize, int, 0644); |
| 133 | module_param(enc_idx_bufsize, int, 0644); |
| 134 | module_param(enc_yuv_bufsize, int, 0644); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 135 | module_param(enc_pcm_bufsize, int, 0644); |
| 136 | |
| 137 | module_param(enc_ts_bufs, int, 0644); |
| 138 | module_param(enc_mpg_bufs, int, 0644); |
| 139 | module_param(enc_idx_bufs, int, 0644); |
| 140 | module_param(enc_yuv_bufs, int, 0644); |
| 141 | module_param(enc_vbi_bufs, int, 0644); |
| 142 | module_param(enc_pcm_bufs, int, 0644); |
| 143 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 144 | MODULE_PARM_DESC(tuner, "Tuner type selection,\n" |
| 145 | "\t\t\tsee tuner.h for values"); |
| 146 | MODULE_PARM_DESC(radio, |
| 147 | "Enable or disable the radio. Use only if autodetection\n" |
| 148 | "\t\t\tfails. 0 = disable, 1 = enable"); |
| 149 | MODULE_PARM_DESC(cardtype, |
| 150 | "Only use this option if your card is not detected properly.\n" |
| 151 | "\t\tSpecify card type:\n" |
| 152 | "\t\t\t 1 = Hauppauge HVR 1600 (ESMT memory)\n" |
| 153 | "\t\t\t 2 = Hauppauge HVR 1600 (Samsung memory)\n" |
| 154 | "\t\t\t 3 = Compro VideoMate H900\n" |
| 155 | "\t\t\t 4 = Yuan MPC718\n" |
Sri Deevi | 03c2808 | 2008-06-21 11:06:44 -0300 | [diff] [blame] | 156 | "\t\t\t 5 = Conexant Raptor PAL/SECAM\n" |
Andy Walls | 9eee4fb | 2008-10-04 20:28:40 -0300 | [diff] [blame] | 157 | "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n" |
Andy Walls | 9d5af86 | 2009-06-09 20:37:24 -0300 | [diff] [blame] | 158 | "\t\t\t 7 = Leadtek WinFast PVR2100\n" |
| 159 | "\t\t\t 8 = Leadtek WinFast DVR3100 H\n" |
Alexey Chernov | a363436 | 2010-10-28 18:12:02 -0300 | [diff] [blame] | 160 | "\t\t\t 9 = GoTView PCI DVD3 Hybrid\n" |
Devin Heitmueller | e3bfeab | 2011-02-26 02:44:38 -0300 | [diff] [blame] | 161 | "\t\t\t 10 = Hauppauge HVR 1600 (S5H1411)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 162 | "\t\t\t 0 = Autodetect (default)\n" |
| 163 | "\t\t\t-1 = Ignore this card\n\t\t"); |
| 164 | MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60"); |
| 165 | MODULE_PARM_DESC(secam, "Set SECAM standard: B, G, H, D, K, L, LC"); |
| 166 | MODULE_PARM_DESC(ntsc, "Set NTSC standard: M, J, K"); |
| 167 | MODULE_PARM_DESC(debug, |
| 168 | "Debug level (bitmask). Default: 0\n" |
| 169 | "\t\t\t 1/0x0001: warning\n" |
| 170 | "\t\t\t 2/0x0002: info\n" |
| 171 | "\t\t\t 4/0x0004: mailbox\n" |
| 172 | "\t\t\t 8/0x0008: dma\n" |
| 173 | "\t\t\t 16/0x0010: ioctl\n" |
| 174 | "\t\t\t 32/0x0020: file\n" |
| 175 | "\t\t\t 64/0x0040: i2c\n" |
| 176 | "\t\t\t128/0x0080: irq\n" |
| 177 | "\t\t\t256/0x0100: high volume\n"); |
| 178 | MODULE_PARM_DESC(cx18_pci_latency, |
| 179 | "Change the PCI latency to 64 if lower: 0 = No, 1 = Yes,\n" |
| 180 | "\t\t\tDefault: Yes"); |
Andy Walls | d267d85 | 2008-09-28 21:46:02 -0300 | [diff] [blame] | 181 | MODULE_PARM_DESC(retry_mmio, |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 182 | "(Deprecated) MMIO writes are now always checked and retried\n" |
| 183 | "\t\t\tEffectively: 1 [Yes]"); |
Andy Walls | c641d09 | 2008-09-01 00:40:41 -0300 | [diff] [blame] | 184 | MODULE_PARM_DESC(mmio_ndelay, |
Andy Walls | 3f75c61 | 2008-11-16 23:33:41 -0300 | [diff] [blame] | 185 | "(Deprecated) MMIO accesses are now never purposely delayed\n" |
| 186 | "\t\t\tEffectively: 0 ns"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 187 | MODULE_PARM_DESC(enc_ts_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 188 | "Encoder TS buffer memory (MB). (enc_ts_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 189 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 190 | MODULE_PARM_DESC(enc_ts_bufsize, |
| 191 | "Size of an encoder TS buffer (kB)\n" |
| 192 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_TS_BUFSIZE)); |
| 193 | MODULE_PARM_DESC(enc_ts_bufs, |
| 194 | "Number of encoder TS buffers\n" |
| 195 | "\t\t\tDefault is computed from other enc_ts_* parameters"); |
| 196 | MODULE_PARM_DESC(enc_mpg_buffers, |
| 197 | "Encoder MPG buffer memory (MB). (enc_mpg_bufs can override)\n" |
| 198 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFFERS)); |
| 199 | MODULE_PARM_DESC(enc_mpg_bufsize, |
| 200 | "Size of an encoder MPG buffer (kB)\n" |
| 201 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_MPG_BUFSIZE)); |
| 202 | MODULE_PARM_DESC(enc_mpg_bufs, |
| 203 | "Number of encoder MPG buffers\n" |
| 204 | "\t\t\tDefault is computed from other enc_mpg_* parameters"); |
| 205 | MODULE_PARM_DESC(enc_idx_buffers, |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 206 | "(Deprecated) Encoder IDX buffer memory (MB)\n" |
| 207 | "\t\t\tIgnored, except 0 disables IDX buffer allocations\n" |
| 208 | "\t\t\tDefault: 1 [Enabled]"); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 209 | MODULE_PARM_DESC(enc_idx_bufsize, |
| 210 | "Size of an encoder IDX buffer (kB)\n" |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 211 | "\t\t\tAllowed values are multiples of 1.5 kB rounded up\n" |
| 212 | "\t\t\t(multiples of size required for 64 index entries)\n" |
| 213 | "\t\t\tDefault: 2"); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 214 | MODULE_PARM_DESC(enc_idx_bufs, |
| 215 | "Number of encoder IDX buffers\n" |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 216 | "\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 217 | MODULE_PARM_DESC(enc_yuv_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 218 | "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 219 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 220 | MODULE_PARM_DESC(enc_yuv_bufsize, |
| 221 | "Size of an encoder YUV buffer (kB)\n" |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 222 | "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n" |
| 223 | "\t\t\t(multiples of size required for 32 screen lines)\n" |
| 224 | "\t\t\tDefault: 102"); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 225 | MODULE_PARM_DESC(enc_yuv_bufs, |
| 226 | "Number of encoder YUV buffers\n" |
| 227 | "\t\t\tDefault is computed from other enc_yuv_* parameters"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 228 | MODULE_PARM_DESC(enc_vbi_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 229 | "Encoder VBI buffer memory (MB). (enc_vbi_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 230 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 231 | MODULE_PARM_DESC(enc_vbi_bufs, |
| 232 | "Number of encoder VBI buffers\n" |
Andy Walls | 127ce5f | 2009-11-11 00:22:57 -0300 | [diff] [blame] | 233 | "\t\t\tDefault is computed from enc_vbi_buffers"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 234 | MODULE_PARM_DESC(enc_pcm_buffers, |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 235 | "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 236 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS)); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 237 | MODULE_PARM_DESC(enc_pcm_bufsize, |
| 238 | "Size of an encoder PCM buffer (kB)\n" |
| 239 | "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFSIZE)); |
| 240 | MODULE_PARM_DESC(enc_pcm_bufs, |
| 241 | "Number of encoder PCM buffers\n" |
| 242 | "\t\t\tDefault is computed from other enc_pcm_* parameters"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 243 | |
Devin Heitmueller | f8bd9d2 | 2009-12-20 23:29:02 -0300 | [diff] [blame] | 244 | MODULE_PARM_DESC(cx18_first_minor, |
| 245 | "Set device node number assigned to first card"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 246 | |
| 247 | MODULE_AUTHOR("Hans Verkuil"); |
| 248 | MODULE_DESCRIPTION("CX23418 driver"); |
| 249 | MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder"); |
| 250 | MODULE_LICENSE("GPL"); |
| 251 | |
| 252 | MODULE_VERSION(CX18_VERSION); |
| 253 | |
Mauro Carvalho Chehab | 583d338 | 2010-01-28 00:19:00 -0200 | [diff] [blame] | 254 | #if defined(CONFIG_MODULES) && defined(MODULE) |
| 255 | static void request_module_async(struct work_struct *work) |
| 256 | { |
| 257 | struct cx18 *dev = container_of(work, struct cx18, request_module_wk); |
| 258 | |
| 259 | /* Make sure cx18-alsa module is loaded */ |
| 260 | request_module("cx18-alsa"); |
| 261 | |
| 262 | /* Initialize cx18-alsa for this instance of the cx18 device */ |
| 263 | if (cx18_ext_init != NULL) |
| 264 | cx18_ext_init(dev); |
| 265 | } |
| 266 | |
| 267 | static void request_modules(struct cx18 *dev) |
| 268 | { |
| 269 | INIT_WORK(&dev->request_module_wk, request_module_async); |
| 270 | schedule_work(&dev->request_module_wk); |
| 271 | } |
Tejun Heo | 707bcf3 | 2010-12-24 16:14:20 +0100 | [diff] [blame] | 272 | |
| 273 | static void flush_request_modules(struct cx18 *dev) |
| 274 | { |
Linus Torvalds | 0b8e74c | 2012-10-07 17:49:05 +0900 | [diff] [blame] | 275 | flush_work(&dev->request_module_wk); |
Tejun Heo | 707bcf3 | 2010-12-24 16:14:20 +0100 | [diff] [blame] | 276 | } |
Mauro Carvalho Chehab | 583d338 | 2010-01-28 00:19:00 -0200 | [diff] [blame] | 277 | #else |
| 278 | #define request_modules(dev) |
Tejun Heo | 707bcf3 | 2010-12-24 16:14:20 +0100 | [diff] [blame] | 279 | #define flush_request_modules(dev) |
Mauro Carvalho Chehab | 583d338 | 2010-01-28 00:19:00 -0200 | [diff] [blame] | 280 | #endif /* CONFIG_MODULES */ |
Devin Heitmueller | d68b687 | 2009-11-20 01:15:54 -0300 | [diff] [blame] | 281 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 282 | /* Generic utility functions */ |
| 283 | int cx18_msleep_timeout(unsigned int msecs, int intr) |
| 284 | { |
Andy Walls | 330c6ec | 2008-11-08 14:19:37 -0300 | [diff] [blame] | 285 | long int timeout = msecs_to_jiffies(msecs); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 286 | int sig; |
| 287 | |
| 288 | do { |
| 289 | set_current_state(intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); |
| 290 | timeout = schedule_timeout(timeout); |
| 291 | sig = intr ? signal_pending(current) : 0; |
| 292 | } while (!sig && timeout); |
| 293 | return sig; |
| 294 | } |
| 295 | |
| 296 | /* Release ioremapped memory */ |
| 297 | static void cx18_iounmap(struct cx18 *cx) |
| 298 | { |
| 299 | if (cx == NULL) |
| 300 | return; |
| 301 | |
| 302 | /* Release io memory */ |
| 303 | if (cx->enc_mem != NULL) { |
| 304 | CX18_DEBUG_INFO("releasing enc_mem\n"); |
| 305 | iounmap(cx->enc_mem); |
| 306 | cx->enc_mem = NULL; |
| 307 | } |
| 308 | } |
| 309 | |
Andy Walls | 25a42e4 | 2009-07-05 17:09:28 -0300 | [diff] [blame] | 310 | static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | CX18_INFO("eeprom dump:\n"); |
| 315 | for (i = 0; i < len; i++) { |
| 316 | if (0 == (i % 16)) |
| 317 | CX18_INFO("eeprom %02x:", i); |
| 318 | printk(KERN_CONT " %02x", eedata[i]); |
| 319 | if (15 == (i % 16)) |
| 320 | printk(KERN_CONT "\n"); |
| 321 | } |
| 322 | } |
| 323 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 324 | /* Hauppauge card? get values from tveeprom */ |
| 325 | void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv) |
| 326 | { |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 327 | struct i2c_client c; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 328 | u8 eedata[256]; |
| 329 | |
Andy Walls | 6246d4e | 2009-02-21 22:27:37 -0300 | [diff] [blame] | 330 | memset(&c, 0, sizeof(c)); |
Andy Walls | 098003d | 2009-02-28 13:19:45 -0300 | [diff] [blame] | 331 | strlcpy(c.name, "cx18 tveeprom tmp", sizeof(c.name)); |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 332 | c.adapter = &cx->i2c_adap[0]; |
| 333 | c.addr = 0xA0 >> 1; |
| 334 | |
Andy Walls | 25a42e4 | 2009-07-05 17:09:28 -0300 | [diff] [blame] | 335 | memset(tv, 0, sizeof(*tv)); |
| 336 | if (tveeprom_read(&c, eedata, sizeof(eedata))) |
| 337 | return; |
| 338 | |
| 339 | switch (cx->card->type) { |
| 340 | case CX18_CARD_HVR_1600_ESMT: |
| 341 | case CX18_CARD_HVR_1600_SAMSUNG: |
Devin Heitmueller | e3bfeab | 2011-02-26 02:44:38 -0300 | [diff] [blame] | 342 | case CX18_CARD_HVR_1600_S5H1411: |
Andy Walls | 25a42e4 | 2009-07-05 17:09:28 -0300 | [diff] [blame] | 343 | tveeprom_hauppauge_analog(&c, tv, eedata); |
| 344 | break; |
| 345 | case CX18_CARD_YUAN_MPC718: |
Alexey Chernov | a363436 | 2010-10-28 18:12:02 -0300 | [diff] [blame] | 346 | case CX18_CARD_GOTVIEW_PCI_DVD3: |
Andy Walls | 25a42e4 | 2009-07-05 17:09:28 -0300 | [diff] [blame] | 347 | tv->model = 0x718; |
| 348 | cx18_eeprom_dump(cx, eedata, sizeof(eedata)); |
| 349 | CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n", |
| 350 | eedata[2], eedata[1], eedata[4], eedata[3]); |
| 351 | break; |
| 352 | default: |
| 353 | tv->model = 0xffffffff; |
| 354 | cx18_eeprom_dump(cx, eedata, sizeof(eedata)); |
| 355 | break; |
| 356 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void cx18_process_eeprom(struct cx18 *cx) |
| 360 | { |
| 361 | struct tveeprom tv; |
| 362 | |
| 363 | cx18_read_eeprom(cx, &tv); |
| 364 | |
| 365 | /* Many thanks to Steven Toth from Hauppauge for providing the |
| 366 | model numbers */ |
Hans Verkuil | 1d08160 | 2008-05-12 14:45:19 -0300 | [diff] [blame] | 367 | /* Note: the Samsung memory models cannot be reliably determined |
| 368 | from the model number. Use the cardtype module option if you |
| 369 | have one of these preproduction models. */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 370 | switch (tv.model) { |
Devin Heitmueller | e3bfeab | 2011-02-26 02:44:38 -0300 | [diff] [blame] | 371 | case 74301: /* Retail models */ |
| 372 | case 74321: |
| 373 | case 74351: /* OEM models */ |
| 374 | case 74361: |
| 375 | /* Digital side is s5h1411/tda18271 */ |
| 376 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_S5H1411); |
| 377 | break; |
| 378 | case 74021: /* Retail models */ |
| 379 | case 74031: |
| 380 | case 74041: |
| 381 | case 74141: |
| 382 | case 74541: /* OEM models */ |
| 383 | case 74551: |
| 384 | case 74591: |
| 385 | case 74651: |
| 386 | case 74691: |
| 387 | case 74751: |
| 388 | case 74891: |
| 389 | /* Digital side is s5h1409/mxl5005s */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 390 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
| 391 | break; |
Andy Walls | 25a42e4 | 2009-07-05 17:09:28 -0300 | [diff] [blame] | 392 | case 0x718: |
| 393 | return; |
| 394 | case 0xffffffff: |
| 395 | CX18_INFO("Unknown EEPROM encoding\n"); |
| 396 | return; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 397 | case 0: |
| 398 | CX18_ERR("Invalid EEPROM\n"); |
| 399 | return; |
| 400 | default: |
Devin Heitmueller | e3bfeab | 2011-02-26 02:44:38 -0300 | [diff] [blame] | 401 | CX18_ERR("Unknown model %d, defaulting to original HVR-1600 " |
| 402 | "(cardtype=1)\n", tv.model); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 403 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | cx->v4l2_cap = cx->card->v4l2_capabilities; |
| 408 | cx->card_name = cx->card->name; |
| 409 | cx->card_i2c = cx->card->i2c; |
| 410 | |
| 411 | CX18_INFO("Autodetected %s\n", cx->card_name); |
| 412 | |
| 413 | if (tv.tuner_type == TUNER_ABSENT) |
Andy Walls | 4442ee8 | 2009-05-09 00:34:31 -0300 | [diff] [blame] | 414 | CX18_ERR("tveeprom cannot autodetect tuner!\n"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 415 | |
| 416 | if (cx->options.tuner == -1) |
| 417 | cx->options.tuner = tv.tuner_type; |
| 418 | if (cx->options.radio == -1) |
| 419 | cx->options.radio = (tv.has_radio != 0); |
| 420 | |
| 421 | if (cx->std != 0) |
| 422 | /* user specified tuner standard */ |
| 423 | return; |
| 424 | |
| 425 | /* autodetect tuner standard */ |
Andy Walls | 2bfe2fa | 2011-03-27 00:20:37 -0300 | [diff] [blame] | 426 | #define TVEEPROM_TUNER_FORMAT_ALL (V4L2_STD_B | V4L2_STD_GH | \ |
| 427 | V4L2_STD_MN | \ |
| 428 | V4L2_STD_PAL_I | \ |
| 429 | V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC | \ |
| 430 | V4L2_STD_DK) |
| 431 | if ((tv.tuner_formats & TVEEPROM_TUNER_FORMAT_ALL) |
| 432 | == TVEEPROM_TUNER_FORMAT_ALL) { |
| 433 | CX18_DEBUG_INFO("Worldwide tuner detected\n"); |
| 434 | cx->std = V4L2_STD_ALL; |
| 435 | } else if (tv.tuner_formats & V4L2_STD_PAL) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 436 | CX18_DEBUG_INFO("PAL tuner detected\n"); |
| 437 | cx->std |= V4L2_STD_PAL_BG | V4L2_STD_PAL_H; |
| 438 | } else if (tv.tuner_formats & V4L2_STD_NTSC) { |
| 439 | CX18_DEBUG_INFO("NTSC tuner detected\n"); |
| 440 | cx->std |= V4L2_STD_NTSC_M; |
| 441 | } else if (tv.tuner_formats & V4L2_STD_SECAM) { |
| 442 | CX18_DEBUG_INFO("SECAM tuner detected\n"); |
| 443 | cx->std |= V4L2_STD_SECAM_L; |
| 444 | } else { |
| 445 | CX18_INFO("No tuner detected, default to NTSC-M\n"); |
| 446 | cx->std |= V4L2_STD_NTSC_M; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | static v4l2_std_id cx18_parse_std(struct cx18 *cx) |
| 451 | { |
| 452 | switch (pal[0]) { |
| 453 | case '6': |
| 454 | return V4L2_STD_PAL_60; |
| 455 | case 'b': |
| 456 | case 'B': |
| 457 | case 'g': |
| 458 | case 'G': |
| 459 | return V4L2_STD_PAL_BG; |
| 460 | case 'h': |
| 461 | case 'H': |
| 462 | return V4L2_STD_PAL_H; |
| 463 | case 'n': |
| 464 | case 'N': |
| 465 | if (pal[1] == 'c' || pal[1] == 'C') |
| 466 | return V4L2_STD_PAL_Nc; |
| 467 | return V4L2_STD_PAL_N; |
| 468 | case 'i': |
| 469 | case 'I': |
| 470 | return V4L2_STD_PAL_I; |
| 471 | case 'd': |
| 472 | case 'D': |
| 473 | case 'k': |
| 474 | case 'K': |
| 475 | return V4L2_STD_PAL_DK; |
| 476 | case 'M': |
| 477 | case 'm': |
| 478 | return V4L2_STD_PAL_M; |
| 479 | case '-': |
| 480 | break; |
| 481 | default: |
| 482 | CX18_WARN("pal= argument not recognised\n"); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | switch (secam[0]) { |
| 487 | case 'b': |
| 488 | case 'B': |
| 489 | case 'g': |
| 490 | case 'G': |
| 491 | case 'h': |
| 492 | case 'H': |
| 493 | return V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H; |
| 494 | case 'd': |
| 495 | case 'D': |
| 496 | case 'k': |
| 497 | case 'K': |
| 498 | return V4L2_STD_SECAM_DK; |
| 499 | case 'l': |
| 500 | case 'L': |
| 501 | if (secam[1] == 'C' || secam[1] == 'c') |
| 502 | return V4L2_STD_SECAM_LC; |
| 503 | return V4L2_STD_SECAM_L; |
| 504 | case '-': |
| 505 | break; |
| 506 | default: |
| 507 | CX18_WARN("secam= argument not recognised\n"); |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | switch (ntsc[0]) { |
| 512 | case 'm': |
| 513 | case 'M': |
| 514 | return V4L2_STD_NTSC_M; |
| 515 | case 'j': |
| 516 | case 'J': |
| 517 | return V4L2_STD_NTSC_M_JP; |
| 518 | case 'k': |
| 519 | case 'K': |
| 520 | return V4L2_STD_NTSC_M_KR; |
| 521 | case '-': |
| 522 | break; |
| 523 | default: |
| 524 | CX18_WARN("ntsc= argument not recognised\n"); |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* no match found */ |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static void cx18_process_options(struct cx18 *cx) |
| 533 | { |
| 534 | int i, j; |
| 535 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 536 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_TS] = enc_ts_buffers; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 537 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_buffers; |
| 538 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_buffers; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 539 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_buffers; |
| 540 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_buffers; |
| 541 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_buffers; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 542 | cx->options.megabytes[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control only */ |
| 543 | |
| 544 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufs; |
| 545 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufs; |
| 546 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufs; |
| 547 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufs; |
| 548 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] = enc_vbi_bufs; |
| 549 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufs; |
| 550 | cx->stream_buffers[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control, no data */ |
| 551 | |
| 552 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_TS] = enc_ts_bufsize; |
| 553 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize; |
| 554 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize; |
| 555 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize; |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 556 | 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] | 557 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize; |
| 558 | cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */ |
| 559 | |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 560 | /* Ensure stream_buffers & stream_buf_size are valid */ |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 561 | for (i = 0; i < CX18_MAX_STREAMS; i++) { |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 562 | if (cx->stream_buffers[i] == 0 || /* User said 0 buffers */ |
| 563 | cx->options.megabytes[i] <= 0 || /* User said 0 MB total */ |
| 564 | cx->stream_buf_size[i] <= 0) { /* User said buf size 0 */ |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 565 | cx->options.megabytes[i] = 0; |
| 566 | cx->stream_buffers[i] = 0; |
| 567 | cx->stream_buf_size[i] = 0; |
| 568 | continue; |
| 569 | } |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 570 | /* |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 571 | * YUV is a special case where the stream_buf_size needs to be |
| 572 | * an integral multiple of 33.75 kB (storage for 32 screens |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 573 | * lines to maintain alignment in case of lost buffers). |
| 574 | * |
| 575 | * IDX is a special case where the stream_buf_size should be |
| 576 | * an integral multiple of 1.5 kB (storage for 64 index entries |
| 577 | * to maintain alignment in case of lost buffers). |
| 578 | * |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 579 | */ |
| 580 | if (i == CX18_ENC_STREAM_TYPE_YUV) { |
| 581 | cx->stream_buf_size[i] *= 1024; |
| 582 | cx->stream_buf_size[i] -= |
| 583 | (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE); |
| 584 | |
| 585 | if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE) |
| 586 | cx->stream_buf_size[i] = |
| 587 | CX18_UNIT_ENC_YUV_BUFSIZE; |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 588 | } else if (i == CX18_ENC_STREAM_TYPE_IDX) { |
| 589 | cx->stream_buf_size[i] *= 1024; |
| 590 | cx->stream_buf_size[i] -= |
| 591 | (cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE); |
| 592 | |
| 593 | if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE) |
| 594 | cx->stream_buf_size[i] = |
| 595 | CX18_UNIT_ENC_IDX_BUFSIZE; |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 596 | } |
| 597 | /* |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 598 | * YUV and IDX are special cases where the stream_buf_size is |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 599 | * now in bytes. |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 600 | * VBI is a special case where the stream_buf_size is fixed |
| 601 | * and already in bytes |
| 602 | */ |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 603 | if (i == CX18_ENC_STREAM_TYPE_VBI || |
Andy Walls | efc0b12 | 2009-12-30 22:54:53 -0300 | [diff] [blame] | 604 | i == CX18_ENC_STREAM_TYPE_YUV || |
| 605 | i == CX18_ENC_STREAM_TYPE_IDX) { |
Andy Walls | 466df46 | 2009-02-07 15:47:28 -0300 | [diff] [blame] | 606 | if (cx->stream_buffers[i] < 0) { |
| 607 | cx->stream_buffers[i] = |
| 608 | cx->options.megabytes[i] * 1024 * 1024 |
| 609 | / cx->stream_buf_size[i]; |
| 610 | } else { |
| 611 | /* N.B. This might round down to 0 */ |
| 612 | cx->options.megabytes[i] = |
| 613 | cx->stream_buffers[i] |
| 614 | * cx->stream_buf_size[i]/(1024 * 1024); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 615 | } |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 616 | } else { |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 617 | /* All other streams have stream_buf_size in kB here */ |
| 618 | if (cx->stream_buffers[i] < 0) { |
| 619 | cx->stream_buffers[i] = |
| 620 | cx->options.megabytes[i] * 1024 |
| 621 | / cx->stream_buf_size[i]; |
| 622 | } else { |
| 623 | /* N.B. This might round down to 0 */ |
| 624 | cx->options.megabytes[i] = |
| 625 | cx->stream_buffers[i] |
| 626 | * cx->stream_buf_size[i] / 1024; |
| 627 | } |
| 628 | /* convert from kB to bytes */ |
| 629 | cx->stream_buf_size[i] *= 1024; |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 630 | } |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 631 | CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, " |
| 632 | "%d bytes\n", i, cx->options.megabytes[i], |
| 633 | cx->stream_buffers[i], cx->stream_buf_size[i]); |
Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 634 | } |
| 635 | |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 636 | cx->options.cardtype = cardtype[cx->instance]; |
| 637 | cx->options.tuner = tuner[cx->instance]; |
| 638 | cx->options.radio = radio[cx->instance]; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 639 | |
| 640 | cx->std = cx18_parse_std(cx); |
| 641 | if (cx->options.cardtype == -1) { |
| 642 | CX18_INFO("Ignore card\n"); |
| 643 | return; |
| 644 | } |
| 645 | cx->card = cx18_get_card(cx->options.cardtype - 1); |
| 646 | if (cx->card) |
| 647 | CX18_INFO("User specified %s card\n", cx->card->name); |
| 648 | else if (cx->options.cardtype != 0) |
| 649 | CX18_ERR("Unknown user specified type, trying to autodetect card\n"); |
| 650 | if (cx->card == NULL) { |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 651 | if (cx->pci_dev->subsystem_vendor == CX18_PCI_ID_HAUPPAUGE) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 652 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
| 653 | CX18_INFO("Autodetected Hauppauge card\n"); |
| 654 | } |
| 655 | } |
| 656 | if (cx->card == NULL) { |
| 657 | for (i = 0; (cx->card = cx18_get_card(i)); i++) { |
| 658 | if (cx->card->pci_list == NULL) |
| 659 | continue; |
| 660 | for (j = 0; cx->card->pci_list[j].device; j++) { |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 661 | if (cx->pci_dev->device != |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 662 | cx->card->pci_list[j].device) |
| 663 | continue; |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 664 | if (cx->pci_dev->subsystem_vendor != |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 665 | cx->card->pci_list[j].subsystem_vendor) |
| 666 | continue; |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 667 | if (cx->pci_dev->subsystem_device != |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 668 | cx->card->pci_list[j].subsystem_device) |
| 669 | continue; |
| 670 | CX18_INFO("Autodetected %s card\n", cx->card->name); |
| 671 | goto done; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | done: |
| 676 | |
| 677 | if (cx->card == NULL) { |
| 678 | cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT); |
Bjorn Helgaas | 29e66a6 | 2008-09-04 17:24:51 -0300 | [diff] [blame] | 679 | CX18_ERR("Unknown card: vendor/device: [%04x:%04x]\n", |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 680 | cx->pci_dev->vendor, cx->pci_dev->device); |
Bjorn Helgaas | 29e66a6 | 2008-09-04 17:24:51 -0300 | [diff] [blame] | 681 | CX18_ERR(" subsystem vendor/device: [%04x:%04x]\n", |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 682 | cx->pci_dev->subsystem_vendor, |
| 683 | cx->pci_dev->subsystem_device); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 684 | CX18_ERR("Defaulting to %s card\n", cx->card->name); |
| 685 | CX18_ERR("Please mail the vendor/device and subsystem vendor/device IDs and what kind of\n"); |
| 686 | CX18_ERR("card you have to the ivtv-devel mailinglist (www.ivtvdriver.org)\n"); |
| 687 | CX18_ERR("Prefix your subject line with [UNKNOWN CX18 CARD].\n"); |
| 688 | } |
| 689 | cx->v4l2_cap = cx->card->v4l2_capabilities; |
| 690 | cx->card_name = cx->card->name; |
| 691 | cx->card_i2c = cx->card->i2c; |
| 692 | } |
| 693 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 694 | static int cx18_create_in_workq(struct cx18 *cx) |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 695 | { |
| 696 | snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in", |
| 697 | cx->v4l2_dev.name); |
Tejun Heo | a3bc5e3 | 2011-01-03 10:49:34 -0300 | [diff] [blame] | 698 | cx->in_work_queue = alloc_ordered_workqueue(cx->in_workq_name, 0); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 699 | if (cx->in_work_queue == NULL) { |
| 700 | CX18_ERR("Unable to create incoming mailbox handler thread\n"); |
| 701 | return -ENOMEM; |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 706 | static void cx18_init_in_work_orders(struct cx18 *cx) |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 707 | { |
| 708 | int i; |
| 709 | for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) { |
| 710 | cx->in_work_order[i].cx = cx; |
| 711 | cx->in_work_order[i].str = cx->epu_debug_str; |
| 712 | INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler); |
| 713 | } |
| 714 | } |
| 715 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 716 | /* Precondition: the cx18 structure has been memset to 0. Only |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 717 | the dev and instance fields have been filled in. |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 718 | No assumptions on the card type may be made here (see cx18_init_struct2 |
| 719 | for that). |
| 720 | */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 721 | static int cx18_init_struct1(struct cx18 *cx) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 722 | { |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 723 | int ret; |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 724 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 725 | cx->base_addr = pci_resource_start(cx->pci_dev, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 726 | |
| 727 | mutex_init(&cx->serialize_lock); |
Andy Walls | 8abdd00 | 2008-07-13 19:05:25 -0300 | [diff] [blame] | 728 | mutex_init(&cx->gpio_lock); |
Andy Walls | 72c2d6d | 2008-11-06 01:15:41 -0300 | [diff] [blame] | 729 | mutex_init(&cx->epu2apu_mb_lock); |
| 730 | mutex_init(&cx->epu2cpu_mb_lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 731 | |
Tejun Heo | a3bc5e3 | 2011-01-03 10:49:34 -0300 | [diff] [blame] | 732 | ret = cx18_create_in_workq(cx); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 733 | if (ret) |
| 734 | return ret; |
| 735 | |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 736 | cx18_init_in_work_orders(cx); |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 737 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 738 | /* start counting open_id at 1 */ |
| 739 | cx->open_id = 1; |
| 740 | |
| 741 | /* Initial settings */ |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 742 | cx->cxhdl.port = CX2341X_PORT_MEMORY; |
| 743 | cx->cxhdl.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI; |
| 744 | cx->cxhdl.ops = &cx18_cxhdl_ops; |
| 745 | cx->cxhdl.func = cx18_api_func; |
Hans Verkuil | 7ee9e64 | 2011-01-25 18:25:39 -0300 | [diff] [blame] | 746 | cx->cxhdl.priv = &cx->streams[CX18_ENC_STREAM_TYPE_MPG]; |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 747 | ret = cx2341x_handler_init(&cx->cxhdl, 50); |
| 748 | if (ret) |
| 749 | return ret; |
| 750 | cx->v4l2_dev.ctrl_handler = &cx->cxhdl.hdl; |
| 751 | |
| 752 | cx->temporal_strength = cx->cxhdl.video_temporal_filter->cur.val; |
| 753 | cx->spatial_strength = cx->cxhdl.video_spatial_filter->cur.val; |
| 754 | cx->filter_mode = cx->cxhdl.video_spatial_filter_mode->cur.val | |
| 755 | (cx->cxhdl.video_temporal_filter_mode->cur.val << 1) | |
| 756 | (cx->cxhdl.video_median_filter_type->cur.val << 2); |
| 757 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 758 | init_waitqueue_head(&cx->cap_w); |
| 759 | init_waitqueue_head(&cx->mb_apu_waitq); |
| 760 | init_waitqueue_head(&cx->mb_cpu_waitq); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 761 | init_waitqueue_head(&cx->dma_waitq); |
| 762 | |
| 763 | /* VBI */ |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 764 | cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 765 | cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced; |
Andy Walls | af009cf | 2008-12-12 20:00:29 -0300 | [diff] [blame] | 766 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 767 | /* IVTV style VBI insertion into MPEG streams */ |
| 768 | INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list); |
| 769 | INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list); |
| 770 | INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list); |
| 771 | list_add(&cx->vbi.sliced_mpeg_buf.list, |
| 772 | &cx->vbi.sliced_mpeg_mdl.buf_list); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /* Second initialization part. Here the card type has been |
| 777 | autodetected. */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 778 | static void cx18_init_struct2(struct cx18 *cx) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 779 | { |
| 780 | int i; |
| 781 | |
| 782 | for (i = 0; i < CX18_CARD_MAX_VIDEO_INPUTS; i++) |
| 783 | if (cx->card->video_inputs[i].video_type == 0) |
| 784 | break; |
| 785 | cx->nof_inputs = i; |
| 786 | for (i = 0; i < CX18_CARD_MAX_AUDIO_INPUTS; i++) |
| 787 | if (cx->card->audio_inputs[i].audio_type == 0) |
| 788 | break; |
| 789 | cx->nof_audio_inputs = i; |
| 790 | |
| 791 | /* Find tuner input */ |
| 792 | for (i = 0; i < cx->nof_inputs; i++) { |
| 793 | if (cx->card->video_inputs[i].video_type == |
| 794 | CX18_CARD_INPUT_VID_TUNER) |
| 795 | break; |
| 796 | } |
| 797 | if (i == cx->nof_inputs) |
| 798 | i = 0; |
| 799 | cx->active_input = i; |
| 800 | cx->audio_input = cx->card->video_inputs[i].audio_index; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 801 | } |
| 802 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 803 | 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] | 804 | const struct pci_device_id *pci_id) |
| 805 | { |
| 806 | u16 cmd; |
| 807 | unsigned char pci_latency; |
| 808 | |
| 809 | CX18_DEBUG_INFO("Enabling pci device\n"); |
| 810 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 811 | if (pci_enable_device(pci_dev)) { |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 812 | CX18_ERR("Can't enable device %d!\n", cx->instance); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 813 | return -EIO; |
| 814 | } |
Santosh Nayak | 3907b01 | 2012-02-21 06:13:31 -0300 | [diff] [blame] | 815 | if (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) { |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 816 | CX18_ERR("No suitable DMA available, card %d\n", cx->instance); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 817 | return -EIO; |
| 818 | } |
| 819 | if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) { |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 820 | CX18_ERR("Cannot request encoder memory region, card %d\n", |
| 821 | cx->instance); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 822 | return -EIO; |
| 823 | } |
| 824 | |
Andy Walls | 4519064 | 2008-08-29 16:10:21 -0300 | [diff] [blame] | 825 | /* Enable bus mastering and memory mapped IO for the CX23418 */ |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 826 | pci_read_config_word(pci_dev, PCI_COMMAND, &cmd); |
Andy Walls | 4519064 | 2008-08-29 16:10:21 -0300 | [diff] [blame] | 827 | cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 828 | pci_write_config_word(pci_dev, PCI_COMMAND, cmd); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 829 | |
Bjørn Mork | abd34d8 | 2011-03-21 11:35:56 -0300 | [diff] [blame] | 830 | cx->card_rev = pci_dev->revision; |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 831 | pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 832 | |
| 833 | if (pci_latency < 64 && cx18_pci_latency) { |
| 834 | CX18_INFO("Unreasonably low latency timer, " |
| 835 | "setting to 64 (was %d)\n", pci_latency); |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 836 | pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, 64); |
| 837 | pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &pci_latency); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 838 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 839 | |
| 840 | CX18_DEBUG_INFO("cx%d (rev %d) at %02x:%02x.%x, " |
Hans Verkuil | 42d0c3a | 2012-05-26 08:07:03 -0300 | [diff] [blame] | 841 | "irq: %d, latency: %d, memory: 0x%llx\n", |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 842 | cx->pci_dev->device, cx->card_rev, pci_dev->bus->number, |
| 843 | PCI_SLOT(pci_dev->devfn), PCI_FUNC(pci_dev->devfn), |
Hans Verkuil | 42d0c3a | 2012-05-26 08:07:03 -0300 | [diff] [blame] | 844 | cx->pci_dev->irq, pci_latency, (u64)cx->base_addr); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 849 | static void cx18_init_subdevs(struct cx18 *cx) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 850 | { |
| 851 | u32 hw = cx->card->hw_all; |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 852 | u32 device; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 853 | int i; |
| 854 | |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 855 | for (i = 0, device = 1; i < 32; i++, device <<= 1) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 856 | |
| 857 | if (!(device & hw)) |
| 858 | continue; |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 859 | |
| 860 | switch (device) { |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 861 | case CX18_HW_DVB: |
| 862 | case CX18_HW_TVEEPROM: |
| 863 | /* These subordinate devices do not use probing */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 864 | cx->hw_flags |= device; |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 865 | break; |
| 866 | case CX18_HW_418_AV: |
| 867 | /* The A/V decoder gets probed earlier to set PLLs */ |
| 868 | /* Just note that the card uses it (i.e. has analog) */ |
| 869 | cx->hw_flags |= device; |
| 870 | break; |
Andy Walls | eefe101 | 2009-02-21 18:42:49 -0300 | [diff] [blame] | 871 | case CX18_HW_GPIO_RESET_CTRL: |
| 872 | /* |
| 873 | * The Reset Controller gets probed and added to |
| 874 | * hw_flags earlier for i2c adapter/bus initialization |
| 875 | */ |
| 876 | break; |
| 877 | case CX18_HW_GPIO_MUX: |
| 878 | if (cx18_gpio_register(cx, device) == 0) |
| 879 | cx->hw_flags |= device; |
| 880 | break; |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 881 | default: |
| 882 | if (cx18_i2c_register(cx, i) == 0) |
| 883 | cx->hw_flags |= device; |
| 884 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 885 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 886 | } |
| 887 | |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 888 | if (cx->hw_flags & CX18_HW_418_AV) |
| 889 | cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV); |
| 890 | |
| 891 | if (cx->card->hw_muxer != 0) |
| 892 | cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 893 | } |
| 894 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 895 | static int cx18_probe(struct pci_dev *pci_dev, |
| 896 | const struct pci_device_id *pci_id) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 897 | { |
| 898 | int retval = 0; |
Andy Walls | ff08657 | 2008-10-18 08:51:28 -0300 | [diff] [blame] | 899 | int i; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 900 | u32 devtype; |
| 901 | struct cx18 *cx; |
| 902 | |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 903 | /* FIXME - module parameter arrays constrain max instances */ |
| 904 | i = atomic_inc_return(&cx18_instance) - 1; |
| 905 | if (i >= CX18_MAX_CARDS) { |
| 906 | printk(KERN_ERR "cx18: cannot manage card %d, driver has a " |
| 907 | "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 908 | return -ENOMEM; |
| 909 | } |
| 910 | |
| 911 | cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC); |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 912 | if (cx == NULL) { |
| 913 | printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n", |
| 914 | i); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 915 | return -ENOMEM; |
| 916 | } |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 917 | cx->pci_dev = pci_dev; |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 918 | cx->instance = i; |
| 919 | |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 920 | retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev); |
| 921 | if (retval) { |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 922 | printk(KERN_ERR "cx18: v4l2_device_register of card %d failed" |
| 923 | "\n", cx->instance); |
| 924 | kfree(cx); |
| 925 | return retval; |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 926 | } |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 927 | snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d", |
| 928 | cx->instance); |
| 929 | CX18_INFO("Initializing card %d\n", cx->instance); |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 930 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 931 | cx18_process_options(cx); |
| 932 | if (cx->options.cardtype == -1) { |
| 933 | retval = -ENODEV; |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 934 | goto err; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 935 | } |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 936 | |
| 937 | retval = cx18_init_struct1(cx); |
| 938 | if (retval) |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 939 | goto err; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 940 | |
Hans Verkuil | 42d0c3a | 2012-05-26 08:07:03 -0300 | [diff] [blame] | 941 | CX18_DEBUG_INFO("base addr: 0x%llx\n", (u64)cx->base_addr); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 942 | |
| 943 | /* PCI Device Setup */ |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 944 | retval = cx18_setup_pci(cx, pci_dev, pci_id); |
Andy Walls | 572bfea | 2008-11-25 21:43:05 -0300 | [diff] [blame] | 945 | if (retval != 0) |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 946 | goto free_workqueues; |
Andy Walls | 572bfea | 2008-11-25 21:43:05 -0300 | [diff] [blame] | 947 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 948 | /* map io memory */ |
Hans Verkuil | 42d0c3a | 2012-05-26 08:07:03 -0300 | [diff] [blame] | 949 | CX18_DEBUG_INFO("attempting ioremap at 0x%llx len 0x%08x\n", |
| 950 | (u64)cx->base_addr + CX18_MEM_OFFSET, CX18_MEM_SIZE); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 951 | cx->enc_mem = ioremap_nocache(cx->base_addr + CX18_MEM_OFFSET, |
| 952 | CX18_MEM_SIZE); |
| 953 | if (!cx->enc_mem) { |
Andy Walls | fa98447 | 2010-12-11 12:31:45 -0300 | [diff] [blame] | 954 | CX18_ERR("ioremap failed. Can't get a window into CX23418 " |
| 955 | "memory and register space\n"); |
| 956 | CX18_ERR("Each capture card with a CX23418 needs 64 MB of " |
| 957 | "vmalloc address space for the window\n"); |
| 958 | CX18_ERR("Check the output of 'grep Vmalloc /proc/meminfo'\n"); |
| 959 | CX18_ERR("Use the vmalloc= kernel command line option to set " |
| 960 | "VmallocTotal to a larger value\n"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 961 | retval = -ENOMEM; |
| 962 | goto free_mem; |
| 963 | } |
| 964 | cx->reg_mem = cx->enc_mem + CX18_REG_OFFSET; |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 965 | devtype = cx18_read_reg(cx, 0xC72028); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 966 | switch (devtype & 0xff000000) { |
| 967 | case 0xff000000: |
| 968 | CX18_INFO("cx23418 revision %08x (A)\n", devtype); |
| 969 | break; |
| 970 | case 0x01000000: |
| 971 | CX18_INFO("cx23418 revision %08x (B)\n", devtype); |
| 972 | break; |
| 973 | default: |
| 974 | CX18_INFO("cx23418 revision %08x (Unknown)\n", devtype); |
| 975 | break; |
| 976 | } |
| 977 | |
| 978 | cx18_init_power(cx, 1); |
| 979 | cx18_init_memory(cx); |
| 980 | |
Al Viro | 990c81c | 2008-05-21 00:32:01 -0300 | [diff] [blame] | 981 | cx->scb = (struct cx18_scb __iomem *)(cx->enc_mem + SCB_OFFSET); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 982 | cx18_init_scb(cx); |
| 983 | |
| 984 | cx18_gpio_init(cx); |
| 985 | |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 986 | /* Initialize integrated A/V decoder early to set PLLs, just in case */ |
| 987 | retval = cx18_av_probe(cx); |
Andy Walls | fa3e703 | 2009-02-16 02:23:25 -0300 | [diff] [blame] | 988 | if (retval) { |
| 989 | CX18_ERR("Could not register A/V decoder subdevice\n"); |
| 990 | goto free_map; |
| 991 | } |
Andy Walls | fa3e703 | 2009-02-16 02:23:25 -0300 | [diff] [blame] | 992 | |
Andy Walls | eefe101 | 2009-02-21 18:42:49 -0300 | [diff] [blame] | 993 | /* Initialize GPIO Reset Controller to do chip resets during i2c init */ |
| 994 | if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) { |
| 995 | if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0) |
| 996 | CX18_WARN("Could not register GPIO reset controller" |
| 997 | "subdevice; proceeding anyway.\n"); |
| 998 | else |
| 999 | cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL; |
| 1000 | } |
| 1001 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1002 | /* active i2c */ |
| 1003 | CX18_DEBUG_INFO("activating i2c...\n"); |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 1004 | retval = init_cx18_i2c(cx); |
| 1005 | if (retval) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1006 | CX18_ERR("Could not initialize i2c\n"); |
| 1007 | goto free_map; |
| 1008 | } |
| 1009 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1010 | if (cx->card->hw_all & CX18_HW_TVEEPROM) { |
| 1011 | /* Based on the model number the cardtype may be changed. |
| 1012 | The PCI IDs are not always reliable. */ |
Andy Walls | 2bfe2fa | 2011-03-27 00:20:37 -0300 | [diff] [blame] | 1013 | const struct cx18_card *orig_card = cx->card; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1014 | cx18_process_eeprom(cx); |
Andy Walls | 2bfe2fa | 2011-03-27 00:20:37 -0300 | [diff] [blame] | 1015 | |
| 1016 | if (cx->card != orig_card) { |
| 1017 | /* Changed the cardtype; re-reset the I2C chips */ |
| 1018 | cx18_gpio_init(cx); |
| 1019 | cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, |
| 1020 | core, reset, (u32) CX18_GPIO_RESET_I2C); |
| 1021 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1022 | } |
| 1023 | if (cx->card->comment) |
| 1024 | CX18_INFO("%s", cx->card->comment); |
| 1025 | if (cx->card->v4l2_capabilities == 0) { |
| 1026 | retval = -ENODEV; |
| 1027 | goto free_i2c; |
| 1028 | } |
| 1029 | cx18_init_memory(cx); |
Andy Walls | fd6b9c9 | 2008-12-14 21:26:25 -0300 | [diff] [blame] | 1030 | cx18_init_scb(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1031 | |
| 1032 | /* Register IRQ */ |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 1033 | retval = request_irq(cx->pci_dev->irq, cx18_irq_handler, |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1034 | IRQF_SHARED | IRQF_DISABLED, |
| 1035 | cx->v4l2_dev.name, (void *)cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1036 | if (retval) { |
| 1037 | CX18_ERR("Failed to register irq %d\n", retval); |
| 1038 | goto free_i2c; |
| 1039 | } |
| 1040 | |
| 1041 | if (cx->std == 0) |
| 1042 | cx->std = V4L2_STD_NTSC_M; |
| 1043 | |
| 1044 | if (cx->options.tuner == -1) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1045 | for (i = 0; i < CX18_CARD_MAX_TUNERS; i++) { |
| 1046 | if ((cx->std & cx->card->tuners[i].std) == 0) |
| 1047 | continue; |
| 1048 | cx->options.tuner = cx->card->tuners[i].tuner; |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | /* if no tuner was found, then pick the first tuner in the card list */ |
| 1053 | if (cx->options.tuner == -1 && cx->card->tuners[0].std) { |
| 1054 | cx->std = cx->card->tuners[0].std; |
Hans Verkuil | c3cb4d9 | 2008-06-27 23:27:25 -0300 | [diff] [blame] | 1055 | if (cx->std & V4L2_STD_PAL) |
| 1056 | cx->std = V4L2_STD_PAL_BG | V4L2_STD_PAL_H; |
| 1057 | else if (cx->std & V4L2_STD_NTSC) |
| 1058 | cx->std = V4L2_STD_NTSC_M; |
| 1059 | else if (cx->std & V4L2_STD_SECAM) |
| 1060 | cx->std = V4L2_STD_SECAM_L; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1061 | cx->options.tuner = cx->card->tuners[0].tuner; |
| 1062 | } |
| 1063 | if (cx->options.radio == -1) |
| 1064 | cx->options.radio = (cx->card->radio_input.audio_type != 0); |
| 1065 | |
| 1066 | /* The card is now fully identified, continue with card-specific |
| 1067 | initialization. */ |
| 1068 | cx18_init_struct2(cx); |
| 1069 | |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 1070 | cx18_init_subdevs(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1071 | |
Andy Walls | 8d037ed | 2009-02-21 22:35:11 -0300 | [diff] [blame] | 1072 | if (cx->std & V4L2_STD_525_60) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1073 | cx->is_60hz = 1; |
Andy Walls | 8d037ed | 2009-02-21 22:35:11 -0300 | [diff] [blame] | 1074 | else |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1075 | cx->is_50hz = 1; |
Andy Walls | 8d037ed | 2009-02-21 22:35:11 -0300 | [diff] [blame] | 1076 | |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 1077 | cx2341x_handler_set_50hz(&cx->cxhdl, !cx->is_60hz); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1078 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1079 | if (cx->options.radio > 0) |
| 1080 | cx->v4l2_cap |= V4L2_CAP_RADIO; |
| 1081 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1082 | if (cx->options.tuner > -1) { |
| 1083 | struct tuner_setup setup; |
| 1084 | |
| 1085 | setup.addr = ADDR_UNSET; |
| 1086 | setup.type = cx->options.tuner; |
| 1087 | setup.mode_mask = T_ANALOG_TV; /* matches TV tuners */ |
Andy Walls | c8c741b | 2011-10-24 00:51:11 +0100 | [diff] [blame] | 1088 | if (cx->options.radio > 0) |
| 1089 | setup.mode_mask |= T_RADIO; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1090 | setup.tuner_callback = (setup.type == TUNER_XC2028) ? |
| 1091 | cx18_reset_tuner_gpio : NULL; |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 1092 | cx18_call_all(cx, tuner, s_type_addr, &setup); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1093 | if (setup.type == TUNER_XC2028) { |
| 1094 | static struct xc2028_ctrl ctrl = { |
| 1095 | .fname = XC2028_DEFAULT_FIRMWARE, |
| 1096 | .max_len = 64, |
| 1097 | }; |
| 1098 | struct v4l2_priv_tun_config cfg = { |
| 1099 | .tuner = cx->options.tuner, |
| 1100 | .priv = &ctrl, |
| 1101 | }; |
Andy Walls | ff2a200 | 2009-02-20 23:52:13 -0300 | [diff] [blame] | 1102 | cx18_call_all(cx, tuner, s_config, &cfg); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* The tuner is fixed to the standard. The other inputs (e.g. S-Video) |
| 1107 | are not. */ |
| 1108 | cx->tuner_std = cx->std; |
Andy Walls | 2bfe2fa | 2011-03-27 00:20:37 -0300 | [diff] [blame] | 1109 | if (cx->std == V4L2_STD_ALL) |
| 1110 | cx->std = V4L2_STD_NTSC_M; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1111 | |
Hans Verkuil | 5e7fdc5 | 2008-05-30 10:51:53 -0300 | [diff] [blame] | 1112 | retval = cx18_streams_setup(cx); |
| 1113 | if (retval) { |
| 1114 | CX18_ERR("Error %d setting up streams\n", retval); |
| 1115 | goto free_irq; |
| 1116 | } |
| 1117 | retval = cx18_streams_register(cx); |
| 1118 | if (retval) { |
| 1119 | CX18_ERR("Error %d registering devices\n", retval); |
| 1120 | goto free_streams; |
| 1121 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1122 | |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1123 | CX18_INFO("Initialized card: %s\n", cx->card_name); |
Devin Heitmueller | d68b687 | 2009-11-20 01:15:54 -0300 | [diff] [blame] | 1124 | |
| 1125 | /* Load cx18 submodules (cx18-alsa) */ |
| 1126 | request_modules(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1127 | return 0; |
| 1128 | |
| 1129 | free_streams: |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 1130 | cx18_streams_cleanup(cx, 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1131 | free_irq: |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 1132 | free_irq(cx->pci_dev->irq, (void *)cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1133 | free_i2c: |
| 1134 | exit_cx18_i2c(cx); |
| 1135 | free_map: |
| 1136 | cx18_iounmap(cx); |
| 1137 | free_mem: |
| 1138 | release_mem_region(cx->base_addr, CX18_MEM_SIZE); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 1139 | free_workqueues: |
Andy Walls | deed75e | 2009-04-13 22:22:40 -0300 | [diff] [blame] | 1140 | destroy_workqueue(cx->in_work_queue); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1141 | err: |
| 1142 | if (retval == 0) |
| 1143 | retval = -ENODEV; |
| 1144 | CX18_ERR("Error %d on initialization\n", retval); |
| 1145 | |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1146 | v4l2_device_unregister(&cx->v4l2_dev); |
| 1147 | kfree(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1148 | return retval; |
| 1149 | } |
| 1150 | |
| 1151 | int cx18_init_on_first_open(struct cx18 *cx) |
| 1152 | { |
| 1153 | int video_input; |
| 1154 | int fw_retry_count = 3; |
| 1155 | struct v4l2_frequency vf; |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 1156 | struct cx18_open_id fh; |
Andy Walls | 2bfe2fa | 2011-03-27 00:20:37 -0300 | [diff] [blame] | 1157 | v4l2_std_id std; |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 1158 | |
| 1159 | fh.cx = cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1160 | |
| 1161 | if (test_bit(CX18_F_I_FAILED, &cx->i_flags)) |
| 1162 | return -ENXIO; |
| 1163 | |
| 1164 | if (test_and_set_bit(CX18_F_I_INITED, &cx->i_flags)) |
| 1165 | return 0; |
| 1166 | |
| 1167 | while (--fw_retry_count > 0) { |
| 1168 | /* load firmware */ |
| 1169 | if (cx18_firmware_init(cx) == 0) |
| 1170 | break; |
| 1171 | if (fw_retry_count > 1) |
| 1172 | CX18_WARN("Retry loading firmware\n"); |
| 1173 | } |
| 1174 | |
| 1175 | if (fw_retry_count == 0) { |
| 1176 | set_bit(CX18_F_I_FAILED, &cx->i_flags); |
| 1177 | return -ENXIO; |
| 1178 | } |
| 1179 | set_bit(CX18_F_I_LOADED_FW, &cx->i_flags); |
| 1180 | |
Andy Walls | 350145a | 2009-01-04 21:51:17 -0300 | [diff] [blame] | 1181 | /* |
| 1182 | * Init the firmware twice to work around a silicon bug |
| 1183 | * with the digital TS. |
| 1184 | * |
| 1185 | * The second firmware load requires us to normalize the APU state, |
| 1186 | * or the audio for the first analog capture will be badly incorrect. |
| 1187 | * |
| 1188 | * I can't seem to call APU_RESETAI and have it succeed without the |
| 1189 | * APU capturing audio, so we start and stop it here to do the reset |
| 1190 | */ |
| 1191 | |
| 1192 | /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */ |
| 1193 | cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0); |
| 1194 | cx18_vapi(cx, CX18_APU_RESETAI, 0); |
| 1195 | cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1196 | |
| 1197 | fw_retry_count = 3; |
| 1198 | while (--fw_retry_count > 0) { |
| 1199 | /* load firmware */ |
| 1200 | if (cx18_firmware_init(cx) == 0) |
| 1201 | break; |
| 1202 | if (fw_retry_count > 1) |
| 1203 | CX18_WARN("Retry loading firmware\n"); |
| 1204 | } |
| 1205 | |
| 1206 | if (fw_retry_count == 0) { |
| 1207 | set_bit(CX18_F_I_FAILED, &cx->i_flags); |
| 1208 | return -ENXIO; |
| 1209 | } |
| 1210 | |
Andy Walls | d5c02f6 | 2009-01-30 22:48:40 -0300 | [diff] [blame] | 1211 | /* |
| 1212 | * The second firmware load requires us to normalize the APU state, |
| 1213 | * or the audio for the first analog capture will be badly incorrect. |
| 1214 | * |
| 1215 | * I can't seem to call APU_RESETAI and have it succeed without the |
| 1216 | * APU capturing audio, so we start and stop it here to do the reset |
| 1217 | */ |
| 1218 | |
| 1219 | /* MPEG Encoding, 224 kbps, MPEG Layer II, 48 ksps */ |
| 1220 | cx18_vapi(cx, CX18_APU_START, 2, CX18_APU_ENCODING_METHOD_MPEG|0xb9, 0); |
| 1221 | cx18_vapi(cx, CX18_APU_RESETAI, 0); |
| 1222 | cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG); |
| 1223 | |
Andy Walls | fa3e703 | 2009-02-16 02:23:25 -0300 | [diff] [blame] | 1224 | /* Init the A/V decoder, if it hasn't been already */ |
Hans Verkuil | cc26b07 | 2009-03-29 19:20:26 -0300 | [diff] [blame] | 1225 | v4l2_subdev_call(cx->sd_av, core, load_fw); |
Andy Walls | fa3e703 | 2009-02-16 02:23:25 -0300 | [diff] [blame] | 1226 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1227 | vf.tuner = 0; |
| 1228 | vf.type = V4L2_TUNER_ANALOG_TV; |
| 1229 | vf.frequency = 6400; /* the tuner 'baseline' frequency */ |
| 1230 | |
| 1231 | /* Set initial frequency. For PAL/SECAM broadcasts no |
| 1232 | 'default' channel exists AFAIK. */ |
| 1233 | if (cx->std == V4L2_STD_NTSC_M_JP) |
| 1234 | vf.frequency = 1460; /* ch. 1 91250*16/1000 */ |
| 1235 | else if (cx->std & V4L2_STD_NTSC_M) |
| 1236 | vf.frequency = 1076; /* ch. 4 67250*16/1000 */ |
| 1237 | |
| 1238 | video_input = cx->active_input; |
| 1239 | cx->active_input++; /* Force update of input */ |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 1240 | cx18_s_input(NULL, &fh, video_input); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1241 | |
| 1242 | /* Let the VIDIOC_S_STD ioctl do all the work, keeps the code |
| 1243 | in one place. */ |
| 1244 | cx->std++; /* Force full standard initialization */ |
Andy Walls | 2bfe2fa | 2011-03-27 00:20:37 -0300 | [diff] [blame] | 1245 | std = (cx->tuner_std == V4L2_STD_ALL) ? V4L2_STD_NTSC_M : cx->tuner_std; |
| 1246 | cx18_s_std(NULL, &fh, &std); |
Andy Walls | 3b6fe58 | 2008-06-21 08:36:31 -0300 | [diff] [blame] | 1247 | cx18_s_frequency(NULL, &fh, &vf); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1248 | return 0; |
| 1249 | } |
| 1250 | |
Andy Walls | deed75e | 2009-04-13 22:22:40 -0300 | [diff] [blame] | 1251 | static void cx18_cancel_in_work_orders(struct cx18 *cx) |
Andy Walls | 18b5dc2 | 2008-11-16 17:15:01 -0300 | [diff] [blame] | 1252 | { |
| 1253 | int i; |
Andy Walls | deed75e | 2009-04-13 22:22:40 -0300 | [diff] [blame] | 1254 | for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) |
| 1255 | cancel_work_sync(&cx->in_work_order[i].work); |
Andy Walls | 18b5dc2 | 2008-11-16 17:15:01 -0300 | [diff] [blame] | 1256 | } |
| 1257 | |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 1258 | static void cx18_cancel_out_work_orders(struct cx18 *cx) |
| 1259 | { |
| 1260 | int i; |
| 1261 | for (i = 0; i < CX18_MAX_STREAMS; i++) |
| 1262 | if (&cx->streams[i].video_dev != NULL) |
| 1263 | cancel_work_sync(&cx->streams[i].out_work_order); |
| 1264 | } |
| 1265 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1266 | static void cx18_remove(struct pci_dev *pci_dev) |
| 1267 | { |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 1268 | struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev); |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1269 | struct cx18 *cx = to_cx18(v4l2_dev); |
Andy Walls | 6da6bf5 | 2009-02-21 19:53:54 -0300 | [diff] [blame] | 1270 | int i; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1271 | |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1272 | CX18_DEBUG_INFO("Removing Card\n"); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1273 | |
Tejun Heo | 707bcf3 | 2010-12-24 16:14:20 +0100 | [diff] [blame] | 1274 | flush_request_modules(cx); |
| 1275 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1276 | /* Stop all captures */ |
| 1277 | CX18_DEBUG_INFO("Stopping all streams\n"); |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 1278 | if (atomic_read(&cx->tot_capturing) > 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1279 | cx18_stop_all_captures(cx); |
| 1280 | |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 1281 | /* Stop interrupts that cause incoming work to be queued */ |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 1282 | cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 1283 | |
| 1284 | /* Incoming work can cause outgoing work, so clean up incoming first */ |
| 1285 | cx18_cancel_in_work_orders(cx); |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 1286 | cx18_cancel_out_work_orders(cx); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 1287 | |
| 1288 | /* Stop ack interrupts that may have been needed for work to finish */ |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 1289 | 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] | 1290 | |
| 1291 | cx18_halt_firmware(cx); |
| 1292 | |
Andy Walls | deed75e | 2009-04-13 22:22:40 -0300 | [diff] [blame] | 1293 | destroy_workqueue(cx->in_work_queue); |
Andy Walls | 572bfea | 2008-11-25 21:43:05 -0300 | [diff] [blame] | 1294 | |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 1295 | cx18_streams_cleanup(cx, 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1296 | |
| 1297 | exit_cx18_i2c(cx); |
| 1298 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 1299 | free_irq(cx->pci_dev->irq, (void *)cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1300 | |
Hans Verkuil | cba627a | 2008-05-12 14:48:26 -0300 | [diff] [blame] | 1301 | cx18_iounmap(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1302 | |
| 1303 | release_mem_region(cx->base_addr, CX18_MEM_SIZE); |
| 1304 | |
Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 1305 | pci_disable_device(cx->pci_dev); |
Andy Walls | 6da6bf5 | 2009-02-21 19:53:54 -0300 | [diff] [blame] | 1306 | |
| 1307 | if (cx->vbi.sliced_mpeg_data[0] != NULL) |
| 1308 | for (i = 0; i < CX18_VBI_FRAMES; i++) |
| 1309 | kfree(cx->vbi.sliced_mpeg_data[i]); |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1310 | |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 1311 | v4l2_ctrl_handler_free(&cx->av_state.hdl); |
| 1312 | |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1313 | CX18_INFO("Removed %s\n", cx->card_name); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1314 | |
Andy Walls | 888cdb0 | 2009-01-11 15:08:53 -0300 | [diff] [blame] | 1315 | v4l2_device_unregister(v4l2_dev); |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 1316 | kfree(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1317 | } |
| 1318 | |
Devin Heitmueller | d68b687 | 2009-11-20 01:15:54 -0300 | [diff] [blame] | 1319 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1320 | /* define a pci_driver for card detection */ |
| 1321 | static struct pci_driver cx18_pci_driver = { |
| 1322 | .name = "cx18", |
| 1323 | .id_table = cx18_pci_tbl, |
| 1324 | .probe = cx18_probe, |
| 1325 | .remove = cx18_remove, |
| 1326 | }; |
| 1327 | |
Peter Huewe | 9710e7a | 2009-11-04 15:28:33 -0300 | [diff] [blame] | 1328 | static int __init module_start(void) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1329 | { |
Devin Heitmueller | f8bd9d2 | 2009-12-20 23:29:02 -0300 | [diff] [blame] | 1330 | printk(KERN_INFO "cx18: Start initialization, version %s\n", |
| 1331 | CX18_VERSION); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1332 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1333 | /* Validate parameters */ |
| 1334 | if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) { |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 1335 | 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] | 1336 | CX18_MAX_CARDS - 1); |
| 1337 | return -1; |
| 1338 | } |
| 1339 | |
| 1340 | if (cx18_debug < 0 || cx18_debug > 511) { |
| 1341 | cx18_debug = 0; |
| 1342 | printk(KERN_INFO "cx18: Debug value must be >= 0 and <= 511!\n"); |
| 1343 | } |
| 1344 | |
| 1345 | if (pci_register_driver(&cx18_pci_driver)) { |
| 1346 | printk(KERN_ERR "cx18: Error detecting PCI card\n"); |
| 1347 | return -ENODEV; |
| 1348 | } |
| 1349 | printk(KERN_INFO "cx18: End initialization\n"); |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
Peter Huewe | 9710e7a | 2009-11-04 15:28:33 -0300 | [diff] [blame] | 1353 | static void __exit module_cleanup(void) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1354 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1355 | pci_unregister_driver(&cx18_pci_driver); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | module_init(module_start); |
| 1359 | module_exit(module_cleanup); |
Tim Gardner | 8a7bf1d | 2012-07-27 12:45:21 -0300 | [diff] [blame] | 1360 | MODULE_FIRMWARE(XC2028_DEFAULT_FIRMWARE); |