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_VERSION "v1.0"
24 #define DRIVER_AUTHOR "Erik Rigtorp"
25 #define DRIVER_DESC "Cypress USB Thermometer driver"
27 #define USB_SKEL_VENDOR_ID 0x04b4
28 #define USB_SKEL_PRODUCT_ID 0x0002
30 static const struct usb_device_id id_table
[] = {
31 { USB_DEVICE(USB_SKEL_VENDOR_ID
, USB_SKEL_PRODUCT_ID
) },
34 MODULE_DEVICE_TABLE (usb
, id_table
);
36 /* Structure to hold all of our device specific stuff */
38 struct usb_device
*udev
; /* save off the usb device pointer */
39 struct usb_interface
*interface
; /* the interface for this device */
44 /* local function prototypes */
45 static int cytherm_probe(struct usb_interface
*interface
,
46 const struct usb_device_id
*id
);
47 static void cytherm_disconnect(struct usb_interface
*interface
);
50 /* usb specific object needed to register this driver with the usb subsystem */
51 static struct usb_driver cytherm_driver
= {
53 .probe
= cytherm_probe
,
54 .disconnect
= cytherm_disconnect
,
59 /* They all operate on one byte at a time */
61 #define READ_ROM 0x01 /* Reads form ROM, value = address */
62 #define READ_RAM 0x02 /* Reads form RAM, value = address */
63 #define WRITE_RAM 0x03 /* Write to RAM, value = address, index = data */
64 #define READ_PORT 0x04 /* Reads from port, value = address */
65 #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */
68 /* Send a vendor command to device */
69 static int vendor_command(struct usb_device
*dev
, unsigned char request
,
70 unsigned char value
, unsigned char index
,
73 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
75 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
78 USB_CTRL_GET_TIMEOUT
);
83 #define BRIGHTNESS 0x2c /* RAM location for brightness value */
84 #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */
86 static ssize_t
show_brightness(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
88 struct usb_interface
*intf
= to_usb_interface(dev
);
89 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
91 return sprintf(buf
, "%i", cytherm
->brightness
);
94 static ssize_t
set_brightness(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
97 struct usb_interface
*intf
= to_usb_interface(dev
);
98 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
100 unsigned char *buffer
;
103 buffer
= kmalloc(8, GFP_KERNEL
);
107 cytherm
->brightness
= simple_strtoul(buf
, NULL
, 10);
109 if (cytherm
->brightness
> 0xFF)
110 cytherm
->brightness
= 0xFF;
111 else if (cytherm
->brightness
< 0)
112 cytherm
->brightness
= 0;
115 retval
= vendor_command(cytherm
->udev
, WRITE_RAM
, BRIGHTNESS
,
116 cytherm
->brightness
, buffer
, 8);
118 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
119 /* Inform µC that we have changed the brightness setting */
120 retval
= vendor_command(cytherm
->udev
, WRITE_RAM
, BRIGHTNESS_SEM
,
123 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
130 static DEVICE_ATTR(brightness
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
131 show_brightness
, set_brightness
);
134 #define TEMP 0x33 /* RAM location for temperature */
135 #define SIGN 0x34 /* RAM location for temperature sign */
137 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
140 struct usb_interface
*intf
= to_usb_interface(dev
);
141 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
144 unsigned char *buffer
;
148 buffer
= kmalloc(8, GFP_KERNEL
);
152 /* read temperature */
153 retval
= vendor_command(cytherm
->udev
, READ_RAM
, TEMP
, 0, buffer
, 8);
155 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
159 retval
= vendor_command(cytherm
->udev
, READ_RAM
, SIGN
, 0, buffer
, 8);
161 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
166 return sprintf(buf
, "%c%i.%i", sign
? '-' : '+', temp
>> 1,
167 5*(temp
- ((temp
>> 1) << 1)));
171 static ssize_t
set_temp(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
176 static DEVICE_ATTR(temp
, S_IRUGO
, show_temp
, set_temp
);
181 static ssize_t
show_button(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
184 struct usb_interface
*intf
= to_usb_interface(dev
);
185 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
188 unsigned char *buffer
;
190 buffer
= kmalloc(8, GFP_KERNEL
);
195 retval
= vendor_command(cytherm
->udev
, READ_RAM
, BUTTON
, 0, buffer
, 8);
197 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
204 return sprintf(buf
, "1");
206 return sprintf(buf
, "0");
210 static ssize_t
set_button(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
215 static DEVICE_ATTR(button
, S_IRUGO
, show_button
, set_button
);
218 static ssize_t
show_port0(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
220 struct usb_interface
*intf
= to_usb_interface(dev
);
221 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
224 unsigned char *buffer
;
226 buffer
= kmalloc(8, GFP_KERNEL
);
230 retval
= vendor_command(cytherm
->udev
, READ_PORT
, 0, 0, buffer
, 8);
232 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
238 return sprintf(buf
, "%d", retval
);
242 static ssize_t
set_port0(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
244 struct usb_interface
*intf
= to_usb_interface(dev
);
245 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
247 unsigned char *buffer
;
251 buffer
= kmalloc(8, GFP_KERNEL
);
255 tmp
= simple_strtoul(buf
, NULL
, 10);
262 retval
= vendor_command(cytherm
->udev
, WRITE_PORT
, 0,
265 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
272 static DEVICE_ATTR(port0
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_port0
, set_port0
);
274 static ssize_t
show_port1(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
276 struct usb_interface
*intf
= to_usb_interface(dev
);
277 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
280 unsigned char *buffer
;
282 buffer
= kmalloc(8, GFP_KERNEL
);
286 retval
= vendor_command(cytherm
->udev
, READ_PORT
, 1, 0, buffer
, 8);
288 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
294 return sprintf(buf
, "%d", retval
);
298 static ssize_t
set_port1(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
300 struct usb_interface
*intf
= to_usb_interface(dev
);
301 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
303 unsigned char *buffer
;
307 buffer
= kmalloc(8, GFP_KERNEL
);
311 tmp
= simple_strtoul(buf
, NULL
, 10);
318 retval
= vendor_command(cytherm
->udev
, WRITE_PORT
, 1,
321 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
328 static DEVICE_ATTR(port1
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_port1
, set_port1
);
332 static int cytherm_probe(struct usb_interface
*interface
,
333 const struct usb_device_id
*id
)
335 struct usb_device
*udev
= interface_to_usbdev(interface
);
336 struct usb_cytherm
*dev
= NULL
;
337 int retval
= -ENOMEM
;
339 dev
= kzalloc (sizeof(struct usb_cytherm
), GFP_KERNEL
);
343 dev
->udev
= usb_get_dev(udev
);
345 usb_set_intfdata (interface
, dev
);
347 dev
->brightness
= 0xFF;
349 retval
= device_create_file(&interface
->dev
, &dev_attr_brightness
);
352 retval
= device_create_file(&interface
->dev
, &dev_attr_temp
);
355 retval
= device_create_file(&interface
->dev
, &dev_attr_button
);
358 retval
= device_create_file(&interface
->dev
, &dev_attr_port0
);
361 retval
= device_create_file(&interface
->dev
, &dev_attr_port1
);
365 dev_info (&interface
->dev
,
366 "Cypress thermometer device now attached\n");
369 device_remove_file(&interface
->dev
, &dev_attr_brightness
);
370 device_remove_file(&interface
->dev
, &dev_attr_temp
);
371 device_remove_file(&interface
->dev
, &dev_attr_button
);
372 device_remove_file(&interface
->dev
, &dev_attr_port0
);
373 device_remove_file(&interface
->dev
, &dev_attr_port1
);
374 usb_set_intfdata (interface
, NULL
);
375 usb_put_dev(dev
->udev
);
381 static void cytherm_disconnect(struct usb_interface
*interface
)
383 struct usb_cytherm
*dev
;
385 dev
= usb_get_intfdata (interface
);
387 device_remove_file(&interface
->dev
, &dev_attr_brightness
);
388 device_remove_file(&interface
->dev
, &dev_attr_temp
);
389 device_remove_file(&interface
->dev
, &dev_attr_button
);
390 device_remove_file(&interface
->dev
, &dev_attr_port0
);
391 device_remove_file(&interface
->dev
, &dev_attr_port1
);
393 /* first remove the files, then NULL the pointer */
394 usb_set_intfdata (interface
, NULL
);
396 usb_put_dev(dev
->udev
);
400 dev_info(&interface
->dev
, "Cypress thermometer now disconnected\n");
403 module_usb_driver(cytherm_driver
);
405 MODULE_AUTHOR(DRIVER_AUTHOR
);
406 MODULE_DESCRIPTION(DRIVER_DESC
);
407 MODULE_LICENSE("GPL");