sonypi.c 37 KB

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