sonypi.c 39 KB

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