trancevibrator.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/usb.h>
  26. #define DRIVER_AUTHOR "Sam Hocevar, sam@zoy.org"
  27. #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver"
  28. #define TRANCEVIBRATOR_VENDOR_ID 0x0b49 /* ASCII Corporation */
  29. #define TRANCEVIBRATOR_PRODUCT_ID 0x064f /* Trance Vibrator */
  30. static const struct usb_device_id id_table[] = {
  31. { USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) },
  32. { },
  33. };
  34. MODULE_DEVICE_TABLE (usb, id_table);
  35. /* Driver-local specific stuff */
  36. struct trancevibrator {
  37. struct usb_device *udev;
  38. unsigned int speed;
  39. };
  40. static ssize_t show_speed(struct device *dev, struct device_attribute *attr,
  41. char *buf)
  42. {
  43. struct usb_interface *intf = to_usb_interface(dev);
  44. struct trancevibrator *tv = usb_get_intfdata(intf);
  45. return sprintf(buf, "%d\n", tv->speed);
  46. }
  47. static ssize_t set_speed(struct device *dev, struct device_attribute *attr,
  48. const char *buf, size_t count)
  49. {
  50. struct usb_interface *intf = to_usb_interface(dev);
  51. struct trancevibrator *tv = usb_get_intfdata(intf);
  52. int temp, retval, old;
  53. temp = simple_strtoul(buf, NULL, 10);
  54. if (temp > 255)
  55. temp = 255;
  56. else if (temp < 0)
  57. temp = 0;
  58. old = tv->speed;
  59. tv->speed = temp;
  60. dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed);
  61. /* Set speed */
  62. retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0),
  63. 0x01, /* vendor request: set speed */
  64. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  65. tv->speed, /* speed value */
  66. 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  67. if (retval) {
  68. tv->speed = old;
  69. dev_dbg(&tv->udev->dev, "retval = %d\n", retval);
  70. return retval;
  71. }
  72. return count;
  73. }
  74. static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR, 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) {
  83. retval = -ENOMEM;
  84. goto error;
  85. }
  86. dev->udev = usb_get_dev(udev);
  87. usb_set_intfdata(interface, dev);
  88. retval = device_create_file(&interface->dev, &dev_attr_speed);
  89. if (retval)
  90. goto error_create_file;
  91. return 0;
  92. error_create_file:
  93. usb_put_dev(udev);
  94. usb_set_intfdata(interface, NULL);
  95. error:
  96. kfree(dev);
  97. return retval;
  98. }
  99. static void tv_disconnect(struct usb_interface *interface)
  100. {
  101. struct trancevibrator *dev;
  102. dev = usb_get_intfdata (interface);
  103. device_remove_file(&interface->dev, &dev_attr_speed);
  104. usb_set_intfdata(interface, NULL);
  105. usb_put_dev(dev->udev);
  106. kfree(dev);
  107. }
  108. /* USB subsystem object */
  109. static struct usb_driver tv_driver = {
  110. .name = "trancevibrator",
  111. .probe = tv_probe,
  112. .disconnect = tv_disconnect,
  113. .id_table = id_table,
  114. };
  115. module_usb_driver(tv_driver);
  116. MODULE_AUTHOR(DRIVER_AUTHOR);
  117. MODULE_DESCRIPTION(DRIVER_DESC);
  118. MODULE_LICENSE("GPL");