2017-11-03 11:28:30 +01:00
// SPDX-License-Identifier: GPL-2.0+
2005-04-16 15:20:36 -07:00
/*
* USB IR Dongle driver
*
* Copyright ( C ) 2001 - 2002 Greg Kroah - Hartman ( greg @ kroah . com )
* Copyright ( C ) 2002 Gary Brubaker ( xavyer @ ix . netcom . com )
2010-05-13 21:02:03 +02:00
* Copyright ( C ) 2010 Johan Hovold ( jhovold @ gmail . com )
2005-04-16 15:20:36 -07:00
*
* This driver allows a USB IrDA device to be used as a " dumb " serial device .
* This can be useful if you do not have access to a full IrDA stack on the
* other side of the connection . If you do have an IrDA stack on both devices ,
* please use the usb - irda driver , as it contains the proper error checking and
* other goodness of a full IrDA stack .
*
* Portions of this driver were taken from drivers / net / irda / irda - usb . c , which
* was written by Roman Weissgaerber < weissg @ vienna . at > , Dag Brattli
* < dag @ brattli . net > , and Jean Tourrilhes < jt @ hpl . hp . com >
*
2019-06-18 18:05:38 -03:00
* See Documentation / usb / usb - serial . rst for more information on using this
2008-06-03 14:47:52 +03:00
* driver
2005-04-16 15:20:36 -07:00
*/
# include <linux/kernel.h>
# include <linux/errno.h>
# include <linux/init.h>
# include <linux/slab.h>
# include <linux/tty.h>
# include <linux/tty_driver.h>
# include <linux/tty_flip.h>
# include <linux/module.h>
# include <linux/spinlock.h>
2008-06-03 14:47:52 +03:00
# include <linux/uaccess.h>
2005-04-16 15:20:36 -07:00
# include <linux/usb.h>
2006-07-11 21:22:58 -07:00
# include <linux/usb/serial.h>
2008-06-03 14:47:52 +03:00
# include <linux/usb/irda.h>
2005-04-16 15:20:36 -07:00
2010-05-13 21:02:03 +02:00
# define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Johan Hovold <jhovold@gmail.com>"
2005-04-16 15:20:36 -07:00
# define DRIVER_DESC "USB IR Dongle driver"
/* if overridden by the user, then use their value for the size of the read and
* write urbs */
static int buffer_size ;
2008-06-03 14:47:52 +03:00
2005-04-16 15:20:36 -07:00
/* if overridden by the user, then use the specified number of XBOFs */
static int xbof = - 1 ;
2008-07-22 11:09:07 +01:00
static int ir_startup ( struct usb_serial * serial ) ;
2020-01-22 11:15:28 +01:00
static int ir_write ( struct tty_struct * tty , struct usb_serial_port * port ,
const unsigned char * buf , int count ) ;
static int ir_write_room ( struct tty_struct * tty ) ;
static void ir_write_bulk_callback ( struct urb * urb ) ;
2010-05-13 21:02:03 +02:00
static void ir_process_read_urb ( struct urb * urb ) ;
2008-07-22 11:09:07 +01:00
static void ir_set_termios ( struct tty_struct * tty ,
struct usb_serial_port * port , struct ktermios * old_termios ) ;
2005-04-16 15:20:36 -07:00
2007-06-22 14:44:54 +01:00
/* Not that this lot means you can only have one per system */
2008-06-03 14:47:52 +03:00
static u8 ir_baud ;
static u8 ir_xbof ;
static u8 ir_add_bof ;
2005-04-16 15:20:36 -07:00
2010-01-10 15:34:24 +01:00
static const struct usb_device_id ir_id_table [ ] = {
2005-04-16 15:20:36 -07:00
{ USB_DEVICE ( 0x050f , 0x0180 ) } , /* KC Technology, KC-180 */
{ USB_DEVICE ( 0x08e9 , 0x0100 ) } , /* XTNDAccess */
{ USB_DEVICE ( 0x09c4 , 0x0011 ) } , /* ACTiSys ACT-IR2000U */
2008-06-03 14:47:52 +03:00
{ USB_INTERFACE_INFO ( USB_CLASS_APP_SPEC , USB_SUBCLASS_IRDA , 0 ) } ,
2005-04-16 15:20:36 -07:00
{ } /* Terminating entry */
} ;
2008-06-03 14:47:52 +03:00
MODULE_DEVICE_TABLE ( usb , ir_id_table ) ;
2005-04-16 15:20:36 -07:00
2005-06-20 21:15:16 -07:00
static struct usb_serial_driver ir_device = {
2008-06-03 14:47:52 +03:00
. driver = {
. owner = THIS_MODULE ,
. name = " ir-usb " ,
2005-06-20 21:15:16 -07:00
} ,
2008-06-03 14:47:52 +03:00
. description = " IR Dongle " ,
. id_table = ir_id_table ,
. num_ports = 1 ,
2020-01-22 11:15:30 +01:00
. num_bulk_in = 1 ,
. num_bulk_out = 1 ,
2008-06-03 14:47:52 +03:00
. set_termios = ir_set_termios ,
. attach = ir_startup ,
2020-01-22 11:15:28 +01:00
. write = ir_write ,
. write_room = ir_write_room ,
. write_bulk_callback = ir_write_bulk_callback ,
2010-05-13 21:02:03 +02:00
. process_read_urb = ir_process_read_urb ,
2005-04-16 15:20:36 -07:00
} ;
2012-02-23 14:56:57 -05:00
static struct usb_serial_driver * const serial_drivers [ ] = {
& ir_device , NULL
} ;
2012-05-15 16:27:23 -07:00
static inline void irda_usb_dump_class_desc ( struct usb_serial * serial ,
struct usb_irda_cs_descriptor * desc )
2005-04-16 15:20:36 -07:00
{
2012-05-15 16:27:23 -07:00
struct device * dev = & serial - > dev - > dev ;
dev_dbg ( dev , " bLength=%x \n " , desc - > bLength ) ;
dev_dbg ( dev , " bDescriptorType=%x \n " , desc - > bDescriptorType ) ;
dev_dbg ( dev , " bcdSpecRevision=%x \n " , __le16_to_cpu ( desc - > bcdSpecRevision ) ) ;
dev_dbg ( dev , " bmDataSize=%x \n " , desc - > bmDataSize ) ;
dev_dbg ( dev , " bmWindowSize=%x \n " , desc - > bmWindowSize ) ;
dev_dbg ( dev , " bmMinTurnaroundTime=%d \n " , desc - > bmMinTurnaroundTime ) ;
dev_dbg ( dev , " wBaudRate=%x \n " , __le16_to_cpu ( desc - > wBaudRate ) ) ;
dev_dbg ( dev , " bmAdditionalBOFs=%x \n " , desc - > bmAdditionalBOFs ) ;
dev_dbg ( dev , " bIrdaRateSniff=%x \n " , desc - > bIrdaRateSniff ) ;
dev_dbg ( dev , " bMaxUnicastList=%x \n " , desc - > bMaxUnicastList ) ;
2005-04-16 15:20:36 -07:00
}
/*------------------------------------------------------------------*/
/*
* Function irda_usb_find_class_desc ( dev , ifnum )
*
* Returns instance of IrDA class descriptor , or NULL if not found
*
* The class descriptor is some extra info that IrDA USB devices will
* offer to us , describing their IrDA characteristics . We will use that in
* irda_usb_init_qos ( )
*
* Based on the same function in drivers / net / irda / irda - usb . c
*/
2008-06-03 14:47:52 +03:00
static struct usb_irda_cs_descriptor *
2012-05-15 16:27:23 -07:00
irda_usb_find_class_desc ( struct usb_serial * serial , unsigned int ifnum )
2005-04-16 15:20:36 -07:00
{
2012-05-15 16:27:23 -07:00
struct usb_device * dev = serial - > dev ;
2008-06-03 14:47:52 +03:00
struct usb_irda_cs_descriptor * desc ;
2005-04-16 15:20:36 -07:00
int ret ;
2008-06-03 14:47:52 +03:00
desc = kzalloc ( sizeof ( * desc ) , GFP_KERNEL ) ;
if ( ! desc )
2005-04-16 15:20:36 -07:00
return NULL ;
2008-06-03 14:47:52 +03:00
ret = usb_control_msg ( dev , usb_rcvctrlpipe ( dev , 0 ) ,
USB_REQ_CS_IRDA_GET_CLASS_DESC ,
2005-04-16 15:20:36 -07:00
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE ,
0 , ifnum , desc , sizeof ( * desc ) , 1000 ) ;
2008-06-03 14:47:52 +03:00
2012-05-15 16:27:23 -07:00
dev_dbg ( & serial - > dev - > dev , " %s - ret=%d \n " , __func__ , ret ) ;
2018-06-25 15:35:18 +08:00
if ( ret < ( int ) sizeof ( * desc ) ) {
2012-05-15 16:27:23 -07:00
dev_dbg ( & serial - > dev - > dev ,
" %s - class descriptor read %s (%d) \n " , __func__ ,
( ret < 0 ) ? " failed " : " too short " , ret ) ;
2005-04-16 15:20:36 -07:00
goto error ;
}
2008-06-03 14:47:52 +03:00
if ( desc - > bDescriptorType ! = USB_DT_CS_IRDA ) {
2012-05-15 16:27:23 -07:00
dev_dbg ( & serial - > dev - > dev , " %s - bad class descriptor type \n " ,
__func__ ) ;
2005-04-16 15:20:36 -07:00
goto error ;
}
2008-06-03 14:47:52 +03:00
2012-05-15 16:27:23 -07:00
irda_usb_dump_class_desc ( serial , desc ) ;
2005-04-16 15:20:36 -07:00
return desc ;
2008-06-03 14:47:52 +03:00
2005-04-16 15:20:36 -07:00
error :
kfree ( desc ) ;
return NULL ;
}
static u8 ir_xbof_change ( u8 xbof )
{
u8 result ;
2008-06-03 14:47:52 +03:00
2005-04-16 15:20:36 -07:00
/* reference irda-usb.c */
2008-06-03 14:47:52 +03:00
switch ( xbof ) {
case 48 :
result = 0x10 ;
break ;
case 28 :
case 24 :
result = 0x20 ;
break ;
default :
case 12 :
result = 0x30 ;
break ;
case 5 :
case 6 :
result = 0x40 ;
break ;
case 3 :
result = 0x50 ;
break ;
case 2 :
result = 0x60 ;
break ;
case 1 :
result = 0x70 ;
break ;
case 0 :
result = 0x80 ;
break ;
2005-04-16 15:20:36 -07:00
}
2008-06-03 14:47:52 +03:00
2005-04-16 15:20:36 -07:00
return ( result ) ;
}
2008-06-03 14:47:52 +03:00
static int ir_startup ( struct usb_serial * serial )
2005-04-16 15:20:36 -07:00
{
2008-06-03 14:47:52 +03:00
struct usb_irda_cs_descriptor * irda_desc ;
2017-05-11 11:41:19 +02:00
int rates ;
2005-04-16 15:20:36 -07:00
2012-05-15 16:27:23 -07:00
irda_desc = irda_usb_find_class_desc ( serial , 0 ) ;
2008-06-03 14:47:52 +03:00
if ( ! irda_desc ) {
dev_err ( & serial - > dev - > dev ,
" IRDA class descriptor not found, device not bound \n " ) ;
2005-04-16 15:20:36 -07:00
return - ENODEV ;
}
2017-05-11 11:41:19 +02:00
rates = le16_to_cpu ( irda_desc - > wBaudRate ) ;
2012-05-15 16:27:23 -07:00
dev_dbg ( & serial - > dev - > dev ,
" %s - Baud rates supported:%s%s%s%s%s%s%s%s%s \n " ,
2008-03-03 16:08:34 -08:00
__func__ ,
2017-05-11 11:41:19 +02:00
( rates & USB_IRDA_BR_2400 ) ? " 2400 " : " " ,
( rates & USB_IRDA_BR_9600 ) ? " 9600 " : " " ,
( rates & USB_IRDA_BR_19200 ) ? " 19200 " : " " ,
( rates & USB_IRDA_BR_38400 ) ? " 38400 " : " " ,
( rates & USB_IRDA_BR_57600 ) ? " 57600 " : " " ,
( rates & USB_IRDA_BR_115200 ) ? " 115200 " : " " ,
( rates & USB_IRDA_BR_576000 ) ? " 576000 " : " " ,
( rates & USB_IRDA_BR_1152000 ) ? " 1152000 " : " " ,
( rates & USB_IRDA_BR_4000000 ) ? " 4000000 " : " " ) ;
2008-06-03 14:47:52 +03:00
switch ( irda_desc - > bmAdditionalBOFs ) {
case USB_IRDA_AB_48 :
ir_add_bof = 48 ;
break ;
case USB_IRDA_AB_24 :
ir_add_bof = 24 ;
break ;
case USB_IRDA_AB_12 :
ir_add_bof = 12 ;
break ;
case USB_IRDA_AB_6 :
ir_add_bof = 6 ;
break ;
case USB_IRDA_AB_3 :
ir_add_bof = 3 ;
break ;
case USB_IRDA_AB_2 :
ir_add_bof = 2 ;
break ;
case USB_IRDA_AB_1 :
ir_add_bof = 1 ;
break ;
case USB_IRDA_AB_0 :
ir_add_bof = 0 ;
break ;
default :
break ;
2005-04-16 15:20:36 -07:00
}
2008-06-03 14:47:52 +03:00
kfree ( irda_desc ) ;
2005-04-16 15:20:36 -07:00
2008-06-03 14:47:52 +03:00
return 0 ;
2005-04-16 15:20:36 -07:00
}
2020-01-22 11:15:28 +01:00
static int ir_write ( struct tty_struct * tty , struct usb_serial_port * port ,
const unsigned char * buf , int count )
2005-04-16 15:20:36 -07:00
{
2020-01-22 11:15:28 +01:00
struct urb * urb = NULL ;
unsigned long flags ;
int ret ;
2005-04-16 15:20:36 -07:00
2020-01-22 11:15:28 +01:00
if ( port - > bulk_out_size = = 0 )
return - EINVAL ;
2008-06-03 14:47:52 +03:00
2020-01-22 11:15:28 +01:00
if ( count = = 0 )
return 0 ;
2005-04-16 15:20:36 -07:00
2020-01-22 11:15:28 +01:00
count = min ( count , port - > bulk_out_size - 1 ) ;
spin_lock_irqsave ( & port - > lock , flags ) ;
if ( __test_and_clear_bit ( 0 , & port - > write_urbs_free ) ) {
urb = port - > write_urbs [ 0 ] ;
port - > tx_bytes + = count ;
}
spin_unlock_irqrestore ( & port - > lock , flags ) ;
if ( ! urb )
return 0 ;
2005-04-16 15:20:36 -07:00
/*
* The first byte of the packet we send to the device contains an
2020-01-22 11:15:28 +01:00
* outbound header which indicates an additional number of BOFs and
2005-04-16 15:20:36 -07:00
* a baud rate change .
*
* See section 5.4 .2 .2 of the USB IrDA spec .
*/
2020-01-22 11:15:28 +01:00
* ( u8 * ) urb - > transfer_buffer = ir_xbof | ir_baud ;
memcpy ( urb - > transfer_buffer + 1 , buf , count ) ;
urb - > transfer_buffer_length = count + 1 ;
urb - > transfer_flags = URB_ZERO_PACKET ;
ret = usb_submit_urb ( urb , GFP_ATOMIC ) ;
if ( ret ) {
dev_err ( & port - > dev , " failed to submit write urb: %d \n " , ret ) ;
spin_lock_irqsave ( & port - > lock , flags ) ;
__set_bit ( 0 , & port - > write_urbs_free ) ;
port - > tx_bytes - = count ;
spin_unlock_irqrestore ( & port - > lock , flags ) ;
return ret ;
}
return count ;
}
static void ir_write_bulk_callback ( struct urb * urb )
{
struct usb_serial_port * port = urb - > context ;
int status = urb - > status ;
unsigned long flags ;
spin_lock_irqsave ( & port - > lock , flags ) ;
__set_bit ( 0 , & port - > write_urbs_free ) ;
port - > tx_bytes - = urb - > transfer_buffer_length - 1 ;
spin_unlock_irqrestore ( & port - > lock , flags ) ;
switch ( status ) {
case 0 :
break ;
case - ENOENT :
case - ECONNRESET :
case - ESHUTDOWN :
dev_dbg ( & port - > dev , " write urb stopped: %d \n " , status ) ;
return ;
case - EPIPE :
dev_err ( & port - > dev , " write urb stopped: %d \n " , status ) ;
return ;
default :
dev_err ( & port - > dev , " nonzero write-urb status: %d \n " , status ) ;
break ;
}
usb_serial_port_softint ( port ) ;
}
static int ir_write_room ( struct tty_struct * tty )
{
struct usb_serial_port * port = tty - > driver_data ;
int count = 0 ;
if ( port - > bulk_out_size = = 0 )
return 0 ;
if ( test_bit ( 0 , & port - > write_urbs_free ) )
count = port - > bulk_out_size - 1 ;
2005-04-16 15:20:36 -07:00
2020-01-22 11:15:28 +01:00
return count ;
2005-04-16 15:20:36 -07:00
}
2010-05-13 21:02:03 +02:00
static void ir_process_read_urb ( struct urb * urb )
2005-04-16 15:20:36 -07:00
{
2008-02-24 18:41:47 +08:00
struct usb_serial_port * port = urb - > context ;
2005-04-16 15:20:36 -07:00
unsigned char * data = urb - > transfer_buffer ;
2010-05-13 21:02:03 +02:00
if ( ! urb - > actual_length )
return ;
/*
* The first byte of the packet we get from the device
* contains a busy indicator and baud rate change .
* See section 5.4 .1 .2 of the USB IrDA spec .
*/
if ( * data & 0x0f )
ir_baud = * data & 0x0f ;
2005-04-16 15:20:36 -07:00
2010-05-13 21:02:03 +02:00
if ( urb - > actual_length = = 1 )
return ;
2013-01-03 15:53:04 +01:00
tty_insert_flip_string ( & port - > port , data + 1 , urb - > actual_length - 1 ) ;
2013-01-03 15:53:06 +01:00
tty_flip_buffer_push ( & port - > port ) ;
2005-04-16 15:20:36 -07:00
}
2008-07-22 11:09:07 +01:00
static void ir_set_termios ( struct tty_struct * tty ,
struct usb_serial_port * port , struct ktermios * old_termios )
2005-04-16 15:20:36 -07:00
{
2020-01-22 11:15:29 +01:00
struct usb_device * udev = port - > serial - > dev ;
2005-04-16 15:20:36 -07:00
unsigned char * transfer_buffer ;
2020-01-22 11:15:29 +01:00
int actual_length ;
2007-06-22 14:44:54 +01:00
speed_t baud ;
int ir_baud ;
2020-01-22 11:15:29 +01:00
int ret ;
2005-04-16 15:20:36 -07:00
2008-07-22 11:09:07 +01:00
baud = tty_get_baud_rate ( tty ) ;
2007-06-22 14:44:54 +01:00
/*
* FIXME , we should compare the baud request against the
* capability stated in the IR header that we got in the
* startup function .
*/
switch ( baud ) {
2008-06-03 14:47:52 +03:00
case 2400 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_2400 ;
2008-06-03 14:47:52 +03:00
break ;
case 9600 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_9600 ;
2008-06-03 14:47:52 +03:00
break ;
case 19200 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_19200 ;
2008-06-03 14:47:52 +03:00
break ;
case 38400 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_38400 ;
2008-06-03 14:47:52 +03:00
break ;
case 57600 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_57600 ;
2008-06-03 14:47:52 +03:00
break ;
case 115200 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_115200 ;
2008-06-03 14:47:52 +03:00
break ;
case 576000 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_576000 ;
2008-06-03 14:47:52 +03:00
break ;
case 1152000 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_1152000 ;
2008-06-03 14:47:52 +03:00
break ;
case 4000000 :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_4000000 ;
2008-06-03 14:47:52 +03:00
break ;
default :
2020-01-22 11:15:27 +01:00
ir_baud = USB_IRDA_LS_9600 ;
2008-06-03 14:47:52 +03:00
baud = 9600 ;
2005-04-16 15:20:36 -07:00
}
2007-06-22 14:44:54 +01:00
if ( xbof = = - 1 )
ir_xbof = ir_xbof_change ( ir_add_bof ) ;
else
ir_xbof = ir_xbof_change ( xbof ) ;
2005-04-16 15:20:36 -07:00
2010-05-13 21:02:02 +02:00
/* Only speed changes are supported */
2012-07-14 15:31:47 +01:00
tty_termios_copy_hw ( & tty - > termios , old_termios ) ;
2010-05-13 21:02:02 +02:00
tty_encode_baud_rate ( tty , baud , baud ) ;
/*
2007-06-22 14:44:54 +01:00
* send the baud change out on an " empty " data packet
*/
2010-05-13 21:02:02 +02:00
transfer_buffer = kmalloc ( 1 , GFP_KERNEL ) ;
2013-12-29 19:22:56 +01:00
if ( ! transfer_buffer )
2020-01-22 11:15:29 +01:00
return ;
2010-05-13 21:02:02 +02:00
2007-06-22 14:44:54 +01:00
* transfer_buffer = ir_xbof | ir_baud ;
2005-04-16 15:20:36 -07:00
2020-01-22 11:15:29 +01:00
ret = usb_bulk_msg ( udev ,
usb_sndbulkpipe ( udev , port - > bulk_out_endpointAddress ) ,
transfer_buffer , 1 , & actual_length , 5000 ) ;
if ( ret | | actual_length ! = 1 ) {
2020-01-29 20:30:37 +03:00
if ( ! ret )
2020-01-22 11:15:29 +01:00
ret = - EIO ;
dev_err ( & port - > dev , " failed to change line speed: %d \n " , ret ) ;
2010-05-13 21:02:02 +02:00
}
2007-10-18 01:24:20 -07:00
2010-05-13 21:02:02 +02:00
kfree ( transfer_buffer ) ;
2005-04-16 15:20:36 -07:00
}
2008-06-03 14:47:52 +03:00
static int __init ir_init ( void )
2005-04-16 15:20:36 -07:00
{
2010-05-13 21:02:01 +02:00
if ( buffer_size ) {
ir_device . bulk_in_size = buffer_size ;
ir_device . bulk_out_size = buffer_size ;
}
2012-09-18 17:00:48 +01:00
return usb_serial_register_drivers ( serial_drivers , KBUILD_MODNAME , ir_id_table ) ;
2005-04-16 15:20:36 -07:00
}
2008-06-03 14:47:52 +03:00
static void __exit ir_exit ( void )
2005-04-16 15:20:36 -07:00
{
2012-05-08 15:46:14 -07:00
usb_serial_deregister_drivers ( serial_drivers ) ;
2005-04-16 15:20:36 -07:00
}
module_init ( ir_init ) ;
module_exit ( ir_exit ) ;
MODULE_AUTHOR ( DRIVER_AUTHOR ) ;
MODULE_DESCRIPTION ( DRIVER_DESC ) ;
MODULE_LICENSE ( " GPL " ) ;
module_param ( xbof , int , 0 ) ;
MODULE_PARM_DESC ( xbof , " Force specific number of XBOFs " ) ;
module_param ( buffer_size , int , 0 ) ;
MODULE_PARM_DESC ( buffer_size , " Size of the transfer buffers " ) ;