-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxylo_led.c
520 lines (443 loc) · 13.2 KB
/
xylo_led.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
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/delay.h>
MODULE_DESCRIPTION("Xylo USB driver");
MODULE_LICENSE("GPL");
#define BUF_SIZE 16
/// begin fix
static char buffer[BUF_SIZE]; //used for the copy_from and copy_to
/// end fix
#define VENDOR_ID 0x04B4
#define PRODUCT_ID 0x8613
/*
* Data for a xylo device
*/
struct usb_xylo_led {
struct usb_device *udev;
int ledmask;
};
static struct usb_driver xylo_led_driver;
/*
* List of devices that work with this driver
*/
static struct usb_device_id id_table [] = {
{ USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
{}
};
/* Declare the list of devices */
MODULE_DEVICE_TABLE (usb, id_table);
/**
** @brief Send a bulk message to the xylo card to update its ledmask
**
** @param dev Device for which the ledmask should be updated
**
** @returns return value of usb_bulk_msg
*/
int xylo_led_send_bulk_ledmask(struct usb_xylo_led *dev)
{
int actual_length = 0;
/*
* Send bulk message
*/
return usb_bulk_msg(dev->udev,
usb_sndbulkpipe(dev->udev, 2),
&dev->ledmask,
1,
&actual_length,
2 * HZ);
}
/**
** @brief Change the ledmask with the given char value,
** and send the bulk message to update the ledmask in the card.
**
** @param dev Device for which to change the ledmask
** @param mask Mask to be set
**
** @returns return value of xylo_led_send_bulk_ledmask
*/
int xylo_led_send_bulk_ledmask_char(struct usb_xylo_led *dev,
unsigned char mask)
{
int ret = 0;
dev->ledmask = mask;
ret = xylo_led_send_bulk_ledmask(dev);
if (ret < 0)
printk (KERN_WARNING "xylo_led_send_bulk_ledmask_char: usb_bulk_msg() error %d\n",
ret);
return ret;
}
/**
** @brief This function sets the xylo led mask given in the buf string,
** and sends the bulk message to the xylo card.
**
** @param dev device for which to set the led mask
** @param buf string containing the mask in hex format
**
** @returns 0 on success.
*/
int xylo_led_send_bulk_ledmask_buf (struct usb_xylo_led *dev,
const char *buf)
{
int ret = 0;
int mask = 0;
/*
* Convert the string to a char
*/
sscanf (buf, "%x", &mask);
/*
* Update the mask for the device
*/
dev->ledmask = (unsigned char) mask;
/*
* Send the bulk message to update the mask
* in the card
*/
ret = xylo_led_send_bulk_ledmask(dev);
/*
* If an error occurs, display a warning message
*/
if (ret < 0)
printk (KERN_WARNING "xylo_led_send_bulk_ledmask_buf: usb_bulk_msg() error %d\n",
ret);
return ret;
}
/**
** @brief Display a small animation on the card to show that
** it is recognized.
**
** @param dev Device for which to show the animation.
*/
void xylo_led_animation(struct usb_xylo_led *dev)
{
char k = 0;
while (k < 8)
{
xylo_led_send_bulk_ledmask_char(dev, k);
msleep(50);
k++;
}
for (k = 0; k < 4; k++)
{
xylo_led_send_bulk_ledmask_char(dev, 0xff * (k % 2));
msleep(50);
}
}
/**
** @brief Send several packets in order to initiate communication
** with the xylo card.
**
*/
static int xylo_led_init_xylo_card(struct usb_xylo_led *udev)
{
int ret = 0;
/*
* Contains the code to be sent tothe xylo card
*/
char packets[11][16] = {
{0x75, 0x81, 0x5f, 0x90, 0xe6, 0x00, 0x74, 0x0a, 0xf0, 0x90, 0xe6, 0x7a, 0x74, 0x01, 0xf0, 0x11},
{0x9b, 0x90, 0xe6, 0x18, 0x74, 0x10, 0xf0, 0x11, 0x9b, 0x90, 0xe6, 0x19, 0x74, 0x10, 0xf0, 0x11},
{0x9b, 0x90, 0xe6, 0x1a, 0x74, 0x0c, 0xf0, 0x11, 0x9b, 0x90, 0xe6, 0x1b, 0x74, 0x0c, 0xf0, 0x11},
{0x9b, 0x90, 0xe6, 0x02, 0x74, 0x98, 0xf0, 0x11, 0x9b, 0x90, 0xe6, 0x03, 0x74, 0xfe, 0xf0, 0x90},
{0xe6, 0x70, 0x74, 0x80, 0xf0, 0x11, 0x9b, 0x90, 0xe6, 0x01, 0x74, 0x03, 0xf0, 0x90, 0xe6, 0x8d},
{0xf0, 0xe5, 0xba, 0x20, 0xe1, 0xfb, 0x90, 0xe6, 0x8d, 0xe0, 0x60, 0x25, 0x90, 0xe7, 0x80, 0xb4},
{0x04, 0x27, 0xe0, 0xf5, 0xb2, 0xa3, 0xe0, 0xf5, 0xb5, 0xa3, 0xe0, 0xf5, 0xb0, 0xa3, 0xe0, 0x90},
{0xe6, 0x09, 0xf0, 0x90, 0xe7, 0xc0, 0xe5, 0xb0, 0xf0, 0x90, 0xe6, 0x8f, 0x74, 0x01, 0xf0, 0x80},
{0xcc, 0x90, 0xe7, 0xc0, 0xe5, 0xaa, 0xf0, 0x80, 0xf0, 0xff, 0xe0, 0xa3, 0x7e, 0x08, 0x13, 0x92},
{0x80, 0xc2, 0x81, 0xd2, 0x81, 0xde, 0xf7, 0xdf, 0xf1, 0x80, 0xb2, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22}
};
/*
* Switch off the card
*/
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0xe600, 0x0000, "\x1", 0x0001, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0000, 0x0000, packets[0], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0010, 0x0000, packets[1], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0020, 0x0000, packets[2], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0030, 0x0000, packets[3], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0040, 0x0000, packets[4], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0050, 0x0000, packets[5], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0060, 0x0000, packets[6], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0070, 0x0000, packets[7], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0080, 0x0000, packets[8], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x0090, 0x0000, packets[9], 0x0010, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0x00a0, 0x0000, packets[10], 0x000b, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
/*
* Switch the card back on
*/
if ((ret = usb_control_msg(udev->udev, usb_sndctrlpipe(udev->udev,0), 0xa0,
0x40, 0xe600, 0x0000, "\x0", 0x0001, 1000)))
printk("xylo_led_init_xylo_card returns %d\n", ret);
return ret;
}
/**
** @brief Called when the char device is open
** get the interface and the private data and set the private data
** to the file structure.
**
*/
static int xylo_led_open (struct inode *inode,
struct file *file)
{
struct usb_xylo_led *dev;
struct usb_interface *interface;
int minor;
minor = iminor(inode);
/*
* Get the interface for the device
*/
interface = usb_find_interface (&xylo_led_driver, minor);
if (!interface)
return -ENODEV;
/*
* Get the device's private data
*/
dev = usb_get_intfdata (interface);
if (dev == NULL) {
printk (KERN_WARNING "xylo_led_open(): can't find device for minor %d\n", minor);
return -ENODEV;
}
/*
* Set the private data to the file structure
*/
file->private_data = (void *)dev;
return 0;
}
/**
** @brief
*/
static int xylo_led_release (struct inode *inode,
struct file *file)
{
return 0;
}
/**
** @brief Called when the char device is written.
** Retrieve the user data, and send the corresponding mask to the card.
*/
static ssize_t xylo_led_write(struct file *file,
const char *buf,
size_t count,
loff_t *ppos)
{
size_t real;
real = min((size_t) BUF_SIZE, count);
if (real)
if (copy_from_user(buffer, buf, real))
return -EFAULT;
printk(KERN_DEBUG "xylo_driver: write %zu chars %s\n", real, buffer);
return real;
}
/**
** @brief Called when the char device is read
** Send the current content of the ledmask buffer
*/
static ssize_t xylo_led_read(struct file *file,
char *buf,
size_t count,
loff_t *ppos)
{
size_t real;
real = min(count, (size_t) BUF_SIZE);
if (real)
if (copy_to_user(buf, buffer, real))
return -EFAULT;
printk(KERN_DEBUG "xylo_driver: read %zu/%zu chars%s\n", real, count, buffer);
/* Changing reading position in the file */
if (*ppos == 0)
{
*ppos+=real;
return real;
}
else
{
return 0;
}
}
/**
** @brief Define the FOPS
*/
static struct file_operations xylo_led_file_operations = {
.open = xylo_led_open,
.release = xylo_led_release,
.write = xylo_led_write,
.read = xylo_led_read
};
static struct usb_class_driver xylo_led_class_driver = {
.name = "usb/xylo_led%d",
.fops = &xylo_led_file_operations,
.minor_base = 0
};
/*
** This function will be called when sys entry is read.
*/
static ssize_t
xylo_led_sys_read (struct device *dev, struct device_attribute *attr,
char *buf)
{
struct usb_interface *interface;
struct usb_xylo_led *mydev;
/*
* Retrieve the interface
*/
interface = to_usb_interface (dev);
/*
* Retrieve the device data
*/
mydev = usb_get_intfdata (interface);
/*
* Write the data to the buffer and return the length
*/
return sprintf (buf, "0x%x\n", mydev->ledmask);
}
/*
** This function will be called when sys entry is written.
*/
static ssize_t
xylo_led_sys_write (struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct usb_interface *interface;
struct usb_xylo_led *mydev;
int ret;
interface = to_usb_interface (dev);
mydev = usb_get_intfdata (interface);
/* Now set the ledmask value */
if ((ret = xylo_led_send_bulk_ledmask_buf (mydev, buf)) < 0)
return ret;
else
return count;
}
/*
* This macro generates a struct device_attribute
*/
static DEVICE_ATTR (ledmask, S_IRUGO | S_IWUGO,
xylo_led_sys_read,
xylo_led_sys_write);
/**
** @brief Called when the xylo device is probed
**
** @param interface
** @param id
**
** @returns
*/
static int xylo_led_probe (struct usb_interface *interface,
const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev (interface);
struct usb_xylo_led *xylo_led_dev;
int ret;
printk (KERN_WARNING "xylo_led_probe()\n");
/// begin fix bineau_t
/* Register char device interface */
usb_register_dev(interface, &xylo_led_class_driver);
/// end fix
xylo_led_dev = kmalloc (sizeof(struct usb_xylo_led), GFP_KERNEL);
if (xylo_led_dev == NULL) {
dev_err (&interface->dev, "Out of memory\n");
return -ENOMEM;
}
/*
* Fill private structure and save it
*/
memset (xylo_led_dev, 0, sizeof (*xylo_led_dev));
xylo_led_dev->udev = usb_get_dev(udev);
xylo_led_dev->ledmask = 0;
usb_set_intfdata (interface, xylo_led_dev);
/*
* Create the /sys entry
*/
ret = device_create_file (&interface->dev, &dev_attr_ledmask);
if (ret < 0) {
printk (KERN_WARNING "xylo_led_probe: device_create_file() error\n");
return ret;
}
/*
* Set the interface to alternate 1
*/
ret = usb_set_interface(udev, 0, 1);
if (ret < 0) {
printk (KERN_WARNING "xylo_led_probe: usb_set_interface() error\n");
return ret;
}
/*
* Launch initialisations with URBs
*/
ret = xylo_led_init_xylo_card(xylo_led_dev);
if (ret < 0)
{
printk (KERN_WARNING "xylo_led_probe: xylo_led_init_xylo_card() returns %d\n", ret);
return ret;
}
/*
* Start small animation to show that the device is recognized
*/
xylo_led_animation(xylo_led_dev);
return 0;
}
/**
** @brief Called when the xylo devie is disconnected
**
** @param interface
*/
static void xylo_led_disconnect(struct usb_interface *interface)
{
struct usb_xylo_led *dev;
dev = usb_get_intfdata(interface);
usb_deregister_dev(interface, &xylo_led_class_driver);
device_remove_file (&interface->dev, &dev_attr_ledmask);
usb_set_intfdata(interface, NULL);
kfree(dev);
dev_info(&interface->dev, "Xylo now disconnected\n");
}
static struct usb_driver xylo_led_driver = {
.name = "xylo_led",
.probe = xylo_led_probe,
.disconnect = xylo_led_disconnect,
.id_table = id_table,
};
static int __init usb_xylo_led_init(void)
{
int retval = 0;
retval = usb_register(&xylo_led_driver);
if (retval)
{
printk("usb_register failed. Error number %d", retval);
return retval;
}
return retval;
}
static void __exit usb_xylo_led_exit(void)
{
usb_deregister(&xylo_led_driver);
}
module_init (usb_xylo_led_init);
module_exit (usb_xylo_led_exit);