trancevibrator.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/module.h>
  25. #include <linux/usb.h>
  26. /* Version Information */
  27. #define DRIVER_VERSION "v1.1"
  28. #define DRIVER_AUTHOR "Sam Hocevar, sam@zoy.org"
  29. #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver"
  30. #define TRANCEVIBRATOR_VENDOR_ID 0x0b49 /* ASCII Corporation */
  31. #define TRANCEVIBRATOR_PRODUCT_ID 0x064f /* Trance Vibrator */
  32. static struct usb_device_id id_table [] = {
  33. { USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) },
  34. { },
  35. };
  36. MODULE_DEVICE_TABLE (usb, id_table);
  37. /* Driver-local specific stuff */
  38. struct trancevibrator {
  39. struct usb_device *udev;
  40. unsigned int speed;
  41. };
  42. static ssize_t show_speed(struct device *dev, struct device_attribute *attr,
  43. char *buf)
  44. {
  45. struct usb_interface *intf = to_usb_interface(dev);
  46. struct trancevibrator *tv = usb_get_intfdata(intf);
  47. return sprintf(buf, "%d\n", tv->speed);
  48. }
  49. static ssize_t set_speed(struct device *dev, struct device_attribute *attr,
  50. const char *buf, size_t count)
  51. {
  52. struct usb_interface *intf = to_usb_interface(dev);
  53. struct trancevibrator *tv = usb_get_intfdata(intf);
  54. int temp, retval;
  55. temp = simple_strtoul(buf, NULL, 10);
  56. if (temp > 255)
  57. temp = 255;
  58. else if (temp < 0)
  59. temp = 0;
  60. tv->speed = temp;
  61. dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
  62. /* Set speed */
  63. retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
  64. 0x01, /* vendor request: set speed */
  65. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  66. tv->speed, /* speed value */
  67. 0, NULL, 0, USB_CTRL_GET_TIMEOUT);
  68. if (retval) {
  69. dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
  70. return retval;
  71. }
  72. return count;
  73. }
  74. static DEVICE_ATTR(speed, S_IWUGO | S_IRUGO, show_speed, set_speed);
  75. static int tv_probe(struct usb_interface *interface,
  76. const struct usb_device_id *id)
  77. {
  78. struct usb_device *udev = interface_to_usbdev(interface);
  79. struct trancevibrator *dev;
  80. int retval;
  81. dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL);
  82. if (dev == NULL) {
  83. dev_err(&interface->dev, "Out of memory\n");
  84. retval = -ENOMEM;
  85. goto error;
  86. }
  87. dev->udev = usb_get_dev(udev);
  88. usb_set_intfdata(interface, dev);
  89. retval = device_create_file(&interface->dev, &dev_attr_speed);
  90. if (retval)
  91. goto error_create_file;
  92. return 0;
  93. error_create_file:
  94. usb_put_dev(udev);
  95. usb_set_intfdata(interface, NULL);
  96. error:
  97. kfree(dev);
  98. return retval;
  99. }
  100. static void tv_disconnect(struct usb_interface *interface)
  101. {
  102. struct trancevibrator *dev;
  103. dev = usb_get_intfdata (interface);
  104. usb_set_intfdata(interface, NULL);
  105. device_remove_file(&interface->dev, &dev_attr_speed);
  106. usb_put_dev(dev->udev);
  107. kfree(dev);
  108. }
  109. /* USB subsystem object */
  110. static struct usb_driver tv_driver = {
  111. .name = "trancevibrator",
  112. .probe = tv_probe,
  113. .disconnect = tv_disconnect,
  114. .id_table = id_table,
  115. };
  116. static int __init tv_init(void)
  117. {
  118. int retval = usb_register(&tv_driver);
  119. if (retval) {
  120. err("usb_register failed. Error number %d", retval);
  121. return retval;
  122. }
  123. info(DRIVER_VERSION ":" DRIVER_DESC);
  124. return 0;
  125. }
  126. static void __exit tv_exit(void)
  127. {
  128. usb_deregister(&tv_driver);
  129. }
  130. module_init (tv_init);
  131. module_exit (tv_exit);
  132. MODULE_AUTHOR(DRIVER_AUTHOR);
  133. MODULE_DESCRIPTION(DRIVER_DESC);
  134. MODULE_LICENSE("GPL");