gs_usb.c 22 KB

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