]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * |
4 | * Support for the mpeg transport stream transfers | |
5 | * PCI function #2 of the cx2388x. | |
6 | * | |
f8de18d4 | 7 | * (c) 2004 Jelle Foks <jelle@foks.us> |
1da177e4 LT |
8 | * (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au> |
9 | * (c) 2004 Gerd Knorr <kraxel@bytesex.org> | |
1da177e4 LT |
10 | */ |
11 | ||
65bc2fe8 MCC |
12 | #include "cx88.h" |
13 | ||
1da177e4 | 14 | #include <linux/module.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
1da177e4 LT |
16 | #include <linux/init.h> |
17 | #include <linux/device.h> | |
c24228da | 18 | #include <linux/dma-mapping.h> |
1da177e4 | 19 | #include <linux/interrupt.h> |
399426ca | 20 | #include <linux/delay.h> |
1da177e4 | 21 | |
1da177e4 LT |
22 | /* ------------------------------------------------------------------ */ |
23 | ||
24 | MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards"); | |
f8de18d4 | 25 | MODULE_AUTHOR("Jelle Foks <jelle@foks.us>"); |
1da177e4 LT |
26 | MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>"); |
27 | MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]"); | |
28 | MODULE_LICENSE("GPL"); | |
1990d50b | 29 | MODULE_VERSION(CX88_VERSION); |
1da177e4 | 30 | |
ff699e6b | 31 | static unsigned int debug; |
7b61ba8f MCC |
32 | module_param(debug, int, 0644); |
33 | MODULE_PARM_DESC(debug, "enable debug messages [mpeg]"); | |
1da177e4 | 34 | |
65bc2fe8 MCC |
35 | #define dprintk(level, fmt, arg...) do { \ |
36 | if (debug + 1 > level) \ | |
37 | printk(KERN_DEBUG pr_fmt("%s: mpeg:" fmt), \ | |
38 | __func__, ##arg); \ | |
39 | } while (0) | |
6c5be74c | 40 | |
77b74774 MR |
41 | #if defined(CONFIG_MODULES) && defined(MODULE) |
42 | static void request_module_async(struct work_struct *work) | |
43 | { | |
399426ca MCC |
44 | struct cx8802_dev *dev = container_of(work, struct cx8802_dev, |
45 | request_module_wk); | |
ced80c67 | 46 | |
6a59d64c | 47 | if (dev->core->board.mpeg & CX88_MPEG_DVB) |
77b74774 | 48 | request_module("cx88-dvb"); |
6a59d64c | 49 | if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD) |
ced80c67 | 50 | request_module("cx88-blackbird"); |
77b74774 MR |
51 | } |
52 | ||
53 | static void request_modules(struct cx8802_dev *dev) | |
54 | { | |
55 | INIT_WORK(&dev->request_module_wk, request_module_async); | |
56 | schedule_work(&dev->request_module_wk); | |
57 | } | |
707bcf32 TH |
58 | |
59 | static void flush_request_modules(struct cx8802_dev *dev) | |
60 | { | |
43829731 | 61 | flush_work(&dev->request_module_wk); |
707bcf32 | 62 | } |
77b74774 MR |
63 | #else |
64 | #define request_modules(dev) | |
707bcf32 | 65 | #define flush_request_modules(dev) |
77b74774 MR |
66 | #endif /* CONFIG_MODULES */ |
67 | ||
6c5be74c | 68 | static LIST_HEAD(cx8802_devlist); |
344d6c6b | 69 | static DEFINE_MUTEX(cx8802_mutex); |
1da177e4 LT |
70 | /* ------------------------------------------------------------------ */ |
71 | ||
0b6b6302 | 72 | int cx8802_start_dma(struct cx8802_dev *dev, |
399426ca MCC |
73 | struct cx88_dmaqueue *q, |
74 | struct cx88_buffer *buf) | |
1da177e4 LT |
75 | { |
76 | struct cx88_core *core = dev->core; | |
77 | ||
65bc2fe8 | 78 | dprintk(1, "w: %d, h: %d, f: %d\n", |
ccd6f1d4 | 79 | core->width, core->height, core->field); |
1da177e4 LT |
80 | |
81 | /* setup fifo + format */ | |
82 | cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28], | |
83 | dev->ts_packet_size, buf->risc.dma); | |
84 | ||
85 | /* write TS length to chip */ | |
0b6b6302 | 86 | cx_write(MO_TS_LNGTH, dev->ts_packet_size); |
1da177e4 | 87 | |
399426ca MCC |
88 | /* |
89 | * FIXME: this needs a review. | |
90 | * also: move to cx88-blackbird + cx88-dvb source files? | |
91 | */ | |
1da177e4 | 92 | |
65bc2fe8 | 93 | dprintk(1, "core->active_type_id = 0x%08x\n", core->active_type_id); |
6c5be74c | 94 | |
7b61ba8f | 95 | if ((core->active_type_id == CX88_MPEG_DVB) && |
399426ca | 96 | (core->board.mpeg & CX88_MPEG_DVB)) { |
65bc2fe8 | 97 | dprintk(1, "cx8802_start_dma doing .dvb\n"); |
1da177e4 | 98 | /* negedge driven & software reset */ |
f1798495 | 99 | cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl); |
1da177e4 LT |
100 | udelay(100); |
101 | cx_write(MO_PINMUX_IO, 0x00); | |
399426ca | 102 | cx_write(TS_HW_SOP_CNTRL, 0x47 << 16 | 188 << 4 | 0x01); |
6a59d64c | 103 | switch (core->boardnr) { |
e52e98a7 MCC |
104 | case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q: |
105 | case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T: | |
106 | case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD: | |
da215d22 | 107 | case CX88_BOARD_PCHDTV_HD5500: |
399426ca | 108 | cx_write(TS_SOP_STAT, 1 << 13); |
e52e98a7 | 109 | break; |
4f3ca2f1 DH |
110 | case CX88_BOARD_SAMSUNG_SMT_7020: |
111 | cx_write(TS_SOP_STAT, 0x00); | |
112 | break; | |
0fa14aa6 ST |
113 | case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1: |
114 | case CX88_BOARD_HAUPPAUGE_NOVASE2_S1: | |
399426ca MCC |
115 | /* Enable MPEG parallel IO and video signal pins */ |
116 | cx_write(MO_PINMUX_IO, 0x88); | |
0fa14aa6 ST |
117 | udelay(100); |
118 | break; | |
6c5be74c | 119 | case CX88_BOARD_HAUPPAUGE_HVR1300: |
337ab6d3 SS |
120 | /* Enable MPEG parallel IO and video signal pins */ |
121 | cx_write(MO_PINMUX_IO, 0x88); | |
122 | cx_write(TS_SOP_STAT, 0); | |
123 | cx_write(TS_VALERR_CNTRL, 0); | |
6c5be74c | 124 | break; |
60464da8 | 125 | case CX88_BOARD_PINNACLE_PCTV_HD_800i: |
4917019d ST |
126 | /* Enable MPEG parallel IO and video signal pins */ |
127 | cx_write(MO_PINMUX_IO, 0x88); | |
128 | cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4)); | |
129 | dev->ts_gen_cntrl = 5; | |
130 | cx_write(TS_SOP_STAT, 0); | |
131 | cx_write(TS_VALERR_CNTRL, 0); | |
60464da8 ST |
132 | udelay(100); |
133 | break; | |
e52e98a7 | 134 | default: |
41ef7c1e | 135 | cx_write(TS_SOP_STAT, 0x00); |
e52e98a7 | 136 | break; |
f1798495 | 137 | } |
1da177e4 LT |
138 | cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl); |
139 | udelay(100); | |
7b61ba8f MCC |
140 | } else if ((core->active_type_id == CX88_MPEG_BLACKBIRD) && |
141 | (core->board.mpeg & CX88_MPEG_BLACKBIRD)) { | |
65bc2fe8 | 142 | dprintk(1, "cx8802_start_dma doing .blackbird\n"); |
1da177e4 LT |
143 | cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */ |
144 | ||
399426ca MCC |
145 | /* punctured clock TS & posedge driven & software reset */ |
146 | cx_write(TS_GEN_CNTRL, 0x46); | |
1da177e4 LT |
147 | udelay(100); |
148 | ||
149 | cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */ | |
1da177e4 LT |
150 | cx_write(TS_VALERR_CNTRL, 0x2000); |
151 | ||
399426ca MCC |
152 | /* punctured clock TS & posedge driven */ |
153 | cx_write(TS_GEN_CNTRL, 0x06); | |
1da177e4 | 154 | udelay(100); |
6c5be74c | 155 | } else { |
65bc2fe8 MCC |
156 | pr_err("%s() Failed. Unsupported value in .mpeg (0x%08x)\n", |
157 | __func__, core->board.mpeg); | |
6c5be74c | 158 | return -EINVAL; |
1da177e4 | 159 | } |
1da177e4 LT |
160 | |
161 | /* reset counter */ | |
162 | cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET); | |
9450684b | 163 | q->count = 0; |
1da177e4 | 164 | |
6f6fae2b DGC |
165 | /* clear interrupt status register */ |
166 | cx_write(MO_TS_INTSTAT, 0x1f1111); | |
167 | ||
1da177e4 | 168 | /* enable irqs */ |
65bc2fe8 | 169 | dprintk(1, "setting the interrupt mask\n"); |
8ddac9ee | 170 | cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT); |
b45009b0 | 171 | cx_set(MO_TS_INTMSK, 0x1f0011); |
1da177e4 LT |
172 | |
173 | /* start dma */ | |
399426ca | 174 | cx_set(MO_DEV_CNTRL2, (1 << 5)); |
b45009b0 | 175 | cx_set(MO_TS_DMACNTRL, 0x11); |
1da177e4 LT |
176 | return 0; |
177 | } | |
399426ca | 178 | EXPORT_SYMBOL(cx8802_start_dma); |
1da177e4 LT |
179 | |
180 | static int cx8802_stop_dma(struct cx8802_dev *dev) | |
181 | { | |
182 | struct cx88_core *core = dev->core; | |
7b61ba8f | 183 | |
65bc2fe8 | 184 | dprintk(1, "\n"); |
1da177e4 LT |
185 | |
186 | /* stop dma */ | |
187 | cx_clear(MO_TS_DMACNTRL, 0x11); | |
188 | ||
189 | /* disable irqs */ | |
8ddac9ee | 190 | cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT); |
1da177e4 LT |
191 | cx_clear(MO_TS_INTMSK, 0x1f0011); |
192 | ||
193 | /* Reset the controller */ | |
194 | cx_write(TS_GEN_CNTRL, 0xcd); | |
195 | return 0; | |
196 | } | |
197 | ||
198 | static int cx8802_restart_queue(struct cx8802_dev *dev, | |
199 | struct cx88_dmaqueue *q) | |
200 | { | |
201 | struct cx88_buffer *buf; | |
1da177e4 | 202 | |
65bc2fe8 | 203 | dprintk(1, "\n"); |
1da177e4 LT |
204 | if (list_empty(&q->active)) |
205 | return 0; | |
206 | ||
0b6b6302 | 207 | buf = list_entry(q->active.next, struct cx88_buffer, list); |
7b61ba8f | 208 | dprintk(2, "restart_queue [%p/%d]: restart dma\n", |
2d700715 | 209 | buf, buf->vb.vb2_buf.index); |
1da177e4 | 210 | cx8802_start_dma(dev, q, buf); |
1da177e4 LT |
211 | return 0; |
212 | } | |
213 | ||
214 | /* ------------------------------------------------------------------ */ | |
215 | ||
0b6b6302 | 216 | int cx8802_buf_prepare(struct vb2_queue *q, struct cx8802_dev *dev, |
399426ca | 217 | struct cx88_buffer *buf) |
1da177e4 LT |
218 | { |
219 | int size = dev->ts_packet_size * dev->ts_packet_count; | |
2d700715 | 220 | struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0); |
5e7045e3 | 221 | struct cx88_riscmem *risc = &buf->risc; |
1da177e4 LT |
222 | int rc; |
223 | ||
2d700715 | 224 | if (vb2_plane_size(&buf->vb.vb2_buf, 0) < size) |
1da177e4 | 225 | return -EINVAL; |
2d700715 | 226 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size); |
1da177e4 | 227 | |
5e7045e3 | 228 | rc = cx88_risc_databuffer(dev->pci, risc, sgt->sgl, |
399426ca | 229 | dev->ts_packet_size, dev->ts_packet_count, 0); |
999b3ceb | 230 | if (rc) { |
5e7045e3 | 231 | if (risc->cpu) |
399426ca MCC |
232 | pci_free_consistent(dev->pci, risc->size, |
233 | risc->cpu, risc->dma); | |
5e7045e3 | 234 | memset(risc, 0, sizeof(*risc)); |
999b3ceb HV |
235 | return rc; |
236 | } | |
0b6b6302 | 237 | return 0; |
1da177e4 | 238 | } |
399426ca | 239 | EXPORT_SYMBOL(cx8802_buf_prepare); |
1da177e4 LT |
240 | |
241 | void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf) | |
242 | { | |
243 | struct cx88_buffer *prev; | |
c7b0ac05 | 244 | struct cx88_dmaqueue *cx88q = &dev->mpegq; |
1da177e4 | 245 | |
65bc2fe8 | 246 | dprintk(1, "\n"); |
0b6b6302 HV |
247 | /* add jump to start */ |
248 | buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8); | |
249 | buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC); | |
250 | buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8); | |
1da177e4 | 251 | |
c7b0ac05 | 252 | if (list_empty(&cx88q->active)) { |
65bc2fe8 | 253 | dprintk(1, "queue is empty - first active\n"); |
0b6b6302 | 254 | list_add_tail(&buf->list, &cx88q->active); |
7b61ba8f | 255 | dprintk(1, "[%p/%d] %s - first active\n", |
2d700715 | 256 | buf, buf->vb.vb2_buf.index, __func__); |
1da177e4 LT |
257 | |
258 | } else { | |
0b6b6302 | 259 | buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1); |
65bc2fe8 | 260 | dprintk(1, "queue is not empty - append to active\n"); |
0b6b6302 HV |
261 | prev = list_entry(cx88q->active.prev, struct cx88_buffer, list); |
262 | list_add_tail(&buf->list, &cx88q->active); | |
1da177e4 | 263 | prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); |
65bc2fe8 | 264 | dprintk(1, "[%p/%d] %s - append to active\n", |
2d700715 | 265 | buf, buf->vb.vb2_buf.index, __func__); |
1da177e4 LT |
266 | } |
267 | } | |
399426ca | 268 | EXPORT_SYMBOL(cx8802_buf_queue); |
1da177e4 LT |
269 | |
270 | /* ----------------------------------------------------------- */ | |
271 | ||
0b6b6302 | 272 | static void do_cancel_buffers(struct cx8802_dev *dev) |
1da177e4 LT |
273 | { |
274 | struct cx88_dmaqueue *q = &dev->mpegq; | |
275 | struct cx88_buffer *buf; | |
276 | unsigned long flags; | |
277 | ||
7b61ba8f | 278 | spin_lock_irqsave(&dev->slock, flags); |
1da177e4 | 279 | while (!list_empty(&q->active)) { |
0b6b6302 HV |
280 | buf = list_entry(q->active.next, struct cx88_buffer, list); |
281 | list_del(&buf->list); | |
2d700715 | 282 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
b45009b0 | 283 | } |
7b61ba8f | 284 | spin_unlock_irqrestore(&dev->slock, flags); |
1da177e4 LT |
285 | } |
286 | ||
287 | void cx8802_cancel_buffers(struct cx8802_dev *dev) | |
288 | { | |
65bc2fe8 | 289 | dprintk(1, "\n"); |
1da177e4 | 290 | cx8802_stop_dma(dev); |
0b6b6302 | 291 | do_cancel_buffers(dev); |
1da177e4 | 292 | } |
399426ca | 293 | EXPORT_SYMBOL(cx8802_cancel_buffers); |
1da177e4 | 294 | |
7b61ba8f | 295 | static const char *cx88_mpeg_irqs[32] = { |
41ef7c1e MCC |
296 | "ts_risci1", NULL, NULL, NULL, |
297 | "ts_risci2", NULL, NULL, NULL, | |
298 | "ts_oflow", NULL, NULL, NULL, | |
299 | "ts_sync", NULL, NULL, NULL, | |
300 | "opc_err", "par_err", "rip_err", "pci_abort", | |
301 | "ts_err?", | |
302 | }; | |
303 | ||
1da177e4 LT |
304 | static void cx8802_mpeg_irq(struct cx8802_dev *dev) |
305 | { | |
306 | struct cx88_core *core = dev->core; | |
307 | u32 status, mask, count; | |
308 | ||
65bc2fe8 | 309 | dprintk(1, "\n"); |
1da177e4 LT |
310 | status = cx_read(MO_TS_INTSTAT); |
311 | mask = cx_read(MO_TS_INTMSK); | |
312 | if (0 == (status & mask)) | |
313 | return; | |
314 | ||
315 | cx_write(MO_TS_INTSTAT, status); | |
41ef7c1e | 316 | |
1da177e4 | 317 | if (debug || (status & mask & ~0xff)) |
65bc2fe8 | 318 | cx88_print_irqbits("irq mpeg ", |
66623a04 MCC |
319 | cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs), |
320 | status, mask); | |
1da177e4 LT |
321 | |
322 | /* risc op code error */ | |
323 | if (status & (1 << 16)) { | |
65bc2fe8 | 324 | pr_warn("mpeg risc op code error\n"); |
1da177e4 | 325 | cx_clear(MO_TS_DMACNTRL, 0x11); |
399426ca MCC |
326 | cx88_sram_channel_dump(dev->core, |
327 | &cx88_sram_channels[SRAM_CH28]); | |
1da177e4 LT |
328 | } |
329 | ||
330 | /* risc1 y */ | |
331 | if (status & 0x01) { | |
65bc2fe8 | 332 | dprintk(1, "wake up\n"); |
1da177e4 LT |
333 | spin_lock(&dev->slock); |
334 | count = cx_read(MO_TS_GPCNT); | |
335 | cx88_wakeup(dev->core, &dev->mpegq, count); | |
336 | spin_unlock(&dev->slock); | |
337 | } | |
338 | ||
4ac97914 MCC |
339 | /* other general errors */ |
340 | if (status & 0x1f0100) { | |
65bc2fe8 | 341 | dprintk(0, "general errors: 0x%08x\n", status & 0x1f0100); |
4ac97914 | 342 | spin_lock(&dev->slock); |
1da177e4 | 343 | cx8802_stop_dma(dev); |
4ac97914 MCC |
344 | spin_unlock(&dev->slock); |
345 | } | |
1da177e4 LT |
346 | } |
347 | ||
b45009b0 MCC |
348 | #define MAX_IRQ_LOOP 10 |
349 | ||
7d12e780 | 350 | static irqreturn_t cx8802_irq(int irq, void *dev_id) |
1da177e4 LT |
351 | { |
352 | struct cx8802_dev *dev = dev_id; | |
353 | struct cx88_core *core = dev->core; | |
354 | u32 status; | |
355 | int loop, handled = 0; | |
356 | ||
b45009b0 | 357 | for (loop = 0; loop < MAX_IRQ_LOOP; loop++) { |
8ddac9ee TP |
358 | status = cx_read(MO_PCI_INTSTAT) & |
359 | (core->pci_irqmask | PCI_INT_TSINT); | |
7b61ba8f | 360 | if (status == 0) |
1da177e4 | 361 | goto out; |
65bc2fe8 MCC |
362 | dprintk(1, "cx8802_irq\n"); |
363 | dprintk(1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP); | |
364 | dprintk(1, " status: %d\n", status); | |
1da177e4 LT |
365 | handled = 1; |
366 | cx_write(MO_PCI_INTSTAT, status); | |
367 | ||
368 | if (status & core->pci_irqmask) | |
7b61ba8f | 369 | cx88_core_irq(core, status); |
8ddac9ee | 370 | if (status & PCI_INT_TSINT) |
1da177e4 | 371 | cx8802_mpeg_irq(dev); |
c2c1b415 | 372 | } |
7b61ba8f | 373 | if (loop == MAX_IRQ_LOOP) { |
65bc2fe8 MCC |
374 | dprintk(0, "clearing mask\n"); |
375 | pr_warn("irq loop -- clearing mask\n"); | |
7b61ba8f | 376 | cx_write(MO_PCI_INTMSK, 0); |
1da177e4 LT |
377 | } |
378 | ||
379 | out: | |
380 | return IRQ_RETVAL(handled); | |
381 | } | |
382 | ||
a2fbaa51 | 383 | static int cx8802_init_common(struct cx8802_dev *dev) |
1da177e4 LT |
384 | { |
385 | struct cx88_core *core = dev->core; | |
386 | int err; | |
387 | ||
388 | /* pci init */ | |
389 | if (pci_enable_device(dev->pci)) | |
390 | return -EIO; | |
391 | pci_set_master(dev->pci); | |
7b61ba8f | 392 | err = pci_set_dma_mask(dev->pci, DMA_BIT_MASK(32)); |
1a47de6e | 393 | if (err) { |
65bc2fe8 | 394 | pr_err("Oops: no 32bit PCI DMA ???\n"); |
1da177e4 LT |
395 | return -EIO; |
396 | } | |
397 | ||
abd34d8d | 398 | dev->pci_rev = dev->pci->revision; |
4ac97914 | 399 | pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER, &dev->pci_lat); |
65bc2fe8 MCC |
400 | pr_info("found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n", |
401 | pci_name(dev->pci), dev->pci_rev, dev->pci->irq, | |
402 | dev->pci_lat, | |
403 | (unsigned long long)pci_resource_start(dev->pci, 0)); | |
1da177e4 LT |
404 | |
405 | /* initialize driver struct */ | |
1da177e4 LT |
406 | spin_lock_init(&dev->slock); |
407 | ||
408 | /* init dma queue */ | |
409 | INIT_LIST_HEAD(&dev->mpegq.active); | |
1da177e4 LT |
410 | |
411 | /* get irq */ | |
412 | err = request_irq(dev->pci->irq, cx8802_irq, | |
3e018fe4 | 413 | IRQF_SHARED, dev->core->name, dev); |
1da177e4 | 414 | if (err < 0) { |
65bc2fe8 | 415 | pr_err("can't get IRQ %d\n", dev->pci->irq); |
1da177e4 LT |
416 | return err; |
417 | } | |
418 | cx_set(MO_PCI_INTMSK, core->pci_irqmask); | |
419 | ||
420 | /* everything worked */ | |
7b61ba8f | 421 | pci_set_drvdata(dev->pci, dev); |
1da177e4 LT |
422 | return 0; |
423 | } | |
424 | ||
a2fbaa51 | 425 | static void cx8802_fini_common(struct cx8802_dev *dev) |
1da177e4 | 426 | { |
65bc2fe8 | 427 | dprintk(2, "\n"); |
1da177e4 LT |
428 | cx8802_stop_dma(dev); |
429 | pci_disable_device(dev->pci); | |
430 | ||
431 | /* unregister stuff */ | |
432 | free_irq(dev->pci->irq, dev); | |
1da177e4 LT |
433 | } |
434 | ||
435 | /* ----------------------------------------------------------- */ | |
436 | ||
a2fbaa51 | 437 | static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state) |
1da177e4 | 438 | { |
4ac97914 | 439 | struct cx8802_dev *dev = pci_get_drvdata(pci_dev); |
5ddfbbb9 | 440 | unsigned long flags; |
1da177e4 LT |
441 | |
442 | /* stop mpeg dma */ | |
5ddfbbb9 | 443 | spin_lock_irqsave(&dev->slock, flags); |
1da177e4 | 444 | if (!list_empty(&dev->mpegq.active)) { |
65bc2fe8 MCC |
445 | dprintk(2, "suspend\n"); |
446 | pr_info("suspend mpeg\n"); | |
1da177e4 | 447 | cx8802_stop_dma(dev); |
1da177e4 | 448 | } |
5ddfbbb9 | 449 | spin_unlock_irqrestore(&dev->slock, flags); |
1da177e4 | 450 | |
1da177e4 LT |
451 | /* FIXME -- shutdown device */ |
452 | cx88_shutdown(dev->core); | |
1da177e4 LT |
453 | |
454 | pci_save_state(pci_dev); | |
399426ca MCC |
455 | if (pci_set_power_state(pci_dev, |
456 | pci_choose_state(pci_dev, state)) != 0) { | |
1da177e4 LT |
457 | pci_disable_device(pci_dev); |
458 | dev->state.disabled = 1; | |
459 | } | |
460 | return 0; | |
461 | } | |
462 | ||
a2fbaa51 | 463 | static int cx8802_resume_common(struct pci_dev *pci_dev) |
1da177e4 | 464 | { |
08adb9e2 | 465 | struct cx8802_dev *dev = pci_get_drvdata(pci_dev); |
5ddfbbb9 | 466 | unsigned long flags; |
08adb9e2 | 467 | int err; |
1da177e4 LT |
468 | |
469 | if (dev->state.disabled) { | |
7b61ba8f | 470 | err = pci_enable_device(pci_dev); |
08adb9e2 | 471 | if (err) { |
65bc2fe8 | 472 | pr_err("can't enable device\n"); |
08adb9e2 MCC |
473 | return err; |
474 | } | |
1da177e4 LT |
475 | dev->state.disabled = 0; |
476 | } | |
7b61ba8f | 477 | err = pci_set_power_state(pci_dev, PCI_D0); |
08adb9e2 | 478 | if (err) { |
65bc2fe8 | 479 | pr_err("can't enable device\n"); |
08adb9e2 MCC |
480 | pci_disable_device(pci_dev); |
481 | dev->state.disabled = 1; | |
482 | ||
483 | return err; | |
484 | } | |
1da177e4 LT |
485 | pci_restore_state(pci_dev); |
486 | ||
1da177e4 LT |
487 | /* FIXME: re-initialize hardware */ |
488 | cx88_reset(dev->core); | |
1da177e4 LT |
489 | |
490 | /* restart video+vbi capture */ | |
5ddfbbb9 | 491 | spin_lock_irqsave(&dev->slock, flags); |
1da177e4 | 492 | if (!list_empty(&dev->mpegq.active)) { |
65bc2fe8 | 493 | pr_info("resume mpeg\n"); |
7b61ba8f | 494 | cx8802_restart_queue(dev, &dev->mpegq); |
1da177e4 | 495 | } |
5ddfbbb9 | 496 | spin_unlock_irqrestore(&dev->slock, flags); |
1da177e4 LT |
497 | |
498 | return 0; | |
499 | } | |
500 | ||
399426ca MCC |
501 | struct cx8802_driver *cx8802_get_driver(struct cx8802_dev *dev, |
502 | enum cx88_board_type btype) | |
6c5be74c | 503 | { |
081c2fc8 | 504 | struct cx8802_driver *d; |
6c5be74c | 505 | |
89a47942 | 506 | list_for_each_entry(d, &dev->drvlist, drvlist) |
081c2fc8 TP |
507 | if (d->type_id == btype) |
508 | return d; | |
6c5be74c ST |
509 | |
510 | return NULL; | |
511 | } | |
399426ca | 512 | EXPORT_SYMBOL(cx8802_get_driver); |
6c5be74c ST |
513 | |
514 | /* Driver asked for hardware access. */ | |
9df2ead5 | 515 | static int cx8802_request_acquire(struct cx8802_driver *drv) |
6c5be74c ST |
516 | { |
517 | struct cx88_core *core = drv->core; | |
d922b8ea | 518 | unsigned int i; |
6c5be74c ST |
519 | |
520 | /* Fail a request for hardware if the device is busy. */ | |
27d0fe18 RS |
521 | if (core->active_type_id != CX88_BOARD_NONE && |
522 | core->active_type_id != drv->type_id) | |
6c5be74c ST |
523 | return -EBUSY; |
524 | ||
243bf1a2 DH |
525 | if (drv->type_id == CX88_MPEG_DVB) { |
526 | /* When switching to DVB, always set the input to the tuner */ | |
527 | core->last_analog_input = core->input; | |
528 | core->input = 0; | |
529 | for (i = 0; | |
399426ca MCC |
530 | i < (sizeof(core->board.input) / |
531 | sizeof(struct cx88_input)); | |
243bf1a2 DH |
532 | i++) { |
533 | if (core->board.input[i].type == CX88_VMUX_DVB) { | |
534 | core->input = i; | |
535 | break; | |
536 | } | |
d922b8ea IV |
537 | } |
538 | } | |
c2cb8fcc | 539 | |
399426ca | 540 | if (drv->advise_acquire) { |
baff6cdd | 541 | core->active_ref++; |
27d0fe18 RS |
542 | if (core->active_type_id == CX88_BOARD_NONE) { |
543 | core->active_type_id = drv->type_id; | |
544 | drv->advise_acquire(drv); | |
545 | } | |
6c5be74c | 546 | |
65bc2fe8 | 547 | dprintk(1, "Post acquire GPIO=%x\n", cx_read(MO_GP0_IO)); |
6c5be74c ST |
548 | } |
549 | ||
550 | return 0; | |
551 | } | |
552 | ||
553 | /* Driver asked to release hardware. */ | |
9df2ead5 | 554 | static int cx8802_request_release(struct cx8802_driver *drv) |
6c5be74c ST |
555 | { |
556 | struct cx88_core *core = drv->core; | |
557 | ||
399426ca | 558 | if (drv->advise_release && --core->active_ref == 0) { |
243bf1a2 | 559 | if (drv->type_id == CX88_MPEG_DVB) { |
399426ca MCC |
560 | /* |
561 | * If the DVB driver is releasing, reset the input | |
562 | * state to the last configured analog input | |
563 | */ | |
243bf1a2 DH |
564 | core->input = core->last_analog_input; |
565 | } | |
566 | ||
6c5be74c ST |
567 | drv->advise_release(drv); |
568 | core->active_type_id = CX88_BOARD_NONE; | |
65bc2fe8 | 569 | dprintk(1, "Post release GPIO=%x\n", cx_read(MO_GP0_IO)); |
6c5be74c ST |
570 | } |
571 | ||
572 | return 0; | |
573 | } | |
574 | ||
575 | static int cx8802_check_driver(struct cx8802_driver *drv) | |
576 | { | |
399426ca | 577 | if (!drv) |
6c5be74c ST |
578 | return -ENODEV; |
579 | ||
580 | if ((drv->type_id != CX88_MPEG_DVB) && | |
399426ca | 581 | (drv->type_id != CX88_MPEG_BLACKBIRD)) |
6c5be74c ST |
582 | return -EINVAL; |
583 | ||
584 | if ((drv->hw_access != CX8802_DRVCTL_SHARED) && | |
399426ca | 585 | (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE)) |
6c5be74c ST |
586 | return -EINVAL; |
587 | ||
399426ca MCC |
588 | if ((!drv->probe) || |
589 | (!drv->remove) || | |
590 | (!drv->advise_acquire) || | |
591 | (!drv->advise_release)) | |
6c5be74c ST |
592 | return -EINVAL; |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
597 | int cx8802_register_driver(struct cx8802_driver *drv) | |
598 | { | |
89a47942 | 599 | struct cx8802_dev *dev; |
6c5be74c | 600 | struct cx8802_driver *driver; |
89a47942 | 601 | int err, i = 0; |
6c5be74c | 602 | |
65bc2fe8 MCC |
603 | pr_info("registering cx8802 driver, type: %s access: %s\n", |
604 | drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird", | |
399426ca MCC |
605 | drv->hw_access == CX8802_DRVCTL_SHARED ? |
606 | "shared" : "exclusive"); | |
6c5be74c | 607 | |
399426ca MCC |
608 | err = cx8802_check_driver(drv); |
609 | if (err) { | |
65bc2fe8 | 610 | pr_err("cx8802_driver is invalid\n"); |
6c5be74c ST |
611 | return err; |
612 | } | |
613 | ||
344d6c6b JN |
614 | mutex_lock(&cx8802_mutex); |
615 | ||
89a47942 | 616 | list_for_each_entry(dev, &cx8802_devlist, devlist) { |
65bc2fe8 MCC |
617 | pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n", |
618 | dev->pci->subsystem_vendor, | |
619 | dev->pci->subsystem_device, dev->core->board.name, | |
620 | dev->core->boardnr); | |
6c5be74c ST |
621 | |
622 | /* Bring up a new struct for each driver instance */ | |
7b61ba8f | 623 | driver = kzalloc(sizeof(*drv), GFP_KERNEL); |
399426ca | 624 | if (!driver) { |
344d6c6b JN |
625 | err = -ENOMEM; |
626 | goto out; | |
627 | } | |
6c5be74c ST |
628 | |
629 | /* Snapshot of the driver registration data */ | |
89a47942 | 630 | drv->core = dev->core; |
6c5be74c ST |
631 | drv->suspend = cx8802_suspend_common; |
632 | drv->resume = cx8802_resume_common; | |
633 | drv->request_acquire = cx8802_request_acquire; | |
634 | drv->request_release = cx8802_request_release; | |
635 | memcpy(driver, drv, sizeof(*driver)); | |
636 | ||
1d6213ab | 637 | mutex_lock(&drv->core->lock); |
6c5be74c ST |
638 | err = drv->probe(driver); |
639 | if (err == 0) { | |
019391e4 | 640 | i++; |
89a47942 | 641 | list_add_tail(&driver->drvlist, &dev->drvlist); |
6c5be74c | 642 | } else { |
65bc2fe8 | 643 | pr_err("cx8802 probe failed, err = %d\n", err); |
6c5be74c | 644 | } |
1d6213ab | 645 | mutex_unlock(&drv->core->lock); |
6c5be74c | 646 | } |
6c5be74c | 647 | |
344d6c6b JN |
648 | err = i ? 0 : -ENODEV; |
649 | out: | |
650 | mutex_unlock(&cx8802_mutex); | |
651 | return err; | |
6c5be74c | 652 | } |
399426ca | 653 | EXPORT_SYMBOL(cx8802_register_driver); |
6c5be74c ST |
654 | |
655 | int cx8802_unregister_driver(struct cx8802_driver *drv) | |
656 | { | |
89a47942 TP |
657 | struct cx8802_dev *dev; |
658 | struct cx8802_driver *d, *dtmp; | |
659 | int err = 0; | |
6c5be74c | 660 | |
65bc2fe8 MCC |
661 | pr_info("unregistering cx8802 driver, type: %s access: %s\n", |
662 | drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird", | |
399426ca MCC |
663 | drv->hw_access == CX8802_DRVCTL_SHARED ? |
664 | "shared" : "exclusive"); | |
6c5be74c | 665 | |
344d6c6b JN |
666 | mutex_lock(&cx8802_mutex); |
667 | ||
89a47942 | 668 | list_for_each_entry(dev, &cx8802_devlist, devlist) { |
65bc2fe8 MCC |
669 | pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n", |
670 | dev->pci->subsystem_vendor, | |
671 | dev->pci->subsystem_device, dev->core->board.name, | |
672 | dev->core->boardnr); | |
6c5be74c | 673 | |
8a317a87 JN |
674 | mutex_lock(&dev->core->lock); |
675 | ||
89a47942 | 676 | list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) { |
6c5be74c ST |
677 | /* only unregister the correct driver type */ |
678 | if (d->type_id != drv->type_id) | |
679 | continue; | |
680 | ||
681 | err = d->remove(d); | |
682 | if (err == 0) { | |
89a47942 | 683 | list_del(&d->drvlist); |
a04036a3 | 684 | kfree(d); |
6c5be74c | 685 | } else |
65bc2fe8 MCC |
686 | pr_err("cx8802 driver remove failed (%d)\n", |
687 | err); | |
6c5be74c ST |
688 | } |
689 | ||
8a317a87 | 690 | mutex_unlock(&dev->core->lock); |
6c5be74c ST |
691 | } |
692 | ||
344d6c6b JN |
693 | mutex_unlock(&cx8802_mutex); |
694 | ||
6c5be74c ST |
695 | return err; |
696 | } | |
399426ca | 697 | EXPORT_SYMBOL(cx8802_unregister_driver); |
6c5be74c | 698 | |
1da177e4 | 699 | /* ----------------------------------------------------------- */ |
4c62e976 GKH |
700 | static int cx8802_probe(struct pci_dev *pci_dev, |
701 | const struct pci_device_id *pci_id) | |
6c5be74c ST |
702 | { |
703 | struct cx8802_dev *dev; | |
704 | struct cx88_core *core; | |
9212a572 | 705 | int err; |
6c5be74c ST |
706 | |
707 | /* general setup */ | |
708 | core = cx88_core_get(pci_dev); | |
399426ca | 709 | if (!core) |
6c5be74c ST |
710 | return -EINVAL; |
711 | ||
65bc2fe8 | 712 | pr_info("cx2388x 8802 Driver Manager\n"); |
6c5be74c ST |
713 | |
714 | err = -ENODEV; | |
6a59d64c | 715 | if (!core->board.mpeg) |
6c5be74c ST |
716 | goto fail_core; |
717 | ||
718 | err = -ENOMEM; | |
7b61ba8f | 719 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
399426ca | 720 | if (!dev) |
6c5be74c ST |
721 | goto fail_core; |
722 | dev->pci = pci_dev; | |
723 | dev->core = core; | |
724 | ||
e32fadc4 MCC |
725 | /* Maintain a reference so cx88-video can query the 8802 device. */ |
726 | core->dvbdev = dev; | |
727 | ||
6c5be74c ST |
728 | err = cx8802_init_common(dev); |
729 | if (err != 0) | |
2bc46b3a | 730 | goto fail_dev; |
6c5be74c | 731 | |
081c2fc8 | 732 | INIT_LIST_HEAD(&dev->drvlist); |
344d6c6b | 733 | mutex_lock(&cx8802_mutex); |
7b61ba8f | 734 | list_add_tail(&dev->devlist, &cx8802_devlist); |
344d6c6b | 735 | mutex_unlock(&cx8802_mutex); |
1da177e4 | 736 | |
77b74774 MR |
737 | /* now autoload cx88-dvb or cx88-blackbird */ |
738 | request_modules(dev); | |
6c5be74c ST |
739 | return 0; |
740 | ||
96df988b | 741 | fail_dev: |
6c5be74c ST |
742 | kfree(dev); |
743 | fail_core: | |
e32fadc4 | 744 | core->dvbdev = NULL; |
7b61ba8f | 745 | cx88_core_put(core, pci_dev); |
6c5be74c ST |
746 | return err; |
747 | } | |
748 | ||
4c62e976 | 749 | static void cx8802_remove(struct pci_dev *pci_dev) |
6c5be74c ST |
750 | { |
751 | struct cx8802_dev *dev; | |
6c5be74c ST |
752 | |
753 | dev = pci_get_drvdata(pci_dev); | |
754 | ||
65bc2fe8 | 755 | dprintk(1, "%s\n", __func__); |
6c5be74c | 756 | |
707bcf32 TH |
757 | flush_request_modules(dev); |
758 | ||
8a317a87 JN |
759 | mutex_lock(&dev->core->lock); |
760 | ||
081c2fc8 | 761 | if (!list_empty(&dev->drvlist)) { |
89a47942 | 762 | struct cx8802_driver *drv, *tmp; |
081c2fc8 TP |
763 | int err; |
764 | ||
65bc2fe8 | 765 | pr_warn("Trying to remove cx8802 driver while cx8802 sub-drivers still loaded?!\n"); |
081c2fc8 | 766 | |
89a47942 | 767 | list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) { |
081c2fc8 TP |
768 | err = drv->remove(drv); |
769 | if (err == 0) { | |
89a47942 | 770 | list_del(&drv->drvlist); |
081c2fc8 | 771 | } else |
65bc2fe8 MCC |
772 | pr_err("cx8802 driver remove failed (%d)\n", |
773 | err); | |
a04036a3 | 774 | kfree(drv); |
6c5be74c | 775 | } |
6c5be74c ST |
776 | } |
777 | ||
8a317a87 JN |
778 | mutex_unlock(&dev->core->lock); |
779 | ||
6c5be74c ST |
780 | /* Destroy any 8802 reference. */ |
781 | dev->core->dvbdev = NULL; | |
782 | ||
783 | /* common */ | |
784 | cx8802_fini_common(dev); | |
7b61ba8f | 785 | cx88_core_put(dev->core, dev->pci); |
6c5be74c ST |
786 | kfree(dev); |
787 | } | |
788 | ||
2e4e98e7 | 789 | static const struct pci_device_id cx8802_pci_tbl[] = { |
6c5be74c ST |
790 | { |
791 | .vendor = 0x14f1, | |
792 | .device = 0x8802, | |
793 | .subvendor = PCI_ANY_ID, | |
794 | .subdevice = PCI_ANY_ID, | |
7b61ba8f | 795 | }, { |
6c5be74c ST |
796 | /* --- end of list --- */ |
797 | } | |
798 | }; | |
799 | MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl); | |
800 | ||
801 | static struct pci_driver cx8802_pci_driver = { | |
802 | .name = "cx88-mpeg driver manager", | |
803 | .id_table = cx8802_pci_tbl, | |
804 | .probe = cx8802_probe, | |
4c62e976 | 805 | .remove = cx8802_remove, |
6c5be74c ST |
806 | }; |
807 | ||
38b25adc | 808 | module_pci_driver(cx8802_pci_driver); |