Merge git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[pandora-kernel.git] / drivers / media / dvb / siano / smscoreapi.h
1 /*
2  *  Driver for the Siano SMS1xxx USB dongle
3  *
4  *  author: Anatoly Greenblat
5  *
6  *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
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 version 3 as
10  *  published by the Free Software Foundation;
11  *
12  *  Software distributed under the License is distributed on an "AS IS"
13  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
14  *
15  *  See the GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef __smscoreapi_h__
23 #define __smscoreapi_h__
24
25 #include <linux/version.h>
26 #include <linux/device.h>
27 #include <linux/list.h>
28 #include <linux/mm.h>
29 #include <linux/scatterlist.h>
30 #include <linux/types.h>
31 #include <asm/page.h>
32
33 #include "dmxdev.h"
34 #include "dvbdev.h"
35 #include "dvb_demux.h"
36 #include "dvb_frontend.h"
37
38 #include <linux/mutex.h>
39
40 #define kmutex_init(_p_) mutex_init(_p_)
41 #define kmutex_lock(_p_) mutex_lock(_p_)
42 #define kmutex_trylock(_p_) mutex_trylock(_p_)
43 #define kmutex_unlock(_p_) mutex_unlock(_p_)
44
45 #ifndef min
46 #define min(a, b) (((a) < (b)) ? (a) : (b))
47 #endif
48
49 #define SMS_ALLOC_ALIGNMENT                                     128
50 #define SMS_DMA_ALIGNMENT                                       16
51 #define SMS_ALIGN_ADDRESS(addr) \
52         ((((uintptr_t)(addr)) + (SMS_DMA_ALIGNMENT-1)) & ~(SMS_DMA_ALIGNMENT-1))
53
54 #define SMS_DEVICE_FAMILY2                                      1
55 #define SMS_ROM_NO_RESPONSE                                     2
56 #define SMS_DEVICE_NOT_READY                            0x8000000
57
58 enum sms_device_type_st {
59         SMS_STELLAR = 0,
60         SMS_NOVA_A0,
61         SMS_NOVA_B0,
62         SMS_VEGA,
63         SMS_NUM_OF_DEVICE_TYPES
64 };
65
66 struct smscore_device_t;
67 struct smscore_client_t;
68 struct smscore_buffer_t;
69
70 typedef int (*hotplug_t)(struct smscore_device_t *coredev,
71                          struct device *device, int arrival);
72
73 typedef int (*setmode_t)(void *context, int mode);
74 typedef void (*detectmode_t)(void *context, int *mode);
75 typedef int (*sendrequest_t)(void *context, void *buffer, size_t size);
76 typedef int (*loadfirmware_t)(void *context, void *buffer, size_t size);
77 typedef int (*preload_t)(void *context);
78 typedef int (*postload_t)(void *context);
79
80 typedef int (*onresponse_t)(void *context, struct smscore_buffer_t *cb);
81 typedef void (*onremove_t)(void *context);
82
83 struct smscore_buffer_t {
84         /* public members, once passed to clients can be changed freely */
85         struct list_head entry;
86         int                             size;
87         int                             offset;
88
89         /* private members, read-only for clients */
90         void                    *p;
91         dma_addr_t              phys;
92         unsigned long   offset_in_common;
93 };
94
95 struct smsdevice_params_t {
96         struct device   *device;
97
98         int                             buffer_size;
99         int                             num_buffers;
100
101         char                    devpath[32];
102         unsigned long   flags;
103
104         setmode_t               setmode_handler;
105         detectmode_t    detectmode_handler;
106         sendrequest_t   sendrequest_handler;
107         preload_t               preload_handler;
108         postload_t              postload_handler;
109
110         void                    *context;
111         enum sms_device_type_st device_type;
112 };
113
114 struct smsclient_params_t {
115         int                             initial_id;
116         int                             data_type;
117         onresponse_t    onresponse_handler;
118         onremove_t              onremove_handler;
119
120         void                    *context;
121 };
122
123 /* GPIO definitions for antenna frequency domain control (SMS8021) */
124 #define SMS_ANTENNA_GPIO_0                                      1
125 #define SMS_ANTENNA_GPIO_1                                      0
126
127 #define BW_8_MHZ                                                        0
128 #define BW_7_MHZ                                                        1
129 #define BW_6_MHZ                                                        2
130 #define BW_5_MHZ                                                        3
131 #define BW_ISDBT_1SEG                                           4
132 #define BW_ISDBT_3SEG                                           5
133
134 #define MSG_HDR_FLAG_SPLIT_MSG                          4
135
136 #define MAX_GPIO_PIN_NUMBER                                     31
137
138 #define HIF_TASK                                                        11
139 #define SMS_HOST_LIB                                            150
140 #define DVBT_BDA_CONTROL_MSG_ID                         201
141
142 #define SMS_MAX_PAYLOAD_SIZE                            240
143 #define SMS_TUNE_TIMEOUT                                        500
144
145 #define MSG_SMS_GPIO_CONFIG_REQ                         507
146 #define MSG_SMS_GPIO_CONFIG_RES                         508
147 #define MSG_SMS_GPIO_SET_LEVEL_REQ                      509
148 #define MSG_SMS_GPIO_SET_LEVEL_RES                      510
149 #define MSG_SMS_GPIO_GET_LEVEL_REQ                      511
150 #define MSG_SMS_GPIO_GET_LEVEL_RES                      512
151 #define MSG_SMS_RF_TUNE_REQ                                     561
152 #define MSG_SMS_RF_TUNE_RES                                     562
153 #define MSG_SMS_INIT_DEVICE_REQ                         578
154 #define MSG_SMS_INIT_DEVICE_RES                         579
155 #define MSG_SMS_ADD_PID_FILTER_REQ                      601
156 #define MSG_SMS_ADD_PID_FILTER_RES                      602
157 #define MSG_SMS_REMOVE_PID_FILTER_REQ           603
158 #define MSG_SMS_REMOVE_PID_FILTER_RES           604
159 #define MSG_SMS_DAB_CHANNEL                                     607
160 #define MSG_SMS_GET_PID_FILTER_LIST_REQ         608
161 #define MSG_SMS_GET_PID_FILTER_LIST_RES         609
162 #define MSG_SMS_GET_STATISTICS_REQ                      615
163 #define MSG_SMS_GET_STATISTICS_RES                      616
164 #define MSG_SMS_SET_ANTENNA_CONFIG_REQ          651
165 #define MSG_SMS_SET_ANTENNA_CONFIG_RES          652
166 #define MSG_SMS_GET_STATISTICS_EX_REQ           653
167 #define MSG_SMS_GET_STATISTICS_EX_RES           654
168 #define MSG_SMS_SLEEP_RESUME_COMP_IND           655
169 #define MSG_SMS_DATA_DOWNLOAD_REQ                       660
170 #define MSG_SMS_DATA_DOWNLOAD_RES                       661
171 #define MSG_SMS_SWDOWNLOAD_TRIGGER_REQ          664
172 #define MSG_SMS_SWDOWNLOAD_TRIGGER_RES          665
173 #define MSG_SMS_SWDOWNLOAD_BACKDOOR_REQ         666
174 #define MSG_SMS_SWDOWNLOAD_BACKDOOR_RES         667
175 #define MSG_SMS_GET_VERSION_EX_REQ                      668
176 #define MSG_SMS_GET_VERSION_EX_RES                      669
177 #define MSG_SMS_SET_CLOCK_OUTPUT_REQ            670
178 #define MSG_SMS_I2C_SET_FREQ_REQ                        685
179 #define MSG_SMS_GENERIC_I2C_REQ                         687
180 #define MSG_SMS_GENERIC_I2C_RES                         688
181 #define MSG_SMS_DVBT_BDA_DATA                           693
182 #define MSG_SW_RELOAD_REQ                                       697
183 #define MSG_SMS_DATA_MSG                                        699
184 #define MSG_SW_RELOAD_START_REQ                         702
185 #define MSG_SW_RELOAD_START_RES                         703
186 #define MSG_SW_RELOAD_EXEC_REQ                          704
187 #define MSG_SW_RELOAD_EXEC_RES                          705
188 #define MSG_SMS_SPI_INT_LINE_SET_REQ            710
189 #define MSG_SMS_ISDBT_TUNE_REQ                          776
190 #define MSG_SMS_ISDBT_TUNE_RES                          777
191
192 #define SMS_INIT_MSG_EX(ptr, type, src, dst, len) do { \
193         (ptr)->msgType = type; (ptr)->msgSrcId = src; (ptr)->msgDstId = dst; \
194         (ptr)->msgLength = len; (ptr)->msgFlags = 0; \
195 } while (0)
196 #define SMS_INIT_MSG(ptr, type, len) \
197         SMS_INIT_MSG_EX(ptr, type, 0, HIF_TASK, len)
198
199 enum SMS_DEVICE_MODE {
200         DEVICE_MODE_NONE = -1,
201         DEVICE_MODE_DVBT = 0,
202         DEVICE_MODE_DVBH,
203         DEVICE_MODE_DAB_TDMB,
204         DEVICE_MODE_DAB_TDMB_DABIP,
205         DEVICE_MODE_DVBT_BDA,
206         DEVICE_MODE_ISDBT,
207         DEVICE_MODE_ISDBT_BDA,
208         DEVICE_MODE_CMMB,
209         DEVICE_MODE_RAW_TUNER,
210         DEVICE_MODE_MAX,
211 };
212
213 struct SmsMsgHdr_ST {
214         u16     msgType;
215         u8      msgSrcId;
216         u8      msgDstId;
217         u16     msgLength; /* Length of entire message, including header */
218         u16     msgFlags;
219 };
220
221 struct SmsMsgData_ST {
222         struct SmsMsgHdr_ST     xMsgHeader;
223         u32                     msgData[1];
224 };
225
226 struct SmsDataDownload_ST {
227         struct SmsMsgHdr_ST     xMsgHeader;
228         u32                     MemAddr;
229         u8                      Payload[SMS_MAX_PAYLOAD_SIZE];
230 };
231
232 struct SmsVersionRes_ST {
233         struct SmsMsgHdr_ST     xMsgHeader;
234
235         u16             ChipModel; /* e.g. 0x1102 for SMS-1102 "Nova" */
236         u8              Step; /* 0 - Step A */
237         u8              MetalFix; /* 0 - Metal 0 */
238
239         u8              FirmwareId; /* 0xFF ï¿½ ROM, otherwise the
240                                      * value indicated by
241                                      * SMSHOSTLIB_DEVICE_MODES_E */
242         u8              SupportedProtocols; /* Bitwise OR combination of
243                                              * supported protocols */
244
245         u8              VersionMajor;
246         u8              VersionMinor;
247         u8              VersionPatch;
248         u8              VersionFieldPatch;
249
250         u8              RomVersionMajor;
251         u8              RomVersionMinor;
252         u8              RomVersionPatch;
253         u8              RomVersionFieldPatch;
254
255         u8              TextLabel[34];
256 };
257
258 struct SmsFirmware_ST {
259         u32                     CheckSum;
260         u32                     Length;
261         u32                     StartAddress;
262         u8                      Payload[1];
263 };
264
265 struct SMSHOSTLIB_STATISTICS_ST {
266         u32 Reserved; /* Reserved */
267
268         /* Common parameters */
269         u32 IsRfLocked; /* 0 - not locked, 1 - locked */
270         u32 IsDemodLocked; /* 0 - not locked, 1 - locked */
271         u32 IsExternalLNAOn; /* 0 - external LNA off, 1 - external LNA on */
272
273         /* Reception quality */
274         s32  SNR; /* dB */
275         u32 BER; /* Post Viterbi BER [1E-5] */
276         u32 FIB_CRC;    /* CRC errors percentage, valid only for DAB */
277         u32 TS_PER; /* Transport stream PER, 0xFFFFFFFF indicate N/A,
278                      * valid only for DVB-T/H */
279         u32 MFER; /* DVB-H frame error rate in percentage,
280                    * 0xFFFFFFFF indicate N/A, valid only for DVB-H */
281         s32  RSSI; /* dBm */
282         s32  InBandPwr; /* In band power in dBM */
283         s32  CarrierOffset; /* Carrier Offset in bin/1024 */
284
285         /* Transmission parameters, valid only for DVB-T/H */
286         u32 Frequency; /* Frequency in Hz */
287         u32 Bandwidth; /* Bandwidth in MHz */
288         u32 TransmissionMode; /* Transmission Mode, for DAB modes 1-4,
289                                * for DVB-T/H FFT mode carriers in Kilos */
290         u32 ModemState; /* from SMS_DvbModemState_ET */
291         u32 GuardInterval; /* Guard Interval, 1 divided by value */
292         u32 CodeRate; /* Code Rate from SMS_DvbModemState_ET */
293         u32 LPCodeRate; /* Low Priority Code Rate from SMS_DvbModemState_ET */
294         u32 Hierarchy; /* Hierarchy from SMS_Hierarchy_ET */
295         u32 Constellation; /* Constellation from SMS_Constellation_ET */
296
297         /* Burst parameters, valid only for DVB-H */
298         u32 BurstSize; /* Current burst size in bytes */
299         u32 BurstDuration; /* Current burst duration in mSec */
300         u32 BurstCycleTime; /* Current burst cycle time in mSec */
301         u32 CalculatedBurstCycleTime; /* Current burst cycle time in mSec,
302                                        * as calculated by demodulator */
303         u32 NumOfRows; /* Number of rows in MPE table */
304         u32 NumOfPaddCols; /* Number of padding columns in MPE table */
305         u32 NumOfPunctCols; /* Number of puncturing columns in MPE table */
306         /* Burst parameters */
307         u32 ErrorTSPackets; /* Number of erroneous transport-stream packets */
308         u32 TotalTSPackets; /* Total number of transport-stream packets */
309         u32 NumOfValidMpeTlbs; /* Number of MPE tables which do not include
310                                 * errors after MPE RS decoding */
311         u32 NumOfInvalidMpeTlbs; /* Number of MPE tables which include errors
312                                   * after MPE RS decoding */
313         u32 NumOfCorrectedMpeTlbs; /* Number of MPE tables which were corrected
314                                     * by MPE RS decoding */
315
316         /* Common params */
317         u32 BERErrorCount; /* Number of errornous SYNC bits. */
318         u32 BERBitCount; /* Total number of SYNC bits. */
319
320         /* Interface information */
321         u32 SmsToHostTxErrors; /* Total number of transmission errors. */
322
323         /* DAB/T-DMB */
324         u32 PreBER; /* DAB/T-DMB only: Pre Viterbi BER [1E-5] */
325
326         /* DVB-H TPS parameters */
327         u32 CellId; /* TPS Cell ID in bits 15..0, bits 31..16 zero;
328                      * if set to 0xFFFFFFFF cell_id not yet recovered */
329
330 };
331
332 struct SmsMsgStatisticsInfo_ST {
333         u32 RequestResult;
334
335         struct SMSHOSTLIB_STATISTICS_ST Stat;
336
337         /* Split the calc of the SNR in DAB */
338         u32 Signal; /* dB */
339         u32 Noise; /* dB */
340
341 };
342
343
344 struct smsdvb_client_t {
345         struct list_head entry;
346
347         struct smscore_device_t *coredev;
348         struct smscore_client_t *smsclient;
349
350         struct dvb_adapter      adapter;
351         struct dvb_demux        demux;
352         struct dmxdev           dmxdev;
353         struct dvb_frontend     frontend;
354
355         fe_status_t             fe_status;
356         int                     fe_ber, fe_snr, fe_signal_strength;
357
358         struct completion       tune_done, stat_done;
359
360         /* todo: save freq/band instead whole struct */
361         struct dvb_frontend_parameters fe_params;
362
363 };
364
365 extern void smscore_registry_setmode(char *devpath, int mode);
366 extern int smscore_registry_getmode(char *devpath);
367
368 extern int smscore_register_hotplug(hotplug_t hotplug);
369 extern void smscore_unregister_hotplug(hotplug_t hotplug);
370
371 extern int smscore_register_device(struct smsdevice_params_t *params,
372                                    struct smscore_device_t **coredev);
373 extern void smscore_unregister_device(struct smscore_device_t *coredev);
374
375 extern int smscore_start_device(struct smscore_device_t *coredev);
376 extern int smscore_load_firmware(struct smscore_device_t *coredev,
377                                  char *filename,
378                                  loadfirmware_t loadfirmware_handler);
379
380 extern int smscore_set_device_mode(struct smscore_device_t *coredev, int mode);
381 extern int smscore_get_device_mode(struct smscore_device_t *coredev);
382
383 extern int smscore_register_client(struct smscore_device_t *coredev,
384                                     struct smsclient_params_t *params,
385                                     struct smscore_client_t **client);
386 extern void smscore_unregister_client(struct smscore_client_t *client);
387
388 extern int smsclient_sendrequest(struct smscore_client_t *client,
389                                  void *buffer, size_t size);
390 extern void smscore_onresponse(struct smscore_device_t *coredev,
391                                struct smscore_buffer_t *cb);
392
393
394 extern
395 struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev);
396 extern void smscore_putbuffer(struct smscore_device_t *coredev,
397                               struct smscore_buffer_t *cb);
398
399 void smscore_set_board_id(struct smscore_device_t *core, int id);
400 int smscore_get_board_id(struct smscore_device_t *core);
401
402 /* smsdvb.c */
403 int smsdvb_register(void);
404 void smsdvb_unregister(void);
405
406 /* smsusb.c */
407 int smsusb_register(void);
408 void smsusb_unregister(void);
409
410 /* ------------------------------------------------------------------------ */
411
412 extern int sms_debug;
413
414 #define DBG_INFO 1
415 #define DBG_ADV  2
416
417 #define sms_printk(kern, fmt, arg...) \
418         printk(kern "%s: " fmt "\n", __func__, ##arg)
419
420 #define dprintk(kern, lvl, fmt, arg...) do {\
421         if (sms_debug & lvl) \
422                 sms_printk(kern, fmt, ##arg); } while (0)
423
424 #define sms_log(fmt, arg...) sms_printk(KERN_INFO, fmt, ##arg)
425 #define sms_err(fmt, arg...) \
426         sms_printk(KERN_ERR, "line: %d: " fmt, __LINE__, ##arg)
427 #define sms_warn(fmt, arg...)  sms_printk(KERN_WARNING, fmt, ##arg)
428 #define sms_info(fmt, arg...) \
429         dprintk(KERN_INFO, DBG_INFO, fmt, ##arg)
430 #define sms_debug(fmt, arg...) \
431         dprintk(KERN_DEBUG, DBG_ADV, fmt, ##arg)
432
433
434 #endif /* __smscoreapi_h__ */