sonypi.c 39 KB

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