usb_8dev.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CAN driver for "8 devices" USB2CAN converter
  4. *
  5. * Copyright (C) 2012 Bernd Krumboeck (krumboeck@universalnet.at)
  6. *
  7. * This driver is inspired by the 3.2.0 version of drivers/net/can/usb/ems_usb.c
  8. * and drivers/net/can/usb/esd_usb2.c
  9. *
  10. * Many thanks to Gerhard Bertelsmann (info@gerhard-bertelsmann.de)
  11. * for testing and fixing this driver. Also many thanks to "8 devices",
  12. * who were very cooperative and answered my questions.
  13. */
  14. #include <linux/signal.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/usb.h>
  19. #include <linux/can.h>
  20. #include <linux/can/dev.h>
  21. #include <linux/can/error.h>
  22. #include <linux/can/led.h>
  23. /* driver constants */
  24. #define MAX_RX_URBS 20
  25. #define MAX_TX_URBS 20
  26. #define RX_BUFFER_SIZE 64
  27. /* vendor and product id */
  28. #define USB_8DEV_VENDOR_ID 0x0483
  29. #define USB_8DEV_PRODUCT_ID 0x1234
  30. /* endpoints */
  31. enum usb_8dev_endpoint {
  32. USB_8DEV_ENDP_DATA_RX = 1,
  33. USB_8DEV_ENDP_DATA_TX,
  34. USB_8DEV_ENDP_CMD_RX,
  35. USB_8DEV_ENDP_CMD_TX
  36. };
  37. /* device CAN clock */
  38. #define USB_8DEV_ABP_CLOCK 32000000
  39. /* setup flags */
  40. #define USB_8DEV_SILENT 0x01
  41. #define USB_8DEV_LOOPBACK 0x02
  42. #define USB_8DEV_DISABLE_AUTO_RESTRANS 0x04
  43. #define USB_8DEV_STATUS_FRAME 0x08
  44. /* commands */
  45. enum usb_8dev_cmd {
  46. USB_8DEV_RESET = 1,
  47. USB_8DEV_OPEN,
  48. USB_8DEV_CLOSE,
  49. USB_8DEV_SET_SPEED,
  50. USB_8DEV_SET_MASK_FILTER,
  51. USB_8DEV_GET_STATUS,
  52. USB_8DEV_GET_STATISTICS,
  53. USB_8DEV_GET_SERIAL,
  54. USB_8DEV_GET_SOFTW_VER,
  55. USB_8DEV_GET_HARDW_VER,
  56. USB_8DEV_RESET_TIMESTAMP,
  57. USB_8DEV_GET_SOFTW_HARDW_VER
  58. };
  59. /* command options */
  60. #define USB_8DEV_BAUD_MANUAL 0x09
  61. #define USB_8DEV_CMD_START 0x11
  62. #define USB_8DEV_CMD_END 0x22
  63. #define USB_8DEV_CMD_SUCCESS 0
  64. #define USB_8DEV_CMD_ERROR 255
  65. #define USB_8DEV_CMD_TIMEOUT 1000
  66. /* frames */
  67. #define USB_8DEV_DATA_START 0x55
  68. #define USB_8DEV_DATA_END 0xAA
  69. #define USB_8DEV_TYPE_CAN_FRAME 0
  70. #define USB_8DEV_TYPE_ERROR_FRAME 3
  71. #define USB_8DEV_EXTID 0x01
  72. #define USB_8DEV_RTR 0x02
  73. #define USB_8DEV_ERR_FLAG 0x04
  74. /* status */
  75. #define USB_8DEV_STATUSMSG_OK 0x00 /* Normal condition. */
  76. #define USB_8DEV_STATUSMSG_OVERRUN 0x01 /* Overrun occured when sending */
  77. #define USB_8DEV_STATUSMSG_BUSLIGHT 0x02 /* Error counter has reached 96 */
  78. #define USB_8DEV_STATUSMSG_BUSHEAVY 0x03 /* Error count. has reached 128 */
  79. #define USB_8DEV_STATUSMSG_BUSOFF 0x04 /* Device is in BUSOFF */
  80. #define USB_8DEV_STATUSMSG_STUFF 0x20 /* Stuff Error */
  81. #define USB_8DEV_STATUSMSG_FORM 0x21 /* Form Error */
  82. #define USB_8DEV_STATUSMSG_ACK 0x23 /* Ack Error */
  83. #define USB_8DEV_STATUSMSG_BIT0 0x24 /* Bit1 Error */
  84. #define USB_8DEV_STATUSMSG_BIT1 0x25 /* Bit0 Error */
  85. #define USB_8DEV_STATUSMSG_CRC 0x27 /* CRC Error */
  86. #define USB_8DEV_RP_MASK 0x7F /* Mask for Receive Error Bit */
  87. /* table of devices that work with this driver */
  88. static const struct usb_device_id usb_8dev_table[] = {
  89. { USB_DEVICE(USB_8DEV_VENDOR_ID, USB_8DEV_PRODUCT_ID) },
  90. { } /* Terminating entry */
  91. };
  92. MODULE_DEVICE_TABLE(usb, usb_8dev_table);
  93. struct usb_8dev_tx_urb_context {
  94. struct usb_8dev_priv *priv;
  95. u32 echo_index;
  96. u8 dlc;
  97. };
  98. /* Structure to hold all of our device specific stuff */
  99. struct usb_8dev_priv {
  100. struct can_priv can; /* must be the first member */
  101. struct sk_buff *echo_skb[MAX_TX_URBS];
  102. struct usb_device *udev;
  103. struct net_device *netdev;
  104. atomic_t active_tx_urbs;
  105. struct usb_anchor tx_submitted;
  106. struct usb_8dev_tx_urb_context tx_contexts[MAX_TX_URBS];
  107. struct usb_anchor rx_submitted;
  108. struct can_berr_counter bec;
  109. u8 *cmd_msg_buffer;
  110. struct mutex usb_8dev_cmd_lock;
  111. void *rxbuf[MAX_RX_URBS];
  112. dma_addr_t rxbuf_dma[MAX_RX_URBS];
  113. };
  114. /* tx frame */
  115. struct __packed usb_8dev_tx_msg {
  116. u8 begin;
  117. u8 flags; /* RTR and EXT_ID flag */
  118. __be32 id; /* upper 3 bits not used */
  119. u8 dlc; /* data length code 0-8 bytes */
  120. u8 data[8]; /* 64-bit data */
  121. u8 end;
  122. };
  123. /* rx frame */
  124. struct __packed usb_8dev_rx_msg {
  125. u8 begin;
  126. u8 type; /* frame type */
  127. u8 flags; /* RTR and EXT_ID flag */
  128. __be32 id; /* upper 3 bits not used */
  129. u8 dlc; /* data length code 0-8 bytes */
  130. u8 data[8]; /* 64-bit data */
  131. __be32 timestamp; /* 32-bit timestamp */
  132. u8 end;
  133. };
  134. /* command frame */
  135. struct __packed usb_8dev_cmd_msg {
  136. u8 begin;
  137. u8 channel; /* unkown - always 0 */
  138. u8 command; /* command to execute */
  139. u8 opt1; /* optional parameter / return value */
  140. u8 opt2; /* optional parameter 2 */
  141. u8 data[10]; /* optional parameter and data */
  142. u8 end;
  143. };
  144. static int usb_8dev_send_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size)
  145. {
  146. int actual_length;
  147. return usb_bulk_msg(priv->udev,
  148. usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_TX),
  149. msg, size, &actual_length, USB_8DEV_CMD_TIMEOUT);
  150. }
  151. static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size,
  152. int *actual_length)
  153. {
  154. return usb_bulk_msg(priv->udev,
  155. usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_RX),
  156. msg, size, actual_length, USB_8DEV_CMD_TIMEOUT);
  157. }
  158. /* Send command to device and receive result.
  159. * Command was successful when opt1 = 0.
  160. */
  161. static int usb_8dev_send_cmd(struct usb_8dev_priv *priv,
  162. struct usb_8dev_cmd_msg *out,
  163. struct usb_8dev_cmd_msg *in)
  164. {
  165. int err;
  166. int num_bytes_read;
  167. struct net_device *netdev;
  168. netdev = priv->netdev;
  169. out->begin = USB_8DEV_CMD_START;
  170. out->end = USB_8DEV_CMD_END;
  171. mutex_lock(&priv->usb_8dev_cmd_lock);
  172. memcpy(priv->cmd_msg_buffer, out,
  173. sizeof(struct usb_8dev_cmd_msg));
  174. err = usb_8dev_send_cmd_msg(priv, priv->cmd_msg_buffer,
  175. sizeof(struct usb_8dev_cmd_msg));
  176. if (err < 0) {
  177. netdev_err(netdev, "sending command message failed\n");
  178. goto failed;
  179. }
  180. err = usb_8dev_wait_cmd_msg(priv, priv->cmd_msg_buffer,
  181. sizeof(struct usb_8dev_cmd_msg),
  182. &num_bytes_read);
  183. if (err < 0) {
  184. netdev_err(netdev, "no command message answer\n");
  185. goto failed;
  186. }
  187. memcpy(in, priv->cmd_msg_buffer, sizeof(struct usb_8dev_cmd_msg));
  188. if (in->begin != USB_8DEV_CMD_START || in->end != USB_8DEV_CMD_END ||
  189. num_bytes_read != 16 || in->opt1 != 0)
  190. err = -EPROTO;
  191. failed:
  192. mutex_unlock(&priv->usb_8dev_cmd_lock);
  193. return err;
  194. }
  195. /* Send open command to device */
  196. static int usb_8dev_cmd_open(struct usb_8dev_priv *priv)
  197. {
  198. struct can_bittiming *bt = &priv->can.bittiming;
  199. struct usb_8dev_cmd_msg outmsg;
  200. struct usb_8dev_cmd_msg inmsg;
  201. u32 ctrlmode = priv->can.ctrlmode;
  202. u32 flags = USB_8DEV_STATUS_FRAME;
  203. __be32 beflags;
  204. __be16 bebrp;
  205. memset(&outmsg, 0, sizeof(outmsg));
  206. outmsg.command = USB_8DEV_OPEN;
  207. outmsg.opt1 = USB_8DEV_BAUD_MANUAL;
  208. outmsg.data[0] = bt->prop_seg + bt->phase_seg1;
  209. outmsg.data[1] = bt->phase_seg2;
  210. outmsg.data[2] = bt->sjw;
  211. /* BRP */
  212. bebrp = cpu_to_be16((u16)bt->brp);
  213. memcpy(&outmsg.data[3], &bebrp, sizeof(bebrp));
  214. /* flags */
  215. if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
  216. flags |= USB_8DEV_LOOPBACK;
  217. if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
  218. flags |= USB_8DEV_SILENT;
  219. if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  220. flags |= USB_8DEV_DISABLE_AUTO_RESTRANS;
  221. beflags = cpu_to_be32(flags);
  222. memcpy(&outmsg.data[5], &beflags, sizeof(beflags));
  223. return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
  224. }
  225. /* Send close command to device */
  226. static int usb_8dev_cmd_close(struct usb_8dev_priv *priv)
  227. {
  228. struct usb_8dev_cmd_msg inmsg;
  229. struct usb_8dev_cmd_msg outmsg = {
  230. .channel = 0,
  231. .command = USB_8DEV_CLOSE,
  232. .opt1 = 0,
  233. .opt2 = 0
  234. };
  235. return usb_8dev_send_cmd(priv, &outmsg, &inmsg);
  236. }
  237. /* Get firmware and hardware version */
  238. static int usb_8dev_cmd_version(struct usb_8dev_priv *priv, u32 *res)
  239. {
  240. struct usb_8dev_cmd_msg inmsg;
  241. struct usb_8dev_cmd_msg outmsg = {
  242. .channel = 0,
  243. .command = USB_8DEV_GET_SOFTW_HARDW_VER,
  244. .opt1 = 0,
  245. .opt2 = 0
  246. };
  247. int err = usb_8dev_send_cmd(priv, &outmsg, &inmsg);
  248. if (err)
  249. return err;
  250. *res = be32_to_cpup((__be32 *)inmsg.data);
  251. return err;
  252. }
  253. /* Set network device mode
  254. *
  255. * Maybe we should leave this function empty, because the device
  256. * set mode variable with open command.
  257. */
  258. static int usb_8dev_set_mode(struct net_device *netdev, enum can_mode mode)
  259. {
  260. struct usb_8dev_priv *priv = netdev_priv(netdev);
  261. int err = 0;
  262. switch (mode) {
  263. case CAN_MODE_START:
  264. err = usb_8dev_cmd_open(priv);
  265. if (err)
  266. netdev_warn(netdev, "couldn't start device");
  267. break;
  268. default:
  269. return -EOPNOTSUPP;
  270. }
  271. return err;
  272. }
  273. /* Read error/status frames */
  274. static void usb_8dev_rx_err_msg(struct usb_8dev_priv *priv,
  275. struct usb_8dev_rx_msg *msg)
  276. {
  277. struct can_frame *cf;
  278. struct sk_buff *skb;
  279. struct net_device_stats *stats = &priv->netdev->stats;
  280. /* Error message:
  281. * byte 0: Status
  282. * byte 1: bit 7: Receive Passive
  283. * byte 1: bit 0-6: Receive Error Counter
  284. * byte 2: Transmit Error Counter
  285. * byte 3: Always 0 (maybe reserved for future use)
  286. */
  287. u8 state = msg->data[0];
  288. u8 rxerr = msg->data[1] & USB_8DEV_RP_MASK;
  289. u8 txerr = msg->data[2];
  290. int rx_errors = 0;
  291. int tx_errors = 0;
  292. skb = alloc_can_err_skb(priv->netdev, &cf);
  293. if (!skb)
  294. return;
  295. switch (state) {
  296. case USB_8DEV_STATUSMSG_OK:
  297. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  298. cf->can_id |= CAN_ERR_PROT;
  299. cf->data[2] = CAN_ERR_PROT_ACTIVE;
  300. break;
  301. case USB_8DEV_STATUSMSG_BUSOFF:
  302. priv->can.state = CAN_STATE_BUS_OFF;
  303. cf->can_id |= CAN_ERR_BUSOFF;
  304. priv->can.can_stats.bus_off++;
  305. can_bus_off(priv->netdev);
  306. break;
  307. case USB_8DEV_STATUSMSG_OVERRUN:
  308. case USB_8DEV_STATUSMSG_BUSLIGHT:
  309. case USB_8DEV_STATUSMSG_BUSHEAVY:
  310. cf->can_id |= CAN_ERR_CRTL;
  311. break;
  312. default:
  313. priv->can.state = CAN_STATE_ERROR_WARNING;
  314. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  315. priv->can.can_stats.bus_error++;
  316. break;
  317. }
  318. switch (state) {
  319. case USB_8DEV_STATUSMSG_OK:
  320. case USB_8DEV_STATUSMSG_BUSOFF:
  321. break;
  322. case USB_8DEV_STATUSMSG_ACK:
  323. cf->can_id |= CAN_ERR_ACK;
  324. tx_errors = 1;
  325. break;
  326. case USB_8DEV_STATUSMSG_CRC:
  327. cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
  328. rx_errors = 1;
  329. break;
  330. case USB_8DEV_STATUSMSG_BIT0:
  331. cf->data[2] |= CAN_ERR_PROT_BIT0;
  332. tx_errors = 1;
  333. break;
  334. case USB_8DEV_STATUSMSG_BIT1:
  335. cf->data[2] |= CAN_ERR_PROT_BIT1;
  336. tx_errors = 1;
  337. break;
  338. case USB_8DEV_STATUSMSG_FORM:
  339. cf->data[2] |= CAN_ERR_PROT_FORM;
  340. rx_errors = 1;
  341. break;
  342. case USB_8DEV_STATUSMSG_STUFF:
  343. cf->data[2] |= CAN_ERR_PROT_STUFF;
  344. rx_errors = 1;
  345. break;
  346. case USB_8DEV_STATUSMSG_OVERRUN:
  347. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  348. stats->rx_over_errors++;
  349. rx_errors = 1;
  350. break;
  351. case USB_8DEV_STATUSMSG_BUSLIGHT:
  352. priv->can.state = CAN_STATE_ERROR_WARNING;
  353. cf->data[1] = (txerr > rxerr) ?
  354. CAN_ERR_CRTL_TX_WARNING :
  355. CAN_ERR_CRTL_RX_WARNING;
  356. priv->can.can_stats.error_warning++;
  357. break;
  358. case USB_8DEV_STATUSMSG_BUSHEAVY:
  359. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  360. cf->data[1] = (txerr > rxerr) ?
  361. CAN_ERR_CRTL_TX_PASSIVE :
  362. CAN_ERR_CRTL_RX_PASSIVE;
  363. priv->can.can_stats.error_passive++;
  364. break;
  365. default:
  366. netdev_warn(priv->netdev,
  367. "Unknown status/error message (%d)\n", state);
  368. break;
  369. }
  370. if (tx_errors) {
  371. cf->data[2] |= CAN_ERR_PROT_TX;
  372. stats->tx_errors++;
  373. }
  374. if (rx_errors)
  375. stats->rx_errors++;
  376. cf->data[6] = txerr;
  377. cf->data[7] = rxerr;
  378. priv->bec.txerr = txerr;
  379. priv->bec.rxerr = rxerr;
  380. stats->rx_packets++;
  381. stats->rx_bytes += cf->can_dlc;
  382. netif_rx(skb);
  383. }
  384. /* Read data and status frames */
  385. static void usb_8dev_rx_can_msg(struct usb_8dev_priv *priv,
  386. struct usb_8dev_rx_msg *msg)
  387. {
  388. struct can_frame *cf;
  389. struct sk_buff *skb;
  390. struct net_device_stats *stats = &priv->netdev->stats;
  391. if (msg->type == USB_8DEV_TYPE_ERROR_FRAME &&
  392. msg->flags == USB_8DEV_ERR_FLAG) {
  393. usb_8dev_rx_err_msg(priv, msg);
  394. } else if (msg->type == USB_8DEV_TYPE_CAN_FRAME) {
  395. skb = alloc_can_skb(priv->netdev, &cf);
  396. if (!skb)
  397. return;
  398. cf->can_id = be32_to_cpu(msg->id);
  399. cf->can_dlc = get_can_dlc(msg->dlc & 0xF);
  400. if (msg->flags & USB_8DEV_EXTID)
  401. cf->can_id |= CAN_EFF_FLAG;
  402. if (msg->flags & USB_8DEV_RTR)
  403. cf->can_id |= CAN_RTR_FLAG;
  404. else
  405. memcpy(cf->data, msg->data, cf->can_dlc);
  406. stats->rx_packets++;
  407. stats->rx_bytes += cf->can_dlc;
  408. netif_rx(skb);
  409. can_led_event(priv->netdev, CAN_LED_EVENT_RX);
  410. } else {
  411. netdev_warn(priv->netdev, "frame type %d unknown",
  412. msg->type);
  413. }
  414. }
  415. /* Callback for reading data from device
  416. *
  417. * Check urb status, call read function and resubmit urb read operation.
  418. */
  419. static void usb_8dev_read_bulk_callback(struct urb *urb)
  420. {
  421. struct usb_8dev_priv *priv = urb->context;
  422. struct net_device *netdev;
  423. int retval;
  424. int pos = 0;
  425. netdev = priv->netdev;
  426. if (!netif_device_present(netdev))
  427. return;
  428. switch (urb->status) {
  429. case 0: /* success */
  430. break;
  431. case -ENOENT:
  432. case -EPIPE:
  433. case -EPROTO:
  434. case -ESHUTDOWN:
  435. return;
  436. default:
  437. netdev_info(netdev, "Rx URB aborted (%d)\n",
  438. urb->status);
  439. goto resubmit_urb;
  440. }
  441. while (pos < urb->actual_length) {
  442. struct usb_8dev_rx_msg *msg;
  443. if (pos + sizeof(struct usb_8dev_rx_msg) > urb->actual_length) {
  444. netdev_err(priv->netdev, "format error\n");
  445. break;
  446. }
  447. msg = (struct usb_8dev_rx_msg *)(urb->transfer_buffer + pos);
  448. usb_8dev_rx_can_msg(priv, msg);
  449. pos += sizeof(struct usb_8dev_rx_msg);
  450. }
  451. resubmit_urb:
  452. usb_fill_bulk_urb(urb, priv->udev,
  453. usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX),
  454. urb->transfer_buffer, RX_BUFFER_SIZE,
  455. usb_8dev_read_bulk_callback, priv);
  456. retval = usb_submit_urb(urb, GFP_ATOMIC);
  457. if (retval == -ENODEV)
  458. netif_device_detach(netdev);
  459. else if (retval)
  460. netdev_err(netdev,
  461. "failed resubmitting read bulk urb: %d\n", retval);
  462. }
  463. /* Callback handler for write operations
  464. *
  465. * Free allocated buffers, check transmit status and
  466. * calculate statistic.
  467. */
  468. static void usb_8dev_write_bulk_callback(struct urb *urb)
  469. {
  470. struct usb_8dev_tx_urb_context *context = urb->context;
  471. struct usb_8dev_priv *priv;
  472. struct net_device *netdev;
  473. BUG_ON(!context);
  474. priv = context->priv;
  475. netdev = priv->netdev;
  476. /* free up our allocated buffer */
  477. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  478. urb->transfer_buffer, urb->transfer_dma);
  479. atomic_dec(&priv->active_tx_urbs);
  480. if (!netif_device_present(netdev))
  481. return;
  482. if (urb->status)
  483. netdev_info(netdev, "Tx URB aborted (%d)\n",
  484. urb->status);
  485. netdev->stats.tx_packets++;
  486. netdev->stats.tx_bytes += context->dlc;
  487. can_get_echo_skb(netdev, context->echo_index);
  488. can_led_event(netdev, CAN_LED_EVENT_TX);
  489. /* Release context */
  490. context->echo_index = MAX_TX_URBS;
  491. netif_wake_queue(netdev);
  492. }
  493. /* Send data to device */
  494. static netdev_tx_t usb_8dev_start_xmit(struct sk_buff *skb,
  495. struct net_device *netdev)
  496. {
  497. struct usb_8dev_priv *priv = netdev_priv(netdev);
  498. struct net_device_stats *stats = &netdev->stats;
  499. struct can_frame *cf = (struct can_frame *) skb->data;
  500. struct usb_8dev_tx_msg *msg;
  501. struct urb *urb;
  502. struct usb_8dev_tx_urb_context *context = NULL;
  503. u8 *buf;
  504. int i, err;
  505. size_t size = sizeof(struct usb_8dev_tx_msg);
  506. if (can_dropped_invalid_skb(netdev, skb))
  507. return NETDEV_TX_OK;
  508. /* create a URB, and a buffer for it, and copy the data to the URB */
  509. urb = usb_alloc_urb(0, GFP_ATOMIC);
  510. if (!urb)
  511. goto nomem;
  512. buf = usb_alloc_coherent(priv->udev, size, GFP_ATOMIC,
  513. &urb->transfer_dma);
  514. if (!buf) {
  515. netdev_err(netdev, "No memory left for USB buffer\n");
  516. goto nomembuf;
  517. }
  518. memset(buf, 0, size);
  519. msg = (struct usb_8dev_tx_msg *)buf;
  520. msg->begin = USB_8DEV_DATA_START;
  521. msg->flags = 0x00;
  522. if (cf->can_id & CAN_RTR_FLAG)
  523. msg->flags |= USB_8DEV_RTR;
  524. if (cf->can_id & CAN_EFF_FLAG)
  525. msg->flags |= USB_8DEV_EXTID;
  526. msg->id = cpu_to_be32(cf->can_id & CAN_ERR_MASK);
  527. msg->dlc = cf->can_dlc;
  528. memcpy(msg->data, cf->data, cf->can_dlc);
  529. msg->end = USB_8DEV_DATA_END;
  530. for (i = 0; i < MAX_TX_URBS; i++) {
  531. if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
  532. context = &priv->tx_contexts[i];
  533. break;
  534. }
  535. }
  536. /* May never happen! When this happens we'd more URBs in flight as
  537. * allowed (MAX_TX_URBS).
  538. */
  539. if (!context)
  540. goto nofreecontext;
  541. context->priv = priv;
  542. context->echo_index = i;
  543. context->dlc = cf->can_dlc;
  544. usb_fill_bulk_urb(urb, priv->udev,
  545. usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_TX),
  546. buf, size, usb_8dev_write_bulk_callback, context);
  547. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  548. usb_anchor_urb(urb, &priv->tx_submitted);
  549. can_put_echo_skb(skb, netdev, context->echo_index);
  550. atomic_inc(&priv->active_tx_urbs);
  551. err = usb_submit_urb(urb, GFP_ATOMIC);
  552. if (unlikely(err))
  553. goto failed;
  554. else if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
  555. /* Slow down tx path */
  556. netif_stop_queue(netdev);
  557. /* Release our reference to this URB, the USB core will eventually free
  558. * it entirely.
  559. */
  560. usb_free_urb(urb);
  561. return NETDEV_TX_OK;
  562. nofreecontext:
  563. usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
  564. usb_free_urb(urb);
  565. netdev_warn(netdev, "couldn't find free context");
  566. return NETDEV_TX_BUSY;
  567. failed:
  568. can_free_echo_skb(netdev, context->echo_index);
  569. usb_unanchor_urb(urb);
  570. usb_free_coherent(priv->udev, size, buf, urb->transfer_dma);
  571. atomic_dec(&priv->active_tx_urbs);
  572. if (err == -ENODEV)
  573. netif_device_detach(netdev);
  574. else
  575. netdev_warn(netdev, "failed tx_urb %d\n", err);
  576. nomembuf:
  577. usb_free_urb(urb);
  578. nomem:
  579. dev_kfree_skb(skb);
  580. stats->tx_dropped++;
  581. return NETDEV_TX_OK;
  582. }
  583. static int usb_8dev_get_berr_counter(const struct net_device *netdev,
  584. struct can_berr_counter *bec)
  585. {
  586. struct usb_8dev_priv *priv = netdev_priv(netdev);
  587. bec->txerr = priv->bec.txerr;
  588. bec->rxerr = priv->bec.rxerr;
  589. return 0;
  590. }
  591. /* Start USB device */
  592. static int usb_8dev_start(struct usb_8dev_priv *priv)
  593. {
  594. struct net_device *netdev = priv->netdev;
  595. int err, i;
  596. for (i = 0; i < MAX_RX_URBS; i++) {
  597. struct urb *urb = NULL;
  598. u8 *buf;
  599. dma_addr_t buf_dma;
  600. /* create a URB, and a buffer for it */
  601. urb = usb_alloc_urb(0, GFP_KERNEL);
  602. if (!urb) {
  603. err = -ENOMEM;
  604. break;
  605. }
  606. buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL,
  607. &buf_dma);
  608. if (!buf) {
  609. netdev_err(netdev, "No memory left for USB buffer\n");
  610. usb_free_urb(urb);
  611. err = -ENOMEM;
  612. break;
  613. }
  614. urb->transfer_dma = buf_dma;
  615. usb_fill_bulk_urb(urb, priv->udev,
  616. usb_rcvbulkpipe(priv->udev,
  617. USB_8DEV_ENDP_DATA_RX),
  618. buf, RX_BUFFER_SIZE,
  619. usb_8dev_read_bulk_callback, priv);
  620. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  621. usb_anchor_urb(urb, &priv->rx_submitted);
  622. err = usb_submit_urb(urb, GFP_KERNEL);
  623. if (err) {
  624. usb_unanchor_urb(urb);
  625. usb_free_coherent(priv->udev, RX_BUFFER_SIZE, buf,
  626. urb->transfer_dma);
  627. usb_free_urb(urb);
  628. break;
  629. }
  630. priv->rxbuf[i] = buf;
  631. priv->rxbuf_dma[i] = buf_dma;
  632. /* Drop reference, USB core will take care of freeing it */
  633. usb_free_urb(urb);
  634. }
  635. /* Did we submit any URBs */
  636. if (i == 0) {
  637. netdev_warn(netdev, "couldn't setup read URBs\n");
  638. return err;
  639. }
  640. /* Warn if we've couldn't transmit all the URBs */
  641. if (i < MAX_RX_URBS)
  642. netdev_warn(netdev, "rx performance may be slow\n");
  643. err = usb_8dev_cmd_open(priv);
  644. if (err)
  645. goto failed;
  646. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  647. return 0;
  648. failed:
  649. if (err == -ENODEV)
  650. netif_device_detach(priv->netdev);
  651. netdev_warn(netdev, "couldn't submit control: %d\n", err);
  652. return err;
  653. }
  654. /* Open USB device */
  655. static int usb_8dev_open(struct net_device *netdev)
  656. {
  657. struct usb_8dev_priv *priv = netdev_priv(netdev);
  658. int err;
  659. /* common open */
  660. err = open_candev(netdev);
  661. if (err)
  662. return err;
  663. can_led_event(netdev, CAN_LED_EVENT_OPEN);
  664. /* finally start device */
  665. err = usb_8dev_start(priv);
  666. if (err) {
  667. if (err == -ENODEV)
  668. netif_device_detach(priv->netdev);
  669. netdev_warn(netdev, "couldn't start device: %d\n",
  670. err);
  671. close_candev(netdev);
  672. return err;
  673. }
  674. netif_start_queue(netdev);
  675. return 0;
  676. }
  677. static void unlink_all_urbs(struct usb_8dev_priv *priv)
  678. {
  679. int i;
  680. usb_kill_anchored_urbs(&priv->rx_submitted);
  681. for (i = 0; i < MAX_RX_URBS; ++i)
  682. usb_free_coherent(priv->udev, RX_BUFFER_SIZE,
  683. priv->rxbuf[i], priv->rxbuf_dma[i]);
  684. usb_kill_anchored_urbs(&priv->tx_submitted);
  685. atomic_set(&priv->active_tx_urbs, 0);
  686. for (i = 0; i < MAX_TX_URBS; i++)
  687. priv->tx_contexts[i].echo_index = MAX_TX_URBS;
  688. }
  689. /* Close USB device */
  690. static int usb_8dev_close(struct net_device *netdev)
  691. {
  692. struct usb_8dev_priv *priv = netdev_priv(netdev);
  693. int err = 0;
  694. /* Send CLOSE command to CAN controller */
  695. err = usb_8dev_cmd_close(priv);
  696. if (err)
  697. netdev_warn(netdev, "couldn't stop device");
  698. priv->can.state = CAN_STATE_STOPPED;
  699. netif_stop_queue(netdev);
  700. /* Stop polling */
  701. unlink_all_urbs(priv);
  702. close_candev(netdev);
  703. can_led_event(netdev, CAN_LED_EVENT_STOP);
  704. return err;
  705. }
  706. static const struct net_device_ops usb_8dev_netdev_ops = {
  707. .ndo_open = usb_8dev_open,
  708. .ndo_stop = usb_8dev_close,
  709. .ndo_start_xmit = usb_8dev_start_xmit,
  710. .ndo_change_mtu = can_change_mtu,
  711. };
  712. static const struct can_bittiming_const usb_8dev_bittiming_const = {
  713. .name = "usb_8dev",
  714. .tseg1_min = 1,
  715. .tseg1_max = 16,
  716. .tseg2_min = 1,
  717. .tseg2_max = 8,
  718. .sjw_max = 4,
  719. .brp_min = 1,
  720. .brp_max = 1024,
  721. .brp_inc = 1,
  722. };
  723. /* Probe USB device
  724. *
  725. * Check device and firmware.
  726. * Set supported modes and bittiming constants.
  727. * Allocate some memory.
  728. */
  729. static int usb_8dev_probe(struct usb_interface *intf,
  730. const struct usb_device_id *id)
  731. {
  732. struct net_device *netdev;
  733. struct usb_8dev_priv *priv;
  734. int i, err = -ENOMEM;
  735. u32 version;
  736. char buf[18];
  737. struct usb_device *usbdev = interface_to_usbdev(intf);
  738. /* product id looks strange, better we also check iProduct string */
  739. if (usb_string(usbdev, usbdev->descriptor.iProduct, buf,
  740. sizeof(buf)) > 0 && strcmp(buf, "USB2CAN converter")) {
  741. dev_info(&usbdev->dev, "ignoring: not an USB2CAN converter\n");
  742. return -ENODEV;
  743. }
  744. netdev = alloc_candev(sizeof(struct usb_8dev_priv), MAX_TX_URBS);
  745. if (!netdev) {
  746. dev_err(&intf->dev, "Couldn't alloc candev\n");
  747. return -ENOMEM;
  748. }
  749. priv = netdev_priv(netdev);
  750. priv->udev = usbdev;
  751. priv->netdev = netdev;
  752. priv->can.state = CAN_STATE_STOPPED;
  753. priv->can.clock.freq = USB_8DEV_ABP_CLOCK;
  754. priv->can.bittiming_const = &usb_8dev_bittiming_const;
  755. priv->can.do_set_mode = usb_8dev_set_mode;
  756. priv->can.do_get_berr_counter = usb_8dev_get_berr_counter;
  757. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  758. CAN_CTRLMODE_LISTENONLY |
  759. CAN_CTRLMODE_ONE_SHOT;
  760. netdev->netdev_ops = &usb_8dev_netdev_ops;
  761. netdev->flags |= IFF_ECHO; /* we support local echo */
  762. init_usb_anchor(&priv->rx_submitted);
  763. init_usb_anchor(&priv->tx_submitted);
  764. atomic_set(&priv->active_tx_urbs, 0);
  765. for (i = 0; i < MAX_TX_URBS; i++)
  766. priv->tx_contexts[i].echo_index = MAX_TX_URBS;
  767. priv->cmd_msg_buffer = devm_kzalloc(&intf->dev, sizeof(struct usb_8dev_cmd_msg),
  768. GFP_KERNEL);
  769. if (!priv->cmd_msg_buffer)
  770. goto cleanup_candev;
  771. usb_set_intfdata(intf, priv);
  772. SET_NETDEV_DEV(netdev, &intf->dev);
  773. mutex_init(&priv->usb_8dev_cmd_lock);
  774. err = register_candev(netdev);
  775. if (err) {
  776. netdev_err(netdev,
  777. "couldn't register CAN device: %d\n", err);
  778. goto cleanup_candev;
  779. }
  780. err = usb_8dev_cmd_version(priv, &version);
  781. if (err) {
  782. netdev_err(netdev, "can't get firmware version\n");
  783. goto cleanup_unregister_candev;
  784. } else {
  785. netdev_info(netdev,
  786. "firmware: %d.%d, hardware: %d.%d\n",
  787. (version>>24) & 0xff, (version>>16) & 0xff,
  788. (version>>8) & 0xff, version & 0xff);
  789. }
  790. devm_can_led_init(netdev);
  791. return 0;
  792. cleanup_unregister_candev:
  793. unregister_netdev(priv->netdev);
  794. cleanup_candev:
  795. free_candev(netdev);
  796. return err;
  797. }
  798. /* Called by the usb core when driver is unloaded or device is removed */
  799. static void usb_8dev_disconnect(struct usb_interface *intf)
  800. {
  801. struct usb_8dev_priv *priv = usb_get_intfdata(intf);
  802. usb_set_intfdata(intf, NULL);
  803. if (priv) {
  804. netdev_info(priv->netdev, "device disconnected\n");
  805. unregister_netdev(priv->netdev);
  806. unlink_all_urbs(priv);
  807. free_candev(priv->netdev);
  808. }
  809. }
  810. static struct usb_driver usb_8dev_driver = {
  811. .name = "usb_8dev",
  812. .probe = usb_8dev_probe,
  813. .disconnect = usb_8dev_disconnect,
  814. .id_table = usb_8dev_table,
  815. };
  816. module_usb_driver(usb_8dev_driver);
  817. MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>");
  818. MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces");
  819. MODULE_LICENSE("GPL v2");