1f155d9c0SMaximilian Schneider /* CAN driver for Geschwister Schneider USB/CAN devices 2f155d9c0SMaximilian Schneider * and bytewerk.org candleLight USB CAN interfaces. 3d08e973aSMaximilian Schneider * 4f155d9c0SMaximilian Schneider * Copyright (C) 2013-2016 Geschwister Schneider Technologie-, 5d08e973aSMaximilian Schneider * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt). 6f155d9c0SMaximilian Schneider * Copyright (C) 2016 Hubert Denkmair 7d08e973aSMaximilian Schneider * 8d08e973aSMaximilian Schneider * Many thanks to all socketcan devs! 9d08e973aSMaximilian Schneider * 10d08e973aSMaximilian Schneider * This program is free software; you can redistribute it and/or modify it 11d08e973aSMaximilian Schneider * under the terms of the GNU General Public License as published 12d08e973aSMaximilian Schneider * by the Free Software Foundation; version 2 of the License. 13d08e973aSMaximilian Schneider * 14d08e973aSMaximilian Schneider * This program is distributed in the hope that it will be useful, but 15d08e973aSMaximilian Schneider * WITHOUT ANY WARRANTY; without even the implied warranty of 16d08e973aSMaximilian Schneider * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17d08e973aSMaximilian Schneider * General Public License for more details. 18d08e973aSMaximilian Schneider */ 19d08e973aSMaximilian Schneider 20d08e973aSMaximilian Schneider #include <linux/init.h> 21d08e973aSMaximilian Schneider #include <linux/signal.h> 22d08e973aSMaximilian Schneider #include <linux/module.h> 23d08e973aSMaximilian Schneider #include <linux/netdevice.h> 24d08e973aSMaximilian Schneider #include <linux/usb.h> 25d08e973aSMaximilian Schneider 26d08e973aSMaximilian Schneider #include <linux/can.h> 27d08e973aSMaximilian Schneider #include <linux/can/dev.h> 28d08e973aSMaximilian Schneider #include <linux/can/error.h> 29d08e973aSMaximilian Schneider 30d08e973aSMaximilian Schneider /* Device specific constants */ 31d08e973aSMaximilian Schneider #define USB_GSUSB_1_VENDOR_ID 0x1d50 32d08e973aSMaximilian Schneider #define USB_GSUSB_1_PRODUCT_ID 0x606f 33d08e973aSMaximilian Schneider 34f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_VENDOR_ID 0x1209 35f155d9c0SMaximilian Schneider #define USB_CANDLELIGHT_PRODUCT_ID 0x2323 36f155d9c0SMaximilian Schneider 37d08e973aSMaximilian Schneider #define GSUSB_ENDPOINT_IN 1 38d08e973aSMaximilian Schneider #define GSUSB_ENDPOINT_OUT 2 39d08e973aSMaximilian Schneider 40d08e973aSMaximilian Schneider /* Device specific constants */ 41d08e973aSMaximilian Schneider enum gs_usb_breq { 42d08e973aSMaximilian Schneider GS_USB_BREQ_HOST_FORMAT = 0, 43d08e973aSMaximilian Schneider GS_USB_BREQ_BITTIMING, 44d08e973aSMaximilian Schneider GS_USB_BREQ_MODE, 45d08e973aSMaximilian Schneider GS_USB_BREQ_BERR, 46d08e973aSMaximilian Schneider GS_USB_BREQ_BT_CONST, 4705ca5270SMaximilian Schneider GS_USB_BREQ_DEVICE_CONFIG, 4805ca5270SMaximilian Schneider GS_USB_BREQ_TIMESTAMP, 4905ca5270SMaximilian Schneider GS_USB_BREQ_IDENTIFY, 50d08e973aSMaximilian Schneider }; 51d08e973aSMaximilian Schneider 52d08e973aSMaximilian Schneider enum gs_can_mode { 53d08e973aSMaximilian Schneider /* reset a channel. turns it off */ 54d08e973aSMaximilian Schneider GS_CAN_MODE_RESET = 0, 55d08e973aSMaximilian Schneider /* starts a channel */ 56d08e973aSMaximilian Schneider GS_CAN_MODE_START 57d08e973aSMaximilian Schneider }; 58d08e973aSMaximilian Schneider 59d08e973aSMaximilian Schneider enum gs_can_state { 60d08e973aSMaximilian Schneider GS_CAN_STATE_ERROR_ACTIVE = 0, 61d08e973aSMaximilian Schneider GS_CAN_STATE_ERROR_WARNING, 62d08e973aSMaximilian Schneider GS_CAN_STATE_ERROR_PASSIVE, 63d08e973aSMaximilian Schneider GS_CAN_STATE_BUS_OFF, 64d08e973aSMaximilian Schneider GS_CAN_STATE_STOPPED, 65d08e973aSMaximilian Schneider GS_CAN_STATE_SLEEPING 66d08e973aSMaximilian Schneider }; 67d08e973aSMaximilian Schneider 6805ca5270SMaximilian Schneider enum gs_can_identify_mode { 6905ca5270SMaximilian Schneider GS_CAN_IDENTIFY_OFF = 0, 7005ca5270SMaximilian Schneider GS_CAN_IDENTIFY_ON 7105ca5270SMaximilian Schneider }; 7205ca5270SMaximilian Schneider 73d08e973aSMaximilian Schneider /* data types passed between host and device */ 74d08e973aSMaximilian Schneider struct gs_host_config { 75d08e973aSMaximilian Schneider u32 byte_order; 76d08e973aSMaximilian Schneider } __packed; 77d08e973aSMaximilian Schneider /* All data exchanged between host and device is exchanged in host byte order, 78d08e973aSMaximilian Schneider * thanks to the struct gs_host_config byte_order member, which is sent first 79d08e973aSMaximilian Schneider * to indicate the desired byte order. 80d08e973aSMaximilian Schneider */ 81d08e973aSMaximilian Schneider 82d08e973aSMaximilian Schneider struct gs_device_config { 83d08e973aSMaximilian Schneider u8 reserved1; 84d08e973aSMaximilian Schneider u8 reserved2; 85d08e973aSMaximilian Schneider u8 reserved3; 86d08e973aSMaximilian Schneider u8 icount; 87d08e973aSMaximilian Schneider u32 sw_version; 88d08e973aSMaximilian Schneider u32 hw_version; 89d08e973aSMaximilian Schneider } __packed; 90d08e973aSMaximilian Schneider 91d08e973aSMaximilian Schneider #define GS_CAN_MODE_NORMAL 0 929be95c91SMaximilian Schneider #define GS_CAN_MODE_LISTEN_ONLY BIT(0) 939be95c91SMaximilian Schneider #define GS_CAN_MODE_LOOP_BACK BIT(1) 949be95c91SMaximilian Schneider #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2) 959be95c91SMaximilian Schneider #define GS_CAN_MODE_ONE_SHOT BIT(3) 96d08e973aSMaximilian Schneider 97d08e973aSMaximilian Schneider struct gs_device_mode { 98d08e973aSMaximilian Schneider u32 mode; 99d08e973aSMaximilian Schneider u32 flags; 100d08e973aSMaximilian Schneider } __packed; 101d08e973aSMaximilian Schneider 102d08e973aSMaximilian Schneider struct gs_device_state { 103d08e973aSMaximilian Schneider u32 state; 104d08e973aSMaximilian Schneider u32 rxerr; 105d08e973aSMaximilian Schneider u32 txerr; 106d08e973aSMaximilian Schneider } __packed; 107d08e973aSMaximilian Schneider 108d08e973aSMaximilian Schneider struct gs_device_bittiming { 109d08e973aSMaximilian Schneider u32 prop_seg; 110d08e973aSMaximilian Schneider u32 phase_seg1; 111d08e973aSMaximilian Schneider u32 phase_seg2; 112d08e973aSMaximilian Schneider u32 sjw; 113d08e973aSMaximilian Schneider u32 brp; 114d08e973aSMaximilian Schneider } __packed; 115d08e973aSMaximilian Schneider 11605ca5270SMaximilian Schneider struct gs_identify_mode { 11705ca5270SMaximilian Schneider u32 mode; 11805ca5270SMaximilian Schneider } __packed; 11905ca5270SMaximilian Schneider 1209be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0) 1219be95c91SMaximilian Schneider #define GS_CAN_FEATURE_LOOP_BACK BIT(1) 1229be95c91SMaximilian Schneider #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2) 1239be95c91SMaximilian Schneider #define GS_CAN_FEATURE_ONE_SHOT BIT(3) 12405ca5270SMaximilian Schneider #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4) 12505ca5270SMaximilian Schneider #define GS_CAN_FEATURE_IDENTIFY BIT(5) 126d08e973aSMaximilian Schneider 127d08e973aSMaximilian Schneider struct gs_device_bt_const { 128d08e973aSMaximilian Schneider u32 feature; 129d08e973aSMaximilian Schneider u32 fclk_can; 130d08e973aSMaximilian Schneider u32 tseg1_min; 131d08e973aSMaximilian Schneider u32 tseg1_max; 132d08e973aSMaximilian Schneider u32 tseg2_min; 133d08e973aSMaximilian Schneider u32 tseg2_max; 134d08e973aSMaximilian Schneider u32 sjw_max; 135d08e973aSMaximilian Schneider u32 brp_min; 136d08e973aSMaximilian Schneider u32 brp_max; 137d08e973aSMaximilian Schneider u32 brp_inc; 138d08e973aSMaximilian Schneider } __packed; 139d08e973aSMaximilian Schneider 140d08e973aSMaximilian Schneider #define GS_CAN_FLAG_OVERFLOW 1 141d08e973aSMaximilian Schneider 142d08e973aSMaximilian Schneider struct gs_host_frame { 143d08e973aSMaximilian Schneider u32 echo_id; 144d08e973aSMaximilian Schneider u32 can_id; 145d08e973aSMaximilian Schneider 146d08e973aSMaximilian Schneider u8 can_dlc; 147d08e973aSMaximilian Schneider u8 channel; 148d08e973aSMaximilian Schneider u8 flags; 149d08e973aSMaximilian Schneider u8 reserved; 150d08e973aSMaximilian Schneider 151d08e973aSMaximilian Schneider u8 data[8]; 152d08e973aSMaximilian Schneider } __packed; 153d08e973aSMaximilian Schneider /* The GS USB devices make use of the same flags and masks as in 154d08e973aSMaximilian Schneider * linux/can.h and linux/can/error.h, and no additional mapping is necessary. 155d08e973aSMaximilian Schneider */ 156d08e973aSMaximilian Schneider 157d08e973aSMaximilian Schneider /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */ 158d08e973aSMaximilian Schneider #define GS_MAX_TX_URBS 10 159d08e973aSMaximilian Schneider /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */ 160d08e973aSMaximilian Schneider #define GS_MAX_RX_URBS 30 161d08e973aSMaximilian Schneider /* Maximum number of interfaces the driver supports per device. 162d08e973aSMaximilian Schneider * Current hardware only supports 2 interfaces. The future may vary. 163d08e973aSMaximilian Schneider */ 164d08e973aSMaximilian Schneider #define GS_MAX_INTF 2 165d08e973aSMaximilian Schneider 166d08e973aSMaximilian Schneider struct gs_tx_context { 167d08e973aSMaximilian Schneider struct gs_can *dev; 168d08e973aSMaximilian Schneider unsigned int echo_id; 169d08e973aSMaximilian Schneider }; 170d08e973aSMaximilian Schneider 171d08e973aSMaximilian Schneider struct gs_can { 172d08e973aSMaximilian Schneider struct can_priv can; /* must be the first member */ 173d08e973aSMaximilian Schneider 174d08e973aSMaximilian Schneider struct gs_usb *parent; 175d08e973aSMaximilian Schneider 176d08e973aSMaximilian Schneider struct net_device *netdev; 177d08e973aSMaximilian Schneider struct usb_device *udev; 178d08e973aSMaximilian Schneider struct usb_interface *iface; 179d08e973aSMaximilian Schneider 180d08e973aSMaximilian Schneider struct can_bittiming_const bt_const; 181d08e973aSMaximilian Schneider unsigned int channel; /* channel number */ 182d08e973aSMaximilian Schneider 183ae421e33SNik Nyby /* This lock prevents a race condition between xmit and receive. */ 184d08e973aSMaximilian Schneider spinlock_t tx_ctx_lock; 185d08e973aSMaximilian Schneider struct gs_tx_context tx_context[GS_MAX_TX_URBS]; 186d08e973aSMaximilian Schneider 187d08e973aSMaximilian Schneider struct usb_anchor tx_submitted; 188d08e973aSMaximilian Schneider atomic_t active_tx_urbs; 189d08e973aSMaximilian Schneider }; 190d08e973aSMaximilian Schneider 191d08e973aSMaximilian Schneider /* usb interface struct */ 192d08e973aSMaximilian Schneider struct gs_usb { 193d08e973aSMaximilian Schneider struct gs_can *canch[GS_MAX_INTF]; 194d08e973aSMaximilian Schneider struct usb_anchor rx_submitted; 195d08e973aSMaximilian Schneider atomic_t active_channels; 196d08e973aSMaximilian Schneider struct usb_device *udev; 197d08e973aSMaximilian Schneider }; 198d08e973aSMaximilian Schneider 199d08e973aSMaximilian Schneider /* 'allocate' a tx context. 200d08e973aSMaximilian Schneider * returns a valid tx context or NULL if there is no space. 201d08e973aSMaximilian Schneider */ 202d08e973aSMaximilian Schneider static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev) 203d08e973aSMaximilian Schneider { 204d08e973aSMaximilian Schneider int i = 0; 205d08e973aSMaximilian Schneider unsigned long flags; 206d08e973aSMaximilian Schneider 207d08e973aSMaximilian Schneider spin_lock_irqsave(&dev->tx_ctx_lock, flags); 208d08e973aSMaximilian Schneider 209d08e973aSMaximilian Schneider for (; i < GS_MAX_TX_URBS; i++) { 210d08e973aSMaximilian Schneider if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) { 211d08e973aSMaximilian Schneider dev->tx_context[i].echo_id = i; 212d08e973aSMaximilian Schneider spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 213d08e973aSMaximilian Schneider return &dev->tx_context[i]; 214d08e973aSMaximilian Schneider } 215d08e973aSMaximilian Schneider } 216d08e973aSMaximilian Schneider 217d08e973aSMaximilian Schneider spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 218d08e973aSMaximilian Schneider return NULL; 219d08e973aSMaximilian Schneider } 220d08e973aSMaximilian Schneider 221d08e973aSMaximilian Schneider /* releases a tx context 222d08e973aSMaximilian Schneider */ 223d08e973aSMaximilian Schneider static void gs_free_tx_context(struct gs_tx_context *txc) 224d08e973aSMaximilian Schneider { 225d08e973aSMaximilian Schneider txc->echo_id = GS_MAX_TX_URBS; 226d08e973aSMaximilian Schneider } 227d08e973aSMaximilian Schneider 228d08e973aSMaximilian Schneider /* Get a tx context by id. 229d08e973aSMaximilian Schneider */ 2309be95c91SMaximilian Schneider static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev, 2319be95c91SMaximilian Schneider unsigned int id) 232d08e973aSMaximilian Schneider { 233d08e973aSMaximilian Schneider unsigned long flags; 234d08e973aSMaximilian Schneider 235d08e973aSMaximilian Schneider if (id < GS_MAX_TX_URBS) { 236d08e973aSMaximilian Schneider spin_lock_irqsave(&dev->tx_ctx_lock, flags); 237d08e973aSMaximilian Schneider if (dev->tx_context[id].echo_id == id) { 238d08e973aSMaximilian Schneider spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 239d08e973aSMaximilian Schneider return &dev->tx_context[id]; 240d08e973aSMaximilian Schneider } 241d08e973aSMaximilian Schneider spin_unlock_irqrestore(&dev->tx_ctx_lock, flags); 242d08e973aSMaximilian Schneider } 243d08e973aSMaximilian Schneider return NULL; 244d08e973aSMaximilian Schneider } 245d08e973aSMaximilian Schneider 246*dda8deb6SMarc Kleine-Budde static int gs_cmd_reset(struct gs_can *gsdev) 247d08e973aSMaximilian Schneider { 248d08e973aSMaximilian Schneider struct gs_device_mode *dm; 249d08e973aSMaximilian Schneider struct usb_interface *intf = gsdev->iface; 250d08e973aSMaximilian Schneider int rc; 251d08e973aSMaximilian Schneider 252d08e973aSMaximilian Schneider dm = kzalloc(sizeof(*dm), GFP_KERNEL); 253d08e973aSMaximilian Schneider if (!dm) 254d08e973aSMaximilian Schneider return -ENOMEM; 255d08e973aSMaximilian Schneider 256d08e973aSMaximilian Schneider dm->mode = GS_CAN_MODE_RESET; 257d08e973aSMaximilian Schneider 258d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 259d08e973aSMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(intf), 0), 260d08e973aSMaximilian Schneider GS_USB_BREQ_MODE, 261d08e973aSMaximilian Schneider USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 262d08e973aSMaximilian Schneider gsdev->channel, 263d08e973aSMaximilian Schneider 0, 264d08e973aSMaximilian Schneider dm, 265d08e973aSMaximilian Schneider sizeof(*dm), 266d08e973aSMaximilian Schneider 1000); 267d08e973aSMaximilian Schneider 2685cda3ee5SMarc Kleine-Budde kfree(dm); 2695cda3ee5SMarc Kleine-Budde 270d08e973aSMaximilian Schneider return rc; 271d08e973aSMaximilian Schneider } 272d08e973aSMaximilian Schneider 273d08e973aSMaximilian Schneider static void gs_update_state(struct gs_can *dev, struct can_frame *cf) 274d08e973aSMaximilian Schneider { 275d08e973aSMaximilian Schneider struct can_device_stats *can_stats = &dev->can.can_stats; 276d08e973aSMaximilian Schneider 277d08e973aSMaximilian Schneider if (cf->can_id & CAN_ERR_RESTARTED) { 278d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_ACTIVE; 279d08e973aSMaximilian Schneider can_stats->restarts++; 280d08e973aSMaximilian Schneider } else if (cf->can_id & CAN_ERR_BUSOFF) { 281d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_BUS_OFF; 282d08e973aSMaximilian Schneider can_stats->bus_off++; 283d08e973aSMaximilian Schneider } else if (cf->can_id & CAN_ERR_CRTL) { 284d08e973aSMaximilian Schneider if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || 285d08e973aSMaximilian Schneider (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) { 286d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_WARNING; 287d08e973aSMaximilian Schneider can_stats->error_warning++; 288d08e973aSMaximilian Schneider } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || 289d08e973aSMaximilian Schneider (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) { 290d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_PASSIVE; 291d08e973aSMaximilian Schneider can_stats->error_passive++; 292d08e973aSMaximilian Schneider } else { 293d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_ACTIVE; 294d08e973aSMaximilian Schneider } 295d08e973aSMaximilian Schneider } 296d08e973aSMaximilian Schneider } 297d08e973aSMaximilian Schneider 298ae421e33SNik Nyby static void gs_usb_receive_bulk_callback(struct urb *urb) 299d08e973aSMaximilian Schneider { 300d08e973aSMaximilian Schneider struct gs_usb *usbcan = urb->context; 301d08e973aSMaximilian Schneider struct gs_can *dev; 302d08e973aSMaximilian Schneider struct net_device *netdev; 303d08e973aSMaximilian Schneider int rc; 304d08e973aSMaximilian Schneider struct net_device_stats *stats; 305d08e973aSMaximilian Schneider struct gs_host_frame *hf = urb->transfer_buffer; 306d08e973aSMaximilian Schneider struct gs_tx_context *txc; 307d08e973aSMaximilian Schneider struct can_frame *cf; 308d08e973aSMaximilian Schneider struct sk_buff *skb; 309d08e973aSMaximilian Schneider 310d08e973aSMaximilian Schneider BUG_ON(!usbcan); 311d08e973aSMaximilian Schneider 312d08e973aSMaximilian Schneider switch (urb->status) { 313d08e973aSMaximilian Schneider case 0: /* success */ 314d08e973aSMaximilian Schneider break; 315d08e973aSMaximilian Schneider case -ENOENT: 316d08e973aSMaximilian Schneider case -ESHUTDOWN: 317d08e973aSMaximilian Schneider return; 318d08e973aSMaximilian Schneider default: 319d08e973aSMaximilian Schneider /* do not resubmit aborted urbs. eg: when device goes down */ 320d08e973aSMaximilian Schneider return; 321d08e973aSMaximilian Schneider } 322d08e973aSMaximilian Schneider 323d08e973aSMaximilian Schneider /* device reports out of range channel id */ 324d08e973aSMaximilian Schneider if (hf->channel >= GS_MAX_INTF) 325d08e973aSMaximilian Schneider goto resubmit_urb; 326d08e973aSMaximilian Schneider 327d08e973aSMaximilian Schneider dev = usbcan->canch[hf->channel]; 328d08e973aSMaximilian Schneider 329d08e973aSMaximilian Schneider netdev = dev->netdev; 330d08e973aSMaximilian Schneider stats = &netdev->stats; 331d08e973aSMaximilian Schneider 332d08e973aSMaximilian Schneider if (!netif_device_present(netdev)) 333d08e973aSMaximilian Schneider return; 334d08e973aSMaximilian Schneider 335d08e973aSMaximilian Schneider if (hf->echo_id == -1) { /* normal rx */ 336d08e973aSMaximilian Schneider skb = alloc_can_skb(dev->netdev, &cf); 337d08e973aSMaximilian Schneider if (!skb) 338d08e973aSMaximilian Schneider return; 339d08e973aSMaximilian Schneider 340d08e973aSMaximilian Schneider cf->can_id = hf->can_id; 341d08e973aSMaximilian Schneider 342d08e973aSMaximilian Schneider cf->can_dlc = get_can_dlc(hf->can_dlc); 343d08e973aSMaximilian Schneider memcpy(cf->data, hf->data, 8); 344d08e973aSMaximilian Schneider 345d08e973aSMaximilian Schneider /* ERROR frames tell us information about the controller */ 346d08e973aSMaximilian Schneider if (hf->can_id & CAN_ERR_FLAG) 347d08e973aSMaximilian Schneider gs_update_state(dev, cf); 348d08e973aSMaximilian Schneider 349d08e973aSMaximilian Schneider netdev->stats.rx_packets++; 350d08e973aSMaximilian Schneider netdev->stats.rx_bytes += hf->can_dlc; 351d08e973aSMaximilian Schneider 352d08e973aSMaximilian Schneider netif_rx(skb); 353d08e973aSMaximilian Schneider } else { /* echo_id == hf->echo_id */ 354d08e973aSMaximilian Schneider if (hf->echo_id >= GS_MAX_TX_URBS) { 355d08e973aSMaximilian Schneider netdev_err(netdev, 356d08e973aSMaximilian Schneider "Unexpected out of range echo id %d\n", 357d08e973aSMaximilian Schneider hf->echo_id); 358d08e973aSMaximilian Schneider goto resubmit_urb; 359d08e973aSMaximilian Schneider } 360d08e973aSMaximilian Schneider 361d08e973aSMaximilian Schneider netdev->stats.tx_packets++; 362d08e973aSMaximilian Schneider netdev->stats.tx_bytes += hf->can_dlc; 363d08e973aSMaximilian Schneider 364d08e973aSMaximilian Schneider txc = gs_get_tx_context(dev, hf->echo_id); 365d08e973aSMaximilian Schneider 366d08e973aSMaximilian Schneider /* bad devices send bad echo_ids. */ 367d08e973aSMaximilian Schneider if (!txc) { 368d08e973aSMaximilian Schneider netdev_err(netdev, 369d08e973aSMaximilian Schneider "Unexpected unused echo id %d\n", 370d08e973aSMaximilian Schneider hf->echo_id); 371d08e973aSMaximilian Schneider goto resubmit_urb; 372d08e973aSMaximilian Schneider } 373d08e973aSMaximilian Schneider 374d08e973aSMaximilian Schneider can_get_echo_skb(netdev, hf->echo_id); 375d08e973aSMaximilian Schneider 376d08e973aSMaximilian Schneider gs_free_tx_context(txc); 377d08e973aSMaximilian Schneider 37897819f94SWolfgang Grandegger atomic_dec(&dev->active_tx_urbs); 37997819f94SWolfgang Grandegger 380d08e973aSMaximilian Schneider netif_wake_queue(netdev); 381d08e973aSMaximilian Schneider } 382d08e973aSMaximilian Schneider 383d08e973aSMaximilian Schneider if (hf->flags & GS_CAN_FLAG_OVERFLOW) { 384d08e973aSMaximilian Schneider skb = alloc_can_err_skb(netdev, &cf); 385d08e973aSMaximilian Schneider if (!skb) 386d08e973aSMaximilian Schneider goto resubmit_urb; 387d08e973aSMaximilian Schneider 388d08e973aSMaximilian Schneider cf->can_id |= CAN_ERR_CRTL; 389d08e973aSMaximilian Schneider cf->can_dlc = CAN_ERR_DLC; 390d08e973aSMaximilian Schneider cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 391d08e973aSMaximilian Schneider stats->rx_over_errors++; 392d08e973aSMaximilian Schneider stats->rx_errors++; 393d08e973aSMaximilian Schneider netif_rx(skb); 394d08e973aSMaximilian Schneider } 395d08e973aSMaximilian Schneider 396d08e973aSMaximilian Schneider resubmit_urb: 397d08e973aSMaximilian Schneider usb_fill_bulk_urb(urb, 398d08e973aSMaximilian Schneider usbcan->udev, 399d08e973aSMaximilian Schneider usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN), 400d08e973aSMaximilian Schneider hf, 401d08e973aSMaximilian Schneider sizeof(struct gs_host_frame), 402ae421e33SNik Nyby gs_usb_receive_bulk_callback, 403d08e973aSMaximilian Schneider usbcan 404d08e973aSMaximilian Schneider ); 405d08e973aSMaximilian Schneider 406d08e973aSMaximilian Schneider rc = usb_submit_urb(urb, GFP_ATOMIC); 407d08e973aSMaximilian Schneider 408d08e973aSMaximilian Schneider /* USB failure take down all interfaces */ 409d08e973aSMaximilian Schneider if (rc == -ENODEV) { 410d08e973aSMaximilian Schneider for (rc = 0; rc < GS_MAX_INTF; rc++) { 411d08e973aSMaximilian Schneider if (usbcan->canch[rc]) 412d08e973aSMaximilian Schneider netif_device_detach(usbcan->canch[rc]->netdev); 413d08e973aSMaximilian Schneider } 414d08e973aSMaximilian Schneider } 415d08e973aSMaximilian Schneider } 416d08e973aSMaximilian Schneider 417d08e973aSMaximilian Schneider static int gs_usb_set_bittiming(struct net_device *netdev) 418d08e973aSMaximilian Schneider { 419d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 420d08e973aSMaximilian Schneider struct can_bittiming *bt = &dev->can.bittiming; 421d08e973aSMaximilian Schneider struct usb_interface *intf = dev->iface; 422d08e973aSMaximilian Schneider int rc; 423d08e973aSMaximilian Schneider struct gs_device_bittiming *dbt; 424d08e973aSMaximilian Schneider 425d08e973aSMaximilian Schneider dbt = kmalloc(sizeof(*dbt), GFP_KERNEL); 426d08e973aSMaximilian Schneider if (!dbt) 427d08e973aSMaximilian Schneider return -ENOMEM; 428d08e973aSMaximilian Schneider 429d08e973aSMaximilian Schneider dbt->prop_seg = bt->prop_seg; 430d08e973aSMaximilian Schneider dbt->phase_seg1 = bt->phase_seg1; 431d08e973aSMaximilian Schneider dbt->phase_seg2 = bt->phase_seg2; 432d08e973aSMaximilian Schneider dbt->sjw = bt->sjw; 433d08e973aSMaximilian Schneider dbt->brp = bt->brp; 434d08e973aSMaximilian Schneider 435d08e973aSMaximilian Schneider /* request bit timings */ 436d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 437d08e973aSMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(intf), 0), 438d08e973aSMaximilian Schneider GS_USB_BREQ_BITTIMING, 439d08e973aSMaximilian Schneider USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 440d08e973aSMaximilian Schneider dev->channel, 441d08e973aSMaximilian Schneider 0, 442d08e973aSMaximilian Schneider dbt, 443d08e973aSMaximilian Schneider sizeof(*dbt), 444d08e973aSMaximilian Schneider 1000); 445d08e973aSMaximilian Schneider 446d08e973aSMaximilian Schneider kfree(dbt); 447d08e973aSMaximilian Schneider 448d08e973aSMaximilian Schneider if (rc < 0) 449d08e973aSMaximilian Schneider dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)", 450d08e973aSMaximilian Schneider rc); 451d08e973aSMaximilian Schneider 452d08e973aSMaximilian Schneider return rc; 453d08e973aSMaximilian Schneider } 454d08e973aSMaximilian Schneider 455d08e973aSMaximilian Schneider static void gs_usb_xmit_callback(struct urb *urb) 456d08e973aSMaximilian Schneider { 457d08e973aSMaximilian Schneider struct gs_tx_context *txc = urb->context; 458d08e973aSMaximilian Schneider struct gs_can *dev = txc->dev; 459d08e973aSMaximilian Schneider struct net_device *netdev = dev->netdev; 460d08e973aSMaximilian Schneider 461d08e973aSMaximilian Schneider if (urb->status) 462d08e973aSMaximilian Schneider netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id); 463d08e973aSMaximilian Schneider 464d08e973aSMaximilian Schneider usb_free_coherent(urb->dev, 465d08e973aSMaximilian Schneider urb->transfer_buffer_length, 466d08e973aSMaximilian Schneider urb->transfer_buffer, 467d08e973aSMaximilian Schneider urb->transfer_dma); 468d08e973aSMaximilian Schneider } 469d08e973aSMaximilian Schneider 4709be95c91SMaximilian Schneider static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, 4719be95c91SMaximilian Schneider struct net_device *netdev) 472d08e973aSMaximilian Schneider { 473d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 474d08e973aSMaximilian Schneider struct net_device_stats *stats = &dev->netdev->stats; 475d08e973aSMaximilian Schneider struct urb *urb; 476d08e973aSMaximilian Schneider struct gs_host_frame *hf; 477d08e973aSMaximilian Schneider struct can_frame *cf; 478d08e973aSMaximilian Schneider int rc; 479d08e973aSMaximilian Schneider unsigned int idx; 480d08e973aSMaximilian Schneider struct gs_tx_context *txc; 481d08e973aSMaximilian Schneider 482d08e973aSMaximilian Schneider if (can_dropped_invalid_skb(netdev, skb)) 483d08e973aSMaximilian Schneider return NETDEV_TX_OK; 484d08e973aSMaximilian Schneider 485d08e973aSMaximilian Schneider /* find an empty context to keep track of transmission */ 486d08e973aSMaximilian Schneider txc = gs_alloc_tx_context(dev); 487d08e973aSMaximilian Schneider if (!txc) 488d08e973aSMaximilian Schneider return NETDEV_TX_BUSY; 489d08e973aSMaximilian Schneider 490d08e973aSMaximilian Schneider /* create a URB, and a buffer for it */ 491d08e973aSMaximilian Schneider urb = usb_alloc_urb(0, GFP_ATOMIC); 4922a4a1e40SWolfram Sang if (!urb) 493d08e973aSMaximilian Schneider goto nomem_urb; 494d08e973aSMaximilian Schneider 495d08e973aSMaximilian Schneider hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC, 496d08e973aSMaximilian Schneider &urb->transfer_dma); 497d08e973aSMaximilian Schneider if (!hf) { 498d08e973aSMaximilian Schneider netdev_err(netdev, "No memory left for USB buffer\n"); 499d08e973aSMaximilian Schneider goto nomem_hf; 500d08e973aSMaximilian Schneider } 501d08e973aSMaximilian Schneider 502d08e973aSMaximilian Schneider idx = txc->echo_id; 503d08e973aSMaximilian Schneider 504d08e973aSMaximilian Schneider if (idx >= GS_MAX_TX_URBS) { 505d08e973aSMaximilian Schneider netdev_err(netdev, "Invalid tx context %d\n", idx); 506d08e973aSMaximilian Schneider goto badidx; 507d08e973aSMaximilian Schneider } 508d08e973aSMaximilian Schneider 509d08e973aSMaximilian Schneider hf->echo_id = idx; 510d08e973aSMaximilian Schneider hf->channel = dev->channel; 511d08e973aSMaximilian Schneider 512d08e973aSMaximilian Schneider cf = (struct can_frame *)skb->data; 513d08e973aSMaximilian Schneider 514d08e973aSMaximilian Schneider hf->can_id = cf->can_id; 515d08e973aSMaximilian Schneider hf->can_dlc = cf->can_dlc; 516d08e973aSMaximilian Schneider memcpy(hf->data, cf->data, cf->can_dlc); 517d08e973aSMaximilian Schneider 518d08e973aSMaximilian Schneider usb_fill_bulk_urb(urb, dev->udev, 519d08e973aSMaximilian Schneider usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT), 520d08e973aSMaximilian Schneider hf, 521d08e973aSMaximilian Schneider sizeof(*hf), 522d08e973aSMaximilian Schneider gs_usb_xmit_callback, 523d08e973aSMaximilian Schneider txc); 524d08e973aSMaximilian Schneider 525d08e973aSMaximilian Schneider urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 526d08e973aSMaximilian Schneider usb_anchor_urb(urb, &dev->tx_submitted); 527d08e973aSMaximilian Schneider 528d08e973aSMaximilian Schneider can_put_echo_skb(skb, netdev, idx); 529d08e973aSMaximilian Schneider 530d08e973aSMaximilian Schneider atomic_inc(&dev->active_tx_urbs); 531d08e973aSMaximilian Schneider 532d08e973aSMaximilian Schneider rc = usb_submit_urb(urb, GFP_ATOMIC); 533d08e973aSMaximilian Schneider if (unlikely(rc)) { /* usb send failed */ 534d08e973aSMaximilian Schneider atomic_dec(&dev->active_tx_urbs); 535d08e973aSMaximilian Schneider 536d08e973aSMaximilian Schneider can_free_echo_skb(netdev, idx); 537d08e973aSMaximilian Schneider gs_free_tx_context(txc); 538d08e973aSMaximilian Schneider 539d08e973aSMaximilian Schneider usb_unanchor_urb(urb); 540d08e973aSMaximilian Schneider usb_free_coherent(dev->udev, 541d08e973aSMaximilian Schneider sizeof(*hf), 542d08e973aSMaximilian Schneider hf, 543d08e973aSMaximilian Schneider urb->transfer_dma); 544d08e973aSMaximilian Schneider 545d08e973aSMaximilian Schneider if (rc == -ENODEV) { 546d08e973aSMaximilian Schneider netif_device_detach(netdev); 547d08e973aSMaximilian Schneider } else { 548d08e973aSMaximilian Schneider netdev_err(netdev, "usb_submit failed (err=%d)\n", rc); 549d08e973aSMaximilian Schneider stats->tx_dropped++; 550d08e973aSMaximilian Schneider } 551d08e973aSMaximilian Schneider } else { 552d08e973aSMaximilian Schneider /* Slow down tx path */ 553d08e973aSMaximilian Schneider if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS) 554d08e973aSMaximilian Schneider netif_stop_queue(netdev); 555d08e973aSMaximilian Schneider } 556d08e973aSMaximilian Schneider 557d08e973aSMaximilian Schneider /* let usb core take care of this urb */ 558d08e973aSMaximilian Schneider usb_free_urb(urb); 559d08e973aSMaximilian Schneider 560d08e973aSMaximilian Schneider return NETDEV_TX_OK; 561d08e973aSMaximilian Schneider 562d08e973aSMaximilian Schneider badidx: 563d08e973aSMaximilian Schneider usb_free_coherent(dev->udev, 564d08e973aSMaximilian Schneider sizeof(*hf), 565d08e973aSMaximilian Schneider hf, 566d08e973aSMaximilian Schneider urb->transfer_dma); 567d08e973aSMaximilian Schneider nomem_hf: 568d08e973aSMaximilian Schneider usb_free_urb(urb); 569d08e973aSMaximilian Schneider 570d08e973aSMaximilian Schneider nomem_urb: 571d08e973aSMaximilian Schneider gs_free_tx_context(txc); 572d08e973aSMaximilian Schneider dev_kfree_skb(skb); 573d08e973aSMaximilian Schneider stats->tx_dropped++; 574d08e973aSMaximilian Schneider return NETDEV_TX_OK; 575d08e973aSMaximilian Schneider } 576d08e973aSMaximilian Schneider 577d08e973aSMaximilian Schneider static int gs_can_open(struct net_device *netdev) 578d08e973aSMaximilian Schneider { 579d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 580d08e973aSMaximilian Schneider struct gs_usb *parent = dev->parent; 581d08e973aSMaximilian Schneider int rc, i; 582d08e973aSMaximilian Schneider struct gs_device_mode *dm; 583d08e973aSMaximilian Schneider u32 ctrlmode; 584d08e973aSMaximilian Schneider 585d08e973aSMaximilian Schneider rc = open_candev(netdev); 586d08e973aSMaximilian Schneider if (rc) 587d08e973aSMaximilian Schneider return rc; 588d08e973aSMaximilian Schneider 589d08e973aSMaximilian Schneider if (atomic_add_return(1, &parent->active_channels) == 1) { 590d08e973aSMaximilian Schneider for (i = 0; i < GS_MAX_RX_URBS; i++) { 591d08e973aSMaximilian Schneider struct urb *urb; 592d08e973aSMaximilian Schneider u8 *buf; 593d08e973aSMaximilian Schneider 594d08e973aSMaximilian Schneider /* alloc rx urb */ 595d08e973aSMaximilian Schneider urb = usb_alloc_urb(0, GFP_KERNEL); 5962a4a1e40SWolfram Sang if (!urb) 597d08e973aSMaximilian Schneider return -ENOMEM; 598d08e973aSMaximilian Schneider 599d08e973aSMaximilian Schneider /* alloc rx buffer */ 600d08e973aSMaximilian Schneider buf = usb_alloc_coherent(dev->udev, 601d08e973aSMaximilian Schneider sizeof(struct gs_host_frame), 602d08e973aSMaximilian Schneider GFP_KERNEL, 603d08e973aSMaximilian Schneider &urb->transfer_dma); 604d08e973aSMaximilian Schneider if (!buf) { 605d08e973aSMaximilian Schneider netdev_err(netdev, 606d08e973aSMaximilian Schneider "No memory left for USB buffer\n"); 607d08e973aSMaximilian Schneider usb_free_urb(urb); 608d08e973aSMaximilian Schneider return -ENOMEM; 609d08e973aSMaximilian Schneider } 610d08e973aSMaximilian Schneider 611d08e973aSMaximilian Schneider /* fill, anchor, and submit rx urb */ 612d08e973aSMaximilian Schneider usb_fill_bulk_urb(urb, 613d08e973aSMaximilian Schneider dev->udev, 614d08e973aSMaximilian Schneider usb_rcvbulkpipe(dev->udev, 615d08e973aSMaximilian Schneider GSUSB_ENDPOINT_IN), 616d08e973aSMaximilian Schneider buf, 617d08e973aSMaximilian Schneider sizeof(struct gs_host_frame), 618ae421e33SNik Nyby gs_usb_receive_bulk_callback, 619d08e973aSMaximilian Schneider parent); 620d08e973aSMaximilian Schneider urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 621d08e973aSMaximilian Schneider 622d08e973aSMaximilian Schneider usb_anchor_urb(urb, &parent->rx_submitted); 623d08e973aSMaximilian Schneider 624d08e973aSMaximilian Schneider rc = usb_submit_urb(urb, GFP_KERNEL); 625d08e973aSMaximilian Schneider if (rc) { 626d08e973aSMaximilian Schneider if (rc == -ENODEV) 627d08e973aSMaximilian Schneider netif_device_detach(dev->netdev); 628d08e973aSMaximilian Schneider 629d08e973aSMaximilian Schneider netdev_err(netdev, 630d08e973aSMaximilian Schneider "usb_submit failed (err=%d)\n", 631d08e973aSMaximilian Schneider rc); 632d08e973aSMaximilian Schneider 633d08e973aSMaximilian Schneider usb_unanchor_urb(urb); 634d08e973aSMaximilian Schneider break; 635d08e973aSMaximilian Schneider } 636d08e973aSMaximilian Schneider 637d08e973aSMaximilian Schneider /* Drop reference, 638d08e973aSMaximilian Schneider * USB core will take care of freeing it 639d08e973aSMaximilian Schneider */ 640d08e973aSMaximilian Schneider usb_free_urb(urb); 641d08e973aSMaximilian Schneider } 642d08e973aSMaximilian Schneider } 643d08e973aSMaximilian Schneider 644d08e973aSMaximilian Schneider dm = kmalloc(sizeof(*dm), GFP_KERNEL); 645d08e973aSMaximilian Schneider if (!dm) 646d08e973aSMaximilian Schneider return -ENOMEM; 647d08e973aSMaximilian Schneider 648d08e973aSMaximilian Schneider /* flags */ 649d08e973aSMaximilian Schneider ctrlmode = dev->can.ctrlmode; 650d08e973aSMaximilian Schneider dm->flags = 0; 651d08e973aSMaximilian Schneider 652d08e973aSMaximilian Schneider if (ctrlmode & CAN_CTRLMODE_LOOPBACK) 653d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_LOOP_BACK; 654d08e973aSMaximilian Schneider else if (ctrlmode & CAN_CTRLMODE_LISTENONLY) 655d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_LISTEN_ONLY; 656d08e973aSMaximilian Schneider 657d08e973aSMaximilian Schneider /* Controller is not allowed to retry TX 658d08e973aSMaximilian Schneider * this mode is unavailable on atmels uc3c hardware 659d08e973aSMaximilian Schneider */ 660d08e973aSMaximilian Schneider if (ctrlmode & CAN_CTRLMODE_ONE_SHOT) 661d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_ONE_SHOT; 662d08e973aSMaximilian Schneider 663d08e973aSMaximilian Schneider if (ctrlmode & CAN_CTRLMODE_3_SAMPLES) 664d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE; 665d08e973aSMaximilian Schneider 666d08e973aSMaximilian Schneider /* finally start device */ 667d08e973aSMaximilian Schneider dm->mode = GS_CAN_MODE_START; 668d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(dev->iface), 669d08e973aSMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0), 670d08e973aSMaximilian Schneider GS_USB_BREQ_MODE, 6719be95c91SMaximilian Schneider USB_DIR_OUT | USB_TYPE_VENDOR | 6729be95c91SMaximilian Schneider USB_RECIP_INTERFACE, 673d08e973aSMaximilian Schneider dev->channel, 674d08e973aSMaximilian Schneider 0, 675d08e973aSMaximilian Schneider dm, 676d08e973aSMaximilian Schneider sizeof(*dm), 677d08e973aSMaximilian Schneider 1000); 678d08e973aSMaximilian Schneider 679d08e973aSMaximilian Schneider if (rc < 0) { 680d08e973aSMaximilian Schneider netdev_err(netdev, "Couldn't start device (err=%d)\n", rc); 681d08e973aSMaximilian Schneider kfree(dm); 682d08e973aSMaximilian Schneider return rc; 683d08e973aSMaximilian Schneider } 684d08e973aSMaximilian Schneider 685d08e973aSMaximilian Schneider kfree(dm); 686d08e973aSMaximilian Schneider 687d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_ACTIVE; 688d08e973aSMaximilian Schneider 689d08e973aSMaximilian Schneider if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) 690d08e973aSMaximilian Schneider netif_start_queue(netdev); 691d08e973aSMaximilian Schneider 692d08e973aSMaximilian Schneider return 0; 693d08e973aSMaximilian Schneider } 694d08e973aSMaximilian Schneider 695d08e973aSMaximilian Schneider static int gs_can_close(struct net_device *netdev) 696d08e973aSMaximilian Schneider { 697d08e973aSMaximilian Schneider int rc; 698d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 699d08e973aSMaximilian Schneider struct gs_usb *parent = dev->parent; 700d08e973aSMaximilian Schneider 701d08e973aSMaximilian Schneider netif_stop_queue(netdev); 702d08e973aSMaximilian Schneider 703d08e973aSMaximilian Schneider /* Stop polling */ 704d08e973aSMaximilian Schneider if (atomic_dec_and_test(&parent->active_channels)) 705d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&parent->rx_submitted); 706d08e973aSMaximilian Schneider 707d08e973aSMaximilian Schneider /* Stop sending URBs */ 708d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&dev->tx_submitted); 709d08e973aSMaximilian Schneider atomic_set(&dev->active_tx_urbs, 0); 710d08e973aSMaximilian Schneider 711d08e973aSMaximilian Schneider /* reset the device */ 712*dda8deb6SMarc Kleine-Budde rc = gs_cmd_reset(dev); 713d08e973aSMaximilian Schneider if (rc < 0) 714d08e973aSMaximilian Schneider netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc); 715d08e973aSMaximilian Schneider 716d08e973aSMaximilian Schneider /* reset tx contexts */ 717d08e973aSMaximilian Schneider for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 718d08e973aSMaximilian Schneider dev->tx_context[rc].dev = dev; 719d08e973aSMaximilian Schneider dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 720d08e973aSMaximilian Schneider } 721d08e973aSMaximilian Schneider 722d08e973aSMaximilian Schneider /* close the netdev */ 723d08e973aSMaximilian Schneider close_candev(netdev); 724d08e973aSMaximilian Schneider 725d08e973aSMaximilian Schneider return 0; 726d08e973aSMaximilian Schneider } 727d08e973aSMaximilian Schneider 728d08e973aSMaximilian Schneider static const struct net_device_ops gs_usb_netdev_ops = { 729d08e973aSMaximilian Schneider .ndo_open = gs_can_open, 730d08e973aSMaximilian Schneider .ndo_stop = gs_can_close, 731d08e973aSMaximilian Schneider .ndo_start_xmit = gs_can_start_xmit, 73250212b42SMarc Kleine-Budde .ndo_change_mtu = can_change_mtu, 733d08e973aSMaximilian Schneider }; 734d08e973aSMaximilian Schneider 73505ca5270SMaximilian Schneider static int gs_usb_set_identify(struct net_device *netdev, bool do_identify) 73605ca5270SMaximilian Schneider { 73705ca5270SMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 738b05c73bdSMaksim Salau struct gs_identify_mode *imode; 73905ca5270SMaximilian Schneider int rc; 74005ca5270SMaximilian Schneider 741b05c73bdSMaksim Salau imode = kmalloc(sizeof(*imode), GFP_KERNEL); 742b05c73bdSMaksim Salau 743b05c73bdSMaksim Salau if (!imode) 744b05c73bdSMaksim Salau return -ENOMEM; 745b05c73bdSMaksim Salau 74605ca5270SMaximilian Schneider if (do_identify) 747b05c73bdSMaksim Salau imode->mode = GS_CAN_IDENTIFY_ON; 74805ca5270SMaximilian Schneider else 749b05c73bdSMaksim Salau imode->mode = GS_CAN_IDENTIFY_OFF; 75005ca5270SMaximilian Schneider 75105ca5270SMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(dev->iface), 75205ca5270SMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(dev->iface), 75305ca5270SMaximilian Schneider 0), 75405ca5270SMaximilian Schneider GS_USB_BREQ_IDENTIFY, 75505ca5270SMaximilian Schneider USB_DIR_OUT | USB_TYPE_VENDOR | 75605ca5270SMaximilian Schneider USB_RECIP_INTERFACE, 75705ca5270SMaximilian Schneider dev->channel, 75805ca5270SMaximilian Schneider 0, 759b05c73bdSMaksim Salau imode, 760b05c73bdSMaksim Salau sizeof(*imode), 76105ca5270SMaximilian Schneider 100); 76205ca5270SMaximilian Schneider 763b05c73bdSMaksim Salau kfree(imode); 764b05c73bdSMaksim Salau 76505ca5270SMaximilian Schneider return (rc > 0) ? 0 : rc; 76605ca5270SMaximilian Schneider } 76705ca5270SMaximilian Schneider 76805ca5270SMaximilian Schneider /* blink LED's for finding the this interface */ 76905ca5270SMaximilian Schneider static int gs_usb_set_phys_id(struct net_device *dev, 77005ca5270SMaximilian Schneider enum ethtool_phys_id_state state) 77105ca5270SMaximilian Schneider { 77205ca5270SMaximilian Schneider int rc = 0; 77305ca5270SMaximilian Schneider 77405ca5270SMaximilian Schneider switch (state) { 77505ca5270SMaximilian Schneider case ETHTOOL_ID_ACTIVE: 77605ca5270SMaximilian Schneider rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON); 77705ca5270SMaximilian Schneider break; 77805ca5270SMaximilian Schneider case ETHTOOL_ID_INACTIVE: 77905ca5270SMaximilian Schneider rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF); 78005ca5270SMaximilian Schneider break; 78105ca5270SMaximilian Schneider default: 78205ca5270SMaximilian Schneider break; 78305ca5270SMaximilian Schneider } 78405ca5270SMaximilian Schneider 78505ca5270SMaximilian Schneider return rc; 78605ca5270SMaximilian Schneider } 78705ca5270SMaximilian Schneider 78805ca5270SMaximilian Schneider static const struct ethtool_ops gs_usb_ethtool_ops = { 78905ca5270SMaximilian Schneider .set_phys_id = gs_usb_set_phys_id, 79005ca5270SMaximilian Schneider }; 79105ca5270SMaximilian Schneider 79205ca5270SMaximilian Schneider static struct gs_can *gs_make_candev(unsigned int channel, 79305ca5270SMaximilian Schneider struct usb_interface *intf, 79405ca5270SMaximilian Schneider struct gs_device_config *dconf) 795d08e973aSMaximilian Schneider { 796d08e973aSMaximilian Schneider struct gs_can *dev; 797d08e973aSMaximilian Schneider struct net_device *netdev; 798d08e973aSMaximilian Schneider int rc; 799d08e973aSMaximilian Schneider struct gs_device_bt_const *bt_const; 800d08e973aSMaximilian Schneider 801d08e973aSMaximilian Schneider bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL); 802d08e973aSMaximilian Schneider if (!bt_const) 803d08e973aSMaximilian Schneider return ERR_PTR(-ENOMEM); 804d08e973aSMaximilian Schneider 805d08e973aSMaximilian Schneider /* fetch bit timing constants */ 806d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 807d08e973aSMaximilian Schneider usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 808d08e973aSMaximilian Schneider GS_USB_BREQ_BT_CONST, 809d08e973aSMaximilian Schneider USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 810d08e973aSMaximilian Schneider channel, 811d08e973aSMaximilian Schneider 0, 812d08e973aSMaximilian Schneider bt_const, 813d08e973aSMaximilian Schneider sizeof(*bt_const), 814d08e973aSMaximilian Schneider 1000); 815d08e973aSMaximilian Schneider 816d08e973aSMaximilian Schneider if (rc < 0) { 817d08e973aSMaximilian Schneider dev_err(&intf->dev, 818d08e973aSMaximilian Schneider "Couldn't get bit timing const for channel (err=%d)\n", 819d08e973aSMaximilian Schneider rc); 820d08e973aSMaximilian Schneider kfree(bt_const); 821d08e973aSMaximilian Schneider return ERR_PTR(rc); 822d08e973aSMaximilian Schneider } 823d08e973aSMaximilian Schneider 824d08e973aSMaximilian Schneider /* create netdev */ 825d08e973aSMaximilian Schneider netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS); 826d08e973aSMaximilian Schneider if (!netdev) { 827d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't allocate candev\n"); 828d08e973aSMaximilian Schneider kfree(bt_const); 829d08e973aSMaximilian Schneider return ERR_PTR(-ENOMEM); 830d08e973aSMaximilian Schneider } 831d08e973aSMaximilian Schneider 832d08e973aSMaximilian Schneider dev = netdev_priv(netdev); 833d08e973aSMaximilian Schneider 834d08e973aSMaximilian Schneider netdev->netdev_ops = &gs_usb_netdev_ops; 835d08e973aSMaximilian Schneider 836d08e973aSMaximilian Schneider netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */ 837d08e973aSMaximilian Schneider 838d08e973aSMaximilian Schneider /* dev settup */ 839d08e973aSMaximilian Schneider strcpy(dev->bt_const.name, "gs_usb"); 840d08e973aSMaximilian Schneider dev->bt_const.tseg1_min = bt_const->tseg1_min; 841d08e973aSMaximilian Schneider dev->bt_const.tseg1_max = bt_const->tseg1_max; 842d08e973aSMaximilian Schneider dev->bt_const.tseg2_min = bt_const->tseg2_min; 843d08e973aSMaximilian Schneider dev->bt_const.tseg2_max = bt_const->tseg2_max; 844d08e973aSMaximilian Schneider dev->bt_const.sjw_max = bt_const->sjw_max; 845d08e973aSMaximilian Schneider dev->bt_const.brp_min = bt_const->brp_min; 846d08e973aSMaximilian Schneider dev->bt_const.brp_max = bt_const->brp_max; 847d08e973aSMaximilian Schneider dev->bt_const.brp_inc = bt_const->brp_inc; 848d08e973aSMaximilian Schneider 849d08e973aSMaximilian Schneider dev->udev = interface_to_usbdev(intf); 850d08e973aSMaximilian Schneider dev->iface = intf; 851d08e973aSMaximilian Schneider dev->netdev = netdev; 852d08e973aSMaximilian Schneider dev->channel = channel; 853d08e973aSMaximilian Schneider 854d08e973aSMaximilian Schneider init_usb_anchor(&dev->tx_submitted); 855d08e973aSMaximilian Schneider atomic_set(&dev->active_tx_urbs, 0); 856d08e973aSMaximilian Schneider spin_lock_init(&dev->tx_ctx_lock); 857d08e973aSMaximilian Schneider for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 858d08e973aSMaximilian Schneider dev->tx_context[rc].dev = dev; 859d08e973aSMaximilian Schneider dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 860d08e973aSMaximilian Schneider } 861d08e973aSMaximilian Schneider 862d08e973aSMaximilian Schneider /* can settup */ 863d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_STOPPED; 864d08e973aSMaximilian Schneider dev->can.clock.freq = bt_const->fclk_can; 865d08e973aSMaximilian Schneider dev->can.bittiming_const = &dev->bt_const; 866d08e973aSMaximilian Schneider dev->can.do_set_bittiming = gs_usb_set_bittiming; 867d08e973aSMaximilian Schneider 868d08e973aSMaximilian Schneider dev->can.ctrlmode_supported = 0; 869d08e973aSMaximilian Schneider 870d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY) 871d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 872d08e973aSMaximilian Schneider 873d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK) 874d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; 875d08e973aSMaximilian Schneider 876d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE) 877d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 878d08e973aSMaximilian Schneider 879d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT) 880d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; 881d08e973aSMaximilian Schneider 882d08e973aSMaximilian Schneider SET_NETDEV_DEV(netdev, &intf->dev); 883d08e973aSMaximilian Schneider 88405ca5270SMaximilian Schneider if (dconf->sw_version > 1) 88505ca5270SMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_IDENTIFY) 88605ca5270SMaximilian Schneider netdev->ethtool_ops = &gs_usb_ethtool_ops; 88705ca5270SMaximilian Schneider 88805ca5270SMaximilian Schneider kfree(bt_const); 88905ca5270SMaximilian Schneider 890d08e973aSMaximilian Schneider rc = register_candev(dev->netdev); 891d08e973aSMaximilian Schneider if (rc) { 892d08e973aSMaximilian Schneider free_candev(dev->netdev); 893d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc); 894d08e973aSMaximilian Schneider return ERR_PTR(rc); 895d08e973aSMaximilian Schneider } 896d08e973aSMaximilian Schneider 897d08e973aSMaximilian Schneider return dev; 898d08e973aSMaximilian Schneider } 899d08e973aSMaximilian Schneider 900d08e973aSMaximilian Schneider static void gs_destroy_candev(struct gs_can *dev) 901d08e973aSMaximilian Schneider { 902d08e973aSMaximilian Schneider unregister_candev(dev->netdev); 903d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&dev->tx_submitted); 904e9a2d81bSMaximilain Schneider free_candev(dev->netdev); 905d08e973aSMaximilian Schneider } 906d08e973aSMaximilian Schneider 90705ca5270SMaximilian Schneider static int gs_usb_probe(struct usb_interface *intf, 90805ca5270SMaximilian Schneider const struct usb_device_id *id) 909d08e973aSMaximilian Schneider { 910d08e973aSMaximilian Schneider struct gs_usb *dev; 911d08e973aSMaximilian Schneider int rc = -ENOMEM; 912d08e973aSMaximilian Schneider unsigned int icount, i; 913c919a306SEthan Zonca struct gs_host_config *hconf; 914c919a306SEthan Zonca struct gs_device_config *dconf; 915c919a306SEthan Zonca 916c919a306SEthan Zonca hconf = kmalloc(sizeof(*hconf), GFP_KERNEL); 917c919a306SEthan Zonca if (!hconf) 918c919a306SEthan Zonca return -ENOMEM; 919c919a306SEthan Zonca 920c919a306SEthan Zonca hconf->byte_order = 0x0000beef; 921d08e973aSMaximilian Schneider 922d08e973aSMaximilian Schneider /* send host config */ 923d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 924d08e973aSMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(intf), 0), 925d08e973aSMaximilian Schneider GS_USB_BREQ_HOST_FORMAT, 926d08e973aSMaximilian Schneider USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 927d08e973aSMaximilian Schneider 1, 928d08e973aSMaximilian Schneider intf->altsetting[0].desc.bInterfaceNumber, 929c919a306SEthan Zonca hconf, 930c919a306SEthan Zonca sizeof(*hconf), 931d08e973aSMaximilian Schneider 1000); 932d08e973aSMaximilian Schneider 933c919a306SEthan Zonca kfree(hconf); 934c919a306SEthan Zonca 935d08e973aSMaximilian Schneider if (rc < 0) { 936d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", 937d08e973aSMaximilian Schneider rc); 938d08e973aSMaximilian Schneider return rc; 939d08e973aSMaximilian Schneider } 940d08e973aSMaximilian Schneider 941c919a306SEthan Zonca dconf = kmalloc(sizeof(*dconf), GFP_KERNEL); 942c919a306SEthan Zonca if (!dconf) 943c919a306SEthan Zonca return -ENOMEM; 944c919a306SEthan Zonca 945d08e973aSMaximilian Schneider /* read device config */ 946d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 947d08e973aSMaximilian Schneider usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 948d08e973aSMaximilian Schneider GS_USB_BREQ_DEVICE_CONFIG, 949d08e973aSMaximilian Schneider USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, 950d08e973aSMaximilian Schneider 1, 951d08e973aSMaximilian Schneider intf->altsetting[0].desc.bInterfaceNumber, 952c919a306SEthan Zonca dconf, 953c919a306SEthan Zonca sizeof(*dconf), 954d08e973aSMaximilian Schneider 1000); 955d08e973aSMaximilian Schneider if (rc < 0) { 956d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n", 957d08e973aSMaximilian Schneider rc); 958c919a306SEthan Zonca kfree(dconf); 959d08e973aSMaximilian Schneider return rc; 960d08e973aSMaximilian Schneider } 961d08e973aSMaximilian Schneider 962c919a306SEthan Zonca icount = dconf->icount + 1; 963d08e973aSMaximilian Schneider dev_info(&intf->dev, "Configuring for %d interfaces\n", icount); 964d08e973aSMaximilian Schneider 965d08e973aSMaximilian Schneider if (icount > GS_MAX_INTF) { 966d08e973aSMaximilian Schneider dev_err(&intf->dev, 967d08e973aSMaximilian Schneider "Driver cannot handle more that %d CAN interfaces\n", 968d08e973aSMaximilian Schneider GS_MAX_INTF); 969c919a306SEthan Zonca kfree(dconf); 970d08e973aSMaximilian Schneider return -EINVAL; 971d08e973aSMaximilian Schneider } 972d08e973aSMaximilian Schneider 973d08e973aSMaximilian Schneider dev = kzalloc(sizeof(*dev), GFP_KERNEL); 974c919a306SEthan Zonca if (!dev) { 975c919a306SEthan Zonca kfree(dconf); 97637920a74SColin Ian King return -ENOMEM; 977c919a306SEthan Zonca } 978c919a306SEthan Zonca 979d08e973aSMaximilian Schneider init_usb_anchor(&dev->rx_submitted); 980d08e973aSMaximilian Schneider 981d08e973aSMaximilian Schneider atomic_set(&dev->active_channels, 0); 982d08e973aSMaximilian Schneider 983d08e973aSMaximilian Schneider usb_set_intfdata(intf, dev); 984d08e973aSMaximilian Schneider dev->udev = interface_to_usbdev(intf); 985d08e973aSMaximilian Schneider 986d08e973aSMaximilian Schneider for (i = 0; i < icount; i++) { 987c919a306SEthan Zonca dev->canch[i] = gs_make_candev(i, intf, dconf); 988d08e973aSMaximilian Schneider if (IS_ERR_OR_NULL(dev->canch[i])) { 989e9a2d81bSMaximilain Schneider /* save error code to return later */ 990e9a2d81bSMaximilain Schneider rc = PTR_ERR(dev->canch[i]); 991e9a2d81bSMaximilain Schneider 992d08e973aSMaximilian Schneider /* on failure destroy previously created candevs */ 993d08e973aSMaximilian Schneider icount = i; 994e9a2d81bSMaximilain Schneider for (i = 0; i < icount; i++) 995d08e973aSMaximilian Schneider gs_destroy_candev(dev->canch[i]); 996e9a2d81bSMaximilain Schneider 997e9a2d81bSMaximilain Schneider usb_kill_anchored_urbs(&dev->rx_submitted); 998c919a306SEthan Zonca kfree(dconf); 999d08e973aSMaximilian Schneider kfree(dev); 1000d08e973aSMaximilian Schneider return rc; 1001d08e973aSMaximilian Schneider } 1002d08e973aSMaximilian Schneider dev->canch[i]->parent = dev; 1003d08e973aSMaximilian Schneider } 1004d08e973aSMaximilian Schneider 1005c919a306SEthan Zonca kfree(dconf); 1006c919a306SEthan Zonca 1007d08e973aSMaximilian Schneider return 0; 1008d08e973aSMaximilian Schneider } 1009d08e973aSMaximilian Schneider 1010d08e973aSMaximilian Schneider static void gs_usb_disconnect(struct usb_interface *intf) 1011d08e973aSMaximilian Schneider { 1012d08e973aSMaximilian Schneider unsigned i; 1013d08e973aSMaximilian Schneider struct gs_usb *dev = usb_get_intfdata(intf); 1014d08e973aSMaximilian Schneider usb_set_intfdata(intf, NULL); 1015d08e973aSMaximilian Schneider 1016d08e973aSMaximilian Schneider if (!dev) { 1017d08e973aSMaximilian Schneider dev_err(&intf->dev, "Disconnect (nodata)\n"); 1018d08e973aSMaximilian Schneider return; 1019d08e973aSMaximilian Schneider } 1020d08e973aSMaximilian Schneider 1021e9a2d81bSMaximilain Schneider for (i = 0; i < GS_MAX_INTF; i++) 1022e9a2d81bSMaximilain Schneider if (dev->canch[i]) 1023e9a2d81bSMaximilain Schneider gs_destroy_candev(dev->canch[i]); 1024d08e973aSMaximilian Schneider 1025d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&dev->rx_submitted); 1026e9a2d81bSMaximilain Schneider kfree(dev); 1027d08e973aSMaximilian Schneider } 1028d08e973aSMaximilian Schneider 1029d08e973aSMaximilian Schneider static const struct usb_device_id gs_usb_table[] = { 10302fe6c943SMaximilian Schneider { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID, 10312fe6c943SMaximilian Schneider USB_GSUSB_1_PRODUCT_ID, 0) }, 1032f155d9c0SMaximilian Schneider { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID, 1033f155d9c0SMaximilian Schneider USB_CANDLELIGHT_PRODUCT_ID, 0) }, 1034d08e973aSMaximilian Schneider {} /* Terminating entry */ 1035d08e973aSMaximilian Schneider }; 1036d08e973aSMaximilian Schneider 1037d08e973aSMaximilian Schneider MODULE_DEVICE_TABLE(usb, gs_usb_table); 1038d08e973aSMaximilian Schneider 1039d08e973aSMaximilian Schneider static struct usb_driver gs_usb_driver = { 1040d08e973aSMaximilian Schneider .name = "gs_usb", 1041d08e973aSMaximilian Schneider .probe = gs_usb_probe, 1042d08e973aSMaximilian Schneider .disconnect = gs_usb_disconnect, 1043d08e973aSMaximilian Schneider .id_table = gs_usb_table, 1044d08e973aSMaximilian Schneider }; 1045d08e973aSMaximilian Schneider 1046d08e973aSMaximilian Schneider module_usb_driver(gs_usb_driver); 1047d08e973aSMaximilian Schneider 1048d08e973aSMaximilian Schneider MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>"); 1049d08e973aSMaximilian Schneider MODULE_DESCRIPTION( 1050d08e973aSMaximilian Schneider "Socket CAN device driver for Geschwister Schneider Technologie-, " 1051f155d9c0SMaximilian Schneider "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n" 1052f155d9c0SMaximilian Schneider "and bytewerk.org candleLight USB CAN interfaces."); 1053d08e973aSMaximilian Schneider MODULE_LICENSE("GPL v2"); 1054