Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * USB ZyXEL omni.net LCD PLUS driver |
| 4 | * |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 5 | * Copyright (C) 2013,2017 Johan Hovold <johan@kernel.org> |
| 6 | * |
Alan Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 7 | * See Documentation/usb/usb-serial.txt for more information on using this |
| 8 | * driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * Please report both successes and troubles to the author at omninet@kroah.com |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #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 Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 22 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #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 Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 29 | /* This one seems to be a re-branded ZyXEL device */ |
| 30 | #define BT_IGNITIONPRO_ID 0x2000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* function prototypes */ |
Johan Hovold | fbf9477 | 2013-04-16 18:01:23 +0200 | [diff] [blame] | 33 | static void omninet_process_read_urb(struct urb *urb); |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 34 | static int omninet_prepare_write_buffer(struct usb_serial_port *port, |
| 35 | void *buf, size_t count); |
Johan Hovold | 2dc1071 | 2017-03-16 17:13:48 +0100 | [diff] [blame] | 36 | static int omninet_calc_num_ports(struct usb_serial *serial, |
| 37 | struct usb_serial_endpoints *epds); |
Johan Hovold | feffa7c | 2012-10-25 10:29:06 +0200 | [diff] [blame] | 38 | static int omninet_port_probe(struct usb_serial_port *port); |
| 39 | static int omninet_port_remove(struct usb_serial_port *port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 41 | static const struct usb_device_id id_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) }, |
| 43 | { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) }, |
| 44 | { } /* Terminating entry */ |
| 45 | }; |
Alan Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 46 | MODULE_DEVICE_TABLE(usb, id_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 48 | static struct usb_serial_driver zyxel_omninet_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 49 | .driver = { |
| 50 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 51 | .name = "omninet", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 52 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 53 | .description = "ZyXEL - omni.net lcd plus usb", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | .id_table = id_table, |
Johan Hovold | 8ee1592 | 2017-03-02 12:51:28 +0100 | [diff] [blame] | 55 | .num_bulk_out = 2, |
Johan Hovold | 2dc1071 | 2017-03-16 17:13:48 +0100 | [diff] [blame] | 56 | .calc_num_ports = omninet_calc_num_ports, |
Johan Hovold | feffa7c | 2012-10-25 10:29:06 +0200 | [diff] [blame] | 57 | .port_probe = omninet_port_probe, |
| 58 | .port_remove = omninet_port_remove, |
Johan Hovold | fbf9477 | 2013-04-16 18:01:23 +0200 | [diff] [blame] | 59 | .process_read_urb = omninet_process_read_urb, |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 60 | .prepare_write_buffer = omninet_prepare_write_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Alan Stern | f667dda | 2012-02-23 14:57:18 -0500 | [diff] [blame] | 63 | static struct usb_serial_driver * const serial_drivers[] = { |
| 64 | &zyxel_omninet_device, NULL |
| 65 | }; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Johan Hovold | b42abbc | 2013-04-16 18:01:20 +0200 | [diff] [blame] | 68 | /* |
| 69 | * The protocol. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * |
| 71 | * The omni.net always exchange 64 bytes of data with the host. The first |
Johan Hovold | b42abbc | 2013-04-16 18:01:20 +0200 | [diff] [blame] | 72 | * four bytes are the control header. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * |
| 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 Hovold | b42abbc | 2013-04-16 18:01:20 +0200 | [diff] [blame] | 77 | * I normally set it to 0x03 in transmitted frames. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * 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 Hovold | b42abbc | 2013-04-16 18:01:20 +0200 | [diff] [blame] | 84 | * 1: handshaking, unknown, usually set to 1 in transmitted frames |
| 85 | * 0: handshaking, unknown, usually set to 1 in transmitted frames |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * oh_pad Probably a pad byte. |
| 87 | * |
| 88 | * After the header you will find data bytes if oh_len was greater than zero. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | */ |
Alan Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 90 | struct omninet_header { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | __u8 oh_seq; |
| 92 | __u8 oh_len; |
| 93 | __u8 oh_xxx; |
| 94 | __u8 oh_pad; |
| 95 | }; |
| 96 | |
Alan Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 97 | struct omninet_data { |
| 98 | __u8 od_outseq; /* Sequence number for bulk_out URBs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Johan Hovold | 2dc1071 | 2017-03-16 17:13:48 +0100 | [diff] [blame] | 101 | static 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 Hovold | feffa7c | 2012-10-25 10:29:06 +0200 | [diff] [blame] | 111 | static int omninet_port_probe(struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Oliver Neukum | 5ec1862 | 2007-03-27 16:02:34 +0200 | [diff] [blame] | 113 | struct omninet_data *od; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Johan Hovold | 81f58c6 | 2013-04-16 18:01:19 +0200 | [diff] [blame] | 115 | od = kzalloc(sizeof(*od), GFP_KERNEL); |
Johan Hovold | feffa7c | 2012-10-25 10:29:06 +0200 | [diff] [blame] | 116 | if (!od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return -ENOMEM; |
Johan Hovold | feffa7c | 2012-10-25 10:29:06 +0200 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | usb_set_serial_port_data(port, od); |
Johan Hovold | feffa7c | 2012-10-25 10:29:06 +0200 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static 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 Neukum | 5ec1862 | 2007-03-27 16:02:34 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Johan Hovold | d91641b | 2013-04-16 18:01:21 +0200 | [diff] [blame] | 134 | #define OMNINET_HEADERLEN 4 |
| 135 | #define OMNINET_BULKOUTSIZE 64 |
| 136 | #define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Johan Hovold | a6c042f | 2013-04-16 18:01:22 +0200 | [diff] [blame] | 138 | static 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 Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 155 | static int omninet_prepare_write_buffer(struct usb_serial_port *port, |
| 156 | void *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Alan Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 158 | struct omninet_data *od = usb_get_serial_port_data(port); |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 159 | struct omninet_header *header = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 161 | count = min_t(size_t, count, OMNINET_PAYLOADSIZE); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 162 | |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 163 | count = kfifo_out_locked(&port->write_fifo, buf + OMNINET_HEADERLEN, |
| 164 | count, &port->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 166 | header->oh_seq = od->od_outseq++; |
| 167 | header->oh_len = count; |
| 168 | header->oh_xxx = 0x03; |
| 169 | header->oh_pad = 0x00; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Johan Hovold | d274441 | 2017-03-16 17:13:49 +0100 | [diff] [blame] | 171 | /* always 64 bytes */ |
| 172 | return OMNINET_BULKOUTSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 175 | module_usb_serial_driver(serial_drivers, id_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Alan Cox | f89d0df | 2008-07-22 11:15:54 +0100 | [diff] [blame] | 177 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 178 | MODULE_DESCRIPTION(DRIVER_DESC); |
Johan Hovold | 627cfa8 | 2017-11-03 18:12:08 +0100 | [diff] [blame] | 179 | MODULE_LICENSE("GPL v2"); |