mcba_usb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /* SocketCAN driver for Microchip CAN BUS Analyzer Tool
  2. *
  3. * Copyright (C) 2017 Mobica Limited
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program.
  16. *
  17. * This driver is inspired by the 4.6.2 version of net/can/usb/usb_8dev.c
  18. */
  19. #include <asm/unaligned.h>
  20. #include <linux/can.h>
  21. #include <linux/can/dev.h>
  22. #include <linux/can/error.h>
  23. #include <linux/can/led.h>
  24. #include <linux/module.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/signal.h>
  27. #include <linux/slab.h>
  28. #include <linux/usb.h>
  29. /* vendor and product id */
  30. #define MCBA_MODULE_NAME "mcba_usb"
  31. #define MCBA_VENDOR_ID 0x04d8
  32. #define MCBA_PRODUCT_ID 0x0a30
  33. /* driver constants */
  34. #define MCBA_MAX_RX_URBS 20
  35. #define MCBA_MAX_TX_URBS 20
  36. #define MCBA_CTX_FREE MCBA_MAX_TX_URBS
  37. /* RX buffer must be bigger than msg size since at the
  38. * beggining USB messages are stacked.
  39. */
  40. #define MCBA_USB_RX_BUFF_SIZE 64
  41. #define MCBA_USB_TX_BUFF_SIZE (sizeof(struct mcba_usb_msg))
  42. /* MCBA endpoint numbers */
  43. #define MCBA_USB_EP_IN 1
  44. #define MCBA_USB_EP_OUT 1
  45. /* Microchip command id */
  46. #define MBCA_CMD_RECEIVE_MESSAGE 0xE3
  47. #define MBCA_CMD_I_AM_ALIVE_FROM_CAN 0xF5
  48. #define MBCA_CMD_I_AM_ALIVE_FROM_USB 0xF7
  49. #define MBCA_CMD_CHANGE_BIT_RATE 0xA1
  50. #define MBCA_CMD_TRANSMIT_MESSAGE_EV 0xA3
  51. #define MBCA_CMD_SETUP_TERMINATION_RESISTANCE 0xA8
  52. #define MBCA_CMD_READ_FW_VERSION 0xA9
  53. #define MBCA_CMD_NOTHING_TO_SEND 0xFF
  54. #define MBCA_CMD_TRANSMIT_MESSAGE_RSP 0xE2
  55. #define MCBA_VER_REQ_USB 1
  56. #define MCBA_VER_REQ_CAN 2
  57. #define MCBA_SIDL_EXID_MASK 0x8
  58. #define MCBA_DLC_MASK 0xf
  59. #define MCBA_DLC_RTR_MASK 0x40
  60. #define MCBA_CAN_STATE_WRN_TH 95
  61. #define MCBA_CAN_STATE_ERR_PSV_TH 127
  62. #define MCBA_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
  63. #define MCBA_TERMINATION_ENABLED 120
  64. struct mcba_usb_ctx {
  65. struct mcba_priv *priv;
  66. u32 ndx;
  67. u8 dlc;
  68. bool can;
  69. };
  70. /* Structure to hold all of our device specific stuff */
  71. struct mcba_priv {
  72. struct can_priv can; /* must be the first member */
  73. struct sk_buff *echo_skb[MCBA_MAX_TX_URBS];
  74. struct mcba_usb_ctx tx_context[MCBA_MAX_TX_URBS];
  75. struct usb_device *udev;
  76. struct net_device *netdev;
  77. struct usb_anchor tx_submitted;
  78. struct usb_anchor rx_submitted;
  79. struct can_berr_counter bec;
  80. bool usb_ka_first_pass;
  81. bool can_ka_first_pass;
  82. bool can_speed_check;
  83. atomic_t free_ctx_cnt;
  84. };
  85. /* CAN frame */
  86. struct __packed mcba_usb_msg_can {
  87. u8 cmd_id;
  88. __be16 eid;
  89. __be16 sid;
  90. u8 dlc;
  91. u8 data[8];
  92. u8 timestamp[4];
  93. u8 checksum;
  94. };
  95. /* command frame */
  96. struct __packed mcba_usb_msg {
  97. u8 cmd_id;
  98. u8 unused[18];
  99. };
  100. struct __packed mcba_usb_msg_ka_usb {
  101. u8 cmd_id;
  102. u8 termination_state;
  103. u8 soft_ver_major;
  104. u8 soft_ver_minor;
  105. u8 unused[15];
  106. };
  107. struct __packed mcba_usb_msg_ka_can {
  108. u8 cmd_id;
  109. u8 tx_err_cnt;
  110. u8 rx_err_cnt;
  111. u8 rx_buff_ovfl;
  112. u8 tx_bus_off;
  113. __be16 can_bitrate;
  114. __le16 rx_lost;
  115. u8 can_stat;
  116. u8 soft_ver_major;
  117. u8 soft_ver_minor;
  118. u8 debug_mode;
  119. u8 test_complete;
  120. u8 test_result;
  121. u8 unused[4];
  122. };
  123. struct __packed mcba_usb_msg_change_bitrate {
  124. u8 cmd_id;
  125. __be16 bitrate;
  126. u8 unused[16];
  127. };
  128. struct __packed mcba_usb_msg_termination {
  129. u8 cmd_id;
  130. u8 termination;
  131. u8 unused[17];
  132. };
  133. struct __packed mcba_usb_msg_fw_ver {
  134. u8 cmd_id;
  135. u8 pic;
  136. u8 unused[17];
  137. };
  138. static const struct usb_device_id mcba_usb_table[] = {
  139. { USB_DEVICE(MCBA_VENDOR_ID, MCBA_PRODUCT_ID) },
  140. {} /* Terminating entry */
  141. };
  142. MODULE_DEVICE_TABLE(usb, mcba_usb_table);
  143. static const u16 mcba_termination[] = { MCBA_TERMINATION_DISABLED,
  144. MCBA_TERMINATION_ENABLED };
  145. static const u32 mcba_bitrate[] = { 20000, 33333, 50000, 80000, 83333,
  146. 100000, 125000, 150000, 175000, 200000,
  147. 225000, 250000, 275000, 300000, 500000,
  148. 625000, 800000, 1000000 };
  149. static inline void mcba_init_ctx(struct mcba_priv *priv)
  150. {
  151. int i = 0;
  152. for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
  153. priv->tx_context[i].ndx = MCBA_CTX_FREE;
  154. priv->tx_context[i].priv = priv;
  155. }
  156. atomic_set(&priv->free_ctx_cnt, ARRAY_SIZE(priv->tx_context));
  157. }
  158. static inline struct mcba_usb_ctx *mcba_usb_get_free_ctx(struct mcba_priv *priv,
  159. struct can_frame *cf)
  160. {
  161. int i = 0;
  162. struct mcba_usb_ctx *ctx = NULL;
  163. for (i = 0; i < MCBA_MAX_TX_URBS; i++) {
  164. if (priv->tx_context[i].ndx == MCBA_CTX_FREE) {
  165. ctx = &priv->tx_context[i];
  166. ctx->ndx = i;
  167. if (cf) {
  168. ctx->can = true;
  169. ctx->dlc = cf->can_dlc;
  170. } else {
  171. ctx->can = false;
  172. ctx->dlc = 0;
  173. }
  174. atomic_dec(&priv->free_ctx_cnt);
  175. break;
  176. }
  177. }
  178. if (!atomic_read(&priv->free_ctx_cnt))
  179. /* That was the last free ctx. Slow down tx path */
  180. netif_stop_queue(priv->netdev);
  181. return ctx;
  182. }
  183. /* mcba_usb_free_ctx and mcba_usb_get_free_ctx are executed by different
  184. * threads. The order of execution in below function is important.
  185. */
  186. static inline void mcba_usb_free_ctx(struct mcba_usb_ctx *ctx)
  187. {
  188. /* Increase number of free ctxs before freeing ctx */
  189. atomic_inc(&ctx->priv->free_ctx_cnt);
  190. ctx->ndx = MCBA_CTX_FREE;
  191. /* Wake up the queue once ctx is marked free */
  192. netif_wake_queue(ctx->priv->netdev);
  193. }
  194. static void mcba_usb_write_bulk_callback(struct urb *urb)
  195. {
  196. struct mcba_usb_ctx *ctx = urb->context;
  197. struct net_device *netdev;
  198. WARN_ON(!ctx);
  199. netdev = ctx->priv->netdev;
  200. /* free up our allocated buffer */
  201. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  202. urb->transfer_buffer, urb->transfer_dma);
  203. if (ctx->can) {
  204. if (!netif_device_present(netdev))
  205. return;
  206. netdev->stats.tx_packets++;
  207. netdev->stats.tx_bytes += ctx->dlc;
  208. can_led_event(netdev, CAN_LED_EVENT_TX);
  209. can_get_echo_skb(netdev, ctx->ndx);
  210. }
  211. if (urb->status)
  212. netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
  213. /* Release the context */
  214. mcba_usb_free_ctx(ctx);
  215. }
  216. /* Send data to device */
  217. static netdev_tx_t mcba_usb_xmit(struct mcba_priv *priv,
  218. struct mcba_usb_msg *usb_msg,
  219. struct mcba_usb_ctx *ctx)
  220. {
  221. struct urb *urb;
  222. u8 *buf;
  223. int err;
  224. /* create a URB, and a buffer for it, and copy the data to the URB */
  225. urb = usb_alloc_urb(0, GFP_ATOMIC);
  226. if (!urb)
  227. return -ENOMEM;
  228. buf = usb_alloc_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, GFP_ATOMIC,
  229. &urb->transfer_dma);
  230. if (!buf) {
  231. err = -ENOMEM;
  232. goto nomembuf;
  233. }
  234. memcpy(buf, usb_msg, MCBA_USB_TX_BUFF_SIZE);
  235. usb_fill_bulk_urb(urb, priv->udev,
  236. usb_sndbulkpipe(priv->udev, MCBA_USB_EP_OUT), buf,
  237. MCBA_USB_TX_BUFF_SIZE, mcba_usb_write_bulk_callback,
  238. ctx);
  239. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  240. usb_anchor_urb(urb, &priv->tx_submitted);
  241. err = usb_submit_urb(urb, GFP_ATOMIC);
  242. if (unlikely(err))
  243. goto failed;
  244. /* Release our reference to this URB, the USB core will eventually free
  245. * it entirely.
  246. */
  247. usb_free_urb(urb);
  248. return 0;
  249. failed:
  250. usb_unanchor_urb(urb);
  251. usb_free_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, buf,
  252. urb->transfer_dma);
  253. if (err == -ENODEV)
  254. netif_device_detach(priv->netdev);
  255. else
  256. netdev_warn(priv->netdev, "failed tx_urb %d\n", err);
  257. nomembuf:
  258. usb_free_urb(urb);
  259. return err;
  260. }
  261. /* Send data to device */
  262. static netdev_tx_t mcba_usb_start_xmit(struct sk_buff *skb,
  263. struct net_device *netdev)
  264. {
  265. struct mcba_priv *priv = netdev_priv(netdev);
  266. struct can_frame *cf = (struct can_frame *)skb->data;
  267. struct mcba_usb_ctx *ctx = NULL;
  268. struct net_device_stats *stats = &priv->netdev->stats;
  269. u16 sid;
  270. int err;
  271. struct mcba_usb_msg_can usb_msg = {
  272. .cmd_id = MBCA_CMD_TRANSMIT_MESSAGE_EV
  273. };
  274. if (can_dropped_invalid_skb(netdev, skb))
  275. return NETDEV_TX_OK;
  276. ctx = mcba_usb_get_free_ctx(priv, cf);
  277. if (!ctx)
  278. return NETDEV_TX_BUSY;
  279. can_put_echo_skb(skb, priv->netdev, ctx->ndx);
  280. if (cf->can_id & CAN_EFF_FLAG) {
  281. /* SIDH | SIDL | EIDH | EIDL
  282. * 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
  283. */
  284. sid = MCBA_SIDL_EXID_MASK;
  285. /* store 28-18 bits */
  286. sid |= (cf->can_id & 0x1ffc0000) >> 13;
  287. /* store 17-16 bits */
  288. sid |= (cf->can_id & 0x30000) >> 16;
  289. put_unaligned_be16(sid, &usb_msg.sid);
  290. /* store 15-0 bits */
  291. put_unaligned_be16(cf->can_id & 0xffff, &usb_msg.eid);
  292. } else {
  293. /* SIDH | SIDL
  294. * 10 - 3 | 2 1 0 x x x x x
  295. */
  296. put_unaligned_be16((cf->can_id & CAN_SFF_MASK) << 5,
  297. &usb_msg.sid);
  298. usb_msg.eid = 0;
  299. }
  300. usb_msg.dlc = cf->can_dlc;
  301. memcpy(usb_msg.data, cf->data, usb_msg.dlc);
  302. if (cf->can_id & CAN_RTR_FLAG)
  303. usb_msg.dlc |= MCBA_DLC_RTR_MASK;
  304. err = mcba_usb_xmit(priv, (struct mcba_usb_msg *)&usb_msg, ctx);
  305. if (err)
  306. goto xmit_failed;
  307. return NETDEV_TX_OK;
  308. xmit_failed:
  309. can_free_echo_skb(priv->netdev, ctx->ndx);
  310. mcba_usb_free_ctx(ctx);
  311. dev_kfree_skb(skb);
  312. stats->tx_dropped++;
  313. return NETDEV_TX_OK;
  314. }
  315. /* Send cmd to device */
  316. static void mcba_usb_xmit_cmd(struct mcba_priv *priv,
  317. struct mcba_usb_msg *usb_msg)
  318. {
  319. struct mcba_usb_ctx *ctx = NULL;
  320. int err;
  321. ctx = mcba_usb_get_free_ctx(priv, NULL);
  322. if (!ctx) {
  323. netdev_err(priv->netdev,
  324. "Lack of free ctx. Sending (%d) cmd aborted",
  325. usb_msg->cmd_id);
  326. return;
  327. }
  328. err = mcba_usb_xmit(priv, usb_msg, ctx);
  329. if (err)
  330. netdev_err(priv->netdev, "Failed to send cmd (%d)",
  331. usb_msg->cmd_id);
  332. }
  333. static void mcba_usb_xmit_change_bitrate(struct mcba_priv *priv, u16 bitrate)
  334. {
  335. struct mcba_usb_msg_change_bitrate usb_msg = {
  336. .cmd_id = MBCA_CMD_CHANGE_BIT_RATE
  337. };
  338. put_unaligned_be16(bitrate, &usb_msg.bitrate);
  339. mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
  340. }
  341. static void mcba_usb_xmit_read_fw_ver(struct mcba_priv *priv, u8 pic)
  342. {
  343. struct mcba_usb_msg_fw_ver usb_msg = {
  344. .cmd_id = MBCA_CMD_READ_FW_VERSION,
  345. .pic = pic
  346. };
  347. mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
  348. }
  349. static void mcba_usb_process_can(struct mcba_priv *priv,
  350. struct mcba_usb_msg_can *msg)
  351. {
  352. struct can_frame *cf;
  353. struct sk_buff *skb;
  354. struct net_device_stats *stats = &priv->netdev->stats;
  355. u16 sid;
  356. skb = alloc_can_skb(priv->netdev, &cf);
  357. if (!skb)
  358. return;
  359. sid = get_unaligned_be16(&msg->sid);
  360. if (sid & MCBA_SIDL_EXID_MASK) {
  361. /* SIDH | SIDL | EIDH | EIDL
  362. * 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0
  363. */
  364. cf->can_id = CAN_EFF_FLAG;
  365. /* store 28-18 bits */
  366. cf->can_id |= (sid & 0xffe0) << 13;
  367. /* store 17-16 bits */
  368. cf->can_id |= (sid & 3) << 16;
  369. /* store 15-0 bits */
  370. cf->can_id |= get_unaligned_be16(&msg->eid);
  371. } else {
  372. /* SIDH | SIDL
  373. * 10 - 3 | 2 1 0 x x x x x
  374. */
  375. cf->can_id = (sid & 0xffe0) >> 5;
  376. }
  377. if (msg->dlc & MCBA_DLC_RTR_MASK)
  378. cf->can_id |= CAN_RTR_FLAG;
  379. cf->can_dlc = get_can_dlc(msg->dlc & MCBA_DLC_MASK);
  380. memcpy(cf->data, msg->data, cf->can_dlc);
  381. stats->rx_packets++;
  382. stats->rx_bytes += cf->can_dlc;
  383. can_led_event(priv->netdev, CAN_LED_EVENT_RX);
  384. netif_rx(skb);
  385. }
  386. static void mcba_usb_process_ka_usb(struct mcba_priv *priv,
  387. struct mcba_usb_msg_ka_usb *msg)
  388. {
  389. if (unlikely(priv->usb_ka_first_pass)) {
  390. netdev_info(priv->netdev, "PIC USB version %hhu.%hhu\n",
  391. msg->soft_ver_major, msg->soft_ver_minor);
  392. priv->usb_ka_first_pass = false;
  393. }
  394. if (msg->termination_state)
  395. priv->can.termination = MCBA_TERMINATION_ENABLED;
  396. else
  397. priv->can.termination = MCBA_TERMINATION_DISABLED;
  398. }
  399. static u32 convert_can2host_bitrate(struct mcba_usb_msg_ka_can *msg)
  400. {
  401. const u32 bitrate = get_unaligned_be16(&msg->can_bitrate);
  402. if ((bitrate == 33) || (bitrate == 83))
  403. return bitrate * 1000 + 333;
  404. else
  405. return bitrate * 1000;
  406. }
  407. static void mcba_usb_process_ka_can(struct mcba_priv *priv,
  408. struct mcba_usb_msg_ka_can *msg)
  409. {
  410. if (unlikely(priv->can_ka_first_pass)) {
  411. netdev_info(priv->netdev, "PIC CAN version %hhu.%hhu\n",
  412. msg->soft_ver_major, msg->soft_ver_minor);
  413. priv->can_ka_first_pass = false;
  414. }
  415. if (unlikely(priv->can_speed_check)) {
  416. const u32 bitrate = convert_can2host_bitrate(msg);
  417. priv->can_speed_check = false;
  418. if (bitrate != priv->can.bittiming.bitrate)
  419. netdev_err(
  420. priv->netdev,
  421. "Wrong bitrate reported by the device (%u). Expected %u",
  422. bitrate, priv->can.bittiming.bitrate);
  423. }
  424. priv->bec.txerr = msg->tx_err_cnt;
  425. priv->bec.rxerr = msg->rx_err_cnt;
  426. if (msg->tx_bus_off)
  427. priv->can.state = CAN_STATE_BUS_OFF;
  428. else if ((priv->bec.txerr > MCBA_CAN_STATE_ERR_PSV_TH) ||
  429. (priv->bec.rxerr > MCBA_CAN_STATE_ERR_PSV_TH))
  430. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  431. else if ((priv->bec.txerr > MCBA_CAN_STATE_WRN_TH) ||
  432. (priv->bec.rxerr > MCBA_CAN_STATE_WRN_TH))
  433. priv->can.state = CAN_STATE_ERROR_WARNING;
  434. }
  435. static void mcba_usb_process_rx(struct mcba_priv *priv,
  436. struct mcba_usb_msg *msg)
  437. {
  438. switch (msg->cmd_id) {
  439. case MBCA_CMD_I_AM_ALIVE_FROM_CAN:
  440. mcba_usb_process_ka_can(priv,
  441. (struct mcba_usb_msg_ka_can *)msg);
  442. break;
  443. case MBCA_CMD_I_AM_ALIVE_FROM_USB:
  444. mcba_usb_process_ka_usb(priv,
  445. (struct mcba_usb_msg_ka_usb *)msg);
  446. break;
  447. case MBCA_CMD_RECEIVE_MESSAGE:
  448. mcba_usb_process_can(priv, (struct mcba_usb_msg_can *)msg);
  449. break;
  450. case MBCA_CMD_NOTHING_TO_SEND:
  451. /* Side effect of communication between PIC_USB and PIC_CAN.
  452. * PIC_CAN is telling us that it has nothing to send
  453. */
  454. break;
  455. case MBCA_CMD_TRANSMIT_MESSAGE_RSP:
  456. /* Transmission response from the device containing timestamp */
  457. break;
  458. default:
  459. netdev_warn(priv->netdev, "Unsupported msg (0x%hhX)",
  460. msg->cmd_id);
  461. break;
  462. }
  463. }
  464. /* Callback for reading data from device
  465. *
  466. * Check urb status, call read function and resubmit urb read operation.
  467. */
  468. static void mcba_usb_read_bulk_callback(struct urb *urb)
  469. {
  470. struct mcba_priv *priv = urb->context;
  471. struct net_device *netdev;
  472. int retval;
  473. int pos = 0;
  474. netdev = priv->netdev;
  475. if (!netif_device_present(netdev))
  476. return;
  477. switch (urb->status) {
  478. case 0: /* success */
  479. break;
  480. case -ENOENT:
  481. case -ESHUTDOWN:
  482. return;
  483. default:
  484. netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
  485. goto resubmit_urb;
  486. }
  487. while (pos < urb->actual_length) {
  488. struct mcba_usb_msg *msg;
  489. if (pos + sizeof(struct mcba_usb_msg) > urb->actual_length) {
  490. netdev_err(priv->netdev, "format error\n");
  491. break;
  492. }
  493. msg = (struct mcba_usb_msg *)(urb->transfer_buffer + pos);
  494. mcba_usb_process_rx(priv, msg);
  495. pos += sizeof(struct mcba_usb_msg);
  496. }
  497. resubmit_urb:
  498. usb_fill_bulk_urb(urb, priv->udev,
  499. usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_OUT),
  500. urb->transfer_buffer, MCBA_USB_RX_BUFF_SIZE,
  501. mcba_usb_read_bulk_callback, priv);
  502. retval = usb_submit_urb(urb, GFP_ATOMIC);
  503. if (retval == -ENODEV)
  504. netif_device_detach(netdev);
  505. else if (retval)
  506. netdev_err(netdev, "failed resubmitting read bulk urb: %d\n",
  507. retval);
  508. }
  509. /* Start USB device */
  510. static int mcba_usb_start(struct mcba_priv *priv)
  511. {
  512. struct net_device *netdev = priv->netdev;
  513. int err, i;
  514. mcba_init_ctx(priv);
  515. for (i = 0; i < MCBA_MAX_RX_URBS; i++) {
  516. struct urb *urb = NULL;
  517. u8 *buf;
  518. /* create a URB, and a buffer for it */
  519. urb = usb_alloc_urb(0, GFP_KERNEL);
  520. if (!urb) {
  521. err = -ENOMEM;
  522. break;
  523. }
  524. buf = usb_alloc_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
  525. GFP_KERNEL, &urb->transfer_dma);
  526. if (!buf) {
  527. netdev_err(netdev, "No memory left for USB buffer\n");
  528. usb_free_urb(urb);
  529. err = -ENOMEM;
  530. break;
  531. }
  532. usb_fill_bulk_urb(urb, priv->udev,
  533. usb_rcvbulkpipe(priv->udev, MCBA_USB_EP_IN),
  534. buf, MCBA_USB_RX_BUFF_SIZE,
  535. mcba_usb_read_bulk_callback, priv);
  536. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  537. usb_anchor_urb(urb, &priv->rx_submitted);
  538. err = usb_submit_urb(urb, GFP_KERNEL);
  539. if (err) {
  540. usb_unanchor_urb(urb);
  541. usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE,
  542. buf, urb->transfer_dma);
  543. usb_free_urb(urb);
  544. break;
  545. }
  546. /* Drop reference, USB core will take care of freeing it */
  547. usb_free_urb(urb);
  548. }
  549. /* Did we submit any URBs */
  550. if (i == 0) {
  551. netdev_warn(netdev, "couldn't setup read URBs\n");
  552. return err;
  553. }
  554. /* Warn if we've couldn't transmit all the URBs */
  555. if (i < MCBA_MAX_RX_URBS)
  556. netdev_warn(netdev, "rx performance may be slow\n");
  557. mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_USB);
  558. mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_CAN);
  559. return err;
  560. }
  561. /* Open USB device */
  562. static int mcba_usb_open(struct net_device *netdev)
  563. {
  564. struct mcba_priv *priv = netdev_priv(netdev);
  565. int err;
  566. /* common open */
  567. err = open_candev(netdev);
  568. if (err)
  569. return err;
  570. priv->can_speed_check = true;
  571. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  572. can_led_event(netdev, CAN_LED_EVENT_OPEN);
  573. netif_start_queue(netdev);
  574. return 0;
  575. }
  576. static void mcba_urb_unlink(struct mcba_priv *priv)
  577. {
  578. usb_kill_anchored_urbs(&priv->rx_submitted);
  579. usb_kill_anchored_urbs(&priv->tx_submitted);
  580. }
  581. /* Close USB device */
  582. static int mcba_usb_close(struct net_device *netdev)
  583. {
  584. struct mcba_priv *priv = netdev_priv(netdev);
  585. priv->can.state = CAN_STATE_STOPPED;
  586. netif_stop_queue(netdev);
  587. /* Stop polling */
  588. mcba_urb_unlink(priv);
  589. close_candev(netdev);
  590. can_led_event(netdev, CAN_LED_EVENT_STOP);
  591. return 0;
  592. }
  593. /* Set network device mode
  594. *
  595. * Maybe we should leave this function empty, because the device
  596. * set mode variable with open command.
  597. */
  598. static int mcba_net_set_mode(struct net_device *netdev, enum can_mode mode)
  599. {
  600. return 0;
  601. }
  602. static int mcba_net_get_berr_counter(const struct net_device *netdev,
  603. struct can_berr_counter *bec)
  604. {
  605. struct mcba_priv *priv = netdev_priv(netdev);
  606. bec->txerr = priv->bec.txerr;
  607. bec->rxerr = priv->bec.rxerr;
  608. return 0;
  609. }
  610. static const struct net_device_ops mcba_netdev_ops = {
  611. .ndo_open = mcba_usb_open,
  612. .ndo_stop = mcba_usb_close,
  613. .ndo_start_xmit = mcba_usb_start_xmit,
  614. };
  615. /* Microchip CANBUS has hardcoded bittiming values by default.
  616. * This function sends request via USB to change the speed and align bittiming
  617. * values for presentation purposes only
  618. */
  619. static int mcba_net_set_bittiming(struct net_device *netdev)
  620. {
  621. struct mcba_priv *priv = netdev_priv(netdev);
  622. const u16 bitrate_kbps = priv->can.bittiming.bitrate / 1000;
  623. mcba_usb_xmit_change_bitrate(priv, bitrate_kbps);
  624. return 0;
  625. }
  626. static int mcba_set_termination(struct net_device *netdev, u16 term)
  627. {
  628. struct mcba_priv *priv = netdev_priv(netdev);
  629. struct mcba_usb_msg_termination usb_msg = {
  630. .cmd_id = MBCA_CMD_SETUP_TERMINATION_RESISTANCE
  631. };
  632. if (term == MCBA_TERMINATION_ENABLED)
  633. usb_msg.termination = 1;
  634. else
  635. usb_msg.termination = 0;
  636. mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg);
  637. return 0;
  638. }
  639. static int mcba_usb_probe(struct usb_interface *intf,
  640. const struct usb_device_id *id)
  641. {
  642. struct net_device *netdev;
  643. struct mcba_priv *priv;
  644. int err = -ENOMEM;
  645. struct usb_device *usbdev = interface_to_usbdev(intf);
  646. netdev = alloc_candev(sizeof(struct mcba_priv), MCBA_MAX_TX_URBS);
  647. if (!netdev) {
  648. dev_err(&intf->dev, "Couldn't alloc candev\n");
  649. return -ENOMEM;
  650. }
  651. priv = netdev_priv(netdev);
  652. priv->udev = usbdev;
  653. priv->netdev = netdev;
  654. priv->usb_ka_first_pass = true;
  655. priv->can_ka_first_pass = true;
  656. priv->can_speed_check = false;
  657. init_usb_anchor(&priv->rx_submitted);
  658. init_usb_anchor(&priv->tx_submitted);
  659. usb_set_intfdata(intf, priv);
  660. /* Init CAN device */
  661. priv->can.state = CAN_STATE_STOPPED;
  662. priv->can.termination_const = mcba_termination;
  663. priv->can.termination_const_cnt = ARRAY_SIZE(mcba_termination);
  664. priv->can.bitrate_const = mcba_bitrate;
  665. priv->can.bitrate_const_cnt = ARRAY_SIZE(mcba_bitrate);
  666. priv->can.do_set_termination = mcba_set_termination;
  667. priv->can.do_set_mode = mcba_net_set_mode;
  668. priv->can.do_get_berr_counter = mcba_net_get_berr_counter;
  669. priv->can.do_set_bittiming = mcba_net_set_bittiming;
  670. netdev->netdev_ops = &mcba_netdev_ops;
  671. netdev->flags |= IFF_ECHO; /* we support local echo */
  672. SET_NETDEV_DEV(netdev, &intf->dev);
  673. err = register_candev(netdev);
  674. if (err) {
  675. netdev_err(netdev, "couldn't register CAN device: %d\n", err);
  676. goto cleanup_free_candev;
  677. }
  678. devm_can_led_init(netdev);
  679. /* Start USB dev only if we have successfully registered CAN device */
  680. err = mcba_usb_start(priv);
  681. if (err) {
  682. if (err == -ENODEV)
  683. netif_device_detach(priv->netdev);
  684. netdev_warn(netdev, "couldn't start device: %d\n", err);
  685. goto cleanup_unregister_candev;
  686. }
  687. dev_info(&intf->dev, "Microchip CAN BUS analizer connected\n");
  688. return 0;
  689. cleanup_unregister_candev:
  690. unregister_candev(priv->netdev);
  691. cleanup_free_candev:
  692. free_candev(netdev);
  693. return err;
  694. }
  695. /* Called by the usb core when driver is unloaded or device is removed */
  696. static void mcba_usb_disconnect(struct usb_interface *intf)
  697. {
  698. struct mcba_priv *priv = usb_get_intfdata(intf);
  699. usb_set_intfdata(intf, NULL);
  700. netdev_info(priv->netdev, "device disconnected\n");
  701. unregister_candev(priv->netdev);
  702. free_candev(priv->netdev);
  703. mcba_urb_unlink(priv);
  704. }
  705. static struct usb_driver mcba_usb_driver = {
  706. .name = MCBA_MODULE_NAME,
  707. .probe = mcba_usb_probe,
  708. .disconnect = mcba_usb_disconnect,
  709. .id_table = mcba_usb_table,
  710. };
  711. module_usb_driver(mcba_usb_driver);
  712. MODULE_AUTHOR("Remigiusz Kołłątaj <remigiusz.kollataj@mobica.com>");
  713. MODULE_DESCRIPTION("SocketCAN driver for Microchip CAN BUS Analyzer Tool");
  714. MODULE_LICENSE("GPL v2");