2 #include <ddekit/usb.h>
3 #include <ddekit/memory.h>
4 #include <ddekit/minix/msg_queue.h>
7 struct ddekit_usb_dev
{
9 unsigned int interfaces
;
11 struct ddekit_usb_dev
*next
;
12 struct ddekit_usb_dev
*prev
;
15 struct ddekit_usb_dev dev_list_head
= {
16 .next
= &dev_list_head
,
17 .prev
= &dev_list_head
,
20 static struct ddekit_usb_driver
*d_usb_driver
;
22 static void _ddekit_usb_completion(struct usb_urb
*mx
);
23 static void _ddekit_usb_connect( unsigned int dev_id
, unsigned int
25 static void _ddekit_usb_disconnect(unsigned dev_id
);
27 struct usb_driver mx_usb_driver
= {
28 .urb_completion
= _ddekit_usb_completion
,
29 .connect_device
= _ddekit_usb_connect
,
30 .disconnect_device
= _ddekit_usb_disconnect
33 /*****************************************************************************
34 * _ddekit_usb_completion *
35 ****************************************************************************/
36 static void _ddekit_usb_completion(struct usb_urb
*mx_urb
)
39 struct ddekit_usb_urb
*d_urb
= (struct ddekit_usb_urb
*) mx_urb
->priv
;
41 /* XXX: copy stuff back into d_urb */
43 d_urb
->status
= mx_urb
->status
;
44 d_urb
->error_count
= mx_urb
->interval
;
45 d_urb
->transfer_flags
= mx_urb
->error_count
;
46 d_urb
->actual_length
= mx_urb
->actual_length
;
47 d_urb
->ddekit_priv
= NULL
;
49 if (mx_urb
->type
== USB_TRANSFER_CTL
) {
50 memcpy(d_urb
->setup_packet
, mx_urb
->setup_packet
, 8);
53 if (mx_urb
->type
== USB_TRANSFER_ISO
) {
54 d_urb
->start_frame
= mx_urb
->start_frame
;
56 memcpy(d_urb
->iso_desc
, mx_urb
->buffer
+ d_urb
->size
,
57 d_urb
->number_of_packets
* sizeof(struct usb_iso_packet_desc
));
60 memcpy(d_urb
->data
, mx_urb
->buffer
, d_urb
->size
);
63 ddekit_simple_free(mx_urb
);
67 d_usb_driver
->completion(d_urb
->priv
);
71 /*****************************************************************************
72 * _ddekit_usb_connect *
73 ****************************************************************************/
74 static void _ddekit_usb_connect(unsigned int dev_id
, unsigned int interfaces
)
76 struct ddekit_usb_dev
*d_dev
= (struct ddekit_usb_dev
*)
77 ddekit_simple_malloc(sizeof(struct ddekit_usb_dev
));
81 d_dev
->interfaces
= interfaces
;
85 d_dev
->next
= dev_list_head
.next
;
86 d_dev
->prev
= &dev_list_head
;
88 dev_list_head
.next
= d_dev
;
89 d_dev
->next
->prev
= d_dev
;
90 d_usb_driver
->connect(d_dev
, interfaces
);
93 /*****************************************************************************
94 * _ddekit_usb_disconnect *
95 ****************************************************************************/
96 void _ddekit_usb_disconnect(unsigned dev_id
)
99 struct ddekit_usb_dev
*it
;
100 struct ddekit_usb_dev
*d_dev
= NULL
;
103 for (it
= dev_list_head
.next
; it
!= &dev_list_head
; it
= it
->next
) {
104 if (it
->id
== dev_id
) {
114 d_usb_driver
->disconnect(d_dev
);
117 /*****************************************************************************
118 * ddekit_usb_dev_set_data *
119 ****************************************************************************/
120 int ddekit_usb_dev_set_data(struct ddekit_usb_dev
*dev
, void *data
)
126 /*****************************************************************************
127 * ddekit_usb_dev_get_data *
128 ****************************************************************************/
129 void *ddekit_usb_dev_get_data(struct ddekit_usb_dev
*dev
)
134 /*****************************************************************************
135 * ddekit_usb_submit_urb *
136 ****************************************************************************/
137 int ddekit_usb_submit_urb(struct ddekit_usb_urb
*d_urb
)
140 unsigned urb_size
= USB_URBSIZE(d_urb
->size
, d_urb
->number_of_packets
);
141 /* create mx urb out of d_urb */
142 struct usb_urb
*mx_urb
= (struct usb_urb
*)
143 ddekit_simple_malloc(urb_size
);
144 mx_urb
->urb_size
= urb_size
;
146 mx_urb
->dev_id
= d_urb
->dev
->id
;
147 mx_urb
->type
= d_urb
->type
;
148 mx_urb
->endpoint
= d_urb
->endpoint
;
149 mx_urb
->direction
= d_urb
->direction
;
150 mx_urb
->interval
= d_urb
->interval
;
151 mx_urb
->transfer_flags
= d_urb
->transfer_flags
;
152 mx_urb
->size
= d_urb
->size
;
153 mx_urb
->priv
= d_urb
;
155 if (mx_urb
->type
== USB_TRANSFER_CTL
) {
156 memcpy(mx_urb
->setup_packet
, d_urb
->setup_packet
, 8);
159 if (mx_urb
->type
== USB_TRANSFER_ISO
) {
160 mx_urb
->number_of_packets
= d_urb
->number_of_packets
;
161 mx_urb
->start_frame
= d_urb
->start_frame
;
162 memcpy(mx_urb
->buffer
+ d_urb
->size
, d_urb
->iso_desc
,
163 d_urb
->number_of_packets
* sizeof(struct usb_iso_packet_desc
));
165 memcpy(mx_urb
->buffer
, d_urb
->data
, d_urb
->size
);
167 d_urb
->ddekit_priv
= mx_urb
;
170 res
= usb_send_urb(mx_urb
);
174 /*****************************************************************************
175 * ddekit_usb_cancle_urb *
176 ****************************************************************************/
177 int ddekit_usb_cancle_urb(struct ddekit_usb_urb
*d_urb
)
181 /* get the associated mx_urb */
182 struct usb_urb
*mx_urb
= (struct usb_urb
*) d_urb
->ddekit_priv
;
184 res
= usb_cancle_urb(mx_urb
);
189 static void _ddekit_usb_thread()
191 struct ddekit_minix_msg_q
*mq
= ddekit_minix_create_msg_q(USB_BASE
,
197 ddekit_minix_rcv(mq
, &m
, &ipc_status
);
198 usb_handle_msg(&mx_usb_driver
, &m
);
203 /*****************************************************************************
205 ****************************************************************************/
207 (struct ddekit_usb_driver
*drv
,
208 ddekit_usb_malloc_fn
*unused
,
209 ddekit_usb_free_fn
*_unused
)
211 /* start usb_thread */
214 _ddekit_usb_thread();