UsbHidDriver.Mod 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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. * System.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 : WORD;
  1006. i: LONGINT;
  1007. BEGIN
  1008. (*get joystick collection: joystick collection uses UsagePage(Generic Desktop Controlsl)->1 and Usage(Joystick)->4*)
  1009. joystickColl := reportManager.GetCollection(UsagePage.GenericDesktopPage,4);
  1010. IF (joystickColl#NIL) THEN
  1011. NEW(joystickState);
  1012. joystickState.buttonCount := 0;
  1013. FOR i:=0 TO 31 DO
  1014. IF( joystickState.buttonReport = NIL) THEN
  1015. joystickState.buttons[i] := reportManager.GetUsage(UsagePage.ButtonPage, i+1, joystickColl,joystickState.buttonReport);
  1016. (*KernelLog.String("Found button report"); KernelLog.Ln;*)
  1017. ELSE
  1018. joystickState.buttons[i] := reportManager.GetUsage(UsagePage.ButtonPage, i+1, joystickColl,temp);
  1019. END;
  1020. IF(joystickState.buttons[i]#NIL) THEN
  1021. joystickState.buttonCount := joystickState.buttonCount +1;
  1022. (*KernelLog.String(" button ");
  1023. KernelLog.Int(joystickState.buttonReport.usages[i].usageID,0); KernelLog.Ln;*)
  1024. END;
  1025. END;
  1026. IF Debug THEN
  1027. KernelLog.String("Found Joystick Configuration"); KernelLog.Ln;
  1028. END;
  1029. NEW(joystickState.joystick,joystickState.buttonCount);
  1030. joystickState.joystick.desc := "USBHIDJoystick";
  1031. joystickState.x := reportManager.GetUsage(UsagePage.GenericDesktopPage, 30H, joystickColl,joystickState.xReport);
  1032. IF(joystickState.x#NIL) THEN
  1033. joystickState.joystick.AddAxis(Joystick.AxisX, joystickState.xReport.logicalMinimum, joystickState.xReport.logicalMaximum);
  1034. END;
  1035. joystickState.y := reportManager.GetUsage(UsagePage.GenericDesktopPage, 31H, joystickColl,joystickState.yReport);
  1036. IF(joystickState.y#NIL) THEN
  1037. joystickState.joystick.AddAxis(Joystick.AxisY, joystickState.yReport.logicalMinimum, joystickState.yReport.logicalMaximum);
  1038. END;
  1039. joystickState.z := reportManager.GetUsage(UsagePage.GenericDesktopPage, 32H, joystickColl,joystickState.zReport);
  1040. IF(joystickState.z#NIL) THEN
  1041. joystickState.joystick.AddAxis(Joystick.AxisZ, joystickState.zReport.logicalMinimum, joystickState.zReport.logicalMaximum);
  1042. END;
  1043. joystickState.rx := reportManager.GetUsage(UsagePage.GenericDesktopPage, 33H, joystickColl,joystickState.rxReport);
  1044. IF(joystickState.rx#NIL) THEN
  1045. joystickState.joystick.AddAxis(Joystick.AxisRx, joystickState.rxReport.logicalMinimum, joystickState.rxReport.logicalMaximum);
  1046. END;
  1047. joystickState.ry := reportManager.GetUsage(UsagePage.GenericDesktopPage, 34H, joystickColl,joystickState.ryReport);
  1048. IF(joystickState.ry#NIL) THEN
  1049. joystickState.joystick.AddAxis(Joystick.AxisRy, joystickState.ryReport.logicalMinimum, joystickState.ryReport.logicalMaximum);
  1050. END;
  1051. joystickState.rz := reportManager.GetUsage(UsagePage.GenericDesktopPage, 35H, joystickColl,joystickState.rzReport);
  1052. IF(joystickState.rz#NIL) THEN
  1053. joystickState.joystick.AddAxis(Joystick.AxisRz, joystickState.rzReport.logicalMinimum, joystickState.rzReport.logicalMaximum);
  1054. END;
  1055. joystickState.slider := reportManager.GetUsage(UsagePage.GenericDesktopPage, 36H, joystickColl,joystickState.sliderReport);
  1056. IF(joystickState.slider#NIL) THEN
  1057. joystickState.joystick.AddAxis(Joystick.Slider1, joystickState.sliderReport.logicalMinimum, joystickState.sliderReport.logicalMaximum);
  1058. END;
  1059. joystickState.hatSwitch:= reportManager.GetUsage(UsagePage.GenericDesktopPage, 39H, joystickColl,joystickState.hatSwitchReport);
  1060. IF(joystickState.hatSwitch#NIL) THEN
  1061. IF (joystickState.hatSwitchReport.logicalMaximum-joystickState.hatSwitchReport.logicalMinimum=7) THEN
  1062. joystickState.joystick.AddCoolieHat();
  1063. ELSE
  1064. KernelLog.String("HatSwitch found, but not compatible. HatSwitch events are not sent to Joysticks.."); KernelLog.Ln;
  1065. END;
  1066. END;
  1067. Joystick.registry.Add(joystickState.joystick,res);
  1068. RETURN TRUE;
  1069. ELSE
  1070. RETURN FALSE;
  1071. END;
  1072. END InitializeJoystickDriver;
  1073. (**
  1074. * checks, whether the device sends mouse informations or not
  1075. *)
  1076. PROCEDURE InitializeTouchscreenDriver():BOOLEAN;
  1077. VAR
  1078. mouseCollection : UsbHidReport.HidCollection;
  1079. temp : UsbHidReport.HidReport;
  1080. i : LONGINT;
  1081. isReportProtocol: BOOLEAN;
  1082. BEGIN
  1083. (*get mouse collection: mouse collection uses UsagePage(Generic Desktop Controlsl)->1 and Usage(Mouse)->2*)
  1084. mouseCollection := reportManager.GetCollection(0DH, 04H);
  1085. IF (mouseCollection#NIL) THEN
  1086. NEW(touchscreenState);
  1087. touchscreenState.prev := FALSE;
  1088. touchscreenState.x := reportManager.GetUsage(UsagePage.GenericDesktopPage, 30H, mouseCollection,temp);
  1089. touchscreenState.minX := temp.logicalMinimum;
  1090. touchscreenState.maxX := temp.logicalMaximum;
  1091. touchscreenState.y := reportManager.GetUsage(UsagePage.GenericDesktopPage, 31H, mouseCollection,temp);
  1092. touchscreenState.minY := temp.logicalMinimum;
  1093. touchscreenState.maxY := temp.logicalMaximum;
  1094. touchscreenState.tipSwitch := reportManager.GetUsage(0DH, 042H, mouseCollection,temp);
  1095. touchscreenState.inRange := reportManager.GetUsage(0DH, 032H, mouseCollection,temp);
  1096. touchscreenState.confidence := reportManager.GetUsage(0DH, 047H, mouseCollection, temp);
  1097. KernelLog.String("Touchscreen device registered with ");
  1098. KernelLog.String("minX = "); KernelLog.Int(touchscreenState.minX,1);
  1099. KernelLog.String(", maxX = "); KernelLog.Int(touchscreenState.maxX,1);
  1100. KernelLog.String(", minY = "); KernelLog.Int(touchscreenState.minY,1);
  1101. KernelLog.String(", maxY = "); KernelLog.Int(touchscreenState.maxY,1);
  1102. IF touchscreenState.confidence # NIL THEN KernelLog.String(", confidence"); END;
  1103. KernelLog.Ln;
  1104. RETURN TRUE;
  1105. ELSE
  1106. RETURN FALSE;
  1107. END;
  1108. END InitializeTouchscreenDriver;
  1109. (**
  1110. * Reads bitlen bits from a position index
  1111. * @param index[in bits]: where to start reading [1..32]
  1112. * @param bitLen: the amount of bits to read
  1113. * @return value
  1114. *)
  1115. PROCEDURE ReadBits(index, bitLen: LONGINT):LONGINT;
  1116. VAR rv : LONGINT;
  1117. BEGIN
  1118. rv := ReadBitsBuffer(index,bitLen,reportBuffer);
  1119. RETURN rv;
  1120. END ReadBits;
  1121. (**
  1122. * Reads bitlen bits from a position index
  1123. * @param index[in bits]: where to start reading [1..32]
  1124. * @param bitLen: the amount of bits to read
  1125. * @param localBuf: the buffer to read from
  1126. * @return value
  1127. *)
  1128. PROCEDURE ReadBitsBuffer(index, bitLen: LONGINT; localBuf: Usbdi.BufferPtr):LONGINT;
  1129. VAR
  1130. endIndex : LONGINT;
  1131. rv : LONGINT;
  1132. temp : LONGINT;
  1133. indexEightAligned : LONGINT;
  1134. bitsToShift : LONGINT;
  1135. set : SET;
  1136. BEGIN
  1137. endIndex := index + bitLen-1;
  1138. IF bitLen<=0 THEN RETURN 0 END;
  1139. IF Debug THEN KernelLog.String("read bits from "); KernelLog.Int(index,0); KernelLog.String(" to "); KernelLog.Int(endIndex,0); KernelLog.Ln; END;
  1140. IF(endIndex>=(8*LEN(localBuf))) THEN
  1141. IF Debug THEN KernelLog.String("ReadBits: Buffer overflow, endindex is out of localBuf"); KernelLog.Ln; END;
  1142. RETURN 0;
  1143. END;
  1144. IF (bitLen=1) THEN
  1145. (*simply get the bit*)
  1146. set := SYSTEM.VAL(SET, localBuf[index DIV 8]);
  1147. IF (index MOD 8) IN set THEN
  1148. rv := 1;
  1149. ELSE
  1150. rv := 0;
  1151. END;
  1152. RETURN rv;
  1153. END;
  1154. IF ((index DIV 8) = (endIndex DIV 8)) THEN
  1155. (*detect reading simple byte*)
  1156. temp := SYSTEM.VAL(LONGINT, ORD(localBuf[index DIV 8]));
  1157. IF (bitLen=8) THEN
  1158. rv:= temp;
  1159. IF Debug THEN
  1160. KernelLog.String("the byte value is: "); KernelLog.Int(rv,0); KernelLog.Ln;
  1161. END;
  1162. RETURN rv;
  1163. ELSE
  1164. (*simply read in the byte index DIV 8*)
  1165. IF Debug THEN
  1166. KernelLog.Ln;
  1167. KernelLog.String(" the value of the byte is: "); KernelLog.Int(temp,0); KernelLog.Ln;
  1168. KernelLog.String(" (");KernelLog.Bits(SYSTEM.VAL(SET, temp),0,8); KernelLog.String(")"); KernelLog.Ln;
  1169. KernelLog.String(" read in the byte from "); KernelLog.Int(index MOD 8,0); KernelLog.String(" to ");
  1170. KernelLog.Int(endIndex MOD 8,0); KernelLog.String(")"); KernelLog.Ln;
  1171. END;
  1172. temp := SYSTEM.VAL(LONGINT,(SYSTEM.VAL(SET, temp) * {(index MOD 8)..(endIndex MOD 8)}));
  1173. IF Debug THEN
  1174. KernelLog.String(" the value of the byte after masking: "); KernelLog.Bits(SYSTEM.VAL(SET, temp),0,8); KernelLog.Ln;
  1175. END;
  1176. bitsToShift := index MOD 8;
  1177. IF Debug THEN
  1178. KernelLog.String(" bits to shift: "); KernelLog.Int(bitsToShift,0); KernelLog.Ln;
  1179. END;
  1180. rv := SYSTEM.VAL(LONGINT,LSH(SYSTEM.VAL(CHAR,temp),-bitsToShift));
  1181. IF Debug THEN
  1182. KernelLog.String(" the value of the byte after shifting: "); KernelLog.Bits(SYSTEM.VAL(SET, rv),0,8); KernelLog.Ln;
  1183. END;
  1184. END;
  1185. ELSE
  1186. (* the index and the endIndex are not in the same byte
  1187. block position k of index is k="index DIV 8"
  1188. so endBit in the same block is eb=k * 8 + 7
  1189. ex: given: index := 27;
  1190. asked: how many bits to shift the current rv to left
  1191. k := 27 div 8
  1192. k := 3;
  1193. eb := 3 * 8 + 7= 31
  1194. *)
  1195. indexEightAligned := SYSTEM.VAL(LONGINT,SYSTEM.VAL(SET,index)+{0..2});
  1196. IF Debug THEN
  1197. KernelLog.String("index, indexEightAligned, endIndex");
  1198. KernelLog.Int(index,6);KernelLog.Int(indexEightAligned,6);KernelLog.Int(endIndex,6); KernelLog.Ln;
  1199. END;
  1200. temp := ReadBitsBuffer(indexEightAligned+1,endIndex-indexEightAligned, localBuf);
  1201. temp := LSH(temp,indexEightAligned-index+1);
  1202. rv := temp + ReadBitsBuffer(index, indexEightAligned-index+1,localBuf);
  1203. END;
  1204. RETURN rv;
  1205. END ReadBitsBuffer;
  1206. (**
  1207. * for testing the readBits Procedure
  1208. PROCEDURE TestReader;
  1209. VAR
  1210. myBuf: Usbdi.BufferPtr;
  1211. test: LONGINT;
  1212. BEGIN
  1213. NEW(myBuf,8);
  1214. myBuf[0] := CHR(5H);
  1215. myBuf[1] := CHR(7H);
  1216. myBuf[2] := CHR(55H);
  1217. myBuf[3] := CHR(3H);
  1218. myBuf[4] := CHR(5H);
  1219. myBuf[5] := CHR(7H);
  1220. myBuf[6] := CHR(0FFH);
  1221. myBuf[7] := CHR(0FFH);
  1222. KernelLog.Ln;
  1223. KernelLog.String("Initialize TestReader"); KernelLog.Ln;
  1224. KernelLog.String("myBuf[0]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[0])),0); KernelLog.Ln;
  1225. KernelLog.String("myBuf[1]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[1])),0); KernelLog.Ln;
  1226. KernelLog.String("myBuf[2]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[2])),0); KernelLog.Ln;
  1227. KernelLog.String("myBuf[3]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[3])),0); KernelLog.Ln;
  1228. KernelLog.String("myBuf[4]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[4])),0); KernelLog.Ln;
  1229. KernelLog.String("myBuf[5]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[5])),0); KernelLog.Ln;
  1230. KernelLog.String("myBuf[6]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[6])),0); KernelLog.Ln;
  1231. KernelLog.String("myBuf[7]: "); KernelLog.Int(SYSTEM.VAL(LONGINT,ORD(myBuf[7])),0); KernelLog.Ln;
  1232. IF FALSE THEN
  1233. KernelLog.Ln;
  1234. KernelLog.String("Starting Testcases"); KernelLog.Ln;
  1235. KernelLog.String("Reading every bit from myBuf[0]: "); KernelLog.Bits(SYSTEM.VAL(SET,myBuf[0]),0,8); KernelLog.Ln;
  1236. KernelLog.String(" Read 1 bit from 0: "); KernelLog.Int(ReadBitsBuffer(0,1,myBuf),0); KernelLog.Ln;
  1237. KernelLog.String(" Read 1 bit from 1: "); KernelLog.Int(ReadBitsBuffer(1,1,myBuf),0); KernelLog.Ln;
  1238. KernelLog.String(" Read 1 bit from 2: "); KernelLog.Int(ReadBitsBuffer(2,1,myBuf),0); KernelLog.Ln;
  1239. KernelLog.String(" Read 1 bit from 3: "); KernelLog.Int(ReadBitsBuffer(3,1,myBuf),0); KernelLog.Ln;
  1240. KernelLog.String(" Read 1 bit from 4: "); KernelLog.Int(ReadBitsBuffer(4,1,myBuf),0); KernelLog.Ln;
  1241. KernelLog.String(" Read 1 bit from 5: "); KernelLog.Int(ReadBitsBuffer(5,1,myBuf),0); KernelLog.Ln;
  1242. KernelLog.String(" Read 1 bit from 6: "); KernelLog.Int(ReadBitsBuffer(6,1,myBuf),0); KernelLog.Ln;
  1243. KernelLog.String(" Read 1 bit from 7: "); KernelLog.Int(ReadBitsBuffer(7,1,myBuf),0); KernelLog.Ln;
  1244. KernelLog.Ln;
  1245. KernelLog.String("Reading 1-8 bits from myBuf[1]: "); KernelLog.Bits(SYSTEM.VAL(SET,myBuf[1]),0,8); KernelLog.Ln;
  1246. KernelLog.String(" Read 1 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,1,myBuf),0); KernelLog.Ln;
  1247. KernelLog.String(" Read 2 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,2,myBuf),0); KernelLog.Ln;
  1248. KernelLog.String(" Read 3 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,3,myBuf),0); KernelLog.Ln;
  1249. KernelLog.String(" Read 4 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,4,myBuf),0); KernelLog.Ln;
  1250. KernelLog.String(" Read 5 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,5,myBuf),0); KernelLog.Ln;
  1251. KernelLog.String(" Read 6 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,6,myBuf),0); KernelLog.Ln;
  1252. KernelLog.String(" Read 7 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,6,myBuf),0); KernelLog.Ln;
  1253. KernelLog.String(" Read 8 bits from 8: "); KernelLog.Int(ReadBitsBuffer(8,8,myBuf),0); KernelLog.Ln;
  1254. KernelLog.Ln;
  1255. KernelLog.String("Reading 1-7 bits from myBuf[2]: "); KernelLog.Bits(SYSTEM.VAL(SET,myBuf[2]),0,8); KernelLog.Ln;
  1256. KernelLog.String(" Read 1 bit from 17: "); KernelLog.Int(ReadBitsBuffer(17,1,myBuf),0); KernelLog.Ln;
  1257. KernelLog.String(" Read 2 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,2,myBuf),0); KernelLog.Ln;
  1258. KernelLog.String(" Read 3 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,3,myBuf),0); KernelLog.Ln;
  1259. KernelLog.String(" Read 4 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,4,myBuf),0); KernelLog.Ln;
  1260. KernelLog.String(" Read 5 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,5,myBuf),0); KernelLog.Ln;
  1261. KernelLog.String(" Read 6 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,6,myBuf),0); KernelLog.Ln;
  1262. KernelLog.String(" Read 7 bits from 17: "); KernelLog.Int(ReadBitsBuffer(17,6,myBuf),0); KernelLog.Ln;
  1263. KernelLog.Ln;
  1264. KernelLog.String("Read 8 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,8,myBuf),0); KernelLog.Ln;
  1265. KernelLog.String("Read 9 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,9,myBuf),0); KernelLog.Ln;
  1266. KernelLog.String("Read 10 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,10,myBuf),0); KernelLog.Ln;
  1267. KernelLog.String("Read 11 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,11,myBuf),0); KernelLog.Ln;
  1268. KernelLog.String("Read 12 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,12,myBuf),0); KernelLog.Ln;
  1269. KernelLog.String("Read 13 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,13,myBuf),0); KernelLog.Ln;
  1270. KernelLog.String("Read 14 bits from 0: "); KernelLog.Int(ReadBitsBuffer(0,14,myBuf),0); KernelLog.Ln;
  1271. KernelLog.Ln;
  1272. KernelLog.String("Read 7 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,7,myBuf),0); KernelLog.Ln;
  1273. KernelLog.String("Read 8 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,8,myBuf),0); KernelLog.Ln;
  1274. KernelLog.String("Read 9 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,9,myBuf),0); KernelLog.Ln;
  1275. KernelLog.String("Read 10 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,10,myBuf),0); KernelLog.Ln;
  1276. KernelLog.String("Read 11 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,11,myBuf),0); KernelLog.Ln;
  1277. KernelLog.String("Read 12 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,12,myBuf),0); KernelLog.Ln;
  1278. KernelLog.String("Read 13 bits from 1: "); KernelLog.Int(ReadBitsBuffer(1,13,myBuf),0); KernelLog.Ln;
  1279. KernelLog.Ln;
  1280. KernelLog.String("Read 8 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,8,myBuf),0); KernelLog.Ln;
  1281. KernelLog.String("Read 9 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,9,myBuf),0); KernelLog.Ln;
  1282. KernelLog.String("Read 10 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,10,myBuf),0); KernelLog.Ln;
  1283. KernelLog.String("Read 11 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,11,myBuf),0); KernelLog.Ln;
  1284. KernelLog.String("Read 12 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,12,myBuf),0); KernelLog.Ln;
  1285. KernelLog.String("Read 13 bits from 23: "); KernelLog.Int(ReadBitsBuffer(32,13,myBuf),0); KernelLog.Ln;
  1286. KernelLog.String("Read 14 bits from 32: "); KernelLog.Int(ReadBitsBuffer(32,14,myBuf),0); KernelLog.Ln;
  1287. KernelLog.Ln;
  1288. KernelLog.String("Read 7 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,7,myBuf),0); KernelLog.Ln;
  1289. KernelLog.String("Read 8 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,8,myBuf),0); KernelLog.Ln;
  1290. KernelLog.String("Read 9 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,9,myBuf),0); KernelLog.Ln;
  1291. KernelLog.String("Read 10 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,10,myBuf),0); KernelLog.Ln;
  1292. KernelLog.String("Read 11 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,11,myBuf),0); KernelLog.Ln;
  1293. KernelLog.String("Read 12 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,12,myBuf),0); KernelLog.Ln;
  1294. KernelLog.String("Read 13 bits from 33: "); KernelLog.Int(ReadBitsBuffer(33,13,myBuf),0); KernelLog.Ln;
  1295. KernelLog.Ln;
  1296. END;
  1297. KernelLog.String("TwosComplement of 6 in (4 Bits): "); KernelLog.Int(TwosComplement(6,4),0); KernelLog.Ln;
  1298. KernelLog.String("TwosComplement of 7 in (4 Bits): "); KernelLog.Int(TwosComplement(7,4),0); KernelLog.Ln;
  1299. KernelLog.String("TwosComplement of 8 in (4 Bits): "); KernelLog.Int(TwosComplement(8,4),0); KernelLog.Ln;
  1300. KernelLog.String("TwosComplement of 13 in (4 Bits): "); KernelLog.Int(TwosComplement(13,4),0); KernelLog.Ln;
  1301. KernelLog.String("TwosComplement of 14 in (4 Bits): "); KernelLog.Int(TwosComplement(14,4),0); KernelLog.Ln;
  1302. KernelLog.String("TwosComplement of 15 in (4 Bits): "); KernelLog.Int(TwosComplement(15,4),0); KernelLog.Ln;
  1303. KernelLog.String("TwosComplement of 15 in (5 Bits): "); KernelLog.Int(TwosComplement(15,5),0); KernelLog.Ln;
  1304. KernelLog.String("TwosComplement of 16 in (5 Bits): "); KernelLog.Int(TwosComplement(16,5),0); KernelLog.Ln;
  1305. KernelLog.String("TwosComplement of 28 in (5 Bits): "); KernelLog.Int(TwosComplement(28,5),0); KernelLog.Ln;
  1306. KernelLog.String("TwosComplement of 29 in (5 Bits): "); KernelLog.Int(TwosComplement(29,5),0); KernelLog.Ln;
  1307. KernelLog.String("TwosComplement of 30 in (5 Bits): "); KernelLog.Int(TwosComplement(30,5),0); KernelLog.Ln;
  1308. KernelLog.String("TwosComplement of 31 in (5 Bits): "); KernelLog.Int(TwosComplement(31,5),0); KernelLog.Ln;
  1309. KernelLog.Ln;
  1310. test := ReadBitsBuffer(48,12, myBuf);
  1311. KernelLog.String("Reading 12 Bits at block 6 (starting at 48, 12 bits: "); KernelLog.Int(test,0); KernelLog.Ln;
  1312. KernelLog.String(" ?= 4095. Twos Complement should be -1: "); KernelLog.Int(TwosComplement(test,12),0); KernelLog.Ln;
  1313. test := ReadBitsBuffer(49,12, myBuf);
  1314. KernelLog.String("Reading 12 Bits at block 6 (starting at 49, 12 bits: "); KernelLog.Int(test,0); KernelLog.Ln;
  1315. KernelLog.String(" ?= 4095. Twos Complement should be -1: "); KernelLog.Int(TwosComplement(test,12),0); KernelLog.Ln;
  1316. END TestReader;
  1317. *)
  1318. (**
  1319. * returns the twos complement of a value by the predicted bitLen
  1320. * @param value: the value to convert
  1321. * @param bitLen: the bit length of the value it can have
  1322. * @return twos complement of value
  1323. *)
  1324. PROCEDURE TwosComplement(value: LONGINT; bitLen: LONGINT) : LONGINT;
  1325. VAR toMuch : LONGINT;
  1326. BEGIN
  1327. IF(bitLen<32) & (bitLen>0) THEN
  1328. IF ((bitLen-1) IN SYSTEM.VAL(SET,value)) THEN
  1329. toMuch:= SYSTEM.VAL(LONGINT,{bitLen});
  1330. value := value - toMuch;
  1331. END;
  1332. END;
  1333. RETURN value;
  1334. END TwosComplement;
  1335. (**
  1336. * Sets the device into normal mode (report protocol mode)
  1337. *)
  1338. PROCEDURE SetReportProtocol():BOOLEAN;
  1339. VAR
  1340. (*0: if boot protocol, 1: if report protocol*)
  1341. bootFlag: LONGINT;
  1342. BEGIN
  1343. IF (GetProtocol(bootFlag)) THEN
  1344. IF(bootFlag=0) THEN
  1345. IF Debug THEN
  1346. KernelLog.String("UsbHidDriver:HidDriver.Connect: GetProtocol returned boot protocol, set to report protocol"); KernelLog.Ln;
  1347. END;
  1348. IF(SetProtocol(1)=FALSE) THEN
  1349. KernelLog.String("UsbHidDriver:HidDriver.Connect: SetProtocol to report failed"); KernelLog.Ln;
  1350. RETURN FALSE;
  1351. END;
  1352. ELSE
  1353. IF Debug THEN
  1354. KernelLog.String("UsbHidDriver:HidDriver.Connect: GetProtocol returned report protocol"); KernelLog.Ln;
  1355. END;
  1356. END;
  1357. END;
  1358. RETURN TRUE;
  1359. END SetReportProtocol;
  1360. END HidDriver;
  1361. (*used for debug output. lists the report descriptor as described in Device Class Definition for Human Interface Devices,
  1362. f.e. page 61 Appendix A, B.2 Protocol 2 (Mouse)*)
  1363. PROCEDURE LayoutBuffer*(CONST buf : Usbdi.Buffer; len : LONGINT);
  1364. VAR temp : LONGINT;
  1365. BEGIN
  1366. KernelLog.String("Buffer Outline:"); KernelLog.Ln;
  1367. FOR temp := 0 TO len-1 DO
  1368. IF (temp MOD 2 = 0) THEN
  1369. KernelLog.Ln();
  1370. KernelLog.Int(temp, 4);
  1371. KernelLog.String(" ");
  1372. KernelLog.Hex(ORD(buf[temp]), -2);
  1373. ELSE
  1374. KernelLog.String(" ");
  1375. KernelLog.Hex(ORD(buf[temp]), -2);
  1376. END;
  1377. END;
  1378. KernelLog.Ln(); KernelLog.Ln();
  1379. END LayoutBuffer;
  1380. (*check, whether the device is a hid device
  1381. * return HidDriver, if hid device found, NIL otherwise
  1382. *)
  1383. PROCEDURE Probe(dev : Usbdi.UsbDevice; if : Usbdi.InterfaceDescriptor) : Usbdi.Driver;
  1384. VAR hidDriver : HidDriver;
  1385. BEGIN
  1386. IF if.bInterfaceClass # 3 THEN RETURN NIL END;
  1387. NEW(hidDriver);
  1388. RETURN hidDriver;
  1389. END Probe;
  1390. (* Called, when detaching the UsbHidDriver *)
  1391. PROCEDURE Cleanup;
  1392. BEGIN
  1393. Usbdi.drivers.Remove(Name);
  1394. END Cleanup;
  1395. PROCEDURE Install*;
  1396. END Install;
  1397. TYPE
  1398. DimensionGetter= PROCEDURE {DELEGATE} (VAR w,h: LONGINT);
  1399. VAR
  1400. getDisplayDimensions: DimensionGetter;
  1401. touchConfidenceDelay: LONGINT;
  1402. PROCEDURE DefaultDisplayDimensions(VAR w,h: LONGINT);
  1403. BEGIN
  1404. w := 1024; h := 768;
  1405. END DefaultDisplayDimensions;
  1406. PROCEDURE InstallDisplayDimensions*(poller: DimensionGetter); (* for touchscreen coordinate transformation*)
  1407. BEGIN
  1408. IF poller # NIL THEN
  1409. getDisplayDimensions := poller;
  1410. ELSE
  1411. getDisplayDimensions := DefaultDisplayDimensions;
  1412. END;
  1413. END InstallDisplayDimensions;
  1414. PROCEDURE Setup;
  1415. VAR s: ARRAY 32 OF CHAR; p: LONGINT;
  1416. BEGIN
  1417. getDisplayDimensions := DefaultDisplayDimensions;
  1418. Machine.GetConfig("TouchDelay", s);
  1419. IF s[0] # 0X THEN
  1420. p := 0; touchConfidenceDelay := Machine.StrToInt(p, s)
  1421. ELSE
  1422. touchConfidenceDelay := 0;
  1423. END;
  1424. Usbdi.drivers.Add(Probe, Name, Description, 10);
  1425. END Setup;
  1426. BEGIN
  1427. Modules.InstallTermHandler(Cleanup);
  1428. Setup;
  1429. END UsbHidDriver.
  1430. UsbHidDriver.Install ~ System.Free UsbHidDriver UsbHidParser UsbHidErrors UsbHidParserExt UsbHidReport UsbHidUP~