UsbHidDriver.Mod 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. MODULE UsbHidDriver; (** AUTHOR "ottigerm"; PURPOSE "USB HID Parser"; *)
  2. (**
  3. * Bluebottle USB HID Driver
  4. *
  5. * This driver currently supports:
  6. *
  7. * Mouse: 2 axis, 1 mouse wheel, up to 32 buttons
  8. * Keyboard incl. consumer keys
  9. * Joystick x, y, z, rx, ry, rz and one slider axis, one coolie hat and arbitrary many buttons
  10. * Touchscreen x,y
  11. *
  12. * Usage:
  13. * UsbHidDriver.Install ~ load this driver
  14. * SystemTools.Free UsbHidDriver ~ unload this driver
  15. *
  16. * Remarks:
  17. *
  18. * References:
  19. * Device Class Definition for Human Interface Devices (HID), Version 1., 27.09.2006, www.usb.org
  20. *
  21. * History:
  22. * 21.04.2006 starting
  23. * 22.01.2007 Version 1.0
  24. *)
  25. IMPORT
  26. SYSTEM, Machine, KernelLog, Modules, Inputs, Usbdi, UsbHid,
  27. HidParser := UsbHidParser, UsbHidReport, UsagePage := UsbHidUP, UsbKeyboard,
  28. Joystick := Joysticks, Kernel;
  29. CONST
  30. Name = "UsbHid";
  31. Description = "USB HID driver";
  32. Debug = HidParser.Debug;
  33. Trace = HidParser.Trace;
  34. (* use for logging the reports *)
  35. ShowNoReport = 0; (* do not show any reports *)
  36. ShowVeryShortReport = 1; (* show short and non zero valued reports *)
  37. ShowShortReport = 2; (* show short reports; only the ids with their assigned value *)
  38. ShowFullReport = 3; (* show reports including description *)
  39. LoggingMode = ShowNoReport;
  40. MouseSpeed = 50;
  41. MouseWheelSpeed = 3;
  42. MouseAcceleration = 0;
  43. TYPE
  44. MouseState = POINTER TO RECORD
  45. (* mouse msg can hold up to 32 buttons *)
  46. buttons: ARRAY 32 OF UsbHidReport.UsageTuple;
  47. (* identifies the last available button *)
  48. buttonCount: LONGINT;
  49. buttonReport: UsbHidReport.HidReport;
  50. x: UsbHidReport.UsageTuple;
  51. y: UsbHidReport.UsageTuple;
  52. axisReport: UsbHidReport.HidReport;
  53. wheel: UsbHidReport.UsageTuple;
  54. wheelReport: UsbHidReport.HidReport;
  55. lastDx, lastDy: LONGINT;
  56. END;
  57. TouchscreenState = POINTER TO RECORD
  58. x,y: UsbHidReport.UsageTuple;
  59. minX, maxX, minY, maxY: LONGINT;
  60. tipSwitch, inRange, confidence: UsbHidReport.UsageTuple;
  61. prev: BOOLEAN;
  62. prevx, prevy: LONGINT;
  63. prevkeys: SET;
  64. prevTip: BOOLEAN;
  65. tipTime: LONGINT;
  66. END;
  67. (*Handling keyboard devices*)
  68. KeyboardState = OBJECT(UsbKeyboard.KeyboardBase);
  69. VAR
  70. modifierUsages: UsbHidReport.PtrToUsageTupleArr; (*first ref on buffer*)
  71. keycodeUsages: UsbHidReport.PtrToUsageTupleArr;
  72. pressed* : POINTER TO ARRAY OF UsbKeyboard.Key;
  73. tempPressed : POINTER TO ARRAY OF UsbKeyboard.Key;
  74. ledStateChanged : BOOLEAN;
  75. (*init settings*)
  76. PROCEDURE Init;
  77. VAR i : ADDRESS; k : ARRAY 32 OF CHAR;
  78. BEGIN
  79. (* Get *)
  80. Machine.GetConfig("Keyboard", k);
  81. i := -1;
  82. IF k # "" THEN i := TableFromFile(k); END;
  83. IF i = -1 THEN (* Fallback to default *) i := UsbKeyboard.TableUS(); END;
  84. SYSTEM.PUT(ADDRESSOF(keytable), i);
  85. (* Apply Numlock boot up state *)
  86. Machine.GetConfig("NumLock", k);
  87. IF k[0] = "1" THEN INCL(leds, UsbKeyboard.NumLock) END;
  88. keyDeadTime := UsbKeyboard.KeyDeadTime DIV 10;
  89. keyDeadTimeRepeat := UsbKeyboard.KeyDeadTimeRepeat DIV 10;
  90. NEW(ledBuffer, 1);
  91. END Init;
  92. (**
  93. * Sets the maximum possible amount of keys, the device is sending at one time
  94. * @param nofkeys: the number of keys maximumely sent by the device
  95. *)
  96. PROCEDURE SetMaxKeycodes(nofKeys: LONGINT);
  97. BEGIN
  98. ASSERT(pressed=NIL);
  99. ASSERT(tempPressed=NIL);
  100. NEW(pressed,nofKeys);
  101. NEW(tempPressed, nofKeys);
  102. END SetMaxKeycodes;
  103. (**
  104. * Handle Keyboard Report
  105. *)
  106. PROCEDURE HandleKeyboardEvent;
  107. VAR
  108. i, j : LONGINT;
  109. c : CHAR;
  110. flags : SET;
  111. found, kill : BOOLEAN;
  112. BEGIN
  113. (*KernelLog.String('handle key'); KernelLog.Ln;*)
  114. (* evaluate modifier keys *)
  115. msg.flags := {};
  116. IF (modifierUsages[0].usageValue=1) THEN INCL(msg.flags, Inputs.LeftCtrl) END;
  117. IF (modifierUsages[1].usageValue=1) THEN INCL(msg.flags, Inputs.LeftShift) END;
  118. IF (modifierUsages[2].usageValue=1) THEN INCL(msg.flags, Inputs.LeftAlt) END;
  119. IF (modifierUsages[3].usageValue=1) THEN INCL(msg.flags, Inputs.LeftMeta) END;
  120. IF (modifierUsages[4].usageValue=1) THEN INCL(msg.flags, Inputs.RightCtrl) END;
  121. IF (modifierUsages[5].usageValue=1) THEN INCL(msg.flags, Inputs.RightShift) END;
  122. IF (modifierUsages[6].usageValue=1) THEN INCL(msg.flags, Inputs.RightAlt) END;
  123. IF (modifierUsages[7].usageValue=1) THEN INCL(msg.flags, Inputs.RightMeta) END;
  124. flags := msg.flags;
  125. (* evaluate the six keycodes *)
  126. FOR i := 2 TO 7 DO
  127. c := SYSTEM.VAL(CHAR, keycodeUsages[i-2].usageValue);
  128. IF c # CHR(0) THEN (* buffer[i] contains key code *)
  129. (* check whether the key is pressed for the first time, is still being pressed or has been released *)
  130. FOR j := 0 TO 5 DO
  131. IF pressed[j].ch = c THEN (* key is still pressed *)
  132. found := TRUE;
  133. pressed[j].updated := TRUE;
  134. tempPressed[i-2].counter := pressed[j].counter + 1;
  135. tempPressed[i-2].ch := pressed[j].ch;
  136. tempPressed[i-2].keysym := pressed[j].keysym;
  137. tempPressed[i-2].updated := FALSE;
  138. tempPressed[i-2].repeat := pressed[j].repeat;
  139. IF pressed[j].repeat THEN
  140. IF (keyDeadTimeRepeat # 0) & (tempPressed[i-2].counter MOD keyDeadTimeRepeat # 0) THEN (* don't send key event *) kill := TRUE; END;
  141. ELSE
  142. IF tempPressed[i-2].counter MOD keyDeadTime # 0 THEN (* don't send key event *)
  143. kill := TRUE;
  144. ELSE
  145. tempPressed[i-2].repeat := TRUE;
  146. END;
  147. END;
  148. END;
  149. END;
  150. END;
  151. IF ~found THEN (* the key has not been pressed down before *)
  152. tempPressed[i-2].ch := c;
  153. tempPressed[i-2].repeat := FALSE;
  154. tempPressed[i-2].updated := FALSE;
  155. tempPressed[i-2].counter := 1;
  156. END;
  157. (* kill : Key is pressed but do not generate key event this time -> repeat rate ... *)
  158. IF (c # CHR(0)) & ~kill THEN
  159. HandleKey(c);
  160. tempPressed[i-2].keysym := msg.keysym; (* msg.keysym asigned by HandleKey() ... *)
  161. END;
  162. END; (* FOR LOOP *)
  163. (* update pressed array. generate keyboard.msg's for released keys *)
  164. FOR i := 0 TO 5 DO
  165. IF (pressed[i].updated = FALSE) & (pressed[i].ch # CHR(0)) THEN (* this key has been released *)
  166. msg.flags := {};
  167. INCL(msg.flags, Inputs.Release);
  168. msg.ch := pressed[i].ch;
  169. msg.keysym := pressed[i].keysym;
  170. dkHack := deadKey; (* value of deadKey should persist the key release event *)
  171. HandleKey(c);
  172. deadKey := dkHack;
  173. END;
  174. pressed[i].counter := tempPressed[i].counter;
  175. pressed[i].ch := tempPressed[i].ch;
  176. pressed[i].keysym := tempPressed[i].keysym;
  177. pressed[i].repeat := tempPressed[i].repeat;
  178. pressed[i].updated := FALSE;
  179. END;
  180. (* Generate events for modifiers *)
  181. HandleModifiers(flags);
  182. (* update status of the LEDs of the keyboad if necessary *)
  183. IF lastLeds # leds THEN (* LED status has changed *)
  184. ledBuffer[0] := SYSTEM.VAL(CHAR, leds); lastLeds := leds;
  185. ledStateChanged := TRUE;
  186. END;
  187. END HandleKeyboardEvent;
  188. END KeyboardState;
  189. (* When user presses button, the system has to store the pressed keys in this linked list *)
  190. ConsumerKey = POINTER TO RECORD
  191. key: LONGINT;
  192. usagePage: LONGINT;
  193. alive: BOOLEAN;
  194. next: ConsumerKey;
  195. END;
  196. (*handling consumer devices*)
  197. ConsumerState= OBJECT
  198. VAR
  199. (*where the consumer report is stored*)
  200. consumerReport : UsbHidReport.HidReport;
  201. first: ConsumerKey;
  202. (**
  203. * Checks, whether the usageID with the usagePage is still pressed by the user
  204. * if found, also sets the alive flag to TRUE, such that the clean up method will not destroy it next time
  205. * @param usageID: the usageID pressed
  206. * @param usagePage: normally 0, for detected consumer devices; 9, if consumer send consumer keys as buttons
  207. * @return TRUE, if found, FALSE otherwise
  208. *)
  209. PROCEDURE IsSet(usageID, usagePage: LONGINT): BOOLEAN;
  210. VAR cursor: ConsumerKey;
  211. BEGIN
  212. cursor := first;
  213. WHILE(cursor#NIL) DO
  214. IF ((cursor.key=usageID) & (cursor.usagePage=usagePage)) THEN
  215. cursor.alive := TRUE;
  216. RETURN TRUE;
  217. END;
  218. cursor := cursor.next;
  219. END;
  220. RETURN FALSE;
  221. END IsSet;
  222. (**
  223. * Adds the tuple.usageID and tuple.usagePage to the linked list
  224. * @param tuple: the tuple to add
  225. *)
  226. PROCEDURE AddKey(tuple:UsbHidReport.UsageTuple);
  227. VAR cursor: ConsumerKey;
  228. BEGIN
  229. IF first=NIL THEN
  230. NEW(first);
  231. ELSE
  232. NEW(cursor);
  233. cursor.next := first;
  234. first := cursor;
  235. END;
  236. first.key := tuple.usageID;
  237. first.usagePage := tuple.usagePage;
  238. first.alive := TRUE;
  239. END AddKey;
  240. (**
  241. * destroys all consumerKeys whose alive flag is not set
  242. *)
  243. PROCEDURE CleanUp;
  244. VAR cursor, previous: ConsumerKey;
  245. BEGIN
  246. cursor := first;
  247. WHILE(cursor#NIL) DO
  248. IF cursor.alive = FALSE THEN
  249. SendKeySym(cursor.key,cursor.usagePage,FALSE);
  250. IF(cursor = first) THEN
  251. first := first.next;
  252. ELSE
  253. previous.next := cursor.next;
  254. END;
  255. ELSE
  256. cursor.alive := FALSE;
  257. previous := cursor;
  258. END;
  259. cursor := cursor.next;
  260. END;
  261. END CleanUp;
  262. (**
  263. * Checks whether the keysym is valid, if yes, it generates a keymsg and sends the key to Inputs
  264. * @param usage: the id of the keysym
  265. * @param usagePage: 0: normally, 9: if key is sent as button
  266. *)
  267. PROCEDURE SendKeySym(usage, usagePage:LONGINT; pressed:BOOLEAN);
  268. VAR keyMsg : Inputs.KeyboardMsg;
  269. BEGIN
  270. IF Debug THEN
  271. KernelLog.String("Handling key");
  272. END;
  273. IF (usagePage=0) THEN
  274. CASE usage OF
  275. 0B5H: keyMsg.keysym := Inputs.KsScanNextTrack; (*KernelLog.String("KsScanNextTrack");*)
  276. |0B6H: keyMsg.keysym := Inputs.KsScanPreviousTrack; (*KernelLog.String("KsScanPreviousTrack");*)
  277. |0B7H: keyMsg.keysym := Inputs.KsStopOSC; (*KernelLog.String("KsStopOSC");*)
  278. |0CDH: keyMsg.keysym := Inputs.KsPlayPause; (*KernelLog.String("KsPlayPause");*)
  279. |0E2H: keyMsg.keysym := Inputs.KsMute; (*KernelLog.String("KsMute");*)
  280. |0E9H: keyMsg.keysym := Inputs.KsVolumeIncrement; (*KernelLog.String("KsVolumeIncrement");*)
  281. |0EAH: keyMsg.keysym := Inputs.KsVolumeDecrement; (*KernelLog.String("KsVolumeDecrement");*)
  282. |183H: keyMsg.keysym := Inputs.KsALConsumerControl; (*KernelLog.String("KsALConsumerControl");*)
  283. |18AH: keyMsg.keysym := Inputs.KsALEmailReader; (*KernelLog.String("KsALEmailReader");*)
  284. |221H: keyMsg.keysym := Inputs.KsACSearch; (*KernelLog.String("KsACSearch");*)
  285. |223H: keyMsg.keysym := Inputs.KsACHome; (*KernelLog.String("KsACHome");*)
  286. |224H: keyMsg.keysym := Inputs.KsACBack; (*KernelLog.String("KsACBack");*)
  287. |225H: keyMsg.keysym := Inputs.KsACForward; (*KernelLog.String("KsACForward");*)
  288. |22AH: keyMsg.keysym := Inputs.KsACBookmarks; (*KernelLog.String("KsACBookmarks");*)
  289. ELSE
  290. IF Trace THEN
  291. KernelLog.String("Key Sym "); KernelLog.Hex(usage,0 ); KernelLog.String("H not found"); KernelLog.Ln;
  292. END;
  293. END;
  294. ELSE
  295. (*special case: when usagePage Button is used*)
  296. IF (usagePage=9H) THEN
  297. keyMsg.keysym := Inputs.KsConsumerButtons+usage;
  298. END;
  299. END;
  300. IF (keyMsg.keysym#0) THEN
  301. IF ~pressed THEN
  302. keyMsg.flags:= {};
  303. keyMsg.keysym := Inputs.KsNil;
  304. INCL(keyMsg.flags, Inputs.Release);
  305. END;
  306. IF Debug THEN
  307. IF usagePage=0 THEN
  308. CASE usage OF
  309. 0B5H: KernelLog.String("KsScanNextTrack");
  310. |0B6H: KernelLog.String("KsScanPreviousTrack");
  311. |0B7H: KernelLog.String("KsStopOSC");
  312. |0CDH: KernelLog.String("KsPlayPause");
  313. |0E2H: KernelLog.String("KsMute");
  314. |0E9H: KernelLog.String("KsVolumeIncrement");
  315. |0EAH: KernelLog.String("KsVolumeDecrement");
  316. |183H: KernelLog.String("KsALConsumerControl");
  317. |18AH: KernelLog.String("KsALEmailReader");
  318. |192H: KernelLog.String("KsALCalculator");
  319. |221H: KernelLog.String("KsACSearch");
  320. |223H: KernelLog.String("KsACHome");
  321. |224H: KernelLog.String("KsACBack");
  322. |225H: KernelLog.String("KsACForward");
  323. |22AH: KernelLog.String("KsACBookmarks");
  324. ELSE
  325. KernelLog.String("Key Sym not found"); KernelLog.Ln;
  326. END;
  327. ELSE
  328. IF usagePage=9 THEN
  329. KernelLog.String("KsConsumerButtons(");KernelLog.Int(usage,0); KernelLog.String(")");
  330. END;
  331. END;
  332. IF pressed THEN
  333. KernelLog.String(" pressed");
  334. ELSE
  335. KernelLog.String(" released");
  336. END;
  337. KernelLog.Ln;
  338. END;
  339. Inputs.keyboard.Handle(keyMsg);
  340. END;
  341. END SendKeySym;
  342. END ConsumerState;
  343. (*handle joystick devices*)
  344. JoystickState = POINTER TO RECORD
  345. (*use the joystick as a mouse*)
  346. (*mouse msg can hold up to 32 buttons*)
  347. buttons: ARRAY 32 OF UsbHidReport.UsageTuple;
  348. (*identifies the last available button*)
  349. buttonCount: LONGINT;
  350. buttonReport: UsbHidReport.HidReport;
  351. x: UsbHidReport.UsageTuple;
  352. y: UsbHidReport.UsageTuple;
  353. z: UsbHidReport.UsageTuple;
  354. rx: UsbHidReport.UsageTuple;
  355. ry: UsbHidReport.UsageTuple;
  356. rz: UsbHidReport.UsageTuple;
  357. slider: UsbHidReport.UsageTuple;
  358. hatSwitch: UsbHidReport.UsageTuple;
  359. xReport: UsbHidReport.HidReport;
  360. yReport: UsbHidReport.HidReport;
  361. zReport: UsbHidReport.HidReport;
  362. rxReport: UsbHidReport.HidReport;
  363. ryReport: UsbHidReport.HidReport;
  364. rzReport: UsbHidReport.HidReport;
  365. sliderReport: UsbHidReport.HidReport;
  366. hatSwitchReport: UsbHidReport.HidReport;
  367. joystick: Joystick.Joystick;
  368. END;
  369. (*the hid driver*)
  370. HidDriver*= OBJECT (UsbHid.HidDriver);
  371. VAR
  372. (*itemParser is responsible for parsing the usb hid report descriptor*)
  373. itemParser : HidParser.ItemParser;
  374. endpoint : LONGINT;
  375. pipe : Usbdi.Pipe;
  376. (*where the report interrupt in report is stored*)
  377. reportBuffer : Usbdi.BufferPtr;
  378. reportManager : UsbHidReport.HidReportManager;
  379. hidReportItemQueue : UsbHidReport.ReportItemQueue;
  380. mouseState : MouseState;
  381. touchscreenState : TouchscreenState;
  382. keyboardState : KeyboardState;
  383. consumerState : ConsumerState;
  384. joystickState : JoystickState;
  385. useReportIDMechanism : BOOLEAN;
  386. recentStatus: Usbdi.Status;
  387. (*
  388. * This procedure is called by the USB system software after an instance of this object has been passed to it via the probe procedure.
  389. * Typically, the code here sets up the communication pipe(s) use by the driver using device.GetPipe(endpointnumber).
  390. *)
  391. PROCEDURE Connect() : BOOLEAN;
  392. VAR
  393. hidDescriptor : UsbHid.HidDescriptor;
  394. i : LONGINT;
  395. reportDescBuffer : Usbdi.BufferPtr;
  396. status : Usbdi.Status;
  397. canManage : BOOLEAN;
  398. BEGIN
  399. (*TestReader;*)
  400. (*parse the hid report descriptor*)
  401. NEW(itemParser);
  402. (*get interface descriptor*)
  403. hidDescriptor := GetHidDescriptor();
  404. IF (hidDescriptor = NIL) THEN
  405. RETURN FALSE;
  406. END;
  407. IF Debug THEN UsbHid.ShowHidDescriptor(hidDescriptor); END;
  408. NEW(reportDescBuffer, hidDescriptor.wDescriptorLength);
  409. IF ~GetDescriptor(hidDescriptor.bClassDescriptorType, 0, interface.bInterfaceNumber , hidDescriptor.wDescriptorLength, reportDescBuffer) THEN
  410. KernelLog.String(" Could not get reportDescriptor"); KernelLog.Ln;
  411. RETURN FALSE;
  412. ELSE
  413. IF Debug THEN
  414. (*print all all bytes of the reportDescBuffer*)
  415. LayoutBuffer(reportDescBuffer, hidDescriptor.wDescriptorLength);
  416. END;
  417. END;
  418. IF(~itemParser.ParseReportDescriptor(hidDescriptor, reportDescBuffer)) THEN
  419. IF Debug THEN KernelLog.String(" Could not parse Report Descriptor correctly"); KernelLog.Ln; END;
  420. END;
  421. IF Trace THEN
  422. (*there are cases, where the report descriptor is not set correctly, but it can be used with this errors.*)
  423. itemParser.errorList.PrintAll;
  424. END;
  425. (*get reportManager and hidReportItemQueue*)
  426. reportManager := itemParser.GetReportManager();
  427. hidReportItemQueue := reportManager.GetReportItemQueue();
  428. LOOP
  429. IF i >= LEN(interface.endpoints) THEN EXIT; END;
  430. IF (interface.endpoints[i].type = Usbdi.InterruptIn) THEN
  431. endpoint := SHORT(SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, interface.endpoints[0].bEndpointAddress) * {0,1,2,3,7}));
  432. EXIT;
  433. END;
  434. INC(i);
  435. END;
  436. IF endpoint = 0 THEN
  437. IF Debug THEN KernelLog.String("UsbMouse: No interrupt IN endpoint found."); KernelLog.Ln; END;
  438. RETURN FALSE;
  439. END;
  440. pipe := device.GetPipe(endpoint);
  441. IF pipe = NIL THEN RETURN FALSE END;
  442. IF InitializeTouchscreenDriver()=TRUE THEN
  443. canManage := TRUE;
  444. ELSIF InitializeMouseDriver() = TRUE THEN
  445. canManage := TRUE;
  446. END;
  447. IF InitializeKeyboardDriver()=TRUE THEN
  448. canManage := TRUE;
  449. END;
  450. IF InitializeConsumerDriver() = TRUE THEN
  451. canManage := TRUE;
  452. END;
  453. IF InitializeJoystickDriver()=TRUE THEN
  454. canManage := TRUE;
  455. END;
  456. IF (canManage) THEN
  457. useReportIDMechanism := reportManager.UsesReportIDMechanism();
  458. NEW(reportBuffer, pipe.maxPacketSize);
  459. pipe.SetTimeout(0);
  460. pipe.SetCompletionHandler(HandleHidEvent);
  461. status := pipe.Transfer(pipe.maxPacketSize, 0, reportBuffer);
  462. recentStatus := status;
  463. IF Debug THEN
  464. KernelLog.String("UsbHidDriver.HidDriver.Connect: Connect successfully finished"); KernelLog.Ln;
  465. END;
  466. (* ELSE NOT SUPPORTED YET, because the whole driver is removed but not only the interface
  467. RETURN FALSE; *)
  468. END;
  469. RETURN TRUE;
  470. END Connect;
  471. (*called when detaching a usb hid device*)
  472. PROCEDURE Disconnect;
  473. BEGIN
  474. itemParser.Disconnect();
  475. itemParser:=NIL;
  476. (*joystick*)
  477. IF (joystickState#NIL) THEN
  478. IF joystickState.joystick#NIL THEN
  479. Joystick.registry.Remove(joystickState.joystick);
  480. END;
  481. END;
  482. IF Debug OR Trace THEN KernelLog.String("USB HID Device disconnected."); KernelLog.Ln; END;
  483. END Disconnect;
  484. (*is always called, when new report arrived*)
  485. PROCEDURE HandleHidEvent(status : Usbdi.Status; actLen : LONGINT);
  486. VAR
  487. ri : UsbHidReport.ReportItem;
  488. i, bitIndex : LONGINT;
  489. reportID : LONGINT;
  490. res : BOOLEAN;
  491. usageTuple : UsbHidReport.UsageTuple;
  492. (*
  493. * update the reportManager with the newest values
  494. *)
  495. PROCEDURE HandleReportItem;
  496. BEGIN
  497. FOR i:=0 TO (ri.reportItemCount-1) DO
  498. IF(ri.values=NIL) THEN
  499. (*there are no values to read, because the reportItem describes a constant field*)
  500. ELSE
  501. IF Debug THEN
  502. KernelLog.String("HandleHidEvent: Reading..."); KernelLog.Ln;
  503. END;
  504. usageTuple := ri.values[i];
  505. usageTuple.usageValue := ReadBits(bitIndex,ri.reportItemSize);
  506. IF (LoggingMode=ShowShortReport) OR (LoggingMode=ShowVeryShortReport) THEN
  507. IF((LoggingMode=ShowShortReport)OR(ri.values[i].usageValue#0)) THEN
  508. KernelLog.String("usageValue for usageID ");
  509. KernelLog.Int(ri.values[i].usageID,0);
  510. KernelLog.String(" is: ");
  511. KernelLog.Int(ri.values[i].usageValue,0);
  512. KernelLog.Ln;
  513. END;
  514. END;
  515. END;
  516. bitIndex := bitIndex + ri.reportItemSize;
  517. END;
  518. END HandleReportItem;
  519. BEGIN
  520. (*start reportParsing*)
  521. IF Debug THEN
  522. IF((hidReportItemQueue=NIL) OR (reportManager=NIL)) THEN
  523. KernelLog.String("UsbHidDriver:HidDriver.HandleHidEvent: Internal Error,hidReportItemQueue or reportManager not found"); KernelLog.Ln;
  524. END;
  525. LayoutBuffer(reportBuffer,actLen);
  526. END;
  527. (*fill up report buffer with new values*)
  528. ri := hidReportItemQueue.first;
  529. IF(ri=NIL) THEN
  530. KernelLog.String("ri=NIL"); KernelLog.Ln;
  531. RETURN;
  532. END;
  533. (*index in the reportBuffer*)
  534. bitIndex := 0;
  535. IF (useReportIDMechanism) THEN
  536. reportID := ReadBits(0, 8);
  537. bitIndex := bitIndex + 8;
  538. WHILE(ri#NIL) DO
  539. IF(ri.reportID=reportID) THEN
  540. HandleReportItem;
  541. END;
  542. ri := ri.next;
  543. END;
  544. ELSE
  545. WHILE(ri#NIL) DO
  546. HandleReportItem;
  547. ri := ri.next;
  548. END;
  549. END;
  550. IF LoggingMode=ShowFullReport THEN reportManager.PrintReportState END;
  551. IF ( (status = Usbdi.Ok) OR (status=Usbdi.ShortPacket)) THEN
  552. recentStatus := status;
  553. IF(mouseState#NIL) THEN
  554. IF Debug THEN KernelLog.String("handle mouse driver"); KernelLog.Ln; END;
  555. HandleMouseDriver;
  556. END;
  557. IF(keyboardState#NIL) THEN
  558. IF Debug THEN KernelLog.String("handle keyboard driver"); KernelLog.Ln; END;
  559. HandleKeyboardDriver;
  560. (* update status of the LEDs of the keyboad if necessary *)
  561. IF keyboardState.lastLeds # keyboardState.leds THEN (* LED status has changed *)
  562. keyboardState.ledBuffer[0] := SYSTEM.VAL(CHAR, keyboardState.leds); keyboardState.lastLeds := keyboardState.leds;
  563. res := SetReport(UsbHid.ReportOutput, 0, keyboardState.ledBuffer, 1); (* ignore res *)
  564. END;
  565. END;
  566. IF(consumerState#NIL) THEN
  567. IF Debug THEN KernelLog.String("handle consumer driver"); KernelLog.Ln; END;
  568. HandleConsumerDriver;
  569. END;
  570. IF(joystickState#NIL) THEN
  571. IF Debug THEN KernelLog.String("handle custom driver"); KernelLog.Ln; END;
  572. HandleJoystickDriver;
  573. END;
  574. IF touchscreenState #NIL THEN
  575. IF Debug THEN KernelLog.String("handle touchscreen driver"); KernelLog.Ln; END;
  576. HandleTouchscreenDriver
  577. END;
  578. (*get new message from hid device*)
  579. status := pipe.Transfer(pipe.maxPacketSize, 0, reportBuffer);
  580. ELSE
  581. IF Debug THEN
  582. KernelLog.String("UsbHidDriver: "); KernelLog.String(name); KernelLog.String("("); KernelLog.String(desc); KernelLog.String(")");
  583. KernelLog.String(" has been disabled."); KernelLog.Ln;
  584. END;
  585. IF (status = Usbdi.Stalled) THEN
  586. IF pipe.ClearHalt() THEN
  587. IF Debug THEN KernelLog.String("UsbHidDriver: Stall on Interrupt Pipe cleared."); KernelLog.Ln; END;
  588. status := pipe.Transfer(pipe.maxPacketSize, 0, reportBuffer); (* ignore status *)
  589. ELSE
  590. IF Debug THEN KernelLog.String("UsbHidDriver: Couldn't clear stall on interrupt pipe. Abort."); KernelLog.Ln; END;
  591. device.FreePipe(pipe);
  592. END;
  593. END;
  594. recentStatus := status;
  595. END;
  596. END HandleHidEvent;
  597. PROCEDURE IsTouchscreen*(): BOOLEAN;
  598. BEGIN
  599. RETURN touchscreenState # NIL
  600. END IsTouchscreen;
  601. PROCEDURE GetPipeStatus*(): Usbdi.Status;
  602. VAR len: LONGINT;
  603. BEGIN
  604. IF pipe # NIL THEN RETURN pipe.GetStatus(len)
  605. ELSE RETURN Usbdi.Error
  606. END;
  607. END GetPipeStatus;
  608. PROCEDURE GetRecentStatus*(): Usbdi.Status;
  609. BEGIN
  610. RETURN recentStatus;
  611. END GetRecentStatus;
  612. (**
  613. * Is called by handleHidEvent when mouse device is found
  614. *)
  615. PROCEDURE HandleMouseDriver;
  616. VAR
  617. mm : Inputs.MouseMsg;
  618. dx, dy,i : LONGINT;
  619. accelX, accelY : REAL;
  620. BEGIN
  621. dx := TwosComplement(mouseState.x.usageValue, mouseState.axisReport.reportSize);
  622. dy := TwosComplement(mouseState.y.usageValue, mouseState.axisReport.reportSize);
  623. IF Debug THEN KernelLog.String("x and y are: ");KernelLog.Int(dx,0); KernelLog.String(" and ");KernelLog.Int(dy,0); KernelLog.Ln; END;
  624. accelX := 1.0 + ABS(dx - mouseState.lastDx) / 128 * MouseAcceleration;
  625. accelY := 1.0 + ABS(dy - mouseState.lastDy) / 128 * MouseAcceleration;
  626. mouseState.lastDx := dx;
  627. mouseState.lastDy := dy;
  628. (*KernelLog.String("X: "); KernelLog.Int(dx,0); KernelLog.String(" Y:"); KernelLog.Int(dy,0); KernelLog.Ln;*)
  629. mm.dx := ENTIER(MouseSpeed / 50.0 * dx * accelX);
  630. mm.dy := ENTIER(MouseSpeed / 50.0 * dy * accelY);
  631. IF (mouseState.wheel#NIL) THEN
  632. mm.dz := - TwosComplement(mouseState.wheel.usageValue, mouseState.wheelReport.reportSize);
  633. IF mm.dz < 0 THEN mm.dz := - MouseWheelSpeed;
  634. ELSIF mm.dz>0 THEN mm.dz := + MouseWheelSpeed;
  635. END;
  636. END;
  637. IF (mouseState.buttons[0].usageValue#0) THEN mm.keys := mm.keys + {0}; END;
  638. IF (mouseState.buttons[1].usageValue#0) THEN mm.keys := mm.keys + {2}; END;
  639. IF (mouseState.buttons[2].usageValue#0) THEN mm.keys := mm.keys + {1}; END;
  640. FOR i:=3 TO 31 DO
  641. IF (mouseState.buttons[i]#NIL) THEN
  642. IF (mouseState.buttons[i].usageValue#0) THEN mm.keys := mm.keys + {i}; END;
  643. END;
  644. END;
  645. Inputs.mouse.Handle(mm);
  646. END HandleMouseDriver;
  647. (**
  648. * Is called by handleHidEvent when mouse device is found
  649. *)
  650. PROCEDURE HandleTouchscreenDriver;
  651. VAR
  652. mm : Inputs.AbsMouseMsg;
  653. x,y: LONGINT;
  654. displayWidth, displayHeight: LONGINT;
  655. BEGIN
  656. getDisplayDimensions(displayWidth, displayHeight);
  657. x := (touchscreenState.x.usageValue-touchscreenState.minX) * displayWidth DIV (touchscreenState.maxX-touchscreenState.minX +1);
  658. y := (touchscreenState.y.usageValue-touchscreenState.minY) * displayHeight DIV (touchscreenState.maxY -touchscreenState.minY + 1);
  659. (*
  660. TRACE(touchscreenState.inRange.usageValue);
  661. IF touchscreenState.confidence # NIL THEN
  662. TRACE(touchscreenState.confidence.usageValue);
  663. END;
  664. *)
  665. IF (touchscreenState.tipSwitch.usageValue#0) THEN
  666. IF (touchConfidenceDelay#0) & ~touchscreenState.prevTip THEN
  667. touchscreenState.tipTime := Kernel.GetTicks();
  668. touchscreenState.prevTip := TRUE
  669. ELSIF (touchConfidenceDelay =0) OR (Kernel.GetTicks()-touchscreenState.tipTime > touchConfidenceDelay) THEN
  670. mm.keys := mm.keys + {0};
  671. END
  672. ELSE
  673. touchscreenState.prevTip := FALSE;
  674. END;
  675. mm.x := x; mm.y := y;
  676. IF ~(0 IN mm.keys) THEN (* up event *)
  677. touchscreenState.prev := FALSE;
  678. (*
  679. There are touchscreen devices that report a (0,0) coordinate with up-events.
  680. This does not work well together with an external mouse.
  681. We keep the coordinate for the time being -- with the side effect that there may be a pointer-over status after a touchscreen click
  682. *)
  683. IF 0 IN touchscreenState.prevkeys THEN
  684. mm.x := touchscreenState.prevx;
  685. mm.y := touchscreenState.prevy;
  686. touchscreenState.prevkeys := mm.keys;
  687. Inputs.mouse.Handle(mm);
  688. END;
  689. ELSIF (0 IN mm.keys) THEN (* down event or pointer move *)
  690. IF ~touchscreenState.prev THEN
  691. (* artificial initial up event in order to set mouse coordinates like with a "real" mouse before sending the down event *)
  692. touchscreenState.prev := TRUE;
  693. EXCL(mm.keys, 0);
  694. Inputs.mouse.Handle(mm);
  695. END;
  696. (* down event or pointer event *)
  697. touchscreenState.prevx := x;
  698. touchscreenState.prevy := y;
  699. touchscreenState.prevkeys := mm.keys;
  700. Inputs.mouse.Handle(mm);
  701. END;
  702. END HandleTouchscreenDriver;
  703. (**
  704. * Is called by handleHidEvent when keyboard device is found
  705. *)
  706. PROCEDURE HandleKeyboardDriver;
  707. VAR res : BOOLEAN;
  708. BEGIN
  709. keyboardState.HandleKeyboardEvent();
  710. IF keyboardState.ledStateChanged THEN
  711. res := SetReport(UsbHid.ReportOutput, 0, keyboardState.ledBuffer, 1); (* ignore res *)
  712. keyboardState.ledStateChanged := FALSE;
  713. END;
  714. END HandleKeyboardDriver;
  715. (**
  716. * Is called by handleHidEvent when consumer device is found
  717. *)
  718. PROCEDURE HandleConsumerDriver;
  719. VAR
  720. temp: UsbHidReport.HidReport;
  721. usageTuple: UsbHidReport.UsageTuple;
  722. i: LONGINT;
  723. dictUsage: LONGINT;
  724. BEGIN
  725. temp := consumerState.consumerReport;
  726. WHILE(temp#NIL) DO
  727. IF temp.usages # NIL THEN
  728. FOR i:=0 TO temp.reportCount-1 DO
  729. IF(temp.usages[i].usageValue#0)THEN
  730. IF HidParser.IDMainIsVariable IN SYSTEM.VAL(SET,temp.mainState) THEN
  731. IF Debug THEN
  732. KernelLog.Int(temp.usages[i].usageID,0);
  733. KernelLog.String(" ("); UsagePage.PrintUsagePage(UsagePage.ConsumerPage, temp.usages[i].usageID);
  734. END;
  735. IF(consumerState.IsSet(temp.usages[i].usageID,temp.usages[i].usagePage)=FALSE) THEN
  736. consumerState.AddKey(temp.usages[i]);
  737. consumerState.SendKeySym(temp.usages[i].usageID, temp.usages[i].usagePage, TRUE);
  738. END;
  739. ELSE
  740. (*the data is sent in an array*)
  741. IF UsbHidReport.UseUsageDictionaryExt THEN
  742. IF Debug THEN
  743. KernelLog.String("-> usage dictionary index: "); KernelLog.Int(temp.usages[i].usageValue,0); KernelLog.String("; ");
  744. END;
  745. usageTuple := reportManager.GetDictKey(temp.usages[i].usageValue-temp.logicalMinimum, temp.supportedUsages);
  746. (*dictUsage := usageTuple.usageID;
  747. KernelLog.String("usageID is "); KernelLog.Int(usageTuple.usagePage,0); KernelLog.String(" "); KernelLog.Hex(dictUsage,0); KernelLog.Ln;*)
  748. IF (consumerState.IsSet(usageTuple.usageID,usageTuple.usagePage)=FALSE) THEN
  749. consumerState.AddKey(usageTuple);
  750. consumerState.SendKeySym(usageTuple.usageID, usageTuple.usagePage, TRUE);
  751. END;
  752. IF Debug THEN
  753. KernelLog.Int(dictUsage,0);
  754. KernelLog.String(" (");
  755. IF usageTuple.usagePage # 0 THEN
  756. UsagePage.PrintUsagePage(usageTuple.usagePage, dictUsage);
  757. ELSE
  758. UsagePage.PrintUsagePage(UsagePage.ConsumerPage, dictUsage);
  759. END;
  760. END;
  761. ELSE
  762. IF Debug THEN
  763. KernelLog.Int(temp.usages[i].usageValue,0);
  764. KernelLog.String(" ("); UsagePage.PrintUsagePage(UsagePage.ConsumerPage, temp.usages[i].usageValue);
  765. END;
  766. IF (consumerState.IsSet(temp.usages[i].usageValue,temp.usages[i].usagePage)=FALSE) THEN
  767. consumerState.AddKey(temp.usages[i]);
  768. consumerState.SendKeySym(temp.usages[i].usageValue,temp.usages[i].usagePage, TRUE);
  769. END;
  770. END;
  771. END;
  772. IF Debug THEN KernelLog.String(") pressed."); KernelLog.Ln; END;
  773. END;
  774. END;
  775. END;
  776. temp := temp.next;
  777. END;
  778. consumerState.CleanUp;
  779. END HandleConsumerDriver;
  780. (**
  781. * Is called by handleHidEvent when joystick device is found
  782. *)
  783. PROCEDURE HandleJoystickDriver;
  784. VAR
  785. msg : Joystick.JoystickDataMessage;
  786. i: LONGINT;
  787. BEGIN
  788. FOR i:=0 TO joystickState.buttonCount-1 DO
  789. IF (joystickState.buttons[i].usageValue#0) THEN
  790. msg.buttons := msg.buttons + {joystickState.buttons[i].usageID-1};
  791. END;
  792. END;
  793. IF joystickState.x # NIL THEN
  794. IF joystickState.xReport.logicalMinimum<0 THEN
  795. msg.axis[Joystick.AxisX] := TwosComplement(joystickState.x.usageValue,joystickState.xReport.reportSize);
  796. ELSE
  797. msg.axis[Joystick.AxisX] := joystickState.x.usageValue;
  798. END;
  799. END;
  800. IF joystickState.y # NIL THEN
  801. IF joystickState.yReport.logicalMinimum<0 THEN
  802. msg.axis[Joystick.AxisY] := TwosComplement(joystickState.y.usageValue,joystickState.yReport.reportSize);
  803. ELSE
  804. msg.axis[Joystick.AxisY] := joystickState.y.usageValue;
  805. END;
  806. END;
  807. IF joystickState.z # NIL THEN
  808. IF joystickState.zReport.logicalMinimum<0 THEN
  809. msg.axis[Joystick.AxisZ] := TwosComplement(joystickState.z.usageValue,joystickState.zReport.reportSize);
  810. ELSE
  811. msg.axis[Joystick.AxisZ] := joystickState.z.usageValue;
  812. END;
  813. END;
  814. IF joystickState.rx # NIL THEN
  815. IF joystickState.rxReport.logicalMinimum<0 THEN
  816. msg.axis[Joystick.AxisRx] := TwosComplement(joystickState.rx.usageValue,joystickState.rxReport.reportSize);
  817. ELSE
  818. msg.axis[Joystick.AxisRx] := joystickState.rx.usageValue;
  819. END;
  820. END;
  821. IF joystickState.ry # NIL THEN
  822. IF joystickState.ryReport.logicalMinimum<0 THEN
  823. msg.axis[Joystick.AxisRy] := TwosComplement(joystickState.ry.usageValue,joystickState.ryReport.reportSize);
  824. ELSE
  825. msg.axis[Joystick.AxisRy] := joystickState.ry.usageValue;
  826. END;
  827. END;
  828. IF joystickState.rz # NIL THEN
  829. IF joystickState.rzReport.logicalMinimum<0 THEN
  830. msg.axis[Joystick.AxisRz] := TwosComplement(joystickState.rz.usageValue,joystickState.rzReport.reportSize);
  831. ELSE
  832. msg.axis[Joystick.AxisRz] := joystickState.rz.usageValue;
  833. END;
  834. END;
  835. IF joystickState.slider # NIL THEN
  836. IF joystickState.sliderReport.logicalMinimum<0 THEN
  837. msg.axis[Joystick.Slider1] := TwosComplement(joystickState.slider.usageValue,joystickState.sliderReport.reportSize);
  838. ELSE
  839. msg.axis[Joystick.Slider1] := joystickState.slider.usageValue;
  840. END;
  841. END;
  842. IF joystickState.hatSwitch # NIL THEN
  843. CASE (joystickState.hatSwitch.usageValue-joystickState.hatSwitchReport.logicalMinimum+1) OF
  844. 1: msg.coolieHat[0]:= {Joystick.HatUp};
  845. |2: msg.coolieHat[0]:= {Joystick.HatUp}+{Joystick.HatLeft};
  846. |3: msg.coolieHat[0]:= {Joystick.HatLeft};
  847. |4: msg.coolieHat[0]:= {Joystick.HatLeft}+{Joystick.HatDown};
  848. |5: msg.coolieHat[0]:= {Joystick.HatDown};
  849. |6: msg.coolieHat[0]:= {Joystick.HatDown}+{Joystick.HatRight};
  850. |7: msg.coolieHat[0]:= {Joystick.HatRight};
  851. |8: msg.coolieHat[0]:= {Joystick.HatRight}+{Joystick.HatUp};
  852. ELSE
  853. END;
  854. END;
  855. joystickState.joystick.Handle(msg);
  856. END HandleJoystickDriver;
  857. (**
  858. * checks, whether the device sends mouse informations or not
  859. *)
  860. PROCEDURE InitializeMouseDriver():BOOLEAN;
  861. VAR
  862. mouseCollection : UsbHidReport.HidCollection;
  863. temp : UsbHidReport.HidReport;
  864. i : LONGINT;
  865. isReportProtocol: BOOLEAN;
  866. BEGIN
  867. (*get mouse collection: mouse collection uses UsagePage(Generic Desktop Controlsl)->1 and Usage(Mouse)->2*)
  868. mouseCollection := reportManager.GetCollection(1,2);
  869. IF (mouseCollection#NIL) THEN
  870. NEW(mouseState);
  871. mouseState.buttonCount := 0;
  872. FOR i:=0 TO 31 DO
  873. IF( mouseState.buttonReport = NIL) THEN
  874. mouseState.buttons[i] := reportManager.GetUsage(UsagePage.ButtonPage, i+1, mouseCollection,mouseState.buttonReport);
  875. ELSE
  876. mouseState.buttons[i] := reportManager.GetUsage(UsagePage.ButtonPage, i+1, mouseCollection,temp);
  877. END;
  878. IF(mouseState.buttons[i]#NIL) THEN
  879. mouseState.buttonCount := i;
  880. END;
  881. (*KernelLog.String("mouseState.buttons ["); KernelLog.Int(i,0); KernelLog.String("] is ");
  882. KernelLog.Int(SYSTEM.VAL(LONGINT, mouseState.buttons[i]),0); KernelLog.Ln;*)
  883. END;
  884. mouseState.x := reportManager.GetUsage(UsagePage.GenericDesktopPage, 30H, mouseCollection,mouseState.axisReport);
  885. IF(mouseState.x=NIL) THEN
  886. KernelLog.String("Initialize mouse driver: error did not find x axis"); KernelLog.Ln;
  887. END;
  888. IF(mouseState.axisReport=NIL) THEN
  889. KernelLog.String("InitializeMouseDriver: Error: Did not find axis report"); KernelLog.Ln;
  890. END;
  891. mouseState.y := reportManager.GetUsage(UsagePage.GenericDesktopPage, 31H, mouseCollection,temp);
  892. IF(mouseState.y=NIL) THEN
  893. KernelLog.String("Initialize mouse driver: error did not find y axis"); KernelLog.Ln;
  894. END;
  895. mouseState.wheel := reportManager.GetUsage(UsagePage.GenericDesktopPage, 38H, mouseCollection,mouseState.wheelReport);
  896. IF(mouseState.wheel=NIL) THEN
  897. KernelLog.String("Initialize mouse driver: warning did not find wheel"); KernelLog.Ln;
  898. END;
  899. IF Trace THEN
  900. KernelLog.String("Found Mouse Configuration"); KernelLog.Ln;
  901. KernelLog.String("Mouse Driver initialized");KernelLog.Ln;
  902. END;
  903. isReportProtocol := SetReportProtocol();
  904. RETURN TRUE;
  905. ELSE
  906. RETURN FALSE;
  907. END;
  908. END InitializeMouseDriver;
  909. (**
  910. * checks, whether the device sends keyboard informations or not
  911. *)
  912. PROCEDURE InitializeKeyboardDriver():BOOLEAN;
  913. VAR
  914. keyboardColl: UsbHidReport.HidCollection;
  915. aUsageTuple : UsbHidReport.UsageTuple;
  916. modifierReport: UsbHidReport.HidReport;
  917. keycodeReport: UsbHidReport.HidReport;
  918. BEGIN
  919. keyboardColl:= reportManager.GetCollection(UsagePage.GenericDesktopPage,UsagePage.KeyboardPage);
  920. IF(keyboardColl#NIL) THEN
  921. IF ~SetIdle(0,10) THEN
  922. IF Debug THEN KernelLog.String("UsbKeyboard: Error: Cannot set idle the keyboard."); KernelLog.Ln; END;
  923. END;
  924. NEW(keyboardState);
  925. keyboardState.Init;
  926. aUsageTuple:= reportManager.GetUsage(UsagePage.KeypadPage, 224,
  927. keyboardColl, modifierReport);
  928. IF (modifierReport=NIL) THEN
  929. IF Debug THEN KernelLog.String("Error did not find modifier"); KernelLog.Ln; END;
  930. keyboardState := NIL;
  931. RETURN FALSE;
  932. ELSE
  933. IF (modifierReport.usages=NIL) THEN
  934. IF Debug THEN KernelLog.String("Error did not find modifiers usages"); KernelLog.Ln; END;
  935. keyboardState := NIL;
  936. RETURN FALSE;
  937. ELSE
  938. IF (LEN(modifierReport.usages)<8) THEN
  939. keyboardState := NIL;
  940. RETURN FALSE;
  941. END;
  942. END;
  943. keyboardState.modifierUsages := modifierReport.usages;
  944. END;
  945. (*assume that the keycodes always begin at usage 0*)
  946. aUsageTuple:= reportManager.GetUsage(UsagePage.KeypadPage, 0,
  947. keyboardColl, keycodeReport);
  948. IF(keycodeReport=NIL) THEN
  949. IF Debug THEN KernelLog.String("Error did not find keycodeReport"); KernelLog.Ln; END;
  950. keyboardState := NIL;
  951. RETURN FALSE;
  952. ELSE
  953. IF(keycodeReport.usages=NIL) THEN
  954. keyboardState := NIL;
  955. IF Debug THEN KernelLog.String("Error did not find keycodeReports usages"); KernelLog.Ln; END;
  956. RETURN FALSE;
  957. (*ELSE
  958. IF (LEN(modifierReport.usages)<8) THEN
  959. keyboardState := NIL;
  960. RETURN FALSE;
  961. END;*)
  962. END;
  963. END;
  964. keyboardState.keycodeUsages := keycodeReport.usages;
  965. keyboardState.SetMaxKeycodes(LEN(keycodeReport.usages));
  966. RETURN SetReportProtocol();
  967. ELSE
  968. RETURN FALSE;
  969. END;
  970. END InitializeKeyboardDriver;
  971. (**
  972. * checks, whether the device sends consumer informations or not
  973. *)
  974. PROCEDURE InitializeConsumerDriver():BOOLEAN;
  975. VAR
  976. consumerColl : UsbHidReport.HidCollection;
  977. temp : UsbHidReport.HidReport;
  978. usageCounter : LONGINT;
  979. BEGIN
  980. consumerColl := reportManager.GetCollection(UsagePage.ConsumerPage, 1H);
  981. IF consumerColl # NIL THEN
  982. NEW(consumerState);
  983. consumerState.consumerReport := consumerColl.firstReport;
  984. IF consumerState.consumerReport # NIL THEN
  985. temp := consumerState.consumerReport;
  986. WHILE (temp # NIL) DO
  987. usageCounter := usageCounter + temp.reportCount;
  988. temp := temp.next;
  989. END;
  990. temp := consumerState.consumerReport;
  991. RETURN TRUE;
  992. ELSE
  993. consumerState := NIL;
  994. END;
  995. END;
  996. RETURN FALSE;
  997. END InitializeConsumerDriver;
  998. (**
  999. * checks, whether the device sends joystick informations or not
  1000. *)
  1001. PROCEDURE InitializeJoystickDriver():BOOLEAN;
  1002. VAR
  1003. joystickColl : UsbHidReport.HidCollection;
  1004. temp : UsbHidReport.HidReport;
  1005. res,i : LONGINT;
  1006. BEGIN
  1007. (*get joystick collection: joystick collection uses UsagePage(Generic Desktop Controlsl)->1 and Usage(Joystick)->4*)
  1008. joystickColl := reportManager.GetCollection(UsagePage.GenericDesktopPage,4);
  1009. IF (joystickColl#NIL) THEN
  1010. NEW(joystickState);
  1011. joystickState.buttonCount := 0;
  1012. FOR i:=0 TO 31 DO
  1013. IF( joystickState.buttonReport = NIL) THEN
  1014. joystickState.buttons[i] := reportManager.GetUsage(UsagePage.ButtonPage, i+1, joystickColl,joystickState.buttonReport);
  1015. (*KernelLog.String("Found button report"); KernelLog.Ln;*)
  1016. ELSE
  1017. joystickState.buttons[i] := reportManager.GetUsage(UsagePage.ButtonPage, i+1, joystickColl,temp);
  1018. END;
  1019. IF(joystickState.buttons[i]#NIL) THEN
  1020. joystickState.buttonCount := joystickState.buttonCount +1;
  1021. (*KernelLog.String(" button ");
  1022. KernelLog.Int(joystickState.buttonReport.usages[i].usageID,0); KernelLog.Ln;*)
  1023. END;
  1024. END;
  1025. IF Debug THEN
  1026. KernelLog.String("Found Joystick Configuration"); KernelLog.Ln;
  1027. END;
  1028. NEW(joystickState.joystick,joystickState.buttonCount);
  1029. joystickState.joystick.desc := "USBHIDJoystick";
  1030. joystickState.x := reportManager.GetUsage(UsagePage.GenericDesktopPage, 30H, joystickColl,joystickState.xReport);
  1031. IF(joystickState.x#NIL) THEN
  1032. joystickState.joystick.AddAxis(Joystick.AxisX, joystickState.xReport.logicalMinimum, joystickState.xReport.logicalMaximum);
  1033. END;
  1034. joystickState.y := reportManager.GetUsage(UsagePage.GenericDesktopPage, 31H, joystickColl,joystickState.yReport);
  1035. IF(joystickState.y#NIL) THEN
  1036. joystickState.joystick.AddAxis(Joystick.AxisY, joystickState.yReport.logicalMinimum, joystickState.yReport.logicalMaximum);
  1037. END;
  1038. joystickState.z := reportManager.GetUsage(UsagePage.GenericDesktopPage, 32H, joystickColl,joystickState.zReport);
  1039. IF(joystickState.z#NIL) THEN
  1040. joystickState.joystick.AddAxis(Joystick.AxisZ, joystickState.zReport.logicalMinimum, joystickState.zReport.logicalMaximum);
  1041. END;
  1042. joystickState.rx := reportManager.GetUsage(UsagePage.GenericDesktopPage, 33H, joystickColl,joystickState.rxReport);
  1043. IF(joystickState.rx#NIL) THEN
  1044. joystickState.joystick.AddAxis(Joystick.AxisRx, joystickState.rxReport.logicalMinimum, joystickState.rxReport.logicalMaximum);
  1045. END;
  1046. joystickState.ry := reportManager.GetUsage(UsagePage.GenericDesktopPage, 34H, joystickColl,joystickState.ryReport);
  1047. IF(joystickState.ry#NIL) THEN
  1048. joystickState.joystick.AddAxis(Joystick.AxisRy, joystickState.ryReport.logicalMinimum, joystickState.ryReport.logicalMaximum);
  1049. END;
  1050. joystickState.rz := reportManager.GetUsage(UsagePage.GenericDesktopPage, 35H, joystickColl,joystickState.rzReport);
  1051. IF(joystickState.rz#NIL) THEN
  1052. joystickState.joystick.AddAxis(Joystick.AxisRz, joystickState.rzReport.logicalMinimum, joystickState.rzReport.logicalMaximum);
  1053. END;
  1054. joystickState.slider := reportManager.GetUsage(UsagePage.GenericDesktopPage, 36H, joystickColl,joystickState.sliderReport);
  1055. IF(joystickState.slider#NIL) THEN
  1056. joystickState.joystick.AddAxis(Joystick.Slider1, joystickState.sliderReport.logicalMinimum, joystickState.sliderReport.logicalMaximum);
  1057. END;
  1058. joystickState.hatSwitch:= reportManager.GetUsage(UsagePage.GenericDesktopPage, 39H, joystickColl,joystickState.hatSwitchReport);
  1059. IF(joystickState.hatSwitch#NIL) THEN
  1060. IF (joystickState.hatSwitchReport.logicalMaximum-joystickState.hatSwitchReport.logicalMinimum=7) THEN
  1061. joystickState.joystick.AddCoolieHat();
  1062. ELSE
  1063. KernelLog.String("HatSwitch found, but not compatible. HatSwitch events are not sent to Joysticks.."); KernelLog.Ln;
  1064. END;
  1065. END;
  1066. Joystick.registry.Add(joystickState.joystick,res);
  1067. RETURN TRUE;
  1068. ELSE
  1069. RETURN FALSE;
  1070. END;
  1071. END InitializeJoystickDriver;
  1072. (**
  1073. * checks, whether the device sends mouse informations or not
  1074. *)
  1075. PROCEDURE InitializeTouchscreenDriver():BOOLEAN;
  1076. VAR
  1077. mouseCollection : UsbHidReport.HidCollection;
  1078. temp : UsbHidReport.HidReport;
  1079. i : LONGINT;
  1080. isReportProtocol: BOOLEAN;
  1081. BEGIN
  1082. (*get mouse collection: mouse collection uses UsagePage(Generic Desktop Controlsl)->1 and Usage(Mouse)->2*)
  1083. mouseCollection := reportManager.GetCollection(0DH, 04H);
  1084. IF (mouseCollection#NIL) THEN
  1085. NEW(touchscreenState);
  1086. touchscreenState.prev := FALSE;
  1087. touchscreenState.x := reportManager.GetUsage(UsagePage.GenericDesktopPage, 30H, mouseCollection,temp);
  1088. touchscreenState.minX := temp.logicalMinimum;
  1089. touchscreenState.maxX := temp.logicalMaximum;
  1090. touchscreenState.y := reportManager.GetUsage(UsagePage.GenericDesktopPage, 31H, mouseCollection,temp);
  1091. touchscreenState.minY := temp.logicalMinimum;
  1092. touchscreenState.maxY := temp.logicalMaximum;
  1093. touchscreenState.tipSwitch := reportManager.GetUsage(0DH, 042H, mouseCollection,temp);
  1094. touchscreenState.inRange := reportManager.GetUsage(0DH, 032H, mouseCollection,temp);
  1095. touchscreenState.confidence := reportManager.GetUsage(0DH, 047H, mouseCollection, temp);
  1096. KernelLog.String("Touchscreen device registered with ");
  1097. KernelLog.String("minX = "); KernelLog.Int(touchscreenState.minX,1);
  1098. KernelLog.String(", maxX = "); KernelLog.Int(touchscreenState.maxX,1);
  1099. KernelLog.String(", minY = "); KernelLog.Int(touchscreenState.minY,1);
  1100. KernelLog.String(", maxY = "); KernelLog.Int(touchscreenState.maxY,1);
  1101. IF touchscreenState.confidence # NIL THEN KernelLog.String(", confidence"); END;
  1102. KernelLog.Ln;
  1103. RETURN TRUE;
  1104. ELSE
  1105. RETURN FALSE;
  1106. END;
  1107. END InitializeTouchscreenDriver;
  1108. (**
  1109. * Reads bitlen bits from a position index
  1110. * @param index[in bits]: where to start reading [1..32]
  1111. * @param bitLen: the amount of bits to read
  1112. * @return value
  1113. *)
  1114. PROCEDURE ReadBits(index, bitLen: LONGINT):LONGINT;
  1115. VAR rv : LONGINT;
  1116. BEGIN
  1117. rv := ReadBitsBuffer(index,bitLen,reportBuffer);
  1118. RETURN rv;
  1119. END ReadBits;
  1120. (**
  1121. * Reads bitlen bits from a position index
  1122. * @param index[in bits]: where to start reading [1..32]
  1123. * @param bitLen: the amount of bits to read
  1124. * @param localBuf: the buffer to read from
  1125. * @return value
  1126. *)
  1127. PROCEDURE ReadBitsBuffer(index, bitLen: LONGINT; localBuf: Usbdi.BufferPtr):LONGINT;
  1128. VAR
  1129. endIndex : LONGINT;
  1130. rv : LONGINT;
  1131. temp : LONGINT;
  1132. indexEightAligned : LONGINT;
  1133. bitsToShift : LONGINT;
  1134. set : SET;
  1135. BEGIN
  1136. endIndex := index + bitLen-1;
  1137. IF bitLen<=0 THEN RETURN 0 END;
  1138. IF Debug THEN KernelLog.String("read bits from "); KernelLog.Int(index,0); KernelLog.String(" to "); KernelLog.Int(endIndex,0); KernelLog.Ln; END;
  1139. IF(endIndex>=(8*LEN(localBuf))) THEN
  1140. IF Debug THEN KernelLog.String("ReadBits: Buffer overflow, endindex is out of localBuf"); KernelLog.Ln; END;
  1141. RETURN 0;
  1142. END;
  1143. IF (bitLen=1) THEN
  1144. (*simply get the bit*)
  1145. set := SYSTEM.VAL(SET, localBuf[index DIV 8]);
  1146. IF (index MOD 8) IN set THEN
  1147. rv := 1;
  1148. ELSE
  1149. rv := 0;
  1150. END;
  1151. RETURN rv;
  1152. END;
  1153. IF ((index DIV 8) = (endIndex DIV 8)) THEN
  1154. (*detect reading simple byte*)
  1155. temp := SYSTEM.VAL(LONGINT, ORD(localBuf[index DIV 8]));
  1156. IF (bitLen=8) THEN
  1157. rv:= temp;
  1158. IF Debug THEN
  1159. KernelLog.String("the byte value is: "); KernelLog.Int(rv,0); KernelLog.Ln;
  1160. END;
  1161. RETURN rv;
  1162. ELSE
  1163. (*simply read in the byte index DIV 8*)
  1164. IF Debug THEN
  1165. KernelLog.Ln;
  1166. KernelLog.String(" the value of the byte is: "); KernelLog.Int(temp,0); KernelLog.Ln;
  1167. KernelLog.String(" (");KernelLog.Bits(SYSTEM.VAL(SET, temp),0,8); KernelLog.String(")"); KernelLog.Ln;
  1168. KernelLog.String(" read in the byte from "); KernelLog.Int(index MOD 8,0); KernelLog.String(" to ");
  1169. KernelLog.Int(endIndex MOD 8,0); KernelLog.String(")"); KernelLog.Ln;
  1170. END;
  1171. temp := SYSTEM.VAL(LONGINT,(SYSTEM.VAL(SET, temp) * {(index MOD 8)..(endIndex MOD 8)}));
  1172. IF Debug THEN
  1173. KernelLog.String(" the value of the byte after masking: "); KernelLog.Bits(SYSTEM.VAL(SET, temp),0,8); KernelLog.Ln;
  1174. END;
  1175. bitsToShift := index MOD 8;
  1176. IF Debug THEN
  1177. KernelLog.String(" bits to shift: "); KernelLog.Int(bitsToShift,0); KernelLog.Ln;
  1178. END;
  1179. rv := SYSTEM.VAL(LONGINT,LSH(SYSTEM.VAL(CHAR,temp),-bitsToShift));
  1180. IF Debug THEN
  1181. KernelLog.String(" the value of the byte after shifting: "); KernelLog.Bits(SYSTEM.VAL(SET, rv),0,8); KernelLog.Ln;
  1182. END;
  1183. END;
  1184. ELSE
  1185. (* the index and the endIndex are not in the same byte
  1186. block position k of index is k="index DIV 8"
  1187. so endBit in the same block is eb=k * 8 + 7
  1188. ex: given: index := 27;
  1189. asked: how many bits to shift the current rv to left
  1190. k := 27 div 8
  1191. k := 3;
  1192. eb := 3 * 8 + 7= 31
  1193. *)
  1194. indexEightAligned := SYSTEM.VAL(LONGINT,SYSTEM.VAL(SET,index)+{0..2});
  1195. IF Debug THEN
  1196. KernelLog.String("index, indexEightAligned, endIndex");
  1197. KernelLog.Int(index,6);KernelLog.Int(indexEightAligned,6);KernelLog.Int(endIndex,6); KernelLog.Ln;
  1198. END;
  1199. temp := ReadBitsBuffer(indexEightAligned+1,endIndex-indexEightAligned, localBuf);
  1200. temp := LSH(temp,indexEightAligned-index+1);
  1201. rv := temp + ReadBitsBuffer(index, indexEightAligned-index+1,localBuf);
  1202. END;
  1203. RETURN rv;
  1204. END ReadBitsBuffer;
  1205. (**
  1206. * for testing the readBits Procedure
  1207. PROCEDURE TestReader;
  1208. VAR
  1209. myBuf: Usbdi.BufferPtr;
  1210. test: LONGINT;
  1211. BEGIN
  1212. NEW(myBuf,8);
  1213. myBuf[0] := CHR(5H);
  1214. myBuf[1] := CHR(7H);
  1215. myBuf[2] := CHR(55H);
  1216. myBuf[3] := CHR(3H);
  1217. myBuf[4] := CHR(5H);
  1218. myBuf[5] := CHR(7H);
  1219. myBuf[6] := CHR(0FFH);
  1220. myBuf[7] := CHR(0FFH);
  1221. KernelLog.Ln;
  1222. KernelLog.String("Initialize TestReader"); KernelLog.Ln;
  1223. KernelLog.String("myBuf[0]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[0])),0); KernelLog.Ln;
  1224. KernelLog.String("myBuf[1]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[1])),0); KernelLog.Ln;
  1225. KernelLog.String("myBuf[2]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[2])),0); KernelLog.Ln;
  1226. KernelLog.String("myBuf[3]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[3])),0); KernelLog.Ln;
  1227. KernelLog.String("myBuf[4]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[4])),0); KernelLog.Ln;
  1228. KernelLog.String("myBuf[5]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[5])),0); KernelLog.Ln;
  1229. KernelLog.String("myBuf[6]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[6])),0); KernelLog.Ln;
  1230. KernelLog.String("myBuf[7]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[7])),0); KernelLog.Ln;
  1231. IF FALSE THEN
  1232. KernelLog.Ln;
  1233. KernelLog.String("Starting Testcases"); KernelLog.Ln;
  1234. KernelLog.String("Reading every bit from myBuf[0]: "); KernelLog.Bits(SYSTEM.VAL(SET,myBuf[0]),0,8); KernelLog.Ln;
  1235. KernelLog.String(" Read 1 bit from 0: "); KernelLog.Int(ReadBitsBuffer(0,1,myBuf),0); KernelLog.Ln;
  1236. KernelLog.String(" Read 1 bit from 1: "); KernelLog.Int(ReadBitsBuffer(1,1,myBuf),0); KernelLog.Ln;
  1237. KernelLog.String(" Read 1 bit from 2: "); KernelLog.Int(ReadBitsBuffer(2,1,myBuf),0); KernelLog.Ln;
  1238. KernelLog.String(" Read 1 bit from 3: "); KernelLog.Int(ReadBitsBuffer(3,1,myBuf),0); KernelLog.Ln;
  1239. KernelLog.String(" Read 1 bit from 4: "); KernelLog.Int(ReadBitsBuffer(4,1,myBuf),0); KernelLog.Ln;
  1240. KernelLog.String(" Read 1 bit from 5: "); KernelLog.Int(ReadBitsBuffer(5,1,myBuf),0); KernelLog.Ln;
  1241. KernelLog.String(" Read 1 bit from 6: "); KernelLog.Int(ReadBitsBuffer(6,1,myBuf),0); KernelLog.Ln;
  1242. KernelLog.String(" Read 1 bit from 7: "); KernelLog.Int(ReadBitsBuffer(7,1,myBuf),0); KernelLog.Ln;
  1243. KernelLog.Ln;
  1244. KernelLog.String("Reading 1-8 bits from myBuf[1]: "); KernelLog.Bits(SYSTEM.VAL(SET,myBuf[1]),0,8); KernelLog.Ln;
  1245. KernelLog.String(" Read 1 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,1,myBuf),0); KernelLog.Ln;
  1246. KernelLog.String(" Read 2 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,2,myBuf),0); KernelLog.Ln;
  1247. KernelLog.String(" Read 3 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,3,myBuf),0); KernelLog.Ln;
  1248. KernelLog.String(" Read 4 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,4,myBuf),0); KernelLog.Ln;
  1249. KernelLog.String(" Read 5 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,5,myBuf),0); KernelLog.Ln;
  1250. KernelLog.String(" Read 6 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,6,myBuf),0); KernelLog.Ln;
  1251. KernelLog.String(" Read 7 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,6,myBuf),0); KernelLog.Ln;
  1252. KernelLog.String(" Read 8 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,8,myBuf),0); KernelLog.Ln;
  1253. KernelLog.Ln;
  1254. KernelLog.String("Reading 1-7 bits from myBuf[2]: "); KernelLog.Bits(SYSTEM.VAL(SET,myBuf[2]),0,8); KernelLog.Ln;
  1255. KernelLog.String(" Read 1 bit from 17: "); KernelLog.Int(ReadBitsBuffer(17,1,myBuf),0); KernelLog.Ln;
  1256. KernelLog.String(" Read 2 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,2,myBuf),0); KernelLog.Ln;
  1257. KernelLog.String(" Read 3 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,3,myBuf),0); KernelLog.Ln;
  1258. KernelLog.String(" Read 4 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,4,myBuf),0); KernelLog.Ln;
  1259. KernelLog.String(" Read 5 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,5,myBuf),0); KernelLog.Ln;
  1260. KernelLog.String(" Read 6 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,6,myBuf),0); KernelLog.Ln;
  1261. KernelLog.String(" Read 7 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,6,myBuf),0); KernelLog.Ln;
  1262. KernelLog.Ln;
  1263. KernelLog.String("Read 8 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,8,myBuf),0); KernelLog.Ln;
  1264. KernelLog.String("Read 9 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,9,myBuf),0); KernelLog.Ln;
  1265. KernelLog.String("Read 10 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,10,myBuf),0); KernelLog.Ln;
  1266. KernelLog.String("Read 11 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,11,myBuf),0); KernelLog.Ln;
  1267. KernelLog.String("Read 12 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,12,myBuf),0); KernelLog.Ln;
  1268. KernelLog.String("Read 13 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,13,myBuf),0); KernelLog.Ln;
  1269. KernelLog.String("Read 14 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,14,myBuf),0); KernelLog.Ln;
  1270. KernelLog.Ln;
  1271. KernelLog.String("Read 7 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,7,myBuf),0); KernelLog.Ln;
  1272. KernelLog.String("Read 8 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,8,myBuf),0); KernelLog.Ln;
  1273. KernelLog.String("Read 9 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,9,myBuf),0); KernelLog.Ln;
  1274. KernelLog.String("Read 10 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,10,myBuf),0); KernelLog.Ln;
  1275. KernelLog.String("Read 11 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,11,myBuf),0); KernelLog.Ln;
  1276. KernelLog.String("Read 12 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,12,myBuf),0); KernelLog.Ln;
  1277. KernelLog.String("Read 13 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,13,myBuf),0); KernelLog.Ln;
  1278. KernelLog.Ln;
  1279. KernelLog.String("Read 8 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,8,myBuf),0); KernelLog.Ln;
  1280. KernelLog.String("Read 9 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,9,myBuf),0); KernelLog.Ln;
  1281. KernelLog.String("Read 10 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,10,myBuf),0); KernelLog.Ln;
  1282. KernelLog.String("Read 11 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,11,myBuf),0); KernelLog.Ln;
  1283. KernelLog.String("Read 12 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,12,myBuf),0); KernelLog.Ln;
  1284. KernelLog.String("Read 13 bits from 23: "); KernelLog.Int(ReadBitsBuffer(32,13,myBuf),0); KernelLog.Ln;
  1285. KernelLog.String("Read 14 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,14,myBuf),0); KernelLog.Ln;
  1286. KernelLog.Ln;
  1287. KernelLog.String("Read 7 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,7,myBuf),0); KernelLog.Ln;
  1288. KernelLog.String("Read 8 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,8,myBuf),0); KernelLog.Ln;
  1289. KernelLog.String("Read 9 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,9,myBuf),0); KernelLog.Ln;
  1290. KernelLog.String("Read 10 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,10,myBuf),0); KernelLog.Ln;
  1291. KernelLog.String("Read 11 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,11,myBuf),0); KernelLog.Ln;
  1292. KernelLog.String("Read 12 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,12,myBuf),0); KernelLog.Ln;
  1293. KernelLog.String("Read 13 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,13,myBuf),0); KernelLog.Ln;
  1294. KernelLog.Ln;
  1295. END;
  1296. KernelLog.String("TwosComplement of 6 in (4 Bits): "); KernelLog.Int(TwosComplement(6,4),0); KernelLog.Ln;
  1297. KernelLog.String("TwosComplement of 7 in (4 Bits): "); KernelLog.Int(TwosComplement(7,4),0); KernelLog.Ln;
  1298. KernelLog.String("TwosComplement of 8 in (4 Bits): "); KernelLog.Int(TwosComplement(8,4),0); KernelLog.Ln;
  1299. KernelLog.String("TwosComplement of 13 in (4 Bits): "); KernelLog.Int(TwosComplement(13,4),0); KernelLog.Ln;
  1300. KernelLog.String("TwosComplement of 14 in (4 Bits): "); KernelLog.Int(TwosComplement(14,4),0); KernelLog.Ln;
  1301. KernelLog.String("TwosComplement of 15 in (4 Bits): "); KernelLog.Int(TwosComplement(15,4),0); KernelLog.Ln;
  1302. KernelLog.String("TwosComplement of 15 in (5 Bits): "); KernelLog.Int(TwosComplement(15,5),0); KernelLog.Ln;
  1303. KernelLog.String("TwosComplement of 16 in (5 Bits): "); KernelLog.Int(TwosComplement(16,5),0); KernelLog.Ln;
  1304. KernelLog.String("TwosComplement of 28 in (5 Bits): "); KernelLog.Int(TwosComplement(28,5),0); KernelLog.Ln;
  1305. KernelLog.String("TwosComplement of 29 in (5 Bits): "); KernelLog.Int(TwosComplement(29,5),0); KernelLog.Ln;
  1306. KernelLog.String("TwosComplement of 30 in (5 Bits): "); KernelLog.Int(TwosComplement(30,5),0); KernelLog.Ln;
  1307. KernelLog.String("TwosComplement of 31 in (5 Bits): "); KernelLog.Int(TwosComplement(31,5),0); KernelLog.Ln;
  1308. KernelLog.Ln;
  1309. test := ReadBitsBuffer(48,12, myBuf);
  1310. KernelLog.String("Reading 12 Bits at block 6 (starting at 48, 12 bits: "); KernelLog.Int(test,0); KernelLog.Ln;
  1311. KernelLog.String(" ?= 4095. Twos Complement should be -1: "); KernelLog.Int(TwosComplement(test,12),0); KernelLog.Ln;
  1312. test := ReadBitsBuffer(49,12, myBuf);
  1313. KernelLog.String("Reading 12 Bits at block 6 (starting at 49, 12 bits: "); KernelLog.Int(test,0); KernelLog.Ln;
  1314. KernelLog.String(" ?= 4095. Twos Complement should be -1: "); KernelLog.Int(TwosComplement(test,12),0); KernelLog.Ln;
  1315. END TestReader;
  1316. *)
  1317. (**
  1318. * returns the twos complement of a value by the predicted bitLen
  1319. * @param value: the value to convert
  1320. * @param bitLen: the bit length of the value it can have
  1321. * @return twos complement of value
  1322. *)
  1323. PROCEDURE TwosComplement(value: LONGINT; bitLen: LONGINT) : LONGINT;
  1324. VAR toMuch : LONGINT;
  1325. BEGIN
  1326. IF(bitLen<32) & (bitLen>0) THEN
  1327. IF ((bitLen-1) IN SYSTEM.VAL(SET,value)) THEN
  1328. toMuch:= SYSTEM.VAL(LONGINT,{bitLen});
  1329. value := value - toMuch;
  1330. END;
  1331. END;
  1332. RETURN value;
  1333. END TwosComplement;
  1334. (**
  1335. * Sets the device into normal mode (report protocol mode)
  1336. *)
  1337. PROCEDURE SetReportProtocol():BOOLEAN;
  1338. VAR
  1339. (*0: if boot protocol, 1: if report protocol*)
  1340. bootFlag: LONGINT;
  1341. BEGIN
  1342. IF (GetProtocol(bootFlag)) THEN
  1343. IF(bootFlag=0) THEN
  1344. IF Debug THEN
  1345. KernelLog.String("UsbHidDriver:HidDriver.Connect: GetProtocol returned boot protocol, set to report protocol"); KernelLog.Ln;
  1346. END;
  1347. IF(SetProtocol(1)=FALSE) THEN
  1348. KernelLog.String("UsbHidDriver:HidDriver.Connect: SetProtocol to report failed"); KernelLog.Ln;
  1349. RETURN FALSE;
  1350. END;
  1351. ELSE
  1352. IF Debug THEN
  1353. KernelLog.String("UsbHidDriver:HidDriver.Connect: GetProtocol returned report protocol"); KernelLog.Ln;
  1354. END;
  1355. END;
  1356. END;
  1357. RETURN TRUE;
  1358. END SetReportProtocol;
  1359. END HidDriver;
  1360. (*used for debug output. lists the report descriptor as described in Device Class Definition for Human Interface Devices,
  1361. f.e. page 61 Appendix A, B.2 Protocol 2 (Mouse)*)
  1362. PROCEDURE LayoutBuffer*(CONST buf : Usbdi.Buffer; len : LONGINT);
  1363. VAR temp : LONGINT;
  1364. BEGIN
  1365. KernelLog.String("Buffer Outline:"); KernelLog.Ln;
  1366. FOR temp := 0 TO len-1 DO
  1367. IF (temp MOD 2 = 0) THEN
  1368. KernelLog.Ln();
  1369. KernelLog.Int(temp, 4);
  1370. KernelLog.String(" ");
  1371. KernelLog.Hex(ORD(buf[temp]), -2);
  1372. ELSE
  1373. KernelLog.String(" ");
  1374. KernelLog.Hex(ORD(buf[temp]), -2);
  1375. END;
  1376. END;
  1377. KernelLog.Ln(); KernelLog.Ln();
  1378. END LayoutBuffer;
  1379. (*check, whether the device is a hid device
  1380. * return HidDriver, if hid device found, NIL otherwise
  1381. *)
  1382. PROCEDURE Probe(dev : Usbdi.UsbDevice; if : Usbdi.InterfaceDescriptor) : Usbdi.Driver;
  1383. VAR hidDriver : HidDriver;
  1384. BEGIN
  1385. IF if.bInterfaceClass # 3 THEN RETURN NIL END;
  1386. NEW(hidDriver);
  1387. RETURN hidDriver;
  1388. END Probe;
  1389. (* Called, when detaching the UsbHidDriver *)
  1390. PROCEDURE Cleanup;
  1391. BEGIN
  1392. Usbdi.drivers.Remove(Name);
  1393. END Cleanup;
  1394. PROCEDURE Install*;
  1395. END Install;
  1396. TYPE
  1397. DimensionGetter= PROCEDURE {DELEGATE} (VAR w,h: LONGINT);
  1398. VAR
  1399. getDisplayDimensions: DimensionGetter;
  1400. touchConfidenceDelay: LONGINT;
  1401. PROCEDURE DefaultDisplayDimensions(VAR w,h: LONGINT);
  1402. BEGIN
  1403. w := 1024; h := 768;
  1404. END DefaultDisplayDimensions;
  1405. PROCEDURE InstallDisplayDimensions*(poller: DimensionGetter); (* for touchscreen coordinate transformation*)
  1406. BEGIN
  1407. IF poller # NIL THEN
  1408. getDisplayDimensions := poller;
  1409. ELSE
  1410. getDisplayDimensions := DefaultDisplayDimensions;
  1411. END;
  1412. END InstallDisplayDimensions;
  1413. PROCEDURE Setup;
  1414. VAR s: ARRAY 32 OF CHAR; p: LONGINT;
  1415. BEGIN
  1416. getDisplayDimensions := DefaultDisplayDimensions;
  1417. Machine.GetConfig("TouchDelay", s);
  1418. IF s[0] # 0X THEN
  1419. p := 0; touchConfidenceDelay := Machine.StrToInt(p, s)
  1420. ELSE
  1421. touchConfidenceDelay := 0;
  1422. END;
  1423. Usbdi.drivers.Add(Probe, Name, Description, 10);
  1424. END Setup;
  1425. BEGIN
  1426. Modules.InstallTermHandler(Cleanup);
  1427. Setup;
  1428. END UsbHidDriver.
  1429. UsbHidDriver.Install ~ SystemTools.Free UsbHidDriver UsbHidParser UsbHidErrors UsbHidParserExt UsbHidReport UsbHidUP~