sony-laptop.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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. #define DRV_PFX "sony-laptop: "
  65. #define LOG_PFX KERN_WARNING DRV_PFX
  66. #define dprintk(msg...) do { \
  67. if (debug) printk(LOG_PFX msg); \
  68. } while (0)
  69. #define SONY_LAPTOP_DRIVER_VERSION "0.5"
  70. #define SONY_NC_CLASS "sony-nc"
  71. #define SONY_NC_HID "SNY5001"
  72. #define SONY_NC_DRIVER_NAME "Sony Notebook Control"
  73. #define SONY_PIC_CLASS "sony-pic"
  74. #define SONY_PIC_HID "SNY6001"
  75. #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control"
  76. MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
  77. MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
  78. MODULE_LICENSE("GPL");
  79. MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
  80. static int debug;
  81. module_param(debug, int, 0);
  82. MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
  83. "the development of this driver");
  84. static int no_spic; /* = 0 */
  85. module_param(no_spic, int, 0444);
  86. MODULE_PARM_DESC(no_spic,
  87. "set this if you don't want to enable the SPIC device");
  88. static int compat; /* = 0 */
  89. module_param(compat, int, 0444);
  90. MODULE_PARM_DESC(compat,
  91. "set this if you want to enable backward compatibility mode for SPIC");
  92. static unsigned long mask = 0xffffffff;
  93. module_param(mask, ulong, 0644);
  94. MODULE_PARM_DESC(mask,
  95. "set this to the mask of event you want to enable (see doc)");
  96. /*********** Input Devices ***********/
  97. #define SONY_LAPTOP_BUF_SIZE 128
  98. struct sony_laptop_input_s {
  99. atomic_t users;
  100. struct input_dev *jog_dev;
  101. struct input_dev *key_dev;
  102. struct kfifo *fifo;
  103. spinlock_t fifo_lock;
  104. struct workqueue_struct *wq;
  105. };
  106. static struct sony_laptop_input_s sony_laptop_input = {
  107. .users = ATOMIC_INIT(0),
  108. };
  109. struct sony_laptop_keypress {
  110. struct input_dev *dev;
  111. int key;
  112. };
  113. /* Correspondance table between sonypi events and input layer events */
  114. static struct {
  115. int sonypiev;
  116. int inputev;
  117. } sony_laptop_inputkeys[] = {
  118. { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
  119. { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
  120. { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
  121. { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
  122. { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
  123. { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
  124. { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
  125. { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
  126. { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
  127. { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
  128. { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
  129. { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
  130. { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
  131. { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
  132. { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
  133. { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
  134. { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
  135. { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
  136. { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
  137. { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
  138. { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
  139. { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
  140. { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
  141. { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
  142. { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
  143. { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
  144. { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
  145. { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
  146. { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
  147. { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
  148. { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
  149. { 0, 0 },
  150. };
  151. /* release buttons after a short delay if pressed */
  152. static void do_sony_laptop_release_key(struct work_struct *work)
  153. {
  154. struct sony_laptop_keypress kp;
  155. while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
  156. sizeof(kp)) == sizeof(kp)) {
  157. msleep(10);
  158. input_report_key(kp.dev, kp.key, 0);
  159. input_sync(kp.dev);
  160. }
  161. }
  162. static DECLARE_WORK(sony_laptop_release_key_work,
  163. do_sony_laptop_release_key);
  164. /* forward event to the input subsytem */
  165. static void sony_laptop_report_input_event(u8 event)
  166. {
  167. struct input_dev *jog_dev = sony_laptop_input.jog_dev;
  168. struct input_dev *key_dev = sony_laptop_input.key_dev;
  169. struct sony_laptop_keypress kp = { NULL };
  170. int i;
  171. if (event == SONYPI_EVENT_FNKEY_RELEASED) {
  172. /* Nothing, not all VAIOs generate this event */
  173. return;
  174. }
  175. /* report events */
  176. switch (event) {
  177. /* jog_dev events */
  178. case SONYPI_EVENT_JOGDIAL_UP:
  179. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  180. input_report_rel(jog_dev, REL_WHEEL, 1);
  181. input_sync(jog_dev);
  182. return;
  183. case SONYPI_EVENT_JOGDIAL_DOWN:
  184. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  185. input_report_rel(jog_dev, REL_WHEEL, -1);
  186. input_sync(jog_dev);
  187. return;
  188. /* key_dev events */
  189. case SONYPI_EVENT_JOGDIAL_PRESSED:
  190. kp.key = BTN_MIDDLE;
  191. kp.dev = jog_dev;
  192. break;
  193. default:
  194. for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
  195. if (event == sony_laptop_inputkeys[i].sonypiev) {
  196. kp.dev = key_dev;
  197. kp.key = sony_laptop_inputkeys[i].inputev;
  198. break;
  199. }
  200. break;
  201. }
  202. if (kp.dev) {
  203. input_report_key(kp.dev, kp.key, 1);
  204. input_sync(kp.dev);
  205. kfifo_put(sony_laptop_input.fifo,
  206. (unsigned char *)&kp, sizeof(kp));
  207. if (!work_pending(&sony_laptop_release_key_work))
  208. queue_work(sony_laptop_input.wq,
  209. &sony_laptop_release_key_work);
  210. } else
  211. dprintk("unknown input event %.2x\n", event);
  212. }
  213. static int sony_laptop_setup_input(void)
  214. {
  215. struct input_dev *jog_dev;
  216. struct input_dev *key_dev;
  217. int i;
  218. int error;
  219. /* don't run again if already initialized */
  220. if (atomic_add_return(1, &sony_laptop_input.users) > 1)
  221. return 0;
  222. /* kfifo */
  223. spin_lock_init(&sony_laptop_input.fifo_lock);
  224. sony_laptop_input.fifo =
  225. kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
  226. &sony_laptop_input.fifo_lock);
  227. if (IS_ERR(sony_laptop_input.fifo)) {
  228. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  229. error = PTR_ERR(sony_laptop_input.fifo);
  230. goto err_dec_users;
  231. }
  232. /* init workqueue */
  233. sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
  234. if (!sony_laptop_input.wq) {
  235. printk(KERN_ERR DRV_PFX
  236. "Unabe to create workqueue.\n");
  237. error = -ENXIO;
  238. goto err_free_kfifo;
  239. }
  240. /* input keys */
  241. key_dev = input_allocate_device();
  242. if (!key_dev) {
  243. error = -ENOMEM;
  244. goto err_destroy_wq;
  245. }
  246. key_dev->name = "Sony Vaio Keys";
  247. key_dev->id.bustype = BUS_ISA;
  248. key_dev->id.vendor = PCI_VENDOR_ID_SONY;
  249. /* Initialize the Input Drivers: special keys */
  250. key_dev->evbit[0] = BIT(EV_KEY);
  251. for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
  252. if (sony_laptop_inputkeys[i].inputev)
  253. set_bit(sony_laptop_inputkeys[i].inputev,
  254. key_dev->keybit);
  255. error = input_register_device(key_dev);
  256. if (error)
  257. goto err_free_keydev;
  258. sony_laptop_input.key_dev = key_dev;
  259. /* jogdial */
  260. jog_dev = input_allocate_device();
  261. if (!jog_dev) {
  262. error = -ENOMEM;
  263. goto err_unregister_keydev;
  264. }
  265. jog_dev->name = "Sony Vaio Jogdial";
  266. jog_dev->id.bustype = BUS_ISA;
  267. jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
  268. jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  269. jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
  270. jog_dev->relbit[0] = BIT(REL_WHEEL);
  271. error = input_register_device(jog_dev);
  272. if (error)
  273. goto err_free_jogdev;
  274. sony_laptop_input.jog_dev = jog_dev;
  275. return 0;
  276. err_free_jogdev:
  277. input_free_device(jog_dev);
  278. err_unregister_keydev:
  279. input_unregister_device(key_dev);
  280. /* to avoid kref underflow below at input_free_device */
  281. key_dev = NULL;
  282. err_free_keydev:
  283. input_free_device(key_dev);
  284. err_destroy_wq:
  285. destroy_workqueue(sony_laptop_input.wq);
  286. err_free_kfifo:
  287. kfifo_free(sony_laptop_input.fifo);
  288. err_dec_users:
  289. atomic_dec(&sony_laptop_input.users);
  290. return error;
  291. }
  292. static void sony_laptop_remove_input(void)
  293. {
  294. /* cleanup only after the last user has gone */
  295. if (!atomic_dec_and_test(&sony_laptop_input.users))
  296. return;
  297. /* flush workqueue first */
  298. flush_workqueue(sony_laptop_input.wq);
  299. /* destroy input devs */
  300. input_unregister_device(sony_laptop_input.key_dev);
  301. sony_laptop_input.key_dev = NULL;
  302. if (sony_laptop_input.jog_dev) {
  303. input_unregister_device(sony_laptop_input.jog_dev);
  304. sony_laptop_input.jog_dev = NULL;
  305. }
  306. destroy_workqueue(sony_laptop_input.wq);
  307. kfifo_free(sony_laptop_input.fifo);
  308. }
  309. /*********** Platform Device ***********/
  310. static atomic_t sony_pf_users = ATOMIC_INIT(0);
  311. static struct platform_driver sony_pf_driver = {
  312. .driver = {
  313. .name = "sony-laptop",
  314. .owner = THIS_MODULE,
  315. }
  316. };
  317. static struct platform_device *sony_pf_device;
  318. static int sony_pf_add(void)
  319. {
  320. int ret = 0;
  321. /* don't run again if already initialized */
  322. if (atomic_add_return(1, &sony_pf_users) > 1)
  323. return 0;
  324. ret = platform_driver_register(&sony_pf_driver);
  325. if (ret)
  326. goto out;
  327. sony_pf_device = platform_device_alloc("sony-laptop", -1);
  328. if (!sony_pf_device) {
  329. ret = -ENOMEM;
  330. goto out_platform_registered;
  331. }
  332. ret = platform_device_add(sony_pf_device);
  333. if (ret)
  334. goto out_platform_alloced;
  335. return 0;
  336. out_platform_alloced:
  337. platform_device_put(sony_pf_device);
  338. sony_pf_device = NULL;
  339. out_platform_registered:
  340. platform_driver_unregister(&sony_pf_driver);
  341. out:
  342. atomic_dec(&sony_pf_users);
  343. return ret;
  344. }
  345. static void sony_pf_remove(void)
  346. {
  347. /* deregister only after the last user has gone */
  348. if (!atomic_dec_and_test(&sony_pf_users))
  349. return;
  350. platform_device_del(sony_pf_device);
  351. platform_device_put(sony_pf_device);
  352. platform_driver_unregister(&sony_pf_driver);
  353. }
  354. /*********** SNC (SNY5001) Device ***********/
  355. /* the device uses 1-based values, while the backlight subsystem uses
  356. 0-based values */
  357. #define SONY_MAX_BRIGHTNESS 8
  358. #define SNC_VALIDATE_IN 0
  359. #define SNC_VALIDATE_OUT 1
  360. static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
  361. char *);
  362. static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
  363. const char *, size_t);
  364. static int boolean_validate(const int, const int);
  365. static int brightness_default_validate(const int, const int);
  366. struct sony_nc_value {
  367. char *name; /* name of the entry */
  368. char **acpiget; /* names of the ACPI get function */
  369. char **acpiset; /* names of the ACPI set function */
  370. int (*validate)(const int, const int); /* input/output validation */
  371. int value; /* current setting */
  372. int valid; /* Has ever been set */
  373. int debug; /* active only in debug mode ? */
  374. struct device_attribute devattr; /* sysfs atribute */
  375. };
  376. #define SNC_HANDLE_NAMES(_name, _values...) \
  377. static char *snc_##_name[] = { _values, NULL }
  378. #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
  379. { \
  380. .name = __stringify(_name), \
  381. .acpiget = _getters, \
  382. .acpiset = _setters, \
  383. .validate = _validate, \
  384. .debug = _debug, \
  385. .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
  386. }
  387. #define SNC_HANDLE_NULL { .name = NULL }
  388. SNC_HANDLE_NAMES(fnkey_get, "GHKE");
  389. SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
  390. SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
  391. SNC_HANDLE_NAMES(cdpower_get, "GCDP");
  392. SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
  393. SNC_HANDLE_NAMES(audiopower_get, "GAZP");
  394. SNC_HANDLE_NAMES(audiopower_set, "AZPW");
  395. SNC_HANDLE_NAMES(lanpower_get, "GLNP");
  396. SNC_HANDLE_NAMES(lanpower_set, "LNPW");
  397. SNC_HANDLE_NAMES(PID_get, "GPID");
  398. SNC_HANDLE_NAMES(CTR_get, "GCTR");
  399. SNC_HANDLE_NAMES(CTR_set, "SCTR");
  400. SNC_HANDLE_NAMES(PCR_get, "GPCR");
  401. SNC_HANDLE_NAMES(PCR_set, "SPCR");
  402. SNC_HANDLE_NAMES(CMI_get, "GCMI");
  403. SNC_HANDLE_NAMES(CMI_set, "SCMI");
  404. static struct sony_nc_value sony_nc_values[] = {
  405. SNC_HANDLE(brightness_default, snc_brightness_def_get,
  406. snc_brightness_def_set, brightness_default_validate, 0),
  407. SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
  408. SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
  409. SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
  410. boolean_validate, 0),
  411. SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
  412. boolean_validate, 1),
  413. /* unknown methods */
  414. SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
  415. SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
  416. SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
  417. SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
  418. SNC_HANDLE_NULL
  419. };
  420. static acpi_handle sony_nc_acpi_handle;
  421. static struct acpi_device *sony_nc_acpi_device = NULL;
  422. /*
  423. * acpi_evaluate_object wrappers
  424. */
  425. static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
  426. {
  427. struct acpi_buffer output;
  428. union acpi_object out_obj;
  429. acpi_status status;
  430. output.length = sizeof(out_obj);
  431. output.pointer = &out_obj;
  432. status = acpi_evaluate_object(handle, name, NULL, &output);
  433. if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
  434. *result = out_obj.integer.value;
  435. return 0;
  436. }
  437. printk(LOG_PFX "acpi_callreadfunc failed\n");
  438. return -1;
  439. }
  440. static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
  441. int *result)
  442. {
  443. struct acpi_object_list params;
  444. union acpi_object in_obj;
  445. struct acpi_buffer output;
  446. union acpi_object out_obj;
  447. acpi_status status;
  448. params.count = 1;
  449. params.pointer = &in_obj;
  450. in_obj.type = ACPI_TYPE_INTEGER;
  451. in_obj.integer.value = value;
  452. output.length = sizeof(out_obj);
  453. output.pointer = &out_obj;
  454. status = acpi_evaluate_object(handle, name, &params, &output);
  455. if (status == AE_OK) {
  456. if (result != NULL) {
  457. if (out_obj.type != ACPI_TYPE_INTEGER) {
  458. printk(LOG_PFX "acpi_evaluate_object bad "
  459. "return type\n");
  460. return -1;
  461. }
  462. *result = out_obj.integer.value;
  463. }
  464. return 0;
  465. }
  466. printk(LOG_PFX "acpi_evaluate_object failed\n");
  467. return -1;
  468. }
  469. /*
  470. * sony_nc_values input/output validate functions
  471. */
  472. /* brightness_default_validate:
  473. *
  474. * manipulate input output values to keep consistency with the
  475. * backlight framework for which brightness values are 0-based.
  476. */
  477. static int brightness_default_validate(const int direction, const int value)
  478. {
  479. switch (direction) {
  480. case SNC_VALIDATE_OUT:
  481. return value - 1;
  482. case SNC_VALIDATE_IN:
  483. if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
  484. return value + 1;
  485. }
  486. return -EINVAL;
  487. }
  488. /* boolean_validate:
  489. *
  490. * on input validate boolean values 0/1, on output just pass the
  491. * received value.
  492. */
  493. static int boolean_validate(const int direction, const int value)
  494. {
  495. if (direction == SNC_VALIDATE_IN) {
  496. if (value != 0 && value != 1)
  497. return -EINVAL;
  498. }
  499. return value;
  500. }
  501. /*
  502. * Sysfs show/store common to all sony_nc_values
  503. */
  504. static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
  505. char *buffer)
  506. {
  507. int value;
  508. struct sony_nc_value *item =
  509. container_of(attr, struct sony_nc_value, devattr);
  510. if (!*item->acpiget)
  511. return -EIO;
  512. if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
  513. return -EIO;
  514. if (item->validate)
  515. value = item->validate(SNC_VALIDATE_OUT, value);
  516. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  517. }
  518. static ssize_t sony_nc_sysfs_store(struct device *dev,
  519. struct device_attribute *attr,
  520. const char *buffer, size_t count)
  521. {
  522. int value;
  523. struct sony_nc_value *item =
  524. container_of(attr, struct sony_nc_value, devattr);
  525. if (!item->acpiset)
  526. return -EIO;
  527. if (count > 31)
  528. return -EINVAL;
  529. value = simple_strtoul(buffer, NULL, 10);
  530. if (item->validate)
  531. value = item->validate(SNC_VALIDATE_IN, value);
  532. if (value < 0)
  533. return value;
  534. if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
  535. return -EIO;
  536. item->value = value;
  537. item->valid = 1;
  538. return count;
  539. }
  540. /*
  541. * Backlight device
  542. */
  543. static int sony_backlight_update_status(struct backlight_device *bd)
  544. {
  545. return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  546. bd->props.brightness + 1, NULL);
  547. }
  548. static int sony_backlight_get_brightness(struct backlight_device *bd)
  549. {
  550. int value;
  551. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
  552. return 0;
  553. /* brightness levels are 1-based, while backlight ones are 0-based */
  554. return value - 1;
  555. }
  556. static struct backlight_device *sony_backlight_device;
  557. static struct backlight_ops sony_backlight_ops = {
  558. .update_status = sony_backlight_update_status,
  559. .get_brightness = sony_backlight_get_brightness,
  560. };
  561. /*
  562. * ACPI callbacks
  563. */
  564. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  565. {
  566. dprintk("sony_acpi_notify, event: %d\n", event);
  567. sony_laptop_report_input_event(event);
  568. acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
  569. }
  570. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  571. void *context, void **return_value)
  572. {
  573. struct acpi_namespace_node *node;
  574. union acpi_operand_object *operand;
  575. node = (struct acpi_namespace_node *)handle;
  576. operand = (union acpi_operand_object *)node->object;
  577. printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  578. (u32) operand->method.param_count);
  579. return AE_OK;
  580. }
  581. /*
  582. * ACPI device
  583. */
  584. static int sony_nc_resume(struct acpi_device *device)
  585. {
  586. struct sony_nc_value *item;
  587. for (item = sony_nc_values; item->name; item++) {
  588. int ret;
  589. if (!item->valid)
  590. continue;
  591. ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
  592. item->value, NULL);
  593. if (ret < 0) {
  594. printk("%s: %d\n", __FUNCTION__, ret);
  595. break;
  596. }
  597. }
  598. return 0;
  599. }
  600. static int sony_nc_add(struct acpi_device *device)
  601. {
  602. acpi_status status;
  603. int result = 0;
  604. acpi_handle handle;
  605. struct sony_nc_value *item;
  606. sony_nc_acpi_device = device;
  607. strcpy(acpi_device_class(device), "sony/hotkey");
  608. sony_nc_acpi_handle = device->handle;
  609. if (debug) {
  610. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
  611. 1, sony_walk_callback, NULL, NULL);
  612. if (ACPI_FAILURE(status)) {
  613. printk(LOG_PFX "unable to walk acpi resources\n");
  614. result = -ENODEV;
  615. goto outwalk;
  616. }
  617. }
  618. /* setup input devices and helper fifo */
  619. result = sony_laptop_setup_input();
  620. if (result) {
  621. printk(KERN_ERR DRV_PFX
  622. "Unabe to create input devices.\n");
  623. goto outwalk;
  624. }
  625. status = acpi_install_notify_handler(sony_nc_acpi_handle,
  626. ACPI_DEVICE_NOTIFY,
  627. sony_acpi_notify, NULL);
  628. if (ACPI_FAILURE(status)) {
  629. printk(LOG_PFX "unable to install notify handler\n");
  630. result = -ENODEV;
  631. goto outinput;
  632. }
  633. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
  634. sony_backlight_device = backlight_device_register("sony", NULL,
  635. NULL,
  636. &sony_backlight_ops);
  637. if (IS_ERR(sony_backlight_device)) {
  638. printk(LOG_PFX "unable to register backlight device\n");
  639. sony_backlight_device = NULL;
  640. } else {
  641. sony_backlight_device->props.brightness =
  642. sony_backlight_get_brightness
  643. (sony_backlight_device);
  644. sony_backlight_device->props.max_brightness =
  645. SONY_MAX_BRIGHTNESS - 1;
  646. }
  647. }
  648. if (sony_pf_add())
  649. goto outbacklight;
  650. /* create sony_pf sysfs attributes related to the SNC device */
  651. for (item = sony_nc_values; item->name; ++item) {
  652. if (!debug && item->debug)
  653. continue;
  654. /* find the available acpiget as described in the DSDT */
  655. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  656. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  657. *item->acpiget,
  658. &handle))) {
  659. dprintk("Found %s getter: %s\n",
  660. item->name, *item->acpiget);
  661. item->devattr.attr.mode |= S_IRUGO;
  662. break;
  663. }
  664. }
  665. /* find the available acpiset as described in the DSDT */
  666. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  667. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  668. *item->acpiset,
  669. &handle))) {
  670. dprintk("Found %s setter: %s\n",
  671. item->name, *item->acpiset);
  672. item->devattr.attr.mode |= S_IWUSR;
  673. break;
  674. }
  675. }
  676. if (item->devattr.attr.mode != 0) {
  677. result =
  678. device_create_file(&sony_pf_device->dev,
  679. &item->devattr);
  680. if (result)
  681. goto out_sysfs;
  682. }
  683. }
  684. printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
  685. return 0;
  686. out_sysfs:
  687. for (item = sony_nc_values; item->name; ++item) {
  688. device_remove_file(&sony_pf_device->dev, &item->devattr);
  689. }
  690. sony_pf_remove();
  691. outbacklight:
  692. if (sony_backlight_device)
  693. backlight_device_unregister(sony_backlight_device);
  694. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  695. ACPI_DEVICE_NOTIFY,
  696. sony_acpi_notify);
  697. if (ACPI_FAILURE(status))
  698. printk(LOG_PFX "unable to remove notify handler\n");
  699. outinput:
  700. sony_laptop_remove_input();
  701. outwalk:
  702. return result;
  703. }
  704. static int sony_nc_remove(struct acpi_device *device, int type)
  705. {
  706. acpi_status status;
  707. struct sony_nc_value *item;
  708. if (sony_backlight_device)
  709. backlight_device_unregister(sony_backlight_device);
  710. sony_nc_acpi_device = NULL;
  711. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  712. ACPI_DEVICE_NOTIFY,
  713. sony_acpi_notify);
  714. if (ACPI_FAILURE(status))
  715. printk(LOG_PFX "unable to remove notify handler\n");
  716. for (item = sony_nc_values; item->name; ++item) {
  717. device_remove_file(&sony_pf_device->dev, &item->devattr);
  718. }
  719. sony_pf_remove();
  720. sony_laptop_remove_input();
  721. printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
  722. return 0;
  723. }
  724. static struct acpi_driver sony_nc_driver = {
  725. .name = SONY_NC_DRIVER_NAME,
  726. .class = SONY_NC_CLASS,
  727. .ids = SONY_NC_HID,
  728. .owner = THIS_MODULE,
  729. .ops = {
  730. .add = sony_nc_add,
  731. .remove = sony_nc_remove,
  732. .resume = sony_nc_resume,
  733. },
  734. };
  735. /*********** SPIC (SNY6001) Device ***********/
  736. #define SONYPI_DEVICE_TYPE1 0x00000001
  737. #define SONYPI_DEVICE_TYPE2 0x00000002
  738. #define SONYPI_DEVICE_TYPE3 0x00000004
  739. #define SONY_PIC_EV_MASK 0xff
  740. struct sony_pic_ioport {
  741. struct acpi_resource_io io;
  742. struct list_head list;
  743. };
  744. struct sony_pic_irq {
  745. struct acpi_resource_irq irq;
  746. struct list_head list;
  747. };
  748. struct sony_pic_dev {
  749. int model;
  750. struct acpi_device *acpi_dev;
  751. struct sony_pic_irq *cur_irq;
  752. struct sony_pic_ioport *cur_ioport;
  753. struct list_head interrupts;
  754. struct list_head ioports;
  755. };
  756. static struct sony_pic_dev spic_dev = {
  757. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  758. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  759. };
  760. /* Event masks */
  761. #define SONYPI_JOGGER_MASK 0x00000001
  762. #define SONYPI_CAPTURE_MASK 0x00000002
  763. #define SONYPI_FNKEY_MASK 0x00000004
  764. #define SONYPI_BLUETOOTH_MASK 0x00000008
  765. #define SONYPI_PKEY_MASK 0x00000010
  766. #define SONYPI_BACK_MASK 0x00000020
  767. #define SONYPI_HELP_MASK 0x00000040
  768. #define SONYPI_LID_MASK 0x00000080
  769. #define SONYPI_ZOOM_MASK 0x00000100
  770. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  771. #define SONYPI_MEYE_MASK 0x00000400
  772. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  773. #define SONYPI_BATTERY_MASK 0x00001000
  774. #define SONYPI_WIRELESS_MASK 0x00002000
  775. struct sonypi_event {
  776. u8 data;
  777. u8 event;
  778. };
  779. /* The set of possible button release events */
  780. static struct sonypi_event sonypi_releaseev[] = {
  781. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  782. { 0, 0 }
  783. };
  784. /* The set of possible jogger events */
  785. static struct sonypi_event sonypi_joggerev[] = {
  786. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  787. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  788. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  789. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  790. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  791. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  792. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  793. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  794. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  795. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  796. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  797. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  798. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  799. { 0, 0 }
  800. };
  801. /* The set of possible capture button events */
  802. static struct sonypi_event sonypi_captureev[] = {
  803. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  804. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  805. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  806. { 0, 0 }
  807. };
  808. /* The set of possible fnkeys events */
  809. static struct sonypi_event sonypi_fnkeyev[] = {
  810. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  811. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  812. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  813. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  814. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  815. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  816. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  817. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  818. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  819. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  820. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  821. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  822. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  823. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  824. { 0x21, SONYPI_EVENT_FNKEY_1 },
  825. { 0x22, SONYPI_EVENT_FNKEY_2 },
  826. { 0x31, SONYPI_EVENT_FNKEY_D },
  827. { 0x32, SONYPI_EVENT_FNKEY_E },
  828. { 0x33, SONYPI_EVENT_FNKEY_F },
  829. { 0x34, SONYPI_EVENT_FNKEY_S },
  830. { 0x35, SONYPI_EVENT_FNKEY_B },
  831. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  832. { 0, 0 }
  833. };
  834. /* The set of possible program key events */
  835. static struct sonypi_event sonypi_pkeyev[] = {
  836. { 0x01, SONYPI_EVENT_PKEY_P1 },
  837. { 0x02, SONYPI_EVENT_PKEY_P2 },
  838. { 0x04, SONYPI_EVENT_PKEY_P3 },
  839. { 0x5c, SONYPI_EVENT_PKEY_P1 },
  840. { 0, 0 }
  841. };
  842. /* The set of possible bluetooth events */
  843. static struct sonypi_event sonypi_blueev[] = {
  844. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  845. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  846. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  847. { 0, 0 }
  848. };
  849. /* The set of possible wireless events */
  850. static struct sonypi_event sonypi_wlessev[] = {
  851. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  852. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  853. { 0, 0 }
  854. };
  855. /* The set of possible back button events */
  856. static struct sonypi_event sonypi_backev[] = {
  857. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  858. { 0, 0 }
  859. };
  860. /* The set of possible help button events */
  861. static struct sonypi_event sonypi_helpev[] = {
  862. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  863. { 0, 0 }
  864. };
  865. /* The set of possible lid events */
  866. static struct sonypi_event sonypi_lidev[] = {
  867. { 0x51, SONYPI_EVENT_LID_CLOSED },
  868. { 0x50, SONYPI_EVENT_LID_OPENED },
  869. { 0, 0 }
  870. };
  871. /* The set of possible zoom events */
  872. static struct sonypi_event sonypi_zoomev[] = {
  873. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  874. { 0, 0 }
  875. };
  876. /* The set of possible thumbphrase events */
  877. static struct sonypi_event sonypi_thumbphraseev[] = {
  878. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  879. { 0, 0 }
  880. };
  881. /* The set of possible motioneye camera events */
  882. static struct sonypi_event sonypi_meyeev[] = {
  883. { 0x00, SONYPI_EVENT_MEYE_FACE },
  884. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  885. { 0, 0 }
  886. };
  887. /* The set of possible memorystick events */
  888. static struct sonypi_event sonypi_memorystickev[] = {
  889. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  890. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  891. { 0, 0 }
  892. };
  893. /* The set of possible battery events */
  894. static struct sonypi_event sonypi_batteryev[] = {
  895. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  896. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  897. { 0, 0 }
  898. };
  899. static struct sonypi_eventtypes {
  900. int model;
  901. u8 data;
  902. unsigned long mask;
  903. struct sonypi_event * events;
  904. } sony_pic_eventtypes[] = {
  905. { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
  906. { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  907. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
  908. { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  909. { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  910. { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  911. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  912. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  913. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  914. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  915. { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
  916. { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
  917. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  918. { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  919. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  920. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  921. { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  922. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
  923. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  924. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  925. { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  926. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  927. { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  928. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  929. { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
  930. { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  931. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  932. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  933. { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  934. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  935. { 0 }
  936. };
  937. static int sony_pic_detect_device_type(void)
  938. {
  939. struct pci_dev *pcidev;
  940. int model = 0;
  941. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  942. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  943. model = SONYPI_DEVICE_TYPE1;
  944. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  945. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  946. model = SONYPI_DEVICE_TYPE3;
  947. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  948. PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
  949. model = SONYPI_DEVICE_TYPE3;
  950. else
  951. model = SONYPI_DEVICE_TYPE2;
  952. if (pcidev)
  953. pci_dev_put(pcidev);
  954. printk(KERN_INFO DRV_PFX "detected Type%d model\n",
  955. model == SONYPI_DEVICE_TYPE1 ? 1 :
  956. model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  957. return model;
  958. }
  959. #define ITERATIONS_LONG 10000
  960. #define ITERATIONS_SHORT 10
  961. #define wait_on_command(command, iterations) { \
  962. unsigned int n = iterations; \
  963. while (--n && (command)) \
  964. udelay(1); \
  965. if (!n) \
  966. dprintk("command failed at %s : %s (line %d)\n", \
  967. __FILE__, __FUNCTION__, __LINE__); \
  968. }
  969. static u8 sony_pic_call1(u8 dev)
  970. {
  971. u8 v1, v2;
  972. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  973. ITERATIONS_LONG);
  974. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  975. v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
  976. v2 = inb_p(spic_dev.cur_ioport->io.minimum);
  977. dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
  978. return v2;
  979. }
  980. static u8 sony_pic_call2(u8 dev, u8 fn)
  981. {
  982. u8 v1;
  983. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  984. ITERATIONS_LONG);
  985. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  986. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  987. ITERATIONS_LONG);
  988. outb(fn, spic_dev.cur_ioport->io.minimum);
  989. v1 = inb_p(spic_dev.cur_ioport->io.minimum);
  990. dprintk("sony_pic_call2: 0x%.4x\n", v1);
  991. return v1;
  992. }
  993. /*
  994. * ACPI callbacks
  995. */
  996. static acpi_status
  997. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  998. {
  999. u32 i;
  1000. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  1001. switch (resource->type) {
  1002. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  1003. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  1004. return AE_OK;
  1005. case ACPI_RESOURCE_TYPE_IRQ:
  1006. {
  1007. struct acpi_resource_irq *p = &resource->data.irq;
  1008. struct sony_pic_irq *interrupt = NULL;
  1009. if (!p || !p->interrupt_count) {
  1010. /*
  1011. * IRQ descriptors may have no IRQ# bits set,
  1012. * particularly those those w/ _STA disabled
  1013. */
  1014. dprintk("Blank IRQ resource\n");
  1015. return AE_OK;
  1016. }
  1017. for (i = 0; i < p->interrupt_count; i++) {
  1018. if (!p->interrupts[i]) {
  1019. printk(KERN_WARNING DRV_PFX
  1020. "Invalid IRQ %d\n",
  1021. p->interrupts[i]);
  1022. continue;
  1023. }
  1024. interrupt = kzalloc(sizeof(*interrupt),
  1025. GFP_KERNEL);
  1026. if (!interrupt)
  1027. return AE_ERROR;
  1028. list_add(&interrupt->list, &dev->interrupts);
  1029. interrupt->irq.triggering = p->triggering;
  1030. interrupt->irq.polarity = p->polarity;
  1031. interrupt->irq.sharable = p->sharable;
  1032. interrupt->irq.interrupt_count = 1;
  1033. interrupt->irq.interrupts[0] = p->interrupts[i];
  1034. }
  1035. return AE_OK;
  1036. }
  1037. case ACPI_RESOURCE_TYPE_IO:
  1038. {
  1039. struct acpi_resource_io *io = &resource->data.io;
  1040. struct sony_pic_ioport *ioport = NULL;
  1041. if (!io) {
  1042. dprintk("Blank IO resource\n");
  1043. return AE_OK;
  1044. }
  1045. ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  1046. if (!ioport)
  1047. return AE_ERROR;
  1048. list_add(&ioport->list, &dev->ioports);
  1049. memcpy(&ioport->io, io, sizeof(*io));
  1050. return AE_OK;
  1051. }
  1052. default:
  1053. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  1054. resource->type);
  1055. case ACPI_RESOURCE_TYPE_END_TAG:
  1056. return AE_OK;
  1057. }
  1058. return AE_CTRL_TERMINATE;
  1059. }
  1060. static int sony_pic_possible_resources(struct acpi_device *device)
  1061. {
  1062. int result = 0;
  1063. acpi_status status = AE_OK;
  1064. if (!device)
  1065. return -EINVAL;
  1066. /* get device status */
  1067. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  1068. dprintk("Evaluating _STA\n");
  1069. result = acpi_bus_get_status(device);
  1070. if (result) {
  1071. printk(KERN_WARNING DRV_PFX "Unable to read status\n");
  1072. goto end;
  1073. }
  1074. if (!device->status.enabled)
  1075. dprintk("Device disabled\n");
  1076. else
  1077. dprintk("Device enabled\n");
  1078. /*
  1079. * Query and parse 'method'
  1080. */
  1081. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  1082. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  1083. sony_pic_read_possible_resource, &spic_dev);
  1084. if (ACPI_FAILURE(status)) {
  1085. printk(KERN_WARNING DRV_PFX
  1086. "Failure evaluating %s\n",
  1087. METHOD_NAME__PRS);
  1088. result = -ENODEV;
  1089. }
  1090. end:
  1091. return result;
  1092. }
  1093. /*
  1094. * Disable the spic device by calling its _DIS method
  1095. */
  1096. static int sony_pic_disable(struct acpi_device *device)
  1097. {
  1098. if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
  1099. return -ENXIO;
  1100. dprintk("Device disabled\n");
  1101. return 0;
  1102. }
  1103. /*
  1104. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  1105. *
  1106. * Call _SRS to set current resources
  1107. */
  1108. static int sony_pic_enable(struct acpi_device *device,
  1109. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  1110. {
  1111. acpi_status status;
  1112. int result = 0;
  1113. struct {
  1114. struct acpi_resource io_res;
  1115. struct acpi_resource irq_res;
  1116. struct acpi_resource end;
  1117. } *resource;
  1118. struct acpi_buffer buffer = { 0, NULL };
  1119. if (!ioport || !irq)
  1120. return -EINVAL;
  1121. /* init acpi_buffer */
  1122. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  1123. if (!resource)
  1124. return -ENOMEM;
  1125. buffer.length = sizeof(*resource) + 1;
  1126. buffer.pointer = resource;
  1127. /* setup io resource */
  1128. resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
  1129. resource->io_res.length = sizeof(struct acpi_resource);
  1130. memcpy(&resource->io_res.data.io, &ioport->io,
  1131. sizeof(struct acpi_resource_io));
  1132. /* setup irq resource */
  1133. resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
  1134. resource->irq_res.length = sizeof(struct acpi_resource);
  1135. memcpy(&resource->irq_res.data.irq, &irq->irq,
  1136. sizeof(struct acpi_resource_irq));
  1137. /* we requested a shared irq */
  1138. resource->irq_res.data.irq.sharable = ACPI_SHARED;
  1139. resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
  1140. /* Attempt to set the resource */
  1141. dprintk("Evaluating _SRS\n");
  1142. status = acpi_set_current_resources(device->handle, &buffer);
  1143. /* check for total failure */
  1144. if (ACPI_FAILURE(status)) {
  1145. printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
  1146. result = -ENODEV;
  1147. goto end;
  1148. }
  1149. /* Necessary device initializations calls (from sonypi) */
  1150. sony_pic_call1(0x82);
  1151. sony_pic_call2(0x81, 0xff);
  1152. sony_pic_call1(compat ? 0x92 : 0x82);
  1153. end:
  1154. kfree(resource);
  1155. return result;
  1156. }
  1157. /*****************
  1158. *
  1159. * ISR: some event is available
  1160. *
  1161. *****************/
  1162. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  1163. {
  1164. int i, j;
  1165. u32 port_val = 0;
  1166. u8 ev = 0;
  1167. u8 data_mask = 0;
  1168. u8 device_event = 0;
  1169. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  1170. acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
  1171. dev->cur_ioport->io.address_length);
  1172. ev = port_val & SONY_PIC_EV_MASK;
  1173. data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
  1174. dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
  1175. port_val, ev, data_mask, dev->cur_ioport->io.minimum);
  1176. if (ev == 0x00 || ev == 0xff)
  1177. return IRQ_HANDLED;
  1178. for (i = 0; sony_pic_eventtypes[i].model; i++) {
  1179. if (spic_dev.model != sony_pic_eventtypes[i].model)
  1180. continue;
  1181. if ((data_mask & sony_pic_eventtypes[i].data) !=
  1182. sony_pic_eventtypes[i].data)
  1183. continue;
  1184. if (!(mask & sony_pic_eventtypes[i].mask))
  1185. continue;
  1186. for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
  1187. if (ev == sony_pic_eventtypes[i].events[j].data) {
  1188. device_event =
  1189. sony_pic_eventtypes[i].events[j].event;
  1190. goto found;
  1191. }
  1192. }
  1193. }
  1194. return IRQ_HANDLED;
  1195. found:
  1196. sony_laptop_report_input_event(device_event);
  1197. acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
  1198. return IRQ_HANDLED;
  1199. }
  1200. /*****************
  1201. *
  1202. * ACPI driver
  1203. *
  1204. *****************/
  1205. static int sony_pic_remove(struct acpi_device *device, int type)
  1206. {
  1207. struct sony_pic_ioport *io, *tmp_io;
  1208. struct sony_pic_irq *irq, *tmp_irq;
  1209. if (sony_pic_disable(device)) {
  1210. printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
  1211. return -ENXIO;
  1212. }
  1213. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  1214. release_region(spic_dev.cur_ioport->io.minimum,
  1215. spic_dev.cur_ioport->io.address_length);
  1216. sony_laptop_remove_input();
  1217. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  1218. list_del(&io->list);
  1219. kfree(io);
  1220. }
  1221. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  1222. list_del(&irq->list);
  1223. kfree(irq);
  1224. }
  1225. spic_dev.cur_ioport = NULL;
  1226. spic_dev.cur_irq = NULL;
  1227. dprintk("removed.\n");
  1228. return 0;
  1229. }
  1230. static int sony_pic_add(struct acpi_device *device)
  1231. {
  1232. int result;
  1233. struct sony_pic_ioport *io, *tmp_io;
  1234. struct sony_pic_irq *irq, *tmp_irq;
  1235. printk(KERN_INFO DRV_PFX
  1236. "Sony Programmable I/O Controller Driver v%s.\n",
  1237. SONY_LAPTOP_DRIVER_VERSION);
  1238. spic_dev.acpi_dev = device;
  1239. strcpy(acpi_device_class(device), "sony/hotkey");
  1240. spic_dev.model = sony_pic_detect_device_type();
  1241. /* read _PRS resources */
  1242. result = sony_pic_possible_resources(device);
  1243. if (result) {
  1244. printk(KERN_ERR DRV_PFX
  1245. "Unabe to read possible resources.\n");
  1246. goto err_free_resources;
  1247. }
  1248. /* setup input devices and helper fifo */
  1249. result = sony_laptop_setup_input();
  1250. if (result) {
  1251. printk(KERN_ERR DRV_PFX
  1252. "Unabe to create input devices.\n");
  1253. goto err_free_resources;
  1254. }
  1255. /* request io port */
  1256. list_for_each_entry(io, &spic_dev.ioports, list) {
  1257. if (request_region(io->io.minimum, io->io.address_length,
  1258. "Sony Programable I/O Device")) {
  1259. dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
  1260. io->io.minimum, io->io.maximum,
  1261. io->io.address_length);
  1262. spic_dev.cur_ioport = io;
  1263. break;
  1264. }
  1265. }
  1266. if (!spic_dev.cur_ioport) {
  1267. printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
  1268. result = -ENODEV;
  1269. goto err_remove_input;
  1270. }
  1271. /* request IRQ */
  1272. list_for_each_entry(irq, &spic_dev.interrupts, list) {
  1273. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  1274. IRQF_SHARED, "sony-laptop", &spic_dev)) {
  1275. dprintk("IRQ: %d - triggering: %d - "
  1276. "polarity: %d - shr: %d\n",
  1277. irq->irq.interrupts[0],
  1278. irq->irq.triggering,
  1279. irq->irq.polarity,
  1280. irq->irq.sharable);
  1281. spic_dev.cur_irq = irq;
  1282. break;
  1283. }
  1284. }
  1285. if (!spic_dev.cur_irq) {
  1286. printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
  1287. result = -ENODEV;
  1288. goto err_release_region;
  1289. }
  1290. /* set resource status _SRS */
  1291. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  1292. if (result) {
  1293. printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
  1294. goto err_free_irq;
  1295. }
  1296. return 0;
  1297. err_free_irq:
  1298. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  1299. err_release_region:
  1300. release_region(spic_dev.cur_ioport->io.minimum,
  1301. spic_dev.cur_ioport->io.address_length);
  1302. err_remove_input:
  1303. sony_laptop_remove_input();
  1304. err_free_resources:
  1305. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  1306. list_del(&io->list);
  1307. kfree(io);
  1308. }
  1309. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  1310. list_del(&irq->list);
  1311. kfree(irq);
  1312. }
  1313. spic_dev.cur_ioport = NULL;
  1314. spic_dev.cur_irq = NULL;
  1315. return result;
  1316. }
  1317. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  1318. {
  1319. if (sony_pic_disable(device))
  1320. return -ENXIO;
  1321. return 0;
  1322. }
  1323. static int sony_pic_resume(struct acpi_device *device)
  1324. {
  1325. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  1326. return 0;
  1327. }
  1328. static struct acpi_driver sony_pic_driver = {
  1329. .name = SONY_PIC_DRIVER_NAME,
  1330. .class = SONY_PIC_CLASS,
  1331. .ids = SONY_PIC_HID,
  1332. .owner = THIS_MODULE,
  1333. .ops = {
  1334. .add = sony_pic_add,
  1335. .remove = sony_pic_remove,
  1336. .suspend = sony_pic_suspend,
  1337. .resume = sony_pic_resume,
  1338. },
  1339. };
  1340. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  1341. {
  1342. .ident = "Sony Vaio",
  1343. .matches = {
  1344. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1345. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  1346. },
  1347. },
  1348. {
  1349. .ident = "Sony Vaio",
  1350. .matches = {
  1351. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1352. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  1353. },
  1354. },
  1355. { }
  1356. };
  1357. static int __init sony_laptop_init(void)
  1358. {
  1359. int result;
  1360. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  1361. result = acpi_bus_register_driver(&sony_pic_driver);
  1362. if (result) {
  1363. printk(KERN_ERR DRV_PFX
  1364. "Unable to register SPIC driver.");
  1365. goto out;
  1366. }
  1367. }
  1368. result = acpi_bus_register_driver(&sony_nc_driver);
  1369. if (result) {
  1370. printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
  1371. goto out_unregister_pic;
  1372. }
  1373. return 0;
  1374. out_unregister_pic:
  1375. if (!no_spic)
  1376. acpi_bus_unregister_driver(&sony_pic_driver);
  1377. out:
  1378. return result;
  1379. }
  1380. static void __exit sony_laptop_exit(void)
  1381. {
  1382. acpi_bus_unregister_driver(&sony_nc_driver);
  1383. if (!no_spic)
  1384. acpi_bus_unregister_driver(&sony_pic_driver);
  1385. }
  1386. module_init(sony_laptop_init);
  1387. module_exit(sony_laptop_exit);