1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * USB IR Dongle driver 4 * 5 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com) 6 * Copyright (C) 2002 Gary Brubaker (xavyer@ix.netcom.com) 7 * Copyright (C) 2010 Johan Hovold (jhovold@gmail.com) 8 * 9 * This driver allows a USB IrDA device to be used as a "dumb" serial device. 10 * This can be useful if you do not have access to a full IrDA stack on the 11 * other side of the connection. If you do have an IrDA stack on both devices, 12 * please use the usb-irda driver, as it contains the proper error checking and 13 * other goodness of a full IrDA stack. 14 * 15 * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which 16 * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli 17 * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com> 18 * 19 * See Documentation/usb/usb-serial.rst for more information on using this 20 * driver 21 */ 22 23 #include <linux/kernel.h> 24 #include <linux/errno.h> 25 #include <linux/init.h> 26 #include <linux/slab.h> 27 #include <linux/tty.h> 28 #include <linux/tty_flip.h> 29 #include <linux/module.h> 30 #include <linux/spinlock.h> 31 #include <linux/usb.h> 32 #include <linux/usb/serial.h> 33 #include <linux/usb/irda.h> 34 35 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Johan Hovold <jhovold@gmail.com>" 36 #define DRIVER_DESC "USB IR Dongle driver" 37 38 /* if overridden by the user, then use their value for the size of the read and 39 * write urbs */ 40 static int buffer_size; 41 42 /* if overridden by the user, then use the specified number of XBOFs */ 43 static int xbof = -1; 44 45 static int ir_startup (struct usb_serial *serial); 46 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port, 47 const unsigned char *buf, int count); 48 static unsigned int ir_write_room(struct tty_struct *tty); 49 static void ir_write_bulk_callback(struct urb *urb); 50 static void ir_process_read_urb(struct urb *urb); 51 static void ir_set_termios(struct tty_struct *tty, 52 struct usb_serial_port *port, 53 const struct ktermios *old_termios); 54 55 /* Not that this lot means you can only have one per system */ 56 static u8 ir_baud; 57 static u8 ir_xbof; 58 static u8 ir_add_bof; 59 60 static const struct usb_device_id ir_id_table[] = { 61 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */ 62 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */ 63 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */ 64 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) }, 65 { } /* Terminating entry */ 66 }; 67 68 MODULE_DEVICE_TABLE(usb, ir_id_table); 69 70 static struct usb_serial_driver ir_device = { 71 .driver = { 72 .name = "ir-usb", 73 }, 74 .description = "IR Dongle", 75 .id_table = ir_id_table, 76 .num_ports = 1, 77 .num_bulk_in = 1, 78 .num_bulk_out = 1, 79 .set_termios = ir_set_termios, 80 .attach = ir_startup, 81 .write = ir_write, 82 .write_room = ir_write_room, 83 .write_bulk_callback = ir_write_bulk_callback, 84 .process_read_urb = ir_process_read_urb, 85 }; 86 87 static struct usb_serial_driver * const serial_drivers[] = { 88 &ir_device, NULL 89 }; 90 91 static inline void irda_usb_dump_class_desc(struct usb_serial *serial, 92 struct usb_irda_cs_descriptor *desc) 93 { 94 struct device *dev = &serial->dev->dev; 95 96 dev_dbg(dev, "bLength=%x\n", desc->bLength); 97 dev_dbg(dev, "bDescriptorType=%x\n", desc->bDescriptorType); 98 dev_dbg(dev, "bcdSpecRevision=%x\n", __le16_to_cpu(desc->bcdSpecRevision)); 99 dev_dbg(dev, "bmDataSize=%x\n", desc->bmDataSize); 100 dev_dbg(dev, "bmWindowSize=%x\n", desc->bmWindowSize); 101 dev_dbg(dev, "bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime); 102 dev_dbg(dev, "wBaudRate=%x\n", __le16_to_cpu(desc->wBaudRate)); 103 dev_dbg(dev, "bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs); 104 dev_dbg(dev, "bIrdaRateSniff=%x\n", desc->bIrdaRateSniff); 105 dev_dbg(dev, "bMaxUnicastList=%x\n", desc->bMaxUnicastList); 106 } 107 108 /*------------------------------------------------------------------*/ 109 /* 110 * Function irda_usb_find_class_desc(dev, ifnum) 111 * 112 * Returns instance of IrDA class descriptor, or NULL if not found 113 * 114 * The class descriptor is some extra info that IrDA USB devices will 115 * offer to us, describing their IrDA characteristics. We will use that in 116 * irda_usb_init_qos() 117 * 118 * Based on the same function in drivers/net/irda/irda-usb.c 119 */ 120 static struct usb_irda_cs_descriptor * 121 irda_usb_find_class_desc(struct usb_serial *serial, unsigned int ifnum) 122 { 123 struct usb_device *dev = serial->dev; 124 struct usb_irda_cs_descriptor *desc; 125 int ret; 126 127 desc = kzalloc_obj(*desc); 128 if (!desc) 129 return NULL; 130 131 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 132 USB_REQ_CS_IRDA_GET_CLASS_DESC, 133 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 134 0, ifnum, desc, sizeof(*desc), 1000); 135 136 dev_dbg(&serial->dev->dev, "%s - ret=%d\n", __func__, ret); 137 if (ret < (int)sizeof(*desc)) { 138 dev_dbg(&serial->dev->dev, 139 "%s - class descriptor read %s (%d)\n", __func__, 140 (ret < 0) ? "failed" : "too short", ret); 141 goto error; 142 } 143 if (desc->bDescriptorType != USB_DT_CS_IRDA) { 144 dev_dbg(&serial->dev->dev, "%s - bad class descriptor type\n", 145 __func__); 146 goto error; 147 } 148 149 irda_usb_dump_class_desc(serial, desc); 150 return desc; 151 152 error: 153 kfree(desc); 154 return NULL; 155 } 156 157 static u8 ir_xbof_change(u8 xbof) 158 { 159 u8 result; 160 161 /* reference irda-usb.c */ 162 switch (xbof) { 163 case 48: 164 result = 0x10; 165 break; 166 case 28: 167 case 24: 168 result = 0x20; 169 break; 170 default: 171 case 12: 172 result = 0x30; 173 break; 174 case 5: 175 case 6: 176 result = 0x40; 177 break; 178 case 3: 179 result = 0x50; 180 break; 181 case 2: 182 result = 0x60; 183 break; 184 case 1: 185 result = 0x70; 186 break; 187 case 0: 188 result = 0x80; 189 break; 190 } 191 192 return(result); 193 } 194 195 static int ir_startup(struct usb_serial *serial) 196 { 197 struct usb_irda_cs_descriptor *irda_desc; 198 int rates; 199 200 irda_desc = irda_usb_find_class_desc(serial, 0); 201 if (!irda_desc) { 202 dev_err(&serial->dev->dev, 203 "IRDA class descriptor not found, device not bound\n"); 204 return -ENODEV; 205 } 206 207 rates = le16_to_cpu(irda_desc->wBaudRate); 208 209 dev_dbg(&serial->dev->dev, 210 "%s - Baud rates supported:%s%s%s%s%s%s%s%s%s\n", 211 __func__, 212 (rates & USB_IRDA_BR_2400) ? " 2400" : "", 213 (rates & USB_IRDA_BR_9600) ? " 9600" : "", 214 (rates & USB_IRDA_BR_19200) ? " 19200" : "", 215 (rates & USB_IRDA_BR_38400) ? " 38400" : "", 216 (rates & USB_IRDA_BR_57600) ? " 57600" : "", 217 (rates & USB_IRDA_BR_115200) ? " 115200" : "", 218 (rates & USB_IRDA_BR_576000) ? " 576000" : "", 219 (rates & USB_IRDA_BR_1152000) ? " 1152000" : "", 220 (rates & USB_IRDA_BR_4000000) ? " 4000000" : ""); 221 222 switch (irda_desc->bmAdditionalBOFs) { 223 case USB_IRDA_AB_48: 224 ir_add_bof = 48; 225 break; 226 case USB_IRDA_AB_24: 227 ir_add_bof = 24; 228 break; 229 case USB_IRDA_AB_12: 230 ir_add_bof = 12; 231 break; 232 case USB_IRDA_AB_6: 233 ir_add_bof = 6; 234 break; 235 case USB_IRDA_AB_3: 236 ir_add_bof = 3; 237 break; 238 case USB_IRDA_AB_2: 239 ir_add_bof = 2; 240 break; 241 case USB_IRDA_AB_1: 242 ir_add_bof = 1; 243 break; 244 case USB_IRDA_AB_0: 245 ir_add_bof = 0; 246 break; 247 default: 248 break; 249 } 250 251 kfree(irda_desc); 252 253 return 0; 254 } 255 256 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port, 257 const unsigned char *buf, int count) 258 { 259 struct urb *urb = NULL; 260 unsigned long flags; 261 int ret; 262 263 if (port->bulk_out_size == 0) 264 return -EINVAL; 265 266 if (count == 0) 267 return 0; 268 269 count = min(count, port->bulk_out_size - 1); 270 271 spin_lock_irqsave(&port->lock, flags); 272 if (__test_and_clear_bit(0, &port->write_urbs_free)) { 273 urb = port->write_urbs[0]; 274 port->tx_bytes += count; 275 } 276 spin_unlock_irqrestore(&port->lock, flags); 277 278 if (!urb) 279 return 0; 280 281 /* 282 * The first byte of the packet we send to the device contains an 283 * outbound header which indicates an additional number of BOFs and 284 * a baud rate change. 285 * 286 * See section 5.4.2.2 of the USB IrDA spec. 287 */ 288 *(u8 *)urb->transfer_buffer = ir_xbof | ir_baud; 289 290 memcpy(urb->transfer_buffer + 1, buf, count); 291 292 urb->transfer_buffer_length = count + 1; 293 urb->transfer_flags = URB_ZERO_PACKET; 294 295 ret = usb_submit_urb(urb, GFP_ATOMIC); 296 if (ret) { 297 dev_err(&port->dev, "failed to submit write urb: %d\n", ret); 298 299 spin_lock_irqsave(&port->lock, flags); 300 __set_bit(0, &port->write_urbs_free); 301 port->tx_bytes -= count; 302 spin_unlock_irqrestore(&port->lock, flags); 303 304 return ret; 305 } 306 307 return count; 308 } 309 310 static void ir_write_bulk_callback(struct urb *urb) 311 { 312 struct usb_serial_port *port = urb->context; 313 int status = urb->status; 314 unsigned long flags; 315 316 spin_lock_irqsave(&port->lock, flags); 317 __set_bit(0, &port->write_urbs_free); 318 port->tx_bytes -= urb->transfer_buffer_length - 1; 319 spin_unlock_irqrestore(&port->lock, flags); 320 321 switch (status) { 322 case 0: 323 break; 324 case -ENOENT: 325 case -ECONNRESET: 326 case -ESHUTDOWN: 327 dev_dbg(&port->dev, "write urb stopped: %d\n", status); 328 return; 329 case -EPIPE: 330 dev_err(&port->dev, "write urb stopped: %d\n", status); 331 return; 332 default: 333 dev_err(&port->dev, "nonzero write-urb status: %d\n", status); 334 break; 335 } 336 337 usb_serial_port_softint(port); 338 } 339 340 static unsigned int ir_write_room(struct tty_struct *tty) 341 { 342 struct usb_serial_port *port = tty->driver_data; 343 unsigned int count = 0; 344 345 if (port->bulk_out_size == 0) 346 return 0; 347 348 if (test_bit(0, &port->write_urbs_free)) 349 count = port->bulk_out_size - 1; 350 351 return count; 352 } 353 354 static void ir_process_read_urb(struct urb *urb) 355 { 356 struct usb_serial_port *port = urb->context; 357 unsigned char *data = urb->transfer_buffer; 358 359 if (!urb->actual_length) 360 return; 361 /* 362 * The first byte of the packet we get from the device 363 * contains a busy indicator and baud rate change. 364 * See section 5.4.1.2 of the USB IrDA spec. 365 */ 366 if (*data & 0x0f) 367 ir_baud = *data & 0x0f; 368 369 if (urb->actual_length == 1) 370 return; 371 372 tty_insert_flip_string(&port->port, data + 1, urb->actual_length - 1); 373 tty_flip_buffer_push(&port->port); 374 } 375 376 static void ir_set_termios(struct tty_struct *tty, 377 struct usb_serial_port *port, 378 const struct ktermios *old_termios) 379 { 380 struct usb_device *udev = port->serial->dev; 381 unsigned char *transfer_buffer; 382 int actual_length; 383 speed_t baud; 384 int ir_baud; 385 int ret; 386 387 baud = tty_get_baud_rate(tty); 388 389 /* 390 * FIXME, we should compare the baud request against the 391 * capability stated in the IR header that we got in the 392 * startup function. 393 */ 394 395 switch (baud) { 396 case 2400: 397 ir_baud = USB_IRDA_LS_2400; 398 break; 399 case 9600: 400 ir_baud = USB_IRDA_LS_9600; 401 break; 402 case 19200: 403 ir_baud = USB_IRDA_LS_19200; 404 break; 405 case 38400: 406 ir_baud = USB_IRDA_LS_38400; 407 break; 408 case 57600: 409 ir_baud = USB_IRDA_LS_57600; 410 break; 411 case 115200: 412 ir_baud = USB_IRDA_LS_115200; 413 break; 414 case 576000: 415 ir_baud = USB_IRDA_LS_576000; 416 break; 417 case 1152000: 418 ir_baud = USB_IRDA_LS_1152000; 419 break; 420 case 4000000: 421 ir_baud = USB_IRDA_LS_4000000; 422 break; 423 default: 424 ir_baud = USB_IRDA_LS_9600; 425 baud = 9600; 426 } 427 428 if (xbof == -1) 429 ir_xbof = ir_xbof_change(ir_add_bof); 430 else 431 ir_xbof = ir_xbof_change(xbof) ; 432 433 /* Only speed changes are supported */ 434 tty_termios_copy_hw(&tty->termios, old_termios); 435 tty_encode_baud_rate(tty, baud, baud); 436 437 /* 438 * send the baud change out on an "empty" data packet 439 */ 440 transfer_buffer = kmalloc(1, GFP_KERNEL); 441 if (!transfer_buffer) 442 return; 443 444 *transfer_buffer = ir_xbof | ir_baud; 445 446 ret = usb_bulk_msg(udev, 447 usb_sndbulkpipe(udev, port->bulk_out_endpointAddress), 448 transfer_buffer, 1, &actual_length, 5000); 449 if (ret || actual_length != 1) { 450 if (!ret) 451 ret = -EIO; 452 dev_err(&port->dev, "failed to change line speed: %d\n", ret); 453 } 454 455 kfree(transfer_buffer); 456 } 457 458 static int __init ir_init(void) 459 { 460 if (buffer_size) { 461 ir_device.bulk_in_size = buffer_size; 462 ir_device.bulk_out_size = buffer_size; 463 } 464 465 return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ir_id_table); 466 } 467 468 static void __exit ir_exit(void) 469 { 470 usb_serial_deregister_drivers(serial_drivers); 471 } 472 473 474 module_init(ir_init); 475 module_exit(ir_exit); 476 477 MODULE_AUTHOR(DRIVER_AUTHOR); 478 MODULE_DESCRIPTION(DRIVER_DESC); 479 MODULE_LICENSE("GPL"); 480 481 module_param(xbof, int, 0); 482 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs"); 483 module_param(buffer_size, int, 0); 484 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers"); 485 486