blob: 5739bf6f7200923afd280219572987a0d9b8f104 [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);
Johan Hovoldfbf94772013-04-16 18:01:23 +020036static void omninet_process_read_urb(struct urb *urb);
Alan Coxf89d0df2008-07-22 11:15:54 +010037static void omninet_write_bulk_callback(struct urb *urb);
38static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
39 const unsigned char *buf, int count);
40static int omninet_write_room(struct tty_struct *tty);
Alan Sternf9c99bb2009-06-02 11:53:55 -040041static void omninet_disconnect(struct usb_serial *serial);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020042static int omninet_port_probe(struct usb_serial_port *port);
43static int omninet_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Németh Márton7d40d7e2010-01-10 15:34:24 +010045static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
47 { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
48 { } /* Terminating entry */
49};
Alan Coxf89d0df2008-07-22 11:15:54 +010050MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070052static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070053 .driver = {
54 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070055 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070056 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070057 .description = "ZyXEL - omni.net lcd plus usb",
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .num_ports = 1,
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020060 .port_probe = omninet_port_probe,
61 .port_remove = omninet_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .open = omninet_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .write = omninet_write,
64 .write_room = omninet_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .write_bulk_callback = omninet_write_bulk_callback,
Johan Hovoldfbf94772013-04-16 18:01:23 +020066 .process_read_urb = omninet_process_read_urb,
Alan Sternf9c99bb2009-06-02 11:53:55 -040067 .disconnect = omninet_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Alan Sternf667dda2012-02-23 14:57:18 -050070static struct usb_serial_driver * const serial_drivers[] = {
71 &zyxel_omninet_device, NULL
72};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Johan Hovoldb42abbc2013-04-16 18:01:20 +020075/*
76 * The protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * The omni.net always exchange 64 bytes of data with the host. The first
Johan Hovoldb42abbc2013-04-16 18:01:20 +020079 * four bytes are the control header.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
81 * oh_seq is a sequence number. Don't know if/how it's used.
82 * oh_len is the length of the data bytes in the packet.
83 * oh_xxx Bit-mapped, related to handshaking and status info.
Johan Hovoldb42abbc2013-04-16 18:01:20 +020084 * I normally set it to 0x03 in transmitted frames.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * 7: Active when the TA is in a CONNECTed state.
86 * 6: unknown
87 * 5: handshaking, unknown
88 * 4: handshaking, unknown
89 * 3: unknown, usually 0
90 * 2: unknown, usually 0
Johan Hovoldb42abbc2013-04-16 18:01:20 +020091 * 1: handshaking, unknown, usually set to 1 in transmitted frames
92 * 0: handshaking, unknown, usually set to 1 in transmitted frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * oh_pad Probably a pad byte.
94 *
95 * After the header you will find data bytes if oh_len was greater than zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Alan Coxf89d0df2008-07-22 11:15:54 +010097struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 __u8 oh_seq;
99 __u8 oh_len;
100 __u8 oh_xxx;
101 __u8 oh_pad;
102};
103
Alan Coxf89d0df2008-07-22 11:15:54 +0100104struct omninet_data {
105 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200108static int omninet_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200110 struct omninet_data *od;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Johan Hovold81f58c62013-04-16 18:01:19 +0200112 od = kzalloc(sizeof(*od), GFP_KERNEL);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200113 if (!od)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -ENOMEM;
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 usb_set_serial_port_data(port, od);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200117
118 return 0;
119}
120
121static int omninet_port_remove(struct usb_serial_port *port)
122{
123 struct omninet_data *od;
124
125 od = usb_get_serial_port_data(port);
126 kfree(od);
127
Oliver Neukum5ec18622007-03-27 16:02:34 +0200128 return 0;
129}
130
Alan Coxa509a7e2009-09-19 13:13:26 -0700131static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
Oliver Neukum5ec18622007-03-27 16:02:34 +0200132{
133 struct usb_serial *serial = port->serial;
134 struct usb_serial_port *wport;
Oliver Neukum5ec18622007-03-27 16:02:34 +0200135
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
Johan Hovoldfbf94772013-04-16 18:01:23 +0200139 return usb_serial_generic_open(tty, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Johan Hovoldd91641b2013-04-16 18:01:21 +0200142#define OMNINET_HEADERLEN 4
143#define OMNINET_BULKOUTSIZE 64
144#define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Johan Hovolda6c042f2013-04-16 18:01:22 +0200146static void omninet_process_read_urb(struct urb *urb)
147{
148 struct usb_serial_port *port = urb->context;
149 const struct omninet_header *hdr = urb->transfer_buffer;
150 const unsigned char *data;
151 size_t data_len;
152
153 if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
154 return;
155
156 data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
157 data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
158 hdr->oh_len);
159 tty_insert_flip_string(&port->port, data, data_len);
160 tty_flip_buffer_push(&port->port);
161}
162
Alan Cox95da3102008-07-22 11:09:07 +0100163static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
164 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Alan Coxf89d0df2008-07-22 11:15:54 +0100166 struct usb_serial *serial = port->serial;
167 struct usb_serial_port *wport = serial->port[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Alan Coxf89d0df2008-07-22 11:15:54 +0100169 struct omninet_data *od = usb_get_serial_port_data(port);
170 struct omninet_header *header = (struct omninet_header *)
171 wport->write_urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 int result;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (count == 0) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700176 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxf89d0df2008-07-22 11:15:54 +0100177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700179
Johan Hovold120f9db2011-11-06 19:06:22 +0100180 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700181 dev_dbg(&port->dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
Johan Hovoldd91641b2013-04-16 18:01:21 +0200185 count = (count > OMNINET_PAYLOADSIZE) ? OMNINET_PAYLOADSIZE : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Johan Hovoldd91641b2013-04-16 18:01:21 +0200187 memcpy(wport->write_urb->transfer_buffer + OMNINET_HEADERLEN,
Alan Coxf89d0df2008-07-22 11:15:54 +0100188 buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100190 usb_serial_debug_data(&port->dev, __func__, count,
191 wport->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 header->oh_seq = od->od_outseq++;
194 header->oh_len = count;
195 header->oh_xxx = 0x03;
196 header->oh_pad = 0x00;
197
198 /* send the data out the bulk port, always 64 bytes */
Johan Hovoldd91641b2013-04-16 18:01:21 +0200199 wport->write_urb->transfer_buffer_length = OMNINET_BULKOUTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700202 if (result) {
Johan Hovold120f9db2011-11-06 19:06:22 +0100203 set_bit(0, &wport->write_urbs_free);
Johan Hovold22a416c2012-02-10 13:20:51 +0100204 dev_err_console(port,
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700205 "%s - failed submitting write urb, error %d\n",
206 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700207 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 result = count;
209
210 return result;
211}
212
213
Alan Coxf89d0df2008-07-22 11:15:54 +0100214static int omninet_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Alan Cox95da3102008-07-22 11:09:07 +0100216 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct usb_serial *serial = port->serial;
218 struct usb_serial_port *wport = serial->port[1];
219
Alan Coxa5b6f602008-04-08 17:16:06 +0100220 int room = 0; /* Default: no room */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Johan Hovold120f9db2011-11-06 19:06:22 +0100222 if (test_bit(0, &wport->write_urbs_free))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 room = wport->bulk_out_size - OMNINET_HEADERLEN;
224
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700225 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alan Coxf89d0df2008-07-22 11:15:54 +0100227 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Alan Coxf89d0df2008-07-22 11:15:54 +0100230static void omninet_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Alan Coxf89d0df2008-07-22 11:15:54 +0100232/* struct omninet_header *header = (struct omninet_header *)
233 urb->transfer_buffer; */
Ming Leicdc97792008-02-24 18:41:47 +0800234 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700235 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Johan Hovold120f9db2011-11-06 19:06:22 +0100237 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700238 if (status) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700239 dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
240 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return;
242 }
243
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700244 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247
Alan Sternf9c99bb2009-06-02 11:53:55 -0400248static void omninet_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200250 struct usb_serial_port *wport = serial->port[1];
Alan Sternf9c99bb2009-06-02 11:53:55 -0400251
Oliver Neukum5ec18622007-03-27 16:02:34 +0200252 usb_kill_urb(wport->write_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400253}
254
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700255module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Alan Coxf89d0df2008-07-22 11:15:54 +0100257MODULE_AUTHOR(DRIVER_AUTHOR);
258MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259MODULE_LICENSE("GPL");