toshiba_acpi.c 72 KB

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