cytherm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/usb.h>
  21. #define DRIVER_VERSION "v1.0"
  22. #define DRIVER_AUTHOR "Erik Rigtorp"
  23. #define DRIVER_DESC "Cypress USB Thermometer driver"
  24. #define USB_SKEL_VENDOR_ID 0x04b4
  25. #define USB_SKEL_PRODUCT_ID 0x0002
  26. static struct usb_device_id id_table [] = {
  27. { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
  28. { }
  29. };
  30. MODULE_DEVICE_TABLE (usb, id_table);
  31. /* Structure to hold all of our device specific stuff */
  32. struct usb_cytherm {
  33. struct usb_device *udev; /* save off the usb device pointer */
  34. struct usb_interface *interface; /* the interface for this device */
  35. int brightness;
  36. };
  37. /* local function prototypes */
  38. static int cytherm_probe(struct usb_interface *interface,
  39. const struct usb_device_id *id);
  40. static void cytherm_disconnect(struct usb_interface *interface);
  41. /* usb specific object needed to register this driver with the usb subsystem */
  42. static struct usb_driver cytherm_driver = {
  43. .owner = THIS_MODULE,
  44. .name = "cytherm",
  45. .probe = cytherm_probe,
  46. .disconnect = cytherm_disconnect,
  47. .id_table = id_table,
  48. };
  49. /* Vendor requests */
  50. /* They all operate on one byte at a time */
  51. #define PING 0x00
  52. #define READ_ROM 0x01 /* Reads form ROM, value = address */
  53. #define READ_RAM 0x02 /* Reads form RAM, value = address */
  54. #define WRITE_RAM 0x03 /* Write to RAM, value = address, index = data */
  55. #define READ_PORT 0x04 /* Reads from port, value = address */
  56. #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */
  57. /* Send a vendor command to device */
  58. static int vendor_command(struct usb_device *dev, unsigned char request,
  59. unsigned char value, unsigned char index,
  60. void *buf, int size)
  61. {
  62. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  63. request,
  64. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  65. value,
  66. index, buf, size,
  67. USB_CTRL_GET_TIMEOUT);
  68. }
  69. #define BRIGHTNESS 0x2c /* RAM location for brightness value */
  70. #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */
  71. static ssize_t show_brightness(struct device *dev, struct device_attribute *attr, char *buf)
  72. {
  73. struct usb_interface *intf = to_usb_interface(dev);
  74. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  75. return sprintf(buf, "%i", cytherm->brightness);
  76. }
  77. static ssize_t set_brightness(struct device *dev, struct device_attribute *attr, const char *buf,
  78. size_t count)
  79. {
  80. struct usb_interface *intf = to_usb_interface(dev);
  81. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  82. unsigned char *buffer;
  83. int retval;
  84. buffer = kmalloc(8, GFP_KERNEL);
  85. if (!buffer) {
  86. dev_err(&cytherm->udev->dev, "out of memory\n");
  87. return 0;
  88. }
  89. 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;
  94. /* Set brightness */
  95. retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS,
  96. cytherm->brightness, buffer, 8);
  97. if (retval)
  98. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  99. /* Inform µC that we have changed the brightness setting */
  100. retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS_SEM,
  101. 0x01, buffer, 8);
  102. if (retval)
  103. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  104. kfree(buffer);
  105. return count;
  106. }
  107. static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP,
  108. show_brightness, set_brightness);
  109. #define TEMP 0x33 /* RAM location for temperature */
  110. #define SIGN 0x34 /* RAM location for temperature sign */
  111. static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  112. {
  113. struct usb_interface *intf = to_usb_interface(dev);
  114. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  115. int retval;
  116. unsigned char *buffer;
  117. int temp, sign;
  118. buffer = kmalloc(8, GFP_KERNEL);
  119. if (!buffer) {
  120. dev_err(&cytherm->udev->dev, "out of memory\n");
  121. return 0;
  122. }
  123. /* read temperature */
  124. retval = vendor_command(cytherm->udev, READ_RAM, TEMP, 0, buffer, 8);
  125. if (retval)
  126. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  127. temp = buffer[1];
  128. /* read sign */
  129. retval = vendor_command(cytherm->udev, READ_RAM, SIGN, 0, buffer, 8);
  130. if (retval)
  131. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  132. sign = buffer[1];
  133. kfree(buffer);
  134. return sprintf(buf, "%c%i.%i", sign ? '-' : '+', temp >> 1,
  135. 5*(temp - ((temp >> 1) << 1)));
  136. }
  137. static ssize_t set_temp(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  138. {
  139. return count;
  140. }
  141. static DEVICE_ATTR(temp, S_IRUGO, show_temp, set_temp);
  142. #define BUTTON 0x7a
  143. static ssize_t show_button(struct device *dev, struct device_attribute *attr, char *buf)
  144. {
  145. struct usb_interface *intf = to_usb_interface(dev);
  146. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  147. int retval;
  148. unsigned char *buffer;
  149. buffer = kmalloc(8, GFP_KERNEL);
  150. if (!buffer) {
  151. dev_err(&cytherm->udev->dev, "out of memory\n");
  152. return 0;
  153. }
  154. /* check button */
  155. retval = vendor_command(cytherm->udev, READ_RAM, BUTTON, 0, buffer, 8);
  156. if (retval)
  157. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  158. retval = buffer[1];
  159. kfree(buffer);
  160. if (retval)
  161. return sprintf(buf, "1");
  162. else
  163. return sprintf(buf, "0");
  164. }
  165. static ssize_t set_button(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  166. {
  167. return count;
  168. }
  169. static DEVICE_ATTR(button, S_IRUGO, show_button, set_button);
  170. static ssize_t show_port0(struct device *dev, struct device_attribute *attr, char *buf)
  171. {
  172. struct usb_interface *intf = to_usb_interface(dev);
  173. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  174. int retval;
  175. unsigned char *buffer;
  176. buffer = kmalloc(8, GFP_KERNEL);
  177. if (!buffer) {
  178. dev_err(&cytherm->udev->dev, "out of memory\n");
  179. return 0;
  180. }
  181. retval = vendor_command(cytherm->udev, READ_PORT, 0, 0, buffer, 8);
  182. if (retval)
  183. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  184. retval = buffer[1];
  185. kfree(buffer);
  186. return sprintf(buf, "%d", retval);
  187. }
  188. static ssize_t set_port0(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  189. {
  190. struct usb_interface *intf = to_usb_interface(dev);
  191. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  192. unsigned char *buffer;
  193. int retval;
  194. int tmp;
  195. buffer = kmalloc(8, GFP_KERNEL);
  196. if (!buffer) {
  197. dev_err(&cytherm->udev->dev, "out of memory\n");
  198. return 0;
  199. }
  200. tmp = simple_strtoul(buf, NULL, 10);
  201. if (tmp > 0xFF)
  202. tmp = 0xFF;
  203. else if (tmp < 0)
  204. tmp = 0;
  205. retval = vendor_command(cytherm->udev, WRITE_PORT, 0,
  206. tmp, buffer, 8);
  207. if (retval)
  208. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  209. kfree(buffer);
  210. return count;
  211. }
  212. static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR | S_IWGRP, show_port0, set_port0);
  213. static ssize_t show_port1(struct device *dev, struct device_attribute *attr, char *buf)
  214. {
  215. struct usb_interface *intf = to_usb_interface(dev);
  216. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  217. int retval;
  218. unsigned char *buffer;
  219. buffer = kmalloc(8, GFP_KERNEL);
  220. if (!buffer) {
  221. dev_err(&cytherm->udev->dev, "out of memory\n");
  222. return 0;
  223. }
  224. retval = vendor_command(cytherm->udev, READ_PORT, 1, 0, buffer, 8);
  225. if (retval)
  226. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  227. retval = buffer[1];
  228. kfree(buffer);
  229. return sprintf(buf, "%d", retval);
  230. }
  231. static ssize_t set_port1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  232. {
  233. struct usb_interface *intf = to_usb_interface(dev);
  234. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  235. unsigned char *buffer;
  236. int retval;
  237. int tmp;
  238. buffer = kmalloc(8, GFP_KERNEL);
  239. if (!buffer) {
  240. dev_err(&cytherm->udev->dev, "out of memory\n");
  241. return 0;
  242. }
  243. tmp = simple_strtoul(buf, NULL, 10);
  244. if (tmp > 0xFF)
  245. tmp = 0xFF;
  246. else if (tmp < 0)
  247. tmp = 0;
  248. retval = vendor_command(cytherm->udev, WRITE_PORT, 1,
  249. tmp, buffer, 8);
  250. if (retval)
  251. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  252. kfree(buffer);
  253. return count;
  254. }
  255. static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR | S_IWGRP, show_port1, set_port1);
  256. static int cytherm_probe(struct usb_interface *interface,
  257. const struct usb_device_id *id)
  258. {
  259. struct usb_device *udev = interface_to_usbdev(interface);
  260. struct usb_cytherm *dev = NULL;
  261. int retval = -ENOMEM;
  262. dev = kmalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
  263. if (dev == NULL) {
  264. dev_err (&interface->dev, "Out of memory\n");
  265. goto error;
  266. }
  267. memset (dev, 0x00, sizeof (*dev));
  268. dev->udev = usb_get_dev(udev);
  269. usb_set_intfdata (interface, dev);
  270. dev->brightness = 0xFF;
  271. device_create_file(&interface->dev, &dev_attr_brightness);
  272. device_create_file(&interface->dev, &dev_attr_temp);
  273. device_create_file(&interface->dev, &dev_attr_button);
  274. device_create_file(&interface->dev, &dev_attr_port0);
  275. device_create_file(&interface->dev, &dev_attr_port1);
  276. dev_info (&interface->dev,
  277. "Cypress thermometer device now attached\n");
  278. return 0;
  279. error:
  280. kfree(dev);
  281. return retval;
  282. }
  283. static void cytherm_disconnect(struct usb_interface *interface)
  284. {
  285. struct usb_cytherm *dev;
  286. dev = usb_get_intfdata (interface);
  287. usb_set_intfdata (interface, NULL);
  288. device_remove_file(&interface->dev, &dev_attr_brightness);
  289. device_remove_file(&interface->dev, &dev_attr_temp);
  290. device_remove_file(&interface->dev, &dev_attr_button);
  291. device_remove_file(&interface->dev, &dev_attr_port0);
  292. device_remove_file(&interface->dev, &dev_attr_port1);
  293. usb_put_dev(dev->udev);
  294. kfree(dev);
  295. dev_info(&interface->dev, "Cypress thermometer now disconnected\n");
  296. }
  297. static int __init usb_cytherm_init(void)
  298. {
  299. int result;
  300. result = usb_register(&cytherm_driver);
  301. if (result)
  302. {
  303. err("usb_register failed. Error number %d", result);
  304. return result;
  305. }
  306. info(DRIVER_VERSION ":" DRIVER_DESC);
  307. return 0;
  308. }
  309. static void __exit usb_cytherm_exit(void)
  310. {
  311. usb_deregister(&cytherm_driver);
  312. }
  313. module_init (usb_cytherm_init);
  314. module_exit (usb_cytherm_exit);
  315. MODULE_AUTHOR(DRIVER_AUTHOR);
  316. MODULE_DESCRIPTION(DRIVER_DESC);
  317. MODULE_LICENSE("GPL");