1 // SPDX-License-Identifier: GPL-2.0
3 * Cypress USB Thermometer driver
7 * This driver works with Elektor magazine USB Interface as published in
8 * issue #291. It should also work with the original starter kit/demo board
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/usb.h>
19 #define DRIVER_AUTHOR "Erik Rigtorp"
20 #define DRIVER_DESC "Cypress USB Thermometer driver"
22 #define USB_SKEL_VENDOR_ID 0x04b4
23 #define USB_SKEL_PRODUCT_ID 0x0002
25 static const struct usb_device_id id_table[] = {
26 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
29 MODULE_DEVICE_TABLE (usb, id_table);
31 /* Structure to hold all of our device specific stuff */
33 struct usb_device *udev; /* save off the usb device pointer */
34 struct usb_interface *interface; /* the interface for this device */
39 /* local function prototypes */
40 static int cytherm_probe(struct usb_interface *interface,
41 const struct usb_device_id *id);
42 static void cytherm_disconnect(struct usb_interface *interface);
45 /* usb specific object needed to register this driver with the usb subsystem */
46 static struct usb_driver cytherm_driver = {
48 .probe = cytherm_probe,
49 .disconnect = cytherm_disconnect,
54 /* They all operate on one byte at a time */
56 #define READ_ROM 0x01 /* Reads form ROM, value = address */
57 #define READ_RAM 0x02 /* Reads form RAM, value = address */
58 #define WRITE_RAM 0x03 /* Write to RAM, value = address, index = data */
59 #define READ_PORT 0x04 /* Reads from port, value = address */
60 #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */
63 /* Send a vendor command to device */
64 static int vendor_command(struct usb_device *dev, unsigned char request,
65 unsigned char value, unsigned char index,
68 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
70 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
73 USB_CTRL_GET_TIMEOUT);
78 #define BRIGHTNESS 0x2c /* RAM location for brightness value */
79 #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */
81 static ssize_t show_brightness(struct device *dev, struct device_attribute *attr, char *buf)
83 struct usb_interface *intf = to_usb_interface(dev);
84 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
86 return sprintf(buf, "%i", cytherm->brightness);
89 static ssize_t set_brightness(struct device *dev, struct device_attribute *attr, const char *buf,
92 struct usb_interface *intf = to_usb_interface(dev);
93 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
95 unsigned char *buffer;
98 buffer = kmalloc(8, GFP_KERNEL);
102 cytherm->brightness = simple_strtoul(buf, NULL, 10);
104 if (cytherm->brightness > 0xFF)
105 cytherm->brightness = 0xFF;
106 else if (cytherm->brightness < 0)
107 cytherm->brightness = 0;
110 retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS,
111 cytherm->brightness, buffer, 8);
113 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
114 /* Inform µC that we have changed the brightness setting */
115 retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS_SEM,
118 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
125 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP,
126 show_brightness, set_brightness);
129 #define TEMP 0x33 /* RAM location for temperature */
130 #define SIGN 0x34 /* RAM location for temperature sign */
132 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
135 struct usb_interface *intf = to_usb_interface(dev);
136 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
139 unsigned char *buffer;
143 buffer = kmalloc(8, GFP_KERNEL);
147 /* read temperature */
148 retval = vendor_command(cytherm->udev, READ_RAM, TEMP, 0, buffer, 8);
150 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
154 retval = vendor_command(cytherm->udev, READ_RAM, SIGN, 0, buffer, 8);
156 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
161 return sprintf(buf, "%c%i.%i", sign ? '-' : '+', temp >> 1,
162 5*(temp - ((temp >> 1) << 1)));
166 static ssize_t set_temp(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
171 static DEVICE_ATTR(temp, S_IRUGO, show_temp, set_temp);
176 static ssize_t show_button(struct device *dev, struct device_attribute *attr, char *buf)
179 struct usb_interface *intf = to_usb_interface(dev);
180 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
183 unsigned char *buffer;
185 buffer = kmalloc(8, GFP_KERNEL);
190 retval = vendor_command(cytherm->udev, READ_RAM, BUTTON, 0, buffer, 8);
192 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
199 return sprintf(buf, "1");
201 return sprintf(buf, "0");
205 static ssize_t set_button(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
210 static DEVICE_ATTR(button, S_IRUGO, show_button, set_button);
213 static ssize_t show_port0(struct device *dev, struct device_attribute *attr, char *buf)
215 struct usb_interface *intf = to_usb_interface(dev);
216 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
219 unsigned char *buffer;
221 buffer = kmalloc(8, GFP_KERNEL);
225 retval = vendor_command(cytherm->udev, READ_PORT, 0, 0, buffer, 8);
227 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
233 return sprintf(buf, "%d", retval);
237 static ssize_t set_port0(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
239 struct usb_interface *intf = to_usb_interface(dev);
240 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
242 unsigned char *buffer;
246 buffer = kmalloc(8, GFP_KERNEL);
250 tmp = simple_strtoul(buf, NULL, 10);
257 retval = vendor_command(cytherm->udev, WRITE_PORT, 0,
260 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
267 static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR | S_IWGRP, show_port0, set_port0);
269 static ssize_t show_port1(struct device *dev, struct device_attribute *attr, char *buf)
271 struct usb_interface *intf = to_usb_interface(dev);
272 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
275 unsigned char *buffer;
277 buffer = kmalloc(8, GFP_KERNEL);
281 retval = vendor_command(cytherm->udev, READ_PORT, 1, 0, buffer, 8);
283 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
289 return sprintf(buf, "%d", retval);
293 static ssize_t set_port1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
295 struct usb_interface *intf = to_usb_interface(dev);
296 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
298 unsigned char *buffer;
302 buffer = kmalloc(8, GFP_KERNEL);
306 tmp = simple_strtoul(buf, NULL, 10);
313 retval = vendor_command(cytherm->udev, WRITE_PORT, 1,
316 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
323 static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR | S_IWGRP, show_port1, set_port1);
327 static int cytherm_probe(struct usb_interface *interface,
328 const struct usb_device_id *id)
330 struct usb_device *udev = interface_to_usbdev(interface);
331 struct usb_cytherm *dev = NULL;
332 int retval = -ENOMEM;
334 dev = kzalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
338 dev->udev = usb_get_dev(udev);
340 usb_set_intfdata (interface, dev);
342 dev->brightness = 0xFF;
344 retval = device_create_file(&interface->dev, &dev_attr_brightness);
347 retval = device_create_file(&interface->dev, &dev_attr_temp);
350 retval = device_create_file(&interface->dev, &dev_attr_button);
353 retval = device_create_file(&interface->dev, &dev_attr_port0);
356 retval = device_create_file(&interface->dev, &dev_attr_port1);
360 dev_info (&interface->dev,
361 "Cypress thermometer device now attached\n");
364 device_remove_file(&interface->dev, &dev_attr_brightness);
365 device_remove_file(&interface->dev, &dev_attr_temp);
366 device_remove_file(&interface->dev, &dev_attr_button);
367 device_remove_file(&interface->dev, &dev_attr_port0);
368 device_remove_file(&interface->dev, &dev_attr_port1);
369 usb_set_intfdata (interface, NULL);
370 usb_put_dev(dev->udev);
376 static void cytherm_disconnect(struct usb_interface *interface)
378 struct usb_cytherm *dev;
380 dev = usb_get_intfdata (interface);
382 device_remove_file(&interface->dev, &dev_attr_brightness);
383 device_remove_file(&interface->dev, &dev_attr_temp);
384 device_remove_file(&interface->dev, &dev_attr_button);
385 device_remove_file(&interface->dev, &dev_attr_port0);
386 device_remove_file(&interface->dev, &dev_attr_port1);
388 /* first remove the files, then NULL the pointer */
389 usb_set_intfdata (interface, NULL);
391 usb_put_dev(dev->udev);
395 dev_info(&interface->dev, "Cypress thermometer now disconnected\n");
398 module_usb_driver(cytherm_driver);
400 MODULE_AUTHOR(DRIVER_AUTHOR);
401 MODULE_DESCRIPTION(DRIVER_DESC);
402 MODULE_LICENSE("GPL");