psmouse-base.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  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 const 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 = true;
  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. /* give PT device some time to settle down before probing */
  1300. if (serio->id.type == SERIO_PS_PSTHRU)
  1301. usleep_range(10000, 15000);
  1302. if (psmouse_probe(psmouse) < 0) {
  1303. error = -ENODEV;
  1304. goto err_close_serio;
  1305. }
  1306. psmouse->rate = psmouse_rate;
  1307. psmouse->resolution = psmouse_resolution;
  1308. psmouse->resetafter = psmouse_resetafter;
  1309. psmouse->resync_time = parent ? 0 : psmouse_resync_time;
  1310. psmouse->smartscroll = psmouse_smartscroll;
  1311. psmouse_switch_protocol(psmouse, NULL);
  1312. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1313. psmouse_initialize(psmouse);
  1314. error = input_register_device(psmouse->dev);
  1315. if (error)
  1316. goto err_protocol_disconnect;
  1317. if (parent && parent->pt_activate)
  1318. parent->pt_activate(parent);
  1319. error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
  1320. if (error)
  1321. goto err_pt_deactivate;
  1322. psmouse_activate(psmouse);
  1323. out:
  1324. /* If this is a pass-through port the parent needs to be re-activated */
  1325. if (parent)
  1326. psmouse_activate(parent);
  1327. mutex_unlock(&psmouse_mutex);
  1328. return retval;
  1329. err_pt_deactivate:
  1330. if (parent && parent->pt_deactivate)
  1331. parent->pt_deactivate(parent);
  1332. input_unregister_device(psmouse->dev);
  1333. input_dev = NULL; /* so we don't try to free it below */
  1334. err_protocol_disconnect:
  1335. if (psmouse->disconnect)
  1336. psmouse->disconnect(psmouse);
  1337. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1338. err_close_serio:
  1339. serio_close(serio);
  1340. err_clear_drvdata:
  1341. serio_set_drvdata(serio, NULL);
  1342. err_free:
  1343. input_free_device(input_dev);
  1344. kfree(psmouse);
  1345. retval = error;
  1346. goto out;
  1347. }
  1348. static int psmouse_reconnect(struct serio *serio)
  1349. {
  1350. struct psmouse *psmouse = serio_get_drvdata(serio);
  1351. struct psmouse *parent = NULL;
  1352. unsigned char type;
  1353. int rc = -1;
  1354. mutex_lock(&psmouse_mutex);
  1355. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1356. parent = serio_get_drvdata(serio->parent);
  1357. psmouse_deactivate(parent);
  1358. }
  1359. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1360. if (psmouse->reconnect) {
  1361. if (psmouse->reconnect(psmouse))
  1362. goto out;
  1363. } else {
  1364. psmouse_reset(psmouse);
  1365. if (psmouse_probe(psmouse) < 0)
  1366. goto out;
  1367. type = psmouse_extensions(psmouse, psmouse_max_proto, false);
  1368. if (psmouse->type != type)
  1369. goto out;
  1370. }
  1371. /*
  1372. * OK, the device type (and capabilities) match the old one,
  1373. * we can continue using it, complete initialization
  1374. */
  1375. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1376. psmouse_initialize(psmouse);
  1377. if (parent && parent->pt_activate)
  1378. parent->pt_activate(parent);
  1379. psmouse_activate(psmouse);
  1380. rc = 0;
  1381. out:
  1382. /* If this is a pass-through port the parent waits to be activated */
  1383. if (parent)
  1384. psmouse_activate(parent);
  1385. mutex_unlock(&psmouse_mutex);
  1386. return rc;
  1387. }
  1388. static struct serio_device_id psmouse_serio_ids[] = {
  1389. {
  1390. .type = SERIO_8042,
  1391. .proto = SERIO_ANY,
  1392. .id = SERIO_ANY,
  1393. .extra = SERIO_ANY,
  1394. },
  1395. {
  1396. .type = SERIO_PS_PSTHRU,
  1397. .proto = SERIO_ANY,
  1398. .id = SERIO_ANY,
  1399. .extra = SERIO_ANY,
  1400. },
  1401. { 0 }
  1402. };
  1403. MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
  1404. static struct serio_driver psmouse_drv = {
  1405. .driver = {
  1406. .name = "psmouse",
  1407. },
  1408. .description = DRIVER_DESC,
  1409. .id_table = psmouse_serio_ids,
  1410. .interrupt = psmouse_interrupt,
  1411. .connect = psmouse_connect,
  1412. .reconnect = psmouse_reconnect,
  1413. .disconnect = psmouse_disconnect,
  1414. .cleanup = psmouse_cleanup,
  1415. };
  1416. ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
  1417. char *buf)
  1418. {
  1419. struct serio *serio = to_serio_port(dev);
  1420. struct psmouse_attribute *attr = to_psmouse_attr(devattr);
  1421. struct psmouse *psmouse;
  1422. psmouse = serio_get_drvdata(serio);
  1423. return attr->show(psmouse, attr->data, buf);
  1424. }
  1425. ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
  1426. const char *buf, size_t count)
  1427. {
  1428. struct serio *serio = to_serio_port(dev);
  1429. struct psmouse_attribute *attr = to_psmouse_attr(devattr);
  1430. struct psmouse *psmouse, *parent = NULL;
  1431. int retval;
  1432. retval = mutex_lock_interruptible(&psmouse_mutex);
  1433. if (retval)
  1434. goto out;
  1435. psmouse = serio_get_drvdata(serio);
  1436. if (attr->protect) {
  1437. if (psmouse->state == PSMOUSE_IGNORE) {
  1438. retval = -ENODEV;
  1439. goto out_unlock;
  1440. }
  1441. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1442. parent = serio_get_drvdata(serio->parent);
  1443. psmouse_deactivate(parent);
  1444. }
  1445. psmouse_deactivate(psmouse);
  1446. }
  1447. retval = attr->set(psmouse, attr->data, buf, count);
  1448. if (attr->protect) {
  1449. if (retval != -ENODEV)
  1450. psmouse_activate(psmouse);
  1451. if (parent)
  1452. psmouse_activate(parent);
  1453. }
  1454. out_unlock:
  1455. mutex_unlock(&psmouse_mutex);
  1456. out:
  1457. return retval;
  1458. }
  1459. static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
  1460. {
  1461. unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
  1462. return sprintf(buf, "%u\n", *field);
  1463. }
  1464. static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
  1465. {
  1466. unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
  1467. unsigned int value;
  1468. int err;
  1469. err = kstrtouint(buf, 10, &value);
  1470. if (err)
  1471. return err;
  1472. *field = value;
  1473. return count;
  1474. }
  1475. static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
  1476. {
  1477. return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
  1478. }
  1479. static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
  1480. {
  1481. struct serio *serio = psmouse->ps2dev.serio;
  1482. struct psmouse *parent = NULL;
  1483. struct input_dev *old_dev, *new_dev;
  1484. const struct psmouse_protocol *proto, *old_proto;
  1485. int error;
  1486. int retry = 0;
  1487. proto = psmouse_protocol_by_name(buf, count);
  1488. if (!proto)
  1489. return -EINVAL;
  1490. if (psmouse->type == proto->type)
  1491. return count;
  1492. new_dev = input_allocate_device();
  1493. if (!new_dev)
  1494. return -ENOMEM;
  1495. while (!list_empty(&serio->children)) {
  1496. if (++retry > 3) {
  1497. psmouse_warn(psmouse,
  1498. "failed to destroy children ports, protocol change aborted.\n");
  1499. input_free_device(new_dev);
  1500. return -EIO;
  1501. }
  1502. mutex_unlock(&psmouse_mutex);
  1503. serio_unregister_child_port(serio);
  1504. mutex_lock(&psmouse_mutex);
  1505. if (serio->drv != &psmouse_drv) {
  1506. input_free_device(new_dev);
  1507. return -ENODEV;
  1508. }
  1509. if (psmouse->type == proto->type) {
  1510. input_free_device(new_dev);
  1511. return count; /* switched by other thread */
  1512. }
  1513. }
  1514. if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
  1515. parent = serio_get_drvdata(serio->parent);
  1516. if (parent->pt_deactivate)
  1517. parent->pt_deactivate(parent);
  1518. }
  1519. old_dev = psmouse->dev;
  1520. old_proto = psmouse_protocol_by_type(psmouse->type);
  1521. if (psmouse->disconnect)
  1522. psmouse->disconnect(psmouse);
  1523. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1524. psmouse->dev = new_dev;
  1525. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1526. if (psmouse_switch_protocol(psmouse, proto) < 0) {
  1527. psmouse_reset(psmouse);
  1528. /* default to PSMOUSE_PS2 */
  1529. psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
  1530. }
  1531. psmouse_initialize(psmouse);
  1532. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1533. error = input_register_device(psmouse->dev);
  1534. if (error) {
  1535. if (psmouse->disconnect)
  1536. psmouse->disconnect(psmouse);
  1537. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  1538. input_free_device(new_dev);
  1539. psmouse->dev = old_dev;
  1540. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  1541. psmouse_switch_protocol(psmouse, old_proto);
  1542. psmouse_initialize(psmouse);
  1543. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  1544. return error;
  1545. }
  1546. input_unregister_device(old_dev);
  1547. if (parent && parent->pt_activate)
  1548. parent->pt_activate(parent);
  1549. return count;
  1550. }
  1551. static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
  1552. {
  1553. unsigned int value;
  1554. int err;
  1555. err = kstrtouint(buf, 10, &value);
  1556. if (err)
  1557. return err;
  1558. psmouse->set_rate(psmouse, value);
  1559. return count;
  1560. }
  1561. static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
  1562. {
  1563. unsigned int value;
  1564. int err;
  1565. err = kstrtouint(buf, 10, &value);
  1566. if (err)
  1567. return err;
  1568. psmouse->set_resolution(psmouse, value);
  1569. return count;
  1570. }
  1571. static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
  1572. {
  1573. const struct psmouse_protocol *proto;
  1574. if (!val)
  1575. return -EINVAL;
  1576. proto = psmouse_protocol_by_name(val, strlen(val));
  1577. if (!proto || !proto->maxproto)
  1578. return -EINVAL;
  1579. *((unsigned int *)kp->arg) = proto->type;
  1580. return 0;
  1581. }
  1582. static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
  1583. {
  1584. int type = *((unsigned int *)kp->arg);
  1585. return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
  1586. }
  1587. static int __init psmouse_init(void)
  1588. {
  1589. int err;
  1590. lifebook_module_init();
  1591. synaptics_module_init();
  1592. hgpk_module_init();
  1593. kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
  1594. if (!kpsmoused_wq) {
  1595. pr_err("failed to create kpsmoused workqueue\n");
  1596. return -ENOMEM;
  1597. }
  1598. err = serio_register_driver(&psmouse_drv);
  1599. if (err)
  1600. destroy_workqueue(kpsmoused_wq);
  1601. return err;
  1602. }
  1603. static void __exit psmouse_exit(void)
  1604. {
  1605. serio_unregister_driver(&psmouse_drv);
  1606. destroy_workqueue(kpsmoused_wq);
  1607. }
  1608. module_init(psmouse_init);
  1609. module_exit(psmouse_exit);