linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>
To: linux-can@vger.kernel.org
Cc: wg@grandegger.com, mkl@pengutronix.de,
	Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>
Subject: [PATCH 05/17] can: ems_usb: Replace constant define RX_BUFFER_SIZE by variable bulk_rd_buf_size, because this will have different values for CPC-USB/ARM7 and CPC-USB/FD. For the same reason added a function pointer ems_usb_write_mode. In device probe function added a switch statement to select between CPC-USB/ARM7 and CPC-USB/FD and rearranged initialization sequence accordingly.
Date: Fri,  6 Nov 2020 18:01:54 +0100	[thread overview]
Message-ID: <20201106170206.32162-6-uttenthaler@ems-wuensche.com> (raw)
In-Reply-To: <20201106170206.32162-1-uttenthaler@ems-wuensche.com>

Signed-off-by: Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>
---
 drivers/net/can/usb/ems_usb.c | 66 +++++++++++++++++++++++++----------
 1 file changed, 47 insertions(+), 19 deletions(-)

diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c
index 4ed0d681a68c..a3943042b8c8 100644
--- a/drivers/net/can/usb/ems_usb.c
+++ b/drivers/net/can/usb/ems_usb.c
@@ -266,7 +266,6 @@ static struct usb_device_id ems_usb_table[] = {
 
 MODULE_DEVICE_TABLE(usb, ems_usb_table);
 
-#define RX_BUFFER_SIZE      64
 #define CPC_HEADER_SIZE     4
 #define INTR_IN_BUFFER_SIZE 4
 
@@ -290,6 +289,8 @@ struct ems_usb {
 	struct usb_device *udev;
 	struct net_device *netdev;
 
+	u32 bulk_rd_buf_size;
+
 	atomic_t active_tx_urbs;
 	struct usb_anchor tx_submitted;
 	struct ems_tx_urb_context tx_contexts[MAX_TX_URBS];
@@ -301,7 +302,9 @@ struct ems_usb {
 	u8 *tx_msg_buffer;
 
 	u8 *intr_in_buffer;
-	unsigned int free_slots; /* remember number of available slots */
+	u32 free_slots; /* remember number of available slots */
+
+	int (*ems_usb_write_mode)(struct ems_usb *dev, u32 mode);
 
 	struct ems_cpc_msg active_params; /* active controller parameters */
 };
@@ -522,7 +525,7 @@ static void ems_usb_read_bulk_callback(struct urb *urb)
 
 resubmit_urb:
 	usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
-			  urb->transfer_buffer, RX_BUFFER_SIZE,
+			  urb->transfer_buffer, dev->bulk_rd_buf_size,
 			  ems_usb_read_bulk_callback, dev);
 
 	retval = usb_submit_urb(urb, GFP_ATOMIC);
@@ -596,9 +599,18 @@ static int ems_usb_command_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
 
 /* Change CAN controllers' mode register
  */
-static int ems_usb_write_mode(struct ems_usb *dev, u8 mode)
+static int ems_usb_write_mode_arm7(struct ems_usb *dev, u32 mode)
 {
-	dev->active_params.msg.can_params.cc_params.sja1000.mode = mode;
+	struct cpc_sja1000_params *sja1000 =
+		&dev->active_params.msg.can_params.cc_params.sja1000;
+
+	if (mode == CPC_USB_RESET_MODE)
+		sja1000->mode |= SJA1000_MOD_RM;
+	else if (mode == CPC_USB_RUN_MODE)
+		sja1000->mode &= ~SJA1000_MOD_RM;
+
+	else
+		return -EINVAL;
 
 	return ems_usb_command_msg(dev, &dev->active_params);
 }
@@ -641,7 +653,7 @@ static int ems_usb_start(struct ems_usb *dev)
 			break;
 		}
 
-		buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
+		buf = usb_alloc_coherent(dev->udev, dev->bulk_rd_buf_size, GFP_KERNEL,
 					 &urb->transfer_dma);
 		if (!buf) {
 			netdev_err(netdev, "No memory left for USB buffer\n");
@@ -651,7 +663,7 @@ static int ems_usb_start(struct ems_usb *dev)
 		}
 
 		usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
-				  buf, RX_BUFFER_SIZE,
+				  buf, dev->bulk_rd_buf_size,
 				  ems_usb_read_bulk_callback, dev);
 		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 		usb_anchor_urb(urb, &dev->rx_submitted);
@@ -659,7 +671,7 @@ static int ems_usb_start(struct ems_usb *dev)
 		err = usb_submit_urb(urb, GFP_KERNEL);
 		if (err) {
 			usb_unanchor_urb(urb);
-			usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
+			usb_free_coherent(dev->udev, dev->bulk_rd_buf_size, buf,
 					  urb->transfer_dma);
 			usb_free_urb(urb);
 			break;
@@ -708,7 +720,7 @@ static int ems_usb_start(struct ems_usb *dev)
 	if (err)
 		goto failed;
 
-	err = ems_usb_write_mode(dev, SJA1000_MOD_NORMAL);
+	err = dev->can.do_set_mode(netdev, CAN_MODE_START);
 	if (err)
 		goto failed;
 
@@ -742,7 +754,7 @@ static int ems_usb_open(struct net_device *netdev)
 	struct ems_usb *dev = netdev_priv(netdev);
 	int err;
 
-	err = ems_usb_write_mode(dev, SJA1000_MOD_RM);
+	err = dev->ems_usb_write_mode(dev, CPC_USB_RESET_MODE);
 	if (err)
 		return err;
 
@@ -900,7 +912,7 @@ static int ems_usb_close(struct net_device *netdev)
 	netif_stop_queue(netdev);
 
 	/* Set CAN controller to reset mode */
-	if (ems_usb_write_mode(dev, SJA1000_MOD_RM))
+	if (dev->ems_usb_write_mode(dev, CPC_USB_RESET_MODE))
 		netdev_warn(netdev, "couldn't stop device");
 
 	close_candev(netdev);
@@ -915,8 +927,8 @@ static const struct net_device_ops ems_usb_netdev_ops = {
 	.ndo_change_mtu = can_change_mtu,
 };
 
-static const struct can_bittiming_const ems_usb_bittiming_const = {
-	.name = "ems_usb",
+static const struct can_bittiming_const ems_usb_bittiming_const_arm7 = {
+	.name = "ems_usb_arm7",
 	.tseg1_min = 1,
 	.tseg1_max = 16,
 	.tseg2_min = 1,
@@ -933,7 +945,7 @@ static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
 
 	switch (mode) {
 	case CAN_MODE_START:
-		if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL))
+		if (dev->ems_usb_write_mode(dev, CPC_USB_RUN_MODE))
 			netdev_warn(netdev, "couldn't start device");
 
 		if (netif_queue_stopped(netdev))
@@ -947,7 +959,7 @@ static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
 	return 0;
 }
 
-static int ems_usb_set_bittiming(struct net_device *netdev)
+static int ems_usb_set_bittiming_arm7(struct net_device *netdev)
 {
 	struct ems_usb *dev = netdev_priv(netdev);
 	struct can_bittiming *bt = &dev->can.bittiming;
@@ -1018,11 +1030,29 @@ static int ems_usb_probe(struct usb_interface *intf,
 	dev->netdev = netdev;
 
 	dev->can.state = CAN_STATE_STOPPED;
+
+	/* Select CPC-USB/ARM7 or CPC-USB/FD */
+	switch (dev->udev->descriptor.idProduct) {
+	case USB_CPCUSB_ARM7_PRODUCT_ID:
+
 	dev->can.clock.freq = EMS_USB_ARM7_CLOCK;
-	dev->can.bittiming_const = &ems_usb_bittiming_const;
-	dev->can.do_set_bittiming = ems_usb_set_bittiming;
+	dev->can.bittiming_const = &ems_usb_bittiming_const_arm7;
+	dev->can.do_set_bittiming = ems_usb_set_bittiming_arm7;
 	dev->can.do_set_mode = ems_usb_set_mode;
 	dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
+	init_params_sja1000(&dev->active_params);
+	dev->ems_usb_write_mode = ems_usb_write_mode_arm7;
+	dev->bulk_rd_buf_size = 64;
+	break;
+
+	case USB_CPCUSB_FD_PRODUCT_ID:
+	// Placeholder for next patchess
+	break;
+
+	default:
+		err = -ENODEV;
+		goto cleanup_candev;
+	}
 
 	netdev->netdev_ops = &ems_usb_netdev_ops;
 
@@ -1053,8 +1083,6 @@ static int ems_usb_probe(struct usb_interface *intf,
 
 	SET_NETDEV_DEV(netdev, &intf->dev);
 
-	init_params_sja1000(&dev->active_params);
-
 	err = ems_usb_command_msg(dev, &dev->active_params);
 	if (err) {
 		netdev_err(netdev, "couldn't initialize controller: %d\n", err);
-- 
2.26.2

-- 
EMS Dr. Thomas Wuensche e.K.
Sonnenhang 3
85304 Ilmmuenster
HR Ingolstadt, HRA 170106

Phone: +49-8441-490260
Fax  : +49-8441-81860
http://www.ems-wuensche.com

  parent reply	other threads:[~2020-11-06 17:11 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-06 17:01 [PATCH 00/17] Add support for CPC-USB/FD CAN FD interface Gerhard Uttenthaler
2020-11-06 17:01 ` [PATCH 01/17] can: ems_usb: Fixed warnings given by checkpatch.pl and fixed some outdated comments Gerhard Uttenthaler
2020-11-06 17:04   ` Marc Kleine-Budde
2020-11-06 17:01 ` [PATCH 02/17] can: ems_usb: Added defines and product id needed for CPC-USB/FD Gerhard Uttenthaler
2020-11-06 17:01 ` [PATCH 03/17] can: ems_usb: Added CAN FD message representation Gerhard Uttenthaler
2020-11-06 17:08   ` Marc Kleine-Budde
2020-11-06 17:01 ` [PATCH 04/17] can: ems_usb: Added struct used for CAN FD initialization Gerhard Uttenthaler
2020-11-06 17:07   ` Marc Kleine-Budde
2020-11-06 17:01 ` Gerhard Uttenthaler [this message]
2020-11-06 17:15   ` [PATCH 05/17] can: ems_usb: Replace constant define RX_BUFFER_SIZE by variable bulk_rd_buf_size, because this will have different values for CPC-USB/ARM7 and CPC-USB/FD. For the same reason added a function pointer ems_usb_write_mode. In device probe function added a switch statement to select between CPC-USB/ARM7 and CPC-USB/FD and rearranged initialization sequence accordingly Marc Kleine-Budde
2020-11-10 10:31     ` Gerhard Uttenthaler
2020-11-10 10:57       ` Marc Kleine-Budde
2020-11-06 17:01 ` [PATCH 06/17] can: ems_usb: Added listen only mode for CPC-USB/ARM7 and moved evaluation of can.ctrlmode from set_bittiming routine to ems_usb_write_mode_arm7 routine. Wrapped a long line Gerhard Uttenthaler
2020-11-06 17:23   ` Marc Kleine-Budde
2020-11-06 17:01 ` [PATCH 07/17] can: ems_usb: Added CPC_ClearCmdQueue routine to get access to the interface even it is flooded with messages which cannot successfully be sent. Set timestamp to 0 in ems_usb_control_cmd Gerhard Uttenthaler
2020-11-06 17:25   ` Marc Kleine-Budde
2020-11-06 17:01 ` [PATCH 08/17] can: ems_usb: Modified ems_usb_read_bulk_callback to be able to handle also CPC-USB/FD Gerhard Uttenthaler
2020-11-06 17:32   ` Marc Kleine-Budde
2020-11-06 17:01 ` [PATCH 09/17] can: ems_usb: For CPC-USB/FD added clock definitions, bittiming constants, set_bittiming functions, bittiming init function and add all that to probe function Gerhard Uttenthaler
2020-11-06 17:51   ` Marc Kleine-Budde
2020-11-11 10:22     ` Gerhard Uttenthaler
2020-11-11 11:28       ` Marc Kleine-Budde
2020-11-06 17:01 ` [PATCH 10/17] can: ems_usb: Added receive routine for CAN FD messages and its call in ems_usb_read_bulk_callback Gerhard Uttenthaler
2020-11-06 17:33   ` Marc Kleine-Budde
2020-11-06 17:43   ` Marc Kleine-Budde
2020-11-06 17:02 ` [PATCH 11/17] can: ems_usb: Added ems_usb_write_mode_fd and its call in device probe routine. Fixed indentation Gerhard Uttenthaler
2020-11-06 17:35   ` Marc Kleine-Budde
2020-11-06 17:02 ` [PATCH 12/17] can: ems_usb: In ems_usb_start_xmit send only bytes with valid content to interface and not the complete buffer. Set first four bytes of buffer to 0. Wrapped long lines Gerhard Uttenthaler
2020-11-06 17:58   ` Marc Kleine-Budde
2020-11-12  8:31     ` Gerhard Uttenthaler
2020-11-12  8:35       ` Marc Kleine-Budde
2020-11-06 17:02 ` [PATCH 13/17] can: ems_usb: Rearrange code in ems_usb_start_xmit to check with can_is_canfd_skb for CAN FD frames Gerhard Uttenthaler
2020-11-06 18:01   ` Marc Kleine-Budde
2020-11-06 17:02 ` [PATCH 14/17] can: ems_usb: Added code to handle CAN FD frames in ems_usb_start_xmit Gerhard Uttenthaler
2020-11-06 17:56   ` Marc Kleine-Budde
2020-11-06 17:02 ` [PATCH 15/17] can: ems_usb: In CAN error handling routine checking which CAN controller type is issuing the error Gerhard Uttenthaler
2020-11-06 18:01   ` Marc Kleine-Budde
2020-11-06 17:02 ` [PATCH 16/17] can: ems_usb: Added error handling part for CPC-USB/FD. Get some structures packed Gerhard Uttenthaler
2020-11-06 18:05   ` Marc Kleine-Budde
2020-11-06 17:02 ` [PATCH 17/17] can: ems_usb: Made another struct packed. Enable CPC-USB/FD by adding it to the drivers device table Gerhard Uttenthaler
2020-11-06 18:05   ` Marc Kleine-Budde
2020-11-06 18:07 ` [PATCH 00/17] Add support for CPC-USB/FD CAN FD interface Marc Kleine-Budde
2020-11-06 19:04   ` Gerhard Uttenthaler
2020-11-06 19:59     ` Marc Kleine-Budde
2020-12-08 17:13       ` Gerhard Uttenthaler
2020-11-06 18:15 ` Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201106170206.32162-6-uttenthaler@ems-wuensche.com \
    --to=uttenthaler@ems-wuensche.com \
    --cc=linux-can@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=wg@grandegger.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).