sony-laptop.c 54 KB

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