sonypi.c 41 KB

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