gtco.c 27 KB

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