1 /*
2  * Abilis Systems Single DVB-T Receiver
3  * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #include <linux/kernel.h>
21 #include "as102_drv.h"
22 #include "as10x_types.h"
23 #include "as10x_cmd.h"
24 
25 /***************************/
26 /* FUNCTION DEFINITION     */
27 /***************************/
28 
29 /**
30  * as10x_cmd_get_context - Send get context command to AS10x
31  * @adap:      pointer to AS10x bus adapter
32  * @tag:       context tag
33  * @pvalue:    pointer where to store context value read
34  *
35  * Return 0 on success or negative value in case of error.
36  */
as10x_cmd_get_context(struct as10x_bus_adapter_t * adap,uint16_t tag,uint32_t * pvalue)37 int as10x_cmd_get_context(struct as10x_bus_adapter_t *adap, uint16_t tag,
38 			  uint32_t *pvalue)
39 {
40 	int  error;
41 	struct as10x_cmd_t *pcmd, *prsp;
42 
43 	ENTER();
44 
45 	pcmd = adap->cmd;
46 	prsp = adap->rsp;
47 
48 	/* prepare command */
49 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
50 			sizeof(pcmd->body.context.req));
51 
52 	/* fill command */
53 	pcmd->body.context.req.proc_id = cpu_to_le16(CONTROL_PROC_CONTEXT);
54 	pcmd->body.context.req.tag = cpu_to_le16(tag);
55 	pcmd->body.context.req.type = cpu_to_le16(GET_CONTEXT_DATA);
56 
57 	/* send command */
58 	if (adap->ops->xfer_cmd) {
59 		error  = adap->ops->xfer_cmd(adap,
60 					     (uint8_t *) pcmd,
61 					     sizeof(pcmd->body.context.req)
62 					     + HEADER_SIZE,
63 					     (uint8_t *) prsp,
64 					     sizeof(prsp->body.context.rsp)
65 					     + HEADER_SIZE);
66 	} else {
67 		error = AS10X_CMD_ERROR;
68 	}
69 
70 	if (error < 0)
71 		goto out;
72 
73 	/* parse response: context command do not follow the common response */
74 	/* structure -> specific handling response parse required            */
75 	error = as10x_context_rsp_parse(prsp, CONTROL_PROC_CONTEXT_RSP);
76 
77 	if (error == 0) {
78 		/* Response OK -> get response data */
79 		*pvalue = le32_to_cpu(prsp->body.context.rsp.reg_val.u.value32);
80 		/* value returned is always a 32-bit value */
81 	}
82 
83 out:
84 	LEAVE();
85 	return error;
86 }
87 
88 /**
89  * as10x_cmd_set_context - send set context command to AS10x
90  * @adap:      pointer to AS10x bus adapter
91  * @tag:       context tag
92  * @value:     value to set in context
93  *
94  * Return 0 on success or negative value in case of error.
95  */
as10x_cmd_set_context(struct as10x_bus_adapter_t * adap,uint16_t tag,uint32_t value)96 int as10x_cmd_set_context(struct as10x_bus_adapter_t *adap, uint16_t tag,
97 			  uint32_t value)
98 {
99 	int error;
100 	struct as10x_cmd_t *pcmd, *prsp;
101 
102 	ENTER();
103 
104 	pcmd = adap->cmd;
105 	prsp = adap->rsp;
106 
107 	/* prepare command */
108 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
109 			sizeof(pcmd->body.context.req));
110 
111 	/* fill command */
112 	pcmd->body.context.req.proc_id = cpu_to_le16(CONTROL_PROC_CONTEXT);
113 	/* pcmd->body.context.req.reg_val.mode initialization is not required */
114 	pcmd->body.context.req.reg_val.u.value32 = cpu_to_le32(value);
115 	pcmd->body.context.req.tag = cpu_to_le16(tag);
116 	pcmd->body.context.req.type = cpu_to_le16(SET_CONTEXT_DATA);
117 
118 	/* send command */
119 	if (adap->ops->xfer_cmd) {
120 		error  = adap->ops->xfer_cmd(adap,
121 					     (uint8_t *) pcmd,
122 					     sizeof(pcmd->body.context.req)
123 					     + HEADER_SIZE,
124 					     (uint8_t *) prsp,
125 					     sizeof(prsp->body.context.rsp)
126 					     + HEADER_SIZE);
127 	} else {
128 		error = AS10X_CMD_ERROR;
129 	}
130 
131 	if (error < 0)
132 		goto out;
133 
134 	/* parse response: context command do not follow the common response */
135 	/* structure -> specific handling response parse required            */
136 	error = as10x_context_rsp_parse(prsp, CONTROL_PROC_CONTEXT_RSP);
137 
138 out:
139 	LEAVE();
140 	return error;
141 }
142 
143 /**
144  * as10x_cmd_eLNA_change_mode - send eLNA change mode command to AS10x
145  * @adap:      pointer to AS10x bus adapter
146  * @mode:      mode selected:
147  *	        - ON    : 0x0 => eLNA always ON
148  *	        - OFF   : 0x1 => eLNA always OFF
149  *	        - AUTO  : 0x2 => eLNA follow hysteresis parameters
150  *				 to be ON or OFF
151  *
152  * Return 0 on success or negative value in case of error.
153  */
as10x_cmd_eLNA_change_mode(struct as10x_bus_adapter_t * adap,uint8_t mode)154 int as10x_cmd_eLNA_change_mode(struct as10x_bus_adapter_t *adap, uint8_t mode)
155 {
156 	int error;
157 	struct as10x_cmd_t *pcmd, *prsp;
158 
159 	ENTER();
160 
161 	pcmd = adap->cmd;
162 	prsp = adap->rsp;
163 
164 	/* prepare command */
165 	as10x_cmd_build(pcmd, (++adap->cmd_xid),
166 			sizeof(pcmd->body.cfg_change_mode.req));
167 
168 	/* fill command */
169 	pcmd->body.cfg_change_mode.req.proc_id =
170 		cpu_to_le16(CONTROL_PROC_ELNA_CHANGE_MODE);
171 	pcmd->body.cfg_change_mode.req.mode = mode;
172 
173 	/* send command */
174 	if (adap->ops->xfer_cmd) {
175 		error  = adap->ops->xfer_cmd(adap, (uint8_t *) pcmd,
176 				sizeof(pcmd->body.cfg_change_mode.req)
177 				+ HEADER_SIZE, (uint8_t *) prsp,
178 				sizeof(prsp->body.cfg_change_mode.rsp)
179 				+ HEADER_SIZE);
180 	} else {
181 		error = AS10X_CMD_ERROR;
182 	}
183 
184 	if (error < 0)
185 		goto out;
186 
187 	/* parse response */
188 	error = as10x_rsp_parse(prsp, CONTROL_PROC_ELNA_CHANGE_MODE_RSP);
189 
190 out:
191 	LEAVE();
192 	return error;
193 }
194 
195 /**
196  * as10x_context_rsp_parse - Parse context command response
197  * @prsp:       pointer to AS10x command response buffer
198  * @proc_id:    id of the command
199  *
200  * Since the contex command reponse does not follow the common
201  * response, a specific parse function is required.
202  * Return 0 on success or negative value in case of error.
203  */
as10x_context_rsp_parse(struct as10x_cmd_t * prsp,uint16_t proc_id)204 int as10x_context_rsp_parse(struct as10x_cmd_t *prsp, uint16_t proc_id)
205 {
206 	int err;
207 
208 	err = prsp->body.context.rsp.error;
209 
210 	if ((err == 0) &&
211 	    (le16_to_cpu(prsp->body.context.rsp.proc_id) == proc_id)) {
212 		return 0;
213 	}
214 	return AS10X_CMD_ERROR;
215 }
216