toshiba_acpi.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354
  1. /*
  2. * toshiba_acpi.c - Toshiba Laptop ACPI Extras
  3. *
  4. * Copyright (C) 2002-2004 John Belmonte
  5. * Copyright (C) 2008 Philip Langdale
  6. * Copyright (C) 2010 Pierre Ducroquet
  7. * Copyright (C) 2014-2016 Azael Avalos
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * The devolpment page for this driver is located at
  23. * http://memebeam.org/toys/ToshibaAcpiDriver.
  24. *
  25. * Credits:
  26. * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
  27. * engineering the Windows drivers
  28. * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
  29. * Rob Miller - TV out and hotkeys help
  30. */
  31. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  32. #define TOSHIBA_ACPI_VERSION "0.24"
  33. #define PROC_INTERFACE_VERSION 1
  34. #include <linux/compiler.h>
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/backlight.h>
  43. #include <linux/input.h>
  44. #include <linux/input/sparse-keymap.h>
  45. #include <linux/leds.h>
  46. #include <linux/slab.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/i8042.h>
  49. #include <linux/acpi.h>
  50. #include <linux/dmi.h>
  51. #include <linux/uaccess.h>
  52. #include <linux/miscdevice.h>
  53. #include <linux/rfkill.h>
  54. #include <linux/iio/iio.h>
  55. #include <linux/toshiba.h>
  56. #include <acpi/video.h>
  57. MODULE_AUTHOR("John Belmonte");
  58. MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
  59. MODULE_LICENSE("GPL");
  60. #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
  61. /* Scan code for Fn key on TOS1900 models */
  62. #define TOS1900_FN_SCAN 0x6e
  63. /* Toshiba ACPI method paths */
  64. #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
  65. /*
  66. * The Toshiba configuration interface is composed of the HCI and the SCI,
  67. * which are defined as follows:
  68. *
  69. * HCI is Toshiba's "Hardware Control Interface" which is supposed to
  70. * be uniform across all their models. Ideally we would just call
  71. * dedicated ACPI methods instead of using this primitive interface.
  72. * However the ACPI methods seem to be incomplete in some areas (for
  73. * example they allow setting, but not reading, the LCD brightness value),
  74. * so this is still useful.
  75. *
  76. * SCI stands for "System Configuration Interface" which aim is to
  77. * conceal differences in hardware between different models.
  78. */
  79. #define TCI_WORDS 6
  80. /* Operations */
  81. #define HCI_SET 0xff00
  82. #define HCI_GET 0xfe00
  83. #define SCI_OPEN 0xf100
  84. #define SCI_CLOSE 0xf200
  85. #define SCI_GET 0xf300
  86. #define SCI_SET 0xf400
  87. /* Return codes */
  88. #define TOS_SUCCESS 0x0000
  89. #define TOS_SUCCESS2 0x0001
  90. #define TOS_OPEN_CLOSE_OK 0x0044
  91. #define TOS_FAILURE 0x1000
  92. #define TOS_NOT_SUPPORTED 0x8000
  93. #define TOS_ALREADY_OPEN 0x8100
  94. #define TOS_NOT_OPENED 0x8200
  95. #define TOS_INPUT_DATA_ERROR 0x8300
  96. #define TOS_WRITE_PROTECTED 0x8400
  97. #define TOS_NOT_PRESENT 0x8600
  98. #define TOS_FIFO_EMPTY 0x8c00
  99. #define TOS_DATA_NOT_AVAILABLE 0x8d20
  100. #define TOS_NOT_INITIALIZED 0x8d50
  101. #define TOS_NOT_INSTALLED 0x8e00
  102. /* Registers */
  103. #define HCI_FAN 0x0004
  104. #define HCI_TR_BACKLIGHT 0x0005
  105. #define HCI_SYSTEM_EVENT 0x0016
  106. #define HCI_VIDEO_OUT 0x001c
  107. #define HCI_HOTKEY_EVENT 0x001e
  108. #define HCI_LCD_BRIGHTNESS 0x002a
  109. #define HCI_WIRELESS 0x0056
  110. #define HCI_ACCELEROMETER 0x006d
  111. #define HCI_COOLING_METHOD 0x007f
  112. #define HCI_KBD_ILLUMINATION 0x0095
  113. #define HCI_ECO_MODE 0x0097
  114. #define HCI_ACCELEROMETER2 0x00a6
  115. #define HCI_SYSTEM_INFO 0xc000
  116. #define SCI_PANEL_POWER_ON 0x010d
  117. #define SCI_ILLUMINATION 0x014e
  118. #define SCI_USB_SLEEP_CHARGE 0x0150
  119. #define SCI_KBD_ILLUM_STATUS 0x015c
  120. #define SCI_USB_SLEEP_MUSIC 0x015e
  121. #define SCI_USB_THREE 0x0169
  122. #define SCI_TOUCHPAD 0x050e
  123. #define SCI_KBD_FUNCTION_KEYS 0x0522
  124. /* Field definitions */
  125. #define HCI_ACCEL_MASK 0x7fff
  126. #define HCI_ACCEL_DIRECTION_MASK 0x8000
  127. #define HCI_HOTKEY_DISABLE 0x0b
  128. #define HCI_HOTKEY_ENABLE 0x09
  129. #define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
  130. #define HCI_LCD_BRIGHTNESS_BITS 3
  131. #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
  132. #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
  133. #define HCI_MISC_SHIFT 0x10
  134. #define HCI_SYSTEM_TYPE1 0x10
  135. #define HCI_SYSTEM_TYPE2 0x11
  136. #define HCI_VIDEO_OUT_LCD 0x1
  137. #define HCI_VIDEO_OUT_CRT 0x2
  138. #define HCI_VIDEO_OUT_TV 0x4
  139. #define SCI_KBD_MODE_MASK 0x1f
  140. #define SCI_KBD_MODE_FNZ 0x1
  141. #define SCI_KBD_MODE_AUTO 0x2
  142. #define SCI_KBD_MODE_ON 0x8
  143. #define SCI_KBD_MODE_OFF 0x10
  144. #define SCI_KBD_TIME_MAX 0x3c001a
  145. #define HCI_WIRELESS_STATUS 0x1
  146. #define HCI_WIRELESS_WWAN 0x3
  147. #define HCI_WIRELESS_WWAN_STATUS 0x2000
  148. #define HCI_WIRELESS_WWAN_POWER 0x4000
  149. #define SCI_USB_CHARGE_MODE_MASK 0xff
  150. #define SCI_USB_CHARGE_DISABLED 0x00
  151. #define SCI_USB_CHARGE_ALTERNATE 0x09
  152. #define SCI_USB_CHARGE_TYPICAL 0x11
  153. #define SCI_USB_CHARGE_AUTO 0x21
  154. #define SCI_USB_CHARGE_BAT_MASK 0x7
  155. #define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
  156. #define SCI_USB_CHARGE_BAT_LVL_ON 0x4
  157. #define SCI_USB_CHARGE_BAT_LVL 0x0200
  158. #define SCI_USB_CHARGE_RAPID_DSP 0x0300
  159. struct toshiba_acpi_dev {
  160. struct acpi_device *acpi_dev;
  161. const char *method_hci;
  162. struct input_dev *hotkey_dev;
  163. struct work_struct hotkey_work;
  164. struct backlight_device *backlight_dev;
  165. struct led_classdev led_dev;
  166. struct led_classdev kbd_led;
  167. struct led_classdev eco_led;
  168. struct miscdevice miscdev;
  169. struct rfkill *wwan_rfk;
  170. struct iio_dev *indio_dev;
  171. int force_fan;
  172. int last_key_event;
  173. int key_event_valid;
  174. int kbd_type;
  175. int kbd_mode;
  176. int kbd_time;
  177. int usbsc_bat_level;
  178. int usbsc_mode_base;
  179. int hotkey_event_type;
  180. int max_cooling_method;
  181. unsigned int illumination_supported:1;
  182. unsigned int video_supported:1;
  183. unsigned int fan_supported:1;
  184. unsigned int system_event_supported:1;
  185. unsigned int ntfy_supported:1;
  186. unsigned int info_supported:1;
  187. unsigned int tr_backlight_supported:1;
  188. unsigned int kbd_illum_supported:1;
  189. unsigned int touchpad_supported:1;
  190. unsigned int eco_supported:1;
  191. unsigned int accelerometer_supported:1;
  192. unsigned int usb_sleep_charge_supported:1;
  193. unsigned int usb_rapid_charge_supported:1;
  194. unsigned int usb_sleep_music_supported:1;
  195. unsigned int kbd_function_keys_supported:1;
  196. unsigned int panel_power_on_supported:1;
  197. unsigned int usb_three_supported:1;
  198. unsigned int wwan_supported:1;
  199. unsigned int cooling_method_supported:1;
  200. unsigned int sysfs_created:1;
  201. unsigned int special_functions;
  202. bool kbd_event_generated;
  203. bool kbd_led_registered;
  204. bool illumination_led_registered;
  205. bool eco_led_registered;
  206. bool killswitch;
  207. };
  208. static struct toshiba_acpi_dev *toshiba_acpi;
  209. static bool disable_hotkeys;
  210. module_param(disable_hotkeys, bool, 0444);
  211. MODULE_PARM_DESC(disable_hotkeys, "Disables the hotkeys activation");
  212. static const struct acpi_device_id toshiba_device_ids[] = {
  213. {"TOS6200", 0},
  214. {"TOS6207", 0},
  215. {"TOS6208", 0},
  216. {"TOS1900", 0},
  217. {"", 0},
  218. };
  219. MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
  220. static const struct key_entry toshiba_acpi_keymap[] = {
  221. { KE_KEY, 0x9e, { KEY_RFKILL } },
  222. { KE_KEY, 0x101, { KEY_MUTE } },
  223. { KE_KEY, 0x102, { KEY_ZOOMOUT } },
  224. { KE_KEY, 0x103, { KEY_ZOOMIN } },
  225. { KE_KEY, 0x10f, { KEY_TAB } },
  226. { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
  227. { KE_KEY, 0x139, { KEY_ZOOMRESET } },
  228. { KE_KEY, 0x13b, { KEY_COFFEE } },
  229. { KE_KEY, 0x13c, { KEY_BATTERY } },
  230. { KE_KEY, 0x13d, { KEY_SLEEP } },
  231. { KE_KEY, 0x13e, { KEY_SUSPEND } },
  232. { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
  233. { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
  234. { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
  235. { KE_KEY, 0x142, { KEY_WLAN } },
  236. { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
  237. { KE_KEY, 0x17f, { KEY_FN } },
  238. { KE_KEY, 0xb05, { KEY_PROG2 } },
  239. { KE_KEY, 0xb06, { KEY_WWW } },
  240. { KE_KEY, 0xb07, { KEY_MAIL } },
  241. { KE_KEY, 0xb30, { KEY_STOP } },
  242. { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
  243. { KE_KEY, 0xb32, { KEY_NEXTSONG } },
  244. { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
  245. { KE_KEY, 0xb5a, { KEY_MEDIA } },
  246. { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
  247. { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
  248. { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
  249. { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
  250. { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
  251. { KE_END, 0 },
  252. };
  253. static const struct key_entry toshiba_acpi_alt_keymap[] = {
  254. { KE_KEY, 0x102, { KEY_ZOOMOUT } },
  255. { KE_KEY, 0x103, { KEY_ZOOMIN } },
  256. { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
  257. { KE_KEY, 0x139, { KEY_ZOOMRESET } },
  258. { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
  259. { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
  260. { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
  261. { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
  262. { KE_KEY, 0x157, { KEY_MUTE } },
  263. { KE_KEY, 0x158, { KEY_WLAN } },
  264. { KE_END, 0 },
  265. };
  266. /*
  267. * List of models which have a broken acpi-video backlight interface and thus
  268. * need to use the toshiba (vendor) interface instead.
  269. */
  270. static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
  271. {}
  272. };
  273. /*
  274. * Utility
  275. */
  276. static inline void _set_bit(u32 *word, u32 mask, int value)
  277. {
  278. *word = (*word & ~mask) | (mask * value);
  279. }
  280. /*
  281. * ACPI interface wrappers
  282. */
  283. static int write_acpi_int(const char *methodName, int val)
  284. {
  285. acpi_status status;
  286. status = acpi_execute_simple_method(NULL, (char *)methodName, val);
  287. return (status == AE_OK) ? 0 : -EIO;
  288. }
  289. /*
  290. * Perform a raw configuration call. Here we don't care about input or output
  291. * buffer format.
  292. */
  293. static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
  294. const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
  295. {
  296. union acpi_object in_objs[TCI_WORDS], out_objs[TCI_WORDS + 1];
  297. struct acpi_object_list params;
  298. struct acpi_buffer results;
  299. acpi_status status;
  300. int i;
  301. params.count = TCI_WORDS;
  302. params.pointer = in_objs;
  303. for (i = 0; i < TCI_WORDS; ++i) {
  304. in_objs[i].type = ACPI_TYPE_INTEGER;
  305. in_objs[i].integer.value = in[i];
  306. }
  307. results.length = sizeof(out_objs);
  308. results.pointer = out_objs;
  309. status = acpi_evaluate_object(dev->acpi_dev->handle,
  310. (char *)dev->method_hci, &params,
  311. &results);
  312. if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
  313. for (i = 0; i < out_objs->package.count; ++i)
  314. out[i] = out_objs->package.elements[i].integer.value;
  315. }
  316. return status;
  317. }
  318. /*
  319. * Common hci tasks
  320. *
  321. * In addition to the ACPI status, the HCI system returns a result which
  322. * may be useful (such as "not supported").
  323. */
  324. static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
  325. {
  326. u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
  327. u32 out[TCI_WORDS];
  328. acpi_status status = tci_raw(dev, in, out);
  329. return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
  330. }
  331. static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
  332. {
  333. u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
  334. u32 out[TCI_WORDS];
  335. acpi_status status = tci_raw(dev, in, out);
  336. if (ACPI_FAILURE(status))
  337. return TOS_FAILURE;
  338. *out1 = out[2];
  339. return out[0];
  340. }
  341. /*
  342. * Common sci tasks
  343. */
  344. static int sci_open(struct toshiba_acpi_dev *dev)
  345. {
  346. u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
  347. u32 out[TCI_WORDS];
  348. acpi_status status = tci_raw(dev, in, out);
  349. if (ACPI_FAILURE(status)) {
  350. pr_err("ACPI call to open SCI failed\n");
  351. return 0;
  352. }
  353. if (out[0] == TOS_OPEN_CLOSE_OK) {
  354. return 1;
  355. } else if (out[0] == TOS_ALREADY_OPEN) {
  356. pr_info("Toshiba SCI already opened\n");
  357. return 1;
  358. } else if (out[0] == TOS_NOT_SUPPORTED) {
  359. /*
  360. * Some BIOSes do not have the SCI open/close functions
  361. * implemented and return 0x8000 (Not Supported), failing to
  362. * register some supported features.
  363. *
  364. * Simply return 1 if we hit those affected laptops to make the
  365. * supported features work.
  366. *
  367. * In the case that some laptops really do not support the SCI,
  368. * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
  369. * and thus, not registering support for the queried feature.
  370. */
  371. return 1;
  372. } else if (out[0] == TOS_NOT_PRESENT) {
  373. pr_info("Toshiba SCI is not present\n");
  374. }
  375. return 0;
  376. }
  377. static void sci_close(struct toshiba_acpi_dev *dev)
  378. {
  379. u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
  380. u32 out[TCI_WORDS];
  381. acpi_status status = tci_raw(dev, in, out);
  382. if (ACPI_FAILURE(status)) {
  383. pr_err("ACPI call to close SCI failed\n");
  384. return;
  385. }
  386. if (out[0] == TOS_OPEN_CLOSE_OK)
  387. return;
  388. else if (out[0] == TOS_NOT_OPENED)
  389. pr_info("Toshiba SCI not opened\n");
  390. else if (out[0] == TOS_NOT_PRESENT)
  391. pr_info("Toshiba SCI is not present\n");
  392. }
  393. static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
  394. {
  395. u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
  396. u32 out[TCI_WORDS];
  397. acpi_status status = tci_raw(dev, in, out);
  398. if (ACPI_FAILURE(status))
  399. return TOS_FAILURE;
  400. *out1 = out[2];
  401. return out[0];
  402. }
  403. static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
  404. {
  405. u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
  406. u32 out[TCI_WORDS];
  407. acpi_status status = tci_raw(dev, in, out);
  408. return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
  409. }
  410. /* Illumination support */
  411. static void toshiba_illumination_available(struct toshiba_acpi_dev *dev)
  412. {
  413. u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
  414. u32 out[TCI_WORDS];
  415. acpi_status status;
  416. dev->illumination_supported = 0;
  417. dev->illumination_led_registered = false;
  418. if (!sci_open(dev))
  419. return;
  420. status = tci_raw(dev, in, out);
  421. sci_close(dev);
  422. if (ACPI_FAILURE(status)) {
  423. pr_err("ACPI call to query Illumination support failed\n");
  424. return;
  425. }
  426. if (out[0] != TOS_SUCCESS)
  427. return;
  428. dev->illumination_supported = 1;
  429. }
  430. static void toshiba_illumination_set(struct led_classdev *cdev,
  431. enum led_brightness brightness)
  432. {
  433. struct toshiba_acpi_dev *dev = container_of(cdev,
  434. struct toshiba_acpi_dev, led_dev);
  435. u32 result;
  436. u32 state;
  437. /* First request : initialize communication. */
  438. if (!sci_open(dev))
  439. return;
  440. /* Switch the illumination on/off */
  441. state = brightness ? 1 : 0;
  442. result = sci_write(dev, SCI_ILLUMINATION, state);
  443. sci_close(dev);
  444. if (result == TOS_FAILURE)
  445. pr_err("ACPI call for illumination failed\n");
  446. }
  447. static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
  448. {
  449. struct toshiba_acpi_dev *dev = container_of(cdev,
  450. struct toshiba_acpi_dev, led_dev);
  451. u32 result;
  452. u32 state;
  453. /* First request : initialize communication. */
  454. if (!sci_open(dev))
  455. return LED_OFF;
  456. /* Check the illumination */
  457. result = sci_read(dev, SCI_ILLUMINATION, &state);
  458. sci_close(dev);
  459. if (result == TOS_FAILURE) {
  460. pr_err("ACPI call for illumination failed\n");
  461. return LED_OFF;
  462. } else if (result != TOS_SUCCESS) {
  463. return LED_OFF;
  464. }
  465. return state ? LED_FULL : LED_OFF;
  466. }
  467. /* KBD Illumination */
  468. static void toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
  469. {
  470. u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
  471. u32 out[TCI_WORDS];
  472. acpi_status status;
  473. dev->kbd_illum_supported = 0;
  474. dev->kbd_led_registered = false;
  475. dev->kbd_event_generated = false;
  476. if (!sci_open(dev))
  477. return;
  478. status = tci_raw(dev, in, out);
  479. sci_close(dev);
  480. if (ACPI_FAILURE(status)) {
  481. pr_err("ACPI call to query kbd illumination support failed\n");
  482. return;
  483. }
  484. if (out[0] != TOS_SUCCESS)
  485. return;
  486. /*
  487. * Check for keyboard backlight timeout max value,
  488. * previous kbd backlight implementation set this to
  489. * 0x3c0003, and now the new implementation set this
  490. * to 0x3c001a, use this to distinguish between them.
  491. */
  492. if (out[3] == SCI_KBD_TIME_MAX)
  493. dev->kbd_type = 2;
  494. else
  495. dev->kbd_type = 1;
  496. /* Get the current keyboard backlight mode */
  497. dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
  498. /* Get the current time (1-60 seconds) */
  499. dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
  500. /* Flag as supported */
  501. dev->kbd_illum_supported = 1;
  502. }
  503. static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
  504. {
  505. u32 result;
  506. if (!sci_open(dev))
  507. return -EIO;
  508. result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
  509. sci_close(dev);
  510. if (result == TOS_FAILURE)
  511. pr_err("ACPI call to set KBD backlight status failed\n");
  512. else if (result == TOS_NOT_SUPPORTED)
  513. return -ENODEV;
  514. return result == TOS_SUCCESS ? 0 : -EIO;
  515. }
  516. static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
  517. {
  518. u32 result;
  519. if (!sci_open(dev))
  520. return -EIO;
  521. result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
  522. sci_close(dev);
  523. if (result == TOS_FAILURE)
  524. pr_err("ACPI call to get KBD backlight status failed\n");
  525. else if (result == TOS_NOT_SUPPORTED)
  526. return -ENODEV;
  527. return result == TOS_SUCCESS ? 0 : -EIO;
  528. }
  529. static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
  530. {
  531. struct toshiba_acpi_dev *dev = container_of(cdev,
  532. struct toshiba_acpi_dev, kbd_led);
  533. u32 result;
  534. u32 state;
  535. /* Check the keyboard backlight state */
  536. result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
  537. if (result == TOS_FAILURE) {
  538. pr_err("ACPI call to get the keyboard backlight failed\n");
  539. return LED_OFF;
  540. } else if (result != TOS_SUCCESS) {
  541. return LED_OFF;
  542. }
  543. return state ? LED_FULL : LED_OFF;
  544. }
  545. static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
  546. enum led_brightness brightness)
  547. {
  548. struct toshiba_acpi_dev *dev = container_of(cdev,
  549. struct toshiba_acpi_dev, kbd_led);
  550. u32 result;
  551. u32 state;
  552. /* Set the keyboard backlight state */
  553. state = brightness ? 1 : 0;
  554. result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
  555. if (result == TOS_FAILURE)
  556. pr_err("ACPI call to set KBD Illumination mode failed\n");
  557. }
  558. /* TouchPad support */
  559. static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
  560. {
  561. u32 result;
  562. if (!sci_open(dev))
  563. return -EIO;
  564. result = sci_write(dev, SCI_TOUCHPAD, state);
  565. sci_close(dev);
  566. if (result == TOS_FAILURE)
  567. pr_err("ACPI call to set the touchpad failed\n");
  568. else if (result == TOS_NOT_SUPPORTED)
  569. return -ENODEV;
  570. return result == TOS_SUCCESS ? 0 : -EIO;
  571. }
  572. static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
  573. {
  574. u32 result;
  575. if (!sci_open(dev))
  576. return -EIO;
  577. result = sci_read(dev, SCI_TOUCHPAD, state);
  578. sci_close(dev);
  579. if (result == TOS_FAILURE)
  580. pr_err("ACPI call to query the touchpad failed\n");
  581. else if (result == TOS_NOT_SUPPORTED)
  582. return -ENODEV;
  583. return result == TOS_SUCCESS ? 0 : -EIO;
  584. }
  585. /* Eco Mode support */
  586. static void toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
  587. {
  588. u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
  589. u32 out[TCI_WORDS];
  590. acpi_status status;
  591. dev->eco_supported = 0;
  592. dev->eco_led_registered = false;
  593. status = tci_raw(dev, in, out);
  594. if (ACPI_FAILURE(status)) {
  595. pr_err("ACPI call to get ECO led failed\n");
  596. return;
  597. }
  598. if (out[0] == TOS_INPUT_DATA_ERROR) {
  599. /*
  600. * If we receive 0x8300 (Input Data Error), it means that the
  601. * LED device is present, but that we just screwed the input
  602. * parameters.
  603. *
  604. * Let's query the status of the LED to see if we really have a
  605. * success response, indicating the actual presense of the LED,
  606. * bail out otherwise.
  607. */
  608. in[3] = 1;
  609. status = tci_raw(dev, in, out);
  610. if (ACPI_FAILURE(status)) {
  611. pr_err("ACPI call to get ECO led failed\n");
  612. return;
  613. }
  614. if (out[0] != TOS_SUCCESS)
  615. return;
  616. dev->eco_supported = 1;
  617. }
  618. }
  619. static enum led_brightness
  620. toshiba_eco_mode_get_status(struct led_classdev *cdev)
  621. {
  622. struct toshiba_acpi_dev *dev = container_of(cdev,
  623. struct toshiba_acpi_dev, eco_led);
  624. u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
  625. u32 out[TCI_WORDS];
  626. acpi_status status;
  627. status = tci_raw(dev, in, out);
  628. if (ACPI_FAILURE(status)) {
  629. pr_err("ACPI call to get ECO led failed\n");
  630. return LED_OFF;
  631. }
  632. if (out[0] != TOS_SUCCESS)
  633. return LED_OFF;
  634. return out[2] ? LED_FULL : LED_OFF;
  635. }
  636. static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
  637. enum led_brightness brightness)
  638. {
  639. struct toshiba_acpi_dev *dev = container_of(cdev,
  640. struct toshiba_acpi_dev, eco_led);
  641. u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
  642. u32 out[TCI_WORDS];
  643. acpi_status status;
  644. /* Switch the Eco Mode led on/off */
  645. in[2] = (brightness) ? 1 : 0;
  646. status = tci_raw(dev, in, out);
  647. if (ACPI_FAILURE(status))
  648. pr_err("ACPI call to set ECO led failed\n");
  649. }
  650. /* Accelerometer support */
  651. static void toshiba_accelerometer_available(struct toshiba_acpi_dev *dev)
  652. {
  653. u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
  654. u32 out[TCI_WORDS];
  655. acpi_status status;
  656. dev->accelerometer_supported = 0;
  657. /*
  658. * Check if the accelerometer call exists,
  659. * this call also serves as initialization
  660. */
  661. status = tci_raw(dev, in, out);
  662. if (ACPI_FAILURE(status)) {
  663. pr_err("ACPI call to query the accelerometer failed\n");
  664. return;
  665. }
  666. if (out[0] != TOS_SUCCESS)
  667. return;
  668. dev->accelerometer_supported = 1;
  669. }
  670. static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
  671. u32 *xy, u32 *z)
  672. {
  673. u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
  674. u32 out[TCI_WORDS];
  675. acpi_status status;
  676. /* Check the Accelerometer status */
  677. status = tci_raw(dev, in, out);
  678. if (ACPI_FAILURE(status)) {
  679. pr_err("ACPI call to query the accelerometer failed\n");
  680. return -EIO;
  681. }
  682. if (out[0] == TOS_NOT_SUPPORTED)
  683. return -ENODEV;
  684. if (out[0] != TOS_SUCCESS)
  685. return -EIO;
  686. *xy = out[2];
  687. *z = out[4];
  688. return 0;
  689. }
  690. /* Sleep (Charge and Music) utilities support */
  691. static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
  692. {
  693. u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
  694. u32 out[TCI_WORDS];
  695. acpi_status status;
  696. dev->usb_sleep_charge_supported = 0;
  697. if (!sci_open(dev))
  698. return;
  699. status = tci_raw(dev, in, out);
  700. if (ACPI_FAILURE(status)) {
  701. pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
  702. sci_close(dev);
  703. return;
  704. }
  705. if (out[0] != TOS_SUCCESS) {
  706. sci_close(dev);
  707. return;
  708. }
  709. dev->usbsc_mode_base = out[4];
  710. in[5] = SCI_USB_CHARGE_BAT_LVL;
  711. status = tci_raw(dev, in, out);
  712. sci_close(dev);
  713. if (ACPI_FAILURE(status)) {
  714. pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
  715. return;
  716. }
  717. if (out[0] != TOS_SUCCESS)
  718. return;
  719. dev->usbsc_bat_level = out[2];
  720. /* Flag as supported */
  721. dev->usb_sleep_charge_supported = 1;
  722. }
  723. static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
  724. u32 *mode)
  725. {
  726. u32 result;
  727. if (!sci_open(dev))
  728. return -EIO;
  729. result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
  730. sci_close(dev);
  731. if (result == TOS_FAILURE)
  732. pr_err("ACPI call to set USB S&C mode failed\n");
  733. else if (result == TOS_NOT_SUPPORTED)
  734. return -ENODEV;
  735. return result == TOS_SUCCESS ? 0 : -EIO;
  736. }
  737. static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
  738. u32 mode)
  739. {
  740. u32 result;
  741. if (!sci_open(dev))
  742. return -EIO;
  743. result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
  744. sci_close(dev);
  745. if (result == TOS_FAILURE)
  746. pr_err("ACPI call to set USB S&C mode failed\n");
  747. else if (result == TOS_NOT_SUPPORTED)
  748. return -ENODEV;
  749. return result == TOS_SUCCESS ? 0 : -EIO;
  750. }
  751. static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
  752. u32 *mode)
  753. {
  754. u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
  755. u32 out[TCI_WORDS];
  756. acpi_status status;
  757. if (!sci_open(dev))
  758. return -EIO;
  759. in[5] = SCI_USB_CHARGE_BAT_LVL;
  760. status = tci_raw(dev, in, out);
  761. sci_close(dev);
  762. if (ACPI_FAILURE(status)) {
  763. pr_err("ACPI call to get USB S&C battery level failed\n");
  764. return -EIO;
  765. }
  766. if (out[0] == TOS_NOT_SUPPORTED)
  767. return -ENODEV;
  768. if (out[0] != TOS_SUCCESS)
  769. return -EIO;
  770. *mode = out[2];
  771. return 0;
  772. }
  773. static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
  774. u32 mode)
  775. {
  776. u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
  777. u32 out[TCI_WORDS];
  778. acpi_status status;
  779. if (!sci_open(dev))
  780. return -EIO;
  781. in[2] = mode;
  782. in[5] = SCI_USB_CHARGE_BAT_LVL;
  783. status = tci_raw(dev, in, out);
  784. sci_close(dev);
  785. if (ACPI_FAILURE(status)) {
  786. pr_err("ACPI call to set USB S&C battery level failed\n");
  787. return -EIO;
  788. }
  789. if (out[0] == TOS_NOT_SUPPORTED)
  790. return -ENODEV;
  791. return out[0] == TOS_SUCCESS ? 0 : -EIO;
  792. }
  793. static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
  794. u32 *state)
  795. {
  796. u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
  797. u32 out[TCI_WORDS];
  798. acpi_status status;
  799. if (!sci_open(dev))
  800. return -EIO;
  801. in[5] = SCI_USB_CHARGE_RAPID_DSP;
  802. status = tci_raw(dev, in, out);
  803. sci_close(dev);
  804. if (ACPI_FAILURE(status)) {
  805. pr_err("ACPI call to get USB Rapid Charge failed\n");
  806. return -EIO;
  807. }
  808. if (out[0] == TOS_NOT_SUPPORTED)
  809. return -ENODEV;
  810. if (out[0] != TOS_SUCCESS && out[0] != TOS_SUCCESS2)
  811. return -EIO;
  812. *state = out[2];
  813. return 0;
  814. }
  815. static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
  816. u32 state)
  817. {
  818. u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
  819. u32 out[TCI_WORDS];
  820. acpi_status status;
  821. if (!sci_open(dev))
  822. return -EIO;
  823. in[2] = state;
  824. in[5] = SCI_USB_CHARGE_RAPID_DSP;
  825. status = tci_raw(dev, in, out);
  826. sci_close(dev);
  827. if (ACPI_FAILURE(status)) {
  828. pr_err("ACPI call to set USB Rapid Charge failed\n");
  829. return -EIO;
  830. }
  831. if (out[0] == TOS_NOT_SUPPORTED)
  832. return -ENODEV;
  833. return (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) ? 0 : -EIO;
  834. }
  835. static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
  836. {
  837. u32 result;
  838. if (!sci_open(dev))
  839. return -EIO;
  840. result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
  841. sci_close(dev);
  842. if (result == TOS_FAILURE)
  843. pr_err("ACPI call to get Sleep and Music failed\n");
  844. else if (result == TOS_NOT_SUPPORTED)
  845. return -ENODEV;
  846. return result == TOS_SUCCESS ? 0 : -EIO;
  847. }
  848. static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
  849. {
  850. u32 result;
  851. if (!sci_open(dev))
  852. return -EIO;
  853. result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
  854. sci_close(dev);
  855. if (result == TOS_FAILURE)
  856. pr_err("ACPI call to set Sleep and Music failed\n");
  857. else if (result == TOS_NOT_SUPPORTED)
  858. return -ENODEV;
  859. return result == TOS_SUCCESS ? 0 : -EIO;
  860. }
  861. /* Keyboard function keys */
  862. static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
  863. {
  864. u32 result;
  865. if (!sci_open(dev))
  866. return -EIO;
  867. result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
  868. sci_close(dev);
  869. if (result == TOS_FAILURE)
  870. pr_err("ACPI call to get KBD function keys failed\n");
  871. else if (result == TOS_NOT_SUPPORTED)
  872. return -ENODEV;
  873. return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
  874. }
  875. static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
  876. {
  877. u32 result;
  878. if (!sci_open(dev))
  879. return -EIO;
  880. result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
  881. sci_close(dev);
  882. if (result == TOS_FAILURE)
  883. pr_err("ACPI call to set KBD function keys failed\n");
  884. else if (result == TOS_NOT_SUPPORTED)
  885. return -ENODEV;
  886. return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
  887. }
  888. /* Panel Power ON */
  889. static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
  890. {
  891. u32 result;
  892. if (!sci_open(dev))
  893. return -EIO;
  894. result = sci_read(dev, SCI_PANEL_POWER_ON, state);
  895. sci_close(dev);
  896. if (result == TOS_FAILURE)
  897. pr_err("ACPI call to get Panel Power ON failed\n");
  898. else if (result == TOS_NOT_SUPPORTED)
  899. return -ENODEV;
  900. return result == TOS_SUCCESS ? 0 : -EIO;
  901. }
  902. static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
  903. {
  904. u32 result;
  905. if (!sci_open(dev))
  906. return -EIO;
  907. result = sci_write(dev, SCI_PANEL_POWER_ON, state);
  908. sci_close(dev);
  909. if (result == TOS_FAILURE)
  910. pr_err("ACPI call to set Panel Power ON failed\n");
  911. else if (result == TOS_NOT_SUPPORTED)
  912. return -ENODEV;
  913. return result == TOS_SUCCESS ? 0 : -EIO;
  914. }
  915. /* USB Three */
  916. static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
  917. {
  918. u32 result;
  919. if (!sci_open(dev))
  920. return -EIO;
  921. result = sci_read(dev, SCI_USB_THREE, state);
  922. sci_close(dev);
  923. if (result == TOS_FAILURE)
  924. pr_err("ACPI call to get USB 3 failed\n");
  925. else if (result == TOS_NOT_SUPPORTED)
  926. return -ENODEV;
  927. return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
  928. }
  929. static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
  930. {
  931. u32 result;
  932. if (!sci_open(dev))
  933. return -EIO;
  934. result = sci_write(dev, SCI_USB_THREE, state);
  935. sci_close(dev);
  936. if (result == TOS_FAILURE)
  937. pr_err("ACPI call to set USB 3 failed\n");
  938. else if (result == TOS_NOT_SUPPORTED)
  939. return -ENODEV;
  940. return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
  941. }
  942. /* Hotkey Event type */
  943. static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
  944. u32 *type)
  945. {
  946. u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
  947. u32 out[TCI_WORDS];
  948. acpi_status status;
  949. status = tci_raw(dev, in, out);
  950. if (ACPI_FAILURE(status)) {
  951. pr_err("ACPI call to get System type failed\n");
  952. return -EIO;
  953. }
  954. if (out[0] == TOS_NOT_SUPPORTED)
  955. return -ENODEV;
  956. if (out[0] != TOS_SUCCESS)
  957. return -EIO;
  958. *type = out[3];
  959. return 0;
  960. }
  961. /* Wireless status (RFKill, WLAN, BT, WWAN) */
  962. static int toshiba_wireless_status(struct toshiba_acpi_dev *dev)
  963. {
  964. u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
  965. u32 out[TCI_WORDS];
  966. acpi_status status;
  967. in[3] = HCI_WIRELESS_STATUS;
  968. status = tci_raw(dev, in, out);
  969. if (ACPI_FAILURE(status)) {
  970. pr_err("ACPI call to get Wireless status failed\n");
  971. return -EIO;
  972. }
  973. if (out[0] == TOS_NOT_SUPPORTED)
  974. return -ENODEV;
  975. if (out[0] != TOS_SUCCESS)
  976. return -EIO;
  977. dev->killswitch = !!(out[2] & HCI_WIRELESS_STATUS);
  978. return 0;
  979. }
  980. /* WWAN */
  981. static void toshiba_wwan_available(struct toshiba_acpi_dev *dev)
  982. {
  983. u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
  984. u32 out[TCI_WORDS];
  985. acpi_status status;
  986. dev->wwan_supported = 0;
  987. /*
  988. * WWAN support can be queried by setting the in[3] value to
  989. * HCI_WIRELESS_WWAN (0x03).
  990. *
  991. * If supported, out[0] contains TOS_SUCCESS and out[2] contains
  992. * HCI_WIRELESS_WWAN_STATUS (0x2000).
  993. *
  994. * If not supported, out[0] contains TOS_INPUT_DATA_ERROR (0x8300)
  995. * or TOS_NOT_SUPPORTED (0x8000).
  996. */
  997. in[3] = HCI_WIRELESS_WWAN;
  998. status = tci_raw(dev, in, out);
  999. if (ACPI_FAILURE(status)) {
  1000. pr_err("ACPI call to get WWAN status failed\n");
  1001. return;
  1002. }
  1003. if (out[0] != TOS_SUCCESS)
  1004. return;
  1005. dev->wwan_supported = (out[2] == HCI_WIRELESS_WWAN_STATUS);
  1006. }
  1007. static int toshiba_wwan_set(struct toshiba_acpi_dev *dev, u32 state)
  1008. {
  1009. u32 in[TCI_WORDS] = { HCI_SET, HCI_WIRELESS, state, 0, 0, 0 };
  1010. u32 out[TCI_WORDS];
  1011. acpi_status status;
  1012. in[3] = HCI_WIRELESS_WWAN_STATUS;
  1013. status = tci_raw(dev, in, out);
  1014. if (ACPI_FAILURE(status)) {
  1015. pr_err("ACPI call to set WWAN status failed\n");
  1016. return -EIO;
  1017. }
  1018. if (out[0] == TOS_NOT_SUPPORTED)
  1019. return -ENODEV;
  1020. if (out[0] != TOS_SUCCESS)
  1021. return -EIO;
  1022. /*
  1023. * Some devices only need to call HCI_WIRELESS_WWAN_STATUS to
  1024. * (de)activate the device, but some others need the
  1025. * HCI_WIRELESS_WWAN_POWER call as well.
  1026. */
  1027. in[3] = HCI_WIRELESS_WWAN_POWER;
  1028. status = tci_raw(dev, in, out);
  1029. if (ACPI_FAILURE(status)) {
  1030. pr_err("ACPI call to set WWAN power failed\n");
  1031. return -EIO;
  1032. }
  1033. if (out[0] == TOS_NOT_SUPPORTED)
  1034. return -ENODEV;
  1035. return out[0] == TOS_SUCCESS ? 0 : -EIO;
  1036. }
  1037. /* Cooling Method */
  1038. static void toshiba_cooling_method_available(struct toshiba_acpi_dev *dev)
  1039. {
  1040. u32 in[TCI_WORDS] = { HCI_GET, HCI_COOLING_METHOD, 0, 0, 0, 0 };
  1041. u32 out[TCI_WORDS];
  1042. acpi_status status;
  1043. dev->cooling_method_supported = 0;
  1044. dev->max_cooling_method = 0;
  1045. status = tci_raw(dev, in, out);
  1046. if (ACPI_FAILURE(status)) {
  1047. pr_err("ACPI call to get Cooling Method failed\n");
  1048. return;
  1049. }
  1050. if (out[0] != TOS_SUCCESS && out[0] != TOS_SUCCESS2)
  1051. return;
  1052. dev->cooling_method_supported = 1;
  1053. dev->max_cooling_method = out[3];
  1054. }
  1055. static int toshiba_cooling_method_get(struct toshiba_acpi_dev *dev, u32 *state)
  1056. {
  1057. u32 result = hci_read(dev, HCI_COOLING_METHOD, state);
  1058. if (result == TOS_FAILURE)
  1059. pr_err("ACPI call to get Cooling Method failed\n");
  1060. if (result == TOS_NOT_SUPPORTED)
  1061. return -ENODEV;
  1062. return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
  1063. }
  1064. static int toshiba_cooling_method_set(struct toshiba_acpi_dev *dev, u32 state)
  1065. {
  1066. u32 result = hci_write(dev, HCI_COOLING_METHOD, state);
  1067. if (result == TOS_FAILURE)
  1068. pr_err("ACPI call to set Cooling Method failed\n");
  1069. if (result == TOS_NOT_SUPPORTED)
  1070. return -ENODEV;
  1071. return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
  1072. }
  1073. /* Transflective Backlight */
  1074. static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 *status)
  1075. {
  1076. u32 result = hci_read(dev, HCI_TR_BACKLIGHT, status);
  1077. if (result == TOS_FAILURE)
  1078. pr_err("ACPI call to get Transflective Backlight failed\n");
  1079. else if (result == TOS_NOT_SUPPORTED)
  1080. return -ENODEV;
  1081. return result == TOS_SUCCESS ? 0 : -EIO;
  1082. }
  1083. static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 status)
  1084. {
  1085. u32 result = hci_write(dev, HCI_TR_BACKLIGHT, !status);
  1086. if (result == TOS_FAILURE)
  1087. pr_err("ACPI call to set Transflective Backlight failed\n");
  1088. else if (result == TOS_NOT_SUPPORTED)
  1089. return -ENODEV;
  1090. return result == TOS_SUCCESS ? 0 : -EIO;
  1091. }
  1092. static struct proc_dir_entry *toshiba_proc_dir;
  1093. /* LCD Brightness */
  1094. static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
  1095. {
  1096. int brightness = 0;
  1097. u32 result;
  1098. u32 value;
  1099. if (dev->tr_backlight_supported) {
  1100. int ret = get_tr_backlight_status(dev, &value);
  1101. if (ret)
  1102. return ret;
  1103. if (value)
  1104. return 0;
  1105. brightness++;
  1106. }
  1107. result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
  1108. if (result == TOS_FAILURE)
  1109. pr_err("ACPI call to get LCD Brightness failed\n");
  1110. else if (result == TOS_NOT_SUPPORTED)
  1111. return -ENODEV;
  1112. return result == TOS_SUCCESS ?
  1113. brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT) :
  1114. -EIO;
  1115. }
  1116. static int get_lcd_brightness(struct backlight_device *bd)
  1117. {
  1118. struct toshiba_acpi_dev *dev = bl_get_data(bd);
  1119. return __get_lcd_brightness(dev);
  1120. }
  1121. static int lcd_proc_show(struct seq_file *m, void *v)
  1122. {
  1123. struct toshiba_acpi_dev *dev = m->private;
  1124. int levels;
  1125. int value;
  1126. if (!dev->backlight_dev)
  1127. return -ENODEV;
  1128. levels = dev->backlight_dev->props.max_brightness + 1;
  1129. value = get_lcd_brightness(dev->backlight_dev);
  1130. if (value < 0) {
  1131. pr_err("Error reading LCD brightness\n");
  1132. return value;
  1133. }
  1134. seq_printf(m, "brightness: %d\n", value);
  1135. seq_printf(m, "brightness_levels: %d\n", levels);
  1136. return 0;
  1137. }
  1138. static int lcd_proc_open(struct inode *inode, struct file *file)
  1139. {
  1140. return single_open(file, lcd_proc_show, PDE_DATA(inode));
  1141. }
  1142. static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
  1143. {
  1144. u32 result;
  1145. if (dev->tr_backlight_supported) {
  1146. int ret = set_tr_backlight_status(dev, !value);
  1147. if (ret)
  1148. return ret;
  1149. if (value)
  1150. value--;
  1151. }
  1152. value = value << HCI_LCD_BRIGHTNESS_SHIFT;
  1153. result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
  1154. if (result == TOS_FAILURE)
  1155. pr_err("ACPI call to set LCD Brightness failed\n");
  1156. else if (result == TOS_NOT_SUPPORTED)
  1157. return -ENODEV;
  1158. return result == TOS_SUCCESS ? 0 : -EIO;
  1159. }
  1160. static int set_lcd_status(struct backlight_device *bd)
  1161. {
  1162. struct toshiba_acpi_dev *dev = bl_get_data(bd);
  1163. return set_lcd_brightness(dev, bd->props.brightness);
  1164. }
  1165. static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
  1166. size_t count, loff_t *pos)
  1167. {
  1168. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  1169. char cmd[42];
  1170. size_t len;
  1171. int levels;
  1172. int value;
  1173. len = min(count, sizeof(cmd) - 1);
  1174. if (copy_from_user(cmd, buf, len))
  1175. return -EFAULT;
  1176. cmd[len] = '\0';
  1177. levels = dev->backlight_dev->props.max_brightness + 1;
  1178. if (sscanf(cmd, " brightness : %i", &value) != 1 &&
  1179. value < 0 && value > levels)
  1180. return -EINVAL;
  1181. if (set_lcd_brightness(dev, value))
  1182. return -EIO;
  1183. return count;
  1184. }
  1185. static const struct file_operations lcd_proc_fops = {
  1186. .owner = THIS_MODULE,
  1187. .open = lcd_proc_open,
  1188. .read = seq_read,
  1189. .llseek = seq_lseek,
  1190. .release = single_release,
  1191. .write = lcd_proc_write,
  1192. };
  1193. /* Video-Out */
  1194. static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
  1195. {
  1196. u32 result = hci_read(dev, HCI_VIDEO_OUT, status);
  1197. if (result == TOS_FAILURE)
  1198. pr_err("ACPI call to get Video-Out failed\n");
  1199. else if (result == TOS_NOT_SUPPORTED)
  1200. return -ENODEV;
  1201. return result == TOS_SUCCESS ? 0 : -EIO;
  1202. }
  1203. static int video_proc_show(struct seq_file *m, void *v)
  1204. {
  1205. struct toshiba_acpi_dev *dev = m->private;
  1206. int is_lcd, is_crt, is_tv;
  1207. u32 value;
  1208. if (get_video_status(dev, &value))
  1209. return -EIO;
  1210. is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  1211. is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  1212. is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  1213. seq_printf(m, "lcd_out: %d\n", is_lcd);
  1214. seq_printf(m, "crt_out: %d\n", is_crt);
  1215. seq_printf(m, "tv_out: %d\n", is_tv);
  1216. return 0;
  1217. }
  1218. static int video_proc_open(struct inode *inode, struct file *file)
  1219. {
  1220. return single_open(file, video_proc_show, PDE_DATA(inode));
  1221. }
  1222. static ssize_t video_proc_write(struct file *file, const char __user *buf,
  1223. size_t count, loff_t *pos)
  1224. {
  1225. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  1226. char *buffer;
  1227. char *cmd;
  1228. int lcd_out, crt_out, tv_out;
  1229. int remain = count;
  1230. int value;
  1231. int ret;
  1232. u32 video_out;
  1233. cmd = memdup_user_nul(buf, count);
  1234. if (IS_ERR(cmd))
  1235. return PTR_ERR(cmd);
  1236. buffer = cmd;
  1237. /*
  1238. * Scan expression. Multiple expressions may be delimited with ;
  1239. * NOTE: To keep scanning simple, invalid fields are ignored.
  1240. */
  1241. while (remain) {
  1242. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  1243. lcd_out = value & 1;
  1244. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  1245. crt_out = value & 1;
  1246. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  1247. tv_out = value & 1;
  1248. /* Advance to one character past the next ; */
  1249. do {
  1250. ++buffer;
  1251. --remain;
  1252. } while (remain && *(buffer - 1) != ';');
  1253. }
  1254. kfree(cmd);
  1255. lcd_out = crt_out = tv_out = -1;
  1256. ret = get_video_status(dev, &video_out);
  1257. if (!ret) {
  1258. unsigned int new_video_out = video_out;
  1259. if (lcd_out != -1)
  1260. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  1261. if (crt_out != -1)
  1262. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  1263. if (tv_out != -1)
  1264. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  1265. /*
  1266. * To avoid unnecessary video disruption, only write the new
  1267. * video setting if something changed.
  1268. */
  1269. if (new_video_out != video_out)
  1270. ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  1271. }
  1272. return ret ? -EIO : count;
  1273. }
  1274. static const struct file_operations video_proc_fops = {
  1275. .owner = THIS_MODULE,
  1276. .open = video_proc_open,
  1277. .read = seq_read,
  1278. .llseek = seq_lseek,
  1279. .release = single_release,
  1280. .write = video_proc_write,
  1281. };
  1282. /* Fan status */
  1283. static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
  1284. {
  1285. u32 result = hci_read(dev, HCI_FAN, status);
  1286. if (result == TOS_FAILURE)
  1287. pr_err("ACPI call to get Fan status failed\n");
  1288. else if (result == TOS_NOT_SUPPORTED)
  1289. return -ENODEV;
  1290. return result == TOS_SUCCESS ? 0 : -EIO;
  1291. }
  1292. static int set_fan_status(struct toshiba_acpi_dev *dev, u32 status)
  1293. {
  1294. u32 result = hci_write(dev, HCI_FAN, status);
  1295. if (result == TOS_FAILURE)
  1296. pr_err("ACPI call to set Fan status failed\n");
  1297. else if (result == TOS_NOT_SUPPORTED)
  1298. return -ENODEV;
  1299. return result == TOS_SUCCESS ? 0 : -EIO;
  1300. }
  1301. static int fan_proc_show(struct seq_file *m, void *v)
  1302. {
  1303. struct toshiba_acpi_dev *dev = m->private;
  1304. u32 value;
  1305. if (get_fan_status(dev, &value))
  1306. return -EIO;
  1307. seq_printf(m, "running: %d\n", (value > 0));
  1308. seq_printf(m, "force_on: %d\n", dev->force_fan);
  1309. return 0;
  1310. }
  1311. static int fan_proc_open(struct inode *inode, struct file *file)
  1312. {
  1313. return single_open(file, fan_proc_show, PDE_DATA(inode));
  1314. }
  1315. static ssize_t fan_proc_write(struct file *file, const char __user *buf,
  1316. size_t count, loff_t *pos)
  1317. {
  1318. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  1319. char cmd[42];
  1320. size_t len;
  1321. int value;
  1322. len = min(count, sizeof(cmd) - 1);
  1323. if (copy_from_user(cmd, buf, len))
  1324. return -EFAULT;
  1325. cmd[len] = '\0';
  1326. if (sscanf(cmd, " force_on : %i", &value) != 1 &&
  1327. value != 0 && value != 1)
  1328. return -EINVAL;
  1329. if (set_fan_status(dev, value))
  1330. return -EIO;
  1331. dev->force_fan = value;
  1332. return count;
  1333. }
  1334. static const struct file_operations fan_proc_fops = {
  1335. .owner = THIS_MODULE,
  1336. .open = fan_proc_open,
  1337. .read = seq_read,
  1338. .llseek = seq_lseek,
  1339. .release = single_release,
  1340. .write = fan_proc_write,
  1341. };
  1342. static int keys_proc_show(struct seq_file *m, void *v)
  1343. {
  1344. struct toshiba_acpi_dev *dev = m->private;
  1345. seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
  1346. seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
  1347. return 0;
  1348. }
  1349. static int keys_proc_open(struct inode *inode, struct file *file)
  1350. {
  1351. return single_open(file, keys_proc_show, PDE_DATA(inode));
  1352. }
  1353. static ssize_t keys_proc_write(struct file *file, const char __user *buf,
  1354. size_t count, loff_t *pos)
  1355. {
  1356. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  1357. char cmd[42];
  1358. size_t len;
  1359. int value;
  1360. len = min(count, sizeof(cmd) - 1);
  1361. if (copy_from_user(cmd, buf, len))
  1362. return -EFAULT;
  1363. cmd[len] = '\0';
  1364. if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
  1365. dev->key_event_valid = 0;
  1366. else
  1367. return -EINVAL;
  1368. return count;
  1369. }
  1370. static const struct file_operations keys_proc_fops = {
  1371. .owner = THIS_MODULE,
  1372. .open = keys_proc_open,
  1373. .read = seq_read,
  1374. .llseek = seq_lseek,
  1375. .release = single_release,
  1376. .write = keys_proc_write,
  1377. };
  1378. static int __maybe_unused version_proc_show(struct seq_file *m, void *v)
  1379. {
  1380. seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  1381. seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
  1382. return 0;
  1383. }
  1384. /*
  1385. * Proc and module init
  1386. */
  1387. #define PROC_TOSHIBA "toshiba"
  1388. static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
  1389. {
  1390. if (dev->backlight_dev)
  1391. proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1392. &lcd_proc_fops, dev);
  1393. if (dev->video_supported)
  1394. proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1395. &video_proc_fops, dev);
  1396. if (dev->fan_supported)
  1397. proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1398. &fan_proc_fops, dev);
  1399. if (dev->hotkey_dev)
  1400. proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1401. &keys_proc_fops, dev);
  1402. proc_create_single_data("version", S_IRUGO, toshiba_proc_dir,
  1403. version_proc_show, dev);
  1404. }
  1405. static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
  1406. {
  1407. if (dev->backlight_dev)
  1408. remove_proc_entry("lcd", toshiba_proc_dir);
  1409. if (dev->video_supported)
  1410. remove_proc_entry("video", toshiba_proc_dir);
  1411. if (dev->fan_supported)
  1412. remove_proc_entry("fan", toshiba_proc_dir);
  1413. if (dev->hotkey_dev)
  1414. remove_proc_entry("keys", toshiba_proc_dir);
  1415. remove_proc_entry("version", toshiba_proc_dir);
  1416. }
  1417. static const struct backlight_ops toshiba_backlight_data = {
  1418. .options = BL_CORE_SUSPENDRESUME,
  1419. .get_brightness = get_lcd_brightness,
  1420. .update_status = set_lcd_status,
  1421. };
  1422. /* Keyboard backlight work */
  1423. static void toshiba_acpi_kbd_bl_work(struct work_struct *work);
  1424. static DECLARE_WORK(kbd_bl_work, toshiba_acpi_kbd_bl_work);
  1425. /*
  1426. * Sysfs files
  1427. */
  1428. static ssize_t version_show(struct device *dev,
  1429. struct device_attribute *attr, char *buf)
  1430. {
  1431. return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
  1432. }
  1433. static DEVICE_ATTR_RO(version);
  1434. static ssize_t fan_store(struct device *dev,
  1435. struct device_attribute *attr,
  1436. const char *buf, size_t count)
  1437. {
  1438. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1439. int state;
  1440. int ret;
  1441. ret = kstrtoint(buf, 0, &state);
  1442. if (ret)
  1443. return ret;
  1444. if (state != 0 && state != 1)
  1445. return -EINVAL;
  1446. ret = set_fan_status(toshiba, state);
  1447. if (ret)
  1448. return ret;
  1449. return count;
  1450. }
  1451. static ssize_t fan_show(struct device *dev,
  1452. struct device_attribute *attr, char *buf)
  1453. {
  1454. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1455. u32 value;
  1456. int ret;
  1457. ret = get_fan_status(toshiba, &value);
  1458. if (ret)
  1459. return ret;
  1460. return sprintf(buf, "%d\n", value);
  1461. }
  1462. static DEVICE_ATTR_RW(fan);
  1463. static ssize_t kbd_backlight_mode_store(struct device *dev,
  1464. struct device_attribute *attr,
  1465. const char *buf, size_t count)
  1466. {
  1467. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1468. int mode;
  1469. int ret;
  1470. ret = kstrtoint(buf, 0, &mode);
  1471. if (ret)
  1472. return ret;
  1473. /* Check for supported modes depending on keyboard backlight type */
  1474. if (toshiba->kbd_type == 1) {
  1475. /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
  1476. if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
  1477. return -EINVAL;
  1478. } else if (toshiba->kbd_type == 2) {
  1479. /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
  1480. if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
  1481. mode != SCI_KBD_MODE_OFF)
  1482. return -EINVAL;
  1483. }
  1484. /*
  1485. * Set the Keyboard Backlight Mode where:
  1486. * Auto - KBD backlight turns off automatically in given time
  1487. * FN-Z - KBD backlight "toggles" when hotkey pressed
  1488. * ON - KBD backlight is always on
  1489. * OFF - KBD backlight is always off
  1490. */
  1491. /* Only make a change if the actual mode has changed */
  1492. if (toshiba->kbd_mode != mode) {
  1493. /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
  1494. int time = toshiba->kbd_time << HCI_MISC_SHIFT;
  1495. /* OR the "base time" to the actual method format */
  1496. if (toshiba->kbd_type == 1) {
  1497. /* Type 1 requires the current mode */
  1498. time |= toshiba->kbd_mode;
  1499. } else if (toshiba->kbd_type == 2) {
  1500. /* Type 2 requires the desired mode */
  1501. time |= mode;
  1502. }
  1503. ret = toshiba_kbd_illum_status_set(toshiba, time);
  1504. if (ret)
  1505. return ret;
  1506. toshiba->kbd_mode = mode;
  1507. toshiba_acpi->kbd_mode = mode;
  1508. /*
  1509. * Some laptop models with the second generation backlit
  1510. * keyboard (type 2) do not generate the keyboard backlight
  1511. * changed event (0x92), and thus, the driver will never update
  1512. * the sysfs entries.
  1513. *
  1514. * The event is generated right when changing the keyboard
  1515. * backlight mode and the *notify function will set the
  1516. * kbd_event_generated to true.
  1517. *
  1518. * In case the event is not generated, schedule the keyboard
  1519. * backlight work to update the sysfs entries and emulate the
  1520. * event via genetlink.
  1521. */
  1522. if (toshiba->kbd_type == 2 &&
  1523. !toshiba->kbd_event_generated)
  1524. schedule_work(&kbd_bl_work);
  1525. }
  1526. return count;
  1527. }
  1528. static ssize_t kbd_backlight_mode_show(struct device *dev,
  1529. struct device_attribute *attr,
  1530. char *buf)
  1531. {
  1532. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1533. u32 time;
  1534. if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
  1535. return -EIO;
  1536. return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
  1537. }
  1538. static DEVICE_ATTR_RW(kbd_backlight_mode);
  1539. static ssize_t kbd_type_show(struct device *dev,
  1540. struct device_attribute *attr, char *buf)
  1541. {
  1542. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1543. return sprintf(buf, "%d\n", toshiba->kbd_type);
  1544. }
  1545. static DEVICE_ATTR_RO(kbd_type);
  1546. static ssize_t available_kbd_modes_show(struct device *dev,
  1547. struct device_attribute *attr,
  1548. char *buf)
  1549. {
  1550. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1551. if (toshiba->kbd_type == 1)
  1552. return sprintf(buf, "0x%x 0x%x\n",
  1553. SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
  1554. return sprintf(buf, "0x%x 0x%x 0x%x\n",
  1555. SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
  1556. }
  1557. static DEVICE_ATTR_RO(available_kbd_modes);
  1558. static ssize_t kbd_backlight_timeout_store(struct device *dev,
  1559. struct device_attribute *attr,
  1560. const char *buf, size_t count)
  1561. {
  1562. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1563. int time;
  1564. int ret;
  1565. ret = kstrtoint(buf, 0, &time);
  1566. if (ret)
  1567. return ret;
  1568. /* Check for supported values depending on kbd_type */
  1569. if (toshiba->kbd_type == 1) {
  1570. if (time < 0 || time > 60)
  1571. return -EINVAL;
  1572. } else if (toshiba->kbd_type == 2) {
  1573. if (time < 1 || time > 60)
  1574. return -EINVAL;
  1575. }
  1576. /* Set the Keyboard Backlight Timeout */
  1577. /* Only make a change if the actual timeout has changed */
  1578. if (toshiba->kbd_time != time) {
  1579. /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
  1580. time = time << HCI_MISC_SHIFT;
  1581. /* OR the "base time" to the actual method format */
  1582. if (toshiba->kbd_type == 1)
  1583. time |= SCI_KBD_MODE_FNZ;
  1584. else if (toshiba->kbd_type == 2)
  1585. time |= SCI_KBD_MODE_AUTO;
  1586. ret = toshiba_kbd_illum_status_set(toshiba, time);
  1587. if (ret)
  1588. return ret;
  1589. toshiba->kbd_time = time >> HCI_MISC_SHIFT;
  1590. }
  1591. return count;
  1592. }
  1593. static ssize_t kbd_backlight_timeout_show(struct device *dev,
  1594. struct device_attribute *attr,
  1595. char *buf)
  1596. {
  1597. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1598. u32 time;
  1599. if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
  1600. return -EIO;
  1601. return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
  1602. }
  1603. static DEVICE_ATTR_RW(kbd_backlight_timeout);
  1604. static ssize_t touchpad_store(struct device *dev,
  1605. struct device_attribute *attr,
  1606. const char *buf, size_t count)
  1607. {
  1608. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1609. int state;
  1610. int ret;
  1611. /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
  1612. ret = kstrtoint(buf, 0, &state);
  1613. if (ret)
  1614. return ret;
  1615. if (state != 0 && state != 1)
  1616. return -EINVAL;
  1617. ret = toshiba_touchpad_set(toshiba, state);
  1618. if (ret)
  1619. return ret;
  1620. return count;
  1621. }
  1622. static ssize_t touchpad_show(struct device *dev,
  1623. struct device_attribute *attr, char *buf)
  1624. {
  1625. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1626. u32 state;
  1627. int ret;
  1628. ret = toshiba_touchpad_get(toshiba, &state);
  1629. if (ret < 0)
  1630. return ret;
  1631. return sprintf(buf, "%i\n", state);
  1632. }
  1633. static DEVICE_ATTR_RW(touchpad);
  1634. static ssize_t usb_sleep_charge_show(struct device *dev,
  1635. struct device_attribute *attr, char *buf)
  1636. {
  1637. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1638. u32 mode;
  1639. int ret;
  1640. ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
  1641. if (ret < 0)
  1642. return ret;
  1643. return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
  1644. }
  1645. static ssize_t usb_sleep_charge_store(struct device *dev,
  1646. struct device_attribute *attr,
  1647. const char *buf, size_t count)
  1648. {
  1649. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1650. int state;
  1651. u32 mode;
  1652. int ret;
  1653. ret = kstrtoint(buf, 0, &state);
  1654. if (ret)
  1655. return ret;
  1656. /*
  1657. * Check for supported values, where:
  1658. * 0 - Disabled
  1659. * 1 - Alternate (Non USB conformant devices that require more power)
  1660. * 2 - Auto (USB conformant devices)
  1661. * 3 - Typical
  1662. */
  1663. if (state != 0 && state != 1 && state != 2 && state != 3)
  1664. return -EINVAL;
  1665. /* Set the USB charging mode to internal value */
  1666. mode = toshiba->usbsc_mode_base;
  1667. if (state == 0)
  1668. mode |= SCI_USB_CHARGE_DISABLED;
  1669. else if (state == 1)
  1670. mode |= SCI_USB_CHARGE_ALTERNATE;
  1671. else if (state == 2)
  1672. mode |= SCI_USB_CHARGE_AUTO;
  1673. else if (state == 3)
  1674. mode |= SCI_USB_CHARGE_TYPICAL;
  1675. ret = toshiba_usb_sleep_charge_set(toshiba, mode);
  1676. if (ret)
  1677. return ret;
  1678. return count;
  1679. }
  1680. static DEVICE_ATTR_RW(usb_sleep_charge);
  1681. static ssize_t sleep_functions_on_battery_show(struct device *dev,
  1682. struct device_attribute *attr,
  1683. char *buf)
  1684. {
  1685. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1686. int bat_lvl, status;
  1687. u32 state;
  1688. int ret;
  1689. int tmp;
  1690. ret = toshiba_sleep_functions_status_get(toshiba, &state);
  1691. if (ret < 0)
  1692. return ret;
  1693. /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
  1694. tmp = state & SCI_USB_CHARGE_BAT_MASK;
  1695. status = (tmp == 0x4) ? 1 : 0;
  1696. /* Determine the battery level set */
  1697. bat_lvl = state >> HCI_MISC_SHIFT;
  1698. return sprintf(buf, "%d %d\n", status, bat_lvl);
  1699. }
  1700. static ssize_t sleep_functions_on_battery_store(struct device *dev,
  1701. struct device_attribute *attr,
  1702. const char *buf, size_t count)
  1703. {
  1704. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1705. u32 status;
  1706. int value;
  1707. int ret;
  1708. int tmp;
  1709. ret = kstrtoint(buf, 0, &value);
  1710. if (ret)
  1711. return ret;
  1712. /*
  1713. * Set the status of the function:
  1714. * 0 - Disabled
  1715. * 1-100 - Enabled
  1716. */
  1717. if (value < 0 || value > 100)
  1718. return -EINVAL;
  1719. if (value == 0) {
  1720. tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
  1721. status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
  1722. } else {
  1723. tmp = value << HCI_MISC_SHIFT;
  1724. status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
  1725. }
  1726. ret = toshiba_sleep_functions_status_set(toshiba, status);
  1727. if (ret < 0)
  1728. return ret;
  1729. toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
  1730. return count;
  1731. }
  1732. static DEVICE_ATTR_RW(sleep_functions_on_battery);
  1733. static ssize_t usb_rapid_charge_show(struct device *dev,
  1734. struct device_attribute *attr, char *buf)
  1735. {
  1736. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1737. u32 state;
  1738. int ret;
  1739. ret = toshiba_usb_rapid_charge_get(toshiba, &state);
  1740. if (ret < 0)
  1741. return ret;
  1742. return sprintf(buf, "%d\n", state);
  1743. }
  1744. static ssize_t usb_rapid_charge_store(struct device *dev,
  1745. struct device_attribute *attr,
  1746. const char *buf, size_t count)
  1747. {
  1748. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1749. int state;
  1750. int ret;
  1751. ret = kstrtoint(buf, 0, &state);
  1752. if (ret)
  1753. return ret;
  1754. if (state != 0 && state != 1)
  1755. return -EINVAL;
  1756. ret = toshiba_usb_rapid_charge_set(toshiba, state);
  1757. if (ret)
  1758. return ret;
  1759. return count;
  1760. }
  1761. static DEVICE_ATTR_RW(usb_rapid_charge);
  1762. static ssize_t usb_sleep_music_show(struct device *dev,
  1763. struct device_attribute *attr, char *buf)
  1764. {
  1765. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1766. u32 state;
  1767. int ret;
  1768. ret = toshiba_usb_sleep_music_get(toshiba, &state);
  1769. if (ret < 0)
  1770. return ret;
  1771. return sprintf(buf, "%d\n", state);
  1772. }
  1773. static ssize_t usb_sleep_music_store(struct device *dev,
  1774. struct device_attribute *attr,
  1775. const char *buf, size_t count)
  1776. {
  1777. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1778. int state;
  1779. int ret;
  1780. ret = kstrtoint(buf, 0, &state);
  1781. if (ret)
  1782. return ret;
  1783. if (state != 0 && state != 1)
  1784. return -EINVAL;
  1785. ret = toshiba_usb_sleep_music_set(toshiba, state);
  1786. if (ret)
  1787. return ret;
  1788. return count;
  1789. }
  1790. static DEVICE_ATTR_RW(usb_sleep_music);
  1791. static ssize_t kbd_function_keys_show(struct device *dev,
  1792. struct device_attribute *attr, char *buf)
  1793. {
  1794. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1795. int mode;
  1796. int ret;
  1797. ret = toshiba_function_keys_get(toshiba, &mode);
  1798. if (ret < 0)
  1799. return ret;
  1800. return sprintf(buf, "%d\n", mode);
  1801. }
  1802. static ssize_t kbd_function_keys_store(struct device *dev,
  1803. struct device_attribute *attr,
  1804. const char *buf, size_t count)
  1805. {
  1806. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1807. int mode;
  1808. int ret;
  1809. ret = kstrtoint(buf, 0, &mode);
  1810. if (ret)
  1811. return ret;
  1812. /*
  1813. * Check for the function keys mode where:
  1814. * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
  1815. * 1 - Special functions (Opposite of the above setting)
  1816. */
  1817. if (mode != 0 && mode != 1)
  1818. return -EINVAL;
  1819. ret = toshiba_function_keys_set(toshiba, mode);
  1820. if (ret)
  1821. return ret;
  1822. pr_info("Reboot for changes to KBD Function Keys to take effect");
  1823. return count;
  1824. }
  1825. static DEVICE_ATTR_RW(kbd_function_keys);
  1826. static ssize_t panel_power_on_show(struct device *dev,
  1827. struct device_attribute *attr, char *buf)
  1828. {
  1829. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1830. u32 state;
  1831. int ret;
  1832. ret = toshiba_panel_power_on_get(toshiba, &state);
  1833. if (ret < 0)
  1834. return ret;
  1835. return sprintf(buf, "%d\n", state);
  1836. }
  1837. static ssize_t panel_power_on_store(struct device *dev,
  1838. struct device_attribute *attr,
  1839. const char *buf, size_t count)
  1840. {
  1841. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1842. int state;
  1843. int ret;
  1844. ret = kstrtoint(buf, 0, &state);
  1845. if (ret)
  1846. return ret;
  1847. if (state != 0 && state != 1)
  1848. return -EINVAL;
  1849. ret = toshiba_panel_power_on_set(toshiba, state);
  1850. if (ret)
  1851. return ret;
  1852. pr_info("Reboot for changes to Panel Power ON to take effect");
  1853. return count;
  1854. }
  1855. static DEVICE_ATTR_RW(panel_power_on);
  1856. static ssize_t usb_three_show(struct device *dev,
  1857. struct device_attribute *attr, char *buf)
  1858. {
  1859. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1860. u32 state;
  1861. int ret;
  1862. ret = toshiba_usb_three_get(toshiba, &state);
  1863. if (ret < 0)
  1864. return ret;
  1865. return sprintf(buf, "%d\n", state);
  1866. }
  1867. static ssize_t usb_three_store(struct device *dev,
  1868. struct device_attribute *attr,
  1869. const char *buf, size_t count)
  1870. {
  1871. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1872. int state;
  1873. int ret;
  1874. ret = kstrtoint(buf, 0, &state);
  1875. if (ret)
  1876. return ret;
  1877. /*
  1878. * Check for USB 3 mode where:
  1879. * 0 - Disabled (Acts like a USB 2 port, saving power)
  1880. * 1 - Enabled
  1881. */
  1882. if (state != 0 && state != 1)
  1883. return -EINVAL;
  1884. ret = toshiba_usb_three_set(toshiba, state);
  1885. if (ret)
  1886. return ret;
  1887. pr_info("Reboot for changes to USB 3 to take effect");
  1888. return count;
  1889. }
  1890. static DEVICE_ATTR_RW(usb_three);
  1891. static ssize_t cooling_method_show(struct device *dev,
  1892. struct device_attribute *attr, char *buf)
  1893. {
  1894. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1895. int state;
  1896. int ret;
  1897. ret = toshiba_cooling_method_get(toshiba, &state);
  1898. if (ret < 0)
  1899. return ret;
  1900. return sprintf(buf, "%d %d\n", state, toshiba->max_cooling_method);
  1901. }
  1902. static ssize_t cooling_method_store(struct device *dev,
  1903. struct device_attribute *attr,
  1904. const char *buf, size_t count)
  1905. {
  1906. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1907. int state;
  1908. int ret;
  1909. ret = kstrtoint(buf, 0, &state);
  1910. if (ret)
  1911. return ret;
  1912. /*
  1913. * Check for supported values
  1914. * Depending on the laptop model, some only support these two:
  1915. * 0 - Maximum Performance
  1916. * 1 - Battery Optimized
  1917. *
  1918. * While some others support all three methods:
  1919. * 0 - Maximum Performance
  1920. * 1 - Performance
  1921. * 2 - Battery Optimized
  1922. */
  1923. if (state < 0 || state > toshiba->max_cooling_method)
  1924. return -EINVAL;
  1925. ret = toshiba_cooling_method_set(toshiba, state);
  1926. if (ret)
  1927. return ret;
  1928. return count;
  1929. }
  1930. static DEVICE_ATTR_RW(cooling_method);
  1931. static struct attribute *toshiba_attributes[] = {
  1932. &dev_attr_version.attr,
  1933. &dev_attr_fan.attr,
  1934. &dev_attr_kbd_backlight_mode.attr,
  1935. &dev_attr_kbd_type.attr,
  1936. &dev_attr_available_kbd_modes.attr,
  1937. &dev_attr_kbd_backlight_timeout.attr,
  1938. &dev_attr_touchpad.attr,
  1939. &dev_attr_usb_sleep_charge.attr,
  1940. &dev_attr_sleep_functions_on_battery.attr,
  1941. &dev_attr_usb_rapid_charge.attr,
  1942. &dev_attr_usb_sleep_music.attr,
  1943. &dev_attr_kbd_function_keys.attr,
  1944. &dev_attr_panel_power_on.attr,
  1945. &dev_attr_usb_three.attr,
  1946. &dev_attr_cooling_method.attr,
  1947. NULL,
  1948. };
  1949. static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
  1950. struct attribute *attr, int idx)
  1951. {
  1952. struct device *dev = container_of(kobj, struct device, kobj);
  1953. struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
  1954. bool exists = true;
  1955. if (attr == &dev_attr_fan.attr)
  1956. exists = (drv->fan_supported) ? true : false;
  1957. else if (attr == &dev_attr_kbd_backlight_mode.attr)
  1958. exists = (drv->kbd_illum_supported) ? true : false;
  1959. else if (attr == &dev_attr_kbd_backlight_timeout.attr)
  1960. exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
  1961. else if (attr == &dev_attr_touchpad.attr)
  1962. exists = (drv->touchpad_supported) ? true : false;
  1963. else if (attr == &dev_attr_usb_sleep_charge.attr)
  1964. exists = (drv->usb_sleep_charge_supported) ? true : false;
  1965. else if (attr == &dev_attr_sleep_functions_on_battery.attr)
  1966. exists = (drv->usb_sleep_charge_supported) ? true : false;
  1967. else if (attr == &dev_attr_usb_rapid_charge.attr)
  1968. exists = (drv->usb_rapid_charge_supported) ? true : false;
  1969. else if (attr == &dev_attr_usb_sleep_music.attr)
  1970. exists = (drv->usb_sleep_music_supported) ? true : false;
  1971. else if (attr == &dev_attr_kbd_function_keys.attr)
  1972. exists = (drv->kbd_function_keys_supported) ? true : false;
  1973. else if (attr == &dev_attr_panel_power_on.attr)
  1974. exists = (drv->panel_power_on_supported) ? true : false;
  1975. else if (attr == &dev_attr_usb_three.attr)
  1976. exists = (drv->usb_three_supported) ? true : false;
  1977. else if (attr == &dev_attr_cooling_method.attr)
  1978. exists = (drv->cooling_method_supported) ? true : false;
  1979. return exists ? attr->mode : 0;
  1980. }
  1981. static const struct attribute_group toshiba_attr_group = {
  1982. .is_visible = toshiba_sysfs_is_visible,
  1983. .attrs = toshiba_attributes,
  1984. };
  1985. static void toshiba_acpi_kbd_bl_work(struct work_struct *work)
  1986. {
  1987. /* Update the sysfs entries */
  1988. if (sysfs_update_group(&toshiba_acpi->acpi_dev->dev.kobj,
  1989. &toshiba_attr_group))
  1990. pr_err("Unable to update sysfs entries\n");
  1991. /* Notify LED subsystem about keyboard backlight change */
  1992. if (toshiba_acpi->kbd_type == 2 &&
  1993. toshiba_acpi->kbd_mode != SCI_KBD_MODE_AUTO)
  1994. led_classdev_notify_brightness_hw_changed(&toshiba_acpi->kbd_led,
  1995. (toshiba_acpi->kbd_mode == SCI_KBD_MODE_ON) ?
  1996. LED_FULL : LED_OFF);
  1997. /* Emulate the keyboard backlight event */
  1998. acpi_bus_generate_netlink_event(toshiba_acpi->acpi_dev->pnp.device_class,
  1999. dev_name(&toshiba_acpi->acpi_dev->dev),
  2000. 0x92, 0);
  2001. }
  2002. /*
  2003. * IIO device
  2004. */
  2005. enum toshiba_iio_accel_chan {
  2006. AXIS_X,
  2007. AXIS_Y,
  2008. AXIS_Z
  2009. };
  2010. static int toshiba_iio_accel_get_axis(enum toshiba_iio_accel_chan chan)
  2011. {
  2012. u32 xyval, zval;
  2013. int ret;
  2014. ret = toshiba_accelerometer_get(toshiba_acpi, &xyval, &zval);
  2015. if (ret < 0)
  2016. return ret;
  2017. switch (chan) {
  2018. case AXIS_X:
  2019. return xyval & HCI_ACCEL_DIRECTION_MASK ?
  2020. -(xyval & HCI_ACCEL_MASK) : xyval & HCI_ACCEL_MASK;
  2021. case AXIS_Y:
  2022. return (xyval >> HCI_MISC_SHIFT) & HCI_ACCEL_DIRECTION_MASK ?
  2023. -((xyval >> HCI_MISC_SHIFT) & HCI_ACCEL_MASK) :
  2024. (xyval >> HCI_MISC_SHIFT) & HCI_ACCEL_MASK;
  2025. case AXIS_Z:
  2026. return zval & HCI_ACCEL_DIRECTION_MASK ?
  2027. -(zval & HCI_ACCEL_MASK) : zval & HCI_ACCEL_MASK;
  2028. }
  2029. return ret;
  2030. }
  2031. static int toshiba_iio_accel_read_raw(struct iio_dev *indio_dev,
  2032. struct iio_chan_spec const *chan,
  2033. int *val, int *val2, long mask)
  2034. {
  2035. int ret;
  2036. switch (mask) {
  2037. case IIO_CHAN_INFO_RAW:
  2038. ret = toshiba_iio_accel_get_axis(chan->channel);
  2039. if (ret == -EIO || ret == -ENODEV)
  2040. return ret;
  2041. *val = ret;
  2042. return IIO_VAL_INT;
  2043. }
  2044. return -EINVAL;
  2045. }
  2046. #define TOSHIBA_IIO_ACCEL_CHANNEL(axis, chan) { \
  2047. .type = IIO_ACCEL, \
  2048. .modified = 1, \
  2049. .channel = chan, \
  2050. .channel2 = IIO_MOD_##axis, \
  2051. .output = 1, \
  2052. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  2053. }
  2054. static const struct iio_chan_spec toshiba_iio_accel_channels[] = {
  2055. TOSHIBA_IIO_ACCEL_CHANNEL(X, AXIS_X),
  2056. TOSHIBA_IIO_ACCEL_CHANNEL(Y, AXIS_Y),
  2057. TOSHIBA_IIO_ACCEL_CHANNEL(Z, AXIS_Z),
  2058. };
  2059. static const struct iio_info toshiba_iio_accel_info = {
  2060. .read_raw = &toshiba_iio_accel_read_raw,
  2061. };
  2062. /*
  2063. * Misc device
  2064. */
  2065. static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
  2066. {
  2067. u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
  2068. regs->edx, regs->esi, regs->edi };
  2069. u32 out[TCI_WORDS];
  2070. acpi_status status;
  2071. status = tci_raw(toshiba_acpi, in, out);
  2072. if (ACPI_FAILURE(status)) {
  2073. pr_err("ACPI call to query SMM registers failed\n");
  2074. return -EIO;
  2075. }
  2076. /* Fillout the SMM struct with the TCI call results */
  2077. regs->eax = out[0];
  2078. regs->ebx = out[1];
  2079. regs->ecx = out[2];
  2080. regs->edx = out[3];
  2081. regs->esi = out[4];
  2082. regs->edi = out[5];
  2083. return 0;
  2084. }
  2085. static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
  2086. unsigned long arg)
  2087. {
  2088. SMMRegisters __user *argp = (SMMRegisters __user *)arg;
  2089. SMMRegisters regs;
  2090. int ret;
  2091. if (!argp)
  2092. return -EINVAL;
  2093. switch (cmd) {
  2094. case TOSH_SMM:
  2095. if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
  2096. return -EFAULT;
  2097. ret = toshiba_acpi_smm_bridge(&regs);
  2098. if (ret)
  2099. return ret;
  2100. if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
  2101. return -EFAULT;
  2102. break;
  2103. case TOSHIBA_ACPI_SCI:
  2104. if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
  2105. return -EFAULT;
  2106. /* Ensure we are being called with a SCI_{GET, SET} register */
  2107. if (regs.eax != SCI_GET && regs.eax != SCI_SET)
  2108. return -EINVAL;
  2109. if (!sci_open(toshiba_acpi))
  2110. return -EIO;
  2111. ret = toshiba_acpi_smm_bridge(&regs);
  2112. sci_close(toshiba_acpi);
  2113. if (ret)
  2114. return ret;
  2115. if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
  2116. return -EFAULT;
  2117. break;
  2118. default:
  2119. return -EINVAL;
  2120. }
  2121. return 0;
  2122. }
  2123. static const struct file_operations toshiba_acpi_fops = {
  2124. .owner = THIS_MODULE,
  2125. .unlocked_ioctl = toshiba_acpi_ioctl,
  2126. .llseek = noop_llseek,
  2127. };
  2128. /*
  2129. * WWAN RFKill handlers
  2130. */
  2131. static int toshiba_acpi_wwan_set_block(void *data, bool blocked)
  2132. {
  2133. struct toshiba_acpi_dev *dev = data;
  2134. int ret;
  2135. ret = toshiba_wireless_status(dev);
  2136. if (ret)
  2137. return ret;
  2138. if (!dev->killswitch)
  2139. return 0;
  2140. return toshiba_wwan_set(dev, !blocked);
  2141. }
  2142. static void toshiba_acpi_wwan_poll(struct rfkill *rfkill, void *data)
  2143. {
  2144. struct toshiba_acpi_dev *dev = data;
  2145. if (toshiba_wireless_status(dev))
  2146. return;
  2147. rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
  2148. }
  2149. static const struct rfkill_ops wwan_rfk_ops = {
  2150. .set_block = toshiba_acpi_wwan_set_block,
  2151. .poll = toshiba_acpi_wwan_poll,
  2152. };
  2153. static int toshiba_acpi_setup_wwan_rfkill(struct toshiba_acpi_dev *dev)
  2154. {
  2155. int ret = toshiba_wireless_status(dev);
  2156. if (ret)
  2157. return ret;
  2158. dev->wwan_rfk = rfkill_alloc("Toshiba WWAN",
  2159. &dev->acpi_dev->dev,
  2160. RFKILL_TYPE_WWAN,
  2161. &wwan_rfk_ops,
  2162. dev);
  2163. if (!dev->wwan_rfk) {
  2164. pr_err("Unable to allocate WWAN rfkill device\n");
  2165. return -ENOMEM;
  2166. }
  2167. rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
  2168. ret = rfkill_register(dev->wwan_rfk);
  2169. if (ret) {
  2170. pr_err("Unable to register WWAN rfkill device\n");
  2171. rfkill_destroy(dev->wwan_rfk);
  2172. }
  2173. return ret;
  2174. }
  2175. /*
  2176. * Hotkeys
  2177. */
  2178. static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
  2179. {
  2180. acpi_status status;
  2181. u32 result;
  2182. status = acpi_evaluate_object(dev->acpi_dev->handle,
  2183. "ENAB", NULL, NULL);
  2184. if (ACPI_FAILURE(status))
  2185. return -ENODEV;
  2186. /*
  2187. * Enable the "Special Functions" mode only if they are
  2188. * supported and if they are activated.
  2189. */
  2190. if (dev->kbd_function_keys_supported && dev->special_functions)
  2191. result = hci_write(dev, HCI_HOTKEY_EVENT,
  2192. HCI_HOTKEY_SPECIAL_FUNCTIONS);
  2193. else
  2194. result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
  2195. if (result == TOS_FAILURE)
  2196. return -EIO;
  2197. else if (result == TOS_NOT_SUPPORTED)
  2198. return -ENODEV;
  2199. return 0;
  2200. }
  2201. static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
  2202. struct serio *port)
  2203. {
  2204. if (str & I8042_STR_AUXDATA)
  2205. return false;
  2206. if (unlikely(data == 0xe0))
  2207. return false;
  2208. if ((data & 0x7f) == TOS1900_FN_SCAN) {
  2209. schedule_work(&toshiba_acpi->hotkey_work);
  2210. return true;
  2211. }
  2212. return false;
  2213. }
  2214. static void toshiba_acpi_hotkey_work(struct work_struct *work)
  2215. {
  2216. acpi_handle ec_handle = ec_get_handle();
  2217. acpi_status status;
  2218. if (!ec_handle)
  2219. return;
  2220. status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
  2221. if (ACPI_FAILURE(status))
  2222. pr_err("ACPI NTFY method execution failed\n");
  2223. }
  2224. /*
  2225. * Returns hotkey scancode, or < 0 on failure.
  2226. */
  2227. static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
  2228. {
  2229. unsigned long long value;
  2230. acpi_status status;
  2231. status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
  2232. NULL, &value);
  2233. if (ACPI_FAILURE(status)) {
  2234. pr_err("ACPI INFO method execution failed\n");
  2235. return -EIO;
  2236. }
  2237. return value;
  2238. }
  2239. static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
  2240. int scancode)
  2241. {
  2242. if (scancode == 0x100)
  2243. return;
  2244. /* Act on key press; ignore key release */
  2245. if (scancode & 0x80)
  2246. return;
  2247. if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
  2248. pr_info("Unknown key %x\n", scancode);
  2249. }
  2250. static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
  2251. {
  2252. if (dev->info_supported) {
  2253. int scancode = toshiba_acpi_query_hotkey(dev);
  2254. if (scancode < 0) {
  2255. pr_err("Failed to query hotkey event\n");
  2256. } else if (scancode != 0) {
  2257. toshiba_acpi_report_hotkey(dev, scancode);
  2258. dev->key_event_valid = 1;
  2259. dev->last_key_event = scancode;
  2260. }
  2261. } else if (dev->system_event_supported) {
  2262. u32 result;
  2263. u32 value;
  2264. int retries = 3;
  2265. do {
  2266. result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
  2267. switch (result) {
  2268. case TOS_SUCCESS:
  2269. toshiba_acpi_report_hotkey(dev, (int)value);
  2270. dev->key_event_valid = 1;
  2271. dev->last_key_event = value;
  2272. break;
  2273. case TOS_NOT_SUPPORTED:
  2274. /*
  2275. * This is a workaround for an unresolved
  2276. * issue on some machines where system events
  2277. * sporadically become disabled.
  2278. */
  2279. result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
  2280. if (result == TOS_SUCCESS)
  2281. pr_notice("Re-enabled hotkeys\n");
  2282. /* Fall through */
  2283. default:
  2284. retries--;
  2285. break;
  2286. }
  2287. } while (retries && result != TOS_FIFO_EMPTY);
  2288. }
  2289. }
  2290. static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
  2291. {
  2292. const struct key_entry *keymap = toshiba_acpi_keymap;
  2293. acpi_handle ec_handle;
  2294. int error;
  2295. if (disable_hotkeys) {
  2296. pr_info("Hotkeys disabled by module parameter\n");
  2297. return 0;
  2298. }
  2299. if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) {
  2300. pr_info("WMI event detected, hotkeys will not be monitored\n");
  2301. return 0;
  2302. }
  2303. error = toshiba_acpi_enable_hotkeys(dev);
  2304. if (error)
  2305. return error;
  2306. if (toshiba_hotkey_event_type_get(dev, &dev->hotkey_event_type))
  2307. pr_notice("Unable to query Hotkey Event Type\n");
  2308. dev->hotkey_dev = input_allocate_device();
  2309. if (!dev->hotkey_dev)
  2310. return -ENOMEM;
  2311. dev->hotkey_dev->name = "Toshiba input device";
  2312. dev->hotkey_dev->phys = "toshiba_acpi/input0";
  2313. dev->hotkey_dev->id.bustype = BUS_HOST;
  2314. if (dev->hotkey_event_type == HCI_SYSTEM_TYPE1 ||
  2315. !dev->kbd_function_keys_supported)
  2316. keymap = toshiba_acpi_keymap;
  2317. else if (dev->hotkey_event_type == HCI_SYSTEM_TYPE2 ||
  2318. dev->kbd_function_keys_supported)
  2319. keymap = toshiba_acpi_alt_keymap;
  2320. else
  2321. pr_info("Unknown event type received %x\n",
  2322. dev->hotkey_event_type);
  2323. error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
  2324. if (error)
  2325. goto err_free_dev;
  2326. /*
  2327. * For some machines the SCI responsible for providing hotkey
  2328. * notification doesn't fire. We can trigger the notification
  2329. * whenever the Fn key is pressed using the NTFY method, if
  2330. * supported, so if it's present set up an i8042 key filter
  2331. * for this purpose.
  2332. */
  2333. ec_handle = ec_get_handle();
  2334. if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
  2335. INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
  2336. error = i8042_install_filter(toshiba_acpi_i8042_filter);
  2337. if (error) {
  2338. pr_err("Error installing key filter\n");
  2339. goto err_free_dev;
  2340. }
  2341. dev->ntfy_supported = 1;
  2342. }
  2343. /*
  2344. * Determine hotkey query interface. Prefer using the INFO
  2345. * method when it is available.
  2346. */
  2347. if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
  2348. dev->info_supported = 1;
  2349. else if (hci_write(dev, HCI_SYSTEM_EVENT, 1) == TOS_SUCCESS)
  2350. dev->system_event_supported = 1;
  2351. if (!dev->info_supported && !dev->system_event_supported) {
  2352. pr_warn("No hotkey query interface found\n");
  2353. goto err_remove_filter;
  2354. }
  2355. error = input_register_device(dev->hotkey_dev);
  2356. if (error) {
  2357. pr_info("Unable to register input device\n");
  2358. goto err_remove_filter;
  2359. }
  2360. return 0;
  2361. err_remove_filter:
  2362. if (dev->ntfy_supported)
  2363. i8042_remove_filter(toshiba_acpi_i8042_filter);
  2364. err_free_dev:
  2365. input_free_device(dev->hotkey_dev);
  2366. dev->hotkey_dev = NULL;
  2367. return error;
  2368. }
  2369. static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
  2370. {
  2371. struct backlight_properties props;
  2372. int brightness;
  2373. int ret;
  2374. /*
  2375. * Some machines don't support the backlight methods at all, and
  2376. * others support it read-only. Either of these is pretty useless,
  2377. * so only register the backlight device if the backlight method
  2378. * supports both reads and writes.
  2379. */
  2380. brightness = __get_lcd_brightness(dev);
  2381. if (brightness < 0)
  2382. return 0;
  2383. /*
  2384. * If transflective backlight is supported and the brightness is zero
  2385. * (lowest brightness level), the set_lcd_brightness function will
  2386. * activate the transflective backlight, making the LCD appear to be
  2387. * turned off, simply increment the brightness level to avoid that.
  2388. */
  2389. if (dev->tr_backlight_supported && brightness == 0)
  2390. brightness++;
  2391. ret = set_lcd_brightness(dev, brightness);
  2392. if (ret) {
  2393. pr_debug("Backlight method is read-only, disabling backlight support\n");
  2394. return 0;
  2395. }
  2396. /*
  2397. * Tell acpi-video-detect code to prefer vendor backlight on all
  2398. * systems with transflective backlight and on dmi matched systems.
  2399. */
  2400. if (dev->tr_backlight_supported ||
  2401. dmi_check_system(toshiba_vendor_backlight_dmi))
  2402. acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
  2403. if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
  2404. return 0;
  2405. memset(&props, 0, sizeof(props));
  2406. props.type = BACKLIGHT_PLATFORM;
  2407. props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  2408. /* Adding an extra level and having 0 change to transflective mode */
  2409. if (dev->tr_backlight_supported)
  2410. props.max_brightness++;
  2411. dev->backlight_dev = backlight_device_register("toshiba",
  2412. &dev->acpi_dev->dev,
  2413. dev,
  2414. &toshiba_backlight_data,
  2415. &props);
  2416. if (IS_ERR(dev->backlight_dev)) {
  2417. ret = PTR_ERR(dev->backlight_dev);
  2418. pr_err("Could not register toshiba backlight device\n");
  2419. dev->backlight_dev = NULL;
  2420. return ret;
  2421. }
  2422. dev->backlight_dev->props.brightness = brightness;
  2423. return 0;
  2424. }
  2425. static void print_supported_features(struct toshiba_acpi_dev *dev)
  2426. {
  2427. pr_info("Supported laptop features:");
  2428. if (dev->hotkey_dev)
  2429. pr_cont(" hotkeys");
  2430. if (dev->backlight_dev)
  2431. pr_cont(" backlight");
  2432. if (dev->video_supported)
  2433. pr_cont(" video-out");
  2434. if (dev->fan_supported)
  2435. pr_cont(" fan");
  2436. if (dev->tr_backlight_supported)
  2437. pr_cont(" transflective-backlight");
  2438. if (dev->illumination_supported)
  2439. pr_cont(" illumination");
  2440. if (dev->kbd_illum_supported)
  2441. pr_cont(" keyboard-backlight");
  2442. if (dev->touchpad_supported)
  2443. pr_cont(" touchpad");
  2444. if (dev->eco_supported)
  2445. pr_cont(" eco-led");
  2446. if (dev->accelerometer_supported)
  2447. pr_cont(" accelerometer-axes");
  2448. if (dev->usb_sleep_charge_supported)
  2449. pr_cont(" usb-sleep-charge");
  2450. if (dev->usb_rapid_charge_supported)
  2451. pr_cont(" usb-rapid-charge");
  2452. if (dev->usb_sleep_music_supported)
  2453. pr_cont(" usb-sleep-music");
  2454. if (dev->kbd_function_keys_supported)
  2455. pr_cont(" special-function-keys");
  2456. if (dev->panel_power_on_supported)
  2457. pr_cont(" panel-power-on");
  2458. if (dev->usb_three_supported)
  2459. pr_cont(" usb3");
  2460. if (dev->wwan_supported)
  2461. pr_cont(" wwan");
  2462. if (dev->cooling_method_supported)
  2463. pr_cont(" cooling-method");
  2464. pr_cont("\n");
  2465. }
  2466. static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
  2467. {
  2468. struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
  2469. misc_deregister(&dev->miscdev);
  2470. remove_toshiba_proc_entries(dev);
  2471. if (dev->accelerometer_supported && dev->indio_dev) {
  2472. iio_device_unregister(dev->indio_dev);
  2473. iio_device_free(dev->indio_dev);
  2474. }
  2475. if (dev->sysfs_created)
  2476. sysfs_remove_group(&dev->acpi_dev->dev.kobj,
  2477. &toshiba_attr_group);
  2478. if (dev->ntfy_supported) {
  2479. i8042_remove_filter(toshiba_acpi_i8042_filter);
  2480. cancel_work_sync(&dev->hotkey_work);
  2481. }
  2482. if (dev->hotkey_dev)
  2483. input_unregister_device(dev->hotkey_dev);
  2484. backlight_device_unregister(dev->backlight_dev);
  2485. if (dev->illumination_led_registered)
  2486. led_classdev_unregister(&dev->led_dev);
  2487. if (dev->kbd_led_registered)
  2488. led_classdev_unregister(&dev->kbd_led);
  2489. if (dev->eco_led_registered)
  2490. led_classdev_unregister(&dev->eco_led);
  2491. if (dev->wwan_rfk) {
  2492. rfkill_unregister(dev->wwan_rfk);
  2493. rfkill_destroy(dev->wwan_rfk);
  2494. }
  2495. if (toshiba_acpi)
  2496. toshiba_acpi = NULL;
  2497. kfree(dev);
  2498. return 0;
  2499. }
  2500. static const char *find_hci_method(acpi_handle handle)
  2501. {
  2502. if (acpi_has_method(handle, "GHCI"))
  2503. return "GHCI";
  2504. if (acpi_has_method(handle, "SPFC"))
  2505. return "SPFC";
  2506. return NULL;
  2507. }
  2508. static int toshiba_acpi_add(struct acpi_device *acpi_dev)
  2509. {
  2510. struct toshiba_acpi_dev *dev;
  2511. const char *hci_method;
  2512. u32 dummy;
  2513. int ret = 0;
  2514. if (toshiba_acpi)
  2515. return -EBUSY;
  2516. pr_info("Toshiba Laptop ACPI Extras version %s\n",
  2517. TOSHIBA_ACPI_VERSION);
  2518. hci_method = find_hci_method(acpi_dev->handle);
  2519. if (!hci_method) {
  2520. pr_err("HCI interface not found\n");
  2521. return -ENODEV;
  2522. }
  2523. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2524. if (!dev)
  2525. return -ENOMEM;
  2526. dev->acpi_dev = acpi_dev;
  2527. dev->method_hci = hci_method;
  2528. dev->miscdev.minor = MISC_DYNAMIC_MINOR;
  2529. dev->miscdev.name = "toshiba_acpi";
  2530. dev->miscdev.fops = &toshiba_acpi_fops;
  2531. ret = misc_register(&dev->miscdev);
  2532. if (ret) {
  2533. pr_err("Failed to register miscdevice\n");
  2534. kfree(dev);
  2535. return ret;
  2536. }
  2537. acpi_dev->driver_data = dev;
  2538. dev_set_drvdata(&acpi_dev->dev, dev);
  2539. /* Query the BIOS for supported features */
  2540. /*
  2541. * The "Special Functions" are always supported by the laptops
  2542. * with the new keyboard layout, query for its presence to help
  2543. * determine the keymap layout to use.
  2544. */
  2545. ret = toshiba_function_keys_get(dev, &dev->special_functions);
  2546. dev->kbd_function_keys_supported = !ret;
  2547. dev->hotkey_event_type = 0;
  2548. if (toshiba_acpi_setup_keyboard(dev))
  2549. pr_info("Unable to activate hotkeys\n");
  2550. /* Determine whether or not BIOS supports transflective backlight */
  2551. ret = get_tr_backlight_status(dev, &dummy);
  2552. dev->tr_backlight_supported = !ret;
  2553. ret = toshiba_acpi_setup_backlight(dev);
  2554. if (ret)
  2555. goto error;
  2556. toshiba_illumination_available(dev);
  2557. if (dev->illumination_supported) {
  2558. dev->led_dev.name = "toshiba::illumination";
  2559. dev->led_dev.max_brightness = 1;
  2560. dev->led_dev.brightness_set = toshiba_illumination_set;
  2561. dev->led_dev.brightness_get = toshiba_illumination_get;
  2562. if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
  2563. dev->illumination_led_registered = true;
  2564. }
  2565. toshiba_eco_mode_available(dev);
  2566. if (dev->eco_supported) {
  2567. dev->eco_led.name = "toshiba::eco_mode";
  2568. dev->eco_led.max_brightness = 1;
  2569. dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
  2570. dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
  2571. if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
  2572. dev->eco_led_registered = true;
  2573. }
  2574. toshiba_kbd_illum_available(dev);
  2575. /*
  2576. * Only register the LED if KBD illumination is supported
  2577. * and the keyboard backlight operation mode is set to FN-Z
  2578. * or we detect a second gen keyboard backlight
  2579. */
  2580. if (dev->kbd_illum_supported &&
  2581. (dev->kbd_mode == SCI_KBD_MODE_FNZ || dev->kbd_type == 2)) {
  2582. dev->kbd_led.name = "toshiba::kbd_backlight";
  2583. dev->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
  2584. dev->kbd_led.max_brightness = 1;
  2585. dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
  2586. dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
  2587. if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
  2588. dev->kbd_led_registered = true;
  2589. }
  2590. ret = toshiba_touchpad_get(dev, &dummy);
  2591. dev->touchpad_supported = !ret;
  2592. toshiba_accelerometer_available(dev);
  2593. if (dev->accelerometer_supported) {
  2594. dev->indio_dev = iio_device_alloc(sizeof(*dev));
  2595. if (!dev->indio_dev) {
  2596. pr_err("Unable to allocate iio device\n");
  2597. goto iio_error;
  2598. }
  2599. pr_info("Registering Toshiba accelerometer iio device\n");
  2600. dev->indio_dev->info = &toshiba_iio_accel_info;
  2601. dev->indio_dev->name = "Toshiba accelerometer";
  2602. dev->indio_dev->dev.parent = &acpi_dev->dev;
  2603. dev->indio_dev->modes = INDIO_DIRECT_MODE;
  2604. dev->indio_dev->channels = toshiba_iio_accel_channels;
  2605. dev->indio_dev->num_channels =
  2606. ARRAY_SIZE(toshiba_iio_accel_channels);
  2607. ret = iio_device_register(dev->indio_dev);
  2608. if (ret < 0) {
  2609. pr_err("Unable to register iio device\n");
  2610. iio_device_free(dev->indio_dev);
  2611. }
  2612. }
  2613. iio_error:
  2614. toshiba_usb_sleep_charge_available(dev);
  2615. ret = toshiba_usb_rapid_charge_get(dev, &dummy);
  2616. dev->usb_rapid_charge_supported = !ret;
  2617. ret = toshiba_usb_sleep_music_get(dev, &dummy);
  2618. dev->usb_sleep_music_supported = !ret;
  2619. ret = toshiba_panel_power_on_get(dev, &dummy);
  2620. dev->panel_power_on_supported = !ret;
  2621. ret = toshiba_usb_three_get(dev, &dummy);
  2622. dev->usb_three_supported = !ret;
  2623. ret = get_video_status(dev, &dummy);
  2624. dev->video_supported = !ret;
  2625. ret = get_fan_status(dev, &dummy);
  2626. dev->fan_supported = !ret;
  2627. toshiba_wwan_available(dev);
  2628. if (dev->wwan_supported)
  2629. toshiba_acpi_setup_wwan_rfkill(dev);
  2630. toshiba_cooling_method_available(dev);
  2631. print_supported_features(dev);
  2632. ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
  2633. &toshiba_attr_group);
  2634. if (ret) {
  2635. dev->sysfs_created = 0;
  2636. goto error;
  2637. }
  2638. dev->sysfs_created = !ret;
  2639. create_toshiba_proc_entries(dev);
  2640. toshiba_acpi = dev;
  2641. return 0;
  2642. error:
  2643. toshiba_acpi_remove(acpi_dev);
  2644. return ret;
  2645. }
  2646. static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
  2647. {
  2648. struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
  2649. switch (event) {
  2650. case 0x80: /* Hotkeys and some system events */
  2651. /*
  2652. * Machines with this WMI GUID aren't supported due to bugs in
  2653. * their AML.
  2654. *
  2655. * Return silently to avoid triggering a netlink event.
  2656. */
  2657. if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
  2658. return;
  2659. toshiba_acpi_process_hotkeys(dev);
  2660. break;
  2661. case 0x81: /* Dock events */
  2662. case 0x82:
  2663. case 0x83:
  2664. pr_info("Dock event received %x\n", event);
  2665. break;
  2666. case 0x88: /* Thermal events */
  2667. pr_info("Thermal event received\n");
  2668. break;
  2669. case 0x8f: /* LID closed */
  2670. case 0x90: /* LID is closed and Dock has been ejected */
  2671. break;
  2672. case 0x8c: /* SATA power events */
  2673. case 0x8b:
  2674. pr_info("SATA power event received %x\n", event);
  2675. break;
  2676. case 0x92: /* Keyboard backlight mode changed */
  2677. dev->kbd_event_generated = true;
  2678. /* Update sysfs entries */
  2679. if (sysfs_update_group(&acpi_dev->dev.kobj,
  2680. &toshiba_attr_group))
  2681. pr_err("Unable to update sysfs entries\n");
  2682. /* Notify LED subsystem about keyboard backlight change */
  2683. if (dev->kbd_type == 2 && dev->kbd_mode != SCI_KBD_MODE_AUTO)
  2684. led_classdev_notify_brightness_hw_changed(&dev->kbd_led,
  2685. (dev->kbd_mode == SCI_KBD_MODE_ON) ?
  2686. LED_FULL : LED_OFF);
  2687. break;
  2688. case 0x85: /* Unknown */
  2689. case 0x8d: /* Unknown */
  2690. case 0x8e: /* Unknown */
  2691. case 0x94: /* Unknown */
  2692. case 0x95: /* Unknown */
  2693. default:
  2694. pr_info("Unknown event received %x\n", event);
  2695. break;
  2696. }
  2697. acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
  2698. dev_name(&acpi_dev->dev),
  2699. event, (event == 0x80) ?
  2700. dev->last_key_event : 0);
  2701. }
  2702. #ifdef CONFIG_PM_SLEEP
  2703. static int toshiba_acpi_suspend(struct device *device)
  2704. {
  2705. struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
  2706. if (dev->hotkey_dev) {
  2707. u32 result;
  2708. result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
  2709. if (result != TOS_SUCCESS)
  2710. pr_info("Unable to disable hotkeys\n");
  2711. }
  2712. return 0;
  2713. }
  2714. static int toshiba_acpi_resume(struct device *device)
  2715. {
  2716. struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
  2717. if (dev->hotkey_dev) {
  2718. if (toshiba_acpi_enable_hotkeys(dev))
  2719. pr_info("Unable to re-enable hotkeys\n");
  2720. }
  2721. if (dev->wwan_rfk) {
  2722. if (!toshiba_wireless_status(dev))
  2723. rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
  2724. }
  2725. return 0;
  2726. }
  2727. #endif
  2728. static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
  2729. toshiba_acpi_suspend, toshiba_acpi_resume);
  2730. static struct acpi_driver toshiba_acpi_driver = {
  2731. .name = "Toshiba ACPI driver",
  2732. .owner = THIS_MODULE,
  2733. .ids = toshiba_device_ids,
  2734. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  2735. .ops = {
  2736. .add = toshiba_acpi_add,
  2737. .remove = toshiba_acpi_remove,
  2738. .notify = toshiba_acpi_notify,
  2739. },
  2740. .drv.pm = &toshiba_acpi_pm,
  2741. };
  2742. static int __init toshiba_acpi_init(void)
  2743. {
  2744. int ret;
  2745. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  2746. if (!toshiba_proc_dir) {
  2747. pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
  2748. return -ENODEV;
  2749. }
  2750. ret = acpi_bus_register_driver(&toshiba_acpi_driver);
  2751. if (ret) {
  2752. pr_err("Failed to register ACPI driver: %d\n", ret);
  2753. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  2754. }
  2755. return ret;
  2756. }
  2757. static void __exit toshiba_acpi_exit(void)
  2758. {
  2759. acpi_bus_unregister_driver(&toshiba_acpi_driver);
  2760. if (toshiba_proc_dir)
  2761. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  2762. }
  2763. module_init(toshiba_acpi_init);
  2764. module_exit(toshiba_acpi_exit);