pcan_usb_pro.c 27 KB

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