1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * USB ZyXEL omni.net LCD PLUS driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2013,2017 Johan Hovold <johan@kernel.org>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * See Documentation/usb/usb-serial.rst for more information on using this
8*4882a593Smuzhiyun * driver
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Please report both successes and troubles to the author at omninet@kroah.com
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/errno.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/tty.h>
17*4882a593Smuzhiyun #include <linux/tty_driver.h>
18*4882a593Smuzhiyun #include <linux/tty_flip.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/uaccess.h>
21*4882a593Smuzhiyun #include <linux/usb.h>
22*4882a593Smuzhiyun #include <linux/usb/serial.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define DRIVER_AUTHOR "Alessandro Zummo"
25*4882a593Smuzhiyun #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define ZYXEL_VENDOR_ID 0x0586
28*4882a593Smuzhiyun #define ZYXEL_OMNINET_ID 0x1000
29*4882a593Smuzhiyun #define ZYXEL_OMNI_56K_PLUS_ID 0x1500
30*4882a593Smuzhiyun /* This one seems to be a re-branded ZyXEL device */
31*4882a593Smuzhiyun #define BT_IGNITIONPRO_ID 0x2000
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /* function prototypes */
34*4882a593Smuzhiyun static void omninet_process_read_urb(struct urb *urb);
35*4882a593Smuzhiyun static int omninet_prepare_write_buffer(struct usb_serial_port *port,
36*4882a593Smuzhiyun void *buf, size_t count);
37*4882a593Smuzhiyun static int omninet_calc_num_ports(struct usb_serial *serial,
38*4882a593Smuzhiyun struct usb_serial_endpoints *epds);
39*4882a593Smuzhiyun static int omninet_port_probe(struct usb_serial_port *port);
40*4882a593Smuzhiyun static int omninet_port_remove(struct usb_serial_port *port);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun static const struct usb_device_id id_table[] = {
43*4882a593Smuzhiyun { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
44*4882a593Smuzhiyun { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNI_56K_PLUS_ID) },
45*4882a593Smuzhiyun { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
46*4882a593Smuzhiyun { } /* Terminating entry */
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, id_table);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun static struct usb_serial_driver zyxel_omninet_device = {
51*4882a593Smuzhiyun .driver = {
52*4882a593Smuzhiyun .owner = THIS_MODULE,
53*4882a593Smuzhiyun .name = "omninet",
54*4882a593Smuzhiyun },
55*4882a593Smuzhiyun .description = "ZyXEL - omni.net lcd plus usb",
56*4882a593Smuzhiyun .id_table = id_table,
57*4882a593Smuzhiyun .num_bulk_out = 2,
58*4882a593Smuzhiyun .calc_num_ports = omninet_calc_num_ports,
59*4882a593Smuzhiyun .port_probe = omninet_port_probe,
60*4882a593Smuzhiyun .port_remove = omninet_port_remove,
61*4882a593Smuzhiyun .process_read_urb = omninet_process_read_urb,
62*4882a593Smuzhiyun .prepare_write_buffer = omninet_prepare_write_buffer,
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun static struct usb_serial_driver * const serial_drivers[] = {
66*4882a593Smuzhiyun &zyxel_omninet_device, NULL
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * The protocol.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * The omni.net always exchange 64 bytes of data with the host. The first
74*4882a593Smuzhiyun * four bytes are the control header.
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * oh_seq is a sequence number. Don't know if/how it's used.
77*4882a593Smuzhiyun * oh_len is the length of the data bytes in the packet.
78*4882a593Smuzhiyun * oh_xxx Bit-mapped, related to handshaking and status info.
79*4882a593Smuzhiyun * I normally set it to 0x03 in transmitted frames.
80*4882a593Smuzhiyun * 7: Active when the TA is in a CONNECTed state.
81*4882a593Smuzhiyun * 6: unknown
82*4882a593Smuzhiyun * 5: handshaking, unknown
83*4882a593Smuzhiyun * 4: handshaking, unknown
84*4882a593Smuzhiyun * 3: unknown, usually 0
85*4882a593Smuzhiyun * 2: unknown, usually 0
86*4882a593Smuzhiyun * 1: handshaking, unknown, usually set to 1 in transmitted frames
87*4882a593Smuzhiyun * 0: handshaking, unknown, usually set to 1 in transmitted frames
88*4882a593Smuzhiyun * oh_pad Probably a pad byte.
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * After the header you will find data bytes if oh_len was greater than zero.
91*4882a593Smuzhiyun */
92*4882a593Smuzhiyun struct omninet_header {
93*4882a593Smuzhiyun __u8 oh_seq;
94*4882a593Smuzhiyun __u8 oh_len;
95*4882a593Smuzhiyun __u8 oh_xxx;
96*4882a593Smuzhiyun __u8 oh_pad;
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun struct omninet_data {
100*4882a593Smuzhiyun __u8 od_outseq; /* Sequence number for bulk_out URBs */
101*4882a593Smuzhiyun };
102*4882a593Smuzhiyun
omninet_calc_num_ports(struct usb_serial * serial,struct usb_serial_endpoints * epds)103*4882a593Smuzhiyun static int omninet_calc_num_ports(struct usb_serial *serial,
104*4882a593Smuzhiyun struct usb_serial_endpoints *epds)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun /* We need only the second bulk-out for our single-port device. */
107*4882a593Smuzhiyun epds->bulk_out[0] = epds->bulk_out[1];
108*4882a593Smuzhiyun epds->num_bulk_out = 1;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun return 1;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
omninet_port_probe(struct usb_serial_port * port)113*4882a593Smuzhiyun static int omninet_port_probe(struct usb_serial_port *port)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct omninet_data *od;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun od = kzalloc(sizeof(*od), GFP_KERNEL);
118*4882a593Smuzhiyun if (!od)
119*4882a593Smuzhiyun return -ENOMEM;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun usb_set_serial_port_data(port, od);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
omninet_port_remove(struct usb_serial_port * port)126*4882a593Smuzhiyun static int omninet_port_remove(struct usb_serial_port *port)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun struct omninet_data *od;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun od = usb_get_serial_port_data(port);
131*4882a593Smuzhiyun kfree(od);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun return 0;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun #define OMNINET_HEADERLEN 4
137*4882a593Smuzhiyun #define OMNINET_BULKOUTSIZE 64
138*4882a593Smuzhiyun #define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
139*4882a593Smuzhiyun
omninet_process_read_urb(struct urb * urb)140*4882a593Smuzhiyun static void omninet_process_read_urb(struct urb *urb)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun struct usb_serial_port *port = urb->context;
143*4882a593Smuzhiyun const struct omninet_header *hdr = urb->transfer_buffer;
144*4882a593Smuzhiyun const unsigned char *data;
145*4882a593Smuzhiyun size_t data_len;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
148*4882a593Smuzhiyun return;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
151*4882a593Smuzhiyun data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
152*4882a593Smuzhiyun hdr->oh_len);
153*4882a593Smuzhiyun tty_insert_flip_string(&port->port, data, data_len);
154*4882a593Smuzhiyun tty_flip_buffer_push(&port->port);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
omninet_prepare_write_buffer(struct usb_serial_port * port,void * buf,size_t count)157*4882a593Smuzhiyun static int omninet_prepare_write_buffer(struct usb_serial_port *port,
158*4882a593Smuzhiyun void *buf, size_t count)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun struct omninet_data *od = usb_get_serial_port_data(port);
161*4882a593Smuzhiyun struct omninet_header *header = buf;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun count = min_t(size_t, count, OMNINET_PAYLOADSIZE);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun count = kfifo_out_locked(&port->write_fifo, buf + OMNINET_HEADERLEN,
166*4882a593Smuzhiyun count, &port->lock);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun header->oh_seq = od->od_outseq++;
169*4882a593Smuzhiyun header->oh_len = count;
170*4882a593Smuzhiyun header->oh_xxx = 0x03;
171*4882a593Smuzhiyun header->oh_pad = 0x00;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* always 64 bytes */
174*4882a593Smuzhiyun return OMNINET_BULKOUTSIZE;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun module_usb_serial_driver(serial_drivers, id_table);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun MODULE_AUTHOR(DRIVER_AUTHOR);
180*4882a593Smuzhiyun MODULE_DESCRIPTION(DRIVER_DESC);
181*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
182