sonypi.c 39 KB

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