1 /*
2  * Abilis Systems Single DVB-T Receiver
3  * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
4  * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include <linux/kernel.h>
22 #include "as102_drv.h"
23 #include "as10x_types.h"
24 #include "as10x_cmd.h"
25 
26 /**
27  * as10x_cmd_turn_on - send turn on command to AS10x
28  * @adap:   pointer to AS10x bus adapter
29  *
30  * Return 0 when no error, < 0 in case of error.
31  */
as10x_cmd_turn_on(struct as10x_bus_adapter_t * adap)32 int as10x_cmd_turn_on(struct as10x_bus_adapter_t *adap)
33 {
34 	int error;
35 	struct as10x_cmd_t *pcmd, *prsp;
36 
37 	ENTER();
38 
39 	pcmd = adap->cmd;
40 	prsp = adap->rsp;
41 
42 	/* prepare command */
43 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
44 			sizeof(pcmd->body.turn_on.req));
45 
46 	/* fill command */
47 	pcmd->body.turn_on.req.proc_id = cpu_to_le16(CONTROL_PROC_TURNON);
48 
49 	/* send command */
50 	if (adap->ops->xfer_cmd) {
51 		error = adap->ops->xfer_cmd(adap, (uint8_t *) pcmd,
52 					    sizeof(pcmd->body.turn_on.req) +
53 					    HEADER_SIZE,
54 					    (uint8_t *) prsp,
55 					    sizeof(prsp->body.turn_on.rsp) +
56 					    HEADER_SIZE);
57 	} else {
58 		error = AS10X_CMD_ERROR;
59 	}
60 
61 	if (error < 0)
62 		goto out;
63 
64 	/* parse response */
65 	error = as10x_rsp_parse(prsp, CONTROL_PROC_TURNON_RSP);
66 
67 out:
68 	LEAVE();
69 	return error;
70 }
71 
72 /**
73  * as10x_cmd_turn_off - send turn off command to AS10x
74  * @adap:   pointer to AS10x bus adapter
75  *
76  * Return 0 on success or negative value in case of error.
77  */
as10x_cmd_turn_off(struct as10x_bus_adapter_t * adap)78 int as10x_cmd_turn_off(struct as10x_bus_adapter_t *adap)
79 {
80 	int error;
81 	struct as10x_cmd_t *pcmd, *prsp;
82 
83 	ENTER();
84 
85 	pcmd = adap->cmd;
86 	prsp = adap->rsp;
87 
88 	/* prepare command */
89 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
90 			sizeof(pcmd->body.turn_off.req));
91 
92 	/* fill command */
93 	pcmd->body.turn_off.req.proc_id = cpu_to_le16(CONTROL_PROC_TURNOFF);
94 
95 	/* send command */
96 	if (adap->ops->xfer_cmd) {
97 		error = adap->ops->xfer_cmd(
98 			adap, (uint8_t *) pcmd,
99 			sizeof(pcmd->body.turn_off.req) + HEADER_SIZE,
100 			(uint8_t *) prsp,
101 			sizeof(prsp->body.turn_off.rsp) + HEADER_SIZE);
102 	} else {
103 		error = AS10X_CMD_ERROR;
104 	}
105 
106 	if (error < 0)
107 		goto out;
108 
109 	/* parse response */
110 	error = as10x_rsp_parse(prsp, CONTROL_PROC_TURNOFF_RSP);
111 
112 out:
113 	LEAVE();
114 	return error;
115 }
116 
117 /**
118  * as10x_cmd_set_tune - send set tune command to AS10x
119  * @adap:    pointer to AS10x bus adapter
120  * @ptune:   tune parameters
121  *
122  * Return 0 on success or negative value in case of error.
123  */
as10x_cmd_set_tune(struct as10x_bus_adapter_t * adap,struct as10x_tune_args * ptune)124 int as10x_cmd_set_tune(struct as10x_bus_adapter_t *adap,
125 		       struct as10x_tune_args *ptune)
126 {
127 	int error;
128 	struct as10x_cmd_t *preq, *prsp;
129 
130 	ENTER();
131 
132 	preq = adap->cmd;
133 	prsp = adap->rsp;
134 
135 	/* prepare command */
136 	as10x_cmd_build(preq, (++adap->cmd_xid),
137 			sizeof(preq->body.set_tune.req));
138 
139 	/* fill command */
140 	preq->body.set_tune.req.proc_id = cpu_to_le16(CONTROL_PROC_SETTUNE);
141 	preq->body.set_tune.req.args.freq = cpu_to_le32(ptune->freq);
142 	preq->body.set_tune.req.args.bandwidth = ptune->bandwidth;
143 	preq->body.set_tune.req.args.hier_select = ptune->hier_select;
144 	preq->body.set_tune.req.args.modulation = ptune->modulation;
145 	preq->body.set_tune.req.args.hierarchy = ptune->hierarchy;
146 	preq->body.set_tune.req.args.interleaving_mode  =
147 		ptune->interleaving_mode;
148 	preq->body.set_tune.req.args.code_rate  = ptune->code_rate;
149 	preq->body.set_tune.req.args.guard_interval = ptune->guard_interval;
150 	preq->body.set_tune.req.args.transmission_mode  =
151 		ptune->transmission_mode;
152 
153 	/* send command */
154 	if (adap->ops->xfer_cmd) {
155 		error = adap->ops->xfer_cmd(adap,
156 					    (uint8_t *) preq,
157 					    sizeof(preq->body.set_tune.req)
158 					    + HEADER_SIZE,
159 					    (uint8_t *) prsp,
160 					    sizeof(prsp->body.set_tune.rsp)
161 					    + HEADER_SIZE);
162 	} else {
163 		error = AS10X_CMD_ERROR;
164 	}
165 
166 	if (error < 0)
167 		goto out;
168 
169 	/* parse response */
170 	error = as10x_rsp_parse(prsp, CONTROL_PROC_SETTUNE_RSP);
171 
172 out:
173 	LEAVE();
174 	return error;
175 }
176 
177 /**
178  * as10x_cmd_get_tune_status - send get tune status command to AS10x
179  * @adap: pointer to AS10x bus adapter
180  * @pstatus: pointer to updated status structure of the current tune
181  *
182  * Return 0 on success or negative value in case of error.
183  */
as10x_cmd_get_tune_status(struct as10x_bus_adapter_t * adap,struct as10x_tune_status * pstatus)184 int as10x_cmd_get_tune_status(struct as10x_bus_adapter_t *adap,
185 			      struct as10x_tune_status *pstatus)
186 {
187 	int error;
188 	struct as10x_cmd_t  *preq, *prsp;
189 
190 	ENTER();
191 
192 	preq = adap->cmd;
193 	prsp = adap->rsp;
194 
195 	/* prepare command */
196 	as10x_cmd_build(preq, (++adap->cmd_xid),
197 			sizeof(preq->body.get_tune_status.req));
198 
199 	/* fill command */
200 	preq->body.get_tune_status.req.proc_id =
201 		cpu_to_le16(CONTROL_PROC_GETTUNESTAT);
202 
203 	/* send command */
204 	if (adap->ops->xfer_cmd) {
205 		error = adap->ops->xfer_cmd(
206 			adap,
207 			(uint8_t *) preq,
208 			sizeof(preq->body.get_tune_status.req) + HEADER_SIZE,
209 			(uint8_t *) prsp,
210 			sizeof(prsp->body.get_tune_status.rsp) + HEADER_SIZE);
211 	} else {
212 		error = AS10X_CMD_ERROR;
213 	}
214 
215 	if (error < 0)
216 		goto out;
217 
218 	/* parse response */
219 	error = as10x_rsp_parse(prsp, CONTROL_PROC_GETTUNESTAT_RSP);
220 	if (error < 0)
221 		goto out;
222 
223 	/* Response OK -> get response data */
224 	pstatus->tune_state = prsp->body.get_tune_status.rsp.sts.tune_state;
225 	pstatus->signal_strength  =
226 		le16_to_cpu(prsp->body.get_tune_status.rsp.sts.signal_strength);
227 	pstatus->PER = le16_to_cpu(prsp->body.get_tune_status.rsp.sts.PER);
228 	pstatus->BER = le16_to_cpu(prsp->body.get_tune_status.rsp.sts.BER);
229 
230 out:
231 	LEAVE();
232 	return error;
233 }
234 
235 /**
236  * as10x_cmd_get_tps - send get TPS command to AS10x
237  * @adap:      pointer to AS10x handle
238  * @ptps:      pointer to TPS parameters structure
239  *
240  * Return 0 on success or negative value in case of error.
241  */
as10x_cmd_get_tps(struct as10x_bus_adapter_t * adap,struct as10x_tps * ptps)242 int as10x_cmd_get_tps(struct as10x_bus_adapter_t *adap, struct as10x_tps *ptps)
243 {
244 	int error;
245 	struct as10x_cmd_t *pcmd, *prsp;
246 
247 	ENTER();
248 
249 	pcmd = adap->cmd;
250 	prsp = adap->rsp;
251 
252 	/* prepare command */
253 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
254 			sizeof(pcmd->body.get_tps.req));
255 
256 	/* fill command */
257 	pcmd->body.get_tune_status.req.proc_id =
258 		cpu_to_le16(CONTROL_PROC_GETTPS);
259 
260 	/* send command */
261 	if (adap->ops->xfer_cmd) {
262 		error = adap->ops->xfer_cmd(adap,
263 					    (uint8_t *) pcmd,
264 					    sizeof(pcmd->body.get_tps.req) +
265 					    HEADER_SIZE,
266 					    (uint8_t *) prsp,
267 					    sizeof(prsp->body.get_tps.rsp) +
268 					    HEADER_SIZE);
269 	} else {
270 		error = AS10X_CMD_ERROR;
271 	}
272 
273 	if (error < 0)
274 		goto out;
275 
276 	/* parse response */
277 	error = as10x_rsp_parse(prsp, CONTROL_PROC_GETTPS_RSP);
278 	if (error < 0)
279 		goto out;
280 
281 	/* Response OK -> get response data */
282 	ptps->modulation = prsp->body.get_tps.rsp.tps.modulation;
283 	ptps->hierarchy = prsp->body.get_tps.rsp.tps.hierarchy;
284 	ptps->interleaving_mode = prsp->body.get_tps.rsp.tps.interleaving_mode;
285 	ptps->code_rate_HP = prsp->body.get_tps.rsp.tps.code_rate_HP;
286 	ptps->code_rate_LP = prsp->body.get_tps.rsp.tps.code_rate_LP;
287 	ptps->guard_interval = prsp->body.get_tps.rsp.tps.guard_interval;
288 	ptps->transmission_mode  = prsp->body.get_tps.rsp.tps.transmission_mode;
289 	ptps->DVBH_mask_HP = prsp->body.get_tps.rsp.tps.DVBH_mask_HP;
290 	ptps->DVBH_mask_LP = prsp->body.get_tps.rsp.tps.DVBH_mask_LP;
291 	ptps->cell_ID = le16_to_cpu(prsp->body.get_tps.rsp.tps.cell_ID);
292 
293 out:
294 	LEAVE();
295 	return error;
296 }
297 
298 /**
299  * as10x_cmd_get_demod_stats - send get demod stats command to AS10x
300  * @adap:          pointer to AS10x bus adapter
301  * @pdemod_stats:  pointer to demod stats parameters structure
302  *
303  * Return 0 on success or negative value in case of error.
304  */
as10x_cmd_get_demod_stats(struct as10x_bus_adapter_t * adap,struct as10x_demod_stats * pdemod_stats)305 int as10x_cmd_get_demod_stats(struct as10x_bus_adapter_t *adap,
306 			      struct as10x_demod_stats *pdemod_stats)
307 {
308 	int error;
309 	struct as10x_cmd_t *pcmd, *prsp;
310 
311 	ENTER();
312 
313 	pcmd = adap->cmd;
314 	prsp = adap->rsp;
315 
316 	/* prepare command */
317 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
318 			sizeof(pcmd->body.get_demod_stats.req));
319 
320 	/* fill command */
321 	pcmd->body.get_demod_stats.req.proc_id =
322 		cpu_to_le16(CONTROL_PROC_GET_DEMOD_STATS);
323 
324 	/* send command */
325 	if (adap->ops->xfer_cmd) {
326 		error = adap->ops->xfer_cmd(adap,
327 				(uint8_t *) pcmd,
328 				sizeof(pcmd->body.get_demod_stats.req)
329 				+ HEADER_SIZE,
330 				(uint8_t *) prsp,
331 				sizeof(prsp->body.get_demod_stats.rsp)
332 				+ HEADER_SIZE);
333 	} else {
334 		error = AS10X_CMD_ERROR;
335 	}
336 
337 	if (error < 0)
338 		goto out;
339 
340 	/* parse response */
341 	error = as10x_rsp_parse(prsp, CONTROL_PROC_GET_DEMOD_STATS_RSP);
342 	if (error < 0)
343 		goto out;
344 
345 	/* Response OK -> get response data */
346 	pdemod_stats->frame_count =
347 		le32_to_cpu(prsp->body.get_demod_stats.rsp.stats.frame_count);
348 	pdemod_stats->bad_frame_count =
349 		le32_to_cpu(prsp->body.get_demod_stats.rsp.stats.bad_frame_count);
350 	pdemod_stats->bytes_fixed_by_rs =
351 		le32_to_cpu(prsp->body.get_demod_stats.rsp.stats.bytes_fixed_by_rs);
352 	pdemod_stats->mer =
353 		le16_to_cpu(prsp->body.get_demod_stats.rsp.stats.mer);
354 	pdemod_stats->has_started =
355 		prsp->body.get_demod_stats.rsp.stats.has_started;
356 
357 out:
358 	LEAVE();
359 	return error;
360 }
361 
362 /**
363  * as10x_cmd_get_impulse_resp - send get impulse response command to AS10x
364  * @adap:     pointer to AS10x bus adapter
365  * @is_ready: pointer to value indicating when impulse
366  *	      response data is ready
367  *
368  * Return 0 on success or negative value in case of error.
369  */
as10x_cmd_get_impulse_resp(struct as10x_bus_adapter_t * adap,uint8_t * is_ready)370 int as10x_cmd_get_impulse_resp(struct as10x_bus_adapter_t *adap,
371 			       uint8_t *is_ready)
372 {
373 	int error;
374 	struct as10x_cmd_t *pcmd, *prsp;
375 
376 	ENTER();
377 
378 	pcmd = adap->cmd;
379 	prsp = adap->rsp;
380 
381 	/* prepare command */
382 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
383 			sizeof(pcmd->body.get_impulse_rsp.req));
384 
385 	/* fill command */
386 	pcmd->body.get_impulse_rsp.req.proc_id =
387 		cpu_to_le16(CONTROL_PROC_GET_IMPULSE_RESP);
388 
389 	/* send command */
390 	if (adap->ops->xfer_cmd) {
391 		error = adap->ops->xfer_cmd(adap,
392 					(uint8_t *) pcmd,
393 					sizeof(pcmd->body.get_impulse_rsp.req)
394 					+ HEADER_SIZE,
395 					(uint8_t *) prsp,
396 					sizeof(prsp->body.get_impulse_rsp.rsp)
397 					+ HEADER_SIZE);
398 	} else {
399 		error = AS10X_CMD_ERROR;
400 	}
401 
402 	if (error < 0)
403 		goto out;
404 
405 	/* parse response */
406 	error = as10x_rsp_parse(prsp, CONTROL_PROC_GET_IMPULSE_RESP_RSP);
407 	if (error < 0)
408 		goto out;
409 
410 	/* Response OK -> get response data */
411 	*is_ready = prsp->body.get_impulse_rsp.rsp.is_ready;
412 
413 out:
414 	LEAVE();
415 	return error;
416 }
417 
418 /**
419  * as10x_cmd_build - build AS10x command header
420  * @pcmd:     pointer to AS10x command buffer
421  * @xid:      sequence id of the command
422  * @cmd_len:  length of the command
423  */
as10x_cmd_build(struct as10x_cmd_t * pcmd,uint16_t xid,uint16_t cmd_len)424 void as10x_cmd_build(struct as10x_cmd_t *pcmd,
425 		     uint16_t xid, uint16_t cmd_len)
426 {
427 	pcmd->header.req_id = cpu_to_le16(xid);
428 	pcmd->header.prog = cpu_to_le16(SERVICE_PROG_ID);
429 	pcmd->header.version = cpu_to_le16(SERVICE_PROG_VERSION);
430 	pcmd->header.data_len = cpu_to_le16(cmd_len);
431 }
432 
433 /**
434  * as10x_rsp_parse - Parse command response
435  * @prsp:       pointer to AS10x command buffer
436  * @proc_id:    id of the command
437  *
438  * Return 0 on success or negative value in case of error.
439  */
as10x_rsp_parse(struct as10x_cmd_t * prsp,uint16_t proc_id)440 int as10x_rsp_parse(struct as10x_cmd_t *prsp, uint16_t proc_id)
441 {
442 	int error;
443 
444 	/* extract command error code */
445 	error = prsp->body.common.rsp.error;
446 
447 	if ((error == 0) &&
448 	    (le16_to_cpu(prsp->body.common.rsp.proc_id) == proc_id)) {
449 		return 0;
450 	}
451 
452 	return AS10X_CMD_ERROR;
453 }
454