sonypi.c 41 KB

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