gs_usb.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /* CAN driver for Geschwister Schneider USB/CAN devices
  2. * and bytewerk.org candleLight USB CAN interfaces.
  3. *
  4. * Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
  5. * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
  6. * Copyright (C) 2016 Hubert Denkmair
  7. *
  8. * Many thanks to all socketcan devs!
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published
  12. * by the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/signal.h>
  21. #include <linux/module.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/usb.h>
  24. #include <linux/can.h>
  25. #include <linux/can/dev.h>
  26. #include <linux/can/error.h>
  27. /* Device specific constants */
  28. #define USB_GSUSB_1_VENDOR_ID 0x1d50
  29. #define USB_GSUSB_1_PRODUCT_ID 0x606f
  30. #define USB_CANDLELIGHT_VENDOR_ID 0x1209
  31. #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
  32. #define GSUSB_ENDPOINT_IN 1
  33. #define GSUSB_ENDPOINT_OUT 2
  34. /* Device specific constants */
  35. enum gs_usb_breq {
  36. GS_USB_BREQ_HOST_FORMAT = 0,
  37. GS_USB_BREQ_BITTIMING,
  38. GS_USB_BREQ_MODE,
  39. GS_USB_BREQ_BERR,
  40. GS_USB_BREQ_BT_CONST,
  41. GS_USB_BREQ_DEVICE_CONFIG,
  42. GS_USB_BREQ_TIMESTAMP,
  43. GS_USB_BREQ_IDENTIFY,
  44. };
  45. enum gs_can_mode {
  46. /* reset a channel. turns it off */
  47. GS_CAN_MODE_RESET = 0,
  48. /* starts a channel */
  49. GS_CAN_MODE_START
  50. };
  51. enum gs_can_state {
  52. GS_CAN_STATE_ERROR_ACTIVE = 0,
  53. GS_CAN_STATE_ERROR_WARNING,
  54. GS_CAN_STATE_ERROR_PASSIVE,
  55. GS_CAN_STATE_BUS_OFF,
  56. GS_CAN_STATE_STOPPED,
  57. GS_CAN_STATE_SLEEPING
  58. };
  59. enum gs_can_identify_mode {
  60. GS_CAN_IDENTIFY_OFF = 0,
  61. GS_CAN_IDENTIFY_ON
  62. };
  63. /* data types passed between host and device */
  64. /* The firmware on the original USB2CAN by Geschwister Schneider
  65. * Technologie Entwicklungs- und Vertriebs UG exchanges all data
  66. * between the host and the device in host byte order. This is done
  67. * with the struct gs_host_config::byte_order member, which is sent
  68. * first to indicate the desired byte order.
  69. *
  70. * The widely used open source firmware candleLight doesn't support
  71. * this feature and exchanges the data in little endian byte order.
  72. */
  73. struct gs_host_config {
  74. __le32 byte_order;
  75. } __packed;
  76. struct gs_device_config {
  77. u8 reserved1;
  78. u8 reserved2;
  79. u8 reserved3;
  80. u8 icount;
  81. __le32 sw_version;
  82. __le32 hw_version;
  83. } __packed;
  84. #define GS_CAN_MODE_NORMAL 0
  85. #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
  86. #define GS_CAN_MODE_LOOP_BACK BIT(1)
  87. #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
  88. #define GS_CAN_MODE_ONE_SHOT BIT(3)
  89. struct gs_device_mode {
  90. __le32 mode;
  91. __le32 flags;
  92. } __packed;
  93. struct gs_device_state {
  94. __le32 state;
  95. __le32 rxerr;
  96. __le32 txerr;
  97. } __packed;
  98. struct gs_device_bittiming {
  99. __le32 prop_seg;
  100. __le32 phase_seg1;
  101. __le32 phase_seg2;
  102. __le32 sjw;
  103. __le32 brp;
  104. } __packed;
  105. struct gs_identify_mode {
  106. __le32 mode;
  107. } __packed;
  108. #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
  109. #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
  110. #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
  111. #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
  112. #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
  113. #define GS_CAN_FEATURE_IDENTIFY BIT(5)
  114. struct gs_device_bt_const {
  115. __le32 feature;
  116. __le32 fclk_can;
  117. __le32 tseg1_min;
  118. __le32 tseg1_max;
  119. __le32 tseg2_min;
  120. __le32 tseg2_max;
  121. __le32 sjw_max;
  122. __le32 brp_min;
  123. __le32 brp_max;
  124. __le32 brp_inc;
  125. } __packed;
  126. #define GS_CAN_FLAG_OVERFLOW 1
  127. struct gs_host_frame {
  128. u32 echo_id;
  129. __le32 can_id;
  130. u8 can_dlc;
  131. u8 channel;
  132. u8 flags;
  133. u8 reserved;
  134. u8 data[8];
  135. } __packed;
  136. /* The GS USB devices make use of the same flags and masks as in
  137. * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
  138. */
  139. /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
  140. #define GS_MAX_TX_URBS 10
  141. /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
  142. #define GS_MAX_RX_URBS 30
  143. /* Maximum number of interfaces the driver supports per device.
  144. * Current hardware only supports 2 interfaces. The future may vary.
  145. */
  146. #define GS_MAX_INTF 2
  147. struct gs_tx_context {
  148. struct gs_can *dev;
  149. unsigned int echo_id;
  150. };
  151. struct gs_can {
  152. struct can_priv can; /* must be the first member */
  153. struct gs_usb *parent;
  154. struct net_device *netdev;
  155. struct usb_device *udev;
  156. struct usb_interface *iface;
  157. struct can_bittiming_const bt_const;
  158. unsigned int channel; /* channel number */
  159. /* This lock prevents a race condition between xmit and receive. */
  160. spinlock_t tx_ctx_lock;
  161. struct gs_tx_context tx_context[GS_MAX_TX_URBS];
  162. struct usb_anchor tx_submitted;
  163. atomic_t active_tx_urbs;
  164. };
  165. /* usb interface struct */
  166. struct gs_usb {
  167. struct gs_can *canch[GS_MAX_INTF];
  168. struct usb_anchor rx_submitted;
  169. atomic_t active_channels;
  170. struct usb_device *udev;
  171. };
  172. /* 'allocate' a tx context.
  173. * returns a valid tx context or NULL if there is no space.
  174. */
  175. static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
  176. {
  177. int i = 0;
  178. unsigned long flags;
  179. spin_lock_irqsave(&dev->tx_ctx_lock, flags);
  180. for (; i < GS_MAX_TX_URBS; i++) {
  181. if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
  182. dev->tx_context[i].echo_id = i;
  183. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  184. return &dev->tx_context[i];
  185. }
  186. }
  187. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  188. return NULL;
  189. }
  190. /* releases a tx context
  191. */
  192. static void gs_free_tx_context(struct gs_tx_context *txc)
  193. {
  194. txc->echo_id = GS_MAX_TX_URBS;
  195. }
  196. /* Get a tx context by id.
  197. */
  198. static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
  199. unsigned int id)
  200. {
  201. unsigned long flags;
  202. if (id < GS_MAX_TX_URBS) {
  203. spin_lock_irqsave(&dev->tx_ctx_lock, flags);
  204. if (dev->tx_context[id].echo_id == id) {
  205. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  206. return &dev->tx_context[id];
  207. }
  208. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  209. }
  210. return NULL;
  211. }
  212. static int gs_cmd_reset(struct gs_can *gsdev)
  213. {
  214. struct gs_device_mode *dm;
  215. struct usb_interface *intf = gsdev->iface;
  216. int rc;
  217. dm = kzalloc(sizeof(*dm), GFP_KERNEL);
  218. if (!dm)
  219. return -ENOMEM;
  220. dm->mode = GS_CAN_MODE_RESET;
  221. rc = usb_control_msg(interface_to_usbdev(intf),
  222. usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  223. GS_USB_BREQ_MODE,
  224. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  225. gsdev->channel,
  226. 0,
  227. dm,
  228. sizeof(*dm),
  229. 1000);
  230. kfree(dm);
  231. return rc;
  232. }
  233. static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
  234. {
  235. struct can_device_stats *can_stats = &dev->can.can_stats;
  236. if (cf->can_id & CAN_ERR_RESTARTED) {
  237. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  238. can_stats->restarts++;
  239. } else if (cf->can_id & CAN_ERR_BUSOFF) {
  240. dev->can.state = CAN_STATE_BUS_OFF;
  241. can_stats->bus_off++;
  242. } else if (cf->can_id & CAN_ERR_CRTL) {
  243. if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
  244. (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
  245. dev->can.state = CAN_STATE_ERROR_WARNING;
  246. can_stats->error_warning++;
  247. } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
  248. (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
  249. dev->can.state = CAN_STATE_ERROR_PASSIVE;
  250. can_stats->error_passive++;
  251. } else {
  252. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  253. }
  254. }
  255. }
  256. static void gs_usb_receive_bulk_callback(struct urb *urb)
  257. {
  258. struct gs_usb *usbcan = urb->context;
  259. struct gs_can *dev;
  260. struct net_device *netdev;
  261. int rc;
  262. struct net_device_stats *stats;
  263. struct gs_host_frame *hf = urb->transfer_buffer;
  264. struct gs_tx_context *txc;
  265. struct can_frame *cf;
  266. struct sk_buff *skb;
  267. BUG_ON(!usbcan);
  268. switch (urb->status) {
  269. case 0: /* success */
  270. break;
  271. case -ENOENT:
  272. case -ESHUTDOWN:
  273. return;
  274. default:
  275. /* do not resubmit aborted urbs. eg: when device goes down */
  276. return;
  277. }
  278. /* device reports out of range channel id */
  279. if (hf->channel >= GS_MAX_INTF)
  280. goto resubmit_urb;
  281. dev = usbcan->canch[hf->channel];
  282. netdev = dev->netdev;
  283. stats = &netdev->stats;
  284. if (!netif_device_present(netdev))
  285. return;
  286. if (hf->echo_id == -1) { /* normal rx */
  287. skb = alloc_can_skb(dev->netdev, &cf);
  288. if (!skb)
  289. return;
  290. cf->can_id = le32_to_cpu(hf->can_id);
  291. cf->can_dlc = get_can_dlc(hf->can_dlc);
  292. memcpy(cf->data, hf->data, 8);
  293. /* ERROR frames tell us information about the controller */
  294. if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
  295. gs_update_state(dev, cf);
  296. netdev->stats.rx_packets++;
  297. netdev->stats.rx_bytes += hf->can_dlc;
  298. netif_rx(skb);
  299. } else { /* echo_id == hf->echo_id */
  300. if (hf->echo_id >= GS_MAX_TX_URBS) {
  301. netdev_err(netdev,
  302. "Unexpected out of range echo id %d\n",
  303. hf->echo_id);
  304. goto resubmit_urb;
  305. }
  306. netdev->stats.tx_packets++;
  307. netdev->stats.tx_bytes += hf->can_dlc;
  308. txc = gs_get_tx_context(dev, hf->echo_id);
  309. /* bad devices send bad echo_ids. */
  310. if (!txc) {
  311. netdev_err(netdev,
  312. "Unexpected unused echo id %d\n",
  313. hf->echo_id);
  314. goto resubmit_urb;
  315. }
  316. can_get_echo_skb(netdev, hf->echo_id);
  317. gs_free_tx_context(txc);
  318. atomic_dec(&dev->active_tx_urbs);
  319. netif_wake_queue(netdev);
  320. }
  321. if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
  322. skb = alloc_can_err_skb(netdev, &cf);
  323. if (!skb)
  324. goto resubmit_urb;
  325. cf->can_id |= CAN_ERR_CRTL;
  326. cf->can_dlc = CAN_ERR_DLC;
  327. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  328. stats->rx_over_errors++;
  329. stats->rx_errors++;
  330. netif_rx(skb);
  331. }
  332. resubmit_urb:
  333. usb_fill_bulk_urb(urb,
  334. usbcan->udev,
  335. usb_rcvbulkpipe(usbcan->udev, GSUSB_ENDPOINT_IN),
  336. hf,
  337. sizeof(struct gs_host_frame),
  338. gs_usb_receive_bulk_callback,
  339. usbcan
  340. );
  341. rc = usb_submit_urb(urb, GFP_ATOMIC);
  342. /* USB failure take down all interfaces */
  343. if (rc == -ENODEV) {
  344. for (rc = 0; rc < GS_MAX_INTF; rc++) {
  345. if (usbcan->canch[rc])
  346. netif_device_detach(usbcan->canch[rc]->netdev);
  347. }
  348. }
  349. }
  350. static int gs_usb_set_bittiming(struct net_device *netdev)
  351. {
  352. struct gs_can *dev = netdev_priv(netdev);
  353. struct can_bittiming *bt = &dev->can.bittiming;
  354. struct usb_interface *intf = dev->iface;
  355. int rc;
  356. struct gs_device_bittiming *dbt;
  357. dbt = kmalloc(sizeof(*dbt), GFP_KERNEL);
  358. if (!dbt)
  359. return -ENOMEM;
  360. dbt->prop_seg = cpu_to_le32(bt->prop_seg);
  361. dbt->phase_seg1 = cpu_to_le32(bt->phase_seg1);
  362. dbt->phase_seg2 = cpu_to_le32(bt->phase_seg2);
  363. dbt->sjw = cpu_to_le32(bt->sjw);
  364. dbt->brp = cpu_to_le32(bt->brp);
  365. /* request bit timings */
  366. rc = usb_control_msg(interface_to_usbdev(intf),
  367. usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  368. GS_USB_BREQ_BITTIMING,
  369. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  370. dev->channel,
  371. 0,
  372. dbt,
  373. sizeof(*dbt),
  374. 1000);
  375. kfree(dbt);
  376. if (rc < 0)
  377. dev_err(netdev->dev.parent, "Couldn't set bittimings (err=%d)",
  378. rc);
  379. return (rc > 0) ? 0 : rc;
  380. }
  381. static void gs_usb_xmit_callback(struct urb *urb)
  382. {
  383. struct gs_tx_context *txc = urb->context;
  384. struct gs_can *dev = txc->dev;
  385. struct net_device *netdev = dev->netdev;
  386. if (urb->status)
  387. netdev_info(netdev, "usb xmit fail %d\n", txc->echo_id);
  388. usb_free_coherent(urb->dev,
  389. urb->transfer_buffer_length,
  390. urb->transfer_buffer,
  391. urb->transfer_dma);
  392. }
  393. static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
  394. struct net_device *netdev)
  395. {
  396. struct gs_can *dev = netdev_priv(netdev);
  397. struct net_device_stats *stats = &dev->netdev->stats;
  398. struct urb *urb;
  399. struct gs_host_frame *hf;
  400. struct can_frame *cf;
  401. int rc;
  402. unsigned int idx;
  403. struct gs_tx_context *txc;
  404. if (can_dropped_invalid_skb(netdev, skb))
  405. return NETDEV_TX_OK;
  406. /* find an empty context to keep track of transmission */
  407. txc = gs_alloc_tx_context(dev);
  408. if (!txc)
  409. return NETDEV_TX_BUSY;
  410. /* create a URB, and a buffer for it */
  411. urb = usb_alloc_urb(0, GFP_ATOMIC);
  412. if (!urb)
  413. goto nomem_urb;
  414. hf = usb_alloc_coherent(dev->udev, sizeof(*hf), GFP_ATOMIC,
  415. &urb->transfer_dma);
  416. if (!hf) {
  417. netdev_err(netdev, "No memory left for USB buffer\n");
  418. goto nomem_hf;
  419. }
  420. idx = txc->echo_id;
  421. if (idx >= GS_MAX_TX_URBS) {
  422. netdev_err(netdev, "Invalid tx context %d\n", idx);
  423. goto badidx;
  424. }
  425. hf->echo_id = idx;
  426. hf->channel = dev->channel;
  427. cf = (struct can_frame *)skb->data;
  428. hf->can_id = cpu_to_le32(cf->can_id);
  429. hf->can_dlc = cf->can_dlc;
  430. memcpy(hf->data, cf->data, cf->can_dlc);
  431. usb_fill_bulk_urb(urb, dev->udev,
  432. usb_sndbulkpipe(dev->udev, GSUSB_ENDPOINT_OUT),
  433. hf,
  434. sizeof(*hf),
  435. gs_usb_xmit_callback,
  436. txc);
  437. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  438. usb_anchor_urb(urb, &dev->tx_submitted);
  439. can_put_echo_skb(skb, netdev, idx);
  440. atomic_inc(&dev->active_tx_urbs);
  441. rc = usb_submit_urb(urb, GFP_ATOMIC);
  442. if (unlikely(rc)) { /* usb send failed */
  443. atomic_dec(&dev->active_tx_urbs);
  444. can_free_echo_skb(netdev, idx);
  445. gs_free_tx_context(txc);
  446. usb_unanchor_urb(urb);
  447. usb_free_coherent(dev->udev,
  448. sizeof(*hf),
  449. hf,
  450. urb->transfer_dma);
  451. if (rc == -ENODEV) {
  452. netif_device_detach(netdev);
  453. } else {
  454. netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
  455. stats->tx_dropped++;
  456. }
  457. } else {
  458. /* Slow down tx path */
  459. if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
  460. netif_stop_queue(netdev);
  461. }
  462. /* let usb core take care of this urb */
  463. usb_free_urb(urb);
  464. return NETDEV_TX_OK;
  465. badidx:
  466. usb_free_coherent(dev->udev,
  467. sizeof(*hf),
  468. hf,
  469. urb->transfer_dma);
  470. nomem_hf:
  471. usb_free_urb(urb);
  472. nomem_urb:
  473. gs_free_tx_context(txc);
  474. dev_kfree_skb(skb);
  475. stats->tx_dropped++;
  476. return NETDEV_TX_OK;
  477. }
  478. static int gs_can_open(struct net_device *netdev)
  479. {
  480. struct gs_can *dev = netdev_priv(netdev);
  481. struct gs_usb *parent = dev->parent;
  482. int rc, i;
  483. struct gs_device_mode *dm;
  484. u32 ctrlmode;
  485. u32 flags = 0;
  486. rc = open_candev(netdev);
  487. if (rc)
  488. return rc;
  489. if (atomic_add_return(1, &parent->active_channels) == 1) {
  490. for (i = 0; i < GS_MAX_RX_URBS; i++) {
  491. struct urb *urb;
  492. u8 *buf;
  493. /* alloc rx urb */
  494. urb = usb_alloc_urb(0, GFP_KERNEL);
  495. if (!urb)
  496. return -ENOMEM;
  497. /* alloc rx buffer */
  498. buf = usb_alloc_coherent(dev->udev,
  499. sizeof(struct gs_host_frame),
  500. GFP_KERNEL,
  501. &urb->transfer_dma);
  502. if (!buf) {
  503. netdev_err(netdev,
  504. "No memory left for USB buffer\n");
  505. usb_free_urb(urb);
  506. return -ENOMEM;
  507. }
  508. /* fill, anchor, and submit rx urb */
  509. usb_fill_bulk_urb(urb,
  510. dev->udev,
  511. usb_rcvbulkpipe(dev->udev,
  512. GSUSB_ENDPOINT_IN),
  513. buf,
  514. sizeof(struct gs_host_frame),
  515. gs_usb_receive_bulk_callback,
  516. parent);
  517. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  518. usb_anchor_urb(urb, &parent->rx_submitted);
  519. rc = usb_submit_urb(urb, GFP_KERNEL);
  520. if (rc) {
  521. if (rc == -ENODEV)
  522. netif_device_detach(dev->netdev);
  523. netdev_err(netdev,
  524. "usb_submit failed (err=%d)\n",
  525. rc);
  526. usb_unanchor_urb(urb);
  527. usb_free_urb(urb);
  528. break;
  529. }
  530. /* Drop reference,
  531. * USB core will take care of freeing it
  532. */
  533. usb_free_urb(urb);
  534. }
  535. }
  536. dm = kmalloc(sizeof(*dm), GFP_KERNEL);
  537. if (!dm)
  538. return -ENOMEM;
  539. /* flags */
  540. ctrlmode = dev->can.ctrlmode;
  541. if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
  542. flags |= GS_CAN_MODE_LOOP_BACK;
  543. else if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
  544. flags |= GS_CAN_MODE_LISTEN_ONLY;
  545. /* Controller is not allowed to retry TX
  546. * this mode is unavailable on atmels uc3c hardware
  547. */
  548. if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  549. flags |= GS_CAN_MODE_ONE_SHOT;
  550. if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  551. flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
  552. /* finally start device */
  553. dm->mode = cpu_to_le32(GS_CAN_MODE_START);
  554. dm->flags = cpu_to_le32(flags);
  555. rc = usb_control_msg(interface_to_usbdev(dev->iface),
  556. usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0),
  557. GS_USB_BREQ_MODE,
  558. USB_DIR_OUT | USB_TYPE_VENDOR |
  559. USB_RECIP_INTERFACE,
  560. dev->channel,
  561. 0,
  562. dm,
  563. sizeof(*dm),
  564. 1000);
  565. if (rc < 0) {
  566. netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
  567. kfree(dm);
  568. return rc;
  569. }
  570. kfree(dm);
  571. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  572. if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
  573. netif_start_queue(netdev);
  574. return 0;
  575. }
  576. static int gs_can_close(struct net_device *netdev)
  577. {
  578. int rc;
  579. struct gs_can *dev = netdev_priv(netdev);
  580. struct gs_usb *parent = dev->parent;
  581. netif_stop_queue(netdev);
  582. /* Stop polling */
  583. if (atomic_dec_and_test(&parent->active_channels))
  584. usb_kill_anchored_urbs(&parent->rx_submitted);
  585. /* Stop sending URBs */
  586. usb_kill_anchored_urbs(&dev->tx_submitted);
  587. atomic_set(&dev->active_tx_urbs, 0);
  588. /* reset the device */
  589. rc = gs_cmd_reset(dev);
  590. if (rc < 0)
  591. netdev_warn(netdev, "Couldn't shutdown device (err=%d)", rc);
  592. /* reset tx contexts */
  593. for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
  594. dev->tx_context[rc].dev = dev;
  595. dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
  596. }
  597. /* close the netdev */
  598. close_candev(netdev);
  599. return 0;
  600. }
  601. static const struct net_device_ops gs_usb_netdev_ops = {
  602. .ndo_open = gs_can_open,
  603. .ndo_stop = gs_can_close,
  604. .ndo_start_xmit = gs_can_start_xmit,
  605. .ndo_change_mtu = can_change_mtu,
  606. };
  607. static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
  608. {
  609. struct gs_can *dev = netdev_priv(netdev);
  610. struct gs_identify_mode *imode;
  611. int rc;
  612. imode = kmalloc(sizeof(*imode), GFP_KERNEL);
  613. if (!imode)
  614. return -ENOMEM;
  615. if (do_identify)
  616. imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
  617. else
  618. imode->mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
  619. rc = usb_control_msg(interface_to_usbdev(dev->iface),
  620. usb_sndctrlpipe(interface_to_usbdev(dev->iface),
  621. 0),
  622. GS_USB_BREQ_IDENTIFY,
  623. USB_DIR_OUT | USB_TYPE_VENDOR |
  624. USB_RECIP_INTERFACE,
  625. dev->channel,
  626. 0,
  627. imode,
  628. sizeof(*imode),
  629. 100);
  630. kfree(imode);
  631. return (rc > 0) ? 0 : rc;
  632. }
  633. /* blink LED's for finding the this interface */
  634. static int gs_usb_set_phys_id(struct net_device *dev,
  635. enum ethtool_phys_id_state state)
  636. {
  637. int rc = 0;
  638. switch (state) {
  639. case ETHTOOL_ID_ACTIVE:
  640. rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON);
  641. break;
  642. case ETHTOOL_ID_INACTIVE:
  643. rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF);
  644. break;
  645. default:
  646. break;
  647. }
  648. return rc;
  649. }
  650. static const struct ethtool_ops gs_usb_ethtool_ops = {
  651. .set_phys_id = gs_usb_set_phys_id,
  652. };
  653. static struct gs_can *gs_make_candev(unsigned int channel,
  654. struct usb_interface *intf,
  655. struct gs_device_config *dconf)
  656. {
  657. struct gs_can *dev;
  658. struct net_device *netdev;
  659. int rc;
  660. struct gs_device_bt_const *bt_const;
  661. u32 feature;
  662. bt_const = kmalloc(sizeof(*bt_const), GFP_KERNEL);
  663. if (!bt_const)
  664. return ERR_PTR(-ENOMEM);
  665. /* fetch bit timing constants */
  666. rc = usb_control_msg(interface_to_usbdev(intf),
  667. usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
  668. GS_USB_BREQ_BT_CONST,
  669. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  670. channel,
  671. 0,
  672. bt_const,
  673. sizeof(*bt_const),
  674. 1000);
  675. if (rc < 0) {
  676. dev_err(&intf->dev,
  677. "Couldn't get bit timing const for channel (err=%d)\n",
  678. rc);
  679. kfree(bt_const);
  680. return ERR_PTR(rc);
  681. }
  682. /* create netdev */
  683. netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
  684. if (!netdev) {
  685. dev_err(&intf->dev, "Couldn't allocate candev\n");
  686. kfree(bt_const);
  687. return ERR_PTR(-ENOMEM);
  688. }
  689. dev = netdev_priv(netdev);
  690. netdev->netdev_ops = &gs_usb_netdev_ops;
  691. netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
  692. /* dev settup */
  693. strcpy(dev->bt_const.name, "gs_usb");
  694. dev->bt_const.tseg1_min = le32_to_cpu(bt_const->tseg1_min);
  695. dev->bt_const.tseg1_max = le32_to_cpu(bt_const->tseg1_max);
  696. dev->bt_const.tseg2_min = le32_to_cpu(bt_const->tseg2_min);
  697. dev->bt_const.tseg2_max = le32_to_cpu(bt_const->tseg2_max);
  698. dev->bt_const.sjw_max = le32_to_cpu(bt_const->sjw_max);
  699. dev->bt_const.brp_min = le32_to_cpu(bt_const->brp_min);
  700. dev->bt_const.brp_max = le32_to_cpu(bt_const->brp_max);
  701. dev->bt_const.brp_inc = le32_to_cpu(bt_const->brp_inc);
  702. dev->udev = interface_to_usbdev(intf);
  703. dev->iface = intf;
  704. dev->netdev = netdev;
  705. dev->channel = channel;
  706. init_usb_anchor(&dev->tx_submitted);
  707. atomic_set(&dev->active_tx_urbs, 0);
  708. spin_lock_init(&dev->tx_ctx_lock);
  709. for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
  710. dev->tx_context[rc].dev = dev;
  711. dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
  712. }
  713. /* can settup */
  714. dev->can.state = CAN_STATE_STOPPED;
  715. dev->can.clock.freq = le32_to_cpu(bt_const->fclk_can);
  716. dev->can.bittiming_const = &dev->bt_const;
  717. dev->can.do_set_bittiming = gs_usb_set_bittiming;
  718. dev->can.ctrlmode_supported = 0;
  719. feature = le32_to_cpu(bt_const->feature);
  720. if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
  721. dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
  722. if (feature & GS_CAN_FEATURE_LOOP_BACK)
  723. dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
  724. if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
  725. dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  726. if (feature & GS_CAN_FEATURE_ONE_SHOT)
  727. dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
  728. SET_NETDEV_DEV(netdev, &intf->dev);
  729. if (le32_to_cpu(dconf->sw_version) > 1)
  730. if (feature & GS_CAN_FEATURE_IDENTIFY)
  731. netdev->ethtool_ops = &gs_usb_ethtool_ops;
  732. kfree(bt_const);
  733. rc = register_candev(dev->netdev);
  734. if (rc) {
  735. free_candev(dev->netdev);
  736. dev_err(&intf->dev, "Couldn't register candev (err=%d)\n", rc);
  737. return ERR_PTR(rc);
  738. }
  739. return dev;
  740. }
  741. static void gs_destroy_candev(struct gs_can *dev)
  742. {
  743. unregister_candev(dev->netdev);
  744. usb_kill_anchored_urbs(&dev->tx_submitted);
  745. free_candev(dev->netdev);
  746. }
  747. static int gs_usb_probe(struct usb_interface *intf,
  748. const struct usb_device_id *id)
  749. {
  750. struct gs_usb *dev;
  751. int rc = -ENOMEM;
  752. unsigned int icount, i;
  753. struct gs_host_config *hconf;
  754. struct gs_device_config *dconf;
  755. hconf = kmalloc(sizeof(*hconf), GFP_KERNEL);
  756. if (!hconf)
  757. return -ENOMEM;
  758. hconf->byte_order = cpu_to_le32(0x0000beef);
  759. /* send host config */
  760. rc = usb_control_msg(interface_to_usbdev(intf),
  761. usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  762. GS_USB_BREQ_HOST_FORMAT,
  763. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  764. 1,
  765. intf->cur_altsetting->desc.bInterfaceNumber,
  766. hconf,
  767. sizeof(*hconf),
  768. 1000);
  769. kfree(hconf);
  770. if (rc < 0) {
  771. dev_err(&intf->dev, "Couldn't send data format (err=%d)\n",
  772. rc);
  773. return rc;
  774. }
  775. dconf = kmalloc(sizeof(*dconf), GFP_KERNEL);
  776. if (!dconf)
  777. return -ENOMEM;
  778. /* read device config */
  779. rc = usb_control_msg(interface_to_usbdev(intf),
  780. usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
  781. GS_USB_BREQ_DEVICE_CONFIG,
  782. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  783. 1,
  784. intf->cur_altsetting->desc.bInterfaceNumber,
  785. dconf,
  786. sizeof(*dconf),
  787. 1000);
  788. if (rc < 0) {
  789. dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
  790. rc);
  791. kfree(dconf);
  792. return rc;
  793. }
  794. icount = dconf->icount + 1;
  795. dev_info(&intf->dev, "Configuring for %d interfaces\n", icount);
  796. if (icount > GS_MAX_INTF) {
  797. dev_err(&intf->dev,
  798. "Driver cannot handle more that %d CAN interfaces\n",
  799. GS_MAX_INTF);
  800. kfree(dconf);
  801. return -EINVAL;
  802. }
  803. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  804. if (!dev) {
  805. kfree(dconf);
  806. return -ENOMEM;
  807. }
  808. init_usb_anchor(&dev->rx_submitted);
  809. atomic_set(&dev->active_channels, 0);
  810. usb_set_intfdata(intf, dev);
  811. dev->udev = interface_to_usbdev(intf);
  812. for (i = 0; i < icount; i++) {
  813. dev->canch[i] = gs_make_candev(i, intf, dconf);
  814. if (IS_ERR_OR_NULL(dev->canch[i])) {
  815. /* save error code to return later */
  816. rc = PTR_ERR(dev->canch[i]);
  817. /* on failure destroy previously created candevs */
  818. icount = i;
  819. for (i = 0; i < icount; i++)
  820. gs_destroy_candev(dev->canch[i]);
  821. usb_kill_anchored_urbs(&dev->rx_submitted);
  822. kfree(dconf);
  823. kfree(dev);
  824. return rc;
  825. }
  826. dev->canch[i]->parent = dev;
  827. }
  828. kfree(dconf);
  829. return 0;
  830. }
  831. static void gs_usb_disconnect(struct usb_interface *intf)
  832. {
  833. unsigned i;
  834. struct gs_usb *dev = usb_get_intfdata(intf);
  835. usb_set_intfdata(intf, NULL);
  836. if (!dev) {
  837. dev_err(&intf->dev, "Disconnect (nodata)\n");
  838. return;
  839. }
  840. for (i = 0; i < GS_MAX_INTF; i++)
  841. if (dev->canch[i])
  842. gs_destroy_candev(dev->canch[i]);
  843. usb_kill_anchored_urbs(&dev->rx_submitted);
  844. kfree(dev);
  845. }
  846. static const struct usb_device_id gs_usb_table[] = {
  847. { USB_DEVICE_INTERFACE_NUMBER(USB_GSUSB_1_VENDOR_ID,
  848. USB_GSUSB_1_PRODUCT_ID, 0) },
  849. { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
  850. USB_CANDLELIGHT_PRODUCT_ID, 0) },
  851. {} /* Terminating entry */
  852. };
  853. MODULE_DEVICE_TABLE(usb, gs_usb_table);
  854. static struct usb_driver gs_usb_driver = {
  855. .name = "gs_usb",
  856. .probe = gs_usb_probe,
  857. .disconnect = gs_usb_disconnect,
  858. .id_table = gs_usb_table,
  859. };
  860. module_usb_driver(gs_usb_driver);
  861. MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
  862. MODULE_DESCRIPTION(
  863. "Socket CAN device driver for Geschwister Schneider Technologie-, "
  864. "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
  865. "and bytewerk.org candleLight USB CAN interfaces.");
  866. MODULE_LICENSE("GPL v2");