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