toshiba_acpi.c 70 KB

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