toshiba_acpi.c 81 KB

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