blob: 7be40dfa3620216d5810fd912385443df4f0b5af [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 */
Johan Hovoldfbf94772013-04-16 18:01:23 +020034static void omninet_process_read_urb(struct urb *urb);
Alan Coxf89d0df2008-07-22 11:15:54 +010035static void omninet_write_bulk_callback(struct urb *urb);
36static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
37 const unsigned char *buf, int count);
38static int omninet_write_room(struct tty_struct *tty);
Alan Sternf9c99bb2009-06-02 11:53:55 -040039static void omninet_disconnect(struct usb_serial *serial);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020040static int omninet_port_probe(struct usb_serial_port *port);
41static int omninet_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Németh Márton7d40d7e2010-01-10 15:34:24 +010043static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
45 { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
46 { } /* Terminating entry */
47};
Alan Coxf89d0df2008-07-22 11:15:54 +010048MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070050static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070051 .driver = {
52 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070053 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070054 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070055 .description = "ZyXEL - omni.net lcd plus usb",
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .num_ports = 1,
Johan Hovold8ee15922017-03-02 12:51:28 +010058 .num_bulk_out = 2,
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 .write = omninet_write,
62 .write_room = omninet_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .write_bulk_callback = omninet_write_bulk_callback,
Johan Hovoldfbf94772013-04-16 18:01:23 +020064 .process_read_urb = omninet_process_read_urb,
Alan Sternf9c99bb2009-06-02 11:53:55 -040065 .disconnect = omninet_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Alan Sternf667dda2012-02-23 14:57:18 -050068static struct usb_serial_driver * const serial_drivers[] = {
69 &zyxel_omninet_device, NULL
70};
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Johan Hovoldb42abbc2013-04-16 18:01:20 +020073/*
74 * The protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
76 * The omni.net always exchange 64 bytes of data with the host. The first
Johan Hovoldb42abbc2013-04-16 18:01:20 +020077 * four bytes are the control header.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
79 * oh_seq is a sequence number. Don't know if/how it's used.
80 * oh_len is the length of the data bytes in the packet.
81 * oh_xxx Bit-mapped, related to handshaking and status info.
Johan Hovoldb42abbc2013-04-16 18:01:20 +020082 * I normally set it to 0x03 in transmitted frames.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * 7: Active when the TA is in a CONNECTed state.
84 * 6: unknown
85 * 5: handshaking, unknown
86 * 4: handshaking, unknown
87 * 3: unknown, usually 0
88 * 2: unknown, usually 0
Johan Hovoldb42abbc2013-04-16 18:01:20 +020089 * 1: handshaking, unknown, usually set to 1 in transmitted frames
90 * 0: handshaking, unknown, usually set to 1 in transmitted frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * oh_pad Probably a pad byte.
92 *
93 * After the header you will find data bytes if oh_len was greater than zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Alan Coxf89d0df2008-07-22 11:15:54 +010095struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 __u8 oh_seq;
97 __u8 oh_len;
98 __u8 oh_xxx;
99 __u8 oh_pad;
100};
101
Alan Coxf89d0df2008-07-22 11:15:54 +0100102struct omninet_data {
103 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200106static int omninet_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200108 struct omninet_data *od;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Johan Hovold81f58c62013-04-16 18:01:19 +0200110 od = kzalloc(sizeof(*od), GFP_KERNEL);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200111 if (!od)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOMEM;
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 usb_set_serial_port_data(port, od);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200115
116 return 0;
117}
118
119static int omninet_port_remove(struct usb_serial_port *port)
120{
121 struct omninet_data *od;
122
123 od = usb_get_serial_port_data(port);
124 kfree(od);
125
Oliver Neukum5ec18622007-03-27 16:02:34 +0200126 return 0;
127}
128
Johan Hovoldd91641b2013-04-16 18:01:21 +0200129#define OMNINET_HEADERLEN 4
130#define OMNINET_BULKOUTSIZE 64
131#define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Johan Hovolda6c042f2013-04-16 18:01:22 +0200133static void omninet_process_read_urb(struct urb *urb)
134{
135 struct usb_serial_port *port = urb->context;
136 const struct omninet_header *hdr = urb->transfer_buffer;
137 const unsigned char *data;
138 size_t data_len;
139
140 if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
141 return;
142
143 data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
144 data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
145 hdr->oh_len);
146 tty_insert_flip_string(&port->port, data, data_len);
147 tty_flip_buffer_push(&port->port);
148}
149
Alan Cox95da3102008-07-22 11:09:07 +0100150static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
151 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Alan Coxf89d0df2008-07-22 11:15:54 +0100153 struct usb_serial *serial = port->serial;
154 struct usb_serial_port *wport = serial->port[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Alan Coxf89d0df2008-07-22 11:15:54 +0100156 struct omninet_data *od = usb_get_serial_port_data(port);
157 struct omninet_header *header = (struct omninet_header *)
158 wport->write_urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 int result;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (count == 0) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700163 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxf89d0df2008-07-22 11:15:54 +0100164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700166
Johan Hovold120f9db2011-11-06 19:06:22 +0100167 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700168 dev_dbg(&port->dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Johan Hovoldd91641b2013-04-16 18:01:21 +0200172 count = (count > OMNINET_PAYLOADSIZE) ? OMNINET_PAYLOADSIZE : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Johan Hovoldd91641b2013-04-16 18:01:21 +0200174 memcpy(wport->write_urb->transfer_buffer + OMNINET_HEADERLEN,
Alan Coxf89d0df2008-07-22 11:15:54 +0100175 buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100177 usb_serial_debug_data(&port->dev, __func__, count,
178 wport->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 header->oh_seq = od->od_outseq++;
181 header->oh_len = count;
182 header->oh_xxx = 0x03;
183 header->oh_pad = 0x00;
184
185 /* send the data out the bulk port, always 64 bytes */
Johan Hovoldd91641b2013-04-16 18:01:21 +0200186 wport->write_urb->transfer_buffer_length = OMNINET_BULKOUTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700189 if (result) {
Johan Hovold120f9db2011-11-06 19:06:22 +0100190 set_bit(0, &wport->write_urbs_free);
Johan Hovold22a416c2012-02-10 13:20:51 +0100191 dev_err_console(port,
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700192 "%s - failed submitting write urb, error %d\n",
193 __func__, result);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700194 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 result = count;
196
197 return result;
198}
199
200
Alan Coxf89d0df2008-07-22 11:15:54 +0100201static int omninet_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Alan Cox95da3102008-07-22 11:09:07 +0100203 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct usb_serial *serial = port->serial;
205 struct usb_serial_port *wport = serial->port[1];
206
Alan Coxa5b6f602008-04-08 17:16:06 +0100207 int room = 0; /* Default: no room */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Johan Hovold120f9db2011-11-06 19:06:22 +0100209 if (test_bit(0, &wport->write_urbs_free))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 room = wport->bulk_out_size - OMNINET_HEADERLEN;
211
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700212 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Alan Coxf89d0df2008-07-22 11:15:54 +0100214 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Alan Coxf89d0df2008-07-22 11:15:54 +0100217static void omninet_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Alan Coxf89d0df2008-07-22 11:15:54 +0100219/* struct omninet_header *header = (struct omninet_header *)
220 urb->transfer_buffer; */
Ming Leicdc97792008-02-24 18:41:47 +0800221 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700222 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Johan Hovold120f9db2011-11-06 19:06:22 +0100224 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartmanfdc2deb2007-06-15 15:44:13 -0700225 if (status) {
Greg Kroah-Hartman7b5ba2752012-09-13 17:41:48 -0700226 dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
227 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return;
229 }
230
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700231 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234
Alan Sternf9c99bb2009-06-02 11:53:55 -0400235static void omninet_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200237 struct usb_serial_port *wport = serial->port[1];
Alan Sternf9c99bb2009-06-02 11:53:55 -0400238
Oliver Neukum5ec18622007-03-27 16:02:34 +0200239 usb_kill_urb(wport->write_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400240}
241
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700242module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Alan Coxf89d0df2008-07-22 11:15:54 +0100244MODULE_AUTHOR(DRIVER_AUTHOR);
245MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246MODULE_LICENSE("GPL");