blob: 6def58b79382f97511929b2c3fc8e386c71ffa51 [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);
47static void omninet_release(struct usb_serial *serial);
Alan Coxf89d0df2008-07-22 11:15:54 +010048static int omninet_attach(struct usb_serial *serial);
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,
Oliver Neukum5ec18622007-03-27 16:02:34 +020065 .attach = omninet_attach,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .open = omninet_open,
67 .close = omninet_close,
68 .write = omninet_write,
69 .write_room = omninet_write_room,
70 .read_bulk_callback = omninet_read_bulk_callback,
71 .write_bulk_callback = omninet_write_bulk_callback,
Alan Sternf9c99bb2009-06-02 11:53:55 -040072 .disconnect = omninet_disconnect,
73 .release = omninet_release,
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
Alan Coxf89d0df2008-07-22 11:15:54 +0100115static int omninet_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200117 struct omninet_data *od;
118 struct usb_serial_port *port = serial->port[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Alan Coxf89d0df2008-07-22 11:15:54 +0100120 od = kmalloc(sizeof(struct omninet_data), GFP_KERNEL);
121 if (!od) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700122 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n",
123 __func__, sizeof(struct omninet_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -ENOMEM;
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 usb_set_serial_port_data(port, od);
Oliver Neukum5ec18622007-03-27 16:02:34 +0200127 return 0;
128}
129
Alan Coxa509a7e2009-09-19 13:13:26 -0700130static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
Oliver Neukum5ec18622007-03-27 16:02:34 +0200131{
132 struct usb_serial *serial = port->serial;
133 struct usb_serial_port *wport;
Oliver Neukum5ec18622007-03-27 16:02:34 +0200134 int result = 0;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 wport = serial->port[1];
Alan Cox4a90f092008-10-13 10:39:46 +0100137 tty_port_tty_set(&wport->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Start reading from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Alan Coxf89d0df2008-07-22 11:15:54 +0100141 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700142 dev_err(&port->dev,
143 "%s - failed submitting read urb, error %d\n",
144 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return result;
146}
147
Alan Cox335f8512009-06-11 12:26:29 +0100148static void omninet_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153
154#define OMNINET_DATAOFFSET 0x04
155#define OMNINET_HEADERLEN sizeof(struct omninet_header)
156#define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
157
Alan Coxf89d0df2008-07-22 11:15:54 +0100158static void omninet_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Ming Leicdc97792008-02-24 18:41:47 +0800160 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned char *data = urb->transfer_buffer;
162 struct omninet_header *header = (struct omninet_header *) &data[0];
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700163 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int result;
165
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700166 if (status) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700167 dev_dbg(&port->dev, "%s - nonzero read bulk status received: %d\n",
168 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return;
170 }
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (urb->actual_length && header->oh_len) {
Alan Cox4a90f092008-10-13 10:39:46 +0100173 struct tty_struct *tty = tty_port_tty_get(&port->port);
Alexey Khoroshilov83216522012-09-13 22:56:55 +0400174 if (tty) {
175 tty_insert_flip_string(tty, data + OMNINET_DATAOFFSET,
Alan Cox4a90f092008-10-13 10:39:46 +0100176 header->oh_len);
Alexey Khoroshilov83216522012-09-13 22:56:55 +0400177 tty_flip_buffer_push(tty);
178 tty_kref_put(tty);
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 result = usb_submit_urb(urb, GFP_ATOMIC);
184 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700185 dev_err(&port->dev,
186 "%s - failed resubmitting read urb, error %d\n",
187 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Alan Cox95da3102008-07-22 11:09:07 +0100190static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
191 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Alan Coxf89d0df2008-07-22 11:15:54 +0100193 struct usb_serial *serial = port->serial;
194 struct usb_serial_port *wport = serial->port[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Alan Coxf89d0df2008-07-22 11:15:54 +0100196 struct omninet_data *od = usb_get_serial_port_data(port);
197 struct omninet_header *header = (struct omninet_header *)
198 wport->write_urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 int result;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (count == 0) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700203 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxf89d0df2008-07-22 11:15:54 +0100204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700206
Johan Hovold120f9db2011-11-06 19:06:22 +0100207 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700208 dev_dbg(&port->dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
213
Alan Coxf89d0df2008-07-22 11:15:54 +0100214 memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
215 buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100217 usb_serial_debug_data(&port->dev, __func__, count,
218 wport->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 header->oh_seq = od->od_outseq++;
221 header->oh_len = count;
222 header->oh_xxx = 0x03;
223 header->oh_pad = 0x00;
224
225 /* send the data out the bulk port, always 64 bytes */
226 wport->write_urb->transfer_buffer_length = 64;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700229 if (result) {
Johan Hovold120f9db2011-11-06 19:06:22 +0100230 set_bit(0, &wport->write_urbs_free);
Johan Hovold22a416c2012-02-10 13:20:51 +0100231 dev_err_console(port,
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700232 "%s - failed submitting write urb, error %d\n",
233 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700234 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 result = count;
236
237 return result;
238}
239
240
Alan Coxf89d0df2008-07-22 11:15:54 +0100241static int omninet_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Alan Cox95da3102008-07-22 11:09:07 +0100243 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct usb_serial *serial = port->serial;
245 struct usb_serial_port *wport = serial->port[1];
246
Alan Coxa5b6f602008-04-08 17:16:06 +0100247 int room = 0; /* Default: no room */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Johan Hovold120f9db2011-11-06 19:06:22 +0100249 if (test_bit(0, &wport->write_urbs_free))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 room = wport->bulk_out_size - OMNINET_HEADERLEN;
251
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700252 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Alan Coxf89d0df2008-07-22 11:15:54 +0100254 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Alan Coxf89d0df2008-07-22 11:15:54 +0100257static void omninet_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Alan Coxf89d0df2008-07-22 11:15:54 +0100259/* struct omninet_header *header = (struct omninet_header *)
260 urb->transfer_buffer; */
Ming Leicdc97792008-02-24 18:41:47 +0800261 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700262 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Johan Hovold120f9db2011-11-06 19:06:22 +0100264 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700265 if (status) {
Greg Kroah-Hartman7b5ba272012-09-13 17:41:48 -0700266 dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
267 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return;
269 }
270
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700271 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274
Alan Sternf9c99bb2009-06-02 11:53:55 -0400275static void omninet_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200277 struct usb_serial_port *wport = serial->port[1];
Alan Sternf9c99bb2009-06-02 11:53:55 -0400278
Oliver Neukum5ec18622007-03-27 16:02:34 +0200279 usb_kill_urb(wport->write_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400280}
281
282
283static void omninet_release(struct usb_serial *serial)
284{
285 struct usb_serial_port *port = serial->port[0];
286
Oliver Neukum5ec18622007-03-27 16:02:34 +0200287 kfree(usb_get_serial_port_data(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
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");