xref: /OK3568_Linux_fs/kernel/drivers/usb/serial/xr_usb_serial_hal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 #define XR_SET_MAP_XR2280X		5
18 #define XR_GET_MAP_XR2280X		5
19 
20 #define XR_SET_MAP_XR21B142X		0
21 #define XR_GET_MAP_XR21B142X		0
22 
23 #define XR_SET_MAP_XR21V141X		0
24 #define XR_GET_MAP_XR21V141X		1
25 
26 #define XR_SET_MAP_XR21B1411		0
27 #define XR_GET_MAP_XR21B1411		1
28 
29 
xr_usb_serial_set_reg(struct xr_usb_serial * xr_usb_serial,int regnum,int value)30 int xr_usb_serial_set_reg(struct xr_usb_serial *xr_usb_serial,int regnum, int value)
31 {
32 	int result;
33 	int channel = 0;
34 	//dev_info(&xr_usb_serial->control->dev, "%s Channel:%d 0x%02x = 0x%02x\n", __func__,channel,regnum, value);
35 	if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
36 	{
37 		int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
38 		result = usb_control_msg(xr_usb_serial->dev,			   	/* usb device */
39 					 usb_sndctrlpipe(xr_usb_serial->dev, 0), 	/* endpoint pipe */
40 					 XR_SET_MAP_XR2280X,			   	/* request */
41 					 USB_DIR_OUT | USB_TYPE_VENDOR,   	  	/* request_type */
42 					 value,                           	  	/* request value */
43 					 XR2280xaddr,           		  	/* index */
44 					 NULL,                            	  	/* data */
45 					 0,                               	  	/* size */
46 					 5000);                           	  	/* timeout */
47 	}
48 	else if((xr_usb_serial->DeviceProduct == 0x1410) ||
49 		(xr_usb_serial->DeviceProduct == 0x1412) ||
50 		(xr_usb_serial->DeviceProduct == 0x1414))
51 	{
52 		if(xr_usb_serial->channel)
53 			channel = xr_usb_serial->channel - 1;
54  		result = usb_control_msg(xr_usb_serial->dev,			  	/* usb device */
55 					usb_sndctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
56 					XR_SET_MAP_XR21V141X, 			  	/* request */
57 					USB_DIR_OUT | USB_TYPE_VENDOR,  	  	/* request_type */
58 					value,					  	/* request value */
59 					regnum | (channel << 8),		  	/* index */
60 					NULL,					  	/* data */
61 					0,					  	/* size */
62 					5000);					  	/* timeout */
63 	}
64 	else if(xr_usb_serial->DeviceProduct == 0x1411)
65 	{
66 		result = usb_control_msg(xr_usb_serial->dev,			    	/* usb device */
67 					 usb_sndctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
68 					 XR_SET_MAP_XR21B1411,			    	/* request */
69 					 USB_DIR_OUT | USB_TYPE_VENDOR, 	    	/* request_type */
70 					 value,                         	    	/* request value */
71 					 regnum ,                         	    	/* index */
72 					 NULL,                            	    	/* data */
73 					 0,                               	    	/* size */
74 					 5000);                           	    	/* timeout */
75 	}
76 	else if((xr_usb_serial->DeviceProduct == 0x1420)||
77 		 (xr_usb_serial->DeviceProduct == 0x1422)||
78 		 (xr_usb_serial->DeviceProduct == 0x1424))
79 	{
80 		channel = (xr_usb_serial->channel - 4)*2;
81 		result = usb_control_msg(xr_usb_serial->dev,			   	/* usb device */
82 					 usb_sndctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
83 					 XR_SET_MAP_XR21B142X,			   	/* request */
84 					 USB_DIR_OUT | USB_TYPE_VENDOR | 1,	   	/* request_type */
85 					 value,                           	   	/* request value */
86 					 regnum | (channel << 8),           	   	/* index */
87 					 NULL,                            	   	/* data */
88 					 0,                               	   	/* size */
89 					 5000);                           	   	/* timeout */
90 	}
91 	else
92 	{
93 		result = -1;
94 	}
95 	if(result < 0)
96 		dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
97 	return result;
98 }
99 
xr_usb_serial_set_reg_ext(struct xr_usb_serial * xr_usb_serial,int channel,int regnum,int value)100 int xr_usb_serial_set_reg_ext(struct xr_usb_serial *xr_usb_serial,int channel,int regnum, int value)
101 {
102 	int result;
103 	int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
104 	//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%02x = 0x%02x\n", __func__,channel,regnum, value);
105 	if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
106 	{
107 		result = usb_control_msg(xr_usb_serial->dev,                    	/* usb device */
108 					 usb_sndctrlpipe(xr_usb_serial->dev, 0), 	/* endpoint pipe */
109 					 XR_SET_MAP_XR2280X,                      	/* request */
110 					 USB_DIR_OUT | USB_TYPE_VENDOR,         	/* request_type */
111 					 value,                                 	/* request value */
112 					 XR2280xaddr,                           	/* index */
113 					 NULL,                                   	/* data */
114 					 0,                                      	/* size */
115 					 5000);                                 	/* timeout */
116 	}
117 	else if((xr_usb_serial->DeviceProduct == 0x1410) ||
118 		(xr_usb_serial->DeviceProduct == 0x1412) ||
119 		(xr_usb_serial->DeviceProduct == 0x1414))
120 	{
121 		result = usb_control_msg(xr_usb_serial->dev,             		/* usb device */
122 					usb_sndctrlpipe(xr_usb_serial->dev, 0), 	/* endpoint pipe */
123 					XR_SET_MAP_XR21V141X,                    	/* request */
124 					USB_DIR_OUT | USB_TYPE_VENDOR,           	/* request_type */
125 					value,	                                	/* request value */
126 					regnum | (channel << 8),                 	/* index */
127 					NULL,                                     	/* data */
128 					0,                                        	/* size */
129 					5000);	                                   	/* timeout */
130 	}
131 	else if(xr_usb_serial->DeviceProduct == 0x1411)
132 	{
133 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
134 					 usb_sndctrlpipe(xr_usb_serial->dev, 0), 	/* endpoint pipe */
135 					 XR_SET_MAP_XR21B1411,                   	/* request */
136 					 USB_DIR_OUT | USB_TYPE_VENDOR ,        	/* request_type */
137 					 value,                           		/* request value */
138 					 regnum ,                         		/* index */
139 					 NULL,                            		/* data */
140 					 0,                               		/* size */
141 					 5000);                           		/* timeout */
142 	}
143 	else if((xr_usb_serial->DeviceProduct == 0x1420)||
144 		(xr_usb_serial->DeviceProduct == 0x1422)||
145 		(xr_usb_serial->DeviceProduct == 0x1424))
146 	{
147 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
148 					 usb_sndctrlpipe(xr_usb_serial->dev, 0), 	/* endpoint pipe */
149 					 XR_SET_MAP_XR21B142X,                   	/* request */
150 					 USB_DIR_OUT | USB_TYPE_VENDOR | 1,       	/* request_type */
151 					 value,                           		/* request value */
152 					 regnum | (channel << 8),           		/* index */
153 					 NULL,                            		/* data */
154 					 0,                               		/* size */
155 					 5000);                           		/* timeout */
156 	}
157 	else
158 	{
159 		result = -1;
160 	}
161 	if(result < 0)
162 		dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
163 	return result;
164 }
165 
xr_usb_serial_get_reg(struct xr_usb_serial * xr_usb_serial,int regnum,short * value)166 int xr_usb_serial_get_reg(struct xr_usb_serial *xr_usb_serial,int regnum, short *value)
167 {
168 	int result;
169 	int channel = 0;
170 	short *dmadata;
171 
172 	/* Use dynamic memory instead of statically allocated buffer
173 	   This is to avoid Error -11 (EAGAIN) for Kernel no longer accept static allocated buffer after 4.9 */
174 	dmadata = kmalloc(2, GFP_KERNEL);
175 
176 	if (!dmadata)
177 	{
178 		dev_err(&xr_usb_serial->control->dev, "[func:%s,line:%d]  error no memory allocated!!  \n", __func__,__LINE__);
179 		return -ENOMEM;
180 	}
181 
182 	if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
183 	{
184 		int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
185 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
186 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0), 	/* endpoint pipe */
187 					 XR_GET_MAP_XR2280X,                     	/* request */
188 					 USB_DIR_IN | USB_TYPE_VENDOR ,         	/* request_type */
189 					 0,                                        	/* request value */
190 					 XR2280xaddr,                           	/* index */
191 					 dmadata,                                	/* data */
192 					 2,                                      	/* size */
193 					 5000);                                    	/* timeout */
194 		memcpy(value, dmadata, 2);
195 	}
196 	else if((xr_usb_serial->DeviceProduct == 0x1410) ||
197 		(xr_usb_serial->DeviceProduct == 0x1412) ||
198 		(xr_usb_serial->DeviceProduct == 0x1414))
199 	{
200 		if(xr_usb_serial->channel)
201 			channel = xr_usb_serial->channel -1;
202 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
203 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0), 	/* endpoint pipe */
204 					 XR_GET_MAP_XR21V141X,                     	/* request */
205 					 USB_DIR_IN | USB_TYPE_VENDOR,           	/* request_type */
206 					 0,                                      	/* request value */
207 					 regnum | (channel << 8),                	/* index */
208 					 dmadata,                                	/* data */
209 					 1,                                       	/* size */
210 					 5000);                                   	/* timeout */
211 		memcpy(value, dmadata, 1);
212 	}
213 	else if(xr_usb_serial->DeviceProduct == 0x1411)
214 	{
215 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
216 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
217 					 XR_GET_MAP_XR21B1411,                     	/* request */
218 					 USB_DIR_IN | USB_TYPE_VENDOR,            	/* request_type */
219 					 0,                                       	/* request value */
220 					 regnum,                                  	/* index */
221 					 dmadata,                                 	/* data */
222 					 2,                                       	/* size */
223 					 5000);                                    	/* timeout */
224 		memcpy(value, dmadata, 2);
225 	}
226 	else if((xr_usb_serial->DeviceProduct == 0x1420)||
227 		(xr_usb_serial->DeviceProduct == 0x1422)||
228 		(xr_usb_serial->DeviceProduct == 0x1424))
229 	{
230 		channel = (xr_usb_serial->channel -4)*2;
231 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
232 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
233 					 XR_GET_MAP_XR21B142X,                     	/* request */
234 					 USB_DIR_IN | USB_TYPE_VENDOR | 1,       	/* request_type */
235 					 0,                                      	/* request value */
236 					 regnum | (channel << 8),                	/* index */
237 					 dmadata,                                 	/* data */
238 					 2,                                        	/* size */
239 					 5000);                                   	/* timeout */
240 		memcpy(value, dmadata, 2);
241 	}
242 	else
243 	{
244 		result = -1;
245 	}
246 
247 	if(result < 0)
248 		dev_err(&xr_usb_serial->control->dev, "%s channel:%d Reg 0x%x Error:%d\n", __func__,channel,regnum,result);
249 	//else
250 	  //dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value);
251 
252 	kfree(dmadata);
253 	return result;
254 }
255 
xr_usb_serial_get_reg_ext(struct xr_usb_serial * xr_usb_serial,int channel,int regnum,short * value)256 int xr_usb_serial_get_reg_ext(struct xr_usb_serial *xr_usb_serial,int channel,int regnum, short *value)
257 {
258 	int result;
259 	int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
260 	short *dmadata;
261 
262 	/* Use dynamic memory instead of statically allocated buffer
263 	   This is to avoid Error -11 (EAGAIN) for Kernel no longer accept static allocated buffer after 4.9 */
264 	dmadata = kmalloc(2, GFP_KERNEL);
265 
266 	if (!dmadata)
267 	{
268 		dev_err(&xr_usb_serial->control->dev, "[func:%s,line:%d]  error no memory allocated!!  \n", __func__,__LINE__);
269 		return -ENOMEM;
270 	}
271 
272 	if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
273 	{
274 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
275 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
276 					 XR_GET_MAP_XR2280X,                      	/* request */
277 					 USB_DIR_IN | USB_TYPE_VENDOR ,           	/* request_type */
278 					 0,                                       	/* request value */
279 					 XR2280xaddr,                             	/* index */
280 					 dmadata,                                 	/* data */
281 					 2,                                        	/* size */
282 					 5000);                                   	/* timeout */
283 		memcpy(value, dmadata, 2);
284 	}
285 	else if((xr_usb_serial->DeviceProduct == 0x1410) ||
286 		(xr_usb_serial->DeviceProduct == 0x1412) ||
287 		(xr_usb_serial->DeviceProduct == 0x1414))
288 	{
289 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
290 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
291 					 XR_GET_MAP_XR21V141X,                     	/* request */
292 					 USB_DIR_IN | USB_TYPE_VENDOR,            	/* request_type */
293 					 0,                                       	/* request value */
294 					 regnum | (channel << 8),               	/* index */
295 					 dmadata,                                 	/* data */
296 					 1,                                        	/* size */
297 					 5000);                                    	/* timeout */
298 		memcpy(value, dmadata, 1);
299 	}
300 	else if(xr_usb_serial->DeviceProduct == 0x1411)
301 	{
302 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
303 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
304 					 XR_GET_MAP_XR21B1411,                    	/* request */
305 					 USB_DIR_IN | USB_TYPE_VENDOR ,           	/* request_type */
306 					 0,                                       	/* request value */
307 					 regnum | (channel << 8),                 	/* index */
308 					 dmadata,                                 	/* data */
309 					 2,                                       	/* size */
310 					 5000);                                   	/* timeout */
311 		memcpy(value, dmadata, 2);
312 	}
313 	else if((xr_usb_serial->DeviceProduct == 0x1420)||
314 		(xr_usb_serial->DeviceProduct == 0x1422)||
315 		(xr_usb_serial->DeviceProduct == 0x1424))
316 	{
317 		result = usb_control_msg(xr_usb_serial->dev,                     	/* usb device */
318 					 usb_rcvctrlpipe(xr_usb_serial->dev, 0),  	/* endpoint pipe */
319 					 XR_GET_MAP_XR21B142X,                    	/* request */
320 					 USB_DIR_IN | USB_TYPE_VENDOR | 1,        	/* request_type */
321 					 0,                                       	/* request value */
322 					 regnum | (channel << 8),                 	/* index */
323 					 dmadata,                                 	/* data */
324 					 2,                                       	/* size */
325 					 5000);                                   	/* timeout */
326 		memcpy(value, dmadata, 2);
327 	}
328 	else
329 	{
330 		result = -1;
331 	}
332 
333 	if(result < 0)
334 		dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
335 	//else
336 		//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value);
337 
338 	kfree(dmadata);
339 	return result;
340 }
341 
342 struct xr21v141x_baud_rate
343 {
344 	unsigned int tx;
345 	unsigned int rx0;
346 	unsigned int rx1;
347 };
348 
349 static struct xr21v141x_baud_rate xr21v141x_baud_rates[] = {
350 	{ 0x000, 0x000, 0x000 },
351 	{ 0x000, 0x000, 0x000 },
352 	{ 0x100, 0x000, 0x100 },
353 	{ 0x020, 0x400, 0x020 },
354 	{ 0x010, 0x100, 0x010 },
355 	{ 0x208, 0x040, 0x208 },
356 	{ 0x104, 0x820, 0x108 },
357 	{ 0x844, 0x210, 0x884 },
358 	{ 0x444, 0x110, 0x444 },
359 	{ 0x122, 0x888, 0x224 },
360 	{ 0x912, 0x448, 0x924 },
361 	{ 0x492, 0x248, 0x492 },
362 	{ 0x252, 0x928, 0x292 },
363 	{ 0X94A, 0X4A4, 0XA52 },
364 	{ 0X52A, 0XAA4, 0X54A },
365 	{ 0XAAA, 0x954, 0X4AA },
366 	{ 0XAAA, 0x554, 0XAAA },
367 	{ 0x555, 0XAD4, 0X5AA },
368 	{ 0XB55, 0XAB4, 0X55A },
369 	{ 0X6B5, 0X5AC, 0XB56 },
370 	{ 0X5B5, 0XD6C, 0X6D6 },
371 	{ 0XB6D, 0XB6A, 0XDB6 },
372 	{ 0X76D, 0X6DA, 0XBB6 },
373 	{ 0XEDD, 0XDDA, 0X76E },
374 	{ 0XDDD, 0XBBA, 0XEEE },
375 	{ 0X7BB, 0XF7A, 0XDDE },
376 	{ 0XF7B, 0XEF6, 0X7DE },
377 	{ 0XDF7, 0XBF6, 0XF7E },
378 	{ 0X7F7, 0XFEE, 0XEFE },
379 	{ 0XFDF, 0XFBE, 0X7FE },
380 	{ 0XF7F, 0XEFE, 0XFFE },
381 	{ 0XFFF, 0XFFE, 0XFFD },
382 };
383 #define UART_CLOCK_DIVISOR_0                               0x004
384 #define UART_CLOCK_DIVISOR_1                               0x005
385 #define UART_CLOCK_DIVISOR_2                               0x006
386 #define UART_TX_CLOCK_MASK_0                               0x007
387 #define UART_TX_CLOCK_MASK_1                               0x008
388 #define UART_RX_CLOCK_MASK_0                               0x009
389 #define UART_RX_CLOCK_MASK_1                               0x00a
390 
xr21v141x_set_baud_rate(struct xr_usb_serial * xr_usb_serial,unsigned int rate)391 static int xr21v141x_set_baud_rate(struct xr_usb_serial *xr_usb_serial, unsigned int rate)
392 {
393 	unsigned int 	divisor = 48000000 / rate;
394 	unsigned int 	i 	= ((32 * 48000000) / rate) & 0x1f;
395 	unsigned int 	tx_mask = xr21v141x_baud_rates[i].tx;
396 	unsigned int 	rx_mask = (divisor & 1) ? xr21v141x_baud_rates[i].rx1 : xr21v141x_baud_rates[i].rx0;
397 
398 	//dev_info(&xr_usb_serial->control->dev, "Setting baud rate to %d: i=%u div=%u tx=%03x rx=%03x\n", rate, i, divisor, tx_mask, rx_mask);
399 
400 	xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_0, (divisor >>  0) & 0xff);
401 	xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_1, (divisor >>  8) & 0xff);
402 	xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_2, (divisor >> 16) & 0xff);
403 	xr_usb_serial_set_reg(xr_usb_serial,UART_TX_CLOCK_MASK_0, (tx_mask >>  0) & 0xff);
404 	xr_usb_serial_set_reg(xr_usb_serial,UART_TX_CLOCK_MASK_1, (tx_mask >>  8) & 0xff);
405 	xr_usb_serial_set_reg(xr_usb_serial,UART_RX_CLOCK_MASK_0, (rx_mask >>  0) & 0xff);
406 	xr_usb_serial_set_reg(xr_usb_serial,UART_RX_CLOCK_MASK_1, (rx_mask >>  8) & 0xff);
407 
408 	return 0;
409 }
410 /* devices aren't required to support these requests.
411  * the cdc xr_usb_serial descriptor tells whether they do...
412  */
xr_usb_serial_set_control(struct xr_usb_serial * xr_usb_serial,unsigned int control)413 int xr_usb_serial_set_control(struct xr_usb_serial *xr_usb_serial, unsigned int control)
414 {
415 	int ret = 0;
416 
417 	if((xr_usb_serial->DeviceProduct == 0x1410) ||
418 	   (xr_usb_serial->DeviceProduct == 0x1412) ||
419 	   (xr_usb_serial->DeviceProduct == 0x1414))
420 	{
421 		if (control & XR_USB_SERIAL_CTRL_DTR)
422 			xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x08);
423 		else
424 			xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_set_addr, 0x08);
425 
426 		if (control & XR_USB_SERIAL_CTRL_RTS)
427 			xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x20);
428 		else
429 			xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_set_addr, 0x20);
430 	}
431 	else
432 	{
433 		ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0);
434 	}
435 
436 	return ret;
437 }
438 
xr_usb_serial_set_line(struct xr_usb_serial * xr_usb_serial,struct usb_cdc_line_coding * line)439 int xr_usb_serial_set_line(struct xr_usb_serial *xr_usb_serial, struct usb_cdc_line_coding* line)
440 {
441 	int ret = 0;
442 	unsigned int format_size;
443 	unsigned int format_parity;
444 	unsigned int format_stop;
445 	if((xr_usb_serial->DeviceProduct == 0x1410) ||
446 	   (xr_usb_serial->DeviceProduct == 0x1412) ||
447 	   (xr_usb_serial->DeviceProduct == 0x1414))
448 	{
449 		xr21v141x_set_baud_rate(xr_usb_serial,le32_to_cpu(line->dwDTERate));
450 		format_size = line->bDataBits;
451 		format_parity = line->bParityType;
452 		format_stop = line->bCharFormat;
453 		xr_usb_serial_set_reg(xr_usb_serial,
454 				xr_usb_serial->reg_map.uart_format_addr,
455 				(format_size << 0) | (format_parity << 4) | (format_stop << 7) );
456 	}
457 	else
458 	{
459 		ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line));
460 	}
461 	return ret;
462 }
463 
xr_usb_serial_set_flow_mode(struct xr_usb_serial * xr_usb_serial,struct tty_struct * tty,unsigned int cflag)464 int xr_usb_serial_set_flow_mode(struct xr_usb_serial *xr_usb_serial, struct tty_struct *tty, unsigned int cflag)
465 {
466 	unsigned int flow;
467 	unsigned int gpio_mode;
468 
469 	if (cflag & CRTSCTS)
470 	{
471 		//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:hardware\n");
472 		flow      = UART_FLOW_MODE_HW;
473 		gpio_mode = UART_GPIO_MODE_SEL_RTS_CTS;
474 	}
475 	else if (I_IXOFF(tty) || I_IXON(tty))
476 	{
477 		unsigned char   start_char = START_CHAR(tty);
478 		unsigned char   stop_char  = STOP_CHAR(tty);
479 		//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:software\n");
480 		flow      = UART_FLOW_MODE_SW;
481 		gpio_mode = UART_GPIO_MODE_SEL_GPIO;
482 
483 		xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_xon_char_addr, start_char);
484 		xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_xoff_char_addr, stop_char);
485 	}
486 	else
487 	{
488 		//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:none\n");
489 		flow      = UART_FLOW_MODE_NONE;
490 		gpio_mode = UART_GPIO_MODE_SEL_GPIO;
491 	}
492 
493 	if((xr_usb_serial->DeviceProduct == 0x1420)||
494 	   (xr_usb_serial->DeviceProduct == 0x1422)||
495 	   (xr_usb_serial->DeviceProduct == 0x1424))
496 	{
497 		//Add support for the TXT and RXT function for 0x1420, 0x1422, 0x1424, by setting GPIO_MODE [9:8] = '11'
498 		gpio_mode |= 0x300;
499 	}
500 
501 	xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_flow_addr, flow);
502 	xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr, gpio_mode);
503 	return 0;
504 }
505 
xr_usb_serial_send_break(struct xr_usb_serial * xr_usb_serial,int state)506 int xr_usb_serial_send_break(struct xr_usb_serial *xr_usb_serial, int state)
507 {
508 	int ret = 0;
509 	if((xr_usb_serial->DeviceProduct == 0x1410)||
510 	   (xr_usb_serial->DeviceProduct == 0x1412)||
511 	   (xr_usb_serial->DeviceProduct == 0x1414))
512 	{
513 		if(state)
514 			ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.tx_break_addr,0xffff);
515 		else
516 			ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.tx_break_addr,0);
517 	}
518 	else
519 	{
520 		ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SEND_BREAK, state, NULL, 0);
521 	}
522 	return ret;
523 }
524 
525 #define URM_REG_BLOCK          	4
526 #define URM_ENABLE_BASE        	0x010
527 #define URM_ENABLE_0_TX        	0x001
528 #define URM_ENABLE_0_RX        	0x002
529 #define URM_RESET_RX_FIFO_BASE	0x018
530 #define URM_RESET_TX_FIFO_BASE	0x01C
531 
xr_usb_serial_enable(struct xr_usb_serial * xr_usb_serial)532 int xr_usb_serial_enable(struct xr_usb_serial *xr_usb_serial)
533 {
534 	int ret = 0;
535 	int channel = xr_usb_serial->channel;
536 	//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_enable channel=%d\n",channel);
537 	if(channel)
538 		channel--;
539 	if((xr_usb_serial->DeviceProduct == 0x1410)||
540 	   (xr_usb_serial->DeviceProduct == 0x1412)||
541 	   (xr_usb_serial->DeviceProduct == 0x1414))
542 	{
543 		ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX);
544 		ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,UART_ENABLE_TX | UART_ENABLE_RX);
545 		ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX | URM_ENABLE_0_RX);
546 	}
547 	else
548 	{
549 		ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,UART_ENABLE_TX | UART_ENABLE_RX);
550 	}
551 
552 	return ret;
553 }
554 
xr_usb_serial_fifo_reset(struct xr_usb_serial * xr_usb_serial)555 int xr_usb_serial_fifo_reset(struct xr_usb_serial *xr_usb_serial)
556 {
557 	int ret = 0;
558 	int channel = xr_usb_serial->channel;
559 
560 	if(channel) channel--;
561 	if((xr_usb_serial->DeviceProduct == 0x1410)||
562 	   (xr_usb_serial->DeviceProduct == 0x1412)||
563 	   (xr_usb_serial->DeviceProduct == 0x1414))
564 	{
565 		ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_RESET_RX_FIFO_BASE + channel,0xff);
566 		ret |= xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_RESET_TX_FIFO_BASE + channel,0xff);
567 	}
568 	return ret;
569 }
570 
xr_usb_serial_disable(struct xr_usb_serial * xr_usb_serial)571 int xr_usb_serial_disable(struct xr_usb_serial *xr_usb_serial)
572 {
573 	int ret = 0;
574 	int channel = xr_usb_serial->channel;
575 	//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_disable channel=%d\n",channel);
576 	if(channel) channel--;
577 	ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,0);
578 	if((xr_usb_serial->DeviceProduct == 0x1410)||
579 	   (xr_usb_serial->DeviceProduct == 0x1412)||
580 	   (xr_usb_serial->DeviceProduct == 0x1414))
581 	{
582 	  ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX);
583 	}
584 
585 	return ret;
586 }
587 
xr_usb_serial_set_loopback(struct xr_usb_serial * xr_usb_serial,int channel)588 int xr_usb_serial_set_loopback(struct xr_usb_serial *xr_usb_serial, int channel)
589 {
590 	int ret = 0;
591 	xr_usb_serial_disable(xr_usb_serial);
592 
593 	if((xr_usb_serial->DeviceProduct == 0x1410) ||
594 	   (xr_usb_serial->DeviceProduct == 0x1412) ||
595 	   (xr_usb_serial->DeviceProduct == 0x1414))
596 	{
597 		switch (channel)
598 		{
599 			case 0:
600 				ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
601 		                            xr_usb_serial->reg_map.uart_loopback_addr,0x40);
602 				break;
603 			case 1:
604 				ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
605 		                            xr_usb_serial->reg_map.uart_loopback_addr,0x41);
606 				break;
607 			case 2:
608 				ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
609 		                            xr_usb_serial->reg_map.uart_loopback_addr,0x42);
610 				break;
611 			case 3:
612 				ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
613 		                            xr_usb_serial->reg_map.uart_loopback_addr,0x43);
614 				break;
615 			default:
616 				break;
617 		}
618 	}
619 	else if((xr_usb_serial->DeviceProduct == 0x1420)||
620 		(xr_usb_serial->DeviceProduct == 0x1422)||
621 		(xr_usb_serial->DeviceProduct == 0x1424))
622 	{
623 		ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
624 		                            xr_usb_serial->reg_map.uart_loopback_addr,0x07);
625 	}
626 	xr_usb_serial_enable(xr_usb_serial);
627 	return ret;
628 }
629 
630 #define XR21V1414_WIDE_MODE_OFFSET         3
631 #define XR21B142X_WIDE_MODE_TX_OFFSET     0x42
632 #define XR21B142X_WIDE_MODE_RX_OFFSET     0x45
633 /* XR2280x_FUNC_MGR_OFFSET will be included in xr_usb_serial_set_reg */
634 #define XR21B140X_WIDE_MODE_TX_OFFSET     0x22
635 #define XR21B140X_WIDE_MODE_RX_OFFSET     0x25
xr_usb_serial_set_wide_mode(struct xr_usb_serial * xr_usb_serial,int preciseflags)636 int xr_usb_serial_set_wide_mode(struct xr_usb_serial *xr_usb_serial, int preciseflags)
637 {
638 	int ret = 0;
639 	int channel = xr_usb_serial->channel;
640 	xr_usb_serial_disable(xr_usb_serial);
641 	if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
642 	{
643 		xr_usb_serial_set_reg(xr_usb_serial, XR21B140X_WIDE_MODE_TX_OFFSET, preciseflags);
644 		xr_usb_serial_set_reg(xr_usb_serial, XR21B140X_WIDE_MODE_RX_OFFSET, preciseflags);
645 	}
646 	else if((xr_usb_serial->DeviceProduct == 0x1410)||
647 		(xr_usb_serial->DeviceProduct == 0x1412)||
648 		(xr_usb_serial->DeviceProduct == 0x1414))
649 	{
650 		if(channel)  channel--;
651 		xr_usb_serial_set_reg_ext(xr_usb_serial, 0x66, channel*8 + XR21V1414_WIDE_MODE_OFFSET, preciseflags);
652 	}
653 	else if(xr_usb_serial->DeviceProduct == 0x1411)
654 	{
655 		xr_usb_serial_set_reg(xr_usb_serial,0xd02, preciseflags);
656 	}
657 	else if((xr_usb_serial->DeviceProduct == 0x1420)||
658 		(xr_usb_serial->DeviceProduct == 0x1422)||
659 		(xr_usb_serial->DeviceProduct == 0x1424))
660 	{
661 		xr_usb_serial_set_reg(xr_usb_serial, XR21B142X_WIDE_MODE_TX_OFFSET, preciseflags);
662 		xr_usb_serial_set_reg(xr_usb_serial, XR21B142X_WIDE_MODE_RX_OFFSET, preciseflags);
663 	}
664 	xr_usb_serial_enable(xr_usb_serial);
665 	return ret;
666 }
667 
xr_usb_serial_tiocmget(struct xr_usb_serial * xr_usb_serial)668 static int xr_usb_serial_tiocmget(struct xr_usb_serial *xr_usb_serial)
669 {
670 	short data;
671 	int result;
672 	result = xr_usb_serial_get_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_status_addr, &data);
673 	//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_tiocmget uart_gpio_status_addr:0x%04x\n",data);
674 	if (result)
675 		return ((data & 0x8) ? 0: TIOCM_DTR) | ((data & 0x20) ? 0:TIOCM_RTS ) | ((data & 0x4) ? 0:TIOCM_DSR) | ((data & 0x1) ? 0 : TIOCM_RI) | ((data & 0x2) ? 0:TIOCM_CD) | ((data & 0x10) ? 0 : TIOCM_CTS);
676 	else
677 		return -EFAULT;
678 }
679 
xr_usb_serial_tiocmset(struct xr_usb_serial * xr_usb_serial,unsigned int set,unsigned int clear)680 static int xr_usb_serial_tiocmset(struct xr_usb_serial *xr_usb_serial,
681 					unsigned int set, unsigned int clear)
682 {
683 	unsigned int newctrl = 0;
684 	newctrl = xr_usb_serial->ctrlout;
685 
686 	set = (set & TIOCM_DTR ? XR_USB_SERIAL_CTRL_DTR : 0) | (set & TIOCM_RTS ? XR_USB_SERIAL_CTRL_RTS : 0);
687 
688 	clear = (clear & TIOCM_DTR ? XR_USB_SERIAL_CTRL_DTR : 0) | (clear & TIOCM_RTS ? XR_USB_SERIAL_CTRL_RTS : 0);
689 
690 	newctrl = (newctrl & ~clear) | set;
691 
692 	if (xr_usb_serial->ctrlout == newctrl)
693 		return 0;
694 
695 	xr_usb_serial->ctrlout = newctrl;
696 
697 	if (newctrl & XR_USB_SERIAL_CTRL_DTR)
698 		xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x08);
699 	else
700 		xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, 0x08);
701 
702 	if (newctrl & XR_USB_SERIAL_CTRL_RTS)
703 		xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x20);
704 	else
705 		xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, 0x20);
706 
707 	return 0;
708 }
709 
710 static struct reg_addr_map xr21b140x_reg_map;
711 static struct reg_addr_map xr21b1411_reg_map;
712 static struct reg_addr_map xr21v141x_reg_map;
713 static struct reg_addr_map xr21b142x_reg_map;
714 
init_xr21b140x_reg_map(void)715 static void init_xr21b140x_reg_map(void)
716 {
717 	xr21b140x_reg_map.uart_enable_addr = 0x00;
718 	xr21b140x_reg_map.uart_format_addr = 0x05;
719 	xr21b140x_reg_map.uart_flow_addr = 0x06;
720 	xr21b140x_reg_map.uart_loopback_addr = 0x16;
721 	xr21b140x_reg_map.uart_xon_char_addr = 0x07;
722 	xr21b140x_reg_map.uart_xoff_char_addr = 0x08;
723 	xr21b140x_reg_map.uart_gpio_mode_addr = 0x0c;
724 	xr21b140x_reg_map.uart_gpio_dir_addr = 0x0d;
725 	xr21b140x_reg_map.uart_gpio_set_addr = 0x0e;
726 	xr21b140x_reg_map.uart_gpio_clr_addr = 0x0f;
727 	xr21b140x_reg_map.uart_gpio_status_addr = 0x10;
728 	xr21b140x_reg_map.tx_break_addr = 0x0a;
729 	xr21b140x_reg_map.uart_custom_driver = 0x41;
730 }
731 
init_xr21b1411_reg_map(void)732 static void init_xr21b1411_reg_map(void)
733 {
734 	xr21b1411_reg_map.uart_enable_addr = 0xc00;
735 	xr21b1411_reg_map.uart_flow_addr = 0xc06;
736 	xr21b1411_reg_map.uart_loopback_addr = 0xc16;
737 	xr21b1411_reg_map.uart_xon_char_addr = 0xc07;
738 	xr21b1411_reg_map.uart_xoff_char_addr = 0xc08;
739 	xr21b1411_reg_map.uart_gpio_mode_addr = 0xc0c;
740 	xr21b1411_reg_map.uart_gpio_dir_addr = 0xc0d;
741 	xr21b1411_reg_map.uart_gpio_set_addr = 0xc0e;
742 	xr21b1411_reg_map.uart_gpio_clr_addr = 0xc0f;
743 	xr21b1411_reg_map.uart_gpio_status_addr = 0xc10;
744 	xr21b1411_reg_map.tx_break_addr = 0xc0a;
745 	xr21b1411_reg_map.uart_custom_driver = 0x20d;
746 }
747 
init_xr21v141x_reg_map(void)748 static void init_xr21v141x_reg_map(void)
749 {
750 	xr21v141x_reg_map.uart_enable_addr = 0x03;
751 	xr21v141x_reg_map.uart_format_addr = 0x0b;
752 	xr21v141x_reg_map.uart_flow_addr = 0x0c;
753 	xr21v141x_reg_map.uart_loopback_addr = 0x12;
754 	xr21v141x_reg_map.uart_xon_char_addr = 0x10;
755 	xr21v141x_reg_map.uart_xoff_char_addr = 0x11;
756 	xr21v141x_reg_map.uart_gpio_mode_addr = 0x1a;
757 	xr21v141x_reg_map.uart_gpio_dir_addr = 0x1b;
758 	xr21v141x_reg_map.uart_gpio_set_addr = 0x1d;
759 	xr21v141x_reg_map.uart_gpio_clr_addr = 0x1e;
760 	xr21v141x_reg_map.uart_gpio_status_addr = 0x1f;
761 	xr21v141x_reg_map.tx_break_addr = 0x14;
762 }
763 
init_xr21b142x_reg_map(void)764 static void init_xr21b142x_reg_map(void)
765 {
766 	xr21b142x_reg_map.uart_enable_addr = 0x00;
767 	xr21b142x_reg_map.uart_flow_addr = 0x06;
768 	xr21b142x_reg_map.uart_loopback_addr = 0x16;
769 	xr21b142x_reg_map.uart_xon_char_addr = 0x07;
770 	xr21b142x_reg_map.uart_xoff_char_addr = 0x08;
771 	xr21b142x_reg_map.uart_gpio_mode_addr = 0x0c;
772 	xr21b142x_reg_map.uart_gpio_dir_addr = 0x0d;
773 	xr21b142x_reg_map.uart_gpio_set_addr = 0x0e;
774 	xr21b142x_reg_map.uart_gpio_clr_addr = 0x0f;
775 	xr21b142x_reg_map.uart_gpio_status_addr = 0x10;
776 	xr21b142x_reg_map.tx_break_addr = 0x0a;
777 	xr21b142x_reg_map.uart_custom_driver = 0x60;
778 	xr21b142x_reg_map.uart_low_latency = 0x46;
779 }
780 
xr_usb_serial_pre_setup(struct xr_usb_serial * xr_usb_serial)781 int xr_usb_serial_pre_setup(struct xr_usb_serial *xr_usb_serial)
782 {
783 	int ret = 0;
784 
785 	init_xr21b140x_reg_map();
786 	init_xr21b1411_reg_map();
787 	init_xr21v141x_reg_map();
788 	init_xr21b142x_reg_map();
789 	if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
790 	{
791 		memcpy(&(xr_usb_serial->reg_map),&xr21b140x_reg_map,sizeof(struct reg_addr_map));
792 	}
793 	else if(xr_usb_serial->DeviceProduct == 0x1411)
794 	{
795 		memcpy(&(xr_usb_serial->reg_map),&xr21b1411_reg_map,sizeof(struct reg_addr_map));
796 	}
797 	else if((xr_usb_serial->DeviceProduct == 0x1410)||
798 					(xr_usb_serial->DeviceProduct == 0x1412)||
799 					(xr_usb_serial->DeviceProduct == 0x1414))
800 	{
801 		memcpy(&(xr_usb_serial->reg_map),&xr21v141x_reg_map,sizeof(struct reg_addr_map));
802 	}
803 	else if((xr_usb_serial->DeviceProduct == 0x1420)||
804 					(xr_usb_serial->DeviceProduct == 0x1422)||
805 					(xr_usb_serial->DeviceProduct == 0x1424))
806 	{
807 		memcpy(&(xr_usb_serial->reg_map),&xr21b142x_reg_map,sizeof(struct reg_addr_map));
808 	}
809 	else
810 	{
811 		ret = -1;
812 	}
813 	if(xr_usb_serial->reg_map.uart_custom_driver)
814 		xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_custom_driver, 1);
815 
816 	xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr, 0);
817 	xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, 0x28);
818 	xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, UART_GPIO_SET_DTR | UART_GPIO_SET_RTS);
819 
820 	return ret;
821 }
822