blob: b595befb24cf783b410ae851be792ba2332b6f6f [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
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
52static 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 */
66static int omninet_open (struct usb_serial_port *port, struct file *filp);
67static void omninet_close (struct usb_serial_port *port, struct file *filp);
68static void omninet_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
69static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
70static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count);
71static int omninet_write_room (struct usb_serial_port *port);
72static void omninet_shutdown (struct usb_serial *serial);
73
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 = {
83 .owner = THIS_MODULE,
84 .name = "omninet",
85 .probe = usb_serial_probe,
86 .disconnect = usb_serial_disconnect,
87 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080088 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
91
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070092static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070093 .driver = {
94 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070095 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070096 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070097 .description = "ZyXEL - omni.net lcd plus usb",
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,
103 .open = omninet_open,
104 .close = omninet_close,
105 .write = omninet_write,
106 .write_room = omninet_write_room,
107 .read_bulk_callback = omninet_read_bulk_callback,
108 .write_bulk_callback = omninet_write_bulk_callback,
109 .shutdown = omninet_shutdown,
110};
111
112
113/* The protocol.
114 *
115 * The omni.net always exchange 64 bytes of data with the host. The first
116 * four bytes are the control header, you can see it in the above structure.
117 *
118 * oh_seq is a sequence number. Don't know if/how it's used.
119 * oh_len is the length of the data bytes in the packet.
120 * oh_xxx Bit-mapped, related to handshaking and status info.
121 * I normally set it to 0x03 in trasmitted frames.
122 * 7: Active when the TA is in a CONNECTed state.
123 * 6: unknown
124 * 5: handshaking, unknown
125 * 4: handshaking, unknown
126 * 3: unknown, usually 0
127 * 2: unknown, usually 0
128 * 1: handshaking, unknown, usually set to 1 in trasmitted frames
129 * 0: handshaking, unknown, usually set to 1 in trasmitted frames
130 * oh_pad Probably a pad byte.
131 *
132 * After the header you will find data bytes if oh_len was greater than zero.
133 *
134 */
135
136struct omninet_header
137{
138 __u8 oh_seq;
139 __u8 oh_len;
140 __u8 oh_xxx;
141 __u8 oh_pad;
142};
143
144struct omninet_data
145{
146 __u8 od_outseq; // Sequence number for bulk_out URBs
147};
148
149static int omninet_open (struct usb_serial_port *port, struct file *filp)
150{
151 struct usb_serial *serial = port->serial;
152 struct usb_serial_port *wport;
153 struct omninet_data *od;
154 int result = 0;
155
156 dbg("%s - port %d", __FUNCTION__, port->number);
157
158 od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
159 if( !od ) {
160 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
161 return -ENOMEM;
162 }
163
164 usb_set_serial_port_data(port, od);
165 wport = serial->port[1];
166 wport->tty = port->tty;
167
168 /* Start reading from the device */
169 usb_fill_bulk_urb(port->read_urb, serial->dev,
170 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
171 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
172 omninet_read_bulk_callback, port);
173 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
174 if (result)
175 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
176
177 return result;
178}
179
180static void omninet_close (struct usb_serial_port *port, struct file * filp)
181{
182 struct usb_serial *serial = port->serial;
183 struct usb_serial_port *wport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 dbg("%s - port %d", __FUNCTION__, port->number);
186
187 wport = serial->port[1];
188 usb_kill_urb(wport->write_urb);
189 usb_kill_urb(port->read_urb);
190
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700191 kfree(usb_get_serial_port_data(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194
195#define OMNINET_DATAOFFSET 0x04
196#define OMNINET_HEADERLEN sizeof(struct omninet_header)
197#define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
198
199static void omninet_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
200{
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];
204
205 int i;
206 int result;
207
208// dbg("omninet_read_bulk_callback");
209
210 if (urb->status) {
211 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
212 return;
213 }
214
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]);
220 }
221 printk ("\n");
222 }
223 }
224
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);
228 }
229 tty_flip_buffer_push(port->tty);
230 }
231
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);
238 if (result)
239 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
240
241 return;
242}
243
244static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count)
245{
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
254// dbg("omninet_write port %d", port->number);
255
256 if (count == 0) {
257 dbg("%s - write request of 0 bytes", __FUNCTION__);
258 return (0);
259 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700260
261 spin_lock(&port->lock);
262 if (port->write_urb_busy) {
263 spin_unlock(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dbg("%s - already writing", __FUNCTION__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700267 port->write_urb_busy = 1;
268 spin_unlock(&port->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
274 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, wport->write_urb->transfer_buffer);
275
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) {
287 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 err("%s - failed submitting write urb, error %d", __FUNCTION__, 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
296static int omninet_write_room (struct usb_serial_port *port)
297{
298 struct usb_serial *serial = port->serial;
299 struct usb_serial_port *wport = serial->port[1];
300
301 int room = 0; // Default: no room
302
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700303 if (wport->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 room = wport->bulk_out_size - OMNINET_HEADERLEN;
305
306// dbg("omninet_write_room returns %d", room);
307
308 return (room);
309}
310
311static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
312{
313/* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */
314 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
315
316// dbg("omninet_write_bulk_callback, port %0x\n", port);
317
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700318 port->write_urb_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (urb->status) {
320 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
321 return;
322 }
323
324 schedule_work(&port->work);
325
326// dbg("omninet_write_bulk_callback, tty %0x\n", tty);
327}
328
329
330static void omninet_shutdown (struct usb_serial *serial)
331{
332 dbg ("%s", __FUNCTION__);
333}
334
335
336static int __init omninet_init (void)
337{
338 int retval;
339 retval = usb_serial_register(&zyxel_omninet_device);
340 if (retval)
341 goto failed_usb_serial_register;
342 retval = usb_register(&omninet_driver);
343 if (retval)
344 goto failed_usb_register;
345 info(DRIVER_VERSION ":" DRIVER_DESC);
346 return 0;
347failed_usb_register:
348 usb_serial_deregister(&zyxel_omninet_device);
349failed_usb_serial_register:
350 return retval;
351}
352
353
354static void __exit omninet_exit (void)
355{
356 usb_deregister (&omninet_driver);
357 usb_serial_deregister (&zyxel_omninet_device);
358}
359
360
361module_init(omninet_init);
362module_exit(omninet_exit);
363
364MODULE_AUTHOR( DRIVER_AUTHOR );
365MODULE_DESCRIPTION( DRIVER_DESC );
366MODULE_LICENSE("GPL");
367
368module_param(debug, bool, S_IRUGO | S_IWUSR);
369MODULE_PARM_DESC(debug, "Debug enabled or not");