usbsevseg.c 9.9 KB

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