1 /*
2  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2007, 2008 Bartosz Fabianowski <freebsd@chillt.de>
8  * All rights reserved.
9  *
10  * Financed by the "Irish Research Council for Science, Engineering and
11  * Technology: funded by the National Development Plan"
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions, and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1998 The NetBSD Foundation, Inc.
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to The NetBSD Foundation
40  * by Lennart Augustsson (lennart@augustsson.net) at
41  * Carlstedt Research & Technology.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *        This product includes software developed by the NetBSD
54  *        Foundation, Inc. and its contributors.
55  * 4. Neither the name of The NetBSD Foundation nor the names of its
56  *    contributors may be used to endorse or promote products derived
57  *    from this software without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69  * POSSIBILITY OF SUCH DAMAGE.
70  */
71 
72 #ifndef _SYS_USB_USBWCM_H
73 #define	_SYS_USB_USBWCM_H
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 #include <sys/ioccom.h>
80 #if defined(_LP64) || defined(_I32LPx)
81 #include <sys/types32.h>
82 #else
83 #include <sys/types.h>
84 #endif
85 #include <sys/time.h>
86 
87 #define	EVTIOCGVERSION	_IOR('E', 0x1, int)
88 #define	EVTIOCGDEVID	_IOR('E', 0x2, struct event_dev_id)
89 #define	EVTIOCGBM(i, s)	_IORN('E', 0x20 + (i), (s))
90 #define	EVTIOCGABS(i)	_IOR('E', 0x40 + (i), struct event_abs_axis)
91 #define	EVTIOC		('E' << 8)
92 
93 struct event_dev_id {
94 	uint16_t bus;
95 #define	ID_BUS_USB 3
96 	uint16_t vendor;
97 	uint16_t product;
98 	uint16_t version;
99 };
100 
101 struct event_abs_axis {
102 	int32_t value;
103 	int32_t min;
104 	int32_t max;
105 	int32_t fuzz;
106 	int32_t flat;
107 };
108 
109 struct event_input {
110 #if defined(_LP64) || defined(_I32LPx)
111 	struct timeval32 time;
112 #else
113 	struct timeval time;
114 #endif
115 	uint16_t type;
116 	uint16_t code;
117 	int32_t value;
118 };
119 
120 #define	EVT_SYN				0x0000
121 #define	EVT_BTN				0x0001
122 #define	EVT_REL				0x0002
123 #define	EVT_ABS				0x0003
124 #define	EVT_MSC				0x0004
125 #define	EVT_USED			0x0005
126 #define	EVT_MAX				0x001f
127 
128 #define	SYN_REPORT			0x0000
129 
130 #define	BTN_MISC_0			0x0100
131 #define	BTN_MISC_1			0x0101
132 #define	BTN_MISC_2			0x0102
133 #define	BTN_MISC_3			0x0103
134 #define	BTN_MISC_4			0x0104
135 #define	BTN_MISC_5			0x0105
136 #define	BTN_MISC_6			0x0106
137 #define	BTN_MISC_7			0x0107
138 #define	BTN_MISC_8			0x0108
139 
140 #define	BTN_LEFT			0x0110
141 #define	BTN_RIGHT			0x0111
142 #define	BTN_MIDDLE			0x0112
143 #define	BTN_SIDE			0x0113
144 #define	BTN_EXTRA			0x0114
145 #define	BTN_TOOL_PEN			0x0140
146 #define	BTN_TOOL_ERASER			0x0141
147 #define	BTN_TOOL_PAD			0x0145
148 #define	BTN_TOOL_MOUSE			0x0146
149 #define	BTN_TIP				0x014a
150 #define	BTN_STYLUS_1			0x014b
151 #define	BTN_STYLUS_2			0x014c
152 #define	BTN_USED			0x014d
153 #define	BTN_MISC_UND			0x01ff
154 #define	BTN_MAX				0x01ff
155 
156 #define	REL_WHEEL			0x0008
157 #define	REL_MAX				0x000f
158 
159 #define	ABS_X				0x0000
160 #define	ABS_Y				0x0001
161 #define	ABS_Z				0x0002
162 #define	ABS_RX				0x0003
163 #define	ABS_RY				0x0004
164 #define	ABS_RZ				0x0005
165 
166 #define	ABS_WHEEL			0x0008
167 #define	ABS_PRESSURE			0x0018
168 #define	ABS_DISTANCE			0x0019
169 #define	ABS_TILT_X			0x001a
170 #define	ABS_TILT_Y			0x001b
171 #define	ABS_MISC			0x0028
172 #define	ABS_USED			0x0029
173 #define	ABS_MAX				0x003f
174 
175 #define	MSC_SERIAL			0x0000
176 #define	MSC_MAX				0x0007
177 
178 #ifdef _KERNEL
179 /* USB IDs */
180 #define	USB_VENDOR_WACOM			0x056a
181 
182 #define	USB_PRODUCT_WACOM_GRAPHIRE		0x0010
183 #define	USB_PRODUCT_WACOM_GRAPHIRE2_4X5		0x0011
184 #define	USB_PRODUCT_WACOM_GRAPHIRE2_5X7		0x0012
185 #define	USB_PRODUCT_WACOM_GRAPHIRE3_4X5		0x0013
186 #define	USB_PRODUCT_WACOM_GRAPHIRE3_6X8		0x0014
187 #define	USB_PRODUCT_WACOM_GRAPHIRE4_4X5		0x0015
188 #define	USB_PRODUCT_WACOM_GRAPHIRE4_6X8		0x0016
189 #define	USB_PRODUCT_WACOM_BAMBOO_FUN_4X5	0x0017
190 #define	USB_PRODUCT_WACOM_BAMBOO_FUN_6X8	0x0018
191 #define	USB_PRODUCT_WACOM_BAMBOO_ONE_6X8	0x0019
192 #define	USB_PRODUCT_WACOM_CINTIQ_21UX		0x003f
193 #define	USB_PRODUCT_WACOM_VOLITO		0x0060
194 #define	USB_PRODUCT_WACOM_PENSTATION2		0x0061
195 #define	USB_PRODUCT_WACOM_VOLITO2_4X5		0x0062
196 #define	USB_PRODUCT_WACOM_VOLITO2_2X3		0x0063
197 #define	USB_PRODUCT_WACOM_PENPARTNER2		0x0064
198 #define	USB_PRODUCT_WACOM_BAMBOO		0x0065
199 #define	USB_PRODUCT_WACOM_BAMBOO_ONE_4X5	0x0069
200 #define	USB_PRODUCT_WACOM_INTUOS3_4X5		0x00b0
201 #define	USB_PRODUCT_WACOM_INTUOS3_6X8		0x00b1
202 #define	USB_PRODUCT_WACOM_INTUOS3_9X12		0x00b2
203 #define	USB_PRODUCT_WACOM_INTUOS3_12X12		0x00b3
204 #define	USB_PRODUCT_WACOM_INTUOS3_12X19		0x00b4
205 #define	USB_PRODUCT_WACOM_INTUOS3_6X11		0x00b5
206 #define	USB_PRODUCT_WACOM_INTUOS3_4X6		0x00b7
207 
208 #define	USB_PRODUCT_WACOM_INTUOS4_4X6		0x00b8
209 #define	USB_PRODUCT_WACOM_INTUOS4_6X9		0x00b9
210 #define	USB_PRODUCT_WACOM_INTUOS4_8X13		0x00ba
211 #define	USB_PRODUCT_WACOM_INTUOS4_12X19		0x00bb
212 
213 #define	TOOL_ID_PEN	0x0002
214 #define	TOOL_ID_MOUSE	0x0006
215 #define	TOOL_ID_ERASER	0x000a
216 #define	TOOL_ID_PAD	0x000f
217 
218 #define	SERIAL_PAD_INTUOS	0xffffffff
219 #define	SERIAL_PAD_GRAPHIRE4	0x000000f0
220 
221 #define	EUWACOMGETVERSION		0x01
222 #define	EUWACOMGETID			0x02
223 #define	EUWACOMGETBM			0x20
224 #define	EUWACOMGETABS			0x40
225 
226 /* Protocols */
227 struct uwacom_protocol_type {
228 	int packet_size;
229 	int distance_max;
230 };
231 
232 enum uwacom_protocol {
233 	/* Graphire family */
234 	GRAPHIRE = 0,
235 	GRAPHIRE4,
236 	MYOFFICE,
237 
238 	/* Intuos family */
239 	INTUOS3S,
240 	INTUOS3L,
241 	INTUOS4S,
242 	INTUOS4L,
243 	CINTIQ
244 };
245 
246 struct uwacom_id {
247 	uint16_t	vid;
248 	uint16_t	pid;
249 };
250 
251 /* Models */
252 struct uwacom_type {
253 	struct uwacom_id	devno;
254 	enum uwacom_protocol	protocol;
255 	int			x_max;
256 	int			y_max;
257 	int			pressure_max;
258 };
259 
260 static const struct uwacom_protocol_type uwacom_protocols[] = {
261 	{ 8, 63},
262 	{ 8, 63},
263 	{ 9, 63},
264 	{10, 63},
265 	{10, 63},
266 	{10, 63},
267 	{10, 63},
268 	{10, 63}
269 };
270 
271 struct uwacom_softc {
272 	const struct uwacom_type	*sc_type;
273 	struct event_dev_id		sc_id;
274 	unsigned long			*sc_bm[EVT_USED];
275 
276 	int				*sc_btn;
277 	struct event_abs_axis		*sc_abs;
278 	int				sc_tool[2];
279 	int				sc_tool_id[2];
280 	unsigned int			sc_serial[2];
281 	int				sc_sync;
282 };
283 
284 typedef struct usbwcm_state {
285 	queue_t		*usbwcm_rq;	/* pointer to read queue */
286 	queue_t		*usbwcm_wq;	/* pointer to write queue */
287 
288 	int32_t		usbwcm_flags;	/* open/qwait status */
289 #define	USBWCM_OPEN    0x00000001	/* opened for business */
290 #define	USBWCM_QWAIT   0x00000002	/* waiting for a response */
291 
292 	/* software state */
293 	struct uwacom_softc	usbwcm_softc;
294 
295 	/* device model data */
296 	hid_vid_pid_t	usbwcm_devid;
297 
298 	/*
299 	 * Is an ioctl fails because an mblk wasn't
300 	 * available, the mlbk is saved here.
301 	 */
302 	mblk_t		*usbwcm_mioctl;
303 	bufcall_id_t	usbwcm_bufcall;	/* id returned by bufcall() */
304 } usbwcm_state_t;
305 
306 #define	abs(x)		((x) < 0 ? -(x) : (x))
307 
308 typedef struct usbwcm_copyin_s {
309 	caddr_t	addr;
310 	int	state;
311 #define	USBWCM_GETSTRUCT 1
312 #define	USBWCM_GETRESULT 2
313 } usbwcm_copyin_t;
314 
315 static const struct uwacom_type uwacom_devs[] = {
316 	{
317 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_GRAPHIRE},
318 		GRAPHIRE, 10206, 7422, 511
319 	},
320 	{
321 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_GRAPHIRE2_4X5},
322 		GRAPHIRE, 10206, 7422, 511
323 	},
324 	{
325 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_GRAPHIRE2_5X7},
326 		GRAPHIRE, 13918, 10206, 511
327 	},
328 	{
329 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_GRAPHIRE3_4X5},
330 		GRAPHIRE, 10208, 7424, 511
331 	},
332 	{
333 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_GRAPHIRE3_6X8},
334 		GRAPHIRE, 16704, 12064, 511
335 	},
336 	{
337 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_GRAPHIRE4_4X5},
338 		GRAPHIRE4, 10208, 7424, 511
339 	},
340 	{
341 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_GRAPHIRE4_6X8},
342 		GRAPHIRE4, 16704, 12064, 511
343 	},
344 	{
345 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_BAMBOO_FUN_4X5},
346 		MYOFFICE, 14760, 9225, 511
347 	},
348 	{
349 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_BAMBOO_FUN_6X8},
350 		MYOFFICE, 21648, 13530, 511
351 	},
352 	{
353 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_BAMBOO_ONE_6X8},
354 		GRAPHIRE, 16704, 12064, 511
355 	},
356 	{
357 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_CINTIQ_21UX},
358 		CINTIQ, 87200, 65600, 1023
359 	},
360 	{
361 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_VOLITO},
362 		GRAPHIRE, 5104, 3712, 511
363 	},
364 	{
365 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_PENSTATION2},
366 		GRAPHIRE, 3250, 2320, 255
367 	},
368 	{
369 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_VOLITO2_4X5},
370 		GRAPHIRE, 5104, 3712, 511
371 	},
372 	{
373 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_VOLITO2_2X3},
374 		GRAPHIRE, 3248, 2320, 511
375 	},
376 	{
377 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_PENPARTNER2},
378 		GRAPHIRE, 3250, 2320, 511
379 	},
380 	{
381 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_BAMBOO},
382 		MYOFFICE, 14760, 9225, 511
383 	},
384 	{
385 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_BAMBOO_ONE_4X5},
386 		GRAPHIRE, 5104, 3712, 511
387 	},
388 	{
389 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS3_4X5},
390 		INTUOS3S, 25400, 20320, 1023
391 	},
392 	{
393 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS3_6X8},
394 		INTUOS3L, 40640, 30480, 1023
395 	},
396 	{
397 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS3_9X12},
398 		INTUOS3L, 60960, 45720, 1023
399 	},
400 	{
401 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS3_12X12},
402 		INTUOS3L, 60960, 60960, 1023
403 	},
404 	{
405 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS3_12X19},
406 		INTUOS3L, 97536, 60960, 1023
407 	},
408 	{
409 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS3_6X11},
410 		INTUOS3L, 54204, 31750, 1023
411 	},
412 	{
413 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS3_4X6},
414 		INTUOS3S, 31496, 19685, 1023
415 	},
416 	{
417 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS4_4X6},
418 		INTUOS4S, 31496, 19685, 2047
419 	},
420 	{
421 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS4_6X9},
422 		INTUOS4L, 44704, 27940, 2047
423 	},
424 	{
425 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS4_8X13},
426 		INTUOS4L, 65024, 40640, 2047
427 	},
428 	{
429 		{USB_VENDOR_WACOM, USB_PRODUCT_WACOM_INTUOS4_12X19},
430 		INTUOS4L, 97536, 60960, 2047
431 	},
432 	{{0, 0},    0,  0, 0, 0}
433 };
434 
435 #define	PACKET_BIT(b, s)		((packet[b] >> (s)) & 1)
436 #define	PACKET_BITS(b, s, n) \
437 	((((s) + (n) > 32 ? ((packet[(b) - 4]) << (32 - (s))) : 0) | \
438 	((s) + (n) > 24 ? ((packet[(b) - 3]) << (24 - (s))) : 0) | \
439 	((s) + (n) > 16 ? ((packet[(b) - 2]) << (16 - (s))) : 0) | \
440 	((s) + (n) >  8 ? ((packet[(b) - 1]) << (8 - (s))) : 0) | \
441 	((packet[(b)]) >> (s))) & \
442 	((n) == 32 ? 0xffffffff : (1 << (n)) - 1))
443 
444 #define	BM_SIZE(x) \
445 	(((x) / (sizeof (long) * 8) + 1) * sizeof (long))
446 #define	BM_SET_BIT(x, y) \
447 	((x)[(y) / (sizeof (long) * 8)] |= (1ul << ((y) % (sizeof (long) * 8))))
448 
449 static const size_t bm_size[EVT_USED] = {
450 	BM_SIZE(EVT_MAX),
451 	BM_SIZE(BTN_MAX),
452 	BM_SIZE(REL_MAX),
453 	BM_SIZE(ABS_MAX),
454 	BM_SIZE(MSC_MAX),
455 };
456 
457 #define	PRINT_MASK_ALL	0xFFFFFFFF
458 
459 #endif	/* _KERNEL */
460 
461 #ifdef __cplusplus
462 }
463 #endif
464 
465 #endif	/* _SYS_USB_USBWCM_H */
466