cytherm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. return 0;
  85. cytherm->brightness = simple_strtoul(buf, NULL, 10);
  86. if (cytherm->brightness > 0xFF)
  87. cytherm->brightness = 0xFF;
  88. else if (cytherm->brightness < 0)
  89. cytherm->brightness = 0;
  90. /* Set brightness */
  91. retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS,
  92. cytherm->brightness, buffer, 8);
  93. if (retval)
  94. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  95. /* Inform µC that we have changed the brightness setting */
  96. retval = vendor_command(cytherm->udev, WRITE_RAM, BRIGHTNESS_SEM,
  97. 0x01, buffer, 8);
  98. if (retval)
  99. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  100. kfree(buffer);
  101. return count;
  102. }
  103. static DEVICE_ATTR(brightness, S_IRUGO | S_IWUSR | S_IWGRP,
  104. show_brightness, set_brightness);
  105. #define TEMP 0x33 /* RAM location for temperature */
  106. #define SIGN 0x34 /* RAM location for temperature sign */
  107. static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  108. {
  109. struct usb_interface *intf = to_usb_interface(dev);
  110. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  111. int retval;
  112. unsigned char *buffer;
  113. int temp, sign;
  114. buffer = kmalloc(8, GFP_KERNEL);
  115. if (!buffer)
  116. return 0;
  117. /* read temperature */
  118. retval = vendor_command(cytherm->udev, READ_RAM, TEMP, 0, buffer, 8);
  119. if (retval)
  120. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  121. temp = buffer[1];
  122. /* read sign */
  123. retval = vendor_command(cytherm->udev, READ_RAM, SIGN, 0, buffer, 8);
  124. if (retval)
  125. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  126. sign = buffer[1];
  127. kfree(buffer);
  128. return sprintf(buf, "%c%i.%i", sign ? '-' : '+', temp >> 1,
  129. 5*(temp - ((temp >> 1) << 1)));
  130. }
  131. static ssize_t set_temp(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  132. {
  133. return count;
  134. }
  135. static DEVICE_ATTR(temp, S_IRUGO, show_temp, set_temp);
  136. #define BUTTON 0x7a
  137. static ssize_t show_button(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. /* check button */
  147. retval = vendor_command(cytherm->udev, READ_RAM, BUTTON, 0, buffer, 8);
  148. if (retval)
  149. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  150. retval = buffer[1];
  151. kfree(buffer);
  152. if (retval)
  153. return sprintf(buf, "1");
  154. else
  155. return sprintf(buf, "0");
  156. }
  157. static ssize_t set_button(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  158. {
  159. return count;
  160. }
  161. static DEVICE_ATTR(button, S_IRUGO, show_button, set_button);
  162. static ssize_t show_port0(struct device *dev, struct device_attribute *attr, char *buf)
  163. {
  164. struct usb_interface *intf = to_usb_interface(dev);
  165. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  166. int retval;
  167. unsigned char *buffer;
  168. buffer = kmalloc(8, GFP_KERNEL);
  169. if (!buffer)
  170. return 0;
  171. retval = vendor_command(cytherm->udev, READ_PORT, 0, 0, buffer, 8);
  172. if (retval)
  173. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  174. retval = buffer[1];
  175. kfree(buffer);
  176. return sprintf(buf, "%d", retval);
  177. }
  178. static ssize_t set_port0(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  179. {
  180. struct usb_interface *intf = to_usb_interface(dev);
  181. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  182. unsigned char *buffer;
  183. int retval;
  184. int tmp;
  185. buffer = kmalloc(8, GFP_KERNEL);
  186. if (!buffer)
  187. return 0;
  188. tmp = simple_strtoul(buf, NULL, 10);
  189. if (tmp > 0xFF)
  190. tmp = 0xFF;
  191. else if (tmp < 0)
  192. tmp = 0;
  193. retval = vendor_command(cytherm->udev, WRITE_PORT, 0,
  194. tmp, buffer, 8);
  195. if (retval)
  196. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  197. kfree(buffer);
  198. return count;
  199. }
  200. static DEVICE_ATTR(port0, S_IRUGO | S_IWUSR | S_IWGRP, show_port0, set_port0);
  201. static ssize_t show_port1(struct device *dev, struct device_attribute *attr, char *buf)
  202. {
  203. struct usb_interface *intf = to_usb_interface(dev);
  204. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  205. int retval;
  206. unsigned char *buffer;
  207. buffer = kmalloc(8, GFP_KERNEL);
  208. if (!buffer)
  209. return 0;
  210. retval = vendor_command(cytherm->udev, READ_PORT, 1, 0, buffer, 8);
  211. if (retval)
  212. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  213. retval = buffer[1];
  214. kfree(buffer);
  215. return sprintf(buf, "%d", retval);
  216. }
  217. static ssize_t set_port1(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  218. {
  219. struct usb_interface *intf = to_usb_interface(dev);
  220. struct usb_cytherm *cytherm = usb_get_intfdata(intf);
  221. unsigned char *buffer;
  222. int retval;
  223. int tmp;
  224. buffer = kmalloc(8, GFP_KERNEL);
  225. if (!buffer)
  226. return 0;
  227. tmp = simple_strtoul(buf, NULL, 10);
  228. if (tmp > 0xFF)
  229. tmp = 0xFF;
  230. else if (tmp < 0)
  231. tmp = 0;
  232. retval = vendor_command(cytherm->udev, WRITE_PORT, 1,
  233. tmp, buffer, 8);
  234. if (retval)
  235. dev_dbg(&cytherm->udev->dev, "retval = %d\n", retval);
  236. kfree(buffer);
  237. return count;
  238. }
  239. static DEVICE_ATTR(port1, S_IRUGO | S_IWUSR | S_IWGRP, show_port1, set_port1);
  240. static int cytherm_probe(struct usb_interface *interface,
  241. const struct usb_device_id *id)
  242. {
  243. struct usb_device *udev = interface_to_usbdev(interface);
  244. struct usb_cytherm *dev = NULL;
  245. int retval = -ENOMEM;
  246. dev = kzalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
  247. if (!dev)
  248. goto error_mem;
  249. dev->udev = usb_get_dev(udev);
  250. usb_set_intfdata (interface, dev);
  251. dev->brightness = 0xFF;
  252. retval = device_create_file(&interface->dev, &dev_attr_brightness);
  253. if (retval)
  254. goto error;
  255. retval = device_create_file(&interface->dev, &dev_attr_temp);
  256. if (retval)
  257. goto error;
  258. retval = device_create_file(&interface->dev, &dev_attr_button);
  259. if (retval)
  260. goto error;
  261. retval = device_create_file(&interface->dev, &dev_attr_port0);
  262. if (retval)
  263. goto error;
  264. retval = device_create_file(&interface->dev, &dev_attr_port1);
  265. if (retval)
  266. goto error;
  267. dev_info (&interface->dev,
  268. "Cypress thermometer device now attached\n");
  269. return 0;
  270. error:
  271. device_remove_file(&interface->dev, &dev_attr_brightness);
  272. device_remove_file(&interface->dev, &dev_attr_temp);
  273. device_remove_file(&interface->dev, &dev_attr_button);
  274. device_remove_file(&interface->dev, &dev_attr_port0);
  275. device_remove_file(&interface->dev, &dev_attr_port1);
  276. usb_set_intfdata (interface, NULL);
  277. usb_put_dev(dev->udev);
  278. kfree(dev);
  279. error_mem:
  280. return retval;
  281. }
  282. static void cytherm_disconnect(struct usb_interface *interface)
  283. {
  284. struct usb_cytherm *dev;
  285. dev = usb_get_intfdata (interface);
  286. device_remove_file(&interface->dev, &dev_attr_brightness);
  287. device_remove_file(&interface->dev, &dev_attr_temp);
  288. device_remove_file(&interface->dev, &dev_attr_button);
  289. device_remove_file(&interface->dev, &dev_attr_port0);
  290. device_remove_file(&interface->dev, &dev_attr_port1);
  291. /* first remove the files, then NULL the pointer */
  292. usb_set_intfdata (interface, NULL);
  293. usb_put_dev(dev->udev);
  294. kfree(dev);
  295. dev_info(&interface->dev, "Cypress thermometer now disconnected\n");
  296. }
  297. module_usb_driver(cytherm_driver);
  298. MODULE_AUTHOR(DRIVER_AUTHOR);
  299. MODULE_DESCRIPTION(DRIVER_DESC);
  300. MODULE_LICENSE("GPL");