psmouse-base.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. /*
  2. * PS/2 mouse driver
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. * Copyright (c) 2003-2004 Dmitry Torokhov
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #define psmouse_fmt(fmt) fmt
  14. #include <linux/bitops.h>
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/input.h>
  20. #include <linux/serio.h>
  21. #include <linux/init.h>
  22. #include <linux/libps2.h>
  23. #include <linux/mutex.h>
  24. #include <linux/types.h>
  25. #include "psmouse.h"
  26. #include "synaptics.h"
  27. #include "logips2pp.h"
  28. #include "alps.h"
  29. #include "hgpk.h"
  30. #include "lifebook.h"
  31. #include "trackpoint.h"
  32. #include "touchkit_ps2.h"
  33. #include "elantech.h"
  34. #include "sentelic.h"
  35. #include "cypress_ps2.h"
  36. #include "focaltech.h"
  37. #include "vmmouse.h"
  38. #include "byd.h"
  39. #define DRIVER_DESC "PS/2 mouse driver"
  40. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  41. MODULE_DESCRIPTION(DRIVER_DESC);
  42. MODULE_LICENSE("GPL");
  43. static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
  44. static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
  45. static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
  46. static const struct kernel_param_ops param_ops_proto_abbrev = {
  47. .set = psmouse_set_maxproto,
  48. .get = psmouse_get_maxproto,
  49. };
  50. #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
  51. module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
  52. MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
  53. static unsigned int psmouse_resolution = 200;
  54. module_param_named(resolution, psmouse_resolution, uint, 0644);
  55. MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
  56. static unsigned int psmouse_rate = 100;
  57. module_param_named(rate, psmouse_rate, uint, 0644);
  58. MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
  59. static bool psmouse_smartscroll = true;
  60. module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
  61. MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
  62. static bool psmouse_a4tech_2wheels;
  63. module_param_named(a4tech_workaround, psmouse_a4tech_2wheels, bool, 0644);
  64. MODULE_PARM_DESC(a4tech_workaround, "A4Tech second scroll wheel workaround, 1 = enabled, 0 = disabled (default).");
  65. static unsigned int psmouse_resetafter = 5;
  66. module_param_named(resetafter, psmouse_resetafter, uint, 0644);
  67. MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
  68. static unsigned int psmouse_resync_time;
  69. module_param_named(resync_time, psmouse_resync_time, uint, 0644);
  70. MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
  71. PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
  72. NULL,
  73. psmouse_attr_show_protocol, psmouse_attr_set_protocol);
  74. PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
  75. (void *) offsetof(struct psmouse, rate),
  76. psmouse_show_int_attr, psmouse_attr_set_rate);
  77. PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
  78. (void *) offsetof(struct psmouse, resolution),
  79. psmouse_show_int_attr, psmouse_attr_set_resolution);
  80. PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
  81. (void *) offsetof(struct psmouse, resetafter),
  82. psmouse_show_int_attr, psmouse_set_int_attr);
  83. PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
  84. (void *) offsetof(struct psmouse, resync_time),
  85. psmouse_show_int_attr, psmouse_set_int_attr);
  86. static struct attribute *psmouse_attributes[] = {
  87. &psmouse_attr_protocol.dattr.attr,
  88. &psmouse_attr_rate.dattr.attr,
  89. &psmouse_attr_resolution.dattr.attr,
  90. &psmouse_attr_resetafter.dattr.attr,
  91. &psmouse_attr_resync_time.dattr.attr,
  92. NULL
  93. };
  94. static const struct attribute_group psmouse_attribute_group = {
  95. .attrs = psmouse_attributes,
  96. };
  97. /*
  98. * psmouse_mutex protects all operations changing state of mouse
  99. * (connecting, disconnecting, changing rate or resolution via
  100. * sysfs). We could use a per-device semaphore but since there
  101. * rarely more than one PS/2 mouse connected and since semaphore
  102. * is taken in "slow" paths it is not worth it.
  103. */
  104. static DEFINE_MUTEX(psmouse_mutex);
  105. static struct workqueue_struct *kpsmoused_wq;
  106. void psmouse_report_standard_buttons(struct input_dev *dev, u8 buttons)
  107. {
  108. input_report_key(dev, BTN_LEFT, buttons & BIT(0));
  109. input_report_key(dev, BTN_MIDDLE, buttons & BIT(2));
  110. input_report_key(dev, BTN_RIGHT, buttons & BIT(1));
  111. }
  112. void psmouse_report_standard_motion(struct input_dev *dev, u8 *packet)
  113. {
  114. int x, y;
  115. x = packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0;
  116. y = packet[2] ? packet[2] - ((packet[0] << 3) & 0x100) : 0;
  117. input_report_rel(dev, REL_X, x);
  118. input_report_rel(dev, REL_Y, -y);
  119. }
  120. void psmouse_report_standard_packet(struct input_dev *dev, u8 *packet)
  121. {
  122. psmouse_report_standard_buttons(dev, packet[0]);
  123. psmouse_report_standard_motion(dev, packet);
  124. }
  125. /*
  126. * psmouse_process_byte() analyzes the PS/2 data stream and reports
  127. * relevant events to the input module once full packet has arrived.
  128. */
  129. psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
  130. {
  131. struct input_dev *dev = psmouse->dev;
  132. u8 *packet = psmouse->packet;
  133. int wheel;
  134. if (psmouse->pktcnt < psmouse->pktsize)
  135. return PSMOUSE_GOOD_DATA;
  136. /* Full packet accumulated, process it */
  137. switch (psmouse->protocol->type) {
  138. case PSMOUSE_IMPS:
  139. /* IntelliMouse has scroll wheel */
  140. input_report_rel(dev, REL_WHEEL, -(s8) packet[3]);
  141. break;
  142. case PSMOUSE_IMEX:
  143. /* Scroll wheel and buttons on IntelliMouse Explorer */
  144. switch (packet[3] & 0xC0) {
  145. case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
  146. input_report_rel(dev, REL_WHEEL,
  147. -sign_extend32(packet[3], 5));
  148. break;
  149. case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
  150. input_report_rel(dev, REL_HWHEEL,
  151. -sign_extend32(packet[3], 5));
  152. break;
  153. case 0x00:
  154. case 0xC0:
  155. wheel = sign_extend32(packet[3], 3);
  156. /*
  157. * Some A4Tech mice have two scroll wheels, with first
  158. * one reporting +/-1 in the lower nibble, and second
  159. * one reporting +/-2.
  160. */
  161. if (psmouse_a4tech_2wheels && abs(wheel) > 1)
  162. input_report_rel(dev, REL_HWHEEL, wheel / 2);
  163. else
  164. input_report_rel(dev, REL_WHEEL, -wheel);
  165. input_report_key(dev, BTN_SIDE, packet[3] & BIT(4));
  166. input_report_key(dev, BTN_EXTRA, packet[3] & BIT(5));
  167. break;
  168. }
  169. break;
  170. case PSMOUSE_GENPS:
  171. /* Report scroll buttons on NetMice */
  172. input_report_rel(dev, REL_WHEEL, -(s8) packet[3]);
  173. /* Extra buttons on Genius NewNet 3D */
  174. input_report_key(dev, BTN_SIDE, packet[0] & BIT(6));
  175. input_report_key(dev, BTN_EXTRA, packet[0] & BIT(7));
  176. break;
  177. case PSMOUSE_THINKPS:
  178. /* Extra button on ThinkingMouse */
  179. input_report_key(dev, BTN_EXTRA, packet[0] & BIT(3));
  180. /*
  181. * Without this bit of weirdness moving up gives wildly
  182. * high Y changes.
  183. */
  184. packet[1] |= (packet[0] & 0x40) << 1;
  185. break;
  186. case PSMOUSE_CORTRON:
  187. /*
  188. * Cortron PS2 Trackball reports SIDE button in the
  189. * 4th bit of the first byte.
  190. */
  191. input_report_key(dev, BTN_SIDE, packet[0] & BIT(3));
  192. packet[0] |= BIT(3);
  193. break;
  194. default:
  195. break;
  196. }
  197. /* Generic PS/2 Mouse */
  198. packet[0] |= psmouse->extra_buttons;
  199. psmouse_report_standard_packet(dev, packet);
  200. input_sync(dev);
  201. return PSMOUSE_FULL_PACKET;
  202. }
  203. void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
  204. unsigned long delay)
  205. {
  206. queue_delayed_work(kpsmoused_wq, work, delay);
  207. }
  208. /*
  209. * __psmouse_set_state() sets new psmouse state and resets all flags.
  210. */
  211. static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
  212. {
  213. psmouse->state = new_state;
  214. psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
  215. psmouse->ps2dev.flags = 0;
  216. psmouse->last = jiffies;
  217. }
  218. /*
  219. * psmouse_set_state() sets new psmouse state and resets all flags and
  220. * counters while holding serio lock so fighting with interrupt handler
  221. * is not a concern.
  222. */
  223. void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
  224. {
  225. serio_pause_rx(psmouse->ps2dev.serio);
  226. __psmouse_set_state(psmouse, new_state);
  227. serio_continue_rx(psmouse->ps2dev.serio);
  228. }
  229. /*
  230. * psmouse_handle_byte() processes one byte of the input data stream
  231. * by calling corresponding protocol handler.
  232. */
  233. static int psmouse_handle_byte(struct psmouse *psmouse)
  234. {
  235. psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
  236. switch (rc) {
  237. case PSMOUSE_BAD_DATA:
  238. if (psmouse->state == PSMOUSE_ACTIVATED) {
  239. psmouse_warn(psmouse,
  240. "%s at %s lost sync at byte %d\n",
  241. psmouse->name, psmouse->phys,
  242. psmouse->pktcnt);
  243. if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
  244. __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  245. psmouse_notice(psmouse,
  246. "issuing reconnect request\n");
  247. serio_reconnect(psmouse->ps2dev.serio);
  248. return -EIO;
  249. }
  250. }
  251. psmouse->pktcnt = 0;
  252. break;
  253. case PSMOUSE_FULL_PACKET:
  254. psmouse->pktcnt = 0;
  255. if (psmouse->out_of_sync_cnt) {
  256. psmouse->out_of_sync_cnt = 0;
  257. psmouse_notice(psmouse,
  258. "%s at %s - driver resynced.\n",
  259. psmouse->name, psmouse->phys);
  260. }
  261. break;
  262. case PSMOUSE_GOOD_DATA:
  263. break;
  264. }
  265. return 0;
  266. }
  267. static void psmouse_handle_oob_data(struct psmouse *psmouse, u8 data)
  268. {
  269. switch (psmouse->oob_data_type) {
  270. case PSMOUSE_OOB_NONE:
  271. psmouse->oob_data_type = data;
  272. break;
  273. case PSMOUSE_OOB_EXTRA_BTNS:
  274. psmouse_report_standard_buttons(psmouse->dev, data);
  275. input_sync(psmouse->dev);
  276. psmouse->extra_buttons = data;
  277. psmouse->oob_data_type = PSMOUSE_OOB_NONE;
  278. break;
  279. default:
  280. psmouse_warn(psmouse,
  281. "unknown OOB_DATA type: 0x%02x\n",
  282. psmouse->oob_data_type);
  283. psmouse->oob_data_type = PSMOUSE_OOB_NONE;
  284. break;
  285. }
  286. }
  287. /*
  288. * psmouse_interrupt() handles incoming characters, either passing them
  289. * for normal processing or gathering them as command response.
  290. */
  291. static irqreturn_t psmouse_interrupt(struct serio *serio,
  292. u8 data, unsigned int flags)
  293. {
  294. struct psmouse *psmouse = serio_get_drvdata(serio);
  295. if (psmouse->state == PSMOUSE_IGNORE)
  296. goto out;
  297. if (unlikely((flags & SERIO_TIMEOUT) ||
  298. ((flags & SERIO_PARITY) &&
  299. !psmouse->protocol->ignore_parity))) {
  300. if (psmouse->state == PSMOUSE_ACTIVATED)
  301. psmouse_warn(psmouse,
  302. "bad data from KBC -%s%s\n",
  303. flags & SERIO_TIMEOUT ? " timeout" : "",
  304. flags & SERIO_PARITY ? " bad parity" : "");
  305. ps2_cmd_aborted(&psmouse->ps2dev);
  306. goto out;
  307. }
  308. if (flags & SERIO_OOB_DATA) {
  309. psmouse_handle_oob_data(psmouse, data);
  310. goto out;
  311. }
  312. if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
  313. if (ps2_handle_ack(&psmouse->ps2dev, data))
  314. goto out;
  315. if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
  316. if (ps2_handle_response(&psmouse->ps2dev, data))
  317. goto out;
  318. if (psmouse->state <= PSMOUSE_RESYNCING)
  319. goto out;
  320. if (psmouse->state == PSMOUSE_ACTIVATED &&
  321. psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
  322. psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
  323. psmouse->name, psmouse->phys, psmouse->pktcnt);
  324. psmouse->badbyte = psmouse->packet[0];
  325. __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
  326. psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
  327. goto out;
  328. }
  329. psmouse->packet[psmouse->pktcnt++] = data;
  330. /* Check if this is a new device announcement (0xAA 0x00) */
  331. if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
  332. if (psmouse->pktcnt == 1) {
  333. psmouse->last = jiffies;
  334. goto out;
  335. }
  336. if (psmouse->packet[1] == PSMOUSE_RET_ID ||
  337. (psmouse->protocol->type == PSMOUSE_HGPK &&
  338. psmouse->packet[1] == PSMOUSE_RET_BAT)) {
  339. __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  340. serio_reconnect(serio);
  341. goto out;
  342. }
  343. /* Not a new device, try processing first byte normally */
  344. psmouse->pktcnt = 1;
  345. if (psmouse_handle_byte(psmouse))
  346. goto out;
  347. psmouse->packet[psmouse->pktcnt++] = data;
  348. }
  349. /*
  350. * See if we need to force resync because mouse was idle for
  351. * too long.
  352. */
  353. if (psmouse->state == PSMOUSE_ACTIVATED &&
  354. psmouse->pktcnt == 1 && psmouse->resync_time &&
  355. time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
  356. psmouse->badbyte = psmouse->packet[0];
  357. __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
  358. psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
  359. goto out;
  360. }
  361. psmouse->last = jiffies;
  362. psmouse_handle_byte(psmouse);
  363. out:
  364. return IRQ_HANDLED;
  365. }
  366. /*
  367. * psmouse_reset() resets the mouse into power-on state.
  368. */
  369. int psmouse_reset(struct psmouse *psmouse)
  370. {
  371. u8 param[2];
  372. int error;
  373. error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT);
  374. if (error)
  375. return error;
  376. if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
  377. return -EIO;
  378. return 0;
  379. }
  380. /*
  381. * Here we set the mouse resolution.
  382. */
  383. void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
  384. {
  385. static const u8 params[] = { 0, 1, 2, 2, 3 };
  386. u8 p;
  387. if (resolution == 0 || resolution > 200)
  388. resolution = 200;
  389. p = params[resolution / 50];
  390. ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
  391. psmouse->resolution = 25 << p;
  392. }
  393. /*
  394. * Here we set the mouse report rate.
  395. */
  396. static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
  397. {
  398. static const u8 rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
  399. u8 r;
  400. int i = 0;
  401. while (rates[i] > rate)
  402. i++;
  403. r = rates[i];
  404. ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
  405. psmouse->rate = r;
  406. }
  407. /*
  408. * Here we set the mouse scaling.
  409. */
  410. static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
  411. {
  412. ps2_command(&psmouse->ps2dev, NULL,
  413. scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
  414. PSMOUSE_CMD_SETSCALE11);
  415. }
  416. /*
  417. * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
  418. */
  419. static int psmouse_poll(struct psmouse *psmouse)
  420. {
  421. return ps2_command(&psmouse->ps2dev, psmouse->packet,
  422. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
  423. }
  424. static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
  425. {
  426. int i;
  427. for (i = 0; ids[i]; i++)
  428. if (!strcasecmp(id, ids[i]))
  429. return true;
  430. return false;
  431. }
  432. /*
  433. * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
  434. */
  435. bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
  436. {
  437. struct serio *serio = psmouse->ps2dev.serio;
  438. char *p, *fw_id_copy, *save_ptr;
  439. bool found = false;
  440. if (strncmp(serio->firmware_id, "PNP: ", 5))
  441. return false;
  442. fw_id_copy = kstrndup(&serio->firmware_id[5],
  443. sizeof(serio->firmware_id) - 5,
  444. GFP_KERNEL);
  445. if (!fw_id_copy)
  446. return false;
  447. save_ptr = fw_id_copy;
  448. while ((p = strsep(&fw_id_copy, " ")) != NULL) {
  449. if (psmouse_check_pnp_id(p, ids)) {
  450. found = true;
  451. break;
  452. }
  453. }
  454. kfree(save_ptr);
  455. return found;
  456. }
  457. /*
  458. * Genius NetMouse magic init.
  459. */
  460. static int genius_detect(struct psmouse *psmouse, bool set_properties)
  461. {
  462. struct ps2dev *ps2dev = &psmouse->ps2dev;
  463. u8 param[4];
  464. param[0] = 3;
  465. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  466. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  467. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  468. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
  469. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
  470. if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
  471. return -ENODEV;
  472. if (set_properties) {
  473. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  474. __set_bit(BTN_EXTRA, psmouse->dev->keybit);
  475. __set_bit(BTN_SIDE, psmouse->dev->keybit);
  476. __set_bit(REL_WHEEL, psmouse->dev->relbit);
  477. psmouse->vendor = "Genius";
  478. psmouse->name = "Mouse";
  479. psmouse->pktsize = 4;
  480. }
  481. return 0;
  482. }
  483. /*
  484. * IntelliMouse magic init.
  485. */
  486. static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
  487. {
  488. struct ps2dev *ps2dev = &psmouse->ps2dev;
  489. u8 param[2];
  490. param[0] = 200;
  491. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  492. param[0] = 100;
  493. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  494. param[0] = 80;
  495. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  496. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  497. if (param[0] != 3)
  498. return -ENODEV;
  499. if (set_properties) {
  500. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  501. __set_bit(REL_WHEEL, psmouse->dev->relbit);
  502. if (!psmouse->vendor)
  503. psmouse->vendor = "Generic";
  504. if (!psmouse->name)
  505. psmouse->name = "Wheel Mouse";
  506. psmouse->pktsize = 4;
  507. }
  508. return 0;
  509. }
  510. /*
  511. * Try IntelliMouse/Explorer magic init.
  512. */
  513. static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
  514. {
  515. struct ps2dev *ps2dev = &psmouse->ps2dev;
  516. u8 param[2];
  517. intellimouse_detect(psmouse, 0);
  518. param[0] = 200;
  519. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  520. param[0] = 200;
  521. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  522. param[0] = 80;
  523. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  524. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  525. if (param[0] != 4)
  526. return -ENODEV;
  527. /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
  528. param[0] = 200;
  529. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  530. param[0] = 80;
  531. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  532. param[0] = 40;
  533. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  534. if (set_properties) {
  535. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  536. __set_bit(REL_WHEEL, psmouse->dev->relbit);
  537. __set_bit(REL_HWHEEL, psmouse->dev->relbit);
  538. __set_bit(BTN_SIDE, psmouse->dev->keybit);
  539. __set_bit(BTN_EXTRA, psmouse->dev->keybit);
  540. if (!psmouse->vendor)
  541. psmouse->vendor = "Generic";
  542. if (!psmouse->name)
  543. psmouse->name = "Explorer Mouse";
  544. psmouse->pktsize = 4;
  545. }
  546. return 0;
  547. }
  548. /*
  549. * Kensington ThinkingMouse / ExpertMouse magic init.
  550. */
  551. static int thinking_detect(struct psmouse *psmouse, bool set_properties)
  552. {
  553. struct ps2dev *ps2dev = &psmouse->ps2dev;
  554. u8 param[2];
  555. static const u8 seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
  556. int i;
  557. param[0] = 10;
  558. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  559. param[0] = 0;
  560. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  561. for (i = 0; i < ARRAY_SIZE(seq); i++) {
  562. param[0] = seq[i];
  563. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  564. }
  565. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  566. if (param[0] != 2)
  567. return -ENODEV;
  568. if (set_properties) {
  569. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  570. __set_bit(BTN_EXTRA, psmouse->dev->keybit);
  571. psmouse->vendor = "Kensington";
  572. psmouse->name = "ThinkingMouse";
  573. }
  574. return 0;
  575. }
  576. /*
  577. * Bare PS/2 protocol "detection". Always succeeds.
  578. */
  579. static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
  580. {
  581. if (set_properties) {
  582. if (!psmouse->vendor)
  583. psmouse->vendor = "Generic";
  584. if (!psmouse->name)
  585. psmouse->name = "Mouse";
  586. /*
  587. * We have no way of figuring true number of buttons so let's
  588. * assume that the device has 3.
  589. */
  590. input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
  591. }
  592. return 0;
  593. }
  594. /*
  595. * Cortron PS/2 protocol detection. There's no special way to detect it, so it
  596. * must be forced by sysfs protocol writing.
  597. */
  598. static int cortron_detect(struct psmouse *psmouse, bool set_properties)
  599. {
  600. if (set_properties) {
  601. psmouse->vendor = "Cortron";
  602. psmouse->name = "PS/2 Trackball";
  603. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  604. __set_bit(BTN_SIDE, psmouse->dev->keybit);
  605. }
  606. return 0;
  607. }
  608. static const struct psmouse_protocol psmouse_protocols[] = {
  609. {
  610. .type = PSMOUSE_PS2,
  611. .name = "PS/2",
  612. .alias = "bare",
  613. .maxproto = true,
  614. .ignore_parity = true,
  615. .detect = ps2bare_detect,
  616. .try_passthru = true,
  617. },
  618. #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
  619. {
  620. .type = PSMOUSE_PS2PP,
  621. .name = "PS2++",
  622. .alias = "logitech",
  623. .detect = ps2pp_detect,
  624. },
  625. #endif
  626. {
  627. .type = PSMOUSE_THINKPS,
  628. .name = "ThinkPS/2",
  629. .alias = "thinkps",
  630. .detect = thinking_detect,
  631. },
  632. #ifdef CONFIG_MOUSE_PS2_CYPRESS
  633. {
  634. .type = PSMOUSE_CYPRESS,
  635. .name = "CyPS/2",
  636. .alias = "cypress",
  637. .detect = cypress_detect,
  638. .init = cypress_init,
  639. },
  640. #endif
  641. {
  642. .type = PSMOUSE_GENPS,
  643. .name = "GenPS/2",
  644. .alias = "genius",
  645. .detect = genius_detect,
  646. },
  647. {
  648. .type = PSMOUSE_IMPS,
  649. .name = "ImPS/2",
  650. .alias = "imps",
  651. .maxproto = true,
  652. .ignore_parity = true,
  653. .detect = intellimouse_detect,
  654. .try_passthru = true,
  655. },
  656. {
  657. .type = PSMOUSE_IMEX,
  658. .name = "ImExPS/2",
  659. .alias = "exps",
  660. .maxproto = true,
  661. .ignore_parity = true,
  662. .detect = im_explorer_detect,
  663. .try_passthru = true,
  664. },
  665. #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
  666. {
  667. .type = PSMOUSE_SYNAPTICS,
  668. .name = "SynPS/2",
  669. .alias = "synaptics",
  670. .detect = synaptics_detect,
  671. .init = synaptics_init_absolute,
  672. },
  673. {
  674. .type = PSMOUSE_SYNAPTICS_RELATIVE,
  675. .name = "SynRelPS/2",
  676. .alias = "synaptics-relative",
  677. .detect = synaptics_detect,
  678. .init = synaptics_init_relative,
  679. },
  680. #endif
  681. #ifdef CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS
  682. {
  683. .type = PSMOUSE_SYNAPTICS_SMBUS,
  684. .name = "SynSMBus",
  685. .alias = "synaptics-smbus",
  686. .detect = synaptics_detect,
  687. .init = synaptics_init_smbus,
  688. .smbus_companion = true,
  689. },
  690. #endif
  691. #ifdef CONFIG_MOUSE_PS2_ALPS
  692. {
  693. .type = PSMOUSE_ALPS,
  694. .name = "AlpsPS/2",
  695. .alias = "alps",
  696. .detect = alps_detect,
  697. .init = alps_init,
  698. },
  699. #endif
  700. #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
  701. {
  702. .type = PSMOUSE_LIFEBOOK,
  703. .name = "LBPS/2",
  704. .alias = "lifebook",
  705. .detect = lifebook_detect,
  706. .init = lifebook_init,
  707. },
  708. #endif
  709. #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
  710. {
  711. .type = PSMOUSE_TRACKPOINT,
  712. .name = "TPPS/2",
  713. .alias = "trackpoint",
  714. .detect = trackpoint_detect,
  715. .try_passthru = true,
  716. },
  717. #endif
  718. #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
  719. {
  720. .type = PSMOUSE_TOUCHKIT_PS2,
  721. .name = "touchkitPS/2",
  722. .alias = "touchkit",
  723. .detect = touchkit_ps2_detect,
  724. },
  725. #endif
  726. #ifdef CONFIG_MOUSE_PS2_OLPC
  727. {
  728. .type = PSMOUSE_HGPK,
  729. .name = "OLPC HGPK",
  730. .alias = "hgpk",
  731. .detect = hgpk_detect,
  732. },
  733. #endif
  734. #ifdef CONFIG_MOUSE_PS2_ELANTECH
  735. {
  736. .type = PSMOUSE_ELANTECH,
  737. .name = "ETPS/2",
  738. .alias = "elantech",
  739. .detect = elantech_detect,
  740. .init = elantech_init_ps2,
  741. },
  742. #endif
  743. #ifdef CONFIG_MOUSE_PS2_ELANTECH_SMBUS
  744. {
  745. .type = PSMOUSE_ELANTECH_SMBUS,
  746. .name = "ETSMBus",
  747. .alias = "elantech-smbus",
  748. .detect = elantech_detect,
  749. .init = elantech_init_smbus,
  750. .smbus_companion = true,
  751. },
  752. #endif
  753. #ifdef CONFIG_MOUSE_PS2_SENTELIC
  754. {
  755. .type = PSMOUSE_FSP,
  756. .name = "FSPPS/2",
  757. .alias = "fsp",
  758. .detect = fsp_detect,
  759. .init = fsp_init,
  760. },
  761. #endif
  762. {
  763. .type = PSMOUSE_CORTRON,
  764. .name = "CortronPS/2",
  765. .alias = "cortps",
  766. .detect = cortron_detect,
  767. },
  768. #ifdef CONFIG_MOUSE_PS2_FOCALTECH
  769. {
  770. .type = PSMOUSE_FOCALTECH,
  771. .name = "FocalTechPS/2",
  772. .alias = "focaltech",
  773. .detect = focaltech_detect,
  774. .init = focaltech_init,
  775. },
  776. #endif
  777. #ifdef CONFIG_MOUSE_PS2_VMMOUSE
  778. {
  779. .type = PSMOUSE_VMMOUSE,
  780. .name = VMMOUSE_PSNAME,
  781. .alias = "vmmouse",
  782. .detect = vmmouse_detect,
  783. .init = vmmouse_init,
  784. },
  785. #endif
  786. #ifdef CONFIG_MOUSE_PS2_BYD
  787. {
  788. .type = PSMOUSE_BYD,
  789. .name = "BYDPS/2",
  790. .alias = "byd",
  791. .detect = byd_detect,
  792. .init = byd_init,
  793. },
  794. #endif
  795. {
  796. .type = PSMOUSE_AUTO,
  797. .name = "auto",
  798. .alias = "any",
  799. .maxproto = true,
  800. },
  801. };
  802. static const struct psmouse_protocol *__psmouse_protocol_by_type(enum psmouse_type type)
  803. {
  804. int i;
  805. for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
  806. if (psmouse_protocols[i].type == type)
  807. return &psmouse_protocols[i];
  808. return NULL;
  809. }
  810. static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
  811. {
  812. const struct psmouse_protocol *proto;
  813. proto = __psmouse_protocol_by_type(type);
  814. if (proto)
  815. return proto;
  816. WARN_ON(1);
  817. return &psmouse_protocols[0];
  818. }
  819. static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
  820. {
  821. const struct psmouse_protocol *p;
  822. int i;
  823. for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
  824. p = &psmouse_protocols[i];
  825. if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
  826. (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
  827. return &psmouse_protocols[i];
  828. }
  829. return NULL;
  830. }
  831. /*
  832. * Apply default settings to the psmouse structure. Most of them will
  833. * be overridden by individual protocol initialization routines.
  834. */
  835. static void psmouse_apply_defaults(struct psmouse *psmouse)
  836. {
  837. struct input_dev *input_dev = psmouse->dev;
  838. bitmap_zero(input_dev->evbit, EV_CNT);
  839. bitmap_zero(input_dev->keybit, KEY_CNT);
  840. bitmap_zero(input_dev->relbit, REL_CNT);
  841. bitmap_zero(input_dev->absbit, ABS_CNT);
  842. bitmap_zero(input_dev->mscbit, MSC_CNT);
  843. input_set_capability(input_dev, EV_KEY, BTN_LEFT);
  844. input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
  845. input_set_capability(input_dev, EV_REL, REL_X);
  846. input_set_capability(input_dev, EV_REL, REL_Y);
  847. __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
  848. psmouse->protocol = &psmouse_protocols[0];
  849. psmouse->set_rate = psmouse_set_rate;
  850. psmouse->set_resolution = psmouse_set_resolution;
  851. psmouse->set_scale = psmouse_set_scale;
  852. psmouse->poll = psmouse_poll;
  853. psmouse->protocol_handler = psmouse_process_byte;
  854. psmouse->pktsize = 3;
  855. psmouse->reconnect = NULL;
  856. psmouse->fast_reconnect = NULL;
  857. psmouse->disconnect = NULL;
  858. psmouse->cleanup = NULL;
  859. psmouse->pt_activate = NULL;
  860. psmouse->pt_deactivate = NULL;
  861. }
  862. static bool psmouse_do_detect(int (*detect)(struct psmouse *, bool),
  863. struct psmouse *psmouse, bool allow_passthrough,
  864. bool set_properties)
  865. {
  866. if (psmouse->ps2dev.serio->id.type == SERIO_PS_PSTHRU &&
  867. !allow_passthrough) {
  868. return false;
  869. }
  870. if (set_properties)
  871. psmouse_apply_defaults(psmouse);
  872. return detect(psmouse, set_properties) == 0;
  873. }
  874. static bool psmouse_try_protocol(struct psmouse *psmouse,
  875. enum psmouse_type type,
  876. unsigned int *max_proto,
  877. bool set_properties, bool init_allowed)
  878. {
  879. const struct psmouse_protocol *proto;
  880. proto = __psmouse_protocol_by_type(type);
  881. if (!proto)
  882. return false;
  883. if (!psmouse_do_detect(proto->detect, psmouse, proto->try_passthru,
  884. set_properties))
  885. return false;
  886. if (set_properties && proto->init && init_allowed) {
  887. if (proto->init(psmouse) != 0) {
  888. /*
  889. * We detected device, but init failed. Adjust
  890. * max_proto so we only try standard protocols.
  891. */
  892. if (*max_proto > PSMOUSE_IMEX)
  893. *max_proto = PSMOUSE_IMEX;
  894. return false;
  895. }
  896. }
  897. return true;
  898. }
  899. /*
  900. * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
  901. * the mouse may have.
  902. */
  903. static int psmouse_extensions(struct psmouse *psmouse,
  904. unsigned int max_proto, bool set_properties)
  905. {
  906. bool synaptics_hardware = false;
  907. int ret;
  908. /*
  909. * Always check for focaltech, this is safe as it uses pnp-id
  910. * matching.
  911. */
  912. if (psmouse_do_detect(focaltech_detect,
  913. psmouse, false, set_properties)) {
  914. if (max_proto > PSMOUSE_IMEX &&
  915. IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH) &&
  916. (!set_properties || focaltech_init(psmouse) == 0)) {
  917. return PSMOUSE_FOCALTECH;
  918. }
  919. /*
  920. * Restrict psmouse_max_proto so that psmouse_initialize()
  921. * does not try to reset rate and resolution, because even
  922. * that upsets the device.
  923. * This also causes us to basically fall through to basic
  924. * protocol detection, where we fully reset the mouse,
  925. * and set it up as bare PS/2 protocol device.
  926. */
  927. psmouse_max_proto = max_proto = PSMOUSE_PS2;
  928. }
  929. /*
  930. * We always check for LifeBook because it does not disturb mouse
  931. * (it only checks DMI information).
  932. */
  933. if (psmouse_try_protocol(psmouse, PSMOUSE_LIFEBOOK, &max_proto,
  934. set_properties, max_proto > PSMOUSE_IMEX))
  935. return PSMOUSE_LIFEBOOK;
  936. if (psmouse_try_protocol(psmouse, PSMOUSE_VMMOUSE, &max_proto,
  937. set_properties, max_proto > PSMOUSE_IMEX))
  938. return PSMOUSE_VMMOUSE;
  939. /*
  940. * Try Kensington ThinkingMouse (we try first, because Synaptics
  941. * probe upsets the ThinkingMouse).
  942. */
  943. if (max_proto > PSMOUSE_IMEX &&
  944. psmouse_try_protocol(psmouse, PSMOUSE_THINKPS, &max_proto,
  945. set_properties, true)) {
  946. return PSMOUSE_THINKPS;
  947. }
  948. /*
  949. * Try Synaptics TouchPad. Note that probing is done even if
  950. * Synaptics protocol support is disabled in config - we need to
  951. * know if it is Synaptics so we can reset it properly after
  952. * probing for IntelliMouse.
  953. */
  954. if (max_proto > PSMOUSE_PS2 &&
  955. psmouse_do_detect(synaptics_detect,
  956. psmouse, false, set_properties)) {
  957. synaptics_hardware = true;
  958. if (max_proto > PSMOUSE_IMEX) {
  959. /*
  960. * Try activating protocol, but check if support is
  961. * enabled first, since we try detecting Synaptics
  962. * even when protocol is disabled.
  963. */
  964. if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) ||
  965. IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)) {
  966. if (!set_properties)
  967. return PSMOUSE_SYNAPTICS;
  968. ret = synaptics_init(psmouse);
  969. if (ret >= 0)
  970. return ret;
  971. }
  972. /*
  973. * Some Synaptics touchpads can emulate extended
  974. * protocols (like IMPS/2). Unfortunately
  975. * Logitech/Genius probes confuse some firmware
  976. * versions so we'll have to skip them.
  977. */
  978. max_proto = PSMOUSE_IMEX;
  979. }
  980. /*
  981. * Make sure that touchpad is in relative mode, gestures
  982. * (taps) are enabled.
  983. */
  984. synaptics_reset(psmouse);
  985. }
  986. /*
  987. * Try Cypress Trackpad. We must try it before Finger Sensing Pad
  988. * because Finger Sensing Pad probe upsets some modules of Cypress
  989. * Trackpads.
  990. */
  991. if (max_proto > PSMOUSE_IMEX &&
  992. psmouse_try_protocol(psmouse, PSMOUSE_CYPRESS, &max_proto,
  993. set_properties, true)) {
  994. return PSMOUSE_CYPRESS;
  995. }
  996. /* Try ALPS TouchPad */
  997. if (max_proto > PSMOUSE_IMEX) {
  998. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  999. if (psmouse_try_protocol(psmouse, PSMOUSE_ALPS,
  1000. &max_proto, set_properties, true))
  1001. return PSMOUSE_ALPS;
  1002. }
  1003. /* Try OLPC HGPK touchpad */
  1004. if (max_proto > PSMOUSE_IMEX &&
  1005. psmouse_try_protocol(psmouse, PSMOUSE_HGPK, &max_proto,
  1006. set_properties, true)) {
  1007. return PSMOUSE_HGPK;
  1008. }
  1009. /* Try Elantech touchpad */
  1010. if (max_proto > PSMOUSE_IMEX &&
  1011. psmouse_try_protocol(psmouse, PSMOUSE_ELANTECH,
  1012. &max_proto, set_properties, false)) {
  1013. if (!set_properties)
  1014. return PSMOUSE_ELANTECH;
  1015. ret = elantech_init(psmouse);
  1016. if (ret >= 0)
  1017. return ret;
  1018. }
  1019. if (max_proto > PSMOUSE_IMEX) {
  1020. if (psmouse_try_protocol(psmouse, PSMOUSE_GENPS,
  1021. &max_proto, set_properties, true))
  1022. return PSMOUSE_GENPS;
  1023. if (psmouse_try_protocol(psmouse, PSMOUSE_PS2PP,
  1024. &max_proto, set_properties, true))
  1025. return PSMOUSE_PS2PP;
  1026. if (psmouse_try_protocol(psmouse, PSMOUSE_TRACKPOINT,
  1027. &max_proto, set_properties, true))
  1028. return PSMOUSE_TRACKPOINT;
  1029. if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2,
  1030. &max_proto, set_properties, true))
  1031. return PSMOUSE_TOUCHKIT_PS2;
  1032. }
  1033. /*
  1034. * Try Finger Sensing Pad. We do it here because its probe upsets
  1035. * Trackpoint devices (causing TP_READ_ID command to time out).
  1036. */
  1037. if (max_proto > PSMOUSE_IMEX &&
  1038. psmouse_try_protocol(psmouse, PSMOUSE_FSP,
  1039. &max_proto, set_properties, true)) {
  1040. return PSMOUSE_FSP;
  1041. }
  1042. /*
  1043. * Reset to defaults in case the device got confused by extended
  1044. * protocol probes. Note that we follow up with full reset because
  1045. * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
  1046. */
  1047. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  1048. psmouse_reset(psmouse);
  1049. if (max_proto >= PSMOUSE_IMEX &&
  1050. psmouse_try_protocol(psmouse, PSMOUSE_IMEX,
  1051. &max_proto, set_properties, true)) {
  1052. return PSMOUSE_IMEX;
  1053. }
  1054. if (max_proto >= PSMOUSE_IMPS &&
  1055. psmouse_try_protocol(psmouse, PSMOUSE_IMPS,
  1056. &max_proto, set_properties, true)) {
  1057. return PSMOUSE_IMPS;
  1058. }
  1059. /*
  1060. * Okay, all failed, we have a standard mouse here. The number of
  1061. * the buttons is still a question, though. We assume 3.
  1062. */
  1063. psmouse_try_protocol(psmouse, PSMOUSE_PS2,
  1064. &max_proto, set_properties, true);
  1065. if (synaptics_hardware) {
  1066. /*
  1067. * We detected Synaptics hardware but it did not respond to
  1068. * IMPS/2 probes. We need to reset the touchpad because if
  1069. * there is a track point on the pass through port it could
  1070. * get disabled while probing for protocol extensions.
  1071. */
  1072. psmouse_reset(psmouse);
  1073. }
  1074. return PSMOUSE_PS2;
  1075. }
  1076. /*
  1077. * psmouse_probe() probes for a PS/2 mouse.
  1078. */
  1079. static int psmouse_probe(struct psmouse *psmouse)
  1080. {
  1081. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1082. u8 param[2];
  1083. int error;
  1084. /*
  1085. * First, we check if it's a mouse. It should send 0x00 or 0x03 in
  1086. * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
  1087. * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and
  1088. * subsequent ID queries, probably due to a firmware bug.
  1089. */
  1090. param[0] = 0xa5;
  1091. error = ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  1092. if (error)
  1093. return error;
  1094. if (param[0] != 0x00 && param[0] != 0x03 &&
  1095. param[0] != 0x04 && param[0] != 0xff)
  1096. return -ENODEV;
  1097. /*
  1098. * Then we reset and disable the mouse so that it doesn't generate
  1099. * events.
  1100. */
  1101. error = ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  1102. if (error)
  1103. psmouse_warn(psmouse, "Failed to reset mouse on %s: %d\n",
  1104. ps2dev->serio->phys, error);
  1105. return 0;
  1106. }
  1107. /*
  1108. * psmouse_initialize() initializes the mouse to a sane state.
  1109. */
  1110. static void psmouse_initialize(struct psmouse *psmouse)
  1111. {
  1112. /*
  1113. * We set the mouse report rate, resolution and scaling.
  1114. */
  1115. if (psmouse_max_proto != PSMOUSE_PS2) {
  1116. psmouse->set_rate(psmouse, psmouse->rate);
  1117. psmouse->set_resolution(psmouse, psmouse->resolution);
  1118. psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
  1119. }
  1120. }
  1121. /*
  1122. * psmouse_activate() enables the mouse so that we get motion reports from it.
  1123. */
  1124. int psmouse_activate(struct psmouse *psmouse)
  1125. {
  1126. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1127. psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
  1128. psmouse->ps2dev.serio->phys);
  1129. return -1;
  1130. }
  1131. psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
  1132. return 0;
  1133. }
  1134. /*
  1135. * psmouse_deactivate() puts the mouse into poll mode so that we don't get
  1136. * motion reports from it unless we explicitly request it.
  1137. */
  1138. int psmouse_deactivate(struct psmouse *psmouse)
  1139. {
  1140. int error;
  1141. error = ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE);
  1142. if (error) {
  1143. psmouse_warn(psmouse, "Failed to deactivate mouse on %s: %d\n",
  1144. psmouse->ps2dev.serio->phys, error);
  1145. return error;
  1146. }
  1147. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1148. return 0;
  1149. }
  1150. /*
  1151. * psmouse_resync() attempts to re-validate current protocol.
  1152. */
  1153. static void psmouse_resync(struct work_struct *work)
  1154. {
  1155. struct psmouse *parent = NULL, *psmouse =
  1156. container_of(work, struct psmouse, resync_work.work);
  1157. struct serio *serio = psmouse->ps2dev.serio;
  1158. psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
  1159. bool failed = false, enabled = false;
  1160. int i;
  1161. mutex_lock(&psmouse_mutex);
  1162. if (psmouse->state != PSMOUSE_RESYNCING)
  1163. goto out;
  1164. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1165. parent = serio_get_drvdata(serio->parent);
  1166. psmouse_deactivate(parent);
  1167. }
  1168. /*
  1169. * Some mice don't ACK commands sent while they are in the middle of
  1170. * transmitting motion packet. To avoid delay we use ps2_sendbyte()
  1171. * instead of ps2_command() which would wait for 200ms for an ACK
  1172. * that may never come.
  1173. * As an additional quirk ALPS touchpads may not only forget to ACK
  1174. * disable command but will stop reporting taps, so if we see that
  1175. * mouse at least once ACKs disable we will do full reconnect if ACK
  1176. * is missing.
  1177. */
  1178. psmouse->num_resyncs++;
  1179. if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
  1180. if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
  1181. failed = true;
  1182. } else
  1183. psmouse->acks_disable_command = true;
  1184. /*
  1185. * Poll the mouse. If it was reset the packet will be shorter than
  1186. * psmouse->pktsize and ps2_command will fail. We do not expect and
  1187. * do not handle scenario when mouse "upgrades" its protocol while
  1188. * disconnected since it would require additional delay. If we ever
  1189. * see a mouse that does it we'll adjust the code.
  1190. */
  1191. if (!failed) {
  1192. if (psmouse->poll(psmouse))
  1193. failed = true;
  1194. else {
  1195. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1196. for (i = 0; i < psmouse->pktsize; i++) {
  1197. psmouse->pktcnt++;
  1198. rc = psmouse->protocol_handler(psmouse);
  1199. if (rc != PSMOUSE_GOOD_DATA)
  1200. break;
  1201. }
  1202. if (rc != PSMOUSE_FULL_PACKET)
  1203. failed = true;
  1204. psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
  1205. }
  1206. }
  1207. /*
  1208. * Now try to enable mouse. We try to do that even if poll failed
  1209. * and also repeat our attempts 5 times, otherwise we may be left
  1210. * out with disabled mouse.
  1211. */
  1212. for (i = 0; i < 5; i++) {
  1213. if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
  1214. enabled = true;
  1215. break;
  1216. }
  1217. msleep(200);
  1218. }
  1219. if (!enabled) {
  1220. psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
  1221. psmouse->ps2dev.serio->phys);
  1222. failed = true;
  1223. }
  1224. if (failed) {
  1225. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1226. psmouse_info(psmouse,
  1227. "resync failed, issuing reconnect request\n");
  1228. serio_reconnect(serio);
  1229. } else
  1230. psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
  1231. if (parent)
  1232. psmouse_activate(parent);
  1233. out:
  1234. mutex_unlock(&psmouse_mutex);
  1235. }
  1236. /*
  1237. * psmouse_cleanup() resets the mouse into power-on state.
  1238. */
  1239. static void psmouse_cleanup(struct serio *serio)
  1240. {
  1241. struct psmouse *psmouse = serio_get_drvdata(serio);
  1242. struct psmouse *parent = NULL;
  1243. mutex_lock(&psmouse_mutex);
  1244. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1245. parent = serio_get_drvdata(serio->parent);
  1246. psmouse_deactivate(parent);
  1247. }
  1248. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1249. /*
  1250. * Disable stream mode so cleanup routine can proceed undisturbed.
  1251. */
  1252. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  1253. psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
  1254. psmouse->ps2dev.serio->phys);
  1255. if (psmouse->cleanup)
  1256. psmouse->cleanup(psmouse);
  1257. /*
  1258. * Reset the mouse to defaults (bare PS/2 protocol).
  1259. */
  1260. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  1261. /*
  1262. * Some boxes, such as HP nx7400, get terribly confused if mouse
  1263. * is not fully enabled before suspending/shutting down.
  1264. */
  1265. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  1266. if (parent) {
  1267. if (parent->pt_deactivate)
  1268. parent->pt_deactivate(parent);
  1269. psmouse_activate(parent);
  1270. }
  1271. mutex_unlock(&psmouse_mutex);
  1272. }
  1273. /*
  1274. * psmouse_disconnect() closes and frees.
  1275. */
  1276. static void psmouse_disconnect(struct serio *serio)
  1277. {
  1278. struct psmouse *psmouse = serio_get_drvdata(serio);
  1279. struct psmouse *parent = NULL;
  1280. sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
  1281. mutex_lock(&psmouse_mutex);
  1282. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1283. /* make sure we don't have a resync in progress */
  1284. mutex_unlock(&psmouse_mutex);
  1285. flush_workqueue(kpsmoused_wq);
  1286. mutex_lock(&psmouse_mutex);
  1287. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1288. parent = serio_get_drvdata(serio->parent);
  1289. psmouse_deactivate(parent);
  1290. }
  1291. if (psmouse->disconnect)
  1292. psmouse->disconnect(psmouse);
  1293. if (parent && parent->pt_deactivate)
  1294. parent->pt_deactivate(parent);
  1295. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1296. serio_close(serio);
  1297. serio_set_drvdata(serio, NULL);
  1298. if (psmouse->dev)
  1299. input_unregister_device(psmouse->dev);
  1300. kfree(psmouse);
  1301. if (parent)
  1302. psmouse_activate(parent);
  1303. mutex_unlock(&psmouse_mutex);
  1304. }
  1305. static int psmouse_switch_protocol(struct psmouse *psmouse,
  1306. const struct psmouse_protocol *proto)
  1307. {
  1308. const struct psmouse_protocol *selected_proto;
  1309. struct input_dev *input_dev = psmouse->dev;
  1310. enum psmouse_type type;
  1311. input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
  1312. if (proto && (proto->detect || proto->init)) {
  1313. psmouse_apply_defaults(psmouse);
  1314. if (proto->detect && proto->detect(psmouse, true) < 0)
  1315. return -1;
  1316. if (proto->init && proto->init(psmouse) < 0)
  1317. return -1;
  1318. selected_proto = proto;
  1319. } else {
  1320. type = psmouse_extensions(psmouse, psmouse_max_proto, true);
  1321. selected_proto = psmouse_protocol_by_type(type);
  1322. }
  1323. psmouse->protocol = selected_proto;
  1324. /*
  1325. * If mouse's packet size is 3 there is no point in polling the
  1326. * device in hopes to detect protocol reset - we won't get less
  1327. * than 3 bytes response anyhow.
  1328. */
  1329. if (psmouse->pktsize == 3)
  1330. psmouse->resync_time = 0;
  1331. /*
  1332. * Some smart KVMs fake response to POLL command returning just
  1333. * 3 bytes and messing up our resync logic, so if initial poll
  1334. * fails we won't try polling the device anymore. Hopefully
  1335. * such KVM will maintain initially selected protocol.
  1336. */
  1337. if (psmouse->resync_time && psmouse->poll(psmouse))
  1338. psmouse->resync_time = 0;
  1339. snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
  1340. selected_proto->name, psmouse->vendor, psmouse->name);
  1341. input_dev->name = psmouse->devname;
  1342. input_dev->phys = psmouse->phys;
  1343. input_dev->id.bustype = BUS_I8042;
  1344. input_dev->id.vendor = 0x0002;
  1345. input_dev->id.product = psmouse->protocol->type;
  1346. input_dev->id.version = psmouse->model;
  1347. return 0;
  1348. }
  1349. /*
  1350. * psmouse_connect() is a callback from the serio module when
  1351. * an unhandled serio port is found.
  1352. */
  1353. static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
  1354. {
  1355. struct psmouse *psmouse, *parent = NULL;
  1356. struct input_dev *input_dev;
  1357. int retval = 0, error = -ENOMEM;
  1358. mutex_lock(&psmouse_mutex);
  1359. /*
  1360. * If this is a pass-through port deactivate parent so the device
  1361. * connected to this port can be successfully identified
  1362. */
  1363. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1364. parent = serio_get_drvdata(serio->parent);
  1365. psmouse_deactivate(parent);
  1366. }
  1367. psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
  1368. input_dev = input_allocate_device();
  1369. if (!psmouse || !input_dev)
  1370. goto err_free;
  1371. ps2_init(&psmouse->ps2dev, serio);
  1372. INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
  1373. psmouse->dev = input_dev;
  1374. snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
  1375. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1376. serio_set_drvdata(serio, psmouse);
  1377. error = serio_open(serio, drv);
  1378. if (error)
  1379. goto err_clear_drvdata;
  1380. /* give PT device some time to settle down before probing */
  1381. if (serio->id.type == SERIO_PS_PSTHRU)
  1382. usleep_range(10000, 15000);
  1383. if (psmouse_probe(psmouse) < 0) {
  1384. error = -ENODEV;
  1385. goto err_close_serio;
  1386. }
  1387. psmouse->rate = psmouse_rate;
  1388. psmouse->resolution = psmouse_resolution;
  1389. psmouse->resetafter = psmouse_resetafter;
  1390. psmouse->resync_time = parent ? 0 : psmouse_resync_time;
  1391. psmouse->smartscroll = psmouse_smartscroll;
  1392. psmouse_switch_protocol(psmouse, NULL);
  1393. if (!psmouse->protocol->smbus_companion) {
  1394. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1395. psmouse_initialize(psmouse);
  1396. error = input_register_device(input_dev);
  1397. if (error)
  1398. goto err_protocol_disconnect;
  1399. } else {
  1400. /* Smbus companion will be reporting events, not us. */
  1401. input_free_device(input_dev);
  1402. psmouse->dev = input_dev = NULL;
  1403. }
  1404. if (parent && parent->pt_activate)
  1405. parent->pt_activate(parent);
  1406. error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
  1407. if (error)
  1408. goto err_pt_deactivate;
  1409. /*
  1410. * PS/2 devices having SMBus companions should stay disabled
  1411. * on PS/2 side, in order to have SMBus part operable.
  1412. */
  1413. if (!psmouse->protocol->smbus_companion)
  1414. psmouse_activate(psmouse);
  1415. out:
  1416. /* If this is a pass-through port the parent needs to be re-activated */
  1417. if (parent)
  1418. psmouse_activate(parent);
  1419. mutex_unlock(&psmouse_mutex);
  1420. return retval;
  1421. err_pt_deactivate:
  1422. if (parent && parent->pt_deactivate)
  1423. parent->pt_deactivate(parent);
  1424. if (input_dev) {
  1425. input_unregister_device(input_dev);
  1426. input_dev = NULL; /* so we don't try to free it below */
  1427. }
  1428. err_protocol_disconnect:
  1429. if (psmouse->disconnect)
  1430. psmouse->disconnect(psmouse);
  1431. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1432. err_close_serio:
  1433. serio_close(serio);
  1434. err_clear_drvdata:
  1435. serio_set_drvdata(serio, NULL);
  1436. err_free:
  1437. input_free_device(input_dev);
  1438. kfree(psmouse);
  1439. retval = error;
  1440. goto out;
  1441. }
  1442. static int __psmouse_reconnect(struct serio *serio, bool fast_reconnect)
  1443. {
  1444. struct psmouse *psmouse = serio_get_drvdata(serio);
  1445. struct psmouse *parent = NULL;
  1446. int (*reconnect_handler)(struct psmouse *);
  1447. enum psmouse_type type;
  1448. int rc = -1;
  1449. mutex_lock(&psmouse_mutex);
  1450. if (fast_reconnect) {
  1451. reconnect_handler = psmouse->fast_reconnect;
  1452. if (!reconnect_handler) {
  1453. rc = -ENOENT;
  1454. goto out_unlock;
  1455. }
  1456. } else {
  1457. reconnect_handler = psmouse->reconnect;
  1458. }
  1459. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1460. parent = serio_get_drvdata(serio->parent);
  1461. psmouse_deactivate(parent);
  1462. }
  1463. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1464. if (reconnect_handler) {
  1465. if (reconnect_handler(psmouse))
  1466. goto out;
  1467. } else {
  1468. psmouse_reset(psmouse);
  1469. if (psmouse_probe(psmouse) < 0)
  1470. goto out;
  1471. type = psmouse_extensions(psmouse, psmouse_max_proto, false);
  1472. if (psmouse->protocol->type != type)
  1473. goto out;
  1474. }
  1475. /*
  1476. * OK, the device type (and capabilities) match the old one,
  1477. * we can continue using it, complete initialization
  1478. */
  1479. if (!psmouse->protocol->smbus_companion) {
  1480. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1481. psmouse_initialize(psmouse);
  1482. }
  1483. if (parent && parent->pt_activate)
  1484. parent->pt_activate(parent);
  1485. /*
  1486. * PS/2 devices having SMBus companions should stay disabled
  1487. * on PS/2 side, in order to have SMBus part operable.
  1488. */
  1489. if (!psmouse->protocol->smbus_companion)
  1490. psmouse_activate(psmouse);
  1491. rc = 0;
  1492. out:
  1493. /* If this is a pass-through port the parent waits to be activated */
  1494. if (parent)
  1495. psmouse_activate(parent);
  1496. out_unlock:
  1497. mutex_unlock(&psmouse_mutex);
  1498. return rc;
  1499. }
  1500. static int psmouse_reconnect(struct serio *serio)
  1501. {
  1502. return __psmouse_reconnect(serio, false);
  1503. }
  1504. static int psmouse_fast_reconnect(struct serio *serio)
  1505. {
  1506. return __psmouse_reconnect(serio, true);
  1507. }
  1508. static struct serio_device_id psmouse_serio_ids[] = {
  1509. {
  1510. .type = SERIO_8042,
  1511. .proto = SERIO_ANY,
  1512. .id = SERIO_ANY,
  1513. .extra = SERIO_ANY,
  1514. },
  1515. {
  1516. .type = SERIO_PS_PSTHRU,
  1517. .proto = SERIO_ANY,
  1518. .id = SERIO_ANY,
  1519. .extra = SERIO_ANY,
  1520. },
  1521. { 0 }
  1522. };
  1523. MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
  1524. static struct serio_driver psmouse_drv = {
  1525. .driver = {
  1526. .name = "psmouse",
  1527. },
  1528. .description = DRIVER_DESC,
  1529. .id_table = psmouse_serio_ids,
  1530. .interrupt = psmouse_interrupt,
  1531. .connect = psmouse_connect,
  1532. .reconnect = psmouse_reconnect,
  1533. .fast_reconnect = psmouse_fast_reconnect,
  1534. .disconnect = psmouse_disconnect,
  1535. .cleanup = psmouse_cleanup,
  1536. };
  1537. ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
  1538. char *buf)
  1539. {
  1540. struct serio *serio = to_serio_port(dev);
  1541. struct psmouse_attribute *attr = to_psmouse_attr(devattr);
  1542. struct psmouse *psmouse = serio_get_drvdata(serio);
  1543. if (psmouse->protocol->smbus_companion &&
  1544. devattr != &psmouse_attr_protocol.dattr)
  1545. return -ENOENT;
  1546. return attr->show(psmouse, attr->data, buf);
  1547. }
  1548. ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
  1549. const char *buf, size_t count)
  1550. {
  1551. struct serio *serio = to_serio_port(dev);
  1552. struct psmouse_attribute *attr = to_psmouse_attr(devattr);
  1553. struct psmouse *psmouse, *parent = NULL;
  1554. int retval;
  1555. retval = mutex_lock_interruptible(&psmouse_mutex);
  1556. if (retval)
  1557. goto out;
  1558. psmouse = serio_get_drvdata(serio);
  1559. if (psmouse->protocol->smbus_companion &&
  1560. devattr != &psmouse_attr_protocol.dattr) {
  1561. retval = -ENOENT;
  1562. goto out_unlock;
  1563. }
  1564. if (attr->protect) {
  1565. if (psmouse->state == PSMOUSE_IGNORE) {
  1566. retval = -ENODEV;
  1567. goto out_unlock;
  1568. }
  1569. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1570. parent = serio_get_drvdata(serio->parent);
  1571. psmouse_deactivate(parent);
  1572. }
  1573. if (!psmouse->protocol->smbus_companion)
  1574. psmouse_deactivate(psmouse);
  1575. }
  1576. retval = attr->set(psmouse, attr->data, buf, count);
  1577. if (attr->protect) {
  1578. if (retval != -ENODEV && !psmouse->protocol->smbus_companion)
  1579. psmouse_activate(psmouse);
  1580. if (parent)
  1581. psmouse_activate(parent);
  1582. }
  1583. out_unlock:
  1584. mutex_unlock(&psmouse_mutex);
  1585. out:
  1586. return retval;
  1587. }
  1588. static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
  1589. {
  1590. unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
  1591. return sprintf(buf, "%u\n", *field);
  1592. }
  1593. static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
  1594. {
  1595. unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
  1596. unsigned int value;
  1597. int err;
  1598. err = kstrtouint(buf, 10, &value);
  1599. if (err)
  1600. return err;
  1601. *field = value;
  1602. return count;
  1603. }
  1604. static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
  1605. {
  1606. return sprintf(buf, "%s\n", psmouse->protocol->name);
  1607. }
  1608. static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
  1609. {
  1610. struct serio *serio = psmouse->ps2dev.serio;
  1611. struct psmouse *parent = NULL;
  1612. struct input_dev *old_dev, *new_dev;
  1613. const struct psmouse_protocol *proto, *old_proto;
  1614. int error;
  1615. int retry = 0;
  1616. proto = psmouse_protocol_by_name(buf, count);
  1617. if (!proto)
  1618. return -EINVAL;
  1619. if (psmouse->protocol == proto)
  1620. return count;
  1621. new_dev = input_allocate_device();
  1622. if (!new_dev)
  1623. return -ENOMEM;
  1624. while (!list_empty(&serio->children)) {
  1625. if (++retry > 3) {
  1626. psmouse_warn(psmouse,
  1627. "failed to destroy children ports, protocol change aborted.\n");
  1628. input_free_device(new_dev);
  1629. return -EIO;
  1630. }
  1631. mutex_unlock(&psmouse_mutex);
  1632. serio_unregister_child_port(serio);
  1633. mutex_lock(&psmouse_mutex);
  1634. if (serio->drv != &psmouse_drv) {
  1635. input_free_device(new_dev);
  1636. return -ENODEV;
  1637. }
  1638. if (psmouse->protocol == proto) {
  1639. input_free_device(new_dev);
  1640. return count; /* switched by other thread */
  1641. }
  1642. }
  1643. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1644. parent = serio_get_drvdata(serio->parent);
  1645. if (parent->pt_deactivate)
  1646. parent->pt_deactivate(parent);
  1647. }
  1648. old_dev = psmouse->dev;
  1649. old_proto = psmouse->protocol;
  1650. if (psmouse->disconnect)
  1651. psmouse->disconnect(psmouse);
  1652. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1653. psmouse->dev = new_dev;
  1654. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1655. if (psmouse_switch_protocol(psmouse, proto) < 0) {
  1656. psmouse_reset(psmouse);
  1657. /* default to PSMOUSE_PS2 */
  1658. psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
  1659. }
  1660. psmouse_initialize(psmouse);
  1661. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1662. if (psmouse->protocol->smbus_companion) {
  1663. input_free_device(psmouse->dev);
  1664. psmouse->dev = NULL;
  1665. } else {
  1666. error = input_register_device(psmouse->dev);
  1667. if (error) {
  1668. if (psmouse->disconnect)
  1669. psmouse->disconnect(psmouse);
  1670. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1671. input_free_device(new_dev);
  1672. psmouse->dev = old_dev;
  1673. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1674. psmouse_switch_protocol(psmouse, old_proto);
  1675. psmouse_initialize(psmouse);
  1676. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1677. return error;
  1678. }
  1679. }
  1680. if (old_dev)
  1681. input_unregister_device(old_dev);
  1682. if (parent && parent->pt_activate)
  1683. parent->pt_activate(parent);
  1684. return count;
  1685. }
  1686. static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
  1687. {
  1688. unsigned int value;
  1689. int err;
  1690. err = kstrtouint(buf, 10, &value);
  1691. if (err)
  1692. return err;
  1693. psmouse->set_rate(psmouse, value);
  1694. return count;
  1695. }
  1696. static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
  1697. {
  1698. unsigned int value;
  1699. int err;
  1700. err = kstrtouint(buf, 10, &value);
  1701. if (err)
  1702. return err;
  1703. psmouse->set_resolution(psmouse, value);
  1704. return count;
  1705. }
  1706. static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
  1707. {
  1708. const struct psmouse_protocol *proto;
  1709. if (!val)
  1710. return -EINVAL;
  1711. proto = psmouse_protocol_by_name(val, strlen(val));
  1712. if (!proto || !proto->maxproto)
  1713. return -EINVAL;
  1714. *((unsigned int *)kp->arg) = proto->type;
  1715. return 0;
  1716. }
  1717. static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
  1718. {
  1719. int type = *((unsigned int *)kp->arg);
  1720. return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
  1721. }
  1722. static int __init psmouse_init(void)
  1723. {
  1724. int err;
  1725. lifebook_module_init();
  1726. synaptics_module_init();
  1727. hgpk_module_init();
  1728. err = psmouse_smbus_module_init();
  1729. if (err)
  1730. return err;
  1731. kpsmoused_wq = alloc_ordered_workqueue("kpsmoused", 0);
  1732. if (!kpsmoused_wq) {
  1733. pr_err("failed to create kpsmoused workqueue\n");
  1734. err = -ENOMEM;
  1735. goto err_smbus_exit;
  1736. }
  1737. err = serio_register_driver(&psmouse_drv);
  1738. if (err)
  1739. goto err_destroy_wq;
  1740. return 0;
  1741. err_destroy_wq:
  1742. destroy_workqueue(kpsmoused_wq);
  1743. err_smbus_exit:
  1744. psmouse_smbus_module_exit();
  1745. return err;
  1746. }
  1747. static void __exit psmouse_exit(void)
  1748. {
  1749. serio_unregister_driver(&psmouse_drv);
  1750. destroy_workqueue(kpsmoused_wq);
  1751. psmouse_smbus_module_exit();
  1752. }
  1753. module_init(psmouse_init);
  1754. module_exit(psmouse_exit);