toshiba_acpi.c 74 KB

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