forked from tietomaakari/ibuddy-lkm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbcomm.c
executable file
·71 lines (60 loc) · 2.01 KB
/
usbcomm.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
#include "ibuddy.h"
static unsigned int usb_timeout = 0;
static int usb_send( struct usb_device* udev, void* data, int len );
/* ---------------------------------------------------------------- */
void ibuddy_usb_setup( unsigned int timeout )
{
usb_timeout = timeout;
}
/* ---------------------------------------------------------------- */
static unsigned char* reset_data = "\x22\x09\x00\x02\x01\x00\x00\x00";
static void ibuddy_setup( struct ibuddy_dev* dev )
{
int retval = usb_send( dev->udev, reset_data, 8 );
if( retval )
ERROR("usb_send in reset failed (%d)\n", retval);
}
/* ---------------------------------------------------------------- */
static unsigned char* cmd_data = "\x55\x53\x42\x43\x00\x40\x02\xFF";
void ibuddy_cmd( struct ibuddy_dev* dev, uint8_t cmd )
{
unsigned char* data = kmemdup( cmd_data, 8, GFP_KERNEL );
if( !data ) {
ERROR( "ibuddy_cmd out of memory\n" );
return;
}
data[7] = cmd;
ibuddy_setup( dev );
if (usb_send( dev->udev, data, 8 ))
ERROR("usb_send in ibuddy_cmd failed\n");
kfree( data );
}
/* ---------------------------------------------------------------- */
static int usb_send( struct usb_device* udev, void* data, int len )
{
int retval;
/* move the data to DMA capable area */
void* msg = kmemdup( data, len, GFP_KERNEL | __GFP_DMA );
if( !msg ) {
ERROR( "out of memory\n" );
return -ENOMEM;
}
/* synchronous control message (wait for completion) */
retval = usb_control_msg( udev,
usb_sndctrlpipe( udev, 0 ) /* 0 endpoint */,
/* http://www.beyondlogic.org/usbnutshell/usb6.shtml */
0x09, /* request == SET_CONFIGURATION */
0x21, /* req type */
/* 00100001
D0..4 = req type = interface
D6..5 = type = class
D7 = direction = host to device
*/
0x02, /* value */
0x01, /* index */
msg, /* data */
len, /* size */
usb_timeout /* timeout in ms, 0 = forever */ );
kfree( msg );
return (retval<0) ? retval : 0;
} /* usb_send */