sonypi.c 37 KB

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