Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CAN driver for esd CAN-USB/2 |
| 3 | * |
| 4 | * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published |
| 8 | * by the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/signal.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/usb.h> |
| 25 | |
| 26 | #include <linux/can.h> |
| 27 | #include <linux/can/dev.h> |
| 28 | #include <linux/can/error.h> |
| 29 | |
| 30 | MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>"); |
| 31 | MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 interfaces"); |
| 32 | MODULE_LICENSE("GPL v2"); |
| 33 | |
| 34 | /* Define these values to match your devices */ |
| 35 | #define USB_ESDGMBH_VENDOR_ID 0x0ab4 |
| 36 | #define USB_CANUSB2_PRODUCT_ID 0x0010 |
| 37 | |
| 38 | #define ESD_USB2_CAN_CLOCK 60000000 |
| 39 | #define ESD_USB2_MAX_NETS 2 |
| 40 | |
| 41 | /* USB2 commands */ |
| 42 | #define CMD_VERSION 1 /* also used for VERSION_REPLY */ |
| 43 | #define CMD_CAN_RX 2 /* device to host only */ |
| 44 | #define CMD_CAN_TX 3 /* also used for TX_DONE */ |
| 45 | #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */ |
| 46 | #define CMD_TS 5 /* also used for TS_REPLY */ |
| 47 | #define CMD_IDADD 6 /* also used for IDADD_REPLY */ |
| 48 | |
| 49 | /* esd CAN message flags - dlc field */ |
| 50 | #define ESD_RTR 0x10 |
| 51 | |
| 52 | /* esd CAN message flags - id field */ |
| 53 | #define ESD_EXTID 0x20000000 |
| 54 | #define ESD_EVENT 0x40000000 |
| 55 | #define ESD_IDMASK 0x1fffffff |
| 56 | |
| 57 | /* esd CAN event ids used by this driver */ |
| 58 | #define ESD_EV_CAN_ERROR_EXT 2 |
| 59 | |
| 60 | /* baudrate message flags */ |
| 61 | #define ESD_USB2_UBR 0x80000000 |
| 62 | #define ESD_USB2_LOM 0x40000000 |
| 63 | #define ESD_USB2_NO_BAUDRATE 0x7fffffff |
| 64 | #define ESD_USB2_TSEG1_MIN 1 |
| 65 | #define ESD_USB2_TSEG1_MAX 16 |
| 66 | #define ESD_USB2_TSEG1_SHIFT 16 |
| 67 | #define ESD_USB2_TSEG2_MIN 1 |
| 68 | #define ESD_USB2_TSEG2_MAX 8 |
| 69 | #define ESD_USB2_TSEG2_SHIFT 20 |
| 70 | #define ESD_USB2_SJW_MAX 4 |
| 71 | #define ESD_USB2_SJW_SHIFT 14 |
| 72 | #define ESD_USB2_BRP_MIN 1 |
| 73 | #define ESD_USB2_BRP_MAX 1024 |
| 74 | #define ESD_USB2_BRP_INC 1 |
| 75 | #define ESD_USB2_3_SAMPLES 0x00800000 |
| 76 | |
| 77 | /* esd IDADD message */ |
| 78 | #define ESD_ID_ENABLE 0x80 |
| 79 | #define ESD_MAX_ID_SEGMENT 64 |
| 80 | |
| 81 | /* SJA1000 ECC register (emulated by usb2 firmware) */ |
| 82 | #define SJA1000_ECC_SEG 0x1F |
| 83 | #define SJA1000_ECC_DIR 0x20 |
| 84 | #define SJA1000_ECC_ERR 0x06 |
| 85 | #define SJA1000_ECC_BIT 0x00 |
| 86 | #define SJA1000_ECC_FORM 0x40 |
| 87 | #define SJA1000_ECC_STUFF 0x80 |
| 88 | #define SJA1000_ECC_MASK 0xc0 |
| 89 | |
| 90 | /* esd bus state event codes */ |
| 91 | #define ESD_BUSSTATE_MASK 0xc0 |
| 92 | #define ESD_BUSSTATE_WARN 0x40 |
| 93 | #define ESD_BUSSTATE_ERRPASSIVE 0x80 |
| 94 | #define ESD_BUSSTATE_BUSOFF 0xc0 |
| 95 | |
| 96 | #define RX_BUFFER_SIZE 1024 |
| 97 | #define MAX_RX_URBS 4 |
| 98 | #define MAX_TX_URBS 16 /* must be power of 2 */ |
| 99 | |
| 100 | struct header_msg { |
| 101 | u8 len; /* len is always the total message length in 32bit words */ |
| 102 | u8 cmd; |
| 103 | u8 rsvd[2]; |
| 104 | }; |
| 105 | |
| 106 | struct version_msg { |
| 107 | u8 len; |
| 108 | u8 cmd; |
| 109 | u8 rsvd; |
| 110 | u8 flags; |
| 111 | __le32 drv_version; |
| 112 | }; |
| 113 | |
| 114 | struct version_reply_msg { |
| 115 | u8 len; |
| 116 | u8 cmd; |
| 117 | u8 nets; |
| 118 | u8 features; |
| 119 | __le32 version; |
| 120 | u8 name[16]; |
| 121 | __le32 rsvd; |
| 122 | __le32 ts; |
| 123 | }; |
| 124 | |
| 125 | struct rx_msg { |
| 126 | u8 len; |
| 127 | u8 cmd; |
| 128 | u8 net; |
| 129 | u8 dlc; |
| 130 | __le32 ts; |
| 131 | __le32 id; /* upper 3 bits contain flags */ |
| 132 | u8 data[8]; |
| 133 | }; |
| 134 | |
| 135 | struct tx_msg { |
| 136 | u8 len; |
| 137 | u8 cmd; |
| 138 | u8 net; |
| 139 | u8 dlc; |
| 140 | __le32 hnd; |
| 141 | __le32 id; /* upper 3 bits contain flags */ |
| 142 | u8 data[8]; |
| 143 | }; |
| 144 | |
| 145 | struct tx_done_msg { |
| 146 | u8 len; |
| 147 | u8 cmd; |
| 148 | u8 net; |
| 149 | u8 status; |
| 150 | __le32 hnd; |
| 151 | __le32 ts; |
| 152 | }; |
| 153 | |
| 154 | struct id_filter_msg { |
| 155 | u8 len; |
| 156 | u8 cmd; |
| 157 | u8 net; |
| 158 | u8 option; |
| 159 | __le32 mask[ESD_MAX_ID_SEGMENT + 1]; |
| 160 | }; |
| 161 | |
| 162 | struct set_baudrate_msg { |
| 163 | u8 len; |
| 164 | u8 cmd; |
| 165 | u8 net; |
| 166 | u8 rsvd; |
| 167 | __le32 baud; |
| 168 | }; |
| 169 | |
| 170 | /* Main message type used between library and application */ |
| 171 | struct __attribute__ ((packed)) esd_usb2_msg { |
| 172 | union { |
| 173 | struct header_msg hdr; |
| 174 | struct version_msg version; |
| 175 | struct version_reply_msg version_reply; |
| 176 | struct rx_msg rx; |
| 177 | struct tx_msg tx; |
| 178 | struct tx_done_msg txdone; |
| 179 | struct set_baudrate_msg setbaud; |
| 180 | struct id_filter_msg filter; |
| 181 | } msg; |
| 182 | }; |
| 183 | |
| 184 | static struct usb_device_id esd_usb2_table[] = { |
| 185 | {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)}, |
| 186 | {} |
| 187 | }; |
| 188 | MODULE_DEVICE_TABLE(usb, esd_usb2_table); |
| 189 | |
| 190 | struct esd_usb2_net_priv; |
| 191 | |
| 192 | struct esd_tx_urb_context { |
| 193 | struct esd_usb2_net_priv *priv; |
| 194 | u32 echo_index; |
| 195 | int dlc; |
| 196 | }; |
| 197 | |
| 198 | struct esd_usb2 { |
| 199 | struct usb_device *udev; |
| 200 | struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS]; |
| 201 | |
| 202 | struct usb_anchor rx_submitted; |
| 203 | |
| 204 | int net_count; |
| 205 | u32 version; |
| 206 | int rxinitdone; |
| 207 | }; |
| 208 | |
| 209 | struct esd_usb2_net_priv { |
| 210 | struct can_priv can; /* must be the first member */ |
| 211 | |
| 212 | atomic_t active_tx_jobs; |
| 213 | struct usb_anchor tx_submitted; |
| 214 | struct esd_tx_urb_context tx_contexts[MAX_TX_URBS]; |
| 215 | |
| 216 | int open_time; |
| 217 | struct esd_usb2 *usb2; |
| 218 | struct net_device *netdev; |
| 219 | int index; |
| 220 | u8 old_state; |
| 221 | struct can_berr_counter bec; |
| 222 | }; |
| 223 | |
| 224 | static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv, |
| 225 | struct esd_usb2_msg *msg) |
| 226 | { |
| 227 | struct net_device_stats *stats = &priv->netdev->stats; |
| 228 | struct can_frame *cf; |
| 229 | struct sk_buff *skb; |
| 230 | u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK; |
| 231 | |
| 232 | if (id == ESD_EV_CAN_ERROR_EXT) { |
| 233 | u8 state = msg->msg.rx.data[0]; |
| 234 | u8 ecc = msg->msg.rx.data[1]; |
| 235 | u8 txerr = msg->msg.rx.data[2]; |
| 236 | u8 rxerr = msg->msg.rx.data[3]; |
| 237 | |
| 238 | skb = alloc_can_err_skb(priv->netdev, &cf); |
| 239 | if (skb == NULL) { |
| 240 | stats->rx_dropped++; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (state != priv->old_state) { |
| 245 | priv->old_state = state; |
| 246 | |
| 247 | switch (state & ESD_BUSSTATE_MASK) { |
| 248 | case ESD_BUSSTATE_BUSOFF: |
| 249 | priv->can.state = CAN_STATE_BUS_OFF; |
| 250 | cf->can_id |= CAN_ERR_BUSOFF; |
| 251 | can_bus_off(priv->netdev); |
| 252 | break; |
| 253 | case ESD_BUSSTATE_WARN: |
| 254 | priv->can.state = CAN_STATE_ERROR_WARNING; |
| 255 | priv->can.can_stats.error_warning++; |
| 256 | break; |
| 257 | case ESD_BUSSTATE_ERRPASSIVE: |
| 258 | priv->can.state = CAN_STATE_ERROR_PASSIVE; |
| 259 | priv->can.can_stats.error_passive++; |
| 260 | break; |
| 261 | default: |
| 262 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 263 | break; |
| 264 | } |
| 265 | } else { |
| 266 | priv->can.can_stats.bus_error++; |
| 267 | stats->rx_errors++; |
| 268 | |
| 269 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; |
| 270 | |
| 271 | switch (ecc & SJA1000_ECC_MASK) { |
| 272 | case SJA1000_ECC_BIT: |
| 273 | cf->data[2] |= CAN_ERR_PROT_BIT; |
| 274 | break; |
| 275 | case SJA1000_ECC_FORM: |
| 276 | cf->data[2] |= CAN_ERR_PROT_FORM; |
| 277 | break; |
| 278 | case SJA1000_ECC_STUFF: |
| 279 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
| 280 | break; |
| 281 | default: |
| 282 | cf->data[2] |= CAN_ERR_PROT_UNSPEC; |
| 283 | cf->data[3] = ecc & SJA1000_ECC_SEG; |
| 284 | break; |
| 285 | } |
| 286 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 287 | /* Error occurred during transmission? */ |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 288 | if (!(ecc & SJA1000_ECC_DIR)) |
| 289 | cf->data[2] |= CAN_ERR_PROT_TX; |
| 290 | |
| 291 | if (priv->can.state == CAN_STATE_ERROR_WARNING || |
| 292 | priv->can.state == CAN_STATE_ERROR_PASSIVE) { |
| 293 | cf->data[1] = (txerr > rxerr) ? |
| 294 | CAN_ERR_CRTL_TX_PASSIVE : |
| 295 | CAN_ERR_CRTL_RX_PASSIVE; |
| 296 | } |
| 297 | cf->data[6] = txerr; |
| 298 | cf->data[7] = rxerr; |
| 299 | } |
| 300 | |
| 301 | netif_rx(skb); |
| 302 | |
| 303 | priv->bec.txerr = txerr; |
| 304 | priv->bec.rxerr = rxerr; |
| 305 | |
| 306 | stats->rx_packets++; |
| 307 | stats->rx_bytes += cf->can_dlc; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv, |
| 312 | struct esd_usb2_msg *msg) |
| 313 | { |
| 314 | struct net_device_stats *stats = &priv->netdev->stats; |
| 315 | struct can_frame *cf; |
| 316 | struct sk_buff *skb; |
| 317 | int i; |
| 318 | u32 id; |
| 319 | |
| 320 | if (!netif_device_present(priv->netdev)) |
| 321 | return; |
| 322 | |
| 323 | id = le32_to_cpu(msg->msg.rx.id); |
| 324 | |
| 325 | if (id & ESD_EVENT) { |
| 326 | esd_usb2_rx_event(priv, msg); |
| 327 | } else { |
| 328 | skb = alloc_can_skb(priv->netdev, &cf); |
| 329 | if (skb == NULL) { |
| 330 | stats->rx_dropped++; |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | cf->can_id = id & ESD_IDMASK; |
| 335 | cf->can_dlc = get_can_dlc(msg->msg.rx.dlc); |
| 336 | |
| 337 | if (id & ESD_EXTID) |
| 338 | cf->can_id |= CAN_EFF_FLAG; |
| 339 | |
| 340 | if (msg->msg.rx.dlc & ESD_RTR) { |
| 341 | cf->can_id |= CAN_RTR_FLAG; |
| 342 | } else { |
| 343 | for (i = 0; i < cf->can_dlc; i++) |
| 344 | cf->data[i] = msg->msg.rx.data[i]; |
| 345 | } |
| 346 | |
| 347 | netif_rx(skb); |
| 348 | |
| 349 | stats->rx_packets++; |
| 350 | stats->rx_bytes += cf->can_dlc; |
| 351 | } |
| 352 | |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv, |
| 357 | struct esd_usb2_msg *msg) |
| 358 | { |
| 359 | struct net_device_stats *stats = &priv->netdev->stats; |
| 360 | struct net_device *netdev = priv->netdev; |
| 361 | struct esd_tx_urb_context *context; |
| 362 | |
| 363 | if (!netif_device_present(netdev)) |
| 364 | return; |
| 365 | |
| 366 | context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)]; |
| 367 | |
| 368 | if (!msg->msg.txdone.status) { |
| 369 | stats->tx_packets++; |
| 370 | stats->tx_bytes += context->dlc; |
| 371 | can_get_echo_skb(netdev, context->echo_index); |
| 372 | } else { |
| 373 | stats->tx_errors++; |
| 374 | can_free_echo_skb(netdev, context->echo_index); |
| 375 | } |
| 376 | |
| 377 | /* Release context */ |
| 378 | context->echo_index = MAX_TX_URBS; |
| 379 | atomic_dec(&priv->active_tx_jobs); |
| 380 | |
| 381 | netif_wake_queue(netdev); |
| 382 | } |
| 383 | |
| 384 | static void esd_usb2_read_bulk_callback(struct urb *urb) |
| 385 | { |
| 386 | struct esd_usb2 *dev = urb->context; |
| 387 | int retval; |
| 388 | int pos = 0; |
| 389 | int i; |
| 390 | |
| 391 | switch (urb->status) { |
| 392 | case 0: /* success */ |
| 393 | break; |
| 394 | |
| 395 | case -ENOENT: |
| 396 | case -ESHUTDOWN: |
| 397 | return; |
| 398 | |
| 399 | default: |
| 400 | dev_info(dev->udev->dev.parent, |
| 401 | "Rx URB aborted (%d)\n", urb->status); |
| 402 | goto resubmit_urb; |
| 403 | } |
| 404 | |
| 405 | while (pos < urb->actual_length) { |
| 406 | struct esd_usb2_msg *msg; |
| 407 | |
| 408 | msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos); |
| 409 | |
| 410 | switch (msg->msg.hdr.cmd) { |
| 411 | case CMD_CAN_RX: |
| 412 | esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg); |
| 413 | break; |
| 414 | |
| 415 | case CMD_CAN_TX: |
| 416 | esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net], |
| 417 | msg); |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | pos += msg->msg.hdr.len << 2; |
| 422 | |
| 423 | if (pos > urb->actual_length) { |
| 424 | dev_err(dev->udev->dev.parent, "format error\n"); |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | resubmit_urb: |
| 430 | usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), |
| 431 | urb->transfer_buffer, RX_BUFFER_SIZE, |
| 432 | esd_usb2_read_bulk_callback, dev); |
| 433 | |
| 434 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 435 | if (retval == -ENODEV) { |
| 436 | for (i = 0; i < dev->net_count; i++) { |
| 437 | if (dev->nets[i]) |
| 438 | netif_device_detach(dev->nets[i]->netdev); |
| 439 | } |
| 440 | } else if (retval) { |
| 441 | dev_err(dev->udev->dev.parent, |
| 442 | "failed resubmitting read bulk urb: %d\n", retval); |
| 443 | } |
| 444 | |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * callback for bulk IN urb |
| 450 | */ |
| 451 | static void esd_usb2_write_bulk_callback(struct urb *urb) |
| 452 | { |
| 453 | struct esd_tx_urb_context *context = urb->context; |
| 454 | struct esd_usb2_net_priv *priv; |
| 455 | struct esd_usb2 *dev; |
| 456 | struct net_device *netdev; |
| 457 | size_t size = sizeof(struct esd_usb2_msg); |
| 458 | |
| 459 | WARN_ON(!context); |
| 460 | |
| 461 | priv = context->priv; |
| 462 | netdev = priv->netdev; |
| 463 | dev = priv->usb2; |
| 464 | |
| 465 | /* free up our allocated buffer */ |
| 466 | usb_free_coherent(urb->dev, size, |
| 467 | urb->transfer_buffer, urb->transfer_dma); |
| 468 | |
| 469 | if (!netif_device_present(netdev)) |
| 470 | return; |
| 471 | |
| 472 | if (urb->status) |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 473 | netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 474 | |
| 475 | netdev->trans_start = jiffies; |
| 476 | } |
| 477 | |
| 478 | static ssize_t show_firmware(struct device *d, |
| 479 | struct device_attribute *attr, char *buf) |
| 480 | { |
| 481 | struct usb_interface *intf = to_usb_interface(d); |
| 482 | struct esd_usb2 *dev = usb_get_intfdata(intf); |
| 483 | |
| 484 | return sprintf(buf, "%d.%d.%d\n", |
| 485 | (dev->version >> 12) & 0xf, |
| 486 | (dev->version >> 8) & 0xf, |
| 487 | dev->version & 0xff); |
| 488 | } |
| 489 | static DEVICE_ATTR(firmware, S_IRUGO, show_firmware, NULL); |
| 490 | |
| 491 | static ssize_t show_hardware(struct device *d, |
| 492 | struct device_attribute *attr, char *buf) |
| 493 | { |
| 494 | struct usb_interface *intf = to_usb_interface(d); |
| 495 | struct esd_usb2 *dev = usb_get_intfdata(intf); |
| 496 | |
| 497 | return sprintf(buf, "%d.%d.%d\n", |
| 498 | (dev->version >> 28) & 0xf, |
| 499 | (dev->version >> 24) & 0xf, |
| 500 | (dev->version >> 16) & 0xff); |
| 501 | } |
| 502 | static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL); |
| 503 | |
| 504 | static ssize_t show_nets(struct device *d, |
| 505 | struct device_attribute *attr, char *buf) |
| 506 | { |
| 507 | struct usb_interface *intf = to_usb_interface(d); |
| 508 | struct esd_usb2 *dev = usb_get_intfdata(intf); |
| 509 | |
| 510 | return sprintf(buf, "%d", dev->net_count); |
| 511 | } |
| 512 | static DEVICE_ATTR(nets, S_IRUGO, show_nets, NULL); |
| 513 | |
| 514 | static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg) |
| 515 | { |
| 516 | int actual_length; |
| 517 | |
| 518 | return usb_bulk_msg(dev->udev, |
| 519 | usb_sndbulkpipe(dev->udev, 2), |
| 520 | msg, |
| 521 | msg->msg.hdr.len << 2, |
| 522 | &actual_length, |
| 523 | 1000); |
| 524 | } |
| 525 | |
| 526 | static int esd_usb2_wait_msg(struct esd_usb2 *dev, |
| 527 | struct esd_usb2_msg *msg) |
| 528 | { |
| 529 | int actual_length; |
| 530 | |
| 531 | return usb_bulk_msg(dev->udev, |
| 532 | usb_rcvbulkpipe(dev->udev, 1), |
| 533 | msg, |
| 534 | sizeof(*msg), |
| 535 | &actual_length, |
| 536 | 1000); |
| 537 | } |
| 538 | |
| 539 | static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) |
| 540 | { |
| 541 | int i, err = 0; |
| 542 | |
| 543 | if (dev->rxinitdone) |
| 544 | return 0; |
| 545 | |
| 546 | for (i = 0; i < MAX_RX_URBS; i++) { |
| 547 | struct urb *urb = NULL; |
| 548 | u8 *buf = NULL; |
| 549 | |
| 550 | /* create a URB, and a buffer for it */ |
| 551 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 552 | if (!urb) { |
| 553 | dev_warn(dev->udev->dev.parent, |
| 554 | "No memory left for URBs\n"); |
| 555 | err = -ENOMEM; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL, |
| 560 | &urb->transfer_dma); |
| 561 | if (!buf) { |
| 562 | dev_warn(dev->udev->dev.parent, |
| 563 | "No memory left for USB buffer\n"); |
| 564 | err = -ENOMEM; |
| 565 | goto freeurb; |
| 566 | } |
| 567 | |
| 568 | usb_fill_bulk_urb(urb, dev->udev, |
| 569 | usb_rcvbulkpipe(dev->udev, 1), |
| 570 | buf, RX_BUFFER_SIZE, |
| 571 | esd_usb2_read_bulk_callback, dev); |
| 572 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 573 | usb_anchor_urb(urb, &dev->rx_submitted); |
| 574 | |
| 575 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 576 | if (err) { |
| 577 | usb_unanchor_urb(urb); |
| 578 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, |
| 579 | urb->transfer_dma); |
| 580 | } |
| 581 | |
| 582 | freeurb: |
| 583 | /* Drop reference, USB core will take care of freeing it */ |
| 584 | usb_free_urb(urb); |
| 585 | if (err) |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | /* Did we submit any URBs */ |
| 590 | if (i == 0) { |
| 591 | dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n"); |
| 592 | return err; |
| 593 | } |
| 594 | |
| 595 | /* Warn if we've couldn't transmit all the URBs */ |
| 596 | if (i < MAX_RX_URBS) { |
| 597 | dev_warn(dev->udev->dev.parent, |
| 598 | "rx performance may be slow\n"); |
| 599 | } |
| 600 | |
| 601 | dev->rxinitdone = 1; |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Start interface |
| 607 | */ |
| 608 | static int esd_usb2_start(struct esd_usb2_net_priv *priv) |
| 609 | { |
| 610 | struct esd_usb2 *dev = priv->usb2; |
| 611 | struct net_device *netdev = priv->netdev; |
| 612 | struct esd_usb2_msg msg; |
| 613 | int err, i; |
| 614 | |
| 615 | /* |
| 616 | * Enable all IDs |
| 617 | * The IDADD message takes up to 64 32 bit bitmasks (2048 bits). |
| 618 | * Each bit represents one 11 bit CAN identifier. A set bit |
| 619 | * enables reception of the corresponding CAN identifier. A cleared |
| 620 | * bit disabled this identifier. An additional bitmask value |
| 621 | * following the CAN 2.0A bits is used to enable reception of |
| 622 | * extended CAN frames. Only the LSB of this final mask is checked |
| 623 | * for the complete 29 bit ID range. The IDADD message also allows |
| 624 | * filter configuration for an ID subset. In this case you can add |
| 625 | * the number of the starting bitmask (0..64) to the filter.option |
| 626 | * field followed by only some bitmasks. |
| 627 | */ |
| 628 | msg.msg.hdr.cmd = CMD_IDADD; |
| 629 | msg.msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT; |
| 630 | msg.msg.filter.net = priv->index; |
| 631 | msg.msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */ |
| 632 | for (i = 0; i < ESD_MAX_ID_SEGMENT; i++) |
| 633 | msg.msg.filter.mask[i] = cpu_to_le32(0xffffffff); |
| 634 | /* enable 29bit extended IDs */ |
| 635 | msg.msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001); |
| 636 | |
| 637 | err = esd_usb2_send_msg(dev, &msg); |
| 638 | if (err) |
| 639 | goto failed; |
| 640 | |
| 641 | err = esd_usb2_setup_rx_urbs(dev); |
| 642 | if (err) |
| 643 | goto failed; |
| 644 | |
| 645 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 646 | |
| 647 | return 0; |
| 648 | |
| 649 | failed: |
| 650 | if (err == -ENODEV) |
| 651 | netif_device_detach(netdev); |
| 652 | |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 653 | netdev_err(netdev, "couldn't start device: %d\n", err); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 654 | |
| 655 | return err; |
| 656 | } |
| 657 | |
| 658 | static void unlink_all_urbs(struct esd_usb2 *dev) |
| 659 | { |
| 660 | struct esd_usb2_net_priv *priv; |
roel kluin | 0b32211 | 2011-03-08 09:52:55 +0000 | [diff] [blame] | 661 | int i, j; |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 662 | |
| 663 | usb_kill_anchored_urbs(&dev->rx_submitted); |
| 664 | for (i = 0; i < dev->net_count; i++) { |
| 665 | priv = dev->nets[i]; |
| 666 | if (priv) { |
| 667 | usb_kill_anchored_urbs(&priv->tx_submitted); |
| 668 | atomic_set(&priv->active_tx_jobs, 0); |
| 669 | |
roel kluin | 0b32211 | 2011-03-08 09:52:55 +0000 | [diff] [blame] | 670 | for (j = 0; j < MAX_TX_URBS; j++) |
| 671 | priv->tx_contexts[j].echo_index = MAX_TX_URBS; |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static int esd_usb2_open(struct net_device *netdev) |
| 677 | { |
| 678 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); |
| 679 | int err; |
| 680 | |
| 681 | /* common open */ |
| 682 | err = open_candev(netdev); |
| 683 | if (err) |
| 684 | return err; |
| 685 | |
| 686 | /* finally start device */ |
| 687 | err = esd_usb2_start(priv); |
| 688 | if (err) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 689 | netdev_warn(netdev, "couldn't start device: %d\n", err); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 690 | close_candev(netdev); |
| 691 | return err; |
| 692 | } |
| 693 | |
| 694 | priv->open_time = jiffies; |
| 695 | |
| 696 | netif_start_queue(netdev); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb, |
| 702 | struct net_device *netdev) |
| 703 | { |
| 704 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); |
| 705 | struct esd_usb2 *dev = priv->usb2; |
| 706 | struct esd_tx_urb_context *context = NULL; |
| 707 | struct net_device_stats *stats = &netdev->stats; |
| 708 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 709 | struct esd_usb2_msg *msg; |
| 710 | struct urb *urb; |
| 711 | u8 *buf; |
| 712 | int i, err; |
| 713 | int ret = NETDEV_TX_OK; |
| 714 | size_t size = sizeof(struct esd_usb2_msg); |
| 715 | |
| 716 | if (can_dropped_invalid_skb(netdev, skb)) |
| 717 | return NETDEV_TX_OK; |
| 718 | |
| 719 | /* create a URB, and a buffer for it, and copy the data to the URB */ |
| 720 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 721 | if (!urb) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 722 | netdev_err(netdev, "No memory left for URBs\n"); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 723 | stats->tx_dropped++; |
| 724 | dev_kfree_skb(skb); |
| 725 | goto nourbmem; |
| 726 | } |
| 727 | |
| 728 | buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, |
| 729 | &urb->transfer_dma); |
| 730 | if (!buf) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 731 | netdev_err(netdev, "No memory left for USB buffer\n"); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 732 | stats->tx_dropped++; |
| 733 | dev_kfree_skb(skb); |
| 734 | goto nobufmem; |
| 735 | } |
| 736 | |
| 737 | msg = (struct esd_usb2_msg *)buf; |
| 738 | |
| 739 | msg->msg.hdr.len = 3; /* minimal length */ |
| 740 | msg->msg.hdr.cmd = CMD_CAN_TX; |
| 741 | msg->msg.tx.net = priv->index; |
| 742 | msg->msg.tx.dlc = cf->can_dlc; |
| 743 | msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK); |
| 744 | |
| 745 | if (cf->can_id & CAN_RTR_FLAG) |
| 746 | msg->msg.tx.dlc |= ESD_RTR; |
| 747 | |
| 748 | if (cf->can_id & CAN_EFF_FLAG) |
| 749 | msg->msg.tx.id |= cpu_to_le32(ESD_EXTID); |
| 750 | |
| 751 | for (i = 0; i < cf->can_dlc; i++) |
| 752 | msg->msg.tx.data[i] = cf->data[i]; |
| 753 | |
| 754 | msg->msg.hdr.len += (cf->can_dlc + 3) >> 2; |
| 755 | |
| 756 | for (i = 0; i < MAX_TX_URBS; i++) { |
| 757 | if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { |
| 758 | context = &priv->tx_contexts[i]; |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * This may never happen. |
| 765 | */ |
| 766 | if (!context) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 767 | netdev_warn(netdev, "couldn't find free context\n"); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 768 | ret = NETDEV_TX_BUSY; |
| 769 | goto releasebuf; |
| 770 | } |
| 771 | |
| 772 | context->priv = priv; |
| 773 | context->echo_index = i; |
| 774 | context->dlc = cf->can_dlc; |
| 775 | |
| 776 | /* hnd must not be 0 - MSB is stripped in txdone handling */ |
| 777 | msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */ |
| 778 | |
| 779 | usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf, |
| 780 | msg->msg.hdr.len << 2, |
| 781 | esd_usb2_write_bulk_callback, context); |
| 782 | |
| 783 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 784 | |
| 785 | usb_anchor_urb(urb, &priv->tx_submitted); |
| 786 | |
| 787 | can_put_echo_skb(skb, netdev, context->echo_index); |
| 788 | |
| 789 | atomic_inc(&priv->active_tx_jobs); |
| 790 | |
| 791 | /* Slow down tx path */ |
| 792 | if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS) |
| 793 | netif_stop_queue(netdev); |
| 794 | |
| 795 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 796 | if (err) { |
| 797 | can_free_echo_skb(netdev, context->echo_index); |
| 798 | |
| 799 | atomic_dec(&priv->active_tx_jobs); |
| 800 | usb_unanchor_urb(urb); |
| 801 | |
| 802 | stats->tx_dropped++; |
| 803 | |
| 804 | if (err == -ENODEV) |
| 805 | netif_device_detach(netdev); |
| 806 | else |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 807 | netdev_warn(netdev, "failed tx_urb %d\n", err); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 808 | |
| 809 | goto releasebuf; |
| 810 | } |
| 811 | |
| 812 | netdev->trans_start = jiffies; |
| 813 | |
| 814 | /* |
| 815 | * Release our reference to this URB, the USB core will eventually free |
| 816 | * it entirely. |
| 817 | */ |
| 818 | usb_free_urb(urb); |
| 819 | |
| 820 | return NETDEV_TX_OK; |
| 821 | |
| 822 | releasebuf: |
| 823 | usb_free_coherent(dev->udev, size, buf, urb->transfer_dma); |
| 824 | |
| 825 | nobufmem: |
| 826 | usb_free_urb(urb); |
| 827 | |
| 828 | nourbmem: |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | static int esd_usb2_close(struct net_device *netdev) |
| 833 | { |
| 834 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); |
| 835 | struct esd_usb2_msg msg; |
| 836 | int i; |
| 837 | |
| 838 | /* Disable all IDs (see esd_usb2_start()) */ |
| 839 | msg.msg.hdr.cmd = CMD_IDADD; |
| 840 | msg.msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT; |
| 841 | msg.msg.filter.net = priv->index; |
| 842 | msg.msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */ |
| 843 | for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++) |
| 844 | msg.msg.filter.mask[i] = 0; |
| 845 | if (esd_usb2_send_msg(priv->usb2, &msg) < 0) |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 846 | netdev_err(netdev, "sending idadd message failed\n"); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 847 | |
| 848 | /* set CAN controller to reset mode */ |
| 849 | msg.msg.hdr.len = 2; |
| 850 | msg.msg.hdr.cmd = CMD_SETBAUD; |
| 851 | msg.msg.setbaud.net = priv->index; |
| 852 | msg.msg.setbaud.rsvd = 0; |
| 853 | msg.msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE); |
| 854 | if (esd_usb2_send_msg(priv->usb2, &msg) < 0) |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 855 | netdev_err(netdev, "sending setbaud message failed\n"); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 856 | |
| 857 | priv->can.state = CAN_STATE_STOPPED; |
| 858 | |
| 859 | netif_stop_queue(netdev); |
| 860 | |
| 861 | close_candev(netdev); |
| 862 | |
| 863 | priv->open_time = 0; |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static const struct net_device_ops esd_usb2_netdev_ops = { |
| 869 | .ndo_open = esd_usb2_open, |
| 870 | .ndo_stop = esd_usb2_close, |
| 871 | .ndo_start_xmit = esd_usb2_start_xmit, |
| 872 | }; |
| 873 | |
Marc Kleine-Budde | 194b9a4 | 2012-07-16 12:58:31 +0200 | [diff] [blame] | 874 | static const struct can_bittiming_const esd_usb2_bittiming_const = { |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 875 | .name = "esd_usb2", |
| 876 | .tseg1_min = ESD_USB2_TSEG1_MIN, |
| 877 | .tseg1_max = ESD_USB2_TSEG1_MAX, |
| 878 | .tseg2_min = ESD_USB2_TSEG2_MIN, |
| 879 | .tseg2_max = ESD_USB2_TSEG2_MAX, |
| 880 | .sjw_max = ESD_USB2_SJW_MAX, |
| 881 | .brp_min = ESD_USB2_BRP_MIN, |
| 882 | .brp_max = ESD_USB2_BRP_MAX, |
| 883 | .brp_inc = ESD_USB2_BRP_INC, |
| 884 | }; |
| 885 | |
| 886 | static int esd_usb2_set_bittiming(struct net_device *netdev) |
| 887 | { |
| 888 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); |
| 889 | struct can_bittiming *bt = &priv->can.bittiming; |
| 890 | struct esd_usb2_msg msg; |
| 891 | u32 canbtr; |
| 892 | |
| 893 | canbtr = ESD_USB2_UBR; |
Matthias Fuchs | a5f8f0e | 2012-10-29 10:54:47 +0100 | [diff] [blame^] | 894 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
| 895 | canbtr |= ESD_USB2_LOM; |
| 896 | |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 897 | canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1); |
| 898 | canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1)) |
| 899 | << ESD_USB2_SJW_SHIFT; |
| 900 | canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1) |
| 901 | & (ESD_USB2_TSEG1_MAX - 1)) |
| 902 | << ESD_USB2_TSEG1_SHIFT; |
| 903 | canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1)) |
| 904 | << ESD_USB2_TSEG2_SHIFT; |
| 905 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 906 | canbtr |= ESD_USB2_3_SAMPLES; |
| 907 | |
| 908 | msg.msg.hdr.len = 2; |
| 909 | msg.msg.hdr.cmd = CMD_SETBAUD; |
| 910 | msg.msg.setbaud.net = priv->index; |
| 911 | msg.msg.setbaud.rsvd = 0; |
| 912 | msg.msg.setbaud.baud = cpu_to_le32(canbtr); |
| 913 | |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 914 | netdev_info(netdev, "setting BTR=%#x\n", canbtr); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 915 | |
| 916 | return esd_usb2_send_msg(priv->usb2, &msg); |
| 917 | } |
| 918 | |
| 919 | static int esd_usb2_get_berr_counter(const struct net_device *netdev, |
| 920 | struct can_berr_counter *bec) |
| 921 | { |
| 922 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); |
| 923 | |
| 924 | bec->txerr = priv->bec.txerr; |
| 925 | bec->rxerr = priv->bec.rxerr; |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode) |
| 931 | { |
| 932 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); |
| 933 | |
| 934 | if (!priv->open_time) |
| 935 | return -EINVAL; |
| 936 | |
| 937 | switch (mode) { |
| 938 | case CAN_MODE_START: |
| 939 | netif_wake_queue(netdev); |
| 940 | break; |
| 941 | |
| 942 | default: |
| 943 | return -EOPNOTSUPP; |
| 944 | } |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | static int esd_usb2_probe_one_net(struct usb_interface *intf, int index) |
| 950 | { |
| 951 | struct esd_usb2 *dev = usb_get_intfdata(intf); |
| 952 | struct net_device *netdev; |
| 953 | struct esd_usb2_net_priv *priv; |
| 954 | int err = 0; |
| 955 | int i; |
| 956 | |
| 957 | netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS); |
| 958 | if (!netdev) { |
| 959 | dev_err(&intf->dev, "couldn't alloc candev\n"); |
| 960 | err = -ENOMEM; |
| 961 | goto done; |
| 962 | } |
| 963 | |
| 964 | priv = netdev_priv(netdev); |
| 965 | |
| 966 | init_usb_anchor(&priv->tx_submitted); |
| 967 | atomic_set(&priv->active_tx_jobs, 0); |
| 968 | |
| 969 | for (i = 0; i < MAX_TX_URBS; i++) |
| 970 | priv->tx_contexts[i].echo_index = MAX_TX_URBS; |
| 971 | |
| 972 | priv->usb2 = dev; |
| 973 | priv->netdev = netdev; |
| 974 | priv->index = index; |
| 975 | |
| 976 | priv->can.state = CAN_STATE_STOPPED; |
Matthias Fuchs | a5f8f0e | 2012-10-29 10:54:47 +0100 | [diff] [blame^] | 977 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY; |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 978 | priv->can.clock.freq = ESD_USB2_CAN_CLOCK; |
| 979 | priv->can.bittiming_const = &esd_usb2_bittiming_const; |
| 980 | priv->can.do_set_bittiming = esd_usb2_set_bittiming; |
| 981 | priv->can.do_set_mode = esd_usb2_set_mode; |
| 982 | priv->can.do_get_berr_counter = esd_usb2_get_berr_counter; |
| 983 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; |
| 984 | |
| 985 | netdev->flags |= IFF_ECHO; /* we support local echo */ |
| 986 | |
| 987 | netdev->netdev_ops = &esd_usb2_netdev_ops; |
| 988 | |
| 989 | SET_NETDEV_DEV(netdev, &intf->dev); |
| 990 | |
| 991 | err = register_candev(netdev); |
| 992 | if (err) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 993 | dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 994 | free_candev(netdev); |
| 995 | err = -ENOMEM; |
| 996 | goto done; |
| 997 | } |
| 998 | |
| 999 | dev->nets[index] = priv; |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1000 | netdev_info(netdev, "device %s registered\n", netdev->name); |
Matthias Fuchs | 96d8e90 | 2010-08-03 02:55:23 +0000 | [diff] [blame] | 1001 | |
| 1002 | done: |
| 1003 | return err; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * probe function for new USB2 devices |
| 1008 | * |
| 1009 | * check version information and number of available |
| 1010 | * CAN interfaces |
| 1011 | */ |
| 1012 | static int esd_usb2_probe(struct usb_interface *intf, |
| 1013 | const struct usb_device_id *id) |
| 1014 | { |
| 1015 | struct esd_usb2 *dev; |
| 1016 | struct esd_usb2_msg msg; |
| 1017 | int i, err; |
| 1018 | |
| 1019 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1020 | if (!dev) { |
| 1021 | err = -ENOMEM; |
| 1022 | goto done; |
| 1023 | } |
| 1024 | |
| 1025 | dev->udev = interface_to_usbdev(intf); |
| 1026 | |
| 1027 | init_usb_anchor(&dev->rx_submitted); |
| 1028 | |
| 1029 | usb_set_intfdata(intf, dev); |
| 1030 | |
| 1031 | /* query number of CAN interfaces (nets) */ |
| 1032 | msg.msg.hdr.cmd = CMD_VERSION; |
| 1033 | msg.msg.hdr.len = 2; |
| 1034 | msg.msg.version.rsvd = 0; |
| 1035 | msg.msg.version.flags = 0; |
| 1036 | msg.msg.version.drv_version = 0; |
| 1037 | |
| 1038 | err = esd_usb2_send_msg(dev, &msg); |
| 1039 | if (err < 0) { |
| 1040 | dev_err(&intf->dev, "sending version message failed\n"); |
| 1041 | goto free_dev; |
| 1042 | } |
| 1043 | |
| 1044 | err = esd_usb2_wait_msg(dev, &msg); |
| 1045 | if (err < 0) { |
| 1046 | dev_err(&intf->dev, "no version message answer\n"); |
| 1047 | goto free_dev; |
| 1048 | } |
| 1049 | |
| 1050 | dev->net_count = (int)msg.msg.version_reply.nets; |
| 1051 | dev->version = le32_to_cpu(msg.msg.version_reply.version); |
| 1052 | |
| 1053 | if (device_create_file(&intf->dev, &dev_attr_firmware)) |
| 1054 | dev_err(&intf->dev, |
| 1055 | "Couldn't create device file for firmware\n"); |
| 1056 | |
| 1057 | if (device_create_file(&intf->dev, &dev_attr_hardware)) |
| 1058 | dev_err(&intf->dev, |
| 1059 | "Couldn't create device file for hardware\n"); |
| 1060 | |
| 1061 | if (device_create_file(&intf->dev, &dev_attr_nets)) |
| 1062 | dev_err(&intf->dev, |
| 1063 | "Couldn't create device file for nets\n"); |
| 1064 | |
| 1065 | /* do per device probing */ |
| 1066 | for (i = 0; i < dev->net_count; i++) |
| 1067 | esd_usb2_probe_one_net(intf, i); |
| 1068 | |
| 1069 | return 0; |
| 1070 | |
| 1071 | free_dev: |
| 1072 | kfree(dev); |
| 1073 | done: |
| 1074 | return err; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * called by the usb core when the device is removed from the system |
| 1079 | */ |
| 1080 | static void esd_usb2_disconnect(struct usb_interface *intf) |
| 1081 | { |
| 1082 | struct esd_usb2 *dev = usb_get_intfdata(intf); |
| 1083 | struct net_device *netdev; |
| 1084 | int i; |
| 1085 | |
| 1086 | device_remove_file(&intf->dev, &dev_attr_firmware); |
| 1087 | device_remove_file(&intf->dev, &dev_attr_hardware); |
| 1088 | device_remove_file(&intf->dev, &dev_attr_nets); |
| 1089 | |
| 1090 | usb_set_intfdata(intf, NULL); |
| 1091 | |
| 1092 | if (dev) { |
| 1093 | for (i = 0; i < dev->net_count; i++) { |
| 1094 | if (dev->nets[i]) { |
| 1095 | netdev = dev->nets[i]->netdev; |
| 1096 | unregister_netdev(netdev); |
| 1097 | free_candev(netdev); |
| 1098 | } |
| 1099 | } |
| 1100 | unlink_all_urbs(dev); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 1105 | static struct usb_driver esd_usb2_driver = { |
| 1106 | .name = "esd_usb2", |
| 1107 | .probe = esd_usb2_probe, |
| 1108 | .disconnect = esd_usb2_disconnect, |
| 1109 | .id_table = esd_usb2_table, |
| 1110 | }; |
| 1111 | |
Greg Kroah-Hartman | d632eb1 | 2011-11-18 09:44:20 -0800 | [diff] [blame] | 1112 | module_usb_driver(esd_usb2_driver); |