blob: 1e1cafe287e4e63f7b7c23a18fd9b66965228e2e [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
77/* The protocol.
78 *
79 * The omni.net always exchange 64 bytes of data with the host. The first
80 * four bytes are the control header, you can see it in the above structure.
81 *
82 * oh_seq is a sequence number. Don't know if/how it's used.
83 * oh_len is the length of the data bytes in the packet.
84 * oh_xxx Bit-mapped, related to handshaking and status info.
85 * I normally set it to 0x03 in trasmitted frames.
86 * 7: Active when the TA is in a CONNECTed state.
87 * 6: unknown
88 * 5: handshaking, unknown
89 * 4: handshaking, unknown
90 * 3: unknown, usually 0
91 * 2: unknown, usually 0
92 * 1: handshaking, unknown, usually set to 1 in trasmitted frames
93 * 0: handshaking, unknown, usually set to 1 in trasmitted frames
94 * oh_pad Probably a pad byte.
95 *
96 * After the header you will find data bytes if oh_len was greater than zero.
97 *
98 */
99
Alan Coxf89d0df2008-07-22 11:15:54 +0100100struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 __u8 oh_seq;
102 __u8 oh_len;
103 __u8 oh_xxx;
104 __u8 oh_pad;
105};
106
Alan Coxf89d0df2008-07-22 11:15:54 +0100107struct omninet_data {
108 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200111static int omninet_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200113 struct omninet_data *od;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Alan Coxf89d0df2008-07-22 11:15:54 +0100115 od = kmalloc(sizeof(struct omninet_data), GFP_KERNEL);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200116 if (!od)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -ENOMEM;
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 usb_set_serial_port_data(port, od);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200120
121 return 0;
122}
123
124static int omninet_port_remove(struct usb_serial_port *port)
125{
126 struct omninet_data *od;
127
128 od = usb_get_serial_port_data(port);
129 kfree(od);
130
Oliver Neukum5ec18622007-03-27 16:02:34 +0200131 return 0;
132}
133
Alan Coxa509a7e2009-09-19 13:13:26 -0700134static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
Oliver Neukum5ec18622007-03-27 16:02:34 +0200135{
136 struct usb_serial *serial = port->serial;
137 struct usb_serial_port *wport;
Oliver Neukum5ec18622007-03-27 16:02:34 +0200138 int result = 0;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 wport = serial->port[1];
Alan Cox4a90f092008-10-13 10:39:46 +0100141 tty_port_tty_set(&wport->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* Start reading from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
Alan Coxf89d0df2008-07-22 11:15:54 +0100145 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700146 dev_err(&port->dev,
147 "%s - failed submitting read urb, error %d\n",
148 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return result;
150}
151
Alan Cox335f8512009-06-11 12:26:29 +0100152static void omninet_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157
158#define OMNINET_DATAOFFSET 0x04
159#define OMNINET_HEADERLEN sizeof(struct omninet_header)
160#define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
161
Alan Coxf89d0df2008-07-22 11:15:54 +0100162static void omninet_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Ming Leicdc97792008-02-24 18:41:47 +0800164 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 unsigned char *data = urb->transfer_buffer;
166 struct omninet_header *header = (struct omninet_header *) &data[0];
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700167 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int result;
169
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700170 if (status) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700171 dev_dbg(&port->dev, "%s - nonzero read bulk status received: %d\n",
172 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
174 }
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (urb->actual_length && header->oh_len) {
Jiri Slaby2e124b42013-01-03 15:53:06 +0100177 tty_insert_flip_string(&port->port, data + OMNINET_DATAOFFSET,
178 header->oh_len);
179 tty_flip_buffer_push(&port->port);
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-Hartman7b5ba2752012-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-Hartman7b5ba2752012-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-Hartman7b5ba2752012-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-Hartman7b5ba2752012-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
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700282module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Alan Coxf89d0df2008-07-22 11:15:54 +0100284MODULE_AUTHOR(DRIVER_AUTHOR);
285MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286MODULE_LICENSE("GPL");