sony-laptop.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  1. /*
  2. * ACPI Sony Notebook Control Driver (SNC and SPIC)
  3. *
  4. * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
  5. * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
  6. *
  7. * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
  8. * which are copyrighted by their respective authors.
  9. *
  10. * The SNY6001 driver part is based on the sonypi driver which includes
  11. * material from:
  12. *
  13. * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
  14. *
  15. * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
  16. *
  17. * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
  18. *
  19. * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
  20. *
  21. * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
  22. *
  23. * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
  24. *
  25. * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
  26. *
  27. * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. *
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/module.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/init.h>
  48. #include <linux/types.h>
  49. #include <linux/backlight.h>
  50. #include <linux/platform_device.h>
  51. #include <linux/err.h>
  52. #include <linux/dmi.h>
  53. #include <linux/pci.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/delay.h>
  56. #include <linux/input.h>
  57. #include <linux/kfifo.h>
  58. #include <linux/workqueue.h>
  59. #include <linux/acpi.h>
  60. #include <acpi/acpi_drivers.h>
  61. #include <acpi/acpi_bus.h>
  62. #include <asm/uaccess.h>
  63. #include <linux/sonypi.h>
  64. #include <linux/sony-laptop.h>
  65. #ifdef CONFIG_SONYPI_COMPAT
  66. #include <linux/poll.h>
  67. #include <linux/miscdevice.h>
  68. #endif
  69. #define DRV_PFX "sony-laptop: "
  70. #define dprintk(msg...) do { \
  71. if (debug) printk(KERN_WARNING DRV_PFX msg); \
  72. } while (0)
  73. #define SONY_LAPTOP_DRIVER_VERSION "0.5"
  74. #define SONY_NC_CLASS "sony-nc"
  75. #define SONY_NC_HID "SNY5001"
  76. #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
  77. #define SONY_PIC_CLASS "sony-pic"
  78. #define SONY_PIC_HID "SNY6001"
  79. #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
  80. MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
  81. MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
  84. static int debug;
  85. module_param(debug, int, 0);
  86. MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
  87. "the development of this driver");
  88. static int no_spic; /* = 0 */
  89. module_param(no_spic, int, 0444);
  90. MODULE_PARM_DESC(no_spic,
  91. "set this if you don't want to enable the SPIC device");
  92. static int compat; /* = 0 */
  93. module_param(compat, int, 0444);
  94. MODULE_PARM_DESC(compat,
  95. "set this if you want to enable backward compatibility mode");
  96. static unsigned long mask = 0xffffffff;
  97. module_param(mask, ulong, 0644);
  98. MODULE_PARM_DESC(mask,
  99. "set this to the mask of event you want to enable (see doc)");
  100. static int camera; /* = 0 */
  101. module_param(camera, int, 0444);
  102. MODULE_PARM_DESC(camera,
  103. "set this to 1 to enable Motion Eye camera controls "
  104. "(only use it if you have a C1VE or C1VN model)");
  105. #ifdef CONFIG_SONYPI_COMPAT
  106. static int minor = -1;
  107. module_param(minor, int, 0);
  108. MODULE_PARM_DESC(minor,
  109. "minor number of the misc device for the SPIC compatibility code, "
  110. "default is -1 (automatic)");
  111. #endif
  112. /*********** Input Devices ***********/
  113. #define SONY_LAPTOP_BUF_SIZE 128
  114. struct sony_laptop_input_s {
  115. atomic_t users;
  116. struct input_dev *jog_dev;
  117. struct input_dev *key_dev;
  118. struct kfifo *fifo;
  119. spinlock_t fifo_lock;
  120. struct workqueue_struct *wq;
  121. };
  122. static struct sony_laptop_input_s sony_laptop_input = {
  123. .users = ATOMIC_INIT(0),
  124. };
  125. struct sony_laptop_keypress {
  126. struct input_dev *dev;
  127. int key;
  128. };
  129. /* Correspondance table between sonypi events
  130. * and input layer indexes in the keymap
  131. */
  132. static int sony_laptop_input_index[] = {
  133. -1, /* no event */
  134. -1, /* SONYPI_EVENT_JOGDIAL_DOWN */
  135. -1, /* SONYPI_EVENT_JOGDIAL_UP */
  136. -1, /* SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
  137. -1, /* SONYPI_EVENT_JOGDIAL_UP_PRESSED */
  138. -1, /* SONYPI_EVENT_JOGDIAL_PRESSED */
  139. -1, /* SONYPI_EVENT_JOGDIAL_RELEASED */
  140. 0, /* SONYPI_EVENT_CAPTURE_PRESSED */
  141. 1, /* SONYPI_EVENT_CAPTURE_RELEASED */
  142. 2, /* SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
  143. 3, /* SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
  144. 4, /* SONYPI_EVENT_FNKEY_ESC */
  145. 5, /* SONYPI_EVENT_FNKEY_F1 */
  146. 6, /* SONYPI_EVENT_FNKEY_F2 */
  147. 7, /* SONYPI_EVENT_FNKEY_F3 */
  148. 8, /* SONYPI_EVENT_FNKEY_F4 */
  149. 9, /* SONYPI_EVENT_FNKEY_F5 */
  150. 10, /* SONYPI_EVENT_FNKEY_F6 */
  151. 11, /* SONYPI_EVENT_FNKEY_F7 */
  152. 12, /* SONYPI_EVENT_FNKEY_F8 */
  153. 13, /* SONYPI_EVENT_FNKEY_F9 */
  154. 14, /* SONYPI_EVENT_FNKEY_F10 */
  155. 15, /* SONYPI_EVENT_FNKEY_F11 */
  156. 16, /* SONYPI_EVENT_FNKEY_F12 */
  157. 17, /* SONYPI_EVENT_FNKEY_1 */
  158. 18, /* SONYPI_EVENT_FNKEY_2 */
  159. 19, /* SONYPI_EVENT_FNKEY_D */
  160. 20, /* SONYPI_EVENT_FNKEY_E */
  161. 21, /* SONYPI_EVENT_FNKEY_F */
  162. 22, /* SONYPI_EVENT_FNKEY_S */
  163. 23, /* SONYPI_EVENT_FNKEY_B */
  164. 24, /* SONYPI_EVENT_BLUETOOTH_PRESSED */
  165. 25, /* SONYPI_EVENT_PKEY_P1 */
  166. 26, /* SONYPI_EVENT_PKEY_P2 */
  167. 27, /* SONYPI_EVENT_PKEY_P3 */
  168. 28, /* SONYPI_EVENT_BACK_PRESSED */
  169. -1, /* SONYPI_EVENT_LID_CLOSED */
  170. -1, /* SONYPI_EVENT_LID_OPENED */
  171. 29, /* SONYPI_EVENT_BLUETOOTH_ON */
  172. 30, /* SONYPI_EVENT_BLUETOOTH_OFF */
  173. 31, /* SONYPI_EVENT_HELP_PRESSED */
  174. 32, /* SONYPI_EVENT_FNKEY_ONLY */
  175. 33, /* SONYPI_EVENT_JOGDIAL_FAST_DOWN */
  176. 34, /* SONYPI_EVENT_JOGDIAL_FAST_UP */
  177. 35, /* SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
  178. 36, /* SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
  179. 37, /* SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
  180. 38, /* SONYPI_EVENT_JOGDIAL_VFAST_UP */
  181. 39, /* SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
  182. 40, /* SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
  183. 41, /* SONYPI_EVENT_ZOOM_PRESSED */
  184. 42, /* SONYPI_EVENT_THUMBPHRASE_PRESSED */
  185. 43, /* SONYPI_EVENT_MEYE_FACE */
  186. 44, /* SONYPI_EVENT_MEYE_OPPOSITE */
  187. 45, /* SONYPI_EVENT_MEMORYSTICK_INSERT */
  188. 46, /* SONYPI_EVENT_MEMORYSTICK_EJECT */
  189. -1, /* SONYPI_EVENT_ANYBUTTON_RELEASED */
  190. -1, /* SONYPI_EVENT_BATTERY_INSERT */
  191. -1, /* SONYPI_EVENT_BATTERY_REMOVE */
  192. -1, /* SONYPI_EVENT_FNKEY_RELEASED */
  193. 47, /* SONYPI_EVENT_WIRELESS_ON */
  194. 48, /* SONYPI_EVENT_WIRELESS_OFF */
  195. };
  196. static int sony_laptop_input_keycode_map[] = {
  197. KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
  198. KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
  199. KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
  200. KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
  201. KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
  202. KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
  203. KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
  204. KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
  205. KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
  206. KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
  207. KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
  208. KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
  209. KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
  210. KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
  211. KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
  212. KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
  213. KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
  214. KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
  215. KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
  216. KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
  217. KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
  218. KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
  219. KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
  220. KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
  221. KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
  222. KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
  223. KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
  224. KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
  225. KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
  226. KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
  227. KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
  228. KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
  229. KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
  230. KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
  231. KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
  232. KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
  233. KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
  234. KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
  235. KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
  236. KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
  237. KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
  238. KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
  239. BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
  240. KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
  241. KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
  242. KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
  243. KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
  244. KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
  245. KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
  246. };
  247. /* release buttons after a short delay if pressed */
  248. static void do_sony_laptop_release_key(struct work_struct *work)
  249. {
  250. struct sony_laptop_keypress kp;
  251. while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
  252. sizeof(kp)) == sizeof(kp)) {
  253. msleep(10);
  254. input_report_key(kp.dev, kp.key, 0);
  255. input_sync(kp.dev);
  256. }
  257. }
  258. static DECLARE_WORK(sony_laptop_release_key_work,
  259. do_sony_laptop_release_key);
  260. /* forward event to the input subsystem */
  261. static void sony_laptop_report_input_event(u8 event)
  262. {
  263. struct input_dev *jog_dev = sony_laptop_input.jog_dev;
  264. struct input_dev *key_dev = sony_laptop_input.key_dev;
  265. struct sony_laptop_keypress kp = { NULL };
  266. if (event == SONYPI_EVENT_FNKEY_RELEASED) {
  267. /* Nothing, not all VAIOs generate this event */
  268. return;
  269. }
  270. /* report events */
  271. switch (event) {
  272. /* jog_dev events */
  273. case SONYPI_EVENT_JOGDIAL_UP:
  274. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  275. input_report_rel(jog_dev, REL_WHEEL, 1);
  276. input_sync(jog_dev);
  277. return;
  278. case SONYPI_EVENT_JOGDIAL_DOWN:
  279. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  280. input_report_rel(jog_dev, REL_WHEEL, -1);
  281. input_sync(jog_dev);
  282. return;
  283. /* key_dev events */
  284. case SONYPI_EVENT_JOGDIAL_PRESSED:
  285. kp.key = BTN_MIDDLE;
  286. kp.dev = jog_dev;
  287. break;
  288. default:
  289. if (event > ARRAY_SIZE (sony_laptop_input_keycode_map)) {
  290. dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
  291. break;
  292. }
  293. if (sony_laptop_input_index[event] != -1) {
  294. kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
  295. if (kp.key != KEY_UNKNOWN)
  296. kp.dev = key_dev;
  297. }
  298. break;
  299. }
  300. if (kp.dev) {
  301. input_report_key(kp.dev, kp.key, 1);
  302. /* we emit the scancode so we can always remap the key */
  303. input_event(kp.dev, EV_MSC, MSC_SCAN, event);
  304. input_sync(kp.dev);
  305. kfifo_put(sony_laptop_input.fifo,
  306. (unsigned char *)&kp, sizeof(kp));
  307. if (!work_pending(&sony_laptop_release_key_work))
  308. queue_work(sony_laptop_input.wq,
  309. &sony_laptop_release_key_work);
  310. } else
  311. dprintk("unknown input event %.2x\n", event);
  312. }
  313. static int sony_laptop_setup_input(struct acpi_device *acpi_device)
  314. {
  315. struct input_dev *jog_dev;
  316. struct input_dev *key_dev;
  317. int i;
  318. int error;
  319. /* don't run again if already initialized */
  320. if (atomic_add_return(1, &sony_laptop_input.users) > 1)
  321. return 0;
  322. /* kfifo */
  323. spin_lock_init(&sony_laptop_input.fifo_lock);
  324. sony_laptop_input.fifo =
  325. kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
  326. &sony_laptop_input.fifo_lock);
  327. if (IS_ERR(sony_laptop_input.fifo)) {
  328. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  329. error = PTR_ERR(sony_laptop_input.fifo);
  330. goto err_dec_users;
  331. }
  332. /* init workqueue */
  333. sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
  334. if (!sony_laptop_input.wq) {
  335. printk(KERN_ERR DRV_PFX
  336. "Unabe to create workqueue.\n");
  337. error = -ENXIO;
  338. goto err_free_kfifo;
  339. }
  340. /* input keys */
  341. key_dev = input_allocate_device();
  342. if (!key_dev) {
  343. error = -ENOMEM;
  344. goto err_destroy_wq;
  345. }
  346. key_dev->name = "Sony Vaio Keys";
  347. key_dev->id.bustype = BUS_ISA;
  348. key_dev->id.vendor = PCI_VENDOR_ID_SONY;
  349. key_dev->dev.parent = &acpi_device->dev;
  350. /* Initialize the Input Drivers: special keys */
  351. set_bit(EV_KEY, key_dev->evbit);
  352. set_bit(EV_MSC, key_dev->evbit);
  353. set_bit(MSC_SCAN, key_dev->mscbit);
  354. key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
  355. key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
  356. key_dev->keycode = &sony_laptop_input_keycode_map;
  357. for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++) {
  358. if (sony_laptop_input_keycode_map[i] != KEY_RESERVED) {
  359. set_bit(sony_laptop_input_keycode_map[i],
  360. key_dev->keybit);
  361. }
  362. }
  363. error = input_register_device(key_dev);
  364. if (error)
  365. goto err_free_keydev;
  366. sony_laptop_input.key_dev = key_dev;
  367. /* jogdial */
  368. jog_dev = input_allocate_device();
  369. if (!jog_dev) {
  370. error = -ENOMEM;
  371. goto err_unregister_keydev;
  372. }
  373. jog_dev->name = "Sony Vaio Jogdial";
  374. jog_dev->id.bustype = BUS_ISA;
  375. jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
  376. key_dev->dev.parent = &acpi_device->dev;
  377. jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  378. jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
  379. jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
  380. error = input_register_device(jog_dev);
  381. if (error)
  382. goto err_free_jogdev;
  383. sony_laptop_input.jog_dev = jog_dev;
  384. return 0;
  385. err_free_jogdev:
  386. input_free_device(jog_dev);
  387. err_unregister_keydev:
  388. input_unregister_device(key_dev);
  389. /* to avoid kref underflow below at input_free_device */
  390. key_dev = NULL;
  391. err_free_keydev:
  392. input_free_device(key_dev);
  393. err_destroy_wq:
  394. destroy_workqueue(sony_laptop_input.wq);
  395. err_free_kfifo:
  396. kfifo_free(sony_laptop_input.fifo);
  397. err_dec_users:
  398. atomic_dec(&sony_laptop_input.users);
  399. return error;
  400. }
  401. static void sony_laptop_remove_input(void)
  402. {
  403. /* cleanup only after the last user has gone */
  404. if (!atomic_dec_and_test(&sony_laptop_input.users))
  405. return;
  406. /* flush workqueue first */
  407. flush_workqueue(sony_laptop_input.wq);
  408. /* destroy input devs */
  409. input_unregister_device(sony_laptop_input.key_dev);
  410. sony_laptop_input.key_dev = NULL;
  411. if (sony_laptop_input.jog_dev) {
  412. input_unregister_device(sony_laptop_input.jog_dev);
  413. sony_laptop_input.jog_dev = NULL;
  414. }
  415. destroy_workqueue(sony_laptop_input.wq);
  416. kfifo_free(sony_laptop_input.fifo);
  417. }
  418. /*********** Platform Device ***********/
  419. static atomic_t sony_pf_users = ATOMIC_INIT(0);
  420. static struct platform_driver sony_pf_driver = {
  421. .driver = {
  422. .name = "sony-laptop",
  423. .owner = THIS_MODULE,
  424. }
  425. };
  426. static struct platform_device *sony_pf_device;
  427. static int sony_pf_add(void)
  428. {
  429. int ret = 0;
  430. /* don't run again if already initialized */
  431. if (atomic_add_return(1, &sony_pf_users) > 1)
  432. return 0;
  433. ret = platform_driver_register(&sony_pf_driver);
  434. if (ret)
  435. goto out;
  436. sony_pf_device = platform_device_alloc("sony-laptop", -1);
  437. if (!sony_pf_device) {
  438. ret = -ENOMEM;
  439. goto out_platform_registered;
  440. }
  441. ret = platform_device_add(sony_pf_device);
  442. if (ret)
  443. goto out_platform_alloced;
  444. return 0;
  445. out_platform_alloced:
  446. platform_device_put(sony_pf_device);
  447. sony_pf_device = NULL;
  448. out_platform_registered:
  449. platform_driver_unregister(&sony_pf_driver);
  450. out:
  451. atomic_dec(&sony_pf_users);
  452. return ret;
  453. }
  454. static void sony_pf_remove(void)
  455. {
  456. /* deregister only after the last user has gone */
  457. if (!atomic_dec_and_test(&sony_pf_users))
  458. return;
  459. platform_device_del(sony_pf_device);
  460. platform_device_put(sony_pf_device);
  461. platform_driver_unregister(&sony_pf_driver);
  462. }
  463. /*********** SNC (SNY5001) Device ***********/
  464. /* the device uses 1-based values, while the backlight subsystem uses
  465. 0-based values */
  466. #define SONY_MAX_BRIGHTNESS 8
  467. #define SNC_VALIDATE_IN 0
  468. #define SNC_VALIDATE_OUT 1
  469. static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
  470. char *);
  471. static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
  472. const char *, size_t);
  473. static int boolean_validate(const int, const int);
  474. static int brightness_default_validate(const int, const int);
  475. struct sony_nc_value {
  476. char *name; /* name of the entry */
  477. char **acpiget; /* names of the ACPI get function */
  478. char **acpiset; /* names of the ACPI set function */
  479. int (*validate)(const int, const int); /* input/output validation */
  480. int value; /* current setting */
  481. int valid; /* Has ever been set */
  482. int debug; /* active only in debug mode ? */
  483. struct device_attribute devattr; /* sysfs atribute */
  484. };
  485. #define SNC_HANDLE_NAMES(_name, _values...) \
  486. static char *snc_##_name[] = { _values, NULL }
  487. #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
  488. { \
  489. .name = __stringify(_name), \
  490. .acpiget = _getters, \
  491. .acpiset = _setters, \
  492. .validate = _validate, \
  493. .debug = _debug, \
  494. .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
  495. }
  496. #define SNC_HANDLE_NULL { .name = NULL }
  497. SNC_HANDLE_NAMES(fnkey_get, "GHKE");
  498. SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
  499. SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
  500. SNC_HANDLE_NAMES(cdpower_get, "GCDP");
  501. SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
  502. SNC_HANDLE_NAMES(audiopower_get, "GAZP");
  503. SNC_HANDLE_NAMES(audiopower_set, "AZPW");
  504. SNC_HANDLE_NAMES(lanpower_get, "GLNP");
  505. SNC_HANDLE_NAMES(lanpower_set, "LNPW");
  506. SNC_HANDLE_NAMES(lidstate_get, "GLID");
  507. SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
  508. SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
  509. SNC_HANDLE_NAMES(gainbass_get, "GMGB");
  510. SNC_HANDLE_NAMES(gainbass_set, "CMGB");
  511. SNC_HANDLE_NAMES(PID_get, "GPID");
  512. SNC_HANDLE_NAMES(CTR_get, "GCTR");
  513. SNC_HANDLE_NAMES(CTR_set, "SCTR");
  514. SNC_HANDLE_NAMES(PCR_get, "GPCR");
  515. SNC_HANDLE_NAMES(PCR_set, "SPCR");
  516. SNC_HANDLE_NAMES(CMI_get, "GCMI");
  517. SNC_HANDLE_NAMES(CMI_set, "SCMI");
  518. static struct sony_nc_value sony_nc_values[] = {
  519. SNC_HANDLE(brightness_default, snc_brightness_def_get,
  520. snc_brightness_def_set, brightness_default_validate, 0),
  521. SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
  522. SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
  523. SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
  524. boolean_validate, 0),
  525. SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
  526. boolean_validate, 1),
  527. SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
  528. boolean_validate, 0),
  529. SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
  530. boolean_validate, 0),
  531. SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
  532. boolean_validate, 0),
  533. /* unknown methods */
  534. SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
  535. SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
  536. SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
  537. SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
  538. SNC_HANDLE_NULL
  539. };
  540. static acpi_handle sony_nc_acpi_handle;
  541. static struct acpi_device *sony_nc_acpi_device = NULL;
  542. /*
  543. * acpi_evaluate_object wrappers
  544. */
  545. static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
  546. {
  547. struct acpi_buffer output;
  548. union acpi_object out_obj;
  549. acpi_status status;
  550. output.length = sizeof(out_obj);
  551. output.pointer = &out_obj;
  552. status = acpi_evaluate_object(handle, name, NULL, &output);
  553. if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
  554. *result = out_obj.integer.value;
  555. return 0;
  556. }
  557. printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
  558. return -1;
  559. }
  560. static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
  561. int *result)
  562. {
  563. struct acpi_object_list params;
  564. union acpi_object in_obj;
  565. struct acpi_buffer output;
  566. union acpi_object out_obj;
  567. acpi_status status;
  568. params.count = 1;
  569. params.pointer = &in_obj;
  570. in_obj.type = ACPI_TYPE_INTEGER;
  571. in_obj.integer.value = value;
  572. output.length = sizeof(out_obj);
  573. output.pointer = &out_obj;
  574. status = acpi_evaluate_object(handle, name, &params, &output);
  575. if (status == AE_OK) {
  576. if (result != NULL) {
  577. if (out_obj.type != ACPI_TYPE_INTEGER) {
  578. printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
  579. "return type\n");
  580. return -1;
  581. }
  582. *result = out_obj.integer.value;
  583. }
  584. return 0;
  585. }
  586. printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
  587. return -1;
  588. }
  589. /*
  590. * sony_nc_values input/output validate functions
  591. */
  592. /* brightness_default_validate:
  593. *
  594. * manipulate input output values to keep consistency with the
  595. * backlight framework for which brightness values are 0-based.
  596. */
  597. static int brightness_default_validate(const int direction, const int value)
  598. {
  599. switch (direction) {
  600. case SNC_VALIDATE_OUT:
  601. return value - 1;
  602. case SNC_VALIDATE_IN:
  603. if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
  604. return value + 1;
  605. }
  606. return -EINVAL;
  607. }
  608. /* boolean_validate:
  609. *
  610. * on input validate boolean values 0/1, on output just pass the
  611. * received value.
  612. */
  613. static int boolean_validate(const int direction, const int value)
  614. {
  615. if (direction == SNC_VALIDATE_IN) {
  616. if (value != 0 && value != 1)
  617. return -EINVAL;
  618. }
  619. return value;
  620. }
  621. /*
  622. * Sysfs show/store common to all sony_nc_values
  623. */
  624. static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
  625. char *buffer)
  626. {
  627. int value;
  628. struct sony_nc_value *item =
  629. container_of(attr, struct sony_nc_value, devattr);
  630. if (!*item->acpiget)
  631. return -EIO;
  632. if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
  633. return -EIO;
  634. if (item->validate)
  635. value = item->validate(SNC_VALIDATE_OUT, value);
  636. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  637. }
  638. static ssize_t sony_nc_sysfs_store(struct device *dev,
  639. struct device_attribute *attr,
  640. const char *buffer, size_t count)
  641. {
  642. int value;
  643. struct sony_nc_value *item =
  644. container_of(attr, struct sony_nc_value, devattr);
  645. if (!item->acpiset)
  646. return -EIO;
  647. if (count > 31)
  648. return -EINVAL;
  649. value = simple_strtoul(buffer, NULL, 10);
  650. if (item->validate)
  651. value = item->validate(SNC_VALIDATE_IN, value);
  652. if (value < 0)
  653. return value;
  654. if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
  655. return -EIO;
  656. item->value = value;
  657. item->valid = 1;
  658. return count;
  659. }
  660. /*
  661. * Backlight device
  662. */
  663. static int sony_backlight_update_status(struct backlight_device *bd)
  664. {
  665. return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  666. bd->props.brightness + 1, NULL);
  667. }
  668. static int sony_backlight_get_brightness(struct backlight_device *bd)
  669. {
  670. int value;
  671. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
  672. return 0;
  673. /* brightness levels are 1-based, while backlight ones are 0-based */
  674. return value - 1;
  675. }
  676. static struct backlight_device *sony_backlight_device;
  677. static struct backlight_ops sony_backlight_ops = {
  678. .update_status = sony_backlight_update_status,
  679. .get_brightness = sony_backlight_get_brightness,
  680. };
  681. /*
  682. * New SNC-only Vaios event mapping to driver known keys
  683. */
  684. struct sony_nc_event {
  685. u8 data;
  686. u8 event;
  687. };
  688. static struct sony_nc_event *sony_nc_events;
  689. /* Vaio C* --maybe also FE*, N* and AR* ?-- special init sequence
  690. * for Fn keys
  691. */
  692. static int sony_nc_C_enable(const struct dmi_system_id *id)
  693. {
  694. int result = 0;
  695. printk(KERN_NOTICE DRV_PFX "detected %s\n", id->ident);
  696. sony_nc_events = id->driver_data;
  697. if (acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x4, &result) < 0
  698. || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x2, &result) < 0
  699. || acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x10, &result) < 0
  700. || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x0, &result) < 0
  701. || acpi_callsetfunc(sony_nc_acpi_handle, "SN03", 0x2, &result) < 0
  702. || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x101, &result) < 0) {
  703. printk(KERN_WARNING DRV_PFX "failed to initialize SNC, some "
  704. "functionalities may be missing\n");
  705. return 1;
  706. }
  707. return 0;
  708. }
  709. static struct sony_nc_event sony_C_events[] = {
  710. { 0x81, SONYPI_EVENT_FNKEY_F1 },
  711. { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
  712. { 0x85, SONYPI_EVENT_FNKEY_F5 },
  713. { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
  714. { 0x86, SONYPI_EVENT_FNKEY_F6 },
  715. { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
  716. { 0x87, SONYPI_EVENT_FNKEY_F7 },
  717. { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
  718. { 0x8A, SONYPI_EVENT_FNKEY_F10 },
  719. { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
  720. { 0x8C, SONYPI_EVENT_FNKEY_F12 },
  721. { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
  722. { 0, 0 },
  723. };
  724. /* SNC-only model map */
  725. static const struct dmi_system_id sony_nc_ids[] = {
  726. {
  727. .ident = "Sony Vaio FE Series",
  728. .callback = sony_nc_C_enable,
  729. .driver_data = sony_C_events,
  730. .matches = {
  731. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  732. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FE"),
  733. },
  734. },
  735. {
  736. .ident = "Sony Vaio FZ Series",
  737. .callback = sony_nc_C_enable,
  738. .driver_data = sony_C_events,
  739. .matches = {
  740. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  741. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FZ"),
  742. },
  743. },
  744. {
  745. .ident = "Sony Vaio C Series",
  746. .callback = sony_nc_C_enable,
  747. .driver_data = sony_C_events,
  748. .matches = {
  749. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  750. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-C"),
  751. },
  752. },
  753. { }
  754. };
  755. /*
  756. * ACPI callbacks
  757. */
  758. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  759. {
  760. struct sony_nc_event *evmap;
  761. u32 ev = event;
  762. int result;
  763. if (ev == 0x92) {
  764. /* read the key pressed from EC.GECR
  765. * A call to SN07 with 0x0202 will do it as well respecting
  766. * the current protocol on different OSes
  767. *
  768. * Note: the path for GECR may be
  769. * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
  770. * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
  771. *
  772. * TODO: we may want to do the same for the older GHKE -need
  773. * dmi list- so this snippet may become one more callback.
  774. */
  775. if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
  776. dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
  777. else
  778. ev = result & 0xFF;
  779. }
  780. if (sony_nc_events)
  781. for (evmap = sony_nc_events; evmap->event; evmap++) {
  782. if (evmap->data == ev) {
  783. ev = evmap->event;
  784. break;
  785. }
  786. }
  787. dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
  788. sony_laptop_report_input_event(ev);
  789. acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
  790. }
  791. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  792. void *context, void **return_value)
  793. {
  794. struct acpi_namespace_node *node;
  795. union acpi_operand_object *operand;
  796. node = (struct acpi_namespace_node *)handle;
  797. operand = (union acpi_operand_object *)node->object;
  798. printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  799. (u32) operand->method.param_count);
  800. return AE_OK;
  801. }
  802. /*
  803. * ACPI device
  804. */
  805. static int sony_nc_resume(struct acpi_device *device)
  806. {
  807. struct sony_nc_value *item;
  808. for (item = sony_nc_values; item->name; item++) {
  809. int ret;
  810. if (!item->valid)
  811. continue;
  812. ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
  813. item->value, NULL);
  814. if (ret < 0) {
  815. printk("%s: %d\n", __FUNCTION__, ret);
  816. break;
  817. }
  818. }
  819. /* set the last requested brightness level */
  820. if (sony_backlight_device &&
  821. !sony_backlight_update_status(sony_backlight_device))
  822. printk(KERN_WARNING DRV_PFX "unable to restore brightness level");
  823. /* re-initialize models with specific requirements */
  824. dmi_check_system(sony_nc_ids);
  825. return 0;
  826. }
  827. static int sony_nc_add(struct acpi_device *device)
  828. {
  829. acpi_status status;
  830. int result = 0;
  831. acpi_handle handle;
  832. struct sony_nc_value *item;
  833. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  834. SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  835. sony_nc_acpi_device = device;
  836. strcpy(acpi_device_class(device), "sony/hotkey");
  837. sony_nc_acpi_handle = device->handle;
  838. /* read device status */
  839. result = acpi_bus_get_status(device);
  840. /* bail IFF the above call was successful and the device is not present */
  841. if (!result && !device->status.present) {
  842. dprintk("Device not present\n");
  843. result = -ENODEV;
  844. goto outwalk;
  845. }
  846. if (debug) {
  847. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
  848. 1, sony_walk_callback, NULL, NULL);
  849. if (ACPI_FAILURE(status)) {
  850. printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
  851. result = -ENODEV;
  852. goto outwalk;
  853. }
  854. }
  855. /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
  856. * should be respected as we already checked for the device presence above */
  857. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
  858. dprintk("Invoking _INI\n");
  859. if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
  860. NULL, NULL)))
  861. dprintk("_INI Method failed\n");
  862. }
  863. /* setup input devices and helper fifo */
  864. result = sony_laptop_setup_input(device);
  865. if (result) {
  866. printk(KERN_ERR DRV_PFX
  867. "Unabe to create input devices.\n");
  868. goto outwalk;
  869. }
  870. status = acpi_install_notify_handler(sony_nc_acpi_handle,
  871. ACPI_DEVICE_NOTIFY,
  872. sony_acpi_notify, NULL);
  873. if (ACPI_FAILURE(status)) {
  874. printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
  875. result = -ENODEV;
  876. goto outinput;
  877. }
  878. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
  879. sony_backlight_device = backlight_device_register("sony", NULL,
  880. NULL,
  881. &sony_backlight_ops);
  882. if (IS_ERR(sony_backlight_device)) {
  883. printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
  884. sony_backlight_device = NULL;
  885. } else {
  886. sony_backlight_device->props.brightness =
  887. sony_backlight_get_brightness
  888. (sony_backlight_device);
  889. sony_backlight_device->props.max_brightness =
  890. SONY_MAX_BRIGHTNESS - 1;
  891. }
  892. }
  893. /* initialize models with specific requirements */
  894. dmi_check_system(sony_nc_ids);
  895. result = sony_pf_add();
  896. if (result)
  897. goto outbacklight;
  898. /* create sony_pf sysfs attributes related to the SNC device */
  899. for (item = sony_nc_values; item->name; ++item) {
  900. if (!debug && item->debug)
  901. continue;
  902. /* find the available acpiget as described in the DSDT */
  903. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  904. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  905. *item->acpiget,
  906. &handle))) {
  907. dprintk("Found %s getter: %s\n",
  908. item->name, *item->acpiget);
  909. item->devattr.attr.mode |= S_IRUGO;
  910. break;
  911. }
  912. }
  913. /* find the available acpiset as described in the DSDT */
  914. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  915. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  916. *item->acpiset,
  917. &handle))) {
  918. dprintk("Found %s setter: %s\n",
  919. item->name, *item->acpiset);
  920. item->devattr.attr.mode |= S_IWUSR;
  921. break;
  922. }
  923. }
  924. if (item->devattr.attr.mode != 0) {
  925. result =
  926. device_create_file(&sony_pf_device->dev,
  927. &item->devattr);
  928. if (result)
  929. goto out_sysfs;
  930. }
  931. }
  932. return 0;
  933. out_sysfs:
  934. for (item = sony_nc_values; item->name; ++item) {
  935. device_remove_file(&sony_pf_device->dev, &item->devattr);
  936. }
  937. sony_pf_remove();
  938. outbacklight:
  939. if (sony_backlight_device)
  940. backlight_device_unregister(sony_backlight_device);
  941. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  942. ACPI_DEVICE_NOTIFY,
  943. sony_acpi_notify);
  944. if (ACPI_FAILURE(status))
  945. printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
  946. outinput:
  947. sony_laptop_remove_input();
  948. outwalk:
  949. return result;
  950. }
  951. static int sony_nc_remove(struct acpi_device *device, int type)
  952. {
  953. acpi_status status;
  954. struct sony_nc_value *item;
  955. if (sony_backlight_device)
  956. backlight_device_unregister(sony_backlight_device);
  957. sony_nc_acpi_device = NULL;
  958. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  959. ACPI_DEVICE_NOTIFY,
  960. sony_acpi_notify);
  961. if (ACPI_FAILURE(status))
  962. printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
  963. for (item = sony_nc_values; item->name; ++item) {
  964. device_remove_file(&sony_pf_device->dev, &item->devattr);
  965. }
  966. sony_pf_remove();
  967. sony_laptop_remove_input();
  968. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  969. return 0;
  970. }
  971. static const struct acpi_device_id sony_device_ids[] = {
  972. {SONY_NC_HID, 0},
  973. {SONY_PIC_HID, 0},
  974. {"", 0},
  975. };
  976. MODULE_DEVICE_TABLE(acpi, sony_device_ids);
  977. static const struct acpi_device_id sony_nc_device_ids[] = {
  978. {SONY_NC_HID, 0},
  979. {"", 0},
  980. };
  981. static struct acpi_driver sony_nc_driver = {
  982. .name = SONY_NC_DRIVER_NAME,
  983. .class = SONY_NC_CLASS,
  984. .ids = sony_nc_device_ids,
  985. .owner = THIS_MODULE,
  986. .ops = {
  987. .add = sony_nc_add,
  988. .remove = sony_nc_remove,
  989. .resume = sony_nc_resume,
  990. },
  991. };
  992. /*********** SPIC (SNY6001) Device ***********/
  993. #define SONYPI_DEVICE_TYPE1 0x00000001
  994. #define SONYPI_DEVICE_TYPE2 0x00000002
  995. #define SONYPI_DEVICE_TYPE3 0x00000004
  996. #define SONYPI_TYPE1_OFFSET 0x04
  997. #define SONYPI_TYPE2_OFFSET 0x12
  998. #define SONYPI_TYPE3_OFFSET 0x12
  999. struct sony_pic_ioport {
  1000. struct acpi_resource_io io1;
  1001. struct acpi_resource_io io2;
  1002. struct list_head list;
  1003. };
  1004. struct sony_pic_irq {
  1005. struct acpi_resource_irq irq;
  1006. struct list_head list;
  1007. };
  1008. struct sony_pic_dev {
  1009. int model;
  1010. u16 evport_offset;
  1011. u8 camera_power;
  1012. u8 bluetooth_power;
  1013. u8 wwan_power;
  1014. struct acpi_device *acpi_dev;
  1015. struct sony_pic_irq *cur_irq;
  1016. struct sony_pic_ioport *cur_ioport;
  1017. struct list_head interrupts;
  1018. struct list_head ioports;
  1019. struct mutex lock;
  1020. };
  1021. static struct sony_pic_dev spic_dev = {
  1022. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  1023. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  1024. };
  1025. /* Event masks */
  1026. #define SONYPI_JOGGER_MASK 0x00000001
  1027. #define SONYPI_CAPTURE_MASK 0x00000002
  1028. #define SONYPI_FNKEY_MASK 0x00000004
  1029. #define SONYPI_BLUETOOTH_MASK 0x00000008
  1030. #define SONYPI_PKEY_MASK 0x00000010
  1031. #define SONYPI_BACK_MASK 0x00000020
  1032. #define SONYPI_HELP_MASK 0x00000040
  1033. #define SONYPI_LID_MASK 0x00000080
  1034. #define SONYPI_ZOOM_MASK 0x00000100
  1035. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  1036. #define SONYPI_MEYE_MASK 0x00000400
  1037. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  1038. #define SONYPI_BATTERY_MASK 0x00001000
  1039. #define SONYPI_WIRELESS_MASK 0x00002000
  1040. struct sonypi_event {
  1041. u8 data;
  1042. u8 event;
  1043. };
  1044. /* The set of possible button release events */
  1045. static struct sonypi_event sonypi_releaseev[] = {
  1046. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  1047. { 0, 0 }
  1048. };
  1049. /* The set of possible jogger events */
  1050. static struct sonypi_event sonypi_joggerev[] = {
  1051. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  1052. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  1053. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  1054. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  1055. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  1056. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  1057. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  1058. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  1059. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  1060. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  1061. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  1062. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  1063. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  1064. { 0, 0 }
  1065. };
  1066. /* The set of possible capture button events */
  1067. static struct sonypi_event sonypi_captureev[] = {
  1068. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  1069. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  1070. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  1071. { 0, 0 }
  1072. };
  1073. /* The set of possible fnkeys events */
  1074. static struct sonypi_event sonypi_fnkeyev[] = {
  1075. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  1076. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  1077. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  1078. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  1079. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  1080. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  1081. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  1082. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  1083. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  1084. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  1085. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  1086. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  1087. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  1088. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  1089. { 0x21, SONYPI_EVENT_FNKEY_1 },
  1090. { 0x22, SONYPI_EVENT_FNKEY_2 },
  1091. { 0x31, SONYPI_EVENT_FNKEY_D },
  1092. { 0x32, SONYPI_EVENT_FNKEY_E },
  1093. { 0x33, SONYPI_EVENT_FNKEY_F },
  1094. { 0x34, SONYPI_EVENT_FNKEY_S },
  1095. { 0x35, SONYPI_EVENT_FNKEY_B },
  1096. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  1097. { 0, 0 }
  1098. };
  1099. /* The set of possible program key events */
  1100. static struct sonypi_event sonypi_pkeyev[] = {
  1101. { 0x01, SONYPI_EVENT_PKEY_P1 },
  1102. { 0x02, SONYPI_EVENT_PKEY_P2 },
  1103. { 0x04, SONYPI_EVENT_PKEY_P3 },
  1104. { 0x5c, SONYPI_EVENT_PKEY_P1 },
  1105. { 0, 0 }
  1106. };
  1107. /* The set of possible bluetooth events */
  1108. static struct sonypi_event sonypi_blueev[] = {
  1109. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  1110. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  1111. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  1112. { 0, 0 }
  1113. };
  1114. /* The set of possible wireless events */
  1115. static struct sonypi_event sonypi_wlessev[] = {
  1116. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  1117. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  1118. { 0, 0 }
  1119. };
  1120. /* The set of possible back button events */
  1121. static struct sonypi_event sonypi_backev[] = {
  1122. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  1123. { 0, 0 }
  1124. };
  1125. /* The set of possible help button events */
  1126. static struct sonypi_event sonypi_helpev[] = {
  1127. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  1128. { 0, 0 }
  1129. };
  1130. /* The set of possible lid events */
  1131. static struct sonypi_event sonypi_lidev[] = {
  1132. { 0x51, SONYPI_EVENT_LID_CLOSED },
  1133. { 0x50, SONYPI_EVENT_LID_OPENED },
  1134. { 0, 0 }
  1135. };
  1136. /* The set of possible zoom events */
  1137. static struct sonypi_event sonypi_zoomev[] = {
  1138. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  1139. { 0, 0 }
  1140. };
  1141. /* The set of possible thumbphrase events */
  1142. static struct sonypi_event sonypi_thumbphraseev[] = {
  1143. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  1144. { 0, 0 }
  1145. };
  1146. /* The set of possible motioneye camera events */
  1147. static struct sonypi_event sonypi_meyeev[] = {
  1148. { 0x00, SONYPI_EVENT_MEYE_FACE },
  1149. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  1150. { 0, 0 }
  1151. };
  1152. /* The set of possible memorystick events */
  1153. static struct sonypi_event sonypi_memorystickev[] = {
  1154. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  1155. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  1156. { 0, 0 }
  1157. };
  1158. /* The set of possible battery events */
  1159. static struct sonypi_event sonypi_batteryev[] = {
  1160. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  1161. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  1162. { 0, 0 }
  1163. };
  1164. static struct sonypi_eventtypes {
  1165. int model;
  1166. u8 data;
  1167. unsigned long mask;
  1168. struct sonypi_event * events;
  1169. } sony_pic_eventtypes[] = {
  1170. { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
  1171. { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  1172. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
  1173. { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1174. { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1175. { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1176. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1177. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1178. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1179. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1180. { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
  1181. { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
  1182. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1183. { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1184. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1185. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1186. { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1187. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
  1188. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  1189. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1190. { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  1191. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1192. { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1193. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1194. { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
  1195. { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1196. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  1197. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1198. { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1199. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1200. { 0 }
  1201. };
  1202. static int sony_pic_detect_device_type(void)
  1203. {
  1204. struct pci_dev *pcidev;
  1205. int model = 0;
  1206. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1207. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  1208. model = SONYPI_DEVICE_TYPE1;
  1209. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1210. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  1211. model = SONYPI_DEVICE_TYPE3;
  1212. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1213. PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
  1214. model = SONYPI_DEVICE_TYPE3;
  1215. else
  1216. model = SONYPI_DEVICE_TYPE2;
  1217. if (pcidev)
  1218. pci_dev_put(pcidev);
  1219. printk(KERN_INFO DRV_PFX "detected Type%d model\n",
  1220. model == SONYPI_DEVICE_TYPE1 ? 1 :
  1221. model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  1222. return model;
  1223. }
  1224. #define ITERATIONS_LONG 10000
  1225. #define ITERATIONS_SHORT 10
  1226. #define wait_on_command(command, iterations) { \
  1227. unsigned int n = iterations; \
  1228. while (--n && (command)) \
  1229. udelay(1); \
  1230. if (!n) \
  1231. dprintk("command failed at %s : %s (line %d)\n", \
  1232. __FILE__, __FUNCTION__, __LINE__); \
  1233. }
  1234. static u8 sony_pic_call1(u8 dev)
  1235. {
  1236. u8 v1, v2;
  1237. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1238. ITERATIONS_LONG);
  1239. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1240. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  1241. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1242. dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
  1243. return v2;
  1244. }
  1245. static u8 sony_pic_call2(u8 dev, u8 fn)
  1246. {
  1247. u8 v1;
  1248. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1249. ITERATIONS_LONG);
  1250. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1251. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1252. ITERATIONS_LONG);
  1253. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1254. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1255. dprintk("sony_pic_call2: 0x%.4x\n", v1);
  1256. return v1;
  1257. }
  1258. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  1259. {
  1260. u8 v1;
  1261. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1262. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1263. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1264. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1265. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1266. outb(v, spic_dev.cur_ioport->io1.minimum);
  1267. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1268. dprintk("sony_pic_call3: 0x%.4x\n", v1);
  1269. return v1;
  1270. }
  1271. /* camera tests and poweron/poweroff */
  1272. #define SONYPI_CAMERA_PICTURE 5
  1273. #define SONYPI_CAMERA_CONTROL 0x10
  1274. #define SONYPI_CAMERA_BRIGHTNESS 0
  1275. #define SONYPI_CAMERA_CONTRAST 1
  1276. #define SONYPI_CAMERA_HUE 2
  1277. #define SONYPI_CAMERA_COLOR 3
  1278. #define SONYPI_CAMERA_SHARPNESS 4
  1279. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  1280. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  1281. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  1282. #define SONYPI_CAMERA_MUTE_MASK 0x40
  1283. /* the rest don't need a loop until not 0xff */
  1284. #define SONYPI_CAMERA_AGC 6
  1285. #define SONYPI_CAMERA_AGC_MASK 0x30
  1286. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  1287. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  1288. #define SONYPI_CAMERA_CONTROL 0x10
  1289. #define SONYPI_CAMERA_STATUS 7
  1290. #define SONYPI_CAMERA_STATUS_READY 0x2
  1291. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  1292. #define SONYPI_DIRECTION_BACKWARDS 0x4
  1293. #define SONYPI_CAMERA_REVISION 8
  1294. #define SONYPI_CAMERA_ROMVERSION 9
  1295. static int __sony_pic_camera_ready(void)
  1296. {
  1297. u8 v;
  1298. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  1299. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  1300. }
  1301. static int __sony_pic_camera_off(void)
  1302. {
  1303. if (!camera) {
  1304. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1305. return -ENODEV;
  1306. }
  1307. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  1308. SONYPI_CAMERA_MUTE_MASK),
  1309. ITERATIONS_SHORT);
  1310. if (spic_dev.camera_power) {
  1311. sony_pic_call2(0x91, 0);
  1312. spic_dev.camera_power = 0;
  1313. }
  1314. return 0;
  1315. }
  1316. static int __sony_pic_camera_on(void)
  1317. {
  1318. int i, j, x;
  1319. if (!camera) {
  1320. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1321. return -ENODEV;
  1322. }
  1323. if (spic_dev.camera_power)
  1324. return 0;
  1325. for (j = 5; j > 0; j--) {
  1326. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  1327. msleep(10);
  1328. sony_pic_call1(0x93);
  1329. for (i = 400; i > 0; i--) {
  1330. if (__sony_pic_camera_ready())
  1331. break;
  1332. msleep(10);
  1333. }
  1334. if (i)
  1335. break;
  1336. }
  1337. if (j == 0) {
  1338. printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
  1339. return -ENODEV;
  1340. }
  1341. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  1342. 0x5a),
  1343. ITERATIONS_SHORT);
  1344. spic_dev.camera_power = 1;
  1345. return 0;
  1346. }
  1347. /* External camera command (exported to the motion eye v4l driver) */
  1348. int sony_pic_camera_command(int command, u8 value)
  1349. {
  1350. if (!camera)
  1351. return -EIO;
  1352. mutex_lock(&spic_dev.lock);
  1353. switch (command) {
  1354. case SONY_PIC_COMMAND_SETCAMERA:
  1355. if (value)
  1356. __sony_pic_camera_on();
  1357. else
  1358. __sony_pic_camera_off();
  1359. break;
  1360. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  1361. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  1362. ITERATIONS_SHORT);
  1363. break;
  1364. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  1365. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  1366. ITERATIONS_SHORT);
  1367. break;
  1368. case SONY_PIC_COMMAND_SETCAMERAHUE:
  1369. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  1370. ITERATIONS_SHORT);
  1371. break;
  1372. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  1373. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  1374. ITERATIONS_SHORT);
  1375. break;
  1376. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  1377. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  1378. ITERATIONS_SHORT);
  1379. break;
  1380. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  1381. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  1382. ITERATIONS_SHORT);
  1383. break;
  1384. case SONY_PIC_COMMAND_SETCAMERAAGC:
  1385. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  1386. ITERATIONS_SHORT);
  1387. break;
  1388. default:
  1389. printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
  1390. command);
  1391. break;
  1392. }
  1393. mutex_unlock(&spic_dev.lock);
  1394. return 0;
  1395. }
  1396. EXPORT_SYMBOL(sony_pic_camera_command);
  1397. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  1398. static void sony_pic_set_wwanpower(u8 state)
  1399. {
  1400. state = !!state;
  1401. mutex_lock(&spic_dev.lock);
  1402. if (spic_dev.wwan_power == state) {
  1403. mutex_unlock(&spic_dev.lock);
  1404. return;
  1405. }
  1406. sony_pic_call2(0xB0, state);
  1407. spic_dev.wwan_power = state;
  1408. mutex_unlock(&spic_dev.lock);
  1409. }
  1410. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  1411. struct device_attribute *attr,
  1412. const char *buffer, size_t count)
  1413. {
  1414. unsigned long value;
  1415. if (count > 31)
  1416. return -EINVAL;
  1417. value = simple_strtoul(buffer, NULL, 10);
  1418. sony_pic_set_wwanpower(value);
  1419. return count;
  1420. }
  1421. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  1422. struct device_attribute *attr, char *buffer)
  1423. {
  1424. ssize_t count;
  1425. mutex_lock(&spic_dev.lock);
  1426. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  1427. mutex_unlock(&spic_dev.lock);
  1428. return count;
  1429. }
  1430. /* bluetooth subsystem power state */
  1431. static void __sony_pic_set_bluetoothpower(u8 state)
  1432. {
  1433. state = !!state;
  1434. if (spic_dev.bluetooth_power == state)
  1435. return;
  1436. sony_pic_call2(0x96, state);
  1437. sony_pic_call1(0x82);
  1438. spic_dev.bluetooth_power = state;
  1439. }
  1440. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  1441. struct device_attribute *attr,
  1442. const char *buffer, size_t count)
  1443. {
  1444. unsigned long value;
  1445. if (count > 31)
  1446. return -EINVAL;
  1447. value = simple_strtoul(buffer, NULL, 10);
  1448. mutex_lock(&spic_dev.lock);
  1449. __sony_pic_set_bluetoothpower(value);
  1450. mutex_unlock(&spic_dev.lock);
  1451. return count;
  1452. }
  1453. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  1454. struct device_attribute *attr, char *buffer)
  1455. {
  1456. ssize_t count = 0;
  1457. mutex_lock(&spic_dev.lock);
  1458. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  1459. mutex_unlock(&spic_dev.lock);
  1460. return count;
  1461. }
  1462. /* fan speed */
  1463. /* FAN0 information (reverse engineered from ACPI tables) */
  1464. #define SONY_PIC_FAN0_STATUS 0x93
  1465. static int sony_pic_set_fanspeed(unsigned long value)
  1466. {
  1467. return ec_write(SONY_PIC_FAN0_STATUS, value);
  1468. }
  1469. static int sony_pic_get_fanspeed(u8 *value)
  1470. {
  1471. return ec_read(SONY_PIC_FAN0_STATUS, value);
  1472. }
  1473. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  1474. struct device_attribute *attr,
  1475. const char *buffer, size_t count)
  1476. {
  1477. unsigned long value;
  1478. if (count > 31)
  1479. return -EINVAL;
  1480. value = simple_strtoul(buffer, NULL, 10);
  1481. if (sony_pic_set_fanspeed(value))
  1482. return -EIO;
  1483. return count;
  1484. }
  1485. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  1486. struct device_attribute *attr, char *buffer)
  1487. {
  1488. u8 value = 0;
  1489. if (sony_pic_get_fanspeed(&value))
  1490. return -EIO;
  1491. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  1492. }
  1493. #define SPIC_ATTR(_name, _mode) \
  1494. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  1495. _mode, sony_pic_## _name ##_show, \
  1496. sony_pic_## _name ##_store)
  1497. static SPIC_ATTR(bluetoothpower, 0644);
  1498. static SPIC_ATTR(wwanpower, 0644);
  1499. static SPIC_ATTR(fanspeed, 0644);
  1500. static struct attribute *spic_attributes[] = {
  1501. &spic_attr_bluetoothpower.attr,
  1502. &spic_attr_wwanpower.attr,
  1503. &spic_attr_fanspeed.attr,
  1504. NULL
  1505. };
  1506. static struct attribute_group spic_attribute_group = {
  1507. .attrs = spic_attributes
  1508. };
  1509. /******** SONYPI compatibility **********/
  1510. #ifdef CONFIG_SONYPI_COMPAT
  1511. /* battery / brightness / temperature addresses */
  1512. #define SONYPI_BAT_FLAGS 0x81
  1513. #define SONYPI_LCD_LIGHT 0x96
  1514. #define SONYPI_BAT1_PCTRM 0xa0
  1515. #define SONYPI_BAT1_LEFT 0xa2
  1516. #define SONYPI_BAT1_MAXRT 0xa4
  1517. #define SONYPI_BAT2_PCTRM 0xa8
  1518. #define SONYPI_BAT2_LEFT 0xaa
  1519. #define SONYPI_BAT2_MAXRT 0xac
  1520. #define SONYPI_BAT1_MAXTK 0xb0
  1521. #define SONYPI_BAT1_FULL 0xb2
  1522. #define SONYPI_BAT2_MAXTK 0xb8
  1523. #define SONYPI_BAT2_FULL 0xba
  1524. #define SONYPI_TEMP_STATUS 0xC1
  1525. struct sonypi_compat_s {
  1526. struct fasync_struct *fifo_async;
  1527. struct kfifo *fifo;
  1528. spinlock_t fifo_lock;
  1529. wait_queue_head_t fifo_proc_list;
  1530. atomic_t open_count;
  1531. };
  1532. static struct sonypi_compat_s sonypi_compat = {
  1533. .open_count = ATOMIC_INIT(0),
  1534. };
  1535. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  1536. {
  1537. int retval;
  1538. retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  1539. if (retval < 0)
  1540. return retval;
  1541. return 0;
  1542. }
  1543. static int sonypi_misc_release(struct inode *inode, struct file *file)
  1544. {
  1545. sonypi_misc_fasync(-1, file, 0);
  1546. atomic_dec(&sonypi_compat.open_count);
  1547. return 0;
  1548. }
  1549. static int sonypi_misc_open(struct inode *inode, struct file *file)
  1550. {
  1551. /* Flush input queue on first open */
  1552. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  1553. kfifo_reset(sonypi_compat.fifo);
  1554. return 0;
  1555. }
  1556. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  1557. size_t count, loff_t *pos)
  1558. {
  1559. ssize_t ret;
  1560. unsigned char c;
  1561. if ((kfifo_len(sonypi_compat.fifo) == 0) &&
  1562. (file->f_flags & O_NONBLOCK))
  1563. return -EAGAIN;
  1564. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  1565. kfifo_len(sonypi_compat.fifo) != 0);
  1566. if (ret)
  1567. return ret;
  1568. while (ret < count &&
  1569. (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
  1570. if (put_user(c, buf++))
  1571. return -EFAULT;
  1572. ret++;
  1573. }
  1574. if (ret > 0) {
  1575. struct inode *inode = file->f_path.dentry->d_inode;
  1576. inode->i_atime = current_fs_time(inode->i_sb);
  1577. }
  1578. return ret;
  1579. }
  1580. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  1581. {
  1582. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  1583. if (kfifo_len(sonypi_compat.fifo))
  1584. return POLLIN | POLLRDNORM;
  1585. return 0;
  1586. }
  1587. static int ec_read16(u8 addr, u16 *value)
  1588. {
  1589. u8 val_lb, val_hb;
  1590. if (ec_read(addr, &val_lb))
  1591. return -1;
  1592. if (ec_read(addr + 1, &val_hb))
  1593. return -1;
  1594. *value = val_lb | (val_hb << 8);
  1595. return 0;
  1596. }
  1597. static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
  1598. unsigned int cmd, unsigned long arg)
  1599. {
  1600. int ret = 0;
  1601. void __user *argp = (void __user *)arg;
  1602. u8 val8;
  1603. u16 val16;
  1604. int value;
  1605. mutex_lock(&spic_dev.lock);
  1606. switch (cmd) {
  1607. case SONYPI_IOCGBRT:
  1608. if (sony_backlight_device == NULL) {
  1609. ret = -EIO;
  1610. break;
  1611. }
  1612. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
  1613. ret = -EIO;
  1614. break;
  1615. }
  1616. val8 = ((value & 0xff) - 1) << 5;
  1617. if (copy_to_user(argp, &val8, sizeof(val8)))
  1618. ret = -EFAULT;
  1619. break;
  1620. case SONYPI_IOCSBRT:
  1621. if (sony_backlight_device == NULL) {
  1622. ret = -EIO;
  1623. break;
  1624. }
  1625. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1626. ret = -EFAULT;
  1627. break;
  1628. }
  1629. if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  1630. (val8 >> 5) + 1, NULL)) {
  1631. ret = -EIO;
  1632. break;
  1633. }
  1634. /* sync the backlight device status */
  1635. sony_backlight_device->props.brightness =
  1636. sony_backlight_get_brightness(sony_backlight_device);
  1637. break;
  1638. case SONYPI_IOCGBAT1CAP:
  1639. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  1640. ret = -EIO;
  1641. break;
  1642. }
  1643. if (copy_to_user(argp, &val16, sizeof(val16)))
  1644. ret = -EFAULT;
  1645. break;
  1646. case SONYPI_IOCGBAT1REM:
  1647. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  1648. ret = -EIO;
  1649. break;
  1650. }
  1651. if (copy_to_user(argp, &val16, sizeof(val16)))
  1652. ret = -EFAULT;
  1653. break;
  1654. case SONYPI_IOCGBAT2CAP:
  1655. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  1656. ret = -EIO;
  1657. break;
  1658. }
  1659. if (copy_to_user(argp, &val16, sizeof(val16)))
  1660. ret = -EFAULT;
  1661. break;
  1662. case SONYPI_IOCGBAT2REM:
  1663. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  1664. ret = -EIO;
  1665. break;
  1666. }
  1667. if (copy_to_user(argp, &val16, sizeof(val16)))
  1668. ret = -EFAULT;
  1669. break;
  1670. case SONYPI_IOCGBATFLAGS:
  1671. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  1672. ret = -EIO;
  1673. break;
  1674. }
  1675. val8 &= 0x07;
  1676. if (copy_to_user(argp, &val8, sizeof(val8)))
  1677. ret = -EFAULT;
  1678. break;
  1679. case SONYPI_IOCGBLUE:
  1680. val8 = spic_dev.bluetooth_power;
  1681. if (copy_to_user(argp, &val8, sizeof(val8)))
  1682. ret = -EFAULT;
  1683. break;
  1684. case SONYPI_IOCSBLUE:
  1685. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1686. ret = -EFAULT;
  1687. break;
  1688. }
  1689. __sony_pic_set_bluetoothpower(val8);
  1690. break;
  1691. /* FAN Controls */
  1692. case SONYPI_IOCGFAN:
  1693. if (sony_pic_get_fanspeed(&val8)) {
  1694. ret = -EIO;
  1695. break;
  1696. }
  1697. if (copy_to_user(argp, &val8, sizeof(val8)))
  1698. ret = -EFAULT;
  1699. break;
  1700. case SONYPI_IOCSFAN:
  1701. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1702. ret = -EFAULT;
  1703. break;
  1704. }
  1705. if (sony_pic_set_fanspeed(val8))
  1706. ret = -EIO;
  1707. break;
  1708. /* GET Temperature (useful under APM) */
  1709. case SONYPI_IOCGTEMP:
  1710. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  1711. ret = -EIO;
  1712. break;
  1713. }
  1714. if (copy_to_user(argp, &val8, sizeof(val8)))
  1715. ret = -EFAULT;
  1716. break;
  1717. default:
  1718. ret = -EINVAL;
  1719. }
  1720. mutex_unlock(&spic_dev.lock);
  1721. return ret;
  1722. }
  1723. static const struct file_operations sonypi_misc_fops = {
  1724. .owner = THIS_MODULE,
  1725. .read = sonypi_misc_read,
  1726. .poll = sonypi_misc_poll,
  1727. .open = sonypi_misc_open,
  1728. .release = sonypi_misc_release,
  1729. .fasync = sonypi_misc_fasync,
  1730. .ioctl = sonypi_misc_ioctl,
  1731. };
  1732. static struct miscdevice sonypi_misc_device = {
  1733. .minor = MISC_DYNAMIC_MINOR,
  1734. .name = "sonypi",
  1735. .fops = &sonypi_misc_fops,
  1736. };
  1737. static void sonypi_compat_report_event(u8 event)
  1738. {
  1739. kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
  1740. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  1741. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  1742. }
  1743. static int sonypi_compat_init(void)
  1744. {
  1745. int error;
  1746. spin_lock_init(&sonypi_compat.fifo_lock);
  1747. sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
  1748. &sonypi_compat.fifo_lock);
  1749. if (IS_ERR(sonypi_compat.fifo)) {
  1750. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  1751. return PTR_ERR(sonypi_compat.fifo);
  1752. }
  1753. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  1754. if (minor != -1)
  1755. sonypi_misc_device.minor = minor;
  1756. error = misc_register(&sonypi_misc_device);
  1757. if (error) {
  1758. printk(KERN_ERR DRV_PFX "misc_register failed\n");
  1759. goto err_free_kfifo;
  1760. }
  1761. if (minor == -1)
  1762. printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
  1763. sonypi_misc_device.minor);
  1764. return 0;
  1765. err_free_kfifo:
  1766. kfifo_free(sonypi_compat.fifo);
  1767. return error;
  1768. }
  1769. static void sonypi_compat_exit(void)
  1770. {
  1771. misc_deregister(&sonypi_misc_device);
  1772. kfifo_free(sonypi_compat.fifo);
  1773. }
  1774. #else
  1775. static int sonypi_compat_init(void) { return 0; }
  1776. static void sonypi_compat_exit(void) { }
  1777. static void sonypi_compat_report_event(u8 event) { }
  1778. #endif /* CONFIG_SONYPI_COMPAT */
  1779. /*
  1780. * ACPI callbacks
  1781. */
  1782. static acpi_status
  1783. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  1784. {
  1785. u32 i;
  1786. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  1787. switch (resource->type) {
  1788. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  1789. {
  1790. /* start IO enumeration */
  1791. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  1792. if (!ioport)
  1793. return AE_ERROR;
  1794. list_add(&ioport->list, &dev->ioports);
  1795. return AE_OK;
  1796. }
  1797. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  1798. /* end IO enumeration */
  1799. return AE_OK;
  1800. case ACPI_RESOURCE_TYPE_IRQ:
  1801. {
  1802. struct acpi_resource_irq *p = &resource->data.irq;
  1803. struct sony_pic_irq *interrupt = NULL;
  1804. if (!p || !p->interrupt_count) {
  1805. /*
  1806. * IRQ descriptors may have no IRQ# bits set,
  1807. * particularly those those w/ _STA disabled
  1808. */
  1809. dprintk("Blank IRQ resource\n");
  1810. return AE_OK;
  1811. }
  1812. for (i = 0; i < p->interrupt_count; i++) {
  1813. if (!p->interrupts[i]) {
  1814. printk(KERN_WARNING DRV_PFX
  1815. "Invalid IRQ %d\n",
  1816. p->interrupts[i]);
  1817. continue;
  1818. }
  1819. interrupt = kzalloc(sizeof(*interrupt),
  1820. GFP_KERNEL);
  1821. if (!interrupt)
  1822. return AE_ERROR;
  1823. list_add(&interrupt->list, &dev->interrupts);
  1824. interrupt->irq.triggering = p->triggering;
  1825. interrupt->irq.polarity = p->polarity;
  1826. interrupt->irq.sharable = p->sharable;
  1827. interrupt->irq.interrupt_count = 1;
  1828. interrupt->irq.interrupts[0] = p->interrupts[i];
  1829. }
  1830. return AE_OK;
  1831. }
  1832. case ACPI_RESOURCE_TYPE_IO:
  1833. {
  1834. struct acpi_resource_io *io = &resource->data.io;
  1835. struct sony_pic_ioport *ioport =
  1836. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  1837. if (!io) {
  1838. dprintk("Blank IO resource\n");
  1839. return AE_OK;
  1840. }
  1841. if (!ioport->io1.minimum) {
  1842. memcpy(&ioport->io1, io, sizeof(*io));
  1843. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  1844. ioport->io1.address_length);
  1845. }
  1846. else if (!ioport->io2.minimum) {
  1847. memcpy(&ioport->io2, io, sizeof(*io));
  1848. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  1849. ioport->io2.address_length);
  1850. }
  1851. else {
  1852. printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
  1853. return AE_ERROR;
  1854. }
  1855. return AE_OK;
  1856. }
  1857. default:
  1858. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  1859. resource->type);
  1860. case ACPI_RESOURCE_TYPE_END_TAG:
  1861. return AE_OK;
  1862. }
  1863. return AE_CTRL_TERMINATE;
  1864. }
  1865. static int sony_pic_possible_resources(struct acpi_device *device)
  1866. {
  1867. int result = 0;
  1868. acpi_status status = AE_OK;
  1869. if (!device)
  1870. return -EINVAL;
  1871. /* get device status */
  1872. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  1873. dprintk("Evaluating _STA\n");
  1874. result = acpi_bus_get_status(device);
  1875. if (result) {
  1876. printk(KERN_WARNING DRV_PFX "Unable to read status\n");
  1877. goto end;
  1878. }
  1879. if (!device->status.enabled)
  1880. dprintk("Device disabled\n");
  1881. else
  1882. dprintk("Device enabled\n");
  1883. /*
  1884. * Query and parse 'method'
  1885. */
  1886. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  1887. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  1888. sony_pic_read_possible_resource, &spic_dev);
  1889. if (ACPI_FAILURE(status)) {
  1890. printk(KERN_WARNING DRV_PFX
  1891. "Failure evaluating %s\n",
  1892. METHOD_NAME__PRS);
  1893. result = -ENODEV;
  1894. }
  1895. end:
  1896. return result;
  1897. }
  1898. /*
  1899. * Disable the spic device by calling its _DIS method
  1900. */
  1901. static int sony_pic_disable(struct acpi_device *device)
  1902. {
  1903. if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
  1904. "_DIS", NULL, NULL)))
  1905. return -ENXIO;
  1906. dprintk("Device disabled\n");
  1907. return 0;
  1908. }
  1909. /*
  1910. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  1911. *
  1912. * Call _SRS to set current resources
  1913. */
  1914. static int sony_pic_enable(struct acpi_device *device,
  1915. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  1916. {
  1917. acpi_status status;
  1918. int result = 0;
  1919. /* Type 1 resource layout is:
  1920. * IO
  1921. * IO
  1922. * IRQNoFlags
  1923. * End
  1924. *
  1925. * Type 2 and 3 resource layout is:
  1926. * IO
  1927. * IRQNoFlags
  1928. * End
  1929. */
  1930. struct {
  1931. struct acpi_resource res1;
  1932. struct acpi_resource res2;
  1933. struct acpi_resource res3;
  1934. struct acpi_resource res4;
  1935. } *resource;
  1936. struct acpi_buffer buffer = { 0, NULL };
  1937. if (!ioport || !irq)
  1938. return -EINVAL;
  1939. /* init acpi_buffer */
  1940. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  1941. if (!resource)
  1942. return -ENOMEM;
  1943. buffer.length = sizeof(*resource) + 1;
  1944. buffer.pointer = resource;
  1945. /* setup Type 1 resources */
  1946. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  1947. /* setup io resources */
  1948. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  1949. resource->res1.length = sizeof(struct acpi_resource);
  1950. memcpy(&resource->res1.data.io, &ioport->io1,
  1951. sizeof(struct acpi_resource_io));
  1952. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  1953. resource->res2.length = sizeof(struct acpi_resource);
  1954. memcpy(&resource->res2.data.io, &ioport->io2,
  1955. sizeof(struct acpi_resource_io));
  1956. /* setup irq resource */
  1957. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  1958. resource->res3.length = sizeof(struct acpi_resource);
  1959. memcpy(&resource->res3.data.irq, &irq->irq,
  1960. sizeof(struct acpi_resource_irq));
  1961. /* we requested a shared irq */
  1962. resource->res3.data.irq.sharable = ACPI_SHARED;
  1963. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  1964. }
  1965. /* setup Type 2/3 resources */
  1966. else {
  1967. /* setup io resource */
  1968. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  1969. resource->res1.length = sizeof(struct acpi_resource);
  1970. memcpy(&resource->res1.data.io, &ioport->io1,
  1971. sizeof(struct acpi_resource_io));
  1972. /* setup irq resource */
  1973. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  1974. resource->res2.length = sizeof(struct acpi_resource);
  1975. memcpy(&resource->res2.data.irq, &irq->irq,
  1976. sizeof(struct acpi_resource_irq));
  1977. /* we requested a shared irq */
  1978. resource->res2.data.irq.sharable = ACPI_SHARED;
  1979. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  1980. }
  1981. /* Attempt to set the resource */
  1982. dprintk("Evaluating _SRS\n");
  1983. status = acpi_set_current_resources(device->handle, &buffer);
  1984. /* check for total failure */
  1985. if (ACPI_FAILURE(status)) {
  1986. printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
  1987. result = -ENODEV;
  1988. goto end;
  1989. }
  1990. /* Necessary device initializations calls (from sonypi) */
  1991. sony_pic_call1(0x82);
  1992. sony_pic_call2(0x81, 0xff);
  1993. sony_pic_call1(compat ? 0x92 : 0x82);
  1994. end:
  1995. kfree(resource);
  1996. return result;
  1997. }
  1998. /*****************
  1999. *
  2000. * ISR: some event is available
  2001. *
  2002. *****************/
  2003. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  2004. {
  2005. int i, j;
  2006. u8 ev = 0;
  2007. u8 data_mask = 0;
  2008. u8 device_event = 0;
  2009. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  2010. ev = inb_p(dev->cur_ioport->io1.minimum);
  2011. if (dev->cur_ioport->io2.minimum)
  2012. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  2013. else
  2014. data_mask = inb_p(dev->cur_ioport->io1.minimum + dev->evport_offset);
  2015. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2016. ev, data_mask, dev->cur_ioport->io1.minimum, dev->evport_offset);
  2017. if (ev == 0x00 || ev == 0xff)
  2018. return IRQ_HANDLED;
  2019. for (i = 0; sony_pic_eventtypes[i].model; i++) {
  2020. if (spic_dev.model != sony_pic_eventtypes[i].model)
  2021. continue;
  2022. if ((data_mask & sony_pic_eventtypes[i].data) !=
  2023. sony_pic_eventtypes[i].data)
  2024. continue;
  2025. if (!(mask & sony_pic_eventtypes[i].mask))
  2026. continue;
  2027. for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
  2028. if (ev == sony_pic_eventtypes[i].events[j].data) {
  2029. device_event =
  2030. sony_pic_eventtypes[i].events[j].event;
  2031. goto found;
  2032. }
  2033. }
  2034. }
  2035. return IRQ_HANDLED;
  2036. found:
  2037. sony_laptop_report_input_event(device_event);
  2038. acpi_bus_generate_proc_event(spic_dev.acpi_dev, 1, device_event);
  2039. sonypi_compat_report_event(device_event);
  2040. return IRQ_HANDLED;
  2041. }
  2042. /*****************
  2043. *
  2044. * ACPI driver
  2045. *
  2046. *****************/
  2047. static int sony_pic_remove(struct acpi_device *device, int type)
  2048. {
  2049. struct sony_pic_ioport *io, *tmp_io;
  2050. struct sony_pic_irq *irq, *tmp_irq;
  2051. if (sony_pic_disable(device)) {
  2052. printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
  2053. return -ENXIO;
  2054. }
  2055. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2056. release_region(spic_dev.cur_ioport->io1.minimum,
  2057. spic_dev.cur_ioport->io1.address_length);
  2058. if (spic_dev.cur_ioport->io2.minimum)
  2059. release_region(spic_dev.cur_ioport->io2.minimum,
  2060. spic_dev.cur_ioport->io2.address_length);
  2061. sonypi_compat_exit();
  2062. sony_laptop_remove_input();
  2063. /* pf attrs */
  2064. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2065. sony_pf_remove();
  2066. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2067. list_del(&io->list);
  2068. kfree(io);
  2069. }
  2070. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2071. list_del(&irq->list);
  2072. kfree(irq);
  2073. }
  2074. spic_dev.cur_ioport = NULL;
  2075. spic_dev.cur_irq = NULL;
  2076. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  2077. return 0;
  2078. }
  2079. static int sony_pic_add(struct acpi_device *device)
  2080. {
  2081. int result;
  2082. struct sony_pic_ioport *io, *tmp_io;
  2083. struct sony_pic_irq *irq, *tmp_irq;
  2084. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  2085. SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  2086. spic_dev.acpi_dev = device;
  2087. strcpy(acpi_device_class(device), "sony/hotkey");
  2088. spic_dev.model = sony_pic_detect_device_type();
  2089. mutex_init(&spic_dev.lock);
  2090. /* model specific characteristics */
  2091. switch(spic_dev.model) {
  2092. case SONYPI_DEVICE_TYPE1:
  2093. spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
  2094. break;
  2095. case SONYPI_DEVICE_TYPE3:
  2096. spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
  2097. break;
  2098. case SONYPI_DEVICE_TYPE2:
  2099. default:
  2100. spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
  2101. break;
  2102. }
  2103. /* read _PRS resources */
  2104. result = sony_pic_possible_resources(device);
  2105. if (result) {
  2106. printk(KERN_ERR DRV_PFX
  2107. "Unabe to read possible resources.\n");
  2108. goto err_free_resources;
  2109. }
  2110. /* setup input devices and helper fifo */
  2111. result = sony_laptop_setup_input(device);
  2112. if (result) {
  2113. printk(KERN_ERR DRV_PFX
  2114. "Unabe to create input devices.\n");
  2115. goto err_free_resources;
  2116. }
  2117. if (sonypi_compat_init())
  2118. goto err_remove_input;
  2119. /* request io port */
  2120. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  2121. if (request_region(io->io1.minimum, io->io1.address_length,
  2122. "Sony Programable I/O Device")) {
  2123. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2124. io->io1.minimum, io->io1.maximum,
  2125. io->io1.address_length);
  2126. /* Type 1 have 2 ioports */
  2127. if (io->io2.minimum) {
  2128. if (request_region(io->io2.minimum,
  2129. io->io2.address_length,
  2130. "Sony Programable I/O Device")) {
  2131. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2132. io->io2.minimum, io->io2.maximum,
  2133. io->io2.address_length);
  2134. spic_dev.cur_ioport = io;
  2135. break;
  2136. }
  2137. else {
  2138. dprintk("Unable to get I/O port2: "
  2139. "0x%.4x (0x%.4x) + 0x%.2x\n",
  2140. io->io2.minimum, io->io2.maximum,
  2141. io->io2.address_length);
  2142. release_region(io->io1.minimum,
  2143. io->io1.address_length);
  2144. }
  2145. }
  2146. else {
  2147. spic_dev.cur_ioport = io;
  2148. break;
  2149. }
  2150. }
  2151. }
  2152. if (!spic_dev.cur_ioport) {
  2153. printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
  2154. result = -ENODEV;
  2155. goto err_remove_compat;
  2156. }
  2157. /* request IRQ */
  2158. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  2159. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  2160. IRQF_SHARED, "sony-laptop", &spic_dev)) {
  2161. dprintk("IRQ: %d - triggering: %d - "
  2162. "polarity: %d - shr: %d\n",
  2163. irq->irq.interrupts[0],
  2164. irq->irq.triggering,
  2165. irq->irq.polarity,
  2166. irq->irq.sharable);
  2167. spic_dev.cur_irq = irq;
  2168. break;
  2169. }
  2170. }
  2171. if (!spic_dev.cur_irq) {
  2172. printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
  2173. result = -ENODEV;
  2174. goto err_release_region;
  2175. }
  2176. /* set resource status _SRS */
  2177. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2178. if (result) {
  2179. printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
  2180. goto err_free_irq;
  2181. }
  2182. spic_dev.bluetooth_power = -1;
  2183. /* create device attributes */
  2184. result = sony_pf_add();
  2185. if (result)
  2186. goto err_disable_device;
  2187. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2188. if (result)
  2189. goto err_remove_pf;
  2190. return 0;
  2191. err_remove_pf:
  2192. sony_pf_remove();
  2193. err_disable_device:
  2194. sony_pic_disable(device);
  2195. err_free_irq:
  2196. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2197. err_release_region:
  2198. release_region(spic_dev.cur_ioport->io1.minimum,
  2199. spic_dev.cur_ioport->io1.address_length);
  2200. if (spic_dev.cur_ioport->io2.minimum)
  2201. release_region(spic_dev.cur_ioport->io2.minimum,
  2202. spic_dev.cur_ioport->io2.address_length);
  2203. err_remove_compat:
  2204. sonypi_compat_exit();
  2205. err_remove_input:
  2206. sony_laptop_remove_input();
  2207. err_free_resources:
  2208. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2209. list_del(&io->list);
  2210. kfree(io);
  2211. }
  2212. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2213. list_del(&irq->list);
  2214. kfree(irq);
  2215. }
  2216. spic_dev.cur_ioport = NULL;
  2217. spic_dev.cur_irq = NULL;
  2218. return result;
  2219. }
  2220. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  2221. {
  2222. if (sony_pic_disable(device))
  2223. return -ENXIO;
  2224. return 0;
  2225. }
  2226. static int sony_pic_resume(struct acpi_device *device)
  2227. {
  2228. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2229. return 0;
  2230. }
  2231. static const struct acpi_device_id sony_pic_device_ids[] = {
  2232. {SONY_PIC_HID, 0},
  2233. {"", 0},
  2234. };
  2235. static struct acpi_driver sony_pic_driver = {
  2236. .name = SONY_PIC_DRIVER_NAME,
  2237. .class = SONY_PIC_CLASS,
  2238. .ids = sony_pic_device_ids,
  2239. .owner = THIS_MODULE,
  2240. .ops = {
  2241. .add = sony_pic_add,
  2242. .remove = sony_pic_remove,
  2243. .suspend = sony_pic_suspend,
  2244. .resume = sony_pic_resume,
  2245. },
  2246. };
  2247. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  2248. {
  2249. .ident = "Sony Vaio",
  2250. .matches = {
  2251. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2252. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  2253. },
  2254. },
  2255. {
  2256. .ident = "Sony Vaio",
  2257. .matches = {
  2258. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2259. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  2260. },
  2261. },
  2262. { }
  2263. };
  2264. static int __init sony_laptop_init(void)
  2265. {
  2266. int result;
  2267. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  2268. result = acpi_bus_register_driver(&sony_pic_driver);
  2269. if (result) {
  2270. printk(KERN_ERR DRV_PFX
  2271. "Unable to register SPIC driver.");
  2272. goto out;
  2273. }
  2274. }
  2275. result = acpi_bus_register_driver(&sony_nc_driver);
  2276. if (result) {
  2277. printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
  2278. goto out_unregister_pic;
  2279. }
  2280. return 0;
  2281. out_unregister_pic:
  2282. if (!no_spic)
  2283. acpi_bus_unregister_driver(&sony_pic_driver);
  2284. out:
  2285. return result;
  2286. }
  2287. static void __exit sony_laptop_exit(void)
  2288. {
  2289. acpi_bus_unregister_driver(&sony_nc_driver);
  2290. if (!no_spic)
  2291. acpi_bus_unregister_driver(&sony_pic_driver);
  2292. }
  2293. module_init(sony_laptop_init);
  2294. module_exit(sony_laptop_exit);