Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / drivers / media / dvb / siano / smsdvb.c
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 2 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 #include <linux/module.h>
23 #include <linux/init.h>
24
25 #include "smscoreapi.h"
26 #include "sms-cards.h"
27
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
29
30 static struct list_head g_smsdvb_clients;
31 static struct mutex g_smsdvb_clientslock;
32
33 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
34 {
35         struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
36         struct SmsMsgHdr_ST *phdr =
37                 (struct SmsMsgHdr_ST *)(((u8 *) cb->p) + cb->offset);
38
39         switch (phdr->msgType) {
40         case MSG_SMS_DVBT_BDA_DATA:
41                 dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
42                                  cb->size - sizeof(struct SmsMsgHdr_ST));
43                 break;
44
45         case MSG_SMS_RF_TUNE_RES:
46                 complete(&client->tune_done);
47                 break;
48
49         case MSG_SMS_GET_STATISTICS_RES:
50         {
51                 struct SmsMsgStatisticsInfo_ST *p =
52                         (struct SmsMsgStatisticsInfo_ST *)(phdr + 1);
53
54                 if (p->Stat.IsDemodLocked) {
55                         client->fe_status = FE_HAS_SIGNAL |
56                                             FE_HAS_CARRIER |
57                                             FE_HAS_VITERBI |
58                                             FE_HAS_SYNC |
59                                             FE_HAS_LOCK;
60
61                         client->fe_snr = p->Stat.SNR;
62                         client->fe_ber = p->Stat.BER;
63
64                         if (p->Stat.InBandPwr < -95)
65                                 client->fe_signal_strength = 0;
66                         else if (p->Stat.InBandPwr > -29)
67                                 client->fe_signal_strength = 100;
68                         else
69                                 client->fe_signal_strength =
70                                         (p->Stat.InBandPwr + 95) * 3 / 2;
71                 } else {
72                         client->fe_status = 0;
73                         client->fe_snr =
74                         client->fe_ber =
75                         client->fe_signal_strength = 0;
76                 }
77
78                 complete(&client->stat_done);
79                 break;
80         } }
81
82         smscore_putbuffer(client->coredev, cb);
83
84         return 0;
85 }
86
87 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
88 {
89         /* must be called under clientslock */
90
91         list_del(&client->entry);
92
93         smscore_unregister_client(client->smsclient);
94         dvb_unregister_frontend(&client->frontend);
95         dvb_dmxdev_release(&client->dmxdev);
96         dvb_dmx_release(&client->demux);
97         dvb_unregister_adapter(&client->adapter);
98         kfree(client);
99 }
100
101 static void smsdvb_onremove(void *context)
102 {
103         kmutex_lock(&g_smsdvb_clientslock);
104
105         smsdvb_unregister_client((struct smsdvb_client_t *) context);
106
107         kmutex_unlock(&g_smsdvb_clientslock);
108 }
109
110 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
111 {
112         struct smsdvb_client_t *client =
113                 container_of(feed->demux, struct smsdvb_client_t, demux);
114         struct SmsMsgData_ST PidMsg;
115
116         sms_debug("add pid %d(%x)",
117                   feed->pid, feed->pid);
118
119         PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
120         PidMsg.xMsgHeader.msgDstId = HIF_TASK;
121         PidMsg.xMsgHeader.msgFlags = 0;
122         PidMsg.xMsgHeader.msgType  = MSG_SMS_ADD_PID_FILTER_REQ;
123         PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
124         PidMsg.msgData[0] = feed->pid;
125
126         return smsclient_sendrequest(client->smsclient,
127                                      &PidMsg, sizeof(PidMsg));
128 }
129
130 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
131 {
132         struct smsdvb_client_t *client =
133                 container_of(feed->demux, struct smsdvb_client_t, demux);
134         struct SmsMsgData_ST PidMsg;
135
136         sms_debug("remove pid %d(%x)",
137                   feed->pid, feed->pid);
138
139         PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
140         PidMsg.xMsgHeader.msgDstId = HIF_TASK;
141         PidMsg.xMsgHeader.msgFlags = 0;
142         PidMsg.xMsgHeader.msgType  = MSG_SMS_REMOVE_PID_FILTER_REQ;
143         PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
144         PidMsg.msgData[0] = feed->pid;
145
146         return smsclient_sendrequest(client->smsclient,
147                                      &PidMsg, sizeof(PidMsg));
148 }
149
150 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
151                                         void *buffer, size_t size,
152                                         struct completion *completion)
153 {
154         int rc = smsclient_sendrequest(client->smsclient, buffer, size);
155         if (rc < 0)
156                 return rc;
157
158         return wait_for_completion_timeout(completion,
159                                            msecs_to_jiffies(2000)) ?
160                                                 0 : -ETIME;
161 }
162
163 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
164 {
165         struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
166                              DVBT_BDA_CONTROL_MSG_ID,
167                              HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 };
168         return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
169                                            &client->stat_done);
170 }
171
172 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
173 {
174         struct smsdvb_client_t *client =
175                 container_of(fe, struct smsdvb_client_t, frontend);
176         int rc = smsdvb_send_statistics_request(client);
177
178         if (!rc)
179                 *stat = client->fe_status;
180
181         return rc;
182 }
183
184 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
185 {
186         struct smsdvb_client_t *client =
187                 container_of(fe, struct smsdvb_client_t, frontend);
188         int rc = smsdvb_send_statistics_request(client);
189
190         if (!rc)
191                 *ber = client->fe_ber;
192
193         return rc;
194 }
195
196 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
197 {
198         struct smsdvb_client_t *client =
199                 container_of(fe, struct smsdvb_client_t, frontend);
200         int rc = smsdvb_send_statistics_request(client);
201
202         if (!rc)
203                 *strength = client->fe_signal_strength;
204
205         return rc;
206 }
207
208 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
209 {
210         struct smsdvb_client_t *client =
211                 container_of(fe, struct smsdvb_client_t, frontend);
212         int rc = smsdvb_send_statistics_request(client);
213
214         if (!rc)
215                 *snr = client->fe_snr;
216
217         return rc;
218 }
219
220 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
221                                     struct dvb_frontend_tune_settings *tune)
222 {
223         sms_debug("");
224
225         tune->min_delay_ms = 400;
226         tune->step_size = 250000;
227         tune->max_drift = 0;
228         return 0;
229 }
230
231 static int smsdvb_set_frontend(struct dvb_frontend *fe,
232                                struct dvb_frontend_parameters *fep)
233 {
234         struct smsdvb_client_t *client =
235                 container_of(fe, struct smsdvb_client_t, frontend);
236
237         struct {
238                 struct SmsMsgHdr_ST     Msg;
239                 u32             Data[3];
240         } Msg;
241
242         Msg.Msg.msgSrcId  = DVBT_BDA_CONTROL_MSG_ID;
243         Msg.Msg.msgDstId  = HIF_TASK;
244         Msg.Msg.msgFlags  = 0;
245         Msg.Msg.msgType   = MSG_SMS_RF_TUNE_REQ;
246         Msg.Msg.msgLength = sizeof(Msg);
247         Msg.Data[0] = fep->frequency;
248         Msg.Data[2] = 12000000;
249
250         sms_debug("freq %d band %d",
251                   fep->frequency, fep->u.ofdm.bandwidth);
252
253         switch (fep->u.ofdm.bandwidth) {
254         case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
255         case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
256         case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
257         case BANDWIDTH_AUTO: return -EOPNOTSUPP;
258         default: return -EINVAL;
259         }
260
261         return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
262                                            &client->tune_done);
263 }
264
265 static int smsdvb_get_frontend(struct dvb_frontend *fe,
266                                struct dvb_frontend_parameters *fep)
267 {
268         struct smsdvb_client_t *client =
269                 container_of(fe, struct smsdvb_client_t, frontend);
270
271         sms_debug("");
272
273         /* todo: */
274         memcpy(fep, &client->fe_params,
275                sizeof(struct dvb_frontend_parameters));
276         return 0;
277 }
278
279 static void smsdvb_release(struct dvb_frontend *fe)
280 {
281         /* do nothing */
282 }
283
284 static struct dvb_frontend_ops smsdvb_fe_ops = {
285         .info = {
286                 .name                   = "Siano Mobile Digital SMS1xxx",
287                 .type                   = FE_OFDM,
288                 .frequency_min          = 44250000,
289                 .frequency_max          = 867250000,
290                 .frequency_stepsize     = 250000,
291                 .caps = FE_CAN_INVERSION_AUTO |
292                         FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
293                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
294                         FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
295                         FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
296                         FE_CAN_GUARD_INTERVAL_AUTO |
297                         FE_CAN_RECOVER |
298                         FE_CAN_HIERARCHY_AUTO,
299         },
300
301         .release = smsdvb_release,
302
303         .set_frontend = smsdvb_set_frontend,
304         .get_frontend = smsdvb_get_frontend,
305         .get_tune_settings = smsdvb_get_tune_settings,
306
307         .read_status = smsdvb_read_status,
308         .read_ber = smsdvb_read_ber,
309         .read_signal_strength = smsdvb_read_signal_strength,
310         .read_snr = smsdvb_read_snr,
311 };
312
313 static int smsdvb_hotplug(struct smscore_device_t *coredev,
314                           struct device *device, int arrival)
315 {
316         struct smsclient_params_t params;
317         struct smsdvb_client_t *client;
318         int rc;
319
320         /* device removal handled by onremove callback */
321         if (!arrival)
322                 return 0;
323
324         if (smscore_get_device_mode(coredev) != 4) {
325                 sms_err("SMS Device mode is not set for "
326                         "DVB operation.");
327                 return 0;
328         }
329
330         client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
331         if (!client) {
332                 sms_err("kmalloc() failed");
333                 return -ENOMEM;
334         }
335
336         /* register dvb adapter */
337         rc = dvb_register_adapter(&client->adapter,
338                                   sms_get_board(
339                                         smscore_get_board_id(coredev))->name,
340                                   THIS_MODULE, device, adapter_nr);
341         if (rc < 0) {
342                 sms_err("dvb_register_adapter() failed %d", rc);
343                 goto adapter_error;
344         }
345
346         /* init dvb demux */
347         client->demux.dmx.capabilities = DMX_TS_FILTERING;
348         client->demux.filternum = 32; /* todo: nova ??? */
349         client->demux.feednum = 32;
350         client->demux.start_feed = smsdvb_start_feed;
351         client->demux.stop_feed = smsdvb_stop_feed;
352
353         rc = dvb_dmx_init(&client->demux);
354         if (rc < 0) {
355                 sms_err("dvb_dmx_init failed %d", rc);
356                 goto dvbdmx_error;
357         }
358
359         /* init dmxdev */
360         client->dmxdev.filternum = 32;
361         client->dmxdev.demux = &client->demux.dmx;
362         client->dmxdev.capabilities = 0;
363
364         rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
365         if (rc < 0) {
366                 sms_err("dvb_dmxdev_init failed %d", rc);
367                 goto dmxdev_error;
368         }
369
370         /* init and register frontend */
371         memcpy(&client->frontend.ops, &smsdvb_fe_ops,
372                sizeof(struct dvb_frontend_ops));
373
374         rc = dvb_register_frontend(&client->adapter, &client->frontend);
375         if (rc < 0) {
376                 sms_err("frontend registration failed %d", rc);
377                 goto frontend_error;
378         }
379
380         params.initial_id = 1;
381         params.data_type = MSG_SMS_DVBT_BDA_DATA;
382         params.onresponse_handler = smsdvb_onresponse;
383         params.onremove_handler = smsdvb_onremove;
384         params.context = client;
385
386         rc = smscore_register_client(coredev, &params, &client->smsclient);
387         if (rc < 0) {
388                 sms_err("smscore_register_client() failed %d", rc);
389                 goto client_error;
390         }
391
392         client->coredev = coredev;
393
394         init_completion(&client->tune_done);
395         init_completion(&client->stat_done);
396
397         kmutex_lock(&g_smsdvb_clientslock);
398
399         list_add(&client->entry, &g_smsdvb_clients);
400
401         kmutex_unlock(&g_smsdvb_clientslock);
402
403         sms_info("success");
404
405         return 0;
406
407 client_error:
408         dvb_unregister_frontend(&client->frontend);
409
410 frontend_error:
411         dvb_dmxdev_release(&client->dmxdev);
412
413 dmxdev_error:
414         dvb_dmx_release(&client->demux);
415
416 dvbdmx_error:
417         dvb_unregister_adapter(&client->adapter);
418
419 adapter_error:
420         kfree(client);
421         return rc;
422 }
423
424 int smsdvb_register(void)
425 {
426         int rc;
427
428         INIT_LIST_HEAD(&g_smsdvb_clients);
429         kmutex_init(&g_smsdvb_clientslock);
430
431         rc = smscore_register_hotplug(smsdvb_hotplug);
432
433         sms_debug("");
434
435         return rc;
436 }
437
438 void smsdvb_unregister(void)
439 {
440         smscore_unregister_hotplug(smsdvb_hotplug);
441
442         kmutex_lock(&g_smsdvb_clientslock);
443
444         while (!list_empty(&g_smsdvb_clients))
445                smsdvb_unregister_client(
446                         (struct smsdvb_client_t *) g_smsdvb_clients.next);
447
448         kmutex_unlock(&g_smsdvb_clientslock);
449 }