cytherm.c 11 KB

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