blob: e51c9464ea4267281f2fff3cf830603e49bc3d46 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * USB ZyXEL omni.net LCD PLUS driver
4 *
Johan Hovoldd2744412017-03-16 17:13:49 +01005 * Copyright (C) 2013,2017 Johan Hovold <johan@kernel.org>
6 *
Alan Coxf89d0df2008-07-22 11:15:54 +01007 * See Documentation/usb/usb-serial.txt for more information on using this
8 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Please report both successes and troubles to the author at omninet@kroah.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/tty.h>
17#include <linux/tty_driver.h>
18#include <linux/tty_flip.h>
19#include <linux/module.h>
Alan Coxf89d0df2008-07-22 11:15:54 +010020#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070022#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define DRIVER_AUTHOR "Alessandro Zummo"
25#define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
26
27#define ZYXEL_VENDOR_ID 0x0586
28#define ZYXEL_OMNINET_ID 0x1000
Alan Coxf89d0df2008-07-22 11:15:54 +010029/* This one seems to be a re-branded ZyXEL device */
30#define BT_IGNITIONPRO_ID 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* function prototypes */
Johan Hovoldfbf94772013-04-16 18:01:23 +020033static void omninet_process_read_urb(struct urb *urb);
Johan Hovoldd2744412017-03-16 17:13:49 +010034static int omninet_prepare_write_buffer(struct usb_serial_port *port,
35 void *buf, size_t count);
Johan Hovold2dc10712017-03-16 17:13:48 +010036static int omninet_calc_num_ports(struct usb_serial *serial,
37 struct usb_serial_endpoints *epds);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020038static int omninet_port_probe(struct usb_serial_port *port);
39static int omninet_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Németh Márton7d40d7e2010-01-10 15:34:24 +010041static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
43 { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
44 { } /* Terminating entry */
45};
Alan Coxf89d0df2008-07-22 11:15:54 +010046MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070048static struct usb_serial_driver zyxel_omninet_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070049 .driver = {
50 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070051 .name = "omninet",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070052 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070053 .description = "ZyXEL - omni.net lcd plus usb",
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .id_table = id_table,
Johan Hovold8ee15922017-03-02 12:51:28 +010055 .num_bulk_out = 2,
Johan Hovold2dc10712017-03-16 17:13:48 +010056 .calc_num_ports = omninet_calc_num_ports,
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020057 .port_probe = omninet_port_probe,
58 .port_remove = omninet_port_remove,
Johan Hovoldfbf94772013-04-16 18:01:23 +020059 .process_read_urb = omninet_process_read_urb,
Johan Hovoldd2744412017-03-16 17:13:49 +010060 .prepare_write_buffer = omninet_prepare_write_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Alan Sternf667dda2012-02-23 14:57:18 -050063static struct usb_serial_driver * const serial_drivers[] = {
64 &zyxel_omninet_device, NULL
65};
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Johan Hovoldb42abbc2013-04-16 18:01:20 +020068/*
69 * The protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * The omni.net always exchange 64 bytes of data with the host. The first
Johan Hovoldb42abbc2013-04-16 18:01:20 +020072 * four bytes are the control header.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * oh_seq is a sequence number. Don't know if/how it's used.
75 * oh_len is the length of the data bytes in the packet.
76 * oh_xxx Bit-mapped, related to handshaking and status info.
Johan Hovoldb42abbc2013-04-16 18:01:20 +020077 * I normally set it to 0x03 in transmitted frames.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * 7: Active when the TA is in a CONNECTed state.
79 * 6: unknown
80 * 5: handshaking, unknown
81 * 4: handshaking, unknown
82 * 3: unknown, usually 0
83 * 2: unknown, usually 0
Johan Hovoldb42abbc2013-04-16 18:01:20 +020084 * 1: handshaking, unknown, usually set to 1 in transmitted frames
85 * 0: handshaking, unknown, usually set to 1 in transmitted frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * oh_pad Probably a pad byte.
87 *
88 * After the header you will find data bytes if oh_len was greater than zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
Alan Coxf89d0df2008-07-22 11:15:54 +010090struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 __u8 oh_seq;
92 __u8 oh_len;
93 __u8 oh_xxx;
94 __u8 oh_pad;
95};
96
Alan Coxf89d0df2008-07-22 11:15:54 +010097struct omninet_data {
98 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Johan Hovold2dc10712017-03-16 17:13:48 +0100101static int omninet_calc_num_ports(struct usb_serial *serial,
102 struct usb_serial_endpoints *epds)
103{
104 /* We need only the second bulk-out for our single-port device. */
105 epds->bulk_out[0] = epds->bulk_out[1];
106 epds->num_bulk_out = 1;
107
108 return 1;
109}
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
Johan Hovold81f58c62013-04-16 18:01:19 +0200115 od = kzalloc(sizeof(*od), 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
Johan Hovoldd91641b2013-04-16 18:01:21 +0200134#define OMNINET_HEADERLEN 4
135#define OMNINET_BULKOUTSIZE 64
136#define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Johan Hovolda6c042f2013-04-16 18:01:22 +0200138static void omninet_process_read_urb(struct urb *urb)
139{
140 struct usb_serial_port *port = urb->context;
141 const struct omninet_header *hdr = urb->transfer_buffer;
142 const unsigned char *data;
143 size_t data_len;
144
145 if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
146 return;
147
148 data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
149 data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
150 hdr->oh_len);
151 tty_insert_flip_string(&port->port, data, data_len);
152 tty_flip_buffer_push(&port->port);
153}
154
Johan Hovoldd2744412017-03-16 17:13:49 +0100155static int omninet_prepare_write_buffer(struct usb_serial_port *port,
156 void *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Alan Coxf89d0df2008-07-22 11:15:54 +0100158 struct omninet_data *od = usb_get_serial_port_data(port);
Johan Hovoldd2744412017-03-16 17:13:49 +0100159 struct omninet_header *header = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Johan Hovoldd2744412017-03-16 17:13:49 +0100161 count = min_t(size_t, count, OMNINET_PAYLOADSIZE);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700162
Johan Hovoldd2744412017-03-16 17:13:49 +0100163 count = kfifo_out_locked(&port->write_fifo, buf + OMNINET_HEADERLEN,
164 count, &port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Johan Hovoldd2744412017-03-16 17:13:49 +0100166 header->oh_seq = od->od_outseq++;
167 header->oh_len = count;
168 header->oh_xxx = 0x03;
169 header->oh_pad = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Johan Hovoldd2744412017-03-16 17:13:49 +0100171 /* always 64 bytes */
172 return OMNINET_BULKOUTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700175module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Alan Coxf89d0df2008-07-22 11:15:54 +0100177MODULE_AUTHOR(DRIVER_AUTHOR);
178MODULE_DESCRIPTION(DRIVER_DESC);
Johan Hovold627cfa82017-11-03 18:12:08 +0100179MODULE_LICENSE("GPL v2");