usbsevseg.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * USB 7 Segment Driver
  3. *
  4. * Copyright (C) 2008 Harrison Metzger <harrisonmetz@gmail.com>
  5. * Based on usbled.c by Greg Kroah-Hartman (greg@kroah.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, version 2.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/string.h>
  18. #include <linux/usb.h>
  19. #define DRIVER_AUTHOR "Harrison Metzger <harrisonmetz@gmail.com>"
  20. #define DRIVER_DESC "USB 7 Segment Driver"
  21. #define VENDOR_ID 0x0fc5
  22. #define PRODUCT_ID 0x1227
  23. #define MAXLEN 6
  24. /* table of devices that work with this driver */
  25. static struct usb_device_id id_table[] = {
  26. { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
  27. { },
  28. };
  29. MODULE_DEVICE_TABLE(usb, id_table);
  30. /* the different text display modes the device is capable of */
  31. static char *display_textmodes[] = {"raw", "hex", "ascii", NULL};
  32. struct usb_sevsegdev {
  33. struct usb_device *udev;
  34. u8 powered;
  35. u8 mode_msb;
  36. u8 mode_lsb;
  37. u8 decimals[MAXLEN];
  38. u8 textmode;
  39. u8 text[MAXLEN];
  40. u16 textlength;
  41. };
  42. /* sysfs_streq can't replace this completely
  43. * If the device was in hex mode, and the user wanted a 0,
  44. * if str commands are used, we would assume the end of string
  45. * so mem commands are used.
  46. */
  47. inline size_t my_memlen(const char *buf, size_t count)
  48. {
  49. if (count > 0 && buf[count-1] == '\n')
  50. return count - 1;
  51. else
  52. return count;
  53. }
  54. static void update_display_powered(struct usb_sevsegdev *mydev)
  55. {
  56. int rc;
  57. rc = usb_control_msg(mydev->udev,
  58. usb_sndctrlpipe(mydev->udev, 0),
  59. 0x12,
  60. 0x48,
  61. (80 * 0x100) + 10, /* (power mode) */
  62. (0x00 * 0x100) + (mydev->powered ? 1 : 0),
  63. NULL,
  64. 0,
  65. 2000);
  66. if (rc < 0)
  67. dev_dbg(&mydev->udev->dev, "power retval = %d\n", rc);
  68. }
  69. static void update_display_mode(struct usb_sevsegdev *mydev)
  70. {
  71. int rc;
  72. rc = usb_control_msg(mydev->udev,
  73. usb_sndctrlpipe(mydev->udev, 0),
  74. 0x12,
  75. 0x48,
  76. (82 * 0x100) + 10, /* (set mode) */
  77. (mydev->mode_msb * 0x100) + mydev->mode_lsb,
  78. NULL,
  79. 0,
  80. 2000);
  81. if (rc < 0)
  82. dev_dbg(&mydev->udev->dev, "mode retval = %d\n", rc);
  83. }
  84. static void update_display_visual(struct usb_sevsegdev *mydev)
  85. {
  86. int rc;
  87. int i;
  88. unsigned char *buffer;
  89. u8 decimals = 0;
  90. buffer = kzalloc(MAXLEN, GFP_KERNEL);
  91. if (!buffer) {
  92. dev_err(&mydev->udev->dev, "out of memory\n");
  93. return;
  94. }
  95. /* The device is right to left, where as you write left to right */
  96. for (i = 0; i < mydev->textlength; i++)
  97. buffer[i] = mydev->text[mydev->textlength-1-i];
  98. rc = usb_control_msg(mydev->udev,
  99. usb_sndctrlpipe(mydev->udev, 0),
  100. 0x12,
  101. 0x48,
  102. (85 * 0x100) + 10, /* (write text) */
  103. (0 * 0x100) + mydev->textmode, /* mode */
  104. buffer,
  105. mydev->textlength,
  106. 2000);
  107. if (rc < 0)
  108. dev_dbg(&mydev->udev->dev, "write retval = %d\n", rc);
  109. kfree(buffer);
  110. /* The device is right to left, where as you write left to right */
  111. for (i = 0; i < sizeof(mydev->decimals); i++)
  112. decimals |= mydev->decimals[i] << i;
  113. rc = usb_control_msg(mydev->udev,
  114. usb_sndctrlpipe(mydev->udev, 0),
  115. 0x12,
  116. 0x48,
  117. (86 * 0x100) + 10, /* (set decimal) */
  118. (0 * 0x100) + decimals, /* decimals */
  119. NULL,
  120. 0,
  121. 2000);
  122. if (rc < 0)
  123. dev_dbg(&mydev->udev->dev, "decimal retval = %d\n", rc);
  124. }
  125. #define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn) \
  126. static ssize_t show_attr_##name(struct device *dev, \
  127. struct device_attribute *attr, char *buf) \
  128. { \
  129. struct usb_interface *intf = to_usb_interface(dev); \
  130. struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
  131. \
  132. return sprintf(buf, "%u\n", mydev->name); \
  133. } \
  134. \
  135. static ssize_t set_attr_##name(struct device *dev, \
  136. struct device_attribute *attr, const char *buf, size_t count) \
  137. { \
  138. struct usb_interface *intf = to_usb_interface(dev); \
  139. struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
  140. \
  141. mydev->name = simple_strtoul(buf, NULL, 10); \
  142. update_fcn(mydev); \
  143. \
  144. return count; \
  145. } \
  146. static DEVICE_ATTR(name, S_IWUGO | S_IRUGO, show_attr_##name, set_attr_##name);
  147. static ssize_t show_attr_text(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct usb_interface *intf = to_usb_interface(dev);
  151. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  152. return snprintf(buf, mydev->textlength, "%s\n", mydev->text);
  153. }
  154. static ssize_t set_attr_text(struct device *dev,
  155. struct device_attribute *attr, const char *buf, size_t count)
  156. {
  157. struct usb_interface *intf = to_usb_interface(dev);
  158. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  159. size_t end = my_memlen(buf, count);
  160. if (end > sizeof(mydev->text))
  161. return -EINVAL;
  162. memset(mydev->text, 0, sizeof(mydev->text));
  163. mydev->textlength = end;
  164. if (end > 0)
  165. memcpy(mydev->text, buf, end);
  166. update_display_visual(mydev);
  167. return count;
  168. }
  169. static DEVICE_ATTR(text, S_IWUGO | S_IRUGO, show_attr_text, set_attr_text);
  170. static ssize_t show_attr_decimals(struct device *dev,
  171. struct device_attribute *attr, char *buf)
  172. {
  173. struct usb_interface *intf = to_usb_interface(dev);
  174. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  175. int i;
  176. int pos;
  177. for (i = 0; i < sizeof(mydev->decimals); i++) {
  178. pos = sizeof(mydev->decimals) - 1 - i;
  179. if (mydev->decimals[i] == 0)
  180. buf[pos] = '0';
  181. else if (mydev->decimals[i] == 1)
  182. buf[pos] = '1';
  183. else
  184. buf[pos] = 'x';
  185. }
  186. buf[sizeof(mydev->decimals)] = '\n';
  187. return sizeof(mydev->decimals) + 1;
  188. }
  189. static ssize_t set_attr_decimals(struct device *dev,
  190. struct device_attribute *attr, const char *buf, size_t count)
  191. {
  192. struct usb_interface *intf = to_usb_interface(dev);
  193. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  194. size_t end = my_memlen(buf, count);
  195. int i;
  196. if (end > sizeof(mydev->decimals))
  197. return -EINVAL;
  198. for (i = 0; i < end; i++)
  199. if (buf[i] != '0' && buf[i] != '1')
  200. return -EINVAL;
  201. memset(mydev->decimals, 0, sizeof(mydev->decimals));
  202. for (i = 0; i < end; i++)
  203. if (buf[i] == '1')
  204. mydev->decimals[end-1-i] = 1;
  205. update_display_visual(mydev);
  206. return count;
  207. }
  208. static DEVICE_ATTR(decimals, S_IWUGO | S_IRUGO,
  209. show_attr_decimals, set_attr_decimals);
  210. static ssize_t show_attr_textmode(struct device *dev,
  211. struct device_attribute *attr, char *buf)
  212. {
  213. struct usb_interface *intf = to_usb_interface(dev);
  214. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  215. int i;
  216. buf[0] = 0;
  217. for (i = 0; display_textmodes[i]; i++) {
  218. if (mydev->textmode == i) {
  219. strcat(buf, " [");
  220. strcat(buf, display_textmodes[i]);
  221. strcat(buf, "] ");
  222. } else {
  223. strcat(buf, " ");
  224. strcat(buf, display_textmodes[i]);
  225. strcat(buf, " ");
  226. }
  227. }
  228. strcat(buf, "\n");
  229. return strlen(buf);
  230. }
  231. static ssize_t set_attr_textmode(struct device *dev,
  232. struct device_attribute *attr, const char *buf, size_t count)
  233. {
  234. struct usb_interface *intf = to_usb_interface(dev);
  235. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  236. int i;
  237. for (i = 0; display_textmodes[i]; i++) {
  238. if (sysfs_streq(display_textmodes[i], buf)) {
  239. mydev->textmode = i;
  240. update_display_visual(mydev);
  241. return count;
  242. }
  243. }
  244. return -EINVAL;
  245. }
  246. static DEVICE_ATTR(textmode, S_IWUGO | S_IRUGO,
  247. show_attr_textmode, set_attr_textmode);
  248. MYDEV_ATTR_SIMPLE_UNSIGNED(powered, update_display_powered);
  249. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb, update_display_mode);
  250. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb, update_display_mode);
  251. static struct attribute *dev_attrs[] = {
  252. &dev_attr_powered.attr,
  253. &dev_attr_text.attr,
  254. &dev_attr_textmode.attr,
  255. &dev_attr_decimals.attr,
  256. &dev_attr_mode_msb.attr,
  257. &dev_attr_mode_lsb.attr,
  258. NULL
  259. };
  260. static struct attribute_group dev_attr_grp = {
  261. .attrs = dev_attrs,
  262. };
  263. static int sevseg_probe(struct usb_interface *interface,
  264. const struct usb_device_id *id)
  265. {
  266. struct usb_device *udev = interface_to_usbdev(interface);
  267. struct usb_sevsegdev *mydev = NULL;
  268. int rc = -ENOMEM;
  269. mydev = kzalloc(sizeof(struct usb_sevsegdev), GFP_KERNEL);
  270. if (mydev == NULL) {
  271. dev_err(&interface->dev, "Out of memory\n");
  272. goto error_mem;
  273. }
  274. mydev->udev = usb_get_dev(udev);
  275. usb_set_intfdata(interface, mydev);
  276. /*set defaults */
  277. mydev->textmode = 0x02; /* ascii mode */
  278. mydev->mode_msb = 0x06; /* 6 characters */
  279. mydev->mode_lsb = 0x3f; /* scanmode for 6 chars */
  280. rc = sysfs_create_group(&interface->dev.kobj, &dev_attr_grp);
  281. if (rc)
  282. goto error;
  283. dev_info(&interface->dev, "USB 7 Segment device now attached\n");
  284. return 0;
  285. error:
  286. usb_set_intfdata(interface, NULL);
  287. usb_put_dev(mydev->udev);
  288. kfree(mydev);
  289. error_mem:
  290. return rc;
  291. }
  292. static void sevseg_disconnect(struct usb_interface *interface)
  293. {
  294. struct usb_sevsegdev *mydev;
  295. mydev = usb_get_intfdata(interface);
  296. sysfs_remove_group(&interface->dev.kobj, &dev_attr_grp);
  297. usb_set_intfdata(interface, NULL);
  298. usb_put_dev(mydev->udev);
  299. kfree(mydev);
  300. dev_info(&interface->dev, "USB 7 Segment now disconnected\n");
  301. }
  302. static struct usb_driver sevseg_driver = {
  303. .name = "usbsevseg",
  304. .probe = sevseg_probe,
  305. .disconnect = sevseg_disconnect,
  306. .id_table = id_table,
  307. };
  308. static int __init usb_sevseg_init(void)
  309. {
  310. int rc = 0;
  311. rc = usb_register(&sevseg_driver);
  312. if (rc)
  313. err("usb_register failed. Error number %d", rc);
  314. return rc;
  315. }
  316. static void __exit usb_sevseg_exit(void)
  317. {
  318. usb_deregister(&sevseg_driver);
  319. }
  320. module_init(usb_sevseg_init);
  321. module_exit(usb_sevseg_exit);
  322. MODULE_AUTHOR(DRIVER_AUTHOR);
  323. MODULE_DESCRIPTION(DRIVER_DESC);
  324. MODULE_LICENSE("GPL");