blob: 80f5f563d4fc4867d5f156ab0364b8744e0436b3 [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 driver internal defines and structures
3 *
4 * Derived from ivtv-driver.h
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24#ifndef CX18_DRIVER_H
25#define CX18_DRIVER_H
26
27#include <linux/version.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/delay.h>
32#include <linux/sched.h>
33#include <linux/fs.h>
34#include <linux/pci.h>
35#include <linux/interrupt.h>
36#include <linux/spinlock.h>
37#include <linux/i2c.h>
38#include <linux/i2c-algo-bit.h>
39#include <linux/list.h>
40#include <linux/unistd.h>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030041#include <linux/pagemap.h>
42#include <linux/workqueue.h>
43#include <linux/mutex.h>
44
45#include <linux/dvb/video.h>
46#include <linux/dvb/audio.h>
47#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030048#include <media/v4l2-ioctl.h>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030049#include <media/tuner.h>
50#include "cx18-mailbox.h"
51#include "cx18-av-core.h"
52#include "cx23418.h"
53
54/* DVB */
55#include "demux.h"
56#include "dmxdev.h"
57#include "dvb_demux.h"
58#include "dvb_frontend.h"
59#include "dvb_net.h"
60#include "dvbdev.h"
61
62#ifndef CONFIG_PCI
63# error "This driver requires kernel PCI support."
64#endif
65
Andy Walls446f2452008-09-06 16:43:59 -030066/* Default delay to throttle mmio access to the CX23418 */
67#define CX18_DEFAULT_MMIO_NDELAY 0 /* 0 ns = 0 PCI clock(s) / 33 MHz */
Andy Wallsc641d092008-09-01 00:40:41 -030068
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030069#define CX18_MEM_OFFSET 0x00000000
70#define CX18_MEM_SIZE 0x04000000
71#define CX18_REG_OFFSET 0x02000000
72
73/* Maximum cx18 driver instances. */
74#define CX18_MAX_CARDS 32
75
76/* Supported cards */
77#define CX18_CARD_HVR_1600_ESMT 0 /* Hauppauge HVR 1600 (ESMT memory) */
78#define CX18_CARD_HVR_1600_SAMSUNG 1 /* Hauppauge HVR 1600 (Samsung memory) */
79#define CX18_CARD_COMPRO_H900 2 /* Compro VideoMate H900 */
80#define CX18_CARD_YUAN_MPC718 3 /* Yuan MPC718 */
Sri Deevi03c28082008-06-21 11:06:44 -030081#define CX18_CARD_CNXT_RAPTOR_PAL 4 /* Conexant Raptor PAL */
82#define CX18_CARD_LAST 4
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030083
84#define CX18_ENC_STREAM_TYPE_MPG 0
85#define CX18_ENC_STREAM_TYPE_TS 1
86#define CX18_ENC_STREAM_TYPE_YUV 2
87#define CX18_ENC_STREAM_TYPE_VBI 3
88#define CX18_ENC_STREAM_TYPE_PCM 4
89#define CX18_ENC_STREAM_TYPE_IDX 5
90#define CX18_ENC_STREAM_TYPE_RAD 6
91#define CX18_MAX_STREAMS 7
92
93/* system vendor and device IDs */
94#define PCI_VENDOR_ID_CX 0x14f1
95#define PCI_DEVICE_ID_CX23418 0x5b7a
96
97/* subsystem vendor ID */
98#define CX18_PCI_ID_HAUPPAUGE 0x0070
99#define CX18_PCI_ID_COMPRO 0x185b
100#define CX18_PCI_ID_YUAN 0x12ab
Sri Deevi03c28082008-06-21 11:06:44 -0300101#define CX18_PCI_ID_CONEXANT 0x14f1
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300102
103/* ======================================================================== */
104/* ========================== START USER SETTABLE DMA VARIABLES =========== */
105/* ======================================================================== */
106
107/* DMA Buffers, Default size in MB allocated */
108#define CX18_DEFAULT_ENC_TS_BUFFERS 1
109#define CX18_DEFAULT_ENC_MPG_BUFFERS 2
110#define CX18_DEFAULT_ENC_IDX_BUFFERS 1
111#define CX18_DEFAULT_ENC_YUV_BUFFERS 2
112#define CX18_DEFAULT_ENC_VBI_BUFFERS 1
113#define CX18_DEFAULT_ENC_PCM_BUFFERS 1
114
115/* i2c stuff */
116#define I2C_CLIENTS_MAX 16
117
118/* debugging */
119
120/* Flag to turn on high volume debugging */
121#define CX18_DBGFLG_WARN (1 << 0)
122#define CX18_DBGFLG_INFO (1 << 1)
123#define CX18_DBGFLG_API (1 << 2)
124#define CX18_DBGFLG_DMA (1 << 3)
125#define CX18_DBGFLG_IOCTL (1 << 4)
126#define CX18_DBGFLG_FILE (1 << 5)
127#define CX18_DBGFLG_I2C (1 << 6)
128#define CX18_DBGFLG_IRQ (1 << 7)
129/* Flag to turn on high volume debugging */
130#define CX18_DBGFLG_HIGHVOL (1 << 8)
131
132/* NOTE: extra space before comma in 'cx->num , ## args' is required for
133 gcc-2.95, otherwise it won't compile. */
134#define CX18_DEBUG(x, type, fmt, args...) \
135 do { \
136 if ((x) & cx18_debug) \
137 printk(KERN_INFO "cx18-%d " type ": " fmt, cx->num , ## args); \
138 } while (0)
139#define CX18_DEBUG_WARN(fmt, args...) CX18_DEBUG(CX18_DBGFLG_WARN, "warning", fmt , ## args)
140#define CX18_DEBUG_INFO(fmt, args...) CX18_DEBUG(CX18_DBGFLG_INFO, "info", fmt , ## args)
141#define CX18_DEBUG_API(fmt, args...) CX18_DEBUG(CX18_DBGFLG_API, "api", fmt , ## args)
142#define CX18_DEBUG_DMA(fmt, args...) CX18_DEBUG(CX18_DBGFLG_DMA, "dma", fmt , ## args)
143#define CX18_DEBUG_IOCTL(fmt, args...) CX18_DEBUG(CX18_DBGFLG_IOCTL, "ioctl", fmt , ## args)
144#define CX18_DEBUG_FILE(fmt, args...) CX18_DEBUG(CX18_DBGFLG_FILE, "file", fmt , ## args)
145#define CX18_DEBUG_I2C(fmt, args...) CX18_DEBUG(CX18_DBGFLG_I2C, "i2c", fmt , ## args)
146#define CX18_DEBUG_IRQ(fmt, args...) CX18_DEBUG(CX18_DBGFLG_IRQ, "irq", fmt , ## args)
147
148#define CX18_DEBUG_HIGH_VOL(x, type, fmt, args...) \
149 do { \
150 if (((x) & cx18_debug) && (cx18_debug & CX18_DBGFLG_HIGHVOL)) \
151 printk(KERN_INFO "cx18%d " type ": " fmt, cx->num , ## args); \
152 } while (0)
153#define CX18_DEBUG_HI_WARN(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_WARN, "warning", fmt , ## args)
154#define CX18_DEBUG_HI_INFO(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_INFO, "info", fmt , ## args)
155#define CX18_DEBUG_HI_API(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_API, "api", fmt , ## args)
156#define CX18_DEBUG_HI_DMA(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_DMA, "dma", fmt , ## args)
157#define CX18_DEBUG_HI_IOCTL(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_IOCTL, "ioctl", fmt , ## args)
158#define CX18_DEBUG_HI_FILE(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_FILE, "file", fmt , ## args)
159#define CX18_DEBUG_HI_I2C(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_I2C, "i2c", fmt , ## args)
160#define CX18_DEBUG_HI_IRQ(fmt, args...) CX18_DEBUG_HIGH_VOL(CX18_DBGFLG_IRQ, "irq", fmt , ## args)
161
162/* Standard kernel messages */
163#define CX18_ERR(fmt, args...) printk(KERN_ERR "cx18-%d: " fmt, cx->num , ## args)
164#define CX18_WARN(fmt, args...) printk(KERN_WARNING "cx18-%d: " fmt, cx->num , ## args)
165#define CX18_INFO(fmt, args...) printk(KERN_INFO "cx18-%d: " fmt, cx->num , ## args)
166
167/* Values for CX18_API_DEC_PLAYBACK_SPEED mpeg_frame_type_mask parameter: */
168#define MPEG_FRAME_TYPE_IFRAME 1
169#define MPEG_FRAME_TYPE_IFRAME_PFRAME 3
170#define MPEG_FRAME_TYPE_ALL 7
171
172#define CX18_MAX_PGM_INDEX (400)
173
Andy Wallsd267d852008-09-28 21:46:02 -0300174extern int cx18_retry_mmio; /* enable check & retry of mmio accesses */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300175extern int cx18_debug;
176
177
178struct cx18_options {
179 int megabytes[CX18_MAX_STREAMS]; /* Size in megabytes of each stream */
180 int cardtype; /* force card type on load */
181 int tuner; /* set tuner on load */
182 int radio; /* enable/disable radio */
Andy Wallsc641d092008-09-01 00:40:41 -0300183 unsigned long mmio_ndelay; /* delay in ns after every PCI mmio access */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300184};
185
186/* per-buffer bit flags */
187#define CX18_F_B_NEED_BUF_SWAP 0 /* this buffer should be byte swapped */
188
189/* per-stream, s_flags */
190#define CX18_F_S_CLAIMED 3 /* this stream is claimed */
191#define CX18_F_S_STREAMING 4 /* the fw is decoding/encoding this stream */
192#define CX18_F_S_INTERNAL_USE 5 /* this stream is used internally (sliced VBI processing) */
193#define CX18_F_S_STREAMOFF 7 /* signal end of stream EOS */
194#define CX18_F_S_APPL_IO 8 /* this stream is used read/written by an application */
195
196/* per-cx18, i_flags */
197#define CX18_F_I_LOADED_FW 0 /* Loaded the firmware the first time */
198#define CX18_F_I_EOS 4 /* End of encoder stream reached */
199#define CX18_F_I_RADIO_USER 5 /* The radio tuner is selected */
200#define CX18_F_I_ENC_PAUSED 13 /* the encoder is paused */
201#define CX18_F_I_INITED 21 /* set after first open */
202#define CX18_F_I_FAILED 22 /* set if first open failed */
203
204/* These are the VBI types as they appear in the embedded VBI private packets. */
205#define CX18_SLICED_TYPE_TELETEXT_B (1)
206#define CX18_SLICED_TYPE_CAPTION_525 (4)
207#define CX18_SLICED_TYPE_WSS_625 (5)
208#define CX18_SLICED_TYPE_VPS (7)
209
210struct cx18_buffer {
211 struct list_head list;
212 dma_addr_t dma_handle;
213 u32 id;
214 unsigned long b_flags;
215 char *buf;
216
217 u32 bytesused;
218 u32 readpos;
219};
220
221struct cx18_queue {
222 struct list_head list;
Andy Wallsb04bce42008-08-22 21:03:11 -0300223 atomic_t buffers;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300224 u32 bytesused;
225};
226
227struct cx18_dvb {
228 struct dmx_frontend hw_frontend;
229 struct dmx_frontend mem_frontend;
230 struct dmxdev dmxdev;
231 struct dvb_adapter dvb_adapter;
232 struct dvb_demux demux;
233 struct dvb_frontend *fe;
234 struct dvb_net dvbnet;
235 int enabled;
236 int feeding;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300237 struct mutex feedlock;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300238};
239
240struct cx18; /* forward reference */
241struct cx18_scb; /* forward reference */
242
Andy Wallsd3c5e702008-08-23 16:42:29 -0300243#define CX18_INVALID_TASK_HANDLE 0xffffffff
244
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300245struct cx18_stream {
246 /* These first four fields are always set, even if the stream
247 is not actually created. */
248 struct video_device *v4l2dev; /* NULL when stream not created */
249 struct cx18 *cx; /* for ease of use */
250 const char *name; /* name of the stream */
251 int type; /* stream type */
252 u32 handle; /* task handle */
253 unsigned mdl_offset;
254
255 u32 id;
256 spinlock_t qlock; /* locks access to the queues */
257 unsigned long s_flags; /* status flags, see above */
258 int dma; /* can be PCI_DMA_TODEVICE,
259 PCI_DMA_FROMDEVICE or
260 PCI_DMA_NONE */
261 u64 dma_pts;
262 wait_queue_head_t waitq;
263
264 /* Buffer Stats */
265 u32 buffers;
266 u32 buf_size;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300267
268 /* Buffer Queues */
269 struct cx18_queue q_free; /* free buffers */
270 struct cx18_queue q_full; /* full buffers */
271 struct cx18_queue q_io; /* waiting for I/O */
272
273 /* DVB / Digital Transport */
274 struct cx18_dvb dvb;
275};
276
277struct cx18_open_id {
278 u32 open_id;
279 int type;
280 enum v4l2_priority prio;
281 struct cx18 *cx;
282};
283
284/* forward declaration of struct defined in cx18-cards.h */
285struct cx18_card;
286
287
288#define CX18_VBI_FRAMES 32
289
290/* VBI data */
291struct vbi_info {
292 u32 enc_size;
293 u32 frame;
294 u8 cc_data_odd[256];
295 u8 cc_data_even[256];
296 int cc_pos;
297 u8 cc_no_update;
298 u8 vps[5];
299 u8 vps_found;
300 int wss;
301 u8 wss_found;
302 u8 wss_no_update;
303 u32 raw_decoder_line_size;
304 u8 raw_decoder_sav_odd_field;
305 u8 raw_decoder_sav_even_field;
306 u32 sliced_decoder_line_size;
307 u8 sliced_decoder_sav_odd_field;
308 u8 sliced_decoder_sav_even_field;
309 struct v4l2_format in;
310 /* convenience pointer to sliced struct in vbi_in union */
311 struct v4l2_sliced_vbi_format *sliced_in;
312 u32 service_set_in;
313 int insert_mpeg;
314
315 /* Buffer for the maximum of 2 * 18 * packet_size sliced VBI lines.
316 One for /dev/vbi0 and one for /dev/vbi8 */
317 struct v4l2_sliced_vbi_data sliced_data[36];
318
319 /* Buffer for VBI data inserted into MPEG stream.
320 The first byte is a dummy byte that's never used.
321 The next 16 bytes contain the MPEG header for the VBI data,
322 the remainder is the actual VBI data.
323 The max size accepted by the MPEG VBI reinsertion turns out
324 to be 1552 bytes, which happens to be 4 + (1 + 42) * (2 * 18) bytes,
325 where 4 is a four byte header, 42 is the max sliced VBI payload, 1 is
326 a single line header byte and 2 * 18 is the number of VBI lines per frame.
327
328 However, it seems that the data must be 1K aligned, so we have to
329 pad the data until the 1 or 2 K boundary.
330
331 This pointer array will allocate 2049 bytes to store each VBI frame. */
332 u8 *sliced_mpeg_data[CX18_VBI_FRAMES];
333 u32 sliced_mpeg_size[CX18_VBI_FRAMES];
334 struct cx18_buffer sliced_mpeg_buf;
335 u32 inserted_frame;
336
337 u32 start[2], count;
338 u32 raw_size;
339 u32 sliced_size;
340};
341
342/* Per cx23418, per I2C bus private algo callback data */
343struct cx18_i2c_algo_callback_data {
344 struct cx18 *cx;
345 int bus_index; /* 0 or 1 for the cx23418's 1st or 2nd I2C bus */
346};
347
Andy Wallsd267d852008-09-28 21:46:02 -0300348#define CX18_MAX_MMIO_RETRIES 10
349
350struct cx18_mmio_stats {
351 atomic_t retried_write[CX18_MAX_MMIO_RETRIES+1];
352 atomic_t retried_read[CX18_MAX_MMIO_RETRIES+1];
353};
354
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300355/* Struct to hold info about cx18 cards */
356struct cx18 {
357 int num; /* board number, -1 during init! */
358 char name[8]; /* board name for printk and interrupts (e.g. 'cx180') */
359 struct pci_dev *dev; /* PCI device */
360 const struct cx18_card *card; /* card information */
361 const char *card_name; /* full name of the card */
362 const struct cx18_card_tuner_i2c *card_i2c; /* i2c addresses to probe for tuner */
363 u8 is_50hz;
364 u8 is_60hz;
365 u8 is_out_50hz;
366 u8 is_out_60hz;
367 u8 nof_inputs; /* number of video inputs */
368 u8 nof_audio_inputs; /* number of audio inputs */
369 u16 buffer_id; /* buffer ID counter */
370 u32 v4l2_cap; /* V4L2 capabilities of card */
371 u32 hw_flags; /* Hardware description of the board */
372 unsigned mdl_offset;
Al Viro990c81c2008-05-21 00:32:01 -0300373 struct cx18_scb __iomem *scb; /* pointer to SCB */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300374
375 struct cx18_av_state av_state;
376
377 /* codec settings */
378 struct cx2341x_mpeg_params params;
379 u32 filter_mode;
380 u32 temporal_strength;
381 u32 spatial_strength;
382
383 /* dualwatch */
384 unsigned long dualwatch_jiffies;
385 u16 dualwatch_stereo_mode;
386
387 /* Digitizer type */
388 int digitizer; /* 0x00EF = saa7114 0x00FO = saa7115 0x0106 = mic */
389
390 struct mutex serialize_lock; /* mutex used to serialize open/close/start/stop/ioctl operations */
391 struct cx18_options options; /* User options */
392 int stream_buf_size[CX18_MAX_STREAMS]; /* Stream buffer size */
393 struct cx18_stream streams[CX18_MAX_STREAMS]; /* Stream data */
394 unsigned long i_flags; /* global cx18 flags */
Hans Verkuil31554ae2008-05-25 11:21:27 -0300395 atomic_t ana_capturing; /* count number of active analog capture streams */
396 atomic_t tot_capturing; /* total count number of active capture streams */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300397 spinlock_t lock; /* lock access to this struct */
398 int search_pack_header;
399
400 spinlock_t dma_reg_lock; /* lock access to DMA engine registers */
401
402 int open_id; /* incremented each time an open occurs, used as
403 unique ID. Starts at 1, so 0 can be used as
404 uninitialized value in the stream->id. */
405
406 u32 base_addr;
407 struct v4l2_prio_state prio;
408
409 u8 card_rev;
410 void __iomem *enc_mem, *reg_mem;
411
412 struct vbi_info vbi;
413
414 u32 pgm_info_offset;
415 u32 pgm_info_num;
416 u32 pgm_info_write_idx;
417 u32 pgm_info_read_idx;
418 struct v4l2_enc_idx_entry pgm_info[CX18_MAX_PGM_INDEX];
419
420 u64 mpg_data_received;
421 u64 vbi_data_inserted;
422
423 wait_queue_head_t mb_apu_waitq;
424 wait_queue_head_t mb_cpu_waitq;
425 wait_queue_head_t mb_epu_waitq;
426 wait_queue_head_t mb_hpu_waitq;
427 wait_queue_head_t cap_w;
428 /* when the current DMA is finished this queue is woken up */
429 wait_queue_head_t dma_waitq;
430
431 /* i2c */
432 struct i2c_adapter i2c_adap[2];
433 struct i2c_algo_bit_data i2c_algo[2];
434 struct cx18_i2c_algo_callback_data i2c_algo_cb_data[2];
435 struct i2c_client i2c_client[2];
436 struct mutex i2c_bus_lock[2];
437 struct i2c_client *i2c_clients[I2C_CLIENTS_MAX];
438
Hans Verkuilba60bc62008-05-25 14:34:36 -0300439 /* gpio */
440 u32 gpio_dir;
441 u32 gpio_val;
Andy Walls8abdd002008-07-13 19:05:25 -0300442 struct mutex gpio_lock;
Hans Verkuilba60bc62008-05-25 14:34:36 -0300443
Andy Wallsd267d852008-09-28 21:46:02 -0300444 /* Statistics */
445 struct cx18_mmio_stats mmio_stats;
446
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300447 /* v4l2 and User settings */
448
449 /* codec settings */
450 u32 audio_input;
451 u32 active_input;
452 u32 active_output;
453 v4l2_std_id std;
454 v4l2_std_id tuner_std; /* The norm of the tuner (fixed) */
455};
456
457/* Globals */
458extern struct cx18 *cx18_cards[];
459extern int cx18_cards_active;
460extern int cx18_first_minor;
461extern spinlock_t cx18_cards_lock;
462
463/*==============Prototypes==================*/
464
465/* Return non-zero if a signal is pending */
466int cx18_msleep_timeout(unsigned int msecs, int intr);
467
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300468/* Read Hauppauge eeprom */
469struct tveeprom; /* forward reference */
470void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv);
471
472/* First-open initialization: load firmware, etc. */
473int cx18_init_on_first_open(struct cx18 *cx);
474
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300475#endif /* CX18_DRIVER_H */