Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * USB IR Dongle driver
0004  *
0005  *  Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
0006  *  Copyright (C) 2002  Gary Brubaker (xavyer@ix.netcom.com)
0007  *  Copyright (C) 2010  Johan Hovold (jhovold@gmail.com)
0008  *
0009  * This driver allows a USB IrDA device to be used as a "dumb" serial device.
0010  * This can be useful if you do not have access to a full IrDA stack on the
0011  * other side of the connection.  If you do have an IrDA stack on both devices,
0012  * please use the usb-irda driver, as it contains the proper error checking and
0013  * other goodness of a full IrDA stack.
0014  *
0015  * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which
0016  * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli
0017  * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com>
0018  *
0019  * See Documentation/usb/usb-serial.rst for more information on using this
0020  * driver
0021  */
0022 
0023 #include <linux/kernel.h>
0024 #include <linux/errno.h>
0025 #include <linux/init.h>
0026 #include <linux/slab.h>
0027 #include <linux/tty.h>
0028 #include <linux/tty_driver.h>
0029 #include <linux/tty_flip.h>
0030 #include <linux/module.h>
0031 #include <linux/spinlock.h>
0032 #include <linux/uaccess.h>
0033 #include <linux/usb.h>
0034 #include <linux/usb/serial.h>
0035 #include <linux/usb/irda.h>
0036 
0037 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Johan Hovold <jhovold@gmail.com>"
0038 #define DRIVER_DESC "USB IR Dongle driver"
0039 
0040 /* if overridden by the user, then use their value for the size of the read and
0041  * write urbs */
0042 static int buffer_size;
0043 
0044 /* if overridden by the user, then use the specified number of XBOFs */
0045 static int xbof = -1;
0046 
0047 static int  ir_startup (struct usb_serial *serial);
0048 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
0049         const unsigned char *buf, int count);
0050 static unsigned int ir_write_room(struct tty_struct *tty);
0051 static void ir_write_bulk_callback(struct urb *urb);
0052 static void ir_process_read_urb(struct urb *urb);
0053 static void ir_set_termios(struct tty_struct *tty,
0054         struct usb_serial_port *port, struct ktermios *old_termios);
0055 
0056 /* Not that this lot means you can only have one per system */
0057 static u8 ir_baud;
0058 static u8 ir_xbof;
0059 static u8 ir_add_bof;
0060 
0061 static const struct usb_device_id ir_id_table[] = {
0062     { USB_DEVICE(0x050f, 0x0180) },     /* KC Technology, KC-180 */
0063     { USB_DEVICE(0x08e9, 0x0100) },     /* XTNDAccess */
0064     { USB_DEVICE(0x09c4, 0x0011) },     /* ACTiSys ACT-IR2000U */
0065     { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
0066     { }                 /* Terminating entry */
0067 };
0068 
0069 MODULE_DEVICE_TABLE(usb, ir_id_table);
0070 
0071 static struct usb_serial_driver ir_device = {
0072     .driver = {
0073         .owner  = THIS_MODULE,
0074         .name   = "ir-usb",
0075     },
0076     .description        = "IR Dongle",
0077     .id_table       = ir_id_table,
0078     .num_ports      = 1,
0079     .num_bulk_in        = 1,
0080     .num_bulk_out       = 1,
0081     .set_termios        = ir_set_termios,
0082     .attach         = ir_startup,
0083     .write          = ir_write,
0084     .write_room     = ir_write_room,
0085     .write_bulk_callback    = ir_write_bulk_callback,
0086     .process_read_urb   = ir_process_read_urb,
0087 };
0088 
0089 static struct usb_serial_driver * const serial_drivers[] = {
0090     &ir_device, NULL
0091 };
0092 
0093 static inline void irda_usb_dump_class_desc(struct usb_serial *serial,
0094                         struct usb_irda_cs_descriptor *desc)
0095 {
0096     struct device *dev = &serial->dev->dev;
0097 
0098     dev_dbg(dev, "bLength=%x\n", desc->bLength);
0099     dev_dbg(dev, "bDescriptorType=%x\n", desc->bDescriptorType);
0100     dev_dbg(dev, "bcdSpecRevision=%x\n", __le16_to_cpu(desc->bcdSpecRevision));
0101     dev_dbg(dev, "bmDataSize=%x\n", desc->bmDataSize);
0102     dev_dbg(dev, "bmWindowSize=%x\n", desc->bmWindowSize);
0103     dev_dbg(dev, "bmMinTurnaroundTime=%d\n", desc->bmMinTurnaroundTime);
0104     dev_dbg(dev, "wBaudRate=%x\n", __le16_to_cpu(desc->wBaudRate));
0105     dev_dbg(dev, "bmAdditionalBOFs=%x\n", desc->bmAdditionalBOFs);
0106     dev_dbg(dev, "bIrdaRateSniff=%x\n", desc->bIrdaRateSniff);
0107     dev_dbg(dev, "bMaxUnicastList=%x\n", desc->bMaxUnicastList);
0108 }
0109 
0110 /*------------------------------------------------------------------*/
0111 /*
0112  * Function irda_usb_find_class_desc(dev, ifnum)
0113  *
0114  *    Returns instance of IrDA class descriptor, or NULL if not found
0115  *
0116  * The class descriptor is some extra info that IrDA USB devices will
0117  * offer to us, describing their IrDA characteristics. We will use that in
0118  * irda_usb_init_qos()
0119  *
0120  * Based on the same function in drivers/net/irda/irda-usb.c
0121  */
0122 static struct usb_irda_cs_descriptor *
0123 irda_usb_find_class_desc(struct usb_serial *serial, unsigned int ifnum)
0124 {
0125     struct usb_device *dev = serial->dev;
0126     struct usb_irda_cs_descriptor *desc;
0127     int ret;
0128 
0129     desc = kzalloc(sizeof(*desc), GFP_KERNEL);
0130     if (!desc)
0131         return NULL;
0132 
0133     ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
0134             USB_REQ_CS_IRDA_GET_CLASS_DESC,
0135             USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
0136             0, ifnum, desc, sizeof(*desc), 1000);
0137 
0138     dev_dbg(&serial->dev->dev, "%s -  ret=%d\n", __func__, ret);
0139     if (ret < (int)sizeof(*desc)) {
0140         dev_dbg(&serial->dev->dev,
0141             "%s - class descriptor read %s (%d)\n", __func__,
0142             (ret < 0) ? "failed" : "too short", ret);
0143         goto error;
0144     }
0145     if (desc->bDescriptorType != USB_DT_CS_IRDA) {
0146         dev_dbg(&serial->dev->dev, "%s - bad class descriptor type\n",
0147             __func__);
0148         goto error;
0149     }
0150 
0151     irda_usb_dump_class_desc(serial, desc);
0152     return desc;
0153 
0154 error:
0155     kfree(desc);
0156     return NULL;
0157 }
0158 
0159 static u8 ir_xbof_change(u8 xbof)
0160 {
0161     u8 result;
0162 
0163     /* reference irda-usb.c */
0164     switch (xbof) {
0165     case 48:
0166         result = 0x10;
0167         break;
0168     case 28:
0169     case 24:
0170         result = 0x20;
0171         break;
0172     default:
0173     case 12:
0174         result = 0x30;
0175         break;
0176     case  5:
0177     case  6:
0178         result = 0x40;
0179         break;
0180     case  3:
0181         result = 0x50;
0182         break;
0183     case  2:
0184         result = 0x60;
0185         break;
0186     case  1:
0187         result = 0x70;
0188         break;
0189     case  0:
0190         result = 0x80;
0191         break;
0192     }
0193 
0194     return(result);
0195 }
0196 
0197 static int ir_startup(struct usb_serial *serial)
0198 {
0199     struct usb_irda_cs_descriptor *irda_desc;
0200     int rates;
0201 
0202     irda_desc = irda_usb_find_class_desc(serial, 0);
0203     if (!irda_desc) {
0204         dev_err(&serial->dev->dev,
0205             "IRDA class descriptor not found, device not bound\n");
0206         return -ENODEV;
0207     }
0208 
0209     rates = le16_to_cpu(irda_desc->wBaudRate);
0210 
0211     dev_dbg(&serial->dev->dev,
0212         "%s - Baud rates supported:%s%s%s%s%s%s%s%s%s\n",
0213         __func__,
0214         (rates & USB_IRDA_BR_2400) ? " 2400" : "",
0215         (rates & USB_IRDA_BR_9600) ? " 9600" : "",
0216         (rates & USB_IRDA_BR_19200) ? " 19200" : "",
0217         (rates & USB_IRDA_BR_38400) ? " 38400" : "",
0218         (rates & USB_IRDA_BR_57600) ? " 57600" : "",
0219         (rates & USB_IRDA_BR_115200) ? " 115200" : "",
0220         (rates & USB_IRDA_BR_576000) ? " 576000" : "",
0221         (rates & USB_IRDA_BR_1152000) ? " 1152000" : "",
0222         (rates & USB_IRDA_BR_4000000) ? " 4000000" : "");
0223 
0224     switch (irda_desc->bmAdditionalBOFs) {
0225     case USB_IRDA_AB_48:
0226         ir_add_bof = 48;
0227         break;
0228     case USB_IRDA_AB_24:
0229         ir_add_bof = 24;
0230         break;
0231     case USB_IRDA_AB_12:
0232         ir_add_bof = 12;
0233         break;
0234     case USB_IRDA_AB_6:
0235         ir_add_bof = 6;
0236         break;
0237     case USB_IRDA_AB_3:
0238         ir_add_bof = 3;
0239         break;
0240     case USB_IRDA_AB_2:
0241         ir_add_bof = 2;
0242         break;
0243     case USB_IRDA_AB_1:
0244         ir_add_bof = 1;
0245         break;
0246     case USB_IRDA_AB_0:
0247         ir_add_bof = 0;
0248         break;
0249     default:
0250         break;
0251     }
0252 
0253     kfree(irda_desc);
0254 
0255     return 0;
0256 }
0257 
0258 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
0259         const unsigned char *buf, int count)
0260 {
0261     struct urb *urb = NULL;
0262     unsigned long flags;
0263     int ret;
0264 
0265     if (port->bulk_out_size == 0)
0266         return -EINVAL;
0267 
0268     if (count == 0)
0269         return 0;
0270 
0271     count = min(count, port->bulk_out_size - 1);
0272 
0273     spin_lock_irqsave(&port->lock, flags);
0274     if (__test_and_clear_bit(0, &port->write_urbs_free)) {
0275         urb = port->write_urbs[0];
0276         port->tx_bytes += count;
0277     }
0278     spin_unlock_irqrestore(&port->lock, flags);
0279 
0280     if (!urb)
0281         return 0;
0282 
0283     /*
0284      * The first byte of the packet we send to the device contains an
0285      * outbound header which indicates an additional number of BOFs and
0286      * a baud rate change.
0287      *
0288      * See section 5.4.2.2 of the USB IrDA spec.
0289      */
0290     *(u8 *)urb->transfer_buffer = ir_xbof | ir_baud;
0291 
0292     memcpy(urb->transfer_buffer + 1, buf, count);
0293 
0294     urb->transfer_buffer_length = count + 1;
0295     urb->transfer_flags = URB_ZERO_PACKET;
0296 
0297     ret = usb_submit_urb(urb, GFP_ATOMIC);
0298     if (ret) {
0299         dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
0300 
0301         spin_lock_irqsave(&port->lock, flags);
0302         __set_bit(0, &port->write_urbs_free);
0303         port->tx_bytes -= count;
0304         spin_unlock_irqrestore(&port->lock, flags);
0305 
0306         return ret;
0307     }
0308 
0309     return count;
0310 }
0311 
0312 static void ir_write_bulk_callback(struct urb *urb)
0313 {
0314     struct usb_serial_port *port = urb->context;
0315     int status = urb->status;
0316     unsigned long flags;
0317 
0318     spin_lock_irqsave(&port->lock, flags);
0319     __set_bit(0, &port->write_urbs_free);
0320     port->tx_bytes -= urb->transfer_buffer_length - 1;
0321     spin_unlock_irqrestore(&port->lock, flags);
0322 
0323     switch (status) {
0324     case 0:
0325         break;
0326     case -ENOENT:
0327     case -ECONNRESET:
0328     case -ESHUTDOWN:
0329         dev_dbg(&port->dev, "write urb stopped: %d\n", status);
0330         return;
0331     case -EPIPE:
0332         dev_err(&port->dev, "write urb stopped: %d\n", status);
0333         return;
0334     default:
0335         dev_err(&port->dev, "nonzero write-urb status: %d\n", status);
0336         break;
0337     }
0338 
0339     usb_serial_port_softint(port);
0340 }
0341 
0342 static unsigned int ir_write_room(struct tty_struct *tty)
0343 {
0344     struct usb_serial_port *port = tty->driver_data;
0345     unsigned int count = 0;
0346 
0347     if (port->bulk_out_size == 0)
0348         return 0;
0349 
0350     if (test_bit(0, &port->write_urbs_free))
0351         count = port->bulk_out_size - 1;
0352 
0353     return count;
0354 }
0355 
0356 static void ir_process_read_urb(struct urb *urb)
0357 {
0358     struct usb_serial_port *port = urb->context;
0359     unsigned char *data = urb->transfer_buffer;
0360 
0361     if (!urb->actual_length)
0362         return;
0363     /*
0364      * The first byte of the packet we get from the device
0365      * contains a busy indicator and baud rate change.
0366      * See section 5.4.1.2 of the USB IrDA spec.
0367      */
0368     if (*data & 0x0f)
0369         ir_baud = *data & 0x0f;
0370 
0371     if (urb->actual_length == 1)
0372         return;
0373 
0374     tty_insert_flip_string(&port->port, data + 1, urb->actual_length - 1);
0375     tty_flip_buffer_push(&port->port);
0376 }
0377 
0378 static void ir_set_termios(struct tty_struct *tty,
0379         struct usb_serial_port *port, struct ktermios *old_termios)
0380 {
0381     struct usb_device *udev = port->serial->dev;
0382     unsigned char *transfer_buffer;
0383     int actual_length;
0384     speed_t baud;
0385     int ir_baud;
0386     int ret;
0387 
0388     baud = tty_get_baud_rate(tty);
0389 
0390     /*
0391      * FIXME, we should compare the baud request against the
0392      * capability stated in the IR header that we got in the
0393      * startup function.
0394      */
0395 
0396     switch (baud) {
0397     case 2400:
0398         ir_baud = USB_IRDA_LS_2400;
0399         break;
0400     case 9600:
0401         ir_baud = USB_IRDA_LS_9600;
0402         break;
0403     case 19200:
0404         ir_baud = USB_IRDA_LS_19200;
0405         break;
0406     case 38400:
0407         ir_baud = USB_IRDA_LS_38400;
0408         break;
0409     case 57600:
0410         ir_baud = USB_IRDA_LS_57600;
0411         break;
0412     case 115200:
0413         ir_baud = USB_IRDA_LS_115200;
0414         break;
0415     case 576000:
0416         ir_baud = USB_IRDA_LS_576000;
0417         break;
0418     case 1152000:
0419         ir_baud = USB_IRDA_LS_1152000;
0420         break;
0421     case 4000000:
0422         ir_baud = USB_IRDA_LS_4000000;
0423         break;
0424     default:
0425         ir_baud = USB_IRDA_LS_9600;
0426         baud = 9600;
0427     }
0428 
0429     if (xbof == -1)
0430         ir_xbof = ir_xbof_change(ir_add_bof);
0431     else
0432         ir_xbof = ir_xbof_change(xbof) ;
0433 
0434     /* Only speed changes are supported */
0435     tty_termios_copy_hw(&tty->termios, old_termios);
0436     tty_encode_baud_rate(tty, baud, baud);
0437 
0438     /*
0439      * send the baud change out on an "empty" data packet
0440      */
0441     transfer_buffer = kmalloc(1, GFP_KERNEL);
0442     if (!transfer_buffer)
0443         return;
0444 
0445     *transfer_buffer = ir_xbof | ir_baud;
0446 
0447     ret = usb_bulk_msg(udev,
0448             usb_sndbulkpipe(udev, port->bulk_out_endpointAddress),
0449             transfer_buffer, 1, &actual_length, 5000);
0450     if (ret || actual_length != 1) {
0451         if (!ret)
0452             ret = -EIO;
0453         dev_err(&port->dev, "failed to change line speed: %d\n", ret);
0454     }
0455 
0456     kfree(transfer_buffer);
0457 }
0458 
0459 static int __init ir_init(void)
0460 {
0461     if (buffer_size) {
0462         ir_device.bulk_in_size = buffer_size;
0463         ir_device.bulk_out_size = buffer_size;
0464     }
0465 
0466     return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ir_id_table);
0467 }
0468 
0469 static void __exit ir_exit(void)
0470 {
0471     usb_serial_deregister_drivers(serial_drivers);
0472 }
0473 
0474 
0475 module_init(ir_init);
0476 module_exit(ir_exit);
0477 
0478 MODULE_AUTHOR(DRIVER_AUTHOR);
0479 MODULE_DESCRIPTION(DRIVER_DESC);
0480 MODULE_LICENSE("GPL");
0481 
0482 module_param(xbof, int, 0);
0483 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
0484 module_param(buffer_size, int, 0);
0485 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
0486