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