thinkpad_acpi.c 75 KB

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