cytherm.c 9.5 KB

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