blob: ec16b92d52b3fdf65fb96ed34b65052846ec940a [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#define DRIVER_AUTHOR "Alessandro Zummo"
27#define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
28
29#define ZYXEL_VENDOR_ID 0x0586
30#define ZYXEL_OMNINET_ID 0x1000
Alan Coxf89d0df2008-07-22 11:15:54 +010031/* This one seems to be a re-branded ZyXEL device */
32#define BT_IGNITIONPRO_ID 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* function prototypes */
Alan Coxa509a7e2009-09-19 13:13:26 -070035static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010036static void omninet_close(struct usb_serial_port *port);
Alan Coxf89d0df2008-07-22 11:15:54 +010037static void omninet_read_bulk_callback(struct urb *urb);
38static void omninet_write_bulk_callback(struct urb *urb);
39static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
40 const unsigned char *buf, int count);
41static int omninet_write_room(struct tty_struct *tty);
Alan Sternf9c99bb2009-06-02 11:53:55 -040042static void omninet_disconnect(struct usb_serial *serial);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020043static int omninet_port_probe(struct usb_serial_port *port);
44static int omninet_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Németh Márton7d40d7e2010-01-10 15:34:24 +010046static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
48 { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
49 { } /* Terminating entry */
50};
Alan Coxf89d0df2008-07-22 11:15:54 +010051MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070053static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070054 .driver = {
55 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070056 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070057 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070058 .description = "ZyXEL - omni.net lcd plus usb",
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .num_ports = 1,
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020061 .port_probe = omninet_port_probe,
62 .port_remove = omninet_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .open = omninet_open,
64 .close = omninet_close,
65 .write = omninet_write,
66 .write_room = omninet_write_room,
67 .read_bulk_callback = omninet_read_bulk_callback,
68 .write_bulk_callback = omninet_write_bulk_callback,
Alan Sternf9c99bb2009-06-02 11:53:55 -040069 .disconnect = omninet_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Alan Sternf667dda2012-02-23 14:57:18 -050072static struct usb_serial_driver * const serial_drivers[] = {
73 &zyxel_omninet_device, NULL
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Johan Hovoldb42abbc2013-04-16 18:01:20 +020077/*
78 * The protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
80 * The omni.net always exchange 64 bytes of data with the host. The first
Johan Hovoldb42abbc2013-04-16 18:01:20 +020081 * four bytes are the control header.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
83 * oh_seq is a sequence number. Don't know if/how it's used.
84 * oh_len is the length of the data bytes in the packet.
85 * oh_xxx Bit-mapped, related to handshaking and status info.
Johan Hovoldb42abbc2013-04-16 18:01:20 +020086 * I normally set it to 0x03 in transmitted frames.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * 7: Active when the TA is in a CONNECTed state.
88 * 6: unknown
89 * 5: handshaking, unknown
90 * 4: handshaking, unknown
91 * 3: unknown, usually 0
92 * 2: unknown, usually 0
Johan Hovoldb42abbc2013-04-16 18:01:20 +020093 * 1: handshaking, unknown, usually set to 1 in transmitted frames
94 * 0: handshaking, unknown, usually set to 1 in transmitted frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * oh_pad Probably a pad byte.
96 *
97 * After the header you will find data bytes if oh_len was greater than zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Alan Coxf89d0df2008-07-22 11:15:54 +010099struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 __u8 oh_seq;
101 __u8 oh_len;
102 __u8 oh_xxx;
103 __u8 oh_pad;
104};
105
Alan Coxf89d0df2008-07-22 11:15:54 +0100106struct omninet_data {
107 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200110static int omninet_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200112 struct omninet_data *od;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Johan Hovold81f58c62013-04-16 18:01:19 +0200114 od = kzalloc(sizeof(*od), GFP_KERNEL);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200115 if (!od)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return -ENOMEM;
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 usb_set_serial_port_data(port, od);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200119
120 return 0;
121}
122
123static int omninet_port_remove(struct usb_serial_port *port)
124{
125 struct omninet_data *od;
126
127 od = usb_get_serial_port_data(port);
128 kfree(od);
129
Oliver Neukum5ec18622007-03-27 16:02:34 +0200130 return 0;
131}
132
Alan Coxa509a7e2009-09-19 13:13:26 -0700133static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
Oliver Neukum5ec18622007-03-27 16:02:34 +0200134{
135 struct usb_serial *serial = port->serial;
136 struct usb_serial_port *wport;
Oliver Neukum5ec18622007-03-27 16:02:34 +0200137 int result = 0;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 wport = serial->port[1];
Alan Cox4a90f092008-10-13 10:39:46 +0100140 tty_port_tty_set(&wport->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* Start reading from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Alan Coxf89d0df2008-07-22 11:15:54 +0100144 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700145 dev_err(&port->dev,
146 "%s - failed submitting read urb, error %d\n",
147 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return result;
149}
150
Alan Cox335f8512009-06-11 12:26:29 +0100151static void omninet_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156
157#define OMNINET_DATAOFFSET 0x04
158#define OMNINET_HEADERLEN sizeof(struct omninet_header)
159#define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
160
Alan Coxf89d0df2008-07-22 11:15:54 +0100161static void omninet_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Ming Leicdc97792008-02-24 18:41:47 +0800163 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned char *data = urb->transfer_buffer;
165 struct omninet_header *header = (struct omninet_header *) &data[0];
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700166 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int result;
168
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700169 if (status) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700170 dev_dbg(&port->dev, "%s - nonzero read bulk status received: %d\n",
171 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return;
173 }
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (urb->actual_length && header->oh_len) {
Jiri Slaby2e124b42013-01-03 15:53:06 +0100176 tty_insert_flip_string(&port->port, data + OMNINET_DATAOFFSET,
177 header->oh_len);
178 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180
181 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 result = usb_submit_urb(urb, GFP_ATOMIC);
183 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700184 dev_err(&port->dev,
185 "%s - failed resubmitting read urb, error %d\n",
186 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Alan Cox95da3102008-07-22 11:09:07 +0100189static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
190 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Alan Coxf89d0df2008-07-22 11:15:54 +0100192 struct usb_serial *serial = port->serial;
193 struct usb_serial_port *wport = serial->port[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Alan Coxf89d0df2008-07-22 11:15:54 +0100195 struct omninet_data *od = usb_get_serial_port_data(port);
196 struct omninet_header *header = (struct omninet_header *)
197 wport->write_urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 int result;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (count == 0) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700202 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxf89d0df2008-07-22 11:15:54 +0100203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700205
Johan Hovold120f9db2011-11-06 19:06:22 +0100206 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700207 dev_dbg(&port->dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
212
Alan Coxf89d0df2008-07-22 11:15:54 +0100213 memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
214 buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100216 usb_serial_debug_data(&port->dev, __func__, count,
217 wport->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 header->oh_seq = od->od_outseq++;
220 header->oh_len = count;
221 header->oh_xxx = 0x03;
222 header->oh_pad = 0x00;
223
224 /* send the data out the bulk port, always 64 bytes */
225 wport->write_urb->transfer_buffer_length = 64;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700228 if (result) {
Johan Hovold120f9db2011-11-06 19:06:22 +0100229 set_bit(0, &wport->write_urbs_free);
Johan Hovold22a416c2012-02-10 13:20:51 +0100230 dev_err_console(port,
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700231 "%s - failed submitting write urb, error %d\n",
232 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700233 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 result = count;
235
236 return result;
237}
238
239
Alan Coxf89d0df2008-07-22 11:15:54 +0100240static int omninet_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Alan Cox95da3102008-07-22 11:09:07 +0100242 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct usb_serial *serial = port->serial;
244 struct usb_serial_port *wport = serial->port[1];
245
Alan Coxa5b6f602008-04-08 17:16:06 +0100246 int room = 0; /* Default: no room */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Johan Hovold120f9db2011-11-06 19:06:22 +0100248 if (test_bit(0, &wport->write_urbs_free))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 room = wport->bulk_out_size - OMNINET_HEADERLEN;
250
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700251 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Alan Coxf89d0df2008-07-22 11:15:54 +0100253 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Alan Coxf89d0df2008-07-22 11:15:54 +0100256static void omninet_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Alan Coxf89d0df2008-07-22 11:15:54 +0100258/* struct omninet_header *header = (struct omninet_header *)
259 urb->transfer_buffer; */
Ming Leicdc97792008-02-24 18:41:47 +0800260 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700261 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Johan Hovold120f9db2011-11-06 19:06:22 +0100263 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700264 if (status) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700265 dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
266 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return;
268 }
269
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700270 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273
Alan Sternf9c99bb2009-06-02 11:53:55 -0400274static void omninet_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200276 struct usb_serial_port *wport = serial->port[1];
Alan Sternf9c99bb2009-06-02 11:53:55 -0400277
Oliver Neukum5ec18622007-03-27 16:02:34 +0200278 usb_kill_urb(wport->write_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400279}
280
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700281module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Alan Coxf89d0df2008-07-22 11:15:54 +0100283MODULE_AUTHOR(DRIVER_AUTHOR);
284MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285MODULE_LICENSE("GPL");