| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- | 
 | 2 |  * Cypress USB Thermometer driver  | 
 | 3 |  *  | 
 | 4 |  * Copyright (c) 2004 Erik Rigtorp <erkki@linux.nu> <erik@rigtorp.com> | 
 | 5 |  *  | 
 | 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 | 
 | 8 |  * from Cypress. | 
 | 9 |  * | 
 | 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. | 
 | 13 |  * | 
 | 14 |  */ | 
 | 15 |  | 
 | 16 |  | 
 | 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> | 
 | 23 |  | 
 | 24 | #define DRIVER_VERSION "v1.0" | 
 | 25 | #define DRIVER_AUTHOR "Erik Rigtorp" | 
 | 26 | #define DRIVER_DESC "Cypress USB Thermometer driver" | 
 | 27 |  | 
 | 28 | #define USB_SKEL_VENDOR_ID	0x04b4 | 
 | 29 | #define USB_SKEL_PRODUCT_ID	0x0002 | 
 | 30 |  | 
 | 31 | static struct usb_device_id id_table [] = { | 
 | 32 | 	{ USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, | 
 | 33 | 	{ } | 
 | 34 | }; | 
 | 35 | MODULE_DEVICE_TABLE (usb, id_table); | 
 | 36 |  | 
 | 37 | /* Structure to hold all of our device specific stuff */ | 
 | 38 | struct usb_cytherm { | 
 | 39 | 	struct usb_device    *udev;	 /* save off the usb device pointer */ | 
 | 40 | 	struct usb_interface *interface; /* the interface for this device */ | 
 | 41 | 	int brightness; | 
 | 42 | }; | 
 | 43 |  | 
 | 44 |  | 
 | 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 |  | 
 | 50 |  | 
 | 51 | /* usb specific object needed to register this driver with the usb subsystem */ | 
 | 52 | static struct usb_driver cytherm_driver = { | 
 | 53 | 	.owner =	THIS_MODULE, | 
 | 54 | 	.name =		"cytherm", | 
 | 55 | 	.probe =	cytherm_probe, | 
 | 56 | 	.disconnect =	cytherm_disconnect, | 
 | 57 | 	.id_table =	id_table, | 
 | 58 | }; | 
 | 59 |  | 
 | 60 | /* Vendor requests */ | 
 | 61 | /* They all operate on one byte at a time */ | 
 | 62 | #define PING       0x00 | 
 | 63 | #define READ_ROM   0x01 /* Reads form ROM, value = address */ | 
 | 64 | #define READ_RAM   0x02 /* Reads form RAM, value = address */ | 
 | 65 | #define WRITE_RAM  0x03 /* Write to RAM, value = address, index = data */ | 
 | 66 | #define READ_PORT  0x04 /* Reads from port, value = address */ | 
 | 67 | #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */  | 
 | 68 |  | 
 | 69 |  | 
 | 70 | /* Send a vendor command to device */ | 
 | 71 | static int vendor_command(struct usb_device *dev, unsigned char request,  | 
 | 72 | 			  unsigned char value, unsigned char index, | 
 | 73 | 			  void *buf, int size) | 
 | 74 | { | 
 | 75 | 	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | 
 | 76 | 			       request,  | 
 | 77 | 			       USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER, | 
 | 78 | 			       value,  | 
 | 79 | 			       index, buf, size, | 
 | 80 | 			       USB_CTRL_GET_TIMEOUT); | 
 | 81 | } | 
 | 82 |  | 
 | 83 |  | 
 | 84 |  | 
 | 85 | #define BRIGHTNESS 0x2c     /* RAM location for brightness value */ | 
 | 86 | #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */ | 
 | 87 |  | 
 | 88 | static ssize_t show_brightness(struct device *dev, char *buf) | 
 | 89 | { | 
 | 90 | 	struct usb_interface *intf = to_usb_interface(dev);     | 
 | 91 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf);      | 
 | 92 |  | 
 | 93 | 	return sprintf(buf, "%i", cytherm->brightness); | 
 | 94 | } | 
 | 95 |  | 
 | 96 | static ssize_t set_brightness(struct device *dev, const char *buf,  | 
 | 97 | 			      size_t count) | 
 | 98 | { | 
 | 99 | 	struct usb_interface *intf = to_usb_interface(dev); | 
 | 100 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf); | 
 | 101 |  | 
 | 102 | 	unsigned char *buffer; | 
 | 103 | 	int retval; | 
 | 104 |     | 
 | 105 | 	buffer = kmalloc(8, GFP_KERNEL); | 
 | 106 | 	if (!buffer) { | 
 | 107 | 		dev_err(&cytherm->udev->dev, "out of memory\n"); | 
 | 108 | 		return 0; | 
 | 109 | 	} | 
 | 110 |  | 
 | 111 | 	cytherm->brightness = simple_strtoul(buf, NULL, 10); | 
 | 112 |     | 
 | 113 | 	if (cytherm->brightness > 0xFF) | 
 | 114 | 		cytherm->brightness = 0xFF; | 
 | 115 | 	else if (cytherm->brightness < 0) | 
 | 116 | 		cytherm->brightness = 0; | 
 | 117 |     | 
 | 118 | 	/* Set brightness */ | 
 | 119 | 	retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS,  | 
 | 120 | 				cytherm->brightness, buffer, 8); | 
 | 121 | 	if (retval) | 
 | 122 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 123 | 	/* Inform µC that we have changed the brightness setting */ | 
 | 124 | 	retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS_SEM, | 
 | 125 | 				0x01, buffer, 8); | 
 | 126 | 	if (retval) | 
 | 127 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 128 |     | 
 | 129 | 	kfree(buffer); | 
 | 130 |     | 
 | 131 | 	return count; | 
 | 132 | } | 
 | 133 |  | 
 | 134 | static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP,  | 
 | 135 | 		   show_brightness, set_brightness); | 
 | 136 |  | 
 | 137 |  | 
 | 138 | #define TEMP 0x33 /* RAM location for temperature */ | 
 | 139 | #define SIGN 0x34 /* RAM location for temperature sign */ | 
 | 140 |  | 
 | 141 | static ssize_t show_temp(struct device *dev, char *buf) | 
 | 142 | { | 
 | 143 |  | 
 | 144 | 	struct usb_interface *intf = to_usb_interface(dev); | 
 | 145 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf); | 
 | 146 |  | 
 | 147 | 	int retval; | 
 | 148 | 	unsigned char *buffer; | 
 | 149 |  | 
 | 150 | 	int temp, sign; | 
 | 151 |     | 
 | 152 | 	buffer = kmalloc(8, GFP_KERNEL); | 
 | 153 | 	if (!buffer) { | 
 | 154 | 		dev_err(&cytherm->udev->dev, "out of memory\n"); | 
 | 155 | 		return 0; | 
 | 156 | 	} | 
 | 157 |  | 
 | 158 | 	/* read temperature */ | 
 | 159 | 	retval = vendor_command(cytherm->udev, READ_RAM, TEMP, 0, buffer, 8); | 
 | 160 | 	if (retval) | 
 | 161 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 162 | 	temp = buffer[1]; | 
 | 163 |     | 
 | 164 | 	/* read sign */ | 
 | 165 | 	retval = vendor_command(cytherm->udev, READ_RAM, SIGN, 0, buffer, 8); | 
 | 166 | 	if (retval) | 
 | 167 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 168 | 	sign = buffer[1]; | 
 | 169 |  | 
 | 170 | 	kfree(buffer); | 
 | 171 |     | 
 | 172 | 	return sprintf(buf, "%c%i.%i", sign ? '-' : '+', temp >> 1, | 
 | 173 | 		       5*(temp - ((temp >> 1) << 1))); | 
 | 174 | } | 
 | 175 |  | 
 | 176 |  | 
 | 177 | static ssize_t set_temp(struct device *dev, const char *buf, size_t count) | 
 | 178 | { | 
 | 179 | 	return count; | 
 | 180 | } | 
 | 181 |  | 
 | 182 | static DEVICE_ATTR(temp, S_IRUGO, show_temp, set_temp); | 
 | 183 |  | 
 | 184 |  | 
 | 185 | #define BUTTON 0x7a | 
 | 186 |  | 
 | 187 | static ssize_t show_button(struct device *dev, char *buf) | 
 | 188 | { | 
 | 189 |  | 
 | 190 | 	struct usb_interface *intf = to_usb_interface(dev); | 
 | 191 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf); | 
 | 192 |  | 
 | 193 | 	int retval; | 
 | 194 | 	unsigned char *buffer; | 
 | 195 |  | 
 | 196 | 	buffer = kmalloc(8, GFP_KERNEL); | 
 | 197 | 	if (!buffer) { | 
 | 198 | 		dev_err(&cytherm->udev->dev, "out of memory\n"); | 
 | 199 | 		return 0; | 
 | 200 | 	} | 
 | 201 |  | 
 | 202 | 	/* check button */ | 
 | 203 | 	retval = vendor_command(cytherm->udev, READ_RAM, BUTTON, 0, buffer, 8); | 
 | 204 | 	if (retval) | 
 | 205 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 206 |     | 
 | 207 | 	retval = buffer[1]; | 
 | 208 |  | 
 | 209 | 	kfree(buffer); | 
 | 210 |  | 
 | 211 | 	if (retval) | 
 | 212 | 		return sprintf(buf, "1"); | 
 | 213 | 	else | 
 | 214 | 		return sprintf(buf, "0"); | 
 | 215 | } | 
 | 216 |  | 
 | 217 |  | 
 | 218 | static ssize_t set_button(struct device *dev, const char *buf, size_t count) | 
 | 219 | { | 
 | 220 | 	return count; | 
 | 221 | } | 
 | 222 |  | 
 | 223 | static DEVICE_ATTR(button, S_IRUGO, show_button, set_button); | 
 | 224 |  | 
 | 225 |  | 
 | 226 | static ssize_t show_port0(struct device *dev, char *buf) | 
 | 227 | { | 
 | 228 | 	struct usb_interface *intf = to_usb_interface(dev); | 
 | 229 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf); | 
 | 230 |  | 
 | 231 | 	int retval; | 
 | 232 | 	unsigned char *buffer; | 
 | 233 |  | 
 | 234 | 	buffer = kmalloc(8, GFP_KERNEL); | 
 | 235 | 	if (!buffer) { | 
 | 236 | 		dev_err(&cytherm->udev->dev, "out of memory\n"); | 
 | 237 | 		return 0; | 
 | 238 | 	} | 
 | 239 |  | 
 | 240 | 	retval = vendor_command(cytherm->udev, READ_PORT, 0, 0, buffer, 8); | 
 | 241 | 	if (retval) | 
 | 242 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 243 |  | 
 | 244 | 	retval = buffer[1]; | 
 | 245 |  | 
 | 246 | 	kfree(buffer); | 
 | 247 |  | 
 | 248 | 	return sprintf(buf, "%d", retval); | 
 | 249 | } | 
 | 250 |  | 
 | 251 |  | 
 | 252 | static ssize_t set_port0(struct device *dev, const char *buf, size_t count) | 
 | 253 | { | 
 | 254 | 	struct usb_interface *intf = to_usb_interface(dev); | 
 | 255 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf); | 
 | 256 |  | 
 | 257 | 	unsigned char *buffer; | 
 | 258 | 	int retval; | 
 | 259 | 	int tmp; | 
 | 260 |     | 
 | 261 | 	buffer = kmalloc(8, GFP_KERNEL); | 
 | 262 | 	if (!buffer) { | 
 | 263 | 		dev_err(&cytherm->udev->dev, "out of memory\n"); | 
 | 264 | 		return 0; | 
 | 265 | 	} | 
 | 266 |  | 
 | 267 | 	tmp = simple_strtoul(buf, NULL, 10); | 
 | 268 |     | 
 | 269 | 	if (tmp > 0xFF) | 
 | 270 | 		tmp = 0xFF; | 
 | 271 | 	else if (tmp < 0) | 
 | 272 | 		tmp = 0; | 
 | 273 |     | 
 | 274 | 	retval = vendor_command(cytherm->udev, WRITE_PORT, 0, | 
 | 275 | 				tmp, buffer, 8); | 
 | 276 | 	if (retval) | 
 | 277 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 278 |  | 
 | 279 | 	kfree(buffer); | 
 | 280 |  | 
 | 281 | 	return count; | 
 | 282 | } | 
 | 283 |  | 
 | 284 | static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR | S_IWGRP, show_port0, set_port0); | 
 | 285 |  | 
 | 286 | static ssize_t show_port1(struct device *dev, char *buf) | 
 | 287 | { | 
 | 288 | 	struct usb_interface *intf = to_usb_interface(dev); | 
 | 289 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf); | 
 | 290 |  | 
 | 291 | 	int retval; | 
 | 292 | 	unsigned char *buffer; | 
 | 293 |  | 
 | 294 | 	buffer = kmalloc(8, GFP_KERNEL); | 
 | 295 | 	if (!buffer) { | 
 | 296 | 		dev_err(&cytherm->udev->dev, "out of memory\n"); | 
 | 297 | 		return 0; | 
 | 298 | 	} | 
 | 299 |  | 
 | 300 | 	retval = vendor_command(cytherm->udev, READ_PORT, 1, 0, buffer, 8); | 
 | 301 | 	if (retval) | 
 | 302 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 303 |     | 
 | 304 | 	retval = buffer[1]; | 
 | 305 |  | 
 | 306 | 	kfree(buffer); | 
 | 307 |  | 
 | 308 | 	return sprintf(buf, "%d", retval); | 
 | 309 | } | 
 | 310 |  | 
 | 311 |  | 
 | 312 | static ssize_t set_port1(struct device *dev, const char *buf, size_t count) | 
 | 313 | { | 
 | 314 | 	struct usb_interface *intf = to_usb_interface(dev); | 
 | 315 | 	struct usb_cytherm *cytherm = usb_get_intfdata(intf); | 
 | 316 |  | 
 | 317 | 	unsigned char *buffer; | 
 | 318 | 	int retval; | 
 | 319 | 	int tmp; | 
 | 320 |     | 
 | 321 | 	buffer = kmalloc(8, GFP_KERNEL); | 
 | 322 | 	if (!buffer) { | 
 | 323 | 		dev_err(&cytherm->udev->dev, "out of memory\n"); | 
 | 324 | 		return 0; | 
 | 325 | 	} | 
 | 326 |  | 
 | 327 | 	tmp = simple_strtoul(buf, NULL, 10); | 
 | 328 |     | 
 | 329 | 	if (tmp > 0xFF) | 
 | 330 | 		tmp = 0xFF; | 
 | 331 | 	else if (tmp < 0) | 
 | 332 | 		tmp = 0; | 
 | 333 |     | 
 | 334 | 	retval = vendor_command(cytherm->udev, WRITE_PORT, 1, | 
 | 335 | 				tmp, buffer, 8); | 
 | 336 | 	if (retval) | 
 | 337 | 		dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval); | 
 | 338 |  | 
 | 339 | 	kfree(buffer); | 
 | 340 |  | 
 | 341 | 	return count; | 
 | 342 | } | 
 | 343 |  | 
 | 344 | static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR | S_IWGRP, show_port1, set_port1); | 
 | 345 |  | 
 | 346 |  | 
 | 347 |  | 
 | 348 | static int cytherm_probe(struct usb_interface *interface,  | 
 | 349 | 			 const struct usb_device_id *id) | 
 | 350 | { | 
 | 351 | 	struct usb_device *udev = interface_to_usbdev(interface); | 
 | 352 | 	struct usb_cytherm *dev = NULL; | 
 | 353 | 	int retval = -ENOMEM; | 
 | 354 |  | 
 | 355 | 	dev = kmalloc (sizeof(struct usb_cytherm), GFP_KERNEL); | 
 | 356 | 	if (dev == NULL) { | 
 | 357 | 		dev_err (&interface->dev, "Out of memory\n"); | 
 | 358 | 		goto error; | 
 | 359 | 	} | 
 | 360 | 	memset (dev, 0x00, sizeof (*dev)); | 
 | 361 |  | 
 | 362 | 	dev->udev = usb_get_dev(udev); | 
 | 363 |  | 
 | 364 | 	usb_set_intfdata (interface, dev); | 
 | 365 |  | 
 | 366 | 	dev->brightness = 0xFF; | 
 | 367 |  | 
 | 368 | 	device_create_file(&interface->dev, &dev_attr_brightness);    | 
 | 369 | 	device_create_file(&interface->dev, &dev_attr_temp); | 
 | 370 | 	device_create_file(&interface->dev, &dev_attr_button); | 
 | 371 | 	device_create_file(&interface->dev, &dev_attr_port0); | 
 | 372 | 	device_create_file(&interface->dev, &dev_attr_port1); | 
 | 373 |  | 
 | 374 | 	dev_info (&interface->dev,  | 
 | 375 | 		  "Cypress thermometer device now attached\n"); | 
 | 376 | 	return 0; | 
 | 377 |  | 
 | 378 |  error: | 
 | 379 | 	kfree(dev); | 
 | 380 | 	return retval; | 
 | 381 | } | 
 | 382 |  | 
 | 383 | static void cytherm_disconnect(struct usb_interface *interface) | 
 | 384 | { | 
 | 385 | 	struct usb_cytherm *dev; | 
 | 386 |  | 
 | 387 | 	dev = usb_get_intfdata (interface); | 
 | 388 | 	usb_set_intfdata (interface, NULL); | 
 | 389 |  | 
 | 390 | 	device_remove_file(&interface->dev, &dev_attr_brightness); | 
 | 391 | 	device_remove_file(&interface->dev, &dev_attr_temp); | 
 | 392 | 	device_remove_file(&interface->dev, &dev_attr_button); | 
 | 393 | 	device_remove_file(&interface->dev, &dev_attr_port0); | 
 | 394 | 	device_remove_file(&interface->dev, &dev_attr_port1); | 
 | 395 |  | 
 | 396 | 	usb_put_dev(dev->udev); | 
 | 397 |  | 
 | 398 | 	kfree(dev); | 
 | 399 |  | 
 | 400 | 	dev_info(&interface->dev, "Cypress thermometer now disconnected\n"); | 
 | 401 | } | 
 | 402 |  | 
 | 403 |  | 
 | 404 | static int __init usb_cytherm_init(void) | 
 | 405 | { | 
 | 406 | 	int result; | 
 | 407 |  | 
 | 408 | 	result = usb_register(&cytherm_driver); | 
 | 409 | 	if (result)  | 
 | 410 | 	{	 | 
 | 411 | 		err("usb_register failed. Error number %d", result); | 
 | 412 | 		return result; | 
 | 413 | 	} | 
 | 414 |  | 
 | 415 | 	info(DRIVER_VERSION ":" DRIVER_DESC); | 
 | 416 | 	return 0; | 
 | 417 | } | 
 | 418 |  | 
 | 419 | static void __exit usb_cytherm_exit(void) | 
 | 420 | { | 
 | 421 | 	usb_deregister(&cytherm_driver); | 
 | 422 | } | 
 | 423 |  | 
 | 424 |  | 
 | 425 | module_init (usb_cytherm_init); | 
 | 426 | module_exit (usb_cytherm_exit); | 
 | 427 |  | 
 | 428 | MODULE_AUTHOR(DRIVER_AUTHOR); | 
 | 429 | MODULE_DESCRIPTION(DRIVER_DESC); | 
 | 430 | MODULE_LICENSE("GPL"); |