blob: 9ab73d2957746d49292357382f7a081faa25cd55 [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 *
Alan Coxf89d0df2008-07-22 11:15:54 +01008 * See Documentation/usb/usb-serial.txt for more information on using this
9 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Please report both successes and troubles to the author at omninet@kroah.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/tty.h>
19#include <linux/tty_driver.h>
20#include <linux/tty_flip.h>
21#include <linux/module.h>
Alan Coxf89d0df2008-07-22 11:15:54 +010022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070024#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/*
27 * Version Information
28 */
29#define DRIVER_VERSION "v1.1"
30#define DRIVER_AUTHOR "Alessandro Zummo"
31#define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
32
33#define ZYXEL_VENDOR_ID 0x0586
34#define ZYXEL_OMNINET_ID 0x1000
Alan Coxf89d0df2008-07-22 11:15:54 +010035/* This one seems to be a re-branded ZyXEL device */
36#define BT_IGNITIONPRO_ID 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* function prototypes */
Alan Coxa509a7e2009-09-19 13:13:26 -070039static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010040static void omninet_close(struct usb_serial_port *port);
Alan Coxf89d0df2008-07-22 11:15:54 +010041static void omninet_read_bulk_callback(struct urb *urb);
42static void omninet_write_bulk_callback(struct urb *urb);
43static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
44 const unsigned char *buf, int count);
45static int omninet_write_room(struct tty_struct *tty);
Alan Sternf9c99bb2009-06-02 11:53:55 -040046static void omninet_disconnect(struct usb_serial *serial);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020047static int omninet_port_probe(struct usb_serial_port *port);
48static int omninet_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Németh Márton7d40d7e2010-01-10 15:34:24 +010050static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
52 { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
53 { } /* Terminating entry */
54};
Alan Coxf89d0df2008-07-22 11:15:54 +010055MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070057static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070058 .driver = {
59 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070060 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070061 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070062 .description = "ZyXEL - omni.net lcd plus usb",
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .num_ports = 1,
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020065 .port_probe = omninet_port_probe,
66 .port_remove = omninet_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .open = omninet_open,
68 .close = omninet_close,
69 .write = omninet_write,
70 .write_room = omninet_write_room,
71 .read_bulk_callback = omninet_read_bulk_callback,
72 .write_bulk_callback = omninet_write_bulk_callback,
Alan Sternf9c99bb2009-06-02 11:53:55 -040073 .disconnect = omninet_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Alan Sternf667dda2012-02-23 14:57:18 -050076static struct usb_serial_driver * const serial_drivers[] = {
77 &zyxel_omninet_device, NULL
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* The protocol.
82 *
83 * The omni.net always exchange 64 bytes of data with the host. The first
84 * four bytes are the control header, you can see it in the above structure.
85 *
86 * oh_seq is a sequence number. Don't know if/how it's used.
87 * oh_len is the length of the data bytes in the packet.
88 * oh_xxx Bit-mapped, related to handshaking and status info.
89 * I normally set it to 0x03 in trasmitted frames.
90 * 7: Active when the TA is in a CONNECTed state.
91 * 6: unknown
92 * 5: handshaking, unknown
93 * 4: handshaking, unknown
94 * 3: unknown, usually 0
95 * 2: unknown, usually 0
96 * 1: handshaking, unknown, usually set to 1 in trasmitted frames
97 * 0: handshaking, unknown, usually set to 1 in trasmitted frames
98 * oh_pad Probably a pad byte.
99 *
100 * After the header you will find data bytes if oh_len was greater than zero.
101 *
102 */
103
Alan Coxf89d0df2008-07-22 11:15:54 +0100104struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 __u8 oh_seq;
106 __u8 oh_len;
107 __u8 oh_xxx;
108 __u8 oh_pad;
109};
110
Alan Coxf89d0df2008-07-22 11:15:54 +0100111struct omninet_data {
112 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200115static int omninet_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200117 struct omninet_data *od;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Alan Coxf89d0df2008-07-22 11:15:54 +0100119 od = kmalloc(sizeof(struct omninet_data), GFP_KERNEL);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200120 if (!od)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -ENOMEM;
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 usb_set_serial_port_data(port, od);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200124
125 return 0;
126}
127
128static int omninet_port_remove(struct usb_serial_port *port)
129{
130 struct omninet_data *od;
131
132 od = usb_get_serial_port_data(port);
133 kfree(od);
134
Oliver Neukum5ec18622007-03-27 16:02:34 +0200135 return 0;
136}
137
Alan Coxa509a7e2009-09-19 13:13:26 -0700138static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
Oliver Neukum5ec18622007-03-27 16:02:34 +0200139{
140 struct usb_serial *serial = port->serial;
141 struct usb_serial_port *wport;
Oliver Neukum5ec18622007-03-27 16:02:34 +0200142 int result = 0;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 wport = serial->port[1];
Alan Cox4a90f092008-10-13 10:39:46 +0100145 tty_port_tty_set(&wport->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* Start reading from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Alan Coxf89d0df2008-07-22 11:15:54 +0100149 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700150 dev_err(&port->dev,
151 "%s - failed submitting read urb, error %d\n",
152 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return result;
154}
155
Alan Cox335f8512009-06-11 12:26:29 +0100156static void omninet_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161
162#define OMNINET_DATAOFFSET 0x04
163#define OMNINET_HEADERLEN sizeof(struct omninet_header)
164#define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
165
Alan Coxf89d0df2008-07-22 11:15:54 +0100166static void omninet_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Ming Leicdc97792008-02-24 18:41:47 +0800168 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned char *data = urb->transfer_buffer;
170 struct omninet_header *header = (struct omninet_header *) &data[0];
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700171 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 int result;
173
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700174 if (status) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700175 dev_dbg(&port->dev, "%s - nonzero read bulk status received: %d\n",
176 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return;
178 }
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (urb->actual_length && header->oh_len) {
Alan Cox4a90f092008-10-13 10:39:46 +0100181 struct tty_struct *tty = tty_port_tty_get(&port->port);
Alexey Khoroshilov83216522012-09-13 22:56:55 +0400182 if (tty) {
183 tty_insert_flip_string(tty, data + OMNINET_DATAOFFSET,
Alan Cox4a90f092008-10-13 10:39:46 +0100184 header->oh_len);
Alexey Khoroshilov83216522012-09-13 22:56:55 +0400185 tty_flip_buffer_push(tty);
186 tty_kref_put(tty);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 result = usb_submit_urb(urb, GFP_ATOMIC);
192 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700193 dev_err(&port->dev,
194 "%s - failed resubmitting read urb, error %d\n",
195 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Alan Cox95da3102008-07-22 11:09:07 +0100198static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
199 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Alan Coxf89d0df2008-07-22 11:15:54 +0100201 struct usb_serial *serial = port->serial;
202 struct usb_serial_port *wport = serial->port[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Alan Coxf89d0df2008-07-22 11:15:54 +0100204 struct omninet_data *od = usb_get_serial_port_data(port);
205 struct omninet_header *header = (struct omninet_header *)
206 wport->write_urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 int result;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (count == 0) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700211 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxf89d0df2008-07-22 11:15:54 +0100212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700214
Johan Hovold120f9db2011-11-06 19:06:22 +0100215 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700216 dev_dbg(&port->dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
220 count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
221
Alan Coxf89d0df2008-07-22 11:15:54 +0100222 memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
223 buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100225 usb_serial_debug_data(&port->dev, __func__, count,
226 wport->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 header->oh_seq = od->od_outseq++;
229 header->oh_len = count;
230 header->oh_xxx = 0x03;
231 header->oh_pad = 0x00;
232
233 /* send the data out the bulk port, always 64 bytes */
234 wport->write_urb->transfer_buffer_length = 64;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700237 if (result) {
Johan Hovold120f9db2011-11-06 19:06:22 +0100238 set_bit(0, &wport->write_urbs_free);
Johan Hovold22a416c2012-02-10 13:20:51 +0100239 dev_err_console(port,
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700240 "%s - failed submitting write urb, error %d\n",
241 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700242 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 result = count;
244
245 return result;
246}
247
248
Alan Coxf89d0df2008-07-22 11:15:54 +0100249static int omninet_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Alan Cox95da3102008-07-22 11:09:07 +0100251 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct usb_serial *serial = port->serial;
253 struct usb_serial_port *wport = serial->port[1];
254
Alan Coxa5b6f602008-04-08 17:16:06 +0100255 int room = 0; /* Default: no room */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Johan Hovold120f9db2011-11-06 19:06:22 +0100257 if (test_bit(0, &wport->write_urbs_free))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 room = wport->bulk_out_size - OMNINET_HEADERLEN;
259
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700260 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Alan Coxf89d0df2008-07-22 11:15:54 +0100262 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Alan Coxf89d0df2008-07-22 11:15:54 +0100265static void omninet_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Alan Coxf89d0df2008-07-22 11:15:54 +0100267/* struct omninet_header *header = (struct omninet_header *)
268 urb->transfer_buffer; */
Ming Leicdc97792008-02-24 18:41:47 +0800269 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700270 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Johan Hovold120f9db2011-11-06 19:06:22 +0100272 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700273 if (status) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700274 dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
275 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return;
277 }
278
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700279 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282
Alan Sternf9c99bb2009-06-02 11:53:55 -0400283static void omninet_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200285 struct usb_serial_port *wport = serial->port[1];
Alan Sternf9c99bb2009-06-02 11:53:55 -0400286
Oliver Neukum5ec18622007-03-27 16:02:34 +0200287 usb_kill_urb(wport->write_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400288}
289
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700290module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Alan Coxf89d0df2008-07-22 11:15:54 +0100292MODULE_AUTHOR(DRIVER_AUTHOR);
293MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294MODULE_LICENSE("GPL");