Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[linux-2.6] / drivers / media / dvb / siano / smsdvb.c
1 /*
2  *  Driver for the Siano SMS1xxx USB dongle
3  *
4  *  Author: Uri Shkolni
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 struct smsdvb_client_t {
31         struct list_head entry;
32
33         struct smscore_device_t *coredev;
34         struct smscore_client_t *smsclient;
35
36         struct dvb_adapter      adapter;
37         struct dvb_demux        demux;
38         struct dmxdev           dmxdev;
39         struct dvb_frontend     frontend;
40
41         fe_status_t             fe_status;
42         int                     fe_ber, fe_snr, fe_unc, fe_signal_strength;
43
44         struct completion       tune_done, stat_done;
45
46         /* todo: save freq/band instead whole struct */
47         struct dvb_frontend_parameters fe_params;
48 };
49
50 static struct list_head g_smsdvb_clients;
51 static struct mutex g_smsdvb_clientslock;
52
53 static int sms_dbg;
54 module_param_named(debug, sms_dbg, int, 0644);
55 MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
56
57 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
58 {
59         struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
60         struct SmsMsgHdr_ST *phdr =
61                 (struct SmsMsgHdr_ST *)(((u8 *) cb->p) + cb->offset);
62
63         switch (phdr->msgType) {
64         case MSG_SMS_DVBT_BDA_DATA:
65                 dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
66                                  cb->size - sizeof(struct SmsMsgHdr_ST));
67                 break;
68
69         case MSG_SMS_RF_TUNE_RES:
70                 complete(&client->tune_done);
71                 break;
72
73         case MSG_SMS_GET_STATISTICS_RES:
74         {
75                 struct SmsMsgStatisticsInfo_ST *p =
76                         (struct SmsMsgStatisticsInfo_ST *)(phdr + 1);
77
78                 if (p->Stat.IsDemodLocked) {
79                         client->fe_status = FE_HAS_SIGNAL |
80                                             FE_HAS_CARRIER |
81                                             FE_HAS_VITERBI |
82                                             FE_HAS_SYNC |
83                                             FE_HAS_LOCK;
84
85                         client->fe_snr = p->Stat.SNR;
86                         client->fe_ber = p->Stat.BER;
87                         client->fe_unc = p->Stat.BERErrorCount;
88
89                         if (p->Stat.InBandPwr < -95)
90                                 client->fe_signal_strength = 0;
91                         else if (p->Stat.InBandPwr > -29)
92                                 client->fe_signal_strength = 100;
93                         else
94                                 client->fe_signal_strength =
95                                         (p->Stat.InBandPwr + 95) * 3 / 2;
96                 } else {
97                         client->fe_status = 0;
98                         client->fe_snr =
99                         client->fe_ber =
100                         client->fe_unc =
101                         client->fe_signal_strength = 0;
102                 }
103
104                 complete(&client->stat_done);
105                 break;
106         } }
107
108         smscore_putbuffer(client->coredev, cb);
109
110         return 0;
111 }
112
113 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
114 {
115         /* must be called under clientslock */
116
117         list_del(&client->entry);
118
119         smscore_unregister_client(client->smsclient);
120         dvb_unregister_frontend(&client->frontend);
121         dvb_dmxdev_release(&client->dmxdev);
122         dvb_dmx_release(&client->demux);
123         dvb_unregister_adapter(&client->adapter);
124         kfree(client);
125 }
126
127 static void smsdvb_onremove(void *context)
128 {
129         kmutex_lock(&g_smsdvb_clientslock);
130
131         smsdvb_unregister_client((struct smsdvb_client_t *) context);
132
133         kmutex_unlock(&g_smsdvb_clientslock);
134 }
135
136 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
137 {
138         struct smsdvb_client_t *client =
139                 container_of(feed->demux, struct smsdvb_client_t, demux);
140         struct SmsMsgData_ST PidMsg;
141
142         sms_debug("add pid %d(%x)",
143                   feed->pid, feed->pid);
144
145         PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
146         PidMsg.xMsgHeader.msgDstId = HIF_TASK;
147         PidMsg.xMsgHeader.msgFlags = 0;
148         PidMsg.xMsgHeader.msgType  = MSG_SMS_ADD_PID_FILTER_REQ;
149         PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
150         PidMsg.msgData[0] = feed->pid;
151
152         return smsclient_sendrequest(client->smsclient,
153                                      &PidMsg, sizeof(PidMsg));
154 }
155
156 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
157 {
158         struct smsdvb_client_t *client =
159                 container_of(feed->demux, struct smsdvb_client_t, demux);
160         struct SmsMsgData_ST PidMsg;
161
162         sms_debug("remove pid %d(%x)",
163                   feed->pid, feed->pid);
164
165         PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
166         PidMsg.xMsgHeader.msgDstId = HIF_TASK;
167         PidMsg.xMsgHeader.msgFlags = 0;
168         PidMsg.xMsgHeader.msgType  = MSG_SMS_REMOVE_PID_FILTER_REQ;
169         PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
170         PidMsg.msgData[0] = feed->pid;
171
172         return smsclient_sendrequest(client->smsclient,
173                                      &PidMsg, sizeof(PidMsg));
174 }
175
176 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
177                                         void *buffer, size_t size,
178                                         struct completion *completion)
179 {
180         int rc = smsclient_sendrequest(client->smsclient, buffer, size);
181         if (rc < 0)
182                 return rc;
183
184         return wait_for_completion_timeout(completion,
185                                            msecs_to_jiffies(2000)) ?
186                                                 0 : -ETIME;
187 }
188
189 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
190 {
191         struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
192                              DVBT_BDA_CONTROL_MSG_ID,
193                              HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 };
194         int ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
195                                               &client->stat_done);
196         if (ret < 0)
197                 return ret;
198
199         if (client->fe_status & FE_HAS_LOCK)
200                 sms_board_led_feedback(client->coredev,
201                                        (client->fe_unc == 0) ?
202                                        SMS_LED_HI : SMS_LED_LO);
203         else
204                 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
205         return ret;
206 }
207
208 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
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                 *stat = client->fe_status;
216
217         return rc;
218 }
219
220 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
221 {
222         struct smsdvb_client_t *client =
223                 container_of(fe, struct smsdvb_client_t, frontend);
224         int rc = smsdvb_send_statistics_request(client);
225
226         if (!rc)
227                 *ber = client->fe_ber;
228
229         return rc;
230 }
231
232 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
233 {
234         struct smsdvb_client_t *client =
235                 container_of(fe, struct smsdvb_client_t, frontend);
236         int rc = smsdvb_send_statistics_request(client);
237
238         if (!rc)
239                 *strength = client->fe_signal_strength;
240
241         return rc;
242 }
243
244 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
245 {
246         struct smsdvb_client_t *client =
247                 container_of(fe, struct smsdvb_client_t, frontend);
248         int rc = smsdvb_send_statistics_request(client);
249
250         if (!rc)
251                 *snr = client->fe_snr;
252
253         return rc;
254 }
255
256 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
257 {
258         struct smsdvb_client_t *client =
259                 container_of(fe, struct smsdvb_client_t, frontend);
260         int rc = smsdvb_send_statistics_request(client);
261
262         if (!rc)
263                 *ucblocks = client->fe_unc;
264
265         return rc;
266 }
267
268 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
269                                     struct dvb_frontend_tune_settings *tune)
270 {
271         sms_debug("");
272
273         tune->min_delay_ms = 400;
274         tune->step_size = 250000;
275         tune->max_drift = 0;
276         return 0;
277 }
278
279 static int smsdvb_set_frontend(struct dvb_frontend *fe,
280                                struct dvb_frontend_parameters *fep)
281 {
282         struct smsdvb_client_t *client =
283                 container_of(fe, struct smsdvb_client_t, frontend);
284
285         struct {
286                 struct SmsMsgHdr_ST     Msg;
287                 u32             Data[3];
288         } Msg;
289         int ret;
290
291         Msg.Msg.msgSrcId  = DVBT_BDA_CONTROL_MSG_ID;
292         Msg.Msg.msgDstId  = HIF_TASK;
293         Msg.Msg.msgFlags  = 0;
294         Msg.Msg.msgType   = MSG_SMS_RF_TUNE_REQ;
295         Msg.Msg.msgLength = sizeof(Msg);
296         Msg.Data[0] = fep->frequency;
297         Msg.Data[2] = 12000000;
298
299         sms_debug("freq %d band %d",
300                   fep->frequency, fep->u.ofdm.bandwidth);
301
302         switch (fep->u.ofdm.bandwidth) {
303         case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
304         case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
305         case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
306         case BANDWIDTH_AUTO: return -EOPNOTSUPP;
307         default: return -EINVAL;
308         }
309
310         /* Disable LNA, if any. An error is returned if no LNA is present */
311         ret = sms_board_lna_control(client->coredev, 0);
312         if (ret == 0) {
313                 fe_status_t status;
314
315                 /* tune with LNA off at first */
316                 ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
317                                                   &client->tune_done);
318
319                 smsdvb_read_status(fe, &status);
320
321                 if (status & FE_HAS_LOCK)
322                         return ret;
323
324                 /* previous tune didnt lock - enable LNA and tune again */
325                 sms_board_lna_control(client->coredev, 1);
326         }
327
328         return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
329                                            &client->tune_done);
330 }
331
332 static int smsdvb_get_frontend(struct dvb_frontend *fe,
333                                struct dvb_frontend_parameters *fep)
334 {
335         struct smsdvb_client_t *client =
336                 container_of(fe, struct smsdvb_client_t, frontend);
337
338         sms_debug("");
339
340         /* todo: */
341         memcpy(fep, &client->fe_params,
342                sizeof(struct dvb_frontend_parameters));
343
344         return 0;
345 }
346
347 static int smsdvb_init(struct dvb_frontend *fe)
348 {
349         struct smsdvb_client_t *client =
350                 container_of(fe, struct smsdvb_client_t, frontend);
351
352         sms_board_power(client->coredev, 1);
353
354         return 0;
355 }
356
357 static int smsdvb_sleep(struct dvb_frontend *fe)
358 {
359         struct smsdvb_client_t *client =
360                 container_of(fe, struct smsdvb_client_t, frontend);
361
362         sms_board_led_feedback(client->coredev, SMS_LED_OFF);
363         sms_board_power(client->coredev, 0);
364
365         return 0;
366 }
367
368 static void smsdvb_release(struct dvb_frontend *fe)
369 {
370         /* do nothing */
371 }
372
373 static struct dvb_frontend_ops smsdvb_fe_ops = {
374         .info = {
375                 .name                   = "Siano Mobile Digital MDTV Receiver",
376                 .type                   = FE_OFDM,
377                 .frequency_min          = 44250000,
378                 .frequency_max          = 867250000,
379                 .frequency_stepsize     = 250000,
380                 .caps = FE_CAN_INVERSION_AUTO |
381                         FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
382                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
383                         FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
384                         FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
385                         FE_CAN_GUARD_INTERVAL_AUTO |
386                         FE_CAN_RECOVER |
387                         FE_CAN_HIERARCHY_AUTO,
388         },
389
390         .release = smsdvb_release,
391
392         .set_frontend = smsdvb_set_frontend,
393         .get_frontend = smsdvb_get_frontend,
394         .get_tune_settings = smsdvb_get_tune_settings,
395
396         .read_status = smsdvb_read_status,
397         .read_ber = smsdvb_read_ber,
398         .read_signal_strength = smsdvb_read_signal_strength,
399         .read_snr = smsdvb_read_snr,
400         .read_ucblocks = smsdvb_read_ucblocks,
401
402         .init = smsdvb_init,
403         .sleep = smsdvb_sleep,
404 };
405
406 static int smsdvb_hotplug(struct smscore_device_t *coredev,
407                           struct device *device, int arrival)
408 {
409         struct smsclient_params_t params;
410         struct smsdvb_client_t *client;
411         int rc;
412
413         /* device removal handled by onremove callback */
414         if (!arrival)
415                 return 0;
416
417         if (smscore_get_device_mode(coredev) != DEVICE_MODE_DVBT_BDA) {
418                 sms_err("SMS Device mode is not set for "
419                         "DVB operation.");
420                 return 0;
421         }
422
423         client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
424         if (!client) {
425                 sms_err("kmalloc() failed");
426                 return -ENOMEM;
427         }
428
429         /* register dvb adapter */
430         rc = dvb_register_adapter(&client->adapter,
431                                   sms_get_board(
432                                         smscore_get_board_id(coredev))->name,
433                                   THIS_MODULE, device, adapter_nr);
434         if (rc < 0) {
435                 sms_err("dvb_register_adapter() failed %d", rc);
436                 goto adapter_error;
437         }
438
439         /* init dvb demux */
440         client->demux.dmx.capabilities = DMX_TS_FILTERING;
441         client->demux.filternum = 32; /* todo: nova ??? */
442         client->demux.feednum = 32;
443         client->demux.start_feed = smsdvb_start_feed;
444         client->demux.stop_feed = smsdvb_stop_feed;
445
446         rc = dvb_dmx_init(&client->demux);
447         if (rc < 0) {
448                 sms_err("dvb_dmx_init failed %d", rc);
449                 goto dvbdmx_error;
450         }
451
452         /* init dmxdev */
453         client->dmxdev.filternum = 32;
454         client->dmxdev.demux = &client->demux.dmx;
455         client->dmxdev.capabilities = 0;
456
457         rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
458         if (rc < 0) {
459                 sms_err("dvb_dmxdev_init failed %d", rc);
460                 goto dmxdev_error;
461         }
462
463         /* init and register frontend */
464         memcpy(&client->frontend.ops, &smsdvb_fe_ops,
465                sizeof(struct dvb_frontend_ops));
466
467         rc = dvb_register_frontend(&client->adapter, &client->frontend);
468         if (rc < 0) {
469                 sms_err("frontend registration failed %d", rc);
470                 goto frontend_error;
471         }
472
473         params.initial_id = 1;
474         params.data_type = MSG_SMS_DVBT_BDA_DATA;
475         params.onresponse_handler = smsdvb_onresponse;
476         params.onremove_handler = smsdvb_onremove;
477         params.context = client;
478
479         rc = smscore_register_client(coredev, &params, &client->smsclient);
480         if (rc < 0) {
481                 sms_err("smscore_register_client() failed %d", rc);
482                 goto client_error;
483         }
484
485         client->coredev = coredev;
486
487         init_completion(&client->tune_done);
488         init_completion(&client->stat_done);
489
490         kmutex_lock(&g_smsdvb_clientslock);
491
492         list_add(&client->entry, &g_smsdvb_clients);
493
494         kmutex_unlock(&g_smsdvb_clientslock);
495
496         sms_info("success");
497
498         sms_board_setup(coredev);
499
500         return 0;
501
502 client_error:
503         dvb_unregister_frontend(&client->frontend);
504
505 frontend_error:
506         dvb_dmxdev_release(&client->dmxdev);
507
508 dmxdev_error:
509         dvb_dmx_release(&client->demux);
510
511 dvbdmx_error:
512         dvb_unregister_adapter(&client->adapter);
513
514 adapter_error:
515         kfree(client);
516         return rc;
517 }
518
519 int smsdvb_module_init(void)
520 {
521         int rc;
522
523         INIT_LIST_HEAD(&g_smsdvb_clients);
524         kmutex_init(&g_smsdvb_clientslock);
525
526         rc = smscore_register_hotplug(smsdvb_hotplug);
527
528         sms_debug("");
529
530         return rc;
531 }
532
533 void smsdvb_module_exit(void)
534 {
535         smscore_unregister_hotplug(smsdvb_hotplug);
536
537         kmutex_lock(&g_smsdvb_clientslock);
538
539         while (!list_empty(&g_smsdvb_clients))
540                smsdvb_unregister_client(
541                         (struct smsdvb_client_t *) g_smsdvb_clients.next);
542
543         kmutex_unlock(&g_smsdvb_clientslock);
544 }
545
546 module_init(smsdvb_module_init);
547 module_exit(smsdvb_module_exit);
548
549 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
550 MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
551 MODULE_LICENSE("GPL");