pcan_usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * CAN driver for PEAK System PCAN-USB adapter
  3. * Derived from the PCAN project file driver/src/pcan_usb.c
  4. *
  5. * Copyright (C) 2003-2010 PEAK System-Technik GmbH
  6. * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
  7. *
  8. * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de>
  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/netdevice.h>
  20. #include <linux/usb.h>
  21. #include <linux/module.h>
  22. #include <linux/can.h>
  23. #include <linux/can/dev.h>
  24. #include <linux/can/error.h>
  25. #include "pcan_usb_core.h"
  26. MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB adapter");
  27. /* PCAN-USB Endpoints */
  28. #define PCAN_USB_EP_CMDOUT 1
  29. #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
  30. #define PCAN_USB_EP_MSGOUT 2
  31. #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
  32. /* PCAN-USB command struct */
  33. #define PCAN_USB_CMD_FUNC 0
  34. #define PCAN_USB_CMD_NUM 1
  35. #define PCAN_USB_CMD_ARGS 2
  36. #define PCAN_USB_CMD_ARGS_LEN 14
  37. #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
  38. PCAN_USB_CMD_ARGS_LEN)
  39. /* PCAN-USB command timeout (ms.) */
  40. #define PCAN_USB_COMMAND_TIMEOUT 1000
  41. /* PCAN-USB startup timeout (ms.) */
  42. #define PCAN_USB_STARTUP_TIMEOUT 10
  43. /* PCAN-USB rx/tx buffers size */
  44. #define PCAN_USB_RX_BUFFER_SIZE 64
  45. #define PCAN_USB_TX_BUFFER_SIZE 64
  46. #define PCAN_USB_MSG_HEADER_LEN 2
  47. /* PCAN-USB adapter internal clock (MHz) */
  48. #define PCAN_USB_CRYSTAL_HZ 16000000
  49. /* PCAN-USB USB message record status/len field */
  50. #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
  51. #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
  52. #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
  53. #define PCAN_USB_STATUSLEN_RTR (1 << 4)
  54. #define PCAN_USB_STATUSLEN_DLC (0xf)
  55. /* PCAN-USB error flags */
  56. #define PCAN_USB_ERROR_TXFULL 0x01
  57. #define PCAN_USB_ERROR_RXQOVR 0x02
  58. #define PCAN_USB_ERROR_BUS_LIGHT 0x04
  59. #define PCAN_USB_ERROR_BUS_HEAVY 0x08
  60. #define PCAN_USB_ERROR_BUS_OFF 0x10
  61. #define PCAN_USB_ERROR_RXQEMPTY 0x20
  62. #define PCAN_USB_ERROR_QOVR 0x40
  63. #define PCAN_USB_ERROR_TXQFULL 0x80
  64. /* SJA1000 modes */
  65. #define SJA1000_MODE_NORMAL 0x00
  66. #define SJA1000_MODE_INIT 0x01
  67. /*
  68. * tick duration = 42.666 us =>
  69. * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000
  70. * accuracy = 10^-7
  71. */
  72. #define PCAN_USB_TS_DIV_SHIFTER 20
  73. #define PCAN_USB_TS_US_PER_TICK 44739243
  74. /* PCAN-USB messages record types */
  75. #define PCAN_USB_REC_ERROR 1
  76. #define PCAN_USB_REC_ANALOG 2
  77. #define PCAN_USB_REC_BUSLOAD 3
  78. #define PCAN_USB_REC_TS 4
  79. #define PCAN_USB_REC_BUSEVT 5
  80. /* private to PCAN-USB adapter */
  81. struct pcan_usb {
  82. struct peak_usb_device dev;
  83. struct peak_time_ref time_ref;
  84. struct timer_list restart_timer;
  85. };
  86. /* incoming message context for decoding */
  87. struct pcan_usb_msg_context {
  88. u16 ts16;
  89. u8 prev_ts8;
  90. u8 *ptr;
  91. u8 *end;
  92. u8 rec_cnt;
  93. u8 rec_idx;
  94. u8 rec_data_idx;
  95. struct net_device *netdev;
  96. struct pcan_usb *pdev;
  97. };
  98. /*
  99. * send a command
  100. */
  101. static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
  102. {
  103. int err;
  104. int actual_length;
  105. /* usb device unregistered? */
  106. if (!(dev->state & PCAN_USB_STATE_CONNECTED))
  107. return 0;
  108. dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
  109. dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
  110. if (p)
  111. memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
  112. p, PCAN_USB_CMD_ARGS_LEN);
  113. err = usb_bulk_msg(dev->udev,
  114. usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
  115. dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
  116. PCAN_USB_COMMAND_TIMEOUT);
  117. if (err)
  118. netdev_err(dev->netdev,
  119. "sending cmd f=0x%x n=0x%x failure: %d\n",
  120. f, n, err);
  121. return err;
  122. }
  123. /*
  124. * send a command then wait for its response
  125. */
  126. static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
  127. {
  128. int err;
  129. int actual_length;
  130. /* usb device unregistered? */
  131. if (!(dev->state & PCAN_USB_STATE_CONNECTED))
  132. return 0;
  133. /* first, send command */
  134. err = pcan_usb_send_cmd(dev, f, n, NULL);
  135. if (err)
  136. return err;
  137. err = usb_bulk_msg(dev->udev,
  138. usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
  139. dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
  140. PCAN_USB_COMMAND_TIMEOUT);
  141. if (err)
  142. netdev_err(dev->netdev,
  143. "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
  144. else if (p)
  145. memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
  146. PCAN_USB_CMD_ARGS_LEN);
  147. return err;
  148. }
  149. static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
  150. {
  151. u8 args[PCAN_USB_CMD_ARGS_LEN] = {
  152. [1] = mode,
  153. };
  154. return pcan_usb_send_cmd(dev, 9, 2, args);
  155. }
  156. static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
  157. {
  158. u8 args[PCAN_USB_CMD_ARGS_LEN] = {
  159. [0] = !!onoff,
  160. };
  161. return pcan_usb_send_cmd(dev, 3, 2, args);
  162. }
  163. static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
  164. {
  165. u8 args[PCAN_USB_CMD_ARGS_LEN] = {
  166. [0] = !!onoff,
  167. };
  168. return pcan_usb_send_cmd(dev, 3, 3, args);
  169. }
  170. static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
  171. {
  172. u8 args[PCAN_USB_CMD_ARGS_LEN] = {
  173. [0] = !!onoff,
  174. };
  175. return pcan_usb_send_cmd(dev, 10, 2, args);
  176. }
  177. /*
  178. * set bittiming value to can
  179. */
  180. static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
  181. struct can_bittiming *bt)
  182. {
  183. u8 args[PCAN_USB_CMD_ARGS_LEN];
  184. u8 btr0, btr1;
  185. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  186. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  187. (((bt->phase_seg2 - 1) & 0x7) << 4);
  188. if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  189. btr1 |= 0x80;
  190. netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
  191. btr0, btr1);
  192. args[0] = btr1;
  193. args[1] = btr0;
  194. return pcan_usb_send_cmd(dev, 1, 2, args);
  195. }
  196. /*
  197. * init/reset can
  198. */
  199. static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
  200. {
  201. int err;
  202. err = pcan_usb_set_bus(dev, onoff);
  203. if (err)
  204. return err;
  205. if (!onoff) {
  206. err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
  207. } else {
  208. /* the PCAN-USB needs time to init */
  209. set_current_state(TASK_INTERRUPTIBLE);
  210. schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
  211. }
  212. return err;
  213. }
  214. /*
  215. * handle end of waiting for the device to reset
  216. */
  217. static void pcan_usb_restart(struct timer_list *t)
  218. {
  219. struct pcan_usb *pdev = from_timer(pdev, t, restart_timer);
  220. struct peak_usb_device *dev = &pdev->dev;
  221. /* notify candev and netdev */
  222. peak_usb_restart_complete(dev);
  223. }
  224. /*
  225. * handle the submission of the restart urb
  226. */
  227. static void pcan_usb_restart_pending(struct urb *urb)
  228. {
  229. struct pcan_usb *pdev = urb->context;
  230. /* the PCAN-USB needs time to restart */
  231. mod_timer(&pdev->restart_timer,
  232. jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
  233. /* can delete usb resources */
  234. peak_usb_async_complete(urb);
  235. }
  236. /*
  237. * handle asynchronous restart
  238. */
  239. static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
  240. u8 *buf)
  241. {
  242. struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
  243. if (timer_pending(&pdev->restart_timer))
  244. return -EBUSY;
  245. /* set bus on */
  246. buf[PCAN_USB_CMD_FUNC] = 3;
  247. buf[PCAN_USB_CMD_NUM] = 2;
  248. buf[PCAN_USB_CMD_ARGS] = 1;
  249. usb_fill_bulk_urb(urb, dev->udev,
  250. usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
  251. buf, PCAN_USB_CMD_LEN,
  252. pcan_usb_restart_pending, pdev);
  253. return usb_submit_urb(urb, GFP_ATOMIC);
  254. }
  255. /*
  256. * read serial number from device
  257. */
  258. static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
  259. {
  260. u8 args[PCAN_USB_CMD_ARGS_LEN];
  261. int err;
  262. err = pcan_usb_wait_rsp(dev, 6, 1, args);
  263. if (err) {
  264. netdev_err(dev->netdev, "getting serial failure: %d\n", err);
  265. } else if (serial_number) {
  266. __le32 tmp32;
  267. memcpy(&tmp32, args, 4);
  268. *serial_number = le32_to_cpu(tmp32);
  269. }
  270. return err;
  271. }
  272. /*
  273. * read device id from device
  274. */
  275. static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
  276. {
  277. u8 args[PCAN_USB_CMD_ARGS_LEN];
  278. int err;
  279. err = pcan_usb_wait_rsp(dev, 4, 1, args);
  280. if (err)
  281. netdev_err(dev->netdev, "getting device id failure: %d\n", err);
  282. else if (device_id)
  283. *device_id = args[0];
  284. return err;
  285. }
  286. /*
  287. * update current time ref with received timestamp
  288. */
  289. static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
  290. {
  291. __le16 tmp16;
  292. if ((mc->ptr+2) > mc->end)
  293. return -EINVAL;
  294. memcpy(&tmp16, mc->ptr, 2);
  295. mc->ts16 = le16_to_cpu(tmp16);
  296. if (mc->rec_idx > 0)
  297. peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
  298. else
  299. peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
  300. return 0;
  301. }
  302. /*
  303. * decode received timestamp
  304. */
  305. static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
  306. {
  307. /* only 1st packet supplies a word timestamp */
  308. if (first_packet) {
  309. __le16 tmp16;
  310. if ((mc->ptr + 2) > mc->end)
  311. return -EINVAL;
  312. memcpy(&tmp16, mc->ptr, 2);
  313. mc->ptr += 2;
  314. mc->ts16 = le16_to_cpu(tmp16);
  315. mc->prev_ts8 = mc->ts16 & 0x00ff;
  316. } else {
  317. u8 ts8;
  318. if ((mc->ptr + 1) > mc->end)
  319. return -EINVAL;
  320. ts8 = *mc->ptr++;
  321. if (ts8 < mc->prev_ts8)
  322. mc->ts16 += 0x100;
  323. mc->ts16 &= 0xff00;
  324. mc->ts16 |= ts8;
  325. mc->prev_ts8 = ts8;
  326. }
  327. return 0;
  328. }
  329. static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
  330. u8 status_len)
  331. {
  332. struct sk_buff *skb;
  333. struct can_frame *cf;
  334. enum can_state new_state;
  335. /* ignore this error until 1st ts received */
  336. if (n == PCAN_USB_ERROR_QOVR)
  337. if (!mc->pdev->time_ref.tick_count)
  338. return 0;
  339. new_state = mc->pdev->dev.can.state;
  340. switch (mc->pdev->dev.can.state) {
  341. case CAN_STATE_ERROR_ACTIVE:
  342. if (n & PCAN_USB_ERROR_BUS_LIGHT) {
  343. new_state = CAN_STATE_ERROR_WARNING;
  344. break;
  345. }
  346. /* else: fall through */
  347. case CAN_STATE_ERROR_WARNING:
  348. if (n & PCAN_USB_ERROR_BUS_HEAVY) {
  349. new_state = CAN_STATE_ERROR_PASSIVE;
  350. break;
  351. }
  352. if (n & PCAN_USB_ERROR_BUS_OFF) {
  353. new_state = CAN_STATE_BUS_OFF;
  354. break;
  355. }
  356. if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
  357. /*
  358. * trick to bypass next comparison and process other
  359. * errors
  360. */
  361. new_state = CAN_STATE_MAX;
  362. break;
  363. }
  364. if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
  365. /* no error (back to active state) */
  366. mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
  367. return 0;
  368. }
  369. break;
  370. case CAN_STATE_ERROR_PASSIVE:
  371. if (n & PCAN_USB_ERROR_BUS_OFF) {
  372. new_state = CAN_STATE_BUS_OFF;
  373. break;
  374. }
  375. if (n & PCAN_USB_ERROR_BUS_LIGHT) {
  376. new_state = CAN_STATE_ERROR_WARNING;
  377. break;
  378. }
  379. if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
  380. /*
  381. * trick to bypass next comparison and process other
  382. * errors
  383. */
  384. new_state = CAN_STATE_MAX;
  385. break;
  386. }
  387. if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
  388. /* no error (back to active state) */
  389. mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
  390. return 0;
  391. }
  392. break;
  393. default:
  394. /* do nothing waiting for restart */
  395. return 0;
  396. }
  397. /* donot post any error if current state didn't change */
  398. if (mc->pdev->dev.can.state == new_state)
  399. return 0;
  400. /* allocate an skb to store the error frame */
  401. skb = alloc_can_err_skb(mc->netdev, &cf);
  402. if (!skb)
  403. return -ENOMEM;
  404. switch (new_state) {
  405. case CAN_STATE_BUS_OFF:
  406. cf->can_id |= CAN_ERR_BUSOFF;
  407. mc->pdev->dev.can.can_stats.bus_off++;
  408. can_bus_off(mc->netdev);
  409. break;
  410. case CAN_STATE_ERROR_PASSIVE:
  411. cf->can_id |= CAN_ERR_CRTL;
  412. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
  413. CAN_ERR_CRTL_RX_PASSIVE;
  414. mc->pdev->dev.can.can_stats.error_passive++;
  415. break;
  416. case CAN_STATE_ERROR_WARNING:
  417. cf->can_id |= CAN_ERR_CRTL;
  418. cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
  419. CAN_ERR_CRTL_RX_WARNING;
  420. mc->pdev->dev.can.can_stats.error_warning++;
  421. break;
  422. default:
  423. /* CAN_STATE_MAX (trick to handle other errors) */
  424. cf->can_id |= CAN_ERR_CRTL;
  425. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  426. mc->netdev->stats.rx_over_errors++;
  427. mc->netdev->stats.rx_errors++;
  428. new_state = mc->pdev->dev.can.state;
  429. break;
  430. }
  431. mc->pdev->dev.can.state = new_state;
  432. if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
  433. struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
  434. peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16,
  435. &hwts->hwtstamp);
  436. }
  437. mc->netdev->stats.rx_packets++;
  438. mc->netdev->stats.rx_bytes += cf->can_dlc;
  439. netif_rx(skb);
  440. return 0;
  441. }
  442. /*
  443. * decode non-data usb message
  444. */
  445. static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
  446. u8 status_len)
  447. {
  448. u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
  449. u8 f, n;
  450. int err;
  451. /* check whether function and number can be read */
  452. if ((mc->ptr + 2) > mc->end)
  453. return -EINVAL;
  454. f = mc->ptr[PCAN_USB_CMD_FUNC];
  455. n = mc->ptr[PCAN_USB_CMD_NUM];
  456. mc->ptr += PCAN_USB_CMD_ARGS;
  457. if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
  458. int err = pcan_usb_decode_ts(mc, !mc->rec_idx);
  459. if (err)
  460. return err;
  461. }
  462. switch (f) {
  463. case PCAN_USB_REC_ERROR:
  464. err = pcan_usb_decode_error(mc, n, status_len);
  465. if (err)
  466. return err;
  467. break;
  468. case PCAN_USB_REC_ANALOG:
  469. /* analog values (ignored) */
  470. rec_len = 2;
  471. break;
  472. case PCAN_USB_REC_BUSLOAD:
  473. /* bus load (ignored) */
  474. rec_len = 1;
  475. break;
  476. case PCAN_USB_REC_TS:
  477. /* only timestamp */
  478. if (pcan_usb_update_ts(mc))
  479. return -EINVAL;
  480. break;
  481. case PCAN_USB_REC_BUSEVT:
  482. /* error frame/bus event */
  483. if (n & PCAN_USB_ERROR_TXQFULL)
  484. netdev_dbg(mc->netdev, "device Tx queue full)\n");
  485. break;
  486. default:
  487. netdev_err(mc->netdev, "unexpected function %u\n", f);
  488. break;
  489. }
  490. if ((mc->ptr + rec_len) > mc->end)
  491. return -EINVAL;
  492. mc->ptr += rec_len;
  493. return 0;
  494. }
  495. /*
  496. * decode data usb message
  497. */
  498. static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
  499. {
  500. u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
  501. struct sk_buff *skb;
  502. struct can_frame *cf;
  503. struct skb_shared_hwtstamps *hwts;
  504. skb = alloc_can_skb(mc->netdev, &cf);
  505. if (!skb)
  506. return -ENOMEM;
  507. if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
  508. __le32 tmp32;
  509. if ((mc->ptr + 4) > mc->end)
  510. goto decode_failed;
  511. memcpy(&tmp32, mc->ptr, 4);
  512. mc->ptr += 4;
  513. cf->can_id = (le32_to_cpu(tmp32) >> 3) | CAN_EFF_FLAG;
  514. } else {
  515. __le16 tmp16;
  516. if ((mc->ptr + 2) > mc->end)
  517. goto decode_failed;
  518. memcpy(&tmp16, mc->ptr, 2);
  519. mc->ptr += 2;
  520. cf->can_id = le16_to_cpu(tmp16) >> 5;
  521. }
  522. cf->can_dlc = get_can_dlc(rec_len);
  523. /* first data packet timestamp is a word */
  524. if (pcan_usb_decode_ts(mc, !mc->rec_data_idx))
  525. goto decode_failed;
  526. /* read data */
  527. memset(cf->data, 0x0, sizeof(cf->data));
  528. if (status_len & PCAN_USB_STATUSLEN_RTR) {
  529. cf->can_id |= CAN_RTR_FLAG;
  530. } else {
  531. if ((mc->ptr + rec_len) > mc->end)
  532. goto decode_failed;
  533. memcpy(cf->data, mc->ptr, cf->can_dlc);
  534. mc->ptr += rec_len;
  535. }
  536. /* convert timestamp into kernel time */
  537. hwts = skb_hwtstamps(skb);
  538. peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp);
  539. /* update statistics */
  540. mc->netdev->stats.rx_packets++;
  541. mc->netdev->stats.rx_bytes += cf->can_dlc;
  542. /* push the skb */
  543. netif_rx(skb);
  544. return 0;
  545. decode_failed:
  546. dev_kfree_skb(skb);
  547. return -EINVAL;
  548. }
  549. /*
  550. * process incoming message
  551. */
  552. static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
  553. {
  554. struct pcan_usb_msg_context mc = {
  555. .rec_cnt = ibuf[1],
  556. .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
  557. .end = ibuf + lbuf,
  558. .netdev = dev->netdev,
  559. .pdev = container_of(dev, struct pcan_usb, dev),
  560. };
  561. int err;
  562. for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
  563. u8 sl = *mc.ptr++;
  564. /* handle status and error frames here */
  565. if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
  566. err = pcan_usb_decode_status(&mc, sl);
  567. /* handle normal can frames here */
  568. } else {
  569. err = pcan_usb_decode_data(&mc, sl);
  570. mc.rec_data_idx++;
  571. }
  572. }
  573. return err;
  574. }
  575. /*
  576. * process any incoming buffer
  577. */
  578. static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
  579. {
  580. int err = 0;
  581. if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
  582. err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
  583. urb->actual_length);
  584. } else if (urb->actual_length > 0) {
  585. netdev_err(dev->netdev, "usb message length error (%u)\n",
  586. urb->actual_length);
  587. err = -EINVAL;
  588. }
  589. return err;
  590. }
  591. /*
  592. * process outgoing packet
  593. */
  594. static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
  595. u8 *obuf, size_t *size)
  596. {
  597. struct net_device *netdev = dev->netdev;
  598. struct net_device_stats *stats = &netdev->stats;
  599. struct can_frame *cf = (struct can_frame *)skb->data;
  600. u8 *pc;
  601. obuf[0] = 2;
  602. obuf[1] = 1;
  603. pc = obuf + PCAN_USB_MSG_HEADER_LEN;
  604. /* status/len byte */
  605. *pc = cf->can_dlc;
  606. if (cf->can_id & CAN_RTR_FLAG)
  607. *pc |= PCAN_USB_STATUSLEN_RTR;
  608. /* can id */
  609. if (cf->can_id & CAN_EFF_FLAG) {
  610. __le32 tmp32 = cpu_to_le32((cf->can_id & CAN_ERR_MASK) << 3);
  611. *pc |= PCAN_USB_STATUSLEN_EXT_ID;
  612. memcpy(++pc, &tmp32, 4);
  613. pc += 4;
  614. } else {
  615. __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5);
  616. memcpy(++pc, &tmp16, 2);
  617. pc += 2;
  618. }
  619. /* can data */
  620. if (!(cf->can_id & CAN_RTR_FLAG)) {
  621. memcpy(pc, cf->data, cf->can_dlc);
  622. pc += cf->can_dlc;
  623. }
  624. obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
  625. return 0;
  626. }
  627. /*
  628. * start interface
  629. */
  630. static int pcan_usb_start(struct peak_usb_device *dev)
  631. {
  632. struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
  633. /* number of bits used in timestamps read from adapter struct */
  634. peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
  635. /* if revision greater than 3, can put silent mode on/off */
  636. if (dev->device_rev > 3) {
  637. int err;
  638. err = pcan_usb_set_silent(dev,
  639. dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
  640. if (err)
  641. return err;
  642. }
  643. return pcan_usb_set_ext_vcc(dev, 0);
  644. }
  645. static int pcan_usb_init(struct peak_usb_device *dev)
  646. {
  647. struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
  648. u32 serial_number;
  649. int err;
  650. /* initialize a timer needed to wait for hardware restart */
  651. timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
  652. /*
  653. * explicit use of dev_xxx() instead of netdev_xxx() here:
  654. * information displayed are related to the device itself, not
  655. * to the canx netdevice.
  656. */
  657. err = pcan_usb_get_serial(dev, &serial_number);
  658. if (err) {
  659. dev_err(dev->netdev->dev.parent,
  660. "unable to read %s serial number (err %d)\n",
  661. pcan_usb.name, err);
  662. return err;
  663. }
  664. dev_info(dev->netdev->dev.parent,
  665. "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
  666. pcan_usb.name, dev->device_rev, serial_number,
  667. pcan_usb.ctrl_count);
  668. return 0;
  669. }
  670. /*
  671. * probe function for new PCAN-USB usb interface
  672. */
  673. static int pcan_usb_probe(struct usb_interface *intf)
  674. {
  675. struct usb_host_interface *if_desc;
  676. int i;
  677. if_desc = intf->altsetting;
  678. /* check interface endpoint addresses */
  679. for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
  680. struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
  681. switch (ep->bEndpointAddress) {
  682. case PCAN_USB_EP_CMDOUT:
  683. case PCAN_USB_EP_CMDIN:
  684. case PCAN_USB_EP_MSGOUT:
  685. case PCAN_USB_EP_MSGIN:
  686. break;
  687. default:
  688. return -ENODEV;
  689. }
  690. }
  691. return 0;
  692. }
  693. /*
  694. * describe the PCAN-USB adapter
  695. */
  696. static const struct can_bittiming_const pcan_usb_const = {
  697. .name = "pcan_usb",
  698. .tseg1_min = 1,
  699. .tseg1_max = 16,
  700. .tseg2_min = 1,
  701. .tseg2_max = 8,
  702. .sjw_max = 4,
  703. .brp_min = 1,
  704. .brp_max = 64,
  705. .brp_inc = 1,
  706. };
  707. const struct peak_usb_adapter pcan_usb = {
  708. .name = "PCAN-USB",
  709. .device_id = PCAN_USB_PRODUCT_ID,
  710. .ctrl_count = 1,
  711. .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
  712. .clock = {
  713. .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
  714. },
  715. .bittiming_const = &pcan_usb_const,
  716. /* size of device private data */
  717. .sizeof_dev_private = sizeof(struct pcan_usb),
  718. /* timestamps usage */
  719. .ts_used_bits = 16,
  720. .ts_period = 24575, /* calibration period in ts. */
  721. .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
  722. .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
  723. /* give here messages in/out endpoints */
  724. .ep_msg_in = PCAN_USB_EP_MSGIN,
  725. .ep_msg_out = {PCAN_USB_EP_MSGOUT},
  726. /* size of rx/tx usb buffers */
  727. .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
  728. .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
  729. /* device callbacks */
  730. .intf_probe = pcan_usb_probe,
  731. .dev_init = pcan_usb_init,
  732. .dev_set_bus = pcan_usb_write_mode,
  733. .dev_set_bittiming = pcan_usb_set_bittiming,
  734. .dev_get_device_id = pcan_usb_get_device_id,
  735. .dev_decode_buf = pcan_usb_decode_buf,
  736. .dev_encode_msg = pcan_usb_encode_msg,
  737. .dev_start = pcan_usb_start,
  738. .dev_restart_async = pcan_usb_restart_async,
  739. };