1010
1111#include "qdl.h"
1212
13+ #define container_of (ptr , typecast , member ) ({ \
14+ void *_ptr = (void *)(ptr); \
15+ ((typecast *)(_ptr - offsetof(typecast, member))); })
16+
1317#define DEFAULT_OUT_CHUNK_SIZE (1024 * 1024)
1418
1519/*
2226#define LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK LIBUSB_TRANSFER_TYPE_BULK
2327#endif
2428
25- static bool qdl_match_usb_serial (struct libusb_device_handle * handle , const char * serial ,
29+ static bool usb_match_usb_serial (struct libusb_device_handle * handle , const char * serial ,
2630 const struct libusb_device_descriptor * desc )
2731{
2832 char buf [128 ];
@@ -49,7 +53,7 @@ static bool qdl_match_usb_serial(struct libusb_device_handle *handle, const char
4953 return strcmp (p , serial ) == 0 ;
5054}
5155
52- static int qdl_try_open (libusb_device * dev , struct qdl_device * qdl , const char * serial )
56+ static int usb_try_open (libusb_device * dev , struct qdl_device_usb * qdl , const char * serial )
5357{
5458 const struct libusb_endpoint_descriptor * endpoint ;
5559 const struct libusb_interface_descriptor * ifc ;
@@ -123,7 +127,7 @@ static int qdl_try_open(libusb_device *dev, struct qdl_device *qdl, const char *
123127 continue ;
124128 }
125129
126- if (!qdl_match_usb_serial (handle , serial , & desc )) {
130+ if (!usb_match_usb_serial (handle , serial , & desc )) {
127131 libusb_close (handle );
128132 continue ;
129133 }
@@ -161,10 +165,11 @@ static int qdl_try_open(libusb_device *dev, struct qdl_device *qdl, const char *
161165 return !!qdl -> usb_handle ;
162166}
163167
164- int qdl_open (struct qdl_device * qdl , const char * serial )
168+ static int usb_open (struct qdl_device * qdl , const char * serial )
165169{
166170 struct libusb_device * * devs ;
167171 struct libusb_device * dev ;
172+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
168173 bool wait_printed = false;
169174 bool found = false;
170175 ssize_t n ;
@@ -183,7 +188,7 @@ int qdl_open(struct qdl_device *qdl, const char *serial)
183188 for (i = 0 ; devs [i ]; i ++ ) {
184189 dev = devs [i ];
185190
186- ret = qdl_try_open (dev , qdl , serial );
191+ ret = usb_try_open (dev , qdl_usb , serial );
187192 if (ret == 1 ) {
188193 found = true;
189194 break ;
@@ -206,38 +211,42 @@ int qdl_open(struct qdl_device *qdl, const char *serial)
206211 return -1 ;
207212}
208213
209- void qdl_close (struct qdl_device * qdl )
214+ static void usb_close (struct qdl_device * qdl )
210215{
211- libusb_close (qdl -> usb_handle );
216+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
217+
218+ libusb_close (qdl_usb -> usb_handle );
212219 libusb_exit (NULL );
213220}
214221
215- int qdl_read (struct qdl_device * qdl , void * buf , size_t len , unsigned int timeout )
222+ static int usb_read (struct qdl_device * qdl , void * buf , size_t len , unsigned int timeout )
216223{
224+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
217225 int actual ;
218226 int ret ;
219227
220- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> in_ep , buf , len , & actual , timeout );
228+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> in_ep , buf , len , & actual , timeout );
221229 if ((ret != 0 && ret != LIBUSB_ERROR_TIMEOUT ) ||
222230 (ret == LIBUSB_ERROR_TIMEOUT && actual == 0 ))
223231 return -1 ;
224232
225233 return actual ;
226234}
227235
228- int qdl_write (struct qdl_device * qdl , const void * buf , size_t len )
236+ static int usb_write (struct qdl_device * qdl , const void * buf , size_t len )
229237{
230238 unsigned char * data = (unsigned char * ) buf ;
239+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
231240 unsigned int count = 0 ;
232241 size_t len_orig = len ;
233242 int actual ;
234243 int xfer ;
235244 int ret ;
236245
237246 while (len > 0 ) {
238- xfer = (len > qdl -> out_chunk_size ) ? qdl -> out_chunk_size : len ;
247+ xfer = (len > qdl_usb -> out_chunk_size ) ? qdl_usb -> out_chunk_size : len ;
239248
240- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> out_ep , data ,
249+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> out_ep , data ,
241250 xfer , & actual , 1000 );
242251 if ((ret != 0 && ret != LIBUSB_ERROR_TIMEOUT ) ||
243252 (ret == LIBUSB_ERROR_TIMEOUT && actual == 0 )) {
@@ -250,8 +259,8 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
250259 data += actual ;
251260 }
252261
253- if (len_orig % qdl -> out_maxpktsize == 0 ) {
254- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> out_ep , NULL ,
262+ if (len_orig % qdl_usb -> out_maxpktsize == 0 ) {
263+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> out_ep , NULL ,
255264 0 , & actual , 1000 );
256265 if (ret < 0 )
257266 return -1 ;
@@ -260,7 +269,25 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
260269 return count ;
261270}
262271
263- void qdl_set_out_chunk_size (struct qdl_device * qdl , long size )
272+ static void usb_set_out_chunk_size (struct qdl_device * qdl , long size )
264273{
265- qdl -> out_chunk_size = size ;
274+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
275+
276+ qdl_usb -> out_chunk_size = size ;
266277}
278+
279+ struct qdl_device * usb_init (void )
280+ {
281+ struct qdl_device * qdl = malloc (sizeof (struct qdl_device_usb ));
282+ if (!qdl )
283+ return NULL ;
284+
285+ qdl -> dev_type = QDL_DEVICE_USB ;
286+ qdl -> open = usb_open ;
287+ qdl -> read = usb_read ;
288+ qdl -> write = usb_write ;
289+ qdl -> close = usb_close ;
290+ qdl -> set_out_chunk_size = usb_set_out_chunk_size ;
291+
292+ return qdl ;
293+ }
0 commit comments