blob: f6c6900bccf01c87c88734b2a7417db9f0fade8d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/tty.h>
18#include <linux/tty_driver.h>
19#include <linux/tty_flip.h>
20#include <linux/module.h>
Alan Coxf89d0df2008-07-22 11:15:54 +010021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070023#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define DRIVER_AUTHOR "Alessandro Zummo"
26#define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
27
28#define ZYXEL_VENDOR_ID 0x0586
29#define ZYXEL_OMNINET_ID 0x1000
Alan Coxf89d0df2008-07-22 11:15:54 +010030/* This one seems to be a re-branded ZyXEL device */
31#define BT_IGNITIONPRO_ID 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* function prototypes */
Alan Coxa509a7e2009-09-19 13:13:26 -070034static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port);
Johan Hovoldfbf94772013-04-16 18:01:23 +020035static void omninet_process_read_urb(struct urb *urb);
Alan Coxf89d0df2008-07-22 11:15:54 +010036static void omninet_write_bulk_callback(struct urb *urb);
37static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
38 const unsigned char *buf, int count);
39static int omninet_write_room(struct tty_struct *tty);
Alan Sternf9c99bb2009-06-02 11:53:55 -040040static void omninet_disconnect(struct usb_serial *serial);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020041static int omninet_port_probe(struct usb_serial_port *port);
42static int omninet_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Németh Márton7d40d7e2010-01-10 15:34:24 +010044static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
46 { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
47 { } /* Terminating entry */
48};
Alan Coxf89d0df2008-07-22 11:15:54 +010049MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070051static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070052 .driver = {
53 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070054 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070055 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070056 .description = "ZyXEL - omni.net lcd plus usb",
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 .num_ports = 1,
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020059 .port_probe = omninet_port_probe,
60 .port_remove = omninet_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .open = omninet_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .write = omninet_write,
63 .write_room = omninet_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .write_bulk_callback = omninet_write_bulk_callback,
Johan Hovoldfbf94772013-04-16 18:01:23 +020065 .process_read_urb = omninet_process_read_urb,
Alan Sternf9c99bb2009-06-02 11:53:55 -040066 .disconnect = omninet_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Alan Sternf667dda2012-02-23 14:57:18 -050069static struct usb_serial_driver * const serial_drivers[] = {
70 &zyxel_omninet_device, NULL
71};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Johan Hovoldb42abbc2013-04-16 18:01:20 +020074/*
75 * The protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * The omni.net always exchange 64 bytes of data with the host. The first
Johan Hovoldb42abbc2013-04-16 18:01:20 +020078 * four bytes are the control header.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
80 * oh_seq is a sequence number. Don't know if/how it's used.
81 * oh_len is the length of the data bytes in the packet.
82 * oh_xxx Bit-mapped, related to handshaking and status info.
Johan Hovoldb42abbc2013-04-16 18:01:20 +020083 * I normally set it to 0x03 in transmitted frames.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * 7: Active when the TA is in a CONNECTed state.
85 * 6: unknown
86 * 5: handshaking, unknown
87 * 4: handshaking, unknown
88 * 3: unknown, usually 0
89 * 2: unknown, usually 0
Johan Hovoldb42abbc2013-04-16 18:01:20 +020090 * 1: handshaking, unknown, usually set to 1 in transmitted frames
91 * 0: handshaking, unknown, usually set to 1 in transmitted frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * oh_pad Probably a pad byte.
93 *
94 * After the header you will find data bytes if oh_len was greater than zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Alan Coxf89d0df2008-07-22 11:15:54 +010096struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 __u8 oh_seq;
98 __u8 oh_len;
99 __u8 oh_xxx;
100 __u8 oh_pad;
101};
102
Alan Coxf89d0df2008-07-22 11:15:54 +0100103struct omninet_data {
104 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200107static int omninet_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200109 struct omninet_data *od;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Johan Hovold81f58c62013-04-16 18:01:19 +0200111 od = kzalloc(sizeof(*od), GFP_KERNEL);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200112 if (!od)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -ENOMEM;
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 usb_set_serial_port_data(port, od);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200116
117 return 0;
118}
119
120static int omninet_port_remove(struct usb_serial_port *port)
121{
122 struct omninet_data *od;
123
124 od = usb_get_serial_port_data(port);
125 kfree(od);
126
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 wport = serial->port[1];
Alan Cox4a90f092008-10-13 10:39:46 +0100136 tty_port_tty_set(&wport->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Johan Hovoldfbf94772013-04-16 18:01:23 +0200138 return usb_serial_generic_open(tty, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Johan Hovoldd91641b2013-04-16 18:01:21 +0200141#define OMNINET_HEADERLEN 4
142#define OMNINET_BULKOUTSIZE 64
143#define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Johan Hovolda6c042f2013-04-16 18:01:22 +0200145static void omninet_process_read_urb(struct urb *urb)
146{
147 struct usb_serial_port *port = urb->context;
148 const struct omninet_header *hdr = urb->transfer_buffer;
149 const unsigned char *data;
150 size_t data_len;
151
152 if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
153 return;
154
155 data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
156 data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
157 hdr->oh_len);
158 tty_insert_flip_string(&port->port, data, data_len);
159 tty_flip_buffer_push(&port->port);
160}
161
Alan Cox95da3102008-07-22 11:09:07 +0100162static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
163 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Alan Coxf89d0df2008-07-22 11:15:54 +0100165 struct usb_serial *serial = port->serial;
166 struct usb_serial_port *wport = serial->port[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Alan Coxf89d0df2008-07-22 11:15:54 +0100168 struct omninet_data *od = usb_get_serial_port_data(port);
169 struct omninet_header *header = (struct omninet_header *)
170 wport->write_urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 int result;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (count == 0) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700175 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxf89d0df2008-07-22 11:15:54 +0100176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700178
Johan Hovold120f9db2011-11-06 19:06:22 +0100179 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700180 dev_dbg(&port->dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
Johan Hovoldd91641b2013-04-16 18:01:21 +0200184 count = (count > OMNINET_PAYLOADSIZE) ? OMNINET_PAYLOADSIZE : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Johan Hovoldd91641b2013-04-16 18:01:21 +0200186 memcpy(wport->write_urb->transfer_buffer + OMNINET_HEADERLEN,
Alan Coxf89d0df2008-07-22 11:15:54 +0100187 buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100189 usb_serial_debug_data(&port->dev, __func__, count,
190 wport->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 header->oh_seq = od->od_outseq++;
193 header->oh_len = count;
194 header->oh_xxx = 0x03;
195 header->oh_pad = 0x00;
196
197 /* send the data out the bulk port, always 64 bytes */
Johan Hovoldd91641b2013-04-16 18:01:21 +0200198 wport->write_urb->transfer_buffer_length = OMNINET_BULKOUTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700201 if (result) {
Johan Hovold120f9db2011-11-06 19:06:22 +0100202 set_bit(0, &wport->write_urbs_free);
Johan Hovold22a416c2012-02-10 13:20:51 +0100203 dev_err_console(port,
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700204 "%s - failed submitting write urb, error %d\n",
205 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700206 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 result = count;
208
209 return result;
210}
211
212
Alan Coxf89d0df2008-07-22 11:15:54 +0100213static int omninet_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Alan Cox95da3102008-07-22 11:09:07 +0100215 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct usb_serial *serial = port->serial;
217 struct usb_serial_port *wport = serial->port[1];
218
Alan Coxa5b6f602008-04-08 17:16:06 +0100219 int room = 0; /* Default: no room */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Johan Hovold120f9db2011-11-06 19:06:22 +0100221 if (test_bit(0, &wport->write_urbs_free))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 room = wport->bulk_out_size - OMNINET_HEADERLEN;
223
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700224 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Alan Coxf89d0df2008-07-22 11:15:54 +0100226 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Alan Coxf89d0df2008-07-22 11:15:54 +0100229static void omninet_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Alan Coxf89d0df2008-07-22 11:15:54 +0100231/* struct omninet_header *header = (struct omninet_header *)
232 urb->transfer_buffer; */
Ming Leicdc97792008-02-24 18:41:47 +0800233 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700234 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Johan Hovold120f9db2011-11-06 19:06:22 +0100236 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700237 if (status) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700238 dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
239 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return;
241 }
242
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700243 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246
Alan Sternf9c99bb2009-06-02 11:53:55 -0400247static void omninet_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200249 struct usb_serial_port *wport = serial->port[1];
Alan Sternf9c99bb2009-06-02 11:53:55 -0400250
Oliver Neukum5ec18622007-03-27 16:02:34 +0200251 usb_kill_urb(wport->write_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400252}
253
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700254module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Alan Coxf89d0df2008-07-22 11:15:54 +0100256MODULE_AUTHOR(DRIVER_AUTHOR);
257MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258MODULE_LICENSE("GPL");