Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Opticon USB barcode to serial driver
0004  *
0005  * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
0006  * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
0007  * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
0008  * Copyright (C) 2008 - 2009 Novell Inc.
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/tty.h>
0013 #include <linux/tty_driver.h>
0014 #include <linux/slab.h>
0015 #include <linux/tty_flip.h>
0016 #include <linux/serial.h>
0017 #include <linux/module.h>
0018 #include <linux/usb.h>
0019 #include <linux/usb/serial.h>
0020 #include <linux/uaccess.h>
0021 
0022 #define CONTROL_RTS         0x02
0023 #define RESEND_CTS_STATE    0x03
0024 
0025 /* max number of write urbs in flight */
0026 #define URB_UPPER_LIMIT 8
0027 
0028 /* This driver works for the Opticon 1D barcode reader
0029  * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
0030 #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)"
0031 
0032 static const struct usb_device_id id_table[] = {
0033     { USB_DEVICE(0x065a, 0x0009) },
0034     { },
0035 };
0036 MODULE_DEVICE_TABLE(usb, id_table);
0037 
0038 /* This structure holds all of the individual device information */
0039 struct opticon_private {
0040     spinlock_t lock;    /* protects the following flags */
0041     bool rts;
0042     bool cts;
0043     int outstanding_urbs;
0044     int outstanding_bytes;
0045 
0046     struct usb_anchor anchor;
0047 };
0048 
0049 
0050 static void opticon_process_data_packet(struct usb_serial_port *port,
0051                     const unsigned char *buf, size_t len)
0052 {
0053     tty_insert_flip_string(&port->port, buf, len);
0054     tty_flip_buffer_push(&port->port);
0055 }
0056 
0057 static void opticon_process_status_packet(struct usb_serial_port *port,
0058                     const unsigned char *buf, size_t len)
0059 {
0060     struct opticon_private *priv = usb_get_serial_port_data(port);
0061     unsigned long flags;
0062 
0063     spin_lock_irqsave(&priv->lock, flags);
0064     if (buf[0] == 0x00)
0065         priv->cts = false;
0066     else
0067         priv->cts = true;
0068     spin_unlock_irqrestore(&priv->lock, flags);
0069 }
0070 
0071 static void opticon_process_read_urb(struct urb *urb)
0072 {
0073     struct usb_serial_port *port = urb->context;
0074     const unsigned char *hdr = urb->transfer_buffer;
0075     const unsigned char *data = hdr + 2;
0076     size_t data_len = urb->actual_length - 2;
0077 
0078     if (urb->actual_length <= 2) {
0079         dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
0080                             urb->actual_length);
0081         return;
0082     }
0083     /*
0084      * Data from the device comes with a 2 byte header:
0085      *
0086      * <0x00><0x00>data...
0087      *      This is real data to be sent to the tty layer
0088      * <0x00><0x01>level
0089      *      This is a CTS level change, the third byte is the CTS
0090      *      value (0 for low, 1 for high).
0091      */
0092     if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
0093         opticon_process_data_packet(port, data, data_len);
0094     } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
0095         opticon_process_status_packet(port, data, data_len);
0096     } else {
0097         dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
0098                             hdr[0], hdr[1]);
0099     }
0100 }
0101 
0102 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
0103                 u8 val)
0104 {
0105     struct usb_serial *serial = port->serial;
0106     int retval;
0107     u8 *buffer;
0108 
0109     buffer = kzalloc(1, GFP_KERNEL);
0110     if (!buffer)
0111         return -ENOMEM;
0112 
0113     buffer[0] = val;
0114     /* Send the message to the vendor control endpoint
0115      * of the connected device */
0116     retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
0117                 requesttype,
0118                 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
0119                 0, 0, buffer, 1, USB_CTRL_SET_TIMEOUT);
0120     kfree(buffer);
0121 
0122     if (retval < 0)
0123         return retval;
0124 
0125     return 0;
0126 }
0127 
0128 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
0129 {
0130     struct opticon_private *priv = usb_get_serial_port_data(port);
0131     unsigned long flags;
0132     int res;
0133 
0134     spin_lock_irqsave(&priv->lock, flags);
0135     priv->rts = false;
0136     spin_unlock_irqrestore(&priv->lock, flags);
0137 
0138     /* Clear RTS line */
0139     send_control_msg(port, CONTROL_RTS, 0);
0140 
0141     /* clear the halt status of the endpoint */
0142     usb_clear_halt(port->serial->dev, port->read_urb->pipe);
0143 
0144     res = usb_serial_generic_open(tty, port);
0145     if (res)
0146         return res;
0147 
0148     /* Request CTS line state, sometimes during opening the current
0149      * CTS state can be missed. */
0150     send_control_msg(port, RESEND_CTS_STATE, 1);
0151 
0152     return res;
0153 }
0154 
0155 static void opticon_close(struct usb_serial_port *port)
0156 {
0157     struct opticon_private *priv = usb_get_serial_port_data(port);
0158 
0159     usb_kill_anchored_urbs(&priv->anchor);
0160 
0161     usb_serial_generic_close(port);
0162 }
0163 
0164 static void opticon_write_control_callback(struct urb *urb)
0165 {
0166     struct usb_serial_port *port = urb->context;
0167     struct opticon_private *priv = usb_get_serial_port_data(port);
0168     int status = urb->status;
0169     unsigned long flags;
0170 
0171     /* free up the transfer buffer, as usb_free_urb() does not do this */
0172     kfree(urb->transfer_buffer);
0173 
0174     /* setup packet may be set if we're using it for writing */
0175     kfree(urb->setup_packet);
0176 
0177     if (status)
0178         dev_dbg(&port->dev,
0179             "%s - non-zero urb status received: %d\n",
0180             __func__, status);
0181 
0182     spin_lock_irqsave(&priv->lock, flags);
0183     --priv->outstanding_urbs;
0184     priv->outstanding_bytes -= urb->transfer_buffer_length;
0185     spin_unlock_irqrestore(&priv->lock, flags);
0186 
0187     usb_serial_port_softint(port);
0188 }
0189 
0190 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
0191              const unsigned char *buf, int count)
0192 {
0193     struct opticon_private *priv = usb_get_serial_port_data(port);
0194     struct usb_serial *serial = port->serial;
0195     struct urb *urb;
0196     unsigned char *buffer;
0197     unsigned long flags;
0198     struct usb_ctrlrequest *dr;
0199     int ret = -ENOMEM;
0200 
0201     spin_lock_irqsave(&priv->lock, flags);
0202     if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
0203         spin_unlock_irqrestore(&priv->lock, flags);
0204         dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
0205         return 0;
0206     }
0207     priv->outstanding_urbs++;
0208     priv->outstanding_bytes += count;
0209     spin_unlock_irqrestore(&priv->lock, flags);
0210 
0211     buffer = kmemdup(buf, count, GFP_ATOMIC);
0212     if (!buffer)
0213         goto error_no_buffer;
0214 
0215     urb = usb_alloc_urb(0, GFP_ATOMIC);
0216     if (!urb)
0217         goto error_no_urb;
0218 
0219     usb_serial_debug_data(&port->dev, __func__, count, buffer);
0220 
0221     /* The connected devices do not have a bulk write endpoint,
0222      * to transmit data to de barcode device the control endpoint is used */
0223     dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
0224     if (!dr)
0225         goto error_no_dr;
0226 
0227     dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
0228     dr->bRequest = 0x01;
0229     dr->wValue = 0;
0230     dr->wIndex = 0;
0231     dr->wLength = cpu_to_le16(count);
0232 
0233     usb_fill_control_urb(urb, serial->dev,
0234         usb_sndctrlpipe(serial->dev, 0),
0235         (unsigned char *)dr, buffer, count,
0236         opticon_write_control_callback, port);
0237 
0238     usb_anchor_urb(urb, &priv->anchor);
0239 
0240     /* send it down the pipe */
0241     ret = usb_submit_urb(urb, GFP_ATOMIC);
0242     if (ret) {
0243         dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
0244         usb_unanchor_urb(urb);
0245         goto error;
0246     }
0247 
0248     /* we are done with this urb, so let the host driver
0249      * really free it when it is finished with it */
0250     usb_free_urb(urb);
0251 
0252     return count;
0253 error:
0254     kfree(dr);
0255 error_no_dr:
0256     usb_free_urb(urb);
0257 error_no_urb:
0258     kfree(buffer);
0259 error_no_buffer:
0260     spin_lock_irqsave(&priv->lock, flags);
0261     --priv->outstanding_urbs;
0262     priv->outstanding_bytes -= count;
0263     spin_unlock_irqrestore(&priv->lock, flags);
0264 
0265     return ret;
0266 }
0267 
0268 static unsigned int opticon_write_room(struct tty_struct *tty)
0269 {
0270     struct usb_serial_port *port = tty->driver_data;
0271     struct opticon_private *priv = usb_get_serial_port_data(port);
0272     unsigned long flags;
0273 
0274     /*
0275      * We really can take almost anything the user throws at us
0276      * but let's pick a nice big number to tell the tty
0277      * layer that we have lots of free space, unless we don't.
0278      */
0279     spin_lock_irqsave(&priv->lock, flags);
0280     if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
0281         spin_unlock_irqrestore(&priv->lock, flags);
0282         dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
0283         return 0;
0284     }
0285     spin_unlock_irqrestore(&priv->lock, flags);
0286 
0287     return 2048;
0288 }
0289 
0290 static unsigned int opticon_chars_in_buffer(struct tty_struct *tty)
0291 {
0292     struct usb_serial_port *port = tty->driver_data;
0293     struct opticon_private *priv = usb_get_serial_port_data(port);
0294     unsigned long flags;
0295     unsigned int count;
0296 
0297     spin_lock_irqsave(&priv->lock, flags);
0298     count = priv->outstanding_bytes;
0299     spin_unlock_irqrestore(&priv->lock, flags);
0300 
0301     return count;
0302 }
0303 
0304 static int opticon_tiocmget(struct tty_struct *tty)
0305 {
0306     struct usb_serial_port *port = tty->driver_data;
0307     struct opticon_private *priv = usb_get_serial_port_data(port);
0308     unsigned long flags;
0309     int result = 0;
0310 
0311     spin_lock_irqsave(&priv->lock, flags);
0312     if (priv->rts)
0313         result |= TIOCM_RTS;
0314     if (priv->cts)
0315         result |= TIOCM_CTS;
0316     spin_unlock_irqrestore(&priv->lock, flags);
0317 
0318     dev_dbg(&port->dev, "%s - %x\n", __func__, result);
0319     return result;
0320 }
0321 
0322 static int opticon_tiocmset(struct tty_struct *tty,
0323                unsigned int set, unsigned int clear)
0324 {
0325     struct usb_serial_port *port = tty->driver_data;
0326     struct opticon_private *priv = usb_get_serial_port_data(port);
0327     unsigned long flags;
0328     bool rts;
0329     bool changed = false;
0330     int ret;
0331 
0332     /* We only support RTS so we only handle that */
0333     spin_lock_irqsave(&priv->lock, flags);
0334 
0335     rts = priv->rts;
0336     if (set & TIOCM_RTS)
0337         priv->rts = true;
0338     if (clear & TIOCM_RTS)
0339         priv->rts = false;
0340     changed = rts ^ priv->rts;
0341     spin_unlock_irqrestore(&priv->lock, flags);
0342 
0343     if (!changed)
0344         return 0;
0345 
0346     ret = send_control_msg(port, CONTROL_RTS, !rts);
0347     if (ret)
0348         return usb_translate_errors(ret);
0349 
0350     return 0;
0351 }
0352 
0353 static int opticon_port_probe(struct usb_serial_port *port)
0354 {
0355     struct opticon_private *priv;
0356 
0357     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0358     if (!priv)
0359         return -ENOMEM;
0360 
0361     spin_lock_init(&priv->lock);
0362     init_usb_anchor(&priv->anchor);
0363 
0364     usb_set_serial_port_data(port, priv);
0365 
0366     return 0;
0367 }
0368 
0369 static void opticon_port_remove(struct usb_serial_port *port)
0370 {
0371     struct opticon_private *priv = usb_get_serial_port_data(port);
0372 
0373     kfree(priv);
0374 }
0375 
0376 static struct usb_serial_driver opticon_device = {
0377     .driver = {
0378         .owner =    THIS_MODULE,
0379         .name =     "opticon",
0380     },
0381     .id_table =     id_table,
0382     .num_ports =        1,
0383     .num_bulk_in =      1,
0384     .bulk_in_size =     256,
0385     .port_probe =       opticon_port_probe,
0386     .port_remove =      opticon_port_remove,
0387     .open =         opticon_open,
0388     .close =        opticon_close,
0389     .write =        opticon_write,
0390     .write_room =       opticon_write_room,
0391     .chars_in_buffer =  opticon_chars_in_buffer,
0392     .throttle =     usb_serial_generic_throttle,
0393     .unthrottle =       usb_serial_generic_unthrottle,
0394     .tiocmget =     opticon_tiocmget,
0395     .tiocmset =     opticon_tiocmset,
0396     .process_read_urb = opticon_process_read_urb,
0397 };
0398 
0399 static struct usb_serial_driver * const serial_drivers[] = {
0400     &opticon_device, NULL
0401 };
0402 
0403 module_usb_serial_driver(serial_drivers, id_table);
0404 
0405 MODULE_DESCRIPTION(DRIVER_DESC);
0406 MODULE_LICENSE("GPL v2");