2 * USB ZyXEL omni.net LCD PLUS driver
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
8 * See Documentation/usb/usb-serial.txt for more information on using this driver
10 * Please report both successes and troubles to the author at omninet@kroah.com
13 * switched from using spinlock to a semaphore, which fixes lots of problems.
16 * Identify version on module load.
18 * (11/01/2000) Adam J. Richter
19 * usb_device_id table support
22 * Fixed bug with urb->dev not being set properly, now that the usb
26 * Added locks for SMP safeness.
27 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
29 * Fixed potential race in omninet_write_bulk_callback
32 * Added module_init and module_exit functions to handle the fact that this
33 * driver is a loadable module now.
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/tty.h>
42 #include <linux/tty_driver.h>
43 #include <linux/tty_flip.h>
44 #include <linux/module.h>
45 #include <linux/spinlock.h>
46 #include <asm/uaccess.h>
47 #include <linux/usb.h>
48 #include <linux/usb/serial.h>
55 #define DRIVER_VERSION "v1.1"
56 #define DRIVER_AUTHOR "Alessandro Zummo"
57 #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
59 #define ZYXEL_VENDOR_ID 0x0586
60 #define ZYXEL_OMNINET_ID 0x1000
61 #define BT_IGNITIONPRO_ID 0x2000 /* This one seems to be a re-branded ZyXEL device */
63 /* function prototypes */
64 static int omninet_open (struct usb_serial_port
*port
, struct file
*filp
);
65 static void omninet_close (struct usb_serial_port
*port
, struct file
*filp
);
66 static void omninet_read_bulk_callback (struct urb
*urb
);
67 static void omninet_write_bulk_callback (struct urb
*urb
);
68 static int omninet_write (struct usb_serial_port
*port
, const unsigned char *buf
, int count
);
69 static int omninet_write_room (struct usb_serial_port
*port
);
70 static void omninet_shutdown (struct usb_serial
*serial
);
71 static int omninet_attach (struct usb_serial
*serial
);
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 */
79 MODULE_DEVICE_TABLE (usb
, id_table
);
81 static struct usb_driver omninet_driver
= {
83 .probe
= usb_serial_probe
,
84 .disconnect
= usb_serial_disconnect
,
90 static struct usb_serial_driver zyxel_omninet_device
= {
95 .description
= "ZyXEL - omni.net lcd plus usb",
96 .usb_driver
= &omninet_driver
,
99 .attach
= omninet_attach
,
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
,
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.
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.
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.
129 * After the header you will find data bytes if oh_len was greater than zero.
133 struct omninet_header
143 __u8 od_outseq
; // Sequence number for bulk_out URBs
146 static int omninet_attach (struct usb_serial
*serial
)
148 struct omninet_data
*od
;
149 struct usb_serial_port
*port
= serial
->port
[0];
151 od
= kmalloc( sizeof(struct omninet_data
), GFP_KERNEL
);
153 err("%s- kmalloc(%Zd) failed.", __func__
, sizeof(struct omninet_data
));
156 usb_set_serial_port_data(port
, od
);
160 static int omninet_open (struct usb_serial_port
*port
, struct file
*filp
)
162 struct usb_serial
*serial
= port
->serial
;
163 struct usb_serial_port
*wport
;
166 dbg("%s - port %d", __func__
, port
->number
);
168 wport
= serial
->port
[1];
169 wport
->tty
= port
->tty
;
171 /* Start reading from the device */
172 usb_fill_bulk_urb(port
->read_urb
, serial
->dev
,
173 usb_rcvbulkpipe(serial
->dev
, port
->bulk_in_endpointAddress
),
174 port
->read_urb
->transfer_buffer
, port
->read_urb
->transfer_buffer_length
,
175 omninet_read_bulk_callback
, port
);
176 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
178 err("%s - failed submitting read urb, error %d", __func__
, result
);
184 static void omninet_close (struct usb_serial_port
*port
, struct file
* filp
)
186 dbg("%s - port %d", __func__
, port
->number
);
187 usb_kill_urb(port
->read_urb
);
191 #define OMNINET_DATAOFFSET 0x04
192 #define OMNINET_HEADERLEN sizeof(struct omninet_header)
193 #define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
195 static void omninet_read_bulk_callback (struct urb
*urb
)
197 struct usb_serial_port
*port
= urb
->context
;
198 unsigned char *data
= urb
->transfer_buffer
;
199 struct omninet_header
*header
= (struct omninet_header
*) &data
[0];
200 int status
= urb
->status
;
204 dbg("%s - port %d", __func__
, port
->number
);
207 dbg("%s - nonzero read bulk status received: %d",
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
]);
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);
226 tty_flip_buffer_push(port
->tty
);
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
);
236 err("%s - failed resubmitting read urb, error %d", __func__
, result
);
241 static int omninet_write (struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
243 struct usb_serial
*serial
= port
->serial
;
244 struct usb_serial_port
*wport
= serial
->port
[1];
246 struct omninet_data
*od
= usb_get_serial_port_data(port
);
247 struct omninet_header
*header
= (struct omninet_header
*) wport
->write_urb
->transfer_buffer
;
251 dbg("%s - port %d", __func__
, port
->number
);
254 dbg("%s - write request of 0 bytes", __func__
);
258 spin_lock_bh(&wport
->lock
);
259 if (wport
->write_urb_busy
) {
260 spin_unlock_bh(&wport
->lock
);
261 dbg("%s - already writing", __func__
);
264 wport
->write_urb_busy
= 1;
265 spin_unlock_bh(&wport
->lock
);
267 count
= (count
> OMNINET_BULKOUTSIZE
) ? OMNINET_BULKOUTSIZE
: count
;
269 memcpy (wport
->write_urb
->transfer_buffer
+ OMNINET_DATAOFFSET
, buf
, count
);
271 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, wport
->write_urb
->transfer_buffer
);
273 header
->oh_seq
= od
->od_outseq
++;
274 header
->oh_len
= count
;
275 header
->oh_xxx
= 0x03;
276 header
->oh_pad
= 0x00;
278 /* send the data out the bulk port, always 64 bytes */
279 wport
->write_urb
->transfer_buffer_length
= 64;
281 wport
->write_urb
->dev
= serial
->dev
;
282 result
= usb_submit_urb(wport
->write_urb
, GFP_ATOMIC
);
284 wport
->write_urb_busy
= 0;
285 err("%s - failed submitting write urb, error %d", __func__
, result
);
293 static int omninet_write_room (struct usb_serial_port
*port
)
295 struct usb_serial
*serial
= port
->serial
;
296 struct usb_serial_port
*wport
= serial
->port
[1];
298 int room
= 0; /* Default: no room */
300 /* FIXME: no consistent locking for write_urb_busy */
301 if (wport
->write_urb_busy
)
302 room
= wport
->bulk_out_size
- OMNINET_HEADERLEN
;
304 dbg("%s - returns %d", __func__
, room
);
309 static void omninet_write_bulk_callback (struct urb
*urb
)
311 /* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */
312 struct usb_serial_port
*port
= urb
->context
;
313 int status
= urb
->status
;
315 dbg("%s - port %0x\n", __func__
, port
->number
);
317 port
->write_urb_busy
= 0;
319 dbg("%s - nonzero write bulk status received: %d",
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", __func__
);
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");