xref: /linux/drivers/usb/serial/ipw.c (revision 1e578ec51cace9dcef961225b72b8da46ade13ae)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * IPWireless 3G UMTS TDD Modem driver (USB connected)
4  *
5  *   Copyright (C) 2004 Roelf Diedericks <roelfd@inet.co.za>
6  *   Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  * All information about the device was acquired using SnoopyPro
9  * on MSFT's O/S, and examing the MSFT drivers' debug output
10  * (insanely left _on_ in the enduser version)
11  *
12  * It was written out of frustration with the IPWireless USB modem
13  * supplied by Axity3G/Sentech South Africa not supporting
14  * Linux whatsoever.
15  *
16  * Nobody provided any proprietary information that was not already
17  * available for this device.
18  *
19  * The modem adheres to the "3GPP TS  27.007 AT command set for 3G
20  * User Equipment (UE)" standard, available from
21  * http://www.3gpp.org/ftp/Specs/html-info/27007.htm
22  *
23  * The code was only tested the IPWireless handheld modem distributed
24  * in South Africa by Sentech.
25  *
26  * It may work for Woosh Inc in .nz too, as it appears they use the
27  * same kit.
28  *
29  * There is still some work to be done in terms of handling
30  * DCD, DTR, RTS, CTS which are currently faked.
31  * It's good enough for PPP at this point. It's based off all kinds of
32  * code found in usb/serial and usb/class
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/slab.h>
38 #include <linux/tty.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/usb.h>
42 #include <linux/usb/serial.h>
43 #include "usb-wwan.h"
44 
45 #define DRIVER_AUTHOR	"Roelf Diedericks"
46 #define DRIVER_DESC	"IPWireless tty driver"
47 
48 #define IPW_TTY_MAJOR	240	/* real device node major id, experimental range */
49 #define IPW_TTY_MINORS	256	/* we support 256 devices, dunno why, it'd be insane :) */
50 
51 #define USB_IPW_MAGIC	0x6d02	/* magic number for ipw struct */
52 
53 
54 /* Message sizes */
55 #define EVENT_BUFFER_SIZE	0xFF
56 #define CHAR2INT16(c1, c0)	(((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
57 
58 /* vendor/product pairs that are known work with this driver*/
59 #define IPW_VID		0x0bc3
60 #define IPW_PID		0x0001
61 
62 
63 /* Vendor commands: */
64 
65 /* baud rates */
66 enum {
67 	ipw_sio_b256000 = 0x000e,
68 	ipw_sio_b128000 = 0x001d,
69 	ipw_sio_b115200 = 0x0020,
70 	ipw_sio_b57600  = 0x0040,
71 	ipw_sio_b56000  = 0x0042,
72 	ipw_sio_b38400  = 0x0060,
73 	ipw_sio_b19200  = 0x00c0,
74 	ipw_sio_b14400  = 0x0100,
75 	ipw_sio_b9600   = 0x0180,
76 	ipw_sio_b4800   = 0x0300,
77 	ipw_sio_b2400   = 0x0600,
78 	ipw_sio_b1200   = 0x0c00,
79 	ipw_sio_b600    = 0x1800
80 };
81 
82 /* data bits */
83 #define ipw_dtb_7		0x700
84 #define ipw_dtb_8		0x810	/* ok so the define is misleading, I know, but forces 8,n,1 */
85 					/* I mean, is there a point to any other setting these days? :) */
86 
87 /* usb control request types : */
88 #define IPW_SIO_RXCTL		0x00	/* control bulk rx channel transmissions, value=1/0 (on/off) */
89 #define IPW_SIO_SET_BAUD	0x01	/* set baud, value=requested ipw_sio_bxxxx */
90 #define IPW_SIO_SET_LINE	0x03	/* set databits, parity. value=ipw_dtb_x */
91 #define IPW_SIO_SET_PIN		0x03	/* set/clear dtr/rts value=ipw_pin_xxx */
92 #define IPW_SIO_POLL		0x08	/* get serial port status byte, call with value=0 */
93 #define IPW_SIO_INIT		0x11	/* initializes ? value=0 (appears as first thing todo on open) */
94 #define IPW_SIO_PURGE		0x12	/* purge all transmissions?, call with value=numchar_to_purge */
95 #define IPW_SIO_HANDFLOW	0x13	/* set xon/xoff limits value=0, and a buffer of 0x10 bytes */
96 #define IPW_SIO_SETCHARS	0x13	/* set the flowcontrol special chars, value=0, buf=6 bytes, */
97 					/* last 2 bytes contain flowcontrol chars e.g. 00 00 00 00 11 13 */
98 
99 /* values used for request IPW_SIO_SET_PIN */
100 #define IPW_PIN_SETDTR		0x101
101 #define IPW_PIN_SETRTS		0x202
102 #define IPW_PIN_CLRDTR		0x100
103 #define IPW_PIN_CLRRTS		0x200 /* unconfirmed */
104 
105 /* values used for request IPW_SIO_RXCTL */
106 #define IPW_RXBULK_ON		1
107 #define IPW_RXBULK_OFF		0
108 
109 /* various 16 byte hardcoded transferbuffers used by flow control */
110 #define IPW_BYTES_FLOWINIT	{ 0x01, 0, 0, 0, 0x40, 0, 0, 0, \
111 					0, 0, 0, 0, 0, 0, 0, 0 }
112 
113 /* Interpretation of modem status lines */
114 /* These need sorting out by individually connecting pins and checking
115  * results. FIXME!
116  * When data is being sent we see 0x30 in the lower byte; this must
117  * contain DSR and CTS ...
118  */
119 #define IPW_DSR			((1<<4) | (1<<5))
120 #define IPW_CTS			((1<<5) | (1<<4))
121 
122 #define IPW_WANTS_TO_SEND	0x30
123 
124 static const struct usb_device_id id_table[] = {
125 	{ USB_DEVICE(IPW_VID, IPW_PID) },
126 	{ },
127 };
128 MODULE_DEVICE_TABLE(usb, id_table);
129 
130 static int ipw_open(struct tty_struct *tty, struct usb_serial_port *port)
131 {
132 	struct usb_device *udev = port->serial->dev;
133 	struct device *dev = &port->dev;
134 	u8 buf_flow_static[16] = IPW_BYTES_FLOWINIT;
135 	u8 *buf_flow_init;
136 	int result;
137 
138 	buf_flow_init = kmemdup(buf_flow_static, 16, GFP_KERNEL);
139 	if (!buf_flow_init)
140 		return -ENOMEM;
141 
142 	/* --1: Tell the modem to initialize (we think) From sniffs this is
143 	 *	always the first thing that gets sent to the modem during
144 	 *	opening of the device */
145 	dev_dbg(dev, "%s: Sending SIO_INIT (we guess)\n", __func__);
146 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
147 			 IPW_SIO_INIT,
148 			 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
149 			 0,
150 			 0, /* index */
151 			 NULL,
152 			 0,
153 			 100000);
154 	if (result < 0)
155 		dev_err(dev, "Init of modem failed (error = %d)\n", result);
156 
157 	/* reset the bulk pipes */
158 	usb_clear_halt(udev, usb_rcvbulkpipe(udev, port->bulk_in_endpointAddress));
159 	usb_clear_halt(udev, usb_sndbulkpipe(udev, port->bulk_out_endpointAddress));
160 
161 	/*--2: Start reading from the device */
162 	dev_dbg(dev, "%s: setting up bulk read callback\n", __func__);
163 	usb_wwan_open(tty, port);
164 
165 	/*--3: Tell the modem to open the floodgates on the rx bulk channel */
166 	dev_dbg(dev, "%s:asking modem for RxRead (RXBULK_ON)\n", __func__);
167 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
168 			 IPW_SIO_RXCTL,
169 			 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
170 			 IPW_RXBULK_ON,
171 			 0, /* index */
172 			 NULL,
173 			 0,
174 			 100000);
175 	if (result < 0)
176 		dev_err(dev, "Enabling bulk RxRead failed (error = %d)\n", result);
177 
178 	/*--4: setup the initial flowcontrol */
179 	dev_dbg(dev, "%s:setting init flowcontrol (%s)\n", __func__, buf_flow_init);
180 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
181 			 IPW_SIO_HANDFLOW,
182 			 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
183 			 0,
184 			 0,
185 			 buf_flow_init,
186 			 0x10,
187 			 200000);
188 	if (result < 0)
189 		dev_err(dev, "initial flowcontrol failed (error = %d)\n", result);
190 
191 	kfree(buf_flow_init);
192 	return 0;
193 }
194 
195 static int ipw_attach(struct usb_serial *serial)
196 {
197 	struct usb_wwan_intf_private *data;
198 
199 	data = kzalloc_obj(struct usb_wwan_intf_private);
200 	if (!data)
201 		return -ENOMEM;
202 
203 	spin_lock_init(&data->susp_lock);
204 	usb_set_serial_data(serial, data);
205 	return 0;
206 }
207 
208 static void ipw_release(struct usb_serial *serial)
209 {
210 	struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
211 
212 	usb_set_serial_data(serial, NULL);
213 	kfree(data);
214 }
215 
216 static void ipw_dtr_rts(struct usb_serial_port *port, int on)
217 {
218 	struct usb_device *udev = port->serial->dev;
219 	struct device *dev = &port->dev;
220 	int result;
221 
222 	dev_dbg(dev, "%s: on = %d\n", __func__, on);
223 
224 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
225 			 IPW_SIO_SET_PIN,
226 			 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
227 			 on ? IPW_PIN_SETDTR : IPW_PIN_CLRDTR,
228 			 0,
229 			 NULL,
230 			 0,
231 			 200000);
232 	if (result < 0)
233 		dev_err(dev, "setting dtr failed (error = %d)\n", result);
234 
235 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
236 			 IPW_SIO_SET_PIN, USB_TYPE_VENDOR |
237 					USB_RECIP_INTERFACE | USB_DIR_OUT,
238 			 on ? IPW_PIN_SETRTS : IPW_PIN_CLRRTS,
239 			 0,
240 			 NULL,
241 			 0,
242 			 200000);
243 	if (result < 0)
244 		dev_err(dev, "setting rts failed (error = %d)\n", result);
245 }
246 
247 static void ipw_close(struct usb_serial_port *port)
248 {
249 	struct usb_device *udev = port->serial->dev;
250 	struct device *dev = &port->dev;
251 	int result;
252 
253 	/*--3: purge */
254 	dev_dbg(dev, "%s:sending purge\n", __func__);
255 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
256 			 IPW_SIO_PURGE, USB_TYPE_VENDOR |
257 			 		USB_RECIP_INTERFACE | USB_DIR_OUT,
258 			 0x03,
259 			 0,
260 			 NULL,
261 			 0,
262 			 200000);
263 	if (result < 0)
264 		dev_err(dev, "purge failed (error = %d)\n", result);
265 
266 
267 	/* send RXBULK_off (tell modem to stop transmitting bulk data on
268 	   rx chan) */
269 	result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
270 			 IPW_SIO_RXCTL,
271 			 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
272 			 IPW_RXBULK_OFF,
273 			 0, /* index */
274 			 NULL,
275 			 0,
276 			 100000);
277 
278 	if (result < 0)
279 		dev_err(dev, "Disabling bulk RxRead failed (error = %d)\n", result);
280 
281 	usb_wwan_close(port);
282 }
283 
284 static struct usb_serial_driver ipw_device = {
285 	.driver = {
286 		.name =		"ipw",
287 	},
288 	.description =		"IPWireless converter",
289 	.id_table =		id_table,
290 	.num_ports =		1,
291 	.open =			ipw_open,
292 	.close =		ipw_close,
293 	.attach =		ipw_attach,
294 	.release =		ipw_release,
295 	.port_probe =		usb_wwan_port_probe,
296 	.port_remove =		usb_wwan_port_remove,
297 	.dtr_rts =		ipw_dtr_rts,
298 	.write =		usb_wwan_write,
299 };
300 
301 static struct usb_serial_driver * const serial_drivers[] = {
302 	&ipw_device, NULL
303 };
304 
305 module_usb_serial_driver(serial_drivers, id_table);
306 
307 /* Module information */
308 MODULE_AUTHOR(DRIVER_AUTHOR);
309 MODULE_DESCRIPTION(DRIVER_DESC);
310 MODULE_LICENSE("GPL");
311