<commit_before>/*
 * LIRC driver for Philips eHome USB Infrared Transciever
 * and the Microsoft MCE 2005 Remote Control
 * 
 * (C) by <NAME> <<EMAIL>>
 *
 * Transmitter support and reception code cleanup.
 * (C) by <NAME> <<EMAIL>>
 *
 * Derived from ATI USB driver by <NAME> and the original
 * MCE USB driver by <NAME>
 *
 * This driver will only work reliably with kernel version 2.6.10
 * or higher, probably because of differences in USB device enumeration
 * in the kernel code. Device initialization fails most of the time
 * with earlier kernel versions.
 *
 **********************************************************************
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5)
#error "*******************************************************"
#error "Sorry, this driver needs kernel version 2.6.5 or higher"
#error "*******************************************************"
#endif

#include <linux/autoconf.h>

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/smp_lock.h>
#include <linux/completion.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <linux/time.h>

#include "drivers/lirc.h"
#include "drivers/kcompat.h"
#include "drivers/lirc_dev/lirc_dev.h"

#define DRIVER_VERSION          "$Revision: 1.20 $"
#define DRIVER_AUTHOR           "<NAME> <<EMAIL>>, <NAME> <<EMAIL>>"
#define DRIVER_DESC             "Philips eHome USB IR Transciever and Microsoft MCE 2005 Remote Control driver for LIRC"
#define DRIVER_NAME		"lirc_mceusb2"

#define USB_BUFLEN              16             /* USB reception buffer length */
#define LIRCBUF_SIZE            256            /* LIRC work buffer length */

/* MCE constants */
#define MCE_CMDBUF_SIZE         384            /* MCE Command buffer length */
#define MCE_TIME_UNIT           50             /* Approx 50us resolution */
#define MCE_CODE_LENGTH         5              /* Normal length of one mce packet (header included) */
#define MCE_PACKET_SIZE         4              /* Normal length of one mce packet (header not included) */
#define MCE_PACKET_HEADER       0x84           /* Actual header format is 0x80 + num_bytes */
#define MCE_CONTROL_HEADER      0x9F           /* MCE status header */
#define MCE_TX_HEADER_LENGTH    3              /* Number of bytes in the initializing tx header */
#define MCE_MAX_CHANNELS        2              /* Two transmitters, hardware dependent? */
#define MCE_DEFAULT_TX_MASK     0x03           /* Supported values are TX1=0x01, TX2=0x02, ALL=0x03 */
#define MCE_PULSE_BIT           0x80           /* Pulse bit, MSB set == PULSE else SPACE */
#define MCE_PULSE_MASK          0x7F           /* Pulse mask */
#define MCE_MAX_PULSE_LENGTH    0x7F           /* Longest transmittable pulse symbol */


/* module parameters */
#ifdef CONFIG_USB_DEBUG
	static int debug = 1;
#else
	static int debug = 0;
#endif
#define dprintk(fmt, args...)                                 \
	do{                                                   \
		if(debug) printk(KERN_DEBUG fmt, ## args);    \
	}while(0)

/* lock irctl structure */
//#define IRLOCK			down_interruptible(&ir->lock)
#define IRLOCK			down(&ir->lock)
#define IRUNLOCK		up(&ir->lock)

/* general constants */
#define SUCCESS			0
#define SEND_FLAG_IN_PROGRESS	1
#define SEND_FLAG_COMPLETE	2
#define RECV_FLAG_IN_PROGRESS	3
#define RECV_FLAG_COMPLETE	4

#define PHILUSB_INBOUND		1
#define PHILUSB_OUTBOUND	2

#define VENDOR_PHILIPS		0x0471
#define VENDOR_SMK              0x0609
#define VENDOR_TATUNG		0x1460
#define VENDOR_GATEWAY		0x107b
#define VENDOR_SHUTTLE		0x1308
#define VENDOR_MITSUMI          0x03ee
#define VENDOR_TOPSEED          0x1784 
#define VENDOR_RICAVISION       0x179d
#define VENDOR_ITRON            0x195d
#define VENDOR_FIC		0x1509

static struct usb_device_id usb_remote_table [] = {
	{ USB_DEVICE(VENDOR_PHILIPS, 0x0815) },	/* Philips eHome Infrared Transciever */
	{ USB_DEVICE(VENDOR_SMK, 0x031d) },	/* SMK/Toshiba G83C0004D410 */
	{ USB_DEVICE(VENDOR_TATUNG, 0x9150) },  /* Tatung eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },  /* Shuttle eHome Infrared Transceiver */
        { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },  /* Gateway eHome Infrared Transceiver */
        { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },  /* Mitsumi */
	{ USB_DEVICE(VENDOR_TOPSEED, 0x0001) },  /* Topseed eHome Infrared Transceiver */ 
	{ USB_DEVICE(VENDOR_RICAVISION, 0x0010) }, /* Ricavision internal Infrared Transceiver */
	{ USB_DEVICE(VENDOR_ITRON, 0x7002) },   /* Itron ione Libra Q-11 */
	{ USB_DEVICE(VENDOR_FIC, 0x9242) },     /* FIC eHome Infrared Transceiver */
	{ }					/* Terminating entry */
};

/* data structure for each usb remote */
struct irctl {

	/* usb */
	struct usb_device *usbdev;
	struct urb *urb_in;
	int devnum;
        struct usb_endpoint_descriptor *usb_ep_in;
        struct usb_endpoint_descriptor *usb_ep_out;

	/* buffers and dma */
	unsigned char *buf_in;
	unsigned int len_in;
	dma_addr_t dma_in;
	dma_addr_t dma_out;

	/* lirc */
	struct lirc_plugin *p;
	lirc_t lircdata;
	unsigned char is_pulse;
	int connected;
	
	unsigned char transmitter_mask;

	/* handle sending (init strings) */
	int send_flags;
	wait_queue_head_t wait_out;
   
	struct semaphore lock;
};

/* init strings */
static char init1[] = {0x00, 0xff, 0xaa, 0xff, 0x0b};
static char init2[] = {0xff, 0x18};



static void usb_remote_printdata(struct irctl *ir, char *buf, int len)
{
	char codes[USB_BUFLEN*3 + 1];
	int i;

	if (len <= 0)
		return;

	for (i = 0; i < len && i < USB_BUFLEN; i++) {
		snprintf(codes+i*3, 4, "%02x ", buf[i] & 0xFF);
	}
	printk(DRIVER_NAME "[%d]: data received %s (length=%d)\n",
		ir->devnum, codes, len);
}

static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
{
	struct irctl *ir;
	int len;

	if (!urb)
		return;
	
	if ((ir = urb->context)) {
		len = urb->actual_length;
<commit_msg>usecs_to_jiffies<commit_after>/*
 * LIRC driver for Philips eHome USB Infrared Transciever
 * and the Microsoft MCE 2005 Remote Control
 * 
 * (C) by <NAME> <<EMAIL>>
 *
 * Transmitter support and reception code cleanup.
 * (C) by <NAME> <<EMAIL>>
 *
 * Derived from ATI USB driver by <NAME> and the original
 * MCE USB driver by <NAME>
 *
 * This driver will only work reliably with kernel version 2.6.10
 * or higher, probably because of differences in USB device enumeration
 * in the kernel code. Device initialization fails most of the time
 * with earlier kernel versions.
 *
 **********************************************************************
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5)
#error "*******************************************************"
#error "Sorry, this driver needs kernel version 2.6.5 or higher"
#error "*******************************************************"
#endif

#include <linux/autoconf.h>

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/smp_lock.h>
#include <linux/completion.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <linux/time.h>

#include "drivers/lirc.h"
#include "drivers/kcompat.h"
#include "drivers/lirc_dev/lirc_dev.h"

#define DRIVER_VERSION          "$Revision: 1.21 $"
#define DRIVER_AUTHOR           "<NAME> <<EMAIL>>, <NAME> <<EMAIL>>"
#define DRIVER_DESC             "Philips eHome USB IR Transciever and Microsoft MCE 2005 Remote Control driver for LIRC"
#define DRIVER_NAME		"lirc_mceusb2"

#define USB_BUFLEN              16             /* USB reception buffer length */
#define LIRCBUF_SIZE            256            /* LIRC work buffer length */

/* MCE constants */
#define MCE_CMDBUF_SIZE         384            /* MCE Command buffer length */
#define MCE_TIME_UNIT           50             /* Approx 50us resolution */
#define MCE_CODE_LENGTH         5              /* Normal length of one mce packet (header included) */
#define MCE_PACKET_SIZE         4              /* Normal length of one mce packet (header not included) */
#define MCE_PACKET_HEADER       0x84           /* Actual header format is 0x80 + num_bytes */
#define MCE_CONTROL_HEADER      0x9F           /* MCE status header */
#define MCE_TX_HEADER_LENGTH    3              /* Number of bytes in the initializing tx header */
#define MCE_MAX_CHANNELS        2              /* Two transmitters, hardware dependent? */
#define MCE_DEFAULT_TX_MASK     0x03           /* Supported values are TX1=0x01, TX2=0x02, ALL=0x03 */
#define MCE_PULSE_BIT           0x80           /* Pulse bit, MSB set == PULSE else SPACE */
#define MCE_PULSE_MASK          0x7F           /* Pulse mask */
#define MCE_MAX_PULSE_LENGTH    0x7F           /* Longest transmittable pulse symbol */


/* module parameters */
#ifdef CONFIG_USB_DEBUG
	static int debug = 1;
#else
	static int debug = 0;
#endif
#define dprintk(fmt, args...)                                 \
	do{                                                   \
		if(debug) printk(KERN_DEBUG fmt, ## args);    \
	}while(0)

/* lock irctl structure */
//#define IRLOCK			down_interruptible(&ir->lock)
#define IRLOCK			down(&ir->lock)
#define IRUNLOCK		up(&ir->lock)

/* general constants */
#define SUCCESS			0
#define SEND_FLAG_IN_PROGRESS	1
#define SEND_FLAG_COMPLETE	2
#define RECV_FLAG_IN_PROGRESS	3
#define RECV_FLAG_COMPLETE	4

#define PHILUSB_INBOUND		1
#define PHILUSB_OUTBOUND	2

#define VENDOR_PHILIPS		0x0471
#define VENDOR_SMK              0x0609
#define VENDOR_TATUNG		0x1460
#define VENDOR_GATEWAY		0x107b
#define VENDOR_SHUTTLE		0x1308
#define VENDOR_MITSUMI          0x03ee
#define VENDOR_TOPSEED          0x1784 
#define VENDOR_RICAVISION       0x179d
#define VENDOR_ITRON            0x195d
#define VENDOR_FIC		0x1509

static struct usb_device_id usb_remote_table [] = {
	{ USB_DEVICE(VENDOR_PHILIPS, 0x0815) },	/* Philips eHome Infrared Transciever */
	{ USB_DEVICE(VENDOR_SMK, 0x031d) },	/* SMK/Toshiba G83C0004D410 */
	{ USB_DEVICE(VENDOR_TATUNG, 0x9150) },  /* Tatung eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },  /* Shuttle eHome Infrared Transceiver */
        { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },  /* Gateway eHome Infrared Transceiver */
        { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },  /* Mitsumi */
	{ USB_DEVICE(VENDOR_TOPSEED, 0x0001) },  /* Topseed eHome Infrared Transceiver */ 
	{ USB_DEVICE(VENDOR_RICAVISION, 0x0010) }, /* Ricavision internal Infrared Transceiver */
	{ USB_DEVICE(VENDOR_ITRON, 0x7002) },   /* Itron ione Libra Q-11 */
	{ USB_DEVICE(VENDOR_FIC, 0x9242) },     /* FIC eHome Infrared Transceiver */
	{ }					/* Terminating entry */
};

/* data structure for each usb remote */
struct irctl {

	/* usb */
	struct usb_device *usbdev;
	struct urb *urb_in;
	int devnum;
        struct usb_endpoint_descriptor *usb_ep_in;
        struct usb_endpoint_descriptor *usb_ep_out;

	/* buffers and dma */
	unsigned char *buf_in;
	unsigned int len_in;
	dma_addr_t dma_in;
	dma_addr_t dma_out;

	/* lirc */
	struct lirc_plugin *p;
	lirc_t lircdata;
	unsigned char is_pulse;
	int connected;
	
	unsigned char transmitter_mask;

	/* handle sending (init strings) */
	int send_flags;
	wait_queue_head_t wait_out;
   
	struct semaphore lock;
};

/* init strings */
static char init1[] = {0x00, 0xff, 0xaa, 0xff, 0x0b};
static char init2[] = {0xff, 0x18};

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
static inline unsigned long usecs_to_jiffies(const unsigned int u)
{
	if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
		return MAX_JIFFY_OFFSET;
#if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
	return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ);
#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
	return u * (HZ / USEC_PER_SEC);
#else
	return (u * HZ + USEC_PER_SEC - 1) / USEC_PER_SEC;
#endif
}
#endif


static void usb_remote_printdata(struct irctl *ir, char *buf, int len)
{
	char codes[USB_BUFLEN*3 + 1];
	int i;

	if (len <= 0)
		return;

	for (i = 0; i < len && i < USB_BUFLEN; i++) {
		snprintf(codes+i*3, 4, "%02x ", buf[i] & 0xFF);
	}
	printk(DRIVER_NAME "[%d]: data received %s (length=%d)\n",
		ir->devnum, codes, len);
}

static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
{
	struct irctl *ir;
	int len;

	if (!urb)
		return;
	
	if ((ir = urb->context)) {
		len = urb->actual_length;

{'fraction_non_alphanumeric': 0.12572826008775084, 'fraction_numerical': 0.03193555347766669, 'mean_word_length': 3.2532884674212297, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '802568', 'n_tokens_mistral': 5041, 'n_tokens_neox': 4767, 'n_words': 1404}