toshiba_acpi.c 53 KB

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