sonypi.c 39 KB

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