20 #include <linux/signal.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
58 pr_info(
"%s dumping %s (%d bytes):\n",
76 static void peak_usb_add_us(
struct timeval *tv,
u32 delta_us)
79 u32 delta_s = delta_us / 1000000;
81 delta_us -= delta_s * 1000000;
99 if (time_ref->
tv_host.tv_sec > 0) {
103 delta_ts &= (1 << time_ref->
adapter->ts_used_bits) - 1;
114 if (time_ref->tv_host_0.tv_sec == 0) {
116 time_ref->tv_host_0 = ktime_to_timeval(
ktime_get());
124 if (time_ref->
tv_host.tv_sec != 0) {
126 - time_ref->tv_host_0.tv_sec;
127 if (delta_s > 4200) {
128 time_ref->tv_host_0 = time_ref->
tv_host;
148 if (time_ref->
tv_host.tv_sec > 0) {
152 if (ts < time_ref->ts_dev_2)
153 delta_us &= (1 << time_ref->
adapter->ts_used_bits) - 1;
157 delta_us *= time_ref->
adapter->us_per_ts_scale;
158 delta_us >>= time_ref->
adapter->us_per_ts_shift;
160 *tv = time_ref->tv_host_0;
161 peak_usb_add_us(tv, (
u32)delta_us);
170 static void peak_usb_read_bulk_callback(
struct urb *
urb)
178 if (!netif_device_present(netdev))
182 switch (urb->status) {
196 "Rx urb aborted (%d)\n", urb->status);
201 if ((urb->actual_length > 0) && (dev->
adapter->dev_decode_buf)) {
204 err = dev->
adapter->dev_decode_buf(dev, urb);
207 urb->transfer_buffer,
208 urb->transfer_buffer_length);
213 usb_fill_bulk_urb(urb, dev->
udev,
215 urb->transfer_buffer, dev->
adapter->rx_buffer_size,
216 peak_usb_read_bulk_callback, dev);
228 netdev_err(netdev,
"failed resubmitting read bulk urb: %d\n",
235 static void peak_usb_write_bulk_callback(
struct urb *urb)
248 if (!netif_device_present(netdev))
252 switch (urb->status) {
255 netdev->
stats.tx_packets++;
256 netdev->
stats.tx_bytes += context->
dlc;
264 netdev_err(netdev,
"Tx urb aborted (%d)\n",
280 netif_wake_queue(netdev);
298 if (can_dropped_invalid_skb(netdev, skb))
302 if (dev->
tx_contexts[i].echo_index == PCAN_USB_MAX_TX_URBS) {
313 obuf = urb->transfer_buffer;
315 err = dev->
adapter->dev_encode_msg(dev, skb, obuf, &
size);
318 netdev_err(netdev,
"packet dropped\n");
349 netdev_warn(netdev,
"tx urb submitting failed err=%d\n",
360 netif_stop_queue(netdev);
382 netdev_err(netdev,
"No memory left for URBs\n");
389 netdev_err(netdev,
"No memory left for USB buffer\n");
395 usb_fill_bulk_urb(urb, dev->
udev,
397 buf, dev->
adapter->rx_buffer_size,
398 peak_usb_read_bulk_callback, dev);
401 urb->transfer_flags |= URB_FREE_BUFFER;
420 if (i < PCAN_USB_MAX_RX_URBS) {
422 netdev_err(netdev,
"couldn't setup any rx URB\n");
426 netdev_warn(netdev,
"rx performance may be slow\n");
438 netdev_err(netdev,
"No memory left for URBs\n");
445 netdev_err(netdev,
"No memory left for USB buffer\n");
455 usb_fill_bulk_urb(urb, dev->
udev,
457 buf, dev->
adapter->tx_buffer_size,
458 peak_usb_write_bulk_callback, context);
461 urb->transfer_flags |= URB_FREE_BUFFER;
465 if (i < PCAN_USB_MAX_TX_URBS) {
467 netdev_err(netdev,
"couldn't setup any tx URB\n");
471 netdev_warn(netdev,
"tx performance may be slow\n");
475 err = dev->
adapter->dev_start(dev);
483 if (dev->
adapter->dev_set_bus) {
484 err = dev->
adapter->dev_set_bus(dev, 1);
497 netdev_warn(netdev,
"couldn't submit control: %d\n", err);
505 static int peak_usb_ndo_open(
struct net_device *netdev)
516 err = peak_usb_start(dev);
518 netdev_err(netdev,
"couldn't start device: %d\n", err);
524 netif_start_queue(netdev);
544 dev->
tx_contexts[i].echo_index != PCAN_USB_MAX_TX_URBS) {
564 static int peak_usb_ndo_stop(
struct net_device *netdev)
569 netif_stop_queue(netdev);
572 peak_usb_unlink_all_urbs(dev);
583 if (dev->
adapter->dev_set_bus) {
584 int err = dev->
adapter->dev_set_bus(dev, 0);
601 netif_wake_queue(dev->
netdev);
606 kfree(urb->transfer_buffer);
624 if (!dev->
adapter->dev_restart_async) {
632 netdev_err(dev->
netdev,
"no memory left for urb\n");
639 netdev_err(dev->
netdev,
"no memory left for async cmd\n");
645 err = dev->
adapter->dev_restart_async(dev, urb, buf);
669 err = peak_usb_restart(dev);
671 netdev_err(netdev,
"couldn't start device (err %d)\n",
685 static int peak_usb_set_bittiming(
struct net_device *netdev)
690 if (dev->
adapter->dev_set_bittiming) {
691 int err = dev->
adapter->dev_set_bittiming(dev, bt);
694 netdev_info(netdev,
"couldn't set bitrate (err %d)\n",
703 .ndo_open = peak_usb_ndo_open,
704 .ndo_stop = peak_usb_ndo_stop,
705 .ndo_start_xmit = peak_usb_ndo_start_xmit,
715 struct usb_device *usb_dev = interface_to_usbdev(intf);
725 netdev =
alloc_candev(sizeof_candev, PCAN_USB_MAX_TX_URBS);
727 dev_err(&intf->dev,
"%s: couldn't alloc candev\n",
732 dev = netdev_priv(netdev);
737 dev_err(&intf->dev,
"%s: couldn't alloc cmd buffer\n",
740 goto lbl_set_intf_data;
745 dev->
adapter = peak_usb_adapter;
752 dev->
can.clock = peak_usb_adapter->
clock;
754 dev->
can.do_set_bittiming = peak_usb_set_bittiming;
755 dev->
can.do_set_mode = peak_usb_set_mode;
769 dev->
tx_contexts[i].echo_index = PCAN_USB_MAX_TX_URBS;
772 usb_set_intfdata(intf, dev);
778 dev_err(&intf->dev,
"couldn't register CAN device: %d\n", err);
779 goto lbl_free_cmd_buf;
786 tmp16 =
le16_to_cpu(usb_dev->descriptor.bcdDevice);
790 err = dev->
adapter->dev_init(dev);
792 goto lbl_free_cmd_buf;
796 if (dev->
adapter->dev_set_bus) {
797 err = dev->
adapter->dev_set_bus(dev, 0);
799 goto lbl_free_cmd_buf;
803 if (dev->
adapter->dev_get_device_id)
806 netdev_info(netdev,
"attached to %s channel %u (device %u)\n",
844 dev_info(&intf->dev,
"%s removed\n", name);
847 usb_set_intfdata(intf,
NULL);
856 struct usb_device *usb_dev = interface_to_usbdev(intf);
857 struct peak_usb_adapter *peak_usb_adapter, **
pp;
860 usb_dev = interface_to_usbdev(intf);
863 for (pp = peak_usb_adapters_list; *
pp; pp++)
864 if ((*pp)->device_id == usb_dev->descriptor.idProduct)
867 peak_usb_adapter = *
pp;
868 if (!peak_usb_adapter) {
870 pr_err(
"%s: didn't find device id. 0x%x in devices list\n",
882 for (i = 0; i < peak_usb_adapter->
ctrl_count; i++) {
883 err = peak_usb_create_dev(peak_usb_adapter, intf, i);
886 peak_usb_disconnect(intf);
895 static struct usb_driver peak_usb_driver = {
897 .disconnect = peak_usb_disconnect,
898 .probe = peak_usb_probe,
899 .id_table = peak_usb_table,
902 static int __init peak_usb_init(
void)
907 err = usb_register(&peak_usb_driver);
909 pr_err(
"%s: usb_register failed (err %d)\n",
915 static int peak_usb_do_device_exit(
struct device *
d,
void *
arg)
924 if (netif_device_present(netdev))
932 static void __exit peak_usb_exit(
void)
938 NULL, peak_usb_do_device_exit);
940 pr_err(
"%s: failed to stop all can devices (err %d)\n",
946 pr_info(
"%s: PCAN-USB interfaces driver unloaded\n",