2 * Linux driver for TerraTec DMX 6Fire USB
4 * Device communications
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Copyright: (C) Torsten Schenk
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
25 static void usb6fire_comm_init_urb(struct comm_runtime
*rt
, struct urb
*urb
,
26 u8
*buffer
, void *context
, void(*handler
)(struct urb
*urb
))
29 urb
->transfer_buffer
= buffer
;
30 urb
->pipe
= usb_sndintpipe(rt
->chip
->dev
, COMM_EP
);
31 urb
->complete
= handler
;
32 urb
->context
= context
;
34 urb
->dev
= rt
->chip
->dev
;
37 static void usb6fire_comm_receiver_handler(struct urb
*urb
)
39 struct comm_runtime
*rt
= urb
->context
;
40 struct midi_runtime
*midi_rt
= rt
->chip
->midi
;
43 if (rt
->receiver_buffer
[0] == 0x10) /* midi in event */
45 midi_rt
->in_received(midi_rt
,
46 rt
->receiver_buffer
+ 2,
47 rt
->receiver_buffer
[1]);
50 if (!rt
->chip
->shutdown
) {
52 urb
->actual_length
= 0;
53 if (usb_submit_urb(urb
, GFP_ATOMIC
) < 0)
54 snd_printk(KERN_WARNING PREFIX
55 "comm data receiver aborted.\n");
59 static void usb6fire_comm_init_buffer(u8
*buffer
, u8 id
, u8 request
,
67 buffer
[1] = 0x05; /* length (starting at buffer[2]) */
74 buffer
[1] = 0x0b; /* length (starting at buffer[2]) */
96 static int usb6fire_comm_send_buffer(u8
*buffer
, struct usb_device
*dev
)
101 ret
= usb_interrupt_msg(dev
, usb_sndintpipe(dev
, COMM_EP
),
102 buffer
, buffer
[1] + 2, &actual_len
, HZ
);
105 else if (actual_len
!= buffer
[1] + 2)
110 static int usb6fire_comm_write8(struct comm_runtime
*rt
, u8 request
,
116 /* 13: maximum length of message */
117 buffer
= kmalloc(13, GFP_KERNEL
);
121 usb6fire_comm_init_buffer(buffer
, 0x00, request
, reg
, value
, 0x00);
122 ret
= usb6fire_comm_send_buffer(buffer
, rt
->chip
->dev
);
128 static int usb6fire_comm_write16(struct comm_runtime
*rt
, u8 request
,
129 u8 reg
, u8 vl
, u8 vh
)
134 /* 13: maximum length of message */
135 buffer
= kmalloc(13, GFP_KERNEL
);
139 usb6fire_comm_init_buffer(buffer
, 0x00, request
, reg
, vl
, vh
);
140 ret
= usb6fire_comm_send_buffer(buffer
, rt
->chip
->dev
);
146 int usb6fire_comm_init(struct sfire_chip
*chip
)
148 struct comm_runtime
*rt
= kzalloc(sizeof(struct comm_runtime
),
156 rt
->receiver_buffer
= kzalloc(COMM_RECEIVER_BUFSIZE
, GFP_KERNEL
);
157 if (!rt
->receiver_buffer
) {
166 rt
->init_urb
= usb6fire_comm_init_urb
;
167 rt
->write8
= usb6fire_comm_write8
;
168 rt
->write16
= usb6fire_comm_write16
;
170 /* submit an urb that receives communication data from device */
171 urb
->transfer_buffer
= rt
->receiver_buffer
;
172 urb
->transfer_buffer_length
= COMM_RECEIVER_BUFSIZE
;
173 urb
->pipe
= usb_rcvintpipe(chip
->dev
, COMM_EP
);
174 urb
->dev
= chip
->dev
;
175 urb
->complete
= usb6fire_comm_receiver_handler
;
178 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
180 kfree(rt
->receiver_buffer
);
182 snd_printk(KERN_ERR PREFIX
"cannot create comm data receiver.");
189 void usb6fire_comm_abort(struct sfire_chip
*chip
)
191 struct comm_runtime
*rt
= chip
->comm
;
194 usb_poison_urb(&rt
->receiver
);
197 void usb6fire_comm_destroy(struct sfire_chip
*chip
)
199 struct comm_runtime
*rt
= chip
->comm
;
201 kfree(rt
->receiver_buffer
);