toshiba_acpi.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. /*
  2. * toshiba_acpi.c - Toshiba Laptop ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2002-2004 John Belmonte
  6. * Copyright (C) 2008 Philip Langdale
  7. * Copyright (C) 2010 Pierre Ducroquet
  8. * Copyright (C) 2014 Azael Avalos
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. *
  25. * The devolpment page for this driver is located at
  26. * http://memebeam.org/toys/ToshibaAcpiDriver.
  27. *
  28. * Credits:
  29. * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
  30. * engineering the Windows drivers
  31. * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
  32. * Rob Miller - TV out and hotkeys help
  33. *
  34. *
  35. * TODO
  36. *
  37. */
  38. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39. #define TOSHIBA_ACPI_VERSION "0.20"
  40. #define PROC_INTERFACE_VERSION 1
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/init.h>
  44. #include <linux/types.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/seq_file.h>
  47. #include <linux/backlight.h>
  48. #include <linux/rfkill.h>
  49. #include <linux/input.h>
  50. #include <linux/input/sparse-keymap.h>
  51. #include <linux/leds.h>
  52. #include <linux/slab.h>
  53. #include <linux/workqueue.h>
  54. #include <linux/i8042.h>
  55. #include <linux/acpi.h>
  56. #include <linux/dmi.h>
  57. #include <asm/uaccess.h>
  58. MODULE_AUTHOR("John Belmonte");
  59. MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
  60. MODULE_LICENSE("GPL");
  61. #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
  62. /* Scan code for Fn key on TOS1900 models */
  63. #define TOS1900_FN_SCAN 0x6e
  64. /* Toshiba ACPI method paths */
  65. #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
  66. /* The Toshiba configuration interface is composed of the HCI and the SCI,
  67. * which are defined as follows:
  68. *
  69. * HCI is Toshiba's "Hardware Control Interface" which is supposed to
  70. * be uniform across all their models. Ideally we would just call
  71. * dedicated ACPI methods instead of using this primitive interface.
  72. * However the ACPI methods seem to be incomplete in some areas (for
  73. * example they allow setting, but not reading, the LCD brightness value),
  74. * so this is still useful.
  75. *
  76. * SCI stands for "System Configuration Interface" which aim is to
  77. * conceal differences in hardware between different models.
  78. */
  79. #define TCI_WORDS 6
  80. /* operations */
  81. #define HCI_SET 0xff00
  82. #define HCI_GET 0xfe00
  83. #define SCI_OPEN 0xf100
  84. #define SCI_CLOSE 0xf200
  85. #define SCI_GET 0xf300
  86. #define SCI_SET 0xf400
  87. /* return codes */
  88. #define TOS_SUCCESS 0x0000
  89. #define TOS_OPEN_CLOSE_OK 0x0044
  90. #define TOS_FAILURE 0x1000
  91. #define TOS_NOT_SUPPORTED 0x8000
  92. #define TOS_ALREADY_OPEN 0x8100
  93. #define TOS_NOT_OPENED 0x8200
  94. #define TOS_INPUT_DATA_ERROR 0x8300
  95. #define TOS_WRITE_PROTECTED 0x8400
  96. #define TOS_NOT_PRESENT 0x8600
  97. #define TOS_FIFO_EMPTY 0x8c00
  98. #define TOS_DATA_NOT_AVAILABLE 0x8d20
  99. #define TOS_NOT_INITIALIZED 0x8d50
  100. /* registers */
  101. #define HCI_FAN 0x0004
  102. #define HCI_TR_BACKLIGHT 0x0005
  103. #define HCI_SYSTEM_EVENT 0x0016
  104. #define HCI_VIDEO_OUT 0x001c
  105. #define HCI_HOTKEY_EVENT 0x001e
  106. #define HCI_LCD_BRIGHTNESS 0x002a
  107. #define HCI_WIRELESS 0x0056
  108. #define HCI_ACCELEROMETER 0x006d
  109. #define HCI_KBD_ILLUMINATION 0x0095
  110. #define HCI_ECO_MODE 0x0097
  111. #define HCI_ACCELEROMETER2 0x00a6
  112. #define SCI_ILLUMINATION 0x014e
  113. #define SCI_KBD_ILLUM_STATUS 0x015c
  114. #define SCI_TOUCHPAD 0x050e
  115. /* field definitions */
  116. #define HCI_ACCEL_MASK 0x7fff
  117. #define HCI_HOTKEY_DISABLE 0x0b
  118. #define HCI_HOTKEY_ENABLE 0x09
  119. #define HCI_LCD_BRIGHTNESS_BITS 3
  120. #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
  121. #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
  122. #define HCI_MISC_SHIFT 0x10
  123. #define HCI_VIDEO_OUT_LCD 0x1
  124. #define HCI_VIDEO_OUT_CRT 0x2
  125. #define HCI_VIDEO_OUT_TV 0x4
  126. #define HCI_WIRELESS_KILL_SWITCH 0x01
  127. #define HCI_WIRELESS_BT_PRESENT 0x0f
  128. #define HCI_WIRELESS_BT_ATTACH 0x40
  129. #define HCI_WIRELESS_BT_POWER 0x80
  130. #define SCI_KBD_MODE_MASK 0x1f
  131. #define SCI_KBD_MODE_FNZ 0x1
  132. #define SCI_KBD_MODE_AUTO 0x2
  133. #define SCI_KBD_MODE_ON 0x8
  134. #define SCI_KBD_MODE_OFF 0x10
  135. #define SCI_KBD_TIME_MAX 0x3c001a
  136. struct toshiba_acpi_dev {
  137. struct acpi_device *acpi_dev;
  138. const char *method_hci;
  139. struct rfkill *bt_rfk;
  140. struct input_dev *hotkey_dev;
  141. struct work_struct hotkey_work;
  142. struct backlight_device *backlight_dev;
  143. struct led_classdev led_dev;
  144. struct led_classdev kbd_led;
  145. struct led_classdev eco_led;
  146. int force_fan;
  147. int last_key_event;
  148. int key_event_valid;
  149. int kbd_type;
  150. int kbd_mode;
  151. int kbd_time;
  152. unsigned int illumination_supported:1;
  153. unsigned int video_supported:1;
  154. unsigned int fan_supported:1;
  155. unsigned int system_event_supported:1;
  156. unsigned int ntfy_supported:1;
  157. unsigned int info_supported:1;
  158. unsigned int tr_backlight_supported:1;
  159. unsigned int kbd_illum_supported:1;
  160. unsigned int kbd_led_registered:1;
  161. unsigned int touchpad_supported:1;
  162. unsigned int eco_supported:1;
  163. unsigned int accelerometer_supported:1;
  164. unsigned int sysfs_created:1;
  165. struct mutex mutex;
  166. };
  167. static struct toshiba_acpi_dev *toshiba_acpi;
  168. static const struct acpi_device_id toshiba_device_ids[] = {
  169. {"TOS6200", 0},
  170. {"TOS6208", 0},
  171. {"TOS1900", 0},
  172. {"", 0},
  173. };
  174. MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
  175. static const struct key_entry toshiba_acpi_keymap[] = {
  176. { KE_KEY, 0x9e, { KEY_RFKILL } },
  177. { KE_KEY, 0x101, { KEY_MUTE } },
  178. { KE_KEY, 0x102, { KEY_ZOOMOUT } },
  179. { KE_KEY, 0x103, { KEY_ZOOMIN } },
  180. { KE_KEY, 0x10f, { KEY_TAB } },
  181. { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
  182. { KE_KEY, 0x139, { KEY_ZOOMRESET } },
  183. { KE_KEY, 0x13b, { KEY_COFFEE } },
  184. { KE_KEY, 0x13c, { KEY_BATTERY } },
  185. { KE_KEY, 0x13d, { KEY_SLEEP } },
  186. { KE_KEY, 0x13e, { KEY_SUSPEND } },
  187. { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
  188. { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
  189. { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
  190. { KE_KEY, 0x142, { KEY_WLAN } },
  191. { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
  192. { KE_KEY, 0x17f, { KEY_FN } },
  193. { KE_KEY, 0xb05, { KEY_PROG2 } },
  194. { KE_KEY, 0xb06, { KEY_WWW } },
  195. { KE_KEY, 0xb07, { KEY_MAIL } },
  196. { KE_KEY, 0xb30, { KEY_STOP } },
  197. { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
  198. { KE_KEY, 0xb32, { KEY_NEXTSONG } },
  199. { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
  200. { KE_KEY, 0xb5a, { KEY_MEDIA } },
  201. { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
  202. { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
  203. { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
  204. { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
  205. { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
  206. { KE_END, 0 },
  207. };
  208. /* alternative keymap */
  209. static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
  210. {
  211. .matches = {
  212. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  213. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
  214. },
  215. },
  216. {
  217. .matches = {
  218. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  219. DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
  220. },
  221. },
  222. {
  223. .matches = {
  224. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  225. DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A50-A"),
  226. },
  227. },
  228. {}
  229. };
  230. static const struct key_entry toshiba_acpi_alt_keymap[] = {
  231. { KE_KEY, 0x157, { KEY_MUTE } },
  232. { KE_KEY, 0x102, { KEY_ZOOMOUT } },
  233. { KE_KEY, 0x103, { KEY_ZOOMIN } },
  234. { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
  235. { KE_KEY, 0x139, { KEY_ZOOMRESET } },
  236. { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
  237. { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
  238. { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
  239. { KE_KEY, 0x158, { KEY_WLAN } },
  240. { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
  241. { KE_END, 0 },
  242. };
  243. /* utility
  244. */
  245. static __inline__ void _set_bit(u32 * word, u32 mask, int value)
  246. {
  247. *word = (*word & ~mask) | (mask * value);
  248. }
  249. /* acpi interface wrappers
  250. */
  251. static int write_acpi_int(const char *methodName, int val)
  252. {
  253. acpi_status status;
  254. status = acpi_execute_simple_method(NULL, (char *)methodName, val);
  255. return (status == AE_OK) ? 0 : -EIO;
  256. }
  257. /* Perform a raw configuration call. Here we don't care about input or output
  258. * buffer format.
  259. */
  260. static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
  261. const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
  262. {
  263. struct acpi_object_list params;
  264. union acpi_object in_objs[TCI_WORDS];
  265. struct acpi_buffer results;
  266. union acpi_object out_objs[TCI_WORDS + 1];
  267. acpi_status status;
  268. int i;
  269. params.count = TCI_WORDS;
  270. params.pointer = in_objs;
  271. for (i = 0; i < TCI_WORDS; ++i) {
  272. in_objs[i].type = ACPI_TYPE_INTEGER;
  273. in_objs[i].integer.value = in[i];
  274. }
  275. results.length = sizeof(out_objs);
  276. results.pointer = out_objs;
  277. status = acpi_evaluate_object(dev->acpi_dev->handle,
  278. (char *)dev->method_hci, &params,
  279. &results);
  280. if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
  281. for (i = 0; i < out_objs->package.count; ++i) {
  282. out[i] = out_objs->package.elements[i].integer.value;
  283. }
  284. }
  285. return status;
  286. }
  287. /* common hci tasks (get or set one or two value)
  288. *
  289. * In addition to the ACPI status, the HCI system returns a result which
  290. * may be useful (such as "not supported").
  291. */
  292. static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
  293. {
  294. u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
  295. u32 out[TCI_WORDS];
  296. acpi_status status = tci_raw(dev, in, out);
  297. return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
  298. }
  299. static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
  300. {
  301. u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
  302. u32 out[TCI_WORDS];
  303. acpi_status status = tci_raw(dev, in, out);
  304. if (ACPI_FAILURE(status))
  305. return TOS_FAILURE;
  306. *out1 = out[2];
  307. return out[0];
  308. }
  309. static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
  310. {
  311. u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
  312. u32 out[TCI_WORDS];
  313. acpi_status status = tci_raw(dev, in, out);
  314. return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
  315. }
  316. static u32 hci_read2(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1, u32 *out2)
  317. {
  318. u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
  319. u32 out[TCI_WORDS];
  320. acpi_status status = tci_raw(dev, in, out);
  321. if (ACPI_FAILURE(status))
  322. return TOS_FAILURE;
  323. *out1 = out[2];
  324. *out2 = out[3];
  325. return out[0];
  326. }
  327. /* common sci tasks
  328. */
  329. static int sci_open(struct toshiba_acpi_dev *dev)
  330. {
  331. u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
  332. u32 out[TCI_WORDS];
  333. acpi_status status;
  334. status = tci_raw(dev, in, out);
  335. if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
  336. pr_err("ACPI call to open SCI failed\n");
  337. return 0;
  338. }
  339. if (out[0] == TOS_OPEN_CLOSE_OK) {
  340. return 1;
  341. } else if (out[0] == TOS_ALREADY_OPEN) {
  342. pr_info("Toshiba SCI already opened\n");
  343. return 1;
  344. } else if (out[0] == TOS_NOT_PRESENT) {
  345. pr_info("Toshiba SCI is not present\n");
  346. }
  347. return 0;
  348. }
  349. static void sci_close(struct toshiba_acpi_dev *dev)
  350. {
  351. u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
  352. u32 out[TCI_WORDS];
  353. acpi_status status;
  354. status = tci_raw(dev, in, out);
  355. if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
  356. pr_err("ACPI call to close SCI failed\n");
  357. return;
  358. }
  359. if (out[0] == TOS_OPEN_CLOSE_OK)
  360. return;
  361. else if (out[0] == TOS_NOT_OPENED)
  362. pr_info("Toshiba SCI not opened\n");
  363. else if (out[0] == TOS_NOT_PRESENT)
  364. pr_info("Toshiba SCI is not present\n");
  365. }
  366. static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
  367. {
  368. u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
  369. u32 out[TCI_WORDS];
  370. acpi_status status = tci_raw(dev, in, out);
  371. if (ACPI_FAILURE(status))
  372. return TOS_FAILURE;
  373. *out1 = out[2];
  374. return out[0];
  375. }
  376. static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
  377. {
  378. u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
  379. u32 out[TCI_WORDS];
  380. acpi_status status = tci_raw(dev, in, out);
  381. return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
  382. }
  383. /* Illumination support */
  384. static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
  385. {
  386. u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
  387. u32 out[TCI_WORDS];
  388. acpi_status status;
  389. if (!sci_open(dev))
  390. return 0;
  391. status = tci_raw(dev, in, out);
  392. sci_close(dev);
  393. if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
  394. pr_err("ACPI call to query Illumination support failed\n");
  395. return 0;
  396. } else if (out[0] == TOS_NOT_SUPPORTED) {
  397. pr_info("Illumination device not available\n");
  398. return 0;
  399. }
  400. return 1;
  401. }
  402. static void toshiba_illumination_set(struct led_classdev *cdev,
  403. enum led_brightness brightness)
  404. {
  405. struct toshiba_acpi_dev *dev = container_of(cdev,
  406. struct toshiba_acpi_dev, led_dev);
  407. u32 state, result;
  408. /* First request : initialize communication. */
  409. if (!sci_open(dev))
  410. return;
  411. /* Switch the illumination on/off */
  412. state = brightness ? 1 : 0;
  413. result = sci_write(dev, SCI_ILLUMINATION, state);
  414. sci_close(dev);
  415. if (result == TOS_FAILURE) {
  416. pr_err("ACPI call for illumination failed\n");
  417. return;
  418. } else if (result == TOS_NOT_SUPPORTED) {
  419. pr_info("Illumination not supported\n");
  420. return;
  421. }
  422. }
  423. static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
  424. {
  425. struct toshiba_acpi_dev *dev = container_of(cdev,
  426. struct toshiba_acpi_dev, led_dev);
  427. u32 state, result;
  428. /* First request : initialize communication. */
  429. if (!sci_open(dev))
  430. return LED_OFF;
  431. /* Check the illumination */
  432. result = sci_read(dev, SCI_ILLUMINATION, &state);
  433. sci_close(dev);
  434. if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
  435. pr_err("ACPI call for illumination failed\n");
  436. return LED_OFF;
  437. } else if (result == TOS_NOT_SUPPORTED) {
  438. pr_info("Illumination not supported\n");
  439. return LED_OFF;
  440. }
  441. return state ? LED_FULL : LED_OFF;
  442. }
  443. /* KBD Illumination */
  444. static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
  445. {
  446. u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
  447. u32 out[TCI_WORDS];
  448. acpi_status status;
  449. if (!sci_open(dev))
  450. return 0;
  451. status = tci_raw(dev, in, out);
  452. sci_close(dev);
  453. if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
  454. pr_err("ACPI call to query kbd illumination support failed\n");
  455. return 0;
  456. } else if (out[0] == TOS_NOT_SUPPORTED) {
  457. pr_info("Keyboard illumination not available\n");
  458. return 0;
  459. }
  460. /* Check for keyboard backlight timeout max value,
  461. * previous kbd backlight implementation set this to
  462. * 0x3c0003, and now the new implementation set this
  463. * to 0x3c001a, use this to distinguish between them
  464. */
  465. if (out[3] == SCI_KBD_TIME_MAX)
  466. dev->kbd_type = 2;
  467. else
  468. dev->kbd_type = 1;
  469. /* Get the current keyboard backlight mode */
  470. dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
  471. /* Get the current time (1-60 seconds) */
  472. dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
  473. return 1;
  474. }
  475. static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
  476. {
  477. u32 result;
  478. if (!sci_open(dev))
  479. return -EIO;
  480. result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
  481. sci_close(dev);
  482. if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
  483. pr_err("ACPI call to set KBD backlight status failed\n");
  484. return -EIO;
  485. } else if (result == TOS_NOT_SUPPORTED) {
  486. pr_info("Keyboard backlight status not supported\n");
  487. return -ENODEV;
  488. }
  489. return 0;
  490. }
  491. static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
  492. {
  493. u32 result;
  494. if (!sci_open(dev))
  495. return -EIO;
  496. result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
  497. sci_close(dev);
  498. if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
  499. pr_err("ACPI call to get KBD backlight status failed\n");
  500. return -EIO;
  501. } else if (result == TOS_NOT_SUPPORTED) {
  502. pr_info("Keyboard backlight status not supported\n");
  503. return -ENODEV;
  504. }
  505. return 0;
  506. }
  507. static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
  508. {
  509. struct toshiba_acpi_dev *dev = container_of(cdev,
  510. struct toshiba_acpi_dev, kbd_led);
  511. u32 state, result;
  512. /* Check the keyboard backlight state */
  513. result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
  514. if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
  515. pr_err("ACPI call to get the keyboard backlight failed\n");
  516. return LED_OFF;
  517. } else if (result == TOS_NOT_SUPPORTED) {
  518. pr_info("Keyboard backlight not supported\n");
  519. return LED_OFF;
  520. }
  521. return state ? LED_FULL : LED_OFF;
  522. }
  523. static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
  524. enum led_brightness brightness)
  525. {
  526. struct toshiba_acpi_dev *dev = container_of(cdev,
  527. struct toshiba_acpi_dev, kbd_led);
  528. u32 state, result;
  529. /* Set the keyboard backlight state */
  530. state = brightness ? 1 : 0;
  531. result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
  532. if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
  533. pr_err("ACPI call to set KBD Illumination mode failed\n");
  534. return;
  535. } else if (result == TOS_NOT_SUPPORTED) {
  536. pr_info("Keyboard backlight not supported\n");
  537. return;
  538. }
  539. }
  540. /* TouchPad support */
  541. static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
  542. {
  543. u32 result;
  544. if (!sci_open(dev))
  545. return -EIO;
  546. result = sci_write(dev, SCI_TOUCHPAD, state);
  547. sci_close(dev);
  548. if (result == TOS_FAILURE) {
  549. pr_err("ACPI call to set the touchpad failed\n");
  550. return -EIO;
  551. } else if (result == TOS_NOT_SUPPORTED) {
  552. return -ENODEV;
  553. }
  554. return 0;
  555. }
  556. static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
  557. {
  558. u32 result;
  559. if (!sci_open(dev))
  560. return -EIO;
  561. result = sci_read(dev, SCI_TOUCHPAD, state);
  562. sci_close(dev);
  563. if (result == TOS_FAILURE) {
  564. pr_err("ACPI call to query the touchpad failed\n");
  565. return -EIO;
  566. } else if (result == TOS_NOT_SUPPORTED) {
  567. return -ENODEV;
  568. }
  569. return 0;
  570. }
  571. /* Eco Mode support */
  572. static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
  573. {
  574. acpi_status status;
  575. u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
  576. u32 out[TCI_WORDS];
  577. status = tci_raw(dev, in, out);
  578. if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
  579. pr_info("ACPI call to get ECO led failed\n");
  580. return 0;
  581. }
  582. return 1;
  583. }
  584. static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
  585. {
  586. struct toshiba_acpi_dev *dev = container_of(cdev,
  587. struct toshiba_acpi_dev, eco_led);
  588. u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
  589. u32 out[TCI_WORDS];
  590. acpi_status status;
  591. status = tci_raw(dev, in, out);
  592. if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
  593. pr_err("ACPI call to get ECO led failed\n");
  594. return LED_OFF;
  595. }
  596. return out[2] ? LED_FULL : LED_OFF;
  597. }
  598. static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
  599. enum led_brightness brightness)
  600. {
  601. struct toshiba_acpi_dev *dev = container_of(cdev,
  602. struct toshiba_acpi_dev, eco_led);
  603. u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
  604. u32 out[TCI_WORDS];
  605. acpi_status status;
  606. /* Switch the Eco Mode led on/off */
  607. in[2] = (brightness) ? 1 : 0;
  608. status = tci_raw(dev, in, out);
  609. if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
  610. pr_err("ACPI call to set ECO led failed\n");
  611. return;
  612. }
  613. }
  614. /* Accelerometer support */
  615. static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
  616. {
  617. u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
  618. u32 out[TCI_WORDS];
  619. acpi_status status;
  620. /* Check if the accelerometer call exists,
  621. * this call also serves as initialization
  622. */
  623. status = tci_raw(dev, in, out);
  624. if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
  625. pr_err("ACPI call to query the accelerometer failed\n");
  626. return -EIO;
  627. } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
  628. out[0] == TOS_NOT_INITIALIZED) {
  629. pr_err("Accelerometer not initialized\n");
  630. return -EIO;
  631. } else if (out[0] == TOS_NOT_SUPPORTED) {
  632. pr_info("Accelerometer not supported\n");
  633. return -ENODEV;
  634. }
  635. return 0;
  636. }
  637. static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
  638. u32 *xy, u32 *z)
  639. {
  640. u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
  641. u32 out[TCI_WORDS];
  642. acpi_status status;
  643. /* Check the Accelerometer status */
  644. status = tci_raw(dev, in, out);
  645. if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
  646. pr_err("ACPI call to query the accelerometer failed\n");
  647. return -EIO;
  648. }
  649. *xy = out[2];
  650. *z = out[4];
  651. return 0;
  652. }
  653. /* Bluetooth rfkill handlers */
  654. static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
  655. {
  656. u32 hci_result;
  657. u32 value, value2;
  658. value = 0;
  659. value2 = 0;
  660. hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
  661. if (hci_result == TOS_SUCCESS)
  662. *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
  663. return hci_result;
  664. }
  665. static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
  666. {
  667. u32 hci_result;
  668. u32 value, value2;
  669. value = 0;
  670. value2 = 0x0001;
  671. hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
  672. *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
  673. return hci_result;
  674. }
  675. static int bt_rfkill_set_block(void *data, bool blocked)
  676. {
  677. struct toshiba_acpi_dev *dev = data;
  678. u32 result1, result2;
  679. u32 value;
  680. int err;
  681. bool radio_state;
  682. value = (blocked == false);
  683. mutex_lock(&dev->mutex);
  684. if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
  685. err = -EIO;
  686. goto out;
  687. }
  688. if (!radio_state) {
  689. err = 0;
  690. goto out;
  691. }
  692. result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
  693. result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
  694. if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
  695. err = -EIO;
  696. else
  697. err = 0;
  698. out:
  699. mutex_unlock(&dev->mutex);
  700. return err;
  701. }
  702. static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
  703. {
  704. bool new_rfk_state;
  705. bool value;
  706. u32 hci_result;
  707. struct toshiba_acpi_dev *dev = data;
  708. mutex_lock(&dev->mutex);
  709. hci_result = hci_get_radio_state(dev, &value);
  710. if (hci_result != TOS_SUCCESS) {
  711. /* Can't do anything useful */
  712. mutex_unlock(&dev->mutex);
  713. return;
  714. }
  715. new_rfk_state = value;
  716. mutex_unlock(&dev->mutex);
  717. if (rfkill_set_hw_state(rfkill, !new_rfk_state))
  718. bt_rfkill_set_block(data, true);
  719. }
  720. static const struct rfkill_ops toshiba_rfk_ops = {
  721. .set_block = bt_rfkill_set_block,
  722. .poll = bt_rfkill_poll,
  723. };
  724. static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
  725. {
  726. u32 hci_result;
  727. u32 status;
  728. hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
  729. *enabled = !status;
  730. return hci_result == TOS_SUCCESS ? 0 : -EIO;
  731. }
  732. static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
  733. {
  734. u32 hci_result;
  735. u32 value = !enable;
  736. hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
  737. return hci_result == TOS_SUCCESS ? 0 : -EIO;
  738. }
  739. static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
  740. static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
  741. {
  742. u32 hci_result;
  743. u32 value;
  744. int brightness = 0;
  745. if (dev->tr_backlight_supported) {
  746. bool enabled;
  747. int ret = get_tr_backlight_status(dev, &enabled);
  748. if (ret)
  749. return ret;
  750. if (enabled)
  751. return 0;
  752. brightness++;
  753. }
  754. hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
  755. if (hci_result == TOS_SUCCESS)
  756. return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
  757. return -EIO;
  758. }
  759. static int get_lcd_brightness(struct backlight_device *bd)
  760. {
  761. struct toshiba_acpi_dev *dev = bl_get_data(bd);
  762. return __get_lcd_brightness(dev);
  763. }
  764. static int lcd_proc_show(struct seq_file *m, void *v)
  765. {
  766. struct toshiba_acpi_dev *dev = m->private;
  767. int value;
  768. int levels;
  769. if (!dev->backlight_dev)
  770. return -ENODEV;
  771. levels = dev->backlight_dev->props.max_brightness + 1;
  772. value = get_lcd_brightness(dev->backlight_dev);
  773. if (value >= 0) {
  774. seq_printf(m, "brightness: %d\n", value);
  775. seq_printf(m, "brightness_levels: %d\n", levels);
  776. return 0;
  777. }
  778. pr_err("Error reading LCD brightness\n");
  779. return -EIO;
  780. }
  781. static int lcd_proc_open(struct inode *inode, struct file *file)
  782. {
  783. return single_open(file, lcd_proc_show, PDE_DATA(inode));
  784. }
  785. static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
  786. {
  787. u32 in[TCI_WORDS] = { HCI_SET, HCI_LCD_BRIGHTNESS, 0, 0, 0, 0 };
  788. u32 out[TCI_WORDS];
  789. acpi_status status;
  790. if (dev->tr_backlight_supported) {
  791. bool enable = !value;
  792. int ret = set_tr_backlight_status(dev, enable);
  793. if (ret)
  794. return ret;
  795. if (value)
  796. value--;
  797. }
  798. in[2] = value << HCI_LCD_BRIGHTNESS_SHIFT;
  799. status = tci_raw(dev, in, out);
  800. if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
  801. pr_err("ACPI call to set brightness failed");
  802. return -EIO;
  803. }
  804. /* Extra check for "incomplete" backlight method, where the AML code
  805. * doesn't check for HCI_SET or HCI_GET and returns TOS_SUCCESS,
  806. * the actual brightness, and in some cases the max brightness.
  807. */
  808. if (out[2] > 0 || out[3] == 0xE000)
  809. return -ENODEV;
  810. return out[0] == TOS_SUCCESS ? 0 : -EIO;
  811. }
  812. static int set_lcd_status(struct backlight_device *bd)
  813. {
  814. struct toshiba_acpi_dev *dev = bl_get_data(bd);
  815. return set_lcd_brightness(dev, bd->props.brightness);
  816. }
  817. static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
  818. size_t count, loff_t *pos)
  819. {
  820. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  821. char cmd[42];
  822. size_t len;
  823. int value;
  824. int ret;
  825. int levels = dev->backlight_dev->props.max_brightness + 1;
  826. len = min(count, sizeof(cmd) - 1);
  827. if (copy_from_user(cmd, buf, len))
  828. return -EFAULT;
  829. cmd[len] = '\0';
  830. if (sscanf(cmd, " brightness : %i", &value) == 1 &&
  831. value >= 0 && value < levels) {
  832. ret = set_lcd_brightness(dev, value);
  833. if (ret == 0)
  834. ret = count;
  835. } else {
  836. ret = -EINVAL;
  837. }
  838. return ret;
  839. }
  840. static const struct file_operations lcd_proc_fops = {
  841. .owner = THIS_MODULE,
  842. .open = lcd_proc_open,
  843. .read = seq_read,
  844. .llseek = seq_lseek,
  845. .release = single_release,
  846. .write = lcd_proc_write,
  847. };
  848. static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
  849. {
  850. u32 hci_result;
  851. hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
  852. return hci_result == TOS_SUCCESS ? 0 : -EIO;
  853. }
  854. static int video_proc_show(struct seq_file *m, void *v)
  855. {
  856. struct toshiba_acpi_dev *dev = m->private;
  857. u32 value;
  858. int ret;
  859. ret = get_video_status(dev, &value);
  860. if (!ret) {
  861. int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
  862. int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
  863. int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
  864. seq_printf(m, "lcd_out: %d\n", is_lcd);
  865. seq_printf(m, "crt_out: %d\n", is_crt);
  866. seq_printf(m, "tv_out: %d\n", is_tv);
  867. }
  868. return ret;
  869. }
  870. static int video_proc_open(struct inode *inode, struct file *file)
  871. {
  872. return single_open(file, video_proc_show, PDE_DATA(inode));
  873. }
  874. static ssize_t video_proc_write(struct file *file, const char __user *buf,
  875. size_t count, loff_t *pos)
  876. {
  877. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  878. char *cmd, *buffer;
  879. int ret;
  880. int value;
  881. int remain = count;
  882. int lcd_out = -1;
  883. int crt_out = -1;
  884. int tv_out = -1;
  885. u32 video_out;
  886. cmd = kmalloc(count + 1, GFP_KERNEL);
  887. if (!cmd)
  888. return -ENOMEM;
  889. if (copy_from_user(cmd, buf, count)) {
  890. kfree(cmd);
  891. return -EFAULT;
  892. }
  893. cmd[count] = '\0';
  894. buffer = cmd;
  895. /* scan expression. Multiple expressions may be delimited with ;
  896. *
  897. * NOTE: to keep scanning simple, invalid fields are ignored
  898. */
  899. while (remain) {
  900. if (sscanf(buffer, " lcd_out : %i", &value) == 1)
  901. lcd_out = value & 1;
  902. else if (sscanf(buffer, " crt_out : %i", &value) == 1)
  903. crt_out = value & 1;
  904. else if (sscanf(buffer, " tv_out : %i", &value) == 1)
  905. tv_out = value & 1;
  906. /* advance to one character past the next ; */
  907. do {
  908. ++buffer;
  909. --remain;
  910. }
  911. while (remain && *(buffer - 1) != ';');
  912. }
  913. kfree(cmd);
  914. ret = get_video_status(dev, &video_out);
  915. if (!ret) {
  916. unsigned int new_video_out = video_out;
  917. if (lcd_out != -1)
  918. _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
  919. if (crt_out != -1)
  920. _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
  921. if (tv_out != -1)
  922. _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
  923. /* To avoid unnecessary video disruption, only write the new
  924. * video setting if something changed. */
  925. if (new_video_out != video_out)
  926. ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
  927. }
  928. return ret ? ret : count;
  929. }
  930. static const struct file_operations video_proc_fops = {
  931. .owner = THIS_MODULE,
  932. .open = video_proc_open,
  933. .read = seq_read,
  934. .llseek = seq_lseek,
  935. .release = single_release,
  936. .write = video_proc_write,
  937. };
  938. static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
  939. {
  940. u32 hci_result;
  941. hci_result = hci_read1(dev, HCI_FAN, status);
  942. return hci_result == TOS_SUCCESS ? 0 : -EIO;
  943. }
  944. static int fan_proc_show(struct seq_file *m, void *v)
  945. {
  946. struct toshiba_acpi_dev *dev = m->private;
  947. int ret;
  948. u32 value;
  949. ret = get_fan_status(dev, &value);
  950. if (!ret) {
  951. seq_printf(m, "running: %d\n", (value > 0));
  952. seq_printf(m, "force_on: %d\n", dev->force_fan);
  953. }
  954. return ret;
  955. }
  956. static int fan_proc_open(struct inode *inode, struct file *file)
  957. {
  958. return single_open(file, fan_proc_show, PDE_DATA(inode));
  959. }
  960. static ssize_t fan_proc_write(struct file *file, const char __user *buf,
  961. size_t count, loff_t *pos)
  962. {
  963. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  964. char cmd[42];
  965. size_t len;
  966. int value;
  967. u32 hci_result;
  968. len = min(count, sizeof(cmd) - 1);
  969. if (copy_from_user(cmd, buf, len))
  970. return -EFAULT;
  971. cmd[len] = '\0';
  972. if (sscanf(cmd, " force_on : %i", &value) == 1 &&
  973. value >= 0 && value <= 1) {
  974. hci_result = hci_write1(dev, HCI_FAN, value);
  975. if (hci_result != TOS_SUCCESS)
  976. return -EIO;
  977. else
  978. dev->force_fan = value;
  979. } else {
  980. return -EINVAL;
  981. }
  982. return count;
  983. }
  984. static const struct file_operations fan_proc_fops = {
  985. .owner = THIS_MODULE,
  986. .open = fan_proc_open,
  987. .read = seq_read,
  988. .llseek = seq_lseek,
  989. .release = single_release,
  990. .write = fan_proc_write,
  991. };
  992. static int keys_proc_show(struct seq_file *m, void *v)
  993. {
  994. struct toshiba_acpi_dev *dev = m->private;
  995. u32 hci_result;
  996. u32 value;
  997. if (!dev->key_event_valid && dev->system_event_supported) {
  998. hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
  999. if (hci_result == TOS_SUCCESS) {
  1000. dev->key_event_valid = 1;
  1001. dev->last_key_event = value;
  1002. } else if (hci_result == TOS_FIFO_EMPTY) {
  1003. /* better luck next time */
  1004. } else if (hci_result == TOS_NOT_SUPPORTED) {
  1005. /* This is a workaround for an unresolved issue on
  1006. * some machines where system events sporadically
  1007. * become disabled. */
  1008. hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
  1009. pr_notice("Re-enabled hotkeys\n");
  1010. } else {
  1011. pr_err("Error reading hotkey status\n");
  1012. return -EIO;
  1013. }
  1014. }
  1015. seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
  1016. seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
  1017. return 0;
  1018. }
  1019. static int keys_proc_open(struct inode *inode, struct file *file)
  1020. {
  1021. return single_open(file, keys_proc_show, PDE_DATA(inode));
  1022. }
  1023. static ssize_t keys_proc_write(struct file *file, const char __user *buf,
  1024. size_t count, loff_t *pos)
  1025. {
  1026. struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
  1027. char cmd[42];
  1028. size_t len;
  1029. int value;
  1030. len = min(count, sizeof(cmd) - 1);
  1031. if (copy_from_user(cmd, buf, len))
  1032. return -EFAULT;
  1033. cmd[len] = '\0';
  1034. if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
  1035. dev->key_event_valid = 0;
  1036. } else {
  1037. return -EINVAL;
  1038. }
  1039. return count;
  1040. }
  1041. static const struct file_operations keys_proc_fops = {
  1042. .owner = THIS_MODULE,
  1043. .open = keys_proc_open,
  1044. .read = seq_read,
  1045. .llseek = seq_lseek,
  1046. .release = single_release,
  1047. .write = keys_proc_write,
  1048. };
  1049. static int version_proc_show(struct seq_file *m, void *v)
  1050. {
  1051. seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
  1052. seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
  1053. return 0;
  1054. }
  1055. static int version_proc_open(struct inode *inode, struct file *file)
  1056. {
  1057. return single_open(file, version_proc_show, PDE_DATA(inode));
  1058. }
  1059. static const struct file_operations version_proc_fops = {
  1060. .owner = THIS_MODULE,
  1061. .open = version_proc_open,
  1062. .read = seq_read,
  1063. .llseek = seq_lseek,
  1064. .release = single_release,
  1065. };
  1066. /* proc and module init
  1067. */
  1068. #define PROC_TOSHIBA "toshiba"
  1069. static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
  1070. {
  1071. if (dev->backlight_dev)
  1072. proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1073. &lcd_proc_fops, dev);
  1074. if (dev->video_supported)
  1075. proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1076. &video_proc_fops, dev);
  1077. if (dev->fan_supported)
  1078. proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1079. &fan_proc_fops, dev);
  1080. if (dev->hotkey_dev)
  1081. proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
  1082. &keys_proc_fops, dev);
  1083. proc_create_data("version", S_IRUGO, toshiba_proc_dir,
  1084. &version_proc_fops, dev);
  1085. }
  1086. static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
  1087. {
  1088. if (dev->backlight_dev)
  1089. remove_proc_entry("lcd", toshiba_proc_dir);
  1090. if (dev->video_supported)
  1091. remove_proc_entry("video", toshiba_proc_dir);
  1092. if (dev->fan_supported)
  1093. remove_proc_entry("fan", toshiba_proc_dir);
  1094. if (dev->hotkey_dev)
  1095. remove_proc_entry("keys", toshiba_proc_dir);
  1096. remove_proc_entry("version", toshiba_proc_dir);
  1097. }
  1098. static const struct backlight_ops toshiba_backlight_data = {
  1099. .options = BL_CORE_SUSPENDRESUME,
  1100. .get_brightness = get_lcd_brightness,
  1101. .update_status = set_lcd_status,
  1102. };
  1103. /*
  1104. * Sysfs files
  1105. */
  1106. static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
  1107. struct device_attribute *attr,
  1108. const char *buf, size_t count);
  1109. static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
  1110. struct device_attribute *attr,
  1111. char *buf);
  1112. static ssize_t toshiba_kbd_type_show(struct device *dev,
  1113. struct device_attribute *attr,
  1114. char *buf);
  1115. static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
  1116. struct device_attribute *attr,
  1117. char *buf);
  1118. static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
  1119. struct device_attribute *attr,
  1120. const char *buf, size_t count);
  1121. static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
  1122. struct device_attribute *attr,
  1123. char *buf);
  1124. static ssize_t toshiba_touchpad_store(struct device *dev,
  1125. struct device_attribute *attr,
  1126. const char *buf, size_t count);
  1127. static ssize_t toshiba_touchpad_show(struct device *dev,
  1128. struct device_attribute *attr,
  1129. char *buf);
  1130. static ssize_t toshiba_position_show(struct device *dev,
  1131. struct device_attribute *attr,
  1132. char *buf);
  1133. static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
  1134. toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
  1135. static DEVICE_ATTR(kbd_type, S_IRUGO, toshiba_kbd_type_show, NULL);
  1136. static DEVICE_ATTR(available_kbd_modes, S_IRUGO,
  1137. toshiba_available_kbd_modes_show, NULL);
  1138. static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
  1139. toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
  1140. static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
  1141. toshiba_touchpad_show, toshiba_touchpad_store);
  1142. static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
  1143. static struct attribute *toshiba_attributes[] = {
  1144. &dev_attr_kbd_backlight_mode.attr,
  1145. &dev_attr_kbd_type.attr,
  1146. &dev_attr_available_kbd_modes.attr,
  1147. &dev_attr_kbd_backlight_timeout.attr,
  1148. &dev_attr_touchpad.attr,
  1149. &dev_attr_position.attr,
  1150. NULL,
  1151. };
  1152. static umode_t toshiba_sysfs_is_visible(struct kobject *,
  1153. struct attribute *, int);
  1154. static struct attribute_group toshiba_attr_group = {
  1155. .is_visible = toshiba_sysfs_is_visible,
  1156. .attrs = toshiba_attributes,
  1157. };
  1158. static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
  1159. struct device_attribute *attr,
  1160. const char *buf, size_t count)
  1161. {
  1162. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1163. int mode;
  1164. int time;
  1165. int ret;
  1166. ret = kstrtoint(buf, 0, &mode);
  1167. if (ret)
  1168. return ret;
  1169. /* Check for supported modes depending on keyboard backlight type */
  1170. if (toshiba->kbd_type == 1) {
  1171. /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
  1172. if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
  1173. return -EINVAL;
  1174. } else if (toshiba->kbd_type == 2) {
  1175. /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
  1176. if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
  1177. mode != SCI_KBD_MODE_OFF)
  1178. return -EINVAL;
  1179. }
  1180. /* Set the Keyboard Backlight Mode where:
  1181. * Auto - KBD backlight turns off automatically in given time
  1182. * FN-Z - KBD backlight "toggles" when hotkey pressed
  1183. * ON - KBD backlight is always on
  1184. * OFF - KBD backlight is always off
  1185. */
  1186. /* Only make a change if the actual mode has changed */
  1187. if (toshiba->kbd_mode != mode) {
  1188. /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
  1189. time = toshiba->kbd_time << HCI_MISC_SHIFT;
  1190. /* OR the "base time" to the actual method format */
  1191. if (toshiba->kbd_type == 1) {
  1192. /* Type 1 requires the current mode */
  1193. time |= toshiba->kbd_mode;
  1194. } else if (toshiba->kbd_type == 2) {
  1195. /* Type 2 requires the desired mode */
  1196. time |= mode;
  1197. }
  1198. ret = toshiba_kbd_illum_status_set(toshiba, time);
  1199. if (ret)
  1200. return ret;
  1201. /* Update sysfs entries on successful mode change*/
  1202. ret = sysfs_update_group(&toshiba->acpi_dev->dev.kobj,
  1203. &toshiba_attr_group);
  1204. if (ret)
  1205. return ret;
  1206. toshiba->kbd_mode = mode;
  1207. }
  1208. return count;
  1209. }
  1210. static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
  1211. struct device_attribute *attr,
  1212. char *buf)
  1213. {
  1214. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1215. u32 time;
  1216. if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
  1217. return -EIO;
  1218. return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
  1219. }
  1220. static ssize_t toshiba_kbd_type_show(struct device *dev,
  1221. struct device_attribute *attr,
  1222. char *buf)
  1223. {
  1224. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1225. return sprintf(buf, "%d\n", toshiba->kbd_type);
  1226. }
  1227. static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
  1228. struct device_attribute *attr,
  1229. char *buf)
  1230. {
  1231. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1232. if (toshiba->kbd_type == 1)
  1233. return sprintf(buf, "%x %x\n",
  1234. SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
  1235. return sprintf(buf, "%x %x %x\n",
  1236. SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
  1237. }
  1238. static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
  1239. struct device_attribute *attr,
  1240. const char *buf, size_t count)
  1241. {
  1242. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1243. int time;
  1244. int ret;
  1245. ret = kstrtoint(buf, 0, &time);
  1246. if (ret)
  1247. return ret;
  1248. /* Check for supported values depending on kbd_type */
  1249. if (toshiba->kbd_type == 1) {
  1250. if (time < 0 || time > 60)
  1251. return -EINVAL;
  1252. } else if (toshiba->kbd_type == 2) {
  1253. if (time < 1 || time > 60)
  1254. return -EINVAL;
  1255. }
  1256. /* Set the Keyboard Backlight Timeout */
  1257. /* Only make a change if the actual timeout has changed */
  1258. if (toshiba->kbd_time != time) {
  1259. /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
  1260. time = time << HCI_MISC_SHIFT;
  1261. /* OR the "base time" to the actual method format */
  1262. if (toshiba->kbd_type == 1)
  1263. time |= SCI_KBD_MODE_FNZ;
  1264. else if (toshiba->kbd_type == 2)
  1265. time |= SCI_KBD_MODE_AUTO;
  1266. ret = toshiba_kbd_illum_status_set(toshiba, time);
  1267. if (ret)
  1268. return ret;
  1269. toshiba->kbd_time = time >> HCI_MISC_SHIFT;
  1270. }
  1271. return count;
  1272. }
  1273. static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
  1274. struct device_attribute *attr,
  1275. char *buf)
  1276. {
  1277. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1278. u32 time;
  1279. if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
  1280. return -EIO;
  1281. return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
  1282. }
  1283. static ssize_t toshiba_touchpad_store(struct device *dev,
  1284. struct device_attribute *attr,
  1285. const char *buf, size_t count)
  1286. {
  1287. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1288. int state;
  1289. int ret;
  1290. /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
  1291. ret = kstrtoint(buf, 0, &state);
  1292. if (ret)
  1293. return ret;
  1294. if (state != 0 && state != 1)
  1295. return -EINVAL;
  1296. ret = toshiba_touchpad_set(toshiba, state);
  1297. if (ret)
  1298. return ret;
  1299. return count;
  1300. }
  1301. static ssize_t toshiba_touchpad_show(struct device *dev,
  1302. struct device_attribute *attr, char *buf)
  1303. {
  1304. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1305. u32 state;
  1306. int ret;
  1307. ret = toshiba_touchpad_get(toshiba, &state);
  1308. if (ret < 0)
  1309. return ret;
  1310. return sprintf(buf, "%i\n", state);
  1311. }
  1312. static ssize_t toshiba_position_show(struct device *dev,
  1313. struct device_attribute *attr, char *buf)
  1314. {
  1315. struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
  1316. u32 xyval, zval, tmp;
  1317. u16 x, y, z;
  1318. int ret;
  1319. xyval = zval = 0;
  1320. ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
  1321. if (ret < 0)
  1322. return ret;
  1323. x = xyval & HCI_ACCEL_MASK;
  1324. tmp = xyval >> HCI_MISC_SHIFT;
  1325. y = tmp & HCI_ACCEL_MASK;
  1326. z = zval & HCI_ACCEL_MASK;
  1327. return sprintf(buf, "%d %d %d\n", x, y, z);
  1328. }
  1329. static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
  1330. struct attribute *attr, int idx)
  1331. {
  1332. struct device *dev = container_of(kobj, struct device, kobj);
  1333. struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
  1334. bool exists = true;
  1335. if (attr == &dev_attr_kbd_backlight_mode.attr)
  1336. exists = (drv->kbd_illum_supported) ? true : false;
  1337. else if (attr == &dev_attr_kbd_backlight_timeout.attr)
  1338. exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
  1339. else if (attr == &dev_attr_touchpad.attr)
  1340. exists = (drv->touchpad_supported) ? true : false;
  1341. else if (attr == &dev_attr_position.attr)
  1342. exists = (drv->accelerometer_supported) ? true : false;
  1343. return exists ? attr->mode : 0;
  1344. }
  1345. static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
  1346. struct serio *port)
  1347. {
  1348. if (str & 0x20)
  1349. return false;
  1350. if (unlikely(data == 0xe0))
  1351. return false;
  1352. if ((data & 0x7f) == TOS1900_FN_SCAN) {
  1353. schedule_work(&toshiba_acpi->hotkey_work);
  1354. return true;
  1355. }
  1356. return false;
  1357. }
  1358. static void toshiba_acpi_hotkey_work(struct work_struct *work)
  1359. {
  1360. acpi_handle ec_handle = ec_get_handle();
  1361. acpi_status status;
  1362. if (!ec_handle)
  1363. return;
  1364. status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
  1365. if (ACPI_FAILURE(status))
  1366. pr_err("ACPI NTFY method execution failed\n");
  1367. }
  1368. /*
  1369. * Returns hotkey scancode, or < 0 on failure.
  1370. */
  1371. static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
  1372. {
  1373. unsigned long long value;
  1374. acpi_status status;
  1375. status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
  1376. NULL, &value);
  1377. if (ACPI_FAILURE(status)) {
  1378. pr_err("ACPI INFO method execution failed\n");
  1379. return -EIO;
  1380. }
  1381. return value;
  1382. }
  1383. static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
  1384. int scancode)
  1385. {
  1386. if (scancode == 0x100)
  1387. return;
  1388. /* act on key press; ignore key release */
  1389. if (scancode & 0x80)
  1390. return;
  1391. if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
  1392. pr_info("Unknown key %x\n", scancode);
  1393. }
  1394. static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
  1395. {
  1396. acpi_status status;
  1397. acpi_handle ec_handle;
  1398. int error;
  1399. u32 hci_result;
  1400. const struct key_entry *keymap = toshiba_acpi_keymap;
  1401. dev->hotkey_dev = input_allocate_device();
  1402. if (!dev->hotkey_dev)
  1403. return -ENOMEM;
  1404. dev->hotkey_dev->name = "Toshiba input device";
  1405. dev->hotkey_dev->phys = "toshiba_acpi/input0";
  1406. dev->hotkey_dev->id.bustype = BUS_HOST;
  1407. if (dmi_check_system(toshiba_alt_keymap_dmi))
  1408. keymap = toshiba_acpi_alt_keymap;
  1409. error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
  1410. if (error)
  1411. goto err_free_dev;
  1412. /*
  1413. * For some machines the SCI responsible for providing hotkey
  1414. * notification doesn't fire. We can trigger the notification
  1415. * whenever the Fn key is pressed using the NTFY method, if
  1416. * supported, so if it's present set up an i8042 key filter
  1417. * for this purpose.
  1418. */
  1419. status = AE_ERROR;
  1420. ec_handle = ec_get_handle();
  1421. if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
  1422. INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
  1423. error = i8042_install_filter(toshiba_acpi_i8042_filter);
  1424. if (error) {
  1425. pr_err("Error installing key filter\n");
  1426. goto err_free_keymap;
  1427. }
  1428. dev->ntfy_supported = 1;
  1429. }
  1430. /*
  1431. * Determine hotkey query interface. Prefer using the INFO
  1432. * method when it is available.
  1433. */
  1434. if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
  1435. dev->info_supported = 1;
  1436. else {
  1437. hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
  1438. if (hci_result == TOS_SUCCESS)
  1439. dev->system_event_supported = 1;
  1440. }
  1441. if (!dev->info_supported && !dev->system_event_supported) {
  1442. pr_warn("No hotkey query interface found\n");
  1443. goto err_remove_filter;
  1444. }
  1445. status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
  1446. if (ACPI_FAILURE(status)) {
  1447. pr_info("Unable to enable hotkeys\n");
  1448. error = -ENODEV;
  1449. goto err_remove_filter;
  1450. }
  1451. error = input_register_device(dev->hotkey_dev);
  1452. if (error) {
  1453. pr_info("Unable to register input device\n");
  1454. goto err_remove_filter;
  1455. }
  1456. hci_result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
  1457. return 0;
  1458. err_remove_filter:
  1459. if (dev->ntfy_supported)
  1460. i8042_remove_filter(toshiba_acpi_i8042_filter);
  1461. err_free_keymap:
  1462. sparse_keymap_free(dev->hotkey_dev);
  1463. err_free_dev:
  1464. input_free_device(dev->hotkey_dev);
  1465. dev->hotkey_dev = NULL;
  1466. return error;
  1467. }
  1468. static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
  1469. {
  1470. struct backlight_properties props;
  1471. int brightness;
  1472. int ret;
  1473. bool enabled;
  1474. /*
  1475. * Some machines don't support the backlight methods at all, and
  1476. * others support it read-only. Either of these is pretty useless,
  1477. * so only register the backlight device if the backlight method
  1478. * supports both reads and writes.
  1479. */
  1480. brightness = __get_lcd_brightness(dev);
  1481. if (brightness < 0)
  1482. return 0;
  1483. ret = set_lcd_brightness(dev, brightness);
  1484. if (ret) {
  1485. pr_debug("Backlight method is read-only, disabling backlight support\n");
  1486. return 0;
  1487. }
  1488. /* Determine whether or not BIOS supports transflective backlight */
  1489. ret = get_tr_backlight_status(dev, &enabled);
  1490. dev->tr_backlight_supported = !ret;
  1491. memset(&props, 0, sizeof(props));
  1492. props.type = BACKLIGHT_PLATFORM;
  1493. props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
  1494. /* adding an extra level and having 0 change to transflective mode */
  1495. if (dev->tr_backlight_supported)
  1496. props.max_brightness++;
  1497. dev->backlight_dev = backlight_device_register("toshiba",
  1498. &dev->acpi_dev->dev,
  1499. dev,
  1500. &toshiba_backlight_data,
  1501. &props);
  1502. if (IS_ERR(dev->backlight_dev)) {
  1503. ret = PTR_ERR(dev->backlight_dev);
  1504. pr_err("Could not register toshiba backlight device\n");
  1505. dev->backlight_dev = NULL;
  1506. return ret;
  1507. }
  1508. dev->backlight_dev->props.brightness = brightness;
  1509. return 0;
  1510. }
  1511. static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
  1512. {
  1513. struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
  1514. remove_toshiba_proc_entries(dev);
  1515. if (dev->sysfs_created)
  1516. sysfs_remove_group(&dev->acpi_dev->dev.kobj,
  1517. &toshiba_attr_group);
  1518. if (dev->ntfy_supported) {
  1519. i8042_remove_filter(toshiba_acpi_i8042_filter);
  1520. cancel_work_sync(&dev->hotkey_work);
  1521. }
  1522. if (dev->hotkey_dev) {
  1523. input_unregister_device(dev->hotkey_dev);
  1524. sparse_keymap_free(dev->hotkey_dev);
  1525. }
  1526. if (dev->bt_rfk) {
  1527. rfkill_unregister(dev->bt_rfk);
  1528. rfkill_destroy(dev->bt_rfk);
  1529. }
  1530. if (dev->backlight_dev)
  1531. backlight_device_unregister(dev->backlight_dev);
  1532. if (dev->illumination_supported)
  1533. led_classdev_unregister(&dev->led_dev);
  1534. if (dev->kbd_led_registered)
  1535. led_classdev_unregister(&dev->kbd_led);
  1536. if (dev->eco_supported)
  1537. led_classdev_unregister(&dev->eco_led);
  1538. if (toshiba_acpi)
  1539. toshiba_acpi = NULL;
  1540. kfree(dev);
  1541. return 0;
  1542. }
  1543. static const char *find_hci_method(acpi_handle handle)
  1544. {
  1545. if (acpi_has_method(handle, "GHCI"))
  1546. return "GHCI";
  1547. if (acpi_has_method(handle, "SPFC"))
  1548. return "SPFC";
  1549. return NULL;
  1550. }
  1551. static int toshiba_acpi_add(struct acpi_device *acpi_dev)
  1552. {
  1553. struct toshiba_acpi_dev *dev;
  1554. const char *hci_method;
  1555. u32 dummy;
  1556. bool bt_present;
  1557. int ret = 0;
  1558. if (toshiba_acpi)
  1559. return -EBUSY;
  1560. pr_info("Toshiba Laptop ACPI Extras version %s\n",
  1561. TOSHIBA_ACPI_VERSION);
  1562. hci_method = find_hci_method(acpi_dev->handle);
  1563. if (!hci_method) {
  1564. pr_err("HCI interface not found\n");
  1565. return -ENODEV;
  1566. }
  1567. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1568. if (!dev)
  1569. return -ENOMEM;
  1570. dev->acpi_dev = acpi_dev;
  1571. dev->method_hci = hci_method;
  1572. acpi_dev->driver_data = dev;
  1573. dev_set_drvdata(&acpi_dev->dev, dev);
  1574. if (toshiba_acpi_setup_keyboard(dev))
  1575. pr_info("Unable to activate hotkeys\n");
  1576. mutex_init(&dev->mutex);
  1577. ret = toshiba_acpi_setup_backlight(dev);
  1578. if (ret)
  1579. goto error;
  1580. /* Register rfkill switch for Bluetooth */
  1581. if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
  1582. dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
  1583. &acpi_dev->dev,
  1584. RFKILL_TYPE_BLUETOOTH,
  1585. &toshiba_rfk_ops,
  1586. dev);
  1587. if (!dev->bt_rfk) {
  1588. pr_err("unable to allocate rfkill device\n");
  1589. ret = -ENOMEM;
  1590. goto error;
  1591. }
  1592. ret = rfkill_register(dev->bt_rfk);
  1593. if (ret) {
  1594. pr_err("unable to register rfkill device\n");
  1595. rfkill_destroy(dev->bt_rfk);
  1596. goto error;
  1597. }
  1598. }
  1599. if (toshiba_illumination_available(dev)) {
  1600. dev->led_dev.name = "toshiba::illumination";
  1601. dev->led_dev.max_brightness = 1;
  1602. dev->led_dev.brightness_set = toshiba_illumination_set;
  1603. dev->led_dev.brightness_get = toshiba_illumination_get;
  1604. if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
  1605. dev->illumination_supported = 1;
  1606. }
  1607. if (toshiba_eco_mode_available(dev)) {
  1608. dev->eco_led.name = "toshiba::eco_mode";
  1609. dev->eco_led.max_brightness = 1;
  1610. dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
  1611. dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
  1612. if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
  1613. dev->eco_supported = 1;
  1614. }
  1615. dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
  1616. /*
  1617. * Only register the LED if KBD illumination is supported
  1618. * and the keyboard backlight operation mode is set to FN-Z
  1619. */
  1620. if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
  1621. dev->kbd_led.name = "toshiba::kbd_backlight";
  1622. dev->kbd_led.max_brightness = 1;
  1623. dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
  1624. dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
  1625. if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
  1626. dev->kbd_led_registered = 1;
  1627. }
  1628. ret = toshiba_touchpad_get(dev, &dummy);
  1629. dev->touchpad_supported = !ret;
  1630. ret = toshiba_accelerometer_supported(dev);
  1631. dev->accelerometer_supported = !ret;
  1632. /* Determine whether or not BIOS supports fan and video interfaces */
  1633. ret = get_video_status(dev, &dummy);
  1634. dev->video_supported = !ret;
  1635. ret = get_fan_status(dev, &dummy);
  1636. dev->fan_supported = !ret;
  1637. ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
  1638. &toshiba_attr_group);
  1639. if (ret) {
  1640. dev->sysfs_created = 0;
  1641. goto error;
  1642. }
  1643. dev->sysfs_created = !ret;
  1644. create_toshiba_proc_entries(dev);
  1645. toshiba_acpi = dev;
  1646. return 0;
  1647. error:
  1648. toshiba_acpi_remove(acpi_dev);
  1649. return ret;
  1650. }
  1651. static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
  1652. {
  1653. struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
  1654. u32 hci_result, value;
  1655. int retries = 3;
  1656. int scancode;
  1657. if (event != 0x80)
  1658. return;
  1659. if (dev->info_supported) {
  1660. scancode = toshiba_acpi_query_hotkey(dev);
  1661. if (scancode < 0)
  1662. pr_err("Failed to query hotkey event\n");
  1663. else if (scancode != 0)
  1664. toshiba_acpi_report_hotkey(dev, scancode);
  1665. } else if (dev->system_event_supported) {
  1666. do {
  1667. hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
  1668. switch (hci_result) {
  1669. case TOS_SUCCESS:
  1670. toshiba_acpi_report_hotkey(dev, (int)value);
  1671. break;
  1672. case TOS_NOT_SUPPORTED:
  1673. /*
  1674. * This is a workaround for an unresolved
  1675. * issue on some machines where system events
  1676. * sporadically become disabled.
  1677. */
  1678. hci_result =
  1679. hci_write1(dev, HCI_SYSTEM_EVENT, 1);
  1680. pr_notice("Re-enabled hotkeys\n");
  1681. /* fall through */
  1682. default:
  1683. retries--;
  1684. break;
  1685. }
  1686. } while (retries && hci_result != TOS_FIFO_EMPTY);
  1687. }
  1688. }
  1689. #ifdef CONFIG_PM_SLEEP
  1690. static int toshiba_acpi_suspend(struct device *device)
  1691. {
  1692. struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
  1693. u32 result;
  1694. if (dev->hotkey_dev)
  1695. result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
  1696. return 0;
  1697. }
  1698. static int toshiba_acpi_resume(struct device *device)
  1699. {
  1700. struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
  1701. u32 result;
  1702. acpi_status status;
  1703. if (dev->hotkey_dev) {
  1704. status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB",
  1705. NULL, NULL);
  1706. if (ACPI_FAILURE(status))
  1707. pr_info("Unable to re-enable hotkeys\n");
  1708. result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
  1709. }
  1710. return 0;
  1711. }
  1712. #endif
  1713. static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
  1714. toshiba_acpi_suspend, toshiba_acpi_resume);
  1715. static struct acpi_driver toshiba_acpi_driver = {
  1716. .name = "Toshiba ACPI driver",
  1717. .owner = THIS_MODULE,
  1718. .ids = toshiba_device_ids,
  1719. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1720. .ops = {
  1721. .add = toshiba_acpi_add,
  1722. .remove = toshiba_acpi_remove,
  1723. .notify = toshiba_acpi_notify,
  1724. },
  1725. .drv.pm = &toshiba_acpi_pm,
  1726. };
  1727. static int __init toshiba_acpi_init(void)
  1728. {
  1729. int ret;
  1730. /*
  1731. * Machines with this WMI guid aren't supported due to bugs in
  1732. * their AML. This check relies on wmi initializing before
  1733. * toshiba_acpi to guarantee guids have been identified.
  1734. */
  1735. if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
  1736. return -ENODEV;
  1737. toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
  1738. if (!toshiba_proc_dir) {
  1739. pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
  1740. return -ENODEV;
  1741. }
  1742. ret = acpi_bus_register_driver(&toshiba_acpi_driver);
  1743. if (ret) {
  1744. pr_err("Failed to register ACPI driver: %d\n", ret);
  1745. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  1746. }
  1747. return ret;
  1748. }
  1749. static void __exit toshiba_acpi_exit(void)
  1750. {
  1751. acpi_bus_unregister_driver(&toshiba_acpi_driver);
  1752. if (toshiba_proc_dir)
  1753. remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
  1754. }
  1755. module_init(toshiba_acpi_init);
  1756. module_exit(toshiba_acpi_exit);