cytherm.c 10 KB

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