@@ -1655,10 +1655,39 @@ static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1655
1655
ly = (ext [4 ] & 0xff ) | ((ext [5 ] & 0x0f ) << 8 );
1656
1656
ry = (ext [6 ] & 0xff ) | ((ext [7 ] & 0x0f ) << 8 );
1657
1657
1658
- input_report_abs (wdata -> extension .input , ABS_X , lx - 0x800 );
1659
- input_report_abs (wdata -> extension .input , ABS_Y , 0x800 - ly );
1660
- input_report_abs (wdata -> extension .input , ABS_RX , rx - 0x800 );
1661
- input_report_abs (wdata -> extension .input , ABS_RY , 0x800 - ry );
1658
+ /* zero-point offsets */
1659
+ lx -= 0x800 ;
1660
+ ly = 0x800 - ly ;
1661
+ rx -= 0x800 ;
1662
+ ry = 0x800 - ry ;
1663
+
1664
+ /* Trivial automatic calibration. We don't know any calibration data
1665
+ * in the EEPROM so we must use the first report to calibrate the
1666
+ * null-position of the analog sticks. Users can retrigger calibration
1667
+ * via sysfs, or set it explicitly. If data is off more than abs(500),
1668
+ * we skip calibration as the sticks are likely to be moved already. */
1669
+ if (!(wdata -> state .flags & WIIPROTO_FLAG_PRO_CALIB_DONE )) {
1670
+ wdata -> state .flags |= WIIPROTO_FLAG_PRO_CALIB_DONE ;
1671
+ if (abs (lx ) < 500 )
1672
+ wdata -> state .calib_pro_sticks [0 ] = - lx ;
1673
+ if (abs (ly ) < 500 )
1674
+ wdata -> state .calib_pro_sticks [1 ] = - ly ;
1675
+ if (abs (rx ) < 500 )
1676
+ wdata -> state .calib_pro_sticks [2 ] = - rx ;
1677
+ if (abs (ry ) < 500 )
1678
+ wdata -> state .calib_pro_sticks [3 ] = - ry ;
1679
+ }
1680
+
1681
+ /* apply calibration data */
1682
+ lx += wdata -> state .calib_pro_sticks [0 ];
1683
+ ly += wdata -> state .calib_pro_sticks [1 ];
1684
+ rx += wdata -> state .calib_pro_sticks [2 ];
1685
+ ry += wdata -> state .calib_pro_sticks [3 ];
1686
+
1687
+ input_report_abs (wdata -> extension .input , ABS_X , lx );
1688
+ input_report_abs (wdata -> extension .input , ABS_Y , ly );
1689
+ input_report_abs (wdata -> extension .input , ABS_RX , rx );
1690
+ input_report_abs (wdata -> extension .input , ABS_RY , ry );
1662
1691
1663
1692
input_report_key (wdata -> extension .input ,
1664
1693
wiimod_pro_map [WIIMOD_PRO_KEY_RIGHT ],
@@ -1766,12 +1795,70 @@ static int wiimod_pro_play(struct input_dev *dev, void *data,
1766
1795
return 0 ;
1767
1796
}
1768
1797
1798
+ static ssize_t wiimod_pro_calib_show (struct device * dev ,
1799
+ struct device_attribute * attr ,
1800
+ char * out )
1801
+ {
1802
+ struct wiimote_data * wdata = dev_to_wii (dev );
1803
+ int r ;
1804
+
1805
+ r = 0 ;
1806
+ r += sprintf (& out [r ], "%+06hd:" , wdata -> state .calib_pro_sticks [0 ]);
1807
+ r += sprintf (& out [r ], "%+06hd " , wdata -> state .calib_pro_sticks [1 ]);
1808
+ r += sprintf (& out [r ], "%+06hd:" , wdata -> state .calib_pro_sticks [2 ]);
1809
+ r += sprintf (& out [r ], "%+06hd\n" , wdata -> state .calib_pro_sticks [3 ]);
1810
+
1811
+ return r ;
1812
+ }
1813
+
1814
+ static ssize_t wiimod_pro_calib_store (struct device * dev ,
1815
+ struct device_attribute * attr ,
1816
+ const char * buf , size_t count )
1817
+ {
1818
+ struct wiimote_data * wdata = dev_to_wii (dev );
1819
+ int r ;
1820
+ s16 x1 , y1 , x2 , y2 ;
1821
+
1822
+ if (!strncmp (buf , "scan\n" , 5 )) {
1823
+ spin_lock_irq (& wdata -> state .lock );
1824
+ wdata -> state .flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE ;
1825
+ spin_unlock_irq (& wdata -> state .lock );
1826
+ } else {
1827
+ r = sscanf (buf , "%hd:%hd %hd:%hd" , & x1 , & y1 , & x2 , & y2 );
1828
+ if (r != 4 )
1829
+ return - EINVAL ;
1830
+
1831
+ spin_lock_irq (& wdata -> state .lock );
1832
+ wdata -> state .flags |= WIIPROTO_FLAG_PRO_CALIB_DONE ;
1833
+ spin_unlock_irq (& wdata -> state .lock );
1834
+
1835
+ wdata -> state .calib_pro_sticks [0 ] = x1 ;
1836
+ wdata -> state .calib_pro_sticks [1 ] = y1 ;
1837
+ wdata -> state .calib_pro_sticks [2 ] = x2 ;
1838
+ wdata -> state .calib_pro_sticks [3 ] = y2 ;
1839
+ }
1840
+
1841
+ return strnlen (buf , PAGE_SIZE );
1842
+ }
1843
+
1844
+ static DEVICE_ATTR (pro_calib , S_IRUGO |S_IWUSR |S_IWGRP , wiimod_pro_calib_show ,
1845
+ wiimod_pro_calib_store ) ;
1846
+
1769
1847
static int wiimod_pro_probe (const struct wiimod_ops * ops ,
1770
1848
struct wiimote_data * wdata )
1771
1849
{
1772
1850
int ret , i ;
1851
+ unsigned long flags ;
1773
1852
1774
1853
INIT_WORK (& wdata -> rumble_worker , wiimod_rumble_worker );
1854
+ wdata -> state .calib_pro_sticks [0 ] = 0 ;
1855
+ wdata -> state .calib_pro_sticks [1 ] = 0 ;
1856
+ wdata -> state .calib_pro_sticks [2 ] = 0 ;
1857
+ wdata -> state .calib_pro_sticks [3 ] = 0 ;
1858
+
1859
+ spin_lock_irqsave (& wdata -> state .lock , flags );
1860
+ wdata -> state .flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE ;
1861
+ spin_unlock_irqrestore (& wdata -> state .lock , flags );
1775
1862
1776
1863
wdata -> extension .input = input_allocate_device ();
1777
1864
if (!wdata -> extension .input )
@@ -1786,6 +1873,13 @@ static int wiimod_pro_probe(const struct wiimod_ops *ops,
1786
1873
goto err_free ;
1787
1874
}
1788
1875
1876
+ ret = device_create_file (& wdata -> hdev -> dev ,
1877
+ & dev_attr_pro_calib );
1878
+ if (ret ) {
1879
+ hid_err (wdata -> hdev , "cannot create sysfs attribute\n" );
1880
+ goto err_free ;
1881
+ }
1882
+
1789
1883
wdata -> extension .input -> open = wiimod_pro_open ;
1790
1884
wdata -> extension .input -> close = wiimod_pro_close ;
1791
1885
wdata -> extension .input -> dev .parent = & wdata -> hdev -> dev ;
@@ -1806,20 +1900,23 @@ static int wiimod_pro_probe(const struct wiimod_ops *ops,
1806
1900
set_bit (ABS_RX , wdata -> extension .input -> absbit );
1807
1901
set_bit (ABS_RY , wdata -> extension .input -> absbit );
1808
1902
input_set_abs_params (wdata -> extension .input ,
1809
- ABS_X , -0x800 , 0x800 , 2 , 4 );
1903
+ ABS_X , -0x400 , 0x400 , 4 , 100 );
1810
1904
input_set_abs_params (wdata -> extension .input ,
1811
- ABS_Y , -0x800 , 0x800 , 2 , 4 );
1905
+ ABS_Y , -0x400 , 0x400 , 4 , 100 );
1812
1906
input_set_abs_params (wdata -> extension .input ,
1813
- ABS_RX , -0x800 , 0x800 , 2 , 4 );
1907
+ ABS_RX , -0x400 , 0x400 , 4 , 100 );
1814
1908
input_set_abs_params (wdata -> extension .input ,
1815
- ABS_RY , -0x800 , 0x800 , 2 , 4 );
1909
+ ABS_RY , -0x400 , 0x400 , 4 , 100 );
1816
1910
1817
1911
ret = input_register_device (wdata -> extension .input );
1818
1912
if (ret )
1819
- goto err_free ;
1913
+ goto err_file ;
1820
1914
1821
1915
return 0 ;
1822
1916
1917
+ err_file :
1918
+ device_remove_file (& wdata -> hdev -> dev ,
1919
+ & dev_attr_pro_calib );
1823
1920
err_free :
1824
1921
input_free_device (wdata -> extension .input );
1825
1922
wdata -> extension .input = NULL ;
@@ -1837,6 +1934,8 @@ static void wiimod_pro_remove(const struct wiimod_ops *ops,
1837
1934
input_unregister_device (wdata -> extension .input );
1838
1935
wdata -> extension .input = NULL ;
1839
1936
cancel_work_sync (& wdata -> rumble_worker );
1937
+ device_remove_file (& wdata -> hdev -> dev ,
1938
+ & dev_attr_pro_calib );
1840
1939
1841
1940
spin_lock_irqsave (& wdata -> state .lock , flags );
1842
1941
wiiproto_req_rumble (wdata , 0 );
0 commit comments