Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge patch series "can: esd_usb: More preparation before supporting esd CAN-USB/3"

Frank Jungclaus <frank.jungclaus@esd.eu> says:

Apply another small batch of patches as preparation for adding support
of the newly available esd CAN-USB/3 to esd_usb.c.

v1 -> v2:
* Make use of GENMASK() macro for ESD_USB_NO_BAUDRATE and
ESD_USB_IDMASK
* Also use the BIT() macro for ESD_USB2_3_SAMPLES
* Removed comments with redundant hexadecimal values from
BIT()-constants
* Reworded (shortened) the commit messages
* Changed the macro ESD_USB_3_SAMPLES to ESD_USB_TRIPLE_SAMPLES

v1:
* Link: https://lore.kernel.org/all/20230517192251.2405290-1-frank.jungclaus@esd.eu

Link: https://lore.kernel.org/r/20230519195600.420644-1-frank.jungclaus@esd.eu
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

+167 -170
+167 -170
drivers/net/can/usb/esd_usb.c
··· 3 3 * CAN driver for esd electronics gmbh CAN-USB/2 and CAN-USB/Micro 4 4 * 5 5 * Copyright (C) 2010-2012 esd electronic system design gmbh, Matthias Fuchs <socketcan@esd.eu> 6 - * Copyright (C) 2022 esd electronics gmbh, Frank Jungclaus <frank.jungclaus@esd.eu> 6 + * Copyright (C) 2022-2023 esd electronics gmbh, Frank Jungclaus <frank.jungclaus@esd.eu> 7 7 */ 8 - #include <linux/ethtool.h> 9 - #include <linux/signal.h> 10 - #include <linux/slab.h> 11 - #include <linux/module.h> 12 - #include <linux/netdevice.h> 13 - #include <linux/usb.h> 14 8 15 9 #include <linux/can.h> 16 10 #include <linux/can/dev.h> 17 11 #include <linux/can/error.h> 12 + #include <linux/ethtool.h> 13 + #include <linux/module.h> 14 + #include <linux/netdevice.h> 15 + #include <linux/signal.h> 16 + #include <linux/slab.h> 17 + #include <linux/units.h> 18 + #include <linux/usb.h> 18 19 19 20 MODULE_AUTHOR("Matthias Fuchs <socketcan@esd.eu>"); 20 21 MODULE_AUTHOR("Frank Jungclaus <frank.jungclaus@esd.eu>"); ··· 23 22 MODULE_LICENSE("GPL v2"); 24 23 25 24 /* USB vendor and product ID */ 26 - #define USB_ESDGMBH_VENDOR_ID 0x0ab4 27 - #define USB_CANUSB2_PRODUCT_ID 0x0010 28 - #define USB_CANUSBM_PRODUCT_ID 0x0011 25 + #define ESD_USB_ESDGMBH_VENDOR_ID 0x0ab4 26 + #define ESD_USB_CANUSB2_PRODUCT_ID 0x0010 27 + #define ESD_USB_CANUSBM_PRODUCT_ID 0x0011 29 28 30 29 /* CAN controller clock frequencies */ 31 - #define ESD_USB2_CAN_CLOCK 60000000 32 - #define ESD_USBM_CAN_CLOCK 36000000 30 + #define ESD_USB_2_CAN_CLOCK (60 * MEGA) /* Hz */ 31 + #define ESD_USB_M_CAN_CLOCK (36 * MEGA) /* Hz */ 33 32 34 33 /* Maximum number of CAN nets */ 35 34 #define ESD_USB_MAX_NETS 2 36 35 37 36 /* USB commands */ 38 - #define CMD_VERSION 1 /* also used for VERSION_REPLY */ 39 - #define CMD_CAN_RX 2 /* device to host only */ 40 - #define CMD_CAN_TX 3 /* also used for TX_DONE */ 41 - #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */ 42 - #define CMD_TS 5 /* also used for TS_REPLY */ 43 - #define CMD_IDADD 6 /* also used for IDADD_REPLY */ 37 + #define ESD_USB_CMD_VERSION 1 /* also used for VERSION_REPLY */ 38 + #define ESD_USB_CMD_CAN_RX 2 /* device to host only */ 39 + #define ESD_USB_CMD_CAN_TX 3 /* also used for TX_DONE */ 40 + #define ESD_USB_CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */ 41 + #define ESD_USB_CMD_TS 5 /* also used for TS_REPLY */ 42 + #define ESD_USB_CMD_IDADD 6 /* also used for IDADD_REPLY */ 44 43 45 44 /* esd CAN message flags - dlc field */ 46 - #define ESD_RTR 0x10 45 + #define ESD_RTR BIT(4) 46 + 47 47 48 48 /* esd CAN message flags - id field */ 49 - #define ESD_EXTID 0x20000000 50 - #define ESD_EVENT 0x40000000 51 - #define ESD_IDMASK 0x1fffffff 49 + #define ESD_USB_EXTID BIT(29) 50 + #define ESD_USB_EVENT BIT(30) 51 + #define ESD_USB_IDMASK GENMASK(28, 0) 52 52 53 53 /* esd CAN event ids */ 54 54 #define ESD_EV_CAN_ERROR_EXT 2 /* CAN controller specific diagnostic data */ 55 55 56 56 /* baudrate message flags */ 57 - #define ESD_USB_UBR 0x80000000 58 - #define ESD_USB_LOM 0x40000000 59 - #define ESD_USB_NO_BAUDRATE 0x7fffffff 57 + #define ESD_USB_LOM BIT(30) /* Listen Only Mode */ 58 + #define ESD_USB_UBR BIT(31) /* User Bit Rate (controller BTR) in bits 0..27 */ 59 + #define ESD_USB_NO_BAUDRATE GENMASK(30, 0) /* bit rate unconfigured */ 60 60 61 - /* bit timing CAN-USB/2 */ 62 - #define ESD_USB2_TSEG1_MIN 1 63 - #define ESD_USB2_TSEG1_MAX 16 64 - #define ESD_USB2_TSEG1_SHIFT 16 65 - #define ESD_USB2_TSEG2_MIN 1 66 - #define ESD_USB2_TSEG2_MAX 8 67 - #define ESD_USB2_TSEG2_SHIFT 20 68 - #define ESD_USB2_SJW_MAX 4 69 - #define ESD_USB2_SJW_SHIFT 14 70 - #define ESD_USBM_SJW_SHIFT 24 71 - #define ESD_USB2_BRP_MIN 1 72 - #define ESD_USB2_BRP_MAX 1024 73 - #define ESD_USB2_BRP_INC 1 74 - #define ESD_USB2_3_SAMPLES 0x00800000 61 + /* bit timing esd CAN-USB */ 62 + #define ESD_USB_2_TSEG1_SHIFT 16 63 + #define ESD_USB_2_TSEG2_SHIFT 20 64 + #define ESD_USB_2_SJW_SHIFT 14 65 + #define ESD_USB_M_SJW_SHIFT 24 66 + #define ESD_USB_TRIPLE_SAMPLES BIT(23) 75 67 76 68 /* esd IDADD message */ 77 - #define ESD_ID_ENABLE 0x80 78 - #define ESD_MAX_ID_SEGMENT 64 69 + #define ESD_USB_ID_ENABLE 0x80 70 + #define ESD_USB_MAX_ID_SEGMENT 64 79 71 80 72 /* SJA1000 ECC register (emulated by usb firmware) */ 81 - #define SJA1000_ECC_SEG 0x1F 82 - #define SJA1000_ECC_DIR 0x20 83 - #define SJA1000_ECC_ERR 0x06 84 - #define SJA1000_ECC_BIT 0x00 85 - #define SJA1000_ECC_FORM 0x40 86 - #define SJA1000_ECC_STUFF 0x80 87 - #define SJA1000_ECC_MASK 0xc0 73 + #define ESD_USB_SJA1000_ECC_SEG 0x1F 74 + #define ESD_USB_SJA1000_ECC_DIR 0x20 75 + #define ESD_USB_SJA1000_ECC_ERR 0x06 76 + #define ESD_USB_SJA1000_ECC_BIT 0x00 77 + #define ESD_USB_SJA1000_ECC_FORM 0x40 78 + #define ESD_USB_SJA1000_ECC_STUFF 0x80 79 + #define ESD_USB_SJA1000_ECC_MASK 0xc0 88 80 89 81 /* esd bus state event codes */ 90 - #define ESD_BUSSTATE_MASK 0xc0 91 - #define ESD_BUSSTATE_WARN 0x40 92 - #define ESD_BUSSTATE_ERRPASSIVE 0x80 93 - #define ESD_BUSSTATE_BUSOFF 0xc0 82 + #define ESD_USB_BUSSTATE_MASK 0xc0 83 + #define ESD_USB_BUSSTATE_WARN 0x40 84 + #define ESD_USB_BUSSTATE_ERRPASSIVE 0x80 85 + #define ESD_USB_BUSSTATE_BUSOFF 0xc0 94 86 95 - #define RX_BUFFER_SIZE 1024 96 - #define MAX_RX_URBS 4 97 - #define MAX_TX_URBS 16 /* must be power of 2 */ 87 + #define ESD_USB_RX_BUFFER_SIZE 1024 88 + #define ESD_USB_MAX_RX_URBS 4 89 + #define ESD_USB_MAX_TX_URBS 16 /* must be power of 2 */ 98 90 99 - struct header_msg { 100 - u8 len; /* len is always the total message length in 32bit words */ 91 + struct esd_usb_header_msg { 92 + u8 len; /* total message length in 32bit words */ 101 93 u8 cmd; 102 94 u8 rsvd[2]; 103 95 }; 104 96 105 - struct version_msg { 106 - u8 len; 97 + struct esd_usb_version_msg { 98 + u8 len; /* total message length in 32bit words */ 107 99 u8 cmd; 108 100 u8 rsvd; 109 101 u8 flags; 110 102 __le32 drv_version; 111 103 }; 112 104 113 - struct version_reply_msg { 114 - u8 len; 105 + struct esd_usb_version_reply_msg { 106 + u8 len; /* total message length in 32bit words */ 115 107 u8 cmd; 116 108 u8 nets; 117 109 u8 features; ··· 114 120 __le32 ts; 115 121 }; 116 122 117 - struct rx_msg { 118 - u8 len; 123 + struct esd_usb_rx_msg { 124 + u8 len; /* total message length in 32bit words */ 119 125 u8 cmd; 120 126 u8 net; 121 127 u8 dlc; 122 128 __le32 ts; 123 129 __le32 id; /* upper 3 bits contain flags */ 124 130 union { 125 - u8 data[8]; 131 + u8 data[CAN_MAX_DLEN]; 126 132 struct { 127 133 u8 status; /* CAN Controller Status */ 128 134 u8 ecc; /* Error Capture Register */ ··· 132 138 }; 133 139 }; 134 140 135 - struct tx_msg { 136 - u8 len; 141 + struct esd_usb_tx_msg { 142 + u8 len; /* total message length in 32bit words */ 137 143 u8 cmd; 138 144 u8 net; 139 145 u8 dlc; 140 146 u32 hnd; /* opaque handle, not used by device */ 141 147 __le32 id; /* upper 3 bits contain flags */ 142 - u8 data[8]; 148 + u8 data[CAN_MAX_DLEN]; 143 149 }; 144 150 145 - struct tx_done_msg { 146 - u8 len; 151 + struct esd_usb_tx_done_msg { 152 + u8 len; /* total message length in 32bit words */ 147 153 u8 cmd; 148 154 u8 net; 149 155 u8 status; ··· 151 157 __le32 ts; 152 158 }; 153 159 154 - struct id_filter_msg { 155 - u8 len; 160 + struct esd_usb_id_filter_msg { 161 + u8 len; /* total message length in 32bit words */ 156 162 u8 cmd; 157 163 u8 net; 158 164 u8 option; 159 - __le32 mask[ESD_MAX_ID_SEGMENT + 1]; 165 + __le32 mask[ESD_USB_MAX_ID_SEGMENT + 1]; /* +1 for 29bit extended IDs */ 160 166 }; 161 167 162 - struct set_baudrate_msg { 163 - u8 len; 168 + struct esd_usb_set_baudrate_msg { 169 + u8 len; /* total message length in 32bit words */ 164 170 u8 cmd; 165 171 u8 net; 166 172 u8 rsvd; ··· 169 175 170 176 /* Main message type used between library and application */ 171 177 union __packed esd_usb_msg { 172 - struct header_msg hdr; 173 - struct version_msg version; 174 - struct version_reply_msg version_reply; 175 - struct rx_msg rx; 176 - struct tx_msg tx; 177 - struct tx_done_msg txdone; 178 - struct set_baudrate_msg setbaud; 179 - struct id_filter_msg filter; 178 + struct esd_usb_header_msg hdr; 179 + struct esd_usb_version_msg version; 180 + struct esd_usb_version_reply_msg version_reply; 181 + struct esd_usb_rx_msg rx; 182 + struct esd_usb_tx_msg tx; 183 + struct esd_usb_tx_done_msg txdone; 184 + struct esd_usb_set_baudrate_msg setbaud; 185 + struct esd_usb_id_filter_msg filter; 180 186 }; 181 187 182 188 static struct usb_device_id esd_usb_table[] = { 183 - {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)}, 184 - {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)}, 189 + {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSB2_PRODUCT_ID)}, 190 + {USB_DEVICE(ESD_USB_ESDGMBH_VENDOR_ID, ESD_USB_CANUSBM_PRODUCT_ID)}, 185 191 {} 186 192 }; 187 193 MODULE_DEVICE_TABLE(usb, esd_usb_table); ··· 202 208 int net_count; 203 209 u32 version; 204 210 int rxinitdone; 205 - void *rxbuf[MAX_RX_URBS]; 206 - dma_addr_t rxbuf_dma[MAX_RX_URBS]; 211 + void *rxbuf[ESD_USB_MAX_RX_URBS]; 212 + dma_addr_t rxbuf_dma[ESD_USB_MAX_RX_URBS]; 207 213 }; 208 214 209 215 struct esd_usb_net_priv { ··· 211 217 212 218 atomic_t active_tx_jobs; 213 219 struct usb_anchor tx_submitted; 214 - struct esd_tx_urb_context tx_contexts[MAX_TX_URBS]; 220 + struct esd_tx_urb_context tx_contexts[ESD_USB_MAX_TX_URBS]; 215 221 216 222 struct esd_usb *usb; 217 223 struct net_device *netdev; ··· 226 232 struct net_device_stats *stats = &priv->netdev->stats; 227 233 struct can_frame *cf; 228 234 struct sk_buff *skb; 229 - u32 id = le32_to_cpu(msg->rx.id) & ESD_IDMASK; 235 + u32 id = le32_to_cpu(msg->rx.id) & ESD_USB_IDMASK; 230 236 231 237 if (id == ESD_EV_CAN_ERROR_EXT) { 232 238 u8 state = msg->rx.ev_can_err_ext.status; ··· 255 261 256 262 priv->old_state = state; 257 263 258 - switch (state & ESD_BUSSTATE_MASK) { 259 - case ESD_BUSSTATE_BUSOFF: 264 + switch (state & ESD_USB_BUSSTATE_MASK) { 265 + case ESD_USB_BUSSTATE_BUSOFF: 260 266 new_state = CAN_STATE_BUS_OFF; 261 267 can_bus_off(priv->netdev); 262 268 break; 263 - case ESD_BUSSTATE_WARN: 269 + case ESD_USB_BUSSTATE_WARN: 264 270 new_state = CAN_STATE_ERROR_WARNING; 265 271 break; 266 - case ESD_BUSSTATE_ERRPASSIVE: 272 + case ESD_USB_BUSSTATE_ERRPASSIVE: 267 273 new_state = CAN_STATE_ERROR_PASSIVE; 268 274 break; 269 275 default: ··· 285 291 286 292 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 287 293 288 - switch (ecc & SJA1000_ECC_MASK) { 289 - case SJA1000_ECC_BIT: 294 + switch (ecc & ESD_USB_SJA1000_ECC_MASK) { 295 + case ESD_USB_SJA1000_ECC_BIT: 290 296 cf->data[2] |= CAN_ERR_PROT_BIT; 291 297 break; 292 - case SJA1000_ECC_FORM: 298 + case ESD_USB_SJA1000_ECC_FORM: 293 299 cf->data[2] |= CAN_ERR_PROT_FORM; 294 300 break; 295 - case SJA1000_ECC_STUFF: 301 + case ESD_USB_SJA1000_ECC_STUFF: 296 302 cf->data[2] |= CAN_ERR_PROT_STUFF; 297 303 break; 298 304 default: ··· 300 306 } 301 307 302 308 /* Error occurred during transmission? */ 303 - if (!(ecc & SJA1000_ECC_DIR)) 309 + if (!(ecc & ESD_USB_SJA1000_ECC_DIR)) 304 310 cf->data[2] |= CAN_ERR_PROT_TX; 305 311 306 312 /* Bit stream position in CAN frame as the error was detected */ 307 - cf->data[3] = ecc & SJA1000_ECC_SEG; 313 + cf->data[3] = ecc & ESD_USB_SJA1000_ECC_SEG; 308 314 } 309 315 310 316 if (skb) { ··· 331 337 332 338 id = le32_to_cpu(msg->rx.id); 333 339 334 - if (id & ESD_EVENT) { 340 + if (id & ESD_USB_EVENT) { 335 341 esd_usb_rx_event(priv, msg); 336 342 } else { 337 343 skb = alloc_can_skb(priv->netdev, &cf); ··· 340 346 return; 341 347 } 342 348 343 - cf->can_id = id & ESD_IDMASK; 349 + cf->can_id = id & ESD_USB_IDMASK; 344 350 can_frame_set_cc_len(cf, msg->rx.dlc & ~ESD_RTR, 345 351 priv->can.ctrlmode); 346 352 347 - if (id & ESD_EXTID) 353 + if (id & ESD_USB_EXTID) 348 354 cf->can_id |= CAN_EFF_FLAG; 349 355 350 356 if (msg->rx.dlc & ESD_RTR) { ··· 371 377 if (!netif_device_present(netdev)) 372 378 return; 373 379 374 - context = &priv->tx_contexts[msg->txdone.hnd & (MAX_TX_URBS - 1)]; 380 + context = &priv->tx_contexts[msg->txdone.hnd & (ESD_USB_MAX_TX_URBS - 1)]; 375 381 376 382 if (!msg->txdone.status) { 377 383 stats->tx_packets++; ··· 383 389 } 384 390 385 391 /* Release context */ 386 - context->echo_index = MAX_TX_URBS; 392 + context->echo_index = ESD_USB_MAX_TX_URBS; 387 393 atomic_dec(&priv->active_tx_jobs); 388 394 389 395 netif_wake_queue(netdev); ··· 418 424 msg = (union esd_usb_msg *)(urb->transfer_buffer + pos); 419 425 420 426 switch (msg->hdr.cmd) { 421 - case CMD_CAN_RX: 427 + case ESD_USB_CMD_CAN_RX: 422 428 if (msg->rx.net >= dev->net_count) { 423 429 dev_err(dev->udev->dev.parent, "format error\n"); 424 430 break; ··· 427 433 esd_usb_rx_can_msg(dev->nets[msg->rx.net], msg); 428 434 break; 429 435 430 - case CMD_CAN_TX: 436 + case ESD_USB_CMD_CAN_TX: 431 437 if (msg->txdone.net >= dev->net_count) { 432 438 dev_err(dev->udev->dev.parent, "format error\n"); 433 439 break; ··· 438 444 break; 439 445 } 440 446 441 - pos += msg->hdr.len << 2; 447 + pos += msg->hdr.len * sizeof(u32); /* convert to # of bytes */ 442 448 443 449 if (pos > urb->actual_length) { 444 450 dev_err(dev->udev->dev.parent, "format error\n"); ··· 448 454 449 455 resubmit_urb: 450 456 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), 451 - urb->transfer_buffer, RX_BUFFER_SIZE, 457 + urb->transfer_buffer, ESD_USB_RX_BUFFER_SIZE, 452 458 esd_usb_read_bulk_callback, dev); 453 459 454 460 retval = usb_submit_urb(urb, GFP_ATOMIC); ··· 532 538 return usb_bulk_msg(dev->udev, 533 539 usb_sndbulkpipe(dev->udev, 2), 534 540 msg, 535 - msg->hdr.len << 2, 541 + msg->hdr.len * sizeof(u32), /* convert to # of bytes */ 536 542 &actual_length, 537 543 1000); 538 544 } ··· 557 563 if (dev->rxinitdone) 558 564 return 0; 559 565 560 - for (i = 0; i < MAX_RX_URBS; i++) { 566 + for (i = 0; i < ESD_USB_MAX_RX_URBS; i++) { 561 567 struct urb *urb = NULL; 562 568 u8 *buf = NULL; 563 569 dma_addr_t buf_dma; ··· 569 575 break; 570 576 } 571 577 572 - buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL, 578 + buf = usb_alloc_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, GFP_KERNEL, 573 579 &buf_dma); 574 580 if (!buf) { 575 581 dev_warn(dev->udev->dev.parent, ··· 582 588 583 589 usb_fill_bulk_urb(urb, dev->udev, 584 590 usb_rcvbulkpipe(dev->udev, 1), 585 - buf, RX_BUFFER_SIZE, 591 + buf, ESD_USB_RX_BUFFER_SIZE, 586 592 esd_usb_read_bulk_callback, dev); 587 593 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 588 594 usb_anchor_urb(urb, &dev->rx_submitted); ··· 590 596 err = usb_submit_urb(urb, GFP_KERNEL); 591 597 if (err) { 592 598 usb_unanchor_urb(urb); 593 - usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, 599 + usb_free_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, buf, 594 600 urb->transfer_dma); 595 601 goto freeurb; 596 602 } ··· 612 618 } 613 619 614 620 /* Warn if we've couldn't transmit all the URBs */ 615 - if (i < MAX_RX_URBS) { 621 + if (i < ESD_USB_MAX_RX_URBS) { 616 622 dev_warn(dev->udev->dev.parent, 617 623 "rx performance may be slow\n"); 618 624 } ··· 647 653 * the number of the starting bitmask (0..64) to the filter.option 648 654 * field followed by only some bitmasks. 649 655 */ 650 - msg->hdr.cmd = CMD_IDADD; 651 - msg->hdr.len = 2 + ESD_MAX_ID_SEGMENT; 656 + msg->hdr.cmd = ESD_USB_CMD_IDADD; 657 + msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32); /* # of 32bit words */ 652 658 msg->filter.net = priv->index; 653 - msg->filter.option = ESD_ID_ENABLE; /* start with segment 0 */ 654 - for (i = 0; i < ESD_MAX_ID_SEGMENT; i++) 659 + msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */ 660 + for (i = 0; i < ESD_USB_MAX_ID_SEGMENT; i++) 655 661 msg->filter.mask[i] = cpu_to_le32(0xffffffff); 656 662 /* enable 29bit extended IDs */ 657 - msg->filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001); 663 + msg->filter.mask[ESD_USB_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001); 658 664 659 665 err = esd_usb_send_msg(dev, msg); 660 666 if (err) ··· 683 689 684 690 usb_kill_anchored_urbs(&dev->rx_submitted); 685 691 686 - for (i = 0; i < MAX_RX_URBS; ++i) 687 - usb_free_coherent(dev->udev, RX_BUFFER_SIZE, 692 + for (i = 0; i < ESD_USB_MAX_RX_URBS; ++i) 693 + usb_free_coherent(dev->udev, ESD_USB_RX_BUFFER_SIZE, 688 694 dev->rxbuf[i], dev->rxbuf_dma[i]); 689 695 690 696 for (i = 0; i < dev->net_count; i++) { ··· 693 699 usb_kill_anchored_urbs(&priv->tx_submitted); 694 700 atomic_set(&priv->active_tx_jobs, 0); 695 701 696 - for (j = 0; j < MAX_TX_URBS; j++) 697 - priv->tx_contexts[j].echo_index = MAX_TX_URBS; 702 + for (j = 0; j < ESD_USB_MAX_TX_URBS; j++) 703 + priv->tx_contexts[j].echo_index = ESD_USB_MAX_TX_URBS; 698 704 } 699 705 } 700 706 } ··· 759 765 760 766 msg = (union esd_usb_msg *)buf; 761 767 762 - msg->hdr.len = 3; /* minimal length */ 763 - msg->hdr.cmd = CMD_CAN_TX; 768 + /* minimal length as # of 32bit words */ 769 + msg->hdr.len = offsetof(struct esd_usb_tx_msg, data) / sizeof(u32); 770 + msg->hdr.cmd = ESD_USB_CMD_CAN_TX; 764 771 msg->tx.net = priv->index; 765 772 msg->tx.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode); 766 773 msg->tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK); ··· 770 775 msg->tx.dlc |= ESD_RTR; 771 776 772 777 if (cf->can_id & CAN_EFF_FLAG) 773 - msg->tx.id |= cpu_to_le32(ESD_EXTID); 778 + msg->tx.id |= cpu_to_le32(ESD_USB_EXTID); 774 779 775 780 for (i = 0; i < cf->len; i++) 776 781 msg->tx.data[i] = cf->data[i]; 777 782 778 - msg->hdr.len += (cf->len + 3) >> 2; 783 + /* round up, then divide by 4 to add the payload length as # of 32bit words */ 784 + msg->hdr.len += DIV_ROUND_UP(cf->len, sizeof(u32)); 779 785 780 - for (i = 0; i < MAX_TX_URBS; i++) { 781 - if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { 786 + for (i = 0; i < ESD_USB_MAX_TX_URBS; i++) { 787 + if (priv->tx_contexts[i].echo_index == ESD_USB_MAX_TX_URBS) { 782 788 context = &priv->tx_contexts[i]; 783 789 break; 784 790 } ··· 799 803 msg->tx.hnd = 0x80000000 | i; /* returned in TX done message */ 800 804 801 805 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf, 802 - msg->hdr.len << 2, 806 + msg->hdr.len * sizeof(u32), /* convert to # of bytes */ 803 807 esd_usb_write_bulk_callback, context); 804 808 805 809 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; ··· 811 815 atomic_inc(&priv->active_tx_jobs); 812 816 813 817 /* Slow down tx path */ 814 - if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS) 818 + if (atomic_read(&priv->active_tx_jobs) >= ESD_USB_MAX_TX_URBS) 815 819 netif_stop_queue(netdev); 816 820 817 821 err = usb_submit_urb(urb, GFP_ATOMIC); ··· 861 865 return -ENOMEM; 862 866 863 867 /* Disable all IDs (see esd_usb_start()) */ 864 - msg->hdr.cmd = CMD_IDADD; 865 - msg->hdr.len = 2 + ESD_MAX_ID_SEGMENT; 868 + msg->hdr.cmd = ESD_USB_CMD_IDADD; 869 + msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32);/* # of 32bit words */ 866 870 msg->filter.net = priv->index; 867 - msg->filter.option = ESD_ID_ENABLE; /* start with segment 0 */ 868 - for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++) 871 + msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */ 872 + for (i = 0; i <= ESD_USB_MAX_ID_SEGMENT; i++) 869 873 msg->filter.mask[i] = 0; 870 874 if (esd_usb_send_msg(priv->usb, msg) < 0) 871 875 netdev_err(netdev, "sending idadd message failed\n"); 872 876 873 877 /* set CAN controller to reset mode */ 874 - msg->hdr.len = 2; 875 - msg->hdr.cmd = CMD_SETBAUD; 878 + msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */ 879 + msg->hdr.cmd = ESD_USB_CMD_SETBAUD; 876 880 msg->setbaud.net = priv->index; 877 881 msg->setbaud.rsvd = 0; 878 882 msg->setbaud.baud = cpu_to_le32(ESD_USB_NO_BAUDRATE); ··· 901 905 .get_ts_info = ethtool_op_get_ts_info, 902 906 }; 903 907 904 - static const struct can_bittiming_const esd_usb2_bittiming_const = { 905 - .name = "esd_usb2", 906 - .tseg1_min = ESD_USB2_TSEG1_MIN, 907 - .tseg1_max = ESD_USB2_TSEG1_MAX, 908 - .tseg2_min = ESD_USB2_TSEG2_MIN, 909 - .tseg2_max = ESD_USB2_TSEG2_MAX, 910 - .sjw_max = ESD_USB2_SJW_MAX, 911 - .brp_min = ESD_USB2_BRP_MIN, 912 - .brp_max = ESD_USB2_BRP_MAX, 913 - .brp_inc = ESD_USB2_BRP_INC, 908 + static const struct can_bittiming_const esd_usb_2_bittiming_const = { 909 + .name = "esd_usb_2", 910 + .tseg1_min = 1, 911 + .tseg1_max = 16, 912 + .tseg2_min = 1, 913 + .tseg2_max = 8, 914 + .sjw_max = 4, 915 + .brp_min = 1, 916 + .brp_max = 1024, 917 + .brp_inc = 1, 914 918 }; 915 919 916 - static int esd_usb2_set_bittiming(struct net_device *netdev) 920 + static int esd_usb_2_set_bittiming(struct net_device *netdev) 917 921 { 922 + const struct can_bittiming_const *btc = &esd_usb_2_bittiming_const; 918 923 struct esd_usb_net_priv *priv = netdev_priv(netdev); 919 924 struct can_bittiming *bt = &priv->can.bittiming; 920 925 union esd_usb_msg *msg; ··· 927 930 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 928 931 canbtr |= ESD_USB_LOM; 929 932 930 - canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1); 933 + canbtr |= (bt->brp - 1) & (btc->brp_max - 1); 931 934 932 935 if (le16_to_cpu(priv->usb->udev->descriptor.idProduct) == 933 - USB_CANUSBM_PRODUCT_ID) 934 - sjw_shift = ESD_USBM_SJW_SHIFT; 936 + ESD_USB_CANUSBM_PRODUCT_ID) 937 + sjw_shift = ESD_USB_M_SJW_SHIFT; 935 938 else 936 - sjw_shift = ESD_USB2_SJW_SHIFT; 939 + sjw_shift = ESD_USB_2_SJW_SHIFT; 937 940 938 - canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1)) 941 + canbtr |= ((bt->sjw - 1) & (btc->sjw_max - 1)) 939 942 << sjw_shift; 940 943 canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1) 941 - & (ESD_USB2_TSEG1_MAX - 1)) 942 - << ESD_USB2_TSEG1_SHIFT; 943 - canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1)) 944 - << ESD_USB2_TSEG2_SHIFT; 944 + & (btc->tseg1_max - 1)) 945 + << ESD_USB_2_TSEG1_SHIFT; 946 + canbtr |= ((bt->phase_seg2 - 1) & (btc->tseg2_max - 1)) 947 + << ESD_USB_2_TSEG2_SHIFT; 945 948 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 946 - canbtr |= ESD_USB2_3_SAMPLES; 949 + canbtr |= ESD_USB_TRIPLE_SAMPLES; 947 950 948 951 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 949 952 if (!msg) 950 953 return -ENOMEM; 951 954 952 - msg->hdr.len = 2; 953 - msg->hdr.cmd = CMD_SETBAUD; 955 + msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */ 956 + msg->hdr.cmd = ESD_USB_CMD_SETBAUD; 954 957 msg->setbaud.net = priv->index; 955 958 msg->setbaud.rsvd = 0; 956 959 msg->setbaud.baud = cpu_to_le32(canbtr); 957 960 958 - netdev_info(netdev, "setting BTR=%#x\n", canbtr); 961 + netdev_dbg(netdev, "setting BTR=%#x\n", canbtr); 959 962 960 963 err = esd_usb_send_msg(priv->usb, msg); 961 964 ··· 996 999 int err = 0; 997 1000 int i; 998 1001 999 - netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS); 1002 + netdev = alloc_candev(sizeof(*priv), ESD_USB_MAX_TX_URBS); 1000 1003 if (!netdev) { 1001 1004 dev_err(&intf->dev, "couldn't alloc candev\n"); 1002 1005 err = -ENOMEM; ··· 1008 1011 init_usb_anchor(&priv->tx_submitted); 1009 1012 atomic_set(&priv->active_tx_jobs, 0); 1010 1013 1011 - for (i = 0; i < MAX_TX_URBS; i++) 1012 - priv->tx_contexts[i].echo_index = MAX_TX_URBS; 1014 + for (i = 0; i < ESD_USB_MAX_TX_URBS; i++) 1015 + priv->tx_contexts[i].echo_index = ESD_USB_MAX_TX_URBS; 1013 1016 1014 1017 priv->usb = dev; 1015 1018 priv->netdev = netdev; ··· 1021 1024 CAN_CTRLMODE_BERR_REPORTING; 1022 1025 1023 1026 if (le16_to_cpu(dev->udev->descriptor.idProduct) == 1024 - USB_CANUSBM_PRODUCT_ID) 1025 - priv->can.clock.freq = ESD_USBM_CAN_CLOCK; 1027 + ESD_USB_CANUSBM_PRODUCT_ID) 1028 + priv->can.clock.freq = ESD_USB_M_CAN_CLOCK; 1026 1029 else { 1027 - priv->can.clock.freq = ESD_USB2_CAN_CLOCK; 1030 + priv->can.clock.freq = ESD_USB_2_CAN_CLOCK; 1028 1031 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 1029 1032 } 1030 1033 1031 - priv->can.bittiming_const = &esd_usb2_bittiming_const; 1032 - priv->can.do_set_bittiming = esd_usb2_set_bittiming; 1034 + priv->can.bittiming_const = &esd_usb_2_bittiming_const; 1035 + priv->can.do_set_bittiming = esd_usb_2_set_bittiming; 1033 1036 priv->can.do_set_mode = esd_usb_set_mode; 1034 1037 priv->can.do_get_berr_counter = esd_usb_get_berr_counter; 1035 1038 ··· 1087 1090 } 1088 1091 1089 1092 /* query number of CAN interfaces (nets) */ 1090 - msg->hdr.cmd = CMD_VERSION; 1091 - msg->hdr.len = 2; 1093 + msg->hdr.cmd = ESD_USB_CMD_VERSION; 1094 + msg->hdr.len = sizeof(struct esd_usb_version_msg) / sizeof(u32); /* # of 32bit words */ 1092 1095 msg->version.rsvd = 0; 1093 1096 msg->version.flags = 0; 1094 1097 msg->version.drv_version = 0;