pcan_usb_pro.c 26 KB

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