GoogleGit

blob: fc6c7360233cb5d7a97035cc621565132dacb4e6 [file] [log] [blame]
  1. /* Driver for Glass low-power sensor hub
  2. *
  3. * Copyright (C) 2012 Google, Inc.
  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; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/poll.h>
  20. #include <linux/delay.h>
  21. #include <linux/i2c.h>
  22. #include <linux/gpio.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/input.h>
  25. #include <linux/slab.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/ctype.h>
  28. /* Used to provide access via misc device */
  29. #include <linux/miscdevice.h>
  30. #include <linux/i2c/glasshub.h>
  31. /* module debug flags */
  32. #define DEBUG_FLASH_MODE 0
  33. /* driver name/version */
  34. #define DEVICE_NAME "glasshub"
  35. #define DRIVER_VERSION "0.15"
  36. /* minimum MCU firmware version required for this driver */
  37. #define MINIMUM_MCU_VERSION 27
  38. /* experimental MCU firmware version */
  39. #define EXPERIMENTAL_MCU_VERSION 0x8000
  40. /* special app code to flash the bootloader */
  41. #define BOOTLOADER_FLASHER 0xff
  42. /* number of retries */
  43. #define NUMBER_OF_I2C_RETRIES 3
  44. /* 8-bit registers for the glass hub MCU */
  45. #define REG_RESET 0
  46. #define REG_STATUS 0
  47. #define REG_PART_ID 1
  48. #define REG_VERSION 2
  49. #define REG_ENABLE_INT 3
  50. #define REG_ENABLE_PASSTHRU 4
  51. #define REG_ENABLE_DON_DOFF 5
  52. #define REG_DON_DOFF 6
  53. #define REG_ENABLE_WINK 7
  54. #define REG_DON_DOFF_HYSTERESIS 8
  55. #define REG_LED_DRIVE 9
  56. #define REG_ERROR_CODE 10
  57. #define REG_FLASH_DATA 11
  58. #define REG_DETECTOR_GAIN 12
  59. #define REG_PROX_PART_ID 13
  60. #define REG_PROX_SEQ_ID 14
  61. #define REG_PAUSE 15
  62. #define REG_WINK_STATUS 16
  63. #define REG_DEBUG 17
  64. #define REG_AMBIENT_ENABLE 18
  65. #define REG_WINK_MIN 19
  66. #define REG_WINK_MAX 20
  67. /* virtual registers */
  68. #define REG_STATUS_READ_ONLY 64
  69. /* 16-bit registers */
  70. #define REG16_DETECTOR_BIAS 0x80
  71. #define REG16_DON_DOFF_THRESH 0x81
  72. #define REG16_MIN_PROX 0x82
  73. #define REG16_PROX_RAW 0x83
  74. #define REG16_PROX_DATA 0x84
  75. #define REG16_ADDRESS 0x85
  76. #define REG16_VIS_DATA 0x86
  77. #define REG16_IR_DATA 0x87
  78. #define REG16_FRAME_COUNT 0x88
  79. #define REG16_DEBUG 0x89
  80. #define REG16_TIMER_COUNT 0x8a
  81. #define REG16_DOFF_THRESH 0x8b
  82. /* virtual 16-bit registers */
  83. #define REG16_DON_THRESH 0xc1
  84. #define CMD_APP_VERSION 0xf6
  85. #define CMD_BOOTLOADER_VERSION 0xf7
  86. #define CMD_FLASH_STATUS 0xf8
  87. #define CMD_FLASH 0xf9
  88. #define CMD_BOOT 0xfA
  89. /* interrupt sources in STATUS register */
  90. #define IRQ_PASSTHRU 0b00000001
  91. #define IRQ_WINK 0b00000010
  92. #define IRQ_DON_DOFF 0b00000100
  93. /* status flags in STATUS register */
  94. #define STATUS_OVERFLOW 0b10000000
  95. /* device ID */
  96. #define GLASSHUB_PART_ID 0xbb
  97. #define PROX_DATA_FIFO_SIZE 16
  98. /* input device constants */
  99. #define PS_MIN_VALUE 0
  100. #define PS_MAX_VALUE 65535
  101. /* bit fields for flags */
  102. #define FLAG_WAKE_THREAD 0
  103. #define FLAG_DEVICE_BOOTED 1
  104. #define FLAG_FLASH_MODE 2
  105. #define FLAG_SYSFS_CREATED 3
  106. #define FLAG_DEVICE_DISABLED 4
  107. #define FLAG_WINK_FLAG_ENABLE 5
  108. #define FLAG_DEVICE_MAY_BE_WEDGED 31
  109. /* flags for device permissions */
  110. #define DEV_MODE_RO (S_IRUSR | S_IRGRP)
  111. #define DEV_MODE_WO (S_IWUSR | S_IWGRP)
  112. #define DEV_MODE_RW (DEV_MODE_RO | DEV_MODE_WO)
  113. /* firmware parameters */
  114. #define FIRMWARE_PAGE_SIZE 64
  115. #define FIRMWARE_TOTAL_SIZE (8*1024)
  116. #define FIRMWARE_TOTAL_PAGES (FIRMWARE_TOTAL_SIZE / FIRMWARE_PAGE_SIZE)
  117. #define FIRMWARE_NUM_DIRTY_BITS ((FIRMWARE_TOTAL_PAGES+ 31) / 32)
  118. #define FIRMWARE_BASE_ADDRESS 0x8000
  119. #define FIRMWARE_END_ADDRESS (FIRMWARE_BASE_ADDRESS + FIRMWARE_TOTAL_SIZE - 1)
  120. /* firmware state machine */
  121. #define FW_STATE_START 0
  122. #define FW_STATE_TYPE 1
  123. #define FW_STATE_COUNT_HI 2
  124. #define FW_STATE_COUNT_LO 3
  125. #define FW_STATE_ADDR_7 4
  126. #define FW_STATE_ADDR_6 5
  127. #define FW_STATE_ADDR_5 6
  128. #define FW_STATE_ADDR_4 7
  129. #define FW_STATE_ADDR_3 8
  130. #define FW_STATE_ADDR_2 9
  131. #define FW_STATE_ADDR_1 10
  132. #define FW_STATE_ADDR_0 11
  133. #define FW_STATE_BYTE_HI 12
  134. #define FW_STATE_BYTE_LO 13
  135. #define FW_STATE_CHECKSUM_HI 14
  136. #define FW_STATE_CHECKSUM_LO 15
  137. /* location of calibration data */
  138. #define CALIB_ADDRESS 0x9ff0
  139. /* number of samples to take for calibration mean */
  140. #define NUM_CALIBRATION_SAMPLES 3
  141. /* number of samples in prox data buffer, must be a power of 2 */
  142. #define PROX_QUEUE_SZ (1<<6)
  143. /* expected prox sample interval in nanoseconds */
  144. #define PROX_INTERVAL (1000000000LL / 32)
  145. /* Interval for drift correction in sample frames. Uses shift
  146. * operation to avoid divide, so the interval must be a power
  147. * of 2. Thus, a value of 5 yields an interval of 32 frames or
  148. * 1 second at 32 Hz.
  149. */
  150. #define PROX_AVERAGING_SHIFT 5
  151. #define PROX_AVERAGING_INTERVAL (1 << PROX_AVERAGING_SHIFT)
  152. /* flags and mask for prox meta data */
  153. #define PROX_DATA_MASK 0x7fff
  154. #define PROX_DATA_MASK_NO_WINK_FLAG 0x3fff
  155. #define PROX_DATA_END_OF_DATA_FLAG 0x8000
  156. #define PROX_DATA_WINK_DETECTED_FLAG 0x4000
  157. /* values for flash_status */
  158. #define FLASH_STATUS_OK 0
  159. #define FLASH_STATUS_READY 1
  160. #define FLASH_ERROR_DEV_REJECTED_PKT -1
  161. #define FLASH_ERROR_INVALID_S_RECORD -2
  162. #define FLASH_ERROR_ADDRESS_RANGE -3
  163. #define FLASH_ERROR_INVALID_HEX_DIGIT -4
  164. #define FLASH_ERROR_SREC_CHECKSUM_ERROR -5
  165. #define FLASH_ERROR_DEVICE_RESET_ERROR -6
  166. #define FLASH_ERROR_I2C_DEVICE_COMM -7
  167. #define FLASH_ERROR_NO_PAGES_FLASHED -8
  168. #define FLASH_ERROR_BOOTLOADER_VERSION -9
  169. #define FLASH_ERROR_DEVICE_BOOT_ERROR -10
  170. /*
  171. * Basic theory of operation:
  172. *
  173. * The glass hub device comes up in bootloader mode. This mode supports only 5 commands:
  174. *
  175. * BOOT Jump to the application code stored in flash
  176. * FLASH Download a 64-byte page of firmware to flash
  177. * FLASH_STATUS Report status of last flash operation
  178. * APP_VERSION Returns the 2-byte application version
  179. * BOOT_VERSION Returns the 1-byte bootloader version
  180. *
  181. * It is now possible to flash the bootloader by loading special application
  182. * code. It reports a version number of 255.xxx which allows the driver to
  183. * identify it as special code to flash the bootloader. Flash operation
  184. * proceeds as normal except that at the point where the firmware image is to
  185. * be flashed, the driver boots into the special application code. Flashing
  186. * takes place as usual, the driver then resets into the new bootloader code
  187. * and reads the bootloader version. To enable this process from user space,
  188. * first download the special bootloader app code, then write a 1 to the
  189. * fw_update_enable sysfs node, download the bootloader app code, then write
  190. * a 255 to the fw_update_enable node. It is this last step that tells the
  191. * driver code that you want to write bootloader code instead of app code.
  192. * There is some risk that this operation could result in a bricked device,
  193. * since a failure to write the boot code is unrecoverable. In this case,
  194. * it will be necessary to attach a SWIM connector and use the debugger
  195. * to flash a new bootloader image.
  196. *
  197. * The application code can be flashed by a simple script. First, write a 1
  198. * to the enable_fw_update sysfs node. The driver will allocate a block of
  199. * memory to save the firmware image and clear the dirty bits for each page
  200. * of code. Next, write the .S19 the .S19 application code image in text
  201. * format (e.g. "cat") to the update_fw_data sysfs node. The driver will
  202. * validate the checksum of each line and store it in the binary code buffer,
  203. * setting a dirty bit for each 64-byte page of code it touches. Be aware
  204. * that it doesn't backfill missing data, i.e. if you touch a single byte on
  205. * a page, the rest of the page will be written with zeroes. Thus, it's
  206. * always a good idea to flash the entire image each time. Finally, write a 0
  207. * to the enable_fw_update sysfs node. This will cause the driver to write
  208. * each dirty page to the device. Note that the concept of a flash
  209. * programming mode is purely an artifact of the driver and not something the
  210. * device itself enforces or is even aware of.
  211. *
  212. * For normal operation, the driver will automatically boot the device to
  213. * start running application code the first time it receives any command that
  214. * requires the application code. From the application code, a RESET command
  215. * will cause the device to return to the bootloader. It is possible that a
  216. * bug in the application code will make it impossible to return to the
  217. * bootloader. In this case, it may be necessary to power-down the device to
  218. * force it back into the bootloader. This allows for recovery in case there
  219. * is a serious bug in the application code.
  220. */
  221. struct glasshub_data {
  222. struct i2c_client *i2c_client;
  223. struct input_dev *ps_input_dev;
  224. const struct glasshub_platform_data *pdata;
  225. uint8_t *fw_image;
  226. uint32_t fw_dirty[FIRMWARE_NUM_DIRTY_BITS];
  227. int flash_status;
  228. int fw_state;
  229. int fw_rec_type;
  230. int fw_index;
  231. int fw_count;
  232. uint32_t fw_value;
  233. uint8_t fw_checksum;
  234. struct mutex device_lock;
  235. uint64_t irq_timestamp;
  236. uint64_t last_timestamp;
  237. uint64_t start_timestamp;
  238. unsigned long count_for_average;
  239. unsigned long average_delta;
  240. unsigned long sample_count;
  241. volatile unsigned long flags;
  242. uint8_t don_doff_state;
  243. uint8_t bootloader_version;
  244. uint8_t app_version_major;
  245. uint8_t app_version_minor;
  246. uint8_t last_irq_status;
  247. int debug;
  248. };
  249. struct glasshub_data *glasshub_private = NULL;
  250. /*
  251. * Kernel FIFO for timestamped prox data
  252. */
  253. static DECLARE_WAIT_QUEUE_HEAD(prox_read_wait);
  254. static DECLARE_KFIFO(prox_fifo, struct glasshub_data_user, PROX_QUEUE_SZ);
  255. static atomic_t glasshub_opened = ATOMIC_INIT(0);
  256. static int register_device_files(struct glasshub_data *glasshub);
  257. /*
  258. * I2C bus transaction read for consecutive data.
  259. * Returns 0 on success.
  260. */
  261. static int _i2c_read(struct glasshub_data *glasshub, uint8_t *txData, int txLength,
  262. uint8_t *rxData,int rxLength)
  263. {
  264. int i;
  265. struct i2c_msg data[] = {
  266. {
  267. .addr = glasshub->i2c_client->addr,
  268. .flags = 0,
  269. .len = txLength,
  270. .buf = txData,
  271. },
  272. {
  273. .addr = glasshub->i2c_client->addr,
  274. .flags = I2C_M_RD,
  275. .len = rxLength,
  276. .buf = rxData,
  277. },
  278. };
  279. for (i = 0; i < NUMBER_OF_I2C_RETRIES; i++) {
  280. if (i2c_transfer(glasshub->i2c_client->adapter, data, 2) > 0)
  281. break;
  282. /* Delay before retrying */
  283. mdelay(10);
  284. }
  285. if (i >= NUMBER_OF_I2C_RETRIES) {
  286. dev_err(&glasshub->i2c_client->dev, "%s i2c read retry exceeded\n", __FUNCTION__);
  287. return -EIO;
  288. }
  289. return 0;
  290. }
  291. /*
  292. * I2C bus transaction to write consecutive data.
  293. * Returns 0 on success.
  294. */
  295. static int _i2c_write_mult(struct glasshub_data *glasshub, uint8_t *txData, int length)
  296. {
  297. int i;
  298. struct i2c_msg data[] = {
  299. {
  300. .addr = glasshub->i2c_client->addr,
  301. .flags = 0,
  302. .len = length,
  303. .buf = txData,
  304. },
  305. };
  306. for (i = 0; i < NUMBER_OF_I2C_RETRIES; i++) {
  307. if (i2c_transfer(glasshub->i2c_client->adapter, data, 1) > 0)
  308. break;
  309. /* Delay before retrying */
  310. mdelay(10);
  311. }
  312. if (i >= NUMBER_OF_I2C_RETRIES) {
  313. dev_err(&glasshub->i2c_client->dev, "%s i2c write retry exceeded\n", __FUNCTION__);
  314. return -EIO;
  315. }
  316. return 0;
  317. }
  318. /*
  319. * I2C bus transaction to read a register
  320. * Returns 0 on success.
  321. */
  322. static int _i2c_read_reg(struct glasshub_data *glasshub, uint8_t reg, unsigned *value)
  323. {
  324. int rc = 0;
  325. uint8_t buffer[2];
  326. *value = 0xffff;
  327. buffer[0] = reg;
  328. buffer[1] = 0;
  329. if (reg & 0x80) {
  330. rc = _i2c_read(glasshub, buffer, 1, buffer, 2);
  331. if (rc == 0) {
  332. *value = buffer[0] | (unsigned) buffer[1] << 8;
  333. }
  334. } else {
  335. rc = _i2c_read(glasshub, buffer, 1, buffer, 1);
  336. if (rc == 0) {
  337. *value = buffer[0];
  338. }
  339. }
  340. return rc;
  341. }
  342. /*
  343. * I2C bus transaction to read single byte.
  344. * Returns 0 on success.
  345. */
  346. static int _i2c_read_reg8(struct glasshub_data *glasshub, uint8_t reg, uint8_t *data)
  347. {
  348. int rc;
  349. unsigned value;
  350. rc = _i2c_read_reg(glasshub, reg, &value);
  351. *data = (uint8_t) value;
  352. return rc;
  353. }
  354. /*
  355. * I2C bus transaction to write register
  356. * Returns 0 on success.
  357. */
  358. static int _i2c_write_reg(struct glasshub_data *glasshub, uint8_t reg, uint16_t data)
  359. {
  360. uint8_t buffer[3];
  361. buffer[0] = reg;
  362. buffer[1] = data & 0xff;
  363. buffer[2] = (data >> 8) & 0xff;
  364. return _i2c_write_mult(glasshub, buffer, reg & 0x80 ? 3 : 2);
  365. }
  366. /* read an 8-bit value from glasshub memory */
  367. static int read_glasshub_memory(struct glasshub_data *glasshub, uint16_t addr, unsigned *value)
  368. {
  369. int rc;
  370. *value = 0;
  371. /* send address */
  372. rc = _i2c_write_reg(glasshub, REG16_ADDRESS, addr);
  373. if (rc) goto Error;
  374. /* read data */
  375. rc = _i2c_read_reg(glasshub, REG_FLASH_DATA, value);
  376. Error:
  377. return rc;
  378. }
  379. static int write_glasshub_memory(struct glasshub_data *glasshub, uint16_t addr, uint8_t value)
  380. {
  381. int rc;
  382. rc = _i2c_write_reg(glasshub, REG16_ADDRESS, addr);
  383. if (rc == 0) {
  384. _i2c_write_reg(glasshub, REG_FLASH_DATA, value);
  385. }
  386. return rc;
  387. }
  388. static int _check_part_id(struct glasshub_data *glasshub)
  389. {
  390. int rc = 0;
  391. uint8_t data = 0;
  392. struct i2c_client *i2c_client = glasshub->i2c_client;
  393. rc = _i2c_read_reg8(glasshub, REG_PART_ID, &data);
  394. if (rc < 0) {
  395. dev_err(&i2c_client->dev, "%s: Unable to read part identifier\n", __FUNCTION__);
  396. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  397. return -EIO;
  398. }
  399. if (data != GLASSHUB_PART_ID) {
  400. dev_err(&i2c_client->dev, "%s Unexpected part ID = %u\n",
  401. __FUNCTION__, (unsigned)data);
  402. rc = -ENODEV;
  403. }
  404. return rc;
  405. }
  406. /* must hold the device lock */
  407. int boot_device_l(struct glasshub_data *glasshub)
  408. {
  409. int retry;
  410. uint8_t temp;
  411. int rc = 0;
  412. /* if already booted, do nothing */
  413. if (test_bit(FLAG_DEVICE_BOOTED, &glasshub->flags)) goto err_out;
  414. /* don't allow boot if disabled */
  415. if (test_bit(FLAG_DEVICE_DISABLED, &glasshub->flags)) {
  416. rc = -ENODEV;
  417. goto err_out;
  418. }
  419. /* don't allow boot from flash mode, unless boot flasher app
  420. * code has been loaded.
  421. */
  422. if (glasshub->app_version_major != BOOTLOADER_FLASHER) {
  423. if (test_bit(FLAG_FLASH_MODE, &glasshub->flags)) {
  424. rc = -ENODEV;
  425. goto err_out;
  426. }
  427. }
  428. /* tell glass hub to boot */
  429. temp = CMD_BOOT;
  430. for (retry = 0; retry < 5; retry++) {
  431. rc = _i2c_write_mult(glasshub, &temp, sizeof(temp));
  432. /* takes some time for the app code to start up */
  433. msleep(100);
  434. if (rc == 0) break;
  435. }
  436. if (rc) {
  437. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  438. dev_err(&glasshub->i2c_client->dev, "%s Unable to boot glasshub device\n",
  439. __FUNCTION__);
  440. goto err_out;
  441. }
  442. /* if special boot flasher code, don't do anything else */
  443. if (glasshub->app_version_major == BOOTLOADER_FLASHER) return rc;
  444. /* verify part ID */
  445. if (_check_part_id(glasshub)) {
  446. rc = -ENODEV;
  447. goto err_out;
  448. }
  449. /* get current don/doff state */
  450. _i2c_read_reg8(glasshub, REG_DON_DOFF, &glasshub->don_doff_state);
  451. set_bit(FLAG_DEVICE_BOOTED, &glasshub->flags);
  452. err_out:
  453. return rc;
  454. }
  455. /* must hold the device lock */
  456. int reset_device_l(struct glasshub_data *glasshub, int force)
  457. {
  458. int retry;
  459. uint8_t temp;
  460. int rc = 0;
  461. if (!force && !test_bit(FLAG_DEVICE_BOOTED, &glasshub->flags)) goto err_out;
  462. /* reset glass hub */
  463. for (retry = 0; retry < 5; retry++) {
  464. temp = REG_RESET;
  465. rc = _i2c_write_mult(glasshub, &temp, sizeof(temp));
  466. if (rc == 0) break;
  467. }
  468. if (rc) {
  469. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  470. dev_err(&glasshub->i2c_client->dev, "%s Unable to reset glasshub device\n",
  471. __FUNCTION__);
  472. goto err_out;
  473. }
  474. /* takes some time for the app code to shut down */
  475. msleep(50);
  476. clear_bit(FLAG_DEVICE_BOOTED, &glasshub->flags);
  477. err_out:
  478. return rc;
  479. }
  480. /* Main interrupt handler. We save a timestamp here and schedule
  481. * the threaded handler to run later, since we might have to
  482. * block on I/O requests from user space.
  483. */
  484. static irqreturn_t glasshub_irq_handler(int irq, void *dev_id)
  485. {
  486. struct glasshub_data *glasshub = (struct glasshub_data*) dev_id;
  487. /* if device is not booted, the interrupt must be someone else */
  488. if (!test_bit(FLAG_DEVICE_BOOTED, &glasshub->flags)) goto Handled;
  489. /* if threaded handler is already scheduled, don't schedule it again */
  490. if (test_and_set_bit(FLAG_WAKE_THREAD, &glasshub->flags)) goto Handled;
  491. /* save timestamp for threaded handler and schedule it */
  492. glasshub->irq_timestamp = read_robust_clock();
  493. return IRQ_WAKE_THREAD;
  494. Handled:
  495. return IRQ_HANDLED;
  496. }
  497. /* Threaded interrupt handler. This is where the real work gets done.
  498. * We grab a mutex to prevent I/O requests from user space from
  499. * running concurrently. The timestamp for critical operations comes
  500. * from the main interrupt handler.
  501. */
  502. static irqreturn_t glasshub_threaded_irq_handler(int irq, void *dev_id)
  503. {
  504. struct glasshub_data *glasshub = (struct glasshub_data*) dev_id;
  505. int rc = 0;
  506. uint8_t status;
  507. uint16_t data[PROX_QUEUE_SZ];
  508. int prox_count = 0;
  509. int i;
  510. uint64_t timestamp;
  511. /* clear in-service flag */
  512. timestamp = glasshub->irq_timestamp;
  513. clear_bit(FLAG_WAKE_THREAD, &glasshub->flags);
  514. mutex_lock(&glasshub->device_lock);
  515. /* just in case the glasshub was reset after scheduling this handler */
  516. if (!test_bit(FLAG_DEVICE_BOOTED, &glasshub->flags)) {
  517. dev_warn(&glasshub->i2c_client->dev,
  518. "%s: Ignoring pending interrupt because device is disabled\n",
  519. __FUNCTION__);
  520. goto Exit;
  521. }
  522. /* read the IRQ source */
  523. rc = _i2c_read_reg8(glasshub, REG_STATUS, &status);
  524. if (rc) goto Error;
  525. glasshub->last_irq_status = status;
  526. /* process prox data */
  527. while (status & (IRQ_PASSTHRU | IRQ_WINK)) {
  528. unsigned value = 0;
  529. /* read value */
  530. rc = _i2c_read_reg(glasshub, REG16_PROX_DATA, &value);
  531. if (rc) goto Error;
  532. /* shouldn't happen, but 0xffff indicates we read past end of buffer */
  533. if (value == 0xffff) {
  534. dev_warn(&glasshub->i2c_client->dev,
  535. "%s: read past end of buffer, status = 0x%02x\n",
  536. __FUNCTION__,
  537. status);
  538. break;
  539. }
  540. ++glasshub->sample_count;
  541. /* DEBUG: read frame counter */
  542. if (glasshub->debug && (prox_count == 0))
  543. {
  544. unsigned frame_count;
  545. rc = _i2c_read_reg(glasshub, REG16_FRAME_COUNT, &frame_count);
  546. if (rc) goto Error;
  547. printk("%s: Frame count = %u\n", __func__, frame_count);
  548. }
  549. /* Buffer up data (and drop data that exceeds our buffer
  550. * length). Note that when the high bit is set, there is
  551. * no more data. This mechanism saves us from doing another
  552. * I2C bus transfer to check the status register.
  553. */
  554. if (atomic_read(&glasshub_opened) && (prox_count < PROX_QUEUE_SZ)) {
  555. data[prox_count++] = (uint16_t) value;
  556. }
  557. /* check for end of data */
  558. if (value & PROX_DATA_END_OF_DATA_FLAG) break;
  559. }
  560. /* pass prox data to misc device driver */
  561. if (prox_count) {
  562. uint16_t mask = PROX_DATA_MASK_NO_WINK_FLAG;
  563. if (glasshub->debug) {
  564. printk("%s: Read %d packets\n", __func__, prox_count);
  565. }
  566. /* should we mask the wink flag bit? */
  567. if (test_bit(FLAG_WINK_FLAG_ENABLE, &glasshub->flags)) {
  568. mask = PROX_DATA_MASK;
  569. }
  570. /* if prox data is not contiguous, start from current timestamp */
  571. if (status & STATUS_OVERFLOW || (glasshub->last_timestamp == 0)) {
  572. uint64_t temp = timestamp - prox_count * glasshub->average_delta;
  573. /* don't go backwards in time (shouldn't happen) */
  574. if (temp > glasshub->last_timestamp) {
  575. glasshub->last_timestamp = temp;
  576. } else {
  577. dev_warn(&glasshub->i2c_client->dev,
  578. "%s: Time travel? %llu is earlier than %llu\n",
  579. __FUNCTION__,
  580. temp,
  581. glasshub->last_timestamp);
  582. }
  583. glasshub->last_timestamp =
  584. (temp < glasshub->last_timestamp ? glasshub->last_timestamp : temp);
  585. glasshub->start_timestamp = 0;
  586. if (glasshub->debug) {
  587. printk("%s: Resync @ %llu\n", __func__, glasshub->start_timestamp);
  588. }
  589. }
  590. /* copy data to FIFO, filling in timestamps */
  591. for (i = 0; i < prox_count; i++) {
  592. struct glasshub_data_user rec;
  593. rec.value = data[i] & mask;
  594. glasshub->last_timestamp += glasshub->average_delta;
  595. rec.timestamp = glasshub->last_timestamp;
  596. kfifo_put(&prox_fifo, &rec);
  597. if (glasshub->debug && (i == 0)) {
  598. printk("%s: First sample in packet @ %llu\n", __func__, glasshub->last_timestamp);
  599. }
  600. }
  601. /* calculate average sample rate */
  602. if (prox_count == 1) {
  603. if (glasshub->start_timestamp == 0) {
  604. glasshub->start_timestamp = timestamp;
  605. glasshub->count_for_average = 0;
  606. } else {
  607. if (++glasshub->count_for_average == PROX_AVERAGING_INTERVAL) {
  608. glasshub->average_delta = (timestamp - glasshub->start_timestamp)
  609. >> PROX_AVERAGING_SHIFT;
  610. glasshub->start_timestamp = timestamp;
  611. glasshub->count_for_average = 0;
  612. }
  613. }
  614. } else {
  615. glasshub->start_timestamp = 0;
  616. }
  617. /* wake up user space */
  618. wake_up_interruptible(&prox_read_wait);
  619. }
  620. /* process don/doff */
  621. if (status & IRQ_DON_DOFF) {
  622. rc = _i2c_read_reg8(glasshub, REG_DON_DOFF, &glasshub->don_doff_state);
  623. if (rc) goto Error;
  624. dev_info(&glasshub->i2c_client->dev, "%s: don/doff state = %u\n",
  625. __FUNCTION__, glasshub->don_doff_state);
  626. sysfs_notify(&glasshub->i2c_client->dev.kobj, NULL, "don_doff");
  627. }
  628. /* process wink signal */
  629. if (status & IRQ_WINK) {
  630. dev_info(&glasshub->i2c_client->dev, "%s: wink signal received\n",
  631. __FUNCTION__);
  632. sysfs_notify(&glasshub->i2c_client->dev.kobj, NULL, "wink");
  633. }
  634. goto Exit;
  635. Error:
  636. dev_err(&glasshub->i2c_client->dev, "%s: device read error\n", __FUNCTION__);
  637. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  638. Exit:
  639. mutex_unlock(&glasshub->device_lock);
  640. return IRQ_HANDLED;
  641. }
  642. /* common routine to return an I2C register to userspace */
  643. static ssize_t show_reg(struct device *dev, char *buf, uint8_t reg)
  644. {
  645. unsigned value = 0xffff;
  646. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  647. mutex_lock(&glasshub->device_lock);
  648. if (boot_device_l(glasshub) == 0) {
  649. if (_i2c_read_reg(glasshub, reg, &value)) {
  650. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  651. }
  652. }
  653. mutex_unlock(&glasshub->device_lock);
  654. return sprintf(buf, "%u\n", value);
  655. }
  656. /* common routine to set an I2C register from userspace */
  657. static ssize_t store_reg(struct device *dev, const char *buf, size_t count,
  658. uint8_t reg, uint16_t min, uint16_t max)
  659. {
  660. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  661. unsigned long value = 0;
  662. if (!kstrtoul(buf, 10, &value)) {
  663. value = (value < min) ? min : value;
  664. value = (value > max) ? max : value;
  665. mutex_lock(&glasshub->device_lock);
  666. if (boot_device_l(glasshub) == 0) {
  667. if (_i2c_write_reg(glasshub, reg, value)) {
  668. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  669. }
  670. }
  671. mutex_unlock(&glasshub->device_lock);
  672. }
  673. return count;
  674. }
  675. /* show the application version number */
  676. static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
  677. {
  678. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  679. return sprintf(buf, "%d.%d\n", glasshub->app_version_major, glasshub->app_version_minor);
  680. }
  681. /* show the bootloader version number */
  682. static ssize_t bootloader_version_show(struct device *dev, struct device_attribute *attr, char *buf)
  683. {
  684. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  685. return sprintf(buf, "%d\n", glasshub->bootloader_version);
  686. }
  687. /* show the driver version number */
  688. static ssize_t driver_version_show(struct device *dev, struct device_attribute *attr, char *buf)
  689. {
  690. return sprintf(buf, "%s\n", DRIVER_VERSION);
  691. }
  692. /* show the driver status */
  693. static ssize_t driver_flags_show(struct device *dev, struct device_attribute *attr, char *buf)
  694. {
  695. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  696. return sprintf(buf, "0x%08lx\n", glasshub->flags);
  697. }
  698. /* show don/doff status */
  699. static ssize_t don_doff_show(struct device *dev, struct device_attribute *attr, char *buf)
  700. {
  701. return show_reg(dev, buf, REG_DON_DOFF);
  702. }
  703. /* show don/doff enable status */
  704. static ssize_t don_doff_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  705. {
  706. return show_reg(dev, buf, REG_ENABLE_DON_DOFF);
  707. }
  708. /* enable/disable don/doff */
  709. static ssize_t don_doff_enable_store(struct device *dev, struct device_attribute *attr,
  710. const char *buf, size_t count)
  711. {
  712. return store_reg(dev, buf, count, REG_ENABLE_DON_DOFF, 0, 1);
  713. }
  714. /* show prox passthrough mode */
  715. static ssize_t passthru_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  716. {
  717. return show_reg(dev, buf, REG_ENABLE_PASSTHRU);
  718. }
  719. /* enable/disable prox passthrough mode */
  720. static ssize_t passthru_enable_store(struct device *dev, struct device_attribute *attr,
  721. const char *buf, size_t count)
  722. {
  723. return store_reg(dev, buf, count, REG_ENABLE_PASSTHRU, 0, 1);
  724. }
  725. /* read prox value */
  726. static int read_prox_raw_l(struct glasshub_data *glasshub, uint16_t *pProxData)
  727. {
  728. int rc;
  729. unsigned data;
  730. *pProxData = 0xffff;
  731. rc = _i2c_read_reg(glasshub, REG16_PROX_RAW, &data);
  732. if (rc == 0) {
  733. *pProxData = (uint16_t) data;
  734. }
  735. return rc;
  736. }
  737. /* show minimum prox value */
  738. static ssize_t proxmin_show(struct device *dev, struct device_attribute *attr, char *buf)
  739. {
  740. return show_reg(dev, buf, REG16_MIN_PROX);
  741. }
  742. /* show raw prox value */
  743. static ssize_t proxraw_show(struct device *dev, struct device_attribute *attr, char *buf)
  744. {
  745. return show_reg(dev, buf, REG16_PROX_RAW);
  746. }
  747. /* show raw IR value */
  748. static ssize_t ir_show(struct device *dev, struct device_attribute *attr, char *buf)
  749. {
  750. return show_reg(dev, buf, REG16_IR_DATA);
  751. }
  752. /* show raw visible light value */
  753. static ssize_t visible_show(struct device *dev, struct device_attribute *attr, char *buf)
  754. {
  755. return show_reg(dev, buf, REG16_VIS_DATA);
  756. }
  757. /* show ambient enable */
  758. static ssize_t ambient_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  759. {
  760. return show_reg(dev, buf, REG_AMBIENT_ENABLE);
  761. }
  762. /* set ambient enable */
  763. static ssize_t ambient_enable_store(struct device *dev, struct device_attribute *attr, const char *buf,
  764. size_t count)
  765. {
  766. return store_reg(dev, buf, count, REG_AMBIENT_ENABLE, 0, 1);
  767. }
  768. /* show don/doff threshold value */
  769. static ssize_t don_doff_threshold_show(struct device *dev, struct device_attribute *attr,
  770. char *buf)
  771. {
  772. return show_reg(dev, buf, REG16_DON_DOFF_THRESH);
  773. }
  774. /* set don/doff threshold value */
  775. static ssize_t don_doff_threshold_store(struct device *dev, struct device_attribute *attr,
  776. const char *buf, size_t count)
  777. {
  778. return store_reg(dev, buf, count, REG16_DON_DOFF_THRESH, 1, 5000);
  779. }
  780. /* show don threshold value */
  781. static ssize_t don_threshold_show(struct device *dev, struct device_attribute *attr,
  782. char *buf)
  783. {
  784. return show_reg(dev, buf, REG16_DON_THRESH);
  785. }
  786. /* set don threshold value */
  787. static ssize_t don_threshold_store(struct device *dev, struct device_attribute *attr,
  788. const char *buf, size_t count)
  789. {
  790. return store_reg(dev, buf, count, REG16_DON_THRESH, 1, 5000);
  791. }
  792. /* show dof threshold value */
  793. static ssize_t doff_threshold_show(struct device *dev, struct device_attribute *attr,
  794. char *buf)
  795. {
  796. return show_reg(dev, buf, REG16_DOFF_THRESH);
  797. }
  798. /* set doff threshold value */
  799. static ssize_t doff_threshold_store(struct device *dev, struct device_attribute *attr,
  800. const char *buf, size_t count)
  801. {
  802. return store_reg(dev, buf, count, REG16_DOFF_THRESH, 1, 5000);
  803. }
  804. /* show don/doff hysteresis value */
  805. static ssize_t don_doff_hysteresis_show(struct device *dev, struct device_attribute *attr,
  806. char *buf)
  807. {
  808. return show_reg(dev, buf, REG_DON_DOFF_HYSTERESIS);
  809. }
  810. /* set don/doff hysteresis value */
  811. static ssize_t don_doff_hysteresis_store(struct device *dev, struct device_attribute *attr,
  812. const char *buf, size_t count)
  813. {
  814. return store_reg(dev, buf, count, REG_DON_DOFF_HYSTERESIS, 1, 255);
  815. }
  816. /* show IR LED drive value */
  817. static ssize_t led_drive_show(struct device *dev, struct device_attribute *attr, char *buf)
  818. {
  819. return show_reg(dev, buf, REG_LED_DRIVE);
  820. }
  821. /* set IR LED drive value */
  822. static ssize_t led_drive_store(struct device *dev, struct device_attribute *attr, const char *buf,
  823. size_t count)
  824. {
  825. return store_reg(dev, buf, count, REG_LED_DRIVE, 0, 7);
  826. }
  827. /* calibrate IR LED drive levels */
  828. static ssize_t calibrate_store(struct device *dev,
  829. struct device_attribute *attr, const char *buf, size_t count)
  830. {
  831. struct glasshub_data *glasshub;
  832. uint32_t sum;
  833. uint16_t temp;
  834. uint16_t proxValues[8];
  835. int i, j;
  836. int rc;
  837. uint8_t led_drive = 0x04;
  838. uint8_t value = 0;
  839. uint16_t addr;
  840. glasshub = dev_get_drvdata(dev);
  841. if (!kstrtou8(buf, 10, &value) && value) {
  842. mutex_lock(&glasshub->device_lock);
  843. if (boot_device_l(glasshub)) {
  844. rc = -ENODEV;
  845. goto Unlock;
  846. }
  847. /* read current drive level */
  848. _i2c_read_reg8(glasshub, REG_LED_DRIVE, &led_drive);
  849. /* test all 7 LED levels */
  850. for (i = 0; i < 8; i++) {
  851. /* set LED drive level */
  852. _i2c_write_reg(glasshub, REG_LED_DRIVE, i);
  853. /* read multiple values and take the mean */
  854. sum = 0;
  855. for (j = 0; j < NUM_CALIBRATION_SAMPLES; j++) {
  856. /* allow charge pump to re-charge */
  857. msleep(35);
  858. /* read raw prox value */
  859. rc = read_prox_raw_l(glasshub, &temp);
  860. if (rc) break;
  861. /* read raw prox value and add to running sum */
  862. sum += temp;
  863. }
  864. /* break on error */
  865. if (rc) break;
  866. /* save mean value */
  867. proxValues[i] = sum / NUM_CALIBRATION_SAMPLES;
  868. }
  869. /* write calibration values to flash */
  870. if (rc == 0) {
  871. addr = CALIB_ADDRESS;
  872. for (i = 0; i < 8; i++) {
  873. /* store calibration data */
  874. write_glasshub_memory(glasshub, addr++, (uint8_t) proxValues[i]);
  875. write_glasshub_memory(glasshub, addr++, (uint8_t) (proxValues[i] >> 8));
  876. }
  877. }
  878. /* restore LED drive level */
  879. _i2c_write_reg(glasshub, REG_LED_DRIVE, led_drive);
  880. Unlock:
  881. mutex_unlock(&glasshub->device_lock);
  882. }
  883. return count;
  884. }
  885. /* read calibration values */
  886. static ssize_t calibration_values_show(struct device *dev, struct device_attribute *attr,
  887. char *buf)
  888. {
  889. int rc;
  890. int i;
  891. unsigned temp;
  892. unsigned proxValues[8];
  893. uint16_t addr;
  894. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  895. memset(proxValues, 0, sizeof(proxValues));
  896. mutex_lock(&glasshub->device_lock);
  897. if (boot_device_l(glasshub)) goto Error;
  898. /* read calibration values from flash */
  899. addr = CALIB_ADDRESS;
  900. for (i = 0; i < 8; i++) {
  901. /* read LSB */
  902. rc = read_glasshub_memory(glasshub, addr++, &temp);
  903. if (rc) goto Error;
  904. proxValues[i] = temp;
  905. /* read MSB */
  906. rc = read_glasshub_memory(glasshub, addr++, &temp);
  907. if (rc) goto Error;
  908. proxValues[i] |= temp << 8;
  909. }
  910. Error:
  911. mutex_unlock(&glasshub->device_lock);
  912. return sprintf(buf, "%u %u %u %u %u %u %u %u\n",
  913. proxValues[0], proxValues[1],
  914. proxValues[2], proxValues[3],
  915. proxValues[4], proxValues[5],
  916. proxValues[6], proxValues[7]);
  917. }
  918. /* return prox version */
  919. static ssize_t prox_version_show(struct device *dev, struct device_attribute *attr, char *buf)
  920. {
  921. uint8_t partId = 0xff;
  922. uint8_t seqId = 0xff;
  923. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  924. mutex_lock(&glasshub->device_lock);
  925. if (boot_device_l(glasshub) == 0) {
  926. _i2c_read_reg8(glasshub, REG_PROX_PART_ID, &partId);
  927. _i2c_read_reg8(glasshub, REG_PROX_SEQ_ID, &seqId);
  928. }
  929. mutex_unlock(&glasshub->device_lock);
  930. return sprintf(buf, "0x%02x 0x%02x\n", partId, seqId);
  931. }
  932. /* show wink status (also clears the status) */
  933. static ssize_t wink_show(struct device *dev, struct device_attribute *attr, char *buf)
  934. {
  935. return show_reg(dev, buf, REG_WINK_STATUS);
  936. }
  937. /* show wink enable */
  938. static ssize_t wink_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  939. {
  940. return show_reg(dev, buf, REG_ENABLE_WINK);
  941. }
  942. /* enable/disable wink */
  943. static ssize_t wink_enable_store(struct device *dev, struct device_attribute *attr,
  944. const char *buf, size_t count)
  945. {
  946. return store_reg(dev, buf, count, REG_ENABLE_WINK, 0, 1);
  947. }
  948. /* show wink_flag_enable value */
  949. static ssize_t wink_flag_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  950. {
  951. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  952. return sprintf(buf, "%d\n", test_bit(FLAG_WINK_FLAG_ENABLE, &glasshub->flags) ? 1 : 0);
  953. }
  954. /* write wink_flag_enable value */
  955. static ssize_t wink_flag_enable_store(struct device *dev, struct device_attribute *attr,
  956. const char *buf, size_t count)
  957. {
  958. unsigned long value = 0;
  959. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  960. /* parse value */
  961. if (kstrtoul(buf, 10, &value)) {
  962. goto err_out;
  963. }
  964. /* disable device */
  965. if (value) {
  966. set_bit(FLAG_WINK_FLAG_ENABLE, &glasshub->flags);
  967. } else {
  968. clear_bit(FLAG_WINK_FLAG_ENABLE, &glasshub->flags);
  969. }
  970. err_out:
  971. return count;
  972. }
  973. /* show pause*/
  974. static ssize_t pause_show(struct device *dev, struct device_attribute *attr, char *buf)
  975. {
  976. return show_reg(dev, buf, REG_PAUSE);
  977. }
  978. /* set pause */
  979. static ssize_t pause_store(struct device *dev, struct device_attribute *attr,
  980. const char *buf, size_t count)
  981. {
  982. return store_reg(dev, buf, count, REG_PAUSE, 0, 1);
  983. }
  984. /* show wink minimum magnitude */
  985. static ssize_t wink_min_show(struct device *dev, struct device_attribute *attr, char *buf)
  986. {
  987. return show_reg(dev, buf, REG_WINK_MIN);
  988. }
  989. /* set wink minimum magnitude */
  990. static ssize_t wink_min_store(struct device *dev, struct device_attribute *attr,
  991. const char *buf, size_t count)
  992. {
  993. return store_reg(dev, buf, count, REG_WINK_MIN, 1, 255);
  994. }
  995. /* show wink maximum magnitude */
  996. static ssize_t wink_max_show(struct device *dev, struct device_attribute *attr, char *buf)
  997. {
  998. return show_reg(dev, buf, REG_WINK_MAX);
  999. }
  1000. /* set wink maximum magnitude */
  1001. static ssize_t wink_max_store(struct device *dev, struct device_attribute *attr,
  1002. const char *buf, size_t count)
  1003. {
  1004. return store_reg(dev, buf, count, REG_WINK_MAX, 1, 255);
  1005. }
  1006. /* show detector gain */
  1007. static ssize_t detector_gain_show(struct device *dev, struct device_attribute *attr, char *buf)
  1008. {
  1009. return show_reg(dev, buf, REG_DETECTOR_GAIN);
  1010. }
  1011. /* set detector gain */
  1012. static ssize_t detector_gain_store(struct device *dev, struct device_attribute *attr,
  1013. const char *buf, size_t count)
  1014. {
  1015. return store_reg(dev, buf, count, REG_DETECTOR_GAIN, 0x01, 0x80);
  1016. }
  1017. /* show detector bias */
  1018. static ssize_t detector_bias_show(struct device *dev, struct device_attribute *attr, char *buf)
  1019. {
  1020. return show_reg(dev, buf, REG16_DETECTOR_BIAS);
  1021. }
  1022. /* set detector bias */
  1023. static ssize_t detector_bias_store(struct device *dev, struct device_attribute *attr,
  1024. const char *buf, size_t count)
  1025. {
  1026. return store_reg(dev, buf, count, REG16_DETECTOR_BIAS, 1, 5000);
  1027. }
  1028. /* show last error code from device */
  1029. static ssize_t error_code_show(struct device *dev, struct device_attribute *attr, char *buf)
  1030. {
  1031. return show_reg(dev, buf, REG_ERROR_CODE);
  1032. }
  1033. /* show frame_count value */
  1034. static ssize_t frame_count_show(struct device *dev, struct device_attribute *attr, char *buf)
  1035. {
  1036. return show_reg(dev, buf, REG16_FRAME_COUNT);
  1037. }
  1038. /* show timer value */
  1039. static ssize_t timer_count_show(struct device *dev, struct device_attribute *attr, char *buf)
  1040. {
  1041. return show_reg(dev, buf, REG16_TIMER_COUNT);
  1042. }
  1043. /* show debug value */
  1044. static ssize_t mcu_debug_show(struct device *dev, struct device_attribute *attr, char *buf)
  1045. {
  1046. return show_reg(dev, buf, REG_DEBUG);
  1047. }
  1048. /* write debug value */
  1049. static ssize_t mcu_debug_store(struct device *dev, struct device_attribute *attr,
  1050. const char *buf, size_t count)
  1051. {
  1052. return store_reg(dev, buf, count, REG_DEBUG, 0, 255);
  1053. }
  1054. /* show debug value */
  1055. static ssize_t mcu_debug16_show(struct device *dev, struct device_attribute *attr, char *buf)
  1056. {
  1057. return show_reg(dev, buf, REG16_DEBUG);
  1058. }
  1059. /* write debug value */
  1060. static ssize_t mcu_debug16_store(struct device *dev, struct device_attribute *attr,
  1061. const char *buf, size_t count)
  1062. {
  1063. return store_reg(dev, buf, count, REG16_DEBUG, 0, 255);
  1064. }
  1065. /* show disable value */
  1066. static ssize_t disable_show(struct device *dev, struct device_attribute *attr, char *buf)
  1067. {
  1068. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1069. return sprintf(buf, "%d\n", test_bit(FLAG_DEVICE_DISABLED, &glasshub->flags) ? 1 : 0);
  1070. }
  1071. /* write disable value */
  1072. static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
  1073. const char *buf, size_t count)
  1074. {
  1075. unsigned long value = 0;
  1076. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1077. /* parse value */
  1078. if (kstrtoul(buf, 10, &value)) {
  1079. goto err_out;
  1080. }
  1081. /* disable device */
  1082. if (value) {
  1083. mutex_lock(&glasshub->device_lock);
  1084. set_bit(FLAG_DEVICE_DISABLED, &glasshub->flags);
  1085. reset_device_l(glasshub, 1);
  1086. mutex_unlock(&glasshub->device_lock);
  1087. } else {
  1088. clear_bit(FLAG_DEVICE_DISABLED, &glasshub->flags);
  1089. }
  1090. err_out:
  1091. return count;
  1092. }
  1093. /* sysfs node for updating device firmware */
  1094. static ssize_t update_fw_data_store(struct device *dev, struct device_attribute *attr,
  1095. const char *buf, size_t count);
  1096. /* hex converter for parsing .S19 files */
  1097. static int convert_hex(const char c, uint32_t *p)
  1098. {
  1099. if (!isxdigit(c)) return -1;
  1100. *p = (*p << 4) | (c <= '9' ? c - '0' : tolower(c) - 'a' + 10);
  1101. return 0;
  1102. }
  1103. /* helper function to get app version */
  1104. static int get_app_version_l(struct glasshub_data *glasshub)
  1105. {
  1106. int rc;
  1107. uint8_t buffer[2];
  1108. /* get current app version number */
  1109. buffer[0] = CMD_APP_VERSION;
  1110. rc = _i2c_read(glasshub, buffer, 1, buffer, sizeof(buffer));
  1111. if (rc) {
  1112. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  1113. dev_err(&glasshub->i2c_client->dev, "%s Error getting firmware version: %d\n",
  1114. __FUNCTION__, rc);
  1115. } else {
  1116. dev_info(&glasshub->i2c_client->dev, "Firmware version: %d.%d\n",
  1117. buffer[0], buffer[1]);
  1118. glasshub->app_version_major = buffer[0];
  1119. glasshub->app_version_minor = buffer[1];
  1120. }
  1121. return rc;
  1122. }
  1123. /* helper function to exit flash programming mode */
  1124. static void exit_flash_mode_l(struct glasshub_data *glasshub)
  1125. {
  1126. if (glasshub->fw_image) {
  1127. kfree(glasshub->fw_image);
  1128. glasshub->fw_image = NULL;
  1129. }
  1130. clear_bit(FLAG_FLASH_MODE, &glasshub->flags);
  1131. /* get app version number */
  1132. get_app_version_l(glasshub);
  1133. }
  1134. /* sysfs node to download device firmware to be flashed */
  1135. static ssize_t update_fw_data_store(struct device *dev, struct device_attribute *attr,
  1136. const char *buf, size_t count)
  1137. {
  1138. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1139. int i;
  1140. int pageNum;
  1141. #if DEBUG_FLASH_MODE
  1142. dev_info(&glasshub->i2c_client->dev, "%s Rx SREC %u bytes\n", __FUNCTION__, count);
  1143. #endif
  1144. mutex_lock(&glasshub->device_lock);
  1145. for (i = 0; i < count; i++) {
  1146. #if DEBUG_FLASH_MODE
  1147. dev_dbg(&glasshub->i2c_client->dev,
  1148. "%s SREC state = %d count = %d input = %c\n",
  1149. __FUNCTION__, glasshub->fw_state, glasshub->fw_count, buf[i]);
  1150. #endif
  1151. switch (glasshub->fw_state) {
  1152. case FW_STATE_START:
  1153. if (buf[i] == 'S') {
  1154. glasshub->fw_state = FW_STATE_TYPE;
  1155. #if DEBUG_FLASH_MODE
  1156. dev_dbg(&glasshub->i2c_client->dev,
  1157. "%s Start of SREC\n", __FUNCTION__);
  1158. #endif
  1159. }
  1160. break;
  1161. case FW_STATE_TYPE:
  1162. if (buf[i] < '0' || buf[i] > '9') {
  1163. glasshub->flash_status = FLASH_ERROR_INVALID_S_RECORD;
  1164. goto err_out;
  1165. }
  1166. /* process only S1-S3 records */
  1167. if (buf[i] >= '1' && buf[i] <= '3') {
  1168. #if DEBUG_FLASH_MODE
  1169. dev_dbg(&glasshub->i2c_client->dev,
  1170. "%s SREC type %c\n", __FUNCTION__, buf[i]);
  1171. #endif
  1172. glasshub->fw_rec_type = buf[i];
  1173. glasshub->fw_value = 0;
  1174. glasshub->fw_checksum = 0;
  1175. glasshub->fw_state = FW_STATE_COUNT_HI;
  1176. }
  1177. /* ignore other records */
  1178. else {
  1179. glasshub->fw_state = FW_STATE_START;
  1180. }
  1181. break;
  1182. case FW_STATE_ADDR_6:
  1183. case FW_STATE_ADDR_4:
  1184. case FW_STATE_ADDR_2:
  1185. if (convert_hex(buf[i], &glasshub->fw_value)) {
  1186. glasshub->flash_status = FLASH_ERROR_INVALID_HEX_DIGIT;
  1187. goto err_out;
  1188. }
  1189. glasshub->fw_checksum += glasshub->fw_value & 0xff;
  1190. glasshub->fw_count--;
  1191. glasshub->fw_state++;
  1192. break;
  1193. case FW_STATE_COUNT_HI:
  1194. case FW_STATE_ADDR_7:
  1195. case FW_STATE_ADDR_5:
  1196. case FW_STATE_ADDR_3:
  1197. case FW_STATE_ADDR_1:
  1198. case FW_STATE_BYTE_HI:
  1199. case FW_STATE_CHECKSUM_HI:
  1200. if (convert_hex(buf[i], &glasshub->fw_value)) {
  1201. glasshub->flash_status = FLASH_ERROR_INVALID_HEX_DIGIT;
  1202. goto err_out;
  1203. }
  1204. glasshub->fw_state++;
  1205. break;
  1206. case FW_STATE_COUNT_LO:
  1207. if (convert_hex(buf[i], &glasshub->fw_value)) {
  1208. glasshub->flash_status = FLASH_ERROR_INVALID_HEX_DIGIT;
  1209. goto err_out;
  1210. }
  1211. glasshub->fw_checksum += glasshub->fw_value & 0xff;
  1212. #if DEBUG_FLASH_MODE
  1213. dev_dbg(&glasshub->i2c_client->dev, "%s SREC byte count %u\n",
  1214. __FUNCTION__, glasshub->fw_value);
  1215. #endif
  1216. /* adjust count for address and checksum bytes */
  1217. glasshub->fw_count = glasshub->fw_value;
  1218. glasshub->fw_value = 0;
  1219. glasshub->fw_state = FW_STATE_ADDR_7 + 2 *
  1220. ('3' - glasshub->fw_rec_type);
  1221. break;
  1222. case FW_STATE_ADDR_0:
  1223. if (convert_hex(buf[i], &glasshub->fw_value)) {
  1224. glasshub->flash_status = FLASH_ERROR_INVALID_HEX_DIGIT;
  1225. goto err_out;
  1226. }
  1227. glasshub->fw_checksum += glasshub->fw_value & 0xff;
  1228. #if DEBUG_FLASH_MODE
  1229. dev_dbg(&glasshub->i2c_client->dev, "%s SREC address %04xh\n",
  1230. __FUNCTION__, glasshub->fw_value);
  1231. #endif
  1232. glasshub->fw_index = glasshub->fw_value - FIRMWARE_BASE_ADDRESS;
  1233. glasshub->fw_count--;
  1234. glasshub->fw_value = 0;
  1235. glasshub->fw_state = FW_STATE_BYTE_HI;
  1236. break;
  1237. case FW_STATE_BYTE_LO:
  1238. if (convert_hex(buf[i], &glasshub->fw_value)) {
  1239. glasshub->flash_status = FLASH_ERROR_INVALID_HEX_DIGIT;
  1240. goto err_out;
  1241. }
  1242. glasshub->fw_checksum += glasshub->fw_value & 0xff;
  1243. /* validate address */
  1244. if (glasshub->fw_index < 0 ||
  1245. glasshub->fw_index > FIRMWARE_TOTAL_SIZE) {
  1246. dev_err(&glasshub->i2c_client->dev,
  1247. "%s Address out of range: address %u count=%u\n",
  1248. __FUNCTION__, glasshub->fw_value,
  1249. glasshub->fw_count);
  1250. glasshub->flash_status = FLASH_ERROR_ADDRESS_RANGE;
  1251. goto err_out;
  1252. }
  1253. /* store byte in image buffer */
  1254. glasshub->fw_image[glasshub->fw_index] = glasshub->fw_value;
  1255. glasshub->fw_value = 0;
  1256. pageNum = glasshub->fw_index / FIRMWARE_PAGE_SIZE;
  1257. glasshub->fw_dirty[pageNum / 32] |= (1 << (pageNum & 31));
  1258. glasshub->fw_index++;
  1259. glasshub->fw_state = FW_STATE_BYTE_HI;
  1260. /* check for end of data */
  1261. if (--glasshub->fw_count == 1) {
  1262. #if DEBUG_FLASH_MODE
  1263. dev_dbg(&glasshub->i2c_client->dev,
  1264. "%s SREC all bytes received\n",
  1265. __FUNCTION__);
  1266. #endif
  1267. glasshub->fw_state = FW_STATE_CHECKSUM_HI;
  1268. }
  1269. break;
  1270. case FW_STATE_CHECKSUM_LO:
  1271. if (convert_hex(buf[i], &glasshub->fw_value)) {
  1272. glasshub->flash_status = FLASH_ERROR_INVALID_HEX_DIGIT;
  1273. goto err_out;
  1274. }
  1275. if (glasshub->fw_value != (~glasshub->fw_checksum & 0xff)) {
  1276. dev_err(&glasshub->i2c_client->dev,
  1277. "%s SREC checksum mismatch %02xh != %02xh\n",
  1278. __FUNCTION__,
  1279. (unsigned)~glasshub->fw_checksum & 0xff,
  1280. (unsigned)glasshub->fw_value);
  1281. glasshub->flash_status = FLASH_ERROR_SREC_CHECKSUM_ERROR;
  1282. goto err_out;
  1283. }
  1284. #if DEBUG_FLASH_MODE
  1285. dev_dbg(&glasshub->i2c_client->dev, "%s SREC checksum OK\n",
  1286. __FUNCTION__);
  1287. #endif
  1288. glasshub->fw_state = FW_STATE_START;
  1289. break;
  1290. }
  1291. }
  1292. mutex_unlock(&glasshub->device_lock);
  1293. return count;
  1294. err_out:
  1295. exit_flash_mode_l(glasshub);
  1296. mutex_unlock(&glasshub->device_lock);
  1297. dev_err(&glasshub->i2c_client->dev, "%s Not an SREC\n", __FUNCTION__);
  1298. return -EINVAL;
  1299. }
  1300. /* sysfs node to enter/exit flash programming mode */
  1301. static ssize_t update_fw_enable_store(struct device *dev, struct device_attribute *attr,
  1302. const char *buf, size_t count)
  1303. {
  1304. unsigned long value = 0;
  1305. int rc = 0;
  1306. int bootflasher = 0;
  1307. int old_boot;
  1308. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1309. if (kstrtoul(buf, 10, &value)) {
  1310. goto err_out;
  1311. }
  1312. #if DEBUG_FLASH_MODE
  1313. dev_info(&glasshub->i2c_client->dev, "%s update_fw_enable = %lu\n", __FUNCTION__, value);
  1314. #endif
  1315. mutex_lock(&glasshub->device_lock);
  1316. /* bootloader version 3 does not support checksum */
  1317. old_boot = (glasshub->bootloader_version < 4) ? 1 : 0;
  1318. /* handle special bootflasher case */
  1319. if ((value == BOOTLOADER_FLASHER) && (glasshub->app_version_major == BOOTLOADER_FLASHER)) {
  1320. bootflasher = 1;
  1321. old_boot = 0;
  1322. value = 0;
  1323. }
  1324. /* enable firmware flash */
  1325. if (value) {
  1326. if (!test_bit(FLAG_FLASH_MODE, &glasshub->flags)) {
  1327. /* allocate memory and clear dirty bits */
  1328. if (!glasshub->fw_image) {
  1329. glasshub->fw_image = kzalloc(FIRMWARE_TOTAL_SIZE, GFP_USER);
  1330. if (!glasshub->fw_image) {
  1331. dev_err(&glasshub->i2c_client->dev,
  1332. "%s failed to allocate memory for firmware image\n",
  1333. __FUNCTION__);
  1334. goto unlock;
  1335. }
  1336. memset(glasshub->fw_dirty, 0, sizeof(glasshub->fw_dirty));
  1337. }
  1338. glasshub->fw_state = FW_STATE_START;
  1339. set_bit(FLAG_FLASH_MODE, &glasshub->flags);
  1340. glasshub->flash_status = FLASH_STATUS_READY;
  1341. }
  1342. } else {
  1343. /* exit flash, write code to device */
  1344. if (test_bit(FLAG_FLASH_MODE, &glasshub->flags)) {
  1345. /* put device into bootloader mode */
  1346. rc = reset_device_l(glasshub, 0);
  1347. if (rc) {
  1348. glasshub->flash_status = FLASH_ERROR_DEVICE_RESET_ERROR;
  1349. goto ExitFlashMode;
  1350. }
  1351. /* if flashing boot code, boot into application code */
  1352. if (bootflasher) {
  1353. rc = boot_device_l(glasshub);
  1354. if (rc) {
  1355. glasshub->flash_status = FLASH_ERROR_DEVICE_BOOT_ERROR;
  1356. goto ExitFlashMode;
  1357. }
  1358. }
  1359. /* here's where we flash the image if it has dirty bits */
  1360. if (glasshub->fw_image) {
  1361. int pagesFlashed = 0;
  1362. int page;
  1363. uint8_t checksum;
  1364. #if DEBUG_FLASH_MODE
  1365. dev_info(&glasshub->i2c_client->dev,
  1366. "Update pages: %08x %08x %08x %08x\n",
  1367. glasshub->fw_dirty[0],
  1368. glasshub->fw_dirty[1],
  1369. glasshub->fw_dirty[2],
  1370. glasshub->fw_dirty[3]);
  1371. #endif
  1372. for (page = FIRMWARE_TOTAL_PAGES - 1; page >= 0; page--) {
  1373. /* flash only dirty pages */
  1374. if (glasshub->fw_dirty[page / 32] &
  1375. (1 << (page & 31))) {
  1376. uint8_t buffer[FIRMWARE_PAGE_SIZE+3];
  1377. int i, j;
  1378. /* initalize command and page number */
  1379. buffer[0] = CMD_FLASH;
  1380. buffer[1] = page;
  1381. /* seed checksum */
  1382. checksum = 0xa5;
  1383. /* copy firmware into buffer */
  1384. for (i = 2, j = page * FIRMWARE_PAGE_SIZE;
  1385. i < FIRMWARE_PAGE_SIZE + 2;
  1386. i++, j++) {
  1387. buffer[i] = glasshub->fw_image[j];
  1388. checksum = (checksum << 1) ^ buffer[i] ^ (checksum >> 7);
  1389. }
  1390. /* add checksum to buffer */
  1391. buffer[66] = checksum;
  1392. #if DEBUG_FLASH_MODE
  1393. dev_info(&glasshub->i2c_client->dev,
  1394. "%s: Flash page %d\n",
  1395. __FUNCTION__, page);
  1396. #endif
  1397. /* don't send checksum for older bootloader version */
  1398. rc = _i2c_write_mult(glasshub, buffer,
  1399. sizeof(buffer) - old_boot);
  1400. if (rc) {
  1401. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  1402. dev_err(&glasshub->i2c_client->dev,
  1403. "%s Unable to flash glasshub device\n",
  1404. __FUNCTION__);
  1405. glasshub->flash_status = FLASH_ERROR_I2C_DEVICE_COMM;
  1406. goto ExitFlashMode;
  1407. }
  1408. /* new bootloader: check status for error */
  1409. if (!old_boot) {
  1410. buffer[0] = CMD_FLASH_STATUS;
  1411. rc = _i2c_read(glasshub, buffer, 1, buffer, 1);
  1412. if (rc) {
  1413. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  1414. dev_err(&glasshub->i2c_client->dev,
  1415. "%s Error reading flash status\n",
  1416. __FUNCTION__);
  1417. glasshub->flash_status = FLASH_ERROR_I2C_DEVICE_COMM;
  1418. goto ExitFlashMode;
  1419. } else if (buffer[0] != 0) {
  1420. dev_err(&glasshub->i2c_client->dev,
  1421. "%s Device rejected packet: %u\n",
  1422. __FUNCTION__, buffer[0]);
  1423. glasshub->flash_status = FLASH_ERROR_DEV_REJECTED_PKT;
  1424. goto ExitFlashMode;
  1425. }
  1426. }
  1427. ++pagesFlashed;
  1428. }
  1429. }
  1430. dev_info(&glasshub->i2c_client->dev,
  1431. "%s: %d code page(s) flashed\n",
  1432. __FUNCTION__, pagesFlashed);
  1433. glasshub->flash_status = pagesFlashed ? FLASH_STATUS_OK : FLASH_ERROR_NO_PAGES_FLASHED;
  1434. }
  1435. ExitFlashMode:
  1436. if (bootflasher) {
  1437. uint8_t buffer[1];
  1438. /* put device back into bootloader mode */
  1439. rc = reset_device_l(glasshub, 1);
  1440. if (rc) {
  1441. glasshub->flash_status = FLASH_ERROR_DEVICE_RESET_ERROR;
  1442. goto ExitFlashMode;
  1443. }
  1444. /* get new bootloader version */
  1445. buffer[0] = CMD_BOOTLOADER_VERSION;
  1446. rc = _i2c_read(glasshub, buffer, 1, buffer, 1);
  1447. if (rc) {
  1448. glasshub->flash_status = FLASH_ERROR_BOOTLOADER_VERSION;
  1449. goto ExitFlashMode;
  1450. }
  1451. glasshub->bootloader_version = buffer[0];
  1452. }
  1453. clear_bit(FLAG_FLASH_MODE, &glasshub->flags);
  1454. }
  1455. }
  1456. unlock:
  1457. if (!test_bit(FLAG_FLASH_MODE, &glasshub->flags) && glasshub->fw_image) {
  1458. exit_flash_mode_l(glasshub);
  1459. }
  1460. mutex_unlock(&glasshub->device_lock);
  1461. err_out:
  1462. return rc < 0 ? rc : count;
  1463. }
  1464. /* show state of firmware update flag */
  1465. static ssize_t update_fw_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  1466. {
  1467. int enable;
  1468. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1469. enable = test_bit(FLAG_FLASH_MODE, &glasshub->flags) ? 1 : 0;
  1470. return sprintf(buf, "%d\n", enable);
  1471. }
  1472. /* show state of IRQ */
  1473. static ssize_t irq_show(struct device *dev, struct device_attribute *attr, char *buf)
  1474. {
  1475. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1476. return sprintf(buf, "%d\n", gpio_get_value(glasshub->pdata->gpio_int_no));
  1477. }
  1478. /* show state of flash process */
  1479. static ssize_t flash_status_show(struct device *dev, struct device_attribute *attr, char *buf)
  1480. {
  1481. int temp;
  1482. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1483. temp = glasshub->flash_status;
  1484. glasshub->flash_status = 0;
  1485. return sprintf(buf, "%d\n", temp);
  1486. }
  1487. /* show count of received samples */
  1488. static ssize_t sample_count_show(struct device *dev, struct device_attribute *attr, char *buf)
  1489. {
  1490. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1491. return sprintf(buf, "%lu\n", glasshub->sample_count);
  1492. }
  1493. /* show debug value */
  1494. static ssize_t debug_show(struct device *dev, struct device_attribute *attr, char *buf)
  1495. {
  1496. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1497. return sprintf(buf, "%d\n", glasshub->debug);
  1498. }
  1499. /* write debug value */
  1500. static ssize_t debug_store(struct device *dev, struct device_attribute *attr,
  1501. const char *buf, size_t count)
  1502. {
  1503. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1504. unsigned long value = 0;
  1505. if (!kstrtoul(buf, 10, &value)) {
  1506. glasshub->debug = value ? 1 : 0;
  1507. }
  1508. return count;
  1509. }
  1510. /* timestamp of last IRQ */
  1511. static ssize_t irq_timestamp_show(struct device *dev, struct device_attribute *attr, char *buf)
  1512. {
  1513. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1514. return sprintf(buf, "%llu\n", glasshub->irq_timestamp);
  1515. }
  1516. /* timestamp of last sample */
  1517. static ssize_t last_timestamp_show(struct device *dev, struct device_attribute *attr, char *buf)
  1518. {
  1519. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1520. return sprintf(buf, "%llu\n", glasshub->last_timestamp);
  1521. }
  1522. /* show current IRQ status without clearing IRQ */
  1523. static ssize_t irq_status_show(struct device *dev, struct device_attribute *attr, char *buf)
  1524. {
  1525. unsigned value = 0xffff;
  1526. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1527. mutex_lock(&glasshub->device_lock);
  1528. if (boot_device_l(glasshub) == 0) {
  1529. if (_i2c_read_reg(glasshub, REG_STATUS_READ_ONLY, &value)) {
  1530. set_bit(FLAG_DEVICE_MAY_BE_WEDGED, &glasshub->flags);
  1531. }
  1532. }
  1533. mutex_unlock(&glasshub->device_lock);
  1534. return sprintf(buf, "0x%02x\n", value);
  1535. }
  1536. /* show last IRQ status */
  1537. static ssize_t last_irq_status_show(struct device *dev, struct device_attribute *attr, char *buf)
  1538. {
  1539. struct glasshub_data *glasshub = dev_get_drvdata(dev);
  1540. return sprintf(buf, "0x%02x\n", glasshub->last_irq_status);
  1541. }
  1542. static DEVICE_ATTR(update_fw_enable, DEV_MODE_RW, update_fw_enable_show, update_fw_enable_store);
  1543. static DEVICE_ATTR(update_fw_data, DEV_MODE_WO, NULL, update_fw_data_store);
  1544. static DEVICE_ATTR(version, DEV_MODE_RO, version_show, NULL);
  1545. static DEVICE_ATTR(bootloader_version, DEV_MODE_RO, bootloader_version_show, NULL);
  1546. static DEVICE_ATTR(driver_version, DEV_MODE_RO, driver_version_show, NULL);
  1547. static DEVICE_ATTR(driver_flags, DEV_MODE_RO, driver_flags_show, NULL);
  1548. static DEVICE_ATTR(passthru_enable, DEV_MODE_RW, passthru_enable_show, passthru_enable_store);
  1549. static DEVICE_ATTR(proxraw, DEV_MODE_RO, proxraw_show, NULL);
  1550. static DEVICE_ATTR(proxmin, DEV_MODE_RO, proxmin_show, NULL);
  1551. static DEVICE_ATTR(ambient_enable, DEV_MODE_RW, ambient_enable_show, ambient_enable_store);
  1552. static DEVICE_ATTR(visible, DEV_MODE_RO, visible_show, NULL);
  1553. static DEVICE_ATTR(ir, DEV_MODE_RO, ir_show, NULL);
  1554. static DEVICE_ATTR(don_doff_enable, DEV_MODE_RW, don_doff_enable_show, don_doff_enable_store);
  1555. static DEVICE_ATTR(don_doff, DEV_MODE_RO, don_doff_show, NULL);
  1556. static DEVICE_ATTR(don_doff_threshold, DEV_MODE_RW, don_doff_threshold_show, don_doff_threshold_store);
  1557. static DEVICE_ATTR(don_threshold, DEV_MODE_RW, don_threshold_show, don_threshold_store);
  1558. static DEVICE_ATTR(doff_threshold, DEV_MODE_RW, doff_threshold_show, doff_threshold_store);
  1559. static DEVICE_ATTR(don_doff_hysteresis, DEV_MODE_RW, don_doff_hysteresis_show, don_doff_hysteresis_store);
  1560. static DEVICE_ATTR(led_drive, DEV_MODE_RW, led_drive_show, led_drive_store);
  1561. static DEVICE_ATTR(calibrate, DEV_MODE_WO, NULL, calibrate_store);
  1562. static DEVICE_ATTR(calibration_values, DEV_MODE_RO, calibration_values_show, NULL);
  1563. static DEVICE_ATTR(prox_version, DEV_MODE_RO, prox_version_show, NULL);
  1564. static DEVICE_ATTR(wink, DEV_MODE_RO, wink_show, NULL);
  1565. static DEVICE_ATTR(wink_enable, DEV_MODE_RW, wink_enable_show, wink_enable_store);
  1566. static DEVICE_ATTR(pause, DEV_MODE_RW, pause_show, pause_store);
  1567. static DEVICE_ATTR(wink_flag_enable, DEV_MODE_RW, wink_flag_enable_show, wink_flag_enable_store);
  1568. static DEVICE_ATTR(wink_min, DEV_MODE_RW, wink_min_show, wink_min_store);
  1569. static DEVICE_ATTR(wink_max, DEV_MODE_RW, wink_max_show, wink_max_store);
  1570. static DEVICE_ATTR(detector_gain, DEV_MODE_RW, detector_gain_show, detector_gain_store);
  1571. static DEVICE_ATTR(detector_bias, DEV_MODE_RW, detector_bias_show, detector_bias_store);
  1572. static DEVICE_ATTR(mcu_debug, DEV_MODE_RW, mcu_debug_show, mcu_debug_store);
  1573. static DEVICE_ATTR(mcu_debug16, DEV_MODE_RW, mcu_debug16_show, mcu_debug16_store);
  1574. static DEVICE_ATTR(error_code, DEV_MODE_RO, error_code_show, NULL);
  1575. static DEVICE_ATTR(irq, DEV_MODE_RO, irq_show, NULL);
  1576. static DEVICE_ATTR(flash_status, DEV_MODE_RO, flash_status_show, NULL);
  1577. static DEVICE_ATTR(sample_count, DEV_MODE_RO, sample_count_show, NULL);
  1578. static DEVICE_ATTR(disable, DEV_MODE_RW, disable_show, disable_store);
  1579. static DEVICE_ATTR(debug, DEV_MODE_RW, debug_show, debug_store);
  1580. static DEVICE_ATTR(frame_count, DEV_MODE_RO, frame_count_show, NULL);
  1581. static DEVICE_ATTR(timer_count, DEV_MODE_RO, timer_count_show, NULL);
  1582. static DEVICE_ATTR(irq_timestamp, DEV_MODE_RO, irq_timestamp_show, NULL);
  1583. static DEVICE_ATTR(last_timestamp, DEV_MODE_RO, last_timestamp_show, NULL);
  1584. static DEVICE_ATTR(irq_status, DEV_MODE_RO, irq_status_show, NULL);
  1585. static DEVICE_ATTR(last_irq_status, DEV_MODE_RO, last_irq_status_show, NULL);
  1586. static struct attribute *attrs[] = {
  1587. &dev_attr_passthru_enable.attr,
  1588. &dev_attr_proxraw.attr,
  1589. &dev_attr_proxmin.attr,
  1590. &dev_attr_ir.attr,
  1591. &dev_attr_visible.attr,
  1592. &dev_attr_ambient_enable.attr,
  1593. &dev_attr_don_doff_enable.attr,
  1594. &dev_attr_don_doff.attr,
  1595. &dev_attr_don_doff_threshold.attr,
  1596. &dev_attr_don_threshold.attr,
  1597. &dev_attr_doff_threshold.attr,
  1598. &dev_attr_don_doff_hysteresis.attr,
  1599. &dev_attr_led_drive.attr,
  1600. &dev_attr_calibrate.attr,
  1601. &dev_attr_calibration_values.attr,
  1602. &dev_attr_prox_version.attr,
  1603. &dev_attr_wink.attr,
  1604. &dev_attr_wink_enable.attr,
  1605. &dev_attr_pause.attr,
  1606. &dev_attr_wink_min.attr,
  1607. &dev_attr_wink_max.attr,
  1608. &dev_attr_wink_flag_enable.attr,
  1609. &dev_attr_detector_gain.attr,
  1610. &dev_attr_detector_bias.attr,
  1611. &dev_attr_mcu_debug.attr,
  1612. &dev_attr_mcu_debug16.attr,
  1613. &dev_attr_error_code.attr,
  1614. &dev_attr_sample_count.attr,
  1615. &dev_attr_frame_count.attr,
  1616. &dev_attr_timer_count.attr,
  1617. &dev_attr_irq_timestamp.attr,
  1618. &dev_attr_last_timestamp.attr,
  1619. &dev_attr_irq_status.attr,
  1620. &dev_attr_last_irq_status.attr,
  1621. NULL
  1622. };
  1623. static struct attribute_group attr_group = {
  1624. .attrs = attrs,
  1625. };
  1626. static struct attribute *bootmode_attrs[] = {
  1627. &dev_attr_bootloader_version.attr,
  1628. &dev_attr_version.attr,
  1629. &dev_attr_driver_version.attr,
  1630. &dev_attr_driver_flags.attr,
  1631. &dev_attr_update_fw_enable.attr,
  1632. &dev_attr_update_fw_data.attr,
  1633. &dev_attr_flash_status.attr,
  1634. &dev_attr_irq.attr,
  1635. &dev_attr_disable.attr,
  1636. &dev_attr_debug.attr,
  1637. NULL
  1638. };
  1639. static struct attribute_group bootmode_attr_group = {
  1640. .attrs = bootmode_attrs,
  1641. };
  1642. static int flush_prox_fifo(struct glasshub_data *glasshub)
  1643. {
  1644. if (mutex_lock_interruptible(&glasshub->device_lock)) {
  1645. dev_err(&glasshub->i2c_client->dev,
  1646. "%s: Unable to acquire device mutex\n", __func__);
  1647. return -EAGAIN;
  1648. }
  1649. kfifo_reset(&prox_fifo);
  1650. mutex_unlock(&glasshub->device_lock);
  1651. return 0;
  1652. }
  1653. /* prox sensor open fops */
  1654. static int glasshub_open(struct inode *inode, struct file *file)
  1655. {
  1656. if (atomic_xchg(&glasshub_opened, 1) != 0) {
  1657. return -EBUSY;
  1658. }
  1659. file->private_data = (void*)glasshub_private;
  1660. return flush_prox_fifo(glasshub_private);
  1661. }
  1662. /* prox sensor release fops */
  1663. static int glasshub_release(struct inode *inode, struct file *file)
  1664. {
  1665. struct glasshub_data *glasshub = (struct glasshub_data *)file->private_data;
  1666. if (atomic_xchg(&glasshub_opened, 0) == 0) {
  1667. dev_err(&glasshub->i2c_client->dev, "%s: Device has not been opened\n", __func__);
  1668. return -EINVAL;
  1669. }
  1670. return 0;
  1671. }
  1672. /* prox sensor read function */
  1673. static ssize_t glasshub_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1674. {
  1675. struct glasshub_data *glasshub = (struct glasshub_data *)file->private_data;
  1676. unsigned int copied = 0;
  1677. int rc = 0;
  1678. /* validate parameters */
  1679. count = (count / sizeof(struct glasshub_data_user)) * sizeof(struct glasshub_data_user);
  1680. if (count < sizeof(struct glasshub_data_user)) {
  1681. dev_err(&glasshub->i2c_client->dev,
  1682. "%s: Invalid data size\n", __func__);
  1683. return -EINVAL;
  1684. }
  1685. /* read from FIFO, blocking if no data */
  1686. while (1) {
  1687. if (mutex_lock_interruptible(&glasshub->device_lock)) {
  1688. dev_err(&glasshub->i2c_client->dev,
  1689. "%s: Unable to acquire device mutex\n", __func__);
  1690. return -EAGAIN;
  1691. }
  1692. rc = kfifo_to_user(&prox_fifo, buf, count, &copied);
  1693. if ((rc < 0) || (copied > 0)) break;
  1694. mutex_unlock(&glasshub->device_lock);
  1695. rc = wait_event_interruptible(prox_read_wait, !kfifo_is_empty(&prox_fifo));
  1696. }
  1697. mutex_unlock(&glasshub->device_lock);
  1698. return copied ? copied : rc;
  1699. }
  1700. static loff_t glasshub_llseek(struct file *file, loff_t offset, int whence)
  1701. {
  1702. if ((offset == 0) && (whence == SEEK_END)) {
  1703. struct glasshub_data *glasshub = (struct glasshub_data *)file->private_data;
  1704. return flush_prox_fifo(glasshub);
  1705. }
  1706. return -EINVAL;
  1707. }
  1708. static unsigned int glasshub_poll(struct file *file, struct poll_table_struct *poll_table)
  1709. {
  1710. struct glasshub_data *glasshub = (struct glasshub_data *)file->private_data;
  1711. unsigned int ret = 0;
  1712. if (mutex_lock_interruptible(&glasshub->device_lock)) {
  1713. dev_err(&glasshub->i2c_client->dev,
  1714. "%s: Unable to acquire device mutex\n", __func__);
  1715. ret |= POLLERR;
  1716. goto Exit;
  1717. }
  1718. if (kfifo_len(&prox_fifo)) {
  1719. ret |= POLLIN|POLLRDNORM;
  1720. } else {
  1721. poll_wait(file, &prox_read_wait, poll_table);
  1722. }
  1723. mutex_unlock(&glasshub->device_lock);
  1724. Exit:
  1725. return ret;
  1726. }
  1727. static const struct file_operations glasshub_fops = {
  1728. .owner = THIS_MODULE,
  1729. .open = glasshub_open,
  1730. .release = glasshub_release,
  1731. .read = glasshub_read,
  1732. .llseek = glasshub_llseek,
  1733. .poll = glasshub_poll,
  1734. };
  1735. struct miscdevice glasshub_misc = {
  1736. .minor = MISC_DYNAMIC_MINOR,
  1737. .name = "glasshub",
  1738. .fops = &glasshub_fops
  1739. };
  1740. /* register device files */
  1741. static int register_device_files(struct glasshub_data *glasshub)
  1742. {
  1743. int rc = 0;
  1744. unsigned app_version;
  1745. /* if special boot flasher, don't register sysfs files */
  1746. if (glasshub->app_version_major == BOOTLOADER_FLASHER) return rc;
  1747. /* check app code version */
  1748. app_version = ((unsigned) glasshub->app_version_major << 8) | glasshub->app_version_minor;
  1749. /* make sure we have a supported firmware build on the MCU */
  1750. if (app_version < MINIMUM_MCU_VERSION) {
  1751. dev_warn(&glasshub->i2c_client->dev,
  1752. "%s: WARNING: MCU application code is down-rev: %u.%u\n",
  1753. __FUNCTION__,
  1754. glasshub->app_version_major,
  1755. glasshub->app_version_minor);
  1756. dev_warn(&glasshub->i2c_client->dev,
  1757. "%s: All functions except firmware update are disabled\n",
  1758. __FUNCTION__);
  1759. /* set device disable bit */
  1760. set_bit(FLAG_DEVICE_DISABLED, &glasshub->flags);
  1761. }
  1762. /* warn if experimental version */
  1763. if (app_version >= EXPERIMENTAL_MCU_VERSION) {
  1764. dev_warn(&glasshub->i2c_client->dev,
  1765. "%s: WARNING: MCU application code is experimental version: %u.%u\n",
  1766. __FUNCTION__,
  1767. glasshub->app_version_major,
  1768. glasshub->app_version_minor);
  1769. }
  1770. /* are sysfs files already created? */
  1771. if (test_and_set_bit(FLAG_SYSFS_CREATED, &glasshub->flags)) goto Exit;
  1772. /* create attributes */
  1773. rc = sysfs_create_group(&glasshub->i2c_client->dev.kobj, &attr_group);
  1774. if (rc) {
  1775. dev_err(&glasshub->i2c_client->dev, "%s Unable to create sysfs class files\n",
  1776. __FUNCTION__);
  1777. goto Exit;
  1778. }
  1779. /* register misc driver for prox data */
  1780. rc = misc_register(&glasshub_misc);
  1781. if (rc) {
  1782. dev_err(&glasshub->i2c_client->dev, "%s Unable to create misc driver\n",
  1783. __FUNCTION__);
  1784. goto Exit;
  1785. }
  1786. Exit:
  1787. return rc;
  1788. }
  1789. static int glasshub_setup(struct glasshub_data *glasshub) {
  1790. int rc = 0;
  1791. uint8_t buffer;
  1792. /* force glass hub reset */
  1793. rc = reset_device_l(glasshub, 1);
  1794. /* get bootloader version */
  1795. buffer = CMD_BOOTLOADER_VERSION;
  1796. rc = _i2c_read(glasshub, &buffer, sizeof(buffer), &buffer, sizeof(buffer));
  1797. if (rc) goto err_out;
  1798. glasshub->bootloader_version = buffer;
  1799. /* get app version number */
  1800. rc = get_app_version_l(glasshub);
  1801. if (rc) goto err_out;
  1802. /* check bootloader version */
  1803. /* TODO: Abort on anything less than V3 once we retire Joey devices */
  1804. if (glasshub->bootloader_version < 3) {
  1805. dev_info(&glasshub->i2c_client->dev,
  1806. "%s: WARNING: MCU bootloader is down-rev: %u\n",
  1807. __FUNCTION__, glasshub->bootloader_version);
  1808. /* this version is completely borked */
  1809. if (glasshub->bootloader_version == 2) {
  1810. rc = -EIO;
  1811. goto err_out;
  1812. }
  1813. }
  1814. /* request IRQ */
  1815. rc = request_threaded_irq(glasshub->pdata->irq, glasshub_irq_handler,
  1816. glasshub_threaded_irq_handler, IRQF_TRIGGER_FALLING,
  1817. "glasshub_irq", glasshub);
  1818. if (rc) {
  1819. dev_err(&glasshub->i2c_client->dev, "%s request_threaded_irq failed\n",
  1820. __FUNCTION__);
  1821. }
  1822. /* Allow this interrupt to wake the system */
  1823. rc = irq_set_irq_wake(glasshub->pdata->irq, 1);
  1824. if (rc) {
  1825. dev_err(&glasshub->i2c_client->dev, "%s irq_set_irq_wake failed\n", __FUNCTION__);
  1826. }
  1827. err_out:
  1828. return rc;
  1829. }
  1830. static int __devinit glasshub_probe(struct i2c_client *i2c_client,
  1831. const struct i2c_device_id *id)
  1832. {
  1833. struct glasshub_data *glasshub = NULL;
  1834. int rc;
  1835. dev_info(&i2c_client->dev, "%s: Probing glasshub...\n", __FUNCTION__);
  1836. glasshub = kzalloc(sizeof(struct glasshub_data), GFP_KERNEL);
  1837. if (!glasshub)
  1838. {
  1839. dev_err(&i2c_client->dev, "%s: Unable to allocate memory for driver structure\n",
  1840. __FUNCTION__);
  1841. return -ENOMEM;
  1842. }
  1843. /* initialize data structure */
  1844. glasshub->i2c_client = i2c_client;
  1845. glasshub->flags = 0;
  1846. glasshub->average_delta = PROX_INTERVAL;
  1847. mutex_init(&glasshub->device_lock);
  1848. /* Set platform defaults */
  1849. glasshub->pdata = (const struct glasshub_platform_data *)i2c_client->dev.platform_data;
  1850. if (!glasshub->pdata) {
  1851. dev_err(&i2c_client->dev, "%s: Platform data has not been set\n", __FUNCTION__);
  1852. goto err_out;
  1853. }
  1854. /* setup the device */
  1855. if (glasshub_setup(glasshub)) {
  1856. goto err_out;
  1857. }
  1858. /* store driver data into device private structure */
  1859. dev_set_drvdata(&i2c_client->dev, glasshub);
  1860. glasshub_private = glasshub;
  1861. /* initialize prox data KFIFO */
  1862. INIT_KFIFO(prox_fifo);
  1863. /* create bootmode sysfs files */
  1864. rc = sysfs_create_group(&glasshub->i2c_client->dev.kobj, &bootmode_attr_group);
  1865. if (rc) {
  1866. dev_err(&glasshub->i2c_client->dev, "%s Unable to create sysfs class files\n",
  1867. __FUNCTION__);
  1868. goto err_out;
  1869. }
  1870. /* create sysfs files unless running the special bootflasher app */
  1871. if (glasshub->app_version_major != BOOTLOADER_FLASHER) {
  1872. rc = register_device_files(glasshub);
  1873. if (rc) {
  1874. dev_err(&glasshub->i2c_client->dev, "%s Unable to create sysfs class files\n",
  1875. __FUNCTION__);
  1876. goto err_out;
  1877. }
  1878. }
  1879. dev_info(&i2c_client->dev, "%s: Probe successful\n", __FUNCTION__);
  1880. return 0;
  1881. err_out:
  1882. dev_err(&i2c_client->dev, "%s: Probe failed\n", __FUNCTION__);
  1883. kfree(glasshub);
  1884. return -ENODEV;
  1885. }
  1886. static const struct i2c_device_id glasshub_id[] = {
  1887. { DEVICE_NAME, 0 },
  1888. };
  1889. static struct i2c_driver glasshub_driver = {
  1890. .probe = glasshub_probe,
  1891. .id_table = glasshub_id,
  1892. .driver = {
  1893. .owner = THIS_MODULE,
  1894. .name = DEVICE_NAME,
  1895. },
  1896. };
  1897. static int __init glasshub_init(void)
  1898. {
  1899. return i2c_add_driver(&glasshub_driver);
  1900. }
  1901. static void __exit glasshub_exit(void)
  1902. {
  1903. i2c_del_driver(&glasshub_driver);
  1904. }
  1905. module_init(glasshub_init);
  1906. module_exit(glasshub_exit);
  1907. MODULE_AUTHOR("davidsparks@google.com");
  1908. MODULE_DESCRIPTION("Glass Hub Driver");
  1909. MODULE_LICENSE("Dual BSD/GPL");
  1910. MODULE_VERSION(DRIVER_VERSION);