gs_usb.c 24 KB

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