pcan_usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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(unsigned long arg)
  218. {
  219. /* notify candev and netdev */
  220. peak_usb_restart_complete((struct peak_usb_device *)arg);
  221. }
  222. /*
  223. * handle the submission of the restart urb
  224. */
  225. static void pcan_usb_restart_pending(struct urb *urb)
  226. {
  227. struct pcan_usb *pdev = urb->context;
  228. /* the PCAN-USB needs time to restart */
  229. mod_timer(&pdev->restart_timer,
  230. jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
  231. /* can delete usb resources */
  232. peak_usb_async_complete(urb);
  233. }
  234. /*
  235. * handle asynchronous restart
  236. */
  237. static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
  238. u8 *buf)
  239. {
  240. struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
  241. if (timer_pending(&pdev->restart_timer))
  242. return -EBUSY;
  243. /* set bus on */
  244. buf[PCAN_USB_CMD_FUNC] = 3;
  245. buf[PCAN_USB_CMD_NUM] = 2;
  246. buf[PCAN_USB_CMD_ARGS] = 1;
  247. usb_fill_bulk_urb(urb, dev->udev,
  248. usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
  249. buf, PCAN_USB_CMD_LEN,
  250. pcan_usb_restart_pending, pdev);
  251. return usb_submit_urb(urb, GFP_ATOMIC);
  252. }
  253. /*
  254. * read serial number from device
  255. */
  256. static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
  257. {
  258. u8 args[PCAN_USB_CMD_ARGS_LEN];
  259. int err;
  260. err = pcan_usb_wait_rsp(dev, 6, 1, args);
  261. if (err) {
  262. netdev_err(dev->netdev, "getting serial failure: %d\n", err);
  263. } else if (serial_number) {
  264. __le32 tmp32;
  265. memcpy(&tmp32, args, 4);
  266. *serial_number = le32_to_cpu(tmp32);
  267. }
  268. return err;
  269. }
  270. /*
  271. * read device id from device
  272. */
  273. static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
  274. {
  275. u8 args[PCAN_USB_CMD_ARGS_LEN];
  276. int err;
  277. err = pcan_usb_wait_rsp(dev, 4, 1, args);
  278. if (err)
  279. netdev_err(dev->netdev, "getting device id failure: %d\n", err);
  280. else if (device_id)
  281. *device_id = args[0];
  282. return err;
  283. }
  284. /*
  285. * update current time ref with received timestamp
  286. */
  287. static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
  288. {
  289. __le16 tmp16;
  290. if ((mc->ptr+2) > mc->end)
  291. return -EINVAL;
  292. memcpy(&tmp16, mc->ptr, 2);
  293. mc->ts16 = le16_to_cpu(tmp16);
  294. if (mc->rec_idx > 0)
  295. peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
  296. else
  297. peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
  298. return 0;
  299. }
  300. /*
  301. * decode received timestamp
  302. */
  303. static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
  304. {
  305. /* only 1st packet supplies a word timestamp */
  306. if (first_packet) {
  307. __le16 tmp16;
  308. if ((mc->ptr + 2) > mc->end)
  309. return -EINVAL;
  310. memcpy(&tmp16, mc->ptr, 2);
  311. mc->ptr += 2;
  312. mc->ts16 = le16_to_cpu(tmp16);
  313. mc->prev_ts8 = mc->ts16 & 0x00ff;
  314. } else {
  315. u8 ts8;
  316. if ((mc->ptr + 1) > mc->end)
  317. return -EINVAL;
  318. ts8 = *mc->ptr++;
  319. if (ts8 < mc->prev_ts8)
  320. mc->ts16 += 0x100;
  321. mc->ts16 &= 0xff00;
  322. mc->ts16 |= ts8;
  323. mc->prev_ts8 = ts8;
  324. }
  325. return 0;
  326. }
  327. static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
  328. u8 status_len)
  329. {
  330. struct sk_buff *skb;
  331. struct can_frame *cf;
  332. struct timeval tv;
  333. enum can_state new_state;
  334. /* ignore this error until 1st ts received */
  335. if (n == PCAN_USB_ERROR_QOVR)
  336. if (!mc->pdev->time_ref.tick_count)
  337. return 0;
  338. new_state = mc->pdev->dev.can.state;
  339. switch (mc->pdev->dev.can.state) {
  340. case CAN_STATE_ERROR_ACTIVE:
  341. if (n & PCAN_USB_ERROR_BUS_LIGHT) {
  342. new_state = CAN_STATE_ERROR_WARNING;
  343. break;
  344. }
  345. case CAN_STATE_ERROR_WARNING:
  346. if (n & PCAN_USB_ERROR_BUS_HEAVY) {
  347. new_state = CAN_STATE_ERROR_PASSIVE;
  348. break;
  349. }
  350. if (n & PCAN_USB_ERROR_BUS_OFF) {
  351. new_state = CAN_STATE_BUS_OFF;
  352. break;
  353. }
  354. if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
  355. /*
  356. * trick to bypass next comparison and process other
  357. * errors
  358. */
  359. new_state = CAN_STATE_MAX;
  360. break;
  361. }
  362. if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) {
  363. /* no error (back to active state) */
  364. mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
  365. return 0;
  366. }
  367. break;
  368. case CAN_STATE_ERROR_PASSIVE:
  369. if (n & PCAN_USB_ERROR_BUS_OFF) {
  370. new_state = CAN_STATE_BUS_OFF;
  371. break;
  372. }
  373. if (n & PCAN_USB_ERROR_BUS_LIGHT) {
  374. new_state = CAN_STATE_ERROR_WARNING;
  375. break;
  376. }
  377. if (n & (PCAN_USB_ERROR_RXQOVR | PCAN_USB_ERROR_QOVR)) {
  378. /*
  379. * trick to bypass next comparison and process other
  380. * errors
  381. */
  382. new_state = CAN_STATE_MAX;
  383. break;
  384. }
  385. if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) {
  386. /* no error (back to active state) */
  387. mc->pdev->dev.can.state = CAN_STATE_ERROR_ACTIVE;
  388. return 0;
  389. }
  390. break;
  391. default:
  392. /* do nothing waiting for restart */
  393. return 0;
  394. }
  395. /* donot post any error if current state didn't change */
  396. if (mc->pdev->dev.can.state == new_state)
  397. return 0;
  398. /* allocate an skb to store the error frame */
  399. skb = alloc_can_err_skb(mc->netdev, &cf);
  400. if (!skb)
  401. return -ENOMEM;
  402. switch (new_state) {
  403. case CAN_STATE_BUS_OFF:
  404. cf->can_id |= CAN_ERR_BUSOFF;
  405. mc->pdev->dev.can.can_stats.bus_off++;
  406. can_bus_off(mc->netdev);
  407. break;
  408. case CAN_STATE_ERROR_PASSIVE:
  409. cf->can_id |= CAN_ERR_CRTL;
  410. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
  411. CAN_ERR_CRTL_RX_PASSIVE;
  412. mc->pdev->dev.can.can_stats.error_passive++;
  413. break;
  414. case CAN_STATE_ERROR_WARNING:
  415. cf->can_id |= CAN_ERR_CRTL;
  416. cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
  417. CAN_ERR_CRTL_RX_WARNING;
  418. mc->pdev->dev.can.can_stats.error_warning++;
  419. break;
  420. default:
  421. /* CAN_STATE_MAX (trick to handle other errors) */
  422. cf->can_id |= CAN_ERR_CRTL;
  423. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  424. mc->netdev->stats.rx_over_errors++;
  425. mc->netdev->stats.rx_errors++;
  426. new_state = mc->pdev->dev.can.state;
  427. break;
  428. }
  429. mc->pdev->dev.can.state = new_state;
  430. if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
  431. struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
  432. peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
  433. hwts->hwtstamp = timeval_to_ktime(tv);
  434. }
  435. mc->netdev->stats.rx_packets++;
  436. mc->netdev->stats.rx_bytes += cf->can_dlc;
  437. netif_rx(skb);
  438. return 0;
  439. }
  440. /*
  441. * decode non-data usb message
  442. */
  443. static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
  444. u8 status_len)
  445. {
  446. u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
  447. u8 f, n;
  448. int err;
  449. /* check whether function and number can be read */
  450. if ((mc->ptr + 2) > mc->end)
  451. return -EINVAL;
  452. f = mc->ptr[PCAN_USB_CMD_FUNC];
  453. n = mc->ptr[PCAN_USB_CMD_NUM];
  454. mc->ptr += PCAN_USB_CMD_ARGS;
  455. if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
  456. int err = pcan_usb_decode_ts(mc, !mc->rec_idx);
  457. if (err)
  458. return err;
  459. }
  460. switch (f) {
  461. case PCAN_USB_REC_ERROR:
  462. err = pcan_usb_decode_error(mc, n, status_len);
  463. if (err)
  464. return err;
  465. break;
  466. case PCAN_USB_REC_ANALOG:
  467. /* analog values (ignored) */
  468. rec_len = 2;
  469. break;
  470. case PCAN_USB_REC_BUSLOAD:
  471. /* bus load (ignored) */
  472. rec_len = 1;
  473. break;
  474. case PCAN_USB_REC_TS:
  475. /* only timestamp */
  476. if (pcan_usb_update_ts(mc))
  477. return -EINVAL;
  478. break;
  479. case PCAN_USB_REC_BUSEVT:
  480. /* error frame/bus event */
  481. if (n & PCAN_USB_ERROR_TXQFULL)
  482. netdev_dbg(mc->netdev, "device Tx queue full)\n");
  483. break;
  484. default:
  485. netdev_err(mc->netdev, "unexpected function %u\n", f);
  486. break;
  487. }
  488. if ((mc->ptr + rec_len) > mc->end)
  489. return -EINVAL;
  490. mc->ptr += rec_len;
  491. return 0;
  492. }
  493. /*
  494. * decode data usb message
  495. */
  496. static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
  497. {
  498. u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
  499. struct sk_buff *skb;
  500. struct can_frame *cf;
  501. struct timeval tv;
  502. struct skb_shared_hwtstamps *hwts;
  503. skb = alloc_can_skb(mc->netdev, &cf);
  504. if (!skb)
  505. return -ENOMEM;
  506. if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
  507. __le32 tmp32;
  508. if ((mc->ptr + 4) > mc->end)
  509. goto decode_failed;
  510. memcpy(&tmp32, mc->ptr, 4);
  511. mc->ptr += 4;
  512. cf->can_id = (le32_to_cpu(tmp32) >> 3) | CAN_EFF_FLAG;
  513. } else {
  514. __le16 tmp16;
  515. if ((mc->ptr + 2) > mc->end)
  516. goto decode_failed;
  517. memcpy(&tmp16, mc->ptr, 2);
  518. mc->ptr += 2;
  519. cf->can_id = le16_to_cpu(tmp16) >> 5;
  520. }
  521. cf->can_dlc = get_can_dlc(rec_len);
  522. /* first data packet timestamp is a word */
  523. if (pcan_usb_decode_ts(mc, !mc->rec_data_idx))
  524. goto decode_failed;
  525. /* read data */
  526. memset(cf->data, 0x0, sizeof(cf->data));
  527. if (status_len & PCAN_USB_STATUSLEN_RTR) {
  528. cf->can_id |= CAN_RTR_FLAG;
  529. } else {
  530. if ((mc->ptr + rec_len) > mc->end)
  531. goto decode_failed;
  532. memcpy(cf->data, mc->ptr, cf->can_dlc);
  533. mc->ptr += rec_len;
  534. }
  535. /* convert timestamp into kernel time */
  536. peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
  537. hwts = skb_hwtstamps(skb);
  538. hwts->hwtstamp = timeval_to_ktime(tv);
  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. init_timer(&pdev->restart_timer);
  652. pdev->restart_timer.function = pcan_usb_restart;
  653. pdev->restart_timer.data = (unsigned long)dev;
  654. /*
  655. * explicit use of dev_xxx() instead of netdev_xxx() here:
  656. * information displayed are related to the device itself, not
  657. * to the canx netdevice.
  658. */
  659. err = pcan_usb_get_serial(dev, &serial_number);
  660. if (err) {
  661. dev_err(dev->netdev->dev.parent,
  662. "unable to read %s serial number (err %d)\n",
  663. pcan_usb.name, err);
  664. return err;
  665. }
  666. dev_info(dev->netdev->dev.parent,
  667. "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
  668. pcan_usb.name, dev->device_rev, serial_number,
  669. pcan_usb.ctrl_count);
  670. return 0;
  671. }
  672. /*
  673. * probe function for new PCAN-USB usb interface
  674. */
  675. static int pcan_usb_probe(struct usb_interface *intf)
  676. {
  677. struct usb_host_interface *if_desc;
  678. int i;
  679. if_desc = intf->altsetting;
  680. /* check interface endpoint addresses */
  681. for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
  682. struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
  683. switch (ep->bEndpointAddress) {
  684. case PCAN_USB_EP_CMDOUT:
  685. case PCAN_USB_EP_CMDIN:
  686. case PCAN_USB_EP_MSGOUT:
  687. case PCAN_USB_EP_MSGIN:
  688. break;
  689. default:
  690. return -ENODEV;
  691. }
  692. }
  693. return 0;
  694. }
  695. /*
  696. * describe the PCAN-USB adapter
  697. */
  698. static const struct can_bittiming_const pcan_usb_const = {
  699. .name = "pcan_usb",
  700. .tseg1_min = 1,
  701. .tseg1_max = 16,
  702. .tseg2_min = 1,
  703. .tseg2_max = 8,
  704. .sjw_max = 4,
  705. .brp_min = 1,
  706. .brp_max = 64,
  707. .brp_inc = 1,
  708. };
  709. const struct peak_usb_adapter pcan_usb = {
  710. .name = "PCAN-USB",
  711. .device_id = PCAN_USB_PRODUCT_ID,
  712. .ctrl_count = 1,
  713. .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
  714. .clock = {
  715. .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
  716. },
  717. .bittiming_const = &pcan_usb_const,
  718. /* size of device private data */
  719. .sizeof_dev_private = sizeof(struct pcan_usb),
  720. /* timestamps usage */
  721. .ts_used_bits = 16,
  722. .ts_period = 24575, /* calibration period in ts. */
  723. .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
  724. .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
  725. /* give here messages in/out endpoints */
  726. .ep_msg_in = PCAN_USB_EP_MSGIN,
  727. .ep_msg_out = {PCAN_USB_EP_MSGOUT},
  728. /* size of rx/tx usb buffers */
  729. .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
  730. .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
  731. /* device callbacks */
  732. .intf_probe = pcan_usb_probe,
  733. .dev_init = pcan_usb_init,
  734. .dev_set_bus = pcan_usb_write_mode,
  735. .dev_set_bittiming = pcan_usb_set_bittiming,
  736. .dev_get_device_id = pcan_usb_get_device_id,
  737. .dev_decode_buf = pcan_usb_decode_buf,
  738. .dev_encode_msg = pcan_usb_encode_msg,
  739. .dev_start = pcan_usb_start,
  740. .dev_restart_async = pcan_usb_restart_async,
  741. };