Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/kernel.h>
- #include <linux/errno.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/module.h>
- #include <linux/usb.h>
- //#include <linux/smp_lock.h>
- #include <asm/uaccess.h>
- #include <linux/version.h>
- #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
- #include <linux/autoconf.h>
- #else
- #include <generated/autoconf.h>
- #endif
- MODULE_LICENSE("Dual BSD/GPL");
- #define VENDOR_ID 0x0001
- #define PRODUCT_ID 0x1110
- static struct usb_device_id id_table[] = {
- { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
- { },
- };
- MODULE_DEVICE_TABLE (usb, id_table);
- struct usb_led {
- struct usb_device *udev;
- unsigned char value;
- };
- static void set_light(struct usb_led *led){
- int retval;
- const unsigned char *buffer = kmalloc(8, GFP_KERNEL);
- retval = usb_control_msg(led->udev,
- usb_sndctrlpipe(led->udev, 0),
- 0x12,
- 0xc8,
- (0x02 * 0x100) + 0x0a,
- (0x00 * 0x100) * led->value,
- (void*)buffer,
- 8,
- 2*HZ);
- kfree(buffer);
- }
- static ssize_t show_led(struct device *dev,
- struct device_attribute *attr,
- char *buf) {
- struct usb_interface *intf = to_usb_interface(dev);
- struct usb_led *led = usb_get_intfdata(intf);
- return sprintf(buf, "%d\n", led->value); // Might need to be ->red
- }
- static ssize_t set_led(struct device *dev,
- struct device_attribute *attr,
- const char *buf,
- size_t count) {
- struct usb_interface *intf = to_usb_interface(dev);
- struct usb_led *led = usb_get_intfdata(intf);
- int temp = simple_strtoul(buf, NULL, 10);
- led->value = temp;
- set_light(led);
- return count;
- }
- static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_led, set_led);
- // Called when the device is initial connected and probed
- // by the kernel
- static int led_probe(struct usb_interface *interface,
- const struct usb_device_id *id) {
- struct usb_device *udev = interface_to_usbdev(interface);
- struct usb_led *dev = NULL;
- dev = kmalloc(sizeof(struct usb_led), GFP_KERNEL);
- memset(dev, 0x00, sizeof(*dev));
- dev->udev = usb_get_dev(udev);
- usb_set_intfdata(interface, dev);
- device_create_file(&interface->dev, &dev_attr_value);
- dev_info(&interface->dev, "USB LED Device now attached");
- return 0;
- }
- // Called when the device is disconnected
- //
- static void led_disconnect(struct usb_interface *interface) {
- struct usb_led *dev;
- dev = usb_get_intfdata (interface);
- usb_set_intfdata (interface, NULL);
- device_remove_file(&interface->dev, &dev_attr_value);
- usb_put_dev(dev->udev);
- kfree(dev);
- dev_info(&interface->dev, "USB LED now disconnected\n");
- }
- // Holds information used by the USB framework
- static struct usb_driver led_driver = {
- // .owner = THIS_MODULE,
- .name = "usbled",
- .probe = led_probe,
- .disconnect = led_disconnect,
- .id_table = id_table,
- };
- // insmod calls this
- static int __init usbled_init(void) {
- int retval = 0;
- retval = usb_register(&led_driver);
- if (retval)
- printk("usb_register failed. Error number %d", retval);
- return retval;
- }
- // rmmod calls this
- static void __exit usbled_exit(void) {
- usb_deregister(&led_driver);
- }
- module_init(usbled_init);
- module_exit(usbled_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement