trancevibrator.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * PlayStation 2 Trance Vibrator driver
  3. *
  4. * Copyright (C) 2006 Sam Hocevar <sam@zoy.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /* Standard include files */
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <linux/usb.h>
  27. /* Version Information */
  28. #define DRIVER_VERSION "v1.1"
  29. #define DRIVER_AUTHOR "Sam Hocevar, sam@zoy.org"
  30. #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver"
  31. #define TRANCEVIBRATOR_VENDOR_ID 0x0b49 /* ASCII Corporation */
  32. #define TRANCEVIBRATOR_PRODUCT_ID 0x064f /* Trance Vibrator */
  33. static const struct usb_device_id id_table[] = {
  34. { USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) },
  35. { },
  36. };
  37. MODULE_DEVICE_TABLE (usb, id_table);
  38. /* Driver-local specific stuff */
  39. struct trancevibrator {
  40. struct usb_device *udev;
  41. unsigned int speed;
  42. };
  43. static ssize_t show_speed(struct device *dev, struct device_attribute *attr,
  44. char *buf)
  45. {
  46. struct usb_interface *intf = to_usb_interface(dev);
  47. struct trancevibrator *tv = usb_get_intfdata(intf);
  48. return sprintf(buf, "%d\n", tv->speed);
  49. }
  50. static ssize_t set_speed(struct device *dev, struct device_attribute *attr,
  51. const char *buf, size_t count)
  52. {
  53. struct usb_interface *intf = to_usb_interface(dev);
  54. struct trancevibrator *tv = usb_get_intfdata(intf);
  55. int temp, retval, old;
  56. temp = simple_strtoul(buf, NULL, 10);
  57. if (temp > 255)
  58. temp = 255;
  59. else if (temp < 0)
  60. temp = 0;
  61. old = tv->speed;
  62. tv->speed = temp;
  63. dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
  64. /* Set speed */
  65. retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
  66. 0x01, /* vendor request: set speed */
  67. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  68. tv->speed, /* speed value */
  69. 0, NULL, 0, USB_CTRL_GET_TIMEOUT);
  70. if (retval) {
  71. tv->speed = old;
  72. dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
  73. return retval;
  74. }
  75. return count;
  76. }
  77. static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR, show_speed, set_speed);
  78. static int tv_probe(struct usb_interface *interface,
  79. const struct usb_device_id *id)
  80. {
  81. struct usb_device *udev = interface_to_usbdev(interface);
  82. struct trancevibrator *dev;
  83. int retval;
  84. dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
  85. if (dev == NULL) {
  86. dev_err(&interface->dev, "Out of memory\n");
  87. retval = -ENOMEM;
  88. goto error;
  89. }
  90. dev->udev = usb_get_dev(udev);
  91. usb_set_intfdata(interface, dev);
  92. retval = device_create_file(&interface->dev, &dev_attr_speed);
  93. if (retval)
  94. goto error_create_file;
  95. return 0;
  96. error_create_file:
  97. usb_put_dev(udev);
  98. usb_set_intfdata(interface, NULL);
  99. error:
  100. kfree(dev);
  101. return retval;
  102. }
  103. static void tv_disconnect(struct usb_interface *interface)
  104. {
  105. struct trancevibrator *dev;
  106. dev = usb_get_intfdata (interface);
  107. device_remove_file(&interface->dev, &dev_attr_speed);
  108. usb_set_intfdata(interface, NULL);
  109. usb_put_dev(dev->udev);
  110. kfree(dev);
  111. }
  112. /* USB subsystem object */
  113. static struct usb_driver tv_driver = {
  114. .name = "trancevibrator",
  115. .probe = tv_probe,
  116. .disconnect = tv_disconnect,
  117. .id_table = id_table,
  118. };
  119. module_usb_driver(tv_driver);
  120. MODULE_AUTHOR(DRIVER_AUTHOR);
  121. MODULE_DESCRIPTION(DRIVER_DESC);
  122. MODULE_LICENSE("GPL");