PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/usb/misc/usbsevseg.c

https://github.com/Dabary/linux_gt-i9000
C | 462 lines | 354 code | 86 blank | 22 comment | 48 complexity | f1fa0c08593b7580fe43301b54f42e69 MD5 | raw file
  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 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_IWUGO | S_IRUGO, 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_IWUGO | S_IRUGO, 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_IWUGO | S_IRUGO,
  228. show_attr_decimals, set_attr_decimals);
  229. static ssize_t show_attr_textmode(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. struct usb_interface *intf = to_usb_interface(dev);
  233. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  234. int i;
  235. buf[0] = 0;
  236. for (i = 0; display_textmodes[i]; i++) {
  237. if (mydev->textmode == i) {
  238. strcat(buf, " [");
  239. strcat(buf, display_textmodes[i]);
  240. strcat(buf, "] ");
  241. } else {
  242. strcat(buf, " ");
  243. strcat(buf, display_textmodes[i]);
  244. strcat(buf, " ");
  245. }
  246. }
  247. strcat(buf, "\n");
  248. return strlen(buf);
  249. }
  250. static ssize_t set_attr_textmode(struct device *dev,
  251. struct device_attribute *attr, const char *buf, size_t count)
  252. {
  253. struct usb_interface *intf = to_usb_interface(dev);
  254. struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
  255. int i;
  256. for (i = 0; display_textmodes[i]; i++) {
  257. if (sysfs_streq(display_textmodes[i], buf)) {
  258. mydev->textmode = i;
  259. update_display_visual(mydev, GFP_KERNEL);
  260. return count;
  261. }
  262. }
  263. return -EINVAL;
  264. }
  265. static DEVICE_ATTR(textmode, S_IWUGO | S_IRUGO,
  266. show_attr_textmode, set_attr_textmode);
  267. MYDEV_ATTR_SIMPLE_UNSIGNED(powered, update_display_powered);
  268. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb, update_display_mode);
  269. MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb, update_display_mode);
  270. static struct attribute *dev_attrs[] = {
  271. &dev_attr_powered.attr,
  272. &dev_attr_text.attr,
  273. &dev_attr_textmode.attr,
  274. &dev_attr_decimals.attr,
  275. &dev_attr_mode_msb.attr,
  276. &dev_attr_mode_lsb.attr,
  277. NULL
  278. };
  279. static struct attribute_group dev_attr_grp = {
  280. .attrs = dev_attrs,
  281. };
  282. static int sevseg_probe(struct usb_interface *interface,
  283. const struct usb_device_id *id)
  284. {
  285. struct usb_device *udev = interface_to_usbdev(interface);
  286. struct usb_sevsegdev *mydev = NULL;
  287. int rc = -ENOMEM;
  288. mydev = kzalloc(sizeof(struct usb_sevsegdev), GFP_KERNEL);
  289. if (mydev == NULL) {
  290. dev_err(&interface->dev, "Out of memory\n");
  291. goto error_mem;
  292. }
  293. mydev->udev = usb_get_dev(udev);
  294. mydev->intf = interface;
  295. usb_set_intfdata(interface, mydev);
  296. /* PM */
  297. mydev->shadow_power = 1; /* currently active */
  298. mydev->has_interface_pm = 0; /* have not issued autopm_get */
  299. /*set defaults */
  300. mydev->textmode = 0x02; /* ascii mode */
  301. mydev->mode_msb = 0x06; /* 6 characters */
  302. mydev->mode_lsb = 0x3f; /* scanmode for 6 chars */
  303. rc = sysfs_create_group(&interface->dev.kobj, &dev_attr_grp);
  304. if (rc)
  305. goto error;
  306. dev_info(&interface->dev, "USB 7 Segment device now attached\n");
  307. return 0;
  308. error:
  309. usb_set_intfdata(interface, NULL);
  310. usb_put_dev(mydev->udev);
  311. kfree(mydev);
  312. error_mem:
  313. return rc;
  314. }
  315. static void sevseg_disconnect(struct usb_interface *interface)
  316. {
  317. struct usb_sevsegdev *mydev;
  318. mydev = usb_get_intfdata(interface);
  319. sysfs_remove_group(&interface->dev.kobj, &dev_attr_grp);
  320. usb_set_intfdata(interface, NULL);
  321. usb_put_dev(mydev->udev);
  322. kfree(mydev);
  323. dev_info(&interface->dev, "USB 7 Segment now disconnected\n");
  324. }
  325. static int sevseg_suspend(struct usb_interface *intf, pm_message_t message)
  326. {
  327. struct usb_sevsegdev *mydev;
  328. mydev = usb_get_intfdata(intf);
  329. mydev->shadow_power = 0;
  330. return 0;
  331. }
  332. static int sevseg_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 int sevseg_reset_resume(struct usb_interface *intf)
  342. {
  343. struct usb_sevsegdev *mydev;
  344. mydev = usb_get_intfdata(intf);
  345. mydev->shadow_power = 1;
  346. update_display_mode(mydev);
  347. update_display_visual(mydev, GFP_NOIO);
  348. return 0;
  349. }
  350. static struct usb_driver sevseg_driver = {
  351. .name = "usbsevseg",
  352. .probe = sevseg_probe,
  353. .disconnect = sevseg_disconnect,
  354. .suspend = sevseg_suspend,
  355. .resume = sevseg_resume,
  356. .reset_resume = sevseg_reset_resume,
  357. .id_table = id_table,
  358. .supports_autosuspend = 1,
  359. };
  360. static int __init usb_sevseg_init(void)
  361. {
  362. int rc = 0;
  363. rc = usb_register(&sevseg_driver);
  364. if (rc)
  365. err("usb_register failed. Error number %d", rc);
  366. return rc;
  367. }
  368. static void __exit usb_sevseg_exit(void)
  369. {
  370. usb_deregister(&sevseg_driver);
  371. }
  372. module_init(usb_sevseg_init);
  373. module_exit(usb_sevseg_exit);
  374. MODULE_AUTHOR(DRIVER_AUTHOR);
  375. MODULE_DESCRIPTION(DRIVER_DESC);
  376. MODULE_LICENSE("GPL");