pcan_usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. u32 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. u16 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. u16 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. can_bus_off(mc->netdev);
  406. break;
  407. case CAN_STATE_ERROR_PASSIVE:
  408. cf->can_id |= CAN_ERR_CRTL;
  409. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE |
  410. CAN_ERR_CRTL_RX_PASSIVE;
  411. mc->pdev->dev.can.can_stats.error_passive++;
  412. break;
  413. case CAN_STATE_ERROR_WARNING:
  414. cf->can_id |= CAN_ERR_CRTL;
  415. cf->data[1] |= CAN_ERR_CRTL_TX_WARNING |
  416. CAN_ERR_CRTL_RX_WARNING;
  417. mc->pdev->dev.can.can_stats.error_warning++;
  418. break;
  419. default:
  420. /* CAN_STATE_MAX (trick to handle other errors) */
  421. cf->can_id |= CAN_ERR_CRTL;
  422. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  423. mc->netdev->stats.rx_over_errors++;
  424. mc->netdev->stats.rx_errors++;
  425. new_state = mc->pdev->dev.can.state;
  426. break;
  427. }
  428. mc->pdev->dev.can.state = new_state;
  429. if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
  430. struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
  431. peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
  432. hwts->hwtstamp = timeval_to_ktime(tv);
  433. }
  434. netif_rx(skb);
  435. mc->netdev->stats.rx_packets++;
  436. mc->netdev->stats.rx_bytes += cf->can_dlc;
  437. return 0;
  438. }
  439. /*
  440. * decode non-data usb message
  441. */
  442. static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
  443. u8 status_len)
  444. {
  445. u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
  446. u8 f, n;
  447. int err;
  448. /* check whether function and number can be read */
  449. if ((mc->ptr + 2) > mc->end)
  450. return -EINVAL;
  451. f = mc->ptr[PCAN_USB_CMD_FUNC];
  452. n = mc->ptr[PCAN_USB_CMD_NUM];
  453. mc->ptr += PCAN_USB_CMD_ARGS;
  454. if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
  455. int err = pcan_usb_decode_ts(mc, !mc->rec_idx);
  456. if (err)
  457. return err;
  458. }
  459. switch (f) {
  460. case PCAN_USB_REC_ERROR:
  461. err = pcan_usb_decode_error(mc, n, status_len);
  462. if (err)
  463. return err;
  464. break;
  465. case PCAN_USB_REC_ANALOG:
  466. /* analog values (ignored) */
  467. rec_len = 2;
  468. break;
  469. case PCAN_USB_REC_BUSLOAD:
  470. /* bus load (ignored) */
  471. rec_len = 1;
  472. break;
  473. case PCAN_USB_REC_TS:
  474. /* only timestamp */
  475. if (pcan_usb_update_ts(mc))
  476. return -EINVAL;
  477. break;
  478. case PCAN_USB_REC_BUSEVT:
  479. /* error frame/bus event */
  480. if (n & PCAN_USB_ERROR_TXQFULL)
  481. netdev_dbg(mc->netdev, "device Tx queue full)\n");
  482. break;
  483. default:
  484. netdev_err(mc->netdev, "unexpected function %u\n", f);
  485. break;
  486. }
  487. if ((mc->ptr + rec_len) > mc->end)
  488. return -EINVAL;
  489. mc->ptr += rec_len;
  490. return 0;
  491. }
  492. /*
  493. * decode data usb message
  494. */
  495. static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
  496. {
  497. u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
  498. struct sk_buff *skb;
  499. struct can_frame *cf;
  500. struct timeval tv;
  501. struct skb_shared_hwtstamps *hwts;
  502. skb = alloc_can_skb(mc->netdev, &cf);
  503. if (!skb)
  504. return -ENOMEM;
  505. if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
  506. u32 tmp32;
  507. if ((mc->ptr + 4) > mc->end)
  508. goto decode_failed;
  509. memcpy(&tmp32, mc->ptr, 4);
  510. mc->ptr += 4;
  511. cf->can_id = le32_to_cpu(tmp32 >> 3) | CAN_EFF_FLAG;
  512. } else {
  513. u16 tmp16;
  514. if ((mc->ptr + 2) > mc->end)
  515. goto decode_failed;
  516. memcpy(&tmp16, mc->ptr, 2);
  517. mc->ptr += 2;
  518. cf->can_id = le16_to_cpu(tmp16 >> 5);
  519. }
  520. cf->can_dlc = get_can_dlc(rec_len);
  521. /* first data packet timestamp is a word */
  522. if (pcan_usb_decode_ts(mc, !mc->rec_data_idx))
  523. goto decode_failed;
  524. /* read data */
  525. memset(cf->data, 0x0, sizeof(cf->data));
  526. if (status_len & PCAN_USB_STATUSLEN_RTR) {
  527. cf->can_id |= CAN_RTR_FLAG;
  528. } else {
  529. if ((mc->ptr + rec_len) > mc->end)
  530. goto decode_failed;
  531. memcpy(cf->data, mc->ptr, cf->can_dlc);
  532. mc->ptr += rec_len;
  533. }
  534. /* convert timestamp into kernel time */
  535. peak_usb_get_ts_tv(&mc->pdev->time_ref, mc->ts16, &tv);
  536. hwts = skb_hwtstamps(skb);
  537. hwts->hwtstamp = timeval_to_ktime(tv);
  538. /* push the skb */
  539. netif_rx(skb);
  540. /* update statistics */
  541. mc->netdev->stats.rx_packets++;
  542. mc->netdev->stats.rx_bytes += cf->can_dlc;
  543. return 0;
  544. decode_failed:
  545. dev_kfree_skb(skb);
  546. return -EINVAL;
  547. }
  548. /*
  549. * process incoming message
  550. */
  551. static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
  552. {
  553. struct pcan_usb_msg_context mc = {
  554. .rec_cnt = ibuf[1],
  555. .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
  556. .end = ibuf + lbuf,
  557. .netdev = dev->netdev,
  558. .pdev = container_of(dev, struct pcan_usb, dev),
  559. };
  560. int err;
  561. for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
  562. u8 sl = *mc.ptr++;
  563. /* handle status and error frames here */
  564. if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
  565. err = pcan_usb_decode_status(&mc, sl);
  566. /* handle normal can frames here */
  567. } else {
  568. err = pcan_usb_decode_data(&mc, sl);
  569. mc.rec_data_idx++;
  570. }
  571. }
  572. return err;
  573. }
  574. /*
  575. * process any incoming buffer
  576. */
  577. static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
  578. {
  579. int err = 0;
  580. if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
  581. err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
  582. urb->actual_length);
  583. } else if (urb->actual_length > 0) {
  584. netdev_err(dev->netdev, "usb message length error (%u)\n",
  585. urb->actual_length);
  586. err = -EINVAL;
  587. }
  588. return err;
  589. }
  590. /*
  591. * process outgoing packet
  592. */
  593. static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
  594. u8 *obuf, size_t *size)
  595. {
  596. struct net_device *netdev = dev->netdev;
  597. struct net_device_stats *stats = &netdev->stats;
  598. struct can_frame *cf = (struct can_frame *)skb->data;
  599. u8 *pc;
  600. obuf[0] = 2;
  601. obuf[1] = 1;
  602. pc = obuf + PCAN_USB_MSG_HEADER_LEN;
  603. /* status/len byte */
  604. *pc = cf->can_dlc;
  605. if (cf->can_id & CAN_RTR_FLAG)
  606. *pc |= PCAN_USB_STATUSLEN_RTR;
  607. /* can id */
  608. if (cf->can_id & CAN_EFF_FLAG) {
  609. __le32 tmp32 = cpu_to_le32((cf->can_id & CAN_ERR_MASK) << 3);
  610. *pc |= PCAN_USB_STATUSLEN_EXT_ID;
  611. memcpy(++pc, &tmp32, 4);
  612. pc += 4;
  613. } else {
  614. __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5);
  615. memcpy(++pc, &tmp16, 2);
  616. pc += 2;
  617. }
  618. /* can data */
  619. if (!(cf->can_id & CAN_RTR_FLAG)) {
  620. memcpy(pc, cf->data, cf->can_dlc);
  621. pc += cf->can_dlc;
  622. }
  623. obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
  624. return 0;
  625. }
  626. /*
  627. * start interface
  628. */
  629. static int pcan_usb_start(struct peak_usb_device *dev)
  630. {
  631. struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
  632. /* number of bits used in timestamps read from adapter struct */
  633. peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
  634. /* if revision greater than 3, can put silent mode on/off */
  635. if (dev->device_rev > 3) {
  636. int err;
  637. err = pcan_usb_set_silent(dev,
  638. dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
  639. if (err)
  640. return err;
  641. }
  642. return pcan_usb_set_ext_vcc(dev, 0);
  643. }
  644. static int pcan_usb_init(struct peak_usb_device *dev)
  645. {
  646. struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
  647. u32 serial_number;
  648. int err;
  649. /* initialize a timer needed to wait for hardware restart */
  650. init_timer(&pdev->restart_timer);
  651. pdev->restart_timer.function = pcan_usb_restart;
  652. pdev->restart_timer.data = (unsigned long)dev;
  653. /*
  654. * explicit use of dev_xxx() instead of netdev_xxx() here:
  655. * information displayed are related to the device itself, not
  656. * to the canx netdevice.
  657. */
  658. err = pcan_usb_get_serial(dev, &serial_number);
  659. if (err) {
  660. dev_err(dev->netdev->dev.parent,
  661. "unable to read %s serial number (err %d)\n",
  662. pcan_usb.name, err);
  663. return err;
  664. }
  665. dev_info(dev->netdev->dev.parent,
  666. "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
  667. pcan_usb.name, dev->device_rev, serial_number,
  668. pcan_usb.ctrl_count);
  669. return 0;
  670. }
  671. /*
  672. * probe function for new PCAN-USB usb interface
  673. */
  674. static int pcan_usb_probe(struct usb_interface *intf)
  675. {
  676. struct usb_host_interface *if_desc;
  677. int i;
  678. if_desc = intf->altsetting;
  679. /* check interface endpoint addresses */
  680. for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
  681. struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
  682. switch (ep->bEndpointAddress) {
  683. case PCAN_USB_EP_CMDOUT:
  684. case PCAN_USB_EP_CMDIN:
  685. case PCAN_USB_EP_MSGOUT:
  686. case PCAN_USB_EP_MSGIN:
  687. break;
  688. default:
  689. return -ENODEV;
  690. }
  691. }
  692. return 0;
  693. }
  694. /*
  695. * describe the PCAN-USB adapter
  696. */
  697. struct peak_usb_adapter pcan_usb = {
  698. .name = "PCAN-USB",
  699. .device_id = PCAN_USB_PRODUCT_ID,
  700. .ctrl_count = 1,
  701. .clock = {
  702. .freq = PCAN_USB_CRYSTAL_HZ / 2 ,
  703. },
  704. .bittiming_const = {
  705. .name = "pcan_usb",
  706. .tseg1_min = 1,
  707. .tseg1_max = 16,
  708. .tseg2_min = 1,
  709. .tseg2_max = 8,
  710. .sjw_max = 4,
  711. .brp_min = 1,
  712. .brp_max = 64,
  713. .brp_inc = 1,
  714. },
  715. /* size of device private data */
  716. .sizeof_dev_private = sizeof(struct pcan_usb),
  717. /* timestamps usage */
  718. .ts_used_bits = 16,
  719. .ts_period = 24575, /* calibration period in ts. */
  720. .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */
  721. .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */
  722. /* give here messages in/out endpoints */
  723. .ep_msg_in = PCAN_USB_EP_MSGIN,
  724. .ep_msg_out = {PCAN_USB_EP_MSGOUT},
  725. /* size of rx/tx usb buffers */
  726. .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
  727. .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
  728. /* device callbacks */
  729. .intf_probe = pcan_usb_probe,
  730. .dev_init = pcan_usb_init,
  731. .dev_set_bus = pcan_usb_write_mode,
  732. .dev_set_bittiming = pcan_usb_set_bittiming,
  733. .dev_get_device_id = pcan_usb_get_device_id,
  734. .dev_decode_buf = pcan_usb_decode_buf,
  735. .dev_encode_msg = pcan_usb_encode_msg,
  736. .dev_start = pcan_usb_start,
  737. .dev_restart_async = pcan_usb_restart_async,
  738. };