usbsevseg.c 10 KB

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