Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 2 | /* |
| 3 | * PlayStation 2 Trance Vibrator driver |
| 4 | * |
| 5 | * Copyright (C) 2006 Sam Hocevar <sam@zoy.org> |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* Standard include files */ |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/usb.h> |
| 14 | |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 15 | #define DRIVER_AUTHOR "Sam Hocevar, sam@zoy.org" |
| 16 | #define DRIVER_DESC "PlayStation 2 Trance Vibrator driver" |
| 17 | |
| 18 | #define TRANCEVIBRATOR_VENDOR_ID 0x0b49 /* ASCII Corporation */ |
| 19 | #define TRANCEVIBRATOR_PRODUCT_ID 0x064f /* Trance Vibrator */ |
| 20 | |
Németh Márton | 33b9e16 | 2010-01-10 15:34:45 +0100 | [diff] [blame] | 21 | static const struct usb_device_id id_table[] = { |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 22 | { USB_DEVICE(TRANCEVIBRATOR_VENDOR_ID, TRANCEVIBRATOR_PRODUCT_ID) }, |
| 23 | { }, |
| 24 | }; |
| 25 | MODULE_DEVICE_TABLE (usb, id_table); |
| 26 | |
| 27 | /* Driver-local specific stuff */ |
| 28 | struct trancevibrator { |
| 29 | struct usb_device *udev; |
| 30 | unsigned int speed; |
| 31 | }; |
| 32 | |
Greg Kroah-Hartman | ed5bd7a | 2018-01-23 11:24:05 +0100 | [diff] [blame] | 33 | static ssize_t speed_show(struct device *dev, struct device_attribute *attr, |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 34 | char *buf) |
| 35 | { |
| 36 | struct usb_interface *intf = to_usb_interface(dev); |
| 37 | struct trancevibrator *tv = usb_get_intfdata(intf); |
| 38 | |
| 39 | return sprintf(buf, "%d\n", tv->speed); |
| 40 | } |
| 41 | |
Greg Kroah-Hartman | ed5bd7a | 2018-01-23 11:24:05 +0100 | [diff] [blame] | 42 | static ssize_t speed_store(struct device *dev, struct device_attribute *attr, |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 43 | const char *buf, size_t count) |
| 44 | { |
| 45 | struct usb_interface *intf = to_usb_interface(dev); |
| 46 | struct trancevibrator *tv = usb_get_intfdata(intf); |
Oliver Neukum | 0cc5e2e | 2008-01-30 16:06:03 +0100 | [diff] [blame] | 47 | int temp, retval, old; |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 48 | |
Ding Xiang | 9d20bca | 2018-08-30 17:31:18 +0800 | [diff] [blame] | 49 | retval = kstrtoint(buf, 10, &temp); |
| 50 | if (retval) |
| 51 | return retval; |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 52 | if (temp > 255) |
| 53 | temp = 255; |
| 54 | else if (temp < 0) |
| 55 | temp = 0; |
Oliver Neukum | 0cc5e2e | 2008-01-30 16:06:03 +0100 | [diff] [blame] | 56 | old = tv->speed; |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 57 | tv->speed = temp; |
| 58 | |
| 59 | dev_dbg(&tv->udev->dev, "speed = %d\n", tv->speed); |
| 60 | |
| 61 | /* Set speed */ |
| 62 | retval = usb_control_msg(tv->udev, usb_sndctrlpipe(tv->udev, 0), |
| 63 | 0x01, /* vendor request: set speed */ |
| 64 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, |
| 65 | tv->speed, /* speed value */ |
| 66 | 0, NULL, 0, USB_CTRL_GET_TIMEOUT); |
| 67 | if (retval) { |
Oliver Neukum | 0cc5e2e | 2008-01-30 16:06:03 +0100 | [diff] [blame] | 68 | tv->speed = old; |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 69 | dev_dbg(&tv->udev->dev, "retval = %d\n", retval); |
| 70 | return retval; |
| 71 | } |
| 72 | return count; |
| 73 | } |
Greg Kroah-Hartman | ed5bd7a | 2018-01-23 11:24:05 +0100 | [diff] [blame] | 74 | static DEVICE_ATTR_RW(speed); |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 75 | |
Greg Kroah-Hartman | f9bbcbe | 2019-08-06 16:45:00 +0200 | [diff] [blame] | 76 | static struct attribute *tv_attrs[] = { |
| 77 | &dev_attr_speed.attr, |
| 78 | NULL, |
| 79 | }; |
| 80 | ATTRIBUTE_GROUPS(tv); |
| 81 | |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 82 | static int tv_probe(struct usb_interface *interface, |
| 83 | const struct usb_device_id *id) |
| 84 | { |
| 85 | struct usb_device *udev = interface_to_usbdev(interface); |
| 86 | struct trancevibrator *dev; |
| 87 | int retval; |
| 88 | |
| 89 | dev = kzalloc(sizeof(struct trancevibrator), GFP_KERNEL); |
Wolfram Sang | 58e6140 | 2016-08-25 19:39:21 +0200 | [diff] [blame] | 90 | if (!dev) { |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 91 | retval = -ENOMEM; |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | dev->udev = usb_get_dev(udev); |
| 96 | usb_set_intfdata(interface, dev); |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 100 | error: |
| 101 | kfree(dev); |
| 102 | return retval; |
| 103 | } |
| 104 | |
| 105 | static void tv_disconnect(struct usb_interface *interface) |
| 106 | { |
| 107 | struct trancevibrator *dev; |
| 108 | |
| 109 | dev = usb_get_intfdata (interface); |
Oliver Neukum | 96ca014 | 2006-11-24 12:55:59 +0100 | [diff] [blame] | 110 | usb_set_intfdata(interface, NULL); |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 111 | usb_put_dev(dev->udev); |
| 112 | kfree(dev); |
| 113 | } |
| 114 | |
| 115 | /* USB subsystem object */ |
| 116 | static struct usb_driver tv_driver = { |
| 117 | .name = "trancevibrator", |
| 118 | .probe = tv_probe, |
| 119 | .disconnect = tv_disconnect, |
| 120 | .id_table = id_table, |
Greg Kroah-Hartman | f9bbcbe | 2019-08-06 16:45:00 +0200 | [diff] [blame] | 121 | .dev_groups = tv_groups, |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 124 | module_usb_driver(tv_driver); |
Sam Hocevar | 5638e4d | 2006-08-30 02:34:56 +0200 | [diff] [blame] | 125 | |
| 126 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 127 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 128 | MODULE_LICENSE("GPL"); |