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