pcan_usb_pro.c 26 KB

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