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/config.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/usb.h>
24 #define DRIVER_VERSION "v1.0"
25 #define DRIVER_AUTHOR "Erik Rigtorp"
26 #define DRIVER_DESC "Cypress USB Thermometer driver"
28 #define USB_SKEL_VENDOR_ID 0x04b4
29 #define USB_SKEL_PRODUCT_ID 0x0002
31 static struct usb_device_id id_table [] = {
32 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
35 MODULE_DEVICE_TABLE (usb, id_table);
37 /* Structure to hold all of our device specific stuff */
39 struct usb_device *udev; /* save off the usb device pointer */
40 struct usb_interface *interface; /* the interface for this device */
45 /* local function prototypes */
46 static int cytherm_probe(struct usb_interface *interface,
47 const struct usb_device_id *id);
48 static void cytherm_disconnect(struct usb_interface *interface);
49 int vendor_command(struct usb_device *dev, unsigned char request,
50 unsigned char value, unsigned char index,
54 /* usb specific object needed to register this driver with the usb subsystem */
55 static struct usb_driver cytherm_driver = {
58 .probe = cytherm_probe,
59 .disconnect = cytherm_disconnect,
64 /* They all operate on one byte at a time */
66 #define READ_ROM 0x01 /* Reads form ROM, value = address */
67 #define READ_RAM 0x02 /* Reads form RAM, value = address */
68 #define WRITE_RAM 0x03 /* Write to RAM, value = address, index = data */
69 #define READ_PORT 0x04 /* Reads from port, value = address */
70 #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */
73 /* Send a vendor command to device */
74 int vendor_command(struct usb_device *dev, unsigned char request,
75 unsigned char value, unsigned char index,
78 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
80 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
83 HZ * USB_CTRL_GET_TIMEOUT);
88 #define BRIGHTNESS 0x2c /* RAM location for brightness value */
89 #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */
91 static ssize_t show_brightness(struct device *dev, char *buf)
93 struct usb_interface *intf = to_usb_interface(dev);
94 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
96 return sprintf(buf, "%i", cytherm->brightness);
99 static ssize_t set_brightness(struct device *dev, const char *buf,
102 struct usb_interface *intf = to_usb_interface(dev);
103 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
105 unsigned char *buffer;
108 buffer = kmalloc(8, GFP_KERNEL);
110 dev_err(&cytherm->udev->dev, "out of memory\n");
114 cytherm->brightness = simple_strtoul(buf, NULL, 10);
116 if (cytherm->brightness > 0xFF)
117 cytherm->brightness = 0xFF;
118 else if (cytherm->brightness < 0)
119 cytherm->brightness = 0;
122 retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS,
123 cytherm->brightness, buffer, 8);
125 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
126 /* Inform µC that we have changed the brightness setting */
127 retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS_SEM,
130 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
137 static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP,
138 show_brightness, set_brightness);
141 #define TEMP 0x33 /* RAM location for temperature */
142 #define SIGN 0x34 /* RAM location for temperature sign */
144 static ssize_t show_temp(struct device *dev, char *buf)
147 struct usb_interface *intf = to_usb_interface(dev);
148 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
151 unsigned char *buffer;
155 buffer = kmalloc(8, GFP_KERNEL);
157 dev_err(&cytherm->udev->dev, "out of memory\n");
161 /* read temperature */
162 retval = vendor_command(cytherm->udev, READ_RAM, TEMP, 0, buffer, 8);
164 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
168 retval = vendor_command(cytherm->udev, READ_RAM, SIGN, 0, buffer, 8);
170 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
175 return sprintf(buf, "%c%i.%i", sign ? '-' : '+', temp >> 1,
176 5*(temp - ((temp >> 1) << 1)));
180 static ssize_t set_temp(struct device *dev, const char *buf, size_t count)
185 static DEVICE_ATTR(temp, S_IRUGO, show_temp, set_temp);
190 static ssize_t show_button(struct device *dev, char *buf)
193 struct usb_interface *intf = to_usb_interface(dev);
194 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
197 unsigned char *buffer;
199 buffer = kmalloc(8, GFP_KERNEL);
201 dev_err(&cytherm->udev->dev, "out of memory\n");
206 retval = vendor_command(cytherm->udev, READ_RAM, BUTTON, 0, buffer, 8);
208 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
215 return sprintf(buf, "1");
217 return sprintf(buf, "0");
221 static ssize_t set_button(struct device *dev, const char *buf, size_t count)
226 static DEVICE_ATTR(button, S_IRUGO, show_button, set_button);
229 static ssize_t show_port0(struct device *dev, char *buf)
231 struct usb_interface *intf = to_usb_interface(dev);
232 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
235 unsigned char *buffer;
237 buffer = kmalloc(8, GFP_KERNEL);
239 dev_err(&cytherm->udev->dev, "out of memory\n");
243 retval = vendor_command(cytherm->udev, READ_PORT, 0, 0, buffer, 8);
245 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
251 return sprintf(buf, "%d", retval);
255 static ssize_t set_port0(struct device *dev, const char *buf, size_t count)
257 struct usb_interface *intf = to_usb_interface(dev);
258 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
260 unsigned char *buffer;
264 buffer = kmalloc(8, GFP_KERNEL);
266 dev_err(&cytherm->udev->dev, "out of memory\n");
270 tmp = simple_strtoul(buf, NULL, 10);
277 retval = vendor_command(cytherm->udev, WRITE_PORT, 0,
280 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
287 static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR | S_IWGRP, show_port0, set_port0);
289 static ssize_t show_port1(struct device *dev, char *buf)
291 struct usb_interface *intf = to_usb_interface(dev);
292 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
295 unsigned char *buffer;
297 buffer = kmalloc(8, GFP_KERNEL);
299 dev_err(&cytherm->udev->dev, "out of memory\n");
303 retval = vendor_command(cytherm->udev, READ_PORT, 1, 0, buffer, 8);
305 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
311 return sprintf(buf, "%d", retval);
315 static ssize_t set_port1(struct device *dev, const char *buf, size_t count)
317 struct usb_interface *intf = to_usb_interface(dev);
318 struct usb_cytherm *cytherm = usb_get_intfdata(intf);
320 unsigned char *buffer;
324 buffer = kmalloc(8, GFP_KERNEL);
326 dev_err(&cytherm->udev->dev, "out of memory\n");
330 tmp = simple_strtoul(buf, NULL, 10);
337 retval = vendor_command(cytherm->udev, WRITE_PORT, 1,
340 dev_dbg(&led->udev->dev, "retval = %d\n", retval);
347 static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR | S_IWGRP, show_port1, set_port1);
351 static int cytherm_probe(struct usb_interface *interface,
352 const struct usb_device_id *id)
354 struct usb_device *udev = interface_to_usbdev(interface);
355 struct usb_cytherm *dev = NULL;
356 int retval = -ENOMEM;
358 dev = kmalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
360 dev_err (&interface->dev, "Out of memory\n");
363 memset (dev, 0x00, sizeof (*dev));
365 dev->udev = usb_get_dev(udev);
367 usb_set_intfdata (interface, dev);
369 dev->brightness = 0xFF;
371 device_create_file(&interface->dev, &dev_attr_brightness);
372 device_create_file(&interface->dev, &dev_attr_temp);
373 device_create_file(&interface->dev, &dev_attr_button);
374 device_create_file(&interface->dev, &dev_attr_port0);
375 device_create_file(&interface->dev, &dev_attr_port1);
377 dev_info (&interface->dev,
378 "Cypress thermometer device now attached\n");
386 static void cytherm_disconnect(struct usb_interface *interface)
388 struct usb_cytherm *dev;
390 dev = usb_get_intfdata (interface);
391 usb_set_intfdata (interface, NULL);
393 device_remove_file(&interface->dev, &dev_attr_brightness);
394 device_remove_file(&interface->dev, &dev_attr_temp);
395 device_remove_file(&interface->dev, &dev_attr_button);
396 device_remove_file(&interface->dev, &dev_attr_port0);
397 device_remove_file(&interface->dev, &dev_attr_port1);
399 usb_put_dev(dev->udev);
403 dev_info(&interface->dev, "Cypress thermometer now disconnected\n");
407 static int __init usb_cytherm_init(void)
411 result = usb_register(&cytherm_driver);
414 err("usb_register failed. Error number %d", result);
418 info(DRIVER_VERSION ":" DRIVER_DESC);
422 static void __exit usb_cytherm_exit(void)
424 usb_deregister(&cytherm_driver);
428 module_init (usb_cytherm_init);
429 module_exit (usb_cytherm_exit);
431 MODULE_AUTHOR(DRIVER_AUTHOR);
432 MODULE_DESCRIPTION(DRIVER_DESC);
433 MODULE_LICENSE("GPL");