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