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 246d08e973aSMaximilian Schneider static int gs_cmd_reset(struct gs_usb *gsusb, 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 268d08e973aSMaximilian Schneider return rc; 269d08e973aSMaximilian Schneider } 270d08e973aSMaximilian Schneider 271d08e973aSMaximilian Schneider static void gs_update_state(struct gs_can *dev, struct can_frame *cf) 272d08e973aSMaximilian Schneider { 273d08e973aSMaximilian Schneider struct can_device_stats *can_stats = &dev->can.can_stats; 274d08e973aSMaximilian Schneider 275d08e973aSMaximilian Schneider if (cf->can_id & CAN_ERR_RESTARTED) { 276d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_ACTIVE; 277d08e973aSMaximilian Schneider can_stats->restarts++; 278d08e973aSMaximilian Schneider } else if (cf->can_id & CAN_ERR_BUSOFF) { 279d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_BUS_OFF; 280d08e973aSMaximilian Schneider can_stats->bus_off++; 281d08e973aSMaximilian Schneider } else if (cf->can_id & CAN_ERR_CRTL) { 282d08e973aSMaximilian Schneider if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) || 283d08e973aSMaximilian Schneider (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) { 284d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_WARNING; 285d08e973aSMaximilian Schneider can_stats->error_warning++; 286d08e973aSMaximilian Schneider } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) || 287d08e973aSMaximilian Schneider (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) { 288d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_PASSIVE; 289d08e973aSMaximilian Schneider can_stats->error_passive++; 290d08e973aSMaximilian Schneider } else { 291d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_ACTIVE; 292d08e973aSMaximilian Schneider } 293d08e973aSMaximilian Schneider } 294d08e973aSMaximilian Schneider } 295d08e973aSMaximilian Schneider 296ae421e33SNik Nyby static void gs_usb_receive_bulk_callback(struct urb *urb) 297d08e973aSMaximilian Schneider { 298d08e973aSMaximilian Schneider struct gs_usb *usbcan = urb->context; 299d08e973aSMaximilian Schneider struct gs_can *dev; 300d08e973aSMaximilian Schneider struct net_device *netdev; 301d08e973aSMaximilian Schneider int rc; 302d08e973aSMaximilian Schneider struct net_device_stats *stats; 303d08e973aSMaximilian Schneider struct gs_host_frame *hf = urb->transfer_buffer; 304d08e973aSMaximilian Schneider struct gs_tx_context *txc; 305d08e973aSMaximilian Schneider struct can_frame *cf; 306d08e973aSMaximilian Schneider struct sk_buff *skb; 307d08e973aSMaximilian Schneider 308d08e973aSMaximilian Schneider BUG_ON(!usbcan); 309d08e973aSMaximilian Schneider 310d08e973aSMaximilian Schneider switch (urb->status) { 311d08e973aSMaximilian Schneider case 0: /* success */ 312d08e973aSMaximilian Schneider break; 313d08e973aSMaximilian Schneider case -ENOENT: 314d08e973aSMaximilian Schneider case -ESHUTDOWN: 315d08e973aSMaximilian Schneider return; 316d08e973aSMaximilian Schneider default: 317d08e973aSMaximilian Schneider /* do not resubmit aborted urbs. eg: when device goes down */ 318d08e973aSMaximilian Schneider return; 319d08e973aSMaximilian Schneider } 320d08e973aSMaximilian Schneider 321d08e973aSMaximilian Schneider /* device reports out of range channel id */ 322d08e973aSMaximilian Schneider if (hf->channel >= GS_MAX_INTF) 323d08e973aSMaximilian Schneider goto resubmit_urb; 324d08e973aSMaximilian Schneider 325d08e973aSMaximilian Schneider dev = usbcan->canch[hf->channel]; 326d08e973aSMaximilian Schneider 327d08e973aSMaximilian Schneider netdev = dev->netdev; 328d08e973aSMaximilian Schneider stats = &netdev->stats; 329d08e973aSMaximilian Schneider 330d08e973aSMaximilian Schneider if (!netif_device_present(netdev)) 331d08e973aSMaximilian Schneider return; 332d08e973aSMaximilian Schneider 333d08e973aSMaximilian Schneider if (hf->echo_id == -1) { /* normal rx */ 334d08e973aSMaximilian Schneider skb = alloc_can_skb(dev->netdev, &cf); 335d08e973aSMaximilian Schneider if (!skb) 336d08e973aSMaximilian Schneider return; 337d08e973aSMaximilian Schneider 338d08e973aSMaximilian Schneider cf->can_id = hf->can_id; 339d08e973aSMaximilian Schneider 340d08e973aSMaximilian Schneider cf->can_dlc = get_can_dlc(hf->can_dlc); 341d08e973aSMaximilian Schneider memcpy(cf->data, hf->data, 8); 342d08e973aSMaximilian Schneider 343d08e973aSMaximilian Schneider /* ERROR frames tell us information about the controller */ 344d08e973aSMaximilian Schneider if (hf->can_id & CAN_ERR_FLAG) 345d08e973aSMaximilian Schneider gs_update_state(dev, cf); 346d08e973aSMaximilian Schneider 347d08e973aSMaximilian Schneider netdev->stats.rx_packets++; 348d08e973aSMaximilian Schneider netdev->stats.rx_bytes += hf->can_dlc; 349d08e973aSMaximilian Schneider 350d08e973aSMaximilian Schneider netif_rx(skb); 351d08e973aSMaximilian Schneider } else { /* echo_id == hf->echo_id */ 352d08e973aSMaximilian Schneider if (hf->echo_id >= GS_MAX_TX_URBS) { 353d08e973aSMaximilian Schneider netdev_err(netdev, 354d08e973aSMaximilian Schneider "Unexpected out of range echo id %d\n", 355d08e973aSMaximilian Schneider hf->echo_id); 356d08e973aSMaximilian Schneider goto resubmit_urb; 357d08e973aSMaximilian Schneider } 358d08e973aSMaximilian Schneider 359d08e973aSMaximilian Schneider netdev->stats.tx_packets++; 360d08e973aSMaximilian Schneider netdev->stats.tx_bytes += hf->can_dlc; 361d08e973aSMaximilian Schneider 362d08e973aSMaximilian Schneider txc = gs_get_tx_context(dev, hf->echo_id); 363d08e973aSMaximilian Schneider 364d08e973aSMaximilian Schneider /* bad devices send bad echo_ids. */ 365d08e973aSMaximilian Schneider if (!txc) { 366d08e973aSMaximilian Schneider netdev_err(netdev, 367d08e973aSMaximilian Schneider "Unexpected unused echo id %d\n", 368d08e973aSMaximilian Schneider hf->echo_id); 369d08e973aSMaximilian Schneider goto resubmit_urb; 370d08e973aSMaximilian Schneider } 371d08e973aSMaximilian Schneider 372d08e973aSMaximilian Schneider can_get_echo_skb(netdev, hf->echo_id); 373d08e973aSMaximilian Schneider 374d08e973aSMaximilian Schneider gs_free_tx_context(txc); 375d08e973aSMaximilian Schneider 376d08e973aSMaximilian Schneider netif_wake_queue(netdev); 377d08e973aSMaximilian Schneider } 378d08e973aSMaximilian Schneider 379d08e973aSMaximilian Schneider if (hf->flags & GS_CAN_FLAG_OVERFLOW) { 380d08e973aSMaximilian Schneider skb = alloc_can_err_skb(netdev, &cf); 381d08e973aSMaximilian Schneider if (!skb) 382d08e973aSMaximilian Schneider goto resubmit_urb; 383d08e973aSMaximilian Schneider 384d08e973aSMaximilian Schneider cf->can_id |= CAN_ERR_CRTL; 385d08e973aSMaximilian Schneider cf->can_dlc = CAN_ERR_DLC; 386d08e973aSMaximilian Schneider cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 387d08e973aSMaximilian Schneider stats->rx_over_errors++; 388d08e973aSMaximilian Schneider stats->rx_errors++; 389d08e973aSMaximilian Schneider netif_rx(skb); 390d08e973aSMaximilian Schneider } 391d08e973aSMaximilian Schneider 392d08e973aSMaximilian Schneider resubmit_urb: 393d08e973aSMaximilian Schneider usb_fill_bulk_urb(urb, 394d08e973aSMaximilian Schneider usbcan->udev, 395d08e973aSMaximilian Schneider usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN), 396d08e973aSMaximilian Schneider hf, 397d08e973aSMaximilian Schneider sizeof(struct gs_host_frame), 398ae421e33SNik Nyby gs_usb_receive_bulk_callback, 399d08e973aSMaximilian Schneider usbcan 400d08e973aSMaximilian Schneider ); 401d08e973aSMaximilian Schneider 402d08e973aSMaximilian Schneider rc = usb_submit_urb(urb, GFP_ATOMIC); 403d08e973aSMaximilian Schneider 404d08e973aSMaximilian Schneider /* USB failure take down all interfaces */ 405d08e973aSMaximilian Schneider if (rc == -ENODEV) { 406d08e973aSMaximilian Schneider for (rc = 0; rc < GS_MAX_INTF; rc++) { 407d08e973aSMaximilian Schneider if (usbcan->canch[rc]) 408d08e973aSMaximilian Schneider netif_device_detach(usbcan->canch[rc]->netdev); 409d08e973aSMaximilian Schneider } 410d08e973aSMaximilian Schneider } 411d08e973aSMaximilian Schneider } 412d08e973aSMaximilian Schneider 413d08e973aSMaximilian Schneider static int gs_usb_set_bittiming(struct net_device *netdev) 414d08e973aSMaximilian Schneider { 415d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 416d08e973aSMaximilian Schneider struct can_bittiming *bt = &dev->can.bittiming; 417d08e973aSMaximilian Schneider struct usb_interface *intf = dev->iface; 418d08e973aSMaximilian Schneider int rc; 419d08e973aSMaximilian Schneider struct gs_device_bittiming *dbt; 420d08e973aSMaximilian Schneider 421d08e973aSMaximilian Schneider dbt = kmalloc(sizeof(*dbt), GFP_KERNEL); 422d08e973aSMaximilian Schneider if (!dbt) 423d08e973aSMaximilian Schneider return -ENOMEM; 424d08e973aSMaximilian Schneider 425d08e973aSMaximilian Schneider dbt->prop_seg = bt->prop_seg; 426d08e973aSMaximilian Schneider dbt->phase_seg1 = bt->phase_seg1; 427d08e973aSMaximilian Schneider dbt->phase_seg2 = bt->phase_seg2; 428d08e973aSMaximilian Schneider dbt->sjw = bt->sjw; 429d08e973aSMaximilian Schneider dbt->brp = bt->brp; 430d08e973aSMaximilian Schneider 431d08e973aSMaximilian Schneider /* request bit timings */ 432d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 433d08e973aSMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(intf), 0), 434d08e973aSMaximilian Schneider GS_USB_BREQ_BITTIMING, 435d08e973aSMaximilian Schneider USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 436d08e973aSMaximilian Schneider dev->channel, 437d08e973aSMaximilian Schneider 0, 438d08e973aSMaximilian Schneider dbt, 439d08e973aSMaximilian Schneider sizeof(*dbt), 440d08e973aSMaximilian Schneider 1000); 441d08e973aSMaximilian Schneider 442d08e973aSMaximilian Schneider kfree(dbt); 443d08e973aSMaximilian Schneider 444d08e973aSMaximilian Schneider if (rc < 0) 445d08e973aSMaximilian Schneider dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)", 446d08e973aSMaximilian Schneider rc); 447d08e973aSMaximilian Schneider 448d08e973aSMaximilian Schneider return rc; 449d08e973aSMaximilian Schneider } 450d08e973aSMaximilian Schneider 451d08e973aSMaximilian Schneider static void gs_usb_xmit_callback(struct urb *urb) 452d08e973aSMaximilian Schneider { 453d08e973aSMaximilian Schneider struct gs_tx_context *txc = urb->context; 454d08e973aSMaximilian Schneider struct gs_can *dev = txc->dev; 455d08e973aSMaximilian Schneider struct net_device *netdev = dev->netdev; 456d08e973aSMaximilian Schneider 457d08e973aSMaximilian Schneider if (urb->status) 458d08e973aSMaximilian Schneider netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id); 459d08e973aSMaximilian Schneider 460d08e973aSMaximilian Schneider usb_free_coherent(urb->dev, 461d08e973aSMaximilian Schneider urb->transfer_buffer_length, 462d08e973aSMaximilian Schneider urb->transfer_buffer, 463d08e973aSMaximilian Schneider urb->transfer_dma); 464d08e973aSMaximilian Schneider 465d08e973aSMaximilian Schneider atomic_dec(&dev->active_tx_urbs); 466d08e973aSMaximilian Schneider 467d08e973aSMaximilian Schneider if (!netif_device_present(netdev)) 468d08e973aSMaximilian Schneider return; 469d08e973aSMaximilian Schneider 470d08e973aSMaximilian Schneider if (netif_queue_stopped(netdev)) 471d08e973aSMaximilian Schneider netif_wake_queue(netdev); 472d08e973aSMaximilian Schneider } 473d08e973aSMaximilian Schneider 4749be95c91SMaximilian Schneider static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, 4759be95c91SMaximilian Schneider struct net_device *netdev) 476d08e973aSMaximilian Schneider { 477d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 478d08e973aSMaximilian Schneider struct net_device_stats *stats = &dev->netdev->stats; 479d08e973aSMaximilian Schneider struct urb *urb; 480d08e973aSMaximilian Schneider struct gs_host_frame *hf; 481d08e973aSMaximilian Schneider struct can_frame *cf; 482d08e973aSMaximilian Schneider int rc; 483d08e973aSMaximilian Schneider unsigned int idx; 484d08e973aSMaximilian Schneider struct gs_tx_context *txc; 485d08e973aSMaximilian Schneider 486d08e973aSMaximilian Schneider if (can_dropped_invalid_skb(netdev, skb)) 487d08e973aSMaximilian Schneider return NETDEV_TX_OK; 488d08e973aSMaximilian Schneider 489d08e973aSMaximilian Schneider /* find an empty context to keep track of transmission */ 490d08e973aSMaximilian Schneider txc = gs_alloc_tx_context(dev); 491d08e973aSMaximilian Schneider if (!txc) 492d08e973aSMaximilian Schneider return NETDEV_TX_BUSY; 493d08e973aSMaximilian Schneider 494d08e973aSMaximilian Schneider /* create a URB, and a buffer for it */ 495d08e973aSMaximilian Schneider urb = usb_alloc_urb(0, GFP_ATOMIC); 4962a4a1e40SWolfram Sang if (!urb) 497d08e973aSMaximilian Schneider goto nomem_urb; 498d08e973aSMaximilian Schneider 499d08e973aSMaximilian Schneider hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC, 500d08e973aSMaximilian Schneider &urb->transfer_dma); 501d08e973aSMaximilian Schneider if (!hf) { 502d08e973aSMaximilian Schneider netdev_err(netdev, "No memory left for USB buffer\n"); 503d08e973aSMaximilian Schneider goto nomem_hf; 504d08e973aSMaximilian Schneider } 505d08e973aSMaximilian Schneider 506d08e973aSMaximilian Schneider idx = txc->echo_id; 507d08e973aSMaximilian Schneider 508d08e973aSMaximilian Schneider if (idx >= GS_MAX_TX_URBS) { 509d08e973aSMaximilian Schneider netdev_err(netdev, "Invalid tx context %d\n", idx); 510d08e973aSMaximilian Schneider goto badidx; 511d08e973aSMaximilian Schneider } 512d08e973aSMaximilian Schneider 513d08e973aSMaximilian Schneider hf->echo_id = idx; 514d08e973aSMaximilian Schneider hf->channel = dev->channel; 515d08e973aSMaximilian Schneider 516d08e973aSMaximilian Schneider cf = (struct can_frame *)skb->data; 517d08e973aSMaximilian Schneider 518d08e973aSMaximilian Schneider hf->can_id = cf->can_id; 519d08e973aSMaximilian Schneider hf->can_dlc = cf->can_dlc; 520d08e973aSMaximilian Schneider memcpy(hf->data, cf->data, cf->can_dlc); 521d08e973aSMaximilian Schneider 522d08e973aSMaximilian Schneider usb_fill_bulk_urb(urb, dev->udev, 523d08e973aSMaximilian Schneider usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT), 524d08e973aSMaximilian Schneider hf, 525d08e973aSMaximilian Schneider sizeof(*hf), 526d08e973aSMaximilian Schneider gs_usb_xmit_callback, 527d08e973aSMaximilian Schneider txc); 528d08e973aSMaximilian Schneider 529d08e973aSMaximilian Schneider urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 530d08e973aSMaximilian Schneider usb_anchor_urb(urb, &dev->tx_submitted); 531d08e973aSMaximilian Schneider 532d08e973aSMaximilian Schneider can_put_echo_skb(skb, netdev, idx); 533d08e973aSMaximilian Schneider 534d08e973aSMaximilian Schneider atomic_inc(&dev->active_tx_urbs); 535d08e973aSMaximilian Schneider 536d08e973aSMaximilian Schneider rc = usb_submit_urb(urb, GFP_ATOMIC); 537d08e973aSMaximilian Schneider if (unlikely(rc)) { /* usb send failed */ 538d08e973aSMaximilian Schneider atomic_dec(&dev->active_tx_urbs); 539d08e973aSMaximilian Schneider 540d08e973aSMaximilian Schneider can_free_echo_skb(netdev, idx); 541d08e973aSMaximilian Schneider gs_free_tx_context(txc); 542d08e973aSMaximilian Schneider 543d08e973aSMaximilian Schneider usb_unanchor_urb(urb); 544d08e973aSMaximilian Schneider usb_free_coherent(dev->udev, 545d08e973aSMaximilian Schneider sizeof(*hf), 546d08e973aSMaximilian Schneider hf, 547d08e973aSMaximilian Schneider urb->transfer_dma); 548d08e973aSMaximilian Schneider 549d08e973aSMaximilian Schneider 550d08e973aSMaximilian Schneider if (rc == -ENODEV) { 551d08e973aSMaximilian Schneider netif_device_detach(netdev); 552d08e973aSMaximilian Schneider } else { 553d08e973aSMaximilian Schneider netdev_err(netdev, "usb_submit failed (err=%d)\n", rc); 554d08e973aSMaximilian Schneider stats->tx_dropped++; 555d08e973aSMaximilian Schneider } 556d08e973aSMaximilian Schneider } else { 557d08e973aSMaximilian Schneider /* Slow down tx path */ 558d08e973aSMaximilian Schneider if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS) 559d08e973aSMaximilian Schneider netif_stop_queue(netdev); 560d08e973aSMaximilian Schneider } 561d08e973aSMaximilian Schneider 562d08e973aSMaximilian Schneider /* let usb core take care of this urb */ 563d08e973aSMaximilian Schneider usb_free_urb(urb); 564d08e973aSMaximilian Schneider 565d08e973aSMaximilian Schneider return NETDEV_TX_OK; 566d08e973aSMaximilian Schneider 567d08e973aSMaximilian Schneider badidx: 568d08e973aSMaximilian Schneider usb_free_coherent(dev->udev, 569d08e973aSMaximilian Schneider sizeof(*hf), 570d08e973aSMaximilian Schneider hf, 571d08e973aSMaximilian Schneider urb->transfer_dma); 572d08e973aSMaximilian Schneider nomem_hf: 573d08e973aSMaximilian Schneider usb_free_urb(urb); 574d08e973aSMaximilian Schneider 575d08e973aSMaximilian Schneider nomem_urb: 576d08e973aSMaximilian Schneider gs_free_tx_context(txc); 577d08e973aSMaximilian Schneider dev_kfree_skb(skb); 578d08e973aSMaximilian Schneider stats->tx_dropped++; 579d08e973aSMaximilian Schneider return NETDEV_TX_OK; 580d08e973aSMaximilian Schneider } 581d08e973aSMaximilian Schneider 582d08e973aSMaximilian Schneider static int gs_can_open(struct net_device *netdev) 583d08e973aSMaximilian Schneider { 584d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 585d08e973aSMaximilian Schneider struct gs_usb *parent = dev->parent; 586d08e973aSMaximilian Schneider int rc, i; 587d08e973aSMaximilian Schneider struct gs_device_mode *dm; 588d08e973aSMaximilian Schneider u32 ctrlmode; 589d08e973aSMaximilian Schneider 590d08e973aSMaximilian Schneider rc = open_candev(netdev); 591d08e973aSMaximilian Schneider if (rc) 592d08e973aSMaximilian Schneider return rc; 593d08e973aSMaximilian Schneider 594d08e973aSMaximilian Schneider if (atomic_add_return(1, &parent->active_channels) == 1) { 595d08e973aSMaximilian Schneider for (i = 0; i < GS_MAX_RX_URBS; i++) { 596d08e973aSMaximilian Schneider struct urb *urb; 597d08e973aSMaximilian Schneider u8 *buf; 598d08e973aSMaximilian Schneider 599d08e973aSMaximilian Schneider /* alloc rx urb */ 600d08e973aSMaximilian Schneider urb = usb_alloc_urb(0, GFP_KERNEL); 6012a4a1e40SWolfram Sang if (!urb) 602d08e973aSMaximilian Schneider return -ENOMEM; 603d08e973aSMaximilian Schneider 604d08e973aSMaximilian Schneider /* alloc rx buffer */ 605d08e973aSMaximilian Schneider buf = usb_alloc_coherent(dev->udev, 606d08e973aSMaximilian Schneider sizeof(struct gs_host_frame), 607d08e973aSMaximilian Schneider GFP_KERNEL, 608d08e973aSMaximilian Schneider &urb->transfer_dma); 609d08e973aSMaximilian Schneider if (!buf) { 610d08e973aSMaximilian Schneider netdev_err(netdev, 611d08e973aSMaximilian Schneider "No memory left for USB buffer\n"); 612d08e973aSMaximilian Schneider usb_free_urb(urb); 613d08e973aSMaximilian Schneider return -ENOMEM; 614d08e973aSMaximilian Schneider } 615d08e973aSMaximilian Schneider 616d08e973aSMaximilian Schneider /* fill, anchor, and submit rx urb */ 617d08e973aSMaximilian Schneider usb_fill_bulk_urb(urb, 618d08e973aSMaximilian Schneider dev->udev, 619d08e973aSMaximilian Schneider usb_rcvbulkpipe(dev->udev, 620d08e973aSMaximilian Schneider GSUSB_ENDPOINT_IN), 621d08e973aSMaximilian Schneider buf, 622d08e973aSMaximilian Schneider sizeof(struct gs_host_frame), 623ae421e33SNik Nyby gs_usb_receive_bulk_callback, 624d08e973aSMaximilian Schneider parent); 625d08e973aSMaximilian Schneider urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 626d08e973aSMaximilian Schneider 627d08e973aSMaximilian Schneider usb_anchor_urb(urb, &parent->rx_submitted); 628d08e973aSMaximilian Schneider 629d08e973aSMaximilian Schneider rc = usb_submit_urb(urb, GFP_KERNEL); 630d08e973aSMaximilian Schneider if (rc) { 631d08e973aSMaximilian Schneider if (rc == -ENODEV) 632d08e973aSMaximilian Schneider netif_device_detach(dev->netdev); 633d08e973aSMaximilian Schneider 634d08e973aSMaximilian Schneider netdev_err(netdev, 635d08e973aSMaximilian Schneider "usb_submit failed (err=%d)\n", 636d08e973aSMaximilian Schneider rc); 637d08e973aSMaximilian Schneider 638d08e973aSMaximilian Schneider usb_unanchor_urb(urb); 639d08e973aSMaximilian Schneider break; 640d08e973aSMaximilian Schneider } 641d08e973aSMaximilian Schneider 642d08e973aSMaximilian Schneider /* Drop reference, 643d08e973aSMaximilian Schneider * USB core will take care of freeing it 644d08e973aSMaximilian Schneider */ 645d08e973aSMaximilian Schneider usb_free_urb(urb); 646d08e973aSMaximilian Schneider } 647d08e973aSMaximilian Schneider } 648d08e973aSMaximilian Schneider 649d08e973aSMaximilian Schneider dm = kmalloc(sizeof(*dm), GFP_KERNEL); 650d08e973aSMaximilian Schneider if (!dm) 651d08e973aSMaximilian Schneider return -ENOMEM; 652d08e973aSMaximilian Schneider 653d08e973aSMaximilian Schneider /* flags */ 654d08e973aSMaximilian Schneider ctrlmode = dev->can.ctrlmode; 655d08e973aSMaximilian Schneider dm->flags = 0; 656d08e973aSMaximilian Schneider 657d08e973aSMaximilian Schneider if (ctrlmode & CAN_CTRLMODE_LOOPBACK) 658d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_LOOP_BACK; 659d08e973aSMaximilian Schneider else if (ctrlmode & CAN_CTRLMODE_LISTENONLY) 660d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_LISTEN_ONLY; 661d08e973aSMaximilian Schneider 662d08e973aSMaximilian Schneider /* Controller is not allowed to retry TX 663d08e973aSMaximilian Schneider * this mode is unavailable on atmels uc3c hardware 664d08e973aSMaximilian Schneider */ 665d08e973aSMaximilian Schneider if (ctrlmode & CAN_CTRLMODE_ONE_SHOT) 666d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_ONE_SHOT; 667d08e973aSMaximilian Schneider 668d08e973aSMaximilian Schneider if (ctrlmode & CAN_CTRLMODE_3_SAMPLES) 669d08e973aSMaximilian Schneider dm->flags |= GS_CAN_MODE_TRIPLE_SAMPLE; 670d08e973aSMaximilian Schneider 671d08e973aSMaximilian Schneider /* finally start device */ 672d08e973aSMaximilian Schneider dm->mode = GS_CAN_MODE_START; 673d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(dev->iface), 674d08e973aSMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0), 675d08e973aSMaximilian Schneider GS_USB_BREQ_MODE, 6769be95c91SMaximilian Schneider USB_DIR_OUT | USB_TYPE_VENDOR | 6779be95c91SMaximilian Schneider USB_RECIP_INTERFACE, 678d08e973aSMaximilian Schneider dev->channel, 679d08e973aSMaximilian Schneider 0, 680d08e973aSMaximilian Schneider dm, 681d08e973aSMaximilian Schneider sizeof(*dm), 682d08e973aSMaximilian Schneider 1000); 683d08e973aSMaximilian Schneider 684d08e973aSMaximilian Schneider if (rc < 0) { 685d08e973aSMaximilian Schneider netdev_err(netdev, "Couldn't start device (err=%d)\n", rc); 686d08e973aSMaximilian Schneider kfree(dm); 687d08e973aSMaximilian Schneider return rc; 688d08e973aSMaximilian Schneider } 689d08e973aSMaximilian Schneider 690d08e973aSMaximilian Schneider kfree(dm); 691d08e973aSMaximilian Schneider 692d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_ERROR_ACTIVE; 693d08e973aSMaximilian Schneider 694d08e973aSMaximilian Schneider if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) 695d08e973aSMaximilian Schneider netif_start_queue(netdev); 696d08e973aSMaximilian Schneider 697d08e973aSMaximilian Schneider return 0; 698d08e973aSMaximilian Schneider } 699d08e973aSMaximilian Schneider 700d08e973aSMaximilian Schneider static int gs_can_close(struct net_device *netdev) 701d08e973aSMaximilian Schneider { 702d08e973aSMaximilian Schneider int rc; 703d08e973aSMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 704d08e973aSMaximilian Schneider struct gs_usb *parent = dev->parent; 705d08e973aSMaximilian Schneider 706d08e973aSMaximilian Schneider netif_stop_queue(netdev); 707d08e973aSMaximilian Schneider 708d08e973aSMaximilian Schneider /* Stop polling */ 709d08e973aSMaximilian Schneider if (atomic_dec_and_test(&parent->active_channels)) 710d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&parent->rx_submitted); 711d08e973aSMaximilian Schneider 712d08e973aSMaximilian Schneider /* Stop sending URBs */ 713d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&dev->tx_submitted); 714d08e973aSMaximilian Schneider atomic_set(&dev->active_tx_urbs, 0); 715d08e973aSMaximilian Schneider 716d08e973aSMaximilian Schneider /* reset the device */ 717d08e973aSMaximilian Schneider rc = gs_cmd_reset(parent, dev); 718d08e973aSMaximilian Schneider if (rc < 0) 719d08e973aSMaximilian Schneider netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc); 720d08e973aSMaximilian Schneider 721d08e973aSMaximilian Schneider /* reset tx contexts */ 722d08e973aSMaximilian Schneider for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 723d08e973aSMaximilian Schneider dev->tx_context[rc].dev = dev; 724d08e973aSMaximilian Schneider dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 725d08e973aSMaximilian Schneider } 726d08e973aSMaximilian Schneider 727d08e973aSMaximilian Schneider /* close the netdev */ 728d08e973aSMaximilian Schneider close_candev(netdev); 729d08e973aSMaximilian Schneider 730d08e973aSMaximilian Schneider return 0; 731d08e973aSMaximilian Schneider } 732d08e973aSMaximilian Schneider 733d08e973aSMaximilian Schneider static const struct net_device_ops gs_usb_netdev_ops = { 734d08e973aSMaximilian Schneider .ndo_open = gs_can_open, 735d08e973aSMaximilian Schneider .ndo_stop = gs_can_close, 736d08e973aSMaximilian Schneider .ndo_start_xmit = gs_can_start_xmit, 73750212b42SMarc Kleine-Budde .ndo_change_mtu = can_change_mtu, 738d08e973aSMaximilian Schneider }; 739d08e973aSMaximilian Schneider 74005ca5270SMaximilian Schneider static int gs_usb_set_identify(struct net_device *netdev, bool do_identify) 74105ca5270SMaximilian Schneider { 74205ca5270SMaximilian Schneider struct gs_can *dev = netdev_priv(netdev); 74305ca5270SMaximilian Schneider struct gs_identify_mode imode; 74405ca5270SMaximilian Schneider int rc; 74505ca5270SMaximilian Schneider 74605ca5270SMaximilian Schneider if (do_identify) 74705ca5270SMaximilian Schneider imode.mode = GS_CAN_IDENTIFY_ON; 74805ca5270SMaximilian Schneider else 74905ca5270SMaximilian Schneider 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, 75905ca5270SMaximilian Schneider &imode, 76005ca5270SMaximilian Schneider sizeof(imode), 76105ca5270SMaximilian Schneider 100); 76205ca5270SMaximilian Schneider 76305ca5270SMaximilian Schneider return (rc > 0) ? 0 : rc; 76405ca5270SMaximilian Schneider } 76505ca5270SMaximilian Schneider 76605ca5270SMaximilian Schneider /* blink LED's for finding the this interface */ 76705ca5270SMaximilian Schneider static int gs_usb_set_phys_id(struct net_device *dev, 76805ca5270SMaximilian Schneider enum ethtool_phys_id_state state) 76905ca5270SMaximilian Schneider { 77005ca5270SMaximilian Schneider int rc = 0; 77105ca5270SMaximilian Schneider 77205ca5270SMaximilian Schneider switch (state) { 77305ca5270SMaximilian Schneider case ETHTOOL_ID_ACTIVE: 77405ca5270SMaximilian Schneider rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON); 77505ca5270SMaximilian Schneider break; 77605ca5270SMaximilian Schneider case ETHTOOL_ID_INACTIVE: 77705ca5270SMaximilian Schneider rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF); 77805ca5270SMaximilian Schneider break; 77905ca5270SMaximilian Schneider default: 78005ca5270SMaximilian Schneider break; 78105ca5270SMaximilian Schneider } 78205ca5270SMaximilian Schneider 78305ca5270SMaximilian Schneider return rc; 78405ca5270SMaximilian Schneider } 78505ca5270SMaximilian Schneider 78605ca5270SMaximilian Schneider static const struct ethtool_ops gs_usb_ethtool_ops = { 78705ca5270SMaximilian Schneider .set_phys_id = gs_usb_set_phys_id, 78805ca5270SMaximilian Schneider }; 78905ca5270SMaximilian Schneider 79005ca5270SMaximilian Schneider static struct gs_can *gs_make_candev(unsigned int channel, 79105ca5270SMaximilian Schneider struct usb_interface *intf, 79205ca5270SMaximilian Schneider struct gs_device_config *dconf) 793d08e973aSMaximilian Schneider { 794d08e973aSMaximilian Schneider struct gs_can *dev; 795d08e973aSMaximilian Schneider struct net_device *netdev; 796d08e973aSMaximilian Schneider int rc; 797d08e973aSMaximilian Schneider struct gs_device_bt_const *bt_const; 798d08e973aSMaximilian Schneider 799d08e973aSMaximilian Schneider bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL); 800d08e973aSMaximilian Schneider if (!bt_const) 801d08e973aSMaximilian Schneider return ERR_PTR(-ENOMEM); 802d08e973aSMaximilian Schneider 803d08e973aSMaximilian Schneider /* fetch bit timing constants */ 804d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 805d08e973aSMaximilian Schneider usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 806d08e973aSMaximilian Schneider GS_USB_BREQ_BT_CONST, 807d08e973aSMaximilian Schneider USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 808d08e973aSMaximilian Schneider channel, 809d08e973aSMaximilian Schneider 0, 810d08e973aSMaximilian Schneider bt_const, 811d08e973aSMaximilian Schneider sizeof(*bt_const), 812d08e973aSMaximilian Schneider 1000); 813d08e973aSMaximilian Schneider 814d08e973aSMaximilian Schneider if (rc < 0) { 815d08e973aSMaximilian Schneider dev_err(&intf->dev, 816d08e973aSMaximilian Schneider "Couldn't get bit timing const for channel (err=%d)\n", 817d08e973aSMaximilian Schneider rc); 818d08e973aSMaximilian Schneider kfree(bt_const); 819d08e973aSMaximilian Schneider return ERR_PTR(rc); 820d08e973aSMaximilian Schneider } 821d08e973aSMaximilian Schneider 822d08e973aSMaximilian Schneider /* create netdev */ 823d08e973aSMaximilian Schneider netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS); 824d08e973aSMaximilian Schneider if (!netdev) { 825d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't allocate candev\n"); 826d08e973aSMaximilian Schneider kfree(bt_const); 827d08e973aSMaximilian Schneider return ERR_PTR(-ENOMEM); 828d08e973aSMaximilian Schneider } 829d08e973aSMaximilian Schneider 830d08e973aSMaximilian Schneider dev = netdev_priv(netdev); 831d08e973aSMaximilian Schneider 832d08e973aSMaximilian Schneider netdev->netdev_ops = &gs_usb_netdev_ops; 833d08e973aSMaximilian Schneider 834d08e973aSMaximilian Schneider netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */ 835d08e973aSMaximilian Schneider 836d08e973aSMaximilian Schneider /* dev settup */ 837d08e973aSMaximilian Schneider strcpy(dev->bt_const.name, "gs_usb"); 838d08e973aSMaximilian Schneider dev->bt_const.tseg1_min = bt_const->tseg1_min; 839d08e973aSMaximilian Schneider dev->bt_const.tseg1_max = bt_const->tseg1_max; 840d08e973aSMaximilian Schneider dev->bt_const.tseg2_min = bt_const->tseg2_min; 841d08e973aSMaximilian Schneider dev->bt_const.tseg2_max = bt_const->tseg2_max; 842d08e973aSMaximilian Schneider dev->bt_const.sjw_max = bt_const->sjw_max; 843d08e973aSMaximilian Schneider dev->bt_const.brp_min = bt_const->brp_min; 844d08e973aSMaximilian Schneider dev->bt_const.brp_max = bt_const->brp_max; 845d08e973aSMaximilian Schneider dev->bt_const.brp_inc = bt_const->brp_inc; 846d08e973aSMaximilian Schneider 847d08e973aSMaximilian Schneider dev->udev = interface_to_usbdev(intf); 848d08e973aSMaximilian Schneider dev->iface = intf; 849d08e973aSMaximilian Schneider dev->netdev = netdev; 850d08e973aSMaximilian Schneider dev->channel = channel; 851d08e973aSMaximilian Schneider 852d08e973aSMaximilian Schneider init_usb_anchor(&dev->tx_submitted); 853d08e973aSMaximilian Schneider atomic_set(&dev->active_tx_urbs, 0); 854d08e973aSMaximilian Schneider spin_lock_init(&dev->tx_ctx_lock); 855d08e973aSMaximilian Schneider for (rc = 0; rc < GS_MAX_TX_URBS; rc++) { 856d08e973aSMaximilian Schneider dev->tx_context[rc].dev = dev; 857d08e973aSMaximilian Schneider dev->tx_context[rc].echo_id = GS_MAX_TX_URBS; 858d08e973aSMaximilian Schneider } 859d08e973aSMaximilian Schneider 860d08e973aSMaximilian Schneider /* can settup */ 861d08e973aSMaximilian Schneider dev->can.state = CAN_STATE_STOPPED; 862d08e973aSMaximilian Schneider dev->can.clock.freq = bt_const->fclk_can; 863d08e973aSMaximilian Schneider dev->can.bittiming_const = &dev->bt_const; 864d08e973aSMaximilian Schneider dev->can.do_set_bittiming = gs_usb_set_bittiming; 865d08e973aSMaximilian Schneider 866d08e973aSMaximilian Schneider dev->can.ctrlmode_supported = 0; 867d08e973aSMaximilian Schneider 868d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_LISTEN_ONLY) 869d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; 870d08e973aSMaximilian Schneider 871d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_LOOP_BACK) 872d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; 873d08e973aSMaximilian Schneider 874d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_TRIPLE_SAMPLE) 875d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 876d08e973aSMaximilian Schneider 877d08e973aSMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT) 878d08e973aSMaximilian Schneider dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; 879d08e973aSMaximilian Schneider 880d08e973aSMaximilian Schneider SET_NETDEV_DEV(netdev, &intf->dev); 881d08e973aSMaximilian Schneider 88205ca5270SMaximilian Schneider if (dconf->sw_version > 1) 88305ca5270SMaximilian Schneider if (bt_const->feature & GS_CAN_FEATURE_IDENTIFY) 88405ca5270SMaximilian Schneider netdev->ethtool_ops = &gs_usb_ethtool_ops; 88505ca5270SMaximilian Schneider 88605ca5270SMaximilian Schneider kfree(bt_const); 88705ca5270SMaximilian Schneider 888d08e973aSMaximilian Schneider rc = register_candev(dev->netdev); 889d08e973aSMaximilian Schneider if (rc) { 890d08e973aSMaximilian Schneider free_candev(dev->netdev); 891d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc); 892d08e973aSMaximilian Schneider return ERR_PTR(rc); 893d08e973aSMaximilian Schneider } 894d08e973aSMaximilian Schneider 895d08e973aSMaximilian Schneider return dev; 896d08e973aSMaximilian Schneider } 897d08e973aSMaximilian Schneider 898d08e973aSMaximilian Schneider static void gs_destroy_candev(struct gs_can *dev) 899d08e973aSMaximilian Schneider { 900d08e973aSMaximilian Schneider unregister_candev(dev->netdev); 901d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&dev->tx_submitted); 902e9a2d81bSMaximilain Schneider free_candev(dev->netdev); 903d08e973aSMaximilian Schneider } 904d08e973aSMaximilian Schneider 90505ca5270SMaximilian Schneider static int gs_usb_probe(struct usb_interface *intf, 90605ca5270SMaximilian Schneider const struct usb_device_id *id) 907d08e973aSMaximilian Schneider { 908d08e973aSMaximilian Schneider struct gs_usb *dev; 909d08e973aSMaximilian Schneider int rc = -ENOMEM; 910d08e973aSMaximilian Schneider unsigned int icount, i; 911*c919a306SEthan Zonca struct gs_host_config *hconf; 912*c919a306SEthan Zonca struct gs_device_config *dconf; 913*c919a306SEthan Zonca 914*c919a306SEthan Zonca hconf = kmalloc(sizeof(*hconf), GFP_KERNEL); 915*c919a306SEthan Zonca if (!hconf) 916*c919a306SEthan Zonca return -ENOMEM; 917*c919a306SEthan Zonca 918*c919a306SEthan Zonca hconf->byte_order = 0x0000beef; 919d08e973aSMaximilian Schneider 920d08e973aSMaximilian Schneider /* send host config */ 921d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 922d08e973aSMaximilian Schneider usb_sndctrlpipe(interface_to_usbdev(intf), 0), 923d08e973aSMaximilian Schneider GS_USB_BREQ_HOST_FORMAT, 924d08e973aSMaximilian Schneider USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 925d08e973aSMaximilian Schneider 1, 926d08e973aSMaximilian Schneider intf->altsetting[0].desc.bInterfaceNumber, 927*c919a306SEthan Zonca hconf, 928*c919a306SEthan Zonca sizeof(*hconf), 929d08e973aSMaximilian Schneider 1000); 930d08e973aSMaximilian Schneider 931*c919a306SEthan Zonca kfree(hconf); 932*c919a306SEthan Zonca 933d08e973aSMaximilian Schneider if (rc < 0) { 934d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", 935d08e973aSMaximilian Schneider rc); 936d08e973aSMaximilian Schneider return rc; 937d08e973aSMaximilian Schneider } 938d08e973aSMaximilian Schneider 939*c919a306SEthan Zonca dconf = kmalloc(sizeof(*dconf), GFP_KERNEL); 940*c919a306SEthan Zonca if (!dconf) 941*c919a306SEthan Zonca return -ENOMEM; 942*c919a306SEthan Zonca 943d08e973aSMaximilian Schneider /* read device config */ 944d08e973aSMaximilian Schneider rc = usb_control_msg(interface_to_usbdev(intf), 945d08e973aSMaximilian Schneider usb_rcvctrlpipe(interface_to_usbdev(intf), 0), 946d08e973aSMaximilian Schneider GS_USB_BREQ_DEVICE_CONFIG, 947d08e973aSMaximilian Schneider USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 948d08e973aSMaximilian Schneider 1, 949d08e973aSMaximilian Schneider intf->altsetting[0].desc.bInterfaceNumber, 950*c919a306SEthan Zonca dconf, 951*c919a306SEthan Zonca sizeof(*dconf), 952d08e973aSMaximilian Schneider 1000); 953d08e973aSMaximilian Schneider if (rc < 0) { 954d08e973aSMaximilian Schneider dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n", 955d08e973aSMaximilian Schneider rc); 956*c919a306SEthan Zonca kfree(dconf); 957d08e973aSMaximilian Schneider return rc; 958d08e973aSMaximilian Schneider } 959d08e973aSMaximilian Schneider 960*c919a306SEthan Zonca icount = dconf->icount + 1; 961d08e973aSMaximilian Schneider dev_info(&intf->dev, "Configuring for %d interfaces\n", icount); 962d08e973aSMaximilian Schneider 963d08e973aSMaximilian Schneider if (icount > GS_MAX_INTF) { 964d08e973aSMaximilian Schneider dev_err(&intf->dev, 965d08e973aSMaximilian Schneider "Driver cannot handle more that %d CAN interfaces\n", 966d08e973aSMaximilian Schneider GS_MAX_INTF); 967*c919a306SEthan Zonca kfree(dconf); 968d08e973aSMaximilian Schneider return -EINVAL; 969d08e973aSMaximilian Schneider } 970d08e973aSMaximilian Schneider 971d08e973aSMaximilian Schneider dev = kzalloc(sizeof(*dev), GFP_KERNEL); 972*c919a306SEthan Zonca if (!dev) { 973*c919a306SEthan Zonca kfree(dconf); 97437920a74SColin Ian King return -ENOMEM; 975*c919a306SEthan Zonca } 976*c919a306SEthan Zonca 977d08e973aSMaximilian Schneider init_usb_anchor(&dev->rx_submitted); 978d08e973aSMaximilian Schneider 979d08e973aSMaximilian Schneider atomic_set(&dev->active_channels, 0); 980d08e973aSMaximilian Schneider 981d08e973aSMaximilian Schneider usb_set_intfdata(intf, dev); 982d08e973aSMaximilian Schneider dev->udev = interface_to_usbdev(intf); 983d08e973aSMaximilian Schneider 984d08e973aSMaximilian Schneider for (i = 0; i < icount; i++) { 985*c919a306SEthan Zonca dev->canch[i] = gs_make_candev(i, intf, dconf); 986d08e973aSMaximilian Schneider if (IS_ERR_OR_NULL(dev->canch[i])) { 987e9a2d81bSMaximilain Schneider /* save error code to return later */ 988e9a2d81bSMaximilain Schneider rc = PTR_ERR(dev->canch[i]); 989e9a2d81bSMaximilain Schneider 990d08e973aSMaximilian Schneider /* on failure destroy previously created candevs */ 991d08e973aSMaximilian Schneider icount = i; 992e9a2d81bSMaximilain Schneider for (i = 0; i < icount; i++) 993d08e973aSMaximilian Schneider gs_destroy_candev(dev->canch[i]); 994e9a2d81bSMaximilain Schneider 995e9a2d81bSMaximilain Schneider usb_kill_anchored_urbs(&dev->rx_submitted); 996*c919a306SEthan Zonca kfree(dconf); 997d08e973aSMaximilian Schneider kfree(dev); 998d08e973aSMaximilian Schneider return rc; 999d08e973aSMaximilian Schneider } 1000d08e973aSMaximilian Schneider dev->canch[i]->parent = dev; 1001d08e973aSMaximilian Schneider } 1002d08e973aSMaximilian Schneider 1003*c919a306SEthan Zonca kfree(dconf); 1004*c919a306SEthan Zonca 1005d08e973aSMaximilian Schneider return 0; 1006d08e973aSMaximilian Schneider } 1007d08e973aSMaximilian Schneider 1008d08e973aSMaximilian Schneider static void gs_usb_disconnect(struct usb_interface *intf) 1009d08e973aSMaximilian Schneider { 1010d08e973aSMaximilian Schneider unsigned i; 1011d08e973aSMaximilian Schneider struct gs_usb *dev = usb_get_intfdata(intf); 1012d08e973aSMaximilian Schneider usb_set_intfdata(intf, NULL); 1013d08e973aSMaximilian Schneider 1014d08e973aSMaximilian Schneider if (!dev) { 1015d08e973aSMaximilian Schneider dev_err(&intf->dev, "Disconnect (nodata)\n"); 1016d08e973aSMaximilian Schneider return; 1017d08e973aSMaximilian Schneider } 1018d08e973aSMaximilian Schneider 1019e9a2d81bSMaximilain Schneider for (i = 0; i < GS_MAX_INTF; i++) 1020e9a2d81bSMaximilain Schneider if (dev->canch[i]) 1021e9a2d81bSMaximilain Schneider gs_destroy_candev(dev->canch[i]); 1022d08e973aSMaximilian Schneider 1023d08e973aSMaximilian Schneider usb_kill_anchored_urbs(&dev->rx_submitted); 1024e9a2d81bSMaximilain Schneider kfree(dev); 1025d08e973aSMaximilian Schneider } 1026d08e973aSMaximilian Schneider 1027d08e973aSMaximilian Schneider static const struct usb_device_id gs_usb_table[] = { 10282fe6c943SMaximilian Schneider { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID, 10292fe6c943SMaximilian Schneider USB_GSUSB_1_PRODUCT_ID, 0) }, 1030f155d9c0SMaximilian Schneider { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID, 1031f155d9c0SMaximilian Schneider USB_CANDLELIGHT_PRODUCT_ID, 0) }, 1032d08e973aSMaximilian Schneider {} /* Terminating entry */ 1033d08e973aSMaximilian Schneider }; 1034d08e973aSMaximilian Schneider 1035d08e973aSMaximilian Schneider MODULE_DEVICE_TABLE(usb, gs_usb_table); 1036d08e973aSMaximilian Schneider 1037d08e973aSMaximilian Schneider static struct usb_driver gs_usb_driver = { 1038d08e973aSMaximilian Schneider .name = "gs_usb", 1039d08e973aSMaximilian Schneider .probe = gs_usb_probe, 1040d08e973aSMaximilian Schneider .disconnect = gs_usb_disconnect, 1041d08e973aSMaximilian Schneider .id_table = gs_usb_table, 1042d08e973aSMaximilian Schneider }; 1043d08e973aSMaximilian Schneider 1044d08e973aSMaximilian Schneider module_usb_driver(gs_usb_driver); 1045d08e973aSMaximilian Schneider 1046d08e973aSMaximilian Schneider MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>"); 1047d08e973aSMaximilian Schneider MODULE_DESCRIPTION( 1048d08e973aSMaximilian Schneider "Socket CAN device driver for Geschwister Schneider Technologie-, " 1049f155d9c0SMaximilian Schneider "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n" 1050f155d9c0SMaximilian Schneider "and bytewerk.org candleLight USB CAN interfaces."); 1051d08e973aSMaximilian Schneider MODULE_LICENSE("GPL v2"); 1052