gtk2/linux blmc config tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
6.3KB

  1. /***************************************************************************
  2. * Copyright (C) 09/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License, *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <dirent.h>
  26. #include <fcntl.h>
  27. #include <errno.h>
  28. #include <sys/ioctl.h>
  29. #include <linux/i2c.h>
  30. #include <linux/i2c-dev.h>
  31. #include "gtk2-gui.h"
  32. #include "i2c.h"
  33. /* TWIBOOT commands */
  34. #define CMD_GET_INFO 0x10
  35. #define CMD_GET_SIGNATURE 0x11
  36. #define CMD_WRITE_FLASH 0x12
  37. #define CMD_READ_FLASH 0x13
  38. #define CMD_WRITE_EEPROM 0x14
  39. #define CMD_READ_EEPROM 0x15
  40. #define CMD_BOOT_APPLICATION 0x1F
  41. #define COOKIE 0x4711
  42. #define WRITE_COOKIE COOKIE
  43. #define FLASH_BLKSIZE 0x40
  44. /* blctrl commands */
  45. //#define CMD_GET_INFO 0x10
  46. #define CMD_SET_PWM 0x21
  47. #define CMD_GET_STATUS 0x22
  48. #define CMD_SET_PARAM 0x23
  49. #define CMD_GET_PARAM 0x24
  50. #define CMD_BOOT_LOADER 0x2F
  51. static int i2c_connected;
  52. void i2c_enumerate_interfaces(void)
  53. {
  54. gui_flush_i2c_interfaces();
  55. char path[64];
  56. strcpy(path, "/dev");
  57. DIR *dp = opendir(path);
  58. if (dp == NULL) {
  59. perror("opendir()");
  60. return;
  61. }
  62. char *ptr = path + strlen(path);
  63. *ptr = '/';
  64. struct dirent *dentry;
  65. while ((dentry = readdir(dp)) != NULL) {
  66. if (!strcmp(dentry->d_name, "."))
  67. continue;
  68. if (!strcmp(dentry->d_name, ".."))
  69. continue;
  70. if (strncmp(dentry->d_name, "i2c-", 4) != 0)
  71. continue;
  72. strcpy(ptr +1, dentry->d_name);
  73. struct stat statbuf;
  74. if (stat(path, &statbuf) == -1) {
  75. perror("stat()");
  76. continue;
  77. }
  78. if (!S_ISCHR(statbuf.st_mode))
  79. continue;
  80. /* TODO: check permissions */
  81. gui_add_i2c_interface(path);
  82. }
  83. closedir(dp);
  84. }
  85. void i2c_close(int fd)
  86. {
  87. i2c_connected = 0;
  88. i2c_close(fd);
  89. }
  90. int i2c_open(const char *path)
  91. {
  92. int fd = open(path, O_RDWR);
  93. if (fd < 0) {
  94. perror("open()");
  95. return -1;
  96. }
  97. unsigned long funcs;
  98. if (ioctl(fd, I2C_FUNCS, &funcs)) {
  99. perror("ioctl(I2C_FUNCS)");
  100. i2c_close(fd);
  101. return -1;
  102. }
  103. if (!(funcs & I2C_FUNC_I2C)) {
  104. fprintf(stderr, "I2C_FUNC_I2C not supported!\n");
  105. i2c_close(fd);
  106. return -1;
  107. }
  108. i2c_connected = 1;
  109. return fd;
  110. }
  111. void i2c_set_address(int fd, int address)
  112. {
  113. if (ioctl(fd, I2C_SLAVE, address) < 0) {
  114. perror("ioctl(I2C_SLAVE)");
  115. i2c_close(fd);
  116. }
  117. }
  118. int i2c_isconnected(void)
  119. {
  120. return i2c_connected;
  121. }
  122. void i2c_cmd_bootloader(int fd)
  123. {
  124. char cmd[] = { CMD_BOOT_LOADER };
  125. write(fd, cmd, 1);
  126. }
  127. void i2c_cmd_application(int fd)
  128. {
  129. char cmd[] = { CMD_BOOT_APPLICATION };
  130. write(fd, cmd, 1);
  131. }
  132. void i2c_cmd_getinfo(int fd, char *buf, int size)
  133. {
  134. char cmd[] = { CMD_GET_INFO };
  135. write(fd, cmd, 1);
  136. size = MIN(16, size);
  137. memset(buf, 0, size);
  138. read(fd, buf, size);
  139. int i;
  140. for (i = 0; i < size; i++)
  141. buf[i] &= ~0x80;
  142. }
  143. void i2c_cmd_getsignature(int fd, unsigned char *buf, int size)
  144. {
  145. char cmd[] = { CMD_GET_SIGNATURE };
  146. write(fd, cmd, 1);
  147. memset(buf, 0, size);
  148. read(fd, buf, MIN(4, size));
  149. }
  150. void i2c_cmd_getparameters(int fd, struct blmc_parameter *blmc)
  151. {
  152. char cmd[] = { CMD_GET_PARAM };
  153. write(fd, cmd, 1);
  154. memset(blmc, 0, sizeof(struct blmc_parameter));
  155. read(fd, blmc, sizeof(struct blmc_parameter));
  156. }
  157. void i2c_cmd_setparameters(int fd, struct blmc_parameter *blmc, int persistent)
  158. {
  159. char cmd[sizeof(struct blmc_parameter) +1];
  160. cmd[0] = CMD_SET_PARAM;
  161. memcpy(cmd +1, blmc, sizeof(struct blmc_parameter));
  162. write(fd, cmd, sizeof(struct blmc_parameter) + (persistent ? 1 : -1));
  163. }
  164. int i2c_write_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size))
  165. {
  166. int pos = 0;
  167. while (pos < size) {
  168. progress_cb(pos, size);
  169. char buf[FLASH_BLKSIZE +5];
  170. buf[0] = CMD_WRITE_FLASH;
  171. buf[1] = (pos >> 8) & 0xFF;
  172. buf[2] = pos & 0xFF;
  173. buf[3] = (COOKIE >> 8) & 0xFF;
  174. buf[4] = COOKIE & 0xFF;
  175. /* copy data and pad with 0xFF */
  176. int len = MIN(FLASH_BLKSIZE, size - pos);
  177. memcpy(buf +5, &data[pos], len);
  178. memset(buf +5 +len, 0xFF, FLASH_BLKSIZE - len);
  179. write(fd, buf, sizeof(buf));
  180. pos += len;
  181. }
  182. progress_cb(pos, size);
  183. return pos;
  184. }
  185. int i2c_read_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size))
  186. {
  187. int pos = 0;
  188. while (pos < size) {
  189. progress_cb(pos, size);
  190. char cmd[3];
  191. cmd[0] = CMD_READ_FLASH;
  192. cmd[1] = (pos >> 8) & 0xFF;
  193. cmd[2] = pos & 0xFF;
  194. write(fd, cmd, 3);
  195. int len = MIN(FLASH_BLKSIZE, size - pos);
  196. read(fd, data + pos, len);
  197. pos += len;
  198. }
  199. progress_cb(pos, size);
  200. return pos;
  201. }
  202. int i2c_verify_flash(int fd, char *data, int size, void (*progress_cb)(int pos, int size))
  203. {
  204. int pos = 0;
  205. while (pos < size) {
  206. progress_cb(pos, size);
  207. char cmd[3];
  208. cmd[0] = CMD_READ_FLASH;
  209. cmd[1] = (pos >> 8) & 0xFF;
  210. cmd[2] = pos & 0xFF;
  211. write(fd, cmd, 3);
  212. char buf2[FLASH_BLKSIZE];
  213. int len = MIN(FLASH_BLKSIZE, size - pos);
  214. read(fd, buf2, len);
  215. if (memcmp(data + pos, buf2, len) != 0)
  216. break;
  217. pos += len;
  218. }
  219. progress_cb(pos, size);
  220. return pos;
  221. }
  222. void i2c_cmd_setpwm(int fd, int pwm)
  223. {
  224. char cmd[] = { CMD_SET_PWM, pwm };
  225. write(fd, cmd, 2);
  226. }
  227. void i2c_cmd_getstatus(int fd, struct blmc_status *status)
  228. {
  229. char cmd[] = { CMD_GET_STATUS };
  230. write(fd, cmd, 1);
  231. memset(status, 0, sizeof(struct blmc_status));
  232. read(fd, status, sizeof(struct blmc_status));
  233. }