toshiba_acpi.c 82 KB

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