sonypi.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Sony Programmable I/O Control Device driver for VAIO
  4. *
  5. * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
  6. *
  7. * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
  8. *
  9. * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
  10. *
  11. * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
  12. *
  13. * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
  14. *
  15. * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
  16. *
  17. * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
  18. *
  19. * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
  20. *
  21. * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/sched.h>
  25. #include <linux/input.h>
  26. #include <linux/pci.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/poll.h>
  31. #include <linux/delay.h>
  32. #include <linux/wait.h>
  33. #include <linux/acpi.h>
  34. #include <linux/dmi.h>
  35. #include <linux/err.h>
  36. #include <linux/kfifo.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/gfp.h>
  39. #include <linux/uaccess.h>
  40. #include <asm/io.h>
  41. #include <linux/sonypi.h>
  42. #define SONYPI_DRIVER_VERSION "1.26"
  43. MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
  44. MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
  45. MODULE_LICENSE("GPL");
  46. MODULE_VERSION(SONYPI_DRIVER_VERSION);
  47. static int minor = -1;
  48. module_param(minor, int, 0);
  49. MODULE_PARM_DESC(minor,
  50. "minor number of the misc device, default is -1 (automatic)");
  51. static int verbose; /* = 0 */
  52. module_param(verbose, int, 0644);
  53. MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
  54. static int fnkeyinit; /* = 0 */
  55. module_param(fnkeyinit, int, 0444);
  56. MODULE_PARM_DESC(fnkeyinit,
  57. "set this if your Fn keys do not generate any event");
  58. static int camera; /* = 0 */
  59. module_param(camera, int, 0444);
  60. MODULE_PARM_DESC(camera,
  61. "set this if you have a MotionEye camera (PictureBook series)");
  62. static int compat; /* = 0 */
  63. module_param(compat, int, 0444);
  64. MODULE_PARM_DESC(compat,
  65. "set this if you want to enable backward compatibility mode");
  66. static unsigned long mask = 0xffffffff;
  67. module_param(mask, ulong, 0644);
  68. MODULE_PARM_DESC(mask,
  69. "set this to the mask of event you want to enable (see doc)");
  70. static int useinput = 1;
  71. module_param(useinput, int, 0444);
  72. MODULE_PARM_DESC(useinput,
  73. "set this if you would like sonypi to feed events to the input subsystem");
  74. static int check_ioport = 1;
  75. module_param(check_ioport, int, 0444);
  76. MODULE_PARM_DESC(check_ioport,
  77. "set this to 0 if you think the automatic ioport check for sony-laptop is wrong");
  78. #define SONYPI_DEVICE_MODEL_TYPE1 1
  79. #define SONYPI_DEVICE_MODEL_TYPE2 2
  80. #define SONYPI_DEVICE_MODEL_TYPE3 3
  81. /* type1 models use those */
  82. #define SONYPI_IRQ_PORT 0x8034
  83. #define SONYPI_IRQ_SHIFT 22
  84. #define SONYPI_TYPE1_BASE 0x50
  85. #define SONYPI_G10A (SONYPI_TYPE1_BASE+0x14)
  86. #define SONYPI_TYPE1_REGION_SIZE 0x08
  87. #define SONYPI_TYPE1_EVTYPE_OFFSET 0x04
  88. /* type2 series specifics */
  89. #define SONYPI_SIRQ 0x9b
  90. #define SONYPI_SLOB 0x9c
  91. #define SONYPI_SHIB 0x9d
  92. #define SONYPI_TYPE2_REGION_SIZE 0x20
  93. #define SONYPI_TYPE2_EVTYPE_OFFSET 0x12
  94. /* type3 series specifics */
  95. #define SONYPI_TYPE3_BASE 0x40
  96. #define SONYPI_TYPE3_GID2 (SONYPI_TYPE3_BASE+0x48) /* 16 bits */
  97. #define SONYPI_TYPE3_MISC (SONYPI_TYPE3_BASE+0x6d) /* 8 bits */
  98. #define SONYPI_TYPE3_REGION_SIZE 0x20
  99. #define SONYPI_TYPE3_EVTYPE_OFFSET 0x12
  100. /* battery / brightness addresses */
  101. #define SONYPI_BAT_FLAGS 0x81
  102. #define SONYPI_LCD_LIGHT 0x96
  103. #define SONYPI_BAT1_PCTRM 0xa0
  104. #define SONYPI_BAT1_LEFT 0xa2
  105. #define SONYPI_BAT1_MAXRT 0xa4
  106. #define SONYPI_BAT2_PCTRM 0xa8
  107. #define SONYPI_BAT2_LEFT 0xaa
  108. #define SONYPI_BAT2_MAXRT 0xac
  109. #define SONYPI_BAT1_MAXTK 0xb0
  110. #define SONYPI_BAT1_FULL 0xb2
  111. #define SONYPI_BAT2_MAXTK 0xb8
  112. #define SONYPI_BAT2_FULL 0xba
  113. /* FAN0 information (reverse engineered from ACPI tables) */
  114. #define SONYPI_FAN0_STATUS 0x93
  115. #define SONYPI_TEMP_STATUS 0xC1
  116. /* ioports used for brightness and type2 events */
  117. #define SONYPI_DATA_IOPORT 0x62
  118. #define SONYPI_CST_IOPORT 0x66
  119. /* The set of possible ioports */
  120. struct sonypi_ioport_list {
  121. u16 port1;
  122. u16 port2;
  123. };
  124. static struct sonypi_ioport_list sonypi_type1_ioport_list[] = {
  125. { 0x10c0, 0x10c4 }, /* looks like the default on C1Vx */
  126. { 0x1080, 0x1084 },
  127. { 0x1090, 0x1094 },
  128. { 0x10a0, 0x10a4 },
  129. { 0x10b0, 0x10b4 },
  130. { 0x0, 0x0 }
  131. };
  132. static struct sonypi_ioport_list sonypi_type2_ioport_list[] = {
  133. { 0x1080, 0x1084 },
  134. { 0x10a0, 0x10a4 },
  135. { 0x10c0, 0x10c4 },
  136. { 0x10e0, 0x10e4 },
  137. { 0x0, 0x0 }
  138. };
  139. /* same as in type 2 models */
  140. static struct sonypi_ioport_list *sonypi_type3_ioport_list =
  141. sonypi_type2_ioport_list;
  142. /* The set of possible interrupts */
  143. struct sonypi_irq_list {
  144. u16 irq;
  145. u16 bits;
  146. };
  147. static struct sonypi_irq_list sonypi_type1_irq_list[] = {
  148. { 11, 0x2 }, /* IRQ 11, GO22=0,GO23=1 in AML */
  149. { 10, 0x1 }, /* IRQ 10, GO22=1,GO23=0 in AML */
  150. { 5, 0x0 }, /* IRQ 5, GO22=0,GO23=0 in AML */
  151. { 0, 0x3 } /* no IRQ, GO22=1,GO23=1 in AML */
  152. };
  153. static struct sonypi_irq_list sonypi_type2_irq_list[] = {
  154. { 11, 0x80 }, /* IRQ 11, 0x80 in SIRQ in AML */
  155. { 10, 0x40 }, /* IRQ 10, 0x40 in SIRQ in AML */
  156. { 9, 0x20 }, /* IRQ 9, 0x20 in SIRQ in AML */
  157. { 6, 0x10 }, /* IRQ 6, 0x10 in SIRQ in AML */
  158. { 0, 0x00 } /* no IRQ, 0x00 in SIRQ in AML */
  159. };
  160. /* same as in type2 models */
  161. static struct sonypi_irq_list *sonypi_type3_irq_list = sonypi_type2_irq_list;
  162. #define SONYPI_CAMERA_BRIGHTNESS 0
  163. #define SONYPI_CAMERA_CONTRAST 1
  164. #define SONYPI_CAMERA_HUE 2
  165. #define SONYPI_CAMERA_COLOR 3
  166. #define SONYPI_CAMERA_SHARPNESS 4
  167. #define SONYPI_CAMERA_PICTURE 5
  168. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  169. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  170. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  171. #define SONYPI_CAMERA_MUTE_MASK 0x40
  172. /* the rest don't need a loop until not 0xff */
  173. #define SONYPI_CAMERA_AGC 6
  174. #define SONYPI_CAMERA_AGC_MASK 0x30
  175. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  176. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  177. #define SONYPI_CAMERA_CONTROL 0x10
  178. #define SONYPI_CAMERA_STATUS 7
  179. #define SONYPI_CAMERA_STATUS_READY 0x2
  180. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  181. #define SONYPI_DIRECTION_BACKWARDS 0x4
  182. #define SONYPI_CAMERA_REVISION 8
  183. #define SONYPI_CAMERA_ROMVERSION 9
  184. /* Event masks */
  185. #define SONYPI_JOGGER_MASK 0x00000001
  186. #define SONYPI_CAPTURE_MASK 0x00000002
  187. #define SONYPI_FNKEY_MASK 0x00000004
  188. #define SONYPI_BLUETOOTH_MASK 0x00000008
  189. #define SONYPI_PKEY_MASK 0x00000010
  190. #define SONYPI_BACK_MASK 0x00000020
  191. #define SONYPI_HELP_MASK 0x00000040
  192. #define SONYPI_LID_MASK 0x00000080
  193. #define SONYPI_ZOOM_MASK 0x00000100
  194. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  195. #define SONYPI_MEYE_MASK 0x00000400
  196. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  197. #define SONYPI_BATTERY_MASK 0x00001000
  198. #define SONYPI_WIRELESS_MASK 0x00002000
  199. struct sonypi_event {
  200. u8 data;
  201. u8 event;
  202. };
  203. /* The set of possible button release events */
  204. static struct sonypi_event sonypi_releaseev[] = {
  205. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  206. { 0, 0 }
  207. };
  208. /* The set of possible jogger events */
  209. static struct sonypi_event sonypi_joggerev[] = {
  210. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  211. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  212. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  213. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  214. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  215. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  216. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  217. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  218. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  219. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  220. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  221. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  222. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  223. { 0, 0 }
  224. };
  225. /* The set of possible capture button events */
  226. static struct sonypi_event sonypi_captureev[] = {
  227. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  228. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  229. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  230. { 0, 0 }
  231. };
  232. /* The set of possible fnkeys events */
  233. static struct sonypi_event sonypi_fnkeyev[] = {
  234. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  235. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  236. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  237. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  238. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  239. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  240. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  241. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  242. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  243. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  244. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  245. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  246. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  247. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  248. { 0x21, SONYPI_EVENT_FNKEY_1 },
  249. { 0x22, SONYPI_EVENT_FNKEY_2 },
  250. { 0x31, SONYPI_EVENT_FNKEY_D },
  251. { 0x32, SONYPI_EVENT_FNKEY_E },
  252. { 0x33, SONYPI_EVENT_FNKEY_F },
  253. { 0x34, SONYPI_EVENT_FNKEY_S },
  254. { 0x35, SONYPI_EVENT_FNKEY_B },
  255. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  256. { 0, 0 }
  257. };
  258. /* The set of possible program key events */
  259. static struct sonypi_event sonypi_pkeyev[] = {
  260. { 0x01, SONYPI_EVENT_PKEY_P1 },
  261. { 0x02, SONYPI_EVENT_PKEY_P2 },
  262. { 0x04, SONYPI_EVENT_PKEY_P3 },
  263. { 0x5c, SONYPI_EVENT_PKEY_P1 },
  264. { 0, 0 }
  265. };
  266. /* The set of possible bluetooth events */
  267. static struct sonypi_event sonypi_blueev[] = {
  268. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  269. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  270. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  271. { 0, 0 }
  272. };
  273. /* The set of possible wireless events */
  274. static struct sonypi_event sonypi_wlessev[] = {
  275. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  276. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  277. { 0, 0 }
  278. };
  279. /* The set of possible back button events */
  280. static struct sonypi_event sonypi_backev[] = {
  281. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  282. { 0, 0 }
  283. };
  284. /* The set of possible help button events */
  285. static struct sonypi_event sonypi_helpev[] = {
  286. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  287. { 0, 0 }
  288. };
  289. /* The set of possible lid events */
  290. static struct sonypi_event sonypi_lidev[] = {
  291. { 0x51, SONYPI_EVENT_LID_CLOSED },
  292. { 0x50, SONYPI_EVENT_LID_OPENED },
  293. { 0, 0 }
  294. };
  295. /* The set of possible zoom events */
  296. static struct sonypi_event sonypi_zoomev[] = {
  297. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  298. { 0, 0 }
  299. };
  300. /* The set of possible thumbphrase events */
  301. static struct sonypi_event sonypi_thumbphraseev[] = {
  302. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  303. { 0, 0 }
  304. };
  305. /* The set of possible motioneye camera events */
  306. static struct sonypi_event sonypi_meyeev[] = {
  307. { 0x00, SONYPI_EVENT_MEYE_FACE },
  308. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  309. { 0, 0 }
  310. };
  311. /* The set of possible memorystick events */
  312. static struct sonypi_event sonypi_memorystickev[] = {
  313. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  314. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  315. { 0, 0 }
  316. };
  317. /* The set of possible battery events */
  318. static struct sonypi_event sonypi_batteryev[] = {
  319. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  320. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  321. { 0, 0 }
  322. };
  323. static struct sonypi_eventtypes {
  324. int model;
  325. u8 data;
  326. unsigned long mask;
  327. struct sonypi_event * events;
  328. } sonypi_eventtypes[] = {
  329. { SONYPI_DEVICE_MODEL_TYPE1, 0, 0xffffffff, sonypi_releaseev },
  330. { SONYPI_DEVICE_MODEL_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  331. { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
  332. { SONYPI_DEVICE_MODEL_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  333. { SONYPI_DEVICE_MODEL_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  334. { SONYPI_DEVICE_MODEL_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  335. { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  336. { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  337. { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  338. { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  339. { SONYPI_DEVICE_MODEL_TYPE2, 0, 0xffffffff, sonypi_releaseev },
  340. { SONYPI_DEVICE_MODEL_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
  341. { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  342. { SONYPI_DEVICE_MODEL_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  343. { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  344. { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  345. { SONYPI_DEVICE_MODEL_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  346. { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
  347. { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  348. { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  349. { SONYPI_DEVICE_MODEL_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  350. { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  351. { SONYPI_DEVICE_MODEL_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  352. { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  353. { SONYPI_DEVICE_MODEL_TYPE3, 0, 0xffffffff, sonypi_releaseev },
  354. { SONYPI_DEVICE_MODEL_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  355. { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  356. { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  357. { SONYPI_DEVICE_MODEL_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  358. { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  359. { 0 }
  360. };
  361. #define SONYPI_BUF_SIZE 128
  362. /* Correspondance table between sonypi events and input layer events */
  363. static struct {
  364. int sonypiev;
  365. int inputev;
  366. } sonypi_inputkeys[] = {
  367. { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
  368. { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
  369. { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
  370. { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
  371. { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
  372. { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
  373. { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
  374. { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
  375. { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
  376. { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
  377. { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
  378. { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
  379. { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
  380. { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
  381. { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
  382. { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
  383. { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
  384. { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
  385. { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
  386. { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
  387. { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
  388. { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
  389. { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
  390. { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
  391. { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
  392. { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
  393. { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
  394. { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
  395. { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
  396. { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
  397. { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
  398. { 0, 0 },
  399. };
  400. struct sonypi_keypress {
  401. struct input_dev *dev;
  402. int key;
  403. };
  404. static struct sonypi_device {
  405. struct pci_dev *dev;
  406. u16 irq;
  407. u16 bits;
  408. u16 ioport1;
  409. u16 ioport2;
  410. u16 region_size;
  411. u16 evtype_offset;
  412. int camera_power;
  413. int bluetooth_power;
  414. struct mutex lock;
  415. struct kfifo fifo;
  416. spinlock_t fifo_lock;
  417. wait_queue_head_t fifo_proc_list;
  418. struct fasync_struct *fifo_async;
  419. int open_count;
  420. int model;
  421. struct input_dev *input_jog_dev;
  422. struct input_dev *input_key_dev;
  423. struct work_struct input_work;
  424. struct kfifo input_fifo;
  425. spinlock_t input_fifo_lock;
  426. } sonypi_device;
  427. #define ITERATIONS_LONG 10000
  428. #define ITERATIONS_SHORT 10
  429. #define wait_on_command(quiet, command, iterations) { \
  430. unsigned int n = iterations; \
  431. while (--n && (command)) \
  432. udelay(1); \
  433. if (!n && (verbose || !quiet)) \
  434. printk(KERN_WARNING "sonypi command failed at %s : %s (line %d)\n", __FILE__, __func__, __LINE__); \
  435. }
  436. #ifdef CONFIG_ACPI
  437. #define SONYPI_ACPI_ACTIVE (!acpi_disabled)
  438. #else
  439. #define SONYPI_ACPI_ACTIVE 0
  440. #endif /* CONFIG_ACPI */
  441. #ifdef CONFIG_ACPI
  442. static struct acpi_device *sonypi_acpi_device;
  443. static int acpi_driver_registered;
  444. #endif
  445. static int sonypi_ec_write(u8 addr, u8 value)
  446. {
  447. #ifdef CONFIG_ACPI
  448. if (SONYPI_ACPI_ACTIVE)
  449. return ec_write(addr, value);
  450. #endif
  451. wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
  452. outb_p(0x81, SONYPI_CST_IOPORT);
  453. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  454. outb_p(addr, SONYPI_DATA_IOPORT);
  455. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  456. outb_p(value, SONYPI_DATA_IOPORT);
  457. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  458. return 0;
  459. }
  460. static int sonypi_ec_read(u8 addr, u8 *value)
  461. {
  462. #ifdef CONFIG_ACPI
  463. if (SONYPI_ACPI_ACTIVE)
  464. return ec_read(addr, value);
  465. #endif
  466. wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
  467. outb_p(0x80, SONYPI_CST_IOPORT);
  468. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  469. outb_p(addr, SONYPI_DATA_IOPORT);
  470. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  471. *value = inb_p(SONYPI_DATA_IOPORT);
  472. return 0;
  473. }
  474. static int ec_read16(u8 addr, u16 *value)
  475. {
  476. u8 val_lb, val_hb;
  477. if (sonypi_ec_read(addr, &val_lb))
  478. return -1;
  479. if (sonypi_ec_read(addr + 1, &val_hb))
  480. return -1;
  481. *value = val_lb | (val_hb << 8);
  482. return 0;
  483. }
  484. /* Initializes the device - this comes from the AML code in the ACPI bios */
  485. static void sonypi_type1_srs(void)
  486. {
  487. u32 v;
  488. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  489. v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
  490. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  491. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  492. v = (v & 0xFFF0FFFF) |
  493. (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
  494. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  495. v = inl(SONYPI_IRQ_PORT);
  496. v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
  497. v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
  498. outl(v, SONYPI_IRQ_PORT);
  499. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  500. v = (v & 0xFF1FFFFF) | 0x00C00000;
  501. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  502. }
  503. static void sonypi_type2_srs(void)
  504. {
  505. if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
  506. printk(KERN_WARNING "ec_write failed\n");
  507. if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
  508. printk(KERN_WARNING "ec_write failed\n");
  509. if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
  510. printk(KERN_WARNING "ec_write failed\n");
  511. udelay(10);
  512. }
  513. static void sonypi_type3_srs(void)
  514. {
  515. u16 v16;
  516. u8 v8;
  517. /* This model type uses the same initialization of
  518. * the embedded controller as the type2 models. */
  519. sonypi_type2_srs();
  520. /* Initialization of PCI config space of the LPC interface bridge. */
  521. v16 = (sonypi_device.ioport1 & 0xFFF0) | 0x01;
  522. pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, v16);
  523. pci_read_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, &v8);
  524. v8 = (v8 & 0xCF) | 0x10;
  525. pci_write_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, v8);
  526. }
  527. /* Disables the device - this comes from the AML code in the ACPI bios */
  528. static void sonypi_type1_dis(void)
  529. {
  530. u32 v;
  531. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  532. v = v & 0xFF3FFFFF;
  533. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  534. v = inl(SONYPI_IRQ_PORT);
  535. v |= (0x3 << SONYPI_IRQ_SHIFT);
  536. outl(v, SONYPI_IRQ_PORT);
  537. }
  538. static void sonypi_type2_dis(void)
  539. {
  540. if (sonypi_ec_write(SONYPI_SHIB, 0))
  541. printk(KERN_WARNING "ec_write failed\n");
  542. if (sonypi_ec_write(SONYPI_SLOB, 0))
  543. printk(KERN_WARNING "ec_write failed\n");
  544. if (sonypi_ec_write(SONYPI_SIRQ, 0))
  545. printk(KERN_WARNING "ec_write failed\n");
  546. }
  547. static void sonypi_type3_dis(void)
  548. {
  549. sonypi_type2_dis();
  550. udelay(10);
  551. pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, 0);
  552. }
  553. static u8 sonypi_call1(u8 dev)
  554. {
  555. u8 v1, v2;
  556. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  557. outb(dev, sonypi_device.ioport2);
  558. v1 = inb_p(sonypi_device.ioport2);
  559. v2 = inb_p(sonypi_device.ioport1);
  560. return v2;
  561. }
  562. static u8 sonypi_call2(u8 dev, u8 fn)
  563. {
  564. u8 v1;
  565. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  566. outb(dev, sonypi_device.ioport2);
  567. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  568. outb(fn, sonypi_device.ioport1);
  569. v1 = inb_p(sonypi_device.ioport1);
  570. return v1;
  571. }
  572. static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
  573. {
  574. u8 v1;
  575. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  576. outb(dev, sonypi_device.ioport2);
  577. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  578. outb(fn, sonypi_device.ioport1);
  579. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  580. outb(v, sonypi_device.ioport1);
  581. v1 = inb_p(sonypi_device.ioport1);
  582. return v1;
  583. }
  584. #if 0
  585. /* Get brightness, hue etc. Unreliable... */
  586. static u8 sonypi_read(u8 fn)
  587. {
  588. u8 v1, v2;
  589. int n = 100;
  590. while (n--) {
  591. v1 = sonypi_call2(0x8f, fn);
  592. v2 = sonypi_call2(0x8f, fn);
  593. if (v1 == v2 && v1 != 0xff)
  594. return v1;
  595. }
  596. return 0xff;
  597. }
  598. #endif
  599. /* Set brightness, hue etc */
  600. static void sonypi_set(u8 fn, u8 v)
  601. {
  602. wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
  603. }
  604. /* Tests if the camera is ready */
  605. static int sonypi_camera_ready(void)
  606. {
  607. u8 v;
  608. v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
  609. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  610. }
  611. /* Turns the camera off */
  612. static void sonypi_camera_off(void)
  613. {
  614. sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
  615. if (!sonypi_device.camera_power)
  616. return;
  617. sonypi_call2(0x91, 0);
  618. sonypi_device.camera_power = 0;
  619. }
  620. /* Turns the camera on */
  621. static void sonypi_camera_on(void)
  622. {
  623. int i, j;
  624. if (sonypi_device.camera_power)
  625. return;
  626. for (j = 5; j > 0; j--) {
  627. while (sonypi_call2(0x91, 0x1))
  628. msleep(10);
  629. sonypi_call1(0x93);
  630. for (i = 400; i > 0; i--) {
  631. if (sonypi_camera_ready())
  632. break;
  633. msleep(10);
  634. }
  635. if (i)
  636. break;
  637. }
  638. if (j == 0) {
  639. printk(KERN_WARNING "sonypi: failed to power on camera\n");
  640. return;
  641. }
  642. sonypi_set(0x10, 0x5a);
  643. sonypi_device.camera_power = 1;
  644. }
  645. /* sets the bluetooth subsystem power state */
  646. static void sonypi_setbluetoothpower(u8 state)
  647. {
  648. state = !!state;
  649. if (sonypi_device.bluetooth_power == state)
  650. return;
  651. sonypi_call2(0x96, state);
  652. sonypi_call1(0x82);
  653. sonypi_device.bluetooth_power = state;
  654. }
  655. static void input_keyrelease(struct work_struct *work)
  656. {
  657. struct sonypi_keypress kp;
  658. while (kfifo_out_locked(&sonypi_device.input_fifo, (unsigned char *)&kp,
  659. sizeof(kp), &sonypi_device.input_fifo_lock)
  660. == sizeof(kp)) {
  661. msleep(10);
  662. input_report_key(kp.dev, kp.key, 0);
  663. input_sync(kp.dev);
  664. }
  665. }
  666. static void sonypi_report_input_event(u8 event)
  667. {
  668. struct input_dev *jog_dev = sonypi_device.input_jog_dev;
  669. struct input_dev *key_dev = sonypi_device.input_key_dev;
  670. struct sonypi_keypress kp = { NULL };
  671. int i;
  672. switch (event) {
  673. case SONYPI_EVENT_JOGDIAL_UP:
  674. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  675. input_report_rel(jog_dev, REL_WHEEL, 1);
  676. input_sync(jog_dev);
  677. break;
  678. case SONYPI_EVENT_JOGDIAL_DOWN:
  679. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  680. input_report_rel(jog_dev, REL_WHEEL, -1);
  681. input_sync(jog_dev);
  682. break;
  683. case SONYPI_EVENT_JOGDIAL_PRESSED:
  684. kp.key = BTN_MIDDLE;
  685. kp.dev = jog_dev;
  686. break;
  687. case SONYPI_EVENT_FNKEY_RELEASED:
  688. /* Nothing, not all VAIOs generate this event */
  689. break;
  690. default:
  691. for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
  692. if (event == sonypi_inputkeys[i].sonypiev) {
  693. kp.dev = key_dev;
  694. kp.key = sonypi_inputkeys[i].inputev;
  695. break;
  696. }
  697. break;
  698. }
  699. if (kp.dev) {
  700. input_report_key(kp.dev, kp.key, 1);
  701. input_sync(kp.dev);
  702. kfifo_in_locked(&sonypi_device.input_fifo,
  703. (unsigned char *)&kp, sizeof(kp),
  704. &sonypi_device.input_fifo_lock);
  705. schedule_work(&sonypi_device.input_work);
  706. }
  707. }
  708. /* Interrupt handler: some event is available */
  709. static irqreturn_t sonypi_irq(int irq, void *dev_id)
  710. {
  711. u8 v1, v2, event = 0;
  712. int i, j;
  713. v1 = inb_p(sonypi_device.ioport1);
  714. v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
  715. for (i = 0; sonypi_eventtypes[i].model; i++) {
  716. if (sonypi_device.model != sonypi_eventtypes[i].model)
  717. continue;
  718. if ((v2 & sonypi_eventtypes[i].data) !=
  719. sonypi_eventtypes[i].data)
  720. continue;
  721. if (!(mask & sonypi_eventtypes[i].mask))
  722. continue;
  723. for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
  724. if (v1 == sonypi_eventtypes[i].events[j].data) {
  725. event = sonypi_eventtypes[i].events[j].event;
  726. goto found;
  727. }
  728. }
  729. }
  730. if (verbose)
  731. printk(KERN_WARNING
  732. "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
  733. v1, v2);
  734. /* We need to return IRQ_HANDLED here because there *are*
  735. * events belonging to the sonypi device we don't know about,
  736. * but we still don't want those to pollute the logs... */
  737. return IRQ_HANDLED;
  738. found:
  739. if (verbose > 1)
  740. printk(KERN_INFO
  741. "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
  742. if (useinput)
  743. sonypi_report_input_event(event);
  744. kfifo_in_locked(&sonypi_device.fifo, (unsigned char *)&event,
  745. sizeof(event), &sonypi_device.fifo_lock);
  746. kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
  747. wake_up_interruptible(&sonypi_device.fifo_proc_list);
  748. return IRQ_HANDLED;
  749. }
  750. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  751. {
  752. return fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
  753. }
  754. static int sonypi_misc_release(struct inode *inode, struct file *file)
  755. {
  756. mutex_lock(&sonypi_device.lock);
  757. sonypi_device.open_count--;
  758. mutex_unlock(&sonypi_device.lock);
  759. return 0;
  760. }
  761. static int sonypi_misc_open(struct inode *inode, struct file *file)
  762. {
  763. mutex_lock(&sonypi_device.lock);
  764. /* Flush input queue on first open */
  765. if (!sonypi_device.open_count)
  766. kfifo_reset(&sonypi_device.fifo);
  767. sonypi_device.open_count++;
  768. mutex_unlock(&sonypi_device.lock);
  769. return 0;
  770. }
  771. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  772. size_t count, loff_t *pos)
  773. {
  774. ssize_t ret;
  775. unsigned char c;
  776. if ((kfifo_len(&sonypi_device.fifo) == 0) &&
  777. (file->f_flags & O_NONBLOCK))
  778. return -EAGAIN;
  779. ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
  780. kfifo_len(&sonypi_device.fifo) != 0);
  781. if (ret)
  782. return ret;
  783. while (ret < count &&
  784. (kfifo_out_locked(&sonypi_device.fifo, &c, sizeof(c),
  785. &sonypi_device.fifo_lock) == sizeof(c))) {
  786. if (put_user(c, buf++))
  787. return -EFAULT;
  788. ret++;
  789. }
  790. if (ret > 0) {
  791. struct inode *inode = file_inode(file);
  792. inode->i_atime = current_time(inode);
  793. }
  794. return ret;
  795. }
  796. static __poll_t sonypi_misc_poll(struct file *file, poll_table *wait)
  797. {
  798. poll_wait(file, &sonypi_device.fifo_proc_list, wait);
  799. if (kfifo_len(&sonypi_device.fifo))
  800. return EPOLLIN | EPOLLRDNORM;
  801. return 0;
  802. }
  803. static long sonypi_misc_ioctl(struct file *fp,
  804. unsigned int cmd, unsigned long arg)
  805. {
  806. long ret = 0;
  807. void __user *argp = (void __user *)arg;
  808. u8 val8;
  809. u16 val16;
  810. mutex_lock(&sonypi_device.lock);
  811. switch (cmd) {
  812. case SONYPI_IOCGBRT:
  813. if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
  814. ret = -EIO;
  815. break;
  816. }
  817. if (copy_to_user(argp, &val8, sizeof(val8)))
  818. ret = -EFAULT;
  819. break;
  820. case SONYPI_IOCSBRT:
  821. if (copy_from_user(&val8, argp, sizeof(val8))) {
  822. ret = -EFAULT;
  823. break;
  824. }
  825. if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
  826. ret = -EIO;
  827. break;
  828. case SONYPI_IOCGBAT1CAP:
  829. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  830. ret = -EIO;
  831. break;
  832. }
  833. if (copy_to_user(argp, &val16, sizeof(val16)))
  834. ret = -EFAULT;
  835. break;
  836. case SONYPI_IOCGBAT1REM:
  837. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  838. ret = -EIO;
  839. break;
  840. }
  841. if (copy_to_user(argp, &val16, sizeof(val16)))
  842. ret = -EFAULT;
  843. break;
  844. case SONYPI_IOCGBAT2CAP:
  845. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  846. ret = -EIO;
  847. break;
  848. }
  849. if (copy_to_user(argp, &val16, sizeof(val16)))
  850. ret = -EFAULT;
  851. break;
  852. case SONYPI_IOCGBAT2REM:
  853. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  854. ret = -EIO;
  855. break;
  856. }
  857. if (copy_to_user(argp, &val16, sizeof(val16)))
  858. ret = -EFAULT;
  859. break;
  860. case SONYPI_IOCGBATFLAGS:
  861. if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
  862. ret = -EIO;
  863. break;
  864. }
  865. val8 &= 0x07;
  866. if (copy_to_user(argp, &val8, sizeof(val8)))
  867. ret = -EFAULT;
  868. break;
  869. case SONYPI_IOCGBLUE:
  870. val8 = sonypi_device.bluetooth_power;
  871. if (copy_to_user(argp, &val8, sizeof(val8)))
  872. ret = -EFAULT;
  873. break;
  874. case SONYPI_IOCSBLUE:
  875. if (copy_from_user(&val8, argp, sizeof(val8))) {
  876. ret = -EFAULT;
  877. break;
  878. }
  879. sonypi_setbluetoothpower(val8);
  880. break;
  881. /* FAN Controls */
  882. case SONYPI_IOCGFAN:
  883. if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
  884. ret = -EIO;
  885. break;
  886. }
  887. if (copy_to_user(argp, &val8, sizeof(val8)))
  888. ret = -EFAULT;
  889. break;
  890. case SONYPI_IOCSFAN:
  891. if (copy_from_user(&val8, argp, sizeof(val8))) {
  892. ret = -EFAULT;
  893. break;
  894. }
  895. if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
  896. ret = -EIO;
  897. break;
  898. /* GET Temperature (useful under APM) */
  899. case SONYPI_IOCGTEMP:
  900. if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
  901. ret = -EIO;
  902. break;
  903. }
  904. if (copy_to_user(argp, &val8, sizeof(val8)))
  905. ret = -EFAULT;
  906. break;
  907. default:
  908. ret = -EINVAL;
  909. }
  910. mutex_unlock(&sonypi_device.lock);
  911. return ret;
  912. }
  913. static const struct file_operations sonypi_misc_fops = {
  914. .owner = THIS_MODULE,
  915. .read = sonypi_misc_read,
  916. .poll = sonypi_misc_poll,
  917. .open = sonypi_misc_open,
  918. .release = sonypi_misc_release,
  919. .fasync = sonypi_misc_fasync,
  920. .unlocked_ioctl = sonypi_misc_ioctl,
  921. .llseek = no_llseek,
  922. };
  923. static struct miscdevice sonypi_misc_device = {
  924. .minor = MISC_DYNAMIC_MINOR,
  925. .name = "sonypi",
  926. .fops = &sonypi_misc_fops,
  927. };
  928. static void sonypi_enable(unsigned int camera_on)
  929. {
  930. switch (sonypi_device.model) {
  931. case SONYPI_DEVICE_MODEL_TYPE1:
  932. sonypi_type1_srs();
  933. break;
  934. case SONYPI_DEVICE_MODEL_TYPE2:
  935. sonypi_type2_srs();
  936. break;
  937. case SONYPI_DEVICE_MODEL_TYPE3:
  938. sonypi_type3_srs();
  939. break;
  940. }
  941. sonypi_call1(0x82);
  942. sonypi_call2(0x81, 0xff);
  943. sonypi_call1(compat ? 0x92 : 0x82);
  944. /* Enable ACPI mode to get Fn key events */
  945. if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
  946. outb(0xf0, 0xb2);
  947. if (camera && camera_on)
  948. sonypi_camera_on();
  949. }
  950. static int sonypi_disable(void)
  951. {
  952. sonypi_call2(0x81, 0); /* make sure we don't get any more events */
  953. if (camera)
  954. sonypi_camera_off();
  955. /* disable ACPI mode */
  956. if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
  957. outb(0xf1, 0xb2);
  958. switch (sonypi_device.model) {
  959. case SONYPI_DEVICE_MODEL_TYPE1:
  960. sonypi_type1_dis();
  961. break;
  962. case SONYPI_DEVICE_MODEL_TYPE2:
  963. sonypi_type2_dis();
  964. break;
  965. case SONYPI_DEVICE_MODEL_TYPE3:
  966. sonypi_type3_dis();
  967. break;
  968. }
  969. return 0;
  970. }
  971. #ifdef CONFIG_ACPI
  972. static int sonypi_acpi_add(struct acpi_device *device)
  973. {
  974. sonypi_acpi_device = device;
  975. strcpy(acpi_device_name(device), "Sony laptop hotkeys");
  976. strcpy(acpi_device_class(device), "sony/hotkey");
  977. return 0;
  978. }
  979. static int sonypi_acpi_remove(struct acpi_device *device)
  980. {
  981. sonypi_acpi_device = NULL;
  982. return 0;
  983. }
  984. static const struct acpi_device_id sonypi_device_ids[] = {
  985. {"SNY6001", 0},
  986. {"", 0},
  987. };
  988. static struct acpi_driver sonypi_acpi_driver = {
  989. .name = "sonypi",
  990. .class = "hkey",
  991. .ids = sonypi_device_ids,
  992. .ops = {
  993. .add = sonypi_acpi_add,
  994. .remove = sonypi_acpi_remove,
  995. },
  996. };
  997. #endif
  998. static int sonypi_create_input_devices(struct platform_device *pdev)
  999. {
  1000. struct input_dev *jog_dev;
  1001. struct input_dev *key_dev;
  1002. int i;
  1003. int error;
  1004. sonypi_device.input_jog_dev = jog_dev = input_allocate_device();
  1005. if (!jog_dev)
  1006. return -ENOMEM;
  1007. jog_dev->name = "Sony Vaio Jogdial";
  1008. jog_dev->id.bustype = BUS_ISA;
  1009. jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
  1010. jog_dev->dev.parent = &pdev->dev;
  1011. jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1012. jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
  1013. jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
  1014. sonypi_device.input_key_dev = key_dev = input_allocate_device();
  1015. if (!key_dev) {
  1016. error = -ENOMEM;
  1017. goto err_free_jogdev;
  1018. }
  1019. key_dev->name = "Sony Vaio Keys";
  1020. key_dev->id.bustype = BUS_ISA;
  1021. key_dev->id.vendor = PCI_VENDOR_ID_SONY;
  1022. key_dev->dev.parent = &pdev->dev;
  1023. /* Initialize the Input Drivers: special keys */
  1024. key_dev->evbit[0] = BIT_MASK(EV_KEY);
  1025. for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
  1026. if (sonypi_inputkeys[i].inputev)
  1027. set_bit(sonypi_inputkeys[i].inputev, key_dev->keybit);
  1028. error = input_register_device(jog_dev);
  1029. if (error)
  1030. goto err_free_keydev;
  1031. error = input_register_device(key_dev);
  1032. if (error)
  1033. goto err_unregister_jogdev;
  1034. return 0;
  1035. err_unregister_jogdev:
  1036. input_unregister_device(jog_dev);
  1037. /* Set to NULL so we don't free it again below */
  1038. jog_dev = NULL;
  1039. err_free_keydev:
  1040. input_free_device(key_dev);
  1041. sonypi_device.input_key_dev = NULL;
  1042. err_free_jogdev:
  1043. input_free_device(jog_dev);
  1044. sonypi_device.input_jog_dev = NULL;
  1045. return error;
  1046. }
  1047. static int sonypi_setup_ioports(struct sonypi_device *dev,
  1048. const struct sonypi_ioport_list *ioport_list)
  1049. {
  1050. /* try to detect if sony-laptop is being used and thus
  1051. * has already requested one of the known ioports.
  1052. * As in the deprecated check_region this is racy has we have
  1053. * multiple ioports available and one of them can be requested
  1054. * between this check and the subsequent request. Anyway, as an
  1055. * attempt to be some more user-friendly as we currently are,
  1056. * this is enough.
  1057. */
  1058. const struct sonypi_ioport_list *check = ioport_list;
  1059. while (check_ioport && check->port1) {
  1060. if (!request_region(check->port1,
  1061. sonypi_device.region_size,
  1062. "Sony Programmable I/O Device Check")) {
  1063. printk(KERN_ERR "sonypi: ioport 0x%.4x busy, using sony-laptop? "
  1064. "if not use check_ioport=0\n",
  1065. check->port1);
  1066. return -EBUSY;
  1067. }
  1068. release_region(check->port1, sonypi_device.region_size);
  1069. check++;
  1070. }
  1071. while (ioport_list->port1) {
  1072. if (request_region(ioport_list->port1,
  1073. sonypi_device.region_size,
  1074. "Sony Programmable I/O Device")) {
  1075. dev->ioport1 = ioport_list->port1;
  1076. dev->ioport2 = ioport_list->port2;
  1077. return 0;
  1078. }
  1079. ioport_list++;
  1080. }
  1081. return -EBUSY;
  1082. }
  1083. static int sonypi_setup_irq(struct sonypi_device *dev,
  1084. const struct sonypi_irq_list *irq_list)
  1085. {
  1086. while (irq_list->irq) {
  1087. if (!request_irq(irq_list->irq, sonypi_irq,
  1088. IRQF_SHARED, "sonypi", sonypi_irq)) {
  1089. dev->irq = irq_list->irq;
  1090. dev->bits = irq_list->bits;
  1091. return 0;
  1092. }
  1093. irq_list++;
  1094. }
  1095. return -EBUSY;
  1096. }
  1097. static void sonypi_display_info(void)
  1098. {
  1099. printk(KERN_INFO "sonypi: detected type%d model, "
  1100. "verbose = %d, fnkeyinit = %s, camera = %s, "
  1101. "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
  1102. sonypi_device.model,
  1103. verbose,
  1104. fnkeyinit ? "on" : "off",
  1105. camera ? "on" : "off",
  1106. compat ? "on" : "off",
  1107. mask,
  1108. useinput ? "on" : "off",
  1109. SONYPI_ACPI_ACTIVE ? "on" : "off");
  1110. printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
  1111. sonypi_device.irq,
  1112. sonypi_device.ioport1, sonypi_device.ioport2);
  1113. if (minor == -1)
  1114. printk(KERN_INFO "sonypi: device allocated minor is %d\n",
  1115. sonypi_misc_device.minor);
  1116. }
  1117. static int sonypi_probe(struct platform_device *dev)
  1118. {
  1119. const struct sonypi_ioport_list *ioport_list;
  1120. const struct sonypi_irq_list *irq_list;
  1121. struct pci_dev *pcidev;
  1122. int error;
  1123. printk(KERN_WARNING "sonypi: please try the sony-laptop module instead "
  1124. "and report failures, see also "
  1125. "http://www.linux.it/~malattia/wiki/index.php/Sony_drivers\n");
  1126. spin_lock_init(&sonypi_device.fifo_lock);
  1127. error = kfifo_alloc(&sonypi_device.fifo, SONYPI_BUF_SIZE, GFP_KERNEL);
  1128. if (error) {
  1129. printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
  1130. return error;
  1131. }
  1132. init_waitqueue_head(&sonypi_device.fifo_proc_list);
  1133. mutex_init(&sonypi_device.lock);
  1134. sonypi_device.bluetooth_power = -1;
  1135. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1136. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  1137. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
  1138. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1139. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  1140. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
  1141. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1142. PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
  1143. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
  1144. else
  1145. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
  1146. if (pcidev && pci_enable_device(pcidev)) {
  1147. printk(KERN_ERR "sonypi: pci_enable_device failed\n");
  1148. error = -EIO;
  1149. goto err_put_pcidev;
  1150. }
  1151. sonypi_device.dev = pcidev;
  1152. if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
  1153. ioport_list = sonypi_type1_ioport_list;
  1154. sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
  1155. sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
  1156. irq_list = sonypi_type1_irq_list;
  1157. } else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
  1158. ioport_list = sonypi_type2_ioport_list;
  1159. sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
  1160. sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
  1161. irq_list = sonypi_type2_irq_list;
  1162. } else {
  1163. ioport_list = sonypi_type3_ioport_list;
  1164. sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
  1165. sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
  1166. irq_list = sonypi_type3_irq_list;
  1167. }
  1168. error = sonypi_setup_ioports(&sonypi_device, ioport_list);
  1169. if (error) {
  1170. printk(KERN_ERR "sonypi: failed to request ioports\n");
  1171. goto err_disable_pcidev;
  1172. }
  1173. error = sonypi_setup_irq(&sonypi_device, irq_list);
  1174. if (error) {
  1175. printk(KERN_ERR "sonypi: request_irq failed\n");
  1176. goto err_free_ioports;
  1177. }
  1178. if (minor != -1)
  1179. sonypi_misc_device.minor = minor;
  1180. error = misc_register(&sonypi_misc_device);
  1181. if (error) {
  1182. printk(KERN_ERR "sonypi: misc_register failed\n");
  1183. goto err_free_irq;
  1184. }
  1185. sonypi_display_info();
  1186. if (useinput) {
  1187. error = sonypi_create_input_devices(dev);
  1188. if (error) {
  1189. printk(KERN_ERR
  1190. "sonypi: failed to create input devices\n");
  1191. goto err_miscdev_unregister;
  1192. }
  1193. spin_lock_init(&sonypi_device.input_fifo_lock);
  1194. error = kfifo_alloc(&sonypi_device.input_fifo, SONYPI_BUF_SIZE,
  1195. GFP_KERNEL);
  1196. if (error) {
  1197. printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
  1198. goto err_inpdev_unregister;
  1199. }
  1200. INIT_WORK(&sonypi_device.input_work, input_keyrelease);
  1201. }
  1202. sonypi_enable(0);
  1203. return 0;
  1204. err_inpdev_unregister:
  1205. input_unregister_device(sonypi_device.input_key_dev);
  1206. input_unregister_device(sonypi_device.input_jog_dev);
  1207. err_miscdev_unregister:
  1208. misc_deregister(&sonypi_misc_device);
  1209. err_free_irq:
  1210. free_irq(sonypi_device.irq, sonypi_irq);
  1211. err_free_ioports:
  1212. release_region(sonypi_device.ioport1, sonypi_device.region_size);
  1213. err_disable_pcidev:
  1214. if (pcidev)
  1215. pci_disable_device(pcidev);
  1216. err_put_pcidev:
  1217. pci_dev_put(pcidev);
  1218. kfifo_free(&sonypi_device.fifo);
  1219. return error;
  1220. }
  1221. static int sonypi_remove(struct platform_device *dev)
  1222. {
  1223. sonypi_disable();
  1224. synchronize_irq(sonypi_device.irq);
  1225. flush_work(&sonypi_device.input_work);
  1226. if (useinput) {
  1227. input_unregister_device(sonypi_device.input_key_dev);
  1228. input_unregister_device(sonypi_device.input_jog_dev);
  1229. kfifo_free(&sonypi_device.input_fifo);
  1230. }
  1231. misc_deregister(&sonypi_misc_device);
  1232. free_irq(sonypi_device.irq, sonypi_irq);
  1233. release_region(sonypi_device.ioport1, sonypi_device.region_size);
  1234. if (sonypi_device.dev) {
  1235. pci_disable_device(sonypi_device.dev);
  1236. pci_dev_put(sonypi_device.dev);
  1237. }
  1238. kfifo_free(&sonypi_device.fifo);
  1239. return 0;
  1240. }
  1241. #ifdef CONFIG_PM_SLEEP
  1242. static int old_camera_power;
  1243. static int sonypi_suspend(struct device *dev)
  1244. {
  1245. old_camera_power = sonypi_device.camera_power;
  1246. sonypi_disable();
  1247. return 0;
  1248. }
  1249. static int sonypi_resume(struct device *dev)
  1250. {
  1251. sonypi_enable(old_camera_power);
  1252. return 0;
  1253. }
  1254. static SIMPLE_DEV_PM_OPS(sonypi_pm, sonypi_suspend, sonypi_resume);
  1255. #define SONYPI_PM (&sonypi_pm)
  1256. #else
  1257. #define SONYPI_PM NULL
  1258. #endif
  1259. static void sonypi_shutdown(struct platform_device *dev)
  1260. {
  1261. sonypi_disable();
  1262. }
  1263. static struct platform_driver sonypi_driver = {
  1264. .driver = {
  1265. .name = "sonypi",
  1266. .pm = SONYPI_PM,
  1267. },
  1268. .probe = sonypi_probe,
  1269. .remove = sonypi_remove,
  1270. .shutdown = sonypi_shutdown,
  1271. };
  1272. static struct platform_device *sonypi_platform_device;
  1273. static const struct dmi_system_id sonypi_dmi_table[] __initconst = {
  1274. {
  1275. .ident = "Sony Vaio",
  1276. .matches = {
  1277. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1278. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  1279. },
  1280. },
  1281. {
  1282. .ident = "Sony Vaio",
  1283. .matches = {
  1284. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1285. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  1286. },
  1287. },
  1288. { }
  1289. };
  1290. static int __init sonypi_init(void)
  1291. {
  1292. int error;
  1293. printk(KERN_INFO
  1294. "sonypi: Sony Programmable I/O Controller Driver v%s.\n",
  1295. SONYPI_DRIVER_VERSION);
  1296. if (!dmi_check_system(sonypi_dmi_table))
  1297. return -ENODEV;
  1298. error = platform_driver_register(&sonypi_driver);
  1299. if (error)
  1300. return error;
  1301. sonypi_platform_device = platform_device_alloc("sonypi", -1);
  1302. if (!sonypi_platform_device) {
  1303. error = -ENOMEM;
  1304. goto err_driver_unregister;
  1305. }
  1306. error = platform_device_add(sonypi_platform_device);
  1307. if (error)
  1308. goto err_free_device;
  1309. #ifdef CONFIG_ACPI
  1310. if (acpi_bus_register_driver(&sonypi_acpi_driver) >= 0)
  1311. acpi_driver_registered = 1;
  1312. #endif
  1313. return 0;
  1314. err_free_device:
  1315. platform_device_put(sonypi_platform_device);
  1316. err_driver_unregister:
  1317. platform_driver_unregister(&sonypi_driver);
  1318. return error;
  1319. }
  1320. static void __exit sonypi_exit(void)
  1321. {
  1322. #ifdef CONFIG_ACPI
  1323. if (acpi_driver_registered)
  1324. acpi_bus_unregister_driver(&sonypi_acpi_driver);
  1325. #endif
  1326. platform_device_unregister(sonypi_platform_device);
  1327. platform_driver_unregister(&sonypi_driver);
  1328. printk(KERN_INFO "sonypi: removed.\n");
  1329. }
  1330. module_init(sonypi_init);
  1331. module_exit(sonypi_exit);