gtco.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /* -*- linux-c -*-
  2. GTCO digitizer USB driver
  3. TO CHECK: Is pressure done right on report 5?
  4. Copyright (C) 2006 GTCO CalComp
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; version 2
  8. of the License.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. Permission to use, copy, modify, distribute, and sell this software and its
  17. documentation for any purpose is hereby granted without fee, provided that
  18. the above copyright notice appear in all copies and that both that
  19. copyright notice and this permission notice appear in supporting
  20. documentation, and that the name of GTCO-CalComp not be used in advertising
  21. or publicity pertaining to distribution of the software without specific,
  22. written prior permission. GTCO-CalComp makes no representations about the
  23. suitability of this software for any purpose. It is provided "as is"
  24. without express or implied warranty.
  25. GTCO-CALCOMP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  26. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  27. EVENT SHALL GTCO-CALCOMP BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  28. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  29. DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  30. TORTIOUS ACTIONS, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  31. PERFORMANCE OF THIS SOFTWARE.
  32. GTCO CalComp, Inc.
  33. 7125 Riverwood Drive
  34. Columbia, MD 21046
  35. Jeremy Roberson jroberson@gtcocalcomp.com
  36. Scott Hill shill@gtcocalcomp.com
  37. */
  38. /*#define DEBUG*/
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/slab.h>
  43. #include <linux/input.h>
  44. #include <linux/usb.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/unaligned.h>
  47. #include <asm/byteorder.h>
  48. #include <linux/bitops.h>
  49. #include <linux/usb/input.h>
  50. /* Version with a Major number of 2 is for kernel inclusion only. */
  51. #define GTCO_VERSION "2.00.0006"
  52. /* MACROS */
  53. #define VENDOR_ID_GTCO 0x078C
  54. #define PID_400 0x400
  55. #define PID_401 0x401
  56. #define PID_1000 0x1000
  57. #define PID_1001 0x1001
  58. #define PID_1002 0x1002
  59. /* Max size of a single report */
  60. #define REPORT_MAX_SIZE 10
  61. /* Bitmask whether pen is in range */
  62. #define MASK_INRANGE 0x20
  63. #define MASK_BUTTON 0x01F
  64. #define PATHLENGTH 64
  65. /* DATA STRUCTURES */
  66. /* Device table */
  67. static const struct usb_device_id gtco_usbid_table[] = {
  68. { USB_DEVICE(VENDOR_ID_GTCO, PID_400) },
  69. { USB_DEVICE(VENDOR_ID_GTCO, PID_401) },
  70. { USB_DEVICE(VENDOR_ID_GTCO, PID_1000) },
  71. { USB_DEVICE(VENDOR_ID_GTCO, PID_1001) },
  72. { USB_DEVICE(VENDOR_ID_GTCO, PID_1002) },
  73. { }
  74. };
  75. MODULE_DEVICE_TABLE (usb, gtco_usbid_table);
  76. /* Structure to hold all of our device specific stuff */
  77. struct gtco {
  78. struct input_dev *inputdevice; /* input device struct pointer */
  79. struct usb_interface *intf; /* the usb interface for this device */
  80. struct urb *urbinfo; /* urb for incoming reports */
  81. dma_addr_t buf_dma; /* dma addr of the data buffer*/
  82. unsigned char * buffer; /* databuffer for reports */
  83. char usbpath[PATHLENGTH];
  84. int openCount;
  85. /* Information pulled from Report Descriptor */
  86. u32 usage;
  87. u32 min_X;
  88. u32 max_X;
  89. u32 min_Y;
  90. u32 max_Y;
  91. s8 mintilt_X;
  92. s8 maxtilt_X;
  93. s8 mintilt_Y;
  94. s8 maxtilt_Y;
  95. u32 maxpressure;
  96. u32 minpressure;
  97. };
  98. /* Code for parsing the HID REPORT DESCRIPTOR */
  99. /* From HID1.11 spec */
  100. struct hid_descriptor
  101. {
  102. struct usb_descriptor_header header;
  103. __le16 bcdHID;
  104. u8 bCountryCode;
  105. u8 bNumDescriptors;
  106. u8 bDescriptorType;
  107. __le16 wDescriptorLength;
  108. } __attribute__ ((packed));
  109. #define HID_DESCRIPTOR_SIZE 9
  110. #define HID_DEVICE_TYPE 33
  111. #define REPORT_DEVICE_TYPE 34
  112. #define PREF_TAG(x) ((x)>>4)
  113. #define PREF_TYPE(x) ((x>>2)&0x03)
  114. #define PREF_SIZE(x) ((x)&0x03)
  115. #define TYPE_MAIN 0
  116. #define TYPE_GLOBAL 1
  117. #define TYPE_LOCAL 2
  118. #define TYPE_RESERVED 3
  119. #define TAG_MAIN_INPUT 0x8
  120. #define TAG_MAIN_OUTPUT 0x9
  121. #define TAG_MAIN_FEATURE 0xB
  122. #define TAG_MAIN_COL_START 0xA
  123. #define TAG_MAIN_COL_END 0xC
  124. #define TAG_GLOB_USAGE 0
  125. #define TAG_GLOB_LOG_MIN 1
  126. #define TAG_GLOB_LOG_MAX 2
  127. #define TAG_GLOB_PHYS_MIN 3
  128. #define TAG_GLOB_PHYS_MAX 4
  129. #define TAG_GLOB_UNIT_EXP 5
  130. #define TAG_GLOB_UNIT 6
  131. #define TAG_GLOB_REPORT_SZ 7
  132. #define TAG_GLOB_REPORT_ID 8
  133. #define TAG_GLOB_REPORT_CNT 9
  134. #define TAG_GLOB_PUSH 10
  135. #define TAG_GLOB_POP 11
  136. #define TAG_GLOB_MAX 12
  137. #define DIGITIZER_USAGE_TIP_PRESSURE 0x30
  138. #define DIGITIZER_USAGE_TILT_X 0x3D
  139. #define DIGITIZER_USAGE_TILT_Y 0x3E
  140. /*
  141. * This is an abbreviated parser for the HID Report Descriptor. We
  142. * know what devices we are talking to, so this is by no means meant
  143. * to be generic. We can make some safe assumptions:
  144. *
  145. * - We know there are no LONG tags, all short
  146. * - We know that we have no MAIN Feature and MAIN Output items
  147. * - We know what the IRQ reports are supposed to look like.
  148. *
  149. * The main purpose of this is to use the HID report desc to figure
  150. * out the mins and maxs of the fields in the IRQ reports. The IRQ
  151. * reports for 400/401 change slightly if the max X is bigger than 64K.
  152. *
  153. */
  154. static void parse_hid_report_descriptor(struct gtco *device, char * report,
  155. int length)
  156. {
  157. struct device *ddev = &device->intf->dev;
  158. int x, i = 0;
  159. /* Tag primitive vars */
  160. __u8 prefix;
  161. __u8 size;
  162. __u8 tag;
  163. __u8 type;
  164. __u8 data = 0;
  165. __u16 data16 = 0;
  166. __u32 data32 = 0;
  167. /* For parsing logic */
  168. int inputnum = 0;
  169. __u32 usage = 0;
  170. /* Global Values, indexed by TAG */
  171. __u32 globalval[TAG_GLOB_MAX];
  172. __u32 oldval[TAG_GLOB_MAX];
  173. /* Debug stuff */
  174. char maintype = 'x';
  175. char globtype[12];
  176. int indent = 0;
  177. char indentstr[10] = "";
  178. dev_dbg(ddev, "======>>>>>>PARSE<<<<<<======\n");
  179. /* Walk this report and pull out the info we need */
  180. while (i < length) {
  181. prefix = report[i];
  182. /* Skip over prefix */
  183. i++;
  184. /* Determine data size and save the data in the proper variable */
  185. size = PREF_SIZE(prefix);
  186. switch (size) {
  187. case 1:
  188. data = report[i];
  189. break;
  190. case 2:
  191. data16 = get_unaligned_le16(&report[i]);
  192. break;
  193. case 3:
  194. size = 4;
  195. data32 = get_unaligned_le32(&report[i]);
  196. break;
  197. }
  198. /* Skip size of data */
  199. i += size;
  200. /* What we do depends on the tag type */
  201. tag = PREF_TAG(prefix);
  202. type = PREF_TYPE(prefix);
  203. switch (type) {
  204. case TYPE_MAIN:
  205. strcpy(globtype, "");
  206. switch (tag) {
  207. case TAG_MAIN_INPUT:
  208. /*
  209. * The INPUT MAIN tag signifies this is
  210. * information from a report. We need to
  211. * figure out what it is and store the
  212. * min/max values
  213. */
  214. maintype = 'I';
  215. if (data == 2)
  216. strcpy(globtype, "Variable");
  217. else if (data == 3)
  218. strcpy(globtype, "Var|Const");
  219. dev_dbg(ddev, "::::: Saving Report: %d input #%d Max: 0x%X(%d) Min:0x%X(%d) of %d bits\n",
  220. globalval[TAG_GLOB_REPORT_ID], inputnum,
  221. globalval[TAG_GLOB_LOG_MAX], globalval[TAG_GLOB_LOG_MAX],
  222. globalval[TAG_GLOB_LOG_MIN], globalval[TAG_GLOB_LOG_MIN],
  223. globalval[TAG_GLOB_REPORT_SZ] * globalval[TAG_GLOB_REPORT_CNT]);
  224. /*
  225. We can assume that the first two input items
  226. are always the X and Y coordinates. After
  227. that, we look for everything else by
  228. local usage value
  229. */
  230. switch (inputnum) {
  231. case 0: /* X coord */
  232. dev_dbg(ddev, "GER: X Usage: 0x%x\n", usage);
  233. if (device->max_X == 0) {
  234. device->max_X = globalval[TAG_GLOB_LOG_MAX];
  235. device->min_X = globalval[TAG_GLOB_LOG_MIN];
  236. }
  237. break;
  238. case 1: /* Y coord */
  239. dev_dbg(ddev, "GER: Y Usage: 0x%x\n", usage);
  240. if (device->max_Y == 0) {
  241. device->max_Y = globalval[TAG_GLOB_LOG_MAX];
  242. device->min_Y = globalval[TAG_GLOB_LOG_MIN];
  243. }
  244. break;
  245. default:
  246. /* Tilt X */
  247. if (usage == DIGITIZER_USAGE_TILT_X) {
  248. if (device->maxtilt_X == 0) {
  249. device->maxtilt_X = globalval[TAG_GLOB_LOG_MAX];
  250. device->mintilt_X = globalval[TAG_GLOB_LOG_MIN];
  251. }
  252. }
  253. /* Tilt Y */
  254. if (usage == DIGITIZER_USAGE_TILT_Y) {
  255. if (device->maxtilt_Y == 0) {
  256. device->maxtilt_Y = globalval[TAG_GLOB_LOG_MAX];
  257. device->mintilt_Y = globalval[TAG_GLOB_LOG_MIN];
  258. }
  259. }
  260. /* Pressure */
  261. if (usage == DIGITIZER_USAGE_TIP_PRESSURE) {
  262. if (device->maxpressure == 0) {
  263. device->maxpressure = globalval[TAG_GLOB_LOG_MAX];
  264. device->minpressure = globalval[TAG_GLOB_LOG_MIN];
  265. }
  266. }
  267. break;
  268. }
  269. inputnum++;
  270. break;
  271. case TAG_MAIN_OUTPUT:
  272. maintype = 'O';
  273. break;
  274. case TAG_MAIN_FEATURE:
  275. maintype = 'F';
  276. break;
  277. case TAG_MAIN_COL_START:
  278. maintype = 'S';
  279. if (data == 0) {
  280. dev_dbg(ddev, "======>>>>>> Physical\n");
  281. strcpy(globtype, "Physical");
  282. } else
  283. dev_dbg(ddev, "======>>>>>>\n");
  284. /* Indent the debug output */
  285. indent++;
  286. for (x = 0; x < indent; x++)
  287. indentstr[x] = '-';
  288. indentstr[x] = 0;
  289. /* Save global tags */
  290. for (x = 0; x < TAG_GLOB_MAX; x++)
  291. oldval[x] = globalval[x];
  292. break;
  293. case TAG_MAIN_COL_END:
  294. dev_dbg(ddev, "<<<<<<======\n");
  295. maintype = 'E';
  296. indent--;
  297. for (x = 0; x < indent; x++)
  298. indentstr[x] = '-';
  299. indentstr[x] = 0;
  300. /* Copy global tags back */
  301. for (x = 0; x < TAG_GLOB_MAX; x++)
  302. globalval[x] = oldval[x];
  303. break;
  304. }
  305. switch (size) {
  306. case 1:
  307. dev_dbg(ddev, "%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x\n",
  308. indentstr, tag, maintype, size, globtype, data);
  309. break;
  310. case 2:
  311. dev_dbg(ddev, "%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x\n",
  312. indentstr, tag, maintype, size, globtype, data16);
  313. break;
  314. case 4:
  315. dev_dbg(ddev, "%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x\n",
  316. indentstr, tag, maintype, size, globtype, data32);
  317. break;
  318. }
  319. break;
  320. case TYPE_GLOBAL:
  321. switch (tag) {
  322. case TAG_GLOB_USAGE:
  323. /*
  324. * First time we hit the global usage tag,
  325. * it should tell us the type of device
  326. */
  327. if (device->usage == 0)
  328. device->usage = data;
  329. strcpy(globtype, "USAGE");
  330. break;
  331. case TAG_GLOB_LOG_MIN:
  332. strcpy(globtype, "LOG_MIN");
  333. break;
  334. case TAG_GLOB_LOG_MAX:
  335. strcpy(globtype, "LOG_MAX");
  336. break;
  337. case TAG_GLOB_PHYS_MIN:
  338. strcpy(globtype, "PHYS_MIN");
  339. break;
  340. case TAG_GLOB_PHYS_MAX:
  341. strcpy(globtype, "PHYS_MAX");
  342. break;
  343. case TAG_GLOB_UNIT_EXP:
  344. strcpy(globtype, "EXP");
  345. break;
  346. case TAG_GLOB_UNIT:
  347. strcpy(globtype, "UNIT");
  348. break;
  349. case TAG_GLOB_REPORT_SZ:
  350. strcpy(globtype, "REPORT_SZ");
  351. break;
  352. case TAG_GLOB_REPORT_ID:
  353. strcpy(globtype, "REPORT_ID");
  354. /* New report, restart numbering */
  355. inputnum = 0;
  356. break;
  357. case TAG_GLOB_REPORT_CNT:
  358. strcpy(globtype, "REPORT_CNT");
  359. break;
  360. case TAG_GLOB_PUSH:
  361. strcpy(globtype, "PUSH");
  362. break;
  363. case TAG_GLOB_POP:
  364. strcpy(globtype, "POP");
  365. break;
  366. }
  367. /* Check to make sure we have a good tag number
  368. so we don't overflow array */
  369. if (tag < TAG_GLOB_MAX) {
  370. switch (size) {
  371. case 1:
  372. dev_dbg(ddev, "%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x\n",
  373. indentstr, globtype, tag, size, data);
  374. globalval[tag] = data;
  375. break;
  376. case 2:
  377. dev_dbg(ddev, "%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x\n",
  378. indentstr, globtype, tag, size, data16);
  379. globalval[tag] = data16;
  380. break;
  381. case 4:
  382. dev_dbg(ddev, "%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x\n",
  383. indentstr, globtype, tag, size, data32);
  384. globalval[tag] = data32;
  385. break;
  386. }
  387. } else {
  388. dev_dbg(ddev, "%sGLOBALTAG: ILLEGAL TAG:%d SIZE: %d\n",
  389. indentstr, tag, size);
  390. }
  391. break;
  392. case TYPE_LOCAL:
  393. switch (tag) {
  394. case TAG_GLOB_USAGE:
  395. strcpy(globtype, "USAGE");
  396. /* Always 1 byte */
  397. usage = data;
  398. break;
  399. case TAG_GLOB_LOG_MIN:
  400. strcpy(globtype, "MIN");
  401. break;
  402. case TAG_GLOB_LOG_MAX:
  403. strcpy(globtype, "MAX");
  404. break;
  405. default:
  406. strcpy(globtype, "UNKNOWN");
  407. break;
  408. }
  409. switch (size) {
  410. case 1:
  411. dev_dbg(ddev, "%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x\n",
  412. indentstr, tag, globtype, size, data);
  413. break;
  414. case 2:
  415. dev_dbg(ddev, "%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x\n",
  416. indentstr, tag, globtype, size, data16);
  417. break;
  418. case 4:
  419. dev_dbg(ddev, "%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x\n",
  420. indentstr, tag, globtype, size, data32);
  421. break;
  422. }
  423. break;
  424. }
  425. }
  426. }
  427. /* INPUT DRIVER Routines */
  428. /*
  429. * Called when opening the input device. This will submit the URB to
  430. * the usb system so we start getting reports
  431. */
  432. static int gtco_input_open(struct input_dev *inputdev)
  433. {
  434. struct gtco *device = input_get_drvdata(inputdev);
  435. device->urbinfo->dev = interface_to_usbdev(device->intf);
  436. if (usb_submit_urb(device->urbinfo, GFP_KERNEL))
  437. return -EIO;
  438. return 0;
  439. }
  440. /*
  441. * Called when closing the input device. This will unlink the URB
  442. */
  443. static void gtco_input_close(struct input_dev *inputdev)
  444. {
  445. struct gtco *device = input_get_drvdata(inputdev);
  446. usb_kill_urb(device->urbinfo);
  447. }
  448. /*
  449. * Setup input device capabilities. Tell the input system what this
  450. * device is capable of generating.
  451. *
  452. * This information is based on what is read from the HID report and
  453. * placed in the struct gtco structure
  454. *
  455. */
  456. static void gtco_setup_caps(struct input_dev *inputdev)
  457. {
  458. struct gtco *device = input_get_drvdata(inputdev);
  459. /* Which events */
  460. inputdev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) |
  461. BIT_MASK(EV_MSC);
  462. /* Misc event menu block */
  463. inputdev->mscbit[0] = BIT_MASK(MSC_SCAN) | BIT_MASK(MSC_SERIAL) |
  464. BIT_MASK(MSC_RAW);
  465. /* Absolute values based on HID report info */
  466. input_set_abs_params(inputdev, ABS_X, device->min_X, device->max_X,
  467. 0, 0);
  468. input_set_abs_params(inputdev, ABS_Y, device->min_Y, device->max_Y,
  469. 0, 0);
  470. /* Proximity */
  471. input_set_abs_params(inputdev, ABS_DISTANCE, 0, 1, 0, 0);
  472. /* Tilt & pressure */
  473. input_set_abs_params(inputdev, ABS_TILT_X, device->mintilt_X,
  474. device->maxtilt_X, 0, 0);
  475. input_set_abs_params(inputdev, ABS_TILT_Y, device->mintilt_Y,
  476. device->maxtilt_Y, 0, 0);
  477. input_set_abs_params(inputdev, ABS_PRESSURE, device->minpressure,
  478. device->maxpressure, 0, 0);
  479. /* Transducer */
  480. input_set_abs_params(inputdev, ABS_MISC, 0, 0xFF, 0, 0);
  481. }
  482. /* USB Routines */
  483. /*
  484. * URB callback routine. Called when we get IRQ reports from the
  485. * digitizer.
  486. *
  487. * This bridges the USB and input device worlds. It generates events
  488. * on the input device based on the USB reports.
  489. */
  490. static void gtco_urb_callback(struct urb *urbinfo)
  491. {
  492. struct gtco *device = urbinfo->context;
  493. struct input_dev *inputdev;
  494. int rc;
  495. u32 val = 0;
  496. char le_buffer[2];
  497. inputdev = device->inputdevice;
  498. /* Was callback OK? */
  499. if (urbinfo->status == -ECONNRESET ||
  500. urbinfo->status == -ENOENT ||
  501. urbinfo->status == -ESHUTDOWN) {
  502. /* Shutdown is occurring. Return and don't queue up any more */
  503. return;
  504. }
  505. if (urbinfo->status != 0) {
  506. /*
  507. * Some unknown error. Hopefully temporary. Just go and
  508. * requeue an URB
  509. */
  510. goto resubmit;
  511. }
  512. /*
  513. * Good URB, now process
  514. */
  515. /* PID dependent when we interpret the report */
  516. if (inputdev->id.product == PID_1000 ||
  517. inputdev->id.product == PID_1001 ||
  518. inputdev->id.product == PID_1002) {
  519. /*
  520. * Switch on the report ID
  521. * Conveniently, the reports have more information, the higher
  522. * the report number. We can just fall through the case
  523. * statements if we start with the highest number report
  524. */
  525. switch (device->buffer[0]) {
  526. case 5:
  527. /* Pressure is 9 bits */
  528. val = ((u16)(device->buffer[8]) << 1);
  529. val |= (u16)(device->buffer[7] >> 7);
  530. input_report_abs(inputdev, ABS_PRESSURE,
  531. device->buffer[8]);
  532. /* Mask out the Y tilt value used for pressure */
  533. device->buffer[7] = (u8)((device->buffer[7]) & 0x7F);
  534. /* Fall thru */
  535. case 4:
  536. /* Tilt */
  537. input_report_abs(inputdev, ABS_TILT_X,
  538. sign_extend32(device->buffer[6], 6));
  539. input_report_abs(inputdev, ABS_TILT_Y,
  540. sign_extend32(device->buffer[7], 6));
  541. /* Fall thru */
  542. case 2:
  543. case 3:
  544. /* Convert buttons, only 5 bits possible */
  545. val = (device->buffer[5]) & MASK_BUTTON;
  546. /* We don't apply any meaning to the bitmask,
  547. just report */
  548. input_event(inputdev, EV_MSC, MSC_SERIAL, val);
  549. /* Fall thru */
  550. case 1:
  551. /* All reports have X and Y coords in the same place */
  552. val = get_unaligned_le16(&device->buffer[1]);
  553. input_report_abs(inputdev, ABS_X, val);
  554. val = get_unaligned_le16(&device->buffer[3]);
  555. input_report_abs(inputdev, ABS_Y, val);
  556. /* Ditto for proximity bit */
  557. val = device->buffer[5] & MASK_INRANGE ? 1 : 0;
  558. input_report_abs(inputdev, ABS_DISTANCE, val);
  559. /* Report 1 is an exception to how we handle buttons */
  560. /* Buttons are an index, not a bitmask */
  561. if (device->buffer[0] == 1) {
  562. /*
  563. * Convert buttons, 5 bit index
  564. * Report value of index set as one,
  565. * the rest as 0
  566. */
  567. val = device->buffer[5] & MASK_BUTTON;
  568. dev_dbg(&device->intf->dev,
  569. "======>>>>>>REPORT 1: val 0x%X(%d)\n",
  570. val, val);
  571. /*
  572. * We don't apply any meaning to the button
  573. * index, just report it
  574. */
  575. input_event(inputdev, EV_MSC, MSC_SERIAL, val);
  576. }
  577. break;
  578. case 7:
  579. /* Menu blocks */
  580. input_event(inputdev, EV_MSC, MSC_SCAN,
  581. device->buffer[1]);
  582. break;
  583. }
  584. }
  585. /* Other pid class */
  586. if (inputdev->id.product == PID_400 ||
  587. inputdev->id.product == PID_401) {
  588. /* Report 2 */
  589. if (device->buffer[0] == 2) {
  590. /* Menu blocks */
  591. input_event(inputdev, EV_MSC, MSC_SCAN, device->buffer[1]);
  592. }
  593. /* Report 1 */
  594. if (device->buffer[0] == 1) {
  595. char buttonbyte;
  596. /* IF X max > 64K, we still a bit from the y report */
  597. if (device->max_X > 0x10000) {
  598. val = (u16)(((u16)(device->buffer[2] << 8)) | (u8)device->buffer[1]);
  599. val |= (u32)(((u8)device->buffer[3] & 0x1) << 16);
  600. input_report_abs(inputdev, ABS_X, val);
  601. le_buffer[0] = (u8)((u8)(device->buffer[3]) >> 1);
  602. le_buffer[0] |= (u8)((device->buffer[3] & 0x1) << 7);
  603. le_buffer[1] = (u8)(device->buffer[4] >> 1);
  604. le_buffer[1] |= (u8)((device->buffer[5] & 0x1) << 7);
  605. val = get_unaligned_le16(le_buffer);
  606. input_report_abs(inputdev, ABS_Y, val);
  607. /*
  608. * Shift the button byte right by one to
  609. * make it look like the standard report
  610. */
  611. buttonbyte = device->buffer[5] >> 1;
  612. } else {
  613. val = get_unaligned_le16(&device->buffer[1]);
  614. input_report_abs(inputdev, ABS_X, val);
  615. val = get_unaligned_le16(&device->buffer[3]);
  616. input_report_abs(inputdev, ABS_Y, val);
  617. buttonbyte = device->buffer[5];
  618. }
  619. /* BUTTONS and PROXIMITY */
  620. val = buttonbyte & MASK_INRANGE ? 1 : 0;
  621. input_report_abs(inputdev, ABS_DISTANCE, val);
  622. /* Convert buttons, only 4 bits possible */
  623. val = buttonbyte & 0x0F;
  624. #ifdef USE_BUTTONS
  625. for (i = 0; i < 5; i++)
  626. input_report_key(inputdev, BTN_DIGI + i, val & (1 << i));
  627. #else
  628. /* We don't apply any meaning to the bitmask, just report */
  629. input_event(inputdev, EV_MSC, MSC_SERIAL, val);
  630. #endif
  631. /* TRANSDUCER */
  632. input_report_abs(inputdev, ABS_MISC, device->buffer[6]);
  633. }
  634. }
  635. /* Everybody gets report ID's */
  636. input_event(inputdev, EV_MSC, MSC_RAW, device->buffer[0]);
  637. /* Sync it up */
  638. input_sync(inputdev);
  639. resubmit:
  640. rc = usb_submit_urb(urbinfo, GFP_ATOMIC);
  641. if (rc != 0)
  642. dev_err(&device->intf->dev,
  643. "usb_submit_urb failed rc=0x%x\n", rc);
  644. }
  645. /*
  646. * The probe routine. This is called when the kernel find the matching USB
  647. * vendor/product. We do the following:
  648. *
  649. * - Allocate mem for a local structure to manage the device
  650. * - Request a HID Report Descriptor from the device and parse it to
  651. * find out the device parameters
  652. * - Create an input device and assign it attributes
  653. * - Allocate an URB so the device can talk to us when the input
  654. * queue is open
  655. */
  656. static int gtco_probe(struct usb_interface *usbinterface,
  657. const struct usb_device_id *id)
  658. {
  659. struct gtco *gtco;
  660. struct input_dev *input_dev;
  661. struct hid_descriptor *hid_desc;
  662. char *report;
  663. int result = 0, retry;
  664. int error;
  665. struct usb_endpoint_descriptor *endpoint;
  666. struct usb_device *udev = interface_to_usbdev(usbinterface);
  667. /* Allocate memory for device structure */
  668. gtco = kzalloc(sizeof(struct gtco), GFP_KERNEL);
  669. input_dev = input_allocate_device();
  670. if (!gtco || !input_dev) {
  671. dev_err(&usbinterface->dev, "No more memory\n");
  672. error = -ENOMEM;
  673. goto err_free_devs;
  674. }
  675. /* Set pointer to the input device */
  676. gtco->inputdevice = input_dev;
  677. /* Save interface information */
  678. gtco->intf = usbinterface;
  679. /* Allocate some data for incoming reports */
  680. gtco->buffer = usb_alloc_coherent(udev, REPORT_MAX_SIZE,
  681. GFP_KERNEL, &gtco->buf_dma);
  682. if (!gtco->buffer) {
  683. dev_err(&usbinterface->dev, "No more memory for us buffers\n");
  684. error = -ENOMEM;
  685. goto err_free_devs;
  686. }
  687. /* Allocate URB for reports */
  688. gtco->urbinfo = usb_alloc_urb(0, GFP_KERNEL);
  689. if (!gtco->urbinfo) {
  690. dev_err(&usbinterface->dev, "Failed to allocate URB\n");
  691. error = -ENOMEM;
  692. goto err_free_buf;
  693. }
  694. /* Sanity check that a device has an endpoint */
  695. if (usbinterface->altsetting[0].desc.bNumEndpoints < 1) {
  696. dev_err(&usbinterface->dev,
  697. "Invalid number of endpoints\n");
  698. error = -EINVAL;
  699. goto err_free_urb;
  700. }
  701. /*
  702. * The endpoint is always altsetting 0, we know this since we know
  703. * this device only has one interrupt endpoint
  704. */
  705. endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
  706. /* Some debug */
  707. dev_dbg(&usbinterface->dev, "gtco # interfaces: %d\n", usbinterface->num_altsetting);
  708. dev_dbg(&usbinterface->dev, "num endpoints: %d\n", usbinterface->cur_altsetting->desc.bNumEndpoints);
  709. dev_dbg(&usbinterface->dev, "interface class: %d\n", usbinterface->cur_altsetting->desc.bInterfaceClass);
  710. dev_dbg(&usbinterface->dev, "endpoint: attribute:0x%x type:0x%x\n", endpoint->bmAttributes, endpoint->bDescriptorType);
  711. if (usb_endpoint_xfer_int(endpoint))
  712. dev_dbg(&usbinterface->dev, "endpoint: we have interrupt endpoint\n");
  713. dev_dbg(&usbinterface->dev, "endpoint extra len:%d\n", usbinterface->altsetting[0].extralen);
  714. /*
  715. * Find the HID descriptor so we can find out the size of the
  716. * HID report descriptor
  717. */
  718. if (usb_get_extra_descriptor(usbinterface->cur_altsetting,
  719. HID_DEVICE_TYPE, &hid_desc) != 0) {
  720. dev_err(&usbinterface->dev,
  721. "Can't retrieve exta USB descriptor to get hid report descriptor length\n");
  722. error = -EIO;
  723. goto err_free_urb;
  724. }
  725. dev_dbg(&usbinterface->dev,
  726. "Extra descriptor success: type:%d len:%d\n",
  727. hid_desc->bDescriptorType, hid_desc->wDescriptorLength);
  728. report = kzalloc(le16_to_cpu(hid_desc->wDescriptorLength), GFP_KERNEL);
  729. if (!report) {
  730. dev_err(&usbinterface->dev, "No more memory for report\n");
  731. error = -ENOMEM;
  732. goto err_free_urb;
  733. }
  734. /* Couple of tries to get reply */
  735. for (retry = 0; retry < 3; retry++) {
  736. result = usb_control_msg(udev,
  737. usb_rcvctrlpipe(udev, 0),
  738. USB_REQ_GET_DESCRIPTOR,
  739. USB_RECIP_INTERFACE | USB_DIR_IN,
  740. REPORT_DEVICE_TYPE << 8,
  741. 0, /* interface */
  742. report,
  743. le16_to_cpu(hid_desc->wDescriptorLength),
  744. 5000); /* 5 secs */
  745. dev_dbg(&usbinterface->dev, "usb_control_msg result: %d\n", result);
  746. if (result == le16_to_cpu(hid_desc->wDescriptorLength)) {
  747. parse_hid_report_descriptor(gtco, report, result);
  748. break;
  749. }
  750. }
  751. kfree(report);
  752. /* If we didn't get the report, fail */
  753. if (result != le16_to_cpu(hid_desc->wDescriptorLength)) {
  754. dev_err(&usbinterface->dev,
  755. "Failed to get HID Report Descriptor of size: %d\n",
  756. hid_desc->wDescriptorLength);
  757. error = -EIO;
  758. goto err_free_urb;
  759. }
  760. /* Create a device file node */
  761. usb_make_path(udev, gtco->usbpath, sizeof(gtco->usbpath));
  762. strlcat(gtco->usbpath, "/input0", sizeof(gtco->usbpath));
  763. /* Set Input device functions */
  764. input_dev->open = gtco_input_open;
  765. input_dev->close = gtco_input_close;
  766. /* Set input device information */
  767. input_dev->name = "GTCO_CalComp";
  768. input_dev->phys = gtco->usbpath;
  769. input_set_drvdata(input_dev, gtco);
  770. /* Now set up all the input device capabilities */
  771. gtco_setup_caps(input_dev);
  772. /* Set input device required ID information */
  773. usb_to_input_id(udev, &input_dev->id);
  774. input_dev->dev.parent = &usbinterface->dev;
  775. /* Setup the URB, it will be posted later on open of input device */
  776. endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
  777. usb_fill_int_urb(gtco->urbinfo,
  778. udev,
  779. usb_rcvintpipe(udev,
  780. endpoint->bEndpointAddress),
  781. gtco->buffer,
  782. REPORT_MAX_SIZE,
  783. gtco_urb_callback,
  784. gtco,
  785. endpoint->bInterval);
  786. gtco->urbinfo->transfer_dma = gtco->buf_dma;
  787. gtco->urbinfo->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  788. /* Save gtco pointer in USB interface gtco */
  789. usb_set_intfdata(usbinterface, gtco);
  790. /* All done, now register the input device */
  791. error = input_register_device(input_dev);
  792. if (error)
  793. goto err_free_urb;
  794. return 0;
  795. err_free_urb:
  796. usb_free_urb(gtco->urbinfo);
  797. err_free_buf:
  798. usb_free_coherent(udev, REPORT_MAX_SIZE,
  799. gtco->buffer, gtco->buf_dma);
  800. err_free_devs:
  801. input_free_device(input_dev);
  802. kfree(gtco);
  803. return error;
  804. }
  805. /*
  806. * This function is a standard USB function called when the USB device
  807. * is disconnected. We will get rid of the URV, de-register the input
  808. * device, and free up allocated memory
  809. */
  810. static void gtco_disconnect(struct usb_interface *interface)
  811. {
  812. /* Grab private device ptr */
  813. struct gtco *gtco = usb_get_intfdata(interface);
  814. struct usb_device *udev = interface_to_usbdev(interface);
  815. /* Now reverse all the registration stuff */
  816. if (gtco) {
  817. input_unregister_device(gtco->inputdevice);
  818. usb_kill_urb(gtco->urbinfo);
  819. usb_free_urb(gtco->urbinfo);
  820. usb_free_coherent(udev, REPORT_MAX_SIZE,
  821. gtco->buffer, gtco->buf_dma);
  822. kfree(gtco);
  823. }
  824. dev_info(&interface->dev, "gtco driver disconnected\n");
  825. }
  826. /* STANDARD MODULE LOAD ROUTINES */
  827. static struct usb_driver gtco_driverinfo_table = {
  828. .name = "gtco",
  829. .id_table = gtco_usbid_table,
  830. .probe = gtco_probe,
  831. .disconnect = gtco_disconnect,
  832. };
  833. module_usb_driver(gtco_driverinfo_table);
  834. MODULE_DESCRIPTION("GTCO digitizer USB driver");
  835. MODULE_LICENSE("GPL");