pcan_usb_pro.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. pcan_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. 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. netif_rx(skb);
  448. netdev->stats.rx_packets++;
  449. netdev->stats.rx_bytes += can_frame->can_dlc;
  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 u32 raw_status = le32_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. can_bus_off(netdev);
  509. break;
  510. case CAN_STATE_ERROR_PASSIVE:
  511. can_frame->can_id |= CAN_ERR_CRTL;
  512. can_frame->data[1] |= err_mask;
  513. dev->can.can_stats.error_passive++;
  514. break;
  515. case CAN_STATE_ERROR_WARNING:
  516. can_frame->can_id |= CAN_ERR_CRTL;
  517. can_frame->data[1] |= err_mask;
  518. dev->can.can_stats.error_warning++;
  519. break;
  520. case CAN_STATE_ERROR_ACTIVE:
  521. break;
  522. default:
  523. /* CAN_STATE_MAX (trick to handle other errors) */
  524. if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) {
  525. can_frame->can_id |= CAN_ERR_PROT;
  526. can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD;
  527. netdev->stats.rx_over_errors++;
  528. netdev->stats.rx_errors++;
  529. }
  530. if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) {
  531. can_frame->can_id |= CAN_ERR_CRTL;
  532. can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  533. netdev->stats.rx_over_errors++;
  534. netdev->stats.rx_errors++;
  535. }
  536. new_state = CAN_STATE_ERROR_ACTIVE;
  537. break;
  538. }
  539. dev->can.state = new_state;
  540. peak_usb_get_ts_tv(&usb_if->time_ref, le32_to_cpu(er->ts32), &tv);
  541. hwts = skb_hwtstamps(skb);
  542. hwts->hwtstamp = timeval_to_ktime(tv);
  543. netif_rx(skb);
  544. netdev->stats.rx_packets++;
  545. netdev->stats.rx_bytes += can_frame->can_dlc;
  546. return 0;
  547. }
  548. static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if,
  549. struct pcan_usb_pro_rxts *ts)
  550. {
  551. /* should wait until clock is stabilized */
  552. if (usb_if->cm_ignore_count > 0)
  553. usb_if->cm_ignore_count--;
  554. else
  555. peak_usb_set_ts_now(&usb_if->time_ref,
  556. le32_to_cpu(ts->ts64[1]));
  557. }
  558. /*
  559. * callback for bulk IN urb
  560. */
  561. static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb)
  562. {
  563. struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev);
  564. struct net_device *netdev = dev->netdev;
  565. struct pcan_usb_pro_msg usb_msg;
  566. u8 *rec_ptr, *msg_end;
  567. u16 rec_cnt;
  568. int err = 0;
  569. rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer,
  570. urb->actual_length);
  571. if (!rec_ptr) {
  572. netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length);
  573. return -EINVAL;
  574. }
  575. /* loop reading all the records from the incoming message */
  576. msg_end = urb->transfer_buffer + urb->actual_length;
  577. rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd);
  578. for (; rec_cnt > 0; rec_cnt--) {
  579. union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr;
  580. u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type];
  581. if (!sizeof_rec) {
  582. netdev_err(netdev,
  583. "got unsupported rec in usb msg:\n");
  584. err = -ENOTSUPP;
  585. break;
  586. }
  587. /* check if the record goes out of current packet */
  588. if (rec_ptr + sizeof_rec > msg_end) {
  589. netdev_err(netdev,
  590. "got frag rec: should inc usb rx buf size\n");
  591. err = -EBADMSG;
  592. break;
  593. }
  594. switch (pr->data_type) {
  595. case PCAN_USBPRO_RXMSG8:
  596. case PCAN_USBPRO_RXMSG4:
  597. case PCAN_USBPRO_RXMSG0:
  598. case PCAN_USBPRO_RXRTR:
  599. err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg);
  600. if (err < 0)
  601. goto fail;
  602. break;
  603. case PCAN_USBPRO_RXSTATUS:
  604. err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status);
  605. if (err < 0)
  606. goto fail;
  607. break;
  608. case PCAN_USBPRO_RXTS:
  609. pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts);
  610. break;
  611. default:
  612. netdev_err(netdev,
  613. "unhandled rec type 0x%02x (%d): ignored\n",
  614. pr->data_type, pr->data_type);
  615. break;
  616. }
  617. rec_ptr += sizeof_rec;
  618. }
  619. fail:
  620. if (err)
  621. pcan_dump_mem("received msg",
  622. urb->transfer_buffer, urb->actual_length);
  623. return err;
  624. }
  625. static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev,
  626. struct sk_buff *skb, u8 *obuf, size_t *size)
  627. {
  628. struct can_frame *cf = (struct can_frame *)skb->data;
  629. u8 data_type, len, flags;
  630. struct pcan_usb_pro_msg usb_msg;
  631. pcan_msg_init_empty(&usb_msg, obuf, *size);
  632. if ((cf->can_id & CAN_RTR_FLAG) || (cf->can_dlc == 0))
  633. data_type = PCAN_USBPRO_TXMSG0;
  634. else if (cf->can_dlc <= 4)
  635. data_type = PCAN_USBPRO_TXMSG4;
  636. else
  637. data_type = PCAN_USBPRO_TXMSG8;
  638. len = (dev->ctrl_idx << 4) | (cf->can_dlc & 0x0f);
  639. flags = 0;
  640. if (cf->can_id & CAN_EFF_FLAG)
  641. flags |= 0x02;
  642. if (cf->can_id & CAN_RTR_FLAG)
  643. flags |= 0x01;
  644. pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id,
  645. cf->data);
  646. *size = usb_msg.rec_buffer_len;
  647. return 0;
  648. }
  649. static int pcan_usb_pro_start(struct peak_usb_device *dev)
  650. {
  651. struct pcan_usb_pro_device *pdev =
  652. container_of(dev, struct pcan_usb_pro_device, dev);
  653. int err;
  654. err = pcan_usb_pro_set_silent(dev,
  655. dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
  656. if (err)
  657. return err;
  658. /* filter mode: 0-> All OFF; 1->bypass */
  659. err = pcan_usb_pro_set_filter(dev, 1);
  660. if (err)
  661. return err;
  662. /* opening first device: */
  663. if (pdev->usb_if->dev_opened_count == 0) {
  664. /* reset time_ref */
  665. peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro);
  666. /* ask device to send ts messages */
  667. err = pcan_usb_pro_set_ts(dev, 1);
  668. }
  669. pdev->usb_if->dev_opened_count++;
  670. return err;
  671. }
  672. /*
  673. * stop interface
  674. * (last chance before set bus off)
  675. */
  676. static int pcan_usb_pro_stop(struct peak_usb_device *dev)
  677. {
  678. struct pcan_usb_pro_device *pdev =
  679. container_of(dev, struct pcan_usb_pro_device, dev);
  680. /* turn off ts msgs for that interface if no other dev opened */
  681. if (pdev->usb_if->dev_opened_count == 1)
  682. pcan_usb_pro_set_ts(dev, 0);
  683. pdev->usb_if->dev_opened_count--;
  684. return 0;
  685. }
  686. /*
  687. * called when probing to initialize a device object.
  688. */
  689. static int pcan_usb_pro_init(struct peak_usb_device *dev)
  690. {
  691. struct pcan_usb_pro_interface *usb_if;
  692. struct pcan_usb_pro_device *pdev =
  693. container_of(dev, struct pcan_usb_pro_device, dev);
  694. /* do this for 1st channel only */
  695. if (!dev->prev_siblings) {
  696. struct pcan_usb_pro_fwinfo fi;
  697. struct pcan_usb_pro_blinfo bi;
  698. int err;
  699. /* allocate netdevices common structure attached to first one */
  700. usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface),
  701. GFP_KERNEL);
  702. if (!usb_if)
  703. return -ENOMEM;
  704. /* number of ts msgs to ignore before taking one into account */
  705. usb_if->cm_ignore_count = 5;
  706. /*
  707. * explicit use of dev_xxx() instead of netdev_xxx() here:
  708. * information displayed are related to the device itself, not
  709. * to the canx netdevices.
  710. */
  711. err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
  712. PCAN_USBPRO_INFO_FW,
  713. &fi, sizeof(fi));
  714. if (err) {
  715. kfree(usb_if);
  716. dev_err(dev->netdev->dev.parent,
  717. "unable to read %s firmware info (err %d)\n",
  718. pcan_usb_pro.name, err);
  719. return err;
  720. }
  721. err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
  722. PCAN_USBPRO_INFO_BL,
  723. &bi, sizeof(bi));
  724. if (err) {
  725. kfree(usb_if);
  726. dev_err(dev->netdev->dev.parent,
  727. "unable to read %s bootloader info (err %d)\n",
  728. pcan_usb_pro.name, err);
  729. return err;
  730. }
  731. dev_info(dev->netdev->dev.parent,
  732. "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
  733. pcan_usb_pro.name,
  734. bi.hw_rev, bi.serial_num_hi, bi.serial_num_lo,
  735. pcan_usb_pro.ctrl_count);
  736. /* tell the device the can driver is running */
  737. pcan_usb_pro_drv_loaded(dev, 1);
  738. } else {
  739. usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
  740. }
  741. pdev->usb_if = usb_if;
  742. usb_if->dev[dev->ctrl_idx] = dev;
  743. /* set LED in default state (end of init phase) */
  744. pcan_usb_pro_set_led(dev, 0, 1);
  745. return 0;
  746. }
  747. static void pcan_usb_pro_exit(struct peak_usb_device *dev)
  748. {
  749. struct pcan_usb_pro_device *pdev =
  750. container_of(dev, struct pcan_usb_pro_device, dev);
  751. /*
  752. * when rmmod called before unplug and if down, should reset things
  753. * before leaving
  754. */
  755. if (dev->can.state != CAN_STATE_STOPPED) {
  756. /* set bus off on the corresponding channel */
  757. pcan_usb_pro_set_bus(dev, 0);
  758. }
  759. /* if channel #0 (only) */
  760. if (dev->ctrl_idx == 0) {
  761. /* turn off calibration message if any device were opened */
  762. if (pdev->usb_if->dev_opened_count > 0)
  763. pcan_usb_pro_set_ts(dev, 0);
  764. /* tell the PCAN-USB Pro device the driver is being unloaded */
  765. pcan_usb_pro_drv_loaded(dev, 0);
  766. }
  767. }
  768. /*
  769. * called when PCAN-USB Pro adapter is unplugged
  770. */
  771. static void pcan_usb_pro_free(struct peak_usb_device *dev)
  772. {
  773. /* last device: can free pcan_usb_pro_interface object now */
  774. if (!dev->prev_siblings && !dev->next_siblings)
  775. kfree(pcan_usb_pro_dev_if(dev));
  776. }
  777. /*
  778. * probe function for new PCAN-USB Pro usb interface
  779. */
  780. static int pcan_usb_pro_probe(struct usb_interface *intf)
  781. {
  782. struct usb_host_interface *if_desc;
  783. int i;
  784. if_desc = intf->altsetting;
  785. /* check interface endpoint addresses */
  786. for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
  787. struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
  788. /*
  789. * below is the list of valid ep addreses. Any other ep address
  790. * is considered as not-CAN interface address => no dev created
  791. */
  792. switch (ep->bEndpointAddress) {
  793. case PCAN_USBPRO_EP_CMDOUT:
  794. case PCAN_USBPRO_EP_CMDIN:
  795. case PCAN_USBPRO_EP_MSGOUT_0:
  796. case PCAN_USBPRO_EP_MSGOUT_1:
  797. case PCAN_USBPRO_EP_MSGIN:
  798. case PCAN_USBPRO_EP_UNUSED:
  799. break;
  800. default:
  801. return -ENODEV;
  802. }
  803. }
  804. return 0;
  805. }
  806. /*
  807. * describe the PCAN-USB Pro adapter
  808. */
  809. struct peak_usb_adapter pcan_usb_pro = {
  810. .name = "PCAN-USB Pro",
  811. .device_id = PCAN_USBPRO_PRODUCT_ID,
  812. .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
  813. .clock = {
  814. .freq = PCAN_USBPRO_CRYSTAL_HZ,
  815. },
  816. .bittiming_const = {
  817. .name = "pcan_usb_pro",
  818. .tseg1_min = 1,
  819. .tseg1_max = 16,
  820. .tseg2_min = 1,
  821. .tseg2_max = 8,
  822. .sjw_max = 4,
  823. .brp_min = 1,
  824. .brp_max = 1024,
  825. .brp_inc = 1,
  826. },
  827. /* size of device private data */
  828. .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
  829. /* timestamps usage */
  830. .ts_used_bits = 32,
  831. .ts_period = 1000000, /* calibration period in ts. */
  832. .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
  833. .us_per_ts_shift = 0,
  834. /* give here messages in/out endpoints */
  835. .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
  836. .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
  837. /* size of rx/tx usb buffers */
  838. .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
  839. .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
  840. /* device callbacks */
  841. .intf_probe = pcan_usb_pro_probe,
  842. .dev_init = pcan_usb_pro_init,
  843. .dev_exit = pcan_usb_pro_exit,
  844. .dev_free = pcan_usb_pro_free,
  845. .dev_set_bus = pcan_usb_pro_set_bus,
  846. .dev_set_bittiming = pcan_usb_pro_set_bittiming,
  847. .dev_get_device_id = pcan_usb_pro_get_device_id,
  848. .dev_decode_buf = pcan_usb_pro_decode_buf,
  849. .dev_encode_msg = pcan_usb_pro_encode_msg,
  850. .dev_start = pcan_usb_pro_start,
  851. .dev_stop = pcan_usb_pro_stop,
  852. .dev_restart_async = pcan_usb_pro_restart_async,
  853. };