From patchwork Mon Dec 20 11:19:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 12687617 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19356C433F5 for ; Mon, 20 Dec 2021 11:19:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231672AbhLTLT0 (ORCPT ); Mon, 20 Dec 2021 06:19:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231661AbhLTLTY (ORCPT ); Mon, 20 Dec 2021 06:19:24 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56694C061574; Mon, 20 Dec 2021 03:19:24 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 15DF6B80E4C; Mon, 20 Dec 2021 11:19:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CFCFFC36AE9; Mon, 20 Dec 2021 11:19:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1639999161; bh=10FVIZPMYhnXccQdS0HX0uRmyH8OdT7D1W/gC+9tVhQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XVlBxoeJETuF3NxpS08hY0UH3u8XKGX1VVG44tZC6YDInyNp1/l57N1YFWDsZurav GSYBtKNTEjLUdERQV+dtqEV+PyRONFZiXK/GapObOzOtUwBs5I/rP7lps68aFja1Ds uW70j3g23/95pMbjtKl31dC3dcLx7sy7PzhrxYGQR+O3FjG7vBC50rAXAHu/LYW/98 QETUgAX0wvmBXqolJB/8XaRxxHm+PvN1sgVA6YwVv8Ye5tkxsCq9lwYWiU0L4ZJjX8 m62Rx61bwKibUPBvb6AgeorF9pmrz93EpR3YjUdcqKVwjLEXv7ESxnVZK9NdOb8oXh RT5ETJx6nJZqQ== Received: from johan by xi.lan with local (Exim 4.94.2) (envelope-from ) id 1mzGhH-00063A-0O; Mon, 20 Dec 2021 12:19:15 +0100 From: Johan Hovold To: Johan Hovold Cc: Marc Ferland , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] gnss: add USB support Date: Mon, 20 Dec 2021 12:19:00 +0100 Message-Id: <20211220111901.23206-2-johan@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211220111901.23206-1-johan@kernel.org> References: <20211220111901.23206-1-johan@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Add a generic driver for GNSS receivers with a USB interface with two bulk endpoints. The driver currently assumes that the device protocol is NMEA (only) but this can be generalised later as needed. Signed-off-by: Johan Hovold --- drivers/gnss/Kconfig | 11 +++ drivers/gnss/Makefile | 3 + drivers/gnss/usb.c | 209 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 223 insertions(+) create mode 100644 drivers/gnss/usb.c diff --git a/drivers/gnss/Kconfig b/drivers/gnss/Kconfig index bd12e3d57baa..d7fe265c2869 100644 --- a/drivers/gnss/Kconfig +++ b/drivers/gnss/Kconfig @@ -54,4 +54,15 @@ config GNSS_UBX_SERIAL If unsure, say N. +config GNSS_USB + tristate "USB GNSS receiver support" + depends on USB + help + Say Y here if you have a GNSS receiver which uses a USB interface. + + To compile this driver as a module, choose M here: the module will + be called gnss-usb. + + If unsure, say N. + endif # GNSS diff --git a/drivers/gnss/Makefile b/drivers/gnss/Makefile index 451f11401ecc..bb2cbada3435 100644 --- a/drivers/gnss/Makefile +++ b/drivers/gnss/Makefile @@ -17,3 +17,6 @@ gnss-sirf-y := sirf.o obj-$(CONFIG_GNSS_UBX_SERIAL) += gnss-ubx.o gnss-ubx-y := ubx.o + +obj-$(CONFIG_GNSS_USB) += gnss-usb.o +gnss-usb-y := usb.o diff --git a/drivers/gnss/usb.c b/drivers/gnss/usb.c new file mode 100644 index 000000000000..5c0251034def --- /dev/null +++ b/drivers/gnss/usb.c @@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Generic USB GNSS receiver driver + * + * Copyright (C) 2021 Johan Hovold + */ + +#include +#include +#include +#include +#include +#include +#include + +#define GNSS_USB_READ_BUF_LEN 512 +#define GNSS_USB_WRITE_TIMEOUT 1000 + +static const struct usb_device_id gnss_usb_id_table[] = { + { } +}; +MODULE_DEVICE_TABLE(usb, gnss_usb_id_table); + +struct gnss_usb { + struct usb_device *udev; + struct usb_interface *intf; + struct gnss_device *gdev; + struct urb *read_urb; + unsigned int write_pipe; +}; + +static void gnss_usb_rx_complete(struct urb *urb) +{ + struct gnss_usb *gusb = urb->context; + struct gnss_device *gdev = gusb->gdev; + int status = urb->status; + int len; + int ret; + + switch (status) { + case 0: + break; + case -ENOENT: + case -ECONNRESET: + case -ESHUTDOWN: + dev_dbg(&gdev->dev, "urb stopped: %d\n", status); + return; + case -EPIPE: + dev_err(&gdev->dev, "urb stopped: %d\n", status); + return; + default: + dev_dbg(&gdev->dev, "nonzero urb status: %d\n", status); + goto resubmit; + } + + len = urb->actual_length; + if (len == 0) + goto resubmit; + + ret = gnss_insert_raw(gdev, urb->transfer_buffer, len); + if (ret < len) + dev_dbg(&gdev->dev, "dropped %d bytes\n", len - ret); +resubmit: + ret = usb_submit_urb(urb, GFP_ATOMIC); + if (ret && ret != -EPERM && ret != -ENODEV) + dev_err(&gdev->dev, "failed to resubmit urb: %d\n", ret); +} + +static int gnss_usb_open(struct gnss_device *gdev) +{ + struct gnss_usb *gusb = gnss_get_drvdata(gdev); + int ret; + + ret = usb_submit_urb(gusb->read_urb, GFP_KERNEL); + if (ret) { + if (ret != -EPERM && ret != -ENODEV) + dev_err(&gdev->dev, "failed to submit urb: %d\n", ret); + return ret; + } + + return 0; +} + +static void gnss_usb_close(struct gnss_device *gdev) +{ + struct gnss_usb *gusb = gnss_get_drvdata(gdev); + + usb_kill_urb(gusb->read_urb); +} + +static int gnss_usb_write_raw(struct gnss_device *gdev, + const unsigned char *buf, size_t count) +{ + struct gnss_usb *gusb = gnss_get_drvdata(gdev); + void *tbuf; + int ret; + + tbuf = kmemdup(buf, count, GFP_KERNEL); + if (!tbuf) + return -ENOMEM; + + ret = usb_bulk_msg(gusb->udev, gusb->write_pipe, tbuf, count, NULL, + GNSS_USB_WRITE_TIMEOUT); + kfree(tbuf); + if (ret) + return ret; + + return count; +} + +static const struct gnss_operations gnss_usb_gnss_ops = { + .open = gnss_usb_open, + .close = gnss_usb_close, + .write_raw = gnss_usb_write_raw, +}; + +static int gnss_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) +{ + struct usb_device *udev = interface_to_usbdev(intf); + struct usb_endpoint_descriptor *in, *out; + struct gnss_device *gdev; + struct gnss_usb *gusb; + struct urb *urb; + size_t buf_len; + void *buf; + int ret; + + ret = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, + NULL); + if (ret) + return ret; + + gusb = kzalloc(sizeof(*gusb), GFP_KERNEL); + if (!gusb) + return -ENOMEM; + + gdev = gnss_allocate_device(&intf->dev); + if (!gdev) { + ret = -ENOMEM; + goto err_free_gusb; + } + + gdev->ops = &gnss_usb_gnss_ops; + gdev->type = GNSS_TYPE_NMEA; + gnss_set_drvdata(gdev, gusb); + + urb = usb_alloc_urb(0, GFP_KERNEL); + if (!urb) + goto err_put_gdev; + + buf_len = max(usb_endpoint_maxp(in), GNSS_USB_READ_BUF_LEN); + + buf = kzalloc(buf_len, GFP_KERNEL); + if (!buf) + goto err_free_urb; + + usb_fill_bulk_urb(urb, udev, + usb_rcvbulkpipe(udev, usb_endpoint_num(in)), + buf, buf_len, gnss_usb_rx_complete, gusb); + + gusb->intf = intf; + gusb->udev = udev; + gusb->gdev = gdev; + gusb->read_urb = urb; + gusb->write_pipe = usb_sndbulkpipe(udev, usb_endpoint_num(out)); + + ret = gnss_register_device(gdev); + if (ret) + goto err_free_buf; + + usb_set_intfdata(intf, gusb); + + return 0; + +err_free_buf: + kfree(buf); +err_free_urb: + usb_free_urb(urb); +err_put_gdev: + gnss_put_device(gdev); +err_free_gusb: + kfree(gusb); + + return ret; +} + +static void gnss_usb_disconnect(struct usb_interface *intf) +{ + struct gnss_usb *gusb = usb_get_intfdata(intf); + + gnss_deregister_device(gusb->gdev); + + kfree(gusb->read_urb->transfer_buffer); + usb_free_urb(gusb->read_urb); + gnss_put_device(gusb->gdev); + kfree(gusb); +} + +static struct usb_driver gnss_usb_driver = { + .name = "gnss-usb", + .probe = gnss_usb_probe, + .disconnect = gnss_usb_disconnect, + .id_table = gnss_usb_id_table, +}; +module_usb_driver(gnss_usb_driver); + +MODULE_AUTHOR("Johan Hovold "); +MODULE_DESCRIPTION("Generic USB GNSS receiver driver"); +MODULE_LICENSE("GPL v2"); From patchwork Mon Dec 20 11:19:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 12687615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66699C4332F for ; Mon, 20 Dec 2021 11:19:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231644AbhLTLTX (ORCPT ); Mon, 20 Dec 2021 06:19:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231171AbhLTLTX (ORCPT ); Mon, 20 Dec 2021 06:19:23 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D11A7C061574; Mon, 20 Dec 2021 03:19:22 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6EAAE60FDA; Mon, 20 Dec 2021 11:19:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD279C36AE7; Mon, 20 Dec 2021 11:19:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1639999161; bh=rFjfI7Vn7vJeC1yK+hK/Qc1NMRxCct+H3Q4AMWxJdCk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rj8tO1uyWdpRHPQ0//QKOsbjqWHCdzwqjq0dKNReb3imsliuoirUMTlvJ95e//VyR iUIJZFeR9gbgqMNmYVa5Zk6dddqcjblgUHzENetfC1734x4QqUv8LVWLKnTf7Anndx LV12a3OOUQZDqH7ihUzQ/gYZwgWac6I4o/yzYvwnS7djqUMlSp7EuE+N4H9oHCxLqG nD4vg9X96gNZ2fBjFNoZKyocf59+6B7l5UCyeddSlBuGXe2RehYb0sytYJPjJLyiew qnT037uIxpYzzOh6TZcZSUyQbEkuu2WEaWtieynoOJvRCTqKVntGoCjY9J+Enc2jBD fFXfAUtbdYzvA== Received: from johan by xi.lan with local (Exim 4.94.2) (envelope-from ) id 1mzGhH-00063C-2s; Mon, 20 Dec 2021 12:19:15 +0100 From: Johan Hovold To: Johan Hovold Cc: Marc Ferland , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] gnss: usb: add support for Sierra Wireless XM1210 Date: Mon, 20 Dec 2021 12:19:01 +0100 Message-Id: <20211220111901.23206-3-johan@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211220111901.23206-1-johan@kernel.org> References: <20211220111901.23206-1-johan@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Add support for the USB interface of the Sierra Wireless XM1210 receiver. Note that the device only supports NMEA. Bus 002 Device 003: ID 1199:b000 Sierra Wireless, Inc. Sierra Wireless_GNSS Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 1.00 bDeviceClass 0 bDeviceSubClass 0 bDeviceProtocol 0 bMaxPacketSize0 64 idVendor 0x1199 Sierra Wireless, Inc. idProduct 0xb000 bcdDevice 0.01 iManufacturer 1 Sierra-wireless iProduct 2 Sierra Wireless_GNSS iSerial 0 bNumConfigurations 1 Configuration Descriptor: bLength 9 bDescriptorType 2 wTotalLength 0x0020 bNumInterfaces 1 bConfigurationValue 1 iConfiguration 0 bmAttributes 0xc0 Self Powered MaxPower 50mA Interface Descriptor: bLength 9 bDescriptorType 4 bInterfaceNumber 0 bAlternateSetting 0 bNumEndpoints 2 bInterfaceClass 0 bInterfaceSubClass 0 bInterfaceProtocol 0 iInterface 0 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x81 EP 1 IN bmAttributes 2 Transfer Type Bulk Synch Type None Usage Type Data wMaxPacketSize 0x0040 1x 64 bytes bInterval 255 Endpoint Descriptor: bLength 7 bDescriptorType 5 bEndpointAddress 0x01 EP 1 OUT bmAttributes 2 Transfer Type Bulk Synch Type None Usage Type Data wMaxPacketSize 0x0040 1x 64 bytes bInterval 255 can't get debug descriptor: Resource temporarily unavailable Device Status: 0x0001 Self Powered Reported-by: Marc Ferland Link: https://lore.kernel.org/r/20211027200223.72701-1-ferlandm@amotus.ca Signed-off-by: Johan Hovold Tested-by: Marc Ferland --- drivers/gnss/usb.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/gnss/usb.c b/drivers/gnss/usb.c index 5c0251034def..792235a688ea 100644 --- a/drivers/gnss/usb.c +++ b/drivers/gnss/usb.c @@ -17,6 +17,7 @@ #define GNSS_USB_WRITE_TIMEOUT 1000 static const struct usb_device_id gnss_usb_id_table[] = { + { USB_DEVICE(0x1199, 0xb000) }, /* Sierra Wireless XM1210 */ { } }; MODULE_DEVICE_TABLE(usb, gnss_usb_id_table);