sonypi.c 41 KB

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