blob: 4adfab988e86630089c57d6ff4001d7986166c94 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070038#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-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static 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 */
65static int omninet_open (struct usb_serial_port *port, struct file *filp);
66static void omninet_close (struct usb_serial_port *port, struct file *filp);
David Howells7d12e782006-10-05 14:55:46 +010067static void omninet_read_bulk_callback (struct urb *urb);
68static void omninet_write_bulk_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count);
70static int omninet_write_room (struct usb_serial_port *port);
71static void omninet_shutdown (struct usb_serial *serial);
Oliver Neukum5ec18622007-03-27 16:02:34 +020072static int omninet_attach (struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static 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
80MODULE_DEVICE_TABLE (usb, id_table);
81
82static struct usb_driver omninet_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 .name = "omninet",
84 .probe = usb_serial_probe,
85 .disconnect = usb_serial_disconnect,
86 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080087 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070091static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070092 .driver = {
93 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070094 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070095 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070096 .description = "ZyXEL - omni.net lcd plus usb",
Johannes Hölzld9b1b782006-12-17 21:50:24 +010097 .usb_driver = &omninet_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .id_table = id_table,
99 .num_interrupt_in = 1,
100 .num_bulk_in = 1,
101 .num_bulk_out = 2,
102 .num_ports = 1,
Oliver Neukum5ec18622007-03-27 16:02:34 +0200103 .attach = omninet_attach,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .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,
111};
112
113
114/* The protocol.
115 *
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.
118 *
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.
124 * 6: unknown
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.
132 *
133 * After the header you will find data bytes if oh_len was greater than zero.
134 *
135 */
136
137struct omninet_header
138{
139 __u8 oh_seq;
140 __u8 oh_len;
141 __u8 oh_xxx;
142 __u8 oh_pad;
143};
144
145struct omninet_data
146{
147 __u8 od_outseq; // Sequence number for bulk_out URBs
148};
149
Oliver Neukum5ec18622007-03-27 16:02:34 +0200150static int omninet_attach (struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200152 struct omninet_data *od;
153 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 usb_set_serial_port_data(port, od);
Oliver Neukum5ec18622007-03-27 16:02:34 +0200161 return 0;
162}
163
164static int omninet_open (struct usb_serial_port *port, struct file *filp)
165{
166 struct usb_serial *serial = port->serial;
167 struct usb_serial_port *wport;
168 struct omninet_data *od = usb_get_serial_port_data(port);
169 int result = 0;
170
171 dbg("%s - port %d", __FUNCTION__, port->number);
172
173 od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 wport = serial->port[1];
175 wport->tty = port->tty;
176
177 /* Start reading from the device */
178 usb_fill_bulk_urb(port->read_urb, serial->dev,
179 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
180 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
181 omninet_read_bulk_callback, port);
182 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Oliver Neukum4f93b3e2007-03-20 13:15:05 +0100183 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
Oliver Neukum4f93b3e2007-03-20 13:15:05 +0100185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 return result;
188}
189
190static void omninet_close (struct usb_serial_port *port, struct file * filp)
191{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196
197#define OMNINET_DATAOFFSET 0x04
198#define OMNINET_HEADERLEN sizeof(struct omninet_header)
199#define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
200
David Howells7d12e782006-10-05 14:55:46 +0100201static void omninet_read_bulk_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
204 unsigned char *data = urb->transfer_buffer;
205 struct omninet_header *header = (struct omninet_header *) &data[0];
206
207 int i;
208 int result;
209
Greg Kroah-Hartman71a89242006-03-20 17:28:39 -0500210 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (urb->status) {
213 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
214 return;
215 }
216
217 if ((debug) && (header->oh_xxx != 0x30)) {
218 if (urb->actual_length) {
219 printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len);
220 for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) {
221 printk ("%.2x ", data[i]);
222 }
223 printk ("\n");
224 }
225 }
226
227 if (urb->actual_length && header->oh_len) {
228 for (i = 0; i < header->oh_len; i++) {
229 tty_insert_flip_char(port->tty, data[OMNINET_DATAOFFSET + i], 0);
230 }
231 tty_flip_buffer_push(port->tty);
232 }
233
234 /* Continue trying to always read */
235 usb_fill_bulk_urb(urb, port->serial->dev,
236 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
237 urb->transfer_buffer, urb->transfer_buffer_length,
238 omninet_read_bulk_callback, port);
239 result = usb_submit_urb(urb, GFP_ATOMIC);
240 if (result)
241 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
242
243 return;
244}
245
246static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count)
247{
248 struct usb_serial *serial = port->serial;
249 struct usb_serial_port *wport = serial->port[1];
250
251 struct omninet_data *od = usb_get_serial_port_data(port);
252 struct omninet_header *header = (struct omninet_header *) wport->write_urb->transfer_buffer;
253
254 int result;
255
Greg Kroah-Hartman71a89242006-03-20 17:28:39 -0500256 dbg("%s - port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (count == 0) {
259 dbg("%s - write request of 0 bytes", __FUNCTION__);
260 return (0);
261 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700262
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200263 spin_lock_bh(&wport->lock);
Greg Kroah-Hartmandf3fccb2006-05-02 08:44:45 +0200264 if (wport->write_urb_busy) {
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200265 spin_unlock_bh(&wport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Greg Kroah-Hartmandf3fccb2006-05-02 08:44:45 +0200269 wport->write_urb_busy = 1;
Peter Zijlstrae81ee632006-09-25 12:51:41 +0200270 spin_unlock_bh(&wport->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
273
274 memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count);
275
276 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, wport->write_urb->transfer_buffer);
277
278 header->oh_seq = od->od_outseq++;
279 header->oh_len = count;
280 header->oh_xxx = 0x03;
281 header->oh_pad = 0x00;
282
283 /* send the data out the bulk port, always 64 bytes */
284 wport->write_urb->transfer_buffer_length = 64;
285
286 wport->write_urb->dev = serial->dev;
287 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700288 if (result) {
Greg Kroah-Hartmandf3fccb2006-05-02 08:44:45 +0200289 wport->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700291 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 result = count;
293
294 return result;
295}
296
297
298static int omninet_write_room (struct usb_serial_port *port)
299{
300 struct usb_serial *serial = port->serial;
301 struct usb_serial_port *wport = serial->port[1];
302
303 int room = 0; // Default: no room
304
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
Greg Kroah-Hartman71a89242006-03-20 17:28:39 -0500308 dbg("%s - returns %d", __FUNCTION__, 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; */
316 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
317
Greg Kroah-Hartman71a89242006-03-20 17:28:39 -0500318 dbg("%s - port %0x\n", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700320 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (urb->status) {
322 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
323 return;
324 }
325
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700326 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329
330static void omninet_shutdown (struct usb_serial *serial)
331{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200332 struct usb_serial_port *wport = serial->port[1];
333 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dbg ("%s", __FUNCTION__);
Oliver Neukum5ec18622007-03-27 16:02:34 +0200335
336 usb_kill_urb(wport->write_urb);
337 kfree(usb_get_serial_port_data(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340
341static int __init omninet_init (void)
342{
343 int retval;
344 retval = usb_serial_register(&zyxel_omninet_device);
345 if (retval)
346 goto failed_usb_serial_register;
347 retval = usb_register(&omninet_driver);
348 if (retval)
349 goto failed_usb_register;
350 info(DRIVER_VERSION ":" DRIVER_DESC);
351 return 0;
352failed_usb_register:
353 usb_serial_deregister(&zyxel_omninet_device);
354failed_usb_serial_register:
355 return retval;
356}
357
358
359static void __exit omninet_exit (void)
360{
361 usb_deregister (&omninet_driver);
362 usb_serial_deregister (&zyxel_omninet_device);
363}
364
365
366module_init(omninet_init);
367module_exit(omninet_exit);
368
369MODULE_AUTHOR( DRIVER_AUTHOR );
370MODULE_DESCRIPTION( DRIVER_DESC );
371MODULE_LICENSE("GPL");
372
373module_param(debug, bool, S_IRUGO | S_IWUSR);
374MODULE_PARM_DESC(debug, "Debug enabled or not");