usbsevseg.c 10 KB

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