1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1999-2001 Vojtech Pavlik
7 * Magellan and Space Mouse 6dof controller driver for Linux
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/input.h>
17 #include <linux/serio.h>
19 #define DRIVER_DESC "Magellan and SpaceMouse 6dof controller driver"
21 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
22 MODULE_DESCRIPTION(DRIVER_DESC
);
23 MODULE_LICENSE("GPL");
26 * Definitions & global arrays.
29 #define MAGELLAN_MAX_LENGTH 32
31 static int magellan_buttons
[] = { BTN_0
, BTN_1
, BTN_2
, BTN_3
, BTN_4
, BTN_5
, BTN_6
, BTN_7
, BTN_8
};
32 static int magellan_axes
[] = { ABS_X
, ABS_Y
, ABS_Z
, ABS_RX
, ABS_RY
, ABS_RZ
};
39 struct input_dev
*dev
;
41 unsigned char data
[MAGELLAN_MAX_LENGTH
];
46 * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan
47 * have correct upper nibbles for the lower ones, if not, the packet will
48 * be thrown away. It also strips these upper halves to simplify further
52 static int magellan_crunch_nibbles(unsigned char *data
, int count
)
54 static unsigned char nibbles
[16] = "0AB3D56GH9:K<MN?";
57 if (data
[count
] == nibbles
[data
[count
] & 0xf])
58 data
[count
] = data
[count
] & 0xf;
66 static void magellan_process_packet(struct magellan
* magellan
)
68 struct input_dev
*dev
= magellan
->dev
;
69 unsigned char *data
= magellan
->data
;
72 if (!magellan
->idx
) return;
74 switch (magellan
->data
[0]) {
76 case 'd': /* Axis data */
77 if (magellan
->idx
!= 25) return;
78 if (magellan_crunch_nibbles(data
, 24)) return;
79 for (i
= 0; i
< 6; i
++)
80 input_report_abs(dev
, magellan_axes
[i
],
81 (data
[(i
<< 2) + 1] << 12 | data
[(i
<< 2) + 2] << 8 |
82 data
[(i
<< 2) + 3] << 4 | data
[(i
<< 2) + 4]) - 32768);
85 case 'k': /* Button data */
86 if (magellan
->idx
!= 4) return;
87 if (magellan_crunch_nibbles(data
, 3)) return;
88 t
= (data
[1] << 1) | (data
[2] << 5) | data
[3];
89 for (i
= 0; i
< 9; i
++) input_report_key(dev
, magellan_buttons
[i
], (t
>> i
) & 1);
96 static irqreturn_t
magellan_interrupt(struct serio
*serio
,
97 unsigned char data
, unsigned int flags
)
99 struct magellan
* magellan
= serio_get_drvdata(serio
);
102 magellan_process_packet(magellan
);
105 if (magellan
->idx
< MAGELLAN_MAX_LENGTH
)
106 magellan
->data
[magellan
->idx
++] = data
;
112 * magellan_disconnect() is the opposite of magellan_connect()
115 static void magellan_disconnect(struct serio
*serio
)
117 struct magellan
* magellan
= serio_get_drvdata(serio
);
120 serio_set_drvdata(serio
, NULL
);
121 input_unregister_device(magellan
->dev
);
126 * magellan_connect() is the routine that is called when someone adds a
127 * new serio device that supports Magellan protocol and registers it as
131 static int magellan_connect(struct serio
*serio
, struct serio_driver
*drv
)
133 struct magellan
*magellan
;
134 struct input_dev
*input_dev
;
138 magellan
= kzalloc(sizeof(struct magellan
), GFP_KERNEL
);
139 input_dev
= input_allocate_device();
140 if (!magellan
|| !input_dev
)
143 magellan
->dev
= input_dev
;
144 snprintf(magellan
->phys
, sizeof(magellan
->phys
), "%s/input0", serio
->phys
);
146 input_dev
->name
= "LogiCad3D Magellan / SpaceMouse";
147 input_dev
->phys
= magellan
->phys
;
148 input_dev
->id
.bustype
= BUS_RS232
;
149 input_dev
->id
.vendor
= SERIO_MAGELLAN
;
150 input_dev
->id
.product
= 0x0001;
151 input_dev
->id
.version
= 0x0100;
152 input_dev
->dev
.parent
= &serio
->dev
;
154 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
156 for (i
= 0; i
< 9; i
++)
157 set_bit(magellan_buttons
[i
], input_dev
->keybit
);
159 for (i
= 0; i
< 6; i
++)
160 input_set_abs_params(input_dev
, magellan_axes
[i
], -360, 360, 0, 0);
162 serio_set_drvdata(serio
, magellan
);
164 err
= serio_open(serio
, drv
);
168 err
= input_register_device(magellan
->dev
);
174 fail3
: serio_close(serio
);
175 fail2
: serio_set_drvdata(serio
, NULL
);
176 fail1
: input_free_device(input_dev
);
182 * The serio driver structure.
185 static const struct serio_device_id magellan_serio_ids
[] = {
188 .proto
= SERIO_MAGELLAN
,
195 MODULE_DEVICE_TABLE(serio
, magellan_serio_ids
);
197 static struct serio_driver magellan_drv
= {
201 .description
= DRIVER_DESC
,
202 .id_table
= magellan_serio_ids
,
203 .interrupt
= magellan_interrupt
,
204 .connect
= magellan_connect
,
205 .disconnect
= magellan_disconnect
,
208 module_serio_driver(magellan_drv
);