99
1010#include "qdl.h"
1111
12+ #define container_of (ptr , typecast , member ) ({ \
13+ void *_ptr = (void *)(ptr); \
14+ ((typecast *)(_ptr - offsetof(typecast, member))); })
15+
1216#define DEFAULT_OUT_CHUNK_SIZE (1024 * 1024)
1317
1418/*
2125#define LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK LIBUSB_TRANSFER_TYPE_BULK
2226#endif
2327
24- static bool qdl_match_usb_serial (struct libusb_device_handle * handle , const char * serial ,
28+ static bool usb_match_usb_serial (struct libusb_device_handle * handle , const char * serial ,
2529 const struct libusb_device_descriptor * desc )
2630{
2731 char buf [128 ];
@@ -48,7 +52,7 @@ static bool qdl_match_usb_serial(struct libusb_device_handle *handle, const char
4852 return strcmp (p , serial ) == 0 ;
4953}
5054
51- static int qdl_try_open (libusb_device * dev , struct qdl_device * qdl , const char * serial )
55+ static int usb_try_open (libusb_device * dev , struct qdl_device_usb * qdl , const char * serial )
5256{
5357 const struct libusb_endpoint_descriptor * endpoint ;
5458 const struct libusb_interface_descriptor * ifc ;
@@ -122,7 +126,7 @@ static int qdl_try_open(libusb_device *dev, struct qdl_device *qdl, const char *
122126 continue ;
123127 }
124128
125- if (!qdl_match_usb_serial (handle , serial , & desc )) {
129+ if (!usb_match_usb_serial (handle , serial , & desc )) {
126130 libusb_close (handle );
127131 continue ;
128132 }
@@ -160,10 +164,11 @@ static int qdl_try_open(libusb_device *dev, struct qdl_device *qdl, const char *
160164 return !!qdl -> usb_handle ;
161165}
162166
163- int qdl_open (struct qdl_device * qdl , const char * serial )
167+ static int usb_open (struct qdl_device * qdl , const char * serial )
164168{
165169 struct libusb_device * * devs ;
166170 struct libusb_device * dev ;
171+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
167172 bool wait_printed = false;
168173 bool found = false;
169174 ssize_t n ;
@@ -182,7 +187,7 @@ int qdl_open(struct qdl_device *qdl, const char *serial)
182187 for (i = 0 ; devs [i ]; i ++ ) {
183188 dev = devs [i ];
184189
185- ret = qdl_try_open (dev , qdl , serial );
190+ ret = usb_try_open (dev , qdl_usb , serial );
186191 if (ret == 1 ) {
187192 found = true;
188193 break ;
@@ -205,38 +210,42 @@ int qdl_open(struct qdl_device *qdl, const char *serial)
205210 return -1 ;
206211}
207212
208- void qdl_close (struct qdl_device * qdl )
213+ static void usb_close (struct qdl_device * qdl )
209214{
210- libusb_close (qdl -> usb_handle );
215+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
216+
217+ libusb_close (qdl_usb -> usb_handle );
211218 libusb_exit (NULL );
212219}
213220
214- int qdl_read (struct qdl_device * qdl , void * buf , size_t len , unsigned int timeout )
221+ static int usb_read (struct qdl_device * qdl , void * buf , size_t len , unsigned int timeout )
215222{
223+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
216224 int actual ;
217225 int ret ;
218226
219- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> in_ep , buf , len , & actual , timeout );
227+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> in_ep , buf , len , & actual , timeout );
220228 if ((ret != 0 && ret != LIBUSB_ERROR_TIMEOUT ) ||
221229 (ret == LIBUSB_ERROR_TIMEOUT && actual == 0 ))
222230 return -1 ;
223231
224232 return actual ;
225233}
226234
227- int qdl_write (struct qdl_device * qdl , const void * buf , size_t len )
235+ static int usb_write (struct qdl_device * qdl , const void * buf , size_t len )
228236{
229237 unsigned char * data = (unsigned char * ) buf ;
238+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
230239 unsigned int count = 0 ;
231240 size_t len_orig = len ;
232241 int actual ;
233242 int xfer ;
234243 int ret ;
235244
236245 while (len > 0 ) {
237- xfer = (len > qdl -> out_chunk_size ) ? qdl -> out_chunk_size : len ;
246+ xfer = (len > qdl_usb -> out_chunk_size ) ? qdl_usb -> out_chunk_size : len ;
238247
239- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> out_ep , data ,
248+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> out_ep , data ,
240249 xfer , & actual , 1000 );
241250 if ((ret != 0 && ret != LIBUSB_ERROR_TIMEOUT ) ||
242251 (ret == LIBUSB_ERROR_TIMEOUT && actual == 0 )) {
@@ -249,8 +258,8 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
249258 data += actual ;
250259 }
251260
252- if (len_orig % qdl -> out_maxpktsize == 0 ) {
253- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> out_ep , NULL ,
261+ if (len_orig % qdl_usb -> out_maxpktsize == 0 ) {
262+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> out_ep , NULL ,
254263 0 , & actual , 1000 );
255264 if (ret < 0 )
256265 return -1 ;
@@ -259,7 +268,25 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
259268 return count ;
260269}
261270
262- void qdl_set_out_chunk_size (struct qdl_device * qdl , long size )
271+ static void usb_set_out_chunk_size (struct qdl_device * qdl , long size )
263272{
264- qdl -> out_chunk_size = size ;
273+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
274+
275+ qdl_usb -> out_chunk_size = size ;
265276}
277+
278+ struct qdl_device * usb_init (void )
279+ {
280+ struct qdl_device * qdl = malloc (sizeof (struct qdl_device_usb ));
281+ if (!qdl )
282+ return NULL ;
283+
284+ qdl -> dev_type = QDL_DEVICE_USB ;
285+ qdl -> open = usb_open ;
286+ qdl -> read = usb_read ;
287+ qdl -> write = usb_write ;
288+ qdl -> close = usb_close ;
289+ qdl -> set_out_chunk_size = usb_set_out_chunk_size ;
290+
291+ return qdl ;
292+ }
0 commit comments