1/*
2 * IPWireless 3G UMTS TDD Modem driver (USB connected)
3 *
4 *   Copyright (C) 2004 Roelf Diedericks <roelfd@inet.co.za>
5 *   Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 *
7 *   This program is free software; you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation; either version 2 of the License, or
10 *   (at your option) any later version.
11 *
12 * All information about the device was acquired using SnoopyPro
13 * on MSFT's O/S, and examing the MSFT drivers' debug output
14 * (insanely left _on_ in the enduser version)
15 *
16 * It was written out of frustration with the IPWireless USB modem
17 * supplied by Axity3G/Sentech South Africa not supporting
18 * Linux whatsoever.
19 *
20 * Nobody provided any proprietary information that was not already
21 * available for this device.
22 *
23 * The modem adheres to the "3GPP TS  27.007 AT command set for 3G
24 * User Equipment (UE)" standard, available from
25 * http://www.3gpp.org/ftp/Specs/html-info/27007.htm
26 *
27 * The code was only tested the IPWireless handheld modem distributed
28 * in South Africa by Sentech.
29 *
30 * It may work for Woosh Inc in .nz too, as it appears they use the
31 * same kit.
32 *
33 * There is still some work to be done in terms of handling
34 * DCD, DTR, RTS, CTS which are currently faked.
35 * It's good enough for PPP at this point. It's based off all kinds of
36 * code found in usb/serial and usb/class
37 *
38 */
39
40#include <linux/kernel.h>
41#include <linux/errno.h>
42#include <linux/init.h>
43#include <linux/slab.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <linux/module.h>
47#include <linux/spinlock.h>
48#include <linux/usb.h>
49#include <linux/usb/serial.h>
50#include <asm/uaccess.h>
51
52/*
53 * Version Information
54 */
55#define DRIVER_VERSION	"v0.3"
56#define DRIVER_AUTHOR	"Roelf Diedericks"
57#define DRIVER_DESC	"IPWireless tty driver"
58
59#define IPW_TTY_MAJOR	240	/* real device node major id, experimental range */
60#define IPW_TTY_MINORS	256	/* we support 256 devices, dunno why, it'd be insane :) */
61
62#define USB_IPW_MAGIC	0x6d02	/* magic number for ipw struct */
63
64
65/* Message sizes */
66#define EVENT_BUFFER_SIZE	0xFF
67#define CHAR2INT16(c1,c0)	(((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
68#define NUM_BULK_URBS		24
69#define NUM_CONTROL_URBS	16
70
71/* vendor/product pairs that are known work with this driver*/
72#define IPW_VID		0x0bc3
73#define IPW_PID		0x0001
74
75
76/* Vendor commands: */
77
78/* baud rates */
79enum {
80	ipw_sio_b256000 = 0x000e,
81	ipw_sio_b128000 = 0x001d,
82	ipw_sio_b115200 = 0x0020,
83	ipw_sio_b57600  = 0x0040,
84	ipw_sio_b56000  = 0x0042,
85	ipw_sio_b38400  = 0x0060,
86	ipw_sio_b19200  = 0x00c0,
87	ipw_sio_b14400  = 0x0100,
88	ipw_sio_b9600   = 0x0180,
89	ipw_sio_b4800   = 0x0300,
90	ipw_sio_b2400   = 0x0600,
91	ipw_sio_b1200   = 0x0c00,
92	ipw_sio_b600    = 0x1800
93};
94
95/* data bits */
96#define ipw_dtb_7		0x700
97#define ipw_dtb_8		0x810	// ok so the define is misleading, I know, but forces 8,n,1
98					// I mean, is there a point to any other setting these days? :)
99
100/* usb control request types : */
101#define IPW_SIO_RXCTL		0x00	// control bulk rx channel transmissions, value=1/0 (on/off)
102#define IPW_SIO_SET_BAUD	0x01	// set baud, value=requested ipw_sio_bxxxx
103#define IPW_SIO_SET_LINE	0x03	// set databits, parity. value=ipw_dtb_x
104#define IPW_SIO_SET_PIN		0x03	// set/clear dtr/rts value=ipw_pin_xxx
105#define IPW_SIO_POLL		0x08	// get serial port status byte, call with value=0
106#define IPW_SIO_INIT		0x11	// initializes ? value=0 (appears as first thing todo on open)
107#define IPW_SIO_PURGE		0x12	// purge all transmissions?, call with value=numchar_to_purge
108#define IPW_SIO_HANDFLOW	0x13	// set xon/xoff limits value=0, and a buffer of 0x10 bytes
109#define IPW_SIO_SETCHARS	0x13	// set the flowcontrol special chars, value=0, buf=6 bytes,
110					// last 2 bytes contain flowcontrol chars e.g. 00 00 00 00 11 13
111
112/* values used for request IPW_SIO_SET_PIN */
113#define IPW_PIN_SETDTR		0x101
114#define IPW_PIN_SETRTS		0x202
115#define IPW_PIN_CLRDTR		0x100
116#define IPW_PIN_CLRRTS		0x200 // unconfirmed
117
118/* values used for request IPW_SIO_RXCTL */
119#define IPW_RXBULK_ON		1
120#define IPW_RXBULK_OFF		0
121
122/* various 16 byte hardcoded transferbuffers used by flow control */
123#define IPW_BYTES_FLOWINIT	{ 0x01, 0, 0, 0, 0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
124
125/* Interpretation of modem status lines */
126#define IPW_DSR			((1<<4) | (1<<5))
127#define IPW_CTS			((1<<5) | (1<<4))
128
129#define IPW_WANTS_TO_SEND	0x30
130//#define IPW_DTR			/* Data Terminal Ready */
131//#define IPW_CTS			/* Clear To Send */
132//#define IPW_CD			/* Carrier Detect */
133//#define IPW_DSR			/* Data Set Ready */
134//#define IPW_RxD			/* Receive pin */
135
136//#define IPW_LE
137//#define IPW_RTS
138//#define IPW_ST
139//#define IPW_SR
140//#define IPW_RI			/* Ring Indicator */
141
142static struct usb_device_id usb_ipw_ids[] = {
143	{ USB_DEVICE(IPW_VID, IPW_PID) },
144	{ },
145};
146
147MODULE_DEVICE_TABLE(usb, usb_ipw_ids);
148
149static struct usb_driver usb_ipw_driver = {
150	.name =		"ipwtty",
151	.probe =	usb_serial_probe,
152	.disconnect =	usb_serial_disconnect,
153	.id_table =	usb_ipw_ids,
154	.no_dynamic_id = 	1,
155};
156
157static int debug;
158
159static void ipw_read_bulk_callback(struct urb *urb)
160{
161	struct usb_serial_port *port = urb->context;
162	unsigned char *data = urb->transfer_buffer;
163	struct tty_struct *tty;
164	int result;
165
166	dbg("%s - port %d", __FUNCTION__, port->number);
167
168	if (urb->status) {
169		dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
170		return;
171	}
172
173	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
174
175	tty = port->tty;
176	if (tty && urb->actual_length) {
177		tty_buffer_request_room(tty, urb->actual_length);
178		tty_insert_flip_string(tty, data, urb->actual_length);
179		tty_flip_buffer_push(tty);
180	}
181
182	/* Continue trying to always read  */
183	usb_fill_bulk_urb (port->read_urb, port->serial->dev,
184			   usb_rcvbulkpipe(port->serial->dev,
185					   port->bulk_in_endpointAddress),
186			   port->read_urb->transfer_buffer,
187			   port->read_urb->transfer_buffer_length,
188			   ipw_read_bulk_callback, port);
189	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
190	if (result)
191		dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
192	return;
193}
194
195static int ipw_open(struct usb_serial_port *port, struct file *filp)
196{
197	struct usb_device *dev = port->serial->dev;
198	u8 buf_flow_static[16] = IPW_BYTES_FLOWINIT;
199	u8 *buf_flow_init;
200	int result;
201
202	dbg("%s", __FUNCTION__);
203
204	buf_flow_init = kmemdup(buf_flow_static, 16, GFP_KERNEL);
205	if (!buf_flow_init)
206		return -ENOMEM;
207
208	if (port->tty)
209		port->tty->low_latency = 1;
210
211	/* --1: Tell the modem to initialize (we think) From sniffs this is always the
212	 * first thing that gets sent to the modem during opening of the device */
213	dbg("%s: Sending SIO_INIT (we guess)",__FUNCTION__);
214	result = usb_control_msg(dev, usb_sndctrlpipe(dev,0),
215				 IPW_SIO_INIT,
216				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
217				 0,
218				 0, /* index */
219				 NULL,
220				 0,
221				 100000);
222	if (result < 0)
223		dev_err(&port->dev, "Init of modem failed (error = %d)", result);
224
225	/* reset the bulk pipes */
226	usb_clear_halt(dev, usb_rcvbulkpipe(dev, port->bulk_in_endpointAddress));
227	usb_clear_halt(dev, usb_sndbulkpipe(dev, port->bulk_out_endpointAddress));
228
229	/*--2: Start reading from the device */
230	dbg("%s: setting up bulk read callback",__FUNCTION__);
231	usb_fill_bulk_urb(port->read_urb, dev,
232			  usb_rcvbulkpipe(dev, port->bulk_in_endpointAddress),
233			  port->bulk_in_buffer,
234			  port->bulk_in_size,
235			  ipw_read_bulk_callback, port);
236	result = usb_submit_urb(port->read_urb, GFP_KERNEL);
237	if (result < 0)
238		dbg("%s - usb_submit_urb(read bulk) failed with status %d", __FUNCTION__, result);
239
240	/*--3: Tell the modem to open the floodgates on the rx bulk channel */
241	dbg("%s:asking modem for RxRead (RXBULK_ON)",__FUNCTION__);
242	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
243				 IPW_SIO_RXCTL,
244				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
245				 IPW_RXBULK_ON,
246				 0, /* index */
247				 NULL,
248				 0,
249				 100000);
250	if (result < 0)
251		dev_err(&port->dev, "Enabling bulk RxRead failed (error = %d)", result);
252
253	/*--4: setup the initial flowcontrol */
254	dbg("%s:setting init flowcontrol (%s)",__FUNCTION__,buf_flow_init);
255	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
256				 IPW_SIO_HANDFLOW,
257				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
258				 0,
259				 0,
260				 buf_flow_init,
261				 0x10,
262				 200000);
263	if (result < 0)
264		dev_err(&port->dev, "initial flowcontrol failed (error = %d)", result);
265
266
267	/*--5: raise the dtr */
268	dbg("%s:raising dtr",__FUNCTION__);
269	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
270				 IPW_SIO_SET_PIN,
271				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
272				 IPW_PIN_SETDTR,
273				 0,
274				 NULL,
275				 0,
276				 200000);
277	if (result < 0)
278		dev_err(&port->dev, "setting dtr failed (error = %d)", result);
279
280	/*--6: raise the rts */
281	dbg("%s:raising rts",__FUNCTION__);
282	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
283				 IPW_SIO_SET_PIN,
284				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
285				 IPW_PIN_SETRTS,
286				 0,
287				 NULL,
288				 0,
289				 200000);
290	if (result < 0)
291		dev_err(&port->dev, "setting dtr failed (error = %d)", result);
292
293	kfree(buf_flow_init);
294	return 0;
295}
296
297static void ipw_close(struct usb_serial_port *port, struct file * filp)
298{
299	struct usb_device *dev = port->serial->dev;
300	int result;
301
302	if (tty_hung_up_p(filp)) {
303		dbg("%s: tty_hung_up_p ...", __FUNCTION__);
304		return;
305	}
306
307	/*--1: drop the dtr */
308	dbg("%s:dropping dtr",__FUNCTION__);
309	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
310				 IPW_SIO_SET_PIN,
311				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
312				 IPW_PIN_CLRDTR,
313				 0,
314				 NULL,
315				 0,
316				 200000);
317	if (result < 0)
318		dev_err(&port->dev, "dropping dtr failed (error = %d)", result);
319
320	/*--2: drop the rts */
321	dbg("%s:dropping rts",__FUNCTION__);
322	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
323				 IPW_SIO_SET_PIN, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
324				 IPW_PIN_CLRRTS,
325				 0,
326				 NULL,
327				 0,
328				 200000);
329	if (result < 0)
330		dev_err(&port->dev, "dropping rts failed (error = %d)", result);
331
332
333	/*--3: purge */
334	dbg("%s:sending purge",__FUNCTION__);
335	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
336				 IPW_SIO_PURGE, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
337				 0x03,
338				 0,
339				 NULL,
340				 0,
341				 200000);
342	if (result < 0)
343		dev_err(&port->dev, "purge failed (error = %d)", result);
344
345
346	/* send RXBULK_off (tell modem to stop transmitting bulk data on rx chan) */
347	result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
348				 IPW_SIO_RXCTL,
349				 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
350				 IPW_RXBULK_OFF,
351				 0, /* index */
352				 NULL,
353				 0,
354				 100000);
355
356	if (result < 0)
357		dev_err(&port->dev, "Disabling bulk RxRead failed (error = %d)", result);
358
359	/* shutdown any in-flight urbs that we know about */
360	usb_kill_urb(port->read_urb);
361	usb_kill_urb(port->write_urb);
362}
363
364static void ipw_write_bulk_callback(struct urb *urb)
365{
366	struct usb_serial_port *port = urb->context;
367
368	dbg("%s", __FUNCTION__);
369
370	port->write_urb_busy = 0;
371
372	if (urb->status)
373		dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
374
375	usb_serial_port_softint(port);
376}
377
378static int ipw_write(struct usb_serial_port *port, const unsigned char *buf, int count)
379{
380	struct usb_device *dev = port->serial->dev;
381	int ret;
382
383	dbg("%s: TOP: count=%d, in_interrupt=%ld", __FUNCTION__,
384		count, in_interrupt() );
385
386	if (count == 0) {
387		dbg("%s - write request of 0 bytes", __FUNCTION__);
388		return 0;
389	}
390
391	spin_lock_bh(&port->lock);
392	if (port->write_urb_busy) {
393		spin_unlock_bh(&port->lock);
394		dbg("%s - already writing", __FUNCTION__);
395		return 0;
396	}
397	port->write_urb_busy = 1;
398	spin_unlock_bh(&port->lock);
399
400	count = min(count, port->bulk_out_size);
401	memcpy(port->bulk_out_buffer, buf, count);
402
403	dbg("%s count now:%d", __FUNCTION__, count);
404
405	usb_fill_bulk_urb(port->write_urb, dev,
406			  usb_sndbulkpipe(dev, port->bulk_out_endpointAddress),
407			  port->write_urb->transfer_buffer,
408			  count,
409			  ipw_write_bulk_callback,
410			  port);
411
412	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
413	if (ret != 0) {
414		port->write_urb_busy = 0;
415		dbg("%s - usb_submit_urb(write bulk) failed with error = %d", __FUNCTION__, ret);
416		return ret;
417	}
418
419	dbg("%s returning %d", __FUNCTION__, count);
420	return count;
421}
422
423static int ipw_probe(struct usb_serial_port *port)
424{
425	return 0;
426}
427
428static int ipw_disconnect(struct usb_serial_port *port)
429{
430	usb_set_serial_port_data(port, NULL);
431	return 0;
432}
433
434static struct usb_serial_driver ipw_device = {
435	.driver = {
436		.owner =	THIS_MODULE,
437		.name =		"ipw",
438	},
439	.description =		"IPWireless converter",
440	.usb_driver = 		&usb_ipw_driver,
441	.id_table =		usb_ipw_ids,
442	.num_interrupt_in =	NUM_DONT_CARE,
443	.num_bulk_in =		1,
444	.num_bulk_out =		1,
445	.num_ports =		1,
446	.open =			ipw_open,
447	.close =		ipw_close,
448	.port_probe = 		ipw_probe,
449	.port_remove =		ipw_disconnect,
450	.write =		ipw_write,
451	.write_bulk_callback =	ipw_write_bulk_callback,
452	.read_bulk_callback =	ipw_read_bulk_callback,
453};
454
455
456
457static int usb_ipw_init(void)
458{
459	int retval;
460
461	retval = usb_serial_register(&ipw_device);
462	if (retval)
463		return retval;
464	retval = usb_register(&usb_ipw_driver);
465	if (retval) {
466		usb_serial_deregister(&ipw_device);
467		return retval;
468	}
469	info(DRIVER_DESC " " DRIVER_VERSION);
470	return 0;
471}
472
473static void usb_ipw_exit(void)
474{
475	usb_deregister(&usb_ipw_driver);
476	usb_serial_deregister(&ipw_device);
477}
478
479module_init(usb_ipw_init);
480module_exit(usb_ipw_exit);
481
482/* Module information */
483MODULE_AUTHOR( DRIVER_AUTHOR );
484MODULE_DESCRIPTION( DRIVER_DESC );
485MODULE_LICENSE("GPL");
486
487module_param(debug, bool, S_IRUGO | S_IWUSR);
488MODULE_PARM_DESC(debug, "Debug enabled or not");
489