psmouse-base.c 48 KB

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