Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * |
3 | * Support for a cx23416 mpeg encoder via cx2388x host port. | |
4 | * "blackbird" reference design. | |
5 | * | |
6 | * (c) 2004 Jelle Foks <jelle@foks.8m.com> | |
7 | * (c) 2004 Gerd Knorr <kraxel@bytesex.org> | |
8 | * | |
9 | * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/), | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | */ | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/moduleparam.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/delay.h> | |
31 | #include <linux/device.h> | |
32 | #include <linux/firmware.h> | |
855dbada MK |
33 | #include <media/v4l2-common.h> |
34 | #include <media/cx2341x.h> | |
1da177e4 LT |
35 | |
36 | #include "cx88.h" | |
37 | ||
38 | MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards"); | |
d21838dd | 39 | MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]"); |
1da177e4 LT |
40 | MODULE_LICENSE("GPL"); |
41 | ||
31629424 | 42 | static unsigned int mpegbufs = 32; |
1da177e4 LT |
43 | module_param(mpegbufs,int,0644); |
44 | MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32"); | |
45 | ||
46 | static unsigned int debug = 0; | |
47 | module_param(debug,int,0644); | |
48 | MODULE_PARM_DESC(debug,"enable debug messages [blackbird]"); | |
49 | ||
50 | #define dprintk(level,fmt, arg...) if (debug >= level) \ | |
51 | printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg) | |
52 | ||
53 | static LIST_HEAD(cx8802_devlist); | |
54 | ||
55 | /* ------------------------------------------------------------------ */ | |
56 | ||
1da177e4 LT |
57 | #define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024 |
58 | ||
59 | /* defines below are from ivtv-driver.h */ | |
60 | ||
61 | #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF | |
62 | ||
b45009b0 | 63 | /* Firmware API commands */ |
b45009b0 MCC |
64 | #define IVTV_API_STD_TIMEOUT 500 |
65 | ||
b45009b0 MCC |
66 | enum blackbird_capture_type { |
67 | BLACKBIRD_MPEG_CAPTURE, | |
68 | BLACKBIRD_RAW_CAPTURE, | |
69 | BLACKBIRD_RAW_PASSTHRU_CAPTURE | |
70 | }; | |
71 | enum blackbird_capture_bits { | |
72 | BLACKBIRD_RAW_BITS_NONE = 0x00, | |
73 | BLACKBIRD_RAW_BITS_YUV_CAPTURE = 0x01, | |
74 | BLACKBIRD_RAW_BITS_PCM_CAPTURE = 0x02, | |
75 | BLACKBIRD_RAW_BITS_VBI_CAPTURE = 0x04, | |
76 | BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08, | |
77 | BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE = 0x10 | |
78 | }; | |
b45009b0 MCC |
79 | enum blackbird_capture_end { |
80 | BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */ | |
81 | BLACKBIRD_END_NOW, /* stop immediately, no irq */ | |
82 | }; | |
b45009b0 MCC |
83 | enum blackbird_framerate { |
84 | BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */ | |
85 | BLACKBIRD_FRAMERATE_PAL_25 /* PAL: 25fps */ | |
86 | }; | |
b45009b0 MCC |
87 | enum blackbird_stream_port { |
88 | BLACKBIRD_OUTPUT_PORT_MEMORY, | |
89 | BLACKBIRD_OUTPUT_PORT_STREAMING, | |
90 | BLACKBIRD_OUTPUT_PORT_SERIAL | |
91 | }; | |
b45009b0 MCC |
92 | enum blackbird_data_xfer_status { |
93 | BLACKBIRD_MORE_BUFFERS_FOLLOW, | |
94 | BLACKBIRD_LAST_BUFFER, | |
95 | }; | |
b45009b0 MCC |
96 | enum blackbird_picture_mask { |
97 | BLACKBIRD_PICTURE_MASK_NONE, | |
98 | BLACKBIRD_PICTURE_MASK_I_FRAMES, | |
99 | BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3, | |
100 | BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7, | |
101 | }; | |
b45009b0 MCC |
102 | enum blackbird_vbi_mode_bits { |
103 | BLACKBIRD_VBI_BITS_SLICED, | |
104 | BLACKBIRD_VBI_BITS_RAW, | |
105 | }; | |
106 | enum blackbird_vbi_insertion_bits { | |
107 | BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA, | |
108 | BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1, | |
109 | BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1, | |
110 | BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1, | |
111 | BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1, | |
112 | }; | |
b45009b0 MCC |
113 | enum blackbird_dma_unit { |
114 | BLACKBIRD_DMA_BYTES, | |
115 | BLACKBIRD_DMA_FRAMES, | |
116 | }; | |
b45009b0 MCC |
117 | enum blackbird_dma_transfer_status_bits { |
118 | BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01, | |
119 | BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04, | |
120 | BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10, | |
121 | }; | |
b45009b0 MCC |
122 | enum blackbird_pause { |
123 | BLACKBIRD_PAUSE_ENCODING, | |
124 | BLACKBIRD_RESUME_ENCODING, | |
125 | }; | |
b45009b0 MCC |
126 | enum blackbird_copyright { |
127 | BLACKBIRD_COPYRIGHT_OFF, | |
128 | BLACKBIRD_COPYRIGHT_ON, | |
129 | }; | |
b45009b0 MCC |
130 | enum blackbird_notification_type { |
131 | BLACKBIRD_NOTIFICATION_REFRESH, | |
132 | }; | |
133 | enum blackbird_notification_status { | |
134 | BLACKBIRD_NOTIFICATION_OFF, | |
135 | BLACKBIRD_NOTIFICATION_ON, | |
136 | }; | |
137 | enum blackbird_notification_mailbox { | |
138 | BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1, | |
139 | }; | |
b45009b0 MCC |
140 | enum blackbird_field1_lines { |
141 | BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */ | |
142 | BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */ | |
143 | BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */ | |
144 | }; | |
145 | enum blackbird_field2_lines { | |
146 | BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */ | |
147 | BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */ | |
148 | BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */ | |
149 | }; | |
b45009b0 MCC |
150 | enum blackbird_custom_data_type { |
151 | BLACKBIRD_CUSTOM_EXTENSION_USR_DATA, | |
152 | BLACKBIRD_CUSTOM_PRIVATE_PACKET, | |
153 | }; | |
b45009b0 MCC |
154 | enum blackbird_mute { |
155 | BLACKBIRD_UNMUTE, | |
156 | BLACKBIRD_MUTE, | |
157 | }; | |
158 | enum blackbird_mute_video_mask { | |
159 | BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00, | |
160 | BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000, | |
161 | BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000, | |
162 | }; | |
163 | enum blackbird_mute_video_shift { | |
164 | BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8, | |
165 | BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16, | |
166 | BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24, | |
167 | }; | |
1da177e4 LT |
168 | |
169 | /* Registers */ | |
170 | #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/) | |
171 | #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/) | |
172 | #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/) | |
173 | #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/) | |
174 | #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/) | |
175 | #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/) | |
176 | ||
177 | /* ------------------------------------------------------------------ */ | |
178 | ||
179 | static void host_setup(struct cx88_core *core) | |
180 | { | |
181 | /* toggle reset of the host */ | |
182 | cx_write(MO_GPHST_SOFT_RST, 1); | |
183 | udelay(100); | |
184 | cx_write(MO_GPHST_SOFT_RST, 0); | |
185 | udelay(100); | |
186 | ||
187 | /* host port setup */ | |
188 | cx_write(MO_GPHST_WSC, 0x44444444U); | |
189 | cx_write(MO_GPHST_XFR, 0); | |
190 | cx_write(MO_GPHST_WDTH, 15); | |
191 | cx_write(MO_GPHST_HDSHK, 0); | |
192 | cx_write(MO_GPHST_MUX16, 0x44448888U); | |
193 | cx_write(MO_GPHST_MODE, 0); | |
194 | } | |
195 | ||
196 | /* ------------------------------------------------------------------ */ | |
197 | ||
198 | #define P1_MDATA0 0x390000 | |
199 | #define P1_MDATA1 0x390001 | |
200 | #define P1_MDATA2 0x390002 | |
201 | #define P1_MDATA3 0x390003 | |
202 | #define P1_MADDR2 0x390004 | |
203 | #define P1_MADDR1 0x390005 | |
204 | #define P1_MADDR0 0x390006 | |
205 | #define P1_RDATA0 0x390008 | |
206 | #define P1_RDATA1 0x390009 | |
207 | #define P1_RDATA2 0x39000A | |
208 | #define P1_RDATA3 0x39000B | |
209 | #define P1_RADDR0 0x39000C | |
210 | #define P1_RADDR1 0x39000D | |
211 | #define P1_RRDWR 0x39000E | |
212 | ||
213 | static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state) | |
214 | { | |
215 | unsigned long timeout = jiffies + msecs_to_jiffies(1); | |
216 | u32 gpio0,need; | |
217 | ||
218 | need = state ? 2 : 0; | |
219 | for (;;) { | |
220 | gpio0 = cx_read(MO_GP0_IO) & 2; | |
221 | if (need == gpio0) | |
222 | return 0; | |
223 | if (time_after(jiffies,timeout)) | |
224 | return -1; | |
225 | udelay(1); | |
226 | } | |
227 | } | |
228 | ||
229 | static int memory_write(struct cx88_core *core, u32 address, u32 value) | |
230 | { | |
231 | /* Warning: address is dword address (4 bytes) */ | |
232 | cx_writeb(P1_MDATA0, (unsigned int)value); | |
233 | cx_writeb(P1_MDATA1, (unsigned int)(value >> 8)); | |
234 | cx_writeb(P1_MDATA2, (unsigned int)(value >> 16)); | |
235 | cx_writeb(P1_MDATA3, (unsigned int)(value >> 24)); | |
236 | cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40); | |
237 | cx_writeb(P1_MADDR1, (unsigned int)(address >> 8)); | |
238 | cx_writeb(P1_MADDR0, (unsigned int)address); | |
239 | cx_read(P1_MDATA0); | |
240 | cx_read(P1_MADDR0); | |
241 | ||
242 | return wait_ready_gpio0_bit1(core,1); | |
243 | } | |
244 | ||
245 | static int memory_read(struct cx88_core *core, u32 address, u32 *value) | |
246 | { | |
4ac97914 | 247 | int retval; |
1da177e4 LT |
248 | u32 val; |
249 | ||
250 | /* Warning: address is dword address (4 bytes) */ | |
251 | cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0); | |
252 | cx_writeb(P1_MADDR1, (unsigned int)(address >> 8)); | |
253 | cx_writeb(P1_MADDR0, (unsigned int)address); | |
254 | cx_read(P1_MADDR0); | |
255 | ||
256 | retval = wait_ready_gpio0_bit1(core,1); | |
257 | ||
258 | cx_writeb(P1_MDATA3, 0); | |
259 | val = (unsigned char)cx_read(P1_MDATA3) << 24; | |
260 | cx_writeb(P1_MDATA2, 0); | |
261 | val |= (unsigned char)cx_read(P1_MDATA2) << 16; | |
262 | cx_writeb(P1_MDATA1, 0); | |
263 | val |= (unsigned char)cx_read(P1_MDATA1) << 8; | |
264 | cx_writeb(P1_MDATA0, 0); | |
265 | val |= (unsigned char)cx_read(P1_MDATA0); | |
266 | ||
267 | *value = val; | |
268 | return retval; | |
269 | } | |
270 | ||
271 | static int register_write(struct cx88_core *core, u32 address, u32 value) | |
272 | { | |
273 | cx_writeb(P1_RDATA0, (unsigned int)value); | |
274 | cx_writeb(P1_RDATA1, (unsigned int)(value >> 8)); | |
275 | cx_writeb(P1_RDATA2, (unsigned int)(value >> 16)); | |
276 | cx_writeb(P1_RDATA3, (unsigned int)(value >> 24)); | |
277 | cx_writeb(P1_RADDR0, (unsigned int)address); | |
278 | cx_writeb(P1_RADDR1, (unsigned int)(address >> 8)); | |
279 | cx_writeb(P1_RRDWR, 1); | |
280 | cx_read(P1_RDATA0); | |
281 | cx_read(P1_RADDR0); | |
282 | ||
283 | return wait_ready_gpio0_bit1(core,1); | |
284 | } | |
285 | ||
286 | ||
287 | static int register_read(struct cx88_core *core, u32 address, u32 *value) | |
288 | { | |
289 | int retval; | |
290 | u32 val; | |
291 | ||
292 | cx_writeb(P1_RADDR0, (unsigned int)address); | |
293 | cx_writeb(P1_RADDR1, (unsigned int)(address >> 8)); | |
294 | cx_writeb(P1_RRDWR, 0); | |
295 | cx_read(P1_RADDR0); | |
296 | ||
297 | retval = wait_ready_gpio0_bit1(core,1); | |
298 | val = (unsigned char)cx_read(P1_RDATA0); | |
299 | val |= (unsigned char)cx_read(P1_RDATA1) << 8; | |
300 | val |= (unsigned char)cx_read(P1_RDATA2) << 16; | |
301 | val |= (unsigned char)cx_read(P1_RDATA3) << 24; | |
302 | ||
303 | *value = val; | |
304 | return retval; | |
305 | } | |
306 | ||
307 | /* ------------------------------------------------------------------ */ | |
308 | ||
f022156b | 309 | static int blackbird_mbox_func(void *priv, int command, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA]) |
1da177e4 | 310 | { |
f022156b | 311 | struct cx8802_dev *dev = priv; |
1da177e4 LT |
312 | unsigned long timeout; |
313 | u32 value, flag, retval; | |
314 | int i; | |
1da177e4 LT |
315 | |
316 | dprintk(1,"%s: 0x%X\n", __FUNCTION__, command); | |
317 | ||
318 | /* this may not be 100% safe if we can't read any memory location | |
319 | without side effects */ | |
320 | memory_read(dev->core, dev->mailbox - 4, &value); | |
321 | if (value != 0x12345678) { | |
322 | dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n"); | |
323 | return -1; | |
324 | } | |
325 | ||
326 | memory_read(dev->core, dev->mailbox, &flag); | |
327 | if (flag) { | |
328 | dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag); | |
329 | return -1; | |
330 | } | |
331 | ||
332 | flag |= 1; /* tell 'em we're working on it */ | |
333 | memory_write(dev->core, dev->mailbox, flag); | |
334 | ||
335 | /* write command + args + fill remaining with zeros */ | |
336 | memory_write(dev->core, dev->mailbox + 1, command); /* command code */ | |
337 | memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */ | |
f022156b HV |
338 | for (i = 0; i < in; i++) { |
339 | memory_write(dev->core, dev->mailbox + 4 + i, data[i]); | |
340 | dprintk(1, "API Input %d = %d\n", i, data[i]); | |
1da177e4 | 341 | } |
f022156b | 342 | for (; i < CX2341X_MBOX_MAX_DATA; i++) |
1da177e4 LT |
343 | memory_write(dev->core, dev->mailbox + 4 + i, 0); |
344 | ||
345 | flag |= 3; /* tell 'em we're done writing */ | |
346 | memory_write(dev->core, dev->mailbox, flag); | |
347 | ||
348 | /* wait for firmware to handle the API command */ | |
349 | timeout = jiffies + msecs_to_jiffies(10); | |
350 | for (;;) { | |
351 | memory_read(dev->core, dev->mailbox, &flag); | |
352 | if (0 != (flag & 4)) | |
353 | break; | |
354 | if (time_after(jiffies,timeout)) { | |
355 | dprintk(0, "ERROR: API Mailbox timeout\n"); | |
356 | return -1; | |
357 | } | |
358 | udelay(10); | |
359 | } | |
360 | ||
361 | /* read output values */ | |
f022156b HV |
362 | for (i = 0; i < out; i++) { |
363 | memory_read(dev->core, dev->mailbox + 4 + i, data + i); | |
364 | dprintk(1, "API Output %d = %d\n", i, data[i]); | |
1da177e4 | 365 | } |
1da177e4 LT |
366 | |
367 | memory_read(dev->core, dev->mailbox + 2, &retval); | |
368 | dprintk(1, "API result = %d\n",retval); | |
369 | ||
370 | flag = 0; | |
371 | memory_write(dev->core, dev->mailbox, flag); | |
372 | return retval; | |
373 | } | |
f022156b | 374 | /* ------------------------------------------------------------------ */ |
1da177e4 | 375 | |
f022156b HV |
376 | /* We don't need to call the API often, so using just one mailbox will probably suffice */ |
377 | static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command, | |
378 | u32 inputcnt, u32 outputcnt, ...) | |
379 | { | |
380 | u32 data[CX2341X_MBOX_MAX_DATA]; | |
381 | va_list vargs; | |
382 | int i, err; | |
383 | ||
384 | va_start(vargs, outputcnt); | |
385 | ||
386 | for (i = 0; i < inputcnt; i++) { | |
387 | data[i] = va_arg(vargs, int); | |
388 | } | |
389 | err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data); | |
390 | for (i = 0; i < outputcnt; i++) { | |
391 | int *vptr = va_arg(vargs, int *); | |
392 | *vptr = data[i]; | |
393 | } | |
394 | va_end(vargs); | |
395 | return err; | |
396 | } | |
1da177e4 LT |
397 | |
398 | static int blackbird_find_mailbox(struct cx8802_dev *dev) | |
399 | { | |
400 | u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456}; | |
401 | int signaturecnt=0; | |
402 | u32 value; | |
403 | int i; | |
404 | ||
405 | for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) { | |
406 | memory_read(dev->core, i, &value); | |
407 | if (value == signature[signaturecnt]) | |
408 | signaturecnt++; | |
409 | else | |
410 | signaturecnt = 0; | |
411 | if (4 == signaturecnt) { | |
412 | dprintk(1, "Mailbox signature found\n"); | |
413 | return i+1; | |
414 | } | |
415 | } | |
416 | dprintk(0, "Mailbox signature values not found!\n"); | |
417 | return -1; | |
418 | } | |
419 | ||
420 | static int blackbird_load_firmware(struct cx8802_dev *dev) | |
421 | { | |
422 | static const unsigned char magic[8] = { | |
423 | 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa | |
424 | }; | |
425 | const struct firmware *firmware; | |
426 | int i, retval = 0; | |
427 | u32 value = 0; | |
428 | u32 checksum = 0; | |
429 | u32 *dataptr; | |
430 | ||
431 | retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED); | |
4ac97914 MCC |
432 | retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST); |
433 | retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640); | |
434 | retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A); | |
1da177e4 | 435 | msleep(1); |
4ac97914 | 436 | retval |= register_write(dev->core, IVTV_REG_APU, 0); |
1da177e4 LT |
437 | |
438 | if (retval < 0) | |
439 | dprintk(0, "Error with register_write\n"); | |
440 | ||
48c35756 | 441 | retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME, |
1da177e4 | 442 | &dev->pci->dev); |
674434c6 MCC |
443 | |
444 | ||
1da177e4 LT |
445 | if (retval != 0) { |
446 | dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n", | |
48c35756 | 447 | CX2341X_FIRM_ENC_FILENAME); |
1da177e4 LT |
448 | dprintk(0, "Please fix your hotplug setup, the board will " |
449 | "not work without firmware loaded!\n"); | |
450 | return -1; | |
451 | } | |
452 | ||
453 | if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) { | |
454 | dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n", | |
455 | firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE); | |
456 | return -1; | |
457 | } | |
458 | ||
459 | if (0 != memcmp(firmware->data, magic, 8)) { | |
460 | dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n"); | |
461 | return -1; | |
462 | } | |
463 | ||
464 | /* transfer to the chip */ | |
465 | dprintk(1,"Loading firmware ...\n"); | |
466 | dataptr = (u32*)firmware->data; | |
467 | for (i = 0; i < (firmware->size >> 2); i++) { | |
468 | value = *dataptr; | |
469 | checksum += ~value; | |
470 | memory_write(dev->core, i, value); | |
471 | dataptr++; | |
472 | } | |
473 | ||
474 | /* read back to verify with the checksum */ | |
475 | for (i--; i >= 0; i--) { | |
476 | memory_read(dev->core, i, &value); | |
477 | checksum -= ~value; | |
478 | } | |
479 | if (checksum) { | |
480 | dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n"); | |
481 | return -1; | |
482 | } | |
483 | release_firmware(firmware); | |
484 | dprintk(0, "Firmware upload successful.\n"); | |
485 | ||
4ac97914 MCC |
486 | retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST); |
487 | retval |= register_read(dev->core, IVTV_REG_SPU, &value); | |
488 | retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE); | |
1da177e4 LT |
489 | msleep(1); |
490 | ||
491 | retval |= register_read(dev->core, IVTV_REG_VPU, &value); | |
4ac97914 | 492 | retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8); |
1da177e4 LT |
493 | |
494 | if (retval < 0) | |
495 | dprintk(0, "Error with register_write\n"); | |
496 | return 0; | |
497 | } | |
498 | ||
b45009b0 MCC |
499 | /** |
500 | Settings used by the windows tv app for PVR2000: | |
501 | ================================================================================================================= | |
502 | Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode | |
503 | ----------------------------------------------------------------------------------------------------------------- | |
504 | MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo | |
505 | MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo | |
506 | VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo | |
507 | DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo | |
508 | DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo | |
509 | ================================================================================================================= | |
510 | *DB: "DirectBurn" | |
511 | */ | |
31629424 | 512 | |
f022156b HV |
513 | static void blackbird_codec_settings(struct cx8802_dev *dev) |
514 | { | |
515 | /* assign frame size */ | |
516 | blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0, | |
517 | dev->height, dev->width); | |
518 | ||
519 | dev->params.width = dev->width; | |
520 | dev->params.height = dev->height; | |
521 | dev->params.is_50hz = (dev->core->tvnorm->id & V4L2_STD_625_50) != 0; | |
522 | ||
523 | cx2341x_update(dev, blackbird_mbox_func, NULL, &dev->params); | |
524 | } | |
525 | ||
31629424 CC |
526 | static struct v4l2_mpeg_compression default_mpeg_params = { |
527 | .st_type = V4L2_MPEG_PS_2, | |
528 | .st_bitrate = { | |
529 | .mode = V4L2_BITRATE_CBR, | |
530 | .min = 0, | |
531 | .target = 0, | |
532 | .max = 0 | |
533 | }, | |
534 | .ts_pid_pmt = 16, | |
535 | .ts_pid_audio = 260, | |
536 | .ts_pid_video = 256, | |
537 | .ts_pid_pcr = 259, | |
538 | .ps_size = 0, | |
539 | .au_type = V4L2_MPEG_AU_2_II, | |
540 | .au_bitrate = { | |
541 | .mode = V4L2_BITRATE_CBR, | |
542 | .min = 224, | |
543 | .target = 224, | |
544 | .max = 224 | |
545 | }, | |
f022156b | 546 | .au_sample_rate = 48000, |
31629424 CC |
547 | .au_pesid = 0, |
548 | .vi_type = V4L2_MPEG_VI_2, | |
549 | .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3, | |
550 | .vi_bitrate = { | |
551 | .mode = V4L2_BITRATE_CBR, | |
552 | .min = 4000, | |
553 | .target = 4500, | |
554 | .max = 6000 | |
555 | }, | |
556 | .vi_frame_rate = 25, | |
f022156b | 557 | .vi_frames_per_gop = 12, |
31629424 CC |
558 | .vi_bframes_count = 2, |
559 | .vi_pesid = 0, | |
f022156b | 560 | .closed_gops = 1, |
31629424 CC |
561 | .pulldown = 0 |
562 | }; | |
563 | ||
1da177e4 LT |
564 | static int blackbird_initialize_codec(struct cx8802_dev *dev) |
565 | { | |
566 | struct cx88_core *core = dev->core; | |
567 | int version; | |
568 | int retval; | |
569 | ||
570 | dprintk(1,"Initialize codec\n"); | |
855dbada | 571 | retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */ |
1da177e4 LT |
572 | if (retval < 0) { |
573 | /* ping was not successful, reset and upload firmware */ | |
574 | cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */ | |
575 | msleep(1); | |
576 | cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */ | |
577 | msleep(1); | |
578 | retval = blackbird_load_firmware(dev); | |
579 | if (retval < 0) | |
580 | return retval; | |
581 | ||
582 | dev->mailbox = blackbird_find_mailbox(dev); | |
583 | if (dev->mailbox < 0) | |
584 | return -1; | |
585 | ||
855dbada | 586 | retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */ |
1da177e4 LT |
587 | if (retval < 0) { |
588 | dprintk(0, "ERROR: Firmware ping failed!\n"); | |
589 | return -1; | |
590 | } | |
591 | ||
855dbada | 592 | retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version); |
1da177e4 LT |
593 | if (retval < 0) { |
594 | dprintk(0, "ERROR: Firmware get encoder version failed!\n"); | |
595 | return -1; | |
596 | } | |
597 | dprintk(0, "Firmware version is 0x%08x\n", version); | |
598 | } | |
599 | msleep(1); | |
600 | ||
601 | cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */ | |
602 | cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */ | |
603 | cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */ | |
604 | cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */ | |
605 | ||
1da177e4 LT |
606 | blackbird_codec_settings(dev); |
607 | msleep(1); | |
608 | ||
b45009b0 MCC |
609 | /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef); |
610 | blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0); | |
611 | blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */ | |
855dbada | 612 | blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0, |
b45009b0 | 613 | BLACKBIRD_FIELD1_SAA7115, |
8a17ef97 | 614 | BLACKBIRD_FIELD2_SAA7115 |
b45009b0 MCC |
615 | ); |
616 | ||
617 | /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */ | |
855dbada | 618 | blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0, |
b45009b0 MCC |
619 | BLACKBIRD_CUSTOM_EXTENSION_USR_DATA, |
620 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
1da177e4 | 621 | |
e52e98a7 | 622 | /* initialize the video input */ |
855dbada | 623 | blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0); |
1da177e4 LT |
624 | |
625 | msleep(1); | |
626 | ||
855dbada | 627 | blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE); |
1da177e4 | 628 | msleep(1); |
855dbada | 629 | blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE); |
1da177e4 LT |
630 | msleep(1); |
631 | ||
e52e98a7 | 632 | /* start capturing to the host interface */ |
855dbada MK |
633 | /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */ |
634 | blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, | |
b45009b0 MCC |
635 | BLACKBIRD_MPEG_CAPTURE, |
636 | BLACKBIRD_RAW_BITS_NONE | |
e52e98a7 | 637 | ); |
b45009b0 | 638 | msleep(10); |
1da177e4 | 639 | |
855dbada | 640 | blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0); |
1da177e4 LT |
641 | return 0; |
642 | } | |
643 | ||
644 | /* ------------------------------------------------------------------ */ | |
645 | ||
646 | static int bb_buf_setup(struct videobuf_queue *q, | |
647 | unsigned int *count, unsigned int *size) | |
648 | { | |
649 | struct cx8802_fh *fh = q->priv_data; | |
650 | ||
e52e98a7 | 651 | fh->dev->ts_packet_size = 188 * 4; /* was: 512 */ |
31629424 | 652 | fh->dev->ts_packet_count = mpegbufs; /* was: 100 */ |
1da177e4 LT |
653 | |
654 | *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count; | |
31629424 | 655 | *count = fh->dev->ts_packet_count; |
1da177e4 LT |
656 | return 0; |
657 | } | |
658 | ||
659 | static int | |
660 | bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, | |
661 | enum v4l2_field field) | |
662 | { | |
663 | struct cx8802_fh *fh = q->priv_data; | |
c7b0ac05 | 664 | return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field); |
1da177e4 LT |
665 | } |
666 | ||
667 | static void | |
668 | bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) | |
669 | { | |
670 | struct cx8802_fh *fh = q->priv_data; | |
671 | cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb); | |
672 | } | |
673 | ||
674 | static void | |
675 | bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb) | |
676 | { | |
c7b0ac05 | 677 | cx88_free_buffer(q, (struct cx88_buffer*)vb); |
1da177e4 LT |
678 | } |
679 | ||
680 | static struct videobuf_queue_ops blackbird_qops = { | |
681 | .buf_setup = bb_buf_setup, | |
682 | .buf_prepare = bb_buf_prepare, | |
683 | .buf_queue = bb_buf_queue, | |
684 | .buf_release = bb_buf_release, | |
685 | }; | |
686 | ||
687 | /* ------------------------------------------------------------------ */ | |
688 | ||
689 | static int mpeg_do_ioctl(struct inode *inode, struct file *file, | |
690 | unsigned int cmd, void *arg) | |
691 | { | |
692 | struct cx8802_fh *fh = file->private_data; | |
693 | struct cx8802_dev *dev = fh->dev; | |
e52e98a7 | 694 | struct cx88_core *core = dev->core; |
1da177e4 LT |
695 | |
696 | if (debug > 1) | |
5e453dc7 | 697 | v4l_print_ioctl(core->name,cmd); |
1da177e4 LT |
698 | |
699 | switch (cmd) { | |
700 | ||
e52e98a7 MCC |
701 | /* --- capabilities ------------------------------------------ */ |
702 | case VIDIOC_QUERYCAP: | |
703 | { | |
704 | struct v4l2_capability *cap = arg; | |
705 | ||
706 | memset(cap,0,sizeof(*cap)); | |
707 | strcpy(cap->driver, "cx88_blackbird"); | |
708 | strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card)); | |
709 | sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci)); | |
710 | cap->version = CX88_VERSION_CODE; | |
711 | cap->capabilities = | |
712 | V4L2_CAP_VIDEO_CAPTURE | | |
713 | V4L2_CAP_READWRITE | | |
714 | V4L2_CAP_STREAMING | | |
e52e98a7 MCC |
715 | 0; |
716 | if (UNSET != core->tuner_type) | |
717 | cap->capabilities |= V4L2_CAP_TUNER; | |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
1da177e4 LT |
722 | /* --- capture ioctls ---------------------------------------- */ |
723 | case VIDIOC_ENUM_FMT: | |
724 | { | |
725 | struct v4l2_fmtdesc *f = arg; | |
726 | int index; | |
727 | ||
728 | index = f->index; | |
729 | if (index != 0) | |
730 | return -EINVAL; | |
731 | ||
732 | memset(f,0,sizeof(*f)); | |
733 | f->index = index; | |
31629424 | 734 | strlcpy(f->description, "MPEG", sizeof(f->description)); |
1da177e4 LT |
735 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
736 | f->pixelformat = V4L2_PIX_FMT_MPEG; | |
737 | return 0; | |
738 | } | |
739 | case VIDIOC_G_FMT: | |
1da177e4 | 740 | { |
1da177e4 LT |
741 | struct v4l2_format *f = arg; |
742 | ||
743 | memset(f,0,sizeof(*f)); | |
744 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
31629424 CC |
745 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
746 | f->fmt.pix.bytesperline = 0; | |
747 | f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */ | |
748 | f->fmt.pix.colorspace = 0; | |
1da177e4 LT |
749 | f->fmt.pix.width = dev->width; |
750 | f->fmt.pix.height = dev->height; | |
31629424 CC |
751 | f->fmt.pix.field = fh->mpegq.field; |
752 | dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n", | |
753 | dev->width, dev->height, fh->mpegq.field ); | |
754 | return 0; | |
755 | } | |
756 | case VIDIOC_TRY_FMT: | |
757 | { | |
758 | struct v4l2_format *f = arg; | |
759 | ||
760 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1da177e4 | 761 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
e52e98a7 | 762 | f->fmt.pix.bytesperline = 0; |
31629424 | 763 | f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */; |
e52e98a7 | 764 | f->fmt.pix.colorspace = 0; |
31629424 CC |
765 | dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n", |
766 | dev->width, dev->height, fh->mpegq.field ); | |
767 | return 0; | |
768 | } | |
769 | case VIDIOC_S_FMT: | |
770 | { | |
771 | struct v4l2_format *f = arg; | |
772 | ||
773 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
774 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; | |
775 | f->fmt.pix.bytesperline = 0; | |
776 | f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */; | |
777 | f->fmt.pix.colorspace = 0; | |
778 | dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n", | |
779 | f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field ); | |
e52e98a7 | 780 | return 0; |
1da177e4 LT |
781 | } |
782 | ||
783 | /* --- streaming capture ------------------------------------- */ | |
784 | case VIDIOC_REQBUFS: | |
785 | return videobuf_reqbufs(&fh->mpegq, arg); | |
786 | ||
787 | case VIDIOC_QUERYBUF: | |
788 | return videobuf_querybuf(&fh->mpegq, arg); | |
789 | ||
790 | case VIDIOC_QBUF: | |
791 | return videobuf_qbuf(&fh->mpegq, arg); | |
792 | ||
793 | case VIDIOC_DQBUF: | |
794 | return videobuf_dqbuf(&fh->mpegq, arg, | |
795 | file->f_flags & O_NONBLOCK); | |
796 | ||
797 | case VIDIOC_STREAMON: | |
798 | return videobuf_streamon(&fh->mpegq); | |
799 | ||
800 | case VIDIOC_STREAMOFF: | |
801 | return videobuf_streamoff(&fh->mpegq); | |
802 | ||
31629424 CC |
803 | /* --- mpeg compression -------------------------------------- */ |
804 | case VIDIOC_G_MPEGCOMP: | |
805 | { | |
806 | struct v4l2_mpeg_compression *f = arg; | |
807 | ||
f81cf753 HV |
808 | printk(KERN_WARNING "VIDIOC_G_MPEGCOMP is obsolete. " |
809 | "Replace with VIDIOC_G_EXT_CTRLS!"); | |
f022156b | 810 | memcpy(f,&default_mpeg_params,sizeof(*f)); |
31629424 CC |
811 | return 0; |
812 | } | |
813 | case VIDIOC_S_MPEGCOMP: | |
f81cf753 HV |
814 | printk(KERN_WARNING "VIDIOC_S_MPEGCOMP is obsolete. " |
815 | "Replace with VIDIOC_S_EXT_CTRLS!"); | |
f022156b HV |
816 | return 0; |
817 | case VIDIOC_G_EXT_CTRLS: | |
31629424 | 818 | { |
f022156b | 819 | struct v4l2_ext_controls *f = arg; |
31629424 | 820 | |
f022156b HV |
821 | if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG) |
822 | return -EINVAL; | |
823 | return cx2341x_ext_ctrls(&dev->params, f, cmd); | |
824 | } | |
825 | case VIDIOC_S_EXT_CTRLS: | |
826 | case VIDIOC_TRY_EXT_CTRLS: | |
827 | { | |
828 | struct v4l2_ext_controls *f = arg; | |
829 | struct cx2341x_mpeg_params p; | |
830 | int err; | |
831 | ||
832 | if (f->ctrl_class != V4L2_CTRL_CLASS_MPEG) | |
833 | return -EINVAL; | |
834 | p = dev->params; | |
835 | err = cx2341x_ext_ctrls(&p, f, cmd); | |
836 | if (err == 0 && cmd == VIDIOC_S_EXT_CTRLS) { | |
837 | err = cx2341x_update(dev, blackbird_mbox_func, &dev->params, &p); | |
838 | dev->params = p; | |
839 | } | |
840 | return err; | |
31629424 | 841 | } |
2544bf2d VZ |
842 | case VIDIOC_S_FREQUENCY: |
843 | { | |
844 | blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, | |
845 | BLACKBIRD_END_NOW, | |
846 | BLACKBIRD_MPEG_CAPTURE, | |
847 | BLACKBIRD_RAW_BITS_NONE); | |
848 | ||
849 | cx88_do_ioctl( inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook ); | |
850 | ||
851 | blackbird_initialize_codec(dev); | |
852 | cx88_set_scale(dev->core, dev->width, dev->height, | |
853 | fh->mpegq.field); | |
854 | return 0; | |
855 | } | |
31629424 | 856 | |
1da177e4 | 857 | default: |
e52e98a7 | 858 | return cx88_do_ioctl( inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook ); |
1da177e4 LT |
859 | } |
860 | return 0; | |
861 | } | |
862 | ||
e52e98a7 MCC |
863 | int (*cx88_ioctl_hook)(struct inode *inode, struct file *file, |
864 | unsigned int cmd, void *arg); | |
865 | unsigned int (*cx88_ioctl_translator)(unsigned int cmd); | |
866 | ||
867 | static unsigned int mpeg_translate_ioctl(unsigned int cmd) | |
868 | { | |
869 | return cmd; | |
870 | } | |
871 | ||
1da177e4 | 872 | static int mpeg_ioctl(struct inode *inode, struct file *file, |
e52e98a7 | 873 | unsigned int cmd, unsigned long arg) |
1da177e4 | 874 | { |
e52e98a7 MCC |
875 | cmd = cx88_ioctl_translator( cmd ); |
876 | return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook); | |
1da177e4 LT |
877 | } |
878 | ||
879 | static int mpeg_open(struct inode *inode, struct file *file) | |
880 | { | |
881 | int minor = iminor(inode); | |
882 | struct cx8802_dev *h,*dev = NULL; | |
883 | struct cx8802_fh *fh; | |
884 | struct list_head *list; | |
885 | ||
886 | list_for_each(list,&cx8802_devlist) { | |
887 | h = list_entry(list, struct cx8802_dev, devlist); | |
888 | if (h->mpeg_dev->minor == minor) | |
889 | dev = h; | |
890 | } | |
891 | if (NULL == dev) | |
892 | return -ENODEV; | |
893 | ||
894 | if (blackbird_initialize_codec(dev) < 0) | |
895 | return -EINVAL; | |
896 | dprintk(1,"open minor=%d\n",minor); | |
897 | ||
898 | /* allocate + initialize per filehandle data */ | |
7408187d | 899 | fh = kzalloc(sizeof(*fh),GFP_KERNEL); |
1da177e4 LT |
900 | if (NULL == fh) |
901 | return -ENOMEM; | |
1da177e4 LT |
902 | file->private_data = fh; |
903 | fh->dev = dev; | |
904 | ||
1da177e4 LT |
905 | videobuf_queue_init(&fh->mpegq, &blackbird_qops, |
906 | dev->pci, &dev->slock, | |
907 | V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
31629424 | 908 | V4L2_FIELD_INTERLACED, |
1da177e4 LT |
909 | sizeof(struct cx88_buffer), |
910 | fh); | |
31629424 CC |
911 | |
912 | /* FIXME: locking against other video device */ | |
913 | cx88_set_scale(dev->core, dev->width, dev->height, | |
914 | fh->mpegq.field); | |
915 | ||
1da177e4 LT |
916 | return 0; |
917 | } | |
918 | ||
919 | static int mpeg_release(struct inode *inode, struct file *file) | |
920 | { | |
921 | struct cx8802_fh *fh = file->private_data; | |
922 | ||
855dbada MK |
923 | /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */ |
924 | blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, | |
b45009b0 MCC |
925 | BLACKBIRD_END_NOW, |
926 | BLACKBIRD_MPEG_CAPTURE, | |
927 | BLACKBIRD_RAW_BITS_NONE | |
928 | ); | |
1da177e4 | 929 | |
0b5f56d6 | 930 | cx8802_cancel_buffers(fh->dev); |
1da177e4 LT |
931 | /* stop mpeg capture */ |
932 | if (fh->mpegq.streaming) | |
933 | videobuf_streamoff(&fh->mpegq); | |
934 | if (fh->mpegq.reading) | |
935 | videobuf_read_stop(&fh->mpegq); | |
936 | ||
937 | videobuf_mmap_free(&fh->mpegq); | |
938 | file->private_data = NULL; | |
939 | kfree(fh); | |
940 | return 0; | |
941 | } | |
942 | ||
943 | static ssize_t | |
944 | mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos) | |
945 | { | |
946 | struct cx8802_fh *fh = file->private_data; | |
947 | ||
948 | return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0, | |
949 | file->f_flags & O_NONBLOCK); | |
950 | } | |
951 | ||
952 | static unsigned int | |
953 | mpeg_poll(struct file *file, struct poll_table_struct *wait) | |
954 | { | |
955 | struct cx8802_fh *fh = file->private_data; | |
956 | ||
957 | return videobuf_poll_stream(file, &fh->mpegq, wait); | |
958 | } | |
959 | ||
960 | static int | |
961 | mpeg_mmap(struct file *file, struct vm_area_struct * vma) | |
962 | { | |
963 | struct cx8802_fh *fh = file->private_data; | |
964 | ||
965 | return videobuf_mmap_mapper(&fh->mpegq, vma); | |
966 | } | |
967 | ||
968 | static struct file_operations mpeg_fops = | |
969 | { | |
970 | .owner = THIS_MODULE, | |
971 | .open = mpeg_open, | |
972 | .release = mpeg_release, | |
973 | .read = mpeg_read, | |
974 | .poll = mpeg_poll, | |
975 | .mmap = mpeg_mmap, | |
976 | .ioctl = mpeg_ioctl, | |
977 | .llseek = no_llseek, | |
978 | }; | |
979 | ||
980 | static struct video_device cx8802_mpeg_template = | |
981 | { | |
982 | .name = "cx8802", | |
983 | .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER, | |
984 | .hardware = 0, | |
985 | .fops = &mpeg_fops, | |
986 | .minor = -1, | |
987 | }; | |
988 | ||
989 | /* ------------------------------------------------------------------ */ | |
990 | ||
991 | static void blackbird_unregister_video(struct cx8802_dev *dev) | |
992 | { | |
993 | if (dev->mpeg_dev) { | |
994 | if (-1 != dev->mpeg_dev->minor) | |
995 | video_unregister_device(dev->mpeg_dev); | |
996 | else | |
997 | video_device_release(dev->mpeg_dev); | |
998 | dev->mpeg_dev = NULL; | |
999 | } | |
1000 | } | |
1001 | ||
1002 | static int blackbird_register_video(struct cx8802_dev *dev) | |
1003 | { | |
1004 | int err; | |
1005 | ||
1006 | dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci, | |
1007 | &cx8802_mpeg_template,"mpeg"); | |
1008 | err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1); | |
1009 | if (err < 0) { | |
1010 | printk(KERN_INFO "%s/2: can't register mpeg device\n", | |
1011 | dev->core->name); | |
1012 | return err; | |
1013 | } | |
1014 | printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n", | |
1015 | dev->core->name,dev->mpeg_dev->minor & 0x1f); | |
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | /* ----------------------------------------------------------- */ | |
1020 | ||
1021 | static int __devinit blackbird_probe(struct pci_dev *pci_dev, | |
1022 | const struct pci_device_id *pci_id) | |
1023 | { | |
1024 | struct cx8802_dev *dev; | |
1025 | struct cx88_core *core; | |
1026 | int err; | |
1027 | ||
1028 | /* general setup */ | |
1029 | core = cx88_core_get(pci_dev); | |
1030 | if (NULL == core) | |
1031 | return -EINVAL; | |
1032 | ||
1033 | err = -ENODEV; | |
1034 | if (!cx88_boards[core->board].blackbird) | |
1035 | goto fail_core; | |
1036 | ||
1037 | err = -ENOMEM; | |
7408187d | 1038 | dev = kzalloc(sizeof(*dev),GFP_KERNEL); |
1da177e4 LT |
1039 | if (NULL == dev) |
1040 | goto fail_core; | |
1da177e4 LT |
1041 | dev->pci = pci_dev; |
1042 | dev->core = core; | |
1043 | dev->width = 720; | |
e52e98a7 | 1044 | dev->height = 576; |
f022156b | 1045 | cx2341x_fill_defaults(&dev->params); |
45ad9f8b | 1046 | dev->params.port = CX2341X_PORT_STREAMING; |
1da177e4 | 1047 | |
01a9cd99 MK |
1048 | switch (core->board) { |
1049 | case CX88_BOARD_HAUPPAUGE_ROSLYN: | |
3febc04d MK |
1050 | if (core->tuner_formats & V4L2_STD_525_60) { |
1051 | dev->height = 480; | |
3febc04d MK |
1052 | } else { |
1053 | dev->height = 576; | |
3febc04d | 1054 | } |
01a9cd99 MK |
1055 | break; |
1056 | case CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT: | |
1057 | if (core->tvnorm->id & V4L2_STD_525_60) { | |
1058 | dev->height = 480; | |
01a9cd99 MK |
1059 | } else { |
1060 | dev->height = 576; | |
01a9cd99 MK |
1061 | } |
1062 | break; | |
0345c387 ST |
1063 | } |
1064 | ||
1da177e4 LT |
1065 | err = cx8802_init_common(dev); |
1066 | if (0 != err) | |
1067 | goto fail_free; | |
1068 | ||
1069 | /* blackbird stuff */ | |
1070 | printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n", | |
1071 | core->name); | |
1072 | host_setup(dev->core); | |
1073 | ||
1074 | list_add_tail(&dev->devlist,&cx8802_devlist); | |
1075 | blackbird_register_video(dev); | |
e52e98a7 MCC |
1076 | |
1077 | /* initial device configuration: needed ? */ | |
1078 | ||
1da177e4 LT |
1079 | return 0; |
1080 | ||
1081 | fail_free: | |
1082 | kfree(dev); | |
1083 | fail_core: | |
1084 | cx88_core_put(core,pci_dev); | |
1085 | return err; | |
1086 | } | |
1087 | ||
1088 | static void __devexit blackbird_remove(struct pci_dev *pci_dev) | |
1089 | { | |
4ac97914 | 1090 | struct cx8802_dev *dev = pci_get_drvdata(pci_dev); |
1da177e4 LT |
1091 | |
1092 | /* blackbird */ | |
1093 | blackbird_unregister_video(dev); | |
1094 | list_del(&dev->devlist); | |
1095 | ||
1096 | /* common */ | |
1097 | cx8802_fini_common(dev); | |
1098 | cx88_core_put(dev->core,dev->pci); | |
1099 | kfree(dev); | |
1100 | } | |
1101 | ||
1102 | static struct pci_device_id cx8802_pci_tbl[] = { | |
1103 | { | |
1104 | .vendor = 0x14f1, | |
1105 | .device = 0x8802, | |
4ac97914 MCC |
1106 | .subvendor = PCI_ANY_ID, |
1107 | .subdevice = PCI_ANY_ID, | |
1da177e4 LT |
1108 | },{ |
1109 | /* --- end of list --- */ | |
1110 | } | |
1111 | }; | |
1112 | MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl); | |
1113 | ||
1114 | static struct pci_driver blackbird_pci_driver = { | |
4ac97914 MCC |
1115 | .name = "cx88-blackbird", |
1116 | .id_table = cx8802_pci_tbl, | |
1117 | .probe = blackbird_probe, | |
1118 | .remove = __devexit_p(blackbird_remove), | |
1da177e4 LT |
1119 | .suspend = cx8802_suspend_common, |
1120 | .resume = cx8802_resume_common, | |
1121 | }; | |
1122 | ||
1123 | static int blackbird_init(void) | |
1124 | { | |
1125 | printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n", | |
1126 | (CX88_VERSION_CODE >> 16) & 0xff, | |
1127 | (CX88_VERSION_CODE >> 8) & 0xff, | |
1128 | CX88_VERSION_CODE & 0xff); | |
1129 | #ifdef SNAPSHOT | |
1130 | printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n", | |
1131 | SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100); | |
1132 | #endif | |
e52e98a7 MCC |
1133 | cx88_ioctl_hook = mpeg_do_ioctl; |
1134 | cx88_ioctl_translator = mpeg_translate_ioctl; | |
1da177e4 LT |
1135 | return pci_register_driver(&blackbird_pci_driver); |
1136 | } | |
1137 | ||
1138 | static void blackbird_fini(void) | |
1139 | { | |
1140 | pci_unregister_driver(&blackbird_pci_driver); | |
1141 | } | |
1142 | ||
1143 | module_init(blackbird_init); | |
1144 | module_exit(blackbird_fini); | |
1145 | ||
e52e98a7 MCC |
1146 | EXPORT_SYMBOL(cx88_ioctl_hook); |
1147 | EXPORT_SYMBOL(cx88_ioctl_translator); | |
1148 | ||
1da177e4 LT |
1149 | /* ----------------------------------------------------------- */ |
1150 | /* | |
1151 | * Local variables: | |
1152 | * c-basic-offset: 8 | |
1153 | * End: | |
b45009b0 | 1154 | * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off |
1da177e4 | 1155 | */ |