gs_usb.c 24 KB

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