^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) * Opticon USB barcode to serial driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2008 - 2009 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define CONTROL_RTS 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define RESEND_CTS_STATE 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* max number of write urbs in flight */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define URB_UPPER_LIMIT 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* This driver works for the Opticon 1D barcode reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) { USB_DEVICE(0x065a, 0x0009) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* This structure holds all of the individual device information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct opticon_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) spinlock_t lock; /* protects the following flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) bool rts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) bool cts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int outstanding_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int outstanding_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct usb_anchor anchor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void opticon_process_data_packet(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const unsigned char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) tty_insert_flip_string(&port->port, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void opticon_process_status_packet(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) const unsigned char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (buf[0] == 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) priv->cts = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) priv->cts = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void opticon_process_read_urb(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) const unsigned char *hdr = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) const unsigned char *data = hdr + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) size_t data_len = urb->actual_length - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (urb->actual_length <= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Data from the device comes with a 2 byte header:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * <0x00><0x00>data...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * This is real data to be sent to the tty layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * <0x00><0x01>level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * This is a CTS level change, the third byte is the CTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * value (0 for low, 1 for high).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) opticon_process_data_packet(port, data, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) opticon_process_status_packet(port, data, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) hdr[0], hdr[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) buffer = kzalloc(1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) buffer[0] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Send the message to the vendor control endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * of the connected device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) requesttype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 0, 0, buffer, 1, USB_CTRL_SET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) priv->rts = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Clear RTS line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) send_control_msg(port, CONTROL_RTS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* clear the halt status of the endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) usb_clear_halt(port->serial->dev, port->read_urb->pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) res = usb_serial_generic_open(tty, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Request CTS line state, sometimes during opening the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * CTS state can be missed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) send_control_msg(port, RESEND_CTS_STATE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void opticon_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) usb_kill_anchored_urbs(&priv->anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) usb_serial_generic_close(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static void opticon_write_control_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* free up the transfer buffer, as usb_free_urb() does not do this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) kfree(urb->transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* setup packet may be set if we're using it for writing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) kfree(urb->setup_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) dev_dbg(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) "%s - non-zero urb status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) --priv->outstanding_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) priv->outstanding_bytes -= urb->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) usb_serial_port_softint(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct usb_ctrlrequest *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) priv->outstanding_urbs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) priv->outstanding_bytes += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) buffer = kmalloc(count, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) goto error_no_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) urb = usb_alloc_urb(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) goto error_no_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) memcpy(buffer, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) usb_serial_debug_data(&port->dev, __func__, count, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* The connected devices do not have a bulk write endpoint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * to transmit data to de barcode device the control endpoint is used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) goto error_no_dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) dr->bRequest = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dr->wValue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dr->wIndex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dr->wLength = cpu_to_le16(count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) usb_fill_control_urb(urb, serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) (unsigned char *)dr, buffer, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) opticon_write_control_callback, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) usb_anchor_urb(urb, &priv->anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* send it down the pipe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ret = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* we are done with this urb, so let the host driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * really free it when it is finished with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) kfree(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) error_no_dr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) error_no_urb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) error_no_buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) --priv->outstanding_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) priv->outstanding_bytes -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int opticon_write_room(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * We really can take almost anything the user throws at us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * but let's pick a nice big number to tell the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * layer that we have lots of free space, unless we don't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return 2048;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static int opticon_chars_in_buffer(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) count = priv->outstanding_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static int opticon_tiocmget(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (priv->rts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) result |= TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (priv->cts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) result |= TIOCM_CTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) dev_dbg(&port->dev, "%s - %x\n", __func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int opticon_tiocmset(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) unsigned int set, unsigned int clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) bool rts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) bool changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* We only support RTS so we only handle that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) rts = priv->rts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (set & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) priv->rts = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (clear & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) priv->rts = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) changed = rts ^ priv->rts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (!changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ret = send_control_msg(port, CONTROL_RTS, !rts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return usb_translate_errors(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int get_serial_info(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct serial_struct *ss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* fake emulate a 16550 uart to make userspace code happy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ss->type = PORT_16550A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ss->line = port->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ss->port = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ss->irq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ss->xmit_fifo_size = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ss->baud_base = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ss->close_delay = 5*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) ss->closing_wait = 30*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int opticon_port_probe(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct opticon_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) priv = kzalloc(sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) spin_lock_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) init_usb_anchor(&priv->anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) usb_set_serial_port_data(port, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static int opticon_port_remove(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct opticon_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static struct usb_serial_driver opticon_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .name = "opticon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .id_table = id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .num_ports = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) .num_bulk_in = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) .bulk_in_size = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) .port_probe = opticon_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) .port_remove = opticon_port_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) .open = opticon_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) .close = opticon_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) .write = opticon_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .write_room = opticon_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .chars_in_buffer = opticon_chars_in_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) .throttle = usb_serial_generic_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) .unthrottle = usb_serial_generic_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) .get_serial = get_serial_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) .tiocmget = opticon_tiocmget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .tiocmset = opticon_tiocmset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) .process_read_urb = opticon_process_read_urb,
^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) static struct usb_serial_driver * const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) &opticon_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) module_usb_serial_driver(serial_drivers, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) MODULE_LICENSE("GPL v2");