-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_opbox.c
782 lines (656 loc) · 20.8 KB
/
usb_opbox.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
* Dream Cheeky USB Missile Launcher driver
*
* Copyright (C) 2007 Matthias Vallentin <vallentin@icsi.berkeley.edu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2.
*
* derived from USB Skeleton driver 2.0
* Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
*
* also inspired from LEGO USB Tower driver
* Copyright (C) 2003 David Glance <davidgsf@sourceforge.net>
* 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
*
* Notes:
* - Apparently it fails sometimes to submit the correction control URB in the
* interrupt-in-endpoint hanlder (-EINVAL).
*/
#include "usb_opbox.h"
MODULE_DEVICE_TABLE (usb, op_box_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 op_box_driver;
static inline void op_box_debug_data(const char *function, int size, const unsigned char *data)
{
int i;
if ((debug_level & DEBUG_LEVEL_DEBUG) == DEBUG_LEVEL_DEBUG) {
printk(KERN_DEBUG "[debug] %s: length = %d, data = ",
function, size);
for (i = 0; i < size; ++i)
printk("%.2x ", data[i]);
printk("\n");
}
}
static void op_box_abort_transfers(struct usb_opbox *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;
}
/* Shutdown transfer */
if (dev->int_in_running) {
dev->int_in_running = 0;
mb();
if (dev->int_in_urb)
usb_kill_urb(dev->int_in_urb);
}
if (dev->ctrl_urb)
usb_kill_urb(dev->ctrl_urb);
}
static inline void op_box_delete(struct usb_opbox *dev)
{
op_box_abort_transfers(dev);
/* Free data structures. */
if (dev->int_in_urb)
usb_free_urb(dev->int_in_urb);
if (dev->ctrl_urb)
usb_free_urb(dev->ctrl_urb);
kfree(dev->int_in_buffer);
kfree(dev->ctrl_buffer);
kfree(dev->ctrl_dr);
kfree(dev);
}
static void op_box_ctrl_callback(struct urb *urb)
{
DBG_INFO("op_box_ctrl_callback");
struct usb_opbox *dev = urb->context;
dev->correction_required = 0; /* TODO: do we need race protection? */
}
static void op_box_int_in_callback(struct urb *urb)
{
struct usb_opbox *dev = urb->context;
int retval;
DBG_INFO("op_box_int_in_callback");
op_box_debug_data(__FUNCTION__, urb->actual_length, urb->transfer_buffer);
if (urb->status) {
if (urb->status == -ENOENT ||
urb->status == -ECONNRESET ||
urb->status == -ESHUTDOWN) {
return;
} else {
DBG_ERR("non-zero urb status (%d)", urb->status);
goto resubmit; /* Maybe we can recover. */
}
}
if (urb->actual_length > 0) {
spin_lock(&dev->cmd_spinlock); //spinlock itself will guarantee the global lock, so it will guarantee that
// there is only one thread-of-control within the region(s) protected by that lock
DBG_INFO("init callback actual_length: %d",urb->actual_length);
// for specefic device
// if (dev->int_in_buffer[0] & ML_MAX_UP && dev->command & ML_UP) {
// dev->command &= ~ML_UP;
// dev->correction_required = 1;
// } else if (dev->int_in_buffer[0] & ML_MAX_DOWN &&
// dev->command & ML_DOWN) {
// dev->command &= ~ML_DOWN;
// dev->correction_required = 1;
// }
// if (dev->int_in_buffer[1] & ML_MAX_LEFT
// && dev->command & ML_LEFT) {
// dev->command &= ~ML_LEFT;
// dev->correction_required = 1;
// } else if (dev->int_in_buffer[1] & ML_MAX_RIGHT
// && dev->command & ML_RIGHT) {
// dev->command &= ~ML_RIGHT;
// dev->correction_required = 1;
// }
// if (dev->correction_required) {
// dev->ctrl_buffer[0] = dev->command;
// spin_unlock(&dev->cmd_spinlock);
// retval = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC);
// if (retval) {
// DBG_ERR("submitting correction control URB failed (%d)",
// retval);
// }
// } else {
// spin_unlock(&dev->cmd_spinlock);
// }
spin_unlock(&dev->cmd_spinlock);
}
resubmit:
/* Resubmit if we're still running. */
if (dev->int_in_running && dev->udev) {
retval = usb_submit_urb(dev->int_in_urb, GFP_ATOMIC);
if (retval) {
DBG_ERR("resubmitting urb failed (%d)", retval);
dev->int_in_running = 0;
}
}
}
static int op_box_open(struct inode *inode, struct file *file)
{
struct usb_opbox *dev = NULL;
struct usb_interface *interface;
int subminor;
int retval = 0;
DBG_INFO("op_box_open Open device");
subminor = iminor(inode);
mutex_lock(&disconnect_mutex);
interface = usb_find_interface(&op_box_driver, subminor);
if (! interface) {
DBG_ERR("can't find device for minor %d", subminor);
retval = -ENODEV;
goto exit;
}
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 exit;
}
/* Increment our usage count for the device. */
++dev->open_count;
if (dev->open_count > 1)
DBG_DEBUG("open_count = %d", dev->open_count);
/* Initialize interrupt URB. */
// is a helper function to properly initialize a urb to be sent to an interrupt endpoint of a USB device
// usb_fill_int_urb(dev->int_in_urb, dev->udev,
// usb_rcvintpipe(dev->udev, dev->int_in_endpoint->bEndpointAddress), // Specifies an interrupt IN endpoint for the specified USB device with the specified endpoint number
// dev->int_in_buffer,
// le16_to_cpu(dev->int_in_endpoint->wMaxPacketSize), // convert a value to whatever the CPU uses
// op_box_int_in_callback,
// dev,
// dev->int_in_endpoint->bInterval); // set interval zero because is not isochronous.
// dev->int_in_running = 1;
// mb();
// retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL);
// if (retval) {
// DBG_ERR("submitting int urb failed (%d)", retval);
// dev->int_in_running = 0;
// --dev->open_count;
// goto unlock_exit;
// }
/* Save our object in the file's private structure. */
file->private_data = dev;
DBG_INFO("op_box_open Opened successfully");
unlock_exit:
up(&dev->sem);
exit:
mutex_unlock(&disconnect_mutex);
return retval;
}
static int op_box_release(struct inode *inode, struct file *file)
{
struct usb_opbox *dev = NULL;
int retval = 0;
DBG_INFO("Release driver");
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 op_box_delete frees dev. */
op_box_delete(dev);
goto exit;
}
if (dev->open_count > 1)
DBG_DEBUG("open_count = %d", dev->open_count);
op_box_abort_transfers(dev);
--dev->open_count;
unlock_exit:
up(&dev->sem);
exit:
return retval;
}
static ssize_t op_box_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
{
struct usb_opbox *dev;
int retval = 0;
char *dmadata;
DBG_INFO("op_box_write");
dev = (struct usb_opbox *)file->private_data;
/* Verify that the device wasn't unplugged. */
if (! dev->udev) {
retval = -ENODEV;
DBG_ERR("No device or device unplugged (%d)", retval);
goto exit;
}
if(buffer[0] == WRITE_CONTROL) {
DBG_INFO("op_box_write is WRITE_CONTROL");
dmadata = kmalloc(count-2, GFP_KERNEL);
if (!dmadata) {
return -ENOMEM;
}
// buffer[0] is flag and buffer[1] is control register
int i=2;
for(i=2; i<count; i++) {
dmadata[i-2] = buffer[i];
}
// dmadata[0] = 0x01 ;
// dmadata[1] = 0x00;
retval = usb_control_msg(dev->udev,
usb_sndctrlpipe(dev->udev, 0),
OPBOX_CTRL_REGISTER_WRITE_VENDOER_REQUEST,
OPBOX_CTRL_REGISTER_WRITE_REQUEST_TYPE,
OPBOX_CTRL_REGISTER_WRITE_VALUE,
buffer[1],
(void *)dmadata,
count-2,
2000);
if (retval < 0) {
DBG_ERR("usb_control_msg failed (%d)", retval);
} else {
DBG_INFO("usb_control_msg writed succesfully");
/* We should have written only one byte. */
retval = count-2;
}
kfree(dmadata);
} else if(buffer[0] == WRITE_DIRECT_RESET) {
DBG_INFO("op_box_write is WRITE_DIRECT");
dmadata = kmalloc(count-2, GFP_KERNEL);
if (!dmadata) {
return -ENOMEM;
}
retval = usb_control_msg(dev->udev,
usb_sndctrlpipe(dev->udev, 0),
buffer[1],
buffer[2],
0x00,
0x00,
(void *)dmadata,
0,
2000);
if (retval < 0) {
DBG_ERR("WRITE_DIRECT failed (%d)", retval);
} else {
DBG_INFO("WRITE_DIRECT writed succesfully");
/* We should have written only one byte. */
retval = 0;
}
kfree(dmadata);
} else if(buffer[0] == WRITE_DIRECT_PULSE_AMPLITUDE) {
DBG_INFO("op_box_write is WRITE_DIRECT_PULSE_AMPLITUDE");
dmadata = kmalloc(1, GFP_KERNEL);
if (!dmadata) {
return -ENOMEM;
}
retval = usb_control_msg(dev->udev,
usb_sndctrlpipe(dev->udev, 0),
PULSE_AMPLITUDE,
DIRECT_OUT_REQUEST_TYPE,
buffer[1],
0x00,
(void *)dmadata,
0,
2000);
if (retval < 0) {
DBG_ERR("WRITE_DIRECT_PULSE_AMPLITUDE failed (%d)", retval);
} else {
DBG_INFO("WRITE_DIRECT_PULSE_AMPLITUDE writed succesfully");
/* We should have written only one byte. */
retval = 0;
}
kfree(dmadata);
}
exit:
return retval;
}
static ssize_t op_box_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
{
struct usb_opbox *dev;
int retval = 0;
int read_cnt;
char *dmadata;
dev = (struct usb_opbox *)file->private_data;
DBG_INFO("op_box_read");
/* Verify that the device wasn't unplugged. */
if (!dev->udev) {
retval = -ENODEV;
DBG_ERR("No device or device unplugged (%d)", retval);
goto exit;
}
if(buffer[0] == READ_CONTROL) {
DBG_INFO("buffer[0] : %02x", buffer[0]);
DBG_INFO("buffer[1] : %02x", buffer[1]);
DBG_INFO("op_box_read control read");
dmadata = kmalloc(count, GFP_KERNEL);
if (!dmadata) {
DBG_INFO("op_box_read dmadata wrong initialize");
return -ENOMEM;
}
retval = usb_control_msg(dev->udev,
usb_rcvctrlpipe(dev->udev, 0),
OPBOX_CTRL_REGISTER_READ_VENDOER_REQUEST,
OPBOX_CTRL_REGISTER_READ_REQUEST_TYPE,
OPBOX_CTRL_REGISTER_READ_VALUE,
buffer[1],
(void *)dmadata, // &dmadata,
sizeof(count),
2000);
if (retval < 0) {
DBG_ERR("usb_control_msg failed (%d)", retval);
} else {
DBG_INFO("op_box_read usb_control_msg readed succefully");
if (copy_to_user(buffer, dmadata, count)) {
retval = -EFAULT;
} else {
retval = count;
}
// memset(&buffer, 0, count);
// memcpy(buffer, dmadata, count);
}
kfree(dmadata);
DBG_INFO("buffer[0] : %02x", (unsigned char)buffer[0]);
DBG_INFO("buffer[1] : %02x", (unsigned char)buffer[1]);
} else if( buffer[0] == READ_DIRECT ) {
DBG_INFO("op_box_read READ_DIRECT");
dmadata = kmalloc(count, GFP_KERNEL);
if (!dmadata) {
DBG_INFO("op_box_read dmadata wrong initialize");
return -ENOMEM;
}
retval = usb_control_msg(dev->udev,
usb_rcvctrlpipe(dev->udev, 0),
DIRECT_FRAME_READY,
DIRECT_IN_REQUEST_TYPE,
0x00,
0x00,
(void *)dmadata, // &dmadata,
sizeof(count),
2000);
if (retval < 0) {
DBG_ERR("usb_control_msg failed (%d)", retval);
} else {
DBG_INFO("op_box_read usb_control_msg readed succefully");
if (copy_to_user(buffer, dmadata, count)) {
retval = -EFAULT;
} else {
retval = count;
}
// memset(&buffer, 0, count);
// memcpy(buffer, dmadata, count);
}
kfree(dmadata);
DBG_INFO("after readbuffer[0] : %02x", (unsigned char)buffer[0]);
} else if( buffer[0] == READ_DATA ){
DBG_INFO("op_box_read data read");
/* do a blocking bulk read to get data from the device */
retval = usb_bulk_msg(dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
dev->bulk_in_buffer,
min(dev->bulk_in_size, count),
&read_cnt, 2000);
if (!retval) {
if (copy_to_user(buffer, dev->bulk_in_buffer, min(count, read_cnt))) {
retval = -EFAULT;
} else {
DBG_INFO("op_box_read bulk data readed successfully");
retval = min(count, read_cnt);
}
} else {
DBG_ERR("read msg failed (%d)", retval);
}
}
exit:
return retval;
}
static ssize_t show_switches(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_opbox *opbox = usb_get_intfdata(intf);
return sprintf(buf, "%d\n", opbox->testData);
}
static DEVICE_ATTR( switches, S_IRUGO, show_switches, NULL );
// static DEVICE_ATTR(testData, S_IRUGO, show_test_data, NULL);
static struct file_operations opbox_fops = {
.owner = THIS_MODULE,
.write = op_box_write,
.open = op_box_open,
.release = op_box_release,
.read = op_box_read,
};
static struct usb_class_driver opbox_class = {
.name = "usb/opbox%d",
.fops = &opbox_fops,
.minor_base = ML_MINOR_BASE,
};
static int op_box_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev(interface);
struct usb_opbox *dev = NULL;
struct usb_host_interface *iface_desc;
struct usb_endpoint_descriptor *endpoint;
int i, int_end_size;
int retval = -ENODEV;
DBG_INFO("Probe missile launcher");
if (! udev) {
DBG_ERR("udev is NULL");
goto exit;
}
dev = kzalloc(sizeof(struct usb_opbox), GFP_KERNEL);
if (! dev) {
DBG_ERR("cannot allocate memory for struct usb_opbox");
retval = -ENOMEM;
goto exit;
}
// dev->command = ML_STOP;
sema_init(&dev->sem, 1);
spin_lock_init(&dev->cmd_spinlock);
dev->udev = udev;
dev->interface = interface;
iface_desc = interface->cur_altsetting;
printk(KERN_INFO "OPBox i/f %d now plugged in: (%04X:%04X)\n",
iface_desc->desc.bInterfaceNumber,
id->idVendor, id->idProduct); // USB_DEVICE_INTERFACE_NUMBER
printk(KERN_INFO "ID->bNumEndpoints: %02X\n",
iface_desc->desc.bNumEndpoints);
//bInterfaceClass=0xFF means that the interface is vendor-specific, and the protocol to talk to it is proprietary
printk(KERN_INFO "ID->bInterfaceClass: %02X\n",
iface_desc->desc.bInterfaceClass); // describe a usb device with a specific interface class
/* Set up interrupt endpoint information. */
for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
endpoint = &iface_desc->endpoint[i].desc;
printk(KERN_INFO "ED[%d]->bEndpointAddress: 0x%02X\n",
i, endpoint->bEndpointAddress);
printk(KERN_INFO "ED[%d]->bmAttributes: 0x%02X\n",
i, endpoint->bmAttributes); // This is the type of endpoint.USB_ENDPOINT_XFER_ISOC , USB_ENDPOINT_XFER_BULK , or of type USB_ENDPOINT_XFER_INT
printk(KERN_INFO "ED[%d]->wMaxPacketSize: 0x%04X (%d)\n",
i, endpoint->wMaxPacketSize,
endpoint->wMaxPacketSize);
if (!dev->bulk_in_endpointAddr &&
(endpoint->bEndpointAddress & USB_DIR_IN) &&
((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
== USB_ENDPOINT_XFER_BULK)) {
printk(KERN_INFO "op_box driver found a bulk in endpoint\n");
/* we found a bulk in endpoint */
// buffer_size = endpoint->wMaxPacketSize;
dev->bulk_in_size = endpoint->wMaxPacketSize;
dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
dev->bulk_in_buffer = kmalloc(endpoint->wMaxPacketSize, GFP_KERNEL);
if (!dev->bulk_in_buffer) {
printk(KERN_INFO "err :Could not allocate bulk_in_buffer\n");
goto error;
}
}
if (!dev->bulk_out_endpointAddr &&
!(endpoint->bEndpointAddress & USB_DIR_IN) &&
((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
== USB_ENDPOINT_XFER_BULK)) {
printk(KERN_INFO "op_box driver found a bulk out endpoint\n");
/* we found a bulk out endpoint */
dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
}
}
// -----------------------
dev->testData = 24;
//-------------------------
// if (! dev->int_in_endpoint) {
// DBG_ERR("could not find interrupt in endpoint");
// goto error;
// }
// int_end_size = le16_to_cpu(dev->int_in_endpoint->wMaxPacketSize);
// dev->int_in_buffer = kmalloc(int_end_size, GFP_KERNEL);
// if (! dev->int_in_buffer) {
// DBG_ERR("could not allocate int_in_buffer");
// retval = -ENOMEM;
// goto error;
// }
// dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
// if (! dev->int_in_urb) {
// DBG_ERR("could not allocate int_in_urb");
// retval = -ENOMEM;
// goto error;
// }
/* Set up the control URB. */
dev->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL); // zero : If you do not want to create an isochronous urb,
// this variable should be set to 0
if (! dev->ctrl_urb) {
DBG_ERR("could not allocate ctrl_urb");
retval = -ENOMEM;
goto error;
}
dev->ctrl_buffer = kzalloc(OPBOX_CTRL_REGISTER_WRITE_REQUEST_LENGTH, 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;
}
// Power Enable
dev->ctrl_dr->bRequestType = OPBOX_CTRL_REGISTER_WRITE_VENDOER_REQUEST;
dev->ctrl_dr->bRequest = OPBOX_CTRL_REGISTER_WRITE_REQUEST_TYPE;
dev->ctrl_dr->wValue = cpu_to_le16(0x00);
dev->ctrl_dr->wIndex = cpu_to_le16(POWER_CTLR);
dev->ctrl_dr->wLength = cpu_to_le16(OPBOX_CTRL_REGISTER_WRITE_REQUEST_LENGTH);
dev->ctrl_buffer[0] = 0x01;
dev->ctrl_buffer[1] = 0x00;
usb_fill_control_urb(dev->ctrl_urb, dev->udev,
usb_sndctrlpipe(dev->udev, 0), // zero is control endpoint
(unsigned char *)dev->ctrl_dr,
dev->ctrl_buffer,
OPBOX_CTRL_REGISTER_WRITE_REQUEST_LENGTH,
op_box_ctrl_callback,
dev);
int result = usb_submit_urb( dev->ctrl_urb, GFP_KERNEL ) ;
if(result < 0) {
printk( KERN_INFO "control urb submitted wrong %d", result );
} else {
printk( KERN_INFO "control urb submitted correctly %d", result );
}
/* Retrieve a serial. */
if (! usb_string(udev, udev->descriptor.iSerialNumber,
dev->serial_number, sizeof(dev->serial_number))) {
DBG_ERR("could not retrieve serial number");
goto error;
}
/* Save our data pointer in this interface device. */
usb_set_intfdata(interface, dev);
device_create_file(&interface->dev, &dev_attr_switches);
/* We can register the device now, as it is ready. */
retval = usb_register_dev(interface, &opbox_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("USB missile launcher now attached to /dev/opbox%d, retval: %d",
interface->minor - ML_MINOR_BASE , retval);
exit:
return retval;
error:
op_box_delete(dev);
return retval;
}
static void op_box_disconnect(struct usb_interface *interface)
{
struct usb_opbox *dev;
int minor;
mutex_lock(&disconnect_mutex); /* Not interruptible */
dev = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
device_remove_file(&interface->dev, &dev_attr_switches);
down(&dev->sem); /* Not interruptible */
minor = dev->minor;
/* Give back our minor. */
usb_deregister_dev(interface, &opbox_class);
/* If the device is not opened, then we clean up right now. */
if (! dev->open_count) {
up(&dev->sem);
op_box_delete(dev);
} else {
dev->udev = NULL;
up(&dev->sem);
}
mutex_unlock(&disconnect_mutex);
DBG_INFO("USB missile launcher /dev/opbox%d now disconnected",
minor - ML_MINOR_BASE);
}
static struct usb_driver op_box_driver = {
.name = "op_box_driver",
.id_table = op_box_table,
.probe = op_box_probe,
.disconnect = op_box_disconnect,
};
static int __init usb_opbox_init(void)
{
int result;
DBG_INFO("Register driver");
result = usb_register(&op_box_driver);
if (result) {
DBG_ERR("registering driver failed");
} else {
DBG_INFO("driver registered successfully");
}
return result;
}
static void __exit usb_opbox_exit(void)
{
usb_deregister(&op_box_driver);
DBG_INFO("module deregistered");
}
module_init(usb_opbox_init);
module_exit(usb_opbox_exit);
MODULE_AUTHOR("Matthias Vallentin");
MODULE_LICENSE("GPL");