// SPDX-License-Identifier: GPL-2.0+
/*
* Safe Encapsulated USB Serial Driver
*
* Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
* Copyright (C) 2001 Lineo
* Copyright (C) 2001 Hewlett-Packard
*
* By:
* Stuart Lynne <sl@lineo.com>, Tom Rushworth <tbr@lineo.com>
*/
/*
* The encapsultaion is designed to overcome difficulties with some USB
* hardware.
*
* While the USB protocol has a CRC over the data while in transit, i.e. while
* being carried over the bus, there is no end to end protection. If the
* hardware has any problems getting the data into or out of the USB transmit
* and receive FIFO's then data can be lost.
*
* This protocol adds a two byte trailer to each USB packet to specify the
* number of bytes of valid data and a 10 bit CRC that will allow the receiver
* to verify that the entire USB packet was received without error.
*
* Because in this case the sender and receiver are the class and function
* drivers there is now end to end protection.
*
* There is an additional option that can be used to force all transmitted
* packets to be padded to the maximum packet size. This provides a work
* around for some devices which have problems with small USB packets.
*
* Assuming a packetsize of N:
*
* 0..N-2 data and optional padding
*
* N-2 bits 7-2 - number of bytes of valid data
* bits 1-0 top two bits of 10 bit CRC
* N-1 bottom 8 bits of 10 bit CRC
*
*
* | Data Length | 10 bit CRC |
* + 7 . 6 . 5 . 4 . 3 . 2 . 1 . 0 | 7 . 6 . 5 . 4 . 3 . 2 . 1 . 0 +
*
* The 10 bit CRC is computed across the sent data, followed by the trailer
* with the length set and the CRC set to zero. The CRC is then OR'd into
* the trailer.
*
* When received a 10 bit CRC is computed over the entire frame including
* the trailer and should be equal to zero.
*
* Two module parameters are used to control the encapsulation, if both are
* turned of the module works as a simple serial device with NO
* encapsulation.
*
* See linux/drivers/usbd/serial_fd for a device function driver
* implementation of this.
*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/gfp.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
static bool safe = true ;
static bool padded = IS_ENABLED(CONFIG_USB_SERIAL_SAFE_PADDED);
#define DRIVER_AUTHOR "sl@lineo.com, tbr@lineo.com, Johan Hovold <jhovold@gmail.com>"
#define DRIVER_DESC "USB Safe Encapsulated Serial"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL" );
module_param(safe, bool , 0 );
MODULE_PARM_DESC(safe, "Turn Safe Encapsulation On/Off" );
module_param(padded, bool , 0 );
MODULE_PARM_DESC(padded, "Pad to full wMaxPacketSize On/Off" );
#define CDC_DEVICE_CLASS 0 x02
#define CDC_INTERFACE_CLASS 0 x02
#define CDC_INTERFACE_SUBCLASS 0 x06
#define LINEO_INTERFACE_CLASS 0 xff
#define LINEO_INTERFACE_SUBCLASS_SAFENET 0 x01
#define LINEO_SAFENET_CRC 0 x01
#define LINEO_SAFENET_CRC_PADDED 0 x02
#define LINEO_INTERFACE_SUBCLASS_SAFESERIAL 0 x02
#define LINEO_SAFESERIAL_CRC 0 x01
#define LINEO_SAFESERIAL_CRC_PADDED 0 x02
#define MY_USB_DEVICE(vend, prod, dc, ic, isc) \
.match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
USB_DEVICE_ID_MATCH_DEV_CLASS | \
USB_DEVICE_ID_MATCH_INT_CLASS | \
USB_DEVICE_ID_MATCH_INT_SUBCLASS, \
.idVendor = (vend), \
.idProduct = (prod),\
.bDeviceClass = (dc),\
.bInterfaceClass = (ic), \
.bInterfaceSubClass = (isc),
static const struct usb_device_id id_table[] = {
{MY_USB_DEVICE(0 x49f, 0 xffff, CDC_DEVICE_CLASS, LINEO_INTERFACE_CLASS, LINEO_INTERFACE_SUBCLASS_SAFESERIAL)}, /* Itsy */
{MY_USB_DEVICE(0 x3f0, 0 x2101, CDC_DEVICE_CLASS, LINEO_INTERFACE_CLASS, LINEO_INTERFACE_SUBCLASS_SAFESERIAL)}, /* Calypso */
{MY_USB_DEVICE(0 x4dd, 0 x8001, CDC_DEVICE_CLASS, LINEO_INTERFACE_CLASS, LINEO_INTERFACE_SUBCLASS_SAFESERIAL)}, /* Iris */
{MY_USB_DEVICE(0 x4dd, 0 x8002, CDC_DEVICE_CLASS, LINEO_INTERFACE_CLASS, LINEO_INTERFACE_SUBCLASS_SAFESERIAL)}, /* Collie */
{MY_USB_DEVICE(0 x4dd, 0 x8003, CDC_DEVICE_CLASS, LINEO_INTERFACE_CLASS, LINEO_INTERFACE_SUBCLASS_SAFESERIAL)}, /* Collie */
{MY_USB_DEVICE(0 x4dd, 0 x8004, CDC_DEVICE_CLASS, LINEO_INTERFACE_CLASS, LINEO_INTERFACE_SUBCLASS_SAFESERIAL)}, /* Collie */
{MY_USB_DEVICE(0 x5f9, 0 xffff, CDC_DEVICE_CLASS, LINEO_INTERFACE_CLASS, LINEO_INTERFACE_SUBCLASS_SAFESERIAL)}, /* Sharp tmp */
{} /* terminating entry */
};
MODULE_DEVICE_TABLE(usb, id_table);
static const __u16 crc10_table[256 ] = {
0 x000, 0 x233, 0 x255, 0 x066, 0 x299, 0 x0aa, 0 x0cc, 0 x2ff,
0 x301, 0 x132, 0 x154, 0 x367, 0 x198, 0 x3ab, 0 x3cd, 0 x1fe,
0 x031, 0 x202, 0 x264, 0 x057, 0 x2a8, 0 x09b, 0 x0fd, 0 x2ce,
0 x330, 0 x103, 0 x165, 0 x356, 0 x1a9, 0 x39a, 0 x3fc, 0 x1cf,
0 x062, 0 x251, 0 x237, 0 x004, 0 x2fb, 0 x0c8, 0 x0ae, 0 x29d,
0 x363, 0 x150, 0 x136, 0 x305, 0 x1fa, 0 x3c9, 0 x3af, 0 x19c,
0 x053, 0 x260, 0 x206, 0 x035, 0 x2ca, 0 x0f9, 0 x09f, 0 x2ac,
0 x352, 0 x161, 0 x107, 0 x334, 0 x1cb, 0 x3f8, 0 x39e, 0 x1ad,
0 x0c4, 0 x2f7, 0 x291, 0 x0a2, 0 x25d, 0 x06e, 0 x008, 0 x23b,
0 x3c5, 0 x1f6, 0 x190, 0 x3a3, 0 x15c, 0 x36f, 0 x309, 0 x13a,
0 x0f5, 0 x2c6, 0 x2a0, 0 x093, 0 x26c, 0 x05f, 0 x039, 0 x20a,
0 x3f4, 0 x1c7, 0 x1a1, 0 x392, 0 x16d, 0 x35e, 0 x338, 0 x10b,
0 x0a6, 0 x295, 0 x2f3, 0 x0c0, 0 x23f, 0 x00c, 0 x06a, 0 x259,
0 x3a7, 0 x194, 0 x1f2, 0 x3c1, 0 x13e, 0 x30d, 0 x36b, 0 x158,
0 x097, 0 x2a4, 0 x2c2, 0 x0f1, 0 x20e, 0 x03d, 0 x05b, 0 x268,
0 x396, 0 x1a5, 0 x1c3, 0 x3f0, 0 x10f, 0 x33c, 0 x35a, 0 x169,
0 x188, 0 x3bb, 0 x3dd, 0 x1ee, 0 x311, 0 x122, 0 x144, 0 x377,
0 x289, 0 x0ba, 0 x0dc, 0 x2ef, 0 x010, 0 x223, 0 x245, 0 x076,
0 x1b9, 0 x38a, 0 x3ec, 0 x1df, 0 x320, 0 x113, 0 x175, 0 x346,
0 x2b8, 0 x08b, 0 x0ed, 0 x2de, 0 x021, 0 x212, 0 x274, 0 x047,
0 x1ea, 0 x3d9, 0 x3bf, 0 x18c, 0 x373, 0 x140, 0 x126, 0 x315,
0 x2eb, 0 x0d8, 0 x0be, 0 x28d, 0 x072, 0 x241, 0 x227, 0 x014,
0 x1db, 0 x3e8, 0 x38e, 0 x1bd, 0 x342, 0 x171, 0 x117, 0 x324,
0 x2da, 0 x0e9, 0 x08f, 0 x2bc, 0 x043, 0 x270, 0 x216, 0 x025,
0 x14c, 0 x37f, 0 x319, 0 x12a, 0 x3d5, 0 x1e6, 0 x180, 0 x3b3,
0 x24d, 0 x07e, 0 x018, 0 x22b, 0 x0d4, 0 x2e7, 0 x281, 0 x0b2,
0 x17d, 0 x34e, 0 x328, 0 x11b, 0 x3e4, 0 x1d7, 0 x1b1, 0 x382,
0 x27c, 0 x04f, 0 x029, 0 x21a, 0 x0e5, 0 x2d6, 0 x2b0, 0 x083,
0 x12e, 0 x31d, 0 x37b, 0 x148, 0 x3b7, 0 x184, 0 x1e2, 0 x3d1,
0 x22f, 0 x01c, 0 x07a, 0 x249, 0 x0b6, 0 x285, 0 x2e3, 0 x0d0,
0 x11f, 0 x32c, 0 x34a, 0 x179, 0 x386, 0 x1b5, 0 x1d3, 0 x3e0,
0 x21e, 0 x02d, 0 x04b, 0 x278, 0 x087, 0 x2b4, 0 x2d2, 0 x0e1,
};
#define CRC10_INITFCS 0 x000 /* Initial FCS value */
#define CRC10_GOODFCS 0 x000 /* Good final FCS value */
#define CRC10_FCS(fcs, c) ((((fcs) << 8 ) & 0 x3ff) ^ crc10_table[((fcs) >> 2 ) & 0 xff] ^ (c))
/**
* fcs_compute10 - memcpy and calculate 10 bit CRC across buffer
* @sp: pointer to buffer
* @len: number of bytes
* @fcs: starting FCS
*
* Perform a memcpy and calculate fcs using ppp 10bit CRC algorithm. Return
* new 10 bit FCS.
*/
static inline __u16 fcs_compute10(unsigned char *sp, int len, __u16 fcs)
{
for (; len-- > 0 ; fcs = CRC10_FCS(fcs, *sp++));
return fcs;
}
static void safe_process_read_urb(struct urb *urb)
{
struct usb_serial_port *port = urb->context;
unsigned char *data = urb->transfer_buffer;
unsigned char length = urb->actual_length;
int actual_length;
__u16 fcs;
if (!length)
return ;
if (!safe)
goto out;
if (length < 2 ) {
dev_err(&port->dev, "malformed packet\n" );
return ;
}
fcs = fcs_compute10(data, length, CRC10_INITFCS);
if (fcs) {
dev_err(&port->dev, "%s - bad CRC %x\n" , __func__, fcs);
return ;
}
actual_length = data[length - 2 ] >> 2 ;
if (actual_length > (length - 2 )) {
dev_err(&port->dev, "%s - inconsistent lengths %d:%d\n" ,
__func__, actual_length, length);
return ;
}
dev_info(&urb->dev->dev, "%s - actual: %d\n" , __func__, actual_length);
length = actual_length;
out:
tty_insert_flip_string(&port->port, data, length);
tty_flip_buffer_push(&port->port);
}
static int safe_prepare_write_buffer(struct usb_serial_port *port,
void *dest, size_t size)
{
unsigned char *buf = dest;
int count;
int trailer_len;
int pkt_len;
__u16 fcs;
trailer_len = safe ? 2 : 0 ;
count = kfifo_out_locked(&port->write_fifo, buf, size - trailer_len,
&port->lock);
if (!safe)
return count;
/* pad if necessary */
if (padded) {
pkt_len = size;
memset(buf + count, '0' , pkt_len - count - trailer_len);
} else {
pkt_len = count + trailer_len;
}
/* set count */
buf[pkt_len - 2 ] = count << 2 ;
buf[pkt_len - 1 ] = 0 ;
/* compute fcs and insert into trailer */
fcs = fcs_compute10(buf, pkt_len, CRC10_INITFCS);
buf[pkt_len - 2 ] |= fcs >> 8 ;
buf[pkt_len - 1 ] |= fcs & 0 xff;
return pkt_len;
}
static int safe_startup(struct usb_serial *serial)
{
struct usb_interface_descriptor *desc;
if (serial->dev->descriptor.bDeviceClass != CDC_DEVICE_CLASS)
return -ENODEV;
desc = &serial->interface->cur_altsetting->desc;
if (desc->bInterfaceClass != LINEO_INTERFACE_CLASS)
return -ENODEV;
if (desc->bInterfaceSubClass != LINEO_INTERFACE_SUBCLASS_SAFESERIAL)
return -ENODEV;
switch (desc->bInterfaceProtocol) {
case LINEO_SAFESERIAL_CRC:
break ;
case LINEO_SAFESERIAL_CRC_PADDED:
padded = true ;
break ;
default :
return -EINVAL;
}
return 0 ;
}
static struct usb_serial_driver safe_device = {
.driver = {
.name = "safe_serial" ,
},
.id_table = id_table,
.num_ports = 1 ,
.process_read_urb = safe_process_read_urb,
.prepare_write_buffer = safe_prepare_write_buffer,
.attach = safe_startup,
};
static struct usb_serial_driver * const serial_drivers[] = {
&safe_device, NULL
};
module_usb_serial_driver(serial_drivers, id_table);
Messung V0.5 in Prozent C=92 H=93 G=92
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-06)
¤
*© Formatika GbR, Deutschland