gs_usb.c 22 KB

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