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" |
1c11d546 | 26 | #include "sms-cards.h" |
2e5c1ec8 | 27 | |
9c59f968 MK |
28 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
29 | ||
2e5c1ec8 MK |
30 | struct list_head g_smsdvb_clients; |
31 | kmutex_t g_smsdvb_clientslock; | |
32 | ||
18245e18 | 33 | int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb) |
2e5c1ec8 | 34 | { |
18245e18 MK |
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); | |
2e5c1ec8 | 38 | |
fa830e8a | 39 | switch (phdr->msgType) { |
82237416 MK |
40 | case MSG_SMS_DVBT_BDA_DATA: |
41 | dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1), | |
18245e18 | 42 | cb->size - sizeof(struct SmsMsgHdr_ST)); |
82237416 MK |
43 | break; |
44 | ||
45 | case MSG_SMS_RF_TUNE_RES: | |
46 | complete(&client->tune_done); | |
47 | break; | |
2e5c1ec8 | 48 | |
82237416 MK |
49 | case MSG_SMS_GET_STATISTICS_RES: |
50 | { | |
18245e18 MK |
51 | struct SmsMsgStatisticsInfo_ST *p = |
52 | (struct SmsMsgStatisticsInfo_ST *)(phdr + 1); | |
2e5c1ec8 | 53 | |
fa830e8a | 54 | if (p->Stat.IsDemodLocked) { |
82237416 MK |
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; | |
2e5c1ec8 | 63 | |
82237416 MK |
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; | |
2e5c1ec8 | 76 | } |
82237416 MK |
77 | |
78 | complete(&client->stat_done); | |
79 | break; | |
80 | } } | |
2e5c1ec8 MK |
81 | |
82 | smscore_putbuffer(client->coredev, cb); | |
83 | ||
84 | return 0; | |
85 | } | |
86 | ||
18245e18 | 87 | void smsdvb_unregister_client(struct smsdvb_client_t *client) |
2e5c1ec8 | 88 | { |
fa830e8a | 89 | /* must be called under clientslock */ |
2e5c1ec8 MK |
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 | void smsdvb_onremove(void *context) | |
102 | { | |
103 | kmutex_lock(&g_smsdvb_clientslock); | |
104 | ||
18245e18 | 105 | smsdvb_unregister_client((struct smsdvb_client_t *) context); |
2e5c1ec8 MK |
106 | |
107 | kmutex_unlock(&g_smsdvb_clientslock); | |
108 | } | |
109 | ||
110 | static int smsdvb_start_feed(struct dvb_demux_feed *feed) | |
111 | { | |
18245e18 MK |
112 | struct smsdvb_client_t *client = |
113 | container_of(feed->demux, struct smsdvb_client_t, demux); | |
114 | struct SmsMsgData_ST PidMsg; | |
2e5c1ec8 | 115 | |
a0c0abcb | 116 | sms_debug("add pid %d(%x)", |
068d6c0f | 117 | feed->pid, feed->pid); |
2e5c1ec8 MK |
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 | ||
82237416 MK |
126 | return smsclient_sendrequest(client->smsclient, |
127 | &PidMsg, sizeof(PidMsg)); | |
2e5c1ec8 MK |
128 | } |
129 | ||
130 | static int smsdvb_stop_feed(struct dvb_demux_feed *feed) | |
131 | { | |
18245e18 MK |
132 | struct smsdvb_client_t *client = |
133 | container_of(feed->demux, struct smsdvb_client_t, demux); | |
134 | struct SmsMsgData_ST PidMsg; | |
2e5c1ec8 | 135 | |
a0c0abcb | 136 | sms_debug("remove pid %d(%x)", |
068d6c0f | 137 | feed->pid, feed->pid); |
2e5c1ec8 MK |
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 | ||
82237416 MK |
146 | return smsclient_sendrequest(client->smsclient, |
147 | &PidMsg, sizeof(PidMsg)); | |
2e5c1ec8 MK |
148 | } |
149 | ||
18245e18 | 150 | static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client, |
a83ccdd6 MK |
151 | void *buffer, size_t size, |
152 | struct completion *completion) | |
2e5c1ec8 MK |
153 | { |
154 | int rc = smsclient_sendrequest(client->smsclient, buffer, size); | |
155 | if (rc < 0) | |
156 | return rc; | |
157 | ||
82237416 MK |
158 | return wait_for_completion_timeout(completion, |
159 | msecs_to_jiffies(2000)) ? | |
160 | 0 : -ETIME; | |
2e5c1ec8 MK |
161 | } |
162 | ||
18245e18 | 163 | static int smsdvb_send_statistics_request(struct smsdvb_client_t *client) |
2e5c1ec8 | 164 | { |
18245e18 | 165 | struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ, |
82237416 | 166 | DVBT_BDA_CONTROL_MSG_ID, |
18245e18 | 167 | HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 }; |
82237416 MK |
168 | return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg), |
169 | &client->stat_done); | |
2e5c1ec8 MK |
170 | } |
171 | ||
172 | static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat) | |
173 | { | |
18245e18 MK |
174 | struct smsdvb_client_t *client = |
175 | container_of(fe, struct smsdvb_client_t, frontend); | |
2e5c1ec8 MK |
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 | { | |
18245e18 MK |
186 | struct smsdvb_client_t *client = |
187 | container_of(fe, struct smsdvb_client_t, frontend); | |
2e5c1ec8 MK |
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 | { | |
18245e18 MK |
198 | struct smsdvb_client_t *client = |
199 | container_of(fe, struct smsdvb_client_t, frontend); | |
2e5c1ec8 MK |
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 | { | |
18245e18 MK |
210 | struct smsdvb_client_t *client = |
211 | container_of(fe, struct smsdvb_client_t, frontend); | |
2e5c1ec8 MK |
212 | int rc = smsdvb_send_statistics_request(client); |
213 | ||
214 | if (!rc) | |
215 | *snr = client->fe_snr; | |
216 | ||
217 | return rc; | |
218 | } | |
219 | ||
82237416 MK |
220 | static int smsdvb_get_tune_settings(struct dvb_frontend *fe, |
221 | struct dvb_frontend_tune_settings *tune) | |
2e5c1ec8 | 222 | { |
a0c0abcb | 223 | sms_debug(""); |
2e5c1ec8 MK |
224 | |
225 | tune->min_delay_ms = 400; | |
226 | tune->step_size = 250000; | |
227 | tune->max_drift = 0; | |
228 | return 0; | |
229 | } | |
230 | ||
82237416 MK |
231 | static int smsdvb_set_frontend(struct dvb_frontend *fe, |
232 | struct dvb_frontend_parameters *fep) | |
2e5c1ec8 | 233 | { |
18245e18 MK |
234 | struct smsdvb_client_t *client = |
235 | container_of(fe, struct smsdvb_client_t, frontend); | |
2e5c1ec8 | 236 | |
18245e18 MK |
237 | struct { |
238 | struct SmsMsgHdr_ST Msg; | |
82237416 | 239 | u32 Data[3]; |
2e5c1ec8 MK |
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 | ||
a0c0abcb | 250 | sms_debug("freq %d band %d", |
068d6c0f | 251 | fep->frequency, fep->u.ofdm.bandwidth); |
2e5c1ec8 | 252 | |
fa830e8a MK |
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; | |
2e5c1ec8 MK |
259 | } |
260 | ||
82237416 MK |
261 | return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg), |
262 | &client->tune_done); | |
2e5c1ec8 MK |
263 | } |
264 | ||
82237416 MK |
265 | static int smsdvb_get_frontend(struct dvb_frontend *fe, |
266 | struct dvb_frontend_parameters *fep) | |
2e5c1ec8 | 267 | { |
18245e18 MK |
268 | struct smsdvb_client_t *client = |
269 | container_of(fe, struct smsdvb_client_t, frontend); | |
2e5c1ec8 | 270 | |
a0c0abcb | 271 | sms_debug(""); |
2e5c1ec8 | 272 | |
fa830e8a | 273 | /* todo: */ |
82237416 MK |
274 | memcpy(fep, &client->fe_params, |
275 | sizeof(struct dvb_frontend_parameters)); | |
2e5c1ec8 MK |
276 | return 0; |
277 | } | |
278 | ||
279 | static void smsdvb_release(struct dvb_frontend *fe) | |
280 | { | |
fa830e8a | 281 | /* do nothing */ |
2e5c1ec8 MK |
282 | } |
283 | ||
284 | static struct dvb_frontend_ops smsdvb_fe_ops = { | |
285 | .info = { | |
1c11d546 | 286 | .name = "Siano Mobile Digital SMS1xxx", |
82237416 | 287 | .type = FE_OFDM, |
2e5c1ec8 MK |
288 | .frequency_min = 44250000, |
289 | .frequency_max = 867250000, | |
290 | .frequency_stepsize = 250000, | |
291 | .caps = FE_CAN_INVERSION_AUTO | | |
82237416 MK |
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, | |
2e5c1ec8 MK |
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 | ||
18245e18 | 313 | int smsdvb_hotplug(struct smscore_device_t *coredev, |
82237416 | 314 | struct device *device, int arrival) |
2e5c1ec8 | 315 | { |
18245e18 MK |
316 | struct smsclient_params_t params; |
317 | struct smsdvb_client_t *client; | |
2e5c1ec8 MK |
318 | int rc; |
319 | ||
fa830e8a | 320 | /* device removal handled by onremove callback */ |
2e5c1ec8 MK |
321 | if (!arrival) |
322 | return 0; | |
323 | ||
82237416 | 324 | if (smscore_get_device_mode(coredev) != 4) { |
a0c0abcb MK |
325 | sms_err("SMS Device mode is not set for " |
326 | "DVB operation."); | |
f17407a8 | 327 | return 0; |
2e5c1ec8 MK |
328 | } |
329 | ||
18245e18 | 330 | client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL); |
82237416 | 331 | if (!client) { |
a0c0abcb | 332 | sms_info("kmalloc() failed"); |
2e5c1ec8 MK |
333 | return -ENOMEM; |
334 | } | |
335 | ||
fa830e8a | 336 | /* register dvb adapter */ |
1c11d546 MK |
337 | rc = dvb_register_adapter(&client->adapter, |
338 | sms_get_board( | |
339 | smscore_get_board_id(coredev))->name, | |
82237416 MK |
340 | THIS_MODULE, device, adapter_nr); |
341 | if (rc < 0) { | |
a0c0abcb | 342 | sms_err("dvb_register_adapter() failed %d", rc); |
2e5c1ec8 MK |
343 | goto adapter_error; |
344 | } | |
345 | ||
fa830e8a | 346 | /* init dvb demux */ |
2e5c1ec8 | 347 | client->demux.dmx.capabilities = DMX_TS_FILTERING; |
fa830e8a | 348 | client->demux.filternum = 32; /* todo: nova ??? */ |
2e5c1ec8 MK |
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); | |
82237416 | 354 | if (rc < 0) { |
a0c0abcb | 355 | sms_err("dvb_dmx_init failed %d", rc); |
2e5c1ec8 MK |
356 | goto dvbdmx_error; |
357 | } | |
358 | ||
fa830e8a | 359 | /* init dmxdev */ |
2e5c1ec8 MK |
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); | |
82237416 | 365 | if (rc < 0) { |
a0c0abcb | 366 | sms_err("dvb_dmxdev_init failed %d", 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) { |
a0c0abcb | 376 | sms_err("frontend registration failed %d", rc); |
2e5c1ec8 MK |
377 | goto frontend_error; |
378 | } | |
379 | ||
f17407a8 | 380 | params.initial_id = 1; |
2e5c1ec8 MK |
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, ¶ms, &client->smsclient); | |
82237416 | 387 | if (rc < 0) { |
a0c0abcb | 388 | sms_info("smscore_register_client() failed %d", rc); |
2e5c1ec8 MK |
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 | ||
a0c0abcb | 403 | sms_info("success"); |
2e5c1ec8 MK |
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 | ||
eae55660 ST |
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 | ||
a0c0abcb | 433 | sms_debug(""); |
eae55660 ST |
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)) | |
82237416 | 445 | smsdvb_unregister_client( |
18245e18 | 446 | (struct smsdvb_client_t *) g_smsdvb_clients.next); |
eae55660 ST |
447 | |
448 | kmutex_unlock(&g_smsdvb_clientslock); | |
eae55660 | 449 | } |