thinkpad_acpi.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076
  1. /*
  2. * thinkpad_acpi.c - ThinkPad ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #define IBM_VERSION "0.14"
  24. /*
  25. * Changelog:
  26. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  27. * drivers/misc.
  28. *
  29. * 2006-11-22 0.13 new maintainer
  30. * changelog now lives in git commit history, and will
  31. * not be updated further in-file.
  32. *
  33. * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
  34. * 2005-03-17 0.11 support for 600e, 770x
  35. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  36. * support for 770e, G41
  37. * G40 and G41 don't have a thinklight
  38. * temperatures no longer experimental
  39. * experimental brightness control
  40. * experimental volume control
  41. * experimental fan enable/disable
  42. * 2005-01-16 0.10 fix module loading on R30, R31
  43. * 2005-01-16 0.9 support for 570, R30, R31
  44. * ultrabay support on A22p, A3x
  45. * limit arg for cmos, led, beep, drop experimental status
  46. * more capable led control on A21e, A22p, T20-22, X20
  47. * experimental temperatures and fan speed
  48. * experimental embedded controller register dump
  49. * mark more functions as __init, drop incorrect __exit
  50. * use MODULE_VERSION
  51. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  52. * fix parameter passing on module loading
  53. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  54. * thanks to Jim Radford <radford@blackbean.org>
  55. * 2004-11-08 0.8 fix init error case, don't return from a macro
  56. * thanks to Chris Wright <chrisw@osdl.org>
  57. * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
  58. * fix led control on A21e
  59. * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
  60. * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
  61. * proc file format changed
  62. * video_switch command
  63. * experimental cmos control
  64. * experimental led control
  65. * experimental acpi sounds
  66. * 2004-09-16 0.4 support for module parameters
  67. * hotkey mask can be prefixed by 0x
  68. * video output switching
  69. * video expansion control
  70. * ultrabay eject support
  71. * removed lcd brightness/on/off control, didn't work
  72. * 2004-08-17 0.3 support for R40
  73. * lcd off, brightness control
  74. * thinklight on/off
  75. * 2004-08-14 0.2 support for T series, X20
  76. * bluetooth enable/disable
  77. * hotkey events disabled by default
  78. * removed fan control, currently useless
  79. * 2004-08-09 0.1 initial release, support for X series
  80. */
  81. #include "thinkpad_acpi.h"
  82. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  83. MODULE_DESCRIPTION(IBM_DESC);
  84. MODULE_VERSION(IBM_VERSION);
  85. MODULE_LICENSE("GPL");
  86. /* Please remove this in year 2009 */
  87. MODULE_ALIAS("ibm_acpi");
  88. #define __unused __attribute__ ((unused))
  89. /****************************************************************************
  90. ****************************************************************************
  91. *
  92. * ACPI Helpers and device model
  93. *
  94. ****************************************************************************
  95. ****************************************************************************/
  96. /*************************************************************************
  97. * ACPI basic handles
  98. */
  99. static acpi_handle root_handle = NULL;
  100. #define IBM_HANDLE(object, parent, paths...) \
  101. static acpi_handle object##_handle; \
  102. static acpi_handle *object##_parent = &parent##_handle; \
  103. static char *object##_path; \
  104. static char *object##_paths[] = { paths }
  105. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  106. "\\_SB.PCI.ISA.EC", /* 570 */
  107. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  108. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  109. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  110. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  111. "\\_SB.PCI0.LPC.EC", /* all others */
  112. );
  113. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  114. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  115. /*************************************************************************
  116. * Misc ACPI handles
  117. */
  118. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  119. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  120. "\\CMS", /* R40, R40e */
  121. ); /* all others */
  122. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  123. "^HKEY", /* R30, R31 */
  124. "HKEY", /* all others */
  125. ); /* 570 */
  126. /*************************************************************************
  127. * ACPI helpers
  128. */
  129. static int acpi_evalf(acpi_handle handle,
  130. void *res, char *method, char *fmt, ...)
  131. {
  132. char *fmt0 = fmt;
  133. struct acpi_object_list params;
  134. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  135. struct acpi_buffer result, *resultp;
  136. union acpi_object out_obj;
  137. acpi_status status;
  138. va_list ap;
  139. char res_type;
  140. int success;
  141. int quiet;
  142. if (!*fmt) {
  143. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  144. return 0;
  145. }
  146. if (*fmt == 'q') {
  147. quiet = 1;
  148. fmt++;
  149. } else
  150. quiet = 0;
  151. res_type = *(fmt++);
  152. params.count = 0;
  153. params.pointer = &in_objs[0];
  154. va_start(ap, fmt);
  155. while (*fmt) {
  156. char c = *(fmt++);
  157. switch (c) {
  158. case 'd': /* int */
  159. in_objs[params.count].integer.value = va_arg(ap, int);
  160. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  161. break;
  162. /* add more types as needed */
  163. default:
  164. printk(IBM_ERR "acpi_evalf() called "
  165. "with invalid format character '%c'\n", c);
  166. return 0;
  167. }
  168. }
  169. va_end(ap);
  170. if (res_type != 'v') {
  171. result.length = sizeof(out_obj);
  172. result.pointer = &out_obj;
  173. resultp = &result;
  174. } else
  175. resultp = NULL;
  176. status = acpi_evaluate_object(handle, method, &params, resultp);
  177. switch (res_type) {
  178. case 'd': /* int */
  179. if (res)
  180. *(int *)res = out_obj.integer.value;
  181. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  182. break;
  183. case 'v': /* void */
  184. success = status == AE_OK;
  185. break;
  186. /* add more types as needed */
  187. default:
  188. printk(IBM_ERR "acpi_evalf() called "
  189. "with invalid format character '%c'\n", res_type);
  190. return 0;
  191. }
  192. if (!success && !quiet)
  193. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  194. method, fmt0, status);
  195. return success;
  196. }
  197. static void __unused acpi_print_int(acpi_handle handle, char *method)
  198. {
  199. int i;
  200. if (acpi_evalf(handle, &i, method, "d"))
  201. printk(IBM_INFO "%s = 0x%x\n", method, i);
  202. else
  203. printk(IBM_ERR "error calling %s\n", method);
  204. }
  205. static int acpi_ec_read(int i, u8 * p)
  206. {
  207. int v;
  208. if (ecrd_handle) {
  209. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  210. return 0;
  211. *p = v;
  212. } else {
  213. if (ec_read(i, p) < 0)
  214. return 0;
  215. }
  216. return 1;
  217. }
  218. static int acpi_ec_write(int i, u8 v)
  219. {
  220. if (ecwr_handle) {
  221. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  222. return 0;
  223. } else {
  224. if (ec_write(i, v) < 0)
  225. return 0;
  226. }
  227. return 1;
  228. }
  229. static int _sta(acpi_handle handle)
  230. {
  231. int status;
  232. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  233. status = 0;
  234. return status;
  235. }
  236. /*************************************************************************
  237. * ACPI device model
  238. */
  239. static void ibm_handle_init(char *name,
  240. acpi_handle *handle, acpi_handle parent,
  241. char **paths, int num_paths, char **path)
  242. {
  243. int i;
  244. acpi_status status;
  245. for (i = 0; i < num_paths; i++) {
  246. status = acpi_get_handle(parent, paths[i], handle);
  247. if (ACPI_SUCCESS(status)) {
  248. *path = paths[i];
  249. return;
  250. }
  251. }
  252. *handle = NULL;
  253. }
  254. static void dispatch_notify(acpi_handle handle, u32 event, void *data)
  255. {
  256. struct ibm_struct *ibm = data;
  257. if (!ibm || !ibm->notify)
  258. return;
  259. ibm->notify(ibm, event);
  260. }
  261. static int __init setup_notify(struct ibm_struct *ibm)
  262. {
  263. acpi_status status;
  264. int ret;
  265. if (!*ibm->handle)
  266. return 0;
  267. dbg_printk(TPACPI_DBG_INIT,
  268. "setting up ACPI notify for %s\n", ibm->name);
  269. ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
  270. if (ret < 0) {
  271. printk(IBM_ERR "%s device not present\n", ibm->name);
  272. return -ENODEV;
  273. }
  274. acpi_driver_data(ibm->device) = ibm;
  275. sprintf(acpi_device_class(ibm->device), "%s/%s",
  276. IBM_ACPI_EVENT_PREFIX,
  277. ibm->name);
  278. status = acpi_install_notify_handler(*ibm->handle, ibm->type,
  279. dispatch_notify, ibm);
  280. if (ACPI_FAILURE(status)) {
  281. if (status == AE_ALREADY_EXISTS) {
  282. printk(IBM_NOTICE "another device driver is already handling %s events\n",
  283. ibm->name);
  284. } else {
  285. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  286. ibm->name, status);
  287. }
  288. return -ENODEV;
  289. }
  290. ibm->notify_installed = 1;
  291. return 0;
  292. }
  293. static int __init ibm_device_add(struct acpi_device *device)
  294. {
  295. return 0;
  296. }
  297. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  298. {
  299. int ret;
  300. dbg_printk(TPACPI_DBG_INIT,
  301. "registering %s as an ACPI driver\n", ibm->name);
  302. ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  303. if (!ibm->driver) {
  304. printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
  305. return -ENOMEM;
  306. }
  307. sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
  308. ibm->driver->ids = ibm->hid;
  309. ibm->driver->ops.add = &ibm_device_add;
  310. ret = acpi_bus_register_driver(ibm->driver);
  311. if (ret < 0) {
  312. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  313. ibm->hid, ret);
  314. kfree(ibm->driver);
  315. ibm->driver = NULL;
  316. } else if (!ret)
  317. ibm->driver_registered = 1;
  318. return ret;
  319. }
  320. /****************************************************************************
  321. ****************************************************************************
  322. *
  323. * Procfs Helpers
  324. *
  325. ****************************************************************************
  326. ****************************************************************************/
  327. static int dispatch_read(char *page, char **start, off_t off, int count,
  328. int *eof, void *data)
  329. {
  330. struct ibm_struct *ibm = data;
  331. int len;
  332. if (!ibm || !ibm->read)
  333. return -EINVAL;
  334. len = ibm->read(page);
  335. if (len < 0)
  336. return len;
  337. if (len <= off + count)
  338. *eof = 1;
  339. *start = page + off;
  340. len -= off;
  341. if (len > count)
  342. len = count;
  343. if (len < 0)
  344. len = 0;
  345. return len;
  346. }
  347. static int dispatch_write(struct file *file, const char __user * userbuf,
  348. unsigned long count, void *data)
  349. {
  350. struct ibm_struct *ibm = data;
  351. char *kernbuf;
  352. int ret;
  353. if (!ibm || !ibm->write)
  354. return -EINVAL;
  355. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  356. if (!kernbuf)
  357. return -ENOMEM;
  358. if (copy_from_user(kernbuf, userbuf, count)) {
  359. kfree(kernbuf);
  360. return -EFAULT;
  361. }
  362. kernbuf[count] = 0;
  363. strcat(kernbuf, ",");
  364. ret = ibm->write(kernbuf);
  365. if (ret == 0)
  366. ret = count;
  367. kfree(kernbuf);
  368. return ret;
  369. }
  370. static char *next_cmd(char **cmds)
  371. {
  372. char *start = *cmds;
  373. char *end;
  374. while ((end = strchr(start, ',')) && end == start)
  375. start = end + 1;
  376. if (!end)
  377. return NULL;
  378. *end = 0;
  379. *cmds = end + 1;
  380. return start;
  381. }
  382. /****************************************************************************
  383. ****************************************************************************
  384. *
  385. * Subdrivers
  386. *
  387. ****************************************************************************
  388. ****************************************************************************/
  389. /*************************************************************************
  390. * thinkpad-acpi init subdriver
  391. */
  392. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  393. {
  394. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  395. printk(IBM_INFO "%s\n", IBM_URL);
  396. if (ibm_thinkpad_ec_found)
  397. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  398. ibm_thinkpad_ec_found);
  399. return 0;
  400. }
  401. static int thinkpad_acpi_driver_read(char *p)
  402. {
  403. int len = 0;
  404. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  405. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  406. return len;
  407. }
  408. static struct ibm_struct thinkpad_acpi_driver_data = {
  409. .name = "driver",
  410. .read = thinkpad_acpi_driver_read,
  411. };
  412. /*************************************************************************
  413. * Hotkey subdriver
  414. */
  415. static int hotkey_supported;
  416. static int hotkey_mask_supported;
  417. static int hotkey_orig_status;
  418. static int hotkey_orig_mask;
  419. static int __init hotkey_init(struct ibm_init_struct *iibm)
  420. {
  421. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  422. IBM_HANDLE_INIT(hkey);
  423. /* hotkey not supported on 570 */
  424. hotkey_supported = hkey_handle != NULL;
  425. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  426. str_supported(hotkey_supported));
  427. if (hotkey_supported) {
  428. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  429. A30, R30, R31, T20-22, X20-21, X22-24 */
  430. hotkey_mask_supported =
  431. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  432. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  433. str_supported(hotkey_mask_supported));
  434. if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
  435. return -ENODEV;
  436. }
  437. return (hotkey_supported)? 0 : 1;
  438. }
  439. static void hotkey_exit(void)
  440. {
  441. if (hotkey_supported) {
  442. dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
  443. hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  444. }
  445. }
  446. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  447. {
  448. int hkey;
  449. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  450. acpi_bus_generate_event(ibm->device, event, hkey);
  451. else {
  452. printk(IBM_ERR "unknown hotkey event %d\n", event);
  453. acpi_bus_generate_event(ibm->device, event, 0);
  454. }
  455. }
  456. static int hotkey_get(int *status, int *mask)
  457. {
  458. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  459. return 0;
  460. if (hotkey_mask_supported)
  461. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  462. return 0;
  463. return 1;
  464. }
  465. static int hotkey_set(int status, int mask)
  466. {
  467. int i;
  468. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  469. return 0;
  470. if (hotkey_mask_supported)
  471. for (i = 0; i < 32; i++) {
  472. int bit = ((1 << i) & mask) != 0;
  473. if (!acpi_evalf(hkey_handle,
  474. NULL, "MHKM", "vdd", i + 1, bit))
  475. return 0;
  476. }
  477. return 1;
  478. }
  479. static int hotkey_read(char *p)
  480. {
  481. int status, mask;
  482. int len = 0;
  483. if (!hotkey_supported) {
  484. len += sprintf(p + len, "status:\t\tnot supported\n");
  485. return len;
  486. }
  487. if (!hotkey_get(&status, &mask))
  488. return -EIO;
  489. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  490. if (hotkey_mask_supported) {
  491. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  492. len += sprintf(p + len,
  493. "commands:\tenable, disable, reset, <mask>\n");
  494. } else {
  495. len += sprintf(p + len, "mask:\t\tnot supported\n");
  496. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  497. }
  498. return len;
  499. }
  500. static int hotkey_write(char *buf)
  501. {
  502. int status, mask;
  503. char *cmd;
  504. int do_cmd = 0;
  505. if (!hotkey_supported)
  506. return -ENODEV;
  507. if (!hotkey_get(&status, &mask))
  508. return -EIO;
  509. while ((cmd = next_cmd(&buf))) {
  510. if (strlencmp(cmd, "enable") == 0) {
  511. status = 1;
  512. } else if (strlencmp(cmd, "disable") == 0) {
  513. status = 0;
  514. } else if (strlencmp(cmd, "reset") == 0) {
  515. status = hotkey_orig_status;
  516. mask = hotkey_orig_mask;
  517. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  518. /* mask set */
  519. } else if (sscanf(cmd, "%x", &mask) == 1) {
  520. /* mask set */
  521. } else
  522. return -EINVAL;
  523. do_cmd = 1;
  524. }
  525. if (do_cmd && !hotkey_set(status, mask))
  526. return -EIO;
  527. return 0;
  528. }
  529. static struct ibm_struct hotkey_driver_data = {
  530. .name = "hotkey",
  531. .hid = IBM_HKEY_HID,
  532. .read = hotkey_read,
  533. .write = hotkey_write,
  534. .exit = hotkey_exit,
  535. .notify = hotkey_notify,
  536. .handle = &hkey_handle,
  537. .type = ACPI_DEVICE_NOTIFY,
  538. };
  539. /*************************************************************************
  540. * Bluetooth subdriver
  541. */
  542. static int bluetooth_supported;
  543. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  544. {
  545. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  546. IBM_HANDLE_INIT(hkey);
  547. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  548. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  549. bluetooth_supported = hkey_handle &&
  550. acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
  551. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s\n",
  552. str_supported(bluetooth_supported));
  553. return (bluetooth_supported)? 0 : 1;
  554. }
  555. static int bluetooth_status(void)
  556. {
  557. int status;
  558. if (!bluetooth_supported ||
  559. !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  560. status = 0;
  561. return status;
  562. }
  563. static int bluetooth_read(char *p)
  564. {
  565. int len = 0;
  566. int status = bluetooth_status();
  567. if (!bluetooth_supported)
  568. len += sprintf(p + len, "status:\t\tnot supported\n");
  569. else if (!(status & 1))
  570. len += sprintf(p + len, "status:\t\tnot installed\n");
  571. else {
  572. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  573. len += sprintf(p + len, "commands:\tenable, disable\n");
  574. }
  575. return len;
  576. }
  577. static int bluetooth_write(char *buf)
  578. {
  579. int status = bluetooth_status();
  580. char *cmd;
  581. int do_cmd = 0;
  582. if (!bluetooth_supported)
  583. return -ENODEV;
  584. while ((cmd = next_cmd(&buf))) {
  585. if (strlencmp(cmd, "enable") == 0) {
  586. status |= 2;
  587. } else if (strlencmp(cmd, "disable") == 0) {
  588. status &= ~2;
  589. } else
  590. return -EINVAL;
  591. do_cmd = 1;
  592. }
  593. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  594. return -EIO;
  595. return 0;
  596. }
  597. static struct ibm_struct bluetooth_driver_data = {
  598. .name = "bluetooth",
  599. .read = bluetooth_read,
  600. .write = bluetooth_write,
  601. };
  602. /*************************************************************************
  603. * Wan subdriver
  604. */
  605. static int wan_supported;
  606. static int __init wan_init(struct ibm_init_struct *iibm)
  607. {
  608. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  609. IBM_HANDLE_INIT(hkey);
  610. wan_supported = hkey_handle &&
  611. acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
  612. vdbg_printk(TPACPI_DBG_INIT, "wan is %s\n",
  613. str_supported(wan_supported));
  614. return (wan_supported)? 0 : 1;
  615. }
  616. static int wan_status(void)
  617. {
  618. int status;
  619. if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  620. status = 0;
  621. return status;
  622. }
  623. static int wan_read(char *p)
  624. {
  625. int len = 0;
  626. int status = wan_status();
  627. if (!wan_supported)
  628. len += sprintf(p + len, "status:\t\tnot supported\n");
  629. else if (!(status & 1))
  630. len += sprintf(p + len, "status:\t\tnot installed\n");
  631. else {
  632. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
  633. len += sprintf(p + len, "commands:\tenable, disable\n");
  634. }
  635. return len;
  636. }
  637. static int wan_write(char *buf)
  638. {
  639. int status = wan_status();
  640. char *cmd;
  641. int do_cmd = 0;
  642. if (!wan_supported)
  643. return -ENODEV;
  644. while ((cmd = next_cmd(&buf))) {
  645. if (strlencmp(cmd, "enable") == 0) {
  646. status |= 2;
  647. } else if (strlencmp(cmd, "disable") == 0) {
  648. status &= ~2;
  649. } else
  650. return -EINVAL;
  651. do_cmd = 1;
  652. }
  653. if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  654. return -EIO;
  655. return 0;
  656. }
  657. static struct ibm_struct wan_driver_data = {
  658. .name = "wan",
  659. .read = wan_read,
  660. .write = wan_write,
  661. .experimental = 1,
  662. };
  663. /*************************************************************************
  664. * Video subdriver
  665. */
  666. static enum video_access_mode video_supported;
  667. static int video_orig_autosw;
  668. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  669. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  670. "\\_SB.PCI0.VID0", /* 770e */
  671. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  672. "\\_SB.PCI0.AGP.VID", /* all others */
  673. ); /* R30, R31 */
  674. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  675. static int __init video_init(struct ibm_init_struct *iibm)
  676. {
  677. int ivga;
  678. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  679. IBM_HANDLE_INIT(vid);
  680. IBM_HANDLE_INIT(vid2);
  681. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  682. /* G41, assume IVGA doesn't change */
  683. vid_handle = vid2_handle;
  684. if (!vid_handle)
  685. /* video switching not supported on R30, R31 */
  686. video_supported = TPACPI_VIDEO_NONE;
  687. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  688. /* 570 */
  689. video_supported = TPACPI_VIDEO_570;
  690. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  691. /* 600e/x, 770e, 770x */
  692. video_supported = TPACPI_VIDEO_770;
  693. else
  694. /* all others */
  695. video_supported = TPACPI_VIDEO_NEW;
  696. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  697. str_supported(video_supported != TPACPI_VIDEO_NONE),
  698. video_supported);
  699. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  700. }
  701. static void video_exit(void)
  702. {
  703. dbg_printk(TPACPI_DBG_EXIT, "restoring original video autoswitch mode\n");
  704. acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
  705. }
  706. static int video_status(void)
  707. {
  708. int status = 0;
  709. int i;
  710. if (video_supported == TPACPI_VIDEO_570) {
  711. if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
  712. status = i & 3;
  713. } else if (video_supported == TPACPI_VIDEO_770) {
  714. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  715. status |= 0x01 * i;
  716. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  717. status |= 0x02 * i;
  718. } else if (video_supported == TPACPI_VIDEO_NEW) {
  719. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
  720. if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
  721. status |= 0x02 * i;
  722. acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
  723. if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
  724. status |= 0x01 * i;
  725. if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
  726. status |= 0x08 * i;
  727. }
  728. return status;
  729. }
  730. static int video_autosw(void)
  731. {
  732. int autosw = 0;
  733. if (video_supported == TPACPI_VIDEO_570)
  734. acpi_evalf(vid_handle, &autosw, "SWIT", "d");
  735. else if (video_supported == TPACPI_VIDEO_770 ||
  736. video_supported == TPACPI_VIDEO_NEW)
  737. acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
  738. return autosw & 1;
  739. }
  740. static int video_switch(void)
  741. {
  742. int autosw = video_autosw();
  743. int ret;
  744. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  745. return -EIO;
  746. ret = video_supported == TPACPI_VIDEO_570 ?
  747. acpi_evalf(ec_handle, NULL, "_Q16", "v") :
  748. acpi_evalf(vid_handle, NULL, "VSWT", "v");
  749. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  750. return ret;
  751. }
  752. static int video_expand(void)
  753. {
  754. if (video_supported == TPACPI_VIDEO_570)
  755. return acpi_evalf(ec_handle, NULL, "_Q17", "v");
  756. else if (video_supported == TPACPI_VIDEO_770)
  757. return acpi_evalf(vid_handle, NULL, "VEXP", "v");
  758. else
  759. return acpi_evalf(NULL, NULL, "\\VEXP", "v");
  760. }
  761. static int video_switch2(int status)
  762. {
  763. int ret;
  764. if (video_supported == TPACPI_VIDEO_570) {
  765. ret = acpi_evalf(NULL, NULL,
  766. "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
  767. } else if (video_supported == TPACPI_VIDEO_770) {
  768. int autosw = video_autosw();
  769. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  770. return -EIO;
  771. ret = acpi_evalf(vid_handle, NULL,
  772. "ASWT", "vdd", status * 0x100, 0);
  773. acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
  774. } else {
  775. ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  776. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  777. }
  778. return ret;
  779. }
  780. static int video_read(char *p)
  781. {
  782. int status = video_status();
  783. int autosw = video_autosw();
  784. int len = 0;
  785. if (!video_supported) {
  786. len += sprintf(p + len, "status:\t\tnot supported\n");
  787. return len;
  788. }
  789. len += sprintf(p + len, "status:\t\tsupported\n");
  790. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  791. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  792. if (video_supported == TPACPI_VIDEO_NEW)
  793. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  794. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  795. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  796. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  797. if (video_supported == TPACPI_VIDEO_NEW)
  798. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  799. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  800. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  801. return len;
  802. }
  803. static int video_write(char *buf)
  804. {
  805. char *cmd;
  806. int enable, disable, status;
  807. if (!video_supported)
  808. return -ENODEV;
  809. enable = disable = 0;
  810. while ((cmd = next_cmd(&buf))) {
  811. if (strlencmp(cmd, "lcd_enable") == 0) {
  812. enable |= 0x01;
  813. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  814. disable |= 0x01;
  815. } else if (strlencmp(cmd, "crt_enable") == 0) {
  816. enable |= 0x02;
  817. } else if (strlencmp(cmd, "crt_disable") == 0) {
  818. disable |= 0x02;
  819. } else if (video_supported == TPACPI_VIDEO_NEW &&
  820. strlencmp(cmd, "dvi_enable") == 0) {
  821. enable |= 0x08;
  822. } else if (video_supported == TPACPI_VIDEO_NEW &&
  823. strlencmp(cmd, "dvi_disable") == 0) {
  824. disable |= 0x08;
  825. } else if (strlencmp(cmd, "auto_enable") == 0) {
  826. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
  827. return -EIO;
  828. } else if (strlencmp(cmd, "auto_disable") == 0) {
  829. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
  830. return -EIO;
  831. } else if (strlencmp(cmd, "video_switch") == 0) {
  832. if (!video_switch())
  833. return -EIO;
  834. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  835. if (!video_expand())
  836. return -EIO;
  837. } else
  838. return -EINVAL;
  839. }
  840. if (enable || disable) {
  841. status = (video_status() & 0x0f & ~disable) | enable;
  842. if (!video_switch2(status))
  843. return -EIO;
  844. }
  845. return 0;
  846. }
  847. static struct ibm_struct video_driver_data = {
  848. .name = "video",
  849. .read = video_read,
  850. .write = video_write,
  851. .exit = video_exit,
  852. };
  853. /*************************************************************************
  854. * Light (thinklight) subdriver
  855. */
  856. static int light_supported;
  857. static int light_status_supported;
  858. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  859. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  860. static int __init light_init(struct ibm_init_struct *iibm)
  861. {
  862. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  863. IBM_HANDLE_INIT(ledb);
  864. IBM_HANDLE_INIT(lght);
  865. IBM_HANDLE_INIT(cmos);
  866. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  867. light_supported = (cmos_handle || lght_handle) && !ledb_handle;
  868. if (light_supported)
  869. /* light status not supported on
  870. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  871. light_status_supported = acpi_evalf(ec_handle, NULL,
  872. "KBLT", "qv");
  873. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  874. str_supported(light_supported));
  875. return (light_supported)? 0 : 1;
  876. }
  877. static int light_read(char *p)
  878. {
  879. int len = 0;
  880. int status = 0;
  881. if (!light_supported) {
  882. len += sprintf(p + len, "status:\t\tnot supported\n");
  883. } else if (!light_status_supported) {
  884. len += sprintf(p + len, "status:\t\tunknown\n");
  885. len += sprintf(p + len, "commands:\ton, off\n");
  886. } else {
  887. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  888. return -EIO;
  889. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  890. len += sprintf(p + len, "commands:\ton, off\n");
  891. }
  892. return len;
  893. }
  894. static int light_write(char *buf)
  895. {
  896. int cmos_cmd, lght_cmd;
  897. char *cmd;
  898. int success;
  899. if (!light_supported)
  900. return -ENODEV;
  901. while ((cmd = next_cmd(&buf))) {
  902. if (strlencmp(cmd, "on") == 0) {
  903. cmos_cmd = 0x0c;
  904. lght_cmd = 1;
  905. } else if (strlencmp(cmd, "off") == 0) {
  906. cmos_cmd = 0x0d;
  907. lght_cmd = 0;
  908. } else
  909. return -EINVAL;
  910. success = cmos_handle ?
  911. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  912. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  913. if (!success)
  914. return -EIO;
  915. }
  916. return 0;
  917. }
  918. static struct ibm_struct light_driver_data = {
  919. .name = "light",
  920. .read = light_read,
  921. .write = light_write,
  922. };
  923. /*************************************************************************
  924. * Dock subdriver
  925. */
  926. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  927. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  928. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  929. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  930. "\\_SB.PCI.ISA.SLCE", /* 570 */
  931. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  932. /* don't list other alternatives as we install a notify handler on the 570 */
  933. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  934. #define dock_docked() (_sta(dock_handle) & 1)
  935. static int __init dock_init(struct ibm_init_struct *iibm)
  936. {
  937. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  938. IBM_HANDLE_INIT(dock);
  939. IBM_HANDLE_INIT(pci);
  940. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  941. str_supported(dock_handle != NULL));
  942. return (dock_handle)? 0 : 1;
  943. }
  944. static void dock_notify(struct ibm_struct *ibm, u32 event)
  945. {
  946. int docked = dock_docked();
  947. int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
  948. if (event == 1 && !pci) /* 570 */
  949. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  950. else if (event == 1 && pci) /* 570 */
  951. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  952. else if (event == 3 && docked)
  953. acpi_bus_generate_event(ibm->device, event, 1); /* button */
  954. else if (event == 3 && !docked)
  955. acpi_bus_generate_event(ibm->device, event, 2); /* undock */
  956. else if (event == 0 && docked)
  957. acpi_bus_generate_event(ibm->device, event, 3); /* dock */
  958. else {
  959. printk(IBM_ERR "unknown dock event %d, status %d\n",
  960. event, _sta(dock_handle));
  961. acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
  962. }
  963. }
  964. static int dock_read(char *p)
  965. {
  966. int len = 0;
  967. int docked = dock_docked();
  968. if (!dock_handle)
  969. len += sprintf(p + len, "status:\t\tnot supported\n");
  970. else if (!docked)
  971. len += sprintf(p + len, "status:\t\tundocked\n");
  972. else {
  973. len += sprintf(p + len, "status:\t\tdocked\n");
  974. len += sprintf(p + len, "commands:\tdock, undock\n");
  975. }
  976. return len;
  977. }
  978. static int dock_write(char *buf)
  979. {
  980. char *cmd;
  981. if (!dock_docked())
  982. return -ENODEV;
  983. while ((cmd = next_cmd(&buf))) {
  984. if (strlencmp(cmd, "undock") == 0) {
  985. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  986. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  987. return -EIO;
  988. } else if (strlencmp(cmd, "dock") == 0) {
  989. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  990. return -EIO;
  991. } else
  992. return -EINVAL;
  993. }
  994. return 0;
  995. }
  996. static struct ibm_struct dock_driver_data[2] = {
  997. {
  998. .name = "dock",
  999. .read = dock_read,
  1000. .write = dock_write,
  1001. .notify = dock_notify,
  1002. .handle = &dock_handle,
  1003. .type = ACPI_SYSTEM_NOTIFY,
  1004. },
  1005. {
  1006. .name = "dock",
  1007. .hid = IBM_PCI_HID,
  1008. .notify = dock_notify,
  1009. .handle = &pci_handle,
  1010. .type = ACPI_SYSTEM_NOTIFY,
  1011. },
  1012. };
  1013. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  1014. /*************************************************************************
  1015. * Bay subdriver
  1016. */
  1017. #ifdef CONFIG_THINKPAD_ACPI_BAY
  1018. static int bay_status_supported;
  1019. static int bay_status2_supported;
  1020. static int bay_eject_supported;
  1021. static int bay_eject2_supported;
  1022. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  1023. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  1024. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  1025. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  1026. ); /* A21e, R30, R31 */
  1027. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  1028. "_EJ0", /* all others */
  1029. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  1030. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  1031. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  1032. ); /* all others */
  1033. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  1034. "_EJ0", /* 770x */
  1035. ); /* all others */
  1036. static int __init bay_init(struct ibm_init_struct *iibm)
  1037. {
  1038. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  1039. IBM_HANDLE_INIT(bay);
  1040. if (bay_handle)
  1041. IBM_HANDLE_INIT(bay_ej);
  1042. IBM_HANDLE_INIT(bay2);
  1043. if (bay2_handle)
  1044. IBM_HANDLE_INIT(bay2_ej);
  1045. bay_status_supported = bay_handle &&
  1046. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  1047. bay_status2_supported = bay2_handle &&
  1048. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  1049. bay_eject_supported = bay_handle && bay_ej_handle &&
  1050. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  1051. bay_eject2_supported = bay2_handle && bay2_ej_handle &&
  1052. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  1053. vdbg_printk(TPACPI_DBG_INIT,
  1054. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  1055. str_supported(bay_status_supported),
  1056. str_supported(bay_eject_supported),
  1057. str_supported(bay_status2_supported),
  1058. str_supported(bay_eject2_supported));
  1059. return (bay_status_supported || bay_eject_supported ||
  1060. bay_status2_supported || bay_eject2_supported)? 0 : 1;
  1061. }
  1062. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1063. {
  1064. acpi_bus_generate_event(ibm->device, event, 0);
  1065. }
  1066. #define bay_occupied(b) (_sta(b##_handle) & 1)
  1067. static int bay_read(char *p)
  1068. {
  1069. int len = 0;
  1070. int occupied = bay_occupied(bay);
  1071. int occupied2 = bay_occupied(bay2);
  1072. int eject, eject2;
  1073. len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
  1074. (occupied ? "occupied" : "unoccupied") :
  1075. "not supported");
  1076. if (bay_status2_supported)
  1077. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  1078. "occupied" : "unoccupied");
  1079. eject = bay_eject_supported && occupied;
  1080. eject2 = bay_eject2_supported && occupied2;
  1081. if (eject && eject2)
  1082. len += sprintf(p + len, "commands:\teject, eject2\n");
  1083. else if (eject)
  1084. len += sprintf(p + len, "commands:\teject\n");
  1085. else if (eject2)
  1086. len += sprintf(p + len, "commands:\teject2\n");
  1087. return len;
  1088. }
  1089. static int bay_write(char *buf)
  1090. {
  1091. char *cmd;
  1092. if (!bay_eject_supported && !bay_eject2_supported)
  1093. return -ENODEV;
  1094. while ((cmd = next_cmd(&buf))) {
  1095. if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
  1096. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  1097. return -EIO;
  1098. } else if (bay_eject2_supported &&
  1099. strlencmp(cmd, "eject2") == 0) {
  1100. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  1101. return -EIO;
  1102. } else
  1103. return -EINVAL;
  1104. }
  1105. return 0;
  1106. }
  1107. static struct ibm_struct bay_driver_data = {
  1108. .name = "bay",
  1109. .read = bay_read,
  1110. .write = bay_write,
  1111. .notify = bay_notify,
  1112. .handle = &bay_handle,
  1113. .type = ACPI_SYSTEM_NOTIFY,
  1114. };
  1115. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  1116. /*************************************************************************
  1117. * CMOS subdriver
  1118. */
  1119. static int __init cmos_init(struct ibm_init_struct *iibm)
  1120. {
  1121. vdbg_printk(TPACPI_DBG_INIT,
  1122. "initializing cmos commands subdriver\n");
  1123. IBM_HANDLE_INIT(cmos);
  1124. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  1125. str_supported(cmos_handle != NULL));
  1126. return (cmos_handle)? 0 : 1;
  1127. }
  1128. static int cmos_eval(int cmos_cmd)
  1129. {
  1130. if (cmos_handle)
  1131. return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
  1132. else
  1133. return 1;
  1134. }
  1135. static int cmos_read(char *p)
  1136. {
  1137. int len = 0;
  1138. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1139. R30, R31, T20-22, X20-21 */
  1140. if (!cmos_handle)
  1141. len += sprintf(p + len, "status:\t\tnot supported\n");
  1142. else {
  1143. len += sprintf(p + len, "status:\t\tsupported\n");
  1144. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1145. }
  1146. return len;
  1147. }
  1148. static int cmos_write(char *buf)
  1149. {
  1150. char *cmd;
  1151. int cmos_cmd;
  1152. if (!cmos_handle)
  1153. return -EINVAL;
  1154. while ((cmd = next_cmd(&buf))) {
  1155. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1156. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1157. /* cmos_cmd set */
  1158. } else
  1159. return -EINVAL;
  1160. if (!cmos_eval(cmos_cmd))
  1161. return -EIO;
  1162. }
  1163. return 0;
  1164. }
  1165. static struct ibm_struct cmos_driver_data = {
  1166. .name = "cmos",
  1167. .read = cmos_read,
  1168. .write = cmos_write,
  1169. };
  1170. /*************************************************************************
  1171. * LED subdriver
  1172. */
  1173. static enum led_access_mode led_supported;
  1174. IBM_HANDLE(led, ec, "SLED", /* 570 */
  1175. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1176. "LED", /* all others */
  1177. ); /* R30, R31 */
  1178. static int __init led_init(struct ibm_init_struct *iibm)
  1179. {
  1180. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  1181. IBM_HANDLE_INIT(led);
  1182. if (!led_handle)
  1183. /* led not supported on R30, R31 */
  1184. led_supported = TPACPI_LED_NONE;
  1185. else if (strlencmp(led_path, "SLED") == 0)
  1186. /* 570 */
  1187. led_supported = TPACPI_LED_570;
  1188. else if (strlencmp(led_path, "SYSL") == 0)
  1189. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1190. led_supported = TPACPI_LED_OLD;
  1191. else
  1192. /* all others */
  1193. led_supported = TPACPI_LED_NEW;
  1194. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  1195. str_supported(led_supported), led_supported);
  1196. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  1197. }
  1198. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1199. static int led_read(char *p)
  1200. {
  1201. int len = 0;
  1202. if (!led_supported) {
  1203. len += sprintf(p + len, "status:\t\tnot supported\n");
  1204. return len;
  1205. }
  1206. len += sprintf(p + len, "status:\t\tsupported\n");
  1207. if (led_supported == TPACPI_LED_570) {
  1208. /* 570 */
  1209. int i, status;
  1210. for (i = 0; i < 8; i++) {
  1211. if (!acpi_evalf(ec_handle,
  1212. &status, "GLED", "dd", 1 << i))
  1213. return -EIO;
  1214. len += sprintf(p + len, "%d:\t\t%s\n",
  1215. i, led_status(status));
  1216. }
  1217. }
  1218. len += sprintf(p + len, "commands:\t"
  1219. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1220. return len;
  1221. }
  1222. /* off, on, blink */
  1223. static const int led_sled_arg1[] = { 0, 1, 3 };
  1224. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1225. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1226. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1227. static int led_write(char *buf)
  1228. {
  1229. char *cmd;
  1230. int led, ind, ret;
  1231. if (!led_supported)
  1232. return -ENODEV;
  1233. while ((cmd = next_cmd(&buf))) {
  1234. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1235. return -EINVAL;
  1236. if (strstr(cmd, "off")) {
  1237. ind = 0;
  1238. } else if (strstr(cmd, "on")) {
  1239. ind = 1;
  1240. } else if (strstr(cmd, "blink")) {
  1241. ind = 2;
  1242. } else
  1243. return -EINVAL;
  1244. if (led_supported == TPACPI_LED_570) {
  1245. /* 570 */
  1246. led = 1 << led;
  1247. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1248. led, led_sled_arg1[ind]))
  1249. return -EIO;
  1250. } else if (led_supported == TPACPI_LED_OLD) {
  1251. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1252. led = 1 << led;
  1253. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  1254. if (ret >= 0)
  1255. ret =
  1256. ec_write(TPACPI_LED_EC_HLBL,
  1257. led * led_exp_hlbl[ind]);
  1258. if (ret >= 0)
  1259. ret =
  1260. ec_write(TPACPI_LED_EC_HLCL,
  1261. led * led_exp_hlcl[ind]);
  1262. if (ret < 0)
  1263. return ret;
  1264. } else {
  1265. /* all others */
  1266. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1267. led, led_led_arg1[ind]))
  1268. return -EIO;
  1269. }
  1270. }
  1271. return 0;
  1272. }
  1273. static struct ibm_struct led_driver_data = {
  1274. .name = "led",
  1275. .read = led_read,
  1276. .write = led_write,
  1277. };
  1278. /*************************************************************************
  1279. * Beep subdriver
  1280. */
  1281. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  1282. static int __init beep_init(struct ibm_init_struct *iibm)
  1283. {
  1284. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  1285. IBM_HANDLE_INIT(beep);
  1286. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  1287. str_supported(beep_handle != NULL));
  1288. return (beep_handle)? 0 : 1;
  1289. }
  1290. static int beep_read(char *p)
  1291. {
  1292. int len = 0;
  1293. if (!beep_handle)
  1294. len += sprintf(p + len, "status:\t\tnot supported\n");
  1295. else {
  1296. len += sprintf(p + len, "status:\t\tsupported\n");
  1297. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1298. }
  1299. return len;
  1300. }
  1301. static int beep_write(char *buf)
  1302. {
  1303. char *cmd;
  1304. int beep_cmd;
  1305. if (!beep_handle)
  1306. return -ENODEV;
  1307. while ((cmd = next_cmd(&buf))) {
  1308. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1309. beep_cmd >= 0 && beep_cmd <= 17) {
  1310. /* beep_cmd set */
  1311. } else
  1312. return -EINVAL;
  1313. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1314. return -EIO;
  1315. }
  1316. return 0;
  1317. }
  1318. static struct ibm_struct beep_driver_data = {
  1319. .name = "beep",
  1320. .read = beep_read,
  1321. .write = beep_write,
  1322. };
  1323. /*************************************************************************
  1324. * Thermal subdriver
  1325. */
  1326. static enum thermal_access_mode thermal_read_mode;
  1327. static int __init thermal_init(struct ibm_init_struct *iibm)
  1328. {
  1329. u8 t, ta1, ta2;
  1330. int i;
  1331. int acpi_tmp7;
  1332. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  1333. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1334. if (ibm_thinkpad_ec_found && experimental) {
  1335. /*
  1336. * Direct EC access mode: sensors at registers
  1337. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1338. * non-implemented, thermal sensors return 0x80 when
  1339. * not available
  1340. */
  1341. ta1 = ta2 = 0;
  1342. for (i = 0; i < 8; i++) {
  1343. if (likely(acpi_ec_read(0x78 + i, &t))) {
  1344. ta1 |= t;
  1345. } else {
  1346. ta1 = 0;
  1347. break;
  1348. }
  1349. if (likely(acpi_ec_read(0xC0 + i, &t))) {
  1350. ta2 |= t;
  1351. } else {
  1352. ta1 = 0;
  1353. break;
  1354. }
  1355. }
  1356. if (ta1 == 0) {
  1357. /* This is sheer paranoia, but we handle it anyway */
  1358. if (acpi_tmp7) {
  1359. printk(IBM_ERR
  1360. "ThinkPad ACPI EC access misbehaving, "
  1361. "falling back to ACPI TMPx access mode\n");
  1362. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1363. } else {
  1364. printk(IBM_ERR
  1365. "ThinkPad ACPI EC access misbehaving, "
  1366. "disabling thermal sensors access\n");
  1367. thermal_read_mode = TPACPI_THERMAL_NONE;
  1368. }
  1369. } else {
  1370. thermal_read_mode =
  1371. (ta2 != 0) ?
  1372. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  1373. }
  1374. } else if (acpi_tmp7) {
  1375. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1376. /* 600e/x, 770e, 770x */
  1377. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  1378. } else {
  1379. /* Standard ACPI TMPx access, max 8 sensors */
  1380. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1381. }
  1382. } else {
  1383. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1384. thermal_read_mode = TPACPI_THERMAL_NONE;
  1385. }
  1386. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  1387. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  1388. thermal_read_mode);
  1389. return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
  1390. }
  1391. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1392. {
  1393. int i, t;
  1394. s8 tmp;
  1395. char tmpi[] = "TMPi";
  1396. if (!s)
  1397. return -EINVAL;
  1398. switch (thermal_read_mode) {
  1399. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  1400. case TPACPI_THERMAL_TPEC_16:
  1401. for (i = 0; i < 8; i++) {
  1402. if (!acpi_ec_read(0xC0 + i, &tmp))
  1403. return -EIO;
  1404. s->temp[i + 8] = tmp * 1000;
  1405. }
  1406. /* fallthrough */
  1407. #endif
  1408. case TPACPI_THERMAL_TPEC_8:
  1409. for (i = 0; i < 8; i++) {
  1410. if (!acpi_ec_read(0x78 + i, &tmp))
  1411. return -EIO;
  1412. s->temp[i] = tmp * 1000;
  1413. }
  1414. return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
  1415. case TPACPI_THERMAL_ACPI_UPDT:
  1416. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1417. return -EIO;
  1418. for (i = 0; i < 8; i++) {
  1419. tmpi[3] = '0' + i;
  1420. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1421. return -EIO;
  1422. s->temp[i] = (t - 2732) * 100;
  1423. }
  1424. return 8;
  1425. case TPACPI_THERMAL_ACPI_TMP07:
  1426. for (i = 0; i < 8; i++) {
  1427. tmpi[3] = '0' + i;
  1428. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1429. return -EIO;
  1430. s->temp[i] = t * 1000;
  1431. }
  1432. return 8;
  1433. case TPACPI_THERMAL_NONE:
  1434. default:
  1435. return 0;
  1436. }
  1437. }
  1438. static int thermal_read(char *p)
  1439. {
  1440. int len = 0;
  1441. int n, i;
  1442. struct ibm_thermal_sensors_struct t;
  1443. n = thermal_get_sensors(&t);
  1444. if (unlikely(n < 0))
  1445. return n;
  1446. len += sprintf(p + len, "temperatures:\t");
  1447. if (n > 0) {
  1448. for (i = 0; i < (n - 1); i++)
  1449. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1450. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1451. } else
  1452. len += sprintf(p + len, "not supported\n");
  1453. return len;
  1454. }
  1455. static struct ibm_struct thermal_driver_data = {
  1456. .name = "thermal",
  1457. .read = thermal_read,
  1458. };
  1459. /*************************************************************************
  1460. * EC Dump subdriver
  1461. */
  1462. static u8 ecdump_regs[256];
  1463. static int ecdump_read(char *p)
  1464. {
  1465. int len = 0;
  1466. int i, j;
  1467. u8 v;
  1468. len += sprintf(p + len, "EC "
  1469. " +00 +01 +02 +03 +04 +05 +06 +07"
  1470. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1471. for (i = 0; i < 256; i += 16) {
  1472. len += sprintf(p + len, "EC 0x%02x:", i);
  1473. for (j = 0; j < 16; j++) {
  1474. if (!acpi_ec_read(i + j, &v))
  1475. break;
  1476. if (v != ecdump_regs[i + j])
  1477. len += sprintf(p + len, " *%02x", v);
  1478. else
  1479. len += sprintf(p + len, " %02x", v);
  1480. ecdump_regs[i + j] = v;
  1481. }
  1482. len += sprintf(p + len, "\n");
  1483. if (j != 16)
  1484. break;
  1485. }
  1486. /* These are way too dangerous to advertise openly... */
  1487. #if 0
  1488. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1489. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1490. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1491. " (<offset> is 00-ff, <value> is 0-255)\n");
  1492. #endif
  1493. return len;
  1494. }
  1495. static int ecdump_write(char *buf)
  1496. {
  1497. char *cmd;
  1498. int i, v;
  1499. while ((cmd = next_cmd(&buf))) {
  1500. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1501. /* i and v set */
  1502. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1503. /* i and v set */
  1504. } else
  1505. return -EINVAL;
  1506. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1507. if (!acpi_ec_write(i, v))
  1508. return -EIO;
  1509. } else
  1510. return -EINVAL;
  1511. }
  1512. return 0;
  1513. }
  1514. static struct ibm_struct ecdump_driver_data = {
  1515. .name = "ecdump",
  1516. .read = ecdump_read,
  1517. .write = ecdump_write,
  1518. .experimental = 1,
  1519. };
  1520. /*************************************************************************
  1521. * Backlight/brightness subdriver
  1522. */
  1523. static struct backlight_device *ibm_backlight_device = NULL;
  1524. static struct backlight_ops ibm_backlight_data = {
  1525. .get_brightness = brightness_get,
  1526. .update_status = brightness_update_status,
  1527. };
  1528. static int __init brightness_init(struct ibm_init_struct *iibm)
  1529. {
  1530. int b;
  1531. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  1532. b = brightness_get(NULL);
  1533. if (b < 0)
  1534. return b;
  1535. ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
  1536. &ibm_backlight_data);
  1537. if (IS_ERR(ibm_backlight_device)) {
  1538. printk(IBM_ERR "Could not register backlight device\n");
  1539. return PTR_ERR(ibm_backlight_device);
  1540. }
  1541. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  1542. ibm_backlight_device->props.max_brightness = 7;
  1543. ibm_backlight_device->props.brightness = b;
  1544. backlight_update_status(ibm_backlight_device);
  1545. return 0;
  1546. }
  1547. static void brightness_exit(void)
  1548. {
  1549. if (ibm_backlight_device) {
  1550. vdbg_printk(TPACPI_DBG_EXIT,
  1551. "calling backlight_device_unregister()\n");
  1552. backlight_device_unregister(ibm_backlight_device);
  1553. ibm_backlight_device = NULL;
  1554. }
  1555. }
  1556. static int brightness_update_status(struct backlight_device *bd)
  1557. {
  1558. return brightness_set(
  1559. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  1560. bd->props.power == FB_BLANK_UNBLANK) ?
  1561. bd->props.brightness : 0);
  1562. }
  1563. static int brightness_get(struct backlight_device *bd)
  1564. {
  1565. u8 level;
  1566. if (!acpi_ec_read(brightness_offset, &level))
  1567. return -EIO;
  1568. level &= 0x7;
  1569. return level;
  1570. }
  1571. static int brightness_set(int value)
  1572. {
  1573. int cmos_cmd, inc, i;
  1574. int current_value = brightness_get(NULL);
  1575. value &= 7;
  1576. cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
  1577. inc = value > current_value ? 1 : -1;
  1578. for (i = current_value; i != value; i += inc) {
  1579. if (!cmos_eval(cmos_cmd))
  1580. return -EIO;
  1581. if (!acpi_ec_write(brightness_offset, i + inc))
  1582. return -EIO;
  1583. }
  1584. return 0;
  1585. }
  1586. static int brightness_read(char *p)
  1587. {
  1588. int len = 0;
  1589. int level;
  1590. if ((level = brightness_get(NULL)) < 0) {
  1591. len += sprintf(p + len, "level:\t\tunreadable\n");
  1592. } else {
  1593. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  1594. len += sprintf(p + len, "commands:\tup, down\n");
  1595. len += sprintf(p + len, "commands:\tlevel <level>"
  1596. " (<level> is 0-7)\n");
  1597. }
  1598. return len;
  1599. }
  1600. static int brightness_write(char *buf)
  1601. {
  1602. int level;
  1603. int new_level;
  1604. char *cmd;
  1605. while ((cmd = next_cmd(&buf))) {
  1606. if ((level = brightness_get(NULL)) < 0)
  1607. return level;
  1608. level &= 7;
  1609. if (strlencmp(cmd, "up") == 0) {
  1610. new_level = level == 7 ? 7 : level + 1;
  1611. } else if (strlencmp(cmd, "down") == 0) {
  1612. new_level = level == 0 ? 0 : level - 1;
  1613. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1614. new_level >= 0 && new_level <= 7) {
  1615. /* new_level set */
  1616. } else
  1617. return -EINVAL;
  1618. brightness_set(new_level);
  1619. }
  1620. return 0;
  1621. }
  1622. static struct ibm_struct brightness_driver_data = {
  1623. .name = "brightness",
  1624. .read = brightness_read,
  1625. .write = brightness_write,
  1626. .exit = brightness_exit,
  1627. };
  1628. /*************************************************************************
  1629. * Volume subdriver
  1630. */
  1631. static int volume_read(char *p)
  1632. {
  1633. int len = 0;
  1634. u8 level;
  1635. if (!acpi_ec_read(volume_offset, &level)) {
  1636. len += sprintf(p + len, "level:\t\tunreadable\n");
  1637. } else {
  1638. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  1639. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  1640. len += sprintf(p + len, "commands:\tup, down, mute\n");
  1641. len += sprintf(p + len, "commands:\tlevel <level>"
  1642. " (<level> is 0-15)\n");
  1643. }
  1644. return len;
  1645. }
  1646. static int volume_write(char *buf)
  1647. {
  1648. int cmos_cmd, inc, i;
  1649. u8 level, mute;
  1650. int new_level, new_mute;
  1651. char *cmd;
  1652. while ((cmd = next_cmd(&buf))) {
  1653. if (!acpi_ec_read(volume_offset, &level))
  1654. return -EIO;
  1655. new_mute = mute = level & 0x40;
  1656. new_level = level = level & 0xf;
  1657. if (strlencmp(cmd, "up") == 0) {
  1658. if (mute)
  1659. new_mute = 0;
  1660. else
  1661. new_level = level == 15 ? 15 : level + 1;
  1662. } else if (strlencmp(cmd, "down") == 0) {
  1663. if (mute)
  1664. new_mute = 0;
  1665. else
  1666. new_level = level == 0 ? 0 : level - 1;
  1667. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1668. new_level >= 0 && new_level <= 15) {
  1669. /* new_level set */
  1670. } else if (strlencmp(cmd, "mute") == 0) {
  1671. new_mute = 0x40;
  1672. } else
  1673. return -EINVAL;
  1674. if (new_level != level) { /* mute doesn't change */
  1675. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  1676. inc = new_level > level ? 1 : -1;
  1677. if (mute && (!cmos_eval(cmos_cmd) ||
  1678. !acpi_ec_write(volume_offset, level)))
  1679. return -EIO;
  1680. for (i = level; i != new_level; i += inc)
  1681. if (!cmos_eval(cmos_cmd) ||
  1682. !acpi_ec_write(volume_offset, i + inc))
  1683. return -EIO;
  1684. if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
  1685. !acpi_ec_write(volume_offset,
  1686. new_level + mute)))
  1687. return -EIO;
  1688. }
  1689. if (new_mute != mute) { /* level doesn't change */
  1690. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  1691. if (!cmos_eval(cmos_cmd) ||
  1692. !acpi_ec_write(volume_offset, level + new_mute))
  1693. return -EIO;
  1694. }
  1695. }
  1696. return 0;
  1697. }
  1698. static struct ibm_struct volume_driver_data = {
  1699. .name = "volume",
  1700. .read = volume_read,
  1701. .write = volume_write,
  1702. };
  1703. /*************************************************************************
  1704. * Fan subdriver
  1705. */
  1706. /*
  1707. * FAN ACCESS MODES
  1708. *
  1709. * TPACPI_FAN_RD_ACPI_GFAN:
  1710. * ACPI GFAN method: returns fan level
  1711. *
  1712. * see TPACPI_FAN_WR_ACPI_SFAN
  1713. * EC 0x2f (HFSP) not available if GFAN exists
  1714. *
  1715. * TPACPI_FAN_WR_ACPI_SFAN:
  1716. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  1717. *
  1718. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  1719. * it for writing.
  1720. *
  1721. * TPACPI_FAN_WR_TPEC:
  1722. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  1723. * Supported on almost all ThinkPads
  1724. *
  1725. * Fan speed changes of any sort (including those caused by the
  1726. * disengaged mode) are usually done slowly by the firmware as the
  1727. * maximum ammount of fan duty cycle change per second seems to be
  1728. * limited.
  1729. *
  1730. * Reading is not available if GFAN exists.
  1731. * Writing is not available if SFAN exists.
  1732. *
  1733. * Bits
  1734. * 7 automatic mode engaged;
  1735. * (default operation mode of the ThinkPad)
  1736. * fan level is ignored in this mode.
  1737. * 6 full speed mode (takes precedence over bit 7);
  1738. * not available on all thinkpads. May disable
  1739. * the tachometer while the fan controller ramps up
  1740. * the speed (which can take up to a few *minutes*).
  1741. * Speeds up fan to 100% duty-cycle, which is far above
  1742. * the standard RPM levels. It is not impossible that
  1743. * it could cause hardware damage.
  1744. * 5-3 unused in some models. Extra bits for fan level
  1745. * in others, but still useless as all values above
  1746. * 7 map to the same speed as level 7 in these models.
  1747. * 2-0 fan level (0..7 usually)
  1748. * 0x00 = stop
  1749. * 0x07 = max (set when temperatures critical)
  1750. * Some ThinkPads may have other levels, see
  1751. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  1752. *
  1753. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  1754. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  1755. * does so, its initial value is meaningless (0x07).
  1756. *
  1757. * For firmware bugs, refer to:
  1758. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  1759. *
  1760. * ----
  1761. *
  1762. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  1763. * Main fan tachometer reading (in RPM)
  1764. *
  1765. * This register is present on all ThinkPads with a new-style EC, and
  1766. * it is known not to be present on the A21m/e, and T22, as there is
  1767. * something else in offset 0x84 according to the ACPI DSDT. Other
  1768. * ThinkPads from this same time period (and earlier) probably lack the
  1769. * tachometer as well.
  1770. *
  1771. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  1772. * was never fixed by IBM to report the EC firmware version string
  1773. * probably support the tachometer (like the early X models), so
  1774. * detecting it is quite hard. We need more data to know for sure.
  1775. *
  1776. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  1777. * might result.
  1778. *
  1779. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  1780. * mode.
  1781. *
  1782. * For firmware bugs, refer to:
  1783. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  1784. *
  1785. * TPACPI_FAN_WR_ACPI_FANS:
  1786. * ThinkPad X31, X40, X41. Not available in the X60.
  1787. *
  1788. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  1789. * high speed. ACPI DSDT seems to map these three speeds to levels
  1790. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  1791. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  1792. *
  1793. * The speeds are stored on handles
  1794. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  1795. *
  1796. * There are three default speed sets, acessible as handles:
  1797. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  1798. *
  1799. * ACPI DSDT switches which set is in use depending on various
  1800. * factors.
  1801. *
  1802. * TPACPI_FAN_WR_TPEC is also available and should be used to
  1803. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  1804. * but the ACPI tables just mention level 7.
  1805. */
  1806. static enum fan_status_access_mode fan_status_access_mode;
  1807. static enum fan_control_access_mode fan_control_access_mode;
  1808. static enum fan_control_commands fan_control_commands;
  1809. static int fan_control_status_known;
  1810. static u8 fan_control_initial_status;
  1811. static void fan_watchdog_fire(struct work_struct *ignored);
  1812. static int fan_watchdog_maxinterval;
  1813. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  1814. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  1815. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  1816. "\\FSPD", /* 600e/x, 770e, 770x */
  1817. ); /* all others */
  1818. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  1819. "JFNS", /* 770x-JL */
  1820. ); /* all others */
  1821. static int __init fan_init(struct ibm_init_struct *iibm)
  1822. {
  1823. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  1824. fan_status_access_mode = TPACPI_FAN_NONE;
  1825. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  1826. fan_control_commands = 0;
  1827. fan_control_status_known = 1;
  1828. fan_watchdog_maxinterval = 0;
  1829. IBM_HANDLE_INIT(fans);
  1830. IBM_HANDLE_INIT(gfan);
  1831. IBM_HANDLE_INIT(sfan);
  1832. if (gfan_handle) {
  1833. /* 570, 600e/x, 770e, 770x */
  1834. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  1835. } else {
  1836. /* all other ThinkPads: note that even old-style
  1837. * ThinkPad ECs supports the fan control register */
  1838. if (likely(acpi_ec_read(fan_status_offset,
  1839. &fan_control_initial_status))) {
  1840. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  1841. /* In some ThinkPads, neither the EC nor the ACPI
  1842. * DSDT initialize the fan status, and it ends up
  1843. * being set to 0x07 when it *could* be either
  1844. * 0x07 or 0x80.
  1845. *
  1846. * Enable for TP-1Y (T43), TP-78 (R51e),
  1847. * TP-76 (R52), TP-70 (T43, R52), which are known
  1848. * to be buggy. */
  1849. if (fan_control_initial_status == 0x07 &&
  1850. ibm_thinkpad_ec_found &&
  1851. ((ibm_thinkpad_ec_found[0] == '1' &&
  1852. ibm_thinkpad_ec_found[1] == 'Y') ||
  1853. (ibm_thinkpad_ec_found[0] == '7' &&
  1854. (ibm_thinkpad_ec_found[1] == '6' ||
  1855. ibm_thinkpad_ec_found[1] == '8' ||
  1856. ibm_thinkpad_ec_found[1] == '0'))
  1857. )) {
  1858. printk(IBM_NOTICE
  1859. "fan_init: initial fan status is "
  1860. "unknown, assuming it is in auto "
  1861. "mode\n");
  1862. fan_control_status_known = 0;
  1863. }
  1864. } else {
  1865. printk(IBM_ERR
  1866. "ThinkPad ACPI EC access misbehaving, "
  1867. "fan status and control unavailable\n");
  1868. return 1;
  1869. }
  1870. }
  1871. if (sfan_handle) {
  1872. /* 570, 770x-JL */
  1873. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  1874. fan_control_commands |=
  1875. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  1876. } else {
  1877. if (!gfan_handle) {
  1878. /* gfan without sfan means no fan control */
  1879. /* all other models implement TP EC 0x2f control */
  1880. if (fans_handle) {
  1881. /* X31, X40, X41 */
  1882. fan_control_access_mode =
  1883. TPACPI_FAN_WR_ACPI_FANS;
  1884. fan_control_commands |=
  1885. TPACPI_FAN_CMD_SPEED |
  1886. TPACPI_FAN_CMD_LEVEL |
  1887. TPACPI_FAN_CMD_ENABLE;
  1888. } else {
  1889. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  1890. fan_control_commands |=
  1891. TPACPI_FAN_CMD_LEVEL |
  1892. TPACPI_FAN_CMD_ENABLE;
  1893. }
  1894. }
  1895. }
  1896. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  1897. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  1898. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  1899. fan_status_access_mode, fan_control_access_mode);
  1900. return (fan_status_access_mode != TPACPI_FAN_NONE ||
  1901. fan_control_access_mode != TPACPI_FAN_WR_NONE)?
  1902. 0 : 1;
  1903. }
  1904. static int fan_get_status(u8 *status)
  1905. {
  1906. u8 s;
  1907. /* TODO:
  1908. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  1909. switch (fan_status_access_mode) {
  1910. case TPACPI_FAN_RD_ACPI_GFAN:
  1911. /* 570, 600e/x, 770e, 770x */
  1912. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  1913. return -EIO;
  1914. if (likely(status))
  1915. *status = s & 0x07;
  1916. break;
  1917. case TPACPI_FAN_RD_TPEC:
  1918. /* all except 570, 600e/x, 770e, 770x */
  1919. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  1920. return -EIO;
  1921. if (likely(status))
  1922. *status = s;
  1923. break;
  1924. default:
  1925. return -ENXIO;
  1926. }
  1927. return 0;
  1928. }
  1929. static void fan_exit(void)
  1930. {
  1931. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending watchdogs\n");
  1932. cancel_delayed_work(&fan_watchdog_task);
  1933. flush_scheduled_work();
  1934. }
  1935. static int fan_get_speed(unsigned int *speed)
  1936. {
  1937. u8 hi, lo;
  1938. switch (fan_status_access_mode) {
  1939. case TPACPI_FAN_RD_TPEC:
  1940. /* all except 570, 600e/x, 770e, 770x */
  1941. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  1942. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  1943. return -EIO;
  1944. if (likely(speed))
  1945. *speed = (hi << 8) | lo;
  1946. break;
  1947. default:
  1948. return -ENXIO;
  1949. }
  1950. return 0;
  1951. }
  1952. static void fan_watchdog_fire(struct work_struct *ignored)
  1953. {
  1954. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  1955. if (fan_set_enable()) {
  1956. printk(IBM_ERR "fan watchdog: error while enabling fan\n");
  1957. /* reschedule for later */
  1958. fan_watchdog_reset();
  1959. }
  1960. }
  1961. static void fan_watchdog_reset(void)
  1962. {
  1963. static int fan_watchdog_active = 0;
  1964. if (fan_watchdog_active)
  1965. cancel_delayed_work(&fan_watchdog_task);
  1966. if (fan_watchdog_maxinterval > 0) {
  1967. fan_watchdog_active = 1;
  1968. if (!schedule_delayed_work(&fan_watchdog_task,
  1969. msecs_to_jiffies(fan_watchdog_maxinterval
  1970. * 1000))) {
  1971. printk(IBM_ERR "failed to schedule the fan watchdog, "
  1972. "watchdog will not trigger\n");
  1973. }
  1974. } else
  1975. fan_watchdog_active = 0;
  1976. }
  1977. static int fan_set_level(int level)
  1978. {
  1979. switch (fan_control_access_mode) {
  1980. case TPACPI_FAN_WR_ACPI_SFAN:
  1981. if (level >= 0 && level <= 7) {
  1982. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  1983. return -EIO;
  1984. } else
  1985. return -EINVAL;
  1986. break;
  1987. case TPACPI_FAN_WR_ACPI_FANS:
  1988. case TPACPI_FAN_WR_TPEC:
  1989. if ((level != TP_EC_FAN_AUTO) &&
  1990. (level != TP_EC_FAN_FULLSPEED) &&
  1991. ((level < 0) || (level > 7)))
  1992. return -EINVAL;
  1993. if (!acpi_ec_write(fan_status_offset, level))
  1994. return -EIO;
  1995. else
  1996. fan_control_status_known = 1;
  1997. break;
  1998. default:
  1999. return -ENXIO;
  2000. }
  2001. return 0;
  2002. }
  2003. static int fan_set_enable(void)
  2004. {
  2005. u8 s;
  2006. int rc;
  2007. switch (fan_control_access_mode) {
  2008. case TPACPI_FAN_WR_ACPI_FANS:
  2009. case TPACPI_FAN_WR_TPEC:
  2010. if ((rc = fan_get_status(&s)) < 0)
  2011. return rc;
  2012. /* Don't go out of emergency fan mode */
  2013. if (s != 7)
  2014. s = TP_EC_FAN_AUTO;
  2015. if (!acpi_ec_write(fan_status_offset, s))
  2016. return -EIO;
  2017. else
  2018. fan_control_status_known = 1;
  2019. break;
  2020. case TPACPI_FAN_WR_ACPI_SFAN:
  2021. if ((rc = fan_get_status(&s)) < 0)
  2022. return rc;
  2023. s &= 0x07;
  2024. /* Set fan to at least level 4 */
  2025. if (s < 4)
  2026. s = 4;
  2027. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  2028. return -EIO;
  2029. break;
  2030. default:
  2031. return -ENXIO;
  2032. }
  2033. return 0;
  2034. }
  2035. static int fan_set_disable(void)
  2036. {
  2037. switch (fan_control_access_mode) {
  2038. case TPACPI_FAN_WR_ACPI_FANS:
  2039. case TPACPI_FAN_WR_TPEC:
  2040. if (!acpi_ec_write(fan_status_offset, 0x00))
  2041. return -EIO;
  2042. else
  2043. fan_control_status_known = 1;
  2044. break;
  2045. case TPACPI_FAN_WR_ACPI_SFAN:
  2046. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  2047. return -EIO;
  2048. break;
  2049. default:
  2050. return -ENXIO;
  2051. }
  2052. return 0;
  2053. }
  2054. static int fan_set_speed(int speed)
  2055. {
  2056. switch (fan_control_access_mode) {
  2057. case TPACPI_FAN_WR_ACPI_FANS:
  2058. if (speed >= 0 && speed <= 65535) {
  2059. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  2060. speed, speed, speed))
  2061. return -EIO;
  2062. } else
  2063. return -EINVAL;
  2064. break;
  2065. default:
  2066. return -ENXIO;
  2067. }
  2068. return 0;
  2069. }
  2070. static int fan_read(char *p)
  2071. {
  2072. int len = 0;
  2073. int rc;
  2074. u8 status;
  2075. unsigned int speed = 0;
  2076. switch (fan_status_access_mode) {
  2077. case TPACPI_FAN_RD_ACPI_GFAN:
  2078. /* 570, 600e/x, 770e, 770x */
  2079. if ((rc = fan_get_status(&status)) < 0)
  2080. return rc;
  2081. len += sprintf(p + len, "status:\t\t%s\n"
  2082. "level:\t\t%d\n",
  2083. (status != 0) ? "enabled" : "disabled", status);
  2084. break;
  2085. case TPACPI_FAN_RD_TPEC:
  2086. /* all except 570, 600e/x, 770e, 770x */
  2087. if ((rc = fan_get_status(&status)) < 0)
  2088. return rc;
  2089. if (unlikely(!fan_control_status_known)) {
  2090. if (status != fan_control_initial_status)
  2091. fan_control_status_known = 1;
  2092. else
  2093. /* Return most likely status. In fact, it
  2094. * might be the only possible status */
  2095. status = TP_EC_FAN_AUTO;
  2096. }
  2097. len += sprintf(p + len, "status:\t\t%s\n",
  2098. (status != 0) ? "enabled" : "disabled");
  2099. if ((rc = fan_get_speed(&speed)) < 0)
  2100. return rc;
  2101. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  2102. if (status & TP_EC_FAN_FULLSPEED)
  2103. /* Disengaged mode takes precedence */
  2104. len += sprintf(p + len, "level:\t\tdisengaged\n");
  2105. else if (status & TP_EC_FAN_AUTO)
  2106. len += sprintf(p + len, "level:\t\tauto\n");
  2107. else
  2108. len += sprintf(p + len, "level:\t\t%d\n", status);
  2109. break;
  2110. case TPACPI_FAN_NONE:
  2111. default:
  2112. len += sprintf(p + len, "status:\t\tnot supported\n");
  2113. }
  2114. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  2115. len += sprintf(p + len, "commands:\tlevel <level>");
  2116. switch (fan_control_access_mode) {
  2117. case TPACPI_FAN_WR_ACPI_SFAN:
  2118. len += sprintf(p + len, " (<level> is 0-7)\n");
  2119. break;
  2120. default:
  2121. len += sprintf(p + len, " (<level> is 0-7, "
  2122. "auto, disengaged)\n");
  2123. break;
  2124. }
  2125. }
  2126. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  2127. len += sprintf(p + len, "commands:\tenable, disable\n"
  2128. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  2129. "1-120 (seconds))\n");
  2130. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  2131. len += sprintf(p + len, "commands:\tspeed <speed>"
  2132. " (<speed> is 0-65535)\n");
  2133. return len;
  2134. }
  2135. static int fan_write_cmd_level(const char *cmd, int *rc)
  2136. {
  2137. int level;
  2138. if (strlencmp(cmd, "level auto") == 0)
  2139. level = TP_EC_FAN_AUTO;
  2140. else if (strlencmp(cmd, "level disengaged") == 0)
  2141. level = TP_EC_FAN_FULLSPEED;
  2142. else if (sscanf(cmd, "level %d", &level) != 1)
  2143. return 0;
  2144. if ((*rc = fan_set_level(level)) == -ENXIO)
  2145. printk(IBM_ERR "level command accepted for unsupported "
  2146. "access mode %d", fan_control_access_mode);
  2147. return 1;
  2148. }
  2149. static int fan_write_cmd_enable(const char *cmd, int *rc)
  2150. {
  2151. if (strlencmp(cmd, "enable") != 0)
  2152. return 0;
  2153. if ((*rc = fan_set_enable()) == -ENXIO)
  2154. printk(IBM_ERR "enable command accepted for unsupported "
  2155. "access mode %d", fan_control_access_mode);
  2156. return 1;
  2157. }
  2158. static int fan_write_cmd_disable(const char *cmd, int *rc)
  2159. {
  2160. if (strlencmp(cmd, "disable") != 0)
  2161. return 0;
  2162. if ((*rc = fan_set_disable()) == -ENXIO)
  2163. printk(IBM_ERR "disable command accepted for unsupported "
  2164. "access mode %d", fan_control_access_mode);
  2165. return 1;
  2166. }
  2167. static int fan_write_cmd_speed(const char *cmd, int *rc)
  2168. {
  2169. int speed;
  2170. /* TODO:
  2171. * Support speed <low> <medium> <high> ? */
  2172. if (sscanf(cmd, "speed %d", &speed) != 1)
  2173. return 0;
  2174. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  2175. printk(IBM_ERR "speed command accepted for unsupported "
  2176. "access mode %d", fan_control_access_mode);
  2177. return 1;
  2178. }
  2179. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  2180. {
  2181. int interval;
  2182. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  2183. return 0;
  2184. if (interval < 0 || interval > 120)
  2185. *rc = -EINVAL;
  2186. else
  2187. fan_watchdog_maxinterval = interval;
  2188. return 1;
  2189. }
  2190. static int fan_write(char *buf)
  2191. {
  2192. char *cmd;
  2193. int rc = 0;
  2194. while (!rc && (cmd = next_cmd(&buf))) {
  2195. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  2196. fan_write_cmd_level(cmd, &rc)) &&
  2197. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  2198. (fan_write_cmd_enable(cmd, &rc) ||
  2199. fan_write_cmd_disable(cmd, &rc) ||
  2200. fan_write_cmd_watchdog(cmd, &rc))) &&
  2201. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  2202. fan_write_cmd_speed(cmd, &rc))
  2203. )
  2204. rc = -EINVAL;
  2205. else if (!rc)
  2206. fan_watchdog_reset();
  2207. }
  2208. return rc;
  2209. }
  2210. static struct ibm_struct fan_driver_data = {
  2211. .name = "fan",
  2212. .read = fan_read,
  2213. .write = fan_write,
  2214. .exit = fan_exit,
  2215. .experimental = 1,
  2216. };
  2217. /****************************************************************************
  2218. ****************************************************************************
  2219. *
  2220. * Infrastructure
  2221. *
  2222. ****************************************************************************
  2223. ****************************************************************************/
  2224. /* /proc support */
  2225. static struct proc_dir_entry *proc_dir = NULL;
  2226. /* Subdriver registry */
  2227. static LIST_HEAD(tpacpi_all_drivers);
  2228. /*
  2229. * Module and infrastructure proble, init and exit handling
  2230. */
  2231. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  2232. static const char * __init str_supported(int is_supported)
  2233. {
  2234. static char text_unsupported[] __initdata = "not supported";
  2235. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  2236. }
  2237. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  2238. static int __init ibm_init(struct ibm_init_struct *iibm)
  2239. {
  2240. int ret;
  2241. struct ibm_struct *ibm = iibm->data;
  2242. struct proc_dir_entry *entry;
  2243. BUG_ON(ibm == NULL);
  2244. INIT_LIST_HEAD(&ibm->all_drivers);
  2245. if (ibm->experimental && !experimental)
  2246. return 0;
  2247. dbg_printk(TPACPI_DBG_INIT,
  2248. "probing for %s\n", ibm->name);
  2249. if (iibm->init) {
  2250. ret = iibm->init(iibm);
  2251. if (ret > 0)
  2252. return 0; /* probe failed */
  2253. if (ret)
  2254. return ret;
  2255. ibm->init_called = 1;
  2256. }
  2257. if (ibm->hid) {
  2258. ret = register_tpacpi_subdriver(ibm);
  2259. if (ret)
  2260. goto err_out;
  2261. }
  2262. if (ibm->notify) {
  2263. ret = setup_notify(ibm);
  2264. if (ret == -ENODEV) {
  2265. printk(IBM_NOTICE "disabling subdriver %s\n",
  2266. ibm->name);
  2267. ret = 0;
  2268. goto err_out;
  2269. }
  2270. if (ret < 0)
  2271. goto err_out;
  2272. }
  2273. dbg_printk(TPACPI_DBG_INIT,
  2274. "%s installed\n", ibm->name);
  2275. if (ibm->read) {
  2276. entry = create_proc_entry(ibm->name,
  2277. S_IFREG | S_IRUGO | S_IWUSR,
  2278. proc_dir);
  2279. if (!entry) {
  2280. printk(IBM_ERR "unable to create proc entry %s\n",
  2281. ibm->name);
  2282. ret = -ENODEV;
  2283. goto err_out;
  2284. }
  2285. entry->owner = THIS_MODULE;
  2286. entry->data = ibm;
  2287. entry->read_proc = &dispatch_read;
  2288. if (ibm->write)
  2289. entry->write_proc = &dispatch_write;
  2290. ibm->proc_created = 1;
  2291. }
  2292. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  2293. return 0;
  2294. err_out:
  2295. dbg_printk(TPACPI_DBG_INIT,
  2296. "%s: at error exit path with result %d\n",
  2297. ibm->name, ret);
  2298. ibm_exit(ibm);
  2299. return (ret < 0)? ret : 0;
  2300. }
  2301. static void ibm_exit(struct ibm_struct *ibm)
  2302. {
  2303. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  2304. list_del_init(&ibm->all_drivers);
  2305. if (ibm->notify_installed) {
  2306. dbg_printk(TPACPI_DBG_EXIT,
  2307. "%s: acpi_remove_notify_handler\n", ibm->name);
  2308. acpi_remove_notify_handler(*ibm->handle, ibm->type,
  2309. dispatch_notify);
  2310. ibm->notify_installed = 0;
  2311. }
  2312. if (ibm->proc_created) {
  2313. dbg_printk(TPACPI_DBG_EXIT,
  2314. "%s: remove_proc_entry\n", ibm->name);
  2315. remove_proc_entry(ibm->name, proc_dir);
  2316. ibm->proc_created = 0;
  2317. }
  2318. if (ibm->driver_registered) {
  2319. dbg_printk(TPACPI_DBG_EXIT,
  2320. "%s: acpi_bus_unregister_driver\n", ibm->name);
  2321. acpi_bus_unregister_driver(ibm->driver);
  2322. kfree(ibm->driver);
  2323. ibm->driver = NULL;
  2324. ibm->driver_registered = 0;
  2325. }
  2326. if (ibm->init_called && ibm->exit) {
  2327. ibm->exit();
  2328. ibm->init_called = 0;
  2329. }
  2330. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  2331. }
  2332. /* Probing */
  2333. static char *ibm_thinkpad_ec_found = NULL;
  2334. static char* __init check_dmi_for_ec(void)
  2335. {
  2336. struct dmi_device *dev = NULL;
  2337. char ec_fw_string[18];
  2338. /*
  2339. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  2340. * X32 or newer, all Z series; Some models must have an
  2341. * up-to-date BIOS or they will not be detected.
  2342. *
  2343. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  2344. */
  2345. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  2346. if (sscanf(dev->name,
  2347. "IBM ThinkPad Embedded Controller -[%17c",
  2348. ec_fw_string) == 1) {
  2349. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  2350. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  2351. return kstrdup(ec_fw_string, GFP_KERNEL);
  2352. }
  2353. }
  2354. return NULL;
  2355. }
  2356. static int __init probe_for_thinkpad(void)
  2357. {
  2358. int is_thinkpad;
  2359. if (acpi_disabled)
  2360. return -ENODEV;
  2361. /*
  2362. * Non-ancient models have better DMI tagging, but very old models
  2363. * don't.
  2364. */
  2365. is_thinkpad = dmi_name_in_vendors("ThinkPad");
  2366. /* ec is required because many other handles are relative to it */
  2367. IBM_HANDLE_INIT(ec);
  2368. if (!ec_handle) {
  2369. if (is_thinkpad)
  2370. printk(IBM_ERR
  2371. "Not yet supported ThinkPad detected!\n");
  2372. return -ENODEV;
  2373. }
  2374. /*
  2375. * Risks a regression on very old machines, but reduces potential
  2376. * false positives a damn great deal
  2377. */
  2378. if (!is_thinkpad)
  2379. is_thinkpad = dmi_name_in_vendors("IBM");
  2380. if (!is_thinkpad && !force_load)
  2381. return -ENODEV;
  2382. return 0;
  2383. }
  2384. /* Module init, exit, parameters */
  2385. static struct ibm_init_struct ibms_init[] __initdata = {
  2386. {
  2387. .init = thinkpad_acpi_driver_init,
  2388. .data = &thinkpad_acpi_driver_data,
  2389. },
  2390. {
  2391. .init = hotkey_init,
  2392. .data = &hotkey_driver_data,
  2393. },
  2394. {
  2395. .init = bluetooth_init,
  2396. .data = &bluetooth_driver_data,
  2397. },
  2398. {
  2399. .init = wan_init,
  2400. .data = &wan_driver_data,
  2401. },
  2402. {
  2403. .init = video_init,
  2404. .data = &video_driver_data,
  2405. },
  2406. {
  2407. .init = light_init,
  2408. .data = &light_driver_data,
  2409. },
  2410. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2411. {
  2412. .init = dock_init,
  2413. .data = &dock_driver_data[0],
  2414. },
  2415. {
  2416. .data = &dock_driver_data[1],
  2417. },
  2418. #endif
  2419. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2420. {
  2421. .init = bay_init,
  2422. .data = &bay_driver_data,
  2423. },
  2424. #endif
  2425. {
  2426. .init = cmos_init,
  2427. .data = &cmos_driver_data,
  2428. },
  2429. {
  2430. .init = led_init,
  2431. .data = &led_driver_data,
  2432. },
  2433. {
  2434. .init = beep_init,
  2435. .data = &beep_driver_data,
  2436. },
  2437. {
  2438. .init = thermal_init,
  2439. .data = &thermal_driver_data,
  2440. },
  2441. {
  2442. .data = &ecdump_driver_data,
  2443. },
  2444. {
  2445. .init = brightness_init,
  2446. .data = &brightness_driver_data,
  2447. },
  2448. {
  2449. .data = &volume_driver_data,
  2450. },
  2451. {
  2452. .init = fan_init,
  2453. .data = &fan_driver_data,
  2454. },
  2455. };
  2456. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  2457. {
  2458. unsigned int i;
  2459. struct ibm_struct *ibm;
  2460. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  2461. ibm = ibms_init[i].data;
  2462. BUG_ON(ibm == NULL);
  2463. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  2464. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  2465. return -ENOSPC;
  2466. strcpy(ibms_init[i].param, val);
  2467. strcat(ibms_init[i].param, ",");
  2468. return 0;
  2469. }
  2470. }
  2471. return -EINVAL;
  2472. }
  2473. static int experimental;
  2474. module_param(experimental, int, 0);
  2475. static u32 dbg_level;
  2476. module_param_named(debug, dbg_level, uint, 0);
  2477. static int force_load;
  2478. module_param(force_load, int, 0);
  2479. #define IBM_PARAM(feature) \
  2480. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  2481. IBM_PARAM(hotkey);
  2482. IBM_PARAM(bluetooth);
  2483. IBM_PARAM(video);
  2484. IBM_PARAM(light);
  2485. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2486. IBM_PARAM(dock);
  2487. #endif
  2488. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2489. IBM_PARAM(bay);
  2490. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  2491. IBM_PARAM(cmos);
  2492. IBM_PARAM(led);
  2493. IBM_PARAM(beep);
  2494. IBM_PARAM(ecdump);
  2495. IBM_PARAM(brightness);
  2496. IBM_PARAM(volume);
  2497. IBM_PARAM(fan);
  2498. static int __init thinkpad_acpi_module_init(void)
  2499. {
  2500. int ret, i;
  2501. ret = probe_for_thinkpad();
  2502. if (ret)
  2503. return ret;
  2504. ibm_thinkpad_ec_found = check_dmi_for_ec();
  2505. IBM_HANDLE_INIT(ecrd);
  2506. IBM_HANDLE_INIT(ecwr);
  2507. proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
  2508. if (!proc_dir) {
  2509. printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
  2510. thinkpad_acpi_module_exit();
  2511. return -ENODEV;
  2512. }
  2513. proc_dir->owner = THIS_MODULE;
  2514. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  2515. ret = ibm_init(&ibms_init[i]);
  2516. if (ret >= 0 && *ibms_init[i].param)
  2517. ret = ibms_init[i].data->write(ibms_init[i].param);
  2518. if (ret < 0) {
  2519. thinkpad_acpi_module_exit();
  2520. return ret;
  2521. }
  2522. }
  2523. return 0;
  2524. }
  2525. static void thinkpad_acpi_module_exit(void)
  2526. {
  2527. struct ibm_struct *ibm, *itmp;
  2528. list_for_each_entry_safe_reverse(ibm, itmp,
  2529. &tpacpi_all_drivers,
  2530. all_drivers) {
  2531. ibm_exit(ibm);
  2532. }
  2533. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  2534. if (proc_dir)
  2535. remove_proc_entry(IBM_DIR, acpi_root_dir);
  2536. if (ibm_thinkpad_ec_found)
  2537. kfree(ibm_thinkpad_ec_found);
  2538. }
  2539. module_init(thinkpad_acpi_module_init);
  2540. module_exit(thinkpad_acpi_module_exit);