-
Notifications
You must be signed in to change notification settings - Fork 3
/
rpi-kinect.c
522 lines (428 loc) · 13 KB
/
rpi-kinect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h> /* kmalloc() */
#include <linux/usb.h> /* USB stuff */
#include <linux/mutex.h> /* mutexes */
#include <linux/ioctl.h>
#include <asm/uaccess.h> /* copy_*_user */
#include "debug.h"
#include "semaphore.h"
#include "rpi-kinect.h"
/*
* kinect motor driver
*/
#define KINECT_MOTOR_VENDOR_ID 0x045e
#define KINECT_MOTOR_PRODUCT_ID 0x02b0
#define KINECT_MOTOR_CTRL_BUFFER_SIZE 10
#ifdef CONFIG_USB_DYNAMIC_MINORS
#define ML_MINOR_BASE 0
#else
#define ML_MINOR_BASE 96
#endif
struct usb_kinect_motor {
struct usb_device *udev;
struct usb_interface *interface;
unsigned char minor;
int open_count; /* Open count for this port */
struct semaphore sem; /* Locks this structure */
spinlock_t cmd_spinlock; /* locks dev->command */
char *ctrl_buffer; /* 8 byte buffer for the control msg */
struct urb *ctrl_urb;
struct usb_ctrlrequest *ctrl_dr; /* Setup packet information */
__u8 command; /* Last issued command */
};
static struct usb_device_id kinect_motor_table [] = {
{ USB_DEVICE(KINECT_MOTOR_VENDOR_ID, KINECT_MOTOR_PRODUCT_ID) },
{ }
};
MODULE_DEVICE_TABLE (usb, kinect_motor_table);
static int debug_level = DEBUG_LEVEL_INFO;
static int debug_trace = 0;
module_param(debug_level, int, S_IRUGO | S_IWUSR);
module_param(debug_trace, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug_level, "debug level (bitmask)");
MODULE_PARM_DESC(debug_trace, "enable function tracing");
/* Prevent races between open() and disconnect */
static DEFINE_MUTEX(disconnect_mutex);
static struct usb_driver kinect_motor_driver;
static void kinect_motor_abort_transfers(struct usb_kinect_motor *dev)
{
if (! dev) {
DBG_ERR("dev is NULL");
return;
}
if (! dev->udev) {
DBG_ERR("udev is NULL");
return;
}
if (dev->udev->state == USB_STATE_NOTATTACHED) {
DBG_ERR("udev not attached");
return;
}
if (dev->ctrl_urb)
usb_kill_urb(dev->ctrl_urb);
}
static inline void kinect_motor_delete(struct usb_kinect_motor *dev)
{
kinect_motor_abort_transfers(dev);
kfree(dev->ctrl_buffer);
kfree(dev->ctrl_dr);
kfree(dev);
}
static void kinect_motor_ctrl_callback(struct urb *urb)
{
struct usb_kinect_motor *dev = urb->context;
}
/*! \brief called when the /dev/kinect%d is opened.
*
* \param inode the inode of the file
* \param file the file object
* \return 0 if all went well
*/
static int kinect_motor_open(struct inode *inode, struct file *file)
{
struct usb_kinect_motor *dev = NULL;
struct usb_interface *interface;
int subminor;
int retval = 0;
subminor = iminor(inode);
// synchronize calls between kinect_motor_open and kinect_motor_disconnect
mutex_lock(&disconnect_mutex);
// find the interface that is registered to this subminor
// number. error out if we can't find one.
interface = usb_find_interface(&kinect_motor_driver, subminor);
if (! interface) {
DBG_ERR("can't find device for minor %d", subminor);
retval = -ENODEV;
goto exit;
}
// find the usb_kinect_motor structure that we have associated
// with this interface. error out if we can't find one.
dev = usb_get_intfdata(interface);
if (! dev) {
retval = -ENODEV;
goto exit;
}
/* lock this device */
if (down_interruptible(&dev->sem)) {
DBG_ERR("sem down failed");
retval = -ERESTARTSYS;
goto unlock_exit;
}
/* Increment our usage count for the device. */
++dev->open_count;
if (dev->open_count > 1)
DBG_DEBUG("open_count = %d", dev->open_count);
file->private_data = dev;
unlock_exit:
up(&dev->sem);
exit:
mutex_unlock(&disconnect_mutex);
return retval;
}
static int kinect_motor_release(struct inode *inode, struct file *file)
{
struct usb_kinect_motor *dev = NULL;
int retval = 0;
dev = file->private_data;
if (! dev) {
DBG_ERR("dev is NULL");
retval = -ENODEV;
goto exit;
}
/* Lock our device */
if (down_interruptible(&dev->sem)) {
retval = -ERESTARTSYS;
goto exit;
}
if (dev->open_count <= 0) {
DBG_ERR("device not opened");
retval = -ENODEV;
goto unlock_exit;
}
if (! dev->udev) {
DBG_DEBUG("device unplugged before the file was released");
up (&dev->sem); /* Unlock here as kinect_motor_delete frees dev. */
kinect_motor_delete(dev);
goto exit;
}
if (dev->open_count > 1)
DBG_DEBUG("open_count = %d", dev->open_count);
kinect_motor_abort_transfers(dev);
--dev->open_count;
unlock_exit:
up(&dev->sem);
exit:
return retval;
}
/*! \brief called when the /dev/kinect%d is read from
*/
static ssize_t kinect_sensors_read(struct file *file,
char __user *user_buf, size_t count,
loff_t *ppos)
{
struct usb_kinect_motor *dev;
int retval = 0, response;
dev = file->private_data;
/* Lock this object. */
if (down_interruptible(&dev->sem)) {
retval = -ERESTARTSYS;
goto exit;
}
/* Verify that the device wasn't unplugged. */
if (! dev->udev) {
retval = -ENODEV;
DBG_ERR("No device or device unplugged (%d)", retval);
goto unlock_exit;
}
// do a synchronous call to transmit to the driver
DBG_DEBUG("calling usb_control_msg");
response = usb_control_msg(dev->udev,
usb_rcvctrlpipe(dev->udev, 0),
0x32,
0xC0,
cpu_to_le16(0x0000),
cpu_to_le16(0x0000),
dev->ctrl_buffer,
KINECT_MOTOR_CTRL_BUFFER_SIZE,
0);
if (response < 0) {
DBG_ERR("calling usb_control_msg = %d", response);
goto exit;
}
// parse the results back into this structure
kinect_sensor_values sensor_values = {
.ux = ((uint16_t)(dev->ctrl_buffer)[2] << 8) | (dev->ctrl_buffer)[3],
.uy = ((uint16_t)(dev->ctrl_buffer)[4] << 8) | (dev->ctrl_buffer)[5],
.uz = ((uint16_t)(dev->ctrl_buffer)[6] << 8) | (dev->ctrl_buffer)[7],
.positive_angle_degrees = (uint8_t)(dev->ctrl_buffer)[8] / 2,
.status_code = (dev->ctrl_buffer)[9]
};
int amount_to_copy = count < sizeof(kinect_sensor_values) ? count : sizeof(kinect_sensor_values);
if (copy_to_user(user_buf, &sensor_values, amount_to_copy)) {
retval = -EFAULT;
goto unlock_exit;
}
unlock_exit:
up(&dev->sem);
exit:
return retval;
}
/*! \brief called when the /dev/kinect%d is written to.
*
* support writes of -128 to 128, one byte at a time -- this
* translates to movement by the kinect itself. no matter how much is
* written, we only look at the first byte.
*
* \param file the file object
* \param user_buf the buffer containing what we want to write
* \param count the number of bytes to write
* \return 0 if all went well
*/
static ssize_t kinect_motor_write(struct file *file,
const char __user *user_buf, size_t count,
loff_t *ppos)
{
struct usb_kinect_motor *dev;
int retval = 0;
__u8 cmd;
dev = file->private_data;
/* Lock this object. */
if (down_interruptible(&dev->sem)) {
retval = -ERESTARTSYS;
goto exit;
}
/* Verify that the device wasn't unplugged. */
if (! dev->udev) {
retval = -ENODEV;
DBG_ERR("No device or device unplugged (%d)", retval);
goto unlock_exit;
}
/* Verify that we actually have some data to write. */
if (count == 0)
goto unlock_exit;
count = 1;
if (copy_from_user(&cmd, user_buf, count)) {
retval = -EFAULT;
goto unlock_exit;
}
signed char command = (signed char)cmd;
DBG_DEBUG("received %d", command);
if ((command < -128) || (command > 128)) {
DBG_ERR("illegal range for motor movement");
retval = -0x2a; // not sure what this means
goto unlock_exit;
}
retval = usb_control_msg(dev->udev,
usb_sndctrlpipe(dev->udev, 0),
0x31,
0x40,
cpu_to_le16(command),
cpu_to_le16(0x0000),
dev->ctrl_buffer,
KINECT_MOTOR_CTRL_BUFFER_SIZE,
0);
if (retval) {
DBG_ERR("usb_control_msg failed (%d)", retval);
goto unlock_exit;
}
unlock_exit:
up(&dev->sem);
exit:
return retval;
}
static struct file_operations kinect_motor_fops = {
.owner = THIS_MODULE,
.read = kinect_sensors_read,
.write = kinect_motor_write,
.open = kinect_motor_open,
.release = kinect_motor_release,
};
static struct usb_class_driver kinect_motor_class = {
.name = "kinect%d",
.fops = &kinect_motor_fops,
.minor_base = ML_MINOR_BASE,
};
static int kinect_motor_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
struct usb_device *udev;
struct usb_kinect_motor *dev = NULL;
int retval, response;
udev = interface_to_usbdev(interface);
retval = -ENODEV;
if (! udev) {
DBG_ERR("udev is NULL");
goto exit;
}
dev = kzalloc(sizeof(struct usb_kinect_motor), GFP_KERNEL);
if (! dev) {
DBG_ERR("cannot allocate memory for struct usb_kinect_motor");
retval = -ENOMEM;
goto exit;
}
init_MUTEX(&dev->sem);
spin_lock_init(&dev->cmd_spinlock);
dev->udev = udev;
dev->interface = interface;
/* Set up the control URB. */
DBG_DEBUG("setting up the control URB");
dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
if (! dev->ctrl_urb) {
DBG_ERR("could not allocate ctrl_urb");
retval = -ENOMEM;
goto error;
}
// the buffer that we would use when we want to send commands
DBG_DEBUG("setting up the control buffer");
dev->ctrl_buffer = kzalloc(KINECT_MOTOR_CTRL_BUFFER_SIZE, GFP_KERNEL);
if (! dev->ctrl_buffer) {
DBG_ERR("could not allocate ctrl_buffer");
retval = -ENOMEM;
goto error;
}
dev->ctrl_dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
if (! dev->ctrl_dr) {
DBG_ERR("could not allocate usb_ctrlrequest");
retval = -ENOMEM;
goto error;
}
// do a synchronous call to transmit to the driver
DBG_DEBUG("calling usb_control_msg");
response = usb_control_msg(dev->udev,
usb_rcvctrlpipe(dev->udev, 0),
0x10,
0xC0,
cpu_to_le16(0x0000),
cpu_to_le16(0x0000),
dev->ctrl_buffer,
KINECT_MOTOR_CTRL_BUFFER_SIZE,
0);
if (response < 0) {
DBG_ERR("calling usb_control_msg = %d", response);
goto exit;
}
DBG_DEBUG("received %d bytes from usb_control_msg", response);
DBG_DEBUG("received %d", dev->ctrl_buffer[0]);
if (dev->ctrl_buffer[0] != 0x22) {
DBG_ERR("did not receive the correct response from the kinect");
goto error;
}
DBG_DEBUG("moving motor to zero position");
response = usb_control_msg(dev->udev,
usb_sndctrlpipe(dev->udev, 0),
0x31,
0x40,
cpu_to_le16(0x0000),
cpu_to_le16(0x0000),
dev->ctrl_buffer,
KINECT_MOTOR_CTRL_BUFFER_SIZE,
0);
if (response < 0) {
DBG_ERR("calling usb_control_msg = %d", response);
goto exit;
}
/* Save our data pointer in this interface device. */
usb_set_intfdata(interface, dev);
/* We can register the device now, as it is ready. */
retval = usb_register_dev(interface, &kinect_motor_class);
if (retval) {
DBG_ERR("not able to get a minor for this device.");
usb_set_intfdata(interface, NULL);
goto error;
}
dev->minor = interface->minor;
DBG_INFO("kinect motor now attached to /dev/kinect%d", interface->minor - ML_MINOR_BASE);
exit:
return retval;
error:
return -1;
/* kinect_motor_delete(dev); */
/* return retval; */
}
static void kinect_motor_disconnect(struct usb_interface *interface)
{
struct usb_kinect_motor *dev;
int minor;
mutex_lock(&disconnect_mutex); /* Not interruptible */
dev = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
down(&dev->sem); /* Not interruptible */
minor = dev->minor;
/* Give back our minor. */
usb_deregister_dev(interface, &kinect_motor_class);
/* If the device is not opened, then we clean up right now. */
if (! dev->open_count) {
up(&dev->sem);
kinect_motor_delete(dev);
} else {
dev->udev = NULL;
up(&dev->sem);
}
mutex_unlock(&disconnect_mutex);
DBG_INFO("kinect motor /dev/kinect%d now disconnected", minor - ML_MINOR_BASE);
}
static struct usb_driver kinect_motor_driver = {
.name = "kinect_motor",
.id_table = kinect_motor_table,
.probe = kinect_motor_probe,
.disconnect = kinect_motor_disconnect,
};
static int __init usb_kinect_init(void)
{
int result = usb_register(&kinect_motor_driver);
if (result) {
DBG_ERR("registering kinect motor driver failed");
} else {
DBG_INFO("kinect driver motor registered successfully");
}
return result;
}
static void __exit usb_kinect_exit(void)
{
usb_deregister(&kinect_motor_driver);
DBG_INFO("kinect motor module deregistered");
}
module_init(usb_kinect_init);
module_exit(usb_kinect_exit);
MODULE_AUTHOR("Raffi Krikorian");
MODULE_LICENSE("GPL");