sony-laptop.c 66 KB

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