pcan_usb_pro.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. dev_err(dev->netdev->dev.parent,
  712. "unable to read %s firmware info (err %d)\n",
  713. pcan_usb_pro.name, err);
  714. return err;
  715. }
  716. err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO,
  717. PCAN_USBPRO_INFO_BL,
  718. &bi, sizeof(bi));
  719. if (err) {
  720. dev_err(dev->netdev->dev.parent,
  721. "unable to read %s bootloader info (err %d)\n",
  722. pcan_usb_pro.name, err);
  723. return err;
  724. }
  725. dev_info(dev->netdev->dev.parent,
  726. "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n",
  727. pcan_usb_pro.name,
  728. bi.hw_rev, bi.serial_num_hi, bi.serial_num_lo,
  729. pcan_usb_pro.ctrl_count);
  730. /* tell the device the can driver is running */
  731. pcan_usb_pro_drv_loaded(dev, 1);
  732. } else {
  733. usb_if = pcan_usb_pro_dev_if(dev->prev_siblings);
  734. }
  735. pdev->usb_if = usb_if;
  736. usb_if->dev[dev->ctrl_idx] = dev;
  737. /* set LED in default state (end of init phase) */
  738. pcan_usb_pro_set_led(dev, 0, 1);
  739. return 0;
  740. }
  741. static void pcan_usb_pro_exit(struct peak_usb_device *dev)
  742. {
  743. struct pcan_usb_pro_device *pdev =
  744. container_of(dev, struct pcan_usb_pro_device, dev);
  745. /*
  746. * when rmmod called before unplug and if down, should reset things
  747. * before leaving
  748. */
  749. if (dev->can.state != CAN_STATE_STOPPED) {
  750. /* set bus off on the corresponding channel */
  751. pcan_usb_pro_set_bus(dev, 0);
  752. }
  753. /* if channel #0 (only) */
  754. if (dev->ctrl_idx == 0) {
  755. /* turn off calibration message if any device were opened */
  756. if (pdev->usb_if->dev_opened_count > 0)
  757. pcan_usb_pro_set_ts(dev, 0);
  758. /* tell the PCAN-USB Pro device the driver is being unloaded */
  759. pcan_usb_pro_drv_loaded(dev, 0);
  760. }
  761. }
  762. /*
  763. * called when PCAN-USB Pro adapter is unplugged
  764. */
  765. static void pcan_usb_pro_free(struct peak_usb_device *dev)
  766. {
  767. /* last device: can free pcan_usb_pro_interface object now */
  768. if (!dev->prev_siblings && !dev->next_siblings)
  769. kfree(pcan_usb_pro_dev_if(dev));
  770. }
  771. /*
  772. * probe function for new PCAN-USB Pro usb interface
  773. */
  774. static int pcan_usb_pro_probe(struct usb_interface *intf)
  775. {
  776. struct usb_host_interface *if_desc;
  777. int i;
  778. if_desc = intf->altsetting;
  779. /* check interface endpoint addresses */
  780. for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
  781. struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
  782. /*
  783. * below is the list of valid ep addreses. Any other ep address
  784. * is considered as not-CAN interface address => no dev created
  785. */
  786. switch (ep->bEndpointAddress) {
  787. case PCAN_USBPRO_EP_CMDOUT:
  788. case PCAN_USBPRO_EP_CMDIN:
  789. case PCAN_USBPRO_EP_MSGOUT_0:
  790. case PCAN_USBPRO_EP_MSGOUT_1:
  791. case PCAN_USBPRO_EP_MSGIN:
  792. case PCAN_USBPRO_EP_UNUSED:
  793. break;
  794. default:
  795. return -ENODEV;
  796. }
  797. }
  798. return 0;
  799. }
  800. /*
  801. * describe the PCAN-USB Pro adapter
  802. */
  803. struct peak_usb_adapter pcan_usb_pro = {
  804. .name = "PCAN-USB Pro",
  805. .device_id = PCAN_USBPRO_PRODUCT_ID,
  806. .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT,
  807. .clock = {
  808. .freq = PCAN_USBPRO_CRYSTAL_HZ,
  809. },
  810. .bittiming_const = {
  811. .name = "pcan_usb_pro",
  812. .tseg1_min = 1,
  813. .tseg1_max = 16,
  814. .tseg2_min = 1,
  815. .tseg2_max = 8,
  816. .sjw_max = 4,
  817. .brp_min = 1,
  818. .brp_max = 1024,
  819. .brp_inc = 1,
  820. },
  821. /* size of device private data */
  822. .sizeof_dev_private = sizeof(struct pcan_usb_pro_device),
  823. /* timestamps usage */
  824. .ts_used_bits = 32,
  825. .ts_period = 1000000, /* calibration period in ts. */
  826. .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */
  827. .us_per_ts_shift = 0,
  828. /* give here messages in/out endpoints */
  829. .ep_msg_in = PCAN_USBPRO_EP_MSGIN,
  830. .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1},
  831. /* size of rx/tx usb buffers */
  832. .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE,
  833. .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE,
  834. /* device callbacks */
  835. .intf_probe = pcan_usb_pro_probe,
  836. .dev_init = pcan_usb_pro_init,
  837. .dev_exit = pcan_usb_pro_exit,
  838. .dev_free = pcan_usb_pro_free,
  839. .dev_set_bus = pcan_usb_pro_set_bus,
  840. .dev_set_bittiming = pcan_usb_pro_set_bittiming,
  841. .dev_get_device_id = pcan_usb_pro_get_device_id,
  842. .dev_decode_buf = pcan_usb_pro_decode_buf,
  843. .dev_encode_msg = pcan_usb_pro_encode_msg,
  844. .dev_start = pcan_usb_pro_start,
  845. .dev_stop = pcan_usb_pro_stop,
  846. .dev_restart_async = pcan_usb_pro_restart_async,
  847. };