toshiba_acpi.c 72 KB

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