sony-laptop.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. /*
  2. * ACPI Sony Notebook Control Driver (SNC and SPIC)
  3. *
  4. * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
  5. * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
  6. *
  7. * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
  8. * which are copyrighted by their respective authors.
  9. *
  10. * The SNY6001 driver part is based on the sonypi driver which includes
  11. * material from:
  12. *
  13. * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
  14. *
  15. * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
  16. *
  17. * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
  18. *
  19. * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
  20. *
  21. * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
  22. *
  23. * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
  24. *
  25. * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
  26. *
  27. * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. *
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/module.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/init.h>
  48. #include <linux/types.h>
  49. #include <linux/backlight.h>
  50. #include <linux/platform_device.h>
  51. #include <linux/err.h>
  52. #include <linux/dmi.h>
  53. #include <linux/pci.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/delay.h>
  56. #include <linux/input.h>
  57. #include <linux/kfifo.h>
  58. #include <linux/workqueue.h>
  59. #include <linux/acpi.h>
  60. #include <acpi/acpi_drivers.h>
  61. #include <acpi/acpi_bus.h>
  62. #include <asm/uaccess.h>
  63. #include <linux/sonypi.h>
  64. #include <linux/sony-laptop.h>
  65. #ifdef CONFIG_SONYPI_COMPAT
  66. #include <linux/poll.h>
  67. #include <linux/miscdevice.h>
  68. #endif
  69. #define DRV_PFX "sony-laptop: "
  70. #define dprintk(msg...) do { \
  71. if (debug) printk(KERN_WARNING DRV_PFX msg); \
  72. } while (0)
  73. #define SONY_LAPTOP_DRIVER_VERSION "0.5"
  74. #define SONY_NC_CLASS "sony-nc"
  75. #define SONY_NC_HID "SNY5001"
  76. #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
  77. #define SONY_PIC_CLASS "sony-pic"
  78. #define SONY_PIC_HID "SNY6001"
  79. #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
  80. MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
  81. MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
  84. static int debug;
  85. module_param(debug, int, 0);
  86. MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
  87. "the development of this driver");
  88. static int no_spic; /* = 0 */
  89. module_param(no_spic, int, 0444);
  90. MODULE_PARM_DESC(no_spic,
  91. "set this if you don't want to enable the SPIC device");
  92. static int compat; /* = 0 */
  93. module_param(compat, int, 0444);
  94. MODULE_PARM_DESC(compat,
  95. "set this if you want to enable backward compatibility mode");
  96. static unsigned long mask = 0xffffffff;
  97. module_param(mask, ulong, 0644);
  98. MODULE_PARM_DESC(mask,
  99. "set this to the mask of event you want to enable (see doc)");
  100. static int camera; /* = 0 */
  101. module_param(camera, int, 0444);
  102. MODULE_PARM_DESC(camera,
  103. "set this to 1 to enable Motion Eye camera controls "
  104. "(only use it if you have a C1VE or C1VN model)");
  105. #ifdef CONFIG_SONYPI_COMPAT
  106. static int minor = -1;
  107. module_param(minor, int, 0);
  108. MODULE_PARM_DESC(minor,
  109. "minor number of the misc device for the SPIC compatibility code, "
  110. "default is -1 (automatic)");
  111. #endif
  112. /*********** Input Devices ***********/
  113. #define SONY_LAPTOP_BUF_SIZE 128
  114. struct sony_laptop_input_s {
  115. atomic_t users;
  116. struct input_dev *jog_dev;
  117. struct input_dev *key_dev;
  118. struct kfifo *fifo;
  119. spinlock_t fifo_lock;
  120. struct workqueue_struct *wq;
  121. };
  122. static struct sony_laptop_input_s sony_laptop_input = {
  123. .users = ATOMIC_INIT(0),
  124. };
  125. struct sony_laptop_keypress {
  126. struct input_dev *dev;
  127. int key;
  128. };
  129. /* Correspondance table between sonypi events and input layer events */
  130. static struct {
  131. int sonypiev;
  132. int inputev;
  133. } sony_laptop_inputkeys[] = {
  134. { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
  135. { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
  136. { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
  137. { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
  138. { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
  139. { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
  140. { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
  141. { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
  142. { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
  143. { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
  144. { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
  145. { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
  146. { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
  147. { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
  148. { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
  149. { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
  150. { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
  151. { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
  152. { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
  153. { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
  154. { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
  155. { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
  156. { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
  157. { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
  158. { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
  159. { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
  160. { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
  161. { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
  162. { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
  163. { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
  164. { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
  165. { 0, 0 },
  166. };
  167. /* release buttons after a short delay if pressed */
  168. static void do_sony_laptop_release_key(struct work_struct *work)
  169. {
  170. struct sony_laptop_keypress kp;
  171. while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
  172. sizeof(kp)) == sizeof(kp)) {
  173. msleep(10);
  174. input_report_key(kp.dev, kp.key, 0);
  175. input_sync(kp.dev);
  176. }
  177. }
  178. static DECLARE_WORK(sony_laptop_release_key_work,
  179. do_sony_laptop_release_key);
  180. /* forward event to the input subsytem */
  181. static void sony_laptop_report_input_event(u8 event)
  182. {
  183. struct input_dev *jog_dev = sony_laptop_input.jog_dev;
  184. struct input_dev *key_dev = sony_laptop_input.key_dev;
  185. struct sony_laptop_keypress kp = { NULL };
  186. int i;
  187. if (event == SONYPI_EVENT_FNKEY_RELEASED) {
  188. /* Nothing, not all VAIOs generate this event */
  189. return;
  190. }
  191. /* report events */
  192. switch (event) {
  193. /* jog_dev events */
  194. case SONYPI_EVENT_JOGDIAL_UP:
  195. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  196. input_report_rel(jog_dev, REL_WHEEL, 1);
  197. input_sync(jog_dev);
  198. return;
  199. case SONYPI_EVENT_JOGDIAL_DOWN:
  200. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  201. input_report_rel(jog_dev, REL_WHEEL, -1);
  202. input_sync(jog_dev);
  203. return;
  204. /* key_dev events */
  205. case SONYPI_EVENT_JOGDIAL_PRESSED:
  206. kp.key = BTN_MIDDLE;
  207. kp.dev = jog_dev;
  208. break;
  209. default:
  210. for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
  211. if (event == sony_laptop_inputkeys[i].sonypiev) {
  212. kp.dev = key_dev;
  213. kp.key = sony_laptop_inputkeys[i].inputev;
  214. break;
  215. }
  216. break;
  217. }
  218. if (kp.dev) {
  219. input_report_key(kp.dev, kp.key, 1);
  220. input_sync(kp.dev);
  221. kfifo_put(sony_laptop_input.fifo,
  222. (unsigned char *)&kp, sizeof(kp));
  223. if (!work_pending(&sony_laptop_release_key_work))
  224. queue_work(sony_laptop_input.wq,
  225. &sony_laptop_release_key_work);
  226. } else
  227. dprintk("unknown input event %.2x\n", event);
  228. }
  229. static int sony_laptop_setup_input(void)
  230. {
  231. struct input_dev *jog_dev;
  232. struct input_dev *key_dev;
  233. int i;
  234. int error;
  235. /* don't run again if already initialized */
  236. if (atomic_add_return(1, &sony_laptop_input.users) > 1)
  237. return 0;
  238. /* kfifo */
  239. spin_lock_init(&sony_laptop_input.fifo_lock);
  240. sony_laptop_input.fifo =
  241. kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
  242. &sony_laptop_input.fifo_lock);
  243. if (IS_ERR(sony_laptop_input.fifo)) {
  244. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  245. error = PTR_ERR(sony_laptop_input.fifo);
  246. goto err_dec_users;
  247. }
  248. /* init workqueue */
  249. sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
  250. if (!sony_laptop_input.wq) {
  251. printk(KERN_ERR DRV_PFX
  252. "Unabe to create workqueue.\n");
  253. error = -ENXIO;
  254. goto err_free_kfifo;
  255. }
  256. /* input keys */
  257. key_dev = input_allocate_device();
  258. if (!key_dev) {
  259. error = -ENOMEM;
  260. goto err_destroy_wq;
  261. }
  262. key_dev->name = "Sony Vaio Keys";
  263. key_dev->id.bustype = BUS_ISA;
  264. key_dev->id.vendor = PCI_VENDOR_ID_SONY;
  265. /* Initialize the Input Drivers: special keys */
  266. key_dev->evbit[0] = BIT(EV_KEY);
  267. for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
  268. if (sony_laptop_inputkeys[i].inputev)
  269. set_bit(sony_laptop_inputkeys[i].inputev,
  270. key_dev->keybit);
  271. error = input_register_device(key_dev);
  272. if (error)
  273. goto err_free_keydev;
  274. sony_laptop_input.key_dev = key_dev;
  275. /* jogdial */
  276. jog_dev = input_allocate_device();
  277. if (!jog_dev) {
  278. error = -ENOMEM;
  279. goto err_unregister_keydev;
  280. }
  281. jog_dev->name = "Sony Vaio Jogdial";
  282. jog_dev->id.bustype = BUS_ISA;
  283. jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
  284. jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  285. jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
  286. jog_dev->relbit[0] = BIT(REL_WHEEL);
  287. error = input_register_device(jog_dev);
  288. if (error)
  289. goto err_free_jogdev;
  290. sony_laptop_input.jog_dev = jog_dev;
  291. return 0;
  292. err_free_jogdev:
  293. input_free_device(jog_dev);
  294. err_unregister_keydev:
  295. input_unregister_device(key_dev);
  296. /* to avoid kref underflow below at input_free_device */
  297. key_dev = NULL;
  298. err_free_keydev:
  299. input_free_device(key_dev);
  300. err_destroy_wq:
  301. destroy_workqueue(sony_laptop_input.wq);
  302. err_free_kfifo:
  303. kfifo_free(sony_laptop_input.fifo);
  304. err_dec_users:
  305. atomic_dec(&sony_laptop_input.users);
  306. return error;
  307. }
  308. static void sony_laptop_remove_input(void)
  309. {
  310. /* cleanup only after the last user has gone */
  311. if (!atomic_dec_and_test(&sony_laptop_input.users))
  312. return;
  313. /* flush workqueue first */
  314. flush_workqueue(sony_laptop_input.wq);
  315. /* destroy input devs */
  316. input_unregister_device(sony_laptop_input.key_dev);
  317. sony_laptop_input.key_dev = NULL;
  318. if (sony_laptop_input.jog_dev) {
  319. input_unregister_device(sony_laptop_input.jog_dev);
  320. sony_laptop_input.jog_dev = NULL;
  321. }
  322. destroy_workqueue(sony_laptop_input.wq);
  323. kfifo_free(sony_laptop_input.fifo);
  324. }
  325. /*********** Platform Device ***********/
  326. static atomic_t sony_pf_users = ATOMIC_INIT(0);
  327. static struct platform_driver sony_pf_driver = {
  328. .driver = {
  329. .name = "sony-laptop",
  330. .owner = THIS_MODULE,
  331. }
  332. };
  333. static struct platform_device *sony_pf_device;
  334. static int sony_pf_add(void)
  335. {
  336. int ret = 0;
  337. /* don't run again if already initialized */
  338. if (atomic_add_return(1, &sony_pf_users) > 1)
  339. return 0;
  340. ret = platform_driver_register(&sony_pf_driver);
  341. if (ret)
  342. goto out;
  343. sony_pf_device = platform_device_alloc("sony-laptop", -1);
  344. if (!sony_pf_device) {
  345. ret = -ENOMEM;
  346. goto out_platform_registered;
  347. }
  348. ret = platform_device_add(sony_pf_device);
  349. if (ret)
  350. goto out_platform_alloced;
  351. return 0;
  352. out_platform_alloced:
  353. platform_device_put(sony_pf_device);
  354. sony_pf_device = NULL;
  355. out_platform_registered:
  356. platform_driver_unregister(&sony_pf_driver);
  357. out:
  358. atomic_dec(&sony_pf_users);
  359. return ret;
  360. }
  361. static void sony_pf_remove(void)
  362. {
  363. /* deregister only after the last user has gone */
  364. if (!atomic_dec_and_test(&sony_pf_users))
  365. return;
  366. platform_device_del(sony_pf_device);
  367. platform_device_put(sony_pf_device);
  368. platform_driver_unregister(&sony_pf_driver);
  369. }
  370. /*********** SNC (SNY5001) Device ***********/
  371. /* the device uses 1-based values, while the backlight subsystem uses
  372. 0-based values */
  373. #define SONY_MAX_BRIGHTNESS 8
  374. #define SNC_VALIDATE_IN 0
  375. #define SNC_VALIDATE_OUT 1
  376. static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
  377. char *);
  378. static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
  379. const char *, size_t);
  380. static int boolean_validate(const int, const int);
  381. static int brightness_default_validate(const int, const int);
  382. struct sony_nc_value {
  383. char *name; /* name of the entry */
  384. char **acpiget; /* names of the ACPI get function */
  385. char **acpiset; /* names of the ACPI set function */
  386. int (*validate)(const int, const int); /* input/output validation */
  387. int value; /* current setting */
  388. int valid; /* Has ever been set */
  389. int debug; /* active only in debug mode ? */
  390. struct device_attribute devattr; /* sysfs atribute */
  391. };
  392. #define SNC_HANDLE_NAMES(_name, _values...) \
  393. static char *snc_##_name[] = { _values, NULL }
  394. #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
  395. { \
  396. .name = __stringify(_name), \
  397. .acpiget = _getters, \
  398. .acpiset = _setters, \
  399. .validate = _validate, \
  400. .debug = _debug, \
  401. .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
  402. }
  403. #define SNC_HANDLE_NULL { .name = NULL }
  404. SNC_HANDLE_NAMES(fnkey_get, "GHKE");
  405. SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
  406. SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
  407. SNC_HANDLE_NAMES(cdpower_get, "GCDP");
  408. SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
  409. SNC_HANDLE_NAMES(audiopower_get, "GAZP");
  410. SNC_HANDLE_NAMES(audiopower_set, "AZPW");
  411. SNC_HANDLE_NAMES(lanpower_get, "GLNP");
  412. SNC_HANDLE_NAMES(lanpower_set, "LNPW");
  413. SNC_HANDLE_NAMES(PID_get, "GPID");
  414. SNC_HANDLE_NAMES(CTR_get, "GCTR");
  415. SNC_HANDLE_NAMES(CTR_set, "SCTR");
  416. SNC_HANDLE_NAMES(PCR_get, "GPCR");
  417. SNC_HANDLE_NAMES(PCR_set, "SPCR");
  418. SNC_HANDLE_NAMES(CMI_get, "GCMI");
  419. SNC_HANDLE_NAMES(CMI_set, "SCMI");
  420. static struct sony_nc_value sony_nc_values[] = {
  421. SNC_HANDLE(brightness_default, snc_brightness_def_get,
  422. snc_brightness_def_set, brightness_default_validate, 0),
  423. SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
  424. SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
  425. SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
  426. boolean_validate, 0),
  427. SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
  428. boolean_validate, 1),
  429. /* unknown methods */
  430. SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
  431. SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
  432. SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
  433. SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
  434. SNC_HANDLE_NULL
  435. };
  436. static acpi_handle sony_nc_acpi_handle;
  437. static struct acpi_device *sony_nc_acpi_device = NULL;
  438. /*
  439. * acpi_evaluate_object wrappers
  440. */
  441. static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
  442. {
  443. struct acpi_buffer output;
  444. union acpi_object out_obj;
  445. acpi_status status;
  446. output.length = sizeof(out_obj);
  447. output.pointer = &out_obj;
  448. status = acpi_evaluate_object(handle, name, NULL, &output);
  449. if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
  450. *result = out_obj.integer.value;
  451. return 0;
  452. }
  453. printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
  454. return -1;
  455. }
  456. static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
  457. int *result)
  458. {
  459. struct acpi_object_list params;
  460. union acpi_object in_obj;
  461. struct acpi_buffer output;
  462. union acpi_object out_obj;
  463. acpi_status status;
  464. params.count = 1;
  465. params.pointer = &in_obj;
  466. in_obj.type = ACPI_TYPE_INTEGER;
  467. in_obj.integer.value = value;
  468. output.length = sizeof(out_obj);
  469. output.pointer = &out_obj;
  470. status = acpi_evaluate_object(handle, name, &params, &output);
  471. if (status == AE_OK) {
  472. if (result != NULL) {
  473. if (out_obj.type != ACPI_TYPE_INTEGER) {
  474. printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
  475. "return type\n");
  476. return -1;
  477. }
  478. *result = out_obj.integer.value;
  479. }
  480. return 0;
  481. }
  482. printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
  483. return -1;
  484. }
  485. /*
  486. * sony_nc_values input/output validate functions
  487. */
  488. /* brightness_default_validate:
  489. *
  490. * manipulate input output values to keep consistency with the
  491. * backlight framework for which brightness values are 0-based.
  492. */
  493. static int brightness_default_validate(const int direction, const int value)
  494. {
  495. switch (direction) {
  496. case SNC_VALIDATE_OUT:
  497. return value - 1;
  498. case SNC_VALIDATE_IN:
  499. if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
  500. return value + 1;
  501. }
  502. return -EINVAL;
  503. }
  504. /* boolean_validate:
  505. *
  506. * on input validate boolean values 0/1, on output just pass the
  507. * received value.
  508. */
  509. static int boolean_validate(const int direction, const int value)
  510. {
  511. if (direction == SNC_VALIDATE_IN) {
  512. if (value != 0 && value != 1)
  513. return -EINVAL;
  514. }
  515. return value;
  516. }
  517. /*
  518. * Sysfs show/store common to all sony_nc_values
  519. */
  520. static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
  521. char *buffer)
  522. {
  523. int value;
  524. struct sony_nc_value *item =
  525. container_of(attr, struct sony_nc_value, devattr);
  526. if (!*item->acpiget)
  527. return -EIO;
  528. if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
  529. return -EIO;
  530. if (item->validate)
  531. value = item->validate(SNC_VALIDATE_OUT, value);
  532. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  533. }
  534. static ssize_t sony_nc_sysfs_store(struct device *dev,
  535. struct device_attribute *attr,
  536. const char *buffer, size_t count)
  537. {
  538. int value;
  539. struct sony_nc_value *item =
  540. container_of(attr, struct sony_nc_value, devattr);
  541. if (!item->acpiset)
  542. return -EIO;
  543. if (count > 31)
  544. return -EINVAL;
  545. value = simple_strtoul(buffer, NULL, 10);
  546. if (item->validate)
  547. value = item->validate(SNC_VALIDATE_IN, value);
  548. if (value < 0)
  549. return value;
  550. if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
  551. return -EIO;
  552. item->value = value;
  553. item->valid = 1;
  554. return count;
  555. }
  556. /*
  557. * Backlight device
  558. */
  559. static int sony_backlight_update_status(struct backlight_device *bd)
  560. {
  561. return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  562. bd->props.brightness + 1, NULL);
  563. }
  564. static int sony_backlight_get_brightness(struct backlight_device *bd)
  565. {
  566. int value;
  567. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
  568. return 0;
  569. /* brightness levels are 1-based, while backlight ones are 0-based */
  570. return value - 1;
  571. }
  572. static struct backlight_device *sony_backlight_device;
  573. static struct backlight_ops sony_backlight_ops = {
  574. .update_status = sony_backlight_update_status,
  575. .get_brightness = sony_backlight_get_brightness,
  576. };
  577. /*
  578. * ACPI callbacks
  579. */
  580. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  581. {
  582. dprintk("sony_acpi_notify, event: %d\n", event);
  583. sony_laptop_report_input_event(event);
  584. acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
  585. }
  586. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  587. void *context, void **return_value)
  588. {
  589. struct acpi_namespace_node *node;
  590. union acpi_operand_object *operand;
  591. node = (struct acpi_namespace_node *)handle;
  592. operand = (union acpi_operand_object *)node->object;
  593. printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  594. (u32) operand->method.param_count);
  595. return AE_OK;
  596. }
  597. /*
  598. * ACPI device
  599. */
  600. static int sony_nc_resume(struct acpi_device *device)
  601. {
  602. struct sony_nc_value *item;
  603. for (item = sony_nc_values; item->name; item++) {
  604. int ret;
  605. if (!item->valid)
  606. continue;
  607. ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
  608. item->value, NULL);
  609. if (ret < 0) {
  610. printk("%s: %d\n", __FUNCTION__, ret);
  611. break;
  612. }
  613. }
  614. return 0;
  615. }
  616. static int sony_nc_add(struct acpi_device *device)
  617. {
  618. acpi_status status;
  619. int result = 0;
  620. acpi_handle handle;
  621. struct sony_nc_value *item;
  622. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  623. SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  624. sony_nc_acpi_device = device;
  625. strcpy(acpi_device_class(device), "sony/hotkey");
  626. sony_nc_acpi_handle = device->handle;
  627. if (debug) {
  628. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
  629. 1, sony_walk_callback, NULL, NULL);
  630. if (ACPI_FAILURE(status)) {
  631. printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
  632. result = -ENODEV;
  633. goto outwalk;
  634. }
  635. }
  636. /* setup input devices and helper fifo */
  637. result = sony_laptop_setup_input();
  638. if (result) {
  639. printk(KERN_ERR DRV_PFX
  640. "Unabe to create input devices.\n");
  641. goto outwalk;
  642. }
  643. status = acpi_install_notify_handler(sony_nc_acpi_handle,
  644. ACPI_DEVICE_NOTIFY,
  645. sony_acpi_notify, NULL);
  646. if (ACPI_FAILURE(status)) {
  647. printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
  648. result = -ENODEV;
  649. goto outinput;
  650. }
  651. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
  652. sony_backlight_device = backlight_device_register("sony", NULL,
  653. NULL,
  654. &sony_backlight_ops);
  655. if (IS_ERR(sony_backlight_device)) {
  656. printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
  657. sony_backlight_device = NULL;
  658. } else {
  659. sony_backlight_device->props.brightness =
  660. sony_backlight_get_brightness
  661. (sony_backlight_device);
  662. sony_backlight_device->props.max_brightness =
  663. SONY_MAX_BRIGHTNESS - 1;
  664. }
  665. }
  666. result = sony_pf_add();
  667. if (result)
  668. goto outbacklight;
  669. /* create sony_pf sysfs attributes related to the SNC device */
  670. for (item = sony_nc_values; item->name; ++item) {
  671. if (!debug && item->debug)
  672. continue;
  673. /* find the available acpiget as described in the DSDT */
  674. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  675. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  676. *item->acpiget,
  677. &handle))) {
  678. dprintk("Found %s getter: %s\n",
  679. item->name, *item->acpiget);
  680. item->devattr.attr.mode |= S_IRUGO;
  681. break;
  682. }
  683. }
  684. /* find the available acpiset as described in the DSDT */
  685. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  686. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  687. *item->acpiset,
  688. &handle))) {
  689. dprintk("Found %s setter: %s\n",
  690. item->name, *item->acpiset);
  691. item->devattr.attr.mode |= S_IWUSR;
  692. break;
  693. }
  694. }
  695. if (item->devattr.attr.mode != 0) {
  696. result =
  697. device_create_file(&sony_pf_device->dev,
  698. &item->devattr);
  699. if (result)
  700. goto out_sysfs;
  701. }
  702. }
  703. return 0;
  704. out_sysfs:
  705. for (item = sony_nc_values; item->name; ++item) {
  706. device_remove_file(&sony_pf_device->dev, &item->devattr);
  707. }
  708. sony_pf_remove();
  709. outbacklight:
  710. if (sony_backlight_device)
  711. backlight_device_unregister(sony_backlight_device);
  712. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  713. ACPI_DEVICE_NOTIFY,
  714. sony_acpi_notify);
  715. if (ACPI_FAILURE(status))
  716. printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
  717. outinput:
  718. sony_laptop_remove_input();
  719. outwalk:
  720. return result;
  721. }
  722. static int sony_nc_remove(struct acpi_device *device, int type)
  723. {
  724. acpi_status status;
  725. struct sony_nc_value *item;
  726. if (sony_backlight_device)
  727. backlight_device_unregister(sony_backlight_device);
  728. sony_nc_acpi_device = NULL;
  729. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  730. ACPI_DEVICE_NOTIFY,
  731. sony_acpi_notify);
  732. if (ACPI_FAILURE(status))
  733. printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
  734. for (item = sony_nc_values; item->name; ++item) {
  735. device_remove_file(&sony_pf_device->dev, &item->devattr);
  736. }
  737. sony_pf_remove();
  738. sony_laptop_remove_input();
  739. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  740. return 0;
  741. }
  742. static struct acpi_driver sony_nc_driver = {
  743. .name = SONY_NC_DRIVER_NAME,
  744. .class = SONY_NC_CLASS,
  745. .ids = SONY_NC_HID,
  746. .owner = THIS_MODULE,
  747. .ops = {
  748. .add = sony_nc_add,
  749. .remove = sony_nc_remove,
  750. .resume = sony_nc_resume,
  751. },
  752. };
  753. /*********** SPIC (SNY6001) Device ***********/
  754. #define SONYPI_DEVICE_TYPE1 0x00000001
  755. #define SONYPI_DEVICE_TYPE2 0x00000002
  756. #define SONYPI_DEVICE_TYPE3 0x00000004
  757. #define SONY_PIC_EV_MASK 0xff
  758. struct sony_pic_ioport {
  759. struct acpi_resource_io io;
  760. struct list_head list;
  761. };
  762. struct sony_pic_irq {
  763. struct acpi_resource_irq irq;
  764. struct list_head list;
  765. };
  766. struct sony_pic_dev {
  767. int model;
  768. u8 camera_power;
  769. u8 bluetooth_power;
  770. u8 wwan_power;
  771. struct acpi_device *acpi_dev;
  772. struct sony_pic_irq *cur_irq;
  773. struct sony_pic_ioport *cur_ioport;
  774. struct list_head interrupts;
  775. struct list_head ioports;
  776. struct mutex lock;
  777. };
  778. static struct sony_pic_dev spic_dev = {
  779. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  780. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  781. };
  782. /* Event masks */
  783. #define SONYPI_JOGGER_MASK 0x00000001
  784. #define SONYPI_CAPTURE_MASK 0x00000002
  785. #define SONYPI_FNKEY_MASK 0x00000004
  786. #define SONYPI_BLUETOOTH_MASK 0x00000008
  787. #define SONYPI_PKEY_MASK 0x00000010
  788. #define SONYPI_BACK_MASK 0x00000020
  789. #define SONYPI_HELP_MASK 0x00000040
  790. #define SONYPI_LID_MASK 0x00000080
  791. #define SONYPI_ZOOM_MASK 0x00000100
  792. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  793. #define SONYPI_MEYE_MASK 0x00000400
  794. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  795. #define SONYPI_BATTERY_MASK 0x00001000
  796. #define SONYPI_WIRELESS_MASK 0x00002000
  797. struct sonypi_event {
  798. u8 data;
  799. u8 event;
  800. };
  801. /* The set of possible button release events */
  802. static struct sonypi_event sonypi_releaseev[] = {
  803. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  804. { 0, 0 }
  805. };
  806. /* The set of possible jogger events */
  807. static struct sonypi_event sonypi_joggerev[] = {
  808. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  809. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  810. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  811. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  812. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  813. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  814. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  815. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  816. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  817. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  818. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  819. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  820. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  821. { 0, 0 }
  822. };
  823. /* The set of possible capture button events */
  824. static struct sonypi_event sonypi_captureev[] = {
  825. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  826. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  827. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  828. { 0, 0 }
  829. };
  830. /* The set of possible fnkeys events */
  831. static struct sonypi_event sonypi_fnkeyev[] = {
  832. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  833. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  834. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  835. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  836. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  837. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  838. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  839. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  840. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  841. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  842. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  843. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  844. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  845. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  846. { 0x21, SONYPI_EVENT_FNKEY_1 },
  847. { 0x22, SONYPI_EVENT_FNKEY_2 },
  848. { 0x31, SONYPI_EVENT_FNKEY_D },
  849. { 0x32, SONYPI_EVENT_FNKEY_E },
  850. { 0x33, SONYPI_EVENT_FNKEY_F },
  851. { 0x34, SONYPI_EVENT_FNKEY_S },
  852. { 0x35, SONYPI_EVENT_FNKEY_B },
  853. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  854. { 0, 0 }
  855. };
  856. /* The set of possible program key events */
  857. static struct sonypi_event sonypi_pkeyev[] = {
  858. { 0x01, SONYPI_EVENT_PKEY_P1 },
  859. { 0x02, SONYPI_EVENT_PKEY_P2 },
  860. { 0x04, SONYPI_EVENT_PKEY_P3 },
  861. { 0x5c, SONYPI_EVENT_PKEY_P1 },
  862. { 0, 0 }
  863. };
  864. /* The set of possible bluetooth events */
  865. static struct sonypi_event sonypi_blueev[] = {
  866. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  867. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  868. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  869. { 0, 0 }
  870. };
  871. /* The set of possible wireless events */
  872. static struct sonypi_event sonypi_wlessev[] = {
  873. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  874. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  875. { 0, 0 }
  876. };
  877. /* The set of possible back button events */
  878. static struct sonypi_event sonypi_backev[] = {
  879. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  880. { 0, 0 }
  881. };
  882. /* The set of possible help button events */
  883. static struct sonypi_event sonypi_helpev[] = {
  884. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  885. { 0, 0 }
  886. };
  887. /* The set of possible lid events */
  888. static struct sonypi_event sonypi_lidev[] = {
  889. { 0x51, SONYPI_EVENT_LID_CLOSED },
  890. { 0x50, SONYPI_EVENT_LID_OPENED },
  891. { 0, 0 }
  892. };
  893. /* The set of possible zoom events */
  894. static struct sonypi_event sonypi_zoomev[] = {
  895. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  896. { 0, 0 }
  897. };
  898. /* The set of possible thumbphrase events */
  899. static struct sonypi_event sonypi_thumbphraseev[] = {
  900. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  901. { 0, 0 }
  902. };
  903. /* The set of possible motioneye camera events */
  904. static struct sonypi_event sonypi_meyeev[] = {
  905. { 0x00, SONYPI_EVENT_MEYE_FACE },
  906. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  907. { 0, 0 }
  908. };
  909. /* The set of possible memorystick events */
  910. static struct sonypi_event sonypi_memorystickev[] = {
  911. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  912. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  913. { 0, 0 }
  914. };
  915. /* The set of possible battery events */
  916. static struct sonypi_event sonypi_batteryev[] = {
  917. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  918. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  919. { 0, 0 }
  920. };
  921. static struct sonypi_eventtypes {
  922. int model;
  923. u8 data;
  924. unsigned long mask;
  925. struct sonypi_event * events;
  926. } sony_pic_eventtypes[] = {
  927. { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
  928. { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  929. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
  930. { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  931. { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  932. { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  933. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  934. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  935. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  936. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  937. { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
  938. { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
  939. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  940. { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  941. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  942. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  943. { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  944. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
  945. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  946. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  947. { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  948. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  949. { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  950. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  951. { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
  952. { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  953. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  954. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  955. { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  956. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  957. { 0 }
  958. };
  959. static int sony_pic_detect_device_type(void)
  960. {
  961. struct pci_dev *pcidev;
  962. int model = 0;
  963. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  964. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  965. model = SONYPI_DEVICE_TYPE1;
  966. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  967. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  968. model = SONYPI_DEVICE_TYPE3;
  969. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  970. PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
  971. model = SONYPI_DEVICE_TYPE3;
  972. else
  973. model = SONYPI_DEVICE_TYPE2;
  974. if (pcidev)
  975. pci_dev_put(pcidev);
  976. printk(KERN_INFO DRV_PFX "detected Type%d model\n",
  977. model == SONYPI_DEVICE_TYPE1 ? 1 :
  978. model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  979. return model;
  980. }
  981. #define ITERATIONS_LONG 10000
  982. #define ITERATIONS_SHORT 10
  983. #define wait_on_command(command, iterations) { \
  984. unsigned int n = iterations; \
  985. while (--n && (command)) \
  986. udelay(1); \
  987. if (!n) \
  988. dprintk("command failed at %s : %s (line %d)\n", \
  989. __FILE__, __FUNCTION__, __LINE__); \
  990. }
  991. static u8 sony_pic_call1(u8 dev)
  992. {
  993. u8 v1, v2;
  994. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  995. ITERATIONS_LONG);
  996. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  997. v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
  998. v2 = inb_p(spic_dev.cur_ioport->io.minimum);
  999. dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
  1000. return v2;
  1001. }
  1002. static u8 sony_pic_call2(u8 dev, u8 fn)
  1003. {
  1004. u8 v1;
  1005. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  1006. ITERATIONS_LONG);
  1007. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  1008. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  1009. ITERATIONS_LONG);
  1010. outb(fn, spic_dev.cur_ioport->io.minimum);
  1011. v1 = inb_p(spic_dev.cur_ioport->io.minimum);
  1012. dprintk("sony_pic_call2: 0x%.4x\n", v1);
  1013. return v1;
  1014. }
  1015. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  1016. {
  1017. u8 v1;
  1018. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
  1019. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  1020. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
  1021. outb(fn, spic_dev.cur_ioport->io.minimum);
  1022. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
  1023. outb(v, spic_dev.cur_ioport->io.minimum);
  1024. v1 = inb_p(spic_dev.cur_ioport->io.minimum);
  1025. dprintk("sony_pic_call3: 0x%.4x\n", v1);
  1026. return v1;
  1027. }
  1028. /* camera tests and poweron/poweroff */
  1029. #define SONYPI_CAMERA_PICTURE 5
  1030. #define SONYPI_CAMERA_CONTROL 0x10
  1031. #define SONYPI_CAMERA_BRIGHTNESS 0
  1032. #define SONYPI_CAMERA_CONTRAST 1
  1033. #define SONYPI_CAMERA_HUE 2
  1034. #define SONYPI_CAMERA_COLOR 3
  1035. #define SONYPI_CAMERA_SHARPNESS 4
  1036. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  1037. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  1038. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  1039. #define SONYPI_CAMERA_MUTE_MASK 0x40
  1040. /* the rest don't need a loop until not 0xff */
  1041. #define SONYPI_CAMERA_AGC 6
  1042. #define SONYPI_CAMERA_AGC_MASK 0x30
  1043. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  1044. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  1045. #define SONYPI_CAMERA_CONTROL 0x10
  1046. #define SONYPI_CAMERA_STATUS 7
  1047. #define SONYPI_CAMERA_STATUS_READY 0x2
  1048. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  1049. #define SONYPI_DIRECTION_BACKWARDS 0x4
  1050. #define SONYPI_CAMERA_REVISION 8
  1051. #define SONYPI_CAMERA_ROMVERSION 9
  1052. static int __sony_pic_camera_ready(void)
  1053. {
  1054. u8 v;
  1055. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  1056. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  1057. }
  1058. static int __sony_pic_camera_off(void)
  1059. {
  1060. if (!camera) {
  1061. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1062. return -ENODEV;
  1063. }
  1064. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  1065. SONYPI_CAMERA_MUTE_MASK),
  1066. ITERATIONS_SHORT);
  1067. if (spic_dev.camera_power) {
  1068. sony_pic_call2(0x91, 0);
  1069. spic_dev.camera_power = 0;
  1070. }
  1071. return 0;
  1072. }
  1073. static int __sony_pic_camera_on(void)
  1074. {
  1075. int i, j, x;
  1076. if (!camera) {
  1077. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1078. return -ENODEV;
  1079. }
  1080. if (spic_dev.camera_power)
  1081. return 0;
  1082. for (j = 5; j > 0; j--) {
  1083. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  1084. msleep(10);
  1085. sony_pic_call1(0x93);
  1086. for (i = 400; i > 0; i--) {
  1087. if (__sony_pic_camera_ready())
  1088. break;
  1089. msleep(10);
  1090. }
  1091. if (i)
  1092. break;
  1093. }
  1094. if (j == 0) {
  1095. printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
  1096. return -ENODEV;
  1097. }
  1098. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  1099. 0x5a),
  1100. ITERATIONS_SHORT);
  1101. spic_dev.camera_power = 1;
  1102. return 0;
  1103. }
  1104. /* External camera command (exported to the motion eye v4l driver) */
  1105. int sony_pic_camera_command(int command, u8 value)
  1106. {
  1107. if (!camera)
  1108. return -EIO;
  1109. mutex_lock(&spic_dev.lock);
  1110. switch (command) {
  1111. case SONY_PIC_COMMAND_SETCAMERA:
  1112. if (value)
  1113. __sony_pic_camera_on();
  1114. else
  1115. __sony_pic_camera_off();
  1116. break;
  1117. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  1118. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  1119. ITERATIONS_SHORT);
  1120. break;
  1121. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  1122. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  1123. ITERATIONS_SHORT);
  1124. break;
  1125. case SONY_PIC_COMMAND_SETCAMERAHUE:
  1126. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  1127. ITERATIONS_SHORT);
  1128. break;
  1129. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  1130. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  1131. ITERATIONS_SHORT);
  1132. break;
  1133. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  1134. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  1135. ITERATIONS_SHORT);
  1136. break;
  1137. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  1138. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  1139. ITERATIONS_SHORT);
  1140. break;
  1141. case SONY_PIC_COMMAND_SETCAMERAAGC:
  1142. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  1143. ITERATIONS_SHORT);
  1144. break;
  1145. default:
  1146. printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
  1147. command);
  1148. break;
  1149. }
  1150. mutex_unlock(&spic_dev.lock);
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL(sony_pic_camera_command);
  1154. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  1155. static void sony_pic_set_wwanpower(u8 state)
  1156. {
  1157. state = !!state;
  1158. mutex_lock(&spic_dev.lock);
  1159. if (spic_dev.wwan_power == state) {
  1160. mutex_unlock(&spic_dev.lock);
  1161. return;
  1162. }
  1163. sony_pic_call2(0xB0, state);
  1164. spic_dev.wwan_power = state;
  1165. mutex_unlock(&spic_dev.lock);
  1166. }
  1167. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  1168. struct device_attribute *attr,
  1169. const char *buffer, size_t count)
  1170. {
  1171. unsigned long value;
  1172. if (count > 31)
  1173. return -EINVAL;
  1174. value = simple_strtoul(buffer, NULL, 10);
  1175. sony_pic_set_wwanpower(value);
  1176. return count;
  1177. }
  1178. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  1179. struct device_attribute *attr, char *buffer)
  1180. {
  1181. ssize_t count;
  1182. mutex_lock(&spic_dev.lock);
  1183. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  1184. mutex_unlock(&spic_dev.lock);
  1185. return count;
  1186. }
  1187. /* bluetooth subsystem power state */
  1188. static void __sony_pic_set_bluetoothpower(u8 state)
  1189. {
  1190. state = !!state;
  1191. if (spic_dev.bluetooth_power == state)
  1192. return;
  1193. sony_pic_call2(0x96, state);
  1194. sony_pic_call1(0x82);
  1195. spic_dev.bluetooth_power = state;
  1196. }
  1197. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  1198. struct device_attribute *attr,
  1199. const char *buffer, size_t count)
  1200. {
  1201. unsigned long value;
  1202. if (count > 31)
  1203. return -EINVAL;
  1204. value = simple_strtoul(buffer, NULL, 10);
  1205. mutex_lock(&spic_dev.lock);
  1206. __sony_pic_set_bluetoothpower(value);
  1207. mutex_unlock(&spic_dev.lock);
  1208. return count;
  1209. }
  1210. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  1211. struct device_attribute *attr, char *buffer)
  1212. {
  1213. ssize_t count = 0;
  1214. mutex_lock(&spic_dev.lock);
  1215. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  1216. mutex_unlock(&spic_dev.lock);
  1217. return count;
  1218. }
  1219. /* fan speed */
  1220. /* FAN0 information (reverse engineered from ACPI tables) */
  1221. #define SONY_PIC_FAN0_STATUS 0x93
  1222. static int sony_pic_set_fanspeed(unsigned long value)
  1223. {
  1224. return ec_write(SONY_PIC_FAN0_STATUS, value);
  1225. }
  1226. static int sony_pic_get_fanspeed(u8 *value)
  1227. {
  1228. return ec_read(SONY_PIC_FAN0_STATUS, value);
  1229. }
  1230. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  1231. struct device_attribute *attr,
  1232. const char *buffer, size_t count)
  1233. {
  1234. unsigned long value;
  1235. if (count > 31)
  1236. return -EINVAL;
  1237. value = simple_strtoul(buffer, NULL, 10);
  1238. if (sony_pic_set_fanspeed(value))
  1239. return -EIO;
  1240. return count;
  1241. }
  1242. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  1243. struct device_attribute *attr, char *buffer)
  1244. {
  1245. u8 value = 0;
  1246. if (sony_pic_get_fanspeed(&value))
  1247. return -EIO;
  1248. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  1249. }
  1250. #define SPIC_ATTR(_name, _mode) \
  1251. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  1252. _mode, sony_pic_## _name ##_show, \
  1253. sony_pic_## _name ##_store)
  1254. static SPIC_ATTR(bluetoothpower, 0644);
  1255. static SPIC_ATTR(wwanpower, 0644);
  1256. static SPIC_ATTR(fanspeed, 0644);
  1257. static struct attribute *spic_attributes[] = {
  1258. &spic_attr_bluetoothpower.attr,
  1259. &spic_attr_wwanpower.attr,
  1260. &spic_attr_fanspeed.attr,
  1261. NULL
  1262. };
  1263. static struct attribute_group spic_attribute_group = {
  1264. .attrs = spic_attributes
  1265. };
  1266. /******** SONYPI compatibility **********/
  1267. #ifdef CONFIG_SONYPI_COMPAT
  1268. /* battery / brightness / temperature addresses */
  1269. #define SONYPI_BAT_FLAGS 0x81
  1270. #define SONYPI_LCD_LIGHT 0x96
  1271. #define SONYPI_BAT1_PCTRM 0xa0
  1272. #define SONYPI_BAT1_LEFT 0xa2
  1273. #define SONYPI_BAT1_MAXRT 0xa4
  1274. #define SONYPI_BAT2_PCTRM 0xa8
  1275. #define SONYPI_BAT2_LEFT 0xaa
  1276. #define SONYPI_BAT2_MAXRT 0xac
  1277. #define SONYPI_BAT1_MAXTK 0xb0
  1278. #define SONYPI_BAT1_FULL 0xb2
  1279. #define SONYPI_BAT2_MAXTK 0xb8
  1280. #define SONYPI_BAT2_FULL 0xba
  1281. #define SONYPI_TEMP_STATUS 0xC1
  1282. struct sonypi_compat_s {
  1283. struct fasync_struct *fifo_async;
  1284. struct kfifo *fifo;
  1285. spinlock_t fifo_lock;
  1286. wait_queue_head_t fifo_proc_list;
  1287. atomic_t open_count;
  1288. };
  1289. static struct sonypi_compat_s sonypi_compat = {
  1290. .open_count = ATOMIC_INIT(0),
  1291. };
  1292. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  1293. {
  1294. int retval;
  1295. retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  1296. if (retval < 0)
  1297. return retval;
  1298. return 0;
  1299. }
  1300. static int sonypi_misc_release(struct inode *inode, struct file *file)
  1301. {
  1302. sonypi_misc_fasync(-1, file, 0);
  1303. atomic_dec(&sonypi_compat.open_count);
  1304. return 0;
  1305. }
  1306. static int sonypi_misc_open(struct inode *inode, struct file *file)
  1307. {
  1308. /* Flush input queue on first open */
  1309. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  1310. kfifo_reset(sonypi_compat.fifo);
  1311. return 0;
  1312. }
  1313. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  1314. size_t count, loff_t *pos)
  1315. {
  1316. ssize_t ret;
  1317. unsigned char c;
  1318. if ((kfifo_len(sonypi_compat.fifo) == 0) &&
  1319. (file->f_flags & O_NONBLOCK))
  1320. return -EAGAIN;
  1321. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  1322. kfifo_len(sonypi_compat.fifo) != 0);
  1323. if (ret)
  1324. return ret;
  1325. while (ret < count &&
  1326. (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
  1327. if (put_user(c, buf++))
  1328. return -EFAULT;
  1329. ret++;
  1330. }
  1331. if (ret > 0) {
  1332. struct inode *inode = file->f_path.dentry->d_inode;
  1333. inode->i_atime = current_fs_time(inode->i_sb);
  1334. }
  1335. return ret;
  1336. }
  1337. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  1338. {
  1339. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  1340. if (kfifo_len(sonypi_compat.fifo))
  1341. return POLLIN | POLLRDNORM;
  1342. return 0;
  1343. }
  1344. static int ec_read16(u8 addr, u16 *value)
  1345. {
  1346. u8 val_lb, val_hb;
  1347. if (ec_read(addr, &val_lb))
  1348. return -1;
  1349. if (ec_read(addr + 1, &val_hb))
  1350. return -1;
  1351. *value = val_lb | (val_hb << 8);
  1352. return 0;
  1353. }
  1354. static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
  1355. unsigned int cmd, unsigned long arg)
  1356. {
  1357. int ret = 0;
  1358. void __user *argp = (void __user *)arg;
  1359. u8 val8;
  1360. u16 val16;
  1361. int value;
  1362. mutex_lock(&spic_dev.lock);
  1363. switch (cmd) {
  1364. case SONYPI_IOCGBRT:
  1365. if (sony_backlight_device == NULL) {
  1366. ret = -EIO;
  1367. break;
  1368. }
  1369. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
  1370. ret = -EIO;
  1371. break;
  1372. }
  1373. val8 = ((value & 0xff) - 1) << 5;
  1374. if (copy_to_user(argp, &val8, sizeof(val8)))
  1375. ret = -EFAULT;
  1376. break;
  1377. case SONYPI_IOCSBRT:
  1378. if (sony_backlight_device == NULL) {
  1379. ret = -EIO;
  1380. break;
  1381. }
  1382. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1383. ret = -EFAULT;
  1384. break;
  1385. }
  1386. if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  1387. (val8 >> 5) + 1, NULL)) {
  1388. ret = -EIO;
  1389. break;
  1390. }
  1391. /* sync the backlight device status */
  1392. sony_backlight_device->props.brightness =
  1393. sony_backlight_get_brightness(sony_backlight_device);
  1394. break;
  1395. case SONYPI_IOCGBAT1CAP:
  1396. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  1397. ret = -EIO;
  1398. break;
  1399. }
  1400. if (copy_to_user(argp, &val16, sizeof(val16)))
  1401. ret = -EFAULT;
  1402. break;
  1403. case SONYPI_IOCGBAT1REM:
  1404. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  1405. ret = -EIO;
  1406. break;
  1407. }
  1408. if (copy_to_user(argp, &val16, sizeof(val16)))
  1409. ret = -EFAULT;
  1410. break;
  1411. case SONYPI_IOCGBAT2CAP:
  1412. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  1413. ret = -EIO;
  1414. break;
  1415. }
  1416. if (copy_to_user(argp, &val16, sizeof(val16)))
  1417. ret = -EFAULT;
  1418. break;
  1419. case SONYPI_IOCGBAT2REM:
  1420. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  1421. ret = -EIO;
  1422. break;
  1423. }
  1424. if (copy_to_user(argp, &val16, sizeof(val16)))
  1425. ret = -EFAULT;
  1426. break;
  1427. case SONYPI_IOCGBATFLAGS:
  1428. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  1429. ret = -EIO;
  1430. break;
  1431. }
  1432. val8 &= 0x07;
  1433. if (copy_to_user(argp, &val8, sizeof(val8)))
  1434. ret = -EFAULT;
  1435. break;
  1436. case SONYPI_IOCGBLUE:
  1437. val8 = spic_dev.bluetooth_power;
  1438. if (copy_to_user(argp, &val8, sizeof(val8)))
  1439. ret = -EFAULT;
  1440. break;
  1441. case SONYPI_IOCSBLUE:
  1442. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1443. ret = -EFAULT;
  1444. break;
  1445. }
  1446. __sony_pic_set_bluetoothpower(val8);
  1447. break;
  1448. /* FAN Controls */
  1449. case SONYPI_IOCGFAN:
  1450. if (sony_pic_get_fanspeed(&val8)) {
  1451. ret = -EIO;
  1452. break;
  1453. }
  1454. if (copy_to_user(argp, &val8, sizeof(val8)))
  1455. ret = -EFAULT;
  1456. break;
  1457. case SONYPI_IOCSFAN:
  1458. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1459. ret = -EFAULT;
  1460. break;
  1461. }
  1462. if (sony_pic_set_fanspeed(val8))
  1463. ret = -EIO;
  1464. break;
  1465. /* GET Temperature (useful under APM) */
  1466. case SONYPI_IOCGTEMP:
  1467. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  1468. ret = -EIO;
  1469. break;
  1470. }
  1471. if (copy_to_user(argp, &val8, sizeof(val8)))
  1472. ret = -EFAULT;
  1473. break;
  1474. default:
  1475. ret = -EINVAL;
  1476. }
  1477. mutex_unlock(&spic_dev.lock);
  1478. return ret;
  1479. }
  1480. static const struct file_operations sonypi_misc_fops = {
  1481. .owner = THIS_MODULE,
  1482. .read = sonypi_misc_read,
  1483. .poll = sonypi_misc_poll,
  1484. .open = sonypi_misc_open,
  1485. .release = sonypi_misc_release,
  1486. .fasync = sonypi_misc_fasync,
  1487. .ioctl = sonypi_misc_ioctl,
  1488. };
  1489. static struct miscdevice sonypi_misc_device = {
  1490. .minor = MISC_DYNAMIC_MINOR,
  1491. .name = "sonypi",
  1492. .fops = &sonypi_misc_fops,
  1493. };
  1494. static void sonypi_compat_report_event(u8 event)
  1495. {
  1496. kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
  1497. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  1498. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  1499. }
  1500. static int sonypi_compat_init(void)
  1501. {
  1502. int error;
  1503. spin_lock_init(&sonypi_compat.fifo_lock);
  1504. sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
  1505. &sonypi_compat.fifo_lock);
  1506. if (IS_ERR(sonypi_compat.fifo)) {
  1507. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  1508. return PTR_ERR(sonypi_compat.fifo);
  1509. }
  1510. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  1511. if (minor != -1)
  1512. sonypi_misc_device.minor = minor;
  1513. error = misc_register(&sonypi_misc_device);
  1514. if (error) {
  1515. printk(KERN_ERR DRV_PFX "misc_register failed\n");
  1516. goto err_free_kfifo;
  1517. }
  1518. if (minor == -1)
  1519. printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
  1520. sonypi_misc_device.minor);
  1521. return 0;
  1522. err_free_kfifo:
  1523. kfifo_free(sonypi_compat.fifo);
  1524. return error;
  1525. }
  1526. static void sonypi_compat_exit(void)
  1527. {
  1528. misc_deregister(&sonypi_misc_device);
  1529. kfifo_free(sonypi_compat.fifo);
  1530. }
  1531. #else
  1532. static int sonypi_compat_init(void) { return 0; }
  1533. static void sonypi_compat_exit(void) { }
  1534. static void sonypi_compat_report_event(u8 event) { }
  1535. #endif /* CONFIG_SONYPI_COMPAT */
  1536. /*
  1537. * ACPI callbacks
  1538. */
  1539. static acpi_status
  1540. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  1541. {
  1542. u32 i;
  1543. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  1544. switch (resource->type) {
  1545. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  1546. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  1547. return AE_OK;
  1548. case ACPI_RESOURCE_TYPE_IRQ:
  1549. {
  1550. struct acpi_resource_irq *p = &resource->data.irq;
  1551. struct sony_pic_irq *interrupt = NULL;
  1552. if (!p || !p->interrupt_count) {
  1553. /*
  1554. * IRQ descriptors may have no IRQ# bits set,
  1555. * particularly those those w/ _STA disabled
  1556. */
  1557. dprintk("Blank IRQ resource\n");
  1558. return AE_OK;
  1559. }
  1560. for (i = 0; i < p->interrupt_count; i++) {
  1561. if (!p->interrupts[i]) {
  1562. printk(KERN_WARNING DRV_PFX
  1563. "Invalid IRQ %d\n",
  1564. p->interrupts[i]);
  1565. continue;
  1566. }
  1567. interrupt = kzalloc(sizeof(*interrupt),
  1568. GFP_KERNEL);
  1569. if (!interrupt)
  1570. return AE_ERROR;
  1571. list_add_tail(&interrupt->list, &dev->interrupts);
  1572. interrupt->irq.triggering = p->triggering;
  1573. interrupt->irq.polarity = p->polarity;
  1574. interrupt->irq.sharable = p->sharable;
  1575. interrupt->irq.interrupt_count = 1;
  1576. interrupt->irq.interrupts[0] = p->interrupts[i];
  1577. }
  1578. return AE_OK;
  1579. }
  1580. case ACPI_RESOURCE_TYPE_IO:
  1581. {
  1582. struct acpi_resource_io *io = &resource->data.io;
  1583. struct sony_pic_ioport *ioport = NULL;
  1584. if (!io) {
  1585. dprintk("Blank IO resource\n");
  1586. return AE_OK;
  1587. }
  1588. ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  1589. if (!ioport)
  1590. return AE_ERROR;
  1591. list_add_tail(&ioport->list, &dev->ioports);
  1592. memcpy(&ioport->io, io, sizeof(*io));
  1593. return AE_OK;
  1594. }
  1595. default:
  1596. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  1597. resource->type);
  1598. case ACPI_RESOURCE_TYPE_END_TAG:
  1599. return AE_OK;
  1600. }
  1601. return AE_CTRL_TERMINATE;
  1602. }
  1603. static int sony_pic_possible_resources(struct acpi_device *device)
  1604. {
  1605. int result = 0;
  1606. acpi_status status = AE_OK;
  1607. if (!device)
  1608. return -EINVAL;
  1609. /* get device status */
  1610. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  1611. dprintk("Evaluating _STA\n");
  1612. result = acpi_bus_get_status(device);
  1613. if (result) {
  1614. printk(KERN_WARNING DRV_PFX "Unable to read status\n");
  1615. goto end;
  1616. }
  1617. if (!device->status.enabled)
  1618. dprintk("Device disabled\n");
  1619. else
  1620. dprintk("Device enabled\n");
  1621. /*
  1622. * Query and parse 'method'
  1623. */
  1624. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  1625. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  1626. sony_pic_read_possible_resource, &spic_dev);
  1627. if (ACPI_FAILURE(status)) {
  1628. printk(KERN_WARNING DRV_PFX
  1629. "Failure evaluating %s\n",
  1630. METHOD_NAME__PRS);
  1631. result = -ENODEV;
  1632. }
  1633. end:
  1634. return result;
  1635. }
  1636. /*
  1637. * Disable the spic device by calling its _DIS method
  1638. */
  1639. static int sony_pic_disable(struct acpi_device *device)
  1640. {
  1641. if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
  1642. return -ENXIO;
  1643. dprintk("Device disabled\n");
  1644. return 0;
  1645. }
  1646. /*
  1647. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  1648. *
  1649. * Call _SRS to set current resources
  1650. */
  1651. static int sony_pic_enable(struct acpi_device *device,
  1652. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  1653. {
  1654. acpi_status status;
  1655. int result = 0;
  1656. struct {
  1657. struct acpi_resource io_res;
  1658. struct acpi_resource irq_res;
  1659. struct acpi_resource end;
  1660. } *resource;
  1661. struct acpi_buffer buffer = { 0, NULL };
  1662. if (!ioport || !irq)
  1663. return -EINVAL;
  1664. /* init acpi_buffer */
  1665. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  1666. if (!resource)
  1667. return -ENOMEM;
  1668. buffer.length = sizeof(*resource) + 1;
  1669. buffer.pointer = resource;
  1670. /* setup io resource */
  1671. resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
  1672. resource->io_res.length = sizeof(struct acpi_resource);
  1673. memcpy(&resource->io_res.data.io, &ioport->io,
  1674. sizeof(struct acpi_resource_io));
  1675. /* setup irq resource */
  1676. resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
  1677. resource->irq_res.length = sizeof(struct acpi_resource);
  1678. memcpy(&resource->irq_res.data.irq, &irq->irq,
  1679. sizeof(struct acpi_resource_irq));
  1680. /* we requested a shared irq */
  1681. resource->irq_res.data.irq.sharable = ACPI_SHARED;
  1682. resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
  1683. /* Attempt to set the resource */
  1684. dprintk("Evaluating _SRS\n");
  1685. status = acpi_set_current_resources(device->handle, &buffer);
  1686. /* check for total failure */
  1687. if (ACPI_FAILURE(status)) {
  1688. printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
  1689. result = -ENODEV;
  1690. goto end;
  1691. }
  1692. /* Necessary device initializations calls (from sonypi) */
  1693. sony_pic_call1(0x82);
  1694. sony_pic_call2(0x81, 0xff);
  1695. sony_pic_call1(compat ? 0x92 : 0x82);
  1696. end:
  1697. kfree(resource);
  1698. return result;
  1699. }
  1700. /*****************
  1701. *
  1702. * ISR: some event is available
  1703. *
  1704. *****************/
  1705. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  1706. {
  1707. int i, j;
  1708. u32 port_val = 0;
  1709. u8 ev = 0;
  1710. u8 data_mask = 0;
  1711. u8 device_event = 0;
  1712. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  1713. acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
  1714. dev->cur_ioport->io.address_length);
  1715. ev = port_val & SONY_PIC_EV_MASK;
  1716. data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
  1717. dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
  1718. port_val, ev, data_mask, dev->cur_ioport->io.minimum);
  1719. if (ev == 0x00 || ev == 0xff)
  1720. return IRQ_HANDLED;
  1721. for (i = 0; sony_pic_eventtypes[i].model; i++) {
  1722. if (spic_dev.model != sony_pic_eventtypes[i].model)
  1723. continue;
  1724. if ((data_mask & sony_pic_eventtypes[i].data) !=
  1725. sony_pic_eventtypes[i].data)
  1726. continue;
  1727. if (!(mask & sony_pic_eventtypes[i].mask))
  1728. continue;
  1729. for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
  1730. if (ev == sony_pic_eventtypes[i].events[j].data) {
  1731. device_event =
  1732. sony_pic_eventtypes[i].events[j].event;
  1733. goto found;
  1734. }
  1735. }
  1736. }
  1737. return IRQ_HANDLED;
  1738. found:
  1739. sony_laptop_report_input_event(device_event);
  1740. acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
  1741. sonypi_compat_report_event(device_event);
  1742. return IRQ_HANDLED;
  1743. }
  1744. /*****************
  1745. *
  1746. * ACPI driver
  1747. *
  1748. *****************/
  1749. static int sony_pic_remove(struct acpi_device *device, int type)
  1750. {
  1751. struct sony_pic_ioport *io, *tmp_io;
  1752. struct sony_pic_irq *irq, *tmp_irq;
  1753. sonypi_compat_exit();
  1754. if (sony_pic_disable(device)) {
  1755. printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
  1756. return -ENXIO;
  1757. }
  1758. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  1759. release_region(spic_dev.cur_ioport->io.minimum,
  1760. spic_dev.cur_ioport->io.address_length);
  1761. sony_laptop_remove_input();
  1762. /* pf attrs */
  1763. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  1764. sony_pf_remove();
  1765. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  1766. list_del(&io->list);
  1767. kfree(io);
  1768. }
  1769. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  1770. list_del(&irq->list);
  1771. kfree(irq);
  1772. }
  1773. spic_dev.cur_ioport = NULL;
  1774. spic_dev.cur_irq = NULL;
  1775. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  1776. return 0;
  1777. }
  1778. static int sony_pic_add(struct acpi_device *device)
  1779. {
  1780. int result;
  1781. struct sony_pic_ioport *io, *tmp_io;
  1782. struct sony_pic_irq *irq, *tmp_irq;
  1783. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  1784. SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  1785. spic_dev.acpi_dev = device;
  1786. strcpy(acpi_device_class(device), "sony/hotkey");
  1787. spic_dev.model = sony_pic_detect_device_type();
  1788. mutex_init(&spic_dev.lock);
  1789. /* read _PRS resources */
  1790. result = sony_pic_possible_resources(device);
  1791. if (result) {
  1792. printk(KERN_ERR DRV_PFX
  1793. "Unabe to read possible resources.\n");
  1794. goto err_free_resources;
  1795. }
  1796. /* setup input devices and helper fifo */
  1797. result = sony_laptop_setup_input();
  1798. if (result) {
  1799. printk(KERN_ERR DRV_PFX
  1800. "Unabe to create input devices.\n");
  1801. goto err_free_resources;
  1802. }
  1803. /* request io port */
  1804. list_for_each_entry(io, &spic_dev.ioports, list) {
  1805. if (request_region(io->io.minimum, io->io.address_length,
  1806. "Sony Programable I/O Device")) {
  1807. dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
  1808. io->io.minimum, io->io.maximum,
  1809. io->io.address_length);
  1810. spic_dev.cur_ioport = io;
  1811. break;
  1812. }
  1813. }
  1814. if (!spic_dev.cur_ioport) {
  1815. printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
  1816. result = -ENODEV;
  1817. goto err_remove_input;
  1818. }
  1819. /* request IRQ */
  1820. list_for_each_entry(irq, &spic_dev.interrupts, list) {
  1821. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  1822. IRQF_SHARED, "sony-laptop", &spic_dev)) {
  1823. dprintk("IRQ: %d - triggering: %d - "
  1824. "polarity: %d - shr: %d\n",
  1825. irq->irq.interrupts[0],
  1826. irq->irq.triggering,
  1827. irq->irq.polarity,
  1828. irq->irq.sharable);
  1829. spic_dev.cur_irq = irq;
  1830. break;
  1831. }
  1832. }
  1833. if (!spic_dev.cur_irq) {
  1834. printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
  1835. result = -ENODEV;
  1836. goto err_release_region;
  1837. }
  1838. /* set resource status _SRS */
  1839. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  1840. if (result) {
  1841. printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
  1842. goto err_free_irq;
  1843. }
  1844. spic_dev.bluetooth_power = -1;
  1845. /* create device attributes */
  1846. result = sony_pf_add();
  1847. if (result)
  1848. goto err_disable_device;
  1849. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  1850. if (result)
  1851. goto err_remove_pf;
  1852. if (sonypi_compat_init())
  1853. goto err_remove_pf;
  1854. return 0;
  1855. err_remove_pf:
  1856. sony_pf_remove();
  1857. err_disable_device:
  1858. sony_pic_disable(device);
  1859. err_free_irq:
  1860. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  1861. err_release_region:
  1862. release_region(spic_dev.cur_ioport->io.minimum,
  1863. spic_dev.cur_ioport->io.address_length);
  1864. err_remove_input:
  1865. sony_laptop_remove_input();
  1866. err_free_resources:
  1867. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  1868. list_del(&io->list);
  1869. kfree(io);
  1870. }
  1871. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  1872. list_del(&irq->list);
  1873. kfree(irq);
  1874. }
  1875. spic_dev.cur_ioport = NULL;
  1876. spic_dev.cur_irq = NULL;
  1877. return result;
  1878. }
  1879. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  1880. {
  1881. if (sony_pic_disable(device))
  1882. return -ENXIO;
  1883. return 0;
  1884. }
  1885. static int sony_pic_resume(struct acpi_device *device)
  1886. {
  1887. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  1888. return 0;
  1889. }
  1890. static struct acpi_driver sony_pic_driver = {
  1891. .name = SONY_PIC_DRIVER_NAME,
  1892. .class = SONY_PIC_CLASS,
  1893. .ids = SONY_PIC_HID,
  1894. .owner = THIS_MODULE,
  1895. .ops = {
  1896. .add = sony_pic_add,
  1897. .remove = sony_pic_remove,
  1898. .suspend = sony_pic_suspend,
  1899. .resume = sony_pic_resume,
  1900. },
  1901. };
  1902. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  1903. {
  1904. .ident = "Sony Vaio",
  1905. .matches = {
  1906. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1907. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  1908. },
  1909. },
  1910. {
  1911. .ident = "Sony Vaio",
  1912. .matches = {
  1913. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1914. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  1915. },
  1916. },
  1917. { }
  1918. };
  1919. static int __init sony_laptop_init(void)
  1920. {
  1921. int result;
  1922. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  1923. result = acpi_bus_register_driver(&sony_pic_driver);
  1924. if (result) {
  1925. printk(KERN_ERR DRV_PFX
  1926. "Unable to register SPIC driver.");
  1927. goto out;
  1928. }
  1929. }
  1930. result = acpi_bus_register_driver(&sony_nc_driver);
  1931. if (result) {
  1932. printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
  1933. goto out_unregister_pic;
  1934. }
  1935. return 0;
  1936. out_unregister_pic:
  1937. if (!no_spic)
  1938. acpi_bus_unregister_driver(&sony_pic_driver);
  1939. out:
  1940. return result;
  1941. }
  1942. static void __exit sony_laptop_exit(void)
  1943. {
  1944. acpi_bus_unregister_driver(&sony_nc_driver);
  1945. if (!no_spic)
  1946. acpi_bus_unregister_driver(&sony_pic_driver);
  1947. }
  1948. module_init(sony_laptop_init);
  1949. module_exit(sony_laptop_exit);