usbsevseg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 const char *display_textmodes[] = {"raw", "hex", "ascii"};
  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. return;
  111. /* The device is right to left, where as you write left to right */
  112. for (i = 0; i < mydev->textlength; i++)
  113. buffer[i] = mydev->text[mydev->textlength-1-i];
  114. rc = usb_control_msg(mydev->udev,
  115. usb_sndctrlpipe(mydev->udev, 0),
  116. 0x12,
  117. 0x48,
  118. (85 * 0x100) + 10, /* (write text) */
  119. (0 * 0x100) + mydev->textmode, /* mode */
  120. buffer,
  121. mydev->textlength,
  122. 2000);
  123. if (rc < 0)
  124. dev_dbg(&mydev->udev->dev, "write retval = %d\n", rc);
  125. kfree(buffer);
  126. /* The device is right to left, where as you write left to right */
  127. for (i = 0; i < sizeof(mydev->decimals); i++)
  128. decimals |= mydev->decimals[i] << i;
  129. rc = usb_control_msg(mydev->udev,
  130. usb_sndctrlpipe(mydev->udev, 0),
  131. 0x12,
  132. 0x48,
  133. (86 * 0x100) + 10, /* (set decimal) */
  134. (0 * 0x100) + decimals, /* decimals */
  135. NULL,
  136. 0,
  137. 2000);
  138. if (rc < 0)
  139. dev_dbg(&mydev->udev->dev, "decimal retval = %d\n", rc);
  140. }
  141. #define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn) \
  142. static ssize_t show_attr_##name(struct device *dev, \
  143. struct device_attribute *attr, char *buf) \
  144. { \
  145. struct usb_interface *intf = to_usb_interface(dev); \
  146. struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
  147. \
  148. return sprintf(buf, "%u\n", mydev->name); \
  149. } \
  150. \
  151. static ssize_t set_attr_##name(struct device *dev, \
  152. struct device_attribute *attr, const char *buf, size_t count) \
  153. { \
  154. struct usb_interface *intf = to_usb_interface(dev); \
  155. struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
  156. \
  157. mydev->name = simple_strtoul(buf, NULL, 10); \
  158. update_fcn(mydev); \
  159. \
  160. return count; \
  161. } \
  162. static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show_attr_##name, set_attr_##name);
  163. static ssize_t show_attr_text(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. struct usb_interface *intf = to_usb_interface(dev);
  167. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  168. return snprintf(buf, mydev->textlength, "%s\n", mydev->text);
  169. }
  170. static ssize_t set_attr_text(struct device *dev,
  171. struct device_attribute *attr, const char *buf, size_t count)
  172. {
  173. struct usb_interface *intf = to_usb_interface(dev);
  174. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  175. size_t end = my_memlen(buf, count);
  176. if (end > sizeof(mydev->text))
  177. return -EINVAL;
  178. memset(mydev->text, 0, sizeof(mydev->text));
  179. mydev->textlength = end;
  180. if (end > 0)
  181. memcpy(mydev->text, buf, end);
  182. update_display_visual(mydev, GFP_KERNEL);
  183. return count;
  184. }
  185. static DEVICE_ATTR(text, S_IRUGO | S_IWUSR, show_attr_text, set_attr_text);
  186. static ssize_t show_attr_decimals(struct device *dev,
  187. struct device_attribute *attr, char *buf)
  188. {
  189. struct usb_interface *intf = to_usb_interface(dev);
  190. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  191. int i;
  192. int pos;
  193. for (i = 0; i < sizeof(mydev->decimals); i++) {
  194. pos = sizeof(mydev->decimals) - 1 - i;
  195. if (mydev->decimals[i] == 0)
  196. buf[pos] = '0';
  197. else if (mydev->decimals[i] == 1)
  198. buf[pos] = '1';
  199. else
  200. buf[pos] = 'x';
  201. }
  202. buf[sizeof(mydev->decimals)] = '\n';
  203. return sizeof(mydev->decimals) + 1;
  204. }
  205. static ssize_t set_attr_decimals(struct device *dev,
  206. struct device_attribute *attr, const char *buf, size_t count)
  207. {
  208. struct usb_interface *intf = to_usb_interface(dev);
  209. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  210. size_t end = my_memlen(buf, count);
  211. int i;
  212. if (end > sizeof(mydev->decimals))
  213. return -EINVAL;
  214. for (i = 0; i < end; i++)
  215. if (buf[i] != '0' && buf[i] != '1')
  216. return -EINVAL;
  217. memset(mydev->decimals, 0, sizeof(mydev->decimals));
  218. for (i = 0; i < end; i++)
  219. if (buf[i] == '1')
  220. mydev->decimals[end-1-i] = 1;
  221. update_display_visual(mydev, GFP_KERNEL);
  222. return count;
  223. }
  224. static DEVICE_ATTR(decimals, S_IRUGO | S_IWUSR, show_attr_decimals, set_attr_decimals);
  225. static ssize_t show_attr_textmode(struct device *dev,
  226. struct device_attribute *attr, char *buf)
  227. {
  228. struct usb_interface *intf = to_usb_interface(dev);
  229. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  230. int i;
  231. buf[0] = 0;
  232. for (i = 0; i < ARRAY_SIZE(display_textmodes); i++) {
  233. if (mydev->textmode == i) {
  234. strcat(buf, " [");
  235. strcat(buf, display_textmodes[i]);
  236. strcat(buf, "] ");
  237. } else {
  238. strcat(buf, " ");
  239. strcat(buf, display_textmodes[i]);
  240. strcat(buf, " ");
  241. }
  242. }
  243. strcat(buf, "\n");
  244. return strlen(buf);
  245. }
  246. static ssize_t set_attr_textmode(struct device *dev,
  247. struct device_attribute *attr, const char *buf, size_t count)
  248. {
  249. struct usb_interface *intf = to_usb_interface(dev);
  250. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  251. int i;
  252. i = sysfs_match_string(display_textmodes, buf);
  253. if (i < 0)
  254. return i;
  255. mydev->textmode = i;
  256. update_display_visual(mydev, GFP_KERNEL);
  257. return count;
  258. }
  259. static DEVICE_ATTR(textmode, S_IRUGO | S_IWUSR, show_attr_textmode, set_attr_textmode);
  260. MYDEV_ATTR_SIMPLE_UNSIGNED(powered, update_display_powered);
  261. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb, update_display_mode);
  262. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb, update_display_mode);
  263. static struct attribute *dev_attrs[] = {
  264. &dev_attr_powered.attr,
  265. &dev_attr_text.attr,
  266. &dev_attr_textmode.attr,
  267. &dev_attr_decimals.attr,
  268. &dev_attr_mode_msb.attr,
  269. &dev_attr_mode_lsb.attr,
  270. NULL
  271. };
  272. static const struct attribute_group dev_attr_grp = {
  273. .attrs = dev_attrs,
  274. };
  275. static int sevseg_probe(struct usb_interface *interface,
  276. const struct usb_device_id *id)
  277. {
  278. struct usb_device *udev = interface_to_usbdev(interface);
  279. struct usb_sevsegdev *mydev = NULL;
  280. int rc = -ENOMEM;
  281. mydev = kzalloc(sizeof(struct usb_sevsegdev), GFP_KERNEL);
  282. if (!mydev)
  283. goto error_mem;
  284. mydev->udev = usb_get_dev(udev);
  285. mydev->intf = interface;
  286. usb_set_intfdata(interface, mydev);
  287. /* PM */
  288. mydev->shadow_power = 1; /* currently active */
  289. mydev->has_interface_pm = 0; /* have not issued autopm_get */
  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. module_usb_driver(sevseg_driver);
  352. MODULE_AUTHOR(DRIVER_AUTHOR);
  353. MODULE_DESCRIPTION(DRIVER_DESC);
  354. MODULE_LICENSE("GPL");