PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/staging/usbip/userspace/src/utils.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 255 lines | 183 code | 65 blank | 7 comment | 29 complexity | d0781e7accd55909275f9a441c38bed5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. *
  3. * Copyright (C) 2005-2007 Takahiro Hirofuchi
  4. */
  5. #include "utils.h"
  6. int read_integer(char *path)
  7. {
  8. char buff[100];
  9. int fd;
  10. int ret = 0;
  11. bzero(buff, sizeof(buff));
  12. fd = open(path, O_RDONLY);
  13. if (fd < 0)
  14. return -1;
  15. ret = read(fd, buff, sizeof(buff));
  16. if (ret < 0) {
  17. close(fd);
  18. return -1;
  19. }
  20. sscanf(buff, "%d", &ret);
  21. close(fd);
  22. return ret;
  23. }
  24. int read_string(char *path, char *string, size_t len)
  25. {
  26. int fd;
  27. int ret = 0;
  28. char *p;
  29. bzero(string, len);
  30. fd = open(path, O_RDONLY);
  31. if (fd < 0) {
  32. string = NULL;
  33. return -1;
  34. }
  35. ret = read(fd, string, len-1);
  36. if (ret < 0) {
  37. string = NULL;
  38. close(fd);
  39. return -1;
  40. }
  41. p = strchr(string, '\n');
  42. *p = '\0';
  43. close(fd);
  44. return 0;
  45. }
  46. int write_integer(char *path, int value)
  47. {
  48. int fd;
  49. int ret;
  50. char buff[100];
  51. snprintf(buff, sizeof(buff), "%d", value);
  52. fd = open(path, O_WRONLY);
  53. if (fd < 0)
  54. return -1;
  55. ret = write(fd, buff, strlen(buff));
  56. if (ret < 0) {
  57. close(fd);
  58. return -1;
  59. }
  60. close(fd);
  61. return 0;
  62. }
  63. int read_bConfigurationValue(char *busid)
  64. {
  65. char path[PATH_MAX];
  66. snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
  67. return read_integer(path);
  68. }
  69. int write_bConfigurationValue(char *busid, int config)
  70. {
  71. char path[PATH_MAX];
  72. snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
  73. return write_integer(path, config);
  74. }
  75. int read_bNumInterfaces(char *busid)
  76. {
  77. char path[PATH_MAX];
  78. snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bNumInterfaces", busid);
  79. return read_integer(path);
  80. }
  81. int read_bDeviceClass(char *busid)
  82. {
  83. char path[PATH_MAX];
  84. snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bDeviceClass", busid);
  85. return read_integer(path);
  86. }
  87. int getdriver(char *busid, int conf, int infnum, char *driver, size_t len)
  88. {
  89. char path[PATH_MAX];
  90. char linkto[PATH_MAX];
  91. int ret;
  92. snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s:%d.%d/driver", busid, conf, infnum);
  93. /* readlink does not add NULL */
  94. bzero(linkto, sizeof(linkto));
  95. ret = readlink(path, linkto, sizeof(linkto)-1);
  96. if (ret < 0) {
  97. strncpy(driver, "none", len);
  98. return -1;
  99. } else {
  100. strncpy(driver, basename(linkto), len);
  101. return 0;
  102. }
  103. }
  104. int getdevicename(char *busid, char *name, size_t len)
  105. {
  106. char path[PATH_MAX];
  107. char idProduct[10], idVendor[10];
  108. snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idVendor", busid);
  109. read_string(path, idVendor, sizeof(idVendor));
  110. snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idProduct", busid);
  111. read_string(path, idProduct, sizeof(idProduct));
  112. if (!idVendor[0] || !idProduct[0])
  113. return -1;
  114. snprintf(name, len, "%s:%s", idVendor, idProduct);
  115. return 0;
  116. }
  117. #define MAXLINE 100
  118. /* if this cannot read a whole line, return -1 */
  119. int readline(int sockfd, char *buff, int bufflen)
  120. {
  121. int ret;
  122. char c;
  123. int index = 0;
  124. while (index < bufflen) {
  125. ret = read(sockfd, &c, sizeof(c));
  126. if (ret < 0 && errno == EINTR)
  127. continue;
  128. if (ret <= 0) {
  129. return -1;
  130. }
  131. buff[index] = c;
  132. if ( index > 0 && buff[index-1] == '\r' && buff[index] == '\n') {
  133. /* end of line */
  134. buff[index-1] = '\0'; /* get rid of delimitor */
  135. return index;
  136. } else
  137. index++;
  138. }
  139. return -1;
  140. }
  141. #if 0
  142. int writeline(int sockfd, char *str, int strlen)
  143. {
  144. int ret;
  145. int index = 0;
  146. int len;
  147. char buff[MAXLINE];
  148. if (strlen + 3 > MAXLINE)
  149. return -1;
  150. strncpy(buff, str, strlen);
  151. buff[strlen+1] = '\r';
  152. buff[strlen+2] = '\n';
  153. buff[strlen+3] = '\0';
  154. len = strlen + 3;
  155. while (len > 0) {
  156. ret = write(sockfd, buff+index, len);
  157. if (ret <= 0) {
  158. return -1;
  159. }
  160. len -= ret;
  161. index += ret;
  162. }
  163. return index;
  164. }
  165. #endif
  166. int writeline(int sockfd, char *str, int strlen)
  167. {
  168. int ret;
  169. int index = 0;
  170. int len;
  171. char buff[MAXLINE];
  172. len = strnlen(str, strlen);
  173. if (strlen + 2 > MAXLINE)
  174. return -1;
  175. memcpy(buff, str, strlen);
  176. buff[strlen] = '\r';
  177. buff[strlen+1] = '\n'; /* strlen+1 <= MAXLINE-1 */
  178. len = strlen + 2;
  179. while (len > 0) {
  180. ret = write(sockfd, buff+index, len);
  181. if (ret < 0 && errno == EINTR)
  182. continue;
  183. if (ret <= 0) {
  184. return -1;
  185. }
  186. len -= ret;
  187. index += ret;
  188. }
  189. return index;
  190. }