gtco.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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_device *usbdev; /* the usb device for this device */
  80. struct usb_interface *intf; /* the usb interface for this device */
  81. struct urb *urbinfo; /* urb for incoming reports */
  82. dma_addr_t buf_dma; /* dma addr of the data buffer*/
  83. unsigned char * buffer; /* databuffer for reports */
  84. char usbpath[PATHLENGTH];
  85. int openCount;
  86. /* Information pulled from Report Descriptor */
  87. u32 usage;
  88. u32 min_X;
  89. u32 max_X;
  90. u32 min_Y;
  91. u32 max_Y;
  92. s8 mintilt_X;
  93. s8 maxtilt_X;
  94. s8 mintilt_Y;
  95. s8 maxtilt_Y;
  96. u32 maxpressure;
  97. u32 minpressure;
  98. };
  99. /* Code for parsing the HID REPORT DESCRIPTOR */
  100. /* From HID1.11 spec */
  101. struct hid_descriptor
  102. {
  103. struct usb_descriptor_header header;
  104. __le16 bcdHID;
  105. u8 bCountryCode;
  106. u8 bNumDescriptors;
  107. u8 bDescriptorType;
  108. __le16 wDescriptorLength;
  109. } __attribute__ ((packed));
  110. #define HID_DESCRIPTOR_SIZE 9
  111. #define HID_DEVICE_TYPE 33
  112. #define REPORT_DEVICE_TYPE 34
  113. #define PREF_TAG(x) ((x)>>4)
  114. #define PREF_TYPE(x) ((x>>2)&0x03)
  115. #define PREF_SIZE(x) ((x)&0x03)
  116. #define TYPE_MAIN 0
  117. #define TYPE_GLOBAL 1
  118. #define TYPE_LOCAL 2
  119. #define TYPE_RESERVED 3
  120. #define TAG_MAIN_INPUT 0x8
  121. #define TAG_MAIN_OUTPUT 0x9
  122. #define TAG_MAIN_FEATURE 0xB
  123. #define TAG_MAIN_COL_START 0xA
  124. #define TAG_MAIN_COL_END 0xC
  125. #define TAG_GLOB_USAGE 0
  126. #define TAG_GLOB_LOG_MIN 1
  127. #define TAG_GLOB_LOG_MAX 2
  128. #define TAG_GLOB_PHYS_MIN 3
  129. #define TAG_GLOB_PHYS_MAX 4
  130. #define TAG_GLOB_UNIT_EXP 5
  131. #define TAG_GLOB_UNIT 6
  132. #define TAG_GLOB_REPORT_SZ 7
  133. #define TAG_GLOB_REPORT_ID 8
  134. #define TAG_GLOB_REPORT_CNT 9
  135. #define TAG_GLOB_PUSH 10
  136. #define TAG_GLOB_POP 11
  137. #define TAG_GLOB_MAX 12
  138. #define DIGITIZER_USAGE_TIP_PRESSURE 0x30
  139. #define DIGITIZER_USAGE_TILT_X 0x3D
  140. #define DIGITIZER_USAGE_TILT_Y 0x3E
  141. /*
  142. * This is an abbreviated parser for the HID Report Descriptor. We
  143. * know what devices we are talking to, so this is by no means meant
  144. * to be generic. We can make some safe assumptions:
  145. *
  146. * - We know there are no LONG tags, all short
  147. * - We know that we have no MAIN Feature and MAIN Output items
  148. * - We know what the IRQ reports are supposed to look like.
  149. *
  150. * The main purpose of this is to use the HID report desc to figure
  151. * out the mins and maxs of the fields in the IRQ reports. The IRQ
  152. * reports for 400/401 change slightly if the max X is bigger than 64K.
  153. *
  154. */
  155. static void parse_hid_report_descriptor(struct gtco *device, char * report,
  156. int length)
  157. {
  158. struct device *ddev = &device->intf->dev;
  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[10] = "";
  179. dev_dbg(ddev, "======>>>>>>PARSE<<<<<<======\n");
  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(ddev,
  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. dev_dbg(ddev, "::::: Saving Report: %d input #%d Max: 0x%X(%d) Min:0x%X(%d) of %d bits\n",
  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. dev_dbg(ddev, "GER: X Usage: 0x%x\n", 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. dev_dbg(ddev, "GER: Y Usage: 0x%x\n", 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 (data == 0) {
  284. dev_dbg(ddev, "======>>>>>> Physical\n");
  285. strcpy(globtype, "Physical");
  286. } else
  287. dev_dbg(ddev, "======>>>>>>\n");
  288. /* Indent the debug output */
  289. indent++;
  290. for (x = 0; x < indent; x++)
  291. indentstr[x] = '-';
  292. indentstr[x] = 0;
  293. /* Save global tags */
  294. for (x = 0; x < TAG_GLOB_MAX; x++)
  295. oldval[x] = globalval[x];
  296. break;
  297. case TAG_MAIN_COL_END:
  298. dev_dbg(ddev, "<<<<<<======\n");
  299. maintype = 'E';
  300. indent--;
  301. for (x = 0; x < indent; x++)
  302. indentstr[x] = '-';
  303. indentstr[x] = 0;
  304. /* Copy global tags back */
  305. for (x = 0; x < TAG_GLOB_MAX; x++)
  306. globalval[x] = oldval[x];
  307. break;
  308. }
  309. switch (size) {
  310. case 1:
  311. dev_dbg(ddev, "%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x\n",
  312. indentstr, tag, maintype, size, globtype, data);
  313. break;
  314. case 2:
  315. dev_dbg(ddev, "%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x\n",
  316. indentstr, tag, maintype, size, globtype, data16);
  317. break;
  318. case 4:
  319. dev_dbg(ddev, "%sMAINTAG:(%d) %c SIZE: %d Data: %s 0x%x\n",
  320. indentstr, tag, maintype, size, globtype, data32);
  321. break;
  322. }
  323. break;
  324. case TYPE_GLOBAL:
  325. switch (tag) {
  326. case TAG_GLOB_USAGE:
  327. /*
  328. * First time we hit the global usage tag,
  329. * it should tell us the type of device
  330. */
  331. if (device->usage == 0)
  332. device->usage = data;
  333. strcpy(globtype, "USAGE");
  334. break;
  335. case TAG_GLOB_LOG_MIN:
  336. strcpy(globtype, "LOG_MIN");
  337. break;
  338. case TAG_GLOB_LOG_MAX:
  339. strcpy(globtype, "LOG_MAX");
  340. break;
  341. case TAG_GLOB_PHYS_MIN:
  342. strcpy(globtype, "PHYS_MIN");
  343. break;
  344. case TAG_GLOB_PHYS_MAX:
  345. strcpy(globtype, "PHYS_MAX");
  346. break;
  347. case TAG_GLOB_UNIT_EXP:
  348. strcpy(globtype, "EXP");
  349. break;
  350. case TAG_GLOB_UNIT:
  351. strcpy(globtype, "UNIT");
  352. break;
  353. case TAG_GLOB_REPORT_SZ:
  354. strcpy(globtype, "REPORT_SZ");
  355. break;
  356. case TAG_GLOB_REPORT_ID:
  357. strcpy(globtype, "REPORT_ID");
  358. /* New report, restart numbering */
  359. inputnum = 0;
  360. break;
  361. case TAG_GLOB_REPORT_CNT:
  362. strcpy(globtype, "REPORT_CNT");
  363. break;
  364. case TAG_GLOB_PUSH:
  365. strcpy(globtype, "PUSH");
  366. break;
  367. case TAG_GLOB_POP:
  368. strcpy(globtype, "POP");
  369. break;
  370. }
  371. /* Check to make sure we have a good tag number
  372. so we don't overflow array */
  373. if (tag < TAG_GLOB_MAX) {
  374. switch (size) {
  375. case 1:
  376. dev_dbg(ddev, "%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x\n",
  377. indentstr, globtype, tag, size, data);
  378. globalval[tag] = data;
  379. break;
  380. case 2:
  381. dev_dbg(ddev, "%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x\n",
  382. indentstr, globtype, tag, size, data16);
  383. globalval[tag] = data16;
  384. break;
  385. case 4:
  386. dev_dbg(ddev, "%sGLOBALTAG:%s(%d) SIZE: %d Data: 0x%x\n",
  387. indentstr, globtype, tag, size, data32);
  388. globalval[tag] = data32;
  389. break;
  390. }
  391. } else {
  392. dev_dbg(ddev, "%sGLOBALTAG: ILLEGAL TAG:%d SIZE: %d\n",
  393. indentstr, tag, size);
  394. }
  395. break;
  396. case TYPE_LOCAL:
  397. switch (tag) {
  398. case TAG_GLOB_USAGE:
  399. strcpy(globtype, "USAGE");
  400. /* Always 1 byte */
  401. usage = data;
  402. break;
  403. case TAG_GLOB_LOG_MIN:
  404. strcpy(globtype, "MIN");
  405. break;
  406. case TAG_GLOB_LOG_MAX:
  407. strcpy(globtype, "MAX");
  408. break;
  409. default:
  410. strcpy(globtype, "UNKNOWN");
  411. break;
  412. }
  413. switch (size) {
  414. case 1:
  415. dev_dbg(ddev, "%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x\n",
  416. indentstr, tag, globtype, size, data);
  417. break;
  418. case 2:
  419. dev_dbg(ddev, "%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x\n",
  420. indentstr, tag, globtype, size, data16);
  421. break;
  422. case 4:
  423. dev_dbg(ddev, "%sLOCALTAG:(%d) %s SIZE: %d Data: 0x%x\n",
  424. indentstr, tag, globtype, size, data32);
  425. break;
  426. }
  427. break;
  428. }
  429. }
  430. }
  431. /* INPUT DRIVER Routines */
  432. /*
  433. * Called when opening the input device. This will submit the URB to
  434. * the usb system so we start getting reports
  435. */
  436. static int gtco_input_open(struct input_dev *inputdev)
  437. {
  438. struct gtco *device = input_get_drvdata(inputdev);
  439. device->urbinfo->dev = device->usbdev;
  440. if (usb_submit_urb(device->urbinfo, GFP_KERNEL))
  441. return -EIO;
  442. return 0;
  443. }
  444. /*
  445. * Called when closing the input device. This will unlink the URB
  446. */
  447. static void gtco_input_close(struct input_dev *inputdev)
  448. {
  449. struct gtco *device = input_get_drvdata(inputdev);
  450. usb_kill_urb(device->urbinfo);
  451. }
  452. /*
  453. * Setup input device capabilities. Tell the input system what this
  454. * device is capable of generating.
  455. *
  456. * This information is based on what is read from the HID report and
  457. * placed in the struct gtco structure
  458. *
  459. */
  460. static void gtco_setup_caps(struct input_dev *inputdev)
  461. {
  462. struct gtco *device = input_get_drvdata(inputdev);
  463. /* Which events */
  464. inputdev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) |
  465. BIT_MASK(EV_MSC);
  466. /* Misc event menu block */
  467. inputdev->mscbit[0] = BIT_MASK(MSC_SCAN) | BIT_MASK(MSC_SERIAL) |
  468. BIT_MASK(MSC_RAW);
  469. /* Absolute values based on HID report info */
  470. input_set_abs_params(inputdev, ABS_X, device->min_X, device->max_X,
  471. 0, 0);
  472. input_set_abs_params(inputdev, ABS_Y, device->min_Y, device->max_Y,
  473. 0, 0);
  474. /* Proximity */
  475. input_set_abs_params(inputdev, ABS_DISTANCE, 0, 1, 0, 0);
  476. /* Tilt & pressure */
  477. input_set_abs_params(inputdev, ABS_TILT_X, device->mintilt_X,
  478. device->maxtilt_X, 0, 0);
  479. input_set_abs_params(inputdev, ABS_TILT_Y, device->mintilt_Y,
  480. device->maxtilt_Y, 0, 0);
  481. input_set_abs_params(inputdev, ABS_PRESSURE, device->minpressure,
  482. device->maxpressure, 0, 0);
  483. /* Transducer */
  484. input_set_abs_params(inputdev, ABS_MISC, 0, 0xFF, 0, 0);
  485. }
  486. /* USB Routines */
  487. /*
  488. * URB callback routine. Called when we get IRQ reports from the
  489. * digitizer.
  490. *
  491. * This bridges the USB and input device worlds. It generates events
  492. * on the input device based on the USB reports.
  493. */
  494. static void gtco_urb_callback(struct urb *urbinfo)
  495. {
  496. struct gtco *device = urbinfo->context;
  497. struct input_dev *inputdev;
  498. int rc;
  499. u32 val = 0;
  500. char le_buffer[2];
  501. inputdev = device->inputdevice;
  502. /* Was callback OK? */
  503. if (urbinfo->status == -ECONNRESET ||
  504. urbinfo->status == -ENOENT ||
  505. urbinfo->status == -ESHUTDOWN) {
  506. /* Shutdown is occurring. Return and don't queue up any more */
  507. return;
  508. }
  509. if (urbinfo->status != 0) {
  510. /*
  511. * Some unknown error. Hopefully temporary. Just go and
  512. * requeue an URB
  513. */
  514. goto resubmit;
  515. }
  516. /*
  517. * Good URB, now process
  518. */
  519. /* PID dependent when we interpret the report */
  520. if (inputdev->id.product == PID_1000 ||
  521. inputdev->id.product == PID_1001 ||
  522. inputdev->id.product == PID_1002) {
  523. /*
  524. * Switch on the report ID
  525. * Conveniently, the reports have more information, the higher
  526. * the report number. We can just fall through the case
  527. * statements if we start with the highest number report
  528. */
  529. switch (device->buffer[0]) {
  530. case 5:
  531. /* Pressure is 9 bits */
  532. val = ((u16)(device->buffer[8]) << 1);
  533. val |= (u16)(device->buffer[7] >> 7);
  534. input_report_abs(inputdev, ABS_PRESSURE,
  535. device->buffer[8]);
  536. /* Mask out the Y tilt value used for pressure */
  537. device->buffer[7] = (u8)((device->buffer[7]) & 0x7F);
  538. /* Fall thru */
  539. case 4:
  540. /* Tilt */
  541. input_report_abs(inputdev, ABS_TILT_X,
  542. sign_extend32(device->buffer[6], 6));
  543. input_report_abs(inputdev, ABS_TILT_Y,
  544. sign_extend32(device->buffer[7], 6));
  545. /* Fall thru */
  546. case 2:
  547. case 3:
  548. /* Convert buttons, only 5 bits possible */
  549. val = (device->buffer[5]) & MASK_BUTTON;
  550. /* We don't apply any meaning to the bitmask,
  551. just report */
  552. input_event(inputdev, EV_MSC, MSC_SERIAL, val);
  553. /* Fall thru */
  554. case 1:
  555. /* All reports have X and Y coords in the same place */
  556. val = get_unaligned_le16(&device->buffer[1]);
  557. input_report_abs(inputdev, ABS_X, val);
  558. val = get_unaligned_le16(&device->buffer[3]);
  559. input_report_abs(inputdev, ABS_Y, val);
  560. /* Ditto for proximity bit */
  561. val = device->buffer[5] & MASK_INRANGE ? 1 : 0;
  562. input_report_abs(inputdev, ABS_DISTANCE, val);
  563. /* Report 1 is an exception to how we handle buttons */
  564. /* Buttons are an index, not a bitmask */
  565. if (device->buffer[0] == 1) {
  566. /*
  567. * Convert buttons, 5 bit index
  568. * Report value of index set as one,
  569. * the rest as 0
  570. */
  571. val = device->buffer[5] & MASK_BUTTON;
  572. dev_dbg(&device->intf->dev,
  573. "======>>>>>>REPORT 1: val 0x%X(%d)\n",
  574. val, val);
  575. /*
  576. * We don't apply any meaning to the button
  577. * index, just report it
  578. */
  579. input_event(inputdev, EV_MSC, MSC_SERIAL, val);
  580. }
  581. break;
  582. case 7:
  583. /* Menu blocks */
  584. input_event(inputdev, EV_MSC, MSC_SCAN,
  585. device->buffer[1]);
  586. break;
  587. }
  588. }
  589. /* Other pid class */
  590. if (inputdev->id.product == PID_400 ||
  591. inputdev->id.product == PID_401) {
  592. /* Report 2 */
  593. if (device->buffer[0] == 2) {
  594. /* Menu blocks */
  595. input_event(inputdev, EV_MSC, MSC_SCAN, device->buffer[1]);
  596. }
  597. /* Report 1 */
  598. if (device->buffer[0] == 1) {
  599. char buttonbyte;
  600. /* IF X max > 64K, we still a bit from the y report */
  601. if (device->max_X > 0x10000) {
  602. val = (u16)(((u16)(device->buffer[2] << 8)) | (u8)device->buffer[1]);
  603. val |= (u32)(((u8)device->buffer[3] & 0x1) << 16);
  604. input_report_abs(inputdev, ABS_X, val);
  605. le_buffer[0] = (u8)((u8)(device->buffer[3]) >> 1);
  606. le_buffer[0] |= (u8)((device->buffer[3] & 0x1) << 7);
  607. le_buffer[1] = (u8)(device->buffer[4] >> 1);
  608. le_buffer[1] |= (u8)((device->buffer[5] & 0x1) << 7);
  609. val = get_unaligned_le16(le_buffer);
  610. input_report_abs(inputdev, ABS_Y, val);
  611. /*
  612. * Shift the button byte right by one to
  613. * make it look like the standard report
  614. */
  615. buttonbyte = device->buffer[5] >> 1;
  616. } else {
  617. val = get_unaligned_le16(&device->buffer[1]);
  618. input_report_abs(inputdev, ABS_X, val);
  619. val = get_unaligned_le16(&device->buffer[3]);
  620. input_report_abs(inputdev, ABS_Y, val);
  621. buttonbyte = device->buffer[5];
  622. }
  623. /* BUTTONS and PROXIMITY */
  624. val = buttonbyte & MASK_INRANGE ? 1 : 0;
  625. input_report_abs(inputdev, ABS_DISTANCE, val);
  626. /* Convert buttons, only 4 bits possible */
  627. val = buttonbyte & 0x0F;
  628. #ifdef USE_BUTTONS
  629. for (i = 0; i < 5; i++)
  630. input_report_key(inputdev, BTN_DIGI + i, val & (1 << i));
  631. #else
  632. /* We don't apply any meaning to the bitmask, just report */
  633. input_event(inputdev, EV_MSC, MSC_SERIAL, val);
  634. #endif
  635. /* TRANSDUCER */
  636. input_report_abs(inputdev, ABS_MISC, device->buffer[6]);
  637. }
  638. }
  639. /* Everybody gets report ID's */
  640. input_event(inputdev, EV_MSC, MSC_RAW, device->buffer[0]);
  641. /* Sync it up */
  642. input_sync(inputdev);
  643. resubmit:
  644. rc = usb_submit_urb(urbinfo, GFP_ATOMIC);
  645. if (rc != 0)
  646. dev_err(&device->intf->dev,
  647. "usb_submit_urb failed rc=0x%x\n", rc);
  648. }
  649. /*
  650. * The probe routine. This is called when the kernel find the matching USB
  651. * vendor/product. We do the following:
  652. *
  653. * - Allocate mem for a local structure to manage the device
  654. * - Request a HID Report Descriptor from the device and parse it to
  655. * find out the device parameters
  656. * - Create an input device and assign it attributes
  657. * - Allocate an URB so the device can talk to us when the input
  658. * queue is open
  659. */
  660. static int gtco_probe(struct usb_interface *usbinterface,
  661. const struct usb_device_id *id)
  662. {
  663. struct gtco *gtco;
  664. struct input_dev *input_dev;
  665. struct hid_descriptor *hid_desc;
  666. char *report;
  667. int result = 0, retry;
  668. int error;
  669. struct usb_endpoint_descriptor *endpoint;
  670. /* Allocate memory for device structure */
  671. gtco = kzalloc(sizeof(struct gtco), GFP_KERNEL);
  672. input_dev = input_allocate_device();
  673. if (!gtco || !input_dev) {
  674. dev_err(&usbinterface->dev, "No more memory\n");
  675. error = -ENOMEM;
  676. goto err_free_devs;
  677. }
  678. /* Set pointer to the input device */
  679. gtco->inputdevice = input_dev;
  680. /* Save interface information */
  681. gtco->usbdev = interface_to_usbdev(usbinterface);
  682. gtco->intf = usbinterface;
  683. /* Allocate some data for incoming reports */
  684. gtco->buffer = usb_alloc_coherent(gtco->usbdev, REPORT_MAX_SIZE,
  685. GFP_KERNEL, &gtco->buf_dma);
  686. if (!gtco->buffer) {
  687. dev_err(&usbinterface->dev, "No more memory for us buffers\n");
  688. error = -ENOMEM;
  689. goto err_free_devs;
  690. }
  691. /* Allocate URB for reports */
  692. gtco->urbinfo = usb_alloc_urb(0, GFP_KERNEL);
  693. if (!gtco->urbinfo) {
  694. dev_err(&usbinterface->dev, "Failed to allocate URB\n");
  695. error = -ENOMEM;
  696. goto err_free_buf;
  697. }
  698. /* Sanity check that a device has an endpoint */
  699. if (usbinterface->altsetting[0].desc.bNumEndpoints < 1) {
  700. dev_err(&usbinterface->dev,
  701. "Invalid number of endpoints\n");
  702. error = -EINVAL;
  703. goto err_free_urb;
  704. }
  705. /*
  706. * The endpoint is always altsetting 0, we know this since we know
  707. * this device only has one interrupt endpoint
  708. */
  709. endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
  710. /* Some debug */
  711. dev_dbg(&usbinterface->dev, "gtco # interfaces: %d\n", usbinterface->num_altsetting);
  712. dev_dbg(&usbinterface->dev, "num endpoints: %d\n", usbinterface->cur_altsetting->desc.bNumEndpoints);
  713. dev_dbg(&usbinterface->dev, "interface class: %d\n", usbinterface->cur_altsetting->desc.bInterfaceClass);
  714. dev_dbg(&usbinterface->dev, "endpoint: attribute:0x%x type:0x%x\n", endpoint->bmAttributes, endpoint->bDescriptorType);
  715. if (usb_endpoint_xfer_int(endpoint))
  716. dev_dbg(&usbinterface->dev, "endpoint: we have interrupt endpoint\n");
  717. dev_dbg(&usbinterface->dev, "endpoint extra len:%d\n", usbinterface->altsetting[0].extralen);
  718. /*
  719. * Find the HID descriptor so we can find out the size of the
  720. * HID report descriptor
  721. */
  722. if (usb_get_extra_descriptor(usbinterface->cur_altsetting,
  723. HID_DEVICE_TYPE, &hid_desc) != 0) {
  724. dev_err(&usbinterface->dev,
  725. "Can't retrieve exta USB descriptor to get hid report descriptor length\n");
  726. error = -EIO;
  727. goto err_free_urb;
  728. }
  729. dev_dbg(&usbinterface->dev,
  730. "Extra descriptor success: type:%d len:%d\n",
  731. hid_desc->bDescriptorType, hid_desc->wDescriptorLength);
  732. report = kzalloc(le16_to_cpu(hid_desc->wDescriptorLength), GFP_KERNEL);
  733. if (!report) {
  734. dev_err(&usbinterface->dev, "No more memory for report\n");
  735. error = -ENOMEM;
  736. goto err_free_urb;
  737. }
  738. /* Couple of tries to get reply */
  739. for (retry = 0; retry < 3; retry++) {
  740. result = usb_control_msg(gtco->usbdev,
  741. usb_rcvctrlpipe(gtco->usbdev, 0),
  742. USB_REQ_GET_DESCRIPTOR,
  743. USB_RECIP_INTERFACE | USB_DIR_IN,
  744. REPORT_DEVICE_TYPE << 8,
  745. 0, /* interface */
  746. report,
  747. le16_to_cpu(hid_desc->wDescriptorLength),
  748. 5000); /* 5 secs */
  749. dev_dbg(&usbinterface->dev, "usb_control_msg result: %d\n", result);
  750. if (result == le16_to_cpu(hid_desc->wDescriptorLength)) {
  751. parse_hid_report_descriptor(gtco, report, result);
  752. break;
  753. }
  754. }
  755. kfree(report);
  756. /* If we didn't get the report, fail */
  757. if (result != le16_to_cpu(hid_desc->wDescriptorLength)) {
  758. dev_err(&usbinterface->dev,
  759. "Failed to get HID Report Descriptor of size: %d\n",
  760. hid_desc->wDescriptorLength);
  761. error = -EIO;
  762. goto err_free_urb;
  763. }
  764. /* Create a device file node */
  765. usb_make_path(gtco->usbdev, gtco->usbpath, sizeof(gtco->usbpath));
  766. strlcat(gtco->usbpath, "/input0", sizeof(gtco->usbpath));
  767. /* Set Input device functions */
  768. input_dev->open = gtco_input_open;
  769. input_dev->close = gtco_input_close;
  770. /* Set input device information */
  771. input_dev->name = "GTCO_CalComp";
  772. input_dev->phys = gtco->usbpath;
  773. input_set_drvdata(input_dev, gtco);
  774. /* Now set up all the input device capabilities */
  775. gtco_setup_caps(input_dev);
  776. /* Set input device required ID information */
  777. usb_to_input_id(gtco->usbdev, &input_dev->id);
  778. input_dev->dev.parent = &usbinterface->dev;
  779. /* Setup the URB, it will be posted later on open of input device */
  780. endpoint = &usbinterface->altsetting[0].endpoint[0].desc;
  781. usb_fill_int_urb(gtco->urbinfo,
  782. gtco->usbdev,
  783. usb_rcvintpipe(gtco->usbdev,
  784. endpoint->bEndpointAddress),
  785. gtco->buffer,
  786. REPORT_MAX_SIZE,
  787. gtco_urb_callback,
  788. gtco,
  789. endpoint->bInterval);
  790. gtco->urbinfo->transfer_dma = gtco->buf_dma;
  791. gtco->urbinfo->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  792. /* Save gtco pointer in USB interface gtco */
  793. usb_set_intfdata(usbinterface, gtco);
  794. /* All done, now register the input device */
  795. error = input_register_device(input_dev);
  796. if (error)
  797. goto err_free_urb;
  798. return 0;
  799. err_free_urb:
  800. usb_free_urb(gtco->urbinfo);
  801. err_free_buf:
  802. usb_free_coherent(gtco->usbdev, REPORT_MAX_SIZE,
  803. gtco->buffer, gtco->buf_dma);
  804. err_free_devs:
  805. input_free_device(input_dev);
  806. kfree(gtco);
  807. return error;
  808. }
  809. /*
  810. * This function is a standard USB function called when the USB device
  811. * is disconnected. We will get rid of the URV, de-register the input
  812. * device, and free up allocated memory
  813. */
  814. static void gtco_disconnect(struct usb_interface *interface)
  815. {
  816. /* Grab private device ptr */
  817. struct gtco *gtco = usb_get_intfdata(interface);
  818. /* Now reverse all the registration stuff */
  819. if (gtco) {
  820. input_unregister_device(gtco->inputdevice);
  821. usb_kill_urb(gtco->urbinfo);
  822. usb_free_urb(gtco->urbinfo);
  823. usb_free_coherent(gtco->usbdev, REPORT_MAX_SIZE,
  824. gtco->buffer, gtco->buf_dma);
  825. kfree(gtco);
  826. }
  827. dev_info(&interface->dev, "gtco driver disconnected\n");
  828. }
  829. /* STANDARD MODULE LOAD ROUTINES */
  830. static struct usb_driver gtco_driverinfo_table = {
  831. .name = "gtco",
  832. .id_table = gtco_usbid_table,
  833. .probe = gtco_probe,
  834. .disconnect = gtco_disconnect,
  835. };
  836. module_usb_driver(gtco_driverinfo_table);
  837. MODULE_DESCRIPTION("GTCO digitizer USB driver");
  838. MODULE_LICENSE("GPL");