sonypi.c 41 KB

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