1 // SPDX-License-Identifier: GPL-2.0
3 * Cypress USB Thermometer driver
5 * Copyright (c) 2004 Erik Rigtorp <erkki@linux.nu> <erik@rigtorp.com>
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 */
40 /* They all operate on one byte at a time */
42 #define READ_ROM 0x01 /* Reads form ROM, value = address */
43 #define READ_RAM 0x02 /* Reads form RAM, value = address */
44 #define WRITE_RAM 0x03 /* Write to RAM, value = address, index = data */
45 #define READ_PORT 0x04 /* Reads from port, value = address */
46 #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */
49 /* Send a vendor command to device */
50 static int vendor_command(struct usb_device
*dev
, unsigned char request
,
51 unsigned char value
, unsigned char index
,
54 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
56 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_OTHER
,
59 USB_CTRL_GET_TIMEOUT
);
64 #define BRIGHTNESS 0x2c /* RAM location for brightness value */
65 #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */
67 static ssize_t
brightness_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
69 struct usb_interface
*intf
= to_usb_interface(dev
);
70 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
72 return sprintf(buf
, "%i", cytherm
->brightness
);
75 static ssize_t
brightness_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
78 struct usb_interface
*intf
= to_usb_interface(dev
);
79 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
81 unsigned char *buffer
;
84 buffer
= kmalloc(8, GFP_KERNEL
);
88 cytherm
->brightness
= simple_strtoul(buf
, NULL
, 10);
90 if (cytherm
->brightness
> 0xFF)
91 cytherm
->brightness
= 0xFF;
92 else if (cytherm
->brightness
< 0)
93 cytherm
->brightness
= 0;
96 retval
= vendor_command(cytherm
->udev
, WRITE_RAM
, BRIGHTNESS
,
97 cytherm
->brightness
, buffer
, 8);
99 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
100 /* Inform µC that we have changed the brightness setting */
101 retval
= vendor_command(cytherm
->udev
, WRITE_RAM
, BRIGHTNESS_SEM
,
104 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
110 static DEVICE_ATTR_RW(brightness
);
113 #define TEMP 0x33 /* RAM location for temperature */
114 #define SIGN 0x34 /* RAM location for temperature sign */
116 static ssize_t
temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
119 struct usb_interface
*intf
= to_usb_interface(dev
);
120 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
123 unsigned char *buffer
;
127 buffer
= kmalloc(8, GFP_KERNEL
);
131 /* read temperature */
132 retval
= vendor_command(cytherm
->udev
, READ_RAM
, TEMP
, 0, buffer
, 8);
134 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
138 retval
= vendor_command(cytherm
->udev
, READ_RAM
, SIGN
, 0, buffer
, 8);
140 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
145 return sprintf(buf
, "%c%i.%i", sign
? '-' : '+', temp
>> 1,
146 5*(temp
- ((temp
>> 1) << 1)));
148 static DEVICE_ATTR_RO(temp
);
153 static ssize_t
button_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
156 struct usb_interface
*intf
= to_usb_interface(dev
);
157 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
160 unsigned char *buffer
;
162 buffer
= kmalloc(8, GFP_KERNEL
);
167 retval
= vendor_command(cytherm
->udev
, READ_RAM
, BUTTON
, 0, buffer
, 8);
169 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
176 return sprintf(buf
, "1");
178 return sprintf(buf
, "0");
180 static DEVICE_ATTR_RO(button
);
183 static ssize_t
port0_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
185 struct usb_interface
*intf
= to_usb_interface(dev
);
186 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
189 unsigned char *buffer
;
191 buffer
= kmalloc(8, GFP_KERNEL
);
195 retval
= vendor_command(cytherm
->udev
, READ_PORT
, 0, 0, buffer
, 8);
197 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
203 return sprintf(buf
, "%d", retval
);
207 static ssize_t
port0_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
209 struct usb_interface
*intf
= to_usb_interface(dev
);
210 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
212 unsigned char *buffer
;
216 buffer
= kmalloc(8, GFP_KERNEL
);
220 tmp
= simple_strtoul(buf
, NULL
, 10);
227 retval
= vendor_command(cytherm
->udev
, WRITE_PORT
, 0,
230 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
236 static DEVICE_ATTR_RW(port0
);
238 static ssize_t
port1_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
240 struct usb_interface
*intf
= to_usb_interface(dev
);
241 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
244 unsigned char *buffer
;
246 buffer
= kmalloc(8, GFP_KERNEL
);
250 retval
= vendor_command(cytherm
->udev
, READ_PORT
, 1, 0, buffer
, 8);
252 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
258 return sprintf(buf
, "%d", retval
);
262 static ssize_t
port1_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
264 struct usb_interface
*intf
= to_usb_interface(dev
);
265 struct usb_cytherm
*cytherm
= usb_get_intfdata(intf
);
267 unsigned char *buffer
;
271 buffer
= kmalloc(8, GFP_KERNEL
);
275 tmp
= simple_strtoul(buf
, NULL
, 10);
282 retval
= vendor_command(cytherm
->udev
, WRITE_PORT
, 1,
285 dev_dbg(&cytherm
->udev
->dev
, "retval = %d\n", retval
);
291 static DEVICE_ATTR_RW(port1
);
293 static struct attribute
*cytherm_attrs
[] = {
294 &dev_attr_brightness
.attr
,
296 &dev_attr_button
.attr
,
297 &dev_attr_port0
.attr
,
298 &dev_attr_port1
.attr
,
301 ATTRIBUTE_GROUPS(cytherm
);
303 static int cytherm_probe(struct usb_interface
*interface
,
304 const struct usb_device_id
*id
)
306 struct usb_device
*udev
= interface_to_usbdev(interface
);
307 struct usb_cytherm
*dev
= NULL
;
308 int retval
= -ENOMEM
;
310 dev
= kzalloc (sizeof(struct usb_cytherm
), GFP_KERNEL
);
314 dev
->udev
= usb_get_dev(udev
);
316 usb_set_intfdata (interface
, dev
);
318 dev
->brightness
= 0xFF;
320 dev_info (&interface
->dev
,
321 "Cypress thermometer device now attached\n");
328 static void cytherm_disconnect(struct usb_interface
*interface
)
330 struct usb_cytherm
*dev
;
332 dev
= usb_get_intfdata (interface
);
334 /* first remove the files, then NULL the pointer */
335 usb_set_intfdata (interface
, NULL
);
337 usb_put_dev(dev
->udev
);
341 dev_info(&interface
->dev
, "Cypress thermometer now disconnected\n");
344 /* usb specific object needed to register this driver with the usb subsystem */
345 static struct usb_driver cytherm_driver
= {
347 .probe
= cytherm_probe
,
348 .disconnect
= cytherm_disconnect
,
349 .id_table
= id_table
,
350 .dev_groups
= cytherm_groups
,
353 module_usb_driver(cytherm_driver
);
355 MODULE_AUTHOR(DRIVER_AUTHOR
);
356 MODULE_DESCRIPTION(DRIVER_DESC
);
357 MODULE_LICENSE("GPL");