^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001 REINER SCT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Matthias Bruestle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Contact: support@reiner-sct.com (see MAINTAINERS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * This program is largely derived from work by the linux-usb group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * and associated source files. Please see the usb/serial files for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * individual credits and copyrights.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * patience.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * In case of problems, please write to the contact e-mail address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * mentioned above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Please note that later models of the cyberjack reader family are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * supported by a libusb-based userspace device driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define CYBERJACK_LOCAL_BUF_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define DRIVER_AUTHOR "Matthias Bruestle"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define CYBERJACK_VENDOR_ID 0x0C4B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define CYBERJACK_PRODUCT_ID 0x0100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* Function prototypes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int cyberjack_port_probe(struct usb_serial_port *port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int cyberjack_port_remove(struct usb_serial_port *port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int cyberjack_open(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct usb_serial_port *port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void cyberjack_close(struct usb_serial_port *port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int cyberjack_write(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct usb_serial_port *port, const unsigned char *buf, int count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int cyberjack_write_room(struct tty_struct *tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void cyberjack_read_int_callback(struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void cyberjack_read_bulk_callback(struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void cyberjack_write_bulk_callback(struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) { } /* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static struct usb_serial_driver cyberjack_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .name = "cyberjack",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .description = "Reiner SCT Cyberjack USB card reader",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .id_table = id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .num_ports = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .num_bulk_out = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .port_probe = cyberjack_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .port_remove = cyberjack_port_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .open = cyberjack_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .close = cyberjack_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .write = cyberjack_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .write_room = cyberjack_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .read_int_callback = cyberjack_read_int_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .read_bulk_callback = cyberjack_read_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .write_bulk_callback = cyberjack_write_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static struct usb_serial_driver * const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) &cyberjack_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct cyberjack_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) spinlock_t lock; /* Lock for SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) short rdtodo; /* Bytes still to read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) short wrfilled; /* Overall data size we already got */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) short wrsent; /* Data already sent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int cyberjack_port_probe(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct cyberjack_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) spin_lock_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) priv->rdtodo = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) priv->wrfilled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) priv->wrsent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) usb_set_serial_port_data(port, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int cyberjack_port_remove(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct cyberjack_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int cyberjack_open(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct cyberjack_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) usb_clear_halt(port->serial->dev, port->write_urb->pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) priv->rdtodo = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) priv->wrfilled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) priv->wrsent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static void cyberjack_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) usb_kill_urb(port->write_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) usb_kill_urb(port->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int cyberjack_write(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct usb_serial_port *port, const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct device *dev = &port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct cyberjack_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int wrexpected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!test_and_clear_bit(0, &port->write_urbs_free)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) dev_dbg(dev, "%s - already writing\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* To much data for buffer. Reset buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) priv->wrfilled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) set_bit(0, &port->write_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* Copy data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) memcpy(priv->wrbuf + priv->wrfilled, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) priv->wrfilled += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (priv->wrfilled >= 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) wrexpected = sizeof(priv->wrbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (priv->wrfilled >= wrexpected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* We have enough data to begin transmission */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) length = (wrexpected > port->bulk_out_size) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) port->bulk_out_size : wrexpected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) priv->wrsent = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* set up our urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) port->write_urb->transfer_buffer_length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* send the data out the bulk port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) "%s - failed submitting write urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) __func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* Throw away data. No better idea what to do with it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) priv->wrfilled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) priv->wrsent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) set_bit(0, &port->write_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (priv->wrsent >= priv->wrfilled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dev_dbg(dev, "%s - buffer cleaned\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) priv->wrfilled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) priv->wrsent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static int cyberjack_write_room(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* FIXME: .... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return CYBERJACK_LOCAL_BUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static void cyberjack_read_int_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct cyberjack_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct device *dev = &port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) unsigned char *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* the urb might have been killed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) usb_serial_debug_data(dev, __func__, urb->actual_length, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* React only to interrupts signaling a bulk_in transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (urb->actual_length == 4 && data[0] == 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) short old_rdtodo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* This is a announcement of coming bulk_ins. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) old_rdtodo = priv->rdtodo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (old_rdtodo > SHRT_MAX - size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) dev_dbg(dev, "Too many bulk_in urbs to do.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* "+=" is probably more fault tolerant than "=" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) priv->rdtodo += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!old_rdtodo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) __func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) resubmit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static void cyberjack_read_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct cyberjack_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct device *dev = &port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) unsigned char *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) short todo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) usb_serial_debug_data(dev, __func__, urb->actual_length, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (urb->actual_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) tty_insert_flip_string(&port->port, data, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* Reduce urbs to do by one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) priv->rdtodo -= urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Just to be sure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (priv->rdtodo < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) priv->rdtodo = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) todo = priv->rdtodo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* Continue to read if we have still urbs to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) __func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static void cyberjack_write_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct cyberjack_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct device *dev = &port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) bool resubmitted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) set_bit(0, &port->write_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* only do something if we have more data to send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (priv->wrfilled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int length, blksize, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) port->bulk_out_size : (priv->wrfilled - priv->wrsent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) memcpy(port->write_urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) priv->wrbuf + priv->wrsent, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) priv->wrsent += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* set up our urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) port->write_urb->transfer_buffer_length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* send the data out the bulk port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) dev_err(dev, "%s - failed submitting write urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) __func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /* Throw away data. No better idea what to do with it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) priv->wrfilled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) priv->wrsent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) resubmitted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (priv->wrsent >= priv->wrfilled ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) priv->wrsent >= blksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) dev_dbg(dev, "%s - buffer cleaned\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) priv->wrfilled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) priv->wrsent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (!resubmitted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) set_bit(0, &port->write_urbs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) usb_serial_port_softint(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) module_usb_serial_driver(serial_drivers, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) MODULE_AUTHOR(DRIVER_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) MODULE_LICENSE("GPL");