xref: /linux/drivers/usb/serial/omninet.c (revision 16dab54b8cbac39bd3f639db5d7d0fd8300a6cb0)
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 
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/tty.h>
44 #include <linux/tty_driver.h>
45 #include <linux/tty_flip.h>
46 #include <linux/module.h>
47 #include <linux/spinlock.h>
48 #include <asm/uaccess.h>
49 #include <linux/usb.h>
50 #include "usb-serial.h"
51 
52 static int debug;
53 
54 /*
55  * Version Information
56  */
57 #define DRIVER_VERSION "v1.1"
58 #define DRIVER_AUTHOR "Alessandro Zummo"
59 #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
60 
61 #define ZYXEL_VENDOR_ID		0x0586
62 #define ZYXEL_OMNINET_ID	0x1000
63 #define BT_IGNITIONPRO_ID	0x2000  /* This one seems to be a re-branded ZyXEL device */
64 
65 /* function prototypes */
66 static int  omninet_open		(struct usb_serial_port *port, struct file *filp);
67 static void omninet_close		(struct usb_serial_port *port, struct file *filp);
68 static void omninet_read_bulk_callback	(struct urb *urb, struct pt_regs *regs);
69 static void omninet_write_bulk_callback	(struct urb *urb, struct pt_regs *regs);
70 static int  omninet_write		(struct usb_serial_port *port, const unsigned char *buf, int count);
71 static int  omninet_write_room		(struct usb_serial_port *port);
72 static void omninet_shutdown		(struct usb_serial *serial);
73 
74 static struct usb_device_id id_table [] = {
75 	{ USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
76 	{ USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
77 	{ }						/* Terminating entry */
78 };
79 
80 MODULE_DEVICE_TABLE (usb, id_table);
81 
82 static struct usb_driver omninet_driver = {
83 	.owner =	THIS_MODULE,
84 	.name =		"omninet",
85 	.probe =	usb_serial_probe,
86 	.disconnect =	usb_serial_disconnect,
87 	.id_table =	id_table,
88 };
89 
90 
91 static struct usb_serial_device_type zyxel_omninet_device = {
92 	.owner =		THIS_MODULE,
93 	.name =			"ZyXEL - omni.net lcd plus usb",
94 	.short_name =		"omninet",
95 	.id_table =		id_table,
96 	.num_interrupt_in =	1,
97 	.num_bulk_in =		1,
98 	.num_bulk_out =		2,
99 	.num_ports =		1,
100 	.open =			omninet_open,
101 	.close =		omninet_close,
102 	.write =		omninet_write,
103 	.write_room =		omninet_write_room,
104 	.read_bulk_callback =	omninet_read_bulk_callback,
105 	.write_bulk_callback =	omninet_write_bulk_callback,
106 	.shutdown =		omninet_shutdown,
107 };
108 
109 
110 /* The protocol.
111  *
112  * The omni.net always exchange 64 bytes of data with the host. The first
113  * four bytes are the control header, you can see it in the above structure.
114  *
115  * oh_seq is a sequence number. Don't know if/how it's used.
116  * oh_len is the length of the data bytes in the packet.
117  * oh_xxx Bit-mapped, related to handshaking and status info.
118  *	I normally set it to 0x03 in trasmitted frames.
119  *	7: Active when the TA is in a CONNECTed state.
120  *	6: unknown
121  *	5: handshaking, unknown
122  *	4: handshaking, unknown
123  *	3: unknown, usually 0
124  *	2: unknown, usually 0
125  *	1: handshaking, unknown, usually set to 1 in trasmitted frames
126  *	0: handshaking, unknown, usually set to 1 in trasmitted frames
127  * oh_pad Probably a pad byte.
128  *
129  * After the header you will find data bytes if oh_len was greater than zero.
130  *
131  */
132 
133 struct omninet_header
134 {
135 	__u8	oh_seq;
136 	__u8	oh_len;
137 	__u8	oh_xxx;
138 	__u8	oh_pad;
139 };
140 
141 struct omninet_data
142 {
143 	__u8	od_outseq;	// Sequence number for bulk_out URBs
144 };
145 
146 static int omninet_open (struct usb_serial_port *port, struct file *filp)
147 {
148 	struct usb_serial	*serial = port->serial;
149 	struct usb_serial_port	*wport;
150 	struct omninet_data	*od;
151 	int			result = 0;
152 
153 	dbg("%s - port %d", __FUNCTION__, port->number);
154 
155 	od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
156 	if( !od ) {
157 		err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
158 		return -ENOMEM;
159 	}
160 
161 	usb_set_serial_port_data(port, od);
162 	wport = serial->port[1];
163 	wport->tty = port->tty;
164 
165 	/* Start reading from the device */
166 	usb_fill_bulk_urb(port->read_urb, serial->dev,
167 		      usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
168 		      port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
169 		      omninet_read_bulk_callback, port);
170 	result = usb_submit_urb(port->read_urb, GFP_KERNEL);
171 	if (result)
172 		err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
173 
174 	return result;
175 }
176 
177 static void omninet_close (struct usb_serial_port *port, struct file * filp)
178 {
179 	struct usb_serial 	*serial = port->serial;
180 	struct usb_serial_port 	*wport;
181 
182 	dbg("%s - port %d", __FUNCTION__, port->number);
183 
184 	wport = serial->port[1];
185 	usb_kill_urb(wport->write_urb);
186 	usb_kill_urb(port->read_urb);
187 
188 	kfree(usb_get_serial_port_data(port));
189 }
190 
191 
192 #define OMNINET_DATAOFFSET	0x04
193 #define OMNINET_HEADERLEN	sizeof(struct omninet_header)
194 #define OMNINET_BULKOUTSIZE 	(64 - OMNINET_HEADERLEN)
195 
196 static void omninet_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
197 {
198 	struct usb_serial_port 	*port 	= (struct usb_serial_port *)urb->context;
199 	unsigned char 		*data 	= urb->transfer_buffer;
200 	struct omninet_header 	*header = (struct omninet_header *) &data[0];
201 
202 	int i;
203 	int result;
204 
205 //	dbg("omninet_read_bulk_callback");
206 
207 	if (urb->status) {
208 		dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
209 		return;
210 	}
211 
212 	if ((debug) && (header->oh_xxx != 0x30)) {
213 		if (urb->actual_length) {
214 			printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len);
215 			for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) {
216 				printk ("%.2x ", data[i]);
217 			}
218 			printk ("\n");
219 		}
220 	}
221 
222 	if (urb->actual_length && header->oh_len) {
223 		for (i = 0; i < header->oh_len; i++) {
224 			 tty_insert_flip_char(port->tty, data[OMNINET_DATAOFFSET + i], 0);
225 	  	}
226 	  	tty_flip_buffer_push(port->tty);
227 	}
228 
229 	/* Continue trying to always read  */
230 	usb_fill_bulk_urb(urb, port->serial->dev,
231 		      usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
232 		      urb->transfer_buffer, urb->transfer_buffer_length,
233 		      omninet_read_bulk_callback, port);
234 	result = usb_submit_urb(urb, GFP_ATOMIC);
235 	if (result)
236 		err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
237 
238 	return;
239 }
240 
241 static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count)
242 {
243 	struct usb_serial 	*serial	= port->serial;
244 	struct usb_serial_port 	*wport	= serial->port[1];
245 
246 	struct omninet_data 	*od 	= usb_get_serial_port_data(port);
247 	struct omninet_header	*header = (struct omninet_header *) wport->write_urb->transfer_buffer;
248 
249 	int			result;
250 
251 //	dbg("omninet_write port %d", port->number);
252 
253 	if (count == 0) {
254 		dbg("%s - write request of 0 bytes", __FUNCTION__);
255 		return (0);
256 	}
257 
258 	spin_lock(&port->lock);
259 	if (port->write_urb_busy) {
260 		spin_unlock(&port->lock);
261 		dbg("%s - already writing", __FUNCTION__);
262 		return 0;
263 	}
264 	port->write_urb_busy = 1;
265 	spin_unlock(&port->lock);
266 
267 	count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
268 
269 	memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count);
270 
271 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, wport->write_urb->transfer_buffer);
272 
273 	header->oh_seq 	= od->od_outseq++;
274 	header->oh_len 	= count;
275 	header->oh_xxx  = 0x03;
276 	header->oh_pad 	= 0x00;
277 
278 	/* send the data out the bulk port, always 64 bytes */
279 	wport->write_urb->transfer_buffer_length = 64;
280 
281 	wport->write_urb->dev = serial->dev;
282 	result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
283 	if (result) {
284 		port->write_urb_busy = 0;
285 		err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
286 	} else
287 		result = count;
288 
289 	return result;
290 }
291 
292 
293 static int omninet_write_room (struct usb_serial_port *port)
294 {
295 	struct usb_serial 	*serial = port->serial;
296 	struct usb_serial_port 	*wport 	= serial->port[1];
297 
298 	int room = 0; // Default: no room
299 
300 	if (wport->write_urb_busy)
301 		room = wport->bulk_out_size - OMNINET_HEADERLEN;
302 
303 //	dbg("omninet_write_room returns %d", room);
304 
305 	return (room);
306 }
307 
308 static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
309 {
310 /*	struct omninet_header	*header = (struct omninet_header  *) urb->transfer_buffer; */
311 	struct usb_serial_port 	*port   = (struct usb_serial_port *) urb->context;
312 
313 //	dbg("omninet_write_bulk_callback, port %0x\n", port);
314 
315 	port->write_urb_busy = 0;
316 	if (urb->status) {
317 		dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
318 		return;
319 	}
320 
321 	schedule_work(&port->work);
322 
323 //	dbg("omninet_write_bulk_callback, tty %0x\n", tty);
324 }
325 
326 
327 static void omninet_shutdown (struct usb_serial *serial)
328 {
329 	dbg ("%s", __FUNCTION__);
330 }
331 
332 
333 static int __init omninet_init (void)
334 {
335 	int retval;
336 	retval = usb_serial_register(&zyxel_omninet_device);
337 	if (retval)
338 		goto failed_usb_serial_register;
339 	retval = usb_register(&omninet_driver);
340 	if (retval)
341 		goto failed_usb_register;
342 	info(DRIVER_VERSION ":" DRIVER_DESC);
343 	return 0;
344 failed_usb_register:
345 	usb_serial_deregister(&zyxel_omninet_device);
346 failed_usb_serial_register:
347 	return retval;
348 }
349 
350 
351 static void __exit omninet_exit (void)
352 {
353 	usb_deregister (&omninet_driver);
354 	usb_serial_deregister (&zyxel_omninet_device);
355 }
356 
357 
358 module_init(omninet_init);
359 module_exit(omninet_exit);
360 
361 MODULE_AUTHOR( DRIVER_AUTHOR );
362 MODULE_DESCRIPTION( DRIVER_DESC );
363 MODULE_LICENSE("GPL");
364 
365 module_param(debug, bool, S_IRUGO | S_IWUSR);
366 MODULE_PARM_DESC(debug, "Debug enabled or not");
367