2 * USB ZyXEL omni.net LCD PLUS driver
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.
9 * See Documentation/usb/usb-serial.txt for more information on using this driver
11 * Please report both successes and troubles to the author at omninet@kroah.com
14 * switched from using spinlock to a semaphore, which fixes lots of problems.
17 * Identify version on module load.
19 * (11/01/2000) Adam J. Richter
20 * usb_device_id table support
23 * Fixed bug with urb->dev not being set properly, now that the usb
27 * Added locks for SMP safeness.
28 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
30 * Fixed potential race in omninet_write_bulk_callback
33 * Added module_init and module_exit functions to handle the fact that this
34 * driver is a loadable module now.
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>
49 #include <linux/usb/serial.h>
56 #define DRIVER_VERSION "v1.1"
57 #define DRIVER_AUTHOR "Alessandro Zummo"
58 #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
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 */
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);
68 static void omninet_write_bulk_callback (struct urb *urb);
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 static int omninet_attach (struct usb_serial *serial);
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 */
80 MODULE_DEVICE_TABLE (usb, id_table);
82 static struct usb_driver omninet_driver = {
84 .probe = usb_serial_probe,
85 .disconnect = usb_serial_disconnect,
91 static struct usb_serial_driver zyxel_omninet_device = {
96 .description = "ZyXEL - omni.net lcd plus usb",
97 .usb_driver = &omninet_driver,
99 .num_interrupt_in = 1,
103 .attach = omninet_attach,
104 .open = omninet_open,
105 .close = omninet_close,
106 .write = omninet_write,
107 .write_room = omninet_write_room,
108 .read_bulk_callback = omninet_read_bulk_callback,
109 .write_bulk_callback = omninet_write_bulk_callback,
110 .shutdown = omninet_shutdown,
116 * The omni.net always exchange 64 bytes of data with the host. The first
117 * four bytes are the control header, you can see it in the above structure.
119 * oh_seq is a sequence number. Don't know if/how it's used.
120 * oh_len is the length of the data bytes in the packet.
121 * oh_xxx Bit-mapped, related to handshaking and status info.
122 * I normally set it to 0x03 in trasmitted frames.
123 * 7: Active when the TA is in a CONNECTed state.
125 * 5: handshaking, unknown
126 * 4: handshaking, unknown
127 * 3: unknown, usually 0
128 * 2: unknown, usually 0
129 * 1: handshaking, unknown, usually set to 1 in trasmitted frames
130 * 0: handshaking, unknown, usually set to 1 in trasmitted frames
131 * oh_pad Probably a pad byte.
133 * After the header you will find data bytes if oh_len was greater than zero.
137 struct omninet_header
147 __u8 od_outseq; // Sequence number for bulk_out URBs
150 static int omninet_attach (struct usb_serial *serial)
152 struct omninet_data *od;
153 struct usb_serial_port *port = serial->port[0];
155 od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
157 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
160 usb_set_serial_port_data(port, od);
164 static int omninet_open (struct usb_serial_port *port, struct file *filp)
166 struct usb_serial *serial = port->serial;
167 struct usb_serial_port *wport;
170 dbg("%s - port %d", __FUNCTION__, port->number);
172 wport = serial->port[1];
173 wport->tty = port->tty;
175 /* Start reading from the device */
176 usb_fill_bulk_urb(port->read_urb, serial->dev,
177 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
178 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
179 omninet_read_bulk_callback, port);
180 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
182 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
188 static void omninet_close (struct usb_serial_port *port, struct file * filp)
190 dbg("%s - port %d", __FUNCTION__, port->number);
191 usb_kill_urb(port->read_urb);
195 #define OMNINET_DATAOFFSET 0x04
196 #define OMNINET_HEADERLEN sizeof(struct omninet_header)
197 #define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
199 static void omninet_read_bulk_callback (struct urb *urb)
201 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
202 unsigned char *data = urb->transfer_buffer;
203 struct omninet_header *header = (struct omninet_header *) &data[0];
208 dbg("%s - port %d", __FUNCTION__, port->number);
211 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
215 if ((debug) && (header->oh_xxx != 0x30)) {
216 if (urb->actual_length) {
217 printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len);
218 for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) {
219 printk ("%.2x ", data[i]);
225 if (urb->actual_length && header->oh_len) {
226 for (i = 0; i < header->oh_len; i++) {
227 tty_insert_flip_char(port->tty, data[OMNINET_DATAOFFSET + i], 0);
229 tty_flip_buffer_push(port->tty);
232 /* Continue trying to always read */
233 usb_fill_bulk_urb(urb, port->serial->dev,
234 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
235 urb->transfer_buffer, urb->transfer_buffer_length,
236 omninet_read_bulk_callback, port);
237 result = usb_submit_urb(urb, GFP_ATOMIC);
239 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
244 static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count)
246 struct usb_serial *serial = port->serial;
247 struct usb_serial_port *wport = serial->port[1];
249 struct omninet_data *od = usb_get_serial_port_data(port);
250 struct omninet_header *header = (struct omninet_header *) wport->write_urb->transfer_buffer;
254 dbg("%s - port %d", __FUNCTION__, port->number);
257 dbg("%s - write request of 0 bytes", __FUNCTION__);
261 spin_lock_bh(&wport->lock);
262 if (wport->write_urb_busy) {
263 spin_unlock_bh(&wport->lock);
264 dbg("%s - already writing", __FUNCTION__);
267 wport->write_urb_busy = 1;
268 spin_unlock_bh(&wport->lock);
270 count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
272 memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count);
274 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, wport->write_urb->transfer_buffer);
276 header->oh_seq = od->od_outseq++;
277 header->oh_len = count;
278 header->oh_xxx = 0x03;
279 header->oh_pad = 0x00;
281 /* send the data out the bulk port, always 64 bytes */
282 wport->write_urb->transfer_buffer_length = 64;
284 wport->write_urb->dev = serial->dev;
285 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
287 wport->write_urb_busy = 0;
288 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
296 static int omninet_write_room (struct usb_serial_port *port)
298 struct usb_serial *serial = port->serial;
299 struct usb_serial_port *wport = serial->port[1];
301 int room = 0; // Default: no room
303 if (wport->write_urb_busy)
304 room = wport->bulk_out_size - OMNINET_HEADERLEN;
306 dbg("%s - returns %d", __FUNCTION__, room);
311 static void omninet_write_bulk_callback (struct urb *urb)
313 /* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */
314 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
316 dbg("%s - port %0x\n", __FUNCTION__, port->number);
318 port->write_urb_busy = 0;
320 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
324 usb_serial_port_softint(port);
328 static void omninet_shutdown (struct usb_serial *serial)
330 struct usb_serial_port *wport = serial->port[1];
331 struct usb_serial_port *port = serial->port[0];
332 dbg ("%s", __FUNCTION__);
334 usb_kill_urb(wport->write_urb);
335 kfree(usb_get_serial_port_data(port));
339 static int __init omninet_init (void)
342 retval = usb_serial_register(&zyxel_omninet_device);
344 goto failed_usb_serial_register;
345 retval = usb_register(&omninet_driver);
347 goto failed_usb_register;
348 info(DRIVER_VERSION ":" DRIVER_DESC);
351 usb_serial_deregister(&zyxel_omninet_device);
352 failed_usb_serial_register:
357 static void __exit omninet_exit (void)
359 usb_deregister (&omninet_driver);
360 usb_serial_deregister (&zyxel_omninet_device);
364 module_init(omninet_init);
365 module_exit(omninet_exit);
367 MODULE_AUTHOR( DRIVER_AUTHOR );
368 MODULE_DESCRIPTION( DRIVER_DESC );
369 MODULE_LICENSE("GPL");
371 module_param(debug, bool, S_IRUGO | S_IWUSR);
372 MODULE_PARM_DESC(debug, "Debug enabled or not");