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