| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * USB ZyXEL omni.net LCD PLUS driver | 
 | 3 |  * | 
 | 4 |  *	This program is free software; you can redistribute it and/or modify | 
 | 5 |  *	it under the terms of the GNU General Public License as published by | 
 | 6 |  *	the Free Software Foundation; either version 2 of the License, or | 
 | 7 |  *	(at your option) any later version. | 
 | 8 |  * | 
 | 9 |  * See Documentation/usb/usb-serial.txt for more information on using this driver | 
 | 10 |  * | 
 | 11 |  * Please report both successes and troubles to the author at omninet@kroah.com | 
 | 12 |  *  | 
 | 13 |  * (05/30/2001) gkh | 
 | 14 |  *	switched from using spinlock to a semaphore, which fixes lots of problems. | 
 | 15 |  * | 
 | 16 |  * (04/08/2001) gb | 
 | 17 |  *	Identify version on module load. | 
 | 18 |  * | 
 | 19 |  * (11/01/2000) Adam J. Richter | 
 | 20 |  *	usb_device_id table support | 
 | 21 |  *  | 
 | 22 |  * (10/05/2000) gkh | 
 | 23 |  *	Fixed bug with urb->dev not being set properly, now that the usb | 
 | 24 |  *	core needs it. | 
 | 25 |  *  | 
 | 26 |  * (08/28/2000) gkh | 
 | 27 |  *	Added locks for SMP safeness. | 
 | 28 |  *	Fixed MOD_INC and MOD_DEC logic and the ability to open a port more  | 
 | 29 |  *	than once. | 
 | 30 |  *	Fixed potential race in omninet_write_bulk_callback | 
 | 31 |  * | 
 | 32 |  * (07/19/2000) gkh | 
 | 33 |  *	Added module_init and module_exit functions to handle the fact that this | 
 | 34 |  *	driver is a loadable module now. | 
 | 35 |  * | 
 | 36 |  */ | 
 | 37 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/kernel.h> | 
 | 39 | #include <linux/errno.h> | 
 | 40 | #include <linux/init.h> | 
 | 41 | #include <linux/slab.h> | 
 | 42 | #include <linux/tty.h> | 
 | 43 | #include <linux/tty_driver.h> | 
 | 44 | #include <linux/tty_flip.h> | 
 | 45 | #include <linux/module.h> | 
 | 46 | #include <linux/spinlock.h> | 
 | 47 | #include <asm/uaccess.h> | 
 | 48 | #include <linux/usb.h> | 
| Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 49 | #include <linux/usb/serial.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
 | 51 | static int debug; | 
 | 52 |  | 
 | 53 | /* | 
 | 54 |  * Version Information | 
 | 55 |  */ | 
 | 56 | #define DRIVER_VERSION "v1.1" | 
 | 57 | #define DRIVER_AUTHOR "Alessandro Zummo" | 
 | 58 | #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver" | 
 | 59 |  | 
 | 60 | #define ZYXEL_VENDOR_ID		0x0586 | 
 | 61 | #define ZYXEL_OMNINET_ID	0x1000 | 
 | 62 | #define BT_IGNITIONPRO_ID	0x2000  /* This one seems to be a re-branded ZyXEL device */ | 
 | 63 |  | 
 | 64 | /* function prototypes */ | 
 | 65 | static int  omninet_open		(struct usb_serial_port *port, struct file *filp); | 
 | 66 | static void omninet_close		(struct usb_serial_port *port, struct file *filp); | 
 | 67 | static void omninet_read_bulk_callback	(struct urb *urb, struct pt_regs *regs); | 
 | 68 | static void omninet_write_bulk_callback	(struct urb *urb, struct pt_regs *regs); | 
 | 69 | static int  omninet_write		(struct usb_serial_port *port, const unsigned char *buf, int count); | 
 | 70 | static int  omninet_write_room		(struct usb_serial_port *port); | 
 | 71 | static void omninet_shutdown		(struct usb_serial *serial); | 
 | 72 |  | 
 | 73 | static struct usb_device_id id_table [] = { | 
 | 74 | 	{ USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) }, | 
 | 75 | 	{ USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) }, | 
 | 76 | 	{ }						/* Terminating entry */ | 
 | 77 | }; | 
 | 78 |  | 
 | 79 | MODULE_DEVICE_TABLE (usb, id_table); | 
 | 80 |  | 
 | 81 | static struct usb_driver omninet_driver = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | 	.name =		"omninet", | 
 | 83 | 	.probe =	usb_serial_probe, | 
 | 84 | 	.disconnect =	usb_serial_disconnect, | 
 | 85 | 	.id_table =	id_table, | 
| Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 86 | 	.no_dynamic_id = 	1, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | }; | 
 | 88 |  | 
 | 89 |  | 
| Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 90 | static struct usb_serial_driver zyxel_omninet_device = { | 
| Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 91 | 	.driver = { | 
 | 92 | 		.owner =	THIS_MODULE, | 
| Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 93 | 		.name =		"omninet", | 
| Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 94 | 	}, | 
| Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 95 | 	.description =		"ZyXEL - omni.net lcd plus usb", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | 	.id_table =		id_table, | 
 | 97 | 	.num_interrupt_in =	1, | 
 | 98 | 	.num_bulk_in =		1, | 
 | 99 | 	.num_bulk_out =		2, | 
 | 100 | 	.num_ports =		1, | 
 | 101 | 	.open =			omninet_open, | 
 | 102 | 	.close =		omninet_close, | 
 | 103 | 	.write =		omninet_write, | 
 | 104 | 	.write_room =		omninet_write_room, | 
 | 105 | 	.read_bulk_callback =	omninet_read_bulk_callback, | 
 | 106 | 	.write_bulk_callback =	omninet_write_bulk_callback, | 
 | 107 | 	.shutdown =		omninet_shutdown, | 
 | 108 | }; | 
 | 109 |  | 
 | 110 |  | 
 | 111 | /* The protocol. | 
 | 112 |  * | 
 | 113 |  * The omni.net always exchange 64 bytes of data with the host. The first | 
 | 114 |  * four bytes are the control header, you can see it in the above structure. | 
 | 115 |  * | 
 | 116 |  * oh_seq is a sequence number. Don't know if/how it's used. | 
 | 117 |  * oh_len is the length of the data bytes in the packet. | 
 | 118 |  * oh_xxx Bit-mapped, related to handshaking and status info. | 
 | 119 |  *	I normally set it to 0x03 in trasmitted frames. | 
 | 120 |  *	7: Active when the TA is in a CONNECTed state. | 
 | 121 |  *	6: unknown | 
 | 122 |  *	5: handshaking, unknown | 
 | 123 |  *	4: handshaking, unknown | 
 | 124 |  *	3: unknown, usually 0 | 
 | 125 |  *	2: unknown, usually 0 | 
 | 126 |  *	1: handshaking, unknown, usually set to 1 in trasmitted frames | 
 | 127 |  *	0: handshaking, unknown, usually set to 1 in trasmitted frames | 
 | 128 |  * oh_pad Probably a pad byte. | 
 | 129 |  * | 
 | 130 |  * After the header you will find data bytes if oh_len was greater than zero. | 
 | 131 |  * | 
 | 132 |  */ | 
 | 133 |  | 
 | 134 | struct omninet_header | 
 | 135 | { | 
 | 136 | 	__u8	oh_seq; | 
 | 137 | 	__u8	oh_len; | 
 | 138 | 	__u8	oh_xxx; | 
 | 139 | 	__u8	oh_pad; | 
 | 140 | }; | 
 | 141 |  | 
 | 142 | struct omninet_data | 
 | 143 | { | 
 | 144 | 	__u8	od_outseq;	// Sequence number for bulk_out URBs | 
 | 145 | }; | 
 | 146 |  | 
 | 147 | static int omninet_open (struct usb_serial_port *port, struct file *filp) | 
 | 148 | { | 
 | 149 | 	struct usb_serial	*serial = port->serial; | 
 | 150 | 	struct usb_serial_port	*wport; | 
 | 151 | 	struct omninet_data	*od; | 
 | 152 | 	int			result = 0; | 
 | 153 |  | 
 | 154 | 	dbg("%s - port %d", __FUNCTION__, port->number); | 
 | 155 |  | 
 | 156 | 	od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL ); | 
 | 157 | 	if( !od ) { | 
 | 158 | 		err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data)); | 
 | 159 | 		return -ENOMEM; | 
 | 160 | 	} | 
 | 161 |  | 
 | 162 | 	usb_set_serial_port_data(port, od); | 
 | 163 | 	wport = serial->port[1]; | 
 | 164 | 	wport->tty = port->tty; | 
 | 165 |  | 
 | 166 | 	/* Start reading from the device */ | 
 | 167 | 	usb_fill_bulk_urb(port->read_urb, serial->dev,  | 
 | 168 | 		      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress), | 
 | 169 | 		      port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length, | 
 | 170 | 		      omninet_read_bulk_callback, port); | 
 | 171 | 	result = usb_submit_urb(port->read_urb, GFP_KERNEL); | 
 | 172 | 	if (result) | 
 | 173 | 		err("%s - failed submitting read urb, error %d", __FUNCTION__, result); | 
 | 174 |  | 
 | 175 | 	return result; | 
 | 176 | } | 
 | 177 |  | 
 | 178 | static void omninet_close (struct usb_serial_port *port, struct file * filp) | 
 | 179 | { | 
 | 180 | 	struct usb_serial 	*serial = port->serial; | 
 | 181 | 	struct usb_serial_port 	*wport; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 |  | 
 | 183 | 	dbg("%s - port %d", __FUNCTION__, port->number); | 
 | 184 |  | 
 | 185 | 	wport = serial->port[1]; | 
 | 186 | 	usb_kill_urb(wport->write_urb); | 
 | 187 | 	usb_kill_urb(port->read_urb); | 
 | 188 |  | 
| Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 189 | 	kfree(usb_get_serial_port_data(port)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } | 
 | 191 |  | 
 | 192 |  | 
 | 193 | #define OMNINET_DATAOFFSET	0x04 | 
 | 194 | #define OMNINET_HEADERLEN	sizeof(struct omninet_header) | 
 | 195 | #define OMNINET_BULKOUTSIZE 	(64 - OMNINET_HEADERLEN) | 
 | 196 |  | 
 | 197 | static void omninet_read_bulk_callback (struct urb *urb, struct pt_regs *regs) | 
 | 198 | { | 
 | 199 | 	struct usb_serial_port 	*port 	= (struct usb_serial_port *)urb->context; | 
 | 200 | 	unsigned char 		*data 	= urb->transfer_buffer; | 
 | 201 | 	struct omninet_header 	*header = (struct omninet_header *) &data[0]; | 
 | 202 |  | 
 | 203 | 	int i; | 
 | 204 | 	int result; | 
 | 205 |  | 
| Greg Kroah-Hartman | 71a8924 | 2006-03-20 17:28:39 -0500 | [diff] [blame] | 206 | 	dbg("%s - port %d", __FUNCTION__, port->number); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 |  | 
 | 208 | 	if (urb->status) { | 
 | 209 | 		dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status); | 
 | 210 | 		return; | 
 | 211 | 	} | 
 | 212 |  | 
 | 213 | 	if ((debug) && (header->oh_xxx != 0x30)) { | 
 | 214 | 		if (urb->actual_length) { | 
 | 215 | 			printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len); | 
 | 216 | 			for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) { | 
 | 217 | 				printk ("%.2x ", data[i]); | 
 | 218 | 			} | 
 | 219 | 			printk ("\n"); | 
 | 220 | 		} | 
 | 221 | 	} | 
 | 222 |  | 
 | 223 | 	if (urb->actual_length && header->oh_len) { | 
 | 224 | 		for (i = 0; i < header->oh_len; i++) { | 
 | 225 | 			 tty_insert_flip_char(port->tty, data[OMNINET_DATAOFFSET + i], 0); | 
 | 226 | 	  	} | 
 | 227 | 	  	tty_flip_buffer_push(port->tty); | 
 | 228 | 	} | 
 | 229 |  | 
 | 230 | 	/* Continue trying to always read  */ | 
 | 231 | 	usb_fill_bulk_urb(urb, port->serial->dev,  | 
 | 232 | 		      usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress), | 
 | 233 | 		      urb->transfer_buffer, urb->transfer_buffer_length, | 
 | 234 | 		      omninet_read_bulk_callback, port); | 
 | 235 | 	result = usb_submit_urb(urb, GFP_ATOMIC); | 
 | 236 | 	if (result) | 
 | 237 | 		err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result); | 
 | 238 |  | 
 | 239 | 	return; | 
 | 240 | } | 
 | 241 |  | 
 | 242 | static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count) | 
 | 243 | { | 
 | 244 | 	struct usb_serial 	*serial	= port->serial; | 
 | 245 | 	struct usb_serial_port 	*wport	= serial->port[1]; | 
 | 246 |  | 
 | 247 | 	struct omninet_data 	*od 	= usb_get_serial_port_data(port); | 
 | 248 | 	struct omninet_header	*header = (struct omninet_header *) wport->write_urb->transfer_buffer; | 
 | 249 |  | 
 | 250 | 	int			result; | 
 | 251 |  | 
| Greg Kroah-Hartman | 71a8924 | 2006-03-20 17:28:39 -0500 | [diff] [blame] | 252 | 	dbg("%s - port %d", __FUNCTION__, port->number); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 |  | 
 | 254 | 	if (count == 0) { | 
 | 255 | 		dbg("%s - write request of 0 bytes", __FUNCTION__); | 
 | 256 | 		return (0); | 
 | 257 | 	} | 
| Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 258 |  | 
| Greg Kroah-Hartman | df3fccb | 2006-05-02 08:44:45 +0200 | [diff] [blame] | 259 | 	spin_lock(&wport->lock); | 
 | 260 | 	if (wport->write_urb_busy) { | 
 | 261 | 		spin_unlock(&wport->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | 		dbg("%s - already writing", __FUNCTION__); | 
| Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 263 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | 	} | 
| Greg Kroah-Hartman | df3fccb | 2006-05-02 08:44:45 +0200 | [diff] [blame] | 265 | 	wport->write_urb_busy = 1; | 
 | 266 | 	spin_unlock(&wport->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 |  | 
 | 268 | 	count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count; | 
 | 269 |  | 
 | 270 | 	memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count); | 
 | 271 |  | 
 | 272 | 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, wport->write_urb->transfer_buffer); | 
 | 273 |  | 
 | 274 | 	header->oh_seq 	= od->od_outseq++; | 
 | 275 | 	header->oh_len 	= count; | 
 | 276 | 	header->oh_xxx  = 0x03; | 
 | 277 | 	header->oh_pad 	= 0x00; | 
 | 278 |  | 
 | 279 | 	/* send the data out the bulk port, always 64 bytes */ | 
 | 280 | 	wport->write_urb->transfer_buffer_length = 64; | 
 | 281 |  | 
 | 282 | 	wport->write_urb->dev = serial->dev; | 
 | 283 | 	result = usb_submit_urb(wport->write_urb, GFP_ATOMIC); | 
| Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 284 | 	if (result) { | 
| Greg Kroah-Hartman | df3fccb | 2006-05-02 08:44:45 +0200 | [diff] [blame] | 285 | 		wport->write_urb_busy = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | 		err("%s - failed submitting write urb, error %d", __FUNCTION__, result); | 
| Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 287 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | 		result = count; | 
 | 289 |  | 
 | 290 | 	return result; | 
 | 291 | } | 
 | 292 |  | 
 | 293 |  | 
 | 294 | static int omninet_write_room (struct usb_serial_port *port) | 
 | 295 | { | 
 | 296 | 	struct usb_serial 	*serial = port->serial; | 
 | 297 | 	struct usb_serial_port 	*wport 	= serial->port[1]; | 
 | 298 |  | 
 | 299 | 	int room = 0; // Default: no room | 
 | 300 |  | 
| Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 301 | 	if (wport->write_urb_busy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | 		room = wport->bulk_out_size - OMNINET_HEADERLEN; | 
 | 303 |  | 
| Greg Kroah-Hartman | 71a8924 | 2006-03-20 17:28:39 -0500 | [diff] [blame] | 304 | 	dbg("%s - returns %d", __FUNCTION__, room); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 |  | 
 | 306 | 	return (room); | 
 | 307 | } | 
 | 308 |  | 
 | 309 | static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs) | 
 | 310 | { | 
 | 311 | /*	struct omninet_header	*header = (struct omninet_header  *) urb->transfer_buffer; */ | 
 | 312 | 	struct usb_serial_port 	*port   = (struct usb_serial_port *) urb->context; | 
 | 313 |  | 
| Greg Kroah-Hartman | 71a8924 | 2006-03-20 17:28:39 -0500 | [diff] [blame] | 314 | 	dbg("%s - port %0x\n", __FUNCTION__, port->number); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 |  | 
| Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 316 | 	port->write_urb_busy = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | 	if (urb->status) { | 
 | 318 | 		dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status); | 
 | 319 | 		return; | 
 | 320 | 	} | 
 | 321 |  | 
| Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 322 | 	usb_serial_port_softint(port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } | 
 | 324 |  | 
 | 325 |  | 
 | 326 | static void omninet_shutdown (struct usb_serial *serial) | 
 | 327 | { | 
 | 328 | 	dbg ("%s", __FUNCTION__); | 
 | 329 | } | 
 | 330 |  | 
 | 331 |  | 
 | 332 | static int __init omninet_init (void) | 
 | 333 | { | 
 | 334 | 	int retval; | 
 | 335 | 	retval = usb_serial_register(&zyxel_omninet_device); | 
 | 336 | 	if (retval) | 
 | 337 | 		goto failed_usb_serial_register; | 
 | 338 | 	retval = usb_register(&omninet_driver); | 
 | 339 | 	if (retval) | 
 | 340 | 		goto failed_usb_register; | 
 | 341 | 	info(DRIVER_VERSION ":" DRIVER_DESC); | 
 | 342 | 	return 0; | 
 | 343 | failed_usb_register: | 
 | 344 | 	usb_serial_deregister(&zyxel_omninet_device); | 
 | 345 | failed_usb_serial_register: | 
 | 346 | 	return retval; | 
 | 347 | } | 
 | 348 |  | 
 | 349 |  | 
 | 350 | static void __exit omninet_exit (void) | 
 | 351 | { | 
 | 352 | 	usb_deregister (&omninet_driver); | 
 | 353 | 	usb_serial_deregister (&zyxel_omninet_device); | 
 | 354 | } | 
 | 355 |  | 
 | 356 |  | 
 | 357 | module_init(omninet_init); | 
 | 358 | module_exit(omninet_exit); | 
 | 359 |  | 
 | 360 | MODULE_AUTHOR( DRIVER_AUTHOR ); | 
 | 361 | MODULE_DESCRIPTION( DRIVER_DESC ); | 
 | 362 | MODULE_LICENSE("GPL"); | 
 | 363 |  | 
 | 364 | module_param(debug, bool, S_IRUGO | S_IWUSR); | 
 | 365 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |