From owner-svn-src-head@freebsd.org Sat Feb 17 14:34:48 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4AD13F1392A; Sat, 17 Feb 2018 14:34:48 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id C940F8192E; Sat, 17 Feb 2018 14:34:47 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id AAD431DFF3; Sat, 17 Feb 2018 14:34:47 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w1HEYlYU063604; Sat, 17 Feb 2018 14:34:47 GMT (envelope-from hselasky@FreeBSD.org) Received: (from hselasky@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w1HEYl8I063603; Sat, 17 Feb 2018 14:34:47 GMT (envelope-from hselasky@FreeBSD.org) Message-Id: <201802171434.w1HEYl8I063603@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: hselasky set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky Date: Sat, 17 Feb 2018 14:34:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r329458 - head/sbin/devmatch X-SVN-Group: head X-SVN-Commit-Author: hselasky X-SVN-Commit-Paths: head/sbin/devmatch X-SVN-Commit-Revision: 329458 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Feb 2018 14:34:48 -0000 Author: hselasky Date: Sat Feb 17 14:34:47 2018 New Revision: 329458 URL: https://svnweb.freebsd.org/changeset/base/329458 Log: Fix USB driver matching in devmatch(8). Multiple drivers can match on the same USB device and the order of loading decides which driver gets the device. Use the supplied mask value as an indication of priority, so that vendor specific device drivers are loaded before more generic ones. Sponsored by: Mellanox Technologies Modified: head/sbin/devmatch/devmatch.c Modified: head/sbin/devmatch/devmatch.c ============================================================================== --- head/sbin/devmatch/devmatch.c Sat Feb 17 14:30:39 2018 (r329457) +++ head/sbin/devmatch/devmatch.c Sat Feb 17 14:34:47 2018 (r329458) @@ -54,6 +54,14 @@ static struct option longopts[] = { { NULL, 0, NULL, 0 } }; +#define DEVMATCH_MAX_HITS 256 + +static struct match_data { + char *descr; + int priority; +} match_data[DEVMATCH_MAX_HITS]; + +static int hit_index; static int all_flag; static int dump_flag; static char *linker_hints; @@ -236,6 +244,35 @@ pnpval_as_str(const char *val, const char *pnpinfo) return retval; } +static int +match_data_compare(const void *_pa, const void *_pb) +{ + const struct match_data *pa = _pa; + const struct match_data *pb = _pb; + + /* biggest value first */ + if (pa->priority > pb->priority) + return (-1); + else if (pa->priority < pb->priority) + return (1); + + /* then sort by string */ + return (strcmp(pa->descr, pb->descr)); +} + +static int +bitrev16(int input) +{ + int retval = 0; + int x; + + for (x = 0; x != 16; x++) { + if ((input >> x) & 1) + retval |= (0x8000 >> x); + } + return (retval); +} + static void search_hints(const char *bus, const char *dev, const char *pnpinfo) { @@ -367,9 +404,20 @@ search_hints(const char *bus, const char *dev, const c printf("\n"); else if (!notme) { if (!unbound_flag) { + char *descr = NULL; + if (all_flag) - printf("%s: ", *dev ? dev : "unattached" ); - printf("%s\n", lastmod); + asprintf(&descr, "%s: %s", *dev ? dev : "unattached", lastmod); + else + asprintf(&descr, "%s", lastmod); + + if (descr != NULL && hit_index < DEVMATCH_MAX_HITS) { + match_data[hit_index].descr = descr; + match_data[hit_index].priority = bitrev16(mask); + hit_index++; + } else { + free(descr); + } } found++; } @@ -382,6 +430,19 @@ search_hints(const char *bus, const char *dev, const c } walker = (void *)(len - sizeof(int) + (intptr_t)walker); } + if (hit_index != 0) { + /* sort hits by priority */ + mergesort(match_data, hit_index, sizeof(match_data[0]), &match_data_compare); + + /* printout */ + for (i = 0; i != hit_index; i++) { + puts(match_data[i].descr); + free(match_data[i].descr); + } + + /* reset hit_index */ + hit_index = 0; + } if (unbound_flag && found == 0 && *pnpinfo) { if (verbose_flag) printf("------------------------- ");