sonypi.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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. u16 irq;
  416. u16 bits;
  417. u16 ioport1;
  418. u16 ioport2;
  419. u16 region_size;
  420. u16 evtype_offset;
  421. int camera_power;
  422. int bluetooth_power;
  423. struct semaphore lock;
  424. struct kfifo *fifo;
  425. spinlock_t fifo_lock;
  426. wait_queue_head_t fifo_proc_list;
  427. struct fasync_struct *fifo_async;
  428. int open_count;
  429. int model;
  430. struct input_dev *input_jog_dev;
  431. struct input_dev *input_key_dev;
  432. struct work_struct input_work;
  433. struct kfifo *input_fifo;
  434. spinlock_t input_fifo_lock;
  435. } sonypi_device;
  436. #define ITERATIONS_LONG 10000
  437. #define ITERATIONS_SHORT 10
  438. #define wait_on_command(quiet, command, iterations) { \
  439. unsigned int n = iterations; \
  440. while (--n && (command)) \
  441. udelay(1); \
  442. if (!n && (verbose || !quiet)) \
  443. printk(KERN_WARNING "sonypi command failed at %s : %s (line %d)\n", __FILE__, __FUNCTION__, __LINE__); \
  444. }
  445. #ifdef CONFIG_ACPI
  446. #define SONYPI_ACPI_ACTIVE (!acpi_disabled)
  447. #else
  448. #define SONYPI_ACPI_ACTIVE 0
  449. #endif /* CONFIG_ACPI */
  450. #ifdef CONFIG_ACPI
  451. static struct acpi_device *sonypi_acpi_device;
  452. static int acpi_enabled;
  453. #endif
  454. static int sonypi_ec_write(u8 addr, u8 value)
  455. {
  456. #ifdef CONFIG_ACPI_EC
  457. if (SONYPI_ACPI_ACTIVE)
  458. return ec_write(addr, value);
  459. #endif
  460. wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
  461. outb_p(0x81, SONYPI_CST_IOPORT);
  462. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  463. outb_p(addr, SONYPI_DATA_IOPORT);
  464. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  465. outb_p(value, SONYPI_DATA_IOPORT);
  466. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  467. return 0;
  468. }
  469. static int sonypi_ec_read(u8 addr, u8 *value)
  470. {
  471. #ifdef CONFIG_ACPI_EC
  472. if (SONYPI_ACPI_ACTIVE)
  473. return ec_read(addr, value);
  474. #endif
  475. wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
  476. outb_p(0x80, SONYPI_CST_IOPORT);
  477. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  478. outb_p(addr, SONYPI_DATA_IOPORT);
  479. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  480. *value = inb_p(SONYPI_DATA_IOPORT);
  481. return 0;
  482. }
  483. static int ec_read16(u8 addr, u16 *value)
  484. {
  485. u8 val_lb, val_hb;
  486. if (sonypi_ec_read(addr, &val_lb))
  487. return -1;
  488. if (sonypi_ec_read(addr + 1, &val_hb))
  489. return -1;
  490. *value = val_lb | (val_hb << 8);
  491. return 0;
  492. }
  493. /* Initializes the device - this comes from the AML code in the ACPI bios */
  494. static void sonypi_type1_srs(void)
  495. {
  496. u32 v;
  497. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  498. v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
  499. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  500. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  501. v = (v & 0xFFF0FFFF) |
  502. (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
  503. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  504. v = inl(SONYPI_IRQ_PORT);
  505. v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
  506. v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
  507. outl(v, SONYPI_IRQ_PORT);
  508. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  509. v = (v & 0xFF1FFFFF) | 0x00C00000;
  510. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  511. }
  512. static void sonypi_type2_srs(void)
  513. {
  514. if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
  515. printk(KERN_WARNING "ec_write failed\n");
  516. if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
  517. printk(KERN_WARNING "ec_write failed\n");
  518. if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
  519. printk(KERN_WARNING "ec_write failed\n");
  520. udelay(10);
  521. }
  522. static void sonypi_type3_srs(void)
  523. {
  524. u16 v16;
  525. u8 v8;
  526. /* This model type uses the same initialiazation of
  527. * the embedded controller as the type2 models. */
  528. sonypi_type2_srs();
  529. /* Initialization of PCI config space of the LPC interface bridge. */
  530. v16 = (sonypi_device.ioport1 & 0xFFF0) | 0x01;
  531. pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, v16);
  532. pci_read_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, &v8);
  533. v8 = (v8 & 0xCF) | 0x10;
  534. pci_write_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, v8);
  535. }
  536. /* Disables the device - this comes from the AML code in the ACPI bios */
  537. static void sonypi_type1_dis(void)
  538. {
  539. u32 v;
  540. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  541. v = v & 0xFF3FFFFF;
  542. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  543. v = inl(SONYPI_IRQ_PORT);
  544. v |= (0x3 << SONYPI_IRQ_SHIFT);
  545. outl(v, SONYPI_IRQ_PORT);
  546. }
  547. static void sonypi_type2_dis(void)
  548. {
  549. if (sonypi_ec_write(SONYPI_SHIB, 0))
  550. printk(KERN_WARNING "ec_write failed\n");
  551. if (sonypi_ec_write(SONYPI_SLOB, 0))
  552. printk(KERN_WARNING "ec_write failed\n");
  553. if (sonypi_ec_write(SONYPI_SIRQ, 0))
  554. printk(KERN_WARNING "ec_write failed\n");
  555. }
  556. static void sonypi_type3_dis(void)
  557. {
  558. sonypi_type2_dis();
  559. udelay(10);
  560. pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, 0);
  561. }
  562. static u8 sonypi_call1(u8 dev)
  563. {
  564. u8 v1, v2;
  565. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  566. outb(dev, sonypi_device.ioport2);
  567. v1 = inb_p(sonypi_device.ioport2);
  568. v2 = inb_p(sonypi_device.ioport1);
  569. return v2;
  570. }
  571. static u8 sonypi_call2(u8 dev, u8 fn)
  572. {
  573. u8 v1;
  574. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  575. outb(dev, sonypi_device.ioport2);
  576. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  577. outb(fn, sonypi_device.ioport1);
  578. v1 = inb_p(sonypi_device.ioport1);
  579. return v1;
  580. }
  581. static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
  582. {
  583. u8 v1;
  584. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  585. outb(dev, sonypi_device.ioport2);
  586. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  587. outb(fn, sonypi_device.ioport1);
  588. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  589. outb(v, sonypi_device.ioport1);
  590. v1 = inb_p(sonypi_device.ioport1);
  591. return v1;
  592. }
  593. #if 0
  594. /* Get brightness, hue etc. Unreliable... */
  595. static u8 sonypi_read(u8 fn)
  596. {
  597. u8 v1, v2;
  598. int n = 100;
  599. while (n--) {
  600. v1 = sonypi_call2(0x8f, fn);
  601. v2 = sonypi_call2(0x8f, fn);
  602. if (v1 == v2 && v1 != 0xff)
  603. return v1;
  604. }
  605. return 0xff;
  606. }
  607. #endif
  608. /* Set brightness, hue etc */
  609. static void sonypi_set(u8 fn, u8 v)
  610. {
  611. wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
  612. }
  613. /* Tests if the camera is ready */
  614. static int sonypi_camera_ready(void)
  615. {
  616. u8 v;
  617. v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
  618. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  619. }
  620. /* Turns the camera off */
  621. static void sonypi_camera_off(void)
  622. {
  623. sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
  624. if (!sonypi_device.camera_power)
  625. return;
  626. sonypi_call2(0x91, 0);
  627. sonypi_device.camera_power = 0;
  628. }
  629. /* Turns the camera on */
  630. static void sonypi_camera_on(void)
  631. {
  632. int i, j;
  633. if (sonypi_device.camera_power)
  634. return;
  635. for (j = 5; j > 0; j--) {
  636. while (sonypi_call2(0x91, 0x1))
  637. msleep(10);
  638. sonypi_call1(0x93);
  639. for (i = 400; i > 0; i--) {
  640. if (sonypi_camera_ready())
  641. break;
  642. msleep(10);
  643. }
  644. if (i)
  645. break;
  646. }
  647. if (j == 0) {
  648. printk(KERN_WARNING "sonypi: failed to power on camera\n");
  649. return;
  650. }
  651. sonypi_set(0x10, 0x5a);
  652. sonypi_device.camera_power = 1;
  653. }
  654. /* sets the bluetooth subsystem power state */
  655. static void sonypi_setbluetoothpower(u8 state)
  656. {
  657. state = !!state;
  658. if (sonypi_device.bluetooth_power == state)
  659. return;
  660. sonypi_call2(0x96, state);
  661. sonypi_call1(0x82);
  662. sonypi_device.bluetooth_power = state;
  663. }
  664. static void input_keyrelease(void *data)
  665. {
  666. struct sonypi_keypress kp;
  667. while (kfifo_get(sonypi_device.input_fifo, (unsigned char *)&kp,
  668. sizeof(kp)) == sizeof(kp)) {
  669. msleep(10);
  670. input_report_key(kp.dev, kp.key, 0);
  671. input_sync(kp.dev);
  672. }
  673. }
  674. static void sonypi_report_input_event(u8 event)
  675. {
  676. struct input_dev *jog_dev = sonypi_device.input_jog_dev;
  677. struct input_dev *key_dev = sonypi_device.input_key_dev;
  678. struct sonypi_keypress kp = { NULL };
  679. int i;
  680. switch (event) {
  681. case SONYPI_EVENT_JOGDIAL_UP:
  682. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  683. input_report_rel(jog_dev, REL_WHEEL, 1);
  684. input_sync(jog_dev);
  685. break;
  686. case SONYPI_EVENT_JOGDIAL_DOWN:
  687. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  688. input_report_rel(jog_dev, REL_WHEEL, -1);
  689. input_sync(jog_dev);
  690. break;
  691. case SONYPI_EVENT_JOGDIAL_PRESSED:
  692. kp.key = BTN_MIDDLE;
  693. kp.dev = jog_dev;
  694. break;
  695. case SONYPI_EVENT_FNKEY_RELEASED:
  696. /* Nothing, not all VAIOs generate this event */
  697. break;
  698. default:
  699. for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
  700. if (event == sonypi_inputkeys[i].sonypiev) {
  701. kp.dev = key_dev;
  702. kp.key = sonypi_inputkeys[i].inputev;
  703. break;
  704. }
  705. break;
  706. }
  707. if (kp.dev) {
  708. input_report_key(kp.dev, kp.key, 1);
  709. input_sync(kp.dev);
  710. kfifo_put(sonypi_device.input_fifo,
  711. (unsigned char *)&kp, sizeof(kp));
  712. schedule_work(&sonypi_device.input_work);
  713. }
  714. }
  715. /* Interrupt handler: some event is available */
  716. static irqreturn_t sonypi_irq(int irq, void *dev_id, struct pt_regs *regs)
  717. {
  718. u8 v1, v2, event = 0;
  719. int i, j;
  720. v1 = inb_p(sonypi_device.ioport1);
  721. v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
  722. for (i = 0; sonypi_eventtypes[i].model; i++) {
  723. if (sonypi_device.model != sonypi_eventtypes[i].model)
  724. continue;
  725. if ((v2 & sonypi_eventtypes[i].data) !=
  726. sonypi_eventtypes[i].data)
  727. continue;
  728. if (!(mask & sonypi_eventtypes[i].mask))
  729. continue;
  730. for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
  731. if (v1 == sonypi_eventtypes[i].events[j].data) {
  732. event = sonypi_eventtypes[i].events[j].event;
  733. goto found;
  734. }
  735. }
  736. }
  737. if (verbose)
  738. printk(KERN_WARNING
  739. "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
  740. v1, v2);
  741. /* We need to return IRQ_HANDLED here because there *are*
  742. * events belonging to the sonypi device we don't know about,
  743. * but we still don't want those to pollute the logs... */
  744. return IRQ_HANDLED;
  745. found:
  746. if (verbose > 1)
  747. printk(KERN_INFO
  748. "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
  749. if (useinput)
  750. sonypi_report_input_event(event);
  751. #ifdef CONFIG_ACPI
  752. if (acpi_enabled)
  753. acpi_bus_generate_event(sonypi_acpi_device, 1, event);
  754. #endif
  755. kfifo_put(sonypi_device.fifo, (unsigned char *)&event, sizeof(event));
  756. kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
  757. wake_up_interruptible(&sonypi_device.fifo_proc_list);
  758. return IRQ_HANDLED;
  759. }
  760. /* External camera command (exported to the motion eye v4l driver) */
  761. int sonypi_camera_command(int command, u8 value)
  762. {
  763. if (!camera)
  764. return -EIO;
  765. down(&sonypi_device.lock);
  766. switch (command) {
  767. case SONYPI_COMMAND_SETCAMERA:
  768. if (value)
  769. sonypi_camera_on();
  770. else
  771. sonypi_camera_off();
  772. break;
  773. case SONYPI_COMMAND_SETCAMERABRIGHTNESS:
  774. sonypi_set(SONYPI_CAMERA_BRIGHTNESS, value);
  775. break;
  776. case SONYPI_COMMAND_SETCAMERACONTRAST:
  777. sonypi_set(SONYPI_CAMERA_CONTRAST, value);
  778. break;
  779. case SONYPI_COMMAND_SETCAMERAHUE:
  780. sonypi_set(SONYPI_CAMERA_HUE, value);
  781. break;
  782. case SONYPI_COMMAND_SETCAMERACOLOR:
  783. sonypi_set(SONYPI_CAMERA_COLOR, value);
  784. break;
  785. case SONYPI_COMMAND_SETCAMERASHARPNESS:
  786. sonypi_set(SONYPI_CAMERA_SHARPNESS, value);
  787. break;
  788. case SONYPI_COMMAND_SETCAMERAPICTURE:
  789. sonypi_set(SONYPI_CAMERA_PICTURE, value);
  790. break;
  791. case SONYPI_COMMAND_SETCAMERAAGC:
  792. sonypi_set(SONYPI_CAMERA_AGC, value);
  793. break;
  794. default:
  795. printk(KERN_ERR "sonypi: sonypi_camera_command invalid: %d\n",
  796. command);
  797. break;
  798. }
  799. up(&sonypi_device.lock);
  800. return 0;
  801. }
  802. EXPORT_SYMBOL(sonypi_camera_command);
  803. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  804. {
  805. int retval;
  806. retval = fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
  807. if (retval < 0)
  808. return retval;
  809. return 0;
  810. }
  811. static int sonypi_misc_release(struct inode *inode, struct file *file)
  812. {
  813. sonypi_misc_fasync(-1, file, 0);
  814. down(&sonypi_device.lock);
  815. sonypi_device.open_count--;
  816. up(&sonypi_device.lock);
  817. return 0;
  818. }
  819. static int sonypi_misc_open(struct inode *inode, struct file *file)
  820. {
  821. down(&sonypi_device.lock);
  822. /* Flush input queue on first open */
  823. if (!sonypi_device.open_count)
  824. kfifo_reset(sonypi_device.fifo);
  825. sonypi_device.open_count++;
  826. up(&sonypi_device.lock);
  827. return 0;
  828. }
  829. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  830. size_t count, loff_t *pos)
  831. {
  832. ssize_t ret;
  833. unsigned char c;
  834. if ((kfifo_len(sonypi_device.fifo) == 0) &&
  835. (file->f_flags & O_NONBLOCK))
  836. return -EAGAIN;
  837. ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
  838. kfifo_len(sonypi_device.fifo) != 0);
  839. if (ret)
  840. return ret;
  841. while (ret < count &&
  842. (kfifo_get(sonypi_device.fifo, &c, sizeof(c)) == sizeof(c))) {
  843. if (put_user(c, buf++))
  844. return -EFAULT;
  845. ret++;
  846. }
  847. if (ret > 0) {
  848. struct inode *inode = file->f_dentry->d_inode;
  849. inode->i_atime = current_fs_time(inode->i_sb);
  850. }
  851. return ret;
  852. }
  853. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  854. {
  855. poll_wait(file, &sonypi_device.fifo_proc_list, wait);
  856. if (kfifo_len(sonypi_device.fifo))
  857. return POLLIN | POLLRDNORM;
  858. return 0;
  859. }
  860. static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
  861. unsigned int cmd, unsigned long arg)
  862. {
  863. int ret = 0;
  864. void __user *argp = (void __user *)arg;
  865. u8 val8;
  866. u16 val16;
  867. down(&sonypi_device.lock);
  868. switch (cmd) {
  869. case SONYPI_IOCGBRT:
  870. if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
  871. ret = -EIO;
  872. break;
  873. }
  874. if (copy_to_user(argp, &val8, sizeof(val8)))
  875. ret = -EFAULT;
  876. break;
  877. case SONYPI_IOCSBRT:
  878. if (copy_from_user(&val8, argp, sizeof(val8))) {
  879. ret = -EFAULT;
  880. break;
  881. }
  882. if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
  883. ret = -EIO;
  884. break;
  885. case SONYPI_IOCGBAT1CAP:
  886. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  887. ret = -EIO;
  888. break;
  889. }
  890. if (copy_to_user(argp, &val16, sizeof(val16)))
  891. ret = -EFAULT;
  892. break;
  893. case SONYPI_IOCGBAT1REM:
  894. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  895. ret = -EIO;
  896. break;
  897. }
  898. if (copy_to_user(argp, &val16, sizeof(val16)))
  899. ret = -EFAULT;
  900. break;
  901. case SONYPI_IOCGBAT2CAP:
  902. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  903. ret = -EIO;
  904. break;
  905. }
  906. if (copy_to_user(argp, &val16, sizeof(val16)))
  907. ret = -EFAULT;
  908. break;
  909. case SONYPI_IOCGBAT2REM:
  910. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  911. ret = -EIO;
  912. break;
  913. }
  914. if (copy_to_user(argp, &val16, sizeof(val16)))
  915. ret = -EFAULT;
  916. break;
  917. case SONYPI_IOCGBATFLAGS:
  918. if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
  919. ret = -EIO;
  920. break;
  921. }
  922. val8 &= 0x07;
  923. if (copy_to_user(argp, &val8, sizeof(val8)))
  924. ret = -EFAULT;
  925. break;
  926. case SONYPI_IOCGBLUE:
  927. val8 = sonypi_device.bluetooth_power;
  928. if (copy_to_user(argp, &val8, sizeof(val8)))
  929. ret = -EFAULT;
  930. break;
  931. case SONYPI_IOCSBLUE:
  932. if (copy_from_user(&val8, argp, sizeof(val8))) {
  933. ret = -EFAULT;
  934. break;
  935. }
  936. sonypi_setbluetoothpower(val8);
  937. break;
  938. /* FAN Controls */
  939. case SONYPI_IOCGFAN:
  940. if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
  941. ret = -EIO;
  942. break;
  943. }
  944. if (copy_to_user(argp, &val8, sizeof(val8)))
  945. ret = -EFAULT;
  946. break;
  947. case SONYPI_IOCSFAN:
  948. if (copy_from_user(&val8, argp, sizeof(val8))) {
  949. ret = -EFAULT;
  950. break;
  951. }
  952. if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
  953. ret = -EIO;
  954. break;
  955. /* GET Temperature (useful under APM) */
  956. case SONYPI_IOCGTEMP:
  957. if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
  958. ret = -EIO;
  959. break;
  960. }
  961. if (copy_to_user(argp, &val8, sizeof(val8)))
  962. ret = -EFAULT;
  963. break;
  964. default:
  965. ret = -EINVAL;
  966. }
  967. up(&sonypi_device.lock);
  968. return ret;
  969. }
  970. static struct file_operations sonypi_misc_fops = {
  971. .owner = THIS_MODULE,
  972. .read = sonypi_misc_read,
  973. .poll = sonypi_misc_poll,
  974. .open = sonypi_misc_open,
  975. .release = sonypi_misc_release,
  976. .fasync = sonypi_misc_fasync,
  977. .ioctl = sonypi_misc_ioctl,
  978. };
  979. static struct miscdevice sonypi_misc_device = {
  980. .minor = MISC_DYNAMIC_MINOR,
  981. .name = "sonypi",
  982. .fops = &sonypi_misc_fops,
  983. };
  984. static void sonypi_enable(unsigned int camera_on)
  985. {
  986. switch (sonypi_device.model) {
  987. case SONYPI_DEVICE_MODEL_TYPE1:
  988. sonypi_type1_srs();
  989. break;
  990. case SONYPI_DEVICE_MODEL_TYPE2:
  991. sonypi_type2_srs();
  992. break;
  993. case SONYPI_DEVICE_MODEL_TYPE3:
  994. sonypi_type3_srs();
  995. break;
  996. }
  997. sonypi_call1(0x82);
  998. sonypi_call2(0x81, 0xff);
  999. sonypi_call1(compat ? 0x92 : 0x82);
  1000. /* Enable ACPI mode to get Fn key events */
  1001. if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
  1002. outb(0xf0, 0xb2);
  1003. if (camera && camera_on)
  1004. sonypi_camera_on();
  1005. }
  1006. static int sonypi_disable(void)
  1007. {
  1008. sonypi_call2(0x81, 0); /* make sure we don't get any more events */
  1009. if (camera)
  1010. sonypi_camera_off();
  1011. /* disable ACPI mode */
  1012. if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
  1013. outb(0xf1, 0xb2);
  1014. switch (sonypi_device.model) {
  1015. case SONYPI_DEVICE_MODEL_TYPE1:
  1016. sonypi_type1_dis();
  1017. break;
  1018. case SONYPI_DEVICE_MODEL_TYPE2:
  1019. sonypi_type2_dis();
  1020. break;
  1021. case SONYPI_DEVICE_MODEL_TYPE3:
  1022. sonypi_type3_dis();
  1023. break;
  1024. }
  1025. return 0;
  1026. }
  1027. #ifdef CONFIG_ACPI
  1028. static int sonypi_acpi_add(struct acpi_device *device)
  1029. {
  1030. sonypi_acpi_device = device;
  1031. strcpy(acpi_device_name(device), "Sony laptop hotkeys");
  1032. strcpy(acpi_device_class(device), "sony/hotkey");
  1033. return 0;
  1034. }
  1035. static int sonypi_acpi_remove(struct acpi_device *device, int type)
  1036. {
  1037. sonypi_acpi_device = NULL;
  1038. return 0;
  1039. }
  1040. static struct acpi_driver sonypi_acpi_driver = {
  1041. .name = "sonypi",
  1042. .class = "hkey",
  1043. .ids = "SNY6001",
  1044. .ops = {
  1045. .add = sonypi_acpi_add,
  1046. .remove = sonypi_acpi_remove,
  1047. },
  1048. };
  1049. #endif
  1050. static int __devinit sonypi_create_input_devices(void)
  1051. {
  1052. struct input_dev *jog_dev;
  1053. struct input_dev *key_dev;
  1054. int i;
  1055. int error;
  1056. sonypi_device.input_jog_dev = jog_dev = input_allocate_device();
  1057. if (!jog_dev)
  1058. return -ENOMEM;
  1059. jog_dev->name = "Sony Vaio Jogdial";
  1060. jog_dev->id.bustype = BUS_ISA;
  1061. jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
  1062. jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  1063. jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
  1064. jog_dev->relbit[0] = BIT(REL_WHEEL);
  1065. sonypi_device.input_key_dev = key_dev = input_allocate_device();
  1066. if (!key_dev) {
  1067. error = -ENOMEM;
  1068. goto err_free_jogdev;
  1069. }
  1070. key_dev->name = "Sony Vaio Keys";
  1071. key_dev->id.bustype = BUS_ISA;
  1072. key_dev->id.vendor = PCI_VENDOR_ID_SONY;
  1073. /* Initialize the Input Drivers: special keys */
  1074. key_dev->evbit[0] = BIT(EV_KEY);
  1075. for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
  1076. if (sonypi_inputkeys[i].inputev)
  1077. set_bit(sonypi_inputkeys[i].inputev, key_dev->keybit);
  1078. error = input_register_device(jog_dev);
  1079. if (error)
  1080. goto err_free_keydev;
  1081. error = input_register_device(key_dev);
  1082. if (error)
  1083. goto err_unregister_jogdev;
  1084. return 0;
  1085. err_unregister_jogdev:
  1086. input_unregister_device(jog_dev);
  1087. /* Set to NULL so we don't free it again below */
  1088. jog_dev = NULL;
  1089. err_free_keydev:
  1090. input_free_device(key_dev);
  1091. sonypi_device.input_key_dev = NULL;
  1092. err_free_jogdev:
  1093. input_free_device(jog_dev);
  1094. sonypi_device.input_jog_dev = NULL;
  1095. return error;
  1096. }
  1097. static int __devinit sonypi_setup_ioports(struct sonypi_device *dev,
  1098. const struct sonypi_ioport_list *ioport_list)
  1099. {
  1100. while (ioport_list->port1) {
  1101. if (request_region(ioport_list->port1,
  1102. sonypi_device.region_size,
  1103. "Sony Programable I/O Device")) {
  1104. dev->ioport1 = ioport_list->port1;
  1105. dev->ioport2 = ioport_list->port2;
  1106. return 0;
  1107. }
  1108. ioport_list++;
  1109. }
  1110. return -EBUSY;
  1111. }
  1112. static int __devinit sonypi_setup_irq(struct sonypi_device *dev,
  1113. const struct sonypi_irq_list *irq_list)
  1114. {
  1115. while (irq_list->irq) {
  1116. if (!request_irq(irq_list->irq, sonypi_irq,
  1117. SA_SHIRQ, "sonypi", sonypi_irq)) {
  1118. dev->irq = irq_list->irq;
  1119. dev->bits = irq_list->bits;
  1120. return 0;
  1121. }
  1122. irq_list++;
  1123. }
  1124. return -EBUSY;
  1125. }
  1126. static void __devinit sonypi_display_info(void)
  1127. {
  1128. printk(KERN_INFO "sonypi: detected type%d model, "
  1129. "verbose = %d, fnkeyinit = %s, camera = %s, "
  1130. "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
  1131. sonypi_device.model,
  1132. verbose,
  1133. fnkeyinit ? "on" : "off",
  1134. camera ? "on" : "off",
  1135. compat ? "on" : "off",
  1136. mask,
  1137. useinput ? "on" : "off",
  1138. SONYPI_ACPI_ACTIVE ? "on" : "off");
  1139. printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
  1140. sonypi_device.irq,
  1141. sonypi_device.ioport1, sonypi_device.ioport2);
  1142. if (minor == -1)
  1143. printk(KERN_INFO "sonypi: device allocated minor is %d\n",
  1144. sonypi_misc_device.minor);
  1145. }
  1146. static int __devinit sonypi_probe(struct platform_device *dev)
  1147. {
  1148. const struct sonypi_ioport_list *ioport_list;
  1149. const struct sonypi_irq_list *irq_list;
  1150. struct pci_dev *pcidev;
  1151. int error;
  1152. spin_lock_init(&sonypi_device.fifo_lock);
  1153. sonypi_device.fifo = kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
  1154. &sonypi_device.fifo_lock);
  1155. if (IS_ERR(sonypi_device.fifo)) {
  1156. printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
  1157. return PTR_ERR(sonypi_device.fifo);
  1158. }
  1159. init_waitqueue_head(&sonypi_device.fifo_proc_list);
  1160. init_MUTEX(&sonypi_device.lock);
  1161. sonypi_device.bluetooth_power = -1;
  1162. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1163. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  1164. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
  1165. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1166. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  1167. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
  1168. else
  1169. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
  1170. if (pcidev && pci_enable_device(pcidev)) {
  1171. printk(KERN_ERR "sonypi: pci_enable_device failed\n");
  1172. error = -EIO;
  1173. goto err_put_pcidev;
  1174. }
  1175. sonypi_device.dev = pcidev;
  1176. if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
  1177. ioport_list = sonypi_type1_ioport_list;
  1178. sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
  1179. sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
  1180. irq_list = sonypi_type1_irq_list;
  1181. } else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
  1182. ioport_list = sonypi_type2_ioport_list;
  1183. sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
  1184. sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
  1185. irq_list = sonypi_type2_irq_list;
  1186. } else {
  1187. ioport_list = sonypi_type3_ioport_list;
  1188. sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
  1189. sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
  1190. irq_list = sonypi_type3_irq_list;
  1191. }
  1192. error = sonypi_setup_ioports(&sonypi_device, ioport_list);
  1193. if (error) {
  1194. printk(KERN_ERR "sonypi: failed to request ioports\n");
  1195. goto err_disable_pcidev;
  1196. }
  1197. error = sonypi_setup_irq(&sonypi_device, irq_list);
  1198. if (error) {
  1199. printk(KERN_ERR "sonypi: request_irq failed\n");
  1200. goto err_free_ioports;
  1201. }
  1202. if (minor != -1)
  1203. sonypi_misc_device.minor = minor;
  1204. error = misc_register(&sonypi_misc_device);
  1205. if (error) {
  1206. printk(KERN_ERR "sonypi: misc_register failed\n");
  1207. goto err_free_irq;
  1208. }
  1209. sonypi_display_info();
  1210. if (useinput) {
  1211. error = sonypi_create_input_devices();
  1212. if (error) {
  1213. printk(KERN_ERR
  1214. "sonypi: failed to create input devices\n");
  1215. goto err_miscdev_unregister;
  1216. }
  1217. spin_lock_init(&sonypi_device.input_fifo_lock);
  1218. sonypi_device.input_fifo =
  1219. kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
  1220. &sonypi_device.input_fifo_lock);
  1221. if (IS_ERR(sonypi_device.input_fifo)) {
  1222. printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
  1223. error = PTR_ERR(sonypi_device.input_fifo);
  1224. goto err_inpdev_unregister;
  1225. }
  1226. INIT_WORK(&sonypi_device.input_work, input_keyrelease, NULL);
  1227. }
  1228. sonypi_enable(0);
  1229. return 0;
  1230. err_inpdev_unregister:
  1231. input_unregister_device(sonypi_device.input_key_dev);
  1232. input_unregister_device(sonypi_device.input_jog_dev);
  1233. err_miscdev_unregister:
  1234. misc_deregister(&sonypi_misc_device);
  1235. err_free_irq:
  1236. free_irq(sonypi_device.irq, sonypi_irq);
  1237. err_free_ioports:
  1238. release_region(sonypi_device.ioport1, sonypi_device.region_size);
  1239. err_disable_pcidev:
  1240. if (pcidev)
  1241. pci_disable_device(pcidev);
  1242. err_put_pcidev:
  1243. pci_dev_put(pcidev);
  1244. kfifo_free(sonypi_device.fifo);
  1245. return error;
  1246. }
  1247. static int __devexit sonypi_remove(struct platform_device *dev)
  1248. {
  1249. sonypi_disable();
  1250. synchronize_sched(); /* Allow sonypi interrupt to complete. */
  1251. flush_scheduled_work();
  1252. if (useinput) {
  1253. input_unregister_device(sonypi_device.input_key_dev);
  1254. input_unregister_device(sonypi_device.input_jog_dev);
  1255. kfifo_free(sonypi_device.input_fifo);
  1256. }
  1257. misc_deregister(&sonypi_misc_device);
  1258. free_irq(sonypi_device.irq, sonypi_irq);
  1259. release_region(sonypi_device.ioport1, sonypi_device.region_size);
  1260. if (sonypi_device.dev) {
  1261. pci_disable_device(sonypi_device.dev);
  1262. pci_dev_put(sonypi_device.dev);
  1263. }
  1264. kfifo_free(sonypi_device.fifo);
  1265. return 0;
  1266. }
  1267. #ifdef CONFIG_PM
  1268. static int old_camera_power;
  1269. static int sonypi_suspend(struct platform_device *dev, pm_message_t state)
  1270. {
  1271. old_camera_power = sonypi_device.camera_power;
  1272. sonypi_disable();
  1273. return 0;
  1274. }
  1275. static int sonypi_resume(struct platform_device *dev)
  1276. {
  1277. sonypi_enable(old_camera_power);
  1278. return 0;
  1279. }
  1280. #else
  1281. #define sonypi_suspend NULL
  1282. #define sonypi_resume NULL
  1283. #endif
  1284. static void sonypi_shutdown(struct platform_device *dev)
  1285. {
  1286. sonypi_disable();
  1287. }
  1288. static struct platform_driver sonypi_driver = {
  1289. .driver = {
  1290. .name = "sonypi",
  1291. .owner = THIS_MODULE,
  1292. },
  1293. .probe = sonypi_probe,
  1294. .remove = __devexit_p(sonypi_remove),
  1295. .shutdown = sonypi_shutdown,
  1296. .suspend = sonypi_suspend,
  1297. .resume = sonypi_resume,
  1298. };
  1299. static struct platform_device *sonypi_platform_device;
  1300. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  1301. {
  1302. .ident = "Sony Vaio",
  1303. .matches = {
  1304. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1305. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  1306. },
  1307. },
  1308. {
  1309. .ident = "Sony Vaio",
  1310. .matches = {
  1311. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1312. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  1313. },
  1314. },
  1315. { }
  1316. };
  1317. static int __init sonypi_init(void)
  1318. {
  1319. int error;
  1320. printk(KERN_INFO
  1321. "sonypi: Sony Programmable I/O Controller Driver v%s.\n",
  1322. SONYPI_DRIVER_VERSION);
  1323. if (!dmi_check_system(sonypi_dmi_table))
  1324. return -ENODEV;
  1325. error = platform_driver_register(&sonypi_driver);
  1326. if (error)
  1327. return error;
  1328. sonypi_platform_device = platform_device_alloc("sonypi", -1);
  1329. if (!sonypi_platform_device) {
  1330. error = -ENOMEM;
  1331. goto err_driver_unregister;
  1332. }
  1333. error = platform_device_add(sonypi_platform_device);
  1334. if (error)
  1335. goto err_free_device;
  1336. #ifdef CONFIG_ACPI
  1337. if (acpi_bus_register_driver(&sonypi_acpi_driver) > 0)
  1338. acpi_enabled = 1;
  1339. #endif
  1340. return 0;
  1341. err_free_device:
  1342. platform_device_put(sonypi_platform_device);
  1343. err_driver_unregister:
  1344. platform_driver_unregister(&sonypi_driver);
  1345. return error;
  1346. }
  1347. static void __exit sonypi_exit(void)
  1348. {
  1349. #ifdef CONFIG_ACPI
  1350. if (acpi_enabled)
  1351. acpi_bus_unregister_driver(&sonypi_acpi_driver);
  1352. #endif
  1353. platform_device_unregister(sonypi_platform_device);
  1354. platform_driver_unregister(&sonypi_driver);
  1355. printk(KERN_INFO "sonypi: removed.\n");
  1356. }
  1357. module_init(sonypi_init);
  1358. module_exit(sonypi_exit);