usbsevseg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. struct usb_interface *intf;
  35. u8 powered;
  36. u8 mode_msb;
  37. u8 mode_lsb;
  38. u8 decimals[MAXLEN];
  39. u8 textmode;
  40. u8 text[MAXLEN];
  41. u16 textlength;
  42. u8 shadow_power; /* for PM */
  43. };
  44. /* sysfs_streq can't replace this completely
  45. * If the device was in hex mode, and the user wanted a 0,
  46. * if str commands are used, we would assume the end of string
  47. * so mem commands are used.
  48. */
  49. inline size_t my_memlen(const char *buf, size_t count)
  50. {
  51. if (count > 0 && buf[count-1] == '\n')
  52. return count - 1;
  53. else
  54. return count;
  55. }
  56. static void update_display_powered(struct usb_sevsegdev *mydev)
  57. {
  58. int rc;
  59. if (!mydev->shadow_power && mydev->powered) {
  60. rc = usb_autopm_get_interface(mydev->intf);
  61. if (rc < 0)
  62. return;
  63. }
  64. rc = usb_control_msg(mydev->udev,
  65. usb_sndctrlpipe(mydev->udev, 0),
  66. 0x12,
  67. 0x48,
  68. (80 * 0x100) + 10, /* (power mode) */
  69. (0x00 * 0x100) + (mydev->powered ? 1 : 0),
  70. NULL,
  71. 0,
  72. 2000);
  73. if (rc < 0)
  74. dev_dbg(&mydev->udev->dev, "power retval = %d\n", rc);
  75. if (mydev->shadow_power && !mydev->powered)
  76. usb_autopm_put_interface(mydev->intf);
  77. }
  78. static void update_display_mode(struct usb_sevsegdev *mydev)
  79. {
  80. int rc;
  81. if(mydev->shadow_power != 1)
  82. return;
  83. rc = usb_control_msg(mydev->udev,
  84. usb_sndctrlpipe(mydev->udev, 0),
  85. 0x12,
  86. 0x48,
  87. (82 * 0x100) + 10, /* (set mode) */
  88. (mydev->mode_msb * 0x100) + mydev->mode_lsb,
  89. NULL,
  90. 0,
  91. 2000);
  92. if (rc < 0)
  93. dev_dbg(&mydev->udev->dev, "mode retval = %d\n", rc);
  94. }
  95. static void update_display_visual(struct usb_sevsegdev *mydev, gfp_t mf)
  96. {
  97. int rc;
  98. int i;
  99. unsigned char *buffer;
  100. u8 decimals = 0;
  101. if(mydev->shadow_power != 1)
  102. return;
  103. buffer = kzalloc(MAXLEN, mf);
  104. if (!buffer) {
  105. dev_err(&mydev->udev->dev, "out of memory\n");
  106. return;
  107. }
  108. /* The device is right to left, where as you write left to right */
  109. for (i = 0; i < mydev->textlength; i++)
  110. buffer[i] = mydev->text[mydev->textlength-1-i];
  111. rc = usb_control_msg(mydev->udev,
  112. usb_sndctrlpipe(mydev->udev, 0),
  113. 0x12,
  114. 0x48,
  115. (85 * 0x100) + 10, /* (write text) */
  116. (0 * 0x100) + mydev->textmode, /* mode */
  117. buffer,
  118. mydev->textlength,
  119. 2000);
  120. if (rc < 0)
  121. dev_dbg(&mydev->udev->dev, "write retval = %d\n", rc);
  122. kfree(buffer);
  123. /* The device is right to left, where as you write left to right */
  124. for (i = 0; i < sizeof(mydev->decimals); i++)
  125. decimals |= mydev->decimals[i] << i;
  126. rc = usb_control_msg(mydev->udev,
  127. usb_sndctrlpipe(mydev->udev, 0),
  128. 0x12,
  129. 0x48,
  130. (86 * 0x100) + 10, /* (set decimal) */
  131. (0 * 0x100) + decimals, /* decimals */
  132. NULL,
  133. 0,
  134. 2000);
  135. if (rc < 0)
  136. dev_dbg(&mydev->udev->dev, "decimal retval = %d\n", rc);
  137. }
  138. #define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn) \
  139. static ssize_t show_attr_##name(struct device *dev, \
  140. struct device_attribute *attr, char *buf) \
  141. { \
  142. struct usb_interface *intf = to_usb_interface(dev); \
  143. struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
  144. \
  145. return sprintf(buf, "%u\n", mydev->name); \
  146. } \
  147. \
  148. static ssize_t set_attr_##name(struct device *dev, \
  149. struct device_attribute *attr, const char *buf, size_t count) \
  150. { \
  151. struct usb_interface *intf = to_usb_interface(dev); \
  152. struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
  153. \
  154. mydev->name = simple_strtoul(buf, NULL, 10); \
  155. update_fcn(mydev); \
  156. \
  157. return count; \
  158. } \
  159. static DEVICE_ATTR(name, S_IWUGO | S_IRUGO, show_attr_##name, set_attr_##name);
  160. static ssize_t show_attr_text(struct device *dev,
  161. struct device_attribute *attr, char *buf)
  162. {
  163. struct usb_interface *intf = to_usb_interface(dev);
  164. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  165. return snprintf(buf, mydev->textlength, "%s\n", mydev->text);
  166. }
  167. static ssize_t set_attr_text(struct device *dev,
  168. struct device_attribute *attr, const char *buf, size_t count)
  169. {
  170. struct usb_interface *intf = to_usb_interface(dev);
  171. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  172. size_t end = my_memlen(buf, count);
  173. if (end > sizeof(mydev->text))
  174. return -EINVAL;
  175. memset(mydev->text, 0, sizeof(mydev->text));
  176. mydev->textlength = end;
  177. if (end > 0)
  178. memcpy(mydev->text, buf, end);
  179. update_display_visual(mydev, GFP_KERNEL);
  180. return count;
  181. }
  182. static DEVICE_ATTR(text, S_IWUGO | S_IRUGO, show_attr_text, set_attr_text);
  183. static ssize_t show_attr_decimals(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct usb_interface *intf = to_usb_interface(dev);
  187. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  188. int i;
  189. int pos;
  190. for (i = 0; i < sizeof(mydev->decimals); i++) {
  191. pos = sizeof(mydev->decimals) - 1 - i;
  192. if (mydev->decimals[i] == 0)
  193. buf[pos] = '0';
  194. else if (mydev->decimals[i] == 1)
  195. buf[pos] = '1';
  196. else
  197. buf[pos] = 'x';
  198. }
  199. buf[sizeof(mydev->decimals)] = '\n';
  200. return sizeof(mydev->decimals) + 1;
  201. }
  202. static ssize_t set_attr_decimals(struct device *dev,
  203. struct device_attribute *attr, const char *buf, size_t count)
  204. {
  205. struct usb_interface *intf = to_usb_interface(dev);
  206. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  207. size_t end = my_memlen(buf, count);
  208. int i;
  209. if (end > sizeof(mydev->decimals))
  210. return -EINVAL;
  211. for (i = 0; i < end; i++)
  212. if (buf[i] != '0' && buf[i] != '1')
  213. return -EINVAL;
  214. memset(mydev->decimals, 0, sizeof(mydev->decimals));
  215. for (i = 0; i < end; i++)
  216. if (buf[i] == '1')
  217. mydev->decimals[end-1-i] = 1;
  218. update_display_visual(mydev, GFP_KERNEL);
  219. return count;
  220. }
  221. static DEVICE_ATTR(decimals, S_IWUGO | S_IRUGO,
  222. show_attr_decimals, set_attr_decimals);
  223. static ssize_t show_attr_textmode(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. struct usb_interface *intf = to_usb_interface(dev);
  227. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  228. int i;
  229. buf[0] = 0;
  230. for (i = 0; display_textmodes[i]; i++) {
  231. if (mydev->textmode == i) {
  232. strcat(buf, " [");
  233. strcat(buf, display_textmodes[i]);
  234. strcat(buf, "] ");
  235. } else {
  236. strcat(buf, " ");
  237. strcat(buf, display_textmodes[i]);
  238. strcat(buf, " ");
  239. }
  240. }
  241. strcat(buf, "\n");
  242. return strlen(buf);
  243. }
  244. static ssize_t set_attr_textmode(struct device *dev,
  245. struct device_attribute *attr, const char *buf, size_t count)
  246. {
  247. struct usb_interface *intf = to_usb_interface(dev);
  248. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  249. int i;
  250. for (i = 0; display_textmodes[i]; i++) {
  251. if (sysfs_streq(display_textmodes[i], buf)) {
  252. mydev->textmode = i;
  253. update_display_visual(mydev, GFP_KERNEL);
  254. return count;
  255. }
  256. }
  257. return -EINVAL;
  258. }
  259. static DEVICE_ATTR(textmode, S_IWUGO | S_IRUGO,
  260. show_attr_textmode, set_attr_textmode);
  261. MYDEV_ATTR_SIMPLE_UNSIGNED(powered, update_display_powered);
  262. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb, update_display_mode);
  263. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb, update_display_mode);
  264. static struct attribute *dev_attrs[] = {
  265. &dev_attr_powered.attr,
  266. &dev_attr_text.attr,
  267. &dev_attr_textmode.attr,
  268. &dev_attr_decimals.attr,
  269. &dev_attr_mode_msb.attr,
  270. &dev_attr_mode_lsb.attr,
  271. NULL
  272. };
  273. static struct attribute_group dev_attr_grp = {
  274. .attrs = dev_attrs,
  275. };
  276. static int sevseg_probe(struct usb_interface *interface,
  277. const struct usb_device_id *id)
  278. {
  279. struct usb_device *udev = interface_to_usbdev(interface);
  280. struct usb_sevsegdev *mydev = NULL;
  281. int rc = -ENOMEM;
  282. mydev = kzalloc(sizeof(struct usb_sevsegdev), GFP_KERNEL);
  283. if (mydev == NULL) {
  284. dev_err(&interface->dev, "Out of memory\n");
  285. goto error_mem;
  286. }
  287. mydev->udev = usb_get_dev(udev);
  288. mydev->intf = interface;
  289. usb_set_intfdata(interface, mydev);
  290. /*set defaults */
  291. mydev->textmode = 0x02; /* ascii mode */
  292. mydev->mode_msb = 0x06; /* 6 characters */
  293. mydev->mode_lsb = 0x3f; /* scanmode for 6 chars */
  294. rc = sysfs_create_group(&interface->dev.kobj, &dev_attr_grp);
  295. if (rc)
  296. goto error;
  297. dev_info(&interface->dev, "USB 7 Segment device now attached\n");
  298. return 0;
  299. error:
  300. usb_set_intfdata(interface, NULL);
  301. usb_put_dev(mydev->udev);
  302. kfree(mydev);
  303. error_mem:
  304. return rc;
  305. }
  306. static void sevseg_disconnect(struct usb_interface *interface)
  307. {
  308. struct usb_sevsegdev *mydev;
  309. mydev = usb_get_intfdata(interface);
  310. sysfs_remove_group(&interface->dev.kobj, &dev_attr_grp);
  311. usb_set_intfdata(interface, NULL);
  312. usb_put_dev(mydev->udev);
  313. kfree(mydev);
  314. dev_info(&interface->dev, "USB 7 Segment now disconnected\n");
  315. }
  316. static int sevseg_suspend(struct usb_interface *intf, pm_message_t message)
  317. {
  318. struct usb_sevsegdev *mydev;
  319. mydev = usb_get_intfdata(intf);
  320. mydev->shadow_power = 0;
  321. return 0;
  322. }
  323. static int sevseg_resume(struct usb_interface *intf)
  324. {
  325. struct usb_sevsegdev *mydev;
  326. mydev = usb_get_intfdata(intf);
  327. mydev->shadow_power = 1;
  328. update_display_mode(mydev);
  329. update_display_visual(mydev, GFP_NOIO);
  330. return 0;
  331. }
  332. static int sevseg_reset_resume(struct usb_interface *intf)
  333. {
  334. struct usb_sevsegdev *mydev;
  335. mydev = usb_get_intfdata(intf);
  336. mydev->shadow_power = 1;
  337. update_display_mode(mydev);
  338. update_display_visual(mydev, GFP_NOIO);
  339. return 0;
  340. }
  341. static struct usb_driver sevseg_driver = {
  342. .name = "usbsevseg",
  343. .probe = sevseg_probe,
  344. .disconnect = sevseg_disconnect,
  345. .suspend = sevseg_suspend,
  346. .resume = sevseg_resume,
  347. .reset_resume = sevseg_reset_resume,
  348. .id_table = id_table,
  349. .supports_autosuspend = 1,
  350. };
  351. static int __init usb_sevseg_init(void)
  352. {
  353. int rc = 0;
  354. rc = usb_register(&sevseg_driver);
  355. if (rc)
  356. err("usb_register failed. Error number %d", rc);
  357. return rc;
  358. }
  359. static void __exit usb_sevseg_exit(void)
  360. {
  361. usb_deregister(&sevseg_driver);
  362. }
  363. module_init(usb_sevseg_init);
  364. module_exit(usb_sevseg_exit);
  365. MODULE_AUTHOR(DRIVER_AUTHOR);
  366. MODULE_DESCRIPTION(DRIVER_DESC);
  367. MODULE_LICENSE("GPL");