toshiba_acpi.c 72 KB

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