Guest User

Untitled

a guest
Oct 19th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.84 KB | None | 0 0
  1. /* --------------------------------------------------------------------------
  2. * @license_begin
  3. *
  4. * Nintendo CLV/Wii Classic/Wii Pro controllers I2C driver
  5. * Copyright (C) 2016 Nintendo Co. Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * @license_end
  21. * ----------------------------------------------------------------------- */
  22.  
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/errno.h>
  27. #include <linux/i2c.h>
  28. #include <linux/input.h>
  29. #include <linux/input-polldev.h>
  30. #include <linux/delay.h>
  31. #include <linux/gpio.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/irq.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/mutex.h>
  36. #include <linux/bug.h>
  37.  
  38. MODULE_AUTHOR("Nintendo Co., Ltd");
  39. MODULE_DESCRIPTION("Nintendo CLV-001/CLV-002/Wii Classic/Wii Pro controllers on I2C");
  40.  
  41. MODULE_LICENSE("GPL");
  42.  
  43. #define DRV_NAME "clvcon"
  44.  
  45. #define CLASSIC_ID 0
  46. #define CONTROLLER_I2C_ADDRESS 0x52
  47. //Rounded up to multiple of 1/HZ
  48. #define POLL_INTERVAL 5
  49.  
  50. // Confirm bit flips before reporting digital button values
  51. // Beware this implies a one polling interval of input lag
  52. #define FILTER_DIGITAL_BUTTON 0
  53.  
  54. /* HOME button is to be reported only after these many successful polling
  55. * positives.
  56. * Keep it at ~0.1s, adapt whenever POLL_INTERVAL is changed */
  57. #define HOME_BUTTON_THRESHOLD 15
  58.  
  59. //Delay expressed in polling intervals
  60. #define RETRY_BASE_DELAY 512
  61.  
  62. #define DATA_FORMAT 3
  63.  
  64. #define DF3_BTN_R 1
  65. #define DF3_BTN_START 2
  66. #define DF3_BTN_HOME 3
  67. #define DF3_BTN_SELECT 4
  68. #define DF3_BTN_L 5
  69. #define DF3_BTN_DOWN 6
  70. #define DF3_BTN_RIGHT 7
  71.  
  72. #define DF3_BTN_UP 0
  73. #define DF3_BTN_LEFT 1
  74. #define DF3_BTN_ZR 2
  75. #define DF3_BTN_X 3
  76. #define DF3_BTN_A 4
  77. #define DF3_BTN_Y 5
  78. #define DF3_BTN_B 6
  79. #define DF3_BTN_ZL 7
  80.  
  81. #define DEAD_ZONE 20
  82. #define DIAG_MAX 40
  83. #define STICK_MAX 72
  84. #define STICK_FUZZ 4
  85.  
  86. #define TRIGGER_MIN 0
  87. #define TRIGGER_MAX 0xff
  88. #define TRIGGER_FUZZ 4
  89.  
  90. #define MAX_CON_COUNT 4
  91.  
  92. #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
  93. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  94.  
  95. #define CLVCON_DETECT_USE_IRQ 0
  96.  
  97. #if CLVCON_DETECT_USE_IRQ
  98. #define INVAL_IRQ -1
  99. #define DETECT_DELAY (HZ / 10)
  100. #else
  101. #define DETECT_DELAY (HZ / 5)
  102. #endif
  103.  
  104. #define DEBOUNCE_VALUE 0x71
  105.  
  106. static struct delayed_work detect_work;
  107.  
  108. static DEFINE_MUTEX(con_state_lock);
  109. static DEFINE_MUTEX(detect_task_lock);
  110.  
  111. #define VERBOSITY 1
  112.  
  113. #if VERBOSITY > 0
  114. #define ERR(m, ...) printk(KERN_ERR "clvcon error: " m "\n", ##__VA_ARGS__)
  115. #define INF(m, ...) printk(KERN_INFO "clvcon info: " m "\n", ##__VA_ARGS__)
  116. #else
  117. #define ERR(m, ...)
  118. #define INF(m, ...)
  119. #endif
  120.  
  121. #if VERBOSITY > 1
  122. #define DBG(m, ...) printk(KERN_DEBUG "clvcon: " m "\n", ##__VA_ARGS__)
  123. #if VERBOSITY > 2
  124. #define FAST_ERR(m, ...) ERR(m, ##__VA_ARGS__)
  125. #define FAST_DBG(m, ...) DBG(m, ##__VA_ARGS__)
  126. #else
  127. #define FAST_ERR(m, ...) trace_printk(KERN_ERR "clvcon error: " m "\n", ##__VA_ARGS__)
  128. #define FAST_DBG(m, ...) trace_printk(KERN_DEBUG "clvcon: " m "\n", ##__VA_ARGS__)
  129. #endif
  130. #else
  131. #define DBG(m, ...)
  132. #define FAST_ERR(m, ...)
  133. #define FAST_DBG(m, ...)
  134. #endif
  135.  
  136. enum ControllerState {
  137. CS_OK,
  138. CS_FAST_RETRY, // reconnect after typical noise timespan, 2*15ms -> 4 polling periods at 150Hz
  139. CS_RETRY_1,
  140. CS_RETRY_2,
  141. CS_ERR
  142. };
  143.  
  144. static bool get_bit(u8 data, int bitnum) {
  145. return (data & (1 << bitnum)) >> bitnum;
  146. }
  147.  
  148. static const struct i2c_device_id clvcon_idtable[] = {
  149. { "classic", CLASSIC_ID },
  150. {}
  151. };
  152.  
  153. MODULE_DEVICE_TABLE(i2c, clvcon_idtable);
  154.  
  155. struct clvcon_button_state
  156. {
  157. bool report;
  158. int changed;
  159. };
  160.  
  161. struct clvcon_info {
  162. struct input_polled_dev *dev;
  163. struct i2c_client *client;
  164. struct i2c_adapter *adapter;
  165. #if CLVCON_DETECT_USE_IRQ
  166. int irq;
  167. #endif
  168. int detection_active;
  169. int gpio;
  170. int id;
  171. enum ControllerState state;
  172. u16 retry_counter;
  173. int home_counter;
  174. struct clvcon_button_state s6[8];
  175. struct clvcon_button_state s7[8];
  176. };
  177.  
  178. static struct clvcon_info con_info_list[MAX_CON_COUNT];
  179. static int module_params[2 * MAX_CON_COUNT];
  180. static int arr_argc = 0;
  181.  
  182. #define CON_NAME_PREFIX "Nintendo Clovercon - controller"
  183. const char *controller_names[] = {CON_NAME_PREFIX"1", CON_NAME_PREFIX"2",
  184. CON_NAME_PREFIX"3", CON_NAME_PREFIX"4"};
  185.  
  186. module_param_array(module_params, int, &arr_argc, 0000);
  187. MODULE_PARM_DESC(module_params, "Input info in the form con0_i2c_bus, con0_detect_gpio, "
  188. "con1_i2c_bus, con1_detect_gpio, ... gpio < 0 means no detection");
  189.  
  190. #if CLVCON_DETECT_USE_IRQ
  191. struct clvcon_info * clvcon_info_from_irq(int irq) {
  192. int i;
  193. for (i = 0; i < MAX_CON_COUNT; i++) {
  194. if (con_info_list[i].irq == irq) {
  195. return &con_info_list[i];
  196. }
  197. }
  198. return NULL;
  199. }
  200. #endif
  201.  
  202. struct clvcon_info * clvcon_info_from_adapter(struct i2c_adapter *adapter) {
  203. int i;
  204. for (i = 0; i < MAX_CON_COUNT; i++) {
  205. if (con_info_list[i].adapter == adapter) {
  206. return &con_info_list[i];
  207. }
  208. }
  209. return NULL;
  210. }
  211.  
  212. static int clvcon_write(struct i2c_client *client, u8 *data, size_t count) {
  213. struct i2c_msg msg = {
  214. .addr = client->addr,
  215. .len = count,
  216. .buf = data
  217. };
  218. int ret;
  219.  
  220. ret = i2c_transfer(client->adapter, &msg, 1);
  221. if (ret < 0) {
  222. //read and write errors are DBG only to account for normal
  223. //errors coming from disconnects
  224. DBG("write failed with error %i", ret);
  225. return ret;
  226. } else if (ret != 1) {
  227. DBG("incomplete write, return value: %i", ret);
  228. return -EIO;
  229. }
  230.  
  231. return 0;
  232. }
  233.  
  234. static int clvcon_prepare_read(struct i2c_client *client, u8 address) {
  235. int ret;
  236.  
  237. ret = clvcon_write(client, &address, 1);
  238. if (ret) {
  239. DBG("prepare_read failed");
  240. return ret;
  241. }
  242.  
  243. return 0;
  244. }
  245.  
  246. static int clvcon_read(struct i2c_client *client, u8 address, u8 *values, size_t count) {
  247. struct i2c_msg data_msg = {
  248. .addr = client->addr,
  249. .flags = I2C_M_RD,
  250. .len = count,
  251. .buf = values
  252. };
  253. int ret;
  254.  
  255. ret = clvcon_prepare_read(client, address);
  256. if (ret)
  257. return ret;
  258.  
  259. // Wait > 150µs before prepare_read and actual read
  260. usleep_range(150, 200);
  261.  
  262. ret = i2c_transfer(client->adapter, &data_msg, 1);
  263. if (ret < 0) {
  264. DBG("read failed with error %i", ret);
  265. return ret;
  266. } else if (ret != 1) {
  267. DBG("incomplete read, return value: %i", ret);
  268. return -EIO;
  269. }
  270.  
  271. return 0;
  272. }
  273.  
  274. static int clvcon_read_controller_info(struct i2c_client *client, u8 *data, size_t len) {
  275. int ret;
  276.  
  277. len = MIN(len, 0xff -0xfa + 1);
  278. ret = clvcon_read(client, 0xfa, data, len);
  279. if (ret)
  280. return ret;
  281.  
  282. return len;
  283. // print_hex_dump(KERN_DEBUG, "Controller info data: " , DUMP_PREFIX_NONE, 16, 256, data, len, false);
  284. }
  285.  
  286. static int clvcon_setup(struct i2c_client *client) {
  287. u8 init_data[] = { 0xf0, 0x55, 0xfb, 0x00, 0xfe, DATA_FORMAT };
  288. static const int CON_INFO_LEN = 6;
  289. u8 con_info_data[CON_INFO_LEN];
  290. int ret;
  291.  
  292. DBG("clvcon setup");
  293.  
  294. ret = clvcon_write(client, &init_data[0], 2);
  295. if (ret)
  296. goto err;
  297. ret = clvcon_write(client, &init_data[2], 2);
  298. if (ret)
  299. goto err;
  300. ret = clvcon_write(client, &init_data[4], 2);
  301. if (ret)
  302. goto err;
  303.  
  304. ret = clvcon_read_controller_info(client, con_info_data, CON_INFO_LEN);
  305. if (ret < 0) {
  306. ERR("error reading controller info");
  307. goto err;
  308. } else if (ret != CON_INFO_LEN) {
  309. ERR("wrong read length %i reading controller info", ret);
  310. ret = -EIO;
  311. goto err;
  312. }
  313. if (con_info_data[4] != DATA_FORMAT) {
  314. ERR("failed to set data format, value is %i", (int)con_info_data[4]);
  315. ret = -EIO;
  316. goto err;
  317. }
  318. if (con_info_data[5] != 1) {
  319. ERR("unsupported controller id %i", (int)con_info_data[5]);
  320. ret = -EIO;
  321. goto err;
  322. }
  323.  
  324. return 0;
  325.  
  326. err:
  327. ERR("controller setup failed with error %i", -ret);
  328. return ret;
  329. }
  330.  
  331. static void clamp_stick(int *px, int *py) {
  332. int x_sign = 1;
  333. int y_sign = 1;
  334. int x = *px;
  335. int y = *py;
  336. int norm;
  337.  
  338. if (x < 0) {
  339. x_sign = -1;
  340. x = -x;
  341. }
  342. if (y < 0) {
  343. y_sign = -1;
  344. y = -y;
  345. }
  346.  
  347. x = MAX(0, x - DEAD_ZONE);
  348. y = MAX(0, y - DEAD_ZONE);
  349.  
  350. if (x == 0 && y == 0) {
  351. goto clamp_end;
  352. }
  353.  
  354. norm = (y <= x) ? (DIAG_MAX * x + (STICK_MAX - DIAG_MAX) * y) :
  355. (DIAG_MAX * y + (STICK_MAX - DIAG_MAX) * x);
  356.  
  357. if (DIAG_MAX * STICK_MAX < norm) {
  358. x = DIAG_MAX * STICK_MAX * x / norm;
  359. y = DIAG_MAX * STICK_MAX * y / norm;
  360. }
  361.  
  362. clamp_end:
  363. *px = x * x_sign;
  364. *py = y * y_sign;
  365. }
  366.  
  367. /* When DC noise is kicking in, we want to avoid the driver to report wrong
  368. * btn state changes.
  369. *
  370. * This is specially important for example to avoid switching to the menui
  371. * spuriously because of noisy home button events.
  372. *
  373. * The following function will filter out as many state changes as the module
  374. * is configured to before actually reporting the change
  375. *
  376. * NB: even the Mini NES controller can report the home button event even
  377. * though no physical button is present. The MCU is the same as the
  378. * classic/pro controller.
  379. */
  380.  
  381. static inline void clvcon_df3_btn(struct clvcon_button_state* btn, const u8 payload, const int id)
  382. {
  383. bool v;
  384.  
  385. v = !get_bit(payload, id);
  386. btn += id;
  387.  
  388. #if FILTER_DIGITAL_BUTTON
  389. if (btn->report != v) {
  390. btn->changed++;
  391. if (btn->changed > 1) {
  392. btn->report = !btn->report;
  393. btn->changed = 0;
  394. }
  395. } else {
  396. btn->changed = 0;
  397. }
  398. #else
  399. btn->report = v;
  400. #endif
  401. }
  402.  
  403. /* The Wii could trust the payload 100% because the Wii Remote
  404. * controller is powered with AA batteries. In the case of
  405. * CLV, high voltage on DC power supply has shown very noisy
  406. * payloads coming out of the I²C bus. But we don't have any
  407. * checksum or parity bits to discard such corrupted payloads.
  408. *
  409. * However the payload zero padding should be impacted by the
  410. * DC noise and some high bits should pop up there too.
  411. *
  412. * This function reports a noisy zero padding area
  413. */
  414. static int inline clvcon_df3_zero_padding_is_noisy(const u8* payload)
  415. {
  416. int i;
  417. int up = 0;
  418. for (i=8; i<21; i++) {
  419. up += payload[i];
  420. }
  421. return up;
  422. }
  423.  
  424. static inline void clvcon_info_reset_button_states(struct clvcon_info* info) {
  425. info->home_counter = 0;
  426. memset(info->s6, 0, sizeof(info->s6));
  427. memset(info->s7, 0, sizeof(info->s7));
  428. }
  429.  
  430. static void clvcon_poll(struct input_polled_dev *polled_dev) {
  431. struct clvcon_info *info = polled_dev->private;
  432. static const int READ_LEN = 21;
  433. u8 data[READ_LEN];
  434. int jx, jy, rx, ry, tl, tr;
  435. u16 retry_delay = RETRY_BASE_DELAY;
  436. int ret;
  437.  
  438. switch (info->state) {
  439. case CS_OK:
  440. ret = clvcon_read(info->client, 0, data, READ_LEN);
  441. if (ret) {
  442. DBG("read failed for active controller - possible controller disconnect");
  443. INF("moving controller %i to error state", info->id);
  444. /* When DC noise kicks in, reading from I²C becomes unreliable
  445. * typical noise bursts have a 15ms duration every 300ms and
  446. * reconnecting after ~30ms would be enough. However hardware
  447. * team reported the error level in their test environment
  448. * would be too high.
  449. *
  450. * So it's impossible to just use CS_FAST_RETRY as the plan was
  451. * Stick to the very conservative value of CS_RETRY_1, leading
  452. * to a 850ms reconnection delay. */
  453. info->state = CS_RETRY_1;
  454. break;
  455. }
  456.  
  457. //print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 256, data, READ_LEN, false);
  458.  
  459. /* Use noise presence in the zero padding as discard criteri
  460. * for the data packet */
  461. if (clvcon_df3_zero_padding_is_noisy(data))
  462. break;
  463.  
  464. // Report analog sticks / buttons
  465. jx = data[0] - 0x80;
  466. rx = data[1] - 0x80;
  467. jy = 0x7fl - data[2];
  468. ry = 0x7fl - data[3];
  469. tl = data[4];
  470. tr = data[5];
  471.  
  472. clamp_stick(&jx, &jy);
  473. clamp_stick(&rx, &ry);
  474.  
  475. input_report_abs(polled_dev->input, ABS_X, jx);
  476. input_report_abs(polled_dev->input, ABS_Y, jy);
  477. input_report_abs(polled_dev->input, ABS_RX, rx);
  478. input_report_abs(polled_dev->input, ABS_RY, ry);
  479. input_report_abs(polled_dev->input, ABS_Z, tl);
  480. input_report_abs(polled_dev->input, ABS_RZ, tr);
  481.  
  482. // Report digital buttons from byte6
  483. clvcon_df3_btn(info->s6, data[6], DF3_BTN_R);
  484. clvcon_df3_btn(info->s6, data[6], DF3_BTN_START);
  485. clvcon_df3_btn(info->s6, data[6], DF3_BTN_HOME);
  486. clvcon_df3_btn(info->s6, data[6], DF3_BTN_SELECT);
  487. clvcon_df3_btn(info->s6, data[6], DF3_BTN_L);
  488. clvcon_df3_btn(info->s6, data[6], DF3_BTN_DOWN);
  489. clvcon_df3_btn(info->s6, data[6], DF3_BTN_RIGHT);
  490.  
  491. if (info->s6[DF3_BTN_HOME].report) {
  492. info->home_counter++;
  493. if (info->home_counter>HOME_BUTTON_THRESHOLD) {
  494. info->home_counter = HOME_BUTTON_THRESHOLD;
  495. }
  496. } else {
  497. info->home_counter = 0;
  498. }
  499.  
  500. input_report_key(polled_dev->input, BTN_TR, info->s6[DF3_BTN_R].report);
  501. input_report_key(polled_dev->input, BTN_START, info->s6[DF3_BTN_START].report);
  502. input_report_key(polled_dev->input, BTN_MODE, (info->home_counter>=HOME_BUTTON_THRESHOLD));
  503. input_report_key(polled_dev->input, BTN_SELECT, info->s6[DF3_BTN_SELECT].report);
  504. input_report_key(polled_dev->input, BTN_TL, info->s6[DF3_BTN_L].report);
  505. input_report_key(polled_dev->input, BTN_TRIGGER_HAPPY4, info->s6[DF3_BTN_DOWN].report);
  506. input_report_key(polled_dev->input, BTN_TRIGGER_HAPPY2, info->s6[DF3_BTN_RIGHT].report);
  507.  
  508. // Report digital buttons from byte7
  509. clvcon_df3_btn(info->s7, data[7], DF3_BTN_UP);
  510. clvcon_df3_btn(info->s7, data[7], DF3_BTN_LEFT);
  511. clvcon_df3_btn(info->s7, data[7], DF3_BTN_ZR);
  512. clvcon_df3_btn(info->s7, data[7], DF3_BTN_X);
  513. clvcon_df3_btn(info->s7, data[7], DF3_BTN_A);
  514. clvcon_df3_btn(info->s7, data[7], DF3_BTN_Y);
  515. clvcon_df3_btn(info->s7, data[7], DF3_BTN_B);
  516. clvcon_df3_btn(info->s7, data[7], DF3_BTN_ZL);
  517.  
  518. input_report_key(polled_dev->input, BTN_TRIGGER_HAPPY3, info->s7[DF3_BTN_UP].report);
  519. input_report_key(polled_dev->input, BTN_TRIGGER_HAPPY1, info->s7[DF3_BTN_LEFT].report);
  520. input_report_key(polled_dev->input, BTN_TR2, info->s7[DF3_BTN_ZR].report);
  521. input_report_key(polled_dev->input, BTN_X, info->s7[DF3_BTN_X].report);
  522. input_report_key(polled_dev->input, BTN_A, info->s7[DF3_BTN_A].report);
  523. input_report_key(polled_dev->input, BTN_Y, info->s7[DF3_BTN_Y].report);
  524. input_report_key(polled_dev->input, BTN_B, info->s7[DF3_BTN_B].report);
  525. input_report_key(polled_dev->input, BTN_TL2, info->s7[DF3_BTN_ZL].report);
  526.  
  527. input_sync(polled_dev->input);
  528.  
  529. break;
  530. case CS_FAST_RETRY:
  531. retry_delay /= 32;
  532. //fall-through
  533. case CS_RETRY_1:
  534. retry_delay /= 2;
  535. //fall-through
  536. case CS_RETRY_2:
  537. retry_delay /= 2;
  538. //fall-through
  539. case CS_ERR:
  540. info->retry_counter++;
  541. if (info->retry_counter == retry_delay) {
  542. DBG("retrying controller setup");
  543. ret = clvcon_setup(info->client);
  544. if (ret) {
  545. info->state = MIN(CS_ERR, info->state + 1);
  546. } else {
  547. info->state = CS_OK;
  548. clvcon_info_reset_button_states(info);
  549. INF("setup succeeded for controller %i, moving to OK state", info->id);
  550. }
  551. info->retry_counter = 0;
  552. }
  553. break;
  554. default:
  555. info->state = CS_ERR;
  556. }
  557. }
  558.  
  559. static void clvcon_open(struct input_polled_dev *polled_dev) {
  560. struct clvcon_info *info = polled_dev->private;
  561. if (clvcon_setup(info->client)) {
  562. info->retry_counter = 0;
  563. info->state = CS_RETRY_1;
  564. INF("opened controller %i, controller in error state after failed setup", info->id);
  565. } else {
  566. info->state = CS_OK;
  567. clvcon_info_reset_button_states(info);
  568. INF("opened controller %i, controller in OK state", info->id);
  569. }
  570. }
  571.  
  572. static int clvcon_probe(struct i2c_client *client, const struct i2c_device_id *id) {
  573. struct clvcon_info *info;
  574. struct input_polled_dev *polled_dev;
  575. struct input_dev *input_dev;
  576. int ret = 0;
  577.  
  578. switch (id->driver_data) {
  579. case CLASSIC_ID:
  580. DBG("probing classic controller");
  581. break;
  582. default:
  583. ERR("unknown id: %lu\n", id->driver_data);
  584. return -EINVAL;
  585. }
  586.  
  587. mutex_lock(&con_state_lock);
  588. info = clvcon_info_from_adapter(client->adapter);
  589. if (!info) {
  590. ERR("unkonwn client passed to probe");
  591. mutex_unlock(&con_state_lock);
  592. return -EINVAL;
  593. }
  594. info->client = client;
  595. i2c_set_clientdata(client, info);
  596. mutex_unlock(&con_state_lock);
  597.  
  598. polled_dev = input_allocate_polled_device();
  599. if (!polled_dev) {
  600. ERR("error allocating polled device");
  601. return -ENOMEM;
  602. }
  603.  
  604. info->dev = polled_dev;
  605.  
  606. polled_dev->poll_interval = POLL_INTERVAL;
  607. polled_dev->poll = clvcon_poll;
  608. polled_dev->open = clvcon_open;
  609. polled_dev->private = info;
  610.  
  611. input_dev = polled_dev->input;
  612.  
  613. //change controller_names initializer when changing MAX_CON_COUNT
  614. BUILD_BUG_ON(MAX_CON_COUNT != ARRAY_SIZE(controller_names));
  615. input_dev->name = controller_names[info->id - 1];
  616. input_dev->phys = DRV_NAME"/clvcon";
  617. input_dev->id.bustype = BUS_I2C;
  618. input_dev->dev.parent = &client->dev;
  619.  
  620. set_bit(EV_ABS, input_dev->evbit);
  621. set_bit(ABS_X, input_dev->absbit);
  622. set_bit(ABS_Y, input_dev->absbit);
  623. set_bit(ABS_RX, input_dev->absbit);
  624. set_bit(ABS_RY, input_dev->absbit);
  625. /*
  626. L/R are analog on the classic controller, digital
  627. on the pro with values 0 - 0xf8
  628. */
  629. set_bit(ABS_Z, input_dev->absbit);
  630. set_bit(ABS_RZ, input_dev->absbit);
  631.  
  632. set_bit(EV_KEY, input_dev->evbit);
  633. set_bit(BTN_X, input_dev->keybit);
  634. set_bit(BTN_B, input_dev->keybit);
  635. set_bit(BTN_A, input_dev->keybit);
  636. set_bit(BTN_Y, input_dev->keybit);
  637. set_bit(BTN_TRIGGER_HAPPY3, input_dev->keybit); // up
  638. set_bit(BTN_TRIGGER_HAPPY4, input_dev->keybit); // down
  639. set_bit(BTN_TRIGGER_HAPPY2, input_dev->keybit); // right
  640. set_bit(BTN_TRIGGER_HAPPY1, input_dev->keybit); // left
  641. set_bit(BTN_TR, input_dev->keybit);
  642. set_bit(BTN_TL, input_dev->keybit);
  643. set_bit(BTN_TR2, input_dev->keybit);
  644. set_bit(BTN_TL2, input_dev->keybit);
  645. set_bit(BTN_SELECT, input_dev->keybit);
  646. set_bit(BTN_START, input_dev->keybit);
  647. set_bit(BTN_MODE, input_dev->keybit);
  648.  
  649. input_set_abs_params(input_dev, ABS_X, -STICK_MAX, STICK_MAX, STICK_FUZZ, 0);
  650. input_set_abs_params(input_dev, ABS_Y, -STICK_MAX, STICK_MAX, STICK_FUZZ, 0);
  651. input_set_abs_params(input_dev, ABS_RX, -STICK_MAX, STICK_MAX, STICK_FUZZ, 0);
  652. input_set_abs_params(input_dev, ABS_RY, -STICK_MAX, STICK_MAX, STICK_FUZZ, 0);
  653. input_set_abs_params(input_dev, ABS_Z, TRIGGER_MIN, TRIGGER_MAX, TRIGGER_FUZZ, 0);
  654. input_set_abs_params(input_dev, ABS_RZ, TRIGGER_MIN, TRIGGER_MAX, TRIGGER_FUZZ, 0);
  655.  
  656. ret = input_register_polled_device(polled_dev);
  657. if (ret) {
  658. ERR("registering polled device failed");
  659. goto err_register_polled_dev;
  660. }
  661.  
  662. INF("probed controller %i", info->id);
  663.  
  664. return 0;
  665.  
  666. err_register_polled_dev:
  667. input_free_polled_device(polled_dev);
  668.  
  669. return ret;
  670. }
  671.  
  672. static int clvcon_remove(struct i2c_client *client) {
  673. struct clvcon_info *info;
  674. struct input_polled_dev *polled_dev;
  675.  
  676. mutex_lock(&con_state_lock);
  677. info = i2c_get_clientdata(client);
  678. polled_dev = info->dev;
  679. info->dev = NULL;
  680. mutex_unlock(&con_state_lock);
  681.  
  682. input_unregister_polled_device(polled_dev);
  683. input_free_polled_device(polled_dev);
  684.  
  685. INF("removed controller %i", info->id);
  686.  
  687. return 0;
  688. }
  689.  
  690. static struct i2c_driver clvcon_driver = {
  691. .driver = {
  692. .name = "clvcon",
  693. .owner = THIS_MODULE,
  694. },
  695.  
  696. .id_table = clvcon_idtable,
  697. .probe = clvcon_probe,
  698. .remove = clvcon_remove,
  699. };
  700.  
  701. static struct i2c_board_info clvcon_i2c_board_info = {
  702. I2C_BOARD_INFO("classic", CONTROLLER_I2C_ADDRESS),
  703. };
  704.  
  705. /* Must be holding con_state_lock */
  706. int clvcon_add_controller(struct clvcon_info *info) {
  707. struct i2c_client *client;
  708.  
  709. mutex_unlock(&con_state_lock);
  710. client = i2c_new_device(info->adapter, &clvcon_i2c_board_info);
  711. mutex_lock(&con_state_lock);
  712. if (!client) {
  713. ERR("could not create i2c device");
  714. return -ENOMEM;
  715. }
  716.  
  717. INF("added device for controller %i", info->id);
  718. return 0;
  719. }
  720.  
  721. /* Must be holding con_state_lock */
  722. void clvcon_remove_controller(struct clvcon_info *info) {
  723. struct i2c_client *client = info->client;
  724.  
  725. mutex_unlock(&con_state_lock);
  726. i2c_unregister_device(client);
  727. mutex_lock(&con_state_lock);
  728. info->client = NULL;
  729.  
  730. INF("removed device for controller %i", info->id);
  731. }
  732.  
  733. static void clvcon_remove_controllers(void) {
  734. int i;
  735.  
  736. mutex_lock(&con_state_lock);
  737. for (i = 0; i < arr_argc / 2; i++) {
  738. if (!con_info_list[i].client) {
  739. continue;
  740. }
  741. clvcon_remove_controller(&con_info_list[i]);
  742. }
  743. mutex_unlock(&con_state_lock);
  744. }
  745.  
  746. static void clvcon_detect_task(struct work_struct *dummy) {
  747. struct clvcon_info *info;
  748. int i;
  749. int val;
  750.  
  751. mutex_lock(&detect_task_lock);
  752. DBG("detect task running");
  753. mutex_lock(&con_state_lock);
  754. for (i = 0; i < MAX_CON_COUNT; i++) {
  755. info = &con_info_list[i];
  756. if (!info->detection_active) {
  757. continue;
  758. }
  759. val = gpio_get_value(info->gpio);
  760. DBG("detect pin value: %i", val);
  761. if (val && !info->client) {
  762. DBG("detect task adding controller %i", i);
  763. clvcon_add_controller(info);
  764. } else if (!val && info->client) {
  765. DBG("detect task removing controller %i", i);
  766. clvcon_remove_controller(info);
  767. }
  768. }
  769. mutex_unlock(&con_state_lock);
  770. mutex_unlock(&detect_task_lock);
  771. DBG("detect task done");
  772. }
  773.  
  774. #if CLVCON_DETECT_USE_IRQ
  775.  
  776. static irqreturn_t clvcon_detect_interrupt(int irq, void* dummy) {
  777. struct clvcon_info *info = clvcon_info_from_irq(irq);
  778. static int initialized = 0;
  779.  
  780. if (info == NULL) {
  781. FAST_ERR("could not find controller info associated with irq %i", irq);
  782. return IRQ_HANDLED;
  783. }
  784.  
  785. if (initialized == 0) {
  786. INIT_DELAYED_WORK(&detect_work, clvcon_detect_task);
  787. initialized = 1;
  788. } else {
  789. PREPARE_DELAYED_WORK(&detect_work, clvcon_detect_task);
  790. }
  791.  
  792. schedule_delayed_work(&detect_work, DETECT_DELAY);
  793.  
  794. FAST_DBG("interrupt handler on int %i", irq);
  795. return IRQ_HANDLED;
  796. }
  797.  
  798. static int clvcon_setup_irq_detect(struct clvcon_info *info) {
  799. int irq;
  800. int ret;
  801.  
  802. ret = gpio_to_irq(info->gpio);
  803. if (ret < 0) {
  804. ERR("gpio to irq failed");
  805. return ret;
  806. } else {
  807. irq = ret;
  808. DBG("irq for gpio %i: %i", info->gpio, irq);
  809. }
  810.  
  811. mutex_lock(&con_state_lock);
  812. info->irq = irq;
  813. info->detection_active = 1;
  814. mutex_unlock(&con_state_lock);
  815.  
  816. ret = request_irq(ret, clvcon_detect_interrupt, IRQ_TYPE_EDGE_BOTH, "clvcon", NULL);
  817. if (ret) {
  818. ERR("failed to request irq");
  819. return ret;
  820. }
  821.  
  822. return 0;
  823. }
  824.  
  825. static void clvcon_teardown_irq_detect(struct clvcon_info *info) {
  826. free_irq(info->irq, NULL);
  827. mutex_lock(&con_state_lock);
  828. info->detection_active = 0;
  829. info->irq = INVAL_IRQ;
  830. mutex_unlock(&con_state_lock);
  831. }
  832.  
  833. #else //CLVCON_DETECT_USE_IRQ
  834.  
  835. static void clvcon_detect_timer_task(struct work_struct *dummy) {
  836. static int initialized = 0;
  837.  
  838. if (initialized == 0) {
  839. INIT_DELAYED_WORK(&detect_work, clvcon_detect_timer_task);
  840. initialized = 1;
  841. } else {
  842. PREPARE_DELAYED_WORK(&detect_work, clvcon_detect_timer_task);
  843. }
  844.  
  845. clvcon_detect_task(NULL);
  846. schedule_delayed_work(&detect_work, DETECT_DELAY);
  847. }
  848.  
  849. static int clvcon_setup_timer_detect(struct clvcon_info *info) {
  850. static int task_running = 0;
  851.  
  852. mutex_lock(&con_state_lock);
  853. info->detection_active = 1;
  854. mutex_unlock(&con_state_lock);
  855.  
  856. if (task_running)
  857. return 0;
  858.  
  859. task_running = 1;
  860. clvcon_detect_timer_task(NULL);
  861.  
  862. return 0;
  863. }
  864.  
  865. static void clvcon_teardown_timer_detect(struct clvcon_info *info) {
  866. mutex_lock(&con_state_lock);
  867. info->detection_active = 0;
  868. mutex_unlock(&con_state_lock);
  869. }
  870.  
  871. #endif //CLVCON_DETECT_USE_IRQ
  872.  
  873. static int clvcon_setup_i2c(struct clvcon_info *info, int i2c_bus) {
  874. struct i2c_adapter *adapter;
  875.  
  876. adapter = i2c_get_adapter(i2c_bus);
  877. if (!adapter) {
  878. ERR("could not access i2c bus %i", i2c_bus);
  879. return -EINVAL;
  880. }
  881.  
  882. info->adapter = adapter;
  883.  
  884. return 0;
  885. }
  886.  
  887. static int clvcon_setup_detection(struct clvcon_info *info, int gpio_pin) {
  888. int ret;
  889.  
  890. ret = gpio_request(gpio_pin, "clvcon_detect");
  891. if (ret) {
  892. ERR("gpio request failed for pin %i", gpio_pin);
  893. return ret;
  894. }
  895.  
  896. ret = gpio_direction_input(gpio_pin);
  897. if (ret) {
  898. ERR("gpio input direction failed");
  899. goto err_gpio_cleanup;
  900. }
  901.  
  902. info->gpio = gpio_pin;
  903.  
  904. ret = gpio_set_debounce(gpio_pin, DEBOUNCE_VALUE);
  905. if (ret) {
  906. ERR("failed to debounce gpio %i", gpio_pin);
  907. goto err_gpio_cleanup;
  908. }
  909.  
  910. #if CLVCON_DETECT_USE_IRQ
  911. info->irq = INVAL_IRQ;
  912. ret = clvcon_setup_irq_detect(info);
  913. #else
  914. ret = clvcon_setup_timer_detect(info);
  915. #endif
  916.  
  917. if (ret) {
  918. ERR("controller detection setup failed");
  919. goto err_detect_cleanup;
  920. }
  921.  
  922. return 0;
  923.  
  924. err_detect_cleanup:
  925. #if CLVCON_DETECT_USE_IRQ
  926. clvcon_teardown_irq_detect(info);
  927. #else
  928. clvcon_teardown_timer_detect(info);
  929. #endif
  930.  
  931. err_gpio_cleanup:
  932. gpio_free(gpio_pin);
  933. return ret;
  934. }
  935.  
  936. static void clvcon_teardown_detection(void) {
  937. int i;
  938. int gpio;
  939.  
  940. cancel_delayed_work_sync(&detect_work);
  941.  
  942. for (i = 0; i < MAX_CON_COUNT; i++) {
  943. if (!con_info_list[i].detection_active) {
  944. continue;
  945. }
  946. #if CLVCON_DETECT_USE_IRQ
  947. clvcon_teardown_irq_detect(&con_info_list[i]);
  948. #else
  949. clvcon_teardown_timer_detect(&con_info_list[i]);
  950. #endif
  951. mutex_lock(&con_state_lock);
  952. con_info_list[i].adapter = NULL;
  953. gpio = con_info_list[i].gpio;
  954. mutex_unlock(&con_state_lock);
  955.  
  956. DBG("Freeing gpio %i", gpio);
  957. gpio_free(gpio);
  958. }
  959. }
  960.  
  961. static int __init clvcon_init(void) {
  962. int i2c_bus;
  963. int gpio_pin;
  964. int ret;
  965. int i;
  966.  
  967. for (i = 0; i < MAX_CON_COUNT; i++) {
  968. con_info_list[i].detection_active = 0;
  969. con_info_list[i].id = i + 1;
  970. }
  971.  
  972. for (i = 0; i < arr_argc / 2; i++) {
  973. i2c_bus = module_params[2 * i];
  974. gpio_pin = module_params[2 * i + 1];
  975.  
  976. DBG("initializing controller %i on bus %i, gpio %i", i, i2c_bus, gpio_pin);
  977.  
  978. ret = clvcon_setup_i2c(&con_info_list[i], i2c_bus);
  979. if (ret) {
  980. ERR("failed to init controller %i", i);
  981. goto err_controller_cleanup;
  982. }
  983.  
  984. if (gpio_pin < 0) {
  985. mutex_lock(&con_state_lock);
  986. ret = clvcon_add_controller(&con_info_list[i]);
  987. mutex_unlock(&con_state_lock);
  988. } else {
  989. ret = clvcon_setup_detection(&con_info_list[i], gpio_pin);
  990. if (ret) {
  991. ERR("failed to init controller %i", i);
  992. goto err_controller_cleanup;
  993. }
  994. }
  995. }
  996.  
  997. ret = i2c_add_driver(&clvcon_driver);
  998. if (ret) {
  999. ERR("failed to add driver");
  1000. goto err_controller_cleanup;
  1001. }
  1002.  
  1003. return 0;
  1004.  
  1005. err_controller_cleanup:
  1006. clvcon_teardown_detection();
  1007. clvcon_remove_controllers();
  1008. return ret;
  1009. }
  1010.  
  1011. module_init(clvcon_init);
  1012.  
  1013. static void __exit clvcon_exit(void) {
  1014. DBG("exit");
  1015.  
  1016. clvcon_teardown_detection();
  1017. clvcon_remove_controllers();
  1018. i2c_del_driver(&clvcon_driver);
  1019. }
  1020.  
  1021. module_exit(clvcon_exit);
Add Comment
Please, Sign In to add comment