cytherm.c 11 KB

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