1 /* 2 * Opticon USB barcode to serial driver 3 * 4 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com> 5 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de> 6 * Copyright (C) 2008 - 2009 Novell Inc. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License version 10 * 2 as published by the Free Software Foundation. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/init.h> 15 #include <linux/tty.h> 16 #include <linux/tty_driver.h> 17 #include <linux/slab.h> 18 #include <linux/tty_flip.h> 19 #include <linux/serial.h> 20 #include <linux/module.h> 21 #include <linux/usb.h> 22 #include <linux/usb/serial.h> 23 #include <linux/uaccess.h> 24 25 #define CONTROL_RTS 0x02 26 #define RESEND_CTS_STATE 0x03 27 28 /* max number of write urbs in flight */ 29 #define URB_UPPER_LIMIT 8 30 31 /* This driver works for the Opticon 1D barcode reader 32 * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */ 33 #define DRIVER_DESC "Opticon USB barcode to serial driver (1D)" 34 35 static const struct usb_device_id id_table[] = { 36 { USB_DEVICE(0x065a, 0x0009) }, 37 { }, 38 }; 39 MODULE_DEVICE_TABLE(usb, id_table); 40 41 /* This structure holds all of the individual device information */ 42 struct opticon_private { 43 struct urb *bulk_read_urb; 44 spinlock_t lock; /* protects the following flags */ 45 bool throttled; 46 bool actually_throttled; 47 bool rts; 48 bool cts; 49 int outstanding_urbs; 50 }; 51 52 53 static void opticon_process_data_packet(struct usb_serial_port *port, 54 const unsigned char *buf, size_t len) 55 { 56 struct tty_struct *tty; 57 58 tty = tty_port_tty_get(&port->port); 59 if (!tty) 60 return; 61 62 tty_insert_flip_string(tty, buf, len); 63 tty_flip_buffer_push(tty); 64 tty_kref_put(tty); 65 } 66 67 static void opticon_process_status_packet(struct usb_serial_port *port, 68 const unsigned char *buf, size_t len) 69 { 70 struct opticon_private *priv = usb_get_serial_port_data(port); 71 unsigned long flags; 72 73 spin_lock_irqsave(&priv->lock, flags); 74 if (buf[0] == 0x00) 75 priv->cts = false; 76 else 77 priv->cts = true; 78 spin_unlock_irqrestore(&priv->lock, flags); 79 } 80 81 static void opticon_process_read_urb(struct urb *urb) 82 { 83 struct usb_serial_port *port = urb->context; 84 const unsigned char *hdr = urb->transfer_buffer; 85 const unsigned char *data = hdr + 2; 86 size_t data_len = urb->actual_length - 2; 87 88 if (urb->actual_length <= 2) { 89 dev_dbg(&port->dev, "malformed packet received: %d bytes\n", 90 urb->actual_length); 91 return; 92 } 93 /* 94 * Data from the device comes with a 2 byte header: 95 * 96 * <0x00><0x00>data... 97 * This is real data to be sent to the tty layer 98 * <0x00><0x01>level 99 * This is a CTS level change, the third byte is the CTS 100 * value (0 for low, 1 for high). 101 */ 102 if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) { 103 opticon_process_data_packet(port, data, data_len); 104 } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) { 105 opticon_process_status_packet(port, data, data_len); 106 } else { 107 dev_dbg(&port->dev, "unknown packet received: %02x %02x\n", 108 hdr[0], hdr[1]); 109 } 110 } 111 112 static void opticon_read_bulk_callback(struct urb *urb) 113 { 114 struct usb_serial_port *port = urb->context; 115 struct opticon_private *priv = usb_get_serial_port_data(port); 116 unsigned char *data = urb->transfer_buffer; 117 int status = urb->status; 118 int result; 119 120 switch (status) { 121 case 0: 122 /* success */ 123 break; 124 case -ECONNRESET: 125 case -ENOENT: 126 case -ESHUTDOWN: 127 /* this urb is terminated, clean up */ 128 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", 129 __func__, status); 130 return; 131 default: 132 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", 133 __func__, status); 134 goto exit; 135 } 136 137 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); 138 139 opticon_process_read_urb(urb); 140 exit: 141 spin_lock(&priv->lock); 142 143 /* Continue trying to always read if we should */ 144 if (!priv->throttled) { 145 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); 146 if (result) 147 dev_err(&port->dev, 148 "%s - failed resubmitting read urb, error %d\n", 149 __func__, result); 150 } else 151 priv->actually_throttled = true; 152 spin_unlock(&priv->lock); 153 } 154 155 static int send_control_msg(struct usb_serial_port *port, u8 requesttype, 156 u8 val) 157 { 158 struct usb_serial *serial = port->serial; 159 int retval; 160 u8 *buffer; 161 162 buffer = kzalloc(1, GFP_KERNEL); 163 if (!buffer) 164 return -ENOMEM; 165 166 buffer[0] = val; 167 /* Send the message to the vendor control endpoint 168 * of the connected device */ 169 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 170 requesttype, 171 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 172 0, 0, buffer, 1, 0); 173 kfree(buffer); 174 175 return retval; 176 } 177 178 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port) 179 { 180 struct opticon_private *priv = usb_get_serial_port_data(port); 181 unsigned long flags; 182 int result = 0; 183 184 spin_lock_irqsave(&priv->lock, flags); 185 priv->throttled = false; 186 priv->actually_throttled = false; 187 priv->rts = false; 188 spin_unlock_irqrestore(&priv->lock, flags); 189 190 /* Clear RTS line */ 191 send_control_msg(port, CONTROL_RTS, 0); 192 193 /* clear the halt status of the enpoint */ 194 usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe); 195 196 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL); 197 if (result) 198 dev_err(&port->dev, 199 "%s - failed resubmitting read urb, error %d\n", 200 __func__, result); 201 /* Request CTS line state, sometimes during opening the current 202 * CTS state can be missed. */ 203 send_control_msg(port, RESEND_CTS_STATE, 1); 204 return result; 205 } 206 207 static void opticon_close(struct usb_serial_port *port) 208 { 209 struct opticon_private *priv = usb_get_serial_port_data(port); 210 211 /* shutdown our urbs */ 212 usb_kill_urb(priv->bulk_read_urb); 213 } 214 215 static void opticon_write_control_callback(struct urb *urb) 216 { 217 struct usb_serial_port *port = urb->context; 218 struct opticon_private *priv = usb_get_serial_port_data(port); 219 int status = urb->status; 220 unsigned long flags; 221 222 /* free up the transfer buffer, as usb_free_urb() does not do this */ 223 kfree(urb->transfer_buffer); 224 225 /* setup packet may be set if we're using it for writing */ 226 kfree(urb->setup_packet); 227 228 if (status) 229 dev_dbg(&port->dev, 230 "%s - non-zero urb status received: %d\n", 231 __func__, status); 232 233 spin_lock_irqsave(&priv->lock, flags); 234 --priv->outstanding_urbs; 235 spin_unlock_irqrestore(&priv->lock, flags); 236 237 usb_serial_port_softint(port); 238 } 239 240 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port, 241 const unsigned char *buf, int count) 242 { 243 struct opticon_private *priv = usb_get_serial_port_data(port); 244 struct usb_serial *serial = port->serial; 245 struct urb *urb; 246 unsigned char *buffer; 247 unsigned long flags; 248 int status; 249 struct usb_ctrlrequest *dr; 250 251 spin_lock_irqsave(&priv->lock, flags); 252 if (priv->outstanding_urbs > URB_UPPER_LIMIT) { 253 spin_unlock_irqrestore(&priv->lock, flags); 254 dev_dbg(&port->dev, "%s - write limit hit\n", __func__); 255 return 0; 256 } 257 priv->outstanding_urbs++; 258 spin_unlock_irqrestore(&priv->lock, flags); 259 260 buffer = kmalloc(count, GFP_ATOMIC); 261 if (!buffer) { 262 dev_err(&port->dev, "out of memory\n"); 263 count = -ENOMEM; 264 265 goto error_no_buffer; 266 } 267 268 urb = usb_alloc_urb(0, GFP_ATOMIC); 269 if (!urb) { 270 dev_err(&port->dev, "no more free urbs\n"); 271 count = -ENOMEM; 272 goto error_no_urb; 273 } 274 275 memcpy(buffer, buf, count); 276 277 usb_serial_debug_data(&port->dev, __func__, count, buffer); 278 279 /* The conncected devices do not have a bulk write endpoint, 280 * to transmit data to de barcode device the control endpoint is used */ 281 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); 282 if (!dr) { 283 dev_err(&port->dev, "out of memory\n"); 284 count = -ENOMEM; 285 goto error_no_dr; 286 } 287 288 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT; 289 dr->bRequest = 0x01; 290 dr->wValue = 0; 291 dr->wIndex = 0; 292 dr->wLength = cpu_to_le16(count); 293 294 usb_fill_control_urb(urb, serial->dev, 295 usb_sndctrlpipe(serial->dev, 0), 296 (unsigned char *)dr, buffer, count, 297 opticon_write_control_callback, port); 298 299 /* send it down the pipe */ 300 status = usb_submit_urb(urb, GFP_ATOMIC); 301 if (status) { 302 dev_err(&port->dev, 303 "%s - usb_submit_urb(write endpoint) failed status = %d\n", 304 __func__, status); 305 count = status; 306 goto error; 307 } 308 309 /* we are done with this urb, so let the host driver 310 * really free it when it is finished with it */ 311 usb_free_urb(urb); 312 313 return count; 314 error: 315 kfree(dr); 316 error_no_dr: 317 usb_free_urb(urb); 318 error_no_urb: 319 kfree(buffer); 320 error_no_buffer: 321 spin_lock_irqsave(&priv->lock, flags); 322 --priv->outstanding_urbs; 323 spin_unlock_irqrestore(&priv->lock, flags); 324 return count; 325 } 326 327 static int opticon_write_room(struct tty_struct *tty) 328 { 329 struct usb_serial_port *port = tty->driver_data; 330 struct opticon_private *priv = usb_get_serial_port_data(port); 331 unsigned long flags; 332 333 /* 334 * We really can take almost anything the user throws at us 335 * but let's pick a nice big number to tell the tty 336 * layer that we have lots of free space, unless we don't. 337 */ 338 spin_lock_irqsave(&priv->lock, flags); 339 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) { 340 spin_unlock_irqrestore(&priv->lock, flags); 341 dev_dbg(&port->dev, "%s - write limit hit\n", __func__); 342 return 0; 343 } 344 spin_unlock_irqrestore(&priv->lock, flags); 345 346 return 2048; 347 } 348 349 static void opticon_throttle(struct tty_struct *tty) 350 { 351 struct usb_serial_port *port = tty->driver_data; 352 struct opticon_private *priv = usb_get_serial_port_data(port); 353 unsigned long flags; 354 355 spin_lock_irqsave(&priv->lock, flags); 356 priv->throttled = true; 357 spin_unlock_irqrestore(&priv->lock, flags); 358 } 359 360 361 static void opticon_unthrottle(struct tty_struct *tty) 362 { 363 struct usb_serial_port *port = tty->driver_data; 364 struct opticon_private *priv = usb_get_serial_port_data(port); 365 unsigned long flags; 366 int result, was_throttled; 367 368 spin_lock_irqsave(&priv->lock, flags); 369 priv->throttled = false; 370 was_throttled = priv->actually_throttled; 371 priv->actually_throttled = false; 372 spin_unlock_irqrestore(&priv->lock, flags); 373 374 if (was_throttled) { 375 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC); 376 if (result) 377 dev_err(&port->dev, 378 "%s - failed submitting read urb, error %d\n", 379 __func__, result); 380 } 381 } 382 383 static int opticon_tiocmget(struct tty_struct *tty) 384 { 385 struct usb_serial_port *port = tty->driver_data; 386 struct opticon_private *priv = usb_get_serial_port_data(port); 387 unsigned long flags; 388 int result = 0; 389 390 spin_lock_irqsave(&priv->lock, flags); 391 if (priv->rts) 392 result |= TIOCM_RTS; 393 if (priv->cts) 394 result |= TIOCM_CTS; 395 spin_unlock_irqrestore(&priv->lock, flags); 396 397 dev_dbg(&port->dev, "%s - %x\n", __func__, result); 398 return result; 399 } 400 401 static int opticon_tiocmset(struct tty_struct *tty, 402 unsigned int set, unsigned int clear) 403 { 404 struct usb_serial_port *port = tty->driver_data; 405 struct usb_serial *serial = port->serial; 406 struct opticon_private *priv = usb_get_serial_port_data(port); 407 unsigned long flags; 408 bool rts; 409 bool changed = false; 410 int ret; 411 412 /* We only support RTS so we only handle that */ 413 spin_lock_irqsave(&priv->lock, flags); 414 415 rts = priv->rts; 416 if (set & TIOCM_RTS) 417 priv->rts = true; 418 if (clear & TIOCM_RTS) 419 priv->rts = false; 420 changed = rts ^ priv->rts; 421 spin_unlock_irqrestore(&priv->lock, flags); 422 423 if (!changed) 424 return 0; 425 426 /* Send the new RTS state to the connected device */ 427 mutex_lock(&serial->disc_mutex); 428 if (!serial->disconnected) 429 ret = send_control_msg(port, CONTROL_RTS, !rts); 430 else 431 ret = -ENODEV; 432 mutex_unlock(&serial->disc_mutex); 433 434 return ret; 435 } 436 437 static int get_serial_info(struct usb_serial_port *port, 438 struct serial_struct __user *serial) 439 { 440 struct serial_struct tmp; 441 442 if (!serial) 443 return -EFAULT; 444 445 memset(&tmp, 0x00, sizeof(tmp)); 446 447 /* fake emulate a 16550 uart to make userspace code happy */ 448 tmp.type = PORT_16550A; 449 tmp.line = port->serial->minor; 450 tmp.port = 0; 451 tmp.irq = 0; 452 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 453 tmp.xmit_fifo_size = 1024; 454 tmp.baud_base = 9600; 455 tmp.close_delay = 5*HZ; 456 tmp.closing_wait = 30*HZ; 457 458 if (copy_to_user(serial, &tmp, sizeof(*serial))) 459 return -EFAULT; 460 return 0; 461 } 462 463 static int opticon_ioctl(struct tty_struct *tty, 464 unsigned int cmd, unsigned long arg) 465 { 466 struct usb_serial_port *port = tty->driver_data; 467 468 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd); 469 470 switch (cmd) { 471 case TIOCGSERIAL: 472 return get_serial_info(port, 473 (struct serial_struct __user *)arg); 474 } 475 476 return -ENOIOCTLCMD; 477 } 478 479 static int opticon_startup(struct usb_serial *serial) 480 { 481 if (!serial->num_bulk_in) { 482 dev_err(&serial->dev->dev, "no bulk in endpoint\n"); 483 return -ENODEV; 484 } 485 486 return 0; 487 } 488 489 static int opticon_port_probe(struct usb_serial_port *port) 490 { 491 struct opticon_private *priv; 492 493 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 494 if (!priv) 495 return -ENOMEM; 496 497 spin_lock_init(&priv->lock); 498 priv->bulk_read_urb = port->read_urbs[0]; 499 500 usb_set_serial_port_data(port, priv); 501 502 return 0; 503 } 504 505 static int opticon_port_remove(struct usb_serial_port *port) 506 { 507 struct opticon_private *priv = usb_get_serial_port_data(port); 508 509 kfree(priv); 510 511 return 0; 512 } 513 514 static int opticon_suspend(struct usb_serial *serial, pm_message_t message) 515 { 516 struct opticon_private *priv; 517 518 priv = usb_get_serial_port_data(serial->port[0]); 519 520 usb_kill_urb(priv->bulk_read_urb); 521 return 0; 522 } 523 524 static int opticon_resume(struct usb_serial *serial) 525 { 526 struct usb_serial_port *port = serial->port[0]; 527 struct opticon_private *priv = usb_get_serial_port_data(port); 528 int result; 529 530 mutex_lock(&port->port.mutex); 531 /* This is protected by the port mutex against close/open */ 532 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags)) 533 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO); 534 else 535 result = 0; 536 mutex_unlock(&port->port.mutex); 537 return result; 538 } 539 540 static struct usb_serial_driver opticon_device = { 541 .driver = { 542 .owner = THIS_MODULE, 543 .name = "opticon", 544 }, 545 .id_table = id_table, 546 .num_ports = 1, 547 .bulk_in_size = 256, 548 .attach = opticon_startup, 549 .port_probe = opticon_port_probe, 550 .port_remove = opticon_port_remove, 551 .open = opticon_open, 552 .close = opticon_close, 553 .write = opticon_write, 554 .write_room = opticon_write_room, 555 .throttle = opticon_throttle, 556 .unthrottle = opticon_unthrottle, 557 .ioctl = opticon_ioctl, 558 .tiocmget = opticon_tiocmget, 559 .tiocmset = opticon_tiocmset, 560 .suspend = opticon_suspend, 561 .resume = opticon_resume, 562 .read_bulk_callback = opticon_read_bulk_callback, 563 }; 564 565 static struct usb_serial_driver * const serial_drivers[] = { 566 &opticon_device, NULL 567 }; 568 569 module_usb_serial_driver(serial_drivers, id_table); 570 571 MODULE_DESCRIPTION(DRIVER_DESC); 572 MODULE_LICENSE("GPL"); 573