Played around with usb kernel code. Ended up with these modifications that reads random data on probe.

This commit is contained in:
Mickey Malone
2021-02-10 20:23:12 -06:00
parent 55de2de8c7
commit 396455021a

View File

@@ -19,9 +19,9 @@ MODULE_VERSION("1.0");
/**
* Static constants
**/
#define VENDOR_ID 0x0
#define PRODUCT_ID 0x4
#define SC_MINOR_BASE 31
#define VENDOR_ID 0x0
#define PRODUCT_ID 0x4
#define SC_MINOR_BASE 31
/**
* Helper macros
@@ -31,6 +31,10 @@ MODULE_VERSION("1.0");
#define LOGGER_WARN(fmt, args ...) printk( KERN_ERR "[warn] %s(%d): " fmt, __FUNCTION__, __LINE__, ## args)
#define LOGGER_DEBUG(fmt, args ...) if (debug == 1) { printk( KERN_DEBUG "[debug] %s(%d): " fmt, __FUNCTION__, __LINE__, ## args); }
static int debug = 0;
module_param(debug, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(debug, "Set the log level to debug");
/**
* Prototype Functions
**/
@@ -39,6 +43,13 @@ static void pico_rng_usb_disconnect(struct usb_interface *interface);
static int __init pico_rng_driver_init(void);
static void __exit pico_rng_driver_exit(void);
/**
static int pico_rng_open(struct inode *inode, struct file *file);
static int pico_rng_release(struct inode *inode, struct file *file);
static ssize_t pico_rng_write(struct file *file, const char __user *user_buf, size_t count, loff_t *ppos);
static ssize_t pico_rng_read(struct file *file, char __user *user, size_t count, loff_t *ppos);
**/
/**
* Data structure of the USB vid:pid device that we will support
@@ -58,16 +69,65 @@ static struct usb_driver pico_rng_usb_driver = {
.disconnect = pico_rng_usb_disconnect,
};
/**
* File operations data structure for the character device
* that will be implemented in this module
**/
/**static struct file_operations pico_rng_fops = {
.owner = THIS_MODULE,
.open = pico_rng_open,
.read = pico_rng_read,
.release = pico_rng_release,
};
**/
/**
* USB: Probe
* This method will be called if the device we plug in matches the vid:pid we are listening for
**/
static int pico_rng_usb_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
LOGGER_INFO("pico rng usb device initialized\n");
struct usb_device *dev = interface_to_usbdev(interface);
struct usb_endpoint_descriptor *endpoint = NULL;
int retval = -ENODEV;
char *buffer;
int actual_length;
int pipe = 0;
// TODO: Setup USB device, create seed thread to continuously gather entropy from device.
return 0;
retval = usb_find_bulk_in_endpoint(interface->cur_altsetting, &endpoint);
if(retval)
{
LOGGER_ERR("Unable to find bulk endpoint %d\n", retval);
return(retval);
}
pipe = usb_rcvbulkpipe(dev, endpoint->bEndpointAddress);
LOGGER_DEBUG("endpoint found %p with pipe %d", endpoint, pipe);
buffer = kmalloc(endpoint->wMaxPacketSize, GFP_KERNEL);
// int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, int *actual_length, int timeout)
retval = usb_bulk_msg (dev,
pipe,
buffer,
64,
&actual_length,
5000);
/* if the read was successful, copy the data to user space */
if (!retval)
{
LOGGER_INFO("%s\n", buffer);
}
else
{
LOGGER_INFO("usb_bulk_msg raised an error %d\n", retval);
}
kfree(buffer);
return retval;
}
/**
@@ -82,21 +142,21 @@ static void pico_rng_usb_disconnect(struct usb_interface *interface)
static int __init pico_rng_driver_init(void)
{
int result;
int retval = 0;
LOGGER_INFO("pico rng driver debut\n");
result = usb_register(&pico_rng_usb_driver);
if(result)
retval = usb_register(&pico_rng_usb_driver);
if(retval)
{
LOGGER_ERR("registering pico rng driver failed\n");
return retval;
}
else
{
LOGGER_INFO("pico rng driver registered successfully\n");
}
return result;
return retval;
}
static void __exit pico_rng_driver_exit(void)