1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
4 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
6 * USB/RS232 I-Force joysticks and wheels.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/input.h>
15 #include <linux/module.h>
16 #include <linux/spinlock.h>
17 #include <linux/usb.h>
18 #include <linux/serio.h>
19 #include <linux/circ_buf.h>
20 #include <linux/mutex.h>
22 /* This module provides arbitrary resource management routines.
23 * I use it to manage the device's memory.
24 * Despite the name of this module, I am *not* going to access the ioports.
26 #include <linux/ioport.h>
29 #define IFORCE_MAX_LENGTH 16
35 #define IFORCE_EFFECTS_MAX 32
37 /* Each force feedback effect is made of one core effect, which can be
38 * associated to at most to effect modifiers
40 #define FF_MOD1_IS_USED 0
41 #define FF_MOD2_IS_USED 1
42 #define FF_CORE_IS_USED 2
43 #define FF_CORE_IS_PLAYED 3 /* Effect is currently being played */
44 #define FF_CORE_SHOULD_PLAY 4 /* User wants the effect to be played */
45 #define FF_CORE_UPDATE 5 /* Effect is being updated */
46 #define FF_MODCORE_CNT 6
48 struct iforce_core_effect
{
49 /* Information about where modifiers are stored in the device's memory */
50 struct resource mod1_chunk
;
51 struct resource mod2_chunk
;
52 unsigned long flags
[BITS_TO_LONGS(FF_MODCORE_CNT
)];
55 #define FF_CMD_EFFECT 0x010e
56 #define FF_CMD_ENVELOPE 0x0208
57 #define FF_CMD_MAGNITUDE 0x0303
58 #define FF_CMD_PERIOD 0x0407
59 #define FF_CMD_CONDITION 0x050a
61 #define FF_CMD_AUTOCENTER 0x4002
62 #define FF_CMD_PLAY 0x4103
63 #define FF_CMD_ENABLE 0x4201
64 #define FF_CMD_GAIN 0x4301
66 #define FF_CMD_QUERY 0xff01
68 /* Buffer for async write */
70 #define XMIT_INC(var, n) (var)+=n; (var)&= XMIT_SIZE -1
71 /* iforce::xmit_flags */
72 #define IFORCE_XMIT_RUNNING 0
73 #define IFORCE_XMIT_AGAIN 1
75 struct iforce_device
{
85 struct input_dev
*dev
; /* Input device interface */
86 struct iforce_device
*type
;
89 unsigned char data
[IFORCE_MAX_LENGTH
];
90 unsigned char edata
[IFORCE_MAX_LENGTH
];
94 #ifdef CONFIG_JOYSTICK_IFORCE_232
95 struct serio
*serio
; /* RS232 transfer */
96 int idx
, pkt
, len
, id
;
99 #ifdef CONFIG_JOYSTICK_IFORCE_USB
100 struct usb_device
*usbdev
; /* USB transfer */
101 struct usb_interface
*intf
;
102 struct urb
*irq
, *out
, *ctrl
;
103 struct usb_ctrlrequest cr
;
105 spinlock_t xmit_lock
;
106 /* Buffer used for asynchronous sending of bytes to the device */
107 struct circ_buf xmit
;
108 unsigned char xmit_data
[XMIT_SIZE
];
109 unsigned long xmit_flags
[1];
112 wait_queue_head_t wait
;
113 struct resource device_memory
;
114 struct iforce_core_effect core_effects
[IFORCE_EFFECTS_MAX
];
115 struct mutex mem_mutex
;
118 /* Get hi and low bytes of a 16-bits int */
119 #define HI(a) ((unsigned char)((a) >> 8))
120 #define LO(a) ((unsigned char)((a) & 0xff))
122 /* For many parameters, it seems that 0x80 is a special value that should
123 * be avoided. Instead, we replace this value by 0x7f
125 #define HIFIX80(a) ((unsigned char)(((a)<0? (a)+255 : (a))>>8))
127 /* Encode a time value */
128 #define TIME_SCALE(a) (a)
131 /* Public functions */
133 void iforce_serial_xmit(struct iforce
*iforce
);
136 void iforce_usb_xmit(struct iforce
*iforce
);
139 int iforce_init_device(struct iforce
*iforce
);
141 /* iforce-packets.c */
142 int iforce_control_playback(struct iforce
*, u16 id
, unsigned int);
143 void iforce_process_packet(struct iforce
*iforce
, u16 cmd
, unsigned char *data
);
144 int iforce_send_packet(struct iforce
*iforce
, u16 cmd
, unsigned char* data
);
145 void iforce_dump_packet(struct iforce
*iforce
, char *msg
, u16 cmd
, unsigned char *data
);
146 int iforce_get_id_packet(struct iforce
*iforce
, char *packet
);
149 int iforce_upload_periodic(struct iforce
*, struct ff_effect
*, struct ff_effect
*);
150 int iforce_upload_constant(struct iforce
*, struct ff_effect
*, struct ff_effect
*);
151 int iforce_upload_condition(struct iforce
*, struct ff_effect
*, struct ff_effect
*);
153 /* Public variables */
154 extern struct serio_driver iforce_serio_drv
;
155 extern struct usb_driver iforce_usb_driver
;