sonypi.c 41 KB

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