blob: 5a2d045562f06e201edfd70c5e0cda637d3f51dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB ZyXEL omni.net LCD PLUS driver
3 *
Greg Kroah-Hartman4d0dce32007-06-12 11:43:37 -07004 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * See Documentation/usb/usb-serial.txt for more information on using this driver
9 *
10 * Please report both successes and troubles to the author at omninet@kroah.com
11 *
12 * (05/30/2001) gkh
13 * switched from using spinlock to a semaphore, which fixes lots of problems.
14 *
15 * (04/08/2001) gb
16 * Identify version on module load.
17 *
18 * (11/01/2000) Adam J. Richter
19 * usb_device_id table support
20 *
21 * (10/05/2000) gkh
22 * Fixed bug with urb->dev not being set properly, now that the usb
23 * core needs it.
24 *
25 * (08/28/2000) gkh
26 * Added locks for SMP safeness.
27 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
28 * than once.
29 * Fixed potential race in omninet_write_bulk_callback
30 *
31 * (07/19/2000) gkh
32 * Added module_init and module_exit functions to handle the fact that this
33 * driver is a loadable module now.
34 *
35 */
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070048#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static int debug;
51
52/*
53 * Version Information
54 */
55#define DRIVER_VERSION "v1.1"
56#define DRIVER_AUTHOR "Alessandro Zummo"
57#define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
58
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 */
62
63/* function prototypes */
Alan Cox95da3102008-07-22 11:09:07 +010064static int omninet_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
65static void omninet_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
David Howells7d12e782006-10-05 14:55:46 +010066static void omninet_read_bulk_callback (struct urb *urb);
67static void omninet_write_bulk_callback (struct urb *urb);
Alan Cox95da3102008-07-22 11:09:07 +010068static int omninet_write (struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count);
69static int omninet_write_room (struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void omninet_shutdown (struct usb_serial *serial);
Oliver Neukum5ec18622007-03-27 16:02:34 +020071static int omninet_attach (struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static 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
79MODULE_DEVICE_TABLE (usb, id_table);
80
81static struct usb_driver omninet_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .name = "omninet",
83 .probe = usb_serial_probe,
84 .disconnect = usb_serial_disconnect,
85 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080086 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
89
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070090static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070091 .driver = {
92 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070093 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070094 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070095 .description = "ZyXEL - omni.net lcd plus usb",
Johannes Hölzld9b1b782006-12-17 21:50:24 +010096 .usb_driver = &omninet_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .num_ports = 1,
Oliver Neukum5ec18622007-03-27 16:02:34 +020099 .attach = omninet_attach,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .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
133struct omninet_header
134{
135 __u8 oh_seq;
136 __u8 oh_len;
137 __u8 oh_xxx;
138 __u8 oh_pad;
139};
140
141struct omninet_data
142{
143 __u8 od_outseq; // Sequence number for bulk_out URBs
144};
145
Oliver Neukum5ec18622007-03-27 16:02:34 +0200146static int omninet_attach (struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200148 struct omninet_data *od;
149 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
152 if( !od ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800153 err("%s- kmalloc(%Zd) failed.", __func__, sizeof(struct omninet_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -ENOMEM;
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 usb_set_serial_port_data(port, od);
Oliver Neukum5ec18622007-03-27 16:02:34 +0200157 return 0;
158}
159
Alan Cox95da3102008-07-22 11:09:07 +0100160static int omninet_open(struct tty_struct *tty,
161 struct usb_serial_port *port, struct file *filp)
Oliver Neukum5ec18622007-03-27 16:02:34 +0200162{
163 struct usb_serial *serial = port->serial;
164 struct usb_serial_port *wport;
Oliver Neukum5ec18622007-03-27 16:02:34 +0200165 int result = 0;
166
Harvey Harrison441b62c2008-03-03 16:08:34 -0800167 dbg("%s - port %d", __func__, port->number);
Oliver Neukum5ec18622007-03-27 16:02:34 +0200168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 wport = serial->port[1];
Alan Cox95da3102008-07-22 11:09:07 +0100170 wport->port.tty = tty; /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* Start reading from the device */
173 usb_fill_bulk_urb(port->read_urb, serial->dev,
174 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
175 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
176 omninet_read_bulk_callback, port);
177 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Oliver Neukum4f93b3e2007-03-20 13:15:05 +0100178 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800179 err("%s - failed submitting read urb, error %d", __func__, result);
Oliver Neukum4f93b3e2007-03-20 13:15:05 +0100180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 return result;
183}
184
Alan Cox95da3102008-07-22 11:09:07 +0100185static void omninet_close(struct tty_struct *tty,
186 struct usb_serial_port *port, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800188 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192
193#define OMNINET_DATAOFFSET 0x04
194#define OMNINET_HEADERLEN sizeof(struct omninet_header)
195#define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
196
David Howells7d12e782006-10-05 14:55:46 +0100197static void omninet_read_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Ming Leicdc97792008-02-24 18:41:47 +0800199 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unsigned char *data = urb->transfer_buffer;
201 struct omninet_header *header = (struct omninet_header *) &data[0];
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700202 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int i;
204 int result;
205
Harvey Harrison441b62c2008-03-03 16:08:34 -0800206 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700208 if (status) {
209 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800210 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return;
212 }
213
214 if ((debug) && (header->oh_xxx != 0x30)) {
215 if (urb->actual_length) {
216 printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len);
217 for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) {
218 printk ("%.2x ", data[i]);
219 }
220 printk ("\n");
221 }
222 }
223
224 if (urb->actual_length && header->oh_len) {
225 for (i = 0; i < header->oh_len; i++) {
Alan Cox95da3102008-07-22 11:09:07 +0100226 tty_insert_flip_char(port->port.tty, data[OMNINET_DATAOFFSET + i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Alan Cox95da3102008-07-22 11:09:07 +0100228 tty_flip_buffer_push(port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
231 /* Continue trying to always read */
232 usb_fill_bulk_urb(urb, port->serial->dev,
233 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
234 urb->transfer_buffer, urb->transfer_buffer_length,
235 omninet_read_bulk_callback, port);
236 result = usb_submit_urb(urb, GFP_ATOMIC);
237 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800238 err("%s - failed resubmitting read urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 return;
241}
242
Alan Cox95da3102008-07-22 11:09:07 +0100243static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
244 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct usb_serial *serial = port->serial;
247 struct usb_serial_port *wport = serial->port[1];
248
249 struct omninet_data *od = usb_get_serial_port_data(port);
250 struct omninet_header *header = (struct omninet_header *) wport->write_urb->transfer_buffer;
251
252 int result;
253
Harvey Harrison441b62c2008-03-03 16:08:34 -0800254 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800257 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return (0);
259 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700260
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200261 spin_lock_bh(&wport->lock);
Greg Kroah-Hartmandf3fccb2006-05-02 08:44:45 +0200262 if (wport->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200263 spin_unlock_bh(&wport->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800264 dbg("%s - already writing", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Greg Kroah-Hartmandf3fccb2006-05-02 08:44:45 +0200267 wport->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200268 spin_unlock_bh(&wport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
271
272 memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count);
273
Harvey Harrison441b62c2008-03-03 16:08:34 -0800274 usb_serial_debug_data(debug, &port->dev, __func__, count, wport->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 header->oh_seq = od->od_outseq++;
277 header->oh_len = count;
278 header->oh_xxx = 0x03;
279 header->oh_pad = 0x00;
280
281 /* send the data out the bulk port, always 64 bytes */
282 wport->write_urb->transfer_buffer_length = 64;
283
284 wport->write_urb->dev = serial->dev;
285 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700286 if (result) {
Greg Kroah-Hartmandf3fccb2006-05-02 08:44:45 +0200287 wport->write_urb_busy = 0;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800288 err("%s - failed submitting write urb, error %d", __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700289 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 result = count;
291
292 return result;
293}
294
295
Alan Cox95da3102008-07-22 11:09:07 +0100296static int omninet_write_room (struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Alan Cox95da3102008-07-22 11:09:07 +0100298 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct usb_serial *serial = port->serial;
300 struct usb_serial_port *wport = serial->port[1];
301
Alan Coxa5b6f602008-04-08 17:16:06 +0100302 int room = 0; /* Default: no room */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Alan Coxa5b6f602008-04-08 17:16:06 +0100304 /* FIXME: no consistent locking for write_urb_busy */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700305 if (wport->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 room = wport->bulk_out_size - OMNINET_HEADERLEN;
307
Harvey Harrison441b62c2008-03-03 16:08:34 -0800308 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 return (room);
311}
312
David Howells7d12e782006-10-05 14:55:46 +0100313static void omninet_write_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315/* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */
Ming Leicdc97792008-02-24 18:41:47 +0800316 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700317 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Harvey Harrison441b62c2008-03-03 16:08:34 -0800319 dbg("%s - port %0x\n", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700321 port->write_urb_busy = 0;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700322 if (status) {
323 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800324 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return;
326 }
327
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700328 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331
332static void omninet_shutdown (struct usb_serial *serial)
333{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200334 struct usb_serial_port *wport = serial->port[1];
335 struct usb_serial_port *port = serial->port[0];
Harvey Harrison441b62c2008-03-03 16:08:34 -0800336 dbg ("%s", __func__);
Oliver Neukum5ec18622007-03-27 16:02:34 +0200337
338 usb_kill_urb(wport->write_urb);
339 kfree(usb_get_serial_port_data(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342
343static int __init omninet_init (void)
344{
345 int retval;
346 retval = usb_serial_register(&zyxel_omninet_device);
347 if (retval)
348 goto failed_usb_serial_register;
349 retval = usb_register(&omninet_driver);
350 if (retval)
351 goto failed_usb_register;
352 info(DRIVER_VERSION ":" DRIVER_DESC);
353 return 0;
354failed_usb_register:
355 usb_serial_deregister(&zyxel_omninet_device);
356failed_usb_serial_register:
357 return retval;
358}
359
360
361static void __exit omninet_exit (void)
362{
363 usb_deregister (&omninet_driver);
364 usb_serial_deregister (&zyxel_omninet_device);
365}
366
367
368module_init(omninet_init);
369module_exit(omninet_exit);
370
371MODULE_AUTHOR( DRIVER_AUTHOR );
372MODULE_DESCRIPTION( DRIVER_DESC );
373MODULE_LICENSE("GPL");
374
375module_param(debug, bool, S_IRUGO | S_IWUSR);
376MODULE_PARM_DESC(debug, "Debug enabled or not");