2 * Cypress USB Thermometer driver
4 * Copyright (c) 2004 Erik Rigtorp <erkki@linux.nu> <erik@rigtorp.com>
6 * This driver works with Elektor magazine USB Interface as published in
7 * issue #291. It should also work with the original starter kit/demo board
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
23 #define DRIVER_AUTHOR "Erik Rigtorp"
24 #define DRIVER_DESC "Cypress USB Thermometer driver"
26 #define USB_SKEL_VENDOR_ID 0x04b4
27 #define USB_SKEL_PRODUCT_ID 0x0002
29 static const struct usb_device_id id_table[] = {
30 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
33 MODULE_DEVICE_TABLE (usb, id_table);
35 /* Structure to hold all of our device specific stuff */
37 struct usb_device *udev; /* save off the usb device pointer */
38 struct usb_interface *interface; /* the interface for this device */
43 /* local function prototypes */
44 static int cytherm_probe(struct usb_interface *interface,
45 const struct usb_device_id *id);
46 static void cytherm_disconnect(struct usb_interface *interface);
49 /* usb specific object needed to register this driver with the usb subsystem */
50 static struct usb_driver cytherm_driver = {
52 .probe = cytherm_probe,
53 .disconnect = cytherm_disconnect,
58 /* They all operate on one byte at a time */
60 #define READ_ROM 0x01 /* Reads form ROM, value = address */
61 #define READ_RAM 0x02 /* Reads form RAM, value = address */
62 #define WRITE_RAM 0x03 /* Write to RAM, value = address, index = data */
63 #define READ_PORT 0x04 /* Reads from port, value = address */
64 #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */
67 /* Send a vendor command to device */
68 static int vendor_command(struct usb_device *dev, unsigned char request,
69 unsigned char value, unsigned char index,
72 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
74 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
77 USB_CTRL_GET_TIMEOUT);
82 #define BRIGHTNESS 0x2c /* RAM location for brightness value */
83 #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */
85 static ssize_t show_brightness(struct device *dev, struct device_attribute *attr, char *buf)
87 struct usb_interface *intf = to_usb_interface(dev);
88 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
90 return sprintf(buf, "%i", cytherm->brightness);
93 static ssize_t set_brightness(struct device *dev, struct device_attribute *attr, const char *buf,
96 struct usb_interface *intf = to_usb_interface(dev);
97 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
99 unsigned char *buffer;
102 buffer = kmalloc(8, GFP_KERNEL);
106 cytherm->brightness = simple_strtoul(buf, NULL, 10);
108 if (cytherm->brightness > 0xFF)
109 cytherm->brightness = 0xFF;
110 else if (cytherm->brightness < 0)
111 cytherm->brightness = 0;
114 retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS,
115 cytherm->brightness, buffer, 8);
117 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
118 /* Inform µC that we have changed the brightness setting */
119 retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS_SEM,
122 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
129 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP,
130 show_brightness, set_brightness);
133 #define TEMP 0x33 /* RAM location for temperature */
134 #define SIGN 0x34 /* RAM location for temperature sign */
136 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
139 struct usb_interface *intf = to_usb_interface(dev);
140 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
143 unsigned char *buffer;
147 buffer = kmalloc(8, GFP_KERNEL);
151 /* read temperature */
152 retval = vendor_command(cytherm->udev, READ_RAM, TEMP, 0, buffer, 8);
154 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
158 retval = vendor_command(cytherm->udev, READ_RAM, SIGN, 0, buffer, 8);
160 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
165 return sprintf(buf, "%c%i.%i", sign ? '-' : '+', temp >> 1,
166 5*(temp - ((temp >> 1) << 1)));
170 static ssize_t set_temp(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
175 static DEVICE_ATTR(temp, S_IRUGO, show_temp, set_temp);
180 static ssize_t show_button(struct device *dev, struct device_attribute *attr, char *buf)
183 struct usb_interface *intf = to_usb_interface(dev);
184 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
187 unsigned char *buffer;
189 buffer = kmalloc(8, GFP_KERNEL);
194 retval = vendor_command(cytherm->udev, READ_RAM, BUTTON, 0, buffer, 8);
196 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
203 return sprintf(buf, "1");
205 return sprintf(buf, "0");
209 static ssize_t set_button(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
214 static DEVICE_ATTR(button, S_IRUGO, show_button, set_button);
217 static ssize_t show_port0(struct device *dev, struct device_attribute *attr, char *buf)
219 struct usb_interface *intf = to_usb_interface(dev);
220 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
223 unsigned char *buffer;
225 buffer = kmalloc(8, GFP_KERNEL);
229 retval = vendor_command(cytherm->udev, READ_PORT, 0, 0, buffer, 8);
231 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
237 return sprintf(buf, "%d", retval);
241 static ssize_t set_port0(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
243 struct usb_interface *intf = to_usb_interface(dev);
244 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
246 unsigned char *buffer;
250 buffer = kmalloc(8, GFP_KERNEL);
254 tmp = simple_strtoul(buf, NULL, 10);
261 retval = vendor_command(cytherm->udev, WRITE_PORT, 0,
264 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
271 static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR | S_IWGRP, show_port0, set_port0);
273 static ssize_t show_port1(struct device *dev, struct device_attribute *attr, char *buf)
275 struct usb_interface *intf = to_usb_interface(dev);
276 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
279 unsigned char *buffer;
281 buffer = kmalloc(8, GFP_KERNEL);
285 retval = vendor_command(cytherm->udev, READ_PORT, 1, 0, buffer, 8);
287 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
293 return sprintf(buf, "%d", retval);
297 static ssize_t set_port1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
299 struct usb_interface *intf = to_usb_interface(dev);
300 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
302 unsigned char *buffer;
306 buffer = kmalloc(8, GFP_KERNEL);
310 tmp = simple_strtoul(buf, NULL, 10);
317 retval = vendor_command(cytherm->udev, WRITE_PORT, 1,
320 dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
327 static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR | S_IWGRP, show_port1, set_port1);
331 static int cytherm_probe(struct usb_interface *interface,
332 const struct usb_device_id *id)
334 struct usb_device *udev = interface_to_usbdev(interface);
335 struct usb_cytherm *dev = NULL;
336 int retval = -ENOMEM;
338 dev = kzalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
342 dev->udev = usb_get_dev(udev);
344 usb_set_intfdata (interface, dev);
346 dev->brightness = 0xFF;
348 retval = device_create_file(&interface->dev, &dev_attr_brightness);
351 retval = device_create_file(&interface->dev, &dev_attr_temp);
354 retval = device_create_file(&interface->dev, &dev_attr_button);
357 retval = device_create_file(&interface->dev, &dev_attr_port0);
360 retval = device_create_file(&interface->dev, &dev_attr_port1);
364 dev_info (&interface->dev,
365 "Cypress thermometer device now attached\n");
368 device_remove_file(&interface->dev, &dev_attr_brightness);
369 device_remove_file(&interface->dev, &dev_attr_temp);
370 device_remove_file(&interface->dev, &dev_attr_button);
371 device_remove_file(&interface->dev, &dev_attr_port0);
372 device_remove_file(&interface->dev, &dev_attr_port1);
373 usb_set_intfdata (interface, NULL);
374 usb_put_dev(dev->udev);
380 static void cytherm_disconnect(struct usb_interface *interface)
382 struct usb_cytherm *dev;
384 dev = usb_get_intfdata (interface);
386 device_remove_file(&interface->dev, &dev_attr_brightness);
387 device_remove_file(&interface->dev, &dev_attr_temp);
388 device_remove_file(&interface->dev, &dev_attr_button);
389 device_remove_file(&interface->dev, &dev_attr_port0);
390 device_remove_file(&interface->dev, &dev_attr_port1);
392 /* first remove the files, then NULL the pointer */
393 usb_set_intfdata (interface, NULL);
395 usb_put_dev(dev->udev);
399 dev_info(&interface->dev, "Cypress thermometer now disconnected\n");
402 module_usb_driver(cytherm_driver);
404 MODULE_AUTHOR(DRIVER_AUTHOR);
405 MODULE_DESCRIPTION(DRIVER_DESC);
406 MODULE_LICENSE("GPL");