cytherm.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* -*- linux-c -*-
  3. * Cypress USB Thermometer driver
  4. *
  5. * Copyright (c) 2004 Erik Rigtorp <erkki@linux.nu> <erik@rigtorp.com>
  6. *
  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
  9. * from Cypress.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #define DRIVER_AUTHOR "Erik Rigtorp"
  17. #define DRIVER_DESC "Cypress USB Thermometer driver"
  18. #define USB_SKEL_VENDOR_ID 0x04b4
  19. #define USB_SKEL_PRODUCT_ID 0x0002
  20. static const struct usb_device_id id_table[] = {
  21. { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
  22. { }
  23. };
  24. MODULE_DEVICE_TABLE (usb, id_table);
  25. /* Structure to hold all of our device specific stuff */
  26. struct usb_cytherm {
  27. struct usb_device *udev; /* save off the usb device pointer */
  28. struct usb_interface *interface; /* the interface for this device */
  29. int brightness;
  30. };
  31. /* Vendor requests */
  32. /* They all operate on one byte at a time */
  33. #define PING 0x00
  34. #define READ_ROM 0x01 /* Reads form ROM, value = address */
  35. #define READ_RAM 0x02 /* Reads form RAM, value = address */
  36. #define WRITE_RAM 0x03 /* Write to RAM, value = address, index = data */
  37. #define READ_PORT 0x04 /* Reads from port, value = address */
  38. #define WRITE_PORT 0x05 /* Write to port, value = address, index = data */
  39. /* Send a vendor command to device */
  40. static int vendor_command(struct usb_device *dev, unsigned char request,
  41. unsigned char value, unsigned char index,
  42. void *buf, int size)
  43. {
  44. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  45. request,
  46. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  47. value,
  48. index, buf, size,
  49. USB_CTRL_GET_TIMEOUT);
  50. }
  51. #define BRIGHTNESS 0x2c /* RAM location for brightness value */
  52. #define BRIGHTNESS_SEM 0x2b /* RAM location for brightness semaphore */
  53. static ssize_t brightness_show(struct device *dev, struct device_attribute *attr, char *buf)
  54. {
  55. struct usb_interface *intf = to_usb_interface(dev);
  56. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  57. return sprintf(buf, "%i", cytherm->brightness);
  58. }
  59. static ssize_t brightness_store(struct device *dev, struct device_attribute *attr, const char *buf,
  60. size_t count)
  61. {
  62. struct usb_interface *intf = to_usb_interface(dev);
  63. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  64. unsigned char *buffer;
  65. int retval;
  66. buffer = kmalloc(8, GFP_KERNEL);
  67. if (!buffer)
  68. return 0;
  69. cytherm->brightness = simple_strtoul(buf, NULL, 10);
  70. if (cytherm->brightness > 0xFF)
  71. cytherm->brightness = 0xFF;
  72. else if (cytherm->brightness < 0)
  73. cytherm->brightness = 0;
  74. /* Set brightness */
  75. retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS,
  76. cytherm->brightness, buffer, 8);
  77. if (retval)
  78. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  79. /* Inform µC that we have changed the brightness setting */
  80. retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS_SEM,
  81. 0x01, buffer, 8);
  82. if (retval)
  83. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  84. kfree(buffer);
  85. return count;
  86. }
  87. static DEVICE_ATTR_RW(brightness);
  88. #define TEMP 0x33 /* RAM location for temperature */
  89. #define SIGN 0x34 /* RAM location for temperature sign */
  90. static ssize_t temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  91. {
  92. struct usb_interface *intf = to_usb_interface(dev);
  93. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  94. int retval;
  95. unsigned char *buffer;
  96. int temp, sign;
  97. buffer = kmalloc(8, GFP_KERNEL);
  98. if (!buffer)
  99. return 0;
  100. /* read temperature */
  101. retval = vendor_command(cytherm->udev, READ_RAM, TEMP, 0, buffer, 8);
  102. if (retval)
  103. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  104. temp = buffer[1];
  105. /* read sign */
  106. retval = vendor_command(cytherm->udev, READ_RAM, SIGN, 0, buffer, 8);
  107. if (retval)
  108. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  109. sign = buffer[1];
  110. kfree(buffer);
  111. return sprintf(buf, "%c%i.%i", sign ? '-' : '+', temp >> 1,
  112. 5*(temp - ((temp >> 1) << 1)));
  113. }
  114. static DEVICE_ATTR_RO(temp);
  115. #define BUTTON 0x7a
  116. static ssize_t button_show(struct device *dev, struct device_attribute *attr, char *buf)
  117. {
  118. struct usb_interface *intf = to_usb_interface(dev);
  119. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  120. int retval;
  121. unsigned char *buffer;
  122. buffer = kmalloc(8, GFP_KERNEL);
  123. if (!buffer)
  124. return 0;
  125. /* check button */
  126. retval = vendor_command(cytherm->udev, READ_RAM, BUTTON, 0, buffer, 8);
  127. if (retval)
  128. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  129. retval = buffer[1];
  130. kfree(buffer);
  131. if (retval)
  132. return sprintf(buf, "1");
  133. else
  134. return sprintf(buf, "0");
  135. }
  136. static DEVICE_ATTR_RO(button);
  137. static ssize_t port0_show(struct device *dev, struct device_attribute *attr, char *buf)
  138. {
  139. struct usb_interface *intf = to_usb_interface(dev);
  140. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  141. int retval;
  142. unsigned char *buffer;
  143. buffer = kmalloc(8, GFP_KERNEL);
  144. if (!buffer)
  145. return 0;
  146. retval = vendor_command(cytherm->udev, READ_PORT, 0, 0, buffer, 8);
  147. if (retval)
  148. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  149. retval = buffer[1];
  150. kfree(buffer);
  151. return sprintf(buf, "%d", retval);
  152. }
  153. static ssize_t port0_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  154. {
  155. struct usb_interface *intf = to_usb_interface(dev);
  156. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  157. unsigned char *buffer;
  158. int retval;
  159. int tmp;
  160. buffer = kmalloc(8, GFP_KERNEL);
  161. if (!buffer)
  162. return 0;
  163. tmp = simple_strtoul(buf, NULL, 10);
  164. if (tmp > 0xFF)
  165. tmp = 0xFF;
  166. else if (tmp < 0)
  167. tmp = 0;
  168. retval = vendor_command(cytherm->udev, WRITE_PORT, 0,
  169. tmp, buffer, 8);
  170. if (retval)
  171. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  172. kfree(buffer);
  173. return count;
  174. }
  175. static DEVICE_ATTR_RW(port0);
  176. static ssize_t port1_show(struct device *dev, struct device_attribute *attr, char *buf)
  177. {
  178. struct usb_interface *intf = to_usb_interface(dev);
  179. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  180. int retval;
  181. unsigned char *buffer;
  182. buffer = kmalloc(8, GFP_KERNEL);
  183. if (!buffer)
  184. return 0;
  185. retval = vendor_command(cytherm->udev, READ_PORT, 1, 0, buffer, 8);
  186. if (retval)
  187. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  188. retval = buffer[1];
  189. kfree(buffer);
  190. return sprintf(buf, "%d", retval);
  191. }
  192. static ssize_t port1_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  193. {
  194. struct usb_interface *intf = to_usb_interface(dev);
  195. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  196. unsigned char *buffer;
  197. int retval;
  198. int tmp;
  199. buffer = kmalloc(8, GFP_KERNEL);
  200. if (!buffer)
  201. return 0;
  202. tmp = simple_strtoul(buf, NULL, 10);
  203. if (tmp > 0xFF)
  204. tmp = 0xFF;
  205. else if (tmp < 0)
  206. tmp = 0;
  207. retval = vendor_command(cytherm->udev, WRITE_PORT, 1,
  208. tmp, buffer, 8);
  209. if (retval)
  210. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  211. kfree(buffer);
  212. return count;
  213. }
  214. static DEVICE_ATTR_RW(port1);
  215. static struct attribute *cytherm_attrs[] = {
  216. &dev_attr_brightness.attr,
  217. &dev_attr_temp.attr,
  218. &dev_attr_button.attr,
  219. &dev_attr_port0.attr,
  220. &dev_attr_port1.attr,
  221. NULL,
  222. };
  223. ATTRIBUTE_GROUPS(cytherm);
  224. static int cytherm_probe(struct usb_interface *interface,
  225. const struct usb_device_id *id)
  226. {
  227. struct usb_device *udev = interface_to_usbdev(interface);
  228. struct usb_cytherm *dev = NULL;
  229. int retval = -ENOMEM;
  230. dev = kzalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
  231. if (!dev)
  232. goto error_mem;
  233. dev->udev = usb_get_dev(udev);
  234. usb_set_intfdata (interface, dev);
  235. dev->brightness = 0xFF;
  236. dev_info (&interface->dev,
  237. "Cypress thermometer device now attached\n");
  238. return 0;
  239. error_mem:
  240. return retval;
  241. }
  242. static void cytherm_disconnect(struct usb_interface *interface)
  243. {
  244. struct usb_cytherm *dev;
  245. dev = usb_get_intfdata (interface);
  246. /* first remove the files, then NULL the pointer */
  247. usb_set_intfdata (interface, NULL);
  248. usb_put_dev(dev->udev);
  249. kfree(dev);
  250. dev_info(&interface->dev, "Cypress thermometer now disconnected\n");
  251. }
  252. /* usb specific object needed to register this driver with the usb subsystem */
  253. static struct usb_driver cytherm_driver = {
  254. .name = "cytherm",
  255. .probe = cytherm_probe,
  256. .disconnect = cytherm_disconnect,
  257. .id_table = id_table,
  258. .dev_groups = cytherm_groups,
  259. };
  260. module_usb_driver(cytherm_driver);
  261. MODULE_AUTHOR(DRIVER_AUTHOR);
  262. MODULE_DESCRIPTION(DRIVER_DESC);
  263. MODULE_LICENSE("GPL");