@@ -179,9 +179,6 @@ static void register_device_error(hid_device *dev, const char *msg)
179179/* Similar to register_device_error, but you can pass a format string into this function. */
180180static void register_device_error_format (hid_device * dev , const char * format , ...)
181181{
182- if (!dev )
183- return ;
184-
185182 va_list args ;
186183 va_start (args , format );
187184 register_error_str_vformat (& dev -> last_error_str , format , args );
@@ -202,9 +199,6 @@ static wchar_t *copy_udev_string(struct udev_device *dev, const char *udev_name)
202199 */
203200static int get_hid_item_size (const __u8 * report_descriptor , __u32 size , unsigned int pos , int * data_len , int * key_size )
204201{
205- if (!report_descriptor || !data_len || !key_size )
206- return 0 ;
207-
208202 int key = report_descriptor [pos ];
209203 int size_code ;
210204
@@ -261,9 +255,6 @@ static int get_hid_item_size(const __u8 *report_descriptor, __u32 size, unsigned
261255 */
262256static __u32 get_hid_report_bytes (const __u8 * rpt , size_t len , size_t num_bytes , size_t cur )
263257{
264- if (!rpt )
265- return 0 ;
266-
267258 /* Return if there aren't enough bytes. */
268259 if (cur + num_bytes >= len )
269260 return 0 ;
@@ -358,9 +349,6 @@ struct hid_usage_iterator {
358349 */
359350static int get_next_hid_usage (const __u8 * report_descriptor , __u32 size , struct hid_usage_iterator * ctx , unsigned short * usage_page , unsigned short * usage )
360351{
361- if (!report_descriptor || !ctx || !usage || !usage_page )
362- return -1 ;
363-
364352 int data_len , key_size ;
365353 int initial = ctx -> pos == 0 ; /* Used to handle case where no top-level application collection is defined */
366354
@@ -384,7 +372,6 @@ static int get_next_hid_usage(const __u8 *report_descriptor, __u32 size, struct
384372 if (data_len == 4 ) { /* Usages 5.5 / Usage Page 6.2.2.7 */
385373 ctx -> usage_page = get_hid_report_bytes (report_descriptor , size , 2 , ctx -> pos + 2 );
386374 ctx -> usage_page_found = 1 ;
387-
388375 * usage = get_hid_report_bytes (report_descriptor , size , 2 , ctx -> pos );
389376 usage_found = 1 ;
390377 }
@@ -428,9 +415,6 @@ static int get_next_hid_usage(const __u8 *report_descriptor, __u32 size, struct
428415 */
429416static int get_hid_report_descriptor (const char * rpt_path , struct hidraw_report_descriptor * rpt_desc )
430417{
431- if (!rpt_path || !rpt_desc )
432- return -1 ;
433-
434418 int rpt_handle ;
435419 ssize_t res ;
436420
@@ -462,12 +446,11 @@ static int get_hid_report_descriptor_from_sysfs(const char *sysfs_path, struct h
462446{
463447 int res = -1 ;
464448
465- if (!sysfs_path )
466- return res ;
467-
468449 /* Construct <sysfs_path>/device/report_descriptor */
469450 size_t rpt_path_len = strlen (sysfs_path ) + 25 + 1 ;
470451 char * rpt_path = (char * ) calloc (1 , rpt_path_len );
452+ if (!rpt_path )
453+ return -1 ;
471454 snprintf (rpt_path , rpt_path_len , "%s/device/report_descriptor" , sysfs_path );
472455
473456 res = get_hid_report_descriptor (rpt_path , rpt_desc );
@@ -479,9 +462,6 @@ static int get_hid_report_descriptor_from_sysfs(const char *sysfs_path, struct h
479462/* return non-zero if successfully parsed */
480463static int parse_hid_vid_pid_from_uevent (const char * uevent , unsigned * bus_type , unsigned short * vendor_id , unsigned short * product_id )
481464{
482- if (!uevent )
483- return 0 ;
484-
485465 char tmp [1024 ];
486466 size_t uevent_len = strlen (uevent );
487467 if (uevent_len > sizeof (tmp ) - 1 )
@@ -554,9 +534,6 @@ static int parse_hid_vid_pid_from_sysfs(const char *sysfs_path, unsigned *bus_ty
554534{
555535 int res = 0 ;
556536
557- if (!sysfs_path )
558- return res ;
559-
560537 /* Construct <sysfs_path>/device/uevent */
561538 size_t uevent_path_len = strlen (sysfs_path ) + 14 + 1 ;
562539 char * uevent_path = (char * ) calloc (1 , uevent_path_len );
@@ -570,9 +547,6 @@ static int parse_hid_vid_pid_from_sysfs(const char *sysfs_path, unsigned *bus_ty
570547
571548static int get_hid_report_descriptor_from_hidraw (hid_device * dev , struct hidraw_report_descriptor * rpt_desc )
572549{
573- if (!dev )
574- return -1 ;
575-
576550 int desc_size = 0 ;
577551
578552 /* Get Report Descriptor Size */
@@ -814,7 +788,14 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
814788 }
815789
816790 /* Usage Page and Usage */
817- result = get_hid_report_descriptor_from_sysfs (sysfs_path , & report_desc );
791+
792+ if (sysfs_path ) {
793+ result = get_hid_report_descriptor_from_sysfs (sysfs_path , & report_desc );
794+ }
795+ else {
796+ result = -1 ;
797+ }
798+
818799 if (result >= 0 ) {
819800 unsigned short page = 0 , usage = 0 ;
820801 struct hid_usage_iterator usage_iterator ;
@@ -839,7 +820,7 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
839820 struct hid_device_info * prev_dev = cur_dev ;
840821
841822 if (!tmp )
842- continue ;
823+ break ;
843824 cur_dev -> next = tmp ;
844825 cur_dev = tmp ;
845826
@@ -866,9 +847,6 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
866847}
867848
868849static struct hid_device_info * create_device_info_for_hid_device (hid_device * dev ) {
869- if (!dev )
870- return NULL ;
871-
872850 struct udev * udev ;
873851 struct udev_device * udev_dev ;
874852 struct stat s ;
@@ -887,6 +865,7 @@ static struct hid_device_info * create_device_info_for_hid_device(hid_device *de
887865 /* Create the udev object */
888866 udev = udev_new ();
889867 if (!udev ) {
868+ errno = ENOMEM ;
890869 register_device_error (dev , "Couldn't create udev context" );
891870 return NULL ;
892871 }
@@ -899,6 +878,7 @@ static struct hid_device_info * create_device_info_for_hid_device(hid_device *de
899878
900879 if (!root ) {
901880 /* TODO: have a better error reporting via create_device_info_for_device */
881+ errno = EIO ;
902882 register_device_error (dev , "Couldn't create hid_device_info" );
903883 }
904884
@@ -1094,6 +1074,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
10941074
10951075 dev = new_hid_device ();
10961076 if (!dev ) {
1077+ errno = ENOMEM ;
10971078 register_global_error ("Couldn't allocate memory" );
10981079 return NULL ;
10991080 }
@@ -1106,8 +1087,8 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11061087 /* Make sure this is a HIDRAW device - responds to HIDIOCGRDESCSIZE */
11071088 res = ioctl (dev -> device_handle , HIDIOCGRDESCSIZE , & desc_size );
11081089 if (res < 0 ) {
1109- hid_close (dev );
11101090 register_global_error_format ("ioctl(GRDESCSIZE) error for '%s', not a HIDRAW device?: %s" , path , strerror (errno ));
1091+ hid_close (dev );
11111092 return NULL ;
11121093 }
11131094
@@ -1124,16 +1105,11 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11241105
11251106int HID_API_EXPORT hid_write (hid_device * dev , const unsigned char * data , size_t length )
11261107{
1127- if (!dev ) {
1128- register_global_error ("Device is NULL" );
1129- return -1 ;
1130- }
1131-
11321108 int bytes_written ;
11331109
11341110 if (!data || (length == 0 )) {
11351111 errno = EINVAL ;
1136- register_device_error (dev , strerror ( errno ) );
1112+ register_device_error (dev , "Zero buffer/length" );
11371113 return -1 ;
11381114 }
11391115
@@ -1147,8 +1123,9 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
11471123
11481124int HID_API_EXPORT hid_read_timeout (hid_device * dev , unsigned char * data , size_t length , int milliseconds )
11491125{
1150- if (!dev ) {
1151- register_global_error ("Device is NULL" );
1126+ if (!data || (length == 0 )) {
1127+ errno = EINVAL ;
1128+ register_error_str (& dev -> last_read_error_str , "Zero buffer/length" );
11521129 return -1 ;
11531130 }
11541131
@@ -1185,6 +1162,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
11851162 indicate a device disconnection. */
11861163 if (fds .revents & (POLLERR | POLLHUP | POLLNVAL )) {
11871164 // We cannot use strerror() here as no -1 was returned from poll().
1165+ errno = EIO ;
11881166 register_error_str (& dev -> last_read_error_str , "hid_read_timeout: unexpected poll error (device disconnected)" );
11891167 return -1 ;
11901168 }
@@ -1204,11 +1182,6 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12041182
12051183int HID_API_EXPORT hid_read (hid_device * dev , unsigned char * data , size_t length )
12061184{
1207- if (!dev ) {
1208- register_global_error ("Device is NULL" );
1209- return -1 ;
1210- }
1211-
12121185 return hid_read_timeout (dev , data , length , (dev -> blocking )? -1 : 0 );
12131186}
12141187
@@ -1221,11 +1194,6 @@ HID_API_EXPORT const wchar_t * HID_API_CALL hid_read_error(hid_device *dev)
12211194
12221195int HID_API_EXPORT hid_set_nonblocking (hid_device * dev , int nonblock )
12231196{
1224- if (!dev ) {
1225- register_global_error ("Device is NULL" );
1226- return -1 ;
1227- }
1228-
12291197 /* Do all non-blocking in userspace using poll(), since it looks
12301198 like there's a bug in the kernel in some versions where
12311199 read() will not return -1 on disconnection of the USB device */
@@ -1237,13 +1205,14 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
12371205
12381206int HID_API_EXPORT hid_send_feature_report (hid_device * dev , const unsigned char * data , size_t length )
12391207{
1240- if (!dev ) {
1241- register_global_error ("Device is NULL" );
1208+ int res ;
1209+
1210+ if (!data || (length == 0 )) {
1211+ errno = EINVAL ;
1212+ register_device_error (dev , "Zero buffer/length" );
12421213 return -1 ;
12431214 }
12441215
1245- int res ;
1246-
12471216 register_device_error (dev , NULL );
12481217
12491218 res = ioctl (dev -> device_handle , HIDIOCSFEATURE (length ), data );
@@ -1255,13 +1224,14 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
12551224
12561225int HID_API_EXPORT hid_get_feature_report (hid_device * dev , unsigned char * data , size_t length )
12571226{
1258- if (!dev ) {
1259- register_global_error ("Device is NULL" );
1227+ int res ;
1228+
1229+ if (!data || (length == 0 )) {
1230+ errno = EINVAL ;
1231+ register_device_error (dev , "Zero buffer/length" );
12601232 return -1 ;
12611233 }
12621234
1263- int res ;
1264-
12651235 register_device_error (dev , NULL );
12661236
12671237 res = ioctl (dev -> device_handle , HIDIOCGFEATURE (length ), data );
@@ -1275,8 +1245,9 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
12751245{
12761246 int res ;
12771247
1278- if (!dev ) {
1279- register_global_error ("Device is NULL" );
1248+ if (!data || (length == 0 )) {
1249+ errno = EINVAL ;
1250+ register_device_error (dev , "Zero buffer/length" );
12801251 return -1 ;
12811252 }
12821253
@@ -1291,13 +1262,14 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
12911262
12921263int HID_API_EXPORT HID_API_CALL hid_get_input_report (hid_device * dev , unsigned char * data , size_t length )
12931264{
1294- if (!dev ) {
1295- register_global_error ("Device is NULL" );
1265+ int res ;
1266+
1267+ if (!data || (length == 0 )) {
1268+ errno = EINVAL ;
1269+ register_device_error (dev , "Zero buffer/length" );
12961270 return -1 ;
12971271 }
12981272
1299- int res ;
1300-
13011273 register_device_error (dev , NULL );
13021274
13031275 res = ioctl (dev -> device_handle , HIDIOCGINPUT (length ), data );
@@ -1326,6 +1298,7 @@ void HID_API_EXPORT hid_close(hid_device *dev)
13261298int HID_API_EXPORT_CALL hid_get_manufacturer_string (hid_device * dev , wchar_t * string , size_t maxlen )
13271299{
13281300 if (!string || !maxlen ) {
1301+ errno = EINVAL ;
13291302 register_device_error (dev , "Zero buffer/length" );
13301303 return -1 ;
13311304 }
@@ -1350,6 +1323,7 @@ int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *st
13501323int HID_API_EXPORT_CALL hid_get_product_string (hid_device * dev , wchar_t * string , size_t maxlen )
13511324{
13521325 if (!string || !maxlen ) {
1326+ errno = EINVAL ;
13531327 register_device_error (dev , "Zero buffer/length" );
13541328 return -1 ;
13551329 }
@@ -1374,6 +1348,7 @@ int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string,
13741348int HID_API_EXPORT_CALL hid_get_serial_number_string (hid_device * dev , wchar_t * string , size_t maxlen )
13751349{
13761350 if (!string || !maxlen ) {
1351+ errno = EINVAL ;
13771352 register_device_error (dev , "Zero buffer/length" );
13781353 return -1 ;
13791354 }
@@ -1397,12 +1372,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s
13971372
13981373
13991374HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info (hid_device * dev ) {
1400- if (!dev ) {
1401- register_global_error ("Device is NULL" );
1402- return NULL ;
1375+ if (dev -> device_info ) {
1376+ register_device_error (dev , NULL );
14031377 }
1404-
1405- if (!dev -> device_info ) {
1378+ else {
14061379 // Lazy initialize device_info
14071380 dev -> device_info = create_device_info_for_hid_device (dev );
14081381 }
@@ -1413,15 +1386,11 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi
14131386
14141387int HID_API_EXPORT_CALL hid_get_indexed_string (hid_device * dev , int string_index , wchar_t * string , size_t maxlen )
14151388{
1416- if (!dev ) {
1417- register_global_error ("Device is NULL" );
1418- return -1 ;
1419- }
1420-
14211389 (void )string_index ;
14221390 (void )string ;
14231391 (void )maxlen ;
14241392
1393+ errno = ENOSYS ;
14251394 register_device_error (dev , "hid_get_indexed_string: not supported by hidraw" );
14261395
14271396 return -1 ;
@@ -1431,6 +1400,15 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
14311400int HID_API_EXPORT_CALL hid_get_report_descriptor (hid_device * dev , unsigned char * buf , size_t buf_size )
14321401{
14331402 struct hidraw_report_descriptor rpt_desc ;
1403+
1404+ if (!buf || !buf_size ) {
1405+ errno = EINVAL ;
1406+ register_device_error (dev , "Zero buffer/length" );
1407+ return -1 ;
1408+ }
1409+
1410+ register_device_error (dev , NULL );
1411+
14341412 int res = get_hid_report_descriptor_from_hidraw (dev , & rpt_desc );
14351413 if (res < 0 ) {
14361414 /* error already registered */
0 commit comments