sonypi.c 37 KB

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