2222#define LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK LIBUSB_TRANSFER_TYPE_BULK
2323#endif
2424
25- static bool qdl_match_usb_serial (struct libusb_device_handle * handle , const char * serial ,
25+ static bool usb_match_usb_serial (struct libusb_device_handle * handle , const char * serial ,
2626 const struct libusb_device_descriptor * desc )
2727{
2828 char buf [128 ];
@@ -49,7 +49,7 @@ static bool qdl_match_usb_serial(struct libusb_device_handle *handle, const char
4949 return strcmp (p , serial ) == 0 ;
5050}
5151
52- static int qdl_try_open (libusb_device * dev , struct qdl_device * qdl , const char * serial )
52+ static int usb_try_open (libusb_device * dev , struct qdl_device_usb * qdl , const char * serial )
5353{
5454 const struct libusb_endpoint_descriptor * endpoint ;
5555 const struct libusb_interface_descriptor * ifc ;
@@ -123,7 +123,7 @@ static int qdl_try_open(libusb_device *dev, struct qdl_device *qdl, const char *
123123 continue ;
124124 }
125125
126- if (!qdl_match_usb_serial (handle , serial , & desc )) {
126+ if (!usb_match_usb_serial (handle , serial , & desc )) {
127127 libusb_close (handle );
128128 continue ;
129129 }
@@ -161,10 +161,11 @@ static int qdl_try_open(libusb_device *dev, struct qdl_device *qdl, const char *
161161 return !!qdl -> usb_handle ;
162162}
163163
164- int qdl_open (struct qdl_device * qdl , const char * serial )
164+ static int usb_open (struct qdl_device * qdl , const char * serial )
165165{
166166 struct libusb_device * * devs ;
167167 struct libusb_device * dev ;
168+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
168169 bool wait_printed = false;
169170 bool found = false;
170171 ssize_t n ;
@@ -183,7 +184,7 @@ int qdl_open(struct qdl_device *qdl, const char *serial)
183184 for (i = 0 ; devs [i ]; i ++ ) {
184185 dev = devs [i ];
185186
186- ret = qdl_try_open (dev , qdl , serial );
187+ ret = usb_try_open (dev , qdl_usb , serial );
187188 if (ret == 1 ) {
188189 found = true;
189190 break ;
@@ -206,38 +207,42 @@ int qdl_open(struct qdl_device *qdl, const char *serial)
206207 return -1 ;
207208}
208209
209- void qdl_close (struct qdl_device * qdl )
210+ static void usb_close (struct qdl_device * qdl )
210211{
211- libusb_close (qdl -> usb_handle );
212+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
213+
214+ libusb_close (qdl_usb -> usb_handle );
212215 libusb_exit (NULL );
213216}
214217
215- int qdl_read (struct qdl_device * qdl , void * buf , size_t len , unsigned int timeout )
218+ static int usb_read (struct qdl_device * qdl , void * buf , size_t len , unsigned int timeout )
216219{
220+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
217221 int actual ;
218222 int ret ;
219223
220- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> in_ep , buf , len , & actual , timeout );
224+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> in_ep , buf , len , & actual , timeout );
221225 if ((ret != 0 && ret != LIBUSB_ERROR_TIMEOUT ) ||
222226 (ret == LIBUSB_ERROR_TIMEOUT && actual == 0 ))
223227 return -1 ;
224228
225229 return actual ;
226230}
227231
228- int qdl_write (struct qdl_device * qdl , const void * buf , size_t len )
232+ static int usb_write (struct qdl_device * qdl , const void * buf , size_t len )
229233{
230234 unsigned char * data = (unsigned char * ) buf ;
235+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
231236 unsigned int count = 0 ;
232237 size_t len_orig = len ;
233238 int actual ;
234239 int xfer ;
235240 int ret ;
236241
237242 while (len > 0 ) {
238- xfer = (len > qdl -> out_chunk_size ) ? qdl -> out_chunk_size : len ;
243+ xfer = (len > qdl_usb -> out_chunk_size ) ? qdl_usb -> out_chunk_size : len ;
239244
240- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> out_ep , data ,
245+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> out_ep , data ,
241246 xfer , & actual , 1000 );
242247 if ((ret != 0 && ret != LIBUSB_ERROR_TIMEOUT ) ||
243248 (ret == LIBUSB_ERROR_TIMEOUT && actual == 0 )) {
@@ -250,8 +255,8 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
250255 data += actual ;
251256 }
252257
253- if (len_orig % qdl -> out_maxpktsize == 0 ) {
254- ret = libusb_bulk_transfer (qdl -> usb_handle , qdl -> out_ep , NULL ,
258+ if (len_orig % qdl_usb -> out_maxpktsize == 0 ) {
259+ ret = libusb_bulk_transfer (qdl_usb -> usb_handle , qdl_usb -> out_ep , NULL ,
255260 0 , & actual , 1000 );
256261 if (ret < 0 )
257262 return -1 ;
@@ -260,7 +265,25 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
260265 return count ;
261266}
262267
263- void qdl_set_out_chunk_size (struct qdl_device * qdl , long size )
268+ static void usb_set_out_chunk_size (struct qdl_device * qdl , long size )
264269{
265- qdl -> out_chunk_size = size ;
270+ struct qdl_device_usb * qdl_usb = container_of (qdl , struct qdl_device_usb , base );
271+
272+ qdl_usb -> out_chunk_size = size ;
266273}
274+
275+ struct qdl_device * usb_init (void )
276+ {
277+ struct qdl_device * qdl = malloc (sizeof (struct qdl_device_usb ));
278+ if (!qdl )
279+ return NULL ;
280+
281+ qdl -> dev_type = QDL_DEVICE_USB ;
282+ qdl -> open = usb_open ;
283+ qdl -> read = usb_read ;
284+ qdl -> write = usb_write ;
285+ qdl -> close = usb_close ;
286+ qdl -> set_out_chunk_size = usb_set_out_chunk_size ;
287+
288+ return qdl ;
289+ }
0 commit comments