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