blob: efcd7feed6f4d1c882627736de30253ca2858da6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB ZyXEL omni.net LCD PLUS driver
3 *
Johan Hovoldd2744412017-03-16 17:13:49 +01004 * Copyright (C) 2013,2017 Johan Hovold <johan@kernel.org>
5 *
Greg Kroah-Hartman4d0dce32007-06-12 11:43:37 -07006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Alan Coxf89d0df2008-07-22 11:15:54 +010010 * See Documentation/usb/usb-serial.txt for more information on using this
11 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * Please report both successes and troubles to the author at omninet@kroah.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/slab.h>
19#include <linux/tty.h>
20#include <linux/tty_driver.h>
21#include <linux/tty_flip.h>
22#include <linux/module.h>
Alan Coxf89d0df2008-07-22 11:15:54 +010023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070025#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define DRIVER_AUTHOR "Alessandro Zummo"
28#define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
29
30#define ZYXEL_VENDOR_ID 0x0586
31#define ZYXEL_OMNINET_ID 0x1000
Alan Coxf89d0df2008-07-22 11:15:54 +010032/* This one seems to be a re-branded ZyXEL device */
33#define BT_IGNITIONPRO_ID 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* function prototypes */
Johan Hovoldfbf94772013-04-16 18:01:23 +020036static void omninet_process_read_urb(struct urb *urb);
Johan Hovoldd2744412017-03-16 17:13:49 +010037static int omninet_prepare_write_buffer(struct usb_serial_port *port,
38 void *buf, size_t count);
Johan Hovold2dc10712017-03-16 17:13:48 +010039static int omninet_calc_num_ports(struct usb_serial *serial,
40 struct usb_serial_endpoints *epds);
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,
Johan Hovold8ee15922017-03-02 12:51:28 +010058 .num_bulk_out = 2,
Johan Hovold2dc10712017-03-16 17:13:48 +010059 .calc_num_ports = omninet_calc_num_ports,
Johan Hovoldfeffa7c2012-10-25 10:29:06 +020060 .port_probe = omninet_port_probe,
61 .port_remove = omninet_port_remove,
Johan Hovoldfbf94772013-04-16 18:01:23 +020062 .process_read_urb = omninet_process_read_urb,
Johan Hovoldd2744412017-03-16 17:13:49 +010063 .prepare_write_buffer = omninet_prepare_write_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Alan Sternf667dda2012-02-23 14:57:18 -050066static struct usb_serial_driver * const serial_drivers[] = {
67 &zyxel_omninet_device, NULL
68};
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Johan Hovoldb42abbc2013-04-16 18:01:20 +020071/*
72 * The protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * The omni.net always exchange 64 bytes of data with the host. The first
Johan Hovoldb42abbc2013-04-16 18:01:20 +020075 * four bytes are the control header.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * oh_seq is a sequence number. Don't know if/how it's used.
78 * oh_len is the length of the data bytes in the packet.
79 * oh_xxx Bit-mapped, related to handshaking and status info.
Johan Hovoldb42abbc2013-04-16 18:01:20 +020080 * I normally set it to 0x03 in transmitted frames.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * 7: Active when the TA is in a CONNECTed state.
82 * 6: unknown
83 * 5: handshaking, unknown
84 * 4: handshaking, unknown
85 * 3: unknown, usually 0
86 * 2: unknown, usually 0
Johan Hovoldb42abbc2013-04-16 18:01:20 +020087 * 1: handshaking, unknown, usually set to 1 in transmitted frames
88 * 0: handshaking, unknown, usually set to 1 in transmitted frames
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * oh_pad Probably a pad byte.
90 *
91 * After the header you will find data bytes if oh_len was greater than zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Alan Coxf89d0df2008-07-22 11:15:54 +010093struct omninet_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 __u8 oh_seq;
95 __u8 oh_len;
96 __u8 oh_xxx;
97 __u8 oh_pad;
98};
99
Alan Coxf89d0df2008-07-22 11:15:54 +0100100struct omninet_data {
101 __u8 od_outseq; /* Sequence number for bulk_out URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Johan Hovold2dc10712017-03-16 17:13:48 +0100104static int omninet_calc_num_ports(struct usb_serial *serial,
105 struct usb_serial_endpoints *epds)
106{
107 /* We need only the second bulk-out for our single-port device. */
108 epds->bulk_out[0] = epds->bulk_out[1];
109 epds->num_bulk_out = 1;
110
111 return 1;
112}
113
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200114static int omninet_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Oliver Neukum5ec18622007-03-27 16:02:34 +0200116 struct omninet_data *od;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Johan Hovold81f58c62013-04-16 18:01:19 +0200118 od = kzalloc(sizeof(*od), GFP_KERNEL);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200119 if (!od)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -ENOMEM;
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 usb_set_serial_port_data(port, od);
Johan Hovoldfeffa7c2012-10-25 10:29:06 +0200123
124 return 0;
125}
126
127static int omninet_port_remove(struct usb_serial_port *port)
128{
129 struct omninet_data *od;
130
131 od = usb_get_serial_port_data(port);
132 kfree(od);
133
Oliver Neukum5ec18622007-03-27 16:02:34 +0200134 return 0;
135}
136
Johan Hovoldd91641b2013-04-16 18:01:21 +0200137#define OMNINET_HEADERLEN 4
138#define OMNINET_BULKOUTSIZE 64
139#define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Johan Hovolda6c042f2013-04-16 18:01:22 +0200141static void omninet_process_read_urb(struct urb *urb)
142{
143 struct usb_serial_port *port = urb->context;
144 const struct omninet_header *hdr = urb->transfer_buffer;
145 const unsigned char *data;
146 size_t data_len;
147
148 if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
149 return;
150
151 data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
152 data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
153 hdr->oh_len);
154 tty_insert_flip_string(&port->port, data, data_len);
155 tty_flip_buffer_push(&port->port);
156}
157
Johan Hovoldd2744412017-03-16 17:13:49 +0100158static int omninet_prepare_write_buffer(struct usb_serial_port *port,
159 void *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Alan Coxf89d0df2008-07-22 11:15:54 +0100161 struct omninet_data *od = usb_get_serial_port_data(port);
Johan Hovoldd2744412017-03-16 17:13:49 +0100162 struct omninet_header *header = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Johan Hovoldd2744412017-03-16 17:13:49 +0100164 count = min_t(size_t, count, OMNINET_PAYLOADSIZE);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700165
Johan Hovoldd2744412017-03-16 17:13:49 +0100166 count = kfifo_out_locked(&port->write_fifo, buf + OMNINET_HEADERLEN,
167 count, &port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Johan Hovoldd2744412017-03-16 17:13:49 +0100169 header->oh_seq = od->od_outseq++;
170 header->oh_len = count;
171 header->oh_xxx = 0x03;
172 header->oh_pad = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Johan Hovoldd2744412017-03-16 17:13:49 +0100174 /* always 64 bytes */
175 return OMNINET_BULKOUTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700178module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Alan Coxf89d0df2008-07-22 11:15:54 +0100180MODULE_AUTHOR(DRIVER_AUTHOR);
181MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182MODULE_LICENSE("GPL");