pcan_usb_pro.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. * CAN driver for PEAK System PCAN-USB Pro adapter
  3. * Derived from the PCAN project file driver/src/pcan_usbpro.c
  4. *
  5. * Copyright (C) 2003-2011 PEAK System-Technik GmbH
  6. * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published
  10. * by the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/netdevice.h>
  18. #include <linux/usb.h>
  19. #include <linux/module.h>
  20. #include <linux/can.h>
  21. #include <linux/can/dev.h>
  22. #include <linux/can/error.h>
  23. #include "pcan_usb_core.h"
  24. #include "pcan_usb_pro.h"
  25. MODULE_SUPPORTED_DEVICE("PEAK-System PCAN-USB Pro adapter");
  26. #define PCAN_USBPRO_CHANNEL_COUNT 2
  27. /* PCAN-USB Pro adapter internal clock (MHz) */
  28. #define PCAN_USBPRO_CRYSTAL_HZ 56000000
  29. /* PCAN-USB Pro command timeout (ms.) */
  30. #define PCAN_USBPRO_COMMAND_TIMEOUT 1000
  31. /* PCAN-USB Pro rx/tx buffers size */
  32. #define PCAN_USBPRO_RX_BUFFER_SIZE 1024
  33. #define PCAN_USBPRO_TX_BUFFER_SIZE 64
  34. #define PCAN_USBPRO_MSG_HEADER_LEN 4
  35. /* some commands responses need to be re-submitted */
  36. #define PCAN_USBPRO_RSP_SUBMIT_MAX 2
  37. #define PCAN_USBPRO_RTR 0x01
  38. #define PCAN_USBPRO_EXT 0x02
  39. #define PCAN_USBPRO_CMD_BUFFER_SIZE 512
  40. /* handle device specific info used by the netdevices */
  41. struct pcan_usb_pro_interface {
  42. struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT];
  43. struct peak_time_ref time_ref;
  44. int cm_ignore_count;
  45. int dev_opened_count;
  46. };
  47. /* device information */
  48. struct pcan_usb_pro_device {
  49. struct peak_usb_device dev;
  50. struct pcan_usb_pro_interface *usb_if;
  51. u32 cached_ccbt;
  52. };
  53. /* internal structure used to handle messages sent to bulk urb */
  54. struct pcan_usb_pro_msg {
  55. u8 *rec_ptr;
  56. int rec_buffer_size;
  57. int rec_buffer_len;
  58. union {
  59. __le16 *rec_cnt_rd;
  60. __le32 *rec_cnt;
  61. u8 *rec_buffer;
  62. } u;
  63. };
  64. /* records sizes table indexed on message id. (8-bits value) */
  65. static u16 pcan_usb_pro_sizeof_rec[256] = {
  66. [PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr),
  67. [PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact),
  68. [PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent),
  69. [PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter),
  70. [PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts),
  71. [PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid),
  72. [PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled),
  73. [PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg),
  74. [PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4,
  75. [PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  76. [PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8,
  77. [PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus),
  78. [PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts),
  79. [PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg),
  80. [PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4,
  81. [PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8,
  82. };
  83. /*
  84. * initialize PCAN-USB Pro message data structure
  85. */
  86. static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr,
  87. int buffer_size)
  88. {
  89. if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN)
  90. return NULL;
  91. pm->u.rec_buffer = (u8 *)buffer_addr;
  92. pm->rec_buffer_size = pm->rec_buffer_len = buffer_size;
  93. pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN;
  94. return pm->rec_ptr;
  95. }
  96. static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm,
  97. void *buffer_addr, int buffer_size)
  98. {
  99. u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size);
  100. if (pr) {
  101. pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN;
  102. *pm->u.rec_cnt = 0;
  103. }
  104. return pr;
  105. }
  106. /*
  107. * add one record to a message being built
  108. */
  109. static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, u8 id, ...)
  110. {
  111. int len, i;
  112. u8 *pc;
  113. va_list ap;
  114. va_start(ap, id);
  115. pc = pm->rec_ptr + 1;
  116. i = 0;
  117. switch (id) {
  118. case PCAN_USBPRO_TXMSG8:
  119. i += 4;
  120. case PCAN_USBPRO_TXMSG4:
  121. i += 4;
  122. case PCAN_USBPRO_TXMSG0:
  123. *pc++ = va_arg(ap, int);
  124. *pc++ = va_arg(ap, int);
  125. *pc++ = va_arg(ap, int);
  126. *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
  127. pc += 4;
  128. memcpy(pc, va_arg(ap, int *), i);
  129. pc += i;
  130. break;
  131. case PCAN_USBPRO_SETBTR:
  132. case PCAN_USBPRO_GETDEVID:
  133. *pc++ = va_arg(ap, int);
  134. pc += 2;
  135. *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
  136. pc += 4;
  137. break;
  138. case PCAN_USBPRO_SETFILTR:
  139. case PCAN_USBPRO_SETBUSACT:
  140. case PCAN_USBPRO_SETSILENT:
  141. *pc++ = va_arg(ap, int);
  142. *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
  143. pc += 2;
  144. break;
  145. case PCAN_USBPRO_SETLED:
  146. *pc++ = va_arg(ap, int);
  147. *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
  148. pc += 2;
  149. *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32));
  150. pc += 4;
  151. break;
  152. case PCAN_USBPRO_SETTS:
  153. pc++;
  154. *(__le16 *)pc = cpu_to_le16(va_arg(ap, int));
  155. pc += 2;
  156. break;
  157. default:
  158. pr_err("%s: %s(): unknown data type %02Xh (%d)\n",
  159. PCAN_USB_DRIVER_NAME, __func__, id, id);
  160. pc--;
  161. break;
  162. }
  163. len = pc - pm->rec_ptr;
  164. if (len > 0) {
  165. *pm->u.rec_cnt = cpu_to_le32(le32_to_cpu(*pm->u.rec_cnt) + 1);
  166. *pm->rec_ptr = id;
  167. pm->rec_ptr = pc;
  168. pm->rec_buffer_len += len;
  169. }
  170. va_end(ap);
  171. return len;
  172. }
  173. /*
  174. * send PCAN-USB Pro command synchronously
  175. */
  176. static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev,
  177. struct pcan_usb_pro_msg *pum)
  178. {
  179. int actual_length;
  180. int err;
  181. /* usb device unregistered? */
  182. if (!(dev->state & PCAN_USB_STATE_CONNECTED))
  183. return 0;
  184. err = usb_bulk_msg(dev->udev,
  185. usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
  186. pum->u.rec_buffer, pum->rec_buffer_len,
  187. &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
  188. if (err)
  189. netdev_err(dev->netdev, "sending command failure: %d\n", err);
  190. return err;
  191. }
  192. /*
  193. * wait for PCAN-USB Pro command response
  194. */
  195. static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev,
  196. struct pcan_usb_pro_msg *pum)
  197. {
  198. u8 req_data_type, req_channel;
  199. int actual_length;
  200. int i, err = 0;
  201. /* usb device unregistered? */
  202. if (!(dev->state & PCAN_USB_STATE_CONNECTED))
  203. return 0;
  204. req_data_type = pum->u.rec_buffer[4];
  205. req_channel = pum->u.rec_buffer[5];
  206. *pum->u.rec_cnt = 0;
  207. for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) {
  208. struct pcan_usb_pro_msg rsp;
  209. union pcan_usb_pro_rec *pr;
  210. u32 r, rec_cnt;
  211. u16 rec_len;
  212. u8 *pc;
  213. err = usb_bulk_msg(dev->udev,
  214. usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN),
  215. pum->u.rec_buffer, pum->rec_buffer_len,
  216. &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT);
  217. if (err) {
  218. netdev_err(dev->netdev, "waiting rsp error %d\n", err);
  219. break;
  220. }
  221. if (actual_length == 0)
  222. continue;
  223. err = -EBADMSG;
  224. if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) {
  225. netdev_err(dev->netdev,
  226. "got abnormal too small rsp (len=%d)\n",
  227. actual_length);
  228. break;
  229. }
  230. pc = pcan_msg_init(&rsp, pum->u.rec_buffer,
  231. actual_length);
  232. rec_cnt = le32_to_cpu(*rsp.u.rec_cnt);
  233. /* loop on records stored into message */
  234. for (r = 0; r < rec_cnt; r++) {
  235. pr = (union pcan_usb_pro_rec *)pc;
  236. rec_len = pcan_usb_pro_sizeof_rec[pr->data_type];
  237. if (!rec_len) {
  238. netdev_err(dev->netdev,
  239. "got unprocessed record in msg\n");
  240. pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer,
  241. actual_length);
  242. break;
  243. }
  244. /* check if response corresponds to request */
  245. if (pr->data_type != req_data_type)
  246. netdev_err(dev->netdev,
  247. "got unwanted rsp %xh: ignored\n",
  248. pr->data_type);
  249. /* check if channel in response corresponds too */
  250. else if ((req_channel != 0xff) && \
  251. (pr->bus_act.channel != req_channel))
  252. netdev_err(dev->netdev,
  253. "got rsp %xh but on chan%u: ignored\n",
  254. req_data_type, pr->bus_act.channel);
  255. /* got the response */
  256. else
  257. return 0;
  258. /* otherwise, go on with next record in message */
  259. pc += rec_len;
  260. }
  261. }
  262. return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err;
  263. }
  264. int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id,
  265. int req_value, void *req_addr, int req_size)
  266. {
  267. int err;
  268. u8 req_type;
  269. unsigned int p;
  270. /* usb device unregistered? */
  271. if (!(dev->state & PCAN_USB_STATE_CONNECTED))
  272. return 0;
  273. req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER;
  274. switch (req_id) {
  275. case PCAN_USBPRO_REQ_FCT:
  276. p = usb_sndctrlpipe(dev->udev, 0);
  277. break;
  278. default:
  279. p = usb_rcvctrlpipe(dev->udev, 0);
  280. req_type |= USB_DIR_IN;
  281. memset(req_addr, '\0', req_size);
  282. break;
  283. }
  284. err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0,
  285. req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT);
  286. if (err < 0) {
  287. netdev_info(dev->netdev,
  288. "unable to request usb[type=%d value=%d] err=%d\n",
  289. req_id, req_value, err);
  290. return err;
  291. }
  292. return 0;
  293. }
  294. static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff)
  295. {
  296. struct pcan_usb_pro_msg um;
  297. pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
  298. pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff);
  299. return pcan_usb_pro_send_cmd(dev, &um);
  300. }
  301. static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt)
  302. {
  303. struct pcan_usb_pro_device *pdev =
  304. container_of(dev, struct pcan_usb_pro_device, dev);
  305. struct pcan_usb_pro_msg um;
  306. pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
  307. pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt);
  308. /* cache the CCBT value to reuse it before next buson */
  309. pdev->cached_ccbt = ccbt;
  310. return pcan_usb_pro_send_cmd(dev, &um);
  311. }
  312. static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff)
  313. {
  314. struct pcan_usb_pro_msg um;
  315. /* if bus=on, be sure the bitrate being set before! */
  316. if (onoff) {
  317. struct pcan_usb_pro_device *pdev =
  318. container_of(dev, struct pcan_usb_pro_device, dev);
  319. pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt);
  320. }
  321. pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
  322. pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff);
  323. return pcan_usb_pro_send_cmd(dev, &um);
  324. }
  325. static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff)
  326. {
  327. struct pcan_usb_pro_msg um;
  328. pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
  329. pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff);
  330. return pcan_usb_pro_send_cmd(dev, &um);
  331. }
  332. static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode)
  333. {
  334. struct pcan_usb_pro_msg um;
  335. pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
  336. pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode);
  337. return pcan_usb_pro_send_cmd(dev, &um);
  338. }
  339. static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode,
  340. u32 timeout)
  341. {
  342. struct pcan_usb_pro_msg um;
  343. pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
  344. pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout);
  345. return pcan_usb_pro_send_cmd(dev, &um);
  346. }
  347. static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev,
  348. u32 *device_id)
  349. {
  350. struct pcan_usb_pro_devid *pdn;
  351. struct pcan_usb_pro_msg um;
  352. int err;
  353. u8 *pc;
  354. pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN);
  355. pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx);
  356. err = pcan_usb_pro_send_cmd(dev, &um);
  357. if (err)
  358. return err;
  359. err = pcan_usb_pro_wait_rsp(dev, &um);
  360. if (err)
  361. return err;
  362. pdn = (struct pcan_usb_pro_devid *)pc;
  363. if (device_id)
  364. *device_id = le32_to_cpu(pdn->serial_num);
  365. return err;
  366. }
  367. static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev,
  368. struct can_bittiming *bt)
  369. {
  370. u32 ccbt;
  371. ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0;
  372. ccbt |= (bt->sjw - 1) << 24;
  373. ccbt |= (bt->phase_seg2 - 1) << 20;
  374. ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */
  375. ccbt |= bt->brp - 1;
  376. netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt);
  377. return pcan_usb_pro_set_bitrate(dev, ccbt);
  378. }
  379. void pcan_usb_pro_restart_complete(struct urb *urb)
  380. {
  381. /* can delete usb resources */
  382. peak_usb_async_complete(urb);
  383. /* notify candev and netdev */
  384. peak_usb_restart_complete(urb->context);
  385. }
  386. /*
  387. * handle restart but in asynchronously way
  388. */
  389. static int pcan_usb_pro_restart_async(struct peak_usb_device *dev,
  390. struct urb *urb, u8 *buf)
  391. {
  392. struct pcan_usb_pro_msg um;
  393. pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN);
  394. pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1);
  395. usb_fill_bulk_urb(urb, dev->udev,
  396. usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT),
  397. buf, PCAN_USB_MAX_CMD_LEN,
  398. pcan_usb_pro_restart_complete, dev);
  399. return usb_submit_urb(urb, GFP_ATOMIC);
  400. }
  401. static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded)
  402. {
  403. u8 *buffer;
  404. int err;
  405. buffer = kmalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL);
  406. if (!buffer)
  407. return -ENOMEM;
  408. buffer[0] = 0;
  409. buffer[1] = !!loaded;
  410. err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT,
  411. PCAN_USBPRO_FCT_DRVLD, buffer,
  412. PCAN_USBPRO_FCT_DRVLD_REQ_LEN);
  413. kfree(buffer);
  414. return err;
  415. }
  416. static inline
  417. struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev)
  418. {
  419. struct pcan_usb_pro_device *pdev =
  420. container_of(dev, struct pcan_usb_pro_device, dev);
  421. return pdev->usb_if;
  422. }
  423. static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if,
  424. struct pcan_usb_pro_rxmsg *rx)
  425. {
  426. const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f;
  427. struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
  428. struct net_device *netdev = dev->netdev;
  429. struct can_frame *can_frame;
  430. struct sk_buff *skb;
  431. struct timeval tv;
  432. struct skb_shared_hwtstamps *hwts;
  433. skb = alloc_can_skb(netdev, &can_frame);
  434. if (!skb)
  435. return -ENOMEM;
  436. can_frame->can_id = le32_to_cpu(rx->id);
  437. can_frame->can_dlc = rx->len & 0x0f;
  438. if (rx->flags & PCAN_USBPRO_EXT)
  439. can_frame->can_id |= CAN_EFF_FLAG;
  440. if (rx->flags & PCAN_USBPRO_RTR)
  441. can_frame->can_id |= CAN_RTR_FLAG;
  442. else
  443. memcpy(can_frame->data, rx->data, can_frame->can_dlc);
  444. peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(rx->ts32), &tv);
  445. hwts = skb_hwtstamps(skb);
  446. hwts->hwtstamp = timeval_to_ktime(tv);
  447. netdev->stats.rx_packets++;
  448. netdev->stats.rx_bytes += can_frame->can_dlc;
  449. netif_rx(skb);
  450. return 0;
  451. }
  452. static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if,
  453. struct pcan_usb_pro_rxstatus *er)
  454. {
  455. const u16 raw_status = le16_to_cpu(er->status);
  456. const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f;
  457. struct peak_usb_device *dev = usb_if->dev[ctrl_idx];
  458. struct net_device *netdev = dev->netdev;
  459. struct can_frame *can_frame;
  460. enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
  461. u8 err_mask = 0;
  462. struct sk_buff *skb;
  463. struct timeval tv;
  464. struct skb_shared_hwtstamps *hwts;
  465. /* nothing should be sent while in BUS_OFF state */
  466. if (dev->can.state == CAN_STATE_BUS_OFF)
  467. return 0;
  468. if (!raw_status) {
  469. /* no error bit (back to active state) */
  470. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  471. return 0;
  472. }
  473. if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN |
  474. PCAN_USBPRO_STATUS_QOVERRUN)) {
  475. /* trick to bypass next comparison and process other errors */
  476. new_state = CAN_STATE_MAX;
  477. }
  478. if (raw_status & PCAN_USBPRO_STATUS_BUS) {
  479. new_state = CAN_STATE_BUS_OFF;
  480. } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) {
  481. u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16;
  482. u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24;
  483. if (rx_err_cnt > 127)
  484. err_mask |= CAN_ERR_CRTL_RX_PASSIVE;
  485. else if (rx_err_cnt > 96)
  486. err_mask |= CAN_ERR_CRTL_RX_WARNING;
  487. if (tx_err_cnt > 127)
  488. err_mask |= CAN_ERR_CRTL_TX_PASSIVE;
  489. else if (tx_err_cnt > 96)
  490. err_mask |= CAN_ERR_CRTL_TX_WARNING;
  491. if (err_mask & (CAN_ERR_CRTL_RX_WARNING |
  492. CAN_ERR_CRTL_TX_WARNING))
  493. new_state = CAN_STATE_ERROR_WARNING;
  494. else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE |
  495. CAN_ERR_CRTL_TX_PASSIVE))
  496. new_state = CAN_STATE_ERROR_PASSIVE;
  497. }
  498. /* donot post any error if current state didn't change */
  499. if (dev->can.state == new_state)
  500. return 0;
  501. /* allocate an skb to store the error frame */
  502. skb = alloc_can_err_skb(netdev, &can_frame);
  503. if (!skb)
  504. return -ENOMEM;
  505. switch (new_state) {
  506. case CAN_STATE_BUS_OFF:
  507. can_frame->can_id |= CAN_ERR_BUSOFF;
  508. dev->can.can_stats.bus_off++;
  509. can_bus_off(netdev);
  510. break;
  511. case CAN_STATE_ERROR_PASSIVE:
  512. can_frame->can_id |= CAN_ERR_CRTL;
  513. can_frame->data[1] |= err_mask;
  514. dev->can.can_stats.error_passive++;
  515. break;
  516. case CAN_STATE_ERROR_WARNING:
  517. can_frame->can_id |= CAN_ERR_CRTL;
  518. can_frame->data[1] |= err_mask;
  519. dev->can.can_stats.error_warning++;
  520. break;
  521. case CAN_STATE_ERROR_ACTIVE:
  522. break;
  523. default:
  524. /* CAN_STATE_MAX (trick to handle other errors) */
  525. if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
  526. can_frame->can_id |= CAN_ERR_PROT;
  527. can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
  528. netdev->stats.rx_over_errors++;
  529. netdev->stats.rx_errors++;
  530. }
  531. if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
  532. can_frame->can_id |= CAN_ERR_CRTL;
  533. can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  534. netdev->stats.rx_over_errors++;
  535. netdev->stats.rx_errors++;
  536. }
  537. new_state = CAN_STATE_ERROR_ACTIVE;
  538. break;
  539. }
  540. dev->can.state = new_state;
  541. peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
  542. hwts = skb_hwtstamps(skb);
  543. hwts->hwtstamp = timeval_to_ktime(tv);
  544. netdev->stats.rx_packets++;
  545. netdev->stats.rx_bytes += can_frame->can_dlc;
  546. netif_rx(skb);
  547. return 0;
  548. }
  549. static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
  550. struct pcan_usb_pro_rxts *ts)
  551. {
  552. /* should wait until clock is stabilized */
  553. if (usb_if->cm_ignore_count > 0)
  554. usb_if->cm_ignore_count--;
  555. else
  556. peak_usb_set_ts_now(&usb_if->time_ref,
  557. le32_to_cpu(ts->ts64[1]));
  558. }
  559. /*
  560. * callback for bulk IN urb
  561. */
  562. static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
  563. {
  564. struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
  565. struct net_device *netdev = dev->netdev;
  566. struct pcan_usb_pro_msg usb_msg;
  567. u8 *rec_ptr, *msg_end;
  568. u16 rec_cnt;
  569. int err = 0;
  570. rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
  571. urb->actual_length);
  572. if (!rec_ptr) {
  573. netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
  574. return -EINVAL;
  575. }
  576. /* loop reading all the records from the incoming message */
  577. msg_end = urb->transfer_buffer + urb->actual_length;
  578. rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
  579. for (; rec_cnt > 0; rec_cnt--) {
  580. union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
  581. u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
  582. if (!sizeof_rec) {
  583. netdev_err(netdev,
  584. "got unsupported rec in usb msg:\n");
  585. err = -ENOTSUPP;
  586. break;
  587. }
  588. /* check if the record goes out of current packet */
  589. if (rec_ptr + sizeof_rec > msg_end) {
  590. netdev_err(netdev,
  591. "got frag rec: should inc usb rx buf size\n");
  592. err = -EBADMSG;
  593. break;
  594. }
  595. switch (pr->data_type) {
  596. case PCAN_USBPRO_RXMSG8:
  597. case PCAN_USBPRO_RXMSG4:
  598. case PCAN_USBPRO_RXMSG0:
  599. case PCAN_USBPRO_RXRTR:
  600. err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
  601. if (err < 0)
  602. goto fail;
  603. break;
  604. case PCAN_USBPRO_RXSTATUS:
  605. err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
  606. if (err < 0)
  607. goto fail;
  608. break;
  609. case PCAN_USBPRO_RXTS:
  610. pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
  611. break;
  612. default:
  613. netdev_err(netdev,
  614. "unhandled rec type 0x%02x (%d): ignored\n",
  615. pr->data_type, pr->data_type);
  616. break;
  617. }
  618. rec_ptr += sizeof_rec;
  619. }
  620. fail:
  621. if (err)
  622. pcan_dump_mem("received msg",
  623. urb->transfer_buffer, urb->actual_length);
  624. return err;
  625. }
  626. static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
  627. struct sk_buff *skb, u8 *obuf, size_t *size)
  628. {
  629. struct can_frame *cf = (struct can_frame *)skb->data;
  630. u8 data_type, len, flags;
  631. struct pcan_usb_pro_msg usb_msg;
  632. pcan_msg_init_empty(&usb_msg, obuf, *size);
  633. if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
  634. data_type = PCAN_USBPRO_TXMSG0;
  635. else if (cf->can_dlc <= 4)
  636. data_type = PCAN_USBPRO_TXMSG4;
  637. else
  638. data_type = PCAN_USBPRO_TXMSG8;
  639. len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
  640. flags = 0;
  641. if (cf->can_id & CAN_EFF_FLAG)
  642. flags |= 0x02;
  643. if (cf->can_id & CAN_RTR_FLAG)
  644. flags |= 0x01;
  645. pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
  646. cf->data);
  647. *size = usb_msg.rec_buffer_len;
  648. return 0;
  649. }
  650. static int pcan_usb_pro_start(struct peak_usb_device *dev)
  651. {
  652. struct pcan_usb_pro_device *pdev =
  653. container_of(dev, struct pcan_usb_pro_device, dev);
  654. int err;
  655. err = pcan_usb_pro_set_silent(dev,
  656. dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
  657. if (err)
  658. return err;
  659. /* filter mode: 0-> All OFF; 1->bypass */
  660. err = pcan_usb_pro_set_filter(dev, 1);
  661. if (err)
  662. return err;
  663. /* opening first device: */
  664. if (pdev->usb_if->dev_opened_count == 0) {
  665. /* reset time_ref */
  666. peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
  667. /* ask device to send ts messages */
  668. err = pcan_usb_pro_set_ts(dev, 1);
  669. }
  670. pdev->usb_if->dev_opened_count++;
  671. return err;
  672. }
  673. /*
  674. * stop interface
  675. * (last chance before set bus off)
  676. */
  677. static int pcan_usb_pro_stop(struct peak_usb_device *dev)
  678. {
  679. struct pcan_usb_pro_device *pdev =
  680. container_of(dev, struct pcan_usb_pro_device, dev);
  681. /* turn off ts msgs for that interface if no other dev opened */
  682. if (pdev->usb_if->dev_opened_count == 1)
  683. pcan_usb_pro_set_ts(dev, 0);
  684. pdev->usb_if->dev_opened_count--;
  685. return 0;
  686. }
  687. /*
  688. * called when probing to initialize a device object.
  689. */
  690. static int pcan_usb_pro_init(struct peak_usb_device *dev)
  691. {
  692. struct pcan_usb_pro_device *pdev =
  693. container_of(dev, struct pcan_usb_pro_device, dev);
  694. struct pcan_usb_pro_interface *usb_if = NULL;
  695. struct pcan_usb_pro_fwinfo *fi = NULL;
  696. struct pcan_usb_pro_blinfo *bi = NULL;
  697. int err;
  698. /* do this for 1st channel only */
  699. if (!dev->prev_siblings) {
  700. /* allocate netdevices common structure attached to first one */
  701. usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
  702. GFP_KERNEL);
  703. fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL);
  704. bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL);
  705. if (!usb_if || !fi || !bi) {
  706. err = -ENOMEM;
  707. goto err_out;
  708. }
  709. /* number of ts msgs to ignore before taking one into account */
  710. usb_if->cm_ignore_count = 5;
  711. /*
  712. * explicit use of dev_xxx() instead of netdev_xxx() here:
  713. * information displayed are related to the device itself, not
  714. * to the canx netdevices.
  715. */
  716. err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
  717. PCAN_USBPRO_INFO_FW,
  718. fi, sizeof(*fi));
  719. if (err) {
  720. dev_err(dev->netdev->dev.parent,
  721. "unable to read %s firmware info (err %d)\n",
  722. pcan_usb_pro.name, err);
  723. goto err_out;
  724. }
  725. err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
  726. PCAN_USBPRO_INFO_BL,
  727. bi, sizeof(*bi));
  728. if (err) {
  729. dev_err(dev->netdev->dev.parent,
  730. "unable to read %s bootloader info (err %d)\n",
  731. pcan_usb_pro.name, err);
  732. goto err_out;
  733. }
  734. /* tell the device the can driver is running */
  735. err = pcan_usb_pro_drv_loaded(dev, 1);
  736. if (err)
  737. goto err_out;
  738. dev_info(dev->netdev->dev.parent,
  739. "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
  740. pcan_usb_pro.name,
  741. bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo,
  742. pcan_usb_pro.ctrl_count);
  743. } else {
  744. usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
  745. }
  746. pdev->usb_if = usb_if;
  747. usb_if->dev[dev->ctrl_idx] = dev;
  748. /* set LED in default state (end of init phase) */
  749. pcan_usb_pro_set_led(dev, 0, 1);
  750. kfree(bi);
  751. kfree(fi);
  752. return 0;
  753. err_out:
  754. kfree(bi);
  755. kfree(fi);
  756. kfree(usb_if);
  757. return err;
  758. }
  759. static void pcan_usb_pro_exit(struct peak_usb_device *dev)
  760. {
  761. struct pcan_usb_pro_device *pdev =
  762. container_of(dev, struct pcan_usb_pro_device, dev);
  763. /*
  764. * when rmmod called before unplug and if down, should reset things
  765. * before leaving
  766. */
  767. if (dev->can.state != CAN_STATE_STOPPED) {
  768. /* set bus off on the corresponding channel */
  769. pcan_usb_pro_set_bus(dev, 0);
  770. }
  771. /* if channel #0 (only) */
  772. if (dev->ctrl_idx == 0) {
  773. /* turn off calibration message if any device were opened */
  774. if (pdev->usb_if->dev_opened_count > 0)
  775. pcan_usb_pro_set_ts(dev, 0);
  776. /* tell the PCAN-USB Pro device the driver is being unloaded */
  777. pcan_usb_pro_drv_loaded(dev, 0);
  778. }
  779. }
  780. /*
  781. * called when PCAN-USB Pro adapter is unplugged
  782. */
  783. static void pcan_usb_pro_free(struct peak_usb_device *dev)
  784. {
  785. /* last device: can free pcan_usb_pro_interface object now */
  786. if (!dev->prev_siblings && !dev->next_siblings)
  787. kfree(pcan_usb_pro_dev_if(dev));
  788. }
  789. /*
  790. * probe function for new PCAN-USB Pro usb interface
  791. */
  792. int pcan_usb_pro_probe(struct usb_interface *intf)
  793. {
  794. struct usb_host_interface *if_desc;
  795. int i;
  796. if_desc = intf->altsetting;
  797. /* check interface endpoint addresses */
  798. for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
  799. struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
  800. /*
  801. * below is the list of valid ep addreses. Any other ep address
  802. * is considered as not-CAN interface address => no dev created
  803. */
  804. switch (ep->bEndpointAddress) {
  805. case PCAN_USBPRO_EP_CMDOUT:
  806. case PCAN_USBPRO_EP_CMDIN:
  807. case PCAN_USBPRO_EP_MSGOUT_0:
  808. case PCAN_USBPRO_EP_MSGOUT_1:
  809. case PCAN_USBPRO_EP_MSGIN:
  810. case PCAN_USBPRO_EP_UNUSED:
  811. break;
  812. default:
  813. return -ENODEV;
  814. }
  815. }
  816. return 0;
  817. }
  818. /*
  819. * describe the PCAN-USB Pro adapter
  820. */
  821. static const struct can_bittiming_const pcan_usb_pro_const = {
  822. .name = "pcan_usb_pro",
  823. .tseg1_min = 1,
  824. .tseg1_max = 16,
  825. .tseg2_min = 1,
  826. .tseg2_max = 8,
  827. .sjw_max = 4,
  828. .brp_min = 1,
  829. .brp_max = 1024,
  830. .brp_inc = 1,
  831. };
  832. const struct peak_usb_adapter pcan_usb_pro = {
  833. .name = "PCAN-USB Pro",
  834. .device_id = PCAN_USBPRO_PRODUCT_ID,
  835. .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
  836. .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY,
  837. .clock = {
  838. .freq = PCAN_USBPRO_CRYSTAL_HZ,
  839. },
  840. .bittiming_const = &pcan_usb_pro_const,
  841. /* size of device private data */
  842. .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
  843. /* timestamps usage */
  844. .ts_used_bits = 32,
  845. .ts_period = 1000000, /* calibration period in ts. */
  846. .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
  847. .us_per_ts_shift = 0,
  848. /* give here messages in/out endpoints */
  849. .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
  850. .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
  851. /* size of rx/tx usb buffers */
  852. .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
  853. .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
  854. /* device callbacks */
  855. .intf_probe = pcan_usb_pro_probe,
  856. .dev_init = pcan_usb_pro_init,
  857. .dev_exit = pcan_usb_pro_exit,
  858. .dev_free = pcan_usb_pro_free,
  859. .dev_set_bus = pcan_usb_pro_set_bus,
  860. .dev_set_bittiming = pcan_usb_pro_set_bittiming,
  861. .dev_get_device_id = pcan_usb_pro_get_device_id,
  862. .dev_decode_buf = pcan_usb_pro_decode_buf,
  863. .dev_encode_msg = pcan_usb_pro_encode_msg,
  864. .dev_start = pcan_usb_pro_start,
  865. .dev_stop = pcan_usb_pro_stop,
  866. .dev_restart_async = pcan_usb_pro_restart_async,
  867. };